1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __ASM_ALTERNATIVE_MACROS_H
3 #define __ASM_ALTERNATIVE_MACROS_H
5 #ifdef CONFIG_RISCV_ALTERNATIVE
9 .macro ALT_ENTRY oldptr newptr vendor_id errata_id new_len
17 .macro ALT_NEW_CONTENT vendor_id, errata_id, enable = 1, new_c : vararg
19 .pushsection .alternative, "a"
20 ALT_ENTRY 886b, 888f, \vendor_id, \errata_id, 889f - 888f
30 .org . - (889b - 888b) + (887b - 886b)
31 .org . - (887b - 886b) + (889b - 888b)
36 .macro __ALTERNATIVE_CFG old_c, new_c, vendor_id, errata_id, enable
44 ALT_NEW_CONTENT \vendor_id, \errata_id, \enable, \new_c
47 #define _ALTERNATIVE_CFG(old_c, new_c, vendor_id, errata_id, CONFIG_k) \
48 __ALTERNATIVE_CFG old_c, new_c, vendor_id, errata_id, IS_ENABLED(CONFIG_k)
50 .macro __ALTERNATIVE_CFG_2 old_c, new_c_1, vendor_id_1, errata_id_1, enable_1, \
51 new_c_2, vendor_id_2, errata_id_2, enable_2
59 ALT_NEW_CONTENT \vendor_id_1, \errata_id_1, \enable_1, \new_c_1
60 ALT_NEW_CONTENT \vendor_id_2, \errata_id_2, \enable_2, \new_c_2
63 #define _ALTERNATIVE_CFG_2(old_c, new_c_1, vendor_id_1, errata_id_1, \
65 new_c_2, vendor_id_2, errata_id_2, \
67 __ALTERNATIVE_CFG_2 old_c, new_c_1, vendor_id_1, errata_id_1, \
68 IS_ENABLED(CONFIG_k_1), \
69 new_c_2, vendor_id_2, errata_id_2, \
70 IS_ENABLED(CONFIG_k_2)
72 #else /* !__ASSEMBLY__ */
75 #include <linux/stringify.h>
77 #define ALT_ENTRY(oldptr, newptr, vendor_id, errata_id, newlen) \
78 RISCV_PTR " " oldptr "\n" \
79 RISCV_PTR " " newptr "\n" \
80 REG_ASM " " vendor_id "\n" \
81 REG_ASM " " newlen "\n" \
82 ".word " errata_id "\n"
84 #define ALT_NEW_CONTENT(vendor_id, errata_id, enable, new_c) \
85 ".if " __stringify(enable) " == 1\n" \
86 ".pushsection .alternative, \"a\"\n" \
87 ALT_ENTRY("886b", "888f", __stringify(vendor_id), __stringify(errata_id), "889f - 888f") \
97 ".org . - (887b - 886b) + (889b - 888b)\n" \
98 ".org . - (889b - 888b) + (887b - 886b)\n" \
102 #define __ALTERNATIVE_CFG(old_c, new_c, vendor_id, errata_id, enable) \
106 ".option norelax\n" \
110 ALT_NEW_CONTENT(vendor_id, errata_id, enable, new_c)
112 #define _ALTERNATIVE_CFG(old_c, new_c, vendor_id, errata_id, CONFIG_k) \
113 __ALTERNATIVE_CFG(old_c, new_c, vendor_id, errata_id, IS_ENABLED(CONFIG_k))
115 #define __ALTERNATIVE_CFG_2(old_c, new_c_1, vendor_id_1, errata_id_1, \
117 new_c_2, vendor_id_2, errata_id_2, \
122 ".option norelax\n" \
126 ALT_NEW_CONTENT(vendor_id_1, errata_id_1, enable_1, new_c_1) \
127 ALT_NEW_CONTENT(vendor_id_2, errata_id_2, enable_2, new_c_2)
129 #define _ALTERNATIVE_CFG_2(old_c, new_c_1, vendor_id_1, errata_id_1, \
131 new_c_2, vendor_id_2, errata_id_2, \
133 __ALTERNATIVE_CFG_2(old_c, new_c_1, vendor_id_1, errata_id_1, \
134 IS_ENABLED(CONFIG_k_1), \
135 new_c_2, vendor_id_2, errata_id_2, \
136 IS_ENABLED(CONFIG_k_2))
138 #endif /* __ASSEMBLY__ */
140 #else /* CONFIG_RISCV_ALTERNATIVE */
143 .macro __ALTERNATIVE_CFG old_c
147 #define _ALTERNATIVE_CFG(old_c, new_c, vendor_id, errata_id, CONFIG_k) \
148 __ALTERNATIVE_CFG old_c
150 #define _ALTERNATIVE_CFG_2(old_c, new_c_1, vendor_id_1, errata_id_1, \
152 new_c_2, vendor_id_2, errata_id_2, \
154 __ALTERNATIVE_CFG old_c
156 #else /* !__ASSEMBLY__ */
158 #define __ALTERNATIVE_CFG(old_c) \
161 #define _ALTERNATIVE_CFG(old_c, new_c, vendor_id, errata_id, CONFIG_k) \
162 __ALTERNATIVE_CFG(old_c)
164 #define _ALTERNATIVE_CFG_2(old_c, new_c_1, vendor_id_1, errata_id_1, \
166 new_c_2, vendor_id_2, errata_id_2, \
168 __ALTERNATIVE_CFG(old_c)
170 #endif /* __ASSEMBLY__ */
171 #endif /* CONFIG_RISCV_ALTERNATIVE */
175 * ALTERNATIVE(old_content, new_content, vendor_id, errata_id, CONFIG_k)
176 * in the assembly code. Otherwise,
177 * asm(ALTERNATIVE(old_content, new_content, vendor_id, errata_id, CONFIG_k));
179 * old_content: The old content which is probably replaced with new content.
180 * new_content: The new content.
181 * vendor_id: The CPU vendor ID.
182 * errata_id: The errata ID.
183 * CONFIG_k: The Kconfig of this errata. When Kconfig is disabled, the old
184 * content will alwyas be executed.
186 #define ALTERNATIVE(old_content, new_content, vendor_id, errata_id, CONFIG_k) \
187 _ALTERNATIVE_CFG(old_content, new_content, vendor_id, errata_id, CONFIG_k)
190 * A vendor wants to replace an old_content, but another vendor has used
191 * ALTERNATIVE() to patch its customized content at the same location. In
192 * this case, this vendor can create a new macro ALTERNATIVE_2() based
193 * on the following sample code and then replace ALTERNATIVE() with
194 * ALTERNATIVE_2() to append its customized content.
196 #define ALTERNATIVE_2(old_content, new_content_1, vendor_id_1, \
197 errata_id_1, CONFIG_k_1, \
198 new_content_2, vendor_id_2, \
199 errata_id_2, CONFIG_k_2) \
200 _ALTERNATIVE_CFG_2(old_content, new_content_1, vendor_id_1, \
201 errata_id_1, CONFIG_k_1, \
202 new_content_2, vendor_id_2, \
203 errata_id_2, CONFIG_k_2)