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