Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 2 | * Copyright 2018 The Hafnium Authors. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 3 | * |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style |
| 5 | * license that can be found in the LICENSE file or at |
| 6 | * https://opensource.org/licenses/BSD-3-Clause. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 9 | #include "hf/fdt_handler.h" |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 10 | |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 11 | #include "hf/check.h" |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 12 | #include "hf/cpu.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 13 | #include "hf/dlog.h" |
| 14 | #include "hf/fdt.h" |
| 15 | #include "hf/mm.h" |
Andrew Scull | 8d9e121 | 2019-04-05 13:52:55 +0100 | [diff] [blame] | 16 | #include "hf/std.h" |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 17 | |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 18 | /** |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 19 | * Initializes the FDT struct with the pointer to the FDT data (header) in |
| 20 | * fdt_ptr. |
| 21 | */ |
| 22 | bool fdt_struct_from_ptr(const void *fdt_ptr, struct fdt *fdt) |
| 23 | { |
| 24 | size_t fdt_size; |
| 25 | |
| 26 | if (!fdt_ptr || !fdt) { |
| 27 | return false; |
| 28 | } |
| 29 | |
| 30 | return fdt_size_from_header(fdt_ptr, &fdt_size) && |
| 31 | fdt_init_from_ptr(fdt, fdt_ptr, fdt_size); |
| 32 | } |
| 33 | |
| 34 | /** |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 35 | * Finds the memory region where initrd is stored. |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 36 | */ |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 37 | bool fdt_find_initrd(const struct fdt *fdt, paddr_t *begin, paddr_t *end) |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 38 | { |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 39 | struct fdt_node n; |
Andrew Scull | b401ba3 | 2018-11-09 10:30:54 +0000 | [diff] [blame] | 40 | uint64_t initrd_begin; |
| 41 | uint64_t initrd_end; |
Andrew Scull | 265ada9 | 2018-07-30 15:19:01 +0100 | [diff] [blame] | 42 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 43 | if (!fdt_find_node(fdt, "/chosen", &n)) { |
| 44 | dlog_error("Unable to find '/chosen'\n"); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 45 | return false; |
| 46 | } |
| 47 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 48 | if (!fdt_read_number(&n, FDT_PROP_INITRD_START, &initrd_begin)) { |
| 49 | dlog_error("Unable to read " FDT_PROP_INITRD_START "\n"); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 50 | return false; |
| 51 | } |
| 52 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 53 | if (!fdt_read_number(&n, FDT_PROP_INITRD_END, &initrd_end)) { |
| 54 | dlog_error("Unable to read " FDT_PROP_INITRD_END "\n"); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 55 | return false; |
| 56 | } |
| 57 | |
Andrew Scull | b401ba3 | 2018-11-09 10:30:54 +0000 | [diff] [blame] | 58 | *begin = pa_init(initrd_begin); |
| 59 | *end = pa_init(initrd_end); |
Andrew Scull | 265ada9 | 2018-07-30 15:19:01 +0100 | [diff] [blame] | 60 | |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 61 | return true; |
| 62 | } |
| 63 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 64 | bool fdt_find_cpus(const struct fdt *fdt, cpu_id_t *cpu_ids, size_t *cpu_count) |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 65 | { |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 66 | static const struct string str_cpu = STRING_INIT("cpu"); |
| 67 | struct fdt_node n; |
| 68 | size_t addr_size; |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 69 | |
| 70 | *cpu_count = 0; |
| 71 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 72 | if (!fdt_find_node(fdt, "/cpus", &n)) { |
| 73 | dlog_error("Unable to find '/cpus'\n"); |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 74 | return false; |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 75 | } |
| 76 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 77 | if (!fdt_address_size(&n, &addr_size)) { |
| 78 | return false; |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 79 | } |
| 80 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 81 | if (!fdt_first_child(&n)) { |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 82 | return false; |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | do { |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 86 | struct memiter data; |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 87 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 88 | if (!fdt_read_property(&n, "device_type", &data) || |
| 89 | !string_eq(&str_cpu, &data) || |
| 90 | !fdt_read_property(&n, "reg", &data)) { |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 91 | continue; |
| 92 | } |
| 93 | |
| 94 | /* Get all entries for this CPU. */ |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 95 | while (memiter_size(&data)) { |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 96 | uint64_t value; |
| 97 | |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 98 | if (*cpu_count >= MAX_CPUS) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 99 | dlog_error("Found more than %d CPUs\n", |
| 100 | MAX_CPUS); |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 101 | return false; |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 102 | } |
| 103 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 104 | if (!fdt_parse_number(&data, addr_size, &value)) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 105 | dlog_error("Could not parse CPU id\n"); |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 106 | return false; |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 107 | } |
| 108 | cpu_ids[(*cpu_count)++] = value; |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 109 | } |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 110 | } while (fdt_next_sibling(&n)); |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 111 | |
| 112 | return true; |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 115 | bool fdt_find_memory_ranges(const struct fdt *fdt, |
| 116 | const struct string *device_type, |
Andrew Scull | 6c9a4ab | 2020-01-27 17:09:12 +0000 | [diff] [blame] | 117 | struct mem_range *mem_ranges, |
| 118 | size_t *mem_ranges_count, size_t mem_range_limit) |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 119 | { |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 120 | struct fdt_node n; |
| 121 | size_t addr_size; |
| 122 | size_t size_size; |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 123 | size_t mem_range_index = 0; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 124 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 125 | if (!fdt_find_node(fdt, "/", &n) || !fdt_address_size(&n, &addr_size) || |
| 126 | !fdt_size_size(&n, &size_size)) { |
| 127 | return false; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 128 | } |
| 129 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 130 | /* Look for nodes with the device_type set to `device_type`. */ |
| 131 | if (!fdt_first_child(&n)) { |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 132 | return false; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | do { |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 136 | struct memiter data; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 137 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 138 | if (!fdt_read_property(&n, "device_type", &data) || |
| 139 | !string_eq(device_type, &data) || |
| 140 | !fdt_read_property(&n, "reg", &data)) { |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 141 | continue; |
| 142 | } |
| 143 | |
| 144 | /* Traverse all memory ranges within this node. */ |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 145 | while (memiter_size(&data)) { |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 146 | uintpaddr_t addr; |
| 147 | size_t len; |
| 148 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 149 | CHECK(fdt_parse_number(&data, addr_size, &addr)); |
| 150 | CHECK(fdt_parse_number(&data, size_size, &len)); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 151 | |
Andrew Scull | 6c9a4ab | 2020-01-27 17:09:12 +0000 | [diff] [blame] | 152 | if (mem_range_index < mem_range_limit) { |
| 153 | mem_ranges[mem_range_index].begin = |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 154 | pa_init(addr); |
Andrew Scull | 6c9a4ab | 2020-01-27 17:09:12 +0000 | [diff] [blame] | 155 | mem_ranges[mem_range_index].end = |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 156 | pa_init(addr + len); |
| 157 | ++mem_range_index; |
| 158 | } else { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 159 | dlog_error( |
Andrew Scull | 6c9a4ab | 2020-01-27 17:09:12 +0000 | [diff] [blame] | 160 | "Found %s range %u in FDT but only %u " |
| 161 | "supported, ignoring additional range " |
| 162 | "of size %u.\n", |
| 163 | string_data(device_type), |
| 164 | mem_range_index, mem_range_limit, len); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 165 | } |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 166 | } |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 167 | } while (fdt_next_sibling(&n)); |
Andrew Scull | 6c9a4ab | 2020-01-27 17:09:12 +0000 | [diff] [blame] | 168 | *mem_ranges_count = mem_range_index; |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 169 | |
| 170 | return true; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 171 | } |
| 172 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 173 | bool fdt_map(struct fdt *fdt, struct mm_stage1_locked stage1_locked, |
| 174 | paddr_t fdt_addr, struct mpool *ppool) |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 175 | { |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 176 | const void *fdt_ptr; |
| 177 | size_t fdt_len; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 178 | |
| 179 | /* Map the fdt header in. */ |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 180 | fdt_ptr = mm_identity_map(stage1_locked, fdt_addr, |
| 181 | pa_add(fdt_addr, FDT_V17_HEADER_SIZE), |
| 182 | MM_MODE_R, ppool); |
| 183 | if (!fdt_ptr) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 184 | dlog_error("Unable to map FDT header.\n"); |
Andrew Scull | b401ba3 | 2018-11-09 10:30:54 +0000 | [diff] [blame] | 185 | return NULL; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 186 | } |
| 187 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 188 | if (!fdt_size_from_header(fdt_ptr, &fdt_len)) { |
| 189 | dlog_error("FDT failed header validation.\n"); |
Andrew Scull | b401ba3 | 2018-11-09 10:30:54 +0000 | [diff] [blame] | 190 | goto fail; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /* Map the rest of the fdt in. */ |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 194 | fdt_ptr = mm_identity_map(stage1_locked, fdt_addr, |
| 195 | pa_add(fdt_addr, fdt_len), MM_MODE_R, ppool); |
| 196 | if (!fdt_ptr) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 197 | dlog_error("Unable to map full FDT.\n"); |
Andrew Scull | b401ba3 | 2018-11-09 10:30:54 +0000 | [diff] [blame] | 198 | goto fail; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 199 | } |
| 200 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 201 | if (!fdt_init_from_ptr(fdt, fdt_ptr, fdt_len)) { |
| 202 | dlog_error("FDT failed validation.\n"); |
| 203 | goto fail_full; |
| 204 | } |
| 205 | |
| 206 | return true; |
| 207 | |
| 208 | fail_full: |
| 209 | mm_unmap(stage1_locked, fdt_addr, pa_add(fdt_addr, fdt_len), ppool); |
| 210 | return false; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 211 | |
Andrew Scull | b401ba3 | 2018-11-09 10:30:54 +0000 | [diff] [blame] | 212 | fail: |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 213 | mm_unmap(stage1_locked, fdt_addr, pa_add(fdt_addr, FDT_V17_HEADER_SIZE), |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 214 | ppool); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 215 | return false; |
| 216 | } |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 217 | |
| 218 | bool fdt_unmap(struct fdt *fdt, struct mm_stage1_locked stage1_locked, |
| 219 | struct mpool *ppool) |
| 220 | { |
| 221 | paddr_t begin = pa_from_va(va_from_ptr(fdt_base(fdt))); |
| 222 | paddr_t end = pa_add(begin, fdt_size(fdt)); |
| 223 | |
| 224 | if (!mm_unmap(stage1_locked, begin, end, ppool)) { |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | /* Invalidate pointer to the buffer. */ |
| 229 | fdt_fini(fdt); |
| 230 | return true; |
| 231 | } |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 232 | |
| 233 | /** |
| 234 | * Gets the size of the first memory range from the FDT into size. |
| 235 | * |
| 236 | * The test framework expects the address space to be contiguous, therefore |
| 237 | * gets the size of the first memory range, if there is more than one range. |
| 238 | */ |
| 239 | bool fdt_get_memory_size(const struct fdt *fdt, size_t *size) |
| 240 | { |
| 241 | const struct string memory_device_type = STRING_INIT("memory"); |
| 242 | struct mem_range mem_range; |
| 243 | size_t mem_ranges_count; |
| 244 | |
| 245 | if (!fdt || !size || |
| 246 | !fdt_find_memory_ranges(fdt, &memory_device_type, &mem_range, |
| 247 | &mem_ranges_count, 1)) { |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | if (mem_ranges_count < 1) { |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | *size = pa_difference(mem_range.begin, mem_range.end); |
| 256 | |
| 257 | return true; |
| 258 | } |