blob: 578ddac19f108fca3113c93e53eca06fe7c48635 [file] [log] [blame]
Angel Pons118a9c72020-04-02 23:48:34 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbin49048022014-02-18 21:55:02 -06002
3#include <console/console.h>
4#include <bootmem.h>
5#include <cbmem.h>
6#include <device/resource.h>
Patrick Rudolph23d62dd2018-04-12 10:36:57 +02007#include <symbols.h>
Patrick Rudolph9ab9db02018-04-05 09:14:51 +02008#include <assert.h>
Elyes HAOUAS93a195c2021-12-31 18:46:13 +01009#include <types.h>
Aaron Durbin49048022014-02-18 21:55:02 -060010
Aaron Durbin4677f6b2018-04-03 00:08:12 -060011static int initialized;
Patrick Rudolph23d62dd2018-04-12 10:36:57 +020012static int table_written;
Aaron Durbin49048022014-02-18 21:55:02 -060013static struct memranges bootmem;
Patrick Rudolph64049be2018-04-20 10:37:51 +020014static struct memranges bootmem_os;
Aaron Durbin49048022014-02-18 21:55:02 -060015
Aaron Durbin4677f6b2018-04-03 00:08:12 -060016static int bootmem_is_initialized(void)
17{
18 return initialized;
19}
20
Patrick Rudolph23d62dd2018-04-12 10:36:57 +020021static int bootmem_memory_table_written(void)
22{
23 return table_written;
24}
25
26/* Platform hook to add bootmem areas the platform / board controls. */
27void __attribute__((weak)) bootmem_platform_add_ranges(void)
28{
29}
30
Patrick Rudolph9ab9db02018-04-05 09:14:51 +020031/* Convert bootmem tag to LB_MEM tag */
32static 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;
Patrick Rudolph4c3da702019-07-07 13:10:56 +020047 case BM_MEM_OPENSBI:
48 return LB_MEM_RESERVED;
Ting Shendff29e02019-01-28 18:15:00 +080049 case BM_MEM_BL31:
50 return LB_MEM_RESERVED;
Patrick Rudolph9ab9db02018-04-05 09:14:51 +020051 case BM_MEM_TABLE:
52 return LB_MEM_TABLE;
Jonathan Zhange111de02021-04-21 10:40:31 -070053 case BM_MEM_SOFT_RESERVED:
54 return LB_MEM_SOFT_RESERVED;
Patrick Rudolph9ab9db02018-04-05 09:14:51 +020055 default:
Julius Wernere9665952022-01-21 17:06:20 -080056 printk(BIOS_ERR, "Unsupported tag %u\n", tag);
Patrick Rudolph9ab9db02018-04-05 09:14:51 +020057 return LB_MEM_RESERVED;
58 }
59}
60
Aaron Durbin4677f6b2018-04-03 00:08:12 -060061static void bootmem_init(void)
Aaron Durbin49048022014-02-18 21:55:02 -060062{
63 const unsigned long cacheable = IORESOURCE_CACHEABLE;
64 const unsigned long reserved = IORESOURCE_RESERVE;
Jonathan Zhange111de02021-04-21 10:40:31 -070065 const unsigned long soft_reserved = IORESOURCE_SOFT_RESERVE;
Aaron Durbin49048022014-02-18 21:55:02 -060066 struct memranges *bm = &bootmem;
67
Aaron Durbin4677f6b2018-04-03 00:08:12 -060068 initialized = 1;
69
Aaron Durbin49048022014-02-18 21:55:02 -060070 /*
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 */
Patrick Rudolph9ab9db02018-04-05 09:14:51 +020075 memranges_init(bm, cacheable, cacheable, BM_MEM_RAM);
76 memranges_add_resources(bm, reserved, reserved, BM_MEM_RESERVED);
Jonathan Zhange111de02021-04-21 10:40:31 -070077 memranges_add_resources(bm, soft_reserved, soft_reserved, BM_MEM_SOFT_RESERVED);
Julius Wernerae05d092018-05-08 17:09:57 -070078 memranges_clone(&bootmem_os, bm);
Aaron Durbin49048022014-02-18 21:55:02 -060079
80 /* Add memory used by CBMEM. */
81 cbmem_add_bootmem();
Aaron Durbind4afa932016-04-19 17:25:59 -050082
Julius Werner7e0dea62019-02-20 18:39:22 -080083 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);
Patrick Rudolph23d62dd2018-04-12 10:36:57 +020087
Aaron Durbind4afa932016-04-19 17:25:59 -050088 bootmem_arch_add_ranges();
Patrick Rudolph23d62dd2018-04-12 10:36:57 +020089 bootmem_platform_add_ranges();
Aaron Durbin49048022014-02-18 21:55:02 -060090}
91
Patrick Rudolph9ab9db02018-04-05 09:14:51 +020092void bootmem_add_range(uint64_t start, uint64_t size,
93 const enum bootmem_type tag)
Aaron Durbin49048022014-02-18 21:55:02 -060094{
Patrick Rudolph9ab9db02018-04-05 09:14:51 +020095 assert(tag > BM_MEM_FIRST && tag < BM_MEM_LAST);
96 assert(bootmem_is_initialized());
Aaron Durbin4677f6b2018-04-03 00:08:12 -060097
Patrick Rudolph9ab9db02018-04-05 09:14:51 +020098 memranges_insert(&bootmem, start, size, tag);
Julius Wernerae05d092018-05-08 17:09:57 -070099 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 };
Aaron Durbin49048022014-02-18 21:55:02 -0600104}
105
106void 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
Aaron Durbin4677f6b2018-04-03 00:08:12 -0600113 bootmem_init();
Aaron Durbin49048022014-02-18 21:55:02 -0600114 bootmem_dump_ranges();
115
Patrick Rudolph64049be2018-04-20 10:37:51 +0200116 memranges_each_entry(r, &bootmem_os) {
Jianjun Wangb2537bd2022-04-08 16:57:28 +0800117 lb_r->start = range_entry_base(r);
118 lb_r->size = range_entry_size(r);
Patrick Rudolph9ab9db02018-04-05 09:14:51 +0200119 lb_r->type = bootmem_to_lb_tag(range_entry_tag(r));
Aaron Durbin49048022014-02-18 21:55:02 -0600120
121 lb_r++;
122 mem->size += sizeof(struct lb_memory_range);
123 }
Patrick Rudolph23d62dd2018-04-12 10:36:57 +0200124
Patrick Rudolph23d62dd2018-04-12 10:36:57 +0200125 table_written = 1;
Aaron Durbin49048022014-02-18 21:55:02 -0600126}
127
128struct range_strings {
Patrick Rudolph9ab9db02018-04-05 09:14:51 +0200129 enum bootmem_type tag;
Aaron Durbin49048022014-02-18 21:55:02 -0600130 const char *str;
131};
132
133static const struct range_strings type_strings[] = {
Patrick Rudolph9ab9db02018-04-05 09:14:51 +0200134 { 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" },
Ting Shendff29e02019-01-28 18:15:00 +0800140 { BM_MEM_BL31, "BL31" },
Patrick Rudolph4c3da702019-07-07 13:10:56 +0200141 { BM_MEM_OPENSBI, "OPENSBI" },
Patrick Rudolph9ab9db02018-04-05 09:14:51 +0200142 { BM_MEM_TABLE, "CONFIGURATION TABLES" },
Jonathan Zhange111de02021-04-21 10:40:31 -0700143 { BM_MEM_SOFT_RESERVED, "SOFT RESERVED" },
Julius Wernerae05d092018-05-08 17:09:57 -0700144 { BM_MEM_RAMSTAGE, "RAMSTAGE" },
145 { BM_MEM_PAYLOAD, "PAYLOAD" },
Aaron Durbin49048022014-02-18 21:55:02 -0600146};
147
Patrick Rudolph9ab9db02018-04-05 09:14:51 +0200148static const char *bootmem_range_string(const enum bootmem_type tag)
Aaron Durbin49048022014-02-18 21:55:02 -0600149{
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
160void 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
Patrick Rudolph64049be2018-04-20 10:37:51 +0200174bool 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
Patrick Rudolphc6536232018-04-10 09:34:29 +0200188bool 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
Ting Shen05532262019-01-28 17:22:22 +0800202int bootmem_region_targets_type(uint64_t start, uint64_t size,
203 enum bootmem_type dest_type)
Aaron Durbin49048022014-02-18 21:55:02 -0600204{
205 const struct range_entry *r;
Ting Shen05532262019-01-28 17:22:22 +0800206 uint64_t end = start + size;
Aaron Durbin49048022014-02-18 21:55:02 -0600207
Ting Shen05532262019-01-28 17:22:22 +0800208 memranges_each_entry(r, &bootmem) {
Aaron Durbin49048022014-02-18 21:55:02 -0600209 /* 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)) {
Ting Shen05532262019-01-28 17:22:22 +0800214 if (range_entry_tag(r) == dest_type)
Aaron Durbin49048022014-02-18 21:55:02 -0600215 return 1;
216 }
217 }
218 return 0;
219}
220
221void *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
Aaron Durbin4677f6b2018-04-03 00:08:12 -0600230 if (!bootmem_is_initialized()) {
Wim Vervoorn87c52802019-11-12 11:15:23 +0100231 printk(BIOS_ERR, "%s: lib uninitialized!\n", __func__);
Aaron Durbin4677f6b2018-04-03 00:08:12 -0600232 return NULL;
233 }
234
Aaron Durbin49048022014-02-18 21:55:02 -0600235 /* 4KiB alignment. */
Elyes Haouasd6b6b222022-10-10 12:34:21 +0200236 size = ALIGN_UP(size, 4096);
Aaron Durbin49048022014-02-18 21:55:02 -0600237 region = NULL;
238 memranges_each_entry(r, &bootmem) {
Jan Dabros821b1e22020-07-16 13:45:49 +0200239 if (range_entry_base(r) >= max_addr)
240 break;
241
Aaron Durbin49048022014-02-18 21:55:02 -0600242 if (range_entry_size(r) < size)
243 continue;
244
Patrick Rudolph9ab9db02018-04-05 09:14:51 +0200245 if (range_entry_tag(r) != BM_MEM_RAM)
Aaron Durbin49048022014-02-18 21:55:02 -0600246 continue;
247
Aaron Durbin49048022014-02-18 21:55:02 -0600248 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. */
Aaron Durbin49048022014-02-18 21:55:02 -0600262 end = range_entry_end(region);
263 if (end > max_addr)
264 end = max_addr;
265 begin = end - size;
266
Elyes HAOUAS0bc9f0b2019-12-06 18:07:33 +0100267 /* Mark buffer as unusable for future buffer use. */
Patrick Rudolph23d62dd2018-04-12 10:36:57 +0200268 bootmem_add_range(begin, size, BM_MEM_PAYLOAD);
Aaron Durbin49048022014-02-18 21:55:02 -0600269
270 return (void *)(uintptr_t)begin;
271}