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