Root/target/linux/ubicom32/files/arch/ubicom32/include/asm/spinlock.h

1/*
2 * arch/ubicom32/include/asm/spinlock.h
3 * Spinlock related definitions for Ubicom32 architecture.
4 *
5 * (C) Copyright 2009, Ubicom, Inc.
6 *
7 * This file is part of the Ubicom32 Linux Kernel Port.
8 *
9 * The Ubicom32 Linux Kernel Port is free software: you can redistribute
10 * it and/or modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation, either version 2 of the
12 * License, or (at your option) any later version.
13 *
14 * The Ubicom32 Linux Kernel Port is distributed in the hope that it
15 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
16 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with the Ubicom32 Linux Kernel Port. If not,
21 * see <http://www.gnu.org/licenses/>.
22 *
23 * Ubicom32 implementation derived from (with many thanks):
24 * arch/m68knommu
25 * arch/blackfin
26 * arch/parisc
27 */
28#ifndef _ASM_UBICOM32_SPINLOCK_H
29#define _ASM_UBICOM32_SPINLOCK_H
30
31#include <asm/system.h>
32#include <asm/processor.h>
33#include <asm/spinlock_types.h>
34
35/*
36 * __raw_spin_lock()
37 * Lock the lock.
38 */
39static inline void __raw_spin_lock(raw_spinlock_t *x)
40{
41    asm volatile (
42    "1: bset %0, %0, #0 \n\t"
43    " jmpne.f 1b \n\t"
44        : "+U4" (x->lock)
45        :
46        : "memory", "cc"
47    );
48}
49
50/*
51 * __raw_spin_unlock()
52 * Unlock the lock.
53 */
54static inline void __raw_spin_unlock(raw_spinlock_t *x)
55{
56    asm volatile (
57    " bclr %0, %0, #0 \n\t"
58        : "+U4" (x->lock)
59        :
60        : "memory", "cc"
61    );
62}
63
64/*
65 * __raw_spin_is_locked()
66 * Test if the lock is locked.
67 */
68static inline int __raw_spin_is_locked(raw_spinlock_t *x)
69{
70    return x->lock;
71}
72
73/*
74 * __raw_spin_unlock_wait()
75 * Wait for the lock to be unlocked.
76 *
77 * Note: the caller has not guarantee that the lock will not
78 * be acquired before they get to it.
79 */
80static inline void __raw_spin_unlock_wait(raw_spinlock_t *x)
81{
82    do {
83        cpu_relax();
84    } while (__raw_spin_is_locked(x));
85}
86
87/*
88 * __raw_spin_trylock()
89 * Try the lock, return 0 on failure, 1 on success.
90 */
91static inline int __raw_spin_trylock(raw_spinlock_t *x)
92{
93    int ret = 0;
94
95    asm volatile (
96    " bset %1, %1, #0 \n\t"
97    " jmpne.f 1f \n\t"
98    " move.4 %0, #1 \n\t"
99    "1: \n\t"
100        : "+r" (ret), "+U4" (x->lock)
101        :
102        : "memory", "cc"
103    );
104
105    return ret;
106}
107
108/*
109 * __raw_spin_lock_flags()
110 * Spin waiting for the lock (enabling IRQ(s))
111 */
112static inline void __raw_spin_lock_flags(raw_spinlock_t *x, unsigned long flags)
113{
114    mb();
115    while (!__raw_spin_trylock(x)) {
116        /*
117         * If the flags from the IRQ are set, interrupts are disabled and we
118         * need to re-enable them.
119         */
120        if (!flags) {
121            cpu_relax();
122        } else {
123            raw_local_irq_enable();
124            cpu_relax();
125            raw_local_irq_disable();
126        }
127    }
128    mb();
129}
130
131/*
132 * Read-write spinlocks, allowing multiple readers but only one writer.
133 * Linux rwlocks are unfair to writers; they can be starved for an indefinite
134 * time by readers. With care, they can also be taken in interrupt context.
135 *
136 * In Ubicom32 architecture implementation, we have a spinlock and a counter.
137 * Readers use the lock to serialise their access to the counter (which
138 * records how many readers currently hold the lock).
139 * Writers hold the spinlock, preventing any readers or other writers from
140 * grabbing the rwlock.
141 */
142
143/*
144 * __raw_read_lock()
145 * Increment the counter in the rwlock.
146 *
147 * Note that we have to ensure interrupts are disabled in case we're
148 * interrupted by some other code that wants to grab the same read lock
149 */
150static inline void __raw_read_lock(raw_rwlock_t *rw)
151{
152    unsigned long flags;
153    raw_local_irq_save(flags);
154    __raw_spin_lock_flags(&rw->lock, flags);
155    rw->counter++;
156    __raw_spin_unlock(&rw->lock);
157    raw_local_irq_restore(flags);
158}
159
160/*
161 * __raw_read_unlock()
162 * Decrement the counter.
163 *
164 * Note that we have to ensure interrupts are disabled in case we're
165 * interrupted by some other code that wants to grab the same read lock
166 */
167static inline void __raw_read_unlock(raw_rwlock_t *rw)
168{
169    unsigned long flags;
170    raw_local_irq_save(flags);
171    __raw_spin_lock_flags(&rw->lock, flags);
172    rw->counter--;
173    __raw_spin_unlock(&rw->lock);
174    raw_local_irq_restore(flags);
175}
176
177/*
178 * __raw_read_trylock()
179 * Increment the counter if we can.
180 *
181 * Note that we have to ensure interrupts are disabled in case we're
182 * interrupted by some other code that wants to grab the same read lock
183 */
184static inline int __raw_read_trylock(raw_rwlock_t *rw)
185{
186    unsigned long flags;
187 retry:
188    raw_local_irq_save(flags);
189    if (__raw_spin_trylock(&rw->lock)) {
190        rw->counter++;
191        __raw_spin_unlock(&rw->lock);
192        raw_local_irq_restore(flags);
193        return 1;
194    }
195
196    raw_local_irq_restore(flags);
197
198    /*
199     * If write-locked, we fail to acquire the lock
200     */
201    if (rw->counter < 0) {
202        return 0;
203    }
204
205    /*
206     * Wait until we have a realistic chance at the lock
207     */
208    while (__raw_spin_is_locked(&rw->lock) && rw->counter >= 0) {
209        cpu_relax();
210    }
211
212    goto retry;
213}
214
215/*
216 * __raw_write_lock()
217 *
218 * Note that we have to ensure interrupts are disabled in case we're
219 * interrupted by some other code that wants to read_trylock() this lock
220 */
221static inline void __raw_write_lock(raw_rwlock_t *rw)
222{
223    unsigned long flags;
224retry:
225    raw_local_irq_save(flags);
226    __raw_spin_lock_flags(&rw->lock, flags);
227
228    if (rw->counter != 0) {
229        __raw_spin_unlock(&rw->lock);
230        raw_local_irq_restore(flags);
231
232        while (rw->counter != 0)
233            cpu_relax();
234
235        goto retry;
236    }
237
238    rw->counter = -1; /* mark as write-locked */
239    mb();
240    raw_local_irq_restore(flags);
241}
242
243static inline void __raw_write_unlock(raw_rwlock_t *rw)
244{
245    rw->counter = 0;
246    __raw_spin_unlock(&rw->lock);
247}
248
249/* Note that we have to ensure interrupts are disabled in case we're
250 * interrupted by some other code that wants to read_trylock() this lock */
251static inline int __raw_write_trylock(raw_rwlock_t *rw)
252{
253    unsigned long flags;
254    int result = 0;
255
256    raw_local_irq_save(flags);
257    if (__raw_spin_trylock(&rw->lock)) {
258        if (rw->counter == 0) {
259            rw->counter = -1;
260            result = 1;
261        } else {
262            /* Read-locked. Oh well. */
263            __raw_spin_unlock(&rw->lock);
264        }
265    }
266    raw_local_irq_restore(flags);
267
268    return result;
269}
270
271/*
272 * read_can_lock - would read_trylock() succeed?
273 * @lock: the rwlock in question.
274 */
275static inline int __raw_read_can_lock(raw_rwlock_t *rw)
276{
277    return rw->counter >= 0;
278}
279
280/*
281 * write_can_lock - would write_trylock() succeed?
282 * @lock: the rwlock in question.
283 */
284static inline int __raw_write_can_lock(raw_rwlock_t *rw)
285{
286    return !rw->counter;
287}
288
289#define __raw_read_lock_flags(lock, flags) __raw_read_lock(lock)
290#define __raw_write_lock_flags(lock, flags) __raw_write_lock(lock)
291
292#define _raw_spin_relax(lock) cpu_relax()
293#define _raw_read_relax(lock) cpu_relax()
294#define _raw_write_relax(lock) cpu_relax()
295
296#endif /* _ASM_UBICOM32_SPINLOCK_H */
297

Archive Download this file



interactive