1217044Snwhitehorn/*-
2217044Snwhitehorn * Copyright (c) 2010 Nathan Whitehorn
3217044Snwhitehorn * All rights reserved.
4217044Snwhitehorn *
5217044Snwhitehorn * Redistribution and use in source and binary forms, with or without
6217044Snwhitehorn * modification, are permitted provided that the following conditions
7217044Snwhitehorn * are met:
8217044Snwhitehorn *
9217044Snwhitehorn * 1. Redistributions of source code must retain the above copyright
10217044Snwhitehorn *    notice, this list of conditions and the following disclaimer.
11217044Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright
12217044Snwhitehorn *    notice, this list of conditions and the following disclaimer in the
13217044Snwhitehorn *    documentation and/or other materials provided with the distribution.
14217044Snwhitehorn *
15217044Snwhitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16217044Snwhitehorn * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17217044Snwhitehorn * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18217044Snwhitehorn * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19217044Snwhitehorn * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20217044Snwhitehorn * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21217044Snwhitehorn * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22217044Snwhitehorn * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23217044Snwhitehorn * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24217044Snwhitehorn * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25217044Snwhitehorn */
26217044Snwhitehorn
27217044Snwhitehorn#include <sys/cdefs.h>
28217044Snwhitehorn__FBSDID("$FreeBSD: releng/11.0/sys/powerpc/ps3/platform_ps3.c 295880 2016-02-22 09:02:20Z skra $");
29217044Snwhitehorn
30217044Snwhitehorn#include <sys/param.h>
31217044Snwhitehorn#include <sys/systm.h>
32217044Snwhitehorn#include <sys/kernel.h>
33217044Snwhitehorn#include <sys/bus.h>
34217044Snwhitehorn#include <sys/pcpu.h>
35217044Snwhitehorn#include <sys/proc.h>
36217044Snwhitehorn#include <sys/reboot.h>
37217044Snwhitehorn#include <sys/smp.h>
38217044Snwhitehorn
39217044Snwhitehorn#include <vm/vm.h>
40217044Snwhitehorn#include <vm/pmap.h>
41217044Snwhitehorn
42217044Snwhitehorn#include <machine/bus.h>
43217044Snwhitehorn#include <machine/cpu.h>
44217044Snwhitehorn#include <machine/hid.h>
45217044Snwhitehorn#include <machine/platform.h>
46217044Snwhitehorn#include <machine/platformvar.h>
47217044Snwhitehorn#include <machine/smp.h>
48217044Snwhitehorn#include <machine/spr.h>
49217044Snwhitehorn#include <machine/vmparam.h>
50217044Snwhitehorn
51276680Snwhitehorn#include <dev/ofw/openfirm.h>
52276680Snwhitehorn
53217044Snwhitehorn#include "platform_if.h"
54217044Snwhitehorn#include "ps3-hvcall.h"
55217044Snwhitehorn
56217044Snwhitehorn#ifdef SMP
57217044Snwhitehornextern void *ap_pcpu;
58217044Snwhitehorn#endif
59217044Snwhitehorn
60217044Snwhitehornstatic int ps3_probe(platform_t);
61217044Snwhitehornstatic int ps3_attach(platform_t);
62258807Snwhitehornstatic void ps3_mem_regions(platform_t, struct mem_region *phys, int *physsz,
63258807Snwhitehorn    struct mem_region *avail, int *availsz);
64217044Snwhitehornstatic vm_offset_t ps3_real_maxaddr(platform_t);
65217044Snwhitehornstatic u_long ps3_timebase_freq(platform_t, struct cpuref *cpuref);
66217044Snwhitehorn#ifdef SMP
67217044Snwhitehornstatic int ps3_smp_first_cpu(platform_t, struct cpuref *cpuref);
68217044Snwhitehornstatic int ps3_smp_next_cpu(platform_t, struct cpuref *cpuref);
69217044Snwhitehornstatic int ps3_smp_get_bsp(platform_t, struct cpuref *cpuref);
70217044Snwhitehornstatic int ps3_smp_start_cpu(platform_t, struct pcpu *cpu);
71217044Snwhitehornstatic struct cpu_group *ps3_smp_topo(platform_t);
72217044Snwhitehorn#endif
73217044Snwhitehornstatic void ps3_reset(platform_t);
74247454Sdavidestatic void ps3_cpu_idle(sbintime_t);
75217044Snwhitehorn
76217044Snwhitehornstatic platform_method_t ps3_methods[] = {
77217044Snwhitehorn	PLATFORMMETHOD(platform_probe, 		ps3_probe),
78217044Snwhitehorn	PLATFORMMETHOD(platform_attach,		ps3_attach),
79217044Snwhitehorn	PLATFORMMETHOD(platform_mem_regions,	ps3_mem_regions),
80217044Snwhitehorn	PLATFORMMETHOD(platform_real_maxaddr,	ps3_real_maxaddr),
81217044Snwhitehorn	PLATFORMMETHOD(platform_timebase_freq,	ps3_timebase_freq),
82217044Snwhitehorn
83217044Snwhitehorn#ifdef SMP
84217044Snwhitehorn	PLATFORMMETHOD(platform_smp_first_cpu,	ps3_smp_first_cpu),
85217044Snwhitehorn	PLATFORMMETHOD(platform_smp_next_cpu,	ps3_smp_next_cpu),
86217044Snwhitehorn	PLATFORMMETHOD(platform_smp_get_bsp,	ps3_smp_get_bsp),
87217044Snwhitehorn	PLATFORMMETHOD(platform_smp_start_cpu,	ps3_smp_start_cpu),
88217044Snwhitehorn	PLATFORMMETHOD(platform_smp_topo,	ps3_smp_topo),
89217044Snwhitehorn#endif
90217044Snwhitehorn
91217044Snwhitehorn	PLATFORMMETHOD(platform_reset,		ps3_reset),
92217044Snwhitehorn
93246732Srpaulo	PLATFORMMETHOD_END
94217044Snwhitehorn};
95217044Snwhitehorn
96217044Snwhitehornstatic platform_def_t ps3_platform = {
97217044Snwhitehorn	"ps3",
98217044Snwhitehorn	ps3_methods,
99217044Snwhitehorn	0
100217044Snwhitehorn};
101217044Snwhitehorn
102217044SnwhitehornPLATFORM_DEF(ps3_platform);
103217044Snwhitehorn
104217044Snwhitehornstatic int
105217044Snwhitehornps3_probe(platform_t plat)
106217044Snwhitehorn{
107276680Snwhitehorn	phandle_t root;
108276680Snwhitehorn	char compatible[64];
109217044Snwhitehorn
110276680Snwhitehorn	root = OF_finddevice("/");
111276680Snwhitehorn	if (OF_getprop(root, "compatible", compatible, sizeof(compatible)) <= 0)
112276680Snwhitehorn                return (BUS_PROBE_NOWILDCARD);
113276680Snwhitehorn
114276680Snwhitehorn	if (strncmp(compatible, "sony,ps3", sizeof(compatible)) != 0)
115276680Snwhitehorn		return (BUS_PROBE_NOWILDCARD);
116276680Snwhitehorn
117276680Snwhitehorn	return (BUS_PROBE_SPECIFIC);
118217044Snwhitehorn}
119217044Snwhitehorn
120217044Snwhitehornstatic int
121217044Snwhitehornps3_attach(platform_t plat)
122217044Snwhitehorn{
123258807Snwhitehorn
124258807Snwhitehorn	pmap_mmu_install("mmu_ps3", BUS_PROBE_SPECIFIC);
125258807Snwhitehorn	cpu_idle_hook = ps3_cpu_idle;
126258807Snwhitehorn
127258807Snwhitehorn	/* Set a breakpoint to make NULL an invalid address */
128258807Snwhitehorn	lv1_set_dabr(0x7 /* read and write, MMU on */, 2 /* kernel accesses */);
129258807Snwhitehorn
130258807Snwhitehorn	return (0);
131258807Snwhitehorn}
132258807Snwhitehorn
133258807Snwhitehornvoid
134258807Snwhitehornps3_mem_regions(platform_t plat, struct mem_region *phys, int *physsz,
135258807Snwhitehorn    struct mem_region *avail_regions, int *availsz)
136258807Snwhitehorn{
137217044Snwhitehorn	uint64_t lpar_id, junk, ppe_id;
138217044Snwhitehorn
139217044Snwhitehorn	/* Get real mode memory region */
140217044Snwhitehorn	avail_regions[0].mr_start = 0;
141217044Snwhitehorn	lv1_get_logical_partition_id(&lpar_id);
142217044Snwhitehorn	lv1_get_logical_ppe_id(&ppe_id);
143217044Snwhitehorn	lv1_get_repository_node_value(lpar_id,
144217044Snwhitehorn	    lv1_repository_string("bi") >> 32, lv1_repository_string("pu"),
145217044Snwhitehorn	    ppe_id, lv1_repository_string("rm_size"),
146217044Snwhitehorn	    &avail_regions[0].mr_size, &junk);
147217044Snwhitehorn
148217044Snwhitehorn	/* Now get extended memory region */
149217044Snwhitehorn	lv1_get_repository_node_value(lpar_id,
150217044Snwhitehorn	    lv1_repository_string("bi") >> 32,
151217044Snwhitehorn	    lv1_repository_string("rgntotal"), 0, 0,
152217044Snwhitehorn	    &avail_regions[1].mr_size, &junk);
153217044Snwhitehorn
154217044Snwhitehorn	/* Convert to maximum amount we can allocate in 16 MB pages */
155217044Snwhitehorn	avail_regions[1].mr_size -= avail_regions[0].mr_size;
156217044Snwhitehorn	avail_regions[1].mr_size -= avail_regions[1].mr_size % (16*1024*1024);
157268898Snwhitehorn
158268898Snwhitehorn	/* Allocate extended memory region */
159268898Snwhitehorn	lv1_allocate_memory(avail_regions[1].mr_size, 24 /* 16 MB pages */,
160268898Snwhitehorn	    0, 0x04 /* any address */, &avail_regions[1].mr_start, &junk);
161268898Snwhitehorn
162258807Snwhitehorn	*availsz = 2;
163217044Snwhitehorn
164258807Snwhitehorn	if (phys != NULL) {
165258807Snwhitehorn		memcpy(phys, avail_regions, sizeof(*phys)*2);
166258807Snwhitehorn		*physsz = 2;
167258807Snwhitehorn	}
168217044Snwhitehorn}
169217044Snwhitehorn
170217044Snwhitehornstatic u_long
171217044Snwhitehornps3_timebase_freq(platform_t plat, struct cpuref *cpuref)
172217044Snwhitehorn{
173217044Snwhitehorn	uint64_t ticks, node_id, junk;
174217044Snwhitehorn
175217044Snwhitehorn	lv1_get_repository_node_value(PS3_LPAR_ID_PME,
176217044Snwhitehorn	    lv1_repository_string("be") >> 32, 0, 0, 0, &node_id, &junk);
177217044Snwhitehorn	lv1_get_repository_node_value(PS3_LPAR_ID_PME,
178217044Snwhitehorn	    lv1_repository_string("be") >> 32, node_id,
179217044Snwhitehorn	    lv1_repository_string("clock"), 0, &ticks, &junk);
180217044Snwhitehorn
181217044Snwhitehorn	return (ticks);
182217044Snwhitehorn}
183217044Snwhitehorn
184217044Snwhitehorn#ifdef SMP
185217044Snwhitehornstatic int
186217044Snwhitehornps3_smp_first_cpu(platform_t plat, struct cpuref *cpuref)
187217044Snwhitehorn{
188217044Snwhitehorn
189217044Snwhitehorn	cpuref->cr_cpuid = 0;
190217044Snwhitehorn	cpuref->cr_hwref = cpuref->cr_cpuid;
191217044Snwhitehorn
192217044Snwhitehorn	return (0);
193217044Snwhitehorn}
194217044Snwhitehorn
195217044Snwhitehornstatic int
196217044Snwhitehornps3_smp_next_cpu(platform_t plat, struct cpuref *cpuref)
197217044Snwhitehorn{
198217044Snwhitehorn
199217044Snwhitehorn	if (cpuref->cr_cpuid >= 1)
200217044Snwhitehorn		return (ENOENT);
201217044Snwhitehorn
202217044Snwhitehorn	cpuref->cr_cpuid++;
203217044Snwhitehorn	cpuref->cr_hwref = cpuref->cr_cpuid;
204217044Snwhitehorn
205217044Snwhitehorn	return (0);
206217044Snwhitehorn}
207217044Snwhitehorn
208217044Snwhitehornstatic int
209217044Snwhitehornps3_smp_get_bsp(platform_t plat, struct cpuref *cpuref)
210217044Snwhitehorn{
211217044Snwhitehorn
212217044Snwhitehorn	cpuref->cr_cpuid = 0;
213217044Snwhitehorn	cpuref->cr_hwref = cpuref->cr_cpuid;
214217044Snwhitehorn
215217044Snwhitehorn	return (0);
216217044Snwhitehorn}
217217044Snwhitehorn
218217044Snwhitehornstatic int
219217044Snwhitehornps3_smp_start_cpu(platform_t plat, struct pcpu *pc)
220217044Snwhitehorn{
221217044Snwhitehorn	/* loader(8) is spinning on 0x40 == 0 right now */
222217044Snwhitehorn	uint32_t *secondary_spin_sem = (uint32_t *)(0x40);
223217044Snwhitehorn	int timeout;
224217044Snwhitehorn
225217044Snwhitehorn	if (pc->pc_hwref != 1)
226217044Snwhitehorn		return (ENXIO);
227217044Snwhitehorn
228217044Snwhitehorn	ap_pcpu = pc;
229217044Snwhitehorn	*secondary_spin_sem = 1;
230217044Snwhitehorn	powerpc_sync();
231217044Snwhitehorn	DELAY(1);
232217044Snwhitehorn
233217044Snwhitehorn	timeout = 10000;
234217044Snwhitehorn	while (!pc->pc_awake && timeout--)
235217044Snwhitehorn		DELAY(100);
236217044Snwhitehorn
237217044Snwhitehorn	return ((pc->pc_awake) ? 0 : EBUSY);
238217044Snwhitehorn}
239217044Snwhitehorn
240217044Snwhitehornstatic struct cpu_group *
241217044Snwhitehornps3_smp_topo(platform_t plat)
242217044Snwhitehorn{
243217044Snwhitehorn	return (smp_topo_1level(CG_SHARE_L1, 2, CG_FLAG_SMT));
244217044Snwhitehorn}
245217044Snwhitehorn#endif
246217044Snwhitehorn
247217044Snwhitehornstatic void
248217044Snwhitehornps3_reset(platform_t plat)
249217044Snwhitehorn{
250217044Snwhitehorn	lv1_panic(1);
251217044Snwhitehorn}
252217044Snwhitehorn
253217044Snwhitehornstatic vm_offset_t
254217044Snwhitehornps3_real_maxaddr(platform_t plat)
255217044Snwhitehorn{
256258807Snwhitehorn	struct mem_region *phys, *avail;
257258807Snwhitehorn	int nphys, navail;
258258807Snwhitehorn
259258807Snwhitehorn	mem_regions(&phys, &nphys, &avail, &navail);
260258807Snwhitehorn
261258807Snwhitehorn	return (phys[0].mr_start + phys[0].mr_size);
262217044Snwhitehorn}
263217044Snwhitehorn
264217044Snwhitehornstatic void
265247454Sdavideps3_cpu_idle(sbintime_t sbt)
266217044Snwhitehorn{
267217044Snwhitehorn	lv1_pause(0);
268217044Snwhitehorn}
269217044Snwhitehorn
270