0a34d9e9c263ed1a2074cb9d5beac2a0648dc525
[~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
45 #include <asm/setup.h>
46 #include <asm/efi.h>
47 #include <asm/time.h>
48 #include <asm/cacheflush.h>
49 #include <asm/tlbflush.h>
50 #include <asm/x86_init.h>
51
52 #define EFI_DEBUG       1
53
54 int efi_enabled;
55 EXPORT_SYMBOL(efi_enabled);
56
57 struct efi __read_mostly efi = {
58         .mps        = EFI_INVALID_TABLE_ADDR,
59         .acpi       = EFI_INVALID_TABLE_ADDR,
60         .acpi20     = EFI_INVALID_TABLE_ADDR,
61         .smbios     = EFI_INVALID_TABLE_ADDR,
62         .sal_systab = EFI_INVALID_TABLE_ADDR,
63         .boot_info  = EFI_INVALID_TABLE_ADDR,
64         .hcdp       = EFI_INVALID_TABLE_ADDR,
65         .uga        = EFI_INVALID_TABLE_ADDR,
66         .uv_systab  = EFI_INVALID_TABLE_ADDR,
67 };
68 EXPORT_SYMBOL(efi);
69
70 struct efi_memory_map memmap;
71
72 bool efi_64bit;
73
74 static struct efi efi_phys __initdata;
75 static efi_system_table_t efi_systab __initdata;
76
77 static inline bool efi_is_native(void)
78 {
79         return IS_ENABLED(CONFIG_X86_64) == efi_64bit;
80 }
81
82 static int __init setup_noefi(char *arg)
83 {
84         efi_enabled = 0;
85         return 0;
86 }
87 early_param("noefi", setup_noefi);
88
89 int add_efi_memmap;
90 EXPORT_SYMBOL(add_efi_memmap);
91
92 static int __init setup_add_efi_memmap(char *arg)
93 {
94         add_efi_memmap = 1;
95         return 0;
96 }
97 early_param("add_efi_memmap", setup_add_efi_memmap);
98
99
100 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
101 {
102         unsigned long flags;
103         efi_status_t status;
104
105         spin_lock_irqsave(&rtc_lock, flags);
106         status = efi_call_virt2(get_time, tm, tc);
107         spin_unlock_irqrestore(&rtc_lock, flags);
108         return status;
109 }
110
111 static efi_status_t virt_efi_set_time(efi_time_t *tm)
112 {
113         unsigned long flags;
114         efi_status_t status;
115
116         spin_lock_irqsave(&rtc_lock, flags);
117         status = efi_call_virt1(set_time, tm);
118         spin_unlock_irqrestore(&rtc_lock, flags);
119         return status;
120 }
121
122 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
123                                              efi_bool_t *pending,
124                                              efi_time_t *tm)
125 {
126         unsigned long flags;
127         efi_status_t status;
128
129         spin_lock_irqsave(&rtc_lock, flags);
130         status = efi_call_virt3(get_wakeup_time,
131                                 enabled, pending, tm);
132         spin_unlock_irqrestore(&rtc_lock, flags);
133         return status;
134 }
135
136 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
137 {
138         unsigned long flags;
139         efi_status_t status;
140
141         spin_lock_irqsave(&rtc_lock, flags);
142         status = efi_call_virt2(set_wakeup_time,
143                                 enabled, tm);
144         spin_unlock_irqrestore(&rtc_lock, flags);
145         return status;
146 }
147
148 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
149                                           efi_guid_t *vendor,
150                                           u32 *attr,
151                                           unsigned long *data_size,
152                                           void *data)
153 {
154         return efi_call_virt5(get_variable,
155                               name, vendor, attr,
156                               data_size, data);
157 }
158
159 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
160                                                efi_char16_t *name,
161                                                efi_guid_t *vendor)
162 {
163         return efi_call_virt3(get_next_variable,
164                               name_size, name, vendor);
165 }
166
167 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
168                                           efi_guid_t *vendor,
169                                           u32 attr,
170                                           unsigned long data_size,
171                                           void *data)
172 {
173         return efi_call_virt5(set_variable,
174                               name, vendor, attr,
175                               data_size, data);
176 }
177
178 static efi_status_t virt_efi_query_variable_info(u32 attr,
179                                                  u64 *storage_space,
180                                                  u64 *remaining_space,
181                                                  u64 *max_variable_size)
182 {
183         if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
184                 return EFI_UNSUPPORTED;
185
186         return efi_call_virt4(query_variable_info, attr, storage_space,
187                               remaining_space, max_variable_size);
188 }
189
190 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
191 {
192         return efi_call_virt1(get_next_high_mono_count, count);
193 }
194
195 static void virt_efi_reset_system(int reset_type,
196                                   efi_status_t status,
197                                   unsigned long data_size,
198                                   efi_char16_t *data)
199 {
200         efi_call_virt4(reset_system, reset_type, status,
201                        data_size, data);
202 }
203
204 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
205                                             unsigned long count,
206                                             unsigned long sg_list)
207 {
208         if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
209                 return EFI_UNSUPPORTED;
210
211         return efi_call_virt3(update_capsule, capsules, count, sg_list);
212 }
213
214 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
215                                                 unsigned long count,
216                                                 u64 *max_size,
217                                                 int *reset_type)
218 {
219         if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
220                 return EFI_UNSUPPORTED;
221
222         return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
223                               reset_type);
224 }
225
226 static efi_status_t __init phys_efi_set_virtual_address_map(
227         unsigned long memory_map_size,
228         unsigned long descriptor_size,
229         u32 descriptor_version,
230         efi_memory_desc_t *virtual_map)
231 {
232         efi_status_t status;
233
234         efi_call_phys_prelog();
235         status = efi_call_phys4(efi_phys.set_virtual_address_map,
236                                 memory_map_size, descriptor_size,
237                                 descriptor_version, virtual_map);
238         efi_call_phys_epilog();
239         return status;
240 }
241
242 static int efi_set_rtc_mmss(unsigned long nowtime)
243 {
244         int real_seconds, real_minutes;
245         efi_status_t    status;
246         efi_time_t      eft;
247         efi_time_cap_t  cap;
248
249         status = efi.get_time(&eft, &cap);
250         if (status != EFI_SUCCESS) {
251                 pr_err("Oops: efitime: can't read time!\n");
252                 return -1;
253         }
254
255         real_seconds = nowtime % 60;
256         real_minutes = nowtime / 60;
257         if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
258                 real_minutes += 30;
259         real_minutes %= 60;
260         eft.minute = real_minutes;
261         eft.second = real_seconds;
262
263         status = efi.set_time(&eft);
264         if (status != EFI_SUCCESS) {
265                 pr_err("Oops: efitime: can't write time!\n");
266                 return -1;
267         }
268         return 0;
269 }
270
271 static unsigned long efi_get_time(void)
272 {
273         efi_status_t status;
274         efi_time_t eft;
275         efi_time_cap_t cap;
276
277         status = efi.get_time(&eft, &cap);
278         if (status != EFI_SUCCESS)
279                 pr_err("Oops: efitime: can't read time!\n");
280
281         return mktime(eft.year, eft.month, eft.day, eft.hour,
282                       eft.minute, eft.second);
283 }
284
285 /*
286  * Tell the kernel about the EFI memory map.  This might include
287  * more than the max 128 entries that can fit in the e820 legacy
288  * (zeropage) memory map.
289  */
290
291 static void __init do_add_efi_memmap(void)
292 {
293         void *p;
294
295         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
296                 efi_memory_desc_t *md = p;
297                 unsigned long long start = md->phys_addr;
298                 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
299                 int e820_type;
300
301                 switch (md->type) {
302                 case EFI_LOADER_CODE:
303                 case EFI_LOADER_DATA:
304                 case EFI_BOOT_SERVICES_CODE:
305                 case EFI_BOOT_SERVICES_DATA:
306                 case EFI_CONVENTIONAL_MEMORY:
307                         if (md->attribute & EFI_MEMORY_WB)
308                                 e820_type = E820_RAM;
309                         else
310                                 e820_type = E820_RESERVED;
311                         break;
312                 case EFI_ACPI_RECLAIM_MEMORY:
313                         e820_type = E820_ACPI;
314                         break;
315                 case EFI_ACPI_MEMORY_NVS:
316                         e820_type = E820_NVS;
317                         break;
318                 case EFI_UNUSABLE_MEMORY:
319                         e820_type = E820_UNUSABLE;
320                         break;
321                 default:
322                         /*
323                          * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
324                          * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
325                          * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
326                          */
327                         e820_type = E820_RESERVED;
328                         break;
329                 }
330                 e820_add_region(start, size, e820_type);
331         }
332         sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
333 }
334
335 int __init efi_memblock_x86_reserve_range(void)
336 {
337         unsigned long pmap;
338
339 #ifdef CONFIG_X86_32
340         /* Can't handle data above 4GB at this time */
341         if (boot_params.efi_info.efi_memmap_hi) {
342                 pr_err("Memory map is above 4GB, disabling EFI.\n");
343                 return -EINVAL;
344         }
345         pmap = boot_params.efi_info.efi_memmap;
346 #else
347         pmap = (boot_params.efi_info.efi_memmap |
348                 ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
349 #endif
350         memmap.phys_map = (void *)pmap;
351         memmap.nr_map = boot_params.efi_info.efi_memmap_size /
352                 boot_params.efi_info.efi_memdesc_size;
353         memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
354         memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
355         memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
356
357         return 0;
358 }
359
360 #if EFI_DEBUG
361 static void __init print_efi_memmap(void)
362 {
363         efi_memory_desc_t *md;
364         void *p;
365         int i;
366
367         for (p = memmap.map, i = 0;
368              p < memmap.map_end;
369              p += memmap.desc_size, i++) {
370                 md = p;
371                 pr_info("mem%02u: type=%u, attr=0x%llx, "
372                         "range=[0x%016llx-0x%016llx) (%lluMB)\n",
373                         i, md->type, md->attribute, md->phys_addr,
374                         md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
375                         (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
376         }
377 }
378 #endif  /*  EFI_DEBUG  */
379
380 void __init efi_reserve_boot_services(void)
381 {
382         void *p;
383
384         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
385                 efi_memory_desc_t *md = p;
386                 u64 start = md->phys_addr;
387                 u64 size = md->num_pages << EFI_PAGE_SHIFT;
388
389                 if (md->type != EFI_BOOT_SERVICES_CODE &&
390                     md->type != EFI_BOOT_SERVICES_DATA)
391                         continue;
392                 /* Only reserve where possible:
393                  * - Not within any already allocated areas
394                  * - Not over any memory area (really needed, if above?)
395                  * - Not within any part of the kernel
396                  * - Not the bios reserved area
397                 */
398                 if ((start+size >= virt_to_phys(_text)
399                                 && start <= virt_to_phys(_end)) ||
400                         !e820_all_mapped(start, start+size, E820_RAM) ||
401                         memblock_is_region_reserved(start, size)) {
402                         /* Could not reserve, skip it */
403                         md->num_pages = 0;
404                         memblock_dbg("Could not reserve boot range "
405                                         "[0x%010llx-0x%010llx]\n",
406                                                 start, start+size-1);
407                 } else
408                         memblock_reserve(start, size);
409         }
410 }
411
412 void __init efi_unmap_memmap(void)
413 {
414         if (memmap.map) {
415                 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
416                 memmap.map = NULL;
417         }
418 }
419
420 void __init efi_free_boot_services(void)
421 {
422         void *p;
423
424         if (!efi_is_native())
425                 return;
426
427         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
428                 efi_memory_desc_t *md = p;
429                 unsigned long long start = md->phys_addr;
430                 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
431
432                 if (md->type != EFI_BOOT_SERVICES_CODE &&
433                     md->type != EFI_BOOT_SERVICES_DATA)
434                         continue;
435
436                 /* Could not reserve boot area */
437                 if (!size)
438                         continue;
439
440                 free_bootmem_late(start, size);
441         }
442
443         efi_unmap_memmap();
444 }
445
446 static int __init efi_systab_init(void *phys)
447 {
448         if (efi_64bit) {
449                 efi_system_table_64_t *systab64;
450                 u64 tmp = 0;
451
452                 systab64 = early_ioremap((unsigned long)phys,
453                                          sizeof(*systab64));
454                 if (systab64 == NULL) {
455                         pr_err("Couldn't map the system table!\n");
456                         return -ENOMEM;
457                 }
458
459                 efi_systab.hdr = systab64->hdr;
460                 efi_systab.fw_vendor = systab64->fw_vendor;
461                 tmp |= systab64->fw_vendor;
462                 efi_systab.fw_revision = systab64->fw_revision;
463                 efi_systab.con_in_handle = systab64->con_in_handle;
464                 tmp |= systab64->con_in_handle;
465                 efi_systab.con_in = systab64->con_in;
466                 tmp |= systab64->con_in;
467                 efi_systab.con_out_handle = systab64->con_out_handle;
468                 tmp |= systab64->con_out_handle;
469                 efi_systab.con_out = systab64->con_out;
470                 tmp |= systab64->con_out;
471                 efi_systab.stderr_handle = systab64->stderr_handle;
472                 tmp |= systab64->stderr_handle;
473                 efi_systab.stderr = systab64->stderr;
474                 tmp |= systab64->stderr;
475                 efi_systab.runtime = (void *)(unsigned long)systab64->runtime;
476                 tmp |= systab64->runtime;
477                 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
478                 tmp |= systab64->boottime;
479                 efi_systab.nr_tables = systab64->nr_tables;
480                 efi_systab.tables = systab64->tables;
481                 tmp |= systab64->tables;
482
483                 early_iounmap(systab64, sizeof(*systab64));
484 #ifdef CONFIG_X86_32
485                 if (tmp >> 32) {
486                         pr_err("EFI data located above 4GB, disabling EFI.\n");
487                         return -EINVAL;
488                 }
489 #endif
490         } else {
491                 efi_system_table_32_t *systab32;
492
493                 systab32 = early_ioremap((unsigned long)phys,
494                                          sizeof(*systab32));
495                 if (systab32 == NULL) {
496                         pr_err("Couldn't map the system table!\n");
497                         return -ENOMEM;
498                 }
499
500                 efi_systab.hdr = systab32->hdr;
501                 efi_systab.fw_vendor = systab32->fw_vendor;
502                 efi_systab.fw_revision = systab32->fw_revision;
503                 efi_systab.con_in_handle = systab32->con_in_handle;
504                 efi_systab.con_in = systab32->con_in;
505                 efi_systab.con_out_handle = systab32->con_out_handle;
506                 efi_systab.con_out = systab32->con_out;
507                 efi_systab.stderr_handle = systab32->stderr_handle;
508                 efi_systab.stderr = systab32->stderr;
509                 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
510                 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
511                 efi_systab.nr_tables = systab32->nr_tables;
512                 efi_systab.tables = systab32->tables;
513
514                 early_iounmap(systab32, sizeof(*systab32));
515         }
516
517         efi.systab = &efi_systab;
518
519         /*
520          * Verify the EFI Table
521          */
522         if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
523                 pr_err("System table signature incorrect!\n");
524                 return -EINVAL;
525         }
526         if ((efi.systab->hdr.revision >> 16) == 0)
527                 pr_err("Warning: System table version "
528                        "%d.%02d, expected 1.00 or greater!\n",
529                        efi.systab->hdr.revision >> 16,
530                        efi.systab->hdr.revision & 0xffff);
531
532         return 0;
533 }
534
535 static int __init efi_config_init(u64 tables, int nr_tables)
536 {
537         void *config_tables, *tablep;
538         int i, sz;
539
540         if (efi_64bit)
541                 sz = sizeof(efi_config_table_64_t);
542         else
543                 sz = sizeof(efi_config_table_32_t);
544
545         /*
546          * Let's see what config tables the firmware passed to us.
547          */
548         config_tables = early_ioremap(tables, nr_tables * sz);
549         if (config_tables == NULL) {
550                 pr_err("Could not map Configuration table!\n");
551                 return -ENOMEM;
552         }
553
554         tablep = config_tables;
555         pr_info("");
556         for (i = 0; i < efi.systab->nr_tables; i++) {
557                 efi_guid_t guid;
558                 unsigned long table;
559
560                 if (efi_64bit) {
561                         u64 table64;
562                         guid = ((efi_config_table_64_t *)tablep)->guid;
563                         table64 = ((efi_config_table_64_t *)tablep)->table;
564                         table = table64;
565 #ifdef CONFIG_X86_32
566                         if (table64 >> 32) {
567                                 pr_cont("\n");
568                                 pr_err("Table located above 4GB, disabling EFI.\n");
569                                 early_iounmap(config_tables,
570                                               efi.systab->nr_tables * sz);
571                                 return -EINVAL;
572                         }
573 #endif
574                 } else {
575                         guid = ((efi_config_table_32_t *)tablep)->guid;
576                         table = ((efi_config_table_32_t *)tablep)->table;
577                 }
578                 if (!efi_guidcmp(guid, MPS_TABLE_GUID)) {
579                         efi.mps = table;
580                         pr_cont(" MPS=0x%lx ", table);
581                 } else if (!efi_guidcmp(guid, ACPI_20_TABLE_GUID)) {
582                         efi.acpi20 = table;
583                         pr_cont(" ACPI 2.0=0x%lx ", table);
584                 } else if (!efi_guidcmp(guid, ACPI_TABLE_GUID)) {
585                         efi.acpi = table;
586                         pr_cont(" ACPI=0x%lx ", table);
587                 } else if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID)) {
588                         efi.smbios = table;
589                         pr_cont(" SMBIOS=0x%lx ", table);
590 #ifdef CONFIG_X86_UV
591                 } else if (!efi_guidcmp(guid, UV_SYSTEM_TABLE_GUID)) {
592                         efi.uv_systab = table;
593                         pr_cont(" UVsystab=0x%lx ", table);
594 #endif
595                 } else if (!efi_guidcmp(guid, HCDP_TABLE_GUID)) {
596                         efi.hcdp = table;
597                         pr_cont(" HCDP=0x%lx ", table);
598                 } else if (!efi_guidcmp(guid, UGA_IO_PROTOCOL_GUID)) {
599                         efi.uga = table;
600                         pr_cont(" UGA=0x%lx ", table);
601                 }
602                 tablep += sz;
603         }
604         pr_cont("\n");
605         early_iounmap(config_tables, efi.systab->nr_tables * sz);
606         return 0;
607 }
608
609 static int __init efi_runtime_init(void)
610 {
611         efi_runtime_services_t *runtime;
612
613         /*
614          * Check out the runtime services table. We need to map
615          * the runtime services table so that we can grab the physical
616          * address of several of the EFI runtime functions, needed to
617          * set the firmware into virtual mode.
618          */
619         runtime = early_ioremap((unsigned long)efi.systab->runtime,
620                                 sizeof(efi_runtime_services_t));
621         if (!runtime) {
622                 pr_err("Could not map the runtime service table!\n");
623                 return -ENOMEM;
624         }
625         /*
626          * We will only need *early* access to the following
627          * EFI runtime service before set_virtual_address_map
628          * is invoked.
629          */
630         efi_phys.set_virtual_address_map =
631                 (efi_set_virtual_address_map_t *)
632                 runtime->set_virtual_address_map;
633
634         early_iounmap(runtime, sizeof(efi_runtime_services_t));
635
636         return 0;
637 }
638
639 static int __init efi_memmap_init(void)
640 {
641         /* Map the EFI memory map */
642         memmap.map = early_ioremap((unsigned long)memmap.phys_map,
643                                    memmap.nr_map * memmap.desc_size);
644         if (memmap.map == NULL) {
645                 pr_err("Could not map the memory map!\n");
646                 return -ENOMEM;
647         }
648         memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
649
650         if (add_efi_memmap)
651                 do_add_efi_memmap();
652
653         return 0;
654 }
655
656 void __init efi_init(void)
657 {
658         efi_char16_t *c16;
659         char vendor[100] = "unknown";
660         int i = 0;
661         void *tmp;
662
663 #ifdef CONFIG_X86_32
664         if (boot_params.efi_info.efi_systab_hi ||
665             boot_params.efi_info.efi_memmap_hi) {
666                 pr_info("Table located above 4GB, disabling EFI.\n");
667                 efi_enabled = 0;
668                 return;
669         }
670         efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
671 #else
672         efi_phys.systab = (efi_system_table_t *)
673                           (boot_params.efi_info.efi_systab |
674                           ((__u64)boot_params.efi_info.efi_systab_hi<<32));
675 #endif
676
677         if (efi_systab_init(efi_phys.systab)) {
678                 efi_enabled = 0;
679                 return;
680         }
681
682         /*
683          * Show what we know for posterity
684          */
685         c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
686         if (c16) {
687                 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
688                         vendor[i] = *c16++;
689                 vendor[i] = '\0';
690         } else
691                 pr_err("Could not map the firmware vendor!\n");
692         early_iounmap(tmp, 2);
693
694         pr_info("EFI v%u.%.02u by %s\n",
695                 efi.systab->hdr.revision >> 16,
696                 efi.systab->hdr.revision & 0xffff, vendor);
697
698         if (efi_config_init(efi.systab->tables, efi.systab->nr_tables)) {
699                 efi_enabled = 0;
700                 return;
701         }
702
703         /*
704          * Note: We currently don't support runtime services on an EFI
705          * that doesn't match the kernel 32/64-bit mode.
706          */
707
708         if (!efi_is_native())
709                 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
710         else if (efi_runtime_init()) {
711                 efi_enabled = 0;
712                 return;
713         }
714
715         if (efi_memmap_init()) {
716                 efi_enabled = 0;
717                 return;
718         }
719         if (efi_is_native()) {
720                 x86_platform.get_wallclock = efi_get_time;
721                 x86_platform.set_wallclock = efi_set_rtc_mmss;
722         }
723
724 #if EFI_DEBUG
725         print_efi_memmap();
726 #endif
727 }
728
729 void __init efi_late_init(void)
730 {
731         efi_bgrt_init();
732 }
733
734 void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
735 {
736         u64 addr, npages;
737
738         addr = md->virt_addr;
739         npages = md->num_pages;
740
741         memrange_efi_to_native(&addr, &npages);
742
743         if (executable)
744                 set_memory_x(addr, npages);
745         else
746                 set_memory_nx(addr, npages);
747 }
748
749 static void __init runtime_code_page_mkexec(void)
750 {
751         efi_memory_desc_t *md;
752         void *p;
753
754         /* Make EFI runtime service code area executable */
755         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
756                 md = p;
757
758                 if (md->type != EFI_RUNTIME_SERVICES_CODE)
759                         continue;
760
761                 efi_set_executable(md, true);
762         }
763 }
764
765 /*
766  * We can't ioremap data in EFI boot services RAM, because we've already mapped
767  * it as RAM.  So, look it up in the existing EFI memory map instead.  Only
768  * callable after efi_enter_virtual_mode and before efi_free_boot_services.
769  */
770 void __iomem *efi_lookup_mapped_addr(u64 phys_addr)
771 {
772         void *p;
773         if (WARN_ON(!memmap.map))
774                 return NULL;
775         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
776                 efi_memory_desc_t *md = p;
777                 u64 size = md->num_pages << EFI_PAGE_SHIFT;
778                 u64 end = md->phys_addr + size;
779                 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
780                     md->type != EFI_BOOT_SERVICES_CODE &&
781                     md->type != EFI_BOOT_SERVICES_DATA)
782                         continue;
783                 if (!md->virt_addr)
784                         continue;
785                 if (phys_addr >= md->phys_addr && phys_addr < end) {
786                         phys_addr += md->virt_addr - md->phys_addr;
787                         return (__force void __iomem *)(unsigned long)phys_addr;
788                 }
789         }
790         return NULL;
791 }
792
793 void efi_memory_uc(u64 addr, unsigned long size)
794 {
795         unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
796         u64 npages;
797
798         npages = round_up(size, page_shift) / page_shift;
799         memrange_efi_to_native(&addr, &npages);
800         set_memory_uc(addr, npages);
801 }
802
803 /*
804  * This function will switch the EFI runtime services to virtual mode.
805  * Essentially, look through the EFI memmap and map every region that
806  * has the runtime attribute bit set in its memory descriptor and update
807  * that memory descriptor with the virtual address obtained from ioremap().
808  * This enables the runtime services to be called without having to
809  * thunk back into physical mode for every invocation.
810  */
811 void __init efi_enter_virtual_mode(void)
812 {
813         efi_memory_desc_t *md, *prev_md = NULL;
814         efi_status_t status;
815         unsigned long size;
816         u64 end, systab, end_pfn;
817         void *p, *va, *new_memmap = NULL;
818         int count = 0;
819
820         efi.systab = NULL;
821
822         /*
823          * We don't do virtual mode, since we don't do runtime services, on
824          * non-native EFI
825          */
826
827         if (!efi_is_native()) {
828                 efi_unmap_memmap();
829                 return;
830         }
831
832         /* Merge contiguous regions of the same type and attribute */
833         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
834                 u64 prev_size;
835                 md = p;
836
837                 if (!prev_md) {
838                         prev_md = md;
839                         continue;
840                 }
841
842                 if (prev_md->type != md->type ||
843                     prev_md->attribute != md->attribute) {
844                         prev_md = md;
845                         continue;
846                 }
847
848                 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
849
850                 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
851                         prev_md->num_pages += md->num_pages;
852                         md->type = EFI_RESERVED_TYPE;
853                         md->attribute = 0;
854                         continue;
855                 }
856                 prev_md = md;
857         }
858
859         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
860                 md = p;
861                 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
862                     md->type != EFI_BOOT_SERVICES_CODE &&
863                     md->type != EFI_BOOT_SERVICES_DATA)
864                         continue;
865
866                 size = md->num_pages << EFI_PAGE_SHIFT;
867                 end = md->phys_addr + size;
868
869                 end_pfn = PFN_UP(end);
870                 if (end_pfn <= max_low_pfn_mapped
871                     || (end_pfn > (1UL << (32 - PAGE_SHIFT))
872                         && end_pfn <= max_pfn_mapped)) {
873                         va = __va(md->phys_addr);
874
875                         if (!(md->attribute & EFI_MEMORY_WB))
876                                 efi_memory_uc((u64)(unsigned long)va, size);
877                 } else
878                         va = efi_ioremap(md->phys_addr, size,
879                                          md->type, md->attribute);
880
881                 md->virt_addr = (u64) (unsigned long) va;
882
883                 if (!va) {
884                         pr_err("ioremap of 0x%llX failed!\n",
885                                (unsigned long long)md->phys_addr);
886                         continue;
887                 }
888
889                 systab = (u64) (unsigned long) efi_phys.systab;
890                 if (md->phys_addr <= systab && systab < end) {
891                         systab += md->virt_addr - md->phys_addr;
892                         efi.systab = (efi_system_table_t *) (unsigned long) systab;
893                 }
894                 new_memmap = krealloc(new_memmap,
895                                       (count + 1) * memmap.desc_size,
896                                       GFP_KERNEL);
897                 memcpy(new_memmap + (count * memmap.desc_size), md,
898                        memmap.desc_size);
899                 count++;
900         }
901
902         BUG_ON(!efi.systab);
903
904         status = phys_efi_set_virtual_address_map(
905                 memmap.desc_size * count,
906                 memmap.desc_size,
907                 memmap.desc_version,
908                 (efi_memory_desc_t *)__pa(new_memmap));
909
910         if (status != EFI_SUCCESS) {
911                 pr_alert("Unable to switch EFI into virtual mode "
912                          "(status=%lx)!\n", status);
913                 panic("EFI call to SetVirtualAddressMap() failed!");
914         }
915
916         /*
917          * Now that EFI is in virtual mode, update the function
918          * pointers in the runtime service table to the new virtual addresses.
919          *
920          * Call EFI services through wrapper functions.
921          */
922         efi.runtime_version = efi_systab.fw_revision;
923         efi.get_time = virt_efi_get_time;
924         efi.set_time = virt_efi_set_time;
925         efi.get_wakeup_time = virt_efi_get_wakeup_time;
926         efi.set_wakeup_time = virt_efi_set_wakeup_time;
927         efi.get_variable = virt_efi_get_variable;
928         efi.get_next_variable = virt_efi_get_next_variable;
929         efi.set_variable = virt_efi_set_variable;
930         efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
931         efi.reset_system = virt_efi_reset_system;
932         efi.set_virtual_address_map = NULL;
933         efi.query_variable_info = virt_efi_query_variable_info;
934         efi.update_capsule = virt_efi_update_capsule;
935         efi.query_capsule_caps = virt_efi_query_capsule_caps;
936         if (__supported_pte_mask & _PAGE_NX)
937                 runtime_code_page_mkexec();
938
939         kfree(new_memmap);
940 }
941
942 /*
943  * Convenience functions to obtain memory types and attributes
944  */
945 u32 efi_mem_type(unsigned long phys_addr)
946 {
947         efi_memory_desc_t *md;
948         void *p;
949
950         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
951                 md = p;
952                 if ((md->phys_addr <= phys_addr) &&
953                     (phys_addr < (md->phys_addr +
954                                   (md->num_pages << EFI_PAGE_SHIFT))))
955                         return md->type;
956         }
957         return 0;
958 }
959
960 u64 efi_mem_attributes(unsigned long phys_addr)
961 {
962         efi_memory_desc_t *md;
963         void *p;
964
965         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
966                 md = p;
967                 if ((md->phys_addr <= phys_addr) &&
968                     (phys_addr < (md->phys_addr +
969                                   (md->num_pages << EFI_PAGE_SHIFT))))
970                         return md->attribute;
971         }
972         return 0;
973 }