cpu.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012 Regents of the University of California
  4. */
  5. #include <linux/init.h>
  6. #include <linux/seq_file.h>
  7. #include <linux/of.h>
  8. #include <asm/smp.h>
  9. /*
  10. * Returns the hart ID of the given device tree node, or -ENODEV if the node
  11. * isn't an enabled and valid RISC-V hart node.
  12. */
  13. int riscv_of_processor_hartid(struct device_node *node)
  14. {
  15. const char *isa;
  16. u32 hart;
  17. if (!of_device_is_compatible(node, "riscv")) {
  18. pr_warn("Found incompatible CPU\n");
  19. return -ENODEV;
  20. }
  21. if (of_property_read_u32(node, "reg", &hart)) {
  22. pr_warn("Found CPU without hart ID\n");
  23. return -ENODEV;
  24. }
  25. if (of_property_read_string(node, "riscv,isa", &isa)) {
  26. pr_warn("CPU with hartid=%d has no \"riscv,isa\" property\n", hart);
  27. return -ENODEV;
  28. }
  29. if (isa[0] != 'r' || isa[1] != 'v') {
  30. pr_warn("CPU with hartid=%d has an invalid ISA of \"%s\"\n", hart, isa);
  31. return -ENODEV;
  32. }
  33. return hart;
  34. }
  35. /*
  36. * Find hart ID of the CPU DT node under which given DT node falls.
  37. *
  38. * To achieve this, we walk up the DT tree until we find an active
  39. * RISC-V core (HART) node and extract the cpuid from it.
  40. */
  41. int riscv_of_parent_hartid(struct device_node *node)
  42. {
  43. for (; node; node = node->parent) {
  44. if (of_device_is_compatible(node, "riscv"))
  45. return riscv_of_processor_hartid(node);
  46. }
  47. return -1;
  48. }
  49. #ifdef CONFIG_PROC_FS
  50. static void print_isa(struct seq_file *f, const char *isa)
  51. {
  52. /* Print the entire ISA as it is */
  53. seq_puts(f, "isa\t\t: ");
  54. seq_write(f, isa, strlen(isa));
  55. seq_puts(f, "\n");
  56. }
  57. static void print_mmu(struct seq_file *f, const char *mmu_type)
  58. {
  59. #if defined(CONFIG_32BIT)
  60. if (strcmp(mmu_type, "riscv,sv32") != 0)
  61. return;
  62. #elif defined(CONFIG_64BIT)
  63. if (strcmp(mmu_type, "riscv,sv39") != 0 &&
  64. strcmp(mmu_type, "riscv,sv48") != 0)
  65. return;
  66. #endif
  67. seq_printf(f, "mmu\t\t: %s\n", mmu_type+6);
  68. }
  69. static void *c_start(struct seq_file *m, loff_t *pos)
  70. {
  71. *pos = cpumask_next(*pos - 1, cpu_online_mask);
  72. if ((*pos) < nr_cpu_ids)
  73. return (void *)(uintptr_t)(1 + *pos);
  74. return NULL;
  75. }
  76. static void *c_next(struct seq_file *m, void *v, loff_t *pos)
  77. {
  78. (*pos)++;
  79. return c_start(m, pos);
  80. }
  81. static void c_stop(struct seq_file *m, void *v)
  82. {
  83. }
  84. static int c_show(struct seq_file *m, void *v)
  85. {
  86. unsigned long cpu_id = (unsigned long)v - 1;
  87. struct device_node *node = of_get_cpu_node(cpu_id, NULL);
  88. const char *compat, *isa, *mmu;
  89. const char *freq, *icache, *dcache, *l2cache, *tlb, *cacheline, *vecver;
  90. seq_printf(m, "processor\t: %lu\n", cpu_id);
  91. seq_printf(m, "hart\t\t: %lu\n", cpuid_to_hartid_map(cpu_id));
  92. if (!of_property_read_string(node, "riscv,isa", &isa))
  93. print_isa(m, isa);
  94. if (!of_property_read_string(node, "mmu-type", &mmu))
  95. print_mmu(m, mmu);
  96. if (!of_property_read_string(node, "compatible", &compat)
  97. && strcmp(compat, "riscv"))
  98. seq_printf(m, "uarch\t\t: %s\n", compat);
  99. if (!of_property_read_string(node, "cpu-freq", &freq))
  100. seq_printf(m, "cpu-freq\t: %s\n", freq);
  101. if (!of_property_read_string(node, "cpu-icache", &icache))
  102. seq_printf(m, "cpu-icache\t: %s\n", icache);
  103. if (!of_property_read_string(node, "cpu-dcache", &dcache))
  104. seq_printf(m, "cpu-dcache\t: %s\n", dcache);
  105. if (!of_property_read_string(node, "cpu-l2cache", &l2cache))
  106. seq_printf(m, "cpu-l2cache\t: %s\n", l2cache);
  107. if (!of_property_read_string(node, "cpu-tlb", &tlb))
  108. seq_printf(m, "cpu-tlb\t\t: %s\n", tlb);
  109. if (!of_property_read_string(node, "cpu-cacheline", &cacheline))
  110. seq_printf(m, "cpu-cacheline\t: %s\n", cacheline);
  111. if (!of_property_read_string(node, "cpu-vector", &vecver))
  112. seq_printf(m, "cpu-vector\t: %s\n", vecver);
  113. seq_puts(m, "\n");
  114. of_node_put(node);
  115. return 0;
  116. }
  117. const struct seq_operations cpuinfo_op = {
  118. .start = c_start,
  119. .next = c_next,
  120. .stop = c_stop,
  121. .show = c_show
  122. };
  123. #endif /* CONFIG_PROC_FS */