xref: /OK3568_Linux_fs/kernel/arch/x86/platform/efi/efi.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Common EFI (Extensible Firmware Interface) support functions
4*4882a593Smuzhiyun  * Based on Extensible Firmware Interface Specification version 1.0
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Copyright (C) 1999 VA Linux Systems
7*4882a593Smuzhiyun  * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
8*4882a593Smuzhiyun  * Copyright (C) 1999-2002 Hewlett-Packard Co.
9*4882a593Smuzhiyun  *	David Mosberger-Tang <davidm@hpl.hp.com>
10*4882a593Smuzhiyun  *	Stephane Eranian <eranian@hpl.hp.com>
11*4882a593Smuzhiyun  * Copyright (C) 2005-2008 Intel Co.
12*4882a593Smuzhiyun  *	Fenghua Yu <fenghua.yu@intel.com>
13*4882a593Smuzhiyun  *	Bibo Mao <bibo.mao@intel.com>
14*4882a593Smuzhiyun  *	Chandramouli Narayanan <mouli@linux.intel.com>
15*4882a593Smuzhiyun  *	Huang Ying <ying.huang@intel.com>
16*4882a593Smuzhiyun  * Copyright (C) 2013 SuSE Labs
17*4882a593Smuzhiyun  *	Borislav Petkov <bp@suse.de> - runtime services VA mapping
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  * Copied from efi_32.c to eliminate the duplicated code between EFI
20*4882a593Smuzhiyun  * 32/64 support code. --ying 2007-10-26
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * All EFI Runtime Services are not implemented yet as EFI only
23*4882a593Smuzhiyun  * supports physical mode addressing on SoftSDV. This is to be fixed
24*4882a593Smuzhiyun  * in a future version.  --drummond 1999-07-20
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * Implemented EFI runtime services and virtual mode calls.  --davidm
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  * Goutham Rao: <goutham.rao@intel.com>
29*4882a593Smuzhiyun  *	Skip non-WB memory and ignore empty memory ranges.
30*4882a593Smuzhiyun  */
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun #include <linux/kernel.h>
35*4882a593Smuzhiyun #include <linux/init.h>
36*4882a593Smuzhiyun #include <linux/efi.h>
37*4882a593Smuzhiyun #include <linux/efi-bgrt.h>
38*4882a593Smuzhiyun #include <linux/export.h>
39*4882a593Smuzhiyun #include <linux/memblock.h>
40*4882a593Smuzhiyun #include <linux/slab.h>
41*4882a593Smuzhiyun #include <linux/spinlock.h>
42*4882a593Smuzhiyun #include <linux/uaccess.h>
43*4882a593Smuzhiyun #include <linux/time.h>
44*4882a593Smuzhiyun #include <linux/io.h>
45*4882a593Smuzhiyun #include <linux/reboot.h>
46*4882a593Smuzhiyun #include <linux/bcd.h>
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #include <asm/setup.h>
49*4882a593Smuzhiyun #include <asm/efi.h>
50*4882a593Smuzhiyun #include <asm/e820/api.h>
51*4882a593Smuzhiyun #include <asm/time.h>
52*4882a593Smuzhiyun #include <asm/tlbflush.h>
53*4882a593Smuzhiyun #include <asm/x86_init.h>
54*4882a593Smuzhiyun #include <asm/uv/uv.h>
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun static unsigned long efi_systab_phys __initdata;
57*4882a593Smuzhiyun static unsigned long prop_phys = EFI_INVALID_TABLE_ADDR;
58*4882a593Smuzhiyun static unsigned long uga_phys = EFI_INVALID_TABLE_ADDR;
59*4882a593Smuzhiyun static unsigned long efi_runtime, efi_nr_tables;
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun unsigned long efi_fw_vendor, efi_config_table;
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun static const efi_config_table_type_t arch_tables[] __initconst = {
64*4882a593Smuzhiyun 	{EFI_PROPERTIES_TABLE_GUID,	&prop_phys,		"PROP"		},
65*4882a593Smuzhiyun 	{UGA_IO_PROTOCOL_GUID,		&uga_phys,		"UGA"		},
66*4882a593Smuzhiyun #ifdef CONFIG_X86_UV
67*4882a593Smuzhiyun 	{UV_SYSTEM_TABLE_GUID,		&uv_systab_phys,	"UVsystab"	},
68*4882a593Smuzhiyun #endif
69*4882a593Smuzhiyun 	{},
70*4882a593Smuzhiyun };
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun static const unsigned long * const efi_tables[] = {
73*4882a593Smuzhiyun 	&efi.acpi,
74*4882a593Smuzhiyun 	&efi.acpi20,
75*4882a593Smuzhiyun 	&efi.smbios,
76*4882a593Smuzhiyun 	&efi.smbios3,
77*4882a593Smuzhiyun 	&uga_phys,
78*4882a593Smuzhiyun #ifdef CONFIG_X86_UV
79*4882a593Smuzhiyun 	&uv_systab_phys,
80*4882a593Smuzhiyun #endif
81*4882a593Smuzhiyun 	&efi_fw_vendor,
82*4882a593Smuzhiyun 	&efi_runtime,
83*4882a593Smuzhiyun 	&efi_config_table,
84*4882a593Smuzhiyun 	&efi.esrt,
85*4882a593Smuzhiyun 	&prop_phys,
86*4882a593Smuzhiyun 	&efi_mem_attr_table,
87*4882a593Smuzhiyun #ifdef CONFIG_EFI_RCI2_TABLE
88*4882a593Smuzhiyun 	&rci2_table_phys,
89*4882a593Smuzhiyun #endif
90*4882a593Smuzhiyun 	&efi.tpm_log,
91*4882a593Smuzhiyun 	&efi.tpm_final_log,
92*4882a593Smuzhiyun 	&efi_rng_seed,
93*4882a593Smuzhiyun #ifdef CONFIG_LOAD_UEFI_KEYS
94*4882a593Smuzhiyun 	&efi.mokvar_table,
95*4882a593Smuzhiyun #endif
96*4882a593Smuzhiyun };
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun u64 efi_setup;		/* efi setup_data physical address */
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun static int add_efi_memmap __initdata;
setup_add_efi_memmap(char * arg)101*4882a593Smuzhiyun static int __init setup_add_efi_memmap(char *arg)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun 	add_efi_memmap = 1;
104*4882a593Smuzhiyun 	return 0;
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun early_param("add_efi_memmap", setup_add_efi_memmap);
107*4882a593Smuzhiyun 
efi_find_mirror(void)108*4882a593Smuzhiyun void __init efi_find_mirror(void)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun 	efi_memory_desc_t *md;
111*4882a593Smuzhiyun 	u64 mirror_size = 0, total_size = 0;
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	if (!efi_enabled(EFI_MEMMAP))
114*4882a593Smuzhiyun 		return;
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	for_each_efi_memory_desc(md) {
117*4882a593Smuzhiyun 		unsigned long long start = md->phys_addr;
118*4882a593Smuzhiyun 		unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 		total_size += size;
121*4882a593Smuzhiyun 		if (md->attribute & EFI_MEMORY_MORE_RELIABLE) {
122*4882a593Smuzhiyun 			memblock_mark_mirror(start, size);
123*4882a593Smuzhiyun 			mirror_size += size;
124*4882a593Smuzhiyun 		}
125*4882a593Smuzhiyun 	}
126*4882a593Smuzhiyun 	if (mirror_size)
127*4882a593Smuzhiyun 		pr_info("Memory: %lldM/%lldM mirrored memory\n",
128*4882a593Smuzhiyun 			mirror_size>>20, total_size>>20);
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun /*
132*4882a593Smuzhiyun  * Tell the kernel about the EFI memory map.  This might include
133*4882a593Smuzhiyun  * more than the max 128 entries that can fit in the passed in e820
134*4882a593Smuzhiyun  * legacy (zeropage) memory map, but the kernel's e820 table can hold
135*4882a593Smuzhiyun  * E820_MAX_ENTRIES.
136*4882a593Smuzhiyun  */
137*4882a593Smuzhiyun 
do_add_efi_memmap(void)138*4882a593Smuzhiyun static void __init do_add_efi_memmap(void)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun 	efi_memory_desc_t *md;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	if (!efi_enabled(EFI_MEMMAP))
143*4882a593Smuzhiyun 		return;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	for_each_efi_memory_desc(md) {
146*4882a593Smuzhiyun 		unsigned long long start = md->phys_addr;
147*4882a593Smuzhiyun 		unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
148*4882a593Smuzhiyun 		int e820_type;
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 		switch (md->type) {
151*4882a593Smuzhiyun 		case EFI_LOADER_CODE:
152*4882a593Smuzhiyun 		case EFI_LOADER_DATA:
153*4882a593Smuzhiyun 		case EFI_BOOT_SERVICES_CODE:
154*4882a593Smuzhiyun 		case EFI_BOOT_SERVICES_DATA:
155*4882a593Smuzhiyun 		case EFI_CONVENTIONAL_MEMORY:
156*4882a593Smuzhiyun 			if (efi_soft_reserve_enabled()
157*4882a593Smuzhiyun 			    && (md->attribute & EFI_MEMORY_SP))
158*4882a593Smuzhiyun 				e820_type = E820_TYPE_SOFT_RESERVED;
159*4882a593Smuzhiyun 			else if (md->attribute & EFI_MEMORY_WB)
160*4882a593Smuzhiyun 				e820_type = E820_TYPE_RAM;
161*4882a593Smuzhiyun 			else
162*4882a593Smuzhiyun 				e820_type = E820_TYPE_RESERVED;
163*4882a593Smuzhiyun 			break;
164*4882a593Smuzhiyun 		case EFI_ACPI_RECLAIM_MEMORY:
165*4882a593Smuzhiyun 			e820_type = E820_TYPE_ACPI;
166*4882a593Smuzhiyun 			break;
167*4882a593Smuzhiyun 		case EFI_ACPI_MEMORY_NVS:
168*4882a593Smuzhiyun 			e820_type = E820_TYPE_NVS;
169*4882a593Smuzhiyun 			break;
170*4882a593Smuzhiyun 		case EFI_UNUSABLE_MEMORY:
171*4882a593Smuzhiyun 			e820_type = E820_TYPE_UNUSABLE;
172*4882a593Smuzhiyun 			break;
173*4882a593Smuzhiyun 		case EFI_PERSISTENT_MEMORY:
174*4882a593Smuzhiyun 			e820_type = E820_TYPE_PMEM;
175*4882a593Smuzhiyun 			break;
176*4882a593Smuzhiyun 		default:
177*4882a593Smuzhiyun 			/*
178*4882a593Smuzhiyun 			 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
179*4882a593Smuzhiyun 			 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
180*4882a593Smuzhiyun 			 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
181*4882a593Smuzhiyun 			 */
182*4882a593Smuzhiyun 			e820_type = E820_TYPE_RESERVED;
183*4882a593Smuzhiyun 			break;
184*4882a593Smuzhiyun 		}
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 		e820__range_add(start, size, e820_type);
187*4882a593Smuzhiyun 	}
188*4882a593Smuzhiyun 	e820__update_table(e820_table);
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun /*
192*4882a593Smuzhiyun  * Given add_efi_memmap defaults to 0 and there there is no alternative
193*4882a593Smuzhiyun  * e820 mechanism for soft-reserved memory, import the full EFI memory
194*4882a593Smuzhiyun  * map if soft reservations are present and enabled. Otherwise, the
195*4882a593Smuzhiyun  * mechanism to disable the kernel's consideration of EFI_MEMORY_SP is
196*4882a593Smuzhiyun  * the efi=nosoftreserve option.
197*4882a593Smuzhiyun  */
do_efi_soft_reserve(void)198*4882a593Smuzhiyun static bool do_efi_soft_reserve(void)
199*4882a593Smuzhiyun {
200*4882a593Smuzhiyun 	efi_memory_desc_t *md;
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	if (!efi_enabled(EFI_MEMMAP))
203*4882a593Smuzhiyun 		return false;
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	if (!efi_soft_reserve_enabled())
206*4882a593Smuzhiyun 		return false;
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	for_each_efi_memory_desc(md)
209*4882a593Smuzhiyun 		if (md->type == EFI_CONVENTIONAL_MEMORY &&
210*4882a593Smuzhiyun 		    (md->attribute & EFI_MEMORY_SP))
211*4882a593Smuzhiyun 			return true;
212*4882a593Smuzhiyun 	return false;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun 
efi_memblock_x86_reserve_range(void)215*4882a593Smuzhiyun int __init efi_memblock_x86_reserve_range(void)
216*4882a593Smuzhiyun {
217*4882a593Smuzhiyun 	struct efi_info *e = &boot_params.efi_info;
218*4882a593Smuzhiyun 	struct efi_memory_map_data data;
219*4882a593Smuzhiyun 	phys_addr_t pmap;
220*4882a593Smuzhiyun 	int rv;
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	if (efi_enabled(EFI_PARAVIRT))
223*4882a593Smuzhiyun 		return 0;
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	/* Can't handle firmware tables above 4GB on i386 */
226*4882a593Smuzhiyun 	if (IS_ENABLED(CONFIG_X86_32) && e->efi_memmap_hi > 0) {
227*4882a593Smuzhiyun 		pr_err("Memory map is above 4GB, disabling EFI.\n");
228*4882a593Smuzhiyun 		return -EINVAL;
229*4882a593Smuzhiyun 	}
230*4882a593Smuzhiyun 	pmap = (phys_addr_t)(e->efi_memmap | ((u64)e->efi_memmap_hi << 32));
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	data.phys_map		= pmap;
233*4882a593Smuzhiyun 	data.size 		= e->efi_memmap_size;
234*4882a593Smuzhiyun 	data.desc_size		= e->efi_memdesc_size;
235*4882a593Smuzhiyun 	data.desc_version	= e->efi_memdesc_version;
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	rv = efi_memmap_init_early(&data);
238*4882a593Smuzhiyun 	if (rv)
239*4882a593Smuzhiyun 		return rv;
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	if (add_efi_memmap || do_efi_soft_reserve())
242*4882a593Smuzhiyun 		do_add_efi_memmap();
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	efi_fake_memmap_early();
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	WARN(efi.memmap.desc_version != 1,
247*4882a593Smuzhiyun 	     "Unexpected EFI_MEMORY_DESCRIPTOR version %ld",
248*4882a593Smuzhiyun 	     efi.memmap.desc_version);
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	memblock_reserve(pmap, efi.memmap.nr_map * efi.memmap.desc_size);
251*4882a593Smuzhiyun 	set_bit(EFI_PRESERVE_BS_REGIONS, &efi.flags);
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	return 0;
254*4882a593Smuzhiyun }
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun #define OVERFLOW_ADDR_SHIFT	(64 - EFI_PAGE_SHIFT)
257*4882a593Smuzhiyun #define OVERFLOW_ADDR_MASK	(U64_MAX << OVERFLOW_ADDR_SHIFT)
258*4882a593Smuzhiyun #define U64_HIGH_BIT		(~(U64_MAX >> 1))
259*4882a593Smuzhiyun 
efi_memmap_entry_valid(const efi_memory_desc_t * md,int i)260*4882a593Smuzhiyun static bool __init efi_memmap_entry_valid(const efi_memory_desc_t *md, int i)
261*4882a593Smuzhiyun {
262*4882a593Smuzhiyun 	u64 end = (md->num_pages << EFI_PAGE_SHIFT) + md->phys_addr - 1;
263*4882a593Smuzhiyun 	u64 end_hi = 0;
264*4882a593Smuzhiyun 	char buf[64];
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	if (md->num_pages == 0) {
267*4882a593Smuzhiyun 		end = 0;
268*4882a593Smuzhiyun 	} else if (md->num_pages > EFI_PAGES_MAX ||
269*4882a593Smuzhiyun 		   EFI_PAGES_MAX - md->num_pages <
270*4882a593Smuzhiyun 		   (md->phys_addr >> EFI_PAGE_SHIFT)) {
271*4882a593Smuzhiyun 		end_hi = (md->num_pages & OVERFLOW_ADDR_MASK)
272*4882a593Smuzhiyun 			>> OVERFLOW_ADDR_SHIFT;
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 		if ((md->phys_addr & U64_HIGH_BIT) && !(end & U64_HIGH_BIT))
275*4882a593Smuzhiyun 			end_hi += 1;
276*4882a593Smuzhiyun 	} else {
277*4882a593Smuzhiyun 		return true;
278*4882a593Smuzhiyun 	}
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	pr_warn_once(FW_BUG "Invalid EFI memory map entries:\n");
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	if (end_hi) {
283*4882a593Smuzhiyun 		pr_warn("mem%02u: %s range=[0x%016llx-0x%llx%016llx] (invalid)\n",
284*4882a593Smuzhiyun 			i, efi_md_typeattr_format(buf, sizeof(buf), md),
285*4882a593Smuzhiyun 			md->phys_addr, end_hi, end);
286*4882a593Smuzhiyun 	} else {
287*4882a593Smuzhiyun 		pr_warn("mem%02u: %s range=[0x%016llx-0x%016llx] (invalid)\n",
288*4882a593Smuzhiyun 			i, efi_md_typeattr_format(buf, sizeof(buf), md),
289*4882a593Smuzhiyun 			md->phys_addr, end);
290*4882a593Smuzhiyun 	}
291*4882a593Smuzhiyun 	return false;
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun 
efi_clean_memmap(void)294*4882a593Smuzhiyun static void __init efi_clean_memmap(void)
295*4882a593Smuzhiyun {
296*4882a593Smuzhiyun 	efi_memory_desc_t *out = efi.memmap.map;
297*4882a593Smuzhiyun 	const efi_memory_desc_t *in = out;
298*4882a593Smuzhiyun 	const efi_memory_desc_t *end = efi.memmap.map_end;
299*4882a593Smuzhiyun 	int i, n_removal;
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	for (i = n_removal = 0; in < end; i++) {
302*4882a593Smuzhiyun 		if (efi_memmap_entry_valid(in, i)) {
303*4882a593Smuzhiyun 			if (out != in)
304*4882a593Smuzhiyun 				memcpy(out, in, efi.memmap.desc_size);
305*4882a593Smuzhiyun 			out = (void *)out + efi.memmap.desc_size;
306*4882a593Smuzhiyun 		} else {
307*4882a593Smuzhiyun 			n_removal++;
308*4882a593Smuzhiyun 		}
309*4882a593Smuzhiyun 		in = (void *)in + efi.memmap.desc_size;
310*4882a593Smuzhiyun 	}
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 	if (n_removal > 0) {
313*4882a593Smuzhiyun 		struct efi_memory_map_data data = {
314*4882a593Smuzhiyun 			.phys_map	= efi.memmap.phys_map,
315*4882a593Smuzhiyun 			.desc_version	= efi.memmap.desc_version,
316*4882a593Smuzhiyun 			.desc_size	= efi.memmap.desc_size,
317*4882a593Smuzhiyun 			.size		= efi.memmap.desc_size * (efi.memmap.nr_map - n_removal),
318*4882a593Smuzhiyun 			.flags		= 0,
319*4882a593Smuzhiyun 		};
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 		pr_warn("Removing %d invalid memory map entries.\n", n_removal);
322*4882a593Smuzhiyun 		efi_memmap_install(&data);
323*4882a593Smuzhiyun 	}
324*4882a593Smuzhiyun }
325*4882a593Smuzhiyun 
efi_print_memmap(void)326*4882a593Smuzhiyun void __init efi_print_memmap(void)
327*4882a593Smuzhiyun {
328*4882a593Smuzhiyun 	efi_memory_desc_t *md;
329*4882a593Smuzhiyun 	int i = 0;
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	for_each_efi_memory_desc(md) {
332*4882a593Smuzhiyun 		char buf[64];
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 		pr_info("mem%02u: %s range=[0x%016llx-0x%016llx] (%lluMB)\n",
335*4882a593Smuzhiyun 			i++, efi_md_typeattr_format(buf, sizeof(buf), md),
336*4882a593Smuzhiyun 			md->phys_addr,
337*4882a593Smuzhiyun 			md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - 1,
338*4882a593Smuzhiyun 			(md->num_pages >> (20 - EFI_PAGE_SHIFT)));
339*4882a593Smuzhiyun 	}
340*4882a593Smuzhiyun }
341*4882a593Smuzhiyun 
efi_systab_init(unsigned long phys)342*4882a593Smuzhiyun static int __init efi_systab_init(unsigned long phys)
343*4882a593Smuzhiyun {
344*4882a593Smuzhiyun 	int size = efi_enabled(EFI_64BIT) ? sizeof(efi_system_table_64_t)
345*4882a593Smuzhiyun 					  : sizeof(efi_system_table_32_t);
346*4882a593Smuzhiyun 	const efi_table_hdr_t *hdr;
347*4882a593Smuzhiyun 	bool over4g = false;
348*4882a593Smuzhiyun 	void *p;
349*4882a593Smuzhiyun 	int ret;
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	hdr = p = early_memremap_ro(phys, size);
352*4882a593Smuzhiyun 	if (p == NULL) {
353*4882a593Smuzhiyun 		pr_err("Couldn't map the system table!\n");
354*4882a593Smuzhiyun 		return -ENOMEM;
355*4882a593Smuzhiyun 	}
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 	ret = efi_systab_check_header(hdr, 1);
358*4882a593Smuzhiyun 	if (ret) {
359*4882a593Smuzhiyun 		early_memunmap(p, size);
360*4882a593Smuzhiyun 		return ret;
361*4882a593Smuzhiyun 	}
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 	if (efi_enabled(EFI_64BIT)) {
364*4882a593Smuzhiyun 		const efi_system_table_64_t *systab64 = p;
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 		efi_runtime	= systab64->runtime;
367*4882a593Smuzhiyun 		over4g		= systab64->runtime > U32_MAX;
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun 		if (efi_setup) {
370*4882a593Smuzhiyun 			struct efi_setup_data *data;
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 			data = early_memremap_ro(efi_setup, sizeof(*data));
373*4882a593Smuzhiyun 			if (!data) {
374*4882a593Smuzhiyun 				early_memunmap(p, size);
375*4882a593Smuzhiyun 				return -ENOMEM;
376*4882a593Smuzhiyun 			}
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 			efi_fw_vendor		= (unsigned long)data->fw_vendor;
379*4882a593Smuzhiyun 			efi_config_table	= (unsigned long)data->tables;
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 			over4g |= data->fw_vendor	> U32_MAX ||
382*4882a593Smuzhiyun 				  data->tables		> U32_MAX;
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 			early_memunmap(data, sizeof(*data));
385*4882a593Smuzhiyun 		} else {
386*4882a593Smuzhiyun 			efi_fw_vendor		= systab64->fw_vendor;
387*4882a593Smuzhiyun 			efi_config_table	= systab64->tables;
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 			over4g |= systab64->fw_vendor	> U32_MAX ||
390*4882a593Smuzhiyun 				  systab64->tables	> U32_MAX;
391*4882a593Smuzhiyun 		}
392*4882a593Smuzhiyun 		efi_nr_tables = systab64->nr_tables;
393*4882a593Smuzhiyun 	} else {
394*4882a593Smuzhiyun 		const efi_system_table_32_t *systab32 = p;
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 		efi_fw_vendor		= systab32->fw_vendor;
397*4882a593Smuzhiyun 		efi_runtime		= systab32->runtime;
398*4882a593Smuzhiyun 		efi_config_table	= systab32->tables;
399*4882a593Smuzhiyun 		efi_nr_tables		= systab32->nr_tables;
400*4882a593Smuzhiyun 	}
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun 	efi.runtime_version = hdr->revision;
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 	efi_systab_report_header(hdr, efi_fw_vendor);
405*4882a593Smuzhiyun 	early_memunmap(p, size);
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 	if (IS_ENABLED(CONFIG_X86_32) && over4g) {
408*4882a593Smuzhiyun 		pr_err("EFI data located above 4GB, disabling EFI.\n");
409*4882a593Smuzhiyun 		return -EINVAL;
410*4882a593Smuzhiyun 	}
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	return 0;
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun 
efi_config_init(const efi_config_table_type_t * arch_tables)415*4882a593Smuzhiyun static int __init efi_config_init(const efi_config_table_type_t *arch_tables)
416*4882a593Smuzhiyun {
417*4882a593Smuzhiyun 	void *config_tables;
418*4882a593Smuzhiyun 	int sz, ret;
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	if (efi_nr_tables == 0)
421*4882a593Smuzhiyun 		return 0;
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	if (efi_enabled(EFI_64BIT))
424*4882a593Smuzhiyun 		sz = sizeof(efi_config_table_64_t);
425*4882a593Smuzhiyun 	else
426*4882a593Smuzhiyun 		sz = sizeof(efi_config_table_32_t);
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 	/*
429*4882a593Smuzhiyun 	 * Let's see what config tables the firmware passed to us.
430*4882a593Smuzhiyun 	 */
431*4882a593Smuzhiyun 	config_tables = early_memremap(efi_config_table, efi_nr_tables * sz);
432*4882a593Smuzhiyun 	if (config_tables == NULL) {
433*4882a593Smuzhiyun 		pr_err("Could not map Configuration table!\n");
434*4882a593Smuzhiyun 		return -ENOMEM;
435*4882a593Smuzhiyun 	}
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 	ret = efi_config_parse_tables(config_tables, efi_nr_tables,
438*4882a593Smuzhiyun 				      arch_tables);
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun 	early_memunmap(config_tables, efi_nr_tables * sz);
441*4882a593Smuzhiyun 	return ret;
442*4882a593Smuzhiyun }
443*4882a593Smuzhiyun 
efi_init(void)444*4882a593Smuzhiyun void __init efi_init(void)
445*4882a593Smuzhiyun {
446*4882a593Smuzhiyun 	if (IS_ENABLED(CONFIG_X86_32) &&
447*4882a593Smuzhiyun 	    (boot_params.efi_info.efi_systab_hi ||
448*4882a593Smuzhiyun 	     boot_params.efi_info.efi_memmap_hi)) {
449*4882a593Smuzhiyun 		pr_info("Table located above 4GB, disabling EFI.\n");
450*4882a593Smuzhiyun 		return;
451*4882a593Smuzhiyun 	}
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 	efi_systab_phys = boot_params.efi_info.efi_systab |
454*4882a593Smuzhiyun 			  ((__u64)boot_params.efi_info.efi_systab_hi << 32);
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 	if (efi_systab_init(efi_systab_phys))
457*4882a593Smuzhiyun 		return;
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun 	if (efi_reuse_config(efi_config_table, efi_nr_tables))
460*4882a593Smuzhiyun 		return;
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 	if (efi_config_init(arch_tables))
463*4882a593Smuzhiyun 		return;
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 	/*
466*4882a593Smuzhiyun 	 * Note: We currently don't support runtime services on an EFI
467*4882a593Smuzhiyun 	 * that doesn't match the kernel 32/64-bit mode.
468*4882a593Smuzhiyun 	 */
469*4882a593Smuzhiyun 
470*4882a593Smuzhiyun 	if (!efi_runtime_supported())
471*4882a593Smuzhiyun 		pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 	if (!efi_runtime_supported() || efi_runtime_disabled()) {
474*4882a593Smuzhiyun 		efi_memmap_unmap();
475*4882a593Smuzhiyun 		return;
476*4882a593Smuzhiyun 	}
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun 	/* Parse the EFI Properties table if it exists */
479*4882a593Smuzhiyun 	if (prop_phys != EFI_INVALID_TABLE_ADDR) {
480*4882a593Smuzhiyun 		efi_properties_table_t *tbl;
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 		tbl = early_memremap_ro(prop_phys, sizeof(*tbl));
483*4882a593Smuzhiyun 		if (tbl == NULL) {
484*4882a593Smuzhiyun 			pr_err("Could not map Properties table!\n");
485*4882a593Smuzhiyun 		} else {
486*4882a593Smuzhiyun 			if (tbl->memory_protection_attribute &
487*4882a593Smuzhiyun 			    EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA)
488*4882a593Smuzhiyun 				set_bit(EFI_NX_PE_DATA, &efi.flags);
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 			early_memunmap(tbl, sizeof(*tbl));
491*4882a593Smuzhiyun 		}
492*4882a593Smuzhiyun 	}
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun 	set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
495*4882a593Smuzhiyun 	efi_clean_memmap();
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 	if (efi_enabled(EFI_DBG))
498*4882a593Smuzhiyun 		efi_print_memmap();
499*4882a593Smuzhiyun }
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun /* Merge contiguous regions of the same type and attribute */
efi_merge_regions(void)502*4882a593Smuzhiyun static void __init efi_merge_regions(void)
503*4882a593Smuzhiyun {
504*4882a593Smuzhiyun 	efi_memory_desc_t *md, *prev_md = NULL;
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 	for_each_efi_memory_desc(md) {
507*4882a593Smuzhiyun 		u64 prev_size;
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 		if (!prev_md) {
510*4882a593Smuzhiyun 			prev_md = md;
511*4882a593Smuzhiyun 			continue;
512*4882a593Smuzhiyun 		}
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 		if (prev_md->type != md->type ||
515*4882a593Smuzhiyun 		    prev_md->attribute != md->attribute) {
516*4882a593Smuzhiyun 			prev_md = md;
517*4882a593Smuzhiyun 			continue;
518*4882a593Smuzhiyun 		}
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 		prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun 		if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
523*4882a593Smuzhiyun 			prev_md->num_pages += md->num_pages;
524*4882a593Smuzhiyun 			md->type = EFI_RESERVED_TYPE;
525*4882a593Smuzhiyun 			md->attribute = 0;
526*4882a593Smuzhiyun 			continue;
527*4882a593Smuzhiyun 		}
528*4882a593Smuzhiyun 		prev_md = md;
529*4882a593Smuzhiyun 	}
530*4882a593Smuzhiyun }
531*4882a593Smuzhiyun 
realloc_pages(void * old_memmap,int old_shift)532*4882a593Smuzhiyun static void *realloc_pages(void *old_memmap, int old_shift)
533*4882a593Smuzhiyun {
534*4882a593Smuzhiyun 	void *ret;
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 	ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1);
537*4882a593Smuzhiyun 	if (!ret)
538*4882a593Smuzhiyun 		goto out;
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun 	/*
541*4882a593Smuzhiyun 	 * A first-time allocation doesn't have anything to copy.
542*4882a593Smuzhiyun 	 */
543*4882a593Smuzhiyun 	if (!old_memmap)
544*4882a593Smuzhiyun 		return ret;
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun 	memcpy(ret, old_memmap, PAGE_SIZE << old_shift);
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun out:
549*4882a593Smuzhiyun 	free_pages((unsigned long)old_memmap, old_shift);
550*4882a593Smuzhiyun 	return ret;
551*4882a593Smuzhiyun }
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun /*
554*4882a593Smuzhiyun  * Iterate the EFI memory map in reverse order because the regions
555*4882a593Smuzhiyun  * will be mapped top-down. The end result is the same as if we had
556*4882a593Smuzhiyun  * mapped things forward, but doesn't require us to change the
557*4882a593Smuzhiyun  * existing implementation of efi_map_region().
558*4882a593Smuzhiyun  */
efi_map_next_entry_reverse(void * entry)559*4882a593Smuzhiyun static inline void *efi_map_next_entry_reverse(void *entry)
560*4882a593Smuzhiyun {
561*4882a593Smuzhiyun 	/* Initial call */
562*4882a593Smuzhiyun 	if (!entry)
563*4882a593Smuzhiyun 		return efi.memmap.map_end - efi.memmap.desc_size;
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 	entry -= efi.memmap.desc_size;
566*4882a593Smuzhiyun 	if (entry < efi.memmap.map)
567*4882a593Smuzhiyun 		return NULL;
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 	return entry;
570*4882a593Smuzhiyun }
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun /*
573*4882a593Smuzhiyun  * efi_map_next_entry - Return the next EFI memory map descriptor
574*4882a593Smuzhiyun  * @entry: Previous EFI memory map descriptor
575*4882a593Smuzhiyun  *
576*4882a593Smuzhiyun  * This is a helper function to iterate over the EFI memory map, which
577*4882a593Smuzhiyun  * we do in different orders depending on the current configuration.
578*4882a593Smuzhiyun  *
579*4882a593Smuzhiyun  * To begin traversing the memory map @entry must be %NULL.
580*4882a593Smuzhiyun  *
581*4882a593Smuzhiyun  * Returns %NULL when we reach the end of the memory map.
582*4882a593Smuzhiyun  */
efi_map_next_entry(void * entry)583*4882a593Smuzhiyun static void *efi_map_next_entry(void *entry)
584*4882a593Smuzhiyun {
585*4882a593Smuzhiyun 	if (efi_enabled(EFI_64BIT)) {
586*4882a593Smuzhiyun 		/*
587*4882a593Smuzhiyun 		 * Starting in UEFI v2.5 the EFI_PROPERTIES_TABLE
588*4882a593Smuzhiyun 		 * config table feature requires us to map all entries
589*4882a593Smuzhiyun 		 * in the same order as they appear in the EFI memory
590*4882a593Smuzhiyun 		 * map. That is to say, entry N must have a lower
591*4882a593Smuzhiyun 		 * virtual address than entry N+1. This is because the
592*4882a593Smuzhiyun 		 * firmware toolchain leaves relative references in
593*4882a593Smuzhiyun 		 * the code/data sections, which are split and become
594*4882a593Smuzhiyun 		 * separate EFI memory regions. Mapping things
595*4882a593Smuzhiyun 		 * out-of-order leads to the firmware accessing
596*4882a593Smuzhiyun 		 * unmapped addresses.
597*4882a593Smuzhiyun 		 *
598*4882a593Smuzhiyun 		 * Since we need to map things this way whether or not
599*4882a593Smuzhiyun 		 * the kernel actually makes use of
600*4882a593Smuzhiyun 		 * EFI_PROPERTIES_TABLE, let's just switch to this
601*4882a593Smuzhiyun 		 * scheme by default for 64-bit.
602*4882a593Smuzhiyun 		 */
603*4882a593Smuzhiyun 		return efi_map_next_entry_reverse(entry);
604*4882a593Smuzhiyun 	}
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun 	/* Initial call */
607*4882a593Smuzhiyun 	if (!entry)
608*4882a593Smuzhiyun 		return efi.memmap.map;
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun 	entry += efi.memmap.desc_size;
611*4882a593Smuzhiyun 	if (entry >= efi.memmap.map_end)
612*4882a593Smuzhiyun 		return NULL;
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun 	return entry;
615*4882a593Smuzhiyun }
616*4882a593Smuzhiyun 
should_map_region(efi_memory_desc_t * md)617*4882a593Smuzhiyun static bool should_map_region(efi_memory_desc_t *md)
618*4882a593Smuzhiyun {
619*4882a593Smuzhiyun 	/*
620*4882a593Smuzhiyun 	 * Runtime regions always require runtime mappings (obviously).
621*4882a593Smuzhiyun 	 */
622*4882a593Smuzhiyun 	if (md->attribute & EFI_MEMORY_RUNTIME)
623*4882a593Smuzhiyun 		return true;
624*4882a593Smuzhiyun 
625*4882a593Smuzhiyun 	/*
626*4882a593Smuzhiyun 	 * 32-bit EFI doesn't suffer from the bug that requires us to
627*4882a593Smuzhiyun 	 * reserve boot services regions, and mixed mode support
628*4882a593Smuzhiyun 	 * doesn't exist for 32-bit kernels.
629*4882a593Smuzhiyun 	 */
630*4882a593Smuzhiyun 	if (IS_ENABLED(CONFIG_X86_32))
631*4882a593Smuzhiyun 		return false;
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 	/*
634*4882a593Smuzhiyun 	 * EFI specific purpose memory may be reserved by default
635*4882a593Smuzhiyun 	 * depending on kernel config and boot options.
636*4882a593Smuzhiyun 	 */
637*4882a593Smuzhiyun 	if (md->type == EFI_CONVENTIONAL_MEMORY &&
638*4882a593Smuzhiyun 	    efi_soft_reserve_enabled() &&
639*4882a593Smuzhiyun 	    (md->attribute & EFI_MEMORY_SP))
640*4882a593Smuzhiyun 		return false;
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun 	/*
643*4882a593Smuzhiyun 	 * Map all of RAM so that we can access arguments in the 1:1
644*4882a593Smuzhiyun 	 * mapping when making EFI runtime calls.
645*4882a593Smuzhiyun 	 */
646*4882a593Smuzhiyun 	if (efi_is_mixed()) {
647*4882a593Smuzhiyun 		if (md->type == EFI_CONVENTIONAL_MEMORY ||
648*4882a593Smuzhiyun 		    md->type == EFI_LOADER_DATA ||
649*4882a593Smuzhiyun 		    md->type == EFI_LOADER_CODE)
650*4882a593Smuzhiyun 			return true;
651*4882a593Smuzhiyun 	}
652*4882a593Smuzhiyun 
653*4882a593Smuzhiyun 	/*
654*4882a593Smuzhiyun 	 * Map boot services regions as a workaround for buggy
655*4882a593Smuzhiyun 	 * firmware that accesses them even when they shouldn't.
656*4882a593Smuzhiyun 	 *
657*4882a593Smuzhiyun 	 * See efi_{reserve,free}_boot_services().
658*4882a593Smuzhiyun 	 */
659*4882a593Smuzhiyun 	if (md->type == EFI_BOOT_SERVICES_CODE ||
660*4882a593Smuzhiyun 	    md->type == EFI_BOOT_SERVICES_DATA)
661*4882a593Smuzhiyun 		return true;
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun 	return false;
664*4882a593Smuzhiyun }
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun /*
667*4882a593Smuzhiyun  * Map the efi memory ranges of the runtime services and update new_mmap with
668*4882a593Smuzhiyun  * virtual addresses.
669*4882a593Smuzhiyun  */
efi_map_regions(int * count,int * pg_shift)670*4882a593Smuzhiyun static void * __init efi_map_regions(int *count, int *pg_shift)
671*4882a593Smuzhiyun {
672*4882a593Smuzhiyun 	void *p, *new_memmap = NULL;
673*4882a593Smuzhiyun 	unsigned long left = 0;
674*4882a593Smuzhiyun 	unsigned long desc_size;
675*4882a593Smuzhiyun 	efi_memory_desc_t *md;
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun 	desc_size = efi.memmap.desc_size;
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun 	p = NULL;
680*4882a593Smuzhiyun 	while ((p = efi_map_next_entry(p))) {
681*4882a593Smuzhiyun 		md = p;
682*4882a593Smuzhiyun 
683*4882a593Smuzhiyun 		if (!should_map_region(md))
684*4882a593Smuzhiyun 			continue;
685*4882a593Smuzhiyun 
686*4882a593Smuzhiyun 		efi_map_region(md);
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 		if (left < desc_size) {
689*4882a593Smuzhiyun 			new_memmap = realloc_pages(new_memmap, *pg_shift);
690*4882a593Smuzhiyun 			if (!new_memmap)
691*4882a593Smuzhiyun 				return NULL;
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun 			left += PAGE_SIZE << *pg_shift;
694*4882a593Smuzhiyun 			(*pg_shift)++;
695*4882a593Smuzhiyun 		}
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun 		memcpy(new_memmap + (*count * desc_size), md, desc_size);
698*4882a593Smuzhiyun 
699*4882a593Smuzhiyun 		left -= desc_size;
700*4882a593Smuzhiyun 		(*count)++;
701*4882a593Smuzhiyun 	}
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun 	return new_memmap;
704*4882a593Smuzhiyun }
705*4882a593Smuzhiyun 
kexec_enter_virtual_mode(void)706*4882a593Smuzhiyun static void __init kexec_enter_virtual_mode(void)
707*4882a593Smuzhiyun {
708*4882a593Smuzhiyun #ifdef CONFIG_KEXEC_CORE
709*4882a593Smuzhiyun 	efi_memory_desc_t *md;
710*4882a593Smuzhiyun 	unsigned int num_pages;
711*4882a593Smuzhiyun 
712*4882a593Smuzhiyun 	/*
713*4882a593Smuzhiyun 	 * We don't do virtual mode, since we don't do runtime services, on
714*4882a593Smuzhiyun 	 * non-native EFI.
715*4882a593Smuzhiyun 	 */
716*4882a593Smuzhiyun 	if (efi_is_mixed()) {
717*4882a593Smuzhiyun 		efi_memmap_unmap();
718*4882a593Smuzhiyun 		clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
719*4882a593Smuzhiyun 		return;
720*4882a593Smuzhiyun 	}
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun 	if (efi_alloc_page_tables()) {
723*4882a593Smuzhiyun 		pr_err("Failed to allocate EFI page tables\n");
724*4882a593Smuzhiyun 		clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
725*4882a593Smuzhiyun 		return;
726*4882a593Smuzhiyun 	}
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun 	/*
729*4882a593Smuzhiyun 	* Map efi regions which were passed via setup_data. The virt_addr is a
730*4882a593Smuzhiyun 	* fixed addr which was used in first kernel of a kexec boot.
731*4882a593Smuzhiyun 	*/
732*4882a593Smuzhiyun 	for_each_efi_memory_desc(md)
733*4882a593Smuzhiyun 		efi_map_region_fixed(md); /* FIXME: add error handling */
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 	/*
736*4882a593Smuzhiyun 	 * Unregister the early EFI memmap from efi_init() and install
737*4882a593Smuzhiyun 	 * the new EFI memory map.
738*4882a593Smuzhiyun 	 */
739*4882a593Smuzhiyun 	efi_memmap_unmap();
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun 	if (efi_memmap_init_late(efi.memmap.phys_map,
742*4882a593Smuzhiyun 				 efi.memmap.desc_size * efi.memmap.nr_map)) {
743*4882a593Smuzhiyun 		pr_err("Failed to remap late EFI memory map\n");
744*4882a593Smuzhiyun 		clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
745*4882a593Smuzhiyun 		return;
746*4882a593Smuzhiyun 	}
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 	num_pages = ALIGN(efi.memmap.nr_map * efi.memmap.desc_size, PAGE_SIZE);
749*4882a593Smuzhiyun 	num_pages >>= PAGE_SHIFT;
750*4882a593Smuzhiyun 
751*4882a593Smuzhiyun 	if (efi_setup_page_tables(efi.memmap.phys_map, num_pages)) {
752*4882a593Smuzhiyun 		clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
753*4882a593Smuzhiyun 		return;
754*4882a593Smuzhiyun 	}
755*4882a593Smuzhiyun 
756*4882a593Smuzhiyun 	efi_sync_low_kernel_mappings();
757*4882a593Smuzhiyun 	efi_native_runtime_setup();
758*4882a593Smuzhiyun #endif
759*4882a593Smuzhiyun }
760*4882a593Smuzhiyun 
761*4882a593Smuzhiyun /*
762*4882a593Smuzhiyun  * This function will switch the EFI runtime services to virtual mode.
763*4882a593Smuzhiyun  * Essentially, we look through the EFI memmap and map every region that
764*4882a593Smuzhiyun  * has the runtime attribute bit set in its memory descriptor into the
765*4882a593Smuzhiyun  * efi_pgd page table.
766*4882a593Smuzhiyun  *
767*4882a593Smuzhiyun  * The new method does a pagetable switch in a preemption-safe manner
768*4882a593Smuzhiyun  * so that we're in a different address space when calling a runtime
769*4882a593Smuzhiyun  * function. For function arguments passing we do copy the PUDs of the
770*4882a593Smuzhiyun  * kernel page table into efi_pgd prior to each call.
771*4882a593Smuzhiyun  *
772*4882a593Smuzhiyun  * Specially for kexec boot, efi runtime maps in previous kernel should
773*4882a593Smuzhiyun  * be passed in via setup_data. In that case runtime ranges will be mapped
774*4882a593Smuzhiyun  * to the same virtual addresses as the first kernel, see
775*4882a593Smuzhiyun  * kexec_enter_virtual_mode().
776*4882a593Smuzhiyun  */
__efi_enter_virtual_mode(void)777*4882a593Smuzhiyun static void __init __efi_enter_virtual_mode(void)
778*4882a593Smuzhiyun {
779*4882a593Smuzhiyun 	int count = 0, pg_shift = 0;
780*4882a593Smuzhiyun 	void *new_memmap = NULL;
781*4882a593Smuzhiyun 	efi_status_t status;
782*4882a593Smuzhiyun 	unsigned long pa;
783*4882a593Smuzhiyun 
784*4882a593Smuzhiyun 	if (efi_alloc_page_tables()) {
785*4882a593Smuzhiyun 		pr_err("Failed to allocate EFI page tables\n");
786*4882a593Smuzhiyun 		goto err;
787*4882a593Smuzhiyun 	}
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun 	efi_merge_regions();
790*4882a593Smuzhiyun 	new_memmap = efi_map_regions(&count, &pg_shift);
791*4882a593Smuzhiyun 	if (!new_memmap) {
792*4882a593Smuzhiyun 		pr_err("Error reallocating memory, EFI runtime non-functional!\n");
793*4882a593Smuzhiyun 		goto err;
794*4882a593Smuzhiyun 	}
795*4882a593Smuzhiyun 
796*4882a593Smuzhiyun 	pa = __pa(new_memmap);
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun 	/*
799*4882a593Smuzhiyun 	 * Unregister the early EFI memmap from efi_init() and install
800*4882a593Smuzhiyun 	 * the new EFI memory map that we are about to pass to the
801*4882a593Smuzhiyun 	 * firmware via SetVirtualAddressMap().
802*4882a593Smuzhiyun 	 */
803*4882a593Smuzhiyun 	efi_memmap_unmap();
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun 	if (efi_memmap_init_late(pa, efi.memmap.desc_size * count)) {
806*4882a593Smuzhiyun 		pr_err("Failed to remap late EFI memory map\n");
807*4882a593Smuzhiyun 		goto err;
808*4882a593Smuzhiyun 	}
809*4882a593Smuzhiyun 
810*4882a593Smuzhiyun 	if (efi_enabled(EFI_DBG)) {
811*4882a593Smuzhiyun 		pr_info("EFI runtime memory map:\n");
812*4882a593Smuzhiyun 		efi_print_memmap();
813*4882a593Smuzhiyun 	}
814*4882a593Smuzhiyun 
815*4882a593Smuzhiyun 	if (efi_setup_page_tables(pa, 1 << pg_shift))
816*4882a593Smuzhiyun 		goto err;
817*4882a593Smuzhiyun 
818*4882a593Smuzhiyun 	efi_sync_low_kernel_mappings();
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun 	status = efi_set_virtual_address_map(efi.memmap.desc_size * count,
821*4882a593Smuzhiyun 					     efi.memmap.desc_size,
822*4882a593Smuzhiyun 					     efi.memmap.desc_version,
823*4882a593Smuzhiyun 					     (efi_memory_desc_t *)pa,
824*4882a593Smuzhiyun 					     efi_systab_phys);
825*4882a593Smuzhiyun 	if (status != EFI_SUCCESS) {
826*4882a593Smuzhiyun 		pr_err("Unable to switch EFI into virtual mode (status=%lx)!\n",
827*4882a593Smuzhiyun 		       status);
828*4882a593Smuzhiyun 		goto err;
829*4882a593Smuzhiyun 	}
830*4882a593Smuzhiyun 
831*4882a593Smuzhiyun 	efi_check_for_embedded_firmwares();
832*4882a593Smuzhiyun 	efi_free_boot_services();
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun 	if (!efi_is_mixed())
835*4882a593Smuzhiyun 		efi_native_runtime_setup();
836*4882a593Smuzhiyun 	else
837*4882a593Smuzhiyun 		efi_thunk_runtime_setup();
838*4882a593Smuzhiyun 
839*4882a593Smuzhiyun 	/*
840*4882a593Smuzhiyun 	 * Apply more restrictive page table mapping attributes now that
841*4882a593Smuzhiyun 	 * SVAM() has been called and the firmware has performed all
842*4882a593Smuzhiyun 	 * necessary relocation fixups for the new virtual addresses.
843*4882a593Smuzhiyun 	 */
844*4882a593Smuzhiyun 	efi_runtime_update_mappings();
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun 	/* clean DUMMY object */
847*4882a593Smuzhiyun 	efi_delete_dummy_variable();
848*4882a593Smuzhiyun 	return;
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun err:
851*4882a593Smuzhiyun 	clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
852*4882a593Smuzhiyun }
853*4882a593Smuzhiyun 
efi_enter_virtual_mode(void)854*4882a593Smuzhiyun void __init efi_enter_virtual_mode(void)
855*4882a593Smuzhiyun {
856*4882a593Smuzhiyun 	if (efi_enabled(EFI_PARAVIRT))
857*4882a593Smuzhiyun 		return;
858*4882a593Smuzhiyun 
859*4882a593Smuzhiyun 	efi.runtime = (efi_runtime_services_t *)efi_runtime;
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun 	if (efi_setup)
862*4882a593Smuzhiyun 		kexec_enter_virtual_mode();
863*4882a593Smuzhiyun 	else
864*4882a593Smuzhiyun 		__efi_enter_virtual_mode();
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun 	efi_dump_pagetable();
867*4882a593Smuzhiyun }
868*4882a593Smuzhiyun 
efi_is_table_address(unsigned long phys_addr)869*4882a593Smuzhiyun bool efi_is_table_address(unsigned long phys_addr)
870*4882a593Smuzhiyun {
871*4882a593Smuzhiyun 	unsigned int i;
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun 	if (phys_addr == EFI_INVALID_TABLE_ADDR)
874*4882a593Smuzhiyun 		return false;
875*4882a593Smuzhiyun 
876*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(efi_tables); i++)
877*4882a593Smuzhiyun 		if (*(efi_tables[i]) == phys_addr)
878*4882a593Smuzhiyun 			return true;
879*4882a593Smuzhiyun 
880*4882a593Smuzhiyun 	return false;
881*4882a593Smuzhiyun }
882*4882a593Smuzhiyun 
efi_systab_show_arch(char * str)883*4882a593Smuzhiyun char *efi_systab_show_arch(char *str)
884*4882a593Smuzhiyun {
885*4882a593Smuzhiyun 	if (uga_phys != EFI_INVALID_TABLE_ADDR)
886*4882a593Smuzhiyun 		str += sprintf(str, "UGA=0x%lx\n", uga_phys);
887*4882a593Smuzhiyun 	return str;
888*4882a593Smuzhiyun }
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun #define EFI_FIELD(var) efi_ ## var
891*4882a593Smuzhiyun 
892*4882a593Smuzhiyun #define EFI_ATTR_SHOW(name) \
893*4882a593Smuzhiyun static ssize_t name##_show(struct kobject *kobj, \
894*4882a593Smuzhiyun 				struct kobj_attribute *attr, char *buf) \
895*4882a593Smuzhiyun { \
896*4882a593Smuzhiyun 	return sprintf(buf, "0x%lx\n", EFI_FIELD(name)); \
897*4882a593Smuzhiyun }
898*4882a593Smuzhiyun 
899*4882a593Smuzhiyun EFI_ATTR_SHOW(fw_vendor);
900*4882a593Smuzhiyun EFI_ATTR_SHOW(runtime);
901*4882a593Smuzhiyun EFI_ATTR_SHOW(config_table);
902*4882a593Smuzhiyun 
903*4882a593Smuzhiyun struct kobj_attribute efi_attr_fw_vendor = __ATTR_RO(fw_vendor);
904*4882a593Smuzhiyun struct kobj_attribute efi_attr_runtime = __ATTR_RO(runtime);
905*4882a593Smuzhiyun struct kobj_attribute efi_attr_config_table = __ATTR_RO(config_table);
906*4882a593Smuzhiyun 
efi_attr_is_visible(struct kobject * kobj,struct attribute * attr,int n)907*4882a593Smuzhiyun umode_t efi_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
908*4882a593Smuzhiyun {
909*4882a593Smuzhiyun 	if (attr == &efi_attr_fw_vendor.attr) {
910*4882a593Smuzhiyun 		if (efi_enabled(EFI_PARAVIRT) ||
911*4882a593Smuzhiyun 				efi_fw_vendor == EFI_INVALID_TABLE_ADDR)
912*4882a593Smuzhiyun 			return 0;
913*4882a593Smuzhiyun 	} else if (attr == &efi_attr_runtime.attr) {
914*4882a593Smuzhiyun 		if (efi_runtime == EFI_INVALID_TABLE_ADDR)
915*4882a593Smuzhiyun 			return 0;
916*4882a593Smuzhiyun 	} else if (attr == &efi_attr_config_table.attr) {
917*4882a593Smuzhiyun 		if (efi_config_table == EFI_INVALID_TABLE_ADDR)
918*4882a593Smuzhiyun 			return 0;
919*4882a593Smuzhiyun 	}
920*4882a593Smuzhiyun 	return attr->mode;
921*4882a593Smuzhiyun }
922