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