blob: 4c170ccc72ed6b70491eca976cd5d45abae8b510 [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
Linus Torvaldsed8d9ad2009-08-03 14:08:48 +0900106#define percpu_from_op(op, var, constraint) \
Joe Perchesbc9e3be2008-03-23 01:03:06 -0700107({ \
Tejun Heo0f5e4812009-10-29 22:34:12 +0900108 typeof(var) pfo_ret__; \
Joe Perchesbc9e3be2008-03-23 01:03:06 -0700109 switch (sizeof(var)) { \
110 case 1: \
Brian Gerst87b26402009-01-19 00:38:59 +0900111 asm(op "b "__percpu_arg(1)",%0" \
Tejun Heo0f5e4812009-10-29 22:34:12 +0900112 : "=q" (pfo_ret__) \
Linus Torvaldsed8d9ad2009-08-03 14:08:48 +0900113 : constraint); \
Joe Perchesbc9e3be2008-03-23 01:03:06 -0700114 break; \
115 case 2: \
Brian Gerst87b26402009-01-19 00:38:59 +0900116 asm(op "w "__percpu_arg(1)",%0" \
Tejun Heo0f5e4812009-10-29 22:34:12 +0900117 : "=r" (pfo_ret__) \
Linus Torvaldsed8d9ad2009-08-03 14:08:48 +0900118 : constraint); \
Joe Perchesbc9e3be2008-03-23 01:03:06 -0700119 break; \
120 case 4: \
Brian Gerst87b26402009-01-19 00:38:59 +0900121 asm(op "l "__percpu_arg(1)",%0" \
Tejun Heo0f5e4812009-10-29 22:34:12 +0900122 : "=r" (pfo_ret__) \
Linus Torvaldsed8d9ad2009-08-03 14:08:48 +0900123 : constraint); \
Tejun Heo9939dda2009-01-13 20:41:35 +0900124 break; \
125 case 8: \
Brian Gerst87b26402009-01-19 00:38:59 +0900126 asm(op "q "__percpu_arg(1)",%0" \
Tejun Heo0f5e4812009-10-29 22:34:12 +0900127 : "=r" (pfo_ret__) \
Linus Torvaldsed8d9ad2009-08-03 14:08:48 +0900128 : constraint); \
Joe Perchesbc9e3be2008-03-23 01:03:06 -0700129 break; \
130 default: __bad_percpu_size(); \
131 } \
Tejun Heo0f5e4812009-10-29 22:34:12 +0900132 pfo_ret__; \
Joe Perchesbc9e3be2008-03-23 01:03:06 -0700133})
travis@sgi.com33340522008-01-30 13:32:53 +0100134
Linus Torvaldsed8d9ad2009-08-03 14:08:48 +0900135/*
136 * percpu_read() makes gcc load the percpu variable every time it is
137 * accessed while percpu_read_stable() allows the value to be cached.
138 * percpu_read_stable() is more efficient and can be used if its value
139 * is guaranteed to be valid across cpus. The current users include
140 * get_current() and get_thread_info() both of which are actually
141 * per-thread variables implemented as per-cpu variables and thus
142 * stable for the duration of the respective task.
143 */
Rusty Russelldd17c8f2009-10-29 22:34:15 +0900144#define percpu_read(var) percpu_from_op("mov", var, "m" (var))
145#define percpu_read_stable(var) percpu_from_op("mov", var, "p" (&(var)))
146#define percpu_write(var, val) percpu_to_op("mov", var, val)
147#define percpu_add(var, val) percpu_to_op("add", var, val)
148#define percpu_sub(var, val) percpu_to_op("sub", var, val)
149#define percpu_and(var, val) percpu_to_op("and", var, val)
150#define percpu_or(var, val) percpu_to_op("or", var, val)
151#define percpu_xor(var, val) percpu_to_op("xor", var, val)
Tejun Heo9939dda2009-01-13 20:41:35 +0900152
Christoph Lameter30ed1a72009-10-03 19:48:22 +0900153#define __this_cpu_read_1(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
154#define __this_cpu_read_2(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
155#define __this_cpu_read_4(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
156
157#define __this_cpu_write_1(pcp, val) percpu_to_op("mov", (pcp), val)
158#define __this_cpu_write_2(pcp, val) percpu_to_op("mov", (pcp), val)
159#define __this_cpu_write_4(pcp, val) percpu_to_op("mov", (pcp), val)
160#define __this_cpu_add_1(pcp, val) percpu_to_op("add", (pcp), val)
161#define __this_cpu_add_2(pcp, val) percpu_to_op("add", (pcp), val)
162#define __this_cpu_add_4(pcp, val) percpu_to_op("add", (pcp), val)
163#define __this_cpu_and_1(pcp, val) percpu_to_op("and", (pcp), val)
164#define __this_cpu_and_2(pcp, val) percpu_to_op("and", (pcp), val)
165#define __this_cpu_and_4(pcp, val) percpu_to_op("and", (pcp), val)
166#define __this_cpu_or_1(pcp, val) percpu_to_op("or", (pcp), val)
167#define __this_cpu_or_2(pcp, val) percpu_to_op("or", (pcp), val)
168#define __this_cpu_or_4(pcp, val) percpu_to_op("or", (pcp), val)
169#define __this_cpu_xor_1(pcp, val) percpu_to_op("xor", (pcp), val)
170#define __this_cpu_xor_2(pcp, val) percpu_to_op("xor", (pcp), val)
171#define __this_cpu_xor_4(pcp, val) percpu_to_op("xor", (pcp), val)
172
173#define this_cpu_read_1(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
174#define this_cpu_read_2(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
175#define this_cpu_read_4(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
176#define this_cpu_write_1(pcp, val) percpu_to_op("mov", (pcp), val)
177#define this_cpu_write_2(pcp, val) percpu_to_op("mov", (pcp), val)
178#define this_cpu_write_4(pcp, val) percpu_to_op("mov", (pcp), val)
179#define this_cpu_add_1(pcp, val) percpu_to_op("add", (pcp), val)
180#define this_cpu_add_2(pcp, val) percpu_to_op("add", (pcp), val)
181#define this_cpu_add_4(pcp, val) percpu_to_op("add", (pcp), val)
182#define this_cpu_and_1(pcp, val) percpu_to_op("and", (pcp), val)
183#define this_cpu_and_2(pcp, val) percpu_to_op("and", (pcp), val)
184#define this_cpu_and_4(pcp, val) percpu_to_op("and", (pcp), val)
185#define this_cpu_or_1(pcp, val) percpu_to_op("or", (pcp), val)
186#define this_cpu_or_2(pcp, val) percpu_to_op("or", (pcp), val)
187#define this_cpu_or_4(pcp, val) percpu_to_op("or", (pcp), val)
188#define this_cpu_xor_1(pcp, val) percpu_to_op("xor", (pcp), val)
189#define this_cpu_xor_2(pcp, val) percpu_to_op("xor", (pcp), val)
190#define this_cpu_xor_4(pcp, val) percpu_to_op("xor", (pcp), val)
191
192#define irqsafe_cpu_add_1(pcp, val) percpu_to_op("add", (pcp), val)
193#define irqsafe_cpu_add_2(pcp, val) percpu_to_op("add", (pcp), val)
194#define irqsafe_cpu_add_4(pcp, val) percpu_to_op("add", (pcp), val)
195#define irqsafe_cpu_and_1(pcp, val) percpu_to_op("and", (pcp), val)
196#define irqsafe_cpu_and_2(pcp, val) percpu_to_op("and", (pcp), val)
197#define irqsafe_cpu_and_4(pcp, val) percpu_to_op("and", (pcp), val)
198#define irqsafe_cpu_or_1(pcp, val) percpu_to_op("or", (pcp), val)
199#define irqsafe_cpu_or_2(pcp, val) percpu_to_op("or", (pcp), val)
200#define irqsafe_cpu_or_4(pcp, val) percpu_to_op("or", (pcp), val)
201#define irqsafe_cpu_xor_1(pcp, val) percpu_to_op("xor", (pcp), val)
202#define irqsafe_cpu_xor_2(pcp, val) percpu_to_op("xor", (pcp), val)
203#define irqsafe_cpu_xor_4(pcp, val) percpu_to_op("xor", (pcp), val)
204
205/*
206 * Per cpu atomic 64 bit operations are only available under 64 bit.
207 * 32 bit must fall back to generic operations.
208 */
209#ifdef CONFIG_X86_64
210#define __this_cpu_read_8(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
211#define __this_cpu_write_8(pcp, val) percpu_to_op("mov", (pcp), val)
212#define __this_cpu_add_8(pcp, val) percpu_to_op("add", (pcp), val)
213#define __this_cpu_and_8(pcp, val) percpu_to_op("and", (pcp), val)
214#define __this_cpu_or_8(pcp, val) percpu_to_op("or", (pcp), val)
215#define __this_cpu_xor_8(pcp, val) percpu_to_op("xor", (pcp), val)
216
217#define this_cpu_read_8(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
218#define this_cpu_write_8(pcp, val) percpu_to_op("mov", (pcp), val)
219#define this_cpu_add_8(pcp, val) percpu_to_op("add", (pcp), val)
220#define this_cpu_and_8(pcp, val) percpu_to_op("and", (pcp), val)
221#define this_cpu_or_8(pcp, val) percpu_to_op("or", (pcp), val)
222#define this_cpu_xor_8(pcp, val) percpu_to_op("xor", (pcp), val)
223
224#define irqsafe_cpu_add_8(pcp, val) percpu_to_op("add", (pcp), val)
225#define irqsafe_cpu_and_8(pcp, val) percpu_to_op("and", (pcp), val)
226#define irqsafe_cpu_or_8(pcp, val) percpu_to_op("or", (pcp), val)
227#define irqsafe_cpu_xor_8(pcp, val) percpu_to_op("xor", (pcp), val)
228
229#endif
230
Tejun Heo49357d12009-01-13 20:41:35 +0900231/* This is not atomic against other CPUs -- CPU preemption needs to be off */
232#define x86_test_and_clear_bit_percpu(bit, var) \
233({ \
234 int old__; \
Brian Gerst87b26402009-01-19 00:38:59 +0900235 asm volatile("btr %2,"__percpu_arg(1)"\n\tsbbl %0,%0" \
Rusty Russelldd17c8f2009-10-29 22:34:15 +0900236 : "=r" (old__), "+m" (var) \
Brian Gerst87b26402009-01-19 00:38:59 +0900237 : "dIr" (bit)); \
Tejun Heo49357d12009-01-13 20:41:35 +0900238 old__; \
239})
240
Ingo Molnar6dbde352009-01-15 22:15:53 +0900241#include <asm-generic/percpu.h>
242
243/* We can use this directly for local CPU (faster). */
244DECLARE_PER_CPU(unsigned long, this_cpu_off);
245
travis@sgi.com33340522008-01-30 13:32:53 +0100246#endif /* !__ASSEMBLY__ */
Mike Travis23ca4bb2008-05-12 21:21:12 +0200247
248#ifdef CONFIG_SMP
249
250/*
251 * Define the "EARLY_PER_CPU" macros. These are used for some per_cpu
252 * variables that are initialized and accessed before there are per_cpu
253 * areas allocated.
254 */
255
256#define DEFINE_EARLY_PER_CPU(_type, _name, _initvalue) \
257 DEFINE_PER_CPU(_type, _name) = _initvalue; \
258 __typeof__(_type) _name##_early_map[NR_CPUS] __initdata = \
259 { [0 ... NR_CPUS-1] = _initvalue }; \
Marcin Slusarzc6a92a22008-08-17 17:50:50 +0200260 __typeof__(_type) *_name##_early_ptr __refdata = _name##_early_map
Mike Travis23ca4bb2008-05-12 21:21:12 +0200261
262#define EXPORT_EARLY_PER_CPU_SYMBOL(_name) \
263 EXPORT_PER_CPU_SYMBOL(_name)
264
265#define DECLARE_EARLY_PER_CPU(_type, _name) \
266 DECLARE_PER_CPU(_type, _name); \
267 extern __typeof__(_type) *_name##_early_ptr; \
268 extern __typeof__(_type) _name##_early_map[]
269
270#define early_per_cpu_ptr(_name) (_name##_early_ptr)
271#define early_per_cpu_map(_name, _idx) (_name##_early_map[_idx])
272#define early_per_cpu(_name, _cpu) \
Tejun Heof10fcd42009-01-13 20:41:34 +0900273 *(early_per_cpu_ptr(_name) ? \
274 &early_per_cpu_ptr(_name)[_cpu] : \
275 &per_cpu(_name, _cpu))
Mike Travis23ca4bb2008-05-12 21:21:12 +0200276
277#else /* !CONFIG_SMP */
278#define DEFINE_EARLY_PER_CPU(_type, _name, _initvalue) \
279 DEFINE_PER_CPU(_type, _name) = _initvalue
280
281#define EXPORT_EARLY_PER_CPU_SYMBOL(_name) \
282 EXPORT_PER_CPU_SYMBOL(_name)
283
284#define DECLARE_EARLY_PER_CPU(_type, _name) \
285 DECLARE_PER_CPU(_type, _name)
286
287#define early_per_cpu(_name, _cpu) per_cpu(_name, _cpu)
288#define early_per_cpu_ptr(_name) NULL
289/* no early_per_cpu_map() */
290
291#endif /* !CONFIG_SMP */
292
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700293#endif /* _ASM_X86_PERCPU_H */