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_or_panic(p->size, p->align);
119
120 printk(KERN_INFO "%s: %lu bytes at %p\n", p->name, p->size,
121 p->address);
122 }
123 #endif
124
125 #if defined(CONFIG_FB_PS3) || defined(CONFIG_FB_PS3_MODULE)
126 struct ps3_prealloc ps3fb_videomemory = {
127 .name = "ps3fb videomemory",
128 .size = CONFIG_FB_PS3_DEFAULT_SIZE_M*1024*1024,
129 .align = 1024*1024 /* the GPU requires 1 MiB alignment */
130 };
131 EXPORT_SYMBOL_GPL(ps3fb_videomemory);
132 #define prealloc_ps3fb_videomemory() prealloc(&ps3fb_videomemory)
133
early_parse_ps3fb(char * p)134 static int __init early_parse_ps3fb(char *p)
135 {
136 if (!p)
137 return 1;
138
139 ps3fb_videomemory.size = ALIGN(memparse(p, &p),
140 ps3fb_videomemory.align);
141 return 0;
142 }
143 early_param("ps3fb", early_parse_ps3fb);
144 #else
145 #define prealloc_ps3fb_videomemory() do { } while (0)
146 #endif
147
148 #if defined(CONFIG_PS3_FLASH) || defined(CONFIG_PS3_FLASH_MODULE)
149 struct ps3_prealloc ps3flash_bounce_buffer = {
150 .name = "ps3flash bounce buffer",
151 .size = 256*1024,
152 .align = 256*1024
153 };
154 EXPORT_SYMBOL_GPL(ps3flash_bounce_buffer);
155 #define prealloc_ps3flash_bounce_buffer() prealloc(&ps3flash_bounce_buffer)
156
early_parse_ps3flash(char * p)157 static int __init early_parse_ps3flash(char *p)
158 {
159 if (!p)
160 return 1;
161
162 if (!strcmp(p, "off"))
163 ps3flash_bounce_buffer.size = 0;
164
165 return 0;
166 }
167 early_param("ps3flash", early_parse_ps3flash);
168 #else
169 #define prealloc_ps3flash_bounce_buffer() do { } while (0)
170 #endif
171
ps3_set_dabr(unsigned long dabr,unsigned long dabrx)172 static int ps3_set_dabr(unsigned long dabr, unsigned long dabrx)
173 {
174 /* Have to set at least one bit in the DABRX */
175 if (dabrx == 0 && dabr == 0)
176 dabrx = DABRX_USER;
177 /* hypervisor only allows us to set BTI, Kernel and user */
178 dabrx &= DABRX_BTI | DABRX_KERNEL | DABRX_USER;
179
180 return lv1_set_dabr(dabr, dabrx) ? -1 : 0;
181 }
182
ps3_fw_version_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)183 static ssize_t ps3_fw_version_show(struct kobject *kobj,
184 struct kobj_attribute *attr, char *buf)
185 {
186 return sprintf(buf, "%s", ps3_firmware_version_str);
187 }
188
ps3_setup_sysfs(void)189 static int __init ps3_setup_sysfs(void)
190 {
191 static struct kobj_attribute attr = __ATTR(fw-version, S_IRUGO,
192 ps3_fw_version_show, NULL);
193 static struct kobject *kobj;
194 int result;
195
196 kobj = kobject_create_and_add("ps3", firmware_kobj);
197
198 if (!kobj) {
199 pr_warn("%s:%d: kobject_create_and_add failed.\n", __func__,
200 __LINE__);
201 return -ENOMEM;
202 }
203
204 result = sysfs_create_file(kobj, &attr.attr);
205
206 if (result) {
207 pr_warn("%s:%d: sysfs_create_file failed.\n", __func__,
208 __LINE__);
209 kobject_put(kobj);
210 return -ENOMEM;
211 }
212
213 return 0;
214 }
215 core_initcall(ps3_setup_sysfs);
216
ps3_setup_arch(void)217 static void __init ps3_setup_arch(void)
218 {
219 u64 tmp;
220
221 DBG(" -> %s:%d\n", __func__, __LINE__);
222
223 lv1_get_version_info(&ps3_firmware_version.raw, &tmp);
224
225 snprintf(ps3_firmware_version_str, sizeof(ps3_firmware_version_str),
226 "%u.%u.%u", ps3_firmware_version.major,
227 ps3_firmware_version.minor, ps3_firmware_version.rev);
228
229 printk(KERN_INFO "PS3 firmware version %s\n", ps3_firmware_version_str);
230
231 ps3_spu_set_platform();
232
233 #ifdef CONFIG_SMP
234 smp_init_ps3();
235 #endif
236
237 prealloc_ps3fb_videomemory();
238 prealloc_ps3flash_bounce_buffer();
239
240 ppc_md.power_save = ps3_power_save;
241 ps3_os_area_init();
242
243 DBG(" <- %s:%d\n", __func__, __LINE__);
244 }
245
ps3_progress(char * s,unsigned short hex)246 static void __init ps3_progress(char *s, unsigned short hex)
247 {
248 printk("*** %04x : %s\n", hex, s ? s : "");
249 }
250
ps3_early_mm_init(void)251 void __init ps3_early_mm_init(void)
252 {
253 unsigned long htab_size;
254
255 ps3_mm_init();
256 ps3_mm_vas_create(&htab_size);
257 ps3_hpte_init(htab_size);
258 }
259
ps3_probe(void)260 static int __init ps3_probe(void)
261 {
262 DBG(" -> %s:%d\n", __func__, __LINE__);
263
264 ps3_os_area_save_params();
265
266 pm_power_off = ps3_power_off;
267
268 DBG(" <- %s:%d\n", __func__, __LINE__);
269 return 1;
270 }
271
272 #if defined(CONFIG_KEXEC_CORE)
ps3_kexec_cpu_down(int crash_shutdown,int secondary)273 static void ps3_kexec_cpu_down(int crash_shutdown, int secondary)
274 {
275 int cpu = smp_processor_id();
276
277 DBG(" -> %s:%d: (%d)\n", __func__, __LINE__, cpu);
278
279 ps3_smp_cleanup_cpu(cpu);
280 ps3_shutdown_IRQ(cpu);
281
282 DBG(" <- %s:%d\n", __func__, __LINE__);
283 }
284 #endif
285
define_machine(ps3)286 define_machine(ps3) {
287 .name = "PS3",
288 .compatible = "sony,ps3",
289 .probe = ps3_probe,
290 .setup_arch = ps3_setup_arch,
291 .init_IRQ = ps3_init_IRQ,
292 .panic = ps3_panic,
293 .get_boot_time = ps3_get_boot_time,
294 .set_dabr = ps3_set_dabr,
295 .calibrate_decr = ps3_calibrate_decr,
296 .progress = ps3_progress,
297 .restart = ps3_restart,
298 .halt = ps3_halt,
299 #if defined(CONFIG_KEXEC_CORE)
300 .kexec_cpu_down = ps3_kexec_cpu_down,
301 #endif
302 };
303