Yoshinori Sato | d2a5f49 | 2015-05-11 02:20:06 +0900 | [diff] [blame] | 1 | #ifndef __ARCH_H8300_ATOMIC__ |
| 2 | #define __ARCH_H8300_ATOMIC__ |
| 3 | |
| 4 | #include <linux/types.h> |
| 5 | #include <asm/cmpxchg.h> |
| 6 | |
| 7 | /* |
| 8 | * Atomic operations that C can't guarantee us. Useful for |
| 9 | * resource counting etc.. |
| 10 | */ |
| 11 | |
| 12 | #define ATOMIC_INIT(i) { (i) } |
| 13 | |
Peter Zijlstra | 62e8a32 | 2015-09-18 11:13:10 +0200 | [diff] [blame] | 14 | #define atomic_read(v) READ_ONCE((v)->counter) |
| 15 | #define atomic_set(v, i) WRITE_ONCE(((v)->counter), (i)) |
Yoshinori Sato | d2a5f49 | 2015-05-11 02:20:06 +0900 | [diff] [blame] | 16 | |
| 17 | #include <linux/kernel.h> |
| 18 | |
Peter Zijlstra | 73ada37 | 2015-07-10 12:55:45 +0200 | [diff] [blame] | 19 | #define ATOMIC_OP_RETURN(op, c_op) \ |
| 20 | static inline int atomic_##op##_return(int i, atomic_t *v) \ |
| 21 | { \ |
| 22 | h8300flags flags; \ |
| 23 | int ret; \ |
| 24 | \ |
| 25 | flags = arch_local_irq_save(); \ |
| 26 | ret = v->counter c_op i; \ |
| 27 | arch_local_irq_restore(flags); \ |
| 28 | return ret; \ |
Yoshinori Sato | d2a5f49 | 2015-05-11 02:20:06 +0900 | [diff] [blame] | 29 | } |
| 30 | |
Peter Zijlstra | 0c074cb | 2016-04-18 01:16:08 +0200 | [diff] [blame] | 31 | #define ATOMIC_FETCH_OP(op, c_op) \ |
| 32 | static inline int atomic_fetch_##op(int i, atomic_t *v) \ |
| 33 | { \ |
| 34 | h8300flags flags; \ |
| 35 | int ret; \ |
| 36 | \ |
| 37 | flags = arch_local_irq_save(); \ |
| 38 | ret = v->counter; \ |
| 39 | v->counter c_op i; \ |
| 40 | arch_local_irq_restore(flags); \ |
| 41 | return ret; \ |
| 42 | } |
| 43 | |
Peter Zijlstra | 73ada37 | 2015-07-10 12:55:45 +0200 | [diff] [blame] | 44 | #define ATOMIC_OP(op, c_op) \ |
| 45 | static inline void atomic_##op(int i, atomic_t *v) \ |
| 46 | { \ |
| 47 | h8300flags flags; \ |
| 48 | \ |
| 49 | flags = arch_local_irq_save(); \ |
| 50 | v->counter c_op i; \ |
| 51 | arch_local_irq_restore(flags); \ |
| 52 | } |
| 53 | |
| 54 | ATOMIC_OP_RETURN(add, +=) |
| 55 | ATOMIC_OP_RETURN(sub, -=) |
| 56 | |
Peter Zijlstra | 0c074cb | 2016-04-18 01:16:08 +0200 | [diff] [blame] | 57 | #define ATOMIC_OPS(op, c_op) \ |
| 58 | ATOMIC_OP(op, c_op) \ |
| 59 | ATOMIC_FETCH_OP(op, c_op) |
| 60 | |
| 61 | ATOMIC_OPS(and, &=) |
| 62 | ATOMIC_OPS(or, |=) |
| 63 | ATOMIC_OPS(xor, ^=) |
| 64 | ATOMIC_OPS(add, +=) |
| 65 | ATOMIC_OPS(sub, -=) |
| 66 | |
| 67 | #undef ATOMIC_OPS |
Peter Zijlstra | 73ada37 | 2015-07-10 12:55:45 +0200 | [diff] [blame] | 68 | #undef ATOMIC_OP_RETURN |
| 69 | #undef ATOMIC_OP |
| 70 | |
Yoshinori Sato | d2a5f49 | 2015-05-11 02:20:06 +0900 | [diff] [blame] | 71 | #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0) |
Peter Zijlstra | 73ada37 | 2015-07-10 12:55:45 +0200 | [diff] [blame] | 72 | #define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0) |
Yoshinori Sato | d2a5f49 | 2015-05-11 02:20:06 +0900 | [diff] [blame] | 73 | |
Peter Zijlstra | 73ada37 | 2015-07-10 12:55:45 +0200 | [diff] [blame] | 74 | #define atomic_inc_return(v) atomic_add_return(1, v) |
| 75 | #define atomic_dec_return(v) atomic_sub_return(1, v) |
Yoshinori Sato | d2a5f49 | 2015-05-11 02:20:06 +0900 | [diff] [blame] | 76 | |
Peter Zijlstra | 73ada37 | 2015-07-10 12:55:45 +0200 | [diff] [blame] | 77 | #define atomic_inc(v) (void)atomic_inc_return(v) |
| 78 | #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0) |
Yoshinori Sato | d2a5f49 | 2015-05-11 02:20:06 +0900 | [diff] [blame] | 79 | |
Peter Zijlstra | 73ada37 | 2015-07-10 12:55:45 +0200 | [diff] [blame] | 80 | #define atomic_dec(v) (void)atomic_dec_return(v) |
| 81 | #define atomic_dec_and_test(v) (atomic_dec_return(v) == 0) |
Yoshinori Sato | d2a5f49 | 2015-05-11 02:20:06 +0900 | [diff] [blame] | 82 | |
| 83 | static inline int atomic_cmpxchg(atomic_t *v, int old, int new) |
| 84 | { |
| 85 | int ret; |
| 86 | h8300flags flags; |
| 87 | |
| 88 | flags = arch_local_irq_save(); |
| 89 | ret = v->counter; |
| 90 | if (likely(ret == old)) |
| 91 | v->counter = new; |
| 92 | arch_local_irq_restore(flags); |
| 93 | return ret; |
| 94 | } |
| 95 | |
| 96 | static inline int __atomic_add_unless(atomic_t *v, int a, int u) |
| 97 | { |
| 98 | int ret; |
| 99 | h8300flags flags; |
| 100 | |
| 101 | flags = arch_local_irq_save(); |
| 102 | ret = v->counter; |
| 103 | if (ret != u) |
| 104 | v->counter += a; |
| 105 | arch_local_irq_restore(flags); |
| 106 | return ret; |
| 107 | } |
| 108 | |
Yoshinori Sato | d2a5f49 | 2015-05-11 02:20:06 +0900 | [diff] [blame] | 109 | #endif /* __ARCH_H8300_ATOMIC __ */ |