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 #ifndef PER_CPU_DEF_ATTRIBUTES
66 #define PER_CPU_DEF_ATTRIBUTES
69 #define raw_cpu_generic_read(pcp) \
71 *raw_cpu_ptr(&(pcp)); \
74 #define raw_cpu_generic_to_op(pcp, val, op) \
76 *raw_cpu_ptr(&(pcp)) op val; \
79 #define raw_cpu_generic_add_return(pcp, val) \
81 typeof(&(pcp)) __p = raw_cpu_ptr(&(pcp)); \
87 #define raw_cpu_generic_xchg(pcp, nval) \
89 typeof(&(pcp)) __p = raw_cpu_ptr(&(pcp)); \
96 #define raw_cpu_generic_cmpxchg(pcp, oval, nval) \
98 typeof(&(pcp)) __p = raw_cpu_ptr(&(pcp)); \
101 if (__ret == (oval)) \
106 #define raw_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2) \
108 typeof(&(pcp1)) __p1 = raw_cpu_ptr(&(pcp1)); \
109 typeof(&(pcp2)) __p2 = raw_cpu_ptr(&(pcp2)); \
111 if (*__p1 == (oval1) && *__p2 == (oval2)) { \
119 #define __this_cpu_generic_read_nopreempt(pcp) \
122 preempt_disable_notrace(); \
123 __ret = READ_ONCE(*raw_cpu_ptr(&(pcp))); \
124 preempt_enable_notrace(); \
128 #define __this_cpu_generic_read_noirq(pcp) \
131 unsigned long __flags; \
132 raw_local_irq_save(__flags); \
133 __ret = raw_cpu_generic_read(pcp); \
134 raw_local_irq_restore(__flags); \
138 #define this_cpu_generic_read(pcp) \
141 if (__native_word(pcp)) \
142 __ret = __this_cpu_generic_read_nopreempt(pcp); \
144 __ret = __this_cpu_generic_read_noirq(pcp); \
148 #define this_cpu_generic_to_op(pcp, val, op) \
150 unsigned long __flags; \
151 raw_local_irq_save(__flags); \
152 raw_cpu_generic_to_op(pcp, val, op); \
153 raw_local_irq_restore(__flags); \
157 #define this_cpu_generic_add_return(pcp, val) \
160 unsigned long __flags; \
161 raw_local_irq_save(__flags); \
162 __ret = raw_cpu_generic_add_return(pcp, val); \
163 raw_local_irq_restore(__flags); \
167 #define this_cpu_generic_xchg(pcp, nval) \
170 unsigned long __flags; \
171 raw_local_irq_save(__flags); \
172 __ret = raw_cpu_generic_xchg(pcp, nval); \
173 raw_local_irq_restore(__flags); \
177 #define this_cpu_generic_cmpxchg(pcp, oval, nval) \
180 unsigned long __flags; \
181 raw_local_irq_save(__flags); \
182 __ret = raw_cpu_generic_cmpxchg(pcp, oval, nval); \
183 raw_local_irq_restore(__flags); \
187 #define this_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2) \
190 unsigned long __flags; \
191 raw_local_irq_save(__flags); \
192 __ret = raw_cpu_generic_cmpxchg_double(pcp1, pcp2, \
193 oval1, oval2, nval1, nval2); \
194 raw_local_irq_restore(__flags); \
198 #ifndef raw_cpu_read_1
199 #define raw_cpu_read_1(pcp) raw_cpu_generic_read(pcp)
201 #ifndef raw_cpu_read_2
202 #define raw_cpu_read_2(pcp) raw_cpu_generic_read(pcp)
204 #ifndef raw_cpu_read_4
205 #define raw_cpu_read_4(pcp) raw_cpu_generic_read(pcp)
207 #ifndef raw_cpu_read_8
208 #define raw_cpu_read_8(pcp) raw_cpu_generic_read(pcp)
211 #ifndef raw_cpu_write_1
212 #define raw_cpu_write_1(pcp, val) raw_cpu_generic_to_op(pcp, val, =)
214 #ifndef raw_cpu_write_2
215 #define raw_cpu_write_2(pcp, val) raw_cpu_generic_to_op(pcp, val, =)
217 #ifndef raw_cpu_write_4
218 #define raw_cpu_write_4(pcp, val) raw_cpu_generic_to_op(pcp, val, =)
220 #ifndef raw_cpu_write_8
221 #define raw_cpu_write_8(pcp, val) raw_cpu_generic_to_op(pcp, val, =)
224 #ifndef raw_cpu_add_1
225 #define raw_cpu_add_1(pcp, val) raw_cpu_generic_to_op(pcp, val, +=)
227 #ifndef raw_cpu_add_2
228 #define raw_cpu_add_2(pcp, val) raw_cpu_generic_to_op(pcp, val, +=)
230 #ifndef raw_cpu_add_4
231 #define raw_cpu_add_4(pcp, val) raw_cpu_generic_to_op(pcp, val, +=)
233 #ifndef raw_cpu_add_8
234 #define raw_cpu_add_8(pcp, val) raw_cpu_generic_to_op(pcp, val, +=)
237 #ifndef raw_cpu_and_1
238 #define raw_cpu_and_1(pcp, val) raw_cpu_generic_to_op(pcp, val, &=)
240 #ifndef raw_cpu_and_2
241 #define raw_cpu_and_2(pcp, val) raw_cpu_generic_to_op(pcp, val, &=)
243 #ifndef raw_cpu_and_4
244 #define raw_cpu_and_4(pcp, val) raw_cpu_generic_to_op(pcp, val, &=)
246 #ifndef raw_cpu_and_8
247 #define raw_cpu_and_8(pcp, val) raw_cpu_generic_to_op(pcp, val, &=)
251 #define raw_cpu_or_1(pcp, val) raw_cpu_generic_to_op(pcp, val, |=)
254 #define raw_cpu_or_2(pcp, val) raw_cpu_generic_to_op(pcp, val, |=)
257 #define raw_cpu_or_4(pcp, val) raw_cpu_generic_to_op(pcp, val, |=)
260 #define raw_cpu_or_8(pcp, val) raw_cpu_generic_to_op(pcp, val, |=)
263 #ifndef raw_cpu_add_return_1
264 #define raw_cpu_add_return_1(pcp, val) raw_cpu_generic_add_return(pcp, val)
266 #ifndef raw_cpu_add_return_2
267 #define raw_cpu_add_return_2(pcp, val) raw_cpu_generic_add_return(pcp, val)
269 #ifndef raw_cpu_add_return_4
270 #define raw_cpu_add_return_4(pcp, val) raw_cpu_generic_add_return(pcp, val)
272 #ifndef raw_cpu_add_return_8
273 #define raw_cpu_add_return_8(pcp, val) raw_cpu_generic_add_return(pcp, val)
276 #ifndef raw_cpu_xchg_1
277 #define raw_cpu_xchg_1(pcp, nval) raw_cpu_generic_xchg(pcp, nval)
279 #ifndef raw_cpu_xchg_2
280 #define raw_cpu_xchg_2(pcp, nval) raw_cpu_generic_xchg(pcp, nval)
282 #ifndef raw_cpu_xchg_4
283 #define raw_cpu_xchg_4(pcp, nval) raw_cpu_generic_xchg(pcp, nval)
285 #ifndef raw_cpu_xchg_8
286 #define raw_cpu_xchg_8(pcp, nval) raw_cpu_generic_xchg(pcp, nval)
289 #ifndef raw_cpu_cmpxchg_1
290 #define raw_cpu_cmpxchg_1(pcp, oval, nval) \
291 raw_cpu_generic_cmpxchg(pcp, oval, nval)
293 #ifndef raw_cpu_cmpxchg_2
294 #define raw_cpu_cmpxchg_2(pcp, oval, nval) \
295 raw_cpu_generic_cmpxchg(pcp, oval, nval)
297 #ifndef raw_cpu_cmpxchg_4
298 #define raw_cpu_cmpxchg_4(pcp, oval, nval) \
299 raw_cpu_generic_cmpxchg(pcp, oval, nval)
301 #ifndef raw_cpu_cmpxchg_8
302 #define raw_cpu_cmpxchg_8(pcp, oval, nval) \
303 raw_cpu_generic_cmpxchg(pcp, oval, nval)
306 #ifndef raw_cpu_cmpxchg_double_1
307 #define raw_cpu_cmpxchg_double_1(pcp1, pcp2, oval1, oval2, nval1, nval2) \
308 raw_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
310 #ifndef raw_cpu_cmpxchg_double_2
311 #define raw_cpu_cmpxchg_double_2(pcp1, pcp2, oval1, oval2, nval1, nval2) \
312 raw_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
314 #ifndef raw_cpu_cmpxchg_double_4
315 #define raw_cpu_cmpxchg_double_4(pcp1, pcp2, oval1, oval2, nval1, nval2) \
316 raw_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
318 #ifndef raw_cpu_cmpxchg_double_8
319 #define raw_cpu_cmpxchg_double_8(pcp1, pcp2, oval1, oval2, nval1, nval2) \
320 raw_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
323 #ifndef this_cpu_read_1
324 #define this_cpu_read_1(pcp) this_cpu_generic_read(pcp)
326 #ifndef this_cpu_read_2
327 #define this_cpu_read_2(pcp) this_cpu_generic_read(pcp)
329 #ifndef this_cpu_read_4
330 #define this_cpu_read_4(pcp) this_cpu_generic_read(pcp)
332 #ifndef this_cpu_read_8
333 #define this_cpu_read_8(pcp) this_cpu_generic_read(pcp)
336 #ifndef this_cpu_write_1
337 #define this_cpu_write_1(pcp, val) this_cpu_generic_to_op(pcp, val, =)
339 #ifndef this_cpu_write_2
340 #define this_cpu_write_2(pcp, val) this_cpu_generic_to_op(pcp, val, =)
342 #ifndef this_cpu_write_4
343 #define this_cpu_write_4(pcp, val) this_cpu_generic_to_op(pcp, val, =)
345 #ifndef this_cpu_write_8
346 #define this_cpu_write_8(pcp, val) this_cpu_generic_to_op(pcp, val, =)
349 #ifndef this_cpu_add_1
350 #define this_cpu_add_1(pcp, val) this_cpu_generic_to_op(pcp, val, +=)
352 #ifndef this_cpu_add_2
353 #define this_cpu_add_2(pcp, val) this_cpu_generic_to_op(pcp, val, +=)
355 #ifndef this_cpu_add_4
356 #define this_cpu_add_4(pcp, val) this_cpu_generic_to_op(pcp, val, +=)
358 #ifndef this_cpu_add_8
359 #define this_cpu_add_8(pcp, val) this_cpu_generic_to_op(pcp, val, +=)
362 #ifndef this_cpu_and_1
363 #define this_cpu_and_1(pcp, val) this_cpu_generic_to_op(pcp, val, &=)
365 #ifndef this_cpu_and_2
366 #define this_cpu_and_2(pcp, val) this_cpu_generic_to_op(pcp, val, &=)
368 #ifndef this_cpu_and_4
369 #define this_cpu_and_4(pcp, val) this_cpu_generic_to_op(pcp, val, &=)
371 #ifndef this_cpu_and_8
372 #define this_cpu_and_8(pcp, val) this_cpu_generic_to_op(pcp, val, &=)
375 #ifndef this_cpu_or_1
376 #define this_cpu_or_1(pcp, val) this_cpu_generic_to_op(pcp, val, |=)
378 #ifndef this_cpu_or_2
379 #define this_cpu_or_2(pcp, val) this_cpu_generic_to_op(pcp, val, |=)
381 #ifndef this_cpu_or_4
382 #define this_cpu_or_4(pcp, val) this_cpu_generic_to_op(pcp, val, |=)
384 #ifndef this_cpu_or_8
385 #define this_cpu_or_8(pcp, val) this_cpu_generic_to_op(pcp, val, |=)
388 #ifndef this_cpu_add_return_1
389 #define this_cpu_add_return_1(pcp, val) this_cpu_generic_add_return(pcp, val)
391 #ifndef this_cpu_add_return_2
392 #define this_cpu_add_return_2(pcp, val) this_cpu_generic_add_return(pcp, val)
394 #ifndef this_cpu_add_return_4
395 #define this_cpu_add_return_4(pcp, val) this_cpu_generic_add_return(pcp, val)
397 #ifndef this_cpu_add_return_8
398 #define this_cpu_add_return_8(pcp, val) this_cpu_generic_add_return(pcp, val)
401 #ifndef this_cpu_xchg_1
402 #define this_cpu_xchg_1(pcp, nval) this_cpu_generic_xchg(pcp, nval)
404 #ifndef this_cpu_xchg_2
405 #define this_cpu_xchg_2(pcp, nval) this_cpu_generic_xchg(pcp, nval)
407 #ifndef this_cpu_xchg_4
408 #define this_cpu_xchg_4(pcp, nval) this_cpu_generic_xchg(pcp, nval)
410 #ifndef this_cpu_xchg_8
411 #define this_cpu_xchg_8(pcp, nval) this_cpu_generic_xchg(pcp, nval)
414 #ifndef this_cpu_cmpxchg_1
415 #define this_cpu_cmpxchg_1(pcp, oval, nval) \
416 this_cpu_generic_cmpxchg(pcp, oval, nval)
418 #ifndef this_cpu_cmpxchg_2
419 #define this_cpu_cmpxchg_2(pcp, oval, nval) \
420 this_cpu_generic_cmpxchg(pcp, oval, nval)
422 #ifndef this_cpu_cmpxchg_4
423 #define this_cpu_cmpxchg_4(pcp, oval, nval) \
424 this_cpu_generic_cmpxchg(pcp, oval, nval)
426 #ifndef this_cpu_cmpxchg_8
427 #define this_cpu_cmpxchg_8(pcp, oval, nval) \
428 this_cpu_generic_cmpxchg(pcp, oval, nval)
431 #ifndef this_cpu_cmpxchg_double_1
432 #define this_cpu_cmpxchg_double_1(pcp1, pcp2, oval1, oval2, nval1, nval2) \
433 this_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
435 #ifndef this_cpu_cmpxchg_double_2
436 #define this_cpu_cmpxchg_double_2(pcp1, pcp2, oval1, oval2, nval1, nval2) \
437 this_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
439 #ifndef this_cpu_cmpxchg_double_4
440 #define this_cpu_cmpxchg_double_4(pcp1, pcp2, oval1, oval2, nval1, nval2) \
441 this_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
443 #ifndef this_cpu_cmpxchg_double_8
444 #define this_cpu_cmpxchg_double_8(pcp1, pcp2, oval1, oval2, nval1, nval2) \
445 this_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
448 #endif /* _ASM_GENERIC_PERCPU_H_ */