Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cvmx-spinlock.h
Go to the documentation of this file.
1 /***********************license start***************
2  * Author: Cavium Networks
3  *
4  * Contact: [email protected]
5  * This file is part of the OCTEON SDK
6  *
7  * Copyright (c) 2003-2008 Cavium Networks
8  *
9  * This file is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License, Version 2, as
11  * published by the Free Software Foundation.
12  *
13  * This file is distributed in the hope that it will be useful, but
14  * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16  * NONINFRINGEMENT. See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this file; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22  * or visit http://www.gnu.org/licenses/.
23  *
24  * This file may also be available under a different license from Cavium.
25  * Contact Cavium Networks for more information
26  ***********************license end**************************************/
27 
35 #ifndef __CVMX_SPINLOCK_H__
36 #define __CVMX_SPINLOCK_H__
37 
38 #include <asm/octeon/cvmx-asm.h>
39 
40 /* Spinlocks for Octeon */
41 
42 /* define these to enable recursive spinlock debugging */
43 /*#define CVMX_SPINLOCK_DEBUG */
44 
48 typedef struct {
49  volatile uint32_t value;
51 
52 /* note - macros not expanded in inline ASM, so values hardcoded */
53 #define CVMX_SPINLOCK_UNLOCKED_VAL 0
54 #define CVMX_SPINLOCK_LOCKED_VAL 1
55 
56 #define CVMX_SPINLOCK_UNLOCKED_INITIALIZER {CVMX_SPINLOCK_UNLOCKED_VAL}
57 
63 static inline void cvmx_spinlock_init(cvmx_spinlock_t *lock)
64 {
66 }
67 
74 static inline int cvmx_spinlock_locked(cvmx_spinlock_t *lock)
75 {
76  return lock->value != CVMX_SPINLOCK_UNLOCKED_VAL;
77 }
78 
84 static inline void cvmx_spinlock_unlock(cvmx_spinlock_t *lock)
85 {
87  lock->value = 0;
89 }
90 
103 static inline unsigned int cvmx_spinlock_trylock(cvmx_spinlock_t *lock)
104 {
105  unsigned int tmp;
106 
107  __asm__ __volatile__(".set noreorder \n"
108  "1: ll %[tmp], %[val] \n"
109  /* if lock held, fail immediately */
110  " bnez %[tmp], 2f \n"
111  " li %[tmp], 1 \n"
112  " sc %[tmp], %[val] \n"
113  " beqz %[tmp], 1b \n"
114  " li %[tmp], 0 \n"
115  "2: \n"
116  ".set reorder \n" :
117  [val] "+m"(lock->value), [tmp] "=&r"(tmp)
118  : : "memory");
119 
120  return tmp != 0; /* normalize to 0 or 1 */
121 }
122 
128 static inline void cvmx_spinlock_lock(cvmx_spinlock_t *lock)
129 {
130  unsigned int tmp;
131 
132  __asm__ __volatile__(".set noreorder \n"
133  "1: ll %[tmp], %[val] \n"
134  " bnez %[tmp], 1b \n"
135  " li %[tmp], 1 \n"
136  " sc %[tmp], %[val] \n"
137  " beqz %[tmp], 1b \n"
138  " nop \n"
139  ".set reorder \n" :
140  [val] "+m"(lock->value), [tmp] "=&r"(tmp)
141  : : "memory");
142 
143 }
144 
161 static inline void cvmx_spinlock_bit_lock(uint32_t *word)
162 {
163  unsigned int tmp;
164  unsigned int sav;
165 
166  __asm__ __volatile__(".set noreorder \n"
167  ".set noat \n"
168  "1: ll %[tmp], %[val] \n"
169  " bbit1 %[tmp], 31, 1b \n"
170  " li $at, 1 \n"
171  " ins %[tmp], $at, 31, 1 \n"
172  " sc %[tmp], %[val] \n"
173  " beqz %[tmp], 1b \n"
174  " nop \n"
175  ".set at \n"
176  ".set reorder \n" :
177  [val] "+m"(*word), [tmp] "=&r"(tmp), [sav] "=&r"(sav)
178  : : "memory");
179 
180 }
181 
193 static inline unsigned int cvmx_spinlock_bit_trylock(uint32_t *word)
194 {
195  unsigned int tmp;
196 
197  __asm__ __volatile__(".set noreorder\n\t"
198  ".set noat\n"
199  "1: ll %[tmp], %[val] \n"
200  /* if lock held, fail immediately */
201  " bbit1 %[tmp], 31, 2f \n"
202  " li $at, 1 \n"
203  " ins %[tmp], $at, 31, 1 \n"
204  " sc %[tmp], %[val] \n"
205  " beqz %[tmp], 1b \n"
206  " li %[tmp], 0 \n"
207  "2: \n"
208  ".set at \n"
209  ".set reorder \n" :
210  [val] "+m"(*word), [tmp] "=&r"(tmp)
211  : : "memory");
212 
213  return tmp != 0; /* normalize to 0 or 1 */
214 }
215 
225 static inline void cvmx_spinlock_bit_unlock(uint32_t *word)
226 {
227  CVMX_SYNCWS;
228  *word &= ~(1UL << 31);
229  CVMX_SYNCWS;
230 }
231 
232 #endif /* __CVMX_SPINLOCK_H__ */