162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2012,2013 - ARM Ltd
462306a36Sopenharmony_ci * Author: Marc Zyngier <marc.zyngier@arm.com>
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Derived from arch/arm/include/uapi/asm/kvm.h:
762306a36Sopenharmony_ci * Copyright (C) 2012 - Virtual Open Systems and Columbia University
862306a36Sopenharmony_ci * Author: Christoffer Dall <c.dall@virtualopensystems.com>
962306a36Sopenharmony_ci *
1062306a36Sopenharmony_ci * This program is free software; you can redistribute it and/or modify
1162306a36Sopenharmony_ci * it under the terms of the GNU General Public License version 2 as
1262306a36Sopenharmony_ci * published by the Free Software Foundation.
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * This program is distributed in the hope that it will be useful,
1562306a36Sopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
1662306a36Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1762306a36Sopenharmony_ci * GNU General Public License for more details.
1862306a36Sopenharmony_ci *
1962306a36Sopenharmony_ci * You should have received a copy of the GNU General Public License
2062306a36Sopenharmony_ci * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2162306a36Sopenharmony_ci */
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci#ifndef __ARM_KVM_H__
2462306a36Sopenharmony_ci#define __ARM_KVM_H__
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#define KVM_SPSR_EL1	0
2762306a36Sopenharmony_ci#define KVM_SPSR_SVC	KVM_SPSR_EL1
2862306a36Sopenharmony_ci#define KVM_SPSR_ABT	1
2962306a36Sopenharmony_ci#define KVM_SPSR_UND	2
3062306a36Sopenharmony_ci#define KVM_SPSR_IRQ	3
3162306a36Sopenharmony_ci#define KVM_SPSR_FIQ	4
3262306a36Sopenharmony_ci#define KVM_NR_SPSR	5
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci#ifndef __ASSEMBLY__
3562306a36Sopenharmony_ci#include <linux/psci.h>
3662306a36Sopenharmony_ci#include <linux/types.h>
3762306a36Sopenharmony_ci#include <asm/ptrace.h>
3862306a36Sopenharmony_ci#include <asm/sve_context.h>
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci#define __KVM_HAVE_GUEST_DEBUG
4162306a36Sopenharmony_ci#define __KVM_HAVE_IRQ_LINE
4262306a36Sopenharmony_ci#define __KVM_HAVE_READONLY_MEM
4362306a36Sopenharmony_ci#define __KVM_HAVE_VCPU_EVENTS
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci#define KVM_COALESCED_MMIO_PAGE_OFFSET 1
4662306a36Sopenharmony_ci#define KVM_DIRTY_LOG_PAGE_OFFSET 64
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci#define KVM_REG_SIZE(id)						\
4962306a36Sopenharmony_ci	(1U << (((id) & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT))
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_cistruct kvm_regs {
5262306a36Sopenharmony_ci	struct user_pt_regs regs;	/* sp = sp_el0 */
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci	__u64	sp_el1;
5562306a36Sopenharmony_ci	__u64	elr_el1;
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci	__u64	spsr[KVM_NR_SPSR];
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci	struct user_fpsimd_state fp_regs;
6062306a36Sopenharmony_ci};
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci/*
6362306a36Sopenharmony_ci * Supported CPU Targets - Adding a new target type is not recommended,
6462306a36Sopenharmony_ci * unless there are some special registers not supported by the
6562306a36Sopenharmony_ci * genericv8 syreg table.
6662306a36Sopenharmony_ci */
6762306a36Sopenharmony_ci#define KVM_ARM_TARGET_AEM_V8		0
6862306a36Sopenharmony_ci#define KVM_ARM_TARGET_FOUNDATION_V8	1
6962306a36Sopenharmony_ci#define KVM_ARM_TARGET_CORTEX_A57	2
7062306a36Sopenharmony_ci#define KVM_ARM_TARGET_XGENE_POTENZA	3
7162306a36Sopenharmony_ci#define KVM_ARM_TARGET_CORTEX_A53	4
7262306a36Sopenharmony_ci/* Generic ARM v8 target */
7362306a36Sopenharmony_ci#define KVM_ARM_TARGET_GENERIC_V8	5
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci#define KVM_ARM_NUM_TARGETS		6
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci/* KVM_ARM_SET_DEVICE_ADDR ioctl id encoding */
7862306a36Sopenharmony_ci#define KVM_ARM_DEVICE_TYPE_SHIFT	0
7962306a36Sopenharmony_ci#define KVM_ARM_DEVICE_TYPE_MASK	GENMASK(KVM_ARM_DEVICE_TYPE_SHIFT + 15, \
8062306a36Sopenharmony_ci						KVM_ARM_DEVICE_TYPE_SHIFT)
8162306a36Sopenharmony_ci#define KVM_ARM_DEVICE_ID_SHIFT		16
8262306a36Sopenharmony_ci#define KVM_ARM_DEVICE_ID_MASK		GENMASK(KVM_ARM_DEVICE_ID_SHIFT + 15, \
8362306a36Sopenharmony_ci						KVM_ARM_DEVICE_ID_SHIFT)
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci/* Supported device IDs */
8662306a36Sopenharmony_ci#define KVM_ARM_DEVICE_VGIC_V2		0
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci/* Supported VGIC address types  */
8962306a36Sopenharmony_ci#define KVM_VGIC_V2_ADDR_TYPE_DIST	0
9062306a36Sopenharmony_ci#define KVM_VGIC_V2_ADDR_TYPE_CPU	1
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci#define KVM_VGIC_V2_DIST_SIZE		0x1000
9362306a36Sopenharmony_ci#define KVM_VGIC_V2_CPU_SIZE		0x2000
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci/* Supported VGICv3 address types  */
9662306a36Sopenharmony_ci#define KVM_VGIC_V3_ADDR_TYPE_DIST	2
9762306a36Sopenharmony_ci#define KVM_VGIC_V3_ADDR_TYPE_REDIST	3
9862306a36Sopenharmony_ci#define KVM_VGIC_ITS_ADDR_TYPE		4
9962306a36Sopenharmony_ci#define KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION	5
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci#define KVM_VGIC_V3_DIST_SIZE		SZ_64K
10262306a36Sopenharmony_ci#define KVM_VGIC_V3_REDIST_SIZE		(2 * SZ_64K)
10362306a36Sopenharmony_ci#define KVM_VGIC_V3_ITS_SIZE		(2 * SZ_64K)
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci#define KVM_ARM_VCPU_POWER_OFF		0 /* CPU is started in OFF state */
10662306a36Sopenharmony_ci#define KVM_ARM_VCPU_EL1_32BIT		1 /* CPU running a 32bit VM */
10762306a36Sopenharmony_ci#define KVM_ARM_VCPU_PSCI_0_2		2 /* CPU uses PSCI v0.2 */
10862306a36Sopenharmony_ci#define KVM_ARM_VCPU_PMU_V3		3 /* Support guest PMUv3 */
10962306a36Sopenharmony_ci#define KVM_ARM_VCPU_SVE		4 /* enable SVE for this CPU */
11062306a36Sopenharmony_ci#define KVM_ARM_VCPU_PTRAUTH_ADDRESS	5 /* VCPU uses address authentication */
11162306a36Sopenharmony_ci#define KVM_ARM_VCPU_PTRAUTH_GENERIC	6 /* VCPU uses generic authentication */
11262306a36Sopenharmony_ci#define KVM_ARM_VCPU_HAS_EL2		7 /* Support nested virtualization */
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_cistruct kvm_vcpu_init {
11562306a36Sopenharmony_ci	__u32 target;
11662306a36Sopenharmony_ci	__u32 features[7];
11762306a36Sopenharmony_ci};
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_cistruct kvm_sregs {
12062306a36Sopenharmony_ci};
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_cistruct kvm_fpu {
12362306a36Sopenharmony_ci};
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci/*
12662306a36Sopenharmony_ci * See v8 ARM ARM D7.3: Debug Registers
12762306a36Sopenharmony_ci *
12862306a36Sopenharmony_ci * The architectural limit is 16 debug registers of each type although
12962306a36Sopenharmony_ci * in practice there are usually less (see ID_AA64DFR0_EL1).
13062306a36Sopenharmony_ci *
13162306a36Sopenharmony_ci * Although the control registers are architecturally defined as 32
13262306a36Sopenharmony_ci * bits wide we use a 64 bit structure here to keep parity with
13362306a36Sopenharmony_ci * KVM_GET/SET_ONE_REG behaviour which treats all system registers as
13462306a36Sopenharmony_ci * 64 bit values. It also allows for the possibility of the
13562306a36Sopenharmony_ci * architecture expanding the control registers without having to
13662306a36Sopenharmony_ci * change the userspace ABI.
13762306a36Sopenharmony_ci */
13862306a36Sopenharmony_ci#define KVM_ARM_MAX_DBG_REGS 16
13962306a36Sopenharmony_cistruct kvm_guest_debug_arch {
14062306a36Sopenharmony_ci	__u64 dbg_bcr[KVM_ARM_MAX_DBG_REGS];
14162306a36Sopenharmony_ci	__u64 dbg_bvr[KVM_ARM_MAX_DBG_REGS];
14262306a36Sopenharmony_ci	__u64 dbg_wcr[KVM_ARM_MAX_DBG_REGS];
14362306a36Sopenharmony_ci	__u64 dbg_wvr[KVM_ARM_MAX_DBG_REGS];
14462306a36Sopenharmony_ci};
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci#define KVM_DEBUG_ARCH_HSR_HIGH_VALID	(1 << 0)
14762306a36Sopenharmony_cistruct kvm_debug_exit_arch {
14862306a36Sopenharmony_ci	__u32 hsr;
14962306a36Sopenharmony_ci	__u32 hsr_high;	/* ESR_EL2[61:32] */
15062306a36Sopenharmony_ci	__u64 far;	/* used for watchpoints */
15162306a36Sopenharmony_ci};
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci/*
15462306a36Sopenharmony_ci * Architecture specific defines for kvm_guest_debug->control
15562306a36Sopenharmony_ci */
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci#define KVM_GUESTDBG_USE_SW_BP		(1 << 16)
15862306a36Sopenharmony_ci#define KVM_GUESTDBG_USE_HW		(1 << 17)
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_cistruct kvm_sync_regs {
16162306a36Sopenharmony_ci	/* Used with KVM_CAP_ARM_USER_IRQ */
16262306a36Sopenharmony_ci	__u64 device_irq_level;
16362306a36Sopenharmony_ci};
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci/*
16662306a36Sopenharmony_ci * PMU filter structure. Describe a range of events with a particular
16762306a36Sopenharmony_ci * action. To be used with KVM_ARM_VCPU_PMU_V3_FILTER.
16862306a36Sopenharmony_ci */
16962306a36Sopenharmony_cistruct kvm_pmu_event_filter {
17062306a36Sopenharmony_ci	__u16	base_event;
17162306a36Sopenharmony_ci	__u16	nevents;
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci#define KVM_PMU_EVENT_ALLOW	0
17462306a36Sopenharmony_ci#define KVM_PMU_EVENT_DENY	1
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci	__u8	action;
17762306a36Sopenharmony_ci	__u8	pad[3];
17862306a36Sopenharmony_ci};
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci/* for KVM_GET/SET_VCPU_EVENTS */
18162306a36Sopenharmony_cistruct kvm_vcpu_events {
18262306a36Sopenharmony_ci	struct {
18362306a36Sopenharmony_ci		__u8 serror_pending;
18462306a36Sopenharmony_ci		__u8 serror_has_esr;
18562306a36Sopenharmony_ci		__u8 ext_dabt_pending;
18662306a36Sopenharmony_ci		/* Align it to 8 bytes */
18762306a36Sopenharmony_ci		__u8 pad[5];
18862306a36Sopenharmony_ci		__u64 serror_esr;
18962306a36Sopenharmony_ci	} exception;
19062306a36Sopenharmony_ci	__u32 reserved[12];
19162306a36Sopenharmony_ci};
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_cistruct kvm_arm_copy_mte_tags {
19462306a36Sopenharmony_ci	__u64 guest_ipa;
19562306a36Sopenharmony_ci	__u64 length;
19662306a36Sopenharmony_ci	void __user *addr;
19762306a36Sopenharmony_ci	__u64 flags;
19862306a36Sopenharmony_ci	__u64 reserved[2];
19962306a36Sopenharmony_ci};
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ci/*
20262306a36Sopenharmony_ci * Counter/Timer offset structure. Describe the virtual/physical offset.
20362306a36Sopenharmony_ci * To be used with KVM_ARM_SET_COUNTER_OFFSET.
20462306a36Sopenharmony_ci */
20562306a36Sopenharmony_cistruct kvm_arm_counter_offset {
20662306a36Sopenharmony_ci	__u64 counter_offset;
20762306a36Sopenharmony_ci	__u64 reserved;
20862306a36Sopenharmony_ci};
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci#define KVM_ARM_TAGS_TO_GUEST		0
21162306a36Sopenharmony_ci#define KVM_ARM_TAGS_FROM_GUEST		1
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci/* If you need to interpret the index values, here is the key: */
21462306a36Sopenharmony_ci#define KVM_REG_ARM_COPROC_MASK		0x000000000FFF0000
21562306a36Sopenharmony_ci#define KVM_REG_ARM_COPROC_SHIFT	16
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ci/* Normal registers are mapped as coprocessor 16. */
21862306a36Sopenharmony_ci#define KVM_REG_ARM_CORE		(0x0010 << KVM_REG_ARM_COPROC_SHIFT)
21962306a36Sopenharmony_ci#define KVM_REG_ARM_CORE_REG(name)	(offsetof(struct kvm_regs, name) / sizeof(__u32))
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ci/* Some registers need more space to represent values. */
22262306a36Sopenharmony_ci#define KVM_REG_ARM_DEMUX		(0x0011 << KVM_REG_ARM_COPROC_SHIFT)
22362306a36Sopenharmony_ci#define KVM_REG_ARM_DEMUX_ID_MASK	0x000000000000FF00
22462306a36Sopenharmony_ci#define KVM_REG_ARM_DEMUX_ID_SHIFT	8
22562306a36Sopenharmony_ci#define KVM_REG_ARM_DEMUX_ID_CCSIDR	(0x00 << KVM_REG_ARM_DEMUX_ID_SHIFT)
22662306a36Sopenharmony_ci#define KVM_REG_ARM_DEMUX_VAL_MASK	0x00000000000000FF
22762306a36Sopenharmony_ci#define KVM_REG_ARM_DEMUX_VAL_SHIFT	0
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_ci/* AArch64 system registers */
23062306a36Sopenharmony_ci#define KVM_REG_ARM64_SYSREG		(0x0013 << KVM_REG_ARM_COPROC_SHIFT)
23162306a36Sopenharmony_ci#define KVM_REG_ARM64_SYSREG_OP0_MASK	0x000000000000c000
23262306a36Sopenharmony_ci#define KVM_REG_ARM64_SYSREG_OP0_SHIFT	14
23362306a36Sopenharmony_ci#define KVM_REG_ARM64_SYSREG_OP1_MASK	0x0000000000003800
23462306a36Sopenharmony_ci#define KVM_REG_ARM64_SYSREG_OP1_SHIFT	11
23562306a36Sopenharmony_ci#define KVM_REG_ARM64_SYSREG_CRN_MASK	0x0000000000000780
23662306a36Sopenharmony_ci#define KVM_REG_ARM64_SYSREG_CRN_SHIFT	7
23762306a36Sopenharmony_ci#define KVM_REG_ARM64_SYSREG_CRM_MASK	0x0000000000000078
23862306a36Sopenharmony_ci#define KVM_REG_ARM64_SYSREG_CRM_SHIFT	3
23962306a36Sopenharmony_ci#define KVM_REG_ARM64_SYSREG_OP2_MASK	0x0000000000000007
24062306a36Sopenharmony_ci#define KVM_REG_ARM64_SYSREG_OP2_SHIFT	0
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ci#define ARM64_SYS_REG_SHIFT_MASK(x,n) \
24362306a36Sopenharmony_ci	(((x) << KVM_REG_ARM64_SYSREG_ ## n ## _SHIFT) & \
24462306a36Sopenharmony_ci	KVM_REG_ARM64_SYSREG_ ## n ## _MASK)
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ci#define __ARM64_SYS_REG(op0,op1,crn,crm,op2) \
24762306a36Sopenharmony_ci	(KVM_REG_ARM64 | KVM_REG_ARM64_SYSREG | \
24862306a36Sopenharmony_ci	ARM64_SYS_REG_SHIFT_MASK(op0, OP0) | \
24962306a36Sopenharmony_ci	ARM64_SYS_REG_SHIFT_MASK(op1, OP1) | \
25062306a36Sopenharmony_ci	ARM64_SYS_REG_SHIFT_MASK(crn, CRN) | \
25162306a36Sopenharmony_ci	ARM64_SYS_REG_SHIFT_MASK(crm, CRM) | \
25262306a36Sopenharmony_ci	ARM64_SYS_REG_SHIFT_MASK(op2, OP2))
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci#define ARM64_SYS_REG(...) (__ARM64_SYS_REG(__VA_ARGS__) | KVM_REG_SIZE_U64)
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_ci/* Physical Timer EL0 Registers */
25762306a36Sopenharmony_ci#define KVM_REG_ARM_PTIMER_CTL		ARM64_SYS_REG(3, 3, 14, 2, 1)
25862306a36Sopenharmony_ci#define KVM_REG_ARM_PTIMER_CVAL		ARM64_SYS_REG(3, 3, 14, 2, 2)
25962306a36Sopenharmony_ci#define KVM_REG_ARM_PTIMER_CNT		ARM64_SYS_REG(3, 3, 14, 0, 1)
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci/*
26262306a36Sopenharmony_ci * EL0 Virtual Timer Registers
26362306a36Sopenharmony_ci *
26462306a36Sopenharmony_ci * WARNING:
26562306a36Sopenharmony_ci *      KVM_REG_ARM_TIMER_CVAL and KVM_REG_ARM_TIMER_CNT are not defined
26662306a36Sopenharmony_ci *      with the appropriate register encodings.  Their values have been
26762306a36Sopenharmony_ci *      accidentally swapped.  As this is set API, the definitions here
26862306a36Sopenharmony_ci *      must be used, rather than ones derived from the encodings.
26962306a36Sopenharmony_ci */
27062306a36Sopenharmony_ci#define KVM_REG_ARM_TIMER_CTL		ARM64_SYS_REG(3, 3, 14, 3, 1)
27162306a36Sopenharmony_ci#define KVM_REG_ARM_TIMER_CVAL		ARM64_SYS_REG(3, 3, 14, 0, 2)
27262306a36Sopenharmony_ci#define KVM_REG_ARM_TIMER_CNT		ARM64_SYS_REG(3, 3, 14, 3, 2)
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_ci/* KVM-as-firmware specific pseudo-registers */
27562306a36Sopenharmony_ci#define KVM_REG_ARM_FW			(0x0014 << KVM_REG_ARM_COPROC_SHIFT)
27662306a36Sopenharmony_ci#define KVM_REG_ARM_FW_REG(r)		(KVM_REG_ARM64 | KVM_REG_SIZE_U64 | \
27762306a36Sopenharmony_ci					 KVM_REG_ARM_FW | ((r) & 0xffff))
27862306a36Sopenharmony_ci#define KVM_REG_ARM_PSCI_VERSION	KVM_REG_ARM_FW_REG(0)
27962306a36Sopenharmony_ci#define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1	KVM_REG_ARM_FW_REG(1)
28062306a36Sopenharmony_ci#define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL		0
28162306a36Sopenharmony_ci#define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_AVAIL		1
28262306a36Sopenharmony_ci#define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_REQUIRED	2
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ci/*
28562306a36Sopenharmony_ci * Only two states can be presented by the host kernel:
28662306a36Sopenharmony_ci * - NOT_REQUIRED: the guest doesn't need to do anything
28762306a36Sopenharmony_ci * - NOT_AVAIL: the guest isn't mitigated (it can still use SSBS if available)
28862306a36Sopenharmony_ci *
28962306a36Sopenharmony_ci * All the other values are deprecated. The host still accepts all
29062306a36Sopenharmony_ci * values (they are ABI), but will narrow them to the above two.
29162306a36Sopenharmony_ci */
29262306a36Sopenharmony_ci#define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2	KVM_REG_ARM_FW_REG(2)
29362306a36Sopenharmony_ci#define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL		0
29462306a36Sopenharmony_ci#define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_UNKNOWN		1
29562306a36Sopenharmony_ci#define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL		2
29662306a36Sopenharmony_ci#define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED	3
29762306a36Sopenharmony_ci#define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED     	(1U << 4)
29862306a36Sopenharmony_ci
29962306a36Sopenharmony_ci#define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3	KVM_REG_ARM_FW_REG(3)
30062306a36Sopenharmony_ci#define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3_NOT_AVAIL		0
30162306a36Sopenharmony_ci#define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3_AVAIL		1
30262306a36Sopenharmony_ci#define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3_NOT_REQUIRED	2
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ci/* SVE registers */
30562306a36Sopenharmony_ci#define KVM_REG_ARM64_SVE		(0x15 << KVM_REG_ARM_COPROC_SHIFT)
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_ci/* Z- and P-regs occupy blocks at the following offsets within this range: */
30862306a36Sopenharmony_ci#define KVM_REG_ARM64_SVE_ZREG_BASE	0
30962306a36Sopenharmony_ci#define KVM_REG_ARM64_SVE_PREG_BASE	0x400
31062306a36Sopenharmony_ci#define KVM_REG_ARM64_SVE_FFR_BASE	0x600
31162306a36Sopenharmony_ci
31262306a36Sopenharmony_ci#define KVM_ARM64_SVE_NUM_ZREGS		__SVE_NUM_ZREGS
31362306a36Sopenharmony_ci#define KVM_ARM64_SVE_NUM_PREGS		__SVE_NUM_PREGS
31462306a36Sopenharmony_ci
31562306a36Sopenharmony_ci#define KVM_ARM64_SVE_MAX_SLICES	32
31662306a36Sopenharmony_ci
31762306a36Sopenharmony_ci#define KVM_REG_ARM64_SVE_ZREG(n, i)					\
31862306a36Sopenharmony_ci	(KVM_REG_ARM64 | KVM_REG_ARM64_SVE | KVM_REG_ARM64_SVE_ZREG_BASE | \
31962306a36Sopenharmony_ci	 KVM_REG_SIZE_U2048 |						\
32062306a36Sopenharmony_ci	 (((n) & (KVM_ARM64_SVE_NUM_ZREGS - 1)) << 5) |			\
32162306a36Sopenharmony_ci	 ((i) & (KVM_ARM64_SVE_MAX_SLICES - 1)))
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_ci#define KVM_REG_ARM64_SVE_PREG(n, i)					\
32462306a36Sopenharmony_ci	(KVM_REG_ARM64 | KVM_REG_ARM64_SVE | KVM_REG_ARM64_SVE_PREG_BASE | \
32562306a36Sopenharmony_ci	 KVM_REG_SIZE_U256 |						\
32662306a36Sopenharmony_ci	 (((n) & (KVM_ARM64_SVE_NUM_PREGS - 1)) << 5) |			\
32762306a36Sopenharmony_ci	 ((i) & (KVM_ARM64_SVE_MAX_SLICES - 1)))
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_ci#define KVM_REG_ARM64_SVE_FFR(i)					\
33062306a36Sopenharmony_ci	(KVM_REG_ARM64 | KVM_REG_ARM64_SVE | KVM_REG_ARM64_SVE_FFR_BASE | \
33162306a36Sopenharmony_ci	 KVM_REG_SIZE_U256 |						\
33262306a36Sopenharmony_ci	 ((i) & (KVM_ARM64_SVE_MAX_SLICES - 1)))
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_ci/*
33562306a36Sopenharmony_ci * Register values for KVM_REG_ARM64_SVE_ZREG(), KVM_REG_ARM64_SVE_PREG() and
33662306a36Sopenharmony_ci * KVM_REG_ARM64_SVE_FFR() are represented in memory in an endianness-
33762306a36Sopenharmony_ci * invariant layout which differs from the layout used for the FPSIMD
33862306a36Sopenharmony_ci * V-registers on big-endian systems: see sigcontext.h for more explanation.
33962306a36Sopenharmony_ci */
34062306a36Sopenharmony_ci
34162306a36Sopenharmony_ci#define KVM_ARM64_SVE_VQ_MIN __SVE_VQ_MIN
34262306a36Sopenharmony_ci#define KVM_ARM64_SVE_VQ_MAX __SVE_VQ_MAX
34362306a36Sopenharmony_ci
34462306a36Sopenharmony_ci/* Vector lengths pseudo-register: */
34562306a36Sopenharmony_ci#define KVM_REG_ARM64_SVE_VLS		(KVM_REG_ARM64 | KVM_REG_ARM64_SVE | \
34662306a36Sopenharmony_ci					 KVM_REG_SIZE_U512 | 0xffff)
34762306a36Sopenharmony_ci#define KVM_ARM64_SVE_VLS_WORDS	\
34862306a36Sopenharmony_ci	((KVM_ARM64_SVE_VQ_MAX - KVM_ARM64_SVE_VQ_MIN) / 64 + 1)
34962306a36Sopenharmony_ci
35062306a36Sopenharmony_ci/* Bitmap feature firmware registers */
35162306a36Sopenharmony_ci#define KVM_REG_ARM_FW_FEAT_BMAP		(0x0016 << KVM_REG_ARM_COPROC_SHIFT)
35262306a36Sopenharmony_ci#define KVM_REG_ARM_FW_FEAT_BMAP_REG(r)		(KVM_REG_ARM64 | KVM_REG_SIZE_U64 | \
35362306a36Sopenharmony_ci						KVM_REG_ARM_FW_FEAT_BMAP |	\
35462306a36Sopenharmony_ci						((r) & 0xffff))
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_ci#define KVM_REG_ARM_STD_BMAP			KVM_REG_ARM_FW_FEAT_BMAP_REG(0)
35762306a36Sopenharmony_ci
35862306a36Sopenharmony_cienum {
35962306a36Sopenharmony_ci	KVM_REG_ARM_STD_BIT_TRNG_V1_0	= 0,
36062306a36Sopenharmony_ci#ifdef __KERNEL__
36162306a36Sopenharmony_ci	KVM_REG_ARM_STD_BMAP_BIT_COUNT,
36262306a36Sopenharmony_ci#endif
36362306a36Sopenharmony_ci};
36462306a36Sopenharmony_ci
36562306a36Sopenharmony_ci#define KVM_REG_ARM_STD_HYP_BMAP		KVM_REG_ARM_FW_FEAT_BMAP_REG(1)
36662306a36Sopenharmony_ci
36762306a36Sopenharmony_cienum {
36862306a36Sopenharmony_ci	KVM_REG_ARM_STD_HYP_BIT_PV_TIME	= 0,
36962306a36Sopenharmony_ci#ifdef __KERNEL__
37062306a36Sopenharmony_ci	KVM_REG_ARM_STD_HYP_BMAP_BIT_COUNT,
37162306a36Sopenharmony_ci#endif
37262306a36Sopenharmony_ci};
37362306a36Sopenharmony_ci
37462306a36Sopenharmony_ci#define KVM_REG_ARM_VENDOR_HYP_BMAP		KVM_REG_ARM_FW_FEAT_BMAP_REG(2)
37562306a36Sopenharmony_ci
37662306a36Sopenharmony_cienum {
37762306a36Sopenharmony_ci	KVM_REG_ARM_VENDOR_HYP_BIT_FUNC_FEAT	= 0,
37862306a36Sopenharmony_ci	KVM_REG_ARM_VENDOR_HYP_BIT_PTP		= 1,
37962306a36Sopenharmony_ci#ifdef __KERNEL__
38062306a36Sopenharmony_ci	KVM_REG_ARM_VENDOR_HYP_BMAP_BIT_COUNT,
38162306a36Sopenharmony_ci#endif
38262306a36Sopenharmony_ci};
38362306a36Sopenharmony_ci
38462306a36Sopenharmony_ci/* Device Control API on vm fd */
38562306a36Sopenharmony_ci#define KVM_ARM_VM_SMCCC_CTRL		0
38662306a36Sopenharmony_ci#define   KVM_ARM_VM_SMCCC_FILTER	0
38762306a36Sopenharmony_ci
38862306a36Sopenharmony_ci/* Device Control API: ARM VGIC */
38962306a36Sopenharmony_ci#define KVM_DEV_ARM_VGIC_GRP_ADDR	0
39062306a36Sopenharmony_ci#define KVM_DEV_ARM_VGIC_GRP_DIST_REGS	1
39162306a36Sopenharmony_ci#define KVM_DEV_ARM_VGIC_GRP_CPU_REGS	2
39262306a36Sopenharmony_ci#define   KVM_DEV_ARM_VGIC_CPUID_SHIFT	32
39362306a36Sopenharmony_ci#define   KVM_DEV_ARM_VGIC_CPUID_MASK	(0xffULL << KVM_DEV_ARM_VGIC_CPUID_SHIFT)
39462306a36Sopenharmony_ci#define   KVM_DEV_ARM_VGIC_V3_MPIDR_SHIFT 32
39562306a36Sopenharmony_ci#define   KVM_DEV_ARM_VGIC_V3_MPIDR_MASK \
39662306a36Sopenharmony_ci			(0xffffffffULL << KVM_DEV_ARM_VGIC_V3_MPIDR_SHIFT)
39762306a36Sopenharmony_ci#define   KVM_DEV_ARM_VGIC_OFFSET_SHIFT	0
39862306a36Sopenharmony_ci#define   KVM_DEV_ARM_VGIC_OFFSET_MASK	(0xffffffffULL << KVM_DEV_ARM_VGIC_OFFSET_SHIFT)
39962306a36Sopenharmony_ci#define   KVM_DEV_ARM_VGIC_SYSREG_INSTR_MASK (0xffff)
40062306a36Sopenharmony_ci#define KVM_DEV_ARM_VGIC_GRP_NR_IRQS	3
40162306a36Sopenharmony_ci#define KVM_DEV_ARM_VGIC_GRP_CTRL	4
40262306a36Sopenharmony_ci#define KVM_DEV_ARM_VGIC_GRP_REDIST_REGS 5
40362306a36Sopenharmony_ci#define KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS 6
40462306a36Sopenharmony_ci#define KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO  7
40562306a36Sopenharmony_ci#define KVM_DEV_ARM_VGIC_GRP_ITS_REGS 8
40662306a36Sopenharmony_ci#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT	10
40762306a36Sopenharmony_ci#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_MASK \
40862306a36Sopenharmony_ci			(0x3fffffULL << KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT)
40962306a36Sopenharmony_ci#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INTID_MASK	0x3ff
41062306a36Sopenharmony_ci#define VGIC_LEVEL_INFO_LINE_LEVEL	0
41162306a36Sopenharmony_ci
41262306a36Sopenharmony_ci#define   KVM_DEV_ARM_VGIC_CTRL_INIT		0
41362306a36Sopenharmony_ci#define   KVM_DEV_ARM_ITS_SAVE_TABLES           1
41462306a36Sopenharmony_ci#define   KVM_DEV_ARM_ITS_RESTORE_TABLES        2
41562306a36Sopenharmony_ci#define   KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES	3
41662306a36Sopenharmony_ci#define   KVM_DEV_ARM_ITS_CTRL_RESET		4
41762306a36Sopenharmony_ci
41862306a36Sopenharmony_ci/* Device Control API on vcpu fd */
41962306a36Sopenharmony_ci#define KVM_ARM_VCPU_PMU_V3_CTRL	0
42062306a36Sopenharmony_ci#define   KVM_ARM_VCPU_PMU_V3_IRQ	0
42162306a36Sopenharmony_ci#define   KVM_ARM_VCPU_PMU_V3_INIT	1
42262306a36Sopenharmony_ci#define   KVM_ARM_VCPU_PMU_V3_FILTER	2
42362306a36Sopenharmony_ci#define   KVM_ARM_VCPU_PMU_V3_SET_PMU	3
42462306a36Sopenharmony_ci#define KVM_ARM_VCPU_TIMER_CTRL		1
42562306a36Sopenharmony_ci#define   KVM_ARM_VCPU_TIMER_IRQ_VTIMER		0
42662306a36Sopenharmony_ci#define   KVM_ARM_VCPU_TIMER_IRQ_PTIMER		1
42762306a36Sopenharmony_ci#define   KVM_ARM_VCPU_TIMER_IRQ_HVTIMER	2
42862306a36Sopenharmony_ci#define   KVM_ARM_VCPU_TIMER_IRQ_HPTIMER	3
42962306a36Sopenharmony_ci#define KVM_ARM_VCPU_PVTIME_CTRL	2
43062306a36Sopenharmony_ci#define   KVM_ARM_VCPU_PVTIME_IPA	0
43162306a36Sopenharmony_ci
43262306a36Sopenharmony_ci/* KVM_IRQ_LINE irq field index values */
43362306a36Sopenharmony_ci#define KVM_ARM_IRQ_VCPU2_SHIFT		28
43462306a36Sopenharmony_ci#define KVM_ARM_IRQ_VCPU2_MASK		0xf
43562306a36Sopenharmony_ci#define KVM_ARM_IRQ_TYPE_SHIFT		24
43662306a36Sopenharmony_ci#define KVM_ARM_IRQ_TYPE_MASK		0xf
43762306a36Sopenharmony_ci#define KVM_ARM_IRQ_VCPU_SHIFT		16
43862306a36Sopenharmony_ci#define KVM_ARM_IRQ_VCPU_MASK		0xff
43962306a36Sopenharmony_ci#define KVM_ARM_IRQ_NUM_SHIFT		0
44062306a36Sopenharmony_ci#define KVM_ARM_IRQ_NUM_MASK		0xffff
44162306a36Sopenharmony_ci
44262306a36Sopenharmony_ci/* irq_type field */
44362306a36Sopenharmony_ci#define KVM_ARM_IRQ_TYPE_CPU		0
44462306a36Sopenharmony_ci#define KVM_ARM_IRQ_TYPE_SPI		1
44562306a36Sopenharmony_ci#define KVM_ARM_IRQ_TYPE_PPI		2
44662306a36Sopenharmony_ci
44762306a36Sopenharmony_ci/* out-of-kernel GIC cpu interrupt injection irq_number field */
44862306a36Sopenharmony_ci#define KVM_ARM_IRQ_CPU_IRQ		0
44962306a36Sopenharmony_ci#define KVM_ARM_IRQ_CPU_FIQ		1
45062306a36Sopenharmony_ci
45162306a36Sopenharmony_ci/*
45262306a36Sopenharmony_ci * This used to hold the highest supported SPI, but it is now obsolete
45362306a36Sopenharmony_ci * and only here to provide source code level compatibility with older
45462306a36Sopenharmony_ci * userland. The highest SPI number can be set via KVM_DEV_ARM_VGIC_GRP_NR_IRQS.
45562306a36Sopenharmony_ci */
45662306a36Sopenharmony_ci#ifndef __KERNEL__
45762306a36Sopenharmony_ci#define KVM_ARM_IRQ_GIC_MAX		127
45862306a36Sopenharmony_ci#endif
45962306a36Sopenharmony_ci
46062306a36Sopenharmony_ci/* One single KVM irqchip, ie. the VGIC */
46162306a36Sopenharmony_ci#define KVM_NR_IRQCHIPS          1
46262306a36Sopenharmony_ci
46362306a36Sopenharmony_ci/* PSCI interface */
46462306a36Sopenharmony_ci#define KVM_PSCI_FN_BASE		0x95c1ba5e
46562306a36Sopenharmony_ci#define KVM_PSCI_FN(n)			(KVM_PSCI_FN_BASE + (n))
46662306a36Sopenharmony_ci
46762306a36Sopenharmony_ci#define KVM_PSCI_FN_CPU_SUSPEND		KVM_PSCI_FN(0)
46862306a36Sopenharmony_ci#define KVM_PSCI_FN_CPU_OFF		KVM_PSCI_FN(1)
46962306a36Sopenharmony_ci#define KVM_PSCI_FN_CPU_ON		KVM_PSCI_FN(2)
47062306a36Sopenharmony_ci#define KVM_PSCI_FN_MIGRATE		KVM_PSCI_FN(3)
47162306a36Sopenharmony_ci
47262306a36Sopenharmony_ci#define KVM_PSCI_RET_SUCCESS		PSCI_RET_SUCCESS
47362306a36Sopenharmony_ci#define KVM_PSCI_RET_NI			PSCI_RET_NOT_SUPPORTED
47462306a36Sopenharmony_ci#define KVM_PSCI_RET_INVAL		PSCI_RET_INVALID_PARAMS
47562306a36Sopenharmony_ci#define KVM_PSCI_RET_DENIED		PSCI_RET_DENIED
47662306a36Sopenharmony_ci
47762306a36Sopenharmony_ci/* arm64-specific kvm_run::system_event flags */
47862306a36Sopenharmony_ci/*
47962306a36Sopenharmony_ci * Reset caused by a PSCI v1.1 SYSTEM_RESET2 call.
48062306a36Sopenharmony_ci * Valid only when the system event has a type of KVM_SYSTEM_EVENT_RESET.
48162306a36Sopenharmony_ci */
48262306a36Sopenharmony_ci#define KVM_SYSTEM_EVENT_RESET_FLAG_PSCI_RESET2	(1ULL << 0)
48362306a36Sopenharmony_ci
48462306a36Sopenharmony_ci/* run->fail_entry.hardware_entry_failure_reason codes. */
48562306a36Sopenharmony_ci#define KVM_EXIT_FAIL_ENTRY_CPU_UNSUPPORTED	(1ULL << 0)
48662306a36Sopenharmony_ci
48762306a36Sopenharmony_cienum kvm_smccc_filter_action {
48862306a36Sopenharmony_ci	KVM_SMCCC_FILTER_HANDLE = 0,
48962306a36Sopenharmony_ci	KVM_SMCCC_FILTER_DENY,
49062306a36Sopenharmony_ci	KVM_SMCCC_FILTER_FWD_TO_USER,
49162306a36Sopenharmony_ci
49262306a36Sopenharmony_ci#ifdef __KERNEL__
49362306a36Sopenharmony_ci	NR_SMCCC_FILTER_ACTIONS
49462306a36Sopenharmony_ci#endif
49562306a36Sopenharmony_ci};
49662306a36Sopenharmony_ci
49762306a36Sopenharmony_cistruct kvm_smccc_filter {
49862306a36Sopenharmony_ci	__u32 base;
49962306a36Sopenharmony_ci	__u32 nr_functions;
50062306a36Sopenharmony_ci	__u8 action;
50162306a36Sopenharmony_ci	__u8 pad[15];
50262306a36Sopenharmony_ci};
50362306a36Sopenharmony_ci
50462306a36Sopenharmony_ci/* arm64-specific KVM_EXIT_HYPERCALL flags */
50562306a36Sopenharmony_ci#define KVM_HYPERCALL_EXIT_SMC		(1U << 0)
50662306a36Sopenharmony_ci#define KVM_HYPERCALL_EXIT_16BIT	(1U << 1)
50762306a36Sopenharmony_ci
50862306a36Sopenharmony_ci#endif
50962306a36Sopenharmony_ci
51062306a36Sopenharmony_ci#endif /* __ARM_KVM_H__ */
511