blob: bd4c3191a21aea34147193a24d9ad56791d609bf [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 Scull8d9e1212019-04-05 13:52:55 +010031#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010032#include "hf/vm.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010033
Andrew Scull19503262018-09-20 14:48:39 +010034#include "vmapi/hf/call.h"
35
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000036alignas(alignof(
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000037 struct mm_page_table)) char ptable_buf[sizeof(struct mm_page_table) *
38 HEAP_PAGES];
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010039
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010040/**
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010041 * Performs one-time initialisation of the hypervisor.
42 */
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010043static void one_time_init(void)
44{
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010045 struct boot_params params;
46 struct boot_params_update update;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010047 struct memiter primary_initrd;
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010048 struct memiter cpio;
Andrew Scull80871322018-08-06 12:04:09 +010049 void *initrd;
Andrew Walbran34ce72e2018-09-13 16:47:44 +010050 size_t i;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000051 struct mpool ppool;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010052
Andrew Scullfdd716e2018-12-20 05:37:31 +000053 dlog("Initialising hafnium\n");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010054
Andrew Scullc960c032018-10-24 15:13:35 +010055 arch_one_time_init();
56
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000057 mpool_init(&ppool, sizeof(struct mm_page_table));
58 mpool_add_chunk(&ppool, ptable_buf, sizeof(ptable_buf));
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010059
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000060 if (!mm_init(&ppool)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010061 panic("mm_init failed");
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010062 }
63
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000064 /* Enable locks now that mm is initialised. */
Andrew Scullfdd716e2018-12-20 05:37:31 +000065 dlog_enable_lock();
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000066 mpool_enable_locks();
67
68 if (!plat_get_boot_params(&params, &ppool)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010069 panic("unable to retrieve boot params");
70 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010071
Andrew Scullbb3ab6c2018-11-26 20:38:49 +000072 cpu_module_init(params.cpu_ids, params.cpu_count);
73
Andrew Walbran34ce72e2018-09-13 16:47:44 +010074 for (i = 0; i < params.mem_ranges_count; ++i) {
75 dlog("Memory range: 0x%x - 0x%x\n",
76 pa_addr(params.mem_ranges[i].begin),
77 pa_addr(params.mem_ranges[i].end) - 1);
78 }
79
Andrew Scull265ada92018-07-30 15:19:01 +010080 dlog("Ramdisk range: 0x%x - 0x%x\n", pa_addr(params.initrd_begin),
81 pa_addr(params.initrd_end) - 1);
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010082
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010083 /* Map initrd in, and initialise cpio parser. */
Andrew Scull80871322018-08-06 12:04:09 +010084 initrd = mm_identity_map(params.initrd_begin, params.initrd_end,
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000085 MM_MODE_R, &ppool);
Andrew Scull80871322018-08-06 12:04:09 +010086 if (!initrd) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010087 panic("unable to map initrd in");
88 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010089
Andrew Scull80871322018-08-06 12:04:09 +010090 memiter_init(&cpio, initrd,
Andrew Walbran2cb43392019-04-17 12:52:45 +010091 pa_difference(params.initrd_begin, params.initrd_end));
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010092
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010093 /* Load all VMs. */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000094 if (!load_primary(&cpio, params.kernel_arg, &primary_initrd, &ppool)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010095 panic("unable to load primary VM");
96 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010097
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +010098 /*
99 * load_secondary will add regions assigned to the secondary VMs from
100 * mem_ranges to reserved_ranges.
101 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100102 update.initrd_begin = pa_from_va(va_from_ptr(primary_initrd.next));
103 update.initrd_end = pa_from_va(va_from_ptr(primary_initrd.limit));
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100104 update.reserved_ranges_count = 0;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000105 if (!load_secondary(&cpio, &params, &update, &ppool)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100106 panic("unable to load secondary VMs");
107 }
108
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100109 /* Prepare to run by updating bootparams as seen by primary VM. */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000110 if (!plat_update_boot_params(&update, &ppool)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100111 panic("plat_update_boot_params failed");
112 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100113
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000114 mm_defrag(&ppool);
115
116 /* Initialise the API page pool. ppool will be empty from now on. */
117 api_init(&ppool);
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100118
Andrew Scullda241972019-01-05 18:17:48 +0000119 /* Enable TLB invalidation for VM page table updates. */
120 mm_vm_enable_invalidation();
121
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100122 dlog("Hafnium initialisation completed\n");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100123}
124
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100125/**
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100126 * The entry point of CPUs when they are turned on. It is supposed to initialise
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100127 * all state and return the first vCPU to run.
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100128 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100129struct vcpu *cpu_main(struct cpu *c)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100130{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100131 struct vcpu *vcpu;
Andrew Scullc960c032018-10-24 15:13:35 +0100132 struct vm *vm;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100133
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100134 /*
135 * Do global one-time initialisation just once. We avoid using atomics
136 * by only touching the variable from cpu 0.
137 */
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100138 static volatile bool inited = false;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000139
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100140 if (cpu_index(c) == 0 && !inited) {
141 inited = true;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100142 one_time_init();
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100143 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100144
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100145 if (!mm_cpu_init()) {
146 panic("mm_cpu_init failed");
147 }
148
Andrew Walbrane1310df2019-04-29 17:28:28 +0100149 vcpu = vm_get_vcpu(vm_get(HF_PRIMARY_VM_ID), cpu_index(c));
Andrew Scullc960c032018-10-24 15:13:35 +0100150 vm = vcpu->vm;
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100151 vcpu->cpu = c;
Andrew Scullc960c032018-10-24 15:13:35 +0100152
153 /* Reset the registers to give a clean start for the primary's vCPU. */
Andrew Scullbb3ab6c2018-11-26 20:38:49 +0000154 arch_regs_reset(&vcpu->regs, true, vm->id, c->id, vm->ptable.root);
Andrew Scullc960c032018-10-24 15:13:35 +0100155
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100156 return vcpu;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100157}