blob: 06ecd92b9bccc8ec11af89960739970483b01978 [file] [log] [blame]
Bin Meng055700e2018-09-26 06:55:14 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
4 */
5
Simon Glass34ee3ed2023-12-15 20:14:09 -07006#include <command.h>
Bin Meng7a3bbfb2018-12-12 06:12:34 -08007#include <cpu.h>
Simon Glass34ee3ed2023-12-15 20:14:09 -07008#include <cpu_func.h>
Bin Mengedfe9a92018-12-12 06:12:38 -08009#include <dm.h>
Heinrich Schuchardtcc382ff2021-09-12 21:11:46 +020010#include <dm/lists.h>
Simon Glassfc557362022-03-04 08:43:05 -070011#include <event.h>
Simon Glass34ee3ed2023-12-15 20:14:09 -070012#include <hang.h>
Simon Glass97589732020-05-10 11:40:02 -060013#include <init.h>
Bin Meng7a3bbfb2018-12-12 06:12:34 -080014#include <log.h>
Bin Menga7544ed2018-12-12 06:12:40 -080015#include <asm/encoding.h>
Simon Glassfc557362022-03-04 08:43:05 -070016#include <asm/system.h>
Mayuresh Chitale7df8a0c2025-01-06 13:04:04 +000017#include <asm/hwcap.h>
18#include <asm/cpufeature.h>
Bin Mengedfe9a92018-12-12 06:12:38 -080019#include <dm/uclass-internal.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060020#include <linux/bitops.h>
Mayuresh Chitale7df8a0c2025-01-06 13:04:04 +000021#include <linux/log2.h>
22#include <linux/ctype.h>
Bin Meng055700e2018-09-26 06:55:14 -070023
Lukas Auer39a652b2018-11-22 11:26:29 +010024/*
Lukas Auera3596652019-03-17 19:28:37 +010025 * The variables here must be stored in the data section since they are used
Lukas Auer39a652b2018-11-22 11:26:29 +010026 * before the bss section is available.
27 */
Nikita Shubin7e5e0292022-09-02 11:47:39 +030028#if !CONFIG_IS_ENABLED(XIP)
Marek BehĂșn4bebdd32021-05-20 13:23:52 +020029u32 hart_lottery __section(".data") = 0;
Lukas Auera3596652019-03-17 19:28:37 +010030
Rick Chen9c4d5c12022-09-21 14:34:54 +080031#ifdef CONFIG_AVAILABLE_HARTS
Lukas Auera3596652019-03-17 19:28:37 +010032/*
33 * The main hart running U-Boot has acquired available_harts_lock until it has
34 * finished initialization of global data.
35 */
36u32 available_harts_lock = 1;
Rick Chene5e6c362019-04-30 13:49:33 +080037#endif
Rick Chen9c4d5c12022-09-21 14:34:54 +080038#endif
Lukas Auer39a652b2018-11-22 11:26:29 +010039
Mayuresh Chitale7df8a0c2025-01-06 13:04:04 +000040/* Host ISA bitmap */
41static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __section(".data");
42
43static unsigned int riscv_cbom_block_size __section(".data");
44static unsigned int riscv_cboz_block_size __section(".data");
45/**
46 * __riscv_isa_extension_available() - Check whether given extension
47 * is available or not
48 *
49 * @bit: bit position of the desired extension
50 * Return: true or false
51 *
52 */
53static bool __riscv_isa_extension_available(unsigned int bit)
54{
55 if (bit >= RISCV_ISA_EXT_MAX)
56 return false;
57
58 return test_bit(bit, riscv_isa) ? true : false;
59}
60
61inline unsigned int riscv_get_cbom_block_size(void)
62{
63 return riscv_cbom_block_size;
64}
65
66inline unsigned int riscv_get_cboz_block_size(void)
67{
68 return riscv_cboz_block_size;
69}
70
71static int riscv_ext_zicbom_validate(const struct riscv_isa_ext_data *data,
72 const unsigned long *isa_bitmap)
73{
74 struct udevice *dev;
75
76 if (!CONFIG_IS_ENABLED(RISCV_ISA_ZICBOM) || riscv_cbom_block_size)
77 return 0;
78
79 uclass_first_device(UCLASS_CPU, &dev);
80 if (!dev) {
81 log_info("Failed to get cpu device!\n");
82 return -ENXIO;
83 }
84
85 if (!dev_read_u32(dev, "riscv,cbom-block-size",
86 &riscv_cbom_block_size)) {
87 if (!riscv_cbom_block_size) {
88 log_err("Zicbom detected in ISA string, disabling as no cbom-block-size found\n");
89 return -EINVAL;
90 }
91 if (!is_power_of_2(riscv_cbom_block_size)) {
92 log_err("Zicbom disabled as cbom-block-size present, but is not a power-of-2\n");
93 return -EINVAL;
94 }
95 return 0;
96 } else {
97 return -EINVAL;
98 }
99}
100
101static int riscv_ext_zicboz_validate(const struct riscv_isa_ext_data *data,
102 const unsigned long *isa_bitmap)
103{
104 struct udevice *dev;
105
106 if (!CONFIG_IS_ENABLED(RISCV_ISA_ZICBOM) || riscv_cboz_block_size)
107 return 0;
108
109 uclass_first_device(UCLASS_CPU, &dev);
110 if (!dev) {
111 log_debug("Failed to get cpu device!\n");
112 return -ENXIO;
113 }
114
115 if (!dev_read_u32(dev, "riscv,cboz-block-size",
116 &riscv_cboz_block_size)) {
117 if (!riscv_cboz_block_size) {
118 log_err("Zicboz detected in ISA string, disabling as no cboz-block-size found\n");
119 return -EINVAL;
120 }
121 if (!is_power_of_2(riscv_cboz_block_size)) {
122 log_err("Zicboz disabled as cboz-block-size present, but is not a power-of-2\n");
123 return -EINVAL;
124 }
125 return 0;
126 } else {
127 return -EINVAL;
128 }
129}
130
131static int riscv_ext_zca_depends(const struct riscv_isa_ext_data *data,
132 const unsigned long *isa_bitmap)
133{
134 if (__riscv_isa_extension_available(RISCV_ISA_EXT_ZCA))
135 return 0;
136
137 return -EINVAL;
138}
139
140static int riscv_ext_zcd_validate(const struct riscv_isa_ext_data *data,
141 const unsigned long *isa_bitmap)
142{
143 if (__riscv_isa_extension_available(RISCV_ISA_EXT_ZCA) &&
144 __riscv_isa_extension_available(RISCV_ISA_EXT_d))
145 return 0;
146
147 return -EINVAL;
148}
149
150static int riscv_ext_zcf_validate(const struct riscv_isa_ext_data *data,
151 const unsigned long *isa_bitmap)
152{
153 if (IS_ENABLED(CONFIG_64BIT))
154 return -EINVAL;
155
156 if (__riscv_isa_extension_available(RISCV_ISA_EXT_ZCA) &&
157 __riscv_isa_extension_available(RISCV_ISA_EXT_f))
158 return 0;
159
160 return -EINVAL;
161}
162
163static const unsigned int riscv_zk_bundled_exts[] = {
164 RISCV_ISA_EXT_ZBKB,
165 RISCV_ISA_EXT_ZBKC,
166 RISCV_ISA_EXT_ZBKX,
167 RISCV_ISA_EXT_ZKND,
168 RISCV_ISA_EXT_ZKNE,
169 RISCV_ISA_EXT_ZKR,
170 RISCV_ISA_EXT_ZKT,
171};
172
173static const unsigned int riscv_zkn_bundled_exts[] = {
174 RISCV_ISA_EXT_ZBKB,
175 RISCV_ISA_EXT_ZBKC,
176 RISCV_ISA_EXT_ZBKX,
177 RISCV_ISA_EXT_ZKND,
178 RISCV_ISA_EXT_ZKNE,
179 RISCV_ISA_EXT_ZKNH,
180};
181
182static const unsigned int riscv_zks_bundled_exts[] = {
183 RISCV_ISA_EXT_ZBKB,
184 RISCV_ISA_EXT_ZBKC,
185 RISCV_ISA_EXT_ZKSED,
186 RISCV_ISA_EXT_ZKSH
187};
188
189#define RISCV_ISA_EXT_ZVKN \
190 RISCV_ISA_EXT_ZVKNED, \
191 RISCV_ISA_EXT_ZVKNHB, \
192 RISCV_ISA_EXT_ZVKB, \
193 RISCV_ISA_EXT_ZVKT
194
195static const unsigned int riscv_zvkn_bundled_exts[] = {
196 RISCV_ISA_EXT_ZVKN
197};
198
199static const unsigned int riscv_zvknc_bundled_exts[] = {
200 RISCV_ISA_EXT_ZVKN,
201 RISCV_ISA_EXT_ZVBC
202};
203
204static const unsigned int riscv_zvkng_bundled_exts[] = {
205 RISCV_ISA_EXT_ZVKN,
206 RISCV_ISA_EXT_ZVKG
207};
208
209#define RISCV_ISA_EXT_ZVKS \
210 RISCV_ISA_EXT_ZVKSED, \
211 RISCV_ISA_EXT_ZVKSH, \
212 RISCV_ISA_EXT_ZVKB, \
213 RISCV_ISA_EXT_ZVKT
214
215static const unsigned int riscv_zvks_bundled_exts[] = {
216 RISCV_ISA_EXT_ZVKS
217};
218
219static const unsigned int riscv_zvksc_bundled_exts[] = {
220 RISCV_ISA_EXT_ZVKS,
221 RISCV_ISA_EXT_ZVBC
222};
223
224static const unsigned int riscv_zvksg_bundled_exts[] = {
225 RISCV_ISA_EXT_ZVKS,
226 RISCV_ISA_EXT_ZVKG
227};
228
229static const unsigned int riscv_zvbb_exts[] = {
230 RISCV_ISA_EXT_ZVKB
231};
232
233#define RISCV_ISA_EXT_ZVE64F_IMPLY_LIST \
234 RISCV_ISA_EXT_ZVE64X, \
235 RISCV_ISA_EXT_ZVE32F, \
236 RISCV_ISA_EXT_ZVE32X
237
238#define RISCV_ISA_EXT_ZVE64D_IMPLY_LIST \
239 RISCV_ISA_EXT_ZVE64F, \
240 RISCV_ISA_EXT_ZVE64F_IMPLY_LIST
241
242#define RISCV_ISA_EXT_V_IMPLY_LIST \
243 RISCV_ISA_EXT_ZVE64D, \
244 RISCV_ISA_EXT_ZVE64D_IMPLY_LIST
245
246static const unsigned int riscv_zve32f_exts[] = {
247 RISCV_ISA_EXT_ZVE32X
248};
249
250static const unsigned int riscv_zve64f_exts[] = {
251 RISCV_ISA_EXT_ZVE64F_IMPLY_LIST
252};
253
254static const unsigned int riscv_zve64d_exts[] = {
255 RISCV_ISA_EXT_ZVE64D_IMPLY_LIST
256};
257
258static const unsigned int riscv_v_exts[] = {
259 RISCV_ISA_EXT_V_IMPLY_LIST
260};
261
262static const unsigned int riscv_zve64x_exts[] = {
263 RISCV_ISA_EXT_ZVE32X,
264 RISCV_ISA_EXT_ZVE64X
265};
266
267/*
268 * While the [ms]envcfg CSRs were not defined until version 1.12 of the RISC-V
269 * privileged ISA, the existence of the CSRs is implied by any extension which
270 * specifies [ms]envcfg bit(s). Hence, we define a custom ISA extension for the
271 * existence of the CSR, and treat it as a subset of those other extensions.
272 */
273static const unsigned int riscv_xlinuxenvcfg_exts[] = {
274 RISCV_ISA_EXT_XLINUXENVCFG
275};
276
277/*
278 * Zc* spec states that:
279 * - C always implies Zca
280 * - C+F implies Zcf (RV32 only)
281 * - C+D implies Zcd
282 *
283 * These extensions will be enabled and then validated depending on the
284 * availability of F/D RV32.
285 */
286static const unsigned int riscv_c_exts[] = {
287 RISCV_ISA_EXT_ZCA,
288 RISCV_ISA_EXT_ZCF,
289 RISCV_ISA_EXT_ZCD,
290};
291
292/*
293 * The canonical order of ISA extension names in the ISA string is defined in
294 * chapter 27 of the unprivileged specification.
295 *
296 * Ordinarily, for in-kernel data structures, this order is unimportant but
297 * isa_ext_arr defines the order of the ISA string in /proc/cpuinfo.
298 *
299 * The specification uses vague wording, such as should, when it comes to
300 * ordering, so for our purposes the following rules apply:
301 *
302 * 1. All multi-letter extensions must be separated from other extensions by an
303 * underscore.
304 *
305 * 2. Additional standard extensions (starting with 'Z') must be sorted after
306 * single-letter extensions and before any higher-privileged extensions.
307 *
308 * 3. The first letter following the 'Z' conventionally indicates the most
309 * closely related alphabetical extension category, IMAFDQLCBKJTPVH.
310 * If multiple 'Z' extensions are named, they must be ordered first by
311 * category, then alphabetically within a category.
312 *
313 * 3. Standard supervisor-level extensions (starting with 'S') must be listed
314 * after standard unprivileged extensions. If multiple supervisor-level
315 * extensions are listed, they must be ordered alphabetically.
316 *
317 * 4. Standard machine-level extensions (starting with 'Zxm') must be listed
318 * after any lower-privileged, standard extensions. If multiple
319 * machine-level extensions are listed, they must be ordered
320 * alphabetically.
321 *
322 * 5. Non-standard extensions (starting with 'X') must be listed after all
323 * standard extensions. If multiple non-standard extensions are listed, they
324 * must be ordered alphabetically.
325 *
326 * An example string following the order is:
327 * rv64imadc_zifoo_zigoo_zafoo_sbar_scar_zxmbaz_xqux_xrux
328 *
329 * New entries to this struct should follow the ordering rules described above.
330 */
331const struct riscv_isa_ext_data riscv_isa_ext[] = {
332 __RISCV_ISA_EXT_DATA(i, RISCV_ISA_EXT_i),
333 __RISCV_ISA_EXT_DATA(m, RISCV_ISA_EXT_m),
334 __RISCV_ISA_EXT_DATA(a, RISCV_ISA_EXT_a),
335 __RISCV_ISA_EXT_DATA(f, RISCV_ISA_EXT_f),
336 __RISCV_ISA_EXT_DATA(d, RISCV_ISA_EXT_d),
337 __RISCV_ISA_EXT_DATA(q, RISCV_ISA_EXT_q),
338 __RISCV_ISA_EXT_SUPERSET(c, RISCV_ISA_EXT_c, riscv_c_exts),
339 __RISCV_ISA_EXT_SUPERSET(v, RISCV_ISA_EXT_v, riscv_v_exts),
340 __RISCV_ISA_EXT_DATA(h, RISCV_ISA_EXT_h),
341 __RISCV_ISA_EXT_SUPERSET_VALIDATE(zicbom, RISCV_ISA_EXT_ZICBOM, riscv_xlinuxenvcfg_exts,
342 riscv_ext_zicbom_validate),
343 __RISCV_ISA_EXT_SUPERSET_VALIDATE(zicboz, RISCV_ISA_EXT_ZICBOZ, riscv_xlinuxenvcfg_exts,
344 riscv_ext_zicboz_validate),
345 __RISCV_ISA_EXT_DATA(zicntr, RISCV_ISA_EXT_ZICNTR),
346 __RISCV_ISA_EXT_DATA(zicond, RISCV_ISA_EXT_ZICOND),
347 __RISCV_ISA_EXT_DATA(zicsr, RISCV_ISA_EXT_ZICSR),
348 __RISCV_ISA_EXT_DATA(zifencei, RISCV_ISA_EXT_ZIFENCEI),
349 __RISCV_ISA_EXT_DATA(zihintntl, RISCV_ISA_EXT_ZIHINTNTL),
350 __RISCV_ISA_EXT_DATA(zihintpause, RISCV_ISA_EXT_ZIHINTPAUSE),
351 __RISCV_ISA_EXT_DATA(zihpm, RISCV_ISA_EXT_ZIHPM),
352 __RISCV_ISA_EXT_DATA(zimop, RISCV_ISA_EXT_ZIMOP),
353 __RISCV_ISA_EXT_DATA(zacas, RISCV_ISA_EXT_ZACAS),
354 __RISCV_ISA_EXT_DATA(zawrs, RISCV_ISA_EXT_ZAWRS),
355 __RISCV_ISA_EXT_DATA(zfa, RISCV_ISA_EXT_ZFA),
356 __RISCV_ISA_EXT_DATA(zfh, RISCV_ISA_EXT_ZFH),
357 __RISCV_ISA_EXT_DATA(zfhmin, RISCV_ISA_EXT_ZFHMIN),
358 __RISCV_ISA_EXT_DATA(zca, RISCV_ISA_EXT_ZCA),
359 __RISCV_ISA_EXT_DATA_VALIDATE(zcb, RISCV_ISA_EXT_ZCB, riscv_ext_zca_depends),
360 __RISCV_ISA_EXT_DATA_VALIDATE(zcd, RISCV_ISA_EXT_ZCD, riscv_ext_zcd_validate),
361 __RISCV_ISA_EXT_DATA_VALIDATE(zcf, RISCV_ISA_EXT_ZCF, riscv_ext_zcf_validate),
362 __RISCV_ISA_EXT_DATA_VALIDATE(zcmop, RISCV_ISA_EXT_ZCMOP, riscv_ext_zca_depends),
363 __RISCV_ISA_EXT_DATA(zba, RISCV_ISA_EXT_ZBA),
364 __RISCV_ISA_EXT_DATA(zbb, RISCV_ISA_EXT_ZBB),
365 __RISCV_ISA_EXT_DATA(zbc, RISCV_ISA_EXT_ZBC),
366 __RISCV_ISA_EXT_DATA(zbkb, RISCV_ISA_EXT_ZBKB),
367 __RISCV_ISA_EXT_DATA(zbkc, RISCV_ISA_EXT_ZBKC),
368 __RISCV_ISA_EXT_DATA(zbkx, RISCV_ISA_EXT_ZBKX),
369 __RISCV_ISA_EXT_DATA(zbs, RISCV_ISA_EXT_ZBS),
370 __RISCV_ISA_EXT_BUNDLE(zk, riscv_zk_bundled_exts),
371 __RISCV_ISA_EXT_BUNDLE(zkn, riscv_zkn_bundled_exts),
372 __RISCV_ISA_EXT_DATA(zknd, RISCV_ISA_EXT_ZKND),
373 __RISCV_ISA_EXT_DATA(zkne, RISCV_ISA_EXT_ZKNE),
374 __RISCV_ISA_EXT_DATA(zknh, RISCV_ISA_EXT_ZKNH),
375 __RISCV_ISA_EXT_DATA(zkr, RISCV_ISA_EXT_ZKR),
376 __RISCV_ISA_EXT_BUNDLE(zks, riscv_zks_bundled_exts),
377 __RISCV_ISA_EXT_DATA(zkt, RISCV_ISA_EXT_ZKT),
378 __RISCV_ISA_EXT_DATA(zksed, RISCV_ISA_EXT_ZKSED),
379 __RISCV_ISA_EXT_DATA(zksh, RISCV_ISA_EXT_ZKSH),
380 __RISCV_ISA_EXT_DATA(ztso, RISCV_ISA_EXT_ZTSO),
381 __RISCV_ISA_EXT_SUPERSET(zvbb, RISCV_ISA_EXT_ZVBB, riscv_zvbb_exts),
382 __RISCV_ISA_EXT_DATA(zvbc, RISCV_ISA_EXT_ZVBC),
383 __RISCV_ISA_EXT_SUPERSET(zve32f, RISCV_ISA_EXT_ZVE32F, riscv_zve32f_exts),
384 __RISCV_ISA_EXT_DATA(zve32x, RISCV_ISA_EXT_ZVE32X),
385 __RISCV_ISA_EXT_SUPERSET(zve64d, RISCV_ISA_EXT_ZVE64D, riscv_zve64d_exts),
386 __RISCV_ISA_EXT_SUPERSET(zve64f, RISCV_ISA_EXT_ZVE64F, riscv_zve64f_exts),
387 __RISCV_ISA_EXT_SUPERSET(zve64x, RISCV_ISA_EXT_ZVE64X, riscv_zve64x_exts),
388 __RISCV_ISA_EXT_DATA(zvfh, RISCV_ISA_EXT_ZVFH),
389 __RISCV_ISA_EXT_DATA(zvfhmin, RISCV_ISA_EXT_ZVFHMIN),
390 __RISCV_ISA_EXT_DATA(zvkb, RISCV_ISA_EXT_ZVKB),
391 __RISCV_ISA_EXT_DATA(zvkg, RISCV_ISA_EXT_ZVKG),
392 __RISCV_ISA_EXT_BUNDLE(zvkn, riscv_zvkn_bundled_exts),
393 __RISCV_ISA_EXT_BUNDLE(zvknc, riscv_zvknc_bundled_exts),
394 __RISCV_ISA_EXT_DATA(zvkned, RISCV_ISA_EXT_ZVKNED),
395 __RISCV_ISA_EXT_BUNDLE(zvkng, riscv_zvkng_bundled_exts),
396 __RISCV_ISA_EXT_DATA(zvknha, RISCV_ISA_EXT_ZVKNHA),
397 __RISCV_ISA_EXT_DATA(zvknhb, RISCV_ISA_EXT_ZVKNHB),
398 __RISCV_ISA_EXT_BUNDLE(zvks, riscv_zvks_bundled_exts),
399 __RISCV_ISA_EXT_BUNDLE(zvksc, riscv_zvksc_bundled_exts),
400 __RISCV_ISA_EXT_DATA(zvksed, RISCV_ISA_EXT_ZVKSED),
401 __RISCV_ISA_EXT_DATA(zvksh, RISCV_ISA_EXT_ZVKSH),
402 __RISCV_ISA_EXT_BUNDLE(zvksg, riscv_zvksg_bundled_exts),
403 __RISCV_ISA_EXT_DATA(zvkt, RISCV_ISA_EXT_ZVKT),
404 __RISCV_ISA_EXT_DATA(smaia, RISCV_ISA_EXT_SMAIA),
405 __RISCV_ISA_EXT_DATA(smmpm, RISCV_ISA_EXT_SMMPM),
406 __RISCV_ISA_EXT_SUPERSET(smnpm, RISCV_ISA_EXT_SMNPM, riscv_xlinuxenvcfg_exts),
407 __RISCV_ISA_EXT_DATA(smstateen, RISCV_ISA_EXT_SMSTATEEN),
408 __RISCV_ISA_EXT_DATA(ssaia, RISCV_ISA_EXT_SSAIA),
409 __RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF),
410 __RISCV_ISA_EXT_SUPERSET(ssnpm, RISCV_ISA_EXT_SSNPM, riscv_xlinuxenvcfg_exts),
411 __RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),
412 __RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),
413 __RISCV_ISA_EXT_DATA(svnapot, RISCV_ISA_EXT_SVNAPOT),
414 __RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),
415 __RISCV_ISA_EXT_DATA(svvptc, RISCV_ISA_EXT_SVVPTC),
416};
417
418const size_t riscv_isa_ext_count = ARRAY_SIZE(riscv_isa_ext);
419
420static void riscv_isa_set_ext(const struct riscv_isa_ext_data *ext, unsigned long *bitmap)
421{
422 if (ext->id != RISCV_ISA_EXT_INVALID)
423 __set_bit(ext->id, bitmap);
424
425 for (int i = 0; i < ext->subset_ext_size; i++) {
426 if (ext->subset_ext_ids[i] != RISCV_ISA_EXT_INVALID)
427 __set_bit(ext->subset_ext_ids[i], bitmap);
428 }
429}
430
431static void match_isa_ext(const char *name, const char *name_end)
432{
433 for (int i = 0; i < riscv_isa_ext_count; i++) {
434 const struct riscv_isa_ext_data *ext = &riscv_isa_ext[i];
435
436 if ((name_end - name == strlen(ext->name)) &&
437 !strncasecmp(name, ext->name, name_end - name)) {
438 if (ext->validate && !ext->validate(ext, riscv_isa))
439 riscv_isa_set_ext(ext, riscv_isa);
440 break;
441 }
442 }
443}
444
445static void riscv_parse_isa_string(const char *isa)
446{
447 /*
448 * For all possible cpus, we have already validated in
449 * the boot process that they at least contain "rv" and
450 * whichever of "32"/"64" this kernel supports, and so this
451 * section can be skipped.
452 */
453 isa += 4;
454
455 while (*isa) {
456 const char *ext = isa++;
457 const char *ext_end = isa;
458 bool ext_err = false;
459
460 switch (*ext) {
461 case 'x':
462 case 'X':
463 log_warning("Vendor extensions are ignored in riscv,isa. Use riscv,isa-extensions instead.");
464 /*
465 * To skip an extension, we find its end.
466 * As multi-letter extensions must be split from other multi-letter
467 * extensions with an "_", the end of a multi-letter extension will
468 * either be the null character or the "_" at the start of the next
469 * multi-letter extension.
470 */
471 for (; *isa && *isa != '_'; ++isa)
472 ;
473 ext_err = true;
474 break;
475 case 's':
476 /*
477 * Workaround for invalid single-letter 's' & 'u' (QEMU).
478 * No need to set the bit in riscv_isa as 's' & 'u' are
479 * not valid ISA extensions. It works unless the first
480 * multi-letter extension in the ISA string begins with
481 * "Su" and is not prefixed with an underscore.
482 */
483 if (ext[-1] != '_' && ext[1] == 'u') {
484 ++isa;
485 ext_err = true;
486 break;
487 }
488 fallthrough;
489 case 'S':
490 case 'z':
491 case 'Z':
492 /*
493 * Before attempting to parse the extension itself, we find its end.
494 * As multi-letter extensions must be split from other multi-letter
495 * extensions with an "_", the end of a multi-letter extension will
496 * either be the null character or the "_" at the start of the next
497 * multi-letter extension.
498 *
499 * Next, as the extensions version is currently ignored, we
500 * eliminate that portion. This is done by parsing backwards from
501 * the end of the extension, removing any numbers. This may be a
502 * major or minor number however, so the process is repeated if a
503 * minor number was found.
504 *
505 * ext_end is intended to represent the first character *after* the
506 * name portion of an extension, but will be decremented to the last
507 * character itself while eliminating the extensions version number.
508 * A simple re-increment solves this problem.
509 */
510 for (; *isa && *isa != '_'; ++isa)
511 if (unlikely(!isalnum(*isa)))
512 ext_err = true;
513
514 ext_end = isa;
515 if (unlikely(ext_err))
516 break;
517
518 if (!isdigit(ext_end[-1]))
519 break;
520
521 while (isdigit(*--ext_end))
522 ;
523
524 if (tolower(ext_end[0]) != 'p' || !isdigit(ext_end[-1])) {
525 ++ext_end;
526 break;
527 }
528
529 while (isdigit(*--ext_end))
530 ;
531
532 ++ext_end;
533 break;
534 default:
535 /*
536 * Things are a little easier for single-letter extensions, as they
537 * are parsed forwards.
538 *
539 * After checking that our starting position is valid, we need to
540 * ensure that, when isa was incremented at the start of the loop,
541 * that it arrived at the start of the next extension.
542 *
543 * If we are already on a non-digit, there is nothing to do. Either
544 * we have a multi-letter extension's _, or the start of an
545 * extension.
546 *
547 * Otherwise we have found the current extension's major version
548 * number. Parse past it, and a subsequent p/minor version number
549 * if present. The `p` extension must not appear immediately after
550 * a number, so there is no fear of missing it.
551 *
552 */
553 if (unlikely(!isalpha(*ext))) {
554 ext_err = true;
555 break;
556 }
557
558 if (!isdigit(*isa))
559 break;
560
561 while (isdigit(*++isa))
562 ;
563
564 if (tolower(*isa) != 'p')
565 break;
566
567 if (!isdigit(*++isa)) {
568 --isa;
569 break;
570 }
571
572 while (isdigit(*++isa))
573 ;
574
575 break;
576 }
577
578 /*
579 * The parser expects that at the start of an iteration isa points to the
580 * first character of the next extension. As we stop parsing an extension
581 * on meeting a non-alphanumeric character, an extra increment is needed
582 * where the succeeding extension is a multi-letter prefixed with an "_".
583 */
584 if (*isa == '_')
585 ++isa;
586
587 if (unlikely(ext_err))
588 continue;
589 match_isa_ext(ext, ext_end);
590 }
591}
592
Bin Meng055700e2018-09-26 06:55:14 -0700593static inline bool supports_extension(char ext)
594{
Nikita Shubinc9382b12022-12-14 08:58:43 +0300595#if CONFIG_IS_ENABLED(RISCV_MMODE)
596 return csr_read(CSR_MISA) & (1 << (ext - 'a'));
597#elif CONFIG_CPU
Mayuresh Chitaleb7108532025-01-06 13:04:05 +0000598 return __riscv_isa_extension_available(ext);
Bin Mengedfe9a92018-12-12 06:12:38 -0800599#else /* !CONFIG_CPU */
Bin Mengedfe9a92018-12-12 06:12:38 -0800600#warning "There is no way to determine the available extensions in S-mode."
601#warning "Please convert your board to use the RISC-V CPU driver."
602 return false;
Bin Mengedfe9a92018-12-12 06:12:38 -0800603#endif /* CONFIG_CPU */
Bin Meng055700e2018-09-26 06:55:14 -0700604}
605
Tom Rinif4d52f62023-09-04 15:06:34 -0400606static int riscv_cpu_probe(void)
Bin Meng7a3bbfb2018-12-12 06:12:34 -0800607{
608#ifdef CONFIG_CPU
609 int ret;
610
611 /* probe cpus so that RISC-V timer can be bound */
612 ret = cpu_probe_all();
613 if (ret)
614 return log_msg_ret("RISC-V cpus probe failed\n", ret);
615#endif
616
617 return 0;
618}
Tom Rinif4d52f62023-09-04 15:06:34 -0400619EVENT_SPY_SIMPLE(EVT_DM_POST_INIT_R, riscv_cpu_probe);
Bin Meng7a3bbfb2018-12-12 06:12:34 -0800620
Sean Andersondd1cd702020-09-21 07:51:38 -0400621/*
622 * This is called on secondary harts just after the IPI is init'd. Currently
623 * there's nothing to do, since we just need to clear any existing IPIs, and
624 * that is handled by the sending of an ipi itself.
625 */
626#if CONFIG_IS_ENABLED(SMP)
627static void dummy_pending_ipi_clear(ulong hart, ulong arg0, ulong arg1)
628{
629}
630#endif
631
Simon Glassb8357c12023-08-21 21:16:56 -0600632int riscv_cpu_setup(void)
Bin Meng7a3bbfb2018-12-12 06:12:34 -0800633{
Mayuresh Chitaleb7108532025-01-06 13:04:05 +0000634 int ret = -ENODEV, ext_count, i;
635 const char *isa, **exts;
636 struct udevice *dev;
637
638 uclass_find_first_device(UCLASS_CPU, &dev);
639 if (!dev) {
640 debug("unable to find the RISC-V cpu device\n");
641 return ret;
642 }
643
644 ext_count = dev_read_string_list(dev, "riscv,isa-extensions", &exts);
645 if (ext_count > 0) {
646 for (i = 0; i < ext_count; i++)
647 match_isa_ext(exts[i], exts[i] + strlen(exts[i]));
648 } else {
649 isa = dev_read_string(dev, "riscv,isa");
650 if (!isa)
651 return ret;
652 riscv_parse_isa_string(isa);
653 }
Bin Menga7544ed2018-12-12 06:12:40 -0800654
655 /* Enable FPU */
656 if (supports_extension('d') || supports_extension('f')) {
657 csr_set(MODE_PREFIX(status), MSTATUS_FS);
Bin Mengf9426362019-07-10 23:43:13 -0700658 csr_write(CSR_FCSR, 0);
Bin Menga7544ed2018-12-12 06:12:40 -0800659 }
660
661 if (CONFIG_IS_ENABLED(RISCV_MMODE)) {
662 /*
663 * Enable perf counters for cycle, time,
664 * and instret counters only
665 */
Nikita Shubinc9382b12022-12-14 08:58:43 +0300666 if (supports_extension('u')) {
Sean Anderson7f4b6662020-06-24 06:41:19 -0400667#ifdef CONFIG_RISCV_PRIV_1_9
Nikita Shubinc9382b12022-12-14 08:58:43 +0300668 csr_write(CSR_MSCOUNTEREN, GENMASK(2, 0));
669 csr_write(CSR_MUCOUNTEREN, GENMASK(2, 0));
Sean Anderson7f4b6662020-06-24 06:41:19 -0400670#else
Nikita Shubinc9382b12022-12-14 08:58:43 +0300671 csr_write(CSR_MCOUNTEREN, GENMASK(2, 0));
Sean Anderson7f4b6662020-06-24 06:41:19 -0400672#endif
Nikita Shubinc9382b12022-12-14 08:58:43 +0300673 }
Bin Menga7544ed2018-12-12 06:12:40 -0800674
675 /* Disable paging */
676 if (supports_extension('s'))
Sean Anderson7f4b6662020-06-24 06:41:19 -0400677#ifdef CONFIG_RISCV_PRIV_1_9
678 csr_read_clear(CSR_MSTATUS, SR_VM);
679#else
Bin Mengf9426362019-07-10 23:43:13 -0700680 csr_write(CSR_SATP, 0);
Sean Anderson7f4b6662020-06-24 06:41:19 -0400681#endif
Bin Menga7544ed2018-12-12 06:12:40 -0800682 }
683
Bin Meng257875d2020-07-19 23:17:07 -0700684#if CONFIG_IS_ENABLED(SMP)
Sean Andersonb1d0cb32020-06-24 06:41:18 -0400685 ret = riscv_init_ipi();
686 if (ret)
687 return ret;
Sean Andersondd1cd702020-09-21 07:51:38 -0400688
689 /*
690 * Clear all pending IPIs on secondary harts. We don't do anything on
691 * the boot hart, since we never send an IPI to ourselves, and no
692 * interrupts are enabled
693 */
694 ret = smp_call_function((ulong)dummy_pending_ipi_clear, 0, 0, 0);
695 if (ret)
696 return ret;
Sean Andersonb1d0cb32020-06-24 06:41:18 -0400697#endif
698
Bin Menga7544ed2018-12-12 06:12:40 -0800699 return 0;
Bin Meng7a3bbfb2018-12-12 06:12:34 -0800700}
Simon Glassb8357c12023-08-21 21:16:56 -0600701EVENT_SPY_SIMPLE(EVT_DM_POST_INIT_F, riscv_cpu_setup);
Bin Meng7a3bbfb2018-12-12 06:12:34 -0800702
703int arch_early_init_r(void)
704{
Heinrich Schuchardtcc382ff2021-09-12 21:11:46 +0200705 if (IS_ENABLED(CONFIG_SYSRESET_SBI))
706 device_bind_driver(gd->dm_root, "sbi-sysreset",
707 "sbi-sysreset", NULL);
708
709 return 0;
Bin Meng7a3bbfb2018-12-12 06:12:34 -0800710}
Green Wan26120802021-05-02 23:23:04 -0700711
712/**
713 * harts_early_init() - A callback function called by start.S to configure
714 * feature settings of each hart.
715 *
716 * In a multi-core system, memory access shall be careful here, it shall
717 * take care of race conditions.
718 */
719__weak void harts_early_init(void)
720{
721}
Simon Glass34ee3ed2023-12-15 20:14:09 -0700722
723#if !CONFIG_IS_ENABLED(SYSRESET)
724void reset_cpu(void)
725{
726 printf("resetting ...\n");
727
728 printf("reset not supported yet\n");
729 hang();
730}
731#endif