blob: 03aa4b00a1c3ff3bf31b1a9a57f49d3c31021841 [file] [log] [blame]
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001#ifndef _ASM_X86_PERCPU_H
2#define _ASM_X86_PERCPU_H
travis@sgi.com33340522008-01-30 13:32:53 +01003
Tejun Heo1a51e3a2009-01-13 20:41:35 +09004#ifdef CONFIG_X86_64
Tejun Heo9939dda2009-01-13 20:41:35 +09005#define __percpu_seg gs
6#define __percpu_mov_op movq
Tejun Heo1a51e3a2009-01-13 20:41:35 +09007#else
Tejun Heo9939dda2009-01-13 20:41:35 +09008#define __percpu_seg fs
9#define __percpu_mov_op movl
Tejun Heo1a51e3a2009-01-13 20:41:35 +090010#endif
travis@sgi.com33340522008-01-30 13:32:53 +010011
12#ifdef __ASSEMBLY__
13
14/*
15 * PER_CPU finds an address of a per-cpu variable.
16 *
17 * Args:
18 * var - variable name
19 * reg - 32bit register
20 *
21 * The resulting address is stored in the "reg" argument.
22 *
23 * Example:
24 * PER_CPU(cpu_gdt_descr, %ebx)
25 */
26#ifdef CONFIG_SMP
Tejun Heo9939dda2009-01-13 20:41:35 +090027#define PER_CPU(var, reg) \
28 __percpu_mov_op %__percpu_seg:per_cpu__this_cpu_off, reg; \
travis@sgi.com33340522008-01-30 13:32:53 +010029 lea per_cpu__##var(reg), reg
Tejun Heo9939dda2009-01-13 20:41:35 +090030#define PER_CPU_VAR(var) %__percpu_seg:per_cpu__##var
travis@sgi.com33340522008-01-30 13:32:53 +010031#else /* ! SMP */
Tejun Heo9939dda2009-01-13 20:41:35 +090032#define PER_CPU(var, reg) \
33 __percpu_mov_op $per_cpu__##var, reg
travis@sgi.com33340522008-01-30 13:32:53 +010034#define PER_CPU_VAR(var) per_cpu__##var
35#endif /* SMP */
36
37#else /* ...!ASSEMBLY */
38
Tejun Heo9939dda2009-01-13 20:41:35 +090039#include <linux/stringify.h>
40
travis@sgi.com33340522008-01-30 13:32:53 +010041#ifdef CONFIG_SMP
Tejun Heo9939dda2009-01-13 20:41:35 +090042#define __percpu_seg_str "%%"__stringify(__percpu_seg)":"
Ingo Molnar6dbde352009-01-15 22:15:53 +090043#define __my_cpu_offset percpu_read(this_cpu_off)
Tejun Heo9939dda2009-01-13 20:41:35 +090044#else
45#define __percpu_seg_str
46#endif
travis@sgi.com33340522008-01-30 13:32:53 +010047
travis@sgi.com33340522008-01-30 13:32:53 +010048/* For arch-specific code, we can use direct single-insn ops (they
49 * don't give an lvalue though). */
50extern void __bad_percpu_size(void);
51
Joe Perchesbc9e3be2008-03-23 01:03:06 -070052#define percpu_to_op(op, var, val) \
53do { \
54 typedef typeof(var) T__; \
55 if (0) { \
56 T__ tmp__; \
57 tmp__ = (val); \
58 } \
59 switch (sizeof(var)) { \
60 case 1: \
Tejun Heo9939dda2009-01-13 20:41:35 +090061 asm(op "b %1,"__percpu_seg_str"%0" \
Joe Perchesbc9e3be2008-03-23 01:03:06 -070062 : "+m" (var) \
63 : "ri" ((T__)val)); \
64 break; \
65 case 2: \
Tejun Heo9939dda2009-01-13 20:41:35 +090066 asm(op "w %1,"__percpu_seg_str"%0" \
Joe Perchesbc9e3be2008-03-23 01:03:06 -070067 : "+m" (var) \
68 : "ri" ((T__)val)); \
69 break; \
70 case 4: \
Tejun Heo9939dda2009-01-13 20:41:35 +090071 asm(op "l %1,"__percpu_seg_str"%0" \
Joe Perchesbc9e3be2008-03-23 01:03:06 -070072 : "+m" (var) \
73 : "ri" ((T__)val)); \
74 break; \
Tejun Heo9939dda2009-01-13 20:41:35 +090075 case 8: \
76 asm(op "q %1,"__percpu_seg_str"%0" \
77 : "+m" (var) \
78 : "r" ((T__)val)); \
79 break; \
Joe Perchesbc9e3be2008-03-23 01:03:06 -070080 default: __bad_percpu_size(); \
81 } \
82} while (0)
travis@sgi.com33340522008-01-30 13:32:53 +010083
Joe Perchesbc9e3be2008-03-23 01:03:06 -070084#define percpu_from_op(op, var) \
85({ \
86 typeof(var) ret__; \
87 switch (sizeof(var)) { \
88 case 1: \
Tejun Heo9939dda2009-01-13 20:41:35 +090089 asm(op "b "__percpu_seg_str"%1,%0" \
Joe Perchesbc9e3be2008-03-23 01:03:06 -070090 : "=r" (ret__) \
91 : "m" (var)); \
92 break; \
93 case 2: \
Tejun Heo9939dda2009-01-13 20:41:35 +090094 asm(op "w "__percpu_seg_str"%1,%0" \
Joe Perchesbc9e3be2008-03-23 01:03:06 -070095 : "=r" (ret__) \
96 : "m" (var)); \
97 break; \
98 case 4: \
Tejun Heo9939dda2009-01-13 20:41:35 +090099 asm(op "l "__percpu_seg_str"%1,%0" \
100 : "=r" (ret__) \
101 : "m" (var)); \
102 break; \
103 case 8: \
104 asm(op "q "__percpu_seg_str"%1,%0" \
Joe Perchesbc9e3be2008-03-23 01:03:06 -0700105 : "=r" (ret__) \
106 : "m" (var)); \
107 break; \
108 default: __bad_percpu_size(); \
109 } \
110 ret__; \
111})
travis@sgi.com33340522008-01-30 13:32:53 +0100112
Ingo Molnar6dbde352009-01-15 22:15:53 +0900113#define percpu_read(var) percpu_from_op("mov", per_cpu__##var)
114#define percpu_write(var, val) percpu_to_op("mov", per_cpu__##var, val)
115#define percpu_add(var, val) percpu_to_op("add", per_cpu__##var, val)
116#define percpu_sub(var, val) percpu_to_op("sub", per_cpu__##var, val)
117#define percpu_and(var, val) percpu_to_op("and", per_cpu__##var, val)
118#define percpu_or(var, val) percpu_to_op("or", per_cpu__##var, val)
119#define percpu_xor(var, val) percpu_to_op("xor", per_cpu__##var, val)
Tejun Heo9939dda2009-01-13 20:41:35 +0900120
Tejun Heo49357d12009-01-13 20:41:35 +0900121/* This is not atomic against other CPUs -- CPU preemption needs to be off */
122#define x86_test_and_clear_bit_percpu(bit, var) \
123({ \
124 int old__; \
125 asm volatile("btr %1,"__percpu_seg_str"%c2\n\tsbbl %0,%0" \
126 : "=r" (old__) \
127 : "dIr" (bit), "i" (&per_cpu__##var) : "memory"); \
128 old__; \
129})
130
Ingo Molnar6dbde352009-01-15 22:15:53 +0900131#include <asm-generic/percpu.h>
132
133/* We can use this directly for local CPU (faster). */
134DECLARE_PER_CPU(unsigned long, this_cpu_off);
135
Tejun Heo9939dda2009-01-13 20:41:35 +0900136#ifdef CONFIG_X86_64
137extern void load_pda_offset(int cpu);
138#else
139static inline void load_pda_offset(int cpu) { }
140#endif
141
travis@sgi.com33340522008-01-30 13:32:53 +0100142#endif /* !__ASSEMBLY__ */
Mike Travis23ca4bb2008-05-12 21:21:12 +0200143
144#ifdef CONFIG_SMP
145
146/*
147 * Define the "EARLY_PER_CPU" macros. These are used for some per_cpu
148 * variables that are initialized and accessed before there are per_cpu
149 * areas allocated.
150 */
151
152#define DEFINE_EARLY_PER_CPU(_type, _name, _initvalue) \
153 DEFINE_PER_CPU(_type, _name) = _initvalue; \
154 __typeof__(_type) _name##_early_map[NR_CPUS] __initdata = \
155 { [0 ... NR_CPUS-1] = _initvalue }; \
Marcin Slusarzc6a92a22008-08-17 17:50:50 +0200156 __typeof__(_type) *_name##_early_ptr __refdata = _name##_early_map
Mike Travis23ca4bb2008-05-12 21:21:12 +0200157
158#define EXPORT_EARLY_PER_CPU_SYMBOL(_name) \
159 EXPORT_PER_CPU_SYMBOL(_name)
160
161#define DECLARE_EARLY_PER_CPU(_type, _name) \
162 DECLARE_PER_CPU(_type, _name); \
163 extern __typeof__(_type) *_name##_early_ptr; \
164 extern __typeof__(_type) _name##_early_map[]
165
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) \
Tejun Heof10fcd42009-01-13 20:41:34 +0900169 *(early_per_cpu_ptr(_name) ? \
170 &early_per_cpu_ptr(_name)[_cpu] : \
171 &per_cpu(_name, _cpu))
Mike Travis23ca4bb2008-05-12 21:21:12 +0200172
173#else /* !CONFIG_SMP */
174#define DEFINE_EARLY_PER_CPU(_type, _name, _initvalue) \
175 DEFINE_PER_CPU(_type, _name) = _initvalue
176
177#define EXPORT_EARLY_PER_CPU_SYMBOL(_name) \
178 EXPORT_PER_CPU_SYMBOL(_name)
179
180#define DECLARE_EARLY_PER_CPU(_type, _name) \
181 DECLARE_PER_CPU(_type, _name)
182
183#define early_per_cpu(_name, _cpu) per_cpu(_name, _cpu)
184#define early_per_cpu_ptr(_name) NULL
185/* no early_per_cpu_map() */
186
187#endif /* !CONFIG_SMP */
188
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700189#endif /* _ASM_X86_PERCPU_H */