Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 1 | /* |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 2 | * Copyright 2018 The Hafnium Authors. |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 3 | * |
| 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 Scull | 9a6384b | 2019-01-02 12:08:40 +0000 | [diff] [blame] | 17 | #include "hf/arch/cpu.h" |
| 18 | |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 19 | #include <stdbool.h> |
| 20 | #include <stddef.h> |
| 21 | #include <stdint.h> |
| 22 | |
Andrew Scull | 550d99b | 2020-02-10 13:55:00 +0000 | [diff] [blame^] | 23 | #include "hf/arch/plat/psci.h" |
| 24 | |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 25 | #include "hf/addr.h" |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 26 | #include "hf/spci.h" |
Andrew Scull | 8d9e121 | 2019-04-05 13:52:55 +0100 | [diff] [blame] | 27 | #include "hf/std.h" |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 28 | #include "hf/vm.h" |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 29 | |
Fuad Tabba | 77a4b01 | 2019-11-15 12:13:08 +0000 | [diff] [blame] | 30 | #include "feature_id.h" |
Fuad Tabba | c8eede3 | 2019-10-31 11:17:50 +0000 | [diff] [blame] | 31 | #include "msr.h" |
Andrew Walbran | 42d89e7 | 2019-11-27 12:40:10 +0000 | [diff] [blame] | 32 | #include "perfmon.h" |
| 33 | #include "sysregs.h" |
Fuad Tabba | c8eede3 | 2019-10-31 11:17:50 +0000 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * The LO field indicates whether LORegions are supported. |
| 37 | */ |
| 38 | #define ID_AA64MMFR1_EL1_LO (UINT64_C(1) << 16) |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 39 | |
Fuad Tabba | c8eede3 | 2019-10-31 11:17:50 +0000 | [diff] [blame] | 40 | static void lor_disable(void) |
| 41 | { |
| 42 | /* |
| 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 | } |
| 48 | } |
| 49 | |
Andrew Walbran | b208b4a | 2019-05-20 12:42:22 +0100 | [diff] [blame] | 50 | static void gic_regs_reset(struct arch_regs *r, bool is_primary) |
| 51 | { |
| 52 | #if GIC_VERSION == 3 || GIC_VERSION == 4 |
| 53 | uint32_t ich_hcr = 0; |
Andrew Walbran | 4b976f4 | 2019-06-05 15:00:50 +0100 | [diff] [blame] | 54 | uint32_t icc_sre_el2 = |
Andrew Walbran | e52006c | 2019-10-22 18:01:28 +0100 | [diff] [blame] | 55 | (1U << 0) | /* SRE, enable ICH_* and ICC_* at EL2. */ |
Andrew Walbran | 4b976f4 | 2019-06-05 15:00:50 +0100 | [diff] [blame] | 56 | (0x3 << 1); /* DIB and DFB, disable IRQ/FIQ bypass. */ |
Andrew Walbran | b208b4a | 2019-05-20 12:42:22 +0100 | [diff] [blame] | 57 | |
Andrew Walbran | 4b976f4 | 2019-06-05 15:00:50 +0100 | [diff] [blame] | 58 | if (is_primary) { |
Andrew Walbran | e52006c | 2019-10-22 18:01:28 +0100 | [diff] [blame] | 59 | icc_sre_el2 |= 1U << 3; /* Enable EL1 access to ICC_SRE_EL1. */ |
Andrew Walbran | 4b976f4 | 2019-06-05 15:00:50 +0100 | [diff] [blame] | 60 | } else { |
Andrew Walbran | b208b4a | 2019-05-20 12:42:22 +0100 | [diff] [blame] | 61 | /* Trap EL1 access to GICv3 system registers. */ |
| 62 | ich_hcr = |
Andrew Walbran | e52006c | 2019-10-22 18:01:28 +0100 | [diff] [blame] | 63 | (0x1fU << 10); /* TDIR, TSEI, TALL1, TALL0, TC bits. */ |
Andrew Walbran | b208b4a | 2019-05-20 12:42:22 +0100 | [diff] [blame] | 64 | } |
| 65 | r->gic.ich_hcr_el2 = ich_hcr; |
Andrew Walbran | 4b976f4 | 2019-06-05 15:00:50 +0100 | [diff] [blame] | 66 | r->gic.icc_sre_el2 = icc_sre_el2; |
Andrew Walbran | b208b4a | 2019-05-20 12:42:22 +0100 | [diff] [blame] | 67 | #endif |
| 68 | } |
| 69 | |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 70 | void arch_regs_reset(struct vcpu *vcpu) |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 71 | { |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 72 | spci_vm_id_t vm_id = vcpu->vm->id; |
| 73 | bool is_primary = vm_id == HF_PRIMARY_VM_ID; |
Mahesh Bireddy | 86808c2 | 2020-01-07 12:13:29 +0530 | [diff] [blame] | 74 | cpu_id_t vcpu_id = is_primary ? vcpu->cpu->id : vcpu_index(vcpu); |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 75 | paddr_t table = vcpu->vm->ptable.root; |
| 76 | struct arch_regs *r = &vcpu->regs; |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 77 | uintreg_t pc = r->pc; |
| 78 | uintreg_t arg = r->r[0]; |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 79 | uintreg_t cnthctl; |
| 80 | |
Andrew Scull | 2b5fbad | 2019-04-05 13:55:56 +0100 | [diff] [blame] | 81 | memset_s(r, sizeof(*r), 0, sizeof(*r)); |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 82 | |
| 83 | r->pc = pc; |
| 84 | r->r[0] = arg; |
| 85 | |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 86 | cnthctl = 0; |
| 87 | |
| 88 | if (is_primary) { |
| 89 | cnthctl |= |
Andrew Walbran | e52006c | 2019-10-22 18:01:28 +0100 | [diff] [blame] | 90 | (1U << 0) | /* EL1PCTEN, don't trap phys cnt access. */ |
| 91 | (1U << 1); /* EL1PCEN, don't trap phys timer access. */ |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Fuad Tabba | 46b8616 | 2019-10-18 13:29:14 +0100 | [diff] [blame] | 94 | r->lazy.hcr_el2 = get_hcr_el2_value(vm_id); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 95 | r->lazy.cnthctl_el2 = cnthctl; |
Andrew Walbran | 9553492 | 2019-06-19 11:32:54 +0100 | [diff] [blame] | 96 | r->lazy.vttbr_el2 = pa_addr(table) | ((uint64_t)vm_id << 48); |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 97 | r->lazy.vmpidr_el2 = vcpu_id; |
Fuad Tabba | 3e9b022 | 2019-11-11 16:47:50 +0000 | [diff] [blame] | 98 | /* Mask (disable) interrupts and run in EL1h mode. */ |
| 99 | r->spsr = PSR_D | PSR_A | PSR_I | PSR_F | PSR_PE_MODE_EL1H; |
Andrew Walbran | b208b4a | 2019-05-20 12:42:22 +0100 | [diff] [blame] | 100 | |
Fuad Tabba | 77a4b01 | 2019-11-15 12:13:08 +0000 | [diff] [blame] | 101 | r->lazy.mdcr_el2 = get_mdcr_el2_value(); |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 102 | |
| 103 | /* |
| 104 | * NOTE: It is important that MDSCR_EL1.MDE (bit 15) is set to 0 for |
| 105 | * secondary VMs as long as Hafnium does not support debug register |
| 106 | * access for secondary VMs. If adding Hafnium support for secondary VM |
| 107 | * debug register accesses, then on context switches Hafnium needs to |
| 108 | * save/restore EL1 debug register state that either might change, or |
| 109 | * that needs to be protected. |
| 110 | */ |
Andrew Walbran | e52006c | 2019-10-22 18:01:28 +0100 | [diff] [blame] | 111 | r->lazy.mdscr_el1 = 0x0U & ~(0x1U << 15); |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 112 | |
Fuad Tabba | f1d6dc5 | 2019-09-18 17:33:14 +0100 | [diff] [blame] | 113 | /* Disable cycle counting on initialization. */ |
| 114 | r->lazy.pmccfiltr_el0 = perfmon_get_pmccfiltr_el0_init_value(vm_id); |
| 115 | |
Fuad Tabba | 77a4b01 | 2019-11-15 12:13:08 +0000 | [diff] [blame] | 116 | /* Set feature-specific register values. */ |
| 117 | feature_set_traps(vcpu->vm, r); |
| 118 | |
Andrew Walbran | b208b4a | 2019-05-20 12:42:22 +0100 | [diff] [blame] | 119 | gic_regs_reset(r, is_primary); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void arch_regs_set_pc_arg(struct arch_regs *r, ipaddr_t pc, uintreg_t arg) |
| 123 | { |
| 124 | r->pc = ipa_addr(pc); |
| 125 | r->r[0] = arg; |
| 126 | } |
| 127 | |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 128 | void arch_regs_set_retval(struct arch_regs *r, struct spci_value v) |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 129 | { |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 130 | r->r[0] = v.func; |
| 131 | r->r[1] = v.arg1; |
| 132 | r->r[2] = v.arg2; |
| 133 | r->r[3] = v.arg3; |
| 134 | r->r[4] = v.arg4; |
| 135 | r->r[5] = v.arg5; |
| 136 | r->r[6] = v.arg6; |
| 137 | r->r[7] = v.arg7; |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 138 | } |
Fuad Tabba | c8eede3 | 2019-10-31 11:17:50 +0000 | [diff] [blame] | 139 | |
| 140 | void arch_cpu_init(void) |
| 141 | { |
Andrew Scull | 550d99b | 2020-02-10 13:55:00 +0000 | [diff] [blame^] | 142 | plat_psci_cpu_resume(); |
| 143 | |
Fuad Tabba | c8eede3 | 2019-10-31 11:17:50 +0000 | [diff] [blame] | 144 | /* |
| 145 | * Linux expects LORegions to be disabled, hence if the current system |
| 146 | * supports them, Hafnium ensures that they are disabled. |
| 147 | */ |
| 148 | lor_disable(); |
Fuad Tabba | 2e2c98b | 2019-11-04 14:37:24 +0000 | [diff] [blame] | 149 | |
| 150 | write_msr(CPTR_EL2, get_cptr_el2_value()); |
Mahesh Bireddy | ef3c3cd | 2020-01-07 12:26:38 +0530 | [diff] [blame] | 151 | |
| 152 | /* Initialize counter-timer virtual offset register to 0. */ |
| 153 | write_msr(CNTVOFF_EL2, 0); |
Fuad Tabba | c8eede3 | 2019-10-31 11:17:50 +0000 | [diff] [blame] | 154 | } |