2 * Common EFI memory map functions.
5 #define pr_fmt(fmt) "efi: " fmt
7 #include <linux/init.h>
8 #include <linux/kernel.h>
11 #include <asm/early_ioremap.h>
12 #include <linux/memblock.h>
13 #include <linux/slab.h>
15 static phys_addr_t __init
__efi_memmap_alloc_early(unsigned long size
)
17 return memblock_alloc(size
, 0);
20 static phys_addr_t __init
__efi_memmap_alloc_late(unsigned long size
)
22 unsigned int order
= get_order(size
);
23 struct page
*p
= alloc_pages(GFP_KERNEL
, order
);
28 return PFN_PHYS(page_to_pfn(p
));
32 * efi_memmap_alloc - Allocate memory for the EFI memory map
33 * @num_entries: Number of entries in the allocated map.
35 * Depending on whether mm_init() has already been invoked or not,
36 * either memblock or "normal" page allocation is used.
38 * Returns the physical address of the allocated memory map on
39 * success, zero on failure.
41 phys_addr_t __init
efi_memmap_alloc(unsigned int num_entries
)
43 unsigned long size
= num_entries
* efi
.memmap
.desc_size
;
45 if (slab_is_available())
46 return __efi_memmap_alloc_late(size
);
48 return __efi_memmap_alloc_early(size
);
52 * __efi_memmap_init - Common code for mapping the EFI memory map
53 * @data: EFI memory map data
54 * @late: Use early or late mapping function?
56 * This function takes care of figuring out which function to use to
57 * map the EFI memory map in efi.memmap based on how far into the boot
60 * During bootup @late should be %false since we only have access to
61 * the early_memremap*() functions as the vmalloc space isn't setup.
62 * Once the kernel is fully booted we can fallback to the more robust
65 * Returns zero on success, a negative error code on failure.
68 __efi_memmap_init(struct efi_memory_map_data
*data
, bool late
)
70 struct efi_memory_map map
;
73 if (efi_enabled(EFI_PARAVIRT
))
76 phys_map
= data
->phys_map
;
79 map
.map
= memremap(phys_map
, data
->size
, MEMREMAP_WB
);
81 map
.map
= early_memremap(phys_map
, data
->size
);
84 pr_err("Could not map the memory map!\n");
88 map
.phys_map
= data
->phys_map
;
89 map
.nr_map
= data
->size
/ data
->desc_size
;
90 map
.map_end
= map
.map
+ data
->size
;
92 map
.desc_version
= data
->desc_version
;
93 map
.desc_size
= data
->desc_size
;
96 set_bit(EFI_MEMMAP
, &efi
.flags
);
104 * efi_memmap_init_early - Map the EFI memory map data structure
105 * @data: EFI memory map data
107 * Use early_memremap() to map the passed in EFI memory map and assign
110 int __init
efi_memmap_init_early(struct efi_memory_map_data
*data
)
112 /* Cannot go backwards */
113 WARN_ON(efi
.memmap
.late
);
115 return __efi_memmap_init(data
, false);
118 void __init
efi_memmap_unmap(void)
120 if (!efi
.memmap
.late
) {
123 size
= efi
.memmap
.desc_size
* efi
.memmap
.nr_map
;
124 early_memunmap(efi
.memmap
.map
, size
);
126 memunmap(efi
.memmap
.map
);
129 efi
.memmap
.map
= NULL
;
130 clear_bit(EFI_MEMMAP
, &efi
.flags
);
134 * efi_memmap_init_late - Map efi.memmap with memremap()
135 * @phys_addr: Physical address of the new EFI memory map
136 * @size: Size in bytes of the new EFI memory map
138 * Setup a mapping of the EFI memory map using ioremap_cache(). This
139 * function should only be called once the vmalloc space has been
140 * setup and is therefore not suitable for calling during early EFI
141 * initialise, e.g. in efi_init(). Additionally, it expects
142 * efi_memmap_init_early() to have already been called.
144 * The reason there are two EFI memmap initialisation
145 * (efi_memmap_init_early() and this late version) is because the
146 * early EFI memmap should be explicitly unmapped once EFI
147 * initialisation is complete as the fixmap space used to map the EFI
148 * memmap (via early_memremap()) is a scarce resource.
150 * This late mapping is intended to persist for the duration of
151 * runtime so that things like efi_mem_desc_lookup() and
152 * efi_mem_attributes() always work.
154 * Returns zero on success, a negative error code on failure.
156 int __init
efi_memmap_init_late(phys_addr_t addr
, unsigned long size
)
158 struct efi_memory_map_data data
= {
163 /* Did we forget to unmap the early EFI memmap? */
164 WARN_ON(efi
.memmap
.map
);
166 /* Were we already called? */
167 WARN_ON(efi
.memmap
.late
);
170 * It makes no sense to allow callers to register different
171 * values for the following fields. Copy them out of the
172 * existing early EFI memmap.
174 data
.desc_version
= efi
.memmap
.desc_version
;
175 data
.desc_size
= efi
.memmap
.desc_size
;
177 return __efi_memmap_init(&data
, true);
181 * efi_memmap_install - Install a new EFI memory map in efi.memmap
182 * @addr: Physical address of the memory map
183 * @nr_map: Number of entries in the memory map
185 * Unlike efi_memmap_init_*(), this function does not allow the caller
186 * to switch from early to late mappings. It simply uses the existing
187 * mapping function and installs the new memmap.
189 * Returns zero on success, a negative error code on failure.
191 int __init
efi_memmap_install(phys_addr_t addr
, unsigned int nr_map
)
193 struct efi_memory_map_data data
;
197 data
.phys_map
= addr
;
198 data
.size
= efi
.memmap
.desc_size
* nr_map
;
199 data
.desc_version
= efi
.memmap
.desc_version
;
200 data
.desc_size
= efi
.memmap
.desc_size
;
202 return __efi_memmap_init(&data
, efi
.memmap
.late
);
206 * efi_memmap_split_count - Count number of additional EFI memmap entries
207 * @md: EFI memory descriptor to split
208 * @range: Address range (start, end) to split around
210 * Returns the number of additional EFI memmap entries required to
213 int __init
efi_memmap_split_count(efi_memory_desc_t
*md
, struct range
*range
)
219 start
= md
->phys_addr
;
220 end
= start
+ (md
->num_pages
<< EFI_PAGE_SHIFT
) - 1;
222 /* modifying range */
223 m_start
= range
->start
;
226 if (m_start
<= start
) {
227 /* split into 2 parts */
228 if (start
< m_end
&& m_end
< end
)
232 if (start
< m_start
&& m_start
< end
) {
233 /* split into 3 parts */
236 /* split into 2 parts */
245 * efi_memmap_insert - Insert a memory region in an EFI memmap
246 * @old_memmap: The existing EFI memory map structure
247 * @buf: Address of buffer to store new map
248 * @mem: Memory map entry to insert
250 * It is suggested that you call efi_memmap_split_count() first
251 * to see how large @buf needs to be.
253 void __init
efi_memmap_insert(struct efi_memory_map
*old_memmap
, void *buf
,
254 struct efi_mem_range
*mem
)
256 u64 m_start
, m_end
, m_attr
;
257 efi_memory_desc_t
*md
;
261 /* modifying range */
262 m_start
= mem
->range
.start
;
263 m_end
= mem
->range
.end
;
264 m_attr
= mem
->attribute
;
267 * The EFI memory map deals with regions in EFI_PAGE_SIZE
268 * units. Ensure that the region described by 'mem' is aligned
271 if (!IS_ALIGNED(m_start
, EFI_PAGE_SIZE
) ||
272 !IS_ALIGNED(m_end
+ 1, EFI_PAGE_SIZE
)) {
277 for (old
= old_memmap
->map
, new = buf
;
278 old
< old_memmap
->map_end
;
279 old
+= old_memmap
->desc_size
, new += old_memmap
->desc_size
) {
281 /* copy original EFI memory descriptor */
282 memcpy(new, old
, old_memmap
->desc_size
);
284 start
= md
->phys_addr
;
285 end
= md
->phys_addr
+ (md
->num_pages
<< EFI_PAGE_SHIFT
) - 1;
287 if (m_start
<= start
&& end
<= m_end
)
288 md
->attribute
|= m_attr
;
290 if (m_start
<= start
&&
291 (start
< m_end
&& m_end
< end
)) {
293 md
->attribute
|= m_attr
;
294 md
->num_pages
= (m_end
- md
->phys_addr
+ 1) >>
297 new += old_memmap
->desc_size
;
298 memcpy(new, old
, old_memmap
->desc_size
);
300 md
->phys_addr
= m_end
+ 1;
301 md
->num_pages
= (end
- md
->phys_addr
+ 1) >>
305 if ((start
< m_start
&& m_start
< end
) && m_end
< end
) {
307 md
->num_pages
= (m_start
- md
->phys_addr
) >>
310 new += old_memmap
->desc_size
;
311 memcpy(new, old
, old_memmap
->desc_size
);
313 md
->attribute
|= m_attr
;
314 md
->phys_addr
= m_start
;
315 md
->num_pages
= (m_end
- m_start
+ 1) >>
318 new += old_memmap
->desc_size
;
319 memcpy(new, old
, old_memmap
->desc_size
);
321 md
->phys_addr
= m_end
+ 1;
322 md
->num_pages
= (end
- m_end
) >>
326 if ((start
< m_start
&& m_start
< end
) &&
329 md
->num_pages
= (m_start
- md
->phys_addr
) >>
332 new += old_memmap
->desc_size
;
333 memcpy(new, old
, old_memmap
->desc_size
);
335 md
->phys_addr
= m_start
;
336 md
->num_pages
= (end
- md
->phys_addr
+ 1) >>
338 md
->attribute
|= m_attr
;