1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3
4ATOMICDIR=$(dirname $0)
5ARCH=$2
6
7. ${ATOMICDIR}/atomic-tbl.sh
8
9#gen_template_fallback(template, meta, pfx, name, sfx, order, arch, atomic, int, args...)
10gen_template_fallback()
11{
12	local template="$1"; shift
13	local meta="$1"; shift
14	local pfx="$1"; shift
15	local name="$1"; shift
16	local sfx="$1"; shift
17	local order="$1"; shift
18	local arch="$1"; shift
19	local atomic="$1"; shift
20	local int="$1"; shift
21
22	local atomicname="${arch}${atomic}_${pfx}${name}${sfx}${order}"
23
24	local ret="$(gen_ret_type "${meta}" "${int}")"
25	local retstmt="$(gen_ret_stmt "${meta}")"
26	local params="$(gen_params "${int}" "${atomic}" "$@")"
27	local args="$(gen_args "$@")"
28
29	if [ ! -z "${template}" ]; then
30		printf "#ifndef ${atomicname}\n"
31		. ${template}
32		printf "#define ${atomicname} ${atomicname}\n"
33		printf "#endif\n\n"
34	fi
35}
36
37#gen_proto_fallback(meta, pfx, name, sfx, order, arch, atomic, int, args...)
38gen_proto_fallback()
39{
40	local meta="$1"; shift
41	local pfx="$1"; shift
42	local name="$1"; shift
43	local sfx="$1"; shift
44	local order="$1"; shift
45
46	local tmpl="$(find_fallback_template "${pfx}" "${name}" "${sfx}" "${order}")"
47	gen_template_fallback "${tmpl}" "${meta}" "${pfx}" "${name}" "${sfx}" "${order}" "$@"
48}
49
50#gen_basic_fallbacks(basename)
51gen_basic_fallbacks()
52{
53	local basename="$1"; shift
54cat << EOF
55#define ${basename}_acquire ${basename}
56#define ${basename}_release ${basename}
57#define ${basename}_relaxed ${basename}
58EOF
59}
60
61gen_proto_order_variant()
62{
63	local meta="$1"; shift
64	local pfx="$1"; shift
65	local name="$1"; shift
66	local sfx="$1"; shift
67	local order="$1"; shift
68	local arch="$1"
69	local atomic="$2"
70
71	local basename="${arch}${atomic}_${pfx}${name}${sfx}"
72
73	printf "#define arch_${basename}${order} ${basename}${order}\n"
74}
75
76#gen_proto_order_variants(meta, pfx, name, sfx, arch, atomic, int, args...)
77gen_proto_order_variants()
78{
79	local meta="$1"; shift
80	local pfx="$1"; shift
81	local name="$1"; shift
82	local sfx="$1"; shift
83	local arch="$1"
84	local atomic="$2"
85
86	local basename="${arch}${atomic}_${pfx}${name}${sfx}"
87
88	local template="$(find_fallback_template "${pfx}" "${name}" "${sfx}" "${order}")"
89
90	if [ -z "$arch" ]; then
91		gen_proto_order_variant "${meta}" "${pfx}" "${name}" "${sfx}" "" "$@"
92
93		if meta_has_acquire "${meta}"; then
94			gen_proto_order_variant "${meta}" "${pfx}" "${name}" "${sfx}" "_acquire" "$@"
95		fi
96		if meta_has_release "${meta}"; then
97			gen_proto_order_variant "${meta}" "${pfx}" "${name}" "${sfx}" "_release" "$@"
98		fi
99		if meta_has_relaxed "${meta}"; then
100			gen_proto_order_variant "${meta}" "${pfx}" "${name}" "${sfx}" "_relaxed" "$@"
101		fi
102
103		echo ""
104	fi
105
106	# If we don't have relaxed atomics, then we don't bother with ordering fallbacks
107	# read_acquire and set_release need to be templated, though
108	if ! meta_has_relaxed "${meta}"; then
109		gen_proto_fallback "${meta}" "${pfx}" "${name}" "${sfx}" "" "$@"
110
111		if meta_has_acquire "${meta}"; then
112			gen_proto_fallback "${meta}" "${pfx}" "${name}" "${sfx}" "_acquire" "$@"
113		fi
114
115		if meta_has_release "${meta}"; then
116			gen_proto_fallback "${meta}" "${pfx}" "${name}" "${sfx}" "_release" "$@"
117		fi
118
119		return
120	fi
121
122	printf "#ifndef ${basename}_relaxed\n"
123
124	if [ ! -z "${template}" ]; then
125		printf "#ifdef ${basename}\n"
126	fi
127
128	gen_basic_fallbacks "${basename}"
129
130	if [ ! -z "${template}" ]; then
131		printf "#endif /* ${arch}${atomic}_${pfx}${name}${sfx} */\n\n"
132		gen_proto_fallback "${meta}" "${pfx}" "${name}" "${sfx}" "" "$@"
133		gen_proto_fallback "${meta}" "${pfx}" "${name}" "${sfx}" "_acquire" "$@"
134		gen_proto_fallback "${meta}" "${pfx}" "${name}" "${sfx}" "_release" "$@"
135		gen_proto_fallback "${meta}" "${pfx}" "${name}" "${sfx}" "_relaxed" "$@"
136	fi
137
138	printf "#else /* ${basename}_relaxed */\n\n"
139
140	gen_template_fallback "${ATOMICDIR}/fallbacks/acquire"  "${meta}" "${pfx}" "${name}" "${sfx}" "_acquire" "$@"
141	gen_template_fallback "${ATOMICDIR}/fallbacks/release"  "${meta}" "${pfx}" "${name}" "${sfx}" "_release" "$@"
142	gen_template_fallback "${ATOMICDIR}/fallbacks/fence"  "${meta}" "${pfx}" "${name}" "${sfx}" "" "$@"
143
144	printf "#endif /* ${basename}_relaxed */\n\n"
145}
146
147gen_xchg_fallbacks()
148{
149	local xchg="$1"; shift
150cat <<EOF
151#ifndef ${xchg}_relaxed
152#define ${xchg}_relaxed		${xchg}
153#define ${xchg}_acquire		${xchg}
154#define ${xchg}_release		${xchg}
155#else /* ${xchg}_relaxed */
156
157#ifndef ${xchg}_acquire
158#define ${xchg}_acquire(...) \\
159	__atomic_op_acquire(${xchg}, __VA_ARGS__)
160#endif
161
162#ifndef ${xchg}_release
163#define ${xchg}_release(...) \\
164	__atomic_op_release(${xchg}, __VA_ARGS__)
165#endif
166
167#ifndef ${xchg}
168#define ${xchg}(...) \\
169	__atomic_op_fence(${xchg}, __VA_ARGS__)
170#endif
171
172#endif /* ${xchg}_relaxed */
173
174EOF
175}
176
177cat << EOF
178// SPDX-License-Identifier: GPL-2.0
179
180// Generated by $0
181// DO NOT MODIFY THIS FILE DIRECTLY
182
183#ifndef _LINUX_ATOMIC_FALLBACK_H
184#define _LINUX_ATOMIC_FALLBACK_H
185
186#include <linux/compiler.h>
187
188EOF
189
190for xchg in "${ARCH}xchg" "${ARCH}cmpxchg" "${ARCH}cmpxchg64"; do
191	gen_xchg_fallbacks "${xchg}"
192done
193
194grep '^[a-z]' "$1" | while read name meta args; do
195	gen_proto "${meta}" "${name}" "${ARCH}" "atomic" "int" ${args}
196done
197
198cat <<EOF
199#ifdef CONFIG_GENERIC_ATOMIC64
200#include <asm-generic/atomic64.h>
201#endif
202
203EOF
204
205grep '^[a-z]' "$1" | while read name meta args; do
206	gen_proto "${meta}" "${name}" "${ARCH}" "atomic64" "s64" ${args}
207done
208
209cat <<EOF
210#endif /* _LINUX_ATOMIC_FALLBACK_H */
211EOF
212