12e0eb483SAtish Patra // SPDX-License-Identifier: GPL-2.0-only
22e0eb483SAtish Patra /*
32e0eb483SAtish Patra  * EFI stub implementation that is shared by arm and arm64 architectures.
42e0eb483SAtish Patra  * This should be #included by the EFI stub implementation files.
52e0eb483SAtish Patra  *
62e0eb483SAtish Patra  * Copyright (C) 2013,2014 Linaro Limited
72e0eb483SAtish Patra  *     Roy Franz <roy.franz@linaro.org
82e0eb483SAtish Patra  * Copyright (C) 2013 Red Hat, Inc.
92e0eb483SAtish Patra  *     Mark Salter <msalter@redhat.com>
102e0eb483SAtish Patra  */
112e0eb483SAtish Patra 
122e0eb483SAtish Patra #include <linux/efi.h>
132e0eb483SAtish Patra #include <linux/libfdt.h>
142e0eb483SAtish Patra #include <asm/efi.h>
152e0eb483SAtish Patra 
162e0eb483SAtish Patra #include "efistub.h"
172e0eb483SAtish Patra 
182e0eb483SAtish Patra /*
192e0eb483SAtish Patra  * This is the base address at which to start allocating virtual memory ranges
202e0eb483SAtish Patra  * for UEFI Runtime Services. This is in the low TTBR0 range so that we can use
212e0eb483SAtish Patra  * any allocation we choose, and eliminate the risk of a conflict after kexec.
222e0eb483SAtish Patra  * The value chosen is the largest non-zero power of 2 suitable for this purpose
232e0eb483SAtish Patra  * both on 32-bit and 64-bit ARM CPUs, to maximize the likelihood that it can
242e0eb483SAtish Patra  * be mapped efficiently.
252e0eb483SAtish Patra  * Since 32-bit ARM could potentially execute with a 1G/3G user/kernel split,
262e0eb483SAtish Patra  * map everything below 1 GB. (512 MB is a reasonable upper bound for the
272e0eb483SAtish Patra  * entire footprint of the UEFI runtime services memory regions)
282e0eb483SAtish Patra  */
292e0eb483SAtish Patra #define EFI_RT_VIRTUAL_BASE	SZ_512M
302e0eb483SAtish Patra #define EFI_RT_VIRTUAL_SIZE	SZ_512M
312e0eb483SAtish Patra 
322e0eb483SAtish Patra #ifdef CONFIG_ARM64
332e0eb483SAtish Patra # define EFI_RT_VIRTUAL_LIMIT	DEFAULT_MAP_WINDOW_64
342e0eb483SAtish Patra #else
352e0eb483SAtish Patra # define EFI_RT_VIRTUAL_LIMIT	TASK_SIZE
362e0eb483SAtish Patra #endif
372e0eb483SAtish Patra 
382e0eb483SAtish Patra static u64 virtmap_base = EFI_RT_VIRTUAL_BASE;
392e0eb483SAtish Patra static bool __efistub_global flat_va_mapping;
402e0eb483SAtish Patra 
412e0eb483SAtish Patra static efi_system_table_t *__efistub_global sys_table;
422e0eb483SAtish Patra 
432e0eb483SAtish Patra __pure efi_system_table_t *efi_system_table(void)
442e0eb483SAtish Patra {
452e0eb483SAtish Patra 	return sys_table;
462e0eb483SAtish Patra }
472e0eb483SAtish Patra 
482e0eb483SAtish Patra static struct screen_info *setup_graphics(void)
492e0eb483SAtish Patra {
502e0eb483SAtish Patra 	efi_guid_t gop_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
512e0eb483SAtish Patra 	efi_status_t status;
522e0eb483SAtish Patra 	unsigned long size;
532e0eb483SAtish Patra 	void **gop_handle = NULL;
542e0eb483SAtish Patra 	struct screen_info *si = NULL;
552e0eb483SAtish Patra 
562e0eb483SAtish Patra 	size = 0;
572e0eb483SAtish Patra 	status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
582e0eb483SAtish Patra 			     &gop_proto, NULL, &size, gop_handle);
592e0eb483SAtish Patra 	if (status == EFI_BUFFER_TOO_SMALL) {
602e0eb483SAtish Patra 		si = alloc_screen_info();
612e0eb483SAtish Patra 		if (!si)
622e0eb483SAtish Patra 			return NULL;
632e0eb483SAtish Patra 		efi_setup_gop(si, &gop_proto, size);
642e0eb483SAtish Patra 	}
652e0eb483SAtish Patra 	return si;
662e0eb483SAtish Patra }
672e0eb483SAtish Patra 
682e0eb483SAtish Patra void install_memreserve_table(void)
692e0eb483SAtish Patra {
702e0eb483SAtish Patra 	struct linux_efi_memreserve *rsv;
712e0eb483SAtish Patra 	efi_guid_t memreserve_table_guid = LINUX_EFI_MEMRESERVE_TABLE_GUID;
722e0eb483SAtish Patra 	efi_status_t status;
732e0eb483SAtish Patra 
742e0eb483SAtish Patra 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(*rsv),
752e0eb483SAtish Patra 			     (void **)&rsv);
762e0eb483SAtish Patra 	if (status != EFI_SUCCESS) {
772e0eb483SAtish Patra 		pr_efi_err("Failed to allocate memreserve entry!\n");
782e0eb483SAtish Patra 		return;
792e0eb483SAtish Patra 	}
802e0eb483SAtish Patra 
812e0eb483SAtish Patra 	rsv->next = 0;
822e0eb483SAtish Patra 	rsv->size = 0;
832e0eb483SAtish Patra 	atomic_set(&rsv->count, 0);
842e0eb483SAtish Patra 
852e0eb483SAtish Patra 	status = efi_bs_call(install_configuration_table,
862e0eb483SAtish Patra 			     &memreserve_table_guid, rsv);
872e0eb483SAtish Patra 	if (status != EFI_SUCCESS)
882e0eb483SAtish Patra 		pr_efi_err("Failed to install memreserve config table!\n");
892e0eb483SAtish Patra }
902e0eb483SAtish Patra 
912e0eb483SAtish Patra static unsigned long get_dram_base(void)
922e0eb483SAtish Patra {
932e0eb483SAtish Patra 	efi_status_t status;
942e0eb483SAtish Patra 	unsigned long map_size, buff_size;
952e0eb483SAtish Patra 	unsigned long membase  = EFI_ERROR;
962e0eb483SAtish Patra 	struct efi_memory_map map;
972e0eb483SAtish Patra 	efi_memory_desc_t *md;
982e0eb483SAtish Patra 	struct efi_boot_memmap boot_map;
992e0eb483SAtish Patra 
1002e0eb483SAtish Patra 	boot_map.map		= (efi_memory_desc_t **)&map.map;
1012e0eb483SAtish Patra 	boot_map.map_size	= &map_size;
1022e0eb483SAtish Patra 	boot_map.desc_size	= &map.desc_size;
1032e0eb483SAtish Patra 	boot_map.desc_ver	= NULL;
1042e0eb483SAtish Patra 	boot_map.key_ptr	= NULL;
1052e0eb483SAtish Patra 	boot_map.buff_size	= &buff_size;
1062e0eb483SAtish Patra 
1072e0eb483SAtish Patra 	status = efi_get_memory_map(&boot_map);
1082e0eb483SAtish Patra 	if (status != EFI_SUCCESS)
1092e0eb483SAtish Patra 		return membase;
1102e0eb483SAtish Patra 
1112e0eb483SAtish Patra 	map.map_end = map.map + map_size;
1122e0eb483SAtish Patra 
1132e0eb483SAtish Patra 	for_each_efi_memory_desc_in_map(&map, md) {
1142e0eb483SAtish Patra 		if (md->attribute & EFI_MEMORY_WB) {
1152e0eb483SAtish Patra 			if (membase > md->phys_addr)
1162e0eb483SAtish Patra 				membase = md->phys_addr;
1172e0eb483SAtish Patra 		}
1182e0eb483SAtish Patra 	}
1192e0eb483SAtish Patra 
1202e0eb483SAtish Patra 	efi_bs_call(free_pool, map.map);
1212e0eb483SAtish Patra 
1222e0eb483SAtish Patra 	return membase;
1232e0eb483SAtish Patra }
1242e0eb483SAtish Patra 
1252e0eb483SAtish Patra /*
1262e0eb483SAtish Patra  * This function handles the architcture specific differences between arm and
1272e0eb483SAtish Patra  * arm64 regarding where the kernel image must be loaded and any memory that
1282e0eb483SAtish Patra  * must be reserved. On failure it is required to free all
1292e0eb483SAtish Patra  * all allocations it has made.
1302e0eb483SAtish Patra  */
1312e0eb483SAtish Patra efi_status_t handle_kernel_image(unsigned long *image_addr,
1322e0eb483SAtish Patra 				 unsigned long *image_size,
1332e0eb483SAtish Patra 				 unsigned long *reserve_addr,
1342e0eb483SAtish Patra 				 unsigned long *reserve_size,
1352e0eb483SAtish Patra 				 unsigned long dram_base,
1362e0eb483SAtish Patra 				 efi_loaded_image_t *image);
1372e0eb483SAtish Patra 
1382e0eb483SAtish Patra asmlinkage void __noreturn efi_enter_kernel(unsigned long entrypoint,
1392e0eb483SAtish Patra 					    unsigned long fdt_addr,
1402e0eb483SAtish Patra 					    unsigned long fdt_size);
1412e0eb483SAtish Patra 
1422e0eb483SAtish Patra /*
1432e0eb483SAtish Patra  * EFI entry point for the arm/arm64 EFI stubs.  This is the entrypoint
1442e0eb483SAtish Patra  * that is described in the PE/COFF header.  Most of the code is the same
1452e0eb483SAtish Patra  * for both archictectures, with the arch-specific code provided in the
1462e0eb483SAtish Patra  * handle_kernel_image() function.
1472e0eb483SAtish Patra  */
1482e0eb483SAtish Patra efi_status_t efi_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg)
1492e0eb483SAtish Patra {
1502e0eb483SAtish Patra 	efi_loaded_image_t *image;
1512e0eb483SAtish Patra 	efi_status_t status;
1522e0eb483SAtish Patra 	unsigned long image_addr;
1532e0eb483SAtish Patra 	unsigned long image_size = 0;
1542e0eb483SAtish Patra 	unsigned long dram_base;
1552e0eb483SAtish Patra 	/* addr/point and size pairs for memory management*/
1562e0eb483SAtish Patra 	unsigned long initrd_addr = 0;
1572e0eb483SAtish Patra 	unsigned long initrd_size = 0;
1582e0eb483SAtish Patra 	unsigned long fdt_addr = 0;  /* Original DTB */
1592e0eb483SAtish Patra 	unsigned long fdt_size = 0;
1602e0eb483SAtish Patra 	char *cmdline_ptr = NULL;
1612e0eb483SAtish Patra 	int cmdline_size = 0;
1622e0eb483SAtish Patra 	efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID;
1632e0eb483SAtish Patra 	unsigned long reserve_addr = 0;
1642e0eb483SAtish Patra 	unsigned long reserve_size = 0;
1652e0eb483SAtish Patra 	enum efi_secureboot_mode secure_boot;
1662e0eb483SAtish Patra 	struct screen_info *si;
1672e0eb483SAtish Patra 	efi_properties_table_t *prop_tbl;
1682e0eb483SAtish Patra 	unsigned long max_addr;
1692e0eb483SAtish Patra 
1702e0eb483SAtish Patra 	sys_table = sys_table_arg;
1712e0eb483SAtish Patra 
1722e0eb483SAtish Patra 	/* Check if we were booted by the EFI firmware */
1732e0eb483SAtish Patra 	if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
1742e0eb483SAtish Patra 		status = EFI_INVALID_PARAMETER;
1752e0eb483SAtish Patra 		goto fail;
1762e0eb483SAtish Patra 	}
1772e0eb483SAtish Patra 
1782e0eb483SAtish Patra 	status = check_platform_features();
1792e0eb483SAtish Patra 	if (status != EFI_SUCCESS)
1802e0eb483SAtish Patra 		goto fail;
1812e0eb483SAtish Patra 
1822e0eb483SAtish Patra 	/*
1832e0eb483SAtish Patra 	 * Get a handle to the loaded image protocol.  This is used to get
1842e0eb483SAtish Patra 	 * information about the running image, such as size and the command
1852e0eb483SAtish Patra 	 * line.
1862e0eb483SAtish Patra 	 */
1872e0eb483SAtish Patra 	status = sys_table->boottime->handle_protocol(handle,
1882e0eb483SAtish Patra 					&loaded_image_proto, (void *)&image);
1892e0eb483SAtish Patra 	if (status != EFI_SUCCESS) {
1902e0eb483SAtish Patra 		pr_efi_err("Failed to get loaded image protocol\n");
1912e0eb483SAtish Patra 		goto fail;
1922e0eb483SAtish Patra 	}
1932e0eb483SAtish Patra 
1942e0eb483SAtish Patra 	dram_base = get_dram_base();
1952e0eb483SAtish Patra 	if (dram_base == EFI_ERROR) {
1962e0eb483SAtish Patra 		pr_efi_err("Failed to find DRAM base\n");
1972e0eb483SAtish Patra 		status = EFI_LOAD_ERROR;
1982e0eb483SAtish Patra 		goto fail;
1992e0eb483SAtish Patra 	}
2002e0eb483SAtish Patra 
2012e0eb483SAtish Patra 	/*
2022e0eb483SAtish Patra 	 * Get the command line from EFI, using the LOADED_IMAGE
2032e0eb483SAtish Patra 	 * protocol. We are going to copy the command line into the
2042e0eb483SAtish Patra 	 * device tree, so this can be allocated anywhere.
2052e0eb483SAtish Patra 	 */
2062e0eb483SAtish Patra 	cmdline_ptr = efi_convert_cmdline(image, &cmdline_size, ULONG_MAX);
2072e0eb483SAtish Patra 	if (!cmdline_ptr) {
2082e0eb483SAtish Patra 		pr_efi_err("getting command line via LOADED_IMAGE_PROTOCOL\n");
2092e0eb483SAtish Patra 		status = EFI_OUT_OF_RESOURCES;
2102e0eb483SAtish Patra 		goto fail;
2112e0eb483SAtish Patra 	}
2122e0eb483SAtish Patra 
2132e0eb483SAtish Patra 	if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) ||
2142e0eb483SAtish Patra 	    IS_ENABLED(CONFIG_CMDLINE_FORCE) ||
2152e0eb483SAtish Patra 	    cmdline_size == 0)
2162e0eb483SAtish Patra 		efi_parse_options(CONFIG_CMDLINE);
2172e0eb483SAtish Patra 
2182e0eb483SAtish Patra 	if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && cmdline_size > 0)
2192e0eb483SAtish Patra 		efi_parse_options(cmdline_ptr);
2202e0eb483SAtish Patra 
2212e0eb483SAtish Patra 	pr_efi("Booting Linux Kernel...\n");
2222e0eb483SAtish Patra 
2232e0eb483SAtish Patra 	si = setup_graphics();
2242e0eb483SAtish Patra 
2252e0eb483SAtish Patra 	status = handle_kernel_image(&image_addr, &image_size,
2262e0eb483SAtish Patra 				     &reserve_addr,
2272e0eb483SAtish Patra 				     &reserve_size,
2282e0eb483SAtish Patra 				     dram_base, image);
2292e0eb483SAtish Patra 	if (status != EFI_SUCCESS) {
2302e0eb483SAtish Patra 		pr_efi_err("Failed to relocate kernel\n");
2312e0eb483SAtish Patra 		goto fail_free_cmdline;
2322e0eb483SAtish Patra 	}
2332e0eb483SAtish Patra 
2342e0eb483SAtish Patra 	efi_retrieve_tpm2_eventlog();
2352e0eb483SAtish Patra 
2362e0eb483SAtish Patra 	/* Ask the firmware to clear memory on unclean shutdown */
2372e0eb483SAtish Patra 	efi_enable_reset_attack_mitigation();
2382e0eb483SAtish Patra 
2392e0eb483SAtish Patra 	secure_boot = efi_get_secureboot();
2402e0eb483SAtish Patra 
2412e0eb483SAtish Patra 	/*
2422e0eb483SAtish Patra 	 * Unauthenticated device tree data is a security hazard, so ignore
2432e0eb483SAtish Patra 	 * 'dtb=' unless UEFI Secure Boot is disabled.  We assume that secure
2442e0eb483SAtish Patra 	 * boot is enabled if we can't determine its state.
2452e0eb483SAtish Patra 	 */
2462e0eb483SAtish Patra 	if (!IS_ENABLED(CONFIG_EFI_ARMSTUB_DTB_LOADER) ||
2472e0eb483SAtish Patra 	     secure_boot != efi_secureboot_mode_disabled) {
2482e0eb483SAtish Patra 		if (strstr(cmdline_ptr, "dtb="))
2492e0eb483SAtish Patra 			pr_efi("Ignoring DTB from command line.\n");
2502e0eb483SAtish Patra 	} else {
2512e0eb483SAtish Patra 		status = efi_load_dtb(image, &fdt_addr, &fdt_size);
2522e0eb483SAtish Patra 
2532e0eb483SAtish Patra 		if (status != EFI_SUCCESS) {
2542e0eb483SAtish Patra 			pr_efi_err("Failed to load device tree!\n");
2552e0eb483SAtish Patra 			goto fail_free_image;
2562e0eb483SAtish Patra 		}
2572e0eb483SAtish Patra 	}
2582e0eb483SAtish Patra 
2592e0eb483SAtish Patra 	if (fdt_addr) {
2602e0eb483SAtish Patra 		pr_efi("Using DTB from command line\n");
2612e0eb483SAtish Patra 	} else {
2622e0eb483SAtish Patra 		/* Look for a device tree configuration table entry. */
2632e0eb483SAtish Patra 		fdt_addr = (uintptr_t)get_fdt(&fdt_size);
2642e0eb483SAtish Patra 		if (fdt_addr)
2652e0eb483SAtish Patra 			pr_efi("Using DTB from configuration table\n");
2662e0eb483SAtish Patra 	}
2672e0eb483SAtish Patra 
2682e0eb483SAtish Patra 	if (!fdt_addr)
2692e0eb483SAtish Patra 		pr_efi("Generating empty DTB\n");
2702e0eb483SAtish Patra 
2712e0eb483SAtish Patra 	if (!noinitrd()) {
2722e0eb483SAtish Patra 		max_addr = efi_get_max_initrd_addr(dram_base, image_addr);
2732e0eb483SAtish Patra 		status = efi_load_initrd_dev_path(&initrd_addr, &initrd_size,
2742e0eb483SAtish Patra 						  max_addr);
2752e0eb483SAtish Patra 		if (status == EFI_SUCCESS) {
2762e0eb483SAtish Patra 			pr_efi("Loaded initrd from LINUX_EFI_INITRD_MEDIA_GUID device path\n");
2772e0eb483SAtish Patra 		} else if (status == EFI_NOT_FOUND) {
2782e0eb483SAtish Patra 			status = efi_load_initrd(image, &initrd_addr, &initrd_size,
2792e0eb483SAtish Patra 						 ULONG_MAX, max_addr);
2802e0eb483SAtish Patra 			if (status == EFI_SUCCESS && initrd_size > 0)
2812e0eb483SAtish Patra 				pr_efi("Loaded initrd from command line option\n");
2822e0eb483SAtish Patra 		}
2832e0eb483SAtish Patra 		if (status != EFI_SUCCESS)
2842e0eb483SAtish Patra 			pr_efi_err("Failed to load initrd!\n");
2852e0eb483SAtish Patra 	}
2862e0eb483SAtish Patra 
2872e0eb483SAtish Patra 	efi_random_get_seed();
2882e0eb483SAtish Patra 
2892e0eb483SAtish Patra 	/*
2902e0eb483SAtish Patra 	 * If the NX PE data feature is enabled in the properties table, we
2912e0eb483SAtish Patra 	 * should take care not to create a virtual mapping that changes the
2922e0eb483SAtish Patra 	 * relative placement of runtime services code and data regions, as
2932e0eb483SAtish Patra 	 * they may belong to the same PE/COFF executable image in memory.
2942e0eb483SAtish Patra 	 * The easiest way to achieve that is to simply use a 1:1 mapping.
2952e0eb483SAtish Patra 	 */
2962e0eb483SAtish Patra 	prop_tbl = get_efi_config_table(EFI_PROPERTIES_TABLE_GUID);
2972e0eb483SAtish Patra 	flat_va_mapping = prop_tbl &&
2982e0eb483SAtish Patra 			  (prop_tbl->memory_protection_attribute &
2992e0eb483SAtish Patra 			   EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA);
3002e0eb483SAtish Patra 
3012e0eb483SAtish Patra 	/* hibernation expects the runtime regions to stay in the same place */
3022e0eb483SAtish Patra 	if (!IS_ENABLED(CONFIG_HIBERNATION) && !nokaslr() && !flat_va_mapping) {
3032e0eb483SAtish Patra 		/*
3042e0eb483SAtish Patra 		 * Randomize the base of the UEFI runtime services region.
3052e0eb483SAtish Patra 		 * Preserve the 2 MB alignment of the region by taking a
3062e0eb483SAtish Patra 		 * shift of 21 bit positions into account when scaling
3072e0eb483SAtish Patra 		 * the headroom value using a 32-bit random value.
3082e0eb483SAtish Patra 		 */
3092e0eb483SAtish Patra 		static const u64 headroom = EFI_RT_VIRTUAL_LIMIT -
3102e0eb483SAtish Patra 					    EFI_RT_VIRTUAL_BASE -
3112e0eb483SAtish Patra 					    EFI_RT_VIRTUAL_SIZE;
3122e0eb483SAtish Patra 		u32 rnd;
3132e0eb483SAtish Patra 
3142e0eb483SAtish Patra 		status = efi_get_random_bytes(sizeof(rnd), (u8 *)&rnd);
3152e0eb483SAtish Patra 		if (status == EFI_SUCCESS) {
3162e0eb483SAtish Patra 			virtmap_base = EFI_RT_VIRTUAL_BASE +
3172e0eb483SAtish Patra 				       (((headroom >> 21) * rnd) >> (32 - 21));
3182e0eb483SAtish Patra 		}
3192e0eb483SAtish Patra 	}
3202e0eb483SAtish Patra 
3212e0eb483SAtish Patra 	install_memreserve_table();
3222e0eb483SAtish Patra 
3232e0eb483SAtish Patra 	status = allocate_new_fdt_and_exit_boot(handle, &fdt_addr,
3242e0eb483SAtish Patra 						efi_get_max_fdt_addr(dram_base),
3252e0eb483SAtish Patra 						initrd_addr, initrd_size,
3262e0eb483SAtish Patra 						cmdline_ptr, fdt_addr, fdt_size);
3272e0eb483SAtish Patra 	if (status != EFI_SUCCESS)
3282e0eb483SAtish Patra 		goto fail_free_initrd;
3292e0eb483SAtish Patra 
3302e0eb483SAtish Patra 	efi_enter_kernel(image_addr, fdt_addr, fdt_totalsize((void *)fdt_addr));
3312e0eb483SAtish Patra 	/* not reached */
3322e0eb483SAtish Patra 
3332e0eb483SAtish Patra fail_free_initrd:
3342e0eb483SAtish Patra 	pr_efi_err("Failed to update FDT and exit boot services\n");
3352e0eb483SAtish Patra 
3362e0eb483SAtish Patra 	efi_free(initrd_size, initrd_addr);
3372e0eb483SAtish Patra 	efi_free(fdt_size, fdt_addr);
3382e0eb483SAtish Patra 
3392e0eb483SAtish Patra fail_free_image:
3402e0eb483SAtish Patra 	efi_free(image_size, image_addr);
3412e0eb483SAtish Patra 	efi_free(reserve_size, reserve_addr);
3422e0eb483SAtish Patra fail_free_cmdline:
3432e0eb483SAtish Patra 	free_screen_info(si);
3442e0eb483SAtish Patra 	efi_free(cmdline_size, (unsigned long)cmdline_ptr);
3452e0eb483SAtish Patra fail:
3462e0eb483SAtish Patra 	return status;
3472e0eb483SAtish Patra }
3482e0eb483SAtish Patra 
3492e0eb483SAtish Patra /*
3502e0eb483SAtish Patra  * efi_get_virtmap() - create a virtual mapping for the EFI memory map
3512e0eb483SAtish Patra  *
3522e0eb483SAtish Patra  * This function populates the virt_addr fields of all memory region descriptors
3532e0eb483SAtish Patra  * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors
3542e0eb483SAtish Patra  * are also copied to @runtime_map, and their total count is returned in @count.
3552e0eb483SAtish Patra  */
3562e0eb483SAtish Patra void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
3572e0eb483SAtish Patra 		     unsigned long desc_size, efi_memory_desc_t *runtime_map,
3582e0eb483SAtish Patra 		     int *count)
3592e0eb483SAtish Patra {
3602e0eb483SAtish Patra 	u64 efi_virt_base = virtmap_base;
3612e0eb483SAtish Patra 	efi_memory_desc_t *in, *out = runtime_map;
3622e0eb483SAtish Patra 	int l;
3632e0eb483SAtish Patra 
3642e0eb483SAtish Patra 	for (l = 0; l < map_size; l += desc_size) {
3652e0eb483SAtish Patra 		u64 paddr, size;
3662e0eb483SAtish Patra 
3672e0eb483SAtish Patra 		in = (void *)memory_map + l;
3682e0eb483SAtish Patra 		if (!(in->attribute & EFI_MEMORY_RUNTIME))
3692e0eb483SAtish Patra 			continue;
3702e0eb483SAtish Patra 
3712e0eb483SAtish Patra 		paddr = in->phys_addr;
3722e0eb483SAtish Patra 		size = in->num_pages * EFI_PAGE_SIZE;
3732e0eb483SAtish Patra 
3742e0eb483SAtish Patra 		in->virt_addr = in->phys_addr;
3752e0eb483SAtish Patra 		if (novamap()) {
3762e0eb483SAtish Patra 			continue;
3772e0eb483SAtish Patra 		}
3782e0eb483SAtish Patra 
3792e0eb483SAtish Patra 		/*
3802e0eb483SAtish Patra 		 * Make the mapping compatible with 64k pages: this allows
3812e0eb483SAtish Patra 		 * a 4k page size kernel to kexec a 64k page size kernel and
3822e0eb483SAtish Patra 		 * vice versa.
3832e0eb483SAtish Patra 		 */
3842e0eb483SAtish Patra 		if (!flat_va_mapping) {
3852e0eb483SAtish Patra 
3862e0eb483SAtish Patra 			paddr = round_down(in->phys_addr, SZ_64K);
3872e0eb483SAtish Patra 			size += in->phys_addr - paddr;
3882e0eb483SAtish Patra 
3892e0eb483SAtish Patra 			/*
3902e0eb483SAtish Patra 			 * Avoid wasting memory on PTEs by choosing a virtual
3912e0eb483SAtish Patra 			 * base that is compatible with section mappings if this
3922e0eb483SAtish Patra 			 * region has the appropriate size and physical
3932e0eb483SAtish Patra 			 * alignment. (Sections are 2 MB on 4k granule kernels)
3942e0eb483SAtish Patra 			 */
3952e0eb483SAtish Patra 			if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M)
3962e0eb483SAtish Patra 				efi_virt_base = round_up(efi_virt_base, SZ_2M);
3972e0eb483SAtish Patra 			else
3982e0eb483SAtish Patra 				efi_virt_base = round_up(efi_virt_base, SZ_64K);
3992e0eb483SAtish Patra 
4002e0eb483SAtish Patra 			in->virt_addr += efi_virt_base - paddr;
4012e0eb483SAtish Patra 			efi_virt_base += size;
4022e0eb483SAtish Patra 		}
4032e0eb483SAtish Patra 
4042e0eb483SAtish Patra 		memcpy(out, in, desc_size);
4052e0eb483SAtish Patra 		out = (void *)out + desc_size;
4062e0eb483SAtish Patra 		++*count;
4072e0eb483SAtish Patra 	}
4082e0eb483SAtish Patra }
409