Skip to content

Commit 7672691

Browse files
paulusmackmpe
authored andcommitted
powerpc/powernv: Provide a way to force a core into SMT4 mode
POWER9 processors up to and including "Nimbus" v2.2 have hardware bugs relating to transactional memory and thread reconfiguration. One of these bugs has a workaround which is to get the core into SMT4 state temporarily. This workaround is only needed when running bare-metal. This patch provides a function which gets the core into SMT4 mode by preventing threads from going to a stop state, and waking up those which are already in a stop state. Once at least 3 threads are not in a stop state, the core will be in SMT4 and we can continue. To do this, we add a "dont_stop" flag to the paca to tell the thread not to go into a stop state. If this flag is set, power9_idle_stop() just returns immediately with a return value of 0. The pnv_power9_force_smt4_catch() function does the following: 1. Set the dont_stop flag for each thread in the core, except ourselves (in fact we use an atomic_inc() in case more than one thread is calling this function concurrently). 2. See how many threads are awake, indicated by their requested_psscr field in the paca being 0. If this is at least 3, skip to step 5. 3. Send a doorbell interrupt to each thread that was seen as being in a stop state in step 2. 4. Until at least 3 threads are awake, scan the threads to which we sent a doorbell interrupt and check if they are awake now. This relies on the following properties: - Once dont_stop is non-zero, requested_psccr can't go from zero to non-zero, except transiently (and without the thread doing stop). - requested_psscr being zero guarantees that the thread isn't in a state-losing stop state where thread reconfiguration could occur. - Doing stop with a PSSCR value of 0 won't be a state-losing stop and thus won't allow thread reconfiguration. - Once threads_per_core/2 + 1 (i.e. 3) threads are awake, the core must be in SMT4 mode, since SMT modes are powers of 2. This does add a sync to power9_idle_stop(), which is necessary to provide the correct ordering between setting requested_psscr and checking dont_stop. The overhead of the sync should be unnoticeable compared to the latency of going into and out of a stop state. Because some objected to incurring this extra latency on systems where the XER[SO] bug is not relevant, I have put the test in power9_idle_stop inside a feature section. This means that pnv_power9_force_smt4_catch() WILL NOT WORK correctly on systems without the CPU_FTR_P9_TM_XER_SO_BUG feature bit set, and will probably hang the system. In order to cater for uses where the caller has an operation that has to be done while the core is in SMT4, the core continues to be kept in SMT4 after pnv_power9_force_smt4_catch() function returns, until the pnv_power9_force_smt4_release() function is called. It undoes the effect of step 1 above and allows the other threads to go into a stop state. Signed-off-by: Paul Mackerras <[email protected]> Signed-off-by: Michael Ellerman <[email protected]>
1 parent b5af4f2 commit 7672691

File tree

6 files changed

+110
-0
lines changed

6 files changed

+110
-0
lines changed

arch/powerpc/include/asm/asm-prototypes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,7 @@ extern int __ucmpdi2(u64, u64);
126126
void _mcount(void);
127127
unsigned long prepare_ftrace_return(unsigned long parent, unsigned long ip);
128128

129+
void pnv_power9_force_smt4_catch(void);
130+
void pnv_power9_force_smt4_release(void);
131+
129132
#endif /* _ASM_POWERPC_ASM_PROTOTYPES_H */

arch/powerpc/include/asm/paca.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <asm/accounting.h>
3333
#include <asm/hmi.h>
3434
#include <asm/cpuidle.h>
35+
#include <asm/atomic.h>
3536

3637
register struct paca_struct *local_paca asm("r13");
3738

@@ -177,6 +178,8 @@ struct paca_struct {
177178
u8 thread_mask;
178179
/* Mask to denote subcore sibling threads */
179180
u8 subcore_sibling_mask;
181+
/* Flag to request this thread not to stop */
182+
atomic_t dont_stop;
180183
/*
181184
* Pointer to an array which contains pointer
182185
* to the sibling threads' paca.

arch/powerpc/include/asm/powernv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ static inline int pnv_npu2_handle_fault(struct npu_context *context,
4040
}
4141

4242
static inline void pnv_tm_init(void) { }
43+
static inline void pnv_power9_force_smt4(void) { }
4344
#endif
4445

4546
#endif /* _ASM_POWERNV_H */

arch/powerpc/kernel/asm-offsets.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,7 @@ int main(void)
759759
OFFSET(PACA_SUBCORE_SIBLING_MASK, paca_struct, subcore_sibling_mask);
760760
OFFSET(PACA_SIBLING_PACA_PTRS, paca_struct, thread_sibling_pacas);
761761
OFFSET(PACA_REQ_PSSCR, paca_struct, requested_psscr);
762+
OFFSET(PACA_DONT_STOP, paca_struct, dont_stop);
762763
#define STOP_SPR(x, f) OFFSET(x, paca_struct, stop_sprs.f)
763764
STOP_SPR(STOP_PID, pid);
764765
STOP_SPR(STOP_LDBAR, ldbar);

arch/powerpc/kernel/idle_book3s.S

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ power_enter_stop:
339339
bne .Lhandle_esl_ec_set
340340
PPC_STOP
341341
li r3,0 /* Since we didn't lose state, return 0 */
342+
std r3, PACA_REQ_PSSCR(r13)
342343

343344
/*
344345
* pnv_wakeup_noloss() expects r12 to contain the SRR1 value so
@@ -429,11 +430,29 @@ ALT_FTR_SECTION_END_NESTED_IFSET(CPU_FTR_ARCH_207S, 66); \
429430
* r3 contains desired PSSCR register value.
430431
*/
431432
_GLOBAL(power9_idle_stop)
433+
BEGIN_FTR_SECTION
434+
lwz r5, PACA_DONT_STOP(r13)
435+
cmpwi r5, 0
436+
bne 1f
432437
std r3, PACA_REQ_PSSCR(r13)
438+
sync
439+
lwz r5, PACA_DONT_STOP(r13)
440+
cmpwi r5, 0
441+
bne 1f
442+
END_FTR_SECTION_IFSET(CPU_FTR_P9_TM_XER_SO_BUG)
433443
mtspr SPRN_PSSCR,r3
434444
LOAD_REG_ADDR(r4,power_enter_stop)
435445
b pnv_powersave_common
436446
/* No return */
447+
1:
448+
/*
449+
* We get here when TM / thread reconfiguration bug workaround
450+
* code wants to get the CPU into SMT4 mode, and therefore
451+
* we are being asked not to stop.
452+
*/
453+
li r3, 0
454+
std r3, PACA_REQ_PSSCR(r13)
455+
blr /* return 0 for wakeup cause / SRR1 value */
437456

438457
/*
439458
* On waking up from stop 0,1,2 with ESL=1 on POWER9 DD1,
@@ -584,6 +603,8 @@ FTR_SECTION_ELSE_NESTED(71)
584603
mfspr r5, SPRN_PSSCR
585604
rldicl r5,r5,4,60
586605
ALT_FTR_SECTION_END_NESTED_IFSET(CPU_FTR_POWER9_DD1, 71)
606+
li r0, 0 /* clear requested_psscr to say we're awake */
607+
std r0, PACA_REQ_PSSCR(r13)
587608
cmpd cr4,r5,r4
588609
bge cr4,pnv_wakeup_tb_loss /* returns to caller */
589610

arch/powerpc/platforms/powernv/idle.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <asm/code-patching.h>
2525
#include <asm/smp.h>
2626
#include <asm/runlatch.h>
27+
#include <asm/dbell.h>
2728

2829
#include "powernv.h"
2930
#include "subcore.h"
@@ -387,6 +388,86 @@ void power9_idle(void)
387388
power9_idle_type(pnv_default_stop_val, pnv_default_stop_mask);
388389
}
389390

