xref: /netbsd-src/sys/arch/powerpc/ibm4xx/cpu.c (revision a536ee5124e62c9a0051a252f7833dc8f50f44c9)
1 /*	$NetBSD: cpu.c,v 1.32 2011/06/21 04:21:16 matt Exp $	*/
2 
3 /*
4  * Copyright 2001 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed for the NetBSD Project by
20  *      Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.32 2011/06/21 04:21:16 matt Exp $");
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/device.h>
44 #include <sys/evcnt.h>
45 #include <sys/cpu.h>
46 
47 #include <uvm/uvm_extern.h>
48 
49 #include <prop/proplib.h>
50 
51 #include <powerpc/ibm4xx/cpu.h>
52 #include <powerpc/ibm4xx/dev/plbvar.h>
53 
54 struct cputab {
55 	u_int version;
56 	u_int mask;
57 	const char *name;
58 	struct cache_info ci;
59 };
60 
61 static const struct cputab models[] = {
62 	{
63 		.version = PVR_401A1,
64 		.mask = 0xffff0000,
65 		.name = "401A1",
66 		.ci = {
67 			.dcache_size = 1024,
68 			.dcache_line_size = 16,
69 			.icache_size = 2848,
70 			.icache_line_size = 16,
71 		}
72 	}, {
73 		.version = PVR_401B2,
74 		.mask = 0xffff0000,
75 		.name = "401B21",
76 		.ci = {
77 			.dcache_size = 8192,
78 			.dcache_line_size = 16,
79 			.icache_size = 16384,
80 			.icache_line_size = 16,
81 		}
82 	}, {
83 		.version = PVR_401C2,
84 		.mask = 0xffff0000,
85 		.name = "401C2",
86 		.ci = {
87 			.dcache_size = 8192,
88 			.dcache_line_size = 16,
89 			.icache_size = 0,
90 			.icache_line_size = 16,
91 		}
92 	}, {
93 		.version = PVR_401D2,
94 		.mask = 0xffff0000,
95 		.name = "401D2",
96 		.ci = {
97 			.dcache_size = 2848,
98 			.dcache_line_size = 16,
99 			.icache_size = 4096,
100 			.icache_line_size = 16,
101 		}
102 	}, {
103 		.version = PVR_401E2,
104 		.mask = 0xffff0000,
105 		.name = "401E2",
106 		.ci = {
107 			.dcache_size = 0,
108 			.dcache_line_size = 16,
109 			.icache_size = 0,
110 			.icache_line_size = 16,
111 		}
112 	}, {
113 		.version = PVR_401F2,
114 		.mask = 0xffff0000,
115 		.name = "401F2",
116 		.ci = {
117 			.dcache_size = 2048,
118 			.dcache_line_size = 16,
119 			.icache_size = 2848,
120 			.icache_line_size = 16,
121 		}
122 	}, {
123 		.version = PVR_401G2,
124 		.mask = 0xffff0000,
125 		.name = "401G2",
126 		.ci = {
127 			.dcache_size = 2848,
128 			.dcache_line_size = 16,
129 			.icache_size = 8192,
130 			.icache_line_size = 16,
131 		}
132 	}, {
133 		.version = PVR_403,
134 		.mask = 0xffff0000,
135 		.name = "403",
136 		.ci = {
137 			.dcache_size = 8192,
138 			.dcache_line_size = 16,
139 			.icache_size = 16384,
140 			.icache_line_size = 16,
141 		}
142 	}, {
143 		.version = PVR_405GP,
144 		.mask = 0xffff0000,
145 		.name = "405GP",
146 		.ci = {
147 			.dcache_size = 8192,
148 			.dcache_line_size = 32,
149 			.icache_size = 8192,
150 			.icache_line_size = 32,
151 		}
152 	}, {
153 		.version = PVR_405GPR,
154 		.mask = 0xffff0000,
155 		.name = "405GPr",
156 		.ci = {
157 			.dcache_size = 16384,
158 			.dcache_line_size = 32,
159 			.icache_size = 16384,
160 			.icache_line_size = 32,
161 		}
162 	}, {
163 		.version = PVR_405D5X1,
164 		.mask = 0xfffff000,
165 		.name = "Xilinx Virtex II Pro",
166 		.ci = {
167 			.dcache_size = 16384,
168 			.dcache_line_size = 32,
169 			.icache_size = 16384,
170 			.icache_line_size = 32,
171 		}
172 	}, {
173 		.version = PVR_405D5X2,
174 		.mask = 0xfffff000,
175 		.name = "Xilinx Virtex 4 FX",
176 		.ci = {
177 			.dcache_size = 16384,
178 			.dcache_line_size = 32,
179 			.icache_size = 16384,
180 			.icache_line_size = 32,
181 		}
182 	}, {
183 		.version = PVR_405EX,
184 		.mask = 0xffff0000,
185 		.name = "405EX",
186 		.ci = {
187 			.dcache_size = 16384,
188 			.dcache_line_size = 32,
189 			.icache_size = 16384,
190 			.icache_line_size = 32,
191 		}
192 	}, {
193 		.version = 0,
194 		.mask = 0,
195 		.name = NULL,
196 		.ci = {
197 			/*
198 			 * Unknown CPU type.  For safety we'll specify a
199 			 * cache with a 4-byte line size.  That way cache
200 			 * flush routines won't miss any lines.
201 			 */
202 			.dcache_line_size = 4,
203 			.icache_line_size = 4,
204 		},
205 	},
206 };
207 
208 static int	cpumatch(device_t, cfdata_t, void *);
209 static void	cpuattach(device_t, device_t, void *);
210 
211 CFATTACH_DECL_NEW(cpu, 0, cpumatch, cpuattach, NULL, NULL);
212 
213 int ncpus;
214 
215 struct cpu_info cpu_info[1] = {
216 	{
217 		/* XXX add more ci_ev_* as we teach 4xx about them */
218 		.ci_ev_clock = EVCNT_INITIALIZER(EVCNT_TYPE_INTR,
219 		    NULL, "cpu0", "clock"),
220 		.ci_ev_statclock = EVCNT_INITIALIZER(EVCNT_TYPE_INTR,
221 		    NULL, "cpu0", "stat clock"),
222 		.ci_curlwp = &lwp0,
223 	}
224 };
225 
226 char cpu_model[80];
227 
228 bool cpufound;
229 
230 static int
231 cpumatch(device_t parent, cfdata_t cf, void *aux)
232 {
233 	struct plb_attach_args *paa = aux;
234 
235 	/* make sure that we're looking for a CPU */
236 	if (strcmp(paa->plb_name, cf->cf_name) != 0)
237 		return (0);
238 
239 	return !cpufound;
240 }
241 
242 static void
243 cpuattach(device_t parent, device_t self, void *aux)
244 {
245 	struct cpu_info * const ci = curcpu();
246 	const struct cputab *cp;
247 	u_int processor_freq;
248 	prop_number_t freq;
249 
250 	freq = prop_dictionary_get(board_properties, "processor-frequency");
251 	KASSERT(freq != NULL);
252 	processor_freq = (unsigned int) prop_number_integer_value(freq);
253 
254 	cpufound = true;
255 	ncpus++;
256 
257 	const u_int pvr = mfpvr();
258 	for (cp = models; cp->name != NULL; cp++) {
259 		if ((pvr & cp->mask) == cp->version) {
260 			strcpy(cpu_model, cp->name);
261 			break;
262 		}
263 	}
264 	if (__predict_false(cp->name == NULL))
265 		sprintf(cpu_model, "Version 0x%x", pvr);
266 
267 	aprint_normal(": %uMHz %s (PVR 0x%x)\n",
268 	    (processor_freq + 500000) / 1000000,
269 	    (cp->name != NULL ? cpu_model : "unknown model"),
270 	    pvr);
271 
272 	cpu_probe_cache();
273 
274 	/* We would crash later on anyway so just make the reason obvious */
275 	if (ci->ci_ci.icache_size == 0 && ci->ci_ci.dcache_size == 0)
276 		panic("%s: %s: could not detect cache size",
277 		    __func__, device_xname(self));
278 
279 	aprint_normal_dev(self, "%uKB/%uB L1 instruction cache\n",
280 	    ci->ci_ci.icache_size / 1024, ci->ci_ci.icache_line_size);
281 	aprint_normal_dev(self, "%uKB/%uB L1 data cache\n",
282 	    ci->ci_ci.dcache_size / 1024, ci->ci_ci.dcache_line_size);
283 }
284 
285 /*
286  * This routine must be explicitly called to initialize the
287  * CPU cache information so cache flushe and memcpy operation
288  * work.
289  */
290 void
291 cpu_probe_cache(void)
292 {
293 	struct cpu_info * const ci = curcpu();
294 	const struct cputab *cp = models;
295 
296 	const u_int pvr = mfpvr();
297 	for (cp = models; cp->name != NULL; cp++) {
298 		if ((pvr & cp->mask) == cp->version)
299 			break;
300 	}
301 
302 	/*
303 	 * Copy the cache from the cputab into cpu_info.
304 	 */
305 	ci->ci_ci = cp->ci;
306 }
307 
308 /*
309  * These small routines may have to be replaced,
310  * if/when we support processors other that the 604.
311  */
312 
313 void
314 dcache_wbinv_page(vaddr_t va)
315 {
316 	const size_t dcache_line_size = curcpu()->ci_ci.dcache_line_size;
317 
318 	if (dcache_line_size) {
319 		for (size_t i = 0; i < PAGE_SIZE; i += dcache_line_size) {
320 			__asm volatile("dcbf %0,%1" : : "b" (va), "r" (i));
321 		}
322 		__asm volatile("sync;isync" : : );
323 	}
324 }
325