barrier.h 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Based on arch/arm/include/asm/barrier.h
 *
 * Copyright (C) 2012 ARM Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef __ASM_BARRIER_H
#define __ASM_BARRIER_H

#ifndef __ASSEMBLY__

23 24 25
#define __nops(n)	".rept	" #n "\nnop\n.endr\n"
#define nops(n)		asm volatile(__nops(n))

26 27 28 29 30
#define sev()		asm volatile("sev" : : : "memory")
#define wfe()		asm volatile("wfe" : : : "memory")
#define wfi()		asm volatile("wfi" : : : "memory")

#define isb()		asm volatile("isb" : : : "memory")
31 32
#define dmb(opt)	asm volatile("dmb " #opt : : : "memory")
#define dsb(opt)	asm volatile("dsb " #opt : : : "memory")
33

34
#define psb_csync()	asm volatile("hint #17" : : : "memory")
35
#define csdb()		asm volatile("hint #20" : : : "memory")
36

37 38 39 40 41 42 43 44 45 46 47 48
#ifdef CONFIG_ARM64_PSEUDO_NMI
#define pmr_sync()						\
	do {							\
		extern struct static_key_false gic_pmr_sync;	\
								\
		if (static_branch_unlikely(&gic_pmr_sync))	\
			dsb(sy);				\
	} while (0)
#else
#define pmr_sync()	do {} while (0)
#endif

49
#define mb()		dsb(sy)
50 51
#define rmb()		dsb(ld)
#define wmb()		dsb(st)
52

53 54 55
#define dma_rmb()	dmb(oshld)
#define dma_wmb()	dmb(oshst)

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
/*
 * Generate a mask for array_index__nospec() that is ~0UL when 0 <= idx < sz
 * and 0 otherwise.
 */
#define array_index_mask_nospec array_index_mask_nospec
static inline unsigned long array_index_mask_nospec(unsigned long idx,
						    unsigned long sz)
{
	unsigned long mask;

	asm volatile(
	"	cmp	%1, %2\n"
	"	sbc	%0, xzr, xzr\n"
	: "=r" (mask)
	: "r" (idx), "Ir" (sz)
	: "cc");

	csdb();
	return mask;
}

M
Michael S. Tsirkin 已提交
77 78 79
#define __smp_mb()	dmb(ish)
#define __smp_rmb()	dmb(ishld)
#define __smp_wmb()	dmb(ishst)
80

81
#define __smp_store_release(p, v)					\
82
do {									\
83 84
	union { typeof(*p) __val; char __c[1]; } __u =			\
		{ .__val = (__force typeof(*p)) (v) }; 			\
85 86
	compiletime_assert_atomic_type(*p);				\
	switch (sizeof(*p)) {						\
87 88
	case 1:								\
		asm volatile ("stlrb %w1, %0"				\
89 90 91
				: "=Q" (*p)				\
				: "r" (*(__u8 *)__u.__c)		\
				: "memory");				\
92 93 94
		break;							\
	case 2:								\
		asm volatile ("stlrh %w1, %0"				\
95 96 97
				: "=Q" (*p)				\
				: "r" (*(__u16 *)__u.__c)		\
				: "memory");				\
98
		break;							\
99 100
	case 4:								\
		asm volatile ("stlr %w1, %0"				\
101 102 103
				: "=Q" (*p)				\
				: "r" (*(__u32 *)__u.__c)		\
				: "memory");				\
104 105 106
		break;							\
	case 8:								\
		asm volatile ("stlr %1, %0"				\
107 108 109
				: "=Q" (*p)				\
				: "r" (*(__u64 *)__u.__c)		\
				: "memory");				\
110 111 112 113
		break;							\
	}								\
} while (0)

M
Michael S. Tsirkin 已提交
114
#define __smp_load_acquire(p)						\
115
({									\
116
	union { typeof(*p) __val; char __c[1]; } __u;			\
117 118
	compiletime_assert_atomic_type(*p);				\
	switch (sizeof(*p)) {						\
119 120
	case 1:								\
		asm volatile ("ldarb %w0, %1"				\
121 122
			: "=r" (*(__u8 *)__u.__c)			\
			: "Q" (*p) : "memory");				\
123 124 125
		break;							\
	case 2:								\
		asm volatile ("ldarh %w0, %1"				\
126 127
			: "=r" (*(__u16 *)__u.__c)			\
			: "Q" (*p) : "memory");				\
128
		break;							\
129 130
	case 4:								\
		asm volatile ("ldar %w0, %1"				\
131 132
			: "=r" (*(__u32 *)__u.__c)			\
			: "Q" (*p) : "memory");				\
133 134 135
		break;							\
	case 8:								\
		asm volatile ("ldar %0, %1"				\
136 137
			: "=r" (*(__u64 *)__u.__c)			\
			: "Q" (*p) : "memory");				\
138 139
		break;							\
	}								\
140
	__u.__val;							\
141 142
})

143 144 145 146 147 148 149 150 151 152 153 154 155
#define smp_cond_load_relaxed(ptr, cond_expr)				\
({									\
	typeof(ptr) __PTR = (ptr);					\
	typeof(*ptr) VAL;						\
	for (;;) {							\
		VAL = READ_ONCE(*__PTR);				\
		if (cond_expr)						\
			break;						\
		__cmpwait_relaxed(__PTR, VAL);				\
	}								\
	VAL;								\
})

156 157 158 159 160 161 162 163 164 165 166 167 168
#define smp_cond_load_acquire(ptr, cond_expr)				\
({									\
	typeof(ptr) __PTR = (ptr);					\
	typeof(*ptr) VAL;						\
	for (;;) {							\
		VAL = smp_load_acquire(__PTR);				\
		if (cond_expr)						\
			break;						\
		__cmpwait_relaxed(__PTR, VAL);				\
	}								\
	VAL;								\
})

169
#include <asm-generic/barrier.h>
P
Peter Zijlstra 已提交
170

171 172 173
#endif	/* __ASSEMBLY__ */

#endif	/* __ASM_BARRIER_H */