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