cpu.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012 Regents of the University of California
  4. */
  5. #include <linux/cpu.h>
  6. #include <linux/init.h>
  7. #include <linux/seq_file.h>
  8. #include <linux/of.h>
  9. #include <asm/csr.h>
  10. #include <asm/hwcap.h>
  11. #include <asm/sbi.h>
  12. #include <asm/smp.h>
  13. #include <asm/pgtable.h>
  14. /*
  15. * Returns the hart ID of the given device tree node, or -ENODEV if the node
  16. * isn't an enabled and valid RISC-V hart node.
  17. */
  18. int riscv_of_processor_hartid(struct device_node *node, unsigned long *hart)
  19. {
  20. const char *isa;
  21. if (!of_device_is_compatible(node, "riscv")) {
  22. pr_warn("Found incompatible CPU\n");
  23. return -ENODEV;
  24. }
  25. *hart = (unsigned long) of_get_cpu_hwid(node, 0);
  26. if (*hart == ~0UL) {
  27. pr_warn("Found CPU without hart ID\n");
  28. return -ENODEV;
  29. }
  30. if (!of_device_is_available(node)) {
  31. pr_info("CPU with hartid=%lu is not available\n", *hart);
  32. return -ENODEV;
  33. }
  34. if (of_property_read_string(node, "riscv,isa", &isa)) {
  35. pr_warn("CPU with hartid=%lu has no \"riscv,isa\" property\n", *hart);
  36. return -ENODEV;
  37. }
  38. if (isa[0] != 'r' || isa[1] != 'v') {
  39. pr_warn("CPU with hartid=%lu has an invalid ISA of \"%s\"\n", *hart, isa);
  40. return -ENODEV;
  41. }
  42. return 0;
  43. }
  44. /*
  45. * Find hart ID of the CPU DT node under which given DT node falls.
  46. *
  47. * To achieve this, we walk up the DT tree until we find an active
  48. * RISC-V core (HART) node and extract the cpuid from it.
  49. */
  50. int riscv_of_parent_hartid(struct device_node *node, unsigned long *hartid)
  51. {
  52. for (; node; node = node->parent) {
  53. if (of_device_is_compatible(node, "riscv")) {
  54. *hartid = (unsigned long)of_get_cpu_hwid(node, 0);
  55. if (*hartid == ~0UL) {
  56. pr_warn("Found CPU without hart ID\n");
  57. return -ENODEV;
  58. }
  59. return 0;
  60. }
  61. }
  62. return -1;
  63. }
  64. #ifdef CONFIG_PROC_FS
  65. struct riscv_cpuinfo {
  66. unsigned long mvendorid;
  67. unsigned long marchid;
  68. unsigned long mimpid;
  69. };
  70. static DEFINE_PER_CPU(struct riscv_cpuinfo, riscv_cpuinfo);
  71. static int riscv_cpuinfo_starting(unsigned int cpu)
  72. {
  73. struct riscv_cpuinfo *ci = this_cpu_ptr(&riscv_cpuinfo);
  74. #if IS_ENABLED(CONFIG_RISCV_SBI)
  75. ci->mvendorid = sbi_spec_is_0_1() ? 0 : sbi_get_mvendorid();
  76. ci->marchid = sbi_spec_is_0_1() ? 0 : sbi_get_marchid();
  77. ci->mimpid = sbi_spec_is_0_1() ? 0 : sbi_get_mimpid();
  78. #elif IS_ENABLED(CONFIG_RISCV_M_MODE)
  79. ci->mvendorid = csr_read(CSR_MVENDORID);
  80. ci->marchid = csr_read(CSR_MARCHID);
  81. ci->mimpid = csr_read(CSR_MIMPID);
  82. #else
  83. ci->mvendorid = 0;
  84. ci->marchid = 0;
  85. ci->mimpid = 0;
  86. #endif
  87. return 0;
  88. }
  89. static int __init riscv_cpuinfo_init(void)
  90. {
  91. int ret;
  92. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "riscv/cpuinfo:starting",
  93. riscv_cpuinfo_starting, NULL);
  94. if (ret < 0) {
  95. pr_err("cpuinfo: failed to register hotplug callbacks.\n");
  96. return ret;
  97. }
  98. return 0;
  99. }
  100. device_initcall(riscv_cpuinfo_init);
  101. #define __RISCV_ISA_EXT_DATA(UPROP, EXTID) \
  102. { \
  103. .uprop = #UPROP, \
  104. .isa_ext_id = EXTID, \
  105. }
  106. /*
  107. * Here are the ordering rules of extension naming defined by RISC-V
  108. * specification :
  109. * 1. All extensions should be separated from other multi-letter extensions
  110. * by an underscore.
  111. * 2. The first letter following the 'Z' conventionally indicates the most
  112. * closely related alphabetical extension category, IMAFDQLCBKJTPVH.
  113. * If multiple 'Z' extensions are named, they should be ordered first
  114. * by category, then alphabetically within a category.
  115. * 3. Standard supervisor-level extensions (starts with 'S') should be
  116. * listed after standard unprivileged extensions. If multiple
  117. * supervisor-level extensions are listed, they should be ordered
  118. * alphabetically.
  119. * 4. Non-standard extensions (starts with 'X') must be listed after all
  120. * standard extensions. They must be separated from other multi-letter
  121. * extensions by an underscore.
  122. */
  123. static struct riscv_isa_ext_data isa_ext_arr[] = {
  124. __RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF),
  125. __RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),
  126. __RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),
  127. __RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),
  128. __RISCV_ISA_EXT_DATA(zicbom, RISCV_ISA_EXT_ZICBOM),
  129. __RISCV_ISA_EXT_DATA(zihintpause, RISCV_ISA_EXT_ZIHINTPAUSE),
  130. __RISCV_ISA_EXT_DATA("", RISCV_ISA_EXT_MAX),
  131. };
  132. static void print_isa_ext(struct seq_file *f)
  133. {
  134. struct riscv_isa_ext_data *edata;
  135. int i = 0, arr_sz;
  136. arr_sz = ARRAY_SIZE(isa_ext_arr) - 1;
  137. /* No extension support available */
  138. if (arr_sz <= 0)
  139. return;
  140. for (i = 0; i <= arr_sz; i++) {
  141. edata = &isa_ext_arr[i];
  142. if (!__riscv_isa_extension_available(NULL, edata->isa_ext_id))
  143. continue;
  144. seq_printf(f, "_%s", edata->uprop);
  145. }
  146. }
  147. /*
  148. * These are the only valid base (single letter) ISA extensions as per the spec.
  149. * It also specifies the canonical order in which it appears in the spec.
  150. * Some of the extension may just be a place holder for now (B, K, P, J).
  151. * This should be updated once corresponding extensions are ratified.
  152. */
  153. static const char base_riscv_exts[13] = "imafdqcbkjpvh";
  154. static void print_isa(struct seq_file *f, const char *isa)
  155. {
  156. int i;
  157. seq_puts(f, "isa\t\t: ");
  158. /* Print the rv[64/32] part */
  159. seq_write(f, isa, 4);
  160. for (i = 0; i < sizeof(base_riscv_exts); i++) {
  161. if (__riscv_isa_extension_available(NULL, base_riscv_exts[i] - 'a'))
  162. /* Print only enabled the base ISA extensions */
  163. seq_write(f, &base_riscv_exts[i], 1);
  164. }
  165. print_isa_ext(f);
  166. seq_puts(f, "\n");
  167. }
  168. static void print_mmu(struct seq_file *f)
  169. {
  170. char sv_type[16];
  171. #ifdef CONFIG_MMU
  172. #if defined(CONFIG_32BIT)
  173. strncpy(sv_type, "sv32", 5);
  174. #elif defined(CONFIG_64BIT)
  175. if (pgtable_l5_enabled)
  176. strncpy(sv_type, "sv57", 5);
  177. else if (pgtable_l4_enabled)
  178. strncpy(sv_type, "sv48", 5);
  179. else
  180. strncpy(sv_type, "sv39", 5);
  181. #endif
  182. #else
  183. strncpy(sv_type, "none", 5);
  184. #endif /* CONFIG_MMU */
  185. seq_printf(f, "mmu\t\t: %s\n", sv_type);
  186. }
  187. static void *c_start(struct seq_file *m, loff_t *pos)
  188. {
  189. if (*pos == nr_cpu_ids)
  190. return NULL;
  191. *pos = cpumask_next(*pos - 1, cpu_online_mask);
  192. if ((*pos) < nr_cpu_ids)
  193. return (void *)(uintptr_t)(1 + *pos);
  194. return NULL;
  195. }
  196. static void *c_next(struct seq_file *m, void *v, loff_t *pos)
  197. {
  198. (*pos)++;
  199. return c_start(m, pos);
  200. }
  201. static void c_stop(struct seq_file *m, void *v)
  202. {
  203. }
  204. static int c_show(struct seq_file *m, void *v)
  205. {
  206. unsigned long cpu_id = (unsigned long)v - 1;
  207. struct device_node *node = of_get_cpu_node(cpu_id, NULL);
  208. struct riscv_cpuinfo *ci = per_cpu_ptr(&riscv_cpuinfo, cpu_id);
  209. const char *compat, *isa;
  210. seq_printf(m, "processor\t: %lu\n", cpu_id);
  211. seq_printf(m, "hart\t\t: %lu\n", cpuid_to_hartid_map(cpu_id));
  212. if (!of_property_read_string(node, "riscv,isa", &isa))
  213. print_isa(m, isa);
  214. print_mmu(m);
  215. if (!of_property_read_string(node, "compatible", &compat)
  216. && strcmp(compat, "riscv"))
  217. seq_printf(m, "uarch\t\t: %s\n", compat);
  218. seq_printf(m, "mvendorid\t: 0x%lx\n", ci->mvendorid);
  219. seq_printf(m, "marchid\t\t: 0x%lx\n", ci->marchid);
  220. seq_printf(m, "mimpid\t\t: 0x%lx\n", ci->mimpid);
  221. seq_puts(m, "\n");
  222. of_node_put(node);
  223. return 0;
  224. }
  225. const struct seq_operations cpuinfo_op = {
  226. .start = c_start,
  227. .next = c_next,
  228. .stop = c_stop,
  229. .show = c_show
  230. };
  231. #endif /* CONFIG_PROC_FS */