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 | |
| 23 | #include "hf/addr.h" |
Andrew Scull | 8d9e121 | 2019-04-05 13:52:55 +0100 | [diff] [blame] | 24 | #include "hf/std.h" |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 25 | |
| 26 | void arch_irq_disable(void) |
| 27 | { |
| 28 | __asm__ volatile("msr DAIFSet, #0xf"); |
| 29 | } |
| 30 | |
| 31 | void arch_irq_enable(void) |
| 32 | { |
| 33 | __asm__ volatile("msr DAIFClr, #0xf"); |
| 34 | } |
| 35 | |
Andrew Walbran | b208b4a | 2019-05-20 12:42:22 +0100 | [diff] [blame] | 36 | static void gic_regs_reset(struct arch_regs *r, bool is_primary) |
| 37 | { |
| 38 | #if GIC_VERSION == 3 || GIC_VERSION == 4 |
| 39 | uint32_t ich_hcr = 0; |
Andrew Walbran | 4b976f4 | 2019-06-05 15:00:50 +0100 | [diff] [blame^] | 40 | uint32_t icc_sre_el2 = |
| 41 | (1u << 0) | /* SRE, enable ICH_* and ICC_* at EL2. */ |
| 42 | (0x3 << 1); /* DIB and DFB, disable IRQ/FIQ bypass. */ |
Andrew Walbran | b208b4a | 2019-05-20 12:42:22 +0100 | [diff] [blame] | 43 | |
Andrew Walbran | 4b976f4 | 2019-06-05 15:00:50 +0100 | [diff] [blame^] | 44 | if (is_primary) { |
| 45 | icc_sre_el2 |= 1u << 3; /* Enable EL1 access to ICC_SRE_EL1. */ |
| 46 | } else { |
Andrew Walbran | b208b4a | 2019-05-20 12:42:22 +0100 | [diff] [blame] | 47 | /* Trap EL1 access to GICv3 system registers. */ |
| 48 | ich_hcr = |
| 49 | (0x1fu << 10); /* TDIR, TSEI, TALL1, TALL0, TC bits. */ |
| 50 | } |
| 51 | r->gic.ich_hcr_el2 = ich_hcr; |
Andrew Walbran | 4b976f4 | 2019-06-05 15:00:50 +0100 | [diff] [blame^] | 52 | r->gic.icc_sre_el2 = icc_sre_el2; |
Andrew Walbran | b208b4a | 2019-05-20 12:42:22 +0100 | [diff] [blame] | 53 | #endif |
| 54 | } |
| 55 | |
Andrew Walbran | 9553492 | 2019-06-19 11:32:54 +0100 | [diff] [blame] | 56 | void arch_regs_reset(struct arch_regs *r, bool is_primary, spci_vm_id_t vm_id, |
Andrew Walbran | 4d3fa28 | 2019-06-26 13:31:15 +0100 | [diff] [blame] | 57 | cpu_id_t vcpu_id, paddr_t table) |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 58 | { |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 59 | uintreg_t pc = r->pc; |
| 60 | uintreg_t arg = r->r[0]; |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 61 | uintreg_t hcr; |
| 62 | uintreg_t cptr; |
| 63 | uintreg_t cnthctl; |
| 64 | |
Andrew Scull | 2b5fbad | 2019-04-05 13:55:56 +0100 | [diff] [blame] | 65 | memset_s(r, sizeof(*r), 0, sizeof(*r)); |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 66 | |
| 67 | r->pc = pc; |
| 68 | r->r[0] = arg; |
| 69 | |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 70 | /* TODO: Determine if we need to set TSW. */ |
| 71 | hcr = (1u << 31) | /* RW bit. */ |
| 72 | (1u << 21) | /* TACR, trap access to ACTRL_EL1. */ |
| 73 | (1u << 19) | /* TSC, trap SMC instructions. */ |
| 74 | (1u << 20) | /* TIDCP, trap impl-defined funct. */ |
| 75 | (1u << 2) | /* PTW, Protected Table Walk. */ |
| 76 | (1u << 0); /* VM: enable stage-2 translation. */ |
| 77 | |
| 78 | cptr = 0; |
| 79 | cnthctl = 0; |
| 80 | |
| 81 | if (is_primary) { |
| 82 | cnthctl |= |
| 83 | (1u << 0) | /* EL1PCTEN, don't trap phys cnt access. */ |
| 84 | (1u << 1); /* EL1PCEN, don't trap phys timer access. */ |
| 85 | } else { |
| 86 | hcr |= (7u << 3) | /* AMO, IMO, FMO bits. */ |
| 87 | (1u << 9) | /* FB bit. */ |
| 88 | (1u << 10) | /* BSU bits set to inner-sh. */ |
| 89 | (3u << 13); /* TWI, TWE bits. */ |
| 90 | |
Conrad Grobler | a824af6 | 2019-03-22 17:33:23 +0000 | [diff] [blame] | 91 | /* TODO: Trap fp access once handler logic is in place. */ |
| 92 | |
| 93 | /* TODO: Investigate fpexc32_el2 for 32bit EL0 support. */ |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | r->lazy.hcr_el2 = hcr; |
| 97 | r->lazy.cptr_el2 = cptr; |
| 98 | r->lazy.cnthctl_el2 = cnthctl; |
Andrew Walbran | 9553492 | 2019-06-19 11:32:54 +0100 | [diff] [blame] | 99 | r->lazy.vttbr_el2 = pa_addr(table) | ((uint64_t)vm_id << 48); |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 100 | r->lazy.vmpidr_el2 = vcpu_id; |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 101 | /* TODO: Use constant here. */ |
| 102 | r->spsr = 5 | /* M bits, set to EL1h. */ |
| 103 | (0xf << 6); /* DAIF bits set; disable interrupts. */ |
Andrew Walbran | b208b4a | 2019-05-20 12:42:22 +0100 | [diff] [blame] | 104 | |
| 105 | gic_regs_reset(r, is_primary); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void arch_regs_set_pc_arg(struct arch_regs *r, ipaddr_t pc, uintreg_t arg) |
| 109 | { |
| 110 | r->pc = ipa_addr(pc); |
| 111 | r->r[0] = arg; |
| 112 | } |
| 113 | |
| 114 | void arch_regs_set_retval(struct arch_regs *r, uintreg_t v) |
| 115 | { |
| 116 | r->r[0] = v; |
| 117 | } |