book3s_hv_rm_xics.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /*
  2. * Copyright 2012 Michael Ellerman, IBM Corporation.
  3. * Copyright 2012 Benjamin Herrenschmidt, IBM Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License, version 2, as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/kvm_host.h>
  11. #include <linux/err.h>
  12. #include <asm/kvm_book3s.h>
  13. #include <asm/kvm_ppc.h>
  14. #include <asm/hvcall.h>
  15. #include <asm/xics.h>
  16. #include <asm/debug.h>
  17. #include <asm/synch.h>
  18. #include <asm/ppc-opcode.h>
  19. #include "book3s_xics.h"
  20. #define DEBUG_PASSUP
  21. static void icp_rm_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
  22. u32 new_irq);
  23. /* -- ICS routines -- */
  24. static void ics_rm_check_resend(struct kvmppc_xics *xics,
  25. struct kvmppc_ics *ics, struct kvmppc_icp *icp)
  26. {
  27. int i;
  28. arch_spin_lock(&ics->lock);
  29. for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
  30. struct ics_irq_state *state = &ics->irq_state[i];
  31. if (!state->resend)
  32. continue;
  33. arch_spin_unlock(&ics->lock);
  34. icp_rm_deliver_irq(xics, icp, state->number);
  35. arch_spin_lock(&ics->lock);
  36. }
  37. arch_spin_unlock(&ics->lock);
  38. }
  39. /* -- ICP routines -- */
  40. static void icp_rm_set_vcpu_irq(struct kvm_vcpu *vcpu,
  41. struct kvm_vcpu *this_vcpu)
  42. {
  43. struct kvmppc_icp *this_icp = this_vcpu->arch.icp;
  44. int cpu;
  45. /* Mark the target VCPU as having an interrupt pending */
  46. vcpu->stat.queue_intr++;
  47. set_bit(BOOK3S_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
  48. /* Kick self ? Just set MER and return */
  49. if (vcpu == this_vcpu) {
  50. mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_MER);
  51. return;
  52. }
  53. /* Check if the core is loaded, if not, too hard */
  54. cpu = vcpu->cpu;
  55. if (cpu < 0 || cpu >= nr_cpu_ids) {
  56. this_icp->rm_action |= XICS_RM_KICK_VCPU;
  57. this_icp->rm_kick_target = vcpu;
  58. return;
  59. }
  60. /* In SMT cpu will always point to thread 0, we adjust it */
  61. cpu += vcpu->arch.ptid;
  62. smp_mb();
  63. kvmhv_rm_send_ipi(cpu);
  64. }
  65. static void icp_rm_clr_vcpu_irq(struct kvm_vcpu *vcpu)
  66. {
  67. /* Note: Only called on self ! */
  68. clear_bit(BOOK3S_IRQPRIO_EXTERNAL_LEVEL,
  69. &vcpu->arch.pending_exceptions);
  70. mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) & ~LPCR_MER);
  71. }
  72. static inline bool icp_rm_try_update(struct kvmppc_icp *icp,
  73. union kvmppc_icp_state old,
  74. union kvmppc_icp_state new)
  75. {
  76. struct kvm_vcpu *this_vcpu = local_paca->kvm_hstate.kvm_vcpu;
  77. bool success;
  78. /* Calculate new output value */
  79. new.out_ee = (new.xisr && (new.pending_pri < new.cppr));
  80. /* Attempt atomic update */
  81. success = cmpxchg64(&icp->state.raw, old.raw, new.raw) == old.raw;
  82. if (!success)
  83. goto bail;
  84. /*
  85. * Check for output state update
  86. *
  87. * Note that this is racy since another processor could be updating
  88. * the state already. This is why we never clear the interrupt output
  89. * here, we only ever set it. The clear only happens prior to doing
  90. * an update and only by the processor itself. Currently we do it
  91. * in Accept (H_XIRR) and Up_Cppr (H_XPPR).
  92. *
  93. * We also do not try to figure out whether the EE state has changed,
  94. * we unconditionally set it if the new state calls for it. The reason
  95. * for that is that we opportunistically remove the pending interrupt
  96. * flag when raising CPPR, so we need to set it back here if an
  97. * interrupt is still pending.
  98. */
  99. if (new.out_ee)
  100. icp_rm_set_vcpu_irq(icp->vcpu, this_vcpu);
  101. /* Expose the state change for debug purposes */
  102. this_vcpu->arch.icp->rm_dbgstate = new;
  103. this_vcpu->arch.icp->rm_dbgtgt = icp->vcpu;
  104. bail:
  105. return success;
  106. }
  107. static inline int check_too_hard(struct kvmppc_xics *xics,
  108. struct kvmppc_icp *icp)
  109. {
  110. return (xics->real_mode_dbg || icp->rm_action) ? H_TOO_HARD : H_SUCCESS;
  111. }
  112. static void icp_rm_check_resend(struct kvmppc_xics *xics,
  113. struct kvmppc_icp *icp)
  114. {
  115. u32 icsid;
  116. /* Order this load with the test for need_resend in the caller */
  117. smp_rmb();
  118. for_each_set_bit(icsid, icp->resend_map, xics->max_icsid + 1) {
  119. struct kvmppc_ics *ics = xics->ics[icsid];
  120. if (!test_and_clear_bit(icsid, icp->resend_map))
  121. continue;
  122. if (!ics)
  123. continue;
  124. ics_rm_check_resend(xics, ics, icp);
  125. }
  126. }
  127. static bool icp_rm_try_to_deliver(struct kvmppc_icp *icp, u32 irq, u8 priority,
  128. u32 *reject)
  129. {
  130. union kvmppc_icp_state old_state, new_state;
  131. bool success;
  132. do {
  133. old_state = new_state = READ_ONCE(icp->state);
  134. *reject = 0;
  135. /* See if we can deliver */
  136. success = new_state.cppr > priority &&
  137. new_state.mfrr > priority &&
  138. new_state.pending_pri > priority;
  139. /*
  140. * If we can, check for a rejection and perform the
  141. * delivery
  142. */
  143. if (success) {
  144. *reject = new_state.xisr;
  145. new_state.xisr = irq;
  146. new_state.pending_pri = priority;
  147. } else {
  148. /*
  149. * If we failed to deliver we set need_resend
  150. * so a subsequent CPPR state change causes us
  151. * to try a new delivery.
  152. */
  153. new_state.need_resend = true;
  154. }
  155. } while (!icp_rm_try_update(icp, old_state, new_state));
  156. return success;
  157. }
  158. static void icp_rm_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
  159. u32 new_irq)
  160. {
  161. struct ics_irq_state *state;
  162. struct kvmppc_ics *ics;
  163. u32 reject;
  164. u16 src;
  165. /*
  166. * This is used both for initial delivery of an interrupt and
  167. * for subsequent rejection.
  168. *
  169. * Rejection can be racy vs. resends. We have evaluated the
  170. * rejection in an atomic ICP transaction which is now complete,
  171. * so potentially the ICP can already accept the interrupt again.
  172. *
  173. * So we need to retry the delivery. Essentially the reject path
  174. * boils down to a failed delivery. Always.
  175. *
  176. * Now the interrupt could also have moved to a different target,
  177. * thus we may need to re-do the ICP lookup as well
  178. */
  179. again:
  180. /* Get the ICS state and lock it */
  181. ics = kvmppc_xics_find_ics(xics, new_irq, &src);
  182. if (!ics) {
  183. /* Unsafe increment, but this does not need to be accurate */
  184. xics->err_noics++;
  185. return;
  186. }
  187. state = &ics->irq_state[src];
  188. /* Get a lock on the ICS */
  189. arch_spin_lock(&ics->lock);
  190. /* Get our server */
  191. if (!icp || state->server != icp->server_num) {
  192. icp = kvmppc_xics_find_server(xics->kvm, state->server);
  193. if (!icp) {
  194. /* Unsafe increment again*/
  195. xics->err_noicp++;
  196. goto out;
  197. }
  198. }
  199. /* Clear the resend bit of that interrupt */
  200. state->resend = 0;
  201. /*
  202. * If masked, bail out
  203. *
  204. * Note: PAPR doesn't mention anything about masked pending
  205. * when doing a resend, only when doing a delivery.
  206. *
  207. * However that would have the effect of losing a masked
  208. * interrupt that was rejected and isn't consistent with
  209. * the whole masked_pending business which is about not
  210. * losing interrupts that occur while masked.
  211. *
  212. * I don't differentiate normal deliveries and resends, this
  213. * implementation will differ from PAPR and not lose such
  214. * interrupts.
  215. */
  216. if (state->priority == MASKED) {
  217. state->masked_pending = 1;
  218. goto out;
  219. }
  220. /*
  221. * Try the delivery, this will set the need_resend flag
  222. * in the ICP as part of the atomic transaction if the
  223. * delivery is not possible.
  224. *
  225. * Note that if successful, the new delivery might have itself
  226. * rejected an interrupt that was "delivered" before we took the
  227. * ics spin lock.
  228. *
  229. * In this case we do the whole sequence all over again for the
  230. * new guy. We cannot assume that the rejected interrupt is less
  231. * favored than the new one, and thus doesn't need to be delivered,
  232. * because by the time we exit icp_rm_try_to_deliver() the target
  233. * processor may well have already consumed & completed it, and thus
  234. * the rejected interrupt might actually be already acceptable.
  235. */
  236. if (icp_rm_try_to_deliver(icp, new_irq, state->priority, &reject)) {
  237. /*
  238. * Delivery was successful, did we reject somebody else ?
  239. */
  240. if (reject && reject != XICS_IPI) {
  241. arch_spin_unlock(&ics->lock);
  242. new_irq = reject;
  243. goto again;
  244. }
  245. } else {
  246. /*
  247. * We failed to deliver the interrupt we need to set the
  248. * resend map bit and mark the ICS state as needing a resend
  249. */
  250. set_bit(ics->icsid, icp->resend_map);
  251. state->resend = 1;
  252. /*
  253. * If the need_resend flag got cleared in the ICP some time
  254. * between icp_rm_try_to_deliver() atomic update and now, then
  255. * we know it might have missed the resend_map bit. So we
  256. * retry
  257. */
  258. smp_mb();
  259. if (!icp->state.need_resend) {
  260. arch_spin_unlock(&ics->lock);
  261. goto again;
  262. }
  263. }
  264. out:
  265. arch_spin_unlock(&ics->lock);
  266. }
  267. static void icp_rm_down_cppr(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
  268. u8 new_cppr)
  269. {
  270. union kvmppc_icp_state old_state, new_state;
  271. bool resend;
  272. /*
  273. * This handles several related states in one operation:
  274. *
  275. * ICP State: Down_CPPR
  276. *
  277. * Load CPPR with new value and if the XISR is 0
  278. * then check for resends:
  279. *
  280. * ICP State: Resend
  281. *
  282. * If MFRR is more favored than CPPR, check for IPIs
  283. * and notify ICS of a potential resend. This is done
  284. * asynchronously (when used in real mode, we will have
  285. * to exit here).
  286. *
  287. * We do not handle the complete Check_IPI as documented
  288. * here. In the PAPR, this state will be used for both
  289. * Set_MFRR and Down_CPPR. However, we know that we aren't
  290. * changing the MFRR state here so we don't need to handle
  291. * the case of an MFRR causing a reject of a pending irq,
  292. * this will have been handled when the MFRR was set in the
  293. * first place.
  294. *
  295. * Thus we don't have to handle rejects, only resends.
  296. *
  297. * When implementing real mode for HV KVM, resend will lead to
  298. * a H_TOO_HARD return and the whole transaction will be handled
  299. * in virtual mode.
  300. */
  301. do {
  302. old_state = new_state = READ_ONCE(icp->state);
  303. /* Down_CPPR */
  304. new_state.cppr = new_cppr;
  305. /*
  306. * Cut down Resend / Check_IPI / IPI
  307. *
  308. * The logic is that we cannot have a pending interrupt
  309. * trumped by an IPI at this point (see above), so we
  310. * know that either the pending interrupt is already an
  311. * IPI (in which case we don't care to override it) or
  312. * it's either more favored than us or non existent
  313. */
  314. if (new_state.mfrr < new_cppr &&
  315. new_state.mfrr <= new_state.pending_pri) {
  316. new_state.pending_pri = new_state.mfrr;
  317. new_state.xisr = XICS_IPI;
  318. }
  319. /* Latch/clear resend bit */
  320. resend = new_state.need_resend;
  321. new_state.need_resend = 0;
  322. } while (!icp_rm_try_update(icp, old_state, new_state));
  323. /*
  324. * Now handle resend checks. Those are asynchronous to the ICP
  325. * state update in HW (ie bus transactions) so we can handle them
  326. * separately here as well.
  327. */
  328. if (resend) {
  329. icp->n_check_resend++;
  330. icp_rm_check_resend(xics, icp);
  331. }
  332. }
  333. unsigned long kvmppc_rm_h_xirr(struct kvm_vcpu *vcpu)
  334. {
  335. union kvmppc_icp_state old_state, new_state;
  336. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  337. struct kvmppc_icp *icp = vcpu->arch.icp;
  338. u32 xirr;
  339. if (!xics || !xics->real_mode)
  340. return H_TOO_HARD;
  341. /* First clear the interrupt */
  342. icp_rm_clr_vcpu_irq(icp->vcpu);
  343. /*
  344. * ICP State: Accept_Interrupt
  345. *
  346. * Return the pending interrupt (if any) along with the
  347. * current CPPR, then clear the XISR & set CPPR to the
  348. * pending priority
  349. */
  350. do {
  351. old_state = new_state = READ_ONCE(icp->state);
  352. xirr = old_state.xisr | (((u32)old_state.cppr) << 24);
  353. if (!old_state.xisr)
  354. break;
  355. new_state.cppr = new_state.pending_pri;
  356. new_state.pending_pri = 0xff;
  357. new_state.xisr = 0;
  358. } while (!icp_rm_try_update(icp, old_state, new_state));
  359. /* Return the result in GPR4 */
  360. vcpu->arch.gpr[4] = xirr;
  361. return check_too_hard(xics, icp);
  362. }
  363. int kvmppc_rm_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
  364. unsigned long mfrr)
  365. {
  366. union kvmppc_icp_state old_state, new_state;
  367. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  368. struct kvmppc_icp *icp, *this_icp = vcpu->arch.icp;
  369. u32 reject;
  370. bool resend;
  371. bool local;
  372. if (!xics || !xics->real_mode)
  373. return H_TOO_HARD;
  374. local = this_icp->server_num == server;
  375. if (local)
  376. icp = this_icp;
  377. else
  378. icp = kvmppc_xics_find_server(vcpu->kvm, server);
  379. if (!icp)
  380. return H_PARAMETER;
  381. /*
  382. * ICP state: Set_MFRR
  383. *
  384. * If the CPPR is more favored than the new MFRR, then
  385. * nothing needs to be done as there can be no XISR to
  386. * reject.
  387. *
  388. * ICP state: Check_IPI
  389. *
  390. * If the CPPR is less favored, then we might be replacing
  391. * an interrupt, and thus need to possibly reject it.
  392. *
  393. * ICP State: IPI
  394. *
  395. * Besides rejecting any pending interrupts, we also
  396. * update XISR and pending_pri to mark IPI as pending.
  397. *
  398. * PAPR does not describe this state, but if the MFRR is being
  399. * made less favored than its earlier value, there might be
  400. * a previously-rejected interrupt needing to be resent.
  401. * Ideally, we would want to resend only if
  402. * prio(pending_interrupt) < mfrr &&
  403. * prio(pending_interrupt) < cppr
  404. * where pending interrupt is the one that was rejected. But
  405. * we don't have that state, so we simply trigger a resend
  406. * whenever the MFRR is made less favored.
  407. */
  408. do {
  409. old_state = new_state = READ_ONCE(icp->state);
  410. /* Set_MFRR */
  411. new_state.mfrr = mfrr;
  412. /* Check_IPI */
  413. reject = 0;
  414. resend = false;
  415. if (mfrr < new_state.cppr) {
  416. /* Reject a pending interrupt if not an IPI */
  417. if (mfrr <= new_state.pending_pri) {
  418. reject = new_state.xisr;
  419. new_state.pending_pri = mfrr;
  420. new_state.xisr = XICS_IPI;
  421. }
  422. }
  423. if (mfrr > old_state.mfrr) {
  424. resend = new_state.need_resend;
  425. new_state.need_resend = 0;
  426. }
  427. } while (!icp_rm_try_update(icp, old_state, new_state));
  428. /* Handle reject in real mode */
  429. if (reject && reject != XICS_IPI) {
  430. this_icp->n_reject++;
  431. icp_rm_deliver_irq(xics, icp, reject);
  432. }
  433. /* Handle resends in real mode */
  434. if (resend) {
  435. this_icp->n_check_resend++;
  436. icp_rm_check_resend(xics, icp);
  437. }
  438. return check_too_hard(xics, this_icp);
  439. }
  440. int kvmppc_rm_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr)
  441. {
  442. union kvmppc_icp_state old_state, new_state;
  443. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  444. struct kvmppc_icp *icp = vcpu->arch.icp;
  445. u32 reject;
  446. if (!xics || !xics->real_mode)
  447. return H_TOO_HARD;
  448. /*
  449. * ICP State: Set_CPPR
  450. *
  451. * We can safely compare the new value with the current
  452. * value outside of the transaction as the CPPR is only
  453. * ever changed by the processor on itself
  454. */
  455. if (cppr > icp->state.cppr) {
  456. icp_rm_down_cppr(xics, icp, cppr);
  457. goto bail;
  458. } else if (cppr == icp->state.cppr)
  459. return H_SUCCESS;
  460. /*
  461. * ICP State: Up_CPPR
  462. *
  463. * The processor is raising its priority, this can result
  464. * in a rejection of a pending interrupt:
  465. *
  466. * ICP State: Reject_Current
  467. *
  468. * We can remove EE from the current processor, the update
  469. * transaction will set it again if needed
  470. */
  471. icp_rm_clr_vcpu_irq(icp->vcpu);
  472. do {
  473. old_state = new_state = READ_ONCE(icp->state);
  474. reject = 0;
  475. new_state.cppr = cppr;
  476. if (cppr <= new_state.pending_pri) {
  477. reject = new_state.xisr;
  478. new_state.xisr = 0;
  479. new_state.pending_pri = 0xff;
  480. }
  481. } while (!icp_rm_try_update(icp, old_state, new_state));
  482. /*
  483. * Check for rejects. They are handled by doing a new delivery
  484. * attempt (see comments in icp_rm_deliver_irq).
  485. */
  486. if (reject && reject != XICS_IPI) {
  487. icp->n_reject++;
  488. icp_rm_deliver_irq(xics, icp, reject);
  489. }
  490. bail:
  491. return check_too_hard(xics, icp);
  492. }
  493. int kvmppc_rm_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr)
  494. {
  495. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  496. struct kvmppc_icp *icp = vcpu->arch.icp;
  497. struct kvmppc_ics *ics;
  498. struct ics_irq_state *state;
  499. u32 irq = xirr & 0x00ffffff;
  500. u16 src;
  501. if (!xics || !xics->real_mode)
  502. return H_TOO_HARD;
  503. /*
  504. * ICP State: EOI
  505. *
  506. * Note: If EOI is incorrectly used by SW to lower the CPPR
  507. * value (ie more favored), we do not check for rejection of
  508. * a pending interrupt, this is a SW error and PAPR sepcifies
  509. * that we don't have to deal with it.
  510. *
  511. * The sending of an EOI to the ICS is handled after the
  512. * CPPR update
  513. *
  514. * ICP State: Down_CPPR which we handle
  515. * in a separate function as it's shared with H_CPPR.
  516. */
  517. icp_rm_down_cppr(xics, icp, xirr >> 24);
  518. /* IPIs have no EOI */
  519. if (irq == XICS_IPI)
  520. goto bail;
  521. /*
  522. * EOI handling: If the interrupt is still asserted, we need to
  523. * resend it. We can take a lockless "peek" at the ICS state here.
  524. *
  525. * "Message" interrupts will never have "asserted" set
  526. */
  527. ics = kvmppc_xics_find_ics(xics, irq, &src);
  528. if (!ics)
  529. goto bail;
  530. state = &ics->irq_state[src];
  531. /* Still asserted, resend it */
  532. if (state->asserted) {
  533. icp->n_reject++;
  534. icp_rm_deliver_irq(xics, icp, irq);
  535. }
  536. if (!hlist_empty(&vcpu->kvm->irq_ack_notifier_list)) {
  537. icp->rm_action |= XICS_RM_NOTIFY_EOI;
  538. icp->rm_eoied_irq = irq;
  539. }
  540. bail:
  541. return check_too_hard(xics, icp);
  542. }