blob: 8a136108c1334555e46addc9d906130c4cc60ca4 [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;
74 struct memiter primary_initrd;
75 struct memiter cpio;
76 void *initrd;
77 size_t i;
78 struct mm_stage1_locked mm_stage1_locked;
79
80 arch_one_time_init();
81
82 /* Enable locks now that mm is initialised. */
83 dlog_enable_lock();
84 mpool_enable_locks();
85
86 mm_stage1_locked = mm_lock_stage1();
87
88 if (!boot_flow_init(mm_stage1_locked, &manifest, &params, &ppool)) {
89 panic("Could not parse data from FDT.");
90 }
91
92 cpu_module_init(params.cpu_ids, params.cpu_count);
93
94 for (i = 0; i < params.mem_ranges_count; ++i) {
95 dlog("Memory range: %#x - %#x\n",
96 pa_addr(params.mem_ranges[i].begin),
97 pa_addr(params.mem_ranges[i].end) - 1);
98 }
99
100 dlog("Ramdisk range: %#x - %#x\n", pa_addr(params.initrd_begin),
101 pa_addr(params.initrd_end) - 1);
102
103 /* Map initrd in, and initialise cpio parser. */
104 initrd = mm_identity_map(mm_stage1_locked, params.initrd_begin,
105 params.initrd_end, MM_MODE_R, &ppool);
106 if (!initrd) {
107 panic("unable to map initrd in");
108 }
109
110 memiter_init(&cpio, initrd,
111 pa_difference(params.initrd_begin, params.initrd_end));
112
113 /* Load all VMs. */
114 if (!load_primary(mm_stage1_locked, &cpio, params.kernel_arg,
115 &primary_initrd, &ppool)) {
116 panic("unable to load primary VM");
117 }
118
119 /*
120 * load_secondary will add regions assigned to the secondary VMs from
121 * mem_ranges to reserved_ranges.
122 */
123 update.initrd_begin = pa_from_va(va_from_ptr(primary_initrd.next));
124 update.initrd_end = pa_from_va(va_from_ptr(primary_initrd.limit));
125 update.reserved_ranges_count = 0;
126 if (!load_secondary(mm_stage1_locked, &manifest, &cpio, &params,
127 &update, &ppool)) {
128 panic("unable to load secondary VMs");
129 }
130
131 /* Prepare to run by updating bootparams as seen by primary VM. */
132 if (!boot_params_patch_fdt(mm_stage1_locked, &update, &ppool)) {
133 panic("plat_update_boot_params failed");
134 }
135
136 mm_defrag(mm_stage1_locked, &ppool);
137 mm_unlock_stage1(&mm_stage1_locked);
138
139 /* Initialise the API page pool. ppool will be empty from now on. */
140 api_init(&ppool);
141
142 /* Enable TLB invalidation for VM page table updates. */
143 mm_vm_enable_invalidation();
144
145 dlog("Hafnium initialisation completed\n");
146}