1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_PERCPU_H
3 #define _ASM_X86_PERCPU_H
6 # define __percpu_seg gs
7 # define __percpu_rel (%rip)
9 # define __percpu_seg fs
16 # define __percpu %__percpu_seg:
21 #define PER_CPU_VAR(var) __percpu(var)__percpu_rel
23 #else /* !__ASSEMBLY__: */
25 #include <linux/args.h>
26 #include <linux/build_bug.h>
27 #include <linux/stringify.h>
32 #define __force_percpu_prefix "%%"__stringify(__percpu_seg)":"
34 #ifdef CONFIG_CC_HAS_NAMED_AS
37 # define __seg_gs __attribute__((address_space(__seg_gs)))
38 # define __seg_fs __attribute__((address_space(__seg_fs)))
41 #define __percpu_prefix
42 #define __percpu_seg_override CONCATENATE(__seg_, __percpu_seg)
44 #else /* !CONFIG_CC_HAS_NAMED_AS: */
46 #define __percpu_prefix __force_percpu_prefix
47 #define __percpu_seg_override
49 #endif /* CONFIG_CC_HAS_NAMED_AS */
52 * Compared to the generic __my_cpu_offset version, the following
53 * saves one instruction and avoids clobbering a temp register.
55 #define __my_cpu_offset this_cpu_read(this_cpu_off)
58 * arch_raw_cpu_ptr should not be used in 32-bit VDSO for a 64-bit
59 * kernel, because games are played with CONFIG_X86_64 there and
60 * sizeof(this_cpu_off) becames 4.
62 #ifndef BUILD_VDSO32_64
63 #define arch_raw_cpu_ptr(_ptr) \
65 unsigned long tcp_ptr__ = raw_cpu_read_long(this_cpu_off); \
67 tcp_ptr__ += (__force unsigned long)(_ptr); \
68 (TYPEOF_UNQUAL(*(_ptr)) __force __kernel *)tcp_ptr__; \
71 #define arch_raw_cpu_ptr(_ptr) \
74 (TYPEOF_UNQUAL(*(_ptr)) __force __kernel *)0; \
78 #define PER_CPU_VAR(var) %__percpu_seg:(var)__percpu_rel
80 #else /* !CONFIG_SMP: */
82 #define __force_percpu_prefix
83 #define __percpu_prefix
84 #define __percpu_seg_override
86 #define PER_CPU_VAR(var) (var)__percpu_rel
88 #endif /* CONFIG_SMP */
90 #if defined(CONFIG_USE_X86_SEG_SUPPORT) && defined(USE_TYPEOF_UNQUAL)
91 # define __my_cpu_type(var) typeof(var)
92 # define __my_cpu_ptr(ptr) (ptr)
93 # define __my_cpu_var(var) (var)
95 # define __percpu_qual __percpu_seg_override
97 # define __my_cpu_type(var) typeof(var) __percpu_seg_override
98 # define __my_cpu_ptr(ptr) (__my_cpu_type(*(ptr))*)(__force uintptr_t)(ptr)
99 # define __my_cpu_var(var) (*__my_cpu_ptr(&(var)))
102 #define __force_percpu_arg(x) __force_percpu_prefix "%" #x
103 #define __percpu_arg(x) __percpu_prefix "%" #x
106 * For arch-specific code, we can use direct single-insn ops (they
107 * don't give an lvalue though).
110 #define __pcpu_type_1 u8
111 #define __pcpu_type_2 u16
112 #define __pcpu_type_4 u32
113 #define __pcpu_type_8 u64
115 #define __pcpu_cast_1(val) ((u8)(((unsigned long) val) & 0xff))
116 #define __pcpu_cast_2(val) ((u16)(((unsigned long) val) & 0xffff))
117 #define __pcpu_cast_4(val) ((u32)(((unsigned long) val) & 0xffffffff))
118 #define __pcpu_cast_8(val) ((u64)(val))
120 #define __pcpu_op_1(op) op "b "
121 #define __pcpu_op_2(op) op "w "
122 #define __pcpu_op_4(op) op "l "
123 #define __pcpu_op_8(op) op "q "
125 #define __pcpu_reg_1(mod, x) mod "q" (x)
126 #define __pcpu_reg_2(mod, x) mod "r" (x)
127 #define __pcpu_reg_4(mod, x) mod "r" (x)
128 #define __pcpu_reg_8(mod, x) mod "r" (x)
130 #define __pcpu_reg_imm_1(x) "qi" (x)
131 #define __pcpu_reg_imm_2(x) "ri" (x)
132 #define __pcpu_reg_imm_4(x) "ri" (x)
133 #define __pcpu_reg_imm_8(x) "re" (x)
135 #ifdef CONFIG_USE_X86_SEG_SUPPORT
137 #define __raw_cpu_read(size, qual, pcp) \
139 *(qual __my_cpu_type(pcp) *)__my_cpu_ptr(&(pcp)); \
142 #define __raw_cpu_write(size, qual, pcp, val) \
144 *(qual __my_cpu_type(pcp) *)__my_cpu_ptr(&(pcp)) = (val); \
147 #define __raw_cpu_read_const(pcp) __raw_cpu_read(, , pcp)
149 #else /* !CONFIG_USE_X86_SEG_SUPPORT: */
151 #define __raw_cpu_read(size, qual, _var) \
153 __pcpu_type_##size pfo_val__; \
155 asm qual (__pcpu_op_##size("mov") \
156 __percpu_arg([var]) ", %[val]" \
157 : [val] __pcpu_reg_##size("=", pfo_val__) \
158 : [var] "m" (__my_cpu_var(_var))); \
160 (typeof(_var))(unsigned long) pfo_val__; \
163 #define __raw_cpu_write(size, qual, _var, _val) \
165 __pcpu_type_##size pto_val__ = __pcpu_cast_##size(_val); \
168 TYPEOF_UNQUAL(_var) pto_tmp__; \
169 pto_tmp__ = (_val); \
172 asm qual (__pcpu_op_##size("mov") "%[val], " \
173 __percpu_arg([var]) \
174 : [var] "=m" (__my_cpu_var(_var)) \
175 : [val] __pcpu_reg_imm_##size(pto_val__)); \
179 * The generic per-CPU infrastrucutre is not suitable for
180 * reading const-qualified variables.
182 #define __raw_cpu_read_const(pcp) ({ BUILD_BUG(); (typeof(pcp))0; })
184 #endif /* CONFIG_USE_X86_SEG_SUPPORT */
186 #define __raw_cpu_read_stable(size, _var) \
188 __pcpu_type_##size pfo_val__; \
190 asm(__pcpu_op_##size("mov") \
191 __force_percpu_arg(a[var]) ", %[val]" \
192 : [val] __pcpu_reg_##size("=", pfo_val__) \
193 : [var] "i" (&(_var))); \
195 (typeof(_var))(unsigned long) pfo_val__; \
198 #define percpu_unary_op(size, qual, op, _var) \
200 asm qual (__pcpu_op_##size(op) __percpu_arg([var]) \
201 : [var] "+m" (__my_cpu_var(_var))); \
204 #define percpu_binary_op(size, qual, op, _var, _val) \
206 __pcpu_type_##size pto_val__ = __pcpu_cast_##size(_val); \
209 TYPEOF_UNQUAL(_var) pto_tmp__; \
210 pto_tmp__ = (_val); \
213 asm qual (__pcpu_op_##size(op) "%[val], " __percpu_arg([var]) \
214 : [var] "+m" (__my_cpu_var(_var)) \
215 : [val] __pcpu_reg_imm_##size(pto_val__)); \
219 * Generate a per-CPU add to memory instruction and optimize code
220 * if one is added or subtracted.
222 #define percpu_add_op(size, qual, var, val) \
224 const int pao_ID__ = \
225 (__builtin_constant_p(val) && \
227 (val) == (typeof(val))-1)) ? (int)(val) : 0; \
230 TYPEOF_UNQUAL(var) pao_tmp__; \
235 percpu_unary_op(size, qual, "inc", var); \
236 else if (pao_ID__ == -1) \
237 percpu_unary_op(size, qual, "dec", var); \
239 percpu_binary_op(size, qual, "add", var, val); \
243 * Add return operation
245 #define percpu_add_return_op(size, qual, _var, _val) \
247 __pcpu_type_##size paro_tmp__ = __pcpu_cast_##size(_val); \
249 asm qual (__pcpu_op_##size("xadd") "%[tmp], " \
250 __percpu_arg([var]) \
251 : [tmp] __pcpu_reg_##size("+", paro_tmp__), \
252 [var] "+m" (__my_cpu_var(_var)) \
254 (typeof(_var))(unsigned long) (paro_tmp__ + _val); \
258 * raw_cpu_xchg() can use a load-store since
259 * it is not required to be IRQ-safe.
261 #define raw_percpu_xchg_op(_var, _nval) \
263 TYPEOF_UNQUAL(_var) pxo_old__ = raw_cpu_read(_var); \
265 raw_cpu_write(_var, _nval); \
271 * this_cpu_xchg() is implemented using CMPXCHG without a LOCK prefix.
272 * XCHG is expensive due to the implied LOCK prefix. The processor
273 * cannot prefetch cachelines if XCHG is used.
275 #define this_percpu_xchg_op(_var, _nval) \
277 TYPEOF_UNQUAL(_var) pxo_old__ = this_cpu_read(_var); \
279 do { } while (!this_cpu_try_cmpxchg(_var, &pxo_old__, _nval)); \
285 * CMPXCHG has no such implied lock semantics as a result it is much
286 * more efficient for CPU-local operations.
288 #define percpu_cmpxchg_op(size, qual, _var, _oval, _nval) \
290 __pcpu_type_##size pco_old__ = __pcpu_cast_##size(_oval); \
291 __pcpu_type_##size pco_new__ = __pcpu_cast_##size(_nval); \
293 asm qual (__pcpu_op_##size("cmpxchg") "%[nval], " \
294 __percpu_arg([var]) \
295 : [oval] "+a" (pco_old__), \
296 [var] "+m" (__my_cpu_var(_var)) \
297 : [nval] __pcpu_reg_##size(, pco_new__) \
300 (typeof(_var))(unsigned long) pco_old__; \
303 #define percpu_try_cmpxchg_op(size, qual, _var, _ovalp, _nval) \
306 __pcpu_type_##size *pco_oval__ = (__pcpu_type_##size *)(_ovalp); \
307 __pcpu_type_##size pco_old__ = *pco_oval__; \
308 __pcpu_type_##size pco_new__ = __pcpu_cast_##size(_nval); \
310 asm qual (__pcpu_op_##size("cmpxchg") "%[nval], " \
311 __percpu_arg([var]) \
313 : CC_OUT(z) (success), \
314 [oval] "+a" (pco_old__), \
315 [var] "+m" (__my_cpu_var(_var)) \
316 : [nval] __pcpu_reg_##size(, pco_new__) \
318 if (unlikely(!success)) \
319 *pco_oval__ = pco_old__; \
324 #if defined(CONFIG_X86_32) && !defined(CONFIG_UML)
326 #define percpu_cmpxchg64_op(size, qual, _var, _oval, _nval) \
339 ALTERNATIVE("call this_cpu_cmpxchg8b_emu", \
340 "cmpxchg8b " __percpu_arg([var]), X86_FEATURE_CX8) \
341 : ALT_OUTPUT_SP([var] "+m" (__my_cpu_var(_var)), \
342 "+a" (old__.low), "+d" (old__.high)) \
343 : "b" (new__.low), "c" (new__.high), \
350 #define raw_cpu_cmpxchg64(pcp, oval, nval) percpu_cmpxchg64_op(8, , pcp, oval, nval)
351 #define this_cpu_cmpxchg64(pcp, oval, nval) percpu_cmpxchg64_op(8, volatile, pcp, oval, nval)
353 #define percpu_try_cmpxchg64_op(size, qual, _var, _ovalp, _nval) \
356 u64 *_oval = (u64 *)(_ovalp); \
364 old__.var = *_oval; \
368 ALTERNATIVE("call this_cpu_cmpxchg8b_emu", \
369 "cmpxchg8b " __percpu_arg([var]), X86_FEATURE_CX8) \
371 : ALT_OUTPUT_SP(CC_OUT(z) (success), \
372 [var] "+m" (__my_cpu_var(_var)), \
373 "+a" (old__.low), "+d" (old__.high)) \
374 : "b" (new__.low), "c" (new__.high), \
377 if (unlikely(!success)) \
378 *_oval = old__.var; \
383 #define raw_cpu_try_cmpxchg64(pcp, ovalp, nval) percpu_try_cmpxchg64_op(8, , pcp, ovalp, nval)
384 #define this_cpu_try_cmpxchg64(pcp, ovalp, nval) percpu_try_cmpxchg64_op(8, volatile, pcp, ovalp, nval)
386 #endif /* defined(CONFIG_X86_32) && !defined(CONFIG_UML) */
389 #define raw_cpu_cmpxchg64(pcp, oval, nval) percpu_cmpxchg_op(8, , pcp, oval, nval);
390 #define this_cpu_cmpxchg64(pcp, oval, nval) percpu_cmpxchg_op(8, volatile, pcp, oval, nval);
392 #define raw_cpu_try_cmpxchg64(pcp, ovalp, nval) percpu_try_cmpxchg_op(8, , pcp, ovalp, nval);
393 #define this_cpu_try_cmpxchg64(pcp, ovalp, nval) percpu_try_cmpxchg_op(8, volatile, pcp, ovalp, nval);
395 #define percpu_cmpxchg128_op(size, qual, _var, _oval, _nval) \
408 ALTERNATIVE("call this_cpu_cmpxchg16b_emu", \
409 "cmpxchg16b " __percpu_arg([var]), X86_FEATURE_CX16) \
410 : ALT_OUTPUT_SP([var] "+m" (__my_cpu_var(_var)), \
411 "+a" (old__.low), "+d" (old__.high)) \
412 : "b" (new__.low), "c" (new__.high), \
419 #define raw_cpu_cmpxchg128(pcp, oval, nval) percpu_cmpxchg128_op(16, , pcp, oval, nval)
420 #define this_cpu_cmpxchg128(pcp, oval, nval) percpu_cmpxchg128_op(16, volatile, pcp, oval, nval)
422 #define percpu_try_cmpxchg128_op(size, qual, _var, _ovalp, _nval) \
425 u128 *_oval = (u128 *)(_ovalp); \
433 old__.var = *_oval; \
437 ALTERNATIVE("call this_cpu_cmpxchg16b_emu", \
438 "cmpxchg16b " __percpu_arg([var]), X86_FEATURE_CX16) \
440 : ALT_OUTPUT_SP(CC_OUT(z) (success), \
441 [var] "+m" (__my_cpu_var(_var)), \
442 "+a" (old__.low), "+d" (old__.high)) \
443 : "b" (new__.low), "c" (new__.high), \
446 if (unlikely(!success)) \
447 *_oval = old__.var; \
452 #define raw_cpu_try_cmpxchg128(pcp, ovalp, nval) percpu_try_cmpxchg128_op(16, , pcp, ovalp, nval)
453 #define this_cpu_try_cmpxchg128(pcp, ovalp, nval) percpu_try_cmpxchg128_op(16, volatile, pcp, ovalp, nval)
455 #endif /* CONFIG_X86_64 */
457 #define raw_cpu_read_1(pcp) __raw_cpu_read(1, , pcp)
458 #define raw_cpu_read_2(pcp) __raw_cpu_read(2, , pcp)
459 #define raw_cpu_read_4(pcp) __raw_cpu_read(4, , pcp)
460 #define raw_cpu_write_1(pcp, val) __raw_cpu_write(1, , pcp, val)
461 #define raw_cpu_write_2(pcp, val) __raw_cpu_write(2, , pcp, val)
462 #define raw_cpu_write_4(pcp, val) __raw_cpu_write(4, , pcp, val)
464 #define this_cpu_read_1(pcp) __raw_cpu_read(1, volatile, pcp)
465 #define this_cpu_read_2(pcp) __raw_cpu_read(2, volatile, pcp)
466 #define this_cpu_read_4(pcp) __raw_cpu_read(4, volatile, pcp)
467 #define this_cpu_write_1(pcp, val) __raw_cpu_write(1, volatile, pcp, val)
468 #define this_cpu_write_2(pcp, val) __raw_cpu_write(2, volatile, pcp, val)
469 #define this_cpu_write_4(pcp, val) __raw_cpu_write(4, volatile, pcp, val)
471 #define this_cpu_read_stable_1(pcp) __raw_cpu_read_stable(1, pcp)
472 #define this_cpu_read_stable_2(pcp) __raw_cpu_read_stable(2, pcp)
473 #define this_cpu_read_stable_4(pcp) __raw_cpu_read_stable(4, pcp)
475 #define raw_cpu_add_1(pcp, val) percpu_add_op(1, , (pcp), val)
476 #define raw_cpu_add_2(pcp, val) percpu_add_op(2, , (pcp), val)
477 #define raw_cpu_add_4(pcp, val) percpu_add_op(4, , (pcp), val)
478 #define raw_cpu_and_1(pcp, val) percpu_binary_op(1, , "and", (pcp), val)
479 #define raw_cpu_and_2(pcp, val) percpu_binary_op(2, , "and", (pcp), val)
480 #define raw_cpu_and_4(pcp, val) percpu_binary_op(4, , "and", (pcp), val)
481 #define raw_cpu_or_1(pcp, val) percpu_binary_op(1, , "or", (pcp), val)
482 #define raw_cpu_or_2(pcp, val) percpu_binary_op(2, , "or", (pcp), val)
483 #define raw_cpu_or_4(pcp, val) percpu_binary_op(4, , "or", (pcp), val)
484 #define raw_cpu_xchg_1(pcp, val) raw_percpu_xchg_op(pcp, val)
485 #define raw_cpu_xchg_2(pcp, val) raw_percpu_xchg_op(pcp, val)
486 #define raw_cpu_xchg_4(pcp, val) raw_percpu_xchg_op(pcp, val)
488 #define this_cpu_add_1(pcp, val) percpu_add_op(1, volatile, (pcp), val)
489 #define this_cpu_add_2(pcp, val) percpu_add_op(2, volatile, (pcp), val)
490 #define this_cpu_add_4(pcp, val) percpu_add_op(4, volatile, (pcp), val)
491 #define this_cpu_and_1(pcp, val) percpu_binary_op(1, volatile, "and", (pcp), val)
492 #define this_cpu_and_2(pcp, val) percpu_binary_op(2, volatile, "and", (pcp), val)
493 #define this_cpu_and_4(pcp, val) percpu_binary_op(4, volatile, "and", (pcp), val)
494 #define this_cpu_or_1(pcp, val) percpu_binary_op(1, volatile, "or", (pcp), val)
495 #define this_cpu_or_2(pcp, val) percpu_binary_op(2, volatile, "or", (pcp), val)
496 #define this_cpu_or_4(pcp, val) percpu_binary_op(4, volatile, "or", (pcp), val)
497 #define this_cpu_xchg_1(pcp, nval) this_percpu_xchg_op(pcp, nval)
498 #define this_cpu_xchg_2(pcp, nval) this_percpu_xchg_op(pcp, nval)
499 #define this_cpu_xchg_4(pcp, nval) this_percpu_xchg_op(pcp, nval)
501 #define raw_cpu_add_return_1(pcp, val) percpu_add_return_op(1, , pcp, val)
502 #define raw_cpu_add_return_2(pcp, val) percpu_add_return_op(2, , pcp, val)
503 #define raw_cpu_add_return_4(pcp, val) percpu_add_return_op(4, , pcp, val)
504 #define raw_cpu_cmpxchg_1(pcp, oval, nval) percpu_cmpxchg_op(1, , pcp, oval, nval)
505 #define raw_cpu_cmpxchg_2(pcp, oval, nval) percpu_cmpxchg_op(2, , pcp, oval, nval)
506 #define raw_cpu_cmpxchg_4(pcp, oval, nval) percpu_cmpxchg_op(4, , pcp, oval, nval)
507 #define raw_cpu_try_cmpxchg_1(pcp, ovalp, nval) percpu_try_cmpxchg_op(1, , pcp, ovalp, nval)
508 #define raw_cpu_try_cmpxchg_2(pcp, ovalp, nval) percpu_try_cmpxchg_op(2, , pcp, ovalp, nval)
509 #define raw_cpu_try_cmpxchg_4(pcp, ovalp, nval) percpu_try_cmpxchg_op(4, , pcp, ovalp, nval)
511 #define this_cpu_add_return_1(pcp, val) percpu_add_return_op(1, volatile, pcp, val)
512 #define this_cpu_add_return_2(pcp, val) percpu_add_return_op(2, volatile, pcp, val)
513 #define this_cpu_add_return_4(pcp, val) percpu_add_return_op(4, volatile, pcp, val)
514 #define this_cpu_cmpxchg_1(pcp, oval, nval) percpu_cmpxchg_op(1, volatile, pcp, oval, nval)
515 #define this_cpu_cmpxchg_2(pcp, oval, nval) percpu_cmpxchg_op(2, volatile, pcp, oval, nval)
516 #define this_cpu_cmpxchg_4(pcp, oval, nval) percpu_cmpxchg_op(4, volatile, pcp, oval, nval)
517 #define this_cpu_try_cmpxchg_1(pcp, ovalp, nval) percpu_try_cmpxchg_op(1, volatile, pcp, ovalp, nval)
518 #define this_cpu_try_cmpxchg_2(pcp, ovalp, nval) percpu_try_cmpxchg_op(2, volatile, pcp, ovalp, nval)
519 #define this_cpu_try_cmpxchg_4(pcp, ovalp, nval) percpu_try_cmpxchg_op(4, volatile, pcp, ovalp, nval)
522 * Per-CPU atomic 64-bit operations are only available under 64-bit kernels.
523 * 32-bit kernels must fall back to generic operations.
527 #define raw_cpu_read_8(pcp) __raw_cpu_read(8, , pcp)
528 #define raw_cpu_write_8(pcp, val) __raw_cpu_write(8, , pcp, val)
530 #define this_cpu_read_8(pcp) __raw_cpu_read(8, volatile, pcp)
531 #define this_cpu_write_8(pcp, val) __raw_cpu_write(8, volatile, pcp, val)
533 #define this_cpu_read_stable_8(pcp) __raw_cpu_read_stable(8, pcp)
535 #define raw_cpu_add_8(pcp, val) percpu_add_op(8, , (pcp), val)
536 #define raw_cpu_and_8(pcp, val) percpu_binary_op(8, , "and", (pcp), val)
537 #define raw_cpu_or_8(pcp, val) percpu_binary_op(8, , "or", (pcp), val)
538 #define raw_cpu_add_return_8(pcp, val) percpu_add_return_op(8, , pcp, val)
539 #define raw_cpu_xchg_8(pcp, nval) raw_percpu_xchg_op(pcp, nval)
540 #define raw_cpu_cmpxchg_8(pcp, oval, nval) percpu_cmpxchg_op(8, , pcp, oval, nval)
541 #define raw_cpu_try_cmpxchg_8(pcp, ovalp, nval) percpu_try_cmpxchg_op(8, , pcp, ovalp, nval)
543 #define this_cpu_add_8(pcp, val) percpu_add_op(8, volatile, (pcp), val)
544 #define this_cpu_and_8(pcp, val) percpu_binary_op(8, volatile, "and", (pcp), val)
545 #define this_cpu_or_8(pcp, val) percpu_binary_op(8, volatile, "or", (pcp), val)
546 #define this_cpu_add_return_8(pcp, val) percpu_add_return_op(8, volatile, pcp, val)
547 #define this_cpu_xchg_8(pcp, nval) this_percpu_xchg_op(pcp, nval)
548 #define this_cpu_cmpxchg_8(pcp, oval, nval) percpu_cmpxchg_op(8, volatile, pcp, oval, nval)
549 #define this_cpu_try_cmpxchg_8(pcp, ovalp, nval) percpu_try_cmpxchg_op(8, volatile, pcp, ovalp, nval)
551 #define raw_cpu_read_long(pcp) raw_cpu_read_8(pcp)
553 #else /* !CONFIG_X86_64: */
555 /* There is no generic 64-bit read stable operation for 32-bit targets. */
556 #define this_cpu_read_stable_8(pcp) ({ BUILD_BUG(); (typeof(pcp))0; })
558 #define raw_cpu_read_long(pcp) raw_cpu_read_4(pcp)
560 #endif /* CONFIG_X86_64 */
562 #define this_cpu_read_const(pcp) __raw_cpu_read_const(pcp)
565 * this_cpu_read() makes the compiler load the per-CPU variable every time
566 * it is accessed while this_cpu_read_stable() allows the value to be cached.
567 * this_cpu_read_stable() is more efficient and can be used if its value
568 * is guaranteed to be valid across CPUs. The current users include
569 * current_task and cpu_current_top_of_stack, both of which are
570 * actually per-thread variables implemented as per-CPU variables and
571 * thus stable for the duration of the respective task.
573 #define this_cpu_read_stable(pcp) __pcpu_size_call_return(this_cpu_read_stable_, pcp)
575 #define x86_this_cpu_constant_test_bit(_nr, _var) \
577 unsigned long __percpu *addr__ = \
578 (unsigned long __percpu *)&(_var) + ((_nr) / BITS_PER_LONG); \
580 !!((1UL << ((_nr) % BITS_PER_LONG)) & raw_cpu_read(*addr__)); \
583 #define x86_this_cpu_variable_test_bit(_nr, _var) \
587 asm volatile("btl %[nr], " __percpu_arg([var]) \
589 : CC_OUT(c) (oldbit) \
590 : [var] "m" (__my_cpu_var(_var)), \
595 #define x86_this_cpu_test_bit(_nr, _var) \
596 (__builtin_constant_p(_nr) \
597 ? x86_this_cpu_constant_test_bit(_nr, _var) \
598 : x86_this_cpu_variable_test_bit(_nr, _var))
601 #include <asm-generic/percpu.h>
603 /* We can use this directly for local CPU (faster). */
604 DECLARE_PER_CPU_CACHE_HOT(unsigned long, this_cpu_off);
606 #endif /* !__ASSEMBLER__ */
611 * Define the "EARLY_PER_CPU" macros. These are used for some per_cpu
612 * variables that are initialized and accessed before there are per_cpu
616 #define DEFINE_EARLY_PER_CPU(_type, _name, _initvalue) \
617 DEFINE_PER_CPU(_type, _name) = _initvalue; \
618 __typeof__(_type) _name##_early_map[NR_CPUS] __initdata = \
619 { [0 ... NR_CPUS-1] = _initvalue }; \
620 __typeof__(_type) *_name##_early_ptr __refdata = _name##_early_map
622 #define DEFINE_EARLY_PER_CPU_READ_MOSTLY(_type, _name, _initvalue) \
623 DEFINE_PER_CPU_READ_MOSTLY(_type, _name) = _initvalue; \
624 __typeof__(_type) _name##_early_map[NR_CPUS] __initdata = \
625 { [0 ... NR_CPUS-1] = _initvalue }; \
626 __typeof__(_type) *_name##_early_ptr __refdata = _name##_early_map
628 #define EXPORT_EARLY_PER_CPU_SYMBOL(_name) \
629 EXPORT_PER_CPU_SYMBOL(_name)
631 #define DECLARE_EARLY_PER_CPU(_type, _name) \
632 DECLARE_PER_CPU(_type, _name); \
633 extern __typeof__(_type) *_name##_early_ptr; \
634 extern __typeof__(_type) _name##_early_map[]
636 #define DECLARE_EARLY_PER_CPU_READ_MOSTLY(_type, _name) \
637 DECLARE_PER_CPU_READ_MOSTLY(_type, _name); \
638 extern __typeof__(_type) *_name##_early_ptr; \
639 extern __typeof__(_type) _name##_early_map[]
641 #define early_per_cpu_ptr(_name) (_name##_early_ptr)
642 #define early_per_cpu_map(_name, _idx) (_name##_early_map[_idx])
644 #define early_per_cpu(_name, _cpu) \
645 *(early_per_cpu_ptr(_name) ? \
646 &early_per_cpu_ptr(_name)[_cpu] : \
647 &per_cpu(_name, _cpu))
649 #else /* !CONFIG_SMP: */
650 #define DEFINE_EARLY_PER_CPU(_type, _name, _initvalue) \
651 DEFINE_PER_CPU(_type, _name) = _initvalue
653 #define DEFINE_EARLY_PER_CPU_READ_MOSTLY(_type, _name, _initvalue) \
654 DEFINE_PER_CPU_READ_MOSTLY(_type, _name) = _initvalue
656 #define EXPORT_EARLY_PER_CPU_SYMBOL(_name) \
657 EXPORT_PER_CPU_SYMBOL(_name)
659 #define DECLARE_EARLY_PER_CPU(_type, _name) \
660 DECLARE_PER_CPU(_type, _name)
662 #define DECLARE_EARLY_PER_CPU_READ_MOSTLY(_type, _name) \
663 DECLARE_PER_CPU_READ_MOSTLY(_type, _name)
665 #define early_per_cpu(_name, _cpu) per_cpu(_name, _cpu)
666 #define early_per_cpu_ptr(_name) NULL
667 /* no early_per_cpu_map() */
669 #endif /* !CONFIG_SMP */
671 #endif /* _ASM_X86_PERCPU_H */