xref: /openbmc/linux/arch/x86/platform/efi/efi.c (revision 94c7b6fc)
1 /*
2  * Common EFI (Extensible Firmware Interface) support functions
3  * Based on Extensible Firmware Interface Specification version 1.0
4  *
5  * Copyright (C) 1999 VA Linux Systems
6  * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
7  * Copyright (C) 1999-2002 Hewlett-Packard Co.
8  *	David Mosberger-Tang <davidm@hpl.hp.com>
9  *	Stephane Eranian <eranian@hpl.hp.com>
10  * Copyright (C) 2005-2008 Intel Co.
11  *	Fenghua Yu <fenghua.yu@intel.com>
12  *	Bibo Mao <bibo.mao@intel.com>
13  *	Chandramouli Narayanan <mouli@linux.intel.com>
14  *	Huang Ying <ying.huang@intel.com>
15  * Copyright (C) 2013 SuSE Labs
16  *	Borislav Petkov <bp@suse.de> - runtime services VA mapping
17  *
18  * Copied from efi_32.c to eliminate the duplicated code between EFI
19  * 32/64 support code. --ying 2007-10-26
20  *
21  * All EFI Runtime Services are not implemented yet as EFI only
22  * supports physical mode addressing on SoftSDV. This is to be fixed
23  * in a future version.  --drummond 1999-07-20
24  *
25  * Implemented EFI runtime services and virtual mode calls.  --davidm
26  *
27  * Goutham Rao: <goutham.rao@intel.com>
28  *	Skip non-WB memory and ignore empty memory ranges.
29  */
30 
31 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32 
33 #include <linux/kernel.h>
34 #include <linux/init.h>
35 #include <linux/efi.h>
36 #include <linux/efi-bgrt.h>
37 #include <linux/export.h>
38 #include <linux/bootmem.h>
39 #include <linux/slab.h>
40 #include <linux/memblock.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/time.h>
51 #include <asm/cacheflush.h>
52 #include <asm/tlbflush.h>
53 #include <asm/x86_init.h>
54 #include <asm/rtc.h>
55 #include <asm/uv/uv.h>
56 
57 #define EFI_DEBUG
58 
59 #define EFI_MIN_RESERVE 5120
60 
61 #define EFI_DUMMY_GUID \
62 	EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
63 
64 static efi_char16_t efi_dummy_name[6] = { 'D', 'U', 'M', 'M', 'Y', 0 };
65 
66 struct efi_memory_map memmap;
67 
68 static struct efi efi_phys __initdata;
69 static efi_system_table_t efi_systab __initdata;
70 
71 static efi_config_table_type_t arch_tables[] __initdata = {
72 #ifdef CONFIG_X86_UV
73 	{UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab},
74 #endif
75 	{NULL_GUID, NULL, NULL},
76 };
77 
78 u64 efi_setup;		/* efi setup_data physical address */
79 
80 static bool disable_runtime __initdata = false;
81 static int __init setup_noefi(char *arg)
82 {
83 	disable_runtime = true;
84 	return 0;
85 }
86 early_param("noefi", setup_noefi);
87 
88 int add_efi_memmap;
89 EXPORT_SYMBOL(add_efi_memmap);
90 
91 static int __init setup_add_efi_memmap(char *arg)
92 {
93 	add_efi_memmap = 1;
94 	return 0;
95 }
96 early_param("add_efi_memmap", setup_add_efi_memmap);
97 
98 static bool efi_no_storage_paranoia;
99 
100 static int __init setup_storage_paranoia(char *arg)
101 {
102 	efi_no_storage_paranoia = true;
103 	return 0;
104 }
105 early_param("efi_no_storage_paranoia", setup_storage_paranoia);
106 
107 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
108 {
109 	unsigned long flags;
110 	efi_status_t status;
111 
112 	spin_lock_irqsave(&rtc_lock, flags);
113 	status = efi_call_virt(get_time, tm, tc);
114 	spin_unlock_irqrestore(&rtc_lock, flags);
115 	return status;
116 }
117 
118 static efi_status_t virt_efi_set_time(efi_time_t *tm)
119 {
120 	unsigned long flags;
121 	efi_status_t status;
122 
123 	spin_lock_irqsave(&rtc_lock, flags);
124 	status = efi_call_virt(set_time, tm);
125 	spin_unlock_irqrestore(&rtc_lock, flags);
126 	return status;
127 }
128 
129 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
130 					     efi_bool_t *pending,
131 					     efi_time_t *tm)
132 {
133 	unsigned long flags;
134 	efi_status_t status;
135 
136 	spin_lock_irqsave(&rtc_lock, flags);
137 	status = efi_call_virt(get_wakeup_time, enabled, pending, tm);
138 	spin_unlock_irqrestore(&rtc_lock, flags);
139 	return status;
140 }
141 
142 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
143 {
144 	unsigned long flags;
145 	efi_status_t status;
146 
147 	spin_lock_irqsave(&rtc_lock, flags);
148 	status = efi_call_virt(set_wakeup_time, enabled, tm);
149 	spin_unlock_irqrestore(&rtc_lock, flags);
150 	return status;
151 }
152 
153 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
154 					  efi_guid_t *vendor,
155 					  u32 *attr,
156 					  unsigned long *data_size,
157 					  void *data)
158 {
159 	return efi_call_virt(get_variable,
160 			     name, vendor, attr,
161 			     data_size, data);
162 }
163 
164 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
165 					       efi_char16_t *name,
166 					       efi_guid_t *vendor)
167 {
168 	return efi_call_virt(get_next_variable,
169 			     name_size, name, vendor);
170 }
171 
172 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
173 					  efi_guid_t *vendor,
174 					  u32 attr,
175 					  unsigned long data_size,
176 					  void *data)
177 {
178 	return efi_call_virt(set_variable,
179 			     name, vendor, attr,
180 			     data_size, data);
181 }
182 
183 static efi_status_t virt_efi_query_variable_info(u32 attr,
184 						 u64 *storage_space,
185 						 u64 *remaining_space,
186 						 u64 *max_variable_size)
187 {
188 	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
189 		return EFI_UNSUPPORTED;
190 
191 	return efi_call_virt(query_variable_info, attr, storage_space,
192 			     remaining_space, max_variable_size);
193 }
194 
195 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
196 {
197 	return efi_call_virt(get_next_high_mono_count, count);
198 }
199 
200 static void virt_efi_reset_system(int reset_type,
201 				  efi_status_t status,
202 				  unsigned long data_size,
203 				  efi_char16_t *data)
204 {
205 	__efi_call_virt(reset_system, reset_type, status,
206 			data_size, data);
207 }
208 
209 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
210 					    unsigned long count,
211 					    unsigned long sg_list)
212 {
213 	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
214 		return EFI_UNSUPPORTED;
215 
216 	return efi_call_virt(update_capsule, capsules, count, sg_list);
217 }
218 
219 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
220 						unsigned long count,
221 						u64 *max_size,
222 						int *reset_type)
223 {
224 	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
225 		return EFI_UNSUPPORTED;
226 
227 	return efi_call_virt(query_capsule_caps, capsules, count, max_size,
228 			     reset_type);
229 }
230 
231 static efi_status_t __init phys_efi_set_virtual_address_map(
232 	unsigned long memory_map_size,
233 	unsigned long descriptor_size,
234 	u32 descriptor_version,
235 	efi_memory_desc_t *virtual_map)
236 {
237 	efi_status_t status;
238 
239 	efi_call_phys_prelog();
240 	status = efi_call_phys(efi_phys.set_virtual_address_map,
241 			       memory_map_size, descriptor_size,
242 			       descriptor_version, virtual_map);
243 	efi_call_phys_epilog();
244 	return status;
245 }
246 
247 int efi_set_rtc_mmss(const struct timespec *now)
248 {
249 	unsigned long nowtime = now->tv_sec;
250 	efi_status_t	status;
251 	efi_time_t	eft;
252 	efi_time_cap_t	cap;
253 	struct rtc_time	tm;
254 
255 	status = efi.get_time(&eft, &cap);
256 	if (status != EFI_SUCCESS) {
257 		pr_err("Oops: efitime: can't read time!\n");
258 		return -1;
259 	}
260 
261 	rtc_time_to_tm(nowtime, &tm);
262 	if (!rtc_valid_tm(&tm)) {
263 		eft.year = tm.tm_year + 1900;
264 		eft.month = tm.tm_mon + 1;
265 		eft.day = tm.tm_mday;
266 		eft.minute = tm.tm_min;
267 		eft.second = tm.tm_sec;
268 		eft.nanosecond = 0;
269 	} else {
270 		pr_err("%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
271 		       __func__, nowtime);
272 		return -1;
273 	}
274 
275 	status = efi.set_time(&eft);
276 	if (status != EFI_SUCCESS) {
277 		pr_err("Oops: efitime: can't write time!\n");
278 		return -1;
279 	}
280 	return 0;
281 }
282 
283 void efi_get_time(struct timespec *now)
284 {
285 	efi_status_t status;
286 	efi_time_t eft;
287 	efi_time_cap_t cap;
288 
289 	status = efi.get_time(&eft, &cap);
290 	if (status != EFI_SUCCESS)
291 		pr_err("Oops: efitime: can't read time!\n");
292 
293 	now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour,
294 			     eft.minute, eft.second);
295 	now->tv_nsec = 0;
296 }
297 
298 /*
299  * Tell the kernel about the EFI memory map.  This might include
300  * more than the max 128 entries that can fit in the e820 legacy
301  * (zeropage) memory map.
302  */
303 
304 static void __init do_add_efi_memmap(void)
305 {
306 	void *p;
307 
308 	for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
309 		efi_memory_desc_t *md = p;
310 		unsigned long long start = md->phys_addr;
311 		unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
312 		int e820_type;
313 
314 		switch (md->type) {
315 		case EFI_LOADER_CODE:
316 		case EFI_LOADER_DATA:
317 		case EFI_BOOT_SERVICES_CODE:
318 		case EFI_BOOT_SERVICES_DATA:
319 		case EFI_CONVENTIONAL_MEMORY:
320 			if (md->attribute & EFI_MEMORY_WB)
321 				e820_type = E820_RAM;
322 			else
323 				e820_type = E820_RESERVED;
324 			break;
325 		case EFI_ACPI_RECLAIM_MEMORY:
326 			e820_type = E820_ACPI;
327 			break;
328 		case EFI_ACPI_MEMORY_NVS:
329 			e820_type = E820_NVS;
330 			break;
331 		case EFI_UNUSABLE_MEMORY:
332 			e820_type = E820_UNUSABLE;
333 			break;
334 		default:
335 			/*
336 			 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
337 			 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
338 			 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
339 			 */
340 			e820_type = E820_RESERVED;
341 			break;
342 		}
343 		e820_add_region(start, size, e820_type);
344 	}
345 	sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
346 }
347 
348 int __init efi_memblock_x86_reserve_range(void)
349 {
350 	struct efi_info *e = &boot_params.efi_info;
351 	unsigned long pmap;
352 
353 #ifdef CONFIG_X86_32
354 	/* Can't handle data above 4GB at this time */
355 	if (e->efi_memmap_hi) {
356 		pr_err("Memory map is above 4GB, disabling EFI.\n");
357 		return -EINVAL;
358 	}
359 	pmap =  e->efi_memmap;
360 #else
361 	pmap = (e->efi_memmap |	((__u64)e->efi_memmap_hi << 32));
362 #endif
363 	memmap.phys_map		= (void *)pmap;
364 	memmap.nr_map		= e->efi_memmap_size /
365 				  e->efi_memdesc_size;
366 	memmap.desc_size	= e->efi_memdesc_size;
367 	memmap.desc_version	= e->efi_memdesc_version;
368 
369 	memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
370 
371 	efi.memmap = &memmap;
372 
373 	return 0;
374 }
375 
376 static void __init print_efi_memmap(void)
377 {
378 #ifdef EFI_DEBUG
379 	efi_memory_desc_t *md;
380 	void *p;
381 	int i;
382 
383 	for (p = memmap.map, i = 0;
384 	     p < memmap.map_end;
385 	     p += memmap.desc_size, i++) {
386 		md = p;
387 		pr_info("mem%02u: type=%u, attr=0x%llx, range=[0x%016llx-0x%016llx) (%lluMB)\n",
388 			i, md->type, md->attribute, md->phys_addr,
389 			md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
390 			(md->num_pages >> (20 - EFI_PAGE_SHIFT)));
391 	}
392 #endif  /*  EFI_DEBUG  */
393 }
394 
395 void __init efi_reserve_boot_services(void)
396 {
397 	void *p;
398 
399 	for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
400 		efi_memory_desc_t *md = p;
401 		u64 start = md->phys_addr;
402 		u64 size = md->num_pages << EFI_PAGE_SHIFT;
403 
404 		if (md->type != EFI_BOOT_SERVICES_CODE &&
405 		    md->type != EFI_BOOT_SERVICES_DATA)
406 			continue;
407 		/* Only reserve where possible:
408 		 * - Not within any already allocated areas
409 		 * - Not over any memory area (really needed, if above?)
410 		 * - Not within any part of the kernel
411 		 * - Not the bios reserved area
412 		*/
413 		if ((start + size > __pa_symbol(_text)
414 				&& start <= __pa_symbol(_end)) ||
415 			!e820_all_mapped(start, start+size, E820_RAM) ||
416 			memblock_is_region_reserved(start, size)) {
417 			/* Could not reserve, skip it */
418 			md->num_pages = 0;
419 			memblock_dbg("Could not reserve boot range [0x%010llx-0x%010llx]\n",
420 				     start, start+size-1);
421 		} else
422 			memblock_reserve(start, size);
423 	}
424 }
425 
426 void __init efi_unmap_memmap(void)
427 {
428 	clear_bit(EFI_MEMMAP, &efi.flags);
429 	if (memmap.map) {
430 		early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
431 		memmap.map = NULL;
432 	}
433 }
434 
435 void __init efi_free_boot_services(void)
436 {
437 	void *p;
438 
439 	for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
440 		efi_memory_desc_t *md = p;
441 		unsigned long long start = md->phys_addr;
442 		unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
443 
444 		if (md->type != EFI_BOOT_SERVICES_CODE &&
445 		    md->type != EFI_BOOT_SERVICES_DATA)
446 			continue;
447 
448 		/* Could not reserve boot area */
449 		if (!size)
450 			continue;
451 
452 		free_bootmem_late(start, size);
453 	}
454 
455 	efi_unmap_memmap();
456 }
457 
458 static int __init efi_systab_init(void *phys)
459 {
460 	if (efi_enabled(EFI_64BIT)) {
461 		efi_system_table_64_t *systab64;
462 		struct efi_setup_data *data = NULL;
463 		u64 tmp = 0;
464 
465 		if (efi_setup) {
466 			data = early_memremap(efi_setup, sizeof(*data));
467 			if (!data)
468 				return -ENOMEM;
469 		}
470 		systab64 = early_ioremap((unsigned long)phys,
471 					 sizeof(*systab64));
472 		if (systab64 == NULL) {
473 			pr_err("Couldn't map the system table!\n");
474 			if (data)
475 				early_iounmap(data, sizeof(*data));
476 			return -ENOMEM;
477 		}
478 
479 		efi_systab.hdr = systab64->hdr;
480 		efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor :
481 					      systab64->fw_vendor;
482 		tmp |= data ? data->fw_vendor : systab64->fw_vendor;
483 		efi_systab.fw_revision = systab64->fw_revision;
484 		efi_systab.con_in_handle = systab64->con_in_handle;
485 		tmp |= systab64->con_in_handle;
486 		efi_systab.con_in = systab64->con_in;
487 		tmp |= systab64->con_in;
488 		efi_systab.con_out_handle = systab64->con_out_handle;
489 		tmp |= systab64->con_out_handle;
490 		efi_systab.con_out = systab64->con_out;
491 		tmp |= systab64->con_out;
492 		efi_systab.stderr_handle = systab64->stderr_handle;
493 		tmp |= systab64->stderr_handle;
494 		efi_systab.stderr = systab64->stderr;
495 		tmp |= systab64->stderr;
496 		efi_systab.runtime = data ?
497 				     (void *)(unsigned long)data->runtime :
498 				     (void *)(unsigned long)systab64->runtime;
499 		tmp |= data ? data->runtime : systab64->runtime;
500 		efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
501 		tmp |= systab64->boottime;
502 		efi_systab.nr_tables = systab64->nr_tables;
503 		efi_systab.tables = data ? (unsigned long)data->tables :
504 					   systab64->tables;
505 		tmp |= data ? data->tables : systab64->tables;
506 
507 		early_iounmap(systab64, sizeof(*systab64));
508 		if (data)
509 			early_iounmap(data, sizeof(*data));
510 #ifdef CONFIG_X86_32
511 		if (tmp >> 32) {
512 			pr_err("EFI data located above 4GB, disabling EFI.\n");
513 			return -EINVAL;
514 		}
515 #endif
516 	} else {
517 		efi_system_table_32_t *systab32;
518 
519 		systab32 = early_ioremap((unsigned long)phys,
520 					 sizeof(*systab32));
521 		if (systab32 == NULL) {
522 			pr_err("Couldn't map the system table!\n");
523 			return -ENOMEM;
524 		}
525 
526 		efi_systab.hdr = systab32->hdr;
527 		efi_systab.fw_vendor = systab32->fw_vendor;
528 		efi_systab.fw_revision = systab32->fw_revision;
529 		efi_systab.con_in_handle = systab32->con_in_handle;
530 		efi_systab.con_in = systab32->con_in;
531 		efi_systab.con_out_handle = systab32->con_out_handle;
532 		efi_systab.con_out = systab32->con_out;
533 		efi_systab.stderr_handle = systab32->stderr_handle;
534 		efi_systab.stderr = systab32->stderr;
535 		efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
536 		efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
537 		efi_systab.nr_tables = systab32->nr_tables;
538 		efi_systab.tables = systab32->tables;
539 
540 		early_iounmap(systab32, sizeof(*systab32));
541 	}
542 
543 	efi.systab = &efi_systab;
544 
545 	/*
546 	 * Verify the EFI Table
547 	 */
548 	if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
549 		pr_err("System table signature incorrect!\n");
550 		return -EINVAL;
551 	}
552 	if ((efi.systab->hdr.revision >> 16) == 0)
553 		pr_err("Warning: System table version %d.%02d, expected 1.00 or greater!\n",
554 		       efi.systab->hdr.revision >> 16,
555 		       efi.systab->hdr.revision & 0xffff);
556 
557 	set_bit(EFI_SYSTEM_TABLES, &efi.flags);
558 
559 	return 0;
560 }
561 
562 static int __init efi_runtime_init32(void)
563 {
564 	efi_runtime_services_32_t *runtime;
565 
566 	runtime = early_ioremap((unsigned long)efi.systab->runtime,
567 			sizeof(efi_runtime_services_32_t));
568 	if (!runtime) {
569 		pr_err("Could not map the runtime service table!\n");
570 		return -ENOMEM;
571 	}
572 
573 	/*
574 	 * We will only need *early* access to the following two
575 	 * EFI runtime services before set_virtual_address_map
576 	 * is invoked.
577 	 */
578 	efi_phys.set_virtual_address_map =
579 			(efi_set_virtual_address_map_t *)
580 			(unsigned long)runtime->set_virtual_address_map;
581 	early_iounmap(runtime, sizeof(efi_runtime_services_32_t));
582 
583 	return 0;
584 }
585 
586 static int __init efi_runtime_init64(void)
587 {
588 	efi_runtime_services_64_t *runtime;
589 
590 	runtime = early_ioremap((unsigned long)efi.systab->runtime,
591 			sizeof(efi_runtime_services_64_t));
592 	if (!runtime) {
593 		pr_err("Could not map the runtime service table!\n");
594 		return -ENOMEM;
595 	}
596 
597 	/*
598 	 * We will only need *early* access to the following two
599 	 * EFI runtime services before set_virtual_address_map
600 	 * is invoked.
601 	 */
602 	efi_phys.set_virtual_address_map =
603 			(efi_set_virtual_address_map_t *)
604 			(unsigned long)runtime->set_virtual_address_map;
605 	early_iounmap(runtime, sizeof(efi_runtime_services_64_t));
606 
607 	return 0;
608 }
609 
610 static int __init efi_runtime_init(void)
611 {
612 	int rv;
613 
614 	/*
615 	 * Check out the runtime services table. We need to map
616 	 * the runtime services table so that we can grab the physical
617 	 * address of several of the EFI runtime functions, needed to
618 	 * set the firmware into virtual mode.
619 	 */
620 	if (efi_enabled(EFI_64BIT))
621 		rv = efi_runtime_init64();
622 	else
623 		rv = efi_runtime_init32();
624 
625 	if (rv)
626 		return rv;
627 
628 	set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
629 
630 	return 0;
631 }
632 
633 static int __init efi_memmap_init(void)
634 {
635 	/* Map the EFI memory map */
636 	memmap.map = early_ioremap((unsigned long)memmap.phys_map,
637 				   memmap.nr_map * memmap.desc_size);
638 	if (memmap.map == NULL) {
639 		pr_err("Could not map the memory map!\n");
640 		return -ENOMEM;
641 	}
642 	memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
643 
644 	if (add_efi_memmap)
645 		do_add_efi_memmap();
646 
647 	set_bit(EFI_MEMMAP, &efi.flags);
648 
649 	return 0;
650 }
651 
652 /*
653  * A number of config table entries get remapped to virtual addresses
654  * after entering EFI virtual mode. However, the kexec kernel requires
655  * their physical addresses therefore we pass them via setup_data and
656  * correct those entries to their respective physical addresses here.
657  *
658  * Currently only handles smbios which is necessary for some firmware
659  * implementation.
660  */
661 static int __init efi_reuse_config(u64 tables, int nr_tables)
662 {
663 	int i, sz, ret = 0;
664 	void *p, *tablep;
665 	struct efi_setup_data *data;
666 
667 	if (!efi_setup)
668 		return 0;
669 
670 	if (!efi_enabled(EFI_64BIT))
671 		return 0;
672 
673 	data = early_memremap(efi_setup, sizeof(*data));
674 	if (!data) {
675 		ret = -ENOMEM;
676 		goto out;
677 	}
678 
679 	if (!data->smbios)
680 		goto out_memremap;
681 
682 	sz = sizeof(efi_config_table_64_t);
683 
684 	p = tablep = early_memremap(tables, nr_tables * sz);
685 	if (!p) {
686 		pr_err("Could not map Configuration table!\n");
687 		ret = -ENOMEM;
688 		goto out_memremap;
689 	}
690 
691 	for (i = 0; i < efi.systab->nr_tables; i++) {
692 		efi_guid_t guid;
693 
694 		guid = ((efi_config_table_64_t *)p)->guid;
695 
696 		if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
697 			((efi_config_table_64_t *)p)->table = data->smbios;
698 		p += sz;
699 	}
700 	early_iounmap(tablep, nr_tables * sz);
701 
702 out_memremap:
703 	early_iounmap(data, sizeof(*data));
704 out:
705 	return ret;
706 }
707 
708 void __init efi_init(void)
709 {
710 	efi_char16_t *c16;
711 	char vendor[100] = "unknown";
712 	int i = 0;
713 	void *tmp;
714 
715 #ifdef CONFIG_X86_32
716 	if (boot_params.efi_info.efi_systab_hi ||
717 	    boot_params.efi_info.efi_memmap_hi) {
718 		pr_info("Table located above 4GB, disabling EFI.\n");
719 		return;
720 	}
721 	efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
722 #else
723 	efi_phys.systab = (efi_system_table_t *)
724 			  (boot_params.efi_info.efi_systab |
725 			  ((__u64)boot_params.efi_info.efi_systab_hi<<32));
726 #endif
727 
728 	if (efi_systab_init(efi_phys.systab))
729 		return;
730 
731 	set_bit(EFI_SYSTEM_TABLES, &efi.flags);
732 
733 	efi.config_table = (unsigned long)efi.systab->tables;
734 	efi.fw_vendor	 = (unsigned long)efi.systab->fw_vendor;
735 	efi.runtime	 = (unsigned long)efi.systab->runtime;
736 
737 	/*
738 	 * Show what we know for posterity
739 	 */
740 	c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
741 	if (c16) {
742 		for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
743 			vendor[i] = *c16++;
744 		vendor[i] = '\0';
745 	} else
746 		pr_err("Could not map the firmware vendor!\n");
747 	early_iounmap(tmp, 2);
748 
749 	pr_info("EFI v%u.%.02u by %s\n",
750 		efi.systab->hdr.revision >> 16,
751 		efi.systab->hdr.revision & 0xffff, vendor);
752 
753 	if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
754 		return;
755 
756 	if (efi_config_init(arch_tables))
757 		return;
758 
759 	/*
760 	 * Note: We currently don't support runtime services on an EFI
761 	 * that doesn't match the kernel 32/64-bit mode.
762 	 */
763 
764 	if (!efi_runtime_supported())
765 		pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
766 	else {
767 		if (disable_runtime || efi_runtime_init())
768 			return;
769 	}
770 	if (efi_memmap_init())
771 		return;
772 
773 	set_bit(EFI_MEMMAP, &efi.flags);
774 
775 	print_efi_memmap();
776 }
777 
778 void __init efi_late_init(void)
779 {
780 	efi_bgrt_init();
781 }
782 
783 void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
784 {
785 	u64 addr, npages;
786 
787 	addr = md->virt_addr;
788 	npages = md->num_pages;
789 
790 	memrange_efi_to_native(&addr, &npages);
791 
792 	if (executable)
793 		set_memory_x(addr, npages);
794 	else
795 		set_memory_nx(addr, npages);
796 }
797 
798 void __init runtime_code_page_mkexec(void)
799 {
800 	efi_memory_desc_t *md;
801 	void *p;
802 
803 	/* Make EFI runtime service code area executable */
804 	for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
805 		md = p;
806 
807 		if (md->type != EFI_RUNTIME_SERVICES_CODE)
808 			continue;
809 
810 		efi_set_executable(md, true);
811 	}
812 }
813 
814 void efi_memory_uc(u64 addr, unsigned long size)
815 {
816 	unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
817 	u64 npages;
818 
819 	npages = round_up(size, page_shift) / page_shift;
820 	memrange_efi_to_native(&addr, &npages);
821 	set_memory_uc(addr, npages);
822 }
823 
824 void __init old_map_region(efi_memory_desc_t *md)
825 {
826 	u64 start_pfn, end_pfn, end;
827 	unsigned long size;
828 	void *va;
829 
830 	start_pfn = PFN_DOWN(md->phys_addr);
831 	size	  = md->num_pages << PAGE_SHIFT;
832 	end	  = md->phys_addr + size;
833 	end_pfn   = PFN_UP(end);
834 
835 	if (pfn_range_is_mapped(start_pfn, end_pfn)) {
836 		va = __va(md->phys_addr);
837 
838 		if (!(md->attribute & EFI_MEMORY_WB))
839 			efi_memory_uc((u64)(unsigned long)va, size);
840 	} else
841 		va = efi_ioremap(md->phys_addr, size,
842 				 md->type, md->attribute);
843 
844 	md->virt_addr = (u64) (unsigned long) va;
845 	if (!va)
846 		pr_err("ioremap of 0x%llX failed!\n",
847 		       (unsigned long long)md->phys_addr);
848 }
849 
850 static void native_runtime_setup(void)
851 {
852 	efi.get_time = virt_efi_get_time;
853 	efi.set_time = virt_efi_set_time;
854 	efi.get_wakeup_time = virt_efi_get_wakeup_time;
855 	efi.set_wakeup_time = virt_efi_set_wakeup_time;
856 	efi.get_variable = virt_efi_get_variable;
857 	efi.get_next_variable = virt_efi_get_next_variable;
858 	efi.set_variable = virt_efi_set_variable;
859 	efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
860 	efi.reset_system = virt_efi_reset_system;
861 	efi.query_variable_info = virt_efi_query_variable_info;
862 	efi.update_capsule = virt_efi_update_capsule;
863 	efi.query_capsule_caps = virt_efi_query_capsule_caps;
864 }
865 
866 /* Merge contiguous regions of the same type and attribute */
867 static void __init efi_merge_regions(void)
868 {
869 	void *p;
870 	efi_memory_desc_t *md, *prev_md = NULL;
871 
872 	for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
873 		u64 prev_size;
874 		md = p;
875 
876 		if (!prev_md) {
877 			prev_md = md;
878 			continue;
879 		}
880 
881 		if (prev_md->type != md->type ||
882 		    prev_md->attribute != md->attribute) {
883 			prev_md = md;
884 			continue;
885 		}
886 
887 		prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
888 
889 		if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
890 			prev_md->num_pages += md->num_pages;
891 			md->type = EFI_RESERVED_TYPE;
892 			md->attribute = 0;
893 			continue;
894 		}
895 		prev_md = md;
896 	}
897 }
898 
899 static void __init get_systab_virt_addr(efi_memory_desc_t *md)
900 {
901 	unsigned long size;
902 	u64 end, systab;
903 
904 	size = md->num_pages << EFI_PAGE_SHIFT;
905 	end = md->phys_addr + size;
906 	systab = (u64)(unsigned long)efi_phys.systab;
907 	if (md->phys_addr <= systab && systab < end) {
908 		systab += md->virt_addr - md->phys_addr;
909 		efi.systab = (efi_system_table_t *)(unsigned long)systab;
910 	}
911 }
912 
913 static void __init save_runtime_map(void)
914 {
915 #ifdef CONFIG_KEXEC
916 	efi_memory_desc_t *md;
917 	void *tmp, *p, *q = NULL;
918 	int count = 0;
919 
920 	if (efi_enabled(EFI_OLD_MEMMAP))
921 		return;
922 
923 	for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
924 		md = p;
925 
926 		if (!(md->attribute & EFI_MEMORY_RUNTIME) ||
927 		    (md->type == EFI_BOOT_SERVICES_CODE) ||
928 		    (md->type == EFI_BOOT_SERVICES_DATA))
929 			continue;
930 		tmp = krealloc(q, (count + 1) * memmap.desc_size, GFP_KERNEL);
931 		if (!tmp)
932 			goto out;
933 		q = tmp;
934 
935 		memcpy(q + count * memmap.desc_size, md, memmap.desc_size);
936 		count++;
937 	}
938 
939 	efi_runtime_map_setup(q, count, memmap.desc_size);
940 	return;
941 
942 out:
943 	kfree(q);
944 	pr_err("Error saving runtime map, efi runtime on kexec non-functional!!\n");
945 #endif
946 }
947 
948 static void *realloc_pages(void *old_memmap, int old_shift)
949 {
950 	void *ret;
951 
952 	ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1);
953 	if (!ret)
954 		goto out;
955 
956 	/*
957 	 * A first-time allocation doesn't have anything to copy.
958 	 */
959 	if (!old_memmap)
960 		return ret;
961 
962 	memcpy(ret, old_memmap, PAGE_SIZE << old_shift);
963 
964 out:
965 	free_pages((unsigned long)old_memmap, old_shift);
966 	return ret;
967 }
968 
969 /*
970  * Map the efi memory ranges of the runtime services and update new_mmap with
971  * virtual addresses.
972  */
973 static void * __init efi_map_regions(int *count, int *pg_shift)
974 {
975 	void *p, *new_memmap = NULL;
976 	unsigned long left = 0;
977 	efi_memory_desc_t *md;
978 
979 	for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
980 		md = p;
981 		if (!(md->attribute & EFI_MEMORY_RUNTIME)) {
982 #ifdef CONFIG_X86_64
983 			if (md->type != EFI_BOOT_SERVICES_CODE &&
984 			    md->type != EFI_BOOT_SERVICES_DATA)
985 #endif
986 				continue;
987 		}
988 
989 		efi_map_region(md);
990 		get_systab_virt_addr(md);
991 
992 		if (left < memmap.desc_size) {
993 			new_memmap = realloc_pages(new_memmap, *pg_shift);
994 			if (!new_memmap)
995 				return NULL;
996 
997 			left += PAGE_SIZE << *pg_shift;
998 			(*pg_shift)++;
999 		}
1000 
1001 		memcpy(new_memmap + (*count * memmap.desc_size), md,
1002 		       memmap.desc_size);
1003 
1004 		left -= memmap.desc_size;
1005 		(*count)++;
1006 	}
1007 
1008 	return new_memmap;
1009 }
1010 
1011 static void __init kexec_enter_virtual_mode(void)
1012 {
1013 #ifdef CONFIG_KEXEC
1014 	efi_memory_desc_t *md;
1015 	void *p;
1016 
1017 	efi.systab = NULL;
1018 
1019 	/*
1020 	 * We don't do virtual mode, since we don't do runtime services, on
1021 	 * non-native EFI
1022 	 */
1023 	if (!efi_is_native()) {
1024 		efi_unmap_memmap();
1025 		return;
1026 	}
1027 
1028 	/*
1029 	* Map efi regions which were passed via setup_data. The virt_addr is a
1030 	* fixed addr which was used in first kernel of a kexec boot.
1031 	*/
1032 	for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1033 		md = p;
1034 		efi_map_region_fixed(md); /* FIXME: add error handling */
1035 		get_systab_virt_addr(md);
1036 	}
1037 
1038 	save_runtime_map();
1039 
1040 	BUG_ON(!efi.systab);
1041 
1042 	efi_sync_low_kernel_mappings();
1043 
1044 	/*
1045 	 * Now that EFI is in virtual mode, update the function
1046 	 * pointers in the runtime service table to the new virtual addresses.
1047 	 *
1048 	 * Call EFI services through wrapper functions.
1049 	 */
1050 	efi.runtime_version = efi_systab.hdr.revision;
1051 
1052 	native_runtime_setup();
1053 
1054 	efi.set_virtual_address_map = NULL;
1055 
1056 	if (efi_enabled(EFI_OLD_MEMMAP) && (__supported_pte_mask & _PAGE_NX))
1057 		runtime_code_page_mkexec();
1058 
1059 	/* clean DUMMY object */
1060 	efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1061 			 EFI_VARIABLE_NON_VOLATILE |
1062 			 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1063 			 EFI_VARIABLE_RUNTIME_ACCESS,
1064 			 0, NULL);
1065 #endif
1066 }
1067 
1068 /*
1069  * This function will switch the EFI runtime services to virtual mode.
1070  * Essentially, we look through the EFI memmap and map every region that
1071  * has the runtime attribute bit set in its memory descriptor into the
1072  * ->trampoline_pgd page table using a top-down VA allocation scheme.
1073  *
1074  * The old method which used to update that memory descriptor with the
1075  * virtual address obtained from ioremap() is still supported when the
1076  * kernel is booted with efi=old_map on its command line. Same old
1077  * method enabled the runtime services to be called without having to
1078  * thunk back into physical mode for every invocation.
1079  *
1080  * The new method does a pagetable switch in a preemption-safe manner
1081  * so that we're in a different address space when calling a runtime
1082  * function. For function arguments passing we do copy the PGDs of the
1083  * kernel page table into ->trampoline_pgd prior to each call.
1084  *
1085  * Specially for kexec boot, efi runtime maps in previous kernel should
1086  * be passed in via setup_data. In that case runtime ranges will be mapped
1087  * to the same virtual addresses as the first kernel, see
1088  * kexec_enter_virtual_mode().
1089  */
1090 static void __init __efi_enter_virtual_mode(void)
1091 {
1092 	int count = 0, pg_shift = 0;
1093 	void *new_memmap = NULL;
1094 	efi_status_t status;
1095 
1096 	efi.systab = NULL;
1097 
1098 	efi_merge_regions();
1099 	new_memmap = efi_map_regions(&count, &pg_shift);
1100 	if (!new_memmap) {
1101 		pr_err("Error reallocating memory, EFI runtime non-functional!\n");
1102 		return;
1103 	}
1104 
1105 	save_runtime_map();
1106 
1107 	BUG_ON(!efi.systab);
1108 
1109 	if (efi_setup_page_tables(__pa(new_memmap), 1 << pg_shift))
1110 		return;
1111 
1112 	efi_sync_low_kernel_mappings();
1113 	efi_dump_pagetable();
1114 
1115 	if (efi_is_native()) {
1116 		status = phys_efi_set_virtual_address_map(
1117 				memmap.desc_size * count,
1118 				memmap.desc_size,
1119 				memmap.desc_version,
1120 				(efi_memory_desc_t *)__pa(new_memmap));
1121 	} else {
1122 		status = efi_thunk_set_virtual_address_map(
1123 				efi_phys.set_virtual_address_map,
1124 				memmap.desc_size * count,
1125 				memmap.desc_size,
1126 				memmap.desc_version,
1127 				(efi_memory_desc_t *)__pa(new_memmap));
1128 	}
1129 
1130 	if (status != EFI_SUCCESS) {
1131 		pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
1132 			 status);
1133 		panic("EFI call to SetVirtualAddressMap() failed!");
1134 	}
1135 
1136 	/*
1137 	 * Now that EFI is in virtual mode, update the function
1138 	 * pointers in the runtime service table to the new virtual addresses.
1139 	 *
1140 	 * Call EFI services through wrapper functions.
1141 	 */
1142 	efi.runtime_version = efi_systab.hdr.revision;
1143 
1144 	if (efi_is_native())
1145 		native_runtime_setup();
1146 	else
1147 		efi_thunk_runtime_setup();
1148 
1149 	efi.set_virtual_address_map = NULL;
1150 
1151 	efi_runtime_mkexec();
1152 
1153 	/*
1154 	 * We mapped the descriptor array into the EFI pagetable above but we're
1155 	 * not unmapping it here. Here's why:
1156 	 *
1157 	 * We're copying select PGDs from the kernel page table to the EFI page
1158 	 * table and when we do so and make changes to those PGDs like unmapping
1159 	 * stuff from them, those changes appear in the kernel page table and we
1160 	 * go boom.
1161 	 *
1162 	 * From setup_real_mode():
1163 	 *
1164 	 * ...
1165 	 * trampoline_pgd[0] = init_level4_pgt[pgd_index(__PAGE_OFFSET)].pgd;
1166 	 *
1167 	 * In this particular case, our allocation is in PGD 0 of the EFI page
1168 	 * table but we've copied that PGD from PGD[272] of the EFI page table:
1169 	 *
1170 	 *	pgd_index(__PAGE_OFFSET = 0xffff880000000000) = 272
1171 	 *
1172 	 * where the direct memory mapping in kernel space is.
1173 	 *
1174 	 * new_memmap's VA comes from that direct mapping and thus clearing it,
1175 	 * it would get cleared in the kernel page table too.
1176 	 *
1177 	 * efi_cleanup_page_tables(__pa(new_memmap), 1 << pg_shift);
1178 	 */
1179 	free_pages((unsigned long)new_memmap, pg_shift);
1180 
1181 	/* clean DUMMY object */
1182 	efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1183 			 EFI_VARIABLE_NON_VOLATILE |
1184 			 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1185 			 EFI_VARIABLE_RUNTIME_ACCESS,
1186 			 0, NULL);
1187 }
1188 
1189 void __init efi_enter_virtual_mode(void)
1190 {
1191 	if (efi_setup)
1192 		kexec_enter_virtual_mode();
1193 	else
1194 		__efi_enter_virtual_mode();
1195 }
1196 
1197 /*
1198  * Convenience functions to obtain memory types and attributes
1199  */
1200 u32 efi_mem_type(unsigned long phys_addr)
1201 {
1202 	efi_memory_desc_t *md;
1203 	void *p;
1204 
1205 	if (!efi_enabled(EFI_MEMMAP))
1206 		return 0;
1207 
1208 	for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1209 		md = p;
1210 		if ((md->phys_addr <= phys_addr) &&
1211 		    (phys_addr < (md->phys_addr +
1212 				  (md->num_pages << EFI_PAGE_SHIFT))))
1213 			return md->type;
1214 	}
1215 	return 0;
1216 }
1217 
1218 u64 efi_mem_attributes(unsigned long phys_addr)
1219 {
1220 	efi_memory_desc_t *md;
1221 	void *p;
1222 
1223 	for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1224 		md = p;
1225 		if ((md->phys_addr <= phys_addr) &&
1226 		    (phys_addr < (md->phys_addr +
1227 				  (md->num_pages << EFI_PAGE_SHIFT))))
1228 			return md->attribute;
1229 	}
1230 	return 0;
1231 }
1232 
1233 /*
1234  * Some firmware implementations refuse to boot if there's insufficient space
1235  * in the variable store. Ensure that we never use more than a safe limit.
1236  *
1237  * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
1238  * store.
1239  */
1240 efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
1241 {
1242 	efi_status_t status;
1243 	u64 storage_size, remaining_size, max_size;
1244 
1245 	if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
1246 		return 0;
1247 
1248 	status = efi.query_variable_info(attributes, &storage_size,
1249 					 &remaining_size, &max_size);
1250 	if (status != EFI_SUCCESS)
1251 		return status;
1252 
1253 	/*
1254 	 * We account for that by refusing the write if permitting it would
1255 	 * reduce the available space to under 5KB. This figure was provided by
1256 	 * Samsung, so should be safe.
1257 	 */
1258 	if ((remaining_size - size < EFI_MIN_RESERVE) &&
1259 		!efi_no_storage_paranoia) {
1260 
1261 		/*
1262 		 * Triggering garbage collection may require that the firmware
1263 		 * generate a real EFI_OUT_OF_RESOURCES error. We can force
1264 		 * that by attempting to use more space than is available.
1265 		 */
1266 		unsigned long dummy_size = remaining_size + 1024;
1267 		void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
1268 
1269 		if (!dummy)
1270 			return EFI_OUT_OF_RESOURCES;
1271 
1272 		status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1273 					  EFI_VARIABLE_NON_VOLATILE |
1274 					  EFI_VARIABLE_BOOTSERVICE_ACCESS |
1275 					  EFI_VARIABLE_RUNTIME_ACCESS,
1276 					  dummy_size, dummy);
1277 
1278 		if (status == EFI_SUCCESS) {
1279 			/*
1280 			 * This should have failed, so if it didn't make sure
1281 			 * that we delete it...
1282 			 */
1283 			efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1284 					 EFI_VARIABLE_NON_VOLATILE |
1285 					 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1286 					 EFI_VARIABLE_RUNTIME_ACCESS,
1287 					 0, dummy);
1288 		}
1289 
1290 		kfree(dummy);
1291 
1292 		/*
1293 		 * The runtime code may now have triggered a garbage collection
1294 		 * run, so check the variable info again
1295 		 */
1296 		status = efi.query_variable_info(attributes, &storage_size,
1297 						 &remaining_size, &max_size);
1298 
1299 		if (status != EFI_SUCCESS)
1300 			return status;
1301 
1302 		/*
1303 		 * There still isn't enough room, so return an error
1304 		 */
1305 		if (remaining_size - size < EFI_MIN_RESERVE)
1306 			return EFI_OUT_OF_RESOURCES;
1307 	}
1308 
1309 	return EFI_SUCCESS;
1310 }
1311 EXPORT_SYMBOL_GPL(efi_query_variable_store);
1312 
1313 static int __init parse_efi_cmdline(char *str)
1314 {
1315 	if (*str == '=')
1316 		str++;
1317 
1318 	if (!strncmp(str, "old_map", 7))
1319 		set_bit(EFI_OLD_MEMMAP, &efi.flags);
1320 
1321 	return 0;
1322 }
1323 early_param("efi", parse_efi_cmdline);
1324 
1325 void __init efi_apply_memmap_quirks(void)
1326 {
1327 	/*
1328 	 * Once setup is done earlier, unmap the EFI memory map on mismatched
1329 	 * firmware/kernel architectures since there is no support for runtime
1330 	 * services.
1331 	 */
1332 	if (!efi_runtime_supported()) {
1333 		pr_info("efi: Setup done, disabling due to 32/64-bit mismatch\n");
1334 		efi_unmap_memmap();
1335 	}
1336 
1337 	/*
1338 	 * UV doesn't support the new EFI pagetable mapping yet.
1339 	 */
1340 	if (is_uv_system())
1341 		set_bit(EFI_OLD_MEMMAP, &efi.flags);
1342 }
1343