1b91540d5SAtish Patra // SPDX-License-Identifier: GPL-2.0 2b91540d5SAtish Patra /* 3b91540d5SAtish Patra * Extensible Firmware Interface 4b91540d5SAtish Patra * 5b91540d5SAtish Patra * Copyright (C) 2020 Western Digital Corporation or its affiliates. 6b91540d5SAtish Patra * 7b91540d5SAtish Patra * Based on Extensible Firmware Interface Specification version 2.4 8b91540d5SAtish Patra * Adapted from drivers/firmware/efi/arm-runtime.c 9b91540d5SAtish Patra * 10b91540d5SAtish Patra */ 11b91540d5SAtish Patra 12b91540d5SAtish Patra #include <linux/dmi.h> 13b91540d5SAtish Patra #include <linux/efi.h> 14b91540d5SAtish Patra #include <linux/io.h> 15b91540d5SAtish Patra #include <linux/memblock.h> 16b91540d5SAtish Patra #include <linux/mm_types.h> 17b91540d5SAtish Patra #include <linux/preempt.h> 18b91540d5SAtish Patra #include <linux/rbtree.h> 19b91540d5SAtish Patra #include <linux/rwsem.h> 20b91540d5SAtish Patra #include <linux/sched.h> 21b91540d5SAtish Patra #include <linux/slab.h> 22b91540d5SAtish Patra #include <linux/spinlock.h> 23b91540d5SAtish Patra #include <linux/pgtable.h> 24b91540d5SAtish Patra 25b91540d5SAtish Patra #include <asm/cacheflush.h> 26b91540d5SAtish Patra #include <asm/efi.h> 27b91540d5SAtish Patra #include <asm/mmu.h> 28b91540d5SAtish Patra #include <asm/pgalloc.h> 29b91540d5SAtish Patra 30b91540d5SAtish Patra static bool __init efi_virtmap_init(void) 31b91540d5SAtish Patra { 32b91540d5SAtish Patra efi_memory_desc_t *md; 33b91540d5SAtish Patra 34b91540d5SAtish Patra efi_mm.pgd = pgd_alloc(&efi_mm); 35b91540d5SAtish Patra mm_init_cpumask(&efi_mm); 36b91540d5SAtish Patra init_new_context(NULL, &efi_mm); 37b91540d5SAtish Patra 38b91540d5SAtish Patra for_each_efi_memory_desc(md) { 39b91540d5SAtish Patra phys_addr_t phys = md->phys_addr; 40b91540d5SAtish Patra int ret; 41b91540d5SAtish Patra 42b91540d5SAtish Patra if (!(md->attribute & EFI_MEMORY_RUNTIME)) 43b91540d5SAtish Patra continue; 44*37926f96SArd Biesheuvel if (md->virt_addr == U64_MAX) 45b91540d5SAtish Patra return false; 46b91540d5SAtish Patra 47b91540d5SAtish Patra ret = efi_create_mapping(&efi_mm, md); 48b91540d5SAtish Patra if (ret) { 49b91540d5SAtish Patra pr_warn(" EFI remap %pa: failed to create mapping (%d)\n", 50b91540d5SAtish Patra &phys, ret); 51b91540d5SAtish Patra return false; 52b91540d5SAtish Patra } 53b91540d5SAtish Patra } 54b91540d5SAtish Patra 55b91540d5SAtish Patra if (efi_memattr_apply_permissions(&efi_mm, efi_set_mapping_permissions)) 56b91540d5SAtish Patra return false; 57b91540d5SAtish Patra 58b91540d5SAtish Patra return true; 59b91540d5SAtish Patra } 60b91540d5SAtish Patra 61b91540d5SAtish Patra /* 62b91540d5SAtish Patra * Enable the UEFI Runtime Services if all prerequisites are in place, i.e., 63b91540d5SAtish Patra * non-early mapping of the UEFI system table and virtual mappings for all 64b91540d5SAtish Patra * EFI_MEMORY_RUNTIME regions. 65b91540d5SAtish Patra */ 66b91540d5SAtish Patra static int __init riscv_enable_runtime_services(void) 67b91540d5SAtish Patra { 68b91540d5SAtish Patra u64 mapsize; 69b91540d5SAtish Patra 70b91540d5SAtish Patra if (!efi_enabled(EFI_BOOT)) { 71b91540d5SAtish Patra pr_info("EFI services will not be available.\n"); 72b91540d5SAtish Patra return 0; 73b91540d5SAtish Patra } 74b91540d5SAtish Patra 75b91540d5SAtish Patra efi_memmap_unmap(); 76b91540d5SAtish Patra 77b91540d5SAtish Patra mapsize = efi.memmap.desc_size * efi.memmap.nr_map; 78b91540d5SAtish Patra 79b91540d5SAtish Patra if (efi_memmap_init_late(efi.memmap.phys_map, mapsize)) { 80b91540d5SAtish Patra pr_err("Failed to remap EFI memory map\n"); 81b91540d5SAtish Patra return 0; 82b91540d5SAtish Patra } 83b91540d5SAtish Patra 84b91540d5SAtish Patra if (efi_soft_reserve_enabled()) { 85b91540d5SAtish Patra efi_memory_desc_t *md; 86b91540d5SAtish Patra 87b91540d5SAtish Patra for_each_efi_memory_desc(md) { 88b91540d5SAtish Patra int md_size = md->num_pages << EFI_PAGE_SHIFT; 89b91540d5SAtish Patra struct resource *res; 90b91540d5SAtish Patra 91b91540d5SAtish Patra if (!(md->attribute & EFI_MEMORY_SP)) 92b91540d5SAtish Patra continue; 93b91540d5SAtish Patra 94b91540d5SAtish Patra res = kzalloc(sizeof(*res), GFP_KERNEL); 95b91540d5SAtish Patra if (WARN_ON(!res)) 96b91540d5SAtish Patra break; 97b91540d5SAtish Patra 98b91540d5SAtish Patra res->start = md->phys_addr; 99b91540d5SAtish Patra res->end = md->phys_addr + md_size - 1; 100b91540d5SAtish Patra res->name = "Soft Reserved"; 101b91540d5SAtish Patra res->flags = IORESOURCE_MEM; 102b91540d5SAtish Patra res->desc = IORES_DESC_SOFT_RESERVED; 103b91540d5SAtish Patra 104b91540d5SAtish Patra insert_resource(&iomem_resource, res); 105b91540d5SAtish Patra } 106b91540d5SAtish Patra } 107b91540d5SAtish Patra 108b91540d5SAtish Patra if (efi_runtime_disabled()) { 109b91540d5SAtish Patra pr_info("EFI runtime services will be disabled.\n"); 110b91540d5SAtish Patra return 0; 111b91540d5SAtish Patra } 112b91540d5SAtish Patra 113b91540d5SAtish Patra if (efi_enabled(EFI_RUNTIME_SERVICES)) { 114b91540d5SAtish Patra pr_info("EFI runtime services access via paravirt.\n"); 115b91540d5SAtish Patra return 0; 116b91540d5SAtish Patra } 117b91540d5SAtish Patra 118b91540d5SAtish Patra pr_info("Remapping and enabling EFI services.\n"); 119b91540d5SAtish Patra 120b91540d5SAtish Patra if (!efi_virtmap_init()) { 121b91540d5SAtish Patra pr_err("UEFI virtual mapping missing or invalid -- runtime services will not be available\n"); 122b91540d5SAtish Patra return -ENOMEM; 123b91540d5SAtish Patra } 124b91540d5SAtish Patra 125b91540d5SAtish Patra /* Set up runtime services function pointers */ 126b91540d5SAtish Patra efi_native_runtime_setup(); 127b91540d5SAtish Patra set_bit(EFI_RUNTIME_SERVICES, &efi.flags); 128b91540d5SAtish Patra 129b91540d5SAtish Patra return 0; 130b91540d5SAtish Patra } 131b91540d5SAtish Patra early_initcall(riscv_enable_runtime_services); 132b91540d5SAtish Patra 133b91540d5SAtish Patra void efi_virtmap_load(void) 134b91540d5SAtish Patra { 135b91540d5SAtish Patra preempt_disable(); 136b91540d5SAtish Patra switch_mm(current->active_mm, &efi_mm, NULL); 137b91540d5SAtish Patra } 138b91540d5SAtish Patra 139b91540d5SAtish Patra void efi_virtmap_unload(void) 140b91540d5SAtish Patra { 141b91540d5SAtish Patra switch_mm(&efi_mm, current->active_mm, NULL); 142b91540d5SAtish Patra preempt_enable(); 143b91540d5SAtish Patra } 144