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