cpuinfo.c 5.6 KB

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