Saurabh Tangri | eeb9db0 | 2014-06-02 05:18:35 -0700 | [diff] [blame] | 1 | #include <linux/init.h> |
| 2 | #include <linux/kernel.h> |
| 3 | #include <linux/string.h> |
| 4 | #include <linux/time.h> |
| 5 | #include <linux/types.h> |
| 6 | #include <linux/efi.h> |
| 7 | #include <linux/slab.h> |
| 8 | #include <linux/memblock.h> |
| 9 | #include <linux/bootmem.h> |
Matt Fleming | 44be28e | 2014-06-13 12:39:55 +0100 | [diff] [blame] | 10 | #include <linux/acpi.h> |
Alex Thorlton | d394f2d | 2015-12-11 14:59:45 -0600 | [diff] [blame^] | 11 | #include <linux/dmi.h> |
Saurabh Tangri | eeb9db0 | 2014-06-02 05:18:35 -0700 | [diff] [blame] | 12 | #include <asm/efi.h> |
| 13 | #include <asm/uv/uv.h> |
| 14 | |
| 15 | #define EFI_MIN_RESERVE 5120 |
| 16 | |
| 17 | #define EFI_DUMMY_GUID \ |
| 18 | EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9) |
| 19 | |
| 20 | static efi_char16_t efi_dummy_name[6] = { 'D', 'U', 'M', 'M', 'Y', 0 }; |
| 21 | |
| 22 | static bool efi_no_storage_paranoia; |
| 23 | |
| 24 | /* |
| 25 | * Some firmware implementations refuse to boot if there's insufficient |
| 26 | * space in the variable store. The implementation of garbage collection |
| 27 | * in some FW versions causes stale (deleted) variables to take up space |
| 28 | * longer than intended and space is only freed once the store becomes |
| 29 | * almost completely full. |
| 30 | * |
| 31 | * Enabling this option disables the space checks in |
| 32 | * efi_query_variable_store() and forces garbage collection. |
| 33 | * |
| 34 | * Only enable this option if deleting EFI variables does not free up |
| 35 | * space in your variable store, e.g. if despite deleting variables |
| 36 | * you're unable to create new ones. |
| 37 | */ |
| 38 | static int __init setup_storage_paranoia(char *arg) |
| 39 | { |
| 40 | efi_no_storage_paranoia = true; |
| 41 | return 0; |
| 42 | } |
| 43 | early_param("efi_no_storage_paranoia", setup_storage_paranoia); |
| 44 | |
| 45 | /* |
| 46 | * Deleting the dummy variable which kicks off garbage collection |
| 47 | */ |
| 48 | void efi_delete_dummy_variable(void) |
| 49 | { |
| 50 | efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID, |
| 51 | EFI_VARIABLE_NON_VOLATILE | |
| 52 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 53 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 54 | 0, NULL); |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Some firmware implementations refuse to boot if there's insufficient space |
| 59 | * in the variable store. Ensure that we never use more than a safe limit. |
| 60 | * |
| 61 | * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable |
| 62 | * store. |
| 63 | */ |
| 64 | efi_status_t efi_query_variable_store(u32 attributes, unsigned long size) |
| 65 | { |
| 66 | efi_status_t status; |
| 67 | u64 storage_size, remaining_size, max_size; |
| 68 | |
| 69 | if (!(attributes & EFI_VARIABLE_NON_VOLATILE)) |
| 70 | return 0; |
| 71 | |
| 72 | status = efi.query_variable_info(attributes, &storage_size, |
| 73 | &remaining_size, &max_size); |
| 74 | if (status != EFI_SUCCESS) |
| 75 | return status; |
| 76 | |
| 77 | /* |
| 78 | * We account for that by refusing the write if permitting it would |
| 79 | * reduce the available space to under 5KB. This figure was provided by |
| 80 | * Samsung, so should be safe. |
| 81 | */ |
| 82 | if ((remaining_size - size < EFI_MIN_RESERVE) && |
| 83 | !efi_no_storage_paranoia) { |
| 84 | |
| 85 | /* |
| 86 | * Triggering garbage collection may require that the firmware |
| 87 | * generate a real EFI_OUT_OF_RESOURCES error. We can force |
| 88 | * that by attempting to use more space than is available. |
| 89 | */ |
| 90 | unsigned long dummy_size = remaining_size + 1024; |
| 91 | void *dummy = kzalloc(dummy_size, GFP_ATOMIC); |
| 92 | |
| 93 | if (!dummy) |
| 94 | return EFI_OUT_OF_RESOURCES; |
| 95 | |
| 96 | status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID, |
| 97 | EFI_VARIABLE_NON_VOLATILE | |
| 98 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 99 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 100 | dummy_size, dummy); |
| 101 | |
| 102 | if (status == EFI_SUCCESS) { |
| 103 | /* |
| 104 | * This should have failed, so if it didn't make sure |
| 105 | * that we delete it... |
| 106 | */ |
| 107 | efi_delete_dummy_variable(); |
| 108 | } |
| 109 | |
| 110 | kfree(dummy); |
| 111 | |
| 112 | /* |
| 113 | * The runtime code may now have triggered a garbage collection |
| 114 | * run, so check the variable info again |
| 115 | */ |
| 116 | status = efi.query_variable_info(attributes, &storage_size, |
| 117 | &remaining_size, &max_size); |
| 118 | |
| 119 | if (status != EFI_SUCCESS) |
| 120 | return status; |
| 121 | |
| 122 | /* |
| 123 | * There still isn't enough room, so return an error |
| 124 | */ |
| 125 | if (remaining_size - size < EFI_MIN_RESERVE) |
| 126 | return EFI_OUT_OF_RESOURCES; |
| 127 | } |
| 128 | |
| 129 | return EFI_SUCCESS; |
| 130 | } |
| 131 | EXPORT_SYMBOL_GPL(efi_query_variable_store); |
| 132 | |
| 133 | /* |
| 134 | * The UEFI specification makes it clear that the operating system is free to do |
| 135 | * whatever it wants with boot services code after ExitBootServices() has been |
| 136 | * called. Ignoring this recommendation a significant bunch of EFI implementations |
| 137 | * continue calling into boot services code (SetVirtualAddressMap). In order to |
| 138 | * work around such buggy implementations we reserve boot services region during |
| 139 | * EFI init and make sure it stays executable. Then, after SetVirtualAddressMap(), it |
| 140 | * is discarded. |
| 141 | */ |
| 142 | void __init efi_reserve_boot_services(void) |
| 143 | { |
| 144 | void *p; |
| 145 | |
| 146 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 147 | efi_memory_desc_t *md = p; |
| 148 | u64 start = md->phys_addr; |
| 149 | u64 size = md->num_pages << EFI_PAGE_SHIFT; |
| 150 | |
| 151 | if (md->type != EFI_BOOT_SERVICES_CODE && |
| 152 | md->type != EFI_BOOT_SERVICES_DATA) |
| 153 | continue; |
| 154 | /* Only reserve where possible: |
| 155 | * - Not within any already allocated areas |
| 156 | * - Not over any memory area (really needed, if above?) |
| 157 | * - Not within any part of the kernel |
| 158 | * - Not the bios reserved area |
| 159 | */ |
| 160 | if ((start + size > __pa_symbol(_text) |
| 161 | && start <= __pa_symbol(_end)) || |
| 162 | !e820_all_mapped(start, start+size, E820_RAM) || |
| 163 | memblock_is_region_reserved(start, size)) { |
| 164 | /* Could not reserve, skip it */ |
| 165 | md->num_pages = 0; |
| 166 | memblock_dbg("Could not reserve boot range [0x%010llx-0x%010llx]\n", |
| 167 | start, start+size-1); |
| 168 | } else |
| 169 | memblock_reserve(start, size); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | void __init efi_free_boot_services(void) |
| 174 | { |
| 175 | void *p; |
| 176 | |
| 177 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 178 | efi_memory_desc_t *md = p; |
| 179 | unsigned long long start = md->phys_addr; |
| 180 | unsigned long long size = md->num_pages << EFI_PAGE_SHIFT; |
| 181 | |
| 182 | if (md->type != EFI_BOOT_SERVICES_CODE && |
| 183 | md->type != EFI_BOOT_SERVICES_DATA) |
| 184 | continue; |
| 185 | |
| 186 | /* Could not reserve boot area */ |
| 187 | if (!size) |
| 188 | continue; |
| 189 | |
| 190 | free_bootmem_late(start, size); |
| 191 | } |
| 192 | |
| 193 | efi_unmap_memmap(); |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * A number of config table entries get remapped to virtual addresses |
| 198 | * after entering EFI virtual mode. However, the kexec kernel requires |
| 199 | * their physical addresses therefore we pass them via setup_data and |
| 200 | * correct those entries to their respective physical addresses here. |
| 201 | * |
| 202 | * Currently only handles smbios which is necessary for some firmware |
| 203 | * implementation. |
| 204 | */ |
| 205 | int __init efi_reuse_config(u64 tables, int nr_tables) |
| 206 | { |
| 207 | int i, sz, ret = 0; |
| 208 | void *p, *tablep; |
| 209 | struct efi_setup_data *data; |
| 210 | |
| 211 | if (!efi_setup) |
| 212 | return 0; |
| 213 | |
| 214 | if (!efi_enabled(EFI_64BIT)) |
| 215 | return 0; |
| 216 | |
| 217 | data = early_memremap(efi_setup, sizeof(*data)); |
| 218 | if (!data) { |
| 219 | ret = -ENOMEM; |
| 220 | goto out; |
| 221 | } |
| 222 | |
| 223 | if (!data->smbios) |
| 224 | goto out_memremap; |
| 225 | |
| 226 | sz = sizeof(efi_config_table_64_t); |
| 227 | |
| 228 | p = tablep = early_memremap(tables, nr_tables * sz); |
| 229 | if (!p) { |
| 230 | pr_err("Could not map Configuration table!\n"); |
| 231 | ret = -ENOMEM; |
| 232 | goto out_memremap; |
| 233 | } |
| 234 | |
| 235 | for (i = 0; i < efi.systab->nr_tables; i++) { |
| 236 | efi_guid_t guid; |
| 237 | |
| 238 | guid = ((efi_config_table_64_t *)p)->guid; |
| 239 | |
| 240 | if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID)) |
| 241 | ((efi_config_table_64_t *)p)->table = data->smbios; |
| 242 | p += sz; |
| 243 | } |
Matt Fleming | 98a716b | 2014-06-09 13:41:26 +0100 | [diff] [blame] | 244 | early_memunmap(tablep, nr_tables * sz); |
Saurabh Tangri | eeb9db0 | 2014-06-02 05:18:35 -0700 | [diff] [blame] | 245 | |
| 246 | out_memremap: |
Matt Fleming | 98a716b | 2014-06-09 13:41:26 +0100 | [diff] [blame] | 247 | early_memunmap(data, sizeof(*data)); |
Saurabh Tangri | eeb9db0 | 2014-06-02 05:18:35 -0700 | [diff] [blame] | 248 | out: |
| 249 | return ret; |
| 250 | } |
| 251 | |
Alex Thorlton | d394f2d | 2015-12-11 14:59:45 -0600 | [diff] [blame^] | 252 | static const struct dmi_system_id sgi_uv1_dmi[] = { |
| 253 | { NULL, "SGI UV1", |
| 254 | { DMI_MATCH(DMI_PRODUCT_NAME, "Stoutland Platform"), |
| 255 | DMI_MATCH(DMI_PRODUCT_VERSION, "1.0"), |
| 256 | DMI_MATCH(DMI_BIOS_VENDOR, "SGI.COM"), |
| 257 | } |
| 258 | }, |
| 259 | { } /* NULL entry stops DMI scanning */ |
| 260 | }; |
| 261 | |
Saurabh Tangri | eeb9db0 | 2014-06-02 05:18:35 -0700 | [diff] [blame] | 262 | void __init efi_apply_memmap_quirks(void) |
| 263 | { |
| 264 | /* |
| 265 | * Once setup is done earlier, unmap the EFI memory map on mismatched |
| 266 | * firmware/kernel architectures since there is no support for runtime |
| 267 | * services. |
| 268 | */ |
| 269 | if (!efi_runtime_supported()) { |
| 270 | pr_info("efi: Setup done, disabling due to 32/64-bit mismatch\n"); |
| 271 | efi_unmap_memmap(); |
| 272 | } |
| 273 | |
Alex Thorlton | d394f2d | 2015-12-11 14:59:45 -0600 | [diff] [blame^] | 274 | /* UV2+ BIOS has a fix for this issue. UV1 still needs the quirk. */ |
| 275 | if (dmi_check_system(sgi_uv1_dmi)) |
Saurabh Tangri | eeb9db0 | 2014-06-02 05:18:35 -0700 | [diff] [blame] | 276 | set_bit(EFI_OLD_MEMMAP, &efi.flags); |
| 277 | } |
Matt Fleming | 44be28e | 2014-06-13 12:39:55 +0100 | [diff] [blame] | 278 | |
| 279 | /* |
| 280 | * For most modern platforms the preferred method of powering off is via |
| 281 | * ACPI. However, there are some that are known to require the use of |
| 282 | * EFI runtime services and for which ACPI does not work at all. |
| 283 | * |
| 284 | * Using EFI is a last resort, to be used only if no other option |
| 285 | * exists. |
| 286 | */ |
| 287 | bool efi_reboot_required(void) |
| 288 | { |
| 289 | if (!acpi_gbl_reduced_hardware) |
| 290 | return false; |
| 291 | |
| 292 | efi_reboot_quirk_mode = EFI_RESET_WARM; |
| 293 | return true; |
| 294 | } |
| 295 | |
| 296 | bool efi_poweroff_required(void) |
| 297 | { |
| 298 | return !!acpi_gbl_reduced_hardware; |
| 299 | } |