Linux Audio

Check our new training course

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