blob: 2f241b8911f04c7a84a88655a0e1204748bf6d34 [file] [log] [blame]
Andrew Scullb2910562019-09-17 14:08:27 +01001/*
2 * Copyright 2018 The Hafnium Authors.
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
17#include "hf/arch/init.h"
18
19#include <stdalign.h>
20#include <stddef.h>
21
22#include "hf/api.h"
23#include "hf/boot_flow.h"
24#include "hf/boot_params.h"
25#include "hf/cpio.h"
26#include "hf/cpu.h"
27#include "hf/dlog.h"
Andrew Scull3c257452019-11-26 13:32:50 +000028#include "hf/fdt_handler.h"
Andrew Scullb2910562019-09-17 14:08:27 +010029#include "hf/load.h"
30#include "hf/mm.h"
31#include "hf/mpool.h"
32#include "hf/panic.h"
Andrew Scull3c257452019-11-26 13:32:50 +000033#include "hf/plat/boot_flow.h"
Andrew Scullb2910562019-09-17 14:08:27 +010034#include "hf/plat/console.h"
Andrew Scull3c257452019-11-26 13:32:50 +000035#include "hf/plat/iommu.h"
Andrew Scullb2910562019-09-17 14:08:27 +010036#include "hf/std.h"
37#include "hf/vm.h"
38
39#include "vmapi/hf/call.h"
40
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000041alignas(MM_PPOOL_ENTRY_SIZE) char ptable_buf[MM_PPOOL_ENTRY_SIZE * HEAP_PAGES];
Andrew Scullb2910562019-09-17 14:08:27 +010042
43static struct mpool ppool;
44
45/**
46 * Performs one-time initialisation of memory management for the hypervisor.
47 *
48 * This is the only C code entry point called with MMU and caching disabled. The
49 * page table returned is used to set up the MMU and caches for all subsequent
50 * code.
51 */
52void one_time_init_mm(void)
53{
54 /* Make sure the console is initialised before calling dlog. */
55 plat_console_init();
56
Andrew Walbran17eebf92020-02-05 16:35:49 +000057 dlog_notice("Initialising hafnium\n");
Andrew Scullb2910562019-09-17 14:08:27 +010058
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000059 mpool_init(&ppool, MM_PPOOL_ENTRY_SIZE);
Andrew Scullb2910562019-09-17 14:08:27 +010060 mpool_add_chunk(&ppool, ptable_buf, sizeof(ptable_buf));
61
62 if (!mm_init(&ppool)) {
63 panic("mm_init failed");
64 }
65}
66
67/**
68 * Performs one-time initialisation of the hypervisor.
69 */
70void one_time_init(void)
71{
David Brazdila2358d42020-01-27 18:51:38 +000072 struct string manifest_fname = STRING_INIT("manifest.dtb");
David Brazdilb856be62020-03-25 10:14:55 +000073 struct fdt fdt;
Andrew Scullb2910562019-09-17 14:08:27 +010074 struct manifest manifest;
David Brazdila2358d42020-01-27 18:51:38 +000075 enum manifest_return_code manifest_ret;
Andrew Scullb2910562019-09-17 14:08:27 +010076 struct boot_params params;
77 struct boot_params_update update;
Andrew Scullb2910562019-09-17 14:08:27 +010078 struct memiter cpio;
David Brazdila2358d42020-01-27 18:51:38 +000079 struct memiter manifest_it;
Andrew Scullb2910562019-09-17 14:08:27 +010080 void *initrd;
81 size_t i;
82 struct mm_stage1_locked mm_stage1_locked;
83
84 arch_one_time_init();
85
86 /* Enable locks now that mm is initialised. */
87 dlog_enable_lock();
88 mpool_enable_locks();
89
90 mm_stage1_locked = mm_lock_stage1();
91
David Brazdilb856be62020-03-25 10:14:55 +000092 if (!fdt_map(&fdt, mm_stage1_locked, plat_boot_flow_get_fdt_addr(),
93 &ppool)) {
David Brazdila2358d42020-01-27 18:51:38 +000094 panic("Unable to map FDT.");
Andrew Scull3c257452019-11-26 13:32:50 +000095 }
96
David Brazdilb856be62020-03-25 10:14:55 +000097 if (!boot_flow_get_params(&params, &fdt)) {
David Brazdila2358d42020-01-27 18:51:38 +000098 panic("Could not parse boot params.");
Andrew Scullb2910562019-09-17 14:08:27 +010099 }
100
Andrew Scullb2910562019-09-17 14:08:27 +0100101 for (i = 0; i < params.mem_ranges_count; ++i) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000102 dlog_info("Memory range: %#x - %#x\n",
103 pa_addr(params.mem_ranges[i].begin),
104 pa_addr(params.mem_ranges[i].end) - 1);
Andrew Scullb2910562019-09-17 14:08:27 +0100105 }
106
Andrew Walbran17eebf92020-02-05 16:35:49 +0000107 dlog_info("Ramdisk range: %#x - %#x\n", pa_addr(params.initrd_begin),
108 pa_addr(params.initrd_end) - 1);
Andrew Scullb2910562019-09-17 14:08:27 +0100109
110 /* Map initrd in, and initialise cpio parser. */
111 initrd = mm_identity_map(mm_stage1_locked, params.initrd_begin,
112 params.initrd_end, MM_MODE_R, &ppool);
113 if (!initrd) {
Andrew Scull72b43c02019-09-18 13:53:45 +0100114 panic("Unable to map initrd.");
Andrew Scullb2910562019-09-17 14:08:27 +0100115 }
116
117 memiter_init(&cpio, initrd,
118 pa_difference(params.initrd_begin, params.initrd_end));
119
David Brazdila2358d42020-01-27 18:51:38 +0000120 if (!cpio_get_file(&cpio, &manifest_fname, &manifest_it)) {
121 panic("Could not find manifest in initrd.");
122 }
123
124 manifest_ret = manifest_init(&manifest, &manifest_it);
125 if (manifest_ret != MANIFEST_SUCCESS) {
126 panic("Could not parse manifest: %s.",
127 manifest_strerror(manifest_ret));
128 }
129
David Brazdilb856be62020-03-25 10:14:55 +0000130 if (!plat_iommu_init(&fdt, mm_stage1_locked, &ppool)) {
David Brazdila2358d42020-01-27 18:51:38 +0000131 panic("Could not initialize IOMMUs.");
132 }
133
David Brazdilb856be62020-03-25 10:14:55 +0000134 if (!fdt_unmap(&fdt, mm_stage1_locked, &ppool)) {
David Brazdila2358d42020-01-27 18:51:38 +0000135 panic("Unable to unmap FDT.");
136 }
137
138 cpu_module_init(params.cpu_ids, params.cpu_count);
139
Andrew Scullb2910562019-09-17 14:08:27 +0100140 /* Load all VMs. */
Andrew Scullb2910562019-09-17 14:08:27 +0100141 update.reserved_ranges_count = 0;
Andrew Scull72b43c02019-09-18 13:53:45 +0100142 if (!load_vms(mm_stage1_locked, &manifest, &cpio, &params, &update,
143 &ppool)) {
144 panic("Unable to load VMs.");
Andrew Scullb2910562019-09-17 14:08:27 +0100145 }
146
David Brazdile6f83222019-09-23 14:47:37 +0100147 if (!boot_flow_update(mm_stage1_locked, &manifest, &update, &cpio,
148 &ppool)) {
Andrew Scullc3771072019-09-19 13:30:42 +0100149 panic("Unable to update boot flow.");
Andrew Scullb2910562019-09-17 14:08:27 +0100150 }
151
152 mm_defrag(mm_stage1_locked, &ppool);
153 mm_unlock_stage1(&mm_stage1_locked);
154
155 /* Initialise the API page pool. ppool will be empty from now on. */
156 api_init(&ppool);
157
158 /* Enable TLB invalidation for VM page table updates. */
159 mm_vm_enable_invalidation();
160
Andrew Walbran17eebf92020-02-05 16:35:49 +0000161 dlog_info("Hafnium initialisation completed\n");
Andrew Scullb2910562019-09-17 14:08:27 +0100162}