18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch> 38c2ecf20Sopenharmony_ci * Copyright (C) 2009 Wind River Systems Inc 48c2ecf20Sopenharmony_ci * Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com 58c2ecf20Sopenharmony_ci * Copyright (C) 2004 Microtronix Datacom Ltd. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public 88c2ecf20Sopenharmony_ci * License. See the file "COPYING" in the main directory of this archive 98c2ecf20Sopenharmony_ci * for more details. 108c2ecf20Sopenharmony_ci */ 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include <linux/export.h> 138c2ecf20Sopenharmony_ci#include <linux/sched.h> 148c2ecf20Sopenharmony_ci#include <linux/mm.h> 158c2ecf20Sopenharmony_ci#include <linux/slab.h> 168c2ecf20Sopenharmony_ci#include <linux/vmalloc.h> 178c2ecf20Sopenharmony_ci#include <linux/io.h> 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#include <asm/cacheflush.h> 208c2ecf20Sopenharmony_ci#include <asm/tlbflush.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_cistatic inline void remap_area_pte(pte_t *pte, unsigned long address, 238c2ecf20Sopenharmony_ci unsigned long size, unsigned long phys_addr, 248c2ecf20Sopenharmony_ci unsigned long flags) 258c2ecf20Sopenharmony_ci{ 268c2ecf20Sopenharmony_ci unsigned long end; 278c2ecf20Sopenharmony_ci unsigned long pfn; 288c2ecf20Sopenharmony_ci pgprot_t pgprot = __pgprot(_PAGE_GLOBAL | _PAGE_PRESENT | _PAGE_READ 298c2ecf20Sopenharmony_ci | _PAGE_WRITE | flags); 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci address &= ~PMD_MASK; 328c2ecf20Sopenharmony_ci end = address + size; 338c2ecf20Sopenharmony_ci if (end > PMD_SIZE) 348c2ecf20Sopenharmony_ci end = PMD_SIZE; 358c2ecf20Sopenharmony_ci if (address >= end) 368c2ecf20Sopenharmony_ci BUG(); 378c2ecf20Sopenharmony_ci pfn = PFN_DOWN(phys_addr); 388c2ecf20Sopenharmony_ci do { 398c2ecf20Sopenharmony_ci if (!pte_none(*pte)) { 408c2ecf20Sopenharmony_ci pr_err("remap_area_pte: page already exists\n"); 418c2ecf20Sopenharmony_ci BUG(); 428c2ecf20Sopenharmony_ci } 438c2ecf20Sopenharmony_ci set_pte(pte, pfn_pte(pfn, pgprot)); 448c2ecf20Sopenharmony_ci address += PAGE_SIZE; 458c2ecf20Sopenharmony_ci pfn++; 468c2ecf20Sopenharmony_ci pte++; 478c2ecf20Sopenharmony_ci } while (address && (address < end)); 488c2ecf20Sopenharmony_ci} 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_cistatic inline int remap_area_pmd(pmd_t *pmd, unsigned long address, 518c2ecf20Sopenharmony_ci unsigned long size, unsigned long phys_addr, 528c2ecf20Sopenharmony_ci unsigned long flags) 538c2ecf20Sopenharmony_ci{ 548c2ecf20Sopenharmony_ci unsigned long end; 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci address &= ~PGDIR_MASK; 578c2ecf20Sopenharmony_ci end = address + size; 588c2ecf20Sopenharmony_ci if (end > PGDIR_SIZE) 598c2ecf20Sopenharmony_ci end = PGDIR_SIZE; 608c2ecf20Sopenharmony_ci phys_addr -= address; 618c2ecf20Sopenharmony_ci if (address >= end) 628c2ecf20Sopenharmony_ci BUG(); 638c2ecf20Sopenharmony_ci do { 648c2ecf20Sopenharmony_ci pte_t *pte = pte_alloc_kernel(pmd, address); 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci if (!pte) 678c2ecf20Sopenharmony_ci return -ENOMEM; 688c2ecf20Sopenharmony_ci remap_area_pte(pte, address, end - address, address + phys_addr, 698c2ecf20Sopenharmony_ci flags); 708c2ecf20Sopenharmony_ci address = (address + PMD_SIZE) & PMD_MASK; 718c2ecf20Sopenharmony_ci pmd++; 728c2ecf20Sopenharmony_ci } while (address && (address < end)); 738c2ecf20Sopenharmony_ci return 0; 748c2ecf20Sopenharmony_ci} 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_cistatic int remap_area_pages(unsigned long address, unsigned long phys_addr, 778c2ecf20Sopenharmony_ci unsigned long size, unsigned long flags) 788c2ecf20Sopenharmony_ci{ 798c2ecf20Sopenharmony_ci int error; 808c2ecf20Sopenharmony_ci pgd_t *dir; 818c2ecf20Sopenharmony_ci unsigned long end = address + size; 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci phys_addr -= address; 848c2ecf20Sopenharmony_ci dir = pgd_offset(&init_mm, address); 858c2ecf20Sopenharmony_ci flush_cache_all(); 868c2ecf20Sopenharmony_ci if (address >= end) 878c2ecf20Sopenharmony_ci BUG(); 888c2ecf20Sopenharmony_ci do { 898c2ecf20Sopenharmony_ci p4d_t *p4d; 908c2ecf20Sopenharmony_ci pud_t *pud; 918c2ecf20Sopenharmony_ci pmd_t *pmd; 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci error = -ENOMEM; 948c2ecf20Sopenharmony_ci p4d = p4d_alloc(&init_mm, dir, address); 958c2ecf20Sopenharmony_ci if (!p4d) 968c2ecf20Sopenharmony_ci break; 978c2ecf20Sopenharmony_ci pud = pud_alloc(&init_mm, p4d, address); 988c2ecf20Sopenharmony_ci if (!pud) 998c2ecf20Sopenharmony_ci break; 1008c2ecf20Sopenharmony_ci pmd = pmd_alloc(&init_mm, pud, address); 1018c2ecf20Sopenharmony_ci if (!pmd) 1028c2ecf20Sopenharmony_ci break; 1038c2ecf20Sopenharmony_ci if (remap_area_pmd(pmd, address, end - address, 1048c2ecf20Sopenharmony_ci phys_addr + address, flags)) 1058c2ecf20Sopenharmony_ci break; 1068c2ecf20Sopenharmony_ci error = 0; 1078c2ecf20Sopenharmony_ci address = (address + PGDIR_SIZE) & PGDIR_MASK; 1088c2ecf20Sopenharmony_ci dir++; 1098c2ecf20Sopenharmony_ci } while (address && (address < end)); 1108c2ecf20Sopenharmony_ci flush_tlb_all(); 1118c2ecf20Sopenharmony_ci return error; 1128c2ecf20Sopenharmony_ci} 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci#define IS_MAPPABLE_UNCACHEABLE(addr) (addr < 0x20000000UL) 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci/* 1178c2ecf20Sopenharmony_ci * Map some physical address range into the kernel address space. 1188c2ecf20Sopenharmony_ci */ 1198c2ecf20Sopenharmony_civoid __iomem *ioremap(unsigned long phys_addr, unsigned long size) 1208c2ecf20Sopenharmony_ci{ 1218c2ecf20Sopenharmony_ci struct vm_struct *area; 1228c2ecf20Sopenharmony_ci unsigned long offset; 1238c2ecf20Sopenharmony_ci unsigned long last_addr; 1248c2ecf20Sopenharmony_ci void *addr; 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci /* Don't allow wraparound or zero size */ 1278c2ecf20Sopenharmony_ci last_addr = phys_addr + size - 1; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci if (!size || last_addr < phys_addr) 1308c2ecf20Sopenharmony_ci return NULL; 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci /* Don't allow anybody to remap normal RAM that we're using */ 1338c2ecf20Sopenharmony_ci if (phys_addr > PHYS_OFFSET && phys_addr < virt_to_phys(high_memory)) { 1348c2ecf20Sopenharmony_ci char *t_addr, *t_end; 1358c2ecf20Sopenharmony_ci struct page *page; 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci t_addr = __va(phys_addr); 1388c2ecf20Sopenharmony_ci t_end = t_addr + (size - 1); 1398c2ecf20Sopenharmony_ci for (page = virt_to_page(t_addr); 1408c2ecf20Sopenharmony_ci page <= virt_to_page(t_end); page++) 1418c2ecf20Sopenharmony_ci if (!PageReserved(page)) 1428c2ecf20Sopenharmony_ci return NULL; 1438c2ecf20Sopenharmony_ci } 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci /* 1468c2ecf20Sopenharmony_ci * Map uncached objects in the low part of address space to 1478c2ecf20Sopenharmony_ci * CONFIG_NIOS2_IO_REGION_BASE 1488c2ecf20Sopenharmony_ci */ 1498c2ecf20Sopenharmony_ci if (IS_MAPPABLE_UNCACHEABLE(phys_addr) && 1508c2ecf20Sopenharmony_ci IS_MAPPABLE_UNCACHEABLE(last_addr)) 1518c2ecf20Sopenharmony_ci return (void __iomem *)(CONFIG_NIOS2_IO_REGION_BASE + phys_addr); 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci /* Mappings have to be page-aligned */ 1548c2ecf20Sopenharmony_ci offset = phys_addr & ~PAGE_MASK; 1558c2ecf20Sopenharmony_ci phys_addr &= PAGE_MASK; 1568c2ecf20Sopenharmony_ci size = PAGE_ALIGN(last_addr + 1) - phys_addr; 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci /* Ok, go for it */ 1598c2ecf20Sopenharmony_ci area = get_vm_area(size, VM_IOREMAP); 1608c2ecf20Sopenharmony_ci if (!area) 1618c2ecf20Sopenharmony_ci return NULL; 1628c2ecf20Sopenharmony_ci addr = area->addr; 1638c2ecf20Sopenharmony_ci if (remap_area_pages((unsigned long) addr, phys_addr, size, 0)) { 1648c2ecf20Sopenharmony_ci vunmap(addr); 1658c2ecf20Sopenharmony_ci return NULL; 1668c2ecf20Sopenharmony_ci } 1678c2ecf20Sopenharmony_ci return (void __iomem *) (offset + (char *)addr); 1688c2ecf20Sopenharmony_ci} 1698c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ioremap); 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci/* 1728c2ecf20Sopenharmony_ci * iounmap unmaps nearly everything, so be careful 1738c2ecf20Sopenharmony_ci * it doesn't free currently pointer/page tables anymore but it 1748c2ecf20Sopenharmony_ci * wasn't used anyway and might be added later. 1758c2ecf20Sopenharmony_ci */ 1768c2ecf20Sopenharmony_civoid iounmap(void __iomem *addr) 1778c2ecf20Sopenharmony_ci{ 1788c2ecf20Sopenharmony_ci struct vm_struct *p; 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci if ((unsigned long) addr > CONFIG_NIOS2_IO_REGION_BASE) 1818c2ecf20Sopenharmony_ci return; 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci p = remove_vm_area((void *) (PAGE_MASK & (unsigned long __force) addr)); 1848c2ecf20Sopenharmony_ci if (!p) 1858c2ecf20Sopenharmony_ci pr_err("iounmap: bad address %p\n", addr); 1868c2ecf20Sopenharmony_ci kfree(p); 1878c2ecf20Sopenharmony_ci} 1888c2ecf20Sopenharmony_ciEXPORT_SYMBOL(iounmap); 189