percpu.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #ifndef _ASM_X86_PERCPU_H
  2. #define _ASM_X86_PERCPU_H
  3. #ifdef CONFIG_X86_64
  4. #define __percpu_seg gs
  5. #define __percpu_mov_op movq
  6. #else
  7. #define __percpu_seg fs
  8. #define __percpu_mov_op movl
  9. #endif
  10. #ifdef __ASSEMBLY__
  11. /*
  12. * PER_CPU finds an address of a per-cpu variable.
  13. *
  14. * Args:
  15. * var - variable name
  16. * reg - 32bit register
  17. *
  18. * The resulting address is stored in the "reg" argument.
  19. *
  20. * Example:
  21. * PER_CPU(cpu_gdt_descr, %ebx)
  22. */
  23. #ifdef CONFIG_SMP
  24. #define PER_CPU(var, reg) \
  25. __percpu_mov_op %__percpu_seg:per_cpu__this_cpu_off, reg; \
  26. lea per_cpu__##var(reg), reg
  27. #define PER_CPU_VAR(var) %__percpu_seg:per_cpu__##var
  28. #else /* ! SMP */
  29. #define PER_CPU(var, reg) \
  30. __percpu_mov_op $per_cpu__##var, reg
  31. #define PER_CPU_VAR(var) per_cpu__##var
  32. #endif /* SMP */
  33. #ifdef CONFIG_X86_64_SMP
  34. #define INIT_PER_CPU_VAR(var) init_per_cpu__##var
  35. #else
  36. #define INIT_PER_CPU_VAR(var) per_cpu__##var
  37. #endif
  38. #else /* ...!ASSEMBLY */
  39. #include <linux/stringify.h>
  40. #include <asm/sections.h>
  41. #define __addr_to_pcpu_ptr(addr) \
  42. (void *)((unsigned long)(addr) - (unsigned long)pcpu_base_addr \
  43. + (unsigned long)__per_cpu_start)
  44. #define __pcpu_ptr_to_addr(ptr) \
  45. (void *)((unsigned long)(ptr) + (unsigned long)pcpu_base_addr \
  46. - (unsigned long)__per_cpu_start)
  47. #ifdef CONFIG_SMP
  48. #define __percpu_arg(x) "%%"__stringify(__percpu_seg)":%P" #x
  49. #define __my_cpu_offset percpu_read(this_cpu_off)
  50. #else
  51. #define __percpu_arg(x) "%" #x
  52. #endif
  53. /*
  54. * Initialized pointers to per-cpu variables needed for the boot
  55. * processor need to use these macros to get the proper address
  56. * offset from __per_cpu_load on SMP.
  57. *
  58. * There also must be an entry in vmlinux_64.lds.S
  59. */
  60. #define DECLARE_INIT_PER_CPU(var) \
  61. extern typeof(per_cpu_var(var)) init_per_cpu_var(var)
  62. #ifdef CONFIG_X86_64_SMP
  63. #define init_per_cpu_var(var) init_per_cpu__##var
  64. #else
  65. #define init_per_cpu_var(var) per_cpu_var(var)
  66. #endif
  67. /* For arch-specific code, we can use direct single-insn ops (they
  68. * don't give an lvalue though). */
  69. extern void __bad_percpu_size(void);
  70. #define percpu_to_op(op, var, val) \
  71. do { \
  72. typedef typeof(var) T__; \
  73. if (0) { \
  74. T__ tmp__; \
  75. tmp__ = (val); \
  76. } \
  77. switch (sizeof(var)) { \
  78. case 1: \
  79. asm(op "b %1,"__percpu_arg(0) \
  80. : "+m" (var) \
  81. : "ri" ((T__)val)); \
  82. break; \
  83. case 2: \
  84. asm(op "w %1,"__percpu_arg(0) \
  85. : "+m" (var) \
  86. : "ri" ((T__)val)); \
  87. break; \
  88. case 4: \
  89. asm(op "l %1,"__percpu_arg(0) \
  90. : "+m" (var) \
  91. : "ri" ((T__)val)); \
  92. break; \
  93. case 8: \
  94. asm(op "q %1,"__percpu_arg(0) \
  95. : "+m" (var) \
  96. : "re" ((T__)val)); \
  97. break; \
  98. default: __bad_percpu_size(); \
  99. } \
  100. } while (0)
  101. #define percpu_from_op(op, var) \
  102. ({ \
  103. typeof(var) ret__; \
  104. switch (sizeof(var)) { \
  105. case 1: \
  106. asm(op "b "__percpu_arg(1)",%0" \
  107. : "=r" (ret__) \
  108. : "m" (var)); \
  109. break; \
  110. case 2: \
  111. asm(op "w "__percpu_arg(1)",%0" \
  112. : "=r" (ret__) \
  113. : "m" (var)); \
  114. break; \
  115. case 4: \
  116. asm(op "l "__percpu_arg(1)",%0" \
  117. : "=r" (ret__) \
  118. : "m" (var)); \
  119. break; \
  120. case 8: \
  121. asm(op "q "__percpu_arg(1)",%0" \
  122. : "=r" (ret__) \
  123. : "m" (var)); \
  124. break; \
  125. default: __bad_percpu_size(); \
  126. } \
  127. ret__; \
  128. })
  129. #define percpu_read(var) percpu_from_op("mov", per_cpu__##var)
  130. #define percpu_write(var, val) percpu_to_op("mov", per_cpu__##var, val)
  131. #define percpu_add(var, val) percpu_to_op("add", per_cpu__##var, val)
  132. #define percpu_sub(var, val) percpu_to_op("sub", per_cpu__##var, val)
  133. #define percpu_and(var, val) percpu_to_op("and", per_cpu__##var, val)
  134. #define percpu_or(var, val) percpu_to_op("or", per_cpu__##var, val)
  135. #define percpu_xor(var, val) percpu_to_op("xor", per_cpu__##var, val)
  136. /* This is not atomic against other CPUs -- CPU preemption needs to be off */
  137. #define x86_test_and_clear_bit_percpu(bit, var) \
  138. ({ \
  139. int old__; \
  140. asm volatile("btr %2,"__percpu_arg(1)"\n\tsbbl %0,%0" \
  141. : "=r" (old__), "+m" (per_cpu__##var) \
  142. : "dIr" (bit)); \
  143. old__; \
  144. })
  145. #include <asm-generic/percpu.h>
  146. /* We can use this directly for local CPU (faster). */
  147. DECLARE_PER_CPU(unsigned long, this_cpu_off);
  148. #endif /* !__ASSEMBLY__ */
  149. #ifdef CONFIG_SMP
  150. /*
  151. * Define the "EARLY_PER_CPU" macros. These are used for some per_cpu
  152. * variables that are initialized and accessed before there are per_cpu
  153. * areas allocated.
  154. */
  155. #define DEFINE_EARLY_PER_CPU(_type, _name, _initvalue) \
  156. DEFINE_PER_CPU(_type, _name) = _initvalue; \
  157. __typeof__(_type) _name##_early_map[NR_CPUS] __initdata = \
  158. { [0 ... NR_CPUS-1] = _initvalue }; \
  159. __typeof__(_type) *_name##_early_ptr __refdata = _name##_early_map
  160. #define EXPORT_EARLY_PER_CPU_SYMBOL(_name) \
  161. EXPORT_PER_CPU_SYMBOL(_name)
  162. #define DECLARE_EARLY_PER_CPU(_type, _name) \
  163. DECLARE_PER_CPU(_type, _name); \
  164. extern __typeof__(_type) *_name##_early_ptr; \
  165. extern __typeof__(_type) _name##_early_map[]
  166. #define early_per_cpu_ptr(_name) (_name##_early_ptr)
  167. #define early_per_cpu_map(_name, _idx) (_name##_early_map[_idx])
  168. #define early_per_cpu(_name, _cpu) \
  169. *(early_per_cpu_ptr(_name) ? \
  170. &early_per_cpu_ptr(_name)[_cpu] : \
  171. &per_cpu(_name, _cpu))
  172. #else /* !CONFIG_SMP */
  173. #define DEFINE_EARLY_PER_CPU(_type, _name, _initvalue) \
  174. DEFINE_PER_CPU(_type, _name) = _initvalue
  175. #define EXPORT_EARLY_PER_CPU_SYMBOL(_name) \
  176. EXPORT_PER_CPU_SYMBOL(_name)
  177. #define DECLARE_EARLY_PER_CPU(_type, _name) \
  178. DECLARE_PER_CPU(_type, _name)
  179. #define early_per_cpu(_name, _cpu) per_cpu(_name, _cpu)
  180. #define early_per_cpu_ptr(_name) NULL
  181. /* no early_per_cpu_map() */
  182. #endif /* !CONFIG_SMP */
  183. #endif /* _ASM_X86_PERCPU_H */