Skip to contentSkip to conversation

powerpc/64s: move machine check SLB flushing to mm/slb.c

PATCH Superseded Nicholas Piggin 3 participants 6 messages Aug 3, 2018 – Aug 9, 2018

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Nicholas Piggin

author reviewer
#0 lore raw

PATCH 1/2 powerpc/64s: move machine check SLB flushing to mm/slb.c

The machine check code that flushes and restores bolted segments in real mode belongs in mm/slb.c. This will be used by pseries machine check and idle code.

---
 arch/powerpc/include/asm/book3s/64/mmu-hash.h |  3 ++
 arch/powerpc/kernel/mce_power.c               | 21 ++--------
 arch/powerpc/mm/slb.c                         | 38 +++++++++++++++++++
 3 files changed, 44 insertions(+), 18 deletions(-)
arch/powerpc/include/asm/book3s/64/mmu-hash.h +3 -0
index 2f74bdc805e0..d4e398185b3a 100644
@@ -497,6 +497,9 @@ extern void hpte_init_native(void);
 
 extern void slb_initialize(void);
 extern void slb_flush_and_rebolt(void);
+extern void slb_flush_all_realmode(void);
+extern void __slb_restore_bolted_realmode(void);
+extern void slb_restore_bolted_realmode(void);
 
 extern void slb_vmalloc_update(void);
 extern void slb_set_size(u16 size);
arch/powerpc/kernel/mce_power.c +3 -18
index d6756af6ec78..50f7b9817246 100644
@@ -62,11 +62,8 @@ static unsigned long addr_to_pfn(struct pt_regs *regs, unsigned long addr)
 #ifdef CONFIG_PPC_BOOK3S_64
 static void flush_and_reload_slb(void)
 {
-	struct slb_shadow *slb;
-	unsigned long i, n;
-
 	/* Invalidate all SLBs */
-	asm volatile("slbmte %0,%0; slbia" : : "r" (0));
+	slb_flush_all_realmode();
 
 #ifdef CONFIG_KVM_BOOK3S_HANDLER
 	/*
@@ -76,22 +73,10 @@ static void flush_and_reload_slb(void)
 	if (get_paca()->kvm_hstate.in_guest)
 		return;
 #endif
-
-	/* For host kernel, reload the SLBs from shadow SLB buffer. */
-	slb = get_slb_shadow();
-	if (!slb)
+	if (early_radix_enabled())
 		return;
 
-	n = min_t(u32, be32_to_cpu(slb->persistent), SLB_MIN_SIZE);
-
-	/* Load up the SLB entries from shadow SLB */
-	for (i = 0; i < n; i++) {
-		unsigned long rb = be64_to_cpu(slb->save_area[i].esid);
-		unsigned long rs = be64_to_cpu(slb->save_area[i].vsid);
-
-		rb = (rb & ~0xFFFul) | i;
-		asm volatile("slbmte %0,%1" : : "r" (rs), "r" (rb));
-	}
+	slb_restore_bolted_realmode();
 }
 #endif
 
arch/powerpc/mm/slb.c +38 -0
index cb796724a6fc..136db8652577 100644
@@ -90,6 +90,44 @@ static inline void create_shadowed_slbe(unsigned long ea, int ssize,
 		     : "memory" );
 }
 
