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)
58 #define PS3FB_FULL_MODE_BIT 0x80
60 #define GPU_INTR_STATUS_VSYNC_0 0 /* vsync on head A */
61 #define GPU_INTR_STATUS_VSYNC_1 1 /* vsync on head B */
62 #define GPU_INTR_STATUS_FLIP_0 3 /* flip head A */
63 #define GPU_INTR_STATUS_FLIP_1 4 /* flip head B */
64 #define GPU_INTR_STATUS_QUEUE_0 5 /* queue head A */
65 #define GPU_INTR_STATUS_QUEUE_1 6 /* queue head B */
67 #define GPU_DRIVER_INFO_VERSION 0x211
101 struct gpu_driver_info {
105 u32 hardware_channel;
107 u32 nvcore_frequency;
108 u32 memory_frequency;
111 struct display_head display_head[8];
118 u64 context_handle, memory_handle;
121 struct gpu_driver_info *dinfo;
124 u64 vblank_count; /* frame count */
125 wait_queue_head_t wait_vsync;
127 u32 num_frames; /* num of frame buffers */
128 atomic_t ext_flip; /* on/off flip with vsync */
129 atomic_t f_count; /* fb_open count */
132 struct task_struct *task;
134 static struct ps3fb_priv ps3fb;
136 struct ps3fb_res_table {
143 #define PS3FB_RES_FULL 1
144 static const struct ps3fb_res_table ps3fb_res[] = {
145 /* res_x,y margin_x,y full */
146 { 720, 480, 72, 48 , 0},
147 { 720, 576, 72, 58 , 0},
148 { 1280, 720, 78, 38 , 0},
149 { 1920, 1080, 116, 58 , 0},
151 { 720, 480, 0, 0 , PS3FB_RES_FULL},
152 { 720, 576, 0, 0 , PS3FB_RES_FULL},
153 { 1280, 720, 0, 0 , PS3FB_RES_FULL},
154 { 1920, 1080, 0, 0 , PS3FB_RES_FULL},
155 /* vesa: normally full mode */
156 { 1280, 768, 0, 0 , 0},
157 { 1280, 1024, 0, 0 , 0},
158 { 1920, 1200, 0, 0 , 0},
161 /* default resolution */
162 #define GPU_RES_INDEX 0 /* 720 x 480 */
164 static const struct fb_videomode ps3fb_modedb[] = {
165 /* 60 Hz broadcast modes (modes "1" to "5") */
168 "480i", 60, 576, 384, 74074, 130, 89, 78, 57, 63, 6,
169 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
172 "480p", 60, 576, 384, 37037, 130, 89, 78, 57, 63, 6,
173 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
176 "720p", 60, 1124, 644, 13481, 298, 148, 57, 44, 80, 5,
177 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
180 "1080i", 60, 1688, 964, 13481, 264, 160, 94, 62, 88, 5,
181 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
184 "1080p", 60, 1688, 964, 6741, 264, 160, 94, 62, 88, 5,
185 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
188 /* 50 Hz broadcast modes (modes "6" to "10") */
191 "576i", 50, 576, 460, 74074, 142, 83, 97, 63, 63, 5,
192 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
195 "576p", 50, 576, 460, 37037, 142, 83, 97, 63, 63, 5,
196 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
199 "720p", 50, 1124, 644, 13468, 298, 478, 57, 44, 80, 5,
200 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
203 "1080i", 50, 1688, 964, 13468, 264, 600, 94, 62, 88, 5,
204 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
207 "1080p", 50, 1688, 964, 6734, 264, 600, 94, 62, 88, 5,
208 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
211 /* VESA modes (modes "11" to "13") */
214 "wxga", 60, 1280, 768, 12924, 160, 24, 29, 3, 136, 6,
215 0, FB_VMODE_NONINTERLACED,
219 "sxga", 60, 1280, 1024, 9259, 248, 48, 38, 1, 112, 3,
220 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED,
224 "wuxga", 60, 1920, 1200, 6494, 80, 48, 26, 3, 32, 6,
225 FB_SYNC_HOR_HIGH_ACT, FB_VMODE_NONINTERLACED,
229 /* 60 Hz broadcast modes (full resolution versions of modes "1" to "5") */
232 "480if", 60, 720, 480, 74074, 58, 17, 30, 9, 63, 6,
233 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
236 "480pf", 60, 720, 480, 37037, 58, 17, 30, 9, 63, 6,
237 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
240 "720pf", 60, 1280, 720, 13481, 220, 70, 19, 6, 80, 5,
241 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
244 "1080if", 60, 1920, 1080, 13481, 148, 44, 36, 4, 88, 5,
245 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
248 "1080pf", 60, 1920, 1080, 6741, 148, 44, 36, 4, 88, 5,
249 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
252 /* 50 Hz broadcast modes (full resolution versions of modes "6" to "10") */
255 "576if", 50, 720, 576, 74074, 70, 11, 39, 5, 63, 5,
256 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
259 "576pf", 50, 720, 576, 37037, 70, 11, 39, 5, 63, 5,
260 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
263 "720pf", 50, 1280, 720, 13468, 220, 400, 19, 6, 80, 5,
264 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
267 "1080f", 50, 1920, 1080, 13468, 148, 484, 36, 4, 88, 5,
268 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
271 "1080pf", 50, 1920, 1080, 6734, 148, 484, 36, 4, 88, 5,
272 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
280 #define X_OFF(i) (ps3fb_res[i].xoff) /* left/right margin (pixel) */
281 #define Y_OFF(i) (ps3fb_res[i].yoff) /* top/bottom margin (pixel) */
282 #define WIDTH(i) (ps3fb_res[i].xres) /* width of FB */
283 #define HEIGHT(i) (ps3fb_res[i].yres) /* height of FB */
284 #define BPP 4 /* number of bytes per pixel */
286 /* Start of the virtual frame buffer (relative to fullscreen ) */
287 #define VP_OFF(i) ((WIDTH(i) * Y_OFF(i) + X_OFF(i)) * BPP)
290 * Start of the virtual frame buffer (relative to start of video memory)
291 * This is PAGE_SIZE aligned for easier mmap()
293 #define VFB_OFF(i) PAGE_ALIGN(VP_OFF(i))
295 /* Start of the fullscreen frame buffer (relative to start of video memory) */
296 #define FB_OFF(i) (-VP_OFF(i) & ~PAGE_MASK)
299 static int ps3fb_mode;
300 module_param(ps3fb_mode, int, 0);
302 static char *mode_option __devinitdata;
304 static int ps3fb_get_res_table(u32 xres, u32 yres, int mode)
310 full_mode = (mode & PS3FB_FULL_MODE_BIT) ? PS3FB_RES_FULL : 0;
312 x = ps3fb_res[i].xres;
313 y = ps3fb_res[i].yres;
314 f = ps3fb_res[i].type;
317 pr_debug("ERROR: ps3fb_get_res_table()\n");
321 if (full_mode == PS3FB_RES_FULL && f != PS3FB_RES_FULL)
324 if (x == xres && (yres == 0 || y == yres))
327 x = x - 2 * ps3fb_res[i].xoff;
328 y = y - 2 * ps3fb_res[i].yoff;
329 if (x == xres && (yres == 0 || y == yres))
335 static unsigned int ps3fb_find_mode(const struct fb_var_screeninfo *var,
338 unsigned int i, mode;
340 for (i = 0; i < ARRAY_SIZE(ps3fb_modedb); i++)
341 if (var->xres == ps3fb_modedb[i].xres &&
342 var->yres == ps3fb_modedb[i].yres &&
343 var->pixclock == ps3fb_modedb[i].pixclock &&
344 var->hsync_len == ps3fb_modedb[i].hsync_len &&
345 var->vsync_len == ps3fb_modedb[i].vsync_len &&
346 var->left_margin == ps3fb_modedb[i].left_margin &&
347 var->right_margin == ps3fb_modedb[i].right_margin &&
348 var->upper_margin == ps3fb_modedb[i].upper_margin &&
349 var->lower_margin == ps3fb_modedb[i].lower_margin &&
350 var->sync == ps3fb_modedb[i].sync &&
351 (var->vmode & FB_VMODE_MASK) == ps3fb_modedb[i].vmode) {
352 /* Cropped broadcast modes use the full line_length */
354 ps3fb_modedb[i < 10 ? i + 13 : i].xres * 4;
355 /* Full broadcast modes have the full mode bit set */
356 mode = i > 12 ? (i - 12) | PS3FB_FULL_MODE_BIT : i + 1;
358 pr_debug("ps3fb_find_mode: mode %u\n", mode);
362 pr_debug("ps3fb_find_mode: mode not found\n");
367 static const struct fb_videomode *ps3fb_default_mode(void)
369 u32 mode = ps3fb_mode & PS3AV_MODE_MASK;
372 if (mode < 1 || mode > 13)
375 flags = ps3fb_mode & ~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 int ps3fb_sync(struct fb_info *info, u32 frame)
392 xres = ps3fb_res[i].xres;
393 yres = ps3fb_res[i].yres;
395 if (frame > ps3fb.num_frames - 1) {
396 dev_dbg(info->device, "%s: invalid frame number (%u)\n",
400 offset = xres * yres * BPP * frame;
402 fb_ioif = GPU_IOIF + FB_OFF(i) + offset;
403 status = lv1_gpu_context_attribute(ps3fb.context_handle,
404 L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT,
406 L1GPU_FB_BLIT_WAIT_FOR_COMPLETION |
408 xres * BPP); /* line_length */
410 dev_err(info->device,
411 "%s: lv1_gpu_context_attribute FB_BLIT failed: %d\n",
414 status = lv1_gpu_context_attribute(ps3fb.context_handle,
415 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
418 dev_err(info->device,
419 "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
423 status = lv1_gpu_context_attribute(ps3fb.context_handle,
424 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
427 dev_err(info->device,
428 "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
435 static int ps3fb_open(struct fb_info *info, int user)
437 atomic_inc(&ps3fb.f_count);
441 static int ps3fb_release(struct fb_info *info, int user)
443 if (atomic_dec_and_test(&ps3fb.f_count)) {
444 if (atomic_read(&ps3fb.ext_flip)) {
445 atomic_set(&ps3fb.ext_flip, 0);
446 ps3fb_sync(info, 0); /* single buffer */
453 * Setting the video mode has been split into two parts.
454 * First part, xxxfb_check_var, must not write anything
455 * to hardware, it should only verify and adjust var.
456 * This means it doesn't alter par but it does use hardware
457 * data from it to check this var.
460 static int ps3fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
466 dev_dbg(info->device, "var->xres:%u info->var.xres:%u\n", var->xres,
468 dev_dbg(info->device, "var->yres:%u info->var.yres:%u\n", var->yres,
471 /* FIXME For now we do exact matches only */
472 mode = ps3fb_find_mode(var, &line_length);
477 * FB_VMODE_CONUPDATE and FB_VMODE_SMOOTH_XPAN are equal!
478 * as FB_VMODE_SMOOTH_XPAN is only used internally
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;
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 dev_dbg(info->device,
491 "Virtual screen and panning are not supported\n");
495 var->xres_virtual = var->xres;
496 var->yres_virtual = var->yres;
498 /* We support ARGB8888 only */
499 if (var->bits_per_pixel > 32 || var->grayscale ||
500 var->red.offset > 16 || var->green.offset > 8 ||
501 var->blue.offset > 0 || var->transp.offset > 24 ||
502 var->red.length > 8 || var->green.length > 8 ||
503 var->blue.length > 8 || var->transp.length > 8 ||
504 var->red.msb_right || var->green.msb_right ||
505 var->blue.msb_right || var->transp.msb_right || var->nonstd) {
506 dev_dbg(info->device, "We support ARGB8888 only\n");
510 var->bits_per_pixel = 32;
511 var->red.offset = 16;
512 var->green.offset = 8;
513 var->blue.offset = 0;
514 var->transp.offset = 24;
516 var->green.length = 8;
517 var->blue.length = 8;
518 var->transp.length = 8;
519 var->red.msb_right = 0;
520 var->green.msb_right = 0;
521 var->blue.msb_right = 0;
522 var->transp.msb_right = 0;
524 /* Rotation is not supported */
526 dev_dbg(info->device, "Rotation is not supported\n");
531 i = ps3fb_get_res_table(var->xres, var->yres, mode);
532 if (ps3fb_res[i].xres*ps3fb_res[i].yres*BPP >
533 ps3fb.xdr_size - VFB_OFF(i)) {
534 dev_dbg(info->device, "Not enough memory\n");
545 * This routine actually sets the video mode.
548 static int ps3fb_set_par(struct fb_info *info)
552 unsigned long offset;
554 dev_dbg(info->device, "xres:%d xv:%d yres:%d yv:%d clock:%d\n",
555 info->var.xres, info->var.xres_virtual,
556 info->var.yres, info->var.yres_virtual, info->var.pixclock);
558 mode = ps3fb_find_mode(&info->var, &info->fix.line_length);
562 i = ps3fb_get_res_table(info->var.xres, info->var.yres, mode);
566 info->fix.smem_start = virt_to_abs(ps3fb.xdr_ea) + offset;
567 info->fix.smem_len = ps3fb.xdr_size - offset;
568 info->screen_base = (char __iomem *)ps3fb.xdr_ea + offset;
569 memset(ps3fb.xdr_ea, 0, ps3fb.xdr_size);
571 ps3fb.num_frames = info->fix.smem_len/
572 (ps3fb_res[i].xres*ps3fb_res[i].yres*BPP);
574 /* Keep the special bits we cannot set using fb_var_screeninfo */
575 ps3fb_mode = (ps3fb_mode & ~PS3AV_MODE_MASK) | mode;
577 if (ps3av_set_video_mode(ps3fb_mode))
584 * Set a single color register. The values supplied are already
585 * rounded down to the hardware's capabilities (according to the
586 * entries in the var structure). Return != 0 for invalid regno.
589 static int ps3fb_setcolreg(unsigned int regno, unsigned int red,
590 unsigned int green, unsigned int blue,
591 unsigned int transp, struct fb_info *info)
601 ((u32 *)info->pseudo_palette)[regno] = transp << 24 | red << 16 |
607 * As we have a virtual frame buffer, we need our own mmap function
610 static int ps3fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
612 unsigned long size, offset;
614 size = vma->vm_end - vma->vm_start;
615 offset = vma->vm_pgoff << PAGE_SHIFT;
616 if (offset + size > info->fix.smem_len)
619 offset += info->fix.smem_start;
620 if (remap_pfn_range(vma, vma->vm_start, offset >> PAGE_SHIFT,
621 size, vma->vm_page_prot))
624 dev_dbg(info->device, "ps3fb: mmap framebuffer P(%lx)->V(%lx)\n",
625 offset, vma->vm_start);
633 static int ps3fb_blank(int blank, struct fb_info *info)
637 dev_dbg(info->device, "%s: blank:%d\n", __func__, blank);
639 case FB_BLANK_POWERDOWN:
640 case FB_BLANK_HSYNC_SUSPEND:
641 case FB_BLANK_VSYNC_SUSPEND:
642 case FB_BLANK_NORMAL:
643 retval = ps3av_video_mute(1); /* mute on */
645 ps3fb.is_blanked = 1;
648 default: /* unblank */
649 retval = ps3av_video_mute(0); /* mute off */
651 ps3fb.is_blanked = 0;
657 static int ps3fb_get_vblank(struct fb_vblank *vblank)
659 memset(vblank, 0, sizeof(&vblank));
660 vblank->flags = FB_VBLANK_HAVE_VSYNC;
664 static int ps3fb_wait_for_vsync(u32 crtc)
669 count = ps3fb.vblank_count;
670 ret = wait_event_interruptible_timeout(ps3fb.wait_vsync,
671 count != ps3fb.vblank_count,
679 static void ps3fb_flip_ctl(int on, void *data)
681 struct ps3fb_priv *priv = data;
683 atomic_dec_if_positive(&priv->ext_flip);
685 atomic_inc(&priv->ext_flip);
693 static int ps3fb_ioctl(struct fb_info *info, unsigned int cmd,
696 void __user *argp = (void __user *)arg;
698 int retval = -EFAULT;
703 struct fb_vblank vblank;
704 dev_dbg(info->device, "FBIOGET_VBLANK:\n");
705 retval = ps3fb_get_vblank(&vblank);
709 if (copy_to_user(argp, &vblank, sizeof(vblank)))
714 case FBIO_WAITFORVSYNC:
717 dev_dbg(info->device, "FBIO_WAITFORVSYNC:\n");
718 if (get_user(crt, (u32 __user *) arg))
721 retval = ps3fb_wait_for_vsync(crt);
725 case PS3FB_IOCTL_SETMODE:
727 const struct fb_videomode *mode;
728 struct fb_var_screeninfo var;
730 if (copy_from_user(&val, argp, sizeof(val)))
733 if (!(val & PS3AV_MODE_MASK)) {
734 u32 id = ps3av_get_auto_mode();
736 val = (val & ~PS3AV_MODE_MASK) | id;
738 dev_dbg(info->device, "PS3FB_IOCTL_SETMODE:%x\n", val);
740 old_mode = ps3fb_mode;
742 mode = ps3fb_default_mode();
745 fb_videomode_to_var(&var, mode);
746 acquire_console_sem();
747 info->flags |= FBINFO_MISC_USEREVENT;
748 /* Force, in case only special bits changed */
749 var.activate |= FB_ACTIVATE_FORCE;
750 retval = fb_set_var(info, &var);
751 info->flags &= ~FBINFO_MISC_USEREVENT;
752 release_console_sem();
755 ps3fb_mode = old_mode;
759 case PS3FB_IOCTL_GETMODE:
760 val = ps3av_get_mode();
761 dev_dbg(info->device, "PS3FB_IOCTL_GETMODE:%x\n", val);
762 if (!copy_to_user(argp, &val, sizeof(val)))
766 case PS3FB_IOCTL_SCREENINFO:
768 struct ps3fb_ioctl_res res;
769 int i = ps3fb.res_index;
770 dev_dbg(info->device, "PS3FB_IOCTL_SCREENINFO:\n");
771 res.xres = ps3fb_res[i].xres;
772 res.yres = ps3fb_res[i].yres;
773 res.xoff = ps3fb_res[i].xoff;
774 res.yoff = ps3fb_res[i].yoff;
775 res.num_frames = ps3fb.num_frames;
776 if (!copy_to_user(argp, &res, sizeof(res)))
782 dev_dbg(info->device, "PS3FB_IOCTL_ON:\n");
783 atomic_inc(&ps3fb.ext_flip);
787 case PS3FB_IOCTL_OFF:
788 dev_dbg(info->device, "PS3FB_IOCTL_OFF:\n");
789 atomic_dec_if_positive(&ps3fb.ext_flip);
793 case PS3FB_IOCTL_FSEL:
794 if (copy_from_user(&val, argp, sizeof(val)))
797 dev_dbg(info->device, "PS3FB_IOCTL_FSEL:%d\n", val);
798 retval = ps3fb_sync(info, val);
802 retval = -ENOIOCTLCMD;
808 static int ps3fbd(void *arg)
810 struct fb_info *info = arg;
813 while (!kthread_should_stop()) {
815 set_current_state(TASK_INTERRUPTIBLE);
816 if (ps3fb.is_kicked) {
818 ps3fb_sync(info, 0); /* single buffer */
825 static irqreturn_t ps3fb_vsync_interrupt(int irq, void *ptr)
827 struct device *dev = ptr;
830 struct display_head *head = &ps3fb.dinfo->display_head[1];
832 status = lv1_gpu_context_intr(ps3fb.context_handle, &v1);
834 dev_err(dev, "%s: lv1_gpu_context_intr failed: %d\n", __func__,
839 if (v1 & (1 << GPU_INTR_STATUS_VSYNC_1)) {
841 ps3fb.vblank_count = head->vblank_count;
842 if (ps3fb.task && !ps3fb.is_blanked &&
843 !atomic_read(&ps3fb.ext_flip)) {
845 wake_up_process(ps3fb.task);
847 wake_up_interruptible(&ps3fb.wait_vsync);
854 static int ps3fb_vsync_settings(struct gpu_driver_info *dinfo,
859 dev_dbg(dev, "version_driver:%x\n", dinfo->version_driver);
860 dev_dbg(dev, "irq outlet:%x\n", dinfo->irq.irq_outlet);
862 "version_gpu: %x memory_size: %x ch: %x core_freq: %d "
864 dinfo->version_gpu, dinfo->memory_size, dinfo->hardware_channel,
865 dinfo->nvcore_frequency/1000000, dinfo->memory_frequency/1000000);
867 if (dinfo->version_driver != GPU_DRIVER_INFO_VERSION) {
868 dev_err(dev, "%s: version_driver err:%x\n", __func__,
869 dinfo->version_driver);
873 error = ps3_irq_plug_setup(PS3_BINDING_CPU_ANY, dinfo->irq.irq_outlet,
876 dev_err(dev, "%s: ps3_alloc_irq failed %d\n", __func__, error);
880 error = request_irq(ps3fb.irq_no, ps3fb_vsync_interrupt, IRQF_DISABLED,
883 dev_err(dev, "%s: request_irq failed %d\n", __func__, error);
884 ps3_irq_plug_destroy(ps3fb.irq_no);
888 dinfo->irq.mask = (1 << GPU_INTR_STATUS_VSYNC_1) |
889 (1 << GPU_INTR_STATUS_FLIP_1);
893 static int ps3fb_xdr_settings(u64 xdr_lpar, struct device *dev)
897 status = lv1_gpu_context_iomap(ps3fb.context_handle, GPU_IOIF,
898 xdr_lpar, ps3fb_videomemory.size, 0);
900 dev_err(dev, "%s: lv1_gpu_context_iomap failed: %d\n",
905 "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);
909 status = lv1_gpu_context_attribute(ps3fb.context_handle,
910 L1GPU_CONTEXT_ATTRIBUTE_FB_SETUP,
911 xdr_lpar + ps3fb.xdr_size,
913 GPU_IOIF + ps3fb.xdr_size, 0);
916 "%s: lv1_gpu_context_attribute FB_SETUP failed: %d\n",
923 static struct fb_ops ps3fb_ops = {
924 .fb_open = ps3fb_open,
925 .fb_release = ps3fb_release,
926 .fb_read = fb_sys_read,
927 .fb_write = fb_sys_write,
928 .fb_check_var = ps3fb_check_var,
929 .fb_set_par = ps3fb_set_par,
930 .fb_setcolreg = ps3fb_setcolreg,
931 .fb_fillrect = sys_fillrect,
932 .fb_copyarea = sys_copyarea,
933 .fb_imageblit = sys_imageblit,
934 .fb_mmap = ps3fb_mmap,
935 .fb_blank = ps3fb_blank,
936 .fb_ioctl = ps3fb_ioctl,
937 .fb_compat_ioctl = ps3fb_ioctl
940 static struct fb_fix_screeninfo ps3fb_fix __initdata = {
942 .type = FB_TYPE_PACKED_PIXELS,
943 .visual = FB_VISUAL_TRUECOLOR,
944 .accel = FB_ACCEL_NONE,
947 static int ps3fb_set_sync(struct device *dev)
952 status = lv1_gpu_context_attribute(0x0,
953 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
954 0, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);
957 "%s: lv1_gpu_context_attribute DISPLAY_SYNC failed: "
964 status = lv1_gpu_context_attribute(0x0,
965 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
966 1, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);
970 "%s: lv1_gpu_context_attribute DISPLAY_SYNC failed: "
979 static int __devinit ps3fb_probe(struct ps3_system_bus_device *dev)
981 struct fb_info *info;
982 int retval = -ENOMEM;
985 u64 lpar_dma_control = 0;
986 u64 lpar_driver_info = 0;
987 u64 lpar_reports = 0;
988 u64 lpar_reports_size = 0;
991 unsigned long offset;
992 struct task_struct *task;
994 status = ps3_open_hv_device(dev);
996 dev_err(&dev->core, "%s: ps3_open_hv_device failed\n",
1002 ps3fb_mode = ps3av_get_mode();
1003 dev_dbg(&dev->core, "ps3av_mode:%d\n", ps3fb_mode);
1005 if (ps3fb_mode > 0 &&
1006 !ps3av_video_mode2res(ps3fb_mode, &xres, &yres)) {
1007 ps3fb.res_index = ps3fb_get_res_table(xres, yres, ps3fb_mode);
1008 dev_dbg(&dev->core, "res_index:%d\n", ps3fb.res_index);
1010 ps3fb.res_index = GPU_RES_INDEX;
1012 atomic_set(&ps3fb.f_count, -1); /* fbcon opens ps3fb */
1013 atomic_set(&ps3fb.ext_flip, 0); /* for flip with vsync */
1014 init_waitqueue_head(&ps3fb.wait_vsync);
1015 ps3fb.num_frames = 1;
1017 ps3fb_set_sync(&dev->core);
1019 /* get gpu context handle */
1020 status = lv1_gpu_memory_allocate(DDR_SIZE, 0, 0, 0, 0,
1021 &ps3fb.memory_handle, &ddr_lpar);
1023 dev_err(&dev->core, "%s: lv1_gpu_memory_allocate failed: %d\n",
1027 dev_dbg(&dev->core, "ddr:lpar:0x%lx\n", ddr_lpar);
1029 status = lv1_gpu_context_allocate(ps3fb.memory_handle, 0,
1030 &ps3fb.context_handle,
1031 &lpar_dma_control, &lpar_driver_info,
1032 &lpar_reports, &lpar_reports_size);
1035 "%s: lv1_gpu_context_attribute failed: %d\n", __func__,
1037 goto err_gpu_memory_free;
1040 /* vsync interrupt */
1041 ps3fb.dinfo = ioremap(lpar_driver_info, 128 * 1024);
1043 dev_err(&dev->core, "%s: ioremap failed\n", __func__);
1044 goto err_gpu_context_free;
1047 retval = ps3fb_vsync_settings(ps3fb.dinfo, &dev->core);
1049 goto err_iounmap_dinfo;
1051 /* XDR frame buffer */
1052 ps3fb.xdr_ea = ps3fb_videomemory.address;
1053 xdr_lpar = ps3_mm_phys_to_lpar(__pa(ps3fb.xdr_ea));
1055 /* Clear memory to prevent kernel info leakage into userspace */
1056 memset(ps3fb.xdr_ea, 0, ps3fb_videomemory.size);
1058 /* The GPU command buffer is at the end of video memory */
1059 ps3fb.xdr_size = ps3fb_videomemory.size - GPU_CMD_BUF_SIZE;
1061 retval = ps3fb_xdr_settings(xdr_lpar, &dev->core);
1065 info = framebuffer_alloc(sizeof(u32) * 16, &dev->core);
1069 offset = VFB_OFF(ps3fb.res_index);
1070 info->screen_base = (char __iomem *)ps3fb.xdr_ea + offset;
1071 info->fbops = &ps3fb_ops;
1073 info->fix = ps3fb_fix;
1074 info->fix.smem_start = virt_to_abs(ps3fb.xdr_ea) + offset;
1075 info->fix.smem_len = ps3fb.xdr_size - offset;
1076 info->pseudo_palette = info->par;
1078 info->flags = FBINFO_DEFAULT | FBINFO_READS_FAST;
1080 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1082 goto err_framebuffer_release;
1084 if (!fb_find_mode(&info->var, info, mode_option, ps3fb_modedb,
1085 ARRAY_SIZE(ps3fb_modedb), ps3fb_default_mode(), 32)) {
1087 goto err_fb_dealloc;
1090 fb_videomode_to_modelist(ps3fb_modedb, ARRAY_SIZE(ps3fb_modedb),
1093 retval = register_framebuffer(info);
1095 goto err_fb_dealloc;
1097 dev->core.driver_data = info;
1099 dev_info(info->device, "%s %s, using %lu KiB of video memory\n",
1100 dev_driver_string(info->dev), info->dev->bus_id,
1101 ps3fb.xdr_size >> 10);
1103 task = kthread_run(ps3fbd, info, DEVICE_NAME);
1105 retval = PTR_ERR(task);
1106 goto err_unregister_framebuffer;
1110 ps3av_register_flip_ctl(ps3fb_flip_ctl, &ps3fb);
1114 err_unregister_framebuffer:
1115 unregister_framebuffer(info);
1117 fb_dealloc_cmap(&info->cmap);
1118 err_framebuffer_release:
1119 framebuffer_release(info);
1121 free_irq(ps3fb.irq_no, dev);
1122 ps3_irq_plug_destroy(ps3fb.irq_no);
1124 iounmap((u8 __iomem *)ps3fb.dinfo);
1125 err_gpu_context_free:
1126 lv1_gpu_context_free(ps3fb.context_handle);
1127 err_gpu_memory_free:
1128 lv1_gpu_memory_free(ps3fb.memory_handle);
1133 static int ps3fb_shutdown(struct ps3_system_bus_device *dev)
1136 struct fb_info *info = dev->core.driver_data;
1138 dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
1140 ps3fb_flip_ctl(0, &ps3fb); /* flip off */
1141 ps3fb.dinfo->irq.mask = 0;
1144 unregister_framebuffer(info);
1145 fb_dealloc_cmap(&info->cmap);
1146 framebuffer_release(info);
1149 ps3av_register_flip_ctl(NULL, NULL);
1151 struct task_struct *task = ps3fb.task;
1156 free_irq(ps3fb.irq_no, dev);
1157 ps3_irq_plug_destroy(ps3fb.irq_no);
1159 iounmap((u8 __iomem *)ps3fb.dinfo);
1161 status = lv1_gpu_context_free(ps3fb.context_handle);
1163 dev_dbg(&dev->core, "lv1_gpu_context_free failed: %d\n",
1166 status = lv1_gpu_memory_free(ps3fb.memory_handle);
1168 dev_dbg(&dev->core, "lv1_gpu_memory_free failed: %d\n",
1171 ps3_close_hv_device(dev);
1172 dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
1177 static struct ps3_system_bus_driver ps3fb_driver = {
1178 .match_id = PS3_MATCH_ID_GRAPHICS,
1179 .core.name = DEVICE_NAME,
1180 .core.owner = THIS_MODULE,
1181 .probe = ps3fb_probe,
1182 .remove = ps3fb_shutdown,
1183 .shutdown = ps3fb_shutdown,
1186 static int __init ps3fb_setup(void)
1194 if (fb_get_options(DEVICE_NAME, &options))
1197 if (!options || !*options)
1201 char *this_opt = strsep(&options, ",");
1207 if (!strncmp(this_opt, "mode:", 5))
1208 ps3fb_mode = simple_strtoul(this_opt + 5, NULL, 0);
1210 mode_option = this_opt;
1215 static int __init ps3fb_init(void)
1217 if (!ps3fb_videomemory.address || ps3fb_setup())
1220 return ps3_system_bus_driver_register(&ps3fb_driver);
1223 static void __exit ps3fb_exit(void)
1225 pr_debug(" -> %s:%d\n", __func__, __LINE__);
1226 ps3_system_bus_driver_unregister(&ps3fb_driver);
1227 pr_debug(" <- %s:%d\n", __func__, __LINE__);
1230 module_init(ps3fb_init);
1231 module_exit(ps3fb_exit);
1233 MODULE_LICENSE("GPL");
1234 MODULE_DESCRIPTION("PS3 GPU Frame Buffer Driver");
1235 MODULE_AUTHOR("Sony Computer Entertainment Inc.");
1236 MODULE_ALIAS(PS3_MODULE_ALIAS_GRAPHICS);