2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
43 #include <machine/altivec.h> /* For save_vec() */
44 #include <machine/bus.h>
45 #include <machine/cpu.h>
46 #include <machine/fpu.h> /* For save_fpu() */
47 #include <machine/hid.h>
48 #include <machine/platformvar.h>
49 #include <machine/setjmp.h>
50 #include <machine/smp.h>
51 #include <machine/spr.h>
53 #include <dev/ofw/openfirm.h>
54 #include <machine/ofw_machdep.h>
56 #include <powerpc/powermac/platform_powermac.h>
58 #include "platform_if.h"
60 extern volatile void *ap_pcpu;
62 static void dummy_timebase(device_t, bool);
63 static device_t powermac_tb_dev;
64 static void (*freeze_timebase)(device_t, bool) = dummy_timebase;
66 static int powermac_probe(platform_t);
67 static int powermac_attach(platform_t);
68 void powermac_mem_regions(platform_t, struct mem_region *phys, int *physsz,
69 struct mem_region *avail, int *availsz);
70 static u_long powermac_timebase_freq(platform_t, struct cpuref *cpuref);
71 static int powermac_smp_first_cpu(platform_t, struct cpuref *cpuref);
72 static int powermac_smp_next_cpu(platform_t, struct cpuref *cpuref);
73 static int powermac_smp_get_bsp(platform_t, struct cpuref *cpuref);
74 static int powermac_smp_start_cpu(platform_t, struct pcpu *cpu);
75 static void powermac_smp_timebase_sync(platform_t, u_long tb, int ap);
76 static void powermac_reset(platform_t);
78 static void powermac_sleep(platform_t);
81 static platform_method_t powermac_methods[] = {
82 PLATFORMMETHOD(platform_probe, powermac_probe),
83 PLATFORMMETHOD(platform_attach, powermac_attach),
84 PLATFORMMETHOD(platform_mem_regions, powermac_mem_regions),
85 PLATFORMMETHOD(platform_timebase_freq, powermac_timebase_freq),
87 PLATFORMMETHOD(platform_smp_first_cpu, powermac_smp_first_cpu),
88 PLATFORMMETHOD(platform_smp_next_cpu, powermac_smp_next_cpu),
89 PLATFORMMETHOD(platform_smp_get_bsp, powermac_smp_get_bsp),
90 PLATFORMMETHOD(platform_smp_start_cpu, powermac_smp_start_cpu),
91 PLATFORMMETHOD(platform_smp_timebase_sync, powermac_smp_timebase_sync),
93 PLATFORMMETHOD(platform_reset, powermac_reset),
95 PLATFORMMETHOD(platform_sleep, powermac_sleep),
101 static platform_def_t powermac_platform = {
107 PLATFORM_DEF(powermac_platform);
110 powermac_probe(platform_t plat)
121 compatlen = OF_getprop(root, "compatible", compat, sizeof(compat));
123 for (curstr = compat; curstr < compat + compatlen;
124 curstr += strlen(curstr) + 1) {
125 if (strncmp(curstr, "MacRISC", 7) == 0)
126 return (BUS_PROBE_SPECIFIC);
133 powermac_mem_regions(platform_t plat, struct mem_region *phys, int *physsz,
134 struct mem_region *avail, int *availsz)
137 cell_t memoryprop[PHYS_AVAIL_SZ * 2];
138 ssize_t propsize, i, j;
141 memory = OF_finddevice("/memory");
143 memory = OF_finddevice("/memory@0");
145 /* "reg" has variable #address-cells, but #size-cells is always 1 */
146 OF_getprop(OF_parent(memory), "#address-cells", &physacells,
149 propsize = OF_getprop(memory, "reg", memoryprop, sizeof(memoryprop));
150 propsize /= sizeof(cell_t);
151 for (i = 0, j = 0; i < propsize; i += physacells+1, j++) {
152 phys[j].mr_start = memoryprop[i];
153 if (physacells == 2) {
154 #ifndef __powerpc64__
155 /* On 32-bit PPC, ignore regions starting above 4 GB */
156 if (memoryprop[i] != 0) {
161 phys[j].mr_start <<= 32;
163 phys[j].mr_start |= memoryprop[i+1];
165 phys[j].mr_size = memoryprop[i + physacells];
169 /* "available" always has #address-cells = 1 */
170 propsize = OF_getprop(memory, "available", memoryprop,
173 for (i = 0; i < *physsz; i++) {
174 avail[i].mr_start = phys[i].mr_start;
175 avail[i].mr_size = phys[i].mr_size;
180 propsize /= sizeof(cell_t);
181 for (i = 0, j = 0; i < propsize; i += 2, j++) {
182 avail[j].mr_start = memoryprop[i];
183 avail[j].mr_size = memoryprop[i + 1];
187 /* Add in regions above 4 GB to the available list */
188 for (i = 0; i < *physsz; i++) {
189 if (phys[i].mr_start > BUS_SPACE_MAXADDR_32BIT) {
190 avail[j].mr_start = phys[i].mr_start;
191 avail[j].mr_size = phys[i].mr_size;
201 powermac_attach(platform_t plat)
207 * Quiesce Open Firmware on PowerMac11,2 and 12,1. It is
208 * necessary there to shut down a background thread doing fan
209 * management, and is harmful on other machines (it will make OF
210 * shut off power to various system components it had turned on).
212 * Note: we don't need to worry about which OF module we are
213 * using since this is called only from very early boot, within
217 rootnode = OF_finddevice("/");
218 if (OF_getprop(rootnode, "model", model, sizeof(model)) > 0) {
219 if (strcmp(model, "PowerMac11,2") == 0 ||
220 strcmp(model, "PowerMac12,1") == 0) {
229 powermac_timebase_freq(platform_t plat, struct cpuref *cpuref)
234 phandle = cpuref->cr_hwref;
236 OF_getprop(phandle, "timebase-frequency", &ticks, sizeof(ticks));
239 panic("Unable to determine timebase frequency!");
245 powermac_smp_fill_cpuref(struct cpuref *cpuref, phandle_t cpu)
250 cpuref->cr_hwref = cpu;
251 res = OF_getprop(cpu, "reg", &cpuid, sizeof(cpuid));
254 * psim doesn't have a reg property, so assume 0 as for the
255 * uniprocessor case in the CHRP spec.
261 cpuref->cr_cpuid = cpuid & 0xff;
266 powermac_smp_first_cpu(platform_t plat, struct cpuref *cpuref)
269 phandle_t cpu, dev, root;
274 dev = OF_child(root);
276 res = OF_getprop(dev, "name", buf, sizeof(buf));
277 if (res > 0 && strcmp(buf, "cpus") == 0)
283 * psim doesn't have a name property on the /cpus node,
284 * but it can be found directly
286 dev = OF_finddevice("/cpus");
294 res = OF_getprop(cpu, "device_type", buf, sizeof(buf));
295 if (res > 0 && strcmp(buf, "cpu") == 0)
302 return (powermac_smp_fill_cpuref(cpuref, cpu));
306 powermac_smp_next_cpu(platform_t plat, struct cpuref *cpuref)
312 cpu = OF_peer(cpuref->cr_hwref);
314 res = OF_getprop(cpu, "device_type", buf, sizeof(buf));
315 if (res > 0 && strcmp(buf, "cpu") == 0)
322 return (powermac_smp_fill_cpuref(cpuref, cpu));
326 powermac_smp_get_bsp(platform_t plat, struct cpuref *cpuref)
329 phandle_t bsp, chosen;
332 chosen = OF_finddevice("/chosen");
336 res = OF_getprop(chosen, "cpu", &inst, sizeof(inst));
340 bsp = OF_instance_to_package(inst);
341 return (powermac_smp_fill_cpuref(cpuref, bsp));
345 powermac_smp_start_cpu(platform_t plat, struct pcpu *pc)
349 volatile uint8_t *rstvec;
350 static volatile uint8_t *rstvec_virtbase = NULL;
351 int res, reset, timeout;
354 res = OF_getprop(cpu, "soft-reset", &reset, sizeof(reset));
358 switch (pc->pc_cpuid) {
378 if (rstvec_virtbase == NULL)
379 rstvec_virtbase = pmap_mapdev(0x80000000, PAGE_SIZE);
381 rstvec = rstvec_virtbase + reset;
394 while (!pc->pc_awake && timeout--)
397 return ((pc->pc_awake) ? 0 : EBUSY);
405 powermac_register_timebase(device_t dev, powermac_tb_disable_t cb)
407 powermac_tb_dev = dev;
408 freeze_timebase = cb;
412 powermac_smp_timebase_sync(platform_t plat, u_long tb, int ap)
414 static volatile bool tb_ready;
415 static volatile int cpu_done;
418 * XXX Temporary fallback for platforms we don't know how to freeze.
420 * This needs to be replaced with a cpu-to-cpu software sync
421 * protocol, because this is not a consistent way to sync timebase.
424 if (freeze_timebase == dummy_timebase)
428 /* APs. Hold off until we get a stable timebase. */
431 atomic_thread_fence_seq_cst();
433 atomic_add_int(&cpu_done, 1);
434 while (cpu_done < mp_ncpus)
435 atomic_thread_fence_seq_cst();
440 /* Ensure cpu_done is zeroed so we can resync at runtime */
441 atomic_set_int(&cpu_done, 0);
442 freeze_timebase(powermac_tb_dev, true);
445 atomic_add_int(&cpu_done, 1);
446 while (cpu_done < mp_ncpus)
447 atomic_thread_fence_seq_cst();
448 freeze_timebase(powermac_tb_dev, false);
449 /* Reset tb_ready so we can resync at runtime */
455 /* Fallback freeze. In case no real handler is found in the device tree. */
457 dummy_timebase(device_t dev, bool freeze)
459 /* Nothing to do here, move along. */
463 powermac_reset(platform_t platform)
468 #ifndef __powerpc64__
470 powermac_sleep(platform_t platform)
472 /* Only supports MPC745x for now. */
473 if (!MPC745X_P(mfspr(SPR_PVR) >> 16)) {
474 printf("sleep only supported for G4 PowerMac hardware.\n");
478 *(unsigned long *)0x80 = 0x100;