11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * This file is subject to the terms and conditions of the GNU General Public 31da177e4SLinus Torvalds * License. See the file "COPYING" in the main directory of this archive 41da177e4SLinus Torvalds * for more details. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * (C) Copyright 1995 1996 Linus Torvalds 71da177e4SLinus Torvalds * (C) Copyright 2001, 2002 Ralf Baechle 81da177e4SLinus Torvalds */ 9d9ba5778SPaul Gortmaker #include <linux/export.h> 101da177e4SLinus Torvalds #include <asm/addrspace.h> 111da177e4SLinus Torvalds #include <asm/byteorder.h> 12523402faSPaul Burton #include <linux/ioport.h> 13e8edc6e0SAlexey Dobriyan #include <linux/sched.h> 145a0e3ad6STejun Heo #include <linux/slab.h> 151da177e4SLinus Torvalds #include <linux/vmalloc.h> 16589ee628SIngo Molnar #include <linux/mm_types.h> 175ce704f8SRalf Baechle #include <asm/cacheflush.h> 185ce704f8SRalf Baechle #include <asm/io.h> 195ce704f8SRalf Baechle #include <asm/tlbflush.h> 20*d257b8feSChristoph Hellwig #include <ioremap.h> 21*d257b8feSChristoph Hellwig 22*d257b8feSChristoph Hellwig #define IS_LOW512(addr) (!((phys_addr_t)(addr) & (phys_addr_t) ~0x1fffffffULL)) 23*d257b8feSChristoph Hellwig #define IS_KSEG1(addr) (((unsigned long)(addr) & ~0x1fffffffUL) == CKSEG1) 245ce704f8SRalf Baechle 255ce704f8SRalf Baechle static inline void remap_area_pte(pte_t * pte, unsigned long address, 2615d45cceSRalf Baechle phys_addr_t size, phys_addr_t phys_addr, unsigned long flags) 275ce704f8SRalf Baechle { 2815d45cceSRalf Baechle phys_addr_t end; 295ce704f8SRalf Baechle unsigned long pfn; 305ce704f8SRalf Baechle pgprot_t pgprot = __pgprot(_PAGE_GLOBAL | _PAGE_PRESENT | __READABLE 315ce704f8SRalf Baechle | __WRITEABLE | flags); 325ce704f8SRalf Baechle 335ce704f8SRalf Baechle address &= ~PMD_MASK; 345ce704f8SRalf Baechle end = address + size; 355ce704f8SRalf Baechle if (end > PMD_SIZE) 365ce704f8SRalf Baechle end = PMD_SIZE; 37b72b7092SRalf Baechle BUG_ON(address >= end); 385ce704f8SRalf Baechle pfn = phys_addr >> PAGE_SHIFT; 395ce704f8SRalf Baechle do { 405ce704f8SRalf Baechle if (!pte_none(*pte)) { 415ce704f8SRalf Baechle printk("remap_area_pte: page already exists\n"); 425ce704f8SRalf Baechle BUG(); 435ce704f8SRalf Baechle } 445ce704f8SRalf Baechle set_pte(pte, pfn_pte(pfn, pgprot)); 455ce704f8SRalf Baechle address += PAGE_SIZE; 465ce704f8SRalf Baechle pfn++; 475ce704f8SRalf Baechle pte++; 485ce704f8SRalf Baechle } while (address && (address < end)); 495ce704f8SRalf Baechle } 505ce704f8SRalf Baechle 515ce704f8SRalf Baechle static inline int remap_area_pmd(pmd_t * pmd, unsigned long address, 5215d45cceSRalf Baechle phys_addr_t size, phys_addr_t phys_addr, unsigned long flags) 535ce704f8SRalf Baechle { 5415d45cceSRalf Baechle phys_addr_t end; 555ce704f8SRalf Baechle 565ce704f8SRalf Baechle address &= ~PGDIR_MASK; 575ce704f8SRalf Baechle end = address + size; 585ce704f8SRalf Baechle if (end > PGDIR_SIZE) 595ce704f8SRalf Baechle end = PGDIR_SIZE; 605ce704f8SRalf Baechle phys_addr -= address; 61b72b7092SRalf Baechle BUG_ON(address >= end); 625ce704f8SRalf Baechle do { 635ce704f8SRalf Baechle pte_t * pte = pte_alloc_kernel(pmd, address); 645ce704f8SRalf Baechle if (!pte) 655ce704f8SRalf Baechle return -ENOMEM; 665ce704f8SRalf Baechle remap_area_pte(pte, address, end - address, address + phys_addr, flags); 675ce704f8SRalf Baechle address = (address + PMD_SIZE) & PMD_MASK; 685ce704f8SRalf Baechle pmd++; 695ce704f8SRalf Baechle } while (address && (address < end)); 705ce704f8SRalf Baechle return 0; 715ce704f8SRalf Baechle } 725ce704f8SRalf Baechle 7315d45cceSRalf Baechle static int remap_area_pages(unsigned long address, phys_addr_t phys_addr, 7415d45cceSRalf Baechle phys_addr_t size, unsigned long flags) 755ce704f8SRalf Baechle { 765ce704f8SRalf Baechle int error; 775ce704f8SRalf Baechle pgd_t * dir; 785ce704f8SRalf Baechle unsigned long end = address + size; 795ce704f8SRalf Baechle 805ce704f8SRalf Baechle phys_addr -= address; 815ce704f8SRalf Baechle dir = pgd_offset(&init_mm, address); 825ce704f8SRalf Baechle flush_cache_all(); 83b72b7092SRalf Baechle BUG_ON(address >= end); 845ce704f8SRalf Baechle do { 852bee1b58SMike Rapoport p4d_t *p4d; 865ce704f8SRalf Baechle pud_t *pud; 875ce704f8SRalf Baechle pmd_t *pmd; 885ce704f8SRalf Baechle 895ce704f8SRalf Baechle error = -ENOMEM; 902bee1b58SMike Rapoport p4d = p4d_alloc(&init_mm, dir, address); 912bee1b58SMike Rapoport if (!p4d) 922bee1b58SMike Rapoport break; 932bee1b58SMike Rapoport pud = pud_alloc(&init_mm, p4d, address); 945ce704f8SRalf Baechle if (!pud) 955ce704f8SRalf Baechle break; 965ce704f8SRalf Baechle pmd = pmd_alloc(&init_mm, pud, address); 975ce704f8SRalf Baechle if (!pmd) 985ce704f8SRalf Baechle break; 995ce704f8SRalf Baechle if (remap_area_pmd(pmd, address, end - address, 1005ce704f8SRalf Baechle phys_addr + address, flags)) 1015ce704f8SRalf Baechle break; 1025ce704f8SRalf Baechle error = 0; 1035ce704f8SRalf Baechle address = (address + PGDIR_SIZE) & PGDIR_MASK; 1045ce704f8SRalf Baechle dir++; 1055ce704f8SRalf Baechle } while (address && (address < end)); 1065ce704f8SRalf Baechle flush_tlb_all(); 1075ce704f8SRalf Baechle return error; 1085ce704f8SRalf Baechle } 1091da177e4SLinus Torvalds 110523402faSPaul Burton static int __ioremap_check_ram(unsigned long start_pfn, unsigned long nr_pages, 111523402faSPaul Burton void *arg) 112523402faSPaul Burton { 113523402faSPaul Burton unsigned long i; 114523402faSPaul Burton 115523402faSPaul Burton for (i = 0; i < nr_pages; i++) { 116523402faSPaul Burton if (pfn_valid(start_pfn + i) && 117523402faSPaul Burton !PageReserved(pfn_to_page(start_pfn + i))) 118523402faSPaul Burton return 1; 119523402faSPaul Burton } 120523402faSPaul Burton 121523402faSPaul Burton return 0; 122523402faSPaul Burton } 123523402faSPaul Burton 1241da177e4SLinus Torvalds /* 125*d257b8feSChristoph Hellwig * ioremap_prot - map bus memory into CPU space 126*d257b8feSChristoph Hellwig * @phys_addr: bus address of the memory 127*d257b8feSChristoph Hellwig * @size: size of the resource to map 1281da177e4SLinus Torvalds * 129*d257b8feSChristoph Hellwig * ioremap_prot gives the caller control over cache coherency attributes (CCA) 1301da177e4SLinus Torvalds */ 131*d257b8feSChristoph Hellwig void __iomem *ioremap_prot(phys_addr_t phys_addr, unsigned long size, 132*d257b8feSChristoph Hellwig unsigned long prot_val) 1331da177e4SLinus Torvalds { 134*d257b8feSChristoph Hellwig unsigned long flags = prot_val & _CACHE_MASK; 135523402faSPaul Burton unsigned long offset, pfn, last_pfn; 1361da177e4SLinus Torvalds struct vm_struct *area; 13715d45cceSRalf Baechle phys_addr_t last_addr; 1381da177e4SLinus Torvalds void *addr; 139*d257b8feSChristoph Hellwig void __iomem *cpu_addr; 140*d257b8feSChristoph Hellwig 141*d257b8feSChristoph Hellwig cpu_addr = plat_ioremap(phys_addr, size, flags); 142*d257b8feSChristoph Hellwig if (cpu_addr) 143*d257b8feSChristoph Hellwig return cpu_addr; 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds phys_addr = fixup_bigphys_addr(phys_addr, size); 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds /* Don't allow wraparound or zero size */ 1481da177e4SLinus Torvalds last_addr = phys_addr + size - 1; 1491da177e4SLinus Torvalds if (!size || last_addr < phys_addr) 1501da177e4SLinus Torvalds return NULL; 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds /* 1531da177e4SLinus Torvalds * Map uncached objects in the low 512mb of address space using KSEG1, 1541da177e4SLinus Torvalds * otherwise map using page tables. 1551da177e4SLinus Torvalds */ 1561da177e4SLinus Torvalds if (IS_LOW512(phys_addr) && IS_LOW512(last_addr) && 1571da177e4SLinus Torvalds flags == _CACHE_UNCACHED) 158c3455b0eSMaciej W. Rozycki return (void __iomem *) CKSEG1ADDR(phys_addr); 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds /* 161523402faSPaul Burton * Don't allow anybody to remap RAM that may be allocated by the page 162523402faSPaul Burton * allocator, since that could lead to races & data clobbering. 1631da177e4SLinus Torvalds */ 164523402faSPaul Burton pfn = PFN_DOWN(phys_addr); 165523402faSPaul Burton last_pfn = PFN_DOWN(last_addr); 166523402faSPaul Burton if (walk_system_ram_range(pfn, last_pfn - pfn + 1, NULL, 167523402faSPaul Burton __ioremap_check_ram) == 1) { 168523402faSPaul Burton WARN_ONCE(1, "ioremap on RAM at %pa - %pa\n", 169523402faSPaul Burton &phys_addr, &last_addr); 1701da177e4SLinus Torvalds return NULL; 1711da177e4SLinus Torvalds } 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds /* 1741da177e4SLinus Torvalds * Mappings have to be page-aligned 1751da177e4SLinus Torvalds */ 1761da177e4SLinus Torvalds offset = phys_addr & ~PAGE_MASK; 1771da177e4SLinus Torvalds phys_addr &= PAGE_MASK; 1781da177e4SLinus Torvalds size = PAGE_ALIGN(last_addr + 1) - phys_addr; 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds /* 1811da177e4SLinus Torvalds * Ok, go for it.. 1821da177e4SLinus Torvalds */ 1831da177e4SLinus Torvalds area = get_vm_area(size, VM_IOREMAP); 1841da177e4SLinus Torvalds if (!area) 1851da177e4SLinus Torvalds return NULL; 1861da177e4SLinus Torvalds addr = area->addr; 1875ce704f8SRalf Baechle if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) { 1881da177e4SLinus Torvalds vunmap(addr); 1891da177e4SLinus Torvalds return NULL; 1901da177e4SLinus Torvalds } 1911da177e4SLinus Torvalds 192c3455b0eSMaciej W. Rozycki return (void __iomem *) (offset + (char *)addr); 1931da177e4SLinus Torvalds } 194*d257b8feSChristoph Hellwig EXPORT_SYMBOL(ioremap_prot); 1951da177e4SLinus Torvalds 196*d257b8feSChristoph Hellwig void iounmap(const volatile void __iomem *addr) 1971da177e4SLinus Torvalds { 1981da177e4SLinus Torvalds struct vm_struct *p; 1991da177e4SLinus Torvalds 200*d257b8feSChristoph Hellwig if (plat_iounmap(addr) || IS_KSEG1(addr)) 2011da177e4SLinus Torvalds return; 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds p = remove_vm_area((void *) (PAGE_MASK & (unsigned long __force) addr)); 204c6e8b587SRalf Baechle if (!p) 2051da177e4SLinus Torvalds printk(KERN_ERR "iounmap: bad address %p\n", addr); 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds kfree(p); 2081da177e4SLinus Torvalds } 209*d257b8feSChristoph Hellwig EXPORT_SYMBOL(iounmap); 210