blob: fcbfa38e8c41d9c7d273342f42323140d965a346 [file] [log] [blame]
Marc Zyngier140b0862015-11-26 17:19:25 +00001/*
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/irqchip/arm-gic.h>
18#include <linux/kvm.h>
19#include <linux/kvm_host.h>
Eric Auger90977732015-12-01 15:02:35 +010020#include <kvm/arm_vgic.h>
21#include <asm/kvm_mmu.h>
Marc Zyngier140b0862015-11-26 17:19:25 +000022
23#include "vgic.h"
24
25/*
26 * Call this function to convert a u64 value to an unsigned long * bitmask
27 * in a way that works on both 32-bit and 64-bit LE and BE platforms.
28 *
29 * Warning: Calling this function may modify *val.
30 */
31static unsigned long *u64_to_bitmask(u64 *val)
32{
33#if defined(CONFIG_CPU_BIG_ENDIAN) && BITS_PER_LONG == 32
34 *val = (*val >> 32) | (*val << 32);
35#endif
36 return (unsigned long *)val;
37}
38
39void vgic_v2_process_maintenance(struct kvm_vcpu *vcpu)
40{
41 struct vgic_v2_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v2;
42
43 if (cpuif->vgic_misr & GICH_MISR_EOI) {
44 u64 eisr = cpuif->vgic_eisr;
45 unsigned long *eisr_bmap = u64_to_bitmask(&eisr);
46 int lr;
47
48 for_each_set_bit(lr, eisr_bmap, kvm_vgic_global_state.nr_lr) {
49 u32 intid = cpuif->vgic_lr[lr] & GICH_LR_VIRTUALID;
50
51 WARN_ON(cpuif->vgic_lr[lr] & GICH_LR_STATE);
52
53 kvm_notify_acked_irq(vcpu->kvm, 0,
54 intid - VGIC_NR_PRIVATE_IRQS);
55 }
56 }
57
58 /* check and disable underflow maintenance IRQ */
59 cpuif->vgic_hcr &= ~GICH_HCR_UIE;
60
61 /*
62 * In the next iterations of the vcpu loop, if we sync the
63 * vgic state after flushing it, but before entering the guest
64 * (this happens for pending signals and vmid rollovers), then
65 * make sure we don't pick up any old maintenance interrupts
66 * here.
67 */
68 cpuif->vgic_eisr = 0;
69}
70
71void vgic_v2_set_underflow(struct kvm_vcpu *vcpu)
72{
73 struct vgic_v2_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v2;
74
75 cpuif->vgic_hcr |= GICH_HCR_UIE;
76}
77
78/*
79 * transfer the content of the LRs back into the corresponding ap_list:
80 * - active bit is transferred as is
81 * - pending bit is
82 * - transferred as is in case of edge sensitive IRQs
83 * - set to the line-level (resample time) for level sensitive IRQs
84 */
85void vgic_v2_fold_lr_state(struct kvm_vcpu *vcpu)
86{
87 struct vgic_v2_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v2;
88 int lr;
89
90 for (lr = 0; lr < vcpu->arch.vgic_cpu.used_lrs; lr++) {
91 u32 val = cpuif->vgic_lr[lr];
92 u32 intid = val & GICH_LR_VIRTUALID;
93 struct vgic_irq *irq;
94
95 irq = vgic_get_irq(vcpu->kvm, vcpu, intid);
96
97 spin_lock(&irq->irq_lock);
98
99 /* Always preserve the active bit */
100 irq->active = !!(val & GICH_LR_ACTIVE_BIT);
101
102 /* Edge is the only case where we preserve the pending bit */
103 if (irq->config == VGIC_CONFIG_EDGE &&
104 (val & GICH_LR_PENDING_BIT)) {
105 irq->pending = true;
106
107 if (vgic_irq_is_sgi(intid)) {
108 u32 cpuid = val & GICH_LR_PHYSID_CPUID;
109
110 cpuid >>= GICH_LR_PHYSID_CPUID_SHIFT;
111 irq->source |= (1 << cpuid);
112 }
113 }
114
115 /* Clear soft pending state when level IRQs have been acked */
116 if (irq->config == VGIC_CONFIG_LEVEL &&
117 !(val & GICH_LR_PENDING_BIT)) {
118 irq->soft_pending = false;
119 irq->pending = irq->line_level;
120 }
121
122 spin_unlock(&irq->irq_lock);
123 }
124}
125
126/*
127 * Populates the particular LR with the state of a given IRQ:
128 * - for an edge sensitive IRQ the pending state is cleared in struct vgic_irq
129 * - for a level sensitive IRQ the pending state value is unchanged;
130 * it is dictated directly by the input level
131 *
132 * If @irq describes an SGI with multiple sources, we choose the
133 * lowest-numbered source VCPU and clear that bit in the source bitmap.
134 *
135 * The irq_lock must be held by the caller.
136 */
137void vgic_v2_populate_lr(struct kvm_vcpu *vcpu, struct vgic_irq *irq, int lr)
138{
139 u32 val = irq->intid;
140
141 if (irq->pending) {
142 val |= GICH_LR_PENDING_BIT;
143
144 if (irq->config == VGIC_CONFIG_EDGE)
145 irq->pending = false;
146
147 if (vgic_irq_is_sgi(irq->intid)) {
148 u32 src = ffs(irq->source);
149
150 BUG_ON(!src);
151 val |= (src - 1) << GICH_LR_PHYSID_CPUID_SHIFT;
152 irq->source &= ~(1 << (src - 1));
153 if (irq->source)
154 irq->pending = true;
155 }
156 }
157
158 if (irq->active)
159 val |= GICH_LR_ACTIVE_BIT;
160
161 if (irq->hw) {
162 val |= GICH_LR_HW;
163 val |= irq->hwintid << GICH_LR_PHYSID_CPUID_SHIFT;
164 } else {
165 if (irq->config == VGIC_CONFIG_LEVEL)
166 val |= GICH_LR_EOI;
167 }
168
169 /* The GICv2 LR only holds five bits of priority. */
170 val |= (irq->priority >> 3) << GICH_LR_PRIORITY_SHIFT;
171
172 vcpu->arch.vgic_cpu.vgic_v2.vgic_lr[lr] = val;
173}
174
175void vgic_v2_clear_lr(struct kvm_vcpu *vcpu, int lr)
176{
177 vcpu->arch.vgic_cpu.vgic_v2.vgic_lr[lr] = 0;
178}
Andre Przywarae4823a72015-12-03 11:47:37 +0000179
180void vgic_v2_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
181{
182 u32 vmcr;
183
184 vmcr = (vmcrp->ctlr << GICH_VMCR_CTRL_SHIFT) & GICH_VMCR_CTRL_MASK;
185 vmcr |= (vmcrp->abpr << GICH_VMCR_ALIAS_BINPOINT_SHIFT) &
186 GICH_VMCR_ALIAS_BINPOINT_MASK;
187 vmcr |= (vmcrp->bpr << GICH_VMCR_BINPOINT_SHIFT) &
188 GICH_VMCR_BINPOINT_MASK;
189 vmcr |= (vmcrp->pmr << GICH_VMCR_PRIMASK_SHIFT) &
190 GICH_VMCR_PRIMASK_MASK;
191
192 vcpu->arch.vgic_cpu.vgic_v2.vgic_vmcr = vmcr;
193}
194
195void vgic_v2_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
196{
197 u32 vmcr = vcpu->arch.vgic_cpu.vgic_v2.vgic_vmcr;
198
199 vmcrp->ctlr = (vmcr & GICH_VMCR_CTRL_MASK) >>
200 GICH_VMCR_CTRL_SHIFT;
201 vmcrp->abpr = (vmcr & GICH_VMCR_ALIAS_BINPOINT_MASK) >>
202 GICH_VMCR_ALIAS_BINPOINT_SHIFT;
203 vmcrp->bpr = (vmcr & GICH_VMCR_BINPOINT_MASK) >>
204 GICH_VMCR_BINPOINT_SHIFT;
205 vmcrp->pmr = (vmcr & GICH_VMCR_PRIMASK_MASK) >>
206 GICH_VMCR_PRIMASK_SHIFT;
207}
Eric Auger90977732015-12-01 15:02:35 +0100208
Eric Augerad275b8b2015-12-21 18:09:38 +0100209/* not yet implemented */
210void vgic_v2_enable(struct kvm_vcpu *vcpu)
211{
212}
213
Eric Auger90977732015-12-01 15:02:35 +0100214/**
215 * vgic_v2_probe - probe for a GICv2 compatible interrupt controller in DT
216 * @node: pointer to the DT node
217 *
218 * Returns 0 if a GICv2 has been found, returns an error code otherwise
219 */
220int vgic_v2_probe(const struct gic_kvm_info *info)
221{
222 int ret;
223 u32 vtr;
224
225 if (!info->vctrl.start) {
226 kvm_err("GICH not present in the firmware table\n");
227 return -ENXIO;
228 }
229
230 if (!PAGE_ALIGNED(info->vcpu.start)) {
231 kvm_err("GICV physical address 0x%llx not page aligned\n",
232 (unsigned long long)info->vcpu.start);
233 return -ENXIO;
234 }
235
236 if (!PAGE_ALIGNED(resource_size(&info->vcpu))) {
237 kvm_err("GICV size 0x%llx not a multiple of page size 0x%lx\n",
238 (unsigned long long)resource_size(&info->vcpu),
239 PAGE_SIZE);
240 return -ENXIO;
241 }
242
243 kvm_vgic_global_state.vctrl_base = ioremap(info->vctrl.start,
244 resource_size(&info->vctrl));
245 if (!kvm_vgic_global_state.vctrl_base) {
246 kvm_err("Cannot ioremap GICH\n");
247 return -ENOMEM;
248 }
249
250 vtr = readl_relaxed(kvm_vgic_global_state.vctrl_base + GICH_VTR);
251 kvm_vgic_global_state.nr_lr = (vtr & 0x3f) + 1;
252
253 ret = create_hyp_io_mappings(kvm_vgic_global_state.vctrl_base,
254 kvm_vgic_global_state.vctrl_base +
255 resource_size(&info->vctrl),
256 info->vctrl.start);
257
258 if (ret) {
259 kvm_err("Cannot map VCTRL into hyp\n");
260 iounmap(kvm_vgic_global_state.vctrl_base);
261 return ret;
262 }
263
264 kvm_vgic_global_state.can_emulate_gicv2 = true;
265 kvm_register_vgic_device(KVM_DEV_TYPE_ARM_VGIC_V2);
266
267 kvm_vgic_global_state.vcpu_base = info->vcpu.start;
268 kvm_vgic_global_state.type = VGIC_V2;
269 kvm_vgic_global_state.max_gic_vcpus = VGIC_V2_MAX_CPUS;
270
271 kvm_info("vgic-v2@%llx\n", info->vctrl.start);
272
273 return 0;
274}