1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
3 * Copyright (C) 2019 Western Digital Corporation or its affiliates.
6 * Anup Patel <anup.patel@wdc.com>
9 #ifndef __LINUX_KVM_RISCV_H
10 #define __LINUX_KVM_RISCV_H
14 #include <linux/types.h>
15 #include <asm/bitsperlong.h>
16 #include <asm/ptrace.h>
18 #define __KVM_HAVE_READONLY_MEM
20 #define KVM_COALESCED_MMIO_PAGE_OFFSET 1
22 #define KVM_INTERRUPT_SET -1U
23 #define KVM_INTERRUPT_UNSET -2U
25 /* for KVM_GET_REGS and KVM_SET_REGS */
29 /* for KVM_GET_FPU and KVM_SET_FPU */
33 /* KVM Debug exit structure */
34 struct kvm_debug_exit_arch {
37 /* for KVM_SET_GUEST_DEBUG */
38 struct kvm_guest_debug_arch {
41 /* definition of registers in kvm_run */
42 struct kvm_sync_regs {
45 /* for KVM_GET_SREGS and KVM_SET_SREGS */
49 /* CONFIG registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */
50 struct kvm_riscv_config {
52 unsigned long zicbom_block_size;
53 unsigned long mvendorid;
54 unsigned long marchid;
56 unsigned long zicboz_block_size;
59 /* CORE registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */
60 struct kvm_riscv_core {
61 struct user_regs_struct regs;
65 /* Possible privilege modes for kvm_riscv_core */
66 #define KVM_RISCV_MODE_S 1
67 #define KVM_RISCV_MODE_U 0
69 /* General CSR registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */
70 struct kvm_riscv_csr {
71 unsigned long sstatus;
74 unsigned long sscratch;
80 unsigned long scounteren;
83 /* AIA CSR registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */
84 struct kvm_riscv_aia_csr {
85 unsigned long siselect;
90 unsigned long iprio1h;
91 unsigned long iprio2h;
94 /* TIMER registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */
95 struct kvm_riscv_timer {
103 * ISA extension IDs specific to KVM. This is not the same as the host ISA
104 * extension IDs as that is internal to the host and should not be exposed
105 * to the guest. This should always be contiguous to keep the mapping simple
106 * in KVM implementation.
108 enum KVM_RISCV_ISA_EXT_ID {
109 KVM_RISCV_ISA_EXT_A = 0,
116 KVM_RISCV_ISA_EXT_SVPBMT,
117 KVM_RISCV_ISA_EXT_SSTC,
118 KVM_RISCV_ISA_EXT_SVINVAL,
119 KVM_RISCV_ISA_EXT_ZIHINTPAUSE,
120 KVM_RISCV_ISA_EXT_ZICBOM,
121 KVM_RISCV_ISA_EXT_ZICBOZ,
122 KVM_RISCV_ISA_EXT_ZBB,
123 KVM_RISCV_ISA_EXT_SSAIA,
125 KVM_RISCV_ISA_EXT_MAX,
129 * SBI extension IDs specific to KVM. This is not the same as the SBI
130 * extension IDs defined by the RISC-V SBI specification.
132 enum KVM_RISCV_SBI_EXT_ID {
133 KVM_RISCV_SBI_EXT_V01 = 0,
134 KVM_RISCV_SBI_EXT_TIME,
135 KVM_RISCV_SBI_EXT_IPI,
136 KVM_RISCV_SBI_EXT_RFENCE,
137 KVM_RISCV_SBI_EXT_SRST,
138 KVM_RISCV_SBI_EXT_HSM,
139 KVM_RISCV_SBI_EXT_PMU,
140 KVM_RISCV_SBI_EXT_EXPERIMENTAL,
141 KVM_RISCV_SBI_EXT_VENDOR,
142 KVM_RISCV_SBI_EXT_MAX,
145 /* Possible states for kvm_riscv_timer */
146 #define KVM_RISCV_TIMER_STATE_OFF 0
147 #define KVM_RISCV_TIMER_STATE_ON 1
149 #define KVM_REG_SIZE(id) \
150 (1U << (((id) & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT))
152 /* If you need to interpret the index values, here is the key: */
153 #define KVM_REG_RISCV_TYPE_MASK 0x00000000FF000000
154 #define KVM_REG_RISCV_TYPE_SHIFT 24
155 #define KVM_REG_RISCV_SUBTYPE_MASK 0x0000000000FF0000
156 #define KVM_REG_RISCV_SUBTYPE_SHIFT 16
158 /* Config registers are mapped as type 1 */
159 #define KVM_REG_RISCV_CONFIG (0x01 << KVM_REG_RISCV_TYPE_SHIFT)
160 #define KVM_REG_RISCV_CONFIG_REG(name) \
161 (offsetof(struct kvm_riscv_config, name) / sizeof(unsigned long))
163 /* Core registers are mapped as type 2 */
164 #define KVM_REG_RISCV_CORE (0x02 << KVM_REG_RISCV_TYPE_SHIFT)
165 #define KVM_REG_RISCV_CORE_REG(name) \
166 (offsetof(struct kvm_riscv_core, name) / sizeof(unsigned long))
168 /* Control and status registers are mapped as type 3 */
169 #define KVM_REG_RISCV_CSR (0x03 << KVM_REG_RISCV_TYPE_SHIFT)
170 #define KVM_REG_RISCV_CSR_GENERAL (0x0 << KVM_REG_RISCV_SUBTYPE_SHIFT)
171 #define KVM_REG_RISCV_CSR_AIA (0x1 << KVM_REG_RISCV_SUBTYPE_SHIFT)
172 #define KVM_REG_RISCV_CSR_REG(name) \
173 (offsetof(struct kvm_riscv_csr, name) / sizeof(unsigned long))
174 #define KVM_REG_RISCV_CSR_AIA_REG(name) \
175 (offsetof(struct kvm_riscv_aia_csr, name) / sizeof(unsigned long))
177 /* Timer registers are mapped as type 4 */
178 #define KVM_REG_RISCV_TIMER (0x04 << KVM_REG_RISCV_TYPE_SHIFT)
179 #define KVM_REG_RISCV_TIMER_REG(name) \
180 (offsetof(struct kvm_riscv_timer, name) / sizeof(__u64))
182 /* F extension registers are mapped as type 5 */
183 #define KVM_REG_RISCV_FP_F (0x05 << KVM_REG_RISCV_TYPE_SHIFT)
184 #define KVM_REG_RISCV_FP_F_REG(name) \
185 (offsetof(struct __riscv_f_ext_state, name) / sizeof(__u32))
187 /* D extension registers are mapped as type 6 */
188 #define KVM_REG_RISCV_FP_D (0x06 << KVM_REG_RISCV_TYPE_SHIFT)
189 #define KVM_REG_RISCV_FP_D_REG(name) \
190 (offsetof(struct __riscv_d_ext_state, name) / sizeof(__u64))
192 /* ISA Extension registers are mapped as type 7 */
193 #define KVM_REG_RISCV_ISA_EXT (0x07 << KVM_REG_RISCV_TYPE_SHIFT)
195 /* SBI extension registers are mapped as type 8 */
196 #define KVM_REG_RISCV_SBI_EXT (0x08 << KVM_REG_RISCV_TYPE_SHIFT)
197 #define KVM_REG_RISCV_SBI_SINGLE (0x0 << KVM_REG_RISCV_SUBTYPE_SHIFT)
198 #define KVM_REG_RISCV_SBI_MULTI_EN (0x1 << KVM_REG_RISCV_SUBTYPE_SHIFT)
199 #define KVM_REG_RISCV_SBI_MULTI_DIS (0x2 << KVM_REG_RISCV_SUBTYPE_SHIFT)
200 #define KVM_REG_RISCV_SBI_MULTI_REG(__ext_id) \
201 ((__ext_id) / __BITS_PER_LONG)
202 #define KVM_REG_RISCV_SBI_MULTI_MASK(__ext_id) \
203 (1UL << ((__ext_id) % __BITS_PER_LONG))
204 #define KVM_REG_RISCV_SBI_MULTI_REG_LAST \
205 KVM_REG_RISCV_SBI_MULTI_REG(KVM_RISCV_SBI_EXT_MAX - 1)
209 #endif /* __LINUX_KVM_RISCV_H */