Paul Gortmaker | 34f2c0a | 2012-04-01 16:38:46 -0400 | [diff] [blame] | 1 | /* |
| 2 | * cmpxchg.h -- forked from asm/atomic.h with this copyright: |
| 3 | * |
| 4 | * Copyright 2010 Tilera Corporation. All Rights Reserved. |
| 5 | * |
| 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. |
| 9 | * |
| 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 |
| 14 | * more details. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | #ifndef _ASM_TILE_CMPXCHG_H |
| 19 | #define _ASM_TILE_CMPXCHG_H |
| 20 | |
| 21 | #ifndef __ASSEMBLY__ |
| 22 | |
Chris Metcalf | 6dc9658 | 2013-09-06 08:56:45 -0400 | [diff] [blame] | 23 | #include <asm/barrier.h> |
Paul Gortmaker | 34f2c0a | 2012-04-01 16:38:46 -0400 | [diff] [blame] | 24 | |
Chris Metcalf | 6dc9658 | 2013-09-06 08:56:45 -0400 | [diff] [blame] | 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"); |
| 30 | |
| 31 | #ifndef __tilegx__ |
| 32 | |
| 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); |
Chen Gang | b924a69 | 2013-09-25 12:14:08 +0800 | [diff] [blame] | 38 | long long _atomic64_xchg(long long *v, long long n); |
| 39 | long long _atomic64_xchg_add(long long *v, long long i); |
| 40 | long long _atomic64_xchg_add_unless(long long *v, long long a, long long u); |
| 41 | long long _atomic64_cmpxchg(long long *v, long long o, long long n); |
Chris Metcalf | 6dc9658 | 2013-09-06 08:56:45 -0400 | [diff] [blame] | 42 | |
| 43 | #define xchg(ptr, n) \ |
| 44 | ({ \ |
| 45 | if (sizeof(*(ptr)) != 4) \ |
| 46 | __xchg_called_with_bad_pointer(); \ |
| 47 | smp_mb(); \ |
| 48 | (typeof(*(ptr)))_atomic_xchg((int *)(ptr), (int)(n)); \ |
| 49 | }) |
| 50 | |
| 51 | #define cmpxchg(ptr, o, n) \ |
| 52 | ({ \ |
| 53 | if (sizeof(*(ptr)) != 4) \ |
| 54 | __cmpxchg_called_with_bad_pointer(); \ |
| 55 | smp_mb(); \ |
Chen Gang | b924a69 | 2013-09-25 12:14:08 +0800 | [diff] [blame] | 56 | (typeof(*(ptr)))_atomic_cmpxchg((int *)ptr, (int)o, \ |
| 57 | (int)n); \ |
Chris Metcalf | 6dc9658 | 2013-09-06 08:56:45 -0400 | [diff] [blame] | 58 | }) |
| 59 | |
| 60 | #define xchg64(ptr, n) \ |
| 61 | ({ \ |
| 62 | if (sizeof(*(ptr)) != 8) \ |
| 63 | __xchg_called_with_bad_pointer(); \ |
| 64 | smp_mb(); \ |
Chen Gang | b924a69 | 2013-09-25 12:14:08 +0800 | [diff] [blame] | 65 | (typeof(*(ptr)))_atomic64_xchg((long long *)(ptr), \ |
| 66 | (long long)(n)); \ |
Chris Metcalf | 6dc9658 | 2013-09-06 08:56:45 -0400 | [diff] [blame] | 67 | }) |
| 68 | |
| 69 | #define cmpxchg64(ptr, o, n) \ |
| 70 | ({ \ |
| 71 | if (sizeof(*(ptr)) != 8) \ |
| 72 | __cmpxchg_called_with_bad_pointer(); \ |
| 73 | smp_mb(); \ |
Chen Gang | b924a69 | 2013-09-25 12:14:08 +0800 | [diff] [blame] | 74 | (typeof(*(ptr)))_atomic64_cmpxchg((long long *)ptr, \ |
| 75 | (long long)o, (long long)n); \ |
Chris Metcalf | 6dc9658 | 2013-09-06 08:56:45 -0400 | [diff] [blame] | 76 | }) |
| 77 | |
| 78 | #else |
| 79 | |
| 80 | #define xchg(ptr, n) \ |
Paul Gortmaker | 34f2c0a | 2012-04-01 16:38:46 -0400 | [diff] [blame] | 81 | ({ \ |
| 82 | typeof(*(ptr)) __x; \ |
Chris Metcalf | 6dc9658 | 2013-09-06 08:56:45 -0400 | [diff] [blame] | 83 | smp_mb(); \ |
Paul Gortmaker | 34f2c0a | 2012-04-01 16:38:46 -0400 | [diff] [blame] | 84 | switch (sizeof(*(ptr))) { \ |
| 85 | case 4: \ |
Chris Metcalf | 6dc9658 | 2013-09-06 08:56:45 -0400 | [diff] [blame] | 86 | __x = (typeof(__x))(unsigned long) \ |
Chen Gang | b924a69 | 2013-09-25 12:14:08 +0800 | [diff] [blame] | 87 | __insn_exch4((ptr), \ |
| 88 | (u32)(unsigned long)(n)); \ |
Paul Gortmaker | 34f2c0a | 2012-04-01 16:38:46 -0400 | [diff] [blame] | 89 | break; \ |
| 90 | case 8: \ |
Chen Gang | b924a69 | 2013-09-25 12:14:08 +0800 | [diff] [blame] | 91 | __x = (typeof(__x)) \ |
Chris Metcalf | 6dc9658 | 2013-09-06 08:56:45 -0400 | [diff] [blame] | 92 | __insn_exch((ptr), (unsigned long)(n)); \ |
Paul Gortmaker | 34f2c0a | 2012-04-01 16:38:46 -0400 | [diff] [blame] | 93 | break; \ |
| 94 | default: \ |
| 95 | __xchg_called_with_bad_pointer(); \ |
Chris Metcalf | 6dc9658 | 2013-09-06 08:56:45 -0400 | [diff] [blame] | 96 | break; \ |
Paul Gortmaker | 34f2c0a | 2012-04-01 16:38:46 -0400 | [diff] [blame] | 97 | } \ |
Chris Metcalf | 6dc9658 | 2013-09-06 08:56:45 -0400 | [diff] [blame] | 98 | smp_mb(); \ |
Paul Gortmaker | 34f2c0a | 2012-04-01 16:38:46 -0400 | [diff] [blame] | 99 | __x; \ |
| 100 | }) |
| 101 | |
| 102 | #define cmpxchg(ptr, o, n) \ |
| 103 | ({ \ |
| 104 | typeof(*(ptr)) __x; \ |
Chris Metcalf | 6dc9658 | 2013-09-06 08:56:45 -0400 | [diff] [blame] | 105 | __insn_mtspr(SPR_CMPEXCH_VALUE, (unsigned long)(o)); \ |
| 106 | smp_mb(); \ |
Paul Gortmaker | 34f2c0a | 2012-04-01 16:38:46 -0400 | [diff] [blame] | 107 | switch (sizeof(*(ptr))) { \ |
| 108 | case 4: \ |
Chris Metcalf | 6dc9658 | 2013-09-06 08:56:45 -0400 | [diff] [blame] | 109 | __x = (typeof(__x))(unsigned long) \ |
Chen Gang | b924a69 | 2013-09-25 12:14:08 +0800 | [diff] [blame] | 110 | __insn_cmpexch4((ptr), \ |
| 111 | (u32)(unsigned long)(n)); \ |
Paul Gortmaker | 34f2c0a | 2012-04-01 16:38:46 -0400 | [diff] [blame] | 112 | break; \ |
| 113 | case 8: \ |
Chen Gang | b924a69 | 2013-09-25 12:14:08 +0800 | [diff] [blame] | 114 | __x = (typeof(__x))__insn_cmpexch((ptr), \ |
| 115 | (long long)(n)); \ |
Paul Gortmaker | 34f2c0a | 2012-04-01 16:38:46 -0400 | [diff] [blame] | 116 | break; \ |
| 117 | default: \ |
| 118 | __cmpxchg_called_with_bad_pointer(); \ |
Chris Metcalf | 6dc9658 | 2013-09-06 08:56:45 -0400 | [diff] [blame] | 119 | break; \ |
Paul Gortmaker | 34f2c0a | 2012-04-01 16:38:46 -0400 | [diff] [blame] | 120 | } \ |
Chris Metcalf | 6dc9658 | 2013-09-06 08:56:45 -0400 | [diff] [blame] | 121 | smp_mb(); \ |
Paul Gortmaker | 34f2c0a | 2012-04-01 16:38:46 -0400 | [diff] [blame] | 122 | __x; \ |
| 123 | }) |
| 124 | |
Chris Metcalf | 6dc9658 | 2013-09-06 08:56:45 -0400 | [diff] [blame] | 125 | #define xchg64 xchg |
| 126 | #define cmpxchg64 cmpxchg |
Paul Gortmaker | 34f2c0a | 2012-04-01 16:38:46 -0400 | [diff] [blame] | 127 | |
Chris Metcalf | 6dc9658 | 2013-09-06 08:56:45 -0400 | [diff] [blame] | 128 | #endif |
| 129 | |
| 130 | #define tas(ptr) xchg((ptr), 1) |
Chen Gang | cf3a13d | 2013-07-01 10:21:51 +0800 | [diff] [blame] | 131 | |
Paul Gortmaker | 34f2c0a | 2012-04-01 16:38:46 -0400 | [diff] [blame] | 132 | #endif /* __ASSEMBLY__ */ |
| 133 | |
| 134 | #endif /* _ASM_TILE_CMPXCHG_H */ |