blob: 900f766ce86f3c385826fb832b7d4c5581ce1368 [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01003 *
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 Scull18c78fc2018-08-20 12:57:41 +010017#include "hf/fdt_handler.h"
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010018
Andrew Scull18c78fc2018-08-20 12:57:41 +010019#include "hf/boot_params.h"
David Brazdil7a462ec2019-08-15 12:27:47 +010020#include "hf/check.h"
Andrew Scullbb3ab6c2018-11-26 20:38:49 +000021#include "hf/cpu.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010022#include "hf/dlog.h"
23#include "hf/fdt.h"
Andrew Scull5991ec92018-10-08 14:55:02 +010024#include "hf/layout.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010025#include "hf/mm.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010026#include "hf/std.h"
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010027
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010028static bool fdt_read_number(const struct fdt_node *node, const char *name,
29 uint64_t *value)
30{
31 const char *data;
32 uint32_t size;
33
34 if (!fdt_read_property(node, name, &data, &size)) {
35 return false;
36 }
37
38 switch (size) {
39 case sizeof(uint32_t):
40 case sizeof(uint64_t):
David Brazdil7a462ec2019-08-15 12:27:47 +010041 CHECK(fdt_parse_number(data, size, value));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010042 break;
43
44 default:
45 return false;
46 }
47
48 return true;
49}
50
51static bool fdt_write_number(struct fdt_node *node, const char *name,
52 uint64_t value)
53{
54 const char *data;
55 uint32_t size;
56 union {
57 volatile uint64_t v;
58 char a[8];
59 } t;
60
61 if (!fdt_read_property(node, name, &data, &size)) {
62 return false;
63 }
64
65 switch (size) {
66 case sizeof(uint32_t):
67 *(uint32_t *)data = be32toh(value);
68 break;
69
70 case sizeof(uint64_t):
71 t.v = be64toh(value);
Andrew Sculla1aa2ba2019-04-05 11:49:02 +010072 memcpy_s((void *)data, size, t.a, sizeof(uint64_t));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010073 break;
74
75 default:
76 return false;
77 }
78
79 return true;
80}
81
82/**
Andrew Walbranfb88f2c2018-09-18 15:23:49 +010083 * Finds the memory region where initrd is stored, and updates the fdt node
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010084 * cursor to the node called "chosen".
85 */
Andrew Scullb401ba32018-11-09 10:30:54 +000086bool fdt_find_initrd(struct fdt_node *n, paddr_t *begin, paddr_t *end)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010087{
Andrew Scullb401ba32018-11-09 10:30:54 +000088 uint64_t initrd_begin;
89 uint64_t initrd_end;
Andrew Scull265ada92018-07-30 15:19:01 +010090
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010091 if (!fdt_find_child(n, "chosen")) {
92 dlog("Unable to find 'chosen'\n");
93 return false;
94 }
95
Andrew Scullb401ba32018-11-09 10:30:54 +000096 if (!fdt_read_number(n, "linux,initrd-start", &initrd_begin)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010097 dlog("Unable to read linux,initrd-start\n");
98 return false;
99 }
100
Andrew Scullb401ba32018-11-09 10:30:54 +0000101 if (!fdt_read_number(n, "linux,initrd-end", &initrd_end)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100102 dlog("Unable to read linux,initrd-end\n");
103 return false;
104 }
105
Andrew Scullb401ba32018-11-09 10:30:54 +0000106 *begin = pa_init(initrd_begin);
107 *end = pa_init(initrd_end);
Andrew Scull265ada92018-07-30 15:19:01 +0100108
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100109 return true;
110}
111
Andrew Walbran4d3fa282019-06-26 13:31:15 +0100112void fdt_find_cpus(const struct fdt_node *root, cpu_id_t *cpu_ids,
Andrew Scullbb3ab6c2018-11-26 20:38:49 +0000113 size_t *cpu_count)
114{
115 struct fdt_node n = *root;
116 const char *name;
117 uint64_t address_size;
118
119 *cpu_count = 0;
120
121 if (!fdt_find_child(&n, "cpus")) {
122 dlog("Unable to find 'cpus'\n");
123 return;
124 }
125
126 if (fdt_read_number(&n, "#address-cells", &address_size)) {
127 address_size *= sizeof(uint32_t);
128 } else {
129 address_size = sizeof(uint32_t);
130 }
131
132 if (!fdt_first_child(&n, &name)) {
133 return;
134 }
135
136 do {
137 const char *data;
138 uint32_t size;
139
140 if (!fdt_read_property(&n, "device_type", &data, &size) ||
141 size != sizeof("cpu") ||
142 memcmp(data, "cpu", sizeof("cpu")) != 0 ||
143 !fdt_read_property(&n, "reg", &data, &size)) {
144 continue;
145 }
146
147 /* Get all entries for this CPU. */
148 while (size >= address_size) {
David Brazdil7a462ec2019-08-15 12:27:47 +0100149 uint64_t value;
150
Andrew Scullbb3ab6c2018-11-26 20:38:49 +0000151 if (*cpu_count >= MAX_CPUS) {
152 dlog("Found more than %d CPUs\n", MAX_CPUS);
153 return;
154 }
155
David Brazdil7a462ec2019-08-15 12:27:47 +0100156 if (!fdt_parse_number(data, address_size, &value)) {
157 dlog("Could not parse CPU id\n");
158 return;
159 }
160 cpu_ids[(*cpu_count)++] = value;
Andrew Scullbb3ab6c2018-11-26 20:38:49 +0000161
162 size -= address_size;
163 data += address_size;
164 }
165 } while (fdt_next_sibling(&n, &name));
166}
167
Andrew Scullb401ba32018-11-09 10:30:54 +0000168void fdt_find_memory_ranges(const struct fdt_node *root, struct boot_params *p)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100169{
170 struct fdt_node n = *root;
171 const char *name;
172 uint64_t address_size;
173 uint64_t size_size;
174 uint64_t entry_size;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100175 size_t mem_range_index = 0;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100176
177 /* Get the sizes of memory range addresses and sizes. */
178 if (fdt_read_number(&n, "#address-cells", &address_size)) {
179 address_size *= sizeof(uint32_t);
180 } else {
181 address_size = sizeof(uint32_t);
182 }
183
184 if (fdt_read_number(&n, "#size-cells", &size_size)) {
185 size_size *= sizeof(uint32_t);
186 } else {
187 size_size = sizeof(uint32_t);
188 }
189
190 entry_size = address_size + size_size;
191
192 /* Look for nodes with the device_type set to "memory". */
193 if (!fdt_first_child(&n, &name)) {
194 return;
195 }
196
197 do {
198 const char *data;
199 uint32_t size;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000200
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100201 if (!fdt_read_property(&n, "device_type", &data, &size) ||
202 size != sizeof("memory") ||
203 memcmp(data, "memory", sizeof("memory")) != 0 ||
204 !fdt_read_property(&n, "reg", &data, &size)) {
205 continue;
206 }
207
208 /* Traverse all memory ranges within this node. */
209 while (size >= entry_size) {
David Brazdil7a462ec2019-08-15 12:27:47 +0100210 uintpaddr_t addr;
211 size_t len;
212
213 CHECK(fdt_parse_number(data, address_size, &addr));
214 CHECK(fdt_parse_number(data + address_size, size_size,
215 &len));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100216
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100217 if (mem_range_index < MAX_MEM_RANGES) {
218 p->mem_ranges[mem_range_index].begin =
219 pa_init(addr);
220 p->mem_ranges[mem_range_index].end =
221 pa_init(addr + len);
222 ++mem_range_index;
223 } else {
224 dlog("Found memory range %u in FDT but only "
225 "%u supported, ignoring additional range "
226 "of size %u.\n",
227 mem_range_index, MAX_MEM_RANGES, len);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100228 }
229
230 size -= entry_size;
231 data += entry_size;
232 }
233 } while (fdt_next_sibling(&n, &name));
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100234 p->mem_ranges_count = mem_range_index;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100235
236 /* TODO: Check for "reserved-memory" nodes. */
237}
238
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100239struct fdt_header *fdt_map(struct mm_stage1_locked stage1_locked,
240 paddr_t fdt_addr, struct fdt_node *n,
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000241 struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100242{
Andrew Scull265ada92018-07-30 15:19:01 +0100243 struct fdt_header *fdt;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100244
245 /* Map the fdt header in. */
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100246 fdt = mm_identity_map(stage1_locked, fdt_addr,
247 pa_add(fdt_addr, fdt_header_size()), MM_MODE_R,
248 ppool);
Andrew Scull80871322018-08-06 12:04:09 +0100249 if (!fdt) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100250 dlog("Unable to map FDT header.\n");
Andrew Scullb401ba32018-11-09 10:30:54 +0000251 return NULL;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100252 }
253
Andrew Scullb401ba32018-11-09 10:30:54 +0000254 if (!fdt_root_node(n, fdt)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100255 dlog("FDT failed validation.\n");
Andrew Scullb401ba32018-11-09 10:30:54 +0000256 goto fail;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100257 }
258
259 /* Map the rest of the fdt in. */
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100260 fdt = mm_identity_map(stage1_locked, fdt_addr,
261 pa_add(fdt_addr, fdt_total_size(fdt)), MM_MODE_R,
262 ppool);
Andrew Scull80871322018-08-06 12:04:09 +0100263 if (!fdt) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100264 dlog("Unable to map full FDT.\n");
Andrew Scullb401ba32018-11-09 10:30:54 +0000265 goto fail;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100266 }
267
Andrew Scullb401ba32018-11-09 10:30:54 +0000268 return fdt;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100269
Andrew Scullb401ba32018-11-09 10:30:54 +0000270fail:
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100271 mm_unmap(stage1_locked, fdt_addr, pa_add(fdt_addr, fdt_header_size()),
272 ppool);
Andrew Scullb401ba32018-11-09 10:30:54 +0000273 return NULL;
274}
275
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100276bool fdt_unmap(struct mm_stage1_locked stage1_locked, struct fdt_header *fdt,
277 struct mpool *ppool)
Andrew Scullb401ba32018-11-09 10:30:54 +0000278{
279 paddr_t fdt_addr = pa_from_va(va_from_ptr(fdt));
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000280
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100281 return mm_unmap(stage1_locked, fdt_addr,
282 pa_add(fdt_addr, fdt_total_size(fdt)), ppool);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100283}
284
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100285bool fdt_patch(struct mm_stage1_locked stage1_locked, paddr_t fdt_addr,
286 struct boot_params_update *p, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100287{
Andrew Scull265ada92018-07-30 15:19:01 +0100288 struct fdt_header *fdt;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100289 struct fdt_node n;
290 bool ret = false;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100291 size_t i;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100292
293 /* Map the fdt header in. */
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100294 fdt = mm_identity_map(stage1_locked, fdt_addr,
295 pa_add(fdt_addr, fdt_header_size()), MM_MODE_R,
296 ppool);
Andrew Scull80871322018-08-06 12:04:09 +0100297 if (!fdt) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100298 dlog("Unable to map FDT header.\n");
299 return false;
300 }
301
302 if (!fdt_root_node(&n, fdt)) {
303 dlog("FDT failed validation.\n");
304 goto err_unmap_fdt_header;
305 }
306
307 /* Map the fdt (+ a page) in r/w mode in preparation for updating it. */
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100308 fdt = mm_identity_map(stage1_locked, fdt_addr,
Andrew Scull80871322018-08-06 12:04:09 +0100309 pa_add(fdt_addr, fdt_total_size(fdt) + PAGE_SIZE),
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000310 MM_MODE_R | MM_MODE_W, ppool);
Andrew Scull80871322018-08-06 12:04:09 +0100311 if (!fdt) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100312 dlog("Unable to map FDT in r/w mode.\n");
313 goto err_unmap_fdt_header;
314 }
315
316 if (!fdt_find_child(&n, "")) {
317 dlog("Unable to find FDT root node.\n");
318 goto out_unmap_fdt;
319 }
320
321 if (!fdt_find_child(&n, "chosen")) {
322 dlog("Unable to find 'chosen'\n");
323 goto out_unmap_fdt;
324 }
325
326 /* Patch FDT to point to new ramdisk. */
Andrew Scull265ada92018-07-30 15:19:01 +0100327 if (!fdt_write_number(&n, "linux,initrd-start",
328 pa_addr(p->initrd_begin))) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100329 dlog("Unable to write linux,initrd-start\n");
330 goto out_unmap_fdt;
331 }
332
Andrew Scull265ada92018-07-30 15:19:01 +0100333 if (!fdt_write_number(&n, "linux,initrd-end", pa_addr(p->initrd_end))) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100334 dlog("Unable to write linux,initrd-end\n");
335 goto out_unmap_fdt;
336 }
337
Andrew Walbran401ff562019-04-09 15:43:31 +0100338 /*
339 * Patch FDT to reserve hypervisor memory so the primary VM doesn't try
340 * to use it.
341 */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000342 fdt_add_mem_reservation(
Andrew Walbran401ff562019-04-09 15:43:31 +0100343 fdt, pa_addr(layout_text_begin()),
Andrew Walbran2cb43392019-04-17 12:52:45 +0100344 pa_difference(layout_text_begin(), layout_text_end()));
Andrew Walbran401ff562019-04-09 15:43:31 +0100345 fdt_add_mem_reservation(
346 fdt, pa_addr(layout_rodata_begin()),
Andrew Walbran2cb43392019-04-17 12:52:45 +0100347 pa_difference(layout_rodata_begin(), layout_rodata_end()));
Andrew Walbran401ff562019-04-09 15:43:31 +0100348 fdt_add_mem_reservation(
349 fdt, pa_addr(layout_data_begin()),
Andrew Walbran2cb43392019-04-17 12:52:45 +0100350 pa_difference(layout_data_begin(), layout_data_end()));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100351
Andrew Walbran401ff562019-04-09 15:43:31 +0100352 /* Patch FDT to reserve memory for secondary VMs. */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100353 for (i = 0; i < p->reserved_ranges_count; ++i) {
354 fdt_add_mem_reservation(
355 fdt, pa_addr(p->reserved_ranges[i].begin),
356 pa_addr(p->reserved_ranges[i].end) -
357 pa_addr(p->reserved_ranges[i].begin));
358 }
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100359
360 ret = true;
361
362out_unmap_fdt:
363 /* Unmap FDT. */
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100364 if (!mm_unmap(stage1_locked, fdt_addr,
Andrew Scullda241972019-01-05 18:17:48 +0000365 pa_add(fdt_addr, fdt_total_size(fdt) + PAGE_SIZE),
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000366 ppool)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100367 dlog("Unable to unmap writable FDT.\n");
368 return false;
369 }
370 return ret;
371
372err_unmap_fdt_header:
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100373 mm_unmap(stage1_locked, fdt_addr, pa_add(fdt_addr, fdt_header_size()),
374 ppool);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100375 return false;
376}