aboutsummaryrefslogtreecommitdiffstats
path: root/arch/x86/platform/efi/efi.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/x86/platform/efi/efi.c')
-rw-r--r--arch/x86/platform/efi/efi.c378
1 files changed, 301 insertions, 77 deletions
diff --git a/arch/x86/platform/efi/efi.c b/arch/x86/platform/efi/efi.c
index cceb813044ef..b97acecf3fd9 100644
--- a/arch/x86/platform/efi/efi.c
+++ b/arch/x86/platform/efi/efi.c
@@ -12,6 +12,8 @@
12 * Bibo Mao <bibo.mao@intel.com> 12 * Bibo Mao <bibo.mao@intel.com>
13 * Chandramouli Narayanan <mouli@linux.intel.com> 13 * Chandramouli Narayanan <mouli@linux.intel.com>
14 * Huang Ying <ying.huang@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
15 * 17 *
16 * Copied from efi_32.c to eliminate the duplicated code between EFI 18 * Copied from efi_32.c to eliminate the duplicated code between EFI
17 * 32/64 support code. --ying 2007-10-26 19 * 32/64 support code. --ying 2007-10-26
@@ -50,8 +52,9 @@
50#include <asm/tlbflush.h> 52#include <asm/tlbflush.h>
51#include <asm/x86_init.h> 53#include <asm/x86_init.h>
52#include <asm/rtc.h> 54#include <asm/rtc.h>
55#include <asm/uv/uv.h>
53 56
54#define EFI_DEBUG 1 57#define EFI_DEBUG
55 58
56#define EFI_MIN_RESERVE 5120 59#define EFI_MIN_RESERVE 5120
57 60
@@ -74,6 +77,8 @@ static __initdata efi_config_table_type_t arch_tables[] = {
74 {NULL_GUID, NULL, NULL}, 77 {NULL_GUID, NULL, NULL},
75}; 78};
76 79
80u64 efi_setup; /* efi setup_data physical address */
81
77/* 82/*
78 * Returns 1 if 'facility' is enabled, 0 otherwise. 83 * Returns 1 if 'facility' is enabled, 0 otherwise.
79 */ 84 */
@@ -110,7 +115,6 @@ static int __init setup_storage_paranoia(char *arg)
110} 115}
111early_param("efi_no_storage_paranoia", setup_storage_paranoia); 116early_param("efi_no_storage_paranoia", setup_storage_paranoia);
112 117
113
114static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc) 118static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
115{ 119{
116 unsigned long flags; 120 unsigned long flags;
@@ -398,9 +402,9 @@ int __init efi_memblock_x86_reserve_range(void)
398 return 0; 402 return 0;
399} 403}
400 404
401#if EFI_DEBUG
402static void __init print_efi_memmap(void) 405static void __init print_efi_memmap(void)
403{ 406{
407#ifdef EFI_DEBUG
404 efi_memory_desc_t *md; 408 efi_memory_desc_t *md;
405 void *p; 409 void *p;
406 int i; 410 int i;
@@ -415,8 +419,8 @@ static void __init print_efi_memmap(void)
415 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT), 419 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
416 (md->num_pages >> (20 - EFI_PAGE_SHIFT))); 420 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
417 } 421 }
418}
419#endif /* EFI_DEBUG */ 422#endif /* EFI_DEBUG */
423}
420 424
421void __init efi_reserve_boot_services(void) 425void __init efi_reserve_boot_services(void)
422{ 426{
@@ -436,7 +440,7 @@ void __init efi_reserve_boot_services(void)
436 * - Not within any part of the kernel 440 * - Not within any part of the kernel
437 * - Not the bios reserved area 441 * - Not the bios reserved area
438 */ 442 */
439 if ((start+size >= __pa_symbol(_text) 443 if ((start + size > __pa_symbol(_text)
440 && start <= __pa_symbol(_end)) || 444 && start <= __pa_symbol(_end)) ||
441 !e820_all_mapped(start, start+size, E820_RAM) || 445 !e820_all_mapped(start, start+size, E820_RAM) ||
442 memblock_is_region_reserved(start, size)) { 446 memblock_is_region_reserved(start, size)) {
@@ -489,18 +493,27 @@ static int __init efi_systab_init(void *phys)
489{ 493{
490 if (efi_enabled(EFI_64BIT)) { 494 if (efi_enabled(EFI_64BIT)) {
491 efi_system_table_64_t *systab64; 495 efi_system_table_64_t *systab64;
496 struct efi_setup_data *data = NULL;
492 u64 tmp = 0; 497 u64 tmp = 0;
493 498
499 if (efi_setup) {
500 data = early_memremap(efi_setup, sizeof(*data));
501 if (!data)
502 return -ENOMEM;
503 }
494 systab64 = early_ioremap((unsigned long)phys, 504 systab64 = early_ioremap((unsigned long)phys,
495 sizeof(*systab64)); 505 sizeof(*systab64));
496 if (systab64 == NULL) { 506 if (systab64 == NULL) {
497 pr_err("Couldn't map the system table!\n"); 507 pr_err("Couldn't map the system table!\n");
508 if (data)
509 early_iounmap(data, sizeof(*data));
498 return -ENOMEM; 510 return -ENOMEM;
499 } 511 }
500 512
501 efi_systab.hdr = systab64->hdr; 513 efi_systab.hdr = systab64->hdr;
502 efi_systab.fw_vendor = systab64->fw_vendor; 514 efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor :
503 tmp |= systab64->fw_vendor; 515 systab64->fw_vendor;
516 tmp |= data ? data->fw_vendor : systab64->fw_vendor;
504 efi_systab.fw_revision = systab64->fw_revision; 517 efi_systab.fw_revision = systab64->fw_revision;
505 efi_systab.con_in_handle = systab64->con_in_handle; 518 efi_systab.con_in_handle = systab64->con_in_handle;
506 tmp |= systab64->con_in_handle; 519 tmp |= systab64->con_in_handle;
@@ -514,15 +527,20 @@ static int __init efi_systab_init(void *phys)
514 tmp |= systab64->stderr_handle; 527 tmp |= systab64->stderr_handle;
515 efi_systab.stderr = systab64->stderr; 528 efi_systab.stderr = systab64->stderr;
516 tmp |= systab64->stderr; 529 tmp |= systab64->stderr;
517 efi_systab.runtime = (void *)(unsigned long)systab64->runtime; 530 efi_systab.runtime = data ?
518 tmp |= systab64->runtime; 531 (void *)(unsigned long)data->runtime :
532 (void *)(unsigned long)systab64->runtime;
533 tmp |= data ? data->runtime : systab64->runtime;
519 efi_systab.boottime = (void *)(unsigned long)systab64->boottime; 534 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
520 tmp |= systab64->boottime; 535 tmp |= systab64->boottime;
521 efi_systab.nr_tables = systab64->nr_tables; 536 efi_systab.nr_tables = systab64->nr_tables;
522 efi_systab.tables = systab64->tables; 537 efi_systab.tables = data ? (unsigned long)data->tables :
523 tmp |= systab64->tables; 538 systab64->tables;
539 tmp |= data ? data->tables : systab64->tables;
524 540
525 early_iounmap(systab64, sizeof(*systab64)); 541 early_iounmap(systab64, sizeof(*systab64));
542 if (data)
543 early_iounmap(data, sizeof(*data));
526#ifdef CONFIG_X86_32 544#ifdef CONFIG_X86_32
527 if (tmp >> 32) { 545 if (tmp >> 32) {
528 pr_err("EFI data located above 4GB, disabling EFI.\n"); 546 pr_err("EFI data located above 4GB, disabling EFI.\n");
@@ -626,6 +644,62 @@ static int __init efi_memmap_init(void)
626 return 0; 644 return 0;
627} 645}
628 646
647/*
648 * A number of config table entries get remapped to virtual addresses
649 * after entering EFI virtual mode. However, the kexec kernel requires
650 * their physical addresses therefore we pass them via setup_data and
651 * correct those entries to their respective physical addresses here.
652 *
653 * Currently only handles smbios which is necessary for some firmware
654 * implementation.
655 */
656static int __init efi_reuse_config(u64 tables, int nr_tables)
657{
658 int i, sz, ret = 0;
659 void *p, *tablep;
660 struct efi_setup_data *data;
661
662 if (!efi_setup)
663 return 0;
664
665 if (!efi_enabled(EFI_64BIT))
666 return 0;
667
668 data = early_memremap(efi_setup, sizeof(*data));
669 if (!data) {
670 ret = -ENOMEM;
671 goto out;
672 }
673
674 if (!data->smbios)
675 goto out_memremap;
676
677 sz = sizeof(efi_config_table_64_t);
678
679 p = tablep = early_memremap(tables, nr_tables * sz);
680 if (!p) {
681 pr_err("Could not map Configuration table!\n");
682 ret = -ENOMEM;
683 goto out_memremap;
684 }
685
686 for (i = 0; i < efi.systab->nr_tables; i++) {
687 efi_guid_t guid;
688
689 guid = ((efi_config_table_64_t *)p)->guid;
690
691 if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
692 ((efi_config_table_64_t *)p)->table = data->smbios;
693 p += sz;
694 }
695 early_iounmap(tablep, nr_tables * sz);
696
697out_memremap:
698 early_iounmap(data, sizeof(*data));
699out:
700 return ret;
701}
702
629void __init efi_init(void) 703void __init efi_init(void)
630{ 704{
631 efi_char16_t *c16; 705 efi_char16_t *c16;
@@ -651,6 +725,10 @@ void __init efi_init(void)
651 725
652 set_bit(EFI_SYSTEM_TABLES, &x86_efi_facility); 726 set_bit(EFI_SYSTEM_TABLES, &x86_efi_facility);
653 727
728 efi.config_table = (unsigned long)efi.systab->tables;
729 efi.fw_vendor = (unsigned long)efi.systab->fw_vendor;
730 efi.runtime = (unsigned long)efi.systab->runtime;
731
654 /* 732 /*
655 * Show what we know for posterity 733 * Show what we know for posterity
656 */ 734 */
@@ -667,6 +745,9 @@ void __init efi_init(void)
667 efi.systab->hdr.revision >> 16, 745 efi.systab->hdr.revision >> 16,
668 efi.systab->hdr.revision & 0xffff, vendor); 746 efi.systab->hdr.revision & 0xffff, vendor);
669 747
748 if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
749 return;
750
670 if (efi_config_init(arch_tables)) 751 if (efi_config_init(arch_tables))
671 return; 752 return;
672 753
@@ -684,15 +765,12 @@ void __init efi_init(void)
684 return; 765 return;
685 set_bit(EFI_RUNTIME_SERVICES, &x86_efi_facility); 766 set_bit(EFI_RUNTIME_SERVICES, &x86_efi_facility);
686 } 767 }
687
688 if (efi_memmap_init()) 768 if (efi_memmap_init())
689 return; 769 return;
690 770
691 set_bit(EFI_MEMMAP, &x86_efi_facility); 771 set_bit(EFI_MEMMAP, &x86_efi_facility);
692 772
693#if EFI_DEBUG
694 print_efi_memmap(); 773 print_efi_memmap();
695#endif
696} 774}
697 775
698void __init efi_late_init(void) 776void __init efi_late_init(void)
@@ -715,7 +793,7 @@ void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
715 set_memory_nx(addr, npages); 793 set_memory_nx(addr, npages);
716} 794}
717 795
718static void __init runtime_code_page_mkexec(void) 796void __init runtime_code_page_mkexec(void)
719{ 797{
720 efi_memory_desc_t *md; 798 efi_memory_desc_t *md;
721 void *p; 799 void *p;
@@ -741,36 +819,38 @@ void efi_memory_uc(u64 addr, unsigned long size)
741 set_memory_uc(addr, npages); 819 set_memory_uc(addr, npages);
742} 820}
743 821
744/* 822void __init old_map_region(efi_memory_desc_t *md)
745 * This function will switch the EFI runtime services to virtual mode.
746 * Essentially, look through the EFI memmap and map every region that
747 * has the runtime attribute bit set in its memory descriptor and update
748 * that memory descriptor with the virtual address obtained from ioremap().
749 * This enables the runtime services to be called without having to
750 * thunk back into physical mode for every invocation.
751 */
752void __init efi_enter_virtual_mode(void)
753{ 823{
754 efi_memory_desc_t *md, *prev_md = NULL; 824 u64 start_pfn, end_pfn, end;
755 efi_status_t status;
756 unsigned long size; 825 unsigned long size;
757 u64 end, systab, start_pfn, end_pfn; 826 void *va;
758 void *p, *va, *new_memmap = NULL;
759 int count = 0;
760 827
761 efi.systab = NULL; 828 start_pfn = PFN_DOWN(md->phys_addr);
829 size = md->num_pages << PAGE_SHIFT;
830 end = md->phys_addr + size;
831 end_pfn = PFN_UP(end);
762 832
763 /* 833 if (pfn_range_is_mapped(start_pfn, end_pfn)) {
764 * We don't do virtual mode, since we don't do runtime services, on 834 va = __va(md->phys_addr);
765 * non-native EFI
766 */
767 835
768 if (!efi_is_native()) { 836 if (!(md->attribute & EFI_MEMORY_WB))
769 efi_unmap_memmap(); 837 efi_memory_uc((u64)(unsigned long)va, size);
770 return; 838 } else
771 } 839 va = efi_ioremap(md->phys_addr, size,
840 md->type, md->attribute);
841
842 md->virt_addr = (u64) (unsigned long) va;
843 if (!va)
844 pr_err("ioremap of 0x%llX failed!\n",
845 (unsigned long long)md->phys_addr);
846}
847
848/* Merge contiguous regions of the same type and attribute */
849static void __init efi_merge_regions(void)
850{
851 void *p;
852 efi_memory_desc_t *md, *prev_md = NULL;
772 853
773 /* Merge contiguous regions of the same type and attribute */
774 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { 854 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
775 u64 prev_size; 855 u64 prev_size;
776 md = p; 856 md = p;
@@ -796,6 +876,77 @@ void __init efi_enter_virtual_mode(void)
796 } 876 }
797 prev_md = md; 877 prev_md = md;
798 } 878 }
879}
880
881static void __init get_systab_virt_addr(efi_memory_desc_t *md)
882{
883 unsigned long size;
884 u64 end, systab;
885
886 size = md->num_pages << EFI_PAGE_SHIFT;
887 end = md->phys_addr + size;
888 systab = (u64)(unsigned long)efi_phys.systab;
889 if (md->phys_addr <= systab && systab < end) {
890 systab += md->virt_addr - md->phys_addr;
891 efi.systab = (efi_system_table_t *)(unsigned long)systab;
892 }
893}
894
895static int __init save_runtime_map(void)
896{
897 efi_memory_desc_t *md;
898 void *tmp, *p, *q = NULL;
899 int count = 0;
900
901 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
902 md = p;
903
904 if (!(md->attribute & EFI_MEMORY_RUNTIME) ||
905 (md->type == EFI_BOOT_SERVICES_CODE) ||
906 (md->type == EFI_BOOT_SERVICES_DATA))
907 continue;
908 tmp = krealloc(q, (count + 1) * memmap.desc_size, GFP_KERNEL);
909 if (!tmp)
910 goto out;
911 q = tmp;
912
913 memcpy(q + count * memmap.desc_size, md, memmap.desc_size);
914 count++;
915 }
916
917 efi_runtime_map_setup(q, count, memmap.desc_size);
918
919 return 0;
920out:
921 kfree(q);
922 return -ENOMEM;
923}
924
925/*
926 * Map efi regions which were passed via setup_data. The virt_addr is a fixed
927 * addr which was used in first kernel of a kexec boot.
928 */
929static void __init efi_map_regions_fixed(void)
930{
931 void *p;
932 efi_memory_desc_t *md;
933
934 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
935 md = p;
936 efi_map_region_fixed(md); /* FIXME: add error handling */
937 get_systab_virt_addr(md);
938 }
939
940}
941
942/*
943 * Map efi memory ranges for runtime serivce and update new_memmap with virtual
944 * addresses.
945 */
946static void * __init efi_map_regions(int *count)
947{
948 efi_memory_desc_t *md;
949 void *p, *tmp, *new_memmap = NULL;
799 950
800 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { 951 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
801 md = p; 952 md = p;
@@ -807,53 +958,95 @@ void __init efi_enter_virtual_mode(void)
807 continue; 958 continue;
808 } 959 }
809 960
810 size = md->num_pages << EFI_PAGE_SHIFT; 961 efi_map_region(md);
811 end = md->phys_addr + size; 962 get_systab_virt_addr(md);
812 963
813 start_pfn = PFN_DOWN(md->phys_addr); 964 tmp = krealloc(new_memmap, (*count + 1) * memmap.desc_size,
814 end_pfn = PFN_UP(end); 965 GFP_KERNEL);
815 if (pfn_range_is_mapped(start_pfn, end_pfn)) { 966 if (!tmp)
816 va = __va(md->phys_addr); 967 goto out;
968 new_memmap = tmp;
969 memcpy(new_memmap + (*count * memmap.desc_size), md,
970 memmap.desc_size);
971 (*count)++;
972 }
817 973
818 if (!(md->attribute & EFI_MEMORY_WB)) 974 return new_memmap;
819 efi_memory_uc((u64)(unsigned long)va, size); 975out:
820 } else 976 kfree(new_memmap);
821 va = efi_ioremap(md->phys_addr, size, 977 return NULL;
822 md->type, md->attribute); 978}
823 979
824 md->virt_addr = (u64) (unsigned long) va; 980/*
981 * This function will switch the EFI runtime services to virtual mode.
982 * Essentially, we look through the EFI memmap and map every region that
983 * has the runtime attribute bit set in its memory descriptor into the
984 * ->trampoline_pgd page table using a top-down VA allocation scheme.
985 *
986 * The old method which used to update that memory descriptor with the
987 * virtual address obtained from ioremap() is still supported when the
988 * kernel is booted with efi=old_map on its command line. Same old
989 * method enabled the runtime services to be called without having to
990 * thunk back into physical mode for every invocation.
991 *
992 * The new method does a pagetable switch in a preemption-safe manner
993 * so that we're in a different address space when calling a runtime
994 * function. For function arguments passing we do copy the PGDs of the
995 * kernel page table into ->trampoline_pgd prior to each call.
996 *
997 * Specially for kexec boot, efi runtime maps in previous kernel should
998 * be passed in via setup_data. In that case runtime ranges will be mapped
999 * to the same virtual addresses as the first kernel.
1000 */
1001void __init efi_enter_virtual_mode(void)
1002{
1003 efi_status_t status;
1004 void *new_memmap = NULL;
1005 int err, count = 0;
825 1006
826 if (!va) { 1007 efi.systab = NULL;
827 pr_err("ioremap of 0x%llX failed!\n", 1008
828 (unsigned long long)md->phys_addr); 1009 /*
829 continue; 1010 * We don't do virtual mode, since we don't do runtime services, on
830 } 1011 * non-native EFI
1012 */
1013 if (!efi_is_native()) {
1014 efi_unmap_memmap();
1015 return;
1016 }
831 1017
832 systab = (u64) (unsigned long) efi_phys.systab; 1018 if (efi_setup) {
833 if (md->phys_addr <= systab && systab < end) { 1019 efi_map_regions_fixed();
834 systab += md->virt_addr - md->phys_addr; 1020 } else {
835 efi.systab = (efi_system_table_t *) (unsigned long) systab; 1021 efi_merge_regions();
1022 new_memmap = efi_map_regions(&count);
1023 if (!new_memmap) {
1024 pr_err("Error reallocating memory, EFI runtime non-functional!\n");
1025 return;
836 } 1026 }
837 new_memmap = krealloc(new_memmap,
838 (count + 1) * memmap.desc_size,
839 GFP_KERNEL);
840 memcpy(new_memmap + (count * memmap.desc_size), md,
841 memmap.desc_size);
842 count++;
843 } 1027 }
844 1028
1029 err = save_runtime_map();
1030 if (err)
1031 pr_err("Error saving runtime map, efi runtime on kexec non-functional!!\n");
1032
845 BUG_ON(!efi.systab); 1033 BUG_ON(!efi.systab);
846 1034
847 status = phys_efi_set_virtual_address_map( 1035 efi_setup_page_tables();
848 memmap.desc_size * count, 1036 efi_sync_low_kernel_mappings();
849 memmap.desc_size,
850 memmap.desc_version,
851 (efi_memory_desc_t *)__pa(new_memmap));
852 1037
853 if (status != EFI_SUCCESS) { 1038 if (!efi_setup) {
854 pr_alert("Unable to switch EFI into virtual mode " 1039 status = phys_efi_set_virtual_address_map(
855 "(status=%lx)!\n", status); 1040 memmap.desc_size * count,
856 panic("EFI call to SetVirtualAddressMap() failed!"); 1041 memmap.desc_size,
1042 memmap.desc_version,
1043 (efi_memory_desc_t *)__pa(new_memmap));
1044
1045 if (status != EFI_SUCCESS) {
1046 pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
1047 status);
1048 panic("EFI call to SetVirtualAddressMap() failed!");
1049 }
857 } 1050 }
858 1051
859 /* 1052 /*
@@ -876,8 +1069,8 @@ void __init efi_enter_virtual_mode(void)
876 efi.query_variable_info = virt_efi_query_variable_info; 1069 efi.query_variable_info = virt_efi_query_variable_info;
877 efi.update_capsule = virt_efi_update_capsule; 1070 efi.update_capsule = virt_efi_update_capsule;
878 efi.query_capsule_caps = virt_efi_query_capsule_caps; 1071 efi.query_capsule_caps = virt_efi_query_capsule_caps;
879 if (__supported_pte_mask & _PAGE_NX) 1072
880 runtime_code_page_mkexec(); 1073 efi_runtime_mkexec();
881 1074
882 kfree(new_memmap); 1075 kfree(new_memmap);
883 1076
@@ -1006,3 +1199,34 @@ efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
1006 return EFI_SUCCESS; 1199 return EFI_SUCCESS;
1007} 1200}
1008EXPORT_SYMBOL_GPL(efi_query_variable_store); 1201EXPORT_SYMBOL_GPL(efi_query_variable_store);
1202
1203static int __init parse_efi_cmdline(char *str)
1204{
1205 if (*str == '=')
1206 str++;
1207
1208 if (!strncmp(str, "old_map", 7))
1209 set_bit(EFI_OLD_MEMMAP, &x86_efi_facility);
1210
1211 return 0;
1212}
1213early_param("efi", parse_efi_cmdline);
1214
1215void __init efi_apply_memmap_quirks(void)
1216{
1217 /*
1218 * Once setup is done earlier, unmap the EFI memory map on mismatched
1219 * firmware/kernel architectures since there is no support for runtime
1220 * services.
1221 */
1222 if (!efi_is_native()) {
1223 pr_info("efi: Setup done, disabling due to 32/64-bit mismatch\n");
1224 efi_unmap_memmap();
1225 }
1226
1227 /*
1228 * UV doesn't support the new EFI pagetable mapping yet.
1229 */
1230 if (is_uv_system())
1231 set_bit(EFI_OLD_MEMMAP, &x86_efi_facility);
1232}