thermal: fix Mediatek thermal controller build
[linux/fpc-iii.git] / arch / x86 / platform / efi / quirks.c
blobab50ada1d56e4f87f6fff3207995ea32312fb37b
1 #define pr_fmt(fmt) "efi: " fmt
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>
17 #define EFI_MIN_RESERVE 5120
19 #define EFI_DUMMY_GUID \
20 EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
22 static efi_char16_t efi_dummy_name[6] = { 'D', 'U', 'M', 'M', 'Y', 0 };
24 static bool efi_no_storage_paranoia;
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.
33 * Enabling this option disables the space checks in
34 * efi_query_variable_store() and forces garbage collection.
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.
40 static int __init setup_storage_paranoia(char *arg)
42 efi_no_storage_paranoia = true;
43 return 0;
45 early_param("efi_no_storage_paranoia", setup_storage_paranoia);
48 * Deleting the dummy variable which kicks off garbage collection
50 void efi_delete_dummy_variable(void)
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);
60 * 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.
65 * This function is intended to be small and simple because it is
66 * invoked from crash handler paths.
68 static efi_status_t
69 query_variable_store_nonblocking(u32 attributes, unsigned long size)
71 efi_status_t status;
72 u64 storage_size, remaining_size, max_size;
74 status = efi.query_variable_info_nonblocking(attributes, &storage_size,
75 &remaining_size,
76 &max_size);
77 if (status != EFI_SUCCESS)
78 return status;
80 if (remaining_size - size < EFI_MIN_RESERVE)
81 return EFI_OUT_OF_RESOURCES;
83 return EFI_SUCCESS;
87 * 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.
90 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
91 * store.
93 efi_status_t efi_query_variable_store(u32 attributes, unsigned long size,
94 bool nonblocking)
96 efi_status_t status;
97 u64 storage_size, remaining_size, max_size;
99 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
100 return 0;
102 if (nonblocking)
103 return query_variable_store_nonblocking(attributes, size);
105 status = efi.query_variable_info(attributes, &storage_size,
106 &remaining_size, &max_size);
107 if (status != EFI_SUCCESS)
108 return status;
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.
115 if ((remaining_size - size < EFI_MIN_RESERVE) &&
116 !efi_no_storage_paranoia) {
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.
123 unsigned long dummy_size = remaining_size + 1024;
124 void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
126 if (!dummy)
127 return EFI_OUT_OF_RESOURCES;
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);
135 if (status == EFI_SUCCESS) {
137 * This should have failed, so if it didn't make sure
138 * that we delete it...
140 efi_delete_dummy_variable();
143 kfree(dummy);
146 * The runtime code may now have triggered a garbage collection
147 * run, so check the variable info again
149 status = efi.query_variable_info(attributes, &storage_size,
150 &remaining_size, &max_size);
152 if (status != EFI_SUCCESS)
153 return status;
156 * There still isn't enough room, so return an error
158 if (remaining_size - size < EFI_MIN_RESERVE)
159 return EFI_OUT_OF_RESOURCES;
162 return EFI_SUCCESS;
164 EXPORT_SYMBOL_GPL(efi_query_variable_store);
167 * Helper function for efi_reserve_boot_services() to figure out if we
168 * can free regions in efi_free_boot_services().
170 * Use this function to ensure we do not free regions owned by somebody
171 * else. We must only reserve (and then free) regions:
173 * - Not within any part of the kernel
174 * - Not the BIOS reserved area (E820_RESERVED, E820_NVS, etc)
176 static bool can_free_region(u64 start, u64 size)
178 if (start + size > __pa_symbol(_text) && start <= __pa_symbol(_end))
179 return false;
181 if (!e820_all_mapped(start, start+size, E820_RAM))
182 return false;
184 return true;
188 * The UEFI specification makes it clear that the operating system is free to do
189 * whatever it wants with boot services code after ExitBootServices() has been
190 * called. Ignoring this recommendation a significant bunch of EFI implementations
191 * continue calling into boot services code (SetVirtualAddressMap). In order to
192 * work around such buggy implementations we reserve boot services region during
193 * EFI init and make sure it stays executable. Then, after SetVirtualAddressMap(), it
194 * is discarded.
196 void __init efi_reserve_boot_services(void)
198 void *p;
200 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
201 efi_memory_desc_t *md = p;
202 u64 start = md->phys_addr;
203 u64 size = md->num_pages << EFI_PAGE_SHIFT;
204 bool already_reserved;
206 if (md->type != EFI_BOOT_SERVICES_CODE &&
207 md->type != EFI_BOOT_SERVICES_DATA)
208 continue;
210 already_reserved = memblock_is_region_reserved(start, size);
213 * Because the following memblock_reserve() is paired
214 * with free_bootmem_late() for this region in
215 * efi_free_boot_services(), we must be extremely
216 * careful not to reserve, and subsequently free,
217 * critical regions of memory (like the kernel image) or
218 * those regions that somebody else has already
219 * reserved.
221 * A good example of a critical region that must not be
222 * freed is page zero (first 4Kb of memory), which may
223 * contain boot services code/data but is marked
224 * E820_RESERVED by trim_bios_range().
226 if (!already_reserved) {
227 memblock_reserve(start, size);
230 * If we are the first to reserve the region, no
231 * one else cares about it. We own it and can
232 * free it later.
234 if (can_free_region(start, size))
235 continue;
239 * We don't own the region. We must not free it.
241 * Setting this bit for a boot services region really
242 * doesn't make sense as far as the firmware is
243 * concerned, but it does provide us with a way to tag
244 * those regions that must not be paired with
245 * free_bootmem_late().
247 md->attribute |= EFI_MEMORY_RUNTIME;
251 void __init efi_free_boot_services(void)
253 void *p;
255 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
256 efi_memory_desc_t *md = p;
257 unsigned long long start = md->phys_addr;
258 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
260 if (md->type != EFI_BOOT_SERVICES_CODE &&
261 md->type != EFI_BOOT_SERVICES_DATA)
262 continue;
264 /* Do not free, someone else owns it: */
265 if (md->attribute & EFI_MEMORY_RUNTIME)
266 continue;
268 free_bootmem_late(start, size);
271 efi_unmap_memmap();
275 * A number of config table entries get remapped to virtual addresses
276 * after entering EFI virtual mode. However, the kexec kernel requires
277 * their physical addresses therefore we pass them via setup_data and
278 * correct those entries to their respective physical addresses here.
280 * Currently only handles smbios which is necessary for some firmware
281 * implementation.
283 int __init efi_reuse_config(u64 tables, int nr_tables)
285 int i, sz, ret = 0;
286 void *p, *tablep;
287 struct efi_setup_data *data;
289 if (!efi_setup)
290 return 0;
292 if (!efi_enabled(EFI_64BIT))
293 return 0;
295 data = early_memremap(efi_setup, sizeof(*data));
296 if (!data) {
297 ret = -ENOMEM;
298 goto out;
301 if (!data->smbios)
302 goto out_memremap;
304 sz = sizeof(efi_config_table_64_t);
306 p = tablep = early_memremap(tables, nr_tables * sz);
307 if (!p) {
308 pr_err("Could not map Configuration table!\n");
309 ret = -ENOMEM;
310 goto out_memremap;
313 for (i = 0; i < efi.systab->nr_tables; i++) {
314 efi_guid_t guid;
316 guid = ((efi_config_table_64_t *)p)->guid;
318 if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
319 ((efi_config_table_64_t *)p)->table = data->smbios;
320 p += sz;
322 early_memunmap(tablep, nr_tables * sz);
324 out_memremap:
325 early_memunmap(data, sizeof(*data));
326 out:
327 return ret;
330 static const struct dmi_system_id sgi_uv1_dmi[] = {
331 { NULL, "SGI UV1",
332 { DMI_MATCH(DMI_PRODUCT_NAME, "Stoutland Platform"),
333 DMI_MATCH(DMI_PRODUCT_VERSION, "1.0"),
334 DMI_MATCH(DMI_BIOS_VENDOR, "SGI.COM"),
337 { } /* NULL entry stops DMI scanning */
340 void __init efi_apply_memmap_quirks(void)
343 * Once setup is done earlier, unmap the EFI memory map on mismatched
344 * firmware/kernel architectures since there is no support for runtime
345 * services.
347 if (!efi_runtime_supported()) {
348 pr_info("Setup done, disabling due to 32/64-bit mismatch\n");
349 efi_unmap_memmap();
352 /* UV2+ BIOS has a fix for this issue. UV1 still needs the quirk. */
353 if (dmi_check_system(sgi_uv1_dmi))
354 set_bit(EFI_OLD_MEMMAP, &efi.flags);
358 * For most modern platforms the preferred method of powering off is via
359 * ACPI. However, there are some that are known to require the use of
360 * EFI runtime services and for which ACPI does not work at all.
362 * Using EFI is a last resort, to be used only if no other option
363 * exists.
365 bool efi_reboot_required(void)
367 if (!acpi_gbl_reduced_hardware)
368 return false;
370 efi_reboot_quirk_mode = EFI_RESET_WARM;
371 return true;
374 bool efi_poweroff_required(void)
376 return !!acpi_gbl_reduced_hardware;