1
2
3
4
5
6
7
8
9#ifndef __ASM_ARC_CMPXCHG_H
10#define __ASM_ARC_CMPXCHG_H
11
12#include <linux/types.h>
13#include <asm/smp.h>
14
15#ifdef CONFIG_ARC_HAS_LLSC
16
17static inline unsigned long
18__cmpxchg(volatile void *ptr, unsigned long expected, unsigned long new)
19{
20 unsigned long prev;
21
22 __asm__ __volatile__(
23 "1: llock %0, [%1] \n"
24 " brne %0, %2, 2f \n"
25 " scond %3, [%1] \n"
26 " bnz 1b \n"
27 "2: \n"
28 : "=&r"(prev)
29 : "r"(ptr), "ir"(expected),
30 "r"(new)
31 : "cc");
32
33 return prev;
34}
35
36#else
37
38static inline unsigned long
39__cmpxchg(volatile void *ptr, unsigned long expected, unsigned long new)
40{
41 unsigned long flags;
42 int prev;
43 volatile unsigned long *p = ptr;
44
45 atomic_ops_lock(flags);
46 prev = *p;
47 if (prev == expected)
48 *p = new;
49 atomic_ops_unlock(flags);
50 return prev;
51}
52
53#endif
54
55#define cmpxchg(ptr, o, n) ((typeof(*(ptr)))__cmpxchg((ptr), \
56 (unsigned long)(o), (unsigned long)(n)))
57
58
59
60
61
62
63
64
65
66
67#define atomic_cmpxchg(v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))
68
69
70
71
72
73static inline unsigned long __xchg(unsigned long val, volatile void *ptr,
74 int size)
75{
76 extern unsigned long __xchg_bad_pointer(void);
77
78 switch (size) {
79 case 4:
80 __asm__ __volatile__(
81 " ex %0, [%1] \n"
82 : "+r"(val)
83 : "r"(ptr)
84 : "memory");
85
86 return val;
87 }
88 return __xchg_bad_pointer();
89}
90
91#define _xchg(ptr, with) ((typeof(*(ptr)))__xchg((unsigned long)(with), (ptr), \
92 sizeof(*(ptr))))
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109#if !defined(CONFIG_ARC_HAS_LLSC) && defined(CONFIG_SMP)
110
111#define xchg(ptr, with) \
112({ \
113 unsigned long flags; \
114 typeof(*(ptr)) old_val; \
115 \
116 atomic_ops_lock(flags); \
117 old_val = _xchg(ptr, with); \
118 atomic_ops_unlock(flags); \
119 old_val; \
120})
121
122#else
123
124#define xchg(ptr, with) _xchg(ptr, with)
125
126#endif
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
142
143#endif
144