1 /*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (C) 1999, 2000 by Ralf Baechle
7 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
8 */
9 #ifndef _ASM_SPINLOCK_H
10 #define _ASM_SPINLOCK_H
11
12 /*
13 * Your basic SMP spinlocks, allowing only a single CPU anywhere
14 */
15
16 typedef struct {
17 volatile unsigned int lock;
18 } spinlock_t;
19
20 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 0 }
21
22 #define spin_lock_init(x) do { (x)->lock = 0; } while(0)
23
24 #define spin_is_locked(x) ((x)->lock != 0)
25 #define spin_unlock_wait(x) do { barrier(); } while ((x)->lock)
26
27 /*
28 * Simple spin lock operations. There are two variants, one clears IRQ's
29 * on the local processor, one does not.
30 *
31 * We make no fairness assumptions. They have a cost.
32 */
33
spin_lock(spinlock_t * lock)34 static inline void spin_lock(spinlock_t *lock)
35 {
36 unsigned int tmp;
37
38 __asm__ __volatile__(
39 ".set\tnoreorder\t\t\t# spin_lock\n"
40 "1:\tll\t%1, %2\n\t"
41 "bnez\t%1, 1b\n\t"
42 " li\t%1, 1\n\t"
43 "sc\t%1, %0\n\t"
44 "beqz\t%1, 1b\n\t"
45 " sync\n\t"
46 ".set\treorder"
47 : "=m" (lock->lock), "=&r" (tmp)
48 : "m" (lock->lock)
49 : "memory");
50 }
51
spin_unlock(spinlock_t * lock)52 static inline void spin_unlock(spinlock_t *lock)
53 {
54 __asm__ __volatile__(
55 ".set\tnoreorder\t\t\t# spin_unlock\n\t"
56 "sync\n\t"
57 "sw\t$0, %0\n\t"
58 ".set\treorder"
59 : "=m" (lock->lock)
60 : "m" (lock->lock)
61 : "memory");
62 }
63
spin_trylock(spinlock_t * lock)64 static inline unsigned int spin_trylock(spinlock_t *lock)
65 {
66 unsigned int temp, res;
67
68 __asm__ __volatile__(
69 ".set\tnoreorder\t\t\t# spin_trylock\n\t"
70 "1:\tll\t%0, %1\n\t"
71 "or\t%2, %0, %3\n\t"
72 "sc\t%2, %1\n\t"
73 "beqz\t%2, 1b\n\t"
74 " and\t%2, %0, %3\n\t"
75 ".set\treorder"
76 : "=&r" (temp), "=m" (lock->lock), "=&r" (res)
77 : "r" (1), "m" (lock->lock)
78 : "memory");
79
80 return res == 0;
81 }
82
83 /*
84 * Read-write spinlocks, allowing multiple readers but only one writer.
85 *
86 * NOTE! it is quite common to have readers in interrupts but no interrupt
87 * writers. For those circumstances we can "mix" irq-safe locks - any writer
88 * needs to get a irq-safe write-lock, but readers can get non-irqsafe
89 * read-locks.
90 */
91
92 typedef struct {
93 volatile unsigned int lock;
94 } rwlock_t;
95
96 #define RW_LOCK_UNLOCKED (rwlock_t) { 0 }
97
98 #define rwlock_init(x) do { *(x) = RW_LOCK_UNLOCKED; } while(0)
99
read_lock(rwlock_t * rw)100 static inline void read_lock(rwlock_t *rw)
101 {
102 unsigned int tmp;
103
104 __asm__ __volatile__(
105 ".set\tnoreorder\t\t\t# read_lock\n"
106 "1:\tll\t%1, %2\n\t"
107 "bltz\t%1, 1b\n\t"
108 " addu\t%1, 1\n\t"
109 "sc\t%1, %0\n\t"
110 "beqz\t%1, 1b\n\t"
111 " sync\n\t"
112 ".set\treorder"
113 : "=m" (rw->lock), "=&r" (tmp)
114 : "m" (rw->lock)
115 : "memory");
116 }
117
118 /* Note the use of sub, not subu which will make the kernel die with an
119 overflow exception if we ever try to unlock an rwlock that is already
120 unlocked or is being held by a writer. */
read_unlock(rwlock_t * rw)121 static inline void read_unlock(rwlock_t *rw)
122 {
123 unsigned int tmp;
124
125 __asm__ __volatile__(
126 ".set\tnoreorder\t\t\t# read_unlock\n"
127 "1:\tll\t%1, %2\n\t"
128 "sub\t%1, 1\n\t"
129 "sc\t%1, %0\n\t"
130 "beqz\t%1, 1b\n\t"
131 " sync\n\t"
132 ".set\treorder"
133 : "=m" (rw->lock), "=&r" (tmp)
134 : "m" (rw->lock)
135 : "memory");
136 }
137
write_lock(rwlock_t * rw)138 static inline void write_lock(rwlock_t *rw)
139 {
140 unsigned int tmp;
141
142 __asm__ __volatile__(
143 ".set\tnoreorder\t\t\t# write_lock\n"
144 "1:\tll\t%1, %2\n\t"
145 "bnez\t%1, 1b\n\t"
146 " lui\t%1, 0x8000\n\t"
147 "sc\t%1, %0\n\t"
148 "beqz\t%1, 1b\n\t"
149 " sync\n\t"
150 ".set\treorder"
151 : "=m" (rw->lock), "=&r" (tmp)
152 : "m" (rw->lock)
153 : "memory");
154 }
155
write_unlock(rwlock_t * rw)156 static inline void write_unlock(rwlock_t *rw)
157 {
158 __asm__ __volatile__(
159 ".set\tnoreorder\t\t\t# write_unlock\n\t"
160 "sync\n\t"
161 "sw\t$0, %0\n\t"
162 ".set\treorder"
163 : "=m" (rw->lock)
164 : "m" (rw->lock)
165 : "memory");
166 }
167
168 #endif /* _ASM_SPINLOCK_H */
169