vgic.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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/kvm.h>
  17. #include <linux/kvm_host.h>
  18. #include <linux/list_sort.h>
  19. #include "vgic.h"
  20. #define CREATE_TRACE_POINTS
  21. #include "../trace.h"
  22. #ifdef CONFIG_DEBUG_SPINLOCK
  23. #define DEBUG_SPINLOCK_BUG_ON(p) BUG_ON(p)
  24. #else
  25. #define DEBUG_SPINLOCK_BUG_ON(p)
  26. #endif
  27. struct vgic_global __section(.hyp.text) kvm_vgic_global_state = {.gicv3_cpuif = STATIC_KEY_FALSE_INIT,};
  28. /*
  29. * Locking order is always:
  30. * its->cmd_lock (mutex)
  31. * its->its_lock (mutex)
  32. * vgic_cpu->ap_list_lock
  33. * kvm->lpi_list_lock
  34. * vgic_irq->irq_lock
  35. *
  36. * If you need to take multiple locks, always take the upper lock first,
  37. * then the lower ones, e.g. first take the its_lock, then the irq_lock.
  38. * If you are already holding a lock and need to take a higher one, you
  39. * have to drop the lower ranking lock first and re-aquire it after having
  40. * taken the upper one.
  41. *
  42. * When taking more than one ap_list_lock at the same time, always take the
  43. * lowest numbered VCPU's ap_list_lock first, so:
  44. * vcpuX->vcpu_id < vcpuY->vcpu_id:
  45. * spin_lock(vcpuX->arch.vgic_cpu.ap_list_lock);
  46. * spin_lock(vcpuY->arch.vgic_cpu.ap_list_lock);
  47. */
  48. /*
  49. * Iterate over the VM's list of mapped LPIs to find the one with a
  50. * matching interrupt ID and return a reference to the IRQ structure.
  51. */
  52. static struct vgic_irq *vgic_get_lpi(struct kvm *kvm, u32 intid)
  53. {
  54. struct vgic_dist *dist = &kvm->arch.vgic;
  55. struct vgic_irq *irq = NULL;
  56. spin_lock(&dist->lpi_list_lock);
  57. list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
  58. if (irq->intid != intid)
  59. continue;
  60. /*
  61. * This increases the refcount, the caller is expected to
  62. * call vgic_put_irq() later once it's finished with the IRQ.
  63. */
  64. vgic_get_irq_kref(irq);
  65. goto out_unlock;
  66. }
  67. irq = NULL;
  68. out_unlock:
  69. spin_unlock(&dist->lpi_list_lock);
  70. return irq;
  71. }
  72. /*
  73. * This looks up the virtual interrupt ID to get the corresponding
  74. * struct vgic_irq. It also increases the refcount, so any caller is expected
  75. * to call vgic_put_irq() once it's finished with this IRQ.
  76. */
  77. struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,
  78. u32 intid)
  79. {
  80. /* SGIs and PPIs */
  81. if (intid <= VGIC_MAX_PRIVATE)
  82. return &vcpu->arch.vgic_cpu.private_irqs[intid];
  83. /* SPIs */
  84. if (intid <= VGIC_MAX_SPI)
  85. return &kvm->arch.vgic.spis[intid - VGIC_NR_PRIVATE_IRQS];
  86. /* LPIs */
  87. if (intid >= VGIC_MIN_LPI)
  88. return vgic_get_lpi(kvm, intid);
  89. WARN(1, "Looking up struct vgic_irq for reserved INTID");
  90. return NULL;
  91. }
  92. /*
  93. * We can't do anything in here, because we lack the kvm pointer to
  94. * lock and remove the item from the lpi_list. So we keep this function
  95. * empty and use the return value of kref_put() to trigger the freeing.
  96. */
  97. static void vgic_irq_release(struct kref *ref)
  98. {
  99. }
  100. void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq)
  101. {
  102. struct vgic_dist *dist = &kvm->arch.vgic;
  103. if (irq->intid < VGIC_MIN_LPI)
  104. return;
  105. spin_lock(&dist->lpi_list_lock);
  106. if (!kref_put(&irq->refcount, vgic_irq_release)) {
  107. spin_unlock(&dist->lpi_list_lock);
  108. return;
  109. };
  110. list_del(&irq->lpi_list);
  111. dist->lpi_list_count--;
  112. spin_unlock(&dist->lpi_list_lock);
  113. kfree(irq);
  114. }
  115. /**
  116. * kvm_vgic_target_oracle - compute the target vcpu for an irq
  117. *
  118. * @irq: The irq to route. Must be already locked.
  119. *
  120. * Based on the current state of the interrupt (enabled, pending,
  121. * active, vcpu and target_vcpu), compute the next vcpu this should be
  122. * given to. Return NULL if this shouldn't be injected at all.
  123. *
  124. * Requires the IRQ lock to be held.
  125. */
  126. static struct kvm_vcpu *vgic_target_oracle(struct vgic_irq *irq)
  127. {
  128. DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
  129. /* If the interrupt is active, it must stay on the current vcpu */
  130. if (irq->active)
  131. return irq->vcpu ? : irq->target_vcpu;
  132. /*
  133. * If the IRQ is not active but enabled and pending, we should direct
  134. * it to its configured target VCPU.
  135. * If the distributor is disabled, pending interrupts shouldn't be
  136. * forwarded.
  137. */
  138. if (irq->enabled && irq->pending) {
  139. if (unlikely(irq->target_vcpu &&
  140. !irq->target_vcpu->kvm->arch.vgic.enabled))
  141. return NULL;
  142. return irq->target_vcpu;
  143. }
  144. /* If neither active nor pending and enabled, then this IRQ should not
  145. * be queued to any VCPU.
  146. */
  147. return NULL;
  148. }
  149. /*
  150. * The order of items in the ap_lists defines how we'll pack things in LRs as
  151. * well, the first items in the list being the first things populated in the
  152. * LRs.
  153. *
  154. * A hard rule is that active interrupts can never be pushed out of the LRs
  155. * (and therefore take priority) since we cannot reliably trap on deactivation
  156. * of IRQs and therefore they have to be present in the LRs.
  157. *
  158. * Otherwise things should be sorted by the priority field and the GIC
  159. * hardware support will take care of preemption of priority groups etc.
  160. *
  161. * Return negative if "a" sorts before "b", 0 to preserve order, and positive
  162. * to sort "b" before "a".
  163. */
  164. static int vgic_irq_cmp(void *priv, struct list_head *a, struct list_head *b)
  165. {
  166. struct vgic_irq *irqa = container_of(a, struct vgic_irq, ap_list);
  167. struct vgic_irq *irqb = container_of(b, struct vgic_irq, ap_list);
  168. bool penda, pendb;
  169. int ret;
  170. spin_lock(&irqa->irq_lock);
  171. spin_lock_nested(&irqb->irq_lock, SINGLE_DEPTH_NESTING);
  172. if (irqa->active || irqb->active) {
  173. ret = (int)irqb->active - (int)irqa->active;
  174. goto out;
  175. }
  176. penda = irqa->enabled && irqa->pending;
  177. pendb = irqb->enabled && irqb->pending;
  178. if (!penda || !pendb) {
  179. ret = (int)pendb - (int)penda;
  180. goto out;
  181. }
  182. /* Both pending and enabled, sort by priority */
  183. ret = irqa->priority - irqb->priority;
  184. out:
  185. spin_unlock(&irqb->irq_lock);
  186. spin_unlock(&irqa->irq_lock);
  187. return ret;
  188. }
  189. /* Must be called with the ap_list_lock held */
  190. static void vgic_sort_ap_list(struct kvm_vcpu *vcpu)
  191. {
  192. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  193. DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
  194. list_sort(NULL, &vgic_cpu->ap_list_head, vgic_irq_cmp);
  195. }
  196. /*
  197. * Only valid injection if changing level for level-triggered IRQs or for a
  198. * rising edge.
  199. */
  200. static bool vgic_validate_injection(struct vgic_irq *irq, bool level)
  201. {
  202. switch (irq->config) {
  203. case VGIC_CONFIG_LEVEL:
  204. return irq->line_level != level;
  205. case VGIC_CONFIG_EDGE:
  206. return level;
  207. }
  208. return false;
  209. }
  210. /*
  211. * Check whether an IRQ needs to (and can) be queued to a VCPU's ap list.
  212. * Do the queuing if necessary, taking the right locks in the right order.
  213. * Returns true when the IRQ was queued, false otherwise.
  214. *
  215. * Needs to be entered with the IRQ lock already held, but will return
  216. * with all locks dropped.
  217. */
  218. bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq)
  219. {
  220. struct kvm_vcpu *vcpu;
  221. DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
  222. retry:
  223. vcpu = vgic_target_oracle(irq);
  224. if (irq->vcpu || !vcpu) {
  225. /*
  226. * If this IRQ is already on a VCPU's ap_list, then it
  227. * cannot be moved or modified and there is no more work for
  228. * us to do.
  229. *
  230. * Otherwise, if the irq is not pending and enabled, it does
  231. * not need to be inserted into an ap_list and there is also
  232. * no more work for us to do.
  233. */
  234. spin_unlock(&irq->irq_lock);
  235. /*
  236. * We have to kick the VCPU here, because we could be
  237. * queueing an edge-triggered interrupt for which we
  238. * get no EOI maintenance interrupt. In that case,
  239. * while the IRQ is already on the VCPU's AP list, the
  240. * VCPU could have EOI'ed the original interrupt and
  241. * won't see this one until it exits for some other
  242. * reason.
  243. */
  244. if (vcpu)
  245. kvm_vcpu_kick(vcpu);
  246. return false;
  247. }
  248. /*
  249. * We must unlock the irq lock to take the ap_list_lock where
  250. * we are going to insert this new pending interrupt.
  251. */
  252. spin_unlock(&irq->irq_lock);
  253. /* someone can do stuff here, which we re-check below */
  254. spin_lock(&vcpu->arch.vgic_cpu.ap_list_lock);
  255. spin_lock(&irq->irq_lock);
  256. /*
  257. * Did something change behind our backs?
  258. *
  259. * There are two cases:
  260. * 1) The irq lost its pending state or was disabled behind our
  261. * backs and/or it was queued to another VCPU's ap_list.
  262. * 2) Someone changed the affinity on this irq behind our
  263. * backs and we are now holding the wrong ap_list_lock.
  264. *
  265. * In both cases, drop the locks and retry.
  266. */
  267. if (unlikely(irq->vcpu || vcpu != vgic_target_oracle(irq))) {
  268. spin_unlock(&irq->irq_lock);
  269. spin_unlock(&vcpu->arch.vgic_cpu.ap_list_lock);
  270. spin_lock(&irq->irq_lock);
  271. goto retry;
  272. }
  273. /*
  274. * Grab a reference to the irq to reflect the fact that it is
  275. * now in the ap_list.
  276. */
  277. vgic_get_irq_kref(irq);
  278. list_add_tail(&irq->ap_list, &vcpu->arch.vgic_cpu.ap_list_head);
  279. irq->vcpu = vcpu;
  280. spin_unlock(&irq->irq_lock);
  281. spin_unlock(&vcpu->arch.vgic_cpu.ap_list_lock);
  282. kvm_vcpu_kick(vcpu);
  283. return true;
  284. }
  285. static int vgic_update_irq_pending(struct kvm *kvm, int cpuid,
  286. unsigned int intid, bool level,
  287. bool mapped_irq)
  288. {
  289. struct kvm_vcpu *vcpu;
  290. struct vgic_irq *irq;
  291. int ret;
  292. trace_vgic_update_irq_pending(cpuid, intid, level);
  293. ret = vgic_lazy_init(kvm);
  294. if (ret)
  295. return ret;
  296. vcpu = kvm_get_vcpu(kvm, cpuid);
  297. if (!vcpu && intid < VGIC_NR_PRIVATE_IRQS)
  298. return -EINVAL;
  299. irq = vgic_get_irq(kvm, vcpu, intid);
  300. if (!irq)
  301. return -EINVAL;
  302. if (irq->hw != mapped_irq) {
  303. vgic_put_irq(kvm, irq);
  304. return -EINVAL;
  305. }
  306. spin_lock(&irq->irq_lock);
  307. if (!vgic_validate_injection(irq, level)) {
  308. /* Nothing to see here, move along... */
  309. spin_unlock(&irq->irq_lock);
  310. vgic_put_irq(kvm, irq);
  311. return 0;
  312. }
  313. if (irq->config == VGIC_CONFIG_LEVEL) {
  314. irq->line_level = level;
  315. irq->pending = level || irq->soft_pending;
  316. } else {
  317. irq->pending = true;
  318. }
  319. vgic_queue_irq_unlock(kvm, irq);
  320. vgic_put_irq(kvm, irq);
  321. return 0;
  322. }
  323. /**
  324. * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
  325. * @kvm: The VM structure pointer
  326. * @cpuid: The CPU for PPIs
  327. * @intid: The INTID to inject a new state to.
  328. * @level: Edge-triggered: true: to trigger the interrupt
  329. * false: to ignore the call
  330. * Level-sensitive true: raise the input signal
  331. * false: lower the input signal
  332. *
  333. * The VGIC is not concerned with devices being active-LOW or active-HIGH for
  334. * level-sensitive interrupts. You can think of the level parameter as 1
  335. * being HIGH and 0 being LOW and all devices being active-HIGH.
  336. */
  337. int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,
  338. bool level)
  339. {
  340. return vgic_update_irq_pending(kvm, cpuid, intid, level, false);
  341. }
  342. int kvm_vgic_inject_mapped_irq(struct kvm *kvm, int cpuid, unsigned int intid,
  343. bool level)
  344. {
  345. return vgic_update_irq_pending(kvm, cpuid, intid, level, true);
  346. }
  347. int kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, u32 virt_irq, u32 phys_irq)
  348. {
  349. struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, virt_irq);
  350. BUG_ON(!irq);
  351. spin_lock(&irq->irq_lock);
  352. irq->hw = true;
  353. irq->hwintid = phys_irq;
  354. spin_unlock(&irq->irq_lock);
  355. vgic_put_irq(vcpu->kvm, irq);
  356. return 0;
  357. }
  358. int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int virt_irq)
  359. {
  360. struct vgic_irq *irq;
  361. if (!vgic_initialized(vcpu->kvm))
  362. return -EAGAIN;
  363. irq = vgic_get_irq(vcpu->kvm, vcpu, virt_irq);
  364. BUG_ON(!irq);
  365. spin_lock(&irq->irq_lock);
  366. irq->hw = false;
  367. irq->hwintid = 0;
  368. spin_unlock(&irq->irq_lock);
  369. vgic_put_irq(vcpu->kvm, irq);
  370. return 0;
  371. }
  372. /**
  373. * vgic_prune_ap_list - Remove non-relevant interrupts from the list
  374. *
  375. * @vcpu: The VCPU pointer
  376. *
  377. * Go over the list of "interesting" interrupts, and prune those that we
  378. * won't have to consider in the near future.
  379. */
  380. static void vgic_prune_ap_list(struct kvm_vcpu *vcpu)
  381. {
  382. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  383. struct vgic_irq *irq, *tmp;
  384. retry:
  385. spin_lock(&vgic_cpu->ap_list_lock);
  386. list_for_each_entry_safe(irq, tmp, &vgic_cpu->ap_list_head, ap_list) {
  387. struct kvm_vcpu *target_vcpu, *vcpuA, *vcpuB;
  388. spin_lock(&irq->irq_lock);
  389. BUG_ON(vcpu != irq->vcpu);
  390. target_vcpu = vgic_target_oracle(irq);
  391. if (!target_vcpu) {
  392. /*
  393. * We don't need to process this interrupt any
  394. * further, move it off the list.
  395. */
  396. list_del(&irq->ap_list);
  397. irq->vcpu = NULL;
  398. spin_unlock(&irq->irq_lock);
  399. /*
  400. * This vgic_put_irq call matches the
  401. * vgic_get_irq_kref in vgic_queue_irq_unlock,
  402. * where we added the LPI to the ap_list. As
  403. * we remove the irq from the list, we drop
  404. * also drop the refcount.
  405. */
  406. vgic_put_irq(vcpu->kvm, irq);
  407. continue;
  408. }
  409. if (target_vcpu == vcpu) {
  410. /* We're on the right CPU */
  411. spin_unlock(&irq->irq_lock);
  412. continue;
  413. }
  414. /* This interrupt looks like it has to be migrated. */
  415. spin_unlock(&irq->irq_lock);
  416. spin_unlock(&vgic_cpu->ap_list_lock);
  417. /*
  418. * Ensure locking order by always locking the smallest
  419. * ID first.
  420. */
  421. if (vcpu->vcpu_id < target_vcpu->vcpu_id) {
  422. vcpuA = vcpu;
  423. vcpuB = target_vcpu;
  424. } else {
  425. vcpuA = target_vcpu;
  426. vcpuB = vcpu;
  427. }
  428. spin_lock(&vcpuA->arch.vgic_cpu.ap_list_lock);
  429. spin_lock_nested(&vcpuB->arch.vgic_cpu.ap_list_lock,
  430. SINGLE_DEPTH_NESTING);
  431. spin_lock(&irq->irq_lock);
  432. /*
  433. * If the affinity has been preserved, move the
  434. * interrupt around. Otherwise, it means things have
  435. * changed while the interrupt was unlocked, and we
  436. * need to replay this.
  437. *
  438. * In all cases, we cannot trust the list not to have
  439. * changed, so we restart from the beginning.
  440. */
  441. if (target_vcpu == vgic_target_oracle(irq)) {
  442. struct vgic_cpu *new_cpu = &target_vcpu->arch.vgic_cpu;
  443. list_del(&irq->ap_list);
  444. irq->vcpu = target_vcpu;
  445. list_add_tail(&irq->ap_list, &new_cpu->ap_list_head);
  446. }
  447. spin_unlock(&irq->irq_lock);
  448. spin_unlock(&vcpuB->arch.vgic_cpu.ap_list_lock);
  449. spin_unlock(&vcpuA->arch.vgic_cpu.ap_list_lock);
  450. goto retry;
  451. }
  452. spin_unlock(&vgic_cpu->ap_list_lock);
  453. }
  454. static inline void vgic_process_maintenance_interrupt(struct kvm_vcpu *vcpu)
  455. {
  456. if (kvm_vgic_global_state.type == VGIC_V2)
  457. vgic_v2_process_maintenance(vcpu);
  458. else
  459. vgic_v3_process_maintenance(vcpu);
  460. }
  461. static inline void vgic_fold_lr_state(struct kvm_vcpu *vcpu)
  462. {
  463. if (kvm_vgic_global_state.type == VGIC_V2)
  464. vgic_v2_fold_lr_state(vcpu);
  465. else
  466. vgic_v3_fold_lr_state(vcpu);
  467. }
  468. /* Requires the irq_lock to be held. */
  469. static inline void vgic_populate_lr(struct kvm_vcpu *vcpu,
  470. struct vgic_irq *irq, int lr)
  471. {
  472. DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
  473. if (kvm_vgic_global_state.type == VGIC_V2)
  474. vgic_v2_populate_lr(vcpu, irq, lr);
  475. else
  476. vgic_v3_populate_lr(vcpu, irq, lr);
  477. }
  478. static inline void vgic_clear_lr(struct kvm_vcpu *vcpu, int lr)
  479. {
  480. if (kvm_vgic_global_state.type == VGIC_V2)
  481. vgic_v2_clear_lr(vcpu, lr);
  482. else
  483. vgic_v3_clear_lr(vcpu, lr);
  484. }
  485. static inline void vgic_set_underflow(struct kvm_vcpu *vcpu)
  486. {
  487. if (kvm_vgic_global_state.type == VGIC_V2)
  488. vgic_v2_set_underflow(vcpu);
  489. else
  490. vgic_v3_set_underflow(vcpu);
  491. }
  492. /* Requires the ap_list_lock to be held. */
  493. static int compute_ap_list_depth(struct kvm_vcpu *vcpu)
  494. {
  495. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  496. struct vgic_irq *irq;
  497. int count = 0;
  498. DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
  499. list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
  500. spin_lock(&irq->irq_lock);
  501. /* GICv2 SGIs can count for more than one... */
  502. if (vgic_irq_is_sgi(irq->intid) && irq->source)
  503. count += hweight8(irq->source);
  504. else
  505. count++;
  506. spin_unlock(&irq->irq_lock);
  507. }
  508. return count;
  509. }
  510. /* Requires the VCPU's ap_list_lock to be held. */
  511. static void vgic_flush_lr_state(struct kvm_vcpu *vcpu)
  512. {
  513. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  514. struct vgic_irq *irq;
  515. int count = 0;
  516. DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
  517. if (compute_ap_list_depth(vcpu) > kvm_vgic_global_state.nr_lr) {
  518. vgic_set_underflow(vcpu);
  519. vgic_sort_ap_list(vcpu);
  520. }
  521. list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
  522. spin_lock(&irq->irq_lock);
  523. if (unlikely(vgic_target_oracle(irq) != vcpu))
  524. goto next;
  525. /*
  526. * If we get an SGI with multiple sources, try to get
  527. * them in all at once.
  528. */
  529. do {
  530. vgic_populate_lr(vcpu, irq, count++);
  531. } while (irq->source && count < kvm_vgic_global_state.nr_lr);
  532. next:
  533. spin_unlock(&irq->irq_lock);
  534. if (count == kvm_vgic_global_state.nr_lr)
  535. break;
  536. }
  537. vcpu->arch.vgic_cpu.used_lrs = count;
  538. /* Nuke remaining LRs */
  539. for ( ; count < kvm_vgic_global_state.nr_lr; count++)
  540. vgic_clear_lr(vcpu, count);
  541. }
  542. /* Sync back the hardware VGIC state into our emulation after a guest's run. */
  543. void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
  544. {
  545. if (unlikely(!vgic_initialized(vcpu->kvm)))
  546. return;
  547. vgic_process_maintenance_interrupt(vcpu);
  548. vgic_fold_lr_state(vcpu);
  549. vgic_prune_ap_list(vcpu);
  550. }
  551. /* Flush our emulation state into the GIC hardware before entering the guest. */
  552. void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
  553. {
  554. if (unlikely(!vgic_initialized(vcpu->kvm)))
  555. return;
  556. spin_lock(&vcpu->arch.vgic_cpu.ap_list_lock);
  557. vgic_flush_lr_state(vcpu);
  558. spin_unlock(&vcpu->arch.vgic_cpu.ap_list_lock);
  559. }
  560. int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
  561. {
  562. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  563. struct vgic_irq *irq;
  564. bool pending = false;
  565. if (!vcpu->kvm->arch.vgic.enabled)
  566. return false;
  567. spin_lock(&vgic_cpu->ap_list_lock);
  568. list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
  569. spin_lock(&irq->irq_lock);
  570. pending = irq->pending && irq->enabled;
  571. spin_unlock(&irq->irq_lock);
  572. if (pending)
  573. break;
  574. }
  575. spin_unlock(&vgic_cpu->ap_list_lock);
  576. return pending;
  577. }
  578. void vgic_kick_vcpus(struct kvm *kvm)
  579. {
  580. struct kvm_vcpu *vcpu;
  581. int c;
  582. /*
  583. * We've injected an interrupt, time to find out who deserves
  584. * a good kick...
  585. */
  586. kvm_for_each_vcpu(c, vcpu, kvm) {
  587. if (kvm_vgic_vcpu_pending_irq(vcpu))
  588. kvm_vcpu_kick(vcpu);
  589. }
  590. }
  591. bool kvm_vgic_map_is_active(struct kvm_vcpu *vcpu, unsigned int virt_irq)
  592. {
  593. struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, virt_irq);
  594. bool map_is_active;
  595. spin_lock(&irq->irq_lock);
  596. map_is_active = irq->hw && irq->active;
  597. spin_unlock(&irq->irq_lock);
  598. vgic_put_irq(vcpu->kvm, irq);
  599. return map_is_active;
  600. }