]> Pileus Git - ~andy/linux/blob - arch/x86/platform/efi/efi.c
Merge tag 'efi-next' of git://git.kernel.org/pub/scm/linux/kernel/git/mfleming/efi...
[~andy/linux] / 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
48 #include <asm/setup.h>
49 #include <asm/efi.h>
50 #include <asm/time.h>
51 #include <asm/cacheflush.h>
52 #include <asm/tlbflush.h>
53 #include <asm/x86_init.h>
54 #include <asm/rtc.h>
55
56 #define EFI_DEBUG
57
58 #define EFI_MIN_RESERVE 5120
59
60 #define EFI_DUMMY_GUID \
61         EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
62
63 static efi_char16_t efi_dummy_name[6] = { 'D', 'U', 'M', 'M', 'Y', 0 };
64
65 struct efi_memory_map memmap;
66
67 static struct efi efi_phys __initdata;
68 static efi_system_table_t efi_systab __initdata;
69
70 unsigned long x86_efi_facility;
71
72 static __initdata efi_config_table_type_t arch_tables[] = {
73 #ifdef CONFIG_X86_UV
74         {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab},
75 #endif
76         {NULL_GUID, NULL, NULL},
77 };
78
79 /*
80  * Returns 1 if 'facility' is enabled, 0 otherwise.
81  */
82 int efi_enabled(int facility)
83 {
84         return test_bit(facility, &x86_efi_facility) != 0;
85 }
86 EXPORT_SYMBOL(efi_enabled);
87
88 static bool __initdata disable_runtime = false;
89 static int __init setup_noefi(char *arg)
90 {
91         disable_runtime = true;
92         return 0;
93 }
94 early_param("noefi", setup_noefi);
95
96 int add_efi_memmap;
97 EXPORT_SYMBOL(add_efi_memmap);
98
99 static int __init setup_add_efi_memmap(char *arg)
100 {
101         add_efi_memmap = 1;
102         return 0;
103 }
104 early_param("add_efi_memmap", setup_add_efi_memmap);
105
106 static bool efi_no_storage_paranoia;
107
108 static int __init setup_storage_paranoia(char *arg)
109 {
110         efi_no_storage_paranoia = true;
111         return 0;
112 }
113 early_param("efi_no_storage_paranoia", setup_storage_paranoia);
114
115
116 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
117 {
118         unsigned long flags;
119         efi_status_t status;
120
121         spin_lock_irqsave(&rtc_lock, flags);
122         status = efi_call_virt2(get_time, tm, tc);
123         spin_unlock_irqrestore(&rtc_lock, flags);
124         return status;
125 }
126
127 static efi_status_t virt_efi_set_time(efi_time_t *tm)
128 {
129         unsigned long flags;
130         efi_status_t status;
131
132         spin_lock_irqsave(&rtc_lock, flags);
133         status = efi_call_virt1(set_time, tm);
134         spin_unlock_irqrestore(&rtc_lock, flags);
135         return status;
136 }
137
138 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
139                                              efi_bool_t *pending,
140                                              efi_time_t *tm)
141 {
142         unsigned long flags;
143         efi_status_t status;
144
145         spin_lock_irqsave(&rtc_lock, flags);
146         status = efi_call_virt3(get_wakeup_time,
147                                 enabled, pending, tm);
148         spin_unlock_irqrestore(&rtc_lock, flags);
149         return status;
150 }
151
152 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
153 {
154         unsigned long flags;
155         efi_status_t status;
156
157         spin_lock_irqsave(&rtc_lock, flags);
158         status = efi_call_virt2(set_wakeup_time,
159                                 enabled, tm);
160         spin_unlock_irqrestore(&rtc_lock, flags);
161         return status;
162 }
163
164 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
165                                           efi_guid_t *vendor,
166                                           u32 *attr,
167                                           unsigned long *data_size,
168                                           void *data)
169 {
170         return efi_call_virt5(get_variable,
171                               name, vendor, attr,
172                               data_size, data);
173 }
174
175 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
176                                                efi_char16_t *name,
177                                                efi_guid_t *vendor)
178 {
179         return efi_call_virt3(get_next_variable,
180                               name_size, name, vendor);
181 }
182
183 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
184                                           efi_guid_t *vendor,
185                                           u32 attr,
186                                           unsigned long data_size,
187                                           void *data)
188 {
189         return efi_call_virt5(set_variable,
190                               name, vendor, attr,
191                               data_size, data);
192 }
193
194 static efi_status_t virt_efi_query_variable_info(u32 attr,
195                                                  u64 *storage_space,
196                                                  u64 *remaining_space,
197                                                  u64 *max_variable_size)
198 {
199         if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
200                 return EFI_UNSUPPORTED;
201
202         return efi_call_virt4(query_variable_info, attr, storage_space,
203                               remaining_space, max_variable_size);
204 }
205
206 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
207 {
208         return efi_call_virt1(get_next_high_mono_count, count);
209 }
210
211 static void virt_efi_reset_system(int reset_type,
212                                   efi_status_t status,
213                                   unsigned long data_size,
214                                   efi_char16_t *data)
215 {
216         efi_call_virt4(reset_system, reset_type, status,
217                        data_size, data);
218 }
219
220 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
221                                             unsigned long count,
222                                             unsigned long sg_list)
223 {
224         if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
225                 return EFI_UNSUPPORTED;
226
227         return efi_call_virt3(update_capsule, capsules, count, sg_list);
228 }
229
230 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
231                                                 unsigned long count,
232                                                 u64 *max_size,
233                                                 int *reset_type)
234 {
235         if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
236                 return EFI_UNSUPPORTED;
237
238         return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
239                               reset_type);
240 }
241
242 static efi_status_t __init phys_efi_set_virtual_address_map(
243         unsigned long memory_map_size,
244         unsigned long descriptor_size,
245         u32 descriptor_version,
246         efi_memory_desc_t *virtual_map)
247 {
248         efi_status_t status;
249
250         efi_call_phys_prelog();
251         status = efi_call_phys4(efi_phys.set_virtual_address_map,
252                                 memory_map_size, descriptor_size,
253                                 descriptor_version, virtual_map);
254         efi_call_phys_epilog();
255         return status;
256 }
257
258 static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
259                                              efi_time_cap_t *tc)
260 {
261         unsigned long flags;
262         efi_status_t status;
263
264         spin_lock_irqsave(&rtc_lock, flags);
265         efi_call_phys_prelog();
266         status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm),
267                                 virt_to_phys(tc));
268         efi_call_phys_epilog();
269         spin_unlock_irqrestore(&rtc_lock, flags);
270         return status;
271 }
272
273 int efi_set_rtc_mmss(const struct timespec *now)
274 {
275         unsigned long nowtime = now->tv_sec;
276         efi_status_t    status;
277         efi_time_t      eft;
278         efi_time_cap_t  cap;
279         struct rtc_time tm;
280
281         status = efi.get_time(&eft, &cap);
282         if (status != EFI_SUCCESS) {
283                 pr_err("Oops: efitime: can't read time!\n");
284                 return -1;
285         }
286
287         rtc_time_to_tm(nowtime, &tm);
288         if (!rtc_valid_tm(&tm)) {
289                 eft.year = tm.tm_year + 1900;
290                 eft.month = tm.tm_mon + 1;
291                 eft.day = tm.tm_mday;
292                 eft.minute = tm.tm_min;
293                 eft.second = tm.tm_sec;
294                 eft.nanosecond = 0;
295         } else {
296                 printk(KERN_ERR
297                        "%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
298                        __FUNCTION__, nowtime);
299                 return -1;
300         }
301
302         status = efi.set_time(&eft);
303         if (status != EFI_SUCCESS) {
304                 pr_err("Oops: efitime: can't write time!\n");
305                 return -1;
306         }
307         return 0;
308 }
309
310 void efi_get_time(struct timespec *now)
311 {
312         efi_status_t status;
313         efi_time_t eft;
314         efi_time_cap_t cap;
315
316         status = efi.get_time(&eft, &cap);
317         if (status != EFI_SUCCESS)
318                 pr_err("Oops: efitime: can't read time!\n");
319
320         now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour,
321                              eft.minute, eft.second);
322         now->tv_nsec = 0;
323 }
324
325 /*
326  * Tell the kernel about the EFI memory map.  This might include
327  * more than the max 128 entries that can fit in the e820 legacy
328  * (zeropage) memory map.
329  */
330
331 static void __init do_add_efi_memmap(void)
332 {
333         void *p;
334
335         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
336                 efi_memory_desc_t *md = p;
337                 unsigned long long start = md->phys_addr;
338                 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
339                 int e820_type;
340
341                 switch (md->type) {
342                 case EFI_LOADER_CODE:
343                 case EFI_LOADER_DATA:
344                 case EFI_BOOT_SERVICES_CODE:
345                 case EFI_BOOT_SERVICES_DATA:
346                 case EFI_CONVENTIONAL_MEMORY:
347                         if (md->attribute & EFI_MEMORY_WB)
348                                 e820_type = E820_RAM;
349                         else
350                                 e820_type = E820_RESERVED;
351                         break;
352                 case EFI_ACPI_RECLAIM_MEMORY:
353                         e820_type = E820_ACPI;
354                         break;
355                 case EFI_ACPI_MEMORY_NVS:
356                         e820_type = E820_NVS;
357                         break;
358                 case EFI_UNUSABLE_MEMORY:
359                         e820_type = E820_UNUSABLE;
360                         break;
361                 default:
362                         /*
363                          * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
364                          * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
365                          * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
366                          */
367                         e820_type = E820_RESERVED;
368                         break;
369                 }
370                 e820_add_region(start, size, e820_type);
371         }
372         sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
373 }
374
375 int __init efi_memblock_x86_reserve_range(void)
376 {
377         struct efi_info *e = &boot_params.efi_info;
378         unsigned long pmap;
379
380 #ifdef CONFIG_X86_32
381         /* Can't handle data above 4GB at this time */
382         if (e->efi_memmap_hi) {
383                 pr_err("Memory map is above 4GB, disabling EFI.\n");
384                 return -EINVAL;
385         }
386         pmap =  e->efi_memmap;
387 #else
388         pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
389 #endif
390         memmap.phys_map         = (void *)pmap;
391         memmap.nr_map           = e->efi_memmap_size /
392                                   e->efi_memdesc_size;
393         memmap.desc_size        = e->efi_memdesc_size;
394         memmap.desc_version     = e->efi_memdesc_version;
395
396         memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
397
398         efi.memmap = &memmap;
399
400         return 0;
401 }
402
403 static void __init print_efi_memmap(void)
404 {
405 #ifdef EFI_DEBUG
406         efi_memory_desc_t *md;
407         void *p;
408         int i;
409
410         for (p = memmap.map, i = 0;
411              p < memmap.map_end;
412              p += memmap.desc_size, i++) {
413                 md = p;
414                 pr_info("mem%02u: type=%u, attr=0x%llx, "
415                         "range=[0x%016llx-0x%016llx) (%lluMB)\n",
416                         i, md->type, md->attribute, md->phys_addr,
417                         md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
418                         (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
419         }
420 #endif  /*  EFI_DEBUG  */
421 }
422
423 void __init efi_reserve_boot_services(void)
424 {
425         void *p;
426
427         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
428                 efi_memory_desc_t *md = p;
429                 u64 start = md->phys_addr;
430                 u64 size = md->num_pages << EFI_PAGE_SHIFT;
431
432                 if (md->type != EFI_BOOT_SERVICES_CODE &&
433                     md->type != EFI_BOOT_SERVICES_DATA)
434                         continue;
435                 /* Only reserve where possible:
436                  * - Not within any already allocated areas
437                  * - Not over any memory area (really needed, if above?)
438                  * - Not within any part of the kernel
439                  * - Not the bios reserved area
440                 */
441                 if ((start+size >= __pa_symbol(_text)
442                                 && start <= __pa_symbol(_end)) ||
443                         !e820_all_mapped(start, start+size, E820_RAM) ||
444                         memblock_is_region_reserved(start, size)) {
445                         /* Could not reserve, skip it */
446                         md->num_pages = 0;
447                         memblock_dbg("Could not reserve boot range "
448                                         "[0x%010llx-0x%010llx]\n",
449                                                 start, start+size-1);
450                 } else
451                         memblock_reserve(start, size);
452         }
453 }
454
455 void __init efi_unmap_memmap(void)
456 {
457         clear_bit(EFI_MEMMAP, &x86_efi_facility);
458         if (memmap.map) {
459                 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
460                 memmap.map = NULL;
461         }
462 }
463
464 void __init efi_free_boot_services(void)
465 {
466         void *p;
467
468         if (!efi_is_native())
469                 return;
470
471         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
472                 efi_memory_desc_t *md = p;
473                 unsigned long long start = md->phys_addr;
474                 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
475
476                 if (md->type != EFI_BOOT_SERVICES_CODE &&
477                     md->type != EFI_BOOT_SERVICES_DATA)
478                         continue;
479
480                 /* Could not reserve boot area */
481                 if (!size)
482                         continue;
483
484                 free_bootmem_late(start, size);
485         }
486
487         efi_unmap_memmap();
488 }
489
490 static int __init efi_systab_init(void *phys)
491 {
492         if (efi_enabled(EFI_64BIT)) {
493                 efi_system_table_64_t *systab64;
494                 u64 tmp = 0;
495
496                 systab64 = early_ioremap((unsigned long)phys,
497                                          sizeof(*systab64));
498                 if (systab64 == NULL) {
499                         pr_err("Couldn't map the system table!\n");
500                         return -ENOMEM;
501                 }
502
503                 efi_systab.hdr = systab64->hdr;
504                 efi_systab.fw_vendor = systab64->fw_vendor;
505                 tmp |= systab64->fw_vendor;
506                 efi_systab.fw_revision = systab64->fw_revision;
507                 efi_systab.con_in_handle = systab64->con_in_handle;
508                 tmp |= systab64->con_in_handle;
509                 efi_systab.con_in = systab64->con_in;
510                 tmp |= systab64->con_in;
511                 efi_systab.con_out_handle = systab64->con_out_handle;
512                 tmp |= systab64->con_out_handle;
513                 efi_systab.con_out = systab64->con_out;
514                 tmp |= systab64->con_out;
515                 efi_systab.stderr_handle = systab64->stderr_handle;
516                 tmp |= systab64->stderr_handle;
517                 efi_systab.stderr = systab64->stderr;
518                 tmp |= systab64->stderr;
519                 efi_systab.runtime = (void *)(unsigned long)systab64->runtime;
520                 tmp |= systab64->runtime;
521                 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
522                 tmp |= systab64->boottime;
523                 efi_systab.nr_tables = systab64->nr_tables;
524                 efi_systab.tables = systab64->tables;
525                 tmp |= systab64->tables;
526
527                 early_iounmap(systab64, sizeof(*systab64));
528 #ifdef CONFIG_X86_32
529                 if (tmp >> 32) {
530                         pr_err("EFI data located above 4GB, disabling EFI.\n");
531                         return -EINVAL;
532                 }
533 #endif
534         } else {
535                 efi_system_table_32_t *systab32;
536
537                 systab32 = early_ioremap((unsigned long)phys,
538                                          sizeof(*systab32));
539                 if (systab32 == NULL) {
540                         pr_err("Couldn't map the system table!\n");
541                         return -ENOMEM;
542                 }
543
544                 efi_systab.hdr = systab32->hdr;
545                 efi_systab.fw_vendor = systab32->fw_vendor;
546                 efi_systab.fw_revision = systab32->fw_revision;
547                 efi_systab.con_in_handle = systab32->con_in_handle;
548                 efi_systab.con_in = systab32->con_in;
549                 efi_systab.con_out_handle = systab32->con_out_handle;
550                 efi_systab.con_out = systab32->con_out;
551                 efi_systab.stderr_handle = systab32->stderr_handle;
552                 efi_systab.stderr = systab32->stderr;
553                 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
554                 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
555                 efi_systab.nr_tables = systab32->nr_tables;
556                 efi_systab.tables = systab32->tables;
557
558                 early_iounmap(systab32, sizeof(*systab32));
559         }
560
561         efi.systab = &efi_systab;
562
563         /*
564          * Verify the EFI Table
565          */
566         if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
567                 pr_err("System table signature incorrect!\n");
568                 return -EINVAL;
569         }
570         if ((efi.systab->hdr.revision >> 16) == 0)
571                 pr_err("Warning: System table version "
572                        "%d.%02d, expected 1.00 or greater!\n",
573                        efi.systab->hdr.revision >> 16,
574                        efi.systab->hdr.revision & 0xffff);
575
576         return 0;
577 }
578
579 static int __init efi_runtime_init(void)
580 {
581         efi_runtime_services_t *runtime;
582
583         /*
584          * Check out the runtime services table. We need to map
585          * the runtime services table so that we can grab the physical
586          * address of several of the EFI runtime functions, needed to
587          * set the firmware into virtual mode.
588          */
589         runtime = early_ioremap((unsigned long)efi.systab->runtime,
590                                 sizeof(efi_runtime_services_t));
591         if (!runtime) {
592                 pr_err("Could not map the runtime service table!\n");
593                 return -ENOMEM;
594         }
595         /*
596          * We will only need *early* access to the following
597          * two EFI runtime services before set_virtual_address_map
598          * is invoked.
599          */
600         efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
601         efi_phys.set_virtual_address_map =
602                 (efi_set_virtual_address_map_t *)
603                 runtime->set_virtual_address_map;
604         /*
605          * Make efi_get_time can be called before entering
606          * virtual mode.
607          */
608         efi.get_time = phys_efi_get_time;
609         early_iounmap(runtime, sizeof(efi_runtime_services_t));
610
611         return 0;
612 }
613
614 static int __init efi_memmap_init(void)
615 {
616         /* Map the EFI memory map */
617         memmap.map = early_ioremap((unsigned long)memmap.phys_map,
618                                    memmap.nr_map * memmap.desc_size);
619         if (memmap.map == NULL) {
620                 pr_err("Could not map the memory map!\n");
621                 return -ENOMEM;
622         }
623         memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
624
625         if (add_efi_memmap)
626                 do_add_efi_memmap();
627
628         return 0;
629 }
630
631 void __init efi_init(void)
632 {
633         efi_char16_t *c16;
634         char vendor[100] = "unknown";
635         int i = 0;
636         void *tmp;
637
638 #ifdef CONFIG_X86_32
639         if (boot_params.efi_info.efi_systab_hi ||
640             boot_params.efi_info.efi_memmap_hi) {
641                 pr_info("Table located above 4GB, disabling EFI.\n");
642                 return;
643         }
644         efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
645 #else
646         efi_phys.systab = (efi_system_table_t *)
647                           (boot_params.efi_info.efi_systab |
648                           ((__u64)boot_params.efi_info.efi_systab_hi<<32));
649 #endif
650
651         if (efi_systab_init(efi_phys.systab))
652                 return;
653
654         set_bit(EFI_SYSTEM_TABLES, &x86_efi_facility);
655
656         /*
657          * Show what we know for posterity
658          */
659         c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
660         if (c16) {
661                 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
662                         vendor[i] = *c16++;
663                 vendor[i] = '\0';
664         } else
665                 pr_err("Could not map the firmware vendor!\n");
666         early_iounmap(tmp, 2);
667
668         pr_info("EFI v%u.%.02u by %s\n",
669                 efi.systab->hdr.revision >> 16,
670                 efi.systab->hdr.revision & 0xffff, vendor);
671
672         if (efi_config_init(arch_tables))
673                 return;
674
675         set_bit(EFI_CONFIG_TABLES, &x86_efi_facility);
676
677         /*
678          * Note: We currently don't support runtime services on an EFI
679          * that doesn't match the kernel 32/64-bit mode.
680          */
681
682         if (!efi_is_native())
683                 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
684         else {
685                 if (disable_runtime || efi_runtime_init())
686                         return;
687                 set_bit(EFI_RUNTIME_SERVICES, &x86_efi_facility);
688         }
689
690         if (efi_memmap_init())
691                 return;
692
693         set_bit(EFI_MEMMAP, &x86_efi_facility);
694
695 #ifdef CONFIG_X86_32
696         if (efi_is_native()) {
697                 x86_platform.get_wallclock = efi_get_time;
698                 x86_platform.set_wallclock = efi_set_rtc_mmss;
699         }
700 #endif
701         print_efi_memmap();
702 }
703
704 void __init efi_late_init(void)
705 {
706         efi_bgrt_init();
707 }
708
709 void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
710 {
711         u64 addr, npages;
712
713         addr = md->virt_addr;
714         npages = md->num_pages;
715
716         memrange_efi_to_native(&addr, &npages);
717
718         if (executable)
719                 set_memory_x(addr, npages);
720         else
721                 set_memory_nx(addr, npages);
722 }
723
724 static void __init runtime_code_page_mkexec(void)
725 {
726         efi_memory_desc_t *md;
727         void *p;
728
729         /* Make EFI runtime service code area executable */
730         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
731                 md = p;
732
733                 if (md->type != EFI_RUNTIME_SERVICES_CODE)
734                         continue;
735
736                 efi_set_executable(md, true);
737         }
738 }
739
740 void efi_memory_uc(u64 addr, unsigned long size)
741 {
742         unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
743         u64 npages;
744
745         npages = round_up(size, page_shift) / page_shift;
746         memrange_efi_to_native(&addr, &npages);
747         set_memory_uc(addr, npages);
748 }
749
750 void __init old_map_region(efi_memory_desc_t *md)
751 {
752         u64 start_pfn, end_pfn, end;
753         unsigned long size;
754         void *va;
755
756         start_pfn = PFN_DOWN(md->phys_addr);
757         size      = md->num_pages << PAGE_SHIFT;
758         end       = md->phys_addr + size;
759         end_pfn   = PFN_UP(end);
760
761         if (pfn_range_is_mapped(start_pfn, end_pfn)) {
762                 va = __va(md->phys_addr);
763
764                 if (!(md->attribute & EFI_MEMORY_WB))
765                         efi_memory_uc((u64)(unsigned long)va, size);
766         } else
767                 va = efi_ioremap(md->phys_addr, size,
768                                  md->type, md->attribute);
769
770         md->virt_addr = (u64) (unsigned long) va;
771         if (!va)
772                 pr_err("ioremap of 0x%llX failed!\n",
773                        (unsigned long long)md->phys_addr);
774 }
775
776 /*
777  * This function will switch the EFI runtime services to virtual mode.
778  * Essentially, we look through the EFI memmap and map every region that
779  * has the runtime attribute bit set in its memory descriptor into the
780  * ->trampoline_pgd page table using a top-down VA allocation scheme.
781  *
782  * The old method which used to update that memory descriptor with the
783  * virtual address obtained from ioremap() is still supported when the
784  * kernel is booted with efi=old_map on its command line. Same old
785  * method enabled the runtime services to be called without having to
786  * thunk back into physical mode for every invocation.
787  *
788  * The new method does a pagetable switch in a preemption-safe manner
789  * so that we're in a different address space when calling a runtime
790  * function. For function arguments passing we do copy the PGDs of the
791  * kernel page table into ->trampoline_pgd prior to each call.
792  */
793 void __init efi_enter_virtual_mode(void)
794 {
795         efi_memory_desc_t *md, *prev_md = NULL;
796         void *p, *new_memmap = NULL;
797         unsigned long size;
798         efi_status_t status;
799         u64 end, systab;
800         int count = 0;
801
802         efi.systab = NULL;
803
804         /*
805          * We don't do virtual mode, since we don't do runtime services, on
806          * non-native EFI
807          */
808         if (!efi_is_native()) {
809                 efi_unmap_memmap();
810                 return;
811         }
812
813         /* Merge contiguous regions of the same type and attribute */
814         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
815                 u64 prev_size;
816                 md = p;
817
818                 if (!prev_md) {
819                         prev_md = md;
820                         continue;
821                 }
822
823                 if (prev_md->type != md->type ||
824                     prev_md->attribute != md->attribute) {
825                         prev_md = md;
826                         continue;
827                 }
828
829                 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
830
831                 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
832                         prev_md->num_pages += md->num_pages;
833                         md->type = EFI_RESERVED_TYPE;
834                         md->attribute = 0;
835                         continue;
836                 }
837                 prev_md = md;
838
839         }
840
841         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
842                 md = p;
843                 if (!(md->attribute & EFI_MEMORY_RUNTIME)) {
844 #ifdef CONFIG_X86_64
845                         if (md->type != EFI_BOOT_SERVICES_CODE &&
846                             md->type != EFI_BOOT_SERVICES_DATA)
847 #endif
848                                 continue;
849                 }
850
851                 efi_map_region(md);
852
853                 size = md->num_pages << EFI_PAGE_SHIFT;
854                 end = md->phys_addr + size;
855
856                 systab = (u64) (unsigned long) efi_phys.systab;
857                 if (md->phys_addr <= systab && systab < end) {
858                         systab += md->virt_addr - md->phys_addr;
859
860                         efi.systab = (efi_system_table_t *) (unsigned long) systab;
861                 }
862
863                 new_memmap = krealloc(new_memmap,
864                                       (count + 1) * memmap.desc_size,
865                                       GFP_KERNEL);
866                 if (!new_memmap)
867                         goto err_out;
868
869                 memcpy(new_memmap + (count * memmap.desc_size), md,
870                        memmap.desc_size);
871                 count++;
872         }
873
874         BUG_ON(!efi.systab);
875
876         efi_setup_page_tables();
877         efi_sync_low_kernel_mappings();
878
879         status = phys_efi_set_virtual_address_map(
880                 memmap.desc_size * count,
881                 memmap.desc_size,
882                 memmap.desc_version,
883                 (efi_memory_desc_t *)__pa(new_memmap));
884
885         if (status != EFI_SUCCESS) {
886                 pr_alert("Unable to switch EFI into virtual mode "
887                          "(status=%lx)!\n", status);
888                 panic("EFI call to SetVirtualAddressMap() failed!");
889         }
890
891         /*
892          * Now that EFI is in virtual mode, update the function
893          * pointers in the runtime service table to the new virtual addresses.
894          *
895          * Call EFI services through wrapper functions.
896          */
897         efi.runtime_version = efi_systab.hdr.revision;
898         efi.get_time = virt_efi_get_time;
899         efi.set_time = virt_efi_set_time;
900         efi.get_wakeup_time = virt_efi_get_wakeup_time;
901         efi.set_wakeup_time = virt_efi_set_wakeup_time;
902         efi.get_variable = virt_efi_get_variable;
903         efi.get_next_variable = virt_efi_get_next_variable;
904         efi.set_variable = virt_efi_set_variable;
905         efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
906         efi.reset_system = virt_efi_reset_system;
907         efi.set_virtual_address_map = NULL;
908         efi.query_variable_info = virt_efi_query_variable_info;
909         efi.update_capsule = virt_efi_update_capsule;
910         efi.query_capsule_caps = virt_efi_query_capsule_caps;
911
912         if (efi_enabled(EFI_OLD_MEMMAP) && (__supported_pte_mask & _PAGE_NX))
913                 runtime_code_page_mkexec();
914
915         kfree(new_memmap);
916
917         /* clean DUMMY object */
918         efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
919                          EFI_VARIABLE_NON_VOLATILE |
920                          EFI_VARIABLE_BOOTSERVICE_ACCESS |
921                          EFI_VARIABLE_RUNTIME_ACCESS,
922                          0, NULL);
923
924         return;
925
926  err_out:
927         pr_err("Error reallocating memory, EFI runtime non-functional!\n");
928 }
929
930 /*
931  * Convenience functions to obtain memory types and attributes
932  */
933 u32 efi_mem_type(unsigned long phys_addr)
934 {
935         efi_memory_desc_t *md;
936         void *p;
937
938         if (!efi_enabled(EFI_MEMMAP))
939                 return 0;
940
941         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
942                 md = p;
943                 if ((md->phys_addr <= phys_addr) &&
944                     (phys_addr < (md->phys_addr +
945                                   (md->num_pages << EFI_PAGE_SHIFT))))
946                         return md->type;
947         }
948         return 0;
949 }
950
951 u64 efi_mem_attributes(unsigned long phys_addr)
952 {
953         efi_memory_desc_t *md;
954         void *p;
955
956         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
957                 md = p;
958                 if ((md->phys_addr <= phys_addr) &&
959                     (phys_addr < (md->phys_addr +
960                                   (md->num_pages << EFI_PAGE_SHIFT))))
961                         return md->attribute;
962         }
963         return 0;
964 }
965
966 /*
967  * Some firmware has serious problems when using more than 50% of the EFI
968  * variable store, i.e. it triggers bugs that can brick machines. Ensure that
969  * we never use more than this safe limit.
970  *
971  * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
972  * store.
973  */
974 efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
975 {
976         efi_status_t status;
977         u64 storage_size, remaining_size, max_size;
978
979         if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
980                 return 0;
981
982         status = efi.query_variable_info(attributes, &storage_size,
983                                          &remaining_size, &max_size);
984         if (status != EFI_SUCCESS)
985                 return status;
986
987         /*
988          * Some firmware implementations refuse to boot if there's insufficient
989          * space in the variable store. We account for that by refusing the
990          * write if permitting it would reduce the available space to under
991          * 5KB. This figure was provided by Samsung, so should be safe.
992          */
993         if ((remaining_size - size < EFI_MIN_RESERVE) &&
994                 !efi_no_storage_paranoia) {
995
996                 /*
997                  * Triggering garbage collection may require that the firmware
998                  * generate a real EFI_OUT_OF_RESOURCES error. We can force
999                  * that by attempting to use more space than is available.
1000                  */
1001                 unsigned long dummy_size = remaining_size + 1024;
1002                 void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
1003
1004                 if (!dummy)
1005                         return EFI_OUT_OF_RESOURCES;
1006
1007                 status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1008                                           EFI_VARIABLE_NON_VOLATILE |
1009                                           EFI_VARIABLE_BOOTSERVICE_ACCESS |
1010                                           EFI_VARIABLE_RUNTIME_ACCESS,
1011                                           dummy_size, dummy);
1012
1013                 if (status == EFI_SUCCESS) {
1014                         /*
1015                          * This should have failed, so if it didn't make sure
1016                          * that we delete it...
1017                          */
1018                         efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1019                                          EFI_VARIABLE_NON_VOLATILE |
1020                                          EFI_VARIABLE_BOOTSERVICE_ACCESS |
1021                                          EFI_VARIABLE_RUNTIME_ACCESS,
1022                                          0, dummy);
1023                 }
1024
1025                 kfree(dummy);
1026
1027                 /*
1028                  * The runtime code may now have triggered a garbage collection
1029                  * run, so check the variable info again
1030                  */
1031                 status = efi.query_variable_info(attributes, &storage_size,
1032                                                  &remaining_size, &max_size);
1033
1034                 if (status != EFI_SUCCESS)
1035                         return status;
1036
1037                 /*
1038                  * There still isn't enough room, so return an error
1039                  */
1040                 if (remaining_size - size < EFI_MIN_RESERVE)
1041                         return EFI_OUT_OF_RESOURCES;
1042         }
1043
1044         return EFI_SUCCESS;
1045 }
1046 EXPORT_SYMBOL_GPL(efi_query_variable_store);
1047
1048 static int __init parse_efi_cmdline(char *str)
1049 {
1050         if (*str == '=')
1051                 str++;
1052
1053         if (!strncmp(str, "old_map", 7))
1054                 set_bit(EFI_OLD_MEMMAP, &x86_efi_facility);
1055
1056         return 0;
1057 }
1058 early_param("efi", parse_efi_cmdline);