blob: 437d7af0868308959ffddf3de3a46010757774a5 [file] [log] [blame]
Marc Zyngierf68d2b12015-10-19 15:50:58 +01001/*
2 * Copyright (C) 2012-2015 - ARM Ltd
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <linux/compiler.h>
19#include <linux/irqchip/arm-gic-v3.h>
20#include <linux/kvm_host.h>
21
Marc Zyngier59da1cb2017-06-09 12:49:33 +010022#include <asm/kvm_emulate.h>
Marc Zyngier13720a52016-01-28 13:44:07 +000023#include <asm/kvm_hyp.h>
Christoffer Dall923a2e32017-10-05 00:18:07 +020024#include <asm/kvm_mmu.h>
Marc Zyngierf68d2b12015-10-19 15:50:58 +010025
26#define vtr_to_max_lr_idx(v) ((v) & 0xf)
Christoffer Dalld68356c2017-06-04 22:17:02 +020027#define vtr_to_nr_pre_bits(v) ((((u32)(v) >> 26) & 7) + 1)
Marc Zyngier132a3242017-06-09 12:49:36 +010028#define vtr_to_nr_apr_regs(v) (1 << (vtr_to_nr_pre_bits(v) - 5))
Marc Zyngierf68d2b12015-10-19 15:50:58 +010029
Marc Zyngier1b8e83c2016-02-17 10:25:05 +000030static u64 __hyp_text __gic_v3_get_lr(unsigned int lr)
31{
32 switch (lr & 0xf) {
33 case 0:
34 return read_gicreg(ICH_LR0_EL2);
35 case 1:
36 return read_gicreg(ICH_LR1_EL2);
37 case 2:
38 return read_gicreg(ICH_LR2_EL2);
39 case 3:
40 return read_gicreg(ICH_LR3_EL2);
41 case 4:
42 return read_gicreg(ICH_LR4_EL2);
43 case 5:
44 return read_gicreg(ICH_LR5_EL2);
45 case 6:
46 return read_gicreg(ICH_LR6_EL2);
47 case 7:
48 return read_gicreg(ICH_LR7_EL2);
49 case 8:
50 return read_gicreg(ICH_LR8_EL2);
51 case 9:
52 return read_gicreg(ICH_LR9_EL2);
53 case 10:
54 return read_gicreg(ICH_LR10_EL2);
55 case 11:
56 return read_gicreg(ICH_LR11_EL2);
57 case 12:
58 return read_gicreg(ICH_LR12_EL2);
59 case 13:
60 return read_gicreg(ICH_LR13_EL2);
61 case 14:
62 return read_gicreg(ICH_LR14_EL2);
63 case 15:
64 return read_gicreg(ICH_LR15_EL2);
65 }
66
67 unreachable();
68}
69
70static void __hyp_text __gic_v3_set_lr(u64 val, int lr)
71{
72 switch (lr & 0xf) {
73 case 0:
74 write_gicreg(val, ICH_LR0_EL2);
75 break;
76 case 1:
77 write_gicreg(val, ICH_LR1_EL2);
78 break;
79 case 2:
80 write_gicreg(val, ICH_LR2_EL2);
81 break;
82 case 3:
83 write_gicreg(val, ICH_LR3_EL2);
84 break;
85 case 4:
86 write_gicreg(val, ICH_LR4_EL2);
87 break;
88 case 5:
89 write_gicreg(val, ICH_LR5_EL2);
90 break;
91 case 6:
92 write_gicreg(val, ICH_LR6_EL2);
93 break;
94 case 7:
95 write_gicreg(val, ICH_LR7_EL2);
96 break;
97 case 8:
98 write_gicreg(val, ICH_LR8_EL2);
99 break;
100 case 9:
101 write_gicreg(val, ICH_LR9_EL2);
102 break;
103 case 10:
104 write_gicreg(val, ICH_LR10_EL2);
105 break;
106 case 11:
107 write_gicreg(val, ICH_LR11_EL2);
108 break;
109 case 12:
110 write_gicreg(val, ICH_LR12_EL2);
111 break;
112 case 13:
113 write_gicreg(val, ICH_LR13_EL2);
114 break;
115 case 14:
116 write_gicreg(val, ICH_LR14_EL2);
117 break;
118 case 15:
119 write_gicreg(val, ICH_LR15_EL2);
120 break;
121 }
122}
123
Marc Zyngier63000dd2017-06-09 12:49:31 +0100124static void __hyp_text __vgic_v3_write_ap0rn(u32 val, int n)
125{
126 switch (n) {
127 case 0:
128 write_gicreg(val, ICH_AP0R0_EL2);
129 break;
130 case 1:
131 write_gicreg(val, ICH_AP0R1_EL2);
132 break;
133 case 2:
134 write_gicreg(val, ICH_AP0R2_EL2);
135 break;
136 case 3:
137 write_gicreg(val, ICH_AP0R3_EL2);
138 break;
139 }
140}
141
142static void __hyp_text __vgic_v3_write_ap1rn(u32 val, int n)
143{
144 switch (n) {
145 case 0:
146 write_gicreg(val, ICH_AP1R0_EL2);
147 break;
148 case 1:
149 write_gicreg(val, ICH_AP1R1_EL2);
150 break;
151 case 2:
152 write_gicreg(val, ICH_AP1R2_EL2);
153 break;
154 case 3:
155 write_gicreg(val, ICH_AP1R3_EL2);
156 break;
157 }
158}
159
160static u32 __hyp_text __vgic_v3_read_ap0rn(int n)
161{
162 u32 val;
163
164 switch (n) {
165 case 0:
166 val = read_gicreg(ICH_AP0R0_EL2);
167 break;
168 case 1:
169 val = read_gicreg(ICH_AP0R1_EL2);
170 break;
171 case 2:
172 val = read_gicreg(ICH_AP0R2_EL2);
173 break;
174 case 3:
175 val = read_gicreg(ICH_AP0R3_EL2);
176 break;
177 default:
178 unreachable();
179 }
180
181 return val;
182}
183
184static u32 __hyp_text __vgic_v3_read_ap1rn(int n)
185{
186 u32 val;
187
188 switch (n) {
189 case 0:
190 val = read_gicreg(ICH_AP1R0_EL2);
191 break;
192 case 1:
193 val = read_gicreg(ICH_AP1R1_EL2);
194 break;
195 case 2:
196 val = read_gicreg(ICH_AP1R2_EL2);
197 break;
198 case 3:
199 val = read_gicreg(ICH_AP1R3_EL2);
200 break;
201 default:
202 unreachable();
203 }
204
205 return val;
206}
207
Marc Zyngierf68d2b12015-10-19 15:50:58 +0100208void __hyp_text __vgic_v3_save_state(struct kvm_vcpu *vcpu)
209{
210 struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3;
Christoffer Dall00dafa02016-12-23 00:04:59 +0100211 u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
Marc Zyngierf68d2b12015-10-19 15:50:58 +0100212 u64 val;
Marc Zyngierf68d2b12015-10-19 15:50:58 +0100213
214 /*
215 * Make sure stores to the GIC via the memory mapped interface
216 * are now visible to the system register interface.
217 */
Marc Zyngierff567612017-04-19 12:15:26 +0100218 if (!cpu_if->vgic_sre) {
Marc Zyngierc5851322016-05-25 15:26:39 +0100219 dsb(st);
Marc Zyngierff567612017-04-19 12:15:26 +0100220 cpu_if->vgic_vmcr = read_gicreg(ICH_VMCR_EL2);
221 }
Marc Zyngierf68d2b12015-10-19 15:50:58 +0100222
Christoffer Dall00dafa02016-12-23 00:04:59 +0100223 if (used_lrs) {
Marc Zyngier1b8e83c2016-02-17 10:25:05 +0000224 int i;
Christoffer Dallbb5ed702017-10-05 00:02:41 +0200225 u32 elrsr;
Marc Zyngierf68d2b12015-10-19 15:50:58 +0100226
Christoffer Dallbb5ed702017-10-05 00:02:41 +0200227 elrsr = read_gicreg(ICH_ELSR_EL2);
Marc Zyngierf68d2b12015-10-19 15:50:58 +0100228
Marc Zyngier1b8e83c2016-02-17 10:25:05 +0000229 write_gicreg(0, ICH_HCR_EL2);
Marc Zyngierf68d2b12015-10-19 15:50:58 +0100230
Marc Zyngiercffcd9d2017-04-10 10:19:44 +0100231 for (i = 0; i < used_lrs; i++) {
Christoffer Dallbb5ed702017-10-05 00:02:41 +0200232 if (elrsr & (1 << i))
Marc Zyngier84e8b9c2016-02-09 17:09:49 +0000233 cpu_if->vgic_lr[i] &= ~ICH_LR_STATE;
Christoffer Dallfa89c772016-05-25 15:26:34 +0100234 else
235 cpu_if->vgic_lr[i] = __gic_v3_get_lr(i);
Marc Zyngier84e8b9c2016-02-09 17:09:49 +0000236
Marc Zyngierb40c4892016-02-09 17:36:09 +0000237 __gic_v3_set_lr(0, i);
Marc Zyngier1b8e83c2016-02-17 10:25:05 +0000238 }
Marc Zyngier1b8e83c2016-02-17 10:25:05 +0000239 } else {
Marc Zyngier374be352017-10-27 15:28:51 +0100240 if (static_branch_unlikely(&vgic_v3_cpuif_trap) ||
241 cpu_if->its_vpe.its_vm)
Marc Zyngier9c7bfc22017-06-09 12:49:40 +0100242 write_gicreg(0, ICH_HCR_EL2);
Marc Zyngierf68d2b12015-10-19 15:50:58 +0100243 }
244
245 val = read_gicreg(ICC_SRE_EL2);
246 write_gicreg(val | ICC_SRE_EL2_ENABLE, ICC_SRE_EL2);
Marc Zyngierc5851322016-05-25 15:26:39 +0100247
248 if (!cpu_if->vgic_sre) {
249 /* Make sure ENABLE is set at EL2 before setting SRE at EL1 */
250 isb();
251 write_gicreg(1, ICC_SRE_EL1);
252 }
Marc Zyngierf68d2b12015-10-19 15:50:58 +0100253}
254
255void __hyp_text __vgic_v3_restore_state(struct kvm_vcpu *vcpu)
256{
257 struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3;
Christoffer Dall00dafa02016-12-23 00:04:59 +0100258 u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
Marc Zyngier1b8e83c2016-02-17 10:25:05 +0000259 int i;
Marc Zyngierf68d2b12015-10-19 15:50:58 +0100260
261 /*
262 * VFIQEn is RES1 if ICC_SRE_EL1.SRE is 1. This causes a
263 * Group0 interrupt (as generated in GICv2 mode) to be
264 * delivered as a FIQ to the guest, with potentially fatal
265 * consequences. So we must make sure that ICC_SRE_EL1 has
266 * been actually programmed with the value we want before
Marc Zyngierff567612017-04-19 12:15:26 +0100267 * starting to mess with the rest of the GIC, and VMCR_EL2 in
268 * particular.
Marc Zyngierf68d2b12015-10-19 15:50:58 +0100269 */
Marc Zyngierc5851322016-05-25 15:26:39 +0100270 if (!cpu_if->vgic_sre) {
271 write_gicreg(0, ICC_SRE_EL1);
272 isb();
Marc Zyngierff567612017-04-19 12:15:26 +0100273 write_gicreg(cpu_if->vgic_vmcr, ICH_VMCR_EL2);
Marc Zyngierc5851322016-05-25 15:26:39 +0100274 }
Marc Zyngierf68d2b12015-10-19 15:50:58 +0100275
Christoffer Dall00dafa02016-12-23 00:04:59 +0100276 if (used_lrs) {
Marc Zyngier1b8e83c2016-02-17 10:25:05 +0000277 write_gicreg(cpu_if->vgic_hcr, ICH_HCR_EL2);
278
Christoffer Dall00dafa02016-12-23 00:04:59 +0100279 for (i = 0; i < used_lrs; i++)
Marc Zyngierb40c4892016-02-09 17:36:09 +0000280 __gic_v3_set_lr(cpu_if->vgic_lr[i], i);
Marc Zyngier9c7bfc22017-06-09 12:49:40 +0100281 } else {
282 /*
283 * If we need to trap system registers, we must write
284 * ICH_HCR_EL2 anyway, even if no interrupts are being
Marc Zyngier374be352017-10-27 15:28:51 +0100285 * injected. Same thing if GICv4 is used, as VLPI
286 * delivery is gated by ICH_HCR_EL2.En.
Marc Zyngier9c7bfc22017-06-09 12:49:40 +0100287 */
Marc Zyngier374be352017-10-27 15:28:51 +0100288 if (static_branch_unlikely(&vgic_v3_cpuif_trap) ||
289 cpu_if->its_vpe.its_vm)
Marc Zyngier9c7bfc22017-06-09 12:49:40 +0100290 write_gicreg(cpu_if->vgic_hcr, ICH_HCR_EL2);
Marc Zyngierf68d2b12015-10-19 15:50:58 +0100291 }
292
293 /*
294 * Ensures that the above will have reached the
295 * (re)distributors. This ensure the guest will read the
296 * correct values from the memory-mapped interface.
297 */
Marc Zyngierc5851322016-05-25 15:26:39 +0100298 if (!cpu_if->vgic_sre) {
299 isb();
300 dsb(sy);
301 }
Marc Zyngierf68d2b12015-10-19 15:50:58 +0100302
303 /*
304 * Prevent the guest from touching the GIC system registers if
305 * SRE isn't enabled for GICv3 emulation.
306 */
Marc Zyngiera0570012016-05-25 15:26:38 +0100307 write_gicreg(read_gicreg(ICC_SRE_EL2) & ~ICC_SRE_EL2_ENABLE,
308 ICC_SRE_EL2);
Marc Zyngierf68d2b12015-10-19 15:50:58 +0100309}
310
Christoffer Dall923a2e32017-10-05 00:18:07 +0200311void __hyp_text __vgic_v3_save_aprs(struct kvm_vcpu *vcpu)
312{
313 struct vgic_v3_cpu_if *cpu_if;
314 u64 val;
315 u32 nr_pre_bits;
316
317 vcpu = kern_hyp_va(vcpu);
318 cpu_if = &vcpu->arch.vgic_cpu.vgic_v3;
319
320 val = read_gicreg(ICH_VTR_EL2);
321 nr_pre_bits = vtr_to_nr_pre_bits(val);
322
323 switch (nr_pre_bits) {
324 case 7:
325 cpu_if->vgic_ap0r[3] = __vgic_v3_read_ap0rn(3);
326 cpu_if->vgic_ap0r[2] = __vgic_v3_read_ap0rn(2);
327 case 6:
328 cpu_if->vgic_ap0r[1] = __vgic_v3_read_ap0rn(1);
329 default:
330 cpu_if->vgic_ap0r[0] = __vgic_v3_read_ap0rn(0);
331 }
332
333 switch (nr_pre_bits) {
334 case 7:
335 cpu_if->vgic_ap1r[3] = __vgic_v3_read_ap1rn(3);
336 cpu_if->vgic_ap1r[2] = __vgic_v3_read_ap1rn(2);
337 case 6:
338 cpu_if->vgic_ap1r[1] = __vgic_v3_read_ap1rn(1);
339 default:
340 cpu_if->vgic_ap1r[0] = __vgic_v3_read_ap1rn(0);
341 }
342}
343
344void __hyp_text __vgic_v3_restore_aprs(struct kvm_vcpu *vcpu)
345{
346 struct vgic_v3_cpu_if *cpu_if;
347 u64 val;
348 u32 nr_pre_bits;
349
350 vcpu = kern_hyp_va(vcpu);
351 cpu_if = &vcpu->arch.vgic_cpu.vgic_v3;
352
353 val = read_gicreg(ICH_VTR_EL2);
354 nr_pre_bits = vtr_to_nr_pre_bits(val);
355
356 switch (nr_pre_bits) {
357 case 7:
358 __vgic_v3_write_ap0rn(cpu_if->vgic_ap0r[3], 3);
359 __vgic_v3_write_ap0rn(cpu_if->vgic_ap0r[2], 2);
360 case 6:
361 __vgic_v3_write_ap0rn(cpu_if->vgic_ap0r[1], 1);
362 default:
363 __vgic_v3_write_ap0rn(cpu_if->vgic_ap0r[0], 0);
364 }
365
366 switch (nr_pre_bits) {
367 case 7:
368 __vgic_v3_write_ap1rn(cpu_if->vgic_ap1r[3], 3);
369 __vgic_v3_write_ap1rn(cpu_if->vgic_ap1r[2], 2);
370 case 6:
371 __vgic_v3_write_ap1rn(cpu_if->vgic_ap1r[1], 1);
372 default:
373 __vgic_v3_write_ap1rn(cpu_if->vgic_ap1r[0], 0);
374 }
375}
376
Marc Zyngier0d98d002016-03-03 15:43:58 +0000377void __hyp_text __vgic_v3_init_lrs(void)
378{
379 int max_lr_idx = vtr_to_max_lr_idx(read_gicreg(ICH_VTR_EL2));
380 int i;
381
382 for (i = 0; i <= max_lr_idx; i++)
383 __gic_v3_set_lr(0, i);
384}
385
Christoffer Dallcf0ba182016-09-01 13:16:03 +0200386u64 __hyp_text __vgic_v3_get_ich_vtr_el2(void)
Marc Zyngierf68d2b12015-10-19 15:50:58 +0100387{
388 return read_gicreg(ICH_VTR_EL2);
389}
Christoffer Dall328e5662016-03-24 11:21:04 +0100390
391u64 __hyp_text __vgic_v3_read_vmcr(void)
392{
393 return read_gicreg(ICH_VMCR_EL2);
394}
395
396void __hyp_text __vgic_v3_write_vmcr(u32 vmcr)
397{
398 write_gicreg(vmcr, ICH_VMCR_EL2);
399}
Marc Zyngier59da1cb2017-06-09 12:49:33 +0100400
401#ifdef CONFIG_ARM64
402
Marc Zyngierd70c7b32017-06-09 12:49:34 +0100403static int __hyp_text __vgic_v3_bpr_min(void)
404{
405 /* See Pseudocode for VPriorityGroup */
406 return 8 - vtr_to_nr_pre_bits(read_gicreg(ICH_VTR_EL2));
407}
408
Marc Zyngier132a3242017-06-09 12:49:36 +0100409static int __hyp_text __vgic_v3_get_group(struct kvm_vcpu *vcpu)
410{
411 u32 esr = kvm_vcpu_get_hsr(vcpu);
412 u8 crm = (esr & ESR_ELx_SYS64_ISS_CRM_MASK) >> ESR_ELx_SYS64_ISS_CRM_SHIFT;
413
414 return crm != 8;
415}
416
417#define GICv3_IDLE_PRIORITY 0xff
418
419static int __hyp_text __vgic_v3_highest_priority_lr(struct kvm_vcpu *vcpu,
420 u32 vmcr,
421 u64 *lr_val)
422{
423 unsigned int used_lrs = vcpu->arch.vgic_cpu.used_lrs;
424 u8 priority = GICv3_IDLE_PRIORITY;
425 int i, lr = -1;
426
427 for (i = 0; i < used_lrs; i++) {
428 u64 val = __gic_v3_get_lr(i);
429 u8 lr_prio = (val & ICH_LR_PRIORITY_MASK) >> ICH_LR_PRIORITY_SHIFT;
430
431 /* Not pending in the state? */
432 if ((val & ICH_LR_STATE) != ICH_LR_PENDING_BIT)
433 continue;
434
435 /* Group-0 interrupt, but Group-0 disabled? */
436 if (!(val & ICH_LR_GROUP) && !(vmcr & ICH_VMCR_ENG0_MASK))
437 continue;
438
439 /* Group-1 interrupt, but Group-1 disabled? */
440 if ((val & ICH_LR_GROUP) && !(vmcr & ICH_VMCR_ENG1_MASK))
441 continue;
442
443 /* Not the highest priority? */
444 if (lr_prio >= priority)
445 continue;
446
447 /* This is a candidate */
448 priority = lr_prio;
449 *lr_val = val;
450 lr = i;
451 }
452
453 if (lr == -1)
454 *lr_val = ICC_IAR1_EL1_SPURIOUS;
455
456 return lr;
457}
458
Marc Zyngierb6f49032017-06-09 12:49:37 +0100459static int __hyp_text __vgic_v3_find_active_lr(struct kvm_vcpu *vcpu,
460 int intid, u64 *lr_val)
461{
462 unsigned int used_lrs = vcpu->arch.vgic_cpu.used_lrs;
463 int i;
464
465 for (i = 0; i < used_lrs; i++) {
466 u64 val = __gic_v3_get_lr(i);
467
468 if ((val & ICH_LR_VIRTUAL_ID_MASK) == intid &&
469 (val & ICH_LR_ACTIVE_BIT)) {
470 *lr_val = val;
471 return i;
472 }
473 }
474
475 *lr_val = ICC_IAR1_EL1_SPURIOUS;
476 return -1;
477}
478
Marc Zyngier132a3242017-06-09 12:49:36 +0100479static int __hyp_text __vgic_v3_get_highest_active_priority(void)
480{
481 u8 nr_apr_regs = vtr_to_nr_apr_regs(read_gicreg(ICH_VTR_EL2));
482 u32 hap = 0;
483 int i;
484
485 for (i = 0; i < nr_apr_regs; i++) {
486 u32 val;
487
488 /*
489 * The ICH_AP0Rn_EL2 and ICH_AP1Rn_EL2 registers
490 * contain the active priority levels for this VCPU
491 * for the maximum number of supported priority
492 * levels, and we return the full priority level only
493 * if the BPR is programmed to its minimum, otherwise
494 * we return a combination of the priority level and
495 * subpriority, as determined by the setting of the
496 * BPR, but without the full subpriority.
497 */
498 val = __vgic_v3_read_ap0rn(i);
499 val |= __vgic_v3_read_ap1rn(i);
500 if (!val) {
501 hap += 32;
502 continue;
503 }
504
505 return (hap + __ffs(val)) << __vgic_v3_bpr_min();
506 }
507
508 return GICv3_IDLE_PRIORITY;
509}
510
Marc Zyngierd70c7b32017-06-09 12:49:34 +0100511static unsigned int __hyp_text __vgic_v3_get_bpr0(u32 vmcr)
512{
513 return (vmcr & ICH_VMCR_BPR0_MASK) >> ICH_VMCR_BPR0_SHIFT;
514}
515
516static unsigned int __hyp_text __vgic_v3_get_bpr1(u32 vmcr)
517{
518 unsigned int bpr;
519
520 if (vmcr & ICH_VMCR_CBPR_MASK) {
521 bpr = __vgic_v3_get_bpr0(vmcr);
522 if (bpr < 7)
523 bpr++;
524 } else {
525 bpr = (vmcr & ICH_VMCR_BPR1_MASK) >> ICH_VMCR_BPR1_SHIFT;
526 }
527
528 return bpr;
529}
530
Marc Zyngier132a3242017-06-09 12:49:36 +0100531/*
532 * Convert a priority to a preemption level, taking the relevant BPR
533 * into account by zeroing the sub-priority bits.
534 */
535static u8 __hyp_text __vgic_v3_pri_to_pre(u8 pri, u32 vmcr, int grp)
536{
537 unsigned int bpr;
538
539 if (!grp)
540 bpr = __vgic_v3_get_bpr0(vmcr) + 1;
541 else
542 bpr = __vgic_v3_get_bpr1(vmcr);
543
544 return pri & (GENMASK(7, 0) << bpr);
545}
546
547/*
548 * The priority value is independent of any of the BPR values, so we
549 * normalize it using the minumal BPR value. This guarantees that no
550 * matter what the guest does with its BPR, we can always set/get the
551 * same value of a priority.
552 */
553static void __hyp_text __vgic_v3_set_active_priority(u8 pri, u32 vmcr, int grp)
554{
555 u8 pre, ap;
556 u32 val;
557 int apr;
558
559 pre = __vgic_v3_pri_to_pre(pri, vmcr, grp);
560 ap = pre >> __vgic_v3_bpr_min();
561 apr = ap / 32;
562
563 if (!grp) {
564 val = __vgic_v3_read_ap0rn(apr);
565 __vgic_v3_write_ap0rn(val | BIT(ap % 32), apr);
566 } else {
567 val = __vgic_v3_read_ap1rn(apr);
568 __vgic_v3_write_ap1rn(val | BIT(ap % 32), apr);
569 }
570}
571
Marc Zyngierb6f49032017-06-09 12:49:37 +0100572static int __hyp_text __vgic_v3_clear_highest_active_priority(void)
573{
574 u8 nr_apr_regs = vtr_to_nr_apr_regs(read_gicreg(ICH_VTR_EL2));
575 u32 hap = 0;
576 int i;
577
578 for (i = 0; i < nr_apr_regs; i++) {
579 u32 ap0, ap1;
580 int c0, c1;
581
582 ap0 = __vgic_v3_read_ap0rn(i);
583 ap1 = __vgic_v3_read_ap1rn(i);
584 if (!ap0 && !ap1) {
585 hap += 32;
586 continue;
587 }
588
589 c0 = ap0 ? __ffs(ap0) : 32;
590 c1 = ap1 ? __ffs(ap1) : 32;
591
592 /* Always clear the LSB, which is the highest priority */
593 if (c0 < c1) {
594 ap0 &= ~BIT(c0);
595 __vgic_v3_write_ap0rn(ap0, i);
596 hap += c0;
597 } else {
598 ap1 &= ~BIT(c1);
599 __vgic_v3_write_ap1rn(ap1, i);
600 hap += c1;
601 }
602
603 /* Rescale to 8 bits of priority */
604 return hap << __vgic_v3_bpr_min();
605 }
606
607 return GICv3_IDLE_PRIORITY;
608}
609
Marc Zyngier132a3242017-06-09 12:49:36 +0100610static void __hyp_text __vgic_v3_read_iar(struct kvm_vcpu *vcpu, u32 vmcr, int rt)
611{
612 u64 lr_val;
613 u8 lr_prio, pmr;
614 int lr, grp;
615
616 grp = __vgic_v3_get_group(vcpu);
617
618 lr = __vgic_v3_highest_priority_lr(vcpu, vmcr, &lr_val);
619 if (lr < 0)
620 goto spurious;
621
622 if (grp != !!(lr_val & ICH_LR_GROUP))
623 goto spurious;
624
625 pmr = (vmcr & ICH_VMCR_PMR_MASK) >> ICH_VMCR_PMR_SHIFT;
626 lr_prio = (lr_val & ICH_LR_PRIORITY_MASK) >> ICH_LR_PRIORITY_SHIFT;
627 if (pmr <= lr_prio)
628 goto spurious;
629
630 if (__vgic_v3_get_highest_active_priority() <= __vgic_v3_pri_to_pre(lr_prio, vmcr, grp))
631 goto spurious;
632
633 lr_val &= ~ICH_LR_STATE;
634 /* No active state for LPIs */
635 if ((lr_val & ICH_LR_VIRTUAL_ID_MASK) <= VGIC_MAX_SPI)
636 lr_val |= ICH_LR_ACTIVE_BIT;
637 __gic_v3_set_lr(lr_val, lr);
638 __vgic_v3_set_active_priority(lr_prio, vmcr, grp);
639 vcpu_set_reg(vcpu, rt, lr_val & ICH_LR_VIRTUAL_ID_MASK);
640 return;
641
642spurious:
643 vcpu_set_reg(vcpu, rt, ICC_IAR1_EL1_SPURIOUS);
644}
645
Marc Zyngierb6f49032017-06-09 12:49:37 +0100646static void __hyp_text __vgic_v3_clear_active_lr(int lr, u64 lr_val)
647{
648 lr_val &= ~ICH_LR_ACTIVE_BIT;
649 if (lr_val & ICH_LR_HW) {
650 u32 pid;
651
652 pid = (lr_val & ICH_LR_PHYS_ID_MASK) >> ICH_LR_PHYS_ID_SHIFT;
653 gic_write_dir(pid);
654 }
655
656 __gic_v3_set_lr(lr_val, lr);
657}
658
659static void __hyp_text __vgic_v3_bump_eoicount(void)
660{
661 u32 hcr;
662
663 hcr = read_gicreg(ICH_HCR_EL2);
664 hcr += 1 << ICH_HCR_EOIcount_SHIFT;
665 write_gicreg(hcr, ICH_HCR_EL2);
666}
667
Marc Zyngier40228ba2017-06-09 12:49:49 +0100668static void __hyp_text __vgic_v3_write_dir(struct kvm_vcpu *vcpu,
669 u32 vmcr, int rt)
670{
671 u32 vid = vcpu_get_reg(vcpu, rt);
672 u64 lr_val;
673 int lr;
674
675 /* EOImode == 0, nothing to be done here */
676 if (!(vmcr & ICH_VMCR_EOIM_MASK))
677 return;
678
679 /* No deactivate to be performed on an LPI */
680 if (vid >= VGIC_MIN_LPI)
681 return;
682
683 lr = __vgic_v3_find_active_lr(vcpu, vid, &lr_val);
684 if (lr == -1) {
685 __vgic_v3_bump_eoicount();
686 return;
687 }
688
689 __vgic_v3_clear_active_lr(lr, lr_val);
690}
691
Marc Zyngierb6f49032017-06-09 12:49:37 +0100692static void __hyp_text __vgic_v3_write_eoir(struct kvm_vcpu *vcpu, u32 vmcr, int rt)
693{
694 u32 vid = vcpu_get_reg(vcpu, rt);
695 u64 lr_val;
696 u8 lr_prio, act_prio;
697 int lr, grp;
698
699 grp = __vgic_v3_get_group(vcpu);
700
701 /* Drop priority in any case */
702 act_prio = __vgic_v3_clear_highest_active_priority();
703
704 /* If EOIing an LPI, no deactivate to be performed */
705 if (vid >= VGIC_MIN_LPI)
706 return;
707
708 /* EOImode == 1, nothing to be done here */
709 if (vmcr & ICH_VMCR_EOIM_MASK)
710 return;
711
712 lr = __vgic_v3_find_active_lr(vcpu, vid, &lr_val);
713 if (lr == -1) {
714 __vgic_v3_bump_eoicount();
715 return;
716 }
717
718 lr_prio = (lr_val & ICH_LR_PRIORITY_MASK) >> ICH_LR_PRIORITY_SHIFT;
719
720 /* If priorities or group do not match, the guest has fscked-up. */
721 if (grp != !!(lr_val & ICH_LR_GROUP) ||
722 __vgic_v3_pri_to_pre(lr_prio, vmcr, grp) != act_prio)
723 return;
724
725 /* Let's now perform the deactivation */
726 __vgic_v3_clear_active_lr(lr, lr_val);
727}
728
Marc Zyngierfbc48a02017-06-09 12:49:43 +0100729static void __hyp_text __vgic_v3_read_igrpen0(struct kvm_vcpu *vcpu, u32 vmcr, int rt)
730{
731 vcpu_set_reg(vcpu, rt, !!(vmcr & ICH_VMCR_ENG0_MASK));
732}
733
Marc Zyngierf8b630b2017-06-09 12:49:35 +0100734static void __hyp_text __vgic_v3_read_igrpen1(struct kvm_vcpu *vcpu, u32 vmcr, int rt)
735{
736 vcpu_set_reg(vcpu, rt, !!(vmcr & ICH_VMCR_ENG1_MASK));
737}
738
Marc Zyngierfbc48a02017-06-09 12:49:43 +0100739static void __hyp_text __vgic_v3_write_igrpen0(struct kvm_vcpu *vcpu, u32 vmcr, int rt)
740{
741 u64 val = vcpu_get_reg(vcpu, rt);
742
743 if (val & 1)
744 vmcr |= ICH_VMCR_ENG0_MASK;
745 else
746 vmcr &= ~ICH_VMCR_ENG0_MASK;
747
748 __vgic_v3_write_vmcr(vmcr);
749}
750
Marc Zyngierf8b630b2017-06-09 12:49:35 +0100751static void __hyp_text __vgic_v3_write_igrpen1(struct kvm_vcpu *vcpu, u32 vmcr, int rt)
752{
753 u64 val = vcpu_get_reg(vcpu, rt);
754
755 if (val & 1)
756 vmcr |= ICH_VMCR_ENG1_MASK;
757 else
758 vmcr &= ~ICH_VMCR_ENG1_MASK;
759
760 __vgic_v3_write_vmcr(vmcr);
761}
762
Marc Zyngier423de852017-06-09 12:49:42 +0100763static void __hyp_text __vgic_v3_read_bpr0(struct kvm_vcpu *vcpu, u32 vmcr, int rt)
764{
765 vcpu_set_reg(vcpu, rt, __vgic_v3_get_bpr0(vmcr));
766}
767
Marc Zyngierd70c7b32017-06-09 12:49:34 +0100768static void __hyp_text __vgic_v3_read_bpr1(struct kvm_vcpu *vcpu, u32 vmcr, int rt)
769{
770 vcpu_set_reg(vcpu, rt, __vgic_v3_get_bpr1(vmcr));
771}
772
Marc Zyngier423de852017-06-09 12:49:42 +0100773static void __hyp_text __vgic_v3_write_bpr0(struct kvm_vcpu *vcpu, u32 vmcr, int rt)
774{
775 u64 val = vcpu_get_reg(vcpu, rt);
776 u8 bpr_min = __vgic_v3_bpr_min() - 1;
777
778 /* Enforce BPR limiting */
779 if (val < bpr_min)
780 val = bpr_min;
781
782 val <<= ICH_VMCR_BPR0_SHIFT;
783 val &= ICH_VMCR_BPR0_MASK;
784 vmcr &= ~ICH_VMCR_BPR0_MASK;
785 vmcr |= val;
786
787 __vgic_v3_write_vmcr(vmcr);
788}
789
Marc Zyngierd70c7b32017-06-09 12:49:34 +0100790static void __hyp_text __vgic_v3_write_bpr1(struct kvm_vcpu *vcpu, u32 vmcr, int rt)
791{
792 u64 val = vcpu_get_reg(vcpu, rt);
793 u8 bpr_min = __vgic_v3_bpr_min();
794
795 if (vmcr & ICH_VMCR_CBPR_MASK)
796 return;
797
798 /* Enforce BPR limiting */
799 if (val < bpr_min)
800 val = bpr_min;
801
802 val <<= ICH_VMCR_BPR1_SHIFT;
803 val &= ICH_VMCR_BPR1_MASK;
804 vmcr &= ~ICH_VMCR_BPR1_MASK;
805 vmcr |= val;
806
807 __vgic_v3_write_vmcr(vmcr);
808}
809
Marc Zyngierf9e74492017-06-09 12:49:38 +0100810static void __hyp_text __vgic_v3_read_apxrn(struct kvm_vcpu *vcpu, int rt, int n)
811{
812 u32 val;
813
814 if (!__vgic_v3_get_group(vcpu))
815 val = __vgic_v3_read_ap0rn(n);
816 else
817 val = __vgic_v3_read_ap1rn(n);
818
819 vcpu_set_reg(vcpu, rt, val);
820}
821
822static void __hyp_text __vgic_v3_write_apxrn(struct kvm_vcpu *vcpu, int rt, int n)
823{
824 u32 val = vcpu_get_reg(vcpu, rt);
825
826 if (!__vgic_v3_get_group(vcpu))
827 __vgic_v3_write_ap0rn(val, n);
828 else
829 __vgic_v3_write_ap1rn(val, n);
830}
831
832static void __hyp_text __vgic_v3_read_apxr0(struct kvm_vcpu *vcpu,
833 u32 vmcr, int rt)
834{
835 __vgic_v3_read_apxrn(vcpu, rt, 0);
836}
837
838static void __hyp_text __vgic_v3_read_apxr1(struct kvm_vcpu *vcpu,
839 u32 vmcr, int rt)
840{
841 __vgic_v3_read_apxrn(vcpu, rt, 1);
842}
843
844static void __hyp_text __vgic_v3_read_apxr2(struct kvm_vcpu *vcpu,
845 u32 vmcr, int rt)
846{
847 __vgic_v3_read_apxrn(vcpu, rt, 2);
848}
849
850static void __hyp_text __vgic_v3_read_apxr3(struct kvm_vcpu *vcpu,
851 u32 vmcr, int rt)
852{
853 __vgic_v3_read_apxrn(vcpu, rt, 3);
854}
855
856static void __hyp_text __vgic_v3_write_apxr0(struct kvm_vcpu *vcpu,
857 u32 vmcr, int rt)
858{
859 __vgic_v3_write_apxrn(vcpu, rt, 0);
860}
861
862static void __hyp_text __vgic_v3_write_apxr1(struct kvm_vcpu *vcpu,
863 u32 vmcr, int rt)
864{
865 __vgic_v3_write_apxrn(vcpu, rt, 1);
866}
867
868static void __hyp_text __vgic_v3_write_apxr2(struct kvm_vcpu *vcpu,
869 u32 vmcr, int rt)
870{
871 __vgic_v3_write_apxrn(vcpu, rt, 2);
872}
873
874static void __hyp_text __vgic_v3_write_apxr3(struct kvm_vcpu *vcpu,
875 u32 vmcr, int rt)
876{
877 __vgic_v3_write_apxrn(vcpu, rt, 3);
878}
879
Marc Zyngier2724c112017-06-09 12:49:39 +0100880static void __hyp_text __vgic_v3_read_hppir(struct kvm_vcpu *vcpu,
881 u32 vmcr, int rt)
882{
883 u64 lr_val;
884 int lr, lr_grp, grp;
885
886 grp = __vgic_v3_get_group(vcpu);
887
888 lr = __vgic_v3_highest_priority_lr(vcpu, vmcr, &lr_val);
889 if (lr == -1)
890 goto spurious;
891
892 lr_grp = !!(lr_val & ICH_LR_GROUP);
893 if (lr_grp != grp)
894 lr_val = ICC_IAR1_EL1_SPURIOUS;
895
896spurious:
897 vcpu_set_reg(vcpu, rt, lr_val & ICH_LR_VIRTUAL_ID_MASK);
898}
899
Marc Zyngier6293d652017-06-09 12:49:52 +0100900static void __hyp_text __vgic_v3_read_pmr(struct kvm_vcpu *vcpu,
901 u32 vmcr, int rt)
902{
903 vmcr &= ICH_VMCR_PMR_MASK;
904 vmcr >>= ICH_VMCR_PMR_SHIFT;
905 vcpu_set_reg(vcpu, rt, vmcr);
906}
907
908static void __hyp_text __vgic_v3_write_pmr(struct kvm_vcpu *vcpu,
909 u32 vmcr, int rt)
910{
911 u32 val = vcpu_get_reg(vcpu, rt);
912
913 val <<= ICH_VMCR_PMR_SHIFT;
914 val &= ICH_VMCR_PMR_MASK;
915 vmcr &= ~ICH_VMCR_PMR_MASK;
916 vmcr |= val;
917
918 write_gicreg(vmcr, ICH_VMCR_EL2);
919}
920
Marc Zyngier43515892017-06-09 12:49:50 +0100921static void __hyp_text __vgic_v3_read_rpr(struct kvm_vcpu *vcpu,
922 u32 vmcr, int rt)
923{
924 u32 val = __vgic_v3_get_highest_active_priority();
925 vcpu_set_reg(vcpu, rt, val);
926}
927
Marc Zyngierd840b2d2017-06-09 12:49:51 +0100928static void __hyp_text __vgic_v3_read_ctlr(struct kvm_vcpu *vcpu,
929 u32 vmcr, int rt)
930{
931 u32 vtr, val;
932
933 vtr = read_gicreg(ICH_VTR_EL2);
934 /* PRIbits */
935 val = ((vtr >> 29) & 7) << ICC_CTLR_EL1_PRI_BITS_SHIFT;
936 /* IDbits */
937 val |= ((vtr >> 23) & 7) << ICC_CTLR_EL1_ID_BITS_SHIFT;
938 /* SEIS */
939 val |= ((vtr >> 22) & 1) << ICC_CTLR_EL1_SEIS_SHIFT;
940 /* A3V */
941 val |= ((vtr >> 21) & 1) << ICC_CTLR_EL1_A3V_SHIFT;
942 /* EOImode */
943 val |= ((vmcr & ICH_VMCR_EOIM_MASK) >> ICH_VMCR_EOIM_SHIFT) << ICC_CTLR_EL1_EOImode_SHIFT;
944 /* CBPR */
945 val |= (vmcr & ICH_VMCR_CBPR_MASK) >> ICH_VMCR_CBPR_SHIFT;
946
947 vcpu_set_reg(vcpu, rt, val);
948}
949
950static void __hyp_text __vgic_v3_write_ctlr(struct kvm_vcpu *vcpu,
951 u32 vmcr, int rt)
952{
953 u32 val = vcpu_get_reg(vcpu, rt);
954
955 if (val & ICC_CTLR_EL1_CBPR_MASK)
956 vmcr |= ICH_VMCR_CBPR_MASK;
957 else
958 vmcr &= ~ICH_VMCR_CBPR_MASK;
959
960 if (val & ICC_CTLR_EL1_EOImode_MASK)
961 vmcr |= ICH_VMCR_EOIM_MASK;
962 else
963 vmcr &= ~ICH_VMCR_EOIM_MASK;
964
965 write_gicreg(vmcr, ICH_VMCR_EL2);
966}
967
Marc Zyngier59da1cb2017-06-09 12:49:33 +0100968int __hyp_text __vgic_v3_perform_cpuif_access(struct kvm_vcpu *vcpu)
969{
970 int rt;
971 u32 esr;
972 u32 vmcr;
973 void (*fn)(struct kvm_vcpu *, u32, int);
974 bool is_read;
975 u32 sysreg;
976
977 esr = kvm_vcpu_get_hsr(vcpu);
978 if (vcpu_mode_is_32bit(vcpu)) {
979 if (!kvm_condition_valid(vcpu))
980 return 1;
981
982 sysreg = esr_cp15_to_sysreg(esr);
983 } else {
984 sysreg = esr_sys64_to_sysreg(esr);
985 }
986
987 is_read = (esr & ESR_ELx_SYS64_ISS_DIR_MASK) == ESR_ELx_SYS64_ISS_DIR_READ;
988
989 switch (sysreg) {
Marc Zyngiereab0b2d2017-06-09 12:49:44 +0100990 case SYS_ICC_IAR0_EL1:
Marc Zyngier132a3242017-06-09 12:49:36 +0100991 case SYS_ICC_IAR1_EL1:
Marc Zyngier7b1dba12017-06-09 12:49:56 +0100992 if (unlikely(!is_read))
993 return 0;
Marc Zyngier132a3242017-06-09 12:49:36 +0100994 fn = __vgic_v3_read_iar;
995 break;
Marc Zyngiereab0b2d2017-06-09 12:49:44 +0100996 case SYS_ICC_EOIR0_EL1:
Marc Zyngierb6f49032017-06-09 12:49:37 +0100997 case SYS_ICC_EOIR1_EL1:
Marc Zyngiere7f1d1e2017-06-09 12:49:55 +0100998 if (unlikely(is_read))
999 return 0;
Marc Zyngierb6f49032017-06-09 12:49:37 +01001000 fn = __vgic_v3_write_eoir;
1001 break;
Mark Rutland21bc5282017-06-05 14:20:00 +01001002 case SYS_ICC_IGRPEN1_EL1:
Marc Zyngierf8b630b2017-06-09 12:49:35 +01001003 if (is_read)
1004 fn = __vgic_v3_read_igrpen1;
1005 else
1006 fn = __vgic_v3_write_igrpen1;
1007 break;
Marc Zyngierd70c7b32017-06-09 12:49:34 +01001008 case SYS_ICC_BPR1_EL1:
1009 if (is_read)
1010 fn = __vgic_v3_read_bpr1;
1011 else
1012 fn = __vgic_v3_write_bpr1;
1013 break;
Marc Zyngiereab0b2d2017-06-09 12:49:44 +01001014 case SYS_ICC_AP0Rn_EL1(0):
Marc Zyngierf9e74492017-06-09 12:49:38 +01001015 case SYS_ICC_AP1Rn_EL1(0):
1016 if (is_read)
1017 fn = __vgic_v3_read_apxr0;
1018 else
1019 fn = __vgic_v3_write_apxr0;
1020 break;
Marc Zyngiereab0b2d2017-06-09 12:49:44 +01001021 case SYS_ICC_AP0Rn_EL1(1):
Marc Zyngierf9e74492017-06-09 12:49:38 +01001022 case SYS_ICC_AP1Rn_EL1(1):
1023 if (is_read)
1024 fn = __vgic_v3_read_apxr1;
1025 else
1026 fn = __vgic_v3_write_apxr1;
1027 break;
Marc Zyngiereab0b2d2017-06-09 12:49:44 +01001028 case SYS_ICC_AP0Rn_EL1(2):
Marc Zyngierf9e74492017-06-09 12:49:38 +01001029 case SYS_ICC_AP1Rn_EL1(2):
1030 if (is_read)
1031 fn = __vgic_v3_read_apxr2;
1032 else
1033 fn = __vgic_v3_write_apxr2;
1034 break;
Marc Zyngiereab0b2d2017-06-09 12:49:44 +01001035 case SYS_ICC_AP0Rn_EL1(3):
Marc Zyngierf9e74492017-06-09 12:49:38 +01001036 case SYS_ICC_AP1Rn_EL1(3):
1037 if (is_read)
1038 fn = __vgic_v3_read_apxr3;
1039 else
1040 fn = __vgic_v3_write_apxr3;
1041 break;
Marc Zyngiereab0b2d2017-06-09 12:49:44 +01001042 case SYS_ICC_HPPIR0_EL1:
Marc Zyngier2724c112017-06-09 12:49:39 +01001043 case SYS_ICC_HPPIR1_EL1:
Marc Zyngier7b1dba12017-06-09 12:49:56 +01001044 if (unlikely(!is_read))
1045 return 0;
Marc Zyngier2724c112017-06-09 12:49:39 +01001046 fn = __vgic_v3_read_hppir;
1047 break;
Mark Rutland21bc5282017-06-05 14:20:00 +01001048 case SYS_ICC_IGRPEN0_EL1:
Marc Zyngierfbc48a02017-06-09 12:49:43 +01001049 if (is_read)
1050 fn = __vgic_v3_read_igrpen0;
1051 else
1052 fn = __vgic_v3_write_igrpen0;
1053 break;
Marc Zyngier423de852017-06-09 12:49:42 +01001054 case SYS_ICC_BPR0_EL1:
1055 if (is_read)
1056 fn = __vgic_v3_read_bpr0;
1057 else
1058 fn = __vgic_v3_write_bpr0;
1059 break;
Marc Zyngier40228ba2017-06-09 12:49:49 +01001060 case SYS_ICC_DIR_EL1:
Marc Zyngiere7f1d1e2017-06-09 12:49:55 +01001061 if (unlikely(is_read))
1062 return 0;
Marc Zyngier40228ba2017-06-09 12:49:49 +01001063 fn = __vgic_v3_write_dir;
1064 break;
Marc Zyngier43515892017-06-09 12:49:50 +01001065 case SYS_ICC_RPR_EL1:
Marc Zyngier7b1dba12017-06-09 12:49:56 +01001066 if (unlikely(!is_read))
1067 return 0;
Marc Zyngier43515892017-06-09 12:49:50 +01001068 fn = __vgic_v3_read_rpr;
1069 break;
Marc Zyngierd840b2d2017-06-09 12:49:51 +01001070 case SYS_ICC_CTLR_EL1:
1071 if (is_read)
1072 fn = __vgic_v3_read_ctlr;
1073 else
1074 fn = __vgic_v3_write_ctlr;
1075 break;
Marc Zyngier6293d652017-06-09 12:49:52 +01001076 case SYS_ICC_PMR_EL1:
1077 if (is_read)
1078 fn = __vgic_v3_read_pmr;
1079 else
1080 fn = __vgic_v3_write_pmr;
1081 break;
Marc Zyngier59da1cb2017-06-09 12:49:33 +01001082 default:
1083 return 0;
1084 }
1085
1086 vmcr = __vgic_v3_read_vmcr();
1087 rt = kvm_vcpu_sys_get_rt(vcpu);
1088 fn(vcpu, vmcr, rt);
1089
1090 return 1;
1091}
1092
1093#endif