vgic.c 25 KB

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