aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorHarvey Harrison <harvey.harrison@gmail.com>2008-01-30 07:31:26 -0500
committerIngo Molnar <mingo@elte.hu>2008-01-30 07:31:26 -0500
commit5638f99394d478bcba6384ac7c51e763d1a3a448 (patch)
tree7edf6a96ed26ef8cde23cad655f76299f93874c9 /include
parent8ee5797a91bdb713b4031741b33bd035f9c43870 (diff)
x86: unify local_{32|64}.h
Signed-off-by: Harvey Harrison <harvey.harrison@gmail.com> Signed-off-by: Ingo Molnar <mingo@elte.hu> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Diffstat (limited to 'include')
-rw-r--r--include/asm-x86/local.h244
-rw-r--r--include/asm-x86/local_32.h219
-rw-r--r--include/asm-x86/local_64.h207
3 files changed, 240 insertions, 430 deletions
diff --git a/include/asm-x86/local.h b/include/asm-x86/local.h
index c7a1b1c66c96..f5677e208601 100644
--- a/include/asm-x86/local.h
+++ b/include/asm-x86/local.h
@@ -1,5 +1,241 @@
1#ifdef CONFIG_X86_32 1#ifndef _ARCH_LOCAL_H
2# include "local_32.h" 2#define _ARCH_LOCAL_H
3#else 3
4# include "local_64.h" 4#include <linux/percpu.h>
5
6#include <asm/system.h>
7#include <asm/atomic.h>
8#include <asm/asm.h>
9
10typedef struct
11{
12 atomic_long_t a;
13} local_t;
14
15#define LOCAL_INIT(i) { ATOMIC_LONG_INIT(i) }
16
17#define local_read(l) atomic_long_read(&(l)->a)
18#define local_set(l,i) atomic_long_set(&(l)->a, (i))
19
20static inline void local_inc(local_t *l)
21{
22 __asm__ __volatile__(
23 _ASM_INC "%0"
24 :"+m" (l->a.counter));
25}
26
27static inline void local_dec(local_t *l)
28{
29 __asm__ __volatile__(
30 _ASM_DEC "%0"
31 :"+m" (l->a.counter));
32}
33
34static inline void local_add(long i, local_t *l)
35{
36 __asm__ __volatile__(
37 _ASM_ADD "%1,%0"
38 :"+m" (l->a.counter)
39 :"ir" (i));
40}
41
42static inline void local_sub(long i, local_t *l)
43{
44 __asm__ __volatile__(
45 _ASM_SUB "%1,%0"
46 :"+m" (l->a.counter)
47 :"ir" (i));
48}
49
50/**
51 * local_sub_and_test - subtract value from variable and test result
52 * @i: integer value to subtract
53 * @l: pointer to type local_t
54 *
55 * Atomically subtracts @i from @l and returns
56 * true if the result is zero, or false for all
57 * other cases.
58 */
59static inline int local_sub_and_test(long i, local_t *l)
60{
61 unsigned char c;
62
63 __asm__ __volatile__(
64 _ASM_SUB "%2,%0; sete %1"
65 :"+m" (l->a.counter), "=qm" (c)
66 :"ir" (i) : "memory");
67 return c;
68}
69
70/**
71 * local_dec_and_test - decrement and test
72 * @l: pointer to type local_t
73 *
74 * Atomically decrements @l by 1 and
75 * returns true if the result is 0, or false for all other
76 * cases.
77 */
78static inline int local_dec_and_test(local_t *l)
79{
80 unsigned char c;
81
82 __asm__ __volatile__(
83 _ASM_DEC "%0; sete %1"
84 :"+m" (l->a.counter), "=qm" (c)
85 : : "memory");
86 return c != 0;
87}
88
89/**
90 * local_inc_and_test - increment and test
91 * @l: pointer to type local_t
92 *
93 * Atomically increments @l by 1
94 * and returns true if the result is zero, or false for all
95 * other cases.
96 */
97static inline int local_inc_and_test(local_t *l)
98{
99 unsigned char c;
100
101 __asm__ __volatile__(
102 _ASM_INC "%0; sete %1"
103 :"+m" (l->a.counter), "=qm" (c)
104 : : "memory");
105 return c != 0;
106}
107
108/**
109 * local_add_negative - add and test if negative
110 * @i: integer value to add
111 * @l: pointer to type local_t
112 *
113 * Atomically adds @i to @l and returns true
114 * if the result is negative, or false when
115 * result is greater than or equal to zero.
116 */
117static inline int local_add_negative(long i, local_t *l)
118{
119 unsigned char c;
120
121 __asm__ __volatile__(
122 _ASM_ADD "%2,%0; sets %1"
123 :"+m" (l->a.counter), "=qm" (c)
124 :"ir" (i) : "memory");
125 return c;
126}
127
128/**
129 * local_add_return - add and return
130 * @i: integer value to add
131 * @l: pointer to type local_t
132 *
133 * Atomically adds @i to @l and returns @i + @l
134 */
135static inline long local_add_return(long i, local_t *l)
136{
137 long __i;
138#ifdef CONFIG_M386
139 unsigned long flags;
140 if(unlikely(boot_cpu_data.x86 <= 3))
141 goto no_xadd;
5#endif 142#endif
143 /* Modern 486+ processor */
144 __i = i;
145 __asm__ __volatile__(
146 _ASM_XADD "%0, %1;"
147 :"+r" (i), "+m" (l->a.counter)
148 : : "memory");
149 return i + __i;
150
151#ifdef CONFIG_M386
152no_xadd: /* Legacy 386 processor */
153 local_irq_save(flags);
154 __i = local_read(l);
155 local_set(l, i + __i);
156 local_irq_restore(flags);
157 return i + __i;
158#endif
159}
160
161static inline long local_sub_return(long i, local_t *l)
162{
163 return local_add_return(-i,l);
164}
165
166#define local_inc_return(l) (local_add_return(1,l))
167#define local_dec_return(l) (local_sub_return(1,l))
168
169#define local_cmpxchg(l, o, n) \
170 (cmpxchg_local(&((l)->a.counter), (o), (n)))
171/* Always has a lock prefix */
172#define local_xchg(l, n) (xchg(&((l)->a.counter), (n)))
173
174/**
175 * local_add_unless - add unless the number is a given value
176 * @l: pointer of type local_t
177 * @a: the amount to add to l...
178 * @u: ...unless l is equal to u.
179 *
180 * Atomically adds @a to @l, so long as it was not @u.
181 * Returns non-zero if @l was not @u, and zero otherwise.
182 */
183#define local_add_unless(l, a, u) \
184({ \
185 long c, old; \
186 c = local_read(l); \
187 for (;;) { \
188 if (unlikely(c == (u))) \
189 break; \
190 old = local_cmpxchg((l), c, c + (a)); \
191 if (likely(old == c)) \
192 break; \
193 c = old; \
194 } \
195 c != (u); \
196})
197#define local_inc_not_zero(l) local_add_unless((l), 1, 0)
198
199/* On x86_32, these are no better than the atomic variants.
200 * On x86-64 these are better than the atomic variants on SMP kernels
201 * because they dont use a lock prefix.
202 */
203#define __local_inc(l) local_inc(l)
204#define __local_dec(l) local_dec(l)
205#define __local_add(i,l) local_add((i),(l))
206#define __local_sub(i,l) local_sub((i),(l))
207
208/* Use these for per-cpu local_t variables: on some archs they are
209 * much more efficient than these naive implementations. Note they take
210 * a variable, not an address.
211 *
212 * X86_64: This could be done better if we moved the per cpu data directly
213 * after GS.
214 */
215
216/* Need to disable preemption for the cpu local counters otherwise we could
217 still access a variable of a previous CPU in a non atomic way. */
218#define cpu_local_wrap_v(l) \
219 ({ local_t res__; \
220 preempt_disable(); \
221 res__ = (l); \
222 preempt_enable(); \
223 res__; })
224#define cpu_local_wrap(l) \
225 ({ preempt_disable(); \
226 l; \
227 preempt_enable(); }) \
228
229#define cpu_local_read(l) cpu_local_wrap_v(local_read(&__get_cpu_var(l)))
230#define cpu_local_set(l, i) cpu_local_wrap(local_set(&__get_cpu_var(l), (i)))
231#define cpu_local_inc(l) cpu_local_wrap(local_inc(&__get_cpu_var(l)))
232#define cpu_local_dec(l) cpu_local_wrap(local_dec(&__get_cpu_var(l)))
233#define cpu_local_add(i, l) cpu_local_wrap(local_add((i), &__get_cpu_var(l)))
234#define cpu_local_sub(i, l) cpu_local_wrap(local_sub((i), &__get_cpu_var(l)))
235
236#define __cpu_local_inc(l) cpu_local_inc(l)
237#define __cpu_local_dec(l) cpu_local_dec(l)
238#define __cpu_local_add(i, l) cpu_local_add((i), (l))
239#define __cpu_local_sub(i, l) cpu_local_sub((i), (l))
240
241#endif /* _ARCH_LOCAL_H */
diff --git a/include/asm-x86/local_32.h b/include/asm-x86/local_32.h
deleted file mode 100644
index c219fe56b3df..000000000000
--- a/include/asm-x86/local_32.h
+++ /dev/null
@@ -1,219 +0,0 @@
1#ifndef _ARCH_I386_LOCAL_H
2#define _ARCH_I386_LOCAL_H
3
4static inline void local_inc(local_t *l)
5{
6 __asm__ __volatile__(
7 _ASM_INC "%0"
8 :"+m" (l->a.counter));
9}
10
11static inline void local_dec(local_t *l)
12{
13 __asm__ __volatile__(
14 _ASM_DEC "%0"
15 :"+m" (l->a.counter));
16}
17
18static inline void local_add(long i, local_t *l)
19{
20 __asm__ __volatile__(
21 _ASM_ADD "%1,%0"
22 :"+m" (l->a.counter)
23 :"ir" (i));
24}
25
26static inline void local_sub(long i, local_t *l)
27{
28 __asm__ __volatile__(
29 _ASM_SUB "%1,%0"
30 :"+m" (l->a.counter)
31 :"ir" (i));
32}
33
34/**
35 * local_sub_and_test - subtract value from variable and test result
36 * @i: integer value to subtract
37 * @l: pointer of type local_t
38 *
39 * Atomically subtracts @i from @l and returns
40 * true if the result is zero, or false for all
41 * other cases.
42 */
43static inline int local_sub_and_test(long i, local_t *l)
44{
45 unsigned char c;
46
47 __asm__ __volatile__(
48 _ASM_SUB "%2,%0; sete %1"
49 :"+m" (l->a.counter), "=qm" (c)
50 :"ir" (i) : "memory");
51 return c;
52}
53
54/**
55 * local_dec_and_test - decrement and test
56 * @l: pointer of type local_t
57 *
58 * Atomically decrements @l by 1 and
59 * returns true if the result is 0, or false for all other
60 * cases.
61 */
62static inline int local_dec_and_test(local_t *l)
63{
64 unsigned char c;
65
66 __asm__ __volatile__(
67 _ASM_DEC "%0; sete %1"
68 :"+m" (l->a.counter), "=qm" (c)
69 : : "memory");
70 return c != 0;
71}
72
73/**
74 * local_inc_and_test - increment and test
75 * @l: pointer of type local_t
76 *
77 * Atomically increments @l by 1
78 * and returns true if the result is zero, or false for all
79 * other cases.
80 */
81static inline int local_inc_and_test(local_t *l)
82{
83 unsigned char c;
84
85 __asm__ __volatile__(
86 _ASM_INC "%0; sete %1"
87 :"+m" (l->a.counter), "=qm" (c)
88 : : "memory");
89 return c != 0;
90}
91
92/**
93 * local_add_negative - add and test if negative
94 * @l: pointer of type local_t
95 * @i: integer value to add
96 *
97 * Atomically adds @i to @l and returns true
98 * if the result is negative, or false when
99 * result is greater than or equal to zero.
100 */
101static inline int local_add_negative(long i, local_t *l)
102{
103 unsigned char c;
104
105 __asm__ __volatile__(
106 _ASM_ADD "%2,%0; sets %1"
107 :"+m" (l->a.counter), "=qm" (c)
108 :"ir" (i) : "memory");
109 return c;
110}
111
112/**
113 * local_add_return - add and return
114 * @l: pointer of type local_t
115 * @i: integer value to add
116 *
117 * Atomically adds @i to @l and returns @i + @l
118 */
119static inline long local_add_return(long i, local_t *l)
120{
121 long __i;
122#ifdef CONFIG_M386
123 unsigned long flags;
124 if(unlikely(boot_cpu_data.x86 <= 3))
125 goto no_xadd;
126#endif
127 /* Modern 486+ processor */
128 __i = i;
129 __asm__ __volatile__(
130 _ASM_XADD "%0, %1;"
131 :"+r" (i), "+m" (l->a.counter)
132 : : "memory");
133 return i + __i;
134
135#ifdef CONFIG_M386
136no_xadd: /* Legacy 386 processor */
137 local_irq_save(flags);
138 __i = local_read(l);
139 local_set(l, i + __i);
140 local_irq_restore(flags);
141 return i + __i;
142#endif
143}
144
145static inline long local_sub_return(long i, local_t *l)
146{
147 return local_add_return(-i,l);
148}
149
150#define local_inc_return(l) (local_add_return(1,l))
151#define local_dec_return(l) (local_sub_return(1,l))
152
153#define local_cmpxchg(l, o, n) \
154 (cmpxchg_local(&((l)->a.counter), (o), (n)))
155/* Always has a lock prefix */
156#define local_xchg(l, n) (xchg(&((l)->a.counter), (n)))
157
158/**
159 * local_add_unless - add unless the number is a given value
160 * @l: pointer of type local_t
161 * @a: the amount to add to l...
162 * @u: ...unless l is equal to u.
163 *
164 * Atomically adds @a to @l, so long as it was not @u.
165 * Returns non-zero if @l was not @u, and zero otherwise.
166 */
167#define local_add_unless(l, a, u) \
168({ \
169 long c, old; \
170 c = local_read(l); \
171 for (;;) { \
172 if (unlikely(c == (u))) \
173 break; \
174 old = local_cmpxchg((l), c, c + (a)); \
175 if (likely(old == c)) \
176 break; \
177 c = old; \
178 } \
179 c != (u); \
180})
181#define local_inc_not_zero(l) local_add_unless((l), 1, 0)
182
183/* On x86, these are no better than the atomic variants. */
184#define __local_inc(l) local_inc(l)
185#define __local_dec(l) local_dec(l)
186#define __local_add(i,l) local_add((i),(l))
187#define __local_sub(i,l) local_sub((i),(l))
188
189/* Use these for per-cpu local_t variables: on some archs they are
190 * much more efficient than these naive implementations. Note they take
191 * a variable, not an address.
192 */
193
194/* Need to disable preemption for the cpu local counters otherwise we could
195 still access a variable of a previous CPU in a non atomic way. */
196#define cpu_local_wrap_v(l) \
197 ({ local_t res__; \
198 preempt_disable(); \
199 res__ = (l); \
200 preempt_enable(); \
201 res__; })
202#define cpu_local_wrap(l) \
203 ({ preempt_disable(); \
204 l; \
205 preempt_enable(); }) \
206
207#define cpu_local_read(l) cpu_local_wrap_v(local_read(&__get_cpu_var(l)))
208#define cpu_local_set(l, i) cpu_local_wrap(local_set(&__get_cpu_var(l), (i)))
209#define cpu_local_inc(l) cpu_local_wrap(local_inc(&__get_cpu_var(l)))
210#define cpu_local_dec(l) cpu_local_wrap(local_dec(&__get_cpu_var(l)))
211#define cpu_local_add(i, l) cpu_local_wrap(local_add((i), &__get_cpu_var(l)))
212#define cpu_local_sub(i, l) cpu_local_wrap(local_sub((i), &__get_cpu_var(l)))
213
214#define __cpu_local_inc(l) cpu_local_inc(l)
215#define __cpu_local_dec(l) cpu_local_dec(l)
216#define __cpu_local_add(i, l) cpu_local_add((i), (l))
217#define __cpu_local_sub(i, l) cpu_local_sub((i), (l))
218
219#endif /* _ARCH_I386_LOCAL_H */
diff --git a/include/asm-x86/local_64.h b/include/asm-x86/local_64.h
deleted file mode 100644
index d685cd7e014f..000000000000
--- a/include/asm-x86/local_64.h
+++ /dev/null
@@ -1,207 +0,0 @@
1#ifndef _ARCH_X8664_LOCAL_H
2#define _ARCH_X8664_LOCAL_H
3
4static inline void local_inc(local_t *l)
5{
6 __asm__ __volatile__(
7 _ASM_INC "%0"
8 :"+m" (l->a.counter));
9}
10
11static inline void local_dec(local_t *l)
12{
13 __asm__ __volatile__(
14 _ASM_DEC "%0"
15 :"+m" (l->a.counter));
16}
17
18static inline void local_add(long i, local_t *l)
19{
20 __asm__ __volatile__(
21 _ASM_ADD "%1,%0"
22 :"+m" (l->a.counter)
23 :"ir" (i));
24}
25
26static inline void local_sub(long i, local_t *l)
27{
28 __asm__ __volatile__(
29 _ASM_SUB "%1,%0"
30 :"+m" (l->a.counter)
31 :"ir" (i));
32}
33
34/**
35 * local_sub_and_test - subtract value from variable and test result
36 * @i: integer value to subtract
37 * @l: pointer to type local_t
38 *
39 * Atomically subtracts @i from @l and returns
40 * true if the result is zero, or false for all
41 * other cases.
42 */
43static inline int local_sub_and_test(long i, local_t *l)
44{
45 unsigned char c;
46
47 __asm__ __volatile__(
48 _ASM_SUB "%2,%0; sete %1"
49 :"+m" (l->a.counter), "=qm" (c)
50 :"ir" (i) : "memory");
51 return c;
52}
53
54/**
55 * local_dec_and_test - decrement and test
56 * @l: pointer to type local_t
57 *
58 * Atomically decrements @l by 1 and
59 * returns true if the result is 0, or false for all other
60 * cases.
61 */
62static inline int local_dec_and_test(local_t *l)
63{
64 unsigned char c;
65
66 __asm__ __volatile__(
67 _ASM_DEC "%0; sete %1"
68 :"+m" (l->a.counter), "=qm" (c)
69 : : "memory");
70 return c != 0;
71}
72
73/**
74 * local_inc_and_test - increment and test
75 * @l: pointer to type local_t
76 *
77 * Atomically increments @l by 1
78 * and returns true if the result is zero, or false for all
79 * other cases.
80 */
81static inline int local_inc_and_test(local_t *l)
82{
83 unsigned char c;
84
85 __asm__ __volatile__(
86 _ASM_INC "%0; sete %1"
87 :"+m" (l->a.counter), "=qm" (c)
88 : : "memory");
89 return c != 0;
90}
91
92/**
93 * local_add_negative - add and test if negative
94 * @i: integer value to add
95 * @l: pointer to type local_t
96 *
97 * Atomically adds @i to @l and returns true
98 * if the result is negative, or false when
99 * result is greater than or equal to zero.
100 */
101static inline int local_add_negative(long i, local_t *l)
102{
103 unsigned char c;
104
105 __asm__ __volatile__(
106 _ASM_ADD "%2,%0; sets %1"
107 :"+m" (l->a.counter), "=qm" (c)
108 :"ir" (i) : "memory");
109 return c;
110}
111
112/**
113 * local_add_return - add and return
114 * @i: integer value to add
115 * @l: pointer to type local_t
116 *
117 * Atomically adds @i to @l and returns @i + @l
118 */
119static inline long local_add_return(long i, local_t *l)
120{
121 long __i = i;
122 __asm__ __volatile__(
123 _ASM_XADD "%0, %1;"
124 :"+r" (i), "+m" (l->a.counter)
125 : : "memory");
126 return i + __i;
127}
128
129static inline long local_sub_return(long i, local_t *l)
130{
131 return local_add_return(-i,l);
132}
133
134#define local_inc_return(l) (local_add_return(1,l))
135#define local_dec_return(l) (local_sub_return(1,l))
136
137#define local_cmpxchg(l, o, n) \
138 (cmpxchg_local(&((l)->a.counter), (o), (n)))
139/* Always has a lock prefix */
140#define local_xchg(l, n) (xchg(&((l)->a.counter), (n)))
141
142/**
143 * atomic_up_add_unless - add unless the number is a given value
144 * @l: pointer of type local_t
145 * @a: the amount to add to l...
146 * @u: ...unless l is equal to u.
147 *
148 * Atomically adds @a to @l, so long as it was not @u.
149 * Returns non-zero if @l was not @u, and zero otherwise.
150 */
151#define local_add_unless(l, a, u) \
152({ \
153 long c, old; \
154 c = local_read(l); \
155 for (;;) { \
156 if (unlikely(c == (u))) \
157 break; \
158 old = local_cmpxchg((l), c, c + (a)); \
159 if (likely(old == c)) \
160 break; \
161 c = old; \
162 } \
163 c != (u); \
164})
165#define local_inc_not_zero(l) local_add_unless((l), 1, 0)
166
167/* On x86-64 these are better than the atomic variants on SMP kernels
168 because they dont use a lock prefix. */
169#define __local_inc(l) local_inc(l)
170#define __local_dec(l) local_dec(l)
171#define __local_add(i,l) local_add((i),(l))
172#define __local_sub(i,l) local_sub((i),(l))
173
174/* Use these for per-cpu local_t variables: on some archs they are
175 * much more efficient than these naive implementations. Note they take
176 * a variable, not an address.
177 *
178 * This could be done better if we moved the per cpu data directly
179 * after GS.
180 */
181
182/* Need to disable preemption for the cpu local counters otherwise we could
183 still access a variable of a previous CPU in a non atomic way. */
184#define cpu_local_wrap_v(l) \
185 ({ local_t res__; \
186 preempt_disable(); \
187 res__ = (l); \
188 preempt_enable(); \
189 res__; })
190#define cpu_local_wrap(l) \
191 ({ preempt_disable(); \
192 l; \
193 preempt_enable(); }) \
194
195#define cpu_local_read(l) cpu_local_wrap_v(local_read(&__get_cpu_var(l)))
196#define cpu_local_set(l, i) cpu_local_wrap(local_set(&__get_cpu_var(l), (i)))
197#define cpu_local_inc(l) cpu_local_wrap(local_inc(&__get_cpu_var(l)))
198#define cpu_local_dec(l) cpu_local_wrap(local_dec(&__get_cpu_var(l)))
199#define cpu_local_add(i, l) cpu_local_wrap(local_add((i), &__get_cpu_var(l)))
200#define cpu_local_sub(i, l) cpu_local_wrap(local_sub((i), &__get_cpu_var(l)))
201
202#define __cpu_local_inc(l) cpu_local_inc(l)
203#define __cpu_local_dec(l) cpu_local_dec(l)
204#define __cpu_local_add(i, l) cpu_local_add((i), (l))
205#define __cpu_local_sub(i, l) cpu_local_sub((i), (l))
206
207#endif /* _ARCH_X8664_LOCAL_H */