percpu.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
  4. */
  5. #ifndef __ASM_PERCPU_H
  6. #define __ASM_PERCPU_H
  7. #include <asm/cmpxchg.h>
  8. #include <asm/loongarch.h>
  9. /*
  10. * The "address" (in fact, offset from $r21) of a per-CPU variable is close to
  11. * the loading address of main kernel image, but far from where the modules are
  12. * loaded. Tell the compiler this fact when using explicit relocs.
  13. */
  14. #if defined(MODULE) && defined(CONFIG_AS_HAS_EXPLICIT_RELOCS)
  15. # if __has_attribute(model)
  16. # define PER_CPU_ATTRIBUTES __attribute__((model("extreme")))
  17. # else
  18. # error compiler support for the model attribute is necessary when a recent assembler is used
  19. # endif
  20. #endif
  21. /* Use r21 for fast access */
  22. register unsigned long __my_cpu_offset __asm__("$r21");
  23. static inline void set_my_cpu_offset(unsigned long off)
  24. {
  25. __my_cpu_offset = off;
  26. csr_write64(off, PERCPU_BASE_KS);
  27. }
  28. #define __my_cpu_offset \
  29. ({ \
  30. __asm__ __volatile__("":"+r"(__my_cpu_offset)); \
  31. __my_cpu_offset; \
  32. })
  33. #define PERCPU_OP(op, asm_op, c_op) \
  34. static __always_inline unsigned long __percpu_##op(void *ptr, \
  35. unsigned long val, int size) \
  36. { \
  37. unsigned long ret; \
  38. \
  39. switch (size) { \
  40. case 4: \
  41. __asm__ __volatile__( \
  42. "am"#asm_op".w" " %[ret], %[val], %[ptr] \n" \
  43. : [ret] "=&r" (ret), [ptr] "+ZB"(*(u32 *)ptr) \
  44. : [val] "r" (val)); \
  45. break; \
  46. case 8: \
  47. __asm__ __volatile__( \
  48. "am"#asm_op".d" " %[ret], %[val], %[ptr] \n" \
  49. : [ret] "=&r" (ret), [ptr] "+ZB"(*(u64 *)ptr) \
  50. : [val] "r" (val)); \
  51. break; \
  52. default: \
  53. ret = 0; \
  54. BUILD_BUG(); \
  55. } \
  56. \
  57. return ret c_op val; \
  58. }
  59. PERCPU_OP(add, add, +)
  60. PERCPU_OP(and, and, &)
  61. PERCPU_OP(or, or, |)
  62. #undef PERCPU_OP
  63. static __always_inline unsigned long __percpu_xchg(void *ptr, unsigned long val, int size)
  64. {
  65. switch (size) {
  66. case 1:
  67. case 2:
  68. return __xchg_small((volatile void *)ptr, val, size);
  69. case 4:
  70. return __xchg_asm("amswap.w", (volatile u32 *)ptr, (u32)val);
  71. case 8:
  72. return __xchg_asm("amswap.d", (volatile u64 *)ptr, (u64)val);
  73. default:
  74. BUILD_BUG();
  75. }
  76. return 0;
  77. }
  78. #define __pcpu_op_1(op) op ".b "
  79. #define __pcpu_op_2(op) op ".h "
  80. #define __pcpu_op_4(op) op ".w "
  81. #define __pcpu_op_8(op) op ".d "
  82. #define _percpu_read(size, _pcp) \
  83. ({ \
  84. typeof(_pcp) __pcp_ret; \
  85. \
  86. __asm__ __volatile__( \
  87. __pcpu_op_##size("ldx") "%[ret], $r21, %[ptr] \n" \
  88. : [ret] "=&r"(__pcp_ret) \
  89. : [ptr] "r"(&(_pcp)) \
  90. : "memory"); \
  91. \
  92. __pcp_ret; \
  93. })
  94. #define _percpu_write(size, _pcp, _val) \
  95. do { \
  96. __asm__ __volatile__( \
  97. __pcpu_op_##size("stx") "%[val], $r21, %[ptr] \n" \
  98. : \
  99. : [val] "r"(_val), [ptr] "r"(&(_pcp)) \
  100. : "memory"); \
  101. } while (0)
  102. /* this_cpu_cmpxchg */
  103. #define _protect_cmpxchg_local(pcp, o, n) \
  104. ({ \
  105. typeof(*raw_cpu_ptr(&(pcp))) __ret; \
  106. preempt_disable_notrace(); \
  107. __ret = cmpxchg_local(raw_cpu_ptr(&(pcp)), o, n); \
  108. preempt_enable_notrace(); \
  109. __ret; \
  110. })
  111. #define _pcp_protect(operation, pcp, val) \
  112. ({ \
  113. typeof(pcp) __retval; \
  114. preempt_disable_notrace(); \
  115. __retval = (typeof(pcp))operation(raw_cpu_ptr(&(pcp)), \
  116. (val), sizeof(pcp)); \
  117. preempt_enable_notrace(); \
  118. __retval; \
  119. })
  120. #define _percpu_add(pcp, val) \
  121. _pcp_protect(__percpu_add, pcp, val)
  122. #define _percpu_add_return(pcp, val) _percpu_add(pcp, val)
  123. #define _percpu_and(pcp, val) \
  124. _pcp_protect(__percpu_and, pcp, val)
  125. #define _percpu_or(pcp, val) \
  126. _pcp_protect(__percpu_or, pcp, val)
  127. #define _percpu_xchg(pcp, val) ((typeof(pcp)) \
  128. _pcp_protect(__percpu_xchg, pcp, (unsigned long)(val)))
  129. #define this_cpu_add_4(pcp, val) _percpu_add(pcp, val)
  130. #define this_cpu_add_8(pcp, val) _percpu_add(pcp, val)
  131. #define this_cpu_add_return_4(pcp, val) _percpu_add_return(pcp, val)
  132. #define this_cpu_add_return_8(pcp, val) _percpu_add_return(pcp, val)
  133. #define this_cpu_and_4(pcp, val) _percpu_and(pcp, val)
  134. #define this_cpu_and_8(pcp, val) _percpu_and(pcp, val)
  135. #define this_cpu_or_4(pcp, val) _percpu_or(pcp, val)
  136. #define this_cpu_or_8(pcp, val) _percpu_or(pcp, val)
  137. #define this_cpu_read_1(pcp) _percpu_read(1, pcp)
  138. #define this_cpu_read_2(pcp) _percpu_read(2, pcp)
  139. #define this_cpu_read_4(pcp) _percpu_read(4, pcp)
  140. #define this_cpu_read_8(pcp) _percpu_read(8, pcp)
  141. #define this_cpu_write_1(pcp, val) _percpu_write(1, pcp, val)
  142. #define this_cpu_write_2(pcp, val) _percpu_write(2, pcp, val)
  143. #define this_cpu_write_4(pcp, val) _percpu_write(4, pcp, val)
  144. #define this_cpu_write_8(pcp, val) _percpu_write(8, pcp, val)
  145. #define this_cpu_xchg_1(pcp, val) _percpu_xchg(pcp, val)
  146. #define this_cpu_xchg_2(pcp, val) _percpu_xchg(pcp, val)
  147. #define this_cpu_xchg_4(pcp, val) _percpu_xchg(pcp, val)
  148. #define this_cpu_xchg_8(pcp, val) _percpu_xchg(pcp, val)
  149. #define this_cpu_cmpxchg_1(ptr, o, n) _protect_cmpxchg_local(ptr, o, n)
  150. #define this_cpu_cmpxchg_2(ptr, o, n) _protect_cmpxchg_local(ptr, o, n)
  151. #define this_cpu_cmpxchg_4(ptr, o, n) _protect_cmpxchg_local(ptr, o, n)
  152. #define this_cpu_cmpxchg_8(ptr, o, n) _protect_cmpxchg_local(ptr, o, n)
  153. #include <asm-generic/percpu.h>
  154. #endif /* __ASM_PERCPU_H */