blob: ecae9ac216fad03dc484470bbb99024c01d182ad [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/slab.h>
13#include <linux/vmalloc.h>
Pekka Paalanend61fc442008-05-12 21:20:57 +020014#include <linux/mmiotrace.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>
venkatesh.pallipadi@intel.comd7677d42008-03-18 17:00:17 -070022#include <asm/pat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Jeremy Fitzhardinge78c86e52009-09-10 10:09:38 -070024#include "physaddr.h"
Thomas Gleixner240d3a72008-01-30 13:34:05 +010025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026/*
Thomas Gleixnere9332ca2008-01-30 13:34:05 +010027 * Fix up the linear direct mapping of the kernel to avoid cache attribute
28 * conflicts.
29 */
venkatesh.pallipadi@intel.com3a96ce82008-03-18 17:00:16 -070030int ioremap_change_attr(unsigned long vaddr, unsigned long size,
Juergen Grossb14097b2014-11-03 14:01:58 +010031 enum page_cache_mode pcm)
Thomas Gleixnere9332ca2008-01-30 13:34:05 +010032{
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +010033 unsigned long nrpages = size >> PAGE_SHIFT;
Harvey Harrison93809be2008-02-01 17:49:43 +010034 int err;
Thomas Gleixnere9332ca2008-01-30 13:34:05 +010035
Juergen Grossb14097b2014-11-03 14:01:58 +010036 switch (pcm) {
37 case _PAGE_CACHE_MODE_UC:
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +010038 default:
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -070039 err = _set_memory_uc(vaddr, nrpages);
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +010040 break;
Juergen Grossb14097b2014-11-03 14:01:58 +010041 case _PAGE_CACHE_MODE_WC:
venkatesh.pallipadi@intel.comb310f381d2008-03-18 17:00:24 -070042 err = _set_memory_wc(vaddr, nrpages);
43 break;
Toshi Kani623dffb2015-06-04 18:55:20 +020044 case _PAGE_CACHE_MODE_WT:
45 err = _set_memory_wt(vaddr, nrpages);
46 break;
Juergen Grossb14097b2014-11-03 14:01:58 +010047 case _PAGE_CACHE_MODE_WB:
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -070048 err = _set_memory_wb(vaddr, nrpages);
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +010049 break;
50 }
Thomas Gleixnere9332ca2008-01-30 13:34:05 +010051
52 return err;
53}
54
Roland Dreierc81c8a12014-05-02 11:18:41 -070055static int __ioremap_check_ram(unsigned long start_pfn, unsigned long nr_pages,
56 void *arg)
57{
58 unsigned long i;
59
60 for (i = 0; i < nr_pages; ++i)
61 if (pfn_valid(start_pfn + i) &&
62 !PageReserved(pfn_to_page(start_pfn + i)))
63 return 1;
64
Roland Dreierc81c8a12014-05-02 11:18:41 -070065 return 0;
66}
67
Thomas Gleixnere9332ca2008-01-30 13:34:05 +010068/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 * Remap an arbitrary physical address space into the kernel virtual
Toshi Kani5d72b4f2015-04-14 15:47:29 -070070 * address space. It transparently creates kernel huge I/O mapping when
71 * the physical address is aligned by a huge page size (1GB or 2MB) and
72 * the requested size is at least the huge page size.
73 *
74 * NOTE: MTRRs can override PAT memory types with a 4KB granularity.
75 * Therefore, the mapping code falls back to use a smaller page toward 4KB
76 * when a mapping range is covered by non-WB type of MTRRs.
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 *
78 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
79 * have to convert them into an offset in a page-aligned mapping, but the
80 * caller shouldn't need to know that small detail.
81 */
Christoph Lameter23016962008-04-28 02:12:42 -070082static void __iomem *__ioremap_caller(resource_size_t phys_addr,
Juergen Grossb14097b2014-11-03 14:01:58 +010083 unsigned long size, enum page_cache_mode pcm, void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Kenji Kaneshigeffa71f32010-06-18 12:22:40 +090085 unsigned long offset, vaddr;
86 resource_size_t pfn, last_pfn, last_addr;
Pekka Paalanen87e547f2008-05-12 21:21:03 +020087 const resource_size_t unaligned_phys_addr = phys_addr;
88 const unsigned long unaligned_size = size;
Thomas Gleixner91eebf42008-01-30 13:34:05 +010089 struct vm_struct *area;
Juergen Grossb14097b2014-11-03 14:01:58 +010090 enum page_cache_mode new_pcm;
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +010091 pgprot_t prot;
Venki Pallipadidee7cbb2008-03-24 14:39:55 -070092 int retval;
Pekka Paalanend61fc442008-05-12 21:20:57 +020093 void __iomem *ret_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 /* Don't allow wraparound or zero size */
96 last_addr = phys_addr + size - 1;
97 if (!size || last_addr < phys_addr)
98 return NULL;
99
Thomas Gleixnere3100c82008-02-27 20:57:40 +0100100 if (!phys_addr_valid(phys_addr)) {
venkatesh.pallipadi@intel.com6997ab42008-03-18 17:00:25 -0700101 printk(KERN_WARNING "ioremap: invalid physical address %llx\n",
Randy Dunlap4c8337a2008-04-10 15:09:50 -0700102 (unsigned long long)phys_addr);
Thomas Gleixnere3100c82008-02-27 20:57:40 +0100103 WARN_ON_ONCE(1);
104 return NULL;
105 }
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 /*
108 * Don't remap the low PCI/ISA area, it's always mapped..
109 */
Andreas Herrmannbcc643d2008-06-20 21:58:46 +0200110 if (is_ISA_range(phys_addr, last_addr))
Thomas Gleixner4b40fce2008-01-30 13:34:05 +0100111 return (__force void __iomem *)phys_to_virt(phys_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 /*
114 * Don't allow anybody to remap normal RAM that we're using..
115 */
Toshi Kani9a58eeb2015-07-16 17:23:15 -0600116 pfn = phys_addr >> PAGE_SHIFT;
117 last_pfn = last_addr >> PAGE_SHIFT;
118 if (walk_system_ram_range(pfn, last_pfn - pfn + 1, NULL,
119 __ioremap_check_ram) == 1) {
Thomas Gleixner8a0a5da2015-07-24 16:13:43 +0200120 WARN_ONCE(1, "ioremap on RAM at %pa - %pa\n",
121 &phys_addr, &last_addr);
Roland Dreierc81c8a12014-05-02 11:18:41 -0700122 return NULL;
Mike Travis906e36c2014-10-13 15:54:05 -0700123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
venkatesh.pallipadi@intel.comd7677d42008-03-18 17:00:17 -0700125 /*
126 * Mappings have to be page-aligned
127 */
128 offset = phys_addr & ~PAGE_MASK;
Kenji Kaneshigeffa71f32010-06-18 12:22:40 +0900129 phys_addr &= PHYSICAL_PAGE_MASK;
venkatesh.pallipadi@intel.comd7677d42008-03-18 17:00:17 -0700130 size = PAGE_ALIGN(last_addr+1) - phys_addr;
131
Andi Kleene213e872008-08-15 18:12:47 +0200132 retval = reserve_memtype(phys_addr, (u64)phys_addr + size,
Juergen Grosse00c8cc2014-11-03 14:01:59 +0100133 pcm, &new_pcm);
Venki Pallipadidee7cbb2008-03-24 14:39:55 -0700134 if (retval) {
Venkatesh Pallipadi279e6692009-07-10 09:57:33 -0700135 printk(KERN_ERR "ioremap reserve_memtype failed %d\n", retval);
Venki Pallipadidee7cbb2008-03-24 14:39:55 -0700136 return NULL;
137 }
138
Juergen Grossb14097b2014-11-03 14:01:58 +0100139 if (pcm != new_pcm) {
140 if (!is_new_memtype_allowed(phys_addr, size, pcm, new_pcm)) {
Venkatesh Pallipadi279e6692009-07-10 09:57:33 -0700141 printk(KERN_ERR
Juergen Grossb14097b2014-11-03 14:01:58 +0100142 "ioremap error for 0x%llx-0x%llx, requested 0x%x, got 0x%x\n",
Randy Dunlap4c8337a2008-04-10 15:09:50 -0700143 (unsigned long long)phys_addr,
144 (unsigned long long)(phys_addr + size),
Juergen Grossb14097b2014-11-03 14:01:58 +0100145 pcm, new_pcm);
Xiaotian Fengde2a47c2009-11-05 10:43:51 +0800146 goto err_free_memtype;
venkatesh.pallipadi@intel.comd7677d42008-03-18 17:00:17 -0700147 }
Juergen Grossb14097b2014-11-03 14:01:58 +0100148 pcm = new_pcm;
venkatesh.pallipadi@intel.comd7677d42008-03-18 17:00:17 -0700149 }
150
Juergen Grossb14097b2014-11-03 14:01:58 +0100151 prot = PAGE_KERNEL_IO;
152 switch (pcm) {
153 case _PAGE_CACHE_MODE_UC:
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +0100154 default:
Juergen Grossb14097b2014-11-03 14:01:58 +0100155 prot = __pgprot(pgprot_val(prot) |
156 cachemode2protval(_PAGE_CACHE_MODE_UC));
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +0100157 break;
Juergen Grossb14097b2014-11-03 14:01:58 +0100158 case _PAGE_CACHE_MODE_UC_MINUS:
159 prot = __pgprot(pgprot_val(prot) |
160 cachemode2protval(_PAGE_CACHE_MODE_UC_MINUS));
Suresh Siddhade33c442008-04-25 17:07:22 -0700161 break;
Juergen Grossb14097b2014-11-03 14:01:58 +0100162 case _PAGE_CACHE_MODE_WC:
163 prot = __pgprot(pgprot_val(prot) |
164 cachemode2protval(_PAGE_CACHE_MODE_WC));
venkatesh.pallipadi@intel.comb310f381d2008-03-18 17:00:24 -0700165 break;
Toshi Kanid8382702015-06-04 18:55:15 +0200166 case _PAGE_CACHE_MODE_WT:
167 prot = __pgprot(pgprot_val(prot) |
168 cachemode2protval(_PAGE_CACHE_MODE_WT));
169 break;
Juergen Grossb14097b2014-11-03 14:01:58 +0100170 case _PAGE_CACHE_MODE_WB:
Thomas Gleixnerd806e5e2008-01-30 13:34:06 +0100171 break;
172 }
Haavard Skinnemoena148ecf2006-09-30 23:29:17 -0700173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 * Ok, go for it..
176 */
Christoph Lameter23016962008-04-28 02:12:42 -0700177 area = get_vm_area_caller(size, VM_IOREMAP, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (!area)
Xiaotian Fengde2a47c2009-11-05 10:43:51 +0800179 goto err_free_memtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 area->phys_addr = phys_addr;
Thomas Gleixnere66aadb2008-02-04 16:48:05 +0100181 vaddr = (unsigned long) area->addr;
Suresh Siddha43a432b2009-04-09 14:26:47 -0700182
Juergen Grossb14097b2014-11-03 14:01:58 +0100183 if (kernel_map_sync_memtype(phys_addr, size, pcm))
Xiaotian Fengde2a47c2009-11-05 10:43:51 +0800184 goto err_free_area;
Thomas Gleixnere9332ca2008-01-30 13:34:05 +0100185
Xiaotian Fengde2a47c2009-11-05 10:43:51 +0800186 if (ioremap_page_range(vaddr, vaddr + size, phys_addr, prot))
187 goto err_free_area;
Thomas Gleixnere9332ca2008-01-30 13:34:05 +0100188
Pekka Paalanend61fc442008-05-12 21:20:57 +0200189 ret_addr = (void __iomem *) (vaddr + offset);
Pekka Paalanen87e547f2008-05-12 21:21:03 +0200190 mmiotrace_ioremap(unaligned_phys_addr, unaligned_size, ret_addr);
Pekka Paalanend61fc442008-05-12 21:20:57 +0200191
Tim Gardnerc7a7b8142011-04-28 11:00:30 -0600192 /*
193 * Check if the request spans more than any BAR in the iomem resource
194 * tree.
195 */
Laura Abbott9abb0ec2015-12-21 12:01:14 -0800196 if (iomem_map_sanity_check(unaligned_phys_addr, unaligned_size))
197 pr_warn("caller %pS mapping multiple BARs\n", caller);
Tim Gardnerc7a7b8142011-04-28 11:00:30 -0600198
Pekka Paalanend61fc442008-05-12 21:20:57 +0200199 return ret_addr;
Xiaotian Fengde2a47c2009-11-05 10:43:51 +0800200err_free_area:
201 free_vm_area(area);
202err_free_memtype:
203 free_memtype(phys_addr, phys_addr + size);
204 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207/**
208 * ioremap_nocache - map bus memory into CPU space
Wanpeng Li9efc31b2012-06-10 10:50:52 +0800209 * @phys_addr: bus address of the memory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 * @size: size of the resource to map
211 *
212 * ioremap_nocache performs a platform specific sequence of operations to
213 * make bus memory CPU accessible via the readb/readw/readl/writeb/
214 * writew/writel functions and the other mmio helpers. The returned
215 * address is not guaranteed to be usable directly as a virtual
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100216 * address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 *
218 * This version of ioremap ensures that the memory is marked uncachable
219 * on the CPU as well as honouring existing caching rules from things like
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100220 * the PCI bus. Note that there are other caches and buffers on many
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 * busses. In particular driver authors should read up on PCI writes
222 *
223 * It's useful if some control registers are in such an area and
224 * write combining or read caching is not desirable:
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100225 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 * Must be freed with iounmap.
227 */
Linus Torvaldsb9e76a02008-03-24 11:22:39 -0700228void __iomem *ioremap_nocache(resource_size_t phys_addr, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Suresh Siddhade33c442008-04-25 17:07:22 -0700230 /*
231 * Ideally, this should be:
Luis R. Rodriguezcb32edf2015-05-26 10:28:15 +0200232 * pat_enabled() ? _PAGE_CACHE_MODE_UC : _PAGE_CACHE_MODE_UC_MINUS;
Suresh Siddhade33c442008-04-25 17:07:22 -0700233 *
234 * Till we fix all X drivers to use ioremap_wc(), we will use
Luis R. Rodrigueze4b6be332015-05-11 10:15:53 +0200235 * UC MINUS. Drivers that are certain they need or can already
236 * be converted over to strong UC can use ioremap_uc().
Suresh Siddhade33c442008-04-25 17:07:22 -0700237 */
Juergen Grossb14097b2014-11-03 14:01:58 +0100238 enum page_cache_mode pcm = _PAGE_CACHE_MODE_UC_MINUS;
Suresh Siddhade33c442008-04-25 17:07:22 -0700239
Juergen Grossb14097b2014-11-03 14:01:58 +0100240 return __ioremap_caller(phys_addr, size, pcm,
Christoph Lameter23016962008-04-28 02:12:42 -0700241 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700243EXPORT_SYMBOL(ioremap_nocache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
venkatesh.pallipadi@intel.comb310f381d2008-03-18 17:00:24 -0700245/**
Luis R. Rodrigueze4b6be332015-05-11 10:15:53 +0200246 * ioremap_uc - map bus memory into CPU space as strongly uncachable
247 * @phys_addr: bus address of the memory
248 * @size: size of the resource to map
249 *
250 * ioremap_uc performs a platform specific sequence of operations to
251 * make bus memory CPU accessible via the readb/readw/readl/writeb/
252 * writew/writel functions and the other mmio helpers. The returned
253 * address is not guaranteed to be usable directly as a virtual
254 * address.
255 *
256 * This version of ioremap ensures that the memory is marked with a strong
257 * preference as completely uncachable on the CPU when possible. For non-PAT
258 * systems this ends up setting page-attribute flags PCD=1, PWT=1. For PAT
259 * systems this will set the PAT entry for the pages as strong UC. This call
260 * will honor existing caching rules from things like the PCI bus. Note that
261 * there are other caches and buffers on many busses. In particular driver
262 * authors should read up on PCI writes.
263 *
264 * It's useful if some control registers are in such an area and
265 * write combining or read caching is not desirable:
266 *
267 * Must be freed with iounmap.
268 */
269void __iomem *ioremap_uc(resource_size_t phys_addr, unsigned long size)
270{
271 enum page_cache_mode pcm = _PAGE_CACHE_MODE_UC;
272
273 return __ioremap_caller(phys_addr, size, pcm,
274 __builtin_return_address(0));
275}
276EXPORT_SYMBOL_GPL(ioremap_uc);
277
278/**
venkatesh.pallipadi@intel.comb310f381d2008-03-18 17:00:24 -0700279 * ioremap_wc - map memory into CPU space write combined
Wanpeng Li9efc31b2012-06-10 10:50:52 +0800280 * @phys_addr: bus address of the memory
venkatesh.pallipadi@intel.comb310f381d2008-03-18 17:00:24 -0700281 * @size: size of the resource to map
282 *
283 * This version of ioremap ensures that the memory is marked write combining.
284 * Write combining allows faster writes to some hardware devices.
285 *
286 * Must be freed with iounmap.
287 */
venkatesh.pallipadi@intel.comd639bab2009-01-09 16:13:13 -0800288void __iomem *ioremap_wc(resource_size_t phys_addr, unsigned long size)
venkatesh.pallipadi@intel.comb310f381d2008-03-18 17:00:24 -0700289{
Borislav Petkov7202fdb2015-06-04 18:55:11 +0200290 return __ioremap_caller(phys_addr, size, _PAGE_CACHE_MODE_WC,
Christoph Lameter23016962008-04-28 02:12:42 -0700291 __builtin_return_address(0));
venkatesh.pallipadi@intel.comb310f381d2008-03-18 17:00:24 -0700292}
293EXPORT_SYMBOL(ioremap_wc);
294
Toshi Kanid8382702015-06-04 18:55:15 +0200295/**
296 * ioremap_wt - map memory into CPU space write through
297 * @phys_addr: bus address of the memory
298 * @size: size of the resource to map
299 *
300 * This version of ioremap ensures that the memory is marked write through.
301 * Write through stores data into memory while keeping the cache up-to-date.
302 *
303 * Must be freed with iounmap.
304 */
305void __iomem *ioremap_wt(resource_size_t phys_addr, unsigned long size)
306{
307 return __ioremap_caller(phys_addr, size, _PAGE_CACHE_MODE_WT,
308 __builtin_return_address(0));
309}
310EXPORT_SYMBOL(ioremap_wt);
311
Linus Torvaldsb9e76a02008-03-24 11:22:39 -0700312void __iomem *ioremap_cache(resource_size_t phys_addr, unsigned long size)
Thomas Gleixner5f868152008-01-30 13:34:06 +0100313{
Juergen Grossb14097b2014-11-03 14:01:58 +0100314 return __ioremap_caller(phys_addr, size, _PAGE_CACHE_MODE_WB,
Christoph Lameter23016962008-04-28 02:12:42 -0700315 __builtin_return_address(0));
Thomas Gleixner5f868152008-01-30 13:34:06 +0100316}
317EXPORT_SYMBOL(ioremap_cache);
318
Rik van Riel28b2ee22008-07-23 21:27:05 -0700319void __iomem *ioremap_prot(resource_size_t phys_addr, unsigned long size,
320 unsigned long prot_val)
321{
Juergen Grossb14097b2014-11-03 14:01:58 +0100322 return __ioremap_caller(phys_addr, size,
323 pgprot2cachemode(__pgprot(prot_val)),
Rik van Riel28b2ee22008-07-23 21:27:05 -0700324 __builtin_return_address(0));
325}
326EXPORT_SYMBOL(ioremap_prot);
327
Andi Kleenbf5421c2005-12-12 22:17:09 -0800328/**
329 * iounmap - Free a IO remapping
330 * @addr: virtual address from ioremap_*
331 *
332 * Caller must ensure there is only one unmapping for the same pointer.
333 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334void iounmap(volatile void __iomem *addr)
335{
Andi Kleenbf5421c2005-12-12 22:17:09 -0800336 struct vm_struct *p, *o;
Andrew Mortonc23a4e962005-07-07 17:56:02 -0700337
338 if ((void __force *)addr <= high_memory)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return;
340
341 /*
342 * __ioremap special-cases the PCI/ISA range by not instantiating a
343 * vm_area and by simply returning an address into the kernel mapping
344 * of ISA space. So handle that here.
345 */
Thomas Gleixner6e92a5a2008-05-12 15:43:35 +0200346 if ((void __force *)addr >= phys_to_virt(ISA_START_ADDRESS) &&
347 (void __force *)addr < phys_to_virt(ISA_END_ADDRESS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return;
349
Karol Herbst0f778412017-11-27 08:51:39 +0100350 mmiotrace_iounmap(addr);
351
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100352 addr = (volatile void __iomem *)
353 (PAGE_MASK & (unsigned long __force)addr);
Andi Kleenbf5421c2005-12-12 22:17:09 -0800354
355 /* Use the vm area unlocked, assuming the caller
356 ensures there isn't another iounmap for the same address
357 in parallel. Reuse of the virtual address is prevented by
358 leaving it in the global lists until we're done with it.
359 cpa takes care of the direct mappings. */
Joonsoo Kimef932472013-04-29 15:07:27 -0700360 p = find_vm_area((void __force *)addr);
Andi Kleenbf5421c2005-12-12 22:17:09 -0800361
362 if (!p) {
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100363 printk(KERN_ERR "iounmap: bad address %p\n", addr);
Andrew Mortonc23a4e962005-07-07 17:56:02 -0700364 dump_stack();
Andi Kleenbf5421c2005-12-12 22:17:09 -0800365 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
367
venkatesh.pallipadi@intel.comd7677d42008-03-18 17:00:17 -0700368 free_memtype(p->phys_addr, p->phys_addr + get_vm_area_size(p));
369
Andi Kleenbf5421c2005-12-12 22:17:09 -0800370 /* Finally remove it */
Thomas Gleixner6e92a5a2008-05-12 15:43:35 +0200371 o = remove_vm_area((void __force *)addr);
Andi Kleenbf5421c2005-12-12 22:17:09 -0800372 BUG_ON(p != o || o == NULL);
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100373 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700375EXPORT_SYMBOL(iounmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Jan Beulich1e6277d2015-05-28 09:29:27 +0100377int __init arch_ioremap_pud_supported(void)
Toshi Kani5d72b4f2015-04-14 15:47:29 -0700378{
379#ifdef CONFIG_X86_64
Borislav Petkovb8291adc2016-03-29 17:41:58 +0200380 return boot_cpu_has(X86_FEATURE_GBPAGES);
Toshi Kani5d72b4f2015-04-14 15:47:29 -0700381#else
382 return 0;
383#endif
384}
385
Jan Beulich1e6277d2015-05-28 09:29:27 +0100386int __init arch_ioremap_pmd_supported(void)
Toshi Kani5d72b4f2015-04-14 15:47:29 -0700387{
Borislav Petkov16bf9222016-03-29 17:42:03 +0200388 return boot_cpu_has(X86_FEATURE_PSE);
Toshi Kani5d72b4f2015-04-14 15:47:29 -0700389}
390
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700391/*
392 * Convert a physical pointer to a virtual kernel pointer for /dev/mem
393 * access
394 */
Thierry Reding4707a342014-07-28 17:20:33 +0200395void *xlate_dev_mem_ptr(phys_addr_t phys)
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700396{
Ingo Molnar94d4b472012-11-23 19:19:07 +0100397 unsigned long start = phys & PAGE_MASK;
398 unsigned long offset = phys & ~PAGE_MASK;
Ingo Molnar562bfca2015-05-08 12:43:53 +0200399 void *vaddr;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700400
401 /* If page is RAM, we can use __va. Otherwise ioremap and unmap. */
402 if (page_is_ram(start >> PAGE_SHIFT))
403 return __va(phys);
404
Ingo Molnar562bfca2015-05-08 12:43:53 +0200405 vaddr = ioremap_cache(start, PAGE_SIZE);
Ingo Molnar94d4b472012-11-23 19:19:07 +0100406 /* Only add the offset on success and return NULL if the ioremap() failed: */
407 if (vaddr)
408 vaddr += offset;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700409
Ingo Molnar562bfca2015-05-08 12:43:53 +0200410 return vaddr;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700411}
412
Thierry Reding4707a342014-07-28 17:20:33 +0200413void unxlate_dev_mem_ptr(phys_addr_t phys, void *addr)
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700414{
415 if (page_is_ram(phys >> PAGE_SHIFT))
416 return;
417
418 iounmap((void __iomem *)((unsigned long)addr & PAGE_MASK));
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700419}
420
Jeremy Fitzhardinge45c7b282009-03-20 17:53:34 -0700421static pte_t bm_pte[PAGE_SIZE/sizeof(pte_t)] __page_aligned_bss;
Huang, Ying0947b2f2008-01-30 13:33:44 +0100422
Ian Campbell551889a2008-02-09 23:24:09 +0100423static inline pmd_t * __init early_ioremap_pmd(unsigned long addr)
Huang, Ying0947b2f2008-01-30 13:33:44 +0100424{
Jeremy Fitzhardinge37cc8d72008-02-13 16:20:35 +0100425 /* Don't assume we're using swapper_pg_dir at this point */
426 pgd_t *base = __va(read_cr3());
427 pgd_t *pgd = &base[pgd_index(addr)];
Ian Campbell551889a2008-02-09 23:24:09 +0100428 pud_t *pud = pud_offset(pgd, addr);
429 pmd_t *pmd = pmd_offset(pud, addr);
430
431 return pmd;
Huang, Ying0947b2f2008-01-30 13:33:44 +0100432}
433
Ian Campbell551889a2008-02-09 23:24:09 +0100434static inline pte_t * __init early_ioremap_pte(unsigned long addr)
Huang, Ying0947b2f2008-01-30 13:33:44 +0100435{
Ian Campbell551889a2008-02-09 23:24:09 +0100436 return &bm_pte[pte_index(addr)];
Huang, Ying0947b2f2008-01-30 13:33:44 +0100437}
438
Jeremy Fitzhardingefef5ba72010-10-13 16:02:24 -0700439bool __init is_early_ioremap_ptep(pte_t *ptep)
440{
441 return ptep >= &bm_pte[0] && ptep < &bm_pte[PAGE_SIZE/sizeof(pte_t)];
442}
443
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100444void __init early_ioremap_init(void)
Huang, Ying0947b2f2008-01-30 13:33:44 +0100445{
Ian Campbell551889a2008-02-09 23:24:09 +0100446 pmd_t *pmd;
Huang, Ying0947b2f2008-01-30 13:33:44 +0100447
Andy Lutomirski73159fd2014-05-05 12:19:31 -0700448#ifdef CONFIG_X86_64
449 BUILD_BUG_ON((fix_to_virt(0) + PAGE_SIZE) & ((1 << PMD_SHIFT) - 1));
450#else
451 WARN_ON((fix_to_virt(0) + PAGE_SIZE) & ((1 << PMD_SHIFT) - 1));
452#endif
453
Mark Salter5b7c73e2014-04-07 15:39:49 -0700454 early_ioremap_setup();
Wang Chen88272472009-03-07 13:34:19 +0800455
Ian Campbell551889a2008-02-09 23:24:09 +0100456 pmd = early_ioremap_pmd(fix_to_virt(FIX_BTMAP_BEGIN));
Jeremy Fitzhardinge45c7b282009-03-20 17:53:34 -0700457 memset(bm_pte, 0, sizeof(bm_pte));
458 pmd_populate_kernel(&init_mm, pmd, bm_pte);
Ian Campbell551889a2008-02-09 23:24:09 +0100459
Ingo Molnar0e3a9542008-01-30 13:33:49 +0100460 /*
Ian Campbell551889a2008-02-09 23:24:09 +0100461 * The boot-ioremap range spans multiple pmds, for which
Ingo Molnar0e3a9542008-01-30 13:33:49 +0100462 * we are not prepared:
463 */
Jan Beulich499a5f12009-12-18 16:05:51 +0000464#define __FIXADDR_TOP (-PAGE_SIZE)
465 BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
466 != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
467#undef __FIXADDR_TOP
Ian Campbell551889a2008-02-09 23:24:09 +0100468 if (pmd != early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END))) {
Ingo Molnar0e3a9542008-01-30 13:33:49 +0100469 WARN_ON(1);
Ian Campbell551889a2008-02-09 23:24:09 +0100470 printk(KERN_WARNING "pmd %p != %p\n",
471 pmd, early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END)));
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100472 printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
Ian Campbell551889a2008-02-09 23:24:09 +0100473 fix_to_virt(FIX_BTMAP_BEGIN));
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100474 printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_END): %08lx\n",
Ian Campbell551889a2008-02-09 23:24:09 +0100475 fix_to_virt(FIX_BTMAP_END));
Ingo Molnar0e3a9542008-01-30 13:33:49 +0100476
Thomas Gleixner91eebf42008-01-30 13:34:05 +0100477 printk(KERN_WARNING "FIX_BTMAP_END: %d\n", FIX_BTMAP_END);
478 printk(KERN_WARNING "FIX_BTMAP_BEGIN: %d\n",
479 FIX_BTMAP_BEGIN);
Ingo Molnar0e3a9542008-01-30 13:33:49 +0100480 }
Huang, Ying0947b2f2008-01-30 13:33:44 +0100481}
482
Mark Salter5b7c73e2014-04-07 15:39:49 -0700483void __init __early_set_fixmap(enum fixed_addresses idx,
484 phys_addr_t phys, pgprot_t flags)
Huang, Ying0947b2f2008-01-30 13:33:44 +0100485{
Ian Campbell551889a2008-02-09 23:24:09 +0100486 unsigned long addr = __fix_to_virt(idx);
487 pte_t *pte;
Huang, Ying0947b2f2008-01-30 13:33:44 +0100488
489 if (idx >= __end_of_fixed_addresses) {
490 BUG();
491 return;
492 }
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100493 pte = early_ioremap_pte(addr);
Jeremy Fitzhardinge4583ed52008-06-25 00:19:03 -0400494
Huang, Ying0947b2f2008-01-30 13:33:44 +0100495 if (pgprot_val(flags))
Ian Campbell551889a2008-02-09 23:24:09 +0100496 set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
Huang, Ying0947b2f2008-01-30 13:33:44 +0100497 else
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400498 pte_clear(&init_mm, addr, pte);
Huang, Ying0947b2f2008-01-30 13:33:44 +0100499 __flush_tlb_one(addr);
500}