391+
#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
392+
/*
393+
* This is used in working around bugs in thread reconfiguration
394+
* on POWER9 (at least up to Nimbus DD2.2) relating to transactional
395+
* memory and the way that XER[SO] is checkpointed.
396+
* This function forces the core into SMT4 in order by asking
397+
* all other threads not to stop, and sending a message to any
398+
* that are in a stop state.
399+
* Must be called with preemption disabled.
400+
*
401+
* DO NOT call this unless cpu_has_feature(CPU_FTR_P9_TM_XER_SO_BUG) is
402+
* true; otherwise this function will hang the system, due to the
403+
* optimization in power9_idle_stop.
404+
*/
405+
void pnv_power9_force_smt4_catch(void)
406+
{
407+
int cpu, cpu0, thr;
408+
struct paca_struct *tpaca;
409+
int awake_threads = 1; /* this thread is awake */
410+
int poke_threads = 0;
411+
int need_awake = threads_per_core;
412+
413+
cpu = smp_processor_id();
414+
cpu0 = cpu & ~(threads_per_core - 1);
415+
tpaca = &paca[cpu0];
416+
for (thr = 0; thr < threads_per_core; ++thr) {
417+
if (cpu != cpu0 + thr)
418+
atomic_inc(&tpaca[thr].dont_stop);
419+
}
420+
/* order setting dont_stop vs testing requested_psscr */
421+
mb();
422+
for (thr = 0; thr < threads_per_core; ++thr) {
423+
if (!tpaca[thr].requested_psscr)
424+
++awake_threads;
425+
else
426+
poke_threads |= (1 << thr);
427+
}
428+
429+
/* If at least 3 threads are awake, the core is in SMT4 already */
430+
if (awake_threads < need_awake) {
431+
/* We have to wake some threads; we'll use msgsnd */
432+
for (thr = 0; thr < threads_per_core; ++thr) {
433+
if (poke_threads & (1 << thr)) {
434+
ppc_msgsnd_sync();
435+
ppc_msgsnd(PPC_DBELL_MSGTYPE, 0,
436+
tpaca[thr].hw_cpu_id);
437+
}
438+
}
439+
/* now spin until at least 3 threads are awake */
440+
do {
441+
for (thr = 0; thr < threads_per_core; ++thr) {
442+
if ((poke_threads & (1 << thr)) &&
443+
!tpaca[thr].requested_psscr) {
444+
++awake_threads;
445+
poke_threads &= ~(1 << thr);
446+
}
447+
}
448+
} while (awake_threads < need_awake);
449+
}
450+
}
451+
EXPORT_SYMBOL_GPL(pnv_power9_force_smt4_catch);
452+
453+
void pnv_power9_force_smt4_release(void)
454+
{
455+
int cpu, cpu0, thr;
456+
struct paca_struct *tpaca;
457+
458+
cpu = smp_processor_id();
459+
cpu0 = cpu & ~(threads_per_core - 1);
460+
tpaca = &paca[cpu0];
461+
462+
/* clear all the dont_stop flags */
463+
for (thr = 0; thr < threads_per_core; ++thr) {
464+
if (cpu != cpu0 + thr)
465+
atomic_dec(&tpaca[thr].dont_stop);
466+
}
467+
}
468+
EXPORT_SYMBOL_GPL(pnv_power9_force_smt4_release);
469+
#endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
470+
390471
#ifdef CONFIG_HOTPLUG_CPU
391472
static void pnv_program_cpu_hotplug_lpcr(unsigned int cpu, u64 lpcr_val)
392473
{

0 commit comments

Comments
 (0)