2 * Common EFI (Extensible Firmware Interface) support functions
3 * Based on Extensible Firmware Interface Specification version 1.0
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>
16 * Copied from efi_32.c to eliminate the duplicated code between EFI
17 * 32/64 support code. --ying 2007-10-26
19 * All EFI Runtime Services are not implemented yet as EFI only
20 * supports physical mode addressing on SoftSDV. This is to be fixed
21 * in a future version. --drummond 1999-07-20
23 * Implemented EFI runtime services and virtual mode calls. --davidm
25 * Goutham Rao: <goutham.rao@intel.com>
26 * Skip non-WB memory and ignore empty memory ranges.
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31 #include <linux/kernel.h>
32 #include <linux/init.h>
33 #include <linux/efi.h>
34 #include <linux/efi-bgrt.h>
35 #include <linux/export.h>
36 #include <linux/bootmem.h>
37 #include <linux/slab.h>
38 #include <linux/memblock.h>
39 #include <linux/spinlock.h>
40 #include <linux/uaccess.h>
41 #include <linux/time.h>
43 #include <linux/reboot.h>
44 #include <linux/bcd.h>
46 #include <asm/setup.h>
49 #include <asm/cacheflush.h>
50 #include <asm/tlbflush.h>
51 #include <asm/x86_init.h>
56 #define EFI_MIN_RESERVE 5120
58 #define EFI_DUMMY_GUID \
59 EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
61 static efi_char16_t efi_dummy_name[6] = { 'D', 'U', 'M', 'M', 'Y', 0 };
63 struct efi __read_mostly efi = {
64 .mps = EFI_INVALID_TABLE_ADDR,
65 .acpi = EFI_INVALID_TABLE_ADDR,
66 .acpi20 = EFI_INVALID_TABLE_ADDR,
67 .smbios = EFI_INVALID_TABLE_ADDR,
68 .sal_systab = EFI_INVALID_TABLE_ADDR,
69 .boot_info = EFI_INVALID_TABLE_ADDR,
70 .hcdp = EFI_INVALID_TABLE_ADDR,
71 .uga = EFI_INVALID_TABLE_ADDR,
72 .uv_systab = EFI_INVALID_TABLE_ADDR,
76 struct efi_memory_map memmap;
78 static struct efi efi_phys __initdata;
79 static efi_system_table_t efi_systab __initdata;
81 unsigned long x86_efi_facility;
84 * Returns 1 if 'facility' is enabled, 0 otherwise.
86 int efi_enabled(int facility)
88 return test_bit(facility, &x86_efi_facility) != 0;
90 EXPORT_SYMBOL(efi_enabled);
92 static bool __initdata disable_runtime = false;
93 static int __init setup_noefi(char *arg)
95 disable_runtime = true;
98 early_param("noefi", setup_noefi);
101 EXPORT_SYMBOL(add_efi_memmap);
103 static int __init setup_add_efi_memmap(char *arg)
108 early_param("add_efi_memmap", setup_add_efi_memmap);
110 static bool efi_no_storage_paranoia;
112 static int __init setup_storage_paranoia(char *arg)
114 efi_no_storage_paranoia = true;
117 early_param("efi_no_storage_paranoia", setup_storage_paranoia);
120 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
125 spin_lock_irqsave(&rtc_lock, flags);
126 status = efi_call_virt2(get_time, tm, tc);
127 spin_unlock_irqrestore(&rtc_lock, flags);
131 static efi_status_t virt_efi_set_time(efi_time_t *tm)
136 spin_lock_irqsave(&rtc_lock, flags);
137 status = efi_call_virt1(set_time, tm);
138 spin_unlock_irqrestore(&rtc_lock, flags);
142 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
149 spin_lock_irqsave(&rtc_lock, flags);
150 status = efi_call_virt3(get_wakeup_time,
151 enabled, pending, tm);
152 spin_unlock_irqrestore(&rtc_lock, flags);
156 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
161 spin_lock_irqsave(&rtc_lock, flags);
162 status = efi_call_virt2(set_wakeup_time,
164 spin_unlock_irqrestore(&rtc_lock, flags);
168 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
171 unsigned long *data_size,
174 return efi_call_virt5(get_variable,
179 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
183 return efi_call_virt3(get_next_variable,
184 name_size, name, vendor);
187 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
190 unsigned long data_size,
193 return efi_call_virt5(set_variable,
198 static efi_status_t virt_efi_query_variable_info(u32 attr,
200 u64 *remaining_space,
201 u64 *max_variable_size)
203 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
204 return EFI_UNSUPPORTED;
206 return efi_call_virt4(query_variable_info, attr, storage_space,
207 remaining_space, max_variable_size);
210 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
212 return efi_call_virt1(get_next_high_mono_count, count);
215 static void virt_efi_reset_system(int reset_type,
217 unsigned long data_size,
220 efi_call_virt4(reset_system, reset_type, status,
224 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
226 unsigned long sg_list)
228 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
229 return EFI_UNSUPPORTED;
231 return efi_call_virt3(update_capsule, capsules, count, sg_list);
234 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
239 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
240 return EFI_UNSUPPORTED;
242 return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
246 static efi_status_t __init phys_efi_set_virtual_address_map(
247 unsigned long memory_map_size,
248 unsigned long descriptor_size,
249 u32 descriptor_version,
250 efi_memory_desc_t *virtual_map)
254 efi_call_phys_prelog();
255 status = efi_call_phys4(efi_phys.set_virtual_address_map,
256 memory_map_size, descriptor_size,
257 descriptor_version, virtual_map);
258 efi_call_phys_epilog();
262 static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
268 spin_lock_irqsave(&rtc_lock, flags);
269 efi_call_phys_prelog();
270 status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm),
272 efi_call_phys_epilog();
273 spin_unlock_irqrestore(&rtc_lock, flags);
277 int efi_set_rtc_mmss(const struct timespec *now)
279 unsigned long nowtime = now->tv_sec;
285 status = efi.get_time(&eft, &cap);
286 if (status != EFI_SUCCESS) {
287 pr_err("Oops: efitime: can't read time!\n");
291 rtc_time_to_tm(nowtime, &tm);
292 if (!rtc_valid_tm(&tm)) {
293 eft.year = tm.tm_year + 1900;
294 eft.month = tm.tm_mon + 1;
295 eft.day = tm.tm_mday;
296 eft.minute = tm.tm_min;
297 eft.second = tm.tm_sec;
301 "%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
302 __FUNCTION__, nowtime);
306 status = efi.set_time(&eft);
307 if (status != EFI_SUCCESS) {
308 pr_err("Oops: efitime: can't write time!\n");
314 void efi_get_time(struct timespec *now)
320 status = efi.get_time(&eft, &cap);
321 if (status != EFI_SUCCESS)
322 pr_err("Oops: efitime: can't read time!\n");
324 now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour,
325 eft.minute, eft.second);
330 * Tell the kernel about the EFI memory map. This might include
331 * more than the max 128 entries that can fit in the e820 legacy
332 * (zeropage) memory map.
335 static void __init do_add_efi_memmap(void)
339 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
340 efi_memory_desc_t *md = p;
341 unsigned long long start = md->phys_addr;
342 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
346 case EFI_LOADER_CODE:
347 case EFI_LOADER_DATA:
348 case EFI_BOOT_SERVICES_CODE:
349 case EFI_BOOT_SERVICES_DATA:
350 case EFI_CONVENTIONAL_MEMORY:
351 if (md->attribute & EFI_MEMORY_WB)
352 e820_type = E820_RAM;
354 e820_type = E820_RESERVED;
356 case EFI_ACPI_RECLAIM_MEMORY:
357 e820_type = E820_ACPI;
359 case EFI_ACPI_MEMORY_NVS:
360 e820_type = E820_NVS;
362 case EFI_UNUSABLE_MEMORY:
363 e820_type = E820_UNUSABLE;
367 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
368 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
369 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
371 e820_type = E820_RESERVED;
374 e820_add_region(start, size, e820_type);
376 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
379 int __init efi_memblock_x86_reserve_range(void)
381 struct efi_info *e = &boot_params.efi_info;
385 /* Can't handle data above 4GB at this time */
386 if (e->efi_memmap_hi) {
387 pr_err("Memory map is above 4GB, disabling EFI.\n");
390 pmap = e->efi_memmap;
392 pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
394 memmap.phys_map = (void *)pmap;
395 memmap.nr_map = e->efi_memmap_size /
397 memmap.desc_size = e->efi_memdesc_size;
398 memmap.desc_version = e->efi_memdesc_version;
400 memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
406 static void __init print_efi_memmap(void)
408 efi_memory_desc_t *md;
412 for (p = memmap.map, i = 0;
414 p += memmap.desc_size, i++) {
416 pr_info("mem%02u: type=%u, attr=0x%llx, "
417 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
418 i, md->type, md->attribute, md->phys_addr,
419 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
420 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
423 #endif /* EFI_DEBUG */
425 void __init efi_reserve_boot_services(void)
429 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
430 efi_memory_desc_t *md = p;
431 u64 start = md->phys_addr;
432 u64 size = md->num_pages << EFI_PAGE_SHIFT;
434 if (md->type != EFI_BOOT_SERVICES_CODE &&
435 md->type != EFI_BOOT_SERVICES_DATA)
437 /* Only reserve where possible:
438 * - Not within any already allocated areas
439 * - Not over any memory area (really needed, if above?)
440 * - Not within any part of the kernel
441 * - Not the bios reserved area
443 if ((start+size >= __pa_symbol(_text)
444 && start <= __pa_symbol(_end)) ||
445 !e820_all_mapped(start, start+size, E820_RAM) ||
446 memblock_is_region_reserved(start, size)) {
447 /* Could not reserve, skip it */
449 memblock_dbg("Could not reserve boot range "
450 "[0x%010llx-0x%010llx]\n",
451 start, start+size-1);
453 memblock_reserve(start, size);
457 void __init efi_unmap_memmap(void)
459 clear_bit(EFI_MEMMAP, &x86_efi_facility);
461 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
466 void __init efi_free_boot_services(void)
470 if (!efi_is_native())
473 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
474 efi_memory_desc_t *md = p;
475 unsigned long long start = md->phys_addr;
476 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
478 if (md->type != EFI_BOOT_SERVICES_CODE &&
479 md->type != EFI_BOOT_SERVICES_DATA)
482 /* Could not reserve boot area */
486 free_bootmem_late(start, size);
492 static int __init efi_systab_init(void *phys)
494 if (efi_enabled(EFI_64BIT)) {
495 efi_system_table_64_t *systab64;
498 systab64 = early_ioremap((unsigned long)phys,
500 if (systab64 == NULL) {
501 pr_err("Couldn't map the system table!\n");
505 efi_systab.hdr = systab64->hdr;
506 efi_systab.fw_vendor = systab64->fw_vendor;
507 tmp |= systab64->fw_vendor;
508 efi_systab.fw_revision = systab64->fw_revision;
509 efi_systab.con_in_handle = systab64->con_in_handle;
510 tmp |= systab64->con_in_handle;
511 efi_systab.con_in = systab64->con_in;
512 tmp |= systab64->con_in;
513 efi_systab.con_out_handle = systab64->con_out_handle;
514 tmp |= systab64->con_out_handle;
515 efi_systab.con_out = systab64->con_out;
516 tmp |= systab64->con_out;
517 efi_systab.stderr_handle = systab64->stderr_handle;
518 tmp |= systab64->stderr_handle;
519 efi_systab.stderr = systab64->stderr;
520 tmp |= systab64->stderr;
521 efi_systab.runtime = (void *)(unsigned long)systab64->runtime;
522 tmp |= systab64->runtime;
523 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
524 tmp |= systab64->boottime;
525 efi_systab.nr_tables = systab64->nr_tables;
526 efi_systab.tables = systab64->tables;
527 tmp |= systab64->tables;
529 early_iounmap(systab64, sizeof(*systab64));
532 pr_err("EFI data located above 4GB, disabling EFI.\n");
537 efi_system_table_32_t *systab32;
539 systab32 = early_ioremap((unsigned long)phys,
541 if (systab32 == NULL) {
542 pr_err("Couldn't map the system table!\n");
546 efi_systab.hdr = systab32->hdr;
547 efi_systab.fw_vendor = systab32->fw_vendor;
548 efi_systab.fw_revision = systab32->fw_revision;
549 efi_systab.con_in_handle = systab32->con_in_handle;
550 efi_systab.con_in = systab32->con_in;
551 efi_systab.con_out_handle = systab32->con_out_handle;
552 efi_systab.con_out = systab32->con_out;
553 efi_systab.stderr_handle = systab32->stderr_handle;
554 efi_systab.stderr = systab32->stderr;
555 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
556 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
557 efi_systab.nr_tables = systab32->nr_tables;
558 efi_systab.tables = systab32->tables;
560 early_iounmap(systab32, sizeof(*systab32));
563 efi.systab = &efi_systab;
566 * Verify the EFI Table
568 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
569 pr_err("System table signature incorrect!\n");
572 if ((efi.systab->hdr.revision >> 16) == 0)
573 pr_err("Warning: System table version "
574 "%d.%02d, expected 1.00 or greater!\n",
575 efi.systab->hdr.revision >> 16,
576 efi.systab->hdr.revision & 0xffff);
581 static int __init efi_config_init(u64 tables, int nr_tables)
583 void *config_tables, *tablep;
586 if (efi_enabled(EFI_64BIT))
587 sz = sizeof(efi_config_table_64_t);
589 sz = sizeof(efi_config_table_32_t);
592 * Let's see what config tables the firmware passed to us.
594 config_tables = early_ioremap(tables, nr_tables * sz);
595 if (config_tables == NULL) {
596 pr_err("Could not map Configuration table!\n");
600 tablep = config_tables;
602 for (i = 0; i < efi.systab->nr_tables; i++) {
606 if (efi_enabled(EFI_64BIT)) {
608 guid = ((efi_config_table_64_t *)tablep)->guid;
609 table64 = ((efi_config_table_64_t *)tablep)->table;
614 pr_err("Table located above 4GB, disabling EFI.\n");
615 early_iounmap(config_tables,
616 efi.systab->nr_tables * sz);
621 guid = ((efi_config_table_32_t *)tablep)->guid;
622 table = ((efi_config_table_32_t *)tablep)->table;
624 if (!efi_guidcmp(guid, MPS_TABLE_GUID)) {
626 pr_cont(" MPS=0x%lx ", table);
627 } else if (!efi_guidcmp(guid, ACPI_20_TABLE_GUID)) {
629 pr_cont(" ACPI 2.0=0x%lx ", table);
630 } else if (!efi_guidcmp(guid, ACPI_TABLE_GUID)) {
632 pr_cont(" ACPI=0x%lx ", table);
633 } else if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID)) {
635 pr_cont(" SMBIOS=0x%lx ", table);
637 } else if (!efi_guidcmp(guid, UV_SYSTEM_TABLE_GUID)) {
638 efi.uv_systab = table;
639 pr_cont(" UVsystab=0x%lx ", table);
641 } else if (!efi_guidcmp(guid, HCDP_TABLE_GUID)) {
643 pr_cont(" HCDP=0x%lx ", table);
644 } else if (!efi_guidcmp(guid, UGA_IO_PROTOCOL_GUID)) {
646 pr_cont(" UGA=0x%lx ", table);
651 early_iounmap(config_tables, efi.systab->nr_tables * sz);
655 static int __init efi_runtime_init(void)
657 efi_runtime_services_t *runtime;
660 * Check out the runtime services table. We need to map
661 * the runtime services table so that we can grab the physical
662 * address of several of the EFI runtime functions, needed to
663 * set the firmware into virtual mode.
665 runtime = early_ioremap((unsigned long)efi.systab->runtime,
666 sizeof(efi_runtime_services_t));
668 pr_err("Could not map the runtime service table!\n");
672 * We will only need *early* access to the following
673 * two EFI runtime services before set_virtual_address_map
676 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
677 efi_phys.set_virtual_address_map =
678 (efi_set_virtual_address_map_t *)
679 runtime->set_virtual_address_map;
681 * Make efi_get_time can be called before entering
684 efi.get_time = phys_efi_get_time;
685 early_iounmap(runtime, sizeof(efi_runtime_services_t));
690 static int __init efi_memmap_init(void)
692 /* Map the EFI memory map */
693 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
694 memmap.nr_map * memmap.desc_size);
695 if (memmap.map == NULL) {
696 pr_err("Could not map the memory map!\n");
699 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
707 void __init efi_init(void)
710 char vendor[100] = "unknown";
715 if (boot_params.efi_info.efi_systab_hi ||
716 boot_params.efi_info.efi_memmap_hi) {
717 pr_info("Table located above 4GB, disabling EFI.\n");
720 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
722 efi_phys.systab = (efi_system_table_t *)
723 (boot_params.efi_info.efi_systab |
724 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
727 if (efi_systab_init(efi_phys.systab))
730 set_bit(EFI_SYSTEM_TABLES, &x86_efi_facility);
733 * Show what we know for posterity
735 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
737 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
741 pr_err("Could not map the firmware vendor!\n");
742 early_iounmap(tmp, 2);
744 pr_info("EFI v%u.%.02u by %s\n",
745 efi.systab->hdr.revision >> 16,
746 efi.systab->hdr.revision & 0xffff, vendor);
748 if (efi_config_init(efi.systab->tables, efi.systab->nr_tables))
751 set_bit(EFI_CONFIG_TABLES, &x86_efi_facility);
754 * Note: We currently don't support runtime services on an EFI
755 * that doesn't match the kernel 32/64-bit mode.
758 if (!efi_is_native())
759 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
761 if (disable_runtime || efi_runtime_init())
763 set_bit(EFI_RUNTIME_SERVICES, &x86_efi_facility);
766 if (efi_memmap_init())
769 set_bit(EFI_MEMMAP, &x86_efi_facility);
772 if (efi_is_native()) {
773 x86_platform.get_wallclock = efi_get_time;
774 x86_platform.set_wallclock = efi_set_rtc_mmss;
783 void __init efi_late_init(void)
788 void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
792 addr = md->virt_addr;
793 npages = md->num_pages;
795 memrange_efi_to_native(&addr, &npages);
798 set_memory_x(addr, npages);
800 set_memory_nx(addr, npages);
803 static void __init runtime_code_page_mkexec(void)
805 efi_memory_desc_t *md;
808 /* Make EFI runtime service code area executable */
809 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
812 if (md->type != EFI_RUNTIME_SERVICES_CODE)
815 efi_set_executable(md, true);
820 * We can't ioremap data in EFI boot services RAM, because we've already mapped
821 * it as RAM. So, look it up in the existing EFI memory map instead. Only
822 * callable after efi_enter_virtual_mode and before efi_free_boot_services.
824 void __iomem *efi_lookup_mapped_addr(u64 phys_addr)
827 if (WARN_ON(!memmap.map))
829 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
830 efi_memory_desc_t *md = p;
831 u64 size = md->num_pages << EFI_PAGE_SHIFT;
832 u64 end = md->phys_addr + size;
833 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
834 md->type != EFI_BOOT_SERVICES_CODE &&
835 md->type != EFI_BOOT_SERVICES_DATA)
839 if (phys_addr >= md->phys_addr && phys_addr < end) {
840 phys_addr += md->virt_addr - md->phys_addr;
841 return (__force void __iomem *)(unsigned long)phys_addr;
847 void efi_memory_uc(u64 addr, unsigned long size)
849 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
852 npages = round_up(size, page_shift) / page_shift;
853 memrange_efi_to_native(&addr, &npages);
854 set_memory_uc(addr, npages);
858 * This function will switch the EFI runtime services to virtual mode.
859 * Essentially, look through the EFI memmap and map every region that
860 * has the runtime attribute bit set in its memory descriptor and update
861 * that memory descriptor with the virtual address obtained from ioremap().
862 * This enables the runtime services to be called without having to
863 * thunk back into physical mode for every invocation.
865 void __init efi_enter_virtual_mode(void)
867 efi_memory_desc_t *md, *prev_md = NULL;
870 u64 end, systab, start_pfn, end_pfn;
871 void *p, *va, *new_memmap = NULL;
877 * We don't do virtual mode, since we don't do runtime services, on
881 if (!efi_is_native()) {
886 /* Merge contiguous regions of the same type and attribute */
887 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
896 if (prev_md->type != md->type ||
897 prev_md->attribute != md->attribute) {
902 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
904 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
905 prev_md->num_pages += md->num_pages;
906 md->type = EFI_RESERVED_TYPE;
913 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
915 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
916 md->type != EFI_BOOT_SERVICES_CODE &&
917 md->type != EFI_BOOT_SERVICES_DATA)
920 size = md->num_pages << EFI_PAGE_SHIFT;
921 end = md->phys_addr + size;
923 start_pfn = PFN_DOWN(md->phys_addr);
924 end_pfn = PFN_UP(end);
925 if (pfn_range_is_mapped(start_pfn, end_pfn)) {
926 va = __va(md->phys_addr);
928 if (!(md->attribute & EFI_MEMORY_WB))
929 efi_memory_uc((u64)(unsigned long)va, size);
931 va = efi_ioremap(md->phys_addr, size,
932 md->type, md->attribute);
934 md->virt_addr = (u64) (unsigned long) va;
937 pr_err("ioremap of 0x%llX failed!\n",
938 (unsigned long long)md->phys_addr);
942 systab = (u64) (unsigned long) efi_phys.systab;
943 if (md->phys_addr <= systab && systab < end) {
944 systab += md->virt_addr - md->phys_addr;
945 efi.systab = (efi_system_table_t *) (unsigned long) systab;
947 new_memmap = krealloc(new_memmap,
948 (count + 1) * memmap.desc_size,
950 memcpy(new_memmap + (count * memmap.desc_size), md,
957 status = phys_efi_set_virtual_address_map(
958 memmap.desc_size * count,
961 (efi_memory_desc_t *)__pa(new_memmap));
963 if (status != EFI_SUCCESS) {
964 pr_alert("Unable to switch EFI into virtual mode "
965 "(status=%lx)!\n", status);
966 panic("EFI call to SetVirtualAddressMap() failed!");
970 * Now that EFI is in virtual mode, update the function
971 * pointers in the runtime service table to the new virtual addresses.
973 * Call EFI services through wrapper functions.
975 efi.runtime_version = efi_systab.hdr.revision;
976 efi.get_time = virt_efi_get_time;
977 efi.set_time = virt_efi_set_time;
978 efi.get_wakeup_time = virt_efi_get_wakeup_time;
979 efi.set_wakeup_time = virt_efi_set_wakeup_time;
980 efi.get_variable = virt_efi_get_variable;
981 efi.get_next_variable = virt_efi_get_next_variable;
982 efi.set_variable = virt_efi_set_variable;
983 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
984 efi.reset_system = virt_efi_reset_system;
985 efi.set_virtual_address_map = NULL;
986 efi.query_variable_info = virt_efi_query_variable_info;
987 efi.update_capsule = virt_efi_update_capsule;
988 efi.query_capsule_caps = virt_efi_query_capsule_caps;
989 if (__supported_pte_mask & _PAGE_NX)
990 runtime_code_page_mkexec();
994 /* clean DUMMY object */
995 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
996 EFI_VARIABLE_NON_VOLATILE |
997 EFI_VARIABLE_BOOTSERVICE_ACCESS |
998 EFI_VARIABLE_RUNTIME_ACCESS,
1003 * Convenience functions to obtain memory types and attributes
1005 u32 efi_mem_type(unsigned long phys_addr)
1007 efi_memory_desc_t *md;
1010 if (!efi_enabled(EFI_MEMMAP))
1013 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1015 if ((md->phys_addr <= phys_addr) &&
1016 (phys_addr < (md->phys_addr +
1017 (md->num_pages << EFI_PAGE_SHIFT))))
1023 u64 efi_mem_attributes(unsigned long phys_addr)
1025 efi_memory_desc_t *md;
1028 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1030 if ((md->phys_addr <= phys_addr) &&
1031 (phys_addr < (md->phys_addr +
1032 (md->num_pages << EFI_PAGE_SHIFT))))
1033 return md->attribute;
1039 * Some firmware has serious problems when using more than 50% of the EFI
1040 * variable store, i.e. it triggers bugs that can brick machines. Ensure that
1041 * we never use more than this safe limit.
1043 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
1046 efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
1048 efi_status_t status;
1049 u64 storage_size, remaining_size, max_size;
1051 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
1054 status = efi.query_variable_info(attributes, &storage_size,
1055 &remaining_size, &max_size);
1056 if (status != EFI_SUCCESS)
1060 * Some firmware implementations refuse to boot if there's insufficient
1061 * space in the variable store. We account for that by refusing the
1062 * write if permitting it would reduce the available space to under
1063 * 5KB. This figure was provided by Samsung, so should be safe.
1065 if ((remaining_size - size < EFI_MIN_RESERVE) &&
1066 !efi_no_storage_paranoia) {
1069 * Triggering garbage collection may require that the firmware
1070 * generate a real EFI_OUT_OF_RESOURCES error. We can force
1071 * that by attempting to use more space than is available.
1073 unsigned long dummy_size = remaining_size + 1024;
1074 void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
1077 return EFI_OUT_OF_RESOURCES;
1079 status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1080 EFI_VARIABLE_NON_VOLATILE |
1081 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1082 EFI_VARIABLE_RUNTIME_ACCESS,
1085 if (status == EFI_SUCCESS) {
1087 * This should have failed, so if it didn't make sure
1088 * that we delete it...
1090 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1091 EFI_VARIABLE_NON_VOLATILE |
1092 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1093 EFI_VARIABLE_RUNTIME_ACCESS,
1100 * The runtime code may now have triggered a garbage collection
1101 * run, so check the variable info again
1103 status = efi.query_variable_info(attributes, &storage_size,
1104 &remaining_size, &max_size);
1106 if (status != EFI_SUCCESS)
1110 * There still isn't enough room, so return an error
1112 if (remaining_size - size < EFI_MIN_RESERVE)
1113 return EFI_OUT_OF_RESOURCES;
1118 EXPORT_SYMBOL_GPL(efi_query_variable_store);