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