cvmx-spinlock.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /***********************license start***************
  2. * Author: Cavium Networks
  3. *
  4. * Contact: support@caviumnetworks.com
  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. /**
  28. * Implementation of spinlocks for Octeon CVMX. Although similar in
  29. * function to Linux kernel spinlocks, they are not compatible.
  30. * Octeon CVMX spinlocks are only used to synchronize with the boot
  31. * monitor and other non-Linux programs running in the system.
  32. */
  33. #ifndef __CVMX_SPINLOCK_H__
  34. #define __CVMX_SPINLOCK_H__
  35. #include <asm/octeon/cvmx-asm.h>
  36. /* Spinlocks for Octeon */
  37. /* define these to enable recursive spinlock debugging */
  38. /*#define CVMX_SPINLOCK_DEBUG */
  39. /**
  40. * Spinlocks for Octeon CVMX
  41. */
  42. typedef struct {
  43. volatile uint32_t value;
  44. } cvmx_spinlock_t;
  45. /* note - macros not expanded in inline ASM, so values hardcoded */
  46. #define CVMX_SPINLOCK_UNLOCKED_VAL 0
  47. #define CVMX_SPINLOCK_LOCKED_VAL 1
  48. #define CVMX_SPINLOCK_UNLOCKED_INITIALIZER {CVMX_SPINLOCK_UNLOCKED_VAL}
  49. /**
  50. * Initialize a spinlock
  51. *
  52. * @lock: Lock to initialize
  53. */
  54. static inline void cvmx_spinlock_init(cvmx_spinlock_t *lock)
  55. {
  56. lock->value = CVMX_SPINLOCK_UNLOCKED_VAL;
  57. }
  58. /**
  59. * Return non-zero if the spinlock is currently locked
  60. *
  61. * @lock: Lock to check
  62. * Returns Non-zero if locked
  63. */
  64. static inline int cvmx_spinlock_locked(cvmx_spinlock_t *lock)
  65. {
  66. return lock->value != CVMX_SPINLOCK_UNLOCKED_VAL;
  67. }
  68. /**
  69. * Releases lock
  70. *
  71. * @lock: pointer to lock structure
  72. */
  73. static inline void cvmx_spinlock_unlock(cvmx_spinlock_t *lock)
  74. {
  75. CVMX_SYNCWS;
  76. lock->value = 0;
  77. CVMX_SYNCWS;
  78. }
  79. /**
  80. * Attempts to take the lock, but does not spin if lock is not available.
  81. * May take some time to acquire the lock even if it is available
  82. * due to the ll/sc not succeeding.
  83. *
  84. * @lock: pointer to lock structure
  85. *
  86. * Returns 0: lock successfully taken
  87. * 1: lock not taken, held by someone else
  88. * These return values match the Linux semantics.
  89. */
  90. static inline unsigned int cvmx_spinlock_trylock(cvmx_spinlock_t *lock)
  91. {
  92. unsigned int tmp;
  93. __asm__ __volatile__(".set noreorder \n"
  94. "1: ll %[tmp], %[val] \n"
  95. /* if lock held, fail immediately */
  96. " bnez %[tmp], 2f \n"
  97. " li %[tmp], 1 \n"
  98. " sc %[tmp], %[val] \n"
  99. " beqz %[tmp], 1b \n"
  100. " li %[tmp], 0 \n"
  101. "2: \n"
  102. ".set reorder \n" :
  103. [val] "+m"(lock->value), [tmp] "=&r"(tmp)
  104. : : "memory");
  105. return tmp != 0; /* normalize to 0 or 1 */
  106. }
  107. /**
  108. * Gets lock, spins until lock is taken
  109. *
  110. * @lock: pointer to lock structure
  111. */
  112. static inline void cvmx_spinlock_lock(cvmx_spinlock_t *lock)
  113. {
  114. unsigned int tmp;
  115. __asm__ __volatile__(".set noreorder \n"
  116. "1: ll %[tmp], %[val] \n"
  117. " bnez %[tmp], 1b \n"
  118. " li %[tmp], 1 \n"
  119. " sc %[tmp], %[val] \n"
  120. " beqz %[tmp], 1b \n"
  121. " nop \n"
  122. ".set reorder \n" :
  123. [val] "+m"(lock->value), [tmp] "=&r"(tmp)
  124. : : "memory");
  125. }
  126. /** ********************************************************************
  127. * Bit spinlocks
  128. * These spinlocks use a single bit (bit 31) of a 32 bit word for locking.
  129. * The rest of the bits in the word are left undisturbed. This enables more
  130. * compact data structures as only 1 bit is consumed for the lock.
  131. *
  132. */
  133. /**
  134. * Gets lock, spins until lock is taken
  135. * Preserves the low 31 bits of the 32 bit
  136. * word used for the lock.
  137. *
  138. *
  139. * @word: word to lock bit 31 of
  140. */
  141. static inline void cvmx_spinlock_bit_lock(uint32_t *word)
  142. {
  143. unsigned int tmp;
  144. unsigned int sav;
  145. __asm__ __volatile__(".set noreorder \n"
  146. ".set noat \n"
  147. "1: ll %[tmp], %[val] \n"
  148. " bbit1 %[tmp], 31, 1b \n"
  149. " li $at, 1 \n"
  150. " ins %[tmp], $at, 31, 1 \n"
  151. " sc %[tmp], %[val] \n"
  152. " beqz %[tmp], 1b \n"
  153. " nop \n"
  154. ".set at \n"
  155. ".set reorder \n" :
  156. [val] "+m"(*word), [tmp] "=&r"(tmp), [sav] "=&r"(sav)
  157. : : "memory");
  158. }
  159. /**
  160. * Attempts to get lock, returns immediately with success/failure
  161. * Preserves the low 31 bits of the 32 bit
  162. * word used for the lock.
  163. *
  164. *
  165. * @word: word to lock bit 31 of
  166. * Returns 0: lock successfully taken
  167. * 1: lock not taken, held by someone else
  168. * These return values match the Linux semantics.
  169. */
  170. static inline unsigned int cvmx_spinlock_bit_trylock(uint32_t *word)
  171. {
  172. unsigned int tmp;
  173. __asm__ __volatile__(".set noreorder\n\t"
  174. ".set noat\n"
  175. "1: ll %[tmp], %[val] \n"
  176. /* if lock held, fail immediately */
  177. " bbit1 %[tmp], 31, 2f \n"
  178. " li $at, 1 \n"
  179. " ins %[tmp], $at, 31, 1 \n"
  180. " sc %[tmp], %[val] \n"
  181. " beqz %[tmp], 1b \n"
  182. " li %[tmp], 0 \n"
  183. "2: \n"
  184. ".set at \n"
  185. ".set reorder \n" :
  186. [val] "+m"(*word), [tmp] "=&r"(tmp)
  187. : : "memory");
  188. return tmp != 0; /* normalize to 0 or 1 */
  189. }
  190. /**
  191. * Releases bit lock
  192. *
  193. * Unconditionally clears bit 31 of the lock word. Note that this is
  194. * done non-atomically, as this implementation assumes that the rest
  195. * of the bits in the word are protected by the lock.
  196. *
  197. * @word: word to unlock bit 31 in
  198. */
  199. static inline void cvmx_spinlock_bit_unlock(uint32_t *word)
  200. {
  201. CVMX_SYNCWS;
  202. *word &= ~(1UL << 31);
  203. CVMX_SYNCWS;
  204. }
  205. #endif /* __CVMX_SPINLOCK_H__ */