H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 1 | #ifndef _ASM_X86_CMPXCHG_64_H |
| 2 | #define _ASM_X86_CMPXCHG_64_H |
Jeff Dike | a436ed9 | 2007-05-08 00:35:02 -0700 | [diff] [blame] | 3 | |
| 4 | #include <asm/alternative.h> /* Provides LOCK_PREFIX */ |
| 5 | |
Jeff Dike | a436ed9 | 2007-05-08 00:35:02 -0700 | [diff] [blame] | 6 | #define __xg(x) ((volatile long *)(x)) |
| 7 | |
H. Peter Anvin | 69309a0 | 2010-07-27 23:29:52 -0700 | [diff] [blame^] | 8 | static inline void set_64bit(volatile u64 *ptr, u64 val) |
Jeff Dike | a436ed9 | 2007-05-08 00:35:02 -0700 | [diff] [blame] | 9 | { |
| 10 | *ptr = val; |
| 11 | } |
| 12 | |
Peter Zijlstra | f3834b9 | 2009-10-09 10:12:46 +0200 | [diff] [blame] | 13 | extern void __xchg_wrong_size(void); |
| 14 | extern void __cmpxchg_wrong_size(void); |
| 15 | |
Jeff Dike | a436ed9 | 2007-05-08 00:35:02 -0700 | [diff] [blame] | 16 | /* |
| 17 | * Note: no "lock" prefix even on SMP: xchg always implies lock anyway |
| 18 | * Note 2: xchg has side effect, so that attribute volatile is necessary, |
| 19 | * but generally the primitive is invalid, *ptr is output argument. --ANK |
| 20 | */ |
Peter Zijlstra | f3834b9 | 2009-10-09 10:12:46 +0200 | [diff] [blame] | 21 | #define __xchg(x, ptr, size) \ |
| 22 | ({ \ |
| 23 | __typeof(*(ptr)) __x = (x); \ |
| 24 | switch (size) { \ |
| 25 | case 1: \ |
| 26 | asm volatile("xchgb %b0,%1" \ |
H. Peter Anvin | 113fc5a | 2010-07-27 17:01:49 -0700 | [diff] [blame] | 27 | : "=q" (__x), "+m" (*__xg(ptr)) \ |
| 28 | : "0" (__x) \ |
Peter Zijlstra | f3834b9 | 2009-10-09 10:12:46 +0200 | [diff] [blame] | 29 | : "memory"); \ |
| 30 | break; \ |
| 31 | case 2: \ |
| 32 | asm volatile("xchgw %w0,%1" \ |
H. Peter Anvin | 113fc5a | 2010-07-27 17:01:49 -0700 | [diff] [blame] | 33 | : "=r" (__x), "+m" (*__xg(ptr)) \ |
| 34 | : "0" (__x) \ |
Peter Zijlstra | f3834b9 | 2009-10-09 10:12:46 +0200 | [diff] [blame] | 35 | : "memory"); \ |
| 36 | break; \ |
| 37 | case 4: \ |
| 38 | asm volatile("xchgl %k0,%1" \ |
H. Peter Anvin | 113fc5a | 2010-07-27 17:01:49 -0700 | [diff] [blame] | 39 | : "=r" (__x), "+m" (*__xg(ptr)) \ |
| 40 | : "0" (__x) \ |
Peter Zijlstra | f3834b9 | 2009-10-09 10:12:46 +0200 | [diff] [blame] | 41 | : "memory"); \ |
| 42 | break; \ |
| 43 | case 8: \ |
| 44 | asm volatile("xchgq %0,%1" \ |
H. Peter Anvin | 113fc5a | 2010-07-27 17:01:49 -0700 | [diff] [blame] | 45 | : "=r" (__x), "+m" (*__xg(ptr)) \ |
| 46 | : "0" (__x) \ |
Peter Zijlstra | f3834b9 | 2009-10-09 10:12:46 +0200 | [diff] [blame] | 47 | : "memory"); \ |
| 48 | break; \ |
| 49 | default: \ |
| 50 | __xchg_wrong_size(); \ |
| 51 | } \ |
| 52 | __x; \ |
| 53 | }) |
| 54 | |
| 55 | #define xchg(ptr, v) \ |
| 56 | __xchg((v), (ptr), sizeof(*ptr)) |
| 57 | |
| 58 | #define __HAVE_ARCH_CMPXCHG 1 |
Jeff Dike | a436ed9 | 2007-05-08 00:35:02 -0700 | [diff] [blame] | 59 | |
| 60 | /* |
| 61 | * Atomic compare and exchange. Compare OLD with MEM, if identical, |
| 62 | * store NEW in MEM. Return the initial value in MEM. Success is |
| 63 | * indicated by comparing RETURN with OLD. |
| 64 | */ |
Peter Zijlstra | f3834b9 | 2009-10-09 10:12:46 +0200 | [diff] [blame] | 65 | #define __raw_cmpxchg(ptr, old, new, size, lock) \ |
| 66 | ({ \ |
| 67 | __typeof__(*(ptr)) __ret; \ |
| 68 | __typeof__(*(ptr)) __old = (old); \ |
| 69 | __typeof__(*(ptr)) __new = (new); \ |
| 70 | switch (size) { \ |
| 71 | case 1: \ |
H. Peter Anvin | 113fc5a | 2010-07-27 17:01:49 -0700 | [diff] [blame] | 72 | asm volatile(lock "cmpxchgb %b2,%1" \ |
| 73 | : "=a" (__ret), "+m" (*__xg(ptr)) \ |
| 74 | : "q" (__new), "0" (__old) \ |
Peter Zijlstra | f3834b9 | 2009-10-09 10:12:46 +0200 | [diff] [blame] | 75 | : "memory"); \ |
| 76 | break; \ |
| 77 | case 2: \ |
H. Peter Anvin | 113fc5a | 2010-07-27 17:01:49 -0700 | [diff] [blame] | 78 | asm volatile(lock "cmpxchgw %w2,%1" \ |
| 79 | : "=a" (__ret), "+m" (*__xg(ptr)) \ |
| 80 | : "r" (__new), "0" (__old) \ |
Peter Zijlstra | f3834b9 | 2009-10-09 10:12:46 +0200 | [diff] [blame] | 81 | : "memory"); \ |
| 82 | break; \ |
| 83 | case 4: \ |
H. Peter Anvin | 113fc5a | 2010-07-27 17:01:49 -0700 | [diff] [blame] | 84 | asm volatile(lock "cmpxchgl %k2,%1" \ |
| 85 | : "=a" (__ret), "+m" (*__xg(ptr)) \ |
| 86 | : "r" (__new), "0" (__old) \ |
Peter Zijlstra | f3834b9 | 2009-10-09 10:12:46 +0200 | [diff] [blame] | 87 | : "memory"); \ |
| 88 | break; \ |
| 89 | case 8: \ |
H. Peter Anvin | 113fc5a | 2010-07-27 17:01:49 -0700 | [diff] [blame] | 90 | asm volatile(lock "cmpxchgq %2,%1" \ |
| 91 | : "=a" (__ret), "+m" (*__xg(ptr)) \ |
| 92 | : "r" (__new), "0" (__old) \ |
Peter Zijlstra | f3834b9 | 2009-10-09 10:12:46 +0200 | [diff] [blame] | 93 | : "memory"); \ |
| 94 | break; \ |
| 95 | default: \ |
| 96 | __cmpxchg_wrong_size(); \ |
| 97 | } \ |
| 98 | __ret; \ |
| 99 | }) |
Jeff Dike | a436ed9 | 2007-05-08 00:35:02 -0700 | [diff] [blame] | 100 | |
Peter Zijlstra | f3834b9 | 2009-10-09 10:12:46 +0200 | [diff] [blame] | 101 | #define __cmpxchg(ptr, old, new, size) \ |
| 102 | __raw_cmpxchg((ptr), (old), (new), (size), LOCK_PREFIX) |
Jeff Dike | a436ed9 | 2007-05-08 00:35:02 -0700 | [diff] [blame] | 103 | |
Peter Zijlstra | f3834b9 | 2009-10-09 10:12:46 +0200 | [diff] [blame] | 104 | #define __sync_cmpxchg(ptr, old, new, size) \ |
| 105 | __raw_cmpxchg((ptr), (old), (new), (size), "lock; ") |
Jeff Dike | a436ed9 | 2007-05-08 00:35:02 -0700 | [diff] [blame] | 106 | |
Peter Zijlstra | f3834b9 | 2009-10-09 10:12:46 +0200 | [diff] [blame] | 107 | #define __cmpxchg_local(ptr, old, new, size) \ |
| 108 | __raw_cmpxchg((ptr), (old), (new), (size), "") |
Jeremy Fitzhardinge | 15878c0 | 2008-06-25 00:19:10 -0400 | [diff] [blame] | 109 | |
Peter Zijlstra | f3834b9 | 2009-10-09 10:12:46 +0200 | [diff] [blame] | 110 | #define cmpxchg(ptr, old, new) \ |
| 111 | __cmpxchg((ptr), (old), (new), sizeof(*ptr)) |
Jeff Dike | a436ed9 | 2007-05-08 00:35:02 -0700 | [diff] [blame] | 112 | |
Peter Zijlstra | f3834b9 | 2009-10-09 10:12:46 +0200 | [diff] [blame] | 113 | #define sync_cmpxchg(ptr, old, new) \ |
| 114 | __sync_cmpxchg((ptr), (old), (new), sizeof(*ptr)) |
| 115 | |
| 116 | #define cmpxchg_local(ptr, old, new) \ |
| 117 | __cmpxchg_local((ptr), (old), (new), sizeof(*ptr)) |
| 118 | |
Mathieu Desnoyers | 32f49eab | 2008-02-07 00:16:10 -0800 | [diff] [blame] | 119 | #define cmpxchg64(ptr, o, n) \ |
Joe Perches | e52da35 | 2008-03-23 01:01:52 -0700 | [diff] [blame] | 120 | ({ \ |
Mathieu Desnoyers | 32f49eab | 2008-02-07 00:16:10 -0800 | [diff] [blame] | 121 | BUILD_BUG_ON(sizeof(*(ptr)) != 8); \ |
| 122 | cmpxchg((ptr), (o), (n)); \ |
Joe Perches | e52da35 | 2008-03-23 01:01:52 -0700 | [diff] [blame] | 123 | }) |
Peter Zijlstra | f3834b9 | 2009-10-09 10:12:46 +0200 | [diff] [blame] | 124 | |
Mathieu Desnoyers | 32f49eab | 2008-02-07 00:16:10 -0800 | [diff] [blame] | 125 | #define cmpxchg64_local(ptr, o, n) \ |
Joe Perches | e52da35 | 2008-03-23 01:01:52 -0700 | [diff] [blame] | 126 | ({ \ |
Mathieu Desnoyers | 32f49eab | 2008-02-07 00:16:10 -0800 | [diff] [blame] | 127 | BUILD_BUG_ON(sizeof(*(ptr)) != 8); \ |
| 128 | cmpxchg_local((ptr), (o), (n)); \ |
Joe Perches | e52da35 | 2008-03-23 01:01:52 -0700 | [diff] [blame] | 129 | }) |
Jeff Dike | a436ed9 | 2007-05-08 00:35:02 -0700 | [diff] [blame] | 130 | |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 131 | #endif /* _ASM_X86_CMPXCHG_64_H */ |