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