blob: 40901e353c2181a9ef2c747b40bd64c4c7f0b8b2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __ARCH_H8300_ATOMIC__
2#define __ARCH_H8300_ATOMIC__
3
Matthew Wilcoxea4354672009-01-06 14:40:39 -08004#include <linux/types.h>
David Howellsa5401ee2012-03-28 18:30:02 +01005#include <asm/cmpxchg.h>
Matthew Wilcoxea4354672009-01-06 14:40:39 -08006
Linus Torvalds1da177e2005-04-16 15:20:36 -07007/*
8 * Atomic operations that C can't guarantee us. Useful for
9 * resource counting etc..
10 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#define ATOMIC_INIT(i) { (i) }
13
Anton Blanchardf3d46f92010-05-17 14:33:53 +100014#define atomic_read(v) (*(volatile int *)&(v)->counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#define atomic_set(v, i) (((v)->counter) = i)
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/kernel.h>
18
19static __inline__ int atomic_add_return(int i, atomic_t *v)
20{
David Howells3ab61eb2010-08-31 16:52:11 +010021 unsigned long flags;
22 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 local_irq_save(flags);
24 ret = v->counter += i;
25 local_irq_restore(flags);
26 return ret;
27}
28
29#define atomic_add(i, v) atomic_add_return(i, v)
30#define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
31
32static __inline__ int atomic_sub_return(int i, atomic_t *v)
33{
David Howells3ab61eb2010-08-31 16:52:11 +010034 unsigned long flags;
35 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 local_irq_save(flags);
37 ret = v->counter -= i;
38 local_irq_restore(flags);
39 return ret;
40}
41
42#define atomic_sub(i, v) atomic_sub_return(i, v)
Yoshinori Satob67405b2007-05-15 01:41:07 -070043#define atomic_sub_and_test(i,v) (atomic_sub_return(i, v) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45static __inline__ int atomic_inc_return(atomic_t *v)
46{
David Howells3ab61eb2010-08-31 16:52:11 +010047 unsigned long flags;
48 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 local_irq_save(flags);
50 v->counter++;
51 ret = v->counter;
52 local_irq_restore(flags);
53 return ret;
54}
55
56#define atomic_inc(v) atomic_inc_return(v)
57
58/*
59 * atomic_inc_and_test - increment and test
60 * @v: pointer of type atomic_t
61 *
62 * Atomically increments @v by 1
63 * and returns true if the result is zero, or false for all
64 * other cases.
65 */
66#define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
67
68static __inline__ int atomic_dec_return(atomic_t *v)
69{
David Howells3ab61eb2010-08-31 16:52:11 +010070 unsigned long flags;
71 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 local_irq_save(flags);
73 --v->counter;
74 ret = v->counter;
75 local_irq_restore(flags);
76 return ret;
77}
78
79#define atomic_dec(v) atomic_dec_return(v)
80
81static __inline__ int atomic_dec_and_test(atomic_t *v)
82{
David Howells3ab61eb2010-08-31 16:52:11 +010083 unsigned long flags;
84 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 local_irq_save(flags);
86 --v->counter;
87 ret = v->counter;
88 local_irq_restore(flags);
89 return ret == 0;
90}
91
Nick Piggin4a6dae62005-11-13 16:07:24 -080092static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
93{
94 int ret;
95 unsigned long flags;
96
97 local_irq_save(flags);
98 ret = v->counter;
99 if (likely(ret == old))
100 v->counter = new;
101 local_irq_restore(flags);
102 return ret;
103}
104
Arun Sharmaf24219b2011-07-26 16:09:07 -0700105static inline int __atomic_add_unless(atomic_t *v, int a, int u)
Nick Piggin8426e1f2005-11-13 16:07:25 -0800106{
107 int ret;
108 unsigned long flags;
109
110 local_irq_save(flags);
111 ret = v->counter;
112 if (ret != u)
113 v->counter += a;
114 local_irq_restore(flags);
Arun Sharmaf24219b2011-07-26 16:09:07 -0700115 return ret;
Nick Piggin8426e1f2005-11-13 16:07:25 -0800116}
Nick Piggin8426e1f2005-11-13 16:07:25 -0800117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static __inline__ void atomic_clear_mask(unsigned long mask, unsigned long *v)
119{
120 __asm__ __volatile__("stc ccr,r1l\n\t"
121 "orc #0x80,ccr\n\t"
122 "mov.l %0,er0\n\t"
123 "and.l %1,er0\n\t"
124 "mov.l er0,%0\n\t"
125 "ldc r1l,ccr"
126 : "=m" (*v) : "g" (~(mask)) :"er0","er1");
127}
128
129static __inline__ void atomic_set_mask(unsigned long mask, unsigned long *v)
130{
131 __asm__ __volatile__("stc ccr,r1l\n\t"
132 "orc #0x80,ccr\n\t"
133 "mov.l %0,er0\n\t"
134 "or.l %1,er0\n\t"
135 "mov.l er0,%0\n\t"
136 "ldc r1l,ccr"
137 : "=m" (*v) : "g" (mask) :"er0","er1");
138}
139
140/* Atomic operations are already serializing */
141#define smp_mb__before_atomic_dec() barrier()
142#define smp_mb__after_atomic_dec() barrier()
143#define smp_mb__before_atomic_inc() barrier()
144#define smp_mb__after_atomic_inc() barrier()
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#endif /* __ARCH_H8300_ATOMIC __ */