book3s_hv_rm_xics.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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 <linux/kernel_stat.h>
  13. #include <asm/kvm_book3s.h>
  14. #include <asm/kvm_ppc.h>
  15. #include <asm/hvcall.h>
  16. #include <asm/xics.h>
  17. #include <asm/synch.h>
  18. #include <asm/cputhreads.h>
  19. #include <asm/pgtable.h>
  20. #include <asm/ppc-opcode.h>
  21. #include <asm/pnv-pci.h>
  22. #include <asm/opal.h>
  23. #include <asm/smp.h>
  24. #include "book3s_xics.h"
  25. #define DEBUG_PASSUP
  26. int h_ipi_redirect = 1;
  27. EXPORT_SYMBOL(h_ipi_redirect);
  28. int kvm_irq_bypass = 1;
  29. EXPORT_SYMBOL(kvm_irq_bypass);
  30. static void icp_rm_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
  31. u32 new_irq, bool check_resend);
  32. static int xics_opal_set_server(unsigned int hw_irq, int server_cpu);
  33. /* -- ICS routines -- */
  34. static void ics_rm_check_resend(struct kvmppc_xics *xics,
  35. struct kvmppc_ics *ics, struct kvmppc_icp *icp)
  36. {
  37. int i;
  38. for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
  39. struct ics_irq_state *state = &ics->irq_state[i];
  40. if (state->resend)
  41. icp_rm_deliver_irq(xics, icp, state->number, true);
  42. }
  43. }
  44. /* -- ICP routines -- */
  45. #ifdef CONFIG_SMP
  46. static inline void icp_send_hcore_msg(int hcore, struct kvm_vcpu *vcpu)
  47. {
  48. int hcpu;
  49. hcpu = hcore << threads_shift;
  50. kvmppc_host_rm_ops_hv->rm_core[hcore].rm_data = vcpu;
  51. smp_muxed_ipi_set_message(hcpu, PPC_MSG_RM_HOST_ACTION);
  52. kvmppc_set_host_ipi(hcpu, 1);
  53. smp_mb();
  54. kvmhv_rm_send_ipi(hcpu);
  55. }
  56. #else
  57. static inline void icp_send_hcore_msg(int hcore, struct kvm_vcpu *vcpu) { }
  58. #endif
  59. /*
  60. * We start the search from our current CPU Id in the core map
  61. * and go in a circle until we get back to our ID looking for a
  62. * core that is running in host context and that hasn't already
  63. * been targeted for another rm_host_ops.
  64. *
  65. * In the future, could consider using a fairer algorithm (one
  66. * that distributes the IPIs better)
  67. *
  68. * Returns -1, if no CPU could be found in the host
  69. * Else, returns a CPU Id which has been reserved for use
  70. */
  71. static inline int grab_next_hostcore(int start,
  72. struct kvmppc_host_rm_core *rm_core, int max, int action)
  73. {
  74. bool success;
  75. int core;
  76. union kvmppc_rm_state old, new;
  77. for (core = start + 1; core < max; core++) {
  78. old = new = READ_ONCE(rm_core[core].rm_state);
  79. if (!old.in_host || old.rm_action)
  80. continue;
  81. /* Try to grab this host core if not taken already. */
  82. new.rm_action = action;
  83. success = cmpxchg64(&rm_core[core].rm_state.raw,
  84. old.raw, new.raw) == old.raw;
  85. if (success) {
  86. /*
  87. * Make sure that the store to the rm_action is made
  88. * visible before we return to caller (and the
  89. * subsequent store to rm_data) to synchronize with
  90. * the IPI handler.
  91. */
  92. smp_wmb();
  93. return core;
  94. }
  95. }
  96. return -1;
  97. }
  98. static inline int find_available_hostcore(int action)
  99. {
  100. int core;
  101. int my_core = smp_processor_id() >> threads_shift;
  102. struct kvmppc_host_rm_core *rm_core = kvmppc_host_rm_ops_hv->rm_core;
  103. core = grab_next_hostcore(my_core, rm_core, cpu_nr_cores(), action);
  104. if (core == -1)
  105. core = grab_next_hostcore(core, rm_core, my_core, action);
  106. return core;
  107. }
  108. static void icp_rm_set_vcpu_irq(struct kvm_vcpu *vcpu,
  109. struct kvm_vcpu *this_vcpu)
  110. {
  111. struct kvmppc_icp *this_icp = this_vcpu->arch.icp;
  112. int cpu;
  113. int hcore;
  114. /* Mark the target VCPU as having an interrupt pending */
  115. vcpu->stat.queue_intr++;
  116. set_bit(BOOK3S_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
  117. /* Kick self ? Just set MER and return */
  118. if (vcpu == this_vcpu) {
  119. mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_MER);
  120. return;
  121. }
  122. /*
  123. * Check if the core is loaded,
  124. * if not, find an available host core to post to wake the VCPU,
  125. * if we can't find one, set up state to eventually return too hard.
  126. */
  127. cpu = vcpu->arch.thread_cpu;
  128. if (cpu < 0 || cpu >= nr_cpu_ids) {
  129. hcore = -1;
  130. if (kvmppc_host_rm_ops_hv && h_ipi_redirect)
  131. hcore = find_available_hostcore(XICS_RM_KICK_VCPU);
  132. if (hcore != -1) {
  133. icp_send_hcore_msg(hcore, vcpu);
  134. } else {
  135. this_icp->rm_action |= XICS_RM_KICK_VCPU;
  136. this_icp->rm_kick_target = vcpu;
  137. }
  138. return;
  139. }
  140. smp_mb();
  141. kvmhv_rm_send_ipi(cpu);
  142. }
  143. static void icp_rm_clr_vcpu_irq(struct kvm_vcpu *vcpu)
  144. {
  145. /* Note: Only called on self ! */
  146. clear_bit(BOOK3S_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
  147. mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) & ~LPCR_MER);
  148. }
  149. static inline bool icp_rm_try_update(struct kvmppc_icp *icp,
  150. union kvmppc_icp_state old,
  151. union kvmppc_icp_state new)
  152. {
  153. struct kvm_vcpu *this_vcpu = local_paca->kvm_hstate.kvm_vcpu;
  154. bool success;
  155. /* Calculate new output value */
  156. new.out_ee = (new.xisr && (new.pending_pri < new.cppr));
  157. /* Attempt atomic update */
  158. success = cmpxchg64(&icp->state.raw, old.raw, new.raw) == old.raw;
  159. if (!success)
  160. goto bail;
  161. /*
  162. * Check for output state update
  163. *
  164. * Note that this is racy since another processor could be updating
  165. * the state already. This is why we never clear the interrupt output
  166. * here, we only ever set it. The clear only happens prior to doing
  167. * an update and only by the processor itself. Currently we do it
  168. * in Accept (H_XIRR) and Up_Cppr (H_XPPR).
  169. *
  170. * We also do not try to figure out whether the EE state has changed,
  171. * we unconditionally set it if the new state calls for it. The reason
  172. * for that is that we opportunistically remove the pending interrupt
  173. * flag when raising CPPR, so we need to set it back here if an
  174. * interrupt is still pending.
  175. */
  176. if (new.out_ee)
  177. icp_rm_set_vcpu_irq(icp->vcpu, this_vcpu);
  178. /* Expose the state change for debug purposes */
  179. this_vcpu->arch.icp->rm_dbgstate = new;
  180. this_vcpu->arch.icp->rm_dbgtgt = icp->vcpu;
  181. bail:
  182. return success;
  183. }
  184. static inline int check_too_hard(struct kvmppc_xics *xics,
  185. struct kvmppc_icp *icp)
  186. {
  187. return (xics->real_mode_dbg || icp->rm_action) ? H_TOO_HARD : H_SUCCESS;
  188. }
  189. static void icp_rm_check_resend(struct kvmppc_xics *xics,
  190. struct kvmppc_icp *icp)
  191. {
  192. u32 icsid;
  193. /* Order this load with the test for need_resend in the caller */
  194. smp_rmb();
  195. for_each_set_bit(icsid, icp->resend_map, xics->max_icsid + 1) {
  196. struct kvmppc_ics *ics = xics->ics[icsid];
  197. if (!test_and_clear_bit(icsid, icp->resend_map))
  198. continue;
  199. if (!ics)
  200. continue;
  201. ics_rm_check_resend(xics, ics, icp);
  202. }
  203. }
  204. static bool icp_rm_try_to_deliver(struct kvmppc_icp *icp, u32 irq, u8 priority,
  205. u32 *reject)
  206. {
  207. union kvmppc_icp_state old_state, new_state;
  208. bool success;
  209. do {
  210. old_state = new_state = READ_ONCE(icp->state);
  211. *reject = 0;
  212. /* See if we can deliver */
  213. success = new_state.cppr > priority &&
  214. new_state.mfrr > priority &&
  215. new_state.pending_pri > priority;
  216. /*
  217. * If we can, check for a rejection and perform the
  218. * delivery
  219. */
  220. if (success) {
  221. *reject = new_state.xisr;
  222. new_state.xisr = irq;
  223. new_state.pending_pri = priority;
  224. } else {
  225. /*
  226. * If we failed to deliver we set need_resend
  227. * so a subsequent CPPR state change causes us
  228. * to try a new delivery.
  229. */
  230. new_state.need_resend = true;
  231. }
  232. } while (!icp_rm_try_update(icp, old_state, new_state));
  233. return success;
  234. }
  235. static void icp_rm_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
  236. u32 new_irq, bool check_resend)
  237. {
  238. struct ics_irq_state *state;
  239. struct kvmppc_ics *ics;
  240. u32 reject;
  241. u16 src;
  242. /*
  243. * This is used both for initial delivery of an interrupt and
  244. * for subsequent rejection.
  245. *
  246. * Rejection can be racy vs. resends. We have evaluated the
  247. * rejection in an atomic ICP transaction which is now complete,
  248. * so potentially the ICP can already accept the interrupt again.
  249. *
  250. * So we need to retry the delivery. Essentially the reject path
  251. * boils down to a failed delivery. Always.
  252. *
  253. * Now the interrupt could also have moved to a different target,
  254. * thus we may need to re-do the ICP lookup as well
  255. */
  256. again:
  257. /* Get the ICS state and lock it */
  258. ics = kvmppc_xics_find_ics(xics, new_irq, &src);
  259. if (!ics) {
  260. /* Unsafe increment, but this does not need to be accurate */
  261. xics->err_noics++;
  262. return;
  263. }
  264. state = &ics->irq_state[src];
  265. /* Get a lock on the ICS */
  266. arch_spin_lock(&ics->lock);
  267. /* Get our server */
  268. if (!icp || state->server != icp->server_num) {
  269. icp = kvmppc_xics_find_server(xics->kvm, state->server);
  270. if (!icp) {
  271. /* Unsafe increment again*/
  272. xics->err_noicp++;
  273. goto out;
  274. }
  275. }
  276. if (check_resend)
  277. if (!state->resend)
  278. goto out;
  279. /* Clear the resend bit of that interrupt */
  280. state->resend = 0;
  281. /*
  282. * If masked, bail out
  283. *
  284. * Note: PAPR doesn't mention anything about masked pending
  285. * when doing a resend, only when doing a delivery.
  286. *
  287. * However that would have the effect of losing a masked
  288. * interrupt that was rejected and isn't consistent with
  289. * the whole masked_pending business which is about not
  290. * losing interrupts that occur while masked.
  291. *
  292. * I don't differentiate normal deliveries and resends, this
  293. * implementation will differ from PAPR and not lose such
  294. * interrupts.
  295. */
  296. if (state->priority == MASKED) {
  297. state->masked_pending = 1;
  298. goto out;
  299. }
  300. /*
  301. * Try the delivery, this will set the need_resend flag
  302. * in the ICP as part of the atomic transaction if the
  303. * delivery is not possible.
  304. *
  305. * Note that if successful, the new delivery might have itself
  306. * rejected an interrupt that was "delivered" before we took the
  307. * ics spin lock.
  308. *
  309. * In this case we do the whole sequence all over again for the
  310. * new guy. We cannot assume that the rejected interrupt is less
  311. * favored than the new one, and thus doesn't need to be delivered,
  312. * because by the time we exit icp_rm_try_to_deliver() the target
  313. * processor may well have already consumed & completed it, and thus
  314. * the rejected interrupt might actually be already acceptable.
  315. */
  316. if (icp_rm_try_to_deliver(icp, new_irq, state->priority, &reject)) {
  317. /*
  318. * Delivery was successful, did we reject somebody else ?
  319. */
  320. if (reject && reject != XICS_IPI) {
  321. arch_spin_unlock(&ics->lock);
  322. icp->n_reject++;
  323. new_irq = reject;
  324. check_resend = 0;
  325. goto again;
  326. }
  327. } else {
  328. /*
  329. * We failed to deliver the interrupt we need to set the
  330. * resend map bit and mark the ICS state as needing a resend
  331. */
  332. state->resend = 1;
  333. /*
  334. * Make sure when checking resend, we don't miss the resend
  335. * if resend_map bit is seen and cleared.
  336. */
  337. smp_wmb();
  338. set_bit(ics->icsid, icp->resend_map);
  339. /*
  340. * If the need_resend flag got cleared in the ICP some time
  341. * between icp_rm_try_to_deliver() atomic update and now, then
  342. * we know it might have missed the resend_map bit. So we
  343. * retry
  344. */
  345. smp_mb();
  346. if (!icp->state.need_resend) {
  347. state->resend = 0;
  348. arch_spin_unlock(&ics->lock);
  349. check_resend = 0;
  350. goto again;
  351. }
  352. }
  353. out:
  354. arch_spin_unlock(&ics->lock);
  355. }
  356. static void icp_rm_down_cppr(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
  357. u8 new_cppr)
  358. {
  359. union kvmppc_icp_state old_state, new_state;
  360. bool resend;
  361. /*
  362. * This handles several related states in one operation:
  363. *
  364. * ICP State: Down_CPPR
  365. *
  366. * Load CPPR with new value and if the XISR is 0
  367. * then check for resends:
  368. *
  369. * ICP State: Resend
  370. *
  371. * If MFRR is more favored than CPPR, check for IPIs
  372. * and notify ICS of a potential resend. This is done
  373. * asynchronously (when used in real mode, we will have
  374. * to exit here).
  375. *
  376. * We do not handle the complete Check_IPI as documented
  377. * here. In the PAPR, this state will be used for both
  378. * Set_MFRR and Down_CPPR. However, we know that we aren't
  379. * changing the MFRR state here so we don't need to handle
  380. * the case of an MFRR causing a reject of a pending irq,
  381. * this will have been handled when the MFRR was set in the
  382. * first place.
  383. *
  384. * Thus we don't have to handle rejects, only resends.
  385. *
  386. * When implementing real mode for HV KVM, resend will lead to
  387. * a H_TOO_HARD return and the whole transaction will be handled
  388. * in virtual mode.
  389. */
  390. do {
  391. old_state = new_state = READ_ONCE(icp->state);
  392. /* Down_CPPR */
  393. new_state.cppr = new_cppr;
  394. /*
  395. * Cut down Resend / Check_IPI / IPI
  396. *
  397. * The logic is that we cannot have a pending interrupt
  398. * trumped by an IPI at this point (see above), so we
  399. * know that either the pending interrupt is already an
  400. * IPI (in which case we don't care to override it) or
  401. * it's either more favored than us or non existent
  402. */
  403. if (new_state.mfrr < new_cppr &&
  404. new_state.mfrr <= new_state.pending_pri) {
  405. new_state.pending_pri = new_state.mfrr;
  406. new_state.xisr = XICS_IPI;
  407. }
  408. /* Latch/clear resend bit */
  409. resend = new_state.need_resend;
  410. new_state.need_resend = 0;
  411. } while (!icp_rm_try_update(icp, old_state, new_state));
  412. /*
  413. * Now handle resend checks. Those are asynchronous to the ICP
  414. * state update in HW (ie bus transactions) so we can handle them
  415. * separately here as well.
  416. */
  417. if (resend) {
  418. icp->n_check_resend++;
  419. icp_rm_check_resend(xics, icp);
  420. }
  421. }
  422. unsigned long xics_rm_h_xirr(struct kvm_vcpu *vcpu)
  423. {
  424. union kvmppc_icp_state old_state, new_state;
  425. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  426. struct kvmppc_icp *icp = vcpu->arch.icp;
  427. u32 xirr;
  428. if (!xics || !xics->real_mode)
  429. return H_TOO_HARD;
  430. /* First clear the interrupt */
  431. icp_rm_clr_vcpu_irq(icp->vcpu);
  432. /*
  433. * ICP State: Accept_Interrupt
  434. *
  435. * Return the pending interrupt (if any) along with the
  436. * current CPPR, then clear the XISR & set CPPR to the
  437. * pending priority
  438. */
  439. do {
  440. old_state = new_state = READ_ONCE(icp->state);
  441. xirr = old_state.xisr | (((u32)old_state.cppr) << 24);
  442. if (!old_state.xisr)
  443. break;
  444. new_state.cppr = new_state.pending_pri;
  445. new_state.pending_pri = 0xff;
  446. new_state.xisr = 0;
  447. } while (!icp_rm_try_update(icp, old_state, new_state));
  448. /* Return the result in GPR4 */
  449. vcpu->arch.regs.gpr[4] = xirr;
  450. return check_too_hard(xics, icp);
  451. }
  452. int xics_rm_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
  453. unsigned long mfrr)
  454. {
  455. union kvmppc_icp_state old_state, new_state;
  456. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  457. struct kvmppc_icp *icp, *this_icp = vcpu->arch.icp;
  458. u32 reject;
  459. bool resend;
  460. bool local;
  461. if (!xics || !xics->real_mode)
  462. return H_TOO_HARD;
  463. local = this_icp->server_num == server;
  464. if (local)
  465. icp = this_icp;
  466. else
  467. icp = kvmppc_xics_find_server(vcpu->kvm, server);
  468. if (!icp)
  469. return H_PARAMETER;
  470. /*
  471. * ICP state: Set_MFRR
  472. *
  473. * If the CPPR is more favored than the new MFRR, then
  474. * nothing needs to be done as there can be no XISR to
  475. * reject.
  476. *
  477. * ICP state: Check_IPI
  478. *
  479. * If the CPPR is less favored, then we might be replacing
  480. * an interrupt, and thus need to possibly reject it.
  481. *
  482. * ICP State: IPI
  483. *
  484. * Besides rejecting any pending interrupts, we also
  485. * update XISR and pending_pri to mark IPI as pending.
  486. *
  487. * PAPR does not describe this state, but if the MFRR is being
  488. * made less favored than its earlier value, there might be
  489. * a previously-rejected interrupt needing to be resent.
  490. * Ideally, we would want to resend only if
  491. * prio(pending_interrupt) < mfrr &&
  492. * prio(pending_interrupt) < cppr
  493. * where pending interrupt is the one that was rejected. But
  494. * we don't have that state, so we simply trigger a resend
  495. * whenever the MFRR is made less favored.
  496. */
  497. do {
  498. old_state = new_state = READ_ONCE(icp->state);
  499. /* Set_MFRR */
  500. new_state.mfrr = mfrr;
  501. /* Check_IPI */
  502. reject = 0;
  503. resend = false;
  504. if (mfrr < new_state.cppr) {
  505. /* Reject a pending interrupt if not an IPI */
  506. if (mfrr <= new_state.pending_pri) {
  507. reject = new_state.xisr;
  508. new_state.pending_pri = mfrr;
  509. new_state.xisr = XICS_IPI;
  510. }
  511. }
  512. if (mfrr > old_state.mfrr) {
  513. resend = new_state.need_resend;
  514. new_state.need_resend = 0;
  515. }
  516. } while (!icp_rm_try_update(icp, old_state, new_state));
  517. /* Handle reject in real mode */
  518. if (reject && reject != XICS_IPI) {
  519. this_icp->n_reject++;
  520. icp_rm_deliver_irq(xics, icp, reject, false);
  521. }
  522. /* Handle resends in real mode */
  523. if (resend) {
  524. this_icp->n_check_resend++;
  525. icp_rm_check_resend(xics, icp);
  526. }
  527. return check_too_hard(xics, this_icp);
  528. }
  529. int xics_rm_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr)
  530. {
  531. union kvmppc_icp_state old_state, new_state;
  532. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  533. struct kvmppc_icp *icp = vcpu->arch.icp;
  534. u32 reject;
  535. if (!xics || !xics->real_mode)
  536. return H_TOO_HARD;
  537. /*
  538. * ICP State: Set_CPPR
  539. *
  540. * We can safely compare the new value with the current
  541. * value outside of the transaction as the CPPR is only
  542. * ever changed by the processor on itself
  543. */
  544. if (cppr > icp->state.cppr) {
  545. icp_rm_down_cppr(xics, icp, cppr);
  546. goto bail;
  547. } else if (cppr == icp->state.cppr)
  548. return H_SUCCESS;
  549. /*
  550. * ICP State: Up_CPPR
  551. *
  552. * The processor is raising its priority, this can result
  553. * in a rejection of a pending interrupt:
  554. *
  555. * ICP State: Reject_Current
  556. *
  557. * We can remove EE from the current processor, the update
  558. * transaction will set it again if needed
  559. */
  560. icp_rm_clr_vcpu_irq(icp->vcpu);
  561. do {
  562. old_state = new_state = READ_ONCE(icp->state);
  563. reject = 0;
  564. new_state.cppr = cppr;
  565. if (cppr <= new_state.pending_pri) {
  566. reject = new_state.xisr;
  567. new_state.xisr = 0;
  568. new_state.pending_pri = 0xff;
  569. }
  570. } while (!icp_rm_try_update(icp, old_state, new_state));
  571. /*
  572. * Check for rejects. They are handled by doing a new delivery
  573. * attempt (see comments in icp_rm_deliver_irq).
  574. */
  575. if (reject && reject != XICS_IPI) {
  576. icp->n_reject++;
  577. icp_rm_deliver_irq(xics, icp, reject, false);
  578. }
  579. bail:
  580. return check_too_hard(xics, icp);
  581. }
  582. static int ics_rm_eoi(struct kvm_vcpu *vcpu, u32 irq)
  583. {
  584. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  585. struct kvmppc_icp *icp = vcpu->arch.icp;
  586. struct kvmppc_ics *ics;
  587. struct ics_irq_state *state;
  588. u16 src;
  589. u32 pq_old, pq_new;
  590. /*
  591. * ICS EOI handling: For LSI, if P bit is still set, we need to
  592. * resend it.
  593. *
  594. * For MSI, we move Q bit into P (and clear Q). If it is set,
  595. * resend it.
  596. */
  597. ics = kvmppc_xics_find_ics(xics, irq, &src);
  598. if (!ics)
  599. goto bail;
  600. state = &ics->irq_state[src];
  601. if (state->lsi)
  602. pq_new = state->pq_state;
  603. else
  604. do {
  605. pq_old = state->pq_state;
  606. pq_new = pq_old >> 1;
  607. } while (cmpxchg(&state->pq_state, pq_old, pq_new) != pq_old);
  608. if (pq_new & PQ_PRESENTED)
  609. icp_rm_deliver_irq(xics, NULL, irq, false);
  610. if (!hlist_empty(&vcpu->kvm->irq_ack_notifier_list)) {
  611. icp->rm_action |= XICS_RM_NOTIFY_EOI;
  612. icp->rm_eoied_irq = irq;
  613. }
  614. if (state->host_irq) {
  615. ++vcpu->stat.pthru_all;
  616. if (state->intr_cpu != -1) {
  617. int pcpu = raw_smp_processor_id();
  618. pcpu = cpu_first_thread_sibling(pcpu);
  619. ++vcpu->stat.pthru_host;
  620. if (state->intr_cpu != pcpu) {
  621. ++vcpu->stat.pthru_bad_aff;
  622. xics_opal_set_server(state->host_irq, pcpu);
  623. }
  624. state->intr_cpu = -1;
  625. }
  626. }
  627. bail:
  628. return check_too_hard(xics, icp);
  629. }
  630. int xics_rm_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr)
  631. {
  632. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  633. struct kvmppc_icp *icp = vcpu->arch.icp;
  634. u32 irq = xirr & 0x00ffffff;
  635. if (!xics || !xics->real_mode)
  636. return H_TOO_HARD;
  637. /*
  638. * ICP State: EOI
  639. *
  640. * Note: If EOI is incorrectly used by SW to lower the CPPR
  641. * value (ie more favored), we do not check for rejection of
  642. * a pending interrupt, this is a SW error and PAPR specifies
  643. * that we don't have to deal with it.
  644. *
  645. * The sending of an EOI to the ICS is handled after the
  646. * CPPR update
  647. *
  648. * ICP State: Down_CPPR which we handle
  649. * in a separate function as it's shared with H_CPPR.
  650. */
  651. icp_rm_down_cppr(xics, icp, xirr >> 24);
  652. /* IPIs have no EOI */
  653. if (irq == XICS_IPI)
  654. return check_too_hard(xics, icp);
  655. return ics_rm_eoi(vcpu, irq);
  656. }
  657. unsigned long eoi_rc;
  658. static void icp_eoi(struct irq_chip *c, u32 hwirq, __be32 xirr, bool *again)
  659. {
  660. void __iomem *xics_phys;
  661. int64_t rc;
  662. if (kvmhv_on_pseries()) {
  663. unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
  664. iosync();
  665. plpar_hcall_raw(H_EOI, retbuf, hwirq);
  666. return;
  667. }
  668. rc = pnv_opal_pci_msi_eoi(c, hwirq);
  669. if (rc)
  670. eoi_rc = rc;
  671. iosync();
  672. /* EOI it */
  673. xics_phys = local_paca->kvm_hstate.xics_phys;
  674. if (xics_phys) {
  675. __raw_rm_writel(xirr, xics_phys + XICS_XIRR);
  676. } else {
  677. rc = opal_int_eoi(be32_to_cpu(xirr));
  678. *again = rc > 0;
  679. }
  680. }
  681. static int xics_opal_set_server(unsigned int hw_irq, int server_cpu)
  682. {
  683. unsigned int mangle_cpu = get_hard_smp_processor_id(server_cpu) << 2;
  684. return opal_set_xive(hw_irq, mangle_cpu, DEFAULT_PRIORITY);
  685. }
  686. /*
  687. * Increment a per-CPU 32-bit unsigned integer variable.
  688. * Safe to call in real-mode. Handles vmalloc'ed addresses
  689. *
  690. * ToDo: Make this work for any integral type
  691. */
  692. static inline void this_cpu_inc_rm(unsigned int __percpu *addr)
  693. {
  694. unsigned long l;
  695. unsigned int *raddr;
  696. int cpu = smp_processor_id();
  697. raddr = per_cpu_ptr(addr, cpu);
  698. l = (unsigned long)raddr;
  699. if (REGION_ID(l) == VMALLOC_REGION_ID) {
  700. l = vmalloc_to_phys(raddr);
  701. raddr = (unsigned int *)l;
  702. }
  703. ++*raddr;
  704. }
  705. /*
  706. * We don't try to update the flags in the irq_desc 'istate' field in
  707. * here as would happen in the normal IRQ handling path for several reasons:
  708. * - state flags represent internal IRQ state and are not expected to be
  709. * updated outside the IRQ subsystem
  710. * - more importantly, these are useful for edge triggered interrupts,
  711. * IRQ probing, etc., but we are only handling MSI/MSIx interrupts here
  712. * and these states shouldn't apply to us.
  713. *
  714. * However, we do update irq_stats - we somewhat duplicate the code in
  715. * kstat_incr_irqs_this_cpu() for this since this function is defined
  716. * in irq/internal.h which we don't want to include here.
  717. * The only difference is that desc->kstat_irqs is an allocated per CPU
  718. * variable and could have been vmalloc'ed, so we can't directly
  719. * call __this_cpu_inc() on it. The kstat structure is a static
  720. * per CPU variable and it should be accessible by real-mode KVM.
  721. *
  722. */
  723. static void kvmppc_rm_handle_irq_desc(struct irq_desc *desc)
  724. {
  725. this_cpu_inc_rm(desc->kstat_irqs);
  726. __this_cpu_inc(kstat.irqs_sum);
  727. }
  728. long kvmppc_deliver_irq_passthru(struct kvm_vcpu *vcpu,
  729. __be32 xirr,
  730. struct kvmppc_irq_map *irq_map,
  731. struct kvmppc_passthru_irqmap *pimap,
  732. bool *again)
  733. {
  734. struct kvmppc_xics *xics;
  735. struct kvmppc_icp *icp;
  736. struct kvmppc_ics *ics;
  737. struct ics_irq_state *state;
  738. u32 irq;
  739. u16 src;
  740. u32 pq_old, pq_new;
  741. irq = irq_map->v_hwirq;
  742. xics = vcpu->kvm->arch.xics;
  743. icp = vcpu->arch.icp;
  744. kvmppc_rm_handle_irq_desc(irq_map->desc);
  745. ics = kvmppc_xics_find_ics(xics, irq, &src);
  746. if (!ics)
  747. return 2;
  748. state = &ics->irq_state[src];
  749. /* only MSIs register bypass producers, so it must be MSI here */
  750. do {
  751. pq_old = state->pq_state;
  752. pq_new = ((pq_old << 1) & 3) | PQ_PRESENTED;
  753. } while (cmpxchg(&state->pq_state, pq_old, pq_new) != pq_old);
  754. /* Test P=1, Q=0, this is the only case where we present */
  755. if (pq_new == PQ_PRESENTED)
  756. icp_rm_deliver_irq(xics, icp, irq, false);
  757. /* EOI the interrupt */
  758. icp_eoi(irq_desc_get_chip(irq_map->desc), irq_map->r_hwirq, xirr,
  759. again);
  760. if (check_too_hard(xics, icp) == H_TOO_HARD)
  761. return 2;
  762. else
  763. return -2;
  764. }
  765. /* --- Non-real mode XICS-related built-in routines --- */
  766. /**
  767. * Host Operations poked by RM KVM
  768. */
  769. static void rm_host_ipi_action(int action, void *data)
  770. {
  771. switch (action) {
  772. case XICS_RM_KICK_VCPU:
  773. kvmppc_host_rm_ops_hv->vcpu_kick(data);
  774. break;
  775. default:
  776. WARN(1, "Unexpected rm_action=%d data=%p\n", action, data);
  777. break;
  778. }
  779. }
  780. void kvmppc_xics_ipi_action(void)
  781. {
  782. int core;
  783. unsigned int cpu = smp_processor_id();
  784. struct kvmppc_host_rm_core *rm_corep;
  785. core = cpu >> threads_shift;
  786. rm_corep = &kvmppc_host_rm_ops_hv->rm_core[core];
  787. if (rm_corep->rm_data) {
  788. rm_host_ipi_action(rm_corep->rm_state.rm_action,
  789. rm_corep->rm_data);
  790. /* Order these stores against the real mode KVM */
  791. rm_corep->rm_data = NULL;
  792. smp_wmb();
  793. rm_corep->rm_state.rm_action = 0;
  794. }
  795. }