percpu.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #ifndef _ASM_X86_PERCPU_H_
  2. #define _ASM_X86_PERCPU_H_
  3. #ifdef CONFIG_X86_64
  4. #include <linux/compiler.h>
  5. /* Same as asm-generic/percpu.h, except that we store the per cpu offset
  6. in the PDA. Longer term the PDA and every per cpu variable
  7. should be just put into a single section and referenced directly
  8. from %gs */
  9. #ifdef CONFIG_SMP
  10. #include <asm/pda.h>
  11. #define __per_cpu_offset(cpu) (cpu_pda(cpu)->data_offset)
  12. #define __my_cpu_offset read_pda(data_offset)
  13. #define per_cpu_offset(x) (__per_cpu_offset(x))
  14. #endif
  15. #include <asm-generic/percpu.h>
  16. DECLARE_PER_CPU(struct x8664_pda, pda);
  17. #else /* CONFIG_X86_64 */
  18. #ifdef __ASSEMBLY__
  19. /*
  20. * PER_CPU finds an address of a per-cpu variable.
  21. *
  22. * Args:
  23. * var - variable name
  24. * reg - 32bit register
  25. *
  26. * The resulting address is stored in the "reg" argument.
  27. *
  28. * Example:
  29. * PER_CPU(cpu_gdt_descr, %ebx)
  30. */
  31. #ifdef CONFIG_SMP
  32. #define PER_CPU(var, reg) \
  33. movl %fs:per_cpu__##this_cpu_off, reg; \
  34. lea per_cpu__##var(reg), reg
  35. #define PER_CPU_VAR(var) %fs:per_cpu__##var
  36. #else /* ! SMP */
  37. #define PER_CPU(var, reg) \
  38. movl $per_cpu__##var, reg
  39. #define PER_CPU_VAR(var) per_cpu__##var
  40. #endif /* SMP */
  41. #else /* ...!ASSEMBLY */
  42. /*
  43. * PER_CPU finds an address of a per-cpu variable.
  44. *
  45. * Args:
  46. * var - variable name
  47. * cpu - 32bit register containing the current CPU number
  48. *
  49. * The resulting address is stored in the "cpu" argument.
  50. *
  51. * Example:
  52. * PER_CPU(cpu_gdt_descr, %ebx)
  53. */
  54. #ifdef CONFIG_SMP
  55. #define __my_cpu_offset x86_read_percpu(this_cpu_off)
  56. /* fs segment starts at (positive) offset == __per_cpu_offset[cpu] */
  57. #define __percpu_seg "%%fs:"
  58. #else /* !SMP */
  59. #define __percpu_seg ""
  60. #endif /* SMP */
  61. #include <asm-generic/percpu.h>
  62. /* We can use this directly for local CPU (faster). */
  63. DECLARE_PER_CPU(unsigned long, this_cpu_off);
  64. /* For arch-specific code, we can use direct single-insn ops (they
  65. * don't give an lvalue though). */
  66. extern void __bad_percpu_size(void);
  67. #define percpu_to_op(op, var, val) \
  68. do { \
  69. typedef typeof(var) T__; \
  70. if (0) { \
  71. T__ tmp__; \
  72. tmp__ = (val); \
  73. } \
  74. switch (sizeof(var)) { \
  75. case 1: \
  76. asm(op "b %1,"__percpu_seg"%0" \
  77. : "+m" (var) \
  78. : "ri" ((T__)val)); \
  79. break; \
  80. case 2: \
  81. asm(op "w %1,"__percpu_seg"%0" \
  82. : "+m" (var) \
  83. : "ri" ((T__)val)); \
  84. break; \
  85. case 4: \
  86. asm(op "l %1,"__percpu_seg"%0" \
  87. : "+m" (var) \
  88. : "ri" ((T__)val)); \
  89. break; \
  90. default: __bad_percpu_size(); \
  91. } \
  92. } while (0)
  93. #define percpu_from_op(op, var) \
  94. ({ \
  95. typeof(var) ret__; \
  96. switch (sizeof(var)) { \
  97. case 1: \
  98. asm(op "b "__percpu_seg"%1,%0" \
  99. : "=r" (ret__) \
  100. : "m" (var)); \
  101. break; \
  102. case 2: \
  103. asm(op "w "__percpu_seg"%1,%0" \
  104. : "=r" (ret__) \
  105. : "m" (var)); \
  106. break; \
  107. case 4: \
  108. asm(op "l "__percpu_seg"%1,%0" \
  109. : "=r" (ret__) \
  110. : "m" (var)); \
  111. break; \
  112. default: __bad_percpu_size(); \
  113. } \
  114. ret__; \
  115. })
  116. #define x86_read_percpu(var) percpu_from_op("mov", per_cpu__##var)
  117. #define x86_write_percpu(var, val) percpu_to_op("mov", per_cpu__##var, val)
  118. #define x86_add_percpu(var, val) percpu_to_op("add", per_cpu__##var, val)
  119. #define x86_sub_percpu(var, val) percpu_to_op("sub", per_cpu__##var, val)
  120. #define x86_or_percpu(var, val) percpu_to_op("or", per_cpu__##var, val)
  121. #endif /* !__ASSEMBLY__ */
  122. #endif /* !CONFIG_X86_64 */
  123. #ifdef CONFIG_SMP
  124. /*
  125. * Define the "EARLY_PER_CPU" macros. These are used for some per_cpu
  126. * variables that are initialized and accessed before there are per_cpu
  127. * areas allocated.
  128. */
  129. #define DEFINE_EARLY_PER_CPU(_type, _name, _initvalue) \
  130. DEFINE_PER_CPU(_type, _name) = _initvalue; \
  131. __typeof__(_type) _name##_early_map[NR_CPUS] __initdata = \
  132. { [0 ... NR_CPUS-1] = _initvalue }; \
  133. __typeof__(_type) *_name##_early_ptr = _name##_early_map
  134. #define EXPORT_EARLY_PER_CPU_SYMBOL(_name) \
  135. EXPORT_PER_CPU_SYMBOL(_name)
  136. #define DECLARE_EARLY_PER_CPU(_type, _name) \
  137. DECLARE_PER_CPU(_type, _name); \
  138. extern __typeof__(_type) *_name##_early_ptr; \
  139. extern __typeof__(_type) _name##_early_map[]
  140. #define early_per_cpu_ptr(_name) (_name##_early_ptr)
  141. #define early_per_cpu_map(_name, _idx) (_name##_early_map[_idx])
  142. #define early_per_cpu(_name, _cpu) \
  143. (early_per_cpu_ptr(_name) ? \
  144. early_per_cpu_ptr(_name)[_cpu] : \
  145. per_cpu(_name, _cpu))
  146. #else /* !CONFIG_SMP */
  147. #define DEFINE_EARLY_PER_CPU(_type, _name, _initvalue) \
  148. DEFINE_PER_CPU(_type, _name) = _initvalue
  149. #define EXPORT_EARLY_PER_CPU_SYMBOL(_name) \
  150. EXPORT_PER_CPU_SYMBOL(_name)
  151. #define DECLARE_EARLY_PER_CPU(_type, _name) \
  152. DECLARE_PER_CPU(_type, _name)
  153. #define early_per_cpu(_name, _cpu) per_cpu(_name, _cpu)
  154. #define early_per_cpu_ptr(_name) NULL
  155. /* no early_per_cpu_map() */
  156. #endif /* !CONFIG_SMP */
  157. #endif /* _ASM_X86_PERCPU_H_ */