blob: 9dd3cb905971a103498f7c7381bd811abf767e5c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Re-map IO memory to kernel address space so that we can access it.
3 * This is needed for high PCI addresses that aren't mapped in the
4 * 640k-1MB IO memory area on PC's
5 *
6 * (C) Copyright 1995 1996 Linus Torvalds
7 */
8
Thomas Gleixnere9332ca2008-01-30 13:34:05 +01009#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/init.h>
Haavard Skinnemoena148ecf2006-09-30 23:29:17 -070011#include <linux/io.h>
Thomas Gleixner3cbd09e2008-01-30 13:34:05 +010012#include <linux/module.h>
13#include <linux/slab.h>
14#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Thomas Gleixner3cbd09e2008-01-30 13:34:05 +010016#include <asm/cacheflush.h>
17#include <asm/e820.h>
18#include <asm/fixmap.h>
19#include <asm/pgtable.h>
20#include <asm/tlbflush.h>
Jeremy Fitzhardingef6df72e2008-01-30 13:34:11 +010021#include <asm/pgalloc.h>
venkatesh.pallipadi@intel.comd7677d42008-03-18 17:00:17 -070022#include <asm/pat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Thomas Gleixner240d3a72008-01-30 13:34:05 +010024#ifdef CONFIG_X86_64
25
Thomas Gleixnere3100c82008-02-27 20:57:40 +010026static inline int phys_addr_valid(unsigned long addr)
27{
28 return addr < (1UL << boot_cpu_data.x86_phys_bits);
29}
30
Jiri Slaby59ea7462008-06-12 13:56:40 +020031unsigned long __phys_addr(unsigned long x)
32{
33 if (x >= __START_KERNEL_map) {
34 x -= __START_KERNEL_map;
35 VIRTUAL_BUG_ON(x >= KERNEL_IMAGE_SIZE);
36 x += phys_base;
37 } else {
38 VIRTUAL_BUG_ON(x < PAGE_OFFSET);
39 x -= PAGE_OFFSET;
40 VIRTUAL_BUG_ON(system_state == SYSTEM_BOOTING ? x > MAXMEM :
41 !phys_addr_valid(x));
42 }
43 return x;
44}
45EXPORT_SYMBOL(__phys_addr);
46
Thomas Gleixnere3100c82008-02-27 20:57:40 +010047#else
48
49static inline int phys_addr_valid(unsigned long addr)
50{
51 return 1;
52}
53
Jiri Slabya1bf9632008-06-12 13:56:40 +020054#ifdef CONFIG_DEBUG_VIRTUAL
Jiri Slaby59ea7462008-06-12 13:56:40 +020055unsigned long __phys_addr(unsigned long x)
56{
57 /* VMALLOC_* aren't constants; not available at the boot time */
58 VIRTUAL_BUG_ON(x < PAGE_OFFSET || (system_state != SYSTEM_BOOTING &&
59 is_vmalloc_addr((void *)x)));
60 return x - PAGE_OFFSET;
61}
62EXPORT_SYMBOL(__phys_addr);
Jiri Slabya1bf9632008-06-12 13:56:40 +020063#endif
Jiri Slaby59ea7462008-06-12 13:56:40 +020064
Thomas Gleixner240d3a72008-01-30 13:34:05 +010065#endif
66
Thomas Gleixner5f5192b2008-01-30 13:34:06 +010067int page_is_ram(unsigned long pagenr)
68{
Ingo Molnar756a6c62008-03-25 08:31:17 +010069 resource_size_t addr, end;
Thomas Gleixner5f5192b2008-01-30 13:34:06 +010070 int i;
71
Arjan van de Vend8a9e6a2008-02-18 09:54:33 -080072 /*
73 * A special case is the first 4Kb of memory;
74 * This is a BIOS owned area, not kernel ram, but generally
75 * not listed as such in the E820 table.
76 */
77 if (pagenr == 0)
78 return 0;
79
Arjan van de Ven156fbc32008-02-18 09:58:45 -080080 /*
81 * Second special case: Some BIOSen report the PC BIOS
82 * area (640->1Mb) as ram even though it is not.
83 */
84 if (pagenr >= (BIOS_BEGIN >> PAGE_SHIFT) &&
85 pagenr < (BIOS_END >> PAGE_SHIFT))
86 return 0;
Arjan van de Vend8a9e6a2008-02-18 09:54:33 -080087
Thomas Gleixner5f5192b2008-01-30 13:34:06 +010088 for (i = 0; i < e820.nr_map; i++) {
89 /*
90 * Not usable memory:
91 */
92 if (e820.map[i].type != E820_RAM)
93 continue;
Thomas Gleixner5f5192b2008-01-30 13:34:06 +010094 addr = (e820.map[i].addr + PAGE_SIZE-1) >> PAGE_SHIFT;
95 end = (e820.map[i].addr + e820.map[i].size) >> PAGE_SHIFT;
Thomas Gleixner950f9d92008-01-30 13:34:06 +010096
Thomas Gleixner950f9d92008-01-30 13:34:06 +010097
Thomas Gleixner5f5192b2008-01-30 13:34:06 +010098 if ((pagenr >= addr) && (pagenr < end))
99 return 1;
100 }
101 return 0;
102}
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/*
Thomas Gleixnere9332ca2008-01-30 13:34:05 +0100105 * Fix up the linear direct mapping of the kernel to avoid cache attribute
106 * conflicts.
107 */
venkatesh.pallipadi@intel.com3a96ce82008-03-18 17:00:16 -0700108int ioremap_change_attr(unsigned long vaddr, unsigned long size,
109 unsigned long prot_val)
Thomas Gleixnere9332ca2008-01-30 13:34:05 +0100110{
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +0100111 unsigned long nrpages = size >> PAGE_SHIFT;
Harvey Harrison93809be2008-02-01 17:49:43 +0100112 int err;
Thomas Gleixnere9332ca2008-01-30 13:34:05 +0100113
venkatesh.pallipadi@intel.com3a96ce82008-03-18 17:00:16 -0700114 switch (prot_val) {
115 case _PAGE_CACHE_UC:
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +0100116 default:
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -0700117 err = _set_memory_uc(vaddr, nrpages);
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +0100118 break;
venkatesh.pallipadi@intel.comb310f381d2008-03-18 17:00:24 -0700119 case _PAGE_CACHE_WC:
120 err = _set_memory_wc(vaddr, nrpages);
121 break;
venkatesh.pallipadi@intel.com3a96ce82008-03-18 17:00:16 -0700122 case _PAGE_CACHE_WB:
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -0700123 err = _set_memory_wb(vaddr, nrpages);
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +0100124 break;
125 }
Thomas Gleixnere9332ca2008-01-30 13:34:05 +0100126
127 return err;
128}
129
130/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * Remap an arbitrary physical address space into the kernel virtual
132 * address space. Needed when the kernel wants to access high addresses
133 * directly.
134 *
135 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
136 * have to convert them into an offset in a page-aligned mapping, but the
137 * caller shouldn't need to know that small detail.
138 */
Christoph Lameter23016962008-04-28 02:12:42 -0700139static void __iomem *__ioremap_caller(resource_size_t phys_addr,
140 unsigned long size, unsigned long prot_val, void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Ingo Molnar756a6c62008-03-25 08:31:17 +0100142 unsigned long pfn, offset, vaddr;
143 resource_size_t last_addr;
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100144 struct vm_struct *area;
venkatesh.pallipadi@intel.comd7677d42008-03-18 17:00:17 -0700145 unsigned long new_prot_val;
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +0100146 pgprot_t prot;
Venki Pallipadidee7cbb2008-03-24 14:39:55 -0700147 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 /* Don't allow wraparound or zero size */
150 last_addr = phys_addr + size - 1;
151 if (!size || last_addr < phys_addr)
152 return NULL;
153
Thomas Gleixnere3100c82008-02-27 20:57:40 +0100154 if (!phys_addr_valid(phys_addr)) {
venkatesh.pallipadi@intel.com6997ab42008-03-18 17:00:25 -0700155 printk(KERN_WARNING "ioremap: invalid physical address %llx\n",
Randy Dunlap4c8337a2008-04-10 15:09:50 -0700156 (unsigned long long)phys_addr);
Thomas Gleixnere3100c82008-02-27 20:57:40 +0100157 WARN_ON_ONCE(1);
158 return NULL;
159 }
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 /*
162 * Don't remap the low PCI/ISA area, it's always mapped..
163 */
164 if (phys_addr >= ISA_START_ADDRESS && last_addr < ISA_END_ADDRESS)
Thomas Gleixner4b40fce2008-01-30 13:34:05 +0100165 return (__force void __iomem *)phys_to_virt(phys_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 /*
168 * Don't allow anybody to remap normal RAM that we're using..
169 */
Andres Salomoncb8ab682008-04-30 11:30:24 -0400170 for (pfn = phys_addr >> PAGE_SHIFT;
171 (pfn << PAGE_SHIFT) < (last_addr & PAGE_MASK);
172 pfn++) {
Ingo Molnarbdd3cee2008-02-28 14:10:49 +0100173
Ingo Molnarba748d22008-03-03 09:37:41 +0100174 int is_ram = page_is_ram(pfn);
175
176 if (is_ram && pfn_valid(pfn) && !PageReserved(pfn_to_page(pfn)))
Thomas Gleixner266b9f82008-01-30 13:34:06 +0100177 return NULL;
Ingo Molnarba748d22008-03-03 09:37:41 +0100178 WARN_ON_ONCE(is_ram);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
180
venkatesh.pallipadi@intel.comd7677d42008-03-18 17:00:17 -0700181 /*
182 * Mappings have to be page-aligned
183 */
184 offset = phys_addr & ~PAGE_MASK;
185 phys_addr &= PAGE_MASK;
186 size = PAGE_ALIGN(last_addr+1) - phys_addr;
187
Venki Pallipadidee7cbb2008-03-24 14:39:55 -0700188 retval = reserve_memtype(phys_addr, phys_addr + size,
189 prot_val, &new_prot_val);
190 if (retval) {
Venki Pallipadib450e5e2008-03-25 16:51:26 -0700191 pr_debug("Warning: reserve_memtype returned %d\n", retval);
Venki Pallipadidee7cbb2008-03-24 14:39:55 -0700192 return NULL;
193 }
194
195 if (prot_val != new_prot_val) {
venkatesh.pallipadi@intel.comd7677d42008-03-18 17:00:17 -0700196 /*
197 * Do not fallback to certain memory types with certain
198 * requested type:
Suresh Siddhade33c442008-04-25 17:07:22 -0700199 * - request is uc-, return cannot be write-back
200 * - request is uc-, return cannot be write-combine
venkatesh.pallipadi@intel.comb310f381d2008-03-18 17:00:24 -0700201 * - request is write-combine, return cannot be write-back
venkatesh.pallipadi@intel.comd7677d42008-03-18 17:00:17 -0700202 */
Suresh Siddhade33c442008-04-25 17:07:22 -0700203 if ((prot_val == _PAGE_CACHE_UC_MINUS &&
venkatesh.pallipadi@intel.comb310f381d2008-03-18 17:00:24 -0700204 (new_prot_val == _PAGE_CACHE_WB ||
205 new_prot_val == _PAGE_CACHE_WC)) ||
206 (prot_val == _PAGE_CACHE_WC &&
venkatesh.pallipadi@intel.comd7677d42008-03-18 17:00:17 -0700207 new_prot_val == _PAGE_CACHE_WB)) {
Venki Pallipadib450e5e2008-03-25 16:51:26 -0700208 pr_debug(
venkatesh.pallipadi@intel.com6997ab42008-03-18 17:00:25 -0700209 "ioremap error for 0x%llx-0x%llx, requested 0x%lx, got 0x%lx\n",
Randy Dunlap4c8337a2008-04-10 15:09:50 -0700210 (unsigned long long)phys_addr,
211 (unsigned long long)(phys_addr + size),
venkatesh.pallipadi@intel.com6997ab42008-03-18 17:00:25 -0700212 prot_val, new_prot_val);
venkatesh.pallipadi@intel.comd7677d42008-03-18 17:00:17 -0700213 free_memtype(phys_addr, phys_addr + size);
214 return NULL;
215 }
216 prot_val = new_prot_val;
217 }
218
venkatesh.pallipadi@intel.com3a96ce82008-03-18 17:00:16 -0700219 switch (prot_val) {
220 case _PAGE_CACHE_UC:
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +0100221 default:
Ingo Molnar55c62682008-03-26 06:19:45 +0100222 prot = PAGE_KERNEL_NOCACHE;
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +0100223 break;
Suresh Siddhade33c442008-04-25 17:07:22 -0700224 case _PAGE_CACHE_UC_MINUS:
225 prot = PAGE_KERNEL_UC_MINUS;
226 break;
venkatesh.pallipadi@intel.comb310f381d2008-03-18 17:00:24 -0700227 case _PAGE_CACHE_WC:
228 prot = PAGE_KERNEL_WC;
229 break;
venkatesh.pallipadi@intel.com3a96ce82008-03-18 17:00:16 -0700230 case _PAGE_CACHE_WB:
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +0100231 prot = PAGE_KERNEL;
232 break;
233 }
Haavard Skinnemoena148ecf2006-09-30 23:29:17 -0700234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 * Ok, go for it..
237 */
Christoph Lameter23016962008-04-28 02:12:42 -0700238 area = get_vm_area_caller(size, VM_IOREMAP, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (!area)
240 return NULL;
241 area->phys_addr = phys_addr;
Thomas Gleixnere66aadb2008-02-04 16:48:05 +0100242 vaddr = (unsigned long) area->addr;
243 if (ioremap_page_range(vaddr, vaddr + size, phys_addr, prot)) {
venkatesh.pallipadi@intel.comd7677d42008-03-18 17:00:17 -0700244 free_memtype(phys_addr, phys_addr + size);
Ingo Molnarb16bf712008-02-28 14:02:08 +0100245 free_vm_area(area);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return NULL;
247 }
Thomas Gleixnere9332ca2008-01-30 13:34:05 +0100248
venkatesh.pallipadi@intel.com3a96ce82008-03-18 17:00:16 -0700249 if (ioremap_change_attr(vaddr, size, prot_val) < 0) {
venkatesh.pallipadi@intel.comd7677d42008-03-18 17:00:17 -0700250 free_memtype(phys_addr, phys_addr + size);
Thomas Gleixnere66aadb2008-02-04 16:48:05 +0100251 vunmap(area->addr);
Thomas Gleixnere9332ca2008-01-30 13:34:05 +0100252 return NULL;
253 }
254
Thomas Gleixnere66aadb2008-02-04 16:48:05 +0100255 return (void __iomem *) (vaddr + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258/**
259 * ioremap_nocache - map bus memory into CPU space
260 * @offset: bus address of the memory
261 * @size: size of the resource to map
262 *
263 * ioremap_nocache performs a platform specific sequence of operations to
264 * make bus memory CPU accessible via the readb/readw/readl/writeb/
265 * writew/writel functions and the other mmio helpers. The returned
266 * address is not guaranteed to be usable directly as a virtual
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100267 * address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 *
269 * This version of ioremap ensures that the memory is marked uncachable
270 * on the CPU as well as honouring existing caching rules from things like
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100271 * the PCI bus. Note that there are other caches and buffers on many
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 * busses. In particular driver authors should read up on PCI writes
273 *
274 * It's useful if some control registers are in such an area and
275 * write combining or read caching is not desirable:
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100276 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 * Must be freed with iounmap.
278 */
Linus Torvaldsb9e76a02008-03-24 11:22:39 -0700279void __iomem *ioremap_nocache(resource_size_t phys_addr, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Suresh Siddhade33c442008-04-25 17:07:22 -0700281 /*
282 * Ideally, this should be:
283 * pat_wc_enabled ? _PAGE_CACHE_UC : _PAGE_CACHE_UC_MINUS;
284 *
285 * Till we fix all X drivers to use ioremap_wc(), we will use
286 * UC MINUS.
287 */
288 unsigned long val = _PAGE_CACHE_UC_MINUS;
289
290 return __ioremap_caller(phys_addr, size, val,
Christoph Lameter23016962008-04-28 02:12:42 -0700291 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700293EXPORT_SYMBOL(ioremap_nocache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
venkatesh.pallipadi@intel.comb310f381d2008-03-18 17:00:24 -0700295/**
296 * ioremap_wc - map memory into CPU space write combined
297 * @offset: bus address of the memory
298 * @size: size of the resource to map
299 *
300 * This version of ioremap ensures that the memory is marked write combining.
301 * Write combining allows faster writes to some hardware devices.
302 *
303 * Must be freed with iounmap.
304 */
305void __iomem *ioremap_wc(unsigned long phys_addr, unsigned long size)
306{
307 if (pat_wc_enabled)
Christoph Lameter23016962008-04-28 02:12:42 -0700308 return __ioremap_caller(phys_addr, size, _PAGE_CACHE_WC,
309 __builtin_return_address(0));
venkatesh.pallipadi@intel.comb310f381d2008-03-18 17:00:24 -0700310 else
311 return ioremap_nocache(phys_addr, size);
312}
313EXPORT_SYMBOL(ioremap_wc);
314
Linus Torvaldsb9e76a02008-03-24 11:22:39 -0700315void __iomem *ioremap_cache(resource_size_t phys_addr, unsigned long size)
Thomas Gleixner5f868152008-01-30 13:34:06 +0100316{
Christoph Lameter23016962008-04-28 02:12:42 -0700317 return __ioremap_caller(phys_addr, size, _PAGE_CACHE_WB,
318 __builtin_return_address(0));
Thomas Gleixner5f868152008-01-30 13:34:06 +0100319}
320EXPORT_SYMBOL(ioremap_cache);
321
Andi Kleenbf5421c2005-12-12 22:17:09 -0800322/**
323 * iounmap - Free a IO remapping
324 * @addr: virtual address from ioremap_*
325 *
326 * Caller must ensure there is only one unmapping for the same pointer.
327 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328void iounmap(volatile void __iomem *addr)
329{
Andi Kleenbf5421c2005-12-12 22:17:09 -0800330 struct vm_struct *p, *o;
Andrew Mortonc23a4e962005-07-07 17:56:02 -0700331
332 if ((void __force *)addr <= high_memory)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return;
334
335 /*
336 * __ioremap special-cases the PCI/ISA range by not instantiating a
337 * vm_area and by simply returning an address into the kernel mapping
338 * of ISA space. So handle that here.
339 */
340 if (addr >= phys_to_virt(ISA_START_ADDRESS) &&
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100341 addr < phys_to_virt(ISA_END_ADDRESS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return;
343
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100344 addr = (volatile void __iomem *)
345 (PAGE_MASK & (unsigned long __force)addr);
Andi Kleenbf5421c2005-12-12 22:17:09 -0800346
347 /* Use the vm area unlocked, assuming the caller
348 ensures there isn't another iounmap for the same address
349 in parallel. Reuse of the virtual address is prevented by
350 leaving it in the global lists until we're done with it.
351 cpa takes care of the direct mappings. */
352 read_lock(&vmlist_lock);
353 for (p = vmlist; p; p = p->next) {
354 if (p->addr == addr)
355 break;
356 }
357 read_unlock(&vmlist_lock);
358
359 if (!p) {
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100360 printk(KERN_ERR "iounmap: bad address %p\n", addr);
Andrew Mortonc23a4e962005-07-07 17:56:02 -0700361 dump_stack();
Andi Kleenbf5421c2005-12-12 22:17:09 -0800362 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
364
venkatesh.pallipadi@intel.comd7677d42008-03-18 17:00:17 -0700365 free_memtype(p->phys_addr, p->phys_addr + get_vm_area_size(p));
366
Andi Kleenbf5421c2005-12-12 22:17:09 -0800367 /* Finally remove it */
368 o = remove_vm_area((void *)addr);
369 BUG_ON(p != o || o == NULL);
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100370 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700372EXPORT_SYMBOL(iounmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700374/*
375 * Convert a physical pointer to a virtual kernel pointer for /dev/mem
376 * access
377 */
378void *xlate_dev_mem_ptr(unsigned long phys)
379{
380 void *addr;
381 unsigned long start = phys & PAGE_MASK;
382
383 /* If page is RAM, we can use __va. Otherwise ioremap and unmap. */
384 if (page_is_ram(start >> PAGE_SHIFT))
385 return __va(phys);
386
387 addr = (void *)ioremap(start, PAGE_SIZE);
388 if (addr)
389 addr = (void *)((unsigned long)addr | (phys & ~PAGE_MASK));
390
391 return addr;
392}
393
394void unxlate_dev_mem_ptr(unsigned long phys, void *addr)
395{
396 if (page_is_ram(phys >> PAGE_SHIFT))
397 return;
398
399 iounmap((void __iomem *)((unsigned long)addr & PAGE_MASK));
400 return;
401}
402
Thomas Gleixner240d3a72008-01-30 13:34:05 +0100403#ifdef CONFIG_X86_32
Ingo Molnard18d6d62008-01-30 13:33:45 +0100404
405int __initdata early_ioremap_debug;
406
407static int __init early_ioremap_debug_setup(char *str)
408{
409 early_ioremap_debug = 1;
410
Huang, Ying793b24a2008-01-30 13:33:45 +0100411 return 0;
Ingo Molnard18d6d62008-01-30 13:33:45 +0100412}
Huang, Ying793b24a2008-01-30 13:33:45 +0100413early_param("early_ioremap_debug", early_ioremap_debug_setup);
Ingo Molnard18d6d62008-01-30 13:33:45 +0100414
Huang, Ying0947b2f2008-01-30 13:33:44 +0100415static __initdata int after_paging_init;
Ian Campbellc92a7a52008-02-17 19:09:42 +0000416static pte_t bm_pte[PAGE_SIZE/sizeof(pte_t)]
417 __section(.bss.page_aligned);
Huang, Ying0947b2f2008-01-30 13:33:44 +0100418
Ian Campbell551889a2008-02-09 23:24:09 +0100419static inline pmd_t * __init early_ioremap_pmd(unsigned long addr)
Huang, Ying0947b2f2008-01-30 13:33:44 +0100420{
Jeremy Fitzhardinge37cc8d72008-02-13 16:20:35 +0100421 /* Don't assume we're using swapper_pg_dir at this point */
422 pgd_t *base = __va(read_cr3());
423 pgd_t *pgd = &base[pgd_index(addr)];
Ian Campbell551889a2008-02-09 23:24:09 +0100424 pud_t *pud = pud_offset(pgd, addr);
425 pmd_t *pmd = pmd_offset(pud, addr);
426
427 return pmd;
Huang, Ying0947b2f2008-01-30 13:33:44 +0100428}
429
Ian Campbell551889a2008-02-09 23:24:09 +0100430static inline pte_t * __init early_ioremap_pte(unsigned long addr)
Huang, Ying0947b2f2008-01-30 13:33:44 +0100431{
Ian Campbell551889a2008-02-09 23:24:09 +0100432 return &bm_pte[pte_index(addr)];
Huang, Ying0947b2f2008-01-30 13:33:44 +0100433}
434
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100435void __init early_ioremap_init(void)
Huang, Ying0947b2f2008-01-30 13:33:44 +0100436{
Ian Campbell551889a2008-02-09 23:24:09 +0100437 pmd_t *pmd;
Huang, Ying0947b2f2008-01-30 13:33:44 +0100438
Ingo Molnard18d6d62008-01-30 13:33:45 +0100439 if (early_ioremap_debug)
Ingo Molnaradafdf62008-01-30 13:34:08 +0100440 printk(KERN_INFO "early_ioremap_init()\n");
Ingo Molnard18d6d62008-01-30 13:33:45 +0100441
Ian Campbell551889a2008-02-09 23:24:09 +0100442 pmd = early_ioremap_pmd(fix_to_virt(FIX_BTMAP_BEGIN));
Huang, Ying0947b2f2008-01-30 13:33:44 +0100443 memset(bm_pte, 0, sizeof(bm_pte));
Ian Campbellb6fbb662008-02-09 23:24:09 +0100444 pmd_populate_kernel(&init_mm, pmd, bm_pte);
Ian Campbell551889a2008-02-09 23:24:09 +0100445
Ingo Molnar0e3a9542008-01-30 13:33:49 +0100446 /*
Ian Campbell551889a2008-02-09 23:24:09 +0100447 * The boot-ioremap range spans multiple pmds, for which
Ingo Molnar0e3a9542008-01-30 13:33:49 +0100448 * we are not prepared:
449 */
Ian Campbell551889a2008-02-09 23:24:09 +0100450 if (pmd != early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END))) {
Ingo Molnar0e3a9542008-01-30 13:33:49 +0100451 WARN_ON(1);
Ian Campbell551889a2008-02-09 23:24:09 +0100452 printk(KERN_WARNING "pmd %p != %p\n",
453 pmd, early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END)));
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100454 printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
Ian Campbell551889a2008-02-09 23:24:09 +0100455 fix_to_virt(FIX_BTMAP_BEGIN));
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100456 printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_END): %08lx\n",
Ian Campbell551889a2008-02-09 23:24:09 +0100457 fix_to_virt(FIX_BTMAP_END));
Ingo Molnar0e3a9542008-01-30 13:33:49 +0100458
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100459 printk(KERN_WARNING "FIX_BTMAP_END: %d\n", FIX_BTMAP_END);
460 printk(KERN_WARNING "FIX_BTMAP_BEGIN: %d\n",
461 FIX_BTMAP_BEGIN);
Ingo Molnar0e3a9542008-01-30 13:33:49 +0100462 }
Huang, Ying0947b2f2008-01-30 13:33:44 +0100463}
464
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100465void __init early_ioremap_clear(void)
Huang, Ying0947b2f2008-01-30 13:33:44 +0100466{
Ian Campbell551889a2008-02-09 23:24:09 +0100467 pmd_t *pmd;
Huang, Ying0947b2f2008-01-30 13:33:44 +0100468
Ingo Molnard18d6d62008-01-30 13:33:45 +0100469 if (early_ioremap_debug)
Ingo Molnaradafdf62008-01-30 13:34:08 +0100470 printk(KERN_INFO "early_ioremap_clear()\n");
Ingo Molnard18d6d62008-01-30 13:33:45 +0100471
Ian Campbell551889a2008-02-09 23:24:09 +0100472 pmd = early_ioremap_pmd(fix_to_virt(FIX_BTMAP_BEGIN));
473 pmd_clear(pmd);
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700474 paravirt_release_pte(__pa(bm_pte) >> PAGE_SHIFT);
Huang, Ying0947b2f2008-01-30 13:33:44 +0100475 __flush_tlb_all();
476}
477
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100478void __init early_ioremap_reset(void)
Huang, Ying0947b2f2008-01-30 13:33:44 +0100479{
480 enum fixed_addresses idx;
Ian Campbell551889a2008-02-09 23:24:09 +0100481 unsigned long addr, phys;
482 pte_t *pte;
Huang, Ying0947b2f2008-01-30 13:33:44 +0100483
484 after_paging_init = 1;
Huang, Ying64a8f852008-01-30 13:33:44 +0100485 for (idx = FIX_BTMAP_BEGIN; idx >= FIX_BTMAP_END; idx--) {
Huang, Ying0947b2f2008-01-30 13:33:44 +0100486 addr = fix_to_virt(idx);
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100487 pte = early_ioremap_pte(addr);
Ian Campbell551889a2008-02-09 23:24:09 +0100488 if (pte_present(*pte)) {
489 phys = pte_val(*pte) & PAGE_MASK;
Huang, Ying0947b2f2008-01-30 13:33:44 +0100490 set_fixmap(idx, phys);
491 }
492 }
493}
494
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100495static void __init __early_set_fixmap(enum fixed_addresses idx,
Huang, Ying0947b2f2008-01-30 13:33:44 +0100496 unsigned long phys, pgprot_t flags)
497{
Ian Campbell551889a2008-02-09 23:24:09 +0100498 unsigned long addr = __fix_to_virt(idx);
499 pte_t *pte;
Huang, Ying0947b2f2008-01-30 13:33:44 +0100500
501 if (idx >= __end_of_fixed_addresses) {
502 BUG();
503 return;
504 }
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100505 pte = early_ioremap_pte(addr);
Huang, Ying0947b2f2008-01-30 13:33:44 +0100506 if (pgprot_val(flags))
Ian Campbell551889a2008-02-09 23:24:09 +0100507 set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
Huang, Ying0947b2f2008-01-30 13:33:44 +0100508 else
Ian Campbell551889a2008-02-09 23:24:09 +0100509 pte_clear(NULL, addr, pte);
Huang, Ying0947b2f2008-01-30 13:33:44 +0100510 __flush_tlb_one(addr);
511}
512
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100513static inline void __init early_set_fixmap(enum fixed_addresses idx,
Huang, Ying0947b2f2008-01-30 13:33:44 +0100514 unsigned long phys)
515{
516 if (after_paging_init)
517 set_fixmap(idx, phys);
518 else
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100519 __early_set_fixmap(idx, phys, PAGE_KERNEL);
Huang, Ying0947b2f2008-01-30 13:33:44 +0100520}
521
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100522static inline void __init early_clear_fixmap(enum fixed_addresses idx)
Huang, Ying0947b2f2008-01-30 13:33:44 +0100523{
524 if (after_paging_init)
525 clear_fixmap(idx);
526 else
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100527 __early_set_fixmap(idx, 0, __pgprot(0));
Huang, Ying0947b2f2008-01-30 13:33:44 +0100528}
529
Ingo Molnar1b42f512008-01-30 13:33:45 +0100530
531int __initdata early_ioremap_nested;
532
Ingo Molnard690b2a2008-01-30 13:33:47 +0100533static int __init check_early_ioremap_leak(void)
534{
535 if (!early_ioremap_nested)
536 return 0;
537
538 printk(KERN_WARNING
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100539 "Debug warning: early ioremap leak of %d areas detected.\n",
540 early_ioremap_nested);
Ingo Molnard690b2a2008-01-30 13:33:47 +0100541 printk(KERN_WARNING
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100542 "please boot with early_ioremap_debug and report the dmesg.\n");
Ingo Molnard690b2a2008-01-30 13:33:47 +0100543 WARN_ON(1);
544
545 return 1;
546}
547late_initcall(check_early_ioremap_leak);
548
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100549void __init *early_ioremap(unsigned long phys_addr, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 unsigned long offset, last_addr;
Ingo Molnar1b42f512008-01-30 13:33:45 +0100552 unsigned int nrpages, nesting;
553 enum fixed_addresses idx0, idx;
554
555 WARN_ON(system_state != SYSTEM_BOOTING);
556
557 nesting = early_ioremap_nested;
Ingo Molnard18d6d62008-01-30 13:33:45 +0100558 if (early_ioremap_debug) {
Ingo Molnaradafdf62008-01-30 13:34:08 +0100559 printk(KERN_INFO "early_ioremap(%08lx, %08lx) [%d] => ",
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100560 phys_addr, size, nesting);
Ingo Molnard18d6d62008-01-30 13:33:45 +0100561 dump_stack();
562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 /* Don't allow wraparound or zero size */
565 last_addr = phys_addr + size - 1;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100566 if (!size || last_addr < phys_addr) {
567 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return NULL;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100571 if (nesting >= FIX_BTMAPS_NESTING) {
572 WARN_ON(1);
Ingo Molnar1b42f512008-01-30 13:33:45 +0100573 return NULL;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100574 }
Ingo Molnar1b42f512008-01-30 13:33:45 +0100575 early_ioremap_nested++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 /*
577 * Mappings have to be page-aligned
578 */
579 offset = phys_addr & ~PAGE_MASK;
580 phys_addr &= PAGE_MASK;
581 size = PAGE_ALIGN(last_addr) - phys_addr;
582
583 /*
584 * Mappings have to fit in the FIX_BTMAP area.
585 */
586 nrpages = size >> PAGE_SHIFT;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100587 if (nrpages > NR_FIX_BTMAPS) {
588 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return NULL;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 /*
593 * Ok, go for it..
594 */
Ingo Molnar1b42f512008-01-30 13:33:45 +0100595 idx0 = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*nesting;
596 idx = idx0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 while (nrpages > 0) {
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100598 early_set_fixmap(idx, phys_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 phys_addr += PAGE_SIZE;
600 --idx;
601 --nrpages;
602 }
Ingo Molnard18d6d62008-01-30 13:33:45 +0100603 if (early_ioremap_debug)
604 printk(KERN_CONT "%08lx + %08lx\n", offset, fix_to_virt(idx0));
Ingo Molnar1b42f512008-01-30 13:33:45 +0100605
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100606 return (void *) (offset + fix_to_virt(idx0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100609void __init early_iounmap(void *addr, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 unsigned long virt_addr;
612 unsigned long offset;
613 unsigned int nrpages;
614 enum fixed_addresses idx;
Ingo Molnar226e9a92008-05-27 09:56:49 +0200615 int nesting;
Ingo Molnar1b42f512008-01-30 13:33:45 +0100616
617 nesting = --early_ioremap_nested;
Ingo Molnar226e9a92008-05-27 09:56:49 +0200618 if (WARN_ON(nesting < 0))
619 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Ingo Molnard18d6d62008-01-30 13:33:45 +0100621 if (early_ioremap_debug) {
Ingo Molnaradafdf62008-01-30 13:34:08 +0100622 printk(KERN_INFO "early_iounmap(%p, %08lx) [%d]\n", addr,
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100623 size, nesting);
Ingo Molnard18d6d62008-01-30 13:33:45 +0100624 dump_stack();
625 }
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 virt_addr = (unsigned long)addr;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100628 if (virt_addr < fix_to_virt(FIX_BTMAP_BEGIN)) {
629 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 offset = virt_addr & ~PAGE_MASK;
633 nrpages = PAGE_ALIGN(offset + size - 1) >> PAGE_SHIFT;
634
Ingo Molnar1b42f512008-01-30 13:33:45 +0100635 idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*nesting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 while (nrpages > 0) {
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100637 early_clear_fixmap(idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 --idx;
639 --nrpages;
640 }
641}
Ingo Molnar1b42f512008-01-30 13:33:45 +0100642
643void __this_fixmap_does_not_exist(void)
644{
645 WARN_ON(1);
646}
Thomas Gleixner240d3a72008-01-30 13:34:05 +0100647
648#endif /* CONFIG_X86_32 */