162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci#ifndef __TOOLS_LINUX_SPARC64_BARRIER_H
362306a36Sopenharmony_ci#define __TOOLS_LINUX_SPARC64_BARRIER_H
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci/* Copied from the kernel sources to tools/:
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * These are here in an effort to more fully work around Spitfire Errata
862306a36Sopenharmony_ci * #51.  Essentially, if a memory barrier occurs soon after a mispredicted
962306a36Sopenharmony_ci * branch, the chip can stop executing instructions until a trap occurs.
1062306a36Sopenharmony_ci * Therefore, if interrupts are disabled, the chip can hang forever.
1162306a36Sopenharmony_ci *
1262306a36Sopenharmony_ci * It used to be believed that the memory barrier had to be right in the
1362306a36Sopenharmony_ci * delay slot, but a case has been traced recently wherein the memory barrier
1462306a36Sopenharmony_ci * was one instruction after the branch delay slot and the chip still hung.
1562306a36Sopenharmony_ci * The offending sequence was the following in sym_wakeup_done() of the
1662306a36Sopenharmony_ci * sym53c8xx_2 driver:
1762306a36Sopenharmony_ci *
1862306a36Sopenharmony_ci *	call	sym_ccb_from_dsa, 0
1962306a36Sopenharmony_ci *	 movge	%icc, 0, %l0
2062306a36Sopenharmony_ci *	brz,pn	%o0, .LL1303
2162306a36Sopenharmony_ci *	 mov	%o0, %l2
2262306a36Sopenharmony_ci *	membar	#LoadLoad
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci * The branch has to be mispredicted for the bug to occur.  Therefore, we put
2562306a36Sopenharmony_ci * the memory barrier explicitly into a "branch always, predicted taken"
2662306a36Sopenharmony_ci * delay slot to avoid the problem case.
2762306a36Sopenharmony_ci */
2862306a36Sopenharmony_ci#define membar_safe(type) \
2962306a36Sopenharmony_cido {	__asm__ __volatile__("ba,pt	%%xcc, 1f\n\t" \
3062306a36Sopenharmony_ci			     " membar	" type "\n" \
3162306a36Sopenharmony_ci			     "1:\n" \
3262306a36Sopenharmony_ci			     : : : "memory"); \
3362306a36Sopenharmony_ci} while (0)
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci/* The kernel always executes in TSO memory model these days,
3662306a36Sopenharmony_ci * and furthermore most sparc64 chips implement more stringent
3762306a36Sopenharmony_ci * memory ordering than required by the specifications.
3862306a36Sopenharmony_ci */
3962306a36Sopenharmony_ci#define mb()	membar_safe("#StoreLoad")
4062306a36Sopenharmony_ci#define rmb()	__asm__ __volatile__("":::"memory")
4162306a36Sopenharmony_ci#define wmb()	__asm__ __volatile__("":::"memory")
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci#define smp_store_release(p, v)			\
4462306a36Sopenharmony_cido {						\
4562306a36Sopenharmony_ci	barrier();				\
4662306a36Sopenharmony_ci	WRITE_ONCE(*p, v);			\
4762306a36Sopenharmony_ci} while (0)
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci#define smp_load_acquire(p)			\
5062306a36Sopenharmony_ci({						\
5162306a36Sopenharmony_ci	typeof(*p) ___p1 = READ_ONCE(*p);	\
5262306a36Sopenharmony_ci	barrier();				\
5362306a36Sopenharmony_ci	___p1;					\
5462306a36Sopenharmony_ci})
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci#endif /* !(__TOOLS_LINUX_SPARC64_BARRIER_H) */
57