GNU Linux-libre 5.19-rc6-gnu
[releases.git] / arch / riscv / include / asm / alternative-macros.h
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 errata_id new_len
10         RISCV_PTR \oldptr
11         RISCV_PTR \newptr
12         REG_ASM \vendor_id
13         REG_ASM \new_len
14         .word   \errata_id
15 .endm
16
17 .macro ALT_NEW_CONTENT vendor_id, errata_id, enable = 1, new_c : vararg
18         .if \enable
19         .pushsection .alternative, "a"
20         ALT_ENTRY 886b, 888f, \vendor_id, \errata_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, errata_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, \errata_id, \enable, \new_c
45 .endm
46
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)
49
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
52 886 :
53         .option push
54         .option norvc
55         .option norelax
56         \old_c
57         .option pop
58 887 :
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
61 .endm
62
63 #define _ALTERNATIVE_CFG_2(old_c, new_c_1, vendor_id_1, errata_id_1,    \
64                                         CONFIG_k_1,                     \
65                                   new_c_2, vendor_id_2, errata_id_2,    \
66                                         CONFIG_k_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)
71
72 #else /* !__ASSEMBLY__ */
73
74 #include <asm/asm.h>
75 #include <linux/stringify.h>
76
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"
83
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") \
88         ".popsection\n"                                                 \
89         ".subsection 1\n"                                               \
90         "888 :\n"                                                       \
91         ".option push\n"                                                \
92         ".option norvc\n"                                               \
93         ".option norelax\n"                                             \
94         new_c "\n"                                                      \
95         ".option pop\n"                                                 \
96         "889 :\n"                                                       \
97         ".org   . - (887b - 886b) + (889b - 888b)\n"                    \
98         ".org   . - (889b - 888b) + (887b - 886b)\n"                    \
99         ".previous\n"                                                   \
100         ".endif\n"
101
102 #define __ALTERNATIVE_CFG(old_c, new_c, vendor_id, errata_id, enable)   \
103         "886 :\n"                                                       \
104         ".option push\n"                                                \
105         ".option norvc\n"                                               \
106         ".option norelax\n"                                             \
107         old_c "\n"                                                      \
108         ".option pop\n"                                                 \
109         "887 :\n"                                                       \
110         ALT_NEW_CONTENT(vendor_id, errata_id, enable, new_c)
111
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))
114
115 #define __ALTERNATIVE_CFG_2(old_c, new_c_1, vendor_id_1, errata_id_1,   \
116                                         enable_1,                       \
117                                    new_c_2, vendor_id_2, errata_id_2,   \
118                                         enable_2)                       \
119         "886 :\n"                                                       \
120         ".option push\n"                                                \
121         ".option norvc\n"                                               \
122         ".option norelax\n"                                             \
123         old_c "\n"                                                      \
124         ".option pop\n"                                                 \
125         "887 :\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)
128
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_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))
137
138 #endif /* __ASSEMBLY__ */
139
140 #else /* CONFIG_RISCV_ALTERNATIVE */
141 #ifdef __ASSEMBLY__
142
143 .macro __ALTERNATIVE_CFG old_c
144         \old_c
145 .endm
146
147 #define _ALTERNATIVE_CFG(old_c, new_c, vendor_id, errata_id, CONFIG_k) \
148         __ALTERNATIVE_CFG old_c
149
150 #define _ALTERNATIVE_CFG_2(old_c, new_c_1, vendor_id_1, errata_id_1,    \
151                                         CONFIG_k_1,                     \
152                                   new_c_2, vendor_id_2, errata_id_2,    \
153                                         CONFIG_k_2)                     \
154        __ALTERNATIVE_CFG old_c
155
156 #else /* !__ASSEMBLY__ */
157
158 #define __ALTERNATIVE_CFG(old_c)  \
159         old_c "\n"
160
161 #define _ALTERNATIVE_CFG(old_c, new_c, vendor_id, errata_id, CONFIG_k) \
162         __ALTERNATIVE_CFG(old_c)
163
164 #define _ALTERNATIVE_CFG_2(old_c, new_c_1, vendor_id_1, errata_id_1,    \
165                                         CONFIG_k_1,                     \
166                                   new_c_2, vendor_id_2, errata_id_2,    \
167                                         CONFIG_k_2) \
168        __ALTERNATIVE_CFG(old_c)
169
170 #endif /* __ASSEMBLY__ */
171 #endif /* CONFIG_RISCV_ALTERNATIVE */
172
173 /*
174  * Usage:
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));
178  *
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.
185  */
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)
188
189 /*
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.
195  */
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)
204
205 #endif