vgic.c 27 KB

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