book3s_hv_rm_xics.c 24 KB

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