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> 129de94dbbSIngo Molnar #include <linux/ioport.h> 13e64c8aa0SThomas Gleixner #include <linux/slab.h> 14e64c8aa0SThomas Gleixner #include <linux/vmalloc.h> 15d61fc448SPekka Paalanen #include <linux/mmiotrace.h> 168f716c9bSTom Lendacky #include <linux/mem_encrypt.h> 178f716c9bSTom Lendacky #include <linux/efi.h> 18e64c8aa0SThomas Gleixner 19d1163651SLaura Abbott #include <asm/set_memory.h> 2066441bd3SIngo Molnar #include <asm/e820/api.h> 21e64c8aa0SThomas Gleixner #include <asm/fixmap.h> 22e64c8aa0SThomas Gleixner #include <asm/pgtable.h> 23e64c8aa0SThomas Gleixner #include <asm/tlbflush.h> 24f6df72e7SJeremy Fitzhardinge #include <asm/pgalloc.h> 25d7677d40Svenkatesh.pallipadi@intel.com #include <asm/pat.h> 268f716c9bSTom Lendacky #include <asm/setup.h> 27e64c8aa0SThomas Gleixner 2878c86e5eSJeremy Fitzhardinge #include "physaddr.h" 29e64c8aa0SThomas Gleixner 300e4c12b4STom Lendacky struct ioremap_mem_flags { 310e4c12b4STom Lendacky bool system_ram; 320e4c12b4STom Lendacky bool desc_other; 330e4c12b4STom Lendacky }; 340e4c12b4STom Lendacky 35e64c8aa0SThomas Gleixner /* 36e64c8aa0SThomas Gleixner * Fix up the linear direct mapping of the kernel to avoid cache attribute 37e64c8aa0SThomas Gleixner * conflicts. 38e64c8aa0SThomas Gleixner */ 393a96ce8cSvenkatesh.pallipadi@intel.com int ioremap_change_attr(unsigned long vaddr, unsigned long size, 40b14097bdSJuergen Gross enum page_cache_mode pcm) 41e64c8aa0SThomas Gleixner { 42d806e5eeSThomas Gleixner unsigned long nrpages = size >> PAGE_SHIFT; 4393809be8SHarvey Harrison int err; 44e64c8aa0SThomas Gleixner 45b14097bdSJuergen Gross switch (pcm) { 46b14097bdSJuergen Gross case _PAGE_CACHE_MODE_UC: 47d806e5eeSThomas Gleixner default: 481219333dSvenkatesh.pallipadi@intel.com err = _set_memory_uc(vaddr, nrpages); 49d806e5eeSThomas Gleixner break; 50b14097bdSJuergen Gross case _PAGE_CACHE_MODE_WC: 51b310f381Svenkatesh.pallipadi@intel.com err = _set_memory_wc(vaddr, nrpages); 52b310f381Svenkatesh.pallipadi@intel.com break; 53623dffb2SToshi Kani case _PAGE_CACHE_MODE_WT: 54623dffb2SToshi Kani err = _set_memory_wt(vaddr, nrpages); 55623dffb2SToshi Kani break; 56b14097bdSJuergen Gross case _PAGE_CACHE_MODE_WB: 571219333dSvenkatesh.pallipadi@intel.com err = _set_memory_wb(vaddr, nrpages); 58d806e5eeSThomas Gleixner break; 59d806e5eeSThomas Gleixner } 60e64c8aa0SThomas Gleixner 61e64c8aa0SThomas Gleixner return err; 62e64c8aa0SThomas Gleixner } 63e64c8aa0SThomas Gleixner 640e4c12b4STom Lendacky static bool __ioremap_check_ram(struct resource *res) 65c81c8a1eSRoland Dreier { 660e4c12b4STom Lendacky unsigned long start_pfn, stop_pfn; 67c81c8a1eSRoland Dreier unsigned long i; 68c81c8a1eSRoland Dreier 690e4c12b4STom Lendacky if ((res->flags & IORESOURCE_SYSTEM_RAM) != IORESOURCE_SYSTEM_RAM) 700e4c12b4STom Lendacky return false; 710e4c12b4STom Lendacky 720e4c12b4STom Lendacky start_pfn = (res->start + PAGE_SIZE - 1) >> PAGE_SHIFT; 730e4c12b4STom Lendacky stop_pfn = (res->end + 1) >> PAGE_SHIFT; 740e4c12b4STom Lendacky if (stop_pfn > start_pfn) { 750e4c12b4STom Lendacky for (i = 0; i < (stop_pfn - start_pfn); ++i) 76c81c8a1eSRoland Dreier if (pfn_valid(start_pfn + i) && 77c81c8a1eSRoland Dreier !PageReserved(pfn_to_page(start_pfn + i))) 780e4c12b4STom Lendacky return true; 790e4c12b4STom Lendacky } 80c81c8a1eSRoland Dreier 810e4c12b4STom Lendacky return false; 820e4c12b4STom Lendacky } 830e4c12b4STom Lendacky 840e4c12b4STom Lendacky static int __ioremap_check_desc_other(struct resource *res) 850e4c12b4STom Lendacky { 860e4c12b4STom Lendacky return (res->desc != IORES_DESC_NONE); 870e4c12b4STom Lendacky } 880e4c12b4STom Lendacky 890e4c12b4STom Lendacky static int __ioremap_res_check(struct resource *res, void *arg) 900e4c12b4STom Lendacky { 910e4c12b4STom Lendacky struct ioremap_mem_flags *flags = arg; 920e4c12b4STom Lendacky 930e4c12b4STom Lendacky if (!flags->system_ram) 940e4c12b4STom Lendacky flags->system_ram = __ioremap_check_ram(res); 950e4c12b4STom Lendacky 960e4c12b4STom Lendacky if (!flags->desc_other) 970e4c12b4STom Lendacky flags->desc_other = __ioremap_check_desc_other(res); 980e4c12b4STom Lendacky 990e4c12b4STom Lendacky return flags->system_ram && flags->desc_other; 1000e4c12b4STom Lendacky } 1010e4c12b4STom Lendacky 1020e4c12b4STom Lendacky /* 1030e4c12b4STom Lendacky * To avoid multiple resource walks, this function walks resources marked as 1040e4c12b4STom Lendacky * IORESOURCE_MEM and IORESOURCE_BUSY and looking for system RAM and/or a 1050e4c12b4STom Lendacky * resource described not as IORES_DESC_NONE (e.g. IORES_DESC_ACPI_TABLES). 1060e4c12b4STom Lendacky */ 1070e4c12b4STom Lendacky static void __ioremap_check_mem(resource_size_t addr, unsigned long size, 1080e4c12b4STom Lendacky struct ioremap_mem_flags *flags) 1090e4c12b4STom Lendacky { 1100e4c12b4STom Lendacky u64 start, end; 1110e4c12b4STom Lendacky 1120e4c12b4STom Lendacky start = (u64)addr; 1130e4c12b4STom Lendacky end = start + size - 1; 1140e4c12b4STom Lendacky memset(flags, 0, sizeof(*flags)); 1150e4c12b4STom Lendacky 1160e4c12b4STom Lendacky walk_mem_res(start, end, flags, __ioremap_res_check); 117c81c8a1eSRoland Dreier } 118c81c8a1eSRoland Dreier 119e64c8aa0SThomas Gleixner /* 120e64c8aa0SThomas Gleixner * Remap an arbitrary physical address space into the kernel virtual 1215d72b4fbSToshi Kani * address space. It transparently creates kernel huge I/O mapping when 1225d72b4fbSToshi Kani * the physical address is aligned by a huge page size (1GB or 2MB) and 1235d72b4fbSToshi Kani * the requested size is at least the huge page size. 1245d72b4fbSToshi Kani * 1255d72b4fbSToshi Kani * NOTE: MTRRs can override PAT memory types with a 4KB granularity. 1265d72b4fbSToshi Kani * Therefore, the mapping code falls back to use a smaller page toward 4KB 1275d72b4fbSToshi Kani * when a mapping range is covered by non-WB type of MTRRs. 128e64c8aa0SThomas Gleixner * 129e64c8aa0SThomas Gleixner * NOTE! We need to allow non-page-aligned mappings too: we will obviously 130e64c8aa0SThomas Gleixner * have to convert them into an offset in a page-aligned mapping, but the 131e64c8aa0SThomas Gleixner * caller shouldn't need to know that small detail. 132e64c8aa0SThomas Gleixner */ 13323016969SChristoph Lameter static void __iomem *__ioremap_caller(resource_size_t phys_addr, 134b14097bdSJuergen Gross unsigned long size, enum page_cache_mode pcm, void *caller) 135e64c8aa0SThomas Gleixner { 136ffa71f33SKenji Kaneshige unsigned long offset, vaddr; 1370e4c12b4STom Lendacky resource_size_t last_addr; 13887e547feSPekka Paalanen const resource_size_t unaligned_phys_addr = phys_addr; 13987e547feSPekka Paalanen const unsigned long unaligned_size = size; 1400e4c12b4STom Lendacky struct ioremap_mem_flags mem_flags; 141e64c8aa0SThomas Gleixner struct vm_struct *area; 142b14097bdSJuergen Gross enum page_cache_mode new_pcm; 143d806e5eeSThomas Gleixner pgprot_t prot; 144dee7cbb2SVenki Pallipadi int retval; 145d61fc448SPekka Paalanen void __iomem *ret_addr; 146e64c8aa0SThomas Gleixner 147e64c8aa0SThomas Gleixner /* Don't allow wraparound or zero size */ 148e64c8aa0SThomas Gleixner last_addr = phys_addr + size - 1; 149e64c8aa0SThomas Gleixner if (!size || last_addr < phys_addr) 150e64c8aa0SThomas Gleixner return NULL; 151e64c8aa0SThomas Gleixner 152e3100c82SThomas Gleixner if (!phys_addr_valid(phys_addr)) { 1536997ab49Svenkatesh.pallipadi@intel.com printk(KERN_WARNING "ioremap: invalid physical address %llx\n", 1544c8337acSRandy Dunlap (unsigned long long)phys_addr); 155e3100c82SThomas Gleixner WARN_ON_ONCE(1); 156e3100c82SThomas Gleixner return NULL; 157e3100c82SThomas Gleixner } 158e3100c82SThomas Gleixner 1590e4c12b4STom Lendacky __ioremap_check_mem(phys_addr, size, &mem_flags); 1600e4c12b4STom Lendacky 161e64c8aa0SThomas Gleixner /* 162e64c8aa0SThomas Gleixner * Don't allow anybody to remap normal RAM that we're using.. 163e64c8aa0SThomas Gleixner */ 1640e4c12b4STom Lendacky if (mem_flags.system_ram) { 1658a0a5da6SThomas Gleixner WARN_ONCE(1, "ioremap on RAM at %pa - %pa\n", 1668a0a5da6SThomas Gleixner &phys_addr, &last_addr); 167e64c8aa0SThomas Gleixner return NULL; 168906e36c5SMike Travis } 1699a58eebeSToshi Kani 170d7677d40Svenkatesh.pallipadi@intel.com /* 171d7677d40Svenkatesh.pallipadi@intel.com * Mappings have to be page-aligned 172d7677d40Svenkatesh.pallipadi@intel.com */ 173d7677d40Svenkatesh.pallipadi@intel.com offset = phys_addr & ~PAGE_MASK; 174ffa71f33SKenji Kaneshige phys_addr &= PHYSICAL_PAGE_MASK; 175d7677d40Svenkatesh.pallipadi@intel.com size = PAGE_ALIGN(last_addr+1) - phys_addr; 176d7677d40Svenkatesh.pallipadi@intel.com 177e213e877SAndi Kleen retval = reserve_memtype(phys_addr, (u64)phys_addr + size, 178e00c8cc9SJuergen Gross pcm, &new_pcm); 179dee7cbb2SVenki Pallipadi if (retval) { 180279e669bSVenkatesh Pallipadi printk(KERN_ERR "ioremap reserve_memtype failed %d\n", retval); 181dee7cbb2SVenki Pallipadi return NULL; 182dee7cbb2SVenki Pallipadi } 183dee7cbb2SVenki Pallipadi 184b14097bdSJuergen Gross if (pcm != new_pcm) { 185b14097bdSJuergen Gross if (!is_new_memtype_allowed(phys_addr, size, pcm, new_pcm)) { 186279e669bSVenkatesh Pallipadi printk(KERN_ERR 187b14097bdSJuergen Gross "ioremap error for 0x%llx-0x%llx, requested 0x%x, got 0x%x\n", 1884c8337acSRandy Dunlap (unsigned long long)phys_addr, 1894c8337acSRandy Dunlap (unsigned long long)(phys_addr + size), 190b14097bdSJuergen Gross pcm, new_pcm); 191de2a47cfSXiaotian Feng goto err_free_memtype; 192d7677d40Svenkatesh.pallipadi@intel.com } 193b14097bdSJuergen Gross pcm = new_pcm; 194d7677d40Svenkatesh.pallipadi@intel.com } 195d7677d40Svenkatesh.pallipadi@intel.com 1960e4c12b4STom Lendacky /* 1970e4c12b4STom Lendacky * If the page being mapped is in memory and SEV is active then 1980e4c12b4STom Lendacky * make sure the memory encryption attribute is enabled in the 1990e4c12b4STom Lendacky * resulting mapping. 2000e4c12b4STom Lendacky */ 201be43d728SJeremy Fitzhardinge prot = PAGE_KERNEL_IO; 2020e4c12b4STom Lendacky if (sev_active() && mem_flags.desc_other) 2030e4c12b4STom Lendacky prot = pgprot_encrypted(prot); 2040e4c12b4STom Lendacky 205b14097bdSJuergen Gross switch (pcm) { 206b14097bdSJuergen Gross case _PAGE_CACHE_MODE_UC: 207b14097bdSJuergen Gross default: 208b14097bdSJuergen Gross prot = __pgprot(pgprot_val(prot) | 209b14097bdSJuergen Gross cachemode2protval(_PAGE_CACHE_MODE_UC)); 210b14097bdSJuergen Gross break; 211b14097bdSJuergen Gross case _PAGE_CACHE_MODE_UC_MINUS: 212b14097bdSJuergen Gross prot = __pgprot(pgprot_val(prot) | 213b14097bdSJuergen Gross cachemode2protval(_PAGE_CACHE_MODE_UC_MINUS)); 214b14097bdSJuergen Gross break; 215b14097bdSJuergen Gross case _PAGE_CACHE_MODE_WC: 216b14097bdSJuergen Gross prot = __pgprot(pgprot_val(prot) | 217b14097bdSJuergen Gross cachemode2protval(_PAGE_CACHE_MODE_WC)); 218b14097bdSJuergen Gross break; 219d838270eSToshi Kani case _PAGE_CACHE_MODE_WT: 220d838270eSToshi Kani prot = __pgprot(pgprot_val(prot) | 221d838270eSToshi Kani cachemode2protval(_PAGE_CACHE_MODE_WT)); 222d838270eSToshi Kani break; 223b14097bdSJuergen Gross case _PAGE_CACHE_MODE_WB: 224d806e5eeSThomas Gleixner break; 225d806e5eeSThomas Gleixner } 226e64c8aa0SThomas Gleixner 227e64c8aa0SThomas Gleixner /* 228e64c8aa0SThomas Gleixner * Ok, go for it.. 229e64c8aa0SThomas Gleixner */ 23023016969SChristoph Lameter area = get_vm_area_caller(size, VM_IOREMAP, caller); 231e64c8aa0SThomas Gleixner if (!area) 232de2a47cfSXiaotian Feng goto err_free_memtype; 233e64c8aa0SThomas Gleixner area->phys_addr = phys_addr; 234e66aadbeSThomas Gleixner vaddr = (unsigned long) area->addr; 23543a432b1SSuresh Siddha 236b14097bdSJuergen Gross if (kernel_map_sync_memtype(phys_addr, size, pcm)) 237de2a47cfSXiaotian Feng goto err_free_area; 238e64c8aa0SThomas Gleixner 239de2a47cfSXiaotian Feng if (ioremap_page_range(vaddr, vaddr + size, phys_addr, prot)) 240de2a47cfSXiaotian Feng goto err_free_area; 241e64c8aa0SThomas Gleixner 242d61fc448SPekka Paalanen ret_addr = (void __iomem *) (vaddr + offset); 24387e547feSPekka Paalanen mmiotrace_ioremap(unaligned_phys_addr, unaligned_size, ret_addr); 244d61fc448SPekka Paalanen 245c7a7b814STim Gardner /* 246c7a7b814STim Gardner * Check if the request spans more than any BAR in the iomem resource 247c7a7b814STim Gardner * tree. 248c7a7b814STim Gardner */ 2499abb0ecdSLaura Abbott if (iomem_map_sanity_check(unaligned_phys_addr, unaligned_size)) 2509abb0ecdSLaura Abbott pr_warn("caller %pS mapping multiple BARs\n", caller); 251c7a7b814STim Gardner 252d61fc448SPekka Paalanen return ret_addr; 253de2a47cfSXiaotian Feng err_free_area: 254de2a47cfSXiaotian Feng free_vm_area(area); 255de2a47cfSXiaotian Feng err_free_memtype: 256de2a47cfSXiaotian Feng free_memtype(phys_addr, phys_addr + size); 257de2a47cfSXiaotian Feng return NULL; 258e64c8aa0SThomas Gleixner } 259e64c8aa0SThomas Gleixner 260e64c8aa0SThomas Gleixner /** 261e64c8aa0SThomas Gleixner * ioremap_nocache - map bus memory into CPU space 2629efc31b8SWanpeng Li * @phys_addr: bus address of the memory 263e64c8aa0SThomas Gleixner * @size: size of the resource to map 264e64c8aa0SThomas Gleixner * 265e64c8aa0SThomas Gleixner * ioremap_nocache performs a platform specific sequence of operations to 266e64c8aa0SThomas Gleixner * make bus memory CPU accessible via the readb/readw/readl/writeb/ 267e64c8aa0SThomas Gleixner * writew/writel functions and the other mmio helpers. The returned 268e64c8aa0SThomas Gleixner * address is not guaranteed to be usable directly as a virtual 269e64c8aa0SThomas Gleixner * address. 270e64c8aa0SThomas Gleixner * 271e64c8aa0SThomas Gleixner * This version of ioremap ensures that the memory is marked uncachable 272e64c8aa0SThomas Gleixner * on the CPU as well as honouring existing caching rules from things like 273e64c8aa0SThomas Gleixner * the PCI bus. Note that there are other caches and buffers on many 274e64c8aa0SThomas Gleixner * busses. In particular driver authors should read up on PCI writes 275e64c8aa0SThomas Gleixner * 276e64c8aa0SThomas Gleixner * It's useful if some control registers are in such an area and 277e64c8aa0SThomas Gleixner * write combining or read caching is not desirable: 278e64c8aa0SThomas Gleixner * 279e64c8aa0SThomas Gleixner * Must be freed with iounmap. 280e64c8aa0SThomas Gleixner */ 281b9e76a00SLinus Torvalds void __iomem *ioremap_nocache(resource_size_t phys_addr, unsigned long size) 282e64c8aa0SThomas Gleixner { 283de33c442SSuresh Siddha /* 284de33c442SSuresh Siddha * Ideally, this should be: 285cb32edf6SLuis R. Rodriguez * pat_enabled() ? _PAGE_CACHE_MODE_UC : _PAGE_CACHE_MODE_UC_MINUS; 286de33c442SSuresh Siddha * 287de33c442SSuresh Siddha * Till we fix all X drivers to use ioremap_wc(), we will use 288e4b6be33SLuis R. Rodriguez * UC MINUS. Drivers that are certain they need or can already 289e4b6be33SLuis R. Rodriguez * be converted over to strong UC can use ioremap_uc(). 290de33c442SSuresh Siddha */ 291b14097bdSJuergen Gross enum page_cache_mode pcm = _PAGE_CACHE_MODE_UC_MINUS; 292de33c442SSuresh Siddha 293b14097bdSJuergen Gross return __ioremap_caller(phys_addr, size, pcm, 29423016969SChristoph Lameter __builtin_return_address(0)); 295e64c8aa0SThomas Gleixner } 296e64c8aa0SThomas Gleixner EXPORT_SYMBOL(ioremap_nocache); 297e64c8aa0SThomas Gleixner 298b310f381Svenkatesh.pallipadi@intel.com /** 299e4b6be33SLuis R. Rodriguez * ioremap_uc - map bus memory into CPU space as strongly uncachable 300e4b6be33SLuis R. Rodriguez * @phys_addr: bus address of the memory 301e4b6be33SLuis R. Rodriguez * @size: size of the resource to map 302e4b6be33SLuis R. Rodriguez * 303e4b6be33SLuis R. Rodriguez * ioremap_uc performs a platform specific sequence of operations to 304e4b6be33SLuis R. Rodriguez * make bus memory CPU accessible via the readb/readw/readl/writeb/ 305e4b6be33SLuis R. Rodriguez * writew/writel functions and the other mmio helpers. The returned 306e4b6be33SLuis R. Rodriguez * address is not guaranteed to be usable directly as a virtual 307e4b6be33SLuis R. Rodriguez * address. 308e4b6be33SLuis R. Rodriguez * 309e4b6be33SLuis R. Rodriguez * This version of ioremap ensures that the memory is marked with a strong 310e4b6be33SLuis R. Rodriguez * preference as completely uncachable on the CPU when possible. For non-PAT 311e4b6be33SLuis R. Rodriguez * systems this ends up setting page-attribute flags PCD=1, PWT=1. For PAT 312e4b6be33SLuis R. Rodriguez * systems this will set the PAT entry for the pages as strong UC. This call 313e4b6be33SLuis R. Rodriguez * will honor existing caching rules from things like the PCI bus. Note that 314e4b6be33SLuis R. Rodriguez * there are other caches and buffers on many busses. In particular driver 315e4b6be33SLuis R. Rodriguez * authors should read up on PCI writes. 316e4b6be33SLuis R. Rodriguez * 317e4b6be33SLuis R. Rodriguez * It's useful if some control registers are in such an area and 318e4b6be33SLuis R. Rodriguez * write combining or read caching is not desirable: 319e4b6be33SLuis R. Rodriguez * 320e4b6be33SLuis R. Rodriguez * Must be freed with iounmap. 321e4b6be33SLuis R. Rodriguez */ 322e4b6be33SLuis R. Rodriguez void __iomem *ioremap_uc(resource_size_t phys_addr, unsigned long size) 323e4b6be33SLuis R. Rodriguez { 324e4b6be33SLuis R. Rodriguez enum page_cache_mode pcm = _PAGE_CACHE_MODE_UC; 325e4b6be33SLuis R. Rodriguez 326e4b6be33SLuis R. Rodriguez return __ioremap_caller(phys_addr, size, pcm, 327e4b6be33SLuis R. Rodriguez __builtin_return_address(0)); 328e4b6be33SLuis R. Rodriguez } 329e4b6be33SLuis R. Rodriguez EXPORT_SYMBOL_GPL(ioremap_uc); 330e4b6be33SLuis R. Rodriguez 331e4b6be33SLuis R. Rodriguez /** 332b310f381Svenkatesh.pallipadi@intel.com * ioremap_wc - map memory into CPU space write combined 3339efc31b8SWanpeng Li * @phys_addr: bus address of the memory 334b310f381Svenkatesh.pallipadi@intel.com * @size: size of the resource to map 335b310f381Svenkatesh.pallipadi@intel.com * 336b310f381Svenkatesh.pallipadi@intel.com * This version of ioremap ensures that the memory is marked write combining. 337b310f381Svenkatesh.pallipadi@intel.com * Write combining allows faster writes to some hardware devices. 338b310f381Svenkatesh.pallipadi@intel.com * 339b310f381Svenkatesh.pallipadi@intel.com * Must be freed with iounmap. 340b310f381Svenkatesh.pallipadi@intel.com */ 341d639bab8Svenkatesh.pallipadi@intel.com void __iomem *ioremap_wc(resource_size_t phys_addr, unsigned long size) 342b310f381Svenkatesh.pallipadi@intel.com { 343b14097bdSJuergen Gross return __ioremap_caller(phys_addr, size, _PAGE_CACHE_MODE_WC, 34423016969SChristoph Lameter __builtin_return_address(0)); 345b310f381Svenkatesh.pallipadi@intel.com } 346b310f381Svenkatesh.pallipadi@intel.com EXPORT_SYMBOL(ioremap_wc); 347b310f381Svenkatesh.pallipadi@intel.com 348d838270eSToshi Kani /** 349d838270eSToshi Kani * ioremap_wt - map memory into CPU space write through 350d838270eSToshi Kani * @phys_addr: bus address of the memory 351d838270eSToshi Kani * @size: size of the resource to map 352d838270eSToshi Kani * 353d838270eSToshi Kani * This version of ioremap ensures that the memory is marked write through. 354d838270eSToshi Kani * Write through stores data into memory while keeping the cache up-to-date. 355d838270eSToshi Kani * 356d838270eSToshi Kani * Must be freed with iounmap. 357d838270eSToshi Kani */ 358d838270eSToshi Kani void __iomem *ioremap_wt(resource_size_t phys_addr, unsigned long size) 359d838270eSToshi Kani { 360d838270eSToshi Kani return __ioremap_caller(phys_addr, size, _PAGE_CACHE_MODE_WT, 361d838270eSToshi Kani __builtin_return_address(0)); 362d838270eSToshi Kani } 363d838270eSToshi Kani EXPORT_SYMBOL(ioremap_wt); 364d838270eSToshi Kani 365b9e76a00SLinus Torvalds void __iomem *ioremap_cache(resource_size_t phys_addr, unsigned long size) 3665f868152SThomas Gleixner { 367b14097bdSJuergen Gross return __ioremap_caller(phys_addr, size, _PAGE_CACHE_MODE_WB, 36823016969SChristoph Lameter __builtin_return_address(0)); 3695f868152SThomas Gleixner } 3705f868152SThomas Gleixner EXPORT_SYMBOL(ioremap_cache); 3715f868152SThomas Gleixner 37228b2ee20SRik van Riel void __iomem *ioremap_prot(resource_size_t phys_addr, unsigned long size, 37328b2ee20SRik van Riel unsigned long prot_val) 37428b2ee20SRik van Riel { 375b14097bdSJuergen Gross return __ioremap_caller(phys_addr, size, 376b14097bdSJuergen Gross pgprot2cachemode(__pgprot(prot_val)), 37728b2ee20SRik van Riel __builtin_return_address(0)); 37828b2ee20SRik van Riel } 37928b2ee20SRik van Riel EXPORT_SYMBOL(ioremap_prot); 38028b2ee20SRik van Riel 381e64c8aa0SThomas Gleixner /** 382e64c8aa0SThomas Gleixner * iounmap - Free a IO remapping 383e64c8aa0SThomas Gleixner * @addr: virtual address from ioremap_* 384e64c8aa0SThomas Gleixner * 385e64c8aa0SThomas Gleixner * Caller must ensure there is only one unmapping for the same pointer. 386e64c8aa0SThomas Gleixner */ 387e64c8aa0SThomas Gleixner void iounmap(volatile void __iomem *addr) 388e64c8aa0SThomas Gleixner { 389e64c8aa0SThomas Gleixner struct vm_struct *p, *o; 390e64c8aa0SThomas Gleixner 391e64c8aa0SThomas Gleixner if ((void __force *)addr <= high_memory) 392e64c8aa0SThomas Gleixner return; 393e64c8aa0SThomas Gleixner 394e64c8aa0SThomas Gleixner /* 39533c2b803STom Lendacky * The PCI/ISA range special-casing was removed from __ioremap() 39633c2b803STom Lendacky * so this check, in theory, can be removed. However, there are 39733c2b803STom Lendacky * cases where iounmap() is called for addresses not obtained via 39833c2b803STom Lendacky * ioremap() (vga16fb for example). Add a warning so that these 39933c2b803STom Lendacky * cases can be caught and fixed. 400e64c8aa0SThomas Gleixner */ 4016e92a5a6SThomas Gleixner if ((void __force *)addr >= phys_to_virt(ISA_START_ADDRESS) && 40233c2b803STom Lendacky (void __force *)addr < phys_to_virt(ISA_END_ADDRESS)) { 40333c2b803STom Lendacky WARN(1, "iounmap() called for ISA range not obtained using ioremap()\n"); 404e64c8aa0SThomas Gleixner return; 40533c2b803STom Lendacky } 406e64c8aa0SThomas Gleixner 407*6d60ce38SKarol Herbst mmiotrace_iounmap(addr); 408*6d60ce38SKarol Herbst 409e64c8aa0SThomas Gleixner addr = (volatile void __iomem *) 410e64c8aa0SThomas Gleixner (PAGE_MASK & (unsigned long __force)addr); 411e64c8aa0SThomas Gleixner 412e64c8aa0SThomas Gleixner /* Use the vm area unlocked, assuming the caller 413e64c8aa0SThomas Gleixner ensures there isn't another iounmap for the same address 414e64c8aa0SThomas Gleixner in parallel. Reuse of the virtual address is prevented by 415e64c8aa0SThomas Gleixner leaving it in the global lists until we're done with it. 416e64c8aa0SThomas Gleixner cpa takes care of the direct mappings. */ 417ef932473SJoonsoo Kim p = find_vm_area((void __force *)addr); 418e64c8aa0SThomas Gleixner 419e64c8aa0SThomas Gleixner if (!p) { 420e64c8aa0SThomas Gleixner printk(KERN_ERR "iounmap: bad address %p\n", addr); 421e64c8aa0SThomas Gleixner dump_stack(); 422e64c8aa0SThomas Gleixner return; 423e64c8aa0SThomas Gleixner } 424e64c8aa0SThomas Gleixner 425d7677d40Svenkatesh.pallipadi@intel.com free_memtype(p->phys_addr, p->phys_addr + get_vm_area_size(p)); 426d7677d40Svenkatesh.pallipadi@intel.com 427e64c8aa0SThomas Gleixner /* Finally remove it */ 4286e92a5a6SThomas Gleixner o = remove_vm_area((void __force *)addr); 429e64c8aa0SThomas Gleixner BUG_ON(p != o || o == NULL); 430e64c8aa0SThomas Gleixner kfree(p); 431e64c8aa0SThomas Gleixner } 432e64c8aa0SThomas Gleixner EXPORT_SYMBOL(iounmap); 433e64c8aa0SThomas Gleixner 4341e6277deSJan Beulich int __init arch_ioremap_pud_supported(void) 4355d72b4fbSToshi Kani { 4365d72b4fbSToshi Kani #ifdef CONFIG_X86_64 437b8291adcSBorislav Petkov return boot_cpu_has(X86_FEATURE_GBPAGES); 4385d72b4fbSToshi Kani #else 4395d72b4fbSToshi Kani return 0; 4405d72b4fbSToshi Kani #endif 4415d72b4fbSToshi Kani } 4425d72b4fbSToshi Kani 4431e6277deSJan Beulich int __init arch_ioremap_pmd_supported(void) 4445d72b4fbSToshi Kani { 44516bf9226SBorislav Petkov return boot_cpu_has(X86_FEATURE_PSE); 4465d72b4fbSToshi Kani } 4475d72b4fbSToshi Kani 448e045fb2aSvenkatesh.pallipadi@intel.com /* 449e045fb2aSvenkatesh.pallipadi@intel.com * Convert a physical pointer to a virtual kernel pointer for /dev/mem 450e045fb2aSvenkatesh.pallipadi@intel.com * access 451e045fb2aSvenkatesh.pallipadi@intel.com */ 4524707a341SThierry Reding void *xlate_dev_mem_ptr(phys_addr_t phys) 453e045fb2aSvenkatesh.pallipadi@intel.com { 454e045fb2aSvenkatesh.pallipadi@intel.com unsigned long start = phys & PAGE_MASK; 45594d4b476SIngo Molnar unsigned long offset = phys & ~PAGE_MASK; 456562bfca4SIngo Molnar void *vaddr; 457e045fb2aSvenkatesh.pallipadi@intel.com 4588458bf94STom Lendacky /* memremap() maps if RAM, otherwise falls back to ioremap() */ 4598458bf94STom Lendacky vaddr = memremap(start, PAGE_SIZE, MEMREMAP_WB); 460e045fb2aSvenkatesh.pallipadi@intel.com 4618458bf94STom Lendacky /* Only add the offset on success and return NULL if memremap() failed */ 46294d4b476SIngo Molnar if (vaddr) 46394d4b476SIngo Molnar vaddr += offset; 464e045fb2aSvenkatesh.pallipadi@intel.com 465562bfca4SIngo Molnar return vaddr; 466e045fb2aSvenkatesh.pallipadi@intel.com } 467e045fb2aSvenkatesh.pallipadi@intel.com 4684707a341SThierry Reding void unxlate_dev_mem_ptr(phys_addr_t phys, void *addr) 469e045fb2aSvenkatesh.pallipadi@intel.com { 4708458bf94STom Lendacky memunmap((void *)((unsigned long)addr & PAGE_MASK)); 471e045fb2aSvenkatesh.pallipadi@intel.com } 472e045fb2aSvenkatesh.pallipadi@intel.com 4738f716c9bSTom Lendacky /* 4748f716c9bSTom Lendacky * Examine the physical address to determine if it is an area of memory 4758f716c9bSTom Lendacky * that should be mapped decrypted. If the memory is not part of the 4768f716c9bSTom Lendacky * kernel usable area it was accessed and created decrypted, so these 4771de32862STom Lendacky * areas should be mapped decrypted. And since the encryption key can 4781de32862STom Lendacky * change across reboots, persistent memory should also be mapped 4791de32862STom Lendacky * decrypted. 480072f58c6STom Lendacky * 481072f58c6STom Lendacky * If SEV is active, that implies that BIOS/UEFI also ran encrypted so 482072f58c6STom Lendacky * only persistent memory should be mapped decrypted. 4838f716c9bSTom Lendacky */ 4848f716c9bSTom Lendacky static bool memremap_should_map_decrypted(resource_size_t phys_addr, 4858f716c9bSTom Lendacky unsigned long size) 4868f716c9bSTom Lendacky { 4871de32862STom Lendacky int is_pmem; 4881de32862STom Lendacky 4891de32862STom Lendacky /* 4901de32862STom Lendacky * Check if the address is part of a persistent memory region. 4911de32862STom Lendacky * This check covers areas added by E820, EFI and ACPI. 4921de32862STom Lendacky */ 4931de32862STom Lendacky is_pmem = region_intersects(phys_addr, size, IORESOURCE_MEM, 4941de32862STom Lendacky IORES_DESC_PERSISTENT_MEMORY); 4951de32862STom Lendacky if (is_pmem != REGION_DISJOINT) 4961de32862STom Lendacky return true; 4971de32862STom Lendacky 4981de32862STom Lendacky /* 4991de32862STom Lendacky * Check if the non-volatile attribute is set for an EFI 5001de32862STom Lendacky * reserved area. 5011de32862STom Lendacky */ 5021de32862STom Lendacky if (efi_enabled(EFI_BOOT)) { 5031de32862STom Lendacky switch (efi_mem_type(phys_addr)) { 5041de32862STom Lendacky case EFI_RESERVED_TYPE: 5051de32862STom Lendacky if (efi_mem_attributes(phys_addr) & EFI_MEMORY_NV) 5061de32862STom Lendacky return true; 5071de32862STom Lendacky break; 5081de32862STom Lendacky default: 5091de32862STom Lendacky break; 5101de32862STom Lendacky } 5111de32862STom Lendacky } 5121de32862STom Lendacky 5138f716c9bSTom Lendacky /* Check if the address is outside kernel usable area */ 5148f716c9bSTom Lendacky switch (e820__get_entry_type(phys_addr, phys_addr + size - 1)) { 5158f716c9bSTom Lendacky case E820_TYPE_RESERVED: 5168f716c9bSTom Lendacky case E820_TYPE_ACPI: 5178f716c9bSTom Lendacky case E820_TYPE_NVS: 5188f716c9bSTom Lendacky case E820_TYPE_UNUSABLE: 519072f58c6STom Lendacky /* For SEV, these areas are encrypted */ 520072f58c6STom Lendacky if (sev_active()) 521072f58c6STom Lendacky break; 522072f58c6STom Lendacky /* Fallthrough */ 523072f58c6STom Lendacky 5241de32862STom Lendacky case E820_TYPE_PRAM: 5258f716c9bSTom Lendacky return true; 5268f716c9bSTom Lendacky default: 5278f716c9bSTom Lendacky break; 5288f716c9bSTom Lendacky } 5298f716c9bSTom Lendacky 5308f716c9bSTom Lendacky return false; 5318f716c9bSTom Lendacky } 5328f716c9bSTom Lendacky 5338f716c9bSTom Lendacky /* 5348f716c9bSTom Lendacky * Examine the physical address to determine if it is EFI data. Check 5358f716c9bSTom Lendacky * it against the boot params structure and EFI tables and memory types. 5368f716c9bSTom Lendacky */ 5378f716c9bSTom Lendacky static bool memremap_is_efi_data(resource_size_t phys_addr, 5388f716c9bSTom Lendacky unsigned long size) 5398f716c9bSTom Lendacky { 5408f716c9bSTom Lendacky u64 paddr; 5418f716c9bSTom Lendacky 5428f716c9bSTom Lendacky /* Check if the address is part of EFI boot/runtime data */ 5438f716c9bSTom Lendacky if (!efi_enabled(EFI_BOOT)) 5448f716c9bSTom Lendacky return false; 5458f716c9bSTom Lendacky 5468f716c9bSTom Lendacky paddr = boot_params.efi_info.efi_memmap_hi; 5478f716c9bSTom Lendacky paddr <<= 32; 5488f716c9bSTom Lendacky paddr |= boot_params.efi_info.efi_memmap; 5498f716c9bSTom Lendacky if (phys_addr == paddr) 5508f716c9bSTom Lendacky return true; 5518f716c9bSTom Lendacky 5528f716c9bSTom Lendacky paddr = boot_params.efi_info.efi_systab_hi; 5538f716c9bSTom Lendacky paddr <<= 32; 5548f716c9bSTom Lendacky paddr |= boot_params.efi_info.efi_systab; 5558f716c9bSTom Lendacky if (phys_addr == paddr) 5568f716c9bSTom Lendacky return true; 5578f716c9bSTom Lendacky 5588f716c9bSTom Lendacky if (efi_is_table_address(phys_addr)) 5598f716c9bSTom Lendacky return true; 5608f716c9bSTom Lendacky 5618f716c9bSTom Lendacky switch (efi_mem_type(phys_addr)) { 5628f716c9bSTom Lendacky case EFI_BOOT_SERVICES_DATA: 5638f716c9bSTom Lendacky case EFI_RUNTIME_SERVICES_DATA: 5648f716c9bSTom Lendacky return true; 5658f716c9bSTom Lendacky default: 5668f716c9bSTom Lendacky break; 5678f716c9bSTom Lendacky } 5688f716c9bSTom Lendacky 5698f716c9bSTom Lendacky return false; 5708f716c9bSTom Lendacky } 5718f716c9bSTom Lendacky 5728f716c9bSTom Lendacky /* 5738f716c9bSTom Lendacky * Examine the physical address to determine if it is boot data by checking 5748f716c9bSTom Lendacky * it against the boot params setup_data chain. 5758f716c9bSTom Lendacky */ 5768f716c9bSTom Lendacky static bool memremap_is_setup_data(resource_size_t phys_addr, 5778f716c9bSTom Lendacky unsigned long size) 5788f716c9bSTom Lendacky { 5798f716c9bSTom Lendacky struct setup_data *data; 5808f716c9bSTom Lendacky u64 paddr, paddr_next; 5818f716c9bSTom Lendacky 5828f716c9bSTom Lendacky paddr = boot_params.hdr.setup_data; 5838f716c9bSTom Lendacky while (paddr) { 5848f716c9bSTom Lendacky unsigned int len; 5858f716c9bSTom Lendacky 5868f716c9bSTom Lendacky if (phys_addr == paddr) 5878f716c9bSTom Lendacky return true; 5888f716c9bSTom Lendacky 5898f716c9bSTom Lendacky data = memremap(paddr, sizeof(*data), 5908f716c9bSTom Lendacky MEMREMAP_WB | MEMREMAP_DEC); 5918f716c9bSTom Lendacky 5928f716c9bSTom Lendacky paddr_next = data->next; 5938f716c9bSTom Lendacky len = data->len; 5948f716c9bSTom Lendacky 5958f716c9bSTom Lendacky memunmap(data); 5968f716c9bSTom Lendacky 5978f716c9bSTom Lendacky if ((phys_addr > paddr) && (phys_addr < (paddr + len))) 5988f716c9bSTom Lendacky return true; 5998f716c9bSTom Lendacky 6008f716c9bSTom Lendacky paddr = paddr_next; 6018f716c9bSTom Lendacky } 6028f716c9bSTom Lendacky 6038f716c9bSTom Lendacky return false; 6048f716c9bSTom Lendacky } 6058f716c9bSTom Lendacky 6068f716c9bSTom Lendacky /* 6078f716c9bSTom Lendacky * Examine the physical address to determine if it is boot data by checking 6088f716c9bSTom Lendacky * it against the boot params setup_data chain (early boot version). 6098f716c9bSTom Lendacky */ 6108f716c9bSTom Lendacky static bool __init early_memremap_is_setup_data(resource_size_t phys_addr, 6118f716c9bSTom Lendacky unsigned long size) 6128f716c9bSTom Lendacky { 6138f716c9bSTom Lendacky struct setup_data *data; 6148f716c9bSTom Lendacky u64 paddr, paddr_next; 6158f716c9bSTom Lendacky 6168f716c9bSTom Lendacky paddr = boot_params.hdr.setup_data; 6178f716c9bSTom Lendacky while (paddr) { 6188f716c9bSTom Lendacky unsigned int len; 6198f716c9bSTom Lendacky 6208f716c9bSTom Lendacky if (phys_addr == paddr) 6218f716c9bSTom Lendacky return true; 6228f716c9bSTom Lendacky 6238f716c9bSTom Lendacky data = early_memremap_decrypted(paddr, sizeof(*data)); 6248f716c9bSTom Lendacky 6258f716c9bSTom Lendacky paddr_next = data->next; 6268f716c9bSTom Lendacky len = data->len; 6278f716c9bSTom Lendacky 6288f716c9bSTom Lendacky early_memunmap(data, sizeof(*data)); 6298f716c9bSTom Lendacky 6308f716c9bSTom Lendacky if ((phys_addr > paddr) && (phys_addr < (paddr + len))) 6318f716c9bSTom Lendacky return true; 6328f716c9bSTom Lendacky 6338f716c9bSTom Lendacky paddr = paddr_next; 6348f716c9bSTom Lendacky } 6358f716c9bSTom Lendacky 6368f716c9bSTom Lendacky return false; 6378f716c9bSTom Lendacky } 6388f716c9bSTom Lendacky 6398f716c9bSTom Lendacky /* 6408f716c9bSTom Lendacky * Architecture function to determine if RAM remap is allowed. By default, a 6418f716c9bSTom Lendacky * RAM remap will map the data as encrypted. Determine if a RAM remap should 6428f716c9bSTom Lendacky * not be done so that the data will be mapped decrypted. 6438f716c9bSTom Lendacky */ 6448f716c9bSTom Lendacky bool arch_memremap_can_ram_remap(resource_size_t phys_addr, unsigned long size, 6458f716c9bSTom Lendacky unsigned long flags) 6468f716c9bSTom Lendacky { 647072f58c6STom Lendacky if (!mem_encrypt_active()) 6488f716c9bSTom Lendacky return true; 6498f716c9bSTom Lendacky 6508f716c9bSTom Lendacky if (flags & MEMREMAP_ENC) 6518f716c9bSTom Lendacky return true; 6528f716c9bSTom Lendacky 6538f716c9bSTom Lendacky if (flags & MEMREMAP_DEC) 6548f716c9bSTom Lendacky return false; 6558f716c9bSTom Lendacky 656072f58c6STom Lendacky if (sme_active()) { 6578f716c9bSTom Lendacky if (memremap_is_setup_data(phys_addr, size) || 658072f58c6STom Lendacky memremap_is_efi_data(phys_addr, size)) 6598f716c9bSTom Lendacky return false; 660072f58c6STom Lendacky } 6618f716c9bSTom Lendacky 662072f58c6STom Lendacky return !memremap_should_map_decrypted(phys_addr, size); 6638f716c9bSTom Lendacky } 6648f716c9bSTom Lendacky 6658f716c9bSTom Lendacky /* 6668f716c9bSTom Lendacky * Architecture override of __weak function to adjust the protection attributes 6678f716c9bSTom Lendacky * used when remapping memory. By default, early_memremap() will map the data 6688f716c9bSTom Lendacky * as encrypted. Determine if an encrypted mapping should not be done and set 6698f716c9bSTom Lendacky * the appropriate protection attributes. 6708f716c9bSTom Lendacky */ 6718f716c9bSTom Lendacky pgprot_t __init early_memremap_pgprot_adjust(resource_size_t phys_addr, 6728f716c9bSTom Lendacky unsigned long size, 6738f716c9bSTom Lendacky pgprot_t prot) 6748f716c9bSTom Lendacky { 675072f58c6STom Lendacky bool encrypted_prot; 676072f58c6STom Lendacky 677072f58c6STom Lendacky if (!mem_encrypt_active()) 6788f716c9bSTom Lendacky return prot; 6798f716c9bSTom Lendacky 680072f58c6STom Lendacky encrypted_prot = true; 681072f58c6STom Lendacky 682072f58c6STom Lendacky if (sme_active()) { 6838f716c9bSTom Lendacky if (early_memremap_is_setup_data(phys_addr, size) || 684072f58c6STom Lendacky memremap_is_efi_data(phys_addr, size)) 685072f58c6STom Lendacky encrypted_prot = false; 686072f58c6STom Lendacky } 6878f716c9bSTom Lendacky 688072f58c6STom Lendacky if (encrypted_prot && memremap_should_map_decrypted(phys_addr, size)) 689072f58c6STom Lendacky encrypted_prot = false; 690072f58c6STom Lendacky 691072f58c6STom Lendacky return encrypted_prot ? pgprot_encrypted(prot) 692072f58c6STom Lendacky : pgprot_decrypted(prot); 6938f716c9bSTom Lendacky } 6948f716c9bSTom Lendacky 6958458bf94STom Lendacky bool phys_mem_access_encrypted(unsigned long phys_addr, unsigned long size) 6968458bf94STom Lendacky { 6978458bf94STom Lendacky return arch_memremap_can_ram_remap(phys_addr, size, 0); 6988458bf94STom Lendacky } 6998458bf94STom Lendacky 700f88a68faSTom Lendacky #ifdef CONFIG_ARCH_USE_MEMREMAP_PROT 701f88a68faSTom Lendacky /* Remap memory with encryption */ 702f88a68faSTom Lendacky void __init *early_memremap_encrypted(resource_size_t phys_addr, 703f88a68faSTom Lendacky unsigned long size) 704f88a68faSTom Lendacky { 705f88a68faSTom Lendacky return early_memremap_prot(phys_addr, size, __PAGE_KERNEL_ENC); 706f88a68faSTom Lendacky } 707f88a68faSTom Lendacky 708f88a68faSTom Lendacky /* 709f88a68faSTom Lendacky * Remap memory with encryption and write-protected - cannot be called 710f88a68faSTom Lendacky * before pat_init() is called 711f88a68faSTom Lendacky */ 712f88a68faSTom Lendacky void __init *early_memremap_encrypted_wp(resource_size_t phys_addr, 713f88a68faSTom Lendacky unsigned long size) 714f88a68faSTom Lendacky { 715f88a68faSTom Lendacky /* Be sure the write-protect PAT entry is set for write-protect */ 716f88a68faSTom Lendacky if (__pte2cachemode_tbl[_PAGE_CACHE_MODE_WP] != _PAGE_CACHE_MODE_WP) 717f88a68faSTom Lendacky return NULL; 718f88a68faSTom Lendacky 719f88a68faSTom Lendacky return early_memremap_prot(phys_addr, size, __PAGE_KERNEL_ENC_WP); 720f88a68faSTom Lendacky } 721f88a68faSTom Lendacky 722f88a68faSTom Lendacky /* Remap memory without encryption */ 723f88a68faSTom Lendacky void __init *early_memremap_decrypted(resource_size_t phys_addr, 724f88a68faSTom Lendacky unsigned long size) 725f88a68faSTom Lendacky { 726f88a68faSTom Lendacky return early_memremap_prot(phys_addr, size, __PAGE_KERNEL_NOENC); 727f88a68faSTom Lendacky } 728f88a68faSTom Lendacky 729f88a68faSTom Lendacky /* 730f88a68faSTom Lendacky * Remap memory without encryption and write-protected - cannot be called 731f88a68faSTom Lendacky * before pat_init() is called 732f88a68faSTom Lendacky */ 733f88a68faSTom Lendacky void __init *early_memremap_decrypted_wp(resource_size_t phys_addr, 734f88a68faSTom Lendacky unsigned long size) 735f88a68faSTom Lendacky { 736f88a68faSTom Lendacky /* Be sure the write-protect PAT entry is set for write-protect */ 737f88a68faSTom Lendacky if (__pte2cachemode_tbl[_PAGE_CACHE_MODE_WP] != _PAGE_CACHE_MODE_WP) 738f88a68faSTom Lendacky return NULL; 739f88a68faSTom Lendacky 740f88a68faSTom Lendacky return early_memremap_prot(phys_addr, size, __PAGE_KERNEL_NOENC_WP); 741f88a68faSTom Lendacky } 742f88a68faSTom Lendacky #endif /* CONFIG_ARCH_USE_MEMREMAP_PROT */ 743f88a68faSTom Lendacky 74445c7b28fSJeremy Fitzhardinge static pte_t bm_pte[PAGE_SIZE/sizeof(pte_t)] __page_aligned_bss; 745e64c8aa0SThomas Gleixner 746551889a6SIan Campbell static inline pmd_t * __init early_ioremap_pmd(unsigned long addr) 747e64c8aa0SThomas Gleixner { 74837cc8d7fSJeremy Fitzhardinge /* Don't assume we're using swapper_pg_dir at this point */ 7496c690ee1SAndy Lutomirski pgd_t *base = __va(read_cr3_pa()); 75037cc8d7fSJeremy Fitzhardinge pgd_t *pgd = &base[pgd_index(addr)]; 751e0c4f675SKirill A. Shutemov p4d_t *p4d = p4d_offset(pgd, addr); 752e0c4f675SKirill A. Shutemov pud_t *pud = pud_offset(p4d, addr); 753551889a6SIan Campbell pmd_t *pmd = pmd_offset(pud, addr); 754551889a6SIan Campbell 755551889a6SIan Campbell return pmd; 756e64c8aa0SThomas Gleixner } 757e64c8aa0SThomas Gleixner 758551889a6SIan Campbell static inline pte_t * __init early_ioremap_pte(unsigned long addr) 759e64c8aa0SThomas Gleixner { 760551889a6SIan Campbell return &bm_pte[pte_index(addr)]; 761e64c8aa0SThomas Gleixner } 762e64c8aa0SThomas Gleixner 763fef5ba79SJeremy Fitzhardinge bool __init is_early_ioremap_ptep(pte_t *ptep) 764fef5ba79SJeremy Fitzhardinge { 765fef5ba79SJeremy Fitzhardinge return ptep >= &bm_pte[0] && ptep < &bm_pte[PAGE_SIZE/sizeof(pte_t)]; 766fef5ba79SJeremy Fitzhardinge } 767fef5ba79SJeremy Fitzhardinge 768e64c8aa0SThomas Gleixner void __init early_ioremap_init(void) 769e64c8aa0SThomas Gleixner { 770551889a6SIan Campbell pmd_t *pmd; 771e64c8aa0SThomas Gleixner 77273159fdcSAndy Lutomirski #ifdef CONFIG_X86_64 77373159fdcSAndy Lutomirski BUILD_BUG_ON((fix_to_virt(0) + PAGE_SIZE) & ((1 << PMD_SHIFT) - 1)); 77473159fdcSAndy Lutomirski #else 77573159fdcSAndy Lutomirski WARN_ON((fix_to_virt(0) + PAGE_SIZE) & ((1 << PMD_SHIFT) - 1)); 77673159fdcSAndy Lutomirski #endif 77773159fdcSAndy Lutomirski 7785b7c73e0SMark Salter early_ioremap_setup(); 7798827247fSWang Chen 780551889a6SIan Campbell pmd = early_ioremap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)); 781e64c8aa0SThomas Gleixner memset(bm_pte, 0, sizeof(bm_pte)); 782b6fbb669SIan Campbell pmd_populate_kernel(&init_mm, pmd, bm_pte); 783551889a6SIan Campbell 784e64c8aa0SThomas Gleixner /* 785551889a6SIan Campbell * The boot-ioremap range spans multiple pmds, for which 786e64c8aa0SThomas Gleixner * we are not prepared: 787e64c8aa0SThomas Gleixner */ 788499a5f1eSJan Beulich #define __FIXADDR_TOP (-PAGE_SIZE) 789499a5f1eSJan Beulich BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT) 790499a5f1eSJan Beulich != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT)); 791499a5f1eSJan Beulich #undef __FIXADDR_TOP 792551889a6SIan Campbell if (pmd != early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END))) { 793e64c8aa0SThomas Gleixner WARN_ON(1); 794551889a6SIan Campbell printk(KERN_WARNING "pmd %p != %p\n", 795551889a6SIan Campbell pmd, early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END))); 796e64c8aa0SThomas Gleixner printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n", 797e64c8aa0SThomas Gleixner fix_to_virt(FIX_BTMAP_BEGIN)); 798e64c8aa0SThomas Gleixner printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_END): %08lx\n", 799e64c8aa0SThomas Gleixner fix_to_virt(FIX_BTMAP_END)); 800e64c8aa0SThomas Gleixner 801e64c8aa0SThomas Gleixner printk(KERN_WARNING "FIX_BTMAP_END: %d\n", FIX_BTMAP_END); 802e64c8aa0SThomas Gleixner printk(KERN_WARNING "FIX_BTMAP_BEGIN: %d\n", 803e64c8aa0SThomas Gleixner FIX_BTMAP_BEGIN); 804e64c8aa0SThomas Gleixner } 805e64c8aa0SThomas Gleixner } 806e64c8aa0SThomas Gleixner 8075b7c73e0SMark Salter void __init __early_set_fixmap(enum fixed_addresses idx, 8089b987aebSMasami Hiramatsu phys_addr_t phys, pgprot_t flags) 809e64c8aa0SThomas Gleixner { 810551889a6SIan Campbell unsigned long addr = __fix_to_virt(idx); 811551889a6SIan Campbell pte_t *pte; 812e64c8aa0SThomas Gleixner 813e64c8aa0SThomas Gleixner if (idx >= __end_of_fixed_addresses) { 814e64c8aa0SThomas Gleixner BUG(); 815e64c8aa0SThomas Gleixner return; 816e64c8aa0SThomas Gleixner } 817e64c8aa0SThomas Gleixner pte = early_ioremap_pte(addr); 8184583ed51SJeremy Fitzhardinge 819e64c8aa0SThomas Gleixner if (pgprot_val(flags)) 820551889a6SIan Campbell set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags)); 821e64c8aa0SThomas Gleixner else 8224f9c11ddSJeremy Fitzhardinge pte_clear(&init_mm, addr, pte); 823e64c8aa0SThomas Gleixner __flush_tlb_one(addr); 824e64c8aa0SThomas Gleixner } 825