Skip to content

Commit e785dfa

Browse files
lixianglaichenhuacai
authored andcommitted
LoongArch: KVM: Add PCHPIC device support
Add device model for PCHPIC interrupt controller, implemente basic create & destroy interface, 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 1ad7efa commit e785dfa

File tree

6 files changed

+130
-0
lines changed

6 files changed

+130
-0
lines changed

arch/loongarch/include/asm/kvm_host.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <asm/kvm_mmu.h>
2121
#include <asm/kvm_ipi.h>
2222
#include <asm/kvm_eiointc.h>
23+
#include <asm/kvm_pch_pic.h>
2324
#include <asm/loongarch.h>
2425

2526
/* Loongarch KVM register ids */
@@ -125,6 +126,7 @@ struct kvm_arch {
125126
struct kvm_context __percpu *vmcs;
126127
struct loongarch_ipi *ipi;
127128
struct loongarch_eiointc *eiointc;
129+
struct loongarch_pch_pic *pch_pic;
128130
};
129131

130132
#define CSR_MAX_NUMS 0x800
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Copyright (C) 2024 Loongson Technology Corporation Limited
4+
*/
5+
6+
#ifndef __ASM_KVM_PCH_PIC_H
7+
#define __ASM_KVM_PCH_PIC_H
8+
9+
#include <kvm/iodev.h>
10+
11+
struct loongarch_pch_pic {
12+
spinlock_t lock;
13+
struct kvm *kvm;
14+
struct kvm_io_device device;
15+
uint64_t mask; /* 1:disable irq, 0:enable irq */
16+
uint64_t htmsi_en; /* 1:msi */
17+
uint64_t edge; /* 1:edge triggered, 0:level triggered */
18+
uint64_t auto_ctrl0; /* only use default value 00b */
19+
uint64_t auto_ctrl1; /* only use default value 00b */
20+
uint64_t last_intirr; /* edge detection */
21+
uint64_t irr; /* interrupt request register */
22+
uint64_t isr; /* interrupt service register */
23+
uint64_t polarity; /* 0: high level trigger, 1: low level trigger */
24+
uint8_t route_entry[64]; /* default value 0, route to int0: eiointc */
25+
uint8_t htmsi_vector[64]; /* irq route table for routing to eiointc */
26+
uint64_t pch_pic_base;
27+
};
28+
29+
int kvm_loongarch_register_pch_pic_device(void);
30+
31+
#endif /* __ASM_KVM_PCH_PIC_H */

arch/loongarch/kvm/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ kvm-y += vcpu.o
2020
kvm-y += vm.o
2121
kvm-y += intc/ipi.o
2222
kvm-y += intc/eiointc.o
23+
kvm-y += intc/pch_pic.o
2324

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

arch/loongarch/kvm/intc/pch_pic.c

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Copyright (C) 2024 Loongson Technology Corporation Limited
4+
*/
5+
6+
#include <asm/kvm_eiointc.h>
7+
#include <asm/kvm_pch_pic.h>
8+
#include <asm/kvm_vcpu.h>
9+
#include <linux/count_zeros.h>
10+
11+
static int kvm_pch_pic_read(struct kvm_vcpu *vcpu,
12+
struct kvm_io_device *dev,
13+
gpa_t addr, int len, void *val)
14+
{
15+
return 0;
16+
}
17+
18+
static int kvm_pch_pic_write(struct kvm_vcpu *vcpu,
19+
struct kvm_io_device *dev,
20+
gpa_t addr, int len, const void *val)
21+
{
22+
return 0;
23+
}
24+
25+
static const struct kvm_io_device_ops kvm_pch_pic_ops = {
26+
.read = kvm_pch_pic_read,
27+
.write = kvm_pch_pic_write,
28+
};
29+
30+
static int kvm_pch_pic_get_attr(struct kvm_device *dev,
31+
struct kvm_device_attr *attr)
32+
{
33+
return 0;
34+
}
35+
36+
static int kvm_pch_pic_set_attr(struct kvm_device *dev,
37+
struct kvm_device_attr *attr)
38+
{
39+
return 0;
40+
}
41+
42+
static int kvm_pch_pic_create(struct kvm_device *dev, u32 type)
43+
{
44+
struct kvm *kvm = dev->kvm;
45+
struct loongarch_pch_pic *s;
46+
47+
/* pch pic should not has been created */
48+
if (kvm->arch.pch_pic)
49+
return -EINVAL;
50+
51+
s = kzalloc(sizeof(struct loongarch_pch_pic), GFP_KERNEL);
52+
if (!s)
53+
return -ENOMEM;
54+
55+
spin_lock_init(&s->lock);
56+
s->kvm = kvm;
57+
kvm->arch.pch_pic = s;
58+
59+
return 0;
60+
}
61+
62+
static void kvm_pch_pic_destroy(struct kvm_device *dev)
63+
{
64+
struct kvm *kvm;
65+
struct loongarch_pch_pic *s;
66+
67+
if (!dev || !dev->kvm || !dev->kvm->arch.pch_pic)
68+
return;
69+
70+
kvm = dev->kvm;
71+
s = kvm->arch.pch_pic;
72+
/* unregister pch pic device and free it's memory */
73+
kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &s->device);
74+
kfree(s);
75+
}
76+
77+
static struct kvm_device_ops kvm_pch_pic_dev_ops = {
78+
.name = "kvm-loongarch-pch-pic",
79+
.create = kvm_pch_pic_create,
80+
.destroy = kvm_pch_pic_destroy,
81+
.set_attr = kvm_pch_pic_set_attr,
82+
.get_attr = kvm_pch_pic_get_attr,
83+
};
84+
85+
int kvm_loongarch_register_pch_pic_device(void)
86+
{
87+
return kvm_register_device_ops(&kvm_pch_pic_dev_ops, KVM_DEV_TYPE_LOONGARCH_PCHPIC);
88+
}

arch/loongarch/kvm/main.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <asm/cpufeature.h>
1111
#include <asm/kvm_csr.h>
1212
#include <asm/kvm_eiointc.h>
13+
#include <asm/kvm_pch_pic.h>
1314
#include "trace.h"
1415

1516
unsigned long vpid_mask;
@@ -376,6 +377,11 @@ static int kvm_loongarch_env_init(void)
376377

377378
/* Register LoongArch EIOINTC interrupt controller interface. */
378379
ret = kvm_loongarch_register_eiointc_device();
380+
if (ret)
381+
return ret;
382+
383+
/* Register LoongArch PCH-PIC interrupt controller interface. */
384+
ret = kvm_loongarch_register_pch_pic_device();
379385

380386
return ret;
381387
}

include/uapi/linux/kvm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,8 @@ enum kvm_device_type {
11621162
#define KVM_DEV_TYPE_LOONGARCH_IPI KVM_DEV_TYPE_LOONGARCH_IPI
11631163
KVM_DEV_TYPE_LOONGARCH_EIOINTC,
11641164
#define KVM_DEV_TYPE_LOONGARCH_EIOINTC KVM_DEV_TYPE_LOONGARCH_EIOINTC
1165+
KVM_DEV_TYPE_LOONGARCH_PCHPIC,
1166+
#define KVM_DEV_TYPE_LOONGARCH_PCHPIC KVM_DEV_TYPE_LOONGARCH_PCHPIC
11651167

11661168
KVM_DEV_TYPE_MAX,
11671169

0 commit comments

Comments
 (0)