blob: 1ae07967bac05e562326fd2a97619566e2f3a8c9 [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"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010018#include "hf/ffa.h"
Madhukar Pappireddy72454a12021-08-03 12:21:46 -050019#include "hf/plat/interrupts.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010020#include "hf/std.h"
Fuad Tabba5c738432019-12-02 11:02:42 +000021#include "hf/vm.h"
Andrew Scull11a4a0c2018-12-29 11:38:31 +000022
Fuad Tabba77a4b012019-11-15 12:13:08 +000023#include "feature_id.h"
Fuad Tabbac8eede32019-10-31 11:17:50 +000024#include "msr.h"
Andrew Walbran42d89e72019-11-27 12:40:10 +000025#include "perfmon.h"
26#include "sysregs.h"
Fuad Tabbac8eede32019-10-31 11:17:50 +000027
Olivier Depreze7d7f322020-12-14 16:01:03 +010028#if BRANCH_PROTECTION
29
30__uint128_t pauth_apia_key;
31
32#endif
33
Fuad Tabbac8eede32019-10-31 11:17:50 +000034/**
35 * The LO field indicates whether LORegions are supported.
36 */
37#define ID_AA64MMFR1_EL1_LO (UINT64_C(1) << 16)
Fuad Tabbac76466d2019-09-06 10:42:12 +010038
Fuad Tabbac8eede32019-10-31 11:17:50 +000039static void lor_disable(void)
40{
Jose Marinhocc071f12019-11-08 14:42:16 +000041#if SECURE_WORLD == 0
Fuad Tabbac8eede32019-10-31 11:17:50 +000042 /*
43 * Accesses to LORC_EL1 are undefined if LORegions are not supported.
44 */
45 if (read_msr(ID_AA64MMFR1_EL1) & ID_AA64MMFR1_EL1_LO) {
46 write_msr(MSR_LORC_EL1, 0);
47 }
Jose Marinhocc071f12019-11-08 14:42:16 +000048#endif
Fuad Tabbac8eede32019-10-31 11:17:50 +000049}
50
Andrew Walbranb208b4a2019-05-20 12:42:22 +010051static void gic_regs_reset(struct arch_regs *r, bool is_primary)
52{
53#if GIC_VERSION == 3 || GIC_VERSION == 4
54 uint32_t ich_hcr = 0;
Andrew Walbran4b976f42019-06-05 15:00:50 +010055 uint32_t icc_sre_el2 =
Andrew Walbrane52006c2019-10-22 18:01:28 +010056 (1U << 0) | /* SRE, enable ICH_* and ICC_* at EL2. */
Andrew Walbran4b976f42019-06-05 15:00:50 +010057 (0x3 << 1); /* DIB and DFB, disable IRQ/FIQ bypass. */
Andrew Walbranb208b4a2019-05-20 12:42:22 +010058
Andrew Walbran4b976f42019-06-05 15:00:50 +010059 if (is_primary) {
Andrew Walbrane52006c2019-10-22 18:01:28 +010060 icc_sre_el2 |= 1U << 3; /* Enable EL1 access to ICC_SRE_EL1. */
Andrew Walbran4b976f42019-06-05 15:00:50 +010061 } else {
Andrew Walbranb208b4a2019-05-20 12:42:22 +010062 /* Trap EL1 access to GICv3 system registers. */
63 ich_hcr =
Andrew Walbrane52006c2019-10-22 18:01:28 +010064 (0x1fU << 10); /* TDIR, TSEI, TALL1, TALL0, TC bits. */
Andrew Walbranb208b4a2019-05-20 12:42:22 +010065 }
66 r->gic.ich_hcr_el2 = ich_hcr;
Andrew Walbran4b976f42019-06-05 15:00:50 +010067 r->gic.icc_sre_el2 = icc_sre_el2;
Andrew Walbranb208b4a2019-05-20 12:42:22 +010068#endif
69}
70
Fuad Tabba5c738432019-12-02 11:02:42 +000071void arch_regs_reset(struct vcpu *vcpu)
Andrew Scull11a4a0c2018-12-29 11:38:31 +000072{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010073 ffa_vm_id_t vm_id = vcpu->vm->id;
Fuad Tabba5c738432019-12-02 11:02:42 +000074 bool is_primary = vm_id == HF_PRIMARY_VM_ID;
Mahesh Bireddy86808c22020-01-07 12:13:29 +053075 cpu_id_t vcpu_id = is_primary ? vcpu->cpu->id : vcpu_index(vcpu);
J-Alvesb37fd082020-10-22 12:29:21 +010076
Fuad Tabba5c738432019-12-02 11:02:42 +000077 paddr_t table = vcpu->vm->ptable.root;
78 struct arch_regs *r = &vcpu->regs;
Andrew Scullc960c032018-10-24 15:13:35 +010079 uintreg_t pc = r->pc;
80 uintreg_t arg = r->r[0];
Andrew Scull11a4a0c2018-12-29 11:38:31 +000081 uintreg_t cnthctl;
82
Andrew Scull2b5fbad2019-04-05 13:55:56 +010083 memset_s(r, sizeof(*r), 0, sizeof(*r));
Andrew Scullc960c032018-10-24 15:13:35 +010084
85 r->pc = pc;
86 r->r[0] = arg;
87
Andrew Scull11a4a0c2018-12-29 11:38:31 +000088 cnthctl = 0;
89
90 if (is_primary) {
Raghu Krishnamurthy84eefa52021-01-17 09:49:37 -080091 /*
92 * cnthctl_el2 is redefined when VHE is enabled.
93 * EL1PCTEN, don't trap phys cnt access.
94 * EL1PCEN, don't trap phys timer access.
95 */
96 if (has_vhe_support()) {
97 cnthctl |= (1U << 10) | (1U << 11);
98 } else {
99 cnthctl |= (1U << 0) | (1U << 1);
100 }
Andrew Scull11a4a0c2018-12-29 11:38:31 +0000101 }
102
Raghu Krishnamurthy5a13c342021-02-13 15:49:29 -0800103 r->hcr_el2 = get_hcr_el2_value(vm_id, vcpu->vm->el0_partition);
Andrew Scull11a4a0c2018-12-29 11:38:31 +0000104 r->lazy.cnthctl_el2 = cnthctl;
Raghu Krishnamurthy5a13c342021-02-13 15:49:29 -0800105 if (vcpu->vm->el0_partition) {
106 CHECK(has_vhe_support());
107 /*
108 * AArch64 hafnium only uses 8 bit ASIDs at the moment.
109 * TCR_EL2.AS is set to 0, and per the Arm ARM, the upper 8 bits
110 * are ignored and treated as 0. There is no need to mask the
111 * VMID (used as asid) to only 8 bits.
112 */
113 r->ttbr0_el2 = pa_addr(table) | ((uint64_t)vm_id << 48);
114 r->spsr = PSR_PE_MODE_EL0T;
115 } else {
116 r->ttbr0_el2 = read_msr(ttbr0_el2);
117 r->lazy.vttbr_el2 = pa_addr(table) | ((uint64_t)vm_id << 48);
118 r->lazy.vmpidr_el2 = vcpu_id;
119 /* Mask (disable) interrupts and run in EL1h mode. */
120 r->spsr = PSR_D | PSR_A | PSR_I | PSR_F | PSR_PE_MODE_EL1H;
Andrew Walbranb208b4a2019-05-20 12:42:22 +0100121
Raghu Krishnamurthy5a13c342021-02-13 15:49:29 -0800122 r->lazy.mdcr_el2 = get_mdcr_el2_value();
Fuad Tabbac76466d2019-09-06 10:42:12 +0100123
Raghu Krishnamurthy5a13c342021-02-13 15:49:29 -0800124 /*
125 * NOTE: It is important that MDSCR_EL1.MDE (bit 15) is set to 0
126 * for secondary VMs as long as Hafnium does not support debug
127 * register access for secondary VMs. If adding Hafnium support
128 * for secondary VM debug register accesses, then on context
129 * switches Hafnium needs to save/restore EL1 debug register
130 * state that either might change, or that needs to be
131 * protected.
132 */
133 r->lazy.mdscr_el1 = 0x0U & ~(0x1U << 15);
Fuad Tabbac76466d2019-09-06 10:42:12 +0100134
Raghu Krishnamurthy5a13c342021-02-13 15:49:29 -0800135 /* Disable cycle counting on initialization. */
136 r->lazy.pmccfiltr_el0 =
137 perfmon_get_pmccfiltr_el0_init_value(vm_id);
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100138
Raghu Krishnamurthy5a13c342021-02-13 15:49:29 -0800139 /* Set feature-specific register values. */
140 feature_set_traps(vcpu->vm, r);
141 }
Fuad Tabba77a4b012019-11-15 12:13:08 +0000142
Olivier Depreze879e872020-11-12 18:07:24 +0100143#if SECURE_WORLD == 1
144 /*
145 * TODO: Secure Partitions are granted access to the GIC system
146 * registers. This is temporary in waiting the GIC para-virtualized
147 * interface is ready for SP usage. This conditional code here will
148 * then be removed.
149 */
150 gic_regs_reset(r, true);
151#else
Andrew Walbranb208b4a2019-05-20 12:42:22 +0100152 gic_regs_reset(r, is_primary);
Olivier Depreze879e872020-11-12 18:07:24 +0100153#endif
Andrew Scull11a4a0c2018-12-29 11:38:31 +0000154}
155
156void arch_regs_set_pc_arg(struct arch_regs *r, ipaddr_t pc, uintreg_t arg)
157{
158 r->pc = ipa_addr(pc);
159 r->r[0] = arg;
160}
161
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100162void arch_regs_set_retval(struct arch_regs *r, struct ffa_value v)
Andrew Scull11a4a0c2018-12-29 11:38:31 +0000163{
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100164 r->r[0] = v.func;
165 r->r[1] = v.arg1;
166 r->r[2] = v.arg2;
167 r->r[3] = v.arg3;
168 r->r[4] = v.arg4;
169 r->r[5] = v.arg5;
170 r->r[6] = v.arg6;
171 r->r[7] = v.arg7;
Andrew Scull11a4a0c2018-12-29 11:38:31 +0000172}
Fuad Tabbac8eede32019-10-31 11:17:50 +0000173
Andrew Walbrand8d3f5d2020-10-07 18:23:01 +0100174struct ffa_value arch_regs_get_args(struct arch_regs *regs)
175{
176 return (struct ffa_value){
177 .func = regs->r[0],
178 .arg1 = regs->r[1],
179 .arg2 = regs->r[2],
180 .arg3 = regs->r[3],
181 .arg4 = regs->r[4],
182 .arg5 = regs->r[5],
183 .arg6 = regs->r[6],
184 .arg7 = regs->r[7],
185 };
186}
187
Olivier Depreze6f7b9d2021-02-01 11:55:48 +0100188void arch_cpu_init(struct cpu *c, ipaddr_t entry_point)
Fuad Tabbac8eede32019-10-31 11:17:50 +0000189{
Olivier Depreze6f7b9d2021-02-01 11:55:48 +0100190 plat_psci_cpu_resume(c, entry_point);
Andrew Scull550d99b2020-02-10 13:55:00 +0000191
Fuad Tabbac8eede32019-10-31 11:17:50 +0000192 /*
193 * Linux expects LORegions to be disabled, hence if the current system
194 * supports them, Hafnium ensures that they are disabled.
195 */
196 lor_disable();
Fuad Tabba2e2c98b2019-11-04 14:37:24 +0000197
198 write_msr(CPTR_EL2, get_cptr_el2_value());
Mahesh Bireddyef3c3cd2020-01-07 12:26:38 +0530199
200 /* Initialize counter-timer virtual offset register to 0. */
201 write_msr(CNTVOFF_EL2, 0);
Madhukar Pappireddy72454a12021-08-03 12:21:46 -0500202
203 plat_interrupts_controller_hw_init(c);
Fuad Tabbac8eede32019-10-31 11:17:50 +0000204}