xref: /linux/arch/x86/mm/ioremap.c (revision b16bf712f491808a8c926dd481c696fe7d73ee5a)
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>
21f6df72e7SJeremy Fitzhardinge #include <asm/pgalloc.h>
22e64c8aa0SThomas Gleixner 
23d806e5eeSThomas Gleixner enum ioremap_mode {
24d806e5eeSThomas Gleixner 	IOR_MODE_UNCACHED,
25d806e5eeSThomas Gleixner 	IOR_MODE_CACHED,
26d806e5eeSThomas Gleixner };
27d806e5eeSThomas Gleixner 
28e64c8aa0SThomas Gleixner #ifdef CONFIG_X86_64
29e64c8aa0SThomas Gleixner 
30e64c8aa0SThomas Gleixner unsigned long __phys_addr(unsigned long x)
31e64c8aa0SThomas Gleixner {
32e64c8aa0SThomas Gleixner 	if (x >= __START_KERNEL_map)
33e64c8aa0SThomas Gleixner 		return x - __START_KERNEL_map + phys_base;
34e64c8aa0SThomas Gleixner 	return x - PAGE_OFFSET;
35e64c8aa0SThomas Gleixner }
36e64c8aa0SThomas Gleixner EXPORT_SYMBOL(__phys_addr);
37e64c8aa0SThomas Gleixner 
38e64c8aa0SThomas Gleixner #endif
39e64c8aa0SThomas Gleixner 
405f5192b9SThomas Gleixner int page_is_ram(unsigned long pagenr)
415f5192b9SThomas Gleixner {
425f5192b9SThomas Gleixner 	unsigned long addr, end;
435f5192b9SThomas Gleixner 	int i;
445f5192b9SThomas Gleixner 
45d8a9e6a5SArjan van de Ven 	/*
46d8a9e6a5SArjan van de Ven 	 * A special case is the first 4Kb of memory;
47d8a9e6a5SArjan van de Ven 	 * This is a BIOS owned area, not kernel ram, but generally
48d8a9e6a5SArjan van de Ven 	 * not listed as such in the E820 table.
49d8a9e6a5SArjan van de Ven 	 */
50d8a9e6a5SArjan van de Ven 	if (pagenr == 0)
51d8a9e6a5SArjan van de Ven 		return 0;
52d8a9e6a5SArjan van de Ven 
53156fbc3fSArjan van de Ven 	/*
54156fbc3fSArjan van de Ven 	 * Second special case: Some BIOSen report the PC BIOS
55156fbc3fSArjan van de Ven 	 * area (640->1Mb) as ram even though it is not.
56156fbc3fSArjan van de Ven 	 */
57156fbc3fSArjan van de Ven 	if (pagenr >= (BIOS_BEGIN >> PAGE_SHIFT) &&
58156fbc3fSArjan van de Ven 		    pagenr < (BIOS_END >> PAGE_SHIFT))
59156fbc3fSArjan van de Ven 		return 0;
60d8a9e6a5SArjan van de Ven 
615f5192b9SThomas Gleixner 	for (i = 0; i < e820.nr_map; i++) {
625f5192b9SThomas Gleixner 		/*
635f5192b9SThomas Gleixner 		 * Not usable memory:
645f5192b9SThomas Gleixner 		 */
655f5192b9SThomas Gleixner 		if (e820.map[i].type != E820_RAM)
665f5192b9SThomas Gleixner 			continue;
675f5192b9SThomas Gleixner 		addr = (e820.map[i].addr + PAGE_SIZE-1) >> PAGE_SHIFT;
685f5192b9SThomas Gleixner 		end = (e820.map[i].addr + e820.map[i].size) >> PAGE_SHIFT;
69950f9d95SThomas Gleixner 
70950f9d95SThomas Gleixner 
715f5192b9SThomas Gleixner 		if ((pagenr >= addr) && (pagenr < end))
725f5192b9SThomas Gleixner 			return 1;
735f5192b9SThomas Gleixner 	}
745f5192b9SThomas Gleixner 	return 0;
755f5192b9SThomas Gleixner }
765f5192b9SThomas Gleixner 
77e64c8aa0SThomas Gleixner /*
78e64c8aa0SThomas Gleixner  * Fix up the linear direct mapping of the kernel to avoid cache attribute
79e64c8aa0SThomas Gleixner  * conflicts.
80e64c8aa0SThomas Gleixner  */
8175ab43bfSThomas Gleixner static int ioremap_change_attr(unsigned long vaddr, unsigned long size,
82d806e5eeSThomas Gleixner 			       enum ioremap_mode mode)
83e64c8aa0SThomas Gleixner {
84d806e5eeSThomas Gleixner 	unsigned long nrpages = size >> PAGE_SHIFT;
8593809be8SHarvey Harrison 	int err;
86e64c8aa0SThomas Gleixner 
87d806e5eeSThomas Gleixner 	switch (mode) {
88d806e5eeSThomas Gleixner 	case IOR_MODE_UNCACHED:
89d806e5eeSThomas Gleixner 	default:
90d806e5eeSThomas Gleixner 		err = set_memory_uc(vaddr, nrpages);
91d806e5eeSThomas Gleixner 		break;
92d806e5eeSThomas Gleixner 	case IOR_MODE_CACHED:
93d806e5eeSThomas Gleixner 		err = set_memory_wb(vaddr, nrpages);
94d806e5eeSThomas Gleixner 		break;
95d806e5eeSThomas Gleixner 	}
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  */
1095f868152SThomas Gleixner static void __iomem *__ioremap(unsigned long phys_addr, unsigned long size,
110d806e5eeSThomas Gleixner 			       enum ioremap_mode mode)
111e64c8aa0SThomas Gleixner {
112e66aadbeSThomas Gleixner 	unsigned long pfn, offset, last_addr, vaddr;
113e64c8aa0SThomas Gleixner 	struct vm_struct *area;
114d806e5eeSThomas Gleixner 	pgprot_t prot;
115e64c8aa0SThomas Gleixner 
116e64c8aa0SThomas Gleixner 	/* Don't allow wraparound or zero size */
117e64c8aa0SThomas Gleixner 	last_addr = phys_addr + size - 1;
118e64c8aa0SThomas Gleixner 	if (!size || last_addr < phys_addr)
119e64c8aa0SThomas Gleixner 		return NULL;
120e64c8aa0SThomas Gleixner 
121e64c8aa0SThomas Gleixner 	/*
122e64c8aa0SThomas Gleixner 	 * Don't remap the low PCI/ISA area, it's always mapped..
123e64c8aa0SThomas Gleixner 	 */
124e64c8aa0SThomas Gleixner 	if (phys_addr >= ISA_START_ADDRESS && last_addr < ISA_END_ADDRESS)
125e64c8aa0SThomas Gleixner 		return (__force void __iomem *)phys_to_virt(phys_addr);
126e64c8aa0SThomas Gleixner 
127e64c8aa0SThomas Gleixner 	/*
128e64c8aa0SThomas Gleixner 	 * Don't allow anybody to remap normal RAM that we're using..
129e64c8aa0SThomas Gleixner 	 */
13038cb47baSIngo Molnar 	for (pfn = phys_addr >> PAGE_SHIFT; pfn < max_pfn_mapped &&
13138cb47baSIngo Molnar 	     (pfn << PAGE_SHIFT) < last_addr; pfn++) {
13238cb47baSIngo Molnar 		if (page_is_ram(pfn) && pfn_valid(pfn) &&
13338cb47baSIngo Molnar 		    !PageReserved(pfn_to_page(pfn)))
134e64c8aa0SThomas Gleixner 			return NULL;
135e64c8aa0SThomas Gleixner 	}
136e64c8aa0SThomas Gleixner 
137fcea424dSArjan van dev Ven 	WARN_ON_ONCE(page_is_ram(pfn));
138fcea424dSArjan van dev Ven 
139d806e5eeSThomas Gleixner 	switch (mode) {
140d806e5eeSThomas Gleixner 	case IOR_MODE_UNCACHED:
141d806e5eeSThomas Gleixner 	default:
142d806e5eeSThomas Gleixner 		prot = PAGE_KERNEL_NOCACHE;
143d806e5eeSThomas Gleixner 		break;
144d806e5eeSThomas Gleixner 	case IOR_MODE_CACHED:
145d806e5eeSThomas Gleixner 		prot = PAGE_KERNEL;
146d806e5eeSThomas Gleixner 		break;
147d806e5eeSThomas Gleixner 	}
148e64c8aa0SThomas Gleixner 
149e64c8aa0SThomas Gleixner 	/*
150e64c8aa0SThomas Gleixner 	 * Mappings have to be page-aligned
151e64c8aa0SThomas Gleixner 	 */
152e64c8aa0SThomas Gleixner 	offset = phys_addr & ~PAGE_MASK;
153e64c8aa0SThomas Gleixner 	phys_addr &= PAGE_MASK;
154e64c8aa0SThomas Gleixner 	size = PAGE_ALIGN(last_addr+1) - phys_addr;
155e64c8aa0SThomas Gleixner 
156e64c8aa0SThomas Gleixner 	/*
157e64c8aa0SThomas Gleixner 	 * Ok, go for it..
158e64c8aa0SThomas Gleixner 	 */
159e64c8aa0SThomas Gleixner 	area = get_vm_area(size, VM_IOREMAP);
160e64c8aa0SThomas Gleixner 	if (!area)
161e64c8aa0SThomas Gleixner 		return NULL;
162e64c8aa0SThomas Gleixner 	area->phys_addr = phys_addr;
163e66aadbeSThomas Gleixner 	vaddr = (unsigned long) area->addr;
164e66aadbeSThomas Gleixner 	if (ioremap_page_range(vaddr, vaddr + size, phys_addr, prot)) {
165*b16bf712SIngo Molnar 		free_vm_area(area);
166e64c8aa0SThomas Gleixner 		return NULL;
167e64c8aa0SThomas Gleixner 	}
168e64c8aa0SThomas Gleixner 
16975ab43bfSThomas Gleixner 	if (ioremap_change_attr(vaddr, size, mode) < 0) {
170e66aadbeSThomas Gleixner 		vunmap(area->addr);
171e64c8aa0SThomas Gleixner 		return NULL;
172e64c8aa0SThomas Gleixner 	}
173e64c8aa0SThomas Gleixner 
174e66aadbeSThomas Gleixner 	return (void __iomem *) (vaddr + offset);
175e64c8aa0SThomas Gleixner }
176e64c8aa0SThomas Gleixner 
177e64c8aa0SThomas Gleixner /**
178e64c8aa0SThomas Gleixner  * ioremap_nocache     -   map bus memory into CPU space
179e64c8aa0SThomas Gleixner  * @offset:    bus address of the memory
180e64c8aa0SThomas Gleixner  * @size:      size of the resource to map
181e64c8aa0SThomas Gleixner  *
182e64c8aa0SThomas Gleixner  * ioremap_nocache performs a platform specific sequence of operations to
183e64c8aa0SThomas Gleixner  * make bus memory CPU accessible via the readb/readw/readl/writeb/
184e64c8aa0SThomas Gleixner  * writew/writel functions and the other mmio helpers. The returned
185e64c8aa0SThomas Gleixner  * address is not guaranteed to be usable directly as a virtual
186e64c8aa0SThomas Gleixner  * address.
187e64c8aa0SThomas Gleixner  *
188e64c8aa0SThomas Gleixner  * This version of ioremap ensures that the memory is marked uncachable
189e64c8aa0SThomas Gleixner  * on the CPU as well as honouring existing caching rules from things like
190e64c8aa0SThomas Gleixner  * the PCI bus. Note that there are other caches and buffers on many
191e64c8aa0SThomas Gleixner  * busses. In particular driver authors should read up on PCI writes
192e64c8aa0SThomas Gleixner  *
193e64c8aa0SThomas Gleixner  * It's useful if some control registers are in such an area and
194e64c8aa0SThomas Gleixner  * write combining or read caching is not desirable:
195e64c8aa0SThomas Gleixner  *
196e64c8aa0SThomas Gleixner  * Must be freed with iounmap.
197e64c8aa0SThomas Gleixner  */
198e64c8aa0SThomas Gleixner void __iomem *ioremap_nocache(unsigned long phys_addr, unsigned long size)
199e64c8aa0SThomas Gleixner {
200d806e5eeSThomas Gleixner 	return __ioremap(phys_addr, size, IOR_MODE_UNCACHED);
201e64c8aa0SThomas Gleixner }
202e64c8aa0SThomas Gleixner EXPORT_SYMBOL(ioremap_nocache);
203e64c8aa0SThomas Gleixner 
2045f868152SThomas Gleixner void __iomem *ioremap_cache(unsigned long phys_addr, unsigned long size)
2055f868152SThomas Gleixner {
206d806e5eeSThomas Gleixner 	return __ioremap(phys_addr, size, IOR_MODE_CACHED);
2075f868152SThomas Gleixner }
2085f868152SThomas Gleixner EXPORT_SYMBOL(ioremap_cache);
2095f868152SThomas Gleixner 
210e64c8aa0SThomas Gleixner /**
211e64c8aa0SThomas Gleixner  * iounmap - Free a IO remapping
212e64c8aa0SThomas Gleixner  * @addr: virtual address from ioremap_*
213e64c8aa0SThomas Gleixner  *
214e64c8aa0SThomas Gleixner  * Caller must ensure there is only one unmapping for the same pointer.
215e64c8aa0SThomas Gleixner  */
216e64c8aa0SThomas Gleixner void iounmap(volatile void __iomem *addr)
217e64c8aa0SThomas Gleixner {
218e64c8aa0SThomas Gleixner 	struct vm_struct *p, *o;
219e64c8aa0SThomas Gleixner 
220e64c8aa0SThomas Gleixner 	if ((void __force *)addr <= high_memory)
221e64c8aa0SThomas Gleixner 		return;
222e64c8aa0SThomas Gleixner 
223e64c8aa0SThomas Gleixner 	/*
224e64c8aa0SThomas Gleixner 	 * __ioremap special-cases the PCI/ISA range by not instantiating a
225e64c8aa0SThomas Gleixner 	 * vm_area and by simply returning an address into the kernel mapping
226e64c8aa0SThomas Gleixner 	 * of ISA space.   So handle that here.
227e64c8aa0SThomas Gleixner 	 */
228e64c8aa0SThomas Gleixner 	if (addr >= phys_to_virt(ISA_START_ADDRESS) &&
229e64c8aa0SThomas Gleixner 	    addr < phys_to_virt(ISA_END_ADDRESS))
230e64c8aa0SThomas Gleixner 		return;
231e64c8aa0SThomas Gleixner 
232e64c8aa0SThomas Gleixner 	addr = (volatile void __iomem *)
233e64c8aa0SThomas Gleixner 		(PAGE_MASK & (unsigned long __force)addr);
234e64c8aa0SThomas Gleixner 
235e64c8aa0SThomas Gleixner 	/* Use the vm area unlocked, assuming the caller
236e64c8aa0SThomas Gleixner 	   ensures there isn't another iounmap for the same address
237e64c8aa0SThomas Gleixner 	   in parallel. Reuse of the virtual address is prevented by
238e64c8aa0SThomas Gleixner 	   leaving it in the global lists until we're done with it.
239e64c8aa0SThomas Gleixner 	   cpa takes care of the direct mappings. */
240e64c8aa0SThomas Gleixner 	read_lock(&vmlist_lock);
241e64c8aa0SThomas Gleixner 	for (p = vmlist; p; p = p->next) {
242e64c8aa0SThomas Gleixner 		if (p->addr == addr)
243e64c8aa0SThomas Gleixner 			break;
244e64c8aa0SThomas Gleixner 	}
245e64c8aa0SThomas Gleixner 	read_unlock(&vmlist_lock);
246e64c8aa0SThomas Gleixner 
247e64c8aa0SThomas Gleixner 	if (!p) {
248e64c8aa0SThomas Gleixner 		printk(KERN_ERR "iounmap: bad address %p\n", addr);
249e64c8aa0SThomas Gleixner 		dump_stack();
250e64c8aa0SThomas Gleixner 		return;
251e64c8aa0SThomas Gleixner 	}
252e64c8aa0SThomas Gleixner 
253e64c8aa0SThomas Gleixner 	/* Finally remove it */
254e64c8aa0SThomas Gleixner 	o = remove_vm_area((void *)addr);
255e64c8aa0SThomas Gleixner 	BUG_ON(p != o || o == NULL);
256e64c8aa0SThomas Gleixner 	kfree(p);
257e64c8aa0SThomas Gleixner }
258e64c8aa0SThomas Gleixner EXPORT_SYMBOL(iounmap);
259e64c8aa0SThomas Gleixner 
260e64c8aa0SThomas Gleixner #ifdef CONFIG_X86_32
261e64c8aa0SThomas Gleixner 
262e64c8aa0SThomas Gleixner int __initdata early_ioremap_debug;
263e64c8aa0SThomas Gleixner 
264e64c8aa0SThomas Gleixner static int __init early_ioremap_debug_setup(char *str)
265e64c8aa0SThomas Gleixner {
266e64c8aa0SThomas Gleixner 	early_ioremap_debug = 1;
267e64c8aa0SThomas Gleixner 
268e64c8aa0SThomas Gleixner 	return 0;
269e64c8aa0SThomas Gleixner }
270e64c8aa0SThomas Gleixner early_param("early_ioremap_debug", early_ioremap_debug_setup);
271e64c8aa0SThomas Gleixner 
272e64c8aa0SThomas Gleixner static __initdata int after_paging_init;
273551889a6SIan Campbell static __initdata pte_t bm_pte[PAGE_SIZE/sizeof(pte_t)]
274e64c8aa0SThomas Gleixner 				__attribute__((aligned(PAGE_SIZE)));
275e64c8aa0SThomas Gleixner 
276551889a6SIan Campbell static inline pmd_t * __init early_ioremap_pmd(unsigned long addr)
277e64c8aa0SThomas Gleixner {
27837cc8d7fSJeremy Fitzhardinge 	/* Don't assume we're using swapper_pg_dir at this point */
27937cc8d7fSJeremy Fitzhardinge 	pgd_t *base = __va(read_cr3());
28037cc8d7fSJeremy Fitzhardinge 	pgd_t *pgd = &base[pgd_index(addr)];
281551889a6SIan Campbell 	pud_t *pud = pud_offset(pgd, addr);
282551889a6SIan Campbell 	pmd_t *pmd = pmd_offset(pud, addr);
283551889a6SIan Campbell 
284551889a6SIan Campbell 	return pmd;
285e64c8aa0SThomas Gleixner }
286e64c8aa0SThomas Gleixner 
287551889a6SIan Campbell static inline pte_t * __init early_ioremap_pte(unsigned long addr)
288e64c8aa0SThomas Gleixner {
289551889a6SIan Campbell 	return &bm_pte[pte_index(addr)];
290e64c8aa0SThomas Gleixner }
291e64c8aa0SThomas Gleixner 
292e64c8aa0SThomas Gleixner void __init early_ioremap_init(void)
293e64c8aa0SThomas Gleixner {
294551889a6SIan Campbell 	pmd_t *pmd;
295e64c8aa0SThomas Gleixner 
296e64c8aa0SThomas Gleixner 	if (early_ioremap_debug)
297adafdf6aSIngo Molnar 		printk(KERN_INFO "early_ioremap_init()\n");
298e64c8aa0SThomas Gleixner 
299551889a6SIan Campbell 	pmd = early_ioremap_pmd(fix_to_virt(FIX_BTMAP_BEGIN));
300e64c8aa0SThomas Gleixner 	memset(bm_pte, 0, sizeof(bm_pte));
301b6fbb669SIan Campbell 	pmd_populate_kernel(&init_mm, pmd, bm_pte);
302551889a6SIan Campbell 
303e64c8aa0SThomas Gleixner 	/*
304551889a6SIan Campbell 	 * The boot-ioremap range spans multiple pmds, for which
305e64c8aa0SThomas Gleixner 	 * we are not prepared:
306e64c8aa0SThomas Gleixner 	 */
307551889a6SIan Campbell 	if (pmd != early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END))) {
308e64c8aa0SThomas Gleixner 		WARN_ON(1);
309551889a6SIan Campbell 		printk(KERN_WARNING "pmd %p != %p\n",
310551889a6SIan Campbell 		       pmd, early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END)));
311e64c8aa0SThomas Gleixner 		printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
312e64c8aa0SThomas Gleixner 			fix_to_virt(FIX_BTMAP_BEGIN));
313e64c8aa0SThomas Gleixner 		printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_END):   %08lx\n",
314e64c8aa0SThomas Gleixner 			fix_to_virt(FIX_BTMAP_END));
315e64c8aa0SThomas Gleixner 
316e64c8aa0SThomas Gleixner 		printk(KERN_WARNING "FIX_BTMAP_END:       %d\n", FIX_BTMAP_END);
317e64c8aa0SThomas Gleixner 		printk(KERN_WARNING "FIX_BTMAP_BEGIN:     %d\n",
318e64c8aa0SThomas Gleixner 		       FIX_BTMAP_BEGIN);
319e64c8aa0SThomas Gleixner 	}
320e64c8aa0SThomas Gleixner }
321e64c8aa0SThomas Gleixner 
322e64c8aa0SThomas Gleixner void __init early_ioremap_clear(void)
323e64c8aa0SThomas Gleixner {
324551889a6SIan Campbell 	pmd_t *pmd;
325e64c8aa0SThomas Gleixner 
326e64c8aa0SThomas Gleixner 	if (early_ioremap_debug)
327adafdf6aSIngo Molnar 		printk(KERN_INFO "early_ioremap_clear()\n");
328e64c8aa0SThomas Gleixner 
329551889a6SIan Campbell 	pmd = early_ioremap_pmd(fix_to_virt(FIX_BTMAP_BEGIN));
330551889a6SIan Campbell 	pmd_clear(pmd);
331b6fbb669SIan Campbell 	paravirt_release_pt(__pa(bm_pte) >> PAGE_SHIFT);
332e64c8aa0SThomas Gleixner 	__flush_tlb_all();
333e64c8aa0SThomas Gleixner }
334e64c8aa0SThomas Gleixner 
335e64c8aa0SThomas Gleixner void __init early_ioremap_reset(void)
336e64c8aa0SThomas Gleixner {
337e64c8aa0SThomas Gleixner 	enum fixed_addresses idx;
338551889a6SIan Campbell 	unsigned long addr, phys;
339551889a6SIan Campbell 	pte_t *pte;
340e64c8aa0SThomas Gleixner 
341e64c8aa0SThomas Gleixner 	after_paging_init = 1;
342e64c8aa0SThomas Gleixner 	for (idx = FIX_BTMAP_BEGIN; idx >= FIX_BTMAP_END; idx--) {
343e64c8aa0SThomas Gleixner 		addr = fix_to_virt(idx);
344e64c8aa0SThomas Gleixner 		pte = early_ioremap_pte(addr);
345551889a6SIan Campbell 		if (pte_present(*pte)) {
346551889a6SIan Campbell 			phys = pte_val(*pte) & PAGE_MASK;
347e64c8aa0SThomas Gleixner 			set_fixmap(idx, phys);
348e64c8aa0SThomas Gleixner 		}
349e64c8aa0SThomas Gleixner 	}
350e64c8aa0SThomas Gleixner }
351e64c8aa0SThomas Gleixner 
352e64c8aa0SThomas Gleixner static void __init __early_set_fixmap(enum fixed_addresses idx,
353e64c8aa0SThomas Gleixner 				   unsigned long phys, pgprot_t flags)
354e64c8aa0SThomas Gleixner {
355551889a6SIan Campbell 	unsigned long addr = __fix_to_virt(idx);
356551889a6SIan Campbell 	pte_t *pte;
357e64c8aa0SThomas Gleixner 
358e64c8aa0SThomas Gleixner 	if (idx >= __end_of_fixed_addresses) {
359e64c8aa0SThomas Gleixner 		BUG();
360e64c8aa0SThomas Gleixner 		return;
361e64c8aa0SThomas Gleixner 	}
362e64c8aa0SThomas Gleixner 	pte = early_ioremap_pte(addr);
363e64c8aa0SThomas Gleixner 	if (pgprot_val(flags))
364551889a6SIan Campbell 		set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
365e64c8aa0SThomas Gleixner 	else
366551889a6SIan Campbell 		pte_clear(NULL, addr, pte);
367e64c8aa0SThomas Gleixner 	__flush_tlb_one(addr);
368e64c8aa0SThomas Gleixner }
369e64c8aa0SThomas Gleixner 
370e64c8aa0SThomas Gleixner static inline void __init early_set_fixmap(enum fixed_addresses idx,
371e64c8aa0SThomas Gleixner 					unsigned long phys)
372e64c8aa0SThomas Gleixner {
373e64c8aa0SThomas Gleixner 	if (after_paging_init)
374e64c8aa0SThomas Gleixner 		set_fixmap(idx, phys);
375e64c8aa0SThomas Gleixner 	else
376e64c8aa0SThomas Gleixner 		__early_set_fixmap(idx, phys, PAGE_KERNEL);
377e64c8aa0SThomas Gleixner }
378e64c8aa0SThomas Gleixner 
379e64c8aa0SThomas Gleixner static inline void __init early_clear_fixmap(enum fixed_addresses idx)
380e64c8aa0SThomas Gleixner {
381e64c8aa0SThomas Gleixner 	if (after_paging_init)
382e64c8aa0SThomas Gleixner 		clear_fixmap(idx);
383e64c8aa0SThomas Gleixner 	else
384e64c8aa0SThomas Gleixner 		__early_set_fixmap(idx, 0, __pgprot(0));
385e64c8aa0SThomas Gleixner }
386e64c8aa0SThomas Gleixner 
387e64c8aa0SThomas Gleixner 
388e64c8aa0SThomas Gleixner int __initdata early_ioremap_nested;
389e64c8aa0SThomas Gleixner 
390e64c8aa0SThomas Gleixner static int __init check_early_ioremap_leak(void)
391e64c8aa0SThomas Gleixner {
392e64c8aa0SThomas Gleixner 	if (!early_ioremap_nested)
393e64c8aa0SThomas Gleixner 		return 0;
394e64c8aa0SThomas Gleixner 
395e64c8aa0SThomas Gleixner 	printk(KERN_WARNING
396e64c8aa0SThomas Gleixner 	       "Debug warning: early ioremap leak of %d areas detected.\n",
397e64c8aa0SThomas Gleixner 	       early_ioremap_nested);
398e64c8aa0SThomas Gleixner 	printk(KERN_WARNING
399e64c8aa0SThomas Gleixner 	       "please boot with early_ioremap_debug and report the dmesg.\n");
400e64c8aa0SThomas Gleixner 	WARN_ON(1);
401e64c8aa0SThomas Gleixner 
402e64c8aa0SThomas Gleixner 	return 1;
403e64c8aa0SThomas Gleixner }
404e64c8aa0SThomas Gleixner late_initcall(check_early_ioremap_leak);
405e64c8aa0SThomas Gleixner 
406e64c8aa0SThomas Gleixner void __init *early_ioremap(unsigned long phys_addr, unsigned long size)
407e64c8aa0SThomas Gleixner {
408e64c8aa0SThomas Gleixner 	unsigned long offset, last_addr;
409e64c8aa0SThomas Gleixner 	unsigned int nrpages, nesting;
410e64c8aa0SThomas Gleixner 	enum fixed_addresses idx0, idx;
411e64c8aa0SThomas Gleixner 
412e64c8aa0SThomas Gleixner 	WARN_ON(system_state != SYSTEM_BOOTING);
413e64c8aa0SThomas Gleixner 
414e64c8aa0SThomas Gleixner 	nesting = early_ioremap_nested;
415e64c8aa0SThomas Gleixner 	if (early_ioremap_debug) {
416adafdf6aSIngo Molnar 		printk(KERN_INFO "early_ioremap(%08lx, %08lx) [%d] => ",
417e64c8aa0SThomas Gleixner 		       phys_addr, size, nesting);
418e64c8aa0SThomas Gleixner 		dump_stack();
419e64c8aa0SThomas Gleixner 	}
420e64c8aa0SThomas Gleixner 
421e64c8aa0SThomas Gleixner 	/* Don't allow wraparound or zero size */
422e64c8aa0SThomas Gleixner 	last_addr = phys_addr + size - 1;
423e64c8aa0SThomas Gleixner 	if (!size || last_addr < phys_addr) {
424e64c8aa0SThomas Gleixner 		WARN_ON(1);
425e64c8aa0SThomas Gleixner 		return NULL;
426e64c8aa0SThomas Gleixner 	}
427e64c8aa0SThomas Gleixner 
428e64c8aa0SThomas Gleixner 	if (nesting >= FIX_BTMAPS_NESTING) {
429e64c8aa0SThomas Gleixner 		WARN_ON(1);
430e64c8aa0SThomas Gleixner 		return NULL;
431e64c8aa0SThomas Gleixner 	}
432e64c8aa0SThomas Gleixner 	early_ioremap_nested++;
433e64c8aa0SThomas Gleixner 	/*
434e64c8aa0SThomas Gleixner 	 * Mappings have to be page-aligned
435e64c8aa0SThomas Gleixner 	 */
436e64c8aa0SThomas Gleixner 	offset = phys_addr & ~PAGE_MASK;
437e64c8aa0SThomas Gleixner 	phys_addr &= PAGE_MASK;
438e64c8aa0SThomas Gleixner 	size = PAGE_ALIGN(last_addr) - phys_addr;
439e64c8aa0SThomas Gleixner 
440e64c8aa0SThomas Gleixner 	/*
441e64c8aa0SThomas Gleixner 	 * Mappings have to fit in the FIX_BTMAP area.
442e64c8aa0SThomas Gleixner 	 */
443e64c8aa0SThomas Gleixner 	nrpages = size >> PAGE_SHIFT;
444e64c8aa0SThomas Gleixner 	if (nrpages > NR_FIX_BTMAPS) {
445e64c8aa0SThomas Gleixner 		WARN_ON(1);
446e64c8aa0SThomas Gleixner 		return NULL;
447e64c8aa0SThomas Gleixner 	}
448e64c8aa0SThomas Gleixner 
449e64c8aa0SThomas Gleixner 	/*
450e64c8aa0SThomas Gleixner 	 * Ok, go for it..
451e64c8aa0SThomas Gleixner 	 */
452e64c8aa0SThomas Gleixner 	idx0 = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*nesting;
453e64c8aa0SThomas Gleixner 	idx = idx0;
454e64c8aa0SThomas Gleixner 	while (nrpages > 0) {
455e64c8aa0SThomas Gleixner 		early_set_fixmap(idx, phys_addr);
456e64c8aa0SThomas Gleixner 		phys_addr += PAGE_SIZE;
457e64c8aa0SThomas Gleixner 		--idx;
458e64c8aa0SThomas Gleixner 		--nrpages;
459e64c8aa0SThomas Gleixner 	}
460e64c8aa0SThomas Gleixner 	if (early_ioremap_debug)
461e64c8aa0SThomas Gleixner 		printk(KERN_CONT "%08lx + %08lx\n", offset, fix_to_virt(idx0));
462e64c8aa0SThomas Gleixner 
463e64c8aa0SThomas Gleixner 	return (void *) (offset + fix_to_virt(idx0));
464e64c8aa0SThomas Gleixner }
465e64c8aa0SThomas Gleixner 
466e64c8aa0SThomas Gleixner void __init early_iounmap(void *addr, unsigned long size)
467e64c8aa0SThomas Gleixner {
468e64c8aa0SThomas Gleixner 	unsigned long virt_addr;
469e64c8aa0SThomas Gleixner 	unsigned long offset;
470e64c8aa0SThomas Gleixner 	unsigned int nrpages;
471e64c8aa0SThomas Gleixner 	enum fixed_addresses idx;
472e64c8aa0SThomas Gleixner 	unsigned int nesting;
473e64c8aa0SThomas Gleixner 
474e64c8aa0SThomas Gleixner 	nesting = --early_ioremap_nested;
475e64c8aa0SThomas Gleixner 	WARN_ON(nesting < 0);
476e64c8aa0SThomas Gleixner 
477e64c8aa0SThomas Gleixner 	if (early_ioremap_debug) {
478adafdf6aSIngo Molnar 		printk(KERN_INFO "early_iounmap(%p, %08lx) [%d]\n", addr,
479e64c8aa0SThomas Gleixner 		       size, nesting);
480e64c8aa0SThomas Gleixner 		dump_stack();
481e64c8aa0SThomas Gleixner 	}
482e64c8aa0SThomas Gleixner 
483e64c8aa0SThomas Gleixner 	virt_addr = (unsigned long)addr;
484e64c8aa0SThomas Gleixner 	if (virt_addr < fix_to_virt(FIX_BTMAP_BEGIN)) {
485e64c8aa0SThomas Gleixner 		WARN_ON(1);
486e64c8aa0SThomas Gleixner 		return;
487e64c8aa0SThomas Gleixner 	}
488e64c8aa0SThomas Gleixner 	offset = virt_addr & ~PAGE_MASK;
489e64c8aa0SThomas Gleixner 	nrpages = PAGE_ALIGN(offset + size - 1) >> PAGE_SHIFT;
490e64c8aa0SThomas Gleixner 
491e64c8aa0SThomas Gleixner 	idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*nesting;
492e64c8aa0SThomas Gleixner 	while (nrpages > 0) {
493e64c8aa0SThomas Gleixner 		early_clear_fixmap(idx);
494e64c8aa0SThomas Gleixner 		--idx;
495e64c8aa0SThomas Gleixner 		--nrpages;
496e64c8aa0SThomas Gleixner 	}
497e64c8aa0SThomas Gleixner }
498e64c8aa0SThomas Gleixner 
499e64c8aa0SThomas Gleixner void __this_fixmap_does_not_exist(void)
500e64c8aa0SThomas Gleixner {
501e64c8aa0SThomas Gleixner 	WARN_ON(1);
502e64c8aa0SThomas Gleixner }
503e64c8aa0SThomas Gleixner 
504e64c8aa0SThomas Gleixner #endif /* CONFIG_X86_32 */
505