1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2008 Marcel Moolenaar 5 * Copyright (c) 2009 Nathan Whitehorn 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 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 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/kernel.h> 34 #include <sys/bus.h> 35 #include <sys/pcpu.h> 36 #include <sys/proc.h> 37 #include <sys/smp.h> 38 #include <vm/vm.h> 39 #include <vm/pmap.h> 40 41 #include <machine/altivec.h> /* For save_vec() */ 42 #include <machine/bus.h> 43 #include <machine/cpu.h> 44 #include <machine/fpu.h> /* For save_fpu() */ 45 #include <machine/hid.h> 46 #include <machine/platformvar.h> 47 #include <machine/setjmp.h> 48 #include <machine/smp.h> 49 #include <machine/spr.h> 50 51 #include <dev/ofw/openfirm.h> 52 #include <machine/ofw_machdep.h> 53 54 #include <powerpc/powermac/platform_powermac.h> 55 56 #include "platform_if.h" 57 58 extern volatile void *ap_pcpu; 59 60 static void dummy_timebase(device_t, bool); 61 static device_t powermac_tb_dev; 62 static void (*freeze_timebase)(device_t, bool) = dummy_timebase; 63 64 static int powermac_probe(platform_t); 65 static int powermac_attach(platform_t); 66 void powermac_mem_regions(platform_t, struct mem_region *phys, int *physsz, 67 struct mem_region *avail, int *availsz); 68 static u_long powermac_timebase_freq(platform_t, struct cpuref *cpuref); 69 static int powermac_smp_first_cpu(platform_t, struct cpuref *cpuref); 70 static int powermac_smp_next_cpu(platform_t, struct cpuref *cpuref); 71 static int powermac_smp_get_bsp(platform_t, struct cpuref *cpuref); 72 static int powermac_smp_start_cpu(platform_t, struct pcpu *cpu); 73 static void powermac_smp_timebase_sync(platform_t, u_long tb, int ap); 74 static void powermac_reset(platform_t); 75 #ifndef __powerpc64__ 76 static void powermac_sleep(platform_t); 77 #endif 78 79 static platform_method_t powermac_methods[] = { 80 PLATFORMMETHOD(platform_probe, powermac_probe), 81 PLATFORMMETHOD(platform_attach, powermac_attach), 82 PLATFORMMETHOD(platform_mem_regions, powermac_mem_regions), 83 PLATFORMMETHOD(platform_timebase_freq, powermac_timebase_freq), 84 85 PLATFORMMETHOD(platform_smp_first_cpu, powermac_smp_first_cpu), 86 PLATFORMMETHOD(platform_smp_next_cpu, powermac_smp_next_cpu), 87 PLATFORMMETHOD(platform_smp_get_bsp, powermac_smp_get_bsp), 88 PLATFORMMETHOD(platform_smp_start_cpu, powermac_smp_start_cpu), 89 PLATFORMMETHOD(platform_smp_timebase_sync, powermac_smp_timebase_sync), 90 91 PLATFORMMETHOD(platform_reset, powermac_reset), 92 #ifndef __powerpc64__ 93 PLATFORMMETHOD(platform_sleep, powermac_sleep), 94 #endif 95 96 PLATFORMMETHOD_END 97 }; 98 99 static platform_def_t powermac_platform = { 100 "powermac", 101 powermac_methods, 102 0 103 }; 104 105 PLATFORM_DEF(powermac_platform); 106 107 static int 108 powermac_probe(platform_t plat) 109 { 110 char compat[255]; 111 ssize_t compatlen; 112 char *curstr; 113 phandle_t root; 114 115 root = OF_peer(0); 116 if (root == 0) 117 return (ENXIO); 118 119 compatlen = OF_getprop(root, "compatible", compat, sizeof(compat)); 120 121 for (curstr = compat; curstr < compat + compatlen; 122 curstr += strlen(curstr) + 1) { 123 if (strncmp(curstr, "MacRISC", 7) == 0) 124 return (BUS_PROBE_SPECIFIC); 125 } 126 127 return (ENXIO); 128 } 129 130 void 131 powermac_mem_regions(platform_t plat, struct mem_region *phys, int *physsz, 132 struct mem_region *avail, int *availsz) 133 { 134 phandle_t memory; 135 cell_t memoryprop[PHYS_AVAIL_SZ * 2]; 136 ssize_t propsize, i, j; 137 int physacells = 1; 138 139 memory = OF_finddevice("/memory"); 140 if (memory == -1) 141 memory = OF_finddevice("/memory@0"); 142 143 /* "reg" has variable #address-cells, but #size-cells is always 1 */ 144 OF_getprop(OF_parent(memory), "#address-cells", &physacells, 145 sizeof(physacells)); 146 147 propsize = OF_getprop(memory, "reg", memoryprop, sizeof(memoryprop)); 148 propsize /= sizeof(cell_t); 149 for (i = 0, j = 0; i < propsize; i += physacells+1, j++) { 150 phys[j].mr_start = memoryprop[i]; 151 if (physacells == 2) { 152 #ifndef __powerpc64__ 153 /* On 32-bit PPC, ignore regions starting above 4 GB */ 154 if (memoryprop[i] != 0) { 155 j--; 156 continue; 157 } 158 #else 159 phys[j].mr_start <<= 32; 160 #endif 161 phys[j].mr_start |= memoryprop[i+1]; 162 } 163 phys[j].mr_size = memoryprop[i + physacells]; 164 } 165 *physsz = j; 166 167 /* "available" always has #address-cells = 1 */ 168 propsize = OF_getprop(memory, "available", memoryprop, 169 sizeof(memoryprop)); 170 if (propsize <= 0) { 171 for (i = 0; i < *physsz; i++) { 172 avail[i].mr_start = phys[i].mr_start; 173 avail[i].mr_size = phys[i].mr_size; 174 } 175 176 *availsz = *physsz; 177 } else { 178 propsize /= sizeof(cell_t); 179 for (i = 0, j = 0; i < propsize; i += 2, j++) { 180 avail[j].mr_start = memoryprop[i]; 181 avail[j].mr_size = memoryprop[i + 1]; 182 } 183 184 #ifdef __powerpc64__ 185 /* Add in regions above 4 GB to the available list */ 186 for (i = 0; i < *physsz; i++) { 187 if (phys[i].mr_start > BUS_SPACE_MAXADDR_32BIT) { 188 avail[j].mr_start = phys[i].mr_start; 189 avail[j].mr_size = phys[i].mr_size; 190 j++; 191 } 192 } 193 #endif 194 *availsz = j; 195 } 196 } 197 198 static int 199 powermac_attach(platform_t plat) 200 { 201 phandle_t rootnode; 202 char model[32]; 203 204 /* 205 * Quiesce Open Firmware on PowerMac11,2 and 12,1. It is 206 * necessary there to shut down a background thread doing fan 207 * management, and is harmful on other machines (it will make OF 208 * shut off power to various system components it had turned on). 209 * 210 * Note: we don't need to worry about which OF module we are 211 * using since this is called only from very early boot, within 212 * OF's boot context. 213 */ 214 215 rootnode = OF_finddevice("/"); 216 if (OF_getprop(rootnode, "model", model, sizeof(model)) > 0) { 217 if (strcmp(model, "PowerMac11,2") == 0 || 218 strcmp(model, "PowerMac12,1") == 0) { 219 ofw_quiesce(); 220 } 221 } 222 223 return (0); 224 } 225 226 static u_long 227 powermac_timebase_freq(platform_t plat, struct cpuref *cpuref) 228 { 229 phandle_t phandle; 230 int32_t ticks = -1; 231 232 phandle = cpuref->cr_hwref; 233 234 OF_getprop(phandle, "timebase-frequency", &ticks, sizeof(ticks)); 235 236 if (ticks <= 0) 237 panic("Unable to determine timebase frequency!"); 238 239 return (ticks); 240 } 241 242 static int 243 powermac_smp_fill_cpuref(struct cpuref *cpuref, phandle_t cpu) 244 { 245 cell_t cpuid; 246 int res; 247 248 cpuref->cr_hwref = cpu; 249 res = OF_getprop(cpu, "reg", &cpuid, sizeof(cpuid)); 250 251 /* 252 * psim doesn't have a reg property, so assume 0 as for the 253 * uniprocessor case in the CHRP spec. 254 */ 255 if (res < 0) { 256 cpuid = 0; 257 } 258 259 cpuref->cr_cpuid = cpuid & 0xff; 260 return (0); 261 } 262 263 static int 264 powermac_smp_first_cpu(platform_t plat, struct cpuref *cpuref) 265 { 266 char buf[8]; 267 phandle_t cpu, dev, root; 268 int res; 269 270 root = OF_peer(0); 271 272 dev = OF_child(root); 273 while (dev != 0) { 274 res = OF_getprop(dev, "name", buf, sizeof(buf)); 275 if (res > 0 && strcmp(buf, "cpus") == 0) 276 break; 277 dev = OF_peer(dev); 278 } 279 if (dev == 0) { 280 /* 281 * psim doesn't have a name property on the /cpus node, 282 * but it can be found directly 283 */ 284 dev = OF_finddevice("/cpus"); 285 if (dev == -1) 286 return (ENOENT); 287 } 288 289 cpu = OF_child(dev); 290 291 while (cpu != 0) { 292 res = OF_getprop(cpu, "device_type", buf, sizeof(buf)); 293 if (res > 0 && strcmp(buf, "cpu") == 0) 294 break; 295 cpu = OF_peer(cpu); 296 } 297 if (cpu == 0) 298 return (ENOENT); 299 300 return (powermac_smp_fill_cpuref(cpuref, cpu)); 301 } 302 303 static int 304 powermac_smp_next_cpu(platform_t plat, struct cpuref *cpuref) 305 { 306 char buf[8]; 307 phandle_t cpu; 308 int res; 309 310 cpu = OF_peer(cpuref->cr_hwref); 311 while (cpu != 0) { 312 res = OF_getprop(cpu, "device_type", buf, sizeof(buf)); 313 if (res > 0 && strcmp(buf, "cpu") == 0) 314 break; 315 cpu = OF_peer(cpu); 316 } 317 if (cpu == 0) 318 return (ENOENT); 319 320 return (powermac_smp_fill_cpuref(cpuref, cpu)); 321 } 322 323 static int 324 powermac_smp_get_bsp(platform_t plat, struct cpuref *cpuref) 325 { 326 ihandle_t inst; 327 phandle_t bsp, chosen; 328 int res; 329 330 chosen = OF_finddevice("/chosen"); 331 if (chosen == -1) 332 return (ENXIO); 333 334 res = OF_getprop(chosen, "cpu", &inst, sizeof(inst)); 335 if (res < 0) 336 return (ENXIO); 337 338 bsp = OF_instance_to_package(inst); 339 return (powermac_smp_fill_cpuref(cpuref, bsp)); 340 } 341 342 static int 343 powermac_smp_start_cpu(platform_t plat, struct pcpu *pc) 344 { 345 #ifdef SMP 346 phandle_t cpu; 347 volatile uint8_t *rstvec; 348 static volatile uint8_t *rstvec_virtbase = NULL; 349 int res, reset, timeout; 350 351 cpu = pc->pc_hwref; 352 res = OF_getprop(cpu, "soft-reset", &reset, sizeof(reset)); 353 if (res < 0) { 354 reset = 0x58; 355 356 switch (pc->pc_cpuid) { 357 case 0: 358 reset += 0x03; 359 break; 360 case 1: 361 reset += 0x04; 362 break; 363 case 2: 364 reset += 0x0f; 365 break; 366 case 3: 367 reset += 0x10; 368 break; 369 default: 370 return (ENXIO); 371 } 372 } 373 374 ap_pcpu = pc; 375 376 if (rstvec_virtbase == NULL) 377 rstvec_virtbase = pmap_mapdev(0x80000000, PAGE_SIZE); 378 379 rstvec = rstvec_virtbase + reset; 380 381 *rstvec = 4; 382 powerpc_sync(); 383 (void)(*rstvec); 384 powerpc_sync(); 385 DELAY(1); 386 *rstvec = 0; 387 powerpc_sync(); 388 (void)(*rstvec); 389 powerpc_sync(); 390 391 timeout = 10000; 392 while (!pc->pc_awake && timeout--) 393 DELAY(100); 394 395 return ((pc->pc_awake) ? 0 : EBUSY); 396 #else 397 /* No SMP support */ 398 return (ENXIO); 399 #endif 400 } 401 402 void 403 powermac_register_timebase(device_t dev, powermac_tb_disable_t cb) 404 { 405 powermac_tb_dev = dev; 406 freeze_timebase = cb; 407 } 408 409 static void 410 powermac_smp_timebase_sync(platform_t plat, u_long tb, int ap) 411 { 412 static volatile bool tb_ready; 413 static volatile int cpu_done; 414 415 /* 416 * XXX Temporary fallback for platforms we don't know how to freeze. 417 * 418 * This needs to be replaced with a cpu-to-cpu software sync 419 * protocol, because this is not a consistent way to sync timebase. 420 */ 421 mttb(tb); 422 if (freeze_timebase == dummy_timebase) 423 return; 424 425 if (ap) { 426 /* APs. Hold off until we get a stable timebase. */ 427 critical_enter(); 428 while (!tb_ready) 429 atomic_thread_fence_seq_cst(); 430 mttb(tb); 431 atomic_add_int(&cpu_done, 1); 432 while (cpu_done < mp_ncpus) 433 atomic_thread_fence_seq_cst(); 434 critical_exit(); 435 } else { 436 /* BSP */ 437 critical_enter(); 438 /* Ensure cpu_done is zeroed so we can resync at runtime */ 439 atomic_set_int(&cpu_done, 0); 440 freeze_timebase(powermac_tb_dev, true); 441 tb_ready = true; 442 mttb(tb); 443 atomic_add_int(&cpu_done, 1); 444 while (cpu_done < mp_ncpus) 445 atomic_thread_fence_seq_cst(); 446 freeze_timebase(powermac_tb_dev, false); 447 /* Reset tb_ready so we can resync at runtime */ 448 tb_ready = false; 449 critical_exit(); 450 } 451 } 452 453 /* Fallback freeze. In case no real handler is found in the device tree. */ 454 static void 455 dummy_timebase(device_t dev, bool freeze) 456 { 457 /* Nothing to do here, move along. */ 458 } 459 460 static void 461 powermac_reset(platform_t platform) 462 { 463 OF_reboot(); 464 } 465 466 #ifndef __powerpc64__ 467 void 468 powermac_sleep(platform_t platform) 469 { 470 /* Only supports MPC745x for now. */ 471 if (!MPC745X_P(mfspr(SPR_PVR) >> 16)) { 472 printf("sleep only supported for G4 PowerMac hardware.\n"); 473 return; 474 } 475 476 *(unsigned long *)0x80 = 0x100; 477 mpc745x_sleep(); 478 } 479 #endif 480