vgic.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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. raw_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. raw_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 + 1);
  94. return &vcpu->arch.vgic_cpu.private_irqs[intid];
  95. }
  96. /* SPIs */
  97. if (intid < (kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS)) {
  98. intid = array_index_nospec(intid, kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS);
  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. raw_spin_lock_irqsave(&dist->lpi_list_lock, flags);
  122. if (!kref_put(&irq->refcount, vgic_irq_release)) {
  123. raw_spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
  124. return;
  125. };
  126. list_del(&irq->lpi_list);
  127. dist->lpi_list_count--;
  128. raw_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. /*
  212. * list_sort may call this function with the same element when
  213. * the list is fairly long.
  214. */
  215. if (unlikely(irqa == irqb))
  216. return 0;
  217. spin_lock(&irqa->irq_lock);
  218. spin_lock_nested(&irqb->irq_lock, SINGLE_DEPTH_NESTING);
  219. if (irqa->active || irqb->active) {
  220. ret = (int)irqb->active - (int)irqa->active;
  221. goto out;
  222. }
  223. penda = irqa->enabled && irq_is_pending(irqa);
  224. pendb = irqb->enabled && irq_is_pending(irqb);
  225. if (!penda || !pendb) {
  226. ret = (int)pendb - (int)penda;
  227. goto out;
  228. }
  229. /* Both pending and enabled, sort by priority */
  230. ret = irqa->priority - irqb->priority;
  231. out:
  232. spin_unlock(&irqb->irq_lock);
  233. spin_unlock(&irqa->irq_lock);
  234. return ret;
  235. }
  236. /* Must be called with the ap_list_lock held */
  237. static void vgic_sort_ap_list(struct kvm_vcpu *vcpu)
  238. {
  239. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  240. DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
  241. list_sort(NULL, &vgic_cpu->ap_list_head, vgic_irq_cmp);
  242. }
  243. /*
  244. * Only valid injection if changing level for level-triggered IRQs or for a
  245. * rising edge, and in-kernel connected IRQ lines can only be controlled by
  246. * their owner.
  247. */
  248. static bool vgic_validate_injection(struct vgic_irq *irq, bool level, void *owner)
  249. {
  250. if (irq->owner != owner)
  251. return false;
  252. switch (irq->config) {
  253. case VGIC_CONFIG_LEVEL:
  254. return irq->line_level != level;
  255. case VGIC_CONFIG_EDGE:
  256. return level;
  257. }
  258. return false;
  259. }
  260. /*
  261. * Check whether an IRQ needs to (and can) be queued to a VCPU's ap list.
  262. * Do the queuing if necessary, taking the right locks in the right order.
  263. * Returns true when the IRQ was queued, false otherwise.
  264. *
  265. * Needs to be entered with the IRQ lock already held, but will return
  266. * with all locks dropped.
  267. */
  268. bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq,
  269. unsigned long flags)
  270. {
  271. struct kvm_vcpu *vcpu;
  272. DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
  273. retry:
  274. vcpu = vgic_target_oracle(irq);
  275. if (irq->vcpu || !vcpu) {
  276. /*
  277. * If this IRQ is already on a VCPU's ap_list, then it
  278. * cannot be moved or modified and there is no more work for
  279. * us to do.
  280. *
  281. * Otherwise, if the irq is not pending and enabled, it does
  282. * not need to be inserted into an ap_list and there is also
  283. * no more work for us to do.
  284. */
  285. spin_unlock_irqrestore(&irq->irq_lock, flags);
  286. /*
  287. * We have to kick the VCPU here, because we could be
  288. * queueing an edge-triggered interrupt for which we
  289. * get no EOI maintenance interrupt. In that case,
  290. * while the IRQ is already on the VCPU's AP list, the
  291. * VCPU could have EOI'ed the original interrupt and
  292. * won't see this one until it exits for some other
  293. * reason.
  294. */
  295. if (vcpu) {
  296. kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
  297. kvm_vcpu_kick(vcpu);
  298. }
  299. return false;
  300. }
  301. /*
  302. * We must unlock the irq lock to take the ap_list_lock where
  303. * we are going to insert this new pending interrupt.
  304. */
  305. spin_unlock_irqrestore(&irq->irq_lock, flags);
  306. /* someone can do stuff here, which we re-check below */
  307. spin_lock_irqsave(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
  308. spin_lock(&irq->irq_lock);
  309. /*
  310. * Did something change behind our backs?
  311. *
  312. * There are two cases:
  313. * 1) The irq lost its pending state or was disabled behind our
  314. * backs and/or it was queued to another VCPU's ap_list.
  315. * 2) Someone changed the affinity on this irq behind our
  316. * backs and we are now holding the wrong ap_list_lock.
  317. *
  318. * In both cases, drop the locks and retry.
  319. */
  320. if (unlikely(irq->vcpu || vcpu != vgic_target_oracle(irq))) {
  321. spin_unlock(&irq->irq_lock);
  322. spin_unlock_irqrestore(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
  323. spin_lock_irqsave(&irq->irq_lock, flags);
  324. goto retry;
  325. }
  326. /*
  327. * Grab a reference to the irq to reflect the fact that it is
  328. * now in the ap_list.
  329. */
  330. vgic_get_irq_kref(irq);
  331. list_add_tail(&irq->ap_list, &vcpu->arch.vgic_cpu.ap_list_head);
  332. irq->vcpu = vcpu;
  333. spin_unlock(&irq->irq_lock);
  334. spin_unlock_irqrestore(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
  335. kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
  336. kvm_vcpu_kick(vcpu);
  337. return true;
  338. }
  339. /**
  340. * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
  341. * @kvm: The VM structure pointer
  342. * @cpuid: The CPU for PPIs
  343. * @intid: The INTID to inject a new state to.
  344. * @level: Edge-triggered: true: to trigger the interrupt
  345. * false: to ignore the call
  346. * Level-sensitive true: raise the input signal
  347. * false: lower the input signal
  348. * @owner: The opaque pointer to the owner of the IRQ being raised to verify
  349. * that the caller is allowed to inject this IRQ. Userspace
  350. * injections will have owner == NULL.
  351. *
  352. * The VGIC is not concerned with devices being active-LOW or active-HIGH for
  353. * level-sensitive interrupts. You can think of the level parameter as 1
  354. * being HIGH and 0 being LOW and all devices being active-HIGH.
  355. */
  356. int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,
  357. bool level, void *owner)
  358. {
  359. struct kvm_vcpu *vcpu;
  360. struct vgic_irq *irq;
  361. unsigned long flags;
  362. int ret;
  363. trace_vgic_update_irq_pending(cpuid, intid, level);
  364. ret = vgic_lazy_init(kvm);
  365. if (ret)
  366. return ret;
  367. vcpu = kvm_get_vcpu(kvm, cpuid);
  368. if (!vcpu && intid < VGIC_NR_PRIVATE_IRQS)
  369. return -EINVAL;
  370. irq = vgic_get_irq(kvm, vcpu, intid);
  371. if (!irq)
  372. return -EINVAL;
  373. spin_lock_irqsave(&irq->irq_lock, flags);
  374. if (!vgic_validate_injection(irq, level, owner)) {
  375. /* Nothing to see here, move along... */
  376. spin_unlock_irqrestore(&irq->irq_lock, flags);
  377. vgic_put_irq(kvm, irq);
  378. return 0;
  379. }
  380. if (irq->config == VGIC_CONFIG_LEVEL)
  381. irq->line_level = level;
  382. else
  383. irq->pending_latch = true;
  384. vgic_queue_irq_unlock(kvm, irq, flags);
  385. vgic_put_irq(kvm, irq);
  386. return 0;
  387. }
  388. /* @irq->irq_lock must be held */
  389. static int kvm_vgic_map_irq(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
  390. unsigned int host_irq,
  391. bool (*get_input_level)(int vindid))
  392. {
  393. struct irq_desc *desc;
  394. struct irq_data *data;
  395. /*
  396. * Find the physical IRQ number corresponding to @host_irq
  397. */
  398. desc = irq_to_desc(host_irq);
  399. if (!desc) {
  400. kvm_err("%s: no interrupt descriptor\n", __func__);
  401. return -EINVAL;
  402. }
  403. data = irq_desc_get_irq_data(desc);
  404. while (data->parent_data)
  405. data = data->parent_data;
  406. irq->hw = true;
  407. irq->host_irq = host_irq;
  408. irq->hwintid = data->hwirq;
  409. irq->get_input_level = get_input_level;
  410. return 0;
  411. }
  412. /* @irq->irq_lock must be held */
  413. static inline void kvm_vgic_unmap_irq(struct vgic_irq *irq)
  414. {
  415. irq->hw = false;
  416. irq->hwintid = 0;
  417. irq->get_input_level = NULL;
  418. }
  419. int kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, unsigned int host_irq,
  420. u32 vintid, bool (*get_input_level)(int vindid))
  421. {
  422. struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
  423. unsigned long flags;
  424. int ret;
  425. BUG_ON(!irq);
  426. spin_lock_irqsave(&irq->irq_lock, flags);
  427. ret = kvm_vgic_map_irq(vcpu, irq, host_irq, get_input_level);
  428. spin_unlock_irqrestore(&irq->irq_lock, flags);
  429. vgic_put_irq(vcpu->kvm, irq);
  430. return ret;
  431. }
  432. /**
  433. * kvm_vgic_reset_mapped_irq - Reset a mapped IRQ
  434. * @vcpu: The VCPU pointer
  435. * @vintid: The INTID of the interrupt
  436. *
  437. * Reset the active and pending states of a mapped interrupt. Kernel
  438. * subsystems injecting mapped interrupts should reset their interrupt lines
  439. * when we are doing a reset of the VM.
  440. */
  441. void kvm_vgic_reset_mapped_irq(struct kvm_vcpu *vcpu, u32 vintid)
  442. {
  443. struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
  444. unsigned long flags;
  445. if (!irq->hw)
  446. goto out;
  447. spin_lock_irqsave(&irq->irq_lock, flags);
  448. irq->active = false;
  449. irq->pending_latch = false;
  450. irq->line_level = false;
  451. spin_unlock_irqrestore(&irq->irq_lock, flags);
  452. out:
  453. vgic_put_irq(vcpu->kvm, irq);
  454. }
  455. int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int vintid)
  456. {
  457. struct vgic_irq *irq;
  458. unsigned long flags;
  459. if (!vgic_initialized(vcpu->kvm))
  460. return -EAGAIN;
  461. irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
  462. BUG_ON(!irq);
  463. spin_lock_irqsave(&irq->irq_lock, flags);
  464. kvm_vgic_unmap_irq(irq);
  465. spin_unlock_irqrestore(&irq->irq_lock, flags);
  466. vgic_put_irq(vcpu->kvm, irq);
  467. return 0;
  468. }
  469. /**
  470. * kvm_vgic_set_owner - Set the owner of an interrupt for a VM
  471. *
  472. * @vcpu: Pointer to the VCPU (used for PPIs)
  473. * @intid: The virtual INTID identifying the interrupt (PPI or SPI)
  474. * @owner: Opaque pointer to the owner
  475. *
  476. * Returns 0 if intid is not already used by another in-kernel device and the
  477. * owner is set, otherwise returns an error code.
  478. */
  479. int kvm_vgic_set_owner(struct kvm_vcpu *vcpu, unsigned int intid, void *owner)
  480. {
  481. struct vgic_irq *irq;
  482. unsigned long flags;
  483. int ret = 0;
  484. if (!vgic_initialized(vcpu->kvm))
  485. return -EAGAIN;
  486. /* SGIs and LPIs cannot be wired up to any device */
  487. if (!irq_is_ppi(intid) && !vgic_valid_spi(vcpu->kvm, intid))
  488. return -EINVAL;
  489. irq = vgic_get_irq(vcpu->kvm, vcpu, intid);
  490. spin_lock_irqsave(&irq->irq_lock, flags);
  491. if (irq->owner && irq->owner != owner)
  492. ret = -EEXIST;
  493. else
  494. irq->owner = owner;
  495. spin_unlock_irqrestore(&irq->irq_lock, flags);
  496. return ret;
  497. }
  498. /**
  499. * vgic_prune_ap_list - Remove non-relevant interrupts from the list
  500. *
  501. * @vcpu: The VCPU pointer
  502. *
  503. * Go over the list of "interesting" interrupts, and prune those that we
  504. * won't have to consider in the near future.
  505. */
  506. static void vgic_prune_ap_list(struct kvm_vcpu *vcpu)
  507. {
  508. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  509. struct vgic_irq *irq, *tmp;
  510. DEBUG_SPINLOCK_BUG_ON(!irqs_disabled());
  511. retry:
  512. spin_lock(&vgic_cpu->ap_list_lock);
  513. list_for_each_entry_safe(irq, tmp, &vgic_cpu->ap_list_head, ap_list) {
  514. struct kvm_vcpu *target_vcpu, *vcpuA, *vcpuB;
  515. bool target_vcpu_needs_kick = false;
  516. spin_lock(&irq->irq_lock);
  517. BUG_ON(vcpu != irq->vcpu);
  518. target_vcpu = vgic_target_oracle(irq);
  519. if (!target_vcpu) {
  520. /*
  521. * We don't need to process this interrupt any
  522. * further, move it off the list.
  523. */
  524. list_del(&irq->ap_list);
  525. irq->vcpu = NULL;
  526. spin_unlock(&irq->irq_lock);
  527. /*
  528. * This vgic_put_irq call matches the
  529. * vgic_get_irq_kref in vgic_queue_irq_unlock,
  530. * where we added the LPI to the ap_list. As
  531. * we remove the irq from the list, we drop
  532. * also drop the refcount.
  533. */
  534. vgic_put_irq(vcpu->kvm, irq);
  535. continue;
  536. }
  537. if (target_vcpu == vcpu) {
  538. /* We're on the right CPU */
  539. spin_unlock(&irq->irq_lock);
  540. continue;
  541. }
  542. /* This interrupt looks like it has to be migrated. */
  543. spin_unlock(&irq->irq_lock);
  544. spin_unlock(&vgic_cpu->ap_list_lock);
  545. /*
  546. * Ensure locking order by always locking the smallest
  547. * ID first.
  548. */
  549. if (vcpu->vcpu_id < target_vcpu->vcpu_id) {
  550. vcpuA = vcpu;
  551. vcpuB = target_vcpu;
  552. } else {
  553. vcpuA = target_vcpu;
  554. vcpuB = vcpu;
  555. }
  556. spin_lock(&vcpuA->arch.vgic_cpu.ap_list_lock);
  557. spin_lock_nested(&vcpuB->arch.vgic_cpu.ap_list_lock,
  558. SINGLE_DEPTH_NESTING);
  559. spin_lock(&irq->irq_lock);
  560. /*
  561. * If the affinity has been preserved, move the
  562. * interrupt around. Otherwise, it means things have
  563. * changed while the interrupt was unlocked, and we
  564. * need to replay this.
  565. *
  566. * In all cases, we cannot trust the list not to have
  567. * changed, so we restart from the beginning.
  568. */
  569. if (target_vcpu == vgic_target_oracle(irq)) {
  570. struct vgic_cpu *new_cpu = &target_vcpu->arch.vgic_cpu;
  571. list_del(&irq->ap_list);
  572. irq->vcpu = target_vcpu;
  573. list_add_tail(&irq->ap_list, &new_cpu->ap_list_head);
  574. target_vcpu_needs_kick = true;
  575. }
  576. spin_unlock(&irq->irq_lock);
  577. spin_unlock(&vcpuB->arch.vgic_cpu.ap_list_lock);
  578. spin_unlock(&vcpuA->arch.vgic_cpu.ap_list_lock);
  579. if (target_vcpu_needs_kick) {
  580. kvm_make_request(KVM_REQ_IRQ_PENDING, target_vcpu);
  581. kvm_vcpu_kick(target_vcpu);
  582. }
  583. goto retry;
  584. }
  585. spin_unlock(&vgic_cpu->ap_list_lock);
  586. }
  587. static inline void vgic_fold_lr_state(struct kvm_vcpu *vcpu)
  588. {
  589. if (kvm_vgic_global_state.type == VGIC_V2)
  590. vgic_v2_fold_lr_state(vcpu);
  591. else
  592. vgic_v3_fold_lr_state(vcpu);
  593. }
  594. /* Requires the irq_lock to be held. */
  595. static inline void vgic_populate_lr(struct kvm_vcpu *vcpu,
  596. struct vgic_irq *irq, int lr)
  597. {
  598. DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
  599. if (kvm_vgic_global_state.type == VGIC_V2)
  600. vgic_v2_populate_lr(vcpu, irq, lr);
  601. else
  602. vgic_v3_populate_lr(vcpu, irq, lr);
  603. }
  604. static inline void vgic_clear_lr(struct kvm_vcpu *vcpu, int lr)
  605. {
  606. if (kvm_vgic_global_state.type == VGIC_V2)
  607. vgic_v2_clear_lr(vcpu, lr);
  608. else
  609. vgic_v3_clear_lr(vcpu, lr);
  610. }
  611. static inline void vgic_set_underflow(struct kvm_vcpu *vcpu)
  612. {
  613. if (kvm_vgic_global_state.type == VGIC_V2)
  614. vgic_v2_set_underflow(vcpu);
  615. else
  616. vgic_v3_set_underflow(vcpu);
  617. }
  618. /* Requires the ap_list_lock to be held. */
  619. static int compute_ap_list_depth(struct kvm_vcpu *vcpu,
  620. bool *multi_sgi)
  621. {
  622. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  623. struct vgic_irq *irq;
  624. int count = 0;
  625. *multi_sgi = false;
  626. DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
  627. list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
  628. int w;
  629. spin_lock(&irq->irq_lock);
  630. /* GICv2 SGIs can count for more than one... */
  631. w = vgic_irq_get_lr_count(irq);
  632. spin_unlock(&irq->irq_lock);
  633. count += w;
  634. *multi_sgi |= (w > 1);
  635. }
  636. return count;
  637. }
  638. /* Requires the VCPU's ap_list_lock to be held. */
  639. static void vgic_flush_lr_state(struct kvm_vcpu *vcpu)
  640. {
  641. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  642. struct vgic_irq *irq;
  643. int count;
  644. bool multi_sgi;
  645. u8 prio = 0xff;
  646. DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
  647. count = compute_ap_list_depth(vcpu, &multi_sgi);
  648. if (count > kvm_vgic_global_state.nr_lr || multi_sgi)
  649. vgic_sort_ap_list(vcpu);
  650. count = 0;
  651. list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
  652. spin_lock(&irq->irq_lock);
  653. /*
  654. * If we have multi-SGIs in the pipeline, we need to
  655. * guarantee that they are all seen before any IRQ of
  656. * lower priority. In that case, we need to filter out
  657. * these interrupts by exiting early. This is easy as
  658. * the AP list has been sorted already.
  659. */
  660. if (multi_sgi && irq->priority > prio) {
  661. spin_unlock(&irq->irq_lock);
  662. break;
  663. }
  664. if (likely(vgic_target_oracle(irq) == vcpu)) {
  665. vgic_populate_lr(vcpu, irq, count++);
  666. if (irq->source)
  667. prio = irq->priority;
  668. }
  669. spin_unlock(&irq->irq_lock);
  670. if (count == kvm_vgic_global_state.nr_lr) {
  671. if (!list_is_last(&irq->ap_list,
  672. &vgic_cpu->ap_list_head))
  673. vgic_set_underflow(vcpu);
  674. break;
  675. }
  676. }
  677. vcpu->arch.vgic_cpu.used_lrs = count;
  678. /* Nuke remaining LRs */
  679. for ( ; count < kvm_vgic_global_state.nr_lr; count++)
  680. vgic_clear_lr(vcpu, count);
  681. }
  682. static inline bool can_access_vgic_from_kernel(void)
  683. {
  684. /*
  685. * GICv2 can always be accessed from the kernel because it is
  686. * memory-mapped, and VHE systems can access GICv3 EL2 system
  687. * registers.
  688. */
  689. return !static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif) || has_vhe();
  690. }
  691. static inline void vgic_save_state(struct kvm_vcpu *vcpu)
  692. {
  693. if (!static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
  694. vgic_v2_save_state(vcpu);
  695. else
  696. __vgic_v3_save_state(vcpu);
  697. }
  698. /* Sync back the hardware VGIC state into our emulation after a guest's run. */
  699. void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
  700. {
  701. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  702. WARN_ON(vgic_v4_sync_hwstate(vcpu));
  703. /* An empty ap_list_head implies used_lrs == 0 */
  704. if (list_empty(&vcpu->arch.vgic_cpu.ap_list_head))
  705. return;
  706. if (can_access_vgic_from_kernel())
  707. vgic_save_state(vcpu);
  708. if (vgic_cpu->used_lrs)
  709. vgic_fold_lr_state(vcpu);
  710. vgic_prune_ap_list(vcpu);
  711. }
  712. static inline void vgic_restore_state(struct kvm_vcpu *vcpu)
  713. {
  714. if (!static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
  715. vgic_v2_restore_state(vcpu);
  716. else
  717. __vgic_v3_restore_state(vcpu);
  718. }
  719. /* Flush our emulation state into the GIC hardware before entering the guest. */
  720. void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
  721. {
  722. WARN_ON(vgic_v4_flush_hwstate(vcpu));
  723. /*
  724. * If there are no virtual interrupts active or pending for this
  725. * VCPU, then there is no work to do and we can bail out without
  726. * taking any lock. There is a potential race with someone injecting
  727. * interrupts to the VCPU, but it is a benign race as the VCPU will
  728. * either observe the new interrupt before or after doing this check,
  729. * and introducing additional synchronization mechanism doesn't change
  730. * this.
  731. */
  732. if (list_empty(&vcpu->arch.vgic_cpu.ap_list_head))
  733. return;
  734. DEBUG_SPINLOCK_BUG_ON(!irqs_disabled());
  735. spin_lock(&vcpu->arch.vgic_cpu.ap_list_lock);
  736. vgic_flush_lr_state(vcpu);
  737. spin_unlock(&vcpu->arch.vgic_cpu.ap_list_lock);
  738. if (can_access_vgic_from_kernel())
  739. vgic_restore_state(vcpu);
  740. }
  741. void kvm_vgic_load(struct kvm_vcpu *vcpu)
  742. {
  743. if (unlikely(!vgic_initialized(vcpu->kvm)))
  744. return;
  745. if (kvm_vgic_global_state.type == VGIC_V2)
  746. vgic_v2_load(vcpu);
  747. else
  748. vgic_v3_load(vcpu);
  749. }
  750. void kvm_vgic_put(struct kvm_vcpu *vcpu)
  751. {
  752. if (unlikely(!vgic_initialized(vcpu->kvm)))
  753. return;
  754. if (kvm_vgic_global_state.type == VGIC_V2)
  755. vgic_v2_put(vcpu);
  756. else
  757. vgic_v3_put(vcpu);
  758. }
  759. void kvm_vgic_vmcr_sync(struct kvm_vcpu *vcpu)
  760. {
  761. if (unlikely(!irqchip_in_kernel(vcpu->kvm)))
  762. return;
  763. if (kvm_vgic_global_state.type == VGIC_V2)
  764. vgic_v2_vmcr_sync(vcpu);
  765. else
  766. vgic_v3_vmcr_sync(vcpu);
  767. }
  768. int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
  769. {
  770. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  771. struct vgic_irq *irq;
  772. bool pending = false;
  773. unsigned long flags;
  774. if (!vcpu->kvm->arch.vgic.enabled)
  775. return false;
  776. if (vcpu->arch.vgic_cpu.vgic_v3.its_vpe.pending_last)
  777. return true;
  778. spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
  779. list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
  780. spin_lock(&irq->irq_lock);
  781. pending = irq_is_pending(irq) && irq->enabled;
  782. spin_unlock(&irq->irq_lock);
  783. if (pending)
  784. break;
  785. }
  786. spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
  787. return pending;
  788. }
  789. void vgic_kick_vcpus(struct kvm *kvm)
  790. {
  791. struct kvm_vcpu *vcpu;
  792. int c;
  793. /*
  794. * We've injected an interrupt, time to find out who deserves
  795. * a good kick...
  796. */
  797. kvm_for_each_vcpu(c, vcpu, kvm) {
  798. if (kvm_vgic_vcpu_pending_irq(vcpu)) {
  799. kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
  800. kvm_vcpu_kick(vcpu);
  801. }
  802. }
  803. }
  804. bool kvm_vgic_map_is_active(struct kvm_vcpu *vcpu, unsigned int vintid)
  805. {
  806. struct vgic_irq *irq;
  807. bool map_is_active;
  808. unsigned long flags;
  809. if (!vgic_initialized(vcpu->kvm))
  810. return false;
  811. irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
  812. spin_lock_irqsave(&irq->irq_lock, flags);
  813. map_is_active = irq->hw && irq->active;
  814. spin_unlock_irqrestore(&irq->irq_lock, flags);
  815. vgic_put_irq(vcpu->kvm, irq);
  816. return map_is_active;
  817. }