blob: 94fde8d0fac1c60e1ee5999d14f8e4f0ec4c1226 [file] [log] [blame]
Mathieu Desnoyers72323112007-05-08 00:34:47 -07001#ifndef _ARCH_MIPS_LOCAL_H
2#define _ARCH_MIPS_LOCAL_H
Linus Torvalds1da177e2005-04-16 15:20:36 -07003
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <linux/percpu.h>
Mathieu Desnoyers72323112007-05-08 00:34:47 -07005#include <linux/bitops.h>
Arun Sharma60063492011-07-26 16:09:06 -07006#include <linux/atomic.h>
Ralf Baechlefef74702007-10-01 04:15:00 +01007#include <asm/cmpxchg.h>
Mathieu Desnoyers72323112007-05-08 00:34:47 -07008#include <asm/war.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Mathieu Desnoyers72323112007-05-08 00:34:47 -070010typedef struct
11{
12 atomic_long_t a;
13} local_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Mathieu Desnoyers72323112007-05-08 00:34:47 -070015#define LOCAL_INIT(i) { ATOMIC_LONG_INIT(i) }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Mathieu Desnoyers72323112007-05-08 00:34:47 -070017#define local_read(l) atomic_long_read(&(l)->a)
Ralf Baechle21a151d2007-10-11 23:46:15 +010018#define local_set(l, i) atomic_long_set(&(l)->a, (i))
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Ralf Baechle21a151d2007-10-11 23:46:15 +010020#define local_add(i, l) atomic_long_add((i), (&(l)->a))
21#define local_sub(i, l) atomic_long_sub((i), (&(l)->a))
Mathieu Desnoyers72323112007-05-08 00:34:47 -070022#define local_inc(l) atomic_long_inc(&(l)->a)
23#define local_dec(l) atomic_long_dec(&(l)->a)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25/*
Mathieu Desnoyers72323112007-05-08 00:34:47 -070026 * Same as above, but return the result value
27 */
28static __inline__ long local_add_return(long i, local_t * l)
29{
30 unsigned long result;
31
David Daneyb791d112009-07-13 11:15:19 -070032 if (kernel_uses_llsc && R10000_LLSC_WAR) {
Mathieu Desnoyers72323112007-05-08 00:34:47 -070033 unsigned long temp;
34
35 __asm__ __volatile__(
36 " .set mips3 \n"
37 "1:" __LL "%1, %2 # local_add_return \n"
38 " addu %0, %1, %3 \n"
39 __SC "%0, %2 \n"
40 " beqzl %0, 1b \n"
41 " addu %0, %1, %3 \n"
42 " .set mips0 \n"
43 : "=&r" (result), "=&r" (temp), "=m" (l->a.counter)
44 : "Ir" (i), "m" (l->a.counter)
45 : "memory");
David Daneyb791d112009-07-13 11:15:19 -070046 } else if (kernel_uses_llsc) {
Mathieu Desnoyers72323112007-05-08 00:34:47 -070047 unsigned long temp;
48
49 __asm__ __volatile__(
50 " .set mips3 \n"
51 "1:" __LL "%1, %2 # local_add_return \n"
52 " addu %0, %1, %3 \n"
53 __SC "%0, %2 \n"
54 " beqz %0, 1b \n"
55 " addu %0, %1, %3 \n"
56 " .set mips0 \n"
57 : "=&r" (result), "=&r" (temp), "=m" (l->a.counter)
58 : "Ir" (i), "m" (l->a.counter)
59 : "memory");
60 } else {
61 unsigned long flags;
62
63 local_irq_save(flags);
64 result = l->a.counter;
65 result += i;
66 l->a.counter = result;
67 local_irq_restore(flags);
68 }
69
70 return result;
71}
72
73static __inline__ long local_sub_return(long i, local_t * l)
74{
75 unsigned long result;
76
David Daneyb791d112009-07-13 11:15:19 -070077 if (kernel_uses_llsc && R10000_LLSC_WAR) {
Mathieu Desnoyers72323112007-05-08 00:34:47 -070078 unsigned long temp;
79
80 __asm__ __volatile__(
81 " .set mips3 \n"
82 "1:" __LL "%1, %2 # local_sub_return \n"
83 " subu %0, %1, %3 \n"
84 __SC "%0, %2 \n"
85 " beqzl %0, 1b \n"
86 " subu %0, %1, %3 \n"
87 " .set mips0 \n"
88 : "=&r" (result), "=&r" (temp), "=m" (l->a.counter)
89 : "Ir" (i), "m" (l->a.counter)
90 : "memory");
David Daneyb791d112009-07-13 11:15:19 -070091 } else if (kernel_uses_llsc) {
Mathieu Desnoyers72323112007-05-08 00:34:47 -070092 unsigned long temp;
93
94 __asm__ __volatile__(
95 " .set mips3 \n"
96 "1:" __LL "%1, %2 # local_sub_return \n"
97 " subu %0, %1, %3 \n"
98 __SC "%0, %2 \n"
99 " beqz %0, 1b \n"
100 " subu %0, %1, %3 \n"
101 " .set mips0 \n"
102 : "=&r" (result), "=&r" (temp), "=m" (l->a.counter)
103 : "Ir" (i), "m" (l->a.counter)
104 : "memory");
105 } else {
106 unsigned long flags;
107
108 local_irq_save(flags);
109 result = l->a.counter;
110 result -= i;
111 l->a.counter = result;
112 local_irq_restore(flags);
113 }
114
115 return result;
116}
117
Mathieu Desnoyers72323112007-05-08 00:34:47 -0700118#define local_cmpxchg(l, o, n) \
119 ((long)cmpxchg_local(&((l)->a.counter), (o), (n)))
Deng-Cheng Zhu6dbd9722010-10-12 19:37:20 +0800120#define local_xchg(l, n) (atomic_long_xchg((&(l)->a), (n)))
Mathieu Desnoyers72323112007-05-08 00:34:47 -0700121
122/**
123 * local_add_unless - add unless the number is a given value
124 * @l: pointer of type local_t
125 * @a: the amount to add to l...
126 * @u: ...unless l is equal to u.
127 *
128 * Atomically adds @a to @l, so long as it was not @u.
129 * Returns non-zero if @l was not @u, and zero otherwise.
130 */
131#define local_add_unless(l, a, u) \
132({ \
133 long c, old; \
134 c = local_read(l); \
135 while (c != (u) && (old = local_cmpxchg((l), c, c + (a))) != c) \
136 c = old; \
137 c != (u); \
138})
139#define local_inc_not_zero(l) local_add_unless((l), 1, 0)
140
Ralf Baechle21a151d2007-10-11 23:46:15 +0100141#define local_dec_return(l) local_sub_return(1, (l))
142#define local_inc_return(l) local_add_return(1, (l))
Mathieu Desnoyers72323112007-05-08 00:34:47 -0700143
144/*
145 * local_sub_and_test - subtract value from variable and test result
146 * @i: integer value to subtract
147 * @l: pointer of type local_t
148 *
149 * Atomically subtracts @i from @l and returns
150 * true if the result is zero, or false for all
151 * other cases.
152 */
Ralf Baechle21a151d2007-10-11 23:46:15 +0100153#define local_sub_and_test(i, l) (local_sub_return((i), (l)) == 0)
Mathieu Desnoyers72323112007-05-08 00:34:47 -0700154
155/*
156 * local_inc_and_test - increment and test
157 * @l: pointer of type local_t
158 *
159 * Atomically increments @l by 1
160 * and returns true if the result is zero, or false for all
161 * other cases.
162 */
163#define local_inc_and_test(l) (local_inc_return(l) == 0)
164
165/*
166 * local_dec_and_test - decrement by 1 and test
167 * @l: pointer of type local_t
168 *
169 * Atomically decrements @l by 1 and
170 * returns true if the result is 0, or false for all other
171 * cases.
172 */
173#define local_dec_and_test(l) (local_sub_return(1, (l)) == 0)
174
175/*
Mathieu Desnoyers72323112007-05-08 00:34:47 -0700176 * local_add_negative - add and test if negative
177 * @l: pointer of type local_t
178 * @i: integer value to add
179 *
180 * Atomically adds @i to @l and returns true
181 * if the result is negative, or false when
182 * result is greater than or equal to zero.
183 */
Ralf Baechle21a151d2007-10-11 23:46:15 +0100184#define local_add_negative(i, l) (local_add_return(i, (l)) < 0)
Mathieu Desnoyers72323112007-05-08 00:34:47 -0700185
186/* Use these for per-cpu local_t variables: on some archs they are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 * much more efficient than these naive implementations. Note they take
188 * a variable, not an address.
189 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Mathieu Desnoyers72323112007-05-08 00:34:47 -0700191#define __local_inc(l) ((l)->a.counter++)
192#define __local_dec(l) ((l)->a.counter++)
Ralf Baechle21a151d2007-10-11 23:46:15 +0100193#define __local_add(i, l) ((l)->a.counter+=(i))
194#define __local_sub(i, l) ((l)->a.counter-=(i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Mathieu Desnoyers72323112007-05-08 00:34:47 -0700196#endif /* _ARCH_MIPS_LOCAL_H */