book3s_hv_rm_xics.c 16 KB

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