blob: 0e792f5cd760807ee2bd2d495ea82aaa87221769 [file] [log] [blame]
Andrew Scull11a4a0c2018-12-29 11:38:31 +00001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Andrew Scull11a4a0c2018-12-29 11:38:31 +00003 *
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
Andrew Scull9a6384b2019-01-02 12:08:40 +000017#include "hf/arch/cpu.h"
18
Andrew Scull11a4a0c2018-12-29 11:38:31 +000019#include <stdbool.h>
20#include <stddef.h>
21#include <stdint.h>
22
23#include "hf/addr.h"
Andrew Walbrand4d2fa12019-10-01 16:47:25 +010024#include "hf/spci.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010025#include "hf/std.h"
Fuad Tabba5c738432019-12-02 11:02:42 +000026#include "hf/vm.h"
Andrew Scull11a4a0c2018-12-29 11:38:31 +000027
Fuad Tabbac8eede32019-10-31 11:17:50 +000028#include "msr.h"
Andrew Walbran42d89e72019-11-27 12:40:10 +000029#include "perfmon.h"
30#include "sysregs.h"
Fuad Tabbac8eede32019-10-31 11:17:50 +000031
32/**
33 * The LO field indicates whether LORegions are supported.
34 */
35#define ID_AA64MMFR1_EL1_LO (UINT64_C(1) << 16)
Fuad Tabbac76466d2019-09-06 10:42:12 +010036
Fuad Tabbac8eede32019-10-31 11:17:50 +000037static void lor_disable(void)
38{
39 /*
40 * Accesses to LORC_EL1 are undefined if LORegions are not supported.
41 */
42 if (read_msr(ID_AA64MMFR1_EL1) & ID_AA64MMFR1_EL1_LO) {
43 write_msr(MSR_LORC_EL1, 0);
44 }
45}
46
Andrew Walbranb208b4a2019-05-20 12:42:22 +010047static void gic_regs_reset(struct arch_regs *r, bool is_primary)
48{
49#if GIC_VERSION == 3 || GIC_VERSION == 4
50 uint32_t ich_hcr = 0;
Andrew Walbran4b976f42019-06-05 15:00:50 +010051 uint32_t icc_sre_el2 =
Andrew Walbrane52006c2019-10-22 18:01:28 +010052 (1U << 0) | /* SRE, enable ICH_* and ICC_* at EL2. */
Andrew Walbran4b976f42019-06-05 15:00:50 +010053 (0x3 << 1); /* DIB and DFB, disable IRQ/FIQ bypass. */
Andrew Walbranb208b4a2019-05-20 12:42:22 +010054
Andrew Walbran4b976f42019-06-05 15:00:50 +010055 if (is_primary) {
Andrew Walbrane52006c2019-10-22 18:01:28 +010056 icc_sre_el2 |= 1U << 3; /* Enable EL1 access to ICC_SRE_EL1. */
Andrew Walbran4b976f42019-06-05 15:00:50 +010057 } else {
Andrew Walbranb208b4a2019-05-20 12:42:22 +010058 /* Trap EL1 access to GICv3 system registers. */
59 ich_hcr =
Andrew Walbrane52006c2019-10-22 18:01:28 +010060 (0x1fU << 10); /* TDIR, TSEI, TALL1, TALL0, TC bits. */
Andrew Walbranb208b4a2019-05-20 12:42:22 +010061 }
62 r->gic.ich_hcr_el2 = ich_hcr;
Andrew Walbran4b976f42019-06-05 15:00:50 +010063 r->gic.icc_sre_el2 = icc_sre_el2;
Andrew Walbranb208b4a2019-05-20 12:42:22 +010064#endif
65}
66
Fuad Tabba5c738432019-12-02 11:02:42 +000067void arch_regs_reset(struct vcpu *vcpu)
Andrew Scull11a4a0c2018-12-29 11:38:31 +000068{
Fuad Tabba5c738432019-12-02 11:02:42 +000069 spci_vm_id_t vm_id = vcpu->vm->id;
70 bool is_primary = vm_id == HF_PRIMARY_VM_ID;
71 cpu_id_t vcpu_id = vcpu_index(vcpu);
72 paddr_t table = vcpu->vm->ptable.root;
73 struct arch_regs *r = &vcpu->regs;
Andrew Scullc960c032018-10-24 15:13:35 +010074 uintreg_t pc = r->pc;
75 uintreg_t arg = r->r[0];
Andrew Scull11a4a0c2018-12-29 11:38:31 +000076 uintreg_t cnthctl;
77
Andrew Scull2b5fbad2019-04-05 13:55:56 +010078 memset_s(r, sizeof(*r), 0, sizeof(*r));
Andrew Scullc960c032018-10-24 15:13:35 +010079
80 r->pc = pc;
81 r->r[0] = arg;
82
Andrew Scull11a4a0c2018-12-29 11:38:31 +000083 cnthctl = 0;
84
85 if (is_primary) {
86 cnthctl |=
Andrew Walbrane52006c2019-10-22 18:01:28 +010087 (1U << 0) | /* EL1PCTEN, don't trap phys cnt access. */
88 (1U << 1); /* EL1PCEN, don't trap phys timer access. */
Andrew Scull11a4a0c2018-12-29 11:38:31 +000089 }
90
Fuad Tabba46b86162019-10-18 13:29:14 +010091 r->lazy.hcr_el2 = get_hcr_el2_value(vm_id);
Andrew Scull11a4a0c2018-12-29 11:38:31 +000092 r->lazy.cnthctl_el2 = cnthctl;
Andrew Walbran95534922019-06-19 11:32:54 +010093 r->lazy.vttbr_el2 = pa_addr(table) | ((uint64_t)vm_id << 48);
Andrew Scullbb3ab6c2018-11-26 20:38:49 +000094 r->lazy.vmpidr_el2 = vcpu_id;
Fuad Tabba3e9b0222019-11-11 16:47:50 +000095 /* Mask (disable) interrupts and run in EL1h mode. */
96 r->spsr = PSR_D | PSR_A | PSR_I | PSR_F | PSR_PE_MODE_EL1H;
Andrew Walbranb208b4a2019-05-20 12:42:22 +010097
Fuad Tabbac76466d2019-09-06 10:42:12 +010098 r->lazy.mdcr_el2 = get_mdcr_el2_value(vm_id);
99
100 /*
101 * NOTE: It is important that MDSCR_EL1.MDE (bit 15) is set to 0 for
102 * secondary VMs as long as Hafnium does not support debug register
103 * access for secondary VMs. If adding Hafnium support for secondary VM
104 * debug register accesses, then on context switches Hafnium needs to
105 * save/restore EL1 debug register state that either might change, or
106 * that needs to be protected.
107 */
Andrew Walbrane52006c2019-10-22 18:01:28 +0100108 r->lazy.mdscr_el1 = 0x0U & ~(0x1U << 15);
Fuad Tabbac76466d2019-09-06 10:42:12 +0100109
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100110 /* Disable cycle counting on initialization. */
111 r->lazy.pmccfiltr_el0 = perfmon_get_pmccfiltr_el0_init_value(vm_id);
112
Andrew Walbranb208b4a2019-05-20 12:42:22 +0100113 gic_regs_reset(r, is_primary);
Andrew Scull11a4a0c2018-12-29 11:38:31 +0000114}
115
116void arch_regs_set_pc_arg(struct arch_regs *r, ipaddr_t pc, uintreg_t arg)
117{
118 r->pc = ipa_addr(pc);
119 r->r[0] = arg;
120}
121
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100122void arch_regs_set_retval(struct arch_regs *r, struct spci_value v)
Andrew Scull11a4a0c2018-12-29 11:38:31 +0000123{
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100124 r->r[0] = v.func;
125 r->r[1] = v.arg1;
126 r->r[2] = v.arg2;
127 r->r[3] = v.arg3;
128 r->r[4] = v.arg4;
129 r->r[5] = v.arg5;
130 r->r[6] = v.arg6;
131 r->r[7] = v.arg7;
Andrew Scull11a4a0c2018-12-29 11:38:31 +0000132}
Fuad Tabbac8eede32019-10-31 11:17:50 +0000133
134void arch_cpu_init(void)
135{
136 /*
137 * Linux expects LORegions to be disabled, hence if the current system
138 * supports them, Hafnium ensures that they are disabled.
139 */
140 lor_disable();
Fuad Tabba2e2c98b2019-11-04 14:37:24 +0000141
142 write_msr(CPTR_EL2, get_cptr_el2_value());
Fuad Tabbac8eede32019-10-31 11:17:50 +0000143}