blob: 9f8f829da4f19c8e3e38faa394dae5c6612de0ee [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
41alignas(alignof(
42 struct mm_page_table)) char ptable_buf[sizeof(struct mm_page_table) *
43 HEAP_PAGES];
44
45static struct mpool ppool;
46
47/**
48 * Performs one-time initialisation of memory management for the hypervisor.
49 *
50 * This is the only C code entry point called with MMU and caching disabled. The
51 * page table returned is used to set up the MMU and caches for all subsequent
52 * code.
53 */
54void one_time_init_mm(void)
55{
56 /* Make sure the console is initialised before calling dlog. */
57 plat_console_init();
58
Andrew Walbran17eebf92020-02-05 16:35:49 +000059 dlog_notice("Initialising hafnium\n");
Andrew Scullb2910562019-09-17 14:08:27 +010060
61 mpool_init(&ppool, sizeof(struct mm_page_table));
62 mpool_add_chunk(&ppool, ptable_buf, sizeof(ptable_buf));
63
64 if (!mm_init(&ppool)) {
65 panic("mm_init failed");
66 }
67}
68
69/**
70 * Performs one-time initialisation of the hypervisor.
71 */
72void one_time_init(void)
73{
David Brazdila2358d42020-01-27 18:51:38 +000074 struct string manifest_fname = STRING_INIT("manifest.dtb");
Andrew Scull3c257452019-11-26 13:32:50 +000075 struct fdt_header *fdt;
76 struct fdt_node fdt_root;
Andrew Scullb2910562019-09-17 14:08:27 +010077 struct manifest manifest;
David Brazdila2358d42020-01-27 18:51:38 +000078 enum manifest_return_code manifest_ret;
Andrew Scullb2910562019-09-17 14:08:27 +010079 struct boot_params params;
80 struct boot_params_update update;
Andrew Scullb2910562019-09-17 14:08:27 +010081 struct memiter cpio;
David Brazdila2358d42020-01-27 18:51:38 +000082 struct memiter manifest_it;
Andrew Scullb2910562019-09-17 14:08:27 +010083 void *initrd;
84 size_t i;
85 struct mm_stage1_locked mm_stage1_locked;
86
87 arch_one_time_init();
88
89 /* Enable locks now that mm is initialised. */
90 dlog_enable_lock();
91 mpool_enable_locks();
92
93 mm_stage1_locked = mm_lock_stage1();
94
Andrew Scull3c257452019-11-26 13:32:50 +000095 fdt = fdt_map(mm_stage1_locked, plat_boot_flow_get_fdt_addr(),
96 &fdt_root, &ppool);
97 if (fdt == NULL) {
David Brazdila2358d42020-01-27 18:51:38 +000098 panic("Unable to map FDT.");
Andrew Scull3c257452019-11-26 13:32:50 +000099 }
100
101 if (!fdt_find_child(&fdt_root, "")) {
David Brazdila2358d42020-01-27 18:51:38 +0000102 panic("Unable to find FDT root node.");
Andrew Scull3c257452019-11-26 13:32:50 +0000103 }
104
David Brazdila2358d42020-01-27 18:51:38 +0000105 if (!boot_flow_get_params(&params, &fdt_root)) {
106 panic("Could not parse boot params.");
Andrew Scullb2910562019-09-17 14:08:27 +0100107 }
108
Andrew Scullb2910562019-09-17 14:08:27 +0100109 for (i = 0; i < params.mem_ranges_count; ++i) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000110 dlog_info("Memory range: %#x - %#x\n",
111 pa_addr(params.mem_ranges[i].begin),
112 pa_addr(params.mem_ranges[i].end) - 1);
Andrew Scullb2910562019-09-17 14:08:27 +0100113 }
114
Andrew Walbran17eebf92020-02-05 16:35:49 +0000115 dlog_info("Ramdisk range: %#x - %#x\n", pa_addr(params.initrd_begin),
116 pa_addr(params.initrd_end) - 1);
Andrew Scullb2910562019-09-17 14:08:27 +0100117
118 /* Map initrd in, and initialise cpio parser. */
119 initrd = mm_identity_map(mm_stage1_locked, params.initrd_begin,
120 params.initrd_end, MM_MODE_R, &ppool);
121 if (!initrd) {
Andrew Scull72b43c02019-09-18 13:53:45 +0100122 panic("Unable to map initrd.");
Andrew Scullb2910562019-09-17 14:08:27 +0100123 }
124
125 memiter_init(&cpio, initrd,
126 pa_difference(params.initrd_begin, params.initrd_end));
127
David Brazdila2358d42020-01-27 18:51:38 +0000128 if (!cpio_get_file(&cpio, &manifest_fname, &manifest_it)) {
129 panic("Could not find manifest in initrd.");
130 }
131
Andrew Walbran1de06be2020-03-03 13:48:59 +0000132 dlog_verbose("Manifest range: %#x - %#x (%d bytes)\n", manifest_it.next,
133 manifest_it.limit, manifest_it.limit - manifest_it.next);
134 if (!is_aligned(manifest_it.next, 4)) {
135 panic("Manifest not aligned.");
136 }
137
David Brazdila2358d42020-01-27 18:51:38 +0000138 manifest_ret = manifest_init(&manifest, &manifest_it);
139 if (manifest_ret != MANIFEST_SUCCESS) {
140 panic("Could not parse manifest: %s.",
141 manifest_strerror(manifest_ret));
142 }
143
144 if (!plat_iommu_init(&fdt_root, mm_stage1_locked, &ppool)) {
145 panic("Could not initialize IOMMUs.");
146 }
147
148 if (!fdt_unmap(mm_stage1_locked, fdt, &ppool)) {
149 panic("Unable to unmap FDT.");
150 }
151
152 cpu_module_init(params.cpu_ids, params.cpu_count);
153
Andrew Scullb2910562019-09-17 14:08:27 +0100154 /* Load all VMs. */
Andrew Scullb2910562019-09-17 14:08:27 +0100155 update.reserved_ranges_count = 0;
Andrew Scull72b43c02019-09-18 13:53:45 +0100156 if (!load_vms(mm_stage1_locked, &manifest, &cpio, &params, &update,
157 &ppool)) {
158 panic("Unable to load VMs.");
Andrew Scullb2910562019-09-17 14:08:27 +0100159 }
160
David Brazdile6f83222019-09-23 14:47:37 +0100161 if (!boot_flow_update(mm_stage1_locked, &manifest, &update, &cpio,
162 &ppool)) {
Andrew Scullc3771072019-09-19 13:30:42 +0100163 panic("Unable to update boot flow.");
Andrew Scullb2910562019-09-17 14:08:27 +0100164 }
165
166 mm_defrag(mm_stage1_locked, &ppool);
167 mm_unlock_stage1(&mm_stage1_locked);
168
169 /* Initialise the API page pool. ppool will be empty from now on. */
170 api_init(&ppool);
171
172 /* Enable TLB invalidation for VM page table updates. */
173 mm_vm_enable_invalidation();
174
Andrew Walbran17eebf92020-02-05 16:35:49 +0000175 dlog_info("Hafnium initialisation completed\n");
Andrew Scullb2910562019-09-17 14:08:27 +0100176}