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 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 17 | #include "hf/fdt_handler.h" |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 18 | |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 19 | #include "hf/check.h" |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 20 | #include "hf/cpu.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 21 | #include "hf/dlog.h" |
| 22 | #include "hf/fdt.h" |
| 23 | #include "hf/mm.h" |
Andrew Scull | 8d9e121 | 2019-04-05 13:52:55 +0100 | [diff] [blame] | 24 | #include "hf/std.h" |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 25 | |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 26 | /** |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 27 | * Finds the memory region where initrd is stored. |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 28 | */ |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 29 | 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] | 30 | { |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 31 | struct fdt_node n; |
Andrew Scull | b401ba3 | 2018-11-09 10:30:54 +0000 | [diff] [blame] | 32 | uint64_t initrd_begin; |
| 33 | uint64_t initrd_end; |
Andrew Scull | 265ada9 | 2018-07-30 15:19:01 +0100 | [diff] [blame] | 34 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 35 | if (!fdt_find_node(fdt, "/chosen", &n)) { |
| 36 | dlog_error("Unable to find '/chosen'\n"); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 37 | return false; |
| 38 | } |
| 39 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 40 | if (!fdt_read_number(&n, FDT_PROP_INITRD_START, &initrd_begin)) { |
| 41 | dlog_error("Unable to read " FDT_PROP_INITRD_START "\n"); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 42 | return false; |
| 43 | } |
| 44 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 45 | if (!fdt_read_number(&n, FDT_PROP_INITRD_END, &initrd_end)) { |
| 46 | dlog_error("Unable to read " FDT_PROP_INITRD_END "\n"); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 47 | return false; |
| 48 | } |
| 49 | |
Andrew Scull | b401ba3 | 2018-11-09 10:30:54 +0000 | [diff] [blame] | 50 | *begin = pa_init(initrd_begin); |
| 51 | *end = pa_init(initrd_end); |
Andrew Scull | 265ada9 | 2018-07-30 15:19:01 +0100 | [diff] [blame] | 52 | |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 53 | return true; |
| 54 | } |
| 55 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 56 | 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] | 57 | { |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 58 | static const struct string str_cpu = STRING_INIT("cpu"); |
| 59 | struct fdt_node n; |
| 60 | size_t addr_size; |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 61 | |
| 62 | *cpu_count = 0; |
| 63 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 64 | if (!fdt_find_node(fdt, "/cpus", &n)) { |
| 65 | dlog_error("Unable to find '/cpus'\n"); |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 66 | return false; |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 67 | } |
| 68 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 69 | if (!fdt_address_size(&n, &addr_size)) { |
| 70 | return false; |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 71 | } |
| 72 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 73 | if (!fdt_first_child(&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 | |
| 77 | do { |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 78 | struct memiter data; |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 79 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 80 | if (!fdt_read_property(&n, "device_type", &data) || |
| 81 | !string_eq(&str_cpu, &data) || |
| 82 | !fdt_read_property(&n, "reg", &data)) { |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 83 | continue; |
| 84 | } |
| 85 | |
| 86 | /* Get all entries for this CPU. */ |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 87 | while (memiter_size(&data)) { |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 88 | uint64_t value; |
| 89 | |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 90 | if (*cpu_count >= MAX_CPUS) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 91 | dlog_error("Found more than %d CPUs\n", |
| 92 | MAX_CPUS); |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 93 | return false; |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 94 | } |
| 95 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 96 | if (!fdt_parse_number(&data, addr_size, &value)) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 97 | dlog_error("Could not parse CPU id\n"); |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 98 | return false; |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 99 | } |
| 100 | cpu_ids[(*cpu_count)++] = value; |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 101 | } |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 102 | } while (fdt_next_sibling(&n)); |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 103 | |
| 104 | return true; |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 105 | } |
| 106 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 107 | bool fdt_find_memory_ranges(const struct fdt *fdt, struct string *device_type, |
Andrew Scull | 6c9a4ab | 2020-01-27 17:09:12 +0000 | [diff] [blame] | 108 | struct mem_range *mem_ranges, |
| 109 | size_t *mem_ranges_count, size_t mem_range_limit) |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 110 | { |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 111 | struct fdt_node n; |
| 112 | size_t addr_size; |
| 113 | size_t size_size; |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 114 | size_t mem_range_index = 0; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 115 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 116 | if (!fdt_find_node(fdt, "/", &n) || !fdt_address_size(&n, &addr_size) || |
| 117 | !fdt_size_size(&n, &size_size)) { |
| 118 | return false; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 119 | } |
| 120 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 121 | /* Look for nodes with the device_type set to `device_type`. */ |
| 122 | if (!fdt_first_child(&n)) { |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 123 | return false; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | do { |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 127 | struct memiter data; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 128 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 129 | if (!fdt_read_property(&n, "device_type", &data) || |
| 130 | !string_eq(device_type, &data) || |
| 131 | !fdt_read_property(&n, "reg", &data)) { |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 132 | continue; |
| 133 | } |
| 134 | |
| 135 | /* Traverse all memory ranges within this node. */ |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 136 | while (memiter_size(&data)) { |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 137 | uintpaddr_t addr; |
| 138 | size_t len; |
| 139 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 140 | CHECK(fdt_parse_number(&data, addr_size, &addr)); |
| 141 | CHECK(fdt_parse_number(&data, size_size, &len)); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 142 | |
Andrew Scull | 6c9a4ab | 2020-01-27 17:09:12 +0000 | [diff] [blame] | 143 | if (mem_range_index < mem_range_limit) { |
| 144 | mem_ranges[mem_range_index].begin = |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 145 | pa_init(addr); |
Andrew Scull | 6c9a4ab | 2020-01-27 17:09:12 +0000 | [diff] [blame] | 146 | mem_ranges[mem_range_index].end = |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 147 | pa_init(addr + len); |
| 148 | ++mem_range_index; |
| 149 | } else { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 150 | dlog_error( |
Andrew Scull | 6c9a4ab | 2020-01-27 17:09:12 +0000 | [diff] [blame] | 151 | "Found %s range %u in FDT but only %u " |
| 152 | "supported, ignoring additional range " |
| 153 | "of size %u.\n", |
| 154 | string_data(device_type), |
| 155 | mem_range_index, mem_range_limit, len); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 156 | } |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 157 | } |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 158 | } while (fdt_next_sibling(&n)); |
Andrew Scull | 6c9a4ab | 2020-01-27 17:09:12 +0000 | [diff] [blame] | 159 | *mem_ranges_count = mem_range_index; |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 160 | |
| 161 | return true; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 162 | } |
| 163 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 164 | bool fdt_map(struct fdt *fdt, struct mm_stage1_locked stage1_locked, |
| 165 | paddr_t fdt_addr, struct mpool *ppool) |
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 | const void *fdt_ptr; |
| 168 | size_t fdt_len; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 169 | |
| 170 | /* Map the fdt header in. */ |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 171 | fdt_ptr = mm_identity_map(stage1_locked, fdt_addr, |
| 172 | pa_add(fdt_addr, FDT_V17_HEADER_SIZE), |
| 173 | MM_MODE_R, ppool); |
| 174 | if (!fdt_ptr) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 175 | dlog_error("Unable to map FDT header.\n"); |
Andrew Scull | b401ba3 | 2018-11-09 10:30:54 +0000 | [diff] [blame] | 176 | return NULL; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 177 | } |
| 178 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 179 | if (!fdt_size_from_header(fdt_ptr, &fdt_len)) { |
| 180 | dlog_error("FDT failed header validation.\n"); |
Andrew Scull | b401ba3 | 2018-11-09 10:30:54 +0000 | [diff] [blame] | 181 | goto fail; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /* Map the rest of the fdt in. */ |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 185 | fdt_ptr = mm_identity_map(stage1_locked, fdt_addr, |
| 186 | pa_add(fdt_addr, fdt_len), MM_MODE_R, ppool); |
| 187 | if (!fdt_ptr) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 188 | dlog_error("Unable to map full FDT.\n"); |
Andrew Scull | b401ba3 | 2018-11-09 10:30:54 +0000 | [diff] [blame] | 189 | goto fail; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 190 | } |
| 191 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 192 | if (!fdt_init_from_ptr(fdt, fdt_ptr, fdt_len)) { |
| 193 | dlog_error("FDT failed validation.\n"); |
| 194 | goto fail_full; |
| 195 | } |
| 196 | |
| 197 | return true; |
| 198 | |
| 199 | fail_full: |
| 200 | mm_unmap(stage1_locked, fdt_addr, pa_add(fdt_addr, fdt_len), ppool); |
| 201 | return false; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 202 | |
Andrew Scull | b401ba3 | 2018-11-09 10:30:54 +0000 | [diff] [blame] | 203 | fail: |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 204 | 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] | 205 | ppool); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 206 | return false; |
| 207 | } |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 208 | |
| 209 | bool fdt_unmap(struct fdt *fdt, struct mm_stage1_locked stage1_locked, |
| 210 | struct mpool *ppool) |
| 211 | { |
| 212 | paddr_t begin = pa_from_va(va_from_ptr(fdt_base(fdt))); |
| 213 | paddr_t end = pa_add(begin, fdt_size(fdt)); |
| 214 | |
| 215 | if (!mm_unmap(stage1_locked, begin, end, ppool)) { |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | /* Invalidate pointer to the buffer. */ |
| 220 | fdt_fini(fdt); |
| 221 | return true; |
| 222 | } |