Matt Fleming | 4d35b93 | 2009-11-05 07:54:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Re-map IO memory to kernel address space so that we can access it. |
| 3 | * |
| 4 | * These functions should only be used when it is necessary to map a |
| 5 | * physical address space into the kernel address space before ioremap() |
| 6 | * can be used, e.g. early in boot before paging_init(). |
| 7 | * |
| 8 | * Copyright (C) 2009 Matt Fleming |
| 9 | */ |
| 10 | |
| 11 | #include <linux/vmalloc.h> |
| 12 | #include <linux/ioport.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/mm.h> |
| 15 | #include <linux/io.h> |
| 16 | #include <linux/bootmem.h> |
| 17 | #include <linux/proc_fs.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <asm/fixmap.h> |
| 20 | #include <asm/page.h> |
| 21 | #include <asm/pgalloc.h> |
| 22 | #include <asm/addrspace.h> |
| 23 | #include <asm/cacheflush.h> |
| 24 | #include <asm/tlbflush.h> |
| 25 | #include <asm/mmu.h> |
| 26 | #include <asm/mmu_context.h> |
| 27 | |
| 28 | struct ioremap_map { |
| 29 | void __iomem *addr; |
| 30 | unsigned long size; |
| 31 | unsigned long fixmap_addr; |
| 32 | }; |
| 33 | |
| 34 | static struct ioremap_map ioremap_maps[FIX_N_IOREMAPS]; |
| 35 | |
| 36 | void __init ioremap_fixed_init(void) |
| 37 | { |
| 38 | struct ioremap_map *map; |
| 39 | int i; |
| 40 | |
| 41 | for (i = 0; i < FIX_N_IOREMAPS; i++) { |
| 42 | map = &ioremap_maps[i]; |
| 43 | map->fixmap_addr = __fix_to_virt(FIX_IOREMAP_BEGIN + i); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | void __init __iomem * |
| 48 | ioremap_fixed(resource_size_t phys_addr, unsigned long size, pgprot_t prot) |
| 49 | { |
| 50 | enum fixed_addresses idx0, idx; |
| 51 | resource_size_t last_addr; |
| 52 | struct ioremap_map *map; |
| 53 | unsigned long offset; |
| 54 | unsigned int nrpages; |
| 55 | int i, slot; |
| 56 | |
| 57 | slot = -1; |
| 58 | for (i = 0; i < FIX_N_IOREMAPS; i++) { |
| 59 | map = &ioremap_maps[i]; |
| 60 | if (!map->addr) { |
| 61 | map->size = size; |
| 62 | slot = i; |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | if (slot < 0) |
| 68 | return NULL; |
| 69 | |
| 70 | /* Don't allow wraparound or zero size */ |
| 71 | last_addr = phys_addr + size - 1; |
| 72 | if (!size || last_addr < phys_addr) |
| 73 | return NULL; |
| 74 | |
| 75 | /* |
| 76 | * Fixmap mappings have to be page-aligned |
| 77 | */ |
| 78 | offset = phys_addr & ~PAGE_MASK; |
| 79 | phys_addr &= PAGE_MASK; |
| 80 | size = PAGE_ALIGN(last_addr + 1) - phys_addr; |
| 81 | |
| 82 | /* |
| 83 | * Mappings have to fit in the FIX_IOREMAP area. |
| 84 | */ |
| 85 | nrpages = size >> PAGE_SHIFT; |
| 86 | if (nrpages > FIX_N_IOREMAPS) |
| 87 | return NULL; |
| 88 | |
| 89 | /* |
| 90 | * Ok, go for it.. |
| 91 | */ |
| 92 | idx0 = FIX_IOREMAP_BEGIN + slot; |
| 93 | idx = idx0; |
| 94 | while (nrpages > 0) { |
| 95 | pgprot_val(prot) |= _PAGE_WIRED; |
| 96 | __set_fixmap(idx, phys_addr, prot); |
| 97 | phys_addr += PAGE_SIZE; |
| 98 | idx++; |
| 99 | --nrpages; |
| 100 | } |
| 101 | |
| 102 | map->addr = (void __iomem *)(offset + map->fixmap_addr); |
| 103 | return map->addr; |
| 104 | } |
| 105 | |
Paul Mundt | 4f744af | 2010-01-18 21:30:29 +0900 | [diff] [blame^] | 106 | int iounmap_fixed(void __iomem *addr) |
Matt Fleming | 4d35b93 | 2009-11-05 07:54:17 +0000 | [diff] [blame] | 107 | { |
| 108 | enum fixed_addresses idx; |
| 109 | unsigned long virt_addr; |
| 110 | struct ioremap_map *map; |
| 111 | unsigned long offset; |
| 112 | unsigned int nrpages; |
| 113 | int i, slot; |
| 114 | pgprot_t prot; |
| 115 | |
| 116 | slot = -1; |
| 117 | for (i = 0; i < FIX_N_IOREMAPS; i++) { |
| 118 | map = &ioremap_maps[i]; |
| 119 | if (map->addr == addr) { |
| 120 | slot = i; |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | |
Paul Mundt | 4f744af | 2010-01-18 21:30:29 +0900 | [diff] [blame^] | 125 | /* |
| 126 | * If we don't match, it's not for us. |
| 127 | */ |
Matt Fleming | 4d35b93 | 2009-11-05 07:54:17 +0000 | [diff] [blame] | 128 | if (slot < 0) |
Paul Mundt | 4f744af | 2010-01-18 21:30:29 +0900 | [diff] [blame^] | 129 | return -EINVAL; |
Matt Fleming | 4d35b93 | 2009-11-05 07:54:17 +0000 | [diff] [blame] | 130 | |
| 131 | virt_addr = (unsigned long)addr; |
| 132 | |
| 133 | offset = virt_addr & ~PAGE_MASK; |
| 134 | nrpages = PAGE_ALIGN(offset + map->size - 1) >> PAGE_SHIFT; |
| 135 | |
| 136 | pgprot_val(prot) = _PAGE_WIRED; |
| 137 | |
| 138 | idx = FIX_IOREMAP_BEGIN + slot + nrpages; |
| 139 | while (nrpages > 0) { |
| 140 | __clear_fixmap(idx, prot); |
| 141 | --idx; |
| 142 | --nrpages; |
| 143 | } |
| 144 | |
| 145 | map->size = 0; |
| 146 | map->addr = NULL; |
Paul Mundt | 4f744af | 2010-01-18 21:30:29 +0900 | [diff] [blame^] | 147 | |
| 148 | return 0; |
Matt Fleming | 4d35b93 | 2009-11-05 07:54:17 +0000 | [diff] [blame] | 149 | } |