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