Skip to contentSkip to conversation

ppc, kvm, cpuidle: Allow offline and kvm standby threads to enter fastsleep

PATCH In review Preeti U Murthy 2 participants 8 messages May 28, 2014 – Jun 5, 2014

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover ppc, kvm, cpuidle: Allow offline and kvm standby threads to enter fastsleep

Fast sleep is a deep idle state on Power8. The support for the state was added in commit 0d94873011. Today the idle threads in the host can potentially be put to fast sleep. But when we launch guests using kvm, the secondary threads are required to be offline and the offline threads are put to nap. Besides this case, when secondary threads are woken up to run guests and eventually go idle or when the guest is killed, they enter nap. So when the entire core goes idle in both the above scenarios, the maximum power savings that we can obtain is as much as we can get from napping the cpus. This patchset adds support in the above two cases for the threads to enter fast sleep.
---

Srivatsa S. Bhat (6):
powernv, cpuidle: Move the flags used for idle state discovery to powernv core powerpc, powernv, CPU hotplug: Put offline CPUs in Fast-Sleep instead of Nap
KVM: PPC: Book3S HV: Enable CPUs to run guest after waking up from fast-sleep
KVM: PPC: Book3S HV: Consolidate the idle-state enter sequence in KVM
KVM: PPC: Book3S HV: Put KVM standby hwthreads to fast-sleep instead of nap ppc,book3s: Go back to same idle state after handling machine check interrupt

 arch/powerpc/include/asm/processor.h    |   12 ++++
 arch/powerpc/kernel/exceptions-64s.S    |   51 +++++++++------
 arch/powerpc/kernel/idle.c              |   52 ++++++++++++++++
 arch/powerpc/kernel/idle_power7.S       |    2 -
 arch/powerpc/kvm/book3s_hv_rmhandlers.S |  103 ++++++++++++++++++++++++-------
 arch/powerpc/platforms/powernv/smp.c    |   12 +++-
 drivers/cpuidle/cpuidle-powernv.c       |    7 +-
 7 files changed, 190 insertions(+), 49 deletions(-)
#1 lore raw

PATCH 1/6 powernv, cpuidle: Move the flags used for idle state discovery to powernv core

From: Srivatsa S. Bhat <[email protected]>

These flags will be used by the cpuidle driver as well as in the cpu offline path. The offline cpus should be put to fastsleep if the idle state is discovered so as to gain maximum power savings in the offline state.

---

 arch/powerpc/include/asm/processor.h |    4 ++++
 drivers/cpuidle/cpuidle-powernv.c    |    7 +++----
 2 files changed, 7 insertions(+), 4 deletions(-)
arch/powerpc/include/asm/processor.h +4 -0
index d660dc3..d922e5c 100644
@@ -445,6 +445,10 @@ static inline unsigned long get_clean_sp(unsigned long sp, int is_32)
 }
 #endif
 
+/* Support for 'nap' and 'sleep' instructions, as discovered from the DT */
+#define IDLE_INST_NAP	0x00010000 /* nap instruction can be used */
+#define IDLE_INST_SLEEP	0x00020000 /* sleep instruction can be used */
+
 extern unsigned long cpuidle_disable;
 enum idle_boot_override {IDLE_NO_OVERRIDE = 0, IDLE_POWERSAVE_OFF};
 
drivers/cpuidle/cpuidle-powernv.c +3 -4
index 719f6fb..5d4f9e8 100644
@@ -17,12 +17,11 @@
 #include <asm/machdep.h>
 #include <asm/firmware.h>
 #include <asm/runlatch.h>
+#include <asm/processor.h>
 
 /* Flags and constants used in PowerNV platform */
 
 #define MAX_POWERNV_IDLE_STATES	8
