vgic.c 21 KB

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