percpu.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2013 ARM Ltd.
  4. */
  5. #ifndef __ASM_PERCPU_H
  6. #define __ASM_PERCPU_H
  7. #include <linux/preempt.h>
  8. #include <asm/alternative.h>
  9. #include <asm/cmpxchg.h>
  10. #include <asm/stack_pointer.h>
  11. #include <asm/sysreg.h>
  12. static inline void set_my_cpu_offset(unsigned long off)
  13. {
  14. asm volatile(ALTERNATIVE("msr tpidr_el1, %0",
  15. "msr tpidr_el2, %0",
  16. ARM64_HAS_VIRT_HOST_EXTN)
  17. :: "r" (off) : "memory");
  18. }
  19. static inline unsigned long __hyp_my_cpu_offset(void)
  20. {
  21. /*
  22. * Non-VHE hyp code runs with preemption disabled. No need to hazard
  23. * the register access against barrier() as in __kern_my_cpu_offset.
  24. */
  25. return read_sysreg(tpidr_el2);
  26. }
  27. static inline unsigned long __kern_my_cpu_offset(void)
  28. {
  29. unsigned long off;
  30. /*
  31. * We want to allow caching the value, so avoid using volatile and
  32. * instead use a fake stack read to hazard against barrier().
  33. */
  34. asm(ALTERNATIVE("mrs %0, tpidr_el1",
  35. "mrs %0, tpidr_el2",
  36. ARM64_HAS_VIRT_HOST_EXTN)
  37. : "=r" (off) :
  38. "Q" (*(const unsigned long *)current_stack_pointer));
  39. return off;
  40. }
  41. #ifdef __KVM_NVHE_HYPERVISOR__
  42. #define __my_cpu_offset __hyp_my_cpu_offset()
  43. #else
  44. #define __my_cpu_offset __kern_my_cpu_offset()
  45. #endif
  46. #define PERCPU_RW_OPS(sz) \
  47. static inline unsigned long __percpu_read_##sz(void *ptr) \
  48. { \
  49. return READ_ONCE(*(u##sz *)ptr); \
  50. } \
  51. \
  52. static inline void __percpu_write_##sz(void *ptr, unsigned long val) \
  53. { \
  54. WRITE_ONCE(*(u##sz *)ptr, (u##sz)val); \
  55. }
  56. #define __PERCPU_OP_CASE(w, sfx, name, sz, op_llsc, op_lse) \
  57. static inline void \
  58. __percpu_##name##_case_##sz(void *ptr, unsigned long val) \
  59. { \
  60. unsigned int loop; \
  61. u##sz tmp; \
  62. \
  63. asm volatile (ARM64_LSE_ATOMIC_INSN( \
  64. /* LL/SC */ \
  65. "1: ldxr" #sfx "\t%" #w "[tmp], %[ptr]\n" \
  66. #op_llsc "\t%" #w "[tmp], %" #w "[tmp], %" #w "[val]\n" \
  67. " stxr" #sfx "\t%w[loop], %" #w "[tmp], %[ptr]\n" \
  68. " cbnz %w[loop], 1b", \
  69. /* LSE atomics */ \
  70. #op_lse "\t%" #w "[val], %[ptr]\n" \
  71. __nops(3)) \
  72. : [loop] "=&r" (loop), [tmp] "=&r" (tmp), \
  73. [ptr] "+Q"(*(u##sz *)ptr) \
  74. : [val] "r" ((u##sz)(val))); \
  75. }
  76. #define __PERCPU_RET_OP_CASE(w, sfx, name, sz, op_llsc, op_lse) \
  77. static inline u##sz \
  78. __percpu_##name##_return_case_##sz(void *ptr, unsigned long val) \
  79. { \
  80. unsigned int loop; \
  81. u##sz ret; \
  82. \
  83. asm volatile (ARM64_LSE_ATOMIC_INSN( \
  84. /* LL/SC */ \
  85. "1: ldxr" #sfx "\t%" #w "[ret], %[ptr]\n" \
  86. #op_llsc "\t%" #w "[ret], %" #w "[ret], %" #w "[val]\n" \
  87. " stxr" #sfx "\t%w[loop], %" #w "[ret], %[ptr]\n" \
  88. " cbnz %w[loop], 1b", \
  89. /* LSE atomics */ \
  90. #op_lse "\t%" #w "[val], %" #w "[ret], %[ptr]\n" \
  91. #op_llsc "\t%" #w "[ret], %" #w "[ret], %" #w "[val]\n" \
  92. __nops(2)) \
  93. : [loop] "=&r" (loop), [ret] "=&r" (ret), \
  94. [ptr] "+Q"(*(u##sz *)ptr) \
  95. : [val] "r" ((u##sz)(val))); \
  96. \
  97. return ret; \
  98. }
  99. #define PERCPU_OP(name, op_llsc, op_lse) \
  100. __PERCPU_OP_CASE(w, b, name, 8, op_llsc, op_lse) \
  101. __PERCPU_OP_CASE(w, h, name, 16, op_llsc, op_lse) \
  102. __PERCPU_OP_CASE(w, , name, 32, op_llsc, op_lse) \
  103. __PERCPU_OP_CASE( , , name, 64, op_llsc, op_lse)
  104. #define PERCPU_RET_OP(name, op_llsc, op_lse) \
  105. __PERCPU_RET_OP_CASE(w, b, name, 8, op_llsc, op_lse) \
  106. __PERCPU_RET_OP_CASE(w, h, name, 16, op_llsc, op_lse) \
  107. __PERCPU_RET_OP_CASE(w, , name, 32, op_llsc, op_lse) \
  108. __PERCPU_RET_OP_CASE( , , name, 64, op_llsc, op_lse)
  109. PERCPU_RW_OPS(8)
  110. PERCPU_RW_OPS(16)
  111. PERCPU_RW_OPS(32)
  112. PERCPU_RW_OPS(64)
  113. PERCPU_OP(add, add, stadd)
  114. PERCPU_OP(andnot, bic, stclr)
  115. PERCPU_OP(or, orr, stset)
  116. PERCPU_RET_OP(add, add, ldadd)
  117. #undef PERCPU_RW_OPS
  118. #undef __PERCPU_OP_CASE
  119. #undef __PERCPU_RET_OP_CASE
  120. #undef PERCPU_OP
  121. #undef PERCPU_RET_OP
  122. /*
  123. * It would be nice to avoid the conditional call into the scheduler when
  124. * re-enabling preemption for preemptible kernels, but doing that in a way
  125. * which builds inside a module would mean messing directly with the preempt
  126. * count. If you do this, peterz and tglx will hunt you down.
  127. */
  128. #define this_cpu_cmpxchg_double_8(ptr1, ptr2, o1, o2, n1, n2) \
  129. ({ \
  130. int __ret; \
  131. preempt_disable_notrace(); \
  132. __ret = cmpxchg_double_local( raw_cpu_ptr(&(ptr1)), \
  133. raw_cpu_ptr(&(ptr2)), \
  134. o1, o2, n1, n2); \
  135. preempt_enable_notrace(); \
  136. __ret; \
  137. })
  138. #define _pcp_protect(op, pcp, ...) \
  139. ({ \
  140. preempt_disable_notrace(); \
  141. op(raw_cpu_ptr(&(pcp)), __VA_ARGS__); \
  142. preempt_enable_notrace(); \
  143. })
  144. #define _pcp_protect_return(op, pcp, args...) \
  145. ({ \
  146. typeof(pcp) __retval; \
  147. preempt_disable_notrace(); \
  148. __retval = (typeof(pcp))op(raw_cpu_ptr(&(pcp)), ##args); \
  149. preempt_enable_notrace(); \
  150. __retval; \
  151. })
  152. #define this_cpu_read_1(pcp) \
  153. _pcp_protect_return(__percpu_read_8, pcp)
  154. #define this_cpu_read_2(pcp) \
  155. _pcp_protect_return(__percpu_read_16, pcp)
  156. #define this_cpu_read_4(pcp) \
  157. _pcp_protect_return(__percpu_read_32, pcp)
  158. #define this_cpu_read_8(pcp) \
  159. _pcp_protect_return(__percpu_read_64, pcp)
  160. #define this_cpu_write_1(pcp, val) \
  161. _pcp_protect(__percpu_write_8, pcp, (unsigned long)val)
  162. #define this_cpu_write_2(pcp, val) \
  163. _pcp_protect(__percpu_write_16, pcp, (unsigned long)val)
  164. #define this_cpu_write_4(pcp, val) \
  165. _pcp_protect(__percpu_write_32, pcp, (unsigned long)val)
  166. #define this_cpu_write_8(pcp, val) \
  167. _pcp_protect(__percpu_write_64, pcp, (unsigned long)val)
  168. #define this_cpu_add_1(pcp, val) \
  169. _pcp_protect(__percpu_add_case_8, pcp, val)
  170. #define this_cpu_add_2(pcp, val) \
  171. _pcp_protect(__percpu_add_case_16, pcp, val)
  172. #define this_cpu_add_4(pcp, val) \
  173. _pcp_protect(__percpu_add_case_32, pcp, val)
  174. #define this_cpu_add_8(pcp, val) \
  175. _pcp_protect(__percpu_add_case_64, pcp, val)
  176. #define this_cpu_add_return_1(pcp, val) \
  177. _pcp_protect_return(__percpu_add_return_case_8, pcp, val)
  178. #define this_cpu_add_return_2(pcp, val) \
  179. _pcp_protect_return(__percpu_add_return_case_16, pcp, val)
  180. #define this_cpu_add_return_4(pcp, val) \
  181. _pcp_protect_return(__percpu_add_return_case_32, pcp, val)
  182. #define this_cpu_add_return_8(pcp, val) \
  183. _pcp_protect_return(__percpu_add_return_case_64, pcp, val)
  184. #define this_cpu_and_1(pcp, val) \
  185. _pcp_protect(__percpu_andnot_case_8, pcp, ~val)
  186. #define this_cpu_and_2(pcp, val) \
  187. _pcp_protect(__percpu_andnot_case_16, pcp, ~val)
  188. #define this_cpu_and_4(pcp, val) \
  189. _pcp_protect(__percpu_andnot_case_32, pcp, ~val)
  190. #define this_cpu_and_8(pcp, val) \
  191. _pcp_protect(__percpu_andnot_case_64, pcp, ~val)
  192. #define this_cpu_or_1(pcp, val) \
  193. _pcp_protect(__percpu_or_case_8, pcp, val)
  194. #define this_cpu_or_2(pcp, val) \
  195. _pcp_protect(__percpu_or_case_16, pcp, val)
  196. #define this_cpu_or_4(pcp, val) \
  197. _pcp_protect(__percpu_or_case_32, pcp, val)
  198. #define this_cpu_or_8(pcp, val) \
  199. _pcp_protect(__percpu_or_case_64, pcp, val)
  200. #define this_cpu_xchg_1(pcp, val) \
  201. _pcp_protect_return(xchg_relaxed, pcp, val)
  202. #define this_cpu_xchg_2(pcp, val) \
  203. _pcp_protect_return(xchg_relaxed, pcp, val)
  204. #define this_cpu_xchg_4(pcp, val) \
  205. _pcp_protect_return(xchg_relaxed, pcp, val)
  206. #define this_cpu_xchg_8(pcp, val) \
  207. _pcp_protect_return(xchg_relaxed, pcp, val)
  208. #define this_cpu_cmpxchg_1(pcp, o, n) \
  209. _pcp_protect_return(cmpxchg_relaxed, pcp, o, n)
  210. #define this_cpu_cmpxchg_2(pcp, o, n) \
  211. _pcp_protect_return(cmpxchg_relaxed, pcp, o, n)
  212. #define this_cpu_cmpxchg_4(pcp, o, n) \
  213. _pcp_protect_return(cmpxchg_relaxed, pcp, o, n)
  214. #define this_cpu_cmpxchg_8(pcp, o, n) \
  215. _pcp_protect_return(cmpxchg_relaxed, pcp, o, n)
  216. #ifdef __KVM_NVHE_HYPERVISOR__
  217. extern unsigned long __hyp_per_cpu_offset(unsigned int cpu);
  218. #define __per_cpu_offset
  219. #define per_cpu_offset(cpu) __hyp_per_cpu_offset((cpu))
  220. #endif
  221. #include <asm-generic/percpu.h>
  222. /* Redefine macros for nVHE hyp under DEBUG_PREEMPT to avoid its dependencies. */
  223. #if defined(__KVM_NVHE_HYPERVISOR__) && defined(CONFIG_DEBUG_PREEMPT)
  224. #undef this_cpu_ptr
  225. #define this_cpu_ptr raw_cpu_ptr
  226. #undef __this_cpu_read
  227. #define __this_cpu_read raw_cpu_read
  228. #undef __this_cpu_write
  229. #define __this_cpu_write raw_cpu_write
  230. #endif
  231. #endif /* __ASM_PERCPU_H */