Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Marc Zyngier | 140b086 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015, 2016 ARM Ltd. |
Marc Zyngier | 140b086 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/irqchip/arm-gic.h> |
| 7 | #include <linux/kvm.h> |
| 8 | #include <linux/kvm_host.h> |
Eric Auger | 9097773 | 2015-12-01 15:02:35 +0100 | [diff] [blame] | 9 | #include <kvm/arm_vgic.h> |
| 10 | #include <asm/kvm_mmu.h> |
Marc Zyngier | 140b086 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 11 | |
| 12 | #include "vgic.h" |
| 13 | |
Christoffer Dall | 5b0d2cc | 2017-03-18 13:56:56 +0100 | [diff] [blame] | 14 | static inline void vgic_v2_write_lr(int lr, u32 val) |
| 15 | { |
| 16 | void __iomem *base = kvm_vgic_global_state.vctrl_base; |
| 17 | |
| 18 | writel_relaxed(val, base + GICH_LR0 + (lr * 4)); |
| 19 | } |
| 20 | |
| 21 | void vgic_v2_init_lrs(void) |
| 22 | { |
| 23 | int i; |
| 24 | |
| 25 | for (i = 0; i < kvm_vgic_global_state.nr_lr; i++) |
| 26 | vgic_v2_write_lr(i, 0); |
| 27 | } |
| 28 | |
Marc Zyngier | 140b086 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 29 | void vgic_v2_set_underflow(struct kvm_vcpu *vcpu) |
| 30 | { |
| 31 | struct vgic_v2_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v2; |
| 32 | |
| 33 | cpuif->vgic_hcr |= GICH_HCR_UIE; |
| 34 | } |
| 35 | |
Christoffer Dall | af06149 | 2016-12-29 15:44:27 +0100 | [diff] [blame] | 36 | static bool lr_signals_eoi_mi(u32 lr_val) |
| 37 | { |
| 38 | return !(lr_val & GICH_LR_STATE) && (lr_val & GICH_LR_EOI) && |
| 39 | !(lr_val & GICH_LR_HW); |
| 40 | } |
| 41 | |
Marc Zyngier | 140b086 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 42 | /* |
| 43 | * transfer the content of the LRs back into the corresponding ap_list: |
| 44 | * - active bit is transferred as is |
| 45 | * - pending bit is |
| 46 | * - transferred as is in case of edge sensitive IRQs |
| 47 | * - set to the line-level (resample time) for level sensitive IRQs |
| 48 | */ |
| 49 | void vgic_v2_fold_lr_state(struct kvm_vcpu *vcpu) |
| 50 | { |
Christoffer Dall | 8ac76ef | 2017-03-18 13:48:42 +0100 | [diff] [blame] | 51 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; |
| 52 | struct vgic_v2_cpu_if *cpuif = &vgic_cpu->vgic_v2; |
Marc Zyngier | 140b086 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 53 | int lr; |
Jia He | d0823cb | 2018-08-03 21:57:04 +0800 | [diff] [blame] | 54 | |
| 55 | DEBUG_SPINLOCK_BUG_ON(!irqs_disabled()); |
Marc Zyngier | 140b086 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 56 | |
Marc Zyngier | 5369290 | 2018-04-18 10:39:04 +0100 | [diff] [blame] | 57 | cpuif->vgic_hcr &= ~GICH_HCR_UIE; |
Christoffer Dall | af06149 | 2016-12-29 15:44:27 +0100 | [diff] [blame] | 58 | |
Christoffer Dall | 8ac76ef | 2017-03-18 13:48:42 +0100 | [diff] [blame] | 59 | for (lr = 0; lr < vgic_cpu->used_lrs; lr++) { |
Marc Zyngier | 140b086 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 60 | u32 val = cpuif->vgic_lr[lr]; |
Marc Zyngier | 5369290 | 2018-04-18 10:39:04 +0100 | [diff] [blame] | 61 | u32 cpuid, intid = val & GICH_LR_VIRTUALID; |
Marc Zyngier | 140b086 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 62 | struct vgic_irq *irq; |
| 63 | |
Marc Zyngier | 5369290 | 2018-04-18 10:39:04 +0100 | [diff] [blame] | 64 | /* Extract the source vCPU id from the LR */ |
| 65 | cpuid = val & GICH_LR_PHYSID_CPUID; |
| 66 | cpuid >>= GICH_LR_PHYSID_CPUID_SHIFT; |
| 67 | cpuid &= 7; |
| 68 | |
Christoffer Dall | af06149 | 2016-12-29 15:44:27 +0100 | [diff] [blame] | 69 | /* Notify fds when the guest EOI'ed a level-triggered SPI */ |
| 70 | if (lr_signals_eoi_mi(val) && vgic_valid_spi(vcpu->kvm, intid)) |
| 71 | kvm_notify_acked_irq(vcpu->kvm, 0, |
| 72 | intid - VGIC_NR_PRIVATE_IRQS); |
| 73 | |
Marc Zyngier | 140b086 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 74 | irq = vgic_get_irq(vcpu->kvm, vcpu, intid); |
| 75 | |
Julien Thierry | 8fa3adb | 2019-01-07 15:06:15 +0000 | [diff] [blame] | 76 | raw_spin_lock(&irq->irq_lock); |
Marc Zyngier | 140b086 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 77 | |
| 78 | /* Always preserve the active bit */ |
| 79 | irq->active = !!(val & GICH_LR_ACTIVE_BIT); |
| 80 | |
Marc Zyngier | 5369290 | 2018-04-18 10:39:04 +0100 | [diff] [blame] | 81 | if (irq->active && vgic_irq_is_sgi(intid)) |
| 82 | irq->active_source = cpuid; |
| 83 | |
Marc Zyngier | 140b086 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 84 | /* Edge is the only case where we preserve the pending bit */ |
| 85 | if (irq->config == VGIC_CONFIG_EDGE && |
| 86 | (val & GICH_LR_PENDING_BIT)) { |
Christoffer Dall | 8694e4d | 2017-01-23 14:07:18 +0100 | [diff] [blame] | 87 | irq->pending_latch = true; |
Marc Zyngier | 140b086 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 88 | |
Marc Zyngier | 5369290 | 2018-04-18 10:39:04 +0100 | [diff] [blame] | 89 | if (vgic_irq_is_sgi(intid)) |
Marc Zyngier | 140b086 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 90 | irq->source |= (1 << cpuid); |
Marc Zyngier | 140b086 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Marc Zyngier | df7942d | 2016-05-25 15:26:35 +0100 | [diff] [blame] | 93 | /* |
| 94 | * Clear soft pending state when level irqs have been acked. |
Marc Zyngier | df7942d | 2016-05-25 15:26:35 +0100 | [diff] [blame] | 95 | */ |
Marc Zyngier | 67b5b67 | 2018-03-09 14:59:40 +0000 | [diff] [blame] | 96 | if (irq->config == VGIC_CONFIG_LEVEL && !(val & GICH_LR_STATE)) |
| 97 | irq->pending_latch = false; |
Marc Zyngier | 140b086 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 98 | |
Christoffer Dall | e40cc57 | 2017-08-29 10:40:44 +0200 | [diff] [blame] | 99 | /* |
| 100 | * Level-triggered mapped IRQs are special because we only |
| 101 | * observe rising edges as input to the VGIC. |
| 102 | * |
| 103 | * If the guest never acked the interrupt we have to sample |
| 104 | * the physical line and set the line level, because the |
| 105 | * device state could have changed or we simply need to |
| 106 | * process the still pending interrupt later. |
| 107 | * |
| 108 | * If this causes us to lower the level, we have to also clear |
| 109 | * the physical active state, since we will otherwise never be |
| 110 | * told when the interrupt becomes asserted again. |
| 111 | */ |
| 112 | if (vgic_irq_is_mapped_level(irq) && (val & GICH_LR_PENDING_BIT)) { |
| 113 | irq->line_level = vgic_get_phys_line_level(irq); |
| 114 | |
| 115 | if (!irq->line_level) |
| 116 | vgic_irq_set_phys_active(irq, false); |
| 117 | } |
| 118 | |
Julien Thierry | 8fa3adb | 2019-01-07 15:06:15 +0000 | [diff] [blame] | 119 | raw_spin_unlock(&irq->irq_lock); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 120 | vgic_put_irq(vcpu->kvm, irq); |
Marc Zyngier | 140b086 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 121 | } |
Christoffer Dall | 8ac76ef | 2017-03-18 13:48:42 +0100 | [diff] [blame] | 122 | |
| 123 | vgic_cpu->used_lrs = 0; |
Marc Zyngier | 140b086 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 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 | */ |
| 137 | void vgic_v2_populate_lr(struct kvm_vcpu *vcpu, struct vgic_irq *irq, int lr) |
| 138 | { |
| 139 | u32 val = irq->intid; |
Marc Zyngier | 67b5b67 | 2018-03-09 14:59:40 +0000 | [diff] [blame] | 140 | bool allow_pending = true; |
Marc Zyngier | 140b086 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 141 | |
Marc Zyngier | 5369290 | 2018-04-18 10:39:04 +0100 | [diff] [blame] | 142 | if (irq->active) { |
Marc Zyngier | 67b5b67 | 2018-03-09 14:59:40 +0000 | [diff] [blame] | 143 | val |= GICH_LR_ACTIVE_BIT; |
Marc Zyngier | 5369290 | 2018-04-18 10:39:04 +0100 | [diff] [blame] | 144 | if (vgic_irq_is_sgi(irq->intid)) |
| 145 | val |= irq->active_source << GICH_LR_PHYSID_CPUID_SHIFT; |
| 146 | if (vgic_irq_is_multi_sgi(irq)) { |
| 147 | allow_pending = false; |
| 148 | val |= GICH_LR_EOI; |
| 149 | } |
| 150 | } |
Marc Zyngier | 67b5b67 | 2018-03-09 14:59:40 +0000 | [diff] [blame] | 151 | |
Christoffer Dall | 8732209 | 2018-07-16 15:06:22 +0200 | [diff] [blame] | 152 | if (irq->group) |
| 153 | val |= GICH_LR_GROUP1; |
| 154 | |
Marc Zyngier | 67b5b67 | 2018-03-09 14:59:40 +0000 | [diff] [blame] | 155 | if (irq->hw) { |
| 156 | val |= GICH_LR_HW; |
| 157 | val |= irq->hwintid << GICH_LR_PHYSID_CPUID_SHIFT; |
| 158 | /* |
| 159 | * Never set pending+active on a HW interrupt, as the |
| 160 | * pending state is kept at the physical distributor |
| 161 | * level. |
| 162 | */ |
| 163 | if (irq->active) |
| 164 | allow_pending = false; |
| 165 | } else { |
| 166 | if (irq->config == VGIC_CONFIG_LEVEL) { |
| 167 | val |= GICH_LR_EOI; |
| 168 | |
| 169 | /* |
| 170 | * Software resampling doesn't work very well |
| 171 | * if we allow P+A, so let's not do that. |
| 172 | */ |
| 173 | if (irq->active) |
| 174 | allow_pending = false; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | if (allow_pending && irq_is_pending(irq)) { |
Marc Zyngier | 140b086 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 179 | val |= GICH_LR_PENDING_BIT; |
| 180 | |
| 181 | if (irq->config == VGIC_CONFIG_EDGE) |
Christoffer Dall | 8694e4d | 2017-01-23 14:07:18 +0100 | [diff] [blame] | 182 | irq->pending_latch = false; |
Marc Zyngier | 140b086 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 183 | |
| 184 | if (vgic_irq_is_sgi(irq->intid)) { |
| 185 | u32 src = ffs(irq->source); |
| 186 | |
| 187 | BUG_ON(!src); |
| 188 | val |= (src - 1) << GICH_LR_PHYSID_CPUID_SHIFT; |
| 189 | irq->source &= ~(1 << (src - 1)); |
Marc Zyngier | 5369290 | 2018-04-18 10:39:04 +0100 | [diff] [blame] | 190 | if (irq->source) { |
Christoffer Dall | 8694e4d | 2017-01-23 14:07:18 +0100 | [diff] [blame] | 191 | irq->pending_latch = true; |
Marc Zyngier | 5369290 | 2018-04-18 10:39:04 +0100 | [diff] [blame] | 192 | val |= GICH_LR_EOI; |
| 193 | } |
Marc Zyngier | 140b086 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Christoffer Dall | e40cc57 | 2017-08-29 10:40:44 +0200 | [diff] [blame] | 197 | /* |
| 198 | * Level-triggered mapped IRQs are special because we only observe |
| 199 | * rising edges as input to the VGIC. We therefore lower the line |
| 200 | * level here, so that we can take new virtual IRQs. See |
| 201 | * vgic_v2_fold_lr_state for more info. |
| 202 | */ |
| 203 | if (vgic_irq_is_mapped_level(irq) && (val & GICH_LR_PENDING_BIT)) |
| 204 | irq->line_level = false; |
| 205 | |
Marc Zyngier | 140b086 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 206 | /* The GICv2 LR only holds five bits of priority. */ |
| 207 | val |= (irq->priority >> 3) << GICH_LR_PRIORITY_SHIFT; |
| 208 | |
| 209 | vcpu->arch.vgic_cpu.vgic_v2.vgic_lr[lr] = val; |
| 210 | } |
| 211 | |
| 212 | void vgic_v2_clear_lr(struct kvm_vcpu *vcpu, int lr) |
| 213 | { |
| 214 | vcpu->arch.vgic_cpu.vgic_v2.vgic_lr[lr] = 0; |
| 215 | } |
Andre Przywara | e4823a7 | 2015-12-03 11:47:37 +0000 | [diff] [blame] | 216 | |
| 217 | void vgic_v2_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp) |
| 218 | { |
Christoffer Dall | 328e5664 | 2016-03-24 11:21:04 +0100 | [diff] [blame] | 219 | struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2; |
Andre Przywara | e4823a7 | 2015-12-03 11:47:37 +0000 | [diff] [blame] | 220 | u32 vmcr; |
| 221 | |
Christoffer Dall | 28232a4 | 2017-05-20 14:12:34 +0200 | [diff] [blame] | 222 | vmcr = (vmcrp->grpen0 << GICH_VMCR_ENABLE_GRP0_SHIFT) & |
| 223 | GICH_VMCR_ENABLE_GRP0_MASK; |
| 224 | vmcr |= (vmcrp->grpen1 << GICH_VMCR_ENABLE_GRP1_SHIFT) & |
| 225 | GICH_VMCR_ENABLE_GRP1_MASK; |
| 226 | vmcr |= (vmcrp->ackctl << GICH_VMCR_ACK_CTL_SHIFT) & |
| 227 | GICH_VMCR_ACK_CTL_MASK; |
| 228 | vmcr |= (vmcrp->fiqen << GICH_VMCR_FIQ_EN_SHIFT) & |
| 229 | GICH_VMCR_FIQ_EN_MASK; |
| 230 | vmcr |= (vmcrp->cbpr << GICH_VMCR_CBPR_SHIFT) & |
| 231 | GICH_VMCR_CBPR_MASK; |
| 232 | vmcr |= (vmcrp->eoim << GICH_VMCR_EOI_MODE_SHIFT) & |
| 233 | GICH_VMCR_EOI_MODE_MASK; |
Andre Przywara | e4823a7 | 2015-12-03 11:47:37 +0000 | [diff] [blame] | 234 | vmcr |= (vmcrp->abpr << GICH_VMCR_ALIAS_BINPOINT_SHIFT) & |
| 235 | GICH_VMCR_ALIAS_BINPOINT_MASK; |
| 236 | vmcr |= (vmcrp->bpr << GICH_VMCR_BINPOINT_SHIFT) & |
| 237 | GICH_VMCR_BINPOINT_MASK; |
Christoffer Dall | 6d56111 | 2017-03-21 22:05:22 +0100 | [diff] [blame] | 238 | vmcr |= ((vmcrp->pmr >> GICV_PMR_PRIORITY_SHIFT) << |
| 239 | GICH_VMCR_PRIMASK_SHIFT) & GICH_VMCR_PRIMASK_MASK; |
Andre Przywara | e4823a7 | 2015-12-03 11:47:37 +0000 | [diff] [blame] | 240 | |
Christoffer Dall | 328e5664 | 2016-03-24 11:21:04 +0100 | [diff] [blame] | 241 | cpu_if->vgic_vmcr = vmcr; |
Andre Przywara | e4823a7 | 2015-12-03 11:47:37 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | void vgic_v2_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp) |
| 245 | { |
Christoffer Dall | 328e5664 | 2016-03-24 11:21:04 +0100 | [diff] [blame] | 246 | struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2; |
| 247 | u32 vmcr; |
| 248 | |
| 249 | vmcr = cpu_if->vgic_vmcr; |
Andre Przywara | e4823a7 | 2015-12-03 11:47:37 +0000 | [diff] [blame] | 250 | |
Christoffer Dall | 28232a4 | 2017-05-20 14:12:34 +0200 | [diff] [blame] | 251 | vmcrp->grpen0 = (vmcr & GICH_VMCR_ENABLE_GRP0_MASK) >> |
| 252 | GICH_VMCR_ENABLE_GRP0_SHIFT; |
| 253 | vmcrp->grpen1 = (vmcr & GICH_VMCR_ENABLE_GRP1_MASK) >> |
| 254 | GICH_VMCR_ENABLE_GRP1_SHIFT; |
| 255 | vmcrp->ackctl = (vmcr & GICH_VMCR_ACK_CTL_MASK) >> |
| 256 | GICH_VMCR_ACK_CTL_SHIFT; |
| 257 | vmcrp->fiqen = (vmcr & GICH_VMCR_FIQ_EN_MASK) >> |
| 258 | GICH_VMCR_FIQ_EN_SHIFT; |
| 259 | vmcrp->cbpr = (vmcr & GICH_VMCR_CBPR_MASK) >> |
| 260 | GICH_VMCR_CBPR_SHIFT; |
| 261 | vmcrp->eoim = (vmcr & GICH_VMCR_EOI_MODE_MASK) >> |
| 262 | GICH_VMCR_EOI_MODE_SHIFT; |
| 263 | |
Andre Przywara | e4823a7 | 2015-12-03 11:47:37 +0000 | [diff] [blame] | 264 | vmcrp->abpr = (vmcr & GICH_VMCR_ALIAS_BINPOINT_MASK) >> |
| 265 | GICH_VMCR_ALIAS_BINPOINT_SHIFT; |
| 266 | vmcrp->bpr = (vmcr & GICH_VMCR_BINPOINT_MASK) >> |
| 267 | GICH_VMCR_BINPOINT_SHIFT; |
Christoffer Dall | 6d56111 | 2017-03-21 22:05:22 +0100 | [diff] [blame] | 268 | vmcrp->pmr = ((vmcr & GICH_VMCR_PRIMASK_MASK) >> |
| 269 | GICH_VMCR_PRIMASK_SHIFT) << GICV_PMR_PRIORITY_SHIFT; |
Andre Przywara | e4823a7 | 2015-12-03 11:47:37 +0000 | [diff] [blame] | 270 | } |
Eric Auger | 9097773 | 2015-12-01 15:02:35 +0100 | [diff] [blame] | 271 | |
Eric Auger | ad275b8b | 2015-12-21 18:09:38 +0100 | [diff] [blame] | 272 | void vgic_v2_enable(struct kvm_vcpu *vcpu) |
| 273 | { |
Eric Auger | f7b6985 | 2015-12-02 10:30:13 +0100 | [diff] [blame] | 274 | /* |
| 275 | * By forcing VMCR to zero, the GIC will restore the binary |
| 276 | * points to their reset values. Anything else resets to zero |
| 277 | * anyway. |
| 278 | */ |
| 279 | vcpu->arch.vgic_cpu.vgic_v2.vgic_vmcr = 0; |
Eric Auger | f7b6985 | 2015-12-02 10:30:13 +0100 | [diff] [blame] | 280 | |
| 281 | /* Get the show on the road... */ |
| 282 | vcpu->arch.vgic_cpu.vgic_v2.vgic_hcr = GICH_HCR_EN; |
Eric Auger | ad275b8b | 2015-12-21 18:09:38 +0100 | [diff] [blame] | 283 | } |
| 284 | |
Eric Auger | b0442ee | 2015-12-21 15:04:42 +0100 | [diff] [blame] | 285 | /* check for overlapping regions and for regions crossing the end of memory */ |
| 286 | static bool vgic_v2_check_base(gpa_t dist_base, gpa_t cpu_base) |
| 287 | { |
| 288 | if (dist_base + KVM_VGIC_V2_DIST_SIZE < dist_base) |
| 289 | return false; |
| 290 | if (cpu_base + KVM_VGIC_V2_CPU_SIZE < cpu_base) |
| 291 | return false; |
| 292 | |
| 293 | if (dist_base + KVM_VGIC_V2_DIST_SIZE <= cpu_base) |
| 294 | return true; |
| 295 | if (cpu_base + KVM_VGIC_V2_CPU_SIZE <= dist_base) |
| 296 | return true; |
| 297 | |
| 298 | return false; |
| 299 | } |
| 300 | |
| 301 | int vgic_v2_map_resources(struct kvm *kvm) |
| 302 | { |
| 303 | struct vgic_dist *dist = &kvm->arch.vgic; |
| 304 | int ret = 0; |
| 305 | |
| 306 | if (vgic_ready(kvm)) |
| 307 | goto out; |
| 308 | |
| 309 | if (IS_VGIC_ADDR_UNDEF(dist->vgic_dist_base) || |
| 310 | IS_VGIC_ADDR_UNDEF(dist->vgic_cpu_base)) { |
| 311 | kvm_err("Need to set vgic cpu and dist addresses first\n"); |
| 312 | ret = -ENXIO; |
| 313 | goto out; |
| 314 | } |
| 315 | |
| 316 | if (!vgic_v2_check_base(dist->vgic_dist_base, dist->vgic_cpu_base)) { |
| 317 | kvm_err("VGIC CPU and dist frames overlap\n"); |
| 318 | ret = -EINVAL; |
| 319 | goto out; |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * Initialize the vgic if this hasn't already been done on demand by |
| 324 | * accessing the vgic state from userspace. |
| 325 | */ |
| 326 | ret = vgic_init(kvm); |
| 327 | if (ret) { |
| 328 | kvm_err("Unable to initialize VGIC dynamic data structures\n"); |
| 329 | goto out; |
| 330 | } |
| 331 | |
| 332 | ret = vgic_register_dist_iodev(kvm, dist->vgic_dist_base, VGIC_V2); |
| 333 | if (ret) { |
| 334 | kvm_err("Unable to register VGIC MMIO regions\n"); |
| 335 | goto out; |
| 336 | } |
| 337 | |
Marc Zyngier | a07d3b0 | 2016-09-06 09:28:47 +0100 | [diff] [blame] | 338 | if (!static_branch_unlikely(&vgic_v2_cpuif_trap)) { |
| 339 | ret = kvm_phys_addr_ioremap(kvm, dist->vgic_cpu_base, |
| 340 | kvm_vgic_global_state.vcpu_base, |
| 341 | KVM_VGIC_V2_CPU_SIZE, true); |
| 342 | if (ret) { |
| 343 | kvm_err("Unable to remap VGIC CPU to VCPU\n"); |
| 344 | goto out; |
| 345 | } |
Eric Auger | b0442ee | 2015-12-21 15:04:42 +0100 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | dist->ready = true; |
| 349 | |
| 350 | out: |
Eric Auger | b0442ee | 2015-12-21 15:04:42 +0100 | [diff] [blame] | 351 | return ret; |
| 352 | } |
| 353 | |
Marc Zyngier | fb5ee36 | 2016-09-06 09:28:45 +0100 | [diff] [blame] | 354 | DEFINE_STATIC_KEY_FALSE(vgic_v2_cpuif_trap); |
| 355 | |
Eric Auger | 9097773 | 2015-12-01 15:02:35 +0100 | [diff] [blame] | 356 | /** |
| 357 | * vgic_v2_probe - probe for a GICv2 compatible interrupt controller in DT |
| 358 | * @node: pointer to the DT node |
| 359 | * |
| 360 | * Returns 0 if a GICv2 has been found, returns an error code otherwise |
| 361 | */ |
| 362 | int vgic_v2_probe(const struct gic_kvm_info *info) |
| 363 | { |
| 364 | int ret; |
| 365 | u32 vtr; |
| 366 | |
| 367 | if (!info->vctrl.start) { |
| 368 | kvm_err("GICH not present in the firmware table\n"); |
| 369 | return -ENXIO; |
| 370 | } |
| 371 | |
Marc Zyngier | a07d3b0 | 2016-09-06 09:28:47 +0100 | [diff] [blame] | 372 | if (!PAGE_ALIGNED(info->vcpu.start) || |
| 373 | !PAGE_ALIGNED(resource_size(&info->vcpu))) { |
| 374 | kvm_info("GICV region size/alignment is unsafe, using trapping (reduced performance)\n"); |
Eric Auger | 9097773 | 2015-12-01 15:02:35 +0100 | [diff] [blame] | 375 | |
Marc Zyngier | 807a378 | 2017-12-04 16:26:09 +0000 | [diff] [blame] | 376 | ret = create_hyp_io_mappings(info->vcpu.start, |
| 377 | resource_size(&info->vcpu), |
Marc Zyngier | 1bb32a4 | 2017-12-04 16:43:23 +0000 | [diff] [blame] | 378 | &kvm_vgic_global_state.vcpu_base_va, |
| 379 | &kvm_vgic_global_state.vcpu_hyp_va); |
Marc Zyngier | a07d3b0 | 2016-09-06 09:28:47 +0100 | [diff] [blame] | 380 | if (ret) { |
| 381 | kvm_err("Cannot map GICV into hyp\n"); |
| 382 | goto out; |
| 383 | } |
| 384 | |
| 385 | static_branch_enable(&vgic_v2_cpuif_trap); |
Eric Auger | 9097773 | 2015-12-01 15:02:35 +0100 | [diff] [blame] | 386 | } |
| 387 | |
Marc Zyngier | 807a378 | 2017-12-04 16:26:09 +0000 | [diff] [blame] | 388 | ret = create_hyp_io_mappings(info->vctrl.start, |
| 389 | resource_size(&info->vctrl), |
Marc Zyngier | 1bb32a4 | 2017-12-04 16:43:23 +0000 | [diff] [blame] | 390 | &kvm_vgic_global_state.vctrl_base, |
| 391 | &kvm_vgic_global_state.vctrl_hyp); |
Marc Zyngier | 807a378 | 2017-12-04 16:26:09 +0000 | [diff] [blame] | 392 | if (ret) { |
| 393 | kvm_err("Cannot map VCTRL into hyp\n"); |
Marc Zyngier | a07d3b0 | 2016-09-06 09:28:47 +0100 | [diff] [blame] | 394 | goto out; |
Eric Auger | 9097773 | 2015-12-01 15:02:35 +0100 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | vtr = readl_relaxed(kvm_vgic_global_state.vctrl_base + GICH_VTR); |
| 398 | kvm_vgic_global_state.nr_lr = (vtr & 0x3f) + 1; |
| 399 | |
Marc Zyngier | a07d3b0 | 2016-09-06 09:28:47 +0100 | [diff] [blame] | 400 | ret = kvm_register_vgic_device(KVM_DEV_TYPE_ARM_VGIC_V2); |
| 401 | if (ret) { |
| 402 | kvm_err("Cannot register GICv2 KVM device\n"); |
| 403 | goto out; |
Eric Auger | 9097773 | 2015-12-01 15:02:35 +0100 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | kvm_vgic_global_state.can_emulate_gicv2 = true; |
Eric Auger | 9097773 | 2015-12-01 15:02:35 +0100 | [diff] [blame] | 407 | kvm_vgic_global_state.vcpu_base = info->vcpu.start; |
| 408 | kvm_vgic_global_state.type = VGIC_V2; |
| 409 | kvm_vgic_global_state.max_gic_vcpus = VGIC_V2_MAX_CPUS; |
| 410 | |
Ard Biesheuvel | 7660042 | 2018-03-02 08:16:30 +0000 | [diff] [blame] | 411 | kvm_debug("vgic-v2@%llx\n", info->vctrl.start); |
Eric Auger | 9097773 | 2015-12-01 15:02:35 +0100 | [diff] [blame] | 412 | |
| 413 | return 0; |
Marc Zyngier | a07d3b0 | 2016-09-06 09:28:47 +0100 | [diff] [blame] | 414 | out: |
| 415 | if (kvm_vgic_global_state.vctrl_base) |
| 416 | iounmap(kvm_vgic_global_state.vctrl_base); |
| 417 | if (kvm_vgic_global_state.vcpu_base_va) |
| 418 | iounmap(kvm_vgic_global_state.vcpu_base_va); |
| 419 | |
| 420 | return ret; |
Eric Auger | 9097773 | 2015-12-01 15:02:35 +0100 | [diff] [blame] | 421 | } |
Christoffer Dall | 328e5664 | 2016-03-24 11:21:04 +0100 | [diff] [blame] | 422 | |
Christoffer Dall | 75174ba | 2016-12-22 20:39:10 +0100 | [diff] [blame] | 423 | static void save_lrs(struct kvm_vcpu *vcpu, void __iomem *base) |
| 424 | { |
| 425 | struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2; |
| 426 | u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs; |
| 427 | u64 elrsr; |
| 428 | int i; |
| 429 | |
| 430 | elrsr = readl_relaxed(base + GICH_ELRSR0); |
| 431 | if (unlikely(used_lrs > 32)) |
| 432 | elrsr |= ((u64)readl_relaxed(base + GICH_ELRSR1)) << 32; |
| 433 | |
| 434 | for (i = 0; i < used_lrs; i++) { |
| 435 | if (elrsr & (1UL << i)) |
| 436 | cpu_if->vgic_lr[i] &= ~GICH_LR_STATE; |
| 437 | else |
| 438 | cpu_if->vgic_lr[i] = readl_relaxed(base + GICH_LR0 + (i * 4)); |
| 439 | |
| 440 | writel_relaxed(0, base + GICH_LR0 + (i * 4)); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | void vgic_v2_save_state(struct kvm_vcpu *vcpu) |
| 445 | { |
Marc Zyngier | 1bb32a4 | 2017-12-04 16:43:23 +0000 | [diff] [blame] | 446 | void __iomem *base = kvm_vgic_global_state.vctrl_base; |
Christoffer Dall | 75174ba | 2016-12-22 20:39:10 +0100 | [diff] [blame] | 447 | u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs; |
| 448 | |
| 449 | if (!base) |
| 450 | return; |
| 451 | |
| 452 | if (used_lrs) { |
Christoffer Dall | 75174ba | 2016-12-22 20:39:10 +0100 | [diff] [blame] | 453 | save_lrs(vcpu, base); |
| 454 | writel_relaxed(0, base + GICH_HCR); |
Christoffer Dall | 75174ba | 2016-12-22 20:39:10 +0100 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | |
| 458 | void vgic_v2_restore_state(struct kvm_vcpu *vcpu) |
| 459 | { |
Christoffer Dall | 75174ba | 2016-12-22 20:39:10 +0100 | [diff] [blame] | 460 | struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2; |
Marc Zyngier | 1bb32a4 | 2017-12-04 16:43:23 +0000 | [diff] [blame] | 461 | void __iomem *base = kvm_vgic_global_state.vctrl_base; |
Christoffer Dall | 75174ba | 2016-12-22 20:39:10 +0100 | [diff] [blame] | 462 | u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs; |
| 463 | int i; |
| 464 | |
| 465 | if (!base) |
| 466 | return; |
| 467 | |
| 468 | if (used_lrs) { |
| 469 | writel_relaxed(cpu_if->vgic_hcr, base + GICH_HCR); |
Christoffer Dall | 75174ba | 2016-12-22 20:39:10 +0100 | [diff] [blame] | 470 | for (i = 0; i < used_lrs; i++) { |
| 471 | writel_relaxed(cpu_if->vgic_lr[i], |
| 472 | base + GICH_LR0 + (i * 4)); |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
Christoffer Dall | 328e5664 | 2016-03-24 11:21:04 +0100 | [diff] [blame] | 477 | void vgic_v2_load(struct kvm_vcpu *vcpu) |
| 478 | { |
| 479 | struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2; |
Christoffer Dall | 328e5664 | 2016-03-24 11:21:04 +0100 | [diff] [blame] | 480 | |
Marc Zyngier | 1bb32a4 | 2017-12-04 16:43:23 +0000 | [diff] [blame] | 481 | writel_relaxed(cpu_if->vgic_vmcr, |
| 482 | kvm_vgic_global_state.vctrl_base + GICH_VMCR); |
| 483 | writel_relaxed(cpu_if->vgic_apr, |
| 484 | kvm_vgic_global_state.vctrl_base + GICH_APR); |
Christoffer Dall | 328e5664 | 2016-03-24 11:21:04 +0100 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | void vgic_v2_put(struct kvm_vcpu *vcpu) |
| 488 | { |
| 489 | struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2; |
Christoffer Dall | 328e5664 | 2016-03-24 11:21:04 +0100 | [diff] [blame] | 490 | |
Marc Zyngier | 1bb32a4 | 2017-12-04 16:43:23 +0000 | [diff] [blame] | 491 | cpu_if->vgic_vmcr = readl_relaxed(kvm_vgic_global_state.vctrl_base + GICH_VMCR); |
| 492 | cpu_if->vgic_apr = readl_relaxed(kvm_vgic_global_state.vctrl_base + GICH_APR); |
Christoffer Dall | 328e5664 | 2016-03-24 11:21:04 +0100 | [diff] [blame] | 493 | } |