Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * arch/parisc/mm/ioremap.c |
| 3 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * (C) Copyright 1995 1996 Linus Torvalds |
| 5 | * (C) Copyright 2001 Helge Deller <deller@gmx.de> |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 6 | * (C) Copyright 2005 Kyle McMartin <kyle@parisc-linux.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/vmalloc.h> |
| 10 | #include <linux/errno.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <asm/io.h> |
| 13 | #include <asm/pgalloc.h> |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 14 | #include <asm/tlbflush.h> |
| 15 | #include <asm/cacheflush.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 17 | static inline void |
| 18 | remap_area_pte(pte_t *pte, unsigned long address, unsigned long size, |
| 19 | unsigned long phys_addr, unsigned long flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | { |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 21 | unsigned long end, pfn; |
| 22 | pgprot_t pgprot = __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY | |
| 23 | _PAGE_ACCESSED | flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | address &= ~PMD_MASK; |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | end = address + size; |
| 28 | if (end > PMD_SIZE) |
| 29 | end = PMD_SIZE; |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 30 | |
| 31 | BUG_ON(address >= end); |
| 32 | |
| 33 | pfn = phys_addr >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | do { |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 35 | BUG_ON(!pte_none(*pte)); |
| 36 | |
| 37 | set_pte(pte, pfn_pte(pfn, pgprot)); |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | address += PAGE_SIZE; |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 40 | pfn++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | pte++; |
| 42 | } while (address && (address < end)); |
| 43 | } |
| 44 | |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 45 | static inline int |
| 46 | remap_area_pmd(pmd_t *pmd, unsigned long address, unsigned long size, |
| 47 | unsigned long phys_addr, unsigned long flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | { |
| 49 | unsigned long end; |
| 50 | |
| 51 | address &= ~PGDIR_MASK; |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | end = address + size; |
| 54 | if (end > PGDIR_SIZE) |
| 55 | end = PGDIR_SIZE; |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 56 | |
| 57 | BUG_ON(address >= end); |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | phys_addr -= address; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | do { |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 61 | pte_t *pte = pte_alloc_kernel(pmd, address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | if (!pte) |
| 63 | return -ENOMEM; |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 64 | |
| 65 | remap_area_pte(pte, address, end - address, |
| 66 | address + phys_addr, flags); |
| 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | address = (address + PMD_SIZE) & PMD_MASK; |
| 69 | pmd++; |
| 70 | } while (address && (address < end)); |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | return 0; |
| 73 | } |
| 74 | |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 75 | static int |
| 76 | remap_area_pages(unsigned long address, unsigned long phys_addr, |
| 77 | unsigned long size, unsigned long flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | { |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 79 | pgd_t *dir; |
| 80 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | unsigned long end = address + size; |
| 82 | |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 83 | BUG_ON(address >= end); |
| 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | phys_addr -= address; |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 86 | dir = pgd_offset_k(address); |
| 87 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | flush_cache_all(); |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 89 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | do { |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 91 | pud_t *pud; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | pmd_t *pmd; |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | error = -ENOMEM; |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 95 | pud = pud_alloc(&init_mm, dir, address); |
| 96 | if (!pud) |
| 97 | break; |
| 98 | |
| 99 | pmd = pmd_alloc(&init_mm, pud, address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | if (!pmd) |
| 101 | break; |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | if (remap_area_pmd(pmd, address, end - address, |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 104 | phys_addr + address, flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | break; |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | error = 0; |
| 108 | address = (address + PGDIR_SIZE) & PGDIR_MASK; |
| 109 | dir++; |
| 110 | } while (address && (address < end)); |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | flush_tlb_all(); |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | return error; |
| 115 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
| 117 | #ifdef CONFIG_DEBUG_IOREMAP |
| 118 | static unsigned long last = 0; |
| 119 | |
| 120 | void gsc_bad_addr(unsigned long addr) |
| 121 | { |
| 122 | if (time_after(jiffies, last + HZ*10)) { |
| 123 | printk("gsc_foo() called with bad address 0x%lx\n", addr); |
| 124 | dump_stack(); |
| 125 | last = jiffies; |
| 126 | } |
| 127 | } |
| 128 | EXPORT_SYMBOL(gsc_bad_addr); |
| 129 | |
| 130 | void __raw_bad_addr(const volatile void __iomem *addr) |
| 131 | { |
| 132 | if (time_after(jiffies, last + HZ*10)) { |
| 133 | printk("__raw_foo() called with bad address 0x%p\n", addr); |
| 134 | dump_stack(); |
| 135 | last = jiffies; |
| 136 | } |
| 137 | } |
| 138 | EXPORT_SYMBOL(__raw_bad_addr); |
| 139 | #endif |
| 140 | |
| 141 | /* |
| 142 | * Generic mapping function (not visible outside): |
| 143 | */ |
| 144 | |
| 145 | /* |
| 146 | * Remap an arbitrary physical address space into the kernel virtual |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 147 | * address space. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | * |
| 149 | * NOTE! We need to allow non-page-aligned mappings too: we will obviously |
| 150 | * have to convert them into an offset in a page-aligned mapping, but the |
| 151 | * caller shouldn't need to know that small detail. |
| 152 | */ |
| 153 | void __iomem * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags) |
| 154 | { |
Helge Deller | cb4ab59 | 2006-03-23 00:40:10 -0700 | [diff] [blame^] | 155 | void *addr; |
| 156 | struct vm_struct *area; |
| 157 | unsigned long offset, last_addr; |
| 158 | |
Helge Deller | 29ef8295 | 2006-03-23 00:32:46 -0700 | [diff] [blame] | 159 | #ifdef CONFIG_EISA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | unsigned long end = phys_addr + size - 1; |
| 161 | /* Support EISA addresses */ |
| 162 | if ((phys_addr >= 0x00080000 && end < 0x000fffff) |
| 163 | || (phys_addr >= 0x00500000 && end < 0x03bfffff)) { |
| 164 | phys_addr |= 0xfc000000; |
Helge Deller | cb4ab59 | 2006-03-23 00:40:10 -0700 | [diff] [blame^] | 165 | #warning "FIXME: EISA regions do not work yet..." |
| 166 | return NULL; /* XXX */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | #endif |
| 169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | /* Don't allow wraparound or zero size */ |
| 171 | last_addr = phys_addr + size - 1; |
| 172 | if (!size || last_addr < phys_addr) |
| 173 | return NULL; |
| 174 | |
| 175 | /* |
| 176 | * Don't allow anybody to remap normal RAM that we're using.. |
| 177 | */ |
| 178 | if (phys_addr < virt_to_phys(high_memory)) { |
| 179 | char *t_addr, *t_end; |
| 180 | struct page *page; |
| 181 | |
| 182 | t_addr = __va(phys_addr); |
| 183 | t_end = t_addr + (size - 1); |
| 184 | |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 185 | for (page = virt_to_page(t_addr); |
| 186 | page <= virt_to_page(t_end); page++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | if(!PageReserved(page)) |
| 188 | return NULL; |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 189 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /* |
| 193 | * Mappings have to be page-aligned |
| 194 | */ |
| 195 | offset = phys_addr & ~PAGE_MASK; |
| 196 | phys_addr &= PAGE_MASK; |
| 197 | size = PAGE_ALIGN(last_addr) - phys_addr; |
| 198 | |
| 199 | /* |
| 200 | * Ok, go for it.. |
| 201 | */ |
| 202 | area = get_vm_area(size, VM_IOREMAP); |
| 203 | if (!area) |
| 204 | return NULL; |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | addr = area->addr; |
| 207 | if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) { |
| 208 | vfree(addr); |
| 209 | return NULL; |
| 210 | } |
Kyle McMartin | e0565a1 | 2006-01-10 20:47:52 -0500 | [diff] [blame] | 211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | return (void __iomem *) (offset + (char *)addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | void iounmap(void __iomem *addr) |
| 216 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | if (addr > high_memory) |
| 218 | return vfree((void *) (PAGE_MASK & (unsigned long __force) addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | } |