blob: d9cb1d7003c9b400d96bb309e30b8656c13b6d3a [file] [log] [blame]
Andrew Scullb2910562019-09-17 14:08:27 +01001/*
2 * Copyright 2018 The Hafnium Authors.
3 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * 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 Scullb2910562019-09-17 14:08:27 +01007 */
8
9#include "hf/arch/init.h"
10
11#include <stdalign.h>
12#include <stddef.h>
13
Olivier Deprez112d2b52020-09-30 07:39:23 +020014#include "hf/arch/other_world.h"
Olivier Deprez55a189e2021-06-09 15:45:27 +020015#include "hf/arch/plat/ffa.h"
Andrew Walbran41a49d82020-01-10 17:46:38 +000016
Andrew Scullb2910562019-09-17 14:08:27 +010017#include "hf/api.h"
18#include "hf/boot_flow.h"
19#include "hf/boot_params.h"
20#include "hf/cpio.h"
21#include "hf/cpu.h"
22#include "hf/dlog.h"
Andrew Scull3c257452019-11-26 13:32:50 +000023#include "hf/fdt_handler.h"
Andrew Scullb2910562019-09-17 14:08:27 +010024#include "hf/load.h"
25#include "hf/mm.h"
26#include "hf/mpool.h"
27#include "hf/panic.h"
Andrew Scull3c257452019-11-26 13:32:50 +000028#include "hf/plat/boot_flow.h"
Andrew Scullb2910562019-09-17 14:08:27 +010029#include "hf/plat/console.h"
Andrew Scull3c257452019-11-26 13:32:50 +000030#include "hf/plat/iommu.h"
Andrew Scullb2910562019-09-17 14:08:27 +010031#include "hf/std.h"
32#include "hf/vm.h"
33
34#include "vmapi/hf/call.h"
35
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000036alignas(MM_PPOOL_ENTRY_SIZE) char ptable_buf[MM_PPOOL_ENTRY_SIZE * HEAP_PAGES];
Andrew Scullb2910562019-09-17 14:08:27 +010037
38static struct mpool ppool;
39
40/**
41 * Performs one-time initialisation of memory management for the hypervisor.
42 *
43 * This is the only C code entry point called with MMU and caching disabled. The
44 * page table returned is used to set up the MMU and caches for all subsequent
45 * code.
46 */
47void one_time_init_mm(void)
48{
49 /* Make sure the console is initialised before calling dlog. */
50 plat_console_init();
51
Olivier Deprez55a189e2021-06-09 15:45:27 +020052 plat_ffa_log_init();
Andrew Scullb2910562019-09-17 14:08:27 +010053
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000054 mpool_init(&ppool, MM_PPOOL_ENTRY_SIZE);
Andrew Scullb2910562019-09-17 14:08:27 +010055 mpool_add_chunk(&ppool, ptable_buf, sizeof(ptable_buf));
56
57 if (!mm_init(&ppool)) {
58 panic("mm_init failed");
59 }
60}
61
62/**
63 * Performs one-time initialisation of the hypervisor.
64 */
65void one_time_init(void)
66{
David Brazdila2358d42020-01-27 18:51:38 +000067 struct string manifest_fname = STRING_INIT("manifest.dtb");
David Brazdilb856be62020-03-25 10:14:55 +000068 struct fdt fdt;
Andrew Scullb2910562019-09-17 14:08:27 +010069 struct manifest manifest;
David Brazdila2358d42020-01-27 18:51:38 +000070 enum manifest_return_code manifest_ret;
Andrew Scullb2910562019-09-17 14:08:27 +010071 struct boot_params params;
72 struct boot_params_update update;
Andrew Scullb2910562019-09-17 14:08:27 +010073 struct memiter cpio;
David Brazdila2358d42020-01-27 18:51:38 +000074 struct memiter manifest_it;
Andrew Scullb2910562019-09-17 14:08:27 +010075 void *initrd;
76 size_t i;
77 struct mm_stage1_locked mm_stage1_locked;
78
79 arch_one_time_init();
80
81 /* Enable locks now that mm is initialised. */
82 dlog_enable_lock();
83 mpool_enable_locks();
84
85 mm_stage1_locked = mm_lock_stage1();
86
David Brazdilb856be62020-03-25 10:14:55 +000087 if (!fdt_map(&fdt, mm_stage1_locked, plat_boot_flow_get_fdt_addr(),
88 &ppool)) {
David Brazdila2358d42020-01-27 18:51:38 +000089 panic("Unable to map FDT.");
Andrew Scull3c257452019-11-26 13:32:50 +000090 }
91
David Brazdilb856be62020-03-25 10:14:55 +000092 if (!boot_flow_get_params(&params, &fdt)) {
David Brazdila2358d42020-01-27 18:51:38 +000093 panic("Could not parse boot params.");
Andrew Scullb2910562019-09-17 14:08:27 +010094 }
95
Andrew Scullb2910562019-09-17 14:08:27 +010096 for (i = 0; i < params.mem_ranges_count; ++i) {
Andrew Walbran17eebf92020-02-05 16:35:49 +000097 dlog_info("Memory range: %#x - %#x\n",
98 pa_addr(params.mem_ranges[i].begin),
99 pa_addr(params.mem_ranges[i].end) - 1);
Andrew Scullb2910562019-09-17 14:08:27 +0100100 }
101
Olivier Deprez62d99e32020-01-09 15:58:07 +0100102 /*
103 * Hafnium manifest is either gathered from the ramdisk or passed
104 * directly to Hafnium entry point by the earlier bootloader stage.
105 * If the ramdisk start address is non-zero it hints the manifest
106 * shall be looked up from the ramdisk. If zero, assume the address
107 * passed to Hafnium entry point is the manifest address.
108 */
109 if (pa_addr(params.initrd_begin)) {
110 dlog_info("Ramdisk range: %#x - %#x\n",
111 pa_addr(params.initrd_begin),
112 pa_addr(params.initrd_end) - 1);
Andrew Scullb2910562019-09-17 14:08:27 +0100113
Olivier Deprez62d99e32020-01-09 15:58:07 +0100114 /* Map initrd in, and initialise cpio parser. */
115 initrd = mm_identity_map(mm_stage1_locked, params.initrd_begin,
116 params.initrd_end, MM_MODE_R, &ppool);
117 if (!initrd) {
118 panic("Unable to map initrd.");
119 }
120
121 memiter_init(
122 &cpio, initrd,
123 pa_difference(params.initrd_begin, params.initrd_end));
124
125 if (!cpio_get_file(&cpio, &manifest_fname, &manifest_it)) {
126 panic("Could not find manifest in initrd.");
127 }
128 } else {
129 manifest_it = fdt.buf;
Andrew Scullb2910562019-09-17 14:08:27 +0100130 }
131
Olivier Deprez62d99e32020-01-09 15:58:07 +0100132 manifest_ret = manifest_init(mm_stage1_locked, &manifest, &manifest_it,
133 &ppool);
Andrew Scullb2910562019-09-17 14:08:27 +0100134
David Brazdila2358d42020-01-27 18:51:38 +0000135 if (manifest_ret != MANIFEST_SUCCESS) {
136 panic("Could not parse manifest: %s.",
137 manifest_strerror(manifest_ret));
138 }
139
David Brazdilb856be62020-03-25 10:14:55 +0000140 if (!plat_iommu_init(&fdt, mm_stage1_locked, &ppool)) {
David Brazdila2358d42020-01-27 18:51:38 +0000141 panic("Could not initialize IOMMUs.");
142 }
143
David Brazdilb856be62020-03-25 10:14:55 +0000144 if (!fdt_unmap(&fdt, mm_stage1_locked, &ppool)) {
David Brazdila2358d42020-01-27 18:51:38 +0000145 panic("Unable to unmap FDT.");
146 }
147
148 cpu_module_init(params.cpu_ids, params.cpu_count);
149
Andrew Scullb2910562019-09-17 14:08:27 +0100150 /* Load all VMs. */
Andrew Scullb2910562019-09-17 14:08:27 +0100151 update.reserved_ranges_count = 0;
Andrew Scull72b43c02019-09-18 13:53:45 +0100152 if (!load_vms(mm_stage1_locked, &manifest, &cpio, &params, &update,
153 &ppool)) {
154 panic("Unable to load VMs.");
Andrew Scullb2910562019-09-17 14:08:27 +0100155 }
156
David Brazdile6f83222019-09-23 14:47:37 +0100157 if (!boot_flow_update(mm_stage1_locked, &manifest, &update, &cpio,
158 &ppool)) {
Andrew Scullc3771072019-09-19 13:30:42 +0100159 panic("Unable to update boot flow.");
Andrew Scullb2910562019-09-17 14:08:27 +0100160 }
161
162 mm_defrag(mm_stage1_locked, &ppool);
163 mm_unlock_stage1(&mm_stage1_locked);
164
165 /* Initialise the API page pool. ppool will be empty from now on. */
166 api_init(&ppool);
167
168 /* Enable TLB invalidation for VM page table updates. */
169 mm_vm_enable_invalidation();
170
Olivier Deprez55a189e2021-06-09 15:45:27 +0200171 /* Set up message buffers for TEE dispatcher. */
172 plat_ffa_init(manifest.ffa_tee_enabled);
Andrew Walbran41a49d82020-01-10 17:46:38 +0000173
Andrew Walbran17eebf92020-02-05 16:35:49 +0000174 dlog_info("Hafnium initialisation completed\n");
Andrew Scullb2910562019-09-17 14:08:27 +0100175}