+/*
+ * Insert bolted entries into SLB (which may not be empty).
+ */
+void __slb_restore_bolted_realmode(void)
+{
+	struct slb_shadow *p = get_slb_shadow();
+	enum slb_index index;
+
+	 /* No isync needed because realmode. */
+	for (index = 0; index < SLB_NUM_BOLTED; index++) {
+		asm volatile("slbmte  %0,%1" :
+		     : "r" (be64_to_cpu(p->save_area[index].vsid)),
+		       "r" (be64_to_cpu(p->save_area[index].esid)));
+	}
+}
+
+/*
+ * Insert bolted entries into an empty SLB.
+ * This is not the same as rebolt because the bolted segments
+ * (e.g., kstack) are not changed (rebolted).
+ */
+void slb_restore_bolted_realmode(void)
+{
+	__slb_restore_bolted_realmode();
+	get_paca()->slb_cache_ptr = 0;
+}
+
+/*
+ * This flushes all SLB entries including 0, so it must be realmode.
+ */
+void slb_flush_all_realmode(void)
+{
+	/*
+	 * This flushes all SLB entries including 0, so it must be realmode.
+	 */
+	asm volatile("slbmte %0,%0; slbia" : : "r" (0));
+}
+
 static void __slb_flush_and_rebolt(void)
 {
 	/* If you change this make sure you change SLB_NUM_BOLTED
Signature · 1 line
2.17.0
author reviewer
#1 lore raw

PATCH 2/2 powerpc/64s: reimplement book3s idle code in C

Reimplement Book3S idle code in C, moving POWER7/8/9 implementation speific HV idle code to the powernv platform code. Generic book3s assembly stubs are kept in common code and used only to save and restore the stack frame and non-volatile GPRs just before going to idle and just after waking up, which can return into C code.

Moving the HMI, SPR, OPAL, locking, etc. to C is the only real way this stuff will cope with non-trivial new CPU implementation details, firmware changes, etc., without becoming unmaintainable.

This is not a strict translation to C code, there are some differences.

- Idle wakeup no longer uses the ->cpu_restore call to reinit SPRs, but saves and restores them all explicitly.

- The optimisation where EC=ESL=0 idle modes did not have to save GPRs or change MSR is restored, because it's now simple to do. State loss modes that did not actually lose GPRs can use this optimization too.

- KVM secondary entry code is now more of a call/return style rather than spaghetti. nap_state_lost is not required beccause KVM always returns via NVGPR restorig path.

This seems pretty solid, so needs more review and testing now. The KVM changes are pretty significant and complicated. POWER7 needs to be tested too.

Open question:
- Why does P9 restore some of the PMU SPRs (but not others), and P8 only zeroes them?

Since RFC v1:
- Now tested and working with POWER9 hash and radix.
- KVM support added. This took a bit of work to untangle and might still have some issues, but POWER9 seems to work including hash on radix with dependent threads mode.
- This snowballed a bit because of KVM and other details making it not feasible to leave POWER7/8 code alone. That's only half done at the moment.
- So far this trades about 800 lines of asm for 500 of C. With POWER7/8 support done it might be another hundred or so lines of C.

Since RFC v2:
- Fixed deep state SLB reloading
- Now tested and working with POWER8.
- Accounted for most feedback.

Since RFC v3:
- Rebased to powerpc merge + idle state bugfix
- Split SLB flush/restore code out and shared with MCE code (pseries MCE patches can also use).
- More testing on POWER8 including KVM with secondaries.
- Performance testing looks good. EC=ESL=0 is about 5% faster, other stop states look a little faster too.
- Adjusted SPR saving to handler POWER7, haven't tested it.

---
 arch/powerpc/include/asm/cpuidle.h       |  19 +-
 arch/powerpc/include/asm/paca.h          |  40 +-
 arch/powerpc/include/asm/processor.h     |   9 +-
 arch/powerpc/include/asm/reg.h           |   7 +-
 arch/powerpc/kernel/asm-offsets.c        |  18 -
 arch/powerpc/kernel/dt_cpu_ftrs.c        |  21 +-
 arch/powerpc/kernel/exceptions-64s.S     |  17 +-
 arch/powerpc/kernel/idle_book3s.S        | 998 ++---------------------
 arch/powerpc/kernel/setup-common.c       |   4 +-
 arch/powerpc/kvm/book3s_hv_rmhandlers.S  |  90 +-
 arch/powerpc/platforms/powernv/idle.c    | 816 ++++++++++++++----
 arch/powerpc/platforms/powernv/subcore.c |   2 +-
 arch/powerpc/xmon/xmon.c                 |  25 +-
 13 files changed, 859 insertions(+), 1207 deletions(-)
arch/powerpc/include/asm/cpuidle.h +3 -16
index 43e5f31fe64d..9844b3ded187 100644
@@ -27,10 +27,11 @@
  * the THREAD_WINKLE_BITS are set, which indicate which threads have not
  * yet woken from the winkle state.
  */
-#define PNV_CORE_IDLE_LOCK_BIT			0x10000000
+#define NR_PNV_CORE_IDLE_LOCK_BIT		28
+#define PNV_CORE_IDLE_LOCK_BIT			(1ULL << NR_PNV_CORE_IDLE_LOCK_BIT)
 
+#define PNV_CORE_IDLE_WINKLE_COUNT_SHIFT	16
 #define PNV_CORE_IDLE_WINKLE_COUNT		0x00010000
-#define PNV_CORE_IDLE_WINKLE_COUNT_ALL_BIT	0x00080000
 #define PNV_CORE_IDLE_WINKLE_COUNT_BITS		0x000F0000
 #define PNV_CORE_IDLE_THREAD_WINKLE_BITS_SHIFT	8
 #define PNV_CORE_IDLE_THREAD_WINKLE_BITS	0x0000FF00
@@ -68,16 +69,6 @@
 #define ERR_DEEP_STATE_ESL_MISMATCH	-2
 
 #ifndef __ASSEMBLY__
-/* Additional SPRs that need to be saved/restored during stop */
-struct stop_sprs {
-	u64 pid;
-	u64 ldbar;
-	u64 fscr;
-	u64 hfscr;
-	u64 mmcr1;
-	u64 mmcr2;
-	u64 mmcra;
-};
 
 #define PNV_IDLE_NAME_LEN    16
 struct pnv_idle_states_t {
@@ -92,10 +83,6 @@ struct pnv_idle_states_t {
 
 extern struct pnv_idle_states_t *pnv_idle_states;
 extern int nr_pnv_idle_states;
-extern u32 pnv_fastsleep_workaround_at_entry[];
-extern u32 pnv_fastsleep_workaround_at_exit[];
-
-extern u64 pnv_first_deep_stop_state;
 
 unsigned long pnv_cpu_offline(unsigned int cpu);
 int validate_psscr_val_mask(u64 *psscr_val, u64 *psscr_mask, u32 flags);
arch/powerpc/include/asm/paca.h +23 -17
index 4e9cede5a7e7..d2cee5ebaaa1 100644
@@ -168,7 +168,6 @@ struct paca_struct {
 	u8 irq_happened;		/* irq happened while soft-disabled */
 	u8 io_sync;			/* writel() needs spin_unlock sync */
 	u8 irq_work_pending;		/* IRQ_WORK interrupt while soft-disable */
-	u8 nap_state_lost;		/* NV GPR values lost in power7_idle */
 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
 	u8 pmcregs_in_use;		/* pseries puts this in lppaca */
 #endif
@@ -178,23 +177,30 @@ struct paca_struct {
 #endif
 
 #ifdef CONFIG_PPC_POWERNV
-	/* Per-core mask tracking idle threads and a lock bit-[L][TTTTTTTT] */
-	u32 *core_idle_state_ptr;
-	u8 thread_idle_state;		/* PNV_THREAD_RUNNING/NAP/SLEEP	*/
-	/* Mask to indicate thread id in core */
-	u8 thread_mask;
-	/* Mask to denote subcore sibling threads */
-	u8 subcore_sibling_mask;
-	/* Flag to request this thread not to stop */
-	atomic_t dont_stop;
-	/* The PSSCR value that the kernel requested before going to stop */
-	u64 requested_psscr;
+	/* PowerNV idle fields */
+	/* PNV_CORE_IDLE_* bits, all siblings work on thread 0 paca */
+	unsigned long idle_state;
+	union {
+		/* P7/P8 specific fields */
+		struct {
+			/* PNV_THREAD_RUNNING/NAP/SLEEP	*/
+			u8 thread_idle_state;
+			/* Mask to indicate thread id in core */
+			u8 thread_mask;
+			/* Mask to denote subcore sibling threads */
+			u8 subcore_sibling_mask;
+		};
 
-	/*
-	 * Save area for additional SPRs that need to be
-	 * saved/restored during cpuidle stop.
-	 */
-	struct stop_sprs stop_sprs;
+		/* P9 specific fields */
+		struct {
+#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
+			/* The PSSCR value that the kernel requested before going to stop */
+			u64 requested_psscr;
+			/* Flag to request this thread not to stop */
+			atomic_t dont_stop;
+#endif
+		};
+	};
 #endif
 
 #ifdef CONFIG_PPC_BOOK3S_64
arch/powerpc/include/asm/processor.h +6 -3
index 52fadded5c1e..3e00d959f628 100644
@@ -506,14 +506,17 @@ static inline unsigned long get_clean_sp(unsigned long sp, int is_32)
 }
 #endif
 
+/* asm stubs */
+extern unsigned long isa3_idle_stop_noloss(unsigned long psscr_val);
+extern unsigned long isa3_idle_stop_mayloss(unsigned long psscr_val);
+extern unsigned long isa206_idle_insn_mayloss(unsigned long type);
+
 extern unsigned long cpuidle_disable;
 enum idle_boot_override {IDLE_NO_OVERRIDE = 0, IDLE_POWERSAVE_OFF};
 
 extern int powersave_nap;	/* set if nap mode can be used in idle loop */
-extern unsigned long power7_idle_insn(unsigned long type); /* PNV_THREAD_NAP/etc*/
+
 extern void power7_idle_type(unsigned long type);
-extern unsigned long power9_idle_stop(unsigned long psscr_val);
-extern unsigned long power9_offline_stop(unsigned long psscr_val);
 extern void power9_idle_type(unsigned long stop_psscr_val,
 			      unsigned long stop_psscr_mask);
 
arch/powerpc/include/asm/reg.h +3 -4
index 486b7c83b8c5..6dd294b1b216 100644
@@ -752,10 +752,9 @@
 #define	  SRR1_WAKERESET	0x00100000 /* System reset */
 #define   SRR1_WAKEHDBELL	0x000c0000 /* Hypervisor doorbell on P8 */
 #define	  SRR1_WAKESTATE	0x00030000 /* Powersave exit mask [46:47] */
-#define	  SRR1_WS_DEEPEST	0x00030000 /* Some resources not maintained,
-					  * may not be recoverable */
-#define	  SRR1_WS_DEEPER	0x00020000 /* Some resources not maintained */
-#define	  SRR1_WS_DEEP		0x00010000 /* All resources maintained */
+#define	  SRR1_WS_HVLOSS	0x00030000 /* HV resources not maintained */
+#define	  SRR1_WS_GPRLOSS	0x00020000 /* GPRs not maintained */
+#define	  SRR1_WS_NOLOSS	0x00010000 /* All resources maintained */
 #define   SRR1_PROGTM		0x00200000 /* TM Bad Thing */
 #define   SRR1_PROGFPE		0x00100000 /* Floating Point Enabled */
 #define   SRR1_PROGILL		0x00080000 /* Illegal instruction */
arch/powerpc/kernel/asm-offsets.c +0 -18
index 89cf15566c4e..7834256585f1 100644
@@ -256,7 +256,6 @@ int main(void)
 	OFFSET(ACCOUNT_USER_TIME, paca_struct, accounting.utime);
 	OFFSET(ACCOUNT_SYSTEM_TIME, paca_struct, accounting.stime);
 	OFFSET(PACA_TRAP_SAVE, paca_struct, trap_save);
-	OFFSET(PACA_NAPSTATELOST, paca_struct, nap_state_lost);
 	OFFSET(PACA_SPRG_VDSO, paca_struct, sprg_vdso);
 #else /* CONFIG_PPC64 */
 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
@@ -761,23 +760,6 @@ int main(void)
 	OFFSET(VCPU_TIMING_LAST_ENTER_TBL, kvm_vcpu, arch.timing_last_enter.tv32.tbl);
 #endif
 
-#ifdef CONFIG_PPC_POWERNV
-	OFFSET(PACA_CORE_IDLE_STATE_PTR, paca_struct, core_idle_state_ptr);
-	OFFSET(PACA_THREAD_IDLE_STATE, paca_struct, thread_idle_state);
-	OFFSET(PACA_THREAD_MASK, paca_struct, thread_mask);
-	OFFSET(PACA_SUBCORE_SIBLING_MASK, paca_struct, subcore_sibling_mask);
-	OFFSET(PACA_REQ_PSSCR, paca_struct, requested_psscr);
-	OFFSET(PACA_DONT_STOP, paca_struct, dont_stop);
-#define STOP_SPR(x, f)	OFFSET(x, paca_struct, stop_sprs.f)
-	STOP_SPR(STOP_PID, pid);
-	STOP_SPR(STOP_LDBAR, ldbar);
-	STOP_SPR(STOP_FSCR, fscr);
-	STOP_SPR(STOP_HFSCR, hfscr);
-	STOP_SPR(STOP_MMCR1, mmcr1);
-	STOP_SPR(STOP_MMCR2, mmcr2);
-	STOP_SPR(STOP_MMCRA, mmcra);
-#endif
-
 	DEFINE(PPC_DBELL_SERVER, PPC_DBELL_SERVER);
 	DEFINE(PPC_DBELL_MSGTYPE, PPC_DBELL_MSGTYPE);
 
arch/powerpc/kernel/dt_cpu_ftrs.c +1 -20
index f432054234a4..d635d78facdc 100644
@@ -71,7 +71,6 @@ static int hv_mode;
 
 static struct {
 	u64	lpcr;
-	u64	lpcr_clear;
 	u64	hfscr;
 	u64	fscr;
 } system_registers;
@@ -80,24 +79,7 @@ static void (*init_pmu_registers)(void);
 
 static void __restore_cpu_cpufeatures(void)
 {
-	u64 lpcr;
-
-	/*
-	 * LPCR is restored by the power on engine already. It can be changed
-	 * after early init e.g., by radix enable, and we have no unified API
-	 * for saving and restoring such SPRs.
-	 *
-	 * This ->restore hook should really be removed from idle and register
-	 * restore moved directly into the idle restore code, because this code
-	 * doesn't know how idle is implemented or what it needs restored here.
-	 *
-	 * The best we can do to accommodate secondary boot and idle restore
-	 * for now is "or" LPCR with existing.
-	 */
-	lpcr = mfspr(SPRN_LPCR);
-	lpcr |= system_registers.lpcr;
-	lpcr &= ~system_registers.lpcr_clear;
-	mtspr(SPRN_LPCR, lpcr);
+	mtspr(SPRN_LPCR, system_registers.lpcr);
 	if (hv_mode) {
 		mtspr(SPRN_LPID, 0);
 		mtspr(SPRN_HFSCR, system_registers.hfscr);
@@ -318,7 +300,6 @@ static int __init feat_enable_mmu_hash_v3(struct dt_cpu_feature *f)
 {
 	u64 lpcr;
 
-	system_registers.lpcr_clear |= (LPCR_ISL | LPCR_UPRT | LPCR_HR);
 	lpcr = mfspr(SPRN_LPCR);
 	lpcr &= ~(LPCR_ISL | LPCR_UPRT | LPCR_HR);
 	mtspr(SPRN_LPCR, lpcr);
arch/powerpc/kernel/exceptions-64s.S +9 -8
index 7a672dafd94f..fe1d9b091ac7 100644
@@ -136,8 +136,9 @@ TRAMP_KVM(PACA_EXNMI, 0x100)
 
 #ifdef CONFIG_PPC_P7_NAP
 EXC_COMMON_BEGIN(system_reset_idle_common)
-	mfspr	r12,SPRN_SRR1
-	b	pnv_powersave_wakeup
+	mfspr	r3,SPRN_SRR1
+	bltlr	cr3	/* no state loss, return to idle caller */
+	b	idle_return_gpr_loss
 #endif
 
 /*
@@ -416,17 +417,17 @@ EXC_COMMON_BEGIN(machine_check_idle_common)
 	 * Then decrement MCE nesting after finishing with the stack.
 	 */
 	ld	r3,_MSR(r1)
+	ld	r4,_LINK(r1)
 
 	lhz	r11,PACA_IN_MCE(r13)
 	subi	r11,r11,1
 	sth	r11,PACA_IN_MCE(r13)
 
-	/* Turn off the RI bit because SRR1 is used by idle wakeup code. */
-	/* Recoverability could be improved by reducing the use of SRR1. */
-	li	r11,0
-	mtmsrd	r11,1
-
-	b	pnv_powersave_wakeup_mce
+	mtlr	r4
+	rlwinm	r10,r3,47-31,30,31
+	cmpwi	cr3,r10,2
+	bltlr	cr3	/* no state loss, return to idle caller */
+	b	idle_return_gpr_loss
 #endif
 	/*
 	 * Handle machine check early in real mode. We come here with
arch/powerpc/kernel/idle_book3s.S +88 -910

1,037 lines not on this page · raw message

arch/powerpc/kernel/setup-common.c +2 -2
index 40b44bb53a4e..e089da156ef3 100644
@@ -401,8 +401,8 @@ void __init check_for_initrd(void)
 
 #ifdef CONFIG_SMP
 
-int threads_per_core, threads_per_subcore, threads_shift;
-cpumask_t threads_core_mask;
+int threads_per_core, threads_per_subcore, threads_shift __read_mostly;
+cpumask_t threads_core_mask __read_mostly;
 EXPORT_SYMBOL_GPL(threads_per_core);
 EXPORT_SYMBOL_GPL(threads_per_subcore);
 EXPORT_SYMBOL_GPL(threads_shift);
arch/powerpc/kvm/book3s_hv_rmhandlers.S +52 -38

141 lines not on this page · raw message

arch/powerpc/platforms/powernv/idle.c +660 -154

974 lines not on this page · raw message

arch/powerpc/platforms/powernv/subcore.c +1 -1
index 45563004feda..1d7a9fd30dd1 100644
@@ -183,7 +183,7 @@ static void unsplit_core(void)
 	cpu = smp_processor_id();
 	if (cpu_thread_in_core(cpu) != 0) {
 		while (mfspr(SPRN_HID0) & mask)
-			power7_idle_insn(PNV_THREAD_NAP);
+			power7_idle_type(PNV_THREAD_NAP);
 
 		per_cpu(split_state, cpu).step = SYNC_STEP_UNSPLIT;
 		return;
arch/powerpc/xmon/xmon.c +11 -14
index c7324e8469ee..b8167f4d53ef 100644
@@ -2417,7 +2417,6 @@ static void dump_one_paca(int cpu)
 	DUMP(p, irq_happened, "%#-*x");
 	DUMP(p, io_sync, "%#-*x");
 	DUMP(p, irq_work_pending, "%#-*x");
-	DUMP(p, nap_state_lost, "%#-*x");
 	DUMP(p, sprg_vdso, "%#-*llx");
 
 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
@@ -2425,19 +2424,17 @@ static void dump_one_paca(int cpu)
 #endif
 
 #ifdef CONFIG_PPC_POWERNV
-	DUMP(p, core_idle_state_ptr, "%-*px");
-	DUMP(p, thread_idle_state, "%#-*x");
-	DUMP(p, thread_mask, "%#-*x");
-	DUMP(p, subcore_sibling_mask, "%#-*x");
-	DUMP(p, requested_psscr, "%#-*llx");
-	DUMP(p, stop_sprs.pid, "%#-*llx");
-	DUMP(p, stop_sprs.ldbar, "%#-*llx");
-	DUMP(p, stop_sprs.fscr, "%#-*llx");
-	DUMP(p, stop_sprs.hfscr, "%#-*llx");
-	DUMP(p, stop_sprs.mmcr1, "%#-*llx");
-	DUMP(p, stop_sprs.mmcr2, "%#-*llx");
-	DUMP(p, stop_sprs.mmcra, "%#-*llx");
-	DUMP(p, dont_stop.counter, "%#-*x");
+	DUMP(p, idle_state, "%#-*lx");
+	if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
+		DUMP(p, thread_idle_state, "%#-*x");
+		DUMP(p, thread_mask, "%#-*x");
+		DUMP(p, subcore_sibling_mask, "%#-*x");
+	} else {
+#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
+		DUMP(p, requested_psscr, "%#-*llx");
+		DUMP(p, dont_stop.counter, "%#-*x");
+#endif
+	}
 #endif
 
 	DUMP(p, accounting.utime, "%#-*lx");
Signature · 1 line
2.17.0
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] powerpc/64s: reimplement book3s idle code in C

Hello Nicholas,

Quoted 25 lines · Nicholas Piggin

Reimplement Book3S idle code in C, moving POWER7/8/9 implementation speific HV idle code to the powernv platform code. Generic book3s assembly stubs are kept in common code and used only to save and restore the stack frame and non-volatile GPRs just before going to idle and just after waking up, which can return into C code.

Moving the HMI, SPR, OPAL, locking, etc. to C is the only real way this stuff will cope with non-trivial new CPU implementation details, firmware changes, etc., without becoming unmaintainable.

This is not a strict translation to C code, there are some differences.

- Idle wakeup no longer uses the ->cpu_restore call to reinit SPRs, but saves and restores them all explicitly.

- The optimisation where EC=ESL=0 idle modes did not have to save GPRs or change MSR is restored, because it's now simple to do. State loss modes that did not actually lose GPRs can use this optimization too.

- KVM secondary entry code is now more of a call/return style rather than spaghetti. nap_state_lost is not required beccause KVM always returns via NVGPR restorig path.

Quoted 7 lines

This seems pretty solid, so needs more review and testing now. The KVM changes are pretty significant and complicated. POWER7 needs to be tested too.

Open question:
- Why does P9 restore some of the PMU SPRs (but not others), and P8 only zeroes them?

We are restoring MMCR0 (from the value saved in the stack) and MMCR1, MMCR2, and MMCRA in the stop_sprs in PACA. We saw that MMCRH and MMCRC are cleared on both POWER8 and POWER9. Hence we didn't restore them. MMCRS is being initialized by the KVM code.

Is there anything apart from these that need to be restored ?

Quoted 17 lines

Since RFC v1:
- Now tested and working with POWER9 hash and radix.
- KVM support added. This took a bit of work to untangle and might still have some issues, but POWER9 seems to work including hash on radix with dependent threads mode.
- This snowballed a bit because of KVM and other details making it not feasible to leave POWER7/8 code alone. That's only half done at the moment.
- So far this trades about 800 lines of asm for 500 of C. With POWER7/8 support done it might be another hundred or so lines of C.

Since RFC v2:
- Fixed deep state SLB reloading
- Now tested and working with POWER8.
- Accounted for most feedback.

Quoted 8 lines

Since RFC v3:
- Rebased to powerpc merge + idle state bugfix
- Split SLB flush/restore code out and shared with MCE code (pseries MCE patches can also use).
- More testing on POWER8 including KVM with secondaries.
- Performance testing looks good. EC=ESL=0 is about 5% faster, other stop states look a little faster too.
- Adjusted SPR saving to handler POWER7, haven't tested it.

This patch looks good to me.

A couple of comments below.

Quoted 1 line

---

[... snip ..]

Quoted 15 lines
arch/powerpc/include/asm/paca.h +0 -11
@@ -178,23 +177,30 @@ struct paca_struct {
 #endif
 
 #ifdef CONFIG_PPC_POWERNV
-	/* Per-core mask tracking idle threads and a lock bit-[L][TTTTTTTT] */
-	u32 *core_idle_state_ptr;
-	u8 thread_idle_state;		/* PNV_THREAD_RUNNING/NAP/SLEEP	*/
-	/* Mask to indicate thread id in core */
-	u8 thread_mask;
-	/* Mask to denote subcore sibling threads */
-	u8 subcore_sibling_mask;
-	/* Flag to request this thread not to stop */
-	atomic_t dont_stop;
-	/* The PSSCR value that the kernel requested before going to stop */
-	u64 requested_psscr;

arch/powerpc/include/asm/paca.h struct paca_struct { · line 189

Quoted 10 lines
arch/powerpc/include/asm/paca.h +10 -0
+	/* PowerNV idle fields */
+	/* PNV_CORE_IDLE_* bits, all siblings work on thread 0 paca */
+	unsigned long idle_state;
+	union {
+		/* P7/P8 specific fields */
+		struct {
+			/* PNV_THREAD_RUNNING/NAP/SLEEP	*/
+			u8 thread_idle_state;
+			/* Mask to indicate thread id in core */
+			u8 thread_mask;

This is no longer needed. We can get this from cpu_thread_in_core() from the C code.

The only place where we are currently using this is to DUMP the value of the thread_mask from xmon but not anywhere else in the idle entry code.

Quoted 20 lines
arch/powerpc/include/asm/paca.h +13 -5
+			/* Mask to denote subcore sibling threads */
+			u8 subcore_sibling_mask;
+		};
 
-	/*
-	 * Save area for additional SPRs that need to be
-	 * saved/restored during cpuidle stop.
-	 */
-	struct stop_sprs stop_sprs;
+		/* P9 specific fields */
+		struct {
+#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
+			/* The PSSCR value that the kernel requested before going to stop */
+			u64 requested_psscr;
+			/* Flag to request this thread not to stop */
+			atomic_t dont_stop;
+#endif
+		};
+	};
 #endif

[..snip..]

arch/powerpc/kernel/exceptions-64s.S TRAMP_KVM(PACA_EXNMI, 0x100) · line 140

Quoted 10 lines
arch/powerpc/kernel/exceptions-64s.S +2 -2
@@ -136,8 +136,9 @@ TRAMP_KVM(PACA_EXNMI, 0x100)
 
 #ifdef CONFIG_PPC_P7_NAP
 EXC_COMMON_BEGIN(system_reset_idle_common)
-	mfspr	r12,SPRN_SRR1
-	b	pnv_powersave_wakeup
+	mfspr	r3,SPRN_SRR1
+	bltlr	cr3	/* no state loss, return to idle caller */

So, if we are in an ESL=EC=0 stop, and we get an xscom SRESET, I guess the expected value in SPRN_SRR1[46:47] will be SRR1_WS_NOLOSS.

In this case, though the LR would correspond to the caller of isa3_idle_stop_noloss(), r3 would have SPRN_SRR1 as opposed to 0 which is what we would have returned from isa3_idle_stop_noloss().

Do we use the value to service the NMI ?

Quoted 13 lines
arch/powerpc/kernel/exceptions-64s.S +2 -0
+	b	idle_return_gpr_loss
 #endif
 
 /*
@@ -416,17 +417,17 @@ EXC_COMMON_BEGIN(machine_check_idle_common)
 	 * Then decrement MCE nesting after finishing with the stack.
 	 */
 	ld	r3,_MSR(r1)
+	ld	r4,_LINK(r1)
 
 	lhz	r11,PACA_IN_MCE(r13)
 	subi	r11,r11,1
 	sth	r11,PACA_IN_MCE(r13)

arch/powerpc/kernel/exceptions-64s.S EXC_COMMON_BEGIN(machine_check_idle_common) · line 428

Quoted 10 lines
arch/powerpc/kernel/exceptions-64s.S +3 -6
 
-	/* Turn off the RI bit because SRR1 is used by idle wakeup code. */
-	/* Recoverability could be improved by reducing the use of SRR1. */
-	li	r11,0
-	mtmsrd	r11,1
-
-	b	pnv_powersave_wakeup_mce
+	mtlr	r4
+	rlwinm	r10,r3,47-31,30,31
+	cmpwi	cr3,r10,2

Can we use SRR1_WS_GPRLOSS here as well as in IDLETEST() ?

Quoted 2 lines
arch/powerpc/kernel/exceptions-64s.S +2 -0
+	bltlr	cr3	/* no state loss, return to idle caller */
+	b	idle_return_gpr_loss

[..snip..]

arch/powerpc/kernel/exceptions-64s.S

Quoted 13 lines
arch/powerpc/kernel/exceptions-64s.S +13 -0
+_GLOBAL(isa206_idle_insn_mayloss)
+	std	r1,PACAR1(r13)
+	mflr	r4
+	mfcr	r5
+	/* use stack red zone rather than a new frame */
+	addi	r6,r1,-INT_FRAME_SIZE
+	SAVE_GPR(2, r6)
+	SAVE_NVGPRS(r6)
+	std	r4,_LINK(r6)
+	std	r5,_CCR(r6)
+	cmpwi	r3,PNV_THREAD_NAP
+	bne	1f
+	IDLE_STATE_ENTER_SEQ_NORET(PPC_NAP)

We can have the following here, since we don't expect to return from nap.

        b	. /* catch bugs */"
Quoted 1 line

+1: cmpwi r3,PNV_THREAD_SLEEP

[..snip..]

Quoted 55 lines
arch/powerpc/kernel/exceptions-64s.S +55 -0
+static unsigned long power7_idle_insn(unsigned long type)
+{
+	int cpu = raw_smp_processor_id();
+	int first = cpu_first_thread_sibling(cpu);
+	unsigned long thread = 1UL << cpu_thread_in_core(cpu);
+	unsigned long *state = &paca_ptrs[first]->idle_state;
+	unsigned long srr1;
+	bool full_winkle;
+	struct p7_sprs sprs;
+	bool sprs_saved = false;
+	int rc;
+
+	memset(&sprs, 0, sizeof(sprs));
+
+	if (unlikely(type != PNV_THREAD_NAP)) {
+		atomic_lock_thread_idle();
+
+		BUG_ON(!(*state & thread));
+		*state &= ~thread;
+
+		if (power7_fastsleep_workaround_entry) {
+			if ((*state & ((1 << threads_per_core) - 1)) == 0) {
+				rc = opal_config_cpu_idle_state(
+						OPAL_CONFIG_IDLE_FASTSLEEP,
+						OPAL_CONFIG_IDLE_APPLY);
+				BUG_ON(rc);
+			}
+		}
+
+		if (type == PNV_THREAD_WINKLE) {
+			sprs.tscr	= mfspr(SPRN_TSCR);
+			sprs.worc	= mfspr(SPRN_WORC);
+
+			sprs.sdr1	= mfspr(SPRN_SDR1);
+			sprs.rpr	= mfspr(SPRN_RPR);
+			sprs.amor	= mfspr(SPRN_AMOR);
+
+			sprs.lpcr	= mfspr(SPRN_LPCR);
+			if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
+				sprs.hfscr	= mfspr(SPRN_HFSCR);
+				sprs.fscr	= mfspr(SPRN_FSCR);
+			}
+			sprs.purr	= mfspr(SPRN_PURR);
+			sprs.spurr	= mfspr(SPRN_SPURR);
+			sprs.dscr	= mfspr(SPRN_DSCR);
+			sprs.wort	= mfspr(SPRN_WORT);
+
+			sprs_saved = true;
+
+			/*
+			 * Increment winkle counter and set all winkle bits if
+			 * all threads are winkling. This allows wakeup side to
+			 * distinguish between fast sleep and winkle state
+			 * loss. Fast sleep still has to resync the timebase so
+			 * this may not be a really big win.

arch/powerpc/kernel/exceptions-64s.S

Quoted 10 lines
arch/powerpc/kernel/exceptions-64s.S +10 -0
+			 */
+			*state += 1 << PNV_CORE_IDLE_WINKLE_COUNT_SHIFT;
+			if ((*state & PNV_CORE_IDLE_WINKLE_COUNT_BITS) >> PNV_CORE_IDLE_WINKLE_COUNT_SHIFT == threads_per_core)
+				*state |= PNV_CORE_IDLE_THREAD_WINKLE_BITS;
+			WARN_ON((*state & PNV_CORE_IDLE_WINKLE_COUNT_BITS) == 0);
+		}
+
+		atomic_unlock_thread_idle();
+	}
+

Could we add the following for debug purposes. We are already dumping thread_idle_state in xmon.

      	paca->thread_idle_state = type; 

arch/powerpc/kernel/exceptions-64s.S

Quoted 1 line
arch/powerpc/kernel/exceptions-64s.S +1 -0
+	srr1 = isa206_idle_insn_mayloss(type);

And the following:

      paca->thread_idle_state = PNV_THREAD_RUNNING;

Apart from debugging purpose, IIRC, on some versions of POWER8, SRR1[46:47] wasn't being updated to 0b00 when we get an xscom SRESET while in running state. Thus, the only way to distinguish whether we entered 0x100 is due to a nap/fastsleep/winkle wakeup or due to an NMI caused when the processor wasn't in idle state was paca->thread_idle_state.

Quoted 5 lines
arch/powerpc/kernel/exceptions-64s.S +5 -0
+
+	WARN_ON_ONCE(!srr1);
+	WARN_ON_ONCE(mfmsr() & (MSR_IR|MSR_DR));
+
+	if (unlikely((srr1 & SRR1_WAKEMASK_P8) == SRR1_WAKEHMI))

[..snip..]

Quoted 13 lines
arch/powerpc/kernel/exceptions-64s.S +13 -0
+static unsigned long power9_idle_stop(unsigned long psscr, bool mmu_on)
+{
+	int cpu = raw_smp_processor_id();
+	int first = cpu_first_thread_sibling(cpu);
+	unsigned long *state = &paca_ptrs[first]->idle_state;
+	unsigned long srr1;
+	unsigned long mmcr0 = 0;
+	struct p9_sprs sprs;
+	bool sprs_saved = false;
+
+	memset(&sprs, 0, sizeof(sprs));
+
+	if (!(psscr & (PSSCR_EC|PSSCR_ESL))) {

arch/powerpc/kernel/exceptions-64s.S

Quoted 10 lines
arch/powerpc/kernel/exceptions-64s.S +10 -0
+		BUG_ON(!mmu_on);
+
+		/*
+		 * Wake synchronously. SRESET via xscom may still cause
+		 * a 0x100 powersave wakeup with SRR1 reason!
+		 */
+		srr1 = isa3_idle_stop_noloss(psscr);
+		if (likely(!srr1))
+			return 0;
+

We come here if if we were woken up from a ESL=0 stop by a xscom SRESET. Where would the SRESET be handled ?

arch/powerpc/kernel/exceptions-64s.S

Quoted 9 lines
arch/powerpc/kernel/exceptions-64s.S +9 -0
+		/*
+		 * Registers not saved, can't recover!
+		 * This would be a hardware bug
+		 */
+		BUG_ON((srr1 & SRR1_WAKESTATE) != SRR1_WS_NOLOSS);
+
+		goto out;
+	}
+

The patch looks good otherwise, especially the idle_book3s.S :-)

--
Thanks and Regards
gautham.

author reviewer
#3 lore raw

Re: [PATCH 2/2] powerpc/64s: reimplement book3s idle code in C

Quoted 35 lines · On Thu, 9 Aug 2018 20:15:04 +0530 Gautham R Shenoy <[email protected]>…

Hello Nicholas,

Quoted 25 lines · Nicholas Piggin

Reimplement Book3S idle code in C, moving POWER7/8/9 implementation speific HV idle code to the powernv platform code. Generic book3s assembly stubs are kept in common code and used only to save and restore the stack frame and non-volatile GPRs just before going to idle and just after waking up, which can return into C code.

Moving the HMI, SPR, OPAL, locking, etc. to C is the only real way this stuff will cope with non-trivial new CPU implementation details, firmware changes, etc., without becoming unmaintainable.

This is not a strict translation to C code, there are some differences.

- Idle wakeup no longer uses the ->cpu_restore call to reinit SPRs, but saves and restores them all explicitly.

- The optimisation where EC=ESL=0 idle modes did not have to save GPRs or change MSR is restored, because it's now simple to do. State loss modes that did not actually lose GPRs can use this optimization too.

- KVM secondary entry code is now more of a call/return style rather than spaghetti. nap_state_lost is not required beccause KVM always returns via NVGPR restorig path.

Quoted 7 lines

This seems pretty solid, so needs more review and testing now. The KVM changes are pretty significant and complicated. POWER7 needs to be tested too.

Open question:
- Why does P9 restore some of the PMU SPRs (but not others), and P8 only zeroes them?

Quoted 6 lines

We are restoring MMCR0 (from the value saved in the stack) and MMCR1, MMCR2, and MMCRA in the stop_sprs in PACA. We saw that MMCRH and MMCRC are cleared on both POWER8 and POWER9. Hence we didn't restore them. MMCRS is being initialized by the KVM code.

Is there anything apart from these that need to be restored ?

No I'm wondering why it is we restore those on POWER9? POWER8 does not restore them, only zeroes. What is the difference with POWER9?

I will leave that in for now so we don't change too much with one patch, but it would be nice to document a bit better the reasons for saving or clearing SPRs.

Quoted 65 lines
Quoted 18 lines

Since RFC v1:
- Now tested and working with POWER9 hash and radix.
- KVM support added. This took a bit of work to untangle and might still have some issues, but POWER9 seems to work including hash on radix with dependent threads mode.
- This snowballed a bit because of KVM and other details making it not feasible to leave POWER7/8 code alone. That's only half done at the moment.
- So far this trades about 800 lines of asm for 500 of C. With POWER7/8 support done it might be another hundred or so lines of C.

Since RFC v2:
- Fixed deep state SLB reloading
- Now tested and working with POWER8.
- Accounted for most feedback.

Quoted 8 lines

Since RFC v3:
- Rebased to powerpc merge + idle state bugfix
- Split SLB flush/restore code out and shared with MCE code (pseries MCE patches can also use).
- More testing on POWER8 including KVM with secondaries.
- Performance testing looks good. EC=ESL=0 is about 5% faster, other stop states look a little faster too.
- Adjusted SPR saving to handler POWER7, haven't tested it.

This patch looks good to me.

A couple of comments below.

Quoted 1 line

---

[... snip ..]

Quoted 15 lines
arch/powerpc/include/asm/paca.h +0 -11
@@ -178,23 +177,30 @@ struct paca_struct {
 #endif
 
 #ifdef CONFIG_PPC_POWERNV
-	/* Per-core mask tracking idle threads and a lock bit-[L][TTTTTTTT] */
-	u32 *core_idle_state_ptr;
-	u8 thread_idle_state;		/* PNV_THREAD_RUNNING/NAP/SLEEP	*/
-	/* Mask to indicate thread id in core */
-	u8 thread_mask;
-	/* Mask to denote subcore sibling threads */
-	u8 subcore_sibling_mask;
-	/* Flag to request this thread not to stop */
-	atomic_t dont_stop;
-	/* The PSSCR value that the kernel requested before going to stop */
-	u64 requested_psscr;

arch/powerpc/include/asm/paca.h struct paca_struct { · line 189

Quoted 10 lines
arch/powerpc/include/asm/paca.h +10 -0
+	/* PowerNV idle fields */
+	/* PNV_CORE_IDLE_* bits, all siblings work on thread 0 paca */
+	unsigned long idle_state;
+	union {
+		/* P7/P8 specific fields */
+		struct {
+			/* PNV_THREAD_RUNNING/NAP/SLEEP	*/
+			u8 thread_idle_state;
+			/* Mask to indicate thread id in core */
+			u8 thread_mask;  

This is no longer needed. We can get this from cpu_thread_in_core() from the C code.

Quoted 3 lines

The only place where we are currently using this is to DUMP the value of the thread_mask from xmon but not anywhere else in the idle entry code.

Good catch, removed it.

Quoted 26 lines
Quoted 20 lines
arch/powerpc/include/asm/paca.h +13 -5
+			/* Mask to denote subcore sibling threads */
+			u8 subcore_sibling_mask;
+		};
 
-	/*
-	 * Save area for additional SPRs that need to be
-	 * saved/restored during cpuidle stop.
-	 */
-	struct stop_sprs stop_sprs;
+		/* P9 specific fields */
+		struct {
+#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
+			/* The PSSCR value that the kernel requested before going to stop */
+			u64 requested_psscr;
+			/* Flag to request this thread not to stop */
+			atomic_t dont_stop;
+#endif
+		};
+	};
 #endif  

[..snip..]

Quoted 4 lines
arch/powerpc/kernel/exceptions-64s.S +0 -0
@@ -136,8 +136,9 @@ TRAMP_KVM(PACA_EXNMI, 0x100)
 
Quoted 10 lines

arch/powerpc/kernel/exceptions-64s.S TRAMP_KVM(PACA_EXNMI, 0x100) · line 140

Quoted 6 lines
arch/powerpc/kernel/exceptions-64s.S +2 -2
 #ifdef CONFIG_PPC_P7_NAP
 EXC_COMMON_BEGIN(system_reset_idle_common)
-	mfspr	r12,SPRN_SRR1
-	b	pnv_powersave_wakeup
+	mfspr	r3,SPRN_SRR1
+	bltlr	cr3	/* no state loss, return to idle caller */  

So, if we are in an ESLì=0 stop, and we get an xscom SRESET, I guess the expected value in SPRN_SRR1[46:47] will be SRR1_WS_NOLOSS.

Yes it should, because ESL=0.

Quoted 6 lines

In this case, though the LR would correspond to the caller of isa3_idle_stop_noloss(), r3 would have SPRN_SRR1 as opposed to 0 which is what we would have returned from isa3_idle_stop_noloss().

Do we use the value to service the NMI ?

Yes. System reset exception is cleared when the interrupt is delivered so we can't ignore it.

Quoted 15 lines
Quoted 15 lines
arch/powerpc/kernel/exceptions-64s.S +2 -0
+	b	idle_return_gpr_loss
 #endif
 
 /*
@@ -416,17 +417,17 @@ EXC_COMMON_BEGIN(machine_check_idle_common)
 	 * Then decrement MCE nesting after finishing with the stack.
 	 */
 	ld	r3,_MSR(r1)
+	ld	r4,_LINK(r1)
 
 	lhz	r11,PACA_IN_MCE(r13)
 	subi	r11,r11,1
 	sth	r11,PACA_IN_MCE(r13)
 
Quoted 11 lines

arch/powerpc/kernel/exceptions-64s.S EXC_COMMON_BEGIN(machine_check_idle_common) · line 428

Quoted 9 lines
arch/powerpc/kernel/exceptions-64s.S +3 -6
-	/* Turn off the RI bit because SRR1 is used by idle wakeup code. */
-	/* Recoverability could be improved by reducing the use of SRR1. */
-	li	r11,0
-	mtmsrd	r11,1
-
-	b	pnv_powersave_wakeup_mce
+	mtlr	r4
+	rlwinm	r10,r3,47-31,30,31
+	cmpwi	cr3,r10,2  

Can we use SRR1_WS_GPRLOSS here as well as in IDLETEST() ?

You mean

cmpwi cr3,r10,SRR1_WS_GPRLOSS >> (63-47)

Quoted 23 lines
Quoted 3 lines
arch/powerpc/kernel/exceptions-64s.S +2 -0
+	bltlr	cr3	/* no state loss, return to idle caller */
+	b	idle_return_gpr_loss  

[..snip..]

arch/powerpc/kernel/exceptions-64s.S

Quoted 13 lines
arch/powerpc/kernel/exceptions-64s.S +13 -0
+_GLOBAL(isa206_idle_insn_mayloss)
+	std	r1,PACAR1(r13)
+	mflr	r4
+	mfcr	r5
+	/* use stack red zone rather than a new frame */
+	addi	r6,r1,-INT_FRAME_SIZE
+	SAVE_GPR(2, r6)
+	SAVE_NVGPRS(r6)
+	std	r4,_LINK(r6)
+	std	r5,_CCR(r6)
+	cmpwi	r3,PNV_THREAD_NAP
+	bne	1f
+	IDLE_STATE_ENTER_SEQ_NORET(PPC_NAP)  

We can have the following here, since we don't expect to return from nap.

Quoted 3 lines
        b	. /* catch bugs */"
Quoted 1 line

+1: cmpwi r3,PNV_THREAD_SLEEP

Sure, added.

Quoted 71 lines

[..snip..]

Quoted 55 lines
arch/powerpc/kernel/exceptions-64s.S +55 -0
+static unsigned long power7_idle_insn(unsigned long type)
+{
+	int cpu = raw_smp_processor_id();
+	int first = cpu_first_thread_sibling(cpu);
+	unsigned long thread = 1UL << cpu_thread_in_core(cpu);
+	unsigned long *state = &paca_ptrs[first]->idle_state;
+	unsigned long srr1;
+	bool full_winkle;
+	struct p7_sprs sprs;
+	bool sprs_saved = false;
+	int rc;
+
+	memset(&sprs, 0, sizeof(sprs));
+
+	if (unlikely(type != PNV_THREAD_NAP)) {
+		atomic_lock_thread_idle();
+
+		BUG_ON(!(*state & thread));
+		*state &= ~thread;
+
+		if (power7_fastsleep_workaround_entry) {
+			if ((*state & ((1 << threads_per_core) - 1)) = 0) {
+				rc = opal_config_cpu_idle_state(
+						OPAL_CONFIG_IDLE_FASTSLEEP,
+						OPAL_CONFIG_IDLE_APPLY);
+				BUG_ON(rc);
+			}
+		}
+
+		if (type = PNV_THREAD_WINKLE) {
+			sprs.tscr	= mfspr(SPRN_TSCR);
+			sprs.worc	= mfspr(SPRN_WORC);
+
+			sprs.sdr1	= mfspr(SPRN_SDR1);
+			sprs.rpr	= mfspr(SPRN_RPR);
+			sprs.amor	= mfspr(SPRN_AMOR);
+
+			sprs.lpcr	= mfspr(SPRN_LPCR);
+			if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
+				sprs.hfscr	= mfspr(SPRN_HFSCR);
+				sprs.fscr	= mfspr(SPRN_FSCR);
+			}
+			sprs.purr	= mfspr(SPRN_PURR);
+			sprs.spurr	= mfspr(SPRN_SPURR);
+			sprs.dscr	= mfspr(SPRN_DSCR);
+			sprs.wort	= mfspr(SPRN_WORT);
+
+			sprs_saved = true;
+
+			/*
+			 * Increment winkle counter and set all winkle bits if
+			 * all threads are winkling. This allows wakeup side to
+			 * distinguish between fast sleep and winkle state
+			 * loss. Fast sleep still has to resync the timebase so
+			 * this may not be a really big win.

arch/powerpc/kernel/exceptions-64s.S

Quoted 10 lines
arch/powerpc/kernel/exceptions-64s.S +10 -0
+			 */
+			*state += 1 << PNV_CORE_IDLE_WINKLE_COUNT_SHIFT;
+			if ((*state & PNV_CORE_IDLE_WINKLE_COUNT_BITS) >> PNV_CORE_IDLE_WINKLE_COUNT_SHIFT = threads_per_core)
+				*state |= PNV_CORE_IDLE_THREAD_WINKLE_BITS;
+			WARN_ON((*state & PNV_CORE_IDLE_WINKLE_COUNT_BITS) = 0);
+		}
+
+		atomic_unlock_thread_idle();
+	}
+  

Could we add the following for debug purposes. We are already dumping thread_idle_state in xmon.

Quoted 6 lines
      	paca->thread_idle_state = type; 

arch/powerpc/kernel/exceptions-64s.S

Quoted 1 line
arch/powerpc/kernel/exceptions-64s.S +1 -0
+	srr1 = isa206_idle_insn_mayloss(type);  

And the following:

      paca->thread_idle_state = PNV_THREAD_RUNNING;

Yes I'll add that.

Quoted 7 lines

Apart from debugging purpose, IIRC, on some versions of POWER8, SRR1[46:47] wasn't being updated to 0b00 when we get an xscom SRESET while in running state. Thus, the only way to distinguish whether we entered 0x100 is due to a nap/fastsleep/winkle wakeup or due to an NMI caused when the processor wasn't in idle state was paca->thread_idle_state.

We don't actually do anything with that today, do we? Is that one of the reasons we don't use xscom SRESET on POWER8?

Quoted 26 lines
Quoted 6 lines
arch/powerpc/kernel/exceptions-64s.S +5 -0
+
+	WARN_ON_ONCE(!srr1);
+	WARN_ON_ONCE(mfmsr() & (MSR_IR|MSR_DR));
+
+	if (unlikely((srr1 & SRR1_WAKEMASK_P8) = SRR1_WAKEHMI))  

[..snip..]

Quoted 18 lines
arch/powerpc/kernel/exceptions-64s.S +18 -0
+static unsigned long power9_idle_stop(unsigned long psscr, bool mmu_on)
+{
+	int cpu = raw_smp_processor_id();
+	int first = cpu_first_thread_sibling(cpu);
+	unsigned long *state = &paca_ptrs[first]->idle_state;
+	unsigned long srr1;
+	unsigned long mmcr0 = 0;
+	struct p9_sprs sprs;
+	bool sprs_saved = false;
+
+	memset(&sprs, 0, sizeof(sprs));
+
+	if (!(psscr & (PSSCR_EC|PSSCR_ESL))) {
+		BUG_ON(!mmu_on);
+
+		/*
+		 * Wake synchronously. SRESET via xscom may still cause
+		 * a 0x100 powersave wakeup with SRR1 reason!
Quoted 8 lines

arch/powerpc/kernel/exceptions-64s.S

Quoted 5 lines
arch/powerpc/kernel/exceptions-64s.S +5 -0
+		 */
+		srr1 = isa3_idle_stop_noloss(psscr);
+		if (likely(!srr1))
+			return 0;
+  

We come here if if we were woken up from a ESL=0 stop by a xscom SRESET. Where would the SRESET be handled ?

In irq_set_pending_from_srr1(), the same as EC=1 states.

Quoted 14 lines

arch/powerpc/kernel/exceptions-64s.S

Quoted 10 lines
arch/powerpc/kernel/exceptions-64s.S +9 -0
+		/*
+		 * Registers not saved, can't recover!
+		 * This would be a hardware bug
+		 */
+		BUG_ON((srr1 & SRR1_WAKESTATE) != SRR1_WS_NOLOSS);
+
+		goto out;
+	}
+  

The patch looks good otherwise, especially the idle_book3s.S :-)

Quoted 1 line

Thank you for all the reviews

Thanks,
Nick

#4 lore raw
In reply to Nicholas Piggin
Quoted 43 lines · On Fri, 3 Aug 2018 14:13:49 +1000 Nicholas Piggin <[email protected]> wrote:

The machine check code that flushes and restores bolted segments in real mode belongs in mm/slb.c. This will be used by pseries machine check and idle code.

---
 arch/powerpc/include/asm/book3s/64/mmu-hash.h |  3 ++
 arch/powerpc/kernel/mce_power.c               | 21 ++--------
 arch/powerpc/mm/slb.c                         | 38

+++++++++++++++++++ 3 files changed, 44 insertions(+), 18 deletions(-)

(quoted diff) +0 -0

b/arch/powerpc/include/asm/book3s/64/mmu-hash.h index 2f74bdc805e0..d4e398185b3a 100644 --- a/arch/powerpc/include/asm/book3s/64/mmu-hash.h +++ b/arch/powerpc/include/asm/book3s/64/mmu-hash.h @@ -497,6 +497,9 @@ extern void hpte_init_native(void);
extern void slb_initialize(void);
extern void slb_flush_and_rebolt(void);

arch/powerpc/include/asm/book3s/64/mmu-hash.h +3 -0
+extern void slb_flush_all_realmode(void);
+extern void __slb_restore_bolted_realmode(void);
+extern void slb_restore_bolted_realmode(void);
 
 extern void slb_vmalloc_update(void);
 extern void slb_set_size(u16 size);
(quoted diff) +0 -0

b/arch/powerpc/kernel/mce_power.c index d6756af6ec78..50f7b9817246 100644 --- a/arch/powerpc/kernel/mce_power.c
+++ b/arch/powerpc/kernel/mce_power.c

arch/powerpc/kernel/mce_power.c +1 -4
@@ -62,11 +62,8 @@ static unsigned long addr_to_pfn(struct pt_regs
 *regs, unsigned long addr) #ifdef CONFIG_PPC_BOOK3S_64
 static void flush_and_reload_slb(void)
 {
-	struct slb_shadow *slb;
-	unsigned long i, n;
-
 	/* Invalidate all SLBs */
-	asm volatile("slbmte %0,%0; slbia" : : "r" (0));
+	slb_flush_all_realmode();
 
 #ifdef CONFIG_KVM_BOOK3S_HANDLER
 	/*

arch/powerpc/kernel/mce_power.c static void flush_and_reload_slb(void) · line 78

Quoted 10 lines
arch/powerpc/kernel/mce_power.c +1 -4
@@ -76,22 +73,10 @@ static void flush_and_reload_slb(void)
 	if (get_paca()->kvm_hstate.in_guest)
 		return;
 #endif
-
-	/* For host kernel, reload the SLBs from shadow SLB buffer.
 */
-	slb = get_slb_shadow();
-	if (!slb)
+	if (early_radix_enabled())
 		return;

And we lose the check that the shadow slb exists. Is !slb equivalent to early_radix_enabled()

Quoted 19 lines
arch/powerpc/kernel/mce_power.c +1 -9
-	n = min_t(u32, be32_to_cpu(slb->persistent), SLB_MIN_SIZE);
-
-	/* Load up the SLB entries from shadow SLB */
-	for (i = 0; i < n; i++) {
-		unsigned long rb > be64_to_cpu(slb->save_area[i].esid);
-		unsigned long rs > be64_to_cpu(slb->save_area[i].vsid); -
-		rb = (rb & ~0xFFFul) | i;
-		asm volatile("slbmte %0,%1" : : "r" (rs), "r" (rb));
-	}
+	slb_restore_bolted_realmode();
 }
 #endif
 
arch/powerpc/mm/slb.c +0 -0
index cb796724a6fc..136db8652577 100644

arch/powerpc/mm/slb.c static inline void create_shadowed_slbe(unsigned · line 99

Quoted 10 lines
arch/powerpc/mm/slb.c +7 -0
@@ -90,6 +90,44 @@ static inline void create_shadowed_slbe(unsigned
 long ea, int ssize, : "memory" );
 }
 
+/*
+ * Insert bolted entries into SLB (which may not be empty).
+ */
+void __slb_restore_bolted_realmode(void)
+{
+	struct slb_shadow *p = get_slb_shadow();
+	enum slb_index index;

or can we get here at some point when shadow slb is not populated?

Thanks

Michal

Quoted 35 lines
arch/powerpc/mm/slb.c +27 -0
+
+	 /* No isync needed because realmode. */
+	for (index = 0; index < SLB_NUM_BOLTED; index++) {
+		asm volatile("slbmte  %0,%1" :
+		     : "r" (be64_to_cpu(p->save_area[index].vsid)),
+		       "r" (be64_to_cpu(p->save_area[index].esid)));
+	}
+}
+
+/*
+ * Insert bolted entries into an empty SLB.
+ * This is not the same as rebolt because the bolted segments
+ * (e.g., kstack) are not changed (rebolted).
+ */
+void slb_restore_bolted_realmode(void)
+{
+	__slb_restore_bolted_realmode();
+	get_paca()->slb_cache_ptr = 0;
+}
+
+/*
+ * This flushes all SLB entries including 0, so it must be realmode.
+ */
+void slb_flush_all_realmode(void)
+{
+	/*
+	 * This flushes all SLB entries including 0, so it must be

realmode.

arch/powerpc/mm/slb.c +4 -0
+	 */
+	asm volatile("slbmte %0,%0; slbia" : : "r" (0));
+}
+
 static void __slb_flush_and_rebolt(void)
 {
 	/* If you change this make sure you change SLB_NUM_BOLTED
author reviewer
#5 lore raw
Quoted 43 lines · On Wed, 8 Aug 2018 22:22:52 +0200 Michal Suchánek <[email protected]> wrote:
Quoted 40 lines · On Fri, 3 Aug 2018 14:13:49 +1000 Nicholas Piggin <[email protected]> wrote:

The machine check code that flushes and restores bolted segments in real mode belongs in mm/slb.c. This will be used by pseries machine check and idle code.

---
 arch/powerpc/include/asm/book3s/64/mmu-hash.h |  3 ++
 arch/powerpc/kernel/mce_power.c               | 21 ++--------
 arch/powerpc/mm/slb.c                         | 38

+++++++++++++++++++ 3 files changed, 44 insertions(+), 18 deletions(-)

(quoted diff) +0 -0

b/arch/powerpc/include/asm/book3s/64/mmu-hash.h index 2f74bdc805e0..d4e398185b3a 100644 --- a/arch/powerpc/include/asm/book3s/64/mmu-hash.h +++ b/arch/powerpc/include/asm/book3s/64/mmu-hash.h @@ -497,6 +497,9 @@ extern void hpte_init_native(void);
extern void slb_initialize(void);
extern void slb_flush_and_rebolt(void);

arch/powerpc/include/asm/book3s/64/mmu-hash.h +3 -0
+extern void slb_flush_all_realmode(void);
+extern void __slb_restore_bolted_realmode(void);
+extern void slb_restore_bolted_realmode(void);
 
 extern void slb_vmalloc_update(void);
 extern void slb_set_size(u16 size);
(quoted diff) +0 -0

b/arch/powerpc/kernel/mce_power.c index d6756af6ec78..50f7b9817246 100644 --- a/arch/powerpc/kernel/mce_power.c
+++ b/arch/powerpc/kernel/mce_power.c

arch/powerpc/kernel/mce_power.c +1 -4
@@ -62,11 +62,8 @@ static unsigned long addr_to_pfn(struct pt_regs
 *regs, unsigned long addr) #ifdef CONFIG_PPC_BOOK3S_64
 static void flush_and_reload_slb(void)
 {
-	struct slb_shadow *slb;
-	unsigned long i, n;
-
 	/* Invalidate all SLBs */
-	asm volatile("slbmte %0,%0; slbia" : : "r" (0));
+	slb_flush_all_realmode();
 
Quoted 16 lines

arch/powerpc/kernel/mce_power.c static void flush_and_reload_slb(void) · line 78

Quoted 13 lines
arch/powerpc/kernel/mce_power.c +1 -4
 #ifdef CONFIG_KVM_BOOK3S_HANDLER
 	/*
@@ -76,22 +73,10 @@ static void flush_and_reload_slb(void)
 	if (get_paca()->kvm_hstate.in_guest)
 		return;
 #endif
-
-	/* For host kernel, reload the SLBs from shadow SLB buffer.
 */
-	slb = get_slb_shadow();
-	if (!slb)
+	if (early_radix_enabled())
 		return;  

And we lose the check that the shadow slb exists. Is !slb equivalent to early_radix_enabled()

Yeah pretty close to.

Quoted 25 lines
Quoted 25 lines
arch/powerpc/kernel/mce_power.c +0 -6
-	n = min_t(u32, be32_to_cpu(slb->persistent), SLB_MIN_SIZE);
-
-	/* Load up the SLB entries from shadow SLB */
-	for (i = 0; i < n; i++) {
-		unsigned long rb =
 be64_to_cpu(slb->save_area[i].esid);
-		unsigned long rs =

be64_to_cpu(slb->save_area[i].vsid); -

arch/powerpc/kernel/mce_power.c +1 -3
-		rb = (rb & ~0xFFFul) | i;
-		asm volatile("slbmte %0,%1" : : "r" (rs), "r" (rb));
-	}
+	slb_restore_bolted_realmode();
 }
 #endif
arch/powerpc/mm/slb.c +0 -0
index cb796724a6fc..136db8652577 100644
@@ -90,6 +90,44 @@ static inline void create_shadowed_slbe(unsigned
 long ea, int ssize, : "memory" );
 }
 
Quoted 9 lines

arch/powerpc/mm/slb.c static inline void create_shadowed_slbe(unsigned long ea, int ssize, · line 99

Quoted 7 lines
arch/powerpc/mm/slb.c +7 -0
+/*
+ * Insert bolted entries into SLB (which may not be empty).
+ */
+void __slb_restore_bolted_realmode(void)
+{
+	struct slb_shadow *p = get_slb_shadow();
+	enum slb_index index;  

or can we get here at some point when shadow slb is not populated?

We shouldn't because we won't turn the MMU on so we shouldn't get SLB MCEs... But I don't think that's guaranteed anywhere, so yeah wouldn't hurt to add that check back in.

I'll send out another revision.

Thanks,
Nick
--
To unsubscribe from this list: send the line "unsubscribe kvm-ppc" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html