vgic-v4.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2017 ARM Ltd.
  4. * Author: Marc Zyngier <[email protected]>
  5. */
  6. #include <linux/interrupt.h>
  7. #include <linux/irq.h>
  8. #include <linux/irqdomain.h>
  9. #include <linux/kvm_host.h>
  10. #include <linux/irqchip/arm-gic-v3.h>
  11. #include "vgic.h"
  12. /*
  13. * How KVM uses GICv4 (insert rude comments here):
  14. *
  15. * The vgic-v4 layer acts as a bridge between several entities:
  16. * - The GICv4 ITS representation offered by the ITS driver
  17. * - VFIO, which is in charge of the PCI endpoint
  18. * - The virtual ITS, which is the only thing the guest sees
  19. *
  20. * The configuration of VLPIs is triggered by a callback from VFIO,
  21. * instructing KVM that a PCI device has been configured to deliver
  22. * MSIs to a vITS.
  23. *
  24. * kvm_vgic_v4_set_forwarding() is thus called with the routing entry,
  25. * and this is used to find the corresponding vITS data structures
  26. * (ITS instance, device, event and irq) using a process that is
  27. * extremely similar to the injection of an MSI.
  28. *
  29. * At this stage, we can link the guest's view of an LPI (uniquely
  30. * identified by the routing entry) and the host irq, using the GICv4
  31. * driver mapping operation. Should the mapping succeed, we've then
  32. * successfully upgraded the guest's LPI to a VLPI. We can then start
  33. * with updating GICv4's view of the property table and generating an
  34. * INValidation in order to kickstart the delivery of this VLPI to the
  35. * guest directly, without software intervention. Well, almost.
  36. *
  37. * When the PCI endpoint is deconfigured, this operation is reversed
  38. * with VFIO calling kvm_vgic_v4_unset_forwarding().
  39. *
  40. * Once the VLPI has been mapped, it needs to follow any change the
  41. * guest performs on its LPI through the vITS. For that, a number of
  42. * command handlers have hooks to communicate these changes to the HW:
  43. * - Any invalidation triggers a call to its_prop_update_vlpi()
  44. * - The INT command results in a irq_set_irqchip_state(), which
  45. * generates an INT on the corresponding VLPI.
  46. * - The CLEAR command results in a irq_set_irqchip_state(), which
  47. * generates an CLEAR on the corresponding VLPI.
  48. * - DISCARD translates into an unmap, similar to a call to
  49. * kvm_vgic_v4_unset_forwarding().
  50. * - MOVI is translated by an update of the existing mapping, changing
  51. * the target vcpu, resulting in a VMOVI being generated.
  52. * - MOVALL is translated by a string of mapping updates (similar to
  53. * the handling of MOVI). MOVALL is horrible.
  54. *
  55. * Note that a DISCARD/MAPTI sequence emitted from the guest without
  56. * reprogramming the PCI endpoint after MAPTI does not result in a
  57. * VLPI being mapped, as there is no callback from VFIO (the guest
  58. * will get the interrupt via the normal SW injection). Fixing this is
  59. * not trivial, and requires some horrible messing with the VFIO
  60. * internals. Not fun. Don't do that.
  61. *
  62. * Then there is the scheduling. Each time a vcpu is about to run on a
  63. * physical CPU, KVM must tell the corresponding redistributor about
  64. * it. And if we've migrated our vcpu from one CPU to another, we must
  65. * tell the ITS (so that the messages reach the right redistributor).
  66. * This is done in two steps: first issue a irq_set_affinity() on the
  67. * irq corresponding to the vcpu, then call its_make_vpe_resident().
  68. * You must be in a non-preemptible context. On exit, a call to
  69. * its_make_vpe_non_resident() tells the redistributor that we're done
  70. * with the vcpu.
  71. *
  72. * Finally, the doorbell handling: Each vcpu is allocated an interrupt
  73. * which will fire each time a VLPI is made pending whilst the vcpu is
  74. * not running. Each time the vcpu gets blocked, the doorbell
  75. * interrupt gets enabled. When the vcpu is unblocked (for whatever
  76. * reason), the doorbell interrupt is disabled.
  77. */
  78. #define DB_IRQ_FLAGS (IRQ_NOAUTOEN | IRQ_DISABLE_UNLAZY | IRQ_NO_BALANCING)
  79. static irqreturn_t vgic_v4_doorbell_handler(int irq, void *info)
  80. {
  81. struct kvm_vcpu *vcpu = info;
  82. /* We got the message, no need to fire again */
  83. if (!kvm_vgic_global_state.has_gicv4_1 &&
  84. !irqd_irq_disabled(&irq_to_desc(irq)->irq_data))
  85. disable_irq_nosync(irq);
  86. /*
  87. * The v4.1 doorbell can fire concurrently with the vPE being
  88. * made non-resident. Ensure we only update pending_last
  89. * *after* the non-residency sequence has completed.
  90. */
  91. raw_spin_lock(&vcpu->arch.vgic_cpu.vgic_v3.its_vpe.vpe_lock);
  92. vcpu->arch.vgic_cpu.vgic_v3.its_vpe.pending_last = true;
  93. raw_spin_unlock(&vcpu->arch.vgic_cpu.vgic_v3.its_vpe.vpe_lock);
  94. kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
  95. kvm_vcpu_kick(vcpu);
  96. return IRQ_HANDLED;
  97. }
  98. static void vgic_v4_sync_sgi_config(struct its_vpe *vpe, struct vgic_irq *irq)
  99. {
  100. vpe->sgi_config[irq->intid].enabled = irq->enabled;
  101. vpe->sgi_config[irq->intid].group = irq->group;
  102. vpe->sgi_config[irq->intid].priority = irq->priority;
  103. }
  104. static void vgic_v4_enable_vsgis(struct kvm_vcpu *vcpu)
  105. {
  106. struct its_vpe *vpe = &vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
  107. int i;
  108. /*
  109. * With GICv4.1, every virtual SGI can be directly injected. So
  110. * let's pretend that they are HW interrupts, tied to a host
  111. * IRQ. The SGI code will do its magic.
  112. */
  113. for (i = 0; i < VGIC_NR_SGIS; i++) {
  114. struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, i);
  115. struct irq_desc *desc;
  116. unsigned long flags;
  117. int ret;
  118. raw_spin_lock_irqsave(&irq->irq_lock, flags);
  119. if (irq->hw)
  120. goto unlock;
  121. irq->hw = true;
  122. irq->host_irq = irq_find_mapping(vpe->sgi_domain, i);
  123. /* Transfer the full irq state to the vPE */
  124. vgic_v4_sync_sgi_config(vpe, irq);
  125. desc = irq_to_desc(irq->host_irq);
  126. ret = irq_domain_activate_irq(irq_desc_get_irq_data(desc),
  127. false);
  128. if (!WARN_ON(ret)) {
  129. /* Transfer pending state */
  130. ret = irq_set_irqchip_state(irq->host_irq,
  131. IRQCHIP_STATE_PENDING,
  132. irq->pending_latch);
  133. WARN_ON(ret);
  134. irq->pending_latch = false;
  135. }
  136. unlock:
  137. raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
  138. vgic_put_irq(vcpu->kvm, irq);
  139. }
  140. }
  141. static void vgic_v4_disable_vsgis(struct kvm_vcpu *vcpu)
  142. {
  143. int i;
  144. for (i = 0; i < VGIC_NR_SGIS; i++) {
  145. struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, i);
  146. struct irq_desc *desc;
  147. unsigned long flags;
  148. int ret;
  149. raw_spin_lock_irqsave(&irq->irq_lock, flags);
  150. if (!irq->hw)
  151. goto unlock;
  152. irq->hw = false;
  153. ret = irq_get_irqchip_state(irq->host_irq,
  154. IRQCHIP_STATE_PENDING,
  155. &irq->pending_latch);
  156. WARN_ON(ret);
  157. desc = irq_to_desc(irq->host_irq);
  158. irq_domain_deactivate_irq(irq_desc_get_irq_data(desc));
  159. unlock:
  160. raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
  161. vgic_put_irq(vcpu->kvm, irq);
  162. }
  163. }
  164. /* Must be called with the kvm lock held */
  165. void vgic_v4_configure_vsgis(struct kvm *kvm)
  166. {
  167. struct vgic_dist *dist = &kvm->arch.vgic;
  168. struct kvm_vcpu *vcpu;
  169. unsigned long i;
  170. kvm_arm_halt_guest(kvm);
  171. kvm_for_each_vcpu(i, vcpu, kvm) {
  172. if (dist->nassgireq)
  173. vgic_v4_enable_vsgis(vcpu);
  174. else
  175. vgic_v4_disable_vsgis(vcpu);
  176. }
  177. kvm_arm_resume_guest(kvm);
  178. }
  179. /*
  180. * Must be called with GICv4.1 and the vPE unmapped, which
  181. * indicates the invalidation of any VPT caches associated
  182. * with the vPE, thus we can get the VLPI state by peeking
  183. * at the VPT.
  184. */
  185. void vgic_v4_get_vlpi_state(struct vgic_irq *irq, bool *val)
  186. {
  187. struct its_vpe *vpe = &irq->target_vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
  188. int mask = BIT(irq->intid % BITS_PER_BYTE);
  189. void *va;
  190. u8 *ptr;
  191. va = page_address(vpe->vpt_page);
  192. ptr = va + irq->intid / BITS_PER_BYTE;
  193. *val = !!(*ptr & mask);
  194. }
  195. int vgic_v4_request_vpe_irq(struct kvm_vcpu *vcpu, int irq)
  196. {
  197. return request_irq(irq, vgic_v4_doorbell_handler, 0, "vcpu", vcpu);
  198. }
  199. /**
  200. * vgic_v4_init - Initialize the GICv4 data structures
  201. * @kvm: Pointer to the VM being initialized
  202. *
  203. * We may be called each time a vITS is created, or when the
  204. * vgic is initialized. In both cases, the number of vcpus
  205. * should now be fixed.
  206. */
  207. int vgic_v4_init(struct kvm *kvm)
  208. {
  209. struct vgic_dist *dist = &kvm->arch.vgic;
  210. struct kvm_vcpu *vcpu;
  211. int nr_vcpus, ret;
  212. unsigned long i;
  213. lockdep_assert_held(&kvm->arch.config_lock);
  214. if (!kvm_vgic_global_state.has_gicv4)
  215. return 0; /* Nothing to see here... move along. */
  216. if (dist->its_vm.vpes)
  217. return 0;
  218. nr_vcpus = atomic_read(&kvm->online_vcpus);
  219. dist->its_vm.vpes = kcalloc(nr_vcpus, sizeof(*dist->its_vm.vpes),
  220. GFP_KERNEL_ACCOUNT);
  221. if (!dist->its_vm.vpes)
  222. return -ENOMEM;
  223. dist->its_vm.nr_vpes = nr_vcpus;
  224. kvm_for_each_vcpu(i, vcpu, kvm)
  225. dist->its_vm.vpes[i] = &vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
  226. ret = its_alloc_vcpu_irqs(&dist->its_vm);
  227. if (ret < 0) {
  228. kvm_err("VPE IRQ allocation failure\n");
  229. kfree(dist->its_vm.vpes);
  230. dist->its_vm.nr_vpes = 0;
  231. dist->its_vm.vpes = NULL;
  232. return ret;
  233. }
  234. kvm_for_each_vcpu(i, vcpu, kvm) {
  235. int irq = dist->its_vm.vpes[i]->irq;
  236. unsigned long irq_flags = DB_IRQ_FLAGS;
  237. /*
  238. * Don't automatically enable the doorbell, as we're
  239. * flipping it back and forth when the vcpu gets
  240. * blocked. Also disable the lazy disabling, as the
  241. * doorbell could kick us out of the guest too
  242. * early...
  243. *
  244. * On GICv4.1, the doorbell is managed in HW and must
  245. * be left enabled.
  246. */
  247. if (kvm_vgic_global_state.has_gicv4_1)
  248. irq_flags &= ~IRQ_NOAUTOEN;
  249. irq_set_status_flags(irq, irq_flags);
  250. ret = vgic_v4_request_vpe_irq(vcpu, irq);
  251. if (ret) {
  252. kvm_err("failed to allocate vcpu IRQ%d\n", irq);
  253. /*
  254. * Trick: adjust the number of vpes so we know
  255. * how many to nuke on teardown...
  256. */
  257. dist->its_vm.nr_vpes = i;
  258. break;
  259. }
  260. }
  261. if (ret)
  262. vgic_v4_teardown(kvm);
  263. return ret;
  264. }
  265. /**
  266. * vgic_v4_teardown - Free the GICv4 data structures
  267. * @kvm: Pointer to the VM being destroyed
  268. */
  269. void vgic_v4_teardown(struct kvm *kvm)
  270. {
  271. struct its_vm *its_vm = &kvm->arch.vgic.its_vm;
  272. int i;
  273. lockdep_assert_held(&kvm->arch.config_lock);
  274. if (!its_vm->vpes)
  275. return;
  276. for (i = 0; i < its_vm->nr_vpes; i++) {
  277. struct kvm_vcpu *vcpu = kvm_get_vcpu(kvm, i);
  278. int irq = its_vm->vpes[i]->irq;
  279. irq_clear_status_flags(irq, DB_IRQ_FLAGS);
  280. free_irq(irq, vcpu);
  281. }
  282. its_free_vcpu_irqs(its_vm);
  283. kfree(its_vm->vpes);
  284. its_vm->nr_vpes = 0;
  285. its_vm->vpes = NULL;
  286. }
  287. int vgic_v4_put(struct kvm_vcpu *vcpu)
  288. {
  289. struct its_vpe *vpe = &vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
  290. if (!vgic_supports_direct_msis(vcpu->kvm) || !vpe->resident)
  291. return 0;
  292. return its_make_vpe_non_resident(vpe, !!vcpu_get_flag(vcpu, IN_WFI));
  293. }
  294. int vgic_v4_load(struct kvm_vcpu *vcpu)
  295. {
  296. struct its_vpe *vpe = &vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
  297. int err;
  298. if (!vgic_supports_direct_msis(vcpu->kvm) || vpe->resident)
  299. return 0;
  300. if (vcpu_get_flag(vcpu, IN_WFI))
  301. return 0;
  302. /*
  303. * Before making the VPE resident, make sure the redistributor
  304. * corresponding to our current CPU expects us here. See the
  305. * doc in drivers/irqchip/irq-gic-v4.c to understand how this
  306. * turns into a VMOVP command at the ITS level.
  307. */
  308. err = irq_set_affinity(vpe->irq, cpumask_of(smp_processor_id()));
  309. if (err)
  310. return err;
  311. err = its_make_vpe_resident(vpe, false, vcpu->kvm->arch.vgic.enabled);
  312. if (err)
  313. return err;
  314. /*
  315. * Now that the VPE is resident, let's get rid of a potential
  316. * doorbell interrupt that would still be pending. This is a
  317. * GICv4.0 only "feature"...
  318. */
  319. if (!kvm_vgic_global_state.has_gicv4_1)
  320. err = irq_set_irqchip_state(vpe->irq, IRQCHIP_STATE_PENDING, false);
  321. return err;
  322. }
  323. void vgic_v4_commit(struct kvm_vcpu *vcpu)
  324. {
  325. struct its_vpe *vpe = &vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
  326. /*
  327. * No need to wait for the vPE to be ready across a shallow guest
  328. * exit, as only a vcpu_put will invalidate it.
  329. */
  330. if (!vpe->ready)
  331. its_commit_vpe(vpe);
  332. }
  333. static struct vgic_its *vgic_get_its(struct kvm *kvm,
  334. struct kvm_kernel_irq_routing_entry *irq_entry)
  335. {
  336. struct kvm_msi msi = (struct kvm_msi) {
  337. .address_lo = irq_entry->msi.address_lo,
  338. .address_hi = irq_entry->msi.address_hi,
  339. .data = irq_entry->msi.data,
  340. .flags = irq_entry->msi.flags,
  341. .devid = irq_entry->msi.devid,
  342. };
  343. return vgic_msi_to_its(kvm, &msi);
  344. }
  345. int kvm_vgic_v4_set_forwarding(struct kvm *kvm, int virq,
  346. struct kvm_kernel_irq_routing_entry *irq_entry)
  347. {
  348. struct vgic_its *its;
  349. struct vgic_irq *irq;
  350. struct its_vlpi_map map;
  351. unsigned long flags;
  352. int ret;
  353. if (!vgic_supports_direct_msis(kvm))
  354. return 0;
  355. /*
  356. * Get the ITS, and escape early on error (not a valid
  357. * doorbell for any of our vITSs).
  358. */
  359. its = vgic_get_its(kvm, irq_entry);
  360. if (IS_ERR(its))
  361. return 0;
  362. mutex_lock(&its->its_lock);
  363. /* Perform the actual DevID/EventID -> LPI translation. */
  364. ret = vgic_its_resolve_lpi(kvm, its, irq_entry->msi.devid,
  365. irq_entry->msi.data, &irq);
  366. if (ret)
  367. goto out;
  368. /*
  369. * Emit the mapping request. If it fails, the ITS probably
  370. * isn't v4 compatible, so let's silently bail out. Holding
  371. * the ITS lock should ensure that nothing can modify the
  372. * target vcpu.
  373. */
  374. map = (struct its_vlpi_map) {
  375. .vm = &kvm->arch.vgic.its_vm,
  376. .vpe = &irq->target_vcpu->arch.vgic_cpu.vgic_v3.its_vpe,
  377. .vintid = irq->intid,
  378. .properties = ((irq->priority & 0xfc) |
  379. (irq->enabled ? LPI_PROP_ENABLED : 0) |
  380. LPI_PROP_GROUP1),
  381. .db_enabled = true,
  382. };
  383. ret = its_map_vlpi(virq, &map);
  384. if (ret)
  385. goto out;
  386. irq->hw = true;
  387. irq->host_irq = virq;
  388. atomic_inc(&map.vpe->vlpi_count);
  389. /* Transfer pending state */
  390. raw_spin_lock_irqsave(&irq->irq_lock, flags);
  391. if (irq->pending_latch) {
  392. ret = irq_set_irqchip_state(irq->host_irq,
  393. IRQCHIP_STATE_PENDING,
  394. irq->pending_latch);
  395. WARN_RATELIMIT(ret, "IRQ %d", irq->host_irq);
  396. /*
  397. * Clear pending_latch and communicate this state
  398. * change via vgic_queue_irq_unlock.
  399. */
  400. irq->pending_latch = false;
  401. vgic_queue_irq_unlock(kvm, irq, flags);
  402. } else {
  403. raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
  404. }
  405. out:
  406. mutex_unlock(&its->its_lock);
  407. return ret;
  408. }
  409. int kvm_vgic_v4_unset_forwarding(struct kvm *kvm, int virq,
  410. struct kvm_kernel_irq_routing_entry *irq_entry)
  411. {
  412. struct vgic_its *its;
  413. struct vgic_irq *irq;
  414. int ret;
  415. if (!vgic_supports_direct_msis(kvm))
  416. return 0;
  417. /*
  418. * Get the ITS, and escape early on error (not a valid
  419. * doorbell for any of our vITSs).
  420. */
  421. its = vgic_get_its(kvm, irq_entry);
  422. if (IS_ERR(its))
  423. return 0;
  424. mutex_lock(&its->its_lock);
  425. ret = vgic_its_resolve_lpi(kvm, its, irq_entry->msi.devid,
  426. irq_entry->msi.data, &irq);
  427. if (ret)
  428. goto out;
  429. WARN_ON(!(irq->hw && irq->host_irq == virq));
  430. if (irq->hw) {
  431. atomic_dec(&irq->target_vcpu->arch.vgic_cpu.vgic_v3.its_vpe.vlpi_count);
  432. irq->hw = false;
  433. ret = its_unmap_vlpi(virq);
  434. }
  435. out:
  436. mutex_unlock(&its->its_lock);
  437. return ret;
  438. }