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