2 * Copyright (C) 2013 ARM Ltd.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #ifndef __ASM_PERCPU_H
17 #define __ASM_PERCPU_H
21 static inline void set_my_cpu_offset(unsigned long off
)
23 asm volatile("msr tpidr_el1, %0" :: "r" (off
) : "memory");
26 static inline unsigned long __my_cpu_offset(void)
31 * We want to allow caching the value, so avoid using volatile and
32 * instead use a fake stack read to hazard against barrier().
34 asm("mrs %0, tpidr_el1" : "=r" (off
) :
35 "Q" (*(const unsigned long *)current_stack_pointer
));
39 #define __my_cpu_offset __my_cpu_offset()
41 #else /* !CONFIG_SMP */
43 #define set_my_cpu_offset(x) do { } while (0)
45 #endif /* CONFIG_SMP */
47 #define PERCPU_OP(op, asm_op) \
48 static inline unsigned long __percpu_##op(void *ptr, \
49 unsigned long val, int size) \
51 unsigned long loop, ret; \
56 asm ("//__per_cpu_" #op "_1\n" \
57 "ldxrb %w[ret], %[ptr]\n" \
58 #asm_op " %w[ret], %w[ret], %w[val]\n" \
59 "stxrb %w[loop], %w[ret], %[ptr]\n" \
60 : [loop] "=&r" (loop), [ret] "=&r" (ret), \
61 [ptr] "+Q"(*(u8 *)ptr) \
62 : [val] "Ir" (val)); \
67 asm ("//__per_cpu_" #op "_2\n" \
68 "ldxrh %w[ret], %[ptr]\n" \
69 #asm_op " %w[ret], %w[ret], %w[val]\n" \
70 "stxrh %w[loop], %w[ret], %[ptr]\n" \
71 : [loop] "=&r" (loop), [ret] "=&r" (ret), \
72 [ptr] "+Q"(*(u16 *)ptr) \
73 : [val] "Ir" (val)); \
78 asm ("//__per_cpu_" #op "_4\n" \
79 "ldxr %w[ret], %[ptr]\n" \
80 #asm_op " %w[ret], %w[ret], %w[val]\n" \
81 "stxr %w[loop], %w[ret], %[ptr]\n" \
82 : [loop] "=&r" (loop), [ret] "=&r" (ret), \
83 [ptr] "+Q"(*(u32 *)ptr) \
84 : [val] "Ir" (val)); \
89 asm ("//__per_cpu_" #op "_8\n" \
90 "ldxr %[ret], %[ptr]\n" \
91 #asm_op " %[ret], %[ret], %[val]\n" \
92 "stxr %w[loop], %[ret], %[ptr]\n" \
93 : [loop] "=&r" (loop), [ret] "=&r" (ret), \
94 [ptr] "+Q"(*(u64 *)ptr) \
95 : [val] "Ir" (val)); \
110 static inline unsigned long __percpu_read(void *ptr
, int size
)
116 ret
= ACCESS_ONCE(*(u8
*)ptr
);
119 ret
= ACCESS_ONCE(*(u16
*)ptr
);
122 ret
= ACCESS_ONCE(*(u32
*)ptr
);
125 ret
= ACCESS_ONCE(*(u64
*)ptr
);
134 static inline void __percpu_write(void *ptr
, unsigned long val
, int size
)
138 ACCESS_ONCE(*(u8
*)ptr
) = (u8
)val
;
141 ACCESS_ONCE(*(u16
*)ptr
) = (u16
)val
;
144 ACCESS_ONCE(*(u32
*)ptr
) = (u32
)val
;
147 ACCESS_ONCE(*(u64
*)ptr
) = (u64
)val
;
154 static inline unsigned long __percpu_xchg(void *ptr
, unsigned long val
,
157 unsigned long ret
, loop
;
162 asm ("//__percpu_xchg_1\n"
163 "ldxrb %w[ret], %[ptr]\n"
164 "stxrb %w[loop], %w[val], %[ptr]\n"
165 : [loop
] "=&r"(loop
), [ret
] "=&r"(ret
),
166 [ptr
] "+Q"(*(u8
*)ptr
)
172 asm ("//__percpu_xchg_2\n"
173 "ldxrh %w[ret], %[ptr]\n"
174 "stxrh %w[loop], %w[val], %[ptr]\n"
175 : [loop
] "=&r"(loop
), [ret
] "=&r"(ret
),
176 [ptr
] "+Q"(*(u16
*)ptr
)
182 asm ("//__percpu_xchg_4\n"
183 "ldxr %w[ret], %[ptr]\n"
184 "stxr %w[loop], %w[val], %[ptr]\n"
185 : [loop
] "=&r"(loop
), [ret
] "=&r"(ret
),
186 [ptr
] "+Q"(*(u32
*)ptr
)
192 asm ("//__percpu_xchg_8\n"
193 "ldxr %[ret], %[ptr]\n"
194 "stxr %w[loop], %[val], %[ptr]\n"
195 : [loop
] "=&r"(loop
), [ret
] "=&r"(ret
),
196 [ptr
] "+Q"(*(u64
*)ptr
)
207 #define _percpu_read(pcp) \
209 typeof(pcp) __retval; \
211 __retval = (typeof(pcp))__percpu_read(raw_cpu_ptr(&(pcp)), \
217 #define _percpu_write(pcp, val) \
220 __percpu_write(raw_cpu_ptr(&(pcp)), (unsigned long)(val), \
225 #define _pcp_protect(operation, pcp, val) \
227 typeof(pcp) __retval; \
229 __retval = (typeof(pcp))operation(raw_cpu_ptr(&(pcp)), \
230 (val), sizeof(pcp)); \
235 #define _percpu_add(pcp, val) \
236 _pcp_protect(__percpu_add, pcp, val)
238 #define _percpu_add_return(pcp, val) _percpu_add(pcp, val)
240 #define _percpu_and(pcp, val) \
241 _pcp_protect(__percpu_and, pcp, val)
243 #define _percpu_or(pcp, val) \
244 _pcp_protect(__percpu_or, pcp, val)
246 #define _percpu_xchg(pcp, val) (typeof(pcp)) \
247 _pcp_protect(__percpu_xchg, pcp, (unsigned long)(val))
249 #define this_cpu_add_1(pcp, val) _percpu_add(pcp, val)
250 #define this_cpu_add_2(pcp, val) _percpu_add(pcp, val)
251 #define this_cpu_add_4(pcp, val) _percpu_add(pcp, val)
252 #define this_cpu_add_8(pcp, val) _percpu_add(pcp, val)
254 #define this_cpu_add_return_1(pcp, val) _percpu_add_return(pcp, val)
255 #define this_cpu_add_return_2(pcp, val) _percpu_add_return(pcp, val)
256 #define this_cpu_add_return_4(pcp, val) _percpu_add_return(pcp, val)
257 #define this_cpu_add_return_8(pcp, val) _percpu_add_return(pcp, val)
259 #define this_cpu_and_1(pcp, val) _percpu_and(pcp, val)
260 #define this_cpu_and_2(pcp, val) _percpu_and(pcp, val)
261 #define this_cpu_and_4(pcp, val) _percpu_and(pcp, val)
262 #define this_cpu_and_8(pcp, val) _percpu_and(pcp, val)
264 #define this_cpu_or_1(pcp, val) _percpu_or(pcp, val)
265 #define this_cpu_or_2(pcp, val) _percpu_or(pcp, val)
266 #define this_cpu_or_4(pcp, val) _percpu_or(pcp, val)
267 #define this_cpu_or_8(pcp, val) _percpu_or(pcp, val)
269 #define this_cpu_read_1(pcp) _percpu_read(pcp)
270 #define this_cpu_read_2(pcp) _percpu_read(pcp)
271 #define this_cpu_read_4(pcp) _percpu_read(pcp)
272 #define this_cpu_read_8(pcp) _percpu_read(pcp)
274 #define this_cpu_write_1(pcp, val) _percpu_write(pcp, val)
275 #define this_cpu_write_2(pcp, val) _percpu_write(pcp, val)
276 #define this_cpu_write_4(pcp, val) _percpu_write(pcp, val)
277 #define this_cpu_write_8(pcp, val) _percpu_write(pcp, val)
279 #define this_cpu_xchg_1(pcp, val) _percpu_xchg(pcp, val)
280 #define this_cpu_xchg_2(pcp, val) _percpu_xchg(pcp, val)
281 #define this_cpu_xchg_4(pcp, val) _percpu_xchg(pcp, val)
282 #define this_cpu_xchg_8(pcp, val) _percpu_xchg(pcp, val)
284 #include <asm-generic/percpu.h>
286 #endif /* __ASM_PERCPU_H */