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