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 patch_id new_len
17 .macro ALT_NEW_CONTENT vendor_id
, patch_id
, enable
= 1, new_c
19 .pushsection
.alternative
, "a"
20 ALT_ENTRY
886b
, 888f
, \vendor_id
, \patch_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
, patch_id
, enable
44 ALT_NEW_CONTENT
\vendor_id
, \patch_id
, \enable
, "\new_c"
47 .macro ALTERNATIVE_CFG_2 old_c
, new_c_1
, vendor_id_1
, patch_id_1
, enable_1
, \
48 new_c_2
, vendor_id_2
, patch_id_2
, enable_2
49 ALTERNATIVE_CFG
"\old_c", "\new_c_1", \vendor_id_
1, \patch_id_1
, \enable_1
50 ALT_NEW_CONTENT
\vendor_id_
2, \patch_id_2
, \enable_2
, "\new_c_2"
53 #define __ALTERNATIVE_CFG(...) ALTERNATIVE_CFG __VA_ARGS__
54 #define __ALTERNATIVE_CFG_2(...) ALTERNATIVE_CFG_2 __VA_ARGS__
56 #else /* !__ASSEMBLY__ */
59 #include <linux/stringify.h>
61 #define ALT_ENTRY(oldptr, newptr, vendor_id, patch_id, newlen) \
62 ".4byte ((" oldptr ") - .) \n" \
63 ".4byte ((" newptr ") - .) \n" \
64 ".2byte " vendor_id "\n" \
65 ".2byte " newlen "\n" \
66 ".4byte " patch_id "\n"
68 #define ALT_NEW_CONTENT(vendor_id, patch_id, enable, new_c) \
69 ".if " __stringify(enable) " == 1\n" \
70 ".pushsection .alternative, \"a\"\n" \
71 ALT_ENTRY("886b", "888f", __stringify(vendor_id), __stringify(patch_id), "889f - 888f") \
81 ".org . - (887b - 886b) + (889b - 888b)\n" \
82 ".org . - (889b - 888b) + (887b - 886b)\n" \
86 #define __ALTERNATIVE_CFG(old_c, new_c, vendor_id, patch_id, enable) \
94 ALT_NEW_CONTENT(vendor_id, patch_id, enable, new_c)
96 #define __ALTERNATIVE_CFG_2(old_c, new_c_1, vendor_id_1, patch_id_1, enable_1, \
97 new_c_2, vendor_id_2, patch_id_2, enable_2) \
98 __ALTERNATIVE_CFG(old_c, new_c_1, vendor_id_1, patch_id_1, enable_1) \
99 ALT_NEW_CONTENT(vendor_id_2, patch_id_2, enable_2, new_c_2)
101 #endif /* __ASSEMBLY__ */
103 #define _ALTERNATIVE_CFG(old_c, new_c, vendor_id, patch_id, CONFIG_k) \
104 __ALTERNATIVE_CFG(old_c, new_c, vendor_id, patch_id, IS_ENABLED(CONFIG_k))
106 #define _ALTERNATIVE_CFG_2(old_c, new_c_1, vendor_id_1, patch_id_1, CONFIG_k_1, \
107 new_c_2, vendor_id_2, patch_id_2, CONFIG_k_2) \
108 __ALTERNATIVE_CFG_2(old_c, new_c_1, vendor_id_1, patch_id_1, IS_ENABLED(CONFIG_k_1), \
109 new_c_2, vendor_id_2, patch_id_2, IS_ENABLED(CONFIG_k_2))
111 #else /* CONFIG_RISCV_ALTERNATIVE */
114 .macro ALTERNATIVE_CFG old_c
118 #define _ALTERNATIVE_CFG(old_c, ...) \
119 ALTERNATIVE_CFG old_c
121 #define _ALTERNATIVE_CFG_2(old_c, ...) \
122 ALTERNATIVE_CFG old_c
124 #else /* !__ASSEMBLY__ */
126 #define __ALTERNATIVE_CFG(old_c) \
129 #define _ALTERNATIVE_CFG(old_c, ...) \
130 __ALTERNATIVE_CFG(old_c)
132 #define _ALTERNATIVE_CFG_2(old_c, ...) \
133 __ALTERNATIVE_CFG(old_c)
135 #endif /* __ASSEMBLY__ */
136 #endif /* CONFIG_RISCV_ALTERNATIVE */
140 * ALTERNATIVE(old_content, new_content, vendor_id, patch_id, CONFIG_k)
141 * in the assembly code. Otherwise,
142 * asm(ALTERNATIVE(old_content, new_content, vendor_id, patch_id, CONFIG_k));
144 * old_content: The old content which is probably replaced with new content.
145 * new_content: The new content.
146 * vendor_id: The CPU vendor ID.
147 * patch_id: The patch ID (erratum ID or cpufeature ID).
148 * CONFIG_k: The Kconfig of this patch ID. When Kconfig is disabled, the old
149 * content will always be executed.
151 #define ALTERNATIVE(old_content, new_content, vendor_id, patch_id, CONFIG_k) \
152 _ALTERNATIVE_CFG(old_content, new_content, vendor_id, patch_id, CONFIG_k)
155 * A vendor wants to replace an old_content, but another vendor has used
156 * ALTERNATIVE() to patch its customized content at the same location. In
157 * this case, this vendor can create a new macro ALTERNATIVE_2() based
158 * on the following sample code and then replace ALTERNATIVE() with
159 * ALTERNATIVE_2() to append its customized content.
161 #define ALTERNATIVE_2(old_content, new_content_1, vendor_id_1, patch_id_1, CONFIG_k_1, \
162 new_content_2, vendor_id_2, patch_id_2, CONFIG_k_2) \
163 _ALTERNATIVE_CFG_2(old_content, new_content_1, vendor_id_1, patch_id_1, CONFIG_k_1, \
164 new_content_2, vendor_id_2, patch_id_2, CONFIG_k_2)