book3s_hv_rm_xics.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Copyright 2012 Michael Ellerman, IBM Corporation.
  3. * Copyright 2012 Benjamin Herrenschmidt, IBM Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License, version 2, as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/kvm_host.h>
  11. #include <linux/err.h>
  12. #include <asm/kvm_book3s.h>
  13. #include <asm/kvm_ppc.h>
  14. #include <asm/hvcall.h>
  15. #include <asm/xics.h>
  16. #include <asm/debug.h>
  17. #include <asm/synch.h>
  18. #include <asm/ppc-opcode.h>
  19. #include "book3s_xics.h"
  20. #define DEBUG_PASSUP
  21. static inline void rm_writeb(unsigned long paddr, u8 val)
  22. {
  23. __asm__ __volatile__("sync; stbcix %0,0,%1"
  24. : : "r" (val), "r" (paddr) : "memory");
  25. }
  26. static void icp_rm_set_vcpu_irq(struct kvm_vcpu *vcpu,
  27. struct kvm_vcpu *this_vcpu)
  28. {
  29. struct kvmppc_icp *this_icp = this_vcpu->arch.icp;
  30. unsigned long xics_phys;
  31. int cpu;
  32. /* Mark the target VCPU as having an interrupt pending */
  33. vcpu->stat.queue_intr++;
  34. set_bit(BOOK3S_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
  35. /* Kick self ? Just set MER and return */
  36. if (vcpu == this_vcpu) {
  37. mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_MER);
  38. return;
  39. }
  40. /* Check if the core is loaded, if not, too hard */
  41. cpu = vcpu->cpu;
  42. if (cpu < 0 || cpu >= nr_cpu_ids) {
  43. this_icp->rm_action |= XICS_RM_KICK_VCPU;
  44. this_icp->rm_kick_target = vcpu;
  45. return;
  46. }
  47. /* In SMT cpu will always point to thread 0, we adjust it */
  48. cpu += vcpu->arch.ptid;
  49. /* Not too hard, then poke the target */
  50. xics_phys = paca[cpu].kvm_hstate.xics_phys;
  51. rm_writeb(xics_phys + XICS_MFRR, IPI_PRIORITY);
  52. }
  53. static void icp_rm_clr_vcpu_irq(struct kvm_vcpu *vcpu)
  54. {
  55. /* Note: Only called on self ! */
  56. clear_bit(BOOK3S_IRQPRIO_EXTERNAL_LEVEL,
  57. &vcpu->arch.pending_exceptions);
  58. mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) & ~LPCR_MER);
  59. }
  60. static inline bool icp_rm_try_update(struct kvmppc_icp *icp,
  61. union kvmppc_icp_state old,
  62. union kvmppc_icp_state new)
  63. {
  64. struct kvm_vcpu *this_vcpu = local_paca->kvm_hstate.kvm_vcpu;
  65. bool success;
  66. /* Calculate new output value */
  67. new.out_ee = (new.xisr && (new.pending_pri < new.cppr));
  68. /* Attempt atomic update */
  69. success = cmpxchg64(&icp->state.raw, old.raw, new.raw) == old.raw;
  70. if (!success)
  71. goto bail;
  72. /*
  73. * Check for output state update
  74. *
  75. * Note that this is racy since another processor could be updating
  76. * the state already. This is why we never clear the interrupt output
  77. * here, we only ever set it. The clear only happens prior to doing
  78. * an update and only by the processor itself. Currently we do it
  79. * in Accept (H_XIRR) and Up_Cppr (H_XPPR).
  80. *
  81. * We also do not try to figure out whether the EE state has changed,
  82. * we unconditionally set it if the new state calls for it. The reason
  83. * for that is that we opportunistically remove the pending interrupt
  84. * flag when raising CPPR, so we need to set it back here if an
  85. * interrupt is still pending.
  86. */
  87. if (new.out_ee)
  88. icp_rm_set_vcpu_irq(icp->vcpu, this_vcpu);
  89. /* Expose the state change for debug purposes */
  90. this_vcpu->arch.icp->rm_dbgstate = new;
  91. this_vcpu->arch.icp->rm_dbgtgt = icp->vcpu;
  92. bail:
  93. return success;
  94. }
  95. static inline int check_too_hard(struct kvmppc_xics *xics,
  96. struct kvmppc_icp *icp)
  97. {
  98. return (xics->real_mode_dbg || icp->rm_action) ? H_TOO_HARD : H_SUCCESS;
  99. }
  100. static void icp_rm_down_cppr(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
  101. u8 new_cppr)
  102. {
  103. union kvmppc_icp_state old_state, new_state;
  104. bool resend;
  105. /*
  106. * This handles several related states in one operation:
  107. *
  108. * ICP State: Down_CPPR
  109. *
  110. * Load CPPR with new value and if the XISR is 0
  111. * then check for resends:
  112. *
  113. * ICP State: Resend
  114. *
  115. * If MFRR is more favored than CPPR, check for IPIs
  116. * and notify ICS of a potential resend. This is done
  117. * asynchronously (when used in real mode, we will have
  118. * to exit here).
  119. *
  120. * We do not handle the complete Check_IPI as documented
  121. * here. In the PAPR, this state will be used for both
  122. * Set_MFRR and Down_CPPR. However, we know that we aren't
  123. * changing the MFRR state here so we don't need to handle
  124. * the case of an MFRR causing a reject of a pending irq,
  125. * this will have been handled when the MFRR was set in the
  126. * first place.
  127. *
  128. * Thus we don't have to handle rejects, only resends.
  129. *
  130. * When implementing real mode for HV KVM, resend will lead to
  131. * a H_TOO_HARD return and the whole transaction will be handled
  132. * in virtual mode.
  133. */
  134. do {
  135. old_state = new_state = ACCESS_ONCE(icp->state);
  136. /* Down_CPPR */
  137. new_state.cppr = new_cppr;
  138. /*
  139. * Cut down Resend / Check_IPI / IPI
  140. *
  141. * The logic is that we cannot have a pending interrupt
  142. * trumped by an IPI at this point (see above), so we
  143. * know that either the pending interrupt is already an
  144. * IPI (in which case we don't care to override it) or
  145. * it's either more favored than us or non existent
  146. */
  147. if (new_state.mfrr < new_cppr &&
  148. new_state.mfrr <= new_state.pending_pri) {
  149. new_state.pending_pri = new_state.mfrr;
  150. new_state.xisr = XICS_IPI;
  151. }
  152. /* Latch/clear resend bit */
  153. resend = new_state.need_resend;
  154. new_state.need_resend = 0;
  155. } while (!icp_rm_try_update(icp, old_state, new_state));
  156. /*
  157. * Now handle resend checks. Those are asynchronous to the ICP
  158. * state update in HW (ie bus transactions) so we can handle them
  159. * separately here as well.
  160. */
  161. if (resend)
  162. icp->rm_action |= XICS_RM_CHECK_RESEND;
  163. }
  164. unsigned long kvmppc_rm_h_xirr(struct kvm_vcpu *vcpu)
  165. {
  166. union kvmppc_icp_state old_state, new_state;
  167. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  168. struct kvmppc_icp *icp = vcpu->arch.icp;
  169. u32 xirr;
  170. if (!xics || !xics->real_mode)
  171. return H_TOO_HARD;
  172. /* First clear the interrupt */
  173. icp_rm_clr_vcpu_irq(icp->vcpu);
  174. /*
  175. * ICP State: Accept_Interrupt
  176. *
  177. * Return the pending interrupt (if any) along with the
  178. * current CPPR, then clear the XISR & set CPPR to the
  179. * pending priority
  180. */
  181. do {
  182. old_state = new_state = ACCESS_ONCE(icp->state);
  183. xirr = old_state.xisr | (((u32)old_state.cppr) << 24);
  184. if (!old_state.xisr)
  185. break;
  186. new_state.cppr = new_state.pending_pri;
  187. new_state.pending_pri = 0xff;
  188. new_state.xisr = 0;
  189. } while (!icp_rm_try_update(icp, old_state, new_state));
  190. /* Return the result in GPR4 */
  191. vcpu->arch.gpr[4] = xirr;
  192. return check_too_hard(xics, icp);
  193. }
  194. int kvmppc_rm_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
  195. unsigned long mfrr)
  196. {
  197. union kvmppc_icp_state old_state, new_state;
  198. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  199. struct kvmppc_icp *icp, *this_icp = vcpu->arch.icp;
  200. u32 reject;
  201. bool resend;
  202. bool local;
  203. if (!xics || !xics->real_mode)
  204. return H_TOO_HARD;
  205. local = this_icp->server_num == server;
  206. if (local)
  207. icp = this_icp;
  208. else
  209. icp = kvmppc_xics_find_server(vcpu->kvm, server);
  210. if (!icp)
  211. return H_PARAMETER;
  212. /*
  213. * ICP state: Set_MFRR
  214. *
  215. * If the CPPR is more favored than the new MFRR, then
  216. * nothing needs to be done as there can be no XISR to
  217. * reject.
  218. *
  219. * If the CPPR is less favored, then we might be replacing
  220. * an interrupt, and thus need to possibly reject it as in
  221. *
  222. * ICP state: Check_IPI
  223. */
  224. do {
  225. old_state = new_state = ACCESS_ONCE(icp->state);
  226. /* Set_MFRR */
  227. new_state.mfrr = mfrr;
  228. /* Check_IPI */
  229. reject = 0;
  230. resend = false;
  231. if (mfrr < new_state.cppr) {
  232. /* Reject a pending interrupt if not an IPI */
  233. if (mfrr <= new_state.pending_pri)
  234. reject = new_state.xisr;
  235. new_state.pending_pri = mfrr;
  236. new_state.xisr = XICS_IPI;
  237. }
  238. if (mfrr > old_state.mfrr && mfrr > new_state.cppr) {
  239. resend = new_state.need_resend;
  240. new_state.need_resend = 0;
  241. }
  242. } while (!icp_rm_try_update(icp, old_state, new_state));
  243. /* Pass rejects to virtual mode */
  244. if (reject && reject != XICS_IPI) {
  245. this_icp->rm_action |= XICS_RM_REJECT;
  246. this_icp->rm_reject = reject;
  247. }
  248. /* Pass resends to virtual mode */
  249. if (resend)
  250. this_icp->rm_action |= XICS_RM_CHECK_RESEND;
  251. return check_too_hard(xics, this_icp);
  252. }
  253. int kvmppc_rm_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr)
  254. {
  255. union kvmppc_icp_state old_state, new_state;
  256. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  257. struct kvmppc_icp *icp = vcpu->arch.icp;
  258. u32 reject;
  259. if (!xics || !xics->real_mode)
  260. return H_TOO_HARD;
  261. /*
  262. * ICP State: Set_CPPR
  263. *
  264. * We can safely compare the new value with the current
  265. * value outside of the transaction as the CPPR is only
  266. * ever changed by the processor on itself
  267. */
  268. if (cppr > icp->state.cppr) {
  269. icp_rm_down_cppr(xics, icp, cppr);
  270. goto bail;
  271. } else if (cppr == icp->state.cppr)
  272. return H_SUCCESS;
  273. /*
  274. * ICP State: Up_CPPR
  275. *
  276. * The processor is raising its priority, this can result
  277. * in a rejection of a pending interrupt:
  278. *
  279. * ICP State: Reject_Current
  280. *
  281. * We can remove EE from the current processor, the update
  282. * transaction will set it again if needed
  283. */
  284. icp_rm_clr_vcpu_irq(icp->vcpu);
  285. do {
  286. old_state = new_state = ACCESS_ONCE(icp->state);
  287. reject = 0;
  288. new_state.cppr = cppr;
  289. if (cppr <= new_state.pending_pri) {
  290. reject = new_state.xisr;
  291. new_state.xisr = 0;
  292. new_state.pending_pri = 0xff;
  293. }
  294. } while (!icp_rm_try_update(icp, old_state, new_state));
  295. /* Pass rejects to virtual mode */
  296. if (reject && reject != XICS_IPI) {
  297. icp->rm_action |= XICS_RM_REJECT;
  298. icp->rm_reject = reject;
  299. }
  300. bail:
  301. return check_too_hard(xics, icp);
  302. }
  303. int kvmppc_rm_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr)
  304. {
  305. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  306. struct kvmppc_icp *icp = vcpu->arch.icp;
  307. struct kvmppc_ics *ics;
  308. struct ics_irq_state *state;
  309. u32 irq = xirr & 0x00ffffff;
  310. u16 src;
  311. if (!xics || !xics->real_mode)
  312. return H_TOO_HARD;
  313. /*
  314. * ICP State: EOI
  315. *
  316. * Note: If EOI is incorrectly used by SW to lower the CPPR
  317. * value (ie more favored), we do not check for rejection of
  318. * a pending interrupt, this is a SW error and PAPR sepcifies
  319. * that we don't have to deal with it.
  320. *
  321. * The sending of an EOI to the ICS is handled after the
  322. * CPPR update
  323. *
  324. * ICP State: Down_CPPR which we handle
  325. * in a separate function as it's shared with H_CPPR.
  326. */
  327. icp_rm_down_cppr(xics, icp, xirr >> 24);
  328. /* IPIs have no EOI */
  329. if (irq == XICS_IPI)
  330. goto bail;
  331. /*
  332. * EOI handling: If the interrupt is still asserted, we need to
  333. * resend it. We can take a lockless "peek" at the ICS state here.
  334. *
  335. * "Message" interrupts will never have "asserted" set
  336. */
  337. ics = kvmppc_xics_find_ics(xics, irq, &src);
  338. if (!ics)
  339. goto bail;
  340. state = &ics->irq_state[src];
  341. /* Still asserted, resend it, we make it look like a reject */
  342. if (state->asserted) {
  343. icp->rm_action |= XICS_RM_REJECT;
  344. icp->rm_reject = irq;
  345. }
  346. bail:
  347. return check_too_hard(xics, icp);
  348. }