xref: /OK3568_Linux_fs/kernel/arch/powerpc/include/asm/barrier.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu>
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun #ifndef _ASM_POWERPC_BARRIER_H
6*4882a593Smuzhiyun #define _ASM_POWERPC_BARRIER_H
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <asm/asm-const.h>
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #ifndef __ASSEMBLY__
11*4882a593Smuzhiyun #include <asm/ppc-opcode.h>
12*4882a593Smuzhiyun #endif
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun /*
15*4882a593Smuzhiyun  * Memory barrier.
16*4882a593Smuzhiyun  * The sync instruction guarantees that all memory accesses initiated
17*4882a593Smuzhiyun  * by this processor have been performed (with respect to all other
18*4882a593Smuzhiyun  * mechanisms that access memory).  The eieio instruction is a barrier
19*4882a593Smuzhiyun  * providing an ordering (separately) for (a) cacheable stores and (b)
20*4882a593Smuzhiyun  * loads and stores to non-cacheable memory (e.g. I/O devices).
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * mb() prevents loads and stores being reordered across this point.
23*4882a593Smuzhiyun  * rmb() prevents loads being reordered across this point.
24*4882a593Smuzhiyun  * wmb() prevents stores being reordered across this point.
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * *mb() variants without smp_ prefix must order all types of memory
27*4882a593Smuzhiyun  * operations with one another. sync is the only instruction sufficient
28*4882a593Smuzhiyun  * to do this.
29*4882a593Smuzhiyun  *
30*4882a593Smuzhiyun  * For the smp_ barriers, ordering is for cacheable memory operations
31*4882a593Smuzhiyun  * only. We have to use the sync instruction for smp_mb(), since lwsync
32*4882a593Smuzhiyun  * doesn't order loads with respect to previous stores.  Lwsync can be
33*4882a593Smuzhiyun  * used for smp_rmb() and smp_wmb().
34*4882a593Smuzhiyun  *
35*4882a593Smuzhiyun  * However, on CPUs that don't support lwsync, lwsync actually maps to a
36*4882a593Smuzhiyun  * heavy-weight sync, so smp_wmb() can be a lighter-weight eieio.
37*4882a593Smuzhiyun  */
38*4882a593Smuzhiyun #define mb()   __asm__ __volatile__ ("sync" : : : "memory")
39*4882a593Smuzhiyun #define rmb()  __asm__ __volatile__ ("sync" : : : "memory")
40*4882a593Smuzhiyun #define wmb()  __asm__ __volatile__ ("sync" : : : "memory")
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun /* The sub-arch has lwsync */
43*4882a593Smuzhiyun #if defined(__powerpc64__) || defined(CONFIG_PPC_E500MC)
44*4882a593Smuzhiyun #    define SMPWMB      LWSYNC
45*4882a593Smuzhiyun #else
46*4882a593Smuzhiyun #    define SMPWMB      eieio
47*4882a593Smuzhiyun #endif
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun /* clang defines this macro for a builtin, which will not work with runtime patching */
50*4882a593Smuzhiyun #undef __lwsync
51*4882a593Smuzhiyun #define __lwsync()	__asm__ __volatile__ (stringify_in_c(LWSYNC) : : :"memory")
52*4882a593Smuzhiyun #define dma_rmb()	__lwsync()
53*4882a593Smuzhiyun #define dma_wmb()	__asm__ __volatile__ (stringify_in_c(SMPWMB) : : :"memory")
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun #define __smp_lwsync()	__lwsync()
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun #define __smp_mb()	mb()
58*4882a593Smuzhiyun #define __smp_rmb()	__lwsync()
59*4882a593Smuzhiyun #define __smp_wmb()	__asm__ __volatile__ (stringify_in_c(SMPWMB) : : :"memory")
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun /*
62*4882a593Smuzhiyun  * This is a barrier which prevents following instructions from being
63*4882a593Smuzhiyun  * started until the value of the argument x is known.  For example, if
64*4882a593Smuzhiyun  * x is a variable loaded from memory, this prevents following
65*4882a593Smuzhiyun  * instructions from being executed until the load has been performed.
66*4882a593Smuzhiyun  */
67*4882a593Smuzhiyun #define data_barrier(x)	\
68*4882a593Smuzhiyun 	asm volatile("twi 0,%0,0; isync" : : "r" (x) : "memory");
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun #define __smp_store_release(p, v)						\
71*4882a593Smuzhiyun do {									\
72*4882a593Smuzhiyun 	compiletime_assert_atomic_type(*p);				\
73*4882a593Smuzhiyun 	__smp_lwsync();							\
74*4882a593Smuzhiyun 	WRITE_ONCE(*p, v);						\
75*4882a593Smuzhiyun } while (0)
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun #define __smp_load_acquire(p)						\
78*4882a593Smuzhiyun ({									\
79*4882a593Smuzhiyun 	typeof(*p) ___p1 = READ_ONCE(*p);				\
80*4882a593Smuzhiyun 	compiletime_assert_atomic_type(*p);				\
81*4882a593Smuzhiyun 	__smp_lwsync();							\
82*4882a593Smuzhiyun 	___p1;								\
83*4882a593Smuzhiyun })
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun #ifdef CONFIG_PPC64
86*4882a593Smuzhiyun #define smp_cond_load_relaxed(ptr, cond_expr) ({		\
87*4882a593Smuzhiyun 	typeof(ptr) __PTR = (ptr);				\
88*4882a593Smuzhiyun 	__unqual_scalar_typeof(*ptr) VAL;			\
89*4882a593Smuzhiyun 	VAL = READ_ONCE(*__PTR);				\
90*4882a593Smuzhiyun 	if (unlikely(!(cond_expr))) {				\
91*4882a593Smuzhiyun 		spin_begin();					\
92*4882a593Smuzhiyun 		do {						\
93*4882a593Smuzhiyun 			VAL = READ_ONCE(*__PTR);		\
94*4882a593Smuzhiyun 		} while (!(cond_expr));				\
95*4882a593Smuzhiyun 		spin_end();					\
96*4882a593Smuzhiyun 	}							\
97*4882a593Smuzhiyun 	(typeof(*ptr))VAL;					\
98*4882a593Smuzhiyun })
99*4882a593Smuzhiyun #endif
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun #ifdef CONFIG_PPC_BOOK3S_64
102*4882a593Smuzhiyun #define NOSPEC_BARRIER_SLOT   nop
103*4882a593Smuzhiyun #elif defined(CONFIG_PPC_FSL_BOOK3E)
104*4882a593Smuzhiyun #define NOSPEC_BARRIER_SLOT   nop; nop
105*4882a593Smuzhiyun #endif
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun #ifdef CONFIG_PPC_BARRIER_NOSPEC
108*4882a593Smuzhiyun /*
109*4882a593Smuzhiyun  * Prevent execution of subsequent instructions until preceding branches have
110*4882a593Smuzhiyun  * been fully resolved and are no longer executing speculatively.
111*4882a593Smuzhiyun  */
112*4882a593Smuzhiyun #define barrier_nospec_asm NOSPEC_BARRIER_FIXUP_SECTION; NOSPEC_BARRIER_SLOT
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun // This also acts as a compiler barrier due to the memory clobber.
115*4882a593Smuzhiyun #define barrier_nospec() asm (stringify_in_c(barrier_nospec_asm) ::: "memory")
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun #else /* !CONFIG_PPC_BARRIER_NOSPEC */
118*4882a593Smuzhiyun #define barrier_nospec_asm
119*4882a593Smuzhiyun #define barrier_nospec()
120*4882a593Smuzhiyun #endif /* CONFIG_PPC_BARRIER_NOSPEC */
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun /*
123*4882a593Smuzhiyun  * pmem_wmb() ensures that all stores for which the modification
124*4882a593Smuzhiyun  * are written to persistent storage by preceding dcbfps/dcbstps
125*4882a593Smuzhiyun  * instructions have updated persistent storage before any data
126*4882a593Smuzhiyun  * access or data transfer caused by subsequent instructions is
127*4882a593Smuzhiyun  * initiated.
128*4882a593Smuzhiyun  */
129*4882a593Smuzhiyun #define pmem_wmb() __asm__ __volatile__(PPC_PHWSYNC ::: "memory")
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun #include <asm-generic/barrier.h>
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun #endif /* _ASM_POWERPC_BARRIER_H */
134