cpuinfo.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2013 Altera Corporation
  4. * Copyright (C) 2011 Tobias Klauser <tklauser@distanz.ch>
  5. *
  6. * Based on cpuinfo.c from microblaze
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/init.h>
  10. #include <linux/delay.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/string.h>
  13. #include <linux/of.h>
  14. #include <asm/cpuinfo.h>
  15. struct cpuinfo cpuinfo;
  16. #define err_cpu(x) \
  17. pr_err("ERROR: Nios II " x " different for kernel and DTS\n")
  18. static inline u32 fcpu(struct device_node *cpu, const char *n)
  19. {
  20. u32 val = 0;
  21. of_property_read_u32(cpu, n, &val);
  22. return val;
  23. }
  24. void __init setup_cpuinfo(void)
  25. {
  26. struct device_node *cpu;
  27. const char *str;
  28. int len;
  29. cpu = of_get_cpu_node(0, NULL);
  30. if (!cpu)
  31. panic("%s: No CPU found in devicetree!\n", __func__);
  32. if (!of_property_read_bool(cpu, "altr,has-initda"))
  33. panic("initda instruction is unimplemented. Please update your "
  34. "hardware system to have more than 4-byte line data "
  35. "cache\n");
  36. cpuinfo.cpu_clock_freq = fcpu(cpu, "clock-frequency");
  37. str = of_get_property(cpu, "altr,implementation", &len);
  38. if (str)
  39. strlcpy(cpuinfo.cpu_impl, str, sizeof(cpuinfo.cpu_impl));
  40. else
  41. strcpy(cpuinfo.cpu_impl, "<unknown>");
  42. cpuinfo.has_div = of_property_read_bool(cpu, "altr,has-div");
  43. cpuinfo.has_mul = of_property_read_bool(cpu, "altr,has-mul");
  44. cpuinfo.has_mulx = of_property_read_bool(cpu, "altr,has-mulx");
  45. cpuinfo.has_bmx = of_property_read_bool(cpu, "altr,has-bmx");
  46. cpuinfo.has_cdx = of_property_read_bool(cpu, "altr,has-cdx");
  47. cpuinfo.mmu = of_property_read_bool(cpu, "altr,has-mmu");
  48. if (IS_ENABLED(CONFIG_NIOS2_HW_DIV_SUPPORT) && !cpuinfo.has_div)
  49. err_cpu("DIV");
  50. if (IS_ENABLED(CONFIG_NIOS2_HW_MUL_SUPPORT) && !cpuinfo.has_mul)
  51. err_cpu("MUL");
  52. if (IS_ENABLED(CONFIG_NIOS2_HW_MULX_SUPPORT) && !cpuinfo.has_mulx)
  53. err_cpu("MULX");
  54. if (IS_ENABLED(CONFIG_NIOS2_BMX_SUPPORT) && !cpuinfo.has_bmx)
  55. err_cpu("BMX");
  56. if (IS_ENABLED(CONFIG_NIOS2_CDX_SUPPORT) && !cpuinfo.has_cdx)
  57. err_cpu("CDX");
  58. cpuinfo.tlb_num_ways = fcpu(cpu, "altr,tlb-num-ways");
  59. if (!cpuinfo.tlb_num_ways)
  60. panic("altr,tlb-num-ways can't be 0. Please check your hardware "
  61. "system\n");
  62. cpuinfo.icache_line_size = fcpu(cpu, "icache-line-size");
  63. cpuinfo.icache_size = fcpu(cpu, "icache-size");
  64. if (CONFIG_NIOS2_ICACHE_SIZE != cpuinfo.icache_size)
  65. pr_warn("Warning: icache size configuration mismatch "
  66. "(0x%x vs 0x%x) of CONFIG_NIOS2_ICACHE_SIZE vs "
  67. "device tree icache-size\n",
  68. CONFIG_NIOS2_ICACHE_SIZE, cpuinfo.icache_size);
  69. cpuinfo.dcache_line_size = fcpu(cpu, "dcache-line-size");
  70. if (CONFIG_NIOS2_DCACHE_LINE_SIZE != cpuinfo.dcache_line_size)
  71. pr_warn("Warning: dcache line size configuration mismatch "
  72. "(0x%x vs 0x%x) of CONFIG_NIOS2_DCACHE_LINE_SIZE vs "
  73. "device tree dcache-line-size\n",
  74. CONFIG_NIOS2_DCACHE_LINE_SIZE, cpuinfo.dcache_line_size);
  75. cpuinfo.dcache_size = fcpu(cpu, "dcache-size");
  76. if (CONFIG_NIOS2_DCACHE_SIZE != cpuinfo.dcache_size)
  77. pr_warn("Warning: dcache size configuration mismatch "
  78. "(0x%x vs 0x%x) of CONFIG_NIOS2_DCACHE_SIZE vs "
  79. "device tree dcache-size\n",
  80. CONFIG_NIOS2_DCACHE_SIZE, cpuinfo.dcache_size);
  81. cpuinfo.tlb_pid_num_bits = fcpu(cpu, "altr,pid-num-bits");
  82. cpuinfo.tlb_num_ways_log2 = ilog2(cpuinfo.tlb_num_ways);
  83. cpuinfo.tlb_num_entries = fcpu(cpu, "altr,tlb-num-entries");
  84. cpuinfo.tlb_num_lines = cpuinfo.tlb_num_entries / cpuinfo.tlb_num_ways;
  85. cpuinfo.tlb_ptr_sz = fcpu(cpu, "altr,tlb-ptr-sz");
  86. cpuinfo.reset_addr = fcpu(cpu, "altr,reset-addr");
  87. cpuinfo.exception_addr = fcpu(cpu, "altr,exception-addr");
  88. cpuinfo.fast_tlb_miss_exc_addr = fcpu(cpu, "altr,fast-tlb-miss-addr");
  89. of_node_put(cpu);
  90. }
  91. #ifdef CONFIG_PROC_FS
  92. /*
  93. * Get CPU information for use by the procfs.
  94. */
  95. static int show_cpuinfo(struct seq_file *m, void *v)
  96. {
  97. const u32 clockfreq = cpuinfo.cpu_clock_freq;
  98. seq_printf(m,
  99. "CPU:\t\tNios II/%s\n"
  100. "REV:\t\t%i\n"
  101. "MMU:\t\t%s\n"
  102. "FPU:\t\tnone\n"
  103. "Clocking:\t%u.%02u MHz\n"
  104. "BogoMips:\t%lu.%02lu\n"
  105. "Calibration:\t%lu loops\n",
  106. cpuinfo.cpu_impl,
  107. CONFIG_NIOS2_ARCH_REVISION,
  108. cpuinfo.mmu ? "present" : "none",
  109. clockfreq / 1000000, (clockfreq / 100000) % 10,
  110. (loops_per_jiffy * HZ) / 500000,
  111. ((loops_per_jiffy * HZ) / 5000) % 100,
  112. (loops_per_jiffy * HZ));
  113. seq_printf(m,
  114. "HW:\n"
  115. " MUL:\t\t%s\n"
  116. " MULX:\t\t%s\n"
  117. " DIV:\t\t%s\n"
  118. " BMX:\t\t%s\n"
  119. " CDX:\t\t%s\n",
  120. cpuinfo.has_mul ? "yes" : "no",
  121. cpuinfo.has_mulx ? "yes" : "no",
  122. cpuinfo.has_div ? "yes" : "no",
  123. cpuinfo.has_bmx ? "yes" : "no",
  124. cpuinfo.has_cdx ? "yes" : "no");
  125. seq_printf(m,
  126. "Icache:\t\t%ukB, line length: %u\n",
  127. cpuinfo.icache_size >> 10,
  128. cpuinfo.icache_line_size);
  129. seq_printf(m,
  130. "Dcache:\t\t%ukB, line length: %u\n",
  131. cpuinfo.dcache_size >> 10,
  132. cpuinfo.dcache_line_size);
  133. seq_printf(m,
  134. "TLB:\t\t%u ways, %u entries, %u PID bits\n",
  135. cpuinfo.tlb_num_ways,
  136. cpuinfo.tlb_num_entries,
  137. cpuinfo.tlb_pid_num_bits);
  138. return 0;
  139. }
  140. static void *cpuinfo_start(struct seq_file *m, loff_t *pos)
  141. {
  142. unsigned long i = *pos;
  143. return i < num_possible_cpus() ? (void *) (i + 1) : NULL;
  144. }
  145. static void *cpuinfo_next(struct seq_file *m, void *v, loff_t *pos)
  146. {
  147. ++*pos;
  148. return cpuinfo_start(m, pos);
  149. }
  150. static void cpuinfo_stop(struct seq_file *m, void *v)
  151. {
  152. }
  153. const struct seq_operations cpuinfo_op = {
  154. .start = cpuinfo_start,
  155. .next = cpuinfo_next,
  156. .stop = cpuinfo_stop,
  157. .show = show_cpuinfo
  158. };
  159. #endif /* CONFIG_PROC_FS */