162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci#ifndef _SPARC64_BACKOFF_H
362306a36Sopenharmony_ci#define _SPARC64_BACKOFF_H
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci/* The macros in this file implement an exponential backoff facility
662306a36Sopenharmony_ci * for atomic operations.
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * When multiple threads compete on an atomic operation, it is
962306a36Sopenharmony_ci * possible for one thread to be continually denied a successful
1062306a36Sopenharmony_ci * completion of the compare-and-swap instruction.  Heavily
1162306a36Sopenharmony_ci * threaded cpu implementations like Niagara can compound this
1262306a36Sopenharmony_ci * problem even further.
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * When an atomic operation fails and needs to be retried, we spin a
1562306a36Sopenharmony_ci * certain number of times.  At each subsequent failure of the same
1662306a36Sopenharmony_ci * operation we double the spin count, realizing an exponential
1762306a36Sopenharmony_ci * backoff.
1862306a36Sopenharmony_ci *
1962306a36Sopenharmony_ci * When we spin, we try to use an operation that will cause the
2062306a36Sopenharmony_ci * current cpu strand to block, and therefore make the core fully
2162306a36Sopenharmony_ci * available to any other runnable strands.  There are two
2262306a36Sopenharmony_ci * options, based upon cpu capabilities.
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci * On all cpus prior to SPARC-T4 we do three dummy reads of the
2562306a36Sopenharmony_ci * condition code register.  Each read blocks the strand for something
2662306a36Sopenharmony_ci * between 40 and 50 cpu cycles.
2762306a36Sopenharmony_ci *
2862306a36Sopenharmony_ci * For SPARC-T4 and later we have a special "pause" instruction
2962306a36Sopenharmony_ci * available.  This is implemented using writes to register %asr27.
3062306a36Sopenharmony_ci * The cpu will block the number of cycles written into the register,
3162306a36Sopenharmony_ci * unless a disrupting trap happens first.  SPARC-T4 specifically
3262306a36Sopenharmony_ci * implements pause with a granularity of 8 cycles.  Each strand has
3362306a36Sopenharmony_ci * an internal pause counter which decrements every 8 cycles.  So the
3462306a36Sopenharmony_ci * chip shifts the %asr27 value down by 3 bits, and writes the result
3562306a36Sopenharmony_ci * into the pause counter.  If a value smaller than 8 is written, the
3662306a36Sopenharmony_ci * chip blocks for 1 cycle.
3762306a36Sopenharmony_ci *
3862306a36Sopenharmony_ci * To achieve the same amount of backoff as the three %ccr reads give
3962306a36Sopenharmony_ci * on earlier chips, we shift the backoff value up by 7 bits.  (Three
4062306a36Sopenharmony_ci * %ccr reads block for about 128 cycles, 1 << 7 == 128) We write the
4162306a36Sopenharmony_ci * whole amount we want to block into the pause register, rather than
4262306a36Sopenharmony_ci * loop writing 128 each time.
4362306a36Sopenharmony_ci */
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci#define BACKOFF_LIMIT	(4 * 1024)
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci#ifdef CONFIG_SMP
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci#define BACKOFF_SETUP(reg)	\
5062306a36Sopenharmony_ci	mov	1, reg
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci#define BACKOFF_LABEL(spin_label, continue_label) \
5362306a36Sopenharmony_ci	spin_label
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci#define BACKOFF_SPIN(reg, tmp, label)		\
5662306a36Sopenharmony_ci	mov		reg, tmp;		\
5762306a36Sopenharmony_ci88:	rd		%ccr, %g0;		\
5862306a36Sopenharmony_ci	rd		%ccr, %g0;		\
5962306a36Sopenharmony_ci	rd		%ccr, %g0;		\
6062306a36Sopenharmony_ci	.section	.pause_3insn_patch,"ax";\
6162306a36Sopenharmony_ci	.word		88b;			\
6262306a36Sopenharmony_ci	sllx		tmp, 7, tmp;		\
6362306a36Sopenharmony_ci	wr		tmp, 0, %asr27;		\
6462306a36Sopenharmony_ci	clr		tmp;			\
6562306a36Sopenharmony_ci	.previous;				\
6662306a36Sopenharmony_ci	brnz,pt		tmp, 88b;		\
6762306a36Sopenharmony_ci	 sub		tmp, 1, tmp;		\
6862306a36Sopenharmony_ci	set		BACKOFF_LIMIT, tmp;	\
6962306a36Sopenharmony_ci	cmp		reg, tmp;		\
7062306a36Sopenharmony_ci	bg,pn		%xcc, label;		\
7162306a36Sopenharmony_ci	 nop;					\
7262306a36Sopenharmony_ci	ba,pt		%xcc, label;		\
7362306a36Sopenharmony_ci	 sllx		reg, 1, reg;
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci#else
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci#define BACKOFF_SETUP(reg)
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci#define BACKOFF_LABEL(spin_label, continue_label) \
8062306a36Sopenharmony_ci	continue_label
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci#define BACKOFF_SPIN(reg, tmp, label)
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci#endif
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci#endif /* _SPARC64_BACKOFF_H */
87