percpu.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (C) 2013 ARM Ltd.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef __ASM_PERCPU_H
  17. #define __ASM_PERCPU_H
  18. static inline void set_my_cpu_offset(unsigned long off)
  19. {
  20. asm volatile("msr tpidr_el1, %0" :: "r" (off) : "memory");
  21. }
  22. static inline unsigned long __my_cpu_offset(void)
  23. {
  24. unsigned long off;
  25. /*
  26. * We want to allow caching the value, so avoid using volatile and
  27. * instead use a fake stack read to hazard against barrier().
  28. */
  29. asm("mrs %0, tpidr_el1" : "=r" (off) :
  30. "Q" (*(const unsigned long *)current_stack_pointer));
  31. return off;
  32. }
  33. #define __my_cpu_offset __my_cpu_offset()
  34. #define PERCPU_OP(op, asm_op) \
  35. static inline unsigned long __percpu_##op(void *ptr, \
  36. unsigned long val, int size) \
  37. { \
  38. unsigned long loop, ret; \
  39. \
  40. switch (size) { \
  41. case 1: \
  42. asm ("//__per_cpu_" #op "_1\n" \
  43. "1: ldxrb %w[ret], %[ptr]\n" \
  44. #asm_op " %w[ret], %w[ret], %w[val]\n" \
  45. " stxrb %w[loop], %w[ret], %[ptr]\n" \
  46. " cbnz %w[loop], 1b" \
  47. : [loop] "=&r" (loop), [ret] "=&r" (ret), \
  48. [ptr] "+Q"(*(u8 *)ptr) \
  49. : [val] "Ir" (val)); \
  50. break; \
  51. case 2: \
  52. asm ("//__per_cpu_" #op "_2\n" \
  53. "1: ldxrh %w[ret], %[ptr]\n" \
  54. #asm_op " %w[ret], %w[ret], %w[val]\n" \
  55. " stxrh %w[loop], %w[ret], %[ptr]\n" \
  56. " cbnz %w[loop], 1b" \
  57. : [loop] "=&r" (loop), [ret] "=&r" (ret), \
  58. [ptr] "+Q"(*(u16 *)ptr) \
  59. : [val] "Ir" (val)); \
  60. break; \
  61. case 4: \
  62. asm ("//__per_cpu_" #op "_4\n" \
  63. "1: ldxr %w[ret], %[ptr]\n" \
  64. #asm_op " %w[ret], %w[ret], %w[val]\n" \
  65. " stxr %w[loop], %w[ret], %[ptr]\n" \
  66. " cbnz %w[loop], 1b" \
  67. : [loop] "=&r" (loop), [ret] "=&r" (ret), \
  68. [ptr] "+Q"(*(u32 *)ptr) \
  69. : [val] "Ir" (val)); \
  70. break; \
  71. case 8: \
  72. asm ("//__per_cpu_" #op "_8\n" \
  73. "1: ldxr %[ret], %[ptr]\n" \
  74. #asm_op " %[ret], %[ret], %[val]\n" \
  75. " stxr %w[loop], %[ret], %[ptr]\n" \
  76. " cbnz %w[loop], 1b" \
  77. : [loop] "=&r" (loop), [ret] "=&r" (ret), \
  78. [ptr] "+Q"(*(u64 *)ptr) \
  79. : [val] "Ir" (val)); \
  80. break; \
  81. default: \
  82. ret = 0; \
  83. BUILD_BUG(); \
  84. } \
  85. \
  86. return ret; \
  87. }
  88. PERCPU_OP(add, add)
  89. PERCPU_OP(and, and)
  90. PERCPU_OP(or, orr)
  91. #undef PERCPU_OP
  92. static inline unsigned long __percpu_read(void *ptr, int size)
  93. {
  94. unsigned long ret;
  95. switch (size) {
  96. case 1:
  97. ret = ACCESS_ONCE(*(u8 *)ptr);
  98. break;
  99. case 2:
  100. ret = ACCESS_ONCE(*(u16 *)ptr);
  101. break;
  102. case 4:
  103. ret = ACCESS_ONCE(*(u32 *)ptr);
  104. break;
  105. case 8:
  106. ret = ACCESS_ONCE(*(u64 *)ptr);
  107. break;
  108. default:
  109. ret = 0;
  110. BUILD_BUG();
  111. }
  112. return ret;
  113. }
  114. static inline void __percpu_write(void *ptr, unsigned long val, int size)
  115. {
  116. switch (size) {
  117. case 1:
  118. ACCESS_ONCE(*(u8 *)ptr) = (u8)val;
  119. break;
  120. case 2:
  121. ACCESS_ONCE(*(u16 *)ptr) = (u16)val;
  122. break;
  123. case 4:
  124. ACCESS_ONCE(*(u32 *)ptr) = (u32)val;
  125. break;
  126. case 8:
  127. ACCESS_ONCE(*(u64 *)ptr) = (u64)val;
  128. break;
  129. default:
  130. BUILD_BUG();
  131. }
  132. }
  133. static inline unsigned long __percpu_xchg(void *ptr, unsigned long val,
  134. int size)
  135. {
  136. unsigned long ret, loop;
  137. switch (size) {
  138. case 1:
  139. asm ("//__percpu_xchg_1\n"
  140. "1: ldxrb %w[ret], %[ptr]\n"
  141. " stxrb %w[loop], %w[val], %[ptr]\n"
  142. " cbnz %w[loop], 1b"
  143. : [loop] "=&r"(loop), [ret] "=&r"(ret),
  144. [ptr] "+Q"(*(u8 *)ptr)
  145. : [val] "r" (val));
  146. break;
  147. case 2:
  148. asm ("//__percpu_xchg_2\n"
  149. "1: ldxrh %w[ret], %[ptr]\n"
  150. " stxrh %w[loop], %w[val], %[ptr]\n"
  151. " cbnz %w[loop], 1b"
  152. : [loop] "=&r"(loop), [ret] "=&r"(ret),
  153. [ptr] "+Q"(*(u16 *)ptr)
  154. : [val] "r" (val));
  155. break;
  156. case 4:
  157. asm ("//__percpu_xchg_4\n"
  158. "1: ldxr %w[ret], %[ptr]\n"
  159. " stxr %w[loop], %w[val], %[ptr]\n"
  160. " cbnz %w[loop], 1b"
  161. : [loop] "=&r"(loop), [ret] "=&r"(ret),
  162. [ptr] "+Q"(*(u32 *)ptr)
  163. : [val] "r" (val));
  164. break;
  165. case 8:
  166. asm ("//__percpu_xchg_8\n"
  167. "1: ldxr %[ret], %[ptr]\n"
  168. " stxr %w[loop], %[val], %[ptr]\n"
  169. " cbnz %w[loop], 1b"
  170. : [loop] "=&r"(loop), [ret] "=&r"(ret),
  171. [ptr] "+Q"(*(u64 *)ptr)
  172. : [val] "r" (val));
  173. break;
  174. default:
  175. ret = 0;
  176. BUILD_BUG();
  177. }
  178. return ret;
  179. }
  180. #define _percpu_read(pcp) \
  181. ({ \
  182. typeof(pcp) __retval; \
  183. preempt_disable(); \
  184. __retval = (typeof(pcp))__percpu_read(raw_cpu_ptr(&(pcp)), \
  185. sizeof(pcp)); \
  186. preempt_enable(); \
  187. __retval; \
  188. })
  189. #define _percpu_write(pcp, val) \
  190. do { \
  191. preempt_disable(); \
  192. __percpu_write(raw_cpu_ptr(&(pcp)), (unsigned long)(val), \
  193. sizeof(pcp)); \
  194. preempt_enable(); \
  195. } while(0) \
  196. #define _pcp_protect(operation, pcp, val) \
  197. ({ \
  198. typeof(pcp) __retval; \
  199. preempt_disable(); \
  200. __retval = (typeof(pcp))operation(raw_cpu_ptr(&(pcp)), \
  201. (val), sizeof(pcp)); \
  202. preempt_enable(); \
  203. __retval; \
  204. })
  205. #define _percpu_add(pcp, val) \
  206. _pcp_protect(__percpu_add, pcp, val)
  207. #define _percpu_add_return(pcp, val) _percpu_add(pcp, val)
  208. #define _percpu_and(pcp, val) \
  209. _pcp_protect(__percpu_and, pcp, val)
  210. #define _percpu_or(pcp, val) \
  211. _pcp_protect(__percpu_or, pcp, val)
  212. #define _percpu_xchg(pcp, val) (typeof(pcp)) \
  213. _pcp_protect(__percpu_xchg, pcp, (unsigned long)(val))
  214. #define this_cpu_add_1(pcp, val) _percpu_add(pcp, val)
  215. #define this_cpu_add_2(pcp, val) _percpu_add(pcp, val)
  216. #define this_cpu_add_4(pcp, val) _percpu_add(pcp, val)
  217. #define this_cpu_add_8(pcp, val) _percpu_add(pcp, val)
  218. #define this_cpu_add_return_1(pcp, val) _percpu_add_return(pcp, val)
  219. #define this_cpu_add_return_2(pcp, val) _percpu_add_return(pcp, val)
  220. #define this_cpu_add_return_4(pcp, val) _percpu_add_return(pcp, val)
  221. #define this_cpu_add_return_8(pcp, val) _percpu_add_return(pcp, val)
  222. #define this_cpu_and_1(pcp, val) _percpu_and(pcp, val)
  223. #define this_cpu_and_2(pcp, val) _percpu_and(pcp, val)
  224. #define this_cpu_and_4(pcp, val) _percpu_and(pcp, val)
  225. #define this_cpu_and_8(pcp, val) _percpu_and(pcp, val)
  226. #define this_cpu_or_1(pcp, val) _percpu_or(pcp, val)
  227. #define this_cpu_or_2(pcp, val) _percpu_or(pcp, val)
  228. #define this_cpu_or_4(pcp, val) _percpu_or(pcp, val)
  229. #define this_cpu_or_8(pcp, val) _percpu_or(pcp, val)
  230. #define this_cpu_read_1(pcp) _percpu_read(pcp)
  231. #define this_cpu_read_2(pcp) _percpu_read(pcp)
  232. #define this_cpu_read_4(pcp) _percpu_read(pcp)
  233. #define this_cpu_read_8(pcp) _percpu_read(pcp)
  234. #define this_cpu_write_1(pcp, val) _percpu_write(pcp, val)
  235. #define this_cpu_write_2(pcp, val) _percpu_write(pcp, val)
  236. #define this_cpu_write_4(pcp, val) _percpu_write(pcp, val)
  237. #define this_cpu_write_8(pcp, val) _percpu_write(pcp, val)
  238. #define this_cpu_xchg_1(pcp, val) _percpu_xchg(pcp, val)
  239. #define this_cpu_xchg_2(pcp, val) _percpu_xchg(pcp, val)
  240. #define this_cpu_xchg_4(pcp, val) _percpu_xchg(pcp, val)
  241. #define this_cpu_xchg_8(pcp, val) _percpu_xchg(pcp, val)
  242. #include <asm-generic/percpu.h>
  243. #endif /* __ASM_PERCPU_H */