xref: /openbmc/linux/arch/x86/platform/efi/efi.c (revision 9b2433a9)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Common EFI (Extensible Firmware Interface) support functions
4  * Based on Extensible Firmware Interface Specification version 1.0
5  *
6  * Copyright (C) 1999 VA Linux Systems
7  * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
8  * Copyright (C) 1999-2002 Hewlett-Packard Co.
9  *	David Mosberger-Tang <davidm@hpl.hp.com>
10  *	Stephane Eranian <eranian@hpl.hp.com>
11  * Copyright (C) 2005-2008 Intel Co.
12  *	Fenghua Yu <fenghua.yu@intel.com>
13  *	Bibo Mao <bibo.mao@intel.com>
14  *	Chandramouli Narayanan <mouli@linux.intel.com>
15  *	Huang Ying <ying.huang@intel.com>
16  * Copyright (C) 2013 SuSE Labs
17  *	Borislav Petkov <bp@suse.de> - runtime services VA mapping
18  *
19  * Copied from efi_32.c to eliminate the duplicated code between EFI
20  * 32/64 support code. --ying 2007-10-26
21  *
22  * All EFI Runtime Services are not implemented yet as EFI only
23  * supports physical mode addressing on SoftSDV. This is to be fixed
24  * in a future version.  --drummond 1999-07-20
25  *
26  * Implemented EFI runtime services and virtual mode calls.  --davidm
27  *
28  * Goutham Rao: <goutham.rao@intel.com>
29  *	Skip non-WB memory and ignore empty memory ranges.
30  */
31 
32 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33 
34 #include <linux/kernel.h>
35 #include <linux/init.h>
36 #include <linux/efi.h>
37 #include <linux/efi-bgrt.h>
38 #include <linux/export.h>
39 #include <linux/memblock.h>
40 #include <linux/slab.h>
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/e820/api.h>
51 #include <asm/time.h>
52 #include <asm/set_memory.h>
53 #include <asm/tlbflush.h>
54 #include <asm/x86_init.h>
55 #include <asm/uv/uv.h>
56 
57 static struct efi efi_phys __initdata;
58 static efi_system_table_t efi_systab __initdata;
59 
60 static efi_config_table_type_t arch_tables[] __initdata = {
61 #ifdef CONFIG_X86_UV
62 	{UV_SYSTEM_TABLE_GUID, "UVsystab", &uv_systab_phys},
63 #endif
64 	{NULL_GUID, NULL, NULL},
65 };
66 
67 static const unsigned long * const efi_tables[] = {
68 	&efi.mps,
69 	&efi.acpi,
70 	&efi.acpi20,
71 	&efi.smbios,
72 	&efi.smbios3,
73 	&efi.boot_info,
74 	&efi.hcdp,
75 	&efi.uga,
76 #ifdef CONFIG_X86_UV
77 	&uv_systab_phys,
78 #endif
79 	&efi.fw_vendor,
80 	&efi.runtime,
81 	&efi.config_table,
82 	&efi.esrt,
83 	&efi.properties_table,
84 	&efi.mem_attr_table,
85 #ifdef CONFIG_EFI_RCI2_TABLE
86 	&rci2_table_phys,
87 #endif
88 };
89 
90 u64 efi_setup;		/* efi setup_data physical address */
91 
92 static int add_efi_memmap __initdata;
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 static efi_status_t __init phys_efi_set_virtual_address_map(
101 	unsigned long memory_map_size,
102 	unsigned long descriptor_size,
103 	u32 descriptor_version,
104 	efi_memory_desc_t *virtual_map)
105 {
106 	efi_status_t status;
107 	unsigned long flags;
108 	pgd_t *save_pgd;
109 
110 	save_pgd = efi_call_phys_prolog();
111 	if (!save_pgd)
112 		return EFI_ABORTED;
113 
114 	/* Disable interrupts around EFI calls: */
115 	local_irq_save(flags);
116 	status = efi_call_phys(efi_phys.set_virtual_address_map,
117 			       memory_map_size, descriptor_size,
118 			       descriptor_version, virtual_map);
119 	local_irq_restore(flags);
120 
121 	efi_call_phys_epilog(save_pgd);
122 
123 	return status;
124 }
125 
126 void __init efi_find_mirror(void)
127 {
128 	efi_memory_desc_t *md;
129 	u64 mirror_size = 0, total_size = 0;
130 
131 	if (!efi_enabled(EFI_MEMMAP))
132 		return;
133 
134 	for_each_efi_memory_desc(md) {
135 		unsigned long long start = md->phys_addr;
136 		unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
137 
138 		total_size += size;
139 		if (md->attribute & EFI_MEMORY_MORE_RELIABLE) {
140 			memblock_mark_mirror(start, size);
141 			mirror_size += size;
142 		}
143 	}
144 	if (mirror_size)
145 		pr_info("Memory: %lldM/%lldM mirrored memory\n",
146 			mirror_size>>20, total_size>>20);
147 }
148 
149 /*
150  * Tell the kernel about the EFI memory map.  This might include
151  * more than the max 128 entries that can fit in the passed in e820
152  * legacy (zeropage) memory map, but the kernel's e820 table can hold
153  * E820_MAX_ENTRIES.
154  */
155 
156 static void __init do_add_efi_memmap(void)
157 {
158 	efi_memory_desc_t *md;
159 
160 	if (!efi_enabled(EFI_MEMMAP))
161 		return;
162 
163 	for_each_efi_memory_desc(md) {
164 		unsigned long long start = md->phys_addr;
165 		unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
166 		int e820_type;
167 
168 		switch (md->type) {
169 		case EFI_LOADER_CODE:
170 		case EFI_LOADER_DATA:
171 		case EFI_BOOT_SERVICES_CODE:
172 		case EFI_BOOT_SERVICES_DATA:
173 		case EFI_CONVENTIONAL_MEMORY:
174 			if (efi_soft_reserve_enabled()
175 			    && (md->attribute & EFI_MEMORY_SP))
176 				e820_type = E820_TYPE_SOFT_RESERVED;
177 			else if (md->attribute & EFI_MEMORY_WB)
178 				e820_type = E820_TYPE_RAM;
179 			else
180 				e820_type = E820_TYPE_RESERVED;
181 			break;
182 		case EFI_ACPI_RECLAIM_MEMORY:
183 			e820_type = E820_TYPE_ACPI;
184 			break;
185 		case EFI_ACPI_MEMORY_NVS:
186 			e820_type = E820_TYPE_NVS;
187 			break;
188 		case EFI_UNUSABLE_MEMORY:
189 			e820_type = E820_TYPE_UNUSABLE;
190 			break;
191 		case EFI_PERSISTENT_MEMORY:
192 			e820_type = E820_TYPE_PMEM;
193 			break;
194 		default:
195 			/*
196 			 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
197 			 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
198 			 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
199 			 */
200 			e820_type = E820_TYPE_RESERVED;
201 			break;
202 		}
203 
204 		e820__range_add(start, size, e820_type);
205 	}
206 	e820__update_table(e820_table);
207 }
208 
209 /*
210  * Given add_efi_memmap defaults to 0 and there there is no alternative
211  * e820 mechanism for soft-reserved memory, import the full EFI memory
212  * map if soft reservations are present and enabled. Otherwise, the
213  * mechanism to disable the kernel's consideration of EFI_MEMORY_SP is
214  * the efi=nosoftreserve option.
215  */
216 static bool do_efi_soft_reserve(void)
217 {
218 	efi_memory_desc_t *md;
219 
220 	if (!efi_enabled(EFI_MEMMAP))
221 		return false;
222 
223 	if (!efi_soft_reserve_enabled())
224 		return false;
225 
226 	for_each_efi_memory_desc(md)
227 		if (md->type == EFI_CONVENTIONAL_MEMORY &&
228 		    (md->attribute & EFI_MEMORY_SP))
229 			return true;
230 	return false;
231 }
232 
233 int __init efi_memblock_x86_reserve_range(void)
234 {
235 	struct efi_info *e = &boot_params.efi_info;
236 	struct efi_memory_map_data data;
237 	phys_addr_t pmap;
238 	int rv;
239 
240 	if (efi_enabled(EFI_PARAVIRT))
241 		return 0;
242 
243 #ifdef CONFIG_X86_32
244 	/* Can't handle data above 4GB at this time */
245 	if (e->efi_memmap_hi) {
246 		pr_err("Memory map is above 4GB, disabling EFI.\n");
247 		return -EINVAL;
248 	}
249 	pmap =  e->efi_memmap;
250 #else
251 	pmap = (e->efi_memmap |	((__u64)e->efi_memmap_hi << 32));
252 #endif
253 	data.phys_map		= pmap;
254 	data.size 		= e->efi_memmap_size;
255 	data.desc_size		= e->efi_memdesc_size;
256 	data.desc_version	= e->efi_memdesc_version;
257 
258 	rv = efi_memmap_init_early(&data);
259 	if (rv)
260 		return rv;
261 
262 	if (add_efi_memmap || do_efi_soft_reserve())
263 		do_add_efi_memmap();
264 
265 	efi_fake_memmap_early();
266 
267 	WARN(efi.memmap.desc_version != 1,
268 	     "Unexpected EFI_MEMORY_DESCRIPTOR version %ld",
269 	     efi.memmap.desc_version);
270 
271 	memblock_reserve(pmap, efi.memmap.nr_map * efi.memmap.desc_size);
272 
273 	return 0;
274 }
275 
276 #define OVERFLOW_ADDR_SHIFT	(64 - EFI_PAGE_SHIFT)
277 #define OVERFLOW_ADDR_MASK	(U64_MAX << OVERFLOW_ADDR_SHIFT)
278 #define U64_HIGH_BIT		(~(U64_MAX >> 1))
279 
280 static bool __init efi_memmap_entry_valid(const efi_memory_desc_t *md, int i)
281 {
282 	u64 end = (md->num_pages << EFI_PAGE_SHIFT) + md->phys_addr - 1;
283 	u64 end_hi = 0;
284 	char buf[64];
285 
286 	if (md->num_pages == 0) {
287 		end = 0;
288 	} else if (md->num_pages > EFI_PAGES_MAX ||
289 		   EFI_PAGES_MAX - md->num_pages <
290 		   (md->phys_addr >> EFI_PAGE_SHIFT)) {
291 		end_hi = (md->num_pages & OVERFLOW_ADDR_MASK)
292 			>> OVERFLOW_ADDR_SHIFT;
293 
294 		if ((md->phys_addr & U64_HIGH_BIT) && !(end & U64_HIGH_BIT))
295 			end_hi += 1;
296 	} else {
297 		return true;
298 	}
299 
300 	pr_warn_once(FW_BUG "Invalid EFI memory map entries:\n");
301 
302 	if (end_hi) {
303 		pr_warn("mem%02u: %s range=[0x%016llx-0x%llx%016llx] (invalid)\n",
304 			i, efi_md_typeattr_format(buf, sizeof(buf), md),
305 			md->phys_addr, end_hi, end);
306 	} else {
307 		pr_warn("mem%02u: %s range=[0x%016llx-0x%016llx] (invalid)\n",
308 			i, efi_md_typeattr_format(buf, sizeof(buf), md),
309 			md->phys_addr, end);
310 	}
311 	return false;
312 }
313 
314 static void __init efi_clean_memmap(void)
315 {
316 	efi_memory_desc_t *out = efi.memmap.map;
317 	const efi_memory_desc_t *in = out;
318 	const efi_memory_desc_t *end = efi.memmap.map_end;
319 	int i, n_removal;
320 
321 	for (i = n_removal = 0; in < end; i++) {
322 		if (efi_memmap_entry_valid(in, i)) {
323 			if (out != in)
324 				memcpy(out, in, efi.memmap.desc_size);
325 			out = (void *)out + efi.memmap.desc_size;
326 		} else {
327 			n_removal++;
328 		}
329 		in = (void *)in + efi.memmap.desc_size;
330 	}
331 
332 	if (n_removal > 0) {
333 		u64 size = efi.memmap.nr_map - n_removal;
334 
335 		pr_warn("Removing %d invalid memory map entries.\n", n_removal);
336 		efi_memmap_install(efi.memmap.phys_map, size);
337 	}
338 }
339 
340 void __init efi_print_memmap(void)
341 {
342 	efi_memory_desc_t *md;
343 	int i = 0;
344 
345 	for_each_efi_memory_desc(md) {
346 		char buf[64];
347 
348 		pr_info("mem%02u: %s range=[0x%016llx-0x%016llx] (%lluMB)\n",
349 			i++, efi_md_typeattr_format(buf, sizeof(buf), md),
350 			md->phys_addr,
351 			md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - 1,
352 			(md->num_pages >> (20 - EFI_PAGE_SHIFT)));
353 	}
354 }
355 
356 static int __init efi_systab_init(void *phys)
357 {
358 	if (efi_enabled(EFI_64BIT)) {
359 		efi_system_table_64_t *systab64;
360 		struct efi_setup_data *data = NULL;
361 		u64 tmp = 0;
362 
363 		if (efi_setup) {
364 			data = early_memremap(efi_setup, sizeof(*data));
365 			if (!data)
366 				return -ENOMEM;
367 		}
368 		systab64 = early_memremap((unsigned long)phys,
369 					 sizeof(*systab64));
370 		if (systab64 == NULL) {
371 			pr_err("Couldn't map the system table!\n");
372 			if (data)
373 				early_memunmap(data, sizeof(*data));
374 			return -ENOMEM;
375 		}
376 
377 		efi_systab.hdr = systab64->hdr;
378 		efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor :
379 					      systab64->fw_vendor;
380 		tmp |= data ? data->fw_vendor : systab64->fw_vendor;
381 		efi_systab.fw_revision = systab64->fw_revision;
382 		efi_systab.con_in_handle = systab64->con_in_handle;
383 		tmp |= systab64->con_in_handle;
384 		efi_systab.con_in = systab64->con_in;
385 		tmp |= systab64->con_in;
386 		efi_systab.con_out_handle = systab64->con_out_handle;
387 		tmp |= systab64->con_out_handle;
388 		efi_systab.con_out = systab64->con_out;
389 		tmp |= systab64->con_out;
390 		efi_systab.stderr_handle = systab64->stderr_handle;
391 		tmp |= systab64->stderr_handle;
392 		efi_systab.stderr = systab64->stderr;
393 		tmp |= systab64->stderr;
394 		efi_systab.runtime = data ?
395 				     (void *)(unsigned long)data->runtime :
396 				     (void *)(unsigned long)systab64->runtime;
397 		tmp |= data ? data->runtime : systab64->runtime;
398 		efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
399 		tmp |= systab64->boottime;
400 		efi_systab.nr_tables = systab64->nr_tables;
401 		efi_systab.tables = data ? (unsigned long)data->tables :
402 					   systab64->tables;
403 		tmp |= data ? data->tables : systab64->tables;
404 
405 		early_memunmap(systab64, sizeof(*systab64));
406 		if (data)
407 			early_memunmap(data, sizeof(*data));
408 #ifdef CONFIG_X86_32
409 		if (tmp >> 32) {
410 			pr_err("EFI data located above 4GB, disabling EFI.\n");
411 			return -EINVAL;
412 		}
413 #endif
414 	} else {
415 		efi_system_table_32_t *systab32;
416 
417 		systab32 = early_memremap((unsigned long)phys,
418 					 sizeof(*systab32));
419 		if (systab32 == NULL) {
420 			pr_err("Couldn't map the system table!\n");
421 			return -ENOMEM;
422 		}
423 
424 		efi_systab.hdr = systab32->hdr;
425 		efi_systab.fw_vendor = systab32->fw_vendor;
426 		efi_systab.fw_revision = systab32->fw_revision;
427 		efi_systab.con_in_handle = systab32->con_in_handle;
428 		efi_systab.con_in = systab32->con_in;
429 		efi_systab.con_out_handle = systab32->con_out_handle;
430 		efi_systab.con_out = systab32->con_out;
431 		efi_systab.stderr_handle = systab32->stderr_handle;
432 		efi_systab.stderr = systab32->stderr;
433 		efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
434 		efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
435 		efi_systab.nr_tables = systab32->nr_tables;
436 		efi_systab.tables = systab32->tables;
437 
438 		early_memunmap(systab32, sizeof(*systab32));
439 	}
440 
441 	efi.systab = &efi_systab;
442 
443 	/*
444 	 * Verify the EFI Table
445 	 */
446 	if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
447 		pr_err("System table signature incorrect!\n");
448 		return -EINVAL;
449 	}
450 	if ((efi.systab->hdr.revision >> 16) == 0)
451 		pr_err("Warning: System table version %d.%02d, expected 1.00 or greater!\n",
452 		       efi.systab->hdr.revision >> 16,
453 		       efi.systab->hdr.revision & 0xffff);
454 
455 	return 0;
456 }
457 
458 static int __init efi_runtime_init32(void)
459 {
460 	efi_runtime_services_32_t *runtime;
461 
462 	runtime = early_memremap((unsigned long)efi.systab->runtime,
463 			sizeof(efi_runtime_services_32_t));
464 	if (!runtime) {
465 		pr_err("Could not map the runtime service table!\n");
466 		return -ENOMEM;
467 	}
468 
469 	/*
470 	 * We will only need *early* access to the SetVirtualAddressMap
471 	 * EFI runtime service. All other runtime services will be called
472 	 * via the virtual mapping.
473 	 */
474 	efi_phys.set_virtual_address_map =
475 			(efi_set_virtual_address_map_t *)
476 			(unsigned long)runtime->set_virtual_address_map;
477 	early_memunmap(runtime, sizeof(efi_runtime_services_32_t));
478 
479 	return 0;
480 }
481 
482 static int __init efi_runtime_init64(void)
483 {
484 	efi_runtime_services_64_t *runtime;
485 
486 	runtime = early_memremap((unsigned long)efi.systab->runtime,
487 			sizeof(efi_runtime_services_64_t));
488 	if (!runtime) {
489 		pr_err("Could not map the runtime service table!\n");
490 		return -ENOMEM;
491 	}
492 
493 	/*
494 	 * We will only need *early* access to the SetVirtualAddressMap
495 	 * EFI runtime service. All other runtime services will be called
496 	 * via the virtual mapping.
497 	 */
498 	efi_phys.set_virtual_address_map =
499 			(efi_set_virtual_address_map_t *)
500 			(unsigned long)runtime->set_virtual_address_map;
501 	early_memunmap(runtime, sizeof(efi_runtime_services_64_t));
502 
503 	return 0;
504 }
505 
506 static int __init efi_runtime_init(void)
507 {
508 	int rv;
509 
510 	/*
511 	 * Check out the runtime services table. We need to map
512 	 * the runtime services table so that we can grab the physical
513 	 * address of several of the EFI runtime functions, needed to
514 	 * set the firmware into virtual mode.
515 	 *
516 	 * When EFI_PARAVIRT is in force then we could not map runtime
517 	 * service memory region because we do not have direct access to it.
518 	 * However, runtime services are available through proxy functions
519 	 * (e.g. in case of Xen dom0 EFI implementation they call special
520 	 * hypercall which executes relevant EFI functions) and that is why
521 	 * they are always enabled.
522 	 */
523 
524 	if (!efi_enabled(EFI_PARAVIRT)) {
525 		if (efi_enabled(EFI_64BIT))
526 			rv = efi_runtime_init64();
527 		else
528 			rv = efi_runtime_init32();
529 
530 		if (rv)
531 			return rv;
532 	}
533 
534 	set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
535 
536 	return 0;
537 }
538 
539 void __init efi_init(void)
540 {
541 	efi_char16_t *c16;
542 	char vendor[100] = "unknown";
543 	int i = 0;
544 	void *tmp;
545 
546 #ifdef CONFIG_X86_32
547 	if (boot_params.efi_info.efi_systab_hi ||
548 	    boot_params.efi_info.efi_memmap_hi) {
549 		pr_info("Table located above 4GB, disabling EFI.\n");
550 		return;
551 	}
552 	efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
553 #else
554 	efi_phys.systab = (efi_system_table_t *)
555 			  (boot_params.efi_info.efi_systab |
556 			  ((__u64)boot_params.efi_info.efi_systab_hi<<32));
557 #endif
558 
559 	if (efi_systab_init(efi_phys.systab))
560 		return;
561 
562 	efi.config_table = (unsigned long)efi.systab->tables;
563 	efi.fw_vendor	 = (unsigned long)efi.systab->fw_vendor;
564 	efi.runtime	 = (unsigned long)efi.systab->runtime;
565 
566 	/*
567 	 * Show what we know for posterity
568 	 */
569 	c16 = tmp = early_memremap(efi.systab->fw_vendor, 2);
570 	if (c16) {
571 		for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
572 			vendor[i] = *c16++;
573 		vendor[i] = '\0';
574 	} else
575 		pr_err("Could not map the firmware vendor!\n");
576 	early_memunmap(tmp, 2);
577 
578 	pr_info("EFI v%u.%.02u by %s\n",
579 		efi.systab->hdr.revision >> 16,
580 		efi.systab->hdr.revision & 0xffff, vendor);
581 
582 	if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
583 		return;
584 
585 	if (efi_config_init(arch_tables))
586 		return;
587 
588 	/*
589 	 * Note: We currently don't support runtime services on an EFI
590 	 * that doesn't match the kernel 32/64-bit mode.
591 	 */
592 
593 	if (!efi_runtime_supported())
594 		pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
595 	else {
596 		if (efi_runtime_disabled() || efi_runtime_init()) {
597 			efi_memmap_unmap();
598 			return;
599 		}
600 	}
601 
602 	efi_clean_memmap();
603 
604 	if (efi_enabled(EFI_DBG))
605 		efi_print_memmap();
606 }
607 
608 void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
609 {
610 	u64 addr, npages;
611 
612 	addr = md->virt_addr;
613 	npages = md->num_pages;
614 
615 	memrange_efi_to_native(&addr, &npages);
616 
617 	if (executable)
618 		set_memory_x(addr, npages);
619 	else
620 		set_memory_nx(addr, npages);
621 }
622 
623 void __init runtime_code_page_mkexec(void)
624 {
625 	efi_memory_desc_t *md;
626 
627 	/* Make EFI runtime service code area executable */
628 	for_each_efi_memory_desc(md) {
629 		if (md->type != EFI_RUNTIME_SERVICES_CODE)
630 			continue;
631 
632 		efi_set_executable(md, true);
633 	}
634 }
635 
636 void __init efi_memory_uc(u64 addr, unsigned long size)
637 {
638 	unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
639 	u64 npages;
640 
641 	npages = round_up(size, page_shift) / page_shift;
642 	memrange_efi_to_native(&addr, &npages);
643 	set_memory_uc(addr, npages);
644 }
645 
646 void __init old_map_region(efi_memory_desc_t *md)
647 {
648 	u64 start_pfn, end_pfn, end;
649 	unsigned long size;
650 	void *va;
651 
652 	start_pfn = PFN_DOWN(md->phys_addr);
653 	size	  = md->num_pages << PAGE_SHIFT;
654 	end	  = md->phys_addr + size;
655 	end_pfn   = PFN_UP(end);
656 
657 	if (pfn_range_is_mapped(start_pfn, end_pfn)) {
658 		va = __va(md->phys_addr);
659 
660 		if (!(md->attribute & EFI_MEMORY_WB))
661 			efi_memory_uc((u64)(unsigned long)va, size);
662 	} else
663 		va = efi_ioremap(md->phys_addr, size,
664 				 md->type, md->attribute);
665 
666 	md->virt_addr = (u64) (unsigned long) va;
667 	if (!va)
668 		pr_err("ioremap of 0x%llX failed!\n",
669 		       (unsigned long long)md->phys_addr);
670 }
671 
672 /* Merge contiguous regions of the same type and attribute */
673 static void __init efi_merge_regions(void)
674 {
675 	efi_memory_desc_t *md, *prev_md = NULL;
676 
677 	for_each_efi_memory_desc(md) {
678 		u64 prev_size;
679 
680 		if (!prev_md) {
681 			prev_md = md;
682 			continue;
683 		}
684 
685 		if (prev_md->type != md->type ||
686 		    prev_md->attribute != md->attribute) {
687 			prev_md = md;
688 			continue;
689 		}
690 
691 		prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
692 
693 		if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
694 			prev_md->num_pages += md->num_pages;
695 			md->type = EFI_RESERVED_TYPE;
696 			md->attribute = 0;
697 			continue;
698 		}
699 		prev_md = md;
700 	}
701 }
702 
703 static void __init get_systab_virt_addr(efi_memory_desc_t *md)
704 {
705 	unsigned long size;
706 	u64 end, systab;
707 
708 	size = md->num_pages << EFI_PAGE_SHIFT;
709 	end = md->phys_addr + size;
710 	systab = (u64)(unsigned long)efi_phys.systab;
711 	if (md->phys_addr <= systab && systab < end) {
712 		systab += md->virt_addr - md->phys_addr;
713 		efi.systab = (efi_system_table_t *)(unsigned long)systab;
714 	}
715 }
716 
717 static void *realloc_pages(void *old_memmap, int old_shift)
718 {
719 	void *ret;
720 
721 	ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1);
722 	if (!ret)
723 		goto out;
724 
725 	/*
726 	 * A first-time allocation doesn't have anything to copy.
727 	 */
728 	if (!old_memmap)
729 		return ret;
730 
731 	memcpy(ret, old_memmap, PAGE_SIZE << old_shift);
732 
733 out:
734 	free_pages((unsigned long)old_memmap, old_shift);
735 	return ret;
736 }
737 
738 /*
739  * Iterate the EFI memory map in reverse order because the regions
740  * will be mapped top-down. The end result is the same as if we had
741  * mapped things forward, but doesn't require us to change the
742  * existing implementation of efi_map_region().
743  */
744 static inline void *efi_map_next_entry_reverse(void *entry)
745 {
746 	/* Initial call */
747 	if (!entry)
748 		return efi.memmap.map_end - efi.memmap.desc_size;
749 
750 	entry -= efi.memmap.desc_size;
751 	if (entry < efi.memmap.map)
752 		return NULL;
753 
754 	return entry;
755 }
756 
757 /*
758  * efi_map_next_entry - Return the next EFI memory map descriptor
759  * @entry: Previous EFI memory map descriptor
760  *
761  * This is a helper function to iterate over the EFI memory map, which
762  * we do in different orders depending on the current configuration.
763  *
764  * To begin traversing the memory map @entry must be %NULL.
765  *
766  * Returns %NULL when we reach the end of the memory map.
767  */
768 static void *efi_map_next_entry(void *entry)
769 {
770 	if (!efi_enabled(EFI_OLD_MEMMAP) && efi_enabled(EFI_64BIT)) {
771 		/*
772 		 * Starting in UEFI v2.5 the EFI_PROPERTIES_TABLE
773 		 * config table feature requires us to map all entries
774 		 * in the same order as they appear in the EFI memory
775 		 * map. That is to say, entry N must have a lower
776 		 * virtual address than entry N+1. This is because the
777 		 * firmware toolchain leaves relative references in
778 		 * the code/data sections, which are split and become
779 		 * separate EFI memory regions. Mapping things
780 		 * out-of-order leads to the firmware accessing
781 		 * unmapped addresses.
782 		 *
783 		 * Since we need to map things this way whether or not
784 		 * the kernel actually makes use of
785 		 * EFI_PROPERTIES_TABLE, let's just switch to this
786 		 * scheme by default for 64-bit.
787 		 */
788 		return efi_map_next_entry_reverse(entry);
789 	}
790 
791 	/* Initial call */
792 	if (!entry)
793 		return efi.memmap.map;
794 
795 	entry += efi.memmap.desc_size;
796 	if (entry >= efi.memmap.map_end)
797 		return NULL;
798 
799 	return entry;
800 }
801 
802 static bool should_map_region(efi_memory_desc_t *md)
803 {
804 	/*
805 	 * Runtime regions always require runtime mappings (obviously).
806 	 */
807 	if (md->attribute & EFI_MEMORY_RUNTIME)
808 		return true;
809 
810 	/*
811 	 * 32-bit EFI doesn't suffer from the bug that requires us to
812 	 * reserve boot services regions, and mixed mode support
813 	 * doesn't exist for 32-bit kernels.
814 	 */
815 	if (IS_ENABLED(CONFIG_X86_32))
816 		return false;
817 
818 	/*
819 	 * EFI specific purpose memory may be reserved by default
820 	 * depending on kernel config and boot options.
821 	 */
822 	if (md->type == EFI_CONVENTIONAL_MEMORY &&
823 	    efi_soft_reserve_enabled() &&
824 	    (md->attribute & EFI_MEMORY_SP))
825 		return false;
826 
827 	/*
828 	 * Map all of RAM so that we can access arguments in the 1:1
829 	 * mapping when making EFI runtime calls.
830 	 */
831 	if (IS_ENABLED(CONFIG_EFI_MIXED) && !efi_is_native()) {
832 		if (md->type == EFI_CONVENTIONAL_MEMORY ||
833 		    md->type == EFI_LOADER_DATA ||
834 		    md->type == EFI_LOADER_CODE)
835 			return true;
836 	}
837 
838 	/*
839 	 * Map boot services regions as a workaround for buggy
840 	 * firmware that accesses them even when they shouldn't.
841 	 *
842 	 * See efi_{reserve,free}_boot_services().
843 	 */
844 	if (md->type == EFI_BOOT_SERVICES_CODE ||
845 	    md->type == EFI_BOOT_SERVICES_DATA)
846 		return true;
847 
848 	return false;
849 }
850 
851 /*
852  * Map the efi memory ranges of the runtime services and update new_mmap with
853  * virtual addresses.
854  */
855 static void * __init efi_map_regions(int *count, int *pg_shift)
856 {
857 	void *p, *new_memmap = NULL;
858 	unsigned long left = 0;
859 	unsigned long desc_size;
860 	efi_memory_desc_t *md;
861 
862 	desc_size = efi.memmap.desc_size;
863 
864 	p = NULL;
865 	while ((p = efi_map_next_entry(p))) {
866 		md = p;
867 
868 		if (!should_map_region(md))
869 			continue;
870 
871 		efi_map_region(md);
872 		get_systab_virt_addr(md);
873 
874 		if (left < desc_size) {
875 			new_memmap = realloc_pages(new_memmap, *pg_shift);
876 			if (!new_memmap)
877 				return NULL;
878 
879 			left += PAGE_SIZE << *pg_shift;
880 			(*pg_shift)++;
881 		}
882 
883 		memcpy(new_memmap + (*count * desc_size), md, desc_size);
884 
885 		left -= desc_size;
886 		(*count)++;
887 	}
888 
889 	return new_memmap;
890 }
891 
892 static void __init kexec_enter_virtual_mode(void)
893 {
894 #ifdef CONFIG_KEXEC_CORE
895 	efi_memory_desc_t *md;
896 	unsigned int num_pages;
897 
898 	efi.systab = NULL;
899 
900 	/*
901 	 * We don't do virtual mode, since we don't do runtime services, on
902 	 * non-native EFI. With efi=old_map, we don't do runtime services in
903 	 * kexec kernel because in the initial boot something else might
904 	 * have been mapped at these virtual addresses.
905 	 */
906 	if (!efi_is_native() || efi_enabled(EFI_OLD_MEMMAP)) {
907 		efi_memmap_unmap();
908 		clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
909 		return;
910 	}
911 
912 	if (efi_alloc_page_tables()) {
913 		pr_err("Failed to allocate EFI page tables\n");
914 		clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
915 		return;
916 	}
917 
918 	/*
919 	* Map efi regions which were passed via setup_data. The virt_addr is a
920 	* fixed addr which was used in first kernel of a kexec boot.
921 	*/
922 	for_each_efi_memory_desc(md) {
923 		efi_map_region_fixed(md); /* FIXME: add error handling */
924 		get_systab_virt_addr(md);
925 	}
926 
927 	/*
928 	 * Unregister the early EFI memmap from efi_init() and install
929 	 * the new EFI memory map.
930 	 */
931 	efi_memmap_unmap();
932 
933 	if (efi_memmap_init_late(efi.memmap.phys_map,
934 				 efi.memmap.desc_size * efi.memmap.nr_map)) {
935 		pr_err("Failed to remap late EFI memory map\n");
936 		clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
937 		return;
938 	}
939 
940 	BUG_ON(!efi.systab);
941 
942 	num_pages = ALIGN(efi.memmap.nr_map * efi.memmap.desc_size, PAGE_SIZE);
943 	num_pages >>= PAGE_SHIFT;
944 
945 	if (efi_setup_page_tables(efi.memmap.phys_map, num_pages)) {
946 		clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
947 		return;
948 	}
949 
950 	efi_sync_low_kernel_mappings();
951 
952 	/*
953 	 * Now that EFI is in virtual mode, update the function
954 	 * pointers in the runtime service table to the new virtual addresses.
955 	 *
956 	 * Call EFI services through wrapper functions.
957 	 */
958 	efi.runtime_version = efi_systab.hdr.revision;
959 
960 	efi_native_runtime_setup();
961 
962 	efi.set_virtual_address_map = NULL;
963 
964 	if (efi_enabled(EFI_OLD_MEMMAP) && (__supported_pte_mask & _PAGE_NX))
965 		runtime_code_page_mkexec();
966 #endif
967 }
968 
969 /*
970  * This function will switch the EFI runtime services to virtual mode.
971  * Essentially, we look through the EFI memmap and map every region that
972  * has the runtime attribute bit set in its memory descriptor into the
973  * efi_pgd page table.
974  *
975  * The old method which used to update that memory descriptor with the
976  * virtual address obtained from ioremap() is still supported when the
977  * kernel is booted with efi=old_map on its command line. Same old
978  * method enabled the runtime services to be called without having to
979  * thunk back into physical mode for every invocation.
980  *
981  * The new method does a pagetable switch in a preemption-safe manner
982  * so that we're in a different address space when calling a runtime
983  * function. For function arguments passing we do copy the PUDs of the
984  * kernel page table into efi_pgd prior to each call.
985  *
986  * Specially for kexec boot, efi runtime maps in previous kernel should
987  * be passed in via setup_data. In that case runtime ranges will be mapped
988  * to the same virtual addresses as the first kernel, see
989  * kexec_enter_virtual_mode().
990  */
991 static void __init __efi_enter_virtual_mode(void)
992 {
993 	int count = 0, pg_shift = 0;
994 	void *new_memmap = NULL;
995 	efi_status_t status;
996 	unsigned long pa;
997 
998 	efi.systab = NULL;
999 
1000 	if (efi_alloc_page_tables()) {
1001 		pr_err("Failed to allocate EFI page tables\n");
1002 		clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
1003 		return;
1004 	}
1005 
1006 	efi_merge_regions();
1007 	new_memmap = efi_map_regions(&count, &pg_shift);
1008 	if (!new_memmap) {
1009 		pr_err("Error reallocating memory, EFI runtime non-functional!\n");
1010 		clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
1011 		return;
1012 	}
1013 
1014 	pa = __pa(new_memmap);
1015 
1016 	/*
1017 	 * Unregister the early EFI memmap from efi_init() and install
1018 	 * the new EFI memory map that we are about to pass to the
1019 	 * firmware via SetVirtualAddressMap().
1020 	 */
1021 	efi_memmap_unmap();
1022 
1023 	if (efi_memmap_init_late(pa, efi.memmap.desc_size * count)) {
1024 		pr_err("Failed to remap late EFI memory map\n");
1025 		clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
1026 		return;
1027 	}
1028 
1029 	if (efi_enabled(EFI_DBG)) {
1030 		pr_info("EFI runtime memory map:\n");
1031 		efi_print_memmap();
1032 	}
1033 
1034 	BUG_ON(!efi.systab);
1035 
1036 	if (efi_setup_page_tables(pa, 1 << pg_shift)) {
1037 		clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
1038 		return;
1039 	}
1040 
1041 	efi_sync_low_kernel_mappings();
1042 
1043 	if (efi_is_native()) {
1044 		status = phys_efi_set_virtual_address_map(
1045 				efi.memmap.desc_size * count,
1046 				efi.memmap.desc_size,
1047 				efi.memmap.desc_version,
1048 				(efi_memory_desc_t *)pa);
1049 	} else {
1050 		status = efi_thunk_set_virtual_address_map(
1051 				efi_phys.set_virtual_address_map,
1052 				efi.memmap.desc_size * count,
1053 				efi.memmap.desc_size,
1054 				efi.memmap.desc_version,
1055 				(efi_memory_desc_t *)pa);
1056 	}
1057 
1058 	if (status != EFI_SUCCESS) {
1059 		pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
1060 			 status);
1061 		panic("EFI call to SetVirtualAddressMap() failed!");
1062 	}
1063 
1064 	efi_free_boot_services();
1065 
1066 	/*
1067 	 * Now that EFI is in virtual mode, update the function
1068 	 * pointers in the runtime service table to the new virtual addresses.
1069 	 *
1070 	 * Call EFI services through wrapper functions.
1071 	 */
1072 	efi.runtime_version = efi_systab.hdr.revision;
1073 
1074 	if (efi_is_native())
1075 		efi_native_runtime_setup();
1076 	else
1077 		efi_thunk_runtime_setup();
1078 
1079 	efi.set_virtual_address_map = NULL;
1080 
1081 	/*
1082 	 * Apply more restrictive page table mapping attributes now that
1083 	 * SVAM() has been called and the firmware has performed all
1084 	 * necessary relocation fixups for the new virtual addresses.
1085 	 */
1086 	efi_runtime_update_mappings();
1087 
1088 	/* clean DUMMY object */
1089 	efi_delete_dummy_variable();
1090 }
1091 
1092 void __init efi_enter_virtual_mode(void)
1093 {
1094 	if (efi_enabled(EFI_PARAVIRT))
1095 		return;
1096 
1097 	if (efi_setup)
1098 		kexec_enter_virtual_mode();
1099 	else
1100 		__efi_enter_virtual_mode();
1101 
1102 	efi_dump_pagetable();
1103 }
1104 
1105 static int __init arch_parse_efi_cmdline(char *str)
1106 {
1107 	if (!str) {
1108 		pr_warn("need at least one option\n");
1109 		return -EINVAL;
1110 	}
1111 
1112 	if (parse_option_str(str, "old_map"))
1113 		set_bit(EFI_OLD_MEMMAP, &efi.flags);
1114 
1115 	return 0;
1116 }
1117 early_param("efi", arch_parse_efi_cmdline);
1118 
1119 bool efi_is_table_address(unsigned long phys_addr)
1120 {
1121 	unsigned int i;
1122 
1123 	if (phys_addr == EFI_INVALID_TABLE_ADDR)
1124 		return false;
1125 
1126 	for (i = 0; i < ARRAY_SIZE(efi_tables); i++)
1127 		if (*(efi_tables[i]) == phys_addr)
1128 			return true;
1129 
1130 	return false;
1131 }
1132