]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/x86/platform/efi/efi.c
efi/runtime-map: Use efi.memmap directly instead of a copy
[mirror_ubuntu-artful-kernel.git] / arch / x86 / platform / efi / efi.c
CommitLineData
5b83683f
HY
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>
d2f7cbe7
BP
15 * Copyright (C) 2013 SuSE Labs
16 * Borislav Petkov <bp@suse.de> - runtime services VA mapping
5b83683f
HY
17 *
18 * Copied from efi_32.c to eliminate the duplicated code between EFI
19 * 32/64 support code. --ying 2007-10-26
20 *
21 * All EFI Runtime Services are not implemented yet as EFI only
22 * supports physical mode addressing on SoftSDV. This is to be fixed
23 * in a future version. --drummond 1999-07-20
24 *
25 * Implemented EFI runtime services and virtual mode calls. --davidm
26 *
27 * Goutham Rao: <goutham.rao@intel.com>
28 * Skip non-WB memory and ignore empty memory ranges.
29 */
30
e3cb3f5a
OJ
31#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32
5b83683f
HY
33#include <linux/kernel.h>
34#include <linux/init.h>
35#include <linux/efi.h>
2223af38 36#include <linux/efi-bgrt.h>
69c60c88 37#include <linux/export.h>
5b83683f 38#include <linux/bootmem.h>
0d01ff25 39#include <linux/slab.h>
a9ce6bc1 40#include <linux/memblock.h>
5b83683f
HY
41#include <linux/spinlock.h>
42#include <linux/uaccess.h>
43#include <linux/time.h>
44#include <linux/io.h>
45#include <linux/reboot.h>
46#include <linux/bcd.h>
47
48#include <asm/setup.h>
49#include <asm/efi.h>
50#include <asm/time.h>
a2172e25
HY
51#include <asm/cacheflush.h>
52#include <asm/tlbflush.h>
7bd867df 53#include <asm/x86_init.h>
a5d90c92 54#include <asm/uv/uv.h>
5b83683f 55
ecaea42e 56static struct efi efi_phys __initdata;
5b83683f
HY
57static efi_system_table_t efi_systab __initdata;
58
9b7d2049 59static efi_config_table_type_t arch_tables[] __initdata = {
272686bf
LL
60#ifdef CONFIG_X86_UV
61 {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab},
62#endif
722da9d2 63 {NULL_GUID, NULL, NULL},
272686bf
LL
64};
65
1fec0533 66u64 efi_setup; /* efi setup_data physical address */
926172d4 67
60920685 68static int add_efi_memmap __initdata;
200001eb
PJ
69static int __init setup_add_efi_memmap(char *arg)
70{
71 add_efi_memmap = 1;
72 return 0;
73}
74early_param("add_efi_memmap", setup_add_efi_memmap);
75
5b83683f
HY
76static efi_status_t __init phys_efi_set_virtual_address_map(
77 unsigned long memory_map_size,
78 unsigned long descriptor_size,
79 u32 descriptor_version,
80 efi_memory_desc_t *virtual_map)
81{
82 efi_status_t status;
23a0d4e8 83 unsigned long flags;
744937b0 84 pgd_t *save_pgd;
5b83683f 85
744937b0 86 save_pgd = efi_call_phys_prolog();
23a0d4e8
IM
87
88 /* Disable interrupts around EFI calls: */
89 local_irq_save(flags);
62fa6e69
MF
90 status = efi_call_phys(efi_phys.set_virtual_address_map,
91 memory_map_size, descriptor_size,
92 descriptor_version, virtual_map);
23a0d4e8
IM
93 local_irq_restore(flags);
94
744937b0 95 efi_call_phys_epilog(save_pgd);
23a0d4e8 96
5b83683f
HY
97 return status;
98}
99
b05b9f5f
TL
100void __init efi_find_mirror(void)
101{
78ce248f 102 efi_memory_desc_t *md;
b05b9f5f
TL
103 u64 mirror_size = 0, total_size = 0;
104
78ce248f 105 for_each_efi_memory_desc(md) {
b05b9f5f
TL
106 unsigned long long start = md->phys_addr;
107 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
108
109 total_size += size;
110 if (md->attribute & EFI_MEMORY_MORE_RELIABLE) {
111 memblock_mark_mirror(start, size);
112 mirror_size += size;
113 }
114 }
115 if (mirror_size)
116 pr_info("Memory: %lldM/%lldM mirrored memory\n",
117 mirror_size>>20, total_size>>20);
118}
119
69c91893
PJ
120/*
121 * Tell the kernel about the EFI memory map. This might include
122 * more than the max 128 entries that can fit in the e820 legacy
123 * (zeropage) memory map.
124 */
125
200001eb 126static void __init do_add_efi_memmap(void)
69c91893 127{
78ce248f 128 efi_memory_desc_t *md;
69c91893 129
78ce248f 130 for_each_efi_memory_desc(md) {
69c91893
PJ
131 unsigned long long start = md->phys_addr;
132 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
133 int e820_type;
134
e2a71476
CW
135 switch (md->type) {
136 case EFI_LOADER_CODE:
137 case EFI_LOADER_DATA:
138 case EFI_BOOT_SERVICES_CODE:
139 case EFI_BOOT_SERVICES_DATA:
140 case EFI_CONVENTIONAL_MEMORY:
141 if (md->attribute & EFI_MEMORY_WB)
142 e820_type = E820_RAM;
143 else
144 e820_type = E820_RESERVED;
145 break;
146 case EFI_ACPI_RECLAIM_MEMORY:
147 e820_type = E820_ACPI;
148 break;
149 case EFI_ACPI_MEMORY_NVS:
150 e820_type = E820_NVS;
151 break;
152 case EFI_UNUSABLE_MEMORY:
153 e820_type = E820_UNUSABLE;
154 break;
ad5fb870
DW
155 case EFI_PERSISTENT_MEMORY:
156 e820_type = E820_PMEM;
157 break;
e2a71476
CW
158 default:
159 /*
160 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
161 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
162 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
163 */
69c91893 164 e820_type = E820_RESERVED;
e2a71476
CW
165 break;
166 }
d0be6bde 167 e820_add_region(start, size, e820_type);
69c91893
PJ
168 }
169 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
170}
171
1adbfa35 172int __init efi_memblock_x86_reserve_range(void)
ecacf09f 173{
15b9c359 174 struct efi_info *e = &boot_params.efi_info;
9479c7ce 175 struct efi_memory_map_data data;
44511fb9 176 phys_addr_t pmap;
9479c7ce 177 int rv;
ecacf09f 178
9f27bc54
DK
179 if (efi_enabled(EFI_PARAVIRT))
180 return 0;
181
05486fa7 182#ifdef CONFIG_X86_32
1adbfa35 183 /* Can't handle data above 4GB at this time */
15b9c359 184 if (e->efi_memmap_hi) {
1adbfa35
OJ
185 pr_err("Memory map is above 4GB, disabling EFI.\n");
186 return -EINVAL;
187 }
15b9c359 188 pmap = e->efi_memmap;
05486fa7 189#else
15b9c359 190 pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
ecacf09f 191#endif
9479c7ce
MF
192 data.phys_map = pmap;
193 data.size = e->efi_memmap_size;
194 data.desc_size = e->efi_memdesc_size;
195 data.desc_version = e->efi_memdesc_version;
196
197 rv = efi_memmap_init_early(&data);
198 if (rv)
199 return rv;
200
201 if (add_efi_memmap)
202 do_add_efi_memmap();
1adbfa35 203
0d054ad9
AB
204 WARN(efi.memmap.desc_version != 1,
205 "Unexpected EFI_MEMORY_DESCRIPTOR version %ld",
206 efi.memmap.desc_version);
207
884f4f66 208 memblock_reserve(pmap, efi.memmap.nr_map * efi.memmap.desc_size);
258f6fd7 209
1adbfa35 210 return 0;
ecacf09f
HY
211}
212
0bbea1ce 213void __init efi_print_memmap(void)
5b83683f
HY
214{
215 efi_memory_desc_t *md;
78ce248f 216 int i = 0;
5b83683f 217
78ce248f 218 for_each_efi_memory_desc(md) {
ace1d121
LE
219 char buf[64];
220
1e82b947 221 pr_info("mem%02u: %s range=[0x%016llx-0x%016llx] (%lluMB)\n",
78ce248f 222 i++, efi_md_typeattr_format(buf, sizeof(buf), md),
ace1d121 223 md->phys_addr,
1e82b947 224 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - 1,
5b83683f
HY
225 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
226 }
f4fccac0 227}
5b83683f 228
140bf275 229static int __init efi_systab_init(void *phys)
5b83683f 230{
83e68189 231 if (efi_enabled(EFI_64BIT)) {
1adbfa35 232 efi_system_table_64_t *systab64;
1fec0533 233 struct efi_setup_data *data = NULL;
1adbfa35
OJ
234 u64 tmp = 0;
235
1fec0533
DY
236 if (efi_setup) {
237 data = early_memremap(efi_setup, sizeof(*data));
238 if (!data)
239 return -ENOMEM;
240 }
abc93f8e 241 systab64 = early_memremap((unsigned long)phys,
1adbfa35
OJ
242 sizeof(*systab64));
243 if (systab64 == NULL) {
244 pr_err("Couldn't map the system table!\n");
1fec0533 245 if (data)
abc93f8e 246 early_memunmap(data, sizeof(*data));
1adbfa35
OJ
247 return -ENOMEM;
248 }
249
250 efi_systab.hdr = systab64->hdr;
1fec0533
DY
251 efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor :
252 systab64->fw_vendor;
253 tmp |= data ? data->fw_vendor : systab64->fw_vendor;
1adbfa35
OJ
254 efi_systab.fw_revision = systab64->fw_revision;
255 efi_systab.con_in_handle = systab64->con_in_handle;
256 tmp |= systab64->con_in_handle;
257 efi_systab.con_in = systab64->con_in;
258 tmp |= systab64->con_in;
259 efi_systab.con_out_handle = systab64->con_out_handle;
260 tmp |= systab64->con_out_handle;
261 efi_systab.con_out = systab64->con_out;
262 tmp |= systab64->con_out;
263 efi_systab.stderr_handle = systab64->stderr_handle;
264 tmp |= systab64->stderr_handle;
265 efi_systab.stderr = systab64->stderr;
266 tmp |= systab64->stderr;
1fec0533
DY
267 efi_systab.runtime = data ?
268 (void *)(unsigned long)data->runtime :
269 (void *)(unsigned long)systab64->runtime;
270 tmp |= data ? data->runtime : systab64->runtime;
1adbfa35
OJ
271 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
272 tmp |= systab64->boottime;
273 efi_systab.nr_tables = systab64->nr_tables;
1fec0533
DY
274 efi_systab.tables = data ? (unsigned long)data->tables :
275 systab64->tables;
276 tmp |= data ? data->tables : systab64->tables;
1adbfa35 277
abc93f8e 278 early_memunmap(systab64, sizeof(*systab64));
1fec0533 279 if (data)
abc93f8e 280 early_memunmap(data, sizeof(*data));
1adbfa35
OJ
281#ifdef CONFIG_X86_32
282 if (tmp >> 32) {
283 pr_err("EFI data located above 4GB, disabling EFI.\n");
284 return -EINVAL;
285 }
286#endif
287 } else {
288 efi_system_table_32_t *systab32;
289
abc93f8e 290 systab32 = early_memremap((unsigned long)phys,
1adbfa35
OJ
291 sizeof(*systab32));
292 if (systab32 == NULL) {
293 pr_err("Couldn't map the system table!\n");
294 return -ENOMEM;
295 }
296
297 efi_systab.hdr = systab32->hdr;
298 efi_systab.fw_vendor = systab32->fw_vendor;
299 efi_systab.fw_revision = systab32->fw_revision;
300 efi_systab.con_in_handle = systab32->con_in_handle;
301 efi_systab.con_in = systab32->con_in;
302 efi_systab.con_out_handle = systab32->con_out_handle;
303 efi_systab.con_out = systab32->con_out;
304 efi_systab.stderr_handle = systab32->stderr_handle;
305 efi_systab.stderr = systab32->stderr;
306 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
307 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
308 efi_systab.nr_tables = systab32->nr_tables;
309 efi_systab.tables = systab32->tables;
310
abc93f8e 311 early_memunmap(systab32, sizeof(*systab32));
140bf275 312 }
1adbfa35 313
5b83683f
HY
314 efi.systab = &efi_systab;
315
316 /*
317 * Verify the EFI Table
318 */
140bf275 319 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
e3cb3f5a 320 pr_err("System table signature incorrect!\n");
140bf275
OJ
321 return -EINVAL;
322 }
5b83683f 323 if ((efi.systab->hdr.revision >> 16) == 0)
9b7d2049 324 pr_err("Warning: System table version %d.%02d, expected 1.00 or greater!\n",
5b83683f
HY
325 efi.systab->hdr.revision >> 16,
326 efi.systab->hdr.revision & 0xffff);
140bf275
OJ
327
328 return 0;
83e7ee66 329}
5b83683f 330
4f9dbcfc 331static int __init efi_runtime_init32(void)
83e7ee66 332{
4f9dbcfc
MF
333 efi_runtime_services_32_t *runtime;
334
abc93f8e 335 runtime = early_memremap((unsigned long)efi.systab->runtime,
4f9dbcfc
MF
336 sizeof(efi_runtime_services_32_t));
337 if (!runtime) {
338 pr_err("Could not map the runtime service table!\n");
339 return -ENOMEM;
340 }
5b83683f
HY
341
342 /*
0ce4605c
MK
343 * We will only need *early* access to the SetVirtualAddressMap
344 * EFI runtime service. All other runtime services will be called
345 * via the virtual mapping.
5b83683f 346 */
4f9dbcfc
MF
347 efi_phys.set_virtual_address_map =
348 (efi_set_virtual_address_map_t *)
349 (unsigned long)runtime->set_virtual_address_map;
abc93f8e 350 early_memunmap(runtime, sizeof(efi_runtime_services_32_t));
4f9dbcfc
MF
351
352 return 0;
353}
354
355static int __init efi_runtime_init64(void)
356{
357 efi_runtime_services_64_t *runtime;
358
abc93f8e 359 runtime = early_memremap((unsigned long)efi.systab->runtime,
4f9dbcfc 360 sizeof(efi_runtime_services_64_t));
140bf275 361 if (!runtime) {
e3cb3f5a 362 pr_err("Could not map the runtime service table!\n");
140bf275
OJ
363 return -ENOMEM;
364 }
4f9dbcfc 365
140bf275 366 /*
0ce4605c
MK
367 * We will only need *early* access to the SetVirtualAddressMap
368 * EFI runtime service. All other runtime services will be called
369 * via the virtual mapping.
140bf275 370 */
140bf275 371 efi_phys.set_virtual_address_map =
4f9dbcfc
MF
372 (efi_set_virtual_address_map_t *)
373 (unsigned long)runtime->set_virtual_address_map;
abc93f8e 374 early_memunmap(runtime, sizeof(efi_runtime_services_64_t));
4f9dbcfc
MF
375
376 return 0;
377}
378
379static int __init efi_runtime_init(void)
380{
381 int rv;
382
383 /*
384 * Check out the runtime services table. We need to map
385 * the runtime services table so that we can grab the physical
386 * address of several of the EFI runtime functions, needed to
387 * set the firmware into virtual mode.
9f27bc54
DK
388 *
389 * When EFI_PARAVIRT is in force then we could not map runtime
390 * service memory region because we do not have direct access to it.
391 * However, runtime services are available through proxy functions
392 * (e.g. in case of Xen dom0 EFI implementation they call special
393 * hypercall which executes relevant EFI functions) and that is why
394 * they are always enabled.
4f9dbcfc 395 */
4f9dbcfc 396
9f27bc54
DK
397 if (!efi_enabled(EFI_PARAVIRT)) {
398 if (efi_enabled(EFI_64BIT))
399 rv = efi_runtime_init64();
400 else
401 rv = efi_runtime_init32();
402
403 if (rv)
404 return rv;
405 }
140bf275 406
0f8093a9
MF
407 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
408
140bf275 409 return 0;
83e7ee66 410}
5b83683f 411
83e7ee66
OJ
412void __init efi_init(void)
413{
414 efi_char16_t *c16;
415 char vendor[100] = "unknown";
416 int i = 0;
417 void *tmp;
418
419#ifdef CONFIG_X86_32
1adbfa35
OJ
420 if (boot_params.efi_info.efi_systab_hi ||
421 boot_params.efi_info.efi_memmap_hi) {
422 pr_info("Table located above 4GB, disabling EFI.\n");
1adbfa35
OJ
423 return;
424 }
83e7ee66
OJ
425 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
426#else
427 efi_phys.systab = (efi_system_table_t *)
1adbfa35
OJ
428 (boot_params.efi_info.efi_systab |
429 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
83e7ee66
OJ
430#endif
431
83e68189 432 if (efi_systab_init(efi_phys.systab))
140bf275 433 return;
83e68189 434
a0998eb1
DY
435 efi.config_table = (unsigned long)efi.systab->tables;
436 efi.fw_vendor = (unsigned long)efi.systab->fw_vendor;
437 efi.runtime = (unsigned long)efi.systab->runtime;
438
83e7ee66
OJ
439 /*
440 * Show what we know for posterity
441 */
abc93f8e 442 c16 = tmp = early_memremap(efi.systab->fw_vendor, 2);
83e7ee66
OJ
443 if (c16) {
444 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
445 vendor[i] = *c16++;
446 vendor[i] = '\0';
447 } else
e3cb3f5a 448 pr_err("Could not map the firmware vendor!\n");
abc93f8e 449 early_memunmap(tmp, 2);
83e7ee66 450
e3cb3f5a
OJ
451 pr_info("EFI v%u.%.02u by %s\n",
452 efi.systab->hdr.revision >> 16,
453 efi.systab->hdr.revision & 0xffff, vendor);
83e7ee66 454
1fec0533
DY
455 if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
456 return;
457
272686bf 458 if (efi_config_init(arch_tables))
140bf275 459 return;
83e68189 460
1adbfa35
OJ
461 /*
462 * Note: We currently don't support runtime services on an EFI
463 * that doesn't match the kernel 32/64-bit mode.
464 */
465
7d453eee 466 if (!efi_runtime_supported())
1adbfa35 467 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
83e68189 468 else {
9479c7ce
MF
469 if (efi_runtime_disabled() || efi_runtime_init()) {
470 efi_memmap_unmap();
83e68189 471 return;
9479c7ce 472 }
140bf275 473 }
83e68189 474
fed6cefe 475 if (efi_enabled(EFI_DBG))
0bbea1ce 476 efi_print_memmap();
0bb54905
PJ
477
478 efi_esrt_init();
5b83683f
HY
479}
480
2223af38
JT
481void __init efi_late_init(void)
482{
483 efi_bgrt_init();
484}
485
9cd2b07c
MG
486void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
487{
488 u64 addr, npages;
489
490 addr = md->virt_addr;
491 npages = md->num_pages;
492
493 memrange_efi_to_native(&addr, &npages);
494
495 if (executable)
496 set_memory_x(addr, npages);
497 else
498 set_memory_nx(addr, npages);
499}
500
c55d016f 501void __init runtime_code_page_mkexec(void)
a2172e25
HY
502{
503 efi_memory_desc_t *md;
a2172e25 504
a2172e25 505 /* Make EFI runtime service code area executable */
78ce248f 506 for_each_efi_memory_desc(md) {
1c083eb2
HY
507 if (md->type != EFI_RUNTIME_SERVICES_CODE)
508 continue;
509
9cd2b07c 510 efi_set_executable(md, true);
a2172e25 511 }
a2172e25 512}
a2172e25 513
4e78eb05 514void __init efi_memory_uc(u64 addr, unsigned long size)
3e8fa263
MF
515{
516 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
517 u64 npages;
518
519 npages = round_up(size, page_shift) / page_shift;
520 memrange_efi_to_native(&addr, &npages);
521 set_memory_uc(addr, npages);
522}
523
d2f7cbe7
BP
524void __init old_map_region(efi_memory_desc_t *md)
525{
526 u64 start_pfn, end_pfn, end;
527 unsigned long size;
528 void *va;
529
530 start_pfn = PFN_DOWN(md->phys_addr);
531 size = md->num_pages << PAGE_SHIFT;
532 end = md->phys_addr + size;
533 end_pfn = PFN_UP(end);
534
535 if (pfn_range_is_mapped(start_pfn, end_pfn)) {
536 va = __va(md->phys_addr);
537
538 if (!(md->attribute & EFI_MEMORY_WB))
539 efi_memory_uc((u64)(unsigned long)va, size);
540 } else
541 va = efi_ioremap(md->phys_addr, size,
542 md->type, md->attribute);
543
544 md->virt_addr = (u64) (unsigned long) va;
545 if (!va)
546 pr_err("ioremap of 0x%llX failed!\n",
547 (unsigned long long)md->phys_addr);
548}
549
481f75c0
DY
550/* Merge contiguous regions of the same type and attribute */
551static void __init efi_merge_regions(void)
5b83683f 552{
202f9d0a 553 efi_memory_desc_t *md, *prev_md = NULL;
202f9d0a 554
78ce248f 555 for_each_efi_memory_desc(md) {
202f9d0a 556 u64 prev_size;
202f9d0a
MG
557
558 if (!prev_md) {
559 prev_md = md;
560 continue;
561 }
562
563 if (prev_md->type != md->type ||
564 prev_md->attribute != md->attribute) {
565 prev_md = md;
566 continue;
567 }
568
569 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
570
571 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
572 prev_md->num_pages += md->num_pages;
573 md->type = EFI_RESERVED_TYPE;
574 md->attribute = 0;
575 continue;
576 }
577 prev_md = md;
481f75c0
DY
578 }
579}
580
581static void __init get_systab_virt_addr(efi_memory_desc_t *md)
582{
583 unsigned long size;
584 u64 end, systab;
d2f7cbe7 585
481f75c0
DY
586 size = md->num_pages << EFI_PAGE_SHIFT;
587 end = md->phys_addr + size;
588 systab = (u64)(unsigned long)efi_phys.systab;
589 if (md->phys_addr <= systab && systab < end) {
590 systab += md->virt_addr - md->phys_addr;
591 efi.systab = (efi_system_table_t *)(unsigned long)systab;
202f9d0a 592 }
481f75c0
DY
593}
594
b7b898ae
BP
595static void *realloc_pages(void *old_memmap, int old_shift)
596{
597 void *ret;
598
599 ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1);
600 if (!ret)
601 goto out;
602
603 /*
604 * A first-time allocation doesn't have anything to copy.
605 */
606 if (!old_memmap)
607 return ret;
608
609 memcpy(ret, old_memmap, PAGE_SIZE << old_shift);
610
611out:
612 free_pages((unsigned long)old_memmap, old_shift);
613 return ret;
614}
615
a5caa209
MF
616/*
617 * Iterate the EFI memory map in reverse order because the regions
618 * will be mapped top-down. The end result is the same as if we had
619 * mapped things forward, but doesn't require us to change the
620 * existing implementation of efi_map_region().
621 */
622static inline void *efi_map_next_entry_reverse(void *entry)
623{
624 /* Initial call */
625 if (!entry)
884f4f66 626 return efi.memmap.map_end - efi.memmap.desc_size;
a5caa209 627
884f4f66
MF
628 entry -= efi.memmap.desc_size;
629 if (entry < efi.memmap.map)
a5caa209
MF
630 return NULL;
631
632 return entry;
633}
634
635/*
636 * efi_map_next_entry - Return the next EFI memory map descriptor
637 * @entry: Previous EFI memory map descriptor
638 *
639 * This is a helper function to iterate over the EFI memory map, which
640 * we do in different orders depending on the current configuration.
641 *
642 * To begin traversing the memory map @entry must be %NULL.
643 *
644 * Returns %NULL when we reach the end of the memory map.
645 */
646static void *efi_map_next_entry(void *entry)
647{
648 if (!efi_enabled(EFI_OLD_MEMMAP) && efi_enabled(EFI_64BIT)) {
649 /*
650 * Starting in UEFI v2.5 the EFI_PROPERTIES_TABLE
651 * config table feature requires us to map all entries
652 * in the same order as they appear in the EFI memory
653 * map. That is to say, entry N must have a lower
654 * virtual address than entry N+1. This is because the
655 * firmware toolchain leaves relative references in
656 * the code/data sections, which are split and become
657 * separate EFI memory regions. Mapping things
658 * out-of-order leads to the firmware accessing
659 * unmapped addresses.
660 *
661 * Since we need to map things this way whether or not
662 * the kernel actually makes use of
663 * EFI_PROPERTIES_TABLE, let's just switch to this
664 * scheme by default for 64-bit.
665 */
666 return efi_map_next_entry_reverse(entry);
667 }
668
669 /* Initial call */
670 if (!entry)
884f4f66 671 return efi.memmap.map;
a5caa209 672
884f4f66
MF
673 entry += efi.memmap.desc_size;
674 if (entry >= efi.memmap.map_end)
a5caa209
MF
675 return NULL;
676
677 return entry;
678}
679
ab72a27d
MF
680static bool should_map_region(efi_memory_desc_t *md)
681{
682 /*
683 * Runtime regions always require runtime mappings (obviously).
684 */
685 if (md->attribute & EFI_MEMORY_RUNTIME)
686 return true;
687
688 /*
689 * 32-bit EFI doesn't suffer from the bug that requires us to
690 * reserve boot services regions, and mixed mode support
691 * doesn't exist for 32-bit kernels.
692 */
693 if (IS_ENABLED(CONFIG_X86_32))
694 return false;
695
696 /*
697 * Map all of RAM so that we can access arguments in the 1:1
698 * mapping when making EFI runtime calls.
699 */
700 if (IS_ENABLED(CONFIG_EFI_MIXED) && !efi_is_native()) {
701 if (md->type == EFI_CONVENTIONAL_MEMORY ||
702 md->type == EFI_LOADER_DATA ||
703 md->type == EFI_LOADER_CODE)
704 return true;
705 }
706
707 /*
708 * Map boot services regions as a workaround for buggy
709 * firmware that accesses them even when they shouldn't.
710 *
711 * See efi_{reserve,free}_boot_services().
712 */
713 if (md->type == EFI_BOOT_SERVICES_CODE ||
714 md->type == EFI_BOOT_SERVICES_DATA)
715 return true;
716
717 return false;
718}
719
481f75c0 720/*
b7b898ae
BP
721 * Map the efi memory ranges of the runtime services and update new_mmap with
722 * virtual addresses.
481f75c0 723 */
b7b898ae 724static void * __init efi_map_regions(int *count, int *pg_shift)
481f75c0 725{
b7b898ae
BP
726 void *p, *new_memmap = NULL;
727 unsigned long left = 0;
884f4f66 728 unsigned long desc_size;
481f75c0 729 efi_memory_desc_t *md;
202f9d0a 730
884f4f66
MF
731 desc_size = efi.memmap.desc_size;
732
a5caa209
MF
733 p = NULL;
734 while ((p = efi_map_next_entry(p))) {
5b83683f 735 md = p;
ab72a27d
MF
736
737 if (!should_map_region(md))
738 continue;
1c083eb2 739
d2f7cbe7 740 efi_map_region(md);
481f75c0
DY
741 get_systab_virt_addr(md);
742
884f4f66 743 if (left < desc_size) {
b7b898ae
BP
744 new_memmap = realloc_pages(new_memmap, *pg_shift);
745 if (!new_memmap)
746 return NULL;
747
748 left += PAGE_SIZE << *pg_shift;
749 (*pg_shift)++;
750 }
751
884f4f66 752 memcpy(new_memmap + (*count * desc_size), md, desc_size);
b7b898ae 753
884f4f66 754 left -= desc_size;
481f75c0
DY
755 (*count)++;
756 }
d2f7cbe7 757
481f75c0 758 return new_memmap;
481f75c0
DY
759}
760
fabb37c7
BP
761static void __init kexec_enter_virtual_mode(void)
762{
2965faa5 763#ifdef CONFIG_KEXEC_CORE
fabb37c7 764 efi_memory_desc_t *md;
753b11ef 765 unsigned int num_pages;
fabb37c7
BP
766
767 efi.systab = NULL;
768
769 /*
770 * We don't do virtual mode, since we don't do runtime services, on
771 * non-native EFI
772 */
773 if (!efi_is_native()) {
9479c7ce 774 efi_memmap_unmap();
a5a750a9 775 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
fabb37c7
BP
776 return;
777 }
778
753b11ef
MF
779 if (efi_alloc_page_tables()) {
780 pr_err("Failed to allocate EFI page tables\n");
781 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
782 return;
783 }
784
fabb37c7
BP
785 /*
786 * Map efi regions which were passed via setup_data. The virt_addr is a
787 * fixed addr which was used in first kernel of a kexec boot.
788 */
78ce248f 789 for_each_efi_memory_desc(md) {
fabb37c7
BP
790 efi_map_region_fixed(md); /* FIXME: add error handling */
791 get_systab_virt_addr(md);
792 }
793
dca0f971
MF
794 /*
795 * Unregister the early EFI memmap from efi_init() and install
796 * the new EFI memory map.
797 */
798 efi_memmap_unmap();
799
800 if (efi_memmap_init_late(efi.memmap.phys_map,
801 efi.memmap.desc_size * efi.memmap.nr_map)) {
802 pr_err("Failed to remap late EFI memory map\n");
803 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
804 return;
805 }
806
fabb37c7
BP
807 BUG_ON(!efi.systab);
808
884f4f66 809 num_pages = ALIGN(efi.memmap.nr_map * efi.memmap.desc_size, PAGE_SIZE);
753b11ef
MF
810 num_pages >>= PAGE_SHIFT;
811
884f4f66 812 if (efi_setup_page_tables(efi.memmap.phys_map, num_pages)) {
753b11ef
MF
813 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
814 return;
815 }
816
fabb37c7
BP
817 efi_sync_low_kernel_mappings();
818
819 /*
820 * Now that EFI is in virtual mode, update the function
821 * pointers in the runtime service table to the new virtual addresses.
822 *
823 * Call EFI services through wrapper functions.
824 */
825 efi.runtime_version = efi_systab.hdr.revision;
994448f1 826
022ee6c5 827 efi_native_runtime_setup();
994448f1 828
fabb37c7 829 efi.set_virtual_address_map = NULL;
fabb37c7
BP
830
831 if (efi_enabled(EFI_OLD_MEMMAP) && (__supported_pte_mask & _PAGE_NX))
832 runtime_code_page_mkexec();
833
834 /* clean DUMMY object */
eeb9db09 835 efi_delete_dummy_variable();
fabb37c7
BP
836#endif
837}
838
481f75c0
DY
839/*
840 * This function will switch the EFI runtime services to virtual mode.
841 * Essentially, we look through the EFI memmap and map every region that
842 * has the runtime attribute bit set in its memory descriptor into the
67a9108e 843 * efi_pgd page table.
481f75c0
DY
844 *
845 * The old method which used to update that memory descriptor with the
846 * virtual address obtained from ioremap() is still supported when the
847 * kernel is booted with efi=old_map on its command line. Same old
848 * method enabled the runtime services to be called without having to
849 * thunk back into physical mode for every invocation.
850 *
851 * The new method does a pagetable switch in a preemption-safe manner
852 * so that we're in a different address space when calling a runtime
67a9108e
MF
853 * function. For function arguments passing we do copy the PUDs of the
854 * kernel page table into efi_pgd prior to each call.
1fec0533
DY
855 *
856 * Specially for kexec boot, efi runtime maps in previous kernel should
857 * be passed in via setup_data. In that case runtime ranges will be mapped
fabb37c7
BP
858 * to the same virtual addresses as the first kernel, see
859 * kexec_enter_virtual_mode().
481f75c0 860 */
fabb37c7 861static void __init __efi_enter_virtual_mode(void)
481f75c0 862{
fabb37c7 863 int count = 0, pg_shift = 0;
481f75c0 864 void *new_memmap = NULL;
b7b898ae 865 efi_status_t status;
dca0f971 866 phys_addr_t pa;
1c083eb2 867
481f75c0 868 efi.systab = NULL;
d2f7cbe7 869
67a9108e
MF
870 if (efi_alloc_page_tables()) {
871 pr_err("Failed to allocate EFI page tables\n");
872 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
873 return;
874 }
875
fabb37c7
BP
876 efi_merge_regions();
877 new_memmap = efi_map_regions(&count, &pg_shift);
878 if (!new_memmap) {
879 pr_err("Error reallocating memory, EFI runtime non-functional!\n");
a5a750a9 880 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
fabb37c7 881 return;
b7b898ae 882 }
926172d4 883
dca0f971
MF
884 pa = __pa(new_memmap);
885
886 /*
887 * Unregister the early EFI memmap from efi_init() and install
888 * the new EFI memory map that we are about to pass to the
889 * firmware via SetVirtualAddressMap().
890 */
891 efi_memmap_unmap();
892
893 if (efi_memmap_init_late(pa, efi.memmap.desc_size * count)) {
894 pr_err("Failed to remap late EFI memory map\n");
895 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
896 return;
897 }
898
5b83683f
HY
899 BUG_ON(!efi.systab);
900
dca0f971 901 if (efi_setup_page_tables(pa, 1 << pg_shift)) {
a5a750a9 902 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
fabb37c7 903 return;
a5a750a9 904 }
b7b898ae 905
d2f7cbe7
BP
906 efi_sync_low_kernel_mappings();
907
994448f1
MF
908 if (efi_is_native()) {
909 status = phys_efi_set_virtual_address_map(
884f4f66
MF
910 efi.memmap.desc_size * count,
911 efi.memmap.desc_size,
912 efi.memmap.desc_version,
dca0f971 913 (efi_memory_desc_t *)pa);
994448f1
MF
914 } else {
915 status = efi_thunk_set_virtual_address_map(
916 efi_phys.set_virtual_address_map,
884f4f66
MF
917 efi.memmap.desc_size * count,
918 efi.memmap.desc_size,
919 efi.memmap.desc_version,
dca0f971 920 (efi_memory_desc_t *)pa);
994448f1 921 }
1fec0533 922
fabb37c7
BP
923 if (status != EFI_SUCCESS) {
924 pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
925 status);
926 panic("EFI call to SetVirtualAddressMap() failed!");
5b83683f
HY
927 }
928
929 /*
930 * Now that EFI is in virtual mode, update the function
931 * pointers in the runtime service table to the new virtual addresses.
932 *
933 * Call EFI services through wrapper functions.
934 */
712ba9e9 935 efi.runtime_version = efi_systab.hdr.revision;
4f9dbcfc
MF
936
937 if (efi_is_native())
022ee6c5 938 efi_native_runtime_setup();
4f9dbcfc
MF
939 else
940 efi_thunk_runtime_setup();
941
2b5e8ef3 942 efi.set_virtual_address_map = NULL;
d2f7cbe7 943
6d0cc887
SP
944 /*
945 * Apply more restrictive page table mapping attributes now that
946 * SVAM() has been called and the firmware has performed all
947 * necessary relocation fixups for the new virtual addresses.
948 */
949 efi_runtime_update_mappings();
950 efi_dump_pagetable();
1adbfa35 951
f8b84043 952 /* clean DUMMY object */
eeb9db09 953 efi_delete_dummy_variable();
5b83683f
HY
954}
955
fabb37c7
BP
956void __init efi_enter_virtual_mode(void)
957{
9f27bc54
DK
958 if (efi_enabled(EFI_PARAVIRT))
959 return;
960
fabb37c7
BP
961 if (efi_setup)
962 kexec_enter_virtual_mode();
963 else
964 __efi_enter_virtual_mode();
965}
966
5b83683f
HY
967/*
968 * Convenience functions to obtain memory types and attributes
969 */
970u32 efi_mem_type(unsigned long phys_addr)
971{
972 efi_memory_desc_t *md;
5b83683f 973
83e68189
MF
974 if (!efi_enabled(EFI_MEMMAP))
975 return 0;
976
78ce248f 977 for_each_efi_memory_desc(md) {
5b83683f
HY
978 if ((md->phys_addr <= phys_addr) &&
979 (phys_addr < (md->phys_addr +
980 (md->num_pages << EFI_PAGE_SHIFT))))
981 return md->type;
982 }
983 return 0;
984}
985
5ae3683c 986static int __init arch_parse_efi_cmdline(char *str)
d2f7cbe7 987{
9115c758
RN
988 if (!str) {
989 pr_warn("need at least one option\n");
990 return -EINVAL;
991 }
992
6ccc72b8
DY
993 if (parse_option_str(str, "old_map"))
994 set_bit(EFI_OLD_MEMMAP, &efi.flags);
d2f7cbe7
BP
995
996 return 0;
997}
5ae3683c 998early_param("efi", arch_parse_efi_cmdline);