blob: 5993841e97f08bb81d9e59057eebe5f78c92a492 [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 *
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 Scull9a6384b2019-01-02 12:08:40 +000017#include "hf/arch/cpu.h"
18
Andrew Scull11a4a0c2018-12-29 11:38:31 +000019#include <stdbool.h>
20#include <stddef.h>
21#include <stdint.h>
22
23#include "hf/addr.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010024#include "hf/std.h"
Andrew Scull11a4a0c2018-12-29 11:38:31 +000025
26void arch_irq_disable(void)
27{
28 __asm__ volatile("msr DAIFSet, #0xf");
29}
30
31void arch_irq_enable(void)
32{
33 __asm__ volatile("msr DAIFClr, #0xf");
34}
35
Andrew Walbranb208b4a2019-05-20 12:42:22 +010036static 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;
40
41 if (!is_primary) {
42 /* Trap EL1 access to GICv3 system registers. */
43 ich_hcr =
44 (0x1fu << 10); /* TDIR, TSEI, TALL1, TALL0, TC bits. */
45 }
46 r->gic.ich_hcr_el2 = ich_hcr;
47#endif
48}
49
Andrew Walbran95534922019-06-19 11:32:54 +010050void arch_regs_reset(struct arch_regs *r, bool is_primary, spci_vm_id_t vm_id,
Andrew Walbran4d3fa282019-06-26 13:31:15 +010051 cpu_id_t vcpu_id, paddr_t table)
Andrew Scull11a4a0c2018-12-29 11:38:31 +000052{
Andrew Scullc960c032018-10-24 15:13:35 +010053 uintreg_t pc = r->pc;
54 uintreg_t arg = r->r[0];
Andrew Scull11a4a0c2018-12-29 11:38:31 +000055 uintreg_t hcr;
56 uintreg_t cptr;
57 uintreg_t cnthctl;
58
Andrew Scull2b5fbad2019-04-05 13:55:56 +010059 memset_s(r, sizeof(*r), 0, sizeof(*r));
Andrew Scullc960c032018-10-24 15:13:35 +010060
61 r->pc = pc;
62 r->r[0] = arg;
63
Andrew Scull11a4a0c2018-12-29 11:38:31 +000064 /* TODO: Determine if we need to set TSW. */
65 hcr = (1u << 31) | /* RW bit. */
66 (1u << 21) | /* TACR, trap access to ACTRL_EL1. */
67 (1u << 19) | /* TSC, trap SMC instructions. */
68 (1u << 20) | /* TIDCP, trap impl-defined funct. */
69 (1u << 2) | /* PTW, Protected Table Walk. */
70 (1u << 0); /* VM: enable stage-2 translation. */
71
72 cptr = 0;
73 cnthctl = 0;
74
75 if (is_primary) {
76 cnthctl |=
77 (1u << 0) | /* EL1PCTEN, don't trap phys cnt access. */
78 (1u << 1); /* EL1PCEN, don't trap phys timer access. */
79 } else {
80 hcr |= (7u << 3) | /* AMO, IMO, FMO bits. */
81 (1u << 9) | /* FB bit. */
82 (1u << 10) | /* BSU bits set to inner-sh. */
83 (3u << 13); /* TWI, TWE bits. */
84
Conrad Groblera824af62019-03-22 17:33:23 +000085 /* TODO: Trap fp access once handler logic is in place. */
86
87 /* TODO: Investigate fpexc32_el2 for 32bit EL0 support. */
Andrew Scull11a4a0c2018-12-29 11:38:31 +000088 }
89
90 r->lazy.hcr_el2 = hcr;
91 r->lazy.cptr_el2 = cptr;
92 r->lazy.cnthctl_el2 = cnthctl;
Andrew Walbran95534922019-06-19 11:32:54 +010093 r->lazy.vttbr_el2 = pa_addr(table) | ((uint64_t)vm_id << 48);
Andrew Scullbb3ab6c2018-11-26 20:38:49 +000094 r->lazy.vmpidr_el2 = vcpu_id;
Andrew Scull11a4a0c2018-12-29 11:38:31 +000095 /* TODO: Use constant here. */
96 r->spsr = 5 | /* M bits, set to EL1h. */
97 (0xf << 6); /* DAIF bits set; disable interrupts. */
Andrew Walbranb208b4a2019-05-20 12:42:22 +010098
99 gic_regs_reset(r, is_primary);
Andrew Scull11a4a0c2018-12-29 11:38:31 +0000100}
101
102void arch_regs_set_pc_arg(struct arch_regs *r, ipaddr_t pc, uintreg_t arg)
103{
104 r->pc = ipa_addr(pc);
105 r->r[0] = arg;
106}
107
108void arch_regs_set_retval(struct arch_regs *r, uintreg_t v)
109{
110 r->r[0] = v;
111}