167730e6cSSean Christopherson /* SPDX-License-Identifier: GPL-2.0 */
267730e6cSSean Christopherson /*
367730e6cSSean Christopherson * AArch64 processor specific defines
467730e6cSSean Christopherson *
567730e6cSSean Christopherson * Copyright (C) 2018, Red Hat, Inc.
667730e6cSSean Christopherson */
767730e6cSSean Christopherson #ifndef SELFTEST_KVM_PROCESSOR_H
867730e6cSSean Christopherson #define SELFTEST_KVM_PROCESSOR_H
967730e6cSSean Christopherson
1067730e6cSSean Christopherson #include "kvm_util.h"
1167730e6cSSean Christopherson #include "ucall_common.h"
1267730e6cSSean Christopherson
1367730e6cSSean Christopherson #include <linux/stringify.h>
1467730e6cSSean Christopherson #include <linux/types.h>
1567730e6cSSean Christopherson #include <asm/brk-imm.h>
1667730e6cSSean Christopherson #include <asm/esr.h>
1767730e6cSSean Christopherson #include <asm/sysreg.h>
1867730e6cSSean Christopherson
1967730e6cSSean Christopherson
2067730e6cSSean Christopherson #define ARM64_CORE_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U64 | \
2167730e6cSSean Christopherson KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
2267730e6cSSean Christopherson
2367730e6cSSean Christopherson /*
2467730e6cSSean Christopherson * KVM_ARM64_SYS_REG(sys_reg_id): Helper macro to convert
2567730e6cSSean Christopherson * SYS_* register definitions in asm/sysreg.h to use in KVM
2667730e6cSSean Christopherson * calls such as vcpu_get_reg() and vcpu_set_reg().
2767730e6cSSean Christopherson */
2867730e6cSSean Christopherson #define KVM_ARM64_SYS_REG(sys_reg_id) \
2967730e6cSSean Christopherson ARM64_SYS_REG(sys_reg_Op0(sys_reg_id), \
3067730e6cSSean Christopherson sys_reg_Op1(sys_reg_id), \
3167730e6cSSean Christopherson sys_reg_CRn(sys_reg_id), \
3267730e6cSSean Christopherson sys_reg_CRm(sys_reg_id), \
3367730e6cSSean Christopherson sys_reg_Op2(sys_reg_id))
3467730e6cSSean Christopherson
3567730e6cSSean Christopherson /*
3667730e6cSSean Christopherson * Default MAIR
3767730e6cSSean Christopherson * index attribute
3867730e6cSSean Christopherson * DEVICE_nGnRnE 0 0000:0000
3967730e6cSSean Christopherson * DEVICE_nGnRE 1 0000:0100
4067730e6cSSean Christopherson * DEVICE_GRE 2 0000:1100
4167730e6cSSean Christopherson * NORMAL_NC 3 0100:0100
4267730e6cSSean Christopherson * NORMAL 4 1111:1111
4367730e6cSSean Christopherson * NORMAL_WT 5 1011:1011
4467730e6cSSean Christopherson */
4567730e6cSSean Christopherson
4667730e6cSSean Christopherson /* Linux doesn't use these memory types, so let's define them. */
4767730e6cSSean Christopherson #define MAIR_ATTR_DEVICE_GRE UL(0x0c)
4867730e6cSSean Christopherson #define MAIR_ATTR_NORMAL_WT UL(0xbb)
4967730e6cSSean Christopherson
5067730e6cSSean Christopherson #define MT_DEVICE_nGnRnE 0
5167730e6cSSean Christopherson #define MT_DEVICE_nGnRE 1
5267730e6cSSean Christopherson #define MT_DEVICE_GRE 2
5367730e6cSSean Christopherson #define MT_NORMAL_NC 3
5467730e6cSSean Christopherson #define MT_NORMAL 4
5567730e6cSSean Christopherson #define MT_NORMAL_WT 5
5667730e6cSSean Christopherson
5767730e6cSSean Christopherson #define DEFAULT_MAIR_EL1 \
5867730e6cSSean Christopherson (MAIR_ATTRIDX(MAIR_ATTR_DEVICE_nGnRnE, MT_DEVICE_nGnRnE) | \
5967730e6cSSean Christopherson MAIR_ATTRIDX(MAIR_ATTR_DEVICE_nGnRE, MT_DEVICE_nGnRE) | \
6067730e6cSSean Christopherson MAIR_ATTRIDX(MAIR_ATTR_DEVICE_GRE, MT_DEVICE_GRE) | \
6167730e6cSSean Christopherson MAIR_ATTRIDX(MAIR_ATTR_NORMAL_NC, MT_NORMAL_NC) | \
6267730e6cSSean Christopherson MAIR_ATTRIDX(MAIR_ATTR_NORMAL, MT_NORMAL) | \
6367730e6cSSean Christopherson MAIR_ATTRIDX(MAIR_ATTR_NORMAL_WT, MT_NORMAL_WT))
6467730e6cSSean Christopherson
65d8d78398SRaghavendra Rao Ananta /* TCR_EL1 specific flags */
66d8d78398SRaghavendra Rao Ananta #define TCR_T0SZ_OFFSET 0
67d8d78398SRaghavendra Rao Ananta #define TCR_T0SZ(x) ((UL(64) - (x)) << TCR_T0SZ_OFFSET)
68d8d78398SRaghavendra Rao Ananta
69d8d78398SRaghavendra Rao Ananta #define TCR_IRGN0_SHIFT 8
70d8d78398SRaghavendra Rao Ananta #define TCR_IRGN0_MASK (UL(3) << TCR_IRGN0_SHIFT)
71d8d78398SRaghavendra Rao Ananta #define TCR_IRGN0_NC (UL(0) << TCR_IRGN0_SHIFT)
72d8d78398SRaghavendra Rao Ananta #define TCR_IRGN0_WBWA (UL(1) << TCR_IRGN0_SHIFT)
73d8d78398SRaghavendra Rao Ananta #define TCR_IRGN0_WT (UL(2) << TCR_IRGN0_SHIFT)
74d8d78398SRaghavendra Rao Ananta #define TCR_IRGN0_WBnWA (UL(3) << TCR_IRGN0_SHIFT)
75d8d78398SRaghavendra Rao Ananta
76d8d78398SRaghavendra Rao Ananta #define TCR_ORGN0_SHIFT 10
77d8d78398SRaghavendra Rao Ananta #define TCR_ORGN0_MASK (UL(3) << TCR_ORGN0_SHIFT)
78d8d78398SRaghavendra Rao Ananta #define TCR_ORGN0_NC (UL(0) << TCR_ORGN0_SHIFT)
79d8d78398SRaghavendra Rao Ananta #define TCR_ORGN0_WBWA (UL(1) << TCR_ORGN0_SHIFT)
80d8d78398SRaghavendra Rao Ananta #define TCR_ORGN0_WT (UL(2) << TCR_ORGN0_SHIFT)
81d8d78398SRaghavendra Rao Ananta #define TCR_ORGN0_WBnWA (UL(3) << TCR_ORGN0_SHIFT)
82d8d78398SRaghavendra Rao Ananta
83d8d78398SRaghavendra Rao Ananta #define TCR_SH0_SHIFT 12
84d8d78398SRaghavendra Rao Ananta #define TCR_SH0_MASK (UL(3) << TCR_SH0_SHIFT)
85d8d78398SRaghavendra Rao Ananta #define TCR_SH0_INNER (UL(3) << TCR_SH0_SHIFT)
86d8d78398SRaghavendra Rao Ananta
87d8d78398SRaghavendra Rao Ananta #define TCR_TG0_SHIFT 14
88d8d78398SRaghavendra Rao Ananta #define TCR_TG0_MASK (UL(3) << TCR_TG0_SHIFT)
89d8d78398SRaghavendra Rao Ananta #define TCR_TG0_4K (UL(0) << TCR_TG0_SHIFT)
90d8d78398SRaghavendra Rao Ananta #define TCR_TG0_64K (UL(1) << TCR_TG0_SHIFT)
91d8d78398SRaghavendra Rao Ananta #define TCR_TG0_16K (UL(2) << TCR_TG0_SHIFT)
92d8d78398SRaghavendra Rao Ananta
93d8d78398SRaghavendra Rao Ananta #define TCR_IPS_SHIFT 32
94d8d78398SRaghavendra Rao Ananta #define TCR_IPS_MASK (UL(7) << TCR_IPS_SHIFT)
95d8d78398SRaghavendra Rao Ananta #define TCR_IPS_52_BITS (UL(6) << TCR_IPS_SHIFT)
96d8d78398SRaghavendra Rao Ananta #define TCR_IPS_48_BITS (UL(5) << TCR_IPS_SHIFT)
97d8d78398SRaghavendra Rao Ananta #define TCR_IPS_40_BITS (UL(2) << TCR_IPS_SHIFT)
98d8d78398SRaghavendra Rao Ananta #define TCR_IPS_36_BITS (UL(1) << TCR_IPS_SHIFT)
99d8d78398SRaghavendra Rao Ananta
100d8d78398SRaghavendra Rao Ananta #define TCR_HA (UL(1) << 39)
101d8d78398SRaghavendra Rao Ananta #define TCR_DS (UL(1) << 59)
102d8d78398SRaghavendra Rao Ananta
103d8d78398SRaghavendra Rao Ananta /*
104d8d78398SRaghavendra Rao Ananta * AttrIndx[2:0] encoding (mapping attributes defined in the MAIR* registers).
105d8d78398SRaghavendra Rao Ananta */
106d8d78398SRaghavendra Rao Ananta #define PTE_ATTRINDX(t) ((t) << 2)
107d8d78398SRaghavendra Rao Ananta #define PTE_ATTRINDX_MASK GENMASK(4, 2)
108d8d78398SRaghavendra Rao Ananta #define PTE_ATTRINDX_SHIFT 2
109d8d78398SRaghavendra Rao Ananta
110d8d78398SRaghavendra Rao Ananta #define PTE_VALID BIT(0)
111d8d78398SRaghavendra Rao Ananta #define PGD_TYPE_TABLE BIT(1)
112d8d78398SRaghavendra Rao Ananta #define PUD_TYPE_TABLE BIT(1)
113d8d78398SRaghavendra Rao Ananta #define PMD_TYPE_TABLE BIT(1)
114d8d78398SRaghavendra Rao Ananta #define PTE_TYPE_PAGE BIT(1)
115d8d78398SRaghavendra Rao Ananta
116*c8631ea5SRaghavendra Rao Ananta #define PTE_SHARED (UL(3) << 8) /* SH[1:0], inner shareable */
117d8d78398SRaghavendra Rao Ananta #define PTE_AF BIT(10)
118d8d78398SRaghavendra Rao Ananta
119d8d78398SRaghavendra Rao Ananta #define PTE_ADDR_MASK(page_shift) GENMASK(47, (page_shift))
120d8d78398SRaghavendra Rao Ananta #define PTE_ADDR_51_48 GENMASK(15, 12)
121d8d78398SRaghavendra Rao Ananta #define PTE_ADDR_51_48_SHIFT 12
122d8d78398SRaghavendra Rao Ananta #define PTE_ADDR_MASK_LPA2(page_shift) GENMASK(49, (page_shift))
123d8d78398SRaghavendra Rao Ananta #define PTE_ADDR_51_50_LPA2 GENMASK(9, 8)
124d8d78398SRaghavendra Rao Ananta #define PTE_ADDR_51_50_LPA2_SHIFT 8
125d8d78398SRaghavendra Rao Ananta
12667730e6cSSean Christopherson void aarch64_vcpu_setup(struct kvm_vcpu *vcpu, struct kvm_vcpu_init *init);
12767730e6cSSean Christopherson struct kvm_vcpu *aarch64_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
12867730e6cSSean Christopherson struct kvm_vcpu_init *init, void *guest_code);
12967730e6cSSean Christopherson
13067730e6cSSean Christopherson struct ex_regs {
13167730e6cSSean Christopherson u64 regs[31];
13267730e6cSSean Christopherson u64 sp;
13367730e6cSSean Christopherson u64 pc;
13467730e6cSSean Christopherson u64 pstate;
13567730e6cSSean Christopherson };
13667730e6cSSean Christopherson
13767730e6cSSean Christopherson #define VECTOR_NUM 16
13867730e6cSSean Christopherson
13967730e6cSSean Christopherson enum {
14067730e6cSSean Christopherson VECTOR_SYNC_CURRENT_SP0,
14167730e6cSSean Christopherson VECTOR_IRQ_CURRENT_SP0,
14267730e6cSSean Christopherson VECTOR_FIQ_CURRENT_SP0,
14367730e6cSSean Christopherson VECTOR_ERROR_CURRENT_SP0,
14467730e6cSSean Christopherson
14567730e6cSSean Christopherson VECTOR_SYNC_CURRENT,
14667730e6cSSean Christopherson VECTOR_IRQ_CURRENT,
14767730e6cSSean Christopherson VECTOR_FIQ_CURRENT,
14867730e6cSSean Christopherson VECTOR_ERROR_CURRENT,
14967730e6cSSean Christopherson
15067730e6cSSean Christopherson VECTOR_SYNC_LOWER_64,
15167730e6cSSean Christopherson VECTOR_IRQ_LOWER_64,
15267730e6cSSean Christopherson VECTOR_FIQ_LOWER_64,
15367730e6cSSean Christopherson VECTOR_ERROR_LOWER_64,
15467730e6cSSean Christopherson
15567730e6cSSean Christopherson VECTOR_SYNC_LOWER_32,
15667730e6cSSean Christopherson VECTOR_IRQ_LOWER_32,
15767730e6cSSean Christopherson VECTOR_FIQ_LOWER_32,
15867730e6cSSean Christopherson VECTOR_ERROR_LOWER_32,
15967730e6cSSean Christopherson };
16067730e6cSSean Christopherson
16167730e6cSSean Christopherson #define VECTOR_IS_SYNC(v) ((v) == VECTOR_SYNC_CURRENT_SP0 || \
16267730e6cSSean Christopherson (v) == VECTOR_SYNC_CURRENT || \
16367730e6cSSean Christopherson (v) == VECTOR_SYNC_LOWER_64 || \
16467730e6cSSean Christopherson (v) == VECTOR_SYNC_LOWER_32)
16567730e6cSSean Christopherson
16667730e6cSSean Christopherson void aarch64_get_supported_page_sizes(uint32_t ipa, uint32_t *ipa4k,
16767730e6cSSean Christopherson uint32_t *ipa16k, uint32_t *ipa64k);
16867730e6cSSean Christopherson
16967730e6cSSean Christopherson void vm_init_descriptor_tables(struct kvm_vm *vm);
17067730e6cSSean Christopherson void vcpu_init_descriptor_tables(struct kvm_vcpu *vcpu);
17167730e6cSSean Christopherson
17267730e6cSSean Christopherson typedef void(*handler_fn)(struct ex_regs *);
17367730e6cSSean Christopherson void vm_install_exception_handler(struct kvm_vm *vm,
17467730e6cSSean Christopherson int vector, handler_fn handler);
17567730e6cSSean Christopherson void vm_install_sync_handler(struct kvm_vm *vm,
17667730e6cSSean Christopherson int vector, int ec, handler_fn handler);
17767730e6cSSean Christopherson
17867730e6cSSean Christopherson uint64_t *virt_get_pte_hva(struct kvm_vm *vm, vm_vaddr_t gva);
17967730e6cSSean Christopherson
cpu_relax(void)18067730e6cSSean Christopherson static inline void cpu_relax(void)
18167730e6cSSean Christopherson {
18267730e6cSSean Christopherson asm volatile("yield" ::: "memory");
18367730e6cSSean Christopherson }
18467730e6cSSean Christopherson
18567730e6cSSean Christopherson #define isb() asm volatile("isb" : : : "memory")
18667730e6cSSean Christopherson #define dsb(opt) asm volatile("dsb " #opt : : : "memory")
18767730e6cSSean Christopherson #define dmb(opt) asm volatile("dmb " #opt : : : "memory")
18867730e6cSSean Christopherson
18967730e6cSSean Christopherson #define dma_wmb() dmb(oshst)
19067730e6cSSean Christopherson #define __iowmb() dma_wmb()
19167730e6cSSean Christopherson
19267730e6cSSean Christopherson #define dma_rmb() dmb(oshld)
19367730e6cSSean Christopherson
19467730e6cSSean Christopherson #define __iormb(v) \
19567730e6cSSean Christopherson ({ \
19667730e6cSSean Christopherson unsigned long tmp; \
19767730e6cSSean Christopherson \
19867730e6cSSean Christopherson dma_rmb(); \
19967730e6cSSean Christopherson \
20067730e6cSSean Christopherson /* \
20167730e6cSSean Christopherson * Courtesy of arch/arm64/include/asm/io.h: \
20267730e6cSSean Christopherson * Create a dummy control dependency from the IO read to any \
20367730e6cSSean Christopherson * later instructions. This ensures that a subsequent call \
20467730e6cSSean Christopherson * to udelay() will be ordered due to the ISB in __delay(). \
20567730e6cSSean Christopherson */ \
20667730e6cSSean Christopherson asm volatile("eor %0, %1, %1\n" \
20767730e6cSSean Christopherson "cbnz %0, ." \
20867730e6cSSean Christopherson : "=r" (tmp) : "r" ((unsigned long)(v)) \
20967730e6cSSean Christopherson : "memory"); \
21067730e6cSSean Christopherson })
21167730e6cSSean Christopherson
__raw_writel(u32 val,volatile void * addr)21267730e6cSSean Christopherson static __always_inline void __raw_writel(u32 val, volatile void *addr)
21367730e6cSSean Christopherson {
21467730e6cSSean Christopherson asm volatile("str %w0, [%1]" : : "rZ" (val), "r" (addr));
21567730e6cSSean Christopherson }
21667730e6cSSean Christopherson
__raw_readl(const volatile void * addr)21767730e6cSSean Christopherson static __always_inline u32 __raw_readl(const volatile void *addr)
21867730e6cSSean Christopherson {
21967730e6cSSean Christopherson u32 val;
22067730e6cSSean Christopherson asm volatile("ldr %w0, [%1]" : "=r" (val) : "r" (addr));
22167730e6cSSean Christopherson return val;
22267730e6cSSean Christopherson }
22367730e6cSSean Christopherson
__raw_writeq(u64 val,volatile void * addr)22467730e6cSSean Christopherson static __always_inline void __raw_writeq(u64 val, volatile void *addr)
22567730e6cSSean Christopherson {
22667730e6cSSean Christopherson asm volatile("str %0, [%1]" : : "rZ" (val), "r" (addr));
22767730e6cSSean Christopherson }
22867730e6cSSean Christopherson
__raw_readq(const volatile void * addr)22967730e6cSSean Christopherson static __always_inline u64 __raw_readq(const volatile void *addr)
23067730e6cSSean Christopherson {
23167730e6cSSean Christopherson u64 val;
23267730e6cSSean Christopherson asm volatile("ldr %0, [%1]" : "=r" (val) : "r" (addr));
23367730e6cSSean Christopherson return val;
23467730e6cSSean Christopherson }
23567730e6cSSean Christopherson
23667730e6cSSean Christopherson #define writel_relaxed(v,c) ((void)__raw_writel((__force u32)cpu_to_le32(v),(c)))
23767730e6cSSean Christopherson #define readl_relaxed(c) ({ u32 __r = le32_to_cpu((__force __le32)__raw_readl(c)); __r; })
23867730e6cSSean Christopherson #define writeq_relaxed(v,c) ((void)__raw_writeq((__force u64)cpu_to_le64(v),(c)))
23967730e6cSSean Christopherson #define readq_relaxed(c) ({ u64 __r = le64_to_cpu((__force __le64)__raw_readq(c)); __r; })
24067730e6cSSean Christopherson
24167730e6cSSean Christopherson #define writel(v,c) ({ __iowmb(); writel_relaxed((v),(c));})
24267730e6cSSean Christopherson #define readl(c) ({ u32 __v = readl_relaxed(c); __iormb(__v); __v; })
24367730e6cSSean Christopherson #define writeq(v,c) ({ __iowmb(); writeq_relaxed((v),(c));})
24467730e6cSSean Christopherson #define readq(c) ({ u64 __v = readq_relaxed(c); __iormb(__v); __v; })
24567730e6cSSean Christopherson
24667730e6cSSean Christopherson
local_irq_enable(void)24767730e6cSSean Christopherson static inline void local_irq_enable(void)
24867730e6cSSean Christopherson {
24967730e6cSSean Christopherson asm volatile("msr daifclr, #3" : : : "memory");
25067730e6cSSean Christopherson }
25167730e6cSSean Christopherson
local_irq_disable(void)25267730e6cSSean Christopherson static inline void local_irq_disable(void)
25367730e6cSSean Christopherson {
25467730e6cSSean Christopherson asm volatile("msr daifset, #3" : : : "memory");
25567730e6cSSean Christopherson }
25667730e6cSSean Christopherson
25767730e6cSSean Christopherson /**
25867730e6cSSean Christopherson * struct arm_smccc_res - Result from SMC/HVC call
25967730e6cSSean Christopherson * @a0-a3 result values from registers 0 to 3
26067730e6cSSean Christopherson */
26167730e6cSSean Christopherson struct arm_smccc_res {
26267730e6cSSean Christopherson unsigned long a0;
26367730e6cSSean Christopherson unsigned long a1;
26467730e6cSSean Christopherson unsigned long a2;
26567730e6cSSean Christopherson unsigned long a3;
26667730e6cSSean Christopherson };
26767730e6cSSean Christopherson
26867730e6cSSean Christopherson /**
26967730e6cSSean Christopherson * smccc_hvc - Invoke a SMCCC function using the hvc conduit
27067730e6cSSean Christopherson * @function_id: the SMCCC function to be called
27167730e6cSSean Christopherson * @arg0-arg6: SMCCC function arguments, corresponding to registers x1-x7
27267730e6cSSean Christopherson * @res: pointer to write the return values from registers x0-x3
27367730e6cSSean Christopherson *
27467730e6cSSean Christopherson */
27567730e6cSSean Christopherson void smccc_hvc(uint32_t function_id, uint64_t arg0, uint64_t arg1,
27667730e6cSSean Christopherson uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5,
27767730e6cSSean Christopherson uint64_t arg6, struct arm_smccc_res *res);
27867730e6cSSean Christopherson
27967730e6cSSean Christopherson /**
28067730e6cSSean Christopherson * smccc_smc - Invoke a SMCCC function using the smc conduit
28167730e6cSSean Christopherson * @function_id: the SMCCC function to be called
28267730e6cSSean Christopherson * @arg0-arg6: SMCCC function arguments, corresponding to registers x1-x7
28367730e6cSSean Christopherson * @res: pointer to write the return values from registers x0-x3
28467730e6cSSean Christopherson *
28567730e6cSSean Christopherson */
28667730e6cSSean Christopherson void smccc_smc(uint32_t function_id, uint64_t arg0, uint64_t arg1,
28767730e6cSSean Christopherson uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5,
28867730e6cSSean Christopherson uint64_t arg6, struct arm_smccc_res *res);
28967730e6cSSean Christopherson
29067730e6cSSean Christopherson /* Execute a Wait For Interrupt instruction. */
29167730e6cSSean Christopherson void wfi(void);
29267730e6cSSean Christopherson
29367730e6cSSean Christopherson #endif /* SELFTEST_KVM_PROCESSOR_H */
294