1 #ifndef _ASM_X86_PERCPU_H_
2 #define _ASM_X86_PERCPU_H_
5 #include <linux/compiler.h>
7 /* Same as asm-generic/percpu.h, except that we store the per cpu offset
8 in the PDA. Longer term the PDA and every per cpu variable
9 should be just put into a single section and referenced directly
15 #define __per_cpu_offset(cpu) (cpu_pda(cpu)->data_offset)
16 #define __my_cpu_offset read_pda(data_offset)
18 #define per_cpu_offset(x) (__per_cpu_offset(x))
21 #include <asm-generic/percpu.h>
23 DECLARE_PER_CPU(struct x8664_pda
, pda
);
26 * These are supposed to be implemented as a single instruction which
27 * operates on the per-cpu data base segment. x86-64 doesn't have
28 * that yet, so this is a fairly inefficient workaround for the
29 * meantime. The single instruction is atomic with respect to
30 * preemption and interrupts, so we need to explicitly disable
31 * interrupts here to achieve the same effect. However, because it
32 * can be used from within interrupt-disable/enable, we can't actually
33 * disable interrupts; disabling preemption is enough.
35 #define x86_read_percpu(var) \
37 typeof(per_cpu_var(var)) __tmp; \
39 __tmp = __get_cpu_var(var); \
44 #define x86_write_percpu(var, val) \
47 __get_cpu_var(var) = (val); \
51 #else /* CONFIG_X86_64 */
56 * PER_CPU finds an address of a per-cpu variable.
60 * reg - 32bit register
62 * The resulting address is stored in the "reg" argument.
65 * PER_CPU(cpu_gdt_descr, %ebx)
68 #define PER_CPU(var, reg) \
69 movl %fs:per_cpu__##this_cpu_off, reg; \
70 lea per_cpu__##var(reg), reg
71 #define PER_CPU_VAR(var) %fs:per_cpu__##var
73 #define PER_CPU(var, reg) \
74 movl $per_cpu__##var, reg
75 #define PER_CPU_VAR(var) per_cpu__##var
78 #else /* ...!ASSEMBLY */
81 * PER_CPU finds an address of a per-cpu variable.
85 * cpu - 32bit register containing the current CPU number
87 * The resulting address is stored in the "cpu" argument.
90 * PER_CPU(cpu_gdt_descr, %ebx)
94 #define __my_cpu_offset x86_read_percpu(this_cpu_off)
96 /* fs segment starts at (positive) offset == __per_cpu_offset[cpu] */
97 #define __percpu_seg "%%fs:"
101 #define __percpu_seg ""
105 #include <asm-generic/percpu.h>
107 /* We can use this directly for local CPU (faster). */
108 DECLARE_PER_CPU(unsigned long, this_cpu_off
);
110 /* For arch-specific code, we can use direct single-insn ops (they
111 * don't give an lvalue though). */
112 extern void __bad_percpu_size(void);
114 #define percpu_to_op(op, var, val) \
116 typedef typeof(var) T__; \
121 switch (sizeof(var)) { \
123 asm(op "b %1,"__percpu_seg"%0" \
125 : "ri" ((T__)val)); \
128 asm(op "w %1,"__percpu_seg"%0" \
130 : "ri" ((T__)val)); \
133 asm(op "l %1,"__percpu_seg"%0" \
135 : "ri" ((T__)val)); \
137 default: __bad_percpu_size(); \
141 #define percpu_from_op(op, var) \
144 switch (sizeof(var)) { \
146 asm(op "b "__percpu_seg"%1,%0" \
151 asm(op "w "__percpu_seg"%1,%0" \
156 asm(op "l "__percpu_seg"%1,%0" \
160 default: __bad_percpu_size(); \
165 #define x86_read_percpu(var) percpu_from_op("mov", per_cpu__##var)
166 #define x86_write_percpu(var, val) percpu_to_op("mov", per_cpu__##var, val)
167 #define x86_add_percpu(var, val) percpu_to_op("add", per_cpu__##var, val)
168 #define x86_sub_percpu(var, val) percpu_to_op("sub", per_cpu__##var, val)
169 #define x86_or_percpu(var, val) percpu_to_op("or", per_cpu__##var, val)
170 #endif /* !__ASSEMBLY__ */
171 #endif /* !CONFIG_X86_64 */
176 * Define the "EARLY_PER_CPU" macros. These are used for some per_cpu
177 * variables that are initialized and accessed before there are per_cpu
181 #define DEFINE_EARLY_PER_CPU(_type, _name, _initvalue) \
182 DEFINE_PER_CPU(_type, _name) = _initvalue; \
183 __typeof__(_type) _name##_early_map[NR_CPUS] __initdata = \
184 { [0 ... NR_CPUS-1] = _initvalue }; \
185 __typeof__(_type) *_name##_early_ptr __refdata = _name##_early_map
187 #define EXPORT_EARLY_PER_CPU_SYMBOL(_name) \
188 EXPORT_PER_CPU_SYMBOL(_name)
190 #define DECLARE_EARLY_PER_CPU(_type, _name) \
191 DECLARE_PER_CPU(_type, _name); \
192 extern __typeof__(_type) *_name##_early_ptr; \
193 extern __typeof__(_type) _name##_early_map[]
195 #define early_per_cpu_ptr(_name) (_name##_early_ptr)
196 #define early_per_cpu_map(_name, _idx) (_name##_early_map[_idx])
197 #define early_per_cpu(_name, _cpu) \
198 (early_per_cpu_ptr(_name) ? \
199 early_per_cpu_ptr(_name)[_cpu] : \
200 per_cpu(_name, _cpu))
202 #else /* !CONFIG_SMP */
203 #define DEFINE_EARLY_PER_CPU(_type, _name, _initvalue) \
204 DEFINE_PER_CPU(_type, _name) = _initvalue
206 #define EXPORT_EARLY_PER_CPU_SYMBOL(_name) \
207 EXPORT_PER_CPU_SYMBOL(_name)
209 #define DECLARE_EARLY_PER_CPU(_type, _name) \
210 DECLARE_PER_CPU(_type, _name)
212 #define early_per_cpu(_name, _cpu) per_cpu(_name, _cpu)
213 #define early_per_cpu_ptr(_name) NULL
214 /* no early_per_cpu_map() */
216 #endif /* !CONFIG_SMP */
218 #endif /* _ASM_X86_PERCPU_H_ */