2 # SPDX-License-Identifier: GPL-2.0
4 ATOMICDIR=$(dirname $0)
6 . ${ATOMICDIR}/atomic-tbl.sh
8 #gen_template_fallback(template, meta, pfx, name, sfx, order, atomic, int, args...)
9 gen_template_fallback()
11 local template="$1"; shift
12 local meta="$1"; shift
14 local name="$1"; shift
16 local order="$1"; shift
17 local atomic="$1"; shift
20 local ret="$(gen_ret_type "${meta}" "${int}")"
21 local retstmt="$(gen_ret_stmt "${meta}")"
22 local params="$(gen_params "${int}" "${atomic}" "$@")"
23 local args="$(gen_args "$@")"
28 #gen_order_fallback(meta, pfx, name, sfx, order, atomic, int, args...)
31 local meta="$1"; shift
33 local name="$1"; shift
35 local order="$1"; shift
37 local tmpl_order=${order#_}
38 local tmpl="${ATOMICDIR}/fallbacks/${tmpl_order:-fence}"
39 gen_template_fallback "${tmpl}" "${meta}" "${pfx}" "${name}" "${sfx}" "${order}" "$@"
42 #gen_proto_fallback(meta, pfx, name, sfx, order, atomic, int, args...)
45 local meta="$1"; shift
47 local name="$1"; shift
49 local order="$1"; shift
51 local tmpl="$(find_fallback_template "${pfx}" "${name}" "${sfx}" "${order}")"
52 gen_template_fallback "${tmpl}" "${meta}" "${pfx}" "${name}" "${sfx}" "${order}" "$@"
55 #gen_proto_order_variant(meta, pfx, name, sfx, order, atomic, int, args...)
56 gen_proto_order_variant()
58 local meta="$1"; shift
60 local name="$1"; shift
62 local order="$1"; shift
63 local atomic="$1"; shift
66 local atomicname="${atomic}_${pfx}${name}${sfx}${order}"
67 local basename="${atomic}_${pfx}${name}${sfx}"
69 local template="$(find_fallback_template "${pfx}" "${name}" "${sfx}" "${order}")"
71 local ret="$(gen_ret_type "${meta}" "${int}")"
72 local retstmt="$(gen_ret_stmt "${meta}")"
73 local params="$(gen_params "${int}" "${atomic}" "$@")"
74 local args="$(gen_args "$@")"
76 gen_kerneldoc "raw_" "${meta}" "${pfx}" "${name}" "${sfx}" "${order}" "${atomic}" "${int}" "$@"
78 printf "static __always_inline ${ret}\n"
79 printf "raw_${atomicname}(${params})\n"
82 # Where there is no possible fallback, this order variant is mandatory
83 # and must be provided by arch code. Add a comment to the header to
86 # Ideally we'd error on a missing definition, but arch code might
87 # define this order variant as a C function without a preprocessor
89 if [ -z ${template} ] && [ -z "${order}" ] && ! meta_has_relaxed "${meta}"; then
90 printf "\t${retstmt}arch_${atomicname}(${args});\n"
95 printf "#if defined(arch_${atomicname})\n"
96 printf "\t${retstmt}arch_${atomicname}(${args});\n"
98 # Allow FULL/ACQUIRE/RELEASE ops to be defined in terms of RELAXED ops
99 if [ "${order}" != "_relaxed" ] && meta_has_relaxed "${meta}"; then
100 printf "#elif defined(arch_${basename}_relaxed)\n"
101 gen_order_fallback "${meta}" "${pfx}" "${name}" "${sfx}" "${order}" "${atomic}" "${int}" "$@"
104 # Allow ACQUIRE/RELEASE/RELAXED ops to be defined in terms of FULL ops
105 if [ ! -z "${order}" ] && ! meta_is_implicitly_relaxed "${meta}"; then
106 printf "#elif defined(arch_${basename})\n"
107 printf "\t${retstmt}arch_${basename}(${args});\n"
111 if [ ! -z "${template}" ]; then
112 gen_proto_fallback "${meta}" "${pfx}" "${name}" "${sfx}" "${order}" "${atomic}" "${int}" "$@"
114 printf "#error \"Unable to define raw_${atomicname}\"\n"
122 #gen_proto_order_variants(meta, pfx, name, sfx, atomic, int, args...)
123 gen_proto_order_variants()
125 local meta="$1"; shift
126 local pfx="$1"; shift
127 local name="$1"; shift
128 local sfx="$1"; shift
131 gen_proto_order_variant "${meta}" "${pfx}" "${name}" "${sfx}" "" "$@"
133 if meta_has_acquire "${meta}"; then
134 gen_proto_order_variant "${meta}" "${pfx}" "${name}" "${sfx}" "_acquire" "$@"
137 if meta_has_release "${meta}"; then
138 gen_proto_order_variant "${meta}" "${pfx}" "${name}" "${sfx}" "_release" "$@"
141 if meta_has_relaxed "${meta}"; then
142 gen_proto_order_variant "${meta}" "${pfx}" "${name}" "${sfx}" "_relaxed" "$@"
146 #gen_basic_fallbacks(basename)
147 gen_basic_fallbacks()
149 local basename="$1"; shift
151 #define raw_${basename}_acquire arch_${basename}
152 #define raw_${basename}_release arch_${basename}
153 #define raw_${basename}_relaxed arch_${basename}
157 gen_order_fallbacks()
159 local xchg="$1"; shift
163 #define raw_${xchg}_relaxed arch_${xchg}_relaxed
165 #ifdef arch_${xchg}_acquire
166 #define raw_${xchg}_acquire arch_${xchg}_acquire
168 #define raw_${xchg}_acquire(...) \\
169 __atomic_op_acquire(arch_${xchg}, __VA_ARGS__)
172 #ifdef arch_${xchg}_release
173 #define raw_${xchg}_release arch_${xchg}_release
175 #define raw_${xchg}_release(...) \\
176 __atomic_op_release(arch_${xchg}, __VA_ARGS__)
180 #define raw_${xchg} arch_${xchg}
182 #define raw_${xchg}(...) \\
183 __atomic_op_fence(arch_${xchg}, __VA_ARGS__)
189 gen_xchg_order_fallback()
191 local xchg="$1"; shift
192 local order="$1"; shift
193 local forder="${order:-_fence}"
195 printf "#if defined(arch_${xchg}${order})\n"
196 printf "#define raw_${xchg}${order} arch_${xchg}${order}\n"
198 if [ "${order}" != "_relaxed" ]; then
199 printf "#elif defined(arch_${xchg}_relaxed)\n"
200 printf "#define raw_${xchg}${order}(...) \\\\\n"
201 printf " __atomic_op${forder}(arch_${xchg}, __VA_ARGS__)\n"
204 if [ ! -z "${order}" ]; then
205 printf "#elif defined(arch_${xchg})\n"
206 printf "#define raw_${xchg}${order} arch_${xchg}\n"
210 printf "extern void raw_${xchg}${order}_not_implemented(void);\n"
211 printf "#define raw_${xchg}${order}(...) raw_${xchg}${order}_not_implemented()\n"
217 local xchg="$1"; shift
219 for order in "" "_acquire" "_release" "_relaxed"; do
220 gen_xchg_order_fallback "${xchg}" "${order}"
224 gen_try_cmpxchg_fallback()
226 local cmpxchg="$1"; shift;
227 local order="$1"; shift;
230 #define raw_try_${cmpxchg}${order}(_ptr, _oldp, _new) \\
232 typeof(*(_ptr)) *___op = (_oldp), ___o = *___op, ___r; \\
233 ___r = raw_${cmpxchg}${order}((_ptr), ___o, (_new)); \\
234 if (unlikely(___r != ___o)) \\
236 likely(___r == ___o); \\
241 gen_try_cmpxchg_order_fallback()
243 local cmpxchg="$1"; shift
244 local order="$1"; shift
245 local forder="${order:-_fence}"
247 printf "#if defined(arch_try_${cmpxchg}${order})\n"
248 printf "#define raw_try_${cmpxchg}${order} arch_try_${cmpxchg}${order}\n"
250 if [ "${order}" != "_relaxed" ]; then
251 printf "#elif defined(arch_try_${cmpxchg}_relaxed)\n"
252 printf "#define raw_try_${cmpxchg}${order}(...) \\\\\n"
253 printf " __atomic_op${forder}(arch_try_${cmpxchg}, __VA_ARGS__)\n"
256 if [ ! -z "${order}" ]; then
257 printf "#elif defined(arch_try_${cmpxchg})\n"
258 printf "#define raw_try_${cmpxchg}${order} arch_try_${cmpxchg}\n"
262 gen_try_cmpxchg_fallback "${cmpxchg}" "${order}"
266 gen_try_cmpxchg_fallbacks()
268 local cmpxchg="$1"; shift;
270 for order in "" "_acquire" "_release" "_relaxed"; do
271 gen_try_cmpxchg_order_fallback "${cmpxchg}" "${order}"
275 gen_cmpxchg_local_fallbacks()
277 local cmpxchg="$1"; shift
279 printf "#define raw_${cmpxchg} arch_${cmpxchg}\n\n"
280 printf "#ifdef arch_try_${cmpxchg}\n"
281 printf "#define raw_try_${cmpxchg} arch_try_${cmpxchg}\n"
283 gen_try_cmpxchg_fallback "${cmpxchg}" ""
288 // SPDX-License-Identifier: GPL-2.0
291 // DO NOT MODIFY THIS FILE DIRECTLY
293 #ifndef _LINUX_ATOMIC_FALLBACK_H
294 #define _LINUX_ATOMIC_FALLBACK_H
296 #include <linux/compiler.h>
300 for xchg in "xchg" "cmpxchg" "cmpxchg64" "cmpxchg128"; do
301 gen_xchg_fallbacks "${xchg}"
304 for cmpxchg in "cmpxchg" "cmpxchg64" "cmpxchg128"; do
305 gen_try_cmpxchg_fallbacks "${cmpxchg}"
308 for cmpxchg in "cmpxchg_local" "cmpxchg64_local" "cmpxchg128_local"; do
309 gen_cmpxchg_local_fallbacks "${cmpxchg}" ""
312 for cmpxchg in "sync_cmpxchg"; do
313 printf "#define raw_${cmpxchg} arch_${cmpxchg}\n\n"
316 grep '^[a-z]' "$1" | while read name meta args; do
317 gen_proto "${meta}" "${name}" "atomic" "int" ${args}
321 #ifdef CONFIG_GENERIC_ATOMIC64
322 #include <asm-generic/atomic64.h>
327 grep '^[a-z]' "$1" | while read name meta args; do
328 gen_proto "${meta}" "${name}" "atomic64" "s64" ${args}
332 #endif /* _LINUX_ATOMIC_FALLBACK_H */