local.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #ifndef __M32R_LOCAL_H
  2. #define __M32R_LOCAL_H
  3. /*
  4. * linux/include/asm-m32r/local.h
  5. *
  6. * M32R version:
  7. * Copyright (C) 2001, 2002 Hitoshi Yamamoto
  8. * Copyright (C) 2004 Hirokazu Takata <takata at linux-m32r.org>
  9. * Copyright (C) 2007 Mathieu Desnoyers <[email protected]>
  10. */
  11. #include <linux/percpu.h>
  12. #include <asm/assembler.h>
  13. #include <asm/local.h>
  14. /*
  15. * Atomic operations that C can't guarantee us. Useful for
  16. * resource counting etc..
  17. */
  18. /*
  19. * Make sure gcc doesn't try to be clever and move things around
  20. * on us. We need to use _exactly_ the address the user gave us,
  21. * not some alias that contains the same information.
  22. */
  23. typedef struct { volatile int counter; } local_t;
  24. #define LOCAL_INIT(i) { (i) }
  25. /**
  26. * local_read - read local variable
  27. * @l: pointer of type local_t
  28. *
  29. * Atomically reads the value of @l.
  30. */
  31. #define local_read(l) ((l)->counter)
  32. /**
  33. * local_set - set local variable
  34. * @l: pointer of type local_t
  35. * @i: required value
  36. *
  37. * Atomically sets the value of @l to @i.
  38. */
  39. #define local_set(l, i) (((l)->counter) = (i))
  40. /**
  41. * local_add_return - add long to local variable and return it
  42. * @i: long value to add
  43. * @l: pointer of type local_t
  44. *
  45. * Atomically adds @i to @l and return (@i + @l).
  46. */
  47. static inline long local_add_return(long i, local_t *l)
  48. {
  49. unsigned long flags;
  50. long result;
  51. local_irq_save(flags);
  52. __asm__ __volatile__ (
  53. "# local_add_return \n\t"
  54. DCACHE_CLEAR("%0", "r4", "%1")
  55. "ld %0, @%1; \n\t"
  56. "add %0, %2; \n\t"
  57. "st %0, @%1; \n\t"
  58. : "=&r" (result)
  59. : "r" (&l->counter), "r" (i)
  60. : "memory"
  61. #ifdef CONFIG_CHIP_M32700_TS1
  62. , "r4"
  63. #endif /* CONFIG_CHIP_M32700_TS1 */
  64. );
  65. local_irq_restore(flags);
  66. return result;
  67. }
  68. /**
  69. * local_sub_return - subtract long from local variable and return it
  70. * @i: long value to subtract
  71. * @l: pointer of type local_t
  72. *
  73. * Atomically subtracts @i from @l and return (@l - @i).
  74. */
  75. static inline long local_sub_return(long i, local_t *l)
  76. {
  77. unsigned long flags;
  78. long result;
  79. local_irq_save(flags);
  80. __asm__ __volatile__ (
  81. "# local_sub_return \n\t"
  82. DCACHE_CLEAR("%0", "r4", "%1")
  83. "ld %0, @%1; \n\t"
  84. "sub %0, %2; \n\t"
  85. "st %0, @%1; \n\t"
  86. : "=&r" (result)
  87. : "r" (&l->counter), "r" (i)
  88. : "memory"
  89. #ifdef CONFIG_CHIP_M32700_TS1
  90. , "r4"
  91. #endif /* CONFIG_CHIP_M32700_TS1 */
  92. );
  93. local_irq_restore(flags);
  94. return result;
  95. }
  96. /**
  97. * local_add - add long to local variable
  98. * @i: long value to add
  99. * @l: pointer of type local_t
  100. *
  101. * Atomically adds @i to @l.
  102. */
  103. #define local_add(i, l) ((void) local_add_return((i), (l)))
  104. /**
  105. * local_sub - subtract the local variable
  106. * @i: long value to subtract
  107. * @l: pointer of type local_t
  108. *
  109. * Atomically subtracts @i from @l.
  110. */
  111. #define local_sub(i, l) ((void) local_sub_return((i), (l)))
  112. /**
  113. * local_sub_and_test - subtract value from variable and test result
  114. * @i: integer value to subtract
  115. * @l: pointer of type local_t
  116. *
  117. * Atomically subtracts @i from @l and returns
  118. * true if the result is zero, or false for all
  119. * other cases.
  120. */
  121. #define local_sub_and_test(i, l) (local_sub_return((i), (l)) == 0)
  122. /**
  123. * local_inc_return - increment local variable and return it
  124. * @l: pointer of type local_t
  125. *
  126. * Atomically increments @l by 1 and returns the result.
  127. */
  128. static inline long local_inc_return(local_t *l)
  129. {
  130. unsigned long flags;
  131. long result;
  132. local_irq_save(flags);
  133. __asm__ __volatile__ (
  134. "# local_inc_return \n\t"
  135. DCACHE_CLEAR("%0", "r4", "%1")
  136. "ld %0, @%1; \n\t"
  137. "addi %0, #1; \n\t"
  138. "st %0, @%1; \n\t"
  139. : "=&r" (result)
  140. : "r" (&l->counter)
  141. : "memory"
  142. #ifdef CONFIG_CHIP_M32700_TS1
  143. , "r4"
  144. #endif /* CONFIG_CHIP_M32700_TS1 */
  145. );
  146. local_irq_restore(flags);
  147. return result;
  148. }
  149. /**
  150. * local_dec_return - decrement local variable and return it
  151. * @l: pointer of type local_t
  152. *
  153. * Atomically decrements @l by 1 and returns the result.
  154. */
  155. static inline long local_dec_return(local_t *l)
  156. {
  157. unsigned long flags;
  158. long result;
  159. local_irq_save(flags);
  160. __asm__ __volatile__ (
  161. "# local_dec_return \n\t"
  162. DCACHE_CLEAR("%0", "r4", "%1")
  163. "ld %0, @%1; \n\t"
  164. "addi %0, #-1; \n\t"
  165. "st %0, @%1; \n\t"
  166. : "=&r" (result)
  167. : "r" (&l->counter)
  168. : "memory"
  169. #ifdef CONFIG_CHIP_M32700_TS1
  170. , "r4"
  171. #endif /* CONFIG_CHIP_M32700_TS1 */
  172. );
  173. local_irq_restore(flags);
  174. return result;
  175. }
  176. /**
  177. * local_inc - increment local variable
  178. * @l: pointer of type local_t
  179. *
  180. * Atomically increments @l by 1.
  181. */
  182. #define local_inc(l) ((void)local_inc_return(l))
  183. /**
  184. * local_dec - decrement local variable
  185. * @l: pointer of type local_t
  186. *
  187. * Atomically decrements @l by 1.
  188. */
  189. #define local_dec(l) ((void)local_dec_return(l))
  190. /**
  191. * local_inc_and_test - increment and test
  192. * @l: pointer of type local_t
  193. *
  194. * Atomically increments @l by 1
  195. * and returns true if the result is zero, or false for all
  196. * other cases.
  197. */
  198. #define local_inc_and_test(l) (local_inc_return(l) == 0)
  199. /**
  200. * local_dec_and_test - decrement and test
  201. * @l: pointer of type local_t
  202. *
  203. * Atomically decrements @l by 1 and
  204. * returns true if the result is 0, or false for all
  205. * other cases.
  206. */
  207. #define local_dec_and_test(l) (local_dec_return(l) == 0)
  208. /**
  209. * local_add_negative - add and test if negative
  210. * @l: pointer of type local_t
  211. * @i: integer value to add
  212. *
  213. * Atomically adds @i to @l and returns true
  214. * if the result is negative, or false when
  215. * result is greater than or equal to zero.
  216. */
  217. #define local_add_negative(i, l) (local_add_return((i), (l)) < 0)
  218. #define local_cmpxchg(l, o, n) (cmpxchg_local(&((l)->counter), (o), (n)))
  219. #define local_xchg(v, new) (xchg_local(&((l)->counter), new))
  220. /**
  221. * local_add_unless - add unless the number is a given value
  222. * @l: pointer of type local_t
  223. * @a: the amount to add to l...
  224. * @u: ...unless l is equal to u.
  225. *
  226. * Atomically adds @a to @l, so long as it was not @u.
  227. * Returns non-zero if @l was not @u, and zero otherwise.
  228. */
  229. static inline int local_add_unless(local_t *l, long a, long u)
  230. {
  231. long c, old;
  232. c = local_read(l);
  233. for (;;) {
  234. if (unlikely(c == (u)))
  235. break;
  236. old = local_cmpxchg((l), c, c + (a));
  237. if (likely(old == c))
  238. break;
  239. c = old;
  240. }
  241. return c != (u);
  242. }
  243. #define local_inc_not_zero(l) local_add_unless((l), 1, 0)
  244. static inline void local_clear_mask(unsigned long mask, local_t *addr)
  245. {
  246. unsigned long flags;
  247. unsigned long tmp;
  248. local_irq_save(flags);
  249. __asm__ __volatile__ (
  250. "# local_clear_mask \n\t"
  251. DCACHE_CLEAR("%0", "r5", "%1")
  252. "ld %0, @%1; \n\t"
  253. "and %0, %2; \n\t"
  254. "st %0, @%1; \n\t"
  255. : "=&r" (tmp)
  256. : "r" (addr), "r" (~mask)
  257. : "memory"
  258. #ifdef CONFIG_CHIP_M32700_TS1
  259. , "r5"
  260. #endif /* CONFIG_CHIP_M32700_TS1 */
  261. );
  262. local_irq_restore(flags);
  263. }
  264. static inline void local_set_mask(unsigned long mask, local_t *addr)
  265. {
  266. unsigned long flags;
  267. unsigned long tmp;
  268. local_irq_save(flags);
  269. __asm__ __volatile__ (
  270. "# local_set_mask \n\t"
  271. DCACHE_CLEAR("%0", "r5", "%1")
  272. "ld %0, @%1; \n\t"
  273. "or %0, %2; \n\t"
  274. "st %0, @%1; \n\t"
  275. : "=&r" (tmp)
  276. : "r" (addr), "r" (mask)
  277. : "memory"
  278. #ifdef CONFIG_CHIP_M32700_TS1
  279. , "r5"
  280. #endif /* CONFIG_CHIP_M32700_TS1 */
  281. );
  282. local_irq_restore(flags);
  283. }
  284. /* Atomic operations are already serializing on m32r */
  285. #define smp_mb__before_local_dec() barrier()
  286. #define smp_mb__after_local_dec() barrier()
  287. #define smp_mb__before_local_inc() barrier()
  288. #define smp_mb__after_local_inc() barrier()
  289. /* Use these for per-cpu local_t variables: on some archs they are
  290. * much more efficient than these naive implementations. Note they take
  291. * a variable, not an address.
  292. */
  293. #define __local_inc(l) ((l)->a.counter++)
  294. #define __local_dec(l) ((l)->a.counter++)
  295. #define __local_add(i, l) ((l)->a.counter += (i))
  296. #define __local_sub(i, l) ((l)->a.counter -= (i))
  297. /* Use these for per-cpu local_t variables: on some archs they are
  298. * much more efficient than these naive implementations. Note they take
  299. * a variable, not an address.
  300. */
  301. #endif /* __M32R_LOCAL_H */