Skip to content

Commit c532de5

Browse files
lixianglaichenhuacai
authored andcommitted
LoongArch: KVM: Add IPI device support
Add device model for IPI interrupt controller, implement basic create & destroy interfaces, and register device model to kvm device table. Signed-off-by: Tianrui Zhao <[email protected]> Signed-off-by: Xianglai Li <[email protected]> Signed-off-by: Huacai Chen <[email protected]>
1 parent 948ccbd commit c532de5

File tree

7 files changed

+162
-2
lines changed

7 files changed

+162
-2
lines changed

arch/loongarch/include/asm/kvm_host.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <asm/inst.h>
2020
#include <asm/kvm_mmu.h>
21+
#include <asm/kvm_ipi.h>
2122
#include <asm/loongarch.h>
2223

2324
/* Loongarch KVM register ids */
@@ -117,6 +118,7 @@ struct kvm_arch {
117118

118119
s64 time_offset;
119120
struct kvm_context __percpu *vmcs;
121+
struct loongarch_ipi *ipi;
120122
};
121123

122124
#define CSR_MAX_NUMS 0x800
@@ -221,6 +223,8 @@ struct kvm_vcpu_arch {
221223
int last_sched_cpu;
222224
/* mp state */
223225
struct kvm_mp_state mp_state;
226+
/* ipi state */
227+
struct ipi_state ipi_state;
224228
/* cpucfg */
225229
u32 cpucfg[KVM_MAX_CPUCFG_REGS];
226230

arch/loongarch/include/asm/kvm_ipi.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Copyright (C) 2024 Loongson Technology Corporation Limited
4+
*/
5+
6+
#ifndef __ASM_KVM_IPI_H
7+
#define __ASM_KVM_IPI_H
8+
9+
#include <kvm/iodev.h>
10+
11+
#define LARCH_INT_IPI 12
12+
13+
struct loongarch_ipi {
14+
spinlock_t lock;
15+
struct kvm *kvm;
16+
struct kvm_io_device device;
17+
};
18+
19+
struct ipi_state {
20+
spinlock_t lock;
21+
uint32_t status;
22+
uint32_t en;
23+
uint32_t set;
24+
uint32_t clear;
25+
uint64_t buf[4];
26+
};
27+
28+
#define IOCSR_IPI_BASE 0x1000
29+
#define IOCSR_IPI_SIZE 0x160
30+
31+
int kvm_loongarch_register_ipi_device(void);
32+
33+
#endif

arch/loongarch/kvm/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ kvm-y += timer.o
1818
kvm-y += tlb.o
1919
kvm-y += vcpu.o
2020
kvm-y += vm.o
21+
kvm-y += intc/ipi.o
2122

2223
CFLAGS_exit.o += $(call cc-option,-Wno-override-init,)

arch/loongarch/kvm/intc/ipi.c

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Copyright (C) 2024 Loongson Technology Corporation Limited
4+
*/
5+
6+
#include <linux/kvm_host.h>
7+
#include <asm/kvm_ipi.h>
8+
#include <asm/kvm_vcpu.h>
9+
10+
static int kvm_ipi_read(struct kvm_vcpu *vcpu,
11+
struct kvm_io_device *dev,
12+
gpa_t addr, int len, void *val)
13+
{
14+
return 0;
15+
}
16+
17+
static int kvm_ipi_write(struct kvm_vcpu *vcpu,
18+
struct kvm_io_device *dev,
19+
gpa_t addr, int len, const void *val)
20+
{
21+
return 0;
22+
}
23+
24+
static const struct kvm_io_device_ops kvm_ipi_ops = {
25+
.read = kvm_ipi_read,
26+
.write = kvm_ipi_write,
27+
};
28+
29+
static int kvm_ipi_get_attr(struct kvm_device *dev,
30+
struct kvm_device_attr *attr)
31+
{
32+
return 0;
33+
}
34+
35+
static int kvm_ipi_set_attr(struct kvm_device *dev,
36+
struct kvm_device_attr *attr)
37+
{
38+
return 0;
39+
}
40+
41+
static int kvm_ipi_create(struct kvm_device *dev, u32 type)
42+
{
43+
int ret;
44+
struct kvm *kvm;
45+
struct kvm_io_device *device;
46+
struct loongarch_ipi *s;
47+
48+
if (!dev) {
49+
kvm_err("%s: kvm_device ptr is invalid!\n", __func__);
50+
return -EINVAL;
51+
}
52+
53+
kvm = dev->kvm;
54+
if (kvm->arch.ipi) {
55+
kvm_err("%s: LoongArch IPI has already been created!\n", __func__);
56+
return -EINVAL;
57+
}
58+
59+
s = kzalloc(sizeof(struct loongarch_ipi), GFP_KERNEL);
60+
if (!s)
61+
return -ENOMEM;
62+
63+
spin_lock_init(&s->lock);
64+
s->kvm = kvm;
65+
66+
/*
67+
* Initialize IOCSR device
68+
*/
69+
device = &s->device;
70+
kvm_iodevice_init(device, &kvm_ipi_ops);
71+
mutex_lock(&kvm->slots_lock);
72+
ret = kvm_io_bus_register_dev(kvm, KVM_IOCSR_BUS, IOCSR_IPI_BASE, IOCSR_IPI_SIZE, device);
73+
mutex_unlock(&kvm->slots_lock);
74+
if (ret < 0) {
75+
kvm_err("%s: Initialize IOCSR dev failed, ret = %d\n", __func__, ret);
76+
goto err;
77+
}
78+
79+
kvm->arch.ipi = s;
80+
return 0;
81+
82+
err:
83+
kfree(s);
84+
return -EFAULT;
85+
}
86+
87+
static void kvm_ipi_destroy(struct kvm_device *dev)
88+
{
89+
struct kvm *kvm;
90+
struct loongarch_ipi *ipi;
91+
92+
if (!dev || !dev->kvm || !dev->kvm->arch.ipi)
93+
return;
94+
95+
kvm = dev->kvm;
96+
ipi = kvm->arch.ipi;
97+
kvm_io_bus_unregister_dev(kvm, KVM_IOCSR_BUS, &ipi->device);
98+
kfree(ipi);
99+
}
100+
101+
static struct kvm_device_ops kvm_ipi_dev_ops = {
102+
.name = "kvm-loongarch-ipi",
103+
.create = kvm_ipi_create,
104+
.destroy = kvm_ipi_destroy,
105+
.set_attr = kvm_ipi_set_attr,
106+
.get_attr = kvm_ipi_get_attr,
107+
};
108+
109+
int kvm_loongarch_register_ipi_device(void)
110+
{
111+
return kvm_register_device_ops(&kvm_ipi_dev_ops, KVM_DEV_TYPE_LOONGARCH_IPI);
112+
}

arch/loongarch/kvm/main.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ void kvm_arch_disable_virtualization_cpu(void)
313313

314314
static int kvm_loongarch_env_init(void)
315315
{
316-
int cpu, order;
316+
int cpu, order, ret;
317317
void *addr;
318318
struct kvm_context *context;
319319

@@ -368,7 +368,10 @@ static int kvm_loongarch_env_init(void)
368368

369369
kvm_init_gcsr_flag();
370370

371-
return 0;
371+
/* Register LoongArch IPI interrupt controller interface. */
372+
ret = kvm_loongarch_register_ipi_device();
373+
374+
return ret;
372375
}
373376

374377
static void kvm_loongarch_env_exit(void)

arch/loongarch/kvm/vcpu.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,9 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
14751475
/* Init */
14761476
vcpu->arch.last_sched_cpu = -1;
14771477

1478+
/* Init ipi_state lock */
1479+
spin_lock_init(&vcpu->arch.ipi_state.lock);
1480+
14781481
/*
14791482
* Initialize guest register state to valid architectural reset state.
14801483
*/

include/uapi/linux/kvm.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,11 @@ enum kvm_device_type {
11581158
#define KVM_DEV_TYPE_ARM_PV_TIME KVM_DEV_TYPE_ARM_PV_TIME
11591159
KVM_DEV_TYPE_RISCV_AIA,
11601160
#define KVM_DEV_TYPE_RISCV_AIA KVM_DEV_TYPE_RISCV_AIA
1161+
KVM_DEV_TYPE_LOONGARCH_IPI,
1162+
#define KVM_DEV_TYPE_LOONGARCH_IPI KVM_DEV_TYPE_LOONGARCH_IPI
1163+
11611164
KVM_DEV_TYPE_MAX,
1165+
11621166
};
11631167

11641168
struct kvm_vfio_spapr_tce {

0 commit comments

Comments
 (0)