blob: bcf5ffcefdc358e39c385bad14c4c6962268fb0d [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 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Andrew Scull11a4a0c2018-12-29 11:38:31 +00007 */
8
Andrew Scull9a6384b2019-01-02 12:08:40 +00009#include "hf/arch/cpu.h"
10
Andrew Scull11a4a0c2018-12-29 11:38:31 +000011#include <stdbool.h>
12#include <stddef.h>
13#include <stdint.h>
14
Andrew Scull550d99b2020-02-10 13:55:00 +000015#include "hf/arch/plat/psci.h"
16
Andrew Scull11a4a0c2018-12-29 11:38:31 +000017#include "hf/addr.h"
Daniel Boulby84350712021-11-26 11:13:20 +000018#include "hf/check.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010019#include "hf/ffa.h"
Madhukar Pappireddy72454a12021-08-03 12:21:46 -050020#include "hf/plat/interrupts.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010021#include "hf/std.h"
Fuad Tabba5c738432019-12-02 11:02:42 +000022#include "hf/vm.h"
Andrew Scull11a4a0c2018-12-29 11:38:31 +000023
Fuad Tabba77a4b012019-11-15 12:13:08 +000024#include "feature_id.h"
Fuad Tabbac8eede32019-10-31 11:17:50 +000025#include "msr.h"
Andrew Walbran42d89e72019-11-27 12:40:10 +000026#include "perfmon.h"
27#include "sysregs.h"
Fuad Tabbac8eede32019-10-31 11:17:50 +000028
Olivier Depreze7d7f322020-12-14 16:01:03 +010029#if BRANCH_PROTECTION
30
31__uint128_t pauth_apia_key;
32
33#endif
34
Maksims Svecovs6a0ccc92022-03-04 15:16:55 +000035#if ENABLE_MTE
36
37/* MTE hypervisor seed. */
38uintptr_t mte_seed;
39
40#endif
41
Fuad Tabbac8eede32019-10-31 11:17:50 +000042/**
43 * The LO field indicates whether LORegions are supported.
44 */
45#define ID_AA64MMFR1_EL1_LO (UINT64_C(1) << 16)
Fuad Tabbac76466d2019-09-06 10:42:12 +010046
Fuad Tabbac8eede32019-10-31 11:17:50 +000047static void lor_disable(void)
48{
Jose Marinhocc071f12019-11-08 14:42:16 +000049#if SECURE_WORLD == 0
Fuad Tabbac8eede32019-10-31 11:17:50 +000050 /*
51 * Accesses to LORC_EL1 are undefined if LORegions are not supported.
52 */
53 if (read_msr(ID_AA64MMFR1_EL1) & ID_AA64MMFR1_EL1_LO) {
54 write_msr(MSR_LORC_EL1, 0);
55 }
Jose Marinhocc071f12019-11-08 14:42:16 +000056#endif
Fuad Tabbac8eede32019-10-31 11:17:50 +000057}
58
Andrew Walbranb208b4a2019-05-20 12:42:22 +010059static void gic_regs_reset(struct arch_regs *r, bool is_primary)
60{
61#if GIC_VERSION == 3 || GIC_VERSION == 4
62 uint32_t ich_hcr = 0;
Andrew Walbran4b976f42019-06-05 15:00:50 +010063 uint32_t icc_sre_el2 =
Andrew Walbrane52006c2019-10-22 18:01:28 +010064 (1U << 0) | /* SRE, enable ICH_* and ICC_* at EL2. */
Andrew Walbran4b976f42019-06-05 15:00:50 +010065 (0x3 << 1); /* DIB and DFB, disable IRQ/FIQ bypass. */
Andrew Walbranb208b4a2019-05-20 12:42:22 +010066
Andrew Walbran4b976f42019-06-05 15:00:50 +010067 if (is_primary) {
Andrew Walbrane52006c2019-10-22 18:01:28 +010068 icc_sre_el2 |= 1U << 3; /* Enable EL1 access to ICC_SRE_EL1. */
Andrew Walbran4b976f42019-06-05 15:00:50 +010069 } else {
Andrew Walbranb208b4a2019-05-20 12:42:22 +010070 /* Trap EL1 access to GICv3 system registers. */
71 ich_hcr =
Andrew Walbrane52006c2019-10-22 18:01:28 +010072 (0x1fU << 10); /* TDIR, TSEI, TALL1, TALL0, TC bits. */
Andrew Walbranb208b4a2019-05-20 12:42:22 +010073 }
74 r->gic.ich_hcr_el2 = ich_hcr;
Andrew Walbran4b976f42019-06-05 15:00:50 +010075 r->gic.icc_sre_el2 = icc_sre_el2;
Andrew Walbranb208b4a2019-05-20 12:42:22 +010076#endif
77}
78
Fuad Tabba5c738432019-12-02 11:02:42 +000079void arch_regs_reset(struct vcpu *vcpu)
Andrew Scull11a4a0c2018-12-29 11:38:31 +000080{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010081 ffa_vm_id_t vm_id = vcpu->vm->id;
Fuad Tabba5c738432019-12-02 11:02:42 +000082 bool is_primary = vm_id == HF_PRIMARY_VM_ID;
Mahesh Bireddy86808c22020-01-07 12:13:29 +053083 cpu_id_t vcpu_id = is_primary ? vcpu->cpu->id : vcpu_index(vcpu);
J-Alvesb37fd082020-10-22 12:29:21 +010084
Fuad Tabba5c738432019-12-02 11:02:42 +000085 paddr_t table = vcpu->vm->ptable.root;
86 struct arch_regs *r = &vcpu->regs;
Andrew Scullc960c032018-10-24 15:13:35 +010087 uintreg_t pc = r->pc;
88 uintreg_t arg = r->r[0];
Andrew Scull11a4a0c2018-12-29 11:38:31 +000089 uintreg_t cnthctl;
90
Andrew Scull2b5fbad2019-04-05 13:55:56 +010091 memset_s(r, sizeof(*r), 0, sizeof(*r));
Andrew Scullc960c032018-10-24 15:13:35 +010092
93 r->pc = pc;
94 r->r[0] = arg;
95
Andrew Scull11a4a0c2018-12-29 11:38:31 +000096 cnthctl = 0;
97
98 if (is_primary) {
Raghu Krishnamurthy84eefa52021-01-17 09:49:37 -080099 /*
100 * cnthctl_el2 is redefined when VHE is enabled.
101 * EL1PCTEN, don't trap phys cnt access.
102 * EL1PCEN, don't trap phys timer access.
103 */
104 if (has_vhe_support()) {
105 cnthctl |= (1U << 10) | (1U << 11);
106 } else {
107 cnthctl |= (1U << 0) | (1U << 1);
108 }
Andrew Scull11a4a0c2018-12-29 11:38:31 +0000109 }
110
Raghu Krishnamurthy5a13c342021-02-13 15:49:29 -0800111 r->hcr_el2 = get_hcr_el2_value(vm_id, vcpu->vm->el0_partition);
Andrew Scull11a4a0c2018-12-29 11:38:31 +0000112 r->lazy.cnthctl_el2 = cnthctl;
Raghu Krishnamurthy5a13c342021-02-13 15:49:29 -0800113 if (vcpu->vm->el0_partition) {
114 CHECK(has_vhe_support());
115 /*
116 * AArch64 hafnium only uses 8 bit ASIDs at the moment.
117 * TCR_EL2.AS is set to 0, and per the Arm ARM, the upper 8 bits
118 * are ignored and treated as 0. There is no need to mask the
119 * VMID (used as asid) to only 8 bits.
120 */
121 r->ttbr0_el2 = pa_addr(table) | ((uint64_t)vm_id << 48);
122 r->spsr = PSR_PE_MODE_EL0T;
123 } else {
124 r->ttbr0_el2 = read_msr(ttbr0_el2);
Olivier Deprezb7f6bd62022-03-08 10:55:52 +0100125 r->lazy.vtcr_el2 = arch_mm_get_vtcr_el2();
Raghu Krishnamurthy5a13c342021-02-13 15:49:29 -0800126 r->lazy.vttbr_el2 = pa_addr(table) | ((uint64_t)vm_id << 48);
Olivier Deprezb7f6bd62022-03-08 10:55:52 +0100127#if SECURE_WORLD == 1
128 r->lazy.vstcr_el2 = arch_mm_get_vstcr_el2();
129 r->lazy.vsttbr_el2 = pa_addr(table);
130#endif
Raghu Krishnamurthy5a13c342021-02-13 15:49:29 -0800131 r->lazy.vmpidr_el2 = vcpu_id;
132 /* Mask (disable) interrupts and run in EL1h mode. */
133 r->spsr = PSR_D | PSR_A | PSR_I | PSR_F | PSR_PE_MODE_EL1H;
Andrew Walbranb208b4a2019-05-20 12:42:22 +0100134
Raghu Krishnamurthy5a13c342021-02-13 15:49:29 -0800135 r->lazy.mdcr_el2 = get_mdcr_el2_value();
Fuad Tabbac76466d2019-09-06 10:42:12 +0100136
Raghu Krishnamurthy5a13c342021-02-13 15:49:29 -0800137 /*
138 * NOTE: It is important that MDSCR_EL1.MDE (bit 15) is set to 0
139 * for secondary VMs as long as Hafnium does not support debug
140 * register access for secondary VMs. If adding Hafnium support
141 * for secondary VM debug register accesses, then on context
142 * switches Hafnium needs to save/restore EL1 debug register
143 * state that either might change, or that needs to be
144 * protected.
145 */
146 r->lazy.mdscr_el1 = 0x0U & ~(0x1U << 15);
Fuad Tabbac76466d2019-09-06 10:42:12 +0100147
Raghu Krishnamurthy5a13c342021-02-13 15:49:29 -0800148 /* Disable cycle counting on initialization. */
149 r->lazy.pmccfiltr_el0 =
150 perfmon_get_pmccfiltr_el0_init_value(vm_id);
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100151
Raghu Krishnamurthy5a13c342021-02-13 15:49:29 -0800152 /* Set feature-specific register values. */
153 feature_set_traps(vcpu->vm, r);
154 }
Fuad Tabba77a4b012019-11-15 12:13:08 +0000155
Andrew Walbranb208b4a2019-05-20 12:42:22 +0100156 gic_regs_reset(r, is_primary);
Andrew Scull11a4a0c2018-12-29 11:38:31 +0000157}
158
159void arch_regs_set_pc_arg(struct arch_regs *r, ipaddr_t pc, uintreg_t arg)
160{
161 r->pc = ipa_addr(pc);
162 r->r[0] = arg;
163}
164
J-Alvesb7800a12022-01-25 17:55:53 +0000165bool arch_regs_reg_num_valid(const unsigned int gp_reg_num)
166{
167 return gp_reg_num < NUM_GP_REGS;
168}
169
170void arch_regs_set_gp_reg(struct arch_regs *r, const uintreg_t value,
171 const unsigned int gp_reg_num)
172{
173 assert(arch_regs_reg_num_valid(gp_reg_num));
174 r->r[gp_reg_num] = value;
175}
176
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100177void arch_regs_set_retval(struct arch_regs *r, struct ffa_value v)
Andrew Scull11a4a0c2018-12-29 11:38:31 +0000178{
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100179 r->r[0] = v.func;
180 r->r[1] = v.arg1;
181 r->r[2] = v.arg2;
182 r->r[3] = v.arg3;
183 r->r[4] = v.arg4;
184 r->r[5] = v.arg5;
185 r->r[6] = v.arg6;
186 r->r[7] = v.arg7;
Andrew Scull11a4a0c2018-12-29 11:38:31 +0000187}
Fuad Tabbac8eede32019-10-31 11:17:50 +0000188
Andrew Walbrand8d3f5d2020-10-07 18:23:01 +0100189struct ffa_value arch_regs_get_args(struct arch_regs *regs)
190{
191 return (struct ffa_value){
192 .func = regs->r[0],
193 .arg1 = regs->r[1],
194 .arg2 = regs->r[2],
195 .arg3 = regs->r[3],
196 .arg4 = regs->r[4],
197 .arg5 = regs->r[5],
198 .arg6 = regs->r[6],
199 .arg7 = regs->r[7],
200 };
201}
202
Olivier Depreze6f7b9d2021-02-01 11:55:48 +0100203void arch_cpu_init(struct cpu *c, ipaddr_t entry_point)
Fuad Tabbac8eede32019-10-31 11:17:50 +0000204{
Olivier Depreze6f7b9d2021-02-01 11:55:48 +0100205 plat_psci_cpu_resume(c, entry_point);
Andrew Scull550d99b2020-02-10 13:55:00 +0000206
Fuad Tabbac8eede32019-10-31 11:17:50 +0000207 /*
208 * Linux expects LORegions to be disabled, hence if the current system
209 * supports them, Hafnium ensures that they are disabled.
210 */
211 lor_disable();
Fuad Tabba2e2c98b2019-11-04 14:37:24 +0000212
213 write_msr(CPTR_EL2, get_cptr_el2_value());
Mahesh Bireddyef3c3cd2020-01-07 12:26:38 +0530214
215 /* Initialize counter-timer virtual offset register to 0. */
216 write_msr(CNTVOFF_EL2, 0);
Madhukar Pappireddy72454a12021-08-03 12:21:46 -0500217
Raghu Krishnamurthy8a025cb2022-03-03 21:34:23 -0800218 isb();
Madhukar Pappireddy72454a12021-08-03 12:21:46 -0500219 plat_interrupts_controller_hw_init(c);
Fuad Tabbac8eede32019-10-31 11:17:50 +0000220}