1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 *  PS3 platform setup routines.
4 *
5 *  Copyright (C) 2006 Sony Computer Entertainment Inc.
6 *  Copyright 2006 Sony Corp.
7 */
8
9#include <linux/kernel.h>
10#include <linux/delay.h>
11#include <linux/fs.h>
12#include <linux/root_dev.h>
13#include <linux/console.h>
14#include <linux/export.h>
15#include <linux/memblock.h>
16
17#include <asm/machdep.h>
18#include <asm/firmware.h>
19#include <asm/time.h>
20#include <asm/iommu.h>
21#include <asm/udbg.h>
22#include <asm/prom.h>
23#include <asm/lv1call.h>
24#include <asm/ps3gpu.h>
25
26#include "platform.h"
27
28#if defined(DEBUG)
29#define DBG udbg_printf
30#else
31#define DBG pr_debug
32#endif
33
34/* mutex synchronizing GPU accesses and video mode changes */
35DEFINE_MUTEX(ps3_gpu_mutex);
36EXPORT_SYMBOL_GPL(ps3_gpu_mutex);
37
38static union ps3_firmware_version ps3_firmware_version;
39
40void ps3_get_firmware_version(union ps3_firmware_version *v)
41{
42	*v = ps3_firmware_version;
43}
44EXPORT_SYMBOL_GPL(ps3_get_firmware_version);
45
46int ps3_compare_firmware_version(u16 major, u16 minor, u16 rev)
47{
48	union ps3_firmware_version x;
49
50	x.pad = 0;
51	x.major = major;
52	x.minor = minor;
53	x.rev = rev;
54
55	return (ps3_firmware_version.raw > x.raw) -
56	       (ps3_firmware_version.raw < x.raw);
57}
58EXPORT_SYMBOL_GPL(ps3_compare_firmware_version);
59
60static void ps3_power_save(void)
61{
62	/*
63	 * lv1_pause() puts the PPE thread into inactive state until an
64	 * irq on an unmasked plug exists. MSR[EE] has no effect.
65	 * flags: 0 = wake on DEC interrupt, 1 = ignore DEC interrupt.
66	 */
67
68	lv1_pause(0);
69}
70
71static void __noreturn ps3_restart(char *cmd)
72{
73	DBG("%s:%d cmd '%s'\n", __func__, __LINE__, cmd);
74
75	smp_send_stop();
76	ps3_sys_manager_restart(); /* never returns */
77}
78
79static void ps3_power_off(void)
80{
81	DBG("%s:%d\n", __func__, __LINE__);
82
83	smp_send_stop();
84	ps3_sys_manager_power_off(); /* never returns */
85}
86
87static void __noreturn ps3_halt(void)
88{
89	DBG("%s:%d\n", __func__, __LINE__);
90
91	smp_send_stop();
92	ps3_sys_manager_halt(); /* never returns */
93}
94
95static void ps3_panic(char *str)
96{
97	DBG("%s:%d %s\n", __func__, __LINE__, str);
98
99	smp_send_stop();
100	printk("\n");
101	printk("   System does not reboot automatically.\n");
102	printk("   Please press POWER button.\n");
103	printk("\n");
104	panic_flush_kmsg_end();
105
106	while(1)
107		lv1_pause(1);
108}
109
110#if defined(CONFIG_FB_PS3) || defined(CONFIG_FB_PS3_MODULE) || \
111    defined(CONFIG_PS3_FLASH) || defined(CONFIG_PS3_FLASH_MODULE)
112static void __init prealloc(struct ps3_prealloc *p)
113{
114	if (!p->size)
115		return;
116
117	p->address = memblock_alloc(p->size, p->align);
118	if (!p->address)
119		panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
120		      __func__, p->size, p->align);
121
122	printk(KERN_INFO "%s: %lu bytes at %p\n", p->name, p->size,
123	       p->address);
124}
125#endif
126
127#if defined(CONFIG_FB_PS3) || defined(CONFIG_FB_PS3_MODULE)
128struct ps3_prealloc ps3fb_videomemory = {
129	.name = "ps3fb videomemory",
130	.size = CONFIG_FB_PS3_DEFAULT_SIZE_M*1024*1024,
131	.align = 1024*1024		/* the GPU requires 1 MiB alignment */
132};
133EXPORT_SYMBOL_GPL(ps3fb_videomemory);
134#define prealloc_ps3fb_videomemory()	prealloc(&ps3fb_videomemory)
135
136static int __init early_parse_ps3fb(char *p)
137{
138	if (!p)
139		return 1;
140
141	ps3fb_videomemory.size = ALIGN(memparse(p, &p),
142					   ps3fb_videomemory.align);
143	return 0;
144}
145early_param("ps3fb", early_parse_ps3fb);
146#else
147#define prealloc_ps3fb_videomemory()	do { } while (0)
148#endif
149
150#if defined(CONFIG_PS3_FLASH) || defined(CONFIG_PS3_FLASH_MODULE)
151struct ps3_prealloc ps3flash_bounce_buffer = {
152	.name = "ps3flash bounce buffer",
153	.size = 256*1024,
154	.align = 256*1024
155};
156EXPORT_SYMBOL_GPL(ps3flash_bounce_buffer);
157#define prealloc_ps3flash_bounce_buffer()	prealloc(&ps3flash_bounce_buffer)
158
159static int __init early_parse_ps3flash(char *p)
160{
161	if (!p)
162		return 1;
163
164	if (!strcmp(p, "off"))
165		ps3flash_bounce_buffer.size = 0;
166
167	return 0;
168}
169early_param("ps3flash", early_parse_ps3flash);
170#else
171#define prealloc_ps3flash_bounce_buffer()	do { } while (0)
172#endif
173
174static int ps3_set_dabr(unsigned long dabr, unsigned long dabrx)
175{
176	/* Have to set at least one bit in the DABRX */
177	if (dabrx == 0 && dabr == 0)
178		dabrx = DABRX_USER;
179	/* hypervisor only allows us to set BTI, Kernel and user */
180	dabrx &= DABRX_BTI | DABRX_KERNEL | DABRX_USER;
181
182	return lv1_set_dabr(dabr, dabrx) ? -1 : 0;
183}
184
185static void __init ps3_setup_arch(void)
186{
187	u64 tmp;
188
189	DBG(" -> %s:%d\n", __func__, __LINE__);
190
191	lv1_get_version_info(&ps3_firmware_version.raw, &tmp);
192
193	printk(KERN_INFO "PS3 firmware version %u.%u.%u\n",
194	       ps3_firmware_version.major, ps3_firmware_version.minor,
195	       ps3_firmware_version.rev);
196
197	ps3_spu_set_platform();
198
199#ifdef CONFIG_SMP
200	smp_init_ps3();
201#endif
202
203	prealloc_ps3fb_videomemory();
204	prealloc_ps3flash_bounce_buffer();
205
206	ppc_md.power_save = ps3_power_save;
207	ps3_os_area_init();
208
209	DBG(" <- %s:%d\n", __func__, __LINE__);
210}
211
212static void __init ps3_progress(char *s, unsigned short hex)
213{
214	printk("*** %04x : %s\n", hex, s ? s : "");
215}
216
217void __init ps3_early_mm_init(void)
218{
219	unsigned long htab_size;
220
221	ps3_mm_init();
222	ps3_mm_vas_create(&htab_size);
223	ps3_hpte_init(htab_size);
224}
225
226static int __init ps3_probe(void)
227{
228	DBG(" -> %s:%d\n", __func__, __LINE__);
229
230	if (!of_machine_is_compatible("sony,ps3"))
231		return 0;
232
233	ps3_os_area_save_params();
234
235	pm_power_off = ps3_power_off;
236
237	DBG(" <- %s:%d\n", __func__, __LINE__);
238	return 1;
239}
240
241#if defined(CONFIG_KEXEC_CORE)
242static void ps3_kexec_cpu_down(int crash_shutdown, int secondary)
243{
244	int cpu = smp_processor_id();
245
246	DBG(" -> %s:%d: (%d)\n", __func__, __LINE__, cpu);
247
248	ps3_smp_cleanup_cpu(cpu);
249	ps3_shutdown_IRQ(cpu);
250
251	DBG(" <- %s:%d\n", __func__, __LINE__);
252}
253#endif
254
255define_machine(ps3) {
256	.name				= "PS3",
257	.probe				= ps3_probe,
258	.setup_arch			= ps3_setup_arch,
259	.init_IRQ			= ps3_init_IRQ,
260	.panic				= ps3_panic,
261	.get_boot_time			= ps3_get_boot_time,
262	.set_dabr			= ps3_set_dabr,
263	.calibrate_decr			= ps3_calibrate_decr,
264	.progress			= ps3_progress,
265	.restart			= ps3_restart,
266	.halt				= ps3_halt,
267#if defined(CONFIG_KEXEC_CORE)
268	.kexec_cpu_down			= ps3_kexec_cpu_down,
269#endif
270};
271