blob: f67b3f6e36bebf85b69f72a913b9800daebbe3c4 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
David Howellsae3a1972012-03-28 18:30:02 +01002/*
3 * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu>
4 */
5#ifndef _ASM_POWERPC_BARRIER_H
6#define _ASM_POWERPC_BARRIER_H
7
8/*
9 * Memory barrier.
10 * The sync instruction guarantees that all memory accesses initiated
11 * by this processor have been performed (with respect to all other
12 * mechanisms that access memory). The eieio instruction is a barrier
13 * providing an ordering (separately) for (a) cacheable stores and (b)
14 * loads and stores to non-cacheable memory (e.g. I/O devices).
15 *
16 * mb() prevents loads and stores being reordered across this point.
17 * rmb() prevents loads being reordered across this point.
18 * wmb() prevents stores being reordered across this point.
19 * read_barrier_depends() prevents data-dependent loads being reordered
20 * across this point (nop on PPC).
21 *
22 * *mb() variants without smp_ prefix must order all types of memory
23 * operations with one another. sync is the only instruction sufficient
24 * to do this.
25 *
26 * For the smp_ barriers, ordering is for cacheable memory operations
27 * only. We have to use the sync instruction for smp_mb(), since lwsync
28 * doesn't order loads with respect to previous stores. Lwsync can be
29 * used for smp_rmb() and smp_wmb().
30 *
31 * However, on CPUs that don't support lwsync, lwsync actually maps to a
32 * heavy-weight sync, so smp_wmb() can be a lighter-weight eieio.
33 */
34#define mb() __asm__ __volatile__ ("sync" : : : "memory")
35#define rmb() __asm__ __volatile__ ("sync" : : : "memory")
36#define wmb() __asm__ __volatile__ ("sync" : : : "memory")
David Howellsae3a1972012-03-28 18:30:02 +010037
Nicholas Piggin0bfdf592018-03-22 20:41:46 +100038/* The sub-arch has lwsync */
39#if defined(__powerpc64__) || defined(CONFIG_PPC_E500MC)
David Howellsae3a1972012-03-28 18:30:02 +010040# define SMPWMB LWSYNC
41#else
42# define SMPWMB eieio
43#endif
44
Peter Zijlstra47933ad2013-11-06 14:57:36 +010045#define __lwsync() __asm__ __volatile__ (stringify_in_c(LWSYNC) : : :"memory")
Alexander Duyck1077fa32014-12-11 15:02:06 -080046#define dma_rmb() __lwsync()
47#define dma_wmb() __asm__ __volatile__ (stringify_in_c(SMPWMB) : : :"memory")
48
Michael S. Tsirkin003472a2015-12-27 15:04:42 +020049#define __smp_lwsync() __lwsync()
Peter Zijlstra47933ad2013-11-06 14:57:36 +010050
Michael S. Tsirkin003472a2015-12-27 15:04:42 +020051#define __smp_mb() mb()
52#define __smp_rmb() __lwsync()
53#define __smp_wmb() __asm__ __volatile__ (stringify_in_c(SMPWMB) : : :"memory")
David Howellsae3a1972012-03-28 18:30:02 +010054
55/*
56 * This is a barrier which prevents following instructions from being
57 * started until the value of the argument x is known. For example, if
58 * x is a variable loaded from memory, this prevents following
59 * instructions from being executed until the load has been performed.
60 */
61#define data_barrier(x) \
62 asm volatile("twi 0,%0,0; isync" : : "r" (x) : "memory");
63
Michael S. Tsirkin003472a2015-12-27 15:04:42 +020064#define __smp_store_release(p, v) \
Peter Zijlstra47933ad2013-11-06 14:57:36 +010065do { \
66 compiletime_assert_atomic_type(*p); \
Michael S. Tsirkin003472a2015-12-27 15:04:42 +020067 __smp_lwsync(); \
Andrey Konovalov76695af2015-08-02 17:11:04 +020068 WRITE_ONCE(*p, v); \
Peter Zijlstra47933ad2013-11-06 14:57:36 +010069} while (0)
70
Michael S. Tsirkin003472a2015-12-27 15:04:42 +020071#define __smp_load_acquire(p) \
Peter Zijlstra47933ad2013-11-06 14:57:36 +010072({ \
Andrey Konovalov76695af2015-08-02 17:11:04 +020073 typeof(*p) ___p1 = READ_ONCE(*p); \
Peter Zijlstra47933ad2013-11-06 14:57:36 +010074 compiletime_assert_atomic_type(*p); \
Michael S. Tsirkin003472a2015-12-27 15:04:42 +020075 __smp_lwsync(); \
Peter Zijlstra47933ad2013-11-06 14:57:36 +010076 ___p1; \
77})
78
Michal Suchaneka6b39642018-04-24 14:15:54 +100079#ifdef CONFIG_PPC_BOOK3S_64
80/*
81 * Prevent execution of subsequent instructions until preceding branches have
82 * been fully resolved and are no longer executing speculatively.
83 */
Michal Suchanek2eea7f02018-04-24 14:15:55 +100084#define barrier_nospec_asm NOSPEC_BARRIER_FIXUP_SECTION; nop
Michal Suchaneka6b39642018-04-24 14:15:54 +100085
86// This also acts as a compiler barrier due to the memory clobber.
87#define barrier_nospec() asm (stringify_in_c(barrier_nospec_asm) ::: "memory")
88
89#else /* !CONFIG_PPC_BOOK3S_64 */
90#define barrier_nospec_asm
91#define barrier_nospec()
92#endif
93
Michael S. Tsirkinfbd7ec02015-12-21 09:22:18 +020094#include <asm-generic/barrier.h>
95
David Howellsae3a1972012-03-28 18:30:02 +010096#endif /* _ASM_POWERPC_BARRIER_H */