2 * cmpxchg.h -- forked from asm/atomic.h with this copyright:
4 * Copyright 2010 Tilera Corporation. All Rights Reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13 * NON INFRINGEMENT. See the GNU General Public License for
18 #ifndef _ASM_TILE_CMPXCHG_H
19 #define _ASM_TILE_CMPXCHG_H
23 #include <asm/barrier.h>
25 /* Nonexistent functions intended to cause compile errors. */
26 extern void __xchg_called_with_bad_pointer(void)
27 __compiletime_error("Bad argument size for xchg");
28 extern void __cmpxchg_called_with_bad_pointer(void)
29 __compiletime_error("Bad argument size for cmpxchg");
33 /* Note the _atomic_xxx() routines include a final mb(). */
34 int _atomic_xchg(int *ptr, int n);
35 int _atomic_xchg_add(int *v, int i);
36 int _atomic_xchg_add_unless(int *v, int a, int u);
37 int _atomic_cmpxchg(int *ptr, int o, int n);
38 u64 _atomic64_xchg(u64 *v, u64 n);
39 u64 _atomic64_xchg_add(u64 *v, u64 i);
40 u64 _atomic64_xchg_add_unless(u64 *v, u64 a, u64 u);
41 u64 _atomic64_cmpxchg(u64 *v, u64 o, u64 n);
43 #define xchg(ptr, n) \
45 if (sizeof(*(ptr)) != 4) \
46 __xchg_called_with_bad_pointer(); \
48 (typeof(*(ptr)))_atomic_xchg((int *)(ptr), (int)(n)); \
51 #define cmpxchg(ptr, o, n) \
53 if (sizeof(*(ptr)) != 4) \
54 __cmpxchg_called_with_bad_pointer(); \
56 (typeof(*(ptr)))_atomic_cmpxchg((int *)ptr, (int)o, (int)n); \
59 #define xchg64(ptr, n) \
61 if (sizeof(*(ptr)) != 8) \
62 __xchg_called_with_bad_pointer(); \
64 (typeof(*(ptr)))_atomic64_xchg((u64 *)(ptr), (u64)(n)); \
67 #define cmpxchg64(ptr, o, n) \
69 if (sizeof(*(ptr)) != 8) \
70 __cmpxchg_called_with_bad_pointer(); \
72 (typeof(*(ptr)))_atomic64_cmpxchg((u64 *)ptr, (u64)o, (u64)n); \
77 #define xchg(ptr, n) \
81 switch (sizeof(*(ptr))) { \
83 __x = (typeof(__x))(unsigned long) \
84 __insn_exch4((ptr), (u32)(unsigned long)(n)); \
88 __insn_exch((ptr), (unsigned long)(n)); \
91 __xchg_called_with_bad_pointer(); \
98 #define cmpxchg(ptr, o, n) \
100 typeof(*(ptr)) __x; \
101 __insn_mtspr(SPR_CMPEXCH_VALUE, (unsigned long)(o)); \
103 switch (sizeof(*(ptr))) { \
105 __x = (typeof(__x))(unsigned long) \
106 __insn_cmpexch4((ptr), (u32)(unsigned long)(n)); \
109 __x = (typeof(__x))__insn_cmpexch((ptr), (u64)(n)); \
112 __cmpxchg_called_with_bad_pointer(); \
120 #define cmpxchg64 cmpxchg
124 #define tas(ptr) xchg((ptr), 1)
126 #endif /* __ASSEMBLY__ */
128 #endif /* _ASM_TILE_CMPXCHG_H */