Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * arch/sh/mm/ioremap.c |
| 3 | * |
| 4 | * Re-map IO memory to kernel address space so that we can access it. |
| 5 | * This is needed for high PCI addresses that aren't mapped in the |
| 6 | * 640k-1MB IO memory area on PC's |
| 7 | * |
| 8 | * (C) Copyright 1995 1996 Linus Torvalds |
Paul Mundt | b66c1a3 | 2006-01-16 22:14:15 -0800 | [diff] [blame] | 9 | * (C) Copyright 2005, 2006 Paul Mundt |
| 10 | * |
| 11 | * This file is subject to the terms and conditions of the GNU General |
| 12 | * Public License. See the file "COPYING" in the main directory of this |
| 13 | * archive for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/vmalloc.h> |
Paul Mundt | b66c1a3 | 2006-01-16 22:14:15 -0800 | [diff] [blame] | 16 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/mm.h> |
Paul Mundt | a3e61d5 | 2006-09-27 16:45:22 +0900 | [diff] [blame^] | 18 | #include <linux/pci.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/io.h> |
| 20 | #include <asm/page.h> |
| 21 | #include <asm/pgalloc.h> |
Paul Mundt | b66c1a3 | 2006-01-16 22:14:15 -0800 | [diff] [blame] | 22 | #include <asm/addrspace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <asm/cacheflush.h> |
| 24 | #include <asm/tlbflush.h> |
| 25 | |
| 26 | static inline void remap_area_pte(pte_t * pte, unsigned long address, |
| 27 | unsigned long size, unsigned long phys_addr, unsigned long flags) |
| 28 | { |
| 29 | unsigned long end; |
| 30 | unsigned long pfn; |
| 31 | pgprot_t pgprot = __pgprot(_PAGE_PRESENT | _PAGE_RW | |
| 32 | _PAGE_DIRTY | _PAGE_ACCESSED | |
| 33 | _PAGE_HW_SHARED | _PAGE_FLAGS_HARD | flags); |
| 34 | |
| 35 | address &= ~PMD_MASK; |
| 36 | end = address + size; |
| 37 | if (end > PMD_SIZE) |
| 38 | end = PMD_SIZE; |
| 39 | if (address >= end) |
| 40 | BUG(); |
| 41 | pfn = phys_addr >> PAGE_SHIFT; |
| 42 | do { |
| 43 | if (!pte_none(*pte)) { |
| 44 | printk("remap_area_pte: page already exists\n"); |
| 45 | BUG(); |
| 46 | } |
| 47 | set_pte(pte, pfn_pte(pfn, pgprot)); |
| 48 | address += PAGE_SIZE; |
| 49 | pfn++; |
| 50 | pte++; |
| 51 | } while (address && (address < end)); |
| 52 | } |
| 53 | |
| 54 | static inline int remap_area_pmd(pmd_t * pmd, unsigned long address, |
| 55 | unsigned long size, unsigned long phys_addr, unsigned long flags) |
| 56 | { |
| 57 | unsigned long end; |
| 58 | |
| 59 | address &= ~PGDIR_MASK; |
| 60 | end = address + size; |
| 61 | if (end > PGDIR_SIZE) |
| 62 | end = PGDIR_SIZE; |
| 63 | phys_addr -= address; |
| 64 | if (address >= end) |
| 65 | BUG(); |
| 66 | do { |
Hugh Dickins | 872fec1 | 2005-10-29 18:16:21 -0700 | [diff] [blame] | 67 | pte_t * pte = pte_alloc_kernel(pmd, address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | if (!pte) |
| 69 | return -ENOMEM; |
| 70 | remap_area_pte(pte, address, end - address, address + phys_addr, flags); |
| 71 | address = (address + PMD_SIZE) & PMD_MASK; |
| 72 | pmd++; |
| 73 | } while (address && (address < end)); |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | int remap_area_pages(unsigned long address, unsigned long phys_addr, |
| 78 | unsigned long size, unsigned long flags) |
| 79 | { |
| 80 | int error; |
| 81 | pgd_t * dir; |
| 82 | unsigned long end = address + size; |
| 83 | |
| 84 | phys_addr -= address; |
| 85 | dir = pgd_offset_k(address); |
| 86 | flush_cache_all(); |
| 87 | if (address >= end) |
| 88 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | do { |
Paul Mundt | b66c1a3 | 2006-01-16 22:14:15 -0800 | [diff] [blame] | 90 | pud_t *pud; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | pmd_t *pmd; |
Paul Mundt | b66c1a3 | 2006-01-16 22:14:15 -0800 | [diff] [blame] | 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | error = -ENOMEM; |
Paul Mundt | b66c1a3 | 2006-01-16 22:14:15 -0800 | [diff] [blame] | 94 | |
| 95 | pud = pud_alloc(&init_mm, dir, address); |
| 96 | if (!pud) |
| 97 | break; |
| 98 | pmd = pmd_alloc(&init_mm, pud, address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | if (!pmd) |
| 100 | break; |
| 101 | if (remap_area_pmd(pmd, address, end - address, |
| 102 | phys_addr + address, flags)) |
| 103 | break; |
| 104 | error = 0; |
| 105 | address = (address + PGDIR_SIZE) & PGDIR_MASK; |
| 106 | dir++; |
| 107 | } while (address && (address < end)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | flush_tlb_all(); |
| 109 | return error; |
| 110 | } |
| 111 | |
| 112 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | * Remap an arbitrary physical address space into the kernel virtual |
| 114 | * address space. Needed when the kernel wants to access high addresses |
| 115 | * directly. |
| 116 | * |
| 117 | * NOTE! We need to allow non-page-aligned mappings too: we will obviously |
| 118 | * have to convert them into an offset in a page-aligned mapping, but the |
| 119 | * caller shouldn't need to know that small detail. |
| 120 | */ |
Paul Mundt | b66c1a3 | 2006-01-16 22:14:15 -0800 | [diff] [blame] | 121 | void __iomem *__ioremap(unsigned long phys_addr, unsigned long size, |
| 122 | unsigned long flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | struct vm_struct * area; |
Paul Mundt | b66c1a3 | 2006-01-16 22:14:15 -0800 | [diff] [blame] | 125 | unsigned long offset, last_addr, addr, orig_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | /* Don't allow wraparound or zero size */ |
| 128 | last_addr = phys_addr + size - 1; |
| 129 | if (!size || last_addr < phys_addr) |
| 130 | return NULL; |
| 131 | |
| 132 | /* |
| 133 | * Don't remap the low PCI/ISA area, it's always mapped.. |
| 134 | */ |
| 135 | if (phys_addr >= 0xA0000 && last_addr < 0x100000) |
Paul Mundt | b66c1a3 | 2006-01-16 22:14:15 -0800 | [diff] [blame] | 136 | return (void __iomem *)phys_to_virt(phys_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
| 138 | /* |
Paul Mundt | a3e61d5 | 2006-09-27 16:45:22 +0900 | [diff] [blame^] | 139 | * If we're on an SH7751 or SH7780 PCI controller, PCI memory is |
| 140 | * mapped at the end of the address space (typically 0xfd000000) |
| 141 | * in a non-translatable area, so mapping through page tables for |
| 142 | * this area is not only pointless, but also fundamentally |
| 143 | * broken. Just return the physical address instead. |
| 144 | * |
| 145 | * For boards that map a small PCI memory aperture somewhere in |
| 146 | * P1/P2 space, ioremap() will already do the right thing, |
| 147 | * and we'll never get this far. |
| 148 | */ |
| 149 | if (is_pci_memaddr(phys_addr) && is_pci_memaddr(last_addr)) |
| 150 | return (void __iomem *)phys_addr; |
| 151 | |
| 152 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | * Don't allow anybody to remap normal RAM that we're using.. |
| 154 | */ |
| 155 | if (phys_addr < virt_to_phys(high_memory)) |
| 156 | return NULL; |
| 157 | |
| 158 | /* |
| 159 | * Mappings have to be page-aligned |
| 160 | */ |
| 161 | offset = phys_addr & ~PAGE_MASK; |
| 162 | phys_addr &= PAGE_MASK; |
| 163 | size = PAGE_ALIGN(last_addr+1) - phys_addr; |
| 164 | |
| 165 | /* |
| 166 | * Ok, go for it.. |
| 167 | */ |
| 168 | area = get_vm_area(size, VM_IOREMAP); |
| 169 | if (!area) |
| 170 | return NULL; |
| 171 | area->phys_addr = phys_addr; |
Paul Mundt | b66c1a3 | 2006-01-16 22:14:15 -0800 | [diff] [blame] | 172 | orig_addr = addr = (unsigned long)area->addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
Paul Mundt | b66c1a3 | 2006-01-16 22:14:15 -0800 | [diff] [blame] | 174 | #ifdef CONFIG_32BIT |
| 175 | /* |
| 176 | * First try to remap through the PMB once a valid VMA has been |
| 177 | * established. Smaller allocations (or the rest of the size |
| 178 | * remaining after a PMB mapping due to the size not being |
| 179 | * perfectly aligned on a PMB size boundary) are then mapped |
| 180 | * through the UTLB using conventional page tables. |
| 181 | * |
| 182 | * PMB entries are all pre-faulted. |
| 183 | */ |
| 184 | if (unlikely(size >= 0x1000000)) { |
| 185 | unsigned long mapped = pmb_remap(addr, phys_addr, size, flags); |
| 186 | |
| 187 | if (likely(mapped)) { |
| 188 | addr += mapped; |
| 189 | phys_addr += mapped; |
| 190 | size -= mapped; |
| 191 | } |
| 192 | } |
| 193 | #endif |
| 194 | |
| 195 | if (likely(size)) |
| 196 | if (remap_area_pages(addr, phys_addr, size, flags)) { |
| 197 | vunmap((void *)orig_addr); |
| 198 | return NULL; |
| 199 | } |
| 200 | |
| 201 | return (void __iomem *)(offset + (char *)orig_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | } |
Paul Mundt | b66c1a3 | 2006-01-16 22:14:15 -0800 | [diff] [blame] | 203 | EXPORT_SYMBOL(__ioremap); |
| 204 | |
| 205 | void __iounmap(void __iomem *addr) |
| 206 | { |
| 207 | unsigned long vaddr = (unsigned long __force)addr; |
| 208 | struct vm_struct *p; |
| 209 | |
Paul Mundt | a3e61d5 | 2006-09-27 16:45:22 +0900 | [diff] [blame^] | 210 | if (PXSEG(vaddr) < P3SEG || is_pci_memaddr(vaddr)) |
Paul Mundt | b66c1a3 | 2006-01-16 22:14:15 -0800 | [diff] [blame] | 211 | return; |
| 212 | |
| 213 | #ifdef CONFIG_32BIT |
| 214 | /* |
| 215 | * Purge any PMB entries that may have been established for this |
| 216 | * mapping, then proceed with conventional VMA teardown. |
| 217 | * |
| 218 | * XXX: Note that due to the way that remove_vm_area() does |
| 219 | * matching of the resultant VMA, we aren't able to fast-forward |
| 220 | * the address past the PMB space until the end of the VMA where |
| 221 | * the page tables reside. As such, unmap_vm_area() will be |
| 222 | * forced to linearly scan over the area until it finds the page |
| 223 | * tables where PTEs that need to be unmapped actually reside, |
| 224 | * which is far from optimal. Perhaps we need to use a separate |
| 225 | * VMA for the PMB mappings? |
| 226 | * -- PFM. |
| 227 | */ |
| 228 | pmb_unmap(vaddr); |
| 229 | #endif |
| 230 | |
| 231 | p = remove_vm_area((void *)(vaddr & PAGE_MASK)); |
| 232 | if (!p) { |
| 233 | printk(KERN_ERR "%s: bad address %p\n", __FUNCTION__, addr); |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | kfree(p); |
| 238 | } |
| 239 | EXPORT_SYMBOL(__iounmap); |