1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * EFI initialization 4 * 5 * Author: Jianmin Lv <lvjianmin@loongson.cn> 6 * Huacai Chen <chenhuacai@loongson.cn> 7 * 8 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited 9 */ 10 11 #include <linux/acpi.h> 12 #include <linux/efi.h> 13 #include <linux/efi-bgrt.h> 14 #include <linux/init.h> 15 #include <linux/kernel.h> 16 #include <linux/export.h> 17 #include <linux/io.h> 18 #include <linux/kobject.h> 19 #include <linux/memblock.h> 20 #include <linux/reboot.h> 21 #include <linux/sysfb.h> 22 #include <linux/uaccess.h> 23 24 #include <asm/early_ioremap.h> 25 #include <asm/efi.h> 26 #include <asm/loongson.h> 27 28 static unsigned long efi_nr_tables; 29 static unsigned long efi_config_table; 30 31 static unsigned long __initdata boot_memmap = EFI_INVALID_TABLE_ADDR; 32 static unsigned long __initdata fdt_pointer = EFI_INVALID_TABLE_ADDR; 33 34 static efi_system_table_t *efi_systab; 35 static efi_config_table_type_t arch_tables[] __initdata = { 36 {LINUX_EFI_BOOT_MEMMAP_GUID, &boot_memmap, "MEMMAP" }, 37 {DEVICE_TREE_GUID, &fdt_pointer, "FDTPTR" }, 38 {}, 39 }; 40 41 void __init *efi_fdt_pointer(void) 42 { 43 if (!efi_systab) 44 return NULL; 45 46 if (fdt_pointer == EFI_INVALID_TABLE_ADDR) 47 return NULL; 48 49 return early_memremap_ro(fdt_pointer, SZ_64K); 50 } 51 52 void __init efi_runtime_init(void) 53 { 54 if (!efi_enabled(EFI_BOOT) || !efi_systab->runtime) 55 return; 56 57 if (efi_runtime_disabled()) { 58 pr_info("EFI runtime services will be disabled.\n"); 59 return; 60 } 61 62 efi.runtime = (efi_runtime_services_t *)efi_systab->runtime; 63 efi.runtime_version = (unsigned int)efi.runtime->hdr.revision; 64 65 efi_native_runtime_setup(); 66 set_bit(EFI_RUNTIME_SERVICES, &efi.flags); 67 } 68 69 bool efi_poweroff_required(void) 70 { 71 return efi_enabled(EFI_RUNTIME_SERVICES) && 72 (acpi_gbl_reduced_hardware || acpi_no_s5); 73 } 74 75 unsigned long __initdata primary_display_table = EFI_INVALID_TABLE_ADDR; 76 77 #if defined(CONFIG_SYSFB) || defined(CONFIG_EFI_EARLYCON) 78 struct sysfb_display_info sysfb_primary_display __section(".data"); 79 EXPORT_SYMBOL_GPL(sysfb_primary_display); 80 #endif 81 82 static void __init init_primary_display(void) 83 { 84 struct sysfb_display_info *dpy; 85 86 if (primary_display_table == EFI_INVALID_TABLE_ADDR) 87 return; 88 89 dpy = early_memremap(primary_display_table, sizeof(*dpy)); 90 if (!dpy) { 91 pr_err("Could not map primary_display config table\n"); 92 return; 93 } 94 sysfb_primary_display = *dpy; 95 memset(dpy, 0, sizeof(*dpy)); 96 early_memunmap(dpy, sizeof(*dpy)); 97 98 memblock_reserve(__screen_info_lfb_base(&sysfb_primary_display.screen), 99 sysfb_primary_display.screen.lfb_size); 100 } 101 102 void __init efi_init(void) 103 { 104 int size; 105 void *config_tables; 106 struct efi_boot_memmap *tbl; 107 108 if (!efi_system_table) 109 return; 110 111 efi_systab = (efi_system_table_t *)early_memremap_ro(efi_system_table, sizeof(*efi_systab)); 112 if (!efi_systab) { 113 pr_err("Can't find EFI system table.\n"); 114 return; 115 } 116 117 efi_systab_report_header(&efi_systab->hdr, efi_systab->fw_vendor); 118 119 if (IS_ENABLED(CONFIG_64BIT)) 120 set_bit(EFI_64BIT, &efi.flags); 121 122 efi_nr_tables = efi_systab->nr_tables; 123 efi_config_table = (unsigned long)efi_systab->tables; 124 125 size = sizeof(efi_config_table_t); 126 config_tables = early_memremap(efi_config_table, efi_nr_tables * size); 127 efi_config_parse_tables(config_tables, efi_systab->nr_tables, arch_tables); 128 early_memunmap(config_tables, efi_nr_tables * size); 129 130 set_bit(EFI_CONFIG_TABLES, &efi.flags); 131 132 if (IS_ENABLED(CONFIG_EFI_EARLYCON) || IS_ENABLED(CONFIG_SYSFB)) 133 init_primary_display(); 134 135 if (boot_memmap == EFI_INVALID_TABLE_ADDR) 136 return; 137 138 tbl = early_memremap_ro(boot_memmap, sizeof(*tbl)); 139 if (tbl) { 140 struct efi_memory_map_data data; 141 142 data.phys_map = boot_memmap + sizeof(*tbl); 143 data.size = tbl->map_size; 144 data.desc_size = tbl->desc_size; 145 data.desc_version = tbl->desc_ver; 146 147 if (efi_memmap_init_early(&data) < 0) 148 panic("Unable to map EFI memory map.\n"); 149 150 /* 151 * Reserve the physical memory region occupied by the EFI 152 * memory map table (header + descriptors). This is crucial 153 * for kdump, as the kdump kernel relies on this original 154 * memmap passed by the bootloader. Without reservation, 155 * this region could be overwritten by the primary kernel. 156 * Also, set the EFI_PRESERVE_BS_REGIONS flag to indicate that 157 * critical boot services code/data regions like this are preserved. 158 */ 159 memblock_reserve((phys_addr_t)boot_memmap, sizeof(*tbl) + data.size); 160 set_bit(EFI_PRESERVE_BS_REGIONS, &efi.flags); 161 162 early_memunmap(tbl, sizeof(*tbl)); 163 } 164 165 efi_esrt_init(); 166 } 167