1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_GENERIC_PERCPU_H_
3 #define _ASM_GENERIC_PERCPU_H_
5 #include <linux/compiler.h>
6 #include <linux/threads.h>
7 #include <linux/percpu-defs.h>
12 * per_cpu_offset() is the offset that has to be added to a
13 * percpu variable to get to the instance for a certain processor.
15 * Most arches use the __per_cpu_offset array for those offsets but
16 * some arches have their own ways of determining the offset (x86_64, s390).
18 #ifndef __per_cpu_offset
19 extern unsigned long __per_cpu_offset
[NR_CPUS
];
21 #define per_cpu_offset(x) (__per_cpu_offset[x])
25 * Determine the offset for the currently active processor.
26 * An arch may define __my_cpu_offset to provide a more effective
27 * means of obtaining the offset to the per cpu variables of the
30 #ifndef __my_cpu_offset
31 #define __my_cpu_offset per_cpu_offset(raw_smp_processor_id())
33 #ifdef CONFIG_DEBUG_PREEMPT
34 #define my_cpu_offset per_cpu_offset(smp_processor_id())
36 #define my_cpu_offset __my_cpu_offset
40 * Arch may define arch_raw_cpu_ptr() to provide more efficient address
41 * translations for raw_cpu_ptr().
43 #ifndef arch_raw_cpu_ptr
44 #define arch_raw_cpu_ptr(ptr) SHIFT_PERCPU_PTR(ptr, __my_cpu_offset)
47 #ifdef CONFIG_HAVE_SETUP_PER_CPU_AREA
48 extern void setup_per_cpu_areas(void);
53 #ifndef PER_CPU_BASE_SECTION
55 #define PER_CPU_BASE_SECTION ".data..percpu"
57 #define PER_CPU_BASE_SECTION ".data"
61 #ifndef PER_CPU_ATTRIBUTES
62 #define PER_CPU_ATTRIBUTES
65 #define raw_cpu_generic_read(pcp) \
67 *raw_cpu_ptr(&(pcp)); \
70 #define raw_cpu_generic_to_op(pcp, val, op) \
72 *raw_cpu_ptr(&(pcp)) op val; \
75 #define raw_cpu_generic_add_return(pcp, val) \
77 typeof(pcp) *__p = raw_cpu_ptr(&(pcp)); \
83 #define raw_cpu_generic_xchg(pcp, nval) \
85 typeof(pcp) *__p = raw_cpu_ptr(&(pcp)); \
92 #define __cpu_fallback_try_cmpxchg(pcp, ovalp, nval, _cmpxchg) \
94 typeof(pcp) __val, __old = *(ovalp); \
95 __val = _cmpxchg(pcp, __old, nval); \
101 #define raw_cpu_generic_try_cmpxchg(pcp, ovalp, nval) \
103 typeof(pcp) *__p = raw_cpu_ptr(&(pcp)); \
104 typeof(pcp) __val = *__p, ___old = *(ovalp); \
106 if (__val == ___old) { \
116 #define raw_cpu_generic_cmpxchg(pcp, oval, nval) \
118 typeof(pcp) __old = (oval); \
119 raw_cpu_generic_try_cmpxchg(pcp, &__old, nval); \
123 #define __this_cpu_generic_read_nopreempt(pcp) \
125 typeof(pcp) ___ret; \
126 preempt_disable_notrace(); \
127 ___ret = READ_ONCE(*raw_cpu_ptr(&(pcp))); \
128 preempt_enable_notrace(); \
132 #define __this_cpu_generic_read_noirq(pcp) \
134 typeof(pcp) ___ret; \
135 unsigned long ___flags; \
136 raw_local_irq_save(___flags); \
137 ___ret = raw_cpu_generic_read(pcp); \
138 raw_local_irq_restore(___flags); \
142 #define this_cpu_generic_read(pcp) \
145 if (__native_word(pcp)) \
146 __ret = __this_cpu_generic_read_nopreempt(pcp); \
148 __ret = __this_cpu_generic_read_noirq(pcp); \
152 #define this_cpu_generic_to_op(pcp, val, op) \
154 unsigned long __flags; \
155 raw_local_irq_save(__flags); \
156 raw_cpu_generic_to_op(pcp, val, op); \
157 raw_local_irq_restore(__flags); \
161 #define this_cpu_generic_add_return(pcp, val) \
164 unsigned long __flags; \
165 raw_local_irq_save(__flags); \
166 __ret = raw_cpu_generic_add_return(pcp, val); \
167 raw_local_irq_restore(__flags); \
171 #define this_cpu_generic_xchg(pcp, nval) \
174 unsigned long __flags; \
175 raw_local_irq_save(__flags); \
176 __ret = raw_cpu_generic_xchg(pcp, nval); \
177 raw_local_irq_restore(__flags); \
181 #define this_cpu_generic_try_cmpxchg(pcp, ovalp, nval) \
184 unsigned long __flags; \
185 raw_local_irq_save(__flags); \
186 __ret = raw_cpu_generic_try_cmpxchg(pcp, ovalp, nval); \
187 raw_local_irq_restore(__flags); \
191 #define this_cpu_generic_cmpxchg(pcp, oval, nval) \
194 unsigned long __flags; \
195 raw_local_irq_save(__flags); \
196 __ret = raw_cpu_generic_cmpxchg(pcp, oval, nval); \
197 raw_local_irq_restore(__flags); \
201 #ifndef raw_cpu_read_1
202 #define raw_cpu_read_1(pcp) raw_cpu_generic_read(pcp)
204 #ifndef raw_cpu_read_2
205 #define raw_cpu_read_2(pcp) raw_cpu_generic_read(pcp)
207 #ifndef raw_cpu_read_4
208 #define raw_cpu_read_4(pcp) raw_cpu_generic_read(pcp)
210 #ifndef raw_cpu_read_8
211 #define raw_cpu_read_8(pcp) raw_cpu_generic_read(pcp)
214 #ifndef raw_cpu_write_1
215 #define raw_cpu_write_1(pcp, val) raw_cpu_generic_to_op(pcp, val, =)
217 #ifndef raw_cpu_write_2
218 #define raw_cpu_write_2(pcp, val) raw_cpu_generic_to_op(pcp, val, =)
220 #ifndef raw_cpu_write_4
221 #define raw_cpu_write_4(pcp, val) raw_cpu_generic_to_op(pcp, val, =)
223 #ifndef raw_cpu_write_8
224 #define raw_cpu_write_8(pcp, val) raw_cpu_generic_to_op(pcp, val, =)
227 #ifndef raw_cpu_add_1
228 #define raw_cpu_add_1(pcp, val) raw_cpu_generic_to_op(pcp, val, +=)
230 #ifndef raw_cpu_add_2
231 #define raw_cpu_add_2(pcp, val) raw_cpu_generic_to_op(pcp, val, +=)
233 #ifndef raw_cpu_add_4
234 #define raw_cpu_add_4(pcp, val) raw_cpu_generic_to_op(pcp, val, +=)
236 #ifndef raw_cpu_add_8
237 #define raw_cpu_add_8(pcp, val) raw_cpu_generic_to_op(pcp, val, +=)
240 #ifndef raw_cpu_and_1
241 #define raw_cpu_and_1(pcp, val) raw_cpu_generic_to_op(pcp, val, &=)
243 #ifndef raw_cpu_and_2
244 #define raw_cpu_and_2(pcp, val) raw_cpu_generic_to_op(pcp, val, &=)
246 #ifndef raw_cpu_and_4
247 #define raw_cpu_and_4(pcp, val) raw_cpu_generic_to_op(pcp, val, &=)
249 #ifndef raw_cpu_and_8
250 #define raw_cpu_and_8(pcp, val) raw_cpu_generic_to_op(pcp, val, &=)
254 #define raw_cpu_or_1(pcp, val) raw_cpu_generic_to_op(pcp, val, |=)
257 #define raw_cpu_or_2(pcp, val) raw_cpu_generic_to_op(pcp, val, |=)
260 #define raw_cpu_or_4(pcp, val) raw_cpu_generic_to_op(pcp, val, |=)
263 #define raw_cpu_or_8(pcp, val) raw_cpu_generic_to_op(pcp, val, |=)
266 #ifndef raw_cpu_add_return_1
267 #define raw_cpu_add_return_1(pcp, val) raw_cpu_generic_add_return(pcp, val)
269 #ifndef raw_cpu_add_return_2
270 #define raw_cpu_add_return_2(pcp, val) raw_cpu_generic_add_return(pcp, val)
272 #ifndef raw_cpu_add_return_4
273 #define raw_cpu_add_return_4(pcp, val) raw_cpu_generic_add_return(pcp, val)
275 #ifndef raw_cpu_add_return_8
276 #define raw_cpu_add_return_8(pcp, val) raw_cpu_generic_add_return(pcp, val)
279 #ifndef raw_cpu_xchg_1
280 #define raw_cpu_xchg_1(pcp, nval) raw_cpu_generic_xchg(pcp, nval)
282 #ifndef raw_cpu_xchg_2
283 #define raw_cpu_xchg_2(pcp, nval) raw_cpu_generic_xchg(pcp, nval)
285 #ifndef raw_cpu_xchg_4
286 #define raw_cpu_xchg_4(pcp, nval) raw_cpu_generic_xchg(pcp, nval)
288 #ifndef raw_cpu_xchg_8
289 #define raw_cpu_xchg_8(pcp, nval) raw_cpu_generic_xchg(pcp, nval)
292 #ifndef raw_cpu_try_cmpxchg_1
293 #ifdef raw_cpu_cmpxchg_1
294 #define raw_cpu_try_cmpxchg_1(pcp, ovalp, nval) \
295 __cpu_fallback_try_cmpxchg(pcp, ovalp, nval, raw_cpu_cmpxchg_1)
297 #define raw_cpu_try_cmpxchg_1(pcp, ovalp, nval) \
298 raw_cpu_generic_try_cmpxchg(pcp, ovalp, nval)
301 #ifndef raw_cpu_try_cmpxchg_2
302 #ifdef raw_cpu_cmpxchg_2
303 #define raw_cpu_try_cmpxchg_2(pcp, ovalp, nval) \
304 __cpu_fallback_try_cmpxchg(pcp, ovalp, nval, raw_cpu_cmpxchg_2)
306 #define raw_cpu_try_cmpxchg_2(pcp, ovalp, nval) \
307 raw_cpu_generic_try_cmpxchg(pcp, ovalp, nval)
310 #ifndef raw_cpu_try_cmpxchg_4
311 #ifdef raw_cpu_cmpxchg_4
312 #define raw_cpu_try_cmpxchg_4(pcp, ovalp, nval) \
313 __cpu_fallback_try_cmpxchg(pcp, ovalp, nval, raw_cpu_cmpxchg_4)
315 #define raw_cpu_try_cmpxchg_4(pcp, ovalp, nval) \
316 raw_cpu_generic_try_cmpxchg(pcp, ovalp, nval)
319 #ifndef raw_cpu_try_cmpxchg_8
320 #ifdef raw_cpu_cmpxchg_8
321 #define raw_cpu_try_cmpxchg_8(pcp, ovalp, nval) \
322 __cpu_fallback_try_cmpxchg(pcp, ovalp, nval, raw_cpu_cmpxchg_8)
324 #define raw_cpu_try_cmpxchg_8(pcp, ovalp, nval) \
325 raw_cpu_generic_try_cmpxchg(pcp, ovalp, nval)
329 #ifndef raw_cpu_try_cmpxchg64
330 #ifdef raw_cpu_cmpxchg64
331 #define raw_cpu_try_cmpxchg64(pcp, ovalp, nval) \
332 __cpu_fallback_try_cmpxchg(pcp, ovalp, nval, raw_cpu_cmpxchg64)
334 #define raw_cpu_try_cmpxchg64(pcp, ovalp, nval) \
335 raw_cpu_generic_try_cmpxchg(pcp, ovalp, nval)
338 #ifndef raw_cpu_try_cmpxchg128
339 #ifdef raw_cpu_cmpxchg128
340 #define raw_cpu_try_cmpxchg128(pcp, ovalp, nval) \
341 __cpu_fallback_try_cmpxchg(pcp, ovalp, nval, raw_cpu_cmpxchg128)
343 #define raw_cpu_try_cmpxchg128(pcp, ovalp, nval) \
344 raw_cpu_generic_try_cmpxchg(pcp, ovalp, nval)
348 #ifndef raw_cpu_cmpxchg_1
349 #define raw_cpu_cmpxchg_1(pcp, oval, nval) \
350 raw_cpu_generic_cmpxchg(pcp, oval, nval)
352 #ifndef raw_cpu_cmpxchg_2
353 #define raw_cpu_cmpxchg_2(pcp, oval, nval) \
354 raw_cpu_generic_cmpxchg(pcp, oval, nval)
356 #ifndef raw_cpu_cmpxchg_4
357 #define raw_cpu_cmpxchg_4(pcp, oval, nval) \
358 raw_cpu_generic_cmpxchg(pcp, oval, nval)
360 #ifndef raw_cpu_cmpxchg_8
361 #define raw_cpu_cmpxchg_8(pcp, oval, nval) \
362 raw_cpu_generic_cmpxchg(pcp, oval, nval)
365 #ifndef raw_cpu_cmpxchg64
366 #define raw_cpu_cmpxchg64(pcp, oval, nval) \
367 raw_cpu_generic_cmpxchg(pcp, oval, nval)
369 #ifndef raw_cpu_cmpxchg128
370 #define raw_cpu_cmpxchg128(pcp, oval, nval) \
371 raw_cpu_generic_cmpxchg(pcp, oval, nval)
374 #ifndef this_cpu_read_1
375 #define this_cpu_read_1(pcp) this_cpu_generic_read(pcp)
377 #ifndef this_cpu_read_2
378 #define this_cpu_read_2(pcp) this_cpu_generic_read(pcp)
380 #ifndef this_cpu_read_4
381 #define this_cpu_read_4(pcp) this_cpu_generic_read(pcp)
383 #ifndef this_cpu_read_8
384 #define this_cpu_read_8(pcp) this_cpu_generic_read(pcp)
387 #ifndef this_cpu_write_1
388 #define this_cpu_write_1(pcp, val) this_cpu_generic_to_op(pcp, val, =)
390 #ifndef this_cpu_write_2
391 #define this_cpu_write_2(pcp, val) this_cpu_generic_to_op(pcp, val, =)
393 #ifndef this_cpu_write_4
394 #define this_cpu_write_4(pcp, val) this_cpu_generic_to_op(pcp, val, =)
396 #ifndef this_cpu_write_8
397 #define this_cpu_write_8(pcp, val) this_cpu_generic_to_op(pcp, val, =)
400 #ifndef this_cpu_add_1
401 #define this_cpu_add_1(pcp, val) this_cpu_generic_to_op(pcp, val, +=)
403 #ifndef this_cpu_add_2
404 #define this_cpu_add_2(pcp, val) this_cpu_generic_to_op(pcp, val, +=)
406 #ifndef this_cpu_add_4
407 #define this_cpu_add_4(pcp, val) this_cpu_generic_to_op(pcp, val, +=)
409 #ifndef this_cpu_add_8
410 #define this_cpu_add_8(pcp, val) this_cpu_generic_to_op(pcp, val, +=)
413 #ifndef this_cpu_and_1
414 #define this_cpu_and_1(pcp, val) this_cpu_generic_to_op(pcp, val, &=)
416 #ifndef this_cpu_and_2
417 #define this_cpu_and_2(pcp, val) this_cpu_generic_to_op(pcp, val, &=)
419 #ifndef this_cpu_and_4
420 #define this_cpu_and_4(pcp, val) this_cpu_generic_to_op(pcp, val, &=)
422 #ifndef this_cpu_and_8
423 #define this_cpu_and_8(pcp, val) this_cpu_generic_to_op(pcp, val, &=)
426 #ifndef this_cpu_or_1
427 #define this_cpu_or_1(pcp, val) this_cpu_generic_to_op(pcp, val, |=)
429 #ifndef this_cpu_or_2
430 #define this_cpu_or_2(pcp, val) this_cpu_generic_to_op(pcp, val, |=)
432 #ifndef this_cpu_or_4
433 #define this_cpu_or_4(pcp, val) this_cpu_generic_to_op(pcp, val, |=)
435 #ifndef this_cpu_or_8
436 #define this_cpu_or_8(pcp, val) this_cpu_generic_to_op(pcp, val, |=)
439 #ifndef this_cpu_add_return_1
440 #define this_cpu_add_return_1(pcp, val) this_cpu_generic_add_return(pcp, val)
442 #ifndef this_cpu_add_return_2
443 #define this_cpu_add_return_2(pcp, val) this_cpu_generic_add_return(pcp, val)
445 #ifndef this_cpu_add_return_4
446 #define this_cpu_add_return_4(pcp, val) this_cpu_generic_add_return(pcp, val)
448 #ifndef this_cpu_add_return_8
449 #define this_cpu_add_return_8(pcp, val) this_cpu_generic_add_return(pcp, val)
452 #ifndef this_cpu_xchg_1
453 #define this_cpu_xchg_1(pcp, nval) this_cpu_generic_xchg(pcp, nval)
455 #ifndef this_cpu_xchg_2
456 #define this_cpu_xchg_2(pcp, nval) this_cpu_generic_xchg(pcp, nval)
458 #ifndef this_cpu_xchg_4
459 #define this_cpu_xchg_4(pcp, nval) this_cpu_generic_xchg(pcp, nval)
461 #ifndef this_cpu_xchg_8
462 #define this_cpu_xchg_8(pcp, nval) this_cpu_generic_xchg(pcp, nval)
465 #ifndef this_cpu_try_cmpxchg_1
466 #ifdef this_cpu_cmpxchg_1
467 #define this_cpu_try_cmpxchg_1(pcp, ovalp, nval) \
468 __cpu_fallback_try_cmpxchg(pcp, ovalp, nval, this_cpu_cmpxchg_1)
470 #define this_cpu_try_cmpxchg_1(pcp, ovalp, nval) \
471 this_cpu_generic_try_cmpxchg(pcp, ovalp, nval)
474 #ifndef this_cpu_try_cmpxchg_2
475 #ifdef this_cpu_cmpxchg_2
476 #define this_cpu_try_cmpxchg_2(pcp, ovalp, nval) \
477 __cpu_fallback_try_cmpxchg(pcp, ovalp, nval, this_cpu_cmpxchg_2)
479 #define this_cpu_try_cmpxchg_2(pcp, ovalp, nval) \
480 this_cpu_generic_try_cmpxchg(pcp, ovalp, nval)
483 #ifndef this_cpu_try_cmpxchg_4
484 #ifdef this_cpu_cmpxchg_4
485 #define this_cpu_try_cmpxchg_4(pcp, ovalp, nval) \
486 __cpu_fallback_try_cmpxchg(pcp, ovalp, nval, this_cpu_cmpxchg_4)
488 #define this_cpu_try_cmpxchg_4(pcp, ovalp, nval) \
489 this_cpu_generic_try_cmpxchg(pcp, ovalp, nval)
492 #ifndef this_cpu_try_cmpxchg_8
493 #ifdef this_cpu_cmpxchg_8
494 #define this_cpu_try_cmpxchg_8(pcp, ovalp, nval) \
495 __cpu_fallback_try_cmpxchg(pcp, ovalp, nval, this_cpu_cmpxchg_8)
497 #define this_cpu_try_cmpxchg_8(pcp, ovalp, nval) \
498 this_cpu_generic_try_cmpxchg(pcp, ovalp, nval)
502 #ifndef this_cpu_try_cmpxchg64
503 #ifdef this_cpu_cmpxchg64
504 #define this_cpu_try_cmpxchg64(pcp, ovalp, nval) \
505 __cpu_fallback_try_cmpxchg(pcp, ovalp, nval, this_cpu_cmpxchg64)
507 #define this_cpu_try_cmpxchg64(pcp, ovalp, nval) \
508 this_cpu_generic_try_cmpxchg(pcp, ovalp, nval)
511 #ifndef this_cpu_try_cmpxchg128
512 #ifdef this_cpu_cmpxchg128
513 #define this_cpu_try_cmpxchg128(pcp, ovalp, nval) \
514 __cpu_fallback_try_cmpxchg(pcp, ovalp, nval, this_cpu_cmpxchg128)
516 #define this_cpu_try_cmpxchg128(pcp, ovalp, nval) \
517 this_cpu_generic_try_cmpxchg(pcp, ovalp, nval)
521 #ifndef this_cpu_cmpxchg_1
522 #define this_cpu_cmpxchg_1(pcp, oval, nval) \
523 this_cpu_generic_cmpxchg(pcp, oval, nval)
525 #ifndef this_cpu_cmpxchg_2
526 #define this_cpu_cmpxchg_2(pcp, oval, nval) \
527 this_cpu_generic_cmpxchg(pcp, oval, nval)
529 #ifndef this_cpu_cmpxchg_4
530 #define this_cpu_cmpxchg_4(pcp, oval, nval) \
531 this_cpu_generic_cmpxchg(pcp, oval, nval)
533 #ifndef this_cpu_cmpxchg_8
534 #define this_cpu_cmpxchg_8(pcp, oval, nval) \
535 this_cpu_generic_cmpxchg(pcp, oval, nval)
538 #ifndef this_cpu_cmpxchg64
539 #define this_cpu_cmpxchg64(pcp, oval, nval) \
540 this_cpu_generic_cmpxchg(pcp, oval, nval)
542 #ifndef this_cpu_cmpxchg128
543 #define this_cpu_cmpxchg128(pcp, oval, nval) \
544 this_cpu_generic_cmpxchg(pcp, oval, nval)
547 #endif /* _ASM_GENERIC_PERCPU_H_ */