Merge tag 'backport/v3.14.24-ltsi-rc1/phy-rcar-gen2-usb-to-v3.15' into backport/v3...
[platform/adaptation/renesas_rcar/renesas_kernel.git] / arch / sparc / lib / atomic32.c
1 /*
2  * atomic32.c: 32-bit atomic_t implementation
3  *
4  * Copyright (C) 2004 Keith M Wesolowski
5  * Copyright (C) 2007 Kyle McMartin
6  * 
7  * Based on asm-parisc/atomic.h Copyright (C) 2000 Philipp Rumpf
8  */
9
10 #include <linux/atomic.h>
11 #include <linux/spinlock.h>
12 #include <linux/module.h>
13
14 #ifdef CONFIG_SMP
15 #define ATOMIC_HASH_SIZE        4
16 #define ATOMIC_HASH(a)  (&__atomic_hash[(((unsigned long)a)>>8) & (ATOMIC_HASH_SIZE-1)])
17
18 spinlock_t __atomic_hash[ATOMIC_HASH_SIZE] = {
19         [0 ... (ATOMIC_HASH_SIZE-1)] = __SPIN_LOCK_UNLOCKED(__atomic_hash)
20 };
21
22 #else /* SMP */
23
24 static DEFINE_SPINLOCK(dummy);
25 #define ATOMIC_HASH_SIZE        1
26 #define ATOMIC_HASH(a)          (&dummy)
27
28 #endif /* SMP */
29
30 int __atomic_add_return(int i, atomic_t *v)
31 {
32         int ret;
33         unsigned long flags;
34         spin_lock_irqsave(ATOMIC_HASH(v), flags);
35
36         ret = (v->counter += i);
37
38         spin_unlock_irqrestore(ATOMIC_HASH(v), flags);
39         return ret;
40 }
41 EXPORT_SYMBOL(__atomic_add_return);
42
43 int atomic_xchg(atomic_t *v, int new)
44 {
45         int ret;
46         unsigned long flags;
47
48         spin_lock_irqsave(ATOMIC_HASH(v), flags);
49         ret = v->counter;
50         v->counter = new;
51         spin_unlock_irqrestore(ATOMIC_HASH(v), flags);
52         return ret;
53 }
54 EXPORT_SYMBOL(atomic_xchg);
55
56 int atomic_cmpxchg(atomic_t *v, int old, int new)
57 {
58         int ret;
59         unsigned long flags;
60
61         spin_lock_irqsave(ATOMIC_HASH(v), flags);
62         ret = v->counter;
63         if (likely(ret == old))
64                 v->counter = new;
65
66         spin_unlock_irqrestore(ATOMIC_HASH(v), flags);
67         return ret;
68 }
69 EXPORT_SYMBOL(atomic_cmpxchg);
70
71 int __atomic_add_unless(atomic_t *v, int a, int u)
72 {
73         int ret;
74         unsigned long flags;
75
76         spin_lock_irqsave(ATOMIC_HASH(v), flags);
77         ret = v->counter;
78         if (ret != u)
79                 v->counter += a;
80         spin_unlock_irqrestore(ATOMIC_HASH(v), flags);
81         return ret;
82 }
83 EXPORT_SYMBOL(__atomic_add_unless);
84
85 /* Atomic operations are already serializing */
86 void atomic_set(atomic_t *v, int i)
87 {
88         unsigned long flags;
89
90         spin_lock_irqsave(ATOMIC_HASH(v), flags);
91         v->counter = i;
92         spin_unlock_irqrestore(ATOMIC_HASH(v), flags);
93 }
94 EXPORT_SYMBOL(atomic_set);
95
96 unsigned long ___set_bit(unsigned long *addr, unsigned long mask)
97 {
98         unsigned long old, flags;
99
100         spin_lock_irqsave(ATOMIC_HASH(addr), flags);
101         old = *addr;
102         *addr = old | mask;
103         spin_unlock_irqrestore(ATOMIC_HASH(addr), flags);
104
105         return old & mask;
106 }
107 EXPORT_SYMBOL(___set_bit);
108
109 unsigned long ___clear_bit(unsigned long *addr, unsigned long mask)
110 {
111         unsigned long old, flags;
112
113         spin_lock_irqsave(ATOMIC_HASH(addr), flags);
114         old = *addr;
115         *addr = old & ~mask;
116         spin_unlock_irqrestore(ATOMIC_HASH(addr), flags);
117
118         return old & mask;
119 }
120 EXPORT_SYMBOL(___clear_bit);
121
122 unsigned long ___change_bit(unsigned long *addr, unsigned long mask)
123 {
124         unsigned long old, flags;
125
126         spin_lock_irqsave(ATOMIC_HASH(addr), flags);
127         old = *addr;
128         *addr = old ^ mask;
129         spin_unlock_irqrestore(ATOMIC_HASH(addr), flags);
130
131         return old & mask;
132 }
133 EXPORT_SYMBOL(___change_bit);
134
135 unsigned long __cmpxchg_u32(volatile u32 *ptr, u32 old, u32 new)
136 {
137         unsigned long flags;
138         u32 prev;
139
140         spin_lock_irqsave(ATOMIC_HASH(ptr), flags);
141         if ((prev = *ptr) == old)
142                 *ptr = new;
143         spin_unlock_irqrestore(ATOMIC_HASH(ptr), flags);
144
145         return (unsigned long)prev;
146 }
147 EXPORT_SYMBOL(__cmpxchg_u32);
148
149 unsigned long __xchg_u32(volatile u32 *ptr, u32 new)
150 {
151         unsigned long flags;
152         u32 prev;
153
154         spin_lock_irqsave(ATOMIC_HASH(ptr), flags);
155         prev = *ptr;
156         *ptr = new;
157         spin_unlock_irqrestore(ATOMIC_HASH(ptr), flags);
158
159         return (unsigned long)prev;
160 }
161 EXPORT_SYMBOL(__xchg_u32);