book3s_hv_rm_xics.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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. icp->n_reject++;
  241. new_irq = reject;
  242. goto again;
  243. }
  244. } else {
  245. /*
  246. * We failed to deliver the interrupt we need to set the
  247. * resend map bit and mark the ICS state as needing a resend
  248. */
  249. set_bit(ics->icsid, icp->resend_map);
  250. state->resend = 1;
  251. /*
  252. * If the need_resend flag got cleared in the ICP some time
  253. * between icp_rm_try_to_deliver() atomic update and now, then
  254. * we know it might have missed the resend_map bit. So we
  255. * retry
  256. */
  257. smp_mb();
  258. if (!icp->state.need_resend) {
  259. arch_spin_unlock(&ics->lock);
  260. goto again;
  261. }
  262. }
  263. out:
  264. arch_spin_unlock(&ics->lock);
  265. }
  266. static void icp_rm_down_cppr(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
  267. u8 new_cppr)
  268. {
  269. union kvmppc_icp_state old_state, new_state;
  270. bool resend;
  271. /*
  272. * This handles several related states in one operation:
  273. *
  274. * ICP State: Down_CPPR
  275. *
  276. * Load CPPR with new value and if the XISR is 0
  277. * then check for resends:
  278. *
  279. * ICP State: Resend
  280. *
  281. * If MFRR is more favored than CPPR, check for IPIs
  282. * and notify ICS of a potential resend. This is done
  283. * asynchronously (when used in real mode, we will have
  284. * to exit here).
  285. *
  286. * We do not handle the complete Check_IPI as documented
  287. * here. In the PAPR, this state will be used for both
  288. * Set_MFRR and Down_CPPR. However, we know that we aren't
  289. * changing the MFRR state here so we don't need to handle
  290. * the case of an MFRR causing a reject of a pending irq,
  291. * this will have been handled when the MFRR was set in the
  292. * first place.
  293. *
  294. * Thus we don't have to handle rejects, only resends.
  295. *
  296. * When implementing real mode for HV KVM, resend will lead to
  297. * a H_TOO_HARD return and the whole transaction will be handled
  298. * in virtual mode.
  299. */
  300. do {
  301. old_state = new_state = READ_ONCE(icp->state);
  302. /* Down_CPPR */
  303. new_state.cppr = new_cppr;
  304. /*
  305. * Cut down Resend / Check_IPI / IPI
  306. *
  307. * The logic is that we cannot have a pending interrupt
  308. * trumped by an IPI at this point (see above), so we
  309. * know that either the pending interrupt is already an
  310. * IPI (in which case we don't care to override it) or
  311. * it's either more favored than us or non existent
  312. */
  313. if (new_state.mfrr < new_cppr &&
  314. new_state.mfrr <= new_state.pending_pri) {
  315. new_state.pending_pri = new_state.mfrr;
  316. new_state.xisr = XICS_IPI;
  317. }
  318. /* Latch/clear resend bit */
  319. resend = new_state.need_resend;
  320. new_state.need_resend = 0;
  321. } while (!icp_rm_try_update(icp, old_state, new_state));
  322. /*
  323. * Now handle resend checks. Those are asynchronous to the ICP
  324. * state update in HW (ie bus transactions) so we can handle them
  325. * separately here as well.
  326. */
  327. if (resend) {
  328. icp->n_check_resend++;
  329. icp_rm_check_resend(xics, icp);
  330. }
  331. }
  332. unsigned long kvmppc_rm_h_xirr(struct kvm_vcpu *vcpu)
  333. {
  334. union kvmppc_icp_state old_state, new_state;
  335. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  336. struct kvmppc_icp *icp = vcpu->arch.icp;
  337. u32 xirr;
  338. if (!xics || !xics->real_mode)
  339. return H_TOO_HARD;
  340. /* First clear the interrupt */
  341. icp_rm_clr_vcpu_irq(icp->vcpu);
  342. /*
  343. * ICP State: Accept_Interrupt
  344. *
  345. * Return the pending interrupt (if any) along with the
  346. * current CPPR, then clear the XISR & set CPPR to the
  347. * pending priority
  348. */
  349. do {
  350. old_state = new_state = READ_ONCE(icp->state);
  351. xirr = old_state.xisr | (((u32)old_state.cppr) << 24);
  352. if (!old_state.xisr)
  353. break;
  354. new_state.cppr = new_state.pending_pri;
  355. new_state.pending_pri = 0xff;
  356. new_state.xisr = 0;
  357. } while (!icp_rm_try_update(icp, old_state, new_state));
  358. /* Return the result in GPR4 */
  359. vcpu->arch.gpr[4] = xirr;
  360. return check_too_hard(xics, icp);
  361. }
  362. int kvmppc_rm_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
  363. unsigned long mfrr)
  364. {
  365. union kvmppc_icp_state old_state, new_state;
  366. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  367. struct kvmppc_icp *icp, *this_icp = vcpu->arch.icp;
  368. u32 reject;
  369. bool resend;
  370. bool local;
  371. if (!xics || !xics->real_mode)
  372. return H_TOO_HARD;
  373. local = this_icp->server_num == server;
  374. if (local)
  375. icp = this_icp;
  376. else
  377. icp = kvmppc_xics_find_server(vcpu->kvm, server);
  378. if (!icp)
  379. return H_PARAMETER;
  380. /*
  381. * ICP state: Set_MFRR
  382. *
  383. * If the CPPR is more favored than the new MFRR, then
  384. * nothing needs to be done as there can be no XISR to
  385. * reject.
  386. *
  387. * ICP state: Check_IPI
  388. *
  389. * If the CPPR is less favored, then we might be replacing
  390. * an interrupt, and thus need to possibly reject it.
  391. *
  392. * ICP State: IPI
  393. *
  394. * Besides rejecting any pending interrupts, we also
  395. * update XISR and pending_pri to mark IPI as pending.
  396. *
  397. * PAPR does not describe this state, but if the MFRR is being
  398. * made less favored than its earlier value, there might be
  399. * a previously-rejected interrupt needing to be resent.
  400. * Ideally, we would want to resend only if
  401. * prio(pending_interrupt) < mfrr &&
  402. * prio(pending_interrupt) < cppr
  403. * where pending interrupt is the one that was rejected. But
  404. * we don't have that state, so we simply trigger a resend
  405. * whenever the MFRR is made less favored.
  406. */
  407. do {
  408. old_state = new_state = READ_ONCE(icp->state);
  409. /* Set_MFRR */
  410. new_state.mfrr = mfrr;
  411. /* Check_IPI */
  412. reject = 0;
  413. resend = false;
  414. if (mfrr < new_state.cppr) {
  415. /* Reject a pending interrupt if not an IPI */
  416. if (mfrr <= new_state.pending_pri) {
  417. reject = new_state.xisr;
  418. new_state.pending_pri = mfrr;
  419. new_state.xisr = XICS_IPI;
  420. }
  421. }
  422. if (mfrr > old_state.mfrr) {
  423. resend = new_state.need_resend;
  424. new_state.need_resend = 0;
  425. }
  426. } while (!icp_rm_try_update(icp, old_state, new_state));
  427. /* Handle reject in real mode */
  428. if (reject && reject != XICS_IPI) {
  429. this_icp->n_reject++;
  430. icp_rm_deliver_irq(xics, icp, reject);
  431. }
  432. /* Handle resends in real mode */
  433. if (resend) {
  434. this_icp->n_check_resend++;
  435. icp_rm_check_resend(xics, icp);
  436. }
  437. return check_too_hard(xics, this_icp);
  438. }
  439. int kvmppc_rm_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr)
  440. {
  441. union kvmppc_icp_state old_state, new_state;
  442. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  443. struct kvmppc_icp *icp = vcpu->arch.icp;
  444. u32 reject;
  445. if (!xics || !xics->real_mode)
  446. return H_TOO_HARD;
  447. /*
  448. * ICP State: Set_CPPR
  449. *
  450. * We can safely compare the new value with the current
  451. * value outside of the transaction as the CPPR is only
  452. * ever changed by the processor on itself
  453. */
  454. if (cppr > icp->state.cppr) {
  455. icp_rm_down_cppr(xics, icp, cppr);
  456. goto bail;
  457. } else if (cppr == icp->state.cppr)
  458. return H_SUCCESS;
  459. /*
  460. * ICP State: Up_CPPR
  461. *
  462. * The processor is raising its priority, this can result
  463. * in a rejection of a pending interrupt:
  464. *
  465. * ICP State: Reject_Current
  466. *
  467. * We can remove EE from the current processor, the update
  468. * transaction will set it again if needed
  469. */
  470. icp_rm_clr_vcpu_irq(icp->vcpu);
  471. do {
  472. old_state = new_state = READ_ONCE(icp->state);
  473. reject = 0;
  474. new_state.cppr = cppr;
  475. if (cppr <= new_state.pending_pri) {
  476. reject = new_state.xisr;
  477. new_state.xisr = 0;
  478. new_state.pending_pri = 0xff;
  479. }
  480. } while (!icp_rm_try_update(icp, old_state, new_state));
  481. /*
  482. * Check for rejects. They are handled by doing a new delivery
  483. * attempt (see comments in icp_rm_deliver_irq).
  484. */
  485. if (reject && reject != XICS_IPI) {
  486. icp->n_reject++;
  487. icp_rm_deliver_irq(xics, icp, reject);
  488. }
  489. bail:
  490. return check_too_hard(xics, icp);
  491. }
  492. int kvmppc_rm_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr)
  493. {
  494. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  495. struct kvmppc_icp *icp = vcpu->arch.icp;
  496. struct kvmppc_ics *ics;
  497. struct ics_irq_state *state;
  498. u32 irq = xirr & 0x00ffffff;
  499. u16 src;
  500. if (!xics || !xics->real_mode)
  501. return H_TOO_HARD;
  502. /*
  503. * ICP State: EOI
  504. *
  505. * Note: If EOI is incorrectly used by SW to lower the CPPR
  506. * value (ie more favored), we do not check for rejection of
  507. * a pending interrupt, this is a SW error and PAPR sepcifies
  508. * that we don't have to deal with it.
  509. *
  510. * The sending of an EOI to the ICS is handled after the
  511. * CPPR update
  512. *
  513. * ICP State: Down_CPPR which we handle
  514. * in a separate function as it's shared with H_CPPR.
  515. */
  516. icp_rm_down_cppr(xics, icp, xirr >> 24);
  517. /* IPIs have no EOI */
  518. if (irq == XICS_IPI)
  519. goto bail;
  520. /*
  521. * EOI handling: If the interrupt is still asserted, we need to
  522. * resend it. We can take a lockless "peek" at the ICS state here.
  523. *
  524. * "Message" interrupts will never have "asserted" set
  525. */
  526. ics = kvmppc_xics_find_ics(xics, irq, &src);
  527. if (!ics)
  528. goto bail;
  529. state = &ics->irq_state[src];
  530. /* Still asserted, resend it */
  531. if (state->asserted)
  532. icp_rm_deliver_irq(xics, icp, irq);
  533. if (!hlist_empty(&vcpu->kvm->irq_ack_notifier_list)) {
  534. icp->rm_action |= XICS_RM_NOTIFY_EOI;
  535. icp->rm_eoied_irq = irq;
  536. }
  537. bail:
  538. return check_too_hard(xics, icp);
  539. }