-#define IDLE_USE_INST_NAP	0x00010000 /* Use nap instruction */
-#define IDLE_USE_INST_SLEEP	0x00020000 /* Use sleep instruction */
 
 struct cpuidle_driver powernv_idle_driver = {
 	.name             = "powernv_idle",
@@ -187,7 +186,7 @@ static int powernv_add_idle_states(void)
 
 	for (i = 0; i < dt_idle_states; i++) {
 
-		if (flags[i] & IDLE_USE_INST_NAP) {
+		if (flags[i] & IDLE_INST_NAP) {
 			/* Add NAP state */
 			strcpy(powernv_states[nr_idle_states].name, "Nap");
 			strcpy(powernv_states[nr_idle_states].desc, "Nap");
@@ -198,7 +197,7 @@ static int powernv_add_idle_states(void)
 			nr_idle_states++;
 		}
 
-		if (flags[i] & IDLE_USE_INST_SLEEP) {
+		if (flags[i] & IDLE_INST_SLEEP) {
 			/* Add FASTSLEEP state */
 			strcpy(powernv_states[nr_idle_states].name, "FastSleep");
 			strcpy(powernv_states[nr_idle_states].desc, "FastSleep");
#2 lore raw

PATCH 2/6 powerpc, powernv, CPU hotplug: Put offline CPUs in Fast-Sleep instead of Nap

In reply to Preeti U Murthy

From: Srivatsa S. Bhat <[email protected]>

The offline cpus are put to fast sleep if the idle state is discovered in the device tree. This is to gain maximum powersavings in the offline state.

---

 arch/powerpc/include/asm/processor.h |    8 +++++
 arch/powerpc/kernel/idle.c           |   52 ++++++++++++++++++++++++++++++++++
 arch/powerpc/platforms/powernv/smp.c |   12 +++++++-
 3 files changed, 71 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/processor.h +8 -0
index d922e5c..c5256db 100644
@@ -449,6 +449,14 @@ static inline unsigned long get_clean_sp(unsigned long sp, int is_32)
 #define IDLE_INST_NAP	0x00010000 /* nap instruction can be used */
 #define IDLE_INST_SLEEP	0x00020000 /* sleep instruction can be used */
 
+/* Flags to indicate which of the CPU idle states are available for use */
+
+#define IDLE_USE_NAP		(1UL << 0)
+#define IDLE_USE_SLEEP		(1UL << 1)
+
+extern unsigned int supported_cpuidle_states;
+extern unsigned int pnv_get_supported_cpuidle_states(void);
+
 extern unsigned long cpuidle_disable;
 enum idle_boot_override {IDLE_NO_OVERRIDE = 0, IDLE_POWERSAVE_OFF};
 
arch/powerpc/kernel/idle.c +52 -0

70 lines not on this page · raw message

arch/powerpc/platforms/powernv/smp.c +11 -1
index bf5fcd4..fc83006 100644
@@ -31,6 +31,7 @@
 #include <asm/xics.h>
 #include <asm/opal.h>
 #include <asm/runlatch.h>
+#include <asm/processor.h>
 
 #include "powernv.h"
 
@@ -142,6 +143,7 @@ static int pnv_smp_cpu_disable(void)
 static void pnv_smp_cpu_kill_self(void)
 {
 	unsigned int cpu;
+	unsigned long idle_states;
 
 	/* Standard hot unplug procedure */
 	local_irq_disable();
@@ -152,13 +154,21 @@ static void pnv_smp_cpu_kill_self(void)
 	generic_set_cpu_dead(cpu);
 	smp_wmb();
 
+	idle_states = pnv_get_supported_cpuidle_states();
+
 	/* We don't want to take decrementer interrupts while we are offline,
 	 * so clear LPCR:PECE1. We keep PECE2 enabled.
 	 */
 	mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) & ~(u64)LPCR_PECE1);
 	while (!generic_check_cpu_restart(cpu)) {
 		ppc64_runlatch_off();
-		power7_nap();
+
+		/* If sleep is supported, go to sleep, instead of nap */
+		if (idle_states & IDLE_USE_SLEEP)
+			power7_sleep();
+		else
+			power7_nap();
+
 		ppc64_runlatch_on();
 		if (!generic_check_cpu_restart(cpu)) {
 			DBG("CPU%d Unexpected exit while offline !\n", cpu);
#3 lore raw

Re: [PATCH 2/6] powerpc, powernv, CPU hotplug: Put offline CPUs in Fast-Sleep instead of Nap

Quoted 4 lines · Preeti U Murthy

From: Srivatsa S. Bhat <[email protected]>

The offline cpus are put to fast sleep if the idle state is discovered in the device tree. This is to gain maximum powersavings in the offline state.

...

arch/powerpc/platforms/powernv/smp.c static void pnv_smp_cpu_kill_self(void) · line 174

Quoted 13 lines
arch/powerpc/platforms/powernv/smp.c +7 -1
 	while (!generic_check_cpu_restart(cpu)) {
 		ppc64_runlatch_off();
-		power7_nap();
+
+		/* If sleep is supported, go to sleep, instead of nap */
+		if (idle_states & IDLE_USE_SLEEP)
+			power7_sleep();
+		else
+			power7_nap();
+
 		ppc64_runlatch_on();
 		if (!generic_check_cpu_restart(cpu)) {
 			DBG("CPU%d Unexpected exit while offline !\n", cpu);

What is the latency for waking up from fast sleep state? I'm concerned this will increase the latency for entering KVM guests.

Paul.

#4 lore raw

PATCH 3/6 KVM: PPC: Book3S HV: Enable CPUs to run guest after waking up from fast-sleep

In reply to Preeti U Murthy

From: Srivatsa S. Bhat <[email protected]>

When guests have to be launched, the secondary threads which are offline are woken up to run the guests. Today these threads wake up from nap and check if they have to run guests. Now that the offline secondary threads can go to fastsleep, add this check in the fastsleep wakeup path as well.

---

 arch/powerpc/kernel/exceptions-64s.S |   30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)
arch/powerpc/kernel/exceptions-64s.S +15 -15
index 3afd391..b4bf464 100644
@@ -100,6 +100,19 @@ system_reset_pSeries:
 	SET_SCRATCH0(r13)
 #ifdef CONFIG_PPC_P7_NAP
 BEGIN_FTR_SECTION
+
+	GET_PACA(r13)
+#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
+	li	r0,KVM_HWTHREAD_IN_KERNEL
+	stb	r0,HSTATE_HWTHREAD_STATE(r13)
+	/* Order setting hwthread_state vs. testing hwthread_req */
+	sync
+	lbz	r0,HSTATE_HWTHREAD_REQ(r13)
+	cmpwi	r0,0
+	beq	1f
+	b	kvm_start_guest
+1:
+#endif
 	/* Running native on arch 2.06 or later, check if we are
 	 * waking up from nap. We only handle no state loss and
 	 * supervisor state loss. We do -not- handle hypervisor
@@ -116,28 +129,15 @@ BEGIN_FTR_SECTION
 	 * OPAL v3 based powernv platforms have new idle states
 	 * which fall in this catagory.
 	 */
-	bgt	cr1,8f
 	GET_PACA(r13)
-
-#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
-	li	r0,KVM_HWTHREAD_IN_KERNEL
-	stb	r0,HSTATE_HWTHREAD_STATE(r13)
-	/* Order setting hwthread_state vs. testing hwthread_req */
-	sync
-	lbz	r0,HSTATE_HWTHREAD_REQ(r13)
-	cmpwi	r0,0
-	beq	1f
-	b	kvm_start_guest
-1:
-#endif
+	bgt	cr1,8f
 
 	beq	cr1,2f
 	b	.power7_wakeup_noloss
 2:	b	.power7_wakeup_loss
 
 	/* Fast Sleep wakeup on PowerNV */
-8:	GET_PACA(r13)
-	b 	.power7_wakeup_tb_loss
+8:	b 	.power7_wakeup_tb_loss
 
 9:
 END_FTR_SECTION_IFSET(CPU_FTR_HVMODE | CPU_FTR_ARCH_206)
#5 lore raw

PATCH 4/6 KVM: PPC: Book3S HV: Consolidate the idle-state enter sequence in KVM

In reply to Preeti U Murthy

From: Srivatsa S. Bhat <[email protected]>

Now that the support for fast sleep idle state is present, the KVM standby threads can be put to fast sleep when they are either idle or do not have a guest to run. Today they enter nap in these scenarios. The purpose is to gain maximum power savings in a KVM scenario as well when an entire cpu core is idle.

As a precursor, consolidate the code common across all idle states.

---

 arch/powerpc/kvm/book3s_hv_rmhandlers.S |   30 ++++++++++++++++--------------
 1 file changed, 16 insertions(+), 14 deletions(-)
arch/powerpc/kvm/book3s_hv_rmhandlers.S +16 -14
index b031f93..43aa806 100644
@@ -40,6 +40,17 @@
 #define NAPPING_CEDE	1
 #define NAPPING_NOVCPU	2
 
+#define IDLE_STATE_ENTER_SEQ_HV(IDLE_INST)			\
+	/* Magic NAP/SLEEP/WINKLE mode enter sequence */	\
+	std	r0, HSTATE_SCRATCH0(r13);			\
+	ptesync;						\
+	ld	r0, HSTATE_SCRATCH0(r13);			\
+1:	cmpd	r0, r0;						\
+	bne	1b;						\
+	IDLE_INST;						\
+	b	.
+
+
 /*
  * Call kvmppc_hv_entry in real mode.
  * Must be called with interrupts hard-disabled.
@@ -325,13 +336,9 @@ kvm_do_nap:
 	rlwimi	r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
 	mtspr	SPRN_LPCR, r4
 	isync
-	std	r0, HSTATE_SCRATCH0(r13)
-	ptesync
-	ld	r0, HSTATE_SCRATCH0(r13)
-1:	cmpd	r0, r0
-	bne	1b
-	nap
-	b	.
+	IDLE_STATE_ENTER_SEQ_HV(PPC_NAP)
+	/* No return */
+
 
 /******************************************************************************
  *                                                                            *
@@ -2027,13 +2034,8 @@ END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
 	mtspr	SPRN_LPCR,r5
 	isync
 	li	r0, 0
-	std	r0, HSTATE_SCRATCH0(r13)
-	ptesync
-	ld	r0, HSTATE_SCRATCH0(r13)
-1:	cmpd	r0, r0
-	bne	1b
-	nap
-	b	.
+	IDLE_STATE_ENTER_SEQ_HV(PPC_NAP)
+	/* No return */
 
 33:	mr	r4, r3
 	li	r3, 0
#6 lore raw

PATCH 5/6 KVM: PPC: Book3S HV: Put KVM standby hwthreads to fast-sleep instead of nap

In reply to Preeti U Murthy

From: Srivatsa S. Bhat <[email protected]>

Now that the support for fast sleep idle state is present, allow the KVM standby threads to go to fast sleep if the platform supports it.This will fetch us maximum power savings if an entire core is idle.

---

 arch/powerpc/kvm/book3s_hv_rmhandlers.S |   73 ++++++++++++++++++++++++++++---
 1 file changed, 65 insertions(+), 8 deletions(-)
arch/powerpc/kvm/book3s_hv_rmhandlers.S +65 -8
index 43aa806..69244cc 100644
@@ -207,7 +207,7 @@ kvmppc_primary_no_guest:
 	li	r3, 1
 	stb	r3, HSTATE_HWTHREAD_REQ(r13)
 
-	b	kvm_do_nap
+	b	kvm_do_idle
 
 kvm_novcpu_wakeup:
 	ld	r1, HSTATE_HOST_R1(r13)
@@ -247,7 +247,7 @@ kvm_novcpu_exit:
 	b	hdec_soon
 
 /*
- * We come in here when wakened from nap mode.
+ * We come in here when wakened from nap or fast-sleep mode.
  * Relocation is off and most register values are lost.
  * r13 points to the PACA.
  */
@@ -303,7 +303,7 @@ kvm_start_guest:
 
 	bl	kvmppc_hv_entry
 
-	/* Back from the guest, go back to nap */
+	/* Back from the guest, go back to nap or fastsleep */
 	/* Clear our vcpu pointer so we don't come back in early */
 	li	r0, 0
 	std	r0, HSTATE_KVM_VCPU(r13)
@@ -314,7 +314,7 @@ kvm_start_guest:
 	 */
 	lwsync
 
-	/* increment the nap count and then go to nap mode */
+	/* increment the nap count and then go to nap or fast-sleep mode */
 	ld	r4, HSTATE_KVM_VCORE(r13)
 	addi	r4, r4, VCORE_NAP_COUNT
 51:	lwarx	r3, 0, r4
@@ -325,6 +325,24 @@ kvm_start_guest:
 kvm_no_guest:
 	li	r0, KVM_HWTHREAD_IN_NAP
 	stb	r0, HSTATE_HWTHREAD_STATE(r13)
+
+kvm_do_idle:
+	/*
+	 * if (supported_cpuidle_states & IDLE_USE_SLEEP)
+	 * 		kvm_do_fastsleep();
+	 * else
+	 *		kvm_do_nap();
+	 */
+	LOAD_REG_ADDRBASE(r3,supported_cpuidle_states)
+	lwz     r4,ADDROFF(supported_cpuidle_states)(r3)
+	/*
+	 * andi. r4,r4,IDLE_USE_SLEEP. Replacing IDLE_USE_SLEEP
+	 * with the immediate value since it is a 32 bit instruction
+	 * and the operand needs to fit into this.
+	 */
+	andi.	r4,r4,2
+	bne	kvm_do_fastsleep
+
 kvm_do_nap:
 	/* Clear the runlatch bit before napping */
 	mfspr	r2, SPRN_CTRLF
@@ -339,6 +357,18 @@ kvm_do_nap:
 	IDLE_STATE_ENTER_SEQ_HV(PPC_NAP)
 	/* No return */
 
+kvm_do_fastsleep:
+	li	r3, LPCR_PECE0
+	mfspr	r4, SPRN_LPCR
+	/* Don't set LPCR_PECE1 since we want to wakeup only on an external
+	 * interrupt, and not on a decrementer interrupt.
+	 */
+	rlwimi	r4, r3, 0, LPCR_PECE0
+	mtspr	SPRN_LPCR, r4
+	isync
+	IDLE_STATE_ENTER_SEQ_HV(PPC_SLEEP)
+	/* No return */
+
 
 /******************************************************************************
  *                                                                            *
@@ -2016,8 +2046,8 @@ END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_206)
 	bl	kvmppc_save_fp
 
 	/*
-	 * Take a nap until a decrementer or external or doobell interrupt
-	 * occurs, with PECE1, PECE0 and PECEDP set in LPCR. Also clear the
+	 * Go to fastsleep until an external or doobell interrupt
+	 * occurs, with PECE0 and PECEDP set in LPCR. Also clear the
 	 * runlatch bit before napping.
 	 */
 	mfspr	r2, SPRN_CTRLF
@@ -2026,6 +2056,22 @@ END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_206)
 
 	li	r0,1
 	stb	r0,HSTATE_HWTHREAD_REQ(r13)
+	/*
+        * if (supported_cpuidle_states & IDLE_USE_SLEEP)
+        *              PPC_SLEEP;
+        * else
+        *              PPC_NAP;
+        */
+       	LOAD_REG_ADDRBASE(r3,supported_cpuidle_states)
+       	lwz     r4,ADDROFF(supported_cpuidle_states)(r3)
+	/*
+	 * andi. r4,r4,IDLE_USE_SLEEP. Replacing IDLE_USE_SLEEP
+	 * with the immediate value since it is a 32 bit instruction
+	 * and the operand needs to fit into this.
+	 */
+       	andi.   r4,r4,2
+       	bne     35f
+
 	mfspr	r5,SPRN_LPCR
 	ori	r5,r5,LPCR_PECE0 | LPCR_PECE1
 BEGIN_FTR_SECTION
@@ -2037,6 +2083,17 @@ END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
 	IDLE_STATE_ENTER_SEQ_HV(PPC_NAP)
 	/* No return */
 
+35:	mfspr	r5,SPRN_LPCR
+	ori	r5,r5,LPCR_PECE0
+BEGIN_FTR_SECTION
+	oris	r5,r5,LPCR_PECEDP@h
+END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
+	mtspr	SPRN_LPCR,r5
+	isync
+	li	r0, 0
+	IDLE_STATE_ENTER_SEQ_HV(PPC_SLEEP)
+	/* No return */
+
 33:	mr	r4, r3
 	li	r3, 0
 	li	r12, 0
@@ -2046,7 +2103,7 @@ kvm_end_cede:
 	/* get vcpu pointer */
 	ld	r4, HSTATE_KVM_VCPU(r13)
 
-	/* Woken by external or decrementer interrupt */
+	/* Woken by external */
 	ld	r1, HSTATE_HOST_R1(r13)
 
 	/* load up FP state */
@@ -2129,7 +2186,7 @@ machine_check_realmode:
 	b	fast_interrupt_c_return
 
 /*
- * Check the reason we woke from nap, and take appropriate action.
+ * Check the reason we woke from nap or fastsleep, and take appropriate action.
  * Returns:
  *	0 if nothing needs to be done
  *	1 if something happened that needs to be handled by the host
#7 lore raw

PATCH 6/6 ppc, book3s: Go back to same idle state after handling machine check interrupt

In reply to Preeti U Murthy

From: Srivatsa S. Bhat <[email protected]>

Now that the support for fast sleep is present, threads could have woken up from fast sleep on getting a machine check interrupt. Hence add code to allow threads to go back to the idle state they woke up from after handling the interrupt. Today they go back to nap by default.

---

 arch/powerpc/kernel/exceptions-64s.S |   21 +++++++++++++++------
 arch/powerpc/kernel/idle_power7.S    |    2 +-
 2 files changed, 16 insertions(+), 7 deletions(-)
arch/powerpc/kernel/exceptions-64s.S +15 -6
index b4bf464..94cee3c 100644
@@ -1396,15 +1396,16 @@ machine_check_handle_early:
 	 * of the following is true:
 	 * a. thread wasn't in power saving mode
 	 * b. thread was in power saving mode with no state loss or
-	 *    supervisor state loss
+	 *    supervisor state loss or hypervisor state loss (fastsleep)
 	 *
-	 * Go back to nap again if (b) is true.
+	 * Go back to nap or fastsleep again if (b) is true.
 	 */
 	rlwinm.	r11,r12,47-31,30,31	/* Was it in power saving mode? */
 	beq	4f			/* No, it wasn;t */
-	/* Thread was in power saving mode. Go back to nap again. */
-	cmpwi	r11,2
-	bne	3f
+	/* Thread was in power saving mode. Go back to the same state again. */
+	cmpwi	cr1,r11,2
+	blt	cr1,3f
+7:
 	/* Supervisor state loss */
 	li	r0,1
 	stb	r0,PACA_NAPSTATELOST(r13)
@@ -1412,7 +1413,15 @@ machine_check_handle_early:
 	MACHINE_CHECK_HANDLER_WINDUP
 	GET_PACA(r13)
 	ld	r1,PACAR1(r13)
-	b	.power7_enter_nap_mode
+	/* We need to pass the idle state in r3: 0 -> nap, 1 -> sleep */
+	bgt	cr1,8f
+	li	r3,0
+	b	.power7_enter_idle
+	/* No return */
+
+8:	li	r3,1 /* Pass 1 in r3 to request sleep in power7_enter_idle */
+	b	.power7_enter_idle
+	/* No return */
 4:
 #endif
 	/*
arch/powerpc/kernel/idle_power7.S +1 -1
index c3ab869..e13e21b 100644
@@ -95,7 +95,7 @@ _GLOBAL(power7_powersave_common)
 	std	r9,_MSR(r1)
 	std	r1,PACAR1(r13)
 
-_GLOBAL(power7_enter_nap_mode)
+_GLOBAL(power7_enter_idle)
 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
 	/* Tell KVM we're napping */
 	li	r4,KVM_HWTHREAD_IN_NAP