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
61#gen_proto_order_variants(meta, pfx, name, sfx, arch, atomic, int, args...)
62gen_proto_order_variants()
63{
64	local meta="$1"; shift
65	local pfx="$1"; shift
66	local name="$1"; shift
67	local sfx="$1"; shift
68	local arch="$1"
69	local atomic="$2"
70
71	local basename="${arch}${atomic}_${pfx}${name}${sfx}"
72
73	local template="$(find_fallback_template "${pfx}" "${name}" "${sfx}" "${order}")"
74
75	# If we don't have relaxed atomics, then we don't bother with ordering fallbacks
76	# read_acquire and set_release need to be templated, though
77	if ! meta_has_relaxed "${meta}"; then
78		gen_proto_fallback "${meta}" "${pfx}" "${name}" "${sfx}" "" "$@"
79
80		if meta_has_acquire "${meta}"; then
81			gen_proto_fallback "${meta}" "${pfx}" "${name}" "${sfx}" "_acquire" "$@"
82		fi
83
84		if meta_has_release "${meta}"; then
85			gen_proto_fallback "${meta}" "${pfx}" "${name}" "${sfx}" "_release" "$@"
86		fi
87
88		return
89	fi
90
91	printf "#ifndef ${basename}_relaxed\n"
92
93	if [ ! -z "${template}" ]; then
94		printf "#ifdef ${basename}\n"
95	fi
96
97	gen_basic_fallbacks "${basename}"
98
99	if [ ! -z "${template}" ]; then
100		printf "#endif /* ${arch}${atomic}_${pfx}${name}${sfx} */\n\n"
101		gen_proto_fallback "${meta}" "${pfx}" "${name}" "${sfx}" "" "$@"
102		gen_proto_fallback "${meta}" "${pfx}" "${name}" "${sfx}" "_acquire" "$@"
103		gen_proto_fallback "${meta}" "${pfx}" "${name}" "${sfx}" "_release" "$@"
104		gen_proto_fallback "${meta}" "${pfx}" "${name}" "${sfx}" "_relaxed" "$@"
105	fi
106
107	printf "#else /* ${basename}_relaxed */\n\n"
108
109	gen_template_fallback "${ATOMICDIR}/fallbacks/acquire"  "${meta}" "${pfx}" "${name}" "${sfx}" "_acquire" "$@"
110	gen_template_fallback "${ATOMICDIR}/fallbacks/release"  "${meta}" "${pfx}" "${name}" "${sfx}" "_release" "$@"
111	gen_template_fallback "${ATOMICDIR}/fallbacks/fence"  "${meta}" "${pfx}" "${name}" "${sfx}" "" "$@"
112
113	printf "#endif /* ${basename}_relaxed */\n\n"
114}
115
116gen_xchg_fallbacks()
117{
118	local xchg="$1"; shift
119cat <<EOF
120#ifndef ${xchg}_relaxed
121#define ${xchg}_relaxed		${xchg}
122#define ${xchg}_acquire		${xchg}
123#define ${xchg}_release		${xchg}
124#else /* ${xchg}_relaxed */
125
126#ifndef ${xchg}_acquire
127#define ${xchg}_acquire(...) \\
128	__atomic_op_acquire(${xchg}, __VA_ARGS__)
129#endif
130
131#ifndef ${xchg}_release
132#define ${xchg}_release(...) \\
133	__atomic_op_release(${xchg}, __VA_ARGS__)
134#endif
135
136#ifndef ${xchg}
137#define ${xchg}(...) \\
138	__atomic_op_fence(${xchg}, __VA_ARGS__)
139#endif
140
141#endif /* ${xchg}_relaxed */
142
143EOF
144}
145
146cat << EOF
147// SPDX-License-Identifier: GPL-2.0
148
149// Generated by $0
150// DO NOT MODIFY THIS FILE DIRECTLY
151
152#ifndef _LINUX_ATOMIC_FALLBACK_H
153#define _LINUX_ATOMIC_FALLBACK_H
154
155#include <linux/compiler.h>
156
157EOF
158
159for xchg in "${ARCH}xchg" "${ARCH}cmpxchg" "${ARCH}cmpxchg64"; do
160	gen_xchg_fallbacks "${xchg}"
161done
162
163grep '^[a-z]' "$1" | while read name meta args; do
164	gen_proto "${meta}" "${name}" "${ARCH}" "atomic" "int" ${args}
165done
166
167cat <<EOF
168#ifdef CONFIG_GENERIC_ATOMIC64
169#include <asm-generic/atomic64.h>
170#endif
171
172EOF
173
174grep '^[a-z]' "$1" | while read name meta args; do
175	gen_proto "${meta}" "${name}" "${ARCH}" "atomic64" "s64" ${args}
176done
177
178cat <<EOF
179#endif /* _LINUX_ATOMIC_FALLBACK_H */
180EOF
181