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