xref: /linux/arch/x86/mm/ioremap.c (revision 5f8681529cb243b3a492e55f2da9d632ad0d5e32)
1e64c8aa0SThomas Gleixner /*
2e64c8aa0SThomas Gleixner  * Re-map IO memory to kernel address space so that we can access it.
3e64c8aa0SThomas Gleixner  * This is needed for high PCI addresses that aren't mapped in the
4e64c8aa0SThomas Gleixner  * 640k-1MB IO memory area on PC's
5e64c8aa0SThomas Gleixner  *
6e64c8aa0SThomas Gleixner  * (C) Copyright 1995 1996 Linus Torvalds
7e64c8aa0SThomas Gleixner  */
8e64c8aa0SThomas Gleixner 
9e64c8aa0SThomas Gleixner #include <linux/bootmem.h>
10e64c8aa0SThomas Gleixner #include <linux/init.h>
11e64c8aa0SThomas Gleixner #include <linux/io.h>
12e64c8aa0SThomas Gleixner #include <linux/module.h>
13e64c8aa0SThomas Gleixner #include <linux/slab.h>
14e64c8aa0SThomas Gleixner #include <linux/vmalloc.h>
15e64c8aa0SThomas Gleixner 
16e64c8aa0SThomas Gleixner #include <asm/cacheflush.h>
17e64c8aa0SThomas Gleixner #include <asm/e820.h>
18e64c8aa0SThomas Gleixner #include <asm/fixmap.h>
19e64c8aa0SThomas Gleixner #include <asm/pgtable.h>
20e64c8aa0SThomas Gleixner #include <asm/tlbflush.h>
21e64c8aa0SThomas Gleixner 
22e64c8aa0SThomas Gleixner #ifdef CONFIG_X86_64
23e64c8aa0SThomas Gleixner 
24e64c8aa0SThomas Gleixner unsigned long __phys_addr(unsigned long x)
25e64c8aa0SThomas Gleixner {
26e64c8aa0SThomas Gleixner 	if (x >= __START_KERNEL_map)
27e64c8aa0SThomas Gleixner 		return x - __START_KERNEL_map + phys_base;
28e64c8aa0SThomas Gleixner 	return x - PAGE_OFFSET;
29e64c8aa0SThomas Gleixner }
30e64c8aa0SThomas Gleixner EXPORT_SYMBOL(__phys_addr);
31e64c8aa0SThomas Gleixner 
32e64c8aa0SThomas Gleixner #endif
33e64c8aa0SThomas Gleixner 
345f5192b9SThomas Gleixner int page_is_ram(unsigned long pagenr)
355f5192b9SThomas Gleixner {
365f5192b9SThomas Gleixner 	unsigned long addr, end;
375f5192b9SThomas Gleixner 	int i;
385f5192b9SThomas Gleixner 
395f5192b9SThomas Gleixner 	for (i = 0; i < e820.nr_map; i++) {
405f5192b9SThomas Gleixner 		/*
415f5192b9SThomas Gleixner 		 * Not usable memory:
425f5192b9SThomas Gleixner 		 */
435f5192b9SThomas Gleixner 		if (e820.map[i].type != E820_RAM)
445f5192b9SThomas Gleixner 			continue;
455f5192b9SThomas Gleixner 		addr = (e820.map[i].addr + PAGE_SIZE-1) >> PAGE_SHIFT;
465f5192b9SThomas Gleixner 		end = (e820.map[i].addr + e820.map[i].size) >> PAGE_SHIFT;
47950f9d95SThomas Gleixner 
48950f9d95SThomas Gleixner 		/*
49950f9d95SThomas Gleixner 		 * Sanity check: Some BIOSen report areas as RAM that
50950f9d95SThomas Gleixner 		 * are not. Notably the 640->1Mb area, which is the
51950f9d95SThomas Gleixner 		 * PCI BIOS area.
52950f9d95SThomas Gleixner 		 */
53950f9d95SThomas Gleixner 		if (addr >= (BIOS_BEGIN >> PAGE_SHIFT) &&
54950f9d95SThomas Gleixner 		    end < (BIOS_END >> PAGE_SHIFT))
55950f9d95SThomas Gleixner 			continue;
56950f9d95SThomas Gleixner 
575f5192b9SThomas Gleixner 		if ((pagenr >= addr) && (pagenr < end))
585f5192b9SThomas Gleixner 			return 1;
595f5192b9SThomas Gleixner 	}
605f5192b9SThomas Gleixner 	return 0;
615f5192b9SThomas Gleixner }
625f5192b9SThomas Gleixner 
63e64c8aa0SThomas Gleixner /*
64e64c8aa0SThomas Gleixner  * Fix up the linear direct mapping of the kernel to avoid cache attribute
65e64c8aa0SThomas Gleixner  * conflicts.
66e64c8aa0SThomas Gleixner  */
67e64c8aa0SThomas Gleixner static int ioremap_change_attr(unsigned long phys_addr, unsigned long size,
68e64c8aa0SThomas Gleixner 			       pgprot_t prot)
69e64c8aa0SThomas Gleixner {
70e64c8aa0SThomas Gleixner 	unsigned long npages, vaddr, last_addr = phys_addr + size - 1;
71e64c8aa0SThomas Gleixner 	int err, level;
72e64c8aa0SThomas Gleixner 
73e64c8aa0SThomas Gleixner 	/* No change for pages after the last mapping */
74e64c8aa0SThomas Gleixner 	if (last_addr >= (max_pfn_mapped << PAGE_SHIFT))
75e64c8aa0SThomas Gleixner 		return 0;
76e64c8aa0SThomas Gleixner 
77e64c8aa0SThomas Gleixner 	npages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
78e64c8aa0SThomas Gleixner 	vaddr = (unsigned long) __va(phys_addr);
79e64c8aa0SThomas Gleixner 
80e64c8aa0SThomas Gleixner 	/*
81e64c8aa0SThomas Gleixner 	 * If there is no identity map for this address,
82e64c8aa0SThomas Gleixner 	 * change_page_attr_addr is unnecessary
83e64c8aa0SThomas Gleixner 	 */
84e64c8aa0SThomas Gleixner 	if (!lookup_address(vaddr, &level))
85e64c8aa0SThomas Gleixner 		return 0;
86e64c8aa0SThomas Gleixner 
87e64c8aa0SThomas Gleixner 	/*
88e64c8aa0SThomas Gleixner 	 * Must use an address here and not struct page because the
89e64c8aa0SThomas Gleixner 	 * phys addr can be a in hole between nodes and not have a
90e64c8aa0SThomas Gleixner 	 * memmap entry.
91e64c8aa0SThomas Gleixner 	 */
92e64c8aa0SThomas Gleixner 	err = change_page_attr_addr(vaddr, npages, prot);
93e64c8aa0SThomas Gleixner 
94e64c8aa0SThomas Gleixner 	if (!err)
95e64c8aa0SThomas Gleixner 		global_flush_tlb();
96e64c8aa0SThomas Gleixner 
97e64c8aa0SThomas Gleixner 	return err;
98e64c8aa0SThomas Gleixner }
99e64c8aa0SThomas Gleixner 
100e64c8aa0SThomas Gleixner /*
101e64c8aa0SThomas Gleixner  * Remap an arbitrary physical address space into the kernel virtual
102e64c8aa0SThomas Gleixner  * address space. Needed when the kernel wants to access high addresses
103e64c8aa0SThomas Gleixner  * directly.
104e64c8aa0SThomas Gleixner  *
105e64c8aa0SThomas Gleixner  * NOTE! We need to allow non-page-aligned mappings too: we will obviously
106e64c8aa0SThomas Gleixner  * have to convert them into an offset in a page-aligned mapping, but the
107e64c8aa0SThomas Gleixner  * caller shouldn't need to know that small detail.
108e64c8aa0SThomas Gleixner  */
109*5f868152SThomas Gleixner static void __iomem *__ioremap(unsigned long phys_addr, unsigned long size,
110e64c8aa0SThomas Gleixner 			       unsigned long flags)
111e64c8aa0SThomas Gleixner {
112e64c8aa0SThomas Gleixner 	void __iomem *addr;
113e64c8aa0SThomas Gleixner 	struct vm_struct *area;
114e64c8aa0SThomas Gleixner 	unsigned long offset, last_addr;
115e64c8aa0SThomas Gleixner 	pgprot_t pgprot;
116e64c8aa0SThomas Gleixner 
117e64c8aa0SThomas Gleixner 	/* Don't allow wraparound or zero size */
118e64c8aa0SThomas Gleixner 	last_addr = phys_addr + size - 1;
119e64c8aa0SThomas Gleixner 	if (!size || last_addr < phys_addr)
120e64c8aa0SThomas Gleixner 		return NULL;
121e64c8aa0SThomas Gleixner 
122e64c8aa0SThomas Gleixner 	/*
123e64c8aa0SThomas Gleixner 	 * Don't remap the low PCI/ISA area, it's always mapped..
124e64c8aa0SThomas Gleixner 	 */
125e64c8aa0SThomas Gleixner 	if (phys_addr >= ISA_START_ADDRESS && last_addr < ISA_END_ADDRESS)
126e64c8aa0SThomas Gleixner 		return (__force void __iomem *)phys_to_virt(phys_addr);
127e64c8aa0SThomas Gleixner 
128e64c8aa0SThomas Gleixner 	/*
129e64c8aa0SThomas Gleixner 	 * Don't allow anybody to remap normal RAM that we're using..
130e64c8aa0SThomas Gleixner 	 */
131266b9f87SThomas Gleixner 	for (offset = phys_addr >> PAGE_SHIFT; offset < max_pfn_mapped &&
132266b9f87SThomas Gleixner 	     (offset << PAGE_SHIFT) < last_addr; offset++) {
133266b9f87SThomas Gleixner 		if (page_is_ram(offset))
134e64c8aa0SThomas Gleixner 			return NULL;
135e64c8aa0SThomas Gleixner 	}
136e64c8aa0SThomas Gleixner 
137e64c8aa0SThomas Gleixner 	pgprot = MAKE_GLOBAL(__PAGE_KERNEL | flags);
138e64c8aa0SThomas Gleixner 
139e64c8aa0SThomas Gleixner 	/*
140e64c8aa0SThomas Gleixner 	 * Mappings have to be page-aligned
141e64c8aa0SThomas Gleixner 	 */
142e64c8aa0SThomas Gleixner 	offset = phys_addr & ~PAGE_MASK;
143e64c8aa0SThomas Gleixner 	phys_addr &= PAGE_MASK;
144e64c8aa0SThomas Gleixner 	size = PAGE_ALIGN(last_addr+1) - phys_addr;
145e64c8aa0SThomas Gleixner 
146e64c8aa0SThomas Gleixner 	/*
147e64c8aa0SThomas Gleixner 	 * Ok, go for it..
148e64c8aa0SThomas Gleixner 	 */
149e64c8aa0SThomas Gleixner 	area = get_vm_area(size, VM_IOREMAP);
150e64c8aa0SThomas Gleixner 	if (!area)
151e64c8aa0SThomas Gleixner 		return NULL;
152e64c8aa0SThomas Gleixner 	area->phys_addr = phys_addr;
153e64c8aa0SThomas Gleixner 	addr = (void __iomem *) area->addr;
154e64c8aa0SThomas Gleixner 	if (ioremap_page_range((unsigned long)addr, (unsigned long)addr + size,
155e64c8aa0SThomas Gleixner 			       phys_addr, pgprot)) {
156e64c8aa0SThomas Gleixner 		remove_vm_area((void *)(PAGE_MASK & (unsigned long) addr));
157e64c8aa0SThomas Gleixner 		return NULL;
158e64c8aa0SThomas Gleixner 	}
159e64c8aa0SThomas Gleixner 
160e64c8aa0SThomas Gleixner 	if (ioremap_change_attr(phys_addr, size, pgprot) < 0) {
161e64c8aa0SThomas Gleixner 		vunmap(addr);
162e64c8aa0SThomas Gleixner 		return NULL;
163e64c8aa0SThomas Gleixner 	}
164e64c8aa0SThomas Gleixner 
165e64c8aa0SThomas Gleixner 	return (void __iomem *) (offset + (char __iomem *)addr);
166e64c8aa0SThomas Gleixner }
167e64c8aa0SThomas Gleixner 
168e64c8aa0SThomas Gleixner /**
169e64c8aa0SThomas Gleixner  * ioremap_nocache     -   map bus memory into CPU space
170e64c8aa0SThomas Gleixner  * @offset:    bus address of the memory
171e64c8aa0SThomas Gleixner  * @size:      size of the resource to map
172e64c8aa0SThomas Gleixner  *
173e64c8aa0SThomas Gleixner  * ioremap_nocache performs a platform specific sequence of operations to
174e64c8aa0SThomas Gleixner  * make bus memory CPU accessible via the readb/readw/readl/writeb/
175e64c8aa0SThomas Gleixner  * writew/writel functions and the other mmio helpers. The returned
176e64c8aa0SThomas Gleixner  * address is not guaranteed to be usable directly as a virtual
177e64c8aa0SThomas Gleixner  * address.
178e64c8aa0SThomas Gleixner  *
179e64c8aa0SThomas Gleixner  * This version of ioremap ensures that the memory is marked uncachable
180e64c8aa0SThomas Gleixner  * on the CPU as well as honouring existing caching rules from things like
181e64c8aa0SThomas Gleixner  * the PCI bus. Note that there are other caches and buffers on many
182e64c8aa0SThomas Gleixner  * busses. In particular driver authors should read up on PCI writes
183e64c8aa0SThomas Gleixner  *
184e64c8aa0SThomas Gleixner  * It's useful if some control registers are in such an area and
185e64c8aa0SThomas Gleixner  * write combining or read caching is not desirable:
186e64c8aa0SThomas Gleixner  *
187e64c8aa0SThomas Gleixner  * Must be freed with iounmap.
188e64c8aa0SThomas Gleixner  */
189e64c8aa0SThomas Gleixner void __iomem *ioremap_nocache(unsigned long phys_addr, unsigned long size)
190e64c8aa0SThomas Gleixner {
191e64c8aa0SThomas Gleixner 	return __ioremap(phys_addr, size, _PAGE_PCD | _PAGE_PWT);
192e64c8aa0SThomas Gleixner }
193e64c8aa0SThomas Gleixner EXPORT_SYMBOL(ioremap_nocache);
194e64c8aa0SThomas Gleixner 
195*5f868152SThomas Gleixner void __iomem *ioremap_cache(unsigned long phys_addr, unsigned long size)
196*5f868152SThomas Gleixner {
197*5f868152SThomas Gleixner 	return __ioremap(phys_addr, size, 0);
198*5f868152SThomas Gleixner }
199*5f868152SThomas Gleixner EXPORT_SYMBOL(ioremap_cache);
200*5f868152SThomas Gleixner 
201e64c8aa0SThomas Gleixner /**
202e64c8aa0SThomas Gleixner  * iounmap - Free a IO remapping
203e64c8aa0SThomas Gleixner  * @addr: virtual address from ioremap_*
204e64c8aa0SThomas Gleixner  *
205e64c8aa0SThomas Gleixner  * Caller must ensure there is only one unmapping for the same pointer.
206e64c8aa0SThomas Gleixner  */
207e64c8aa0SThomas Gleixner void iounmap(volatile void __iomem *addr)
208e64c8aa0SThomas Gleixner {
209e64c8aa0SThomas Gleixner 	struct vm_struct *p, *o;
210e64c8aa0SThomas Gleixner 
211e64c8aa0SThomas Gleixner 	if ((void __force *)addr <= high_memory)
212e64c8aa0SThomas Gleixner 		return;
213e64c8aa0SThomas Gleixner 
214e64c8aa0SThomas Gleixner 	/*
215e64c8aa0SThomas Gleixner 	 * __ioremap special-cases the PCI/ISA range by not instantiating a
216e64c8aa0SThomas Gleixner 	 * vm_area and by simply returning an address into the kernel mapping
217e64c8aa0SThomas Gleixner 	 * of ISA space.   So handle that here.
218e64c8aa0SThomas Gleixner 	 */
219e64c8aa0SThomas Gleixner 	if (addr >= phys_to_virt(ISA_START_ADDRESS) &&
220e64c8aa0SThomas Gleixner 	    addr < phys_to_virt(ISA_END_ADDRESS))
221e64c8aa0SThomas Gleixner 		return;
222e64c8aa0SThomas Gleixner 
223e64c8aa0SThomas Gleixner 	addr = (volatile void __iomem *)
224e64c8aa0SThomas Gleixner 		(PAGE_MASK & (unsigned long __force)addr);
225e64c8aa0SThomas Gleixner 
226e64c8aa0SThomas Gleixner 	/* Use the vm area unlocked, assuming the caller
227e64c8aa0SThomas Gleixner 	   ensures there isn't another iounmap for the same address
228e64c8aa0SThomas Gleixner 	   in parallel. Reuse of the virtual address is prevented by
229e64c8aa0SThomas Gleixner 	   leaving it in the global lists until we're done with it.
230e64c8aa0SThomas Gleixner 	   cpa takes care of the direct mappings. */
231e64c8aa0SThomas Gleixner 	read_lock(&vmlist_lock);
232e64c8aa0SThomas Gleixner 	for (p = vmlist; p; p = p->next) {
233e64c8aa0SThomas Gleixner 		if (p->addr == addr)
234e64c8aa0SThomas Gleixner 			break;
235e64c8aa0SThomas Gleixner 	}
236e64c8aa0SThomas Gleixner 	read_unlock(&vmlist_lock);
237e64c8aa0SThomas Gleixner 
238e64c8aa0SThomas Gleixner 	if (!p) {
239e64c8aa0SThomas Gleixner 		printk(KERN_ERR "iounmap: bad address %p\n", addr);
240e64c8aa0SThomas Gleixner 		dump_stack();
241e64c8aa0SThomas Gleixner 		return;
242e64c8aa0SThomas Gleixner 	}
243e64c8aa0SThomas Gleixner 
244e64c8aa0SThomas Gleixner 	/* Reset the direct mapping. Can block */
245e64c8aa0SThomas Gleixner 	ioremap_change_attr(p->phys_addr, p->size, PAGE_KERNEL);
246e64c8aa0SThomas Gleixner 
247e64c8aa0SThomas Gleixner 	/* Finally remove it */
248e64c8aa0SThomas Gleixner 	o = remove_vm_area((void *)addr);
249e64c8aa0SThomas Gleixner 	BUG_ON(p != o || o == NULL);
250e64c8aa0SThomas Gleixner 	kfree(p);
251e64c8aa0SThomas Gleixner }
252e64c8aa0SThomas Gleixner EXPORT_SYMBOL(iounmap);
253e64c8aa0SThomas Gleixner 
254e64c8aa0SThomas Gleixner #ifdef CONFIG_X86_32
255e64c8aa0SThomas Gleixner 
256e64c8aa0SThomas Gleixner int __initdata early_ioremap_debug;
257e64c8aa0SThomas Gleixner 
258e64c8aa0SThomas Gleixner static int __init early_ioremap_debug_setup(char *str)
259e64c8aa0SThomas Gleixner {
260e64c8aa0SThomas Gleixner 	early_ioremap_debug = 1;
261e64c8aa0SThomas Gleixner 
262e64c8aa0SThomas Gleixner 	return 0;
263e64c8aa0SThomas Gleixner }
264e64c8aa0SThomas Gleixner early_param("early_ioremap_debug", early_ioremap_debug_setup);
265e64c8aa0SThomas Gleixner 
266e64c8aa0SThomas Gleixner static __initdata int after_paging_init;
267e64c8aa0SThomas Gleixner static __initdata unsigned long bm_pte[1024]
268e64c8aa0SThomas Gleixner 				__attribute__((aligned(PAGE_SIZE)));
269e64c8aa0SThomas Gleixner 
270e64c8aa0SThomas Gleixner static inline unsigned long * __init early_ioremap_pgd(unsigned long addr)
271e64c8aa0SThomas Gleixner {
272e64c8aa0SThomas Gleixner 	return (unsigned long *)swapper_pg_dir + ((addr >> 22) & 1023);
273e64c8aa0SThomas Gleixner }
274e64c8aa0SThomas Gleixner 
275e64c8aa0SThomas Gleixner static inline unsigned long * __init early_ioremap_pte(unsigned long addr)
276e64c8aa0SThomas Gleixner {
277e64c8aa0SThomas Gleixner 	return bm_pte + ((addr >> PAGE_SHIFT) & 1023);
278e64c8aa0SThomas Gleixner }
279e64c8aa0SThomas Gleixner 
280e64c8aa0SThomas Gleixner void __init early_ioremap_init(void)
281e64c8aa0SThomas Gleixner {
282e64c8aa0SThomas Gleixner 	unsigned long *pgd;
283e64c8aa0SThomas Gleixner 
284e64c8aa0SThomas Gleixner 	if (early_ioremap_debug)
285e64c8aa0SThomas Gleixner 		printk(KERN_DEBUG "early_ioremap_init()\n");
286e64c8aa0SThomas Gleixner 
287e64c8aa0SThomas Gleixner 	pgd = early_ioremap_pgd(fix_to_virt(FIX_BTMAP_BEGIN));
288e64c8aa0SThomas Gleixner 	*pgd = __pa(bm_pte) | _PAGE_TABLE;
289e64c8aa0SThomas Gleixner 	memset(bm_pte, 0, sizeof(bm_pte));
290e64c8aa0SThomas Gleixner 	/*
291e64c8aa0SThomas Gleixner 	 * The boot-ioremap range spans multiple pgds, for which
292e64c8aa0SThomas Gleixner 	 * we are not prepared:
293e64c8aa0SThomas Gleixner 	 */
294e64c8aa0SThomas Gleixner 	if (pgd != early_ioremap_pgd(fix_to_virt(FIX_BTMAP_END))) {
295e64c8aa0SThomas Gleixner 		WARN_ON(1);
296e64c8aa0SThomas Gleixner 		printk(KERN_WARNING "pgd %p != %p\n",
297e64c8aa0SThomas Gleixner 		       pgd, early_ioremap_pgd(fix_to_virt(FIX_BTMAP_END)));
298e64c8aa0SThomas Gleixner 		printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
299e64c8aa0SThomas Gleixner 		       fix_to_virt(FIX_BTMAP_BEGIN));
300e64c8aa0SThomas Gleixner 		printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_END):   %08lx\n",
301e64c8aa0SThomas Gleixner 		       fix_to_virt(FIX_BTMAP_END));
302e64c8aa0SThomas Gleixner 
303e64c8aa0SThomas Gleixner 		printk(KERN_WARNING "FIX_BTMAP_END:       %d\n", FIX_BTMAP_END);
304e64c8aa0SThomas Gleixner 		printk(KERN_WARNING "FIX_BTMAP_BEGIN:     %d\n",
305e64c8aa0SThomas Gleixner 		       FIX_BTMAP_BEGIN);
306e64c8aa0SThomas Gleixner 	}
307e64c8aa0SThomas Gleixner }
308e64c8aa0SThomas Gleixner 
309e64c8aa0SThomas Gleixner void __init early_ioremap_clear(void)
310e64c8aa0SThomas Gleixner {
311e64c8aa0SThomas Gleixner 	unsigned long *pgd;
312e64c8aa0SThomas Gleixner 
313e64c8aa0SThomas Gleixner 	if (early_ioremap_debug)
314e64c8aa0SThomas Gleixner 		printk(KERN_DEBUG "early_ioremap_clear()\n");
315e64c8aa0SThomas Gleixner 
316e64c8aa0SThomas Gleixner 	pgd = early_ioremap_pgd(fix_to_virt(FIX_BTMAP_BEGIN));
317e64c8aa0SThomas Gleixner 	*pgd = 0;
318e64c8aa0SThomas Gleixner 	__flush_tlb_all();
319e64c8aa0SThomas Gleixner }
320e64c8aa0SThomas Gleixner 
321e64c8aa0SThomas Gleixner void __init early_ioremap_reset(void)
322e64c8aa0SThomas Gleixner {
323e64c8aa0SThomas Gleixner 	enum fixed_addresses idx;
324e64c8aa0SThomas Gleixner 	unsigned long *pte, phys, addr;
325e64c8aa0SThomas Gleixner 
326e64c8aa0SThomas Gleixner 	after_paging_init = 1;
327e64c8aa0SThomas Gleixner 	for (idx = FIX_BTMAP_BEGIN; idx >= FIX_BTMAP_END; idx--) {
328e64c8aa0SThomas Gleixner 		addr = fix_to_virt(idx);
329e64c8aa0SThomas Gleixner 		pte = early_ioremap_pte(addr);
330e64c8aa0SThomas Gleixner 		if (!*pte & _PAGE_PRESENT) {
331e64c8aa0SThomas Gleixner 			phys = *pte & PAGE_MASK;
332e64c8aa0SThomas Gleixner 			set_fixmap(idx, phys);
333e64c8aa0SThomas Gleixner 		}
334e64c8aa0SThomas Gleixner 	}
335e64c8aa0SThomas Gleixner }
336e64c8aa0SThomas Gleixner 
337e64c8aa0SThomas Gleixner static void __init __early_set_fixmap(enum fixed_addresses idx,
338e64c8aa0SThomas Gleixner 				   unsigned long phys, pgprot_t flags)
339e64c8aa0SThomas Gleixner {
340e64c8aa0SThomas Gleixner 	unsigned long *pte, addr = __fix_to_virt(idx);
341e64c8aa0SThomas Gleixner 
342e64c8aa0SThomas Gleixner 	if (idx >= __end_of_fixed_addresses) {
343e64c8aa0SThomas Gleixner 		BUG();
344e64c8aa0SThomas Gleixner 		return;
345e64c8aa0SThomas Gleixner 	}
346e64c8aa0SThomas Gleixner 	pte = early_ioremap_pte(addr);
347e64c8aa0SThomas Gleixner 	if (pgprot_val(flags))
348e64c8aa0SThomas Gleixner 		*pte = (phys & PAGE_MASK) | pgprot_val(flags);
349e64c8aa0SThomas Gleixner 	else
350e64c8aa0SThomas Gleixner 		*pte = 0;
351e64c8aa0SThomas Gleixner 	__flush_tlb_one(addr);
352e64c8aa0SThomas Gleixner }
353e64c8aa0SThomas Gleixner 
354e64c8aa0SThomas Gleixner static inline void __init early_set_fixmap(enum fixed_addresses idx,
355e64c8aa0SThomas Gleixner 					unsigned long phys)
356e64c8aa0SThomas Gleixner {
357e64c8aa0SThomas Gleixner 	if (after_paging_init)
358e64c8aa0SThomas Gleixner 		set_fixmap(idx, phys);
359e64c8aa0SThomas Gleixner 	else
360e64c8aa0SThomas Gleixner 		__early_set_fixmap(idx, phys, PAGE_KERNEL);
361e64c8aa0SThomas Gleixner }
362e64c8aa0SThomas Gleixner 
363e64c8aa0SThomas Gleixner static inline void __init early_clear_fixmap(enum fixed_addresses idx)
364e64c8aa0SThomas Gleixner {
365e64c8aa0SThomas Gleixner 	if (after_paging_init)
366e64c8aa0SThomas Gleixner 		clear_fixmap(idx);
367e64c8aa0SThomas Gleixner 	else
368e64c8aa0SThomas Gleixner 		__early_set_fixmap(idx, 0, __pgprot(0));
369e64c8aa0SThomas Gleixner }
370e64c8aa0SThomas Gleixner 
371e64c8aa0SThomas Gleixner 
372e64c8aa0SThomas Gleixner int __initdata early_ioremap_nested;
373e64c8aa0SThomas Gleixner 
374e64c8aa0SThomas Gleixner static int __init check_early_ioremap_leak(void)
375e64c8aa0SThomas Gleixner {
376e64c8aa0SThomas Gleixner 	if (!early_ioremap_nested)
377e64c8aa0SThomas Gleixner 		return 0;
378e64c8aa0SThomas Gleixner 
379e64c8aa0SThomas Gleixner 	printk(KERN_WARNING
380e64c8aa0SThomas Gleixner 	       "Debug warning: early ioremap leak of %d areas detected.\n",
381e64c8aa0SThomas Gleixner 	       early_ioremap_nested);
382e64c8aa0SThomas Gleixner 	printk(KERN_WARNING
383e64c8aa0SThomas Gleixner 	       "please boot with early_ioremap_debug and report the dmesg.\n");
384e64c8aa0SThomas Gleixner 	WARN_ON(1);
385e64c8aa0SThomas Gleixner 
386e64c8aa0SThomas Gleixner 	return 1;
387e64c8aa0SThomas Gleixner }
388e64c8aa0SThomas Gleixner late_initcall(check_early_ioremap_leak);
389e64c8aa0SThomas Gleixner 
390e64c8aa0SThomas Gleixner void __init *early_ioremap(unsigned long phys_addr, unsigned long size)
391e64c8aa0SThomas Gleixner {
392e64c8aa0SThomas Gleixner 	unsigned long offset, last_addr;
393e64c8aa0SThomas Gleixner 	unsigned int nrpages, nesting;
394e64c8aa0SThomas Gleixner 	enum fixed_addresses idx0, idx;
395e64c8aa0SThomas Gleixner 
396e64c8aa0SThomas Gleixner 	WARN_ON(system_state != SYSTEM_BOOTING);
397e64c8aa0SThomas Gleixner 
398e64c8aa0SThomas Gleixner 	nesting = early_ioremap_nested;
399e64c8aa0SThomas Gleixner 	if (early_ioremap_debug) {
400e64c8aa0SThomas Gleixner 		printk(KERN_DEBUG "early_ioremap(%08lx, %08lx) [%d] => ",
401e64c8aa0SThomas Gleixner 		       phys_addr, size, nesting);
402e64c8aa0SThomas Gleixner 		dump_stack();
403e64c8aa0SThomas Gleixner 	}
404e64c8aa0SThomas Gleixner 
405e64c8aa0SThomas Gleixner 	/* Don't allow wraparound or zero size */
406e64c8aa0SThomas Gleixner 	last_addr = phys_addr + size - 1;
407e64c8aa0SThomas Gleixner 	if (!size || last_addr < phys_addr) {
408e64c8aa0SThomas Gleixner 		WARN_ON(1);
409e64c8aa0SThomas Gleixner 		return NULL;
410e64c8aa0SThomas Gleixner 	}
411e64c8aa0SThomas Gleixner 
412e64c8aa0SThomas Gleixner 	if (nesting >= FIX_BTMAPS_NESTING) {
413e64c8aa0SThomas Gleixner 		WARN_ON(1);
414e64c8aa0SThomas Gleixner 		return NULL;
415e64c8aa0SThomas Gleixner 	}
416e64c8aa0SThomas Gleixner 	early_ioremap_nested++;
417e64c8aa0SThomas Gleixner 	/*
418e64c8aa0SThomas Gleixner 	 * Mappings have to be page-aligned
419e64c8aa0SThomas Gleixner 	 */
420e64c8aa0SThomas Gleixner 	offset = phys_addr & ~PAGE_MASK;
421e64c8aa0SThomas Gleixner 	phys_addr &= PAGE_MASK;
422e64c8aa0SThomas Gleixner 	size = PAGE_ALIGN(last_addr) - phys_addr;
423e64c8aa0SThomas Gleixner 
424e64c8aa0SThomas Gleixner 	/*
425e64c8aa0SThomas Gleixner 	 * Mappings have to fit in the FIX_BTMAP area.
426e64c8aa0SThomas Gleixner 	 */
427e64c8aa0SThomas Gleixner 	nrpages = size >> PAGE_SHIFT;
428e64c8aa0SThomas Gleixner 	if (nrpages > NR_FIX_BTMAPS) {
429e64c8aa0SThomas Gleixner 		WARN_ON(1);
430e64c8aa0SThomas Gleixner 		return NULL;
431e64c8aa0SThomas Gleixner 	}
432e64c8aa0SThomas Gleixner 
433e64c8aa0SThomas Gleixner 	/*
434e64c8aa0SThomas Gleixner 	 * Ok, go for it..
435e64c8aa0SThomas Gleixner 	 */
436e64c8aa0SThomas Gleixner 	idx0 = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*nesting;
437e64c8aa0SThomas Gleixner 	idx = idx0;
438e64c8aa0SThomas Gleixner 	while (nrpages > 0) {
439e64c8aa0SThomas Gleixner 		early_set_fixmap(idx, phys_addr);
440e64c8aa0SThomas Gleixner 		phys_addr += PAGE_SIZE;
441e64c8aa0SThomas Gleixner 		--idx;
442e64c8aa0SThomas Gleixner 		--nrpages;
443e64c8aa0SThomas Gleixner 	}
444e64c8aa0SThomas Gleixner 	if (early_ioremap_debug)
445e64c8aa0SThomas Gleixner 		printk(KERN_CONT "%08lx + %08lx\n", offset, fix_to_virt(idx0));
446e64c8aa0SThomas Gleixner 
447e64c8aa0SThomas Gleixner 	return (void *) (offset + fix_to_virt(idx0));
448e64c8aa0SThomas Gleixner }
449e64c8aa0SThomas Gleixner 
450e64c8aa0SThomas Gleixner void __init early_iounmap(void *addr, unsigned long size)
451e64c8aa0SThomas Gleixner {
452e64c8aa0SThomas Gleixner 	unsigned long virt_addr;
453e64c8aa0SThomas Gleixner 	unsigned long offset;
454e64c8aa0SThomas Gleixner 	unsigned int nrpages;
455e64c8aa0SThomas Gleixner 	enum fixed_addresses idx;
456e64c8aa0SThomas Gleixner 	unsigned int nesting;
457e64c8aa0SThomas Gleixner 
458e64c8aa0SThomas Gleixner 	nesting = --early_ioremap_nested;
459e64c8aa0SThomas Gleixner 	WARN_ON(nesting < 0);
460e64c8aa0SThomas Gleixner 
461e64c8aa0SThomas Gleixner 	if (early_ioremap_debug) {
462e64c8aa0SThomas Gleixner 		printk(KERN_DEBUG "early_iounmap(%p, %08lx) [%d]\n", addr,
463e64c8aa0SThomas Gleixner 		       size, nesting);
464e64c8aa0SThomas Gleixner 		dump_stack();
465e64c8aa0SThomas Gleixner 	}
466e64c8aa0SThomas Gleixner 
467e64c8aa0SThomas Gleixner 	virt_addr = (unsigned long)addr;
468e64c8aa0SThomas Gleixner 	if (virt_addr < fix_to_virt(FIX_BTMAP_BEGIN)) {
469e64c8aa0SThomas Gleixner 		WARN_ON(1);
470e64c8aa0SThomas Gleixner 		return;
471e64c8aa0SThomas Gleixner 	}
472e64c8aa0SThomas Gleixner 	offset = virt_addr & ~PAGE_MASK;
473e64c8aa0SThomas Gleixner 	nrpages = PAGE_ALIGN(offset + size - 1) >> PAGE_SHIFT;
474e64c8aa0SThomas Gleixner 
475e64c8aa0SThomas Gleixner 	idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*nesting;
476e64c8aa0SThomas Gleixner 	while (nrpages > 0) {
477e64c8aa0SThomas Gleixner 		early_clear_fixmap(idx);
478e64c8aa0SThomas Gleixner 		--idx;
479e64c8aa0SThomas Gleixner 		--nrpages;
480e64c8aa0SThomas Gleixner 	}
481e64c8aa0SThomas Gleixner }
482e64c8aa0SThomas Gleixner 
483e64c8aa0SThomas Gleixner void __this_fixmap_does_not_exist(void)
484e64c8aa0SThomas Gleixner {
485e64c8aa0SThomas Gleixner 	WARN_ON(1);
486e64c8aa0SThomas Gleixner }
487e64c8aa0SThomas Gleixner 
488e64c8aa0SThomas Gleixner #endif /* CONFIG_X86_32 */
489