xref: /freebsd/sys/powerpc/ps3/platform_ps3.c (revision 6be3386466ab79a84b48429ae66244f21526d3df)
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 void ps3_smp_probe_threads(platform_t);
74 static struct cpu_group *ps3_smp_topo(platform_t);
75 #endif
76 static void ps3_reset(platform_t);
77 static void ps3_cpu_idle(sbintime_t);
78 
79 static platform_method_t ps3_methods[] = {
80 	PLATFORMMETHOD(platform_probe, 		ps3_probe),
81 	PLATFORMMETHOD(platform_attach,		ps3_attach),
82 	PLATFORMMETHOD(platform_mem_regions,	ps3_mem_regions),
83 	PLATFORMMETHOD(platform_real_maxaddr,	ps3_real_maxaddr),
84 	PLATFORMMETHOD(platform_timebase_freq,	ps3_timebase_freq),
85 
86 #ifdef SMP
87 	PLATFORMMETHOD(platform_smp_first_cpu,	ps3_smp_first_cpu),
88 	PLATFORMMETHOD(platform_smp_next_cpu,	ps3_smp_next_cpu),
89 	PLATFORMMETHOD(platform_smp_get_bsp,	ps3_smp_get_bsp),
90 	PLATFORMMETHOD(platform_smp_start_cpu,	ps3_smp_start_cpu),
91 	PLATFORMMETHOD(platform_smp_probe_threads,	ps3_smp_probe_threads),
92 	PLATFORMMETHOD(platform_smp_topo,	ps3_smp_topo),
93 #endif
94 
95 	PLATFORMMETHOD(platform_reset,		ps3_reset),
96 
97 	PLATFORMMETHOD_END
98 };
99 
100 static platform_def_t ps3_platform = {
101 	"ps3",
102 	ps3_methods,
103 	0
104 };
105 
106 PLATFORM_DEF(ps3_platform);
107 
108 static int ps3_boot_pir = 0;
109 
110 static int
111 ps3_probe(platform_t plat)
112 {
113 	phandle_t root;
114 	char compatible[64];
115 
116 	root = OF_finddevice("/");
117 	if (OF_getprop(root, "compatible", compatible, sizeof(compatible)) <= 0)
118                 return (BUS_PROBE_NOWILDCARD);
119 
120 	if (strncmp(compatible, "sony,ps3", sizeof(compatible)) != 0)
121 		return (BUS_PROBE_NOWILDCARD);
122 
123 	return (BUS_PROBE_SPECIFIC);
124 }
125 
126 static int
127 ps3_attach(platform_t plat)
128 {
129 
130 	pmap_mmu_install("mmu_ps3", BUS_PROBE_SPECIFIC);
131 	cpu_idle_hook = ps3_cpu_idle;
132 
133 	/* Record our PIR at boot for later */
134 	ps3_boot_pir = mfspr(SPR_PIR);
135 
136 	return (0);
137 }
138 
139 void
140 ps3_mem_regions(platform_t plat, struct mem_region *phys, int *physsz,
141     struct mem_region *avail_regions, int *availsz)
142 {
143 	uint64_t lpar_id, junk;
144 	int i;
145 
146 	/* Prefer device tree information if available */
147 	if (OF_finddevice("/") != -1) {
148 		ofw_mem_regions(phys, physsz, avail_regions, availsz);
149 	} else {
150 		/* Real mode memory region is first segment */
151 		phys[0].mr_start = 0;
152 		phys[0].mr_size = ps3_real_maxaddr(plat);
153 		*physsz = *availsz = 1;
154 		avail_regions[0] = phys[0];
155 	}
156 
157 	/* Now get extended memory region */
158 	lv1_get_logical_partition_id(&lpar_id);
159 	lv1_get_repository_node_value(lpar_id,
160 	    lv1_repository_string("bi") >> 32,
161 	    lv1_repository_string("rgntotal"), 0, 0,
162 	    &phys[*physsz].mr_size, &junk);
163 	for (i = 0; i < *physsz; i++)
164 		phys[*physsz].mr_size -= phys[i].mr_size;
165 
166 	/* Convert to maximum amount we can allocate in 16 MB pages */
167 	phys[*physsz].mr_size -= phys[*physsz].mr_size % (16*1024*1024);
168 
169 	/* Allocate extended memory region */
170 	lv1_allocate_memory(phys[*physsz].mr_size, 24 /* 16 MB pages */,
171 	    0, 0x04 /* any address */, &phys[*physsz].mr_start, &junk);
172 	avail_regions[*availsz] = phys[*physsz];
173 	(*physsz)++;
174 	(*availsz)++;
175 }
176 
177 static u_long
178 ps3_timebase_freq(platform_t plat, struct cpuref *cpuref)
179 {
180 	uint64_t ticks, node_id, junk;
181 
182 	lv1_get_repository_node_value(PS3_LPAR_ID_PME,
183 	    lv1_repository_string("be") >> 32, 0, 0, 0, &node_id, &junk);
184 	lv1_get_repository_node_value(PS3_LPAR_ID_PME,
185 	    lv1_repository_string("be") >> 32, node_id,
186 	    lv1_repository_string("clock"), 0, &ticks, &junk);
187 
188 	return (ticks);
189 }
190 
191 #ifdef SMP
192 static int
193 ps3_smp_first_cpu(platform_t plat, struct cpuref *cpuref)
194 {
195 
196 	cpuref->cr_cpuid = 0;
197 	cpuref->cr_hwref = ps3_boot_pir;
198 
199 	return (0);
200 }
201 
202 static int
203 ps3_smp_next_cpu(platform_t plat, struct cpuref *cpuref)
204 {
205 
206 	if (cpuref->cr_cpuid >= 1)
207 		return (ENOENT);
208 
209 	cpuref->cr_cpuid++;
210 	cpuref->cr_hwref = !ps3_boot_pir;
211 
212 	return (0);
213 }
214 
215 static int
216 ps3_smp_get_bsp(platform_t plat, struct cpuref *cpuref)
217 {
218 
219 	cpuref->cr_cpuid = 0;
220 	cpuref->cr_hwref = ps3_boot_pir;
221 
222 	return (0);
223 }
224 
225 static int
226 ps3_smp_start_cpu(platform_t plat, struct pcpu *pc)
227 {
228 	/* kernel is spinning on 0x40 == -1 right now */
229 	volatile uint32_t *secondary_spin_sem =
230 	    (uint32_t *)PHYS_TO_DMAP((uintptr_t)0x40);
231 	int remote_pir = pc->pc_hwref;
232 	int timeout;
233 
234 	ap_pcpu = pc;
235 
236 	/* Try both PIR values, looping a few times: the HV likes moving us */
237 	timeout = 10000;
238 	while (!pc->pc_awake && timeout--) {
239 		*secondary_spin_sem = remote_pir;
240 		powerpc_sync();
241 		DELAY(100);
242 		remote_pir = !remote_pir;
243 	}
244 
245 	return ((pc->pc_awake) ? 0 : EBUSY);
246 }
247 
248 static void
249 ps3_smp_probe_threads(platform_t plat)
250 {
251 	mp_ncores = 1;
252 	smp_threads_per_core = 2;
253 }
254 
255 static struct cpu_group *
256 ps3_smp_topo(platform_t plat)
257 {
258 	return (smp_topo_1level(CG_SHARE_L1, 2, CG_FLAG_SMT));
259 }
260 #endif
261 
262 static void
263 ps3_reset(platform_t plat)
264 {
265 	lv1_panic(1);
266 }
267 
268 static vm_offset_t
269 ps3_real_maxaddr(platform_t plat)
270 {
271 	uint64_t lpar_id, junk, ppe_id;
272 	static uint64_t rm_maxaddr = 0;
273 
274 	if (rm_maxaddr == 0) {
275 		/* Get real mode memory region */
276 		lv1_get_logical_partition_id(&lpar_id);
277 		lv1_get_logical_ppe_id(&ppe_id);
278 
279 		lv1_get_repository_node_value(lpar_id,
280 		    lv1_repository_string("bi") >> 32,
281 		    lv1_repository_string("pu"),
282 		    ppe_id, lv1_repository_string("rm_size"),
283 		    &rm_maxaddr, &junk);
284 	}
285 
286 	return (rm_maxaddr);
287 }
288 
289 static void
290 ps3_cpu_idle(sbintime_t sbt)
291 {
292 	lv1_pause(0);
293 }
294