cpufeature.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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/bitmap.h>
  9. #include <linux/ctype.h>
  10. #include <linux/libfdt.h>
  11. #include <linux/memory.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <asm/alternative.h>
  15. #include <asm/cacheflush.h>
  16. #include <asm/errata_list.h>
  17. #include <asm/hwcap.h>
  18. #include <asm/patch.h>
  19. #include <asm/pgtable.h>
  20. #include <asm/processor.h>
  21. #include <asm/smp.h>
  22. #include <asm/switch_to.h>
  23. #define NUM_ALPHA_EXTS ('z' - 'a' + 1)
  24. unsigned long elf_hwcap __read_mostly;
  25. /* Host ISA bitmap */
  26. static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly;
  27. DEFINE_STATIC_KEY_ARRAY_FALSE(riscv_isa_ext_keys, RISCV_ISA_EXT_KEY_MAX);
  28. EXPORT_SYMBOL(riscv_isa_ext_keys);
  29. /**
  30. * riscv_isa_extension_base() - Get base extension word
  31. *
  32. * @isa_bitmap: ISA bitmap to use
  33. * Return: base extension word as unsigned long value
  34. *
  35. * NOTE: If isa_bitmap is NULL then Host ISA bitmap will be used.
  36. */
  37. unsigned long riscv_isa_extension_base(const unsigned long *isa_bitmap)
  38. {
  39. if (!isa_bitmap)
  40. return riscv_isa[0];
  41. return isa_bitmap[0];
  42. }
  43. EXPORT_SYMBOL_GPL(riscv_isa_extension_base);
  44. /**
  45. * __riscv_isa_extension_available() - Check whether given extension
  46. * is available or not
  47. *
  48. * @isa_bitmap: ISA bitmap to use
  49. * @bit: bit position of the desired extension
  50. * Return: true or false
  51. *
  52. * NOTE: If isa_bitmap is NULL then Host ISA bitmap will be used.
  53. */
  54. bool __riscv_isa_extension_available(const unsigned long *isa_bitmap, int bit)
  55. {
  56. const unsigned long *bmap = (isa_bitmap) ? isa_bitmap : riscv_isa;
  57. if (bit >= RISCV_ISA_EXT_MAX)
  58. return false;
  59. return test_bit(bit, bmap) ? true : false;
  60. }
  61. EXPORT_SYMBOL_GPL(__riscv_isa_extension_available);
  62. void __init riscv_fill_hwcap(void)
  63. {
  64. struct device_node *node;
  65. const char *isa;
  66. char print_str[NUM_ALPHA_EXTS + 1];
  67. int i, j, rc;
  68. static unsigned long isa2hwcap[256] = {0};
  69. unsigned long hartid;
  70. isa2hwcap['i'] = isa2hwcap['I'] = COMPAT_HWCAP_ISA_I;
  71. isa2hwcap['m'] = isa2hwcap['M'] = COMPAT_HWCAP_ISA_M;
  72. isa2hwcap['a'] = isa2hwcap['A'] = COMPAT_HWCAP_ISA_A;
  73. isa2hwcap['f'] = isa2hwcap['F'] = COMPAT_HWCAP_ISA_F;
  74. isa2hwcap['d'] = isa2hwcap['D'] = COMPAT_HWCAP_ISA_D;
  75. isa2hwcap['c'] = isa2hwcap['C'] = COMPAT_HWCAP_ISA_C;
  76. elf_hwcap = 0;
  77. bitmap_zero(riscv_isa, RISCV_ISA_EXT_MAX);
  78. for_each_of_cpu_node(node) {
  79. unsigned long this_hwcap = 0;
  80. DECLARE_BITMAP(this_isa, RISCV_ISA_EXT_MAX);
  81. const char *temp;
  82. rc = riscv_of_processor_hartid(node, &hartid);
  83. if (rc < 0)
  84. continue;
  85. if (of_property_read_string(node, "riscv,isa", &isa)) {
  86. pr_warn("Unable to find \"riscv,isa\" devicetree entry\n");
  87. continue;
  88. }
  89. temp = isa;
  90. #if IS_ENABLED(CONFIG_32BIT)
  91. if (!strncmp(isa, "rv32", 4))
  92. isa += 4;
  93. #elif IS_ENABLED(CONFIG_64BIT)
  94. if (!strncmp(isa, "rv64", 4))
  95. isa += 4;
  96. #endif
  97. /* The riscv,isa DT property must start with rv64 or rv32 */
  98. if (temp == isa)
  99. continue;
  100. bitmap_zero(this_isa, RISCV_ISA_EXT_MAX);
  101. for (; *isa; ++isa) {
  102. const char *ext = isa++;
  103. const char *ext_end = isa;
  104. bool ext_long = false, ext_err = false;
  105. switch (*ext) {
  106. case 's':
  107. /**
  108. * Workaround for invalid single-letter 's' & 'u'(QEMU).
  109. * No need to set the bit in riscv_isa as 's' & 'u' are
  110. * not valid ISA extensions. It works until multi-letter
  111. * extension starting with "Su" appears.
  112. */
  113. if (ext[-1] != '_' && ext[1] == 'u') {
  114. ++isa;
  115. ext_err = true;
  116. break;
  117. }
  118. fallthrough;
  119. case 'x':
  120. case 'z':
  121. ext_long = true;
  122. /* Multi-letter extension must be delimited */
  123. for (; *isa && *isa != '_'; ++isa)
  124. if (unlikely(!islower(*isa)
  125. && !isdigit(*isa)))
  126. ext_err = true;
  127. /* Parse backwards */
  128. ext_end = isa;
  129. if (unlikely(ext_err))
  130. break;
  131. if (!isdigit(ext_end[-1]))
  132. break;
  133. /* Skip the minor version */
  134. while (isdigit(*--ext_end))
  135. ;
  136. if (ext_end[0] != 'p'
  137. || !isdigit(ext_end[-1])) {
  138. /* Advance it to offset the pre-decrement */
  139. ++ext_end;
  140. break;
  141. }
  142. /* Skip the major version */
  143. while (isdigit(*--ext_end))
  144. ;
  145. ++ext_end;
  146. break;
  147. default:
  148. if (unlikely(!islower(*ext))) {
  149. ext_err = true;
  150. break;
  151. }
  152. /* Find next extension */
  153. if (!isdigit(*isa))
  154. break;
  155. /* Skip the minor version */
  156. while (isdigit(*++isa))
  157. ;
  158. if (*isa != 'p')
  159. break;
  160. if (!isdigit(*++isa)) {
  161. --isa;
  162. break;
  163. }
  164. /* Skip the major version */
  165. while (isdigit(*++isa))
  166. ;
  167. break;
  168. }
  169. if (*isa != '_')
  170. --isa;
  171. #define SET_ISA_EXT_MAP(name, bit) \
  172. do { \
  173. if ((ext_end - ext == sizeof(name) - 1) && \
  174. !memcmp(ext, name, sizeof(name) - 1)) \
  175. set_bit(bit, this_isa); \
  176. } while (false) \
  177. if (unlikely(ext_err))
  178. continue;
  179. if (!ext_long) {
  180. this_hwcap |= isa2hwcap[(unsigned char)(*ext)];
  181. set_bit(*ext - 'a', this_isa);
  182. } else {
  183. SET_ISA_EXT_MAP("sscofpmf", RISCV_ISA_EXT_SSCOFPMF);
  184. SET_ISA_EXT_MAP("svpbmt", RISCV_ISA_EXT_SVPBMT);
  185. SET_ISA_EXT_MAP("zicbom", RISCV_ISA_EXT_ZICBOM);
  186. SET_ISA_EXT_MAP("zihintpause", RISCV_ISA_EXT_ZIHINTPAUSE);
  187. SET_ISA_EXT_MAP("sstc", RISCV_ISA_EXT_SSTC);
  188. SET_ISA_EXT_MAP("svinval", RISCV_ISA_EXT_SVINVAL);
  189. }
  190. #undef SET_ISA_EXT_MAP
  191. }
  192. /*
  193. * All "okay" hart should have same isa. Set HWCAP based on
  194. * common capabilities of every "okay" hart, in case they don't
  195. * have.
  196. */
  197. if (elf_hwcap)
  198. elf_hwcap &= this_hwcap;
  199. else
  200. elf_hwcap = this_hwcap;
  201. if (bitmap_empty(riscv_isa, RISCV_ISA_EXT_MAX))
  202. bitmap_copy(riscv_isa, this_isa, RISCV_ISA_EXT_MAX);
  203. else
  204. bitmap_and(riscv_isa, riscv_isa, this_isa, RISCV_ISA_EXT_MAX);
  205. }
  206. /* We don't support systems with F but without D, so mask those out
  207. * here. */
  208. if ((elf_hwcap & COMPAT_HWCAP_ISA_F) && !(elf_hwcap & COMPAT_HWCAP_ISA_D)) {
  209. pr_info("This kernel does not support systems with F but not D\n");
  210. elf_hwcap &= ~COMPAT_HWCAP_ISA_F;
  211. }
  212. memset(print_str, 0, sizeof(print_str));
  213. for (i = 0, j = 0; i < NUM_ALPHA_EXTS; i++)
  214. if (riscv_isa[0] & BIT_MASK(i))
  215. print_str[j++] = (char)('a' + i);
  216. pr_info("riscv: base ISA extensions %s\n", print_str);
  217. memset(print_str, 0, sizeof(print_str));
  218. for (i = 0, j = 0; i < NUM_ALPHA_EXTS; i++)
  219. if (elf_hwcap & BIT_MASK(i))
  220. print_str[j++] = (char)('a' + i);
  221. pr_info("riscv: ELF capabilities %s\n", print_str);
  222. for_each_set_bit(i, riscv_isa, RISCV_ISA_EXT_MAX) {
  223. j = riscv_isa_ext2key(i);
  224. if (j >= 0)
  225. static_branch_enable(&riscv_isa_ext_keys[j]);
  226. }
  227. }
  228. #ifdef CONFIG_RISCV_ALTERNATIVE
  229. static bool __init_or_module cpufeature_probe_svpbmt(unsigned int stage)
  230. {
  231. if (!IS_ENABLED(CONFIG_RISCV_ISA_SVPBMT))
  232. return false;
  233. if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
  234. return false;
  235. return riscv_isa_extension_available(NULL, SVPBMT);
  236. }
  237. static bool __init_or_module cpufeature_probe_zicbom(unsigned int stage)
  238. {
  239. if (!IS_ENABLED(CONFIG_RISCV_ISA_ZICBOM))
  240. return false;
  241. if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
  242. return false;
  243. if (!riscv_isa_extension_available(NULL, ZICBOM))
  244. return false;
  245. riscv_noncoherent_supported();
  246. return true;
  247. }
  248. /*
  249. * Probe presence of individual extensions.
  250. *
  251. * This code may also be executed before kernel relocation, so we cannot use
  252. * addresses generated by the address-of operator as they won't be valid in
  253. * this context.
  254. */
  255. static u32 __init_or_module cpufeature_probe(unsigned int stage)
  256. {
  257. u32 cpu_req_feature = 0;
  258. if (cpufeature_probe_svpbmt(stage))
  259. cpu_req_feature |= BIT(CPUFEATURE_SVPBMT);
  260. if (cpufeature_probe_zicbom(stage))
  261. cpu_req_feature |= BIT(CPUFEATURE_ZICBOM);
  262. return cpu_req_feature;
  263. }
  264. void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,
  265. struct alt_entry *end,
  266. unsigned int stage)
  267. {
  268. u32 cpu_req_feature = cpufeature_probe(stage);
  269. struct alt_entry *alt;
  270. u32 tmp;
  271. for (alt = begin; alt < end; alt++) {
  272. if (alt->vendor_id != 0)
  273. continue;
  274. if (alt->errata_id >= CPUFEATURE_NUMBER) {
  275. WARN(1, "This feature id:%d is not in kernel cpufeature list",
  276. alt->errata_id);
  277. continue;
  278. }
  279. tmp = (1U << alt->errata_id);
  280. if (cpu_req_feature & tmp) {
  281. mutex_lock(&text_mutex);
  282. patch_text_nosync(alt->old_ptr, alt->alt_ptr, alt->alt_len);
  283. mutex_unlock(&text_mutex);
  284. }
  285. }
  286. }
  287. #endif