platform_powermac.c revision 262675
1/*-
2 * Copyright (c) 2008 Marcel Moolenaar
3 * Copyright (c) 2009 Nathan Whitehorn
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD: stable/10/sys/powerpc/powermac/platform_powermac.c 262675 2014-03-02 02:35:46Z jhibbits $");
30
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/pmap.h>
48#include <machine/setjmp.h>
49#include <machine/smp.h>
50#include <machine/spr.h>
51
52#include <dev/ofw/openfirm.h>
53#include <machine/ofw_machdep.h>
54
55#include "platform_if.h"
56
57extern void *ap_pcpu;
58
59static int powermac_probe(platform_t);
60static int powermac_attach(platform_t);
61void powermac_mem_regions(platform_t, struct mem_region **phys, int *physsz,
62    struct mem_region **avail, int *availsz);
63static u_long powermac_timebase_freq(platform_t, struct cpuref *cpuref);
64static int powermac_smp_first_cpu(platform_t, struct cpuref *cpuref);
65static int powermac_smp_next_cpu(platform_t, struct cpuref *cpuref);
66static int powermac_smp_get_bsp(platform_t, struct cpuref *cpuref);
67static int powermac_smp_start_cpu(platform_t, struct pcpu *cpu);
68static void powermac_reset(platform_t);
69static void powermac_sleep(platform_t);
70
71static platform_method_t powermac_methods[] = {
72	PLATFORMMETHOD(platform_probe, 		powermac_probe),
73	PLATFORMMETHOD(platform_attach,		powermac_attach),
74	PLATFORMMETHOD(platform_mem_regions,	powermac_mem_regions),
75	PLATFORMMETHOD(platform_timebase_freq,	powermac_timebase_freq),
76
77	PLATFORMMETHOD(platform_smp_first_cpu,	powermac_smp_first_cpu),
78	PLATFORMMETHOD(platform_smp_next_cpu,	powermac_smp_next_cpu),
79	PLATFORMMETHOD(platform_smp_get_bsp,	powermac_smp_get_bsp),
80	PLATFORMMETHOD(platform_smp_start_cpu,	powermac_smp_start_cpu),
81
82	PLATFORMMETHOD(platform_reset,		powermac_reset),
83	PLATFORMMETHOD(platform_sleep,		powermac_sleep),
84
85	PLATFORMMETHOD_END
86};
87
88static platform_def_t powermac_platform = {
89	"powermac",
90	powermac_methods,
91	0
92};
93
94PLATFORM_DEF(powermac_platform);
95
96static int
97powermac_probe(platform_t plat)
98{
99	char compat[255];
100	ssize_t compatlen;
101	char *curstr;
102	phandle_t root;
103
104	root = OF_peer(0);
105	if (root == 0)
106		return (ENXIO);
107
108	compatlen = OF_getprop(root, "compatible", compat, sizeof(compat));
109
110	for (curstr = compat; curstr < compat + compatlen;
111	    curstr += strlen(curstr) + 1) {
112		if (strncmp(curstr, "MacRISC", 7) == 0)
113			return (BUS_PROBE_SPECIFIC);
114	}
115
116	return (ENXIO);
117}
118
119void
120powermac_mem_regions(platform_t plat, struct mem_region **phys, int *physsz,
121    struct mem_region **avail, int *availsz)
122{
123	ofw_mem_regions(phys,physsz,avail,availsz);
124}
125
126static int
127powermac_attach(platform_t plat)
128{
129	phandle_t rootnode;
130	char model[32];
131
132
133	/*
134	 * Quiesce Open Firmware on PowerMac11,2 and 12,1. It is
135	 * necessary there to shut down a background thread doing fan
136	 * management, and is harmful on other machines (it will make OF
137	 * shut off power to various system components it had turned on).
138	 *
139	 * Note: we don't need to worry about which OF module we are
140	 * using since this is called only from very early boot, within
141	 * OF's boot context.
142	 */
143
144	rootnode = OF_finddevice("/");
145	if (OF_getprop(rootnode, "model", model, sizeof(model)) > 0) {
146		if (strcmp(model, "PowerMac11,2") == 0 ||
147		    strcmp(model, "PowerMac12,1") == 0) {
148			ofw_quiesce();
149		}
150	}
151
152	return (0);
153}
154
155static u_long
156powermac_timebase_freq(platform_t plat, struct cpuref *cpuref)
157{
158	phandle_t phandle;
159	int32_t ticks = -1;
160
161	phandle = cpuref->cr_hwref;
162
163	OF_getprop(phandle, "timebase-frequency", &ticks, sizeof(ticks));
164
165	if (ticks <= 0)
166		panic("Unable to determine timebase frequency!");
167
168	return (ticks);
169}
170
171
172static int
173powermac_smp_fill_cpuref(struct cpuref *cpuref, phandle_t cpu)
174{
175	cell_t cpuid;
176	int res;
177
178	cpuref->cr_hwref = cpu;
179	res = OF_getprop(cpu, "reg", &cpuid, sizeof(cpuid));
180
181	/*
182	 * psim doesn't have a reg property, so assume 0 as for the
183	 * uniprocessor case in the CHRP spec.
184	 */
185	if (res < 0) {
186		cpuid = 0;
187	}
188
189	cpuref->cr_cpuid = cpuid & 0xff;
190	return (0);
191}
192
193static int
194powermac_smp_first_cpu(platform_t plat, struct cpuref *cpuref)
195{
196	char buf[8];
197	phandle_t cpu, dev, root;
198	int res;
199
200	root = OF_peer(0);
201
202	dev = OF_child(root);
203	while (dev != 0) {
204		res = OF_getprop(dev, "name", buf, sizeof(buf));
205		if (res > 0 && strcmp(buf, "cpus") == 0)
206			break;
207		dev = OF_peer(dev);
208	}
209	if (dev == 0) {
210		/*
211		 * psim doesn't have a name property on the /cpus node,
212		 * but it can be found directly
213		 */
214		dev = OF_finddevice("/cpus");
215		if (dev == -1)
216			return (ENOENT);
217	}
218
219	cpu = OF_child(dev);
220
221	while (cpu != 0) {
222		res = OF_getprop(cpu, "device_type", buf, sizeof(buf));
223		if (res > 0 && strcmp(buf, "cpu") == 0)
224			break;
225		cpu = OF_peer(cpu);
226	}
227	if (cpu == 0)
228		return (ENOENT);
229
230	return (powermac_smp_fill_cpuref(cpuref, cpu));
231}
232
233static int
234powermac_smp_next_cpu(platform_t plat, struct cpuref *cpuref)
235{
236	char buf[8];
237	phandle_t cpu;
238	int res;
239
240	cpu = OF_peer(cpuref->cr_hwref);
241	while (cpu != 0) {
242		res = OF_getprop(cpu, "device_type", buf, sizeof(buf));
243		if (res > 0 && strcmp(buf, "cpu") == 0)
244			break;
245		cpu = OF_peer(cpu);
246	}
247	if (cpu == 0)
248		return (ENOENT);
249
250	return (powermac_smp_fill_cpuref(cpuref, cpu));
251}
252
253static int
254powermac_smp_get_bsp(platform_t plat, struct cpuref *cpuref)
255{
256	ihandle_t inst;
257	phandle_t bsp, chosen;
258	int res;
259
260	chosen = OF_finddevice("/chosen");
261	if (chosen == -1)
262		return (ENXIO);
263
264	res = OF_getprop(chosen, "cpu", &inst, sizeof(inst));
265	if (res < 0)
266		return (ENXIO);
267
268	bsp = OF_instance_to_package(inst);
269	return (powermac_smp_fill_cpuref(cpuref, bsp));
270}
271
272static int
273powermac_smp_start_cpu(platform_t plat, struct pcpu *pc)
274{
275#ifdef SMP
276	phandle_t cpu;
277	volatile uint8_t *rstvec;
278	static volatile uint8_t *rstvec_virtbase = NULL;
279	int res, reset, timeout;
280
281	cpu = pc->pc_hwref;
282	res = OF_getprop(cpu, "soft-reset", &reset, sizeof(reset));
283	if (res < 0) {
284		reset = 0x58;
285
286		switch (pc->pc_cpuid) {
287		case 0:
288			reset += 0x03;
289			break;
290		case 1:
291			reset += 0x04;
292			break;
293		case 2:
294			reset += 0x0f;
295			break;
296		case 3:
297			reset += 0x10;
298			break;
299		default:
300			return (ENXIO);
301		}
302	}
303
304	ap_pcpu = pc;
305
306	if (rstvec_virtbase == NULL)
307		rstvec_virtbase = pmap_mapdev(0x80000000, PAGE_SIZE);
308
309	rstvec = rstvec_virtbase + reset;
310
311	*rstvec = 4;
312	powerpc_sync();
313	(void)(*rstvec);
314	powerpc_sync();
315	DELAY(1);
316	*rstvec = 0;
317	powerpc_sync();
318	(void)(*rstvec);
319	powerpc_sync();
320
321	timeout = 10000;
322	while (!pc->pc_awake && timeout--)
323		DELAY(100);
324
325	return ((pc->pc_awake) ? 0 : EBUSY);
326#else
327	/* No SMP support */
328	return (ENXIO);
329#endif
330}
331
332static void
333powermac_reset(platform_t platform)
334{
335	OF_reboot();
336}
337
338void
339powermac_sleep(platform_t platform)
340{
341
342	*(unsigned long *)0x80 = 0x100;
343	cpu_sleep();
344}
345
346