1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Extensible Firmware Interface 4 * 5 * Based on Extensible Firmware Interface Specification version 2.4 6 * 7 * Copyright (C) 2013 - 2015 Linaro Ltd. 8 */ 9 10 #define pr_fmt(fmt) "efi: " fmt 11 12 #include <linux/efi.h> 13 #include <linux/fwnode.h> 14 #include <linux/init.h> 15 #include <linux/memblock.h> 16 #include <linux/mm_types.h> 17 #include <linux/of.h> 18 #include <linux/of_address.h> 19 #include <linux/of_fdt.h> 20 #include <linux/platform_device.h> 21 #include <linux/screen_info.h> 22 23 #include <asm/efi.h> 24 25 static int __init is_memory(efi_memory_desc_t *md) 26 { 27 if (md->attribute & (EFI_MEMORY_WB|EFI_MEMORY_WT|EFI_MEMORY_WC)) 28 return 1; 29 return 0; 30 } 31 32 /* 33 * Translate a EFI virtual address into a physical address: this is necessary, 34 * as some data members of the EFI system table are virtually remapped after 35 * SetVirtualAddressMap() has been called. 36 */ 37 static phys_addr_t __init efi_to_phys(unsigned long addr) 38 { 39 efi_memory_desc_t *md; 40 41 for_each_efi_memory_desc(md) { 42 if (!(md->attribute & EFI_MEMORY_RUNTIME)) 43 continue; 44 if (md->virt_addr == 0) 45 /* no virtual mapping has been installed by the stub */ 46 break; 47 if (md->virt_addr <= addr && 48 (addr - md->virt_addr) < (md->num_pages << EFI_PAGE_SHIFT)) 49 return md->phys_addr + addr - md->virt_addr; 50 } 51 return addr; 52 } 53 54 extern __weak const efi_config_table_type_t efi_arch_tables[]; 55 56 static void __init init_screen_info(void) 57 { 58 if (screen_info.orig_video_isVGA == VIDEO_TYPE_EFI && 59 memblock_is_map_memory(screen_info.lfb_base)) 60 memblock_mark_nomap(screen_info.lfb_base, screen_info.lfb_size); 61 } 62 63 static int __init uefi_init(u64 efi_system_table) 64 { 65 efi_config_table_t *config_tables; 66 efi_system_table_t *systab; 67 size_t table_size; 68 int retval; 69 70 systab = early_memremap_ro(efi_system_table, sizeof(efi_system_table_t)); 71 if (systab == NULL) { 72 pr_warn("Unable to map EFI system table.\n"); 73 return -ENOMEM; 74 } 75 76 set_bit(EFI_BOOT, &efi.flags); 77 if (IS_ENABLED(CONFIG_64BIT)) 78 set_bit(EFI_64BIT, &efi.flags); 79 80 retval = efi_systab_check_header(&systab->hdr, 2); 81 if (retval) 82 goto out; 83 84 efi.runtime = systab->runtime; 85 efi.runtime_version = systab->hdr.revision; 86 87 efi_systab_report_header(&systab->hdr, efi_to_phys(systab->fw_vendor)); 88 89 table_size = sizeof(efi_config_table_t) * systab->nr_tables; 90 config_tables = early_memremap_ro(efi_to_phys(systab->tables), 91 table_size); 92 if (config_tables == NULL) { 93 pr_warn("Unable to map EFI config table array.\n"); 94 retval = -ENOMEM; 95 goto out; 96 } 97 retval = efi_config_parse_tables(config_tables, systab->nr_tables, 98 efi_arch_tables); 99 100 early_memunmap(config_tables, table_size); 101 out: 102 early_memunmap(systab, sizeof(efi_system_table_t)); 103 return retval; 104 } 105 106 /* 107 * Return true for regions that can be used as System RAM. 108 */ 109 static __init int is_usable_memory(efi_memory_desc_t *md) 110 { 111 switch (md->type) { 112 case EFI_LOADER_CODE: 113 case EFI_LOADER_DATA: 114 case EFI_ACPI_RECLAIM_MEMORY: 115 case EFI_BOOT_SERVICES_CODE: 116 case EFI_BOOT_SERVICES_DATA: 117 case EFI_CONVENTIONAL_MEMORY: 118 case EFI_PERSISTENT_MEMORY: 119 /* 120 * Special purpose memory is 'soft reserved', which means it 121 * is set aside initially, but can be hotplugged back in or 122 * be assigned to the dax driver after boot. 123 */ 124 if (efi_soft_reserve_enabled() && 125 (md->attribute & EFI_MEMORY_SP)) 126 return false; 127 128 /* 129 * According to the spec, these regions are no longer reserved 130 * after calling ExitBootServices(). However, we can only use 131 * them as System RAM if they can be mapped writeback cacheable. 132 */ 133 return (md->attribute & EFI_MEMORY_WB); 134 default: 135 break; 136 } 137 return false; 138 } 139 140 static __init void reserve_regions(void) 141 { 142 efi_memory_desc_t *md; 143 u64 paddr, npages, size; 144 145 if (efi_enabled(EFI_DBG)) 146 pr_info("Processing EFI memory map:\n"); 147 148 /* 149 * Discard memblocks discovered so far: if there are any at this 150 * point, they originate from memory nodes in the DT, and UEFI 151 * uses its own memory map instead. 152 */ 153 memblock_dump_all(); 154 memblock_remove(0, PHYS_ADDR_MAX); 155 156 for_each_efi_memory_desc(md) { 157 paddr = md->phys_addr; 158 npages = md->num_pages; 159 160 if (efi_enabled(EFI_DBG)) { 161 char buf[64]; 162 163 pr_info(" 0x%012llx-0x%012llx %s\n", 164 paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1, 165 efi_md_typeattr_format(buf, sizeof(buf), md)); 166 } 167 168 memrange_efi_to_native(&paddr, &npages); 169 size = npages << PAGE_SHIFT; 170 171 if (is_memory(md)) { 172 early_init_dt_add_memory_arch(paddr, size); 173 174 if (!is_usable_memory(md)) 175 memblock_mark_nomap(paddr, size); 176 177 /* keep ACPI reclaim memory intact for kexec etc. */ 178 if (md->type == EFI_ACPI_RECLAIM_MEMORY) 179 memblock_reserve(paddr, size); 180 } 181 } 182 } 183 184 void __init efi_init(void) 185 { 186 struct efi_memory_map_data data; 187 u64 efi_system_table; 188 189 /* Grab UEFI information placed in FDT by stub */ 190 efi_system_table = efi_get_fdt_params(&data); 191 if (!efi_system_table) 192 return; 193 194 if (efi_memmap_init_early(&data) < 0) { 195 /* 196 * If we are booting via UEFI, the UEFI memory map is the only 197 * description of memory we have, so there is little point in 198 * proceeding if we cannot access it. 199 */ 200 panic("Unable to map EFI memory map.\n"); 201 } 202 203 WARN(efi.memmap.desc_version != 1, 204 "Unexpected EFI_MEMORY_DESCRIPTOR version %ld", 205 efi.memmap.desc_version); 206 207 if (uefi_init(efi_system_table) < 0) { 208 efi_memmap_unmap(); 209 return; 210 } 211 212 reserve_regions(); 213 /* 214 * For memblock manipulation, the cap should come after the memblock_add(). 215 * And now, memblock is fully populated, it is time to do capping. 216 */ 217 early_init_dt_check_for_usable_mem_range(); 218 efi_find_mirror(); 219 efi_esrt_init(); 220 efi_mokvar_table_init(); 221 222 memblock_reserve(data.phys_map & PAGE_MASK, 223 PAGE_ALIGN(data.size + (data.phys_map & ~PAGE_MASK))); 224 225 init_screen_info(); 226 } 227