blob: e8048c380562a51e57c7ac4a1e2a8a537fd413cc [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"
Andrew Scull8d9e1212019-04-05 13:52:55 +010019#include "hf/std.h"
Fuad Tabba5c738432019-12-02 11:02:42 +000020#include "hf/vm.h"
Andrew Scull11a4a0c2018-12-29 11:38:31 +000021
Fuad Tabba77a4b012019-11-15 12:13:08 +000022#include "feature_id.h"
Fuad Tabbac8eede32019-10-31 11:17:50 +000023#include "msr.h"
Andrew Walbran42d89e72019-11-27 12:40:10 +000024#include "perfmon.h"
25#include "sysregs.h"
Fuad Tabbac8eede32019-10-31 11:17:50 +000026
27/**
28 * The LO field indicates whether LORegions are supported.
29 */
30#define ID_AA64MMFR1_EL1_LO (UINT64_C(1) << 16)
Fuad Tabbac76466d2019-09-06 10:42:12 +010031
Fuad Tabbac8eede32019-10-31 11:17:50 +000032static void lor_disable(void)
33{
34 /*
35 * Accesses to LORC_EL1 are undefined if LORegions are not supported.
36 */
37 if (read_msr(ID_AA64MMFR1_EL1) & ID_AA64MMFR1_EL1_LO) {
38 write_msr(MSR_LORC_EL1, 0);
39 }
40}
41
Andrew Walbranb208b4a2019-05-20 12:42:22 +010042static void gic_regs_reset(struct arch_regs *r, bool is_primary)
43{
44#if GIC_VERSION == 3 || GIC_VERSION == 4
45 uint32_t ich_hcr = 0;
Andrew Walbran4b976f42019-06-05 15:00:50 +010046 uint32_t icc_sre_el2 =
Andrew Walbrane52006c2019-10-22 18:01:28 +010047 (1U << 0) | /* SRE, enable ICH_* and ICC_* at EL2. */
Andrew Walbran4b976f42019-06-05 15:00:50 +010048 (0x3 << 1); /* DIB and DFB, disable IRQ/FIQ bypass. */
Andrew Walbranb208b4a2019-05-20 12:42:22 +010049
Andrew Walbran4b976f42019-06-05 15:00:50 +010050 if (is_primary) {
Andrew Walbrane52006c2019-10-22 18:01:28 +010051 icc_sre_el2 |= 1U << 3; /* Enable EL1 access to ICC_SRE_EL1. */
Andrew Walbran4b976f42019-06-05 15:00:50 +010052 } else {
Andrew Walbranb208b4a2019-05-20 12:42:22 +010053 /* Trap EL1 access to GICv3 system registers. */
54 ich_hcr =
Andrew Walbrane52006c2019-10-22 18:01:28 +010055 (0x1fU << 10); /* TDIR, TSEI, TALL1, TALL0, TC bits. */
Andrew Walbranb208b4a2019-05-20 12:42:22 +010056 }
57 r->gic.ich_hcr_el2 = ich_hcr;
Andrew Walbran4b976f42019-06-05 15:00:50 +010058 r->gic.icc_sre_el2 = icc_sre_el2;
Andrew Walbranb208b4a2019-05-20 12:42:22 +010059#endif
60}
61
Fuad Tabba5c738432019-12-02 11:02:42 +000062void arch_regs_reset(struct vcpu *vcpu)
Andrew Scull11a4a0c2018-12-29 11:38:31 +000063{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010064 ffa_vm_id_t vm_id = vcpu->vm->id;
Fuad Tabba5c738432019-12-02 11:02:42 +000065 bool is_primary = vm_id == HF_PRIMARY_VM_ID;
Mahesh Bireddy86808c22020-01-07 12:13:29 +053066 cpu_id_t vcpu_id = is_primary ? vcpu->cpu->id : vcpu_index(vcpu);
Fuad Tabba5c738432019-12-02 11:02:42 +000067 paddr_t table = vcpu->vm->ptable.root;
68 struct arch_regs *r = &vcpu->regs;
Andrew Scullc960c032018-10-24 15:13:35 +010069 uintreg_t pc = r->pc;
70 uintreg_t arg = r->r[0];
Andrew Scull11a4a0c2018-12-29 11:38:31 +000071 uintreg_t cnthctl;
72
Andrew Scull2b5fbad2019-04-05 13:55:56 +010073 memset_s(r, sizeof(*r), 0, sizeof(*r));
Andrew Scullc960c032018-10-24 15:13:35 +010074
75 r->pc = pc;
76 r->r[0] = arg;
77
Andrew Scull11a4a0c2018-12-29 11:38:31 +000078 cnthctl = 0;
79
80 if (is_primary) {
81 cnthctl |=
Andrew Walbrane52006c2019-10-22 18:01:28 +010082 (1U << 0) | /* EL1PCTEN, don't trap phys cnt access. */
83 (1U << 1); /* EL1PCEN, don't trap phys timer access. */
Andrew Scull11a4a0c2018-12-29 11:38:31 +000084 }
85
Fuad Tabba46b86162019-10-18 13:29:14 +010086 r->lazy.hcr_el2 = get_hcr_el2_value(vm_id);
Andrew Scull11a4a0c2018-12-29 11:38:31 +000087 r->lazy.cnthctl_el2 = cnthctl;
Andrew Walbran95534922019-06-19 11:32:54 +010088 r->lazy.vttbr_el2 = pa_addr(table) | ((uint64_t)vm_id << 48);
Andrew Scullbb3ab6c2018-11-26 20:38:49 +000089 r->lazy.vmpidr_el2 = vcpu_id;
Fuad Tabba3e9b0222019-11-11 16:47:50 +000090 /* Mask (disable) interrupts and run in EL1h mode. */
91 r->spsr = PSR_D | PSR_A | PSR_I | PSR_F | PSR_PE_MODE_EL1H;
Andrew Walbranb208b4a2019-05-20 12:42:22 +010092
Fuad Tabba77a4b012019-11-15 12:13:08 +000093 r->lazy.mdcr_el2 = get_mdcr_el2_value();
Fuad Tabbac76466d2019-09-06 10:42:12 +010094
95 /*
96 * NOTE: It is important that MDSCR_EL1.MDE (bit 15) is set to 0 for
97 * secondary VMs as long as Hafnium does not support debug register
98 * access for secondary VMs. If adding Hafnium support for secondary VM
99 * debug register accesses, then on context switches Hafnium needs to
100 * save/restore EL1 debug register state that either might change, or
101 * that needs to be protected.
102 */
Andrew Walbrane52006c2019-10-22 18:01:28 +0100103 r->lazy.mdscr_el1 = 0x0U & ~(0x1U << 15);
Fuad Tabbac76466d2019-09-06 10:42:12 +0100104
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100105 /* Disable cycle counting on initialization. */
106 r->lazy.pmccfiltr_el0 = perfmon_get_pmccfiltr_el0_init_value(vm_id);
107
Fuad Tabba77a4b012019-11-15 12:13:08 +0000108 /* Set feature-specific register values. */
109 feature_set_traps(vcpu->vm, r);
110
Andrew Walbranb208b4a2019-05-20 12:42:22 +0100111 gic_regs_reset(r, is_primary);
Andrew Scull11a4a0c2018-12-29 11:38:31 +0000112}
113
114void arch_regs_set_pc_arg(struct arch_regs *r, ipaddr_t pc, uintreg_t arg)
115{
116 r->pc = ipa_addr(pc);
117 r->r[0] = arg;
118}
119
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100120void arch_regs_set_retval(struct arch_regs *r, struct ffa_value v)
Andrew Scull11a4a0c2018-12-29 11:38:31 +0000121{
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100122 r->r[0] = v.func;
123 r->r[1] = v.arg1;
124 r->r[2] = v.arg2;
125 r->r[3] = v.arg3;
126 r->r[4] = v.arg4;
127 r->r[5] = v.arg5;
128 r->r[6] = v.arg6;
129 r->r[7] = v.arg7;
Andrew Scull11a4a0c2018-12-29 11:38:31 +0000130}
Fuad Tabbac8eede32019-10-31 11:17:50 +0000131
132void arch_cpu_init(void)
133{
Andrew Scull550d99b2020-02-10 13:55:00 +0000134 plat_psci_cpu_resume();
135
Fuad Tabbac8eede32019-10-31 11:17:50 +0000136 /*
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());
Mahesh Bireddyef3c3cd2020-01-07 12:26:38 +0530143
144 /* Initialize counter-timer virtual offset register to 0. */
145 write_msr(CNTVOFF_EL2, 0);
Fuad Tabbac8eede32019-10-31 11:17:50 +0000146}