atomic_32.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. *
  14. * Do not include directly; use <asm/atomic.h>.
  15. */
  16. #ifndef _ASM_TILE_ATOMIC_32_H
  17. #define _ASM_TILE_ATOMIC_32_H
  18. #include <arch/chip.h>
  19. #ifndef __ASSEMBLY__
  20. /* Tile-specific routines to support <asm/atomic.h>. */
  21. int _atomic_xchg(atomic_t *v, int n);
  22. int _atomic_xchg_add(atomic_t *v, int i);
  23. int _atomic_xchg_add_unless(atomic_t *v, int a, int u);
  24. int _atomic_cmpxchg(atomic_t *v, int o, int n);
  25. /**
  26. * atomic_xchg - atomically exchange contents of memory with a new value
  27. * @v: pointer of type atomic_t
  28. * @i: integer value to store in memory
  29. *
  30. * Atomically sets @v to @i and returns old @v
  31. */
  32. static inline int atomic_xchg(atomic_t *v, int n)
  33. {
  34. smp_mb(); /* barrier for proper semantics */
  35. return _atomic_xchg(v, n);
  36. }
  37. /**
  38. * atomic_cmpxchg - atomically exchange contents of memory if it matches
  39. * @v: pointer of type atomic_t
  40. * @o: old value that memory should have
  41. * @n: new value to write to memory if it matches
  42. *
  43. * Atomically checks if @v holds @o and replaces it with @n if so.
  44. * Returns the old value at @v.
  45. */
  46. static inline int atomic_cmpxchg(atomic_t *v, int o, int n)
  47. {
  48. smp_mb(); /* barrier for proper semantics */
  49. return _atomic_cmpxchg(v, o, n);
  50. }
  51. /**
  52. * atomic_add - add integer to atomic variable
  53. * @i: integer value to add
  54. * @v: pointer of type atomic_t
  55. *
  56. * Atomically adds @i to @v.
  57. */
  58. static inline void atomic_add(int i, atomic_t *v)
  59. {
  60. _atomic_xchg_add(v, i);
  61. }
  62. /**
  63. * atomic_add_return - add integer and return
  64. * @v: pointer of type atomic_t
  65. * @i: integer value to add
  66. *
  67. * Atomically adds @i to @v and returns @i + @v
  68. */
  69. static inline int atomic_add_return(int i, atomic_t *v)
  70. {
  71. smp_mb(); /* barrier for proper semantics */
  72. return _atomic_xchg_add(v, i) + i;
  73. }
  74. /**
  75. * atomic_add_unless - add unless the number is already a given value
  76. * @v: pointer of type atomic_t
  77. * @a: the amount to add to v...
  78. * @u: ...unless v is equal to u.
  79. *
  80. * Atomically adds @a to @v, so long as @v was not already @u.
  81. * Returns non-zero if @v was not @u, and zero otherwise.
  82. */
  83. static inline int atomic_add_unless(atomic_t *v, int a, int u)
  84. {
  85. smp_mb(); /* barrier for proper semantics */
  86. return _atomic_xchg_add_unless(v, a, u) != u;
  87. }
  88. /**
  89. * atomic_set - set atomic variable
  90. * @v: pointer of type atomic_t
  91. * @i: required value
  92. *
  93. * Atomically sets the value of @v to @i.
  94. *
  95. * atomic_set() can't be just a raw store, since it would be lost if it
  96. * fell between the load and store of one of the other atomic ops.
  97. */
  98. static inline void atomic_set(atomic_t *v, int n)
  99. {
  100. _atomic_xchg(v, n);
  101. }
  102. /* A 64bit atomic type */
  103. typedef struct {
  104. u64 __aligned(8) counter;
  105. } atomic64_t;
  106. #define ATOMIC64_INIT(val) { (val) }
  107. u64 _atomic64_xchg(atomic64_t *v, u64 n);
  108. u64 _atomic64_xchg_add(atomic64_t *v, u64 i);
  109. u64 _atomic64_xchg_add_unless(atomic64_t *v, u64 a, u64 u);
  110. u64 _atomic64_cmpxchg(atomic64_t *v, u64 o, u64 n);
  111. /**
  112. * atomic64_read - read atomic variable
  113. * @v: pointer of type atomic64_t
  114. *
  115. * Atomically reads the value of @v.
  116. */
  117. static inline u64 atomic64_read(const atomic64_t *v)
  118. {
  119. /*
  120. * Requires an atomic op to read both 32-bit parts consistently.
  121. * Casting away const is safe since the atomic support routines
  122. * do not write to memory if the value has not been modified.
  123. */
  124. return _atomic64_xchg_add((atomic64_t *)v, 0);
  125. }
  126. /**
  127. * atomic64_xchg - atomically exchange contents of memory with a new value
  128. * @v: pointer of type atomic64_t
  129. * @i: integer value to store in memory
  130. *
  131. * Atomically sets @v to @i and returns old @v
  132. */
  133. static inline u64 atomic64_xchg(atomic64_t *v, u64 n)
  134. {
  135. smp_mb(); /* barrier for proper semantics */
  136. return _atomic64_xchg(v, n);
  137. }
  138. /**
  139. * atomic64_cmpxchg - atomically exchange contents of memory if it matches
  140. * @v: pointer of type atomic64_t
  141. * @o: old value that memory should have
  142. * @n: new value to write to memory if it matches
  143. *
  144. * Atomically checks if @v holds @o and replaces it with @n if so.
  145. * Returns the old value at @v.
  146. */
  147. static inline u64 atomic64_cmpxchg(atomic64_t *v, u64 o, u64 n)
  148. {
  149. smp_mb(); /* barrier for proper semantics */
  150. return _atomic64_cmpxchg(v, o, n);
  151. }
  152. /**
  153. * atomic64_add - add integer to atomic variable
  154. * @i: integer value to add
  155. * @v: pointer of type atomic64_t
  156. *
  157. * Atomically adds @i to @v.
  158. */
  159. static inline void atomic64_add(u64 i, atomic64_t *v)
  160. {
  161. _atomic64_xchg_add(v, i);
  162. }
  163. /**
  164. * atomic64_add_return - add integer and return
  165. * @v: pointer of type atomic64_t
  166. * @i: integer value to add
  167. *
  168. * Atomically adds @i to @v and returns @i + @v
  169. */
  170. static inline u64 atomic64_add_return(u64 i, atomic64_t *v)
  171. {
  172. smp_mb(); /* barrier for proper semantics */
  173. return _atomic64_xchg_add(v, i) + i;
  174. }
  175. /**
  176. * atomic64_add_unless - add unless the number is already a given value
  177. * @v: pointer of type atomic64_t
  178. * @a: the amount to add to v...
  179. * @u: ...unless v is equal to u.
  180. *
  181. * Atomically adds @a to @v, so long as @v was not already @u.
  182. * Returns non-zero if @v was not @u, and zero otherwise.
  183. */
  184. static inline u64 atomic64_add_unless(atomic64_t *v, u64 a, u64 u)
  185. {
  186. smp_mb(); /* barrier for proper semantics */
  187. return _atomic64_xchg_add_unless(v, a, u) != u;
  188. }
  189. /**
  190. * atomic64_set - set atomic variable
  191. * @v: pointer of type atomic64_t
  192. * @i: required value
  193. *
  194. * Atomically sets the value of @v to @i.
  195. *
  196. * atomic64_set() can't be just a raw store, since it would be lost if it
  197. * fell between the load and store of one of the other atomic ops.
  198. */
  199. static inline void atomic64_set(atomic64_t *v, u64 n)
  200. {
  201. _atomic64_xchg(v, n);
  202. }
  203. #define atomic64_add_negative(a, v) (atomic64_add_return((a), (v)) < 0)
  204. #define atomic64_inc(v) atomic64_add(1LL, (v))
  205. #define atomic64_inc_return(v) atomic64_add_return(1LL, (v))
  206. #define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
  207. #define atomic64_sub_return(i, v) atomic64_add_return(-(i), (v))
  208. #define atomic64_sub_and_test(a, v) (atomic64_sub_return((a), (v)) == 0)
  209. #define atomic64_sub(i, v) atomic64_add(-(i), (v))
  210. #define atomic64_dec(v) atomic64_sub(1LL, (v))
  211. #define atomic64_dec_return(v) atomic64_sub_return(1LL, (v))
  212. #define atomic64_dec_and_test(v) (atomic64_dec_return((v)) == 0)
  213. #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1LL, 0LL)
  214. /*
  215. * We need to barrier before modifying the word, since the _atomic_xxx()
  216. * routines just tns the lock and then read/modify/write of the word.
  217. * But after the word is updated, the routine issues an "mf" before returning,
  218. * and since it's a function call, we don't even need a compiler barrier.
  219. */
  220. #define smp_mb__before_atomic_dec() smp_mb()
  221. #define smp_mb__before_atomic_inc() smp_mb()
  222. #define smp_mb__after_atomic_dec() do { } while (0)
  223. #define smp_mb__after_atomic_inc() do { } while (0)
  224. #endif /* !__ASSEMBLY__ */
  225. /*
  226. * Internal definitions only beyond this point.
  227. */
  228. #define ATOMIC_LOCKS_FOUND_VIA_TABLE() \
  229. (!CHIP_HAS_CBOX_HOME_MAP() && defined(CONFIG_SMP))
  230. #if ATOMIC_LOCKS_FOUND_VIA_TABLE()
  231. /* Number of entries in atomic_lock_ptr[]. */
  232. #define ATOMIC_HASH_L1_SHIFT 6
  233. #define ATOMIC_HASH_L1_SIZE (1 << ATOMIC_HASH_L1_SHIFT)
  234. /* Number of locks in each struct pointed to by atomic_lock_ptr[]. */
  235. #define ATOMIC_HASH_L2_SHIFT (CHIP_L2_LOG_LINE_SIZE() - 2)
  236. #define ATOMIC_HASH_L2_SIZE (1 << ATOMIC_HASH_L2_SHIFT)
  237. #else /* ATOMIC_LOCKS_FOUND_VIA_TABLE() */
  238. /*
  239. * Number of atomic locks in atomic_locks[]. Must be a power of two.
  240. * There is no reason for more than PAGE_SIZE / 8 entries, since that
  241. * is the maximum number of pointer bits we can use to index this.
  242. * And we cannot have more than PAGE_SIZE / 4, since this has to
  243. * fit on a single page and each entry takes 4 bytes.
  244. */
  245. #define ATOMIC_HASH_SHIFT (PAGE_SHIFT - 3)
  246. #define ATOMIC_HASH_SIZE (1 << ATOMIC_HASH_SHIFT)
  247. #ifndef __ASSEMBLY__
  248. extern int atomic_locks[];
  249. #endif
  250. #endif /* ATOMIC_LOCKS_FOUND_VIA_TABLE() */
  251. /*
  252. * All the code that may fault while holding an atomic lock must
  253. * place the pointer to the lock in ATOMIC_LOCK_REG so the fault code
  254. * can correctly release and reacquire the lock. Note that we
  255. * mention the register number in a comment in "lib/atomic_asm.S" to help
  256. * assembly coders from using this register by mistake, so if it
  257. * is changed here, change that comment as well.
  258. */
  259. #define ATOMIC_LOCK_REG 20
  260. #define ATOMIC_LOCK_REG_NAME r20
  261. #ifndef __ASSEMBLY__
  262. /* Called from setup to initialize a hash table to point to per_cpu locks. */
  263. void __init_atomic_per_cpu(void);
  264. #ifdef CONFIG_SMP
  265. /* Support releasing the atomic lock in do_page_fault_ics(). */
  266. void __atomic_fault_unlock(int *lock_ptr);
  267. #endif
  268. /* Private helper routines in lib/atomic_asm_32.S */
  269. extern struct __get_user __atomic_cmpxchg(volatile int *p,
  270. int *lock, int o, int n);
  271. extern struct __get_user __atomic_xchg(volatile int *p, int *lock, int n);
  272. extern struct __get_user __atomic_xchg_add(volatile int *p, int *lock, int n);
  273. extern struct __get_user __atomic_xchg_add_unless(volatile int *p,
  274. int *lock, int o, int n);
  275. extern struct __get_user __atomic_or(volatile int *p, int *lock, int n);
  276. extern struct __get_user __atomic_andn(volatile int *p, int *lock, int n);
  277. extern struct __get_user __atomic_xor(volatile int *p, int *lock, int n);
  278. extern u64 __atomic64_cmpxchg(volatile u64 *p, int *lock, u64 o, u64 n);
  279. extern u64 __atomic64_xchg(volatile u64 *p, int *lock, u64 n);
  280. extern u64 __atomic64_xchg_add(volatile u64 *p, int *lock, u64 n);
  281. extern u64 __atomic64_xchg_add_unless(volatile u64 *p,
  282. int *lock, u64 o, u64 n);
  283. #endif /* !__ASSEMBLY__ */
  284. #endif /* _ASM_TILE_ATOMIC_32_H */