blob: 07fdad1604afa7c150fc3f641aadf4cfe5dcf7b8 [file] [log] [blame]
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001/*
2 * linux/drivers/video/ps3fb.c -- PS3 GPU frame buffer device
3 *
4 * Copyright (C) 2006 Sony Computer Entertainment Inc.
5 * Copyright 2006, 2007 Sony Corporation
6 *
7 * This file is based on :
8 *
9 * linux/drivers/video/vfb.c -- Virtual frame buffer device
10 *
11 * Copyright (C) 2002 James Simmons
12 *
13 * Copyright (C) 1997 Geert Uytterhoeven
14 *
15 * This file is subject to the terms and conditions of the GNU General Public
16 * License. See the file COPYING in the main directory of this archive for
17 * more details.
18 */
19
20#include <linux/module.h>
21#include <linux/kernel.h>
22#include <linux/errno.h>
23#include <linux/string.h>
24#include <linux/mm.h>
25#include <linux/tty.h>
26#include <linux/slab.h>
27#include <linux/vmalloc.h>
28#include <linux/delay.h>
29#include <linux/interrupt.h>
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -080030#include <linux/console.h>
31#include <linux/ioctl.h>
32#include <linux/notifier.h>
33#include <linux/reboot.h>
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +020034#include <linux/kthread.h>
35#include <linux/freezer.h>
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -080036
Krzysztof Helt84902b72007-10-16 01:29:04 -070037#include <linux/uaccess.h>
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -080038#include <linux/fb.h>
39#include <linux/init.h>
40#include <asm/time.h>
41
42#include <asm/abs_addr.h>
43#include <asm/lv1call.h>
44#include <asm/ps3av.h>
45#include <asm/ps3fb.h>
46#include <asm/ps3.h>
47
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +100048
49#define DEVICE_NAME "ps3fb"
50
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -080051#ifdef PS3FB_DEBUG
Geert Uytterhoevend778c9a2007-05-02 14:48:37 +020052#define DPRINTK(fmt, args...) printk("%s: " fmt, __func__ , ##args)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -080053#else
54#define DPRINTK(fmt, args...)
55#endif
56
57#define L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC 0x101
58#define L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP 0x102
59#define L1GPU_CONTEXT_ATTRIBUTE_FB_SETUP 0x600
60#define L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT 0x601
61#define L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT_SYNC 0x602
62
63#define L1GPU_FB_BLIT_WAIT_FOR_COMPLETION (1ULL << 32)
64
65#define L1GPU_DISPLAY_SYNC_HSYNC 1
66#define L1GPU_DISPLAY_SYNC_VSYNC 2
67
68#define DDR_SIZE (0) /* used no ddr */
69#define GPU_OFFSET (64 * 1024)
70#define GPU_IOIF (0x0d000000UL)
71
72#define PS3FB_FULL_MODE_BIT 0x80
73
74#define GPU_INTR_STATUS_VSYNC_0 0 /* vsync on head A */
75#define GPU_INTR_STATUS_VSYNC_1 1 /* vsync on head B */
76#define GPU_INTR_STATUS_FLIP_0 3 /* flip head A */
77#define GPU_INTR_STATUS_FLIP_1 4 /* flip head B */
78#define GPU_INTR_STATUS_QUEUE_0 5 /* queue head A */
79#define GPU_INTR_STATUS_QUEUE_1 6 /* queue head B */
80
81#define GPU_DRIVER_INFO_VERSION 0x211
82
83/* gpu internals */
84struct display_head {
85 u64 be_time_stamp;
86 u32 status;
87 u32 offset;
88 u32 res1;
89 u32 res2;
90 u32 field;
91 u32 reserved1;
92
93 u64 res3;
94 u32 raster;
95
96 u64 vblank_count;
97 u32 field_vsync;
98 u32 reserved2;
99};
100
101struct gpu_irq {
102 u32 irq_outlet;
103 u32 status;
104 u32 mask;
105 u32 video_cause;
106 u32 graph_cause;
107 u32 user_cause;
108
109 u32 res1;
110 u64 res2;
111
112 u32 reserved[4];
113};
114
115struct gpu_driver_info {
116 u32 version_driver;
117 u32 version_gpu;
118 u32 memory_size;
119 u32 hardware_channel;
120
121 u32 nvcore_frequency;
122 u32 memory_frequency;
123
124 u32 reserved[1063];
125 struct display_head display_head[8];
126 struct gpu_irq irq;
127};
128
129struct ps3fb_priv {
130 unsigned int irq_no;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800131
132 u64 context_handle, memory_handle;
133 void *xdr_ea;
134 struct gpu_driver_info *dinfo;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800135 u32 res_index;
136
137 u64 vblank_count; /* frame count */
138 wait_queue_head_t wait_vsync;
139
140 u32 num_frames; /* num of frame buffers */
141 atomic_t ext_flip; /* on/off flip with vsync */
142 atomic_t f_count; /* fb_open count */
143 int is_blanked;
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +0200144 int is_kicked;
145 struct task_struct *task;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800146};
147static struct ps3fb_priv ps3fb;
148
149struct ps3fb_res_table {
150 u32 xres;
151 u32 yres;
152 u32 xoff;
153 u32 yoff;
154 u32 type;
155};
156#define PS3FB_RES_FULL 1
157static const struct ps3fb_res_table ps3fb_res[] = {
158 /* res_x,y margin_x,y full */
159 { 720, 480, 72, 48 , 0},
160 { 720, 576, 72, 58 , 0},
161 { 1280, 720, 78, 38 , 0},
162 { 1920, 1080, 116, 58 , 0},
163 /* full mode */
164 { 720, 480, 0, 0 , PS3FB_RES_FULL},
165 { 720, 576, 0, 0 , PS3FB_RES_FULL},
166 { 1280, 720, 0, 0 , PS3FB_RES_FULL},
167 { 1920, 1080, 0, 0 , PS3FB_RES_FULL},
168 /* vesa: normally full mode */
169 { 1280, 768, 0, 0 , 0},
170 { 1280, 1024, 0, 0 , 0},
171 { 1920, 1200, 0, 0 , 0},
172 { 0, 0, 0, 0 , 0} };
173
174/* default resolution */
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000175#define GPU_RES_INDEX 0 /* 720 x 480 */
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800176
177static const struct fb_videomode ps3fb_modedb[] = {
178 /* 60 Hz broadcast modes (modes "1" to "5") */
179 {
180 /* 480i */
181 "480i", 60, 576, 384, 74074, 130, 89, 78, 57, 63, 6,
182 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
183 }, {
184 /* 480p */
185 "480p", 60, 576, 384, 37037, 130, 89, 78, 57, 63, 6,
186 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
187 }, {
188 /* 720p */
189 "720p", 60, 1124, 644, 13481, 298, 148, 57, 44, 80, 5,
190 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
191 }, {
192 /* 1080i */
193 "1080i", 60, 1688, 964, 13481, 264, 160, 94, 62, 88, 5,
194 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
195 }, {
196 /* 1080p */
197 "1080p", 60, 1688, 964, 6741, 264, 160, 94, 62, 88, 5,
198 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
199 },
200
201 /* 50 Hz broadcast modes (modes "6" to "10") */
202 {
203 /* 576i */
204 "576i", 50, 576, 460, 74074, 142, 83, 97, 63, 63, 5,
205 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
206 }, {
207 /* 576p */
208 "576p", 50, 576, 460, 37037, 142, 83, 97, 63, 63, 5,
209 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
210 }, {
211 /* 720p */
212 "720p", 50, 1124, 644, 13468, 298, 478, 57, 44, 80, 5,
213 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
214 }, {
215 /* 1080 */
216 "1080i", 50, 1688, 964, 13468, 264, 600, 94, 62, 88, 5,
217 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
218 }, {
219 /* 1080p */
220 "1080p", 50, 1688, 964, 6734, 264, 600, 94, 62, 88, 5,
221 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
222 },
223
224 /* VESA modes (modes "11" to "13") */
225 {
226 /* WXGA */
227 "wxga", 60, 1280, 768, 12924, 160, 24, 29, 3, 136, 6,
228 0, FB_VMODE_NONINTERLACED,
229 FB_MODE_IS_VESA
230 }, {
231 /* SXGA */
232 "sxga", 60, 1280, 1024, 9259, 248, 48, 38, 1, 112, 3,
233 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED,
234 FB_MODE_IS_VESA
235 }, {
236 /* WUXGA */
237 "wuxga", 60, 1920, 1200, 6494, 80, 48, 26, 3, 32, 6,
238 FB_SYNC_HOR_HIGH_ACT, FB_VMODE_NONINTERLACED,
239 FB_MODE_IS_VESA
240 },
241
242 /* 60 Hz broadcast modes (full resolution versions of modes "1" to "5") */
243 {
244 /* 480if */
245 "480if", 60, 720, 480, 74074, 58, 17, 30, 9, 63, 6,
246 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
247 }, {
248 /* 480pf */
249 "480pf", 60, 720, 480, 37037, 58, 17, 30, 9, 63, 6,
250 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
251 }, {
252 /* 720pf */
253 "720pf", 60, 1280, 720, 13481, 220, 70, 19, 6, 80, 5,
254 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
255 }, {
256 /* 1080if */
257 "1080if", 60, 1920, 1080, 13481, 148, 44, 36, 4, 88, 5,
258 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
259 }, {
260 /* 1080pf */
261 "1080pf", 60, 1920, 1080, 6741, 148, 44, 36, 4, 88, 5,
262 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
263 },
264
265 /* 50 Hz broadcast modes (full resolution versions of modes "6" to "10") */
266 {
267 /* 576if */
268 "576if", 50, 720, 576, 74074, 70, 11, 39, 5, 63, 5,
269 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
270 }, {
271 /* 576pf */
272 "576pf", 50, 720, 576, 37037, 70, 11, 39, 5, 63, 5,
273 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
274 }, {
275 /* 720pf */
276 "720pf", 50, 1280, 720, 13468, 220, 400, 19, 6, 80, 5,
277 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
278 }, {
279 /* 1080if */
280 "1080f", 50, 1920, 1080, 13468, 148, 484, 36, 4, 88, 5,
281 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
282 }, {
283 /* 1080pf */
284 "1080pf", 50, 1920, 1080, 6734, 148, 484, 36, 4, 88, 5,
285 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
286 }
287};
288
289
290#define HEAD_A
291#define HEAD_B
292
293#define X_OFF(i) (ps3fb_res[i].xoff) /* left/right margin (pixel) */
294#define Y_OFF(i) (ps3fb_res[i].yoff) /* top/bottom margin (pixel) */
295#define WIDTH(i) (ps3fb_res[i].xres) /* width of FB */
296#define HEIGHT(i) (ps3fb_res[i].yres) /* height of FB */
297#define BPP 4 /* number of bytes per pixel */
298#define VP_OFF(i) (WIDTH(i) * Y_OFF(i) * BPP + X_OFF(i) * BPP)
299#define FB_OFF(i) (GPU_OFFSET - VP_OFF(i) % GPU_OFFSET)
300
Geert Uytterhoevenbd685ac2007-05-02 14:48:34 +0200301static int ps3fb_mode;
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000302module_param(ps3fb_mode, int, 0);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800303
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000304static char *mode_option __devinitdata;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800305
Geert Uytterhoevenc95344a2007-10-16 01:29:25 -0700306static int ps3fb_get_res_table(u32 xres, u32 yres, int mode)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800307{
308 int full_mode;
309 unsigned int i;
310 u32 x, y, f;
311
Geert Uytterhoevenc95344a2007-10-16 01:29:25 -0700312 full_mode = (mode & PS3FB_FULL_MODE_BIT) ? PS3FB_RES_FULL : 0;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800313 for (i = 0;; i++) {
314 x = ps3fb_res[i].xres;
315 y = ps3fb_res[i].yres;
316 f = ps3fb_res[i].type;
317
318 if (!x) {
319 DPRINTK("ERROR: ps3fb_get_res_table()\n");
320 return -1;
321 }
322
323 if (full_mode == PS3FB_RES_FULL && f != PS3FB_RES_FULL)
324 continue;
325
326 if (x == xres && (yres == 0 || y == yres))
327 break;
328
329 x = x - 2 * ps3fb_res[i].xoff;
330 y = y - 2 * ps3fb_res[i].yoff;
331 if (x == xres && (yres == 0 || y == yres))
332 break;
333 }
334 return i;
335}
336
337static unsigned int ps3fb_find_mode(const struct fb_var_screeninfo *var,
338 u32 *line_length)
339{
340 unsigned int i, mode;
341
342 for (i = 0; i < ARRAY_SIZE(ps3fb_modedb); i++)
343 if (var->xres == ps3fb_modedb[i].xres &&
344 var->yres == ps3fb_modedb[i].yres &&
345 var->pixclock == ps3fb_modedb[i].pixclock &&
346 var->hsync_len == ps3fb_modedb[i].hsync_len &&
347 var->vsync_len == ps3fb_modedb[i].vsync_len &&
348 var->left_margin == ps3fb_modedb[i].left_margin &&
349 var->right_margin == ps3fb_modedb[i].right_margin &&
350 var->upper_margin == ps3fb_modedb[i].upper_margin &&
351 var->lower_margin == ps3fb_modedb[i].lower_margin &&
352 var->sync == ps3fb_modedb[i].sync &&
353 (var->vmode & FB_VMODE_MASK) == ps3fb_modedb[i].vmode) {
354 /* Cropped broadcast modes use the full line_length */
355 *line_length =
356 ps3fb_modedb[i < 10 ? i + 13 : i].xres * 4;
357 /* Full broadcast modes have the full mode bit set */
358 mode = i > 12 ? (i - 12) | PS3FB_FULL_MODE_BIT : i + 1;
359
360 DPRINTK("ps3fb_find_mode: mode %u\n", mode);
361 return mode;
362 }
363
364 DPRINTK("ps3fb_find_mode: mode not found\n");
365 return 0;
366
367}
368
369static const struct fb_videomode *ps3fb_default_mode(void)
370{
371 u32 mode = ps3fb_mode & PS3AV_MODE_MASK;
372 u32 flags;
373
374 if (mode < 1 || mode > 13)
375 return NULL;
376
377 flags = ps3fb_mode & ~PS3AV_MODE_MASK;
378
379 if (mode <= 10 && flags & PS3FB_FULL_MODE_BIT) {
380 /* Full broadcast mode */
381 return &ps3fb_modedb[mode + 12];
382 }
383
384 return &ps3fb_modedb[mode - 1];
385}
386
387static int ps3fb_sync(u32 frame)
388{
389 int i, status;
390 u32 xres, yres;
391 u64 fb_ioif, offset;
392
393 i = ps3fb.res_index;
394 xres = ps3fb_res[i].xres;
395 yres = ps3fb_res[i].yres;
396
397 if (frame > ps3fb.num_frames - 1) {
398 printk(KERN_WARNING "%s: invalid frame number (%u)\n",
Geert Uytterhoevend778c9a2007-05-02 14:48:37 +0200399 __func__, frame);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800400 return -EINVAL;
401 }
402 offset = xres * yres * BPP * frame;
403
404 fb_ioif = GPU_IOIF + FB_OFF(i) + offset;
405 status = lv1_gpu_context_attribute(ps3fb.context_handle,
406 L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT,
407 offset, fb_ioif,
408 L1GPU_FB_BLIT_WAIT_FOR_COMPLETION |
409 (xres << 16) | yres,
410 xres * BPP); /* line_length */
411 if (status)
Geert Uytterhoevend778c9a2007-05-02 14:48:37 +0200412 printk(KERN_ERR
413 "%s: lv1_gpu_context_attribute FB_BLIT failed: %d\n",
414 __func__, status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800415#ifdef HEAD_A
416 status = lv1_gpu_context_attribute(ps3fb.context_handle,
417 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
418 0, offset, 0, 0);
419 if (status)
Geert Uytterhoevend778c9a2007-05-02 14:48:37 +0200420 printk(KERN_ERR
421 "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
422 __func__, status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800423#endif
424#ifdef HEAD_B
425 status = lv1_gpu_context_attribute(ps3fb.context_handle,
426 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
427 1, offset, 0, 0);
428 if (status)
Geert Uytterhoevend778c9a2007-05-02 14:48:37 +0200429 printk(KERN_ERR
430 "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
431 __func__, status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800432#endif
433 return 0;
434}
435
436
437static int ps3fb_open(struct fb_info *info, int user)
438{
439 atomic_inc(&ps3fb.f_count);
440 return 0;
441}
442
443static int ps3fb_release(struct fb_info *info, int user)
444{
445 if (atomic_dec_and_test(&ps3fb.f_count)) {
446 if (atomic_read(&ps3fb.ext_flip)) {
447 atomic_set(&ps3fb.ext_flip, 0);
448 ps3fb_sync(0); /* single buffer */
449 }
450 }
451 return 0;
452}
453
454 /*
455 * Setting the video mode has been split into two parts.
456 * First part, xxxfb_check_var, must not write anything
457 * to hardware, it should only verify and adjust var.
458 * This means it doesn't alter par but it does use hardware
459 * data from it to check this var.
460 */
461
462static int ps3fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
463{
464 u32 line_length;
465 int mode;
466 int i;
467
468 DPRINTK("var->xres:%u info->var.xres:%u\n", var->xres, info->var.xres);
469 DPRINTK("var->yres:%u info->var.yres:%u\n", var->yres, info->var.yres);
470
471 /* FIXME For now we do exact matches only */
472 mode = ps3fb_find_mode(var, &line_length);
473 if (!mode)
474 return -EINVAL;
475
476 /*
477 * FB_VMODE_CONUPDATE and FB_VMODE_SMOOTH_XPAN are equal!
478 * as FB_VMODE_SMOOTH_XPAN is only used internally
479 */
480
481 if (var->vmode & FB_VMODE_CONUPDATE) {
482 var->vmode |= FB_VMODE_YWRAP;
483 var->xoffset = info->var.xoffset;
484 var->yoffset = info->var.yoffset;
485 }
486
487 /* Virtual screen and panning are not supported */
488 if (var->xres_virtual > var->xres || var->yres_virtual > var->yres ||
489 var->xoffset || var->yoffset) {
490 DPRINTK("Virtual screen and panning are not supported\n");
491 return -EINVAL;
492 }
493
494 var->xres_virtual = var->xres;
495 var->yres_virtual = var->yres;
496
497 /* We support ARGB8888 only */
498 if (var->bits_per_pixel > 32 || var->grayscale ||
499 var->red.offset > 16 || var->green.offset > 8 ||
500 var->blue.offset > 0 || var->transp.offset > 24 ||
501 var->red.length > 8 || var->green.length > 8 ||
502 var->blue.length > 8 || var->transp.length > 8 ||
503 var->red.msb_right || var->green.msb_right ||
504 var->blue.msb_right || var->transp.msb_right || var->nonstd) {
505 DPRINTK("We support ARGB8888 only\n");
506 return -EINVAL;
507 }
508
509 var->bits_per_pixel = 32;
510 var->red.offset = 16;
511 var->green.offset = 8;
512 var->blue.offset = 0;
513 var->transp.offset = 24;
514 var->red.length = 8;
515 var->green.length = 8;
516 var->blue.length = 8;
517 var->transp.length = 8;
518 var->red.msb_right = 0;
519 var->green.msb_right = 0;
520 var->blue.msb_right = 0;
521 var->transp.msb_right = 0;
522
523 /* Rotation is not supported */
524 if (var->rotate) {
525 DPRINTK("Rotation is not supported\n");
526 return -EINVAL;
527 }
528
529 /* Memory limit */
Geert Uytterhoevenc95344a2007-10-16 01:29:25 -0700530 i = ps3fb_get_res_table(var->xres, var->yres, mode);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800531 if (ps3fb_res[i].xres*ps3fb_res[i].yres*BPP > ps3fb_videomemory.size) {
532 DPRINTK("Not enough memory\n");
533 return -ENOMEM;
534 }
535
536 var->height = -1;
537 var->width = -1;
538
539 return 0;
540}
541
542 /*
543 * This routine actually sets the video mode.
544 */
545
546static int ps3fb_set_par(struct fb_info *info)
547{
548 unsigned int mode;
549 int i;
550 unsigned long offset;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800551
552 DPRINTK("xres:%d xv:%d yres:%d yv:%d clock:%d\n",
553 info->var.xres, info->var.xres_virtual,
554 info->var.yres, info->var.yres_virtual, info->var.pixclock);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800555
556 mode = ps3fb_find_mode(&info->var, &info->fix.line_length);
557 if (!mode)
558 return -EINVAL;
559
Geert Uytterhoevenc95344a2007-10-16 01:29:25 -0700560 i = ps3fb_get_res_table(info->var.xres, info->var.yres, mode);
561 ps3fb.res_index = i;
562
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800563 offset = FB_OFF(i) + VP_OFF(i);
564 info->fix.smem_len = ps3fb_videomemory.size - offset;
565 info->screen_base = (char __iomem *)ps3fb.xdr_ea + offset;
566 memset(ps3fb.xdr_ea, 0, ps3fb_videomemory.size);
567
568 ps3fb.num_frames = ps3fb_videomemory.size/
569 (ps3fb_res[i].xres*ps3fb_res[i].yres*BPP);
570
571 /* Keep the special bits we cannot set using fb_var_screeninfo */
572 ps3fb_mode = (ps3fb_mode & ~PS3AV_MODE_MASK) | mode;
573
Geert Uytterhoevence4c3712007-10-16 01:29:44 -0700574 if (ps3av_set_video_mode(ps3fb_mode))
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800575 return -EINVAL;
576
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800577 return 0;
578}
579
580 /*
581 * Set a single color register. The values supplied are already
582 * rounded down to the hardware's capabilities (according to the
583 * entries in the var structure). Return != 0 for invalid regno.
584 */
585
586static int ps3fb_setcolreg(unsigned int regno, unsigned int red,
587 unsigned int green, unsigned int blue,
588 unsigned int transp, struct fb_info *info)
589{
590 if (regno >= 16)
591 return 1;
592
593 red >>= 8;
594 green >>= 8;
595 blue >>= 8;
596 transp >>= 8;
597
598 ((u32 *)info->pseudo_palette)[regno] = transp << 24 | red << 16 |
599 green << 8 | blue;
600 return 0;
601}
602
603 /*
604 * As we have a virtual frame buffer, we need our own mmap function
605 */
606
607static int ps3fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
608{
609 unsigned long size, offset;
610 int i;
611
Geert Uytterhoevenc95344a2007-10-16 01:29:25 -0700612 i = ps3fb_get_res_table(info->var.xres, info->var.yres, ps3fb_mode);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800613 if (i == -1)
614 return -EINVAL;
615
616 size = vma->vm_end - vma->vm_start;
617 offset = vma->vm_pgoff << PAGE_SHIFT;
618 if (offset + size > info->fix.smem_len)
619 return -EINVAL;
620
621 offset += info->fix.smem_start + FB_OFF(i) + VP_OFF(i);
622 if (remap_pfn_range(vma, vma->vm_start, offset >> PAGE_SHIFT,
623 size, vma->vm_page_prot))
624 return -EAGAIN;
625
626 printk(KERN_DEBUG "ps3fb: mmap framebuffer P(%lx)->V(%lx)\n", offset,
627 vma->vm_start);
628 return 0;
629}
630
631 /*
632 * Blank the display
633 */
634
635static int ps3fb_blank(int blank, struct fb_info *info)
636{
637 int retval;
638
Geert Uytterhoevend778c9a2007-05-02 14:48:37 +0200639 DPRINTK("%s: blank:%d\n", __func__, blank);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800640 switch (blank) {
641 case FB_BLANK_POWERDOWN:
642 case FB_BLANK_HSYNC_SUSPEND:
643 case FB_BLANK_VSYNC_SUSPEND:
644 case FB_BLANK_NORMAL:
645 retval = ps3av_video_mute(1); /* mute on */
646 if (!retval)
647 ps3fb.is_blanked = 1;
648 break;
649
650 default: /* unblank */
651 retval = ps3av_video_mute(0); /* mute off */
652 if (!retval)
653 ps3fb.is_blanked = 0;
654 break;
655 }
656 return retval;
657}
658
659static int ps3fb_get_vblank(struct fb_vblank *vblank)
660{
661 memset(vblank, 0, sizeof(&vblank));
662 vblank->flags = FB_VBLANK_HAVE_VSYNC;
663 return 0;
664}
665
666int ps3fb_wait_for_vsync(u32 crtc)
667{
668 int ret;
669 u64 count;
670
671 count = ps3fb.vblank_count;
672 ret = wait_event_interruptible_timeout(ps3fb.wait_vsync,
673 count != ps3fb.vblank_count,
674 HZ / 10);
675 if (!ret)
676 return -ETIMEDOUT;
677
678 return 0;
679}
680
681EXPORT_SYMBOL_GPL(ps3fb_wait_for_vsync);
682
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000683void ps3fb_flip_ctl(int on, void *data)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800684{
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000685 struct ps3fb_priv *priv = data;
Geert Uytterhoeveneca28742007-05-02 14:48:32 +0200686 if (on)
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000687 atomic_dec_if_positive(&priv->ext_flip);
Geert Uytterhoeveneca28742007-05-02 14:48:32 +0200688 else
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000689 atomic_inc(&priv->ext_flip);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800690}
691
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800692
693 /*
694 * ioctl
695 */
696
697static int ps3fb_ioctl(struct fb_info *info, unsigned int cmd,
698 unsigned long arg)
699{
700 void __user *argp = (void __user *)arg;
701 u32 val, old_mode;
702 int retval = -EFAULT;
703
704 switch (cmd) {
705 case FBIOGET_VBLANK:
706 {
707 struct fb_vblank vblank;
708 DPRINTK("FBIOGET_VBLANK:\n");
709 retval = ps3fb_get_vblank(&vblank);
710 if (retval)
711 break;
712
713 if (copy_to_user(argp, &vblank, sizeof(vblank)))
714 retval = -EFAULT;
715 break;
716 }
717
718 case FBIO_WAITFORVSYNC:
719 {
720 u32 crt;
721 DPRINTK("FBIO_WAITFORVSYNC:\n");
722 if (get_user(crt, (u32 __user *) arg))
723 break;
724
725 retval = ps3fb_wait_for_vsync(crt);
726 break;
727 }
728
729 case PS3FB_IOCTL_SETMODE:
730 {
731 const struct fb_videomode *mode;
732 struct fb_var_screeninfo var;
733
734 if (copy_from_user(&val, argp, sizeof(val)))
735 break;
736
Masashi Kimoto64072902007-05-02 14:48:36 +0200737 if (!(val & PS3AV_MODE_MASK)) {
Geert Uytterhoevence4c3712007-10-16 01:29:44 -0700738 u32 id = ps3av_get_auto_mode();
Masashi Kimoto64072902007-05-02 14:48:36 +0200739 if (id > 0)
740 val = (val & ~PS3AV_MODE_MASK) | id;
741 }
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800742 DPRINTK("PS3FB_IOCTL_SETMODE:%x\n", val);
743 retval = -EINVAL;
744 old_mode = ps3fb_mode;
745 ps3fb_mode = val;
746 mode = ps3fb_default_mode();
747 if (mode) {
748 var = info->var;
749 fb_videomode_to_var(&var, mode);
750 acquire_console_sem();
751 info->flags |= FBINFO_MISC_USEREVENT;
752 /* Force, in case only special bits changed */
753 var.activate |= FB_ACTIVATE_FORCE;
754 retval = fb_set_var(info, &var);
755 info->flags &= ~FBINFO_MISC_USEREVENT;
756 release_console_sem();
757 }
758 if (retval)
759 ps3fb_mode = old_mode;
760 break;
761 }
762
763 case PS3FB_IOCTL_GETMODE:
764 val = ps3av_get_mode();
765 DPRINTK("PS3FB_IOCTL_GETMODE:%x\n", val);
766 if (!copy_to_user(argp, &val, sizeof(val)))
767 retval = 0;
768 break;
769
770 case PS3FB_IOCTL_SCREENINFO:
771 {
772 struct ps3fb_ioctl_res res;
773 int i = ps3fb.res_index;
774 DPRINTK("PS3FB_IOCTL_SCREENINFO:\n");
775 res.xres = ps3fb_res[i].xres;
776 res.yres = ps3fb_res[i].yres;
777 res.xoff = ps3fb_res[i].xoff;
778 res.yoff = ps3fb_res[i].yoff;
779 res.num_frames = ps3fb.num_frames;
780 if (!copy_to_user(argp, &res, sizeof(res)))
781 retval = 0;
782 break;
783 }
784
785 case PS3FB_IOCTL_ON:
786 DPRINTK("PS3FB_IOCTL_ON:\n");
787 atomic_inc(&ps3fb.ext_flip);
788 retval = 0;
789 break;
790
791 case PS3FB_IOCTL_OFF:
792 DPRINTK("PS3FB_IOCTL_OFF:\n");
Geert Uytterhoeveneca28742007-05-02 14:48:32 +0200793 atomic_dec_if_positive(&ps3fb.ext_flip);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800794 retval = 0;
795 break;
796
797 case PS3FB_IOCTL_FSEL:
798 if (copy_from_user(&val, argp, sizeof(val)))
799 break;
800
801 DPRINTK("PS3FB_IOCTL_FSEL:%d\n", val);
802 retval = ps3fb_sync(val);
803 break;
804
805 default:
806 retval = -ENOIOCTLCMD;
807 break;
808 }
809 return retval;
810}
811
812static int ps3fbd(void *arg)
813{
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700814 set_freezable();
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +0200815 while (!kthread_should_stop()) {
816 try_to_freeze();
817 set_current_state(TASK_INTERRUPTIBLE);
818 if (ps3fb.is_kicked) {
819 ps3fb.is_kicked = 0;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800820 ps3fb_sync(0); /* single buffer */
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +0200821 }
822 schedule();
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800823 }
824 return 0;
825}
826
827static irqreturn_t ps3fb_vsync_interrupt(int irq, void *ptr)
828{
829 u64 v1;
830 int status;
831 struct display_head *head = &ps3fb.dinfo->display_head[1];
832
833 status = lv1_gpu_context_intr(ps3fb.context_handle, &v1);
834 if (status) {
835 printk(KERN_ERR "%s: lv1_gpu_context_intr failed: %d\n",
Geert Uytterhoevend778c9a2007-05-02 14:48:37 +0200836 __func__, status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800837 return IRQ_NONE;
838 }
839
840 if (v1 & (1 << GPU_INTR_STATUS_VSYNC_1)) {
841 /* VSYNC */
842 ps3fb.vblank_count = head->vblank_count;
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +0200843 if (ps3fb.task && !ps3fb.is_blanked &&
844 !atomic_read(&ps3fb.ext_flip)) {
845 ps3fb.is_kicked = 1;
846 wake_up_process(ps3fb.task);
847 }
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800848 wake_up_interruptible(&ps3fb.wait_vsync);
849 }
850
851 return IRQ_HANDLED;
852}
853
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800854
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000855static int ps3fb_vsync_settings(struct gpu_driver_info *dinfo,
856 struct ps3_system_bus_device *dev)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800857{
858 int error;
859
860 DPRINTK("version_driver:%x\n", dinfo->version_driver);
861 DPRINTK("irq outlet:%x\n", dinfo->irq.irq_outlet);
862 DPRINTK("version_gpu:%x memory_size:%x ch:%x core_freq:%d mem_freq:%d\n",
863 dinfo->version_gpu, dinfo->memory_size, dinfo->hardware_channel,
864 dinfo->nvcore_frequency/1000000, dinfo->memory_frequency/1000000);
865
866 if (dinfo->version_driver != GPU_DRIVER_INFO_VERSION) {
Geert Uytterhoevend778c9a2007-05-02 14:48:37 +0200867 printk(KERN_ERR "%s: version_driver err:%x\n", __func__,
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800868 dinfo->version_driver);
869 return -EINVAL;
870 }
871
Geoff Levanddc4f60c2007-05-01 07:01:01 +1000872 error = ps3_irq_plug_setup(PS3_BINDING_CPU_ANY, dinfo->irq.irq_outlet,
873 &ps3fb.irq_no);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800874 if (error) {
Geert Uytterhoevend778c9a2007-05-02 14:48:37 +0200875 printk(KERN_ERR "%s: ps3_alloc_irq failed %d\n", __func__,
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800876 error);
877 return error;
878 }
879
880 error = request_irq(ps3fb.irq_no, ps3fb_vsync_interrupt, IRQF_DISABLED,
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000881 DEVICE_NAME, dev);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800882 if (error) {
Geert Uytterhoevend778c9a2007-05-02 14:48:37 +0200883 printk(KERN_ERR "%s: request_irq failed %d\n", __func__,
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800884 error);
Geoff Levanddc4f60c2007-05-01 07:01:01 +1000885 ps3_irq_plug_destroy(ps3fb.irq_no);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800886 return error;
887 }
888
889 dinfo->irq.mask = (1 << GPU_INTR_STATUS_VSYNC_1) |
890 (1 << GPU_INTR_STATUS_FLIP_1);
891 return 0;
892}
893
894static int ps3fb_xdr_settings(u64 xdr_lpar)
895{
896 int status;
897
898 status = lv1_gpu_context_iomap(ps3fb.context_handle, GPU_IOIF,
899 xdr_lpar, ps3fb_videomemory.size, 0);
900 if (status) {
901 printk(KERN_ERR "%s: lv1_gpu_context_iomap failed: %d\n",
Geert Uytterhoevend778c9a2007-05-02 14:48:37 +0200902 __func__, status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800903 return -ENXIO;
904 }
905 DPRINTK("video:%p xdr_ea:%p ioif:%lx lpar:%lx phys:%lx size:%lx\n",
906 ps3fb_videomemory.address, ps3fb.xdr_ea, GPU_IOIF, xdr_lpar,
907 virt_to_abs(ps3fb.xdr_ea), ps3fb_videomemory.size);
908
909 status = lv1_gpu_context_attribute(ps3fb.context_handle,
910 L1GPU_CONTEXT_ATTRIBUTE_FB_SETUP,
911 xdr_lpar, ps3fb_videomemory.size,
912 GPU_IOIF, 0);
913 if (status) {
Geert Uytterhoevend778c9a2007-05-02 14:48:37 +0200914 printk(KERN_ERR
915 "%s: lv1_gpu_context_attribute FB_SETUP failed: %d\n",
916 __func__, status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800917 return -ENXIO;
918 }
919 return 0;
920}
921
922static struct fb_ops ps3fb_ops = {
923 .fb_open = ps3fb_open,
924 .fb_release = ps3fb_release,
Geert Uytterhoeven92c45792007-05-23 13:57:50 -0700925 .fb_read = fb_sys_read,
926 .fb_write = fb_sys_write,
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800927 .fb_check_var = ps3fb_check_var,
928 .fb_set_par = ps3fb_set_par,
929 .fb_setcolreg = ps3fb_setcolreg,
Geert Uytterhoeven92c45792007-05-23 13:57:50 -0700930 .fb_fillrect = sys_fillrect,
931 .fb_copyarea = sys_copyarea,
932 .fb_imageblit = sys_imageblit,
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800933 .fb_mmap = ps3fb_mmap,
934 .fb_blank = ps3fb_blank,
935 .fb_ioctl = ps3fb_ioctl,
936 .fb_compat_ioctl = ps3fb_ioctl
937};
938
939static struct fb_fix_screeninfo ps3fb_fix __initdata = {
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000940 .id = DEVICE_NAME,
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800941 .type = FB_TYPE_PACKED_PIXELS,
942 .visual = FB_VISUAL_TRUECOLOR,
943 .accel = FB_ACCEL_NONE,
944};
945
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000946static int ps3fb_set_sync(void)
947{
948 int status;
949
950#ifdef HEAD_A
951 status = lv1_gpu_context_attribute(0x0,
952 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
953 0, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);
954 if (status) {
955 printk(KERN_ERR "%s: lv1_gpu_context_attribute DISPLAY_SYNC "
956 "failed: %d\n", __func__, status);
957 return -1;
958 }
959#endif
960#ifdef HEAD_B
961 status = lv1_gpu_context_attribute(0x0,
962 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
963 1, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);
964
965 if (status) {
966 printk(KERN_ERR "%s: lv1_gpu_context_attribute DISPLAY_MODE "
967 "failed: %d\n", __func__, status);
968 return -1;
969 }
970#endif
971 return 0;
972}
973
974static int __devinit ps3fb_probe(struct ps3_system_bus_device *dev)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800975{
976 struct fb_info *info;
977 int retval = -ENOMEM;
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000978 u32 xres, yres;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800979 u64 ddr_lpar = 0;
980 u64 lpar_dma_control = 0;
981 u64 lpar_driver_info = 0;
982 u64 lpar_reports = 0;
983 u64 lpar_reports_size = 0;
984 u64 xdr_lpar;
985 int status;
986 unsigned long offset;
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +0200987 struct task_struct *task;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800988
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000989 status = ps3_open_hv_device(dev);
990 if (status) {
991 printk(KERN_ERR "%s: ps3_open_hv_device failed\n", __func__);
992 goto err;
993 }
994
995 if (!ps3fb_mode)
996 ps3fb_mode = ps3av_get_mode();
997 DPRINTK("ps3av_mode:%d\n", ps3fb_mode);
998
999 if (ps3fb_mode > 0 &&
1000 !ps3av_video_mode2res(ps3fb_mode, &xres, &yres)) {
Geert Uytterhoevenc95344a2007-10-16 01:29:25 -07001001 ps3fb.res_index = ps3fb_get_res_table(xres, yres, ps3fb_mode);
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001002 DPRINTK("res_index:%d\n", ps3fb.res_index);
1003 } else
1004 ps3fb.res_index = GPU_RES_INDEX;
1005
1006 atomic_set(&ps3fb.f_count, -1); /* fbcon opens ps3fb */
1007 atomic_set(&ps3fb.ext_flip, 0); /* for flip with vsync */
1008 init_waitqueue_head(&ps3fb.wait_vsync);
1009 ps3fb.num_frames = 1;
1010
1011 ps3fb_set_sync();
1012
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001013 /* get gpu context handle */
1014 status = lv1_gpu_memory_allocate(DDR_SIZE, 0, 0, 0, 0,
1015 &ps3fb.memory_handle, &ddr_lpar);
1016 if (status) {
1017 printk(KERN_ERR "%s: lv1_gpu_memory_allocate failed: %d\n",
Geert Uytterhoevend778c9a2007-05-02 14:48:37 +02001018 __func__, status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001019 goto err;
1020 }
1021 DPRINTK("ddr:lpar:0x%lx\n", ddr_lpar);
1022
1023 status = lv1_gpu_context_allocate(ps3fb.memory_handle, 0,
1024 &ps3fb.context_handle,
1025 &lpar_dma_control, &lpar_driver_info,
1026 &lpar_reports, &lpar_reports_size);
1027 if (status) {
1028 printk(KERN_ERR "%s: lv1_gpu_context_attribute failed: %d\n",
Geert Uytterhoevend778c9a2007-05-02 14:48:37 +02001029 __func__, status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001030 goto err_gpu_memory_free;
1031 }
1032
1033 /* vsync interrupt */
1034 ps3fb.dinfo = ioremap(lpar_driver_info, 128 * 1024);
1035 if (!ps3fb.dinfo) {
Geert Uytterhoevend778c9a2007-05-02 14:48:37 +02001036 printk(KERN_ERR "%s: ioremap failed\n", __func__);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001037 goto err_gpu_context_free;
1038 }
1039
1040 retval = ps3fb_vsync_settings(ps3fb.dinfo, dev);
1041 if (retval)
1042 goto err_iounmap_dinfo;
1043
1044 /* xdr frame buffer */
1045 ps3fb.xdr_ea = ps3fb_videomemory.address;
1046 xdr_lpar = ps3_mm_phys_to_lpar(__pa(ps3fb.xdr_ea));
1047 retval = ps3fb_xdr_settings(xdr_lpar);
1048 if (retval)
1049 goto err_free_irq;
1050
1051 /*
1052 * ps3fb must clear memory to prevent kernel info
1053 * leakage into userspace
1054 */
1055 memset(ps3fb.xdr_ea, 0, ps3fb_videomemory.size);
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001056 info = framebuffer_alloc(sizeof(u32) * 16, &dev->core);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001057 if (!info)
1058 goto err_free_irq;
1059
1060 offset = FB_OFF(ps3fb.res_index) + VP_OFF(ps3fb.res_index);
1061 info->screen_base = (char __iomem *)ps3fb.xdr_ea + offset;
1062 info->fbops = &ps3fb_ops;
1063
1064 info->fix = ps3fb_fix;
1065 info->fix.smem_start = virt_to_abs(ps3fb.xdr_ea);
1066 info->fix.smem_len = ps3fb_videomemory.size - offset;
1067 info->pseudo_palette = info->par;
1068 info->par = NULL;
Geert Uytterhoevencfd13af2007-07-21 04:37:51 -07001069 info->flags = FBINFO_DEFAULT | FBINFO_READS_FAST;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001070
1071 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1072 if (retval < 0)
1073 goto err_framebuffer_release;
1074
1075 if (!fb_find_mode(&info->var, info, mode_option, ps3fb_modedb,
1076 ARRAY_SIZE(ps3fb_modedb), ps3fb_default_mode(), 32)) {
1077 retval = -EINVAL;
1078 goto err_fb_dealloc;
1079 }
1080
1081 fb_videomode_to_modelist(ps3fb_modedb, ARRAY_SIZE(ps3fb_modedb),
1082 &info->modelist);
1083
1084 retval = register_framebuffer(info);
1085 if (retval < 0)
1086 goto err_fb_dealloc;
1087
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001088 dev->core.driver_data = info;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001089
1090 printk(KERN_INFO
1091 "fb%d: PS3 frame buffer device, using %ld KiB of video memory\n",
1092 info->node, ps3fb_videomemory.size >> 10);
1093
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001094 task = kthread_run(ps3fbd, info, DEVICE_NAME);
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +02001095 if (IS_ERR(task)) {
1096 retval = PTR_ERR(task);
1097 goto err_unregister_framebuffer;
1098 }
1099
1100 ps3fb.task = task;
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001101 ps3av_register_flip_ctl(ps3fb_flip_ctl, &ps3fb);
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +02001102
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001103 return 0;
1104
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +02001105err_unregister_framebuffer:
1106 unregister_framebuffer(info);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001107err_fb_dealloc:
1108 fb_dealloc_cmap(&info->cmap);
1109err_framebuffer_release:
1110 framebuffer_release(info);
1111err_free_irq:
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001112 free_irq(ps3fb.irq_no, dev);
Geoff Levanddc4f60c2007-05-01 07:01:01 +10001113 ps3_irq_plug_destroy(ps3fb.irq_no);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001114err_iounmap_dinfo:
1115 iounmap((u8 __iomem *)ps3fb.dinfo);
1116err_gpu_context_free:
1117 lv1_gpu_context_free(ps3fb.context_handle);
1118err_gpu_memory_free:
1119 lv1_gpu_memory_free(ps3fb.memory_handle);
1120err:
1121 return retval;
1122}
1123
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001124static int ps3fb_shutdown(struct ps3_system_bus_device *dev)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001125{
1126 int status;
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001127 struct fb_info *info = dev->core.driver_data;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001128
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001129 DPRINTK(" -> %s:%d\n", __func__, __LINE__);
1130
1131 ps3fb_flip_ctl(0, &ps3fb); /* flip off */
1132 ps3fb.dinfo->irq.mask = 0;
1133
1134 if (info) {
1135 unregister_framebuffer(info);
1136 fb_dealloc_cmap(&info->cmap);
1137 framebuffer_release(info);
1138 }
1139
1140 ps3av_register_flip_ctl(NULL, NULL);
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +02001141 if (ps3fb.task) {
1142 struct task_struct *task = ps3fb.task;
1143 ps3fb.task = NULL;
1144 kthread_stop(task);
1145 }
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001146 if (ps3fb.irq_no) {
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001147 free_irq(ps3fb.irq_no, dev);
Geoff Levanddc4f60c2007-05-01 07:01:01 +10001148 ps3_irq_plug_destroy(ps3fb.irq_no);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001149 }
1150 iounmap((u8 __iomem *)ps3fb.dinfo);
1151
1152 status = lv1_gpu_context_free(ps3fb.context_handle);
1153 if (status)
1154 DPRINTK("lv1_gpu_context_free failed: %d\n", status);
1155
1156 status = lv1_gpu_memory_free(ps3fb.memory_handle);
1157 if (status)
1158 DPRINTK("lv1_gpu_memory_free failed: %d\n", status);
1159
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001160 ps3_close_hv_device(dev);
1161 DPRINTK(" <- %s:%d\n", __func__, __LINE__);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001162
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001163 return 0;
1164}
1165
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001166static struct ps3_system_bus_driver ps3fb_driver = {
1167 .match_id = PS3_MATCH_ID_GRAPHICS,
1168 .core.name = DEVICE_NAME,
1169 .core.owner = THIS_MODULE,
1170 .probe = ps3fb_probe,
1171 .remove = ps3fb_shutdown,
1172 .shutdown = ps3fb_shutdown,
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001173};
1174
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001175static int __init ps3fb_setup(void)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001176{
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001177 char *options;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001178
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001179#ifdef MODULE
1180 return 0;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001181#endif
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001182
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001183 if (fb_get_options(DEVICE_NAME, &options))
1184 return -ENXIO;
1185
1186 if (!options || !*options)
1187 return 0;
1188
1189 while (1) {
1190 char *this_opt = strsep(&options, ",");
1191
1192 if (!this_opt)
1193 break;
1194 if (!*this_opt)
1195 continue;
1196 if (!strncmp(this_opt, "mode:", 5))
1197 ps3fb_mode = simple_strtoul(this_opt + 5, NULL, 0);
1198 else
1199 mode_option = this_opt;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001200 }
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001201 return 0;
1202}
1203
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001204static int __init ps3fb_init(void)
1205{
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001206 if (!ps3fb_videomemory.address || ps3fb_setup())
1207 return -ENXIO;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001208
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001209 return ps3_system_bus_driver_register(&ps3fb_driver);
1210}
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001211
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001212static void __exit ps3fb_exit(void)
1213{
1214 DPRINTK(" -> %s:%d\n", __func__, __LINE__);
1215 ps3_system_bus_driver_unregister(&ps3fb_driver);
1216 DPRINTK(" <- %s:%d\n", __func__, __LINE__);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001217}
1218
1219module_init(ps3fb_init);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001220module_exit(ps3fb_exit);
1221
1222MODULE_LICENSE("GPL");
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001223MODULE_DESCRIPTION("PS3 GPU Frame Buffer Driver");
1224MODULE_AUTHOR("Sony Computer Entertainment Inc.");
1225MODULE_ALIAS(PS3_MODULE_ALIAS_GRAPHICS);