Linux-libre 3.16.85-gnu
[librecmc/linux-libre.git] / arch / x86 / platform / efi / efi.c
1 /*
2  * Common EFI (Extensible Firmware Interface) support functions
3  * Based on Extensible Firmware Interface Specification version 1.0
4  *
5  * Copyright (C) 1999 VA Linux Systems
6  * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
7  * Copyright (C) 1999-2002 Hewlett-Packard Co.
8  *      David Mosberger-Tang <davidm@hpl.hp.com>
9  *      Stephane Eranian <eranian@hpl.hp.com>
10  * Copyright (C) 2005-2008 Intel Co.
11  *      Fenghua Yu <fenghua.yu@intel.com>
12  *      Bibo Mao <bibo.mao@intel.com>
13  *      Chandramouli Narayanan <mouli@linux.intel.com>
14  *      Huang Ying <ying.huang@intel.com>
15  * Copyright (C) 2013 SuSE Labs
16  *      Borislav Petkov <bp@suse.de> - runtime services VA mapping
17  *
18  * Copied from efi_32.c to eliminate the duplicated code between EFI
19  * 32/64 support code. --ying 2007-10-26
20  *
21  * All EFI Runtime Services are not implemented yet as EFI only
22  * supports physical mode addressing on SoftSDV. This is to be fixed
23  * in a future version.  --drummond 1999-07-20
24  *
25  * Implemented EFI runtime services and virtual mode calls.  --davidm
26  *
27  * Goutham Rao: <goutham.rao@intel.com>
28  *      Skip non-WB memory and ignore empty memory ranges.
29  */
30
31 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32
33 #include <linux/kernel.h>
34 #include <linux/init.h>
35 #include <linux/efi.h>
36 #include <linux/efi-bgrt.h>
37 #include <linux/export.h>
38 #include <linux/bootmem.h>
39 #include <linux/slab.h>
40 #include <linux/memblock.h>
41 #include <linux/spinlock.h>
42 #include <linux/uaccess.h>
43 #include <linux/time.h>
44 #include <linux/io.h>
45 #include <linux/reboot.h>
46 #include <linux/bcd.h>
47 #include <linux/acpi.h>
48
49 #include <asm/setup.h>
50 #include <asm/efi.h>
51 #include <asm/time.h>
52 #include <asm/cacheflush.h>
53 #include <asm/tlbflush.h>
54 #include <asm/x86_init.h>
55 #include <asm/rtc.h>
56 #include <asm/uv/uv.h>
57
58 #define EFI_DEBUG
59
60 #define EFI_MIN_RESERVE 5120
61
62 #define EFI_DUMMY_GUID \
63         EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
64
65 static efi_char16_t efi_dummy_name[6] = { 'D', 'U', 'M', 'M', 'Y', 0 };
66
67 struct efi_memory_map memmap;
68
69 static struct efi efi_phys __initdata;
70 static efi_system_table_t efi_systab __initdata;
71
72 static efi_config_table_type_t arch_tables[] __initdata = {
73 #ifdef CONFIG_X86_UV
74         {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab},
75 #endif
76         {NULL_GUID, NULL, NULL},
77 };
78
79 u64 efi_setup;          /* efi setup_data physical address */
80
81 static bool disable_runtime __initdata = false;
82 static int __init setup_noefi(char *arg)
83 {
84         disable_runtime = true;
85         return 0;
86 }
87 early_param("noefi", setup_noefi);
88
89 int add_efi_memmap;
90 EXPORT_SYMBOL(add_efi_memmap);
91
92 static int __init setup_add_efi_memmap(char *arg)
93 {
94         add_efi_memmap = 1;
95         return 0;
96 }
97 early_param("add_efi_memmap", setup_add_efi_memmap);
98
99 static bool efi_no_storage_paranoia;
100
101 static int __init setup_storage_paranoia(char *arg)
102 {
103         efi_no_storage_paranoia = true;
104         return 0;
105 }
106 early_param("efi_no_storage_paranoia", setup_storage_paranoia);
107
108 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
109 {
110         unsigned long flags;
111         efi_status_t status;
112
113         spin_lock_irqsave(&rtc_lock, flags);
114         status = efi_call_virt(get_time, tm, tc);
115         spin_unlock_irqrestore(&rtc_lock, flags);
116         return status;
117 }
118
119 static efi_status_t virt_efi_set_time(efi_time_t *tm)
120 {
121         unsigned long flags;
122         efi_status_t status;
123
124         spin_lock_irqsave(&rtc_lock, flags);
125         status = efi_call_virt(set_time, tm);
126         spin_unlock_irqrestore(&rtc_lock, flags);
127         return status;
128 }
129
130 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
131                                              efi_bool_t *pending,
132                                              efi_time_t *tm)
133 {
134         unsigned long flags;
135         efi_status_t status;
136
137         spin_lock_irqsave(&rtc_lock, flags);
138         status = efi_call_virt(get_wakeup_time, enabled, pending, tm);
139         spin_unlock_irqrestore(&rtc_lock, flags);
140         return status;
141 }
142
143 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
144 {
145         unsigned long flags;
146         efi_status_t status;
147
148         spin_lock_irqsave(&rtc_lock, flags);
149         status = efi_call_virt(set_wakeup_time, enabled, tm);
150         spin_unlock_irqrestore(&rtc_lock, flags);
151         return status;
152 }
153
154 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
155                                           efi_guid_t *vendor,
156                                           u32 *attr,
157                                           unsigned long *data_size,
158                                           void *data)
159 {
160         return efi_call_virt(get_variable,
161                              name, vendor, attr,
162                              data_size, data);
163 }
164
165 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
166                                                efi_char16_t *name,
167                                                efi_guid_t *vendor)
168 {
169         return efi_call_virt(get_next_variable,
170                              name_size, name, vendor);
171 }
172
173 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
174                                           efi_guid_t *vendor,
175                                           u32 attr,
176                                           unsigned long data_size,
177                                           void *data)
178 {
179         return efi_call_virt(set_variable,
180                              name, vendor, attr,
181                              data_size, data);
182 }
183
184 static efi_status_t virt_efi_query_variable_info(u32 attr,
185                                                  u64 *storage_space,
186                                                  u64 *remaining_space,
187                                                  u64 *max_variable_size)
188 {
189         if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
190                 return EFI_UNSUPPORTED;
191
192         return efi_call_virt(query_variable_info, attr, storage_space,
193                              remaining_space, max_variable_size);
194 }
195
196 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
197 {
198         return efi_call_virt(get_next_high_mono_count, count);
199 }
200
201 static void virt_efi_reset_system(int reset_type,
202                                   efi_status_t status,
203                                   unsigned long data_size,
204                                   efi_char16_t *data)
205 {
206         __efi_call_virt(reset_system, reset_type, status,
207                         data_size, data);
208 }
209
210 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
211                                             unsigned long count,
212                                             unsigned long sg_list)
213 {
214         if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
215                 return EFI_UNSUPPORTED;
216
217         return efi_call_virt(update_capsule, capsules, count, sg_list);
218 }
219
220 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
221                                                 unsigned long count,
222                                                 u64 *max_size,
223                                                 int *reset_type)
224 {
225         if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
226                 return EFI_UNSUPPORTED;
227
228         return efi_call_virt(query_capsule_caps, capsules, count, max_size,
229                              reset_type);
230 }
231
232 static efi_status_t __init phys_efi_set_virtual_address_map(
233         unsigned long memory_map_size,
234         unsigned long descriptor_size,
235         u32 descriptor_version,
236         efi_memory_desc_t *virtual_map)
237 {
238         efi_status_t status;
239         unsigned long flags;
240
241         efi_call_phys_prelog();
242
243         /* Disable interrupts around EFI calls: */
244         local_irq_save(flags);
245         status = efi_call_phys(efi_phys.set_virtual_address_map,
246                                memory_map_size, descriptor_size,
247                                descriptor_version, virtual_map);
248         local_irq_restore(flags);
249
250         efi_call_phys_epilog();
251
252         return status;
253 }
254
255 int efi_set_rtc_mmss(const struct timespec *now)
256 {
257         unsigned long nowtime = now->tv_sec;
258         efi_status_t    status;
259         efi_time_t      eft;
260         efi_time_cap_t  cap;
261         struct rtc_time tm;
262
263         status = efi.get_time(&eft, &cap);
264         if (status != EFI_SUCCESS) {
265                 pr_err("Oops: efitime: can't read time!\n");
266                 return -1;
267         }
268
269         rtc_time_to_tm(nowtime, &tm);
270         if (!rtc_valid_tm(&tm)) {
271                 eft.year = tm.tm_year + 1900;
272                 eft.month = tm.tm_mon + 1;
273                 eft.day = tm.tm_mday;
274                 eft.minute = tm.tm_min;
275                 eft.second = tm.tm_sec;
276                 eft.nanosecond = 0;
277         } else {
278                 pr_err("%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
279                        __func__, nowtime);
280                 return -1;
281         }
282
283         status = efi.set_time(&eft);
284         if (status != EFI_SUCCESS) {
285                 pr_err("Oops: efitime: can't write time!\n");
286                 return -1;
287         }
288         return 0;
289 }
290
291 void efi_get_time(struct timespec *now)
292 {
293         efi_status_t status;
294         efi_time_t eft;
295         efi_time_cap_t cap;
296
297         status = efi.get_time(&eft, &cap);
298         if (status != EFI_SUCCESS)
299                 pr_err("Oops: efitime: can't read time!\n");
300
301         now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour,
302                              eft.minute, eft.second);
303         now->tv_nsec = 0;
304 }
305
306 /*
307  * Tell the kernel about the EFI memory map.  This might include
308  * more than the max 128 entries that can fit in the e820 legacy
309  * (zeropage) memory map.
310  */
311
312 static void __init do_add_efi_memmap(void)
313 {
314         void *p;
315
316         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
317                 efi_memory_desc_t *md = p;
318                 unsigned long long start = md->phys_addr;
319                 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
320                 int e820_type;
321
322                 switch (md->type) {
323                 case EFI_LOADER_CODE:
324                 case EFI_LOADER_DATA:
325                 case EFI_BOOT_SERVICES_CODE:
326                 case EFI_BOOT_SERVICES_DATA:
327                 case EFI_CONVENTIONAL_MEMORY:
328                         if (md->attribute & EFI_MEMORY_WB)
329                                 e820_type = E820_RAM;
330                         else
331                                 e820_type = E820_RESERVED;
332                         break;
333                 case EFI_ACPI_RECLAIM_MEMORY:
334                         e820_type = E820_ACPI;
335                         break;
336                 case EFI_ACPI_MEMORY_NVS:
337                         e820_type = E820_NVS;
338                         break;
339                 case EFI_UNUSABLE_MEMORY:
340                         e820_type = E820_UNUSABLE;
341                         break;
342                 default:
343                         /*
344                          * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
345                          * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
346                          * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
347                          */
348                         e820_type = E820_RESERVED;
349                         break;
350                 }
351                 e820_add_region(start, size, e820_type);
352         }
353         sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
354 }
355
356 int __init efi_memblock_x86_reserve_range(void)
357 {
358         struct efi_info *e = &boot_params.efi_info;
359         unsigned long pmap;
360
361 #ifdef CONFIG_X86_32
362         /* Can't handle data above 4GB at this time */
363         if (e->efi_memmap_hi) {
364                 pr_err("Memory map is above 4GB, disabling EFI.\n");
365                 return -EINVAL;
366         }
367         pmap =  e->efi_memmap;
368 #else
369         pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
370 #endif
371         memmap.phys_map         = (void *)pmap;
372         memmap.nr_map           = e->efi_memmap_size /
373                                   e->efi_memdesc_size;
374         memmap.desc_size        = e->efi_memdesc_size;
375         memmap.desc_version     = e->efi_memdesc_version;
376
377         memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
378
379         efi.memmap = &memmap;
380
381         return 0;
382 }
383
384 static void __init print_efi_memmap(void)
385 {
386 #ifdef EFI_DEBUG
387         efi_memory_desc_t *md;
388         void *p;
389         int i;
390
391         for (p = memmap.map, i = 0;
392              p < memmap.map_end;
393              p += memmap.desc_size, i++) {
394                 md = p;
395                 pr_info("mem%02u: type=%u, attr=0x%llx, range=[0x%016llx-0x%016llx) (%lluMB)\n",
396                         i, md->type, md->attribute, md->phys_addr,
397                         md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
398                         (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
399         }
400 #endif  /*  EFI_DEBUG  */
401 }
402
403 void __init efi_reserve_boot_services(void)
404 {
405         void *p;
406
407         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
408                 efi_memory_desc_t *md = p;
409                 u64 start = md->phys_addr;
410                 u64 size = md->num_pages << EFI_PAGE_SHIFT;
411
412                 if (md->type != EFI_BOOT_SERVICES_CODE &&
413                     md->type != EFI_BOOT_SERVICES_DATA)
414                         continue;
415                 /* Only reserve where possible:
416                  * - Not within any already allocated areas
417                  * - Not over any memory area (really needed, if above?)
418                  * - Not within any part of the kernel
419                  * - Not the bios reserved area
420                 */
421                 if ((start + size > __pa_symbol(_text)
422                                 && start <= __pa_symbol(_end)) ||
423                         !e820_all_mapped(start, start+size, E820_RAM) ||
424                         memblock_is_region_reserved(start, size)) {
425                         /* Could not reserve, skip it */
426                         md->num_pages = 0;
427                         memblock_dbg("Could not reserve boot range [0x%010llx-0x%010llx]\n",
428                                      start, start+size-1);
429                 } else
430                         memblock_reserve(start, size);
431         }
432 }
433
434 void __init efi_unmap_memmap(void)
435 {
436         clear_bit(EFI_MEMMAP, &efi.flags);
437         if (memmap.map) {
438                 early_memunmap(memmap.map, memmap.nr_map * memmap.desc_size);
439                 memmap.map = NULL;
440         }
441 }
442
443 void __init efi_free_boot_services(void)
444 {
445         void *p;
446
447         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
448                 efi_memory_desc_t *md = p;
449                 unsigned long long start = md->phys_addr;
450                 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
451
452                 if (md->type != EFI_BOOT_SERVICES_CODE &&
453                     md->type != EFI_BOOT_SERVICES_DATA)
454                         continue;
455
456                 /* Could not reserve boot area */
457                 if (!size)
458                         continue;
459
460                 free_bootmem_late(start, size);
461         }
462
463         efi_unmap_memmap();
464 }
465
466 static int __init efi_systab_init(void *phys)
467 {
468         if (efi_enabled(EFI_64BIT)) {
469                 efi_system_table_64_t *systab64;
470                 struct efi_setup_data *data = NULL;
471                 u64 tmp = 0;
472
473                 if (efi_setup) {
474                         data = early_memremap(efi_setup, sizeof(*data));
475                         if (!data)
476                                 return -ENOMEM;
477                 }
478                 systab64 = early_memremap((unsigned long)phys,
479                                          sizeof(*systab64));
480                 if (systab64 == NULL) {
481                         pr_err("Couldn't map the system table!\n");
482                         if (data)
483                                 early_memunmap(data, sizeof(*data));
484                         return -ENOMEM;
485                 }
486
487                 efi_systab.hdr = systab64->hdr;
488                 efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor :
489                                               systab64->fw_vendor;
490                 tmp |= data ? data->fw_vendor : systab64->fw_vendor;
491                 efi_systab.fw_revision = systab64->fw_revision;
492                 efi_systab.con_in_handle = systab64->con_in_handle;
493                 tmp |= systab64->con_in_handle;
494                 efi_systab.con_in = systab64->con_in;
495                 tmp |= systab64->con_in;
496                 efi_systab.con_out_handle = systab64->con_out_handle;
497                 tmp |= systab64->con_out_handle;
498                 efi_systab.con_out = systab64->con_out;
499                 tmp |= systab64->con_out;
500                 efi_systab.stderr_handle = systab64->stderr_handle;
501                 tmp |= systab64->stderr_handle;
502                 efi_systab.stderr = systab64->stderr;
503                 tmp |= systab64->stderr;
504                 efi_systab.runtime = data ?
505                                      (void *)(unsigned long)data->runtime :
506                                      (void *)(unsigned long)systab64->runtime;
507                 tmp |= data ? data->runtime : systab64->runtime;
508                 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
509                 tmp |= systab64->boottime;
510                 efi_systab.nr_tables = systab64->nr_tables;
511                 efi_systab.tables = data ? (unsigned long)data->tables :
512                                            systab64->tables;
513                 tmp |= data ? data->tables : systab64->tables;
514
515                 early_memunmap(systab64, sizeof(*systab64));
516                 if (data)
517                         early_memunmap(data, sizeof(*data));
518 #ifdef CONFIG_X86_32
519                 if (tmp >> 32) {
520                         pr_err("EFI data located above 4GB, disabling EFI.\n");
521                         return -EINVAL;
522                 }
523 #endif
524         } else {
525                 efi_system_table_32_t *systab32;
526
527                 systab32 = early_memremap((unsigned long)phys,
528                                          sizeof(*systab32));
529                 if (systab32 == NULL) {
530                         pr_err("Couldn't map the system table!\n");
531                         return -ENOMEM;
532                 }
533
534                 efi_systab.hdr = systab32->hdr;
535                 efi_systab.fw_vendor = systab32->fw_vendor;
536                 efi_systab.fw_revision = systab32->fw_revision;
537                 efi_systab.con_in_handle = systab32->con_in_handle;
538                 efi_systab.con_in = systab32->con_in;
539                 efi_systab.con_out_handle = systab32->con_out_handle;
540                 efi_systab.con_out = systab32->con_out;
541                 efi_systab.stderr_handle = systab32->stderr_handle;
542                 efi_systab.stderr = systab32->stderr;
543                 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
544                 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
545                 efi_systab.nr_tables = systab32->nr_tables;
546                 efi_systab.tables = systab32->tables;
547
548                 early_memunmap(systab32, sizeof(*systab32));
549         }
550
551         efi.systab = &efi_systab;
552
553         /*
554          * Verify the EFI Table
555          */
556         if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
557                 pr_err("System table signature incorrect!\n");
558                 return -EINVAL;
559         }
560         if ((efi.systab->hdr.revision >> 16) == 0)
561                 pr_err("Warning: System table version %d.%02d, expected 1.00 or greater!\n",
562                        efi.systab->hdr.revision >> 16,
563                        efi.systab->hdr.revision & 0xffff);
564
565         set_bit(EFI_SYSTEM_TABLES, &efi.flags);
566
567         return 0;
568 }
569
570 static int __init efi_runtime_init32(void)
571 {
572         efi_runtime_services_32_t *runtime;
573
574         runtime = early_memremap((unsigned long)efi.systab->runtime,
575                         sizeof(efi_runtime_services_32_t));
576         if (!runtime) {
577                 pr_err("Could not map the runtime service table!\n");
578                 return -ENOMEM;
579         }
580
581         /*
582          * We will only need *early* access to the following two
583          * EFI runtime services before set_virtual_address_map
584          * is invoked.
585          */
586         efi_phys.set_virtual_address_map =
587                         (efi_set_virtual_address_map_t *)
588                         (unsigned long)runtime->set_virtual_address_map;
589         early_memunmap(runtime, sizeof(efi_runtime_services_32_t));
590
591         return 0;
592 }
593
594 static int __init efi_runtime_init64(void)
595 {
596         efi_runtime_services_64_t *runtime;
597
598         runtime = early_memremap((unsigned long)efi.systab->runtime,
599                         sizeof(efi_runtime_services_64_t));
600         if (!runtime) {
601                 pr_err("Could not map the runtime service table!\n");
602                 return -ENOMEM;
603         }
604
605         /*
606          * We will only need *early* access to the following two
607          * EFI runtime services before set_virtual_address_map
608          * is invoked.
609          */
610         efi_phys.set_virtual_address_map =
611                         (efi_set_virtual_address_map_t *)
612                         (unsigned long)runtime->set_virtual_address_map;
613         early_memunmap(runtime, sizeof(efi_runtime_services_64_t));
614
615         return 0;
616 }
617
618 static int __init efi_runtime_init(void)
619 {
620         int rv;
621
622         /*
623          * Check out the runtime services table. We need to map
624          * the runtime services table so that we can grab the physical
625          * address of several of the EFI runtime functions, needed to
626          * set the firmware into virtual mode.
627          */
628         if (efi_enabled(EFI_64BIT))
629                 rv = efi_runtime_init64();
630         else
631                 rv = efi_runtime_init32();
632
633         if (rv)
634                 return rv;
635
636         set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
637
638         return 0;
639 }
640
641 static int __init efi_memmap_init(void)
642 {
643         /* Map the EFI memory map */
644         memmap.map = early_memremap((unsigned long)memmap.phys_map,
645                                    memmap.nr_map * memmap.desc_size);
646         if (memmap.map == NULL) {
647                 pr_err("Could not map the memory map!\n");
648                 return -ENOMEM;
649         }
650         memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
651
652         if (add_efi_memmap)
653                 do_add_efi_memmap();
654
655         set_bit(EFI_MEMMAP, &efi.flags);
656
657         return 0;
658 }
659
660 /*
661  * A number of config table entries get remapped to virtual addresses
662  * after entering EFI virtual mode. However, the kexec kernel requires
663  * their physical addresses therefore we pass them via setup_data and
664  * correct those entries to their respective physical addresses here.
665  *
666  * Currently only handles smbios which is necessary for some firmware
667  * implementation.
668  */
669 static int __init efi_reuse_config(u64 tables, int nr_tables)
670 {
671         int i, sz, ret = 0;
672         void *p, *tablep;
673         struct efi_setup_data *data;
674
675         if (!efi_setup)
676                 return 0;
677
678         if (!efi_enabled(EFI_64BIT))
679                 return 0;
680
681         data = early_memremap(efi_setup, sizeof(*data));
682         if (!data) {
683                 ret = -ENOMEM;
684                 goto out;
685         }
686
687         if (!data->smbios)
688                 goto out_memremap;
689
690         sz = sizeof(efi_config_table_64_t);
691
692         p = tablep = early_memremap(tables, nr_tables * sz);
693         if (!p) {
694                 pr_err("Could not map Configuration table!\n");
695                 ret = -ENOMEM;
696                 goto out_memremap;
697         }
698
699         for (i = 0; i < efi.systab->nr_tables; i++) {
700                 efi_guid_t guid;
701
702                 guid = ((efi_config_table_64_t *)p)->guid;
703
704                 if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
705                         ((efi_config_table_64_t *)p)->table = data->smbios;
706                 p += sz;
707         }
708         early_iounmap(tablep, nr_tables * sz);
709
710 out_memremap:
711         early_iounmap(data, sizeof(*data));
712 out:
713         return ret;
714 }
715
716 void __init efi_init(void)
717 {
718         efi_char16_t *c16;
719         char vendor[100] = "unknown";
720         int i = 0;
721
722 #ifdef CONFIG_X86_32
723         if (boot_params.efi_info.efi_systab_hi ||
724             boot_params.efi_info.efi_memmap_hi) {
725                 pr_info("Table located above 4GB, disabling EFI.\n");
726                 return;
727         }
728         efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
729 #else
730         efi_phys.systab = (efi_system_table_t *)
731                           (boot_params.efi_info.efi_systab |
732                           ((__u64)boot_params.efi_info.efi_systab_hi<<32));
733 #endif
734
735         if (efi_systab_init(efi_phys.systab))
736                 return;
737
738         set_bit(EFI_SYSTEM_TABLES, &efi.flags);
739
740         efi.config_table = (unsigned long)efi.systab->tables;
741         efi.fw_vendor    = (unsigned long)efi.systab->fw_vendor;
742         efi.runtime      = (unsigned long)efi.systab->runtime;
743
744         /*
745          * Show what we know for posterity
746          */
747         c16 = early_memremap(efi.systab->fw_vendor,
748                              sizeof(vendor) * sizeof(efi_char16_t));
749         if (c16) {
750                 for (i = 0; i < sizeof(vendor) - 1 && c16[i]; ++i)
751                         vendor[i] = c16[i];
752                 vendor[i] = '\0';
753                 early_memunmap(c16, sizeof(vendor) * sizeof(efi_char16_t));
754         } else {
755                 pr_err("Could not map the firmware vendor!\n");
756         }
757
758         pr_info("EFI v%u.%.02u by %s\n",
759                 efi.systab->hdr.revision >> 16,
760                 efi.systab->hdr.revision & 0xffff, vendor);
761
762         if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
763                 return;
764
765         if (efi_config_init(arch_tables))
766                 return;
767
768         /*
769          * Note: We currently don't support runtime services on an EFI
770          * that doesn't match the kernel 32/64-bit mode.
771          */
772
773         if (!efi_runtime_supported())
774                 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
775         else {
776                 if (disable_runtime || efi_runtime_init())
777                         return;
778         }
779         if (efi_memmap_init())
780                 return;
781
782         set_bit(EFI_MEMMAP, &efi.flags);
783
784         print_efi_memmap();
785 }
786
787 void __init efi_late_init(void)
788 {
789         efi_bgrt_init();
790 }
791
792 void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
793 {
794         u64 addr, npages;
795
796         addr = md->virt_addr;
797         npages = md->num_pages;
798
799         memrange_efi_to_native(&addr, &npages);
800
801         if (executable)
802                 set_memory_x(addr, npages);
803         else
804                 set_memory_nx(addr, npages);
805 }
806
807 void __init runtime_code_page_mkexec(void)
808 {
809         efi_memory_desc_t *md;
810         void *p;
811
812         /* Make EFI runtime service code area executable */
813         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
814                 md = p;
815
816                 if (md->type != EFI_RUNTIME_SERVICES_CODE)
817                         continue;
818
819                 efi_set_executable(md, true);
820         }
821 }
822
823 void efi_memory_uc(u64 addr, unsigned long size)
824 {
825         unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
826         u64 npages;
827
828         npages = round_up(size, page_shift) / page_shift;
829         memrange_efi_to_native(&addr, &npages);
830         set_memory_uc(addr, npages);
831 }
832
833 void __init old_map_region(efi_memory_desc_t *md)
834 {
835         u64 start_pfn, end_pfn, end;
836         unsigned long size;
837         void *va;
838
839         start_pfn = PFN_DOWN(md->phys_addr);
840         size      = md->num_pages << PAGE_SHIFT;
841         end       = md->phys_addr + size;
842         end_pfn   = PFN_UP(end);
843
844         if (pfn_range_is_mapped(start_pfn, end_pfn)) {
845                 va = __va(md->phys_addr);
846
847                 if (!(md->attribute & EFI_MEMORY_WB))
848                         efi_memory_uc((u64)(unsigned long)va, size);
849         } else
850                 va = efi_ioremap(md->phys_addr, size,
851                                  md->type, md->attribute);
852
853         md->virt_addr = (u64) (unsigned long) va;
854         if (!va)
855                 pr_err("ioremap of 0x%llX failed!\n",
856                        (unsigned long long)md->phys_addr);
857 }
858
859 static void native_runtime_setup(void)
860 {
861         efi.get_time = virt_efi_get_time;
862         efi.set_time = virt_efi_set_time;
863         efi.get_wakeup_time = virt_efi_get_wakeup_time;
864         efi.set_wakeup_time = virt_efi_set_wakeup_time;
865         efi.get_variable = virt_efi_get_variable;
866         efi.get_next_variable = virt_efi_get_next_variable;
867         efi.set_variable = virt_efi_set_variable;
868         efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
869         efi.reset_system = virt_efi_reset_system;
870         efi.query_variable_info = virt_efi_query_variable_info;
871         efi.update_capsule = virt_efi_update_capsule;
872         efi.query_capsule_caps = virt_efi_query_capsule_caps;
873 }
874
875 /* Merge contiguous regions of the same type and attribute */
876 static void __init efi_merge_regions(void)
877 {
878         void *p;
879         efi_memory_desc_t *md, *prev_md = NULL;
880
881         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
882                 u64 prev_size;
883                 md = p;
884
885                 if (!prev_md) {
886                         prev_md = md;
887                         continue;
888                 }
889
890                 if (prev_md->type != md->type ||
891                     prev_md->attribute != md->attribute) {
892                         prev_md = md;
893                         continue;
894                 }
895
896                 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
897
898                 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
899                         prev_md->num_pages += md->num_pages;
900                         md->type = EFI_RESERVED_TYPE;
901                         md->attribute = 0;
902                         continue;
903                 }
904                 prev_md = md;
905         }
906 }
907
908 static void __init get_systab_virt_addr(efi_memory_desc_t *md)
909 {
910         unsigned long size;
911         u64 end, systab;
912
913         size = md->num_pages << EFI_PAGE_SHIFT;
914         end = md->phys_addr + size;
915         systab = (u64)(unsigned long)efi_phys.systab;
916         if (md->phys_addr <= systab && systab < end) {
917                 systab += md->virt_addr - md->phys_addr;
918                 efi.systab = (efi_system_table_t *)(unsigned long)systab;
919         }
920 }
921
922 static void __init save_runtime_map(void)
923 {
924 #ifdef CONFIG_KEXEC
925         efi_memory_desc_t *md;
926         void *tmp, *p, *q = NULL;
927         int count = 0;
928
929         if (efi_enabled(EFI_OLD_MEMMAP))
930                 return;
931
932         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
933                 md = p;
934
935                 if (!(md->attribute & EFI_MEMORY_RUNTIME) ||
936                     (md->type == EFI_BOOT_SERVICES_CODE) ||
937                     (md->type == EFI_BOOT_SERVICES_DATA))
938                         continue;
939                 tmp = krealloc(q, (count + 1) * memmap.desc_size, GFP_KERNEL);
940                 if (!tmp)
941                         goto out;
942                 q = tmp;
943
944                 memcpy(q + count * memmap.desc_size, md, memmap.desc_size);
945                 count++;
946         }
947
948         efi_runtime_map_setup(q, count, memmap.desc_size);
949         return;
950
951 out:
952         kfree(q);
953         pr_err("Error saving runtime map, efi runtime on kexec non-functional!!\n");
954 #endif
955 }
956
957 static void *realloc_pages(void *old_memmap, int old_shift)
958 {
959         void *ret;
960
961         ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1);
962         if (!ret)
963                 goto out;
964
965         /*
966          * A first-time allocation doesn't have anything to copy.
967          */
968         if (!old_memmap)
969                 return ret;
970
971         memcpy(ret, old_memmap, PAGE_SIZE << old_shift);
972
973 out:
974         free_pages((unsigned long)old_memmap, old_shift);
975         return ret;
976 }
977
978 /*
979  * Iterate the EFI memory map in reverse order because the regions
980  * will be mapped top-down. The end result is the same as if we had
981  * mapped things forward, but doesn't require us to change the
982  * existing implementation of efi_map_region().
983  */
984 static inline void *efi_map_next_entry_reverse(void *entry)
985 {
986         /* Initial call */
987         if (!entry)
988                 return memmap.map_end - memmap.desc_size;
989
990         entry -= memmap.desc_size;
991         if (entry < memmap.map)
992                 return NULL;
993
994         return entry;
995 }
996
997 /*
998  * efi_map_next_entry - Return the next EFI memory map descriptor
999  * @entry: Previous EFI memory map descriptor
1000  *
1001  * This is a helper function to iterate over the EFI memory map, which
1002  * we do in different orders depending on the current configuration.
1003  *
1004  * To begin traversing the memory map @entry must be %NULL.
1005  *
1006  * Returns %NULL when we reach the end of the memory map.
1007  */
1008 static void *efi_map_next_entry(void *entry)
1009 {
1010         if (!efi_enabled(EFI_OLD_MEMMAP) && efi_enabled(EFI_64BIT)) {
1011                 /*
1012                  * Starting in UEFI v2.5 the EFI_PROPERTIES_TABLE
1013                  * config table feature requires us to map all entries
1014                  * in the same order as they appear in the EFI memory
1015                  * map. That is to say, entry N must have a lower
1016                  * virtual address than entry N+1. This is because the
1017                  * firmware toolchain leaves relative references in
1018                  * the code/data sections, which are split and become
1019                  * separate EFI memory regions. Mapping things
1020                  * out-of-order leads to the firmware accessing
1021                  * unmapped addresses.
1022                  *
1023                  * Since we need to map things this way whether or not
1024                  * the kernel actually makes use of
1025                  * EFI_PROPERTIES_TABLE, let's just switch to this
1026                  * scheme by default for 64-bit.
1027                  */
1028                 return efi_map_next_entry_reverse(entry);
1029         }
1030
1031         /* Initial call */
1032         if (!entry)
1033                 return memmap.map;
1034
1035         entry += memmap.desc_size;
1036         if (entry >= memmap.map_end)
1037                 return NULL;
1038
1039         return entry;
1040 }
1041
1042 /*
1043  * Map the efi memory ranges of the runtime services and update new_mmap with
1044  * virtual addresses.
1045  */
1046 static void * __init efi_map_regions(int *count, int *pg_shift)
1047 {
1048         void *p, *new_memmap = NULL;
1049         unsigned long left = 0;
1050         efi_memory_desc_t *md;
1051
1052         p = NULL;
1053         while ((p = efi_map_next_entry(p))) {
1054                 md = p;
1055                 if (!(md->attribute & EFI_MEMORY_RUNTIME)) {
1056 #ifdef CONFIG_X86_64
1057                         if (md->type != EFI_BOOT_SERVICES_CODE &&
1058                             md->type != EFI_BOOT_SERVICES_DATA)
1059 #endif
1060                                 continue;
1061                 }
1062
1063                 efi_map_region(md);
1064                 get_systab_virt_addr(md);
1065
1066                 if (left < memmap.desc_size) {
1067                         new_memmap = realloc_pages(new_memmap, *pg_shift);
1068                         if (!new_memmap)
1069                                 return NULL;
1070
1071                         left += PAGE_SIZE << *pg_shift;
1072                         (*pg_shift)++;
1073                 }
1074
1075                 memcpy(new_memmap + (*count * memmap.desc_size), md,
1076                        memmap.desc_size);
1077
1078                 left -= memmap.desc_size;
1079                 (*count)++;
1080         }
1081
1082         return new_memmap;
1083 }
1084
1085 static void __init kexec_enter_virtual_mode(void)
1086 {
1087 #ifdef CONFIG_KEXEC
1088         efi_memory_desc_t *md;
1089         void *p;
1090
1091         efi.systab = NULL;
1092
1093         /*
1094          * We don't do virtual mode, since we don't do runtime services, on
1095          * non-native EFI
1096          */
1097         if (!efi_is_native()) {
1098                 efi_unmap_memmap();
1099                 return;
1100         }
1101
1102         /*
1103         * Map efi regions which were passed via setup_data. The virt_addr is a
1104         * fixed addr which was used in first kernel of a kexec boot.
1105         */
1106         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1107                 md = p;
1108                 efi_map_region_fixed(md); /* FIXME: add error handling */
1109                 get_systab_virt_addr(md);
1110         }
1111
1112         save_runtime_map();
1113
1114         BUG_ON(!efi.systab);
1115
1116         efi_sync_low_kernel_mappings();
1117
1118         /*
1119          * Now that EFI is in virtual mode, update the function
1120          * pointers in the runtime service table to the new virtual addresses.
1121          *
1122          * Call EFI services through wrapper functions.
1123          */
1124         efi.runtime_version = efi_systab.hdr.revision;
1125
1126         native_runtime_setup();
1127
1128         efi.set_virtual_address_map = NULL;
1129
1130         if (efi_enabled(EFI_OLD_MEMMAP) && (__supported_pte_mask & _PAGE_NX))
1131                 runtime_code_page_mkexec();
1132
1133         /* clean DUMMY object */
1134         efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1135                          EFI_VARIABLE_NON_VOLATILE |
1136                          EFI_VARIABLE_BOOTSERVICE_ACCESS |
1137                          EFI_VARIABLE_RUNTIME_ACCESS,
1138                          0, NULL);
1139 #endif
1140 }
1141
1142 /*
1143  * This function will switch the EFI runtime services to virtual mode.
1144  * Essentially, we look through the EFI memmap and map every region that
1145  * has the runtime attribute bit set in its memory descriptor into the
1146  * ->trampoline_pgd page table using a top-down VA allocation scheme.
1147  *
1148  * The old method which used to update that memory descriptor with the
1149  * virtual address obtained from ioremap() is still supported when the
1150  * kernel is booted with efi=old_map on its command line. Same old
1151  * method enabled the runtime services to be called without having to
1152  * thunk back into physical mode for every invocation.
1153  *
1154  * The new method does a pagetable switch in a preemption-safe manner
1155  * so that we're in a different address space when calling a runtime
1156  * function. For function arguments passing we do copy the PGDs of the
1157  * kernel page table into ->trampoline_pgd prior to each call.
1158  *
1159  * Specially for kexec boot, efi runtime maps in previous kernel should
1160  * be passed in via setup_data. In that case runtime ranges will be mapped
1161  * to the same virtual addresses as the first kernel, see
1162  * kexec_enter_virtual_mode().
1163  */
1164 static void __init __efi_enter_virtual_mode(void)
1165 {
1166         int count = 0, pg_shift = 0;
1167         void *new_memmap = NULL;
1168         efi_status_t status;
1169
1170         efi.systab = NULL;
1171
1172         efi_merge_regions();
1173         new_memmap = efi_map_regions(&count, &pg_shift);
1174         if (!new_memmap) {
1175                 pr_err("Error reallocating memory, EFI runtime non-functional!\n");
1176                 return;
1177         }
1178
1179         save_runtime_map();
1180
1181         BUG_ON(!efi.systab);
1182
1183         if (efi_setup_page_tables(__pa(new_memmap), 1 << pg_shift))
1184                 return;
1185
1186         efi_sync_low_kernel_mappings();
1187         efi_dump_pagetable();
1188
1189         if (efi_is_native()) {
1190                 status = phys_efi_set_virtual_address_map(
1191                                 memmap.desc_size * count,
1192                                 memmap.desc_size,
1193                                 memmap.desc_version,
1194                                 (efi_memory_desc_t *)__pa(new_memmap));
1195         } else {
1196                 status = efi_thunk_set_virtual_address_map(
1197                                 efi_phys.set_virtual_address_map,
1198                                 memmap.desc_size * count,
1199                                 memmap.desc_size,
1200                                 memmap.desc_version,
1201                                 (efi_memory_desc_t *)__pa(new_memmap));
1202         }
1203
1204         if (status != EFI_SUCCESS) {
1205                 pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
1206                          status);
1207                 panic("EFI call to SetVirtualAddressMap() failed!");
1208         }
1209
1210         /*
1211          * Now that EFI is in virtual mode, update the function
1212          * pointers in the runtime service table to the new virtual addresses.
1213          *
1214          * Call EFI services through wrapper functions.
1215          */
1216         efi.runtime_version = efi_systab.hdr.revision;
1217
1218         if (efi_is_native())
1219                 native_runtime_setup();
1220         else
1221                 efi_thunk_runtime_setup();
1222
1223         efi.set_virtual_address_map = NULL;
1224
1225         efi_runtime_mkexec();
1226
1227         /*
1228          * We mapped the descriptor array into the EFI pagetable above but we're
1229          * not unmapping it here. Here's why:
1230          *
1231          * We're copying select PGDs from the kernel page table to the EFI page
1232          * table and when we do so and make changes to those PGDs like unmapping
1233          * stuff from them, those changes appear in the kernel page table and we
1234          * go boom.
1235          *
1236          * From setup_real_mode():
1237          *
1238          * ...
1239          * trampoline_pgd[0] = init_level4_pgt[pgd_index(__PAGE_OFFSET)].pgd;
1240          *
1241          * In this particular case, our allocation is in PGD 0 of the EFI page
1242          * table but we've copied that PGD from PGD[272] of the EFI page table:
1243          *
1244          *      pgd_index(__PAGE_OFFSET = 0xffff880000000000) = 272
1245          *
1246          * where the direct memory mapping in kernel space is.
1247          *
1248          * new_memmap's VA comes from that direct mapping and thus clearing it,
1249          * it would get cleared in the kernel page table too.
1250          *
1251          * efi_cleanup_page_tables(__pa(new_memmap), 1 << pg_shift);
1252          */
1253         free_pages((unsigned long)new_memmap, pg_shift);
1254
1255         /* clean DUMMY object */
1256         efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1257                          EFI_VARIABLE_NON_VOLATILE |
1258                          EFI_VARIABLE_BOOTSERVICE_ACCESS |
1259                          EFI_VARIABLE_RUNTIME_ACCESS,
1260                          0, NULL);
1261 }
1262
1263 void __init efi_enter_virtual_mode(void)
1264 {
1265         if (efi_setup)
1266                 kexec_enter_virtual_mode();
1267         else
1268                 __efi_enter_virtual_mode();
1269 }
1270
1271 /*
1272  * Convenience functions to obtain memory types and attributes
1273  */
1274 u32 efi_mem_type(unsigned long phys_addr)
1275 {
1276         efi_memory_desc_t *md;
1277         void *p;
1278
1279         if (!efi_enabled(EFI_MEMMAP))
1280                 return 0;
1281
1282         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1283                 md = p;
1284                 if ((md->phys_addr <= phys_addr) &&
1285                     (phys_addr < (md->phys_addr +
1286                                   (md->num_pages << EFI_PAGE_SHIFT))))
1287                         return md->type;
1288         }
1289         return 0;
1290 }
1291
1292 u64 efi_mem_attributes(unsigned long phys_addr)
1293 {
1294         efi_memory_desc_t *md;
1295         void *p;
1296
1297         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1298                 md = p;
1299                 if ((md->phys_addr <= phys_addr) &&
1300                     (phys_addr < (md->phys_addr +
1301                                   (md->num_pages << EFI_PAGE_SHIFT))))
1302                         return md->attribute;
1303         }
1304         return 0;
1305 }
1306
1307 /*
1308  * Some firmware implementations refuse to boot if there's insufficient space
1309  * in the variable store. Ensure that we never use more than a safe limit.
1310  *
1311  * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
1312  * store.
1313  */
1314 efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
1315 {
1316         efi_status_t status;
1317         u64 storage_size, remaining_size, max_size;
1318
1319         if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
1320                 return 0;
1321
1322         status = efi.query_variable_info(attributes, &storage_size,
1323                                          &remaining_size, &max_size);
1324         if (status != EFI_SUCCESS)
1325                 return status;
1326
1327         /*
1328          * We account for that by refusing the write if permitting it would
1329          * reduce the available space to under 5KB. This figure was provided by
1330          * Samsung, so should be safe.
1331          */
1332         if ((remaining_size - size < EFI_MIN_RESERVE) &&
1333                 !efi_no_storage_paranoia) {
1334
1335                 /*
1336                  * Triggering garbage collection may require that the firmware
1337                  * generate a real EFI_OUT_OF_RESOURCES error. We can force
1338                  * that by attempting to use more space than is available.
1339                  */
1340                 unsigned long dummy_size = remaining_size + 1024;
1341                 void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
1342
1343                 if (!dummy)
1344                         return EFI_OUT_OF_RESOURCES;
1345
1346                 status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1347                                           EFI_VARIABLE_NON_VOLATILE |
1348                                           EFI_VARIABLE_BOOTSERVICE_ACCESS |
1349                                           EFI_VARIABLE_RUNTIME_ACCESS,
1350                                           dummy_size, dummy);
1351
1352                 if (status == EFI_SUCCESS) {
1353                         /*
1354                          * This should have failed, so if it didn't make sure
1355                          * that we delete it...
1356                          */
1357                         efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1358                                          EFI_VARIABLE_NON_VOLATILE |
1359                                          EFI_VARIABLE_BOOTSERVICE_ACCESS |
1360                                          EFI_VARIABLE_RUNTIME_ACCESS,
1361                                          0, dummy);
1362                 }
1363
1364                 kfree(dummy);
1365
1366                 /*
1367                  * The runtime code may now have triggered a garbage collection
1368                  * run, so check the variable info again
1369                  */
1370                 status = efi.query_variable_info(attributes, &storage_size,
1371                                                  &remaining_size, &max_size);
1372
1373                 if (status != EFI_SUCCESS)
1374                         return status;
1375
1376                 /*
1377                  * There still isn't enough room, so return an error
1378                  */
1379                 if (remaining_size - size < EFI_MIN_RESERVE)
1380                         return EFI_OUT_OF_RESOURCES;
1381         }
1382
1383         return EFI_SUCCESS;
1384 }
1385 EXPORT_SYMBOL_GPL(efi_query_variable_store);
1386
1387 static int __init parse_efi_cmdline(char *str)
1388 {
1389         if (*str == '=')
1390                 str++;
1391
1392         if (!strncmp(str, "old_map", 7))
1393                 set_bit(EFI_OLD_MEMMAP, &efi.flags);
1394
1395         return 0;
1396 }
1397 early_param("efi", parse_efi_cmdline);
1398
1399 void __init efi_apply_memmap_quirks(void)
1400 {
1401         /*
1402          * Once setup is done earlier, unmap the EFI memory map on mismatched
1403          * firmware/kernel architectures since there is no support for runtime
1404          * services.
1405          */
1406         if (!efi_runtime_supported()) {
1407                 pr_info("efi: Setup done, disabling due to 32/64-bit mismatch\n");
1408                 efi_unmap_memmap();
1409         }
1410
1411         /*
1412          * UV doesn't support the new EFI pagetable mapping yet.
1413          */
1414         if (is_uv_system())
1415                 set_bit(EFI_OLD_MEMMAP, &efi.flags);
1416 }
1417
1418 /*
1419  * For most modern platforms the preferred method of powering off is via
1420  * ACPI. However, there are some that are known to require the use of
1421  * EFI runtime services and for which ACPI does not work at all.
1422  *
1423  * Using EFI is a last resort, to be used only if no other option
1424  * exists.
1425  */
1426 bool efi_reboot_required(void)
1427 {
1428         if (!acpi_gbl_reduced_hardware)
1429                 return false;
1430
1431         efi_reboot_quirk_mode = EFI_RESET_WARM;
1432         return true;
1433 }
1434
1435 bool efi_poweroff_required(void)
1436 {
1437         return !!acpi_gbl_reduced_hardware;
1438 }