blob: b38e94e8f74ad577b5a337dc2082565be83608b6 [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
Marc Zyngier4493b1c2016-04-26 11:06:12 +01002/*
3 * VGIC MMIO handling functions
Marc Zyngier4493b1c2016-04-26 11:06:12 +01004 */
5
6#include <linux/bitops.h>
7#include <linux/bsearch.h>
Marc Zyngieref1820b2020-03-04 20:33:25 +00008#include <linux/interrupt.h>
9#include <linux/irq.h>
Marc Zyngier4493b1c2016-04-26 11:06:12 +010010#include <linux/kvm.h>
11#include <linux/kvm_host.h>
12#include <kvm/iodev.h>
Christoffer Dalldf635c52017-09-01 16:25:12 +020013#include <kvm/arm_arch_timer.h>
Marc Zyngier4493b1c2016-04-26 11:06:12 +010014#include <kvm/arm_vgic.h>
15
16#include "vgic.h"
17#include "vgic-mmio.h"
18
19unsigned long vgic_mmio_read_raz(struct kvm_vcpu *vcpu,
20 gpa_t addr, unsigned int len)
21{
22 return 0;
23}
24
25unsigned long vgic_mmio_read_rao(struct kvm_vcpu *vcpu,
26 gpa_t addr, unsigned int len)
27{
28 return -1UL;
29}
30
31void vgic_mmio_write_wi(struct kvm_vcpu *vcpu, gpa_t addr,
32 unsigned int len, unsigned long val)
33{
34 /* Ignore */
35}
36
Christoffer Dallc6e09172018-07-16 15:06:23 +020037int vgic_mmio_uaccess_write_wi(struct kvm_vcpu *vcpu, gpa_t addr,
38 unsigned int len, unsigned long val)
39{
40 /* Ignore */
41 return 0;
42}
43
Christoffer Dalld53c2c292018-07-16 15:06:25 +020044unsigned long vgic_mmio_read_group(struct kvm_vcpu *vcpu,
45 gpa_t addr, unsigned int len)
46{
47 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
48 u32 value = 0;
49 int i;
50
51 /* Loop over all IRQs affected by this read */
52 for (i = 0; i < len * 8; i++) {
53 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
54
55 if (irq->group)
56 value |= BIT(i);
57
58 vgic_put_irq(vcpu->kvm, irq);
59 }
60
61 return value;
62}
63
Marc Zyngieref1820b2020-03-04 20:33:25 +000064static void vgic_update_vsgi(struct vgic_irq *irq)
65{
66 WARN_ON(its_prop_update_vsgi(irq->host_irq, irq->priority, irq->group));
67}
68
Christoffer Dalld53c2c292018-07-16 15:06:25 +020069void vgic_mmio_write_group(struct kvm_vcpu *vcpu, gpa_t addr,
70 unsigned int len, unsigned long val)
71{
72 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
73 int i;
74 unsigned long flags;
75
76 for (i = 0; i < len * 8; i++) {
77 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
78
Julien Thierry8fa3adb2019-01-07 15:06:15 +000079 raw_spin_lock_irqsave(&irq->irq_lock, flags);
Christoffer Dalld53c2c292018-07-16 15:06:25 +020080 irq->group = !!(val & BIT(i));
Marc Zyngieref1820b2020-03-04 20:33:25 +000081 if (irq->hw && vgic_irq_is_sgi(irq->intid)) {
82 vgic_update_vsgi(irq);
83 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
84 } else {
85 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
86 }
Christoffer Dalld53c2c292018-07-16 15:06:25 +020087
88 vgic_put_irq(vcpu->kvm, irq);
89 }
90}
91
Andre Przywarafd122e62015-12-01 14:33:05 +000092/*
93 * Read accesses to both GICD_ICENABLER and GICD_ISENABLER return the value
94 * of the enabled bit, so there is only one function for both here.
95 */
96unsigned long vgic_mmio_read_enable(struct kvm_vcpu *vcpu,
97 gpa_t addr, unsigned int len)
98{
99 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
100 u32 value = 0;
101 int i;
102
103 /* Loop over all IRQs affected by this read */
104 for (i = 0; i < len * 8; i++) {
105 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
106
107 if (irq->enabled)
108 value |= (1U << i);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100109
110 vgic_put_irq(vcpu->kvm, irq);
Andre Przywarafd122e62015-12-01 14:33:05 +0000111 }
112
113 return value;
114}
115
116void vgic_mmio_write_senable(struct kvm_vcpu *vcpu,
117 gpa_t addr, unsigned int len,
118 unsigned long val)
119{
120 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
121 int i;
Christoffer Dall006df0f2016-10-16 22:19:11 +0200122 unsigned long flags;
Andre Przywarafd122e62015-12-01 14:33:05 +0000123
124 for_each_set_bit(i, &val, len * 8) {
125 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
126
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000127 raw_spin_lock_irqsave(&irq->irq_lock, flags);
Marc Zyngieref1820b2020-03-04 20:33:25 +0000128 if (irq->hw && vgic_irq_is_sgi(irq->intid)) {
129 if (!irq->enabled) {
130 struct irq_data *data;
131
132 irq->enabled = true;
133 data = &irq_to_desc(irq->host_irq)->irq_data;
134 while (irqd_irq_disabled(data))
135 enable_irq(irq->host_irq);
136 }
137
138 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
139 vgic_put_irq(vcpu->kvm, irq);
140
141 continue;
142 } else if (vgic_irq_is_mapped_level(irq)) {
Alexandru Elisei16e604a2019-08-07 10:53:20 +0100143 bool was_high = irq->line_level;
144
145 /*
146 * We need to update the state of the interrupt because
147 * the guest might have changed the state of the device
148 * while the interrupt was disabled at the VGIC level.
149 */
150 irq->line_level = vgic_get_phys_line_level(irq);
151 /*
152 * Deactivate the physical interrupt so the GIC will let
153 * us know when it is asserted again.
154 */
155 if (!irq->active && was_high && !irq->line_level)
156 vgic_irq_set_phys_active(irq, false);
157 }
Andre Przywarafd122e62015-12-01 14:33:05 +0000158 irq->enabled = true;
Christoffer Dall006df0f2016-10-16 22:19:11 +0200159 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100160
161 vgic_put_irq(vcpu->kvm, irq);
Andre Przywarafd122e62015-12-01 14:33:05 +0000162 }
163}
164
165void vgic_mmio_write_cenable(struct kvm_vcpu *vcpu,
166 gpa_t addr, unsigned int len,
167 unsigned long val)
168{
169 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
170 int i;
Christoffer Dall006df0f2016-10-16 22:19:11 +0200171 unsigned long flags;
Andre Przywarafd122e62015-12-01 14:33:05 +0000172
173 for_each_set_bit(i, &val, len * 8) {
174 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
175
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000176 raw_spin_lock_irqsave(&irq->irq_lock, flags);
Marc Zyngieref1820b2020-03-04 20:33:25 +0000177 if (irq->hw && vgic_irq_is_sgi(irq->intid) && irq->enabled)
178 disable_irq_nosync(irq->host_irq);
Andre Przywarafd122e62015-12-01 14:33:05 +0000179
180 irq->enabled = false;
181
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000182 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100183 vgic_put_irq(vcpu->kvm, irq);
Andre Przywarafd122e62015-12-01 14:33:05 +0000184 }
185}
186
Andre Przywara96b29802015-12-01 14:33:41 +0000187unsigned long vgic_mmio_read_pending(struct kvm_vcpu *vcpu,
188 gpa_t addr, unsigned int len)
189{
190 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
191 u32 value = 0;
192 int i;
193
194 /* Loop over all IRQs affected by this read */
195 for (i = 0; i < len * 8; i++) {
196 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
Andre Przywara62b06f82018-03-06 09:21:06 +0000197 unsigned long flags;
Marc Zyngieref1820b2020-03-04 20:33:25 +0000198 bool val;
Andre Przywara96b29802015-12-01 14:33:41 +0000199
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000200 raw_spin_lock_irqsave(&irq->irq_lock, flags);
Marc Zyngieref1820b2020-03-04 20:33:25 +0000201 if (irq->hw && vgic_irq_is_sgi(irq->intid)) {
202 int err;
203
204 val = false;
205 err = irq_get_irqchip_state(irq->host_irq,
206 IRQCHIP_STATE_PENDING,
207 &val);
208 WARN_RATELIMIT(err, "IRQ %d", irq->host_irq);
209 } else {
210 val = irq_is_pending(irq);
211 }
212
213 value |= ((u32)val << i);
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000214 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100215
216 vgic_put_irq(vcpu->kvm, irq);
Andre Przywara96b29802015-12-01 14:33:41 +0000217 }
218
219 return value;
220}
221
Christoffer Dalldf635c52017-09-01 16:25:12 +0200222/* Must be called with irq->irq_lock held */
223static void vgic_hw_irq_spending(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
224 bool is_uaccess)
225{
226 if (is_uaccess)
227 return;
228
229 irq->pending_latch = true;
230 vgic_irq_set_phys_active(irq, true);
231}
232
Marc Zyngier82e40f52019-08-28 11:10:16 +0100233static bool is_vgic_v2_sgi(struct kvm_vcpu *vcpu, struct vgic_irq *irq)
234{
235 return (vgic_irq_is_sgi(irq->intid) &&
236 vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V2);
237}
238
Andre Przywara96b29802015-12-01 14:33:41 +0000239void vgic_mmio_write_spending(struct kvm_vcpu *vcpu,
240 gpa_t addr, unsigned int len,
241 unsigned long val)
242{
Paolo Bonzini7495e222020-01-09 09:57:19 -0500243 bool is_uaccess = !kvm_get_running_vcpu();
Andre Przywara96b29802015-12-01 14:33:41 +0000244 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
245 int i;
Christoffer Dall006df0f2016-10-16 22:19:11 +0200246 unsigned long flags;
Andre Przywara96b29802015-12-01 14:33:41 +0000247
248 for_each_set_bit(i, &val, len * 8) {
249 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
250
Marc Zyngier82e40f52019-08-28 11:10:16 +0100251 /* GICD_ISPENDR0 SGI bits are WI */
252 if (is_vgic_v2_sgi(vcpu, irq)) {
253 vgic_put_irq(vcpu->kvm, irq);
254 continue;
255 }
256
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000257 raw_spin_lock_irqsave(&irq->irq_lock, flags);
Marc Zyngieref1820b2020-03-04 20:33:25 +0000258
259 if (irq->hw && vgic_irq_is_sgi(irq->intid)) {
260 /* HW SGI? Ask the GIC to inject it */
261 int err;
262 err = irq_set_irqchip_state(irq->host_irq,
263 IRQCHIP_STATE_PENDING,
264 true);
265 WARN_RATELIMIT(err, "IRQ %d", irq->host_irq);
266
267 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
268 vgic_put_irq(vcpu->kvm, irq);
269
270 continue;
271 }
272
Christoffer Dalldf635c52017-09-01 16:25:12 +0200273 if (irq->hw)
274 vgic_hw_irq_spending(vcpu, irq, is_uaccess);
275 else
276 irq->pending_latch = true;
Christoffer Dall006df0f2016-10-16 22:19:11 +0200277 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100278 vgic_put_irq(vcpu->kvm, irq);
Andre Przywara96b29802015-12-01 14:33:41 +0000279 }
280}
281
Christoffer Dalldf635c52017-09-01 16:25:12 +0200282/* Must be called with irq->irq_lock held */
283static void vgic_hw_irq_cpending(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
284 bool is_uaccess)
285{
286 if (is_uaccess)
287 return;
288
289 irq->pending_latch = false;
290
291 /*
292 * We don't want the guest to effectively mask the physical
293 * interrupt by doing a write to SPENDR followed by a write to
294 * CPENDR for HW interrupts, so we clear the active state on
295 * the physical side if the virtual interrupt is not active.
296 * This may lead to taking an additional interrupt on the
297 * host, but that should not be a problem as the worst that
298 * can happen is an additional vgic injection. We also clear
299 * the pending state to maintain proper semantics for edge HW
300 * interrupts.
301 */
302 vgic_irq_set_phys_pending(irq, false);
303 if (!irq->active)
304 vgic_irq_set_phys_active(irq, false);
305}
306
Andre Przywara96b29802015-12-01 14:33:41 +0000307void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu,
308 gpa_t addr, unsigned int len,
309 unsigned long val)
310{
Paolo Bonzini7495e222020-01-09 09:57:19 -0500311 bool is_uaccess = !kvm_get_running_vcpu();
Andre Przywara96b29802015-12-01 14:33:41 +0000312 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
313 int i;
Christoffer Dall006df0f2016-10-16 22:19:11 +0200314 unsigned long flags;
Andre Przywara96b29802015-12-01 14:33:41 +0000315
316 for_each_set_bit(i, &val, len * 8) {
317 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
318
Marc Zyngier82e40f52019-08-28 11:10:16 +0100319 /* GICD_ICPENDR0 SGI bits are WI */
320 if (is_vgic_v2_sgi(vcpu, irq)) {
321 vgic_put_irq(vcpu->kvm, irq);
322 continue;
323 }
324
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000325 raw_spin_lock_irqsave(&irq->irq_lock, flags);
Andre Przywara96b29802015-12-01 14:33:41 +0000326
Marc Zyngieref1820b2020-03-04 20:33:25 +0000327 if (irq->hw && vgic_irq_is_sgi(irq->intid)) {
328 /* HW SGI? Ask the GIC to clear its pending bit */
329 int err;
330 err = irq_set_irqchip_state(irq->host_irq,
331 IRQCHIP_STATE_PENDING,
332 false);
333 WARN_RATELIMIT(err, "IRQ %d", irq->host_irq);
334
335 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
336 vgic_put_irq(vcpu->kvm, irq);
337
338 continue;
339 }
340
Christoffer Dalldf635c52017-09-01 16:25:12 +0200341 if (irq->hw)
342 vgic_hw_irq_cpending(vcpu, irq, is_uaccess);
343 else
344 irq->pending_latch = false;
Andre Przywara96b29802015-12-01 14:33:41 +0000345
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000346 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100347 vgic_put_irq(vcpu->kvm, irq);
Andre Przywara96b29802015-12-01 14:33:41 +0000348 }
349}
350
Marc Zyngier9a50ebb2020-04-06 16:21:20 +0100351
352/*
353 * If we are fiddling with an IRQ's active state, we have to make sure the IRQ
354 * is not queued on some running VCPU's LRs, because then the change to the
355 * active state can be overwritten when the VCPU's state is synced coming back
356 * from the guest.
357 *
358 * For shared interrupts as well as GICv3 private interrupts, we have to
359 * stop all the VCPUs because interrupts can be migrated while we don't hold
360 * the IRQ locks and we don't want to be chasing moving targets.
361 *
362 * For GICv2 private interrupts we don't have to do anything because
363 * userspace accesses to the VGIC state already require all VCPUs to be
364 * stopped, and only the VCPU itself can modify its private interrupts
365 * active state, which guarantees that the VCPU is not running.
366 */
367static void vgic_access_active_prepare(struct kvm_vcpu *vcpu, u32 intid)
368{
369 if (vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3 ||
370 intid >= VGIC_NR_PRIVATE_IRQS)
371 kvm_arm_halt_guest(vcpu->kvm);
372}
373
374/* See vgic_access_active_prepare */
375static void vgic_access_active_finish(struct kvm_vcpu *vcpu, u32 intid)
376{
377 if (vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3 ||
378 intid >= VGIC_NR_PRIVATE_IRQS)
379 kvm_arm_resume_guest(vcpu->kvm);
380}
381
382static unsigned long __vgic_mmio_read_active(struct kvm_vcpu *vcpu,
383 gpa_t addr, unsigned int len)
Andre Przywara69b6fe02015-12-01 12:40:58 +0000384{
385 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
386 u32 value = 0;
387 int i;
388
389 /* Loop over all IRQs affected by this read */
390 for (i = 0; i < len * 8; i++) {
391 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
392
Marc Zyngier9a50ebb2020-04-06 16:21:20 +0100393 /*
394 * Even for HW interrupts, don't evaluate the HW state as
395 * all the guest is interested in is the virtual state.
396 */
Andre Przywara69b6fe02015-12-01 12:40:58 +0000397 if (irq->active)
398 value |= (1U << i);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100399
400 vgic_put_irq(vcpu->kvm, irq);
Andre Przywara69b6fe02015-12-01 12:40:58 +0000401 }
402
403 return value;
404}
405
Marc Zyngier9a50ebb2020-04-06 16:21:20 +0100406unsigned long vgic_mmio_read_active(struct kvm_vcpu *vcpu,
407 gpa_t addr, unsigned int len)
408{
409 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
410 u32 val;
411
412 mutex_lock(&vcpu->kvm->lock);
413 vgic_access_active_prepare(vcpu, intid);
414
415 val = __vgic_mmio_read_active(vcpu, addr, len);
416
417 vgic_access_active_finish(vcpu, intid);
418 mutex_unlock(&vcpu->kvm->lock);
419
420 return val;
421}
422
423unsigned long vgic_uaccess_read_active(struct kvm_vcpu *vcpu,
424 gpa_t addr, unsigned int len)
425{
426 return __vgic_mmio_read_active(vcpu, addr, len);
427}
428
Christoffer Dalldf635c52017-09-01 16:25:12 +0200429/* Must be called with irq->irq_lock held */
430static void vgic_hw_irq_change_active(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
431 bool active, bool is_uaccess)
432{
433 if (is_uaccess)
434 return;
435
436 irq->active = active;
437 vgic_irq_set_phys_active(irq, active);
438}
439
Christoffer Dall35a2d582016-05-20 15:25:28 +0200440static void vgic_mmio_change_active(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
Christoffer Dalldf635c52017-09-01 16:25:12 +0200441 bool active)
Christoffer Dall35a2d582016-05-20 15:25:28 +0200442{
Christoffer Dall006df0f2016-10-16 22:19:11 +0200443 unsigned long flags;
Paolo Bonzini7495e222020-01-09 09:57:19 -0500444 struct kvm_vcpu *requester_vcpu = kvm_get_running_vcpu();
Jintack Lim370a0ec2017-03-06 05:42:37 -0800445
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000446 raw_spin_lock_irqsave(&irq->irq_lock, flags);
Jintack Lim370a0ec2017-03-06 05:42:37 -0800447
Marc Zyngieref1820b2020-03-04 20:33:25 +0000448 if (irq->hw && !vgic_irq_is_sgi(irq->intid)) {
Christoffer Dalldf635c52017-09-01 16:25:12 +0200449 vgic_hw_irq_change_active(vcpu, irq, active, !requester_vcpu);
Marc Zyngieref1820b2020-03-04 20:33:25 +0000450 } else if (irq->hw && vgic_irq_is_sgi(irq->intid)) {
451 /*
452 * GICv4.1 VSGI feature doesn't track an active state,
453 * so let's not kid ourselves, there is nothing we can
454 * do here.
455 */
456 irq->active = false;
Marc Zyngier53692902018-04-18 10:39:04 +0100457 } else {
458 u32 model = vcpu->kvm->arch.vgic.vgic_model;
Christoffer Dall60c3ab32018-12-11 12:51:03 +0100459 u8 active_source;
Marc Zyngier53692902018-04-18 10:39:04 +0100460
Christoffer Dalldf635c52017-09-01 16:25:12 +0200461 irq->active = active;
Christoffer Dall60c3ab32018-12-11 12:51:03 +0100462
463 /*
464 * The GICv2 architecture indicates that the source CPUID for
465 * an SGI should be provided during an EOI which implies that
466 * the active state is stored somewhere, but at the same time
467 * this state is not architecturally exposed anywhere and we
468 * have no way of knowing the right source.
469 *
470 * This may lead to a VCPU not being able to receive
471 * additional instances of a particular SGI after migration
472 * for a GICv2 VM on some GIC implementations. Oh well.
473 */
474 active_source = (requester_vcpu) ? requester_vcpu->vcpu_id : 0;
475
Marc Zyngier53692902018-04-18 10:39:04 +0100476 if (model == KVM_DEV_TYPE_ARM_VGIC_V2 &&
477 active && vgic_irq_is_sgi(irq->intid))
Christoffer Dall60c3ab32018-12-11 12:51:03 +0100478 irq->active_source = active_source;
Marc Zyngier53692902018-04-18 10:39:04 +0100479 }
Christoffer Dalldf635c52017-09-01 16:25:12 +0200480
481 if (irq->active)
Christoffer Dall006df0f2016-10-16 22:19:11 +0200482 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
Christoffer Dall35a2d582016-05-20 15:25:28 +0200483 else
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000484 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
Christoffer Dall35a2d582016-05-20 15:25:28 +0200485}
486
Christoffer Dall31971912017-05-16 09:44:39 +0200487static void __vgic_mmio_write_cactive(struct kvm_vcpu *vcpu,
488 gpa_t addr, unsigned int len,
489 unsigned long val)
Andre Przywara69b6fe02015-12-01 12:40:58 +0000490{
491 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
492 int i;
493
Andre Przywara69b6fe02015-12-01 12:40:58 +0000494 for_each_set_bit(i, &val, len * 8) {
495 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
Christoffer Dall35a2d582016-05-20 15:25:28 +0200496 vgic_mmio_change_active(vcpu, irq, false);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100497 vgic_put_irq(vcpu->kvm, irq);
Andre Przywara69b6fe02015-12-01 12:40:58 +0000498 }
Christoffer Dall31971912017-05-16 09:44:39 +0200499}
500
501void vgic_mmio_write_cactive(struct kvm_vcpu *vcpu,
502 gpa_t addr, unsigned int len,
503 unsigned long val)
504{
505 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
506
Christoffer Dallabd72292017-05-06 20:01:24 +0200507 mutex_lock(&vcpu->kvm->lock);
Marc Zyngier9a50ebb2020-04-06 16:21:20 +0100508 vgic_access_active_prepare(vcpu, intid);
Christoffer Dall31971912017-05-16 09:44:39 +0200509
510 __vgic_mmio_write_cactive(vcpu, addr, len, val);
511
Marc Zyngier9a50ebb2020-04-06 16:21:20 +0100512 vgic_access_active_finish(vcpu, intid);
Christoffer Dallabd72292017-05-06 20:01:24 +0200513 mutex_unlock(&vcpu->kvm->lock);
Andre Przywara69b6fe02015-12-01 12:40:58 +0000514}
515
Christoffer Dallc6e09172018-07-16 15:06:23 +0200516int vgic_mmio_uaccess_write_cactive(struct kvm_vcpu *vcpu,
Christoffer Dall31971912017-05-16 09:44:39 +0200517 gpa_t addr, unsigned int len,
518 unsigned long val)
519{
520 __vgic_mmio_write_cactive(vcpu, addr, len, val);
Christoffer Dallc6e09172018-07-16 15:06:23 +0200521 return 0;
Christoffer Dall31971912017-05-16 09:44:39 +0200522}
523
524static void __vgic_mmio_write_sactive(struct kvm_vcpu *vcpu,
525 gpa_t addr, unsigned int len,
526 unsigned long val)
527{
528 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
529 int i;
530
531 for_each_set_bit(i, &val, len * 8) {
532 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
533 vgic_mmio_change_active(vcpu, irq, true);
534 vgic_put_irq(vcpu->kvm, irq);
535 }
536}
537
Andre Przywara69b6fe02015-12-01 12:40:58 +0000538void vgic_mmio_write_sactive(struct kvm_vcpu *vcpu,
539 gpa_t addr, unsigned int len,
540 unsigned long val)
541{
542 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
Andre Przywara69b6fe02015-12-01 12:40:58 +0000543
Christoffer Dallabd72292017-05-06 20:01:24 +0200544 mutex_lock(&vcpu->kvm->lock);
Marc Zyngier9a50ebb2020-04-06 16:21:20 +0100545 vgic_access_active_prepare(vcpu, intid);
Christoffer Dall31971912017-05-16 09:44:39 +0200546
547 __vgic_mmio_write_sactive(vcpu, addr, len, val);
548
Marc Zyngier9a50ebb2020-04-06 16:21:20 +0100549 vgic_access_active_finish(vcpu, intid);
Christoffer Dallabd72292017-05-06 20:01:24 +0200550 mutex_unlock(&vcpu->kvm->lock);
Andre Przywara69b6fe02015-12-01 12:40:58 +0000551}
552
Christoffer Dallc6e09172018-07-16 15:06:23 +0200553int vgic_mmio_uaccess_write_sactive(struct kvm_vcpu *vcpu,
Christoffer Dall31971912017-05-16 09:44:39 +0200554 gpa_t addr, unsigned int len,
555 unsigned long val)
556{
557 __vgic_mmio_write_sactive(vcpu, addr, len, val);
Christoffer Dallc6e09172018-07-16 15:06:23 +0200558 return 0;
Christoffer Dall31971912017-05-16 09:44:39 +0200559}
560
Andre Przywara055658b2015-12-01 14:34:02 +0000561unsigned long vgic_mmio_read_priority(struct kvm_vcpu *vcpu,
562 gpa_t addr, unsigned int len)
563{
564 u32 intid = VGIC_ADDR_TO_INTID(addr, 8);
565 int i;
566 u64 val = 0;
567
568 for (i = 0; i < len; i++) {
569 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
570
571 val |= (u64)irq->priority << (i * 8);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100572
573 vgic_put_irq(vcpu->kvm, irq);
Andre Przywara055658b2015-12-01 14:34:02 +0000574 }
575
576 return val;
577}
578
579/*
580 * We currently don't handle changing the priority of an interrupt that
581 * is already pending on a VCPU. If there is a need for this, we would
582 * need to make this VCPU exit and re-evaluate the priorities, potentially
583 * leading to this interrupt getting presented now to the guest (if it has
584 * been masked by the priority mask before).
585 */
586void vgic_mmio_write_priority(struct kvm_vcpu *vcpu,
587 gpa_t addr, unsigned int len,
588 unsigned long val)
589{
590 u32 intid = VGIC_ADDR_TO_INTID(addr, 8);
591 int i;
Christoffer Dall006df0f2016-10-16 22:19:11 +0200592 unsigned long flags;
Andre Przywara055658b2015-12-01 14:34:02 +0000593
594 for (i = 0; i < len; i++) {
595 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
596
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000597 raw_spin_lock_irqsave(&irq->irq_lock, flags);
Andre Przywara055658b2015-12-01 14:34:02 +0000598 /* Narrow the priority range to what we actually support */
599 irq->priority = (val >> (i * 8)) & GENMASK(7, 8 - VGIC_PRI_BITS);
Marc Zyngieref1820b2020-03-04 20:33:25 +0000600 if (irq->hw && vgic_irq_is_sgi(irq->intid))
601 vgic_update_vsgi(irq);
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000602 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100603
604 vgic_put_irq(vcpu->kvm, irq);
Andre Przywara055658b2015-12-01 14:34:02 +0000605 }
606}
607
Andre Przywara79717e42015-12-01 12:41:31 +0000608unsigned long vgic_mmio_read_config(struct kvm_vcpu *vcpu,
609 gpa_t addr, unsigned int len)
610{
611 u32 intid = VGIC_ADDR_TO_INTID(addr, 2);
612 u32 value = 0;
613 int i;
614
615 for (i = 0; i < len * 4; i++) {
616 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
617
618 if (irq->config == VGIC_CONFIG_EDGE)
619 value |= (2U << (i * 2));
Andre Przywara5dd4b922016-07-15 12:43:27 +0100620
621 vgic_put_irq(vcpu->kvm, irq);
Andre Przywara79717e42015-12-01 12:41:31 +0000622 }
623
624 return value;
625}
626
627void vgic_mmio_write_config(struct kvm_vcpu *vcpu,
628 gpa_t addr, unsigned int len,
629 unsigned long val)
630{
631 u32 intid = VGIC_ADDR_TO_INTID(addr, 2);
632 int i;
Christoffer Dall006df0f2016-10-16 22:19:11 +0200633 unsigned long flags;
Andre Przywara79717e42015-12-01 12:41:31 +0000634
635 for (i = 0; i < len * 4; i++) {
Andre Przywara5dd4b922016-07-15 12:43:27 +0100636 struct vgic_irq *irq;
Andre Przywara79717e42015-12-01 12:41:31 +0000637
638 /*
639 * The configuration cannot be changed for SGIs in general,
640 * for PPIs this is IMPLEMENTATION DEFINED. The arch timer
641 * code relies on PPIs being level triggered, so we also
642 * make them read-only here.
643 */
644 if (intid + i < VGIC_NR_PRIVATE_IRQS)
645 continue;
646
Andre Przywara5dd4b922016-07-15 12:43:27 +0100647 irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000648 raw_spin_lock_irqsave(&irq->irq_lock, flags);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100649
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100650 if (test_bit(i * 2 + 1, &val))
Andre Przywara79717e42015-12-01 12:41:31 +0000651 irq->config = VGIC_CONFIG_EDGE;
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100652 else
Andre Przywara79717e42015-12-01 12:41:31 +0000653 irq->config = VGIC_CONFIG_LEVEL;
Andre Przywara5dd4b922016-07-15 12:43:27 +0100654
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000655 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100656 vgic_put_irq(vcpu->kvm, irq);
Andre Przywara79717e42015-12-01 12:41:31 +0000657 }
658}
659
Vijaya Kumar Ke96a0062017-01-26 19:50:52 +0530660u64 vgic_read_irq_line_level_info(struct kvm_vcpu *vcpu, u32 intid)
661{
662 int i;
663 u64 val = 0;
664 int nr_irqs = vcpu->kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS;
665
666 for (i = 0; i < 32; i++) {
667 struct vgic_irq *irq;
668
669 if ((intid + i) < VGIC_NR_SGIS || (intid + i) >= nr_irqs)
670 continue;
671
672 irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
673 if (irq->config == VGIC_CONFIG_LEVEL && irq->line_level)
674 val |= (1U << i);
675
676 vgic_put_irq(vcpu->kvm, irq);
677 }
678
679 return val;
680}
681
682void vgic_write_irq_line_level_info(struct kvm_vcpu *vcpu, u32 intid,
683 const u64 val)
684{
685 int i;
686 int nr_irqs = vcpu->kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS;
Christoffer Dall006df0f2016-10-16 22:19:11 +0200687 unsigned long flags;
Vijaya Kumar Ke96a0062017-01-26 19:50:52 +0530688
689 for (i = 0; i < 32; i++) {
690 struct vgic_irq *irq;
691 bool new_level;
692
693 if ((intid + i) < VGIC_NR_SGIS || (intid + i) >= nr_irqs)
694 continue;
695
696 irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
697
698 /*
699 * Line level is set irrespective of irq type
700 * (level or edge) to avoid dependency that VM should
701 * restore irq config before line level.
702 */
703 new_level = !!(val & (1U << i));
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000704 raw_spin_lock_irqsave(&irq->irq_lock, flags);
Vijaya Kumar Ke96a0062017-01-26 19:50:52 +0530705 irq->line_level = new_level;
706 if (new_level)
Christoffer Dall006df0f2016-10-16 22:19:11 +0200707 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
Vijaya Kumar Ke96a0062017-01-26 19:50:52 +0530708 else
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000709 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
Vijaya Kumar Ke96a0062017-01-26 19:50:52 +0530710
711 vgic_put_irq(vcpu->kvm, irq);
712 }
713}
714
Marc Zyngier4493b1c2016-04-26 11:06:12 +0100715static int match_region(const void *key, const void *elt)
716{
717 const unsigned int offset = (unsigned long)key;
718 const struct vgic_register_region *region = elt;
719
720 if (offset < region->reg_offset)
721 return -1;
722
723 if (offset >= region->reg_offset + region->len)
724 return 1;
725
726 return 0;
727}
728
Eric Auger4b7171a2016-12-20 09:20:00 +0100729const struct vgic_register_region *
730vgic_find_mmio_region(const struct vgic_register_region *regions,
731 int nr_regions, unsigned int offset)
Marc Zyngier4493b1c2016-04-26 11:06:12 +0100732{
Eric Auger4b7171a2016-12-20 09:20:00 +0100733 return bsearch((void *)(uintptr_t)offset, regions, nr_regions,
734 sizeof(regions[0]), match_region);
Marc Zyngier4493b1c2016-04-26 11:06:12 +0100735}
736
Vijaya Kumar K5fb247d2017-01-26 19:50:50 +0530737void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
738{
739 if (kvm_vgic_global_state.type == VGIC_V2)
740 vgic_v2_set_vmcr(vcpu, vmcr);
741 else
742 vgic_v3_set_vmcr(vcpu, vmcr);
743}
744
745void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
746{
747 if (kvm_vgic_global_state.type == VGIC_V2)
748 vgic_v2_get_vmcr(vcpu, vmcr);
749 else
750 vgic_v3_get_vmcr(vcpu, vmcr);
751}
752
Marc Zyngier4493b1c2016-04-26 11:06:12 +0100753/*
754 * kvm_mmio_read_buf() returns a value in a format where it can be converted
755 * to a byte array and be directly observed as the guest wanted it to appear
756 * in memory if it had done the store itself, which is LE for the GIC, as the
757 * guest knows the GIC is always LE.
758 *
759 * We convert this value to the CPUs native format to deal with it as a data
760 * value.
761 */
762unsigned long vgic_data_mmio_bus_to_host(const void *val, unsigned int len)
763{
764 unsigned long data = kvm_mmio_read_buf(val, len);
765
766 switch (len) {
767 case 1:
768 return data;
769 case 2:
770 return le16_to_cpu(data);
771 case 4:
772 return le32_to_cpu(data);
773 default:
774 return le64_to_cpu(data);
775 }
776}
777
778/*
779 * kvm_mmio_write_buf() expects a value in a format such that if converted to
780 * a byte array it is observed as the guest would see it if it could perform
781 * the load directly. Since the GIC is LE, and the guest knows this, the
782 * guest expects a value in little endian format.
783 *
784 * We convert the data value from the CPUs native format to LE so that the
785 * value is returned in the proper format.
786 */
787void vgic_data_host_to_mmio_bus(void *buf, unsigned int len,
788 unsigned long data)
789{
790 switch (len) {
791 case 1:
792 break;
793 case 2:
794 data = cpu_to_le16(data);
795 break;
796 case 4:
797 data = cpu_to_le32(data);
798 break;
799 default:
800 data = cpu_to_le64(data);
801 }
802
803 kvm_mmio_write_buf(buf, len, data);
804}
805
806static
807struct vgic_io_device *kvm_to_vgic_iodev(const struct kvm_io_device *dev)
808{
809 return container_of(dev, struct vgic_io_device, dev);
810}
811
Andre Przywara112b0b82016-11-01 18:00:08 +0000812static bool check_region(const struct kvm *kvm,
813 const struct vgic_register_region *region,
Marc Zyngier4493b1c2016-04-26 11:06:12 +0100814 gpa_t addr, int len)
815{
Andre Przywara112b0b82016-11-01 18:00:08 +0000816 int flags, nr_irqs = kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS;
817
818 switch (len) {
819 case sizeof(u8):
820 flags = VGIC_ACCESS_8bit;
821 break;
822 case sizeof(u32):
823 flags = VGIC_ACCESS_32bit;
824 break;
825 case sizeof(u64):
826 flags = VGIC_ACCESS_64bit;
827 break;
828 default:
829 return false;
830 }
831
832 if ((region->access_flags & flags) && IS_ALIGNED(addr, len)) {
833 if (!region->bits_per_irq)
834 return true;
835
836 /* Do we access a non-allocated IRQ? */
837 return VGIC_ADDR_TO_INTID(addr, region->bits_per_irq) < nr_irqs;
838 }
Marc Zyngier4493b1c2016-04-26 11:06:12 +0100839
840 return false;
841}
842
Vijaya Kumar K94574c92017-01-26 19:50:47 +0530843const struct vgic_register_region *
Vijaya Kumar K2df903a2017-01-26 19:50:46 +0530844vgic_get_mmio_region(struct kvm_vcpu *vcpu, struct vgic_io_device *iodev,
845 gpa_t addr, int len)
846{
847 const struct vgic_register_region *region;
848
849 region = vgic_find_mmio_region(iodev->regions, iodev->nr_regions,
850 addr - iodev->base_addr);
851 if (!region || !check_region(vcpu->kvm, region, addr, len))
852 return NULL;
853
854 return region;
855}
856
857static int vgic_uaccess_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
858 gpa_t addr, u32 *val)
859{
860 struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev);
861 const struct vgic_register_region *region;
862 struct kvm_vcpu *r_vcpu;
863
864 region = vgic_get_mmio_region(vcpu, iodev, addr, sizeof(u32));
865 if (!region) {
866 *val = 0;
867 return 0;
868 }
869
870 r_vcpu = iodev->redist_vcpu ? iodev->redist_vcpu : vcpu;
871 if (region->uaccess_read)
872 *val = region->uaccess_read(r_vcpu, addr, sizeof(u32));
873 else
874 *val = region->read(r_vcpu, addr, sizeof(u32));
875
876 return 0;
877}
878
879static int vgic_uaccess_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
880 gpa_t addr, const u32 *val)
881{
882 struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev);
883 const struct vgic_register_region *region;
884 struct kvm_vcpu *r_vcpu;
885
886 region = vgic_get_mmio_region(vcpu, iodev, addr, sizeof(u32));
887 if (!region)
888 return 0;
889
890 r_vcpu = iodev->redist_vcpu ? iodev->redist_vcpu : vcpu;
891 if (region->uaccess_write)
Christoffer Dallc6e09172018-07-16 15:06:23 +0200892 return region->uaccess_write(r_vcpu, addr, sizeof(u32), *val);
Vijaya Kumar K2df903a2017-01-26 19:50:46 +0530893
Christoffer Dallc6e09172018-07-16 15:06:23 +0200894 region->write(r_vcpu, addr, sizeof(u32), *val);
Vijaya Kumar K2df903a2017-01-26 19:50:46 +0530895 return 0;
896}
897
898/*
899 * Userland access to VGIC registers.
900 */
901int vgic_uaccess(struct kvm_vcpu *vcpu, struct vgic_io_device *dev,
902 bool is_write, int offset, u32 *val)
903{
904 if (is_write)
905 return vgic_uaccess_write(vcpu, &dev->dev, offset, val);
906 else
907 return vgic_uaccess_read(vcpu, &dev->dev, offset, val);
908}
909
Marc Zyngier4493b1c2016-04-26 11:06:12 +0100910static int dispatch_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
911 gpa_t addr, int len, void *val)
912{
913 struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev);
914 const struct vgic_register_region *region;
Andre Przywara59c5ab42016-07-15 12:43:30 +0100915 unsigned long data = 0;
Marc Zyngier4493b1c2016-04-26 11:06:12 +0100916
Vijaya Kumar K2df903a2017-01-26 19:50:46 +0530917 region = vgic_get_mmio_region(vcpu, iodev, addr, len);
918 if (!region) {
Marc Zyngier4493b1c2016-04-26 11:06:12 +0100919 memset(val, 0, len);
920 return 0;
921 }
922
Andre Przywara59c5ab42016-07-15 12:43:30 +0100923 switch (iodev->iodev_type) {
924 case IODEV_CPUIF:
Eric Auger9d5fcb92016-07-18 10:57:36 +0000925 data = region->read(vcpu, addr, len);
926 break;
Andre Przywara59c5ab42016-07-15 12:43:30 +0100927 case IODEV_DIST:
928 data = region->read(vcpu, addr, len);
929 break;
930 case IODEV_REDIST:
931 data = region->read(iodev->redist_vcpu, addr, len);
932 break;
933 case IODEV_ITS:
934 data = region->its_read(vcpu->kvm, iodev->its, addr, len);
935 break;
936 }
937
Marc Zyngier4493b1c2016-04-26 11:06:12 +0100938 vgic_data_host_to_mmio_bus(val, len, data);
939 return 0;
940}
941
942static int dispatch_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
943 gpa_t addr, int len, const void *val)
944{
945 struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev);
946 const struct vgic_register_region *region;
Marc Zyngier4493b1c2016-04-26 11:06:12 +0100947 unsigned long data = vgic_data_mmio_bus_to_host(val, len);
948
Vijaya Kumar K2df903a2017-01-26 19:50:46 +0530949 region = vgic_get_mmio_region(vcpu, iodev, addr, len);
950 if (!region)
Marc Zyngier4493b1c2016-04-26 11:06:12 +0100951 return 0;
952
Andre Przywara59c5ab42016-07-15 12:43:30 +0100953 switch (iodev->iodev_type) {
954 case IODEV_CPUIF:
Eric Auger9d5fcb92016-07-18 10:57:36 +0000955 region->write(vcpu, addr, len, data);
Andre Przywara59c5ab42016-07-15 12:43:30 +0100956 break;
957 case IODEV_DIST:
958 region->write(vcpu, addr, len, data);
959 break;
960 case IODEV_REDIST:
961 region->write(iodev->redist_vcpu, addr, len, data);
962 break;
963 case IODEV_ITS:
964 region->its_write(vcpu->kvm, iodev->its, addr, len, data);
965 break;
966 }
967
Marc Zyngier4493b1c2016-04-26 11:06:12 +0100968 return 0;
969}
970
971struct kvm_io_device_ops kvm_io_gic_ops = {
972 .read = dispatch_mmio_read,
973 .write = dispatch_mmio_write,
974};
Andre Przywarafb848db2016-04-26 21:32:49 +0100975
976int vgic_register_dist_iodev(struct kvm *kvm, gpa_t dist_base_address,
977 enum vgic_type type)
978{
979 struct vgic_io_device *io_device = &kvm->arch.vgic.dist_iodev;
980 int ret = 0;
981 unsigned int len;
982
983 switch (type) {
984 case VGIC_V2:
985 len = vgic_v2_init_dist_iodev(io_device);
986 break;
Andre Przywaraed9b8ce2015-12-01 14:34:34 +0000987 case VGIC_V3:
988 len = vgic_v3_init_dist_iodev(io_device);
989 break;
Andre Przywarafb848db2016-04-26 21:32:49 +0100990 default:
991 BUG_ON(1);
992 }
993
994 io_device->base_addr = dist_base_address;
Andre Przywara59c5ab42016-07-15 12:43:30 +0100995 io_device->iodev_type = IODEV_DIST;
Andre Przywarafb848db2016-04-26 21:32:49 +0100996 io_device->redist_vcpu = NULL;
997
998 mutex_lock(&kvm->slots_lock);
999 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, dist_base_address,
1000 len, &io_device->dev);
1001 mutex_unlock(&kvm->slots_lock);
1002
1003 return ret;
1004}