book3s_hv_rm_xics.c 23 KB

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