book3s_hv_rm_xics.c 22 KB

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