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