xref: /freebsd/sys/powerpc/ps3/platform_ps3.c (revision 97cb52fa9aefd90fad38790fded50905aeeb9b9e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2010 Nathan Whitehorn
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/bus.h>
36 #include <sys/pcpu.h>
37 #include <sys/proc.h>
38 #include <sys/reboot.h>
39 #include <sys/smp.h>
40 
41 #include <vm/vm.h>
42 #include <vm/pmap.h>
43 
44 #include <machine/bus.h>
45 #include <machine/cpu.h>
46 #include <machine/hid.h>
47 #include <machine/platform.h>
48 #include <machine/platformvar.h>
49 #include <machine/smp.h>
50 #include <machine/spr.h>
51 #include <machine/vmparam.h>
52 
53 #include <dev/ofw/openfirm.h>
54 
55 #include "platform_if.h"
56 #include "ps3-hvcall.h"
57 
58 #ifdef SMP
59 extern void *ap_pcpu;
60 #endif
61 
62 static int ps3_probe(platform_t);
63 static int ps3_attach(platform_t);
64 static void ps3_mem_regions(platform_t, struct mem_region *phys, int *physsz,
65     struct mem_region *avail, int *availsz);
66 static vm_offset_t ps3_real_maxaddr(platform_t);
67 static u_long ps3_timebase_freq(platform_t, struct cpuref *cpuref);
68 #ifdef SMP
69 static int ps3_smp_first_cpu(platform_t, struct cpuref *cpuref);
70 static int ps3_smp_next_cpu(platform_t, struct cpuref *cpuref);
71 static int ps3_smp_get_bsp(platform_t, struct cpuref *cpuref);
72 static int ps3_smp_start_cpu(platform_t, struct pcpu *cpu);
73 static struct cpu_group *ps3_smp_topo(platform_t);
74 #endif
75 static void ps3_reset(platform_t);
76 static void ps3_cpu_idle(sbintime_t);
77 
78 static platform_method_t ps3_methods[] = {
79 	PLATFORMMETHOD(platform_probe, 		ps3_probe),
80 	PLATFORMMETHOD(platform_attach,		ps3_attach),
81 	PLATFORMMETHOD(platform_mem_regions,	ps3_mem_regions),
82 	PLATFORMMETHOD(platform_real_maxaddr,	ps3_real_maxaddr),
83 	PLATFORMMETHOD(platform_timebase_freq,	ps3_timebase_freq),
84 
85 #ifdef SMP
86 	PLATFORMMETHOD(platform_smp_first_cpu,	ps3_smp_first_cpu),
87 	PLATFORMMETHOD(platform_smp_next_cpu,	ps3_smp_next_cpu),
88 	PLATFORMMETHOD(platform_smp_get_bsp,	ps3_smp_get_bsp),
89 	PLATFORMMETHOD(platform_smp_start_cpu,	ps3_smp_start_cpu),
90 	PLATFORMMETHOD(platform_smp_topo,	ps3_smp_topo),
91 #endif
92 
93 	PLATFORMMETHOD(platform_reset,		ps3_reset),
94 
95 	PLATFORMMETHOD_END
96 };
97 
98 static platform_def_t ps3_platform = {
99 	"ps3",
100 	ps3_methods,
101 	0
102 };
103 
104 PLATFORM_DEF(ps3_platform);
105 
106 static int
107 ps3_probe(platform_t plat)
108 {
109 	phandle_t root;
110 	char compatible[64];
111 
112 	root = OF_finddevice("/");
113 	if (OF_getprop(root, "compatible", compatible, sizeof(compatible)) <= 0)
114                 return (BUS_PROBE_NOWILDCARD);
115 
116 	if (strncmp(compatible, "sony,ps3", sizeof(compatible)) != 0)
117 		return (BUS_PROBE_NOWILDCARD);
118 
119 	return (BUS_PROBE_SPECIFIC);
120 }
121 
122 static int
123 ps3_attach(platform_t plat)
124 {
125 
126 	pmap_mmu_install("mmu_ps3", BUS_PROBE_SPECIFIC);
127 	cpu_idle_hook = ps3_cpu_idle;
128 
129 	/* Set a breakpoint to make NULL an invalid address */
130 	lv1_set_dabr(0x7 /* read and write, MMU on */, 2 /* kernel accesses */);
131 
132 	return (0);
133 }
134 
135 void
136 ps3_mem_regions(platform_t plat, struct mem_region *phys, int *physsz,
137     struct mem_region *avail_regions, int *availsz)
138 {
139 	uint64_t lpar_id, junk, ppe_id;
140 
141 	/* Get real mode memory region */
142 	avail_regions[0].mr_start = 0;
143 	lv1_get_logical_partition_id(&lpar_id);
144 	lv1_get_logical_ppe_id(&ppe_id);
145 	lv1_get_repository_node_value(lpar_id,
146 	    lv1_repository_string("bi") >> 32, lv1_repository_string("pu"),
147 	    ppe_id, lv1_repository_string("rm_size"),
148 	    &avail_regions[0].mr_size, &junk);
149 
150 	/* Now get extended memory region */
151 	lv1_get_repository_node_value(lpar_id,
152 	    lv1_repository_string("bi") >> 32,
153 	    lv1_repository_string("rgntotal"), 0, 0,
154 	    &avail_regions[1].mr_size, &junk);
155 
156 	/* Convert to maximum amount we can allocate in 16 MB pages */
157 	avail_regions[1].mr_size -= avail_regions[0].mr_size;
158 	avail_regions[1].mr_size -= avail_regions[1].mr_size % (16*1024*1024);
159 
160 	/* Allocate extended memory region */
161 	lv1_allocate_memory(avail_regions[1].mr_size, 24 /* 16 MB pages */,
162 	    0, 0x04 /* any address */, &avail_regions[1].mr_start, &junk);
163 
164 	*availsz = 2;
165 
166 	if (phys != NULL) {
167 		memcpy(phys, avail_regions, sizeof(*phys)*2);
168 		*physsz = 2;
169 	}
170 }
171 
172 static u_long
173 ps3_timebase_freq(platform_t plat, struct cpuref *cpuref)
174 {
175 	uint64_t ticks, node_id, junk;
176 
177 	lv1_get_repository_node_value(PS3_LPAR_ID_PME,
178 	    lv1_repository_string("be") >> 32, 0, 0, 0, &node_id, &junk);
179 	lv1_get_repository_node_value(PS3_LPAR_ID_PME,
180 	    lv1_repository_string("be") >> 32, node_id,
181 	    lv1_repository_string("clock"), 0, &ticks, &junk);
182 
183 	return (ticks);
184 }
185 
186 #ifdef SMP
187 static int
188 ps3_smp_first_cpu(platform_t plat, struct cpuref *cpuref)
189 {
190 
191 	cpuref->cr_cpuid = 0;
192 	cpuref->cr_hwref = cpuref->cr_cpuid;
193 
194 	return (0);
195 }
196 
197 static int
198 ps3_smp_next_cpu(platform_t plat, struct cpuref *cpuref)
199 {
200 
201 	if (cpuref->cr_cpuid >= 1)
202 		return (ENOENT);
203 
204 	cpuref->cr_cpuid++;
205 	cpuref->cr_hwref = cpuref->cr_cpuid;
206 
207 	return (0);
208 }
209 
210 static int
211 ps3_smp_get_bsp(platform_t plat, struct cpuref *cpuref)
212 {
213 
214 	cpuref->cr_cpuid = 0;
215 	cpuref->cr_hwref = cpuref->cr_cpuid;
216 
217 	return (0);
218 }
219 
220 static int
221 ps3_smp_start_cpu(platform_t plat, struct pcpu *pc)
222 {
223 	/* loader(8) is spinning on 0x40 == 0 right now */
224 	uint32_t *secondary_spin_sem = (uint32_t *)(0x40);
225 	int timeout;
226 
227 	if (pc->pc_hwref != 1)
228 		return (ENXIO);
229 
230 	ap_pcpu = pc;
231 	*secondary_spin_sem = 1;
232 	powerpc_sync();
233 	DELAY(1);
234 
235 	timeout = 10000;
236 	while (!pc->pc_awake && timeout--)
237 		DELAY(100);
238 
239 	return ((pc->pc_awake) ? 0 : EBUSY);
240 }
241 
242 static struct cpu_group *
243 ps3_smp_topo(platform_t plat)
244 {
245 	return (smp_topo_1level(CG_SHARE_L1, 2, CG_FLAG_SMT));
246 }
247 #endif
248 
249 static void
250 ps3_reset(platform_t plat)
251 {
252 	lv1_panic(1);
253 }
254 
255 static vm_offset_t
256 ps3_real_maxaddr(platform_t plat)
257 {
258 	struct mem_region *phys, *avail;
259 	int nphys, navail;
260 
261 	mem_regions(&phys, &nphys, &avail, &navail);
262 
263 	return (phys[0].mr_start + phys[0].mr_size);
264 }
265 
266 static void
267 ps3_cpu_idle(sbintime_t sbt)
268 {
269 	lv1_pause(0);
270 }
271 
272