1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <console/console.h>
6 #include <device/resource.h>
11 static int initialized
;
12 static int table_written
;
13 static struct memranges bootmem
;
14 static struct memranges bootmem_os
;
16 static int bootmem_is_initialized(void)
21 static int bootmem_memory_table_written(void)
26 /* Platform hook to add bootmem areas the platform / board controls. */
27 void __attribute__((weak
)) bootmem_platform_add_ranges(void)
31 /* Convert bootmem tag to LB_MEM tag */
32 static uint32_t bootmem_to_lb_tag(const enum bootmem_type tag
)
38 return LB_MEM_RESERVED
;
44 return LB_MEM_UNUSABLE
;
45 case BM_MEM_VENDOR_RSVD
:
46 return LB_MEM_VENDOR_RSVD
;
48 return LB_MEM_RESERVED
;
50 return LB_MEM_RESERVED
;
54 printk(BIOS_ERR
, "Unsupported tag %u\n", tag
);
55 return LB_MEM_RESERVED
;
59 static void bootmem_init(void)
61 const unsigned long cacheable
= IORESOURCE_CACHEABLE
;
62 const unsigned long reserved
= IORESOURCE_RESERVE
;
63 struct memranges
*bm
= &bootmem
;
68 * Fill the memory map out. The order of operations is important in
69 * that each overlapping range will take over the next. Therefore,
70 * add cacheable resources as RAM then add the reserved resources.
72 memranges_init(bm
, cacheable
, cacheable
, BM_MEM_RAM
);
73 memranges_add_resources(bm
, reserved
, reserved
, BM_MEM_RESERVED
);
74 memranges_clone(&bootmem_os
, bm
);
76 /* Add memory used by CBMEM. */
79 bootmem_add_range((uintptr_t)_stack
, REGION_SIZE(stack
),
81 bootmem_add_range((uintptr_t)_program
, REGION_SIZE(program
),
84 bootmem_arch_add_ranges();
85 bootmem_platform_add_ranges();
88 void bootmem_add_range(uint64_t start
, uint64_t size
,
89 const enum bootmem_type tag
)
91 assert(tag
> BM_MEM_FIRST
&& tag
< BM_MEM_LAST
);
92 assert(bootmem_is_initialized());
94 memranges_insert(&bootmem
, start
, size
, tag
);
95 if (tag
<= BM_MEM_OS_CUTOFF
) {
96 /* Can't change OS tables anymore after they are written out. */
97 assert(!bootmem_memory_table_written());
98 memranges_insert(&bootmem_os
, start
, size
, tag
);
102 void bootmem_write_memory_table(struct lb_memory
*mem
)
104 const struct range_entry
*r
;
105 struct lb_memory_range
*lb_r
;
110 bootmem_dump_ranges();
112 memranges_each_entry(r
, &bootmem_os
) {
113 lb_r
->start
= range_entry_base(r
);
114 lb_r
->size
= range_entry_size(r
);
115 lb_r
->type
= bootmem_to_lb_tag(range_entry_tag(r
));
118 mem
->size
+= sizeof(struct lb_memory_range
);
124 struct range_strings
{
125 enum bootmem_type tag
;
129 static const struct range_strings type_strings
[] = {
130 { BM_MEM_RAM
, "RAM" },
131 { BM_MEM_RESERVED
, "RESERVED" },
132 { BM_MEM_ACPI
, "ACPI" },
133 { BM_MEM_NVS
, "NVS" },
134 { BM_MEM_UNUSABLE
, "UNUSABLE" },
135 { BM_MEM_VENDOR_RSVD
, "VENDOR RESERVED" },
136 { BM_MEM_BL31
, "BL31" },
137 { BM_MEM_OPENSBI
, "OPENSBI" },
138 { BM_MEM_TABLE
, "CONFIGURATION TABLES" },
139 { BM_MEM_RAMSTAGE
, "RAMSTAGE" },
140 { BM_MEM_PAYLOAD
, "PAYLOAD" },
143 static const char *bootmem_range_string(const enum bootmem_type tag
)
147 for (i
= 0; i
< ARRAY_SIZE(type_strings
); i
++) {
148 if (type_strings
[i
].tag
== tag
)
149 return type_strings
[i
].str
;
155 void bootmem_dump_ranges(void)
158 const struct range_entry
*r
;
161 memranges_each_entry(r
, &bootmem
) {
162 printk(BIOS_DEBUG
, "%2d. %016llx-%016llx: %s\n",
163 i
, range_entry_base(r
), range_entry_end(r
) - 1,
164 bootmem_range_string(range_entry_tag(r
)));
169 bool bootmem_walk_os_mem(range_action_t action
, void *arg
)
171 const struct range_entry
*r
;
173 assert(bootmem_is_initialized());
175 memranges_each_entry(r
, &bootmem_os
) {
183 bool bootmem_walk(range_action_t action
, void *arg
)
185 const struct range_entry
*r
;
187 assert(bootmem_is_initialized());
189 memranges_each_entry(r
, &bootmem
) {
197 int bootmem_region_targets_type(uint64_t start
, uint64_t size
,
198 enum bootmem_type dest_type
)
200 const struct range_entry
*r
;
201 uint64_t end
= start
+ size
;
203 memranges_each_entry(r
, &bootmem
) {
204 /* All further bootmem entries are beyond this range. */
205 if (end
<= range_entry_base(r
))
208 if (start
>= range_entry_base(r
) && end
<= range_entry_end(r
)) {
209 if (range_entry_tag(r
) == dest_type
)
216 void *bootmem_allocate_buffer(size_t size
)
218 const struct range_entry
*r
;
219 const struct range_entry
*region
;
220 /* All allocated buffers fall below the 32-bit boundary. */
221 const resource_t max_addr
= 1ULL << 32;
225 if (!bootmem_is_initialized()) {
226 printk(BIOS_ERR
, "%s: lib uninitialized!\n", __func__
);
230 /* 4KiB alignment. */
231 size
= ALIGN_UP(size
, 4096);
233 memranges_each_entry(r
, &bootmem
) {
234 if (range_entry_base(r
) >= max_addr
)
237 if (range_entry_size(r
) < size
)
240 if (range_entry_tag(r
) != BM_MEM_RAM
)
243 end
= range_entry_end(r
);
247 if ((end
- range_entry_base(r
)) < size
)
256 /* region now points to the highest usable region for the given size. */
257 end
= range_entry_end(region
);
262 /* Mark buffer as unusable for future buffer use. */
263 bootmem_add_range(begin
, size
, BM_MEM_PAYLOAD
);
265 return (void *)(uintptr_t)begin
;