cpufeature.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copied from arch/arm64/kernel/cpufeature.c
  4. *
  5. * Copyright (C) 2015 ARM Ltd.
  6. * Copyright (C) 2017 SiFive
  7. */
  8. #include <linux/acpi.h>
  9. #include <linux/bitmap.h>
  10. #include <linux/cpu.h>
  11. #include <linux/cpuhotplug.h>
  12. #include <linux/ctype.h>
  13. #include <linux/log2.h>
  14. #include <linux/memory.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <asm/acpi.h>
  18. #include <asm/alternative.h>
  19. #include <asm/cacheflush.h>
  20. #include <asm/cpufeature.h>
  21. #include <asm/hwcap.h>
  22. #include <asm/patch.h>
  23. #include <asm/processor.h>
  24. #include <asm/sbi.h>
  25. #include <asm/vector.h>
  26. #include <asm/vendor_extensions.h>
  27. #define NUM_ALPHA_EXTS ('z' - 'a' + 1)
  28. unsigned long elf_hwcap __read_mostly;
  29. /* Host ISA bitmap */
  30. static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly;
  31. /* Per-cpu ISA extensions. */
  32. struct riscv_isainfo hart_isa[NR_CPUS];
  33. /**
  34. * riscv_isa_extension_base() - Get base extension word
  35. *
  36. * @isa_bitmap: ISA bitmap to use
  37. * Return: base extension word as unsigned long value
  38. *
  39. * NOTE: If isa_bitmap is NULL then Host ISA bitmap will be used.
  40. */
  41. unsigned long riscv_isa_extension_base(const unsigned long *isa_bitmap)
  42. {
  43. if (!isa_bitmap)
  44. return riscv_isa[0];
  45. return isa_bitmap[0];
  46. }
  47. EXPORT_SYMBOL_GPL(riscv_isa_extension_base);
  48. /**
  49. * __riscv_isa_extension_available() - Check whether given extension
  50. * is available or not
  51. *
  52. * @isa_bitmap: ISA bitmap to use
  53. * @bit: bit position of the desired extension
  54. * Return: true or false
  55. *
  56. * NOTE: If isa_bitmap is NULL then Host ISA bitmap will be used.
  57. */
  58. bool __riscv_isa_extension_available(const unsigned long *isa_bitmap, unsigned int bit)
  59. {
  60. const unsigned long *bmap = (isa_bitmap) ? isa_bitmap : riscv_isa;
  61. if (bit >= RISCV_ISA_EXT_MAX)
  62. return false;
  63. return test_bit(bit, bmap) ? true : false;
  64. }
  65. EXPORT_SYMBOL_GPL(__riscv_isa_extension_available);
  66. static int riscv_ext_zicbom_validate(const struct riscv_isa_ext_data *data,
  67. const unsigned long *isa_bitmap)
  68. {
  69. if (!riscv_cbom_block_size) {
  70. pr_err("Zicbom detected in ISA string, disabling as no cbom-block-size found\n");
  71. return -EINVAL;
  72. }
  73. if (!is_power_of_2(riscv_cbom_block_size)) {
  74. pr_err("Zicbom disabled as cbom-block-size present, but is not a power-of-2\n");
  75. return -EINVAL;
  76. }
  77. return 0;
  78. }
  79. static int riscv_ext_zicboz_validate(const struct riscv_isa_ext_data *data,
  80. const unsigned long *isa_bitmap)
  81. {
  82. if (!riscv_cboz_block_size) {
  83. pr_err("Zicboz detected in ISA string, disabling as no cboz-block-size found\n");
  84. return -EINVAL;
  85. }
  86. if (!is_power_of_2(riscv_cboz_block_size)) {
  87. pr_err("Zicboz disabled as cboz-block-size present, but is not a power-of-2\n");
  88. return -EINVAL;
  89. }
  90. return 0;
  91. }
  92. static int riscv_ext_zca_depends(const struct riscv_isa_ext_data *data,
  93. const unsigned long *isa_bitmap)
  94. {
  95. if (__riscv_isa_extension_available(isa_bitmap, RISCV_ISA_EXT_ZCA))
  96. return 0;
  97. return -EPROBE_DEFER;
  98. }
  99. static int riscv_ext_zcd_validate(const struct riscv_isa_ext_data *data,
  100. const unsigned long *isa_bitmap)
  101. {
  102. if (__riscv_isa_extension_available(isa_bitmap, RISCV_ISA_EXT_ZCA) &&
  103. __riscv_isa_extension_available(isa_bitmap, RISCV_ISA_EXT_d))
  104. return 0;
  105. return -EPROBE_DEFER;
  106. }
  107. static int riscv_ext_zcf_validate(const struct riscv_isa_ext_data *data,
  108. const unsigned long *isa_bitmap)
  109. {
  110. if (IS_ENABLED(CONFIG_64BIT))
  111. return -EINVAL;
  112. if (__riscv_isa_extension_available(isa_bitmap, RISCV_ISA_EXT_ZCA) &&
  113. __riscv_isa_extension_available(isa_bitmap, RISCV_ISA_EXT_f))
  114. return 0;
  115. return -EPROBE_DEFER;
  116. }
  117. static const unsigned int riscv_zk_bundled_exts[] = {
  118. RISCV_ISA_EXT_ZBKB,
  119. RISCV_ISA_EXT_ZBKC,
  120. RISCV_ISA_EXT_ZBKX,
  121. RISCV_ISA_EXT_ZKND,
  122. RISCV_ISA_EXT_ZKNE,
  123. RISCV_ISA_EXT_ZKR,
  124. RISCV_ISA_EXT_ZKT,
  125. };
  126. static const unsigned int riscv_zkn_bundled_exts[] = {
  127. RISCV_ISA_EXT_ZBKB,
  128. RISCV_ISA_EXT_ZBKC,
  129. RISCV_ISA_EXT_ZBKX,
  130. RISCV_ISA_EXT_ZKND,
  131. RISCV_ISA_EXT_ZKNE,
  132. RISCV_ISA_EXT_ZKNH,
  133. };
  134. static const unsigned int riscv_zks_bundled_exts[] = {
  135. RISCV_ISA_EXT_ZBKB,
  136. RISCV_ISA_EXT_ZBKC,
  137. RISCV_ISA_EXT_ZKSED,
  138. RISCV_ISA_EXT_ZKSH
  139. };
  140. #define RISCV_ISA_EXT_ZVKN \
  141. RISCV_ISA_EXT_ZVKNED, \
  142. RISCV_ISA_EXT_ZVKNHB, \
  143. RISCV_ISA_EXT_ZVKB, \
  144. RISCV_ISA_EXT_ZVKT
  145. static const unsigned int riscv_zvkn_bundled_exts[] = {
  146. RISCV_ISA_EXT_ZVKN
  147. };
  148. static const unsigned int riscv_zvknc_bundled_exts[] = {
  149. RISCV_ISA_EXT_ZVKN,
  150. RISCV_ISA_EXT_ZVBC
  151. };
  152. static const unsigned int riscv_zvkng_bundled_exts[] = {
  153. RISCV_ISA_EXT_ZVKN,
  154. RISCV_ISA_EXT_ZVKG
  155. };
  156. #define RISCV_ISA_EXT_ZVKS \
  157. RISCV_ISA_EXT_ZVKSED, \
  158. RISCV_ISA_EXT_ZVKSH, \
  159. RISCV_ISA_EXT_ZVKB, \
  160. RISCV_ISA_EXT_ZVKT
  161. static const unsigned int riscv_zvks_bundled_exts[] = {
  162. RISCV_ISA_EXT_ZVKS
  163. };
  164. static const unsigned int riscv_zvksc_bundled_exts[] = {
  165. RISCV_ISA_EXT_ZVKS,
  166. RISCV_ISA_EXT_ZVBC
  167. };
  168. static const unsigned int riscv_zvksg_bundled_exts[] = {
  169. RISCV_ISA_EXT_ZVKS,
  170. RISCV_ISA_EXT_ZVKG
  171. };
  172. static const unsigned int riscv_zvbb_exts[] = {
  173. RISCV_ISA_EXT_ZVKB
  174. };
  175. #define RISCV_ISA_EXT_ZVE64F_IMPLY_LIST \
  176. RISCV_ISA_EXT_ZVE64X, \
  177. RISCV_ISA_EXT_ZVE32F, \
  178. RISCV_ISA_EXT_ZVE32X
  179. #define RISCV_ISA_EXT_ZVE64D_IMPLY_LIST \
  180. RISCV_ISA_EXT_ZVE64F, \
  181. RISCV_ISA_EXT_ZVE64F_IMPLY_LIST
  182. #define RISCV_ISA_EXT_V_IMPLY_LIST \
  183. RISCV_ISA_EXT_ZVE64D, \
  184. RISCV_ISA_EXT_ZVE64D_IMPLY_LIST
  185. static const unsigned int riscv_zve32f_exts[] = {
  186. RISCV_ISA_EXT_ZVE32X
  187. };
  188. static const unsigned int riscv_zve64f_exts[] = {
  189. RISCV_ISA_EXT_ZVE64F_IMPLY_LIST
  190. };
  191. static const unsigned int riscv_zve64d_exts[] = {
  192. RISCV_ISA_EXT_ZVE64D_IMPLY_LIST
  193. };
  194. static const unsigned int riscv_v_exts[] = {
  195. RISCV_ISA_EXT_V_IMPLY_LIST
  196. };
  197. static const unsigned int riscv_zve64x_exts[] = {
  198. RISCV_ISA_EXT_ZVE32X,
  199. RISCV_ISA_EXT_ZVE64X
  200. };
  201. /*
  202. * While the [ms]envcfg CSRs were not defined until version 1.12 of the RISC-V
  203. * privileged ISA, the existence of the CSRs is implied by any extension which
  204. * specifies [ms]envcfg bit(s). Hence, we define a custom ISA extension for the
  205. * existence of the CSR, and treat it as a subset of those other extensions.
  206. */
  207. static const unsigned int riscv_xlinuxenvcfg_exts[] = {
  208. RISCV_ISA_EXT_XLINUXENVCFG
  209. };
  210. /*
  211. * Zc* spec states that:
  212. * - C always implies Zca
  213. * - C+F implies Zcf (RV32 only)
  214. * - C+D implies Zcd
  215. *
  216. * These extensions will be enabled and then validated depending on the
  217. * availability of F/D RV32.
  218. */
  219. static const unsigned int riscv_c_exts[] = {
  220. RISCV_ISA_EXT_ZCA,
  221. RISCV_ISA_EXT_ZCF,
  222. RISCV_ISA_EXT_ZCD,
  223. };
  224. /*
  225. * The canonical order of ISA extension names in the ISA string is defined in
  226. * chapter 27 of the unprivileged specification.
  227. *
  228. * Ordinarily, for in-kernel data structures, this order is unimportant but
  229. * isa_ext_arr defines the order of the ISA string in /proc/cpuinfo.
  230. *
  231. * The specification uses vague wording, such as should, when it comes to
  232. * ordering, so for our purposes the following rules apply:
  233. *
  234. * 1. All multi-letter extensions must be separated from other extensions by an
  235. * underscore.
  236. *
  237. * 2. Additional standard extensions (starting with 'Z') must be sorted after
  238. * single-letter extensions and before any higher-privileged extensions.
  239. *
  240. * 3. The first letter following the 'Z' conventionally indicates the most
  241. * closely related alphabetical extension category, IMAFDQLCBKJTPVH.
  242. * If multiple 'Z' extensions are named, they must be ordered first by
  243. * category, then alphabetically within a category.
  244. *
  245. * 3. Standard supervisor-level extensions (starting with 'S') must be listed
  246. * after standard unprivileged extensions. If multiple supervisor-level
  247. * extensions are listed, they must be ordered alphabetically.
  248. *
  249. * 4. Standard machine-level extensions (starting with 'Zxm') must be listed
  250. * after any lower-privileged, standard extensions. If multiple
  251. * machine-level extensions are listed, they must be ordered
  252. * alphabetically.
  253. *
  254. * 5. Non-standard extensions (starting with 'X') must be listed after all
  255. * standard extensions. If multiple non-standard extensions are listed, they
  256. * must be ordered alphabetically.
  257. *
  258. * An example string following the order is:
  259. * rv64imadc_zifoo_zigoo_zafoo_sbar_scar_zxmbaz_xqux_xrux
  260. *
  261. * New entries to this struct should follow the ordering rules described above.
  262. */
  263. const struct riscv_isa_ext_data riscv_isa_ext[] = {
  264. __RISCV_ISA_EXT_DATA(i, RISCV_ISA_EXT_i),
  265. __RISCV_ISA_EXT_DATA(m, RISCV_ISA_EXT_m),
  266. __RISCV_ISA_EXT_DATA(a, RISCV_ISA_EXT_a),
  267. __RISCV_ISA_EXT_DATA(f, RISCV_ISA_EXT_f),
  268. __RISCV_ISA_EXT_DATA(d, RISCV_ISA_EXT_d),
  269. __RISCV_ISA_EXT_DATA(q, RISCV_ISA_EXT_q),
  270. __RISCV_ISA_EXT_SUPERSET(c, RISCV_ISA_EXT_c, riscv_c_exts),
  271. __RISCV_ISA_EXT_SUPERSET(v, RISCV_ISA_EXT_v, riscv_v_exts),
  272. __RISCV_ISA_EXT_DATA(h, RISCV_ISA_EXT_h),
  273. __RISCV_ISA_EXT_SUPERSET_VALIDATE(zicbom, RISCV_ISA_EXT_ZICBOM, riscv_xlinuxenvcfg_exts,
  274. riscv_ext_zicbom_validate),
  275. __RISCV_ISA_EXT_SUPERSET_VALIDATE(zicboz, RISCV_ISA_EXT_ZICBOZ, riscv_xlinuxenvcfg_exts,
  276. riscv_ext_zicboz_validate),
  277. __RISCV_ISA_EXT_DATA(zicntr, RISCV_ISA_EXT_ZICNTR),
  278. __RISCV_ISA_EXT_DATA(zicond, RISCV_ISA_EXT_ZICOND),
  279. __RISCV_ISA_EXT_DATA(zicsr, RISCV_ISA_EXT_ZICSR),
  280. __RISCV_ISA_EXT_DATA(zifencei, RISCV_ISA_EXT_ZIFENCEI),
  281. __RISCV_ISA_EXT_DATA(zihintntl, RISCV_ISA_EXT_ZIHINTNTL),
  282. __RISCV_ISA_EXT_DATA(zihintpause, RISCV_ISA_EXT_ZIHINTPAUSE),
  283. __RISCV_ISA_EXT_DATA(zihpm, RISCV_ISA_EXT_ZIHPM),
  284. __RISCV_ISA_EXT_DATA(zimop, RISCV_ISA_EXT_ZIMOP),
  285. __RISCV_ISA_EXT_DATA(zacas, RISCV_ISA_EXT_ZACAS),
  286. __RISCV_ISA_EXT_DATA(zawrs, RISCV_ISA_EXT_ZAWRS),
  287. __RISCV_ISA_EXT_DATA(zfa, RISCV_ISA_EXT_ZFA),
  288. __RISCV_ISA_EXT_DATA(zfh, RISCV_ISA_EXT_ZFH),
  289. __RISCV_ISA_EXT_DATA(zfhmin, RISCV_ISA_EXT_ZFHMIN),
  290. __RISCV_ISA_EXT_DATA(zca, RISCV_ISA_EXT_ZCA),
  291. __RISCV_ISA_EXT_DATA_VALIDATE(zcb, RISCV_ISA_EXT_ZCB, riscv_ext_zca_depends),
  292. __RISCV_ISA_EXT_DATA_VALIDATE(zcd, RISCV_ISA_EXT_ZCD, riscv_ext_zcd_validate),
  293. __RISCV_ISA_EXT_DATA_VALIDATE(zcf, RISCV_ISA_EXT_ZCF, riscv_ext_zcf_validate),
  294. __RISCV_ISA_EXT_DATA_VALIDATE(zcmop, RISCV_ISA_EXT_ZCMOP, riscv_ext_zca_depends),
  295. __RISCV_ISA_EXT_DATA(zba, RISCV_ISA_EXT_ZBA),
  296. __RISCV_ISA_EXT_DATA(zbb, RISCV_ISA_EXT_ZBB),
  297. __RISCV_ISA_EXT_DATA(zbc, RISCV_ISA_EXT_ZBC),
  298. __RISCV_ISA_EXT_DATA(zbkb, RISCV_ISA_EXT_ZBKB),
  299. __RISCV_ISA_EXT_DATA(zbkc, RISCV_ISA_EXT_ZBKC),
  300. __RISCV_ISA_EXT_DATA(zbkx, RISCV_ISA_EXT_ZBKX),
  301. __RISCV_ISA_EXT_DATA(zbs, RISCV_ISA_EXT_ZBS),
  302. __RISCV_ISA_EXT_BUNDLE(zk, riscv_zk_bundled_exts),
  303. __RISCV_ISA_EXT_BUNDLE(zkn, riscv_zkn_bundled_exts),
  304. __RISCV_ISA_EXT_DATA(zknd, RISCV_ISA_EXT_ZKND),
  305. __RISCV_ISA_EXT_DATA(zkne, RISCV_ISA_EXT_ZKNE),
  306. __RISCV_ISA_EXT_DATA(zknh, RISCV_ISA_EXT_ZKNH),
  307. __RISCV_ISA_EXT_DATA(zkr, RISCV_ISA_EXT_ZKR),
  308. __RISCV_ISA_EXT_BUNDLE(zks, riscv_zks_bundled_exts),
  309. __RISCV_ISA_EXT_DATA(zkt, RISCV_ISA_EXT_ZKT),
  310. __RISCV_ISA_EXT_DATA(zksed, RISCV_ISA_EXT_ZKSED),
  311. __RISCV_ISA_EXT_DATA(zksh, RISCV_ISA_EXT_ZKSH),
  312. __RISCV_ISA_EXT_DATA(ztso, RISCV_ISA_EXT_ZTSO),
  313. __RISCV_ISA_EXT_SUPERSET(zvbb, RISCV_ISA_EXT_ZVBB, riscv_zvbb_exts),
  314. __RISCV_ISA_EXT_DATA(zvbc, RISCV_ISA_EXT_ZVBC),
  315. __RISCV_ISA_EXT_SUPERSET(zve32f, RISCV_ISA_EXT_ZVE32F, riscv_zve32f_exts),
  316. __RISCV_ISA_EXT_DATA(zve32x, RISCV_ISA_EXT_ZVE32X),
  317. __RISCV_ISA_EXT_SUPERSET(zve64d, RISCV_ISA_EXT_ZVE64D, riscv_zve64d_exts),
  318. __RISCV_ISA_EXT_SUPERSET(zve64f, RISCV_ISA_EXT_ZVE64F, riscv_zve64f_exts),
  319. __RISCV_ISA_EXT_SUPERSET(zve64x, RISCV_ISA_EXT_ZVE64X, riscv_zve64x_exts),
  320. __RISCV_ISA_EXT_DATA(zvfh, RISCV_ISA_EXT_ZVFH),
  321. __RISCV_ISA_EXT_DATA(zvfhmin, RISCV_ISA_EXT_ZVFHMIN),
  322. __RISCV_ISA_EXT_DATA(zvkb, RISCV_ISA_EXT_ZVKB),
  323. __RISCV_ISA_EXT_DATA(zvkg, RISCV_ISA_EXT_ZVKG),
  324. __RISCV_ISA_EXT_BUNDLE(zvkn, riscv_zvkn_bundled_exts),
  325. __RISCV_ISA_EXT_BUNDLE(zvknc, riscv_zvknc_bundled_exts),
  326. __RISCV_ISA_EXT_DATA(zvkned, RISCV_ISA_EXT_ZVKNED),
  327. __RISCV_ISA_EXT_BUNDLE(zvkng, riscv_zvkng_bundled_exts),
  328. __RISCV_ISA_EXT_DATA(zvknha, RISCV_ISA_EXT_ZVKNHA),
  329. __RISCV_ISA_EXT_DATA(zvknhb, RISCV_ISA_EXT_ZVKNHB),
  330. __RISCV_ISA_EXT_BUNDLE(zvks, riscv_zvks_bundled_exts),
  331. __RISCV_ISA_EXT_BUNDLE(zvksc, riscv_zvksc_bundled_exts),
  332. __RISCV_ISA_EXT_DATA(zvksed, RISCV_ISA_EXT_ZVKSED),
  333. __RISCV_ISA_EXT_DATA(zvksh, RISCV_ISA_EXT_ZVKSH),
  334. __RISCV_ISA_EXT_BUNDLE(zvksg, riscv_zvksg_bundled_exts),
  335. __RISCV_ISA_EXT_DATA(zvkt, RISCV_ISA_EXT_ZVKT),
  336. __RISCV_ISA_EXT_DATA(smaia, RISCV_ISA_EXT_SMAIA),
  337. __RISCV_ISA_EXT_DATA(smstateen, RISCV_ISA_EXT_SMSTATEEN),
  338. __RISCV_ISA_EXT_DATA(ssaia, RISCV_ISA_EXT_SSAIA),
  339. __RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF),
  340. __RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),
  341. __RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),
  342. __RISCV_ISA_EXT_DATA(svnapot, RISCV_ISA_EXT_SVNAPOT),
  343. __RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),
  344. __RISCV_ISA_EXT_DATA(svvptc, RISCV_ISA_EXT_SVVPTC),
  345. };
  346. const size_t riscv_isa_ext_count = ARRAY_SIZE(riscv_isa_ext);
  347. static void riscv_isa_set_ext(const struct riscv_isa_ext_data *ext, unsigned long *bitmap)
  348. {
  349. if (ext->id != RISCV_ISA_EXT_INVALID)
  350. set_bit(ext->id, bitmap);
  351. for (int i = 0; i < ext->subset_ext_size; i++) {
  352. if (ext->subset_ext_ids[i] != RISCV_ISA_EXT_INVALID)
  353. set_bit(ext->subset_ext_ids[i], bitmap);
  354. }
  355. }
  356. static const struct riscv_isa_ext_data *riscv_get_isa_ext_data(unsigned int ext_id)
  357. {
  358. for (int i = 0; i < riscv_isa_ext_count; i++) {
  359. if (riscv_isa_ext[i].id == ext_id)
  360. return &riscv_isa_ext[i];
  361. }
  362. return NULL;
  363. }
  364. /*
  365. * "Resolve" a source ISA bitmap into one that matches kernel configuration as
  366. * well as correct extension dependencies. Some extensions depends on specific
  367. * kernel configuration to be usable (V needs CONFIG_RISCV_ISA_V for instance)
  368. * and this function will actually validate all the extensions provided in
  369. * source_isa into the resolved_isa based on extensions validate() callbacks.
  370. */
  371. static void __init riscv_resolve_isa(unsigned long *source_isa,
  372. unsigned long *resolved_isa, unsigned long *this_hwcap,
  373. unsigned long *isa2hwcap)
  374. {
  375. bool loop;
  376. const struct riscv_isa_ext_data *ext;
  377. DECLARE_BITMAP(prev_resolved_isa, RISCV_ISA_EXT_MAX);
  378. int max_loop_count = riscv_isa_ext_count, ret;
  379. unsigned int bit;
  380. do {
  381. loop = false;
  382. if (max_loop_count-- < 0) {
  383. pr_err("Failed to reach a stable ISA state\n");
  384. return;
  385. }
  386. bitmap_copy(prev_resolved_isa, resolved_isa, RISCV_ISA_EXT_MAX);
  387. for_each_set_bit(bit, source_isa, RISCV_ISA_EXT_MAX) {
  388. ext = riscv_get_isa_ext_data(bit);
  389. if (ext && ext->validate) {
  390. ret = ext->validate(ext, resolved_isa);
  391. if (ret == -EPROBE_DEFER) {
  392. loop = true;
  393. continue;
  394. } else if (ret) {
  395. /* Disable the extension entirely */
  396. clear_bit(bit, source_isa);
  397. continue;
  398. }
  399. }
  400. set_bit(bit, resolved_isa);
  401. /* No need to keep it in source isa now that it is enabled */
  402. clear_bit(bit, source_isa);
  403. /* Single letter extensions get set in hwcap */
  404. if (bit < RISCV_ISA_EXT_BASE)
  405. *this_hwcap |= isa2hwcap[bit];
  406. }
  407. } while (loop && memcmp(prev_resolved_isa, resolved_isa, sizeof(prev_resolved_isa)));
  408. }
  409. static void __init match_isa_ext(const char *name, const char *name_end, unsigned long *bitmap)
  410. {
  411. for (int i = 0; i < riscv_isa_ext_count; i++) {
  412. const struct riscv_isa_ext_data *ext = &riscv_isa_ext[i];
  413. if ((name_end - name == strlen(ext->name)) &&
  414. !strncasecmp(name, ext->name, name_end - name)) {
  415. riscv_isa_set_ext(ext, bitmap);
  416. break;
  417. }
  418. }
  419. }
  420. static void __init riscv_parse_isa_string(const char *isa, unsigned long *bitmap)
  421. {
  422. /*
  423. * For all possible cpus, we have already validated in
  424. * the boot process that they at least contain "rv" and
  425. * whichever of "32"/"64" this kernel supports, and so this
  426. * section can be skipped.
  427. */
  428. isa += 4;
  429. while (*isa) {
  430. const char *ext = isa++;
  431. const char *ext_end = isa;
  432. bool ext_err = false;
  433. switch (*ext) {
  434. case 'x':
  435. case 'X':
  436. if (acpi_disabled)
  437. pr_warn_once("Vendor extensions are ignored in riscv,isa. Use riscv,isa-extensions instead.");
  438. /*
  439. * To skip an extension, we find its end.
  440. * As multi-letter extensions must be split from other multi-letter
  441. * extensions with an "_", the end of a multi-letter extension will
  442. * either be the null character or the "_" at the start of the next
  443. * multi-letter extension.
  444. */
  445. for (; *isa && *isa != '_'; ++isa)
  446. ;
  447. ext_err = true;
  448. break;
  449. case 's':
  450. /*
  451. * Workaround for invalid single-letter 's' & 'u' (QEMU).
  452. * No need to set the bit in riscv_isa as 's' & 'u' are
  453. * not valid ISA extensions. It works unless the first
  454. * multi-letter extension in the ISA string begins with
  455. * "Su" and is not prefixed with an underscore.
  456. */
  457. if (ext[-1] != '_' && ext[1] == 'u') {
  458. ++isa;
  459. ext_err = true;
  460. break;
  461. }
  462. fallthrough;
  463. case 'S':
  464. case 'z':
  465. case 'Z':
  466. /*
  467. * Before attempting to parse the extension itself, we find its end.
  468. * As multi-letter extensions must be split from other multi-letter
  469. * extensions with an "_", the end of a multi-letter extension will
  470. * either be the null character or the "_" at the start of the next
  471. * multi-letter extension.
  472. *
  473. * Next, as the extensions version is currently ignored, we
  474. * eliminate that portion. This is done by parsing backwards from
  475. * the end of the extension, removing any numbers. This may be a
  476. * major or minor number however, so the process is repeated if a
  477. * minor number was found.
  478. *
  479. * ext_end is intended to represent the first character *after* the
  480. * name portion of an extension, but will be decremented to the last
  481. * character itself while eliminating the extensions version number.
  482. * A simple re-increment solves this problem.
  483. */
  484. for (; *isa && *isa != '_'; ++isa)
  485. if (unlikely(!isalnum(*isa)))
  486. ext_err = true;
  487. ext_end = isa;
  488. if (unlikely(ext_err))
  489. break;
  490. if (!isdigit(ext_end[-1]))
  491. break;
  492. while (isdigit(*--ext_end))
  493. ;
  494. if (tolower(ext_end[0]) != 'p' || !isdigit(ext_end[-1])) {
  495. ++ext_end;
  496. break;
  497. }
  498. while (isdigit(*--ext_end))
  499. ;
  500. ++ext_end;
  501. break;
  502. default:
  503. /*
  504. * Things are a little easier for single-letter extensions, as they
  505. * are parsed forwards.
  506. *
  507. * After checking that our starting position is valid, we need to
  508. * ensure that, when isa was incremented at the start of the loop,
  509. * that it arrived at the start of the next extension.
  510. *
  511. * If we are already on a non-digit, there is nothing to do. Either
  512. * we have a multi-letter extension's _, or the start of an
  513. * extension.
  514. *
  515. * Otherwise we have found the current extension's major version
  516. * number. Parse past it, and a subsequent p/minor version number
  517. * if present. The `p` extension must not appear immediately after
  518. * a number, so there is no fear of missing it.
  519. *
  520. */
  521. if (unlikely(!isalpha(*ext))) {
  522. ext_err = true;
  523. break;
  524. }
  525. if (!isdigit(*isa))
  526. break;
  527. while (isdigit(*++isa))
  528. ;
  529. if (tolower(*isa) != 'p')
  530. break;
  531. if (!isdigit(*++isa)) {
  532. --isa;
  533. break;
  534. }
  535. while (isdigit(*++isa))
  536. ;
  537. break;
  538. }
  539. /*
  540. * The parser expects that at the start of an iteration isa points to the
  541. * first character of the next extension. As we stop parsing an extension
  542. * on meeting a non-alphanumeric character, an extra increment is needed
  543. * where the succeeding extension is a multi-letter prefixed with an "_".
  544. */
  545. if (*isa == '_')
  546. ++isa;
  547. if (unlikely(ext_err))
  548. continue;
  549. match_isa_ext(ext, ext_end, bitmap);
  550. }
  551. }
  552. static void __init riscv_fill_hwcap_from_isa_string(unsigned long *isa2hwcap)
  553. {
  554. struct device_node *node;
  555. const char *isa;
  556. int rc;
  557. struct acpi_table_header *rhct;
  558. acpi_status status;
  559. unsigned int cpu;
  560. u64 boot_vendorid;
  561. u64 boot_archid;
  562. if (!acpi_disabled) {
  563. status = acpi_get_table(ACPI_SIG_RHCT, 0, &rhct);
  564. if (ACPI_FAILURE(status))
  565. return;
  566. }
  567. boot_vendorid = riscv_get_mvendorid();
  568. boot_archid = riscv_get_marchid();
  569. for_each_possible_cpu(cpu) {
  570. struct riscv_isainfo *isainfo = &hart_isa[cpu];
  571. unsigned long this_hwcap = 0;
  572. DECLARE_BITMAP(source_isa, RISCV_ISA_EXT_MAX) = { 0 };
  573. if (acpi_disabled) {
  574. node = of_cpu_device_node_get(cpu);
  575. if (!node) {
  576. pr_warn("Unable to find cpu node\n");
  577. continue;
  578. }
  579. rc = of_property_read_string(node, "riscv,isa", &isa);
  580. of_node_put(node);
  581. if (rc) {
  582. pr_warn("Unable to find \"riscv,isa\" devicetree entry\n");
  583. continue;
  584. }
  585. } else {
  586. rc = acpi_get_riscv_isa(rhct, cpu, &isa);
  587. if (rc < 0) {
  588. pr_warn("Unable to get ISA for the hart - %d\n", cpu);
  589. continue;
  590. }
  591. }
  592. riscv_parse_isa_string(isa, source_isa);
  593. /*
  594. * These ones were as they were part of the base ISA when the
  595. * port & dt-bindings were upstreamed, and so can be set
  596. * unconditionally where `i` is in riscv,isa on DT systems.
  597. */
  598. if (acpi_disabled) {
  599. set_bit(RISCV_ISA_EXT_ZICSR, source_isa);
  600. set_bit(RISCV_ISA_EXT_ZIFENCEI, source_isa);
  601. set_bit(RISCV_ISA_EXT_ZICNTR, source_isa);
  602. set_bit(RISCV_ISA_EXT_ZIHPM, source_isa);
  603. }
  604. /*
  605. * "V" in ISA strings is ambiguous in practice: it should mean
  606. * just the standard V-1.0 but vendors aren't well behaved.
  607. * Many vendors with T-Head CPU cores which implement the 0.7.1
  608. * version of the vector specification put "v" into their DTs.
  609. * CPU cores with the ratified spec will contain non-zero
  610. * marchid.
  611. */
  612. if (acpi_disabled && boot_vendorid == THEAD_VENDOR_ID && boot_archid == 0x0) {
  613. this_hwcap &= ~isa2hwcap[RISCV_ISA_EXT_v];
  614. clear_bit(RISCV_ISA_EXT_v, source_isa);
  615. }
  616. riscv_resolve_isa(source_isa, isainfo->isa, &this_hwcap, isa2hwcap);
  617. /*
  618. * All "okay" hart should have same isa. Set HWCAP based on
  619. * common capabilities of every "okay" hart, in case they don't
  620. * have.
  621. */
  622. if (elf_hwcap)
  623. elf_hwcap &= this_hwcap;
  624. else
  625. elf_hwcap = this_hwcap;
  626. if (bitmap_empty(riscv_isa, RISCV_ISA_EXT_MAX))
  627. bitmap_copy(riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX);
  628. else
  629. bitmap_and(riscv_isa, riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX);
  630. }
  631. if (!acpi_disabled && rhct)
  632. acpi_put_table((struct acpi_table_header *)rhct);
  633. }
  634. static void __init riscv_fill_cpu_vendor_ext(struct device_node *cpu_node, int cpu)
  635. {
  636. if (!IS_ENABLED(CONFIG_RISCV_ISA_VENDOR_EXT))
  637. return;
  638. for (int i = 0; i < riscv_isa_vendor_ext_list_size; i++) {
  639. struct riscv_isa_vendor_ext_data_list *ext_list = riscv_isa_vendor_ext_list[i];
  640. for (int j = 0; j < ext_list->ext_data_count; j++) {
  641. const struct riscv_isa_ext_data ext = ext_list->ext_data[j];
  642. struct riscv_isavendorinfo *isavendorinfo = &ext_list->per_hart_isa_bitmap[cpu];
  643. if (of_property_match_string(cpu_node, "riscv,isa-extensions",
  644. ext.property) < 0)
  645. continue;
  646. /*
  647. * Assume that subset extensions are all members of the
  648. * same vendor.
  649. */
  650. if (ext.subset_ext_size)
  651. for (int k = 0; k < ext.subset_ext_size; k++)
  652. set_bit(ext.subset_ext_ids[k], isavendorinfo->isa);
  653. set_bit(ext.id, isavendorinfo->isa);
  654. }
  655. }
  656. }
  657. /*
  658. * Populate all_harts_isa_bitmap for each vendor with all of the extensions that
  659. * are shared across CPUs for that vendor.
  660. */
  661. static void __init riscv_fill_vendor_ext_list(int cpu)
  662. {
  663. if (!IS_ENABLED(CONFIG_RISCV_ISA_VENDOR_EXT))
  664. return;
  665. for (int i = 0; i < riscv_isa_vendor_ext_list_size; i++) {
  666. struct riscv_isa_vendor_ext_data_list *ext_list = riscv_isa_vendor_ext_list[i];
  667. if (!ext_list->is_initialized) {
  668. bitmap_copy(ext_list->all_harts_isa_bitmap.isa,
  669. ext_list->per_hart_isa_bitmap[cpu].isa,
  670. RISCV_ISA_VENDOR_EXT_MAX);
  671. ext_list->is_initialized = true;
  672. } else {
  673. bitmap_and(ext_list->all_harts_isa_bitmap.isa,
  674. ext_list->all_harts_isa_bitmap.isa,
  675. ext_list->per_hart_isa_bitmap[cpu].isa,
  676. RISCV_ISA_VENDOR_EXT_MAX);
  677. }
  678. }
  679. }
  680. static int __init riscv_fill_hwcap_from_ext_list(unsigned long *isa2hwcap)
  681. {
  682. unsigned int cpu;
  683. for_each_possible_cpu(cpu) {
  684. unsigned long this_hwcap = 0;
  685. struct device_node *cpu_node;
  686. struct riscv_isainfo *isainfo = &hart_isa[cpu];
  687. DECLARE_BITMAP(source_isa, RISCV_ISA_EXT_MAX) = { 0 };
  688. cpu_node = of_cpu_device_node_get(cpu);
  689. if (!cpu_node) {
  690. pr_warn("Unable to find cpu node\n");
  691. continue;
  692. }
  693. if (!of_property_present(cpu_node, "riscv,isa-extensions")) {
  694. of_node_put(cpu_node);
  695. continue;
  696. }
  697. for (int i = 0; i < riscv_isa_ext_count; i++) {
  698. const struct riscv_isa_ext_data *ext = &riscv_isa_ext[i];
  699. if (of_property_match_string(cpu_node, "riscv,isa-extensions",
  700. ext->property) < 0)
  701. continue;
  702. riscv_isa_set_ext(ext, source_isa);
  703. }
  704. riscv_resolve_isa(source_isa, isainfo->isa, &this_hwcap, isa2hwcap);
  705. riscv_fill_cpu_vendor_ext(cpu_node, cpu);
  706. of_node_put(cpu_node);
  707. /*
  708. * All "okay" harts should have same isa. Set HWCAP based on
  709. * common capabilities of every "okay" hart, in case they don't.
  710. */
  711. if (elf_hwcap)
  712. elf_hwcap &= this_hwcap;
  713. else
  714. elf_hwcap = this_hwcap;
  715. if (bitmap_empty(riscv_isa, RISCV_ISA_EXT_MAX))
  716. bitmap_copy(riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX);
  717. else
  718. bitmap_and(riscv_isa, riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX);
  719. riscv_fill_vendor_ext_list(cpu);
  720. }
  721. if (bitmap_empty(riscv_isa, RISCV_ISA_EXT_MAX))
  722. return -ENOENT;
  723. return 0;
  724. }
  725. #ifdef CONFIG_RISCV_ISA_FALLBACK
  726. bool __initdata riscv_isa_fallback = true;
  727. #else
  728. bool __initdata riscv_isa_fallback;
  729. static int __init riscv_isa_fallback_setup(char *__unused)
  730. {
  731. riscv_isa_fallback = true;
  732. return 1;
  733. }
  734. early_param("riscv_isa_fallback", riscv_isa_fallback_setup);
  735. #endif
  736. void __init riscv_fill_hwcap(void)
  737. {
  738. char print_str[NUM_ALPHA_EXTS + 1];
  739. unsigned long isa2hwcap[26] = {0};
  740. int i, j;
  741. isa2hwcap['i' - 'a'] = COMPAT_HWCAP_ISA_I;
  742. isa2hwcap['m' - 'a'] = COMPAT_HWCAP_ISA_M;
  743. isa2hwcap['a' - 'a'] = COMPAT_HWCAP_ISA_A;
  744. isa2hwcap['f' - 'a'] = COMPAT_HWCAP_ISA_F;
  745. isa2hwcap['d' - 'a'] = COMPAT_HWCAP_ISA_D;
  746. isa2hwcap['c' - 'a'] = COMPAT_HWCAP_ISA_C;
  747. isa2hwcap['v' - 'a'] = COMPAT_HWCAP_ISA_V;
  748. if (!acpi_disabled) {
  749. riscv_fill_hwcap_from_isa_string(isa2hwcap);
  750. } else {
  751. int ret = riscv_fill_hwcap_from_ext_list(isa2hwcap);
  752. if (ret && riscv_isa_fallback) {
  753. pr_info("Falling back to deprecated \"riscv,isa\"\n");
  754. riscv_fill_hwcap_from_isa_string(isa2hwcap);
  755. }
  756. }
  757. /*
  758. * We don't support systems with F but without D, so mask those out
  759. * here.
  760. */
  761. if ((elf_hwcap & COMPAT_HWCAP_ISA_F) && !(elf_hwcap & COMPAT_HWCAP_ISA_D)) {
  762. pr_info("This kernel does not support systems with F but not D\n");
  763. elf_hwcap &= ~COMPAT_HWCAP_ISA_F;
  764. }
  765. if (__riscv_isa_extension_available(NULL, RISCV_ISA_EXT_ZVE32X)) {
  766. /*
  767. * This cannot fail when called on the boot hart
  768. */
  769. riscv_v_setup_vsize();
  770. }
  771. if (elf_hwcap & COMPAT_HWCAP_ISA_V) {
  772. /*
  773. * ISA string in device tree might have 'v' flag, but
  774. * CONFIG_RISCV_ISA_V is disabled in kernel.
  775. * Clear V flag in elf_hwcap if CONFIG_RISCV_ISA_V is disabled.
  776. */
  777. if (!IS_ENABLED(CONFIG_RISCV_ISA_V))
  778. elf_hwcap &= ~COMPAT_HWCAP_ISA_V;
  779. }
  780. memset(print_str, 0, sizeof(print_str));
  781. for (i = 0, j = 0; i < NUM_ALPHA_EXTS; i++)
  782. if (riscv_isa[0] & BIT_MASK(i))
  783. print_str[j++] = (char)('a' + i);
  784. pr_info("riscv: base ISA extensions %s\n", print_str);
  785. memset(print_str, 0, sizeof(print_str));
  786. for (i = 0, j = 0; i < NUM_ALPHA_EXTS; i++)
  787. if (elf_hwcap & BIT_MASK(i))
  788. print_str[j++] = (char)('a' + i);
  789. pr_info("riscv: ELF capabilities %s\n", print_str);
  790. }
  791. unsigned long riscv_get_elf_hwcap(void)
  792. {
  793. unsigned long hwcap;
  794. hwcap = (elf_hwcap & ((1UL << RISCV_ISA_EXT_BASE) - 1));
  795. if (!riscv_v_vstate_ctrl_user_allowed())
  796. hwcap &= ~COMPAT_HWCAP_ISA_V;
  797. return hwcap;
  798. }
  799. void riscv_user_isa_enable(void)
  800. {
  801. if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_ZICBOZ))
  802. csr_set(CSR_ENVCFG, ENVCFG_CBZE);
  803. }
  804. #ifdef CONFIG_RISCV_ALTERNATIVE
  805. /*
  806. * Alternative patch sites consider 48 bits when determining when to patch
  807. * the old instruction sequence with the new. These bits are broken into a
  808. * 16-bit vendor ID and a 32-bit patch ID. A non-zero vendor ID means the
  809. * patch site is for an erratum, identified by the 32-bit patch ID. When
  810. * the vendor ID is zero, the patch site is for a cpufeature. cpufeatures
  811. * further break down patch ID into two 16-bit numbers. The lower 16 bits
  812. * are the cpufeature ID and the upper 16 bits are used for a value specific
  813. * to the cpufeature and patch site. If the upper 16 bits are zero, then it
  814. * implies no specific value is specified. cpufeatures that want to control
  815. * patching on a per-site basis will provide non-zero values and implement
  816. * checks here. The checks return true when patching should be done, and
  817. * false otherwise.
  818. */
  819. static bool riscv_cpufeature_patch_check(u16 id, u16 value)
  820. {
  821. if (!value)
  822. return true;
  823. switch (id) {
  824. case RISCV_ISA_EXT_ZICBOZ:
  825. /*
  826. * Zicboz alternative applications provide the maximum
  827. * supported block size order, or zero when it doesn't
  828. * matter. If the current block size exceeds the maximum,
  829. * then the alternative cannot be applied.
  830. */
  831. return riscv_cboz_block_size <= (1U << value);
  832. }
  833. return false;
  834. }
  835. void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,
  836. struct alt_entry *end,
  837. unsigned int stage)
  838. {
  839. struct alt_entry *alt;
  840. void *oldptr, *altptr;
  841. u16 id, value, vendor;
  842. if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
  843. return;
  844. for (alt = begin; alt < end; alt++) {
  845. id = PATCH_ID_CPUFEATURE_ID(alt->patch_id);
  846. vendor = PATCH_ID_CPUFEATURE_ID(alt->vendor_id);
  847. /*
  848. * Any alternative with a patch_id that is less than
  849. * RISCV_ISA_EXT_MAX is interpreted as a standard extension.
  850. *
  851. * Any alternative with patch_id that is greater than or equal
  852. * to RISCV_VENDOR_EXT_ALTERNATIVES_BASE is interpreted as a
  853. * vendor extension.
  854. */
  855. if (id < RISCV_ISA_EXT_MAX) {
  856. /*
  857. * This patch should be treated as errata so skip
  858. * processing here.
  859. */
  860. if (alt->vendor_id != 0)
  861. continue;
  862. if (!__riscv_isa_extension_available(NULL, id))
  863. continue;
  864. value = PATCH_ID_CPUFEATURE_VALUE(alt->patch_id);
  865. if (!riscv_cpufeature_patch_check(id, value))
  866. continue;
  867. } else if (id >= RISCV_VENDOR_EXT_ALTERNATIVES_BASE) {
  868. if (!__riscv_isa_vendor_extension_available(VENDOR_EXT_ALL_CPUS, vendor,
  869. id - RISCV_VENDOR_EXT_ALTERNATIVES_BASE))
  870. continue;
  871. } else {
  872. WARN(1, "This extension id:%d is not in ISA extension list", id);
  873. continue;
  874. }
  875. oldptr = ALT_OLD_PTR(alt);
  876. altptr = ALT_ALT_PTR(alt);
  877. mutex_lock(&text_mutex);
  878. patch_text_nosync(oldptr, altptr, alt->alt_len);
  879. riscv_alternative_fix_offsets(oldptr, alt->alt_len, oldptr - altptr);
  880. mutex_unlock(&text_mutex);
  881. }
  882. }
  883. #endif