// SPDX-License-Identifier: GPL-2.01/*2* Common EFI memory map functions.3*/45#define pr_fmt(fmt) "efi: " fmt67#include <linux/init.h>8#include <linux/kernel.h>9#include <linux/efi.h>10#include <linux/io.h>11#include <linux/memblock.h>12#include <linux/slab.h>1314#include <asm/early_ioremap.h>15#include <asm/efi.h>1617/**18* __efi_memmap_init - Common code for mapping the EFI memory map19* @data: EFI memory map data20*21* This function takes care of figuring out which function to use to22* map the EFI memory map in efi.memmap based on how far into the boot23* we are.24*25* During bootup EFI_MEMMAP_LATE in data->flags should be clear since we26* only have access to the early_memremap*() functions as the vmalloc27* space isn't setup. Once the kernel is fully booted we can fallback28* to the more robust memremap*() API.29*30* Returns: zero on success, a negative error code on failure.31*/32int __init __efi_memmap_init(struct efi_memory_map_data *data)33{34struct efi_memory_map map;35phys_addr_t phys_map;3637phys_map = data->phys_map;3839if (data->flags & EFI_MEMMAP_LATE)40map.map = memremap(phys_map, data->size, MEMREMAP_WB);41else42map.map = early_memremap(phys_map, data->size);4344if (!map.map) {45pr_err("Could not map the memory map! phys_map=%pa, size=0x%lx\n",46&phys_map, data->size);47return -ENOMEM;48}4950map.phys_map = data->phys_map;51map.nr_map = data->size / data->desc_size;52map.map_end = map.map + data->size;5354map.desc_version = data->desc_version;55map.desc_size = data->desc_size;56map.flags = data->flags;5758set_bit(EFI_MEMMAP, &efi.flags);5960efi.memmap = map;6162return 0;63}6465/**66* efi_memmap_init_early - Map the EFI memory map data structure67* @data: EFI memory map data68*69* Use early_memremap() to map the passed in EFI memory map and assign70* it to efi.memmap.71*72* Returns: zero on success, a negative error code on failure.73*/74int __init efi_memmap_init_early(struct efi_memory_map_data *data)75{76/* Cannot go backwards */77WARN_ON(efi.memmap.flags & EFI_MEMMAP_LATE);7879data->flags = 0;80return __efi_memmap_init(data);81}8283void __init efi_memmap_unmap(void)84{85if (!efi_enabled(EFI_MEMMAP))86return;8788if (!(efi.memmap.flags & EFI_MEMMAP_LATE)) {89unsigned long size;9091size = efi.memmap.desc_size * efi.memmap.nr_map;92early_memunmap(efi.memmap.map, size);93} else {94memunmap(efi.memmap.map);95}9697efi.memmap.map = NULL;98clear_bit(EFI_MEMMAP, &efi.flags);99}100101/**102* efi_memmap_init_late - Map efi.memmap with memremap()103* @addr: Physical address of the new EFI memory map104* @size: Size in bytes of the new EFI memory map105*106* Setup a mapping of the EFI memory map using ioremap_cache(). This107* function should only be called once the vmalloc space has been108* setup and is therefore not suitable for calling during early EFI109* initialise, e.g. in efi_init(). Additionally, it expects110* efi_memmap_init_early() to have already been called.111*112* The reason there are two EFI memmap initialisation113* (efi_memmap_init_early() and this late version) is because the114* early EFI memmap should be explicitly unmapped once EFI115* initialisation is complete as the fixmap space used to map the EFI116* memmap (via early_memremap()) is a scarce resource.117*118* This late mapping is intended to persist for the duration of119* runtime so that things like efi_mem_desc_lookup() and120* efi_mem_attributes() always work.121*122* Returns: zero on success, a negative error code on failure.123*/124int __init efi_memmap_init_late(phys_addr_t addr, unsigned long size)125{126struct efi_memory_map_data data = {127.phys_map = addr,128.size = size,129.flags = EFI_MEMMAP_LATE,130};131132/* Did we forget to unmap the early EFI memmap? */133WARN_ON(efi.memmap.map);134135/* Were we already called? */136WARN_ON(efi.memmap.flags & EFI_MEMMAP_LATE);137138/*139* It makes no sense to allow callers to register different140* values for the following fields. Copy them out of the141* existing early EFI memmap.142*/143data.desc_version = efi.memmap.desc_version;144data.desc_size = efi.memmap.desc_size;145146return __efi_memmap_init(&data);147}148149150