]> asedeno.scripts.mit.edu Git - linux.git/blob - virt/kvm/arm/vgic/vgic-its.c
d06330abd5e86d192485929f7618193cd4369a07
[linux.git] / virt / kvm / arm / vgic / vgic-its.c
1 /*
2  * GICv3 ITS emulation
3  *
4  * Copyright (C) 2015,2016 ARM Ltd.
5  * Author: Andre Przywara <andre.przywara@arm.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <linux/cpu.h>
21 #include <linux/kvm.h>
22 #include <linux/kvm_host.h>
23 #include <linux/interrupt.h>
24 #include <linux/list.h>
25 #include <linux/uaccess.h>
26
27 #include <linux/irqchip/arm-gic-v3.h>
28
29 #include <asm/kvm_emulate.h>
30 #include <asm/kvm_arm.h>
31 #include <asm/kvm_mmu.h>
32
33 #include "vgic.h"
34 #include "vgic-mmio.h"
35
36 /*
37  * Creates a new (reference to a) struct vgic_irq for a given LPI.
38  * If this LPI is already mapped on another ITS, we increase its refcount
39  * and return a pointer to the existing structure.
40  * If this is a "new" LPI, we allocate and initialize a new struct vgic_irq.
41  * This function returns a pointer to the _unlocked_ structure.
42  */
43 static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid)
44 {
45         struct vgic_dist *dist = &kvm->arch.vgic;
46         struct vgic_irq *irq = vgic_get_irq(kvm, NULL, intid), *oldirq;
47
48         /* In this case there is no put, since we keep the reference. */
49         if (irq)
50                 return irq;
51
52         irq = kzalloc(sizeof(struct vgic_irq), GFP_KERNEL);
53         if (!irq)
54                 return ERR_PTR(-ENOMEM);
55
56         INIT_LIST_HEAD(&irq->lpi_list);
57         INIT_LIST_HEAD(&irq->ap_list);
58         spin_lock_init(&irq->irq_lock);
59
60         irq->config = VGIC_CONFIG_EDGE;
61         kref_init(&irq->refcount);
62         irq->intid = intid;
63
64         spin_lock(&dist->lpi_list_lock);
65
66         /*
67          * There could be a race with another vgic_add_lpi(), so we need to
68          * check that we don't add a second list entry with the same LPI.
69          */
70         list_for_each_entry(oldirq, &dist->lpi_list_head, lpi_list) {
71                 if (oldirq->intid != intid)
72                         continue;
73
74                 /* Someone was faster with adding this LPI, lets use that. */
75                 kfree(irq);
76                 irq = oldirq;
77
78                 /*
79                  * This increases the refcount, the caller is expected to
80                  * call vgic_put_irq() on the returned pointer once it's
81                  * finished with the IRQ.
82                  */
83                 vgic_get_irq_kref(irq);
84
85                 goto out_unlock;
86         }
87
88         list_add_tail(&irq->lpi_list, &dist->lpi_list_head);
89         dist->lpi_list_count++;
90
91 out_unlock:
92         spin_unlock(&dist->lpi_list_lock);
93
94         return irq;
95 }
96
97 struct its_device {
98         struct list_head dev_list;
99
100         /* the head for the list of ITTEs */
101         struct list_head itt_head;
102         u32 device_id;
103 };
104
105 #define COLLECTION_NOT_MAPPED ((u32)~0)
106
107 struct its_collection {
108         struct list_head coll_list;
109
110         u32 collection_id;
111         u32 target_addr;
112 };
113
114 #define its_is_collection_mapped(coll) ((coll) && \
115                                 ((coll)->target_addr != COLLECTION_NOT_MAPPED))
116
117 struct its_itte {
118         struct list_head itte_list;
119
120         struct vgic_irq *irq;
121         struct its_collection *collection;
122         u32 lpi;
123         u32 event_id;
124 };
125
126 /*
127  * Find and returns a device in the device table for an ITS.
128  * Must be called with the its_lock mutex held.
129  */
130 static struct its_device *find_its_device(struct vgic_its *its, u32 device_id)
131 {
132         struct its_device *device;
133
134         list_for_each_entry(device, &its->device_list, dev_list)
135                 if (device_id == device->device_id)
136                         return device;
137
138         return NULL;
139 }
140
141 /*
142  * Find and returns an interrupt translation table entry (ITTE) for a given
143  * Device ID/Event ID pair on an ITS.
144  * Must be called with the its_lock mutex held.
145  */
146 static struct its_itte *find_itte(struct vgic_its *its, u32 device_id,
147                                   u32 event_id)
148 {
149         struct its_device *device;
150         struct its_itte *itte;
151
152         device = find_its_device(its, device_id);
153         if (device == NULL)
154                 return NULL;
155
156         list_for_each_entry(itte, &device->itt_head, itte_list)
157                 if (itte->event_id == event_id)
158                         return itte;
159
160         return NULL;
161 }
162
163 /* To be used as an iterator this macro misses the enclosing parentheses */
164 #define for_each_lpi_its(dev, itte, its) \
165         list_for_each_entry(dev, &(its)->device_list, dev_list) \
166                 list_for_each_entry(itte, &(dev)->itt_head, itte_list)
167
168 /*
169  * We only implement 48 bits of PA at the moment, although the ITS
170  * supports more. Let's be restrictive here.
171  */
172 #define BASER_ADDRESS(x)        ((x) & GENMASK_ULL(47, 16))
173 #define CBASER_ADDRESS(x)       ((x) & GENMASK_ULL(47, 12))
174 #define PENDBASER_ADDRESS(x)    ((x) & GENMASK_ULL(47, 16))
175 #define PROPBASER_ADDRESS(x)    ((x) & GENMASK_ULL(47, 12))
176
177 #define GIC_LPI_OFFSET 8192
178
179 /*
180  * Finds and returns a collection in the ITS collection table.
181  * Must be called with the its_lock mutex held.
182  */
183 static struct its_collection *find_collection(struct vgic_its *its, int coll_id)
184 {
185         struct its_collection *collection;
186
187         list_for_each_entry(collection, &its->collection_list, coll_list) {
188                 if (coll_id == collection->collection_id)
189                         return collection;
190         }
191
192         return NULL;
193 }
194
195 #define LPI_PROP_ENABLE_BIT(p)  ((p) & LPI_PROP_ENABLED)
196 #define LPI_PROP_PRIORITY(p)    ((p) & 0xfc)
197
198 /*
199  * Reads the configuration data for a given LPI from guest memory and
200  * updates the fields in struct vgic_irq.
201  * If filter_vcpu is not NULL, applies only if the IRQ is targeting this
202  * VCPU. Unconditionally applies if filter_vcpu is NULL.
203  */
204 static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq,
205                              struct kvm_vcpu *filter_vcpu)
206 {
207         u64 propbase = PROPBASER_ADDRESS(kvm->arch.vgic.propbaser);
208         u8 prop;
209         int ret;
210
211         ret = kvm_read_guest(kvm, propbase + irq->intid - GIC_LPI_OFFSET,
212                              &prop, 1);
213
214         if (ret)
215                 return ret;
216
217         spin_lock(&irq->irq_lock);
218
219         if (!filter_vcpu || filter_vcpu == irq->target_vcpu) {
220                 irq->priority = LPI_PROP_PRIORITY(prop);
221                 irq->enabled = LPI_PROP_ENABLE_BIT(prop);
222
223                 vgic_queue_irq_unlock(kvm, irq);
224         } else {
225                 spin_unlock(&irq->irq_lock);
226         }
227
228         return 0;
229 }
230
231 /*
232  * Create a snapshot of the current LPI list, so that we can enumerate all
233  * LPIs without holding any lock.
234  * Returns the array length and puts the kmalloc'ed array into intid_ptr.
235  */
236 static int vgic_copy_lpi_list(struct kvm *kvm, u32 **intid_ptr)
237 {
238         struct vgic_dist *dist = &kvm->arch.vgic;
239         struct vgic_irq *irq;
240         u32 *intids;
241         int irq_count = dist->lpi_list_count, i = 0;
242
243         /*
244          * We use the current value of the list length, which may change
245          * after the kmalloc. We don't care, because the guest shouldn't
246          * change anything while the command handling is still running,
247          * and in the worst case we would miss a new IRQ, which one wouldn't
248          * expect to be covered by this command anyway.
249          */
250         intids = kmalloc_array(irq_count, sizeof(intids[0]), GFP_KERNEL);
251         if (!intids)
252                 return -ENOMEM;
253
254         spin_lock(&dist->lpi_list_lock);
255         list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
256                 /* We don't need to "get" the IRQ, as we hold the list lock. */
257                 intids[i] = irq->intid;
258                 if (++i == irq_count)
259                         break;
260         }
261         spin_unlock(&dist->lpi_list_lock);
262
263         *intid_ptr = intids;
264         return irq_count;
265 }
266
267 /*
268  * Promotes the ITS view of affinity of an ITTE (which redistributor this LPI
269  * is targeting) to the VGIC's view, which deals with target VCPUs.
270  * Needs to be called whenever either the collection for a LPIs has
271  * changed or the collection itself got retargeted.
272  */
273 static void update_affinity_itte(struct kvm *kvm, struct its_itte *itte)
274 {
275         struct kvm_vcpu *vcpu;
276
277         if (!its_is_collection_mapped(itte->collection))
278                 return;
279
280         vcpu = kvm_get_vcpu(kvm, itte->collection->target_addr);
281
282         spin_lock(&itte->irq->irq_lock);
283         itte->irq->target_vcpu = vcpu;
284         spin_unlock(&itte->irq->irq_lock);
285 }
286
287 /*
288  * Updates the target VCPU for every LPI targeting this collection.
289  * Must be called with the its_lock mutex held.
290  */
291 static void update_affinity_collection(struct kvm *kvm, struct vgic_its *its,
292                                        struct its_collection *coll)
293 {
294         struct its_device *device;
295         struct its_itte *itte;
296
297         for_each_lpi_its(device, itte, its) {
298                 if (!itte->collection || coll != itte->collection)
299                         continue;
300
301                 update_affinity_itte(kvm, itte);
302         }
303 }
304
305 static u32 max_lpis_propbaser(u64 propbaser)
306 {
307         int nr_idbits = (propbaser & 0x1f) + 1;
308
309         return 1U << min(nr_idbits, INTERRUPT_ID_BITS_ITS);
310 }
311
312 /*
313  * Scan the whole LPI pending table and sync the pending bit in there
314  * with our own data structures. This relies on the LPI being
315  * mapped before.
316  */
317 static int its_sync_lpi_pending_table(struct kvm_vcpu *vcpu)
318 {
319         gpa_t pendbase = PENDBASER_ADDRESS(vcpu->arch.vgic_cpu.pendbaser);
320         struct vgic_irq *irq;
321         int last_byte_offset = -1;
322         int ret = 0;
323         u32 *intids;
324         int nr_irqs, i;
325
326         nr_irqs = vgic_copy_lpi_list(vcpu->kvm, &intids);
327         if (nr_irqs < 0)
328                 return nr_irqs;
329
330         for (i = 0; i < nr_irqs; i++) {
331                 int byte_offset, bit_nr;
332                 u8 pendmask;
333
334                 byte_offset = intids[i] / BITS_PER_BYTE;
335                 bit_nr = intids[i] % BITS_PER_BYTE;
336
337                 /*
338                  * For contiguously allocated LPIs chances are we just read
339                  * this very same byte in the last iteration. Reuse that.
340                  */
341                 if (byte_offset != last_byte_offset) {
342                         ret = kvm_read_guest(vcpu->kvm, pendbase + byte_offset,
343                                              &pendmask, 1);
344                         if (ret) {
345                                 kfree(intids);
346                                 return ret;
347                         }
348                         last_byte_offset = byte_offset;
349                 }
350
351                 irq = vgic_get_irq(vcpu->kvm, NULL, intids[i]);
352                 spin_lock(&irq->irq_lock);
353                 irq->pending = pendmask & (1U << bit_nr);
354                 vgic_queue_irq_unlock(vcpu->kvm, irq);
355                 vgic_put_irq(vcpu->kvm, irq);
356         }
357
358         kfree(intids);
359
360         return ret;
361 }
362
363 static unsigned long vgic_mmio_read_its_ctlr(struct kvm *vcpu,
364                                              struct vgic_its *its,
365                                              gpa_t addr, unsigned int len)
366 {
367         u32 reg = 0;
368
369         mutex_lock(&its->cmd_lock);
370         if (its->creadr == its->cwriter)
371                 reg |= GITS_CTLR_QUIESCENT;
372         if (its->enabled)
373                 reg |= GITS_CTLR_ENABLE;
374         mutex_unlock(&its->cmd_lock);
375
376         return reg;
377 }
378
379 static void vgic_mmio_write_its_ctlr(struct kvm *kvm, struct vgic_its *its,
380                                      gpa_t addr, unsigned int len,
381                                      unsigned long val)
382 {
383         its->enabled = !!(val & GITS_CTLR_ENABLE);
384 }
385
386 static unsigned long vgic_mmio_read_its_typer(struct kvm *kvm,
387                                               struct vgic_its *its,
388                                               gpa_t addr, unsigned int len)
389 {
390         u64 reg = GITS_TYPER_PLPIS;
391
392         /*
393          * We use linear CPU numbers for redistributor addressing,
394          * so GITS_TYPER.PTA is 0.
395          * Also we force all PROPBASER registers to be the same, so
396          * CommonLPIAff is 0 as well.
397          * To avoid memory waste in the guest, we keep the number of IDBits and
398          * DevBits low - as least for the time being.
399          */
400         reg |= 0x0f << GITS_TYPER_DEVBITS_SHIFT;
401         reg |= 0x0f << GITS_TYPER_IDBITS_SHIFT;
402
403         return extract_bytes(reg, addr & 7, len);
404 }
405
406 static unsigned long vgic_mmio_read_its_iidr(struct kvm *kvm,
407                                              struct vgic_its *its,
408                                              gpa_t addr, unsigned int len)
409 {
410         return (PRODUCT_ID_KVM << 24) | (IMPLEMENTER_ARM << 0);
411 }
412
413 static unsigned long vgic_mmio_read_its_idregs(struct kvm *kvm,
414                                                struct vgic_its *its,
415                                                gpa_t addr, unsigned int len)
416 {
417         switch (addr & 0xffff) {
418         case GITS_PIDR0:
419                 return 0x92;    /* part number, bits[7:0] */
420         case GITS_PIDR1:
421                 return 0xb4;    /* part number, bits[11:8] */
422         case GITS_PIDR2:
423                 return GIC_PIDR2_ARCH_GICv3 | 0x0b;
424         case GITS_PIDR4:
425                 return 0x40;    /* This is a 64K software visible page */
426         /* The following are the ID registers for (any) GIC. */
427         case GITS_CIDR0:
428                 return 0x0d;
429         case GITS_CIDR1:
430                 return 0xf0;
431         case GITS_CIDR2:
432                 return 0x05;
433         case GITS_CIDR3:
434                 return 0xb1;
435         }
436
437         return 0;
438 }
439
440 /*
441  * Find the target VCPU and the LPI number for a given devid/eventid pair
442  * and make this IRQ pending, possibly injecting it.
443  * Must be called with the its_lock mutex held.
444  * Returns 0 on success, a positive error value for any ITS mapping
445  * related errors and negative error values for generic errors.
446  */
447 static int vgic_its_trigger_msi(struct kvm *kvm, struct vgic_its *its,
448                                 u32 devid, u32 eventid)
449 {
450         struct kvm_vcpu *vcpu;
451         struct its_itte *itte;
452
453         if (!its->enabled)
454                 return -EBUSY;
455
456         itte = find_itte(its, devid, eventid);
457         if (!itte || !its_is_collection_mapped(itte->collection))
458                 return E_ITS_INT_UNMAPPED_INTERRUPT;
459
460         vcpu = kvm_get_vcpu(kvm, itte->collection->target_addr);
461         if (!vcpu)
462                 return E_ITS_INT_UNMAPPED_INTERRUPT;
463
464         if (!vcpu->arch.vgic_cpu.lpis_enabled)
465                 return -EBUSY;
466
467         spin_lock(&itte->irq->irq_lock);
468         itte->irq->pending = true;
469         vgic_queue_irq_unlock(kvm, itte->irq);
470
471         return 0;
472 }
473
474 /*
475  * Queries the KVM IO bus framework to get the ITS pointer from the given
476  * doorbell address.
477  * We then call vgic_its_trigger_msi() with the decoded data.
478  * According to the KVM_SIGNAL_MSI API description returns 1 on success.
479  */
480 int vgic_its_inject_msi(struct kvm *kvm, struct kvm_msi *msi)
481 {
482         u64 address;
483         struct kvm_io_device *kvm_io_dev;
484         struct vgic_io_device *iodev;
485         int ret;
486
487         if (!vgic_has_its(kvm))
488                 return -ENODEV;
489
490         if (!(msi->flags & KVM_MSI_VALID_DEVID))
491                 return -EINVAL;
492
493         address = (u64)msi->address_hi << 32 | msi->address_lo;
494
495         kvm_io_dev = kvm_io_bus_get_dev(kvm, KVM_MMIO_BUS, address);
496         if (!kvm_io_dev)
497                 return -ENODEV;
498
499         iodev = container_of(kvm_io_dev, struct vgic_io_device, dev);
500
501         mutex_lock(&iodev->its->its_lock);
502         ret = vgic_its_trigger_msi(kvm, iodev->its, msi->devid, msi->data);
503         mutex_unlock(&iodev->its->its_lock);
504
505         if (ret < 0)
506                 return ret;
507
508         /*
509          * KVM_SIGNAL_MSI demands a return value > 0 for success and 0
510          * if the guest has blocked the MSI. So we map any LPI mapping
511          * related error to that.
512          */
513         if (ret)
514                 return 0;
515         else
516                 return 1;
517 }
518
519 /* Requires the its_lock to be held. */
520 static void its_free_itte(struct kvm *kvm, struct its_itte *itte)
521 {
522         list_del(&itte->itte_list);
523
524         /* This put matches the get in vgic_add_lpi. */
525         if (itte->irq)
526                 vgic_put_irq(kvm, itte->irq);
527
528         kfree(itte);
529 }
530
531 static u64 its_cmd_mask_field(u64 *its_cmd, int word, int shift, int size)
532 {
533         return (le64_to_cpu(its_cmd[word]) >> shift) & (BIT_ULL(size) - 1);
534 }
535
536 #define its_cmd_get_command(cmd)        its_cmd_mask_field(cmd, 0,  0,  8)
537 #define its_cmd_get_deviceid(cmd)       its_cmd_mask_field(cmd, 0, 32, 32)
538 #define its_cmd_get_id(cmd)             its_cmd_mask_field(cmd, 1,  0, 32)
539 #define its_cmd_get_physical_id(cmd)    its_cmd_mask_field(cmd, 1, 32, 32)
540 #define its_cmd_get_collection(cmd)     its_cmd_mask_field(cmd, 2,  0, 16)
541 #define its_cmd_get_target_addr(cmd)    its_cmd_mask_field(cmd, 2, 16, 32)
542 #define its_cmd_get_validbit(cmd)       its_cmd_mask_field(cmd, 2, 63,  1)
543
544 /*
545  * The DISCARD command frees an Interrupt Translation Table Entry (ITTE).
546  * Must be called with the its_lock mutex held.
547  */
548 static int vgic_its_cmd_handle_discard(struct kvm *kvm, struct vgic_its *its,
549                                        u64 *its_cmd)
550 {
551         u32 device_id = its_cmd_get_deviceid(its_cmd);
552         u32 event_id = its_cmd_get_id(its_cmd);
553         struct its_itte *itte;
554
555
556         itte = find_itte(its, device_id, event_id);
557         if (itte && itte->collection) {
558                 /*
559                  * Though the spec talks about removing the pending state, we
560                  * don't bother here since we clear the ITTE anyway and the
561                  * pending state is a property of the ITTE struct.
562                  */
563                 its_free_itte(kvm, itte);
564                 return 0;
565         }
566
567         return E_ITS_DISCARD_UNMAPPED_INTERRUPT;
568 }
569
570 /*
571  * The MOVI command moves an ITTE to a different collection.
572  * Must be called with the its_lock mutex held.
573  */
574 static int vgic_its_cmd_handle_movi(struct kvm *kvm, struct vgic_its *its,
575                                     u64 *its_cmd)
576 {
577         u32 device_id = its_cmd_get_deviceid(its_cmd);
578         u32 event_id = its_cmd_get_id(its_cmd);
579         u32 coll_id = its_cmd_get_collection(its_cmd);
580         struct kvm_vcpu *vcpu;
581         struct its_itte *itte;
582         struct its_collection *collection;
583
584         itte = find_itte(its, device_id, event_id);
585         if (!itte)
586                 return E_ITS_MOVI_UNMAPPED_INTERRUPT;
587
588         if (!its_is_collection_mapped(itte->collection))
589                 return E_ITS_MOVI_UNMAPPED_COLLECTION;
590
591         collection = find_collection(its, coll_id);
592         if (!its_is_collection_mapped(collection))
593                 return E_ITS_MOVI_UNMAPPED_COLLECTION;
594
595         itte->collection = collection;
596         vcpu = kvm_get_vcpu(kvm, collection->target_addr);
597
598         spin_lock(&itte->irq->irq_lock);
599         itte->irq->target_vcpu = vcpu;
600         spin_unlock(&itte->irq->irq_lock);
601
602         return 0;
603 }
604
605 /*
606  * Check whether an ID can be stored into the corresponding guest table.
607  * For a direct table this is pretty easy, but gets a bit nasty for
608  * indirect tables. We check whether the resulting guest physical address
609  * is actually valid (covered by a memslot and guest accessbible).
610  * For this we have to read the respective first level entry.
611  */
612 static bool vgic_its_check_id(struct vgic_its *its, u64 baser, int id)
613 {
614         int l1_tbl_size = GITS_BASER_NR_PAGES(baser) * SZ_64K;
615         int index;
616         u64 indirect_ptr;
617         gfn_t gfn;
618
619         if (!(baser & GITS_BASER_INDIRECT)) {
620                 phys_addr_t addr;
621
622                 if (id >= (l1_tbl_size / GITS_BASER_ENTRY_SIZE(baser)))
623                         return false;
624
625                 addr = BASER_ADDRESS(baser) + id * GITS_BASER_ENTRY_SIZE(baser);
626                 gfn = addr >> PAGE_SHIFT;
627
628                 return kvm_is_visible_gfn(its->dev->kvm, gfn);
629         }
630
631         /* calculate and check the index into the 1st level */
632         index = id / (SZ_64K / GITS_BASER_ENTRY_SIZE(baser));
633         if (index >= (l1_tbl_size / sizeof(u64)))
634                 return false;
635
636         /* Each 1st level entry is represented by a 64-bit value. */
637         if (kvm_read_guest(its->dev->kvm,
638                            BASER_ADDRESS(baser) + index * sizeof(indirect_ptr),
639                            &indirect_ptr, sizeof(indirect_ptr)))
640                 return false;
641
642         indirect_ptr = le64_to_cpu(indirect_ptr);
643
644         /* check the valid bit of the first level entry */
645         if (!(indirect_ptr & BIT_ULL(63)))
646                 return false;
647
648         /*
649          * Mask the guest physical address and calculate the frame number.
650          * Any address beyond our supported 48 bits of PA will be caught
651          * by the actual check in the final step.
652          */
653         indirect_ptr &= GENMASK_ULL(51, 16);
654
655         /* Find the address of the actual entry */
656         index = id % (SZ_64K / GITS_BASER_ENTRY_SIZE(baser));
657         indirect_ptr += index * GITS_BASER_ENTRY_SIZE(baser);
658         gfn = indirect_ptr >> PAGE_SHIFT;
659
660         return kvm_is_visible_gfn(its->dev->kvm, gfn);
661 }
662
663 static int vgic_its_alloc_collection(struct vgic_its *its,
664                                      struct its_collection **colp,
665                                      u32 coll_id)
666 {
667         struct its_collection *collection;
668
669         if (!vgic_its_check_id(its, its->baser_coll_table, coll_id))
670                 return E_ITS_MAPC_COLLECTION_OOR;
671
672         collection = kzalloc(sizeof(*collection), GFP_KERNEL);
673
674         collection->collection_id = coll_id;
675         collection->target_addr = COLLECTION_NOT_MAPPED;
676
677         list_add_tail(&collection->coll_list, &its->collection_list);
678         *colp = collection;
679
680         return 0;
681 }
682
683 static void vgic_its_free_collection(struct vgic_its *its, u32 coll_id)
684 {
685         struct its_collection *collection;
686         struct its_device *device;
687         struct its_itte *itte;
688
689         /*
690          * Clearing the mapping for that collection ID removes the
691          * entry from the list. If there wasn't any before, we can
692          * go home early.
693          */
694         collection = find_collection(its, coll_id);
695         if (!collection)
696                 return;
697
698         for_each_lpi_its(device, itte, its)
699                 if (itte->collection &&
700                     itte->collection->collection_id == coll_id)
701                         itte->collection = NULL;
702
703         list_del(&collection->coll_list);
704         kfree(collection);
705 }
706
707 /*
708  * The MAPTI and MAPI commands map LPIs to ITTEs.
709  * Must be called with its_lock mutex held.
710  */
711 static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its,
712                                     u64 *its_cmd)
713 {
714         u32 device_id = its_cmd_get_deviceid(its_cmd);
715         u32 event_id = its_cmd_get_id(its_cmd);
716         u32 coll_id = its_cmd_get_collection(its_cmd);
717         struct its_itte *itte, *new_itte = NULL;
718         struct its_device *device;
719         struct its_collection *collection, *new_coll = NULL;
720         int lpi_nr;
721         struct vgic_irq *irq;
722
723         device = find_its_device(its, device_id);
724         if (!device)
725                 return E_ITS_MAPTI_UNMAPPED_DEVICE;
726
727         if (its_cmd_get_command(its_cmd) == GITS_CMD_MAPTI)
728                 lpi_nr = its_cmd_get_physical_id(its_cmd);
729         else
730                 lpi_nr = event_id;
731         if (lpi_nr < GIC_LPI_OFFSET ||
732             lpi_nr >= max_lpis_propbaser(kvm->arch.vgic.propbaser))
733                 return E_ITS_MAPTI_PHYSICALID_OOR;
734
735         collection = find_collection(its, coll_id);
736         if (!collection) {
737                 int ret = vgic_its_alloc_collection(its, &collection, coll_id);
738                 if (ret)
739                         return ret;
740                 new_coll = collection;
741         }
742
743         itte = find_itte(its, device_id, event_id);
744         if (!itte) {
745                 itte = kzalloc(sizeof(struct its_itte), GFP_KERNEL);
746                 if (!itte) {
747                         if (new_coll)
748                                 vgic_its_free_collection(its, coll_id);
749                         return -ENOMEM;
750                 }
751
752                 new_itte = itte;
753                 itte->event_id  = event_id;
754                 list_add_tail(&itte->itte_list, &device->itt_head);
755         }
756
757         itte->collection = collection;
758         itte->lpi = lpi_nr;
759
760         irq = vgic_add_lpi(kvm, lpi_nr);
761         if (IS_ERR(irq)) {
762                 if (new_coll)
763                         vgic_its_free_collection(its, coll_id);
764                 if (new_itte)
765                         its_free_itte(kvm, new_itte);
766                 return PTR_ERR(irq);
767         }
768         itte->irq = irq;
769
770         update_affinity_itte(kvm, itte);
771
772         /*
773          * We "cache" the configuration table entries in out struct vgic_irq's.
774          * However we only have those structs for mapped IRQs, so we read in
775          * the respective config data from memory here upon mapping the LPI.
776          */
777         update_lpi_config(kvm, itte->irq, NULL);
778
779         return 0;
780 }
781
782 /* Requires the its_lock to be held. */
783 static void vgic_its_unmap_device(struct kvm *kvm, struct its_device *device)
784 {
785         struct its_itte *itte, *temp;
786
787         /*
788          * The spec says that unmapping a device with still valid
789          * ITTEs associated is UNPREDICTABLE. We remove all ITTEs,
790          * since we cannot leave the memory unreferenced.
791          */
792         list_for_each_entry_safe(itte, temp, &device->itt_head, itte_list)
793                 its_free_itte(kvm, itte);
794
795         list_del(&device->dev_list);
796         kfree(device);
797 }
798
799 /*
800  * MAPD maps or unmaps a device ID to Interrupt Translation Tables (ITTs).
801  * Must be called with the its_lock mutex held.
802  */
803 static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its,
804                                     u64 *its_cmd)
805 {
806         u32 device_id = its_cmd_get_deviceid(its_cmd);
807         bool valid = its_cmd_get_validbit(its_cmd);
808         struct its_device *device;
809
810         if (!vgic_its_check_id(its, its->baser_device_table, device_id))
811                 return E_ITS_MAPD_DEVICE_OOR;
812
813         device = find_its_device(its, device_id);
814
815         /*
816          * The spec says that calling MAPD on an already mapped device
817          * invalidates all cached data for this device. We implement this
818          * by removing the mapping and re-establishing it.
819          */
820         if (device)
821                 vgic_its_unmap_device(kvm, device);
822
823         /*
824          * The spec does not say whether unmapping a not-mapped device
825          * is an error, so we are done in any case.
826          */
827         if (!valid)
828                 return 0;
829
830         device = kzalloc(sizeof(struct its_device), GFP_KERNEL);
831         if (!device)
832                 return -ENOMEM;
833
834         device->device_id = device_id;
835         INIT_LIST_HEAD(&device->itt_head);
836
837         list_add_tail(&device->dev_list, &its->device_list);
838
839         return 0;
840 }
841
842 /*
843  * The MAPC command maps collection IDs to redistributors.
844  * Must be called with the its_lock mutex held.
845  */
846 static int vgic_its_cmd_handle_mapc(struct kvm *kvm, struct vgic_its *its,
847                                     u64 *its_cmd)
848 {
849         u16 coll_id;
850         u32 target_addr;
851         struct its_collection *collection;
852         bool valid;
853
854         valid = its_cmd_get_validbit(its_cmd);
855         coll_id = its_cmd_get_collection(its_cmd);
856         target_addr = its_cmd_get_target_addr(its_cmd);
857
858         if (target_addr >= atomic_read(&kvm->online_vcpus))
859                 return E_ITS_MAPC_PROCNUM_OOR;
860
861         if (!valid) {
862                 vgic_its_free_collection(its, coll_id);
863         } else {
864                 collection = find_collection(its, coll_id);
865
866                 if (!collection) {
867                         int ret;
868
869                         ret = vgic_its_alloc_collection(its, &collection,
870                                                         coll_id);
871                         if (ret)
872                                 return ret;
873                         collection->target_addr = target_addr;
874                 } else {
875                         collection->target_addr = target_addr;
876                         update_affinity_collection(kvm, its, collection);
877                 }
878         }
879
880         return 0;
881 }
882
883 /*
884  * The CLEAR command removes the pending state for a particular LPI.
885  * Must be called with the its_lock mutex held.
886  */
887 static int vgic_its_cmd_handle_clear(struct kvm *kvm, struct vgic_its *its,
888                                      u64 *its_cmd)
889 {
890         u32 device_id = its_cmd_get_deviceid(its_cmd);
891         u32 event_id = its_cmd_get_id(its_cmd);
892         struct its_itte *itte;
893
894
895         itte = find_itte(its, device_id, event_id);
896         if (!itte)
897                 return E_ITS_CLEAR_UNMAPPED_INTERRUPT;
898
899         itte->irq->pending = false;
900
901         return 0;
902 }
903
904 /*
905  * The INV command syncs the configuration bits from the memory table.
906  * Must be called with the its_lock mutex held.
907  */
908 static int vgic_its_cmd_handle_inv(struct kvm *kvm, struct vgic_its *its,
909                                    u64 *its_cmd)
910 {
911         u32 device_id = its_cmd_get_deviceid(its_cmd);
912         u32 event_id = its_cmd_get_id(its_cmd);
913         struct its_itte *itte;
914
915
916         itte = find_itte(its, device_id, event_id);
917         if (!itte)
918                 return E_ITS_INV_UNMAPPED_INTERRUPT;
919
920         return update_lpi_config(kvm, itte->irq, NULL);
921 }
922
923 /*
924  * The INVALL command requests flushing of all IRQ data in this collection.
925  * Find the VCPU mapped to that collection, then iterate over the VM's list
926  * of mapped LPIs and update the configuration for each IRQ which targets
927  * the specified vcpu. The configuration will be read from the in-memory
928  * configuration table.
929  * Must be called with the its_lock mutex held.
930  */
931 static int vgic_its_cmd_handle_invall(struct kvm *kvm, struct vgic_its *its,
932                                       u64 *its_cmd)
933 {
934         u32 coll_id = its_cmd_get_collection(its_cmd);
935         struct its_collection *collection;
936         struct kvm_vcpu *vcpu;
937         struct vgic_irq *irq;
938         u32 *intids;
939         int irq_count, i;
940
941         collection = find_collection(its, coll_id);
942         if (!its_is_collection_mapped(collection))
943                 return E_ITS_INVALL_UNMAPPED_COLLECTION;
944
945         vcpu = kvm_get_vcpu(kvm, collection->target_addr);
946
947         irq_count = vgic_copy_lpi_list(kvm, &intids);
948         if (irq_count < 0)
949                 return irq_count;
950
951         for (i = 0; i < irq_count; i++) {
952                 irq = vgic_get_irq(kvm, NULL, intids[i]);
953                 if (!irq)
954                         continue;
955                 update_lpi_config(kvm, irq, vcpu);
956                 vgic_put_irq(kvm, irq);
957         }
958
959         kfree(intids);
960
961         return 0;
962 }
963
964 /*
965  * The MOVALL command moves the pending state of all IRQs targeting one
966  * redistributor to another. We don't hold the pending state in the VCPUs,
967  * but in the IRQs instead, so there is really not much to do for us here.
968  * However the spec says that no IRQ must target the old redistributor
969  * afterwards, so we make sure that no LPI is using the associated target_vcpu.
970  * This command affects all LPIs in the system that target that redistributor.
971  */
972 static int vgic_its_cmd_handle_movall(struct kvm *kvm, struct vgic_its *its,
973                                       u64 *its_cmd)
974 {
975         struct vgic_dist *dist = &kvm->arch.vgic;
976         u32 target1_addr = its_cmd_get_target_addr(its_cmd);
977         u32 target2_addr = its_cmd_mask_field(its_cmd, 3, 16, 32);
978         struct kvm_vcpu *vcpu1, *vcpu2;
979         struct vgic_irq *irq;
980
981         if (target1_addr >= atomic_read(&kvm->online_vcpus) ||
982             target2_addr >= atomic_read(&kvm->online_vcpus))
983                 return E_ITS_MOVALL_PROCNUM_OOR;
984
985         if (target1_addr == target2_addr)
986                 return 0;
987
988         vcpu1 = kvm_get_vcpu(kvm, target1_addr);
989         vcpu2 = kvm_get_vcpu(kvm, target2_addr);
990
991         spin_lock(&dist->lpi_list_lock);
992
993         list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
994                 spin_lock(&irq->irq_lock);
995
996                 if (irq->target_vcpu == vcpu1)
997                         irq->target_vcpu = vcpu2;
998
999                 spin_unlock(&irq->irq_lock);
1000         }
1001
1002         spin_unlock(&dist->lpi_list_lock);
1003
1004         return 0;
1005 }
1006
1007 /*
1008  * The INT command injects the LPI associated with that DevID/EvID pair.
1009  * Must be called with the its_lock mutex held.
1010  */
1011 static int vgic_its_cmd_handle_int(struct kvm *kvm, struct vgic_its *its,
1012                                    u64 *its_cmd)
1013 {
1014         u32 msi_data = its_cmd_get_id(its_cmd);
1015         u64 msi_devid = its_cmd_get_deviceid(its_cmd);
1016
1017         return vgic_its_trigger_msi(kvm, its, msi_devid, msi_data);
1018 }
1019
1020 /*
1021  * This function is called with the its_cmd lock held, but the ITS data
1022  * structure lock dropped.
1023  */
1024 static int vgic_its_handle_command(struct kvm *kvm, struct vgic_its *its,
1025                                    u64 *its_cmd)
1026 {
1027         int ret = -ENODEV;
1028
1029         mutex_lock(&its->its_lock);
1030         switch (its_cmd_get_command(its_cmd)) {
1031         case GITS_CMD_MAPD:
1032                 ret = vgic_its_cmd_handle_mapd(kvm, its, its_cmd);
1033                 break;
1034         case GITS_CMD_MAPC:
1035                 ret = vgic_its_cmd_handle_mapc(kvm, its, its_cmd);
1036                 break;
1037         case GITS_CMD_MAPI:
1038                 ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd);
1039                 break;
1040         case GITS_CMD_MAPTI:
1041                 ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd);
1042                 break;
1043         case GITS_CMD_MOVI:
1044                 ret = vgic_its_cmd_handle_movi(kvm, its, its_cmd);
1045                 break;
1046         case GITS_CMD_DISCARD:
1047                 ret = vgic_its_cmd_handle_discard(kvm, its, its_cmd);
1048                 break;
1049         case GITS_CMD_CLEAR:
1050                 ret = vgic_its_cmd_handle_clear(kvm, its, its_cmd);
1051                 break;
1052         case GITS_CMD_MOVALL:
1053                 ret = vgic_its_cmd_handle_movall(kvm, its, its_cmd);
1054                 break;
1055         case GITS_CMD_INT:
1056                 ret = vgic_its_cmd_handle_int(kvm, its, its_cmd);
1057                 break;
1058         case GITS_CMD_INV:
1059                 ret = vgic_its_cmd_handle_inv(kvm, its, its_cmd);
1060                 break;
1061         case GITS_CMD_INVALL:
1062                 ret = vgic_its_cmd_handle_invall(kvm, its, its_cmd);
1063                 break;
1064         case GITS_CMD_SYNC:
1065                 /* we ignore this command: we are in sync all of the time */
1066                 ret = 0;
1067                 break;
1068         }
1069         mutex_unlock(&its->its_lock);
1070
1071         return ret;
1072 }
1073
1074 static u64 vgic_sanitise_its_baser(u64 reg)
1075 {
1076         reg = vgic_sanitise_field(reg, GITS_BASER_SHAREABILITY_MASK,
1077                                   GITS_BASER_SHAREABILITY_SHIFT,
1078                                   vgic_sanitise_shareability);
1079         reg = vgic_sanitise_field(reg, GITS_BASER_INNER_CACHEABILITY_MASK,
1080                                   GITS_BASER_INNER_CACHEABILITY_SHIFT,
1081                                   vgic_sanitise_inner_cacheability);
1082         reg = vgic_sanitise_field(reg, GITS_BASER_OUTER_CACHEABILITY_MASK,
1083                                   GITS_BASER_OUTER_CACHEABILITY_SHIFT,
1084                                   vgic_sanitise_outer_cacheability);
1085
1086         /* Bits 15:12 contain bits 51:48 of the PA, which we don't support. */
1087         reg &= ~GENMASK_ULL(15, 12);
1088
1089         /* We support only one (ITS) page size: 64K */
1090         reg = (reg & ~GITS_BASER_PAGE_SIZE_MASK) | GITS_BASER_PAGE_SIZE_64K;
1091
1092         return reg;
1093 }
1094
1095 static u64 vgic_sanitise_its_cbaser(u64 reg)
1096 {
1097         reg = vgic_sanitise_field(reg, GITS_CBASER_SHAREABILITY_MASK,
1098                                   GITS_CBASER_SHAREABILITY_SHIFT,
1099                                   vgic_sanitise_shareability);
1100         reg = vgic_sanitise_field(reg, GITS_CBASER_INNER_CACHEABILITY_MASK,
1101                                   GITS_CBASER_INNER_CACHEABILITY_SHIFT,
1102                                   vgic_sanitise_inner_cacheability);
1103         reg = vgic_sanitise_field(reg, GITS_CBASER_OUTER_CACHEABILITY_MASK,
1104                                   GITS_CBASER_OUTER_CACHEABILITY_SHIFT,
1105                                   vgic_sanitise_outer_cacheability);
1106
1107         /*
1108          * Sanitise the physical address to be 64k aligned.
1109          * Also limit the physical addresses to 48 bits.
1110          */
1111         reg &= ~(GENMASK_ULL(51, 48) | GENMASK_ULL(15, 12));
1112
1113         return reg;
1114 }
1115
1116 static unsigned long vgic_mmio_read_its_cbaser(struct kvm *kvm,
1117                                                struct vgic_its *its,
1118                                                gpa_t addr, unsigned int len)
1119 {
1120         return extract_bytes(its->cbaser, addr & 7, len);
1121 }
1122
1123 static void vgic_mmio_write_its_cbaser(struct kvm *kvm, struct vgic_its *its,
1124                                        gpa_t addr, unsigned int len,
1125                                        unsigned long val)
1126 {
1127         /* When GITS_CTLR.Enable is 1, this register is RO. */
1128         if (its->enabled)
1129                 return;
1130
1131         mutex_lock(&its->cmd_lock);
1132         its->cbaser = update_64bit_reg(its->cbaser, addr & 7, len, val);
1133         its->cbaser = vgic_sanitise_its_cbaser(its->cbaser);
1134         its->creadr = 0;
1135         /*
1136          * CWRITER is architecturally UNKNOWN on reset, but we need to reset
1137          * it to CREADR to make sure we start with an empty command buffer.
1138          */
1139         its->cwriter = its->creadr;
1140         mutex_unlock(&its->cmd_lock);
1141 }
1142
1143 #define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
1144 #define ITS_CMD_SIZE                    32
1145 #define ITS_CMD_OFFSET(reg)             ((reg) & GENMASK(19, 5))
1146
1147 /*
1148  * By writing to CWRITER the guest announces new commands to be processed.
1149  * To avoid any races in the first place, we take the its_cmd lock, which
1150  * protects our ring buffer variables, so that there is only one user
1151  * per ITS handling commands at a given time.
1152  */
1153 static void vgic_mmio_write_its_cwriter(struct kvm *kvm, struct vgic_its *its,
1154                                         gpa_t addr, unsigned int len,
1155                                         unsigned long val)
1156 {
1157         gpa_t cbaser;
1158         u64 cmd_buf[4];
1159         u32 reg;
1160
1161         if (!its)
1162                 return;
1163
1164         mutex_lock(&its->cmd_lock);
1165
1166         reg = update_64bit_reg(its->cwriter, addr & 7, len, val);
1167         reg = ITS_CMD_OFFSET(reg);
1168         if (reg >= ITS_CMD_BUFFER_SIZE(its->cbaser)) {
1169                 mutex_unlock(&its->cmd_lock);
1170                 return;
1171         }
1172
1173         its->cwriter = reg;
1174         cbaser = CBASER_ADDRESS(its->cbaser);
1175
1176         while (its->cwriter != its->creadr) {
1177                 int ret = kvm_read_guest(kvm, cbaser + its->creadr,
1178                                          cmd_buf, ITS_CMD_SIZE);
1179                 /*
1180                  * If kvm_read_guest() fails, this could be due to the guest
1181                  * programming a bogus value in CBASER or something else going
1182                  * wrong from which we cannot easily recover.
1183                  * According to section 6.3.2 in the GICv3 spec we can just
1184                  * ignore that command then.
1185                  */
1186                 if (!ret)
1187                         vgic_its_handle_command(kvm, its, cmd_buf);
1188
1189                 its->creadr += ITS_CMD_SIZE;
1190                 if (its->creadr == ITS_CMD_BUFFER_SIZE(its->cbaser))
1191                         its->creadr = 0;
1192         }
1193
1194         mutex_unlock(&its->cmd_lock);
1195 }
1196
1197 static unsigned long vgic_mmio_read_its_cwriter(struct kvm *kvm,
1198                                                 struct vgic_its *its,
1199                                                 gpa_t addr, unsigned int len)
1200 {
1201         return extract_bytes(its->cwriter, addr & 0x7, len);
1202 }
1203
1204 static unsigned long vgic_mmio_read_its_creadr(struct kvm *kvm,
1205                                                struct vgic_its *its,
1206                                                gpa_t addr, unsigned int len)
1207 {
1208         return extract_bytes(its->creadr, addr & 0x7, len);
1209 }
1210
1211 #define BASER_INDEX(addr) (((addr) / sizeof(u64)) & 0x7)
1212 static unsigned long vgic_mmio_read_its_baser(struct kvm *kvm,
1213                                               struct vgic_its *its,
1214                                               gpa_t addr, unsigned int len)
1215 {
1216         u64 reg;
1217
1218         switch (BASER_INDEX(addr)) {
1219         case 0:
1220                 reg = its->baser_device_table;
1221                 break;
1222         case 1:
1223                 reg = its->baser_coll_table;
1224                 break;
1225         default:
1226                 reg = 0;
1227                 break;
1228         }
1229
1230         return extract_bytes(reg, addr & 7, len);
1231 }
1232
1233 #define GITS_BASER_RO_MASK      (GENMASK_ULL(52, 48) | GENMASK_ULL(58, 56))
1234 static void vgic_mmio_write_its_baser(struct kvm *kvm,
1235                                       struct vgic_its *its,
1236                                       gpa_t addr, unsigned int len,
1237                                       unsigned long val)
1238 {
1239         u64 entry_size, device_type;
1240         u64 reg, *regptr, clearbits = 0;
1241
1242         /* When GITS_CTLR.Enable is 1, we ignore write accesses. */
1243         if (its->enabled)
1244                 return;
1245
1246         switch (BASER_INDEX(addr)) {
1247         case 0:
1248                 regptr = &its->baser_device_table;
1249                 entry_size = 8;
1250                 device_type = GITS_BASER_TYPE_DEVICE;
1251                 break;
1252         case 1:
1253                 regptr = &its->baser_coll_table;
1254                 entry_size = 8;
1255                 device_type = GITS_BASER_TYPE_COLLECTION;
1256                 clearbits = GITS_BASER_INDIRECT;
1257                 break;
1258         default:
1259                 return;
1260         }
1261
1262         reg = update_64bit_reg(*regptr, addr & 7, len, val);
1263         reg &= ~GITS_BASER_RO_MASK;
1264         reg &= ~clearbits;
1265
1266         reg |= (entry_size - 1) << GITS_BASER_ENTRY_SIZE_SHIFT;
1267         reg |= device_type << GITS_BASER_TYPE_SHIFT;
1268         reg = vgic_sanitise_its_baser(reg);
1269
1270         *regptr = reg;
1271 }
1272
1273 #define REGISTER_ITS_DESC(off, rd, wr, length, acc)             \
1274 {                                                               \
1275         .reg_offset = off,                                      \
1276         .len = length,                                          \
1277         .access_flags = acc,                                    \
1278         .its_read = rd,                                         \
1279         .its_write = wr,                                        \
1280 }
1281
1282 static void its_mmio_write_wi(struct kvm *kvm, struct vgic_its *its,
1283                               gpa_t addr, unsigned int len, unsigned long val)
1284 {
1285         /* Ignore */
1286 }
1287
1288 static struct vgic_register_region its_registers[] = {
1289         REGISTER_ITS_DESC(GITS_CTLR,
1290                 vgic_mmio_read_its_ctlr, vgic_mmio_write_its_ctlr, 4,
1291                 VGIC_ACCESS_32bit),
1292         REGISTER_ITS_DESC(GITS_IIDR,
1293                 vgic_mmio_read_its_iidr, its_mmio_write_wi, 4,
1294                 VGIC_ACCESS_32bit),
1295         REGISTER_ITS_DESC(GITS_TYPER,
1296                 vgic_mmio_read_its_typer, its_mmio_write_wi, 8,
1297                 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1298         REGISTER_ITS_DESC(GITS_CBASER,
1299                 vgic_mmio_read_its_cbaser, vgic_mmio_write_its_cbaser, 8,
1300                 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1301         REGISTER_ITS_DESC(GITS_CWRITER,
1302                 vgic_mmio_read_its_cwriter, vgic_mmio_write_its_cwriter, 8,
1303                 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1304         REGISTER_ITS_DESC(GITS_CREADR,
1305                 vgic_mmio_read_its_creadr, its_mmio_write_wi, 8,
1306                 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1307         REGISTER_ITS_DESC(GITS_BASER,
1308                 vgic_mmio_read_its_baser, vgic_mmio_write_its_baser, 0x40,
1309                 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1310         REGISTER_ITS_DESC(GITS_IDREGS_BASE,
1311                 vgic_mmio_read_its_idregs, its_mmio_write_wi, 0x30,
1312                 VGIC_ACCESS_32bit),
1313 };
1314
1315 /* This is called on setting the LPI enable bit in the redistributor. */
1316 void vgic_enable_lpis(struct kvm_vcpu *vcpu)
1317 {
1318         if (!(vcpu->arch.vgic_cpu.pendbaser & GICR_PENDBASER_PTZ))
1319                 its_sync_lpi_pending_table(vcpu);
1320 }
1321
1322 static int vgic_its_init_its(struct kvm *kvm, struct vgic_its *its)
1323 {
1324         struct vgic_io_device *iodev = &its->iodev;
1325         int ret;
1326
1327         if (its->initialized)
1328                 return 0;
1329
1330         if (IS_VGIC_ADDR_UNDEF(its->vgic_its_base))
1331                 return -ENXIO;
1332
1333         iodev->regions = its_registers;
1334         iodev->nr_regions = ARRAY_SIZE(its_registers);
1335         kvm_iodevice_init(&iodev->dev, &kvm_io_gic_ops);
1336
1337         iodev->base_addr = its->vgic_its_base;
1338         iodev->iodev_type = IODEV_ITS;
1339         iodev->its = its;
1340         mutex_lock(&kvm->slots_lock);
1341         ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, iodev->base_addr,
1342                                       KVM_VGIC_V3_ITS_SIZE, &iodev->dev);
1343         mutex_unlock(&kvm->slots_lock);
1344
1345         if (!ret)
1346                 its->initialized = true;
1347
1348         return ret;
1349 }
1350
1351 #define INITIAL_BASER_VALUE                                               \
1352         (GIC_BASER_CACHEABILITY(GITS_BASER, INNER, RaWb)                | \
1353          GIC_BASER_CACHEABILITY(GITS_BASER, OUTER, SameAsInner)         | \
1354          GIC_BASER_SHAREABILITY(GITS_BASER, InnerShareable)             | \
1355          ((8ULL - 1) << GITS_BASER_ENTRY_SIZE_SHIFT)                    | \
1356          GITS_BASER_PAGE_SIZE_64K)
1357
1358 #define INITIAL_PROPBASER_VALUE                                           \
1359         (GIC_BASER_CACHEABILITY(GICR_PROPBASER, INNER, RaWb)            | \
1360          GIC_BASER_CACHEABILITY(GICR_PROPBASER, OUTER, SameAsInner)     | \
1361          GIC_BASER_SHAREABILITY(GICR_PROPBASER, InnerShareable))
1362
1363 static int vgic_its_create(struct kvm_device *dev, u32 type)
1364 {
1365         struct vgic_its *its;
1366
1367         if (type != KVM_DEV_TYPE_ARM_VGIC_ITS)
1368                 return -ENODEV;
1369
1370         its = kzalloc(sizeof(struct vgic_its), GFP_KERNEL);
1371         if (!its)
1372                 return -ENOMEM;
1373
1374         mutex_init(&its->its_lock);
1375         mutex_init(&its->cmd_lock);
1376
1377         its->vgic_its_base = VGIC_ADDR_UNDEF;
1378
1379         INIT_LIST_HEAD(&its->device_list);
1380         INIT_LIST_HEAD(&its->collection_list);
1381
1382         dev->kvm->arch.vgic.has_its = true;
1383         its->initialized = false;
1384         its->enabled = false;
1385         its->dev = dev;
1386
1387         its->baser_device_table = INITIAL_BASER_VALUE                   |
1388                 ((u64)GITS_BASER_TYPE_DEVICE << GITS_BASER_TYPE_SHIFT);
1389         its->baser_coll_table = INITIAL_BASER_VALUE |
1390                 ((u64)GITS_BASER_TYPE_COLLECTION << GITS_BASER_TYPE_SHIFT);
1391         dev->kvm->arch.vgic.propbaser = INITIAL_PROPBASER_VALUE;
1392
1393         dev->private = its;
1394
1395         return 0;
1396 }
1397
1398 static void vgic_its_destroy(struct kvm_device *kvm_dev)
1399 {
1400         struct kvm *kvm = kvm_dev->kvm;
1401         struct vgic_its *its = kvm_dev->private;
1402         struct its_device *dev;
1403         struct its_itte *itte;
1404         struct list_head *dev_cur, *dev_temp;
1405         struct list_head *cur, *temp;
1406
1407         /*
1408          * We may end up here without the lists ever having been initialized.
1409          * Check this and bail out early to avoid dereferencing a NULL pointer.
1410          */
1411         if (!its->device_list.next)
1412                 return;
1413
1414         mutex_lock(&its->its_lock);
1415         list_for_each_safe(dev_cur, dev_temp, &its->device_list) {
1416                 dev = container_of(dev_cur, struct its_device, dev_list);
1417                 list_for_each_safe(cur, temp, &dev->itt_head) {
1418                         itte = (container_of(cur, struct its_itte, itte_list));
1419                         its_free_itte(kvm, itte);
1420                 }
1421                 list_del(dev_cur);
1422                 kfree(dev);
1423         }
1424
1425         list_for_each_safe(cur, temp, &its->collection_list) {
1426                 list_del(cur);
1427                 kfree(container_of(cur, struct its_collection, coll_list));
1428         }
1429         mutex_unlock(&its->its_lock);
1430
1431         kfree(its);
1432 }
1433
1434 static int vgic_its_has_attr(struct kvm_device *dev,
1435                              struct kvm_device_attr *attr)
1436 {
1437         switch (attr->group) {
1438         case KVM_DEV_ARM_VGIC_GRP_ADDR:
1439                 switch (attr->attr) {
1440                 case KVM_VGIC_ITS_ADDR_TYPE:
1441                         return 0;
1442                 }
1443                 break;
1444         case KVM_DEV_ARM_VGIC_GRP_CTRL:
1445                 switch (attr->attr) {
1446                 case KVM_DEV_ARM_VGIC_CTRL_INIT:
1447                         return 0;
1448                 }
1449                 break;
1450         }
1451         return -ENXIO;
1452 }
1453
1454 static int vgic_its_set_attr(struct kvm_device *dev,
1455                              struct kvm_device_attr *attr)
1456 {
1457         struct vgic_its *its = dev->private;
1458         int ret;
1459
1460         switch (attr->group) {
1461         case KVM_DEV_ARM_VGIC_GRP_ADDR: {
1462                 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
1463                 unsigned long type = (unsigned long)attr->attr;
1464                 u64 addr;
1465
1466                 if (type != KVM_VGIC_ITS_ADDR_TYPE)
1467                         return -ENODEV;
1468
1469                 if (its->initialized)
1470                         return -EBUSY;
1471
1472                 if (copy_from_user(&addr, uaddr, sizeof(addr)))
1473                         return -EFAULT;
1474
1475                 ret = vgic_check_ioaddr(dev->kvm, &its->vgic_its_base,
1476                                         addr, SZ_64K);
1477                 if (ret)
1478                         return ret;
1479
1480                 its->vgic_its_base = addr;
1481
1482                 return 0;
1483         }
1484         case KVM_DEV_ARM_VGIC_GRP_CTRL:
1485                 switch (attr->attr) {
1486                 case KVM_DEV_ARM_VGIC_CTRL_INIT:
1487                         return vgic_its_init_its(dev->kvm, its);
1488                 }
1489                 break;
1490         }
1491         return -ENXIO;
1492 }
1493
1494 static int vgic_its_get_attr(struct kvm_device *dev,
1495                              struct kvm_device_attr *attr)
1496 {
1497         switch (attr->group) {
1498         case KVM_DEV_ARM_VGIC_GRP_ADDR: {
1499                 struct vgic_its *its = dev->private;
1500                 u64 addr = its->vgic_its_base;
1501                 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
1502                 unsigned long type = (unsigned long)attr->attr;
1503
1504                 if (type != KVM_VGIC_ITS_ADDR_TYPE)
1505                         return -ENODEV;
1506
1507                 if (copy_to_user(uaddr, &addr, sizeof(addr)))
1508                         return -EFAULT;
1509                 break;
1510         default:
1511                 return -ENXIO;
1512         }
1513         }
1514
1515         return 0;
1516 }
1517
1518 static struct kvm_device_ops kvm_arm_vgic_its_ops = {
1519         .name = "kvm-arm-vgic-its",
1520         .create = vgic_its_create,
1521         .destroy = vgic_its_destroy,
1522         .set_attr = vgic_its_set_attr,
1523         .get_attr = vgic_its_get_attr,
1524         .has_attr = vgic_its_has_attr,
1525 };
1526
1527 int kvm_vgic_register_its_device(void)
1528 {
1529         return kvm_register_device_ops(&kvm_arm_vgic_its_ops,
1530                                        KVM_DEV_TYPE_ARM_VGIC_ITS);
1531 }