1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
8 #include <asm/cmpxchg.h>
9 #include <asm/loongarch.h>
12 * The "address" (in fact, offset from $r21) of a per-CPU variable is close to
13 * the loading address of main kernel image, but far from where the modules are
14 * loaded. Tell the compiler this fact when using explicit relocs.
16 #if defined(MODULE) && defined(CONFIG_AS_HAS_EXPLICIT_RELOCS)
17 # if __has_attribute(model)
18 # define PER_CPU_ATTRIBUTES __attribute__((model("extreme")))
20 # error compiler support for the model attribute is necessary when a recent assembler is used
24 /* Use r21 for fast access */
25 register unsigned long __my_cpu_offset
__asm__("$r21");
27 static inline void set_my_cpu_offset(unsigned long off
)
29 __my_cpu_offset
= off
;
30 csr_write64(off
, PERCPU_BASE_KS
);
33 #define __my_cpu_offset \
35 __asm__ __volatile__("":"+r"(__my_cpu_offset)); \
39 #define PERCPU_OP(op, asm_op, c_op) \
40 static __always_inline unsigned long __percpu_##op(void *ptr, \
41 unsigned long val, int size) \
47 __asm__ __volatile__( \
48 "am"#asm_op".w" " %[ret], %[val], %[ptr] \n" \
49 : [ret] "=&r" (ret), [ptr] "+ZB"(*(u32 *)ptr) \
53 __asm__ __volatile__( \
54 "am"#asm_op".d" " %[ret], %[val], %[ptr] \n" \
55 : [ret] "=&r" (ret), [ptr] "+ZB"(*(u64 *)ptr) \
63 return ret c_op val; \
66 PERCPU_OP(add
, add
, +)
67 PERCPU_OP(and, and, &)
71 static __always_inline
unsigned long __percpu_xchg(void *ptr
, unsigned long val
, int size
)
76 return __xchg_small((volatile void *)ptr
, val
, size
);
79 return __xchg_asm("amswap.w", (volatile u32
*)ptr
, (u32
)val
);
82 return __xchg_asm("amswap.d", (volatile u64
*)ptr
, (u64
)val
);
91 #define __pcpu_op_1(op) op ".b "
92 #define __pcpu_op_2(op) op ".h "
93 #define __pcpu_op_4(op) op ".w "
94 #define __pcpu_op_8(op) op ".d "
96 #define _percpu_read(size, _pcp) \
98 typeof(_pcp) __pcp_ret; \
100 __asm__ __volatile__( \
101 __pcpu_op_##size("ldx") "%[ret], $r21, %[ptr] \n" \
102 : [ret] "=&r"(__pcp_ret) \
103 : [ptr] "r"(&(_pcp)) \
109 #define _percpu_write(size, _pcp, _val) \
111 __asm__ __volatile__( \
112 __pcpu_op_##size("stx") "%[val], $r21, %[ptr] \n" \
114 : [val] "r"(_val), [ptr] "r"(&(_pcp)) \
118 /* this_cpu_cmpxchg */
119 #define _protect_cmpxchg_local(pcp, o, n) \
121 typeof(*raw_cpu_ptr(&(pcp))) __ret; \
122 preempt_disable_notrace(); \
123 __ret = cmpxchg_local(raw_cpu_ptr(&(pcp)), o, n); \
124 preempt_enable_notrace(); \
128 #define _pcp_protect(operation, pcp, val) \
130 typeof(pcp) __retval; \
131 preempt_disable_notrace(); \
132 __retval = (typeof(pcp))operation(raw_cpu_ptr(&(pcp)), \
133 (val), sizeof(pcp)); \
134 preempt_enable_notrace(); \
138 #define _percpu_add(pcp, val) \
139 _pcp_protect(__percpu_add, pcp, val)
141 #define _percpu_add_return(pcp, val) _percpu_add(pcp, val)
143 #define _percpu_and(pcp, val) \
144 _pcp_protect(__percpu_and, pcp, val)
146 #define _percpu_or(pcp, val) \
147 _pcp_protect(__percpu_or, pcp, val)
149 #define _percpu_xchg(pcp, val) ((typeof(pcp)) \
150 _pcp_protect(__percpu_xchg, pcp, (unsigned long)(val)))
152 #define this_cpu_add_4(pcp, val) _percpu_add(pcp, val)
153 #define this_cpu_add_8(pcp, val) _percpu_add(pcp, val)
155 #define this_cpu_add_return_4(pcp, val) _percpu_add_return(pcp, val)
156 #define this_cpu_add_return_8(pcp, val) _percpu_add_return(pcp, val)
158 #define this_cpu_and_4(pcp, val) _percpu_and(pcp, val)
159 #define this_cpu_and_8(pcp, val) _percpu_and(pcp, val)
161 #define this_cpu_or_4(pcp, val) _percpu_or(pcp, val)
162 #define this_cpu_or_8(pcp, val) _percpu_or(pcp, val)
164 #define this_cpu_read_1(pcp) _percpu_read(1, pcp)
165 #define this_cpu_read_2(pcp) _percpu_read(2, pcp)
166 #define this_cpu_read_4(pcp) _percpu_read(4, pcp)
167 #define this_cpu_read_8(pcp) _percpu_read(8, pcp)
169 #define this_cpu_write_1(pcp, val) _percpu_write(1, pcp, val)
170 #define this_cpu_write_2(pcp, val) _percpu_write(2, pcp, val)
171 #define this_cpu_write_4(pcp, val) _percpu_write(4, pcp, val)
172 #define this_cpu_write_8(pcp, val) _percpu_write(8, pcp, val)
174 #define this_cpu_xchg_1(pcp, val) _percpu_xchg(pcp, val)
175 #define this_cpu_xchg_2(pcp, val) _percpu_xchg(pcp, val)
176 #define this_cpu_xchg_4(pcp, val) _percpu_xchg(pcp, val)
177 #define this_cpu_xchg_8(pcp, val) _percpu_xchg(pcp, val)
179 #define this_cpu_cmpxchg_1(ptr, o, n) _protect_cmpxchg_local(ptr, o, n)
180 #define this_cpu_cmpxchg_2(ptr, o, n) _protect_cmpxchg_local(ptr, o, n)
181 #define this_cpu_cmpxchg_4(ptr, o, n) _protect_cmpxchg_local(ptr, o, n)
182 #define this_cpu_cmpxchg_8(ptr, o, n) _protect_cmpxchg_local(ptr, o, n)
184 #include <asm-generic/percpu.h>
186 #endif /* __ASM_PERCPU_H */