blob: 9a66975a285bb490bc55f01e1c649dd7f4fcaf4d [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01003 *
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
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010017#include <stdalign.h>
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010018#include <stddef.h>
19
Andrew Scullc960c032018-10-24 15:13:35 +010020#include "hf/arch/init.h"
21
Andrew Scull18c78fc2018-08-20 12:57:41 +010022#include "hf/api.h"
23#include "hf/boot_params.h"
24#include "hf/cpio.h"
25#include "hf/cpu.h"
26#include "hf/dlog.h"
27#include "hf/load.h"
28#include "hf/mm.h"
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000029#include "hf/mpool.h"
Andrew Sculla9c172d2019-04-03 14:10:00 +010030#include "hf/panic.h"
Andrew Walbran48699362019-05-20 14:38:00 +010031#include "hf/plat/console.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010032#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010033#include "hf/vm.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010034
Andrew Scull19503262018-09-20 14:48:39 +010035#include "vmapi/hf/call.h"
36
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000037alignas(alignof(
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000038 struct mm_page_table)) char ptable_buf[sizeof(struct mm_page_table) *
39 HEAP_PAGES];
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010040
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010041/**
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010042 * Performs one-time initialisation of the hypervisor.
43 */
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010044static void one_time_init(void)
45{
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010046 struct boot_params params;
47 struct boot_params_update update;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010048 struct memiter primary_initrd;
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010049 struct memiter cpio;
Andrew Scull80871322018-08-06 12:04:09 +010050 void *initrd;
Andrew Walbran34ce72e2018-09-13 16:47:44 +010051 size_t i;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000052 struct mpool ppool;
Andrew Scull3c0a90a2019-07-01 11:55:53 +010053 struct mm_stage1_locked mm_stage1_locked;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010054
Andrew Walbran48699362019-05-20 14:38:00 +010055 /* Make sure the console is initialised before calling dlog. */
56 plat_console_init();
57
Andrew Scullfdd716e2018-12-20 05:37:31 +000058 dlog("Initialising hafnium\n");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010059
Andrew Scullc960c032018-10-24 15:13:35 +010060 arch_one_time_init();
61
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000062 mpool_init(&ppool, sizeof(struct mm_page_table));
63 mpool_add_chunk(&ppool, ptable_buf, sizeof(ptable_buf));
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010064
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000065 if (!mm_init(&ppool)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010066 panic("mm_init failed");
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010067 }
68
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000069 /* Enable locks now that mm is initialised. */
Andrew Scullfdd716e2018-12-20 05:37:31 +000070 dlog_enable_lock();
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000071 mpool_enable_locks();
72
Andrew Scull3c0a90a2019-07-01 11:55:53 +010073 mm_stage1_locked = mm_lock_stage1();
74
75 if (!plat_get_boot_params(mm_stage1_locked, &params, &ppool)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010076 panic("unable to retrieve boot params");
77 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010078
Andrew Scullbb3ab6c2018-11-26 20:38:49 +000079 cpu_module_init(params.cpu_ids, params.cpu_count);
80
Andrew Walbran34ce72e2018-09-13 16:47:44 +010081 for (i = 0; i < params.mem_ranges_count; ++i) {
82 dlog("Memory range: 0x%x - 0x%x\n",
83 pa_addr(params.mem_ranges[i].begin),
84 pa_addr(params.mem_ranges[i].end) - 1);
85 }
86
Andrew Scull265ada92018-07-30 15:19:01 +010087 dlog("Ramdisk range: 0x%x - 0x%x\n", pa_addr(params.initrd_begin),
88 pa_addr(params.initrd_end) - 1);
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010089
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010090 /* Map initrd in, and initialise cpio parser. */
Andrew Scull3c0a90a2019-07-01 11:55:53 +010091 initrd = mm_identity_map(mm_stage1_locked, params.initrd_begin,
92 params.initrd_end, MM_MODE_R, &ppool);
Andrew Scull80871322018-08-06 12:04:09 +010093 if (!initrd) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010094 panic("unable to map initrd in");
95 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010096
Andrew Scull80871322018-08-06 12:04:09 +010097 memiter_init(&cpio, initrd,
Andrew Walbran2cb43392019-04-17 12:52:45 +010098 pa_difference(params.initrd_begin, params.initrd_end));
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010099
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100100 /* Load all VMs. */
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100101 if (!load_primary(mm_stage1_locked, &cpio, params.kernel_arg,
102 &primary_initrd, &ppool)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100103 panic("unable to load primary VM");
104 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100105
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100106 /*
107 * load_secondary will add regions assigned to the secondary VMs from
108 * mem_ranges to reserved_ranges.
109 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100110 update.initrd_begin = pa_from_va(va_from_ptr(primary_initrd.next));
111 update.initrd_end = pa_from_va(va_from_ptr(primary_initrd.limit));
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100112 update.reserved_ranges_count = 0;
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100113 if (!load_secondary(mm_stage1_locked, &cpio, &params, &update,
114 &ppool)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100115 panic("unable to load secondary VMs");
116 }
117
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100118 /* Prepare to run by updating bootparams as seen by primary VM. */
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100119 if (!plat_update_boot_params(mm_stage1_locked, &update, &ppool)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100120 panic("plat_update_boot_params failed");
121 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100122
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100123 mm_defrag(mm_stage1_locked, &ppool);
124 mm_unlock_stage1(&mm_stage1_locked);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000125
126 /* Initialise the API page pool. ppool will be empty from now on. */
127 api_init(&ppool);
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100128
Andrew Scullda241972019-01-05 18:17:48 +0000129 /* Enable TLB invalidation for VM page table updates. */
130 mm_vm_enable_invalidation();
131
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100132 dlog("Hafnium initialisation completed\n");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100133}
134
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100135/**
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100136 * The entry point of CPUs when they are turned on. It is supposed to initialise
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100137 * all state and return the first vCPU to run.
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100138 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100139struct vcpu *cpu_main(struct cpu *c)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100140{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100141 struct vcpu *vcpu;
Andrew Scullc960c032018-10-24 15:13:35 +0100142 struct vm *vm;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100143
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100144 /*
145 * Do global one-time initialisation just once. We avoid using atomics
146 * by only touching the variable from cpu 0.
147 */
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100148 static volatile bool inited = false;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000149
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100150 if (cpu_index(c) == 0 && !inited) {
151 inited = true;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100152 one_time_init();
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100153 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100154
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100155 if (!mm_cpu_init()) {
156 panic("mm_cpu_init failed");
157 }
158
Andrew Walbran42347a92019-05-09 13:59:03 +0100159 vcpu = vm_get_vcpu(vm_find(HF_PRIMARY_VM_ID), cpu_index(c));
Andrew Scullc960c032018-10-24 15:13:35 +0100160 vm = vcpu->vm;
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100161 vcpu->cpu = c;
Andrew Scullc960c032018-10-24 15:13:35 +0100162
163 /* Reset the registers to give a clean start for the primary's vCPU. */
Andrew Scullbb3ab6c2018-11-26 20:38:49 +0000164 arch_regs_reset(&vcpu->regs, true, vm->id, c->id, vm->ptable.root);
Andrew Scullc960c032018-10-24 15:13:35 +0100165
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100166 return vcpu;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100167}