1 /* 2 * Common EFI (Extensible Firmware Interface) support functions 3 * Based on Extensible Firmware Interface Specification version 1.0 4 * 5 * Copyright (C) 1999 VA Linux Systems 6 * Copyright (C) 1999 Walt Drummond <drummond@valinux.com> 7 * Copyright (C) 1999-2002 Hewlett-Packard Co. 8 * David Mosberger-Tang <davidm@hpl.hp.com> 9 * Stephane Eranian <eranian@hpl.hp.com> 10 * Copyright (C) 2005-2008 Intel Co. 11 * Fenghua Yu <fenghua.yu@intel.com> 12 * Bibo Mao <bibo.mao@intel.com> 13 * Chandramouli Narayanan <mouli@linux.intel.com> 14 * Huang Ying <ying.huang@intel.com> 15 * Copyright (C) 2013 SuSE Labs 16 * Borislav Petkov <bp@suse.de> - runtime services VA mapping 17 * 18 * Copied from efi_32.c to eliminate the duplicated code between EFI 19 * 32/64 support code. --ying 2007-10-26 20 * 21 * All EFI Runtime Services are not implemented yet as EFI only 22 * supports physical mode addressing on SoftSDV. This is to be fixed 23 * in a future version. --drummond 1999-07-20 24 * 25 * Implemented EFI runtime services and virtual mode calls. --davidm 26 * 27 * Goutham Rao: <goutham.rao@intel.com> 28 * Skip non-WB memory and ignore empty memory ranges. 29 */ 30 31 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 32 33 #include <linux/kernel.h> 34 #include <linux/init.h> 35 #include <linux/efi.h> 36 #include <linux/efi-bgrt.h> 37 #include <linux/export.h> 38 #include <linux/bootmem.h> 39 #include <linux/slab.h> 40 #include <linux/memblock.h> 41 #include <linux/spinlock.h> 42 #include <linux/uaccess.h> 43 #include <linux/time.h> 44 #include <linux/io.h> 45 #include <linux/reboot.h> 46 #include <linux/bcd.h> 47 48 #include <asm/setup.h> 49 #include <asm/efi.h> 50 #include <asm/time.h> 51 #include <asm/cacheflush.h> 52 #include <asm/tlbflush.h> 53 #include <asm/x86_init.h> 54 #include <asm/rtc.h> 55 #include <asm/uv/uv.h> 56 57 #define EFI_DEBUG 58 59 struct efi_memory_map memmap; 60 61 static struct efi efi_phys __initdata; 62 static efi_system_table_t efi_systab __initdata; 63 64 static efi_config_table_type_t arch_tables[] __initdata = { 65 #ifdef CONFIG_X86_UV 66 {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab}, 67 #endif 68 {NULL_GUID, NULL, NULL}, 69 }; 70 71 u64 efi_setup; /* efi setup_data physical address */ 72 73 static int add_efi_memmap __initdata; 74 static int __init setup_add_efi_memmap(char *arg) 75 { 76 add_efi_memmap = 1; 77 return 0; 78 } 79 early_param("add_efi_memmap", setup_add_efi_memmap); 80 81 static efi_status_t __init phys_efi_set_virtual_address_map( 82 unsigned long memory_map_size, 83 unsigned long descriptor_size, 84 u32 descriptor_version, 85 efi_memory_desc_t *virtual_map) 86 { 87 efi_status_t status; 88 unsigned long flags; 89 pgd_t *save_pgd; 90 91 save_pgd = efi_call_phys_prolog(); 92 93 /* Disable interrupts around EFI calls: */ 94 local_irq_save(flags); 95 status = efi_call_phys(efi_phys.set_virtual_address_map, 96 memory_map_size, descriptor_size, 97 descriptor_version, virtual_map); 98 local_irq_restore(flags); 99 100 efi_call_phys_epilog(save_pgd); 101 102 return status; 103 } 104 105 void efi_get_time(struct timespec *now) 106 { 107 efi_status_t status; 108 efi_time_t eft; 109 efi_time_cap_t cap; 110 111 status = efi.get_time(&eft, &cap); 112 if (status != EFI_SUCCESS) 113 pr_err("Oops: efitime: can't read time!\n"); 114 115 now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour, 116 eft.minute, eft.second); 117 now->tv_nsec = 0; 118 } 119 120 void __init efi_find_mirror(void) 121 { 122 efi_memory_desc_t *md; 123 u64 mirror_size = 0, total_size = 0; 124 125 for_each_efi_memory_desc(md) { 126 unsigned long long start = md->phys_addr; 127 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT; 128 129 total_size += size; 130 if (md->attribute & EFI_MEMORY_MORE_RELIABLE) { 131 memblock_mark_mirror(start, size); 132 mirror_size += size; 133 } 134 } 135 if (mirror_size) 136 pr_info("Memory: %lldM/%lldM mirrored memory\n", 137 mirror_size>>20, total_size>>20); 138 } 139 140 /* 141 * Tell the kernel about the EFI memory map. This might include 142 * more than the max 128 entries that can fit in the e820 legacy 143 * (zeropage) memory map. 144 */ 145 146 static void __init do_add_efi_memmap(void) 147 { 148 efi_memory_desc_t *md; 149 150 for_each_efi_memory_desc(md) { 151 unsigned long long start = md->phys_addr; 152 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT; 153 int e820_type; 154 155 switch (md->type) { 156 case EFI_LOADER_CODE: 157 case EFI_LOADER_DATA: 158 case EFI_BOOT_SERVICES_CODE: 159 case EFI_BOOT_SERVICES_DATA: 160 case EFI_CONVENTIONAL_MEMORY: 161 if (md->attribute & EFI_MEMORY_WB) 162 e820_type = E820_RAM; 163 else 164 e820_type = E820_RESERVED; 165 break; 166 case EFI_ACPI_RECLAIM_MEMORY: 167 e820_type = E820_ACPI; 168 break; 169 case EFI_ACPI_MEMORY_NVS: 170 e820_type = E820_NVS; 171 break; 172 case EFI_UNUSABLE_MEMORY: 173 e820_type = E820_UNUSABLE; 174 break; 175 case EFI_PERSISTENT_MEMORY: 176 e820_type = E820_PMEM; 177 break; 178 default: 179 /* 180 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE 181 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO 182 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE 183 */ 184 e820_type = E820_RESERVED; 185 break; 186 } 187 e820_add_region(start, size, e820_type); 188 } 189 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map); 190 } 191 192 int __init efi_memblock_x86_reserve_range(void) 193 { 194 struct efi_info *e = &boot_params.efi_info; 195 phys_addr_t pmap; 196 197 if (efi_enabled(EFI_PARAVIRT)) 198 return 0; 199 200 #ifdef CONFIG_X86_32 201 /* Can't handle data above 4GB at this time */ 202 if (e->efi_memmap_hi) { 203 pr_err("Memory map is above 4GB, disabling EFI.\n"); 204 return -EINVAL; 205 } 206 pmap = e->efi_memmap; 207 #else 208 pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32)); 209 #endif 210 memmap.phys_map = pmap; 211 memmap.nr_map = e->efi_memmap_size / 212 e->efi_memdesc_size; 213 memmap.desc_size = e->efi_memdesc_size; 214 memmap.desc_version = e->efi_memdesc_version; 215 216 memblock_reserve(pmap, memmap.nr_map * memmap.desc_size); 217 218 efi.memmap = &memmap; 219 220 return 0; 221 } 222 223 void __init efi_print_memmap(void) 224 { 225 #ifdef EFI_DEBUG 226 efi_memory_desc_t *md; 227 int i = 0; 228 229 for_each_efi_memory_desc(md) { 230 char buf[64]; 231 232 pr_info("mem%02u: %s range=[0x%016llx-0x%016llx] (%lluMB)\n", 233 i++, efi_md_typeattr_format(buf, sizeof(buf), md), 234 md->phys_addr, 235 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - 1, 236 (md->num_pages >> (20 - EFI_PAGE_SHIFT))); 237 } 238 #endif /* EFI_DEBUG */ 239 } 240 241 void __init efi_unmap_memmap(void) 242 { 243 clear_bit(EFI_MEMMAP, &efi.flags); 244 if (memmap.map) { 245 early_memunmap(memmap.map, memmap.nr_map * memmap.desc_size); 246 memmap.map = NULL; 247 } 248 } 249 250 static int __init efi_systab_init(void *phys) 251 { 252 if (efi_enabled(EFI_64BIT)) { 253 efi_system_table_64_t *systab64; 254 struct efi_setup_data *data = NULL; 255 u64 tmp = 0; 256 257 if (efi_setup) { 258 data = early_memremap(efi_setup, sizeof(*data)); 259 if (!data) 260 return -ENOMEM; 261 } 262 systab64 = early_memremap((unsigned long)phys, 263 sizeof(*systab64)); 264 if (systab64 == NULL) { 265 pr_err("Couldn't map the system table!\n"); 266 if (data) 267 early_memunmap(data, sizeof(*data)); 268 return -ENOMEM; 269 } 270 271 efi_systab.hdr = systab64->hdr; 272 efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor : 273 systab64->fw_vendor; 274 tmp |= data ? data->fw_vendor : systab64->fw_vendor; 275 efi_systab.fw_revision = systab64->fw_revision; 276 efi_systab.con_in_handle = systab64->con_in_handle; 277 tmp |= systab64->con_in_handle; 278 efi_systab.con_in = systab64->con_in; 279 tmp |= systab64->con_in; 280 efi_systab.con_out_handle = systab64->con_out_handle; 281 tmp |= systab64->con_out_handle; 282 efi_systab.con_out = systab64->con_out; 283 tmp |= systab64->con_out; 284 efi_systab.stderr_handle = systab64->stderr_handle; 285 tmp |= systab64->stderr_handle; 286 efi_systab.stderr = systab64->stderr; 287 tmp |= systab64->stderr; 288 efi_systab.runtime = data ? 289 (void *)(unsigned long)data->runtime : 290 (void *)(unsigned long)systab64->runtime; 291 tmp |= data ? data->runtime : systab64->runtime; 292 efi_systab.boottime = (void *)(unsigned long)systab64->boottime; 293 tmp |= systab64->boottime; 294 efi_systab.nr_tables = systab64->nr_tables; 295 efi_systab.tables = data ? (unsigned long)data->tables : 296 systab64->tables; 297 tmp |= data ? data->tables : systab64->tables; 298 299 early_memunmap(systab64, sizeof(*systab64)); 300 if (data) 301 early_memunmap(data, sizeof(*data)); 302 #ifdef CONFIG_X86_32 303 if (tmp >> 32) { 304 pr_err("EFI data located above 4GB, disabling EFI.\n"); 305 return -EINVAL; 306 } 307 #endif 308 } else { 309 efi_system_table_32_t *systab32; 310 311 systab32 = early_memremap((unsigned long)phys, 312 sizeof(*systab32)); 313 if (systab32 == NULL) { 314 pr_err("Couldn't map the system table!\n"); 315 return -ENOMEM; 316 } 317 318 efi_systab.hdr = systab32->hdr; 319 efi_systab.fw_vendor = systab32->fw_vendor; 320 efi_systab.fw_revision = systab32->fw_revision; 321 efi_systab.con_in_handle = systab32->con_in_handle; 322 efi_systab.con_in = systab32->con_in; 323 efi_systab.con_out_handle = systab32->con_out_handle; 324 efi_systab.con_out = systab32->con_out; 325 efi_systab.stderr_handle = systab32->stderr_handle; 326 efi_systab.stderr = systab32->stderr; 327 efi_systab.runtime = (void *)(unsigned long)systab32->runtime; 328 efi_systab.boottime = (void *)(unsigned long)systab32->boottime; 329 efi_systab.nr_tables = systab32->nr_tables; 330 efi_systab.tables = systab32->tables; 331 332 early_memunmap(systab32, sizeof(*systab32)); 333 } 334 335 efi.systab = &efi_systab; 336 337 /* 338 * Verify the EFI Table 339 */ 340 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) { 341 pr_err("System table signature incorrect!\n"); 342 return -EINVAL; 343 } 344 if ((efi.systab->hdr.revision >> 16) == 0) 345 pr_err("Warning: System table version %d.%02d, expected 1.00 or greater!\n", 346 efi.systab->hdr.revision >> 16, 347 efi.systab->hdr.revision & 0xffff); 348 349 return 0; 350 } 351 352 static int __init efi_runtime_init32(void) 353 { 354 efi_runtime_services_32_t *runtime; 355 356 runtime = early_memremap((unsigned long)efi.systab->runtime, 357 sizeof(efi_runtime_services_32_t)); 358 if (!runtime) { 359 pr_err("Could not map the runtime service table!\n"); 360 return -ENOMEM; 361 } 362 363 /* 364 * We will only need *early* access to the SetVirtualAddressMap 365 * EFI runtime service. All other runtime services will be called 366 * via the virtual mapping. 367 */ 368 efi_phys.set_virtual_address_map = 369 (efi_set_virtual_address_map_t *) 370 (unsigned long)runtime->set_virtual_address_map; 371 early_memunmap(runtime, sizeof(efi_runtime_services_32_t)); 372 373 return 0; 374 } 375 376 static int __init efi_runtime_init64(void) 377 { 378 efi_runtime_services_64_t *runtime; 379 380 runtime = early_memremap((unsigned long)efi.systab->runtime, 381 sizeof(efi_runtime_services_64_t)); 382 if (!runtime) { 383 pr_err("Could not map the runtime service table!\n"); 384 return -ENOMEM; 385 } 386 387 /* 388 * We will only need *early* access to the SetVirtualAddressMap 389 * EFI runtime service. All other runtime services will be called 390 * via the virtual mapping. 391 */ 392 efi_phys.set_virtual_address_map = 393 (efi_set_virtual_address_map_t *) 394 (unsigned long)runtime->set_virtual_address_map; 395 early_memunmap(runtime, sizeof(efi_runtime_services_64_t)); 396 397 return 0; 398 } 399 400 static int __init efi_runtime_init(void) 401 { 402 int rv; 403 404 /* 405 * Check out the runtime services table. We need to map 406 * the runtime services table so that we can grab the physical 407 * address of several of the EFI runtime functions, needed to 408 * set the firmware into virtual mode. 409 * 410 * When EFI_PARAVIRT is in force then we could not map runtime 411 * service memory region because we do not have direct access to it. 412 * However, runtime services are available through proxy functions 413 * (e.g. in case of Xen dom0 EFI implementation they call special 414 * hypercall which executes relevant EFI functions) and that is why 415 * they are always enabled. 416 */ 417 418 if (!efi_enabled(EFI_PARAVIRT)) { 419 if (efi_enabled(EFI_64BIT)) 420 rv = efi_runtime_init64(); 421 else 422 rv = efi_runtime_init32(); 423 424 if (rv) 425 return rv; 426 } 427 428 set_bit(EFI_RUNTIME_SERVICES, &efi.flags); 429 430 return 0; 431 } 432 433 static int __init efi_memmap_init(void) 434 { 435 if (efi_enabled(EFI_PARAVIRT)) 436 return 0; 437 438 /* Map the EFI memory map */ 439 memmap.map = early_memremap((unsigned long)memmap.phys_map, 440 memmap.nr_map * memmap.desc_size); 441 if (memmap.map == NULL) { 442 pr_err("Could not map the memory map!\n"); 443 return -ENOMEM; 444 } 445 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size); 446 447 if (add_efi_memmap) 448 do_add_efi_memmap(); 449 450 set_bit(EFI_MEMMAP, &efi.flags); 451 452 return 0; 453 } 454 455 void __init efi_init(void) 456 { 457 efi_char16_t *c16; 458 char vendor[100] = "unknown"; 459 int i = 0; 460 void *tmp; 461 462 #ifdef CONFIG_X86_32 463 if (boot_params.efi_info.efi_systab_hi || 464 boot_params.efi_info.efi_memmap_hi) { 465 pr_info("Table located above 4GB, disabling EFI.\n"); 466 return; 467 } 468 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab; 469 #else 470 efi_phys.systab = (efi_system_table_t *) 471 (boot_params.efi_info.efi_systab | 472 ((__u64)boot_params.efi_info.efi_systab_hi<<32)); 473 #endif 474 475 if (efi_systab_init(efi_phys.systab)) 476 return; 477 478 efi.config_table = (unsigned long)efi.systab->tables; 479 efi.fw_vendor = (unsigned long)efi.systab->fw_vendor; 480 efi.runtime = (unsigned long)efi.systab->runtime; 481 482 /* 483 * Show what we know for posterity 484 */ 485 c16 = tmp = early_memremap(efi.systab->fw_vendor, 2); 486 if (c16) { 487 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i) 488 vendor[i] = *c16++; 489 vendor[i] = '\0'; 490 } else 491 pr_err("Could not map the firmware vendor!\n"); 492 early_memunmap(tmp, 2); 493 494 pr_info("EFI v%u.%.02u by %s\n", 495 efi.systab->hdr.revision >> 16, 496 efi.systab->hdr.revision & 0xffff, vendor); 497 498 if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables)) 499 return; 500 501 if (efi_config_init(arch_tables)) 502 return; 503 504 /* 505 * Note: We currently don't support runtime services on an EFI 506 * that doesn't match the kernel 32/64-bit mode. 507 */ 508 509 if (!efi_runtime_supported()) 510 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n"); 511 else { 512 if (efi_runtime_disabled() || efi_runtime_init()) 513 return; 514 } 515 if (efi_memmap_init()) 516 return; 517 518 if (efi_enabled(EFI_DBG)) 519 efi_print_memmap(); 520 521 efi_esrt_init(); 522 } 523 524 void __init efi_late_init(void) 525 { 526 efi_bgrt_init(); 527 } 528 529 void __init efi_set_executable(efi_memory_desc_t *md, bool executable) 530 { 531 u64 addr, npages; 532 533 addr = md->virt_addr; 534 npages = md->num_pages; 535 536 memrange_efi_to_native(&addr, &npages); 537 538 if (executable) 539 set_memory_x(addr, npages); 540 else 541 set_memory_nx(addr, npages); 542 } 543 544 void __init runtime_code_page_mkexec(void) 545 { 546 efi_memory_desc_t *md; 547 548 /* Make EFI runtime service code area executable */ 549 for_each_efi_memory_desc(md) { 550 if (md->type != EFI_RUNTIME_SERVICES_CODE) 551 continue; 552 553 efi_set_executable(md, true); 554 } 555 } 556 557 void __init efi_memory_uc(u64 addr, unsigned long size) 558 { 559 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT; 560 u64 npages; 561 562 npages = round_up(size, page_shift) / page_shift; 563 memrange_efi_to_native(&addr, &npages); 564 set_memory_uc(addr, npages); 565 } 566 567 void __init old_map_region(efi_memory_desc_t *md) 568 { 569 u64 start_pfn, end_pfn, end; 570 unsigned long size; 571 void *va; 572 573 start_pfn = PFN_DOWN(md->phys_addr); 574 size = md->num_pages << PAGE_SHIFT; 575 end = md->phys_addr + size; 576 end_pfn = PFN_UP(end); 577 578 if (pfn_range_is_mapped(start_pfn, end_pfn)) { 579 va = __va(md->phys_addr); 580 581 if (!(md->attribute & EFI_MEMORY_WB)) 582 efi_memory_uc((u64)(unsigned long)va, size); 583 } else 584 va = efi_ioremap(md->phys_addr, size, 585 md->type, md->attribute); 586 587 md->virt_addr = (u64) (unsigned long) va; 588 if (!va) 589 pr_err("ioremap of 0x%llX failed!\n", 590 (unsigned long long)md->phys_addr); 591 } 592 593 /* Merge contiguous regions of the same type and attribute */ 594 static void __init efi_merge_regions(void) 595 { 596 efi_memory_desc_t *md, *prev_md = NULL; 597 598 for_each_efi_memory_desc(md) { 599 u64 prev_size; 600 601 if (!prev_md) { 602 prev_md = md; 603 continue; 604 } 605 606 if (prev_md->type != md->type || 607 prev_md->attribute != md->attribute) { 608 prev_md = md; 609 continue; 610 } 611 612 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT; 613 614 if (md->phys_addr == (prev_md->phys_addr + prev_size)) { 615 prev_md->num_pages += md->num_pages; 616 md->type = EFI_RESERVED_TYPE; 617 md->attribute = 0; 618 continue; 619 } 620 prev_md = md; 621 } 622 } 623 624 static void __init get_systab_virt_addr(efi_memory_desc_t *md) 625 { 626 unsigned long size; 627 u64 end, systab; 628 629 size = md->num_pages << EFI_PAGE_SHIFT; 630 end = md->phys_addr + size; 631 systab = (u64)(unsigned long)efi_phys.systab; 632 if (md->phys_addr <= systab && systab < end) { 633 systab += md->virt_addr - md->phys_addr; 634 efi.systab = (efi_system_table_t *)(unsigned long)systab; 635 } 636 } 637 638 static void __init save_runtime_map(void) 639 { 640 #ifdef CONFIG_KEXEC_CORE 641 efi_memory_desc_t *md; 642 void *tmp, *q = NULL; 643 int count = 0; 644 645 if (efi_enabled(EFI_OLD_MEMMAP)) 646 return; 647 648 for_each_efi_memory_desc(md) { 649 if (!(md->attribute & EFI_MEMORY_RUNTIME) || 650 (md->type == EFI_BOOT_SERVICES_CODE) || 651 (md->type == EFI_BOOT_SERVICES_DATA)) 652 continue; 653 tmp = krealloc(q, (count + 1) * memmap.desc_size, GFP_KERNEL); 654 if (!tmp) 655 goto out; 656 q = tmp; 657 658 memcpy(q + count * memmap.desc_size, md, memmap.desc_size); 659 count++; 660 } 661 662 efi_runtime_map_setup(q, count, memmap.desc_size); 663 return; 664 665 out: 666 kfree(q); 667 pr_err("Error saving runtime map, efi runtime on kexec non-functional!!\n"); 668 #endif 669 } 670 671 static void *realloc_pages(void *old_memmap, int old_shift) 672 { 673 void *ret; 674 675 ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1); 676 if (!ret) 677 goto out; 678 679 /* 680 * A first-time allocation doesn't have anything to copy. 681 */ 682 if (!old_memmap) 683 return ret; 684 685 memcpy(ret, old_memmap, PAGE_SIZE << old_shift); 686 687 out: 688 free_pages((unsigned long)old_memmap, old_shift); 689 return ret; 690 } 691 692 /* 693 * Iterate the EFI memory map in reverse order because the regions 694 * will be mapped top-down. The end result is the same as if we had 695 * mapped things forward, but doesn't require us to change the 696 * existing implementation of efi_map_region(). 697 */ 698 static inline void *efi_map_next_entry_reverse(void *entry) 699 { 700 /* Initial call */ 701 if (!entry) 702 return memmap.map_end - memmap.desc_size; 703 704 entry -= memmap.desc_size; 705 if (entry < memmap.map) 706 return NULL; 707 708 return entry; 709 } 710 711 /* 712 * efi_map_next_entry - Return the next EFI memory map descriptor 713 * @entry: Previous EFI memory map descriptor 714 * 715 * This is a helper function to iterate over the EFI memory map, which 716 * we do in different orders depending on the current configuration. 717 * 718 * To begin traversing the memory map @entry must be %NULL. 719 * 720 * Returns %NULL when we reach the end of the memory map. 721 */ 722 static void *efi_map_next_entry(void *entry) 723 { 724 if (!efi_enabled(EFI_OLD_MEMMAP) && efi_enabled(EFI_64BIT)) { 725 /* 726 * Starting in UEFI v2.5 the EFI_PROPERTIES_TABLE 727 * config table feature requires us to map all entries 728 * in the same order as they appear in the EFI memory 729 * map. That is to say, entry N must have a lower 730 * virtual address than entry N+1. This is because the 731 * firmware toolchain leaves relative references in 732 * the code/data sections, which are split and become 733 * separate EFI memory regions. Mapping things 734 * out-of-order leads to the firmware accessing 735 * unmapped addresses. 736 * 737 * Since we need to map things this way whether or not 738 * the kernel actually makes use of 739 * EFI_PROPERTIES_TABLE, let's just switch to this 740 * scheme by default for 64-bit. 741 */ 742 return efi_map_next_entry_reverse(entry); 743 } 744 745 /* Initial call */ 746 if (!entry) 747 return memmap.map; 748 749 entry += memmap.desc_size; 750 if (entry >= memmap.map_end) 751 return NULL; 752 753 return entry; 754 } 755 756 /* 757 * Map the efi memory ranges of the runtime services and update new_mmap with 758 * virtual addresses. 759 */ 760 static void * __init efi_map_regions(int *count, int *pg_shift) 761 { 762 void *p, *new_memmap = NULL; 763 unsigned long left = 0; 764 efi_memory_desc_t *md; 765 766 p = NULL; 767 while ((p = efi_map_next_entry(p))) { 768 md = p; 769 if (!(md->attribute & EFI_MEMORY_RUNTIME)) { 770 #ifdef CONFIG_X86_64 771 if (md->type != EFI_BOOT_SERVICES_CODE && 772 md->type != EFI_BOOT_SERVICES_DATA) 773 #endif 774 continue; 775 } 776 777 efi_map_region(md); 778 get_systab_virt_addr(md); 779 780 if (left < memmap.desc_size) { 781 new_memmap = realloc_pages(new_memmap, *pg_shift); 782 if (!new_memmap) 783 return NULL; 784 785 left += PAGE_SIZE << *pg_shift; 786 (*pg_shift)++; 787 } 788 789 memcpy(new_memmap + (*count * memmap.desc_size), md, 790 memmap.desc_size); 791 792 left -= memmap.desc_size; 793 (*count)++; 794 } 795 796 return new_memmap; 797 } 798 799 static void __init kexec_enter_virtual_mode(void) 800 { 801 #ifdef CONFIG_KEXEC_CORE 802 efi_memory_desc_t *md; 803 unsigned int num_pages; 804 805 efi.systab = NULL; 806 807 /* 808 * We don't do virtual mode, since we don't do runtime services, on 809 * non-native EFI 810 */ 811 if (!efi_is_native()) { 812 efi_unmap_memmap(); 813 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags); 814 return; 815 } 816 817 if (efi_alloc_page_tables()) { 818 pr_err("Failed to allocate EFI page tables\n"); 819 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags); 820 return; 821 } 822 823 /* 824 * Map efi regions which were passed via setup_data. The virt_addr is a 825 * fixed addr which was used in first kernel of a kexec boot. 826 */ 827 for_each_efi_memory_desc(md) { 828 efi_map_region_fixed(md); /* FIXME: add error handling */ 829 get_systab_virt_addr(md); 830 } 831 832 save_runtime_map(); 833 834 BUG_ON(!efi.systab); 835 836 num_pages = ALIGN(memmap.nr_map * memmap.desc_size, PAGE_SIZE); 837 num_pages >>= PAGE_SHIFT; 838 839 if (efi_setup_page_tables(memmap.phys_map, num_pages)) { 840 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags); 841 return; 842 } 843 844 efi_sync_low_kernel_mappings(); 845 846 /* 847 * Now that EFI is in virtual mode, update the function 848 * pointers in the runtime service table to the new virtual addresses. 849 * 850 * Call EFI services through wrapper functions. 851 */ 852 efi.runtime_version = efi_systab.hdr.revision; 853 854 efi_native_runtime_setup(); 855 856 efi.set_virtual_address_map = NULL; 857 858 if (efi_enabled(EFI_OLD_MEMMAP) && (__supported_pte_mask & _PAGE_NX)) 859 runtime_code_page_mkexec(); 860 861 /* clean DUMMY object */ 862 efi_delete_dummy_variable(); 863 #endif 864 } 865 866 /* 867 * This function will switch the EFI runtime services to virtual mode. 868 * Essentially, we look through the EFI memmap and map every region that 869 * has the runtime attribute bit set in its memory descriptor into the 870 * efi_pgd page table. 871 * 872 * The old method which used to update that memory descriptor with the 873 * virtual address obtained from ioremap() is still supported when the 874 * kernel is booted with efi=old_map on its command line. Same old 875 * method enabled the runtime services to be called without having to 876 * thunk back into physical mode for every invocation. 877 * 878 * The new method does a pagetable switch in a preemption-safe manner 879 * so that we're in a different address space when calling a runtime 880 * function. For function arguments passing we do copy the PUDs of the 881 * kernel page table into efi_pgd prior to each call. 882 * 883 * Specially for kexec boot, efi runtime maps in previous kernel should 884 * be passed in via setup_data. In that case runtime ranges will be mapped 885 * to the same virtual addresses as the first kernel, see 886 * kexec_enter_virtual_mode(). 887 */ 888 static void __init __efi_enter_virtual_mode(void) 889 { 890 int count = 0, pg_shift = 0; 891 void *new_memmap = NULL; 892 efi_status_t status; 893 894 efi.systab = NULL; 895 896 if (efi_alloc_page_tables()) { 897 pr_err("Failed to allocate EFI page tables\n"); 898 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags); 899 return; 900 } 901 902 efi_merge_regions(); 903 new_memmap = efi_map_regions(&count, &pg_shift); 904 if (!new_memmap) { 905 pr_err("Error reallocating memory, EFI runtime non-functional!\n"); 906 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags); 907 return; 908 } 909 910 save_runtime_map(); 911 912 BUG_ON(!efi.systab); 913 914 if (efi_setup_page_tables(__pa(new_memmap), 1 << pg_shift)) { 915 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags); 916 return; 917 } 918 919 efi_sync_low_kernel_mappings(); 920 921 if (efi_is_native()) { 922 status = phys_efi_set_virtual_address_map( 923 memmap.desc_size * count, 924 memmap.desc_size, 925 memmap.desc_version, 926 (efi_memory_desc_t *)__pa(new_memmap)); 927 } else { 928 status = efi_thunk_set_virtual_address_map( 929 efi_phys.set_virtual_address_map, 930 memmap.desc_size * count, 931 memmap.desc_size, 932 memmap.desc_version, 933 (efi_memory_desc_t *)__pa(new_memmap)); 934 } 935 936 if (status != EFI_SUCCESS) { 937 pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n", 938 status); 939 panic("EFI call to SetVirtualAddressMap() failed!"); 940 } 941 942 /* 943 * Now that EFI is in virtual mode, update the function 944 * pointers in the runtime service table to the new virtual addresses. 945 * 946 * Call EFI services through wrapper functions. 947 */ 948 efi.runtime_version = efi_systab.hdr.revision; 949 950 if (efi_is_native()) 951 efi_native_runtime_setup(); 952 else 953 efi_thunk_runtime_setup(); 954 955 efi.set_virtual_address_map = NULL; 956 957 /* 958 * Apply more restrictive page table mapping attributes now that 959 * SVAM() has been called and the firmware has performed all 960 * necessary relocation fixups for the new virtual addresses. 961 */ 962 efi_runtime_update_mappings(); 963 efi_dump_pagetable(); 964 965 /* 966 * We mapped the descriptor array into the EFI pagetable above 967 * but we're not unmapping it here because if we're running in 968 * EFI mixed mode we need all of memory to be accessible when 969 * we pass parameters to the EFI runtime services in the 970 * thunking code. 971 * 972 * efi_cleanup_page_tables(__pa(new_memmap), 1 << pg_shift); 973 */ 974 free_pages((unsigned long)new_memmap, pg_shift); 975 976 /* clean DUMMY object */ 977 efi_delete_dummy_variable(); 978 } 979 980 void __init efi_enter_virtual_mode(void) 981 { 982 if (efi_enabled(EFI_PARAVIRT)) 983 return; 984 985 if (efi_setup) 986 kexec_enter_virtual_mode(); 987 else 988 __efi_enter_virtual_mode(); 989 } 990 991 /* 992 * Convenience functions to obtain memory types and attributes 993 */ 994 u32 efi_mem_type(unsigned long phys_addr) 995 { 996 efi_memory_desc_t *md; 997 998 if (!efi_enabled(EFI_MEMMAP)) 999 return 0; 1000 1001 for_each_efi_memory_desc(md) { 1002 if ((md->phys_addr <= phys_addr) && 1003 (phys_addr < (md->phys_addr + 1004 (md->num_pages << EFI_PAGE_SHIFT)))) 1005 return md->type; 1006 } 1007 return 0; 1008 } 1009 1010 static int __init arch_parse_efi_cmdline(char *str) 1011 { 1012 if (!str) { 1013 pr_warn("need at least one option\n"); 1014 return -EINVAL; 1015 } 1016 1017 if (parse_option_str(str, "old_map")) 1018 set_bit(EFI_OLD_MEMMAP, &efi.flags); 1019 1020 return 0; 1021 } 1022 early_param("efi", arch_parse_efi_cmdline); 1023