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