Linux 4.18.10
[linux/fpc-iii.git] / drivers / firmware / efi / memmap.c
blob5fc70520e04c4b57cd047c9dc6c0f5f7fab590f3
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Common EFI memory map functions.
4 */
6 #define pr_fmt(fmt) "efi: " fmt
8 #include <linux/init.h>
9 #include <linux/kernel.h>
10 #include <linux/efi.h>
11 #include <linux/io.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_alloc(size, 0);
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);
26 if (!p)
27 return 0;
29 return PFN_PHYS(page_to_pfn(p));
32 /**
33 * efi_memmap_alloc - Allocate memory for the EFI memory map
34 * @num_entries: Number of entries in the allocated map.
36 * Depending on whether mm_init() has already been invoked or not,
37 * either memblock or "normal" page allocation is used.
39 * Returns the physical address of the allocated memory map on
40 * success, zero on failure.
42 phys_addr_t __init efi_memmap_alloc(unsigned int num_entries)
44 unsigned long size = num_entries * efi.memmap.desc_size;
46 if (slab_is_available())
47 return __efi_memmap_alloc_late(size);
49 return __efi_memmap_alloc_early(size);
52 /**
53 * __efi_memmap_init - Common code for mapping the EFI memory map
54 * @data: EFI memory map data
55 * @late: Use early or late mapping function?
57 * This function takes care of figuring out which function to use to
58 * map the EFI memory map in efi.memmap based on how far into the boot
59 * we are.
61 * During bootup @late should be %false since we only have access to
62 * the early_memremap*() functions as the vmalloc space isn't setup.
63 * Once the kernel is fully booted we can fallback to the more robust
64 * memremap*() API.
66 * Returns zero on success, a negative error code on failure.
68 static int __init
69 __efi_memmap_init(struct efi_memory_map_data *data, bool late)
71 struct efi_memory_map map;
72 phys_addr_t phys_map;
74 if (efi_enabled(EFI_PARAVIRT))
75 return 0;
77 phys_map = data->phys_map;
79 if (late)
80 map.map = memremap(phys_map, data->size, MEMREMAP_WB);
81 else
82 map.map = early_memremap(phys_map, data->size);
84 if (!map.map) {
85 pr_err("Could not map the memory map!\n");
86 return -ENOMEM;
89 map.phys_map = data->phys_map;
90 map.nr_map = data->size / data->desc_size;
91 map.map_end = map.map + data->size;
93 map.desc_version = data->desc_version;
94 map.desc_size = data->desc_size;
95 map.late = late;
97 set_bit(EFI_MEMMAP, &efi.flags);
99 efi.memmap = map;
101 return 0;
105 * efi_memmap_init_early - Map the EFI memory map data structure
106 * @data: EFI memory map data
108 * Use early_memremap() to map the passed in EFI memory map and assign
109 * it to efi.memmap.
111 int __init efi_memmap_init_early(struct efi_memory_map_data *data)
113 /* Cannot go backwards */
114 WARN_ON(efi.memmap.late);
116 return __efi_memmap_init(data, false);
119 void __init efi_memmap_unmap(void)
121 if (!efi.memmap.late) {
122 unsigned long size;
124 size = efi.memmap.desc_size * efi.memmap.nr_map;
125 early_memunmap(efi.memmap.map, size);
126 } else {
127 memunmap(efi.memmap.map);
130 efi.memmap.map = NULL;
131 clear_bit(EFI_MEMMAP, &efi.flags);
135 * efi_memmap_init_late - Map efi.memmap with memremap()
136 * @phys_addr: Physical address of the new EFI memory map
137 * @size: Size in bytes of the new EFI memory map
139 * Setup a mapping of the EFI memory map using ioremap_cache(). This
140 * function should only be called once the vmalloc space has been
141 * setup and is therefore not suitable for calling during early EFI
142 * initialise, e.g. in efi_init(). Additionally, it expects
143 * efi_memmap_init_early() to have already been called.
145 * The reason there are two EFI memmap initialisation
146 * (efi_memmap_init_early() and this late version) is because the
147 * early EFI memmap should be explicitly unmapped once EFI
148 * initialisation is complete as the fixmap space used to map the EFI
149 * memmap (via early_memremap()) is a scarce resource.
151 * This late mapping is intended to persist for the duration of
152 * runtime so that things like efi_mem_desc_lookup() and
153 * efi_mem_attributes() always work.
155 * Returns zero on success, a negative error code on failure.
157 int __init efi_memmap_init_late(phys_addr_t addr, unsigned long size)
159 struct efi_memory_map_data data = {
160 .phys_map = addr,
161 .size = size,
164 /* Did we forget to unmap the early EFI memmap? */
165 WARN_ON(efi.memmap.map);
167 /* Were we already called? */
168 WARN_ON(efi.memmap.late);
171 * It makes no sense to allow callers to register different
172 * values for the following fields. Copy them out of the
173 * existing early EFI memmap.
175 data.desc_version = efi.memmap.desc_version;
176 data.desc_size = efi.memmap.desc_size;
178 return __efi_memmap_init(&data, true);
182 * efi_memmap_install - Install a new EFI memory map in efi.memmap
183 * @addr: Physical address of the memory map
184 * @nr_map: Number of entries in the memory map
186 * Unlike efi_memmap_init_*(), this function does not allow the caller
187 * to switch from early to late mappings. It simply uses the existing
188 * mapping function and installs the new memmap.
190 * Returns zero on success, a negative error code on failure.
192 int __init efi_memmap_install(phys_addr_t addr, unsigned int nr_map)
194 struct efi_memory_map_data data;
196 efi_memmap_unmap();
198 data.phys_map = addr;
199 data.size = efi.memmap.desc_size * nr_map;
200 data.desc_version = efi.memmap.desc_version;
201 data.desc_size = efi.memmap.desc_size;
203 return __efi_memmap_init(&data, efi.memmap.late);
207 * efi_memmap_split_count - Count number of additional EFI memmap entries
208 * @md: EFI memory descriptor to split
209 * @range: Address range (start, end) to split around
211 * Returns the number of additional EFI memmap entries required to
212 * accomodate @range.
214 int __init efi_memmap_split_count(efi_memory_desc_t *md, struct range *range)
216 u64 m_start, m_end;
217 u64 start, end;
218 int count = 0;
220 start = md->phys_addr;
221 end = start + (md->num_pages << EFI_PAGE_SHIFT) - 1;
223 /* modifying range */
224 m_start = range->start;
225 m_end = range->end;
227 if (m_start <= start) {
228 /* split into 2 parts */
229 if (start < m_end && m_end < end)
230 count++;
233 if (start < m_start && m_start < end) {
234 /* split into 3 parts */
235 if (m_end < end)
236 count += 2;
237 /* split into 2 parts */
238 if (end <= m_end)
239 count++;
242 return count;
246 * efi_memmap_insert - Insert a memory region in an EFI memmap
247 * @old_memmap: The existing EFI memory map structure
248 * @buf: Address of buffer to store new map
249 * @mem: Memory map entry to insert
251 * It is suggested that you call efi_memmap_split_count() first
252 * to see how large @buf needs to be.
254 void __init efi_memmap_insert(struct efi_memory_map *old_memmap, void *buf,
255 struct efi_mem_range *mem)
257 u64 m_start, m_end, m_attr;
258 efi_memory_desc_t *md;
259 u64 start, end;
260 void *old, *new;
262 /* modifying range */
263 m_start = mem->range.start;
264 m_end = mem->range.end;
265 m_attr = mem->attribute;
268 * The EFI memory map deals with regions in EFI_PAGE_SIZE
269 * units. Ensure that the region described by 'mem' is aligned
270 * correctly.
272 if (!IS_ALIGNED(m_start, EFI_PAGE_SIZE) ||
273 !IS_ALIGNED(m_end + 1, EFI_PAGE_SIZE)) {
274 WARN_ON(1);
275 return;
278 for (old = old_memmap->map, new = buf;
279 old < old_memmap->map_end;
280 old += old_memmap->desc_size, new += old_memmap->desc_size) {
282 /* copy original EFI memory descriptor */
283 memcpy(new, old, old_memmap->desc_size);
284 md = new;
285 start = md->phys_addr;
286 end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - 1;
288 if (m_start <= start && end <= m_end)
289 md->attribute |= m_attr;
291 if (m_start <= start &&
292 (start < m_end && m_end < end)) {
293 /* first part */
294 md->attribute |= m_attr;
295 md->num_pages = (m_end - md->phys_addr + 1) >>
296 EFI_PAGE_SHIFT;
297 /* latter part */
298 new += old_memmap->desc_size;
299 memcpy(new, old, old_memmap->desc_size);
300 md = new;
301 md->phys_addr = m_end + 1;
302 md->num_pages = (end - md->phys_addr + 1) >>
303 EFI_PAGE_SHIFT;
306 if ((start < m_start && m_start < end) && m_end < end) {
307 /* first part */
308 md->num_pages = (m_start - md->phys_addr) >>
309 EFI_PAGE_SHIFT;
310 /* middle part */
311 new += old_memmap->desc_size;
312 memcpy(new, old, old_memmap->desc_size);
313 md = new;
314 md->attribute |= m_attr;
315 md->phys_addr = m_start;
316 md->num_pages = (m_end - m_start + 1) >>
317 EFI_PAGE_SHIFT;
318 /* last part */
319 new += old_memmap->desc_size;
320 memcpy(new, old, old_memmap->desc_size);
321 md = new;
322 md->phys_addr = m_end + 1;
323 md->num_pages = (end - m_end) >>
324 EFI_PAGE_SHIFT;
327 if ((start < m_start && m_start < end) &&
328 (end <= m_end)) {
329 /* first part */
330 md->num_pages = (m_start - md->phys_addr) >>
331 EFI_PAGE_SHIFT;
332 /* latter part */
333 new += old_memmap->desc_size;
334 memcpy(new, old, old_memmap->desc_size);
335 md = new;
336 md->phys_addr = m_start;
337 md->num_pages = (end - md->phys_addr + 1) >>
338 EFI_PAGE_SHIFT;
339 md->attribute |= m_attr;