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/10.3/sys/powerpc/ps3/platform_ps3.c 271114 2014-09-04 18:28:30Z nwhitehorn $");
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/pmap.h>
48217044Snwhitehorn#include <machine/smp.h>
49217044Snwhitehorn#include <machine/spr.h>
50217044Snwhitehorn#include <machine/vmparam.h>
51217044Snwhitehorn
52217044Snwhitehorn#include "platform_if.h"
53217044Snwhitehorn#include "ps3-hvcall.h"
54217044Snwhitehorn
55217044Snwhitehorn#ifdef SMP
56217044Snwhitehornextern void *ap_pcpu;
57217044Snwhitehorn#endif
58217044Snwhitehorn
59217044Snwhitehornstatic int ps3_probe(platform_t);
60217044Snwhitehornstatic int ps3_attach(platform_t);
61266020Sianstatic void ps3_mem_regions(platform_t, struct mem_region *phys, int *physsz,
62266020Sian    struct mem_region *avail, int *availsz);
63217044Snwhitehornstatic vm_offset_t ps3_real_maxaddr(platform_t);
64217044Snwhitehornstatic u_long ps3_timebase_freq(platform_t, struct cpuref *cpuref);
65217044Snwhitehorn#ifdef SMP
66217044Snwhitehornstatic int ps3_smp_first_cpu(platform_t, struct cpuref *cpuref);
67217044Snwhitehornstatic int ps3_smp_next_cpu(platform_t, struct cpuref *cpuref);
68217044Snwhitehornstatic int ps3_smp_get_bsp(platform_t, struct cpuref *cpuref);
69217044Snwhitehornstatic int ps3_smp_start_cpu(platform_t, struct pcpu *cpu);
70217044Snwhitehornstatic struct cpu_group *ps3_smp_topo(platform_t);
71217044Snwhitehorn#endif
72217044Snwhitehornstatic void ps3_reset(platform_t);
73247454Sdavidestatic void ps3_cpu_idle(sbintime_t);
74217044Snwhitehorn
75217044Snwhitehornstatic platform_method_t ps3_methods[] = {
76217044Snwhitehorn	PLATFORMMETHOD(platform_probe, 		ps3_probe),
77217044Snwhitehorn	PLATFORMMETHOD(platform_attach,		ps3_attach),
78217044Snwhitehorn	PLATFORMMETHOD(platform_mem_regions,	ps3_mem_regions),
79217044Snwhitehorn	PLATFORMMETHOD(platform_real_maxaddr,	ps3_real_maxaddr),
80217044Snwhitehorn	PLATFORMMETHOD(platform_timebase_freq,	ps3_timebase_freq),
81217044Snwhitehorn
82217044Snwhitehorn#ifdef SMP
83217044Snwhitehorn	PLATFORMMETHOD(platform_smp_first_cpu,	ps3_smp_first_cpu),
84217044Snwhitehorn	PLATFORMMETHOD(platform_smp_next_cpu,	ps3_smp_next_cpu),
85217044Snwhitehorn	PLATFORMMETHOD(platform_smp_get_bsp,	ps3_smp_get_bsp),
86217044Snwhitehorn	PLATFORMMETHOD(platform_smp_start_cpu,	ps3_smp_start_cpu),
87217044Snwhitehorn	PLATFORMMETHOD(platform_smp_topo,	ps3_smp_topo),
88217044Snwhitehorn#endif
89217044Snwhitehorn
90217044Snwhitehorn	PLATFORMMETHOD(platform_reset,		ps3_reset),
91217044Snwhitehorn
92246732Srpaulo	PLATFORMMETHOD_END
93217044Snwhitehorn};
94217044Snwhitehorn
95217044Snwhitehornstatic platform_def_t ps3_platform = {
96217044Snwhitehorn	"ps3",
97217044Snwhitehorn	ps3_methods,
98217044Snwhitehorn	0
99217044Snwhitehorn};
100217044Snwhitehorn
101217044SnwhitehornPLATFORM_DEF(ps3_platform);
102217044Snwhitehorn
103217044Snwhitehornstatic int
104217044Snwhitehornps3_probe(platform_t plat)
105217044Snwhitehorn{
106217044Snwhitehorn
107217044Snwhitehorn	return (BUS_PROBE_NOWILDCARD);
108217044Snwhitehorn}
109217044Snwhitehorn
110217044Snwhitehornstatic int
111217044Snwhitehornps3_attach(platform_t plat)
112217044Snwhitehorn{
113266020Sian
114266020Sian	pmap_mmu_install("mmu_ps3", BUS_PROBE_SPECIFIC);
115266020Sian	cpu_idle_hook = ps3_cpu_idle;
116266020Sian
117266020Sian	/* Set a breakpoint to make NULL an invalid address */
118266020Sian	lv1_set_dabr(0x7 /* read and write, MMU on */, 2 /* kernel accesses */);
119266020Sian
120266020Sian	return (0);
121266020Sian}
122266020Sian
123266020Sianvoid
124266020Sianps3_mem_regions(platform_t plat, struct mem_region *phys, int *physsz,
125266020Sian    struct mem_region *avail_regions, int *availsz)
126266020Sian{
127217044Snwhitehorn	uint64_t lpar_id, junk, ppe_id;
128217044Snwhitehorn
129217044Snwhitehorn	/* Get real mode memory region */
130217044Snwhitehorn	avail_regions[0].mr_start = 0;
131217044Snwhitehorn	lv1_get_logical_partition_id(&lpar_id);
132217044Snwhitehorn	lv1_get_logical_ppe_id(&ppe_id);
133217044Snwhitehorn	lv1_get_repository_node_value(lpar_id,
134217044Snwhitehorn	    lv1_repository_string("bi") >> 32, lv1_repository_string("pu"),
135217044Snwhitehorn	    ppe_id, lv1_repository_string("rm_size"),
136217044Snwhitehorn	    &avail_regions[0].mr_size, &junk);
137217044Snwhitehorn
138217044Snwhitehorn	/* Now get extended memory region */
139217044Snwhitehorn	lv1_get_repository_node_value(lpar_id,
140217044Snwhitehorn	    lv1_repository_string("bi") >> 32,
141217044Snwhitehorn	    lv1_repository_string("rgntotal"), 0, 0,
142217044Snwhitehorn	    &avail_regions[1].mr_size, &junk);
143217044Snwhitehorn
144217044Snwhitehorn	/* Convert to maximum amount we can allocate in 16 MB pages */
145217044Snwhitehorn	avail_regions[1].mr_size -= avail_regions[0].mr_size;
146217044Snwhitehorn	avail_regions[1].mr_size -= avail_regions[1].mr_size % (16*1024*1024);
147271114Snwhitehorn
148271114Snwhitehorn	/* Allocate extended memory region */
149271114Snwhitehorn	lv1_allocate_memory(avail_regions[1].mr_size, 24 /* 16 MB pages */,
150271114Snwhitehorn	    0, 0x04 /* any address */, &avail_regions[1].mr_start, &junk);
151271114Snwhitehorn
152266020Sian	*availsz = 2;
153217044Snwhitehorn
154266020Sian	if (phys != NULL) {
155266020Sian		memcpy(phys, avail_regions, sizeof(*phys)*2);
156266020Sian		*physsz = 2;
157266020Sian	}
158217044Snwhitehorn}
159217044Snwhitehorn
160217044Snwhitehornstatic u_long
161217044Snwhitehornps3_timebase_freq(platform_t plat, struct cpuref *cpuref)
162217044Snwhitehorn{
163217044Snwhitehorn	uint64_t ticks, node_id, junk;
164217044Snwhitehorn
165217044Snwhitehorn	lv1_get_repository_node_value(PS3_LPAR_ID_PME,
166217044Snwhitehorn	    lv1_repository_string("be") >> 32, 0, 0, 0, &node_id, &junk);
167217044Snwhitehorn	lv1_get_repository_node_value(PS3_LPAR_ID_PME,
168217044Snwhitehorn	    lv1_repository_string("be") >> 32, node_id,
169217044Snwhitehorn	    lv1_repository_string("clock"), 0, &ticks, &junk);
170217044Snwhitehorn
171217044Snwhitehorn	return (ticks);
172217044Snwhitehorn}
173217044Snwhitehorn
174217044Snwhitehorn#ifdef SMP
175217044Snwhitehornstatic int
176217044Snwhitehornps3_smp_first_cpu(platform_t plat, struct cpuref *cpuref)
177217044Snwhitehorn{
178217044Snwhitehorn
179217044Snwhitehorn	cpuref->cr_cpuid = 0;
180217044Snwhitehorn	cpuref->cr_hwref = cpuref->cr_cpuid;
181217044Snwhitehorn
182217044Snwhitehorn	return (0);
183217044Snwhitehorn}
184217044Snwhitehorn
185217044Snwhitehornstatic int
186217044Snwhitehornps3_smp_next_cpu(platform_t plat, struct cpuref *cpuref)
187217044Snwhitehorn{
188217044Snwhitehorn
189217044Snwhitehorn	if (cpuref->cr_cpuid >= 1)
190217044Snwhitehorn		return (ENOENT);
191217044Snwhitehorn
192217044Snwhitehorn	cpuref->cr_cpuid++;
193217044Snwhitehorn	cpuref->cr_hwref = cpuref->cr_cpuid;
194217044Snwhitehorn
195217044Snwhitehorn	return (0);
196217044Snwhitehorn}
197217044Snwhitehorn
198217044Snwhitehornstatic int
199217044Snwhitehornps3_smp_get_bsp(platform_t plat, struct cpuref *cpuref)
200217044Snwhitehorn{
201217044Snwhitehorn
202217044Snwhitehorn	cpuref->cr_cpuid = 0;
203217044Snwhitehorn	cpuref->cr_hwref = cpuref->cr_cpuid;
204217044Snwhitehorn
205217044Snwhitehorn	return (0);
206217044Snwhitehorn}
207217044Snwhitehorn
208217044Snwhitehornstatic int
209217044Snwhitehornps3_smp_start_cpu(platform_t plat, struct pcpu *pc)
210217044Snwhitehorn{
211217044Snwhitehorn	/* loader(8) is spinning on 0x40 == 0 right now */
212217044Snwhitehorn	uint32_t *secondary_spin_sem = (uint32_t *)(0x40);
213217044Snwhitehorn	int timeout;
214217044Snwhitehorn
215217044Snwhitehorn	if (pc->pc_hwref != 1)
216217044Snwhitehorn		return (ENXIO);
217217044Snwhitehorn
218217044Snwhitehorn	ap_pcpu = pc;
219217044Snwhitehorn	*secondary_spin_sem = 1;
220217044Snwhitehorn	powerpc_sync();
221217044Snwhitehorn	DELAY(1);
222217044Snwhitehorn
223217044Snwhitehorn	timeout = 10000;
224217044Snwhitehorn	while (!pc->pc_awake && timeout--)
225217044Snwhitehorn		DELAY(100);
226217044Snwhitehorn
227217044Snwhitehorn	return ((pc->pc_awake) ? 0 : EBUSY);
228217044Snwhitehorn}
229217044Snwhitehorn
230217044Snwhitehornstatic struct cpu_group *
231217044Snwhitehornps3_smp_topo(platform_t plat)
232217044Snwhitehorn{
233217044Snwhitehorn	return (smp_topo_1level(CG_SHARE_L1, 2, CG_FLAG_SMT));
234217044Snwhitehorn}
235217044Snwhitehorn#endif
236217044Snwhitehorn
237217044Snwhitehornstatic void
238217044Snwhitehornps3_reset(platform_t plat)
239217044Snwhitehorn{
240217044Snwhitehorn	lv1_panic(1);
241217044Snwhitehorn}
242217044Snwhitehorn
243217044Snwhitehornstatic vm_offset_t
244217044Snwhitehornps3_real_maxaddr(platform_t plat)
245217044Snwhitehorn{
246266020Sian	struct mem_region *phys, *avail;
247266020Sian	int nphys, navail;
248266020Sian
249266020Sian	mem_regions(&phys, &nphys, &avail, &navail);
250266020Sian
251266020Sian	return (phys[0].mr_start + phys[0].mr_size);
252217044Snwhitehorn}
253217044Snwhitehorn
254217044Snwhitehornstatic void
255247454Sdavideps3_cpu_idle(sbintime_t sbt)
256217044Snwhitehorn{
257217044Snwhitehorn	lv1_pause(0);
258217044Snwhitehorn}
259217044Snwhitehorn
260