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_order_fallbacks() 148{ 149 local xchg="$1"; shift 150 151cat <<EOF 152 153#ifndef ${xchg}_acquire 154#define ${xchg}_acquire(...) \\ 155 __atomic_op_acquire(${xchg}, __VA_ARGS__) 156#endif 157 158#ifndef ${xchg}_release 159#define ${xchg}_release(...) \\ 160 __atomic_op_release(${xchg}, __VA_ARGS__) 161#endif 162 163#ifndef ${xchg} 164#define ${xchg}(...) \\ 165 __atomic_op_fence(${xchg}, __VA_ARGS__) 166#endif 167 168EOF 169} 170 171gen_xchg_fallbacks() 172{ 173 local xchg="$1"; shift 174 printf "#ifndef ${xchg}_relaxed\n" 175 176 gen_basic_fallbacks ${xchg} 177 178 printf "#else /* ${xchg}_relaxed */\n" 179 180 gen_order_fallbacks ${xchg} 181 182 printf "#endif /* ${xchg}_relaxed */\n\n" 183} 184 185gen_try_cmpxchg_fallback() 186{ 187 local order="$1"; shift; 188 189cat <<EOF 190#ifndef ${ARCH}try_cmpxchg${order} 191#define ${ARCH}try_cmpxchg${order}(_ptr, _oldp, _new) \\ 192({ \\ 193 typeof(*(_ptr)) *___op = (_oldp), ___o = *___op, ___r; \\ 194 ___r = ${ARCH}cmpxchg${order}((_ptr), ___o, (_new)); \\ 195 if (unlikely(___r != ___o)) \\ 196 *___op = ___r; \\ 197 likely(___r == ___o); \\ 198}) 199#endif /* ${ARCH}try_cmpxchg${order} */ 200 201EOF 202} 203 204gen_try_cmpxchg_fallbacks() 205{ 206 printf "#ifndef ${ARCH}try_cmpxchg_relaxed\n" 207 printf "#ifdef ${ARCH}try_cmpxchg\n" 208 209 gen_basic_fallbacks "${ARCH}try_cmpxchg" 210 211 printf "#endif /* ${ARCH}try_cmpxchg */\n\n" 212 213 for order in "" "_acquire" "_release" "_relaxed"; do 214 gen_try_cmpxchg_fallback "${order}" 215 done 216 217 printf "#else /* ${ARCH}try_cmpxchg_relaxed */\n" 218 219 gen_order_fallbacks "${ARCH}try_cmpxchg" 220 221 printf "#endif /* ${ARCH}try_cmpxchg_relaxed */\n\n" 222} 223 224cat << EOF 225// SPDX-License-Identifier: GPL-2.0 226 227// Generated by $0 228// DO NOT MODIFY THIS FILE DIRECTLY 229 230#ifndef _LINUX_ATOMIC_FALLBACK_H 231#define _LINUX_ATOMIC_FALLBACK_H 232 233#include <linux/compiler.h> 234 235EOF 236 237for xchg in "${ARCH}xchg" "${ARCH}cmpxchg" "${ARCH}cmpxchg64"; do 238 gen_xchg_fallbacks "${xchg}" 239done 240 241gen_try_cmpxchg_fallbacks 242 243grep '^[a-z]' "$1" | while read name meta args; do 244 gen_proto "${meta}" "${name}" "${ARCH}" "atomic" "int" ${args} 245done 246 247cat <<EOF 248#ifdef CONFIG_GENERIC_ATOMIC64 249#include <asm-generic/atomic64.h> 250#endif 251 252EOF 253 254grep '^[a-z]' "$1" | while read name meta args; do 255 gen_proto "${meta}" "${name}" "${ARCH}" "atomic64" "s64" ${args} 256done 257 258cat <<EOF 259#endif /* _LINUX_ATOMIC_FALLBACK_H */ 260EOF 261