blob: ac9ab20d8092dbd643120cd0a3fed56673786b31 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +010022enum ioremap_mode {
23 IOR_MODE_UNCACHED,
24 IOR_MODE_CACHED,
25};
26
Thomas Gleixner240d3a72008-01-30 13:34:05 +010027#ifdef CONFIG_X86_64
28
29unsigned long __phys_addr(unsigned long x)
30{
31 if (x >= __START_KERNEL_map)
32 return x - __START_KERNEL_map + phys_base;
33 return x - PAGE_OFFSET;
34}
35EXPORT_SYMBOL(__phys_addr);
36
37#endif
38
Thomas Gleixner5f5192b2008-01-30 13:34:06 +010039int page_is_ram(unsigned long pagenr)
40{
41 unsigned long addr, end;
42 int i;
43
44 for (i = 0; i < e820.nr_map; i++) {
45 /*
46 * Not usable memory:
47 */
48 if (e820.map[i].type != E820_RAM)
49 continue;
Thomas Gleixner5f5192b2008-01-30 13:34:06 +010050 addr = (e820.map[i].addr + PAGE_SIZE-1) >> PAGE_SHIFT;
51 end = (e820.map[i].addr + e820.map[i].size) >> PAGE_SHIFT;
Thomas Gleixner950f9d92008-01-30 13:34:06 +010052
53 /*
54 * Sanity check: Some BIOSen report areas as RAM that
55 * are not. Notably the 640->1Mb area, which is the
56 * PCI BIOS area.
57 */
58 if (addr >= (BIOS_BEGIN >> PAGE_SHIFT) &&
59 end < (BIOS_END >> PAGE_SHIFT))
60 continue;
61
Thomas Gleixner5f5192b2008-01-30 13:34:06 +010062 if ((pagenr >= addr) && (pagenr < end))
63 return 1;
64 }
65 return 0;
66}
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/*
Thomas Gleixnere9332ca2008-01-30 13:34:05 +010069 * Fix up the linear direct mapping of the kernel to avoid cache attribute
70 * conflicts.
71 */
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +010072static int ioremap_change_attr(unsigned long paddr, unsigned long size,
73 enum ioremap_mode mode)
Thomas Gleixnere9332ca2008-01-30 13:34:05 +010074{
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +010075 unsigned long vaddr = (unsigned long)__va(paddr);
76 unsigned long nrpages = size >> PAGE_SHIFT;
Thomas Gleixnere9332ca2008-01-30 13:34:05 +010077 int err, level;
78
79 /* No change for pages after the last mapping */
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +010080 if ((paddr + size - 1) >= (max_pfn_mapped << PAGE_SHIFT))
Thomas Gleixnere9332ca2008-01-30 13:34:05 +010081 return 0;
82
Thomas Gleixnere9332ca2008-01-30 13:34:05 +010083 /*
84 * If there is no identity map for this address,
85 * change_page_attr_addr is unnecessary
86 */
87 if (!lookup_address(vaddr, &level))
88 return 0;
89
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +010090 switch (mode) {
91 case IOR_MODE_UNCACHED:
92 default:
93 err = set_memory_uc(vaddr, nrpages);
94 break;
95 case IOR_MODE_CACHED:
96 err = set_memory_wb(vaddr, nrpages);
97 break;
98 }
Thomas Gleixnere9332ca2008-01-30 13:34:05 +010099
100 return err;
101}
102
103/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * Remap an arbitrary physical address space into the kernel virtual
105 * address space. Needed when the kernel wants to access high addresses
106 * directly.
107 *
108 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
109 * have to convert them into an offset in a page-aligned mapping, but the
110 * caller shouldn't need to know that small detail.
111 */
Thomas Gleixner5f868152008-01-30 13:34:06 +0100112static void __iomem *__ioremap(unsigned long phys_addr, unsigned long size,
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +0100113 enum ioremap_mode mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100115 void __iomem *addr;
116 struct vm_struct *area;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 unsigned long offset, last_addr;
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +0100118 pgprot_t prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 /* Don't allow wraparound or zero size */
121 last_addr = phys_addr + size - 1;
122 if (!size || last_addr < phys_addr)
123 return NULL;
124
125 /*
126 * Don't remap the low PCI/ISA area, it's always mapped..
127 */
128 if (phys_addr >= ISA_START_ADDRESS && last_addr < ISA_END_ADDRESS)
Thomas Gleixner4b40fce2008-01-30 13:34:05 +0100129 return (__force void __iomem *)phys_to_virt(phys_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 /*
132 * Don't allow anybody to remap normal RAM that we're using..
133 */
Thomas Gleixner266b9f82008-01-30 13:34:06 +0100134 for (offset = phys_addr >> PAGE_SHIFT; offset < max_pfn_mapped &&
135 (offset << PAGE_SHIFT) < last_addr; offset++) {
136 if (page_is_ram(offset))
137 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +0100140 switch (mode) {
141 case IOR_MODE_UNCACHED:
142 default:
143 prot = PAGE_KERNEL_NOCACHE;
144 break;
145 case IOR_MODE_CACHED:
146 prot = PAGE_KERNEL;
147 break;
148 }
Haavard Skinnemoena148ecf2006-09-30 23:29:17 -0700149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 /*
151 * Mappings have to be page-aligned
152 */
153 offset = phys_addr & ~PAGE_MASK;
154 phys_addr &= PAGE_MASK;
155 size = PAGE_ALIGN(last_addr+1) - phys_addr;
156
157 /*
158 * Ok, go for it..
159 */
Thomas Gleixner74ff2852008-01-30 13:34:05 +0100160 area = get_vm_area(size, VM_IOREMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (!area)
162 return NULL;
163 area->phys_addr = phys_addr;
164 addr = (void __iomem *) area->addr;
Thomas Gleixnere9332ca2008-01-30 13:34:05 +0100165 if (ioremap_page_range((unsigned long)addr, (unsigned long)addr + size,
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +0100166 phys_addr, prot)) {
Thomas Gleixnere4c1b972008-01-30 13:34:05 +0100167 remove_vm_area((void *)(PAGE_MASK & (unsigned long) addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return NULL;
169 }
Thomas Gleixnere9332ca2008-01-30 13:34:05 +0100170
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +0100171 if (ioremap_change_attr(phys_addr, size, mode) < 0) {
Thomas Gleixnere9332ca2008-01-30 13:34:05 +0100172 vunmap(addr);
173 return NULL;
174 }
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return (void __iomem *) (offset + (char __iomem *)addr);
177}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179/**
180 * ioremap_nocache - map bus memory into CPU space
181 * @offset: bus address of the memory
182 * @size: size of the resource to map
183 *
184 * ioremap_nocache performs a platform specific sequence of operations to
185 * make bus memory CPU accessible via the readb/readw/readl/writeb/
186 * writew/writel functions and the other mmio helpers. The returned
187 * address is not guaranteed to be usable directly as a virtual
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100188 * address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 *
190 * This version of ioremap ensures that the memory is marked uncachable
191 * on the CPU as well as honouring existing caching rules from things like
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100192 * the PCI bus. Note that there are other caches and buffers on many
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 * busses. In particular driver authors should read up on PCI writes
194 *
195 * It's useful if some control registers are in such an area and
196 * write combining or read caching is not desirable:
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100197 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * Must be freed with iounmap.
199 */
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100200void __iomem *ioremap_nocache(unsigned long phys_addr, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +0100202 return __ioremap(phys_addr, size, IOR_MODE_UNCACHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700204EXPORT_SYMBOL(ioremap_nocache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Thomas Gleixner5f868152008-01-30 13:34:06 +0100206void __iomem *ioremap_cache(unsigned long phys_addr, unsigned long size)
207{
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +0100208 return __ioremap(phys_addr, size, IOR_MODE_CACHED);
Thomas Gleixner5f868152008-01-30 13:34:06 +0100209}
210EXPORT_SYMBOL(ioremap_cache);
211
Andi Kleenbf5421c2005-12-12 22:17:09 -0800212/**
213 * iounmap - Free a IO remapping
214 * @addr: virtual address from ioremap_*
215 *
216 * Caller must ensure there is only one unmapping for the same pointer.
217 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218void iounmap(volatile void __iomem *addr)
219{
Andi Kleenbf5421c2005-12-12 22:17:09 -0800220 struct vm_struct *p, *o;
Andrew Mortonc23a4e92005-07-07 17:56:02 -0700221
222 if ((void __force *)addr <= high_memory)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return;
224
225 /*
226 * __ioremap special-cases the PCI/ISA range by not instantiating a
227 * vm_area and by simply returning an address into the kernel mapping
228 * of ISA space. So handle that here.
229 */
230 if (addr >= phys_to_virt(ISA_START_ADDRESS) &&
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100231 addr < phys_to_virt(ISA_END_ADDRESS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return;
233
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100234 addr = (volatile void __iomem *)
235 (PAGE_MASK & (unsigned long __force)addr);
Andi Kleenbf5421c2005-12-12 22:17:09 -0800236
237 /* Use the vm area unlocked, assuming the caller
238 ensures there isn't another iounmap for the same address
239 in parallel. Reuse of the virtual address is prevented by
240 leaving it in the global lists until we're done with it.
241 cpa takes care of the direct mappings. */
242 read_lock(&vmlist_lock);
243 for (p = vmlist; p; p = p->next) {
244 if (p->addr == addr)
245 break;
246 }
247 read_unlock(&vmlist_lock);
248
249 if (!p) {
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100250 printk(KERN_ERR "iounmap: bad address %p\n", addr);
Andrew Mortonc23a4e92005-07-07 17:56:02 -0700251 dump_stack();
Andi Kleenbf5421c2005-12-12 22:17:09 -0800252 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254
Andi Kleenbf5421c2005-12-12 22:17:09 -0800255 /* Reset the direct mapping. Can block */
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +0100256 ioremap_change_attr(p->phys_addr, p->size, IOR_MODE_CACHED);
Andi Kleenbf5421c2005-12-12 22:17:09 -0800257
258 /* Finally remove it */
259 o = remove_vm_area((void *)addr);
260 BUG_ON(p != o || o == NULL);
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100261 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700263EXPORT_SYMBOL(iounmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Thomas Gleixner240d3a72008-01-30 13:34:05 +0100265#ifdef CONFIG_X86_32
Ingo Molnard18d6d62008-01-30 13:33:45 +0100266
267int __initdata early_ioremap_debug;
268
269static int __init early_ioremap_debug_setup(char *str)
270{
271 early_ioremap_debug = 1;
272
Huang, Ying793b24a2008-01-30 13:33:45 +0100273 return 0;
Ingo Molnard18d6d62008-01-30 13:33:45 +0100274}
Huang, Ying793b24a2008-01-30 13:33:45 +0100275early_param("early_ioremap_debug", early_ioremap_debug_setup);
Ingo Molnard18d6d62008-01-30 13:33:45 +0100276
Huang, Ying0947b2f2008-01-30 13:33:44 +0100277static __initdata int after_paging_init;
278static __initdata unsigned long bm_pte[1024]
279 __attribute__((aligned(PAGE_SIZE)));
280
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100281static inline unsigned long * __init early_ioremap_pgd(unsigned long addr)
Huang, Ying0947b2f2008-01-30 13:33:44 +0100282{
283 return (unsigned long *)swapper_pg_dir + ((addr >> 22) & 1023);
284}
285
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100286static inline unsigned long * __init early_ioremap_pte(unsigned long addr)
Huang, Ying0947b2f2008-01-30 13:33:44 +0100287{
288 return bm_pte + ((addr >> PAGE_SHIFT) & 1023);
289}
290
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100291void __init early_ioremap_init(void)
Huang, Ying0947b2f2008-01-30 13:33:44 +0100292{
293 unsigned long *pgd;
294
Ingo Molnard18d6d62008-01-30 13:33:45 +0100295 if (early_ioremap_debug)
Ingo Molnaradafdf62008-01-30 13:34:08 +0100296 printk(KERN_INFO "early_ioremap_init()\n");
Ingo Molnard18d6d62008-01-30 13:33:45 +0100297
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100298 pgd = early_ioremap_pgd(fix_to_virt(FIX_BTMAP_BEGIN));
Huang, Ying0947b2f2008-01-30 13:33:44 +0100299 *pgd = __pa(bm_pte) | _PAGE_TABLE;
300 memset(bm_pte, 0, sizeof(bm_pte));
Ingo Molnar0e3a9542008-01-30 13:33:49 +0100301 /*
302 * The boot-ioremap range spans multiple pgds, for which
303 * we are not prepared:
304 */
305 if (pgd != early_ioremap_pgd(fix_to_virt(FIX_BTMAP_END))) {
306 WARN_ON(1);
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100307 printk(KERN_WARNING "pgd %p != %p\n",
308 pgd, early_ioremap_pgd(fix_to_virt(FIX_BTMAP_END)));
309 printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
310 fix_to_virt(FIX_BTMAP_BEGIN));
311 printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_END): %08lx\n",
312 fix_to_virt(FIX_BTMAP_END));
Ingo Molnar0e3a9542008-01-30 13:33:49 +0100313
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100314 printk(KERN_WARNING "FIX_BTMAP_END: %d\n", FIX_BTMAP_END);
315 printk(KERN_WARNING "FIX_BTMAP_BEGIN: %d\n",
316 FIX_BTMAP_BEGIN);
Ingo Molnar0e3a9542008-01-30 13:33:49 +0100317 }
Huang, Ying0947b2f2008-01-30 13:33:44 +0100318}
319
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100320void __init early_ioremap_clear(void)
Huang, Ying0947b2f2008-01-30 13:33:44 +0100321{
322 unsigned long *pgd;
323
Ingo Molnard18d6d62008-01-30 13:33:45 +0100324 if (early_ioremap_debug)
Ingo Molnaradafdf62008-01-30 13:34:08 +0100325 printk(KERN_INFO "early_ioremap_clear()\n");
Ingo Molnard18d6d62008-01-30 13:33:45 +0100326
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100327 pgd = early_ioremap_pgd(fix_to_virt(FIX_BTMAP_BEGIN));
Huang, Ying0947b2f2008-01-30 13:33:44 +0100328 *pgd = 0;
329 __flush_tlb_all();
330}
331
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100332void __init early_ioremap_reset(void)
Huang, Ying0947b2f2008-01-30 13:33:44 +0100333{
334 enum fixed_addresses idx;
335 unsigned long *pte, phys, addr;
336
337 after_paging_init = 1;
Huang, Ying64a8f852008-01-30 13:33:44 +0100338 for (idx = FIX_BTMAP_BEGIN; idx >= FIX_BTMAP_END; idx--) {
Huang, Ying0947b2f2008-01-30 13:33:44 +0100339 addr = fix_to_virt(idx);
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100340 pte = early_ioremap_pte(addr);
Huang, Ying0947b2f2008-01-30 13:33:44 +0100341 if (!*pte & _PAGE_PRESENT) {
342 phys = *pte & PAGE_MASK;
343 set_fixmap(idx, phys);
344 }
345 }
346}
347
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100348static void __init __early_set_fixmap(enum fixed_addresses idx,
Huang, Ying0947b2f2008-01-30 13:33:44 +0100349 unsigned long phys, pgprot_t flags)
350{
351 unsigned long *pte, addr = __fix_to_virt(idx);
352
353 if (idx >= __end_of_fixed_addresses) {
354 BUG();
355 return;
356 }
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100357 pte = early_ioremap_pte(addr);
Huang, Ying0947b2f2008-01-30 13:33:44 +0100358 if (pgprot_val(flags))
359 *pte = (phys & PAGE_MASK) | pgprot_val(flags);
360 else
361 *pte = 0;
362 __flush_tlb_one(addr);
363}
364
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100365static inline void __init early_set_fixmap(enum fixed_addresses idx,
Huang, Ying0947b2f2008-01-30 13:33:44 +0100366 unsigned long phys)
367{
368 if (after_paging_init)
369 set_fixmap(idx, phys);
370 else
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100371 __early_set_fixmap(idx, phys, PAGE_KERNEL);
Huang, Ying0947b2f2008-01-30 13:33:44 +0100372}
373
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100374static inline void __init early_clear_fixmap(enum fixed_addresses idx)
Huang, Ying0947b2f2008-01-30 13:33:44 +0100375{
376 if (after_paging_init)
377 clear_fixmap(idx);
378 else
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100379 __early_set_fixmap(idx, 0, __pgprot(0));
Huang, Ying0947b2f2008-01-30 13:33:44 +0100380}
381
Ingo Molnar1b42f512008-01-30 13:33:45 +0100382
383int __initdata early_ioremap_nested;
384
Ingo Molnard690b2a2008-01-30 13:33:47 +0100385static int __init check_early_ioremap_leak(void)
386{
387 if (!early_ioremap_nested)
388 return 0;
389
390 printk(KERN_WARNING
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100391 "Debug warning: early ioremap leak of %d areas detected.\n",
392 early_ioremap_nested);
Ingo Molnard690b2a2008-01-30 13:33:47 +0100393 printk(KERN_WARNING
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100394 "please boot with early_ioremap_debug and report the dmesg.\n");
Ingo Molnard690b2a2008-01-30 13:33:47 +0100395 WARN_ON(1);
396
397 return 1;
398}
399late_initcall(check_early_ioremap_leak);
400
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100401void __init *early_ioremap(unsigned long phys_addr, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 unsigned long offset, last_addr;
Ingo Molnar1b42f512008-01-30 13:33:45 +0100404 unsigned int nrpages, nesting;
405 enum fixed_addresses idx0, idx;
406
407 WARN_ON(system_state != SYSTEM_BOOTING);
408
409 nesting = early_ioremap_nested;
Ingo Molnard18d6d62008-01-30 13:33:45 +0100410 if (early_ioremap_debug) {
Ingo Molnaradafdf62008-01-30 13:34:08 +0100411 printk(KERN_INFO "early_ioremap(%08lx, %08lx) [%d] => ",
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100412 phys_addr, size, nesting);
Ingo Molnard18d6d62008-01-30 13:33:45 +0100413 dump_stack();
414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 /* Don't allow wraparound or zero size */
417 last_addr = phys_addr + size - 1;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100418 if (!size || last_addr < phys_addr) {
419 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return NULL;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100423 if (nesting >= FIX_BTMAPS_NESTING) {
424 WARN_ON(1);
Ingo Molnar1b42f512008-01-30 13:33:45 +0100425 return NULL;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100426 }
Ingo Molnar1b42f512008-01-30 13:33:45 +0100427 early_ioremap_nested++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /*
429 * Mappings have to be page-aligned
430 */
431 offset = phys_addr & ~PAGE_MASK;
432 phys_addr &= PAGE_MASK;
433 size = PAGE_ALIGN(last_addr) - phys_addr;
434
435 /*
436 * Mappings have to fit in the FIX_BTMAP area.
437 */
438 nrpages = size >> PAGE_SHIFT;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100439 if (nrpages > NR_FIX_BTMAPS) {
440 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return NULL;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 /*
445 * Ok, go for it..
446 */
Ingo Molnar1b42f512008-01-30 13:33:45 +0100447 idx0 = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*nesting;
448 idx = idx0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 while (nrpages > 0) {
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100450 early_set_fixmap(idx, phys_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 phys_addr += PAGE_SIZE;
452 --idx;
453 --nrpages;
454 }
Ingo Molnard18d6d62008-01-30 13:33:45 +0100455 if (early_ioremap_debug)
456 printk(KERN_CONT "%08lx + %08lx\n", offset, fix_to_virt(idx0));
Ingo Molnar1b42f512008-01-30 13:33:45 +0100457
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100458 return (void *) (offset + fix_to_virt(idx0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100461void __init early_iounmap(void *addr, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 unsigned long virt_addr;
464 unsigned long offset;
465 unsigned int nrpages;
466 enum fixed_addresses idx;
Ingo Molnar1b42f512008-01-30 13:33:45 +0100467 unsigned int nesting;
468
469 nesting = --early_ioremap_nested;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100470 WARN_ON(nesting < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Ingo Molnard18d6d62008-01-30 13:33:45 +0100472 if (early_ioremap_debug) {
Ingo Molnaradafdf62008-01-30 13:34:08 +0100473 printk(KERN_INFO "early_iounmap(%p, %08lx) [%d]\n", addr,
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100474 size, nesting);
Ingo Molnard18d6d62008-01-30 13:33:45 +0100475 dump_stack();
476 }
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 virt_addr = (unsigned long)addr;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100479 if (virt_addr < fix_to_virt(FIX_BTMAP_BEGIN)) {
480 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 offset = virt_addr & ~PAGE_MASK;
484 nrpages = PAGE_ALIGN(offset + size - 1) >> PAGE_SHIFT;
485
Ingo Molnar1b42f512008-01-30 13:33:45 +0100486 idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*nesting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 while (nrpages > 0) {
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100488 early_clear_fixmap(idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 --idx;
490 --nrpages;
491 }
492}
Ingo Molnar1b42f512008-01-30 13:33:45 +0100493
494void __this_fixmap_does_not_exist(void)
495{
496 WARN_ON(1);
497}
Thomas Gleixner240d3a72008-01-30 13:34:05 +0100498
499#endif /* CONFIG_X86_32 */