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>
14 * __efi_memmap_init - Common code for mapping the EFI memory map
15 * @data: EFI memory map data
16 * @late: Use early or late mapping function?
18 * This function takes care of figuring out which function to use to
19 * map the EFI memory map in efi.memmap based on how far into the boot
22 * During bootup @late should be %false since we only have access to
23 * the early_memremap*() functions as the vmalloc space isn't setup.
24 * Once the kernel is fully booted we can fallback to the more robust
27 * Returns zero on success, a negative error code on failure.
30 __efi_memmap_init(struct efi_memory_map_data
*data
, bool late
)
32 struct efi_memory_map map
;
35 if (efi_enabled(EFI_PARAVIRT
))
38 phys_map
= data
->phys_map
;
41 map
.map
= memremap(phys_map
, data
->size
, MEMREMAP_WB
);
43 map
.map
= early_memremap(phys_map
, data
->size
);
46 pr_err("Could not map the memory map!\n");
50 map
.phys_map
= data
->phys_map
;
51 map
.nr_map
= data
->size
/ data
->desc_size
;
52 map
.map_end
= map
.map
+ data
->size
;
54 map
.desc_version
= data
->desc_version
;
55 map
.desc_size
= data
->desc_size
;
58 set_bit(EFI_MEMMAP
, &efi
.flags
);
66 * efi_memmap_init_early - Map the EFI memory map data structure
67 * @data: EFI memory map data
69 * Use early_memremap() to map the passed in EFI memory map and assign
72 int __init
efi_memmap_init_early(struct efi_memory_map_data
*data
)
74 /* Cannot go backwards */
75 WARN_ON(efi
.memmap
.late
);
77 return __efi_memmap_init(data
, false);
80 void __init
efi_memmap_unmap(void)
82 if (!efi
.memmap
.late
) {
85 size
= efi
.memmap
.desc_size
* efi
.memmap
.nr_map
;
86 early_memunmap(efi
.memmap
.map
, size
);
88 memunmap(efi
.memmap
.map
);
91 efi
.memmap
.map
= NULL
;
92 clear_bit(EFI_MEMMAP
, &efi
.flags
);
96 * efi_memmap_init_late - Map efi.memmap with memremap()
97 * @phys_addr: Physical address of the new EFI memory map
98 * @size: Size in bytes of the new EFI memory map
100 * Setup a mapping of the EFI memory map using ioremap_cache(). This
101 * function should only be called once the vmalloc space has been
102 * setup and is therefore not suitable for calling during early EFI
103 * initialise, e.g. in efi_init(). Additionally, it expects
104 * efi_memmap_init_early() to have already been called.
106 * The reason there are two EFI memmap initialisation
107 * (efi_memmap_init_early() and this late version) is because the
108 * early EFI memmap should be explicitly unmapped once EFI
109 * initialisation is complete as the fixmap space used to map the EFI
110 * memmap (via early_memremap()) is a scarce resource.
112 * This late mapping is intended to persist for the duration of
113 * runtime so that things like efi_mem_desc_lookup() and
114 * efi_mem_attributes() always work.
116 * Returns zero on success, a negative error code on failure.
118 int __init
efi_memmap_init_late(phys_addr_t addr
, unsigned long size
)
120 struct efi_memory_map_data data
= {
125 /* Did we forget to unmap the early EFI memmap? */
126 WARN_ON(efi
.memmap
.map
);
128 /* Were we already called? */
129 WARN_ON(efi
.memmap
.late
);
132 * It makes no sense to allow callers to register different
133 * values for the following fields. Copy them out of the
134 * existing early EFI memmap.
136 data
.desc_version
= efi
.memmap
.desc_version
;
137 data
.desc_size
= efi
.memmap
.desc_size
;
139 return __efi_memmap_init(&data
, true);
143 * efi_memmap_install - Install a new EFI memory map in efi.memmap
144 * @addr: Physical address of the memory map
145 * @nr_map: Number of entries in the memory map
147 * Unlike efi_memmap_init_*(), this function does not allow the caller
148 * to switch from early to late mappings. It simply uses the existing
149 * mapping function and installs the new memmap.
151 * Returns zero on success, a negative error code on failure.
153 int __init
efi_memmap_install(phys_addr_t addr
, unsigned int nr_map
)
155 struct efi_memory_map_data data
;
159 data
.phys_map
= addr
;
160 data
.size
= efi
.memmap
.desc_size
* nr_map
;
161 data
.desc_version
= efi
.memmap
.desc_version
;
162 data
.desc_size
= efi
.memmap
.desc_size
;
164 return __efi_memmap_init(&data
, efi
.memmap
.late
);
168 * efi_memmap_split_count - Count number of additional EFI memmap entries
169 * @md: EFI memory descriptor to split
170 * @range: Address range (start, end) to split around
172 * Returns the number of additional EFI memmap entries required to
175 int __init
efi_memmap_split_count(efi_memory_desc_t
*md
, struct range
*range
)
181 start
= md
->phys_addr
;
182 end
= start
+ (md
->num_pages
<< EFI_PAGE_SHIFT
) - 1;
184 /* modifying range */
185 m_start
= range
->start
;
188 if (m_start
<= start
) {
189 /* split into 2 parts */
190 if (start
< m_end
&& m_end
< end
)
194 if (start
< m_start
&& m_start
< end
) {
195 /* split into 3 parts */
198 /* split into 2 parts */
207 * efi_memmap_insert - Insert a memory region in an EFI memmap
208 * @old_memmap: The existing EFI memory map structure
209 * @buf: Address of buffer to store new map
210 * @mem: Memory map entry to insert
212 * It is suggested that you call efi_memmap_split_count() first
213 * to see how large @buf needs to be.
215 void __init
efi_memmap_insert(struct efi_memory_map
*old_memmap
, void *buf
,
216 struct efi_mem_range
*mem
)
218 u64 m_start
, m_end
, m_attr
;
219 efi_memory_desc_t
*md
;
223 /* modifying range */
224 m_start
= mem
->range
.start
;
225 m_end
= mem
->range
.end
;
226 m_attr
= mem
->attribute
;
229 * The EFI memory map deals with regions in EFI_PAGE_SIZE
230 * units. Ensure that the region described by 'mem' is aligned
233 if (!IS_ALIGNED(m_start
, EFI_PAGE_SIZE
) ||
234 !IS_ALIGNED(m_end
+ 1, EFI_PAGE_SIZE
)) {
239 for (old
= old_memmap
->map
, new = buf
;
240 old
< old_memmap
->map_end
;
241 old
+= old_memmap
->desc_size
, new += old_memmap
->desc_size
) {
243 /* copy original EFI memory descriptor */
244 memcpy(new, old
, old_memmap
->desc_size
);
246 start
= md
->phys_addr
;
247 end
= md
->phys_addr
+ (md
->num_pages
<< EFI_PAGE_SHIFT
) - 1;
249 if (m_start
<= start
&& end
<= m_end
)
250 md
->attribute
|= m_attr
;
252 if (m_start
<= start
&&
253 (start
< m_end
&& m_end
< end
)) {
255 md
->attribute
|= m_attr
;
256 md
->num_pages
= (m_end
- md
->phys_addr
+ 1) >>
259 new += old_memmap
->desc_size
;
260 memcpy(new, old
, old_memmap
->desc_size
);
262 md
->phys_addr
= m_end
+ 1;
263 md
->num_pages
= (end
- md
->phys_addr
+ 1) >>
267 if ((start
< m_start
&& m_start
< end
) && m_end
< end
) {
269 md
->num_pages
= (m_start
- md
->phys_addr
) >>
272 new += old_memmap
->desc_size
;
273 memcpy(new, old
, old_memmap
->desc_size
);
275 md
->attribute
|= m_attr
;
276 md
->phys_addr
= m_start
;
277 md
->num_pages
= (m_end
- m_start
+ 1) >>
280 new += old_memmap
->desc_size
;
281 memcpy(new, old
, old_memmap
->desc_size
);
283 md
->phys_addr
= m_end
+ 1;
284 md
->num_pages
= (end
- m_end
) >>
288 if ((start
< m_start
&& m_start
< end
) &&
291 md
->num_pages
= (m_start
- md
->phys_addr
) >>
294 new += old_memmap
->desc_size
;
295 memcpy(new, old
, old_memmap
->desc_size
);
297 md
->phys_addr
= m_start
;
298 md
->num_pages
= (end
- md
->phys_addr
+ 1) >>
300 md
->attribute
|= m_attr
;