Marc Zyngier | f68d2b1 | 2015-10-19 15:50:58 +0100 | [diff] [blame] | 1 | /* |
| 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 Zyngier | 13720a5 | 2016-01-28 13:44:07 +0000 | [diff] [blame] | 22 | #include <asm/kvm_hyp.h> |
Marc Zyngier | f68d2b1 | 2015-10-19 15:50:58 +0100 | [diff] [blame] | 23 | |
| 24 | #define vtr_to_max_lr_idx(v) ((v) & 0xf) |
| 25 | #define vtr_to_nr_pri_bits(v) (((u32)(v) >> 29) + 1) |
| 26 | |
Marc Zyngier | 1b8e83c | 2016-02-17 10:25:05 +0000 | [diff] [blame] | 27 | static u64 __hyp_text __gic_v3_get_lr(unsigned int lr) |
| 28 | { |
| 29 | switch (lr & 0xf) { |
| 30 | case 0: |
| 31 | return read_gicreg(ICH_LR0_EL2); |
| 32 | case 1: |
| 33 | return read_gicreg(ICH_LR1_EL2); |
| 34 | case 2: |
| 35 | return read_gicreg(ICH_LR2_EL2); |
| 36 | case 3: |
| 37 | return read_gicreg(ICH_LR3_EL2); |
| 38 | case 4: |
| 39 | return read_gicreg(ICH_LR4_EL2); |
| 40 | case 5: |
| 41 | return read_gicreg(ICH_LR5_EL2); |
| 42 | case 6: |
| 43 | return read_gicreg(ICH_LR6_EL2); |
| 44 | case 7: |
| 45 | return read_gicreg(ICH_LR7_EL2); |
| 46 | case 8: |
| 47 | return read_gicreg(ICH_LR8_EL2); |
| 48 | case 9: |
| 49 | return read_gicreg(ICH_LR9_EL2); |
| 50 | case 10: |
| 51 | return read_gicreg(ICH_LR10_EL2); |
| 52 | case 11: |
| 53 | return read_gicreg(ICH_LR11_EL2); |
| 54 | case 12: |
| 55 | return read_gicreg(ICH_LR12_EL2); |
| 56 | case 13: |
| 57 | return read_gicreg(ICH_LR13_EL2); |
| 58 | case 14: |
| 59 | return read_gicreg(ICH_LR14_EL2); |
| 60 | case 15: |
| 61 | return read_gicreg(ICH_LR15_EL2); |
| 62 | } |
| 63 | |
| 64 | unreachable(); |
| 65 | } |
| 66 | |
| 67 | static void __hyp_text __gic_v3_set_lr(u64 val, int lr) |
| 68 | { |
| 69 | switch (lr & 0xf) { |
| 70 | case 0: |
| 71 | write_gicreg(val, ICH_LR0_EL2); |
| 72 | break; |
| 73 | case 1: |
| 74 | write_gicreg(val, ICH_LR1_EL2); |
| 75 | break; |
| 76 | case 2: |
| 77 | write_gicreg(val, ICH_LR2_EL2); |
| 78 | break; |
| 79 | case 3: |
| 80 | write_gicreg(val, ICH_LR3_EL2); |
| 81 | break; |
| 82 | case 4: |
| 83 | write_gicreg(val, ICH_LR4_EL2); |
| 84 | break; |
| 85 | case 5: |
| 86 | write_gicreg(val, ICH_LR5_EL2); |
| 87 | break; |
| 88 | case 6: |
| 89 | write_gicreg(val, ICH_LR6_EL2); |
| 90 | break; |
| 91 | case 7: |
| 92 | write_gicreg(val, ICH_LR7_EL2); |
| 93 | break; |
| 94 | case 8: |
| 95 | write_gicreg(val, ICH_LR8_EL2); |
| 96 | break; |
| 97 | case 9: |
| 98 | write_gicreg(val, ICH_LR9_EL2); |
| 99 | break; |
| 100 | case 10: |
| 101 | write_gicreg(val, ICH_LR10_EL2); |
| 102 | break; |
| 103 | case 11: |
| 104 | write_gicreg(val, ICH_LR11_EL2); |
| 105 | break; |
| 106 | case 12: |
| 107 | write_gicreg(val, ICH_LR12_EL2); |
| 108 | break; |
| 109 | case 13: |
| 110 | write_gicreg(val, ICH_LR13_EL2); |
| 111 | break; |
| 112 | case 14: |
| 113 | write_gicreg(val, ICH_LR14_EL2); |
| 114 | break; |
| 115 | case 15: |
| 116 | write_gicreg(val, ICH_LR15_EL2); |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | |
Christoffer Dall | 00dafa0 | 2016-12-23 00:04:59 +0100 | [diff] [blame^] | 121 | static void __hyp_text save_maint_int_state(struct kvm_vcpu *vcpu) |
Marc Zyngier | b434454 | 2016-02-09 18:53:04 +0000 | [diff] [blame] | 122 | { |
| 123 | struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3; |
| 124 | int i; |
| 125 | bool expect_mi; |
Christoffer Dall | 00dafa0 | 2016-12-23 00:04:59 +0100 | [diff] [blame^] | 126 | u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs; |
Marc Zyngier | b434454 | 2016-02-09 18:53:04 +0000 | [diff] [blame] | 127 | |
| 128 | expect_mi = !!(cpu_if->vgic_hcr & ICH_HCR_UIE); |
| 129 | |
Christoffer Dall | 00dafa0 | 2016-12-23 00:04:59 +0100 | [diff] [blame^] | 130 | for (i = 0; i < used_lrs; i++) { |
Marc Zyngier | b434454 | 2016-02-09 18:53:04 +0000 | [diff] [blame] | 131 | expect_mi |= (!(cpu_if->vgic_lr[i] & ICH_LR_HW) && |
| 132 | (cpu_if->vgic_lr[i] & ICH_LR_EOI)); |
| 133 | } |
| 134 | |
| 135 | if (expect_mi) { |
| 136 | cpu_if->vgic_misr = read_gicreg(ICH_MISR_EL2); |
| 137 | |
| 138 | if (cpu_if->vgic_misr & ICH_MISR_EOI) |
| 139 | cpu_if->vgic_eisr = read_gicreg(ICH_EISR_EL2); |
| 140 | else |
| 141 | cpu_if->vgic_eisr = 0; |
| 142 | } else { |
| 143 | cpu_if->vgic_misr = 0; |
| 144 | cpu_if->vgic_eisr = 0; |
| 145 | } |
| 146 | } |
| 147 | |
Marc Zyngier | f68d2b1 | 2015-10-19 15:50:58 +0100 | [diff] [blame] | 148 | void __hyp_text __vgic_v3_save_state(struct kvm_vcpu *vcpu) |
| 149 | { |
| 150 | struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3; |
Christoffer Dall | 00dafa0 | 2016-12-23 00:04:59 +0100 | [diff] [blame^] | 151 | u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs; |
Marc Zyngier | f68d2b1 | 2015-10-19 15:50:58 +0100 | [diff] [blame] | 152 | u64 val; |
Marc Zyngier | f68d2b1 | 2015-10-19 15:50:58 +0100 | [diff] [blame] | 153 | |
| 154 | /* |
| 155 | * Make sure stores to the GIC via the memory mapped interface |
| 156 | * are now visible to the system register interface. |
| 157 | */ |
Marc Zyngier | c585132 | 2016-05-25 15:26:39 +0100 | [diff] [blame] | 158 | if (!cpu_if->vgic_sre) |
| 159 | dsb(st); |
Marc Zyngier | f68d2b1 | 2015-10-19 15:50:58 +0100 | [diff] [blame] | 160 | |
Christoffer Dall | 00dafa0 | 2016-12-23 00:04:59 +0100 | [diff] [blame^] | 161 | if (used_lrs) { |
Marc Zyngier | 1b8e83c | 2016-02-17 10:25:05 +0000 | [diff] [blame] | 162 | int i; |
Christoffer Dall | 00dafa0 | 2016-12-23 00:04:59 +0100 | [diff] [blame^] | 163 | u32 nr_pri_bits; |
Marc Zyngier | f68d2b1 | 2015-10-19 15:50:58 +0100 | [diff] [blame] | 164 | |
Marc Zyngier | 1b8e83c | 2016-02-17 10:25:05 +0000 | [diff] [blame] | 165 | cpu_if->vgic_elrsr = read_gicreg(ICH_ELSR_EL2); |
Marc Zyngier | f68d2b1 | 2015-10-19 15:50:58 +0100 | [diff] [blame] | 166 | |
Marc Zyngier | 1b8e83c | 2016-02-17 10:25:05 +0000 | [diff] [blame] | 167 | write_gicreg(0, ICH_HCR_EL2); |
| 168 | val = read_gicreg(ICH_VTR_EL2); |
Marc Zyngier | 1b8e83c | 2016-02-17 10:25:05 +0000 | [diff] [blame] | 169 | nr_pri_bits = vtr_to_nr_pri_bits(val); |
Marc Zyngier | f68d2b1 | 2015-10-19 15:50:58 +0100 | [diff] [blame] | 170 | |
Christoffer Dall | 00dafa0 | 2016-12-23 00:04:59 +0100 | [diff] [blame^] | 171 | save_maint_int_state(vcpu); |
Marc Zyngier | b434454 | 2016-02-09 18:53:04 +0000 | [diff] [blame] | 172 | |
Christoffer Dall | 00dafa0 | 2016-12-23 00:04:59 +0100 | [diff] [blame^] | 173 | for (i = 0; i <= used_lrs; i++) { |
Christoffer Dall | fa89c77 | 2016-05-25 15:26:34 +0100 | [diff] [blame] | 174 | if (cpu_if->vgic_elrsr & (1 << i)) |
Marc Zyngier | 84e8b9c | 2016-02-09 17:09:49 +0000 | [diff] [blame] | 175 | cpu_if->vgic_lr[i] &= ~ICH_LR_STATE; |
Christoffer Dall | fa89c77 | 2016-05-25 15:26:34 +0100 | [diff] [blame] | 176 | else |
| 177 | cpu_if->vgic_lr[i] = __gic_v3_get_lr(i); |
Marc Zyngier | 84e8b9c | 2016-02-09 17:09:49 +0000 | [diff] [blame] | 178 | |
Marc Zyngier | b40c489 | 2016-02-09 17:36:09 +0000 | [diff] [blame] | 179 | __gic_v3_set_lr(0, i); |
Marc Zyngier | 1b8e83c | 2016-02-17 10:25:05 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | switch (nr_pri_bits) { |
| 183 | case 7: |
| 184 | cpu_if->vgic_ap0r[3] = read_gicreg(ICH_AP0R3_EL2); |
| 185 | cpu_if->vgic_ap0r[2] = read_gicreg(ICH_AP0R2_EL2); |
| 186 | case 6: |
| 187 | cpu_if->vgic_ap0r[1] = read_gicreg(ICH_AP0R1_EL2); |
| 188 | default: |
| 189 | cpu_if->vgic_ap0r[0] = read_gicreg(ICH_AP0R0_EL2); |
| 190 | } |
| 191 | |
| 192 | switch (nr_pri_bits) { |
| 193 | case 7: |
| 194 | cpu_if->vgic_ap1r[3] = read_gicreg(ICH_AP1R3_EL2); |
| 195 | cpu_if->vgic_ap1r[2] = read_gicreg(ICH_AP1R2_EL2); |
| 196 | case 6: |
| 197 | cpu_if->vgic_ap1r[1] = read_gicreg(ICH_AP1R1_EL2); |
| 198 | default: |
| 199 | cpu_if->vgic_ap1r[0] = read_gicreg(ICH_AP1R0_EL2); |
| 200 | } |
Marc Zyngier | 1b8e83c | 2016-02-17 10:25:05 +0000 | [diff] [blame] | 201 | } else { |
| 202 | cpu_if->vgic_misr = 0; |
| 203 | cpu_if->vgic_eisr = 0; |
| 204 | cpu_if->vgic_elrsr = 0xffff; |
| 205 | cpu_if->vgic_ap0r[0] = 0; |
| 206 | cpu_if->vgic_ap0r[1] = 0; |
| 207 | cpu_if->vgic_ap0r[2] = 0; |
| 208 | cpu_if->vgic_ap0r[3] = 0; |
| 209 | cpu_if->vgic_ap1r[0] = 0; |
| 210 | cpu_if->vgic_ap1r[1] = 0; |
| 211 | cpu_if->vgic_ap1r[2] = 0; |
| 212 | cpu_if->vgic_ap1r[3] = 0; |
Marc Zyngier | f68d2b1 | 2015-10-19 15:50:58 +0100 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | val = read_gicreg(ICC_SRE_EL2); |
| 216 | write_gicreg(val | ICC_SRE_EL2_ENABLE, ICC_SRE_EL2); |
Marc Zyngier | c585132 | 2016-05-25 15:26:39 +0100 | [diff] [blame] | 217 | |
| 218 | if (!cpu_if->vgic_sre) { |
| 219 | /* Make sure ENABLE is set at EL2 before setting SRE at EL1 */ |
| 220 | isb(); |
| 221 | write_gicreg(1, ICC_SRE_EL1); |
| 222 | } |
Marc Zyngier | f68d2b1 | 2015-10-19 15:50:58 +0100 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void __hyp_text __vgic_v3_restore_state(struct kvm_vcpu *vcpu) |
| 226 | { |
| 227 | struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3; |
Christoffer Dall | 00dafa0 | 2016-12-23 00:04:59 +0100 | [diff] [blame^] | 228 | u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs; |
Marc Zyngier | f68d2b1 | 2015-10-19 15:50:58 +0100 | [diff] [blame] | 229 | u64 val; |
Christoffer Dall | 00dafa0 | 2016-12-23 00:04:59 +0100 | [diff] [blame^] | 230 | u32 nr_pri_bits; |
Marc Zyngier | 1b8e83c | 2016-02-17 10:25:05 +0000 | [diff] [blame] | 231 | int i; |
Marc Zyngier | f68d2b1 | 2015-10-19 15:50:58 +0100 | [diff] [blame] | 232 | |
| 233 | /* |
| 234 | * VFIQEn is RES1 if ICC_SRE_EL1.SRE is 1. This causes a |
| 235 | * Group0 interrupt (as generated in GICv2 mode) to be |
| 236 | * delivered as a FIQ to the guest, with potentially fatal |
| 237 | * consequences. So we must make sure that ICC_SRE_EL1 has |
| 238 | * been actually programmed with the value we want before |
| 239 | * starting to mess with the rest of the GIC. |
| 240 | */ |
Marc Zyngier | c585132 | 2016-05-25 15:26:39 +0100 | [diff] [blame] | 241 | if (!cpu_if->vgic_sre) { |
| 242 | write_gicreg(0, ICC_SRE_EL1); |
| 243 | isb(); |
| 244 | } |
Marc Zyngier | f68d2b1 | 2015-10-19 15:50:58 +0100 | [diff] [blame] | 245 | |
Marc Zyngier | f68d2b1 | 2015-10-19 15:50:58 +0100 | [diff] [blame] | 246 | val = read_gicreg(ICH_VTR_EL2); |
Marc Zyngier | f68d2b1 | 2015-10-19 15:50:58 +0100 | [diff] [blame] | 247 | nr_pri_bits = vtr_to_nr_pri_bits(val); |
| 248 | |
Christoffer Dall | 00dafa0 | 2016-12-23 00:04:59 +0100 | [diff] [blame^] | 249 | if (used_lrs) { |
Marc Zyngier | 1b8e83c | 2016-02-17 10:25:05 +0000 | [diff] [blame] | 250 | write_gicreg(cpu_if->vgic_hcr, ICH_HCR_EL2); |
| 251 | |
| 252 | switch (nr_pri_bits) { |
| 253 | case 7: |
| 254 | write_gicreg(cpu_if->vgic_ap0r[3], ICH_AP0R3_EL2); |
| 255 | write_gicreg(cpu_if->vgic_ap0r[2], ICH_AP0R2_EL2); |
| 256 | case 6: |
| 257 | write_gicreg(cpu_if->vgic_ap0r[1], ICH_AP0R1_EL2); |
| 258 | default: |
| 259 | write_gicreg(cpu_if->vgic_ap0r[0], ICH_AP0R0_EL2); |
| 260 | } |
| 261 | |
| 262 | switch (nr_pri_bits) { |
| 263 | case 7: |
| 264 | write_gicreg(cpu_if->vgic_ap1r[3], ICH_AP1R3_EL2); |
| 265 | write_gicreg(cpu_if->vgic_ap1r[2], ICH_AP1R2_EL2); |
| 266 | case 6: |
| 267 | write_gicreg(cpu_if->vgic_ap1r[1], ICH_AP1R1_EL2); |
| 268 | default: |
| 269 | write_gicreg(cpu_if->vgic_ap1r[0], ICH_AP1R0_EL2); |
| 270 | } |
| 271 | |
Christoffer Dall | 00dafa0 | 2016-12-23 00:04:59 +0100 | [diff] [blame^] | 272 | for (i = 0; i < used_lrs; i++) |
Marc Zyngier | b40c489 | 2016-02-09 17:36:09 +0000 | [diff] [blame] | 273 | __gic_v3_set_lr(cpu_if->vgic_lr[i], i); |
Marc Zyngier | f68d2b1 | 2015-10-19 15:50:58 +0100 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /* |
| 277 | * Ensures that the above will have reached the |
| 278 | * (re)distributors. This ensure the guest will read the |
| 279 | * correct values from the memory-mapped interface. |
| 280 | */ |
Marc Zyngier | c585132 | 2016-05-25 15:26:39 +0100 | [diff] [blame] | 281 | if (!cpu_if->vgic_sre) { |
| 282 | isb(); |
| 283 | dsb(sy); |
| 284 | } |
Marc Zyngier | f68d2b1 | 2015-10-19 15:50:58 +0100 | [diff] [blame] | 285 | |
| 286 | /* |
| 287 | * Prevent the guest from touching the GIC system registers if |
| 288 | * SRE isn't enabled for GICv3 emulation. |
| 289 | */ |
Marc Zyngier | a057001 | 2016-05-25 15:26:38 +0100 | [diff] [blame] | 290 | write_gicreg(read_gicreg(ICC_SRE_EL2) & ~ICC_SRE_EL2_ENABLE, |
| 291 | ICC_SRE_EL2); |
Marc Zyngier | f68d2b1 | 2015-10-19 15:50:58 +0100 | [diff] [blame] | 292 | } |
| 293 | |
Marc Zyngier | 0d98d00 | 2016-03-03 15:43:58 +0000 | [diff] [blame] | 294 | void __hyp_text __vgic_v3_init_lrs(void) |
| 295 | { |
| 296 | int max_lr_idx = vtr_to_max_lr_idx(read_gicreg(ICH_VTR_EL2)); |
| 297 | int i; |
| 298 | |
| 299 | for (i = 0; i <= max_lr_idx; i++) |
| 300 | __gic_v3_set_lr(0, i); |
| 301 | } |
| 302 | |
Christoffer Dall | cf0ba18 | 2016-09-01 13:16:03 +0200 | [diff] [blame] | 303 | u64 __hyp_text __vgic_v3_get_ich_vtr_el2(void) |
Marc Zyngier | f68d2b1 | 2015-10-19 15:50:58 +0100 | [diff] [blame] | 304 | { |
| 305 | return read_gicreg(ICH_VTR_EL2); |
| 306 | } |
Christoffer Dall | 328e566 | 2016-03-24 11:21:04 +0100 | [diff] [blame] | 307 | |
| 308 | u64 __hyp_text __vgic_v3_read_vmcr(void) |
| 309 | { |
| 310 | return read_gicreg(ICH_VMCR_EL2); |
| 311 | } |
| 312 | |
| 313 | void __hyp_text __vgic_v3_write_vmcr(u32 vmcr) |
| 314 | { |
| 315 | write_gicreg(vmcr, ICH_VMCR_EL2); |
| 316 | } |