xref: /openbmc/linux/drivers/firmware/efi/arm-runtime.c (revision 96d3c5a7d20ec546e44695983fe0508c6f904248)
14febfb8dSArd Biesheuvel // SPDX-License-Identifier: GPL-2.0
2e5bc22a4SArd Biesheuvel /*
3e5bc22a4SArd Biesheuvel  * Extensible Firmware Interface
4e5bc22a4SArd Biesheuvel  *
5e5bc22a4SArd Biesheuvel  * Based on Extensible Firmware Interface Specification version 2.4
6e5bc22a4SArd Biesheuvel  *
7e5bc22a4SArd Biesheuvel  * Copyright (C) 2013, 2014 Linaro Ltd.
8e5bc22a4SArd Biesheuvel  */
9e5bc22a4SArd Biesheuvel 
10bb817befSArd Biesheuvel #include <linux/dmi.h>
11e5bc22a4SArd Biesheuvel #include <linux/efi.h>
12f7d92489SArd Biesheuvel #include <linux/io.h>
13e5bc22a4SArd Biesheuvel #include <linux/memblock.h>
14e5bc22a4SArd Biesheuvel #include <linux/mm_types.h>
15e5bc22a4SArd Biesheuvel #include <linux/preempt.h>
16e5bc22a4SArd Biesheuvel #include <linux/rbtree.h>
17e5bc22a4SArd Biesheuvel #include <linux/rwsem.h>
18e5bc22a4SArd Biesheuvel #include <linux/sched.h>
19e5bc22a4SArd Biesheuvel #include <linux/slab.h>
20e5bc22a4SArd Biesheuvel #include <linux/spinlock.h>
2165fddcfcSMike Rapoport #include <linux/pgtable.h>
22e5bc22a4SArd Biesheuvel 
23e5bc22a4SArd Biesheuvel #include <asm/cacheflush.h>
24e5bc22a4SArd Biesheuvel #include <asm/efi.h>
25e5bc22a4SArd Biesheuvel #include <asm/mmu.h>
26f7d92489SArd Biesheuvel #include <asm/pgalloc.h>
27e5bc22a4SArd Biesheuvel 
28e513ffd8SWang Kefeng #if defined(CONFIG_PTDUMP_DEBUGFS) || defined(CONFIG_ARM_PTDUMP_DEBUGFS)
299d80448aSArd Biesheuvel #include <asm/ptdump.h>
309d80448aSArd Biesheuvel 
319d80448aSArd Biesheuvel static struct ptdump_info efi_ptdump_info = {
329d80448aSArd Biesheuvel 	.mm		= &efi_mm,
339d80448aSArd Biesheuvel 	.markers	= (struct addr_marker[]){
349d80448aSArd Biesheuvel 		{ 0,				"UEFI runtime start" },
35e513ffd8SWang Kefeng 		{ EFI_RUNTIME_MAP_END,		"UEFI runtime end" },
3674c953caSQian Cai 		{ -1,				NULL }
379d80448aSArd Biesheuvel 	},
389d80448aSArd Biesheuvel 	.base_addr	= 0,
399d80448aSArd Biesheuvel };
409d80448aSArd Biesheuvel 
ptdump_init(void)419d80448aSArd Biesheuvel static int __init ptdump_init(void)
429d80448aSArd Biesheuvel {
4367f52a95SNathan Chancellor 	if (efi_enabled(EFI_RUNTIME_SERVICES))
4467f52a95SNathan Chancellor 		ptdump_debugfs_register(&efi_ptdump_info, "efi_page_tables");
456b31a2faSMark Rutland 
4667f52a95SNathan Chancellor 	return 0;
479d80448aSArd Biesheuvel }
489d80448aSArd Biesheuvel device_initcall(ptdump_init);
499d80448aSArd Biesheuvel 
509d80448aSArd Biesheuvel #endif
519d80448aSArd Biesheuvel 
efi_virtmap_init(void)52e5bc22a4SArd Biesheuvel static bool __init efi_virtmap_init(void)
53e5bc22a4SArd Biesheuvel {
54e5bc22a4SArd Biesheuvel 	efi_memory_desc_t *md;
55e5bc22a4SArd Biesheuvel 
56f7d92489SArd Biesheuvel 	efi_mm.pgd = pgd_alloc(&efi_mm);
57d1eb9814SArd Biesheuvel 	mm_init_cpumask(&efi_mm);
58e5bc22a4SArd Biesheuvel 	init_new_context(NULL, &efi_mm);
59e5bc22a4SArd Biesheuvel 
6078ce248fSMatt Fleming 	for_each_efi_memory_desc(md) {
61f7d92489SArd Biesheuvel 		phys_addr_t phys = md->phys_addr;
62f7d92489SArd Biesheuvel 		int ret;
63e5bc22a4SArd Biesheuvel 
64e5bc22a4SArd Biesheuvel 		if (!(md->attribute & EFI_MEMORY_RUNTIME))
65e5bc22a4SArd Biesheuvel 			continue;
6637926f96SArd Biesheuvel 		if (md->virt_addr == U64_MAX)
67e5bc22a4SArd Biesheuvel 			return false;
68e5bc22a4SArd Biesheuvel 
69f7d92489SArd Biesheuvel 		ret = efi_create_mapping(&efi_mm, md);
701832e641SArd Biesheuvel 		if (ret) {
71f7d92489SArd Biesheuvel 			pr_warn("  EFI remap %pa: failed to create mapping (%d)\n",
72f7d92489SArd Biesheuvel 				&phys, ret);
73f7d92489SArd Biesheuvel 			return false;
74f7d92489SArd Biesheuvel 		}
75789957efSArd Biesheuvel 	}
76789957efSArd Biesheuvel 
77789957efSArd Biesheuvel 	if (efi_memattr_apply_permissions(&efi_mm, efi_set_mapping_permissions))
78789957efSArd Biesheuvel 		return false;
79789957efSArd Biesheuvel 
80789957efSArd Biesheuvel 	return true;
81e5bc22a4SArd Biesheuvel }
82e5bc22a4SArd Biesheuvel 
83e5bc22a4SArd Biesheuvel /*
84e5bc22a4SArd Biesheuvel  * Enable the UEFI Runtime Services if all prerequisites are in place, i.e.,
85e5bc22a4SArd Biesheuvel  * non-early mapping of the UEFI system table and virtual mappings for all
86e5bc22a4SArd Biesheuvel  * EFI_MEMORY_RUNTIME regions.
87e5bc22a4SArd Biesheuvel  */
arm_enable_runtime_services(void)88f7d92489SArd Biesheuvel static int __init arm_enable_runtime_services(void)
89e5bc22a4SArd Biesheuvel {
90e5bc22a4SArd Biesheuvel 	u64 mapsize;
91e5bc22a4SArd Biesheuvel 
9233412b86SArd Biesheuvel 	if (!efi_enabled(EFI_BOOT)) {
93e5bc22a4SArd Biesheuvel 		pr_info("EFI services will not be available.\n");
94e5bc22a4SArd Biesheuvel 		return 0;
95e5bc22a4SArd Biesheuvel 	}
96e5bc22a4SArd Biesheuvel 
973ea86495SArd Biesheuvel 	efi_memmap_unmap();
983ea86495SArd Biesheuvel 
9920d12cf9SAKASHI Takahiro 	mapsize = efi.memmap.desc_size * efi.memmap.nr_map;
10020d12cf9SAKASHI Takahiro 
10120d12cf9SAKASHI Takahiro 	if (efi_memmap_init_late(efi.memmap.phys_map, mapsize)) {
10220d12cf9SAKASHI Takahiro 		pr_err("Failed to remap EFI memory map\n");
10320d12cf9SAKASHI Takahiro 		return 0;
10420d12cf9SAKASHI Takahiro 	}
10520d12cf9SAKASHI Takahiro 
10616993c0fSDan Williams 	if (efi_soft_reserve_enabled()) {
10716993c0fSDan Williams 		efi_memory_desc_t *md;
10816993c0fSDan Williams 
10916993c0fSDan Williams 		for_each_efi_memory_desc(md) {
110*cf3d6813SAndrew Bresticker 			u64 md_size = md->num_pages << EFI_PAGE_SHIFT;
11116993c0fSDan Williams 			struct resource *res;
11216993c0fSDan Williams 
11316993c0fSDan Williams 			if (!(md->attribute & EFI_MEMORY_SP))
11416993c0fSDan Williams 				continue;
11516993c0fSDan Williams 
11616993c0fSDan Williams 			res = kzalloc(sizeof(*res), GFP_KERNEL);
11716993c0fSDan Williams 			if (WARN_ON(!res))
11816993c0fSDan Williams 				break;
11916993c0fSDan Williams 
12016993c0fSDan Williams 			res->start	= md->phys_addr;
12116993c0fSDan Williams 			res->end	= md->phys_addr + md_size - 1;
12216993c0fSDan Williams 			res->name	= "Soft Reserved";
12316993c0fSDan Williams 			res->flags	= IORESOURCE_MEM;
12416993c0fSDan Williams 			res->desc	= IORES_DESC_SOFT_RESERVED;
12516993c0fSDan Williams 
12616993c0fSDan Williams 			insert_resource(&iomem_resource, res);
12716993c0fSDan Williams 		}
12816993c0fSDan Williams 	}
12916993c0fSDan Williams 
130e5bc22a4SArd Biesheuvel 	if (efi_runtime_disabled()) {
131e5bc22a4SArd Biesheuvel 		pr_info("EFI runtime services will be disabled.\n");
132e5bc22a4SArd Biesheuvel 		return 0;
133e5bc22a4SArd Biesheuvel 	}
134e5bc22a4SArd Biesheuvel 
1350cac5c30SShannon Zhao 	if (efi_enabled(EFI_RUNTIME_SERVICES)) {
1360cac5c30SShannon Zhao 		pr_info("EFI runtime services access via paravirt.\n");
1370cac5c30SShannon Zhao 		return 0;
1380cac5c30SShannon Zhao 	}
1390cac5c30SShannon Zhao 
140e5bc22a4SArd Biesheuvel 	pr_info("Remapping and enabling EFI services.\n");
141e5bc22a4SArd Biesheuvel 
142e5bc22a4SArd Biesheuvel 	if (!efi_virtmap_init()) {
14314c43be6SArd Biesheuvel 		pr_err("UEFI virtual mapping missing or invalid -- runtime services will not be available\n");
144e5bc22a4SArd Biesheuvel 		return -ENOMEM;
145e5bc22a4SArd Biesheuvel 	}
146e5bc22a4SArd Biesheuvel 
147e5bc22a4SArd Biesheuvel 	/* Set up runtime services function pointers */
148e5bc22a4SArd Biesheuvel 	efi_native_runtime_setup();
149e5bc22a4SArd Biesheuvel 	set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
150e5bc22a4SArd Biesheuvel 
151e5bc22a4SArd Biesheuvel 	return 0;
152e5bc22a4SArd Biesheuvel }
153f7d92489SArd Biesheuvel early_initcall(arm_enable_runtime_services);
154e5bc22a4SArd Biesheuvel 
efi_virtmap_load(void)155e5bc22a4SArd Biesheuvel void efi_virtmap_load(void)
156e5bc22a4SArd Biesheuvel {
157e5bc22a4SArd Biesheuvel 	preempt_disable();
158e5bc22a4SArd Biesheuvel 	efi_set_pgd(&efi_mm);
159e5bc22a4SArd Biesheuvel }
160e5bc22a4SArd Biesheuvel 
efi_virtmap_unload(void)161e5bc22a4SArd Biesheuvel void efi_virtmap_unload(void)
162e5bc22a4SArd Biesheuvel {
163e5bc22a4SArd Biesheuvel 	efi_set_pgd(current->active_mm);
164e5bc22a4SArd Biesheuvel 	preempt_enable();
165e5bc22a4SArd Biesheuvel }
166bb817befSArd Biesheuvel 
167bb817befSArd Biesheuvel 
arm_dmi_init(void)168bb817befSArd Biesheuvel static int __init arm_dmi_init(void)
169bb817befSArd Biesheuvel {
170bb817befSArd Biesheuvel 	/*
1710fca0812SRobert Richter 	 * On arm64/ARM, DMI depends on UEFI, and dmi_setup() needs to
172bb817befSArd Biesheuvel 	 * be called early because dmi_id_init(), which is an arch_initcall
173bb817befSArd Biesheuvel 	 * itself, depends on dmi_scan_machine() having been called already.
174bb817befSArd Biesheuvel 	 */
1750fca0812SRobert Richter 	dmi_setup();
176bb817befSArd Biesheuvel 	return 0;
177bb817befSArd Biesheuvel }
178bb817befSArd Biesheuvel core_initcall(arm_dmi_init);
179