1 /* $NetBSD: cpu.c,v 1.26 2007/11/19 02:18:33 ad Exp $ */
4 * Copyright 2001 Wasabi Systems, Inc.
7 * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
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.
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.26 2007/11/19 02:18:33 ad Exp $");
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/device.h>
44 #include <sys/evcnt.h>
46 #include <uvm/uvm_extern.h>
48 #include <prop/proplib.h>
50 #include <machine/cpu.h>
51 #include <powerpc/ibm4xx/dev/plbvar.h>
58 static struct cputab models
[] = {
59 { PVR_401A1
, 0xffff0000, "401A1" },
60 { PVR_401B2
, 0xffff0000, "401B21" },
61 { PVR_401C2
, 0xffff0000, "401C2" },
62 { PVR_401D2
, 0xffff0000, "401D2" },
63 { PVR_401E2
, 0xffff0000, "401E2" },
64 { PVR_401F2
, 0xffff0000, "401F2" },
65 { PVR_401G2
, 0xffff0000, "401G2" },
66 { PVR_403
, 0xffff0000, "403" },
67 { PVR_405GP
, 0xffff0000, "405GP" },
68 { PVR_405GPR
, 0xffff0000, "405GPr" },
69 { PVR_405D5X1
, 0xfffff000, "Xilinx Virtex II Pro" },
70 { PVR_405D5X2
, 0xfffff000, "Xilinx Virtex 4 FX" },
74 static int cpumatch(struct device
*, struct cfdata
*, void *);
75 static void cpuattach(struct device
*, struct device
*, void *);
77 CFATTACH_DECL(cpu
, sizeof(struct device
),
78 cpumatch
, cpuattach
, NULL
, NULL
);
82 struct cpu_info cpu_info
[1] = {
84 /* XXX add more ci_ev_* as we teach 4xx about them */
85 .ci_ev_clock
= EVCNT_INITIALIZER(EVCNT_TYPE_INTR
,
86 NULL
, "cpu0", "clock"),
87 .ci_ev_statclock
= EVCNT_INITIALIZER(EVCNT_TYPE_INTR
,
88 NULL
, "cpu0", "stat clock"),
89 .ci_ev_softclock
= EVCNT_INITIALIZER(EVCNT_TYPE_INTR
,
90 NULL
, "cpu0", "soft clock"),
91 .ci_ev_softnet
= EVCNT_INITIALIZER(EVCNT_TYPE_INTR
,
92 NULL
, "cpu0", "soft net"),
93 .ci_ev_softserial
= EVCNT_INITIALIZER(EVCNT_TYPE_INTR
,
94 NULL
, "cpu0", "soft serial"),
104 cpumatch(struct device
*parent
, struct cfdata
*cf
, void *aux
)
106 struct plb_attach_args
*paa
= aux
;
108 /* make sure that we're looking for a CPU */
109 if (strcmp(paa
->plb_name
, cf
->cf_name
) != 0)
116 cpuattach(struct device
*parent
, struct device
*self
, void *aux
)
118 struct cputab
*cp
= models
;
120 u_int processor_freq
;
123 freq
= prop_dictionary_get(board_properties
, "processor-frequency");
124 KASSERT(freq
!= NULL
);
125 processor_freq
= (unsigned int) prop_number_integer_value(freq
);
132 if ((pvr
& cp
->mask
) == cp
->version
)
137 strcpy(cpu_model
, cp
->name
);
139 sprintf(cpu_model
, "Version 0x%x", pvr
);
141 printf(": %dMHz %s (PVR 0x%x)\n", processor_freq
/ 1000 / 1000,
142 cp
->name
? cp
->name
: "unknown model", pvr
);
146 /* We would crash later on anyway so just make the reason obvious */
147 if (curcpu()->ci_ci
.icache_size
== 0 &&
148 curcpu()->ci_ci
.dcache_size
== 0)
149 panic("%s could not detect cache size", device_xname(self
));
151 printf("%s: Instruction cache size %d line size %d\n",
153 curcpu()->ci_ci
.icache_size
, curcpu()->ci_ci
.icache_line_size
);
154 printf("%s: Data cache size %d line size %d\n",
156 curcpu()->ci_ci
.dcache_size
, curcpu()->ci_ci
.dcache_line_size
);
160 * This routine must be explicitly called to initialize the
161 * CPU cache information so cache flushe and memcpy operation
165 cpu_probe_cache(void)
168 * First we need to identify the CPU and determine the
169 * cache line size, or things like memset/memcpy may lose
172 switch (mfpvr() & 0xffff0000) {
174 curcpu()->ci_ci
.dcache_size
= 1024;
175 curcpu()->ci_ci
.dcache_line_size
= 16;
176 curcpu()->ci_ci
.icache_size
= 2848;
177 curcpu()->ci_ci
.icache_line_size
= 16;
180 curcpu()->ci_ci
.dcache_size
= 8192;
181 curcpu()->ci_ci
.dcache_line_size
= 16;
182 curcpu()->ci_ci
.icache_size
= 16384;
183 curcpu()->ci_ci
.icache_line_size
= 16;
186 curcpu()->ci_ci
.dcache_size
= 8192;
187 curcpu()->ci_ci
.dcache_line_size
= 16;
188 curcpu()->ci_ci
.icache_size
= 0;
189 curcpu()->ci_ci
.icache_line_size
= 16;
192 curcpu()->ci_ci
.dcache_size
= 2848;
193 curcpu()->ci_ci
.dcache_line_size
= 16;
194 curcpu()->ci_ci
.icache_size
= 4096;
195 curcpu()->ci_ci
.icache_line_size
= 16;
198 curcpu()->ci_ci
.dcache_size
= 0;
199 curcpu()->ci_ci
.dcache_line_size
= 16;
200 curcpu()->ci_ci
.icache_size
= 0;
201 curcpu()->ci_ci
.icache_line_size
= 16;
204 curcpu()->ci_ci
.dcache_size
= 2048;
205 curcpu()->ci_ci
.dcache_line_size
= 16;
206 curcpu()->ci_ci
.icache_size
= 2848;
207 curcpu()->ci_ci
.icache_line_size
= 16;
210 curcpu()->ci_ci
.dcache_size
= 2848;
211 curcpu()->ci_ci
.dcache_line_size
= 16;
212 curcpu()->ci_ci
.icache_size
= 8192;
213 curcpu()->ci_ci
.icache_line_size
= 16;
216 curcpu()->ci_ci
.dcache_size
= 8192;
217 curcpu()->ci_ci
.dcache_line_size
= 16;
218 curcpu()->ci_ci
.icache_size
= 16384;
219 curcpu()->ci_ci
.icache_line_size
= 16;
222 curcpu()->ci_ci
.dcache_size
= 8192;
223 curcpu()->ci_ci
.dcache_line_size
= 32;
224 curcpu()->ci_ci
.icache_size
= 8192;
225 curcpu()->ci_ci
.icache_line_size
= 32;
230 curcpu()->ci_ci
.dcache_size
= 16384;
231 curcpu()->ci_ci
.dcache_line_size
= 32;
232 curcpu()->ci_ci
.icache_size
= 16384;
233 curcpu()->ci_ci
.icache_line_size
= 32;
237 * Unknown CPU type. For safety we'll specify a
238 * cache with a 4-byte line size. That way cache
239 * flush routines won't miss any lines.
241 curcpu()->ci_ci
.dcache_line_size
= 4;
242 curcpu()->ci_ci
.icache_line_size
= 4;
249 * These small routines may have to be replaced,
250 * if/when we support processors other that the 604.
254 dcache_flush_page(vaddr_t va
)
258 if (curcpu()->ci_ci
.dcache_line_size
)
259 for (i
= 0; i
< PAGE_SIZE
;
260 i
+= curcpu()->ci_ci
.dcache_line_size
)
261 __asm
volatile("dcbf %0,%1" : : "r" (va
), "r" (i
));
262 __asm
volatile("sync;isync" : : );
266 icache_flush_page(vaddr_t va
)
270 if (curcpu()->ci_ci
.icache_line_size
)
271 for (i
= 0; i
< PAGE_SIZE
;
272 i
+= curcpu()->ci_ci
.icache_line_size
)
273 __asm
volatile("icbi %0,%1" : : "r" (va
), "r" (i
));
274 __asm
volatile("sync;isync" : : );
278 dcache_flush(vaddr_t va
, vsize_t len
)
285 /* Make sure we flush all cache lines */
286 len
+= va
& (curcpu()->ci_ci
.dcache_line_size
-1);
287 if (curcpu()->ci_ci
.dcache_line_size
)
288 for (i
= 0; i
< len
; i
+= curcpu()->ci_ci
.dcache_line_size
)
289 __asm
volatile("dcbf %0,%1" : : "r" (va
), "r" (i
));
290 __asm
volatile("sync;isync" : : );
294 icache_flush(vaddr_t va
, vsize_t len
)
301 /* Make sure we flush all cache lines */
302 len
+= va
& (curcpu()->ci_ci
.icache_line_size
-1);
303 if (curcpu()->ci_ci
.icache_line_size
)
304 for (i
= 0; i
< len
; i
+= curcpu()->ci_ci
.icache_line_size
)
305 __asm
volatile("icbi %0,%1" : : "r" (va
), "r" (i
));
306 __asm
volatile("sync;isync" : : );