local.h 7.8 KB

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