xref: /openbmc/linux/arch/powerpc/include/asm/barrier.h (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2ae3a197eSDavid Howells /*
3ae3a197eSDavid Howells  * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu>
4ae3a197eSDavid Howells  */
5ae3a197eSDavid Howells #ifndef _ASM_POWERPC_BARRIER_H
6ae3a197eSDavid Howells #define _ASM_POWERPC_BARRIER_H
7ae3a197eSDavid Howells 
8ec0c464cSChristophe Leroy #include <asm/asm-const.h>
9ec0c464cSChristophe Leroy 
1076e6c73fSAneesh Kumar K.V #ifndef __ASSEMBLY__
1176e6c73fSAneesh Kumar K.V #include <asm/ppc-opcode.h>
1276e6c73fSAneesh Kumar K.V #endif
1376e6c73fSAneesh Kumar K.V 
14ae3a197eSDavid Howells /*
15ae3a197eSDavid Howells  * Memory barrier.
16ae3a197eSDavid Howells  * The sync instruction guarantees that all memory accesses initiated
17ae3a197eSDavid Howells  * by this processor have been performed (with respect to all other
18ae3a197eSDavid Howells  * mechanisms that access memory).  The eieio instruction is a barrier
19ae3a197eSDavid Howells  * providing an ordering (separately) for (a) cacheable stores and (b)
20ae3a197eSDavid Howells  * loads and stores to non-cacheable memory (e.g. I/O devices).
21ae3a197eSDavid Howells  *
22ae3a197eSDavid Howells  * mb() prevents loads and stores being reordered across this point.
23ae3a197eSDavid Howells  * rmb() prevents loads being reordered across this point.
24ae3a197eSDavid Howells  * wmb() prevents stores being reordered across this point.
25ae3a197eSDavid Howells  *
26ae3a197eSDavid Howells  * *mb() variants without smp_ prefix must order all types of memory
27ae3a197eSDavid Howells  * operations with one another. sync is the only instruction sufficient
28ae3a197eSDavid Howells  * to do this.
29ae3a197eSDavid Howells  *
30ae3a197eSDavid Howells  * For the smp_ barriers, ordering is for cacheable memory operations
31ae3a197eSDavid Howells  * only. We have to use the sync instruction for smp_mb(), since lwsync
32ae3a197eSDavid Howells  * doesn't order loads with respect to previous stores.  Lwsync can be
33ae3a197eSDavid Howells  * used for smp_rmb() and smp_wmb().
34ae3a197eSDavid Howells  *
35ae3a197eSDavid Howells  * However, on CPUs that don't support lwsync, lwsync actually maps to a
36ae3a197eSDavid Howells  * heavy-weight sync, so smp_wmb() can be a lighter-weight eieio.
37ae3a197eSDavid Howells  */
38*b6e25929SRohan McLure #define __mb()   __asm__ __volatile__ ("sync" : : : "memory")
39*b6e25929SRohan McLure #define __rmb()  __asm__ __volatile__ ("sync" : : : "memory")
40*b6e25929SRohan McLure #define __wmb()  __asm__ __volatile__ ("sync" : : : "memory")
41ae3a197eSDavid Howells 
420bfdf598SNicholas Piggin /* The sub-arch has lwsync */
431f1676bbSMichael Ellerman #if defined(CONFIG_PPC64) || defined(CONFIG_PPC_E500MC)
44ae3a197eSDavid Howells #    define SMPWMB      LWSYNC
452255411dSChristophe Leroy #elif defined(CONFIG_BOOKE)
462255411dSChristophe Leroy #    define SMPWMB      mbar
47ae3a197eSDavid Howells #else
48ae3a197eSDavid Howells #    define SMPWMB      eieio
49ae3a197eSDavid Howells #endif
50ae3a197eSDavid Howells 
51015d9814SNathan Chancellor /* clang defines this macro for a builtin, which will not work with runtime patching */
52015d9814SNathan Chancellor #undef __lwsync
5347933ad4SPeter Zijlstra #define __lwsync()	__asm__ __volatile__ (stringify_in_c(LWSYNC) : : :"memory")
54*b6e25929SRohan McLure #define __dma_rmb()	__lwsync()
55*b6e25929SRohan McLure #define __dma_wmb()	__asm__ __volatile__ (stringify_in_c(SMPWMB) : : :"memory")
561077fa36SAlexander Duyck 
57003472a9SMichael S. Tsirkin #define __smp_lwsync()	__lwsync()
5847933ad4SPeter Zijlstra 
59*b6e25929SRohan McLure #define __smp_mb()	__mb()
60003472a9SMichael S. Tsirkin #define __smp_rmb()	__lwsync()
61003472a9SMichael S. Tsirkin #define __smp_wmb()	__asm__ __volatile__ (stringify_in_c(SMPWMB) : : :"memory")
62ae3a197eSDavid Howells 
63ae3a197eSDavid Howells /*
64ae3a197eSDavid Howells  * This is a barrier which prevents following instructions from being
65ae3a197eSDavid Howells  * started until the value of the argument x is known.  For example, if
66ae3a197eSDavid Howells  * x is a variable loaded from memory, this prevents following
67ae3a197eSDavid Howells  * instructions from being executed until the load has been performed.
68ae3a197eSDavid Howells  */
69ae3a197eSDavid Howells #define data_barrier(x)	\
70ae3a197eSDavid Howells 	asm volatile("twi 0,%0,0; isync" : : "r" (x) : "memory");
71ae3a197eSDavid Howells 
72003472a9SMichael S. Tsirkin #define __smp_store_release(p, v)						\
7347933ad4SPeter Zijlstra do {									\
7447933ad4SPeter Zijlstra 	compiletime_assert_atomic_type(*p);				\
75003472a9SMichael S. Tsirkin 	__smp_lwsync();							\
7676695af2SAndrey Konovalov 	WRITE_ONCE(*p, v);						\
7747933ad4SPeter Zijlstra } while (0)
7847933ad4SPeter Zijlstra 
79003472a9SMichael S. Tsirkin #define __smp_load_acquire(p)						\
8047933ad4SPeter Zijlstra ({									\
8176695af2SAndrey Konovalov 	typeof(*p) ___p1 = READ_ONCE(*p);				\
8247933ad4SPeter Zijlstra 	compiletime_assert_atomic_type(*p);				\
83003472a9SMichael S. Tsirkin 	__smp_lwsync();							\
8447933ad4SPeter Zijlstra 	___p1;								\
8547933ad4SPeter Zijlstra })
8647933ad4SPeter Zijlstra 
87ebcd1bfcSDiana Craciun #ifdef CONFIG_PPC_BOOK3S_64
88ebcd1bfcSDiana Craciun #define NOSPEC_BARRIER_SLOT   nop
893e731858SChristophe Leroy #elif defined(CONFIG_PPC_E500)
90ebcd1bfcSDiana Craciun #define NOSPEC_BARRIER_SLOT   nop; nop
91ebcd1bfcSDiana Craciun #endif
92ebcd1bfcSDiana Craciun 
93179ab1cbSMichael Ellerman #ifdef CONFIG_PPC_BARRIER_NOSPEC
94a6b3964aSMichal Suchanek /*
95a6b3964aSMichal Suchanek  * Prevent execution of subsequent instructions until preceding branches have
96a6b3964aSMichal Suchanek  * been fully resolved and are no longer executing speculatively.
97a6b3964aSMichal Suchanek  */
98ebcd1bfcSDiana Craciun #define barrier_nospec_asm NOSPEC_BARRIER_FIXUP_SECTION; NOSPEC_BARRIER_SLOT
99a6b3964aSMichal Suchanek 
100a6b3964aSMichal Suchanek // This also acts as a compiler barrier due to the memory clobber.
101a6b3964aSMichal Suchanek #define barrier_nospec() asm (stringify_in_c(barrier_nospec_asm) ::: "memory")
102a6b3964aSMichal Suchanek 
103179ab1cbSMichael Ellerman #else /* !CONFIG_PPC_BARRIER_NOSPEC */
104a6b3964aSMichal Suchanek #define barrier_nospec_asm
105a6b3964aSMichal Suchanek #define barrier_nospec()
106179ab1cbSMichael Ellerman #endif /* CONFIG_PPC_BARRIER_NOSPEC */
107a6b3964aSMichal Suchanek 
10876e6c73fSAneesh Kumar K.V /*
10976e6c73fSAneesh Kumar K.V  * pmem_wmb() ensures that all stores for which the modification
11076e6c73fSAneesh Kumar K.V  * are written to persistent storage by preceding dcbfps/dcbstps
11176e6c73fSAneesh Kumar K.V  * instructions have updated persistent storage before any data
11276e6c73fSAneesh Kumar K.V  * access or data transfer caused by subsequent instructions is
11376e6c73fSAneesh Kumar K.V  * initiated.
11476e6c73fSAneesh Kumar K.V  */
11576e6c73fSAneesh Kumar K.V #define pmem_wmb() __asm__ __volatile__(PPC_PHWSYNC ::: "memory")
11676e6c73fSAneesh Kumar K.V 
117fbd7ec02SMichael S. Tsirkin #include <asm-generic/barrier.h>
118fbd7ec02SMichael S. Tsirkin 
119ae3a197eSDavid Howells #endif /* _ASM_POWERPC_BARRIER_H */
120