spinlock.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Spinlock support for the Hexagon architecture
  3. *
  4. * Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.
  5. *
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 and
  9. * only version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19. * 02110-1301, USA.
  20. */
  21. #ifndef _ASM_SPINLOCK_H
  22. #define _ASM_SPINLOCK_H
  23. #include <asm/irqflags.h>
  24. #include <asm/barrier.h>
  25. #include <asm/processor.h>
  26. /*
  27. * This file is pulled in for SMP builds.
  28. * Really need to check all the barrier stuff for "true" SMP
  29. */
  30. /*
  31. * Read locks:
  32. * - load the lock value
  33. * - increment it
  34. * - if the lock value is still negative, go back and try again.
  35. * - unsuccessful store is unsuccessful. Go back and try again. Loser.
  36. * - successful store new lock value if positive -> lock acquired
  37. */
  38. static inline void arch_read_lock(arch_rwlock_t *lock)
  39. {
  40. __asm__ __volatile__(
  41. "1: R6 = memw_locked(%0);\n"
  42. " { P3 = cmp.ge(R6,#0); R6 = add(R6,#1);}\n"
  43. " { if !P3 jump 1b; }\n"
  44. " memw_locked(%0,P3) = R6;\n"
  45. " { if !P3 jump 1b; }\n"
  46. :
  47. : "r" (&lock->lock)
  48. : "memory", "r6", "p3"
  49. );
  50. }
  51. static inline void arch_read_unlock(arch_rwlock_t *lock)
  52. {
  53. __asm__ __volatile__(
  54. "1: R6 = memw_locked(%0);\n"
  55. " R6 = add(R6,#-1);\n"
  56. " memw_locked(%0,P3) = R6\n"
  57. " if !P3 jump 1b;\n"
  58. :
  59. : "r" (&lock->lock)
  60. : "memory", "r6", "p3"
  61. );
  62. }
  63. /* I think this returns 0 on fail, 1 on success. */
  64. static inline int arch_read_trylock(arch_rwlock_t *lock)
  65. {
  66. int temp;
  67. __asm__ __volatile__(
  68. " R6 = memw_locked(%1);\n"
  69. " { %0 = #0; P3 = cmp.ge(R6,#0); R6 = add(R6,#1);}\n"
  70. " { if !P3 jump 1f; }\n"
  71. " memw_locked(%1,P3) = R6;\n"
  72. " { %0 = P3 }\n"
  73. "1:\n"
  74. : "=&r" (temp)
  75. : "r" (&lock->lock)
  76. : "memory", "r6", "p3"
  77. );
  78. return temp;
  79. }
  80. /* Stuffs a -1 in the lock value? */
  81. static inline void arch_write_lock(arch_rwlock_t *lock)
  82. {
  83. __asm__ __volatile__(
  84. "1: R6 = memw_locked(%0)\n"
  85. " { P3 = cmp.eq(R6,#0); R6 = #-1;}\n"
  86. " { if !P3 jump 1b; }\n"
  87. " memw_locked(%0,P3) = R6;\n"
  88. " { if !P3 jump 1b; }\n"
  89. :
  90. : "r" (&lock->lock)
  91. : "memory", "r6", "p3"
  92. );
  93. }
  94. static inline int arch_write_trylock(arch_rwlock_t *lock)
  95. {
  96. int temp;
  97. __asm__ __volatile__(
  98. " R6 = memw_locked(%1)\n"
  99. " { %0 = #0; P3 = cmp.eq(R6,#0); R6 = #-1;}\n"
  100. " { if !P3 jump 1f; }\n"
  101. " memw_locked(%1,P3) = R6;\n"
  102. " %0 = P3;\n"
  103. "1:\n"
  104. : "=&r" (temp)
  105. : "r" (&lock->lock)
  106. : "memory", "r6", "p3"
  107. );
  108. return temp;
  109. }
  110. static inline void arch_write_unlock(arch_rwlock_t *lock)
  111. {
  112. smp_mb();
  113. lock->lock = 0;
  114. }
  115. static inline void arch_spin_lock(arch_spinlock_t *lock)
  116. {
  117. __asm__ __volatile__(
  118. "1: R6 = memw_locked(%0);\n"
  119. " P3 = cmp.eq(R6,#0);\n"
  120. " { if !P3 jump 1b; R6 = #1; }\n"
  121. " memw_locked(%0,P3) = R6;\n"
  122. " { if !P3 jump 1b; }\n"
  123. :
  124. : "r" (&lock->lock)
  125. : "memory", "r6", "p3"
  126. );
  127. }
  128. static inline void arch_spin_unlock(arch_spinlock_t *lock)
  129. {
  130. smp_mb();
  131. lock->lock = 0;
  132. }
  133. static inline unsigned int arch_spin_trylock(arch_spinlock_t *lock)
  134. {
  135. int temp;
  136. __asm__ __volatile__(
  137. " R6 = memw_locked(%1);\n"
  138. " P3 = cmp.eq(R6,#0);\n"
  139. " { if !P3 jump 1f; R6 = #1; %0 = #0; }\n"
  140. " memw_locked(%1,P3) = R6;\n"
  141. " %0 = P3;\n"
  142. "1:\n"
  143. : "=&r" (temp)
  144. : "r" (&lock->lock)
  145. : "memory", "r6", "p3"
  146. );
  147. return temp;
  148. }
  149. /*
  150. * SMP spinlocks are intended to allow only a single CPU at the lock
  151. */
  152. #define arch_spin_is_locked(x) ((x)->lock != 0)
  153. #endif