xref: /freebsd/sys/powerpc/powermac/platform_powermac.c (revision de2dd83fb9a41d39ca41efc734b8f940e3eec29a)
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$");
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/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 "platform_if.h"
55 
56 extern void *ap_pcpu;
57 
58 static int powermac_probe(platform_t);
59 static int powermac_attach(platform_t);
60 void powermac_mem_regions(platform_t, struct mem_region *phys, int *physsz,
61     struct mem_region *avail, int *availsz);
62 static u_long powermac_timebase_freq(platform_t, struct cpuref *cpuref);
63 static int powermac_smp_first_cpu(platform_t, struct cpuref *cpuref);
64 static int powermac_smp_next_cpu(platform_t, struct cpuref *cpuref);
65 static int powermac_smp_get_bsp(platform_t, struct cpuref *cpuref);
66 static int powermac_smp_start_cpu(platform_t, struct pcpu *cpu);
67 static void powermac_smp_timebase_sync(platform_t, u_long tb, int ap);
68 static void powermac_reset(platform_t);
69 static void powermac_sleep(platform_t);
70 
71 static 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 	PLATFORMMETHOD(platform_smp_timebase_sync, powermac_smp_timebase_sync),
82 
83 	PLATFORMMETHOD(platform_reset,		powermac_reset),
84 	PLATFORMMETHOD(platform_sleep,		powermac_sleep),
85 
86 	PLATFORMMETHOD_END
87 };
88 
89 static platform_def_t powermac_platform = {
90 	"powermac",
91 	powermac_methods,
92 	0
93 };
94 
95 PLATFORM_DEF(powermac_platform);
96 
97 static int
98 powermac_probe(platform_t plat)
99 {
100 	char compat[255];
101 	ssize_t compatlen;
102 	char *curstr;
103 	phandle_t root;
104 
105 	root = OF_peer(0);
106 	if (root == 0)
107 		return (ENXIO);
108 
109 	compatlen = OF_getprop(root, "compatible", compat, sizeof(compat));
110 
111 	for (curstr = compat; curstr < compat + compatlen;
112 	    curstr += strlen(curstr) + 1) {
113 		if (strncmp(curstr, "MacRISC", 7) == 0)
114 			return (BUS_PROBE_SPECIFIC);
115 	}
116 
117 	return (ENXIO);
118 }
119 
120 void
121 powermac_mem_regions(platform_t plat, struct mem_region *phys, int *physsz,
122     struct mem_region *avail, int *availsz)
123 {
124 	phandle_t memory;
125 	cell_t memoryprop[PHYS_AVAIL_SZ * 2];
126 	ssize_t propsize, i, j;
127 	int physacells = 1;
128 
129 	memory = OF_finddevice("/memory");
130 	if (memory == -1)
131 		memory = OF_finddevice("/memory@0");
132 
133 	/* "reg" has variable #address-cells, but #size-cells is always 1 */
134 	OF_getprop(OF_parent(memory), "#address-cells", &physacells,
135 	    sizeof(physacells));
136 
137 	propsize = OF_getprop(memory, "reg", memoryprop, sizeof(memoryprop));
138 	propsize /= sizeof(cell_t);
139 	for (i = 0, j = 0; i < propsize; i += physacells+1, j++) {
140 		phys[j].mr_start = memoryprop[i];
141 		if (physacells == 2) {
142 #ifndef __powerpc64__
143 			/* On 32-bit PPC, ignore regions starting above 4 GB */
144 			if (memoryprop[i] != 0) {
145 				j--;
146 				continue;
147 			}
148 #else
149 			phys[j].mr_start <<= 32;
150 #endif
151 			phys[j].mr_start |= memoryprop[i+1];
152 		}
153 		phys[j].mr_size = memoryprop[i + physacells];
154 	}
155 	*physsz = j;
156 
157 	/* "available" always has #address-cells = 1 */
158 	propsize = OF_getprop(memory, "available", memoryprop,
159 	    sizeof(memoryprop));
160 	if (propsize <= 0) {
161 		for (i = 0; i < *physsz; i++) {
162 			avail[i].mr_start = phys[i].mr_start;
163 			avail[i].mr_size = phys[i].mr_size;
164 		}
165 
166 		*availsz = *physsz;
167 	} else {
168 		propsize /= sizeof(cell_t);
169 		for (i = 0, j = 0; i < propsize; i += 2, j++) {
170 			avail[j].mr_start = memoryprop[i];
171 			avail[j].mr_size = memoryprop[i + 1];
172 		}
173 
174 #ifdef __powerpc64__
175 		/* Add in regions above 4 GB to the available list */
176 		for (i = 0; i < *physsz; i++) {
177 			if (phys[i].mr_start > BUS_SPACE_MAXADDR_32BIT) {
178 				avail[j].mr_start = phys[i].mr_start;
179 				avail[j].mr_size = phys[i].mr_size;
180 				j++;
181 			}
182 		}
183 #endif
184 		*availsz = j;
185 	}
186 }
187 
188 static int
189 powermac_attach(platform_t plat)
190 {
191 	phandle_t rootnode;
192 	char model[32];
193 
194 
195 	/*
196 	 * Quiesce Open Firmware on PowerMac11,2 and 12,1. It is
197 	 * necessary there to shut down a background thread doing fan
198 	 * management, and is harmful on other machines (it will make OF
199 	 * shut off power to various system components it had turned on).
200 	 *
201 	 * Note: we don't need to worry about which OF module we are
202 	 * using since this is called only from very early boot, within
203 	 * OF's boot context.
204 	 */
205 
206 	rootnode = OF_finddevice("/");
207 	if (OF_getprop(rootnode, "model", model, sizeof(model)) > 0) {
208 		if (strcmp(model, "PowerMac11,2") == 0 ||
209 		    strcmp(model, "PowerMac12,1") == 0) {
210 			ofw_quiesce();
211 		}
212 	}
213 
214 	return (0);
215 }
216 
217 static u_long
218 powermac_timebase_freq(platform_t plat, struct cpuref *cpuref)
219 {
220 	phandle_t phandle;
221 	int32_t ticks = -1;
222 
223 	phandle = cpuref->cr_hwref;
224 
225 	OF_getprop(phandle, "timebase-frequency", &ticks, sizeof(ticks));
226 
227 	if (ticks <= 0)
228 		panic("Unable to determine timebase frequency!");
229 
230 	return (ticks);
231 }
232 
233 
234 static int
235 powermac_smp_fill_cpuref(struct cpuref *cpuref, phandle_t cpu)
236 {
237 	cell_t cpuid;
238 	int res;
239 
240 	cpuref->cr_hwref = cpu;
241 	res = OF_getprop(cpu, "reg", &cpuid, sizeof(cpuid));
242 
243 	/*
244 	 * psim doesn't have a reg property, so assume 0 as for the
245 	 * uniprocessor case in the CHRP spec.
246 	 */
247 	if (res < 0) {
248 		cpuid = 0;
249 	}
250 
251 	cpuref->cr_cpuid = cpuid & 0xff;
252 	return (0);
253 }
254 
255 static int
256 powermac_smp_first_cpu(platform_t plat, struct cpuref *cpuref)
257 {
258 	char buf[8];
259 	phandle_t cpu, dev, root;
260 	int res;
261 
262 	root = OF_peer(0);
263 
264 	dev = OF_child(root);
265 	while (dev != 0) {
266 		res = OF_getprop(dev, "name", buf, sizeof(buf));
267 		if (res > 0 && strcmp(buf, "cpus") == 0)
268 			break;
269 		dev = OF_peer(dev);
270 	}
271 	if (dev == 0) {
272 		/*
273 		 * psim doesn't have a name property on the /cpus node,
274 		 * but it can be found directly
275 		 */
276 		dev = OF_finddevice("/cpus");
277 		if (dev == -1)
278 			return (ENOENT);
279 	}
280 
281 	cpu = OF_child(dev);
282 
283 	while (cpu != 0) {
284 		res = OF_getprop(cpu, "device_type", buf, sizeof(buf));
285 		if (res > 0 && strcmp(buf, "cpu") == 0)
286 			break;
287 		cpu = OF_peer(cpu);
288 	}
289 	if (cpu == 0)
290 		return (ENOENT);
291 
292 	return (powermac_smp_fill_cpuref(cpuref, cpu));
293 }
294 
295 static int
296 powermac_smp_next_cpu(platform_t plat, struct cpuref *cpuref)
297 {
298 	char buf[8];
299 	phandle_t cpu;
300 	int res;
301 
302 	cpu = OF_peer(cpuref->cr_hwref);
303 	while (cpu != 0) {
304 		res = OF_getprop(cpu, "device_type", buf, sizeof(buf));
305 		if (res > 0 && strcmp(buf, "cpu") == 0)
306 			break;
307 		cpu = OF_peer(cpu);
308 	}
309 	if (cpu == 0)
310 		return (ENOENT);
311 
312 	return (powermac_smp_fill_cpuref(cpuref, cpu));
313 }
314 
315 static int
316 powermac_smp_get_bsp(platform_t plat, struct cpuref *cpuref)
317 {
318 	ihandle_t inst;
319 	phandle_t bsp, chosen;
320 	int res;
321 
322 	chosen = OF_finddevice("/chosen");
323 	if (chosen == -1)
324 		return (ENXIO);
325 
326 	res = OF_getprop(chosen, "cpu", &inst, sizeof(inst));
327 	if (res < 0)
328 		return (ENXIO);
329 
330 	bsp = OF_instance_to_package(inst);
331 	return (powermac_smp_fill_cpuref(cpuref, bsp));
332 }
333 
334 static int
335 powermac_smp_start_cpu(platform_t plat, struct pcpu *pc)
336 {
337 #ifdef SMP
338 	phandle_t cpu;
339 	volatile uint8_t *rstvec;
340 	static volatile uint8_t *rstvec_virtbase = NULL;
341 	int res, reset, timeout;
342 
343 	cpu = pc->pc_hwref;
344 	res = OF_getprop(cpu, "soft-reset", &reset, sizeof(reset));
345 	if (res < 0) {
346 		reset = 0x58;
347 
348 		switch (pc->pc_cpuid) {
349 		case 0:
350 			reset += 0x03;
351 			break;
352 		case 1:
353 			reset += 0x04;
354 			break;
355 		case 2:
356 			reset += 0x0f;
357 			break;
358 		case 3:
359 			reset += 0x10;
360 			break;
361 		default:
362 			return (ENXIO);
363 		}
364 	}
365 
366 	ap_pcpu = pc;
367 
368 	if (rstvec_virtbase == NULL)
369 		rstvec_virtbase = pmap_mapdev(0x80000000, PAGE_SIZE);
370 
371 	rstvec = rstvec_virtbase + reset;
372 
373 	*rstvec = 4;
374 	powerpc_sync();
375 	(void)(*rstvec);
376 	powerpc_sync();
377 	DELAY(1);
378 	*rstvec = 0;
379 	powerpc_sync();
380 	(void)(*rstvec);
381 	powerpc_sync();
382 
383 	timeout = 10000;
384 	while (!pc->pc_awake && timeout--)
385 		DELAY(100);
386 
387 	return ((pc->pc_awake) ? 0 : EBUSY);
388 #else
389 	/* No SMP support */
390 	return (ENXIO);
391 #endif
392 }
393 
394 static void
395 powermac_smp_timebase_sync(platform_t plat, u_long tb, int ap)
396 {
397 
398 	mttb(tb);
399 }
400 
401 static void
402 powermac_reset(platform_t platform)
403 {
404 	OF_reboot();
405 }
406 
407 void
408 powermac_sleep(platform_t platform)
409 {
410 
411 	*(unsigned long *)0x80 = 0x100;
412 	cpu_sleep();
413 }
414 
415