alternative-macros.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __ASM_ALTERNATIVE_MACROS_H
  3. #define __ASM_ALTERNATIVE_MACROS_H
  4. #ifdef CONFIG_RISCV_ALTERNATIVE
  5. #ifdef __ASSEMBLY__
  6. .macro ALT_ENTRY oldptr newptr vendor_id errata_id new_len
  7. RISCV_PTR \oldptr
  8. RISCV_PTR \newptr
  9. REG_ASM \vendor_id
  10. REG_ASM \new_len
  11. .word \errata_id
  12. .endm
  13. .macro ALT_NEW_CONTENT vendor_id, errata_id, enable = 1, new_c : vararg
  14. .if \enable
  15. .pushsection .alternative, "a"
  16. ALT_ENTRY 886b, 888f, \vendor_id, \errata_id, 889f - 888f
  17. .popsection
  18. .subsection 1
  19. 888 :
  20. .option push
  21. .option norvc
  22. .option norelax
  23. \new_c
  24. .option pop
  25. 889 :
  26. .org . - (889b - 888b) + (887b - 886b)
  27. .org . - (887b - 886b) + (889b - 888b)
  28. .previous
  29. .endif
  30. .endm
  31. .macro __ALTERNATIVE_CFG old_c, new_c, vendor_id, errata_id, enable
  32. 886 :
  33. .option push
  34. .option norvc
  35. .option norelax
  36. \old_c
  37. .option pop
  38. 887 :
  39. ALT_NEW_CONTENT \vendor_id, \errata_id, \enable, \new_c
  40. .endm
  41. #define _ALTERNATIVE_CFG(old_c, new_c, vendor_id, errata_id, CONFIG_k) \
  42. __ALTERNATIVE_CFG old_c, new_c, vendor_id, errata_id, IS_ENABLED(CONFIG_k)
  43. .macro __ALTERNATIVE_CFG_2 old_c, new_c_1, vendor_id_1, errata_id_1, enable_1, \
  44. new_c_2, vendor_id_2, errata_id_2, enable_2
  45. 886 :
  46. .option push
  47. .option norvc
  48. .option norelax
  49. \old_c
  50. .option pop
  51. 887 :
  52. ALT_NEW_CONTENT \vendor_id_1, \errata_id_1, \enable_1, \new_c_1
  53. ALT_NEW_CONTENT \vendor_id_2, \errata_id_2, \enable_2, \new_c_2
  54. .endm
  55. #define _ALTERNATIVE_CFG_2(old_c, new_c_1, vendor_id_1, errata_id_1, \
  56. CONFIG_k_1, \
  57. new_c_2, vendor_id_2, errata_id_2, \
  58. CONFIG_k_2) \
  59. __ALTERNATIVE_CFG_2 old_c, new_c_1, vendor_id_1, errata_id_1, \
  60. IS_ENABLED(CONFIG_k_1), \
  61. new_c_2, vendor_id_2, errata_id_2, \
  62. IS_ENABLED(CONFIG_k_2)
  63. #else /* !__ASSEMBLY__ */
  64. #include <asm/asm.h>
  65. #include <linux/stringify.h>
  66. #define ALT_ENTRY(oldptr, newptr, vendor_id, errata_id, newlen) \
  67. RISCV_PTR " " oldptr "\n" \
  68. RISCV_PTR " " newptr "\n" \
  69. REG_ASM " " vendor_id "\n" \
  70. REG_ASM " " newlen "\n" \
  71. ".word " errata_id "\n"
  72. #define ALT_NEW_CONTENT(vendor_id, errata_id, enable, new_c) \
  73. ".if " __stringify(enable) " == 1\n" \
  74. ".pushsection .alternative, \"a\"\n" \
  75. ALT_ENTRY("886b", "888f", __stringify(vendor_id), __stringify(errata_id), "889f - 888f") \
  76. ".popsection\n" \
  77. ".subsection 1\n" \
  78. "888 :\n" \
  79. ".option push\n" \
  80. ".option norvc\n" \
  81. ".option norelax\n" \
  82. new_c "\n" \
  83. ".option pop\n" \
  84. "889 :\n" \
  85. ".org . - (887b - 886b) + (889b - 888b)\n" \
  86. ".org . - (889b - 888b) + (887b - 886b)\n" \
  87. ".previous\n" \
  88. ".endif\n"
  89. #define __ALTERNATIVE_CFG(old_c, new_c, vendor_id, errata_id, enable) \
  90. "886 :\n" \
  91. ".option push\n" \
  92. ".option norvc\n" \
  93. ".option norelax\n" \
  94. old_c "\n" \
  95. ".option pop\n" \
  96. "887 :\n" \
  97. ALT_NEW_CONTENT(vendor_id, errata_id, enable, new_c)
  98. #define _ALTERNATIVE_CFG(old_c, new_c, vendor_id, errata_id, CONFIG_k) \
  99. __ALTERNATIVE_CFG(old_c, new_c, vendor_id, errata_id, IS_ENABLED(CONFIG_k))
  100. #define __ALTERNATIVE_CFG_2(old_c, new_c_1, vendor_id_1, errata_id_1, \
  101. enable_1, \
  102. new_c_2, vendor_id_2, errata_id_2, \
  103. enable_2) \
  104. "886 :\n" \
  105. ".option push\n" \
  106. ".option norvc\n" \
  107. ".option norelax\n" \
  108. old_c "\n" \
  109. ".option pop\n" \
  110. "887 :\n" \
  111. ALT_NEW_CONTENT(vendor_id_1, errata_id_1, enable_1, new_c_1) \
  112. ALT_NEW_CONTENT(vendor_id_2, errata_id_2, enable_2, new_c_2)
  113. #define _ALTERNATIVE_CFG_2(old_c, new_c_1, vendor_id_1, errata_id_1, \
  114. CONFIG_k_1, \
  115. new_c_2, vendor_id_2, errata_id_2, \
  116. CONFIG_k_2) \
  117. __ALTERNATIVE_CFG_2(old_c, new_c_1, vendor_id_1, errata_id_1, \
  118. IS_ENABLED(CONFIG_k_1), \
  119. new_c_2, vendor_id_2, errata_id_2, \
  120. IS_ENABLED(CONFIG_k_2))
  121. #endif /* __ASSEMBLY__ */
  122. #else /* CONFIG_RISCV_ALTERNATIVE */
  123. #ifdef __ASSEMBLY__
  124. .macro __ALTERNATIVE_CFG old_c
  125. \old_c
  126. .endm
  127. #define _ALTERNATIVE_CFG(old_c, new_c, vendor_id, errata_id, CONFIG_k) \
  128. __ALTERNATIVE_CFG old_c
  129. #define _ALTERNATIVE_CFG_2(old_c, new_c_1, vendor_id_1, errata_id_1, \
  130. CONFIG_k_1, \
  131. new_c_2, vendor_id_2, errata_id_2, \
  132. CONFIG_k_2) \
  133. __ALTERNATIVE_CFG old_c
  134. #else /* !__ASSEMBLY__ */
  135. #define __ALTERNATIVE_CFG(old_c) \
  136. old_c "\n"
  137. #define _ALTERNATIVE_CFG(old_c, new_c, vendor_id, errata_id, CONFIG_k) \
  138. __ALTERNATIVE_CFG(old_c)
  139. #define _ALTERNATIVE_CFG_2(old_c, new_c_1, vendor_id_1, errata_id_1, \
  140. CONFIG_k_1, \
  141. new_c_2, vendor_id_2, errata_id_2, \
  142. CONFIG_k_2) \
  143. __ALTERNATIVE_CFG(old_c)
  144. #endif /* __ASSEMBLY__ */
  145. #endif /* CONFIG_RISCV_ALTERNATIVE */
  146. /*
  147. * Usage:
  148. * ALTERNATIVE(old_content, new_content, vendor_id, errata_id, CONFIG_k)
  149. * in the assembly code. Otherwise,
  150. * asm(ALTERNATIVE(old_content, new_content, vendor_id, errata_id, CONFIG_k));
  151. *
  152. * old_content: The old content which is probably replaced with new content.
  153. * new_content: The new content.
  154. * vendor_id: The CPU vendor ID.
  155. * errata_id: The errata ID.
  156. * CONFIG_k: The Kconfig of this errata. When Kconfig is disabled, the old
  157. * content will alwyas be executed.
  158. */
  159. #define ALTERNATIVE(old_content, new_content, vendor_id, errata_id, CONFIG_k) \
  160. _ALTERNATIVE_CFG(old_content, new_content, vendor_id, errata_id, CONFIG_k)
  161. /*
  162. * A vendor wants to replace an old_content, but another vendor has used
  163. * ALTERNATIVE() to patch its customized content at the same location. In
  164. * this case, this vendor can create a new macro ALTERNATIVE_2() based
  165. * on the following sample code and then replace ALTERNATIVE() with
  166. * ALTERNATIVE_2() to append its customized content.
  167. */
  168. #define ALTERNATIVE_2(old_content, new_content_1, vendor_id_1, \
  169. errata_id_1, CONFIG_k_1, \
  170. new_content_2, vendor_id_2, \
  171. errata_id_2, CONFIG_k_2) \
  172. _ALTERNATIVE_CFG_2(old_content, new_content_1, vendor_id_1, \
  173. errata_id_1, CONFIG_k_1, \
  174. new_content_2, vendor_id_2, \
  175. errata_id_2, CONFIG_k_2)
  176. #endif