xref: /aosp_15_r20/external/coreboot/src/lib/bootmem.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <console/console.h>
4 #include <bootmem.h>
5 #include <cbmem.h>
6 #include <device/resource.h>
7 #include <symbols.h>
8 #include <assert.h>
9 #include <types.h>
10 
11 static int initialized;
12 static int table_written;
13 static struct memranges bootmem;
14 static struct memranges bootmem_os;
15 
bootmem_is_initialized(void)16 static int bootmem_is_initialized(void)
17 {
18 	return initialized;
19 }
20 
bootmem_memory_table_written(void)21 static int bootmem_memory_table_written(void)
22 {
23 	return table_written;
24 }
25 
26 /* Platform hook to add bootmem areas the platform / board controls. */
bootmem_platform_add_ranges(void)27 void __attribute__((weak)) bootmem_platform_add_ranges(void)
28 {
29 }
30 
31 /* Convert bootmem tag to LB_MEM tag */
bootmem_to_lb_tag(const enum bootmem_type tag)32 static uint32_t bootmem_to_lb_tag(const enum bootmem_type tag)
33 {
34 	switch (tag) {
35 	case BM_MEM_RAM:
36 		return LB_MEM_RAM;
37 	case BM_MEM_RESERVED:
38 		return LB_MEM_RESERVED;
39 	case BM_MEM_ACPI:
40 		return LB_MEM_ACPI;
41 	case BM_MEM_NVS:
42 		return LB_MEM_NVS;
43 	case BM_MEM_UNUSABLE:
44 		return LB_MEM_UNUSABLE;
45 	case BM_MEM_VENDOR_RSVD:
46 		return LB_MEM_VENDOR_RSVD;
47 	case BM_MEM_OPENSBI:
48 		return LB_MEM_RESERVED;
49 	case BM_MEM_BL31:
50 		return LB_MEM_RESERVED;
51 	case BM_MEM_TABLE:
52 		return LB_MEM_TABLE;
53 	case BM_MEM_SOFT_RESERVED:
54 		return LB_MEM_SOFT_RESERVED;
55 	default:
56 		printk(BIOS_ERR, "Unsupported tag %u\n", tag);
57 		return LB_MEM_RESERVED;
58 	}
59 }
60 
bootmem_init(void)61 static void bootmem_init(void)
62 {
63 	const unsigned long cacheable = IORESOURCE_CACHEABLE;
64 	const unsigned long reserved = IORESOURCE_RESERVE;
65 	const unsigned long soft_reserved = IORESOURCE_SOFT_RESERVE;
66 	struct memranges *bm = &bootmem;
67 
68 	initialized = 1;
69 
70 	/*
71 	 * Fill the memory map out. The order of operations is important in
72 	 * that each overlapping range will take over the next. Therefore,
73 	 * add cacheable resources as RAM then add the reserved resources.
74 	 */
75 	memranges_init(bm, cacheable, cacheable, BM_MEM_RAM);
76 	memranges_add_resources(bm, reserved, reserved, BM_MEM_RESERVED);
77 	memranges_add_resources(bm, soft_reserved, soft_reserved, BM_MEM_SOFT_RESERVED);
78 	memranges_clone(&bootmem_os, bm);
79 
80 	/* Add memory used by CBMEM. */
81 	cbmem_add_bootmem();
82 
83 	bootmem_add_range((uintptr_t)_stack, REGION_SIZE(stack),
84 			  BM_MEM_RAMSTAGE);
85 	bootmem_add_range((uintptr_t)_program, REGION_SIZE(program),
86 			  BM_MEM_RAMSTAGE);
87 
88 	bootmem_arch_add_ranges();
89 	bootmem_platform_add_ranges();
90 }
91 
bootmem_add_range(uint64_t start,uint64_t size,const enum bootmem_type tag)92 void bootmem_add_range(uint64_t start, uint64_t size,
93 		       const enum bootmem_type tag)
94 {
95 	assert(tag > BM_MEM_FIRST && tag < BM_MEM_LAST);
96 	assert(bootmem_is_initialized());
97 
98 	memranges_insert(&bootmem, start, size, tag);
99 	if (tag <= BM_MEM_OS_CUTOFF) {
100 		/* Can't change OS tables anymore after they are written out. */
101 		assert(!bootmem_memory_table_written());
102 		memranges_insert(&bootmem_os, start, size, tag);
103 	};
104 }
105 
bootmem_write_memory_table(struct lb_memory * mem)106 void bootmem_write_memory_table(struct lb_memory *mem)
107 {
108 	const struct range_entry *r;
109 	struct lb_memory_range *lb_r;
110 
111 	lb_r = &mem->map[0];
112 
113 	bootmem_init();
114 	bootmem_dump_ranges();
115 
116 	memranges_each_entry(r, &bootmem_os) {
117 		lb_r->start = range_entry_base(r);
118 		lb_r->size = range_entry_size(r);
119 		lb_r->type = bootmem_to_lb_tag(range_entry_tag(r));
120 
121 		lb_r++;
122 		mem->size += sizeof(struct lb_memory_range);
123 	}
124 
125 	table_written = 1;
126 }
127 
128 struct range_strings {
129 	enum bootmem_type tag;
130 	const char *str;
131 };
132 
133 static const struct range_strings type_strings[] = {
134 	{ BM_MEM_RAM, "RAM" },
135 	{ BM_MEM_RESERVED, "RESERVED" },
136 	{ BM_MEM_ACPI, "ACPI" },
137 	{ BM_MEM_NVS, "NVS" },
138 	{ BM_MEM_UNUSABLE, "UNUSABLE" },
139 	{ BM_MEM_VENDOR_RSVD, "VENDOR RESERVED" },
140 	{ BM_MEM_BL31, "BL31" },
141 	{ BM_MEM_OPENSBI, "OPENSBI" },
142 	{ BM_MEM_TABLE, "CONFIGURATION TABLES" },
143 	{ BM_MEM_SOFT_RESERVED, "SOFT RESERVED" },
144 	{ BM_MEM_RAMSTAGE, "RAMSTAGE" },
145 	{ BM_MEM_PAYLOAD, "PAYLOAD" },
146 };
147 
bootmem_range_string(const enum bootmem_type tag)148 static const char *bootmem_range_string(const enum bootmem_type tag)
149 {
150 	int i;
151 
152 	for (i = 0; i < ARRAY_SIZE(type_strings); i++) {
153 		if (type_strings[i].tag == tag)
154 			return type_strings[i].str;
155 	}
156 
157 	return "UNKNOWN!";
158 }
159 
bootmem_dump_ranges(void)160 void bootmem_dump_ranges(void)
161 {
162 	int i;
163 	const struct range_entry *r;
164 
165 	i = 0;
166 	memranges_each_entry(r, &bootmem) {
167 		printk(BIOS_DEBUG, "%2d. %016llx-%016llx: %s\n",
168 			i, range_entry_base(r), range_entry_end(r) - 1,
169 			bootmem_range_string(range_entry_tag(r)));
170 		i++;
171 	}
172 }
173 
bootmem_walk_os_mem(range_action_t action,void * arg)174 bool bootmem_walk_os_mem(range_action_t action, void *arg)
175 {
176 	const struct range_entry *r;
177 
178 	assert(bootmem_is_initialized());
179 
180 	memranges_each_entry(r, &bootmem_os) {
181 		if (!action(r, arg))
182 			return true;
183 	}
184 
185 	return false;
186 }
187 
bootmem_walk(range_action_t action,void * arg)188 bool bootmem_walk(range_action_t action, void *arg)
189 {
190 	const struct range_entry *r;
191 
192 	assert(bootmem_is_initialized());
193 
194 	memranges_each_entry(r, &bootmem) {
195 		if (!action(r, arg))
196 			return true;
197 	}
198 
199 	return false;
200 }
201 
bootmem_region_targets_type(uint64_t start,uint64_t size,enum bootmem_type dest_type)202 int bootmem_region_targets_type(uint64_t start, uint64_t size,
203 				enum bootmem_type dest_type)
204 {
205 	const struct range_entry *r;
206 	uint64_t end = start + size;
207 
208 	memranges_each_entry(r, &bootmem) {
209 		/* All further bootmem entries are beyond this range. */
210 		if (end <= range_entry_base(r))
211 			break;
212 
213 		if (start >= range_entry_base(r) && end <= range_entry_end(r)) {
214 			if (range_entry_tag(r) == dest_type)
215 				return 1;
216 		}
217 	}
218 	return 0;
219 }
220 
bootmem_allocate_buffer(size_t size)221 void *bootmem_allocate_buffer(size_t size)
222 {
223 	const struct range_entry *r;
224 	const struct range_entry *region;
225 	/* All allocated buffers fall below the 32-bit boundary. */
226 	const resource_t max_addr = 1ULL << 32;
227 	resource_t begin;
228 	resource_t end;
229 
230 	if (!bootmem_is_initialized()) {
231 		printk(BIOS_ERR, "%s: lib uninitialized!\n", __func__);
232 		return NULL;
233 	}
234 
235 	/* 4KiB alignment. */
236 	size = ALIGN_UP(size, 4096);
237 	region = NULL;
238 	memranges_each_entry(r, &bootmem) {
239 		if (range_entry_base(r) >= max_addr)
240 			break;
241 
242 		if (range_entry_size(r) < size)
243 			continue;
244 
245 		if (range_entry_tag(r) != BM_MEM_RAM)
246 			continue;
247 
248 		end = range_entry_end(r);
249 		if (end > max_addr)
250 			end = max_addr;
251 
252 		if ((end - range_entry_base(r)) < size)
253 			continue;
254 
255 		region = r;
256 	}
257 
258 	if (region == NULL)
259 		return NULL;
260 
261 	/* region now points to the highest usable region for the given size. */
262 	end = range_entry_end(region);
263 	if (end > max_addr)
264 		end = max_addr;
265 	begin = end - size;
266 
267 	/* Mark buffer as unusable for future buffer use. */
268 	bootmem_add_range(begin, size, BM_MEM_PAYLOAD);
269 
270 	return (void *)(uintptr_t)begin;
271 }
272