GNU Linux-libre 4.14.290-gnu1
[releases.git] / virt / kvm / arm / vgic / vgic-mmio.c
1 /*
2  * VGIC MMIO handling functions
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <linux/bitops.h>
15 #include <linux/bsearch.h>
16 #include <linux/kvm.h>
17 #include <linux/kvm_host.h>
18 #include <kvm/iodev.h>
19 #include <kvm/arm_vgic.h>
20
21 #include "vgic.h"
22 #include "vgic-mmio.h"
23
24 unsigned long vgic_mmio_read_raz(struct kvm_vcpu *vcpu,
25                                  gpa_t addr, unsigned int len)
26 {
27         return 0;
28 }
29
30 unsigned long vgic_mmio_read_rao(struct kvm_vcpu *vcpu,
31                                  gpa_t addr, unsigned int len)
32 {
33         return -1UL;
34 }
35
36 void vgic_mmio_write_wi(struct kvm_vcpu *vcpu, gpa_t addr,
37                         unsigned int len, unsigned long val)
38 {
39         /* Ignore */
40 }
41
42 /*
43  * Read accesses to both GICD_ICENABLER and GICD_ISENABLER return the value
44  * of the enabled bit, so there is only one function for both here.
45  */
46 unsigned long vgic_mmio_read_enable(struct kvm_vcpu *vcpu,
47                                     gpa_t addr, unsigned int len)
48 {
49         u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
50         u32 value = 0;
51         int i;
52
53         /* Loop over all IRQs affected by this read */
54         for (i = 0; i < len * 8; i++) {
55                 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
56
57                 if (irq->enabled)
58                         value |= (1U << i);
59
60                 vgic_put_irq(vcpu->kvm, irq);
61         }
62
63         return value;
64 }
65
66 void vgic_mmio_write_senable(struct kvm_vcpu *vcpu,
67                              gpa_t addr, unsigned int len,
68                              unsigned long val)
69 {
70         u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
71         int i;
72
73         for_each_set_bit(i, &val, len * 8) {
74                 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
75
76                 spin_lock(&irq->irq_lock);
77                 irq->enabled = true;
78                 vgic_queue_irq_unlock(vcpu->kvm, irq);
79
80                 vgic_put_irq(vcpu->kvm, irq);
81         }
82 }
83
84 void vgic_mmio_write_cenable(struct kvm_vcpu *vcpu,
85                              gpa_t addr, unsigned int len,
86                              unsigned long val)
87 {
88         u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
89         int i;
90
91         for_each_set_bit(i, &val, len * 8) {
92                 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
93
94                 spin_lock(&irq->irq_lock);
95
96                 irq->enabled = false;
97
98                 spin_unlock(&irq->irq_lock);
99                 vgic_put_irq(vcpu->kvm, irq);
100         }
101 }
102
103 unsigned long vgic_mmio_read_pending(struct kvm_vcpu *vcpu,
104                                      gpa_t addr, unsigned int len)
105 {
106         u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
107         u32 value = 0;
108         int i;
109
110         /* Loop over all IRQs affected by this read */
111         for (i = 0; i < len * 8; i++) {
112                 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
113                 unsigned long flags;
114
115                 spin_lock_irqsave(&irq->irq_lock, flags);
116                 if (irq_is_pending(irq))
117                         value |= (1U << i);
118                 spin_unlock_irqrestore(&irq->irq_lock, flags);
119
120                 vgic_put_irq(vcpu->kvm, irq);
121         }
122
123         return value;
124 }
125
126 static bool is_vgic_v2_sgi(struct kvm_vcpu *vcpu, struct vgic_irq *irq)
127 {
128         return (vgic_irq_is_sgi(irq->intid) &&
129                 vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V2);
130 }
131
132 void vgic_mmio_write_spending(struct kvm_vcpu *vcpu,
133                               gpa_t addr, unsigned int len,
134                               unsigned long val)
135 {
136         u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
137         int i;
138
139         for_each_set_bit(i, &val, len * 8) {
140                 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
141
142                 /* GICD_ISPENDR0 SGI bits are WI */
143                 if (is_vgic_v2_sgi(vcpu, irq)) {
144                         vgic_put_irq(vcpu->kvm, irq);
145                         continue;
146                 }
147
148                 spin_lock(&irq->irq_lock);
149                 irq->pending_latch = true;
150
151                 vgic_queue_irq_unlock(vcpu->kvm, irq);
152                 vgic_put_irq(vcpu->kvm, irq);
153         }
154 }
155
156 void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu,
157                               gpa_t addr, unsigned int len,
158                               unsigned long val)
159 {
160         u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
161         int i;
162
163         for_each_set_bit(i, &val, len * 8) {
164                 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
165
166                 /* GICD_ICPENDR0 SGI bits are WI */
167                 if (is_vgic_v2_sgi(vcpu, irq)) {
168                         vgic_put_irq(vcpu->kvm, irq);
169                         continue;
170                 }
171
172                 spin_lock(&irq->irq_lock);
173
174                 irq->pending_latch = false;
175
176                 spin_unlock(&irq->irq_lock);
177                 vgic_put_irq(vcpu->kvm, irq);
178         }
179 }
180
181 unsigned long vgic_mmio_read_active(struct kvm_vcpu *vcpu,
182                                     gpa_t addr, unsigned int len)
183 {
184         u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
185         u32 value = 0;
186         int i;
187
188         /* Loop over all IRQs affected by this read */
189         for (i = 0; i < len * 8; i++) {
190                 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
191
192                 if (irq->active)
193                         value |= (1U << i);
194
195                 vgic_put_irq(vcpu->kvm, irq);
196         }
197
198         return value;
199 }
200
201 static void vgic_mmio_change_active(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
202                                     bool new_active_state)
203 {
204         struct kvm_vcpu *requester_vcpu;
205         spin_lock(&irq->irq_lock);
206
207         /*
208          * The vcpu parameter here can mean multiple things depending on how
209          * this function is called; when handling a trap from the kernel it
210          * depends on the GIC version, and these functions are also called as
211          * part of save/restore from userspace.
212          *
213          * Therefore, we have to figure out the requester in a reliable way.
214          *
215          * When accessing VGIC state from user space, the requester_vcpu is
216          * NULL, which is fine, because we guarantee that no VCPUs are running
217          * when accessing VGIC state from user space so irq->vcpu->cpu is
218          * always -1.
219          */
220         requester_vcpu = kvm_arm_get_running_vcpu();
221
222         /*
223          * If this virtual IRQ was written into a list register, we
224          * have to make sure the CPU that runs the VCPU thread has
225          * synced back the LR state to the struct vgic_irq.
226          *
227          * As long as the conditions below are true, we know the VCPU thread
228          * may be on its way back from the guest (we kicked the VCPU thread in
229          * vgic_change_active_prepare)  and still has to sync back this IRQ,
230          * so we release and re-acquire the spin_lock to let the other thread
231          * sync back the IRQ.
232          */
233         while (irq->vcpu && /* IRQ may have state in an LR somewhere */
234                irq->vcpu != requester_vcpu && /* Current thread is not the VCPU thread */
235                irq->vcpu->cpu != -1) /* VCPU thread is running */
236                 cond_resched_lock(&irq->irq_lock);
237
238         irq->active = new_active_state;
239         if (new_active_state)
240                 vgic_queue_irq_unlock(vcpu->kvm, irq);
241         else
242                 spin_unlock(&irq->irq_lock);
243 }
244
245 /*
246  * If we are fiddling with an IRQ's active state, we have to make sure the IRQ
247  * is not queued on some running VCPU's LRs, because then the change to the
248  * active state can be overwritten when the VCPU's state is synced coming back
249  * from the guest.
250  *
251  * For shared interrupts, we have to stop all the VCPUs because interrupts can
252  * be migrated while we don't hold the IRQ locks and we don't want to be
253  * chasing moving targets.
254  *
255  * For private interrupts we don't have to do anything because userspace
256  * accesses to the VGIC state already require all VCPUs to be stopped, and
257  * only the VCPU itself can modify its private interrupts active state, which
258  * guarantees that the VCPU is not running.
259  */
260 static void vgic_change_active_prepare(struct kvm_vcpu *vcpu, u32 intid)
261 {
262         if (vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3 ||
263             intid >= VGIC_NR_PRIVATE_IRQS)
264                 kvm_arm_halt_guest(vcpu->kvm);
265 }
266
267 /* See vgic_change_active_prepare */
268 static void vgic_change_active_finish(struct kvm_vcpu *vcpu, u32 intid)
269 {
270         if (vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3 ||
271             intid >= VGIC_NR_PRIVATE_IRQS)
272                 kvm_arm_resume_guest(vcpu->kvm);
273 }
274
275 static void __vgic_mmio_write_cactive(struct kvm_vcpu *vcpu,
276                                       gpa_t addr, unsigned int len,
277                                       unsigned long val)
278 {
279         u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
280         int i;
281
282         for_each_set_bit(i, &val, len * 8) {
283                 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
284                 vgic_mmio_change_active(vcpu, irq, false);
285                 vgic_put_irq(vcpu->kvm, irq);
286         }
287 }
288
289 void vgic_mmio_write_cactive(struct kvm_vcpu *vcpu,
290                              gpa_t addr, unsigned int len,
291                              unsigned long val)
292 {
293         u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
294
295         mutex_lock(&vcpu->kvm->lock);
296         vgic_change_active_prepare(vcpu, intid);
297
298         __vgic_mmio_write_cactive(vcpu, addr, len, val);
299
300         vgic_change_active_finish(vcpu, intid);
301         mutex_unlock(&vcpu->kvm->lock);
302 }
303
304 void vgic_mmio_uaccess_write_cactive(struct kvm_vcpu *vcpu,
305                                      gpa_t addr, unsigned int len,
306                                      unsigned long val)
307 {
308         __vgic_mmio_write_cactive(vcpu, addr, len, val);
309 }
310
311 static void __vgic_mmio_write_sactive(struct kvm_vcpu *vcpu,
312                                       gpa_t addr, unsigned int len,
313                                       unsigned long val)
314 {
315         u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
316         int i;
317
318         for_each_set_bit(i, &val, len * 8) {
319                 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
320                 vgic_mmio_change_active(vcpu, irq, true);
321                 vgic_put_irq(vcpu->kvm, irq);
322         }
323 }
324
325 void vgic_mmio_write_sactive(struct kvm_vcpu *vcpu,
326                              gpa_t addr, unsigned int len,
327                              unsigned long val)
328 {
329         u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
330
331         mutex_lock(&vcpu->kvm->lock);
332         vgic_change_active_prepare(vcpu, intid);
333
334         __vgic_mmio_write_sactive(vcpu, addr, len, val);
335
336         vgic_change_active_finish(vcpu, intid);
337         mutex_unlock(&vcpu->kvm->lock);
338 }
339
340 void vgic_mmio_uaccess_write_sactive(struct kvm_vcpu *vcpu,
341                                      gpa_t addr, unsigned int len,
342                                      unsigned long val)
343 {
344         __vgic_mmio_write_sactive(vcpu, addr, len, val);
345 }
346
347 unsigned long vgic_mmio_read_priority(struct kvm_vcpu *vcpu,
348                                       gpa_t addr, unsigned int len)
349 {
350         u32 intid = VGIC_ADDR_TO_INTID(addr, 8);
351         int i;
352         u64 val = 0;
353
354         for (i = 0; i < len; i++) {
355                 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
356
357                 val |= (u64)irq->priority << (i * 8);
358
359                 vgic_put_irq(vcpu->kvm, irq);
360         }
361
362         return val;
363 }
364
365 /*
366  * We currently don't handle changing the priority of an interrupt that
367  * is already pending on a VCPU. If there is a need for this, we would
368  * need to make this VCPU exit and re-evaluate the priorities, potentially
369  * leading to this interrupt getting presented now to the guest (if it has
370  * been masked by the priority mask before).
371  */
372 void vgic_mmio_write_priority(struct kvm_vcpu *vcpu,
373                               gpa_t addr, unsigned int len,
374                               unsigned long val)
375 {
376         u32 intid = VGIC_ADDR_TO_INTID(addr, 8);
377         int i;
378
379         for (i = 0; i < len; i++) {
380                 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
381
382                 spin_lock(&irq->irq_lock);
383                 /* Narrow the priority range to what we actually support */
384                 irq->priority = (val >> (i * 8)) & GENMASK(7, 8 - VGIC_PRI_BITS);
385                 spin_unlock(&irq->irq_lock);
386
387                 vgic_put_irq(vcpu->kvm, irq);
388         }
389 }
390
391 unsigned long vgic_mmio_read_config(struct kvm_vcpu *vcpu,
392                                     gpa_t addr, unsigned int len)
393 {
394         u32 intid = VGIC_ADDR_TO_INTID(addr, 2);
395         u32 value = 0;
396         int i;
397
398         for (i = 0; i < len * 4; i++) {
399                 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
400
401                 if (irq->config == VGIC_CONFIG_EDGE)
402                         value |= (2U << (i * 2));
403
404                 vgic_put_irq(vcpu->kvm, irq);
405         }
406
407         return value;
408 }
409
410 void vgic_mmio_write_config(struct kvm_vcpu *vcpu,
411                             gpa_t addr, unsigned int len,
412                             unsigned long val)
413 {
414         u32 intid = VGIC_ADDR_TO_INTID(addr, 2);
415         int i;
416
417         for (i = 0; i < len * 4; i++) {
418                 struct vgic_irq *irq;
419
420                 /*
421                  * The configuration cannot be changed for SGIs in general,
422                  * for PPIs this is IMPLEMENTATION DEFINED. The arch timer
423                  * code relies on PPIs being level triggered, so we also
424                  * make them read-only here.
425                  */
426                 if (intid + i < VGIC_NR_PRIVATE_IRQS)
427                         continue;
428
429                 irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
430                 spin_lock(&irq->irq_lock);
431
432                 if (test_bit(i * 2 + 1, &val))
433                         irq->config = VGIC_CONFIG_EDGE;
434                 else
435                         irq->config = VGIC_CONFIG_LEVEL;
436
437                 spin_unlock(&irq->irq_lock);
438                 vgic_put_irq(vcpu->kvm, irq);
439         }
440 }
441
442 u64 vgic_read_irq_line_level_info(struct kvm_vcpu *vcpu, u32 intid)
443 {
444         int i;
445         u64 val = 0;
446         int nr_irqs = vcpu->kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS;
447
448         for (i = 0; i < 32; i++) {
449                 struct vgic_irq *irq;
450
451                 if ((intid + i) < VGIC_NR_SGIS || (intid + i) >= nr_irqs)
452                         continue;
453
454                 irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
455                 if (irq->config == VGIC_CONFIG_LEVEL && irq->line_level)
456                         val |= (1U << i);
457
458                 vgic_put_irq(vcpu->kvm, irq);
459         }
460
461         return val;
462 }
463
464 void vgic_write_irq_line_level_info(struct kvm_vcpu *vcpu, u32 intid,
465                                     const u64 val)
466 {
467         int i;
468         int nr_irqs = vcpu->kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS;
469
470         for (i = 0; i < 32; i++) {
471                 struct vgic_irq *irq;
472                 bool new_level;
473
474                 if ((intid + i) < VGIC_NR_SGIS || (intid + i) >= nr_irqs)
475                         continue;
476
477                 irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
478
479                 /*
480                  * Line level is set irrespective of irq type
481                  * (level or edge) to avoid dependency that VM should
482                  * restore irq config before line level.
483                  */
484                 new_level = !!(val & (1U << i));
485                 spin_lock(&irq->irq_lock);
486                 irq->line_level = new_level;
487                 if (new_level)
488                         vgic_queue_irq_unlock(vcpu->kvm, irq);
489                 else
490                         spin_unlock(&irq->irq_lock);
491
492                 vgic_put_irq(vcpu->kvm, irq);
493         }
494 }
495
496 static int match_region(const void *key, const void *elt)
497 {
498         const unsigned int offset = (unsigned long)key;
499         const struct vgic_register_region *region = elt;
500
501         if (offset < region->reg_offset)
502                 return -1;
503
504         if (offset >= region->reg_offset + region->len)
505                 return 1;
506
507         return 0;
508 }
509
510 const struct vgic_register_region *
511 vgic_find_mmio_region(const struct vgic_register_region *regions,
512                       int nr_regions, unsigned int offset)
513 {
514         return bsearch((void *)(uintptr_t)offset, regions, nr_regions,
515                        sizeof(regions[0]), match_region);
516 }
517
518 void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
519 {
520         if (kvm_vgic_global_state.type == VGIC_V2)
521                 vgic_v2_set_vmcr(vcpu, vmcr);
522         else
523                 vgic_v3_set_vmcr(vcpu, vmcr);
524 }
525
526 void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
527 {
528         if (kvm_vgic_global_state.type == VGIC_V2)
529                 vgic_v2_get_vmcr(vcpu, vmcr);
530         else
531                 vgic_v3_get_vmcr(vcpu, vmcr);
532 }
533
534 /*
535  * kvm_mmio_read_buf() returns a value in a format where it can be converted
536  * to a byte array and be directly observed as the guest wanted it to appear
537  * in memory if it had done the store itself, which is LE for the GIC, as the
538  * guest knows the GIC is always LE.
539  *
540  * We convert this value to the CPUs native format to deal with it as a data
541  * value.
542  */
543 unsigned long vgic_data_mmio_bus_to_host(const void *val, unsigned int len)
544 {
545         unsigned long data = kvm_mmio_read_buf(val, len);
546
547         switch (len) {
548         case 1:
549                 return data;
550         case 2:
551                 return le16_to_cpu(data);
552         case 4:
553                 return le32_to_cpu(data);
554         default:
555                 return le64_to_cpu(data);
556         }
557 }
558
559 /*
560  * kvm_mmio_write_buf() expects a value in a format such that if converted to
561  * a byte array it is observed as the guest would see it if it could perform
562  * the load directly.  Since the GIC is LE, and the guest knows this, the
563  * guest expects a value in little endian format.
564  *
565  * We convert the data value from the CPUs native format to LE so that the
566  * value is returned in the proper format.
567  */
568 void vgic_data_host_to_mmio_bus(void *buf, unsigned int len,
569                                 unsigned long data)
570 {
571         switch (len) {
572         case 1:
573                 break;
574         case 2:
575                 data = cpu_to_le16(data);
576                 break;
577         case 4:
578                 data = cpu_to_le32(data);
579                 break;
580         default:
581                 data = cpu_to_le64(data);
582         }
583
584         kvm_mmio_write_buf(buf, len, data);
585 }
586
587 static
588 struct vgic_io_device *kvm_to_vgic_iodev(const struct kvm_io_device *dev)
589 {
590         return container_of(dev, struct vgic_io_device, dev);
591 }
592
593 static bool check_region(const struct kvm *kvm,
594                          const struct vgic_register_region *region,
595                          gpa_t addr, int len)
596 {
597         int flags, nr_irqs = kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS;
598
599         switch (len) {
600         case sizeof(u8):
601                 flags = VGIC_ACCESS_8bit;
602                 break;
603         case sizeof(u32):
604                 flags = VGIC_ACCESS_32bit;
605                 break;
606         case sizeof(u64):
607                 flags = VGIC_ACCESS_64bit;
608                 break;
609         default:
610                 return false;
611         }
612
613         if ((region->access_flags & flags) && IS_ALIGNED(addr, len)) {
614                 if (!region->bits_per_irq)
615                         return true;
616
617                 /* Do we access a non-allocated IRQ? */
618                 return VGIC_ADDR_TO_INTID(addr, region->bits_per_irq) < nr_irqs;
619         }
620
621         return false;
622 }
623
624 const struct vgic_register_region *
625 vgic_get_mmio_region(struct kvm_vcpu *vcpu, struct vgic_io_device *iodev,
626                      gpa_t addr, int len)
627 {
628         const struct vgic_register_region *region;
629
630         region = vgic_find_mmio_region(iodev->regions, iodev->nr_regions,
631                                        addr - iodev->base_addr);
632         if (!region || !check_region(vcpu->kvm, region, addr, len))
633                 return NULL;
634
635         return region;
636 }
637
638 static int vgic_uaccess_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
639                              gpa_t addr, u32 *val)
640 {
641         struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev);
642         const struct vgic_register_region *region;
643         struct kvm_vcpu *r_vcpu;
644
645         region = vgic_get_mmio_region(vcpu, iodev, addr, sizeof(u32));
646         if (!region) {
647                 *val = 0;
648                 return 0;
649         }
650
651         r_vcpu = iodev->redist_vcpu ? iodev->redist_vcpu : vcpu;
652         if (region->uaccess_read)
653                 *val = region->uaccess_read(r_vcpu, addr, sizeof(u32));
654         else
655                 *val = region->read(r_vcpu, addr, sizeof(u32));
656
657         return 0;
658 }
659
660 static int vgic_uaccess_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
661                               gpa_t addr, const u32 *val)
662 {
663         struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev);
664         const struct vgic_register_region *region;
665         struct kvm_vcpu *r_vcpu;
666
667         region = vgic_get_mmio_region(vcpu, iodev, addr, sizeof(u32));
668         if (!region)
669                 return 0;
670
671         r_vcpu = iodev->redist_vcpu ? iodev->redist_vcpu : vcpu;
672         if (region->uaccess_write)
673                 region->uaccess_write(r_vcpu, addr, sizeof(u32), *val);
674         else
675                 region->write(r_vcpu, addr, sizeof(u32), *val);
676
677         return 0;
678 }
679
680 /*
681  * Userland access to VGIC registers.
682  */
683 int vgic_uaccess(struct kvm_vcpu *vcpu, struct vgic_io_device *dev,
684                  bool is_write, int offset, u32 *val)
685 {
686         if (is_write)
687                 return vgic_uaccess_write(vcpu, &dev->dev, offset, val);
688         else
689                 return vgic_uaccess_read(vcpu, &dev->dev, offset, val);
690 }
691
692 static int dispatch_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
693                               gpa_t addr, int len, void *val)
694 {
695         struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev);
696         const struct vgic_register_region *region;
697         unsigned long data = 0;
698
699         region = vgic_get_mmio_region(vcpu, iodev, addr, len);
700         if (!region) {
701                 memset(val, 0, len);
702                 return 0;
703         }
704
705         switch (iodev->iodev_type) {
706         case IODEV_CPUIF:
707                 data = region->read(vcpu, addr, len);
708                 break;
709         case IODEV_DIST:
710                 data = region->read(vcpu, addr, len);
711                 break;
712         case IODEV_REDIST:
713                 data = region->read(iodev->redist_vcpu, addr, len);
714                 break;
715         case IODEV_ITS:
716                 data = region->its_read(vcpu->kvm, iodev->its, addr, len);
717                 break;
718         }
719
720         vgic_data_host_to_mmio_bus(val, len, data);
721         return 0;
722 }
723
724 static int dispatch_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
725                                gpa_t addr, int len, const void *val)
726 {
727         struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev);
728         const struct vgic_register_region *region;
729         unsigned long data = vgic_data_mmio_bus_to_host(val, len);
730
731         region = vgic_get_mmio_region(vcpu, iodev, addr, len);
732         if (!region)
733                 return 0;
734
735         switch (iodev->iodev_type) {
736         case IODEV_CPUIF:
737                 region->write(vcpu, addr, len, data);
738                 break;
739         case IODEV_DIST:
740                 region->write(vcpu, addr, len, data);
741                 break;
742         case IODEV_REDIST:
743                 region->write(iodev->redist_vcpu, addr, len, data);
744                 break;
745         case IODEV_ITS:
746                 region->its_write(vcpu->kvm, iodev->its, addr, len, data);
747                 break;
748         }
749
750         return 0;
751 }
752
753 struct kvm_io_device_ops kvm_io_gic_ops = {
754         .read = dispatch_mmio_read,
755         .write = dispatch_mmio_write,
756 };
757
758 int vgic_register_dist_iodev(struct kvm *kvm, gpa_t dist_base_address,
759                              enum vgic_type type)
760 {
761         struct vgic_io_device *io_device = &kvm->arch.vgic.dist_iodev;
762         int ret = 0;
763         unsigned int len;
764
765         switch (type) {
766         case VGIC_V2:
767                 len = vgic_v2_init_dist_iodev(io_device);
768                 break;
769         case VGIC_V3:
770                 len = vgic_v3_init_dist_iodev(io_device);
771                 break;
772         default:
773                 BUG_ON(1);
774         }
775
776         io_device->base_addr = dist_base_address;
777         io_device->iodev_type = IODEV_DIST;
778         io_device->redist_vcpu = NULL;
779
780         mutex_lock(&kvm->slots_lock);
781         ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, dist_base_address,
782                                       len, &io_device->dev);
783         mutex_unlock(&kvm->slots_lock);
784
785         return ret;
786 }