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