xref: /linux/arch/nios2/kernel/cpuinfo.c (revision 23460839b983d5d8d47fe90f341599f66523dd81)
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 
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/seq_file.h>
26 #include <linux/string.h>
27 #include <linux/of.h>
28 #include <asm/cpuinfo.h>
29 
30 struct cpuinfo cpuinfo;
31 
32 #define err_cpu(x) \
33 	pr_err("ERROR: Nios II " x " different for kernel and DTS\n")
34 
35 static inline u32 fcpu(struct device_node *cpu, const char *n)
36 {
37 	u32 val = 0;
38 
39 	of_property_read_u32(cpu, n, &val);
40 
41 	return val;
42 }
43 
44 void __init setup_cpuinfo(void)
45 {
46 	struct device_node *cpu;
47 	const char *str;
48 	int len;
49 
50 	cpu = of_find_node_by_type(NULL, "cpu");
51 	if (!cpu)
52 		panic("%s: No CPU found in devicetree!\n", __func__);
53 
54 	if (!of_property_read_bool(cpu, "altr,has-initda"))
55 		panic("initda instruction is unimplemented. Please update your "
56 			"hardware system to have more than 4-byte line data "
57 			"cache\n");
58 
59 	cpuinfo.cpu_clock_freq = fcpu(cpu, "clock-frequency");
60 
61 	str = of_get_property(cpu, "altr,implementation", &len);
62 	if (str)
63 		strlcpy(cpuinfo.cpu_impl, str, sizeof(cpuinfo.cpu_impl));
64 	else
65 		strcpy(cpuinfo.cpu_impl, "<unknown>");
66 
67 	cpuinfo.has_div = of_property_read_bool(cpu, "altr,has-div");
68 	cpuinfo.has_mul = of_property_read_bool(cpu, "altr,has-mul");
69 	cpuinfo.has_mulx = of_property_read_bool(cpu, "altr,has-mulx");
70 	cpuinfo.has_bmx = of_property_read_bool(cpu, "altr,has-bmx");
71 	cpuinfo.mmu = of_property_read_bool(cpu, "altr,has-mmu");
72 
73 	if (IS_ENABLED(CONFIG_NIOS2_HW_DIV_SUPPORT) && !cpuinfo.has_div)
74 		err_cpu("DIV");
75 
76 	if (IS_ENABLED(CONFIG_NIOS2_HW_MUL_SUPPORT) && !cpuinfo.has_mul)
77 		err_cpu("MUL");
78 
79 	if (IS_ENABLED(CONFIG_NIOS2_HW_MULX_SUPPORT) && !cpuinfo.has_mulx)
80 		err_cpu("MULX");
81 
82 	if (IS_ENABLED(CONFIG_NIOS2_BMX_SUPPORT) && !cpuinfo.has_bmx)
83 		err_cpu("BMX");
84 
85 	cpuinfo.tlb_num_ways = fcpu(cpu, "altr,tlb-num-ways");
86 	if (!cpuinfo.tlb_num_ways)
87 		panic("altr,tlb-num-ways can't be 0. Please check your hardware "
88 			"system\n");
89 	cpuinfo.icache_line_size = fcpu(cpu, "icache-line-size");
90 	cpuinfo.icache_size = fcpu(cpu, "icache-size");
91 	if (CONFIG_NIOS2_ICACHE_SIZE != cpuinfo.icache_size)
92 		pr_warn("Warning: icache size configuration mismatch "
93 		"(0x%x vs 0x%x) of CONFIG_NIOS2_ICACHE_SIZE vs "
94 		"device tree icache-size\n",
95 		CONFIG_NIOS2_ICACHE_SIZE, cpuinfo.icache_size);
96 
97 	cpuinfo.dcache_line_size = fcpu(cpu, "dcache-line-size");
98 	if (CONFIG_NIOS2_DCACHE_LINE_SIZE != cpuinfo.dcache_line_size)
99 		pr_warn("Warning: dcache line size configuration mismatch "
100 		"(0x%x vs 0x%x) of CONFIG_NIOS2_DCACHE_LINE_SIZE vs "
101 		"device tree dcache-line-size\n",
102 		CONFIG_NIOS2_DCACHE_LINE_SIZE, cpuinfo.dcache_line_size);
103 	cpuinfo.dcache_size = fcpu(cpu, "dcache-size");
104 	if (CONFIG_NIOS2_DCACHE_SIZE != cpuinfo.dcache_size)
105 		pr_warn("Warning: dcache size configuration mismatch "
106 			"(0x%x vs 0x%x) of CONFIG_NIOS2_DCACHE_SIZE vs "
107 			"device tree dcache-size\n",
108 			CONFIG_NIOS2_DCACHE_SIZE, cpuinfo.dcache_size);
109 
110 	cpuinfo.tlb_pid_num_bits = fcpu(cpu, "altr,pid-num-bits");
111 	cpuinfo.tlb_num_ways_log2 = ilog2(cpuinfo.tlb_num_ways);
112 	cpuinfo.tlb_num_entries = fcpu(cpu, "altr,tlb-num-entries");
113 	cpuinfo.tlb_num_lines = cpuinfo.tlb_num_entries / cpuinfo.tlb_num_ways;
114 	cpuinfo.tlb_ptr_sz = fcpu(cpu, "altr,tlb-ptr-sz");
115 
116 	cpuinfo.reset_addr = fcpu(cpu, "altr,reset-addr");
117 	cpuinfo.exception_addr = fcpu(cpu, "altr,exception-addr");
118 	cpuinfo.fast_tlb_miss_exc_addr = fcpu(cpu, "altr,fast-tlb-miss-addr");
119 }
120 
121 #ifdef CONFIG_PROC_FS
122 
123 /*
124  * Get CPU information for use by the procfs.
125  */
126 static int show_cpuinfo(struct seq_file *m, void *v)
127 {
128 	const u32 clockfreq = cpuinfo.cpu_clock_freq;
129 
130 	seq_printf(m,
131 		   "CPU:\t\tNios II/%s\n"
132 		   "REV:\t\t%i\n"
133 		   "MMU:\t\t%s\n"
134 		   "FPU:\t\tnone\n"
135 		   "Clocking:\t%u.%02u MHz\n"
136 		   "BogoMips:\t%lu.%02lu\n"
137 		   "Calibration:\t%lu loops\n",
138 		   cpuinfo.cpu_impl,
139 		   CONFIG_NIOS2_ARCH_REVISION,
140 		   cpuinfo.mmu ? "present" : "none",
141 		   clockfreq / 1000000, (clockfreq / 100000) % 10,
142 		   (loops_per_jiffy * HZ) / 500000,
143 		   ((loops_per_jiffy * HZ) / 5000) % 100,
144 		   (loops_per_jiffy * HZ));
145 
146 	seq_printf(m,
147 		   "HW:\n"
148 		   " MUL:\t\t%s\n"
149 		   " MULX:\t\t%s\n"
150 		   " DIV:\t\t%s\n"
151 		   " BMX:\t\t%s\n",
152 		   cpuinfo.has_mul ? "yes" : "no",
153 		   cpuinfo.has_mulx ? "yes" : "no",
154 		   cpuinfo.has_div ? "yes" : "no",
155 		   cpuinfo.has_bmx ? "yes" : "no");
156 
157 	seq_printf(m,
158 		   "Icache:\t\t%ukB, line length: %u\n",
159 		   cpuinfo.icache_size >> 10,
160 		   cpuinfo.icache_line_size);
161 
162 	seq_printf(m,
163 		   "Dcache:\t\t%ukB, line length: %u\n",
164 		   cpuinfo.dcache_size >> 10,
165 		   cpuinfo.dcache_line_size);
166 
167 	seq_printf(m,
168 		   "TLB:\t\t%u ways, %u entries, %u PID bits\n",
169 		   cpuinfo.tlb_num_ways,
170 		   cpuinfo.tlb_num_entries,
171 		   cpuinfo.tlb_pid_num_bits);
172 
173 	return 0;
174 }
175 
176 static void *cpuinfo_start(struct seq_file *m, loff_t *pos)
177 {
178 	unsigned long i = *pos;
179 
180 	return i < num_possible_cpus() ? (void *) (i + 1) : NULL;
181 }
182 
183 static void *cpuinfo_next(struct seq_file *m, void *v, loff_t *pos)
184 {
185 	++*pos;
186 	return cpuinfo_start(m, pos);
187 }
188 
189 static void cpuinfo_stop(struct seq_file *m, void *v)
190 {
191 }
192 
193 const struct seq_operations cpuinfo_op = {
194 	.start	= cpuinfo_start,
195 	.next	= cpuinfo_next,
196 	.stop	= cpuinfo_stop,
197 	.show	= show_cpuinfo
198 };
199 
200 #endif /* CONFIG_PROC_FS */
201