Andre Przywara | 03f0c94 | 2016-03-25 00:04:53 +0000 | [diff] [blame] | 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> |
Eric Auger | 180ae7b | 2016-07-22 16:20:41 +0000 | [diff] [blame] | 20 | #include <kvm/arm_vgic.h> |
| 21 | #include "vgic.h" |
Andre Przywara | 03f0c94 | 2016-03-25 00:04:53 +0000 | [diff] [blame] | 22 | |
Eric Auger | 180ae7b | 2016-07-22 16:20:41 +0000 | [diff] [blame] | 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) |
Andre Przywara | 03f0c94 | 2016-03-25 00:04:53 +0000 | [diff] [blame] | 32 | { |
Eric Auger | 180ae7b | 2016-07-22 16:20:41 +0000 | [diff] [blame] | 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); |
Andre Przywara | 03f0c94 | 2016-03-25 00:04:53 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Eric Auger | 180ae7b | 2016-07-22 16:20:41 +0000 | [diff] [blame] | 40 | /** |
| 41 | * kvm_set_routing_entry: populate a kvm routing entry |
| 42 | * from a user routing entry |
| 43 | * |
Marc Zyngier | 3f312db | 2016-07-24 10:34:08 +0100 | [diff] [blame] | 44 | * @kvm: the VM this entry is applied to |
Eric Auger | 180ae7b | 2016-07-22 16:20:41 +0000 | [diff] [blame] | 45 | * @e: kvm kernel routing entry handle |
| 46 | * @ue: user api routing entry handle |
| 47 | * return 0 on success, -EINVAL on errors. |
| 48 | */ |
Marc Zyngier | 3f312db | 2016-07-24 10:34:08 +0100 | [diff] [blame] | 49 | int kvm_set_routing_entry(struct kvm *kvm, |
| 50 | struct kvm_kernel_irq_routing_entry *e, |
| 51 | const struct kvm_irq_routing_entry *ue) |
Andre Przywara | 03f0c94 | 2016-03-25 00:04:53 +0000 | [diff] [blame] | 52 | { |
Eric Auger | 180ae7b | 2016-07-22 16:20:41 +0000 | [diff] [blame] | 53 | int r = -EINVAL; |
| 54 | |
| 55 | switch (ue->type) { |
| 56 | case KVM_IRQ_ROUTING_IRQCHIP: |
| 57 | e->set = vgic_irqfd_set_irq; |
| 58 | e->irqchip.irqchip = ue->u.irqchip.irqchip; |
| 59 | e->irqchip.pin = ue->u.irqchip.pin; |
| 60 | if ((e->irqchip.pin >= KVM_IRQCHIP_NUM_PINS) || |
| 61 | (e->irqchip.irqchip >= KVM_NR_IRQCHIPS)) |
| 62 | goto out; |
| 63 | break; |
Eric Auger | 995a0ee | 2016-07-22 16:20:42 +0000 | [diff] [blame] | 64 | case KVM_IRQ_ROUTING_MSI: |
| 65 | e->set = kvm_set_msi; |
| 66 | e->msi.address_lo = ue->u.msi.address_lo; |
| 67 | e->msi.address_hi = ue->u.msi.address_hi; |
| 68 | e->msi.data = ue->u.msi.data; |
| 69 | e->msi.flags = ue->flags; |
| 70 | e->msi.devid = ue->u.msi.devid; |
| 71 | break; |
Eric Auger | 180ae7b | 2016-07-22 16:20:41 +0000 | [diff] [blame] | 72 | default: |
| 73 | goto out; |
| 74 | } |
| 75 | r = 0; |
| 76 | out: |
| 77 | return r; |
Andre Przywara | 03f0c94 | 2016-03-25 00:04:53 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Eric Auger | 180ae7b | 2016-07-22 16:20:41 +0000 | [diff] [blame] | 80 | /** |
| 81 | * kvm_set_msi: inject the MSI corresponding to the |
| 82 | * MSI routing entry |
| 83 | * |
| 84 | * This is the entry point for irqfd MSI injection |
| 85 | * and userspace MSI injection. |
| 86 | */ |
Andre Przywara | 03f0c94 | 2016-03-25 00:04:53 +0000 | [diff] [blame] | 87 | int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, |
| 88 | struct kvm *kvm, int irq_source_id, |
| 89 | int level, bool line_status) |
| 90 | { |
Eric Auger | 180ae7b | 2016-07-22 16:20:41 +0000 | [diff] [blame] | 91 | struct kvm_msi msi; |
| 92 | |
| 93 | msi.address_lo = e->msi.address_lo; |
| 94 | msi.address_hi = e->msi.address_hi; |
| 95 | msi.data = e->msi.data; |
| 96 | msi.flags = e->msi.flags; |
| 97 | msi.devid = e->msi.devid; |
| 98 | |
| 99 | if (!vgic_has_its(kvm)) |
| 100 | return -ENODEV; |
| 101 | |
| 102 | return vgic_its_inject_msi(kvm, &msi); |
| 103 | } |
| 104 | |
| 105 | int kvm_vgic_setup_default_irq_routing(struct kvm *kvm) |
| 106 | { |
| 107 | struct kvm_irq_routing_entry *entries; |
| 108 | struct vgic_dist *dist = &kvm->arch.vgic; |
| 109 | u32 nr = dist->nr_spis; |
| 110 | int i, ret; |
| 111 | |
| 112 | entries = kcalloc(nr, sizeof(struct kvm_kernel_irq_routing_entry), |
| 113 | GFP_KERNEL); |
| 114 | if (!entries) |
| 115 | return -ENOMEM; |
| 116 | |
| 117 | for (i = 0; i < nr; i++) { |
| 118 | entries[i].gsi = i; |
| 119 | entries[i].type = KVM_IRQ_ROUTING_IRQCHIP; |
| 120 | entries[i].u.irqchip.irqchip = 0; |
| 121 | entries[i].u.irqchip.pin = i; |
| 122 | } |
| 123 | ret = kvm_set_irq_routing(kvm, entries, nr, 0); |
| 124 | kfree(entries); |
| 125 | return ret; |
Andre Przywara | 03f0c94 | 2016-03-25 00:04:53 +0000 | [diff] [blame] | 126 | } |