]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - virt/kvm/arm/vgic/vgic-irqfd.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 234
[mirror_ubuntu-jammy-kernel.git] / virt / kvm / arm / vgic / vgic-irqfd.c
CommitLineData
caab277b 1// SPDX-License-Identifier: GPL-2.0-only
03f0c94c
AP
2/*
3 * Copyright (C) 2015, 2016 ARM Ltd.
03f0c94c
AP
4 */
5
6#include <linux/kvm.h>
7#include <linux/kvm_host.h>
8#include <trace/events/kvm.h>
180ae7b1
EA
9#include <kvm/arm_vgic.h>
10#include "vgic.h"
03f0c94c 11
180ae7b1
EA
12/**
13 * vgic_irqfd_set_irq: inject the IRQ corresponding to the
14 * irqchip routing entry
15 *
16 * This is the entry point for irqfd IRQ injection
17 */
18static int vgic_irqfd_set_irq(struct kvm_kernel_irq_routing_entry *e,
19 struct kvm *kvm, int irq_source_id,
20 int level, bool line_status)
03f0c94c 21{
180ae7b1
EA
22 unsigned int spi_id = e->irqchip.pin + VGIC_NR_PRIVATE_IRQS;
23
24 if (!vgic_valid_spi(kvm, spi_id))
25 return -EINVAL;
cb3f0ad8 26 return kvm_vgic_inject_irq(kvm, 0, spi_id, level, NULL);
03f0c94c
AP
27}
28
180ae7b1
EA
29/**
30 * kvm_set_routing_entry: populate a kvm routing entry
31 * from a user routing entry
32 *
3f312db6 33 * @kvm: the VM this entry is applied to
180ae7b1
EA
34 * @e: kvm kernel routing entry handle
35 * @ue: user api routing entry handle
36 * return 0 on success, -EINVAL on errors.
37 */
3f312db6
MZ
38int kvm_set_routing_entry(struct kvm *kvm,
39 struct kvm_kernel_irq_routing_entry *e,
40 const struct kvm_irq_routing_entry *ue)
03f0c94c 41{
180ae7b1
EA
42 int r = -EINVAL;
43
44 switch (ue->type) {
45 case KVM_IRQ_ROUTING_IRQCHIP:
46 e->set = vgic_irqfd_set_irq;
47 e->irqchip.irqchip = ue->u.irqchip.irqchip;
48 e->irqchip.pin = ue->u.irqchip.pin;
49 if ((e->irqchip.pin >= KVM_IRQCHIP_NUM_PINS) ||
50 (e->irqchip.irqchip >= KVM_NR_IRQCHIPS))
51 goto out;
52 break;
995a0ee9
EA
53 case KVM_IRQ_ROUTING_MSI:
54 e->set = kvm_set_msi;
55 e->msi.address_lo = ue->u.msi.address_lo;
56 e->msi.address_hi = ue->u.msi.address_hi;
57 e->msi.data = ue->u.msi.data;
58 e->msi.flags = ue->flags;
59 e->msi.devid = ue->u.msi.devid;
60 break;
180ae7b1
EA
61 default:
62 goto out;
63 }
64 r = 0;
65out:
66 return r;
03f0c94c
AP
67}
68
180ae7b1
EA
69/**
70 * kvm_set_msi: inject the MSI corresponding to the
71 * MSI routing entry
72 *
73 * This is the entry point for irqfd MSI injection
74 * and userspace MSI injection.
75 */
76int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
77 struct kvm *kvm, int irq_source_id,
78 int level, bool line_status)
03f0c94c 79{
180ae7b1 80 struct kvm_msi msi;
03f0c94c 81
180ae7b1
EA
82 msi.address_lo = e->msi.address_lo;
83 msi.address_hi = e->msi.address_hi;
84 msi.data = e->msi.data;
85 msi.flags = e->msi.flags;
86 msi.devid = e->msi.devid;
03f0c94c 87
180ae7b1
EA
88 if (!vgic_has_its(kvm))
89 return -ENODEV;
03f0c94c 90
0bdbf3b0
SD
91 if (!level)
92 return -1;
93
180ae7b1 94 return vgic_its_inject_msi(kvm, &msi);
03f0c94c
AP
95}
96
180ae7b1 97int kvm_vgic_setup_default_irq_routing(struct kvm *kvm)
03f0c94c 98{
180ae7b1
EA
99 struct kvm_irq_routing_entry *entries;
100 struct vgic_dist *dist = &kvm->arch.vgic;
101 u32 nr = dist->nr_spis;
102 int i, ret;
103
150009e2 104 entries = kcalloc(nr, sizeof(*entries), GFP_KERNEL);
180ae7b1
EA
105 if (!entries)
106 return -ENOMEM;
107
108 for (i = 0; i < nr; i++) {
109 entries[i].gsi = i;
110 entries[i].type = KVM_IRQ_ROUTING_IRQCHIP;
111 entries[i].u.irqchip.irqchip = 0;
112 entries[i].u.irqchip.pin = i;
113 }
114 ret = kvm_set_irq_routing(kvm, entries, nr, 0);
115 kfree(entries);
116 return ret;
03f0c94c 117}