]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - virt/kvm/arm/vgic/vgic-init.c
KVM: arm/arm64: vgic-new: vgic_init: implement kvm_vgic_hyp_init
[mirror_ubuntu-zesty-kernel.git] / virt / kvm / arm / vgic / vgic-init.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/uaccess.h>
18 #include <linux/interrupt.h>
19 #include <linux/cpu.h>
20 #include <linux/kvm_host.h>
21 #include <kvm/arm_vgic.h>
22 #include <asm/kvm_mmu.h>
23 #include "vgic.h"
24
25 /* GENERIC PROBE */
26
27 static void vgic_init_maintenance_interrupt(void *info)
28 {
29 enable_percpu_irq(kvm_vgic_global_state.maint_irq, 0);
30 }
31
32 static int vgic_cpu_notify(struct notifier_block *self,
33 unsigned long action, void *cpu)
34 {
35 switch (action) {
36 case CPU_STARTING:
37 case CPU_STARTING_FROZEN:
38 vgic_init_maintenance_interrupt(NULL);
39 break;
40 case CPU_DYING:
41 case CPU_DYING_FROZEN:
42 disable_percpu_irq(kvm_vgic_global_state.maint_irq);
43 break;
44 }
45
46 return NOTIFY_OK;
47 }
48
49 static struct notifier_block vgic_cpu_nb = {
50 .notifier_call = vgic_cpu_notify,
51 };
52
53 static irqreturn_t vgic_maintenance_handler(int irq, void *data)
54 {
55 /*
56 * We cannot rely on the vgic maintenance interrupt to be
57 * delivered synchronously. This means we can only use it to
58 * exit the VM, and we perform the handling of EOIed
59 * interrupts on the exit path (see vgic_process_maintenance).
60 */
61 return IRQ_HANDLED;
62 }
63
64 /**
65 * kvm_vgic_hyp_init: populates the kvm_vgic_global_state variable
66 * according to the host GIC model. Accordingly calls either
67 * vgic_v2/v3_probe which registers the KVM_DEVICE that can be
68 * instantiated by a guest later on .
69 */
70 int kvm_vgic_hyp_init(void)
71 {
72 const struct gic_kvm_info *gic_kvm_info;
73 int ret;
74
75 gic_kvm_info = gic_get_kvm_info();
76 if (!gic_kvm_info)
77 return -ENODEV;
78
79 if (!gic_kvm_info->maint_irq) {
80 kvm_err("No vgic maintenance irq\n");
81 return -ENXIO;
82 }
83
84 switch (gic_kvm_info->type) {
85 case GIC_V2:
86 ret = vgic_v2_probe(gic_kvm_info);
87 break;
88 case GIC_V3:
89 ret = vgic_v3_probe(gic_kvm_info);
90 break;
91 default:
92 ret = -ENODEV;
93 };
94
95 if (ret)
96 return ret;
97
98 kvm_vgic_global_state.maint_irq = gic_kvm_info->maint_irq;
99 ret = request_percpu_irq(kvm_vgic_global_state.maint_irq,
100 vgic_maintenance_handler,
101 "vgic", kvm_get_running_vcpus());
102 if (ret) {
103 kvm_err("Cannot register interrupt %d\n",
104 kvm_vgic_global_state.maint_irq);
105 return ret;
106 }
107
108 ret = __register_cpu_notifier(&vgic_cpu_nb);
109 if (ret) {
110 kvm_err("Cannot register vgic CPU notifier\n");
111 goto out_free_irq;
112 }
113
114 on_each_cpu(vgic_init_maintenance_interrupt, NULL, 1);
115
116 kvm_info("vgic interrupt IRQ%d\n", kvm_vgic_global_state.maint_irq);
117 return 0;
118
119 out_free_irq:
120 free_percpu_irq(kvm_vgic_global_state.maint_irq,
121 kvm_get_running_vcpus());
122 return ret;
123 }