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