Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Re-map IO memory to kernel address space so that we can access it.
0004  * This is needed for high PCI addresses that aren't mapped in the
0005  * 640k-1MB IO memory area on PC's
0006  *
0007  * (C) Copyright 1995 1996 Linus Torvalds
0008  */
0009 
0010 #include <linux/memblock.h>
0011 #include <linux/init.h>
0012 #include <linux/io.h>
0013 #include <linux/ioport.h>
0014 #include <linux/slab.h>
0015 #include <linux/vmalloc.h>
0016 #include <linux/mmiotrace.h>
0017 #include <linux/cc_platform.h>
0018 #include <linux/efi.h>
0019 #include <linux/pgtable.h>
0020 
0021 #include <asm/set_memory.h>
0022 #include <asm/e820/api.h>
0023 #include <asm/efi.h>
0024 #include <asm/fixmap.h>
0025 #include <asm/tlbflush.h>
0026 #include <asm/pgalloc.h>
0027 #include <asm/memtype.h>
0028 #include <asm/setup.h>
0029 
0030 #include "physaddr.h"
0031 
0032 /*
0033  * Descriptor controlling ioremap() behavior.
0034  */
0035 struct ioremap_desc {
0036     unsigned int flags;
0037 };
0038 
0039 /*
0040  * Fix up the linear direct mapping of the kernel to avoid cache attribute
0041  * conflicts.
0042  */
0043 int ioremap_change_attr(unsigned long vaddr, unsigned long size,
0044             enum page_cache_mode pcm)
0045 {
0046     unsigned long nrpages = size >> PAGE_SHIFT;
0047     int err;
0048 
0049     switch (pcm) {
0050     case _PAGE_CACHE_MODE_UC:
0051     default:
0052         err = _set_memory_uc(vaddr, nrpages);
0053         break;
0054     case _PAGE_CACHE_MODE_WC:
0055         err = _set_memory_wc(vaddr, nrpages);
0056         break;
0057     case _PAGE_CACHE_MODE_WT:
0058         err = _set_memory_wt(vaddr, nrpages);
0059         break;
0060     case _PAGE_CACHE_MODE_WB:
0061         err = _set_memory_wb(vaddr, nrpages);
0062         break;
0063     }
0064 
0065     return err;
0066 }
0067 
0068 /* Does the range (or a subset of) contain normal RAM? */
0069 static unsigned int __ioremap_check_ram(struct resource *res)
0070 {
0071     unsigned long start_pfn, stop_pfn;
0072     unsigned long i;
0073 
0074     if ((res->flags & IORESOURCE_SYSTEM_RAM) != IORESOURCE_SYSTEM_RAM)
0075         return 0;
0076 
0077     start_pfn = (res->start + PAGE_SIZE - 1) >> PAGE_SHIFT;
0078     stop_pfn = (res->end + 1) >> PAGE_SHIFT;
0079     if (stop_pfn > start_pfn) {
0080         for (i = 0; i < (stop_pfn - start_pfn); ++i)
0081             if (pfn_valid(start_pfn + i) &&
0082                 !PageReserved(pfn_to_page(start_pfn + i)))
0083                 return IORES_MAP_SYSTEM_RAM;
0084     }
0085 
0086     return 0;
0087 }
0088 
0089 /*
0090  * In a SEV guest, NONE and RESERVED should not be mapped encrypted because
0091  * there the whole memory is already encrypted.
0092  */
0093 static unsigned int __ioremap_check_encrypted(struct resource *res)
0094 {
0095     if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
0096         return 0;
0097 
0098     switch (res->desc) {
0099     case IORES_DESC_NONE:
0100     case IORES_DESC_RESERVED:
0101         break;
0102     default:
0103         return IORES_MAP_ENCRYPTED;
0104     }
0105 
0106     return 0;
0107 }
0108 
0109 /*
0110  * The EFI runtime services data area is not covered by walk_mem_res(), but must
0111  * be mapped encrypted when SEV is active.
0112  */
0113 static void __ioremap_check_other(resource_size_t addr, struct ioremap_desc *desc)
0114 {
0115     if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
0116         return;
0117 
0118     if (!IS_ENABLED(CONFIG_EFI))
0119         return;
0120 
0121     if (efi_mem_type(addr) == EFI_RUNTIME_SERVICES_DATA ||
0122         (efi_mem_type(addr) == EFI_BOOT_SERVICES_DATA &&
0123          efi_mem_attributes(addr) & EFI_MEMORY_RUNTIME))
0124         desc->flags |= IORES_MAP_ENCRYPTED;
0125 }
0126 
0127 static int __ioremap_collect_map_flags(struct resource *res, void *arg)
0128 {
0129     struct ioremap_desc *desc = arg;
0130 
0131     if (!(desc->flags & IORES_MAP_SYSTEM_RAM))
0132         desc->flags |= __ioremap_check_ram(res);
0133 
0134     if (!(desc->flags & IORES_MAP_ENCRYPTED))
0135         desc->flags |= __ioremap_check_encrypted(res);
0136 
0137     return ((desc->flags & (IORES_MAP_SYSTEM_RAM | IORES_MAP_ENCRYPTED)) ==
0138                    (IORES_MAP_SYSTEM_RAM | IORES_MAP_ENCRYPTED));
0139 }
0140 
0141 /*
0142  * To avoid multiple resource walks, this function walks resources marked as
0143  * IORESOURCE_MEM and IORESOURCE_BUSY and looking for system RAM and/or a
0144  * resource described not as IORES_DESC_NONE (e.g. IORES_DESC_ACPI_TABLES).
0145  *
0146  * After that, deal with misc other ranges in __ioremap_check_other() which do
0147  * not fall into the above category.
0148  */
0149 static void __ioremap_check_mem(resource_size_t addr, unsigned long size,
0150                 struct ioremap_desc *desc)
0151 {
0152     u64 start, end;
0153 
0154     start = (u64)addr;
0155     end = start + size - 1;
0156     memset(desc, 0, sizeof(struct ioremap_desc));
0157 
0158     walk_mem_res(start, end, desc, __ioremap_collect_map_flags);
0159 
0160     __ioremap_check_other(addr, desc);
0161 }
0162 
0163 /*
0164  * Remap an arbitrary physical address space into the kernel virtual
0165  * address space. It transparently creates kernel huge I/O mapping when
0166  * the physical address is aligned by a huge page size (1GB or 2MB) and
0167  * the requested size is at least the huge page size.
0168  *
0169  * NOTE: MTRRs can override PAT memory types with a 4KB granularity.
0170  * Therefore, the mapping code falls back to use a smaller page toward 4KB
0171  * when a mapping range is covered by non-WB type of MTRRs.
0172  *
0173  * NOTE! We need to allow non-page-aligned mappings too: we will obviously
0174  * have to convert them into an offset in a page-aligned mapping, but the
0175  * caller shouldn't need to know that small detail.
0176  */
0177 static void __iomem *
0178 __ioremap_caller(resource_size_t phys_addr, unsigned long size,
0179          enum page_cache_mode pcm, void *caller, bool encrypted)
0180 {
0181     unsigned long offset, vaddr;
0182     resource_size_t last_addr;
0183     const resource_size_t unaligned_phys_addr = phys_addr;
0184     const unsigned long unaligned_size = size;
0185     struct ioremap_desc io_desc;
0186     struct vm_struct *area;
0187     enum page_cache_mode new_pcm;
0188     pgprot_t prot;
0189     int retval;
0190     void __iomem *ret_addr;
0191 
0192     /* Don't allow wraparound or zero size */
0193     last_addr = phys_addr + size - 1;
0194     if (!size || last_addr < phys_addr)
0195         return NULL;
0196 
0197     if (!phys_addr_valid(phys_addr)) {
0198         printk(KERN_WARNING "ioremap: invalid physical address %llx\n",
0199                (unsigned long long)phys_addr);
0200         WARN_ON_ONCE(1);
0201         return NULL;
0202     }
0203 
0204     __ioremap_check_mem(phys_addr, size, &io_desc);
0205 
0206     /*
0207      * Don't allow anybody to remap normal RAM that we're using..
0208      */
0209     if (io_desc.flags & IORES_MAP_SYSTEM_RAM) {
0210         WARN_ONCE(1, "ioremap on RAM at %pa - %pa\n",
0211               &phys_addr, &last_addr);
0212         return NULL;
0213     }
0214 
0215     /*
0216      * Mappings have to be page-aligned
0217      */
0218     offset = phys_addr & ~PAGE_MASK;
0219     phys_addr &= PHYSICAL_PAGE_MASK;
0220     size = PAGE_ALIGN(last_addr+1) - phys_addr;
0221 
0222     retval = memtype_reserve(phys_addr, (u64)phys_addr + size,
0223                         pcm, &new_pcm);
0224     if (retval) {
0225         printk(KERN_ERR "ioremap memtype_reserve failed %d\n", retval);
0226         return NULL;
0227     }
0228 
0229     if (pcm != new_pcm) {
0230         if (!is_new_memtype_allowed(phys_addr, size, pcm, new_pcm)) {
0231             printk(KERN_ERR
0232         "ioremap error for 0x%llx-0x%llx, requested 0x%x, got 0x%x\n",
0233                 (unsigned long long)phys_addr,
0234                 (unsigned long long)(phys_addr + size),
0235                 pcm, new_pcm);
0236             goto err_free_memtype;
0237         }
0238         pcm = new_pcm;
0239     }
0240 
0241     /*
0242      * If the page being mapped is in memory and SEV is active then
0243      * make sure the memory encryption attribute is enabled in the
0244      * resulting mapping.
0245      * In TDX guests, memory is marked private by default. If encryption
0246      * is not requested (using encrypted), explicitly set decrypt
0247      * attribute in all IOREMAPPED memory.
0248      */
0249     prot = PAGE_KERNEL_IO;
0250     if ((io_desc.flags & IORES_MAP_ENCRYPTED) || encrypted)
0251         prot = pgprot_encrypted(prot);
0252     else
0253         prot = pgprot_decrypted(prot);
0254 
0255     switch (pcm) {
0256     case _PAGE_CACHE_MODE_UC:
0257     default:
0258         prot = __pgprot(pgprot_val(prot) |
0259                 cachemode2protval(_PAGE_CACHE_MODE_UC));
0260         break;
0261     case _PAGE_CACHE_MODE_UC_MINUS:
0262         prot = __pgprot(pgprot_val(prot) |
0263                 cachemode2protval(_PAGE_CACHE_MODE_UC_MINUS));
0264         break;
0265     case _PAGE_CACHE_MODE_WC:
0266         prot = __pgprot(pgprot_val(prot) |
0267                 cachemode2protval(_PAGE_CACHE_MODE_WC));
0268         break;
0269     case _PAGE_CACHE_MODE_WT:
0270         prot = __pgprot(pgprot_val(prot) |
0271                 cachemode2protval(_PAGE_CACHE_MODE_WT));
0272         break;
0273     case _PAGE_CACHE_MODE_WB:
0274         break;
0275     }
0276 
0277     /*
0278      * Ok, go for it..
0279      */
0280     area = get_vm_area_caller(size, VM_IOREMAP, caller);
0281     if (!area)
0282         goto err_free_memtype;
0283     area->phys_addr = phys_addr;
0284     vaddr = (unsigned long) area->addr;
0285 
0286     if (memtype_kernel_map_sync(phys_addr, size, pcm))
0287         goto err_free_area;
0288 
0289     if (ioremap_page_range(vaddr, vaddr + size, phys_addr, prot))
0290         goto err_free_area;
0291 
0292     ret_addr = (void __iomem *) (vaddr + offset);
0293     mmiotrace_ioremap(unaligned_phys_addr, unaligned_size, ret_addr);
0294 
0295     /*
0296      * Check if the request spans more than any BAR in the iomem resource
0297      * tree.
0298      */
0299     if (iomem_map_sanity_check(unaligned_phys_addr, unaligned_size))
0300         pr_warn("caller %pS mapping multiple BARs\n", caller);
0301 
0302     return ret_addr;
0303 err_free_area:
0304     free_vm_area(area);
0305 err_free_memtype:
0306     memtype_free(phys_addr, phys_addr + size);
0307     return NULL;
0308 }
0309 
0310 /**
0311  * ioremap     -   map bus memory into CPU space
0312  * @phys_addr:    bus address of the memory
0313  * @size:      size of the resource to map
0314  *
0315  * ioremap performs a platform specific sequence of operations to
0316  * make bus memory CPU accessible via the readb/readw/readl/writeb/
0317  * writew/writel functions and the other mmio helpers. The returned
0318  * address is not guaranteed to be usable directly as a virtual
0319  * address.
0320  *
0321  * This version of ioremap ensures that the memory is marked uncachable
0322  * on the CPU as well as honouring existing caching rules from things like
0323  * the PCI bus. Note that there are other caches and buffers on many
0324  * busses. In particular driver authors should read up on PCI writes
0325  *
0326  * It's useful if some control registers are in such an area and
0327  * write combining or read caching is not desirable:
0328  *
0329  * Must be freed with iounmap.
0330  */
0331 void __iomem *ioremap(resource_size_t phys_addr, unsigned long size)
0332 {
0333     /*
0334      * Ideally, this should be:
0335      *  pat_enabled() ? _PAGE_CACHE_MODE_UC : _PAGE_CACHE_MODE_UC_MINUS;
0336      *
0337      * Till we fix all X drivers to use ioremap_wc(), we will use
0338      * UC MINUS. Drivers that are certain they need or can already
0339      * be converted over to strong UC can use ioremap_uc().
0340      */
0341     enum page_cache_mode pcm = _PAGE_CACHE_MODE_UC_MINUS;
0342 
0343     return __ioremap_caller(phys_addr, size, pcm,
0344                 __builtin_return_address(0), false);
0345 }
0346 EXPORT_SYMBOL(ioremap);
0347 
0348 /**
0349  * ioremap_uc     -   map bus memory into CPU space as strongly uncachable
0350  * @phys_addr:    bus address of the memory
0351  * @size:      size of the resource to map
0352  *
0353  * ioremap_uc performs a platform specific sequence of operations to
0354  * make bus memory CPU accessible via the readb/readw/readl/writeb/
0355  * writew/writel functions and the other mmio helpers. The returned
0356  * address is not guaranteed to be usable directly as a virtual
0357  * address.
0358  *
0359  * This version of ioremap ensures that the memory is marked with a strong
0360  * preference as completely uncachable on the CPU when possible. For non-PAT
0361  * systems this ends up setting page-attribute flags PCD=1, PWT=1. For PAT
0362  * systems this will set the PAT entry for the pages as strong UC.  This call
0363  * will honor existing caching rules from things like the PCI bus. Note that
0364  * there are other caches and buffers on many busses. In particular driver
0365  * authors should read up on PCI writes.
0366  *
0367  * It's useful if some control registers are in such an area and
0368  * write combining or read caching is not desirable:
0369  *
0370  * Must be freed with iounmap.
0371  */
0372 void __iomem *ioremap_uc(resource_size_t phys_addr, unsigned long size)
0373 {
0374     enum page_cache_mode pcm = _PAGE_CACHE_MODE_UC;
0375 
0376     return __ioremap_caller(phys_addr, size, pcm,
0377                 __builtin_return_address(0), false);
0378 }
0379 EXPORT_SYMBOL_GPL(ioremap_uc);
0380 
0381 /**
0382  * ioremap_wc   -   map memory into CPU space write combined
0383  * @phys_addr:  bus address of the memory
0384  * @size:   size of the resource to map
0385  *
0386  * This version of ioremap ensures that the memory is marked write combining.
0387  * Write combining allows faster writes to some hardware devices.
0388  *
0389  * Must be freed with iounmap.
0390  */
0391 void __iomem *ioremap_wc(resource_size_t phys_addr, unsigned long size)
0392 {
0393     return __ioremap_caller(phys_addr, size, _PAGE_CACHE_MODE_WC,
0394                     __builtin_return_address(0), false);
0395 }
0396 EXPORT_SYMBOL(ioremap_wc);
0397 
0398 /**
0399  * ioremap_wt   -   map memory into CPU space write through
0400  * @phys_addr:  bus address of the memory
0401  * @size:   size of the resource to map
0402  *
0403  * This version of ioremap ensures that the memory is marked write through.
0404  * Write through stores data into memory while keeping the cache up-to-date.
0405  *
0406  * Must be freed with iounmap.
0407  */
0408 void __iomem *ioremap_wt(resource_size_t phys_addr, unsigned long size)
0409 {
0410     return __ioremap_caller(phys_addr, size, _PAGE_CACHE_MODE_WT,
0411                     __builtin_return_address(0), false);
0412 }
0413 EXPORT_SYMBOL(ioremap_wt);
0414 
0415 void __iomem *ioremap_encrypted(resource_size_t phys_addr, unsigned long size)
0416 {
0417     return __ioremap_caller(phys_addr, size, _PAGE_CACHE_MODE_WB,
0418                 __builtin_return_address(0), true);
0419 }
0420 EXPORT_SYMBOL(ioremap_encrypted);
0421 
0422 void __iomem *ioremap_cache(resource_size_t phys_addr, unsigned long size)
0423 {
0424     return __ioremap_caller(phys_addr, size, _PAGE_CACHE_MODE_WB,
0425                 __builtin_return_address(0), false);
0426 }
0427 EXPORT_SYMBOL(ioremap_cache);
0428 
0429 void __iomem *ioremap_prot(resource_size_t phys_addr, unsigned long size,
0430                 unsigned long prot_val)
0431 {
0432     return __ioremap_caller(phys_addr, size,
0433                 pgprot2cachemode(__pgprot(prot_val)),
0434                 __builtin_return_address(0), false);
0435 }
0436 EXPORT_SYMBOL(ioremap_prot);
0437 
0438 /**
0439  * iounmap - Free a IO remapping
0440  * @addr: virtual address from ioremap_*
0441  *
0442  * Caller must ensure there is only one unmapping for the same pointer.
0443  */
0444 void iounmap(volatile void __iomem *addr)
0445 {
0446     struct vm_struct *p, *o;
0447 
0448     if ((void __force *)addr <= high_memory)
0449         return;
0450 
0451     /*
0452      * The PCI/ISA range special-casing was removed from __ioremap()
0453      * so this check, in theory, can be removed. However, there are
0454      * cases where iounmap() is called for addresses not obtained via
0455      * ioremap() (vga16fb for example). Add a warning so that these
0456      * cases can be caught and fixed.
0457      */
0458     if ((void __force *)addr >= phys_to_virt(ISA_START_ADDRESS) &&
0459         (void __force *)addr < phys_to_virt(ISA_END_ADDRESS)) {
0460         WARN(1, "iounmap() called for ISA range not obtained using ioremap()\n");
0461         return;
0462     }
0463 
0464     mmiotrace_iounmap(addr);
0465 
0466     addr = (volatile void __iomem *)
0467         (PAGE_MASK & (unsigned long __force)addr);
0468 
0469     /* Use the vm area unlocked, assuming the caller
0470        ensures there isn't another iounmap for the same address
0471        in parallel. Reuse of the virtual address is prevented by
0472        leaving it in the global lists until we're done with it.
0473        cpa takes care of the direct mappings. */
0474     p = find_vm_area((void __force *)addr);
0475 
0476     if (!p) {
0477         printk(KERN_ERR "iounmap: bad address %p\n", addr);
0478         dump_stack();
0479         return;
0480     }
0481 
0482     memtype_free(p->phys_addr, p->phys_addr + get_vm_area_size(p));
0483 
0484     /* Finally remove it */
0485     o = remove_vm_area((void __force *)addr);
0486     BUG_ON(p != o || o == NULL);
0487     kfree(p);
0488 }
0489 EXPORT_SYMBOL(iounmap);
0490 
0491 /*
0492  * Convert a physical pointer to a virtual kernel pointer for /dev/mem
0493  * access
0494  */
0495 void *xlate_dev_mem_ptr(phys_addr_t phys)
0496 {
0497     unsigned long start  = phys &  PAGE_MASK;
0498     unsigned long offset = phys & ~PAGE_MASK;
0499     void *vaddr;
0500 
0501     /* memremap() maps if RAM, otherwise falls back to ioremap() */
0502     vaddr = memremap(start, PAGE_SIZE, MEMREMAP_WB);
0503 
0504     /* Only add the offset on success and return NULL if memremap() failed */
0505     if (vaddr)
0506         vaddr += offset;
0507 
0508     return vaddr;
0509 }
0510 
0511 void unxlate_dev_mem_ptr(phys_addr_t phys, void *addr)
0512 {
0513     memunmap((void *)((unsigned long)addr & PAGE_MASK));
0514 }
0515 
0516 #ifdef CONFIG_AMD_MEM_ENCRYPT
0517 /*
0518  * Examine the physical address to determine if it is an area of memory
0519  * that should be mapped decrypted.  If the memory is not part of the
0520  * kernel usable area it was accessed and created decrypted, so these
0521  * areas should be mapped decrypted. And since the encryption key can
0522  * change across reboots, persistent memory should also be mapped
0523  * decrypted.
0524  *
0525  * If SEV is active, that implies that BIOS/UEFI also ran encrypted so
0526  * only persistent memory should be mapped decrypted.
0527  */
0528 static bool memremap_should_map_decrypted(resource_size_t phys_addr,
0529                       unsigned long size)
0530 {
0531     int is_pmem;
0532 
0533     /*
0534      * Check if the address is part of a persistent memory region.
0535      * This check covers areas added by E820, EFI and ACPI.
0536      */
0537     is_pmem = region_intersects(phys_addr, size, IORESOURCE_MEM,
0538                     IORES_DESC_PERSISTENT_MEMORY);
0539     if (is_pmem != REGION_DISJOINT)
0540         return true;
0541 
0542     /*
0543      * Check if the non-volatile attribute is set for an EFI
0544      * reserved area.
0545      */
0546     if (efi_enabled(EFI_BOOT)) {
0547         switch (efi_mem_type(phys_addr)) {
0548         case EFI_RESERVED_TYPE:
0549             if (efi_mem_attributes(phys_addr) & EFI_MEMORY_NV)
0550                 return true;
0551             break;
0552         default:
0553             break;
0554         }
0555     }
0556 
0557     /* Check if the address is outside kernel usable area */
0558     switch (e820__get_entry_type(phys_addr, phys_addr + size - 1)) {
0559     case E820_TYPE_RESERVED:
0560     case E820_TYPE_ACPI:
0561     case E820_TYPE_NVS:
0562     case E820_TYPE_UNUSABLE:
0563         /* For SEV, these areas are encrypted */
0564         if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
0565             break;
0566         fallthrough;
0567 
0568     case E820_TYPE_PRAM:
0569         return true;
0570     default:
0571         break;
0572     }
0573 
0574     return false;
0575 }
0576 
0577 /*
0578  * Examine the physical address to determine if it is EFI data. Check
0579  * it against the boot params structure and EFI tables and memory types.
0580  */
0581 static bool memremap_is_efi_data(resource_size_t phys_addr,
0582                  unsigned long size)
0583 {
0584     u64 paddr;
0585 
0586     /* Check if the address is part of EFI boot/runtime data */
0587     if (!efi_enabled(EFI_BOOT))
0588         return false;
0589 
0590     paddr = boot_params.efi_info.efi_memmap_hi;
0591     paddr <<= 32;
0592     paddr |= boot_params.efi_info.efi_memmap;
0593     if (phys_addr == paddr)
0594         return true;
0595 
0596     paddr = boot_params.efi_info.efi_systab_hi;
0597     paddr <<= 32;
0598     paddr |= boot_params.efi_info.efi_systab;
0599     if (phys_addr == paddr)
0600         return true;
0601 
0602     if (efi_is_table_address(phys_addr))
0603         return true;
0604 
0605     switch (efi_mem_type(phys_addr)) {
0606     case EFI_BOOT_SERVICES_DATA:
0607     case EFI_RUNTIME_SERVICES_DATA:
0608         return true;
0609     default:
0610         break;
0611     }
0612 
0613     return false;
0614 }
0615 
0616 /*
0617  * Examine the physical address to determine if it is boot data by checking
0618  * it against the boot params setup_data chain.
0619  */
0620 static bool memremap_is_setup_data(resource_size_t phys_addr,
0621                    unsigned long size)
0622 {
0623     struct setup_indirect *indirect;
0624     struct setup_data *data;
0625     u64 paddr, paddr_next;
0626 
0627     paddr = boot_params.hdr.setup_data;
0628     while (paddr) {
0629         unsigned int len;
0630 
0631         if (phys_addr == paddr)
0632             return true;
0633 
0634         data = memremap(paddr, sizeof(*data),
0635                 MEMREMAP_WB | MEMREMAP_DEC);
0636         if (!data) {
0637             pr_warn("failed to memremap setup_data entry\n");
0638             return false;
0639         }
0640 
0641         paddr_next = data->next;
0642         len = data->len;
0643 
0644         if ((phys_addr > paddr) && (phys_addr < (paddr + len))) {
0645             memunmap(data);
0646             return true;
0647         }
0648 
0649         if (data->type == SETUP_INDIRECT) {
0650             memunmap(data);
0651             data = memremap(paddr, sizeof(*data) + len,
0652                     MEMREMAP_WB | MEMREMAP_DEC);
0653             if (!data) {
0654                 pr_warn("failed to memremap indirect setup_data\n");
0655                 return false;
0656             }
0657 
0658             indirect = (struct setup_indirect *)data->data;
0659 
0660             if (indirect->type != SETUP_INDIRECT) {
0661                 paddr = indirect->addr;
0662                 len = indirect->len;
0663             }
0664         }
0665 
0666         memunmap(data);
0667 
0668         if ((phys_addr > paddr) && (phys_addr < (paddr + len)))
0669             return true;
0670 
0671         paddr = paddr_next;
0672     }
0673 
0674     return false;
0675 }
0676 
0677 /*
0678  * Examine the physical address to determine if it is boot data by checking
0679  * it against the boot params setup_data chain (early boot version).
0680  */
0681 static bool __init early_memremap_is_setup_data(resource_size_t phys_addr,
0682                         unsigned long size)
0683 {
0684     struct setup_indirect *indirect;
0685     struct setup_data *data;
0686     u64 paddr, paddr_next;
0687 
0688     paddr = boot_params.hdr.setup_data;
0689     while (paddr) {
0690         unsigned int len, size;
0691 
0692         if (phys_addr == paddr)
0693             return true;
0694 
0695         data = early_memremap_decrypted(paddr, sizeof(*data));
0696         if (!data) {
0697             pr_warn("failed to early memremap setup_data entry\n");
0698             return false;
0699         }
0700 
0701         size = sizeof(*data);
0702 
0703         paddr_next = data->next;
0704         len = data->len;
0705 
0706         if ((phys_addr > paddr) && (phys_addr < (paddr + len))) {
0707             early_memunmap(data, sizeof(*data));
0708             return true;
0709         }
0710 
0711         if (data->type == SETUP_INDIRECT) {
0712             size += len;
0713             early_memunmap(data, sizeof(*data));
0714             data = early_memremap_decrypted(paddr, size);
0715             if (!data) {
0716                 pr_warn("failed to early memremap indirect setup_data\n");
0717                 return false;
0718             }
0719 
0720             indirect = (struct setup_indirect *)data->data;
0721 
0722             if (indirect->type != SETUP_INDIRECT) {
0723                 paddr = indirect->addr;
0724                 len = indirect->len;
0725             }
0726         }
0727 
0728         early_memunmap(data, size);
0729 
0730         if ((phys_addr > paddr) && (phys_addr < (paddr + len)))
0731             return true;
0732 
0733         paddr = paddr_next;
0734     }
0735 
0736     return false;
0737 }
0738 
0739 /*
0740  * Architecture function to determine if RAM remap is allowed. By default, a
0741  * RAM remap will map the data as encrypted. Determine if a RAM remap should
0742  * not be done so that the data will be mapped decrypted.
0743  */
0744 bool arch_memremap_can_ram_remap(resource_size_t phys_addr, unsigned long size,
0745                  unsigned long flags)
0746 {
0747     if (!cc_platform_has(CC_ATTR_MEM_ENCRYPT))
0748         return true;
0749 
0750     if (flags & MEMREMAP_ENC)
0751         return true;
0752 
0753     if (flags & MEMREMAP_DEC)
0754         return false;
0755 
0756     if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) {
0757         if (memremap_is_setup_data(phys_addr, size) ||
0758             memremap_is_efi_data(phys_addr, size))
0759             return false;
0760     }
0761 
0762     return !memremap_should_map_decrypted(phys_addr, size);
0763 }
0764 
0765 /*
0766  * Architecture override of __weak function to adjust the protection attributes
0767  * used when remapping memory. By default, early_memremap() will map the data
0768  * as encrypted. Determine if an encrypted mapping should not be done and set
0769  * the appropriate protection attributes.
0770  */
0771 pgprot_t __init early_memremap_pgprot_adjust(resource_size_t phys_addr,
0772                          unsigned long size,
0773                          pgprot_t prot)
0774 {
0775     bool encrypted_prot;
0776 
0777     if (!cc_platform_has(CC_ATTR_MEM_ENCRYPT))
0778         return prot;
0779 
0780     encrypted_prot = true;
0781 
0782     if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) {
0783         if (early_memremap_is_setup_data(phys_addr, size) ||
0784             memremap_is_efi_data(phys_addr, size))
0785             encrypted_prot = false;
0786     }
0787 
0788     if (encrypted_prot && memremap_should_map_decrypted(phys_addr, size))
0789         encrypted_prot = false;
0790 
0791     return encrypted_prot ? pgprot_encrypted(prot)
0792                   : pgprot_decrypted(prot);
0793 }
0794 
0795 bool phys_mem_access_encrypted(unsigned long phys_addr, unsigned long size)
0796 {
0797     return arch_memremap_can_ram_remap(phys_addr, size, 0);
0798 }
0799 
0800 /* Remap memory with encryption */
0801 void __init *early_memremap_encrypted(resource_size_t phys_addr,
0802                       unsigned long size)
0803 {
0804     return early_memremap_prot(phys_addr, size, __PAGE_KERNEL_ENC);
0805 }
0806 
0807 /*
0808  * Remap memory with encryption and write-protected - cannot be called
0809  * before pat_init() is called
0810  */
0811 void __init *early_memremap_encrypted_wp(resource_size_t phys_addr,
0812                      unsigned long size)
0813 {
0814     if (!x86_has_pat_wp())
0815         return NULL;
0816     return early_memremap_prot(phys_addr, size, __PAGE_KERNEL_ENC_WP);
0817 }
0818 
0819 /* Remap memory without encryption */
0820 void __init *early_memremap_decrypted(resource_size_t phys_addr,
0821                       unsigned long size)
0822 {
0823     return early_memremap_prot(phys_addr, size, __PAGE_KERNEL_NOENC);
0824 }
0825 
0826 /*
0827  * Remap memory without encryption and write-protected - cannot be called
0828  * before pat_init() is called
0829  */
0830 void __init *early_memremap_decrypted_wp(resource_size_t phys_addr,
0831                      unsigned long size)
0832 {
0833     if (!x86_has_pat_wp())
0834         return NULL;
0835     return early_memremap_prot(phys_addr, size, __PAGE_KERNEL_NOENC_WP);
0836 }
0837 #endif  /* CONFIG_AMD_MEM_ENCRYPT */
0838 
0839 static pte_t bm_pte[PAGE_SIZE/sizeof(pte_t)] __page_aligned_bss;
0840 
0841 static inline pmd_t * __init early_ioremap_pmd(unsigned long addr)
0842 {
0843     /* Don't assume we're using swapper_pg_dir at this point */
0844     pgd_t *base = __va(read_cr3_pa());
0845     pgd_t *pgd = &base[pgd_index(addr)];
0846     p4d_t *p4d = p4d_offset(pgd, addr);
0847     pud_t *pud = pud_offset(p4d, addr);
0848     pmd_t *pmd = pmd_offset(pud, addr);
0849 
0850     return pmd;
0851 }
0852 
0853 static inline pte_t * __init early_ioremap_pte(unsigned long addr)
0854 {
0855     return &bm_pte[pte_index(addr)];
0856 }
0857 
0858 bool __init is_early_ioremap_ptep(pte_t *ptep)
0859 {
0860     return ptep >= &bm_pte[0] && ptep < &bm_pte[PAGE_SIZE/sizeof(pte_t)];
0861 }
0862 
0863 void __init early_ioremap_init(void)
0864 {
0865     pmd_t *pmd;
0866 
0867 #ifdef CONFIG_X86_64
0868     BUILD_BUG_ON((fix_to_virt(0) + PAGE_SIZE) & ((1 << PMD_SHIFT) - 1));
0869 #else
0870     WARN_ON((fix_to_virt(0) + PAGE_SIZE) & ((1 << PMD_SHIFT) - 1));
0871 #endif
0872 
0873     early_ioremap_setup();
0874 
0875     pmd = early_ioremap_pmd(fix_to_virt(FIX_BTMAP_BEGIN));
0876     memset(bm_pte, 0, sizeof(bm_pte));
0877     pmd_populate_kernel(&init_mm, pmd, bm_pte);
0878 
0879     /*
0880      * The boot-ioremap range spans multiple pmds, for which
0881      * we are not prepared:
0882      */
0883 #define __FIXADDR_TOP (-PAGE_SIZE)
0884     BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
0885              != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
0886 #undef __FIXADDR_TOP
0887     if (pmd != early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END))) {
0888         WARN_ON(1);
0889         printk(KERN_WARNING "pmd %p != %p\n",
0890                pmd, early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END)));
0891         printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
0892             fix_to_virt(FIX_BTMAP_BEGIN));
0893         printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_END):   %08lx\n",
0894             fix_to_virt(FIX_BTMAP_END));
0895 
0896         printk(KERN_WARNING "FIX_BTMAP_END:       %d\n", FIX_BTMAP_END);
0897         printk(KERN_WARNING "FIX_BTMAP_BEGIN:     %d\n",
0898                FIX_BTMAP_BEGIN);
0899     }
0900 }
0901 
0902 void __init __early_set_fixmap(enum fixed_addresses idx,
0903                    phys_addr_t phys, pgprot_t flags)
0904 {
0905     unsigned long addr = __fix_to_virt(idx);
0906     pte_t *pte;
0907 
0908     if (idx >= __end_of_fixed_addresses) {
0909         BUG();
0910         return;
0911     }
0912     pte = early_ioremap_pte(addr);
0913 
0914     /* Sanitize 'prot' against any unsupported bits: */
0915     pgprot_val(flags) &= __supported_pte_mask;
0916 
0917     if (pgprot_val(flags))
0918         set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
0919     else
0920         pte_clear(&init_mm, addr, pte);
0921     flush_tlb_one_kernel(addr);
0922 }