1 // SPDX-License-Identifier: GPL-2.0
3 * Common EFI memory map functions.
6 #define pr_fmt(fmt) "efi: " fmt
8 #include <linux/init.h>
9 #include <linux/kernel.h>
10 #include <linux/efi.h>
12 #include <asm/early_ioremap.h>
13 #include <linux/memblock.h>
14 #include <linux/slab.h>
16 static phys_addr_t __init
__efi_memmap_alloc_early(unsigned long size
)
18 return memblock_phys_alloc(size
, SMP_CACHE_BYTES
);
21 static phys_addr_t __init
__efi_memmap_alloc_late(unsigned long size
)
23 unsigned int order
= get_order(size
);
24 struct page
*p
= alloc_pages(GFP_KERNEL
, order
);
29 return PFN_PHYS(page_to_pfn(p
));
32 void __init
__efi_memmap_free(u64 phys
, unsigned long size
, unsigned long flags
)
34 if (flags
& EFI_MEMMAP_MEMBLOCK
) {
35 if (slab_is_available())
36 memblock_free_late(phys
, size
);
38 memblock_free(phys
, size
);
39 } else if (flags
& EFI_MEMMAP_SLAB
) {
40 struct page
*p
= pfn_to_page(PHYS_PFN(phys
));
41 unsigned int order
= get_order(size
);
43 free_pages((unsigned long) page_address(p
), order
);
47 static void __init
efi_memmap_free(void)
49 __efi_memmap_free(efi
.memmap
.phys_map
,
50 efi
.memmap
.desc_size
* efi
.memmap
.nr_map
,
55 * efi_memmap_alloc - Allocate memory for the EFI memory map
56 * @num_entries: Number of entries in the allocated map.
57 * @data: efi memmap installation parameters
59 * Depending on whether mm_init() has already been invoked or not,
60 * either memblock or "normal" page allocation is used.
62 * Returns the physical address of the allocated memory map on
63 * success, zero on failure.
65 int __init
efi_memmap_alloc(unsigned int num_entries
,
66 struct efi_memory_map_data
*data
)
68 /* Expect allocation parameters are zero initialized */
69 WARN_ON(data
->phys_map
|| data
->size
);
71 data
->size
= num_entries
* efi
.memmap
.desc_size
;
72 data
->desc_version
= efi
.memmap
.desc_version
;
73 data
->desc_size
= efi
.memmap
.desc_size
;
74 data
->flags
&= ~(EFI_MEMMAP_SLAB
| EFI_MEMMAP_MEMBLOCK
);
75 data
->flags
|= efi
.memmap
.flags
& EFI_MEMMAP_LATE
;
77 if (slab_is_available()) {
78 data
->flags
|= EFI_MEMMAP_SLAB
;
79 data
->phys_map
= __efi_memmap_alloc_late(data
->size
);
81 data
->flags
|= EFI_MEMMAP_MEMBLOCK
;
82 data
->phys_map
= __efi_memmap_alloc_early(data
->size
);
91 * __efi_memmap_init - Common code for mapping the EFI memory map
92 * @data: EFI memory map data
94 * This function takes care of figuring out which function to use to
95 * map the EFI memory map in efi.memmap based on how far into the boot
98 * During bootup EFI_MEMMAP_LATE in data->flags should be clear since we
99 * only have access to the early_memremap*() functions as the vmalloc
100 * space isn't setup. Once the kernel is fully booted we can fallback
101 * to the more robust memremap*() API.
103 * Returns zero on success, a negative error code on failure.
105 static int __init
__efi_memmap_init(struct efi_memory_map_data
*data
)
107 struct efi_memory_map map
;
108 phys_addr_t phys_map
;
110 if (efi_enabled(EFI_PARAVIRT
))
113 phys_map
= data
->phys_map
;
115 if (data
->flags
& EFI_MEMMAP_LATE
)
116 map
.map
= memremap(phys_map
, data
->size
, MEMREMAP_WB
);
118 map
.map
= early_memremap(phys_map
, data
->size
);
121 pr_err("Could not map the memory map!\n");
125 /* NOP if data->flags & (EFI_MEMMAP_MEMBLOCK | EFI_MEMMAP_SLAB) == 0 */
128 map
.phys_map
= data
->phys_map
;
129 map
.nr_map
= data
->size
/ data
->desc_size
;
130 map
.map_end
= map
.map
+ data
->size
;
132 map
.desc_version
= data
->desc_version
;
133 map
.desc_size
= data
->desc_size
;
134 map
.flags
= data
->flags
;
136 set_bit(EFI_MEMMAP
, &efi
.flags
);
144 * efi_memmap_init_early - Map the EFI memory map data structure
145 * @data: EFI memory map data
147 * Use early_memremap() to map the passed in EFI memory map and assign
150 int __init
efi_memmap_init_early(struct efi_memory_map_data
*data
)
152 /* Cannot go backwards */
153 WARN_ON(efi
.memmap
.flags
& EFI_MEMMAP_LATE
);
156 return __efi_memmap_init(data
);
159 void __init
efi_memmap_unmap(void)
161 if (!efi_enabled(EFI_MEMMAP
))
164 if (!(efi
.memmap
.flags
& EFI_MEMMAP_LATE
)) {
167 size
= efi
.memmap
.desc_size
* efi
.memmap
.nr_map
;
168 early_memunmap(efi
.memmap
.map
, size
);
170 memunmap(efi
.memmap
.map
);
173 efi
.memmap
.map
= NULL
;
174 clear_bit(EFI_MEMMAP
, &efi
.flags
);
178 * efi_memmap_init_late - Map efi.memmap with memremap()
179 * @phys_addr: Physical address of the new EFI memory map
180 * @size: Size in bytes of the new EFI memory map
182 * Setup a mapping of the EFI memory map using ioremap_cache(). This
183 * function should only be called once the vmalloc space has been
184 * setup and is therefore not suitable for calling during early EFI
185 * initialise, e.g. in efi_init(). Additionally, it expects
186 * efi_memmap_init_early() to have already been called.
188 * The reason there are two EFI memmap initialisation
189 * (efi_memmap_init_early() and this late version) is because the
190 * early EFI memmap should be explicitly unmapped once EFI
191 * initialisation is complete as the fixmap space used to map the EFI
192 * memmap (via early_memremap()) is a scarce resource.
194 * This late mapping is intended to persist for the duration of
195 * runtime so that things like efi_mem_desc_lookup() and
196 * efi_mem_attributes() always work.
198 * Returns zero on success, a negative error code on failure.
200 int __init
efi_memmap_init_late(phys_addr_t addr
, unsigned long size
)
202 struct efi_memory_map_data data
= {
205 .flags
= EFI_MEMMAP_LATE
,
208 /* Did we forget to unmap the early EFI memmap? */
209 WARN_ON(efi
.memmap
.map
);
211 /* Were we already called? */
212 WARN_ON(efi
.memmap
.flags
& EFI_MEMMAP_LATE
);
215 * It makes no sense to allow callers to register different
216 * values for the following fields. Copy them out of the
217 * existing early EFI memmap.
219 data
.desc_version
= efi
.memmap
.desc_version
;
220 data
.desc_size
= efi
.memmap
.desc_size
;
222 return __efi_memmap_init(&data
);
226 * efi_memmap_install - Install a new EFI memory map in efi.memmap
227 * @ctx: map allocation parameters (address, size, flags)
229 * Unlike efi_memmap_init_*(), this function does not allow the caller
230 * to switch from early to late mappings. It simply uses the existing
231 * mapping function and installs the new memmap.
233 * Returns zero on success, a negative error code on failure.
235 int __init
efi_memmap_install(struct efi_memory_map_data
*data
)
239 return __efi_memmap_init(data
);
243 * efi_memmap_split_count - Count number of additional EFI memmap entries
244 * @md: EFI memory descriptor to split
245 * @range: Address range (start, end) to split around
247 * Returns the number of additional EFI memmap entries required to
250 int __init
efi_memmap_split_count(efi_memory_desc_t
*md
, struct range
*range
)
256 start
= md
->phys_addr
;
257 end
= start
+ (md
->num_pages
<< EFI_PAGE_SHIFT
) - 1;
259 /* modifying range */
260 m_start
= range
->start
;
263 if (m_start
<= start
) {
264 /* split into 2 parts */
265 if (start
< m_end
&& m_end
< end
)
269 if (start
< m_start
&& m_start
< end
) {
270 /* split into 3 parts */
273 /* split into 2 parts */
282 * efi_memmap_insert - Insert a memory region in an EFI memmap
283 * @old_memmap: The existing EFI memory map structure
284 * @buf: Address of buffer to store new map
285 * @mem: Memory map entry to insert
287 * It is suggested that you call efi_memmap_split_count() first
288 * to see how large @buf needs to be.
290 void __init
efi_memmap_insert(struct efi_memory_map
*old_memmap
, void *buf
,
291 struct efi_mem_range
*mem
)
293 u64 m_start
, m_end
, m_attr
;
294 efi_memory_desc_t
*md
;
298 /* modifying range */
299 m_start
= mem
->range
.start
;
300 m_end
= mem
->range
.end
;
301 m_attr
= mem
->attribute
;
304 * The EFI memory map deals with regions in EFI_PAGE_SIZE
305 * units. Ensure that the region described by 'mem' is aligned
308 if (!IS_ALIGNED(m_start
, EFI_PAGE_SIZE
) ||
309 !IS_ALIGNED(m_end
+ 1, EFI_PAGE_SIZE
)) {
314 for (old
= old_memmap
->map
, new = buf
;
315 old
< old_memmap
->map_end
;
316 old
+= old_memmap
->desc_size
, new += old_memmap
->desc_size
) {
318 /* copy original EFI memory descriptor */
319 memcpy(new, old
, old_memmap
->desc_size
);
321 start
= md
->phys_addr
;
322 end
= md
->phys_addr
+ (md
->num_pages
<< EFI_PAGE_SHIFT
) - 1;
324 if (m_start
<= start
&& end
<= m_end
)
325 md
->attribute
|= m_attr
;
327 if (m_start
<= start
&&
328 (start
< m_end
&& m_end
< end
)) {
330 md
->attribute
|= m_attr
;
331 md
->num_pages
= (m_end
- md
->phys_addr
+ 1) >>
334 new += old_memmap
->desc_size
;
335 memcpy(new, old
, old_memmap
->desc_size
);
337 md
->phys_addr
= m_end
+ 1;
338 md
->num_pages
= (end
- md
->phys_addr
+ 1) >>
342 if ((start
< m_start
&& m_start
< end
) && m_end
< end
) {
344 md
->num_pages
= (m_start
- md
->phys_addr
) >>
347 new += old_memmap
->desc_size
;
348 memcpy(new, old
, old_memmap
->desc_size
);
350 md
->attribute
|= m_attr
;
351 md
->phys_addr
= m_start
;
352 md
->num_pages
= (m_end
- m_start
+ 1) >>
355 new += old_memmap
->desc_size
;
356 memcpy(new, old
, old_memmap
->desc_size
);
358 md
->phys_addr
= m_end
+ 1;
359 md
->num_pages
= (end
- m_end
) >>
363 if ((start
< m_start
&& m_start
< end
) &&
366 md
->num_pages
= (m_start
- md
->phys_addr
) >>
369 new += old_memmap
->desc_size
;
370 memcpy(new, old
, old_memmap
->desc_size
);
372 md
->phys_addr
= m_start
;
373 md
->num_pages
= (end
- md
->phys_addr
+ 1) >>
375 md
->attribute
|= m_attr
;