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 */
146 struct ps3fb_res_table {
153 #define PS3FB_RES_FULL 1
154 static const struct ps3fb_res_table ps3fb_res[] = {
155 /* res_x,y margin_x,y full */
156 { 720, 480, 72, 48 , 0},
157 { 720, 576, 72, 58 , 0},
158 { 1280, 720, 78, 38 , 0},
159 { 1920, 1080, 116, 58 , 0},
161 { 720, 480, 0, 0 , PS3FB_RES_FULL},
162 { 720, 576, 0, 0 , PS3FB_RES_FULL},
163 { 1280, 720, 0, 0 , PS3FB_RES_FULL},
164 { 1920, 1080, 0, 0 , PS3FB_RES_FULL},
165 /* vesa: normally full mode */
166 { 1280, 768, 0, 0 , 0},
167 { 1280, 1024, 0, 0 , 0},
168 { 1920, 1200, 0, 0 , 0},
171 /* default resolution */
172 #define GPU_RES_INDEX 0 /* 720 x 480 */
174 static const struct fb_videomode ps3fb_modedb[] = {
175 /* 60 Hz broadcast modes (modes "1" to "5") */
178 "480i", 60, 576, 384, 74074, 130, 89, 78, 57, 63, 6,
179 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
182 "480p", 60, 576, 384, 37037, 130, 89, 78, 57, 63, 6,
183 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
186 "720p", 60, 1124, 644, 13481, 298, 148, 57, 44, 80, 5,
187 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
190 "1080i", 60, 1688, 964, 13481, 264, 160, 94, 62, 88, 5,
191 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
194 "1080p", 60, 1688, 964, 6741, 264, 160, 94, 62, 88, 5,
195 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
198 /* 50 Hz broadcast modes (modes "6" to "10") */
201 "576i", 50, 576, 460, 74074, 142, 83, 97, 63, 63, 5,
202 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
205 "576p", 50, 576, 460, 37037, 142, 83, 97, 63, 63, 5,
206 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
209 "720p", 50, 1124, 644, 13468, 298, 478, 57, 44, 80, 5,
210 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
213 "1080i", 50, 1688, 964, 13468, 264, 600, 94, 62, 88, 5,
214 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
217 "1080p", 50, 1688, 964, 6734, 264, 600, 94, 62, 88, 5,
218 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
221 /* VESA modes (modes "11" to "13") */
224 "wxga", 60, 1280, 768, 12924, 160, 24, 29, 3, 136, 6,
225 0, FB_VMODE_NONINTERLACED,
229 "sxga", 60, 1280, 1024, 9259, 248, 48, 38, 1, 112, 3,
230 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED,
234 "wuxga", 60, 1920, 1200, 6494, 80, 48, 26, 3, 32, 6,
235 FB_SYNC_HOR_HIGH_ACT, FB_VMODE_NONINTERLACED,
239 /* 60 Hz broadcast modes (full resolution versions of modes "1" to "5") */
242 "480if", 60, 720, 480, 74074, 58, 17, 30, 9, 63, 6,
243 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
246 "480pf", 60, 720, 480, 37037, 58, 17, 30, 9, 63, 6,
247 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
250 "720pf", 60, 1280, 720, 13481, 220, 70, 19, 6, 80, 5,
251 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
254 "1080if", 60, 1920, 1080, 13481, 148, 44, 36, 4, 88, 5,
255 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
258 "1080pf", 60, 1920, 1080, 6741, 148, 44, 36, 4, 88, 5,
259 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
262 /* 50 Hz broadcast modes (full resolution versions of modes "6" to "10") */
265 "576if", 50, 720, 576, 74074, 70, 11, 39, 5, 63, 5,
266 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
269 "576pf", 50, 720, 576, 37037, 70, 11, 39, 5, 63, 5,
270 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
273 "720pf", 50, 1280, 720, 13468, 220, 400, 19, 6, 80, 5,
274 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
277 "1080f", 50, 1920, 1080, 13468, 148, 484, 36, 4, 88, 5,
278 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
281 "1080pf", 50, 1920, 1080, 6734, 148, 484, 36, 4, 88, 5,
282 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
290 #define X_OFF(i) (ps3fb_res[i].xoff) /* left/right margin (pixel) */
291 #define Y_OFF(i) (ps3fb_res[i].yoff) /* top/bottom margin (pixel) */
292 #define WIDTH(i) (ps3fb_res[i].xres) /* width of FB */
293 #define HEIGHT(i) (ps3fb_res[i].yres) /* height of FB */
294 #define BPP 4 /* number of bytes per pixel */
296 /* Start of the virtual frame buffer (relative to fullscreen ) */
297 #define VP_OFF(i) ((WIDTH(i) * Y_OFF(i) + X_OFF(i)) * BPP)
300 static int ps3fb_mode;
301 module_param(ps3fb_mode, int, 0);
303 static char *mode_option __devinitdata;
305 static int ps3fb_get_res_table(u32 xres, u32 yres, int mode)
311 full_mode = (mode & PS3FB_FULL_MODE_BIT) ? PS3FB_RES_FULL : 0;
313 x = ps3fb_res[i].xres;
314 y = ps3fb_res[i].yres;
315 f = ps3fb_res[i].type;
318 pr_debug("ERROR: ps3fb_get_res_table()\n");
322 if (full_mode == PS3FB_RES_FULL && f != PS3FB_RES_FULL)
325 if (x == xres && (yres == 0 || y == yres))
328 x = x - 2 * ps3fb_res[i].xoff;
329 y = y - 2 * ps3fb_res[i].yoff;
330 if (x == xres && (yres == 0 || y == yres))
336 static unsigned int ps3fb_find_mode(const struct fb_var_screeninfo *var,
339 unsigned int i, mode;
341 for (i = 0; i < ARRAY_SIZE(ps3fb_modedb); i++)
342 if (var->xres == ps3fb_modedb[i].xres &&
343 var->yres == ps3fb_modedb[i].yres &&
344 var->pixclock == ps3fb_modedb[i].pixclock &&
345 var->hsync_len == ps3fb_modedb[i].hsync_len &&
346 var->vsync_len == ps3fb_modedb[i].vsync_len &&
347 var->left_margin == ps3fb_modedb[i].left_margin &&
348 var->right_margin == ps3fb_modedb[i].right_margin &&
349 var->upper_margin == ps3fb_modedb[i].upper_margin &&
350 var->lower_margin == ps3fb_modedb[i].lower_margin &&
351 var->sync == ps3fb_modedb[i].sync &&
352 (var->vmode & FB_VMODE_MASK) == ps3fb_modedb[i].vmode) {
353 /* Cropped broadcast modes use the full line_length */
355 ps3fb_modedb[i < 10 ? i + 13 : i].xres * 4;
356 /* Full broadcast modes have the full mode bit set */
357 mode = i > 12 ? (i - 12) | PS3FB_FULL_MODE_BIT : i + 1;
359 pr_debug("ps3fb_find_mode: mode %u\n", mode);
363 pr_debug("ps3fb_find_mode: mode not found\n");
367 static const struct fb_videomode *ps3fb_default_mode(int id)
369 u32 mode = id & PS3AV_MODE_MASK;
372 if (mode < 1 || mode > 13)
375 flags = id & ~PS3AV_MODE_MASK;
377 if (mode <= 10 && flags & PS3FB_FULL_MODE_BIT) {
378 /* Full broadcast mode */
379 return &ps3fb_modedb[mode + 12];
382 return &ps3fb_modedb[mode - 1];
385 static void ps3fb_sync_image(struct device *dev, u64 frame_offset,
386 u64 dst_offset, u64 src_offset, u32 width,
387 u32 height, u64 line_length)
391 status = lv1_gpu_context_attribute(ps3fb.context_handle,
392 L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT,
393 dst_offset, GPU_IOIF + src_offset,
394 L1GPU_FB_BLIT_WAIT_FOR_COMPLETION |
395 (width << 16) | height,
399 "%s: lv1_gpu_context_attribute FB_BLIT failed: %d\n",
402 status = lv1_gpu_context_attribute(ps3fb.context_handle,
403 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
404 0, frame_offset, 0, 0);
406 dev_err(dev, "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
410 status = lv1_gpu_context_attribute(ps3fb.context_handle,
411 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
412 1, frame_offset, 0, 0);
414 dev_err(dev, "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
419 static int ps3fb_sync(struct fb_info *info, u32 frame)
421 struct ps3fb_par *par = info->par;
424 u64 line_length, base;
426 acquire_console_sem();
428 if (frame > par->num_frames - 1) {
429 dev_dbg(info->device, "%s: invalid frame number (%u)\n",
436 xres = ps3fb_res[i].xres;
437 yres = ps3fb_res[i].yres;
439 line_length = xres * BPP;
440 base = frame * yres * line_length;
442 ps3fb_sync_image(info->device, base + par->full_offset,
443 base + par->fb_offset, base, par->width, par->height,
447 release_console_sem();
451 static int ps3fb_open(struct fb_info *info, int user)
453 atomic_inc(&ps3fb.f_count);
457 static int ps3fb_release(struct fb_info *info, int user)
459 if (atomic_dec_and_test(&ps3fb.f_count)) {
460 if (atomic_read(&ps3fb.ext_flip)) {
461 atomic_set(&ps3fb.ext_flip, 0);
462 ps3fb_sync(info, 0); /* single buffer */
469 * Setting the video mode has been split into two parts.
470 * First part, xxxfb_check_var, must not write anything
471 * to hardware, it should only verify and adjust var.
472 * This means it doesn't alter par but it does use hardware
473 * data from it to check this var.
476 static int ps3fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
481 dev_dbg(info->device, "var->xres:%u info->var.xres:%u\n", var->xres,
483 dev_dbg(info->device, "var->yres:%u info->var.yres:%u\n", var->yres,
486 /* FIXME For now we do exact matches only */
487 mode = ps3fb_find_mode(var, &line_length);
492 * FB_VMODE_CONUPDATE and FB_VMODE_SMOOTH_XPAN are equal!
493 * as FB_VMODE_SMOOTH_XPAN is only used internally
496 if (var->vmode & FB_VMODE_CONUPDATE) {
497 var->vmode |= FB_VMODE_YWRAP;
498 var->xoffset = info->var.xoffset;
499 var->yoffset = info->var.yoffset;
502 /* Virtual screen and panning are not supported */
503 if (var->xres_virtual > var->xres || var->yres_virtual > var->yres ||
504 var->xoffset || var->yoffset) {
505 dev_dbg(info->device,
506 "Virtual screen and panning are not supported\n");
510 var->xres_virtual = var->xres;
511 var->yres_virtual = var->yres;
513 /* We support ARGB8888 only */
514 if (var->bits_per_pixel > 32 || var->grayscale ||
515 var->red.offset > 16 || var->green.offset > 8 ||
516 var->blue.offset > 0 || var->transp.offset > 24 ||
517 var->red.length > 8 || var->green.length > 8 ||
518 var->blue.length > 8 || var->transp.length > 8 ||
519 var->red.msb_right || var->green.msb_right ||
520 var->blue.msb_right || var->transp.msb_right || var->nonstd) {
521 dev_dbg(info->device, "We support ARGB8888 only\n");
525 var->bits_per_pixel = 32;
526 var->red.offset = 16;
527 var->green.offset = 8;
528 var->blue.offset = 0;
529 var->transp.offset = 24;
531 var->green.length = 8;
532 var->blue.length = 8;
533 var->transp.length = 8;
534 var->red.msb_right = 0;
535 var->green.msb_right = 0;
536 var->blue.msb_right = 0;
537 var->transp.msb_right = 0;
539 /* Rotation is not supported */
541 dev_dbg(info->device, "Rotation is not supported\n");
546 if (var->yres * line_length > ps3fb.xdr_size) {
547 dev_dbg(info->device, "Not enough memory\n");
558 * This routine actually sets the video mode.
561 static int ps3fb_set_par(struct fb_info *info)
563 struct ps3fb_par *par = info->par;
564 unsigned int mode, lines, maxlines;
566 unsigned long offset, dst;
568 dev_dbg(info->device, "xres:%d xv:%d yres:%d yv:%d clock:%d\n",
569 info->var.xres, info->var.xres_virtual,
570 info->var.yres, info->var.yres_virtual, info->var.pixclock);
572 mode = ps3fb_find_mode(&info->var, &info->fix.line_length);
576 i = ps3fb_get_res_table(info->var.xres, info->var.yres, mode);
579 info->fix.smem_start = virt_to_abs(ps3fb.xdr_ea);
580 info->fix.smem_len = ps3fb.xdr_size;
581 info->screen_base = (char __iomem *)ps3fb.xdr_ea;
583 par->num_frames = info->fix.smem_len/
584 (ps3fb_res[i].xres*ps3fb_res[i].yres*BPP);
586 /* Keep the special bits we cannot set using fb_var_screeninfo */
587 par->new_mode_id = (par->new_mode_id & ~PS3AV_MODE_MASK) | mode;
589 par->width = info->var.xres;
590 par->height = info->var.yres;
592 par->fb_offset = GPU_ALIGN_UP(offset);
593 par->full_offset = par->fb_offset - offset;
595 if (par->new_mode_id != par->mode_id) {
596 if (ps3av_set_video_mode(par->new_mode_id)) {
597 par->new_mode_id = par->mode_id;
600 par->mode_id = par->new_mode_id;
603 /* Clear XDR frame buffer memory */
604 memset(ps3fb.xdr_ea, 0, ps3fb.xdr_size);
606 /* Clear DDR frame buffer memory */
607 lines = ps3fb_res[i].yres * par->num_frames;
608 if (par->full_offset)
610 maxlines = ps3fb.xdr_size / info->fix.line_length;
611 for (dst = 0; lines; dst += maxlines * info->fix.line_length) {
612 unsigned int l = min(lines, maxlines);
613 ps3fb_sync_image(info->device, 0, dst, 0, ps3fb_res[i].xres, l,
614 info->fix.line_length);
622 * Set a single color register. The values supplied are already
623 * rounded down to the hardware's capabilities (according to the
624 * entries in the var structure). Return != 0 for invalid regno.
627 static int ps3fb_setcolreg(unsigned int regno, unsigned int red,
628 unsigned int green, unsigned int blue,
629 unsigned int transp, struct fb_info *info)
639 ((u32 *)info->pseudo_palette)[regno] = transp << 24 | red << 16 |
645 * As we have a virtual frame buffer, we need our own mmap function
648 static int ps3fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
650 unsigned long size, offset;
652 size = vma->vm_end - vma->vm_start;
653 offset = vma->vm_pgoff << PAGE_SHIFT;
654 if (offset + size > info->fix.smem_len)
657 offset += info->fix.smem_start;
658 if (remap_pfn_range(vma, vma->vm_start, offset >> PAGE_SHIFT,
659 size, vma->vm_page_prot))
662 dev_dbg(info->device, "ps3fb: mmap framebuffer P(%lx)->V(%lx)\n",
663 offset, vma->vm_start);
671 static int ps3fb_blank(int blank, struct fb_info *info)
675 dev_dbg(info->device, "%s: blank:%d\n", __func__, blank);
677 case FB_BLANK_POWERDOWN:
678 case FB_BLANK_HSYNC_SUSPEND:
679 case FB_BLANK_VSYNC_SUSPEND:
680 case FB_BLANK_NORMAL:
681 retval = ps3av_video_mute(1); /* mute on */
683 ps3fb.is_blanked = 1;
686 default: /* unblank */
687 retval = ps3av_video_mute(0); /* mute off */
689 ps3fb.is_blanked = 0;
695 static int ps3fb_get_vblank(struct fb_vblank *vblank)
697 memset(vblank, 0, sizeof(&vblank));
698 vblank->flags = FB_VBLANK_HAVE_VSYNC;
702 static int ps3fb_wait_for_vsync(u32 crtc)
707 count = ps3fb.vblank_count;
708 ret = wait_event_interruptible_timeout(ps3fb.wait_vsync,
709 count != ps3fb.vblank_count,
717 static void ps3fb_flip_ctl(int on, void *data)
719 struct ps3fb_priv *priv = data;
721 atomic_dec_if_positive(&priv->ext_flip);
723 atomic_inc(&priv->ext_flip);
731 static int ps3fb_ioctl(struct fb_info *info, unsigned int cmd,
734 void __user *argp = (void __user *)arg;
736 int retval = -EFAULT;
741 struct fb_vblank vblank;
742 dev_dbg(info->device, "FBIOGET_VBLANK:\n");
743 retval = ps3fb_get_vblank(&vblank);
747 if (copy_to_user(argp, &vblank, sizeof(vblank)))
752 case FBIO_WAITFORVSYNC:
755 dev_dbg(info->device, "FBIO_WAITFORVSYNC:\n");
756 if (get_user(crt, (u32 __user *) arg))
759 retval = ps3fb_wait_for_vsync(crt);
763 case PS3FB_IOCTL_SETMODE:
765 struct ps3fb_par *par = info->par;
766 const struct fb_videomode *mode;
767 struct fb_var_screeninfo var;
769 if (copy_from_user(&val, argp, sizeof(val)))
772 if (!(val & PS3AV_MODE_MASK)) {
773 u32 id = ps3av_get_auto_mode();
775 val = (val & ~PS3AV_MODE_MASK) | id;
777 dev_dbg(info->device, "PS3FB_IOCTL_SETMODE:%x\n", val);
779 mode = ps3fb_default_mode(val);
782 fb_videomode_to_var(&var, mode);
783 acquire_console_sem();
784 info->flags |= FBINFO_MISC_USEREVENT;
785 /* Force, in case only special bits changed */
786 var.activate |= FB_ACTIVATE_FORCE;
787 par->new_mode_id = val;
788 retval = fb_set_var(info, &var);
789 info->flags &= ~FBINFO_MISC_USEREVENT;
790 release_console_sem();
795 case PS3FB_IOCTL_GETMODE:
796 val = ps3av_get_mode();
797 dev_dbg(info->device, "PS3FB_IOCTL_GETMODE:%x\n", val);
798 if (!copy_to_user(argp, &val, sizeof(val)))
802 case PS3FB_IOCTL_SCREENINFO:
804 struct ps3fb_par *par = info->par;
805 struct ps3fb_ioctl_res res;
806 int i = par->res_index;
807 dev_dbg(info->device, "PS3FB_IOCTL_SCREENINFO:\n");
808 res.xres = ps3fb_res[i].xres;
809 res.yres = ps3fb_res[i].yres;
810 res.xoff = ps3fb_res[i].xoff;
811 res.yoff = ps3fb_res[i].yoff;
812 res.num_frames = par->num_frames;
813 if (!copy_to_user(argp, &res, sizeof(res)))
819 dev_dbg(info->device, "PS3FB_IOCTL_ON:\n");
820 atomic_inc(&ps3fb.ext_flip);
824 case PS3FB_IOCTL_OFF:
825 dev_dbg(info->device, "PS3FB_IOCTL_OFF:\n");
826 atomic_dec_if_positive(&ps3fb.ext_flip);
830 case PS3FB_IOCTL_FSEL:
831 if (copy_from_user(&val, argp, sizeof(val)))
834 dev_dbg(info->device, "PS3FB_IOCTL_FSEL:%d\n", val);
835 retval = ps3fb_sync(info, val);
839 retval = -ENOIOCTLCMD;
845 static int ps3fbd(void *arg)
847 struct fb_info *info = arg;
850 while (!kthread_should_stop()) {
852 set_current_state(TASK_INTERRUPTIBLE);
853 if (ps3fb.is_kicked) {
855 ps3fb_sync(info, 0); /* single buffer */
862 static irqreturn_t ps3fb_vsync_interrupt(int irq, void *ptr)
864 struct device *dev = ptr;
867 struct display_head *head = &ps3fb.dinfo->display_head[1];
869 status = lv1_gpu_context_intr(ps3fb.context_handle, &v1);
871 dev_err(dev, "%s: lv1_gpu_context_intr failed: %d\n", __func__,
876 if (v1 & (1 << GPU_INTR_STATUS_VSYNC_1)) {
878 ps3fb.vblank_count = head->vblank_count;
879 if (ps3fb.task && !ps3fb.is_blanked &&
880 !atomic_read(&ps3fb.ext_flip)) {
882 wake_up_process(ps3fb.task);
884 wake_up_interruptible(&ps3fb.wait_vsync);
891 static int ps3fb_vsync_settings(struct gpu_driver_info *dinfo,
896 dev_dbg(dev, "version_driver:%x\n", dinfo->version_driver);
897 dev_dbg(dev, "irq outlet:%x\n", dinfo->irq.irq_outlet);
899 "version_gpu: %x memory_size: %x ch: %x core_freq: %d "
901 dinfo->version_gpu, dinfo->memory_size, dinfo->hardware_channel,
902 dinfo->nvcore_frequency/1000000, dinfo->memory_frequency/1000000);
904 if (dinfo->version_driver != GPU_DRIVER_INFO_VERSION) {
905 dev_err(dev, "%s: version_driver err:%x\n", __func__,
906 dinfo->version_driver);
910 error = ps3_irq_plug_setup(PS3_BINDING_CPU_ANY, dinfo->irq.irq_outlet,
913 dev_err(dev, "%s: ps3_alloc_irq failed %d\n", __func__, error);
917 error = request_irq(ps3fb.irq_no, ps3fb_vsync_interrupt, IRQF_DISABLED,
920 dev_err(dev, "%s: request_irq failed %d\n", __func__, error);
921 ps3_irq_plug_destroy(ps3fb.irq_no);
925 dinfo->irq.mask = (1 << GPU_INTR_STATUS_VSYNC_1) |
926 (1 << GPU_INTR_STATUS_FLIP_1);
930 static int ps3fb_xdr_settings(u64 xdr_lpar, struct device *dev)
934 status = lv1_gpu_context_iomap(ps3fb.context_handle, GPU_IOIF,
935 xdr_lpar, ps3fb_videomemory.size, 0);
937 dev_err(dev, "%s: lv1_gpu_context_iomap failed: %d\n",
942 "video:%p xdr_ea:%p ioif:%lx lpar:%lx phys:%lx size:%lx\n",
943 ps3fb_videomemory.address, ps3fb.xdr_ea, GPU_IOIF, xdr_lpar,
944 virt_to_abs(ps3fb.xdr_ea), ps3fb_videomemory.size);
946 status = lv1_gpu_context_attribute(ps3fb.context_handle,
947 L1GPU_CONTEXT_ATTRIBUTE_FB_SETUP,
948 xdr_lpar + ps3fb.xdr_size,
950 GPU_IOIF + ps3fb.xdr_size, 0);
953 "%s: lv1_gpu_context_attribute FB_SETUP failed: %d\n",
960 static struct fb_ops ps3fb_ops = {
961 .fb_open = ps3fb_open,
962 .fb_release = ps3fb_release,
963 .fb_read = fb_sys_read,
964 .fb_write = fb_sys_write,
965 .fb_check_var = ps3fb_check_var,
966 .fb_set_par = ps3fb_set_par,
967 .fb_setcolreg = ps3fb_setcolreg,
968 .fb_fillrect = sys_fillrect,
969 .fb_copyarea = sys_copyarea,
970 .fb_imageblit = sys_imageblit,
971 .fb_mmap = ps3fb_mmap,
972 .fb_blank = ps3fb_blank,
973 .fb_ioctl = ps3fb_ioctl,
974 .fb_compat_ioctl = ps3fb_ioctl
977 static struct fb_fix_screeninfo ps3fb_fix __initdata = {
979 .type = FB_TYPE_PACKED_PIXELS,
980 .visual = FB_VISUAL_TRUECOLOR,
981 .accel = FB_ACCEL_NONE,
984 static int ps3fb_set_sync(struct device *dev)
989 status = lv1_gpu_context_attribute(0x0,
990 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
991 0, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);
994 "%s: lv1_gpu_context_attribute DISPLAY_SYNC failed: "
1001 status = lv1_gpu_context_attribute(0x0,
1002 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
1003 1, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);
1007 "%s: lv1_gpu_context_attribute DISPLAY_SYNC failed: "
1016 static int __devinit ps3fb_probe(struct ps3_system_bus_device *dev)
1018 struct fb_info *info;
1019 struct ps3fb_par *par;
1020 int retval = -ENOMEM;
1023 u64 lpar_dma_control = 0;
1024 u64 lpar_driver_info = 0;
1025 u64 lpar_reports = 0;
1026 u64 lpar_reports_size = 0;
1028 int status, res_index;
1029 struct task_struct *task;
1031 status = ps3_open_hv_device(dev);
1033 dev_err(&dev->core, "%s: ps3_open_hv_device failed\n",
1039 ps3fb_mode = ps3av_get_mode();
1040 dev_dbg(&dev->core, "ps3av_mode:%d\n", ps3fb_mode);
1042 if (ps3fb_mode > 0 &&
1043 !ps3av_video_mode2res(ps3fb_mode, &xres, &yres)) {
1044 res_index = ps3fb_get_res_table(xres, yres, ps3fb_mode);
1045 dev_dbg(&dev->core, "res_index:%d\n", res_index);
1047 res_index = GPU_RES_INDEX;
1049 atomic_set(&ps3fb.f_count, -1); /* fbcon opens ps3fb */
1050 atomic_set(&ps3fb.ext_flip, 0); /* for flip with vsync */
1051 init_waitqueue_head(&ps3fb.wait_vsync);
1053 ps3fb_set_sync(&dev->core);
1055 /* get gpu context handle */
1056 status = lv1_gpu_memory_allocate(DDR_SIZE, 0, 0, 0, 0,
1057 &ps3fb.memory_handle, &ddr_lpar);
1059 dev_err(&dev->core, "%s: lv1_gpu_memory_allocate failed: %d\n",
1063 dev_dbg(&dev->core, "ddr:lpar:0x%lx\n", ddr_lpar);
1065 status = lv1_gpu_context_allocate(ps3fb.memory_handle, 0,
1066 &ps3fb.context_handle,
1067 &lpar_dma_control, &lpar_driver_info,
1068 &lpar_reports, &lpar_reports_size);
1071 "%s: lv1_gpu_context_attribute failed: %d\n", __func__,
1073 goto err_gpu_memory_free;
1076 /* vsync interrupt */
1077 ps3fb.dinfo = ioremap(lpar_driver_info, 128 * 1024);
1079 dev_err(&dev->core, "%s: ioremap failed\n", __func__);
1080 goto err_gpu_context_free;
1083 retval = ps3fb_vsync_settings(ps3fb.dinfo, &dev->core);
1085 goto err_iounmap_dinfo;
1087 /* XDR frame buffer */
1088 ps3fb.xdr_ea = ps3fb_videomemory.address;
1089 xdr_lpar = ps3_mm_phys_to_lpar(__pa(ps3fb.xdr_ea));
1091 /* Clear memory to prevent kernel info leakage into userspace */
1092 memset(ps3fb.xdr_ea, 0, ps3fb_videomemory.size);
1094 /* The GPU command buffer is at the end of video memory */
1095 ps3fb.xdr_size = ps3fb_videomemory.size - GPU_CMD_BUF_SIZE;
1097 retval = ps3fb_xdr_settings(xdr_lpar, &dev->core);
1101 info = framebuffer_alloc(sizeof(struct ps3fb_par), &dev->core);
1106 par->mode_id = ~ps3fb_mode; /* != ps3fb_mode, to trigger change */
1107 par->new_mode_id = ps3fb_mode;
1108 par->res_index = res_index;
1109 par->num_frames = 1;
1111 info->screen_base = (char __iomem *)ps3fb.xdr_ea;
1112 info->fbops = &ps3fb_ops;
1114 info->fix = ps3fb_fix;
1115 info->fix.smem_start = virt_to_abs(ps3fb.xdr_ea);
1116 info->fix.smem_len = ps3fb.xdr_size;
1117 info->pseudo_palette = par->pseudo_palette;
1118 info->flags = FBINFO_DEFAULT | FBINFO_READS_FAST;
1120 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1122 goto err_framebuffer_release;
1124 if (!fb_find_mode(&info->var, info, mode_option, ps3fb_modedb,
1125 ARRAY_SIZE(ps3fb_modedb),
1126 ps3fb_default_mode(par->new_mode_id), 32)) {
1128 goto err_fb_dealloc;
1131 fb_videomode_to_modelist(ps3fb_modedb, ARRAY_SIZE(ps3fb_modedb),
1134 retval = register_framebuffer(info);
1136 goto err_fb_dealloc;
1138 dev->core.driver_data = info;
1140 dev_info(info->device, "%s %s, using %lu KiB of video memory\n",
1141 dev_driver_string(info->dev), info->dev->bus_id,
1142 ps3fb.xdr_size >> 10);
1144 task = kthread_run(ps3fbd, info, DEVICE_NAME);
1146 retval = PTR_ERR(task);
1147 goto err_unregister_framebuffer;
1151 ps3av_register_flip_ctl(ps3fb_flip_ctl, &ps3fb);
1155 err_unregister_framebuffer:
1156 unregister_framebuffer(info);
1158 fb_dealloc_cmap(&info->cmap);
1159 err_framebuffer_release:
1160 framebuffer_release(info);
1162 free_irq(ps3fb.irq_no, dev);
1163 ps3_irq_plug_destroy(ps3fb.irq_no);
1165 iounmap((u8 __iomem *)ps3fb.dinfo);
1166 err_gpu_context_free:
1167 lv1_gpu_context_free(ps3fb.context_handle);
1168 err_gpu_memory_free:
1169 lv1_gpu_memory_free(ps3fb.memory_handle);
1174 static int ps3fb_shutdown(struct ps3_system_bus_device *dev)
1177 struct fb_info *info = dev->core.driver_data;
1179 dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
1181 ps3fb_flip_ctl(0, &ps3fb); /* flip off */
1182 ps3fb.dinfo->irq.mask = 0;
1185 unregister_framebuffer(info);
1186 fb_dealloc_cmap(&info->cmap);
1187 framebuffer_release(info);
1190 ps3av_register_flip_ctl(NULL, NULL);
1192 struct task_struct *task = ps3fb.task;
1197 free_irq(ps3fb.irq_no, dev);
1198 ps3_irq_plug_destroy(ps3fb.irq_no);
1200 iounmap((u8 __iomem *)ps3fb.dinfo);
1202 status = lv1_gpu_context_free(ps3fb.context_handle);
1204 dev_dbg(&dev->core, "lv1_gpu_context_free failed: %d\n",
1207 status = lv1_gpu_memory_free(ps3fb.memory_handle);
1209 dev_dbg(&dev->core, "lv1_gpu_memory_free failed: %d\n",
1212 ps3_close_hv_device(dev);
1213 dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
1218 static struct ps3_system_bus_driver ps3fb_driver = {
1219 .match_id = PS3_MATCH_ID_GRAPHICS,
1220 .core.name = DEVICE_NAME,
1221 .core.owner = THIS_MODULE,
1222 .probe = ps3fb_probe,
1223 .remove = ps3fb_shutdown,
1224 .shutdown = ps3fb_shutdown,
1227 static int __init ps3fb_setup(void)
1235 if (fb_get_options(DEVICE_NAME, &options))
1238 if (!options || !*options)
1242 char *this_opt = strsep(&options, ",");
1248 if (!strncmp(this_opt, "mode:", 5))
1249 ps3fb_mode = simple_strtoul(this_opt + 5, NULL, 0);
1251 mode_option = this_opt;
1256 static int __init ps3fb_init(void)
1258 if (!ps3fb_videomemory.address || ps3fb_setup())
1261 return ps3_system_bus_driver_register(&ps3fb_driver);
1264 static void __exit ps3fb_exit(void)
1266 pr_debug(" -> %s:%d\n", __func__, __LINE__);
1267 ps3_system_bus_driver_unregister(&ps3fb_driver);
1268 pr_debug(" <- %s:%d\n", __func__, __LINE__);
1271 module_init(ps3fb_init);
1272 module_exit(ps3fb_exit);
1274 MODULE_LICENSE("GPL");
1275 MODULE_DESCRIPTION("PS3 GPU Frame Buffer Driver");
1276 MODULE_AUTHOR("Sony Computer Entertainment Inc.");
1277 MODULE_ALIAS(PS3_MODULE_ALIAS_GRAPHICS);