KVM: arm/arm64: Enable MSI routing
[deliverable/linux.git] / virt / kvm / arm / vgic / vgic-irqfd.c
CommitLineData
03f0c94c
AP
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>
180ae7b1
EA
20#include <kvm/arm_vgic.h>
21#include "vgic.h"
03f0c94c 22
180ae7b1
EA
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 */
29static 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)
03f0c94c 32{
180ae7b1
EA
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);
03f0c94c
AP
38}
39
180ae7b1
EA
40/**
41 * kvm_set_routing_entry: populate a kvm routing entry
42 * from a user routing entry
43 *
44 * @e: kvm kernel routing entry handle
45 * @ue: user api routing entry handle
46 * return 0 on success, -EINVAL on errors.
47 */
48int kvm_set_routing_entry(struct kvm_kernel_irq_routing_entry *e,
49 const struct kvm_irq_routing_entry *ue)
03f0c94c 50{
180ae7b1
EA
51 int r = -EINVAL;
52
53 switch (ue->type) {
54 case KVM_IRQ_ROUTING_IRQCHIP:
55 e->set = vgic_irqfd_set_irq;
56 e->irqchip.irqchip = ue->u.irqchip.irqchip;
57 e->irqchip.pin = ue->u.irqchip.pin;
58 if ((e->irqchip.pin >= KVM_IRQCHIP_NUM_PINS) ||
59 (e->irqchip.irqchip >= KVM_NR_IRQCHIPS))
60 goto out;
61 break;
995a0ee9
EA
62 case KVM_IRQ_ROUTING_MSI:
63 e->set = kvm_set_msi;
64 e->msi.address_lo = ue->u.msi.address_lo;
65 e->msi.address_hi = ue->u.msi.address_hi;
66 e->msi.data = ue->u.msi.data;
67 e->msi.flags = ue->flags;
68 e->msi.devid = ue->u.msi.devid;
69 break;
180ae7b1
EA
70 default:
71 goto out;
72 }
73 r = 0;
74out:
75 return r;
03f0c94c
AP
76}
77
180ae7b1
EA
78/**
79 * kvm_set_msi: inject the MSI corresponding to the
80 * MSI routing entry
81 *
82 * This is the entry point for irqfd MSI injection
83 * and userspace MSI injection.
84 */
85int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
86 struct kvm *kvm, int irq_source_id,
87 int level, bool line_status)
03f0c94c 88{
180ae7b1 89 struct kvm_msi msi;
03f0c94c 90
180ae7b1
EA
91 msi.address_lo = e->msi.address_lo;
92 msi.address_hi = e->msi.address_hi;
93 msi.data = e->msi.data;
94 msi.flags = e->msi.flags;
95 msi.devid = e->msi.devid;
03f0c94c 96
180ae7b1
EA
97 if (!vgic_has_its(kvm))
98 return -ENODEV;
03f0c94c 99
180ae7b1 100 return vgic_its_inject_msi(kvm, &msi);
03f0c94c
AP
101}
102
180ae7b1 103int kvm_vgic_setup_default_irq_routing(struct kvm *kvm)
03f0c94c 104{
180ae7b1
EA
105 struct kvm_irq_routing_entry *entries;
106 struct vgic_dist *dist = &kvm->arch.vgic;
107 u32 nr = dist->nr_spis;
108 int i, ret;
109
110 entries = kcalloc(nr, sizeof(struct kvm_kernel_irq_routing_entry),
111 GFP_KERNEL);
112 if (!entries)
113 return -ENOMEM;
114
115 for (i = 0; i < nr; i++) {
116 entries[i].gsi = i;
117 entries[i].type = KVM_IRQ_ROUTING_IRQCHIP;
118 entries[i].u.irqchip.irqchip = 0;
119 entries[i].u.irqchip.pin = i;
120 }
121 ret = kvm_set_irq_routing(kvm, entries, nr, 0);
122 kfree(entries);
123 return ret;
03f0c94c 124}
This page took 0.033906 seconds and 5 git commands to generate.