2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2008 Marcel Moolenaar
5 * Copyright (c) 2009 Nathan Whitehorn
8 * Redistribution and use in source and binary forms, with or without
9 * 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.
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.
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
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>
51 #include <dev/ofw/openfirm.h>
52 #include <machine/ofw_machdep.h>
54 #include <powerpc/powermac/platform_powermac.h>
56 #include "platform_if.h"
58 extern volatile void *ap_pcpu;
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;
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);
76 static void powermac_sleep(platform_t);
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),
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),
91 PLATFORMMETHOD(platform_reset, powermac_reset),
93 PLATFORMMETHOD(platform_sleep, powermac_sleep),
99 static platform_def_t powermac_platform = {
105 PLATFORM_DEF(powermac_platform);
108 powermac_probe(platform_t plat)
119 compatlen = OF_getprop(root, "compatible", compat, sizeof(compat));
121 for (curstr = compat; curstr < compat + compatlen;
122 curstr += strlen(curstr) + 1) {
123 if (strncmp(curstr, "MacRISC", 7) == 0)
124 return (BUS_PROBE_SPECIFIC);
131 powermac_mem_regions(platform_t plat, struct mem_region *phys, int *physsz,
132 struct mem_region *avail, int *availsz)
135 cell_t memoryprop[PHYS_AVAIL_SZ * 2];
136 ssize_t propsize, i, j;
139 memory = OF_finddevice("/memory");
141 memory = OF_finddevice("/memory@0");
143 /* "reg" has variable #address-cells, but #size-cells is always 1 */
144 OF_getprop(OF_parent(memory), "#address-cells", &physacells,
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) {
159 phys[j].mr_start <<= 32;
161 phys[j].mr_start |= memoryprop[i+1];
163 phys[j].mr_size = memoryprop[i + physacells];
167 /* "available" always has #address-cells = 1 */
168 propsize = OF_getprop(memory, "available", memoryprop,
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;
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];
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;
199 powermac_attach(platform_t plat)
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).
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
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) {
227 powermac_timebase_freq(platform_t plat, struct cpuref *cpuref)
232 phandle = cpuref->cr_hwref;
234 OF_getprop(phandle, "timebase-frequency", &ticks, sizeof(ticks));
237 panic("Unable to determine timebase frequency!");
243 powermac_smp_fill_cpuref(struct cpuref *cpuref, phandle_t cpu)
248 cpuref->cr_hwref = cpu;
249 res = OF_getprop(cpu, "reg", &cpuid, sizeof(cpuid));
252 * psim doesn't have a reg property, so assume 0 as for the
253 * uniprocessor case in the CHRP spec.
259 cpuref->cr_cpuid = cpuid & 0xff;
264 powermac_smp_first_cpu(platform_t plat, struct cpuref *cpuref)
267 phandle_t cpu, dev, root;
272 dev = OF_child(root);
274 res = OF_getprop(dev, "name", buf, sizeof(buf));
275 if (res > 0 && strcmp(buf, "cpus") == 0)
281 * psim doesn't have a name property on the /cpus node,
282 * but it can be found directly
284 dev = OF_finddevice("/cpus");
292 res = OF_getprop(cpu, "device_type", buf, sizeof(buf));
293 if (res > 0 && strcmp(buf, "cpu") == 0)
300 return (powermac_smp_fill_cpuref(cpuref, cpu));
304 powermac_smp_next_cpu(platform_t plat, struct cpuref *cpuref)
310 cpu = OF_peer(cpuref->cr_hwref);
312 res = OF_getprop(cpu, "device_type", buf, sizeof(buf));
313 if (res > 0 && strcmp(buf, "cpu") == 0)
320 return (powermac_smp_fill_cpuref(cpuref, cpu));
324 powermac_smp_get_bsp(platform_t plat, struct cpuref *cpuref)
327 phandle_t bsp, chosen;
330 chosen = OF_finddevice("/chosen");
334 res = OF_getprop(chosen, "cpu", &inst, sizeof(inst));
338 bsp = OF_instance_to_package(inst);
339 return (powermac_smp_fill_cpuref(cpuref, bsp));
343 powermac_smp_start_cpu(platform_t plat, struct pcpu *pc)
347 volatile uint8_t *rstvec;
348 static volatile uint8_t *rstvec_virtbase = NULL;
349 int res, reset, timeout;
352 res = OF_getprop(cpu, "soft-reset", &reset, sizeof(reset));
356 switch (pc->pc_cpuid) {
376 if (rstvec_virtbase == NULL)
377 rstvec_virtbase = pmap_mapdev(0x80000000, PAGE_SIZE);
379 rstvec = rstvec_virtbase + reset;
392 while (!pc->pc_awake && timeout--)
395 return ((pc->pc_awake) ? 0 : EBUSY);
403 powermac_register_timebase(device_t dev, powermac_tb_disable_t cb)
405 powermac_tb_dev = dev;
406 freeze_timebase = cb;
410 powermac_smp_timebase_sync(platform_t plat, u_long tb, int ap)
412 static volatile bool tb_ready;
413 static volatile int cpu_done;
416 * XXX Temporary fallback for platforms we don't know how to freeze.
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.
422 if (freeze_timebase == dummy_timebase)
426 /* APs. Hold off until we get a stable timebase. */
429 atomic_thread_fence_seq_cst();
431 atomic_add_int(&cpu_done, 1);
432 while (cpu_done < mp_ncpus)
433 atomic_thread_fence_seq_cst();
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);
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 */
453 /* Fallback freeze. In case no real handler is found in the device tree. */
455 dummy_timebase(device_t dev, bool freeze)
457 /* Nothing to do here, move along. */
461 powermac_reset(platform_t platform)
466 #ifndef __powerpc64__
468 powermac_sleep(platform_t platform)
470 /* Only supports MPC745x for now. */
471 if (!MPC745X_P(mfspr(SPR_PVR) >> 16)) {
472 printf("sleep only supported for G4 PowerMac hardware.\n");
476 *(unsigned long *)0x80 = 0x100;