book3s_hv_ras.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright 2012 Paul Mackerras, IBM Corp. <[email protected]>
  5. */
  6. #include <linux/types.h>
  7. #include <linux/string.h>
  8. #include <linux/kvm.h>
  9. #include <linux/kvm_host.h>
  10. #include <linux/kernel.h>
  11. #include <asm/lppaca.h>
  12. #include <asm/opal.h>
  13. #include <asm/mce.h>
  14. #include <asm/machdep.h>
  15. #include <asm/cputhreads.h>
  16. #include <asm/hmi.h>
  17. #include <asm/kvm_ppc.h>
  18. /* SRR1 bits for machine check on POWER7 */
  19. #define SRR1_MC_LDSTERR (1ul << (63-42))
  20. #define SRR1_MC_IFETCH_SH (63-45)
  21. #define SRR1_MC_IFETCH_MASK 0x7
  22. #define SRR1_MC_IFETCH_SLBPAR 2 /* SLB parity error */
  23. #define SRR1_MC_IFETCH_SLBMULTI 3 /* SLB multi-hit */
  24. #define SRR1_MC_IFETCH_SLBPARMULTI 4 /* SLB parity + multi-hit */
  25. #define SRR1_MC_IFETCH_TLBMULTI 5 /* I-TLB multi-hit */
  26. /* DSISR bits for machine check on POWER7 */
  27. #define DSISR_MC_DERAT_MULTI 0x800 /* D-ERAT multi-hit */
  28. #define DSISR_MC_TLB_MULTI 0x400 /* D-TLB multi-hit */
  29. #define DSISR_MC_SLB_PARITY 0x100 /* SLB parity error */
  30. #define DSISR_MC_SLB_MULTI 0x080 /* SLB multi-hit */
  31. #define DSISR_MC_SLB_PARMULTI 0x040 /* SLB parity + multi-hit */
  32. /* POWER7 SLB flush and reload */
  33. static void reload_slb(struct kvm_vcpu *vcpu)
  34. {
  35. struct slb_shadow *slb;
  36. unsigned long i, n;
  37. /* First clear out SLB */
  38. asm volatile("slbmte %0,%0; slbia" : : "r" (0));
  39. /* Do they have an SLB shadow buffer registered? */
  40. slb = vcpu->arch.slb_shadow.pinned_addr;
  41. if (!slb)
  42. return;
  43. /* Sanity check */
  44. n = min_t(u32, be32_to_cpu(slb->persistent), SLB_MIN_SIZE);
  45. if ((void *) &slb->save_area[n] > vcpu->arch.slb_shadow.pinned_end)
  46. return;
  47. /* Load up the SLB from that */
  48. for (i = 0; i < n; ++i) {
  49. unsigned long rb = be64_to_cpu(slb->save_area[i].esid);
  50. unsigned long rs = be64_to_cpu(slb->save_area[i].vsid);
  51. rb = (rb & ~0xFFFul) | i; /* insert entry number */
  52. asm volatile("slbmte %0,%1" : : "r" (rs), "r" (rb));
  53. }
  54. }
  55. /*
  56. * On POWER7, see if we can handle a machine check that occurred inside
  57. * the guest in real mode, without switching to the host partition.
  58. */
  59. static long kvmppc_realmode_mc_power7(struct kvm_vcpu *vcpu)
  60. {
  61. unsigned long srr1 = vcpu->arch.shregs.msr;
  62. long handled = 1;
  63. if (srr1 & SRR1_MC_LDSTERR) {
  64. /* error on load/store */
  65. unsigned long dsisr = vcpu->arch.shregs.dsisr;
  66. if (dsisr & (DSISR_MC_SLB_PARMULTI | DSISR_MC_SLB_MULTI |
  67. DSISR_MC_SLB_PARITY | DSISR_MC_DERAT_MULTI)) {
  68. /* flush and reload SLB; flushes D-ERAT too */
  69. reload_slb(vcpu);
  70. dsisr &= ~(DSISR_MC_SLB_PARMULTI | DSISR_MC_SLB_MULTI |
  71. DSISR_MC_SLB_PARITY | DSISR_MC_DERAT_MULTI);
  72. }
  73. if (dsisr & DSISR_MC_TLB_MULTI) {
  74. tlbiel_all_lpid(vcpu->kvm->arch.radix);
  75. dsisr &= ~DSISR_MC_TLB_MULTI;
  76. }
  77. /* Any other errors we don't understand? */
  78. if (dsisr & 0xffffffffUL)
  79. handled = 0;
  80. }
  81. switch ((srr1 >> SRR1_MC_IFETCH_SH) & SRR1_MC_IFETCH_MASK) {
  82. case 0:
  83. break;
  84. case SRR1_MC_IFETCH_SLBPAR:
  85. case SRR1_MC_IFETCH_SLBMULTI:
  86. case SRR1_MC_IFETCH_SLBPARMULTI:
  87. reload_slb(vcpu);
  88. break;
  89. case SRR1_MC_IFETCH_TLBMULTI:
  90. tlbiel_all_lpid(vcpu->kvm->arch.radix);
  91. break;
  92. default:
  93. handled = 0;
  94. }
  95. return handled;
  96. }
  97. void kvmppc_realmode_machine_check(struct kvm_vcpu *vcpu)
  98. {
  99. struct machine_check_event mce_evt;
  100. long handled;
  101. if (vcpu->kvm->arch.fwnmi_enabled) {
  102. /* FWNMI guests handle their own recovery */
  103. handled = 0;
  104. } else {
  105. handled = kvmppc_realmode_mc_power7(vcpu);
  106. }
  107. /*
  108. * Now get the event and stash it in the vcpu struct so it can
  109. * be handled by the primary thread in virtual mode. We can't
  110. * call machine_check_queue_event() here if we are running on
  111. * an offline secondary thread.
  112. */
  113. if (get_mce_event(&mce_evt, MCE_EVENT_RELEASE)) {
  114. if (handled && mce_evt.version == MCE_V1)
  115. mce_evt.disposition = MCE_DISPOSITION_RECOVERED;
  116. } else {
  117. memset(&mce_evt, 0, sizeof(mce_evt));
  118. }
  119. vcpu->arch.mce_evt = mce_evt;
  120. }
  121. long kvmppc_p9_realmode_hmi_handler(struct kvm_vcpu *vcpu)
  122. {
  123. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  124. long ret = 0;
  125. /*
  126. * Unapply and clear the offset first. That way, if the TB was not
  127. * resynced then it will remain in host-offset, and if it was resynced
  128. * then it is brought into host-offset. Then the tb offset is
  129. * re-applied before continuing with the KVM exit.
  130. *
  131. * This way, we don't need to actually know whether not OPAL resynced
  132. * the timebase or do any of the complicated dance that the P7/8
  133. * path requires.
  134. */
  135. if (vc->tb_offset_applied) {
  136. u64 new_tb = mftb() - vc->tb_offset_applied;
  137. mtspr(SPRN_TBU40, new_tb);
  138. if ((mftb() & 0xffffff) < (new_tb & 0xffffff)) {
  139. new_tb += 0x1000000;
  140. mtspr(SPRN_TBU40, new_tb);
  141. }
  142. vc->tb_offset_applied = 0;
  143. }
  144. local_paca->hmi_irqs++;
  145. if (hmi_handle_debugtrig(NULL) >= 0) {
  146. ret = 1;
  147. goto out;
  148. }
  149. if (ppc_md.hmi_exception_early)
  150. ppc_md.hmi_exception_early(NULL);
  151. out:
  152. if (vc->tb_offset) {
  153. u64 new_tb = mftb() + vc->tb_offset;
  154. mtspr(SPRN_TBU40, new_tb);
  155. if ((mftb() & 0xffffff) < (new_tb & 0xffffff)) {
  156. new_tb += 0x1000000;
  157. mtspr(SPRN_TBU40, new_tb);
  158. }
  159. vc->tb_offset_applied = vc->tb_offset;
  160. }
  161. return ret;
  162. }
  163. /*
  164. * The following subcore HMI handling is all only for pre-POWER9 CPUs.
  165. */
  166. /* Check if dynamic split is in force and return subcore size accordingly. */
  167. static inline int kvmppc_cur_subcore_size(void)
  168. {
  169. if (local_paca->kvm_hstate.kvm_split_mode)
  170. return local_paca->kvm_hstate.kvm_split_mode->subcore_size;
  171. return threads_per_subcore;
  172. }
  173. void kvmppc_subcore_enter_guest(void)
  174. {
  175. int thread_id, subcore_id;
  176. thread_id = cpu_thread_in_core(local_paca->paca_index);
  177. subcore_id = thread_id / kvmppc_cur_subcore_size();
  178. local_paca->sibling_subcore_state->in_guest[subcore_id] = 1;
  179. }
  180. EXPORT_SYMBOL_GPL(kvmppc_subcore_enter_guest);
  181. void kvmppc_subcore_exit_guest(void)
  182. {
  183. int thread_id, subcore_id;
  184. thread_id = cpu_thread_in_core(local_paca->paca_index);
  185. subcore_id = thread_id / kvmppc_cur_subcore_size();
  186. local_paca->sibling_subcore_state->in_guest[subcore_id] = 0;
  187. }
  188. EXPORT_SYMBOL_GPL(kvmppc_subcore_exit_guest);
  189. static bool kvmppc_tb_resync_required(void)
  190. {
  191. if (test_and_set_bit(CORE_TB_RESYNC_REQ_BIT,
  192. &local_paca->sibling_subcore_state->flags))
  193. return false;
  194. return true;
  195. }
  196. static void kvmppc_tb_resync_done(void)
  197. {
  198. clear_bit(CORE_TB_RESYNC_REQ_BIT,
  199. &local_paca->sibling_subcore_state->flags);
  200. }
  201. /*
  202. * kvmppc_realmode_hmi_handler() is called only by primary thread during
  203. * guest exit path.
  204. *
  205. * There are multiple reasons why HMI could occur, one of them is
  206. * Timebase (TB) error. If this HMI is due to TB error, then TB would
  207. * have been in stopped state. The opal hmi handler Will fix it and
  208. * restore the TB value with host timebase value. For HMI caused due
  209. * to non-TB errors, opal hmi handler will not touch/restore TB register
  210. * and hence there won't be any change in TB value.
  211. *
  212. * Since we are not sure about the cause of this HMI, we can't be sure
  213. * about the content of TB register whether it holds guest or host timebase
  214. * value. Hence the idea is to resync the TB on every HMI, so that we
  215. * know about the exact state of the TB value. Resync TB call will
  216. * restore TB to host timebase.
  217. *
  218. * Things to consider:
  219. * - On TB error, HMI interrupt is reported on all the threads of the core
  220. * that has encountered TB error irrespective of split-core mode.
  221. * - The very first thread on the core that get chance to fix TB error
  222. * would rsync the TB with local chipTOD value.
  223. * - The resync TB is a core level action i.e. it will sync all the TBs
  224. * in that core independent of split-core mode. This means if we trigger
  225. * TB sync from a thread from one subcore, it would affect TB values of
  226. * sibling subcores of the same core.
  227. *
  228. * All threads need to co-ordinate before making opal hmi handler.
  229. * All threads will use sibling_subcore_state->in_guest[] (shared by all
  230. * threads in the core) in paca which holds information about whether
  231. * sibling subcores are in Guest mode or host mode. The in_guest[] array
  232. * is of size MAX_SUBCORE_PER_CORE=4, indexed using subcore id to set/unset
  233. * subcore status. Only primary threads from each subcore is responsible
  234. * to set/unset its designated array element while entering/exiting the
  235. * guset.
  236. *
  237. * After invoking opal hmi handler call, one of the thread (of entire core)
  238. * will need to resync the TB. Bit 63 from subcore state bitmap flags
  239. * (sibling_subcore_state->flags) will be used to co-ordinate between
  240. * primary threads to decide who takes up the responsibility.
  241. *
  242. * This is what we do:
  243. * - Primary thread from each subcore tries to set resync required bit[63]
  244. * of paca->sibling_subcore_state->flags.
  245. * - The first primary thread that is able to set the flag takes the
  246. * responsibility of TB resync. (Let us call it as thread leader)
  247. * - All other threads which are in host will call
  248. * wait_for_subcore_guest_exit() and wait for in_guest[0-3] from
  249. * paca->sibling_subcore_state to get cleared.
  250. * - All the primary thread will clear its subcore status from subcore
  251. * state in_guest[] array respectively.
  252. * - Once all primary threads clear in_guest[0-3], all of them will invoke
  253. * opal hmi handler.
  254. * - Now all threads will wait for TB resync to complete by invoking
  255. * wait_for_tb_resync() except the thread leader.
  256. * - Thread leader will do a TB resync by invoking opal_resync_timebase()
  257. * call and the it will clear the resync required bit.
  258. * - All other threads will now come out of resync wait loop and proceed
  259. * with individual execution.
  260. * - On return of this function, primary thread will signal all
  261. * secondary threads to proceed.
  262. * - All secondary threads will eventually call opal hmi handler on
  263. * their exit path.
  264. *
  265. * Returns 1 if the timebase offset should be applied, 0 if not.
  266. */
  267. long kvmppc_realmode_hmi_handler(void)
  268. {
  269. bool resync_req;
  270. local_paca->hmi_irqs++;
  271. if (hmi_handle_debugtrig(NULL) >= 0)
  272. return 1;
  273. /*
  274. * By now primary thread has already completed guest->host
  275. * partition switch but haven't signaled secondaries yet.
  276. * All the secondary threads on this subcore is waiting
  277. * for primary thread to signal them to go ahead.
  278. *
  279. * For threads from subcore which isn't in guest, they all will
  280. * wait until all other subcores on this core exit the guest.
  281. *
  282. * Now set the resync required bit. If you are the first to
  283. * set this bit then kvmppc_tb_resync_required() function will
  284. * return true. For rest all other subcores
  285. * kvmppc_tb_resync_required() will return false.
  286. *
  287. * If resync_req == true, then this thread is responsible to
  288. * initiate TB resync after hmi handler has completed.
  289. * All other threads on this core will wait until this thread
  290. * clears the resync required bit flag.
  291. */
  292. resync_req = kvmppc_tb_resync_required();
  293. /* Reset the subcore status to indicate it has exited guest */
  294. kvmppc_subcore_exit_guest();
  295. /*
  296. * Wait for other subcores on this core to exit the guest.
  297. * All the primary threads and threads from subcore that are
  298. * not in guest will wait here until all subcores are out
  299. * of guest context.
  300. */
  301. wait_for_subcore_guest_exit();
  302. /*
  303. * At this point we are sure that primary threads from each
  304. * subcore on this core have completed guest->host partition
  305. * switch. Now it is safe to call HMI handler.
  306. */
  307. if (ppc_md.hmi_exception_early)
  308. ppc_md.hmi_exception_early(NULL);
  309. /*
  310. * Check if this thread is responsible to resync TB.
  311. * All other threads will wait until this thread completes the
  312. * TB resync.
  313. */
  314. if (resync_req) {
  315. opal_resync_timebase();
  316. /* Reset TB resync req bit */
  317. kvmppc_tb_resync_done();
  318. } else {
  319. wait_for_tb_resync();
  320. }
  321. /*
  322. * Reset tb_offset_applied so the guest exit code won't try
  323. * to subtract the previous timebase offset from the timebase.
  324. */
  325. if (local_paca->kvm_hstate.kvm_vcore)
  326. local_paca->kvm_hstate.kvm_vcore->tb_offset_applied = 0;
  327. return 0;
  328. }