blob: 1a88d1572a7781376176cfee36af0b382e44be9d [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +010023enum ioremap_mode {
24 IOR_MODE_UNCACHED,
25 IOR_MODE_CACHED,
26};
27
Thomas Gleixner240d3a72008-01-30 13:34:05 +010028#ifdef CONFIG_X86_64
29
30unsigned long __phys_addr(unsigned long x)
31{
32 if (x >= __START_KERNEL_map)
33 return x - __START_KERNEL_map + phys_base;
34 return x - PAGE_OFFSET;
35}
36EXPORT_SYMBOL(__phys_addr);
37
38#endif
39
Thomas Gleixner5f5192b2008-01-30 13:34:06 +010040int page_is_ram(unsigned long pagenr)
41{
42 unsigned long addr, end;
43 int i;
44
45 for (i = 0; i < e820.nr_map; i++) {
46 /*
47 * Not usable memory:
48 */
49 if (e820.map[i].type != E820_RAM)
50 continue;
Thomas Gleixner5f5192b2008-01-30 13:34:06 +010051 addr = (e820.map[i].addr + PAGE_SIZE-1) >> PAGE_SHIFT;
52 end = (e820.map[i].addr + e820.map[i].size) >> PAGE_SHIFT;
Thomas Gleixner950f9d92008-01-30 13:34:06 +010053
54 /*
55 * Sanity check: Some BIOSen report areas as RAM that
56 * are not. Notably the 640->1Mb area, which is the
57 * PCI BIOS area.
58 */
59 if (addr >= (BIOS_BEGIN >> PAGE_SHIFT) &&
60 end < (BIOS_END >> PAGE_SHIFT))
61 continue;
62
Thomas Gleixner5f5192b2008-01-30 13:34:06 +010063 if ((pagenr >= addr) && (pagenr < end))
64 return 1;
65 }
66 return 0;
67}
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/*
Thomas Gleixnere9332ca2008-01-30 13:34:05 +010070 * Fix up the linear direct mapping of the kernel to avoid cache attribute
71 * conflicts.
72 */
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +010073static int ioremap_change_attr(unsigned long paddr, unsigned long size,
74 enum ioremap_mode mode)
Thomas Gleixnere9332ca2008-01-30 13:34:05 +010075{
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +010076 unsigned long vaddr = (unsigned long)__va(paddr);
77 unsigned long nrpages = size >> PAGE_SHIFT;
Harvey Harrison93809be2008-02-01 17:49:43 +010078 unsigned int level;
79 int err;
Thomas Gleixnere9332ca2008-01-30 13:34:05 +010080
81 /* No change for pages after the last mapping */
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +010082 if ((paddr + size - 1) >= (max_pfn_mapped << PAGE_SHIFT))
Thomas Gleixnere9332ca2008-01-30 13:34:05 +010083 return 0;
84
Thomas Gleixnere9332ca2008-01-30 13:34:05 +010085 /*
86 * If there is no identity map for this address,
87 * change_page_attr_addr is unnecessary
88 */
89 if (!lookup_address(vaddr, &level))
90 return 0;
91
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +010092 switch (mode) {
93 case IOR_MODE_UNCACHED:
94 default:
95 err = set_memory_uc(vaddr, nrpages);
96 break;
97 case IOR_MODE_CACHED:
98 err = set_memory_wb(vaddr, nrpages);
99 break;
100 }
Thomas Gleixnere9332ca2008-01-30 13:34:05 +0100101
102 return err;
103}
104
105/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 * Remap an arbitrary physical address space into the kernel virtual
107 * address space. Needed when the kernel wants to access high addresses
108 * directly.
109 *
110 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
111 * have to convert them into an offset in a page-aligned mapping, but the
112 * caller shouldn't need to know that small detail.
113 */
Thomas Gleixner5f868152008-01-30 13:34:06 +0100114static void __iomem *__ioremap(unsigned long phys_addr, unsigned long size,
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +0100115 enum ioremap_mode mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100117 void __iomem *addr;
118 struct vm_struct *area;
Ingo Molnar38cb47b2008-02-04 16:47:54 +0100119 unsigned long pfn, offset, last_addr;
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +0100120 pgprot_t prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 /* Don't allow wraparound or zero size */
123 last_addr = phys_addr + size - 1;
124 if (!size || last_addr < phys_addr)
125 return NULL;
126
127 /*
128 * Don't remap the low PCI/ISA area, it's always mapped..
129 */
130 if (phys_addr >= ISA_START_ADDRESS && last_addr < ISA_END_ADDRESS)
Thomas Gleixner4b40fce2008-01-30 13:34:05 +0100131 return (__force void __iomem *)phys_to_virt(phys_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 /*
134 * Don't allow anybody to remap normal RAM that we're using..
135 */
Ingo Molnar38cb47b2008-02-04 16:47:54 +0100136 for (pfn = phys_addr >> PAGE_SHIFT; pfn < max_pfn_mapped &&
137 (pfn << PAGE_SHIFT) < last_addr; pfn++) {
138 if (page_is_ram(pfn) && pfn_valid(pfn) &&
139 !PageReserved(pfn_to_page(pfn)))
Thomas Gleixner266b9f82008-01-30 13:34:06 +0100140 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
142
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +0100143 switch (mode) {
144 case IOR_MODE_UNCACHED:
145 default:
146 prot = PAGE_KERNEL_NOCACHE;
147 break;
148 case IOR_MODE_CACHED:
149 prot = PAGE_KERNEL;
150 break;
151 }
Haavard Skinnemoena148ecf2006-09-30 23:29:17 -0700152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 /*
154 * Mappings have to be page-aligned
155 */
156 offset = phys_addr & ~PAGE_MASK;
157 phys_addr &= PAGE_MASK;
158 size = PAGE_ALIGN(last_addr+1) - phys_addr;
159
160 /*
161 * Ok, go for it..
162 */
Thomas Gleixner74ff2852008-01-30 13:34:05 +0100163 area = get_vm_area(size, VM_IOREMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (!area)
165 return NULL;
166 area->phys_addr = phys_addr;
167 addr = (void __iomem *) area->addr;
Thomas Gleixnere9332ca2008-01-30 13:34:05 +0100168 if (ioremap_page_range((unsigned long)addr, (unsigned long)addr + size,
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +0100169 phys_addr, prot)) {
Thomas Gleixnere4c1b972008-01-30 13:34:05 +0100170 remove_vm_area((void *)(PAGE_MASK & (unsigned long) addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return NULL;
172 }
Thomas Gleixnere9332ca2008-01-30 13:34:05 +0100173
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +0100174 if (ioremap_change_attr(phys_addr, size, mode) < 0) {
Thomas Gleixnere9332ca2008-01-30 13:34:05 +0100175 vunmap(addr);
176 return NULL;
177 }
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return (void __iomem *) (offset + (char __iomem *)addr);
180}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182/**
183 * ioremap_nocache - map bus memory into CPU space
184 * @offset: bus address of the memory
185 * @size: size of the resource to map
186 *
187 * ioremap_nocache performs a platform specific sequence of operations to
188 * make bus memory CPU accessible via the readb/readw/readl/writeb/
189 * writew/writel functions and the other mmio helpers. The returned
190 * address is not guaranteed to be usable directly as a virtual
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100191 * address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 *
193 * This version of ioremap ensures that the memory is marked uncachable
194 * on the CPU as well as honouring existing caching rules from things like
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100195 * the PCI bus. Note that there are other caches and buffers on many
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * busses. In particular driver authors should read up on PCI writes
197 *
198 * It's useful if some control registers are in such an area and
199 * write combining or read caching is not desirable:
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100200 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 * Must be freed with iounmap.
202 */
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100203void __iomem *ioremap_nocache(unsigned long phys_addr, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +0100205 return __ioremap(phys_addr, size, IOR_MODE_UNCACHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700207EXPORT_SYMBOL(ioremap_nocache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Thomas Gleixner5f868152008-01-30 13:34:06 +0100209void __iomem *ioremap_cache(unsigned long phys_addr, unsigned long size)
210{
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +0100211 return __ioremap(phys_addr, size, IOR_MODE_CACHED);
Thomas Gleixner5f868152008-01-30 13:34:06 +0100212}
213EXPORT_SYMBOL(ioremap_cache);
214
Andi Kleenbf5421c2005-12-12 22:17:09 -0800215/**
216 * iounmap - Free a IO remapping
217 * @addr: virtual address from ioremap_*
218 *
219 * Caller must ensure there is only one unmapping for the same pointer.
220 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221void iounmap(volatile void __iomem *addr)
222{
Andi Kleenbf5421c2005-12-12 22:17:09 -0800223 struct vm_struct *p, *o;
Andrew Mortonc23a4e92005-07-07 17:56:02 -0700224
225 if ((void __force *)addr <= high_memory)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return;
227
228 /*
229 * __ioremap special-cases the PCI/ISA range by not instantiating a
230 * vm_area and by simply returning an address into the kernel mapping
231 * of ISA space. So handle that here.
232 */
233 if (addr >= phys_to_virt(ISA_START_ADDRESS) &&
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100234 addr < phys_to_virt(ISA_END_ADDRESS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return;
236
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100237 addr = (volatile void __iomem *)
238 (PAGE_MASK & (unsigned long __force)addr);
Andi Kleenbf5421c2005-12-12 22:17:09 -0800239
240 /* Use the vm area unlocked, assuming the caller
241 ensures there isn't another iounmap for the same address
242 in parallel. Reuse of the virtual address is prevented by
243 leaving it in the global lists until we're done with it.
244 cpa takes care of the direct mappings. */
245 read_lock(&vmlist_lock);
246 for (p = vmlist; p; p = p->next) {
247 if (p->addr == addr)
248 break;
249 }
250 read_unlock(&vmlist_lock);
251
252 if (!p) {
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100253 printk(KERN_ERR "iounmap: bad address %p\n", addr);
Andrew Mortonc23a4e92005-07-07 17:56:02 -0700254 dump_stack();
Andi Kleenbf5421c2005-12-12 22:17:09 -0800255 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257
Andi Kleenbf5421c2005-12-12 22:17:09 -0800258 /* Reset the direct mapping. Can block */
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +0100259 ioremap_change_attr(p->phys_addr, p->size, IOR_MODE_CACHED);
Andi Kleenbf5421c2005-12-12 22:17:09 -0800260
261 /* Finally remove it */
262 o = remove_vm_area((void *)addr);
263 BUG_ON(p != o || o == NULL);
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100264 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700266EXPORT_SYMBOL(iounmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Thomas Gleixner240d3a72008-01-30 13:34:05 +0100268#ifdef CONFIG_X86_32
Ingo Molnard18d6d62008-01-30 13:33:45 +0100269
270int __initdata early_ioremap_debug;
271
272static int __init early_ioremap_debug_setup(char *str)
273{
274 early_ioremap_debug = 1;
275
Huang, Ying793b24a2008-01-30 13:33:45 +0100276 return 0;
Ingo Molnard18d6d62008-01-30 13:33:45 +0100277}
Huang, Ying793b24a2008-01-30 13:33:45 +0100278early_param("early_ioremap_debug", early_ioremap_debug_setup);
Ingo Molnard18d6d62008-01-30 13:33:45 +0100279
Huang, Ying0947b2f2008-01-30 13:33:44 +0100280static __initdata int after_paging_init;
281static __initdata unsigned long bm_pte[1024]
282 __attribute__((aligned(PAGE_SIZE)));
283
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100284static inline unsigned long * __init early_ioremap_pgd(unsigned long addr)
Huang, Ying0947b2f2008-01-30 13:33:44 +0100285{
286 return (unsigned long *)swapper_pg_dir + ((addr >> 22) & 1023);
287}
288
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100289static inline unsigned long * __init early_ioremap_pte(unsigned long addr)
Huang, Ying0947b2f2008-01-30 13:33:44 +0100290{
291 return bm_pte + ((addr >> PAGE_SHIFT) & 1023);
292}
293
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100294void __init early_ioremap_init(void)
Huang, Ying0947b2f2008-01-30 13:33:44 +0100295{
296 unsigned long *pgd;
297
Ingo Molnard18d6d62008-01-30 13:33:45 +0100298 if (early_ioremap_debug)
Ingo Molnaradafdf62008-01-30 13:34:08 +0100299 printk(KERN_INFO "early_ioremap_init()\n");
Ingo Molnard18d6d62008-01-30 13:33:45 +0100300
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100301 pgd = early_ioremap_pgd(fix_to_virt(FIX_BTMAP_BEGIN));
Huang, Ying0947b2f2008-01-30 13:33:44 +0100302 *pgd = __pa(bm_pte) | _PAGE_TABLE;
303 memset(bm_pte, 0, sizeof(bm_pte));
Ingo Molnar0e3a9542008-01-30 13:33:49 +0100304 /*
305 * The boot-ioremap range spans multiple pgds, for which
306 * we are not prepared:
307 */
308 if (pgd != early_ioremap_pgd(fix_to_virt(FIX_BTMAP_END))) {
309 WARN_ON(1);
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100310 printk(KERN_WARNING "pgd %p != %p\n",
311 pgd, early_ioremap_pgd(fix_to_virt(FIX_BTMAP_END)));
312 printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
313 fix_to_virt(FIX_BTMAP_BEGIN));
314 printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_END): %08lx\n",
315 fix_to_virt(FIX_BTMAP_END));
Ingo Molnar0e3a9542008-01-30 13:33:49 +0100316
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100317 printk(KERN_WARNING "FIX_BTMAP_END: %d\n", FIX_BTMAP_END);
318 printk(KERN_WARNING "FIX_BTMAP_BEGIN: %d\n",
319 FIX_BTMAP_BEGIN);
Ingo Molnar0e3a9542008-01-30 13:33:49 +0100320 }
Huang, Ying0947b2f2008-01-30 13:33:44 +0100321}
322
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100323void __init early_ioremap_clear(void)
Huang, Ying0947b2f2008-01-30 13:33:44 +0100324{
325 unsigned long *pgd;
326
Ingo Molnard18d6d62008-01-30 13:33:45 +0100327 if (early_ioremap_debug)
Ingo Molnaradafdf62008-01-30 13:34:08 +0100328 printk(KERN_INFO "early_ioremap_clear()\n");
Ingo Molnard18d6d62008-01-30 13:33:45 +0100329
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100330 pgd = early_ioremap_pgd(fix_to_virt(FIX_BTMAP_BEGIN));
Huang, Ying0947b2f2008-01-30 13:33:44 +0100331 *pgd = 0;
Jeremy Fitzhardingef6df72e2008-01-30 13:34:11 +0100332 paravirt_release_pt(__pa(pgd) >> PAGE_SHIFT);
Huang, Ying0947b2f2008-01-30 13:33:44 +0100333 __flush_tlb_all();
334}
335
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100336void __init early_ioremap_reset(void)
Huang, Ying0947b2f2008-01-30 13:33:44 +0100337{
338 enum fixed_addresses idx;
339 unsigned long *pte, phys, addr;
340
341 after_paging_init = 1;
Huang, Ying64a8f852008-01-30 13:33:44 +0100342 for (idx = FIX_BTMAP_BEGIN; idx >= FIX_BTMAP_END; idx--) {
Huang, Ying0947b2f2008-01-30 13:33:44 +0100343 addr = fix_to_virt(idx);
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100344 pte = early_ioremap_pte(addr);
Huang, Ying1fd6a532008-01-31 22:05:45 +0100345 if (*pte & _PAGE_PRESENT) {
Huang, Ying0947b2f2008-01-30 13:33:44 +0100346 phys = *pte & PAGE_MASK;
347 set_fixmap(idx, phys);
348 }
349 }
350}
351
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100352static void __init __early_set_fixmap(enum fixed_addresses idx,
Huang, Ying0947b2f2008-01-30 13:33:44 +0100353 unsigned long phys, pgprot_t flags)
354{
355 unsigned long *pte, addr = __fix_to_virt(idx);
356
357 if (idx >= __end_of_fixed_addresses) {
358 BUG();
359 return;
360 }
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100361 pte = early_ioremap_pte(addr);
Huang, Ying0947b2f2008-01-30 13:33:44 +0100362 if (pgprot_val(flags))
363 *pte = (phys & PAGE_MASK) | pgprot_val(flags);
364 else
365 *pte = 0;
366 __flush_tlb_one(addr);
367}
368
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100369static inline void __init early_set_fixmap(enum fixed_addresses idx,
Huang, Ying0947b2f2008-01-30 13:33:44 +0100370 unsigned long phys)
371{
372 if (after_paging_init)
373 set_fixmap(idx, phys);
374 else
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100375 __early_set_fixmap(idx, phys, PAGE_KERNEL);
Huang, Ying0947b2f2008-01-30 13:33:44 +0100376}
377
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100378static inline void __init early_clear_fixmap(enum fixed_addresses idx)
Huang, Ying0947b2f2008-01-30 13:33:44 +0100379{
380 if (after_paging_init)
381 clear_fixmap(idx);
382 else
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100383 __early_set_fixmap(idx, 0, __pgprot(0));
Huang, Ying0947b2f2008-01-30 13:33:44 +0100384}
385
Ingo Molnar1b42f512008-01-30 13:33:45 +0100386
387int __initdata early_ioremap_nested;
388
Ingo Molnard690b2a2008-01-30 13:33:47 +0100389static int __init check_early_ioremap_leak(void)
390{
391 if (!early_ioremap_nested)
392 return 0;
393
394 printk(KERN_WARNING
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100395 "Debug warning: early ioremap leak of %d areas detected.\n",
396 early_ioremap_nested);
Ingo Molnard690b2a2008-01-30 13:33:47 +0100397 printk(KERN_WARNING
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100398 "please boot with early_ioremap_debug and report the dmesg.\n");
Ingo Molnard690b2a2008-01-30 13:33:47 +0100399 WARN_ON(1);
400
401 return 1;
402}
403late_initcall(check_early_ioremap_leak);
404
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100405void __init *early_ioremap(unsigned long phys_addr, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 unsigned long offset, last_addr;
Ingo Molnar1b42f512008-01-30 13:33:45 +0100408 unsigned int nrpages, nesting;
409 enum fixed_addresses idx0, idx;
410
411 WARN_ON(system_state != SYSTEM_BOOTING);
412
413 nesting = early_ioremap_nested;
Ingo Molnard18d6d62008-01-30 13:33:45 +0100414 if (early_ioremap_debug) {
Ingo Molnaradafdf62008-01-30 13:34:08 +0100415 printk(KERN_INFO "early_ioremap(%08lx, %08lx) [%d] => ",
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100416 phys_addr, size, nesting);
Ingo Molnard18d6d62008-01-30 13:33:45 +0100417 dump_stack();
418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 /* Don't allow wraparound or zero size */
421 last_addr = phys_addr + size - 1;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100422 if (!size || last_addr < phys_addr) {
423 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return NULL;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100427 if (nesting >= FIX_BTMAPS_NESTING) {
428 WARN_ON(1);
Ingo Molnar1b42f512008-01-30 13:33:45 +0100429 return NULL;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100430 }
Ingo Molnar1b42f512008-01-30 13:33:45 +0100431 early_ioremap_nested++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 /*
433 * Mappings have to be page-aligned
434 */
435 offset = phys_addr & ~PAGE_MASK;
436 phys_addr &= PAGE_MASK;
437 size = PAGE_ALIGN(last_addr) - phys_addr;
438
439 /*
440 * Mappings have to fit in the FIX_BTMAP area.
441 */
442 nrpages = size >> PAGE_SHIFT;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100443 if (nrpages > NR_FIX_BTMAPS) {
444 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return NULL;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 /*
449 * Ok, go for it..
450 */
Ingo Molnar1b42f512008-01-30 13:33:45 +0100451 idx0 = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*nesting;
452 idx = idx0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 while (nrpages > 0) {
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100454 early_set_fixmap(idx, phys_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 phys_addr += PAGE_SIZE;
456 --idx;
457 --nrpages;
458 }
Ingo Molnard18d6d62008-01-30 13:33:45 +0100459 if (early_ioremap_debug)
460 printk(KERN_CONT "%08lx + %08lx\n", offset, fix_to_virt(idx0));
Ingo Molnar1b42f512008-01-30 13:33:45 +0100461
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100462 return (void *) (offset + fix_to_virt(idx0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100465void __init early_iounmap(void *addr, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 unsigned long virt_addr;
468 unsigned long offset;
469 unsigned int nrpages;
470 enum fixed_addresses idx;
Ingo Molnar1b42f512008-01-30 13:33:45 +0100471 unsigned int nesting;
472
473 nesting = --early_ioremap_nested;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100474 WARN_ON(nesting < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Ingo Molnard18d6d62008-01-30 13:33:45 +0100476 if (early_ioremap_debug) {
Ingo Molnaradafdf62008-01-30 13:34:08 +0100477 printk(KERN_INFO "early_iounmap(%p, %08lx) [%d]\n", addr,
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100478 size, nesting);
Ingo Molnard18d6d62008-01-30 13:33:45 +0100479 dump_stack();
480 }
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 virt_addr = (unsigned long)addr;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100483 if (virt_addr < fix_to_virt(FIX_BTMAP_BEGIN)) {
484 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 offset = virt_addr & ~PAGE_MASK;
488 nrpages = PAGE_ALIGN(offset + size - 1) >> PAGE_SHIFT;
489
Ingo Molnar1b42f512008-01-30 13:33:45 +0100490 idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*nesting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 while (nrpages > 0) {
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100492 early_clear_fixmap(idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 --idx;
494 --nrpages;
495 }
496}
Ingo Molnar1b42f512008-01-30 13:33:45 +0100497
498void __this_fixmap_does_not_exist(void)
499{
500 WARN_ON(1);
501}
Thomas Gleixner240d3a72008-01-30 13:34:05 +0100502
503#endif /* CONFIG_X86_32 */