Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Extensible Firmware Interface
0004  *
0005  * Based on Extensible Firmware Interface Specification version 2.4
0006  *
0007  * Copyright (C) 2013 - 2015 Linaro Ltd.
0008  */
0009 
0010 #define pr_fmt(fmt) "efi: " fmt
0011 
0012 #include <linux/efi.h>
0013 #include <linux/fwnode.h>
0014 #include <linux/init.h>
0015 #include <linux/memblock.h>
0016 #include <linux/mm_types.h>
0017 #include <linux/of.h>
0018 #include <linux/of_address.h>
0019 #include <linux/of_fdt.h>
0020 #include <linux/platform_device.h>
0021 #include <linux/screen_info.h>
0022 
0023 #include <asm/efi.h>
0024 
0025 static int __init is_memory(efi_memory_desc_t *md)
0026 {
0027     if (md->attribute & (EFI_MEMORY_WB|EFI_MEMORY_WT|EFI_MEMORY_WC))
0028         return 1;
0029     return 0;
0030 }
0031 
0032 /*
0033  * Translate a EFI virtual address into a physical address: this is necessary,
0034  * as some data members of the EFI system table are virtually remapped after
0035  * SetVirtualAddressMap() has been called.
0036  */
0037 static phys_addr_t __init efi_to_phys(unsigned long addr)
0038 {
0039     efi_memory_desc_t *md;
0040 
0041     for_each_efi_memory_desc(md) {
0042         if (!(md->attribute & EFI_MEMORY_RUNTIME))
0043             continue;
0044         if (md->virt_addr == 0)
0045             /* no virtual mapping has been installed by the stub */
0046             break;
0047         if (md->virt_addr <= addr &&
0048             (addr - md->virt_addr) < (md->num_pages << EFI_PAGE_SHIFT))
0049             return md->phys_addr + addr - md->virt_addr;
0050     }
0051     return addr;
0052 }
0053 
0054 static __initdata unsigned long screen_info_table = EFI_INVALID_TABLE_ADDR;
0055 static __initdata unsigned long cpu_state_table = EFI_INVALID_TABLE_ADDR;
0056 
0057 static const efi_config_table_type_t arch_tables[] __initconst = {
0058     {LINUX_EFI_ARM_SCREEN_INFO_TABLE_GUID, &screen_info_table},
0059     {LINUX_EFI_ARM_CPU_STATE_TABLE_GUID, &cpu_state_table},
0060     {}
0061 };
0062 
0063 static void __init init_screen_info(void)
0064 {
0065     struct screen_info *si;
0066 
0067     if (IS_ENABLED(CONFIG_ARM) &&
0068         screen_info_table != EFI_INVALID_TABLE_ADDR) {
0069         si = early_memremap_ro(screen_info_table, sizeof(*si));
0070         if (!si) {
0071             pr_err("Could not map screen_info config table\n");
0072             return;
0073         }
0074         screen_info = *si;
0075         early_memunmap(si, sizeof(*si));
0076 
0077         /* dummycon on ARM needs non-zero values for columns/lines */
0078         screen_info.orig_video_cols = 80;
0079         screen_info.orig_video_lines = 25;
0080     }
0081 
0082     if (screen_info.orig_video_isVGA == VIDEO_TYPE_EFI &&
0083         memblock_is_map_memory(screen_info.lfb_base))
0084         memblock_mark_nomap(screen_info.lfb_base, screen_info.lfb_size);
0085 }
0086 
0087 static int __init uefi_init(u64 efi_system_table)
0088 {
0089     efi_config_table_t *config_tables;
0090     efi_system_table_t *systab;
0091     size_t table_size;
0092     int retval;
0093 
0094     systab = early_memremap_ro(efi_system_table, sizeof(efi_system_table_t));
0095     if (systab == NULL) {
0096         pr_warn("Unable to map EFI system table.\n");
0097         return -ENOMEM;
0098     }
0099 
0100     set_bit(EFI_BOOT, &efi.flags);
0101     if (IS_ENABLED(CONFIG_64BIT))
0102         set_bit(EFI_64BIT, &efi.flags);
0103 
0104     retval = efi_systab_check_header(&systab->hdr, 2);
0105     if (retval)
0106         goto out;
0107 
0108     efi.runtime = systab->runtime;
0109     efi.runtime_version = systab->hdr.revision;
0110 
0111     efi_systab_report_header(&systab->hdr, efi_to_phys(systab->fw_vendor));
0112 
0113     table_size = sizeof(efi_config_table_t) * systab->nr_tables;
0114     config_tables = early_memremap_ro(efi_to_phys(systab->tables),
0115                       table_size);
0116     if (config_tables == NULL) {
0117         pr_warn("Unable to map EFI config table array.\n");
0118         retval = -ENOMEM;
0119         goto out;
0120     }
0121     retval = efi_config_parse_tables(config_tables, systab->nr_tables,
0122                      IS_ENABLED(CONFIG_ARM) ? arch_tables
0123                                 : NULL);
0124 
0125     early_memunmap(config_tables, table_size);
0126 out:
0127     early_memunmap(systab, sizeof(efi_system_table_t));
0128     return retval;
0129 }
0130 
0131 /*
0132  * Return true for regions that can be used as System RAM.
0133  */
0134 static __init int is_usable_memory(efi_memory_desc_t *md)
0135 {
0136     switch (md->type) {
0137     case EFI_LOADER_CODE:
0138     case EFI_LOADER_DATA:
0139     case EFI_ACPI_RECLAIM_MEMORY:
0140     case EFI_BOOT_SERVICES_CODE:
0141     case EFI_BOOT_SERVICES_DATA:
0142     case EFI_CONVENTIONAL_MEMORY:
0143     case EFI_PERSISTENT_MEMORY:
0144         /*
0145          * Special purpose memory is 'soft reserved', which means it
0146          * is set aside initially, but can be hotplugged back in or
0147          * be assigned to the dax driver after boot.
0148          */
0149         if (efi_soft_reserve_enabled() &&
0150             (md->attribute & EFI_MEMORY_SP))
0151             return false;
0152 
0153         /*
0154          * According to the spec, these regions are no longer reserved
0155          * after calling ExitBootServices(). However, we can only use
0156          * them as System RAM if they can be mapped writeback cacheable.
0157          */
0158         return (md->attribute & EFI_MEMORY_WB);
0159     default:
0160         break;
0161     }
0162     return false;
0163 }
0164 
0165 static __init void reserve_regions(void)
0166 {
0167     efi_memory_desc_t *md;
0168     u64 paddr, npages, size;
0169 
0170     if (efi_enabled(EFI_DBG))
0171         pr_info("Processing EFI memory map:\n");
0172 
0173     /*
0174      * Discard memblocks discovered so far: if there are any at this
0175      * point, they originate from memory nodes in the DT, and UEFI
0176      * uses its own memory map instead.
0177      */
0178     memblock_dump_all();
0179     memblock_remove(0, PHYS_ADDR_MAX);
0180 
0181     for_each_efi_memory_desc(md) {
0182         paddr = md->phys_addr;
0183         npages = md->num_pages;
0184 
0185         if (efi_enabled(EFI_DBG)) {
0186             char buf[64];
0187 
0188             pr_info("  0x%012llx-0x%012llx %s\n",
0189                 paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,
0190                 efi_md_typeattr_format(buf, sizeof(buf), md));
0191         }
0192 
0193         memrange_efi_to_native(&paddr, &npages);
0194         size = npages << PAGE_SHIFT;
0195 
0196         if (is_memory(md)) {
0197             early_init_dt_add_memory_arch(paddr, size);
0198 
0199             if (!is_usable_memory(md))
0200                 memblock_mark_nomap(paddr, size);
0201 
0202             /* keep ACPI reclaim memory intact for kexec etc. */
0203             if (md->type == EFI_ACPI_RECLAIM_MEMORY)
0204                 memblock_reserve(paddr, size);
0205         }
0206     }
0207 }
0208 
0209 void __init efi_init(void)
0210 {
0211     struct efi_memory_map_data data;
0212     u64 efi_system_table;
0213 
0214     /* Grab UEFI information placed in FDT by stub */
0215     efi_system_table = efi_get_fdt_params(&data);
0216     if (!efi_system_table)
0217         return;
0218 
0219     if (efi_memmap_init_early(&data) < 0) {
0220         /*
0221         * If we are booting via UEFI, the UEFI memory map is the only
0222         * description of memory we have, so there is little point in
0223         * proceeding if we cannot access it.
0224         */
0225         panic("Unable to map EFI memory map.\n");
0226     }
0227 
0228     WARN(efi.memmap.desc_version != 1,
0229          "Unexpected EFI_MEMORY_DESCRIPTOR version %ld",
0230           efi.memmap.desc_version);
0231 
0232     if (uefi_init(efi_system_table) < 0) {
0233         efi_memmap_unmap();
0234         return;
0235     }
0236 
0237     reserve_regions();
0238     /*
0239      * For memblock manipulation, the cap should come after the memblock_add().
0240      * And now, memblock is fully populated, it is time to do capping.
0241      */
0242     early_init_dt_check_for_usable_mem_range();
0243     efi_find_mirror();
0244     efi_esrt_init();
0245     efi_mokvar_table_init();
0246 
0247     memblock_reserve(data.phys_map & PAGE_MASK,
0248              PAGE_ALIGN(data.size + (data.phys_map & ~PAGE_MASK)));
0249 
0250     init_screen_info();
0251 
0252 #ifdef CONFIG_ARM
0253     /* ARM does not permit early mappings to persist across paging_init() */
0254     efi_memmap_unmap();
0255 
0256     if (cpu_state_table != EFI_INVALID_TABLE_ADDR) {
0257         struct efi_arm_entry_state *state;
0258         bool dump_state = true;
0259 
0260         state = early_memremap_ro(cpu_state_table,
0261                       sizeof(struct efi_arm_entry_state));
0262         if (state == NULL) {
0263             pr_warn("Unable to map CPU entry state table.\n");
0264             return;
0265         }
0266 
0267         if ((state->sctlr_before_ebs & 1) == 0)
0268             pr_warn(FW_BUG "EFI stub was entered with MMU and Dcache disabled, please fix your firmware!\n");
0269         else if ((state->sctlr_after_ebs & 1) == 0)
0270             pr_warn(FW_BUG "ExitBootServices() returned with MMU and Dcache disabled, please fix your firmware!\n");
0271         else
0272             dump_state = false;
0273 
0274         if (dump_state || efi_enabled(EFI_DBG)) {
0275             pr_info("CPSR at EFI stub entry        : 0x%08x\n", state->cpsr_before_ebs);
0276             pr_info("SCTLR at EFI stub entry       : 0x%08x\n", state->sctlr_before_ebs);
0277             pr_info("CPSR after ExitBootServices() : 0x%08x\n", state->cpsr_after_ebs);
0278             pr_info("SCTLR after ExitBootServices(): 0x%08x\n", state->sctlr_after_ebs);
0279         }
0280         early_memunmap(state, sizeof(struct efi_arm_entry_state));
0281     }
0282 #endif
0283 }