1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2019 Intel Corporation. All rights reserved. */
3 #include <linux/efi.h>
4 #include <asm/e820/api.h>
5 #include "fake_mem.h"
6 
efi_fake_memmap_early(void)7 void __init efi_fake_memmap_early(void)
8 {
9 	int i;
10 
11 	/*
12 	 * The late efi_fake_mem() call can handle all requests if
13 	 * EFI_MEMORY_SP support is disabled.
14 	 */
15 	if (!efi_soft_reserve_enabled())
16 		return;
17 
18 	if (!efi_enabled(EFI_MEMMAP) || !nr_fake_mem)
19 		return;
20 
21 	/*
22 	 * Given that efi_fake_memmap() needs to perform memblock
23 	 * allocations it needs to run after e820__memblock_setup().
24 	 * However, if efi_fake_mem specifies EFI_MEMORY_SP for a given
25 	 * address range that potentially needs to mark the memory as
26 	 * reserved prior to e820__memblock_setup(). Update e820
27 	 * directly if EFI_MEMORY_SP is specified for an
28 	 * EFI_CONVENTIONAL_MEMORY descriptor.
29 	 */
30 	for (i = 0; i < nr_fake_mem; i++) {
31 		struct efi_mem_range *mem = &efi_fake_mems[i];
32 		efi_memory_desc_t *md;
33 		u64 m_start, m_end;
34 
35 		if ((mem->attribute & EFI_MEMORY_SP) == 0)
36 			continue;
37 
38 		m_start = mem->range.start;
39 		m_end = mem->range.end;
40 		for_each_efi_memory_desc(md) {
41 			u64 start, end, size;
42 
43 			if (md->type != EFI_CONVENTIONAL_MEMORY)
44 				continue;
45 
46 			start = md->phys_addr;
47 			end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - 1;
48 
49 			if (m_start <= end && m_end >= start)
50 				/* fake range overlaps descriptor */;
51 			else
52 				continue;
53 
54 			/*
55 			 * Trim the boundary of the e820 update to the
56 			 * descriptor in case the fake range overlaps
57 			 * !EFI_CONVENTIONAL_MEMORY
58 			 */
59 			start = max(start, m_start);
60 			end = min(end, m_end);
61 			size = end - start + 1;
62 
63 			if (end <= start)
64 				continue;
65 
66 			/*
67 			 * Ensure each efi_fake_mem instance results in
68 			 * a unique e820 resource
69 			 */
70 			e820__range_remove(start, size, E820_TYPE_RAM, 1);
71 			e820__range_add(start, size, E820_TYPE_SOFT_RESERVED);
72 			e820__update_table(e820_table);
73 		}
74 	}
75 }
76