xref: /linux/arch/riscv/include/asm/alternative-macros.h (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __ASM_ALTERNATIVE_MACROS_H
3 #define __ASM_ALTERNATIVE_MACROS_H
4 
5 #ifdef CONFIG_RISCV_ALTERNATIVE
6 
7 #ifdef __ASSEMBLY__
8 
9 .macro ALT_ENTRY oldptr newptr vendor_id patch_id new_len
10 	.4byte \oldptr - .
11 	.4byte \newptr - .
12 	.2byte \vendor_id
13 	.2byte \new_len
14 	.4byte \patch_id
15 .endm
16 
17 .macro ALT_NEW_CONTENT vendor_id, patch_id, enable = 1, new_c
18 	.if \enable
19 	.pushsection .alternative, "a"
20 	ALT_ENTRY 886b, 888f, \vendor_id, \patch_id, 889f - 888f
21 	.popsection
22 	.subsection 1
23 888 :
24 	.option push
25 	.option norvc
26 	.option norelax
27 	\new_c
28 	.option pop
29 889 :
30 	.org    . - (889b - 888b) + (887b - 886b)
31 	.org    . - (887b - 886b) + (889b - 888b)
32 	.previous
33 	.endif
34 .endm
35 
36 .macro ALTERNATIVE_CFG old_c, new_c, vendor_id, patch_id, enable
37 886 :
38 	.option push
39 	.option norvc
40 	.option norelax
41 	\old_c
42 	.option pop
43 887 :
44 	ALT_NEW_CONTENT \vendor_id, \patch_id, \enable, "\new_c"
45 .endm
46 
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"
51 .endm
52 
53 #define __ALTERNATIVE_CFG(...)		ALTERNATIVE_CFG __VA_ARGS__
54 #define __ALTERNATIVE_CFG_2(...)	ALTERNATIVE_CFG_2 __VA_ARGS__
55 
56 #else /* !__ASSEMBLY__ */
57 
58 #include <asm/asm.h>
59 #include <linux/stringify.h>
60 
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"
67 
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") \
72 	".popsection\n"							\
73 	".subsection 1\n"						\
74 	"888 :\n"							\
75 	".option push\n"						\
76 	".option norvc\n"						\
77 	".option norelax\n"						\
78 	new_c "\n"							\
79 	".option pop\n"							\
80 	"889 :\n"							\
81 	".org	. - (887b - 886b) + (889b - 888b)\n"			\
82 	".org	. - (889b - 888b) + (887b - 886b)\n"			\
83 	".previous\n"							\
84 	".endif\n"
85 
86 #define __ALTERNATIVE_CFG(old_c, new_c, vendor_id, patch_id, enable)	\
87 	"886 :\n"							\
88 	".option push\n"						\
89 	".option norvc\n"						\
90 	".option norelax\n"						\
91 	old_c "\n"							\
92 	".option pop\n"							\
93 	"887 :\n"							\
94 	ALT_NEW_CONTENT(vendor_id, patch_id, enable, new_c)
95 
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)
100 
101 #endif /* __ASSEMBLY__ */
102 
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))
105 
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))
110 
111 #else /* CONFIG_RISCV_ALTERNATIVE */
112 #ifdef __ASSEMBLY__
113 
114 .macro ALTERNATIVE_CFG old_c
115 	\old_c
116 .endm
117 
118 #define _ALTERNATIVE_CFG(old_c, ...)	\
119 	ALTERNATIVE_CFG old_c
120 
121 #define _ALTERNATIVE_CFG_2(old_c, ...)	\
122 	ALTERNATIVE_CFG old_c
123 
124 #else /* !__ASSEMBLY__ */
125 
126 #define __ALTERNATIVE_CFG(old_c)	\
127 	old_c "\n"
128 
129 #define _ALTERNATIVE_CFG(old_c, ...)	\
130 	__ALTERNATIVE_CFG(old_c)
131 
132 #define _ALTERNATIVE_CFG_2(old_c, ...)	\
133 	__ALTERNATIVE_CFG(old_c)
134 
135 #endif /* __ASSEMBLY__ */
136 #endif /* CONFIG_RISCV_ALTERNATIVE */
137 
138 /*
139  * Usage:
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));
143  *
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.
150  */
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)
153 
154 /*
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.
160  */
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)
165 
166 #endif
167