18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu>
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci#ifndef _ASM_POWERPC_BARRIER_H
68c2ecf20Sopenharmony_ci#define _ASM_POWERPC_BARRIER_H
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <asm/asm-const.h>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__
118c2ecf20Sopenharmony_ci#include <asm/ppc-opcode.h>
128c2ecf20Sopenharmony_ci#endif
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci/*
158c2ecf20Sopenharmony_ci * Memory barrier.
168c2ecf20Sopenharmony_ci * The sync instruction guarantees that all memory accesses initiated
178c2ecf20Sopenharmony_ci * by this processor have been performed (with respect to all other
188c2ecf20Sopenharmony_ci * mechanisms that access memory).  The eieio instruction is a barrier
198c2ecf20Sopenharmony_ci * providing an ordering (separately) for (a) cacheable stores and (b)
208c2ecf20Sopenharmony_ci * loads and stores to non-cacheable memory (e.g. I/O devices).
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * mb() prevents loads and stores being reordered across this point.
238c2ecf20Sopenharmony_ci * rmb() prevents loads being reordered across this point.
248c2ecf20Sopenharmony_ci * wmb() prevents stores being reordered across this point.
258c2ecf20Sopenharmony_ci *
268c2ecf20Sopenharmony_ci * *mb() variants without smp_ prefix must order all types of memory
278c2ecf20Sopenharmony_ci * operations with one another. sync is the only instruction sufficient
288c2ecf20Sopenharmony_ci * to do this.
298c2ecf20Sopenharmony_ci *
308c2ecf20Sopenharmony_ci * For the smp_ barriers, ordering is for cacheable memory operations
318c2ecf20Sopenharmony_ci * only. We have to use the sync instruction for smp_mb(), since lwsync
328c2ecf20Sopenharmony_ci * doesn't order loads with respect to previous stores.  Lwsync can be
338c2ecf20Sopenharmony_ci * used for smp_rmb() and smp_wmb().
348c2ecf20Sopenharmony_ci *
358c2ecf20Sopenharmony_ci * However, on CPUs that don't support lwsync, lwsync actually maps to a
368c2ecf20Sopenharmony_ci * heavy-weight sync, so smp_wmb() can be a lighter-weight eieio.
378c2ecf20Sopenharmony_ci */
388c2ecf20Sopenharmony_ci#define mb()   __asm__ __volatile__ ("sync" : : : "memory")
398c2ecf20Sopenharmony_ci#define rmb()  __asm__ __volatile__ ("sync" : : : "memory")
408c2ecf20Sopenharmony_ci#define wmb()  __asm__ __volatile__ ("sync" : : : "memory")
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci/* The sub-arch has lwsync */
438c2ecf20Sopenharmony_ci#if defined(__powerpc64__) || defined(CONFIG_PPC_E500MC)
448c2ecf20Sopenharmony_ci#    define SMPWMB      LWSYNC
458c2ecf20Sopenharmony_ci#else
468c2ecf20Sopenharmony_ci#    define SMPWMB      eieio
478c2ecf20Sopenharmony_ci#endif
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci/* clang defines this macro for a builtin, which will not work with runtime patching */
508c2ecf20Sopenharmony_ci#undef __lwsync
518c2ecf20Sopenharmony_ci#define __lwsync()	__asm__ __volatile__ (stringify_in_c(LWSYNC) : : :"memory")
528c2ecf20Sopenharmony_ci#define dma_rmb()	__lwsync()
538c2ecf20Sopenharmony_ci#define dma_wmb()	__asm__ __volatile__ (stringify_in_c(SMPWMB) : : :"memory")
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci#define __smp_lwsync()	__lwsync()
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci#define __smp_mb()	mb()
588c2ecf20Sopenharmony_ci#define __smp_rmb()	__lwsync()
598c2ecf20Sopenharmony_ci#define __smp_wmb()	__asm__ __volatile__ (stringify_in_c(SMPWMB) : : :"memory")
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci/*
628c2ecf20Sopenharmony_ci * This is a barrier which prevents following instructions from being
638c2ecf20Sopenharmony_ci * started until the value of the argument x is known.  For example, if
648c2ecf20Sopenharmony_ci * x is a variable loaded from memory, this prevents following
658c2ecf20Sopenharmony_ci * instructions from being executed until the load has been performed.
668c2ecf20Sopenharmony_ci */
678c2ecf20Sopenharmony_ci#define data_barrier(x)	\
688c2ecf20Sopenharmony_ci	asm volatile("twi 0,%0,0; isync" : : "r" (x) : "memory");
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci#define __smp_store_release(p, v)						\
718c2ecf20Sopenharmony_cido {									\
728c2ecf20Sopenharmony_ci	compiletime_assert_atomic_type(*p);				\
738c2ecf20Sopenharmony_ci	__smp_lwsync();							\
748c2ecf20Sopenharmony_ci	WRITE_ONCE(*p, v);						\
758c2ecf20Sopenharmony_ci} while (0)
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci#define __smp_load_acquire(p)						\
788c2ecf20Sopenharmony_ci({									\
798c2ecf20Sopenharmony_ci	typeof(*p) ___p1 = READ_ONCE(*p);				\
808c2ecf20Sopenharmony_ci	compiletime_assert_atomic_type(*p);				\
818c2ecf20Sopenharmony_ci	__smp_lwsync();							\
828c2ecf20Sopenharmony_ci	___p1;								\
838c2ecf20Sopenharmony_ci})
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC64
868c2ecf20Sopenharmony_ci#define smp_cond_load_relaxed(ptr, cond_expr) ({		\
878c2ecf20Sopenharmony_ci	typeof(ptr) __PTR = (ptr);				\
888c2ecf20Sopenharmony_ci	__unqual_scalar_typeof(*ptr) VAL;			\
898c2ecf20Sopenharmony_ci	VAL = READ_ONCE(*__PTR);				\
908c2ecf20Sopenharmony_ci	if (unlikely(!(cond_expr))) {				\
918c2ecf20Sopenharmony_ci		spin_begin();					\
928c2ecf20Sopenharmony_ci		do {						\
938c2ecf20Sopenharmony_ci			VAL = READ_ONCE(*__PTR);		\
948c2ecf20Sopenharmony_ci		} while (!(cond_expr));				\
958c2ecf20Sopenharmony_ci		spin_end();					\
968c2ecf20Sopenharmony_ci	}							\
978c2ecf20Sopenharmony_ci	(typeof(*ptr))VAL;					\
988c2ecf20Sopenharmony_ci})
998c2ecf20Sopenharmony_ci#endif
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_BOOK3S_64
1028c2ecf20Sopenharmony_ci#define NOSPEC_BARRIER_SLOT   nop
1038c2ecf20Sopenharmony_ci#elif defined(CONFIG_PPC_FSL_BOOK3E)
1048c2ecf20Sopenharmony_ci#define NOSPEC_BARRIER_SLOT   nop; nop
1058c2ecf20Sopenharmony_ci#endif
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_BARRIER_NOSPEC
1088c2ecf20Sopenharmony_ci/*
1098c2ecf20Sopenharmony_ci * Prevent execution of subsequent instructions until preceding branches have
1108c2ecf20Sopenharmony_ci * been fully resolved and are no longer executing speculatively.
1118c2ecf20Sopenharmony_ci */
1128c2ecf20Sopenharmony_ci#define barrier_nospec_asm NOSPEC_BARRIER_FIXUP_SECTION; NOSPEC_BARRIER_SLOT
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci// This also acts as a compiler barrier due to the memory clobber.
1158c2ecf20Sopenharmony_ci#define barrier_nospec() asm (stringify_in_c(barrier_nospec_asm) ::: "memory")
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci#else /* !CONFIG_PPC_BARRIER_NOSPEC */
1188c2ecf20Sopenharmony_ci#define barrier_nospec_asm
1198c2ecf20Sopenharmony_ci#define barrier_nospec()
1208c2ecf20Sopenharmony_ci#endif /* CONFIG_PPC_BARRIER_NOSPEC */
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci/*
1238c2ecf20Sopenharmony_ci * pmem_wmb() ensures that all stores for which the modification
1248c2ecf20Sopenharmony_ci * are written to persistent storage by preceding dcbfps/dcbstps
1258c2ecf20Sopenharmony_ci * instructions have updated persistent storage before any data
1268c2ecf20Sopenharmony_ci * access or data transfer caused by subsequent instructions is
1278c2ecf20Sopenharmony_ci * initiated.
1288c2ecf20Sopenharmony_ci */
1298c2ecf20Sopenharmony_ci#define pmem_wmb() __asm__ __volatile__(PPC_PHWSYNC ::: "memory")
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci#include <asm-generic/barrier.h>
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci#endif /* _ASM_POWERPC_BARRIER_H */
134