43e7cf6c6111ab7ad7a4b02cc9daf385106643a8
[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_virt2(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_virt1(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_virt3(get_wakeup_time,
138                                 enabled, pending, tm);
139         spin_unlock_irqrestore(&rtc_lock, flags);
140         return status;
141 }
142
143 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
144 {
145         unsigned long flags;
146         efi_status_t status;
147
148         spin_lock_irqsave(&rtc_lock, flags);
149         status = efi_call_virt2(set_wakeup_time,
150                                 enabled, tm);
151         spin_unlock_irqrestore(&rtc_lock, flags);
152         return status;
153 }
154
155 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
156                                           efi_guid_t *vendor,
157                                           u32 *attr,
158                                           unsigned long *data_size,
159                                           void *data)
160 {
161         return efi_call_virt5(get_variable,
162                               name, vendor, attr,
163                               data_size, data);
164 }
165
166 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
167                                                efi_char16_t *name,
168                                                efi_guid_t *vendor)
169 {
170         return efi_call_virt3(get_next_variable,
171                               name_size, name, vendor);
172 }
173
174 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
175                                           efi_guid_t *vendor,
176                                           u32 attr,
177                                           unsigned long data_size,
178                                           void *data)
179 {
180         return efi_call_virt5(set_variable,
181                               name, vendor, attr,
182                               data_size, data);
183 }
184
185 static efi_status_t virt_efi_query_variable_info(u32 attr,
186                                                  u64 *storage_space,
187                                                  u64 *remaining_space,
188                                                  u64 *max_variable_size)
189 {
190         if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
191                 return EFI_UNSUPPORTED;
192
193         return efi_call_virt4(query_variable_info, attr, storage_space,
194                               remaining_space, max_variable_size);
195 }
196
197 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
198 {
199         return efi_call_virt1(get_next_high_mono_count, count);
200 }
201
202 static void virt_efi_reset_system(int reset_type,
203                                   efi_status_t status,
204                                   unsigned long data_size,
205                                   efi_char16_t *data)
206 {
207         efi_call_virt4(reset_system, reset_type, status,
208                        data_size, data);
209 }
210
211 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
212                                             unsigned long count,
213                                             unsigned long sg_list)
214 {
215         if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
216                 return EFI_UNSUPPORTED;
217
218         return efi_call_virt3(update_capsule, capsules, count, sg_list);
219 }
220
221 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
222                                                 unsigned long count,
223                                                 u64 *max_size,
224                                                 int *reset_type)
225 {
226         if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
227                 return EFI_UNSUPPORTED;
228
229         return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
230                               reset_type);
231 }
232
233 static efi_status_t __init phys_efi_set_virtual_address_map(
234         unsigned long memory_map_size,
235         unsigned long descriptor_size,
236         u32 descriptor_version,
237         efi_memory_desc_t *virtual_map)
238 {
239         efi_status_t status;
240
241         efi_call_phys_prelog();
242         status = efi_call_phys4(efi_phys.set_virtual_address_map,
243                                 memory_map_size, descriptor_size,
244                                 descriptor_version, virtual_map);
245         efi_call_phys_epilog();
246         return status;
247 }
248
249 static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
250                                              efi_time_cap_t *tc)
251 {
252         unsigned long flags;
253         efi_status_t status;
254
255         spin_lock_irqsave(&rtc_lock, flags);
256         efi_call_phys_prelog();
257         status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm),
258                                 virt_to_phys(tc));
259         efi_call_phys_epilog();
260         spin_unlock_irqrestore(&rtc_lock, flags);
261         return status;
262 }
263
264 int efi_set_rtc_mmss(const struct timespec *now)
265 {
266         unsigned long nowtime = now->tv_sec;
267         efi_status_t    status;
268         efi_time_t      eft;
269         efi_time_cap_t  cap;
270         struct rtc_time tm;
271
272         status = efi.get_time(&eft, &cap);
273         if (status != EFI_SUCCESS) {
274                 pr_err("Oops: efitime: can't read time!\n");
275                 return -1;
276         }
277
278         rtc_time_to_tm(nowtime, &tm);
279         if (!rtc_valid_tm(&tm)) {
280                 eft.year = tm.tm_year + 1900;
281                 eft.month = tm.tm_mon + 1;
282                 eft.day = tm.tm_mday;
283                 eft.minute = tm.tm_min;
284                 eft.second = tm.tm_sec;
285                 eft.nanosecond = 0;
286         } else {
287                 pr_err("%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
288                        __func__, 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, range=[0x%016llx-0x%016llx) (%lluMB)\n",
405                         i, md->type, md->attribute, md->phys_addr,
406                         md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
407                         (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
408         }
409 #endif  /*  EFI_DEBUG  */
410 }
411
412 void __init efi_reserve_boot_services(void)
413 {
414         void *p;
415
416         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
417                 efi_memory_desc_t *md = p;
418                 u64 start = md->phys_addr;
419                 u64 size = md->num_pages << EFI_PAGE_SHIFT;
420
421                 if (md->type != EFI_BOOT_SERVICES_CODE &&
422                     md->type != EFI_BOOT_SERVICES_DATA)
423                         continue;
424                 /* Only reserve where possible:
425                  * - Not within any already allocated areas
426                  * - Not over any memory area (really needed, if above?)
427                  * - Not within any part of the kernel
428                  * - Not the bios reserved area
429                 */
430                 if ((start + size > __pa_symbol(_text)
431                                 && start <= __pa_symbol(_end)) ||
432                         !e820_all_mapped(start, start+size, E820_RAM) ||
433                         memblock_is_region_reserved(start, size)) {
434                         /* Could not reserve, skip it */
435                         md->num_pages = 0;
436                         memblock_dbg("Could not reserve boot range [0x%010llx-0x%010llx]\n",
437                                      start, start+size-1);
438                 } else
439                         memblock_reserve(start, size);
440         }
441 }
442
443 void __init efi_unmap_memmap(void)
444 {
445         clear_bit(EFI_MEMMAP, &efi.flags);
446         if (memmap.map) {
447                 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
448                 memmap.map = NULL;
449         }
450 }
451
452 void __init efi_free_boot_services(void)
453 {
454         void *p;
455
456         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
457                 efi_memory_desc_t *md = p;
458                 unsigned long long start = md->phys_addr;
459                 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
460
461                 if (md->type != EFI_BOOT_SERVICES_CODE &&
462                     md->type != EFI_BOOT_SERVICES_DATA)
463                         continue;
464
465                 /* Could not reserve boot area */
466                 if (!size)
467                         continue;
468
469                 free_bootmem_late(start, size);
470         }
471
472         efi_unmap_memmap();
473 }
474
475 static int __init efi_systab_init(void *phys)
476 {
477         if (efi_enabled(EFI_64BIT)) {
478                 efi_system_table_64_t *systab64;
479                 struct efi_setup_data *data = NULL;
480                 u64 tmp = 0;
481
482                 if (efi_setup) {
483                         data = early_memremap(efi_setup, sizeof(*data));
484                         if (!data)
485                                 return -ENOMEM;
486                 }
487                 systab64 = early_ioremap((unsigned long)phys,
488                                          sizeof(*systab64));
489                 if (systab64 == NULL) {
490                         pr_err("Couldn't map the system table!\n");
491                         if (data)
492                                 early_iounmap(data, sizeof(*data));
493                         return -ENOMEM;
494                 }
495
496                 efi_systab.hdr = systab64->hdr;
497                 efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor :
498                                               systab64->fw_vendor;
499                 tmp |= data ? data->fw_vendor : systab64->fw_vendor;
500                 efi_systab.fw_revision = systab64->fw_revision;
501                 efi_systab.con_in_handle = systab64->con_in_handle;
502                 tmp |= systab64->con_in_handle;
503                 efi_systab.con_in = systab64->con_in;
504                 tmp |= systab64->con_in;
505                 efi_systab.con_out_handle = systab64->con_out_handle;
506                 tmp |= systab64->con_out_handle;
507                 efi_systab.con_out = systab64->con_out;
508                 tmp |= systab64->con_out;
509                 efi_systab.stderr_handle = systab64->stderr_handle;
510                 tmp |= systab64->stderr_handle;
511                 efi_systab.stderr = systab64->stderr;
512                 tmp |= systab64->stderr;
513                 efi_systab.runtime = data ?
514                                      (void *)(unsigned long)data->runtime :
515                                      (void *)(unsigned long)systab64->runtime;
516                 tmp |= data ? data->runtime : systab64->runtime;
517                 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
518                 tmp |= systab64->boottime;
519                 efi_systab.nr_tables = systab64->nr_tables;
520                 efi_systab.tables = data ? (unsigned long)data->tables :
521                                            systab64->tables;
522                 tmp |= data ? data->tables : systab64->tables;
523
524                 early_iounmap(systab64, sizeof(*systab64));
525                 if (data)
526                         early_iounmap(data, sizeof(*data));
527 #ifdef CONFIG_X86_32
528                 if (tmp >> 32) {
529                         pr_err("EFI data located above 4GB, disabling EFI.\n");
530                         return -EINVAL;
531                 }
532 #endif
533         } else {
534                 efi_system_table_32_t *systab32;
535
536                 systab32 = early_ioremap((unsigned long)phys,
537                                          sizeof(*systab32));
538                 if (systab32 == NULL) {
539                         pr_err("Couldn't map the system table!\n");
540                         return -ENOMEM;
541                 }
542
543                 efi_systab.hdr = systab32->hdr;
544                 efi_systab.fw_vendor = systab32->fw_vendor;
545                 efi_systab.fw_revision = systab32->fw_revision;
546                 efi_systab.con_in_handle = systab32->con_in_handle;
547                 efi_systab.con_in = systab32->con_in;
548                 efi_systab.con_out_handle = systab32->con_out_handle;
549                 efi_systab.con_out = systab32->con_out;
550                 efi_systab.stderr_handle = systab32->stderr_handle;
551                 efi_systab.stderr = systab32->stderr;
552                 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
553                 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
554                 efi_systab.nr_tables = systab32->nr_tables;
555                 efi_systab.tables = systab32->tables;
556
557                 early_iounmap(systab32, sizeof(*systab32));
558         }
559
560         efi.systab = &efi_systab;
561
562         /*
563          * Verify the EFI Table
564          */
565         if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
566                 pr_err("System table signature incorrect!\n");
567                 return -EINVAL;
568         }
569         if ((efi.systab->hdr.revision >> 16) == 0)
570                 pr_err("Warning: System table version %d.%02d, expected 1.00 or greater!\n",
571                        efi.systab->hdr.revision >> 16,
572                        efi.systab->hdr.revision & 0xffff);
573
574         set_bit(EFI_SYSTEM_TABLES, &efi.flags);
575
576         return 0;
577 }
578
579 static int __init efi_runtime_init32(void)
580 {
581         efi_runtime_services_32_t *runtime;
582
583         runtime = early_ioremap((unsigned long)efi.systab->runtime,
584                         sizeof(efi_runtime_services_32_t));
585         if (!runtime) {
586                 pr_err("Could not map the runtime service table!\n");
587                 return -ENOMEM;
588         }
589
590         /*
591          * We will only need *early* access to the following two
592          * EFI runtime services before set_virtual_address_map
593          * is invoked.
594          */
595         efi_phys.get_time = (efi_get_time_t *)
596                         (unsigned long)runtime->get_time;
597         efi_phys.set_virtual_address_map =
598                         (efi_set_virtual_address_map_t *)
599                         (unsigned long)runtime->set_virtual_address_map;
600         /*
601          * Make efi_get_time can be called before entering
602          * virtual mode.
603          */
604         efi.get_time = phys_efi_get_time;
605         early_iounmap(runtime, sizeof(efi_runtime_services_32_t));
606
607         return 0;
608 }
609
610 static int __init efi_runtime_init64(void)
611 {
612         efi_runtime_services_64_t *runtime;
613
614         runtime = early_ioremap((unsigned long)efi.systab->runtime,
615                         sizeof(efi_runtime_services_64_t));
616         if (!runtime) {
617                 pr_err("Could not map the runtime service table!\n");
618                 return -ENOMEM;
619         }
620
621         /*
622          * We will only need *early* access to the following two
623          * EFI runtime services before set_virtual_address_map
624          * is invoked.
625          */
626         efi_phys.get_time = (efi_get_time_t *)
627                         (unsigned long)runtime->get_time;
628         efi_phys.set_virtual_address_map =
629                         (efi_set_virtual_address_map_t *)
630                         (unsigned long)runtime->set_virtual_address_map;
631         /*
632          * Make efi_get_time can be called before entering
633          * virtual mode.
634          */
635         efi.get_time = phys_efi_get_time;
636         early_iounmap(runtime, sizeof(efi_runtime_services_64_t));
637
638         return 0;
639 }
640
641 static int __init efi_runtime_init(void)
642 {
643         int rv;
644
645         /*
646          * Check out the runtime services table. We need to map
647          * the runtime services table so that we can grab the physical
648          * address of several of the EFI runtime functions, needed to
649          * set the firmware into virtual mode.
650          */
651         if (efi_enabled(EFI_64BIT))
652                 rv = efi_runtime_init64();
653         else
654                 rv = efi_runtime_init32();
655
656         if (rv)
657                 return rv;
658
659         set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
660
661         return 0;
662 }
663
664 static int __init efi_memmap_init(void)
665 {
666         /* Map the EFI memory map */
667         memmap.map = early_ioremap((unsigned long)memmap.phys_map,
668                                    memmap.nr_map * memmap.desc_size);
669         if (memmap.map == NULL) {
670                 pr_err("Could not map the memory map!\n");
671                 return -ENOMEM;
672         }
673         memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
674
675         if (add_efi_memmap)
676                 do_add_efi_memmap();
677
678         set_bit(EFI_MEMMAP, &efi.flags);
679
680         return 0;
681 }
682
683 /*
684  * A number of config table entries get remapped to virtual addresses
685  * after entering EFI virtual mode. However, the kexec kernel requires
686  * their physical addresses therefore we pass them via setup_data and
687  * correct those entries to their respective physical addresses here.
688  *
689  * Currently only handles smbios which is necessary for some firmware
690  * implementation.
691  */
692 static int __init efi_reuse_config(u64 tables, int nr_tables)
693 {
694         int i, sz, ret = 0;
695         void *p, *tablep;
696         struct efi_setup_data *data;
697
698         if (!efi_setup)
699                 return 0;
700
701         if (!efi_enabled(EFI_64BIT))
702                 return 0;
703
704         data = early_memremap(efi_setup, sizeof(*data));
705         if (!data) {
706                 ret = -ENOMEM;
707                 goto out;
708         }
709
710         if (!data->smbios)
711                 goto out_memremap;
712
713         sz = sizeof(efi_config_table_64_t);
714
715         p = tablep = early_memremap(tables, nr_tables * sz);
716         if (!p) {
717                 pr_err("Could not map Configuration table!\n");
718                 ret = -ENOMEM;
719                 goto out_memremap;
720         }
721
722         for (i = 0; i < efi.systab->nr_tables; i++) {
723                 efi_guid_t guid;
724
725                 guid = ((efi_config_table_64_t *)p)->guid;
726
727                 if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
728                         ((efi_config_table_64_t *)p)->table = data->smbios;
729                 p += sz;
730         }
731         early_iounmap(tablep, nr_tables * sz);
732
733 out_memremap:
734         early_iounmap(data, sizeof(*data));
735 out:
736         return ret;
737 }
738
739 void __init efi_init(void)
740 {
741         efi_char16_t *c16;
742         char vendor[100] = "unknown";
743         int i = 0;
744         void *tmp;
745
746 #ifdef CONFIG_X86_32
747         if (boot_params.efi_info.efi_systab_hi ||
748             boot_params.efi_info.efi_memmap_hi) {
749                 pr_info("Table located above 4GB, disabling EFI.\n");
750                 return;
751         }
752         efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
753 #else
754         efi_phys.systab = (efi_system_table_t *)
755                           (boot_params.efi_info.efi_systab |
756                           ((__u64)boot_params.efi_info.efi_systab_hi<<32));
757 #endif
758
759         if (efi_systab_init(efi_phys.systab))
760                 return;
761
762         set_bit(EFI_SYSTEM_TABLES, &efi.flags);
763
764         efi.config_table = (unsigned long)efi.systab->tables;
765         efi.fw_vendor    = (unsigned long)efi.systab->fw_vendor;
766         efi.runtime      = (unsigned long)efi.systab->runtime;
767
768         /*
769          * Show what we know for posterity
770          */
771         c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
772         if (c16) {
773                 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
774                         vendor[i] = *c16++;
775                 vendor[i] = '\0';
776         } else
777                 pr_err("Could not map the firmware vendor!\n");
778         early_iounmap(tmp, 2);
779
780         pr_info("EFI v%u.%.02u by %s\n",
781                 efi.systab->hdr.revision >> 16,
782                 efi.systab->hdr.revision & 0xffff, vendor);
783
784         if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
785                 return;
786
787         if (efi_config_init(arch_tables))
788                 return;
789
790         /*
791          * Note: We currently don't support runtime services on an EFI
792          * that doesn't match the kernel 32/64-bit mode.
793          */
794
795         if (!efi_runtime_supported())
796                 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
797         else {
798                 if (disable_runtime || efi_runtime_init())
799                         return;
800         }
801         if (efi_memmap_init())
802                 return;
803
804         set_bit(EFI_MEMMAP, &efi.flags);
805
806         print_efi_memmap();
807 }
808
809 void __init efi_late_init(void)
810 {
811         efi_bgrt_init();
812 }
813
814 void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
815 {
816         u64 addr, npages;
817
818         addr = md->virt_addr;
819         npages = md->num_pages;
820
821         memrange_efi_to_native(&addr, &npages);
822
823         if (executable)
824                 set_memory_x(addr, npages);
825         else
826                 set_memory_nx(addr, npages);
827 }
828
829 void __init runtime_code_page_mkexec(void)
830 {
831         efi_memory_desc_t *md;
832         void *p;
833
834         /* Make EFI runtime service code area executable */
835         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
836                 md = p;
837
838                 if (md->type != EFI_RUNTIME_SERVICES_CODE)
839                         continue;
840
841                 efi_set_executable(md, true);
842         }
843 }
844
845 void efi_memory_uc(u64 addr, unsigned long size)
846 {
847         unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
848         u64 npages;
849
850         npages = round_up(size, page_shift) / page_shift;
851         memrange_efi_to_native(&addr, &npages);
852         set_memory_uc(addr, npages);
853 }
854
855 void __init old_map_region(efi_memory_desc_t *md)
856 {
857         u64 start_pfn, end_pfn, end;
858         unsigned long size;
859         void *va;
860
861         start_pfn = PFN_DOWN(md->phys_addr);
862         size      = md->num_pages << PAGE_SHIFT;
863         end       = md->phys_addr + size;
864         end_pfn   = PFN_UP(end);
865
866         if (pfn_range_is_mapped(start_pfn, end_pfn)) {
867                 va = __va(md->phys_addr);
868
869                 if (!(md->attribute & EFI_MEMORY_WB))
870                         efi_memory_uc((u64)(unsigned long)va, size);
871         } else
872                 va = efi_ioremap(md->phys_addr, size,
873                                  md->type, md->attribute);
874
875         md->virt_addr = (u64) (unsigned long) va;
876         if (!va)
877                 pr_err("ioremap of 0x%llX failed!\n",
878                        (unsigned long long)md->phys_addr);
879 }
880
881 static void native_runtime_setup(void)
882 {
883         efi.get_time = virt_efi_get_time;
884         efi.set_time = virt_efi_set_time;
885         efi.get_wakeup_time = virt_efi_get_wakeup_time;
886         efi.set_wakeup_time = virt_efi_set_wakeup_time;
887         efi.get_variable = virt_efi_get_variable;
888         efi.get_next_variable = virt_efi_get_next_variable;
889         efi.set_variable = virt_efi_set_variable;
890         efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
891         efi.reset_system = virt_efi_reset_system;
892         efi.query_variable_info = virt_efi_query_variable_info;
893         efi.update_capsule = virt_efi_update_capsule;
894         efi.query_capsule_caps = virt_efi_query_capsule_caps;
895 }
896
897 /* Merge contiguous regions of the same type and attribute */
898 static void __init efi_merge_regions(void)
899 {
900         void *p;
901         efi_memory_desc_t *md, *prev_md = NULL;
902
903         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
904                 u64 prev_size;
905                 md = p;
906
907                 if (!prev_md) {
908                         prev_md = md;
909                         continue;
910                 }
911
912                 if (prev_md->type != md->type ||
913                     prev_md->attribute != md->attribute) {
914                         prev_md = md;
915                         continue;
916                 }
917
918                 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
919
920                 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
921                         prev_md->num_pages += md->num_pages;
922                         md->type = EFI_RESERVED_TYPE;
923                         md->attribute = 0;
924                         continue;
925                 }
926                 prev_md = md;
927         }
928 }
929
930 static void __init get_systab_virt_addr(efi_memory_desc_t *md)
931 {
932         unsigned long size;
933         u64 end, systab;
934
935         size = md->num_pages << EFI_PAGE_SHIFT;
936         end = md->phys_addr + size;
937         systab = (u64)(unsigned long)efi_phys.systab;
938         if (md->phys_addr <= systab && systab < end) {
939                 systab += md->virt_addr - md->phys_addr;
940                 efi.systab = (efi_system_table_t *)(unsigned long)systab;
941         }
942 }
943
944 static void __init save_runtime_map(void)
945 {
946 #ifdef CONFIG_KEXEC
947         efi_memory_desc_t *md;
948         void *tmp, *p, *q = NULL;
949         int count = 0;
950
951         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
952                 md = p;
953
954                 if (!(md->attribute & EFI_MEMORY_RUNTIME) ||
955                     (md->type == EFI_BOOT_SERVICES_CODE) ||
956                     (md->type == EFI_BOOT_SERVICES_DATA))
957                         continue;
958                 tmp = krealloc(q, (count + 1) * memmap.desc_size, GFP_KERNEL);
959                 if (!tmp)
960                         goto out;
961                 q = tmp;
962
963                 memcpy(q + count * memmap.desc_size, md, memmap.desc_size);
964                 count++;
965         }
966
967         efi_runtime_map_setup(q, count, memmap.desc_size);
968         return;
969
970 out:
971         kfree(q);
972         pr_err("Error saving runtime map, efi runtime on kexec non-functional!!\n");
973 #endif
974 }
975
976 static void *realloc_pages(void *old_memmap, int old_shift)
977 {
978         void *ret;
979
980         ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1);
981         if (!ret)
982                 goto out;
983
984         /*
985          * A first-time allocation doesn't have anything to copy.
986          */
987         if (!old_memmap)
988                 return ret;
989
990         memcpy(ret, old_memmap, PAGE_SIZE << old_shift);
991
992 out:
993         free_pages((unsigned long)old_memmap, old_shift);
994         return ret;
995 }
996
997 /*
998  * Map the efi memory ranges of the runtime services and update new_mmap with
999  * virtual addresses.
1000  */
1001 static void * __init efi_map_regions(int *count, int *pg_shift)
1002 {
1003         void *p, *new_memmap = NULL;
1004         unsigned long left = 0;
1005         efi_memory_desc_t *md;
1006
1007         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1008                 md = p;
1009                 if (!(md->attribute & EFI_MEMORY_RUNTIME)) {
1010 #ifdef CONFIG_X86_64
1011                         if (md->type != EFI_BOOT_SERVICES_CODE &&
1012                             md->type != EFI_BOOT_SERVICES_DATA)
1013 #endif
1014                                 continue;
1015                 }
1016
1017                 efi_map_region(md);
1018                 get_systab_virt_addr(md);
1019
1020                 if (left < memmap.desc_size) {
1021                         new_memmap = realloc_pages(new_memmap, *pg_shift);
1022                         if (!new_memmap)
1023                                 return NULL;
1024
1025                         left += PAGE_SIZE << *pg_shift;
1026                         (*pg_shift)++;
1027                 }
1028
1029                 memcpy(new_memmap + (*count * memmap.desc_size), md,
1030                        memmap.desc_size);
1031
1032                 left -= memmap.desc_size;
1033                 (*count)++;
1034         }
1035
1036         return new_memmap;
1037 }
1038
1039 static void __init kexec_enter_virtual_mode(void)
1040 {
1041 #ifdef CONFIG_KEXEC
1042         efi_memory_desc_t *md;
1043         void *p;
1044
1045         efi.systab = NULL;
1046
1047         /*
1048          * We don't do virtual mode, since we don't do runtime services, on
1049          * non-native EFI
1050          */
1051         if (!efi_is_native()) {
1052                 efi_unmap_memmap();
1053                 return;
1054         }
1055
1056         /*
1057         * Map efi regions which were passed via setup_data. The virt_addr is a
1058         * fixed addr which was used in first kernel of a kexec boot.
1059         */
1060         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1061                 md = p;
1062                 efi_map_region_fixed(md); /* FIXME: add error handling */
1063                 get_systab_virt_addr(md);
1064         }
1065
1066         save_runtime_map();
1067
1068         BUG_ON(!efi.systab);
1069
1070         efi_sync_low_kernel_mappings();
1071
1072         /*
1073          * Now that EFI is in virtual mode, update the function
1074          * pointers in the runtime service table to the new virtual addresses.
1075          *
1076          * Call EFI services through wrapper functions.
1077          */
1078         efi.runtime_version = efi_systab.hdr.revision;
1079
1080         native_runtime_setup();
1081
1082         efi.set_virtual_address_map = NULL;
1083
1084         if (efi_enabled(EFI_OLD_MEMMAP) && (__supported_pte_mask & _PAGE_NX))
1085                 runtime_code_page_mkexec();
1086
1087         /* clean DUMMY object */
1088         efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1089                          EFI_VARIABLE_NON_VOLATILE |
1090                          EFI_VARIABLE_BOOTSERVICE_ACCESS |
1091                          EFI_VARIABLE_RUNTIME_ACCESS,
1092                          0, NULL);
1093 #endif
1094 }
1095
1096 /*
1097  * This function will switch the EFI runtime services to virtual mode.
1098  * Essentially, we look through the EFI memmap and map every region that
1099  * has the runtime attribute bit set in its memory descriptor into the
1100  * ->trampoline_pgd page table using a top-down VA allocation scheme.
1101  *
1102  * The old method which used to update that memory descriptor with the
1103  * virtual address obtained from ioremap() is still supported when the
1104  * kernel is booted with efi=old_map on its command line. Same old
1105  * method enabled the runtime services to be called without having to
1106  * thunk back into physical mode for every invocation.
1107  *
1108  * The new method does a pagetable switch in a preemption-safe manner
1109  * so that we're in a different address space when calling a runtime
1110  * function. For function arguments passing we do copy the PGDs of the
1111  * kernel page table into ->trampoline_pgd prior to each call.
1112  *
1113  * Specially for kexec boot, efi runtime maps in previous kernel should
1114  * be passed in via setup_data. In that case runtime ranges will be mapped
1115  * to the same virtual addresses as the first kernel, see
1116  * kexec_enter_virtual_mode().
1117  */
1118 static void __init __efi_enter_virtual_mode(void)
1119 {
1120         int count = 0, pg_shift = 0;
1121         void *new_memmap = NULL;
1122         efi_status_t status;
1123
1124         efi.systab = NULL;
1125
1126         efi_merge_regions();
1127         new_memmap = efi_map_regions(&count, &pg_shift);
1128         if (!new_memmap) {
1129                 pr_err("Error reallocating memory, EFI runtime non-functional!\n");
1130                 return;
1131         }
1132
1133         save_runtime_map();
1134
1135         BUG_ON(!efi.systab);
1136
1137         if (efi_setup_page_tables(__pa(new_memmap), 1 << pg_shift))
1138                 return;
1139
1140         efi_sync_low_kernel_mappings();
1141         efi_dump_pagetable();
1142
1143         if (efi_is_native()) {
1144                 status = phys_efi_set_virtual_address_map(
1145                                 memmap.desc_size * count,
1146                                 memmap.desc_size,
1147                                 memmap.desc_version,
1148                                 (efi_memory_desc_t *)__pa(new_memmap));
1149         } else {
1150                 status = efi_thunk_set_virtual_address_map(
1151                                 efi_phys.set_virtual_address_map,
1152                                 memmap.desc_size * count,
1153                                 memmap.desc_size,
1154                                 memmap.desc_version,
1155                                 (efi_memory_desc_t *)__pa(new_memmap));
1156         }
1157
1158         if (status != EFI_SUCCESS) {
1159                 pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
1160                          status);
1161                 panic("EFI call to SetVirtualAddressMap() failed!");
1162         }
1163
1164         /*
1165          * Now that EFI is in virtual mode, update the function
1166          * pointers in the runtime service table to the new virtual addresses.
1167          *
1168          * Call EFI services through wrapper functions.
1169          */
1170         efi.runtime_version = efi_systab.hdr.revision;
1171
1172         if (efi_is_native())
1173                 native_runtime_setup();
1174         else
1175                 efi_thunk_runtime_setup();
1176
1177         efi.set_virtual_address_map = NULL;
1178
1179         efi_runtime_mkexec();
1180
1181         /*
1182          * We mapped the descriptor array into the EFI pagetable above but we're
1183          * not unmapping it here. Here's why:
1184          *
1185          * We're copying select PGDs from the kernel page table to the EFI page
1186          * table and when we do so and make changes to those PGDs like unmapping
1187          * stuff from them, those changes appear in the kernel page table and we
1188          * go boom.
1189          *
1190          * From setup_real_mode():
1191          *
1192          * ...
1193          * trampoline_pgd[0] = init_level4_pgt[pgd_index(__PAGE_OFFSET)].pgd;
1194          *
1195          * In this particular case, our allocation is in PGD 0 of the EFI page
1196          * table but we've copied that PGD from PGD[272] of the EFI page table:
1197          *
1198          *      pgd_index(__PAGE_OFFSET = 0xffff880000000000) = 272
1199          *
1200          * where the direct memory mapping in kernel space is.
1201          *
1202          * new_memmap's VA comes from that direct mapping and thus clearing it,
1203          * it would get cleared in the kernel page table too.
1204          *
1205          * efi_cleanup_page_tables(__pa(new_memmap), 1 << pg_shift);
1206          */
1207         free_pages((unsigned long)new_memmap, pg_shift);
1208
1209         /* clean DUMMY object */
1210         efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1211                          EFI_VARIABLE_NON_VOLATILE |
1212                          EFI_VARIABLE_BOOTSERVICE_ACCESS |
1213                          EFI_VARIABLE_RUNTIME_ACCESS,
1214                          0, NULL);
1215 }
1216
1217 void __init efi_enter_virtual_mode(void)
1218 {
1219         if (efi_setup)
1220                 kexec_enter_virtual_mode();
1221         else
1222                 __efi_enter_virtual_mode();
1223 }
1224
1225 /*
1226  * Convenience functions to obtain memory types and attributes
1227  */
1228 u32 efi_mem_type(unsigned long phys_addr)
1229 {
1230         efi_memory_desc_t *md;
1231         void *p;
1232
1233         if (!efi_enabled(EFI_MEMMAP))
1234                 return 0;
1235
1236         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1237                 md = p;
1238                 if ((md->phys_addr <= phys_addr) &&
1239                     (phys_addr < (md->phys_addr +
1240                                   (md->num_pages << EFI_PAGE_SHIFT))))
1241                         return md->type;
1242         }
1243         return 0;
1244 }
1245
1246 u64 efi_mem_attributes(unsigned long phys_addr)
1247 {
1248         efi_memory_desc_t *md;
1249         void *p;
1250
1251         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1252                 md = p;
1253                 if ((md->phys_addr <= phys_addr) &&
1254                     (phys_addr < (md->phys_addr +
1255                                   (md->num_pages << EFI_PAGE_SHIFT))))
1256                         return md->attribute;
1257         }
1258         return 0;
1259 }
1260
1261 /*
1262  * Some firmware implementations refuse to boot if there's insufficient space
1263  * in the variable store. Ensure that we never use more than a safe limit.
1264  *
1265  * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
1266  * store.
1267  */
1268 efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
1269 {
1270         efi_status_t status;
1271         u64 storage_size, remaining_size, max_size;
1272
1273         if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
1274                 return 0;
1275
1276         status = efi.query_variable_info(attributes, &storage_size,
1277                                          &remaining_size, &max_size);
1278         if (status != EFI_SUCCESS)
1279                 return status;
1280
1281         /*
1282          * We account for that by refusing the write if permitting it would
1283          * reduce the available space to under 5KB. This figure was provided by
1284          * Samsung, so should be safe.
1285          */
1286         if ((remaining_size - size < EFI_MIN_RESERVE) &&
1287                 !efi_no_storage_paranoia) {
1288
1289                 /*
1290                  * Triggering garbage collection may require that the firmware
1291                  * generate a real EFI_OUT_OF_RESOURCES error. We can force
1292                  * that by attempting to use more space than is available.
1293                  */
1294                 unsigned long dummy_size = remaining_size + 1024;
1295                 void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
1296
1297                 if (!dummy)
1298                         return EFI_OUT_OF_RESOURCES;
1299
1300                 status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1301                                           EFI_VARIABLE_NON_VOLATILE |
1302                                           EFI_VARIABLE_BOOTSERVICE_ACCESS |
1303                                           EFI_VARIABLE_RUNTIME_ACCESS,
1304                                           dummy_size, dummy);
1305
1306                 if (status == EFI_SUCCESS) {
1307                         /*
1308                          * This should have failed, so if it didn't make sure
1309                          * that we delete it...
1310                          */
1311                         efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1312                                          EFI_VARIABLE_NON_VOLATILE |
1313                                          EFI_VARIABLE_BOOTSERVICE_ACCESS |
1314                                          EFI_VARIABLE_RUNTIME_ACCESS,
1315                                          0, dummy);
1316                 }
1317
1318                 kfree(dummy);
1319
1320                 /*
1321                  * The runtime code may now have triggered a garbage collection
1322                  * run, so check the variable info again
1323                  */
1324                 status = efi.query_variable_info(attributes, &storage_size,
1325                                                  &remaining_size, &max_size);
1326
1327                 if (status != EFI_SUCCESS)
1328                         return status;
1329
1330                 /*
1331                  * There still isn't enough room, so return an error
1332                  */
1333                 if (remaining_size - size < EFI_MIN_RESERVE)
1334                         return EFI_OUT_OF_RESOURCES;
1335         }
1336
1337         return EFI_SUCCESS;
1338 }
1339 EXPORT_SYMBOL_GPL(efi_query_variable_store);
1340
1341 static int __init parse_efi_cmdline(char *str)
1342 {
1343         if (*str == '=')
1344                 str++;
1345
1346         if (!strncmp(str, "old_map", 7))
1347                 set_bit(EFI_OLD_MEMMAP, &efi.flags);
1348
1349         return 0;
1350 }
1351 early_param("efi", parse_efi_cmdline);
1352
1353 void __init efi_apply_memmap_quirks(void)
1354 {
1355         /*
1356          * Once setup is done earlier, unmap the EFI memory map on mismatched
1357          * firmware/kernel architectures since there is no support for runtime
1358          * services.
1359          */
1360         if (!efi_runtime_supported()) {
1361                 pr_info("efi: Setup done, disabling due to 32/64-bit mismatch\n");
1362                 efi_unmap_memmap();
1363         }
1364
1365         /*
1366          * UV doesn't support the new EFI pagetable mapping yet.
1367          */
1368         if (is_uv_system())
1369                 set_bit(EFI_OLD_MEMMAP, &efi.flags);
1370 }