Auto-update from upstream
[pandora-kernel.git] / include / asm-parisc / spinlock.h
1 #ifndef __ASM_SPINLOCK_H
2 #define __ASM_SPINLOCK_H
3
4 #include <asm/system.h>
5 #include <asm/processor.h>
6 #include <asm/spinlock_types.h>
7
8 static inline int __raw_spin_is_locked(raw_spinlock_t *x)
9 {
10         volatile unsigned int *a = __ldcw_align(x);
11         return *a == 0;
12 }
13
14 #define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock)
15 #define __raw_spin_unlock_wait(x) \
16                 do { cpu_relax(); } while (__raw_spin_is_locked(x))
17
18 static inline void __raw_spin_lock(raw_spinlock_t *x)
19 {
20         volatile unsigned int *a;
21
22         mb();
23         a = __ldcw_align(x);
24         while (__ldcw(a) == 0)
25                 while (*a == 0);
26         mb();
27 }
28
29 static inline void __raw_spin_unlock(raw_spinlock_t *x)
30 {
31         volatile unsigned int *a;
32         mb();
33         a = __ldcw_align(x);
34         *a = 1;
35         mb();
36 }
37
38 static inline int __raw_spin_trylock(raw_spinlock_t *x)
39 {
40         volatile unsigned int *a;
41         int ret;
42
43         mb();
44         a = __ldcw_align(x);
45         ret = __ldcw(a) != 0;
46         mb();
47
48         return ret;
49 }
50
51 /*
52  * Read-write spinlocks, allowing multiple readers
53  * but only one writer.
54  */
55
56 #define __raw_read_trylock(lock) generic__raw_read_trylock(lock)
57
58 /* read_lock, read_unlock are pretty straightforward.  Of course it somehow
59  * sucks we end up saving/restoring flags twice for read_lock_irqsave aso. */
60
61 static  __inline__ void __raw_read_lock(raw_rwlock_t *rw)
62 {
63         unsigned long flags;
64         local_irq_save(flags);
65         __raw_spin_lock(&rw->lock);
66
67         rw->counter++;
68
69         __raw_spin_unlock(&rw->lock);
70         local_irq_restore(flags);
71 }
72
73 static  __inline__ void __raw_read_unlock(raw_rwlock_t *rw)
74 {
75         unsigned long flags;
76         local_irq_save(flags);
77         __raw_spin_lock(&rw->lock);
78
79         rw->counter--;
80
81         __raw_spin_unlock(&rw->lock);
82         local_irq_restore(flags);
83 }
84
85 /* write_lock is less trivial.  We optimistically grab the lock and check
86  * if we surprised any readers.  If so we release the lock and wait till
87  * they're all gone before trying again
88  *
89  * Also note that we don't use the _irqsave / _irqrestore suffixes here.
90  * If we're called with interrupts enabled and we've got readers (or other
91  * writers) in interrupt handlers someone fucked up and we'd dead-lock
92  * sooner or later anyway.   prumpf */
93
94 static  __inline__ void __raw_write_lock(raw_rwlock_t *rw)
95 {
96 retry:
97         __raw_spin_lock(&rw->lock);
98
99         if(rw->counter != 0) {
100                 /* this basically never happens */
101                 __raw_spin_unlock(&rw->lock);
102
103                 while (rw->counter != 0)
104                         cpu_relax();
105
106                 goto retry;
107         }
108
109         /* got it.  now leave without unlocking */
110         rw->counter = -1; /* remember we are locked */
111 }
112
113 /* write_unlock is absolutely trivial - we don't have to wait for anything */
114
115 static  __inline__ void __raw_write_unlock(raw_rwlock_t *rw)
116 {
117         rw->counter = 0;
118         __raw_spin_unlock(&rw->lock);
119 }
120
121 static  __inline__ int __raw_write_trylock(raw_rwlock_t *rw)
122 {
123         __raw_spin_lock(&rw->lock);
124         if (rw->counter != 0) {
125                 /* this basically never happens */
126                 __raw_spin_unlock(&rw->lock);
127
128                 return 0;
129         }
130
131         /* got it.  now leave without unlocking */
132         rw->counter = -1; /* remember we are locked */
133         return 1;
134 }
135
136 static __inline__ int __raw_is_read_locked(raw_rwlock_t *rw)
137 {
138         return rw->counter > 0;
139 }
140
141 static __inline__ int __raw_is_write_locked(raw_rwlock_t *rw)
142 {
143         return rw->counter < 0;
144 }
145
146 #endif /* __ASM_SPINLOCK_H */