Angel Pons | 118a9c7 | 2020-04-02 23:48:34 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 2 | |
| 3 | #include <console/console.h> |
| 4 | #include <bootmem.h> |
| 5 | #include <cbmem.h> |
| 6 | #include <device/resource.h> |
| 7 | #include <stdlib.h> |
Patrick Rudolph | 23d62dd | 2018-04-12 10:36:57 +0200 | [diff] [blame] | 8 | #include <symbols.h> |
Patrick Rudolph | 9ab9db0 | 2018-04-05 09:14:51 +0200 | [diff] [blame] | 9 | #include <assert.h> |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 10 | |
Aaron Durbin | 4677f6b | 2018-04-03 00:08:12 -0600 | [diff] [blame] | 11 | static int initialized; |
Patrick Rudolph | 23d62dd | 2018-04-12 10:36:57 +0200 | [diff] [blame] | 12 | static int table_written; |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 13 | static struct memranges bootmem; |
Patrick Rudolph | 64049be | 2018-04-20 10:37:51 +0200 | [diff] [blame] | 14 | static struct memranges bootmem_os; |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 15 | |
Aaron Durbin | 4677f6b | 2018-04-03 00:08:12 -0600 | [diff] [blame] | 16 | static int bootmem_is_initialized(void) |
| 17 | { |
| 18 | return initialized; |
| 19 | } |
| 20 | |
Patrick Rudolph | 23d62dd | 2018-04-12 10:36:57 +0200 | [diff] [blame] | 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. */ |
| 27 | void __attribute__((weak)) bootmem_platform_add_ranges(void) |
| 28 | { |
| 29 | } |
| 30 | |
Patrick Rudolph | 9ab9db0 | 2018-04-05 09:14:51 +0200 | [diff] [blame] | 31 | /* Convert bootmem tag to LB_MEM 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; |
Patrick Rudolph | 4c3da70 | 2019-07-07 13:10:56 +0200 | [diff] [blame] | 47 | case BM_MEM_OPENSBI: |
| 48 | return LB_MEM_RESERVED; |
Ting Shen | dff29e0 | 2019-01-28 18:15:00 +0800 | [diff] [blame] | 49 | case BM_MEM_BL31: |
| 50 | return LB_MEM_RESERVED; |
Patrick Rudolph | 9ab9db0 | 2018-04-05 09:14:51 +0200 | [diff] [blame] | 51 | case BM_MEM_TABLE: |
| 52 | return LB_MEM_TABLE; |
| 53 | default: |
| 54 | printk(BIOS_ERR, "ERROR: Unsupported tag %u\n", tag); |
| 55 | return LB_MEM_RESERVED; |
| 56 | } |
| 57 | } |
| 58 | |
Aaron Durbin | 4677f6b | 2018-04-03 00:08:12 -0600 | [diff] [blame] | 59 | static void bootmem_init(void) |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 60 | { |
| 61 | const unsigned long cacheable = IORESOURCE_CACHEABLE; |
| 62 | const unsigned long reserved = IORESOURCE_RESERVE; |
| 63 | struct memranges *bm = &bootmem; |
| 64 | |
Aaron Durbin | 4677f6b | 2018-04-03 00:08:12 -0600 | [diff] [blame] | 65 | initialized = 1; |
| 66 | |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 67 | /* |
| 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. |
| 71 | */ |
Patrick Rudolph | 9ab9db0 | 2018-04-05 09:14:51 +0200 | [diff] [blame] | 72 | memranges_init(bm, cacheable, cacheable, BM_MEM_RAM); |
| 73 | memranges_add_resources(bm, reserved, reserved, BM_MEM_RESERVED); |
Julius Werner | ae05d09 | 2018-05-08 17:09:57 -0700 | [diff] [blame] | 74 | memranges_clone(&bootmem_os, bm); |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 75 | |
| 76 | /* Add memory used by CBMEM. */ |
| 77 | cbmem_add_bootmem(); |
Aaron Durbin | d4afa93 | 2016-04-19 17:25:59 -0500 | [diff] [blame] | 78 | |
Julius Werner | 7e0dea6 | 2019-02-20 18:39:22 -0800 | [diff] [blame] | 79 | bootmem_add_range((uintptr_t)_stack, REGION_SIZE(stack), |
| 80 | BM_MEM_RAMSTAGE); |
| 81 | bootmem_add_range((uintptr_t)_program, REGION_SIZE(program), |
| 82 | BM_MEM_RAMSTAGE); |
Patrick Rudolph | 23d62dd | 2018-04-12 10:36:57 +0200 | [diff] [blame] | 83 | |
Aaron Durbin | d4afa93 | 2016-04-19 17:25:59 -0500 | [diff] [blame] | 84 | bootmem_arch_add_ranges(); |
Patrick Rudolph | 23d62dd | 2018-04-12 10:36:57 +0200 | [diff] [blame] | 85 | bootmem_platform_add_ranges(); |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 86 | } |
| 87 | |
Patrick Rudolph | 9ab9db0 | 2018-04-05 09:14:51 +0200 | [diff] [blame] | 88 | void bootmem_add_range(uint64_t start, uint64_t size, |
| 89 | const enum bootmem_type tag) |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 90 | { |
Patrick Rudolph | 9ab9db0 | 2018-04-05 09:14:51 +0200 | [diff] [blame] | 91 | assert(tag > BM_MEM_FIRST && tag < BM_MEM_LAST); |
| 92 | assert(bootmem_is_initialized()); |
Aaron Durbin | 4677f6b | 2018-04-03 00:08:12 -0600 | [diff] [blame] | 93 | |
Patrick Rudolph | 9ab9db0 | 2018-04-05 09:14:51 +0200 | [diff] [blame] | 94 | memranges_insert(&bootmem, start, size, tag); |
Julius Werner | ae05d09 | 2018-05-08 17:09:57 -0700 | [diff] [blame] | 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); |
| 99 | }; |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void bootmem_write_memory_table(struct lb_memory *mem) |
| 103 | { |
| 104 | const struct range_entry *r; |
| 105 | struct lb_memory_range *lb_r; |
| 106 | |
| 107 | lb_r = &mem->map[0]; |
| 108 | |
Aaron Durbin | 4677f6b | 2018-04-03 00:08:12 -0600 | [diff] [blame] | 109 | bootmem_init(); |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 110 | bootmem_dump_ranges(); |
| 111 | |
Patrick Rudolph | 64049be | 2018-04-20 10:37:51 +0200 | [diff] [blame] | 112 | memranges_each_entry(r, &bootmem_os) { |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 113 | lb_r->start = pack_lb64(range_entry_base(r)); |
| 114 | lb_r->size = pack_lb64(range_entry_size(r)); |
Patrick Rudolph | 9ab9db0 | 2018-04-05 09:14:51 +0200 | [diff] [blame] | 115 | lb_r->type = bootmem_to_lb_tag(range_entry_tag(r)); |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 116 | |
| 117 | lb_r++; |
| 118 | mem->size += sizeof(struct lb_memory_range); |
| 119 | } |
Patrick Rudolph | 23d62dd | 2018-04-12 10:36:57 +0200 | [diff] [blame] | 120 | |
Patrick Rudolph | 23d62dd | 2018-04-12 10:36:57 +0200 | [diff] [blame] | 121 | table_written = 1; |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | struct range_strings { |
Patrick Rudolph | 9ab9db0 | 2018-04-05 09:14:51 +0200 | [diff] [blame] | 125 | enum bootmem_type tag; |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 126 | const char *str; |
| 127 | }; |
| 128 | |
| 129 | static const struct range_strings type_strings[] = { |
Patrick Rudolph | 9ab9db0 | 2018-04-05 09:14:51 +0200 | [diff] [blame] | 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" }, |
Ting Shen | dff29e0 | 2019-01-28 18:15:00 +0800 | [diff] [blame] | 136 | { BM_MEM_BL31, "BL31" }, |
Patrick Rudolph | 4c3da70 | 2019-07-07 13:10:56 +0200 | [diff] [blame] | 137 | { BM_MEM_OPENSBI, "OPENSBI" }, |
Patrick Rudolph | 9ab9db0 | 2018-04-05 09:14:51 +0200 | [diff] [blame] | 138 | { BM_MEM_TABLE, "CONFIGURATION TABLES" }, |
Julius Werner | ae05d09 | 2018-05-08 17:09:57 -0700 | [diff] [blame] | 139 | { BM_MEM_RAMSTAGE, "RAMSTAGE" }, |
| 140 | { BM_MEM_PAYLOAD, "PAYLOAD" }, |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 141 | }; |
| 142 | |
Patrick Rudolph | 9ab9db0 | 2018-04-05 09:14:51 +0200 | [diff] [blame] | 143 | static const char *bootmem_range_string(const enum bootmem_type tag) |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 144 | { |
| 145 | int i; |
| 146 | |
| 147 | for (i = 0; i < ARRAY_SIZE(type_strings); i++) { |
| 148 | if (type_strings[i].tag == tag) |
| 149 | return type_strings[i].str; |
| 150 | } |
| 151 | |
| 152 | return "UNKNOWN!"; |
| 153 | } |
| 154 | |
| 155 | void bootmem_dump_ranges(void) |
| 156 | { |
| 157 | int i; |
| 158 | const struct range_entry *r; |
| 159 | |
| 160 | i = 0; |
| 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))); |
| 165 | i++; |
| 166 | } |
| 167 | } |
| 168 | |
Patrick Rudolph | 64049be | 2018-04-20 10:37:51 +0200 | [diff] [blame] | 169 | bool bootmem_walk_os_mem(range_action_t action, void *arg) |
| 170 | { |
| 171 | const struct range_entry *r; |
| 172 | |
| 173 | assert(bootmem_is_initialized()); |
| 174 | |
| 175 | memranges_each_entry(r, &bootmem_os) { |
| 176 | if (!action(r, arg)) |
| 177 | return true; |
| 178 | } |
| 179 | |
| 180 | return false; |
| 181 | } |
| 182 | |
Patrick Rudolph | c653623 | 2018-04-10 09:34:29 +0200 | [diff] [blame] | 183 | bool bootmem_walk(range_action_t action, void *arg) |
| 184 | { |
| 185 | const struct range_entry *r; |
| 186 | |
| 187 | assert(bootmem_is_initialized()); |
| 188 | |
| 189 | memranges_each_entry(r, &bootmem) { |
| 190 | if (!action(r, arg)) |
| 191 | return true; |
| 192 | } |
| 193 | |
| 194 | return false; |
| 195 | } |
| 196 | |
Ting Shen | 0553226 | 2019-01-28 17:22:22 +0800 | [diff] [blame] | 197 | int bootmem_region_targets_type(uint64_t start, uint64_t size, |
| 198 | enum bootmem_type dest_type) |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 199 | { |
| 200 | const struct range_entry *r; |
Ting Shen | 0553226 | 2019-01-28 17:22:22 +0800 | [diff] [blame] | 201 | uint64_t end = start + size; |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 202 | |
Ting Shen | 0553226 | 2019-01-28 17:22:22 +0800 | [diff] [blame] | 203 | memranges_each_entry(r, &bootmem) { |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 204 | /* All further bootmem entries are beyond this range. */ |
| 205 | if (end <= range_entry_base(r)) |
| 206 | break; |
| 207 | |
| 208 | if (start >= range_entry_base(r) && end <= range_entry_end(r)) { |
Ting Shen | 0553226 | 2019-01-28 17:22:22 +0800 | [diff] [blame] | 209 | if (range_entry_tag(r) == dest_type) |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 210 | return 1; |
| 211 | } |
| 212 | } |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | void *bootmem_allocate_buffer(size_t size) |
| 217 | { |
| 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; |
| 222 | resource_t begin; |
| 223 | resource_t end; |
| 224 | |
Aaron Durbin | 4677f6b | 2018-04-03 00:08:12 -0600 | [diff] [blame] | 225 | if (!bootmem_is_initialized()) { |
Wim Vervoorn | 87c5280 | 2019-11-12 11:15:23 +0100 | [diff] [blame] | 226 | printk(BIOS_ERR, "%s: lib uninitialized!\n", __func__); |
Aaron Durbin | 4677f6b | 2018-04-03 00:08:12 -0600 | [diff] [blame] | 227 | return NULL; |
| 228 | } |
| 229 | |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 230 | /* 4KiB alignment. */ |
| 231 | size = ALIGN(size, 4096); |
| 232 | region = NULL; |
| 233 | memranges_each_entry(r, &bootmem) { |
Jan Dabros | 821b1e2 | 2020-07-16 13:45:49 +0200 | [diff] [blame] | 234 | if (range_entry_base(r) >= max_addr) |
| 235 | break; |
| 236 | |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 237 | if (range_entry_size(r) < size) |
| 238 | continue; |
| 239 | |
Patrick Rudolph | 9ab9db0 | 2018-04-05 09:14:51 +0200 | [diff] [blame] | 240 | if (range_entry_tag(r) != BM_MEM_RAM) |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 241 | continue; |
| 242 | |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 243 | end = range_entry_end(r); |
| 244 | if (end > max_addr) |
| 245 | end = max_addr; |
| 246 | |
| 247 | if ((end - range_entry_base(r)) < size) |
| 248 | continue; |
| 249 | |
| 250 | region = r; |
| 251 | } |
| 252 | |
| 253 | if (region == NULL) |
| 254 | return NULL; |
| 255 | |
| 256 | /* region now points to the highest usable region for the given size. */ |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 257 | end = range_entry_end(region); |
| 258 | if (end > max_addr) |
| 259 | end = max_addr; |
| 260 | begin = end - size; |
| 261 | |
Elyes HAOUAS | 0bc9f0b | 2019-12-06 18:07:33 +0100 | [diff] [blame] | 262 | /* Mark buffer as unusable for future buffer use. */ |
Patrick Rudolph | 23d62dd | 2018-04-12 10:36:57 +0200 | [diff] [blame] | 263 | bootmem_add_range(begin, size, BM_MEM_PAYLOAD); |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 264 | |
| 265 | return (void *)(uintptr_t)begin; |
| 266 | } |