GNU Linux-libre 4.4.288-gnu1
[releases.git] / arch / powerpc / include / asm / barrier.h
1 /*
2  * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu>
3  */
4 #ifndef _ASM_POWERPC_BARRIER_H
5 #define _ASM_POWERPC_BARRIER_H
6
7 /*
8  * Memory barrier.
9  * The sync instruction guarantees that all memory accesses initiated
10  * by this processor have been performed (with respect to all other
11  * mechanisms that access memory).  The eieio instruction is a barrier
12  * providing an ordering (separately) for (a) cacheable stores and (b)
13  * loads and stores to non-cacheable memory (e.g. I/O devices).
14  *
15  * mb() prevents loads and stores being reordered across this point.
16  * rmb() prevents loads being reordered across this point.
17  * wmb() prevents stores being reordered across this point.
18  * read_barrier_depends() prevents data-dependent loads being reordered
19  *      across this point (nop on PPC).
20  *
21  * *mb() variants without smp_ prefix must order all types of memory
22  * operations with one another. sync is the only instruction sufficient
23  * to do this.
24  *
25  * For the smp_ barriers, ordering is for cacheable memory operations
26  * only. We have to use the sync instruction for smp_mb(), since lwsync
27  * doesn't order loads with respect to previous stores.  Lwsync can be
28  * used for smp_rmb() and smp_wmb().
29  *
30  * However, on CPUs that don't support lwsync, lwsync actually maps to a
31  * heavy-weight sync, so smp_wmb() can be a lighter-weight eieio.
32  */
33 #define mb()   __asm__ __volatile__ ("sync" : : : "memory")
34 #define rmb()  __asm__ __volatile__ ("sync" : : : "memory")
35 #define wmb()  __asm__ __volatile__ ("sync" : : : "memory")
36
37 #define smp_store_mb(var, value)        do { WRITE_ONCE(var, value); mb(); } while (0)
38
39 /* The sub-arch has lwsync */
40 #if defined(__powerpc64__) || defined(CONFIG_PPC_E500MC)
41 #    define SMPWMB      LWSYNC
42 #else
43 #    define SMPWMB      eieio
44 #endif
45
46 /* clang defines this macro for a builtin, which will not work with runtime patching */
47 #undef __lwsync
48 #define __lwsync()      __asm__ __volatile__ (stringify_in_c(LWSYNC) : : :"memory")
49 #define dma_rmb()       __lwsync()
50 #define dma_wmb()       __asm__ __volatile__ (stringify_in_c(SMPWMB) : : :"memory")
51
52 #ifdef CONFIG_SMP
53 #define smp_lwsync()    __lwsync()
54
55 #define smp_mb()        mb()
56 #define smp_rmb()       __lwsync()
57 #define smp_wmb()       __asm__ __volatile__ (stringify_in_c(SMPWMB) : : :"memory")
58 #else
59 #define smp_lwsync()    barrier()
60
61 #define smp_mb()        barrier()
62 #define smp_rmb()       barrier()
63 #define smp_wmb()       barrier()
64 #endif /* CONFIG_SMP */
65
66 #define read_barrier_depends()          do { } while (0)
67 #define smp_read_barrier_depends()      do { } while (0)
68
69 /*
70  * This is a barrier which prevents following instructions from being
71  * started until the value of the argument x is known.  For example, if
72  * x is a variable loaded from memory, this prevents following
73  * instructions from being executed until the load has been performed.
74  */
75 #define data_barrier(x) \
76         asm volatile("twi 0,%0,0; isync" : : "r" (x) : "memory");
77
78 #define smp_store_release(p, v)                                         \
79 do {                                                                    \
80         compiletime_assert_atomic_type(*p);                             \
81         smp_lwsync();                                                   \
82         WRITE_ONCE(*p, v);                                              \
83 } while (0)
84
85 #define smp_load_acquire(p)                                             \
86 ({                                                                      \
87         typeof(*p) ___p1 = READ_ONCE(*p);                               \
88         compiletime_assert_atomic_type(*p);                             \
89         smp_lwsync();                                                   \
90         ___p1;                                                          \
91 })
92
93 #define smp_mb__before_atomic()     smp_mb()
94 #define smp_mb__after_atomic()      smp_mb()
95 #define smp_mb__before_spinlock()   smp_mb()
96
97 #ifdef CONFIG_PPC_BOOK3S_64
98 #define NOSPEC_BARRIER_SLOT   nop
99 #elif defined(CONFIG_PPC_FSL_BOOK3E)
100 #define NOSPEC_BARRIER_SLOT   nop; nop
101 #endif
102
103 #ifdef CONFIG_PPC_BARRIER_NOSPEC
104 /*
105  * Prevent execution of subsequent instructions until preceding branches have
106  * been fully resolved and are no longer executing speculatively.
107  */
108 #define barrier_nospec_asm NOSPEC_BARRIER_FIXUP_SECTION; NOSPEC_BARRIER_SLOT
109
110 // This also acts as a compiler barrier due to the memory clobber.
111 #define barrier_nospec() asm (stringify_in_c(barrier_nospec_asm) ::: "memory")
112
113 #else /* !CONFIG_PPC_BARRIER_NOSPEC */
114 #define barrier_nospec_asm
115 #define barrier_nospec()
116 #endif /* CONFIG_PPC_BARRIER_NOSPEC */
117
118 #endif /* _ASM_POWERPC_BARRIER_H */