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