xref: /freebsd/sys/powerpc/ps3/platform_ps3.c (revision fdafd315)
103479763SNathan Whitehorn /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
371e3c308SPedro F. Giffuni  *
403479763SNathan Whitehorn  * Copyright (c) 2010 Nathan Whitehorn
503479763SNathan Whitehorn  * All rights reserved.
603479763SNathan Whitehorn  *
703479763SNathan Whitehorn  * Redistribution and use in source and binary forms, with or without
803479763SNathan Whitehorn  * modification, are permitted provided that the following conditions
903479763SNathan Whitehorn  * are met:
1003479763SNathan Whitehorn  *
1103479763SNathan Whitehorn  * 1. Redistributions of source code must retain the above copyright
1203479763SNathan Whitehorn  *    notice, this list of conditions and the following disclaimer.
1303479763SNathan Whitehorn  * 2. Redistributions in binary form must reproduce the above copyright
1403479763SNathan Whitehorn  *    notice, this list of conditions and the following disclaimer in the
1503479763SNathan Whitehorn  *    documentation and/or other materials provided with the distribution.
1603479763SNathan Whitehorn  *
1703479763SNathan Whitehorn  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1803479763SNathan Whitehorn  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1903479763SNathan Whitehorn  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2003479763SNathan Whitehorn  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2103479763SNathan Whitehorn  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2203479763SNathan Whitehorn  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2303479763SNathan Whitehorn  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2403479763SNathan Whitehorn  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2503479763SNathan Whitehorn  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2603479763SNathan Whitehorn  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2703479763SNathan Whitehorn  */
2803479763SNathan Whitehorn 
2903479763SNathan Whitehorn #include <sys/param.h>
3003479763SNathan Whitehorn #include <sys/systm.h>
3103479763SNathan Whitehorn #include <sys/kernel.h>
3203479763SNathan Whitehorn #include <sys/bus.h>
3303479763SNathan Whitehorn #include <sys/pcpu.h>
3403479763SNathan Whitehorn #include <sys/proc.h>
3503479763SNathan Whitehorn #include <sys/reboot.h>
3603479763SNathan Whitehorn #include <sys/smp.h>
3703479763SNathan Whitehorn 
3803479763SNathan Whitehorn #include <vm/vm.h>
3903479763SNathan Whitehorn #include <vm/pmap.h>
4003479763SNathan Whitehorn 
4103479763SNathan Whitehorn #include <machine/bus.h>
4203479763SNathan Whitehorn #include <machine/cpu.h>
4303479763SNathan Whitehorn #include <machine/hid.h>
4403479763SNathan Whitehorn #include <machine/platform.h>
4503479763SNathan Whitehorn #include <machine/platformvar.h>
4603479763SNathan Whitehorn #include <machine/smp.h>
4703479763SNathan Whitehorn #include <machine/spr.h>
4803479763SNathan Whitehorn #include <machine/vmparam.h>
4903479763SNathan Whitehorn 
50fd520b6eSNathan Whitehorn #include <dev/ofw/openfirm.h>
51fd520b6eSNathan Whitehorn 
5203479763SNathan Whitehorn #include "platform_if.h"
5303479763SNathan Whitehorn #include "ps3-hvcall.h"
5403479763SNathan Whitehorn 
5503479763SNathan Whitehorn #ifdef SMP
5603479763SNathan Whitehorn extern void *ap_pcpu;
5703479763SNathan Whitehorn #endif
5803479763SNathan Whitehorn 
5903479763SNathan Whitehorn static int ps3_probe(platform_t);
6003479763SNathan Whitehorn static int ps3_attach(platform_t);
61c1cb22d7SNathan Whitehorn static void ps3_mem_regions(platform_t, struct mem_region *phys, int *physsz,
62c1cb22d7SNathan Whitehorn     struct mem_region *avail, int *availsz);
6303479763SNathan Whitehorn static vm_offset_t ps3_real_maxaddr(platform_t);
6403479763SNathan Whitehorn static u_long ps3_timebase_freq(platform_t, struct cpuref *cpuref);
6503479763SNathan Whitehorn #ifdef SMP
6603479763SNathan Whitehorn static int ps3_smp_first_cpu(platform_t, struct cpuref *cpuref);
6703479763SNathan Whitehorn static int ps3_smp_next_cpu(platform_t, struct cpuref *cpuref);
6803479763SNathan Whitehorn static int ps3_smp_get_bsp(platform_t, struct cpuref *cpuref);
6903479763SNathan Whitehorn static int ps3_smp_start_cpu(platform_t, struct pcpu *cpu);
70bba9cbe3SConrad Meyer static void ps3_smp_probe_threads(platform_t);
7103479763SNathan Whitehorn static struct cpu_group *ps3_smp_topo(platform_t);
7203479763SNathan Whitehorn #endif
7303479763SNathan Whitehorn static void ps3_reset(platform_t);
74acccf7d8SDavide Italiano static void ps3_cpu_idle(sbintime_t);
7503479763SNathan Whitehorn 
7603479763SNathan Whitehorn static platform_method_t ps3_methods[] = {
7703479763SNathan Whitehorn 	PLATFORMMETHOD(platform_probe, 		ps3_probe),
7803479763SNathan Whitehorn 	PLATFORMMETHOD(platform_attach,		ps3_attach),
7903479763SNathan Whitehorn 	PLATFORMMETHOD(platform_mem_regions,	ps3_mem_regions),
8003479763SNathan Whitehorn 	PLATFORMMETHOD(platform_real_maxaddr,	ps3_real_maxaddr),
8103479763SNathan Whitehorn 	PLATFORMMETHOD(platform_timebase_freq,	ps3_timebase_freq),
8203479763SNathan Whitehorn 
8303479763SNathan Whitehorn #ifdef SMP
8403479763SNathan Whitehorn 	PLATFORMMETHOD(platform_smp_first_cpu,	ps3_smp_first_cpu),
8503479763SNathan Whitehorn 	PLATFORMMETHOD(platform_smp_next_cpu,	ps3_smp_next_cpu),
8603479763SNathan Whitehorn 	PLATFORMMETHOD(platform_smp_get_bsp,	ps3_smp_get_bsp),
8703479763SNathan Whitehorn 	PLATFORMMETHOD(platform_smp_start_cpu,	ps3_smp_start_cpu),
88bba9cbe3SConrad Meyer 	PLATFORMMETHOD(platform_smp_probe_threads,	ps3_smp_probe_threads),
8903479763SNathan Whitehorn 	PLATFORMMETHOD(platform_smp_topo,	ps3_smp_topo),
9003479763SNathan Whitehorn #endif
9103479763SNathan Whitehorn 
9203479763SNathan Whitehorn 	PLATFORMMETHOD(platform_reset,		ps3_reset),
9303479763SNathan Whitehorn 
94eaba9848SRui Paulo 	PLATFORMMETHOD_END
9503479763SNathan Whitehorn };
9603479763SNathan Whitehorn 
9703479763SNathan Whitehorn static platform_def_t ps3_platform = {
9803479763SNathan Whitehorn 	"ps3",
9903479763SNathan Whitehorn 	ps3_methods,
10003479763SNathan Whitehorn 	0
10103479763SNathan Whitehorn };
10203479763SNathan Whitehorn 
10303479763SNathan Whitehorn PLATFORM_DEF(ps3_platform);
10403479763SNathan Whitehorn 
105ba06dbb8SNathan Whitehorn static int ps3_boot_pir = 0;
106ba06dbb8SNathan Whitehorn 
10703479763SNathan Whitehorn static int
ps3_probe(platform_t plat)10803479763SNathan Whitehorn ps3_probe(platform_t plat)
10903479763SNathan Whitehorn {
110fd520b6eSNathan Whitehorn 	phandle_t root;
111fd520b6eSNathan Whitehorn 	char compatible[64];
11203479763SNathan Whitehorn 
113fd520b6eSNathan Whitehorn 	root = OF_finddevice("/");
114fd520b6eSNathan Whitehorn 	if (OF_getprop(root, "compatible", compatible, sizeof(compatible)) <= 0)
11503479763SNathan Whitehorn                 return (BUS_PROBE_NOWILDCARD);
116fd520b6eSNathan Whitehorn 
117fd520b6eSNathan Whitehorn 	if (strncmp(compatible, "sony,ps3", sizeof(compatible)) != 0)
118fd520b6eSNathan Whitehorn 		return (BUS_PROBE_NOWILDCARD);
119fd520b6eSNathan Whitehorn 
120fd520b6eSNathan Whitehorn 	return (BUS_PROBE_SPECIFIC);
12103479763SNathan Whitehorn }
12203479763SNathan Whitehorn 
12303479763SNathan Whitehorn static int
ps3_attach(platform_t plat)12403479763SNathan Whitehorn ps3_attach(platform_t plat)
12503479763SNathan Whitehorn {
126c1cb22d7SNathan Whitehorn 
127c1cb22d7SNathan Whitehorn 	pmap_mmu_install("mmu_ps3", BUS_PROBE_SPECIFIC);
128c1cb22d7SNathan Whitehorn 	cpu_idle_hook = ps3_cpu_idle;
129c1cb22d7SNathan Whitehorn 
130ba06dbb8SNathan Whitehorn 	/* Record our PIR at boot for later */
131ba06dbb8SNathan Whitehorn 	ps3_boot_pir = mfspr(SPR_PIR);
132ba06dbb8SNathan Whitehorn 
133c1cb22d7SNathan Whitehorn 	return (0);
134c1cb22d7SNathan Whitehorn }
135c1cb22d7SNathan Whitehorn 
136c1cb22d7SNathan Whitehorn void
ps3_mem_regions(platform_t plat,struct mem_region * phys,int * physsz,struct mem_region * avail_regions,int * availsz)137c1cb22d7SNathan Whitehorn ps3_mem_regions(platform_t plat, struct mem_region *phys, int *physsz,
138c1cb22d7SNathan Whitehorn     struct mem_region *avail_regions, int *availsz)
139c1cb22d7SNathan Whitehorn {
140c1d6f6ebSNathan Whitehorn 	uint64_t lpar_id, junk;
141c1d6f6ebSNathan Whitehorn 	int i;
14203479763SNathan Whitehorn 
143c1d6f6ebSNathan Whitehorn 	/* Prefer device tree information if available */
144c1d6f6ebSNathan Whitehorn 	if (OF_finddevice("/") != -1) {
145c1d6f6ebSNathan Whitehorn 		ofw_mem_regions(phys, physsz, avail_regions, availsz);
146c1d6f6ebSNathan Whitehorn 	} else {
147c1d6f6ebSNathan Whitehorn 		/* Real mode memory region is first segment */
148c1d6f6ebSNathan Whitehorn 		phys[0].mr_start = 0;
149c1d6f6ebSNathan Whitehorn 		phys[0].mr_size = ps3_real_maxaddr(plat);
150c1d6f6ebSNathan Whitehorn 		*physsz = *availsz = 1;
151c1d6f6ebSNathan Whitehorn 		avail_regions[0] = phys[0];
152c1d6f6ebSNathan Whitehorn 	}
15303479763SNathan Whitehorn 
15403479763SNathan Whitehorn 	/* Now get extended memory region */
155c1d6f6ebSNathan Whitehorn 	lv1_get_logical_partition_id(&lpar_id);
15603479763SNathan Whitehorn 	lv1_get_repository_node_value(lpar_id,
15703479763SNathan Whitehorn 	    lv1_repository_string("bi") >> 32,
15803479763SNathan Whitehorn 	    lv1_repository_string("rgntotal"), 0, 0,
159c1d6f6ebSNathan Whitehorn 	    &phys[*physsz].mr_size, &junk);
160c1d6f6ebSNathan Whitehorn 	for (i = 0; i < *physsz; i++)
161c1d6f6ebSNathan Whitehorn 		phys[*physsz].mr_size -= phys[i].mr_size;
16203479763SNathan Whitehorn 
16303479763SNathan Whitehorn 	/* Convert to maximum amount we can allocate in 16 MB pages */
164c1d6f6ebSNathan Whitehorn 	phys[*physsz].mr_size -= phys[*physsz].mr_size % (16*1024*1024);
16531411feaSNathan Whitehorn 
16631411feaSNathan Whitehorn 	/* Allocate extended memory region */
167c1d6f6ebSNathan Whitehorn 	lv1_allocate_memory(phys[*physsz].mr_size, 24 /* 16 MB pages */,
168c1d6f6ebSNathan Whitehorn 	    0, 0x04 /* any address */, &phys[*physsz].mr_start, &junk);
169c1d6f6ebSNathan Whitehorn 	avail_regions[*availsz] = phys[*physsz];
170c1d6f6ebSNathan Whitehorn 	(*physsz)++;
171c1d6f6ebSNathan Whitehorn 	(*availsz)++;
17203479763SNathan Whitehorn }
17303479763SNathan Whitehorn 
17403479763SNathan Whitehorn static u_long
ps3_timebase_freq(platform_t plat,struct cpuref * cpuref)17503479763SNathan Whitehorn ps3_timebase_freq(platform_t plat, struct cpuref *cpuref)
17603479763SNathan Whitehorn {
17703479763SNathan Whitehorn 	uint64_t ticks, node_id, junk;
17803479763SNathan Whitehorn 
17903479763SNathan Whitehorn 	lv1_get_repository_node_value(PS3_LPAR_ID_PME,
18003479763SNathan Whitehorn 	    lv1_repository_string("be") >> 32, 0, 0, 0, &node_id, &junk);
18103479763SNathan Whitehorn 	lv1_get_repository_node_value(PS3_LPAR_ID_PME,
18203479763SNathan Whitehorn 	    lv1_repository_string("be") >> 32, node_id,
18303479763SNathan Whitehorn 	    lv1_repository_string("clock"), 0, &ticks, &junk);
18403479763SNathan Whitehorn 
18503479763SNathan Whitehorn 	return (ticks);
18603479763SNathan Whitehorn }
18703479763SNathan Whitehorn 
18803479763SNathan Whitehorn #ifdef SMP
18903479763SNathan Whitehorn static int
ps3_smp_first_cpu(platform_t plat,struct cpuref * cpuref)19003479763SNathan Whitehorn ps3_smp_first_cpu(platform_t plat, struct cpuref *cpuref)
19103479763SNathan Whitehorn {
19203479763SNathan Whitehorn 
19303479763SNathan Whitehorn 	cpuref->cr_cpuid = 0;
194ba06dbb8SNathan Whitehorn 	cpuref->cr_hwref = ps3_boot_pir;
19503479763SNathan Whitehorn 
19603479763SNathan Whitehorn 	return (0);
19703479763SNathan Whitehorn }
19803479763SNathan Whitehorn 
19903479763SNathan Whitehorn static int
ps3_smp_next_cpu(platform_t plat,struct cpuref * cpuref)20003479763SNathan Whitehorn ps3_smp_next_cpu(platform_t plat, struct cpuref *cpuref)
20103479763SNathan Whitehorn {
20203479763SNathan Whitehorn 
20303479763SNathan Whitehorn 	if (cpuref->cr_cpuid >= 1)
20403479763SNathan Whitehorn 		return (ENOENT);
20503479763SNathan Whitehorn 
20603479763SNathan Whitehorn 	cpuref->cr_cpuid++;
207ba06dbb8SNathan Whitehorn 	cpuref->cr_hwref = !ps3_boot_pir;
20803479763SNathan Whitehorn 
20903479763SNathan Whitehorn 	return (0);
21003479763SNathan Whitehorn }
21103479763SNathan Whitehorn 
21203479763SNathan Whitehorn static int
ps3_smp_get_bsp(platform_t plat,struct cpuref * cpuref)21303479763SNathan Whitehorn ps3_smp_get_bsp(platform_t plat, struct cpuref *cpuref)
21403479763SNathan Whitehorn {
21503479763SNathan Whitehorn 
21603479763SNathan Whitehorn 	cpuref->cr_cpuid = 0;
217ba06dbb8SNathan Whitehorn 	cpuref->cr_hwref = ps3_boot_pir;
21803479763SNathan Whitehorn 
21903479763SNathan Whitehorn 	return (0);
22003479763SNathan Whitehorn }
22103479763SNathan Whitehorn 
22203479763SNathan Whitehorn static int
ps3_smp_start_cpu(platform_t plat,struct pcpu * pc)22303479763SNathan Whitehorn ps3_smp_start_cpu(platform_t plat, struct pcpu *pc)
22403479763SNathan Whitehorn {
225ba06dbb8SNathan Whitehorn 	/* kernel is spinning on 0x40 == -1 right now */
226f9edb09dSNathan Whitehorn 	volatile uint32_t *secondary_spin_sem =
227f9edb09dSNathan Whitehorn 	    (uint32_t *)PHYS_TO_DMAP((uintptr_t)0x40);
228ba06dbb8SNathan Whitehorn 	int remote_pir = pc->pc_hwref;
22903479763SNathan Whitehorn 	int timeout;
23003479763SNathan Whitehorn 
23103479763SNathan Whitehorn 	ap_pcpu = pc;
23203479763SNathan Whitehorn 
233ba06dbb8SNathan Whitehorn 	/* Try both PIR values, looping a few times: the HV likes moving us */
23403479763SNathan Whitehorn 	timeout = 10000;
235ba06dbb8SNathan Whitehorn 	while (!pc->pc_awake && timeout--) {
236ba06dbb8SNathan Whitehorn 		*secondary_spin_sem = remote_pir;
237ba06dbb8SNathan Whitehorn 		powerpc_sync();
23803479763SNathan Whitehorn 		DELAY(100);
239ba06dbb8SNathan Whitehorn 		remote_pir = !remote_pir;
240ba06dbb8SNathan Whitehorn 	}
24103479763SNathan Whitehorn 
24203479763SNathan Whitehorn 	return ((pc->pc_awake) ? 0 : EBUSY);
24303479763SNathan Whitehorn }
24403479763SNathan Whitehorn 
245bba9cbe3SConrad Meyer static void
ps3_smp_probe_threads(platform_t plat)246bba9cbe3SConrad Meyer ps3_smp_probe_threads(platform_t plat)
24703479763SNathan Whitehorn {
2486b83069eSConrad Meyer 	mp_ncores = 1;
2496b83069eSConrad Meyer 	smp_threads_per_core = 2;
250bba9cbe3SConrad Meyer }
251bba9cbe3SConrad Meyer 
252bba9cbe3SConrad Meyer static struct cpu_group *
ps3_smp_topo(platform_t plat)253bba9cbe3SConrad Meyer ps3_smp_topo(platform_t plat)
254bba9cbe3SConrad Meyer {
25503479763SNathan Whitehorn 	return (smp_topo_1level(CG_SHARE_L1, 2, CG_FLAG_SMT));
25603479763SNathan Whitehorn }
25703479763SNathan Whitehorn #endif
25803479763SNathan Whitehorn 
25903479763SNathan Whitehorn static void
ps3_reset(platform_t plat)26003479763SNathan Whitehorn ps3_reset(platform_t plat)
26103479763SNathan Whitehorn {
26203479763SNathan Whitehorn 	lv1_panic(1);
26303479763SNathan Whitehorn }
26403479763SNathan Whitehorn 
26503479763SNathan Whitehorn static vm_offset_t
ps3_real_maxaddr(platform_t plat)26603479763SNathan Whitehorn ps3_real_maxaddr(platform_t plat)
26703479763SNathan Whitehorn {
268c1d6f6ebSNathan Whitehorn 	uint64_t lpar_id, junk, ppe_id;
269c1d6f6ebSNathan Whitehorn 	static uint64_t rm_maxaddr = 0;
270c1cb22d7SNathan Whitehorn 
271c1d6f6ebSNathan Whitehorn 	if (rm_maxaddr == 0) {
272c1d6f6ebSNathan Whitehorn 		/* Get real mode memory region */
273c1d6f6ebSNathan Whitehorn 		lv1_get_logical_partition_id(&lpar_id);
274c1d6f6ebSNathan Whitehorn 		lv1_get_logical_ppe_id(&ppe_id);
275c1cb22d7SNathan Whitehorn 
276c1d6f6ebSNathan Whitehorn 		lv1_get_repository_node_value(lpar_id,
277c1d6f6ebSNathan Whitehorn 		    lv1_repository_string("bi") >> 32,
278c1d6f6ebSNathan Whitehorn 		    lv1_repository_string("pu"),
279c1d6f6ebSNathan Whitehorn 		    ppe_id, lv1_repository_string("rm_size"),
280c1d6f6ebSNathan Whitehorn 		    &rm_maxaddr, &junk);
281c1d6f6ebSNathan Whitehorn 	}
282c1d6f6ebSNathan Whitehorn 
283c1d6f6ebSNathan Whitehorn 	return (rm_maxaddr);
28403479763SNathan Whitehorn }
28503479763SNathan Whitehorn 
28603479763SNathan Whitehorn static void
ps3_cpu_idle(sbintime_t sbt)287acccf7d8SDavide Italiano ps3_cpu_idle(sbintime_t sbt)
28803479763SNathan Whitehorn {
28903479763SNathan Whitehorn 	lv1_pause(0);
29003479763SNathan Whitehorn }
291