blob: 66a272dfd8b8aa0d5fcb788ace9bec17da31e236 [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) \
Rusty Russelldd17c8f2009-10-29 22:34:15 +090028 __percpu_mov_op %__percpu_seg:this_cpu_off, reg; \
29 lea var(reg), reg
30#define PER_CPU_VAR(var) %__percpu_seg:var
travis@sgi.com33340522008-01-30 13:32:53 +010031#else /* ! SMP */
Rusty Russelldd17c8f2009-10-29 22:34:15 +090032#define PER_CPU(var, reg) __percpu_mov_op $var, reg
33#define PER_CPU_VAR(var) var
travis@sgi.com33340522008-01-30 13:32:53 +010034#endif /* SMP */
35
Brian Gerst2add8e22009-02-08 09:58:39 -050036#ifdef CONFIG_X86_64_SMP
37#define INIT_PER_CPU_VAR(var) init_per_cpu__##var
38#else
Rusty Russelldd17c8f2009-10-29 22:34:15 +090039#define INIT_PER_CPU_VAR(var) var
Brian Gerst2add8e22009-02-08 09:58:39 -050040#endif
41
travis@sgi.com33340522008-01-30 13:32:53 +010042#else /* ...!ASSEMBLY */
43
Tejun Heoe59a1bb2009-06-22 11:56:24 +090044#include <linux/kernel.h>
Tejun Heo9939dda2009-01-13 20:41:35 +090045#include <linux/stringify.h>
46
travis@sgi.com33340522008-01-30 13:32:53 +010047#ifdef CONFIG_SMP
Brian Gerst87b26402009-01-19 00:38:59 +090048#define __percpu_arg(x) "%%"__stringify(__percpu_seg)":%P" #x
Ingo Molnar6dbde352009-01-15 22:15:53 +090049#define __my_cpu_offset percpu_read(this_cpu_off)
Tejun Heo9939dda2009-01-13 20:41:35 +090050#else
Linus Torvaldsed8d9ad2009-08-03 14:08:48 +090051#define __percpu_arg(x) "%P" #x
Tejun Heo9939dda2009-01-13 20:41:35 +090052#endif
travis@sgi.com33340522008-01-30 13:32:53 +010053
Brian Gerst2add8e22009-02-08 09:58:39 -050054/*
55 * Initialized pointers to per-cpu variables needed for the boot
56 * processor need to use these macros to get the proper address
57 * offset from __per_cpu_load on SMP.
58 *
59 * There also must be an entry in vmlinux_64.lds.S
60 */
61#define DECLARE_INIT_PER_CPU(var) \
Rusty Russelldd17c8f2009-10-29 22:34:15 +090062 extern typeof(var) init_per_cpu_var(var)
Brian Gerst2add8e22009-02-08 09:58:39 -050063
64#ifdef CONFIG_X86_64_SMP
65#define init_per_cpu_var(var) init_per_cpu__##var
66#else
Rusty Russelldd17c8f2009-10-29 22:34:15 +090067#define init_per_cpu_var(var) var
Brian Gerst2add8e22009-02-08 09:58:39 -050068#endif
69
travis@sgi.com33340522008-01-30 13:32:53 +010070/* For arch-specific code, we can use direct single-insn ops (they
71 * don't give an lvalue though). */
72extern void __bad_percpu_size(void);
73
Joe Perchesbc9e3be2008-03-23 01:03:06 -070074#define percpu_to_op(op, var, val) \
75do { \
Tejun Heo0f5e4812009-10-29 22:34:12 +090076 typedef typeof(var) pto_T__; \
Joe Perchesbc9e3be2008-03-23 01:03:06 -070077 if (0) { \
Tejun Heo0f5e4812009-10-29 22:34:12 +090078 pto_T__ pto_tmp__; \
79 pto_tmp__ = (val); \
Joe Perchesbc9e3be2008-03-23 01:03:06 -070080 } \
81 switch (sizeof(var)) { \
82 case 1: \
Brian Gerst87b26402009-01-19 00:38:59 +090083 asm(op "b %1,"__percpu_arg(0) \
Joe Perchesbc9e3be2008-03-23 01:03:06 -070084 : "+m" (var) \
Tejun Heo0f5e4812009-10-29 22:34:12 +090085 : "qi" ((pto_T__)(val))); \
Joe Perchesbc9e3be2008-03-23 01:03:06 -070086 break; \
87 case 2: \
Brian Gerst87b26402009-01-19 00:38:59 +090088 asm(op "w %1,"__percpu_arg(0) \
Joe Perchesbc9e3be2008-03-23 01:03:06 -070089 : "+m" (var) \
Tejun Heo0f5e4812009-10-29 22:34:12 +090090 : "ri" ((pto_T__)(val))); \
Joe Perchesbc9e3be2008-03-23 01:03:06 -070091 break; \
92 case 4: \
Brian Gerst87b26402009-01-19 00:38:59 +090093 asm(op "l %1,"__percpu_arg(0) \
Joe Perchesbc9e3be2008-03-23 01:03:06 -070094 : "+m" (var) \
Tejun Heo0f5e4812009-10-29 22:34:12 +090095 : "ri" ((pto_T__)(val))); \
Joe Perchesbc9e3be2008-03-23 01:03:06 -070096 break; \
Tejun Heo9939dda2009-01-13 20:41:35 +090097 case 8: \
Brian Gerst87b26402009-01-19 00:38:59 +090098 asm(op "q %1,"__percpu_arg(0) \
Tejun Heo9939dda2009-01-13 20:41:35 +090099 : "+m" (var) \
Tejun Heo0f5e4812009-10-29 22:34:12 +0900100 : "re" ((pto_T__)(val))); \
Tejun Heo9939dda2009-01-13 20:41:35 +0900101 break; \
Joe Perchesbc9e3be2008-03-23 01:03:06 -0700102 default: __bad_percpu_size(); \
103 } \
104} while (0)
travis@sgi.com33340522008-01-30 13:32:53 +0100105
Christoph Lameter5917dae2010-01-05 15:34:50 +0900106/*
107 * Generate a percpu add to memory instruction and optimize code
108 * if a one is added or subtracted.
109 */
110#define percpu_add_op(var, val) \
111do { \
112 typedef typeof(var) pao_T__; \
113 const int pao_ID__ = (__builtin_constant_p(val) && \
114 ((val) == 1 || (val) == -1)) ? (val) : 0; \
115 if (0) { \
116 pao_T__ pao_tmp__; \
117 pao_tmp__ = (val); \
118 } \
119 switch (sizeof(var)) { \
120 case 1: \
121 if (pao_ID__ == 1) \
122 asm("incb "__percpu_arg(0) : "+m" (var)); \
123 else if (pao_ID__ == -1) \
124 asm("decb "__percpu_arg(0) : "+m" (var)); \
125 else \
126 asm("addb %1, "__percpu_arg(0) \
127 : "+m" (var) \
128 : "qi" ((pao_T__)(val))); \
129 break; \
130 case 2: \
131 if (pao_ID__ == 1) \
132 asm("incw "__percpu_arg(0) : "+m" (var)); \
133 else if (pao_ID__ == -1) \
134 asm("decw "__percpu_arg(0) : "+m" (var)); \
135 else \
136 asm("addw %1, "__percpu_arg(0) \
137 : "+m" (var) \
138 : "ri" ((pao_T__)(val))); \
139 break; \
140 case 4: \
141 if (pao_ID__ == 1) \
142 asm("incl "__percpu_arg(0) : "+m" (var)); \
143 else if (pao_ID__ == -1) \
144 asm("decl "__percpu_arg(0) : "+m" (var)); \
145 else \
146 asm("addl %1, "__percpu_arg(0) \
147 : "+m" (var) \
148 : "ri" ((pao_T__)(val))); \
149 break; \
150 case 8: \
151 if (pao_ID__ == 1) \
152 asm("incq "__percpu_arg(0) : "+m" (var)); \
153 else if (pao_ID__ == -1) \
154 asm("decq "__percpu_arg(0) : "+m" (var)); \
155 else \
156 asm("addq %1, "__percpu_arg(0) \
157 : "+m" (var) \
158 : "re" ((pao_T__)(val))); \
159 break; \
160 default: __bad_percpu_size(); \
161 } \
162} while (0)
163
Linus Torvaldsed8d9ad2009-08-03 14:08:48 +0900164#define percpu_from_op(op, var, constraint) \
Joe Perchesbc9e3be2008-03-23 01:03:06 -0700165({ \
Tejun Heo0f5e4812009-10-29 22:34:12 +0900166 typeof(var) pfo_ret__; \
Joe Perchesbc9e3be2008-03-23 01:03:06 -0700167 switch (sizeof(var)) { \
168 case 1: \
Brian Gerst87b26402009-01-19 00:38:59 +0900169 asm(op "b "__percpu_arg(1)",%0" \
Tejun Heo0f5e4812009-10-29 22:34:12 +0900170 : "=q" (pfo_ret__) \
Linus Torvaldsed8d9ad2009-08-03 14:08:48 +0900171 : constraint); \
Joe Perchesbc9e3be2008-03-23 01:03:06 -0700172 break; \
173 case 2: \
Brian Gerst87b26402009-01-19 00:38:59 +0900174 asm(op "w "__percpu_arg(1)",%0" \
Tejun Heo0f5e4812009-10-29 22:34:12 +0900175 : "=r" (pfo_ret__) \
Linus Torvaldsed8d9ad2009-08-03 14:08:48 +0900176 : constraint); \
Joe Perchesbc9e3be2008-03-23 01:03:06 -0700177 break; \
178 case 4: \
Brian Gerst87b26402009-01-19 00:38:59 +0900179 asm(op "l "__percpu_arg(1)",%0" \
Tejun Heo0f5e4812009-10-29 22:34:12 +0900180 : "=r" (pfo_ret__) \
Linus Torvaldsed8d9ad2009-08-03 14:08:48 +0900181 : constraint); \
Tejun Heo9939dda2009-01-13 20:41:35 +0900182 break; \
183 case 8: \
Brian Gerst87b26402009-01-19 00:38:59 +0900184 asm(op "q "__percpu_arg(1)",%0" \
Tejun Heo0f5e4812009-10-29 22:34:12 +0900185 : "=r" (pfo_ret__) \
Linus Torvaldsed8d9ad2009-08-03 14:08:48 +0900186 : constraint); \
Joe Perchesbc9e3be2008-03-23 01:03:06 -0700187 break; \
188 default: __bad_percpu_size(); \
189 } \
Tejun Heo0f5e4812009-10-29 22:34:12 +0900190 pfo_ret__; \
Joe Perchesbc9e3be2008-03-23 01:03:06 -0700191})
travis@sgi.com33340522008-01-30 13:32:53 +0100192
Linus Torvaldsed8d9ad2009-08-03 14:08:48 +0900193/*
194 * percpu_read() makes gcc load the percpu variable every time it is
195 * accessed while percpu_read_stable() allows the value to be cached.
196 * percpu_read_stable() is more efficient and can be used if its value
197 * is guaranteed to be valid across cpus. The current users include
198 * get_current() and get_thread_info() both of which are actually
199 * per-thread variables implemented as per-cpu variables and thus
200 * stable for the duration of the respective task.
201 */
Rusty Russelldd17c8f2009-10-29 22:34:15 +0900202#define percpu_read(var) percpu_from_op("mov", var, "m" (var))
203#define percpu_read_stable(var) percpu_from_op("mov", var, "p" (&(var)))
204#define percpu_write(var, val) percpu_to_op("mov", var, val)
Christoph Lameter5917dae2010-01-05 15:34:50 +0900205#define percpu_add(var, val) percpu_add_op(var, val)
206#define percpu_sub(var, val) percpu_add_op(var, -(val))
Rusty Russelldd17c8f2009-10-29 22:34:15 +0900207#define percpu_and(var, val) percpu_to_op("and", var, val)
208#define percpu_or(var, val) percpu_to_op("or", var, val)
209#define percpu_xor(var, val) percpu_to_op("xor", var, val)
Tejun Heo9939dda2009-01-13 20:41:35 +0900210
Christoph Lameter30ed1a72009-10-03 19:48:22 +0900211#define __this_cpu_read_1(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
212#define __this_cpu_read_2(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
213#define __this_cpu_read_4(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
214
215#define __this_cpu_write_1(pcp, val) percpu_to_op("mov", (pcp), val)
216#define __this_cpu_write_2(pcp, val) percpu_to_op("mov", (pcp), val)
217#define __this_cpu_write_4(pcp, val) percpu_to_op("mov", (pcp), val)
Christoph Lameter5917dae2010-01-05 15:34:50 +0900218#define __this_cpu_add_1(pcp, val) percpu_add_op((pcp), val)
219#define __this_cpu_add_2(pcp, val) percpu_add_op((pcp), val)
220#define __this_cpu_add_4(pcp, val) percpu_add_op((pcp), val)
Christoph Lameter30ed1a72009-10-03 19:48:22 +0900221#define __this_cpu_and_1(pcp, val) percpu_to_op("and", (pcp), val)
222#define __this_cpu_and_2(pcp, val) percpu_to_op("and", (pcp), val)
223#define __this_cpu_and_4(pcp, val) percpu_to_op("and", (pcp), val)
224#define __this_cpu_or_1(pcp, val) percpu_to_op("or", (pcp), val)
225#define __this_cpu_or_2(pcp, val) percpu_to_op("or", (pcp), val)
226#define __this_cpu_or_4(pcp, val) percpu_to_op("or", (pcp), val)
227#define __this_cpu_xor_1(pcp, val) percpu_to_op("xor", (pcp), val)
228#define __this_cpu_xor_2(pcp, val) percpu_to_op("xor", (pcp), val)
229#define __this_cpu_xor_4(pcp, val) percpu_to_op("xor", (pcp), val)
230
231#define this_cpu_read_1(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
232#define this_cpu_read_2(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
233#define this_cpu_read_4(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
234#define this_cpu_write_1(pcp, val) percpu_to_op("mov", (pcp), val)
235#define this_cpu_write_2(pcp, val) percpu_to_op("mov", (pcp), val)
236#define this_cpu_write_4(pcp, val) percpu_to_op("mov", (pcp), val)
Christoph Lameter5917dae2010-01-05 15:34:50 +0900237#define this_cpu_add_1(pcp, val) percpu_add_op((pcp), val)
238#define this_cpu_add_2(pcp, val) percpu_add_op((pcp), val)
239#define this_cpu_add_4(pcp, val) percpu_add_op((pcp), val)
Christoph Lameter30ed1a72009-10-03 19:48:22 +0900240#define this_cpu_and_1(pcp, val) percpu_to_op("and", (pcp), val)
241#define this_cpu_and_2(pcp, val) percpu_to_op("and", (pcp), val)
242#define this_cpu_and_4(pcp, val) percpu_to_op("and", (pcp), val)
243#define this_cpu_or_1(pcp, val) percpu_to_op("or", (pcp), val)
244#define this_cpu_or_2(pcp, val) percpu_to_op("or", (pcp), val)
245#define this_cpu_or_4(pcp, val) percpu_to_op("or", (pcp), val)
246#define this_cpu_xor_1(pcp, val) percpu_to_op("xor", (pcp), val)
247#define this_cpu_xor_2(pcp, val) percpu_to_op("xor", (pcp), val)
248#define this_cpu_xor_4(pcp, val) percpu_to_op("xor", (pcp), val)
249
Christoph Lameter5917dae2010-01-05 15:34:50 +0900250#define irqsafe_cpu_add_1(pcp, val) percpu_add_op((pcp), val)
251#define irqsafe_cpu_add_2(pcp, val) percpu_add_op((pcp), val)
252#define irqsafe_cpu_add_4(pcp, val) percpu_add_op((pcp), val)
Christoph Lameter30ed1a72009-10-03 19:48:22 +0900253#define irqsafe_cpu_and_1(pcp, val) percpu_to_op("and", (pcp), val)
254#define irqsafe_cpu_and_2(pcp, val) percpu_to_op("and", (pcp), val)
255#define irqsafe_cpu_and_4(pcp, val) percpu_to_op("and", (pcp), val)
256#define irqsafe_cpu_or_1(pcp, val) percpu_to_op("or", (pcp), val)
257#define irqsafe_cpu_or_2(pcp, val) percpu_to_op("or", (pcp), val)
258#define irqsafe_cpu_or_4(pcp, val) percpu_to_op("or", (pcp), val)
259#define irqsafe_cpu_xor_1(pcp, val) percpu_to_op("xor", (pcp), val)
260#define irqsafe_cpu_xor_2(pcp, val) percpu_to_op("xor", (pcp), val)
261#define irqsafe_cpu_xor_4(pcp, val) percpu_to_op("xor", (pcp), val)
262
263/*
264 * Per cpu atomic 64 bit operations are only available under 64 bit.
265 * 32 bit must fall back to generic operations.
266 */
267#ifdef CONFIG_X86_64
268#define __this_cpu_read_8(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
269#define __this_cpu_write_8(pcp, val) percpu_to_op("mov", (pcp), val)
Christoph Lameter5917dae2010-01-05 15:34:50 +0900270#define __this_cpu_add_8(pcp, val) percpu_add_op((pcp), val)
Christoph Lameter30ed1a72009-10-03 19:48:22 +0900271#define __this_cpu_and_8(pcp, val) percpu_to_op("and", (pcp), val)
272#define __this_cpu_or_8(pcp, val) percpu_to_op("or", (pcp), val)
273#define __this_cpu_xor_8(pcp, val) percpu_to_op("xor", (pcp), val)
274
275#define this_cpu_read_8(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
276#define this_cpu_write_8(pcp, val) percpu_to_op("mov", (pcp), val)
Christoph Lameter5917dae2010-01-05 15:34:50 +0900277#define this_cpu_add_8(pcp, val) percpu_add_op((pcp), val)
Christoph Lameter30ed1a72009-10-03 19:48:22 +0900278#define this_cpu_and_8(pcp, val) percpu_to_op("and", (pcp), val)
279#define this_cpu_or_8(pcp, val) percpu_to_op("or", (pcp), val)
280#define this_cpu_xor_8(pcp, val) percpu_to_op("xor", (pcp), val)
281
Christoph Lameter5917dae2010-01-05 15:34:50 +0900282#define irqsafe_cpu_add_8(pcp, val) percpu_add_op((pcp), val)
Christoph Lameter30ed1a72009-10-03 19:48:22 +0900283#define irqsafe_cpu_and_8(pcp, val) percpu_to_op("and", (pcp), val)
284#define irqsafe_cpu_or_8(pcp, val) percpu_to_op("or", (pcp), val)
285#define irqsafe_cpu_xor_8(pcp, val) percpu_to_op("xor", (pcp), val)
286
287#endif
288
Tejun Heo49357d12009-01-13 20:41:35 +0900289/* This is not atomic against other CPUs -- CPU preemption needs to be off */
290#define x86_test_and_clear_bit_percpu(bit, var) \
291({ \
292 int old__; \
Brian Gerst87b26402009-01-19 00:38:59 +0900293 asm volatile("btr %2,"__percpu_arg(1)"\n\tsbbl %0,%0" \
Rusty Russelldd17c8f2009-10-29 22:34:15 +0900294 : "=r" (old__), "+m" (var) \
Brian Gerst87b26402009-01-19 00:38:59 +0900295 : "dIr" (bit)); \
Tejun Heo49357d12009-01-13 20:41:35 +0900296 old__; \
297})
298
Ingo Molnar6dbde352009-01-15 22:15:53 +0900299#include <asm-generic/percpu.h>
300
301/* We can use this directly for local CPU (faster). */
302DECLARE_PER_CPU(unsigned long, this_cpu_off);
303
travis@sgi.com33340522008-01-30 13:32:53 +0100304#endif /* !__ASSEMBLY__ */
Mike Travis23ca4bb2008-05-12 21:21:12 +0200305
306#ifdef CONFIG_SMP
307
308/*
309 * Define the "EARLY_PER_CPU" macros. These are used for some per_cpu
310 * variables that are initialized and accessed before there are per_cpu
311 * areas allocated.
312 */
313
314#define DEFINE_EARLY_PER_CPU(_type, _name, _initvalue) \
315 DEFINE_PER_CPU(_type, _name) = _initvalue; \
316 __typeof__(_type) _name##_early_map[NR_CPUS] __initdata = \
317 { [0 ... NR_CPUS-1] = _initvalue }; \
Marcin Slusarzc6a92a22008-08-17 17:50:50 +0200318 __typeof__(_type) *_name##_early_ptr __refdata = _name##_early_map
Mike Travis23ca4bb2008-05-12 21:21:12 +0200319
320#define EXPORT_EARLY_PER_CPU_SYMBOL(_name) \
321 EXPORT_PER_CPU_SYMBOL(_name)
322
323#define DECLARE_EARLY_PER_CPU(_type, _name) \
324 DECLARE_PER_CPU(_type, _name); \
325 extern __typeof__(_type) *_name##_early_ptr; \
326 extern __typeof__(_type) _name##_early_map[]
327
328#define early_per_cpu_ptr(_name) (_name##_early_ptr)
329#define early_per_cpu_map(_name, _idx) (_name##_early_map[_idx])
330#define early_per_cpu(_name, _cpu) \
Tejun Heof10fcd42009-01-13 20:41:34 +0900331 *(early_per_cpu_ptr(_name) ? \
332 &early_per_cpu_ptr(_name)[_cpu] : \
333 &per_cpu(_name, _cpu))
Mike Travis23ca4bb2008-05-12 21:21:12 +0200334
335#else /* !CONFIG_SMP */
336#define DEFINE_EARLY_PER_CPU(_type, _name, _initvalue) \
337 DEFINE_PER_CPU(_type, _name) = _initvalue
338
339#define EXPORT_EARLY_PER_CPU_SYMBOL(_name) \
340 EXPORT_PER_CPU_SYMBOL(_name)
341
342#define DECLARE_EARLY_PER_CPU(_type, _name) \
343 DECLARE_PER_CPU(_type, _name)
344
345#define early_per_cpu(_name, _cpu) per_cpu(_name, _cpu)
346#define early_per_cpu_ptr(_name) NULL
347/* no early_per_cpu_map() */
348
349#endif /* !CONFIG_SMP */
350
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700351#endif /* _ASM_X86_PERCPU_H */