1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2025 Loongson Technology Corporation Limited
4 */
5
6 #include <linux/kvm_host.h>
7 #include <asm/kvm_csr.h>
8 #include <asm/kvm_dmsintc.h>
9 #include <asm/kvm_vcpu.h>
10
dmsintc_inject_irq(struct kvm_vcpu * vcpu)11 void dmsintc_inject_irq(struct kvm_vcpu *vcpu)
12 {
13 unsigned int i;
14 unsigned long vector[4], old;
15 struct dmsintc_state *ds = &vcpu->arch.dmsintc_state;
16
17 if (!ds)
18 return;
19
20 for (i = 0; i < 4; i++) {
21 old = atomic64_read(&(ds->vector_map[i]));
22 vector[i] = old ? atomic64_xchg(&(ds->vector_map[i]), 0) : 0;
23 }
24
25 if (vector[0]) {
26 old = kvm_read_hw_gcsr(LOONGARCH_CSR_ISR0);
27 kvm_write_hw_gcsr(LOONGARCH_CSR_ISR0, vector[0] | old);
28 }
29
30 if (vector[1]) {
31 old = kvm_read_hw_gcsr(LOONGARCH_CSR_ISR1);
32 kvm_write_hw_gcsr(LOONGARCH_CSR_ISR1, vector[1] | old);
33 }
34
35 if (vector[2]) {
36 old = kvm_read_hw_gcsr(LOONGARCH_CSR_ISR2);
37 kvm_write_hw_gcsr(LOONGARCH_CSR_ISR2, vector[2] | old);
38 }
39
40 if (vector[3]) {
41 old = kvm_read_hw_gcsr(LOONGARCH_CSR_ISR3);
42 kvm_write_hw_gcsr(LOONGARCH_CSR_ISR3, vector[3] | old);
43 }
44 }
45
dmsintc_deliver_msi_to_vcpu(struct kvm * kvm,struct kvm_vcpu * vcpu,u32 vector,int level)46 int dmsintc_deliver_msi_to_vcpu(struct kvm *kvm,
47 struct kvm_vcpu *vcpu, u32 vector, int level)
48 {
49 struct dmsintc_state *ds = &vcpu->arch.dmsintc_state;
50
51 if (!level)
52 return 0;
53 if (!vcpu || vector >= 256)
54 return -EINVAL;
55 if (!ds)
56 return -ENODEV;
57
58 if (!kvm_guest_has_msgint(&vcpu->arch))
59 return -EINVAL;
60
61 set_bit(vector, (unsigned long *)&ds->vector_map);
62 kvm_queue_irq(vcpu, INT_AVEC);
63 kvm_vcpu_kick(vcpu);
64
65 return 0;
66 }
67
dmsintc_set_irq(struct kvm * kvm,u64 addr,int data,int level)68 int dmsintc_set_irq(struct kvm *kvm, u64 addr, int data, int level)
69 {
70 unsigned int irq, cpu;
71 struct kvm_vcpu *vcpu;
72
73 irq = (addr >> AVEC_IRQ_SHIFT) & AVEC_IRQ_MASK;
74 cpu = (addr >> AVEC_CPU_SHIFT) & kvm->arch.dmsintc->cpu_mask;
75 if (cpu >= KVM_MAX_VCPUS)
76 return -EINVAL;
77 vcpu = kvm_get_vcpu_by_cpuid(kvm, cpu);
78 if (!vcpu)
79 return -EINVAL;
80
81 return dmsintc_deliver_msi_to_vcpu(kvm, vcpu, irq, level);
82 }
83
kvm_dmsintc_ctrl_access(struct kvm_device * dev,struct kvm_device_attr * attr,bool is_write)84 static int kvm_dmsintc_ctrl_access(struct kvm_device *dev,
85 struct kvm_device_attr *attr, bool is_write)
86 {
87 int addr = attr->attr;
88 unsigned long cpu_bit, val;
89 void __user *data = (void __user *)attr->addr;
90 struct loongarch_dmsintc *s = dev->kvm->arch.dmsintc;
91
92 switch (addr) {
93 case KVM_DEV_LOONGARCH_DMSINTC_MSG_ADDR_BASE:
94 if (is_write) {
95 if (copy_from_user(&val, data, sizeof(s->msg_addr_base)))
96 return -EFAULT;
97 if (s->msg_addr_base)
98 return -EFAULT; /* Duplicate setting are not allowed. */
99 if ((val & (BIT(AVEC_CPU_SHIFT) - 1)) != 0)
100 return -EINVAL;
101 s->msg_addr_base = val;
102 cpu_bit = find_first_bit((unsigned long *)&(s->msg_addr_base), 64) - AVEC_CPU_SHIFT;
103 cpu_bit = min(cpu_bit, AVEC_CPU_BIT);
104 s->cpu_mask = GENMASK(cpu_bit - 1, 0) & AVEC_CPU_MASK;
105 }
106 break;
107 case KVM_DEV_LOONGARCH_DMSINTC_MSG_ADDR_SIZE:
108 if (is_write) {
109 if (copy_from_user(&val, data, sizeof(s->msg_addr_size)))
110 return -EFAULT;
111 if (s->msg_addr_size)
112 return -EFAULT; /*Duplicate setting are not allowed. */
113 s->msg_addr_size = val;
114 }
115 break;
116 default:
117 kvm_pr_unimpl("%s: unknown dmsintc register, addr = %d\n", __func__, addr);
118 return -ENXIO;
119 }
120
121 return 0;
122 }
123
kvm_dmsintc_set_attr(struct kvm_device * dev,struct kvm_device_attr * attr)124 static int kvm_dmsintc_set_attr(struct kvm_device *dev,
125 struct kvm_device_attr *attr)
126 {
127 switch (attr->group) {
128 case KVM_DEV_LOONGARCH_DMSINTC_GRP_CTRL:
129 return kvm_dmsintc_ctrl_access(dev, attr, true);
130 default:
131 kvm_pr_unimpl("%s: unknown group (%d)\n", __func__, attr->group);
132 return -EINVAL;
133 }
134 }
135
kvm_dmsintc_create(struct kvm_device * dev,u32 type)136 static int kvm_dmsintc_create(struct kvm_device *dev, u32 type)
137 {
138 struct kvm *kvm;
139 struct loongarch_dmsintc *s;
140
141 if (!dev) {
142 kvm_pr_unimpl("%s: kvm_device ptr is invalid!\n", __func__);
143 return -EINVAL;
144 }
145
146 kvm = dev->kvm;
147 if (kvm->arch.dmsintc) {
148 kvm_pr_unimpl("%s: LoongArch DMSINTC has already been created!\n", __func__);
149 return -EINVAL;
150 }
151
152 s = kzalloc_obj(struct loongarch_dmsintc);
153 if (!s)
154 return -ENOMEM;
155
156 s->kvm = kvm;
157 kvm->arch.dmsintc = s;
158
159 return 0;
160 }
161
kvm_dmsintc_destroy(struct kvm_device * dev)162 static void kvm_dmsintc_destroy(struct kvm_device *dev)
163 {
164
165 if (!dev || !dev->kvm || !dev->kvm->arch.dmsintc)
166 return;
167
168 kfree(dev->kvm->arch.dmsintc);
169 kfree(dev);
170 }
171
172 static struct kvm_device_ops kvm_dmsintc_dev_ops = {
173 .name = "kvm-loongarch-dmsintc",
174 .create = kvm_dmsintc_create,
175 .destroy = kvm_dmsintc_destroy,
176 .set_attr = kvm_dmsintc_set_attr,
177 };
178
kvm_loongarch_register_dmsintc_device(void)179 int kvm_loongarch_register_dmsintc_device(void)
180 {
181 return kvm_register_device_ops(&kvm_dmsintc_dev_ops, KVM_DEV_TYPE_LOONGARCH_DMSINTC);
182 }
183
kvm_loongarch_unregister_dmsintc_device(void)184 void kvm_loongarch_unregister_dmsintc_device(void)
185 {
186 kvm_unregister_device_ops(KVM_DEV_TYPE_LOONGARCH_DMSINTC);
187 }
188