gen-atomic-fallback.sh 5.7 KB

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