b31a51a14efbe9e26abda69c6998b316a10aaf39
[cascardo/linux.git] / virt / kvm / arm / vgic / vgic-irqfd.c
1 /*
2  * Copyright (C) 2015, 2016 ARM Ltd.
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  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16
17 #include <linux/kvm.h>
18 #include <linux/kvm_host.h>
19 #include <trace/events/kvm.h>
20 #include <kvm/arm_vgic.h>
21 #include "vgic.h"
22
23 /**
24  * vgic_irqfd_set_irq: inject the IRQ corresponding to the
25  * irqchip routing entry
26  *
27  * This is the entry point for irqfd IRQ injection
28  */
29 static int vgic_irqfd_set_irq(struct kvm_kernel_irq_routing_entry *e,
30                         struct kvm *kvm, int irq_source_id,
31                         int level, bool line_status)
32 {
33         unsigned int spi_id = e->irqchip.pin + VGIC_NR_PRIVATE_IRQS;
34
35         if (!vgic_valid_spi(kvm, spi_id))
36                 return -EINVAL;
37         return kvm_vgic_inject_irq(kvm, 0, spi_id, level);
38 }
39
40 /**
41  * kvm_set_routing_entry: populate a kvm routing entry
42  * from a user routing entry
43  *
44  * @kvm: the VM this entry is applied to
45  * @e: kvm kernel routing entry handle
46  * @ue: user api routing entry handle
47  * return 0 on success, -EINVAL on errors.
48  */
49 #ifdef KVM_CAP_X2APIC_API
50 int kvm_set_routing_entry(struct kvm *kvm,
51                           struct kvm_kernel_irq_routing_entry *e,
52                           const struct kvm_irq_routing_entry *ue)
53 #else
54 /* Remove this version and the ifdefery once merged into 4.8 */
55 int kvm_set_routing_entry(struct kvm_kernel_irq_routing_entry *e,
56                           const struct kvm_irq_routing_entry *ue)
57 #endif
58 {
59         int r = -EINVAL;
60
61         switch (ue->type) {
62         case KVM_IRQ_ROUTING_IRQCHIP:
63                 e->set = vgic_irqfd_set_irq;
64                 e->irqchip.irqchip = ue->u.irqchip.irqchip;
65                 e->irqchip.pin = ue->u.irqchip.pin;
66                 if ((e->irqchip.pin >= KVM_IRQCHIP_NUM_PINS) ||
67                     (e->irqchip.irqchip >= KVM_NR_IRQCHIPS))
68                         goto out;
69                 break;
70         case KVM_IRQ_ROUTING_MSI:
71                 e->set = kvm_set_msi;
72                 e->msi.address_lo = ue->u.msi.address_lo;
73                 e->msi.address_hi = ue->u.msi.address_hi;
74                 e->msi.data = ue->u.msi.data;
75                 e->msi.flags = ue->flags;
76                 e->msi.devid = ue->u.msi.devid;
77                 break;
78         default:
79                 goto out;
80         }
81         r = 0;
82 out:
83         return r;
84 }
85
86 /**
87  * kvm_set_msi: inject the MSI corresponding to the
88  * MSI routing entry
89  *
90  * This is the entry point for irqfd MSI injection
91  * and userspace MSI injection.
92  */
93 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
94                 struct kvm *kvm, int irq_source_id,
95                 int level, bool line_status)
96 {
97         struct kvm_msi msi;
98
99         msi.address_lo = e->msi.address_lo;
100         msi.address_hi = e->msi.address_hi;
101         msi.data = e->msi.data;
102         msi.flags = e->msi.flags;
103         msi.devid = e->msi.devid;
104
105         if (!vgic_has_its(kvm))
106                 return -ENODEV;
107
108         return vgic_its_inject_msi(kvm, &msi);
109 }
110
111 int kvm_vgic_setup_default_irq_routing(struct kvm *kvm)
112 {
113         struct kvm_irq_routing_entry *entries;
114         struct vgic_dist *dist = &kvm->arch.vgic;
115         u32 nr = dist->nr_spis;
116         int i, ret;
117
118         entries = kcalloc(nr, sizeof(struct kvm_kernel_irq_routing_entry),
119                           GFP_KERNEL);
120         if (!entries)
121                 return -ENOMEM;
122
123         for (i = 0; i < nr; i++) {
124                 entries[i].gsi = i;
125                 entries[i].type = KVM_IRQ_ROUTING_IRQCHIP;
126                 entries[i].u.irqchip.irqchip = 0;
127                 entries[i].u.irqchip.pin = i;
128         }
129         ret = kvm_set_irq_routing(kvm, entries, nr, 0);
130         kfree(entries);
131         return ret;
132 }