Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is subject to the terms and conditions of the GNU General Public |
| 3 | * License. See the file "COPYING" in the main directory of this archive |
| 4 | * for more details. |
| 5 | * |
| 6 | * (C) Copyright 1995 1996 Linus Torvalds |
| 7 | * (C) Copyright 2001, 2002 Ralf Baechle |
| 8 | */ |
Haavard Skinnemoen | 8e08792 | 2006-12-08 02:38:04 -0800 | [diff] [blame] | 9 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/module.h> |
| 11 | #include <asm/addrspace.h> |
| 12 | #include <asm/byteorder.h> |
| 13 | |
| 14 | #include <linux/vmalloc.h> |
Haavard Skinnemoen | 8e08792 | 2006-12-08 02:38:04 -0800 | [diff] [blame] | 15 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | * Generic mapping function (not visible outside): |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * Remap an arbitrary physical address space into the kernel virtual |
| 23 | * address space. Needed when the kernel wants to access high addresses |
| 24 | * directly. |
| 25 | * |
| 26 | * NOTE! We need to allow non-page-aligned mappings too: we will obviously |
| 27 | * have to convert them into an offset in a page-aligned mapping, but the |
| 28 | * caller shouldn't need to know that small detail. |
| 29 | */ |
| 30 | |
| 31 | #define IS_LOW512(addr) (!((phys_t)(addr) & (phys_t) ~0x1fffffffULL)) |
| 32 | |
Maciej W. Rozycki | c3455b0 | 2005-06-30 10:48:40 +0000 | [diff] [blame] | 33 | void __iomem * __ioremap(phys_t phys_addr, phys_t size, unsigned long flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | { |
| 35 | struct vm_struct * area; |
| 36 | unsigned long offset; |
| 37 | phys_t last_addr; |
| 38 | void * addr; |
Haavard Skinnemoen | 8e08792 | 2006-12-08 02:38:04 -0800 | [diff] [blame] | 39 | pgprot_t pgprot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | phys_addr = fixup_bigphys_addr(phys_addr, size); |
| 42 | |
| 43 | /* Don't allow wraparound or zero size */ |
| 44 | last_addr = phys_addr + size - 1; |
| 45 | if (!size || last_addr < phys_addr) |
| 46 | return NULL; |
| 47 | |
| 48 | /* |
| 49 | * Map uncached objects in the low 512mb of address space using KSEG1, |
| 50 | * otherwise map using page tables. |
| 51 | */ |
| 52 | if (IS_LOW512(phys_addr) && IS_LOW512(last_addr) && |
| 53 | flags == _CACHE_UNCACHED) |
Maciej W. Rozycki | c3455b0 | 2005-06-30 10:48:40 +0000 | [diff] [blame] | 54 | return (void __iomem *) CKSEG1ADDR(phys_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
| 56 | /* |
| 57 | * Don't allow anybody to remap normal RAM that we're using.. |
| 58 | */ |
| 59 | if (phys_addr < virt_to_phys(high_memory)) { |
| 60 | char *t_addr, *t_end; |
| 61 | struct page *page; |
| 62 | |
| 63 | t_addr = __va(phys_addr); |
| 64 | t_end = t_addr + (size - 1); |
| 65 | |
| 66 | for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++) |
| 67 | if(!PageReserved(page)) |
| 68 | return NULL; |
| 69 | } |
| 70 | |
Haavard Skinnemoen | 8e08792 | 2006-12-08 02:38:04 -0800 | [diff] [blame] | 71 | pgprot = __pgprot(_PAGE_GLOBAL | _PAGE_PRESENT | __READABLE |
| 72 | | __WRITEABLE | flags); |
| 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | /* |
| 75 | * Mappings have to be page-aligned |
| 76 | */ |
| 77 | offset = phys_addr & ~PAGE_MASK; |
| 78 | phys_addr &= PAGE_MASK; |
| 79 | size = PAGE_ALIGN(last_addr + 1) - phys_addr; |
| 80 | |
| 81 | /* |
| 82 | * Ok, go for it.. |
| 83 | */ |
| 84 | area = get_vm_area(size, VM_IOREMAP); |
| 85 | if (!area) |
| 86 | return NULL; |
| 87 | addr = area->addr; |
Haavard Skinnemoen | 8e08792 | 2006-12-08 02:38:04 -0800 | [diff] [blame] | 88 | if (ioremap_page_range((unsigned long)addr, (unsigned long)addr + size, |
| 89 | phys_addr, pgprot)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | vunmap(addr); |
| 91 | return NULL; |
| 92 | } |
| 93 | |
Maciej W. Rozycki | c3455b0 | 2005-06-30 10:48:40 +0000 | [diff] [blame] | 94 | return (void __iomem *) (offset + (char *)addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Ralf Baechle | c6e8b58 | 2005-02-10 12:19:59 +0000 | [diff] [blame] | 97 | #define IS_KSEG1(addr) (((unsigned long)(addr) & ~0x1fffffffUL) == CKSEG1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Ralf Baechle | d89e36d | 2006-10-19 14:21:47 +0100 | [diff] [blame] | 99 | void __iounmap(const volatile void __iomem *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
| 101 | struct vm_struct *p; |
| 102 | |
| 103 | if (IS_KSEG1(addr)) |
| 104 | return; |
| 105 | |
| 106 | p = remove_vm_area((void *) (PAGE_MASK & (unsigned long __force) addr)); |
Ralf Baechle | c6e8b58 | 2005-02-10 12:19:59 +0000 | [diff] [blame] | 107 | if (!p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | printk(KERN_ERR "iounmap: bad address %p\n", addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | kfree(p); |
| 111 | } |
| 112 | |
| 113 | EXPORT_SYMBOL(__ioremap); |
| 114 | EXPORT_SYMBOL(__iounmap); |