blob: 057a2ca96fad72389e53d04207330d17459dca81 [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/**
David Brazdil0dbb41f2019-09-09 18:03:35 +010083 * Finds the memory region where initrd is stored.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010084 */
David Brazdil0dbb41f2019-09-09 18:03:35 +010085bool fdt_find_initrd(const struct fdt_node *root, paddr_t *begin, paddr_t *end)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010086{
David Brazdil0dbb41f2019-09-09 18:03:35 +010087 struct fdt_node n = *root;
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
David Brazdil0dbb41f2019-09-09 18:03:35 +010091 if (!fdt_find_child(&n, "chosen")) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010092 dlog("Unable to find 'chosen'\n");
93 return false;
94 }
95
David Brazdil0dbb41f2019-09-09 18:03:35 +010096 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
David Brazdil0dbb41f2019-09-09 18:03:35 +0100101 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
David Brazdil0dbb41f2019-09-09 18:03:35 +0100112bool 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");
David Brazdil0dbb41f2019-09-09 18:03:35 +0100123 return false;
Andrew Scullbb3ab6c2018-11-26 20:38:49 +0000124 }
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)) {
David Brazdil0dbb41f2019-09-09 18:03:35 +0100133 return false;
Andrew Scullbb3ab6c2018-11-26 20:38:49 +0000134 }
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);
David Brazdil0dbb41f2019-09-09 18:03:35 +0100153 return false;
Andrew Scullbb3ab6c2018-11-26 20:38:49 +0000154 }
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");
David Brazdil0dbb41f2019-09-09 18:03:35 +0100158 return false;
David Brazdil7a462ec2019-08-15 12:27:47 +0100159 }
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));
David Brazdil0dbb41f2019-09-09 18:03:35 +0100166
167 return true;
Andrew Scullbb3ab6c2018-11-26 20:38:49 +0000168}
169
David Brazdil0dbb41f2019-09-09 18:03:35 +0100170bool fdt_find_memory_ranges(const struct fdt_node *root, struct boot_params *p)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100171{
172 struct fdt_node n = *root;
173 const char *name;
174 uint64_t address_size;
175 uint64_t size_size;
176 uint64_t entry_size;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100177 size_t mem_range_index = 0;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100178
179 /* Get the sizes of memory range addresses and sizes. */
180 if (fdt_read_number(&n, "#address-cells", &address_size)) {
181 address_size *= sizeof(uint32_t);
182 } else {
183 address_size = sizeof(uint32_t);
184 }
185
186 if (fdt_read_number(&n, "#size-cells", &size_size)) {
187 size_size *= sizeof(uint32_t);
188 } else {
189 size_size = sizeof(uint32_t);
190 }
191
192 entry_size = address_size + size_size;
193
194 /* Look for nodes with the device_type set to "memory". */
195 if (!fdt_first_child(&n, &name)) {
David Brazdil0dbb41f2019-09-09 18:03:35 +0100196 return false;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100197 }
198
199 do {
200 const char *data;
201 uint32_t size;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000202
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100203 if (!fdt_read_property(&n, "device_type", &data, &size) ||
204 size != sizeof("memory") ||
205 memcmp(data, "memory", sizeof("memory")) != 0 ||
206 !fdt_read_property(&n, "reg", &data, &size)) {
207 continue;
208 }
209
210 /* Traverse all memory ranges within this node. */
211 while (size >= entry_size) {
David Brazdil7a462ec2019-08-15 12:27:47 +0100212 uintpaddr_t addr;
213 size_t len;
214
215 CHECK(fdt_parse_number(data, address_size, &addr));
216 CHECK(fdt_parse_number(data + address_size, size_size,
217 &len));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100218
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100219 if (mem_range_index < MAX_MEM_RANGES) {
220 p->mem_ranges[mem_range_index].begin =
221 pa_init(addr);
222 p->mem_ranges[mem_range_index].end =
223 pa_init(addr + len);
224 ++mem_range_index;
225 } else {
226 dlog("Found memory range %u in FDT but only "
227 "%u supported, ignoring additional range "
228 "of size %u.\n",
229 mem_range_index, MAX_MEM_RANGES, len);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100230 }
231
232 size -= entry_size;
233 data += entry_size;
234 }
235 } while (fdt_next_sibling(&n, &name));
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100236 p->mem_ranges_count = mem_range_index;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100237
238 /* TODO: Check for "reserved-memory" nodes. */
David Brazdil0dbb41f2019-09-09 18:03:35 +0100239
240 return true;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100241}
242
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100243struct fdt_header *fdt_map(struct mm_stage1_locked stage1_locked,
244 paddr_t fdt_addr, struct fdt_node *n,
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000245 struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100246{
Andrew Scull265ada92018-07-30 15:19:01 +0100247 struct fdt_header *fdt;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100248
249 /* Map the fdt header in. */
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100250 fdt = mm_identity_map(stage1_locked, fdt_addr,
251 pa_add(fdt_addr, fdt_header_size()), MM_MODE_R,
252 ppool);
Andrew Scull80871322018-08-06 12:04:09 +0100253 if (!fdt) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100254 dlog("Unable to map FDT header.\n");
Andrew Scullb401ba32018-11-09 10:30:54 +0000255 return NULL;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100256 }
257
Andrew Scullb401ba32018-11-09 10:30:54 +0000258 if (!fdt_root_node(n, fdt)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100259 dlog("FDT failed validation.\n");
Andrew Scullb401ba32018-11-09 10:30:54 +0000260 goto fail;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100261 }
262
263 /* Map the rest of the fdt in. */
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100264 fdt = mm_identity_map(stage1_locked, fdt_addr,
265 pa_add(fdt_addr, fdt_total_size(fdt)), MM_MODE_R,
266 ppool);
Andrew Scull80871322018-08-06 12:04:09 +0100267 if (!fdt) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100268 dlog("Unable to map full FDT.\n");
Andrew Scullb401ba32018-11-09 10:30:54 +0000269 goto fail;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100270 }
271
Andrew Scullb401ba32018-11-09 10:30:54 +0000272 return fdt;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100273
Andrew Scullb401ba32018-11-09 10:30:54 +0000274fail:
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100275 mm_unmap(stage1_locked, fdt_addr, pa_add(fdt_addr, fdt_header_size()),
276 ppool);
Andrew Scullb401ba32018-11-09 10:30:54 +0000277 return NULL;
278}
279
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100280bool fdt_unmap(struct mm_stage1_locked stage1_locked, struct fdt_header *fdt,
281 struct mpool *ppool)
Andrew Scullb401ba32018-11-09 10:30:54 +0000282{
283 paddr_t fdt_addr = pa_from_va(va_from_ptr(fdt));
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000284
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100285 return mm_unmap(stage1_locked, fdt_addr,
286 pa_add(fdt_addr, fdt_total_size(fdt)), ppool);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100287}
288
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100289bool fdt_patch(struct mm_stage1_locked stage1_locked, paddr_t fdt_addr,
290 struct boot_params_update *p, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100291{
Andrew Scull265ada92018-07-30 15:19:01 +0100292 struct fdt_header *fdt;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100293 struct fdt_node n;
294 bool ret = false;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100295 size_t i;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100296
297 /* Map the fdt header in. */
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100298 fdt = mm_identity_map(stage1_locked, fdt_addr,
299 pa_add(fdt_addr, fdt_header_size()), MM_MODE_R,
300 ppool);
Andrew Scull80871322018-08-06 12:04:09 +0100301 if (!fdt) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100302 dlog("Unable to map FDT header.\n");
303 return false;
304 }
305
306 if (!fdt_root_node(&n, fdt)) {
307 dlog("FDT failed validation.\n");
308 goto err_unmap_fdt_header;
309 }
310
311 /* Map the fdt (+ a page) in r/w mode in preparation for updating it. */
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100312 fdt = mm_identity_map(stage1_locked, fdt_addr,
Andrew Scull80871322018-08-06 12:04:09 +0100313 pa_add(fdt_addr, fdt_total_size(fdt) + PAGE_SIZE),
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000314 MM_MODE_R | MM_MODE_W, ppool);
Andrew Scull80871322018-08-06 12:04:09 +0100315 if (!fdt) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100316 dlog("Unable to map FDT in r/w mode.\n");
317 goto err_unmap_fdt_header;
318 }
319
320 if (!fdt_find_child(&n, "")) {
321 dlog("Unable to find FDT root node.\n");
322 goto out_unmap_fdt;
323 }
324
325 if (!fdt_find_child(&n, "chosen")) {
326 dlog("Unable to find 'chosen'\n");
327 goto out_unmap_fdt;
328 }
329
330 /* Patch FDT to point to new ramdisk. */
Andrew Scull265ada92018-07-30 15:19:01 +0100331 if (!fdt_write_number(&n, "linux,initrd-start",
332 pa_addr(p->initrd_begin))) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100333 dlog("Unable to write linux,initrd-start\n");
334 goto out_unmap_fdt;
335 }
336
Andrew Scull265ada92018-07-30 15:19:01 +0100337 if (!fdt_write_number(&n, "linux,initrd-end", pa_addr(p->initrd_end))) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100338 dlog("Unable to write linux,initrd-end\n");
339 goto out_unmap_fdt;
340 }
341
Andrew Walbran401ff562019-04-09 15:43:31 +0100342 /*
343 * Patch FDT to reserve hypervisor memory so the primary VM doesn't try
344 * to use it.
345 */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000346 fdt_add_mem_reservation(
Andrew Walbran401ff562019-04-09 15:43:31 +0100347 fdt, pa_addr(layout_text_begin()),
Andrew Walbran2cb43392019-04-17 12:52:45 +0100348 pa_difference(layout_text_begin(), layout_text_end()));
Andrew Walbran401ff562019-04-09 15:43:31 +0100349 fdt_add_mem_reservation(
350 fdt, pa_addr(layout_rodata_begin()),
Andrew Walbran2cb43392019-04-17 12:52:45 +0100351 pa_difference(layout_rodata_begin(), layout_rodata_end()));
Andrew Walbran401ff562019-04-09 15:43:31 +0100352 fdt_add_mem_reservation(
353 fdt, pa_addr(layout_data_begin()),
Andrew Walbran2cb43392019-04-17 12:52:45 +0100354 pa_difference(layout_data_begin(), layout_data_end()));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100355
Andrew Walbran401ff562019-04-09 15:43:31 +0100356 /* Patch FDT to reserve memory for secondary VMs. */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100357 for (i = 0; i < p->reserved_ranges_count; ++i) {
358 fdt_add_mem_reservation(
359 fdt, pa_addr(p->reserved_ranges[i].begin),
360 pa_addr(p->reserved_ranges[i].end) -
361 pa_addr(p->reserved_ranges[i].begin));
362 }
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100363
364 ret = true;
365
366out_unmap_fdt:
367 /* Unmap FDT. */
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100368 if (!mm_unmap(stage1_locked, fdt_addr,
Andrew Scullda241972019-01-05 18:17:48 +0000369 pa_add(fdt_addr, fdt_total_size(fdt) + PAGE_SIZE),
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000370 ppool)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100371 dlog("Unable to unmap writable FDT.\n");
372 return false;
373 }
374 return ret;
375
376err_unmap_fdt_header:
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100377 mm_unmap(stage1_locked, fdt_addr, pa_add(fdt_addr, fdt_header_size()),
378 ppool);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100379 return false;
380}