1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Re-map IO memory to kernel address space so that we can access it. 4 * This is needed for high PCI addresses that aren't mapped in the 5 * 640k-1MB IO memory area on PC's 6 * 7 * (C) Copyright 1995 1996 Linus Torvalds 8 */ 9 10 #include <linux/memblock.h> 11 #include <linux/init.h> 12 #include <linux/io.h> 13 #include <linux/ioport.h> 14 #include <linux/slab.h> 15 #include <linux/vmalloc.h> 16 #include <linux/mmiotrace.h> 17 #include <linux/mem_encrypt.h> 18 #include <linux/efi.h> 19 20 #include <asm/set_memory.h> 21 #include <asm/e820/api.h> 22 #include <asm/efi.h> 23 #include <asm/fixmap.h> 24 #include <asm/pgtable.h> 25 #include <asm/tlbflush.h> 26 #include <asm/pgalloc.h> 27 #include <asm/memtype.h> 28 #include <asm/setup.h> 29 30 #include "physaddr.h" 31 32 /* 33 * Descriptor controlling ioremap() behavior. 34 */ 35 struct ioremap_desc { 36 unsigned int flags; 37 }; 38 39 /* 40 * Fix up the linear direct mapping of the kernel to avoid cache attribute 41 * conflicts. 42 */ 43 int ioremap_change_attr(unsigned long vaddr, unsigned long size, 44 enum page_cache_mode pcm) 45 { 46 unsigned long nrpages = size >> PAGE_SHIFT; 47 int err; 48 49 switch (pcm) { 50 case _PAGE_CACHE_MODE_UC: 51 default: 52 err = _set_memory_uc(vaddr, nrpages); 53 break; 54 case _PAGE_CACHE_MODE_WC: 55 err = _set_memory_wc(vaddr, nrpages); 56 break; 57 case _PAGE_CACHE_MODE_WT: 58 err = _set_memory_wt(vaddr, nrpages); 59 break; 60 case _PAGE_CACHE_MODE_WB: 61 err = _set_memory_wb(vaddr, nrpages); 62 break; 63 } 64 65 return err; 66 } 67 68 /* Does the range (or a subset of) contain normal RAM? */ 69 static unsigned int __ioremap_check_ram(struct resource *res) 70 { 71 unsigned long start_pfn, stop_pfn; 72 unsigned long i; 73 74 if ((res->flags & IORESOURCE_SYSTEM_RAM) != IORESOURCE_SYSTEM_RAM) 75 return 0; 76 77 start_pfn = (res->start + PAGE_SIZE - 1) >> PAGE_SHIFT; 78 stop_pfn = (res->end + 1) >> PAGE_SHIFT; 79 if (stop_pfn > start_pfn) { 80 for (i = 0; i < (stop_pfn - start_pfn); ++i) 81 if (pfn_valid(start_pfn + i) && 82 !PageReserved(pfn_to_page(start_pfn + i))) 83 return IORES_MAP_SYSTEM_RAM; 84 } 85 86 return 0; 87 } 88 89 /* 90 * In a SEV guest, NONE and RESERVED should not be mapped encrypted because 91 * there the whole memory is already encrypted. 92 */ 93 static unsigned int __ioremap_check_encrypted(struct resource *res) 94 { 95 if (!sev_active()) 96 return 0; 97 98 switch (res->desc) { 99 case IORES_DESC_NONE: 100 case IORES_DESC_RESERVED: 101 break; 102 default: 103 return IORES_MAP_ENCRYPTED; 104 } 105 106 return 0; 107 } 108 109 /* 110 * The EFI runtime services data area is not covered by walk_mem_res(), but must 111 * be mapped encrypted when SEV is active. 112 */ 113 static void __ioremap_check_other(resource_size_t addr, struct ioremap_desc *desc) 114 { 115 if (!sev_active()) 116 return; 117 118 if (efi_mem_type(addr) == EFI_RUNTIME_SERVICES_DATA) 119 desc->flags |= IORES_MAP_ENCRYPTED; 120 } 121 122 static int __ioremap_collect_map_flags(struct resource *res, void *arg) 123 { 124 struct ioremap_desc *desc = arg; 125 126 if (!(desc->flags & IORES_MAP_SYSTEM_RAM)) 127 desc->flags |= __ioremap_check_ram(res); 128 129 if (!(desc->flags & IORES_MAP_ENCRYPTED)) 130 desc->flags |= __ioremap_check_encrypted(res); 131 132 return ((desc->flags & (IORES_MAP_SYSTEM_RAM | IORES_MAP_ENCRYPTED)) == 133 (IORES_MAP_SYSTEM_RAM | IORES_MAP_ENCRYPTED)); 134 } 135 136 /* 137 * To avoid multiple resource walks, this function walks resources marked as 138 * IORESOURCE_MEM and IORESOURCE_BUSY and looking for system RAM and/or a 139 * resource described not as IORES_DESC_NONE (e.g. IORES_DESC_ACPI_TABLES). 140 * 141 * After that, deal with misc other ranges in __ioremap_check_other() which do 142 * not fall into the above category. 143 */ 144 static void __ioremap_check_mem(resource_size_t addr, unsigned long size, 145 struct ioremap_desc *desc) 146 { 147 u64 start, end; 148 149 start = (u64)addr; 150 end = start + size - 1; 151 memset(desc, 0, sizeof(struct ioremap_desc)); 152 153 walk_mem_res(start, end, desc, __ioremap_collect_map_flags); 154 155 __ioremap_check_other(addr, desc); 156 } 157 158 /* 159 * Remap an arbitrary physical address space into the kernel virtual 160 * address space. It transparently creates kernel huge I/O mapping when 161 * the physical address is aligned by a huge page size (1GB or 2MB) and 162 * the requested size is at least the huge page size. 163 * 164 * NOTE: MTRRs can override PAT memory types with a 4KB granularity. 165 * Therefore, the mapping code falls back to use a smaller page toward 4KB 166 * when a mapping range is covered by non-WB type of MTRRs. 167 * 168 * NOTE! We need to allow non-page-aligned mappings too: we will obviously 169 * have to convert them into an offset in a page-aligned mapping, but the 170 * caller shouldn't need to know that small detail. 171 */ 172 static void __iomem * 173 __ioremap_caller(resource_size_t phys_addr, unsigned long size, 174 enum page_cache_mode pcm, void *caller, bool encrypted) 175 { 176 unsigned long offset, vaddr; 177 resource_size_t last_addr; 178 const resource_size_t unaligned_phys_addr = phys_addr; 179 const unsigned long unaligned_size = size; 180 struct ioremap_desc io_desc; 181 struct vm_struct *area; 182 enum page_cache_mode new_pcm; 183 pgprot_t prot; 184 int retval; 185 void __iomem *ret_addr; 186 187 /* Don't allow wraparound or zero size */ 188 last_addr = phys_addr + size - 1; 189 if (!size || last_addr < phys_addr) 190 return NULL; 191 192 if (!phys_addr_valid(phys_addr)) { 193 printk(KERN_WARNING "ioremap: invalid physical address %llx\n", 194 (unsigned long long)phys_addr); 195 WARN_ON_ONCE(1); 196 return NULL; 197 } 198 199 __ioremap_check_mem(phys_addr, size, &io_desc); 200 201 /* 202 * Don't allow anybody to remap normal RAM that we're using.. 203 */ 204 if (io_desc.flags & IORES_MAP_SYSTEM_RAM) { 205 WARN_ONCE(1, "ioremap on RAM at %pa - %pa\n", 206 &phys_addr, &last_addr); 207 return NULL; 208 } 209 210 /* 211 * Mappings have to be page-aligned 212 */ 213 offset = phys_addr & ~PAGE_MASK; 214 phys_addr &= PHYSICAL_PAGE_MASK; 215 size = PAGE_ALIGN(last_addr+1) - phys_addr; 216 217 retval = memtype_reserve(phys_addr, (u64)phys_addr + size, 218 pcm, &new_pcm); 219 if (retval) { 220 printk(KERN_ERR "ioremap memtype_reserve failed %d\n", retval); 221 return NULL; 222 } 223 224 if (pcm != new_pcm) { 225 if (!is_new_memtype_allowed(phys_addr, size, pcm, new_pcm)) { 226 printk(KERN_ERR 227 "ioremap error for 0x%llx-0x%llx, requested 0x%x, got 0x%x\n", 228 (unsigned long long)phys_addr, 229 (unsigned long long)(phys_addr + size), 230 pcm, new_pcm); 231 goto err_free_memtype; 232 } 233 pcm = new_pcm; 234 } 235 236 /* 237 * If the page being mapped is in memory and SEV is active then 238 * make sure the memory encryption attribute is enabled in the 239 * resulting mapping. 240 */ 241 prot = PAGE_KERNEL_IO; 242 if ((io_desc.flags & IORES_MAP_ENCRYPTED) || encrypted) 243 prot = pgprot_encrypted(prot); 244 245 switch (pcm) { 246 case _PAGE_CACHE_MODE_UC: 247 default: 248 prot = __pgprot(pgprot_val(prot) | 249 cachemode2protval(_PAGE_CACHE_MODE_UC)); 250 break; 251 case _PAGE_CACHE_MODE_UC_MINUS: 252 prot = __pgprot(pgprot_val(prot) | 253 cachemode2protval(_PAGE_CACHE_MODE_UC_MINUS)); 254 break; 255 case _PAGE_CACHE_MODE_WC: 256 prot = __pgprot(pgprot_val(prot) | 257 cachemode2protval(_PAGE_CACHE_MODE_WC)); 258 break; 259 case _PAGE_CACHE_MODE_WT: 260 prot = __pgprot(pgprot_val(prot) | 261 cachemode2protval(_PAGE_CACHE_MODE_WT)); 262 break; 263 case _PAGE_CACHE_MODE_WB: 264 break; 265 } 266 267 /* 268 * Ok, go for it.. 269 */ 270 area = get_vm_area_caller(size, VM_IOREMAP, caller); 271 if (!area) 272 goto err_free_memtype; 273 area->phys_addr = phys_addr; 274 vaddr = (unsigned long) area->addr; 275 276 if (memtype_kernel_map_sync(phys_addr, size, pcm)) 277 goto err_free_area; 278 279 if (ioremap_page_range(vaddr, vaddr + size, phys_addr, prot)) 280 goto err_free_area; 281 282 ret_addr = (void __iomem *) (vaddr + offset); 283 mmiotrace_ioremap(unaligned_phys_addr, unaligned_size, ret_addr); 284 285 /* 286 * Check if the request spans more than any BAR in the iomem resource 287 * tree. 288 */ 289 if (iomem_map_sanity_check(unaligned_phys_addr, unaligned_size)) 290 pr_warn("caller %pS mapping multiple BARs\n", caller); 291 292 return ret_addr; 293 err_free_area: 294 free_vm_area(area); 295 err_free_memtype: 296 memtype_free(phys_addr, phys_addr + size); 297 return NULL; 298 } 299 300 /** 301 * ioremap - map bus memory into CPU space 302 * @phys_addr: bus address of the memory 303 * @size: size of the resource to map 304 * 305 * ioremap performs a platform specific sequence of operations to 306 * make bus memory CPU accessible via the readb/readw/readl/writeb/ 307 * writew/writel functions and the other mmio helpers. The returned 308 * address is not guaranteed to be usable directly as a virtual 309 * address. 310 * 311 * This version of ioremap ensures that the memory is marked uncachable 312 * on the CPU as well as honouring existing caching rules from things like 313 * the PCI bus. Note that there are other caches and buffers on many 314 * busses. In particular driver authors should read up on PCI writes 315 * 316 * It's useful if some control registers are in such an area and 317 * write combining or read caching is not desirable: 318 * 319 * Must be freed with iounmap. 320 */ 321 void __iomem *ioremap(resource_size_t phys_addr, unsigned long size) 322 { 323 /* 324 * Ideally, this should be: 325 * pat_enabled() ? _PAGE_CACHE_MODE_UC : _PAGE_CACHE_MODE_UC_MINUS; 326 * 327 * Till we fix all X drivers to use ioremap_wc(), we will use 328 * UC MINUS. Drivers that are certain they need or can already 329 * be converted over to strong UC can use ioremap_uc(). 330 */ 331 enum page_cache_mode pcm = _PAGE_CACHE_MODE_UC_MINUS; 332 333 return __ioremap_caller(phys_addr, size, pcm, 334 __builtin_return_address(0), false); 335 } 336 EXPORT_SYMBOL(ioremap); 337 338 /** 339 * ioremap_uc - map bus memory into CPU space as strongly uncachable 340 * @phys_addr: bus address of the memory 341 * @size: size of the resource to map 342 * 343 * ioremap_uc performs a platform specific sequence of operations to 344 * make bus memory CPU accessible via the readb/readw/readl/writeb/ 345 * writew/writel functions and the other mmio helpers. The returned 346 * address is not guaranteed to be usable directly as a virtual 347 * address. 348 * 349 * This version of ioremap ensures that the memory is marked with a strong 350 * preference as completely uncachable on the CPU when possible. For non-PAT 351 * systems this ends up setting page-attribute flags PCD=1, PWT=1. For PAT 352 * systems this will set the PAT entry for the pages as strong UC. This call 353 * will honor existing caching rules from things like the PCI bus. Note that 354 * there are other caches and buffers on many busses. In particular driver 355 * authors should read up on PCI writes. 356 * 357 * It's useful if some control registers are in such an area and 358 * write combining or read caching is not desirable: 359 * 360 * Must be freed with iounmap. 361 */ 362 void __iomem *ioremap_uc(resource_size_t phys_addr, unsigned long size) 363 { 364 enum page_cache_mode pcm = _PAGE_CACHE_MODE_UC; 365 366 return __ioremap_caller(phys_addr, size, pcm, 367 __builtin_return_address(0), false); 368 } 369 EXPORT_SYMBOL_GPL(ioremap_uc); 370 371 /** 372 * ioremap_wc - map memory into CPU space write combined 373 * @phys_addr: bus address of the memory 374 * @size: size of the resource to map 375 * 376 * This version of ioremap ensures that the memory is marked write combining. 377 * Write combining allows faster writes to some hardware devices. 378 * 379 * Must be freed with iounmap. 380 */ 381 void __iomem *ioremap_wc(resource_size_t phys_addr, unsigned long size) 382 { 383 return __ioremap_caller(phys_addr, size, _PAGE_CACHE_MODE_WC, 384 __builtin_return_address(0), false); 385 } 386 EXPORT_SYMBOL(ioremap_wc); 387 388 /** 389 * ioremap_wt - map memory into CPU space write through 390 * @phys_addr: bus address of the memory 391 * @size: size of the resource to map 392 * 393 * This version of ioremap ensures that the memory is marked write through. 394 * Write through stores data into memory while keeping the cache up-to-date. 395 * 396 * Must be freed with iounmap. 397 */ 398 void __iomem *ioremap_wt(resource_size_t phys_addr, unsigned long size) 399 { 400 return __ioremap_caller(phys_addr, size, _PAGE_CACHE_MODE_WT, 401 __builtin_return_address(0), false); 402 } 403 EXPORT_SYMBOL(ioremap_wt); 404 405 void __iomem *ioremap_encrypted(resource_size_t phys_addr, unsigned long size) 406 { 407 return __ioremap_caller(phys_addr, size, _PAGE_CACHE_MODE_WB, 408 __builtin_return_address(0), true); 409 } 410 EXPORT_SYMBOL(ioremap_encrypted); 411 412 void __iomem *ioremap_cache(resource_size_t phys_addr, unsigned long size) 413 { 414 return __ioremap_caller(phys_addr, size, _PAGE_CACHE_MODE_WB, 415 __builtin_return_address(0), false); 416 } 417 EXPORT_SYMBOL(ioremap_cache); 418 419 void __iomem *ioremap_prot(resource_size_t phys_addr, unsigned long size, 420 unsigned long prot_val) 421 { 422 return __ioremap_caller(phys_addr, size, 423 pgprot2cachemode(__pgprot(prot_val)), 424 __builtin_return_address(0), false); 425 } 426 EXPORT_SYMBOL(ioremap_prot); 427 428 /** 429 * iounmap - Free a IO remapping 430 * @addr: virtual address from ioremap_* 431 * 432 * Caller must ensure there is only one unmapping for the same pointer. 433 */ 434 void iounmap(volatile void __iomem *addr) 435 { 436 struct vm_struct *p, *o; 437 438 if ((void __force *)addr <= high_memory) 439 return; 440 441 /* 442 * The PCI/ISA range special-casing was removed from __ioremap() 443 * so this check, in theory, can be removed. However, there are 444 * cases where iounmap() is called for addresses not obtained via 445 * ioremap() (vga16fb for example). Add a warning so that these 446 * cases can be caught and fixed. 447 */ 448 if ((void __force *)addr >= phys_to_virt(ISA_START_ADDRESS) && 449 (void __force *)addr < phys_to_virt(ISA_END_ADDRESS)) { 450 WARN(1, "iounmap() called for ISA range not obtained using ioremap()\n"); 451 return; 452 } 453 454 mmiotrace_iounmap(addr); 455 456 addr = (volatile void __iomem *) 457 (PAGE_MASK & (unsigned long __force)addr); 458 459 /* Use the vm area unlocked, assuming the caller 460 ensures there isn't another iounmap for the same address 461 in parallel. Reuse of the virtual address is prevented by 462 leaving it in the global lists until we're done with it. 463 cpa takes care of the direct mappings. */ 464 p = find_vm_area((void __force *)addr); 465 466 if (!p) { 467 printk(KERN_ERR "iounmap: bad address %p\n", addr); 468 dump_stack(); 469 return; 470 } 471 472 memtype_free(p->phys_addr, p->phys_addr + get_vm_area_size(p)); 473 474 /* Finally remove it */ 475 o = remove_vm_area((void __force *)addr); 476 BUG_ON(p != o || o == NULL); 477 kfree(p); 478 } 479 EXPORT_SYMBOL(iounmap); 480 481 int __init arch_ioremap_p4d_supported(void) 482 { 483 return 0; 484 } 485 486 int __init arch_ioremap_pud_supported(void) 487 { 488 #ifdef CONFIG_X86_64 489 return boot_cpu_has(X86_FEATURE_GBPAGES); 490 #else 491 return 0; 492 #endif 493 } 494 495 int __init arch_ioremap_pmd_supported(void) 496 { 497 return boot_cpu_has(X86_FEATURE_PSE); 498 } 499 500 /* 501 * Convert a physical pointer to a virtual kernel pointer for /dev/mem 502 * access 503 */ 504 void *xlate_dev_mem_ptr(phys_addr_t phys) 505 { 506 unsigned long start = phys & PAGE_MASK; 507 unsigned long offset = phys & ~PAGE_MASK; 508 void *vaddr; 509 510 /* memremap() maps if RAM, otherwise falls back to ioremap() */ 511 vaddr = memremap(start, PAGE_SIZE, MEMREMAP_WB); 512 513 /* Only add the offset on success and return NULL if memremap() failed */ 514 if (vaddr) 515 vaddr += offset; 516 517 return vaddr; 518 } 519 520 void unxlate_dev_mem_ptr(phys_addr_t phys, void *addr) 521 { 522 memunmap((void *)((unsigned long)addr & PAGE_MASK)); 523 } 524 525 /* 526 * Examine the physical address to determine if it is an area of memory 527 * that should be mapped decrypted. If the memory is not part of the 528 * kernel usable area it was accessed and created decrypted, so these 529 * areas should be mapped decrypted. And since the encryption key can 530 * change across reboots, persistent memory should also be mapped 531 * decrypted. 532 * 533 * If SEV is active, that implies that BIOS/UEFI also ran encrypted so 534 * only persistent memory should be mapped decrypted. 535 */ 536 static bool memremap_should_map_decrypted(resource_size_t phys_addr, 537 unsigned long size) 538 { 539 int is_pmem; 540 541 /* 542 * Check if the address is part of a persistent memory region. 543 * This check covers areas added by E820, EFI and ACPI. 544 */ 545 is_pmem = region_intersects(phys_addr, size, IORESOURCE_MEM, 546 IORES_DESC_PERSISTENT_MEMORY); 547 if (is_pmem != REGION_DISJOINT) 548 return true; 549 550 /* 551 * Check if the non-volatile attribute is set for an EFI 552 * reserved area. 553 */ 554 if (efi_enabled(EFI_BOOT)) { 555 switch (efi_mem_type(phys_addr)) { 556 case EFI_RESERVED_TYPE: 557 if (efi_mem_attributes(phys_addr) & EFI_MEMORY_NV) 558 return true; 559 break; 560 default: 561 break; 562 } 563 } 564 565 /* Check if the address is outside kernel usable area */ 566 switch (e820__get_entry_type(phys_addr, phys_addr + size - 1)) { 567 case E820_TYPE_RESERVED: 568 case E820_TYPE_ACPI: 569 case E820_TYPE_NVS: 570 case E820_TYPE_UNUSABLE: 571 /* For SEV, these areas are encrypted */ 572 if (sev_active()) 573 break; 574 /* Fallthrough */ 575 576 case E820_TYPE_PRAM: 577 return true; 578 default: 579 break; 580 } 581 582 return false; 583 } 584 585 /* 586 * Examine the physical address to determine if it is EFI data. Check 587 * it against the boot params structure and EFI tables and memory types. 588 */ 589 static bool memremap_is_efi_data(resource_size_t phys_addr, 590 unsigned long size) 591 { 592 u64 paddr; 593 594 /* Check if the address is part of EFI boot/runtime data */ 595 if (!efi_enabled(EFI_BOOT)) 596 return false; 597 598 paddr = boot_params.efi_info.efi_memmap_hi; 599 paddr <<= 32; 600 paddr |= boot_params.efi_info.efi_memmap; 601 if (phys_addr == paddr) 602 return true; 603 604 paddr = boot_params.efi_info.efi_systab_hi; 605 paddr <<= 32; 606 paddr |= boot_params.efi_info.efi_systab; 607 if (phys_addr == paddr) 608 return true; 609 610 if (efi_is_table_address(phys_addr)) 611 return true; 612 613 switch (efi_mem_type(phys_addr)) { 614 case EFI_BOOT_SERVICES_DATA: 615 case EFI_RUNTIME_SERVICES_DATA: 616 return true; 617 default: 618 break; 619 } 620 621 return false; 622 } 623 624 /* 625 * Examine the physical address to determine if it is boot data by checking 626 * it against the boot params setup_data chain. 627 */ 628 static bool memremap_is_setup_data(resource_size_t phys_addr, 629 unsigned long size) 630 { 631 struct setup_data *data; 632 u64 paddr, paddr_next; 633 634 paddr = boot_params.hdr.setup_data; 635 while (paddr) { 636 unsigned int len; 637 638 if (phys_addr == paddr) 639 return true; 640 641 data = memremap(paddr, sizeof(*data), 642 MEMREMAP_WB | MEMREMAP_DEC); 643 644 paddr_next = data->next; 645 len = data->len; 646 647 if ((phys_addr > paddr) && (phys_addr < (paddr + len))) { 648 memunmap(data); 649 return true; 650 } 651 652 if (data->type == SETUP_INDIRECT && 653 ((struct setup_indirect *)data->data)->type != SETUP_INDIRECT) { 654 paddr = ((struct setup_indirect *)data->data)->addr; 655 len = ((struct setup_indirect *)data->data)->len; 656 } 657 658 memunmap(data); 659 660 if ((phys_addr > paddr) && (phys_addr < (paddr + len))) 661 return true; 662 663 paddr = paddr_next; 664 } 665 666 return false; 667 } 668 669 /* 670 * Examine the physical address to determine if it is boot data by checking 671 * it against the boot params setup_data chain (early boot version). 672 */ 673 static bool __init early_memremap_is_setup_data(resource_size_t phys_addr, 674 unsigned long size) 675 { 676 struct setup_data *data; 677 u64 paddr, paddr_next; 678 679 paddr = boot_params.hdr.setup_data; 680 while (paddr) { 681 unsigned int len; 682 683 if (phys_addr == paddr) 684 return true; 685 686 data = early_memremap_decrypted(paddr, sizeof(*data)); 687 688 paddr_next = data->next; 689 len = data->len; 690 691 early_memunmap(data, sizeof(*data)); 692 693 if ((phys_addr > paddr) && (phys_addr < (paddr + len))) 694 return true; 695 696 paddr = paddr_next; 697 } 698 699 return false; 700 } 701 702 /* 703 * Architecture function to determine if RAM remap is allowed. By default, a 704 * RAM remap will map the data as encrypted. Determine if a RAM remap should 705 * not be done so that the data will be mapped decrypted. 706 */ 707 bool arch_memremap_can_ram_remap(resource_size_t phys_addr, unsigned long size, 708 unsigned long flags) 709 { 710 if (!mem_encrypt_active()) 711 return true; 712 713 if (flags & MEMREMAP_ENC) 714 return true; 715 716 if (flags & MEMREMAP_DEC) 717 return false; 718 719 if (sme_active()) { 720 if (memremap_is_setup_data(phys_addr, size) || 721 memremap_is_efi_data(phys_addr, size)) 722 return false; 723 } 724 725 return !memremap_should_map_decrypted(phys_addr, size); 726 } 727 728 /* 729 * Architecture override of __weak function to adjust the protection attributes 730 * used when remapping memory. By default, early_memremap() will map the data 731 * as encrypted. Determine if an encrypted mapping should not be done and set 732 * the appropriate protection attributes. 733 */ 734 pgprot_t __init early_memremap_pgprot_adjust(resource_size_t phys_addr, 735 unsigned long size, 736 pgprot_t prot) 737 { 738 bool encrypted_prot; 739 740 if (!mem_encrypt_active()) 741 return prot; 742 743 encrypted_prot = true; 744 745 if (sme_active()) { 746 if (early_memremap_is_setup_data(phys_addr, size) || 747 memremap_is_efi_data(phys_addr, size)) 748 encrypted_prot = false; 749 } 750 751 if (encrypted_prot && memremap_should_map_decrypted(phys_addr, size)) 752 encrypted_prot = false; 753 754 return encrypted_prot ? pgprot_encrypted(prot) 755 : pgprot_decrypted(prot); 756 } 757 758 bool phys_mem_access_encrypted(unsigned long phys_addr, unsigned long size) 759 { 760 return arch_memremap_can_ram_remap(phys_addr, size, 0); 761 } 762 763 #ifdef CONFIG_AMD_MEM_ENCRYPT 764 /* Remap memory with encryption */ 765 void __init *early_memremap_encrypted(resource_size_t phys_addr, 766 unsigned long size) 767 { 768 return early_memremap_prot(phys_addr, size, __PAGE_KERNEL_ENC); 769 } 770 771 /* 772 * Remap memory with encryption and write-protected - cannot be called 773 * before pat_init() is called 774 */ 775 void __init *early_memremap_encrypted_wp(resource_size_t phys_addr, 776 unsigned long size) 777 { 778 /* Be sure the write-protect PAT entry is set for write-protect */ 779 if (__pte2cachemode_tbl[_PAGE_CACHE_MODE_WP] != _PAGE_CACHE_MODE_WP) 780 return NULL; 781 782 return early_memremap_prot(phys_addr, size, __PAGE_KERNEL_ENC_WP); 783 } 784 785 /* Remap memory without encryption */ 786 void __init *early_memremap_decrypted(resource_size_t phys_addr, 787 unsigned long size) 788 { 789 return early_memremap_prot(phys_addr, size, __PAGE_KERNEL_NOENC); 790 } 791 792 /* 793 * Remap memory without encryption and write-protected - cannot be called 794 * before pat_init() is called 795 */ 796 void __init *early_memremap_decrypted_wp(resource_size_t phys_addr, 797 unsigned long size) 798 { 799 /* Be sure the write-protect PAT entry is set for write-protect */ 800 if (__pte2cachemode_tbl[_PAGE_CACHE_MODE_WP] != _PAGE_CACHE_MODE_WP) 801 return NULL; 802 803 return early_memremap_prot(phys_addr, size, __PAGE_KERNEL_NOENC_WP); 804 } 805 #endif /* CONFIG_AMD_MEM_ENCRYPT */ 806 807 static pte_t bm_pte[PAGE_SIZE/sizeof(pte_t)] __page_aligned_bss; 808 809 static inline pmd_t * __init early_ioremap_pmd(unsigned long addr) 810 { 811 /* Don't assume we're using swapper_pg_dir at this point */ 812 pgd_t *base = __va(read_cr3_pa()); 813 pgd_t *pgd = &base[pgd_index(addr)]; 814 p4d_t *p4d = p4d_offset(pgd, addr); 815 pud_t *pud = pud_offset(p4d, addr); 816 pmd_t *pmd = pmd_offset(pud, addr); 817 818 return pmd; 819 } 820 821 static inline pte_t * __init early_ioremap_pte(unsigned long addr) 822 { 823 return &bm_pte[pte_index(addr)]; 824 } 825 826 bool __init is_early_ioremap_ptep(pte_t *ptep) 827 { 828 return ptep >= &bm_pte[0] && ptep < &bm_pte[PAGE_SIZE/sizeof(pte_t)]; 829 } 830 831 void __init early_ioremap_init(void) 832 { 833 pmd_t *pmd; 834 835 #ifdef CONFIG_X86_64 836 BUILD_BUG_ON((fix_to_virt(0) + PAGE_SIZE) & ((1 << PMD_SHIFT) - 1)); 837 #else 838 WARN_ON((fix_to_virt(0) + PAGE_SIZE) & ((1 << PMD_SHIFT) - 1)); 839 #endif 840 841 early_ioremap_setup(); 842 843 pmd = early_ioremap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)); 844 memset(bm_pte, 0, sizeof(bm_pte)); 845 pmd_populate_kernel(&init_mm, pmd, bm_pte); 846 847 /* 848 * The boot-ioremap range spans multiple pmds, for which 849 * we are not prepared: 850 */ 851 #define __FIXADDR_TOP (-PAGE_SIZE) 852 BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT) 853 != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT)); 854 #undef __FIXADDR_TOP 855 if (pmd != early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END))) { 856 WARN_ON(1); 857 printk(KERN_WARNING "pmd %p != %p\n", 858 pmd, early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END))); 859 printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n", 860 fix_to_virt(FIX_BTMAP_BEGIN)); 861 printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_END): %08lx\n", 862 fix_to_virt(FIX_BTMAP_END)); 863 864 printk(KERN_WARNING "FIX_BTMAP_END: %d\n", FIX_BTMAP_END); 865 printk(KERN_WARNING "FIX_BTMAP_BEGIN: %d\n", 866 FIX_BTMAP_BEGIN); 867 } 868 } 869 870 void __init __early_set_fixmap(enum fixed_addresses idx, 871 phys_addr_t phys, pgprot_t flags) 872 { 873 unsigned long addr = __fix_to_virt(idx); 874 pte_t *pte; 875 876 if (idx >= __end_of_fixed_addresses) { 877 BUG(); 878 return; 879 } 880 pte = early_ioremap_pte(addr); 881 882 /* Sanitize 'prot' against any unsupported bits: */ 883 pgprot_val(flags) &= __supported_pte_mask; 884 885 if (pgprot_val(flags)) 886 set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags)); 887 else 888 pte_clear(&init_mm, addr, pte); 889 __flush_tlb_one_kernel(addr); 890 } 891