cpu.c revision 1.2.6.6 1 1.2.6.6 nathanw /* $NetBSD: cpu.c,v 1.2.6.6 2002/10/18 02:39:29 nathanw Exp $ */
2 1.2.6.2 nathanw
3 1.2.6.2 nathanw /*
4 1.2.6.2 nathanw * Copyright 2001 Wasabi Systems, Inc.
5 1.2.6.2 nathanw * All rights reserved.
6 1.2.6.2 nathanw *
7 1.2.6.2 nathanw * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
8 1.2.6.2 nathanw *
9 1.2.6.2 nathanw * Redistribution and use in source and binary forms, with or without
10 1.2.6.2 nathanw * modification, are permitted provided that the following conditions
11 1.2.6.2 nathanw * are met:
12 1.2.6.2 nathanw * 1. Redistributions of source code must retain the above copyright
13 1.2.6.2 nathanw * notice, this list of conditions and the following disclaimer.
14 1.2.6.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
15 1.2.6.2 nathanw * notice, this list of conditions and the following disclaimer in the
16 1.2.6.2 nathanw * documentation and/or other materials provided with the distribution.
17 1.2.6.2 nathanw * 3. All advertising materials mentioning features or use of this software
18 1.2.6.2 nathanw * must display the following acknowledgement:
19 1.2.6.2 nathanw * This product includes software developed for the NetBSD Project by
20 1.2.6.2 nathanw * Wasabi Systems, Inc.
21 1.2.6.2 nathanw * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.2.6.2 nathanw * or promote products derived from this software without specific prior
23 1.2.6.2 nathanw * written permission.
24 1.2.6.2 nathanw *
25 1.2.6.2 nathanw * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.2.6.2 nathanw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.2.6.2 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.2.6.2 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.2.6.2 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.2.6.2 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.2.6.2 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.2.6.2 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.2.6.2 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.2.6.2 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.2.6.2 nathanw * POSSIBILITY OF SUCH DAMAGE.
36 1.2.6.2 nathanw */
37 1.2.6.2 nathanw
38 1.2.6.2 nathanw #include <sys/param.h>
39 1.2.6.2 nathanw #include <sys/systm.h>
40 1.2.6.2 nathanw #include <sys/device.h>
41 1.2.6.2 nathanw #include <sys/properties.h>
42 1.2.6.2 nathanw
43 1.2.6.2 nathanw #include <machine/cpu.h>
44 1.2.6.4 nathanw #include <powerpc/ibm4xx/dev/plbvar.h>
45 1.2.6.2 nathanw
46 1.2.6.2 nathanw struct cputab {
47 1.2.6.2 nathanw int version;
48 1.2.6.2 nathanw char *name;
49 1.2.6.2 nathanw };
50 1.2.6.2 nathanw static struct cputab models[] = {
51 1.2.6.2 nathanw { PVR_401A1 >> 16, "401A1" },
52 1.2.6.2 nathanw { PVR_401B2 >> 16, "401B21" },
53 1.2.6.2 nathanw { PVR_401C2 >> 16, "401C2" },
54 1.2.6.2 nathanw { PVR_401D2 >> 16, "401D2" },
55 1.2.6.2 nathanw { PVR_401E2 >> 16, "401E2" },
56 1.2.6.2 nathanw { PVR_401F2 >> 16, "401F2" },
57 1.2.6.2 nathanw { PVR_401G2 >> 16, "401G2" },
58 1.2.6.2 nathanw { PVR_403 >> 16, "403" },
59 1.2.6.2 nathanw { PVR_405GP >> 16, "405GP" },
60 1.2.6.2 nathanw { 0, NULL }
61 1.2.6.2 nathanw };
62 1.2.6.2 nathanw
63 1.2.6.2 nathanw static int cpumatch(struct device *, struct cfdata *, void *);
64 1.2.6.2 nathanw static void cpuattach(struct device *, struct device *, void *);
65 1.2.6.2 nathanw
66 1.2.6.6 nathanw CFATTACH_DECL(cpu, sizeof(struct device),
67 1.2.6.6 nathanw cpumatch, cpuattach, NULL, NULL);
68 1.2.6.2 nathanw
69 1.2.6.2 nathanw int ncpus;
70 1.2.6.2 nathanw
71 1.2.6.2 nathanw struct cpu_info cpu_info_store;
72 1.2.6.2 nathanw
73 1.2.6.2 nathanw int cpufound = 0;
74 1.2.6.2 nathanw
75 1.2.6.2 nathanw static int
76 1.2.6.2 nathanw cpumatch(struct device *parent, struct cfdata *cf, void *aux)
77 1.2.6.2 nathanw {
78 1.2.6.4 nathanw struct plb_attach_args *paa = aux;
79 1.2.6.2 nathanw
80 1.2.6.2 nathanw /* make sure that we're looking for a CPU */
81 1.2.6.6 nathanw if (strcmp(paa->plb_name, cf->cf_name) != 0)
82 1.2.6.3 nathanw return (0);
83 1.2.6.2 nathanw
84 1.2.6.2 nathanw return !cpufound;
85 1.2.6.2 nathanw }
86 1.2.6.2 nathanw
87 1.2.6.2 nathanw static void
88 1.2.6.2 nathanw cpuattach(struct device *parent, struct device *self, void *aux)
89 1.2.6.2 nathanw {
90 1.2.6.2 nathanw int pvr, cpu;
91 1.2.6.2 nathanw int own, pcf, cas, pcl, aid;
92 1.2.6.2 nathanw struct cputab *cp = models;
93 1.2.6.2 nathanw unsigned int processor_freq;
94 1.2.6.2 nathanw
95 1.2.6.3 nathanw if (board_info_get("processor-frequency",
96 1.2.6.2 nathanw &processor_freq, sizeof(processor_freq)) == -1)
97 1.2.6.2 nathanw panic("no processor-frequency");
98 1.2.6.2 nathanw
99 1.2.6.2 nathanw cpufound++;
100 1.2.6.2 nathanw ncpus++;
101 1.2.6.2 nathanw
102 1.2.6.2 nathanw asm ("mfpvr %0" : "=r"(pvr));
103 1.2.6.2 nathanw cpu = pvr >> 16;
104 1.2.6.2 nathanw
105 1.2.6.2 nathanw /* Break PVR up into separate fields and print them out. */
106 1.2.6.2 nathanw own = (pvr >> 20) & 0xfff;
107 1.2.6.2 nathanw pcf = (pvr >> 16) & 0xf;
108 1.2.6.2 nathanw cas = (pvr >> 10) & 0x3f;
109 1.2.6.2 nathanw pcl = (pvr >> 6) & 0xf;
110 1.2.6.2 nathanw aid = pvr & 0x3f;
111 1.2.6.2 nathanw
112 1.2.6.2 nathanw while (cp->name) {
113 1.2.6.2 nathanw if (cp->version == cpu)
114 1.2.6.2 nathanw break;
115 1.2.6.2 nathanw cp++;
116 1.2.6.2 nathanw }
117 1.2.6.2 nathanw if (cp->name)
118 1.2.6.2 nathanw strcpy(cpu_model, cp->name);
119 1.2.6.2 nathanw else
120 1.2.6.2 nathanw sprintf(cpu_model, "Version 0x%x", cpu);
121 1.2.6.2 nathanw sprintf(cpu_model + strlen(cpu_model), " (Revision %d.%d)",
122 1.2.6.2 nathanw (pvr >> 8) & 0xff, pvr & 0xff);
123 1.2.6.2 nathanw
124 1.2.6.2 nathanw #if 1
125 1.2.6.2 nathanw printf(": %dMHz %s\n", processor_freq / 1000 / 1000,
126 1.2.6.2 nathanw cpu_model);
127 1.2.6.2 nathanw #endif
128 1.2.6.2 nathanw
129 1.2.6.2 nathanw cpu_probe_cache();
130 1.2.6.2 nathanw
131 1.2.6.3 nathanw printf("Instruction cache size %d line size %d\n",
132 1.2.6.2 nathanw curcpu()->ci_ci.icache_size, curcpu()->ci_ci.icache_line_size);
133 1.2.6.3 nathanw printf("Data cache size %d line size %d\n",
134 1.2.6.2 nathanw curcpu()->ci_ci.dcache_size, curcpu()->ci_ci.dcache_line_size);
135 1.2.6.2 nathanw
136 1.2.6.2 nathanw #ifdef DEBUG
137 1.2.6.2 nathanw /* It sux that the cache info here is useless. */
138 1.2.6.2 nathanw printf("PVR: owner %x core family %x cache %x version %x asic %x\n",
139 1.2.6.2 nathanw own, pcf, cas, pcl, aid);
140 1.2.6.2 nathanw #endif
141 1.2.6.2 nathanw }
142 1.2.6.2 nathanw
143 1.2.6.2 nathanw /*
144 1.2.6.3 nathanw * This routine must be explicitly called to initialize the
145 1.2.6.3 nathanw * CPU cache information so cache flushe and memcpy operation
146 1.2.6.2 nathanw * work.
147 1.2.6.2 nathanw */
148 1.2.6.2 nathanw void
149 1.2.6.2 nathanw cpu_probe_cache()
150 1.2.6.2 nathanw {
151 1.2.6.2 nathanw int version;
152 1.2.6.2 nathanw
153 1.2.6.2 nathanw /*
154 1.2.6.3 nathanw * First we need to identify the cpu and determine the
155 1.2.6.2 nathanw * cache line size, or things like memset/memcpy may lose
156 1.2.6.2 nathanw * badly.
157 1.2.6.2 nathanw */
158 1.2.6.2 nathanw __asm __volatile("mfpvr %0" : "=r" (version));
159 1.2.6.2 nathanw switch (version & 0xffff0000) {
160 1.2.6.2 nathanw case PVR_401A1:
161 1.2.6.2 nathanw curcpu()->ci_ci.dcache_size = 1024;
162 1.2.6.2 nathanw curcpu()->ci_ci.dcache_line_size = 16;
163 1.2.6.2 nathanw curcpu()->ci_ci.icache_size = 2848;
164 1.2.6.2 nathanw curcpu()->ci_ci.icache_line_size = 16;
165 1.2.6.2 nathanw break;
166 1.2.6.2 nathanw case PVR_401B2:
167 1.2.6.2 nathanw curcpu()->ci_ci.dcache_size = 8192;
168 1.2.6.2 nathanw curcpu()->ci_ci.dcache_line_size = 16;
169 1.2.6.2 nathanw curcpu()->ci_ci.icache_size = 16384;
170 1.2.6.2 nathanw curcpu()->ci_ci.icache_line_size = 16;
171 1.2.6.2 nathanw break;
172 1.2.6.2 nathanw case PVR_401C2:
173 1.2.6.2 nathanw curcpu()->ci_ci.dcache_size = 8192;
174 1.2.6.2 nathanw curcpu()->ci_ci.dcache_line_size = 16;
175 1.2.6.2 nathanw curcpu()->ci_ci.icache_size = 0;
176 1.2.6.2 nathanw curcpu()->ci_ci.icache_line_size = 16;
177 1.2.6.2 nathanw break;
178 1.2.6.2 nathanw case PVR_401D2:
179 1.2.6.2 nathanw curcpu()->ci_ci.dcache_size = 2848;
180 1.2.6.2 nathanw curcpu()->ci_ci.dcache_line_size = 16;
181 1.2.6.2 nathanw curcpu()->ci_ci.icache_size = 4096;
182 1.2.6.2 nathanw curcpu()->ci_ci.icache_line_size = 16;
183 1.2.6.2 nathanw break;
184 1.2.6.2 nathanw case PVR_401E2:
185 1.2.6.2 nathanw curcpu()->ci_ci.dcache_size = 0;
186 1.2.6.2 nathanw curcpu()->ci_ci.dcache_line_size = 16;
187 1.2.6.2 nathanw curcpu()->ci_ci.icache_size = 0;
188 1.2.6.2 nathanw curcpu()->ci_ci.icache_line_size = 16;
189 1.2.6.2 nathanw break;
190 1.2.6.2 nathanw case PVR_401F2:
191 1.2.6.2 nathanw curcpu()->ci_ci.dcache_size = 2048;
192 1.2.6.2 nathanw curcpu()->ci_ci.dcache_line_size = 16;
193 1.2.6.2 nathanw curcpu()->ci_ci.icache_size = 2848;
194 1.2.6.2 nathanw curcpu()->ci_ci.icache_line_size = 16;
195 1.2.6.2 nathanw break;
196 1.2.6.2 nathanw case PVR_401G2:
197 1.2.6.2 nathanw curcpu()->ci_ci.dcache_size = 2848;
198 1.2.6.2 nathanw curcpu()->ci_ci.dcache_line_size = 16;
199 1.2.6.2 nathanw curcpu()->ci_ci.icache_size = 8192;
200 1.2.6.2 nathanw curcpu()->ci_ci.icache_line_size = 16;
201 1.2.6.2 nathanw break;
202 1.2.6.2 nathanw case PVR_403:
203 1.2.6.2 nathanw curcpu()->ci_ci.dcache_line_size = 16;
204 1.2.6.2 nathanw curcpu()->ci_ci.icache_line_size = 16;
205 1.2.6.2 nathanw break;
206 1.2.6.2 nathanw case PVR_405GP:
207 1.2.6.2 nathanw curcpu()->ci_ci.dcache_size = 8192;
208 1.2.6.2 nathanw curcpu()->ci_ci.dcache_line_size = 32;
209 1.2.6.2 nathanw curcpu()->ci_ci.icache_size = 8192;
210 1.2.6.2 nathanw curcpu()->ci_ci.icache_line_size = 32;
211 1.2.6.2 nathanw break;
212 1.2.6.2 nathanw default:
213 1.2.6.3 nathanw /*
214 1.2.6.3 nathanw * Unknown CPU type. For safety we'll specify a
215 1.2.6.3 nathanw * cache with a 4-byte line size. That way cache
216 1.2.6.2 nathanw * flush routines won't miss any lines.
217 1.2.6.2 nathanw */
218 1.2.6.2 nathanw curcpu()->ci_ci.dcache_line_size = 4;
219 1.2.6.2 nathanw curcpu()->ci_ci.icache_line_size = 4;
220 1.2.6.2 nathanw break;
221 1.2.6.2 nathanw }
222 1.2.6.2 nathanw
223 1.2.6.2 nathanw }
224 1.2.6.2 nathanw
225 1.2.6.2 nathanw /*
226 1.2.6.2 nathanw * These small routines may have to be replaced,
227 1.2.6.2 nathanw * if/when we support processors other that the 604.
228 1.2.6.2 nathanw */
229 1.2.6.2 nathanw
230 1.2.6.2 nathanw void
231 1.2.6.2 nathanw dcache_flush_page(vaddr_t va)
232 1.2.6.2 nathanw {
233 1.2.6.2 nathanw int i;
234 1.2.6.2 nathanw
235 1.2.6.2 nathanw if (curcpu()->ci_ci.dcache_line_size)
236 1.2.6.2 nathanw for (i = 0; i < NBPG; i += curcpu()->ci_ci.dcache_line_size)
237 1.2.6.2 nathanw asm volatile("dcbf %0,%1" : : "r" (va), "r" (i));
238 1.2.6.2 nathanw asm volatile("sync;isync" : : );
239 1.2.6.2 nathanw }
240 1.2.6.2 nathanw
241 1.2.6.2 nathanw void
242 1.2.6.2 nathanw icache_flush_page(vaddr_t va)
243 1.2.6.2 nathanw {
244 1.2.6.2 nathanw int i;
245 1.2.6.2 nathanw
246 1.2.6.2 nathanw if (curcpu()->ci_ci.icache_line_size)
247 1.2.6.2 nathanw for (i = 0; i < NBPG; i += curcpu()->ci_ci.icache_line_size)
248 1.2.6.2 nathanw asm volatile("icbi %0,%1" : : "r" (va), "r" (i));
249 1.2.6.2 nathanw asm volatile("sync;isync" : : );
250 1.2.6.2 nathanw }
251 1.2.6.2 nathanw
252 1.2.6.2 nathanw void
253 1.2.6.2 nathanw dcache_flush(vaddr_t va, vsize_t len)
254 1.2.6.2 nathanw {
255 1.2.6.2 nathanw int i;
256 1.2.6.2 nathanw
257 1.2.6.2 nathanw if (len == 0)
258 1.2.6.2 nathanw return;
259 1.2.6.2 nathanw
260 1.2.6.2 nathanw /* Make sure we flush all cache lines */
261 1.2.6.2 nathanw len += va & (curcpu()->ci_ci.dcache_line_size-1);
262 1.2.6.2 nathanw if (curcpu()->ci_ci.dcache_line_size)
263 1.2.6.2 nathanw for (i = 0; i < len; i += curcpu()->ci_ci.dcache_line_size)
264 1.2.6.2 nathanw asm volatile("dcbf %0,%1" : : "r" (va), "r" (i));
265 1.2.6.2 nathanw asm volatile("sync;isync" : : );
266 1.2.6.2 nathanw }
267 1.2.6.2 nathanw
268 1.2.6.2 nathanw void
269 1.2.6.2 nathanw icache_flush(vaddr_t va, vsize_t len)
270 1.2.6.2 nathanw {
271 1.2.6.2 nathanw int i;
272 1.2.6.2 nathanw
273 1.2.6.2 nathanw if (len == 0)
274 1.2.6.2 nathanw return;
275 1.2.6.2 nathanw
276 1.2.6.2 nathanw /* Make sure we flush all cache lines */
277 1.2.6.2 nathanw len += va & (curcpu()->ci_ci.icache_line_size-1);
278 1.2.6.2 nathanw if (curcpu()->ci_ci.icache_line_size)
279 1.2.6.2 nathanw for (i = 0; i < len; i += curcpu()->ci_ci.icache_line_size)
280 1.2.6.2 nathanw asm volatile("icbi %0,%1" : : "r" (va), "r" (i));
281 1.2.6.2 nathanw asm volatile("sync;isync" : : );
282 1.2.6.2 nathanw }
283