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