blob: c6a14b11f496c1b89beb4808d7eeeff4130d9897 [file] [log] [blame]
Matt Fleming26d7f652015-10-25 10:26:35 +00001#define pr_fmt(fmt) "efi: " fmt
2
Saurabh Tangrieeb9db02014-06-02 05:18:35 -07003#include <linux/init.h>
4#include <linux/kernel.h>
5#include <linux/string.h>
6#include <linux/time.h>
7#include <linux/types.h>
8#include <linux/efi.h>
9#include <linux/slab.h>
10#include <linux/memblock.h>
11#include <linux/bootmem.h>
Matt Fleming44be28e2014-06-13 12:39:55 +010012#include <linux/acpi.h>
Alex Thorltond394f2d2015-12-11 14:59:45 -060013#include <linux/dmi.h>
Ingo Molnar5520b7e2017-01-27 11:59:46 +010014
15#include <asm/e820/api.h>
Saurabh Tangrieeb9db02014-06-02 05:18:35 -070016#include <asm/efi.h>
17#include <asm/uv/uv.h>
18
19#define EFI_MIN_RESERVE 5120
20
21#define EFI_DUMMY_GUID \
22 EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
23
24static efi_char16_t efi_dummy_name[6] = { 'D', 'U', 'M', 'M', 'Y', 0 };
25
26static bool efi_no_storage_paranoia;
27
28/*
29 * Some firmware implementations refuse to boot if there's insufficient
30 * space in the variable store. The implementation of garbage collection
31 * in some FW versions causes stale (deleted) variables to take up space
32 * longer than intended and space is only freed once the store becomes
33 * almost completely full.
34 *
35 * Enabling this option disables the space checks in
36 * efi_query_variable_store() and forces garbage collection.
37 *
38 * Only enable this option if deleting EFI variables does not free up
39 * space in your variable store, e.g. if despite deleting variables
40 * you're unable to create new ones.
41 */
42static int __init setup_storage_paranoia(char *arg)
43{
44 efi_no_storage_paranoia = true;
45 return 0;
46}
47early_param("efi_no_storage_paranoia", setup_storage_paranoia);
48
49/*
50 * Deleting the dummy variable which kicks off garbage collection
51*/
52void efi_delete_dummy_variable(void)
53{
54 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
55 EFI_VARIABLE_NON_VOLATILE |
56 EFI_VARIABLE_BOOTSERVICE_ACCESS |
57 EFI_VARIABLE_RUNTIME_ACCESS,
58 0, NULL);
59}
60
61/*
Ard Biesheuvelca0e30d2016-02-01 22:06:58 +000062 * In the nonblocking case we do not attempt to perform garbage
63 * collection if we do not have enough free space. Rather, we do the
64 * bare minimum check and give up immediately if the available space
65 * is below EFI_MIN_RESERVE.
66 *
67 * This function is intended to be small and simple because it is
68 * invoked from crash handler paths.
69 */
70static efi_status_t
71query_variable_store_nonblocking(u32 attributes, unsigned long size)
72{
73 efi_status_t status;
74 u64 storage_size, remaining_size, max_size;
75
76 status = efi.query_variable_info_nonblocking(attributes, &storage_size,
77 &remaining_size,
78 &max_size);
79 if (status != EFI_SUCCESS)
80 return status;
81
82 if (remaining_size - size < EFI_MIN_RESERVE)
83 return EFI_OUT_OF_RESOURCES;
84
85 return EFI_SUCCESS;
86}
87
88/*
Saurabh Tangrieeb9db02014-06-02 05:18:35 -070089 * Some firmware implementations refuse to boot if there's insufficient space
90 * in the variable store. Ensure that we never use more than a safe limit.
91 *
92 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
93 * store.
94 */
Ard Biesheuvelca0e30d2016-02-01 22:06:58 +000095efi_status_t efi_query_variable_store(u32 attributes, unsigned long size,
96 bool nonblocking)
Saurabh Tangrieeb9db02014-06-02 05:18:35 -070097{
98 efi_status_t status;
99 u64 storage_size, remaining_size, max_size;
100
101 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
102 return 0;
103
Ard Biesheuvelca0e30d2016-02-01 22:06:58 +0000104 if (nonblocking)
105 return query_variable_store_nonblocking(attributes, size);
106
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700107 status = efi.query_variable_info(attributes, &storage_size,
108 &remaining_size, &max_size);
109 if (status != EFI_SUCCESS)
110 return status;
111
112 /*
113 * We account for that by refusing the write if permitting it would
114 * reduce the available space to under 5KB. This figure was provided by
115 * Samsung, so should be safe.
116 */
117 if ((remaining_size - size < EFI_MIN_RESERVE) &&
118 !efi_no_storage_paranoia) {
119
120 /*
121 * Triggering garbage collection may require that the firmware
122 * generate a real EFI_OUT_OF_RESOURCES error. We can force
123 * that by attempting to use more space than is available.
124 */
125 unsigned long dummy_size = remaining_size + 1024;
126 void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
127
128 if (!dummy)
129 return EFI_OUT_OF_RESOURCES;
130
131 status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
132 EFI_VARIABLE_NON_VOLATILE |
133 EFI_VARIABLE_BOOTSERVICE_ACCESS |
134 EFI_VARIABLE_RUNTIME_ACCESS,
135 dummy_size, dummy);
136
137 if (status == EFI_SUCCESS) {
138 /*
139 * This should have failed, so if it didn't make sure
140 * that we delete it...
141 */
142 efi_delete_dummy_variable();
143 }
144
145 kfree(dummy);
146
147 /*
148 * The runtime code may now have triggered a garbage collection
149 * run, so check the variable info again
150 */
151 status = efi.query_variable_info(attributes, &storage_size,
152 &remaining_size, &max_size);
153
154 if (status != EFI_SUCCESS)
155 return status;
156
157 /*
158 * There still isn't enough room, so return an error
159 */
160 if (remaining_size - size < EFI_MIN_RESERVE)
161 return EFI_OUT_OF_RESOURCES;
162 }
163
164 return EFI_SUCCESS;
165}
166EXPORT_SYMBOL_GPL(efi_query_variable_store);
167
168/*
Matt Fleming816e7612016-02-29 21:22:52 +0000169 * The UEFI specification makes it clear that the operating system is
170 * free to do whatever it wants with boot services code after
171 * ExitBootServices() has been called. Ignoring this recommendation a
172 * significant bunch of EFI implementations continue calling into boot
173 * services code (SetVirtualAddressMap). In order to work around such
174 * buggy implementations we reserve boot services region during EFI
175 * init and make sure it stays executable. Then, after
176 * SetVirtualAddressMap(), it is discarded.
177 *
178 * However, some boot services regions contain data that is required
179 * by drivers, so we need to track which memory ranges can never be
180 * freed. This is done by tagging those regions with the
181 * EFI_MEMORY_RUNTIME attribute.
182 *
183 * Any driver that wants to mark a region as reserved must use
184 * efi_mem_reserve() which will insert a new EFI memory descriptor
185 * into efi.memmap (splitting existing regions if necessary) and tag
186 * it with EFI_MEMORY_RUNTIME.
187 */
188void __init efi_arch_mem_reserve(phys_addr_t addr, u64 size)
189{
190 phys_addr_t new_phys, new_size;
191 struct efi_mem_range mr;
192 efi_memory_desc_t md;
193 int num_entries;
194 void *new;
195
196 if (efi_mem_desc_lookup(addr, &md)) {
197 pr_err("Failed to lookup EFI memory descriptor for %pa\n", &addr);
198 return;
199 }
200
201 if (addr + size > md.phys_addr + (md.num_pages << EFI_PAGE_SHIFT)) {
202 pr_err("Region spans EFI memory descriptors, %pa\n", &addr);
203 return;
204 }
205
Matt Fleming92dc3352016-09-16 15:12:47 +0100206 size += addr % EFI_PAGE_SIZE;
207 size = round_up(size, EFI_PAGE_SIZE);
208 addr = round_down(addr, EFI_PAGE_SIZE);
209
Matt Fleming816e7612016-02-29 21:22:52 +0000210 mr.range.start = addr;
Matt Fleming92dc3352016-09-16 15:12:47 +0100211 mr.range.end = addr + size - 1;
Matt Fleming816e7612016-02-29 21:22:52 +0000212 mr.attribute = md.attribute | EFI_MEMORY_RUNTIME;
213
214 num_entries = efi_memmap_split_count(&md, &mr.range);
215 num_entries += efi.memmap.nr_map;
216
217 new_size = efi.memmap.desc_size * num_entries;
218
Nicolai Stange20b1e222017-01-05 13:51:29 +0100219 new_phys = efi_memmap_alloc(num_entries);
Matt Fleming816e7612016-02-29 21:22:52 +0000220 if (!new_phys) {
221 pr_err("Could not allocate boot services memmap\n");
222 return;
223 }
224
225 new = early_memremap(new_phys, new_size);
226 if (!new) {
227 pr_err("Failed to map new boot services memmap\n");
228 return;
229 }
230
231 efi_memmap_insert(&efi.memmap, new, &mr);
232 early_memunmap(new, new_size);
233
234 efi_memmap_install(new_phys, num_entries);
235}
236
237/*
Matt Fleming452308d2016-03-11 11:19:23 +0000238 * Helper function for efi_reserve_boot_services() to figure out if we
239 * can free regions in efi_free_boot_services().
240 *
241 * Use this function to ensure we do not free regions owned by somebody
242 * else. We must only reserve (and then free) regions:
243 *
244 * - Not within any part of the kernel
245 * - Not the BIOS reserved area (E820_RESERVED, E820_NVS, etc)
246 */
247static bool can_free_region(u64 start, u64 size)
248{
249 if (start + size > __pa_symbol(_text) && start <= __pa_symbol(_end))
250 return false;
251
252 if (!e820_all_mapped(start, start+size, E820_RAM))
253 return false;
254
255 return true;
256}
257
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700258void __init efi_reserve_boot_services(void)
259{
Matt Fleming78ce2482016-04-25 21:06:38 +0100260 efi_memory_desc_t *md;
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700261
Matt Fleming78ce2482016-04-25 21:06:38 +0100262 for_each_efi_memory_desc(md) {
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700263 u64 start = md->phys_addr;
264 u64 size = md->num_pages << EFI_PAGE_SHIFT;
Matt Fleming452308d2016-03-11 11:19:23 +0000265 bool already_reserved;
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700266
267 if (md->type != EFI_BOOT_SERVICES_CODE &&
268 md->type != EFI_BOOT_SERVICES_DATA)
269 continue;
Matt Fleming452308d2016-03-11 11:19:23 +0000270
271 already_reserved = memblock_is_region_reserved(start, size);
272
273 /*
274 * Because the following memblock_reserve() is paired
275 * with free_bootmem_late() for this region in
276 * efi_free_boot_services(), we must be extremely
277 * careful not to reserve, and subsequently free,
278 * critical regions of memory (like the kernel image) or
279 * those regions that somebody else has already
280 * reserved.
281 *
282 * A good example of a critical region that must not be
283 * freed is page zero (first 4Kb of memory), which may
284 * contain boot services code/data but is marked
285 * E820_RESERVED by trim_bios_range().
286 */
287 if (!already_reserved) {
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700288 memblock_reserve(start, size);
Matt Fleming452308d2016-03-11 11:19:23 +0000289
290 /*
291 * If we are the first to reserve the region, no
292 * one else cares about it. We own it and can
293 * free it later.
294 */
295 if (can_free_region(start, size))
296 continue;
297 }
298
299 /*
300 * We don't own the region. We must not free it.
301 *
302 * Setting this bit for a boot services region really
303 * doesn't make sense as far as the firmware is
304 * concerned, but it does provide us with a way to tag
305 * those regions that must not be paired with
306 * free_bootmem_late().
307 */
308 md->attribute |= EFI_MEMORY_RUNTIME;
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700309 }
310}
311
312void __init efi_free_boot_services(void)
313{
Matt Fleming816e7612016-02-29 21:22:52 +0000314 phys_addr_t new_phys, new_size;
Matt Fleming78ce2482016-04-25 21:06:38 +0100315 efi_memory_desc_t *md;
Matt Fleming816e7612016-02-29 21:22:52 +0000316 int num_entries = 0;
317 void *new, *new_md;
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700318
Matt Fleming78ce2482016-04-25 21:06:38 +0100319 for_each_efi_memory_desc(md) {
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700320 unsigned long long start = md->phys_addr;
321 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
Andy Lutomirski5bc653b2016-08-10 02:29:17 -0700322 size_t rm_size;
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700323
324 if (md->type != EFI_BOOT_SERVICES_CODE &&
Matt Fleming816e7612016-02-29 21:22:52 +0000325 md->type != EFI_BOOT_SERVICES_DATA) {
326 num_entries++;
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700327 continue;
Matt Fleming816e7612016-02-29 21:22:52 +0000328 }
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700329
Matt Fleming452308d2016-03-11 11:19:23 +0000330 /* Do not free, someone else owns it: */
Matt Fleming816e7612016-02-29 21:22:52 +0000331 if (md->attribute & EFI_MEMORY_RUNTIME) {
332 num_entries++;
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700333 continue;
Matt Fleming816e7612016-02-29 21:22:52 +0000334 }
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700335
Andy Lutomirski5bc653b2016-08-10 02:29:17 -0700336 /*
337 * Nasty quirk: if all sub-1MB memory is used for boot
338 * services, we can get here without having allocated the
339 * real mode trampoline. It's too late to hand boot services
340 * memory back to the memblock allocator, so instead
341 * try to manually allocate the trampoline if needed.
342 *
343 * I've seen this on a Dell XPS 13 9350 with firmware
344 * 1.4.4 with SGX enabled booting Linux via Fedora 24's
345 * grub2-efi on a hard disk. (And no, I don't know why
346 * this happened, but Linux should still try to boot rather
347 * panicing early.)
348 */
349 rm_size = real_mode_size_needed();
350 if (rm_size && (start + rm_size) < (1<<20) && size >= rm_size) {
351 set_real_mode_mem(start, rm_size);
352 start += rm_size;
353 size -= rm_size;
354 }
355
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700356 free_bootmem_late(start, size);
357 }
Matt Fleming816e7612016-02-29 21:22:52 +0000358
359 new_size = efi.memmap.desc_size * num_entries;
Nicolai Stange20b1e222017-01-05 13:51:29 +0100360 new_phys = efi_memmap_alloc(num_entries);
Matt Fleming816e7612016-02-29 21:22:52 +0000361 if (!new_phys) {
362 pr_err("Failed to allocate new EFI memmap\n");
363 return;
364 }
365
366 new = memremap(new_phys, new_size, MEMREMAP_WB);
367 if (!new) {
368 pr_err("Failed to map new EFI memmap\n");
369 return;
370 }
371
372 /*
373 * Build a new EFI memmap that excludes any boot services
374 * regions that are not tagged EFI_MEMORY_RUNTIME, since those
375 * regions have now been freed.
376 */
377 new_md = new;
378 for_each_efi_memory_desc(md) {
379 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
380 (md->type == EFI_BOOT_SERVICES_CODE ||
381 md->type == EFI_BOOT_SERVICES_DATA))
382 continue;
383
384 memcpy(new_md, md, efi.memmap.desc_size);
385 new_md += efi.memmap.desc_size;
386 }
387
388 memunmap(new);
389
390 if (efi_memmap_install(new_phys, num_entries)) {
391 pr_err("Could not install new EFI memmap\n");
392 return;
393 }
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700394}
395
396/*
397 * A number of config table entries get remapped to virtual addresses
398 * after entering EFI virtual mode. However, the kexec kernel requires
399 * their physical addresses therefore we pass them via setup_data and
400 * correct those entries to their respective physical addresses here.
401 *
402 * Currently only handles smbios which is necessary for some firmware
403 * implementation.
404 */
405int __init efi_reuse_config(u64 tables, int nr_tables)
406{
407 int i, sz, ret = 0;
408 void *p, *tablep;
409 struct efi_setup_data *data;
410
411 if (!efi_setup)
412 return 0;
413
414 if (!efi_enabled(EFI_64BIT))
415 return 0;
416
417 data = early_memremap(efi_setup, sizeof(*data));
418 if (!data) {
419 ret = -ENOMEM;
420 goto out;
421 }
422
423 if (!data->smbios)
424 goto out_memremap;
425
426 sz = sizeof(efi_config_table_64_t);
427
428 p = tablep = early_memremap(tables, nr_tables * sz);
429 if (!p) {
430 pr_err("Could not map Configuration table!\n");
431 ret = -ENOMEM;
432 goto out_memremap;
433 }
434
435 for (i = 0; i < efi.systab->nr_tables; i++) {
436 efi_guid_t guid;
437
438 guid = ((efi_config_table_64_t *)p)->guid;
439
440 if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
441 ((efi_config_table_64_t *)p)->table = data->smbios;
442 p += sz;
443 }
Matt Fleming98a716b2014-06-09 13:41:26 +0100444 early_memunmap(tablep, nr_tables * sz);
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700445
446out_memremap:
Matt Fleming98a716b2014-06-09 13:41:26 +0100447 early_memunmap(data, sizeof(*data));
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700448out:
449 return ret;
450}
451
Alex Thorltond394f2d2015-12-11 14:59:45 -0600452static const struct dmi_system_id sgi_uv1_dmi[] = {
453 { NULL, "SGI UV1",
454 { DMI_MATCH(DMI_PRODUCT_NAME, "Stoutland Platform"),
455 DMI_MATCH(DMI_PRODUCT_VERSION, "1.0"),
456 DMI_MATCH(DMI_BIOS_VENDOR, "SGI.COM"),
457 }
458 },
459 { } /* NULL entry stops DMI scanning */
460};
461
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700462void __init efi_apply_memmap_quirks(void)
463{
464 /*
465 * Once setup is done earlier, unmap the EFI memory map on mismatched
466 * firmware/kernel architectures since there is no support for runtime
467 * services.
468 */
469 if (!efi_runtime_supported()) {
Matt Fleming26d7f652015-10-25 10:26:35 +0000470 pr_info("Setup done, disabling due to 32/64-bit mismatch\n");
Matt Fleming9479c7c2016-02-26 21:22:05 +0000471 efi_memmap_unmap();
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700472 }
473
Alex Thorltond394f2d2015-12-11 14:59:45 -0600474 /* UV2+ BIOS has a fix for this issue. UV1 still needs the quirk. */
475 if (dmi_check_system(sgi_uv1_dmi))
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700476 set_bit(EFI_OLD_MEMMAP, &efi.flags);
477}
Matt Fleming44be28e2014-06-13 12:39:55 +0100478
479/*
480 * For most modern platforms the preferred method of powering off is via
481 * ACPI. However, there are some that are known to require the use of
482 * EFI runtime services and for which ACPI does not work at all.
483 *
484 * Using EFI is a last resort, to be used only if no other option
485 * exists.
486 */
487bool efi_reboot_required(void)
488{
489 if (!acpi_gbl_reduced_hardware)
490 return false;
491
492 efi_reboot_quirk_mode = EFI_RESET_WARM;
493 return true;
494}
495
496bool efi_poweroff_required(void)
497{
Chen Yu13737182016-03-22 08:51:10 +0800498 return acpi_gbl_reduced_hardware || acpi_no_s5;
Matt Fleming44be28e2014-06-13 12:39:55 +0100499}