Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *
0004  * Copyright 2012 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
0005  */
0006 
0007 #include <linux/types.h>
0008 #include <linux/string.h>
0009 #include <linux/kvm.h>
0010 #include <linux/kvm_host.h>
0011 #include <linux/kernel.h>
0012 #include <asm/opal.h>
0013 #include <asm/mce.h>
0014 #include <asm/machdep.h>
0015 #include <asm/cputhreads.h>
0016 #include <asm/hmi.h>
0017 #include <asm/kvm_ppc.h>
0018 
0019 /* SRR1 bits for machine check on POWER7 */
0020 #define SRR1_MC_LDSTERR     (1ul << (63-42))
0021 #define SRR1_MC_IFETCH_SH   (63-45)
0022 #define SRR1_MC_IFETCH_MASK 0x7
0023 #define SRR1_MC_IFETCH_SLBPAR       2   /* SLB parity error */
0024 #define SRR1_MC_IFETCH_SLBMULTI     3   /* SLB multi-hit */
0025 #define SRR1_MC_IFETCH_SLBPARMULTI  4   /* SLB parity + multi-hit */
0026 #define SRR1_MC_IFETCH_TLBMULTI     5   /* I-TLB multi-hit */
0027 
0028 /* DSISR bits for machine check on POWER7 */
0029 #define DSISR_MC_DERAT_MULTI    0x800       /* D-ERAT multi-hit */
0030 #define DSISR_MC_TLB_MULTI  0x400       /* D-TLB multi-hit */
0031 #define DSISR_MC_SLB_PARITY 0x100       /* SLB parity error */
0032 #define DSISR_MC_SLB_MULTI  0x080       /* SLB multi-hit */
0033 #define DSISR_MC_SLB_PARMULTI   0x040       /* SLB parity + multi-hit */
0034 
0035 /* POWER7 SLB flush and reload */
0036 static void reload_slb(struct kvm_vcpu *vcpu)
0037 {
0038     struct slb_shadow *slb;
0039     unsigned long i, n;
0040 
0041     /* First clear out SLB */
0042     asm volatile("slbmte %0,%0; slbia" : : "r" (0));
0043 
0044     /* Do they have an SLB shadow buffer registered? */
0045     slb = vcpu->arch.slb_shadow.pinned_addr;
0046     if (!slb)
0047         return;
0048 
0049     /* Sanity check */
0050     n = min_t(u32, be32_to_cpu(slb->persistent), SLB_MIN_SIZE);
0051     if ((void *) &slb->save_area[n] > vcpu->arch.slb_shadow.pinned_end)
0052         return;
0053 
0054     /* Load up the SLB from that */
0055     for (i = 0; i < n; ++i) {
0056         unsigned long rb = be64_to_cpu(slb->save_area[i].esid);
0057         unsigned long rs = be64_to_cpu(slb->save_area[i].vsid);
0058 
0059         rb = (rb & ~0xFFFul) | i;   /* insert entry number */
0060         asm volatile("slbmte %0,%1" : : "r" (rs), "r" (rb));
0061     }
0062 }
0063 
0064 /*
0065  * On POWER7, see if we can handle a machine check that occurred inside
0066  * the guest in real mode, without switching to the host partition.
0067  */
0068 static long kvmppc_realmode_mc_power7(struct kvm_vcpu *vcpu)
0069 {
0070     unsigned long srr1 = vcpu->arch.shregs.msr;
0071     long handled = 1;
0072 
0073     if (srr1 & SRR1_MC_LDSTERR) {
0074         /* error on load/store */
0075         unsigned long dsisr = vcpu->arch.shregs.dsisr;
0076 
0077         if (dsisr & (DSISR_MC_SLB_PARMULTI | DSISR_MC_SLB_MULTI |
0078                  DSISR_MC_SLB_PARITY | DSISR_MC_DERAT_MULTI)) {
0079             /* flush and reload SLB; flushes D-ERAT too */
0080             reload_slb(vcpu);
0081             dsisr &= ~(DSISR_MC_SLB_PARMULTI | DSISR_MC_SLB_MULTI |
0082                    DSISR_MC_SLB_PARITY | DSISR_MC_DERAT_MULTI);
0083         }
0084         if (dsisr & DSISR_MC_TLB_MULTI) {
0085             tlbiel_all_lpid(vcpu->kvm->arch.radix);
0086             dsisr &= ~DSISR_MC_TLB_MULTI;
0087         }
0088         /* Any other errors we don't understand? */
0089         if (dsisr & 0xffffffffUL)
0090             handled = 0;
0091     }
0092 
0093     switch ((srr1 >> SRR1_MC_IFETCH_SH) & SRR1_MC_IFETCH_MASK) {
0094     case 0:
0095         break;
0096     case SRR1_MC_IFETCH_SLBPAR:
0097     case SRR1_MC_IFETCH_SLBMULTI:
0098     case SRR1_MC_IFETCH_SLBPARMULTI:
0099         reload_slb(vcpu);
0100         break;
0101     case SRR1_MC_IFETCH_TLBMULTI:
0102         tlbiel_all_lpid(vcpu->kvm->arch.radix);
0103         break;
0104     default:
0105         handled = 0;
0106     }
0107 
0108     return handled;
0109 }
0110 
0111 void kvmppc_realmode_machine_check(struct kvm_vcpu *vcpu)
0112 {
0113     struct machine_check_event mce_evt;
0114     long handled;
0115 
0116     if (vcpu->kvm->arch.fwnmi_enabled) {
0117         /* FWNMI guests handle their own recovery */
0118         handled = 0;
0119     } else {
0120         handled = kvmppc_realmode_mc_power7(vcpu);
0121     }
0122 
0123     /*
0124      * Now get the event and stash it in the vcpu struct so it can
0125      * be handled by the primary thread in virtual mode.  We can't
0126      * call machine_check_queue_event() here if we are running on
0127      * an offline secondary thread.
0128      */
0129     if (get_mce_event(&mce_evt, MCE_EVENT_RELEASE)) {
0130         if (handled && mce_evt.version == MCE_V1)
0131             mce_evt.disposition = MCE_DISPOSITION_RECOVERED;
0132     } else {
0133         memset(&mce_evt, 0, sizeof(mce_evt));
0134     }
0135 
0136     vcpu->arch.mce_evt = mce_evt;
0137 }
0138 
0139 
0140 long kvmppc_p9_realmode_hmi_handler(struct kvm_vcpu *vcpu)
0141 {
0142     struct kvmppc_vcore *vc = vcpu->arch.vcore;
0143     long ret = 0;
0144 
0145     /*
0146      * Unapply and clear the offset first. That way, if the TB was not
0147      * resynced then it will remain in host-offset, and if it was resynced
0148      * then it is brought into host-offset. Then the tb offset is
0149      * re-applied before continuing with the KVM exit.
0150      *
0151      * This way, we don't need to actually know whether not OPAL resynced
0152      * the timebase or do any of the complicated dance that the P7/8
0153      * path requires.
0154      */
0155     if (vc->tb_offset_applied) {
0156         u64 new_tb = mftb() - vc->tb_offset_applied;
0157         mtspr(SPRN_TBU40, new_tb);
0158         if ((mftb() & 0xffffff) < (new_tb & 0xffffff)) {
0159             new_tb += 0x1000000;
0160             mtspr(SPRN_TBU40, new_tb);
0161         }
0162         vc->tb_offset_applied = 0;
0163     }
0164 
0165     local_paca->hmi_irqs++;
0166 
0167     if (hmi_handle_debugtrig(NULL) >= 0) {
0168         ret = 1;
0169         goto out;
0170     }
0171 
0172     if (ppc_md.hmi_exception_early)
0173         ppc_md.hmi_exception_early(NULL);
0174 
0175 out:
0176     if (vc->tb_offset) {
0177         u64 new_tb = mftb() + vc->tb_offset;
0178         mtspr(SPRN_TBU40, new_tb);
0179         if ((mftb() & 0xffffff) < (new_tb & 0xffffff)) {
0180             new_tb += 0x1000000;
0181             mtspr(SPRN_TBU40, new_tb);
0182         }
0183         vc->tb_offset_applied = vc->tb_offset;
0184     }
0185 
0186     return ret;
0187 }
0188 
0189 /*
0190  * The following subcore HMI handling is all only for pre-POWER9 CPUs.
0191  */
0192 
0193 /* Check if dynamic split is in force and return subcore size accordingly. */
0194 static inline int kvmppc_cur_subcore_size(void)
0195 {
0196     if (local_paca->kvm_hstate.kvm_split_mode)
0197         return local_paca->kvm_hstate.kvm_split_mode->subcore_size;
0198 
0199     return threads_per_subcore;
0200 }
0201 
0202 void kvmppc_subcore_enter_guest(void)
0203 {
0204     int thread_id, subcore_id;
0205 
0206     thread_id = cpu_thread_in_core(local_paca->paca_index);
0207     subcore_id = thread_id / kvmppc_cur_subcore_size();
0208 
0209     local_paca->sibling_subcore_state->in_guest[subcore_id] = 1;
0210 }
0211 EXPORT_SYMBOL_GPL(kvmppc_subcore_enter_guest);
0212 
0213 void kvmppc_subcore_exit_guest(void)
0214 {
0215     int thread_id, subcore_id;
0216 
0217     thread_id = cpu_thread_in_core(local_paca->paca_index);
0218     subcore_id = thread_id / kvmppc_cur_subcore_size();
0219 
0220     local_paca->sibling_subcore_state->in_guest[subcore_id] = 0;
0221 }
0222 EXPORT_SYMBOL_GPL(kvmppc_subcore_exit_guest);
0223 
0224 static bool kvmppc_tb_resync_required(void)
0225 {
0226     if (test_and_set_bit(CORE_TB_RESYNC_REQ_BIT,
0227                 &local_paca->sibling_subcore_state->flags))
0228         return false;
0229 
0230     return true;
0231 }
0232 
0233 static void kvmppc_tb_resync_done(void)
0234 {
0235     clear_bit(CORE_TB_RESYNC_REQ_BIT,
0236             &local_paca->sibling_subcore_state->flags);
0237 }
0238 
0239 /*
0240  * kvmppc_realmode_hmi_handler() is called only by primary thread during
0241  * guest exit path.
0242  *
0243  * There are multiple reasons why HMI could occur, one of them is
0244  * Timebase (TB) error. If this HMI is due to TB error, then TB would
0245  * have been in stopped state. The opal hmi handler Will fix it and
0246  * restore the TB value with host timebase value. For HMI caused due
0247  * to non-TB errors, opal hmi handler will not touch/restore TB register
0248  * and hence there won't be any change in TB value.
0249  *
0250  * Since we are not sure about the cause of this HMI, we can't be sure
0251  * about the content of TB register whether it holds guest or host timebase
0252  * value. Hence the idea is to resync the TB on every HMI, so that we
0253  * know about the exact state of the TB value. Resync TB call will
0254  * restore TB to host timebase.
0255  *
0256  * Things to consider:
0257  * - On TB error, HMI interrupt is reported on all the threads of the core
0258  *   that has encountered TB error irrespective of split-core mode.
0259  * - The very first thread on the core that get chance to fix TB error
0260  *   would rsync the TB with local chipTOD value.
0261  * - The resync TB is a core level action i.e. it will sync all the TBs
0262  *   in that core independent of split-core mode. This means if we trigger
0263  *   TB sync from a thread from one subcore, it would affect TB values of
0264  *   sibling subcores of the same core.
0265  *
0266  * All threads need to co-ordinate before making opal hmi handler.
0267  * All threads will use sibling_subcore_state->in_guest[] (shared by all
0268  * threads in the core) in paca which holds information about whether
0269  * sibling subcores are in Guest mode or host mode. The in_guest[] array
0270  * is of size MAX_SUBCORE_PER_CORE=4, indexed using subcore id to set/unset
0271  * subcore status. Only primary threads from each subcore is responsible
0272  * to set/unset its designated array element while entering/exiting the
0273  * guset.
0274  *
0275  * After invoking opal hmi handler call, one of the thread (of entire core)
0276  * will need to resync the TB. Bit 63 from subcore state bitmap flags
0277  * (sibling_subcore_state->flags) will be used to co-ordinate between
0278  * primary threads to decide who takes up the responsibility.
0279  *
0280  * This is what we do:
0281  * - Primary thread from each subcore tries to set resync required bit[63]
0282  *   of paca->sibling_subcore_state->flags.
0283  * - The first primary thread that is able to set the flag takes the
0284  *   responsibility of TB resync. (Let us call it as thread leader)
0285  * - All other threads which are in host will call
0286  *   wait_for_subcore_guest_exit() and wait for in_guest[0-3] from
0287  *   paca->sibling_subcore_state to get cleared.
0288  * - All the primary thread will clear its subcore status from subcore
0289  *   state in_guest[] array respectively.
0290  * - Once all primary threads clear in_guest[0-3], all of them will invoke
0291  *   opal hmi handler.
0292  * - Now all threads will wait for TB resync to complete by invoking
0293  *   wait_for_tb_resync() except the thread leader.
0294  * - Thread leader will do a TB resync by invoking opal_resync_timebase()
0295  *   call and the it will clear the resync required bit.
0296  * - All other threads will now come out of resync wait loop and proceed
0297  *   with individual execution.
0298  * - On return of this function, primary thread will signal all
0299  *   secondary threads to proceed.
0300  * - All secondary threads will eventually call opal hmi handler on
0301  *   their exit path.
0302  *
0303  * Returns 1 if the timebase offset should be applied, 0 if not.
0304  */
0305 
0306 long kvmppc_realmode_hmi_handler(void)
0307 {
0308     bool resync_req;
0309 
0310     local_paca->hmi_irqs++;
0311 
0312     if (hmi_handle_debugtrig(NULL) >= 0)
0313         return 1;
0314 
0315     /*
0316      * By now primary thread has already completed guest->host
0317      * partition switch but haven't signaled secondaries yet.
0318      * All the secondary threads on this subcore is waiting
0319      * for primary thread to signal them to go ahead.
0320      *
0321      * For threads from subcore which isn't in guest, they all will
0322      * wait until all other subcores on this core exit the guest.
0323      *
0324      * Now set the resync required bit. If you are the first to
0325      * set this bit then kvmppc_tb_resync_required() function will
0326      * return true. For rest all other subcores
0327      * kvmppc_tb_resync_required() will return false.
0328      *
0329      * If resync_req == true, then this thread is responsible to
0330      * initiate TB resync after hmi handler has completed.
0331      * All other threads on this core will wait until this thread
0332      * clears the resync required bit flag.
0333      */
0334     resync_req = kvmppc_tb_resync_required();
0335 
0336     /* Reset the subcore status to indicate it has exited guest */
0337     kvmppc_subcore_exit_guest();
0338 
0339     /*
0340      * Wait for other subcores on this core to exit the guest.
0341      * All the primary threads and threads from subcore that are
0342      * not in guest will wait here until all subcores are out
0343      * of guest context.
0344      */
0345     wait_for_subcore_guest_exit();
0346 
0347     /*
0348      * At this point we are sure that primary threads from each
0349      * subcore on this core have completed guest->host partition
0350      * switch. Now it is safe to call HMI handler.
0351      */
0352     if (ppc_md.hmi_exception_early)
0353         ppc_md.hmi_exception_early(NULL);
0354 
0355     /*
0356      * Check if this thread is responsible to resync TB.
0357      * All other threads will wait until this thread completes the
0358      * TB resync.
0359      */
0360     if (resync_req) {
0361         opal_resync_timebase();
0362         /* Reset TB resync req bit */
0363         kvmppc_tb_resync_done();
0364     } else {
0365         wait_for_tb_resync();
0366     }
0367 
0368     /*
0369      * Reset tb_offset_applied so the guest exit code won't try
0370      * to subtract the previous timebase offset from the timebase.
0371      */
0372     if (local_paca->kvm_hstate.kvm_vcore)
0373         local_paca->kvm_hstate.kvm_vcore->tb_offset_applied = 0;
0374 
0375     return 0;
0376 }