vgic-v3.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/irqchip/arm-gic-v3.h>
  3. #include <linux/irq.h>
  4. #include <linux/irqdomain.h>
  5. #include <linux/kstrtox.h>
  6. #include <linux/kvm.h>
  7. #include <linux/kvm_host.h>
  8. #include <kvm/arm_vgic.h>
  9. #include <asm/kvm_hyp.h>
  10. #include <asm/kvm_mmu.h>
  11. #include <asm/kvm_asm.h>
  12. #include "vgic.h"
  13. static bool group0_trap;
  14. static bool group1_trap;
  15. static bool common_trap;
  16. static bool dir_trap;
  17. static bool gicv4_enable;
  18. void vgic_v3_set_underflow(struct kvm_vcpu *vcpu)
  19. {
  20. struct vgic_v3_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v3;
  21. cpuif->vgic_hcr |= ICH_HCR_UIE;
  22. }
  23. static bool lr_signals_eoi_mi(u64 lr_val)
  24. {
  25. return !(lr_val & ICH_LR_STATE) && (lr_val & ICH_LR_EOI) &&
  26. !(lr_val & ICH_LR_HW);
  27. }
  28. void vgic_v3_fold_lr_state(struct kvm_vcpu *vcpu)
  29. {
  30. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  31. struct vgic_v3_cpu_if *cpuif = &vgic_cpu->vgic_v3;
  32. u32 model = vcpu->kvm->arch.vgic.vgic_model;
  33. int lr;
  34. DEBUG_SPINLOCK_BUG_ON(!irqs_disabled());
  35. cpuif->vgic_hcr &= ~ICH_HCR_UIE;
  36. for (lr = 0; lr < cpuif->used_lrs; lr++) {
  37. u64 val = cpuif->vgic_lr[lr];
  38. u32 intid, cpuid;
  39. struct vgic_irq *irq;
  40. bool is_v2_sgi = false;
  41. bool deactivated;
  42. cpuid = val & GICH_LR_PHYSID_CPUID;
  43. cpuid >>= GICH_LR_PHYSID_CPUID_SHIFT;
  44. if (model == KVM_DEV_TYPE_ARM_VGIC_V3) {
  45. intid = val & ICH_LR_VIRTUAL_ID_MASK;
  46. } else {
  47. intid = val & GICH_LR_VIRTUALID;
  48. is_v2_sgi = vgic_irq_is_sgi(intid);
  49. }
  50. /* Notify fds when the guest EOI'ed a level-triggered IRQ */
  51. if (lr_signals_eoi_mi(val) && vgic_valid_spi(vcpu->kvm, intid))
  52. kvm_notify_acked_irq(vcpu->kvm, 0,
  53. intid - VGIC_NR_PRIVATE_IRQS);
  54. irq = vgic_get_irq(vcpu->kvm, vcpu, intid);
  55. if (!irq) /* An LPI could have been unmapped. */
  56. continue;
  57. raw_spin_lock(&irq->irq_lock);
  58. /* Always preserve the active bit, note deactivation */
  59. deactivated = irq->active && !(val & ICH_LR_ACTIVE_BIT);
  60. irq->active = !!(val & ICH_LR_ACTIVE_BIT);
  61. if (irq->active && is_v2_sgi)
  62. irq->active_source = cpuid;
  63. /* Edge is the only case where we preserve the pending bit */
  64. if (irq->config == VGIC_CONFIG_EDGE &&
  65. (val & ICH_LR_PENDING_BIT)) {
  66. irq->pending_latch = true;
  67. if (is_v2_sgi)
  68. irq->source |= (1 << cpuid);
  69. }
  70. /*
  71. * Clear soft pending state when level irqs have been acked.
  72. */
  73. if (irq->config == VGIC_CONFIG_LEVEL && !(val & ICH_LR_STATE))
  74. irq->pending_latch = false;
  75. /* Handle resampling for mapped interrupts if required */
  76. vgic_irq_handle_resampling(irq, deactivated, val & ICH_LR_PENDING_BIT);
  77. raw_spin_unlock(&irq->irq_lock);
  78. vgic_put_irq(vcpu->kvm, irq);
  79. }
  80. cpuif->used_lrs = 0;
  81. }
  82. /* Requires the irq to be locked already */
  83. void vgic_v3_populate_lr(struct kvm_vcpu *vcpu, struct vgic_irq *irq, int lr)
  84. {
  85. u32 model = vcpu->kvm->arch.vgic.vgic_model;
  86. u64 val = irq->intid;
  87. bool allow_pending = true, is_v2_sgi;
  88. is_v2_sgi = (vgic_irq_is_sgi(irq->intid) &&
  89. model == KVM_DEV_TYPE_ARM_VGIC_V2);
  90. if (irq->active) {
  91. val |= ICH_LR_ACTIVE_BIT;
  92. if (is_v2_sgi)
  93. val |= irq->active_source << GICH_LR_PHYSID_CPUID_SHIFT;
  94. if (vgic_irq_is_multi_sgi(irq)) {
  95. allow_pending = false;
  96. val |= ICH_LR_EOI;
  97. }
  98. }
  99. if (irq->hw && !vgic_irq_needs_resampling(irq)) {
  100. val |= ICH_LR_HW;
  101. val |= ((u64)irq->hwintid) << ICH_LR_PHYS_ID_SHIFT;
  102. /*
  103. * Never set pending+active on a HW interrupt, as the
  104. * pending state is kept at the physical distributor
  105. * level.
  106. */
  107. if (irq->active)
  108. allow_pending = false;
  109. } else {
  110. if (irq->config == VGIC_CONFIG_LEVEL) {
  111. val |= ICH_LR_EOI;
  112. /*
  113. * Software resampling doesn't work very well
  114. * if we allow P+A, so let's not do that.
  115. */
  116. if (irq->active)
  117. allow_pending = false;
  118. }
  119. }
  120. if (allow_pending && irq_is_pending(irq)) {
  121. val |= ICH_LR_PENDING_BIT;
  122. if (irq->config == VGIC_CONFIG_EDGE)
  123. irq->pending_latch = false;
  124. if (vgic_irq_is_sgi(irq->intid) &&
  125. model == KVM_DEV_TYPE_ARM_VGIC_V2) {
  126. u32 src = ffs(irq->source);
  127. if (WARN_RATELIMIT(!src, "No SGI source for INTID %d\n",
  128. irq->intid))
  129. return;
  130. val |= (src - 1) << GICH_LR_PHYSID_CPUID_SHIFT;
  131. irq->source &= ~(1 << (src - 1));
  132. if (irq->source) {
  133. irq->pending_latch = true;
  134. val |= ICH_LR_EOI;
  135. }
  136. }
  137. }
  138. /*
  139. * Level-triggered mapped IRQs are special because we only observe
  140. * rising edges as input to the VGIC. We therefore lower the line
  141. * level here, so that we can take new virtual IRQs. See
  142. * vgic_v3_fold_lr_state for more info.
  143. */
  144. if (vgic_irq_is_mapped_level(irq) && (val & ICH_LR_PENDING_BIT))
  145. irq->line_level = false;
  146. if (irq->group)
  147. val |= ICH_LR_GROUP;
  148. val |= (u64)irq->priority << ICH_LR_PRIORITY_SHIFT;
  149. vcpu->arch.vgic_cpu.vgic_v3.vgic_lr[lr] = val;
  150. }
  151. void vgic_v3_clear_lr(struct kvm_vcpu *vcpu, int lr)
  152. {
  153. vcpu->arch.vgic_cpu.vgic_v3.vgic_lr[lr] = 0;
  154. }
  155. void vgic_v3_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
  156. {
  157. struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3;
  158. u32 model = vcpu->kvm->arch.vgic.vgic_model;
  159. u32 vmcr;
  160. if (model == KVM_DEV_TYPE_ARM_VGIC_V2) {
  161. vmcr = (vmcrp->ackctl << ICH_VMCR_ACK_CTL_SHIFT) &
  162. ICH_VMCR_ACK_CTL_MASK;
  163. vmcr |= (vmcrp->fiqen << ICH_VMCR_FIQ_EN_SHIFT) &
  164. ICH_VMCR_FIQ_EN_MASK;
  165. } else {
  166. /*
  167. * When emulating GICv3 on GICv3 with SRE=1 on the
  168. * VFIQEn bit is RES1 and the VAckCtl bit is RES0.
  169. */
  170. vmcr = ICH_VMCR_FIQ_EN_MASK;
  171. }
  172. vmcr |= (vmcrp->cbpr << ICH_VMCR_CBPR_SHIFT) & ICH_VMCR_CBPR_MASK;
  173. vmcr |= (vmcrp->eoim << ICH_VMCR_EOIM_SHIFT) & ICH_VMCR_EOIM_MASK;
  174. vmcr |= (vmcrp->abpr << ICH_VMCR_BPR1_SHIFT) & ICH_VMCR_BPR1_MASK;
  175. vmcr |= (vmcrp->bpr << ICH_VMCR_BPR0_SHIFT) & ICH_VMCR_BPR0_MASK;
  176. vmcr |= (vmcrp->pmr << ICH_VMCR_PMR_SHIFT) & ICH_VMCR_PMR_MASK;
  177. vmcr |= (vmcrp->grpen0 << ICH_VMCR_ENG0_SHIFT) & ICH_VMCR_ENG0_MASK;
  178. vmcr |= (vmcrp->grpen1 << ICH_VMCR_ENG1_SHIFT) & ICH_VMCR_ENG1_MASK;
  179. cpu_if->vgic_vmcr = vmcr;
  180. }
  181. void vgic_v3_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
  182. {
  183. struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3;
  184. u32 model = vcpu->kvm->arch.vgic.vgic_model;
  185. u32 vmcr;
  186. vmcr = cpu_if->vgic_vmcr;
  187. if (model == KVM_DEV_TYPE_ARM_VGIC_V2) {
  188. vmcrp->ackctl = (vmcr & ICH_VMCR_ACK_CTL_MASK) >>
  189. ICH_VMCR_ACK_CTL_SHIFT;
  190. vmcrp->fiqen = (vmcr & ICH_VMCR_FIQ_EN_MASK) >>
  191. ICH_VMCR_FIQ_EN_SHIFT;
  192. } else {
  193. /*
  194. * When emulating GICv3 on GICv3 with SRE=1 on the
  195. * VFIQEn bit is RES1 and the VAckCtl bit is RES0.
  196. */
  197. vmcrp->fiqen = 1;
  198. vmcrp->ackctl = 0;
  199. }
  200. vmcrp->cbpr = (vmcr & ICH_VMCR_CBPR_MASK) >> ICH_VMCR_CBPR_SHIFT;
  201. vmcrp->eoim = (vmcr & ICH_VMCR_EOIM_MASK) >> ICH_VMCR_EOIM_SHIFT;
  202. vmcrp->abpr = (vmcr & ICH_VMCR_BPR1_MASK) >> ICH_VMCR_BPR1_SHIFT;
  203. vmcrp->bpr = (vmcr & ICH_VMCR_BPR0_MASK) >> ICH_VMCR_BPR0_SHIFT;
  204. vmcrp->pmr = (vmcr & ICH_VMCR_PMR_MASK) >> ICH_VMCR_PMR_SHIFT;
  205. vmcrp->grpen0 = (vmcr & ICH_VMCR_ENG0_MASK) >> ICH_VMCR_ENG0_SHIFT;
  206. vmcrp->grpen1 = (vmcr & ICH_VMCR_ENG1_MASK) >> ICH_VMCR_ENG1_SHIFT;
  207. }
  208. #define INITIAL_PENDBASER_VALUE \
  209. (GIC_BASER_CACHEABILITY(GICR_PENDBASER, INNER, RaWb) | \
  210. GIC_BASER_CACHEABILITY(GICR_PENDBASER, OUTER, SameAsInner) | \
  211. GIC_BASER_SHAREABILITY(GICR_PENDBASER, InnerShareable))
  212. void vgic_v3_enable(struct kvm_vcpu *vcpu)
  213. {
  214. struct vgic_v3_cpu_if *vgic_v3 = &vcpu->arch.vgic_cpu.vgic_v3;
  215. /*
  216. * By forcing VMCR to zero, the GIC will restore the binary
  217. * points to their reset values. Anything else resets to zero
  218. * anyway.
  219. */
  220. vgic_v3->vgic_vmcr = 0;
  221. /*
  222. * If we are emulating a GICv3, we do it in an non-GICv2-compatible
  223. * way, so we force SRE to 1 to demonstrate this to the guest.
  224. * Also, we don't support any form of IRQ/FIQ bypass.
  225. * This goes with the spec allowing the value to be RAO/WI.
  226. */
  227. if (vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) {
  228. vgic_v3->vgic_sre = (ICC_SRE_EL1_DIB |
  229. ICC_SRE_EL1_DFB |
  230. ICC_SRE_EL1_SRE);
  231. vcpu->arch.vgic_cpu.pendbaser = INITIAL_PENDBASER_VALUE;
  232. } else {
  233. vgic_v3->vgic_sre = 0;
  234. }
  235. vcpu->arch.vgic_cpu.num_id_bits = (kvm_vgic_global_state.ich_vtr_el2 &
  236. ICH_VTR_ID_BITS_MASK) >>
  237. ICH_VTR_ID_BITS_SHIFT;
  238. vcpu->arch.vgic_cpu.num_pri_bits = ((kvm_vgic_global_state.ich_vtr_el2 &
  239. ICH_VTR_PRI_BITS_MASK) >>
  240. ICH_VTR_PRI_BITS_SHIFT) + 1;
  241. /* Get the show on the road... */
  242. vgic_v3->vgic_hcr = ICH_HCR_EN;
  243. }
  244. void vcpu_set_ich_hcr(struct kvm_vcpu *vcpu)
  245. {
  246. struct vgic_v3_cpu_if *vgic_v3 = &vcpu->arch.vgic_cpu.vgic_v3;
  247. /* Hide GICv3 sysreg if necessary */
  248. if (!kvm_has_gicv3(vcpu->kvm)) {
  249. vgic_v3->vgic_hcr |= ICH_HCR_TALL0 | ICH_HCR_TALL1 | ICH_HCR_TC;
  250. return;
  251. }
  252. if (group0_trap)
  253. vgic_v3->vgic_hcr |= ICH_HCR_TALL0;
  254. if (group1_trap)
  255. vgic_v3->vgic_hcr |= ICH_HCR_TALL1;
  256. if (common_trap)
  257. vgic_v3->vgic_hcr |= ICH_HCR_TC;
  258. if (dir_trap)
  259. vgic_v3->vgic_hcr |= ICH_HCR_TDIR;
  260. }
  261. int vgic_v3_lpi_sync_pending_status(struct kvm *kvm, struct vgic_irq *irq)
  262. {
  263. struct kvm_vcpu *vcpu;
  264. int byte_offset, bit_nr;
  265. gpa_t pendbase, ptr;
  266. bool status;
  267. u8 val;
  268. int ret;
  269. unsigned long flags;
  270. retry:
  271. vcpu = irq->target_vcpu;
  272. if (!vcpu)
  273. return 0;
  274. pendbase = GICR_PENDBASER_ADDRESS(vcpu->arch.vgic_cpu.pendbaser);
  275. byte_offset = irq->intid / BITS_PER_BYTE;
  276. bit_nr = irq->intid % BITS_PER_BYTE;
  277. ptr = pendbase + byte_offset;
  278. ret = kvm_read_guest_lock(kvm, ptr, &val, 1);
  279. if (ret)
  280. return ret;
  281. status = val & (1 << bit_nr);
  282. raw_spin_lock_irqsave(&irq->irq_lock, flags);
  283. if (irq->target_vcpu != vcpu) {
  284. raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
  285. goto retry;
  286. }
  287. irq->pending_latch = status;
  288. vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
  289. if (status) {
  290. /* clear consumed data */
  291. val &= ~(1 << bit_nr);
  292. ret = vgic_write_guest_lock(kvm, ptr, &val, 1);
  293. if (ret)
  294. return ret;
  295. }
  296. return 0;
  297. }
  298. /*
  299. * The deactivation of the doorbell interrupt will trigger the
  300. * unmapping of the associated vPE.
  301. */
  302. static void unmap_all_vpes(struct kvm *kvm)
  303. {
  304. struct vgic_dist *dist = &kvm->arch.vgic;
  305. int i;
  306. for (i = 0; i < dist->its_vm.nr_vpes; i++)
  307. free_irq(dist->its_vm.vpes[i]->irq, kvm_get_vcpu(kvm, i));
  308. }
  309. static void map_all_vpes(struct kvm *kvm)
  310. {
  311. struct vgic_dist *dist = &kvm->arch.vgic;
  312. int i;
  313. for (i = 0; i < dist->its_vm.nr_vpes; i++)
  314. WARN_ON(vgic_v4_request_vpe_irq(kvm_get_vcpu(kvm, i),
  315. dist->its_vm.vpes[i]->irq));
  316. }
  317. /*
  318. * vgic_v3_save_pending_tables - Save the pending tables into guest RAM
  319. * kvm lock and all vcpu lock must be held
  320. */
  321. int vgic_v3_save_pending_tables(struct kvm *kvm)
  322. {
  323. struct vgic_dist *dist = &kvm->arch.vgic;
  324. struct vgic_irq *irq;
  325. gpa_t last_ptr = ~(gpa_t)0;
  326. bool vlpi_avail = false;
  327. unsigned long index;
  328. int ret = 0;
  329. u8 val;
  330. if (unlikely(!vgic_initialized(kvm)))
  331. return -ENXIO;
  332. /*
  333. * A preparation for getting any VLPI states.
  334. * The above vgic initialized check also ensures that the allocation
  335. * and enabling of the doorbells have already been done.
  336. */
  337. if (kvm_vgic_global_state.has_gicv4_1) {
  338. unmap_all_vpes(kvm);
  339. vlpi_avail = true;
  340. }
  341. xa_for_each(&dist->lpi_xa, index, irq) {
  342. int byte_offset, bit_nr;
  343. struct kvm_vcpu *vcpu;
  344. gpa_t pendbase, ptr;
  345. bool is_pending;
  346. bool stored;
  347. vcpu = irq->target_vcpu;
  348. if (!vcpu)
  349. continue;
  350. pendbase = GICR_PENDBASER_ADDRESS(vcpu->arch.vgic_cpu.pendbaser);
  351. byte_offset = irq->intid / BITS_PER_BYTE;
  352. bit_nr = irq->intid % BITS_PER_BYTE;
  353. ptr = pendbase + byte_offset;
  354. if (ptr != last_ptr) {
  355. ret = kvm_read_guest_lock(kvm, ptr, &val, 1);
  356. if (ret)
  357. goto out;
  358. last_ptr = ptr;
  359. }
  360. stored = val & (1U << bit_nr);
  361. is_pending = irq->pending_latch;
  362. if (irq->hw && vlpi_avail)
  363. vgic_v4_get_vlpi_state(irq, &is_pending);
  364. if (stored == is_pending)
  365. continue;
  366. if (is_pending)
  367. val |= 1 << bit_nr;
  368. else
  369. val &= ~(1 << bit_nr);
  370. ret = vgic_write_guest_lock(kvm, ptr, &val, 1);
  371. if (ret)
  372. goto out;
  373. }
  374. out:
  375. if (vlpi_avail)
  376. map_all_vpes(kvm);
  377. return ret;
  378. }
  379. /**
  380. * vgic_v3_rdist_overlap - check if a region overlaps with any
  381. * existing redistributor region
  382. *
  383. * @kvm: kvm handle
  384. * @base: base of the region
  385. * @size: size of region
  386. *
  387. * Return: true if there is an overlap
  388. */
  389. bool vgic_v3_rdist_overlap(struct kvm *kvm, gpa_t base, size_t size)
  390. {
  391. struct vgic_dist *d = &kvm->arch.vgic;
  392. struct vgic_redist_region *rdreg;
  393. list_for_each_entry(rdreg, &d->rd_regions, list) {
  394. if ((base + size > rdreg->base) &&
  395. (base < rdreg->base + vgic_v3_rd_region_size(kvm, rdreg)))
  396. return true;
  397. }
  398. return false;
  399. }
  400. /*
  401. * Check for overlapping regions and for regions crossing the end of memory
  402. * for base addresses which have already been set.
  403. */
  404. bool vgic_v3_check_base(struct kvm *kvm)
  405. {
  406. struct vgic_dist *d = &kvm->arch.vgic;
  407. struct vgic_redist_region *rdreg;
  408. if (!IS_VGIC_ADDR_UNDEF(d->vgic_dist_base) &&
  409. d->vgic_dist_base + KVM_VGIC_V3_DIST_SIZE < d->vgic_dist_base)
  410. return false;
  411. list_for_each_entry(rdreg, &d->rd_regions, list) {
  412. size_t sz = vgic_v3_rd_region_size(kvm, rdreg);
  413. if (vgic_check_iorange(kvm, VGIC_ADDR_UNDEF,
  414. rdreg->base, SZ_64K, sz))
  415. return false;
  416. }
  417. if (IS_VGIC_ADDR_UNDEF(d->vgic_dist_base))
  418. return true;
  419. return !vgic_v3_rdist_overlap(kvm, d->vgic_dist_base,
  420. KVM_VGIC_V3_DIST_SIZE);
  421. }
  422. /**
  423. * vgic_v3_rdist_free_slot - Look up registered rdist regions and identify one
  424. * which has free space to put a new rdist region.
  425. *
  426. * @rd_regions: redistributor region list head
  427. *
  428. * A redistributor regions maps n redistributors, n = region size / (2 x 64kB).
  429. * Stride between redistributors is 0 and regions are filled in the index order.
  430. *
  431. * Return: the redist region handle, if any, that has space to map a new rdist
  432. * region.
  433. */
  434. struct vgic_redist_region *vgic_v3_rdist_free_slot(struct list_head *rd_regions)
  435. {
  436. struct vgic_redist_region *rdreg;
  437. list_for_each_entry(rdreg, rd_regions, list) {
  438. if (!vgic_v3_redist_region_full(rdreg))
  439. return rdreg;
  440. }
  441. return NULL;
  442. }
  443. struct vgic_redist_region *vgic_v3_rdist_region_from_index(struct kvm *kvm,
  444. u32 index)
  445. {
  446. struct list_head *rd_regions = &kvm->arch.vgic.rd_regions;
  447. struct vgic_redist_region *rdreg;
  448. list_for_each_entry(rdreg, rd_regions, list) {
  449. if (rdreg->index == index)
  450. return rdreg;
  451. }
  452. return NULL;
  453. }
  454. int vgic_v3_map_resources(struct kvm *kvm)
  455. {
  456. struct vgic_dist *dist = &kvm->arch.vgic;
  457. struct kvm_vcpu *vcpu;
  458. unsigned long c;
  459. kvm_for_each_vcpu(c, vcpu, kvm) {
  460. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  461. if (IS_VGIC_ADDR_UNDEF(vgic_cpu->rd_iodev.base_addr)) {
  462. kvm_debug("vcpu %ld redistributor base not set\n", c);
  463. return -ENXIO;
  464. }
  465. }
  466. if (IS_VGIC_ADDR_UNDEF(dist->vgic_dist_base)) {
  467. kvm_debug("Need to set vgic distributor addresses first\n");
  468. return -ENXIO;
  469. }
  470. if (!vgic_v3_check_base(kvm)) {
  471. kvm_debug("VGIC redist and dist frames overlap\n");
  472. return -EINVAL;
  473. }
  474. /*
  475. * For a VGICv3 we require the userland to explicitly initialize
  476. * the VGIC before we need to use it.
  477. */
  478. if (!vgic_initialized(kvm)) {
  479. return -EBUSY;
  480. }
  481. if (kvm_vgic_global_state.has_gicv4_1)
  482. vgic_v4_configure_vsgis(kvm);
  483. return 0;
  484. }
  485. DEFINE_STATIC_KEY_FALSE(vgic_v3_cpuif_trap);
  486. static int __init early_group0_trap_cfg(char *buf)
  487. {
  488. return kstrtobool(buf, &group0_trap);
  489. }
  490. early_param("kvm-arm.vgic_v3_group0_trap", early_group0_trap_cfg);
  491. static int __init early_group1_trap_cfg(char *buf)
  492. {
  493. return kstrtobool(buf, &group1_trap);
  494. }
  495. early_param("kvm-arm.vgic_v3_group1_trap", early_group1_trap_cfg);
  496. static int __init early_common_trap_cfg(char *buf)
  497. {
  498. return kstrtobool(buf, &common_trap);
  499. }
  500. early_param("kvm-arm.vgic_v3_common_trap", early_common_trap_cfg);
  501. static int __init early_gicv4_enable(char *buf)
  502. {
  503. return kstrtobool(buf, &gicv4_enable);
  504. }
  505. early_param("kvm-arm.vgic_v4_enable", early_gicv4_enable);
  506. static const struct midr_range broken_seis[] = {
  507. MIDR_ALL_VERSIONS(MIDR_APPLE_M1_ICESTORM),
  508. MIDR_ALL_VERSIONS(MIDR_APPLE_M1_FIRESTORM),
  509. MIDR_ALL_VERSIONS(MIDR_APPLE_M1_ICESTORM_PRO),
  510. MIDR_ALL_VERSIONS(MIDR_APPLE_M1_FIRESTORM_PRO),
  511. MIDR_ALL_VERSIONS(MIDR_APPLE_M1_ICESTORM_MAX),
  512. MIDR_ALL_VERSIONS(MIDR_APPLE_M1_FIRESTORM_MAX),
  513. MIDR_ALL_VERSIONS(MIDR_APPLE_M2_BLIZZARD),
  514. MIDR_ALL_VERSIONS(MIDR_APPLE_M2_AVALANCHE),
  515. MIDR_ALL_VERSIONS(MIDR_APPLE_M2_BLIZZARD_PRO),
  516. MIDR_ALL_VERSIONS(MIDR_APPLE_M2_AVALANCHE_PRO),
  517. MIDR_ALL_VERSIONS(MIDR_APPLE_M2_BLIZZARD_MAX),
  518. MIDR_ALL_VERSIONS(MIDR_APPLE_M2_AVALANCHE_MAX),
  519. {},
  520. };
  521. static bool vgic_v3_broken_seis(void)
  522. {
  523. return ((kvm_vgic_global_state.ich_vtr_el2 & ICH_VTR_SEIS_MASK) &&
  524. is_midr_in_range_list(read_cpuid_id(), broken_seis));
  525. }
  526. /**
  527. * vgic_v3_probe - probe for a VGICv3 compatible interrupt controller
  528. * @info: pointer to the GIC description
  529. *
  530. * Returns 0 if the VGICv3 has been probed successfully, returns an error code
  531. * otherwise
  532. */
  533. int vgic_v3_probe(const struct gic_kvm_info *info)
  534. {
  535. u64 ich_vtr_el2 = kvm_call_hyp_ret(__vgic_v3_get_gic_config);
  536. bool has_v2;
  537. int ret;
  538. has_v2 = ich_vtr_el2 >> 63;
  539. ich_vtr_el2 = (u32)ich_vtr_el2;
  540. /*
  541. * The ListRegs field is 5 bits, but there is an architectural
  542. * maximum of 16 list registers. Just ignore bit 4...
  543. */
  544. kvm_vgic_global_state.nr_lr = (ich_vtr_el2 & 0xf) + 1;
  545. kvm_vgic_global_state.can_emulate_gicv2 = false;
  546. kvm_vgic_global_state.ich_vtr_el2 = ich_vtr_el2;
  547. /* GICv4 support? */
  548. if (info->has_v4) {
  549. kvm_vgic_global_state.has_gicv4 = gicv4_enable;
  550. kvm_vgic_global_state.has_gicv4_1 = info->has_v4_1 && gicv4_enable;
  551. kvm_info("GICv4%s support %sabled\n",
  552. kvm_vgic_global_state.has_gicv4_1 ? ".1" : "",
  553. gicv4_enable ? "en" : "dis");
  554. }
  555. kvm_vgic_global_state.vcpu_base = 0;
  556. if (!info->vcpu.start) {
  557. kvm_info("GICv3: no GICV resource entry\n");
  558. } else if (!has_v2) {
  559. pr_warn(FW_BUG "CPU interface incapable of MMIO access\n");
  560. } else if (!PAGE_ALIGNED(info->vcpu.start)) {
  561. pr_warn("GICV physical address 0x%llx not page aligned\n",
  562. (unsigned long long)info->vcpu.start);
  563. } else if (kvm_get_mode() != KVM_MODE_PROTECTED) {
  564. kvm_vgic_global_state.vcpu_base = info->vcpu.start;
  565. kvm_vgic_global_state.can_emulate_gicv2 = true;
  566. ret = kvm_register_vgic_device(KVM_DEV_TYPE_ARM_VGIC_V2);
  567. if (ret) {
  568. kvm_err("Cannot register GICv2 KVM device.\n");
  569. return ret;
  570. }
  571. kvm_info("vgic-v2@%llx\n", info->vcpu.start);
  572. }
  573. ret = kvm_register_vgic_device(KVM_DEV_TYPE_ARM_VGIC_V3);
  574. if (ret) {
  575. kvm_err("Cannot register GICv3 KVM device.\n");
  576. kvm_unregister_device_ops(KVM_DEV_TYPE_ARM_VGIC_V2);
  577. return ret;
  578. }
  579. if (kvm_vgic_global_state.vcpu_base == 0)
  580. kvm_info("disabling GICv2 emulation\n");
  581. if (cpus_have_final_cap(ARM64_WORKAROUND_CAVIUM_30115)) {
  582. group0_trap = true;
  583. group1_trap = true;
  584. }
  585. if (vgic_v3_broken_seis()) {
  586. kvm_info("GICv3 with broken locally generated SEI\n");
  587. kvm_vgic_global_state.ich_vtr_el2 &= ~ICH_VTR_SEIS_MASK;
  588. group0_trap = true;
  589. group1_trap = true;
  590. if (ich_vtr_el2 & ICH_VTR_TDS_MASK)
  591. dir_trap = true;
  592. else
  593. common_trap = true;
  594. }
  595. if (group0_trap || group1_trap || common_trap | dir_trap) {
  596. kvm_info("GICv3 sysreg trapping enabled ([%s%s%s%s], reduced performance)\n",
  597. group0_trap ? "G0" : "",
  598. group1_trap ? "G1" : "",
  599. common_trap ? "C" : "",
  600. dir_trap ? "D" : "");
  601. static_branch_enable(&vgic_v3_cpuif_trap);
  602. }
  603. kvm_vgic_global_state.vctrl_base = NULL;
  604. kvm_vgic_global_state.type = VGIC_V3;
  605. kvm_vgic_global_state.max_gic_vcpus = VGIC_V3_MAX_CPUS;
  606. return 0;
  607. }
  608. void vgic_v3_load(struct kvm_vcpu *vcpu)
  609. {
  610. struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3;
  611. kvm_call_hyp(__vgic_v3_restore_vmcr_aprs, cpu_if);
  612. if (has_vhe())
  613. __vgic_v3_activate_traps(cpu_if);
  614. WARN_ON(vgic_v4_load(vcpu));
  615. }
  616. void vgic_v3_put(struct kvm_vcpu *vcpu)
  617. {
  618. struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3;
  619. kvm_call_hyp(__vgic_v3_save_vmcr_aprs, cpu_if);
  620. WARN_ON(vgic_v4_put(vcpu));
  621. if (has_vhe())
  622. __vgic_v3_deactivate_traps(cpu_if);
  623. }