xref: /openbmc/linux/drivers/firmware/efi/memmap.c (revision ba61bb17)
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 <asm/early_ioremap.h>
13 #include <linux/memblock.h>
14 #include <linux/slab.h>
15 
16 static phys_addr_t __init __efi_memmap_alloc_early(unsigned long size)
17 {
18 	return memblock_alloc(size, 0);
19 }
20 
21 static phys_addr_t __init __efi_memmap_alloc_late(unsigned long size)
22 {
23 	unsigned int order = get_order(size);
24 	struct page *p = alloc_pages(GFP_KERNEL, order);
25 
26 	if (!p)
27 		return 0;
28 
29 	return PFN_PHYS(page_to_pfn(p));
30 }
31 
32 /**
33  * efi_memmap_alloc - Allocate memory for the EFI memory map
34  * @num_entries: Number of entries in the allocated map.
35  *
36  * Depending on whether mm_init() has already been invoked or not,
37  * either memblock or "normal" page allocation is used.
38  *
39  * Returns the physical address of the allocated memory map on
40  * success, zero on failure.
41  */
42 phys_addr_t __init efi_memmap_alloc(unsigned int num_entries)
43 {
44 	unsigned long size = num_entries * efi.memmap.desc_size;
45 
46 	if (slab_is_available())
47 		return __efi_memmap_alloc_late(size);
48 
49 	return __efi_memmap_alloc_early(size);
50 }
51 
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?
56  *
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.
60  *
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.
65  *
66  * Returns zero on success, a negative error code on failure.
67  */
68 static int __init
69 __efi_memmap_init(struct efi_memory_map_data *data, bool late)
70 {
71 	struct efi_memory_map map;
72 	phys_addr_t phys_map;
73 
74 	if (efi_enabled(EFI_PARAVIRT))
75 		return 0;
76 
77 	phys_map = data->phys_map;
78 
79 	if (late)
80 		map.map = memremap(phys_map, data->size, MEMREMAP_WB);
81 	else
82 		map.map = early_memremap(phys_map, data->size);
83 
84 	if (!map.map) {
85 		pr_err("Could not map the memory map!\n");
86 		return -ENOMEM;
87 	}
88 
89 	map.phys_map = data->phys_map;
90 	map.nr_map = data->size / data->desc_size;
91 	map.map_end = map.map + data->size;
92 
93 	map.desc_version = data->desc_version;
94 	map.desc_size = data->desc_size;
95 	map.late = late;
96 
97 	set_bit(EFI_MEMMAP, &efi.flags);
98 
99 	efi.memmap = map;
100 
101 	return 0;
102 }
103 
104 /**
105  * efi_memmap_init_early - Map the EFI memory map data structure
106  * @data: EFI memory map data
107  *
108  * Use early_memremap() to map the passed in EFI memory map and assign
109  * it to efi.memmap.
110  */
111 int __init efi_memmap_init_early(struct efi_memory_map_data *data)
112 {
113 	/* Cannot go backwards */
114 	WARN_ON(efi.memmap.late);
115 
116 	return __efi_memmap_init(data, false);
117 }
118 
119 void __init efi_memmap_unmap(void)
120 {
121 	if (!efi.memmap.late) {
122 		unsigned long size;
123 
124 		size = efi.memmap.desc_size * efi.memmap.nr_map;
125 		early_memunmap(efi.memmap.map, size);
126 	} else {
127 		memunmap(efi.memmap.map);
128 	}
129 
130 	efi.memmap.map = NULL;
131 	clear_bit(EFI_MEMMAP, &efi.flags);
132 }
133 
134 /**
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
138  *
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.
144  *
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.
150  *
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.
154  *
155  * Returns zero on success, a negative error code on failure.
156  */
157 int __init efi_memmap_init_late(phys_addr_t addr, unsigned long size)
158 {
159 	struct efi_memory_map_data data = {
160 		.phys_map = addr,
161 		.size = size,
162 	};
163 
164 	/* Did we forget to unmap the early EFI memmap? */
165 	WARN_ON(efi.memmap.map);
166 
167 	/* Were we already called? */
168 	WARN_ON(efi.memmap.late);
169 
170 	/*
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.
174 	 */
175 	data.desc_version = efi.memmap.desc_version;
176 	data.desc_size = efi.memmap.desc_size;
177 
178 	return __efi_memmap_init(&data, true);
179 }
180 
181 /**
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
185  *
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.
189  *
190  * Returns zero on success, a negative error code on failure.
191  */
192 int __init efi_memmap_install(phys_addr_t addr, unsigned int nr_map)
193 {
194 	struct efi_memory_map_data data;
195 
196 	efi_memmap_unmap();
197 
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;
202 
203 	return __efi_memmap_init(&data, efi.memmap.late);
204 }
205 
206 /**
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
210  *
211  * Returns the number of additional EFI memmap entries required to
212  * accomodate @range.
213  */
214 int __init efi_memmap_split_count(efi_memory_desc_t *md, struct range *range)
215 {
216 	u64 m_start, m_end;
217 	u64 start, end;
218 	int count = 0;
219 
220 	start = md->phys_addr;
221 	end = start + (md->num_pages << EFI_PAGE_SHIFT) - 1;
222 
223 	/* modifying range */
224 	m_start = range->start;
225 	m_end = range->end;
226 
227 	if (m_start <= start) {
228 		/* split into 2 parts */
229 		if (start < m_end && m_end < end)
230 			count++;
231 	}
232 
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++;
240 	}
241 
242 	return count;
243 }
244 
245 /**
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
250  *
251  * It is suggested that you call efi_memmap_split_count() first
252  * to see how large @buf needs to be.
253  */
254 void __init efi_memmap_insert(struct efi_memory_map *old_memmap, void *buf,
255 			      struct efi_mem_range *mem)
256 {
257 	u64 m_start, m_end, m_attr;
258 	efi_memory_desc_t *md;
259 	u64 start, end;
260 	void *old, *new;
261 
262 	/* modifying range */
263 	m_start = mem->range.start;
264 	m_end = mem->range.end;
265 	m_attr = mem->attribute;
266 
267 	/*
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.
271 	 */
272 	if (!IS_ALIGNED(m_start, EFI_PAGE_SIZE) ||
273 	    !IS_ALIGNED(m_end + 1, EFI_PAGE_SIZE)) {
274 		WARN_ON(1);
275 		return;
276 	}
277 
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) {
281 
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;
287 
288 		if (m_start <= start && end <= m_end)
289 			md->attribute |= m_attr;
290 
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;
304 		}
305 
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;
325 		}
326 
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;
340 		}
341 	}
342 }
343