Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Extensible Firmware Interface |
| 3 | * |
| 4 | * Based on Extensible Firmware Interface Specification version 2.4 |
| 5 | * |
| 6 | * Copyright (C) 2013 - 2015 Linaro Ltd. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | * |
| 12 | */ |
| 13 | |
Ard Biesheuvel | 801820b | 2016-04-25 21:06:53 +0100 | [diff] [blame] | 14 | #define pr_fmt(fmt) "efi: " fmt |
| 15 | |
Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 16 | #include <linux/efi.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/memblock.h> |
| 19 | #include <linux/mm_types.h> |
| 20 | #include <linux/of.h> |
| 21 | #include <linux/of_fdt.h> |
Ard Biesheuvel | e3271c9 | 2016-04-25 21:06:55 +0100 | [diff] [blame] | 22 | #include <linux/platform_device.h> |
Ard Biesheuvel | 801820b | 2016-04-25 21:06:53 +0100 | [diff] [blame] | 23 | #include <linux/screen_info.h> |
Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 24 | |
| 25 | #include <asm/efi.h> |
| 26 | |
Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 27 | u64 efi_system_table; |
| 28 | |
Ard Biesheuvel | cb82cce | 2016-08-25 18:17:09 +0200 | [diff] [blame^] | 29 | static int __init is_memory(efi_memory_desc_t *md) |
Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 30 | { |
Ard Biesheuvel | cb82cce | 2016-08-25 18:17:09 +0200 | [diff] [blame^] | 31 | if (md->attribute & (EFI_MEMORY_WB|EFI_MEMORY_WT|EFI_MEMORY_WC)) |
Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 32 | return 1; |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | /* |
| 37 | * Translate a EFI virtual address into a physical address: this is necessary, |
| 38 | * as some data members of the EFI system table are virtually remapped after |
| 39 | * SetVirtualAddressMap() has been called. |
| 40 | */ |
| 41 | static phys_addr_t efi_to_phys(unsigned long addr) |
| 42 | { |
| 43 | efi_memory_desc_t *md; |
| 44 | |
Matt Fleming | 884f4f6 | 2016-04-25 21:06:39 +0100 | [diff] [blame] | 45 | for_each_efi_memory_desc(md) { |
Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 46 | if (!(md->attribute & EFI_MEMORY_RUNTIME)) |
| 47 | continue; |
| 48 | if (md->virt_addr == 0) |
| 49 | /* no virtual mapping has been installed by the stub */ |
| 50 | break; |
| 51 | if (md->virt_addr <= addr && |
| 52 | (addr - md->virt_addr) < (md->num_pages << EFI_PAGE_SHIFT)) |
| 53 | return md->phys_addr + addr - md->virt_addr; |
| 54 | } |
| 55 | return addr; |
| 56 | } |
| 57 | |
Ard Biesheuvel | 801820b | 2016-04-25 21:06:53 +0100 | [diff] [blame] | 58 | static __initdata unsigned long screen_info_table = EFI_INVALID_TABLE_ADDR; |
| 59 | |
| 60 | static __initdata efi_config_table_type_t arch_tables[] = { |
| 61 | {LINUX_EFI_ARM_SCREEN_INFO_TABLE_GUID, NULL, &screen_info_table}, |
| 62 | {NULL_GUID, NULL, NULL} |
| 63 | }; |
| 64 | |
| 65 | static void __init init_screen_info(void) |
| 66 | { |
| 67 | struct screen_info *si; |
| 68 | |
| 69 | if (screen_info_table != EFI_INVALID_TABLE_ADDR) { |
| 70 | si = early_memremap_ro(screen_info_table, sizeof(*si)); |
| 71 | if (!si) { |
| 72 | pr_err("Could not map screen_info config table\n"); |
| 73 | return; |
| 74 | } |
| 75 | screen_info = *si; |
| 76 | early_memunmap(si, sizeof(*si)); |
| 77 | |
| 78 | /* dummycon on ARM needs non-zero values for columns/lines */ |
| 79 | screen_info.orig_video_cols = 80; |
| 80 | screen_info.orig_video_lines = 25; |
| 81 | } |
Ard Biesheuvel | e3271c9 | 2016-04-25 21:06:55 +0100 | [diff] [blame] | 82 | |
| 83 | if (screen_info.orig_video_isVGA == VIDEO_TYPE_EFI && |
| 84 | memblock_is_map_memory(screen_info.lfb_base)) |
| 85 | memblock_mark_nomap(screen_info.lfb_base, screen_info.lfb_size); |
Ard Biesheuvel | 801820b | 2016-04-25 21:06:53 +0100 | [diff] [blame] | 86 | } |
| 87 | |
Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 88 | static int __init uefi_init(void) |
| 89 | { |
| 90 | efi_char16_t *c16; |
| 91 | void *config_tables; |
Ard Biesheuvel | f7d9248 | 2015-11-30 13:28:19 +0100 | [diff] [blame] | 92 | size_t table_size; |
Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 93 | char vendor[100] = "unknown"; |
| 94 | int i, retval; |
| 95 | |
Ard Biesheuvel | 2eec5de | 2016-02-17 12:36:00 +0000 | [diff] [blame] | 96 | efi.systab = early_memremap_ro(efi_system_table, |
| 97 | sizeof(efi_system_table_t)); |
Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 98 | if (efi.systab == NULL) { |
| 99 | pr_warn("Unable to map EFI system table.\n"); |
| 100 | return -ENOMEM; |
| 101 | } |
| 102 | |
| 103 | set_bit(EFI_BOOT, &efi.flags); |
Ard Biesheuvel | f7d9248 | 2015-11-30 13:28:19 +0100 | [diff] [blame] | 104 | if (IS_ENABLED(CONFIG_64BIT)) |
| 105 | set_bit(EFI_64BIT, &efi.flags); |
Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 106 | |
| 107 | /* |
| 108 | * Verify the EFI Table |
| 109 | */ |
| 110 | if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) { |
| 111 | pr_err("System table signature incorrect\n"); |
| 112 | retval = -EINVAL; |
| 113 | goto out; |
| 114 | } |
| 115 | if ((efi.systab->hdr.revision >> 16) < 2) |
| 116 | pr_warn("Warning: EFI system table version %d.%02d, expected 2.00 or greater\n", |
| 117 | efi.systab->hdr.revision >> 16, |
| 118 | efi.systab->hdr.revision & 0xffff); |
| 119 | |
Ard Biesheuvel | 14c43be | 2016-04-25 21:06:34 +0100 | [diff] [blame] | 120 | efi.runtime_version = efi.systab->hdr.revision; |
| 121 | |
Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 122 | /* Show what we know for posterity */ |
Ard Biesheuvel | 2eec5de | 2016-02-17 12:36:00 +0000 | [diff] [blame] | 123 | c16 = early_memremap_ro(efi_to_phys(efi.systab->fw_vendor), |
| 124 | sizeof(vendor) * sizeof(efi_char16_t)); |
Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 125 | if (c16) { |
| 126 | for (i = 0; i < (int) sizeof(vendor) - 1 && *c16; ++i) |
| 127 | vendor[i] = c16[i]; |
| 128 | vendor[i] = '\0'; |
| 129 | early_memunmap(c16, sizeof(vendor) * sizeof(efi_char16_t)); |
| 130 | } |
| 131 | |
| 132 | pr_info("EFI v%u.%.02u by %s\n", |
| 133 | efi.systab->hdr.revision >> 16, |
| 134 | efi.systab->hdr.revision & 0xffff, vendor); |
| 135 | |
| 136 | table_size = sizeof(efi_config_table_64_t) * efi.systab->nr_tables; |
Ard Biesheuvel | 2eec5de | 2016-02-17 12:36:00 +0000 | [diff] [blame] | 137 | config_tables = early_memremap_ro(efi_to_phys(efi.systab->tables), |
| 138 | table_size); |
Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 139 | if (config_tables == NULL) { |
| 140 | pr_warn("Unable to map EFI config table array.\n"); |
| 141 | retval = -ENOMEM; |
| 142 | goto out; |
| 143 | } |
| 144 | retval = efi_config_parse_tables(config_tables, efi.systab->nr_tables, |
Ard Biesheuvel | 801820b | 2016-04-25 21:06:53 +0100 | [diff] [blame] | 145 | sizeof(efi_config_table_t), |
| 146 | arch_tables); |
Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 147 | |
| 148 | early_memunmap(config_tables, table_size); |
| 149 | out: |
| 150 | early_memunmap(efi.systab, sizeof(efi_system_table_t)); |
| 151 | return retval; |
| 152 | } |
| 153 | |
| 154 | /* |
Ard Biesheuvel | cb82cce | 2016-08-25 18:17:09 +0200 | [diff] [blame^] | 155 | * Return true for regions that can be used as System RAM. |
Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 156 | */ |
Ard Biesheuvel | cb82cce | 2016-08-25 18:17:09 +0200 | [diff] [blame^] | 157 | static __init int is_usable_memory(efi_memory_desc_t *md) |
Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 158 | { |
| 159 | switch (md->type) { |
| 160 | case EFI_LOADER_CODE: |
| 161 | case EFI_LOADER_DATA: |
| 162 | case EFI_BOOT_SERVICES_CODE: |
| 163 | case EFI_BOOT_SERVICES_DATA: |
| 164 | case EFI_CONVENTIONAL_MEMORY: |
| 165 | case EFI_PERSISTENT_MEMORY: |
Ard Biesheuvel | cb82cce | 2016-08-25 18:17:09 +0200 | [diff] [blame^] | 166 | /* |
| 167 | * According to the spec, these regions are no longer reserved |
| 168 | * after calling ExitBootServices(). However, we can only use |
| 169 | * them as System RAM if they can be mapped writeback cacheable. |
| 170 | */ |
| 171 | return (md->attribute & EFI_MEMORY_WB); |
Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 172 | default: |
| 173 | break; |
| 174 | } |
Ard Biesheuvel | cb82cce | 2016-08-25 18:17:09 +0200 | [diff] [blame^] | 175 | return false; |
Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | static __init void reserve_regions(void) |
| 179 | { |
| 180 | efi_memory_desc_t *md; |
| 181 | u64 paddr, npages, size; |
| 182 | |
| 183 | if (efi_enabled(EFI_DBG)) |
| 184 | pr_info("Processing EFI memory map:\n"); |
| 185 | |
Ard Biesheuvel | 500899c | 2016-04-08 15:50:23 -0700 | [diff] [blame] | 186 | /* |
| 187 | * Discard memblocks discovered so far: if there are any at this |
| 188 | * point, they originate from memory nodes in the DT, and UEFI |
| 189 | * uses its own memory map instead. |
| 190 | */ |
| 191 | memblock_dump_all(); |
Arnd Bergmann | 7464b6e | 2016-04-18 10:34:19 +0200 | [diff] [blame] | 192 | memblock_remove(0, (phys_addr_t)ULLONG_MAX); |
Ard Biesheuvel | 500899c | 2016-04-08 15:50:23 -0700 | [diff] [blame] | 193 | |
Matt Fleming | 884f4f6 | 2016-04-25 21:06:39 +0100 | [diff] [blame] | 194 | for_each_efi_memory_desc(md) { |
Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 195 | paddr = md->phys_addr; |
| 196 | npages = md->num_pages; |
| 197 | |
| 198 | if (efi_enabled(EFI_DBG)) { |
| 199 | char buf[64]; |
| 200 | |
Ard Biesheuvel | cb82cce | 2016-08-25 18:17:09 +0200 | [diff] [blame^] | 201 | pr_info(" 0x%012llx-0x%012llx %s\n", |
Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 202 | paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1, |
Ard Biesheuvel | cb82cce | 2016-08-25 18:17:09 +0200 | [diff] [blame^] | 203 | efi_md_typeattr_format(buf, sizeof(buf), md)); |
Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | memrange_efi_to_native(&paddr, &npages); |
| 207 | size = npages << PAGE_SHIFT; |
| 208 | |
Ard Biesheuvel | cb82cce | 2016-08-25 18:17:09 +0200 | [diff] [blame^] | 209 | if (is_memory(md)) { |
Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 210 | early_init_dt_add_memory_arch(paddr, size); |
| 211 | |
Ard Biesheuvel | cb82cce | 2016-08-25 18:17:09 +0200 | [diff] [blame^] | 212 | if (!is_usable_memory(md)) |
| 213 | memblock_mark_nomap(paddr, size); |
| 214 | } |
Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 215 | } |
Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | void __init efi_init(void) |
| 219 | { |
Matt Fleming | 9479c7c | 2016-02-26 21:22:05 +0000 | [diff] [blame] | 220 | struct efi_memory_map_data data; |
Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 221 | struct efi_fdt_params params; |
| 222 | |
| 223 | /* Grab UEFI information placed in FDT by stub */ |
| 224 | if (!efi_get_fdt_params(¶ms)) |
| 225 | return; |
| 226 | |
| 227 | efi_system_table = params.system_table; |
| 228 | |
Matt Fleming | 9479c7c | 2016-02-26 21:22:05 +0000 | [diff] [blame] | 229 | data.desc_version = params.desc_ver; |
| 230 | data.desc_size = params.desc_size; |
| 231 | data.size = params.mmap_size; |
| 232 | data.phys_map = params.mmap; |
| 233 | |
| 234 | if (efi_memmap_init_early(&data) < 0) { |
Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 235 | /* |
| 236 | * If we are booting via UEFI, the UEFI memory map is the only |
| 237 | * description of memory we have, so there is little point in |
| 238 | * proceeding if we cannot access it. |
| 239 | */ |
| 240 | panic("Unable to map EFI memory map.\n"); |
| 241 | } |
Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 242 | |
Ard Biesheuvel | 0d054ad | 2016-04-25 21:06:40 +0100 | [diff] [blame] | 243 | WARN(efi.memmap.desc_version != 1, |
| 244 | "Unexpected EFI_MEMORY_DESCRIPTOR version %ld", |
| 245 | efi.memmap.desc_version); |
| 246 | |
Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 247 | if (uefi_init() < 0) |
| 248 | return; |
| 249 | |
| 250 | reserve_regions(); |
Ard Biesheuvel | 789957e | 2016-04-25 21:06:46 +0100 | [diff] [blame] | 251 | efi_memattr_init(); |
Ard Biesheuvel | 2ead308 | 2016-07-11 21:00:46 +0200 | [diff] [blame] | 252 | efi_esrt_init(); |
Matt Fleming | 9479c7c | 2016-02-26 21:22:05 +0000 | [diff] [blame] | 253 | efi_memmap_unmap(); |
Ard Biesheuvel | 7cc8cbc | 2016-03-30 09:46:23 +0200 | [diff] [blame] | 254 | |
Ard Biesheuvel | 249f763 | 2016-04-25 21:07:02 +0100 | [diff] [blame] | 255 | memblock_reserve(params.mmap & PAGE_MASK, |
| 256 | PAGE_ALIGN(params.mmap_size + |
| 257 | (params.mmap & ~PAGE_MASK))); |
Ard Biesheuvel | 801820b | 2016-04-25 21:06:53 +0100 | [diff] [blame] | 258 | |
| 259 | init_screen_info(); |
Ard Biesheuvel | e5bc22a | 2015-11-30 13:28:18 +0100 | [diff] [blame] | 260 | } |
Ard Biesheuvel | e3271c9 | 2016-04-25 21:06:55 +0100 | [diff] [blame] | 261 | |
| 262 | static int __init register_gop_device(void) |
| 263 | { |
| 264 | void *pd; |
| 265 | |
| 266 | if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI) |
| 267 | return 0; |
| 268 | |
| 269 | pd = platform_device_register_data(NULL, "efi-framebuffer", 0, |
| 270 | &screen_info, sizeof(screen_info)); |
| 271 | return PTR_ERR_OR_ZERO(pd); |
| 272 | } |
| 273 | subsys_initcall(register_gop_device); |