blob: 2326bf51978f04e0bc3da8a30375a57536ea5bca [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/*
Ard Biesheuvelca0e30d2016-02-01 22:06:58 +000060 * In the nonblocking case we do not attempt to perform garbage
61 * collection if we do not have enough free space. Rather, we do the
62 * bare minimum check and give up immediately if the available space
63 * is below EFI_MIN_RESERVE.
64 *
65 * This function is intended to be small and simple because it is
66 * invoked from crash handler paths.
67 */
68static efi_status_t
69query_variable_store_nonblocking(u32 attributes, unsigned long size)
70{
71 efi_status_t status;
72 u64 storage_size, remaining_size, max_size;
73
74 status = efi.query_variable_info_nonblocking(attributes, &storage_size,
75 &remaining_size,
76 &max_size);
77 if (status != EFI_SUCCESS)
78 return status;
79
80 if (remaining_size - size < EFI_MIN_RESERVE)
81 return EFI_OUT_OF_RESOURCES;
82
83 return EFI_SUCCESS;
84}
85
86/*
Saurabh Tangrieeb9db02014-06-02 05:18:35 -070087 * Some firmware implementations refuse to boot if there's insufficient space
88 * in the variable store. Ensure that we never use more than a safe limit.
89 *
90 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
91 * store.
92 */
Ard Biesheuvelca0e30d2016-02-01 22:06:58 +000093efi_status_t efi_query_variable_store(u32 attributes, unsigned long size,
94 bool nonblocking)
Saurabh Tangrieeb9db02014-06-02 05:18:35 -070095{
96 efi_status_t status;
97 u64 storage_size, remaining_size, max_size;
98
99 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
100 return 0;
101
Ard Biesheuvelca0e30d2016-02-01 22:06:58 +0000102 if (nonblocking)
103 return query_variable_store_nonblocking(attributes, size);
104
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700105 status = efi.query_variable_info(attributes, &storage_size,
106 &remaining_size, &max_size);
107 if (status != EFI_SUCCESS)
108 return status;
109
110 /*
111 * We account for that by refusing the write if permitting it would
112 * reduce the available space to under 5KB. This figure was provided by
113 * Samsung, so should be safe.
114 */
115 if ((remaining_size - size < EFI_MIN_RESERVE) &&
116 !efi_no_storage_paranoia) {
117
118 /*
119 * Triggering garbage collection may require that the firmware
120 * generate a real EFI_OUT_OF_RESOURCES error. We can force
121 * that by attempting to use more space than is available.
122 */
123 unsigned long dummy_size = remaining_size + 1024;
124 void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
125
126 if (!dummy)
127 return EFI_OUT_OF_RESOURCES;
128
129 status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
130 EFI_VARIABLE_NON_VOLATILE |
131 EFI_VARIABLE_BOOTSERVICE_ACCESS |
132 EFI_VARIABLE_RUNTIME_ACCESS,
133 dummy_size, dummy);
134
135 if (status == EFI_SUCCESS) {
136 /*
137 * This should have failed, so if it didn't make sure
138 * that we delete it...
139 */
140 efi_delete_dummy_variable();
141 }
142
143 kfree(dummy);
144
145 /*
146 * The runtime code may now have triggered a garbage collection
147 * run, so check the variable info again
148 */
149 status = efi.query_variable_info(attributes, &storage_size,
150 &remaining_size, &max_size);
151
152 if (status != EFI_SUCCESS)
153 return status;
154
155 /*
156 * There still isn't enough room, so return an error
157 */
158 if (remaining_size - size < EFI_MIN_RESERVE)
159 return EFI_OUT_OF_RESOURCES;
160 }
161
162 return EFI_SUCCESS;
163}
164EXPORT_SYMBOL_GPL(efi_query_variable_store);
165
166/*
167 * The UEFI specification makes it clear that the operating system is free to do
168 * whatever it wants with boot services code after ExitBootServices() has been
169 * called. Ignoring this recommendation a significant bunch of EFI implementations
170 * continue calling into boot services code (SetVirtualAddressMap). In order to
171 * work around such buggy implementations we reserve boot services region during
172 * EFI init and make sure it stays executable. Then, after SetVirtualAddressMap(), it
173* is discarded.
174*/
175void __init efi_reserve_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 u64 start = md->phys_addr;
182 u64 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 /* Only reserve where possible:
188 * - Not within any already allocated areas
189 * - Not over any memory area (really needed, if above?)
190 * - Not within any part of the kernel
191 * - Not the bios reserved area
192 */
193 if ((start + size > __pa_symbol(_text)
194 && start <= __pa_symbol(_end)) ||
195 !e820_all_mapped(start, start+size, E820_RAM) ||
196 memblock_is_region_reserved(start, size)) {
197 /* Could not reserve, skip it */
198 md->num_pages = 0;
199 memblock_dbg("Could not reserve boot range [0x%010llx-0x%010llx]\n",
200 start, start+size-1);
201 } else
202 memblock_reserve(start, size);
203 }
204}
205
206void __init efi_free_boot_services(void)
207{
208 void *p;
209
210 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
211 efi_memory_desc_t *md = p;
212 unsigned long long start = md->phys_addr;
213 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
214
215 if (md->type != EFI_BOOT_SERVICES_CODE &&
216 md->type != EFI_BOOT_SERVICES_DATA)
217 continue;
218
219 /* Could not reserve boot area */
220 if (!size)
221 continue;
222
223 free_bootmem_late(start, size);
224 }
225
226 efi_unmap_memmap();
227}
228
229/*
230 * A number of config table entries get remapped to virtual addresses
231 * after entering EFI virtual mode. However, the kexec kernel requires
232 * their physical addresses therefore we pass them via setup_data and
233 * correct those entries to their respective physical addresses here.
234 *
235 * Currently only handles smbios which is necessary for some firmware
236 * implementation.
237 */
238int __init efi_reuse_config(u64 tables, int nr_tables)
239{
240 int i, sz, ret = 0;
241 void *p, *tablep;
242 struct efi_setup_data *data;
243
244 if (!efi_setup)
245 return 0;
246
247 if (!efi_enabled(EFI_64BIT))
248 return 0;
249
250 data = early_memremap(efi_setup, sizeof(*data));
251 if (!data) {
252 ret = -ENOMEM;
253 goto out;
254 }
255
256 if (!data->smbios)
257 goto out_memremap;
258
259 sz = sizeof(efi_config_table_64_t);
260
261 p = tablep = early_memremap(tables, nr_tables * sz);
262 if (!p) {
263 pr_err("Could not map Configuration table!\n");
264 ret = -ENOMEM;
265 goto out_memremap;
266 }
267
268 for (i = 0; i < efi.systab->nr_tables; i++) {
269 efi_guid_t guid;
270
271 guid = ((efi_config_table_64_t *)p)->guid;
272
273 if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
274 ((efi_config_table_64_t *)p)->table = data->smbios;
275 p += sz;
276 }
Matt Fleming98a716b2014-06-09 13:41:26 +0100277 early_memunmap(tablep, nr_tables * sz);
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700278
279out_memremap:
Matt Fleming98a716b2014-06-09 13:41:26 +0100280 early_memunmap(data, sizeof(*data));
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700281out:
282 return ret;
283}
284
Alex Thorltond394f2d2015-12-11 14:59:45 -0600285static const struct dmi_system_id sgi_uv1_dmi[] = {
286 { NULL, "SGI UV1",
287 { DMI_MATCH(DMI_PRODUCT_NAME, "Stoutland Platform"),
288 DMI_MATCH(DMI_PRODUCT_VERSION, "1.0"),
289 DMI_MATCH(DMI_BIOS_VENDOR, "SGI.COM"),
290 }
291 },
292 { } /* NULL entry stops DMI scanning */
293};
294
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700295void __init efi_apply_memmap_quirks(void)
296{
297 /*
298 * Once setup is done earlier, unmap the EFI memory map on mismatched
299 * firmware/kernel architectures since there is no support for runtime
300 * services.
301 */
302 if (!efi_runtime_supported()) {
Matt Fleming26d7f652015-10-25 10:26:35 +0000303 pr_info("Setup done, disabling due to 32/64-bit mismatch\n");
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700304 efi_unmap_memmap();
305 }
306
Alex Thorltond394f2d2015-12-11 14:59:45 -0600307 /* UV2+ BIOS has a fix for this issue. UV1 still needs the quirk. */
308 if (dmi_check_system(sgi_uv1_dmi))
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700309 set_bit(EFI_OLD_MEMMAP, &efi.flags);
310}
Matt Fleming44be28e2014-06-13 12:39:55 +0100311
312/*
313 * For most modern platforms the preferred method of powering off is via
314 * ACPI. However, there are some that are known to require the use of
315 * EFI runtime services and for which ACPI does not work at all.
316 *
317 * Using EFI is a last resort, to be used only if no other option
318 * exists.
319 */
320bool efi_reboot_required(void)
321{
322 if (!acpi_gbl_reduced_hardware)
323 return false;
324
325 efi_reboot_quirk_mode = EFI_RESET_WARM;
326 return true;
327}
328
329bool efi_poweroff_required(void)
330{
331 return !!acpi_gbl_reduced_hardware;
332}