atomic.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Atomic operations 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_ATOMIC_H
  22. #define _ASM_ATOMIC_H
  23. #include <linux/types.h>
  24. #include <asm/cmpxchg.h>
  25. #define ATOMIC_INIT(i) { (i) }
  26. #define atomic_set(v, i) ((v)->counter = (i))
  27. /**
  28. * atomic_read - reads a word, atomically
  29. * @v: pointer to atomic value
  30. *
  31. * Assumes all word reads on our architecture are atomic.
  32. */
  33. #define atomic_read(v) ((v)->counter)
  34. /**
  35. * atomic_xchg - atomic
  36. * @v: pointer to memory to change
  37. * @new: new value (technically passed in a register -- see xchg)
  38. */
  39. #define atomic_xchg(v, new) (xchg(&((v)->counter), (new)))
  40. /**
  41. * atomic_cmpxchg - atomic compare-and-exchange values
  42. * @v: pointer to value to change
  43. * @old: desired old value to match
  44. * @new: new value to put in
  45. *
  46. * Parameters are then pointer, value-in-register, value-in-register,
  47. * and the output is the old value.
  48. *
  49. * Apparently this is complicated for archs that don't support
  50. * the memw_locked like we do (or it's broken or whatever).
  51. *
  52. * Kind of the lynchpin of the rest of the generically defined routines.
  53. * Remember V2 had that bug with dotnew predicate set by memw_locked.
  54. *
  55. * "old" is "expected" old val, __oldval is actual old value
  56. */
  57. static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
  58. {
  59. int __oldval;
  60. asm volatile(
  61. "1: %0 = memw_locked(%1);\n"
  62. " { P0 = cmp.eq(%0,%2);\n"
  63. " if (!P0.new) jump:nt 2f; }\n"
  64. " memw_locked(%1,P0) = %3;\n"
  65. " if (!P0) jump 1b;\n"
  66. "2:\n"
  67. : "=&r" (__oldval)
  68. : "r" (&v->counter), "r" (old), "r" (new)
  69. : "memory", "p0"
  70. );
  71. return __oldval;
  72. }
  73. static inline int atomic_add_return(int i, atomic_t *v)
  74. {
  75. int output;
  76. __asm__ __volatile__ (
  77. "1: %0 = memw_locked(%1);\n"
  78. " %0 = add(%0,%2);\n"
  79. " memw_locked(%1,P3)=%0;\n"
  80. " if !P3 jump 1b;\n"
  81. : "=&r" (output)
  82. : "r" (&v->counter), "r" (i)
  83. : "memory", "p3"
  84. );
  85. return output;
  86. }
  87. #define atomic_add(i, v) atomic_add_return(i, (v))
  88. static inline int atomic_sub_return(int i, atomic_t *v)
  89. {
  90. int output;
  91. __asm__ __volatile__ (
  92. "1: %0 = memw_locked(%1);\n"
  93. " %0 = sub(%0,%2);\n"
  94. " memw_locked(%1,P3)=%0\n"
  95. " if !P3 jump 1b;\n"
  96. : "=&r" (output)
  97. : "r" (&v->counter), "r" (i)
  98. : "memory", "p3"
  99. );
  100. return output;
  101. }
  102. #define atomic_sub(i, v) atomic_sub_return(i, (v))
  103. /**
  104. * atomic_add_unless - add unless the number is a given value
  105. * @v: pointer to value
  106. * @a: amount to add
  107. * @u: unless value is equal to u
  108. *
  109. * Returns 1 if the add happened, 0 if it didn't.
  110. */
  111. static inline int __atomic_add_unless(atomic_t *v, int a, int u)
  112. {
  113. int output, __oldval;
  114. asm volatile(
  115. "1: %0 = memw_locked(%2);"
  116. " {"
  117. " p3 = cmp.eq(%0, %4);"
  118. " if (p3.new) jump:nt 2f;"
  119. " %0 = add(%0, %3);"
  120. " %1 = #0;"
  121. " }"
  122. " memw_locked(%2, p3) = %0;"
  123. " {"
  124. " if !p3 jump 1b;"
  125. " %1 = #1;"
  126. " }"
  127. "2:"
  128. : "=&r" (__oldval), "=&r" (output)
  129. : "r" (v), "r" (a), "r" (u)
  130. : "memory", "p3"
  131. );
  132. return output;
  133. }
  134. #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
  135. #define atomic_inc(v) atomic_add(1, (v))
  136. #define atomic_dec(v) atomic_sub(1, (v))
  137. #define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
  138. #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
  139. #define atomic_sub_and_test(i, v) (atomic_sub_return(i, (v)) == 0)
  140. #define atomic_add_negative(i, v) (atomic_add_return(i, (v)) < 0)
  141. #define atomic_inc_return(v) (atomic_add_return(1, v))
  142. #define atomic_dec_return(v) (atomic_sub_return(1, v))
  143. #endif