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/tty.h>
26 #include <linux/slab.h>
27 #include <linux/vmalloc.h>
28 #include <linux/delay.h>
29 #include <linux/interrupt.h>
30 #include <linux/platform_device.h>
31 #include <linux/console.h>
32 #include <linux/ioctl.h>
33 #include <linux/notifier.h>
34 #include <linux/reboot.h>
35 #include <linux/kthread.h>
36 #include <linux/freezer.h>
38 #include <asm/uaccess.h>
40 #include <linux/init.h>
43 #include <asm/abs_addr.h>
44 #include <asm/lv1call.h>
45 #include <asm/ps3av.h>
46 #include <asm/ps3fb.h>
50 #define DPRINTK(fmt, args...) printk("%s: " fmt, __func__ , ##args)
52 #define DPRINTK(fmt, args...)
55 #define L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC 0x101
56 #define L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP 0x102
57 #define L1GPU_CONTEXT_ATTRIBUTE_FB_SETUP 0x600
58 #define L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT 0x601
59 #define L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT_SYNC 0x602
61 #define L1GPU_FB_BLIT_WAIT_FOR_COMPLETION (1ULL << 32)
63 #define L1GPU_DISPLAY_SYNC_HSYNC 1
64 #define L1GPU_DISPLAY_SYNC_VSYNC 2
66 #define DDR_SIZE (0) /* used no ddr */
67 #define GPU_OFFSET (64 * 1024)
68 #define GPU_IOIF (0x0d000000UL)
70 #define PS3FB_FULL_MODE_BIT 0x80
72 #define GPU_INTR_STATUS_VSYNC_0 0 /* vsync on head A */
73 #define GPU_INTR_STATUS_VSYNC_1 1 /* vsync on head B */
74 #define GPU_INTR_STATUS_FLIP_0 3 /* flip head A */
75 #define GPU_INTR_STATUS_FLIP_1 4 /* flip head B */
76 #define GPU_INTR_STATUS_QUEUE_0 5 /* queue head A */
77 #define GPU_INTR_STATUS_QUEUE_1 6 /* queue head B */
79 #define GPU_DRIVER_INFO_VERSION 0x211
113 struct gpu_driver_info {
117 u32 hardware_channel;
119 u32 nvcore_frequency;
120 u32 memory_frequency;
123 struct display_head display_head[8];
131 u64 context_handle, memory_handle;
133 struct gpu_driver_info *dinfo;
136 u64 vblank_count; /* frame count */
137 wait_queue_head_t wait_vsync;
139 u32 num_frames; /* num of frame buffers */
140 atomic_t ext_flip; /* on/off flip with vsync */
141 atomic_t f_count; /* fb_open count */
144 struct task_struct *task;
146 static struct ps3fb_priv ps3fb;
148 struct ps3fb_res_table {
155 #define PS3FB_RES_FULL 1
156 static const struct ps3fb_res_table ps3fb_res[] = {
157 /* res_x,y margin_x,y full */
158 { 720, 480, 72, 48 , 0},
159 { 720, 576, 72, 58 , 0},
160 { 1280, 720, 78, 38 , 0},
161 { 1920, 1080, 116, 58 , 0},
163 { 720, 480, 0, 0 , PS3FB_RES_FULL},
164 { 720, 576, 0, 0 , PS3FB_RES_FULL},
165 { 1280, 720, 0, 0 , PS3FB_RES_FULL},
166 { 1920, 1080, 0, 0 , PS3FB_RES_FULL},
167 /* vesa: normally full mode */
168 { 1280, 768, 0, 0 , 0},
169 { 1280, 1024, 0, 0 , 0},
170 { 1920, 1200, 0, 0 , 0},
173 /* default resolution */
174 #define GPU_RES_INDEX 0 /* 720 x 480 */
176 static const struct fb_videomode ps3fb_modedb[] = {
177 /* 60 Hz broadcast modes (modes "1" to "5") */
180 "480i", 60, 576, 384, 74074, 130, 89, 78, 57, 63, 6,
181 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
184 "480p", 60, 576, 384, 37037, 130, 89, 78, 57, 63, 6,
185 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
188 "720p", 60, 1124, 644, 13481, 298, 148, 57, 44, 80, 5,
189 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
192 "1080i", 60, 1688, 964, 13481, 264, 160, 94, 62, 88, 5,
193 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
196 "1080p", 60, 1688, 964, 6741, 264, 160, 94, 62, 88, 5,
197 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
200 /* 50 Hz broadcast modes (modes "6" to "10") */
203 "576i", 50, 576, 460, 74074, 142, 83, 97, 63, 63, 5,
204 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
207 "576p", 50, 576, 460, 37037, 142, 83, 97, 63, 63, 5,
208 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
211 "720p", 50, 1124, 644, 13468, 298, 478, 57, 44, 80, 5,
212 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
215 "1080i", 50, 1688, 964, 13468, 264, 600, 94, 62, 88, 5,
216 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
219 "1080p", 50, 1688, 964, 6734, 264, 600, 94, 62, 88, 5,
220 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
223 /* VESA modes (modes "11" to "13") */
226 "wxga", 60, 1280, 768, 12924, 160, 24, 29, 3, 136, 6,
227 0, FB_VMODE_NONINTERLACED,
231 "sxga", 60, 1280, 1024, 9259, 248, 48, 38, 1, 112, 3,
232 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED,
236 "wuxga", 60, 1920, 1200, 6494, 80, 48, 26, 3, 32, 6,
237 FB_SYNC_HOR_HIGH_ACT, FB_VMODE_NONINTERLACED,
241 /* 60 Hz broadcast modes (full resolution versions of modes "1" to "5") */
244 "480if", 60, 720, 480, 74074, 58, 17, 30, 9, 63, 6,
245 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
248 "480pf", 60, 720, 480, 37037, 58, 17, 30, 9, 63, 6,
249 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
252 "720pf", 60, 1280, 720, 13481, 220, 70, 19, 6, 80, 5,
253 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
256 "1080if", 60, 1920, 1080, 13481, 148, 44, 36, 4, 88, 5,
257 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
260 "1080pf", 60, 1920, 1080, 6741, 148, 44, 36, 4, 88, 5,
261 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
264 /* 50 Hz broadcast modes (full resolution versions of modes "6" to "10") */
267 "576if", 50, 720, 576, 74074, 70, 11, 39, 5, 63, 5,
268 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
271 "576pf", 50, 720, 576, 37037, 70, 11, 39, 5, 63, 5,
272 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
275 "720pf", 50, 1280, 720, 13468, 220, 400, 19, 6, 80, 5,
276 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
279 "1080f", 50, 1920, 1080, 13468, 148, 484, 36, 4, 88, 5,
280 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
283 "1080pf", 50, 1920, 1080, 6734, 148, 484, 36, 4, 88, 5,
284 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
292 #define X_OFF(i) (ps3fb_res[i].xoff) /* left/right margin (pixel) */
293 #define Y_OFF(i) (ps3fb_res[i].yoff) /* top/bottom margin (pixel) */
294 #define WIDTH(i) (ps3fb_res[i].xres) /* width of FB */
295 #define HEIGHT(i) (ps3fb_res[i].yres) /* height of FB */
296 #define BPP 4 /* number of bytes per pixel */
297 #define VP_OFF(i) (WIDTH(i) * Y_OFF(i) * BPP + X_OFF(i) * BPP)
298 #define FB_OFF(i) (GPU_OFFSET - VP_OFF(i) % GPU_OFFSET)
300 static int ps3fb_mode;
301 module_param(ps3fb_mode, bool, 0);
303 static char *mode_option __initdata;
306 static int ps3fb_get_res_table(u32 xres, u32 yres)
312 full_mode = (ps3fb_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 DPRINTK("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 DPRINTK("ps3fb_find_mode: mode %u\n", mode);
364 DPRINTK("ps3fb_find_mode: mode not found\n");
369 static const struct fb_videomode *ps3fb_default_mode(void)
371 u32 mode = ps3fb_mode & PS3AV_MODE_MASK;
374 if (mode < 1 || mode > 13)
377 flags = ps3fb_mode & ~PS3AV_MODE_MASK;
379 if (mode <= 10 && flags & PS3FB_FULL_MODE_BIT) {
380 /* Full broadcast mode */
381 return &ps3fb_modedb[mode + 12];
384 return &ps3fb_modedb[mode - 1];
387 static int ps3fb_sync(u32 frame)
394 xres = ps3fb_res[i].xres;
395 yres = ps3fb_res[i].yres;
397 if (frame > ps3fb.num_frames - 1) {
398 printk(KERN_WARNING "%s: invalid frame number (%u)\n",
402 offset = xres * yres * BPP * frame;
404 fb_ioif = GPU_IOIF + FB_OFF(i) + offset;
405 status = lv1_gpu_context_attribute(ps3fb.context_handle,
406 L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT,
408 L1GPU_FB_BLIT_WAIT_FOR_COMPLETION |
410 xres * BPP); /* line_length */
413 "%s: lv1_gpu_context_attribute FB_BLIT failed: %d\n",
416 status = lv1_gpu_context_attribute(ps3fb.context_handle,
417 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
421 "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
425 status = lv1_gpu_context_attribute(ps3fb.context_handle,
426 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
430 "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
437 static int ps3fb_open(struct fb_info *info, int user)
439 atomic_inc(&ps3fb.f_count);
443 static int ps3fb_release(struct fb_info *info, int user)
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 */
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.
462 static int ps3fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
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);
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 DPRINTK("Virtual screen and panning are not supported\n");
494 var->xres_virtual = var->xres;
495 var->yres_virtual = var->yres;
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");
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;
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;
523 /* Rotation is not supported */
525 DPRINTK("Rotation is not supported\n");
530 i = ps3fb_get_res_table(var->xres, var->yres);
531 if (ps3fb_res[i].xres*ps3fb_res[i].yres*BPP > ps3fb_videomemory.size) {
532 DPRINTK("Not enough memory\n");
543 * This routine actually sets the video mode.
546 static int ps3fb_set_par(struct fb_info *info)
550 unsigned long offset;
551 static int first = 1;
553 DPRINTK("xres:%d xv:%d yres:%d yv:%d clock:%d\n",
554 info->var.xres, info->var.xres_virtual,
555 info->var.yres, info->var.yres_virtual, info->var.pixclock);
556 i = ps3fb_get_res_table(info->var.xres, info->var.yres);
559 mode = ps3fb_find_mode(&info->var, &info->fix.line_length);
563 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);
568 ps3fb.num_frames = ps3fb_videomemory.size/
569 (ps3fb_res[i].xres*ps3fb_res[i].yres*BPP);
571 /* Keep the special bits we cannot set using fb_var_screeninfo */
572 ps3fb_mode = (ps3fb_mode & ~PS3AV_MODE_MASK) | mode;
574 if (ps3av_set_video_mode(ps3fb_mode, first))
582 * Set a single color register. The values supplied are already
583 * rounded down to the hardware's capabilities (according to the
584 * entries in the var structure). Return != 0 for invalid regno.
587 static int ps3fb_setcolreg(unsigned int regno, unsigned int red,
588 unsigned int green, unsigned int blue,
589 unsigned int transp, struct fb_info *info)
599 ((u32 *)info->pseudo_palette)[regno] = transp << 24 | red << 16 |
605 * As we have a virtual frame buffer, we need our own mmap function
608 static int ps3fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
610 unsigned long size, offset;
613 i = ps3fb_get_res_table(info->var.xres, info->var.yres);
617 size = vma->vm_end - vma->vm_start;
618 offset = vma->vm_pgoff << PAGE_SHIFT;
619 if (offset + size > info->fix.smem_len)
622 offset += info->fix.smem_start + FB_OFF(i) + VP_OFF(i);
623 if (remap_pfn_range(vma, vma->vm_start, offset >> PAGE_SHIFT,
624 size, vma->vm_page_prot))
627 printk(KERN_DEBUG "ps3fb: mmap framebuffer P(%lx)->V(%lx)\n", offset,
636 static int ps3fb_blank(int blank, struct fb_info *info)
640 DPRINTK("%s: blank:%d\n", __func__, blank);
642 case FB_BLANK_POWERDOWN:
643 case FB_BLANK_HSYNC_SUSPEND:
644 case FB_BLANK_VSYNC_SUSPEND:
645 case FB_BLANK_NORMAL:
646 retval = ps3av_video_mute(1); /* mute on */
648 ps3fb.is_blanked = 1;
651 default: /* unblank */
652 retval = ps3av_video_mute(0); /* mute off */
654 ps3fb.is_blanked = 0;
660 static int ps3fb_get_vblank(struct fb_vblank *vblank)
662 memset(vblank, 0, sizeof(&vblank));
663 vblank->flags = FB_VBLANK_HAVE_VSYNC;
667 int ps3fb_wait_for_vsync(u32 crtc)
672 count = ps3fb.vblank_count;
673 ret = wait_event_interruptible_timeout(ps3fb.wait_vsync,
674 count != ps3fb.vblank_count,
682 EXPORT_SYMBOL_GPL(ps3fb_wait_for_vsync);
684 void ps3fb_flip_ctl(int on)
687 atomic_dec_if_positive(&ps3fb.ext_flip);
689 atomic_inc(&ps3fb.ext_flip);
692 EXPORT_SYMBOL_GPL(ps3fb_flip_ctl);
698 static int ps3fb_ioctl(struct fb_info *info, unsigned int cmd,
701 void __user *argp = (void __user *)arg;
703 int retval = -EFAULT;
708 struct fb_vblank vblank;
709 DPRINTK("FBIOGET_VBLANK:\n");
710 retval = ps3fb_get_vblank(&vblank);
714 if (copy_to_user(argp, &vblank, sizeof(vblank)))
719 case FBIO_WAITFORVSYNC:
722 DPRINTK("FBIO_WAITFORVSYNC:\n");
723 if (get_user(crt, (u32 __user *) arg))
726 retval = ps3fb_wait_for_vsync(crt);
730 case PS3FB_IOCTL_SETMODE:
732 const struct fb_videomode *mode;
733 struct fb_var_screeninfo var;
735 if (copy_from_user(&val, argp, sizeof(val)))
738 if (!(val & PS3AV_MODE_MASK)) {
739 u32 id = ps3av_get_auto_mode(0);
741 val = (val & ~PS3AV_MODE_MASK) | id;
743 DPRINTK("PS3FB_IOCTL_SETMODE:%x\n", val);
745 old_mode = ps3fb_mode;
747 mode = ps3fb_default_mode();
750 fb_videomode_to_var(&var, mode);
751 acquire_console_sem();
752 info->flags |= FBINFO_MISC_USEREVENT;
753 /* Force, in case only special bits changed */
754 var.activate |= FB_ACTIVATE_FORCE;
755 retval = fb_set_var(info, &var);
756 info->flags &= ~FBINFO_MISC_USEREVENT;
757 release_console_sem();
760 ps3fb_mode = old_mode;
764 case PS3FB_IOCTL_GETMODE:
765 val = ps3av_get_mode();
766 DPRINTK("PS3FB_IOCTL_GETMODE:%x\n", val);
767 if (!copy_to_user(argp, &val, sizeof(val)))
771 case PS3FB_IOCTL_SCREENINFO:
773 struct ps3fb_ioctl_res res;
774 int i = ps3fb.res_index;
775 DPRINTK("PS3FB_IOCTL_SCREENINFO:\n");
776 res.xres = ps3fb_res[i].xres;
777 res.yres = ps3fb_res[i].yres;
778 res.xoff = ps3fb_res[i].xoff;
779 res.yoff = ps3fb_res[i].yoff;
780 res.num_frames = ps3fb.num_frames;
781 if (!copy_to_user(argp, &res, sizeof(res)))
787 DPRINTK("PS3FB_IOCTL_ON:\n");
788 atomic_inc(&ps3fb.ext_flip);
792 case PS3FB_IOCTL_OFF:
793 DPRINTK("PS3FB_IOCTL_OFF:\n");
794 atomic_dec_if_positive(&ps3fb.ext_flip);
798 case PS3FB_IOCTL_FSEL:
799 if (copy_from_user(&val, argp, sizeof(val)))
802 DPRINTK("PS3FB_IOCTL_FSEL:%d\n", val);
803 retval = ps3fb_sync(val);
807 retval = -ENOIOCTLCMD;
813 static int ps3fbd(void *arg)
815 while (!kthread_should_stop()) {
817 set_current_state(TASK_INTERRUPTIBLE);
818 if (ps3fb.is_kicked) {
820 ps3fb_sync(0); /* single buffer */
827 static irqreturn_t ps3fb_vsync_interrupt(int irq, void *ptr)
831 struct display_head *head = &ps3fb.dinfo->display_head[1];
833 status = lv1_gpu_context_intr(ps3fb.context_handle, &v1);
835 printk(KERN_ERR "%s: lv1_gpu_context_intr failed: %d\n",
840 if (v1 & (1 << GPU_INTR_STATUS_VSYNC_1)) {
842 ps3fb.vblank_count = head->vblank_count;
843 if (ps3fb.task && !ps3fb.is_blanked &&
844 !atomic_read(&ps3fb.ext_flip)) {
846 wake_up_process(ps3fb.task);
848 wake_up_interruptible(&ps3fb.wait_vsync);
855 static int __init ps3fb_setup(char *options)
860 if (!options || !*options)
861 return 0; /* no options */
863 while ((this_opt = strsep(&options, ",")) != NULL) {
866 if (!strncmp(this_opt, "mode:", 5))
867 mode = simple_strtoul(this_opt + 5, NULL, 0);
869 mode_option = this_opt;
879 static void ps3fb_platform_release(struct device *device)
881 /* This is called when the reference count goes to zero. */
884 static int ps3fb_vsync_settings(struct gpu_driver_info *dinfo, void *dev)
888 DPRINTK("version_driver:%x\n", dinfo->version_driver);
889 DPRINTK("irq outlet:%x\n", dinfo->irq.irq_outlet);
890 DPRINTK("version_gpu:%x memory_size:%x ch:%x core_freq:%d mem_freq:%d\n",
891 dinfo->version_gpu, dinfo->memory_size, dinfo->hardware_channel,
892 dinfo->nvcore_frequency/1000000, dinfo->memory_frequency/1000000);
894 if (dinfo->version_driver != GPU_DRIVER_INFO_VERSION) {
895 printk(KERN_ERR "%s: version_driver err:%x\n", __func__,
896 dinfo->version_driver);
901 error = ps3_irq_plug_setup(PS3_BINDING_CPU_ANY, dinfo->irq.irq_outlet,
904 printk(KERN_ERR "%s: ps3_alloc_irq failed %d\n", __func__,
909 error = request_irq(ps3fb.irq_no, ps3fb_vsync_interrupt, IRQF_DISABLED,
910 "ps3fb vsync", ps3fb.dev);
912 printk(KERN_ERR "%s: request_irq failed %d\n", __func__,
914 ps3_irq_plug_destroy(ps3fb.irq_no);
918 dinfo->irq.mask = (1 << GPU_INTR_STATUS_VSYNC_1) |
919 (1 << GPU_INTR_STATUS_FLIP_1);
923 static int ps3fb_xdr_settings(u64 xdr_lpar)
927 status = lv1_gpu_context_iomap(ps3fb.context_handle, GPU_IOIF,
928 xdr_lpar, ps3fb_videomemory.size, 0);
930 printk(KERN_ERR "%s: lv1_gpu_context_iomap failed: %d\n",
934 DPRINTK("video:%p xdr_ea:%p ioif:%lx lpar:%lx phys:%lx size:%lx\n",
935 ps3fb_videomemory.address, ps3fb.xdr_ea, GPU_IOIF, xdr_lpar,
936 virt_to_abs(ps3fb.xdr_ea), ps3fb_videomemory.size);
938 status = lv1_gpu_context_attribute(ps3fb.context_handle,
939 L1GPU_CONTEXT_ATTRIBUTE_FB_SETUP,
940 xdr_lpar, ps3fb_videomemory.size,
944 "%s: lv1_gpu_context_attribute FB_SETUP failed: %d\n",
951 static struct fb_ops ps3fb_ops = {
952 .fb_open = ps3fb_open,
953 .fb_release = ps3fb_release,
954 .fb_read = fb_sys_read,
955 .fb_write = fb_sys_write,
956 .fb_check_var = ps3fb_check_var,
957 .fb_set_par = ps3fb_set_par,
958 .fb_setcolreg = ps3fb_setcolreg,
959 .fb_fillrect = sys_fillrect,
960 .fb_copyarea = sys_copyarea,
961 .fb_imageblit = sys_imageblit,
962 .fb_mmap = ps3fb_mmap,
963 .fb_blank = ps3fb_blank,
964 .fb_ioctl = ps3fb_ioctl,
965 .fb_compat_ioctl = ps3fb_ioctl
968 static struct fb_fix_screeninfo ps3fb_fix __initdata = {
970 .type = FB_TYPE_PACKED_PIXELS,
971 .visual = FB_VISUAL_TRUECOLOR,
972 .accel = FB_ACCEL_NONE,
975 static int __init ps3fb_probe(struct platform_device *dev)
977 struct fb_info *info;
978 int retval = -ENOMEM;
980 u64 lpar_dma_control = 0;
981 u64 lpar_driver_info = 0;
982 u64 lpar_reports = 0;
983 u64 lpar_reports_size = 0;
986 unsigned long offset;
987 struct task_struct *task;
989 /* get gpu context handle */
990 status = lv1_gpu_memory_allocate(DDR_SIZE, 0, 0, 0, 0,
991 &ps3fb.memory_handle, &ddr_lpar);
993 printk(KERN_ERR "%s: lv1_gpu_memory_allocate failed: %d\n",
997 DPRINTK("ddr:lpar:0x%lx\n", ddr_lpar);
999 status = lv1_gpu_context_allocate(ps3fb.memory_handle, 0,
1000 &ps3fb.context_handle,
1001 &lpar_dma_control, &lpar_driver_info,
1002 &lpar_reports, &lpar_reports_size);
1004 printk(KERN_ERR "%s: lv1_gpu_context_attribute failed: %d\n",
1006 goto err_gpu_memory_free;
1009 /* vsync interrupt */
1010 ps3fb.dinfo = ioremap(lpar_driver_info, 128 * 1024);
1012 printk(KERN_ERR "%s: ioremap failed\n", __func__);
1013 goto err_gpu_context_free;
1016 retval = ps3fb_vsync_settings(ps3fb.dinfo, dev);
1018 goto err_iounmap_dinfo;
1020 /* xdr frame buffer */
1021 ps3fb.xdr_ea = ps3fb_videomemory.address;
1022 xdr_lpar = ps3_mm_phys_to_lpar(__pa(ps3fb.xdr_ea));
1023 retval = ps3fb_xdr_settings(xdr_lpar);
1028 * ps3fb must clear memory to prevent kernel info
1029 * leakage into userspace
1031 memset(ps3fb.xdr_ea, 0, ps3fb_videomemory.size);
1032 info = framebuffer_alloc(sizeof(u32) * 16, &dev->dev);
1036 offset = FB_OFF(ps3fb.res_index) + VP_OFF(ps3fb.res_index);
1037 info->screen_base = (char __iomem *)ps3fb.xdr_ea + offset;
1038 info->fbops = &ps3fb_ops;
1040 info->fix = ps3fb_fix;
1041 info->fix.smem_start = virt_to_abs(ps3fb.xdr_ea);
1042 info->fix.smem_len = ps3fb_videomemory.size - offset;
1043 info->pseudo_palette = info->par;
1045 info->flags = FBINFO_FLAG_DEFAULT;
1047 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1049 goto err_framebuffer_release;
1051 if (!fb_find_mode(&info->var, info, mode_option, ps3fb_modedb,
1052 ARRAY_SIZE(ps3fb_modedb), ps3fb_default_mode(), 32)) {
1054 goto err_fb_dealloc;
1057 fb_videomode_to_modelist(ps3fb_modedb, ARRAY_SIZE(ps3fb_modedb),
1060 retval = register_framebuffer(info);
1062 goto err_fb_dealloc;
1064 platform_set_drvdata(dev, info);
1067 "fb%d: PS3 frame buffer device, using %ld KiB of video memory\n",
1068 info->node, ps3fb_videomemory.size >> 10);
1070 task = kthread_run(ps3fbd, info, "ps3fbd");
1072 retval = PTR_ERR(task);
1073 goto err_unregister_framebuffer;
1080 err_unregister_framebuffer:
1081 unregister_framebuffer(info);
1083 fb_dealloc_cmap(&info->cmap);
1084 err_framebuffer_release:
1085 framebuffer_release(info);
1087 free_irq(ps3fb.irq_no, ps3fb.dev);
1088 ps3_irq_plug_destroy(ps3fb.irq_no);
1090 iounmap((u8 __iomem *)ps3fb.dinfo);
1091 err_gpu_context_free:
1092 lv1_gpu_context_free(ps3fb.context_handle);
1093 err_gpu_memory_free:
1094 lv1_gpu_memory_free(ps3fb.memory_handle);
1099 static void ps3fb_shutdown(struct platform_device *dev)
1101 ps3fb_flip_ctl(0); /* flip off */
1102 ps3fb.dinfo->irq.mask = 0;
1103 free_irq(ps3fb.irq_no, ps3fb.dev);
1104 ps3_irq_plug_destroy(ps3fb.irq_no);
1105 iounmap((u8 __iomem *)ps3fb.dinfo);
1108 void ps3fb_cleanup(void)
1113 struct task_struct *task = ps3fb.task;
1118 free_irq(ps3fb.irq_no, ps3fb.dev);
1119 ps3_irq_plug_destroy(ps3fb.irq_no);
1121 iounmap((u8 __iomem *)ps3fb.dinfo);
1123 status = lv1_gpu_context_free(ps3fb.context_handle);
1125 DPRINTK("lv1_gpu_context_free failed: %d\n", status);
1127 status = lv1_gpu_memory_free(ps3fb.memory_handle);
1129 DPRINTK("lv1_gpu_memory_free failed: %d\n", status);
1134 EXPORT_SYMBOL_GPL(ps3fb_cleanup);
1136 static int ps3fb_remove(struct platform_device *dev)
1138 struct fb_info *info = platform_get_drvdata(dev);
1141 unregister_framebuffer(info);
1142 fb_dealloc_cmap(&info->cmap);
1143 framebuffer_release(info);
1149 static struct platform_driver ps3fb_driver = {
1150 .probe = ps3fb_probe,
1151 .remove = ps3fb_remove,
1152 .shutdown = ps3fb_shutdown,
1153 .driver = { .name = "ps3fb" }
1156 static struct platform_device ps3fb_device = {
1159 .dev = { .release = ps3fb_platform_release }
1162 int ps3fb_set_sync(void)
1167 status = lv1_gpu_context_attribute(0x0,
1168 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
1169 0, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);
1172 "%s: lv1_gpu_context_attribute DISPLAY_SYNC failed: %d\n",
1178 status = lv1_gpu_context_attribute(0x0,
1179 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
1180 1, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);
1184 "%s: lv1_gpu_context_attribute DISPLAY_MODE failed: %d\n",
1192 EXPORT_SYMBOL_GPL(ps3fb_set_sync);
1194 static int __init ps3fb_init(void)
1199 char *option = NULL;
1201 if (fb_get_options("ps3fb", &option))
1205 if (!ps3fb_videomemory.address)
1208 error = ps3av_dev_open();
1210 printk(KERN_ERR "%s: ps3av_dev_open failed\n", __func__);
1214 ps3fb_mode = ps3av_get_mode();
1215 DPRINTK("ps3av_mode:%d\n", ps3fb_mode);
1217 mode = ps3fb_setup(option); /* check boot option */
1221 if (ps3fb_mode > 0) {
1223 ps3av_video_mode2res(ps3fb_mode, &xres, &yres);
1224 ps3fb.res_index = ps3fb_get_res_table(xres, yres);
1225 DPRINTK("res_index:%d\n", ps3fb.res_index);
1227 ps3fb.res_index = GPU_RES_INDEX;
1229 atomic_set(&ps3fb.f_count, -1); /* fbcon opens ps3fb */
1230 atomic_set(&ps3fb.ext_flip, 0); /* for flip with vsync */
1231 init_waitqueue_head(&ps3fb.wait_vsync);
1232 ps3fb.num_frames = 1;
1234 error = platform_driver_register(&ps3fb_driver);
1236 error = platform_device_register(&ps3fb_device);
1238 platform_driver_unregister(&ps3fb_driver);
1249 module_init(ps3fb_init);
1252 static void __exit ps3fb_exit(void)
1254 platform_device_unregister(&ps3fb_device);
1255 platform_driver_unregister(&ps3fb_driver);
1258 module_exit(ps3fb_exit);
1260 MODULE_LICENSE("GPL");