blob: 4ed8c878a9baca4f24c604fa71c13340b1bb1331 [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"
28#include "hf/load.h"
29#include "hf/mm.h"
30#include "hf/mpool.h"
31#include "hf/panic.h"
32#include "hf/plat/console.h"
33#include "hf/std.h"
34#include "hf/vm.h"
35
36#include "vmapi/hf/call.h"
37
38alignas(alignof(
39 struct mm_page_table)) char ptable_buf[sizeof(struct mm_page_table) *
40 HEAP_PAGES];
41
42static struct mpool ppool;
43
44/**
45 * Performs one-time initialisation of memory management for the hypervisor.
46 *
47 * This is the only C code entry point called with MMU and caching disabled. The
48 * page table returned is used to set up the MMU and caches for all subsequent
49 * code.
50 */
51void one_time_init_mm(void)
52{
53 /* Make sure the console is initialised before calling dlog. */
54 plat_console_init();
55
56 dlog("Initialising hafnium\n");
57
58 mpool_init(&ppool, sizeof(struct mm_page_table));
59 mpool_add_chunk(&ppool, ptable_buf, sizeof(ptable_buf));
60
61 if (!mm_init(&ppool)) {
62 panic("mm_init failed");
63 }
64}
65
66/**
67 * Performs one-time initialisation of the hypervisor.
68 */
69void one_time_init(void)
70{
71 struct manifest manifest;
72 struct boot_params params;
73 struct boot_params_update update;
Andrew Scullb2910562019-09-17 14:08:27 +010074 struct memiter cpio;
75 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
87 if (!boot_flow_init(mm_stage1_locked, &manifest, &params, &ppool)) {
88 panic("Could not parse data from FDT.");
89 }
90
91 cpu_module_init(params.cpu_ids, params.cpu_count);
92
93 for (i = 0; i < params.mem_ranges_count; ++i) {
94 dlog("Memory range: %#x - %#x\n",
95 pa_addr(params.mem_ranges[i].begin),
96 pa_addr(params.mem_ranges[i].end) - 1);
97 }
98
99 dlog("Ramdisk range: %#x - %#x\n", pa_addr(params.initrd_begin),
100 pa_addr(params.initrd_end) - 1);
101
102 /* Map initrd in, and initialise cpio parser. */
103 initrd = mm_identity_map(mm_stage1_locked, params.initrd_begin,
104 params.initrd_end, MM_MODE_R, &ppool);
105 if (!initrd) {
Andrew Scull72b43c02019-09-18 13:53:45 +0100106 panic("Unable to map initrd.");
Andrew Scullb2910562019-09-17 14:08:27 +0100107 }
108
109 memiter_init(&cpio, initrd,
110 pa_difference(params.initrd_begin, params.initrd_end));
111
112 /* Load all VMs. */
Andrew Scullb2910562019-09-17 14:08:27 +0100113 update.reserved_ranges_count = 0;
Andrew Scull72b43c02019-09-18 13:53:45 +0100114 if (!load_vms(mm_stage1_locked, &manifest, &cpio, &params, &update,
115 &ppool)) {
116 panic("Unable to load VMs.");
Andrew Scullb2910562019-09-17 14:08:27 +0100117 }
118
119 /* Prepare to run by updating bootparams as seen by primary VM. */
120 if (!boot_params_patch_fdt(mm_stage1_locked, &update, &ppool)) {
121 panic("plat_update_boot_params failed");
122 }
123
124 mm_defrag(mm_stage1_locked, &ppool);
125 mm_unlock_stage1(&mm_stage1_locked);
126
127 /* Initialise the API page pool. ppool will be empty from now on. */
128 api_init(&ppool);
129
130 /* Enable TLB invalidation for VM page table updates. */
131 mm_vm_enable_invalidation();
132
133 dlog("Hafnium initialisation completed\n");
134}