2 * linux/drivers/video/ps3fb.c -- PS3 GPU frame buffer device
4 * Copyright (C) 2006 Sony Computer Entertainment Inc.
5 * Copyright 2006, 2007 Sony Corporation
7 * This file is based on :
9 * linux/drivers/video/vfb.c -- Virtual frame buffer device
11 * Copyright (C) 2002 James Simmons
13 * Copyright (C) 1997 Geert Uytterhoeven
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
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/string.h>
25 #include <linux/interrupt.h>
26 #include <linux/console.h>
27 #include <linux/ioctl.h>
28 #include <linux/kthread.h>
29 #include <linux/freezer.h>
30 #include <linux/uaccess.h>
32 #include <linux/init.h>
34 #include <asm/abs_addr.h>
35 #include <asm/lv1call.h>
36 #include <asm/ps3av.h>
37 #include <asm/ps3fb.h>
41 #define DEVICE_NAME "ps3fb"
43 #define L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC 0x101
44 #define L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP 0x102
45 #define L1GPU_CONTEXT_ATTRIBUTE_FB_SETUP 0x600
46 #define L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT 0x601
47 #define L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT_SYNC 0x602
49 #define L1GPU_FB_BLIT_WAIT_FOR_COMPLETION (1ULL << 32)
51 #define L1GPU_DISPLAY_SYNC_HSYNC 1
52 #define L1GPU_DISPLAY_SYNC_VSYNC 2
54 #define DDR_SIZE (0) /* used no ddr */
55 #define GPU_CMD_BUF_SIZE (64 * 1024)
56 #define GPU_IOIF (0x0d000000UL)
57 #define GPU_ALIGN_UP(x) _ALIGN_UP((x), 64)
59 #define PS3FB_FULL_MODE_BIT 0x80
61 #define GPU_INTR_STATUS_VSYNC_0 0 /* vsync on head A */
62 #define GPU_INTR_STATUS_VSYNC_1 1 /* vsync on head B */
63 #define GPU_INTR_STATUS_FLIP_0 3 /* flip head A */
64 #define GPU_INTR_STATUS_FLIP_1 4 /* flip head B */
65 #define GPU_INTR_STATUS_QUEUE_0 5 /* queue head A */
66 #define GPU_INTR_STATUS_QUEUE_1 6 /* queue head B */
68 #define GPU_DRIVER_INFO_VERSION 0x211
102 struct gpu_driver_info
{
106 u32 hardware_channel
;
108 u32 nvcore_frequency
;
109 u32 memory_frequency
;
112 struct display_head display_head
[8];
119 u64 context_handle
, memory_handle
;
122 struct gpu_driver_info
*dinfo
;
124 u64 vblank_count
; /* frame count */
125 wait_queue_head_t wait_vsync
;
127 atomic_t ext_flip
; /* on/off flip with vsync */
128 atomic_t f_count
; /* fb_open count */
131 struct task_struct
*task
;
133 static struct ps3fb_priv ps3fb
;
136 u32 pseudo_palette
[16];
137 int mode_id
, new_mode_id
;
139 unsigned int num_frames
; /* num of frame buffers */
142 unsigned long full_offset
; /* start of fullscreen DDR fb */
143 unsigned long fb_offset
; /* start of actual DDR fb */
144 unsigned long pan_offset
;
147 struct ps3fb_res_table
{
154 #define PS3FB_RES_FULL 1
155 static const struct ps3fb_res_table ps3fb_res
[] = {
156 /* res_x,y margin_x,y full */
157 { 720, 480, 72, 48 , 0},
158 { 720, 576, 72, 58 , 0},
159 { 1280, 720, 78, 38 , 0},
160 { 1920, 1080, 116, 58 , 0},
162 { 720, 480, 0, 0 , PS3FB_RES_FULL
},
163 { 720, 576, 0, 0 , PS3FB_RES_FULL
},
164 { 1280, 720, 0, 0 , PS3FB_RES_FULL
},
165 { 1920, 1080, 0, 0 , PS3FB_RES_FULL
},
166 /* vesa: normally full mode */
167 { 1280, 768, 0, 0 , 0},
168 { 1280, 1024, 0, 0 , 0},
169 { 1920, 1200, 0, 0 , 0},
172 /* default resolution */
173 #define GPU_RES_INDEX 0 /* 720 x 480 */
175 static const struct fb_videomode ps3fb_modedb
[] = {
176 /* 60 Hz broadcast modes (modes "1" to "5") */
179 "480i", 60, 576, 384, 74074, 130, 89, 78, 57, 63, 6,
180 FB_SYNC_BROADCAST
, FB_VMODE_INTERLACED
183 "480p", 60, 576, 384, 37037, 130, 89, 78, 57, 63, 6,
184 FB_SYNC_BROADCAST
, FB_VMODE_NONINTERLACED
187 "720p", 60, 1124, 644, 13481, 298, 148, 57, 44, 80, 5,
188 FB_SYNC_BROADCAST
, FB_VMODE_NONINTERLACED
191 "1080i", 60, 1688, 964, 13481, 264, 160, 94, 62, 88, 5,
192 FB_SYNC_BROADCAST
, FB_VMODE_INTERLACED
195 "1080p", 60, 1688, 964, 6741, 264, 160, 94, 62, 88, 5,
196 FB_SYNC_BROADCAST
, FB_VMODE_NONINTERLACED
199 /* 50 Hz broadcast modes (modes "6" to "10") */
202 "576i", 50, 576, 460, 74074, 142, 83, 97, 63, 63, 5,
203 FB_SYNC_BROADCAST
, FB_VMODE_INTERLACED
206 "576p", 50, 576, 460, 37037, 142, 83, 97, 63, 63, 5,
207 FB_SYNC_BROADCAST
, FB_VMODE_NONINTERLACED
210 "720p", 50, 1124, 644, 13468, 298, 478, 57, 44, 80, 5,
211 FB_SYNC_BROADCAST
, FB_VMODE_NONINTERLACED
214 "1080i", 50, 1688, 964, 13468, 264, 600, 94, 62, 88, 5,
215 FB_SYNC_BROADCAST
, FB_VMODE_INTERLACED
218 "1080p", 50, 1688, 964, 6734, 264, 600, 94, 62, 88, 5,
219 FB_SYNC_BROADCAST
, FB_VMODE_NONINTERLACED
222 /* VESA modes (modes "11" to "13") */
225 "wxga", 60, 1280, 768, 12924, 160, 24, 29, 3, 136, 6,
226 0, FB_VMODE_NONINTERLACED
,
230 "sxga", 60, 1280, 1024, 9259, 248, 48, 38, 1, 112, 3,
231 FB_SYNC_HOR_HIGH_ACT
| FB_SYNC_VERT_HIGH_ACT
, FB_VMODE_NONINTERLACED
,
235 "wuxga", 60, 1920, 1200, 6494, 80, 48, 26, 3, 32, 6,
236 FB_SYNC_HOR_HIGH_ACT
, FB_VMODE_NONINTERLACED
,
240 /* 60 Hz broadcast modes (full resolution versions of modes "1" to "5") */
243 "480if", 60, 720, 480, 74074, 58, 17, 30, 9, 63, 6,
244 FB_SYNC_BROADCAST
, FB_VMODE_INTERLACED
247 "480pf", 60, 720, 480, 37037, 58, 17, 30, 9, 63, 6,
248 FB_SYNC_BROADCAST
, FB_VMODE_NONINTERLACED
251 "720pf", 60, 1280, 720, 13481, 220, 70, 19, 6, 80, 5,
252 FB_SYNC_BROADCAST
, FB_VMODE_NONINTERLACED
255 "1080if", 60, 1920, 1080, 13481, 148, 44, 36, 4, 88, 5,
256 FB_SYNC_BROADCAST
, FB_VMODE_INTERLACED
259 "1080pf", 60, 1920, 1080, 6741, 148, 44, 36, 4, 88, 5,
260 FB_SYNC_BROADCAST
, FB_VMODE_NONINTERLACED
263 /* 50 Hz broadcast modes (full resolution versions of modes "6" to "10") */
266 "576if", 50, 720, 576, 74074, 70, 11, 39, 5, 63, 5,
267 FB_SYNC_BROADCAST
, FB_VMODE_INTERLACED
270 "576pf", 50, 720, 576, 37037, 70, 11, 39, 5, 63, 5,
271 FB_SYNC_BROADCAST
, FB_VMODE_NONINTERLACED
274 "720pf", 50, 1280, 720, 13468, 220, 400, 19, 6, 80, 5,
275 FB_SYNC_BROADCAST
, FB_VMODE_NONINTERLACED
278 "1080f", 50, 1920, 1080, 13468, 148, 484, 36, 4, 88, 5,
279 FB_SYNC_BROADCAST
, FB_VMODE_INTERLACED
282 "1080pf", 50, 1920, 1080, 6734, 148, 484, 36, 4, 88, 5,
283 FB_SYNC_BROADCAST
, FB_VMODE_NONINTERLACED
291 #define X_OFF(i) (ps3fb_res[i].xoff) /* left/right margin (pixel) */
292 #define Y_OFF(i) (ps3fb_res[i].yoff) /* top/bottom margin (pixel) */
293 #define WIDTH(i) (ps3fb_res[i].xres) /* width of FB */
294 #define HEIGHT(i) (ps3fb_res[i].yres) /* height of FB */
295 #define BPP 4 /* number of bytes per pixel */
297 /* Start of the virtual frame buffer (relative to fullscreen ) */
298 #define VP_OFF(i) ((WIDTH(i) * Y_OFF(i) + X_OFF(i)) * BPP)
301 static int ps3fb_mode
;
302 module_param(ps3fb_mode
, int, 0);
304 static char *mode_option __devinitdata
;
306 static int ps3fb_get_res_table(u32 xres
, u32 yres
, int mode
)
312 full_mode
= (mode
& PS3FB_FULL_MODE_BIT
) ? PS3FB_RES_FULL
: 0;
314 x
= ps3fb_res
[i
].xres
;
315 y
= ps3fb_res
[i
].yres
;
316 f
= ps3fb_res
[i
].type
;
319 pr_debug("ERROR: ps3fb_get_res_table()\n");
323 if (full_mode
== PS3FB_RES_FULL
&& f
!= PS3FB_RES_FULL
)
326 if (x
== xres
&& (yres
== 0 || y
== yres
))
329 x
= x
- 2 * ps3fb_res
[i
].xoff
;
330 y
= y
- 2 * ps3fb_res
[i
].yoff
;
331 if (x
== xres
&& (yres
== 0 || y
== yres
))
337 static unsigned int ps3fb_find_mode(const struct fb_var_screeninfo
*var
,
340 unsigned int i
, mode
;
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 */
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;
360 pr_debug("ps3fb_find_mode: mode %u\n", mode
);
364 pr_debug("ps3fb_find_mode: mode not found\n");
368 static const struct fb_videomode
*ps3fb_default_mode(int id
)
370 u32 mode
= id
& PS3AV_MODE_MASK
;
373 if (mode
< 1 || mode
> 13)
376 flags
= id
& ~PS3AV_MODE_MASK
;
378 if (mode
<= 10 && flags
& PS3FB_FULL_MODE_BIT
) {
379 /* Full broadcast mode */
380 return &ps3fb_modedb
[mode
+ 12];
383 return &ps3fb_modedb
[mode
- 1];
386 static void ps3fb_sync_image(struct device
*dev
, u64 frame_offset
,
387 u64 dst_offset
, u64 src_offset
, u32 width
,
388 u32 height
, u64 line_length
)
392 status
= lv1_gpu_context_attribute(ps3fb
.context_handle
,
393 L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT
,
394 dst_offset
, GPU_IOIF
+ src_offset
,
395 L1GPU_FB_BLIT_WAIT_FOR_COMPLETION
|
396 (width
<< 16) | height
,
400 "%s: lv1_gpu_context_attribute FB_BLIT failed: %d\n",
403 status
= lv1_gpu_context_attribute(ps3fb
.context_handle
,
404 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP
,
405 0, frame_offset
, 0, 0);
407 dev_err(dev
, "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
411 status
= lv1_gpu_context_attribute(ps3fb
.context_handle
,
412 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP
,
413 1, frame_offset
, 0, 0);
415 dev_err(dev
, "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
420 static int ps3fb_sync(struct fb_info
*info
, u32 frame
)
422 struct ps3fb_par
*par
= info
->par
;
425 u64 line_length
, base
;
427 acquire_console_sem();
429 if (frame
> par
->num_frames
- 1) {
430 dev_dbg(info
->device
, "%s: invalid frame number (%u)\n",
437 xres
= ps3fb_res
[i
].xres
;
438 yres
= ps3fb_res
[i
].yres
;
440 line_length
= xres
* BPP
;
441 base
= frame
* yres
* line_length
;
443 ps3fb_sync_image(info
->device
, base
+ par
->full_offset
,
444 base
+ par
->fb_offset
, base
+ par
->pan_offset
,
445 par
->width
, par
->height
, line_length
);
448 release_console_sem();
452 static int ps3fb_open(struct fb_info
*info
, int user
)
454 atomic_inc(&ps3fb
.f_count
);
458 static int ps3fb_release(struct fb_info
*info
, int user
)
460 if (atomic_dec_and_test(&ps3fb
.f_count
)) {
461 if (atomic_read(&ps3fb
.ext_flip
)) {
462 atomic_set(&ps3fb
.ext_flip
, 0);
463 ps3fb_sync(info
, 0); /* single buffer */
470 * Setting the video mode has been split into two parts.
471 * First part, xxxfb_check_var, must not write anything
472 * to hardware, it should only verify and adjust var.
473 * This means it doesn't alter par but it does use hardware
474 * data from it to check this var.
477 static int ps3fb_check_var(struct fb_var_screeninfo
*var
, struct fb_info
*info
)
482 dev_dbg(info
->device
, "var->xres:%u info->var.xres:%u\n", var
->xres
,
484 dev_dbg(info
->device
, "var->yres:%u info->var.yres:%u\n", var
->yres
,
487 /* FIXME For now we do exact matches only */
488 mode
= ps3fb_find_mode(var
, &line_length
);
493 if (var
->xres_virtual
< var
->xres
)
494 var
->xres_virtual
= var
->xres
;
495 if (var
->yres_virtual
< var
->yres
)
496 var
->yres_virtual
= var
->yres
;
498 if (var
->xres_virtual
> line_length
/ BPP
) {
499 dev_dbg(info
->device
,
500 "Horizontal virtual screen size too large\n");
504 if (var
->xoffset
+ var
->xres
> var
->xres_virtual
||
505 var
->yoffset
+ var
->yres
> var
->yres_virtual
) {
506 dev_dbg(info
->device
, "panning out-of-range\n");
510 /* We support ARGB8888 only */
511 if (var
->bits_per_pixel
> 32 || var
->grayscale
||
512 var
->red
.offset
> 16 || var
->green
.offset
> 8 ||
513 var
->blue
.offset
> 0 || var
->transp
.offset
> 24 ||
514 var
->red
.length
> 8 || var
->green
.length
> 8 ||
515 var
->blue
.length
> 8 || var
->transp
.length
> 8 ||
516 var
->red
.msb_right
|| var
->green
.msb_right
||
517 var
->blue
.msb_right
|| var
->transp
.msb_right
|| var
->nonstd
) {
518 dev_dbg(info
->device
, "We support ARGB8888 only\n");
522 var
->bits_per_pixel
= 32;
523 var
->red
.offset
= 16;
524 var
->green
.offset
= 8;
525 var
->blue
.offset
= 0;
526 var
->transp
.offset
= 24;
528 var
->green
.length
= 8;
529 var
->blue
.length
= 8;
530 var
->transp
.length
= 8;
531 var
->red
.msb_right
= 0;
532 var
->green
.msb_right
= 0;
533 var
->blue
.msb_right
= 0;
534 var
->transp
.msb_right
= 0;
536 /* Rotation is not supported */
538 dev_dbg(info
->device
, "Rotation is not supported\n");
543 if (var
->yres_virtual
* line_length
> ps3fb
.xdr_size
) {
544 dev_dbg(info
->device
, "Not enough memory\n");
555 * This routine actually sets the video mode.
558 static int ps3fb_set_par(struct fb_info
*info
)
560 struct ps3fb_par
*par
= info
->par
;
561 unsigned int mode
, line_length
, lines
, maxlines
;
563 unsigned long offset
, dst
;
565 dev_dbg(info
->device
, "xres:%d xv:%d yres:%d yv:%d clock:%d\n",
566 info
->var
.xres
, info
->var
.xres_virtual
,
567 info
->var
.yres
, info
->var
.yres_virtual
, info
->var
.pixclock
);
569 mode
= ps3fb_find_mode(&info
->var
, &line_length
);
573 i
= ps3fb_get_res_table(info
->var
.xres
, info
->var
.yres
, mode
);
576 info
->fix
.smem_start
= virt_to_abs(ps3fb
.xdr_ea
);
577 info
->fix
.smem_len
= ps3fb
.xdr_size
;
578 info
->fix
.xpanstep
= info
->var
.xres_virtual
> info
->var
.xres
? 1 : 0;
579 info
->fix
.ypanstep
= info
->var
.yres_virtual
> info
->var
.yres
? 1 : 0;
580 info
->fix
.line_length
= line_length
;
582 info
->screen_base
= (char __iomem
*)ps3fb
.xdr_ea
;
584 par
->num_frames
= info
->fix
.smem_len
/
585 (ps3fb_res
[i
].xres
*ps3fb_res
[i
].yres
*BPP
);
587 /* Keep the special bits we cannot set using fb_var_screeninfo */
588 par
->new_mode_id
= (par
->new_mode_id
& ~PS3AV_MODE_MASK
) | mode
;
590 par
->width
= info
->var
.xres
;
591 par
->height
= info
->var
.yres
;
593 par
->fb_offset
= GPU_ALIGN_UP(offset
);
594 par
->full_offset
= par
->fb_offset
- offset
;
595 par
->pan_offset
= info
->var
.yoffset
* line_length
+
596 info
->var
.xoffset
* BPP
;
598 if (par
->new_mode_id
!= par
->mode_id
) {
599 if (ps3av_set_video_mode(par
->new_mode_id
)) {
600 par
->new_mode_id
= par
->mode_id
;
603 par
->mode_id
= par
->new_mode_id
;
606 /* Clear XDR frame buffer memory */
607 memset(ps3fb
.xdr_ea
, 0, ps3fb
.xdr_size
);
609 /* Clear DDR frame buffer memory */
610 lines
= ps3fb_res
[i
].yres
* par
->num_frames
;
611 if (par
->full_offset
)
613 maxlines
= ps3fb
.xdr_size
/ line_length
;
614 for (dst
= 0; lines
; dst
+= maxlines
* line_length
) {
615 unsigned int l
= min(lines
, maxlines
);
616 ps3fb_sync_image(info
->device
, 0, dst
, 0, ps3fb_res
[i
].xres
, l
,
625 * Set a single color register. The values supplied are already
626 * rounded down to the hardware's capabilities (according to the
627 * entries in the var structure). Return != 0 for invalid regno.
630 static int ps3fb_setcolreg(unsigned int regno
, unsigned int red
,
631 unsigned int green
, unsigned int blue
,
632 unsigned int transp
, struct fb_info
*info
)
642 ((u32
*)info
->pseudo_palette
)[regno
] = transp
<< 24 | red
<< 16 |
647 static int ps3fb_pan_display(struct fb_var_screeninfo
*var
,
648 struct fb_info
*info
)
650 struct ps3fb_par
*par
= info
->par
;
652 par
->pan_offset
= var
->yoffset
* info
->fix
.line_length
+
658 * As we have a virtual frame buffer, we need our own mmap function
661 static int ps3fb_mmap(struct fb_info
*info
, struct vm_area_struct
*vma
)
663 unsigned long size
, offset
;
665 size
= vma
->vm_end
- vma
->vm_start
;
666 offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
667 if (offset
+ size
> info
->fix
.smem_len
)
670 offset
+= info
->fix
.smem_start
;
671 if (remap_pfn_range(vma
, vma
->vm_start
, offset
>> PAGE_SHIFT
,
672 size
, vma
->vm_page_prot
))
675 dev_dbg(info
->device
, "ps3fb: mmap framebuffer P(%lx)->V(%lx)\n",
676 offset
, vma
->vm_start
);
684 static int ps3fb_blank(int blank
, struct fb_info
*info
)
688 dev_dbg(info
->device
, "%s: blank:%d\n", __func__
, blank
);
690 case FB_BLANK_POWERDOWN
:
691 case FB_BLANK_HSYNC_SUSPEND
:
692 case FB_BLANK_VSYNC_SUSPEND
:
693 case FB_BLANK_NORMAL
:
694 retval
= ps3av_video_mute(1); /* mute on */
696 ps3fb
.is_blanked
= 1;
699 default: /* unblank */
700 retval
= ps3av_video_mute(0); /* mute off */
702 ps3fb
.is_blanked
= 0;
708 static int ps3fb_get_vblank(struct fb_vblank
*vblank
)
710 memset(vblank
, 0, sizeof(&vblank
));
711 vblank
->flags
= FB_VBLANK_HAVE_VSYNC
;
715 static int ps3fb_wait_for_vsync(u32 crtc
)
720 count
= ps3fb
.vblank_count
;
721 ret
= wait_event_interruptible_timeout(ps3fb
.wait_vsync
,
722 count
!= ps3fb
.vblank_count
,
730 static void ps3fb_flip_ctl(int on
, void *data
)
732 struct ps3fb_priv
*priv
= data
;
734 atomic_dec_if_positive(&priv
->ext_flip
);
736 atomic_inc(&priv
->ext_flip
);
744 static int ps3fb_ioctl(struct fb_info
*info
, unsigned int cmd
,
747 void __user
*argp
= (void __user
*)arg
;
749 int retval
= -EFAULT
;
754 struct fb_vblank vblank
;
755 dev_dbg(info
->device
, "FBIOGET_VBLANK:\n");
756 retval
= ps3fb_get_vblank(&vblank
);
760 if (copy_to_user(argp
, &vblank
, sizeof(vblank
)))
765 case FBIO_WAITFORVSYNC
:
768 dev_dbg(info
->device
, "FBIO_WAITFORVSYNC:\n");
769 if (get_user(crt
, (u32 __user
*) arg
))
772 retval
= ps3fb_wait_for_vsync(crt
);
776 case PS3FB_IOCTL_SETMODE
:
778 struct ps3fb_par
*par
= info
->par
;
779 const struct fb_videomode
*mode
;
780 struct fb_var_screeninfo var
;
782 if (copy_from_user(&val
, argp
, sizeof(val
)))
785 if (!(val
& PS3AV_MODE_MASK
)) {
786 u32 id
= ps3av_get_auto_mode();
788 val
= (val
& ~PS3AV_MODE_MASK
) | id
;
790 dev_dbg(info
->device
, "PS3FB_IOCTL_SETMODE:%x\n", val
);
792 mode
= ps3fb_default_mode(val
);
795 fb_videomode_to_var(&var
, mode
);
796 acquire_console_sem();
797 info
->flags
|= FBINFO_MISC_USEREVENT
;
798 /* Force, in case only special bits changed */
799 var
.activate
|= FB_ACTIVATE_FORCE
;
800 par
->new_mode_id
= val
;
801 retval
= fb_set_var(info
, &var
);
802 info
->flags
&= ~FBINFO_MISC_USEREVENT
;
803 release_console_sem();
808 case PS3FB_IOCTL_GETMODE
:
809 val
= ps3av_get_mode();
810 dev_dbg(info
->device
, "PS3FB_IOCTL_GETMODE:%x\n", val
);
811 if (!copy_to_user(argp
, &val
, sizeof(val
)))
815 case PS3FB_IOCTL_SCREENINFO
:
817 struct ps3fb_par
*par
= info
->par
;
818 struct ps3fb_ioctl_res res
;
819 int i
= par
->res_index
;
820 dev_dbg(info
->device
, "PS3FB_IOCTL_SCREENINFO:\n");
821 res
.xres
= ps3fb_res
[i
].xres
;
822 res
.yres
= ps3fb_res
[i
].yres
;
823 res
.xoff
= ps3fb_res
[i
].xoff
;
824 res
.yoff
= ps3fb_res
[i
].yoff
;
825 res
.num_frames
= par
->num_frames
;
826 if (!copy_to_user(argp
, &res
, sizeof(res
)))
832 dev_dbg(info
->device
, "PS3FB_IOCTL_ON:\n");
833 atomic_inc(&ps3fb
.ext_flip
);
837 case PS3FB_IOCTL_OFF
:
838 dev_dbg(info
->device
, "PS3FB_IOCTL_OFF:\n");
839 atomic_dec_if_positive(&ps3fb
.ext_flip
);
843 case PS3FB_IOCTL_FSEL
:
844 if (copy_from_user(&val
, argp
, sizeof(val
)))
847 dev_dbg(info
->device
, "PS3FB_IOCTL_FSEL:%d\n", val
);
848 retval
= ps3fb_sync(info
, val
);
852 retval
= -ENOIOCTLCMD
;
858 static int ps3fbd(void *arg
)
860 struct fb_info
*info
= arg
;
863 while (!kthread_should_stop()) {
865 set_current_state(TASK_INTERRUPTIBLE
);
866 if (ps3fb
.is_kicked
) {
868 ps3fb_sync(info
, 0); /* single buffer */
875 static irqreturn_t
ps3fb_vsync_interrupt(int irq
, void *ptr
)
877 struct device
*dev
= ptr
;
880 struct display_head
*head
= &ps3fb
.dinfo
->display_head
[1];
882 status
= lv1_gpu_context_intr(ps3fb
.context_handle
, &v1
);
884 dev_err(dev
, "%s: lv1_gpu_context_intr failed: %d\n", __func__
,
889 if (v1
& (1 << GPU_INTR_STATUS_VSYNC_1
)) {
891 ps3fb
.vblank_count
= head
->vblank_count
;
892 if (ps3fb
.task
&& !ps3fb
.is_blanked
&&
893 !atomic_read(&ps3fb
.ext_flip
)) {
895 wake_up_process(ps3fb
.task
);
897 wake_up_interruptible(&ps3fb
.wait_vsync
);
904 static int ps3fb_vsync_settings(struct gpu_driver_info
*dinfo
,
909 dev_dbg(dev
, "version_driver:%x\n", dinfo
->version_driver
);
910 dev_dbg(dev
, "irq outlet:%x\n", dinfo
->irq
.irq_outlet
);
912 "version_gpu: %x memory_size: %x ch: %x core_freq: %d "
914 dinfo
->version_gpu
, dinfo
->memory_size
, dinfo
->hardware_channel
,
915 dinfo
->nvcore_frequency
/1000000, dinfo
->memory_frequency
/1000000);
917 if (dinfo
->version_driver
!= GPU_DRIVER_INFO_VERSION
) {
918 dev_err(dev
, "%s: version_driver err:%x\n", __func__
,
919 dinfo
->version_driver
);
923 error
= ps3_irq_plug_setup(PS3_BINDING_CPU_ANY
, dinfo
->irq
.irq_outlet
,
926 dev_err(dev
, "%s: ps3_alloc_irq failed %d\n", __func__
, error
);
930 error
= request_irq(ps3fb
.irq_no
, ps3fb_vsync_interrupt
, IRQF_DISABLED
,
933 dev_err(dev
, "%s: request_irq failed %d\n", __func__
, error
);
934 ps3_irq_plug_destroy(ps3fb
.irq_no
);
938 dinfo
->irq
.mask
= (1 << GPU_INTR_STATUS_VSYNC_1
) |
939 (1 << GPU_INTR_STATUS_FLIP_1
);
943 static int ps3fb_xdr_settings(u64 xdr_lpar
, struct device
*dev
)
947 status
= lv1_gpu_context_iomap(ps3fb
.context_handle
, GPU_IOIF
,
948 xdr_lpar
, ps3fb_videomemory
.size
, 0);
950 dev_err(dev
, "%s: lv1_gpu_context_iomap failed: %d\n",
955 "video:%p xdr_ea:%p ioif:%lx lpar:%lx phys:%lx size:%lx\n",
956 ps3fb_videomemory
.address
, ps3fb
.xdr_ea
, GPU_IOIF
, xdr_lpar
,
957 virt_to_abs(ps3fb
.xdr_ea
), ps3fb_videomemory
.size
);
959 status
= lv1_gpu_context_attribute(ps3fb
.context_handle
,
960 L1GPU_CONTEXT_ATTRIBUTE_FB_SETUP
,
961 xdr_lpar
+ ps3fb
.xdr_size
,
963 GPU_IOIF
+ ps3fb
.xdr_size
, 0);
966 "%s: lv1_gpu_context_attribute FB_SETUP failed: %d\n",
973 static struct fb_ops ps3fb_ops
= {
974 .fb_open
= ps3fb_open
,
975 .fb_release
= ps3fb_release
,
976 .fb_read
= fb_sys_read
,
977 .fb_write
= fb_sys_write
,
978 .fb_check_var
= ps3fb_check_var
,
979 .fb_set_par
= ps3fb_set_par
,
980 .fb_setcolreg
= ps3fb_setcolreg
,
981 .fb_pan_display
= ps3fb_pan_display
,
982 .fb_fillrect
= sys_fillrect
,
983 .fb_copyarea
= sys_copyarea
,
984 .fb_imageblit
= sys_imageblit
,
985 .fb_mmap
= ps3fb_mmap
,
986 .fb_blank
= ps3fb_blank
,
987 .fb_ioctl
= ps3fb_ioctl
,
988 .fb_compat_ioctl
= ps3fb_ioctl
991 static struct fb_fix_screeninfo ps3fb_fix __initdata
= {
993 .type
= FB_TYPE_PACKED_PIXELS
,
994 .visual
= FB_VISUAL_TRUECOLOR
,
995 .accel
= FB_ACCEL_NONE
,
998 static int ps3fb_set_sync(struct device
*dev
)
1003 status
= lv1_gpu_context_attribute(0x0,
1004 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC
,
1005 0, L1GPU_DISPLAY_SYNC_VSYNC
, 0, 0);
1008 "%s: lv1_gpu_context_attribute DISPLAY_SYNC failed: "
1015 status
= lv1_gpu_context_attribute(0x0,
1016 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC
,
1017 1, L1GPU_DISPLAY_SYNC_VSYNC
, 0, 0);
1021 "%s: lv1_gpu_context_attribute DISPLAY_SYNC failed: "
1030 static int __devinit
ps3fb_probe(struct ps3_system_bus_device
*dev
)
1032 struct fb_info
*info
;
1033 struct ps3fb_par
*par
;
1034 int retval
= -ENOMEM
;
1037 u64 lpar_dma_control
= 0;
1038 u64 lpar_driver_info
= 0;
1039 u64 lpar_reports
= 0;
1040 u64 lpar_reports_size
= 0;
1042 int status
, res_index
;
1043 struct task_struct
*task
;
1045 status
= ps3_open_hv_device(dev
);
1047 dev_err(&dev
->core
, "%s: ps3_open_hv_device failed\n",
1053 ps3fb_mode
= ps3av_get_mode();
1054 dev_dbg(&dev
->core
, "ps3av_mode:%d\n", ps3fb_mode
);
1056 if (ps3fb_mode
> 0 &&
1057 !ps3av_video_mode2res(ps3fb_mode
, &xres
, &yres
)) {
1058 res_index
= ps3fb_get_res_table(xres
, yres
, ps3fb_mode
);
1059 dev_dbg(&dev
->core
, "res_index:%d\n", res_index
);
1061 res_index
= GPU_RES_INDEX
;
1063 atomic_set(&ps3fb
.f_count
, -1); /* fbcon opens ps3fb */
1064 atomic_set(&ps3fb
.ext_flip
, 0); /* for flip with vsync */
1065 init_waitqueue_head(&ps3fb
.wait_vsync
);
1067 ps3fb_set_sync(&dev
->core
);
1069 /* get gpu context handle */
1070 status
= lv1_gpu_memory_allocate(DDR_SIZE
, 0, 0, 0, 0,
1071 &ps3fb
.memory_handle
, &ddr_lpar
);
1073 dev_err(&dev
->core
, "%s: lv1_gpu_memory_allocate failed: %d\n",
1077 dev_dbg(&dev
->core
, "ddr:lpar:0x%lx\n", ddr_lpar
);
1079 status
= lv1_gpu_context_allocate(ps3fb
.memory_handle
, 0,
1080 &ps3fb
.context_handle
,
1081 &lpar_dma_control
, &lpar_driver_info
,
1082 &lpar_reports
, &lpar_reports_size
);
1085 "%s: lv1_gpu_context_attribute failed: %d\n", __func__
,
1087 goto err_gpu_memory_free
;
1090 /* vsync interrupt */
1091 ps3fb
.dinfo
= ioremap(lpar_driver_info
, 128 * 1024);
1093 dev_err(&dev
->core
, "%s: ioremap failed\n", __func__
);
1094 goto err_gpu_context_free
;
1097 retval
= ps3fb_vsync_settings(ps3fb
.dinfo
, &dev
->core
);
1099 goto err_iounmap_dinfo
;
1101 /* XDR frame buffer */
1102 ps3fb
.xdr_ea
= ps3fb_videomemory
.address
;
1103 xdr_lpar
= ps3_mm_phys_to_lpar(__pa(ps3fb
.xdr_ea
));
1105 /* Clear memory to prevent kernel info leakage into userspace */
1106 memset(ps3fb
.xdr_ea
, 0, ps3fb_videomemory
.size
);
1108 /* The GPU command buffer is at the end of video memory */
1109 ps3fb
.xdr_size
= ps3fb_videomemory
.size
- GPU_CMD_BUF_SIZE
;
1111 retval
= ps3fb_xdr_settings(xdr_lpar
, &dev
->core
);
1115 info
= framebuffer_alloc(sizeof(struct ps3fb_par
), &dev
->core
);
1120 par
->mode_id
= ~ps3fb_mode
; /* != ps3fb_mode, to trigger change */
1121 par
->new_mode_id
= ps3fb_mode
;
1122 par
->res_index
= res_index
;
1123 par
->num_frames
= 1;
1125 info
->screen_base
= (char __iomem
*)ps3fb
.xdr_ea
;
1126 info
->fbops
= &ps3fb_ops
;
1128 info
->fix
= ps3fb_fix
;
1129 info
->fix
.smem_start
= virt_to_abs(ps3fb
.xdr_ea
);
1130 info
->fix
.smem_len
= ps3fb
.xdr_size
;
1131 info
->pseudo_palette
= par
->pseudo_palette
;
1132 info
->flags
= FBINFO_DEFAULT
| FBINFO_READS_FAST
|
1133 FBINFO_HWACCEL_XPAN
| FBINFO_HWACCEL_YPAN
;
1135 retval
= fb_alloc_cmap(&info
->cmap
, 256, 0);
1137 goto err_framebuffer_release
;
1139 if (!fb_find_mode(&info
->var
, info
, mode_option
, ps3fb_modedb
,
1140 ARRAY_SIZE(ps3fb_modedb
),
1141 ps3fb_default_mode(par
->new_mode_id
), 32)) {
1143 goto err_fb_dealloc
;
1146 fb_videomode_to_modelist(ps3fb_modedb
, ARRAY_SIZE(ps3fb_modedb
),
1149 retval
= register_framebuffer(info
);
1151 goto err_fb_dealloc
;
1153 dev
->core
.driver_data
= info
;
1155 dev_info(info
->device
, "%s %s, using %lu KiB of video memory\n",
1156 dev_driver_string(info
->dev
), info
->dev
->bus_id
,
1157 ps3fb
.xdr_size
>> 10);
1159 task
= kthread_run(ps3fbd
, info
, DEVICE_NAME
);
1161 retval
= PTR_ERR(task
);
1162 goto err_unregister_framebuffer
;
1166 ps3av_register_flip_ctl(ps3fb_flip_ctl
, &ps3fb
);
1170 err_unregister_framebuffer
:
1171 unregister_framebuffer(info
);
1173 fb_dealloc_cmap(&info
->cmap
);
1174 err_framebuffer_release
:
1175 framebuffer_release(info
);
1177 free_irq(ps3fb
.irq_no
, dev
);
1178 ps3_irq_plug_destroy(ps3fb
.irq_no
);
1180 iounmap((u8 __iomem
*)ps3fb
.dinfo
);
1181 err_gpu_context_free
:
1182 lv1_gpu_context_free(ps3fb
.context_handle
);
1183 err_gpu_memory_free
:
1184 lv1_gpu_memory_free(ps3fb
.memory_handle
);
1189 static int ps3fb_shutdown(struct ps3_system_bus_device
*dev
)
1192 struct fb_info
*info
= dev
->core
.driver_data
;
1194 dev_dbg(&dev
->core
, " -> %s:%d\n", __func__
, __LINE__
);
1196 ps3fb_flip_ctl(0, &ps3fb
); /* flip off */
1197 ps3fb
.dinfo
->irq
.mask
= 0;
1200 unregister_framebuffer(info
);
1201 fb_dealloc_cmap(&info
->cmap
);
1202 framebuffer_release(info
);
1205 ps3av_register_flip_ctl(NULL
, NULL
);
1207 struct task_struct
*task
= ps3fb
.task
;
1212 free_irq(ps3fb
.irq_no
, dev
);
1213 ps3_irq_plug_destroy(ps3fb
.irq_no
);
1215 iounmap((u8 __iomem
*)ps3fb
.dinfo
);
1217 status
= lv1_gpu_context_free(ps3fb
.context_handle
);
1219 dev_dbg(&dev
->core
, "lv1_gpu_context_free failed: %d\n",
1222 status
= lv1_gpu_memory_free(ps3fb
.memory_handle
);
1224 dev_dbg(&dev
->core
, "lv1_gpu_memory_free failed: %d\n",
1227 ps3_close_hv_device(dev
);
1228 dev_dbg(&dev
->core
, " <- %s:%d\n", __func__
, __LINE__
);
1233 static struct ps3_system_bus_driver ps3fb_driver
= {
1234 .match_id
= PS3_MATCH_ID_GRAPHICS
,
1235 .core
.name
= DEVICE_NAME
,
1236 .core
.owner
= THIS_MODULE
,
1237 .probe
= ps3fb_probe
,
1238 .remove
= ps3fb_shutdown
,
1239 .shutdown
= ps3fb_shutdown
,
1242 static int __init
ps3fb_setup(void)
1250 if (fb_get_options(DEVICE_NAME
, &options
))
1253 if (!options
|| !*options
)
1257 char *this_opt
= strsep(&options
, ",");
1263 if (!strncmp(this_opt
, "mode:", 5))
1264 ps3fb_mode
= simple_strtoul(this_opt
+ 5, NULL
, 0);
1266 mode_option
= this_opt
;
1271 static int __init
ps3fb_init(void)
1273 if (!ps3fb_videomemory
.address
|| ps3fb_setup())
1276 return ps3_system_bus_driver_register(&ps3fb_driver
);
1279 static void __exit
ps3fb_exit(void)
1281 pr_debug(" -> %s:%d\n", __func__
, __LINE__
);
1282 ps3_system_bus_driver_unregister(&ps3fb_driver
);
1283 pr_debug(" <- %s:%d\n", __func__
, __LINE__
);
1286 module_init(ps3fb_init
);
1287 module_exit(ps3fb_exit
);
1289 MODULE_LICENSE("GPL");
1290 MODULE_DESCRIPTION("PS3 GPU Frame Buffer Driver");
1291 MODULE_AUTHOR("Sony Computer Entertainment Inc.");
1292 MODULE_ALIAS(PS3_MODULE_ALIAS_GRAPHICS
);