Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef __ARCH_H8300_ATOMIC__ |
| 2 | #define __ARCH_H8300_ATOMIC__ |
| 3 | |
Matthew Wilcox | ea435467 | 2009-01-06 14:40:39 -0800 | [diff] [blame] | 4 | #include <linux/types.h> |
David Howells | a5401ee | 2012-03-28 18:30:02 +0100 | [diff] [blame] | 5 | #include <asm/cmpxchg.h> |
Matthew Wilcox | ea435467 | 2009-01-06 14:40:39 -0800 | [diff] [blame] | 6 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | /* |
| 8 | * Atomic operations that C can't guarantee us. Useful for |
| 9 | * resource counting etc.. |
| 10 | */ |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #define ATOMIC_INIT(i) { (i) } |
| 13 | |
Anton Blanchard | f3d46f9 | 2010-05-17 14:33:53 +1000 | [diff] [blame] | 14 | #define atomic_read(v) (*(volatile int *)&(v)->counter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #define atomic_set(v, i) (((v)->counter) = i) |
| 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/kernel.h> |
| 18 | |
| 19 | static __inline__ int atomic_add_return(int i, atomic_t *v) |
| 20 | { |
David Howells | 3ab61eb | 2010-08-31 16:52:11 +0100 | [diff] [blame] | 21 | unsigned long flags; |
| 22 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | 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 | |
| 32 | static __inline__ int atomic_sub_return(int i, atomic_t *v) |
| 33 | { |
David Howells | 3ab61eb | 2010-08-31 16:52:11 +0100 | [diff] [blame] | 34 | unsigned long flags; |
| 35 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | 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 Sato | b67405b | 2007-05-15 01:41:07 -0700 | [diff] [blame] | 43 | #define atomic_sub_and_test(i,v) (atomic_sub_return(i, v) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | static __inline__ int atomic_inc_return(atomic_t *v) |
| 46 | { |
David Howells | 3ab61eb | 2010-08-31 16:52:11 +0100 | [diff] [blame] | 47 | unsigned long flags; |
| 48 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | 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 | |
| 68 | static __inline__ int atomic_dec_return(atomic_t *v) |
| 69 | { |
David Howells | 3ab61eb | 2010-08-31 16:52:11 +0100 | [diff] [blame] | 70 | unsigned long flags; |
| 71 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | 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 | |
| 81 | static __inline__ int atomic_dec_and_test(atomic_t *v) |
| 82 | { |
David Howells | 3ab61eb | 2010-08-31 16:52:11 +0100 | [diff] [blame] | 83 | unsigned long flags; |
| 84 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | local_irq_save(flags); |
| 86 | --v->counter; |
| 87 | ret = v->counter; |
| 88 | local_irq_restore(flags); |
| 89 | return ret == 0; |
| 90 | } |
| 91 | |
Nick Piggin | 4a6dae6 | 2005-11-13 16:07:24 -0800 | [diff] [blame] | 92 | static 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 Sharma | f24219b | 2011-07-26 16:09:07 -0700 | [diff] [blame] | 105 | static inline int __atomic_add_unless(atomic_t *v, int a, int u) |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 106 | { |
| 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 Sharma | f24219b | 2011-07-26 16:09:07 -0700 | [diff] [blame] | 115 | return ret; |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 116 | } |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | static __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 | |
| 129 | static __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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | #endif /* __ARCH_H8300_ATOMIC __ */ |