xref: /linux/arch/x86/mm/ioremap.c (revision ffa71f33a820d1ab3f2fc5723819ac60fb76080b)
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>
15d61fc448SPekka Paalanen #include <linux/mmiotrace.h>
16e64c8aa0SThomas Gleixner 
17e64c8aa0SThomas Gleixner #include <asm/cacheflush.h>
18e64c8aa0SThomas Gleixner #include <asm/e820.h>
19e64c8aa0SThomas Gleixner #include <asm/fixmap.h>
20e64c8aa0SThomas Gleixner #include <asm/pgtable.h>
21e64c8aa0SThomas Gleixner #include <asm/tlbflush.h>
22f6df72e7SJeremy Fitzhardinge #include <asm/pgalloc.h>
23d7677d40Svenkatesh.pallipadi@intel.com #include <asm/pat.h>
24e64c8aa0SThomas Gleixner 
2578c86e5eSJeremy Fitzhardinge #include "physaddr.h"
26e64c8aa0SThomas Gleixner 
27e64c8aa0SThomas Gleixner /*
28e64c8aa0SThomas Gleixner  * Fix up the linear direct mapping of the kernel to avoid cache attribute
29e64c8aa0SThomas Gleixner  * conflicts.
30e64c8aa0SThomas Gleixner  */
313a96ce8cSvenkatesh.pallipadi@intel.com int ioremap_change_attr(unsigned long vaddr, unsigned long size,
323a96ce8cSvenkatesh.pallipadi@intel.com 			       unsigned long prot_val)
33e64c8aa0SThomas Gleixner {
34d806e5eeSThomas Gleixner 	unsigned long nrpages = size >> PAGE_SHIFT;
3593809be8SHarvey Harrison 	int err;
36e64c8aa0SThomas Gleixner 
373a96ce8cSvenkatesh.pallipadi@intel.com 	switch (prot_val) {
383a96ce8cSvenkatesh.pallipadi@intel.com 	case _PAGE_CACHE_UC:
39d806e5eeSThomas Gleixner 	default:
401219333dSvenkatesh.pallipadi@intel.com 		err = _set_memory_uc(vaddr, nrpages);
41d806e5eeSThomas Gleixner 		break;
42b310f381Svenkatesh.pallipadi@intel.com 	case _PAGE_CACHE_WC:
43b310f381Svenkatesh.pallipadi@intel.com 		err = _set_memory_wc(vaddr, nrpages);
44b310f381Svenkatesh.pallipadi@intel.com 		break;
453a96ce8cSvenkatesh.pallipadi@intel.com 	case _PAGE_CACHE_WB:
461219333dSvenkatesh.pallipadi@intel.com 		err = _set_memory_wb(vaddr, nrpages);
47d806e5eeSThomas Gleixner 		break;
48d806e5eeSThomas Gleixner 	}
49e64c8aa0SThomas Gleixner 
50e64c8aa0SThomas Gleixner 	return err;
51e64c8aa0SThomas Gleixner }
52e64c8aa0SThomas Gleixner 
53e64c8aa0SThomas Gleixner /*
54e64c8aa0SThomas Gleixner  * Remap an arbitrary physical address space into the kernel virtual
55e64c8aa0SThomas Gleixner  * address space. Needed when the kernel wants to access high addresses
56e64c8aa0SThomas Gleixner  * directly.
57e64c8aa0SThomas Gleixner  *
58e64c8aa0SThomas Gleixner  * NOTE! We need to allow non-page-aligned mappings too: we will obviously
59e64c8aa0SThomas Gleixner  * have to convert them into an offset in a page-aligned mapping, but the
60e64c8aa0SThomas Gleixner  * caller shouldn't need to know that small detail.
61e64c8aa0SThomas Gleixner  */
6223016969SChristoph Lameter static void __iomem *__ioremap_caller(resource_size_t phys_addr,
6323016969SChristoph Lameter 		unsigned long size, unsigned long prot_val, void *caller)
64e64c8aa0SThomas Gleixner {
65*ffa71f33SKenji Kaneshige 	unsigned long offset, vaddr;
66*ffa71f33SKenji Kaneshige 	resource_size_t pfn, last_pfn, last_addr;
6787e547feSPekka Paalanen 	const resource_size_t unaligned_phys_addr = phys_addr;
6887e547feSPekka Paalanen 	const unsigned long unaligned_size = size;
69e64c8aa0SThomas Gleixner 	struct vm_struct *area;
70d7677d40Svenkatesh.pallipadi@intel.com 	unsigned long new_prot_val;
71d806e5eeSThomas Gleixner 	pgprot_t prot;
72dee7cbb2SVenki Pallipadi 	int retval;
73d61fc448SPekka Paalanen 	void __iomem *ret_addr;
74e64c8aa0SThomas Gleixner 
75e64c8aa0SThomas Gleixner 	/* Don't allow wraparound or zero size */
76e64c8aa0SThomas Gleixner 	last_addr = phys_addr + size - 1;
77e64c8aa0SThomas Gleixner 	if (!size || last_addr < phys_addr)
78e64c8aa0SThomas Gleixner 		return NULL;
79e64c8aa0SThomas Gleixner 
80e3100c82SThomas Gleixner 	if (!phys_addr_valid(phys_addr)) {
816997ab49Svenkatesh.pallipadi@intel.com 		printk(KERN_WARNING "ioremap: invalid physical address %llx\n",
824c8337acSRandy Dunlap 		       (unsigned long long)phys_addr);
83e3100c82SThomas Gleixner 		WARN_ON_ONCE(1);
84e3100c82SThomas Gleixner 		return NULL;
85e3100c82SThomas Gleixner 	}
86e3100c82SThomas Gleixner 
87e64c8aa0SThomas Gleixner 	/*
88e64c8aa0SThomas Gleixner 	 * Don't remap the low PCI/ISA area, it's always mapped..
89e64c8aa0SThomas Gleixner 	 */
90bcc643dcSAndreas Herrmann 	if (is_ISA_range(phys_addr, last_addr))
91e64c8aa0SThomas Gleixner 		return (__force void __iomem *)phys_to_virt(phys_addr);
92e64c8aa0SThomas Gleixner 
93e64c8aa0SThomas Gleixner 	/*
94379daf62SSuresh Siddha 	 * Check if the request spans more than any BAR in the iomem resource
95379daf62SSuresh Siddha 	 * tree.
96379daf62SSuresh Siddha 	 */
978808500fSIngo Molnar 	WARN_ONCE(iomem_map_sanity_check(phys_addr, size),
988808500fSIngo Molnar 		  KERN_INFO "Info: mapping multiple BARs. Your kernel is fine.");
99379daf62SSuresh Siddha 
100379daf62SSuresh Siddha 	/*
101e64c8aa0SThomas Gleixner 	 * Don't allow anybody to remap normal RAM that we're using..
102e64c8aa0SThomas Gleixner 	 */
103*ffa71f33SKenji Kaneshige 	last_pfn = last_addr >> PAGE_SHIFT;
104*ffa71f33SKenji Kaneshige 	for (pfn = phys_addr >> PAGE_SHIFT; pfn < last_pfn; pfn++) {
105ba748d22SIngo Molnar 		int is_ram = page_is_ram(pfn);
106ba748d22SIngo Molnar 
107ba748d22SIngo Molnar 		if (is_ram && pfn_valid(pfn) && !PageReserved(pfn_to_page(pfn)))
108e64c8aa0SThomas Gleixner 			return NULL;
109ba748d22SIngo Molnar 		WARN_ON_ONCE(is_ram);
110e64c8aa0SThomas Gleixner 	}
111e64c8aa0SThomas Gleixner 
112d7677d40Svenkatesh.pallipadi@intel.com 	/*
113d7677d40Svenkatesh.pallipadi@intel.com 	 * Mappings have to be page-aligned
114d7677d40Svenkatesh.pallipadi@intel.com 	 */
115d7677d40Svenkatesh.pallipadi@intel.com 	offset = phys_addr & ~PAGE_MASK;
116*ffa71f33SKenji Kaneshige 	phys_addr &= PHYSICAL_PAGE_MASK;
117d7677d40Svenkatesh.pallipadi@intel.com 	size = PAGE_ALIGN(last_addr+1) - phys_addr;
118d7677d40Svenkatesh.pallipadi@intel.com 
119e213e877SAndi Kleen 	retval = reserve_memtype(phys_addr, (u64)phys_addr + size,
120dee7cbb2SVenki Pallipadi 						prot_val, &new_prot_val);
121dee7cbb2SVenki Pallipadi 	if (retval) {
122279e669bSVenkatesh Pallipadi 		printk(KERN_ERR "ioremap reserve_memtype failed %d\n", retval);
123dee7cbb2SVenki Pallipadi 		return NULL;
124dee7cbb2SVenki Pallipadi 	}
125dee7cbb2SVenki Pallipadi 
126dee7cbb2SVenki Pallipadi 	if (prot_val != new_prot_val) {
127b855192cSH. Peter Anvin 		if (!is_new_memtype_allowed(phys_addr, size,
128b855192cSH. Peter Anvin 					    prot_val, new_prot_val)) {
129279e669bSVenkatesh Pallipadi 			printk(KERN_ERR
1306997ab49Svenkatesh.pallipadi@intel.com 		"ioremap error for 0x%llx-0x%llx, requested 0x%lx, got 0x%lx\n",
1314c8337acSRandy Dunlap 				(unsigned long long)phys_addr,
1324c8337acSRandy Dunlap 				(unsigned long long)(phys_addr + size),
1336997ab49Svenkatesh.pallipadi@intel.com 				prot_val, new_prot_val);
134de2a47cfSXiaotian Feng 			goto err_free_memtype;
135d7677d40Svenkatesh.pallipadi@intel.com 		}
136d7677d40Svenkatesh.pallipadi@intel.com 		prot_val = new_prot_val;
137d7677d40Svenkatesh.pallipadi@intel.com 	}
138d7677d40Svenkatesh.pallipadi@intel.com 
1393a96ce8cSvenkatesh.pallipadi@intel.com 	switch (prot_val) {
1403a96ce8cSvenkatesh.pallipadi@intel.com 	case _PAGE_CACHE_UC:
141d806e5eeSThomas Gleixner 	default:
142be43d728SJeremy Fitzhardinge 		prot = PAGE_KERNEL_IO_NOCACHE;
143d806e5eeSThomas Gleixner 		break;
144de33c442SSuresh Siddha 	case _PAGE_CACHE_UC_MINUS:
145be43d728SJeremy Fitzhardinge 		prot = PAGE_KERNEL_IO_UC_MINUS;
146de33c442SSuresh Siddha 		break;
147b310f381Svenkatesh.pallipadi@intel.com 	case _PAGE_CACHE_WC:
148be43d728SJeremy Fitzhardinge 		prot = PAGE_KERNEL_IO_WC;
149b310f381Svenkatesh.pallipadi@intel.com 		break;
1503a96ce8cSvenkatesh.pallipadi@intel.com 	case _PAGE_CACHE_WB:
151be43d728SJeremy Fitzhardinge 		prot = PAGE_KERNEL_IO;
152d806e5eeSThomas Gleixner 		break;
153d806e5eeSThomas Gleixner 	}
154e64c8aa0SThomas Gleixner 
155e64c8aa0SThomas Gleixner 	/*
156e64c8aa0SThomas Gleixner 	 * Ok, go for it..
157e64c8aa0SThomas Gleixner 	 */
15823016969SChristoph Lameter 	area = get_vm_area_caller(size, VM_IOREMAP, caller);
159e64c8aa0SThomas Gleixner 	if (!area)
160de2a47cfSXiaotian Feng 		goto err_free_memtype;
161e64c8aa0SThomas Gleixner 	area->phys_addr = phys_addr;
162e66aadbeSThomas Gleixner 	vaddr = (unsigned long) area->addr;
16343a432b1SSuresh Siddha 
164de2a47cfSXiaotian Feng 	if (kernel_map_sync_memtype(phys_addr, size, prot_val))
165de2a47cfSXiaotian Feng 		goto err_free_area;
166e64c8aa0SThomas Gleixner 
167de2a47cfSXiaotian Feng 	if (ioremap_page_range(vaddr, vaddr + size, phys_addr, prot))
168de2a47cfSXiaotian Feng 		goto err_free_area;
169e64c8aa0SThomas Gleixner 
170d61fc448SPekka Paalanen 	ret_addr = (void __iomem *) (vaddr + offset);
17187e547feSPekka Paalanen 	mmiotrace_ioremap(unaligned_phys_addr, unaligned_size, ret_addr);
172d61fc448SPekka Paalanen 
173d61fc448SPekka Paalanen 	return ret_addr;
174de2a47cfSXiaotian Feng err_free_area:
175de2a47cfSXiaotian Feng 	free_vm_area(area);
176de2a47cfSXiaotian Feng err_free_memtype:
177de2a47cfSXiaotian Feng 	free_memtype(phys_addr, phys_addr + size);
178de2a47cfSXiaotian Feng 	return NULL;
179e64c8aa0SThomas Gleixner }
180e64c8aa0SThomas Gleixner 
181e64c8aa0SThomas Gleixner /**
182e64c8aa0SThomas Gleixner  * ioremap_nocache     -   map bus memory into CPU space
183e64c8aa0SThomas Gleixner  * @offset:    bus address of the memory
184e64c8aa0SThomas Gleixner  * @size:      size of the resource to map
185e64c8aa0SThomas Gleixner  *
186e64c8aa0SThomas Gleixner  * ioremap_nocache performs a platform specific sequence of operations to
187e64c8aa0SThomas Gleixner  * make bus memory CPU accessible via the readb/readw/readl/writeb/
188e64c8aa0SThomas Gleixner  * writew/writel functions and the other mmio helpers. The returned
189e64c8aa0SThomas Gleixner  * address is not guaranteed to be usable directly as a virtual
190e64c8aa0SThomas Gleixner  * address.
191e64c8aa0SThomas Gleixner  *
192e64c8aa0SThomas Gleixner  * This version of ioremap ensures that the memory is marked uncachable
193e64c8aa0SThomas Gleixner  * on the CPU as well as honouring existing caching rules from things like
194e64c8aa0SThomas Gleixner  * the PCI bus. Note that there are other caches and buffers on many
195e64c8aa0SThomas Gleixner  * busses. In particular driver authors should read up on PCI writes
196e64c8aa0SThomas Gleixner  *
197e64c8aa0SThomas Gleixner  * It's useful if some control registers are in such an area and
198e64c8aa0SThomas Gleixner  * write combining or read caching is not desirable:
199e64c8aa0SThomas Gleixner  *
200e64c8aa0SThomas Gleixner  * Must be freed with iounmap.
201e64c8aa0SThomas Gleixner  */
202b9e76a00SLinus Torvalds void __iomem *ioremap_nocache(resource_size_t phys_addr, unsigned long size)
203e64c8aa0SThomas Gleixner {
204de33c442SSuresh Siddha 	/*
205de33c442SSuresh Siddha 	 * Ideally, this should be:
206499f8f84SAndreas Herrmann 	 *	pat_enabled ? _PAGE_CACHE_UC : _PAGE_CACHE_UC_MINUS;
207de33c442SSuresh Siddha 	 *
208de33c442SSuresh Siddha 	 * Till we fix all X drivers to use ioremap_wc(), we will use
209de33c442SSuresh Siddha 	 * UC MINUS.
210de33c442SSuresh Siddha 	 */
211de33c442SSuresh Siddha 	unsigned long val = _PAGE_CACHE_UC_MINUS;
212de33c442SSuresh Siddha 
213de33c442SSuresh Siddha 	return __ioremap_caller(phys_addr, size, val,
21423016969SChristoph Lameter 				__builtin_return_address(0));
215e64c8aa0SThomas Gleixner }
216e64c8aa0SThomas Gleixner EXPORT_SYMBOL(ioremap_nocache);
217e64c8aa0SThomas Gleixner 
218b310f381Svenkatesh.pallipadi@intel.com /**
219b310f381Svenkatesh.pallipadi@intel.com  * ioremap_wc	-	map memory into CPU space write combined
220b310f381Svenkatesh.pallipadi@intel.com  * @offset:	bus address of the memory
221b310f381Svenkatesh.pallipadi@intel.com  * @size:	size of the resource to map
222b310f381Svenkatesh.pallipadi@intel.com  *
223b310f381Svenkatesh.pallipadi@intel.com  * This version of ioremap ensures that the memory is marked write combining.
224b310f381Svenkatesh.pallipadi@intel.com  * Write combining allows faster writes to some hardware devices.
225b310f381Svenkatesh.pallipadi@intel.com  *
226b310f381Svenkatesh.pallipadi@intel.com  * Must be freed with iounmap.
227b310f381Svenkatesh.pallipadi@intel.com  */
228d639bab8Svenkatesh.pallipadi@intel.com void __iomem *ioremap_wc(resource_size_t phys_addr, unsigned long size)
229b310f381Svenkatesh.pallipadi@intel.com {
230499f8f84SAndreas Herrmann 	if (pat_enabled)
23123016969SChristoph Lameter 		return __ioremap_caller(phys_addr, size, _PAGE_CACHE_WC,
23223016969SChristoph Lameter 					__builtin_return_address(0));
233b310f381Svenkatesh.pallipadi@intel.com 	else
234b310f381Svenkatesh.pallipadi@intel.com 		return ioremap_nocache(phys_addr, size);
235b310f381Svenkatesh.pallipadi@intel.com }
236b310f381Svenkatesh.pallipadi@intel.com EXPORT_SYMBOL(ioremap_wc);
237b310f381Svenkatesh.pallipadi@intel.com 
238b9e76a00SLinus Torvalds void __iomem *ioremap_cache(resource_size_t phys_addr, unsigned long size)
2395f868152SThomas Gleixner {
24023016969SChristoph Lameter 	return __ioremap_caller(phys_addr, size, _PAGE_CACHE_WB,
24123016969SChristoph Lameter 				__builtin_return_address(0));
2425f868152SThomas Gleixner }
2435f868152SThomas Gleixner EXPORT_SYMBOL(ioremap_cache);
2445f868152SThomas Gleixner 
24528b2ee20SRik van Riel void __iomem *ioremap_prot(resource_size_t phys_addr, unsigned long size,
24628b2ee20SRik van Riel 				unsigned long prot_val)
24728b2ee20SRik van Riel {
24828b2ee20SRik van Riel 	return __ioremap_caller(phys_addr, size, (prot_val & _PAGE_CACHE_MASK),
24928b2ee20SRik van Riel 				__builtin_return_address(0));
25028b2ee20SRik van Riel }
25128b2ee20SRik van Riel EXPORT_SYMBOL(ioremap_prot);
25228b2ee20SRik van Riel 
253e64c8aa0SThomas Gleixner /**
254e64c8aa0SThomas Gleixner  * iounmap - Free a IO remapping
255e64c8aa0SThomas Gleixner  * @addr: virtual address from ioremap_*
256e64c8aa0SThomas Gleixner  *
257e64c8aa0SThomas Gleixner  * Caller must ensure there is only one unmapping for the same pointer.
258e64c8aa0SThomas Gleixner  */
259e64c8aa0SThomas Gleixner void iounmap(volatile void __iomem *addr)
260e64c8aa0SThomas Gleixner {
261e64c8aa0SThomas Gleixner 	struct vm_struct *p, *o;
262e64c8aa0SThomas Gleixner 
263e64c8aa0SThomas Gleixner 	if ((void __force *)addr <= high_memory)
264e64c8aa0SThomas Gleixner 		return;
265e64c8aa0SThomas Gleixner 
266e64c8aa0SThomas Gleixner 	/*
267e64c8aa0SThomas Gleixner 	 * __ioremap special-cases the PCI/ISA range by not instantiating a
268e64c8aa0SThomas Gleixner 	 * vm_area and by simply returning an address into the kernel mapping
269e64c8aa0SThomas Gleixner 	 * of ISA space.   So handle that here.
270e64c8aa0SThomas Gleixner 	 */
2716e92a5a6SThomas Gleixner 	if ((void __force *)addr >= phys_to_virt(ISA_START_ADDRESS) &&
2726e92a5a6SThomas Gleixner 	    (void __force *)addr < phys_to_virt(ISA_END_ADDRESS))
273e64c8aa0SThomas Gleixner 		return;
274e64c8aa0SThomas Gleixner 
275e64c8aa0SThomas Gleixner 	addr = (volatile void __iomem *)
276e64c8aa0SThomas Gleixner 		(PAGE_MASK & (unsigned long __force)addr);
277e64c8aa0SThomas Gleixner 
278d61fc448SPekka Paalanen 	mmiotrace_iounmap(addr);
279d61fc448SPekka Paalanen 
280e64c8aa0SThomas Gleixner 	/* Use the vm area unlocked, assuming the caller
281e64c8aa0SThomas Gleixner 	   ensures there isn't another iounmap for the same address
282e64c8aa0SThomas Gleixner 	   in parallel. Reuse of the virtual address is prevented by
283e64c8aa0SThomas Gleixner 	   leaving it in the global lists until we're done with it.
284e64c8aa0SThomas Gleixner 	   cpa takes care of the direct mappings. */
285e64c8aa0SThomas Gleixner 	read_lock(&vmlist_lock);
286e64c8aa0SThomas Gleixner 	for (p = vmlist; p; p = p->next) {
2876e92a5a6SThomas Gleixner 		if (p->addr == (void __force *)addr)
288e64c8aa0SThomas Gleixner 			break;
289e64c8aa0SThomas Gleixner 	}
290e64c8aa0SThomas Gleixner 	read_unlock(&vmlist_lock);
291e64c8aa0SThomas Gleixner 
292e64c8aa0SThomas Gleixner 	if (!p) {
293e64c8aa0SThomas Gleixner 		printk(KERN_ERR "iounmap: bad address %p\n", addr);
294e64c8aa0SThomas Gleixner 		dump_stack();
295e64c8aa0SThomas Gleixner 		return;
296e64c8aa0SThomas Gleixner 	}
297e64c8aa0SThomas Gleixner 
298d7677d40Svenkatesh.pallipadi@intel.com 	free_memtype(p->phys_addr, p->phys_addr + get_vm_area_size(p));
299d7677d40Svenkatesh.pallipadi@intel.com 
300e64c8aa0SThomas Gleixner 	/* Finally remove it */
3016e92a5a6SThomas Gleixner 	o = remove_vm_area((void __force *)addr);
302e64c8aa0SThomas Gleixner 	BUG_ON(p != o || o == NULL);
303e64c8aa0SThomas Gleixner 	kfree(p);
304e64c8aa0SThomas Gleixner }
305e64c8aa0SThomas Gleixner EXPORT_SYMBOL(iounmap);
306e64c8aa0SThomas Gleixner 
307e045fb2aSvenkatesh.pallipadi@intel.com /*
308e045fb2aSvenkatesh.pallipadi@intel.com  * Convert a physical pointer to a virtual kernel pointer for /dev/mem
309e045fb2aSvenkatesh.pallipadi@intel.com  * access
310e045fb2aSvenkatesh.pallipadi@intel.com  */
311e045fb2aSvenkatesh.pallipadi@intel.com void *xlate_dev_mem_ptr(unsigned long phys)
312e045fb2aSvenkatesh.pallipadi@intel.com {
313e045fb2aSvenkatesh.pallipadi@intel.com 	void *addr;
314e045fb2aSvenkatesh.pallipadi@intel.com 	unsigned long start = phys & PAGE_MASK;
315e045fb2aSvenkatesh.pallipadi@intel.com 
316e045fb2aSvenkatesh.pallipadi@intel.com 	/* If page is RAM, we can use __va. Otherwise ioremap and unmap. */
317e045fb2aSvenkatesh.pallipadi@intel.com 	if (page_is_ram(start >> PAGE_SHIFT))
318e045fb2aSvenkatesh.pallipadi@intel.com 		return __va(phys);
319e045fb2aSvenkatesh.pallipadi@intel.com 
3202fb8f4e6SXiaotian Feng 	addr = (void __force *)ioremap_cache(start, PAGE_SIZE);
321e045fb2aSvenkatesh.pallipadi@intel.com 	if (addr)
322e045fb2aSvenkatesh.pallipadi@intel.com 		addr = (void *)((unsigned long)addr | (phys & ~PAGE_MASK));
323e045fb2aSvenkatesh.pallipadi@intel.com 
324e045fb2aSvenkatesh.pallipadi@intel.com 	return addr;
325e045fb2aSvenkatesh.pallipadi@intel.com }
326e045fb2aSvenkatesh.pallipadi@intel.com 
327e045fb2aSvenkatesh.pallipadi@intel.com void unxlate_dev_mem_ptr(unsigned long phys, void *addr)
328e045fb2aSvenkatesh.pallipadi@intel.com {
329e045fb2aSvenkatesh.pallipadi@intel.com 	if (page_is_ram(phys >> PAGE_SHIFT))
330e045fb2aSvenkatesh.pallipadi@intel.com 		return;
331e045fb2aSvenkatesh.pallipadi@intel.com 
332e045fb2aSvenkatesh.pallipadi@intel.com 	iounmap((void __iomem *)((unsigned long)addr & PAGE_MASK));
333e045fb2aSvenkatesh.pallipadi@intel.com 	return;
334e045fb2aSvenkatesh.pallipadi@intel.com }
335e045fb2aSvenkatesh.pallipadi@intel.com 
3364b6e9f27SJaswinder Singh static int __initdata early_ioremap_debug;
337e64c8aa0SThomas Gleixner 
338e64c8aa0SThomas Gleixner static int __init early_ioremap_debug_setup(char *str)
339e64c8aa0SThomas Gleixner {
340e64c8aa0SThomas Gleixner 	early_ioremap_debug = 1;
341e64c8aa0SThomas Gleixner 
342e64c8aa0SThomas Gleixner 	return 0;
343e64c8aa0SThomas Gleixner }
344e64c8aa0SThomas Gleixner early_param("early_ioremap_debug", early_ioremap_debug_setup);
345e64c8aa0SThomas Gleixner 
346e64c8aa0SThomas Gleixner static __initdata int after_paging_init;
34745c7b28fSJeremy Fitzhardinge static pte_t bm_pte[PAGE_SIZE/sizeof(pte_t)] __page_aligned_bss;
348e64c8aa0SThomas Gleixner 
349551889a6SIan Campbell static inline pmd_t * __init early_ioremap_pmd(unsigned long addr)
350e64c8aa0SThomas Gleixner {
35137cc8d7fSJeremy Fitzhardinge 	/* Don't assume we're using swapper_pg_dir at this point */
35237cc8d7fSJeremy Fitzhardinge 	pgd_t *base = __va(read_cr3());
35337cc8d7fSJeremy Fitzhardinge 	pgd_t *pgd = &base[pgd_index(addr)];
354551889a6SIan Campbell 	pud_t *pud = pud_offset(pgd, addr);
355551889a6SIan Campbell 	pmd_t *pmd = pmd_offset(pud, addr);
356551889a6SIan Campbell 
357551889a6SIan Campbell 	return pmd;
358e64c8aa0SThomas Gleixner }
359e64c8aa0SThomas Gleixner 
360551889a6SIan Campbell static inline pte_t * __init early_ioremap_pte(unsigned long addr)
361e64c8aa0SThomas Gleixner {
362551889a6SIan Campbell 	return &bm_pte[pte_index(addr)];
363e64c8aa0SThomas Gleixner }
364e64c8aa0SThomas Gleixner 
3658827247fSWang Chen static unsigned long slot_virt[FIX_BTMAPS_SLOTS] __initdata;
3668827247fSWang Chen 
367e64c8aa0SThomas Gleixner void __init early_ioremap_init(void)
368e64c8aa0SThomas Gleixner {
369551889a6SIan Campbell 	pmd_t *pmd;
3708827247fSWang Chen 	int i;
371e64c8aa0SThomas Gleixner 
372e64c8aa0SThomas Gleixner 	if (early_ioremap_debug)
373adafdf6aSIngo Molnar 		printk(KERN_INFO "early_ioremap_init()\n");
374e64c8aa0SThomas Gleixner 
3758827247fSWang Chen 	for (i = 0; i < FIX_BTMAPS_SLOTS; i++)
3769f4f25c8SWang Chen 		slot_virt[i] = __fix_to_virt(FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*i);
3778827247fSWang Chen 
378551889a6SIan Campbell 	pmd = early_ioremap_pmd(fix_to_virt(FIX_BTMAP_BEGIN));
379e64c8aa0SThomas Gleixner 	memset(bm_pte, 0, sizeof(bm_pte));
380b6fbb669SIan Campbell 	pmd_populate_kernel(&init_mm, pmd, bm_pte);
381551889a6SIan Campbell 
382e64c8aa0SThomas Gleixner 	/*
383551889a6SIan Campbell 	 * The boot-ioremap range spans multiple pmds, for which
384e64c8aa0SThomas Gleixner 	 * we are not prepared:
385e64c8aa0SThomas Gleixner 	 */
386499a5f1eSJan Beulich #define __FIXADDR_TOP (-PAGE_SIZE)
387499a5f1eSJan Beulich 	BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
388499a5f1eSJan Beulich 		     != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
389499a5f1eSJan Beulich #undef __FIXADDR_TOP
390551889a6SIan Campbell 	if (pmd != early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END))) {
391e64c8aa0SThomas Gleixner 		WARN_ON(1);
392551889a6SIan Campbell 		printk(KERN_WARNING "pmd %p != %p\n",
393551889a6SIan Campbell 		       pmd, early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END)));
394e64c8aa0SThomas Gleixner 		printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
395e64c8aa0SThomas Gleixner 			fix_to_virt(FIX_BTMAP_BEGIN));
396e64c8aa0SThomas Gleixner 		printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_END):   %08lx\n",
397e64c8aa0SThomas Gleixner 			fix_to_virt(FIX_BTMAP_END));
398e64c8aa0SThomas Gleixner 
399e64c8aa0SThomas Gleixner 		printk(KERN_WARNING "FIX_BTMAP_END:       %d\n", FIX_BTMAP_END);
400e64c8aa0SThomas Gleixner 		printk(KERN_WARNING "FIX_BTMAP_BEGIN:     %d\n",
401e64c8aa0SThomas Gleixner 		       FIX_BTMAP_BEGIN);
402e64c8aa0SThomas Gleixner 	}
403e64c8aa0SThomas Gleixner }
404e64c8aa0SThomas Gleixner 
405e64c8aa0SThomas Gleixner void __init early_ioremap_reset(void)
406e64c8aa0SThomas Gleixner {
407e64c8aa0SThomas Gleixner 	after_paging_init = 1;
408e64c8aa0SThomas Gleixner }
409e64c8aa0SThomas Gleixner 
410e64c8aa0SThomas Gleixner static void __init __early_set_fixmap(enum fixed_addresses idx,
4119b987aebSMasami Hiramatsu 				      phys_addr_t phys, pgprot_t flags)
412e64c8aa0SThomas Gleixner {
413551889a6SIan Campbell 	unsigned long addr = __fix_to_virt(idx);
414551889a6SIan Campbell 	pte_t *pte;
415e64c8aa0SThomas Gleixner 
416e64c8aa0SThomas Gleixner 	if (idx >= __end_of_fixed_addresses) {
417e64c8aa0SThomas Gleixner 		BUG();
418e64c8aa0SThomas Gleixner 		return;
419e64c8aa0SThomas Gleixner 	}
420e64c8aa0SThomas Gleixner 	pte = early_ioremap_pte(addr);
4214583ed51SJeremy Fitzhardinge 
422e64c8aa0SThomas Gleixner 	if (pgprot_val(flags))
423551889a6SIan Campbell 		set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
424e64c8aa0SThomas Gleixner 	else
4254f9c11ddSJeremy Fitzhardinge 		pte_clear(&init_mm, addr, pte);
426e64c8aa0SThomas Gleixner 	__flush_tlb_one(addr);
427e64c8aa0SThomas Gleixner }
428e64c8aa0SThomas Gleixner 
429e64c8aa0SThomas Gleixner static inline void __init early_set_fixmap(enum fixed_addresses idx,
4309b987aebSMasami Hiramatsu 					   phys_addr_t phys, pgprot_t prot)
431e64c8aa0SThomas Gleixner {
432e64c8aa0SThomas Gleixner 	if (after_paging_init)
43314941779SJeremy Fitzhardinge 		__set_fixmap(idx, phys, prot);
434e64c8aa0SThomas Gleixner 	else
43514941779SJeremy Fitzhardinge 		__early_set_fixmap(idx, phys, prot);
436e64c8aa0SThomas Gleixner }
437e64c8aa0SThomas Gleixner 
438e64c8aa0SThomas Gleixner static inline void __init early_clear_fixmap(enum fixed_addresses idx)
439e64c8aa0SThomas Gleixner {
440e64c8aa0SThomas Gleixner 	if (after_paging_init)
441e64c8aa0SThomas Gleixner 		clear_fixmap(idx);
442e64c8aa0SThomas Gleixner 	else
443e64c8aa0SThomas Gleixner 		__early_set_fixmap(idx, 0, __pgprot(0));
444e64c8aa0SThomas Gleixner }
445e64c8aa0SThomas Gleixner 
4461d6cf1feSHarvey Harrison static void __iomem *prev_map[FIX_BTMAPS_SLOTS] __initdata;
447c1a2f4b1SYinghai Lu static unsigned long prev_size[FIX_BTMAPS_SLOTS] __initdata;
4488827247fSWang Chen 
449e67a807fSLiang Li void __init fixup_early_ioremap(void)
450e67a807fSLiang Li {
451e67a807fSLiang Li 	int i;
452e67a807fSLiang Li 
453e67a807fSLiang Li 	for (i = 0; i < FIX_BTMAPS_SLOTS; i++) {
454e67a807fSLiang Li 		if (prev_map[i]) {
455e67a807fSLiang Li 			WARN_ON(1);
456e67a807fSLiang Li 			break;
457e67a807fSLiang Li 		}
458e67a807fSLiang Li 	}
459e67a807fSLiang Li 
460e67a807fSLiang Li 	early_ioremap_init();
461e67a807fSLiang Li }
462e67a807fSLiang Li 
463e64c8aa0SThomas Gleixner static int __init check_early_ioremap_leak(void)
464e64c8aa0SThomas Gleixner {
465c1a2f4b1SYinghai Lu 	int count = 0;
466c1a2f4b1SYinghai Lu 	int i;
467c1a2f4b1SYinghai Lu 
468c1a2f4b1SYinghai Lu 	for (i = 0; i < FIX_BTMAPS_SLOTS; i++)
469c1a2f4b1SYinghai Lu 		if (prev_map[i])
470c1a2f4b1SYinghai Lu 			count++;
471c1a2f4b1SYinghai Lu 
472c1a2f4b1SYinghai Lu 	if (!count)
473e64c8aa0SThomas Gleixner 		return 0;
4740c072bb4SArjan van de Ven 	WARN(1, KERN_WARNING
475e64c8aa0SThomas Gleixner 	       "Debug warning: early ioremap leak of %d areas detected.\n",
476c1a2f4b1SYinghai Lu 		count);
477e64c8aa0SThomas Gleixner 	printk(KERN_WARNING
478e64c8aa0SThomas Gleixner 		"please boot with early_ioremap_debug and report the dmesg.\n");
479e64c8aa0SThomas Gleixner 
480e64c8aa0SThomas Gleixner 	return 1;
481e64c8aa0SThomas Gleixner }
482e64c8aa0SThomas Gleixner late_initcall(check_early_ioremap_leak);
483e64c8aa0SThomas Gleixner 
4848827247fSWang Chen static void __init __iomem *
4859b987aebSMasami Hiramatsu __early_ioremap(resource_size_t phys_addr, unsigned long size, pgprot_t prot)
486e64c8aa0SThomas Gleixner {
4879b987aebSMasami Hiramatsu 	unsigned long offset;
4889b987aebSMasami Hiramatsu 	resource_size_t last_addr;
489c1a2f4b1SYinghai Lu 	unsigned int nrpages;
490e64c8aa0SThomas Gleixner 	enum fixed_addresses idx0, idx;
491c1a2f4b1SYinghai Lu 	int i, slot;
492e64c8aa0SThomas Gleixner 
493e64c8aa0SThomas Gleixner 	WARN_ON(system_state != SYSTEM_BOOTING);
494e64c8aa0SThomas Gleixner 
495c1a2f4b1SYinghai Lu 	slot = -1;
496c1a2f4b1SYinghai Lu 	for (i = 0; i < FIX_BTMAPS_SLOTS; i++) {
497c1a2f4b1SYinghai Lu 		if (!prev_map[i]) {
498c1a2f4b1SYinghai Lu 			slot = i;
499c1a2f4b1SYinghai Lu 			break;
500c1a2f4b1SYinghai Lu 		}
501c1a2f4b1SYinghai Lu 	}
502c1a2f4b1SYinghai Lu 
503c1a2f4b1SYinghai Lu 	if (slot < 0) {
5049b987aebSMasami Hiramatsu 		printk(KERN_INFO "early_iomap(%08llx, %08lx) not found slot\n",
5059b987aebSMasami Hiramatsu 			 (u64)phys_addr, size);
506c1a2f4b1SYinghai Lu 		WARN_ON(1);
507c1a2f4b1SYinghai Lu 		return NULL;
508c1a2f4b1SYinghai Lu 	}
509c1a2f4b1SYinghai Lu 
510e64c8aa0SThomas Gleixner 	if (early_ioremap_debug) {
5119b987aebSMasami Hiramatsu 		printk(KERN_INFO "early_ioremap(%08llx, %08lx) [%d] => ",
5129b987aebSMasami Hiramatsu 		       (u64)phys_addr, size, slot);
513e64c8aa0SThomas Gleixner 		dump_stack();
514e64c8aa0SThomas Gleixner 	}
515e64c8aa0SThomas Gleixner 
516e64c8aa0SThomas Gleixner 	/* Don't allow wraparound or zero size */
517e64c8aa0SThomas Gleixner 	last_addr = phys_addr + size - 1;
518e64c8aa0SThomas Gleixner 	if (!size || last_addr < phys_addr) {
519e64c8aa0SThomas Gleixner 		WARN_ON(1);
520e64c8aa0SThomas Gleixner 		return NULL;
521e64c8aa0SThomas Gleixner 	}
522e64c8aa0SThomas Gleixner 
523c1a2f4b1SYinghai Lu 	prev_size[slot] = size;
524e64c8aa0SThomas Gleixner 	/*
525e64c8aa0SThomas Gleixner 	 * Mappings have to be page-aligned
526e64c8aa0SThomas Gleixner 	 */
527e64c8aa0SThomas Gleixner 	offset = phys_addr & ~PAGE_MASK;
528e64c8aa0SThomas Gleixner 	phys_addr &= PAGE_MASK;
529c613ec1aSAlan Cox 	size = PAGE_ALIGN(last_addr + 1) - phys_addr;
530e64c8aa0SThomas Gleixner 
531e64c8aa0SThomas Gleixner 	/*
532e64c8aa0SThomas Gleixner 	 * Mappings have to fit in the FIX_BTMAP area.
533e64c8aa0SThomas Gleixner 	 */
534e64c8aa0SThomas Gleixner 	nrpages = size >> PAGE_SHIFT;
535e64c8aa0SThomas Gleixner 	if (nrpages > NR_FIX_BTMAPS) {
536e64c8aa0SThomas Gleixner 		WARN_ON(1);
537e64c8aa0SThomas Gleixner 		return NULL;
538e64c8aa0SThomas Gleixner 	}
539e64c8aa0SThomas Gleixner 
540e64c8aa0SThomas Gleixner 	/*
541e64c8aa0SThomas Gleixner 	 * Ok, go for it..
542e64c8aa0SThomas Gleixner 	 */
543c1a2f4b1SYinghai Lu 	idx0 = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*slot;
544e64c8aa0SThomas Gleixner 	idx = idx0;
545e64c8aa0SThomas Gleixner 	while (nrpages > 0) {
54614941779SJeremy Fitzhardinge 		early_set_fixmap(idx, phys_addr, prot);
547e64c8aa0SThomas Gleixner 		phys_addr += PAGE_SIZE;
548e64c8aa0SThomas Gleixner 		--idx;
549e64c8aa0SThomas Gleixner 		--nrpages;
550e64c8aa0SThomas Gleixner 	}
551e64c8aa0SThomas Gleixner 	if (early_ioremap_debug)
5528827247fSWang Chen 		printk(KERN_CONT "%08lx + %08lx\n", offset, slot_virt[slot]);
553e64c8aa0SThomas Gleixner 
5548827247fSWang Chen 	prev_map[slot] = (void __iomem *)(offset + slot_virt[slot]);
555c1a2f4b1SYinghai Lu 	return prev_map[slot];
556e64c8aa0SThomas Gleixner }
557e64c8aa0SThomas Gleixner 
55814941779SJeremy Fitzhardinge /* Remap an IO device */
5599b987aebSMasami Hiramatsu void __init __iomem *
5609b987aebSMasami Hiramatsu early_ioremap(resource_size_t phys_addr, unsigned long size)
56114941779SJeremy Fitzhardinge {
56214941779SJeremy Fitzhardinge 	return __early_ioremap(phys_addr, size, PAGE_KERNEL_IO);
56314941779SJeremy Fitzhardinge }
56414941779SJeremy Fitzhardinge 
56514941779SJeremy Fitzhardinge /* Remap memory */
5669b987aebSMasami Hiramatsu void __init __iomem *
5679b987aebSMasami Hiramatsu early_memremap(resource_size_t phys_addr, unsigned long size)
56814941779SJeremy Fitzhardinge {
56914941779SJeremy Fitzhardinge 	return __early_ioremap(phys_addr, size, PAGE_KERNEL);
57014941779SJeremy Fitzhardinge }
57114941779SJeremy Fitzhardinge 
5721d6cf1feSHarvey Harrison void __init early_iounmap(void __iomem *addr, unsigned long size)
573e64c8aa0SThomas Gleixner {
574e64c8aa0SThomas Gleixner 	unsigned long virt_addr;
575e64c8aa0SThomas Gleixner 	unsigned long offset;
576e64c8aa0SThomas Gleixner 	unsigned int nrpages;
577e64c8aa0SThomas Gleixner 	enum fixed_addresses idx;
578c1a2f4b1SYinghai Lu 	int i, slot;
579e64c8aa0SThomas Gleixner 
580c1a2f4b1SYinghai Lu 	slot = -1;
581c1a2f4b1SYinghai Lu 	for (i = 0; i < FIX_BTMAPS_SLOTS; i++) {
582c1a2f4b1SYinghai Lu 		if (prev_map[i] == addr) {
583c1a2f4b1SYinghai Lu 			slot = i;
584c1a2f4b1SYinghai Lu 			break;
585c1a2f4b1SYinghai Lu 		}
586c1a2f4b1SYinghai Lu 	}
587c1a2f4b1SYinghai Lu 
588c1a2f4b1SYinghai Lu 	if (slot < 0) {
589c1a2f4b1SYinghai Lu 		printk(KERN_INFO "early_iounmap(%p, %08lx) not found slot\n",
590c1a2f4b1SYinghai Lu 			 addr, size);
591c1a2f4b1SYinghai Lu 		WARN_ON(1);
592226e9a93SIngo Molnar 		return;
593c1a2f4b1SYinghai Lu 	}
594c1a2f4b1SYinghai Lu 
595c1a2f4b1SYinghai Lu 	if (prev_size[slot] != size) {
596c1a2f4b1SYinghai Lu 		printk(KERN_INFO "early_iounmap(%p, %08lx) [%d] size not consistent %08lx\n",
597c1a2f4b1SYinghai Lu 			 addr, size, slot, prev_size[slot]);
598c1a2f4b1SYinghai Lu 		WARN_ON(1);
599c1a2f4b1SYinghai Lu 		return;
600c1a2f4b1SYinghai Lu 	}
601e64c8aa0SThomas Gleixner 
602e64c8aa0SThomas Gleixner 	if (early_ioremap_debug) {
603adafdf6aSIngo Molnar 		printk(KERN_INFO "early_iounmap(%p, %08lx) [%d]\n", addr,
604c1a2f4b1SYinghai Lu 		       size, slot);
605e64c8aa0SThomas Gleixner 		dump_stack();
606e64c8aa0SThomas Gleixner 	}
607e64c8aa0SThomas Gleixner 
608e64c8aa0SThomas Gleixner 	virt_addr = (unsigned long)addr;
609e64c8aa0SThomas Gleixner 	if (virt_addr < fix_to_virt(FIX_BTMAP_BEGIN)) {
610e64c8aa0SThomas Gleixner 		WARN_ON(1);
611e64c8aa0SThomas Gleixner 		return;
612e64c8aa0SThomas Gleixner 	}
613e64c8aa0SThomas Gleixner 	offset = virt_addr & ~PAGE_MASK;
614e64c8aa0SThomas Gleixner 	nrpages = PAGE_ALIGN(offset + size - 1) >> PAGE_SHIFT;
615e64c8aa0SThomas Gleixner 
616c1a2f4b1SYinghai Lu 	idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*slot;
617e64c8aa0SThomas Gleixner 	while (nrpages > 0) {
618e64c8aa0SThomas Gleixner 		early_clear_fixmap(idx);
619e64c8aa0SThomas Gleixner 		--idx;
620e64c8aa0SThomas Gleixner 		--nrpages;
621e64c8aa0SThomas Gleixner 	}
6221d6cf1feSHarvey Harrison 	prev_map[slot] = NULL;
623e64c8aa0SThomas Gleixner }
624