1 /* $NetBSD: cpu.c,v 1.33 2014/03/24 19:29:59 christos 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.33 2014/03/24 19:29:59 christos 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 bool cpufound; 227 228 static int 229 cpumatch(device_t parent, cfdata_t cf, void *aux) 230 { 231 struct plb_attach_args *paa = aux; 232 233 /* make sure that we're looking for a CPU */ 234 if (strcmp(paa->plb_name, cf->cf_name) != 0) 235 return (0); 236 237 return !cpufound; 238 } 239 240 static void 241 cpuattach(device_t parent, device_t self, void *aux) 242 { 243 struct cpu_info * const ci = curcpu(); 244 const struct cputab *cp; 245 u_int processor_freq; 246 prop_number_t freq; 247 248 freq = prop_dictionary_get(board_properties, "processor-frequency"); 249 KASSERT(freq != NULL); 250 processor_freq = (unsigned int) prop_number_integer_value(freq); 251 252 cpufound = true; 253 ncpus++; 254 255 const u_int pvr = mfpvr(); 256 for (cp = models; cp->name != NULL; cp++) { 257 if ((pvr & cp->mask) == cp->version) { 258 cpu_setmodel("%s", cp->name); 259 break; 260 } 261 } 262 if (__predict_false(cp->name == NULL)) 263 cpu_setmodel("Version 0x%x", pvr); 264 265 aprint_normal(": %uMHz %s (PVR 0x%x)\n", 266 (processor_freq + 500000) / 1000000, 267 (cp->name != NULL ? cpu_getmodel() : "unknown model"), 268 pvr); 269 270 cpu_probe_cache(); 271 272 /* We would crash later on anyway so just make the reason obvious */ 273 if (ci->ci_ci.icache_size == 0 && ci->ci_ci.dcache_size == 0) 274 panic("%s: %s: could not detect cache size", 275 __func__, device_xname(self)); 276 277 aprint_normal_dev(self, "%uKB/%uB L1 instruction cache\n", 278 ci->ci_ci.icache_size / 1024, ci->ci_ci.icache_line_size); 279 aprint_normal_dev(self, "%uKB/%uB L1 data cache\n", 280 ci->ci_ci.dcache_size / 1024, ci->ci_ci.dcache_line_size); 281 } 282 283 /* 284 * This routine must be explicitly called to initialize the 285 * CPU cache information so cache flushe and memcpy operation 286 * work. 287 */ 288 void 289 cpu_probe_cache(void) 290 { 291 struct cpu_info * const ci = curcpu(); 292 const struct cputab *cp = models; 293 294 const u_int pvr = mfpvr(); 295 for (cp = models; cp->name != NULL; cp++) { 296 if ((pvr & cp->mask) == cp->version) 297 break; 298 } 299 300 /* 301 * Copy the cache from the cputab into cpu_info. 302 */ 303 ci->ci_ci = cp->ci; 304 } 305 306 /* 307 * These small routines may have to be replaced, 308 * if/when we support processors other that the 604. 309 */ 310 311 void 312 dcache_wbinv_page(vaddr_t va) 313 { 314 const size_t dcache_line_size = curcpu()->ci_ci.dcache_line_size; 315 316 if (dcache_line_size) { 317 for (size_t i = 0; i < PAGE_SIZE; i += dcache_line_size) { 318 __asm volatile("dcbf %0,%1" : : "b" (va), "r" (i)); 319 } 320 __asm volatile("sync;isync" : : ); 321 } 322 } 323