cmpxchg.h 3.5 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
/*
 * cmpxchg.h -- forked from asm/atomic.h with this copyright:
 *
 * Copyright 2010 Tilera Corporation. All Rights Reserved.
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   as published by the Free Software Foundation, version 2.
 *
 *   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, GOOD TITLE or
 *   NON INFRINGEMENT.  See the GNU General Public License for
 *   more details.
 *
 */

#ifndef _ASM_TILE_CMPXCHG_H
#define _ASM_TILE_CMPXCHG_H

#ifndef __ASSEMBLY__

C
Chris Metcalf 已提交
23
#include <asm/barrier.h>
24

C
Chris Metcalf 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37
/* Nonexistent functions intended to cause compile errors. */
extern void __xchg_called_with_bad_pointer(void)
	__compiletime_error("Bad argument size for xchg");
extern void __cmpxchg_called_with_bad_pointer(void)
	__compiletime_error("Bad argument size for cmpxchg");

#ifndef __tilegx__

/* Note the _atomic_xxx() routines include a final mb(). */
int _atomic_xchg(int *ptr, int n);
int _atomic_xchg_add(int *v, int i);
int _atomic_xchg_add_unless(int *v, int a, int u);
int _atomic_cmpxchg(int *ptr, int o, int n);
38 39 40 41
long long _atomic64_xchg(long long *v, long long n);
long long _atomic64_xchg_add(long long *v, long long i);
long long _atomic64_xchg_add_unless(long long *v, long long a, long long u);
long long _atomic64_cmpxchg(long long *v, long long o, long long n);
C
Chris Metcalf 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55

#define xchg(ptr, n)							\
	({								\
		if (sizeof(*(ptr)) != 4)				\
			__xchg_called_with_bad_pointer();		\
		smp_mb();						\
		(typeof(*(ptr)))_atomic_xchg((int *)(ptr), (int)(n));	\
	})

#define cmpxchg(ptr, o, n)						\
	({								\
		if (sizeof(*(ptr)) != 4)				\
			__cmpxchg_called_with_bad_pointer();		\
		smp_mb();						\
56 57
		(typeof(*(ptr)))_atomic_cmpxchg((int *)ptr, (int)o,	\
						(int)n);		\
C
Chris Metcalf 已提交
58 59 60 61 62 63 64
	})

#define xchg64(ptr, n)							\
	({								\
		if (sizeof(*(ptr)) != 8)				\
			__xchg_called_with_bad_pointer();		\
		smp_mb();						\
65 66
		(typeof(*(ptr)))_atomic64_xchg((long long *)(ptr),	\
						(long long)(n));	\
C
Chris Metcalf 已提交
67 68 69 70 71 72 73
	})

#define cmpxchg64(ptr, o, n)						\
	({								\
		if (sizeof(*(ptr)) != 8)				\
			__cmpxchg_called_with_bad_pointer();		\
		smp_mb();						\
74 75
		(typeof(*(ptr)))_atomic64_cmpxchg((long long *)ptr,	\
					(long long)o, (long long)n);	\
C
Chris Metcalf 已提交
76 77 78 79 80
	})

#else

#define xchg(ptr, n)							\
81 82
	({								\
		typeof(*(ptr)) __x;					\
C
Chris Metcalf 已提交
83
		smp_mb();						\
84 85
		switch (sizeof(*(ptr))) {				\
		case 4:							\
C
Chris Metcalf 已提交
86
			__x = (typeof(__x))(unsigned long)		\
87 88
				__insn_exch4((ptr),			\
					(u32)(unsigned long)(n));	\
89 90
			break;						\
		case 8:							\
91
			__x = (typeof(__x))				\
C
Chris Metcalf 已提交
92
				__insn_exch((ptr), (unsigned long)(n));	\
93 94 95
			break;						\
		default:						\
			__xchg_called_with_bad_pointer();		\
C
Chris Metcalf 已提交
96
			break;						\
97
		}							\
C
Chris Metcalf 已提交
98
		smp_mb();						\
99 100 101 102 103 104
		__x;							\
	})

#define cmpxchg(ptr, o, n)						\
	({								\
		typeof(*(ptr)) __x;					\
C
Chris Metcalf 已提交
105 106
		__insn_mtspr(SPR_CMPEXCH_VALUE, (unsigned long)(o));	\
		smp_mb();						\
107 108
		switch (sizeof(*(ptr))) {				\
		case 4:							\
C
Chris Metcalf 已提交
109
			__x = (typeof(__x))(unsigned long)		\
110 111
				__insn_cmpexch4((ptr),			\
					(u32)(unsigned long)(n));	\
112 113
			break;						\
		case 8:							\
114 115
			__x = (typeof(__x))__insn_cmpexch((ptr),	\
						(long long)(n));	\
116 117 118
			break;						\
		default:						\
			__cmpxchg_called_with_bad_pointer();		\
C
Chris Metcalf 已提交
119
			break;						\
120
		}							\
C
Chris Metcalf 已提交
121
		smp_mb();						\
122 123 124
		__x;							\
	})

C
Chris Metcalf 已提交
125 126
#define xchg64 xchg
#define cmpxchg64 cmpxchg
127

C
Chris Metcalf 已提交
128 129
#endif

130 131 132
#endif /* __ASSEMBLY__ */

#endif /* _ASM_TILE_CMPXCHG_H */