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 #include <linux/of.h>
17 
18 #include <asm/machdep.h>
19 #include <asm/firmware.h>
20 #include <asm/time.h>
21 #include <asm/iommu.h>
22 #include <asm/udbg.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 */
35 DEFINE_MUTEX(ps3_gpu_mutex);
36 EXPORT_SYMBOL_GPL(ps3_gpu_mutex);
37 
38 static union ps3_firmware_version ps3_firmware_version;
39 static char ps3_firmware_version_str[16];
40 
ps3_get_firmware_version(union ps3_firmware_version * v)41 void ps3_get_firmware_version(union ps3_firmware_version *v)
42 {
43 	*v = ps3_firmware_version;
44 }
45 EXPORT_SYMBOL_GPL(ps3_get_firmware_version);
46 
ps3_compare_firmware_version(u16 major,u16 minor,u16 rev)47 int ps3_compare_firmware_version(u16 major, u16 minor, u16 rev)
48 {
49 	union ps3_firmware_version x;
50 
51 	x.pad = 0;
52 	x.major = major;
53 	x.minor = minor;
54 	x.rev = rev;
55 
56 	return (ps3_firmware_version.raw > x.raw) -
57 	       (ps3_firmware_version.raw < x.raw);
58 }
59 EXPORT_SYMBOL_GPL(ps3_compare_firmware_version);
60 
ps3_power_save(void)61 static void ps3_power_save(void)
62 {
63 	/*
64 	 * lv1_pause() puts the PPE thread into inactive state until an
65 	 * irq on an unmasked plug exists. MSR[EE] has no effect.
66 	 * flags: 0 = wake on DEC interrupt, 1 = ignore DEC interrupt.
67 	 */
68 
69 	lv1_pause(0);
70 }
71 
ps3_restart(char * cmd)72 static void __noreturn ps3_restart(char *cmd)
73 {
74 	DBG("%s:%d cmd '%s'\n", __func__, __LINE__, cmd);
75 
76 	smp_send_stop();
77 	ps3_sys_manager_restart(); /* never returns */
78 }
79 
ps3_power_off(void)80 static void ps3_power_off(void)
81 {
82 	DBG("%s:%d\n", __func__, __LINE__);
83 
84 	smp_send_stop();
85 	ps3_sys_manager_power_off(); /* never returns */
86 }
87 
ps3_halt(void)88 static void __noreturn ps3_halt(void)
89 {
90 	DBG("%s:%d\n", __func__, __LINE__);
91 
92 	smp_send_stop();
93 	ps3_sys_manager_halt(); /* never returns */
94 }
95 
ps3_panic(char * str)96 static void ps3_panic(char *str)
97 {
98 	DBG("%s:%d %s\n", __func__, __LINE__, str);
99 
100 	smp_send_stop();
101 	printk("\n");
102 	printk("   System does not reboot automatically.\n");
103 	printk("   Please press POWER button.\n");
104 	printk("\n");
105 	panic_flush_kmsg_end();
106 
107 	while(1)
108 		lv1_pause(1);
109 }
110 
111 #if defined(CONFIG_FB_PS3) || defined(CONFIG_FB_PS3_MODULE) || \
112     defined(CONFIG_PS3_FLASH) || defined(CONFIG_PS3_FLASH_MODULE)
prealloc(struct ps3_prealloc * p)113 static void __init prealloc(struct ps3_prealloc *p)
114 {
115 	if (!p->size)
116 		return;
117 
118 	p->address = memblock_alloc(p->size, p->align);
119 	if (!p->address)
120 		panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
121 		      __func__, p->size, p->align);
122 
123 	printk(KERN_INFO "%s: %lu bytes at %p\n", p->name, p->size,
124 	       p->address);
125 }
126 #endif
127 
128 #if defined(CONFIG_FB_PS3) || defined(CONFIG_FB_PS3_MODULE)
129 struct ps3_prealloc ps3fb_videomemory = {
130 	.name = "ps3fb videomemory",
131 	.size = CONFIG_FB_PS3_DEFAULT_SIZE_M*1024*1024,
132 	.align = 1024*1024		/* the GPU requires 1 MiB alignment */
133 };
134 EXPORT_SYMBOL_GPL(ps3fb_videomemory);
135 #define prealloc_ps3fb_videomemory()	prealloc(&ps3fb_videomemory)
136 
early_parse_ps3fb(char * p)137 static int __init early_parse_ps3fb(char *p)
138 {
139 	if (!p)
140 		return 1;
141 
142 	ps3fb_videomemory.size = ALIGN(memparse(p, &p),
143 					   ps3fb_videomemory.align);
144 	return 0;
145 }
146 early_param("ps3fb", early_parse_ps3fb);
147 #else
148 #define prealloc_ps3fb_videomemory()	do { } while (0)
149 #endif
150 
151 #if defined(CONFIG_PS3_FLASH) || defined(CONFIG_PS3_FLASH_MODULE)
152 struct ps3_prealloc ps3flash_bounce_buffer = {
153 	.name = "ps3flash bounce buffer",
154 	.size = 256*1024,
155 	.align = 256*1024
156 };
157 EXPORT_SYMBOL_GPL(ps3flash_bounce_buffer);
158 #define prealloc_ps3flash_bounce_buffer()	prealloc(&ps3flash_bounce_buffer)
159 
early_parse_ps3flash(char * p)160 static int __init early_parse_ps3flash(char *p)
161 {
162 	if (!p)
163 		return 1;
164 
165 	if (!strcmp(p, "off"))
166 		ps3flash_bounce_buffer.size = 0;
167 
168 	return 0;
169 }
170 early_param("ps3flash", early_parse_ps3flash);
171 #else
172 #define prealloc_ps3flash_bounce_buffer()	do { } while (0)
173 #endif
174 
ps3_set_dabr(unsigned long dabr,unsigned long dabrx)175 static int ps3_set_dabr(unsigned long dabr, unsigned long dabrx)
176 {
177 	/* Have to set at least one bit in the DABRX */
178 	if (dabrx == 0 && dabr == 0)
179 		dabrx = DABRX_USER;
180 	/* hypervisor only allows us to set BTI, Kernel and user */
181 	dabrx &= DABRX_BTI | DABRX_KERNEL | DABRX_USER;
182 
183 	return lv1_set_dabr(dabr, dabrx) ? -1 : 0;
184 }
185 
ps3_fw_version_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)186 static ssize_t ps3_fw_version_show(struct kobject *kobj,
187 	struct kobj_attribute *attr, char *buf)
188 {
189 	return sprintf(buf, "%s", ps3_firmware_version_str);
190 }
191 
ps3_setup_sysfs(void)192 static int __init ps3_setup_sysfs(void)
193 {
194 	static struct kobj_attribute attr = __ATTR(fw-version, S_IRUGO,
195 		ps3_fw_version_show, NULL);
196 	static struct kobject *kobj;
197 	int result;
198 
199 	kobj = kobject_create_and_add("ps3", firmware_kobj);
200 
201 	if (!kobj) {
202 		pr_warn("%s:%d: kobject_create_and_add failed.\n", __func__,
203 			__LINE__);
204 		return -ENOMEM;
205 	}
206 
207 	result = sysfs_create_file(kobj, &attr.attr);
208 
209 	if (result) {
210 		pr_warn("%s:%d: sysfs_create_file failed.\n", __func__,
211 			__LINE__);
212 		kobject_put(kobj);
213 		return -ENOMEM;
214 	}
215 
216 	return 0;
217 }
218 core_initcall(ps3_setup_sysfs);
219 
ps3_setup_arch(void)220 static void __init ps3_setup_arch(void)
221 {
222 	u64 tmp;
223 
224 	DBG(" -> %s:%d\n", __func__, __LINE__);
225 
226 	lv1_get_version_info(&ps3_firmware_version.raw, &tmp);
227 
228 	snprintf(ps3_firmware_version_str, sizeof(ps3_firmware_version_str),
229 		"%u.%u.%u", ps3_firmware_version.major,
230 		ps3_firmware_version.minor, ps3_firmware_version.rev);
231 
232 	printk(KERN_INFO "PS3 firmware version %s\n", ps3_firmware_version_str);
233 
234 	ps3_spu_set_platform();
235 
236 #ifdef CONFIG_SMP
237 	smp_init_ps3();
238 #endif
239 
240 	prealloc_ps3fb_videomemory();
241 	prealloc_ps3flash_bounce_buffer();
242 
243 	ppc_md.power_save = ps3_power_save;
244 	ps3_os_area_init();
245 
246 	DBG(" <- %s:%d\n", __func__, __LINE__);
247 }
248 
ps3_progress(char * s,unsigned short hex)249 static void __init ps3_progress(char *s, unsigned short hex)
250 {
251 	printk("*** %04x : %s\n", hex, s ? s : "");
252 }
253 
ps3_early_mm_init(void)254 void __init ps3_early_mm_init(void)
255 {
256 	unsigned long htab_size;
257 
258 	ps3_mm_init();
259 	ps3_mm_vas_create(&htab_size);
260 	ps3_hpte_init(htab_size);
261 }
262 
ps3_probe(void)263 static int __init ps3_probe(void)
264 {
265 	DBG(" -> %s:%d\n", __func__, __LINE__);
266 
267 	ps3_os_area_save_params();
268 
269 	pm_power_off = ps3_power_off;
270 
271 	DBG(" <- %s:%d\n", __func__, __LINE__);
272 	return 1;
273 }
274 
275 #if defined(CONFIG_KEXEC_CORE)
ps3_kexec_cpu_down(int crash_shutdown,int secondary)276 static void ps3_kexec_cpu_down(int crash_shutdown, int secondary)
277 {
278 	int cpu = smp_processor_id();
279 
280 	DBG(" -> %s:%d: (%d)\n", __func__, __LINE__, cpu);
281 
282 	ps3_smp_cleanup_cpu(cpu);
283 	ps3_shutdown_IRQ(cpu);
284 
285 	DBG(" <- %s:%d\n", __func__, __LINE__);
286 }
287 #endif
288 
define_machine(ps3)289 define_machine(ps3) {
290 	.name				= "PS3",
291 	.compatible			= "sony,ps3",
292 	.probe				= ps3_probe,
293 	.setup_arch			= ps3_setup_arch,
294 	.init_IRQ			= ps3_init_IRQ,
295 	.panic				= ps3_panic,
296 	.get_boot_time			= ps3_get_boot_time,
297 	.set_dabr			= ps3_set_dabr,
298 	.calibrate_decr			= ps3_calibrate_decr,
299 	.progress			= ps3_progress,
300 	.restart			= ps3_restart,
301 	.halt				= ps3_halt,
302 #if defined(CONFIG_KEXEC_CORE)
303 	.kexec_cpu_down			= ps3_kexec_cpu_down,
304 #endif
305 };
306