vgic.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. /*
  2. * Copyright (C) 2015, 2016 ARM Ltd.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <linux/interrupt.h>
  17. #include <linux/irq.h>
  18. #include <linux/kvm.h>
  19. #include <linux/kvm_host.h>
  20. #include <linux/list_sort.h>
  21. #include <linux/nospec.h>
  22. #include <asm/kvm_hyp.h>
  23. #include "vgic.h"
  24. #define CREATE_TRACE_POINTS
  25. #include "trace.h"
  26. struct vgic_global kvm_vgic_global_state __ro_after_init = {
  27. .gicv3_cpuif = STATIC_KEY_FALSE_INIT,
  28. };
  29. /*
  30. * Locking order is always:
  31. * kvm->lock (mutex)
  32. * its->cmd_lock (mutex)
  33. * its->its_lock (mutex)
  34. * vgic_cpu->ap_list_lock must be taken with IRQs disabled
  35. * kvm->lpi_list_lock must be taken with IRQs disabled
  36. * vgic_irq->irq_lock must be taken with IRQs disabled
  37. *
  38. * As the ap_list_lock might be taken from the timer interrupt handler,
  39. * we have to disable IRQs before taking this lock and everything lower
  40. * than it.
  41. *
  42. * If you need to take multiple locks, always take the upper lock first,
  43. * then the lower ones, e.g. first take the its_lock, then the irq_lock.
  44. * If you are already holding a lock and need to take a higher one, you
  45. * have to drop the lower ranking lock first and re-aquire it after having
  46. * taken the upper one.
  47. *
  48. * When taking more than one ap_list_lock at the same time, always take the
  49. * lowest numbered VCPU's ap_list_lock first, so:
  50. * vcpuX->vcpu_id < vcpuY->vcpu_id:
  51. * spin_lock(vcpuX->arch.vgic_cpu.ap_list_lock);
  52. * spin_lock(vcpuY->arch.vgic_cpu.ap_list_lock);
  53. *
  54. * Since the VGIC must support injecting virtual interrupts from ISRs, we have
  55. * to use the spin_lock_irqsave/spin_unlock_irqrestore versions of outer
  56. * spinlocks for any lock that may be taken while injecting an interrupt.
  57. */
  58. /*
  59. * Iterate over the VM's list of mapped LPIs to find the one with a
  60. * matching interrupt ID and return a reference to the IRQ structure.
  61. */
  62. static struct vgic_irq *vgic_get_lpi(struct kvm *kvm, u32 intid)
  63. {
  64. struct vgic_dist *dist = &kvm->arch.vgic;
  65. struct vgic_irq *irq = NULL;
  66. unsigned long flags;
  67. spin_lock_irqsave(&dist->lpi_list_lock, flags);
  68. list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
  69. if (irq->intid != intid)
  70. continue;
  71. /*
  72. * This increases the refcount, the caller is expected to
  73. * call vgic_put_irq() later once it's finished with the IRQ.
  74. */
  75. vgic_get_irq_kref(irq);
  76. goto out_unlock;
  77. }
  78. irq = NULL;
  79. out_unlock:
  80. spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
  81. return irq;
  82. }
  83. /*
  84. * This looks up the virtual interrupt ID to get the corresponding
  85. * struct vgic_irq. It also increases the refcount, so any caller is expected
  86. * to call vgic_put_irq() once it's finished with this IRQ.
  87. */
  88. struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,
  89. u32 intid)
  90. {
  91. /* SGIs and PPIs */
  92. if (intid <= VGIC_MAX_PRIVATE) {
  93. intid = array_index_nospec(intid, VGIC_MAX_PRIVATE);
  94. return &vcpu->arch.vgic_cpu.private_irqs[intid];
  95. }
  96. /* SPIs */
  97. if (intid <= VGIC_MAX_SPI) {
  98. intid = array_index_nospec(intid, VGIC_MAX_SPI);
  99. return &kvm->arch.vgic.spis[intid - VGIC_NR_PRIVATE_IRQS];
  100. }
  101. /* LPIs */
  102. if (intid >= VGIC_MIN_LPI)
  103. return vgic_get_lpi(kvm, intid);
  104. WARN(1, "Looking up struct vgic_irq for reserved INTID");
  105. return NULL;
  106. }
  107. /*
  108. * We can't do anything in here, because we lack the kvm pointer to
  109. * lock and remove the item from the lpi_list. So we keep this function
  110. * empty and use the return value of kref_put() to trigger the freeing.
  111. */
  112. static void vgic_irq_release(struct kref *ref)
  113. {
  114. }
  115. void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq)
  116. {
  117. struct vgic_dist *dist = &kvm->arch.vgic;
  118. unsigned long flags;
  119. if (irq->intid < VGIC_MIN_LPI)
  120. return;
  121. spin_lock_irqsave(&dist->lpi_list_lock, flags);
  122. if (!kref_put(&irq->refcount, vgic_irq_release)) {
  123. spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
  124. return;
  125. };
  126. list_del(&irq->lpi_list);
  127. dist->lpi_list_count--;
  128. spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
  129. kfree(irq);
  130. }
  131. void vgic_irq_set_phys_pending(struct vgic_irq *irq, bool pending)
  132. {
  133. WARN_ON(irq_set_irqchip_state(irq->host_irq,
  134. IRQCHIP_STATE_PENDING,
  135. pending));
  136. }
  137. bool vgic_get_phys_line_level(struct vgic_irq *irq)
  138. {
  139. bool line_level;
  140. BUG_ON(!irq->hw);
  141. if (irq->get_input_level)
  142. return irq->get_input_level(irq->intid);
  143. WARN_ON(irq_get_irqchip_state(irq->host_irq,
  144. IRQCHIP_STATE_PENDING,
  145. &line_level));
  146. return line_level;
  147. }
  148. /* Set/Clear the physical active state */
  149. void vgic_irq_set_phys_active(struct vgic_irq *irq, bool active)
  150. {
  151. BUG_ON(!irq->hw);
  152. WARN_ON(irq_set_irqchip_state(irq->host_irq,
  153. IRQCHIP_STATE_ACTIVE,
  154. active));
  155. }
  156. /**
  157. * kvm_vgic_target_oracle - compute the target vcpu for an irq
  158. *
  159. * @irq: The irq to route. Must be already locked.
  160. *
  161. * Based on the current state of the interrupt (enabled, pending,
  162. * active, vcpu and target_vcpu), compute the next vcpu this should be
  163. * given to. Return NULL if this shouldn't be injected at all.
  164. *
  165. * Requires the IRQ lock to be held.
  166. */
  167. static struct kvm_vcpu *vgic_target_oracle(struct vgic_irq *irq)
  168. {
  169. DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
  170. /* If the interrupt is active, it must stay on the current vcpu */
  171. if (irq->active)
  172. return irq->vcpu ? : irq->target_vcpu;
  173. /*
  174. * If the IRQ is not active but enabled and pending, we should direct
  175. * it to its configured target VCPU.
  176. * If the distributor is disabled, pending interrupts shouldn't be
  177. * forwarded.
  178. */
  179. if (irq->enabled && irq_is_pending(irq)) {
  180. if (unlikely(irq->target_vcpu &&
  181. !irq->target_vcpu->kvm->arch.vgic.enabled))
  182. return NULL;
  183. return irq->target_vcpu;
  184. }
  185. /* If neither active nor pending and enabled, then this IRQ should not
  186. * be queued to any VCPU.
  187. */
  188. return NULL;
  189. }
  190. /*
  191. * The order of items in the ap_lists defines how we'll pack things in LRs as
  192. * well, the first items in the list being the first things populated in the
  193. * LRs.
  194. *
  195. * A hard rule is that active interrupts can never be pushed out of the LRs
  196. * (and therefore take priority) since we cannot reliably trap on deactivation
  197. * of IRQs and therefore they have to be present in the LRs.
  198. *
  199. * Otherwise things should be sorted by the priority field and the GIC
  200. * hardware support will take care of preemption of priority groups etc.
  201. *
  202. * Return negative if "a" sorts before "b", 0 to preserve order, and positive
  203. * to sort "b" before "a".
  204. */
  205. static int vgic_irq_cmp(void *priv, struct list_head *a, struct list_head *b)
  206. {
  207. struct vgic_irq *irqa = container_of(a, struct vgic_irq, ap_list);
  208. struct vgic_irq *irqb = container_of(b, struct vgic_irq, ap_list);
  209. bool penda, pendb;
  210. int ret;
  211. spin_lock(&irqa->irq_lock);
  212. spin_lock_nested(&irqb->irq_lock, SINGLE_DEPTH_NESTING);
  213. if (irqa->active || irqb->active) {
  214. ret = (int)irqb->active - (int)irqa->active;
  215. goto out;
  216. }
  217. penda = irqa->enabled && irq_is_pending(irqa);
  218. pendb = irqb->enabled && irq_is_pending(irqb);
  219. if (!penda || !pendb) {
  220. ret = (int)pendb - (int)penda;
  221. goto out;
  222. }
  223. /* Both pending and enabled, sort by priority */
  224. ret = irqa->priority - irqb->priority;
  225. out:
  226. spin_unlock(&irqb->irq_lock);
  227. spin_unlock(&irqa->irq_lock);
  228. return ret;
  229. }
  230. /* Must be called with the ap_list_lock held */
  231. static void vgic_sort_ap_list(struct kvm_vcpu *vcpu)
  232. {
  233. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  234. DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
  235. list_sort(NULL, &vgic_cpu->ap_list_head, vgic_irq_cmp);
  236. }
  237. /*
  238. * Only valid injection if changing level for level-triggered IRQs or for a
  239. * rising edge, and in-kernel connected IRQ lines can only be controlled by
  240. * their owner.
  241. */
  242. static bool vgic_validate_injection(struct vgic_irq *irq, bool level, void *owner)
  243. {
  244. if (irq->owner != owner)
  245. return false;
  246. switch (irq->config) {
  247. case VGIC_CONFIG_LEVEL:
  248. return irq->line_level != level;
  249. case VGIC_CONFIG_EDGE:
  250. return level;
  251. }
  252. return false;
  253. }
  254. /*
  255. * Check whether an IRQ needs to (and can) be queued to a VCPU's ap list.
  256. * Do the queuing if necessary, taking the right locks in the right order.
  257. * Returns true when the IRQ was queued, false otherwise.
  258. *
  259. * Needs to be entered with the IRQ lock already held, but will return
  260. * with all locks dropped.
  261. */
  262. bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq,
  263. unsigned long flags)
  264. {
  265. struct kvm_vcpu *vcpu;
  266. DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
  267. retry:
  268. vcpu = vgic_target_oracle(irq);
  269. if (irq->vcpu || !vcpu) {
  270. /*
  271. * If this IRQ is already on a VCPU's ap_list, then it
  272. * cannot be moved or modified and there is no more work for
  273. * us to do.
  274. *
  275. * Otherwise, if the irq is not pending and enabled, it does
  276. * not need to be inserted into an ap_list and there is also
  277. * no more work for us to do.
  278. */
  279. spin_unlock_irqrestore(&irq->irq_lock, flags);
  280. /*
  281. * We have to kick the VCPU here, because we could be
  282. * queueing an edge-triggered interrupt for which we
  283. * get no EOI maintenance interrupt. In that case,
  284. * while the IRQ is already on the VCPU's AP list, the
  285. * VCPU could have EOI'ed the original interrupt and
  286. * won't see this one until it exits for some other
  287. * reason.
  288. */
  289. if (vcpu) {
  290. kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
  291. kvm_vcpu_kick(vcpu);
  292. }
  293. return false;
  294. }
  295. /*
  296. * We must unlock the irq lock to take the ap_list_lock where
  297. * we are going to insert this new pending interrupt.
  298. */
  299. spin_unlock_irqrestore(&irq->irq_lock, flags);
  300. /* someone can do stuff here, which we re-check below */
  301. spin_lock_irqsave(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
  302. spin_lock(&irq->irq_lock);
  303. /*
  304. * Did something change behind our backs?
  305. *
  306. * There are two cases:
  307. * 1) The irq lost its pending state or was disabled behind our
  308. * backs and/or it was queued to another VCPU's ap_list.
  309. * 2) Someone changed the affinity on this irq behind our
  310. * backs and we are now holding the wrong ap_list_lock.
  311. *
  312. * In both cases, drop the locks and retry.
  313. */
  314. if (unlikely(irq->vcpu || vcpu != vgic_target_oracle(irq))) {
  315. spin_unlock(&irq->irq_lock);
  316. spin_unlock_irqrestore(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
  317. spin_lock_irqsave(&irq->irq_lock, flags);
  318. goto retry;
  319. }
  320. /*
  321. * Grab a reference to the irq to reflect the fact that it is
  322. * now in the ap_list.
  323. */
  324. vgic_get_irq_kref(irq);
  325. list_add_tail(&irq->ap_list, &vcpu->arch.vgic_cpu.ap_list_head);
  326. irq->vcpu = vcpu;
  327. spin_unlock(&irq->irq_lock);
  328. spin_unlock_irqrestore(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
  329. kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
  330. kvm_vcpu_kick(vcpu);
  331. return true;
  332. }
  333. /**
  334. * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
  335. * @kvm: The VM structure pointer
  336. * @cpuid: The CPU for PPIs
  337. * @intid: The INTID to inject a new state to.
  338. * @level: Edge-triggered: true: to trigger the interrupt
  339. * false: to ignore the call
  340. * Level-sensitive true: raise the input signal
  341. * false: lower the input signal
  342. * @owner: The opaque pointer to the owner of the IRQ being raised to verify
  343. * that the caller is allowed to inject this IRQ. Userspace
  344. * injections will have owner == NULL.
  345. *
  346. * The VGIC is not concerned with devices being active-LOW or active-HIGH for
  347. * level-sensitive interrupts. You can think of the level parameter as 1
  348. * being HIGH and 0 being LOW and all devices being active-HIGH.
  349. */
  350. int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,
  351. bool level, void *owner)
  352. {
  353. struct kvm_vcpu *vcpu;
  354. struct vgic_irq *irq;
  355. unsigned long flags;
  356. int ret;
  357. trace_vgic_update_irq_pending(cpuid, intid, level);
  358. ret = vgic_lazy_init(kvm);
  359. if (ret)
  360. return ret;
  361. vcpu = kvm_get_vcpu(kvm, cpuid);
  362. if (!vcpu && intid < VGIC_NR_PRIVATE_IRQS)
  363. return -EINVAL;
  364. irq = vgic_get_irq(kvm, vcpu, intid);
  365. if (!irq)
  366. return -EINVAL;
  367. spin_lock_irqsave(&irq->irq_lock, flags);
  368. if (!vgic_validate_injection(irq, level, owner)) {
  369. /* Nothing to see here, move along... */
  370. spin_unlock_irqrestore(&irq->irq_lock, flags);
  371. vgic_put_irq(kvm, irq);
  372. return 0;
  373. }
  374. if (irq->config == VGIC_CONFIG_LEVEL)
  375. irq->line_level = level;
  376. else
  377. irq->pending_latch = true;
  378. vgic_queue_irq_unlock(kvm, irq, flags);
  379. vgic_put_irq(kvm, irq);
  380. return 0;
  381. }
  382. /* @irq->irq_lock must be held */
  383. static int kvm_vgic_map_irq(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
  384. unsigned int host_irq,
  385. bool (*get_input_level)(int vindid))
  386. {
  387. struct irq_desc *desc;
  388. struct irq_data *data;
  389. /*
  390. * Find the physical IRQ number corresponding to @host_irq
  391. */
  392. desc = irq_to_desc(host_irq);
  393. if (!desc) {
  394. kvm_err("%s: no interrupt descriptor\n", __func__);
  395. return -EINVAL;
  396. }
  397. data = irq_desc_get_irq_data(desc);
  398. while (data->parent_data)
  399. data = data->parent_data;
  400. irq->hw = true;
  401. irq->host_irq = host_irq;
  402. irq->hwintid = data->hwirq;
  403. irq->get_input_level = get_input_level;
  404. return 0;
  405. }
  406. /* @irq->irq_lock must be held */
  407. static inline void kvm_vgic_unmap_irq(struct vgic_irq *irq)
  408. {
  409. irq->hw = false;
  410. irq->hwintid = 0;
  411. irq->get_input_level = NULL;
  412. }
  413. int kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, unsigned int host_irq,
  414. u32 vintid, bool (*get_input_level)(int vindid))
  415. {
  416. struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
  417. unsigned long flags;
  418. int ret;
  419. BUG_ON(!irq);
  420. spin_lock_irqsave(&irq->irq_lock, flags);
  421. ret = kvm_vgic_map_irq(vcpu, irq, host_irq, get_input_level);
  422. spin_unlock_irqrestore(&irq->irq_lock, flags);
  423. vgic_put_irq(vcpu->kvm, irq);
  424. return ret;
  425. }
  426. /**
  427. * kvm_vgic_reset_mapped_irq - Reset a mapped IRQ
  428. * @vcpu: The VCPU pointer
  429. * @vintid: The INTID of the interrupt
  430. *
  431. * Reset the active and pending states of a mapped interrupt. Kernel
  432. * subsystems injecting mapped interrupts should reset their interrupt lines
  433. * when we are doing a reset of the VM.
  434. */
  435. void kvm_vgic_reset_mapped_irq(struct kvm_vcpu *vcpu, u32 vintid)
  436. {
  437. struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
  438. unsigned long flags;
  439. if (!irq->hw)
  440. goto out;
  441. spin_lock_irqsave(&irq->irq_lock, flags);
  442. irq->active = false;
  443. irq->pending_latch = false;
  444. irq->line_level = false;
  445. spin_unlock_irqrestore(&irq->irq_lock, flags);
  446. out:
  447. vgic_put_irq(vcpu->kvm, irq);
  448. }
  449. int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int vintid)
  450. {
  451. struct vgic_irq *irq;
  452. unsigned long flags;
  453. if (!vgic_initialized(vcpu->kvm))
  454. return -EAGAIN;
  455. irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
  456. BUG_ON(!irq);
  457. spin_lock_irqsave(&irq->irq_lock, flags);
  458. kvm_vgic_unmap_irq(irq);
  459. spin_unlock_irqrestore(&irq->irq_lock, flags);
  460. vgic_put_irq(vcpu->kvm, irq);
  461. return 0;
  462. }
  463. /**
  464. * kvm_vgic_set_owner - Set the owner of an interrupt for a VM
  465. *
  466. * @vcpu: Pointer to the VCPU (used for PPIs)
  467. * @intid: The virtual INTID identifying the interrupt (PPI or SPI)
  468. * @owner: Opaque pointer to the owner
  469. *
  470. * Returns 0 if intid is not already used by another in-kernel device and the
  471. * owner is set, otherwise returns an error code.
  472. */
  473. int kvm_vgic_set_owner(struct kvm_vcpu *vcpu, unsigned int intid, void *owner)
  474. {
  475. struct vgic_irq *irq;
  476. unsigned long flags;
  477. int ret = 0;
  478. if (!vgic_initialized(vcpu->kvm))
  479. return -EAGAIN;
  480. /* SGIs and LPIs cannot be wired up to any device */
  481. if (!irq_is_ppi(intid) && !vgic_valid_spi(vcpu->kvm, intid))
  482. return -EINVAL;
  483. irq = vgic_get_irq(vcpu->kvm, vcpu, intid);
  484. spin_lock_irqsave(&irq->irq_lock, flags);
  485. if (irq->owner && irq->owner != owner)
  486. ret = -EEXIST;
  487. else
  488. irq->owner = owner;
  489. spin_unlock_irqrestore(&irq->irq_lock, flags);
  490. return ret;
  491. }
  492. /**
  493. * vgic_prune_ap_list - Remove non-relevant interrupts from the list
  494. *
  495. * @vcpu: The VCPU pointer
  496. *
  497. * Go over the list of "interesting" interrupts, and prune those that we
  498. * won't have to consider in the near future.
  499. */
  500. static void vgic_prune_ap_list(struct kvm_vcpu *vcpu)
  501. {
  502. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  503. struct vgic_irq *irq, *tmp;
  504. DEBUG_SPINLOCK_BUG_ON(!irqs_disabled());
  505. retry:
  506. spin_lock(&vgic_cpu->ap_list_lock);
  507. list_for_each_entry_safe(irq, tmp, &vgic_cpu->ap_list_head, ap_list) {
  508. struct kvm_vcpu *target_vcpu, *vcpuA, *vcpuB;
  509. bool target_vcpu_needs_kick = false;
  510. spin_lock(&irq->irq_lock);
  511. BUG_ON(vcpu != irq->vcpu);
  512. target_vcpu = vgic_target_oracle(irq);
  513. if (!target_vcpu) {
  514. /*
  515. * We don't need to process this interrupt any
  516. * further, move it off the list.
  517. */
  518. list_del(&irq->ap_list);
  519. irq->vcpu = NULL;
  520. spin_unlock(&irq->irq_lock);
  521. /*
  522. * This vgic_put_irq call matches the
  523. * vgic_get_irq_kref in vgic_queue_irq_unlock,
  524. * where we added the LPI to the ap_list. As
  525. * we remove the irq from the list, we drop
  526. * also drop the refcount.
  527. */
  528. vgic_put_irq(vcpu->kvm, irq);
  529. continue;
  530. }
  531. if (target_vcpu == vcpu) {
  532. /* We're on the right CPU */
  533. spin_unlock(&irq->irq_lock);
  534. continue;
  535. }
  536. /* This interrupt looks like it has to be migrated. */
  537. spin_unlock(&irq->irq_lock);
  538. spin_unlock(&vgic_cpu->ap_list_lock);
  539. /*
  540. * Ensure locking order by always locking the smallest
  541. * ID first.
  542. */
  543. if (vcpu->vcpu_id < target_vcpu->vcpu_id) {
  544. vcpuA = vcpu;
  545. vcpuB = target_vcpu;
  546. } else {
  547. vcpuA = target_vcpu;
  548. vcpuB = vcpu;
  549. }
  550. spin_lock(&vcpuA->arch.vgic_cpu.ap_list_lock);
  551. spin_lock_nested(&vcpuB->arch.vgic_cpu.ap_list_lock,
  552. SINGLE_DEPTH_NESTING);
  553. spin_lock(&irq->irq_lock);
  554. /*
  555. * If the affinity has been preserved, move the
  556. * interrupt around. Otherwise, it means things have
  557. * changed while the interrupt was unlocked, and we
  558. * need to replay this.
  559. *
  560. * In all cases, we cannot trust the list not to have
  561. * changed, so we restart from the beginning.
  562. */
  563. if (target_vcpu == vgic_target_oracle(irq)) {
  564. struct vgic_cpu *new_cpu = &target_vcpu->arch.vgic_cpu;
  565. list_del(&irq->ap_list);
  566. irq->vcpu = target_vcpu;
  567. list_add_tail(&irq->ap_list, &new_cpu->ap_list_head);
  568. target_vcpu_needs_kick = true;
  569. }
  570. spin_unlock(&irq->irq_lock);
  571. spin_unlock(&vcpuB->arch.vgic_cpu.ap_list_lock);
  572. spin_unlock(&vcpuA->arch.vgic_cpu.ap_list_lock);
  573. if (target_vcpu_needs_kick) {
  574. kvm_make_request(KVM_REQ_IRQ_PENDING, target_vcpu);
  575. kvm_vcpu_kick(target_vcpu);
  576. }
  577. goto retry;
  578. }
  579. spin_unlock(&vgic_cpu->ap_list_lock);
  580. }
  581. static inline void vgic_fold_lr_state(struct kvm_vcpu *vcpu)
  582. {
  583. if (kvm_vgic_global_state.type == VGIC_V2)
  584. vgic_v2_fold_lr_state(vcpu);
  585. else
  586. vgic_v3_fold_lr_state(vcpu);
  587. }
  588. /* Requires the irq_lock to be held. */
  589. static inline void vgic_populate_lr(struct kvm_vcpu *vcpu,
  590. struct vgic_irq *irq, int lr)
  591. {
  592. DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
  593. if (kvm_vgic_global_state.type == VGIC_V2)
  594. vgic_v2_populate_lr(vcpu, irq, lr);
  595. else
  596. vgic_v3_populate_lr(vcpu, irq, lr);
  597. }
  598. static inline void vgic_clear_lr(struct kvm_vcpu *vcpu, int lr)
  599. {
  600. if (kvm_vgic_global_state.type == VGIC_V2)
  601. vgic_v2_clear_lr(vcpu, lr);
  602. else
  603. vgic_v3_clear_lr(vcpu, lr);
  604. }
  605. static inline void vgic_set_underflow(struct kvm_vcpu *vcpu)
  606. {
  607. if (kvm_vgic_global_state.type == VGIC_V2)
  608. vgic_v2_set_underflow(vcpu);
  609. else
  610. vgic_v3_set_underflow(vcpu);
  611. }
  612. /* Requires the ap_list_lock to be held. */
  613. static int compute_ap_list_depth(struct kvm_vcpu *vcpu,
  614. bool *multi_sgi)
  615. {
  616. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  617. struct vgic_irq *irq;
  618. int count = 0;
  619. *multi_sgi = false;
  620. DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
  621. list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
  622. int w;
  623. spin_lock(&irq->irq_lock);
  624. /* GICv2 SGIs can count for more than one... */
  625. w = vgic_irq_get_lr_count(irq);
  626. spin_unlock(&irq->irq_lock);
  627. count += w;
  628. *multi_sgi |= (w > 1);
  629. }
  630. return count;
  631. }
  632. /* Requires the VCPU's ap_list_lock to be held. */
  633. static void vgic_flush_lr_state(struct kvm_vcpu *vcpu)
  634. {
  635. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  636. struct vgic_irq *irq;
  637. int count;
  638. bool multi_sgi;
  639. u8 prio = 0xff;
  640. DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
  641. count = compute_ap_list_depth(vcpu, &multi_sgi);
  642. if (count > kvm_vgic_global_state.nr_lr || multi_sgi)
  643. vgic_sort_ap_list(vcpu);
  644. count = 0;
  645. list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
  646. spin_lock(&irq->irq_lock);
  647. /*
  648. * If we have multi-SGIs in the pipeline, we need to
  649. * guarantee that they are all seen before any IRQ of
  650. * lower priority. In that case, we need to filter out
  651. * these interrupts by exiting early. This is easy as
  652. * the AP list has been sorted already.
  653. */
  654. if (multi_sgi && irq->priority > prio) {
  655. spin_unlock(&irq->irq_lock);
  656. break;
  657. }
  658. if (likely(vgic_target_oracle(irq) == vcpu)) {
  659. vgic_populate_lr(vcpu, irq, count++);
  660. if (irq->source)
  661. prio = irq->priority;
  662. }
  663. spin_unlock(&irq->irq_lock);
  664. if (count == kvm_vgic_global_state.nr_lr) {
  665. if (!list_is_last(&irq->ap_list,
  666. &vgic_cpu->ap_list_head))
  667. vgic_set_underflow(vcpu);
  668. break;
  669. }
  670. }
  671. vcpu->arch.vgic_cpu.used_lrs = count;
  672. /* Nuke remaining LRs */
  673. for ( ; count < kvm_vgic_global_state.nr_lr; count++)
  674. vgic_clear_lr(vcpu, count);
  675. }
  676. static inline bool can_access_vgic_from_kernel(void)
  677. {
  678. /*
  679. * GICv2 can always be accessed from the kernel because it is
  680. * memory-mapped, and VHE systems can access GICv3 EL2 system
  681. * registers.
  682. */
  683. return !static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif) || has_vhe();
  684. }
  685. static inline void vgic_save_state(struct kvm_vcpu *vcpu)
  686. {
  687. if (!static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
  688. vgic_v2_save_state(vcpu);
  689. else
  690. __vgic_v3_save_state(vcpu);
  691. }
  692. /* Sync back the hardware VGIC state into our emulation after a guest's run. */
  693. void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
  694. {
  695. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  696. WARN_ON(vgic_v4_sync_hwstate(vcpu));
  697. /* An empty ap_list_head implies used_lrs == 0 */
  698. if (list_empty(&vcpu->arch.vgic_cpu.ap_list_head))
  699. return;
  700. if (can_access_vgic_from_kernel())
  701. vgic_save_state(vcpu);
  702. if (vgic_cpu->used_lrs)
  703. vgic_fold_lr_state(vcpu);
  704. vgic_prune_ap_list(vcpu);
  705. }
  706. static inline void vgic_restore_state(struct kvm_vcpu *vcpu)
  707. {
  708. if (!static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
  709. vgic_v2_restore_state(vcpu);
  710. else
  711. __vgic_v3_restore_state(vcpu);
  712. }
  713. /* Flush our emulation state into the GIC hardware before entering the guest. */
  714. void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
  715. {
  716. WARN_ON(vgic_v4_flush_hwstate(vcpu));
  717. /*
  718. * If there are no virtual interrupts active or pending for this
  719. * VCPU, then there is no work to do and we can bail out without
  720. * taking any lock. There is a potential race with someone injecting
  721. * interrupts to the VCPU, but it is a benign race as the VCPU will
  722. * either observe the new interrupt before or after doing this check,
  723. * and introducing additional synchronization mechanism doesn't change
  724. * this.
  725. */
  726. if (list_empty(&vcpu->arch.vgic_cpu.ap_list_head))
  727. return;
  728. DEBUG_SPINLOCK_BUG_ON(!irqs_disabled());
  729. spin_lock(&vcpu->arch.vgic_cpu.ap_list_lock);
  730. vgic_flush_lr_state(vcpu);
  731. spin_unlock(&vcpu->arch.vgic_cpu.ap_list_lock);
  732. if (can_access_vgic_from_kernel())
  733. vgic_restore_state(vcpu);
  734. }
  735. void kvm_vgic_load(struct kvm_vcpu *vcpu)
  736. {
  737. if (unlikely(!vgic_initialized(vcpu->kvm)))
  738. return;
  739. if (kvm_vgic_global_state.type == VGIC_V2)
  740. vgic_v2_load(vcpu);
  741. else
  742. vgic_v3_load(vcpu);
  743. }
  744. void kvm_vgic_put(struct kvm_vcpu *vcpu)
  745. {
  746. if (unlikely(!vgic_initialized(vcpu->kvm)))
  747. return;
  748. if (kvm_vgic_global_state.type == VGIC_V2)
  749. vgic_v2_put(vcpu);
  750. else
  751. vgic_v3_put(vcpu);
  752. }
  753. int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
  754. {
  755. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  756. struct vgic_irq *irq;
  757. bool pending = false;
  758. unsigned long flags;
  759. if (!vcpu->kvm->arch.vgic.enabled)
  760. return false;
  761. if (vcpu->arch.vgic_cpu.vgic_v3.its_vpe.pending_last)
  762. return true;
  763. spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
  764. list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
  765. spin_lock(&irq->irq_lock);
  766. pending = irq_is_pending(irq) && irq->enabled;
  767. spin_unlock(&irq->irq_lock);
  768. if (pending)
  769. break;
  770. }
  771. spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
  772. return pending;
  773. }
  774. void vgic_kick_vcpus(struct kvm *kvm)
  775. {
  776. struct kvm_vcpu *vcpu;
  777. int c;
  778. /*
  779. * We've injected an interrupt, time to find out who deserves
  780. * a good kick...
  781. */
  782. kvm_for_each_vcpu(c, vcpu, kvm) {
  783. if (kvm_vgic_vcpu_pending_irq(vcpu)) {
  784. kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
  785. kvm_vcpu_kick(vcpu);
  786. }
  787. }
  788. }
  789. bool kvm_vgic_map_is_active(struct kvm_vcpu *vcpu, unsigned int vintid)
  790. {
  791. struct vgic_irq *irq;
  792. bool map_is_active;
  793. unsigned long flags;
  794. if (!vgic_initialized(vcpu->kvm))
  795. return false;
  796. irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
  797. spin_lock_irqsave(&irq->irq_lock, flags);
  798. map_is_active = irq->hw && irq->active;
  799. spin_unlock_irqrestore(&irq->irq_lock, flags);
  800. vgic_put_irq(vcpu->kvm, irq);
  801. return map_is_active;
  802. }