1 /***********************license start***************
2 * Author: Cavium Networks
4 * Contact: support@caviumnetworks.com
5 * This file is part of the OCTEON SDK
7 * Copyright (c) 2003-2008 Cavium Networks
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.
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
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/.
24 * This file may also be available under a different license from Cavium.
25 * Contact Cavium Networks for more information
26 ***********************license end**************************************/
29 * Implementation of spinlocks for Octeon CVMX. Although similar in
30 * function to Linux kernel spinlocks, they are not compatible.
31 * Octeon CVMX spinlocks are only used to synchronize with the boot
32 * monitor and other non-Linux programs running in the system.
35 #ifndef __CVMX_SPINLOCK_H__
36 #define __CVMX_SPINLOCK_H__
38 #include <asm/octeon/cvmx-asm.h>
40 /* Spinlocks for Octeon */
42 /* define these to enable recursive spinlock debugging */
43 /*#define CVMX_SPINLOCK_DEBUG */
46 * Spinlocks for Octeon CVMX
49 volatile uint32_t value
;
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
56 #define CVMX_SPINLOCK_UNLOCKED_INITIALIZER {CVMX_SPINLOCK_UNLOCKED_VAL}
59 * Initialize a spinlock
61 * @lock: Lock to initialize
63 static inline void cvmx_spinlock_init(cvmx_spinlock_t
*lock
)
65 lock
->value
= CVMX_SPINLOCK_UNLOCKED_VAL
;
69 * Return non-zero if the spinlock is currently locked
71 * @lock: Lock to check
72 * Returns Non-zero if locked
74 static inline int cvmx_spinlock_locked(cvmx_spinlock_t
*lock
)
76 return lock
->value
!= CVMX_SPINLOCK_UNLOCKED_VAL
;
82 * @lock: pointer to lock structure
84 static inline void cvmx_spinlock_unlock(cvmx_spinlock_t
*lock
)
92 * Attempts to take the lock, but does not spin if lock is not available.
93 * May take some time to acquire the lock even if it is available
94 * due to the ll/sc not succeeding.
96 * @lock: pointer to lock structure
98 * Returns 0: lock successfully taken
99 * 1: lock not taken, held by someone else
100 * These return values match the Linux semantics.
103 static inline unsigned int cvmx_spinlock_trylock(cvmx_spinlock_t
*lock
)
107 __asm__
__volatile__(".set noreorder \n"
108 "1: ll %[tmp], %[val] \n"
109 /* if lock held, fail immediately */
110 " bnez %[tmp], 2f \n"
112 " sc %[tmp], %[val] \n"
113 " beqz %[tmp], 1b \n"
117 [val
] "+m"(lock
->value
), [tmp
] "=&r"(tmp
)
120 return tmp
!= 0; /* normalize to 0 or 1 */
124 * Gets lock, spins until lock is taken
126 * @lock: pointer to lock structure
128 static inline void cvmx_spinlock_lock(cvmx_spinlock_t
*lock
)
132 __asm__
__volatile__(".set noreorder \n"
133 "1: ll %[tmp], %[val] \n"
134 " bnez %[tmp], 1b \n"
136 " sc %[tmp], %[val] \n"
137 " beqz %[tmp], 1b \n"
140 [val
] "+m"(lock
->value
), [tmp
] "=&r"(tmp
)
145 /** ********************************************************************
147 * These spinlocks use a single bit (bit 31) of a 32 bit word for locking.
148 * The rest of the bits in the word are left undisturbed. This enables more
149 * compact data structures as only 1 bit is consumed for the lock.
154 * Gets lock, spins until lock is taken
155 * Preserves the low 31 bits of the 32 bit
156 * word used for the lock.
159 * @word: word to lock bit 31 of
161 static inline void cvmx_spinlock_bit_lock(uint32_t *word
)
166 __asm__
__volatile__(".set noreorder \n"
168 "1: ll %[tmp], %[val] \n"
169 " bbit1 %[tmp], 31, 1b \n"
171 " ins %[tmp], $at, 31, 1 \n"
172 " sc %[tmp], %[val] \n"
173 " beqz %[tmp], 1b \n"
177 [val
] "+m"(*word
), [tmp
] "=&r"(tmp
), [sav
] "=&r"(sav
)
183 * Attempts to get lock, returns immediately with success/failure
184 * Preserves the low 31 bits of the 32 bit
185 * word used for the lock.
188 * @word: word to lock bit 31 of
189 * Returns 0: lock successfully taken
190 * 1: lock not taken, held by someone else
191 * These return values match the Linux semantics.
193 static inline unsigned int cvmx_spinlock_bit_trylock(uint32_t *word
)
197 __asm__
__volatile__(".set noreorder\n\t"
199 "1: ll %[tmp], %[val] \n"
200 /* if lock held, fail immediately */
201 " bbit1 %[tmp], 31, 2f \n"
203 " ins %[tmp], $at, 31, 1 \n"
204 " sc %[tmp], %[val] \n"
205 " beqz %[tmp], 1b \n"
210 [val
] "+m"(*word
), [tmp
] "=&r"(tmp
)
213 return tmp
!= 0; /* normalize to 0 or 1 */
219 * Unconditionally clears bit 31 of the lock word. Note that this is
220 * done non-atomically, as this implementation assumes that the rest
221 * of the bits in the word are protected by the lock.
223 * @word: word to unlock bit 31 in
225 static inline void cvmx_spinlock_bit_unlock(uint32_t *word
)
228 *word
&= ~(1UL << 31);
232 #endif /* __CVMX_SPINLOCK_H__ */