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> 12*523402faSPaul 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> 205ce704f8SRalf Baechle 215ce704f8SRalf Baechle static inline void remap_area_pte(pte_t * pte, unsigned long address, 2215d45cceSRalf Baechle phys_addr_t size, phys_addr_t phys_addr, unsigned long flags) 235ce704f8SRalf Baechle { 2415d45cceSRalf Baechle phys_addr_t end; 255ce704f8SRalf Baechle unsigned long pfn; 265ce704f8SRalf Baechle pgprot_t pgprot = __pgprot(_PAGE_GLOBAL | _PAGE_PRESENT | __READABLE 275ce704f8SRalf Baechle | __WRITEABLE | flags); 285ce704f8SRalf Baechle 295ce704f8SRalf Baechle address &= ~PMD_MASK; 305ce704f8SRalf Baechle end = address + size; 315ce704f8SRalf Baechle if (end > PMD_SIZE) 325ce704f8SRalf Baechle end = PMD_SIZE; 33b72b7092SRalf Baechle BUG_ON(address >= end); 345ce704f8SRalf Baechle pfn = phys_addr >> PAGE_SHIFT; 355ce704f8SRalf Baechle do { 365ce704f8SRalf Baechle if (!pte_none(*pte)) { 375ce704f8SRalf Baechle printk("remap_area_pte: page already exists\n"); 385ce704f8SRalf Baechle BUG(); 395ce704f8SRalf Baechle } 405ce704f8SRalf Baechle set_pte(pte, pfn_pte(pfn, pgprot)); 415ce704f8SRalf Baechle address += PAGE_SIZE; 425ce704f8SRalf Baechle pfn++; 435ce704f8SRalf Baechle pte++; 445ce704f8SRalf Baechle } while (address && (address < end)); 455ce704f8SRalf Baechle } 465ce704f8SRalf Baechle 475ce704f8SRalf Baechle static inline int remap_area_pmd(pmd_t * pmd, unsigned long address, 4815d45cceSRalf Baechle phys_addr_t size, phys_addr_t phys_addr, unsigned long flags) 495ce704f8SRalf Baechle { 5015d45cceSRalf Baechle phys_addr_t end; 515ce704f8SRalf Baechle 525ce704f8SRalf Baechle address &= ~PGDIR_MASK; 535ce704f8SRalf Baechle end = address + size; 545ce704f8SRalf Baechle if (end > PGDIR_SIZE) 555ce704f8SRalf Baechle end = PGDIR_SIZE; 565ce704f8SRalf Baechle phys_addr -= address; 57b72b7092SRalf Baechle BUG_ON(address >= end); 585ce704f8SRalf Baechle do { 595ce704f8SRalf Baechle pte_t * pte = pte_alloc_kernel(pmd, address); 605ce704f8SRalf Baechle if (!pte) 615ce704f8SRalf Baechle return -ENOMEM; 625ce704f8SRalf Baechle remap_area_pte(pte, address, end - address, address + phys_addr, flags); 635ce704f8SRalf Baechle address = (address + PMD_SIZE) & PMD_MASK; 645ce704f8SRalf Baechle pmd++; 655ce704f8SRalf Baechle } while (address && (address < end)); 665ce704f8SRalf Baechle return 0; 675ce704f8SRalf Baechle } 685ce704f8SRalf Baechle 6915d45cceSRalf Baechle static int remap_area_pages(unsigned long address, phys_addr_t phys_addr, 7015d45cceSRalf Baechle phys_addr_t size, unsigned long flags) 715ce704f8SRalf Baechle { 725ce704f8SRalf Baechle int error; 735ce704f8SRalf Baechle pgd_t * dir; 745ce704f8SRalf Baechle unsigned long end = address + size; 755ce704f8SRalf Baechle 765ce704f8SRalf Baechle phys_addr -= address; 775ce704f8SRalf Baechle dir = pgd_offset(&init_mm, address); 785ce704f8SRalf Baechle flush_cache_all(); 79b72b7092SRalf Baechle BUG_ON(address >= end); 805ce704f8SRalf Baechle do { 815ce704f8SRalf Baechle pud_t *pud; 825ce704f8SRalf Baechle pmd_t *pmd; 835ce704f8SRalf Baechle 845ce704f8SRalf Baechle error = -ENOMEM; 855ce704f8SRalf Baechle pud = pud_alloc(&init_mm, dir, address); 865ce704f8SRalf Baechle if (!pud) 875ce704f8SRalf Baechle break; 885ce704f8SRalf Baechle pmd = pmd_alloc(&init_mm, pud, address); 895ce704f8SRalf Baechle if (!pmd) 905ce704f8SRalf Baechle break; 915ce704f8SRalf Baechle if (remap_area_pmd(pmd, address, end - address, 925ce704f8SRalf Baechle phys_addr + address, flags)) 935ce704f8SRalf Baechle break; 945ce704f8SRalf Baechle error = 0; 955ce704f8SRalf Baechle address = (address + PGDIR_SIZE) & PGDIR_MASK; 965ce704f8SRalf Baechle dir++; 975ce704f8SRalf Baechle } while (address && (address < end)); 985ce704f8SRalf Baechle flush_tlb_all(); 995ce704f8SRalf Baechle return error; 1005ce704f8SRalf Baechle } 1011da177e4SLinus Torvalds 102*523402faSPaul Burton static int __ioremap_check_ram(unsigned long start_pfn, unsigned long nr_pages, 103*523402faSPaul Burton void *arg) 104*523402faSPaul Burton { 105*523402faSPaul Burton unsigned long i; 106*523402faSPaul Burton 107*523402faSPaul Burton for (i = 0; i < nr_pages; i++) { 108*523402faSPaul Burton if (pfn_valid(start_pfn + i) && 109*523402faSPaul Burton !PageReserved(pfn_to_page(start_pfn + i))) 110*523402faSPaul Burton return 1; 111*523402faSPaul Burton } 112*523402faSPaul Burton 113*523402faSPaul Burton return 0; 114*523402faSPaul Burton } 115*523402faSPaul Burton 1161da177e4SLinus Torvalds /* 1171da177e4SLinus Torvalds * Generic mapping function (not visible outside): 1181da177e4SLinus Torvalds */ 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds /* 1211da177e4SLinus Torvalds * Remap an arbitrary physical address space into the kernel virtual 1221da177e4SLinus Torvalds * address space. Needed when the kernel wants to access high addresses 1231da177e4SLinus Torvalds * directly. 1241da177e4SLinus Torvalds * 1251da177e4SLinus Torvalds * NOTE! We need to allow non-page-aligned mappings too: we will obviously 1261da177e4SLinus Torvalds * have to convert them into an offset in a page-aligned mapping, but the 1271da177e4SLinus Torvalds * caller shouldn't need to know that small detail. 1281da177e4SLinus Torvalds */ 1291da177e4SLinus Torvalds 13015d45cceSRalf Baechle #define IS_LOW512(addr) (!((phys_addr_t)(addr) & (phys_addr_t) ~0x1fffffffULL)) 1311da177e4SLinus Torvalds 13215d45cceSRalf Baechle void __iomem * __ioremap(phys_addr_t phys_addr, phys_addr_t size, unsigned long flags) 1331da177e4SLinus Torvalds { 134*523402faSPaul Burton unsigned long offset, pfn, last_pfn; 1351da177e4SLinus Torvalds struct vm_struct * area; 13615d45cceSRalf Baechle phys_addr_t last_addr; 1371da177e4SLinus Torvalds void * addr; 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds phys_addr = fixup_bigphys_addr(phys_addr, size); 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds /* Don't allow wraparound or zero size */ 1421da177e4SLinus Torvalds last_addr = phys_addr + size - 1; 1431da177e4SLinus Torvalds if (!size || last_addr < phys_addr) 1441da177e4SLinus Torvalds return NULL; 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds /* 1471da177e4SLinus Torvalds * Map uncached objects in the low 512mb of address space using KSEG1, 1481da177e4SLinus Torvalds * otherwise map using page tables. 1491da177e4SLinus Torvalds */ 1501da177e4SLinus Torvalds if (IS_LOW512(phys_addr) && IS_LOW512(last_addr) && 1511da177e4SLinus Torvalds flags == _CACHE_UNCACHED) 152c3455b0eSMaciej W. Rozycki return (void __iomem *) CKSEG1ADDR(phys_addr); 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds /* 155*523402faSPaul Burton * Don't allow anybody to remap RAM that may be allocated by the page 156*523402faSPaul Burton * allocator, since that could lead to races & data clobbering. 1571da177e4SLinus Torvalds */ 158*523402faSPaul Burton pfn = PFN_DOWN(phys_addr); 159*523402faSPaul Burton last_pfn = PFN_DOWN(last_addr); 160*523402faSPaul Burton if (walk_system_ram_range(pfn, last_pfn - pfn + 1, NULL, 161*523402faSPaul Burton __ioremap_check_ram) == 1) { 162*523402faSPaul Burton WARN_ONCE(1, "ioremap on RAM at %pa - %pa\n", 163*523402faSPaul Burton &phys_addr, &last_addr); 1641da177e4SLinus Torvalds return NULL; 1651da177e4SLinus Torvalds } 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds /* 1681da177e4SLinus Torvalds * Mappings have to be page-aligned 1691da177e4SLinus Torvalds */ 1701da177e4SLinus Torvalds offset = phys_addr & ~PAGE_MASK; 1711da177e4SLinus Torvalds phys_addr &= PAGE_MASK; 1721da177e4SLinus Torvalds size = PAGE_ALIGN(last_addr + 1) - phys_addr; 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds /* 1751da177e4SLinus Torvalds * Ok, go for it.. 1761da177e4SLinus Torvalds */ 1771da177e4SLinus Torvalds area = get_vm_area(size, VM_IOREMAP); 1781da177e4SLinus Torvalds if (!area) 1791da177e4SLinus Torvalds return NULL; 1801da177e4SLinus Torvalds addr = area->addr; 1815ce704f8SRalf Baechle if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) { 1821da177e4SLinus Torvalds vunmap(addr); 1831da177e4SLinus Torvalds return NULL; 1841da177e4SLinus Torvalds } 1851da177e4SLinus Torvalds 186c3455b0eSMaciej W. Rozycki return (void __iomem *) (offset + (char *)addr); 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds 189c6e8b587SRalf Baechle #define IS_KSEG1(addr) (((unsigned long)(addr) & ~0x1fffffffUL) == CKSEG1) 1901da177e4SLinus Torvalds 191d89e36d8SRalf Baechle void __iounmap(const volatile void __iomem *addr) 1921da177e4SLinus Torvalds { 1931da177e4SLinus Torvalds struct vm_struct *p; 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds if (IS_KSEG1(addr)) 1961da177e4SLinus Torvalds return; 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds p = remove_vm_area((void *) (PAGE_MASK & (unsigned long __force) addr)); 199c6e8b587SRalf Baechle if (!p) 2001da177e4SLinus Torvalds printk(KERN_ERR "iounmap: bad address %p\n", addr); 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds kfree(p); 2031da177e4SLinus Torvalds } 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds EXPORT_SYMBOL(__ioremap); 2061da177e4SLinus Torvalds EXPORT_SYMBOL(__iounmap); 207