xref: /linux/drivers/firmware/efi/memmap.c (revision 4afa688d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Common EFI memory map functions.
4  */
5 
6 #define pr_fmt(fmt) "efi: " fmt
7 
8 #include <linux/init.h>
9 #include <linux/kernel.h>
10 #include <linux/efi.h>
11 #include <linux/io.h>
12 #include <linux/memblock.h>
13 #include <linux/slab.h>
14 
15 #include <asm/early_ioremap.h>
16 #include <asm/efi.h>
17 
18 #ifndef __efi_memmap_free
19 #define __efi_memmap_free(phys, size, flags) do { } while (0)
20 #endif
21 
22 /**
23  * __efi_memmap_init - Common code for mapping the EFI memory map
24  * @data: EFI memory map data
25  *
26  * This function takes care of figuring out which function to use to
27  * map the EFI memory map in efi.memmap based on how far into the boot
28  * we are.
29  *
30  * During bootup EFI_MEMMAP_LATE in data->flags should be clear since we
31  * only have access to the early_memremap*() functions as the vmalloc
32  * space isn't setup.  Once the kernel is fully booted we can fallback
33  * to the more robust memremap*() API.
34  *
35  * Returns: zero on success, a negative error code on failure.
36  */
__efi_memmap_init(struct efi_memory_map_data * data)37 int __init __efi_memmap_init(struct efi_memory_map_data *data)
38 {
39 	struct efi_memory_map map;
40 	phys_addr_t phys_map;
41 
42 	phys_map = data->phys_map;
43 
44 	if (data->flags & EFI_MEMMAP_LATE)
45 		map.map = memremap(phys_map, data->size, MEMREMAP_WB);
46 	else
47 		map.map = early_memremap(phys_map, data->size);
48 
49 	if (!map.map) {
50 		pr_err("Could not map the memory map!\n");
51 		return -ENOMEM;
52 	}
53 
54 	if (efi.memmap.flags & (EFI_MEMMAP_MEMBLOCK | EFI_MEMMAP_SLAB))
55 		__efi_memmap_free(efi.memmap.phys_map,
56 				  efi.memmap.desc_size * efi.memmap.nr_map,
57 				  efi.memmap.flags);
58 
59 	map.phys_map = data->phys_map;
60 	map.nr_map = data->size / data->desc_size;
61 	map.map_end = map.map + data->size;
62 
63 	map.desc_version = data->desc_version;
64 	map.desc_size = data->desc_size;
65 	map.flags = data->flags;
66 
67 	set_bit(EFI_MEMMAP, &efi.flags);
68 
69 	efi.memmap = map;
70 
71 	return 0;
72 }
73 
74 /**
75  * efi_memmap_init_early - Map the EFI memory map data structure
76  * @data: EFI memory map data
77  *
78  * Use early_memremap() to map the passed in EFI memory map and assign
79  * it to efi.memmap.
80  *
81  * Returns: zero on success, a negative error code on failure.
82  */
efi_memmap_init_early(struct efi_memory_map_data * data)83 int __init efi_memmap_init_early(struct efi_memory_map_data *data)
84 {
85 	/* Cannot go backwards */
86 	WARN_ON(efi.memmap.flags & EFI_MEMMAP_LATE);
87 
88 	data->flags = 0;
89 	return __efi_memmap_init(data);
90 }
91 
efi_memmap_unmap(void)92 void __init efi_memmap_unmap(void)
93 {
94 	if (!efi_enabled(EFI_MEMMAP))
95 		return;
96 
97 	if (!(efi.memmap.flags & EFI_MEMMAP_LATE)) {
98 		unsigned long size;
99 
100 		size = efi.memmap.desc_size * efi.memmap.nr_map;
101 		early_memunmap(efi.memmap.map, size);
102 	} else {
103 		memunmap(efi.memmap.map);
104 	}
105 
106 	efi.memmap.map = NULL;
107 	clear_bit(EFI_MEMMAP, &efi.flags);
108 }
109 
110 /**
111  * efi_memmap_init_late - Map efi.memmap with memremap()
112  * @addr: Physical address of the new EFI memory map
113  * @size: Size in bytes of the new EFI memory map
114  *
115  * Setup a mapping of the EFI memory map using ioremap_cache(). This
116  * function should only be called once the vmalloc space has been
117  * setup and is therefore not suitable for calling during early EFI
118  * initialise, e.g. in efi_init(). Additionally, it expects
119  * efi_memmap_init_early() to have already been called.
120  *
121  * The reason there are two EFI memmap initialisation
122  * (efi_memmap_init_early() and this late version) is because the
123  * early EFI memmap should be explicitly unmapped once EFI
124  * initialisation is complete as the fixmap space used to map the EFI
125  * memmap (via early_memremap()) is a scarce resource.
126  *
127  * This late mapping is intended to persist for the duration of
128  * runtime so that things like efi_mem_desc_lookup() and
129  * efi_mem_attributes() always work.
130  *
131  * Returns: zero on success, a negative error code on failure.
132  */
efi_memmap_init_late(phys_addr_t addr,unsigned long size)133 int __init efi_memmap_init_late(phys_addr_t addr, unsigned long size)
134 {
135 	struct efi_memory_map_data data = {
136 		.phys_map = addr,
137 		.size = size,
138 		.flags = EFI_MEMMAP_LATE,
139 	};
140 
141 	/* Did we forget to unmap the early EFI memmap? */
142 	WARN_ON(efi.memmap.map);
143 
144 	/* Were we already called? */
145 	WARN_ON(efi.memmap.flags & EFI_MEMMAP_LATE);
146 
147 	/*
148 	 * It makes no sense to allow callers to register different
149 	 * values for the following fields. Copy them out of the
150 	 * existing early EFI memmap.
151 	 */
152 	data.desc_version = efi.memmap.desc_version;
153 	data.desc_size = efi.memmap.desc_size;
154 
155 	return __efi_memmap_init(&data);
156 }
157