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>
36 #include <asm/uaccess.h>
38 #include <linux/init.h>
41 #include <asm/abs_addr.h>
42 #include <asm/lv1call.h>
43 #include <asm/ps3av.h>
44 #include <asm/ps3fb.h>
48 #define DPRINTK(fmt, args...) printk("%s: " fmt, __FUNCTION__ , ##args)
50 #define DPRINTK(fmt, args...)
53 #define L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC 0x101
54 #define L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP 0x102
55 #define L1GPU_CONTEXT_ATTRIBUTE_FB_SETUP 0x600
56 #define L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT 0x601
57 #define L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT_SYNC 0x602
59 #define L1GPU_FB_BLIT_WAIT_FOR_COMPLETION (1ULL << 32)
61 #define L1GPU_DISPLAY_SYNC_HSYNC 1
62 #define L1GPU_DISPLAY_SYNC_VSYNC 2
64 #define DDR_SIZE (0) /* used no ddr */
65 #define GPU_OFFSET (64 * 1024)
66 #define GPU_IOIF (0x0d000000UL)
68 #define PS3FB_FULL_MODE_BIT 0x80
70 #define GPU_INTR_STATUS_VSYNC_0 0 /* vsync on head A */
71 #define GPU_INTR_STATUS_VSYNC_1 1 /* vsync on head B */
72 #define GPU_INTR_STATUS_FLIP_0 3 /* flip head A */
73 #define GPU_INTR_STATUS_FLIP_1 4 /* flip head B */
74 #define GPU_INTR_STATUS_QUEUE_0 5 /* queue head A */
75 #define GPU_INTR_STATUS_QUEUE_1 6 /* queue head B */
77 #define GPU_DRIVER_INFO_VERSION 0x211
111 struct gpu_driver_info {
115 u32 hardware_channel;
117 u32 nvcore_frequency;
118 u32 memory_frequency;
121 struct display_head display_head[8];
129 u64 context_handle, memory_handle;
131 struct gpu_driver_info *dinfo;
132 struct semaphore sem;
135 u64 vblank_count; /* frame count */
136 wait_queue_head_t wait_vsync;
138 u32 num_frames; /* num of frame buffers */
139 atomic_t ext_flip; /* on/off flip with vsync */
140 atomic_t f_count; /* fb_open count */
143 static struct ps3fb_priv ps3fb;
145 struct ps3fb_res_table {
152 #define PS3FB_RES_FULL 1
153 static const struct ps3fb_res_table ps3fb_res[] = {
154 /* res_x,y margin_x,y full */
155 { 720, 480, 72, 48 , 0},
156 { 720, 576, 72, 58 , 0},
157 { 1280, 720, 78, 38 , 0},
158 { 1920, 1080, 116, 58 , 0},
160 { 720, 480, 0, 0 , PS3FB_RES_FULL},
161 { 720, 576, 0, 0 , PS3FB_RES_FULL},
162 { 1280, 720, 0, 0 , PS3FB_RES_FULL},
163 { 1920, 1080, 0, 0 , PS3FB_RES_FULL},
164 /* vesa: normally full mode */
165 { 1280, 768, 0, 0 , 0},
166 { 1280, 1024, 0, 0 , 0},
167 { 1920, 1200, 0, 0 , 0},
170 /* default resolution */
171 #define GPU_RES_INDEX 0 /* 720 x 480 */
173 static const struct fb_videomode ps3fb_modedb[] = {
174 /* 60 Hz broadcast modes (modes "1" to "5") */
177 "480i", 60, 576, 384, 74074, 130, 89, 78, 57, 63, 6,
178 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
181 "480p", 60, 576, 384, 37037, 130, 89, 78, 57, 63, 6,
182 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
185 "720p", 60, 1124, 644, 13481, 298, 148, 57, 44, 80, 5,
186 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
189 "1080i", 60, 1688, 964, 13481, 264, 160, 94, 62, 88, 5,
190 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
193 "1080p", 60, 1688, 964, 6741, 264, 160, 94, 62, 88, 5,
194 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
197 /* 50 Hz broadcast modes (modes "6" to "10") */
200 "576i", 50, 576, 460, 74074, 142, 83, 97, 63, 63, 5,
201 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
204 "576p", 50, 576, 460, 37037, 142, 83, 97, 63, 63, 5,
205 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
208 "720p", 50, 1124, 644, 13468, 298, 478, 57, 44, 80, 5,
209 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
212 "1080i", 50, 1688, 964, 13468, 264, 600, 94, 62, 88, 5,
213 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
216 "1080p", 50, 1688, 964, 6734, 264, 600, 94, 62, 88, 5,
217 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
220 /* VESA modes (modes "11" to "13") */
223 "wxga", 60, 1280, 768, 12924, 160, 24, 29, 3, 136, 6,
224 0, FB_VMODE_NONINTERLACED,
228 "sxga", 60, 1280, 1024, 9259, 248, 48, 38, 1, 112, 3,
229 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED,
233 "wuxga", 60, 1920, 1200, 6494, 80, 48, 26, 3, 32, 6,
234 FB_SYNC_HOR_HIGH_ACT, FB_VMODE_NONINTERLACED,
238 /* 60 Hz broadcast modes (full resolution versions of modes "1" to "5") */
241 "480if", 60, 720, 480, 74074, 58, 17, 30, 9, 63, 6,
242 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
245 "480pf", 60, 720, 480, 37037, 58, 17, 30, 9, 63, 6,
246 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
249 "720pf", 60, 1280, 720, 13481, 220, 70, 19, 6, 80, 5,
250 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
253 "1080if", 60, 1920, 1080, 13481, 148, 44, 36, 4, 88, 5,
254 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
257 "1080pf", 60, 1920, 1080, 6741, 148, 44, 36, 4, 88, 5,
258 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
261 /* 50 Hz broadcast modes (full resolution versions of modes "6" to "10") */
264 "576if", 50, 720, 576, 74074, 70, 11, 39, 5, 63, 5,
265 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
268 "576pf", 50, 720, 576, 37037, 70, 11, 39, 5, 63, 5,
269 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
272 "720pf", 50, 1280, 720, 13468, 220, 400, 19, 6, 80, 5,
273 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
276 "1080f", 50, 1920, 1080, 13468, 148, 484, 36, 4, 88, 5,
277 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
280 "1080pf", 50, 1920, 1080, 6734, 148, 484, 36, 4, 88, 5,
281 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
289 #define X_OFF(i) (ps3fb_res[i].xoff) /* left/right margin (pixel) */
290 #define Y_OFF(i) (ps3fb_res[i].yoff) /* top/bottom margin (pixel) */
291 #define WIDTH(i) (ps3fb_res[i].xres) /* width of FB */
292 #define HEIGHT(i) (ps3fb_res[i].yres) /* height of FB */
293 #define BPP 4 /* number of bytes per pixel */
294 #define VP_OFF(i) (WIDTH(i) * Y_OFF(i) * BPP + X_OFF(i) * BPP)
295 #define FB_OFF(i) (GPU_OFFSET - VP_OFF(i) % GPU_OFFSET)
297 static int ps3fb_mode = 0;
298 module_param(ps3fb_mode, bool, 0);
300 static char *mode_option __initdata = NULL;
303 static int ps3fb_get_res_table(u32 xres, u32 yres)
309 full_mode = (ps3fb_mode & PS3FB_FULL_MODE_BIT) ? PS3FB_RES_FULL : 0;
311 x = ps3fb_res[i].xres;
312 y = ps3fb_res[i].yres;
313 f = ps3fb_res[i].type;
316 DPRINTK("ERROR: ps3fb_get_res_table()\n");
320 if (full_mode == PS3FB_RES_FULL && f != PS3FB_RES_FULL)
323 if (x == xres && (yres == 0 || y == yres))
326 x = x - 2 * ps3fb_res[i].xoff;
327 y = y - 2 * ps3fb_res[i].yoff;
328 if (x == xres && (yres == 0 || y == yres))
334 static unsigned int ps3fb_find_mode(const struct fb_var_screeninfo *var,
337 unsigned int i, mode;
339 for (i = 0; i < ARRAY_SIZE(ps3fb_modedb); i++)
340 if (var->xres == ps3fb_modedb[i].xres &&
341 var->yres == ps3fb_modedb[i].yres &&
342 var->pixclock == ps3fb_modedb[i].pixclock &&
343 var->hsync_len == ps3fb_modedb[i].hsync_len &&
344 var->vsync_len == ps3fb_modedb[i].vsync_len &&
345 var->left_margin == ps3fb_modedb[i].left_margin &&
346 var->right_margin == ps3fb_modedb[i].right_margin &&
347 var->upper_margin == ps3fb_modedb[i].upper_margin &&
348 var->lower_margin == ps3fb_modedb[i].lower_margin &&
349 var->sync == ps3fb_modedb[i].sync &&
350 (var->vmode & FB_VMODE_MASK) == ps3fb_modedb[i].vmode) {
351 /* Cropped broadcast modes use the full line_length */
353 ps3fb_modedb[i < 10 ? i + 13 : i].xres * 4;
354 /* Full broadcast modes have the full mode bit set */
355 mode = i > 12 ? (i - 12) | PS3FB_FULL_MODE_BIT : i + 1;
357 DPRINTK("ps3fb_find_mode: mode %u\n", mode);
361 DPRINTK("ps3fb_find_mode: mode not found\n");
366 static const struct fb_videomode *ps3fb_default_mode(void)
368 u32 mode = ps3fb_mode & PS3AV_MODE_MASK;
371 if (mode < 1 || mode > 13)
374 flags = ps3fb_mode & ~PS3AV_MODE_MASK;
376 if (mode <= 10 && flags & PS3FB_FULL_MODE_BIT) {
377 /* Full broadcast mode */
378 return &ps3fb_modedb[mode + 12];
381 return &ps3fb_modedb[mode - 1];
384 static int ps3fb_sync(u32 frame)
391 xres = ps3fb_res[i].xres;
392 yres = ps3fb_res[i].yres;
394 if (frame > ps3fb.num_frames - 1) {
395 printk(KERN_WARNING "%s: invalid frame number (%u)\n",
396 __FUNCTION__, frame);
399 offset = xres * yres * BPP * frame;
401 fb_ioif = GPU_IOIF + FB_OFF(i) + offset;
402 status = lv1_gpu_context_attribute(ps3fb.context_handle,
403 L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT,
405 L1GPU_FB_BLIT_WAIT_FOR_COMPLETION |
407 xres * BPP); /* line_length */
409 printk(KERN_ERR "%s: lv1_gpu_context_attribute FB_BLIT failed: %d\n",
410 __FUNCTION__, status);
412 status = lv1_gpu_context_attribute(ps3fb.context_handle,
413 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
416 printk(KERN_ERR "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
417 __FUNCTION__, status);
420 status = lv1_gpu_context_attribute(ps3fb.context_handle,
421 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
424 printk(KERN_ERR "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
425 __FUNCTION__, status);
431 static int ps3fb_open(struct fb_info *info, int user)
433 atomic_inc(&ps3fb.f_count);
437 static int ps3fb_release(struct fb_info *info, int user)
439 if (atomic_dec_and_test(&ps3fb.f_count)) {
440 if (atomic_read(&ps3fb.ext_flip)) {
441 atomic_set(&ps3fb.ext_flip, 0);
442 ps3fb_sync(0); /* single buffer */
449 * Setting the video mode has been split into two parts.
450 * First part, xxxfb_check_var, must not write anything
451 * to hardware, it should only verify and adjust var.
452 * This means it doesn't alter par but it does use hardware
453 * data from it to check this var.
456 static int ps3fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
462 DPRINTK("var->xres:%u info->var.xres:%u\n", var->xres, info->var.xres);
463 DPRINTK("var->yres:%u info->var.yres:%u\n", var->yres, info->var.yres);
465 /* FIXME For now we do exact matches only */
466 mode = ps3fb_find_mode(var, &line_length);
471 * FB_VMODE_CONUPDATE and FB_VMODE_SMOOTH_XPAN are equal!
472 * as FB_VMODE_SMOOTH_XPAN is only used internally
475 if (var->vmode & FB_VMODE_CONUPDATE) {
476 var->vmode |= FB_VMODE_YWRAP;
477 var->xoffset = info->var.xoffset;
478 var->yoffset = info->var.yoffset;
481 /* Virtual screen and panning are not supported */
482 if (var->xres_virtual > var->xres || var->yres_virtual > var->yres ||
483 var->xoffset || var->yoffset) {
484 DPRINTK("Virtual screen and panning are not supported\n");
488 var->xres_virtual = var->xres;
489 var->yres_virtual = var->yres;
491 /* We support ARGB8888 only */
492 if (var->bits_per_pixel > 32 || var->grayscale ||
493 var->red.offset > 16 || var->green.offset > 8 ||
494 var->blue.offset > 0 || var->transp.offset > 24 ||
495 var->red.length > 8 || var->green.length > 8 ||
496 var->blue.length > 8 || var->transp.length > 8 ||
497 var->red.msb_right || var->green.msb_right ||
498 var->blue.msb_right || var->transp.msb_right || var->nonstd) {
499 DPRINTK("We support ARGB8888 only\n");
503 var->bits_per_pixel = 32;
504 var->red.offset = 16;
505 var->green.offset = 8;
506 var->blue.offset = 0;
507 var->transp.offset = 24;
509 var->green.length = 8;
510 var->blue.length = 8;
511 var->transp.length = 8;
512 var->red.msb_right = 0;
513 var->green.msb_right = 0;
514 var->blue.msb_right = 0;
515 var->transp.msb_right = 0;
517 /* Rotation is not supported */
519 DPRINTK("Rotation is not supported\n");
524 i = ps3fb_get_res_table(var->xres, var->yres);
525 if (ps3fb_res[i].xres*ps3fb_res[i].yres*BPP > ps3fb_videomemory.size) {
526 DPRINTK("Not enough memory\n");
537 * This routine actually sets the video mode.
540 static int ps3fb_set_par(struct fb_info *info)
544 unsigned long offset;
545 static int first = 1;
547 DPRINTK("xres:%d xv:%d yres:%d yv:%d clock:%d\n",
548 info->var.xres, info->var.xres_virtual,
549 info->var.yres, info->var.yres_virtual, info->var.pixclock);
550 i = ps3fb_get_res_table(info->var.xres, info->var.yres);
553 mode = ps3fb_find_mode(&info->var, &info->fix.line_length);
557 offset = FB_OFF(i) + VP_OFF(i);
558 info->fix.smem_len = ps3fb_videomemory.size - offset;
559 info->screen_base = (char __iomem *)ps3fb.xdr_ea + offset;
560 memset(ps3fb.xdr_ea, 0, ps3fb_videomemory.size);
562 ps3fb.num_frames = ps3fb_videomemory.size/
563 (ps3fb_res[i].xres*ps3fb_res[i].yres*BPP);
565 /* Keep the special bits we cannot set using fb_var_screeninfo */
566 ps3fb_mode = (ps3fb_mode & ~PS3AV_MODE_MASK) | mode;
568 if (ps3av_set_video_mode(ps3fb_mode, first))
576 * Set a single color register. The values supplied are already
577 * rounded down to the hardware's capabilities (according to the
578 * entries in the var structure). Return != 0 for invalid regno.
581 static int ps3fb_setcolreg(unsigned int regno, unsigned int red,
582 unsigned int green, unsigned int blue,
583 unsigned int transp, struct fb_info *info)
593 ((u32 *)info->pseudo_palette)[regno] = transp << 24 | red << 16 |
599 * As we have a virtual frame buffer, we need our own mmap function
602 static int ps3fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
604 unsigned long size, offset;
607 i = ps3fb_get_res_table(info->var.xres, info->var.yres);
611 size = vma->vm_end - vma->vm_start;
612 offset = vma->vm_pgoff << PAGE_SHIFT;
613 if (offset + size > info->fix.smem_len)
616 offset += info->fix.smem_start + FB_OFF(i) + VP_OFF(i);
617 if (remap_pfn_range(vma, vma->vm_start, offset >> PAGE_SHIFT,
618 size, vma->vm_page_prot))
621 printk(KERN_DEBUG "ps3fb: mmap framebuffer P(%lx)->V(%lx)\n", offset,
630 static int ps3fb_blank(int blank, struct fb_info *info)
634 DPRINTK("%s: blank:%d\n", __FUNCTION__, blank);
636 case FB_BLANK_POWERDOWN:
637 case FB_BLANK_HSYNC_SUSPEND:
638 case FB_BLANK_VSYNC_SUSPEND:
639 case FB_BLANK_NORMAL:
640 retval = ps3av_video_mute(1); /* mute on */
642 ps3fb.is_blanked = 1;
645 default: /* unblank */
646 retval = ps3av_video_mute(0); /* mute off */
648 ps3fb.is_blanked = 0;
654 static int ps3fb_get_vblank(struct fb_vblank *vblank)
656 memset(vblank, 0, sizeof(&vblank));
657 vblank->flags = FB_VBLANK_HAVE_VSYNC;
661 int ps3fb_wait_for_vsync(u32 crtc)
666 count = ps3fb.vblank_count;
667 ret = wait_event_interruptible_timeout(ps3fb.wait_vsync,
668 count != ps3fb.vblank_count,
676 EXPORT_SYMBOL_GPL(ps3fb_wait_for_vsync);
678 void ps3fb_flip_ctl(int on)
681 if (atomic_read(&ps3fb.ext_flip) > 0) {
682 atomic_dec(&ps3fb.ext_flip);
685 atomic_inc(&ps3fb.ext_flip);
689 EXPORT_SYMBOL_GPL(ps3fb_flip_ctl);
695 static int ps3fb_ioctl(struct fb_info *info, unsigned int cmd,
698 void __user *argp = (void __user *)arg;
700 int retval = -EFAULT;
705 struct fb_vblank vblank;
706 DPRINTK("FBIOGET_VBLANK:\n");
707 retval = ps3fb_get_vblank(&vblank);
711 if (copy_to_user(argp, &vblank, sizeof(vblank)))
716 case FBIO_WAITFORVSYNC:
719 DPRINTK("FBIO_WAITFORVSYNC:\n");
720 if (get_user(crt, (u32 __user *) arg))
723 retval = ps3fb_wait_for_vsync(crt);
727 case PS3FB_IOCTL_SETMODE:
729 const struct fb_videomode *mode;
730 struct fb_var_screeninfo var;
732 if (copy_from_user(&val, argp, sizeof(val)))
735 DPRINTK("PS3FB_IOCTL_SETMODE:%x\n", val);
737 old_mode = ps3fb_mode;
739 mode = ps3fb_default_mode();
742 fb_videomode_to_var(&var, mode);
743 acquire_console_sem();
744 info->flags |= FBINFO_MISC_USEREVENT;
745 /* Force, in case only special bits changed */
746 var.activate |= FB_ACTIVATE_FORCE;
747 retval = fb_set_var(info, &var);
748 info->flags &= ~FBINFO_MISC_USEREVENT;
749 release_console_sem();
752 ps3fb_mode = old_mode;
756 case PS3FB_IOCTL_GETMODE:
757 val = ps3av_get_mode();
758 DPRINTK("PS3FB_IOCTL_GETMODE:%x\n", val);
759 if (!copy_to_user(argp, &val, sizeof(val)))
763 case PS3FB_IOCTL_SCREENINFO:
765 struct ps3fb_ioctl_res res;
766 int i = ps3fb.res_index;
767 DPRINTK("PS3FB_IOCTL_SCREENINFO:\n");
768 res.xres = ps3fb_res[i].xres;
769 res.yres = ps3fb_res[i].yres;
770 res.xoff = ps3fb_res[i].xoff;
771 res.yoff = ps3fb_res[i].yoff;
772 res.num_frames = ps3fb.num_frames;
773 if (!copy_to_user(argp, &res, sizeof(res)))
779 DPRINTK("PS3FB_IOCTL_ON:\n");
780 atomic_inc(&ps3fb.ext_flip);
784 case PS3FB_IOCTL_OFF:
785 DPRINTK("PS3FB_IOCTL_OFF:\n");
786 if (atomic_read(&ps3fb.ext_flip) > 0)
787 atomic_dec(&ps3fb.ext_flip);
791 case PS3FB_IOCTL_FSEL:
792 if (copy_from_user(&val, argp, sizeof(val)))
795 DPRINTK("PS3FB_IOCTL_FSEL:%d\n", val);
796 retval = ps3fb_sync(val);
800 retval = -ENOIOCTLCMD;
806 static int ps3fbd(void *arg)
811 if (atomic_read(&ps3fb.ext_flip) == 0)
812 ps3fb_sync(0); /* single buffer */
817 static irqreturn_t ps3fb_vsync_interrupt(int irq, void *ptr)
821 struct display_head *head = &ps3fb.dinfo->display_head[1];
823 status = lv1_gpu_context_intr(ps3fb.context_handle, &v1);
825 printk(KERN_ERR "%s: lv1_gpu_context_intr failed: %d\n",
826 __FUNCTION__, status);
830 if (v1 & (1 << GPU_INTR_STATUS_VSYNC_1)) {
832 ps3fb.vblank_count = head->vblank_count;
833 if (!ps3fb.is_blanked)
835 wake_up_interruptible(&ps3fb.wait_vsync);
842 static int __init ps3fb_setup(char *options)
847 if (!options || !*options)
848 return 0; /* no options */
850 while ((this_opt = strsep(&options, ",")) != NULL) {
853 if (!strncmp(this_opt, "mode:", 5))
854 mode = simple_strtoul(this_opt + 5, NULL, 0);
856 mode_option = this_opt;
866 static void ps3fb_platform_release(struct device *device)
868 /* This is called when the reference count goes to zero. */
871 static int ps3fb_vsync_settings(struct gpu_driver_info *dinfo, void *dev)
875 DPRINTK("version_driver:%x\n", dinfo->version_driver);
876 DPRINTK("irq outlet:%x\n", dinfo->irq.irq_outlet);
877 DPRINTK("version_gpu:%x memory_size:%x ch:%x core_freq:%d mem_freq:%d\n",
878 dinfo->version_gpu, dinfo->memory_size, dinfo->hardware_channel,
879 dinfo->nvcore_frequency/1000000, dinfo->memory_frequency/1000000);
881 if (dinfo->version_driver != GPU_DRIVER_INFO_VERSION) {
882 printk(KERN_ERR "%s: version_driver err:%x\n", __FUNCTION__,
883 dinfo->version_driver);
888 error = ps3_alloc_irq(PS3_BINDING_CPU_ANY, dinfo->irq.irq_outlet,
891 printk(KERN_ERR "%s: ps3_alloc_irq failed %d\n", __FUNCTION__,
896 error = request_irq(ps3fb.irq_no, ps3fb_vsync_interrupt, IRQF_DISABLED,
897 "ps3fb vsync", ps3fb.dev);
899 printk(KERN_ERR "%s: request_irq failed %d\n", __FUNCTION__,
901 ps3_free_irq(ps3fb.irq_no);
905 dinfo->irq.mask = (1 << GPU_INTR_STATUS_VSYNC_1) |
906 (1 << GPU_INTR_STATUS_FLIP_1);
910 static int ps3fb_xdr_settings(u64 xdr_lpar)
914 status = lv1_gpu_context_iomap(ps3fb.context_handle, GPU_IOIF,
915 xdr_lpar, ps3fb_videomemory.size, 0);
917 printk(KERN_ERR "%s: lv1_gpu_context_iomap failed: %d\n",
918 __FUNCTION__, status);
921 DPRINTK("video:%p xdr_ea:%p ioif:%lx lpar:%lx phys:%lx size:%lx\n",
922 ps3fb_videomemory.address, ps3fb.xdr_ea, GPU_IOIF, xdr_lpar,
923 virt_to_abs(ps3fb.xdr_ea), ps3fb_videomemory.size);
925 status = lv1_gpu_context_attribute(ps3fb.context_handle,
926 L1GPU_CONTEXT_ATTRIBUTE_FB_SETUP,
927 xdr_lpar, ps3fb_videomemory.size,
930 printk(KERN_ERR "%s: lv1_gpu_context_attribute FB_SETUP failed: %d\n",
931 __FUNCTION__, status);
937 static struct fb_ops ps3fb_ops = {
938 .fb_open = ps3fb_open,
939 .fb_release = ps3fb_release,
940 .fb_check_var = ps3fb_check_var,
941 .fb_set_par = ps3fb_set_par,
942 .fb_setcolreg = ps3fb_setcolreg,
943 .fb_fillrect = cfb_fillrect,
944 .fb_copyarea = cfb_copyarea,
945 .fb_imageblit = cfb_imageblit,
946 .fb_mmap = ps3fb_mmap,
947 .fb_blank = ps3fb_blank,
948 .fb_ioctl = ps3fb_ioctl,
949 .fb_compat_ioctl = ps3fb_ioctl
952 static struct fb_fix_screeninfo ps3fb_fix __initdata = {
954 .type = FB_TYPE_PACKED_PIXELS,
955 .visual = FB_VISUAL_TRUECOLOR,
956 .accel = FB_ACCEL_NONE,
959 static int __init ps3fb_probe(struct platform_device *dev)
961 struct fb_info *info;
962 int retval = -ENOMEM;
964 u64 lpar_dma_control = 0;
965 u64 lpar_driver_info = 0;
966 u64 lpar_reports = 0;
967 u64 lpar_reports_size = 0;
970 unsigned long offset;
972 /* get gpu context handle */
973 status = lv1_gpu_memory_allocate(DDR_SIZE, 0, 0, 0, 0,
974 &ps3fb.memory_handle, &ddr_lpar);
976 printk(KERN_ERR "%s: lv1_gpu_memory_allocate failed: %d\n",
977 __FUNCTION__, status);
980 DPRINTK("ddr:lpar:0x%lx\n", ddr_lpar);
982 status = lv1_gpu_context_allocate(ps3fb.memory_handle, 0,
983 &ps3fb.context_handle,
984 &lpar_dma_control, &lpar_driver_info,
985 &lpar_reports, &lpar_reports_size);
987 printk(KERN_ERR "%s: lv1_gpu_context_attribute failed: %d\n",
988 __FUNCTION__, status);
989 goto err_gpu_memory_free;
992 /* vsync interrupt */
993 ps3fb.dinfo = ioremap(lpar_driver_info, 128 * 1024);
995 printk(KERN_ERR "%s: ioremap failed\n", __FUNCTION__);
996 goto err_gpu_context_free;
999 retval = ps3fb_vsync_settings(ps3fb.dinfo, dev);
1001 goto err_iounmap_dinfo;
1003 /* xdr frame buffer */
1004 ps3fb.xdr_ea = ps3fb_videomemory.address;
1005 xdr_lpar = ps3_mm_phys_to_lpar(__pa(ps3fb.xdr_ea));
1006 retval = ps3fb_xdr_settings(xdr_lpar);
1011 * ps3fb must clear memory to prevent kernel info
1012 * leakage into userspace
1014 memset(ps3fb.xdr_ea, 0, ps3fb_videomemory.size);
1015 info = framebuffer_alloc(sizeof(u32) * 16, &dev->dev);
1019 offset = FB_OFF(ps3fb.res_index) + VP_OFF(ps3fb.res_index);
1020 info->screen_base = (char __iomem *)ps3fb.xdr_ea + offset;
1021 info->fbops = &ps3fb_ops;
1023 info->fix = ps3fb_fix;
1024 info->fix.smem_start = virt_to_abs(ps3fb.xdr_ea);
1025 info->fix.smem_len = ps3fb_videomemory.size - offset;
1026 info->pseudo_palette = info->par;
1028 info->flags = FBINFO_FLAG_DEFAULT;
1030 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1032 goto err_framebuffer_release;
1034 if (!fb_find_mode(&info->var, info, mode_option, ps3fb_modedb,
1035 ARRAY_SIZE(ps3fb_modedb), ps3fb_default_mode(), 32)) {
1037 goto err_fb_dealloc;
1040 fb_videomode_to_modelist(ps3fb_modedb, ARRAY_SIZE(ps3fb_modedb),
1043 retval = register_framebuffer(info);
1045 goto err_fb_dealloc;
1047 platform_set_drvdata(dev, info);
1050 "fb%d: PS3 frame buffer device, using %ld KiB of video memory\n",
1051 info->node, ps3fb_videomemory.size >> 10);
1053 kernel_thread(ps3fbd, info, CLONE_KERNEL);
1057 fb_dealloc_cmap(&info->cmap);
1058 err_framebuffer_release:
1059 framebuffer_release(info);
1061 free_irq(ps3fb.irq_no, ps3fb.dev);
1062 ps3_free_irq(ps3fb.irq_no);
1064 iounmap((u8 __iomem *)ps3fb.dinfo);
1065 err_gpu_context_free:
1066 lv1_gpu_context_free(ps3fb.context_handle);
1067 err_gpu_memory_free:
1068 lv1_gpu_memory_free(ps3fb.memory_handle);
1073 static void ps3fb_shutdown(struct platform_device *dev)
1075 ps3fb_flip_ctl(0); /* flip off */
1076 ps3fb.dinfo->irq.mask = 0;
1077 free_irq(ps3fb.irq_no, ps3fb.dev);
1078 ps3_free_irq(ps3fb.irq_no);
1079 iounmap((u8 __iomem *)ps3fb.dinfo);
1082 void ps3fb_cleanup(void)
1087 free_irq(ps3fb.irq_no, ps3fb.dev);
1088 ps3_free_irq(ps3fb.irq_no);
1090 iounmap((u8 __iomem *)ps3fb.dinfo);
1092 status = lv1_gpu_context_free(ps3fb.context_handle);
1094 DPRINTK("lv1_gpu_context_free failed: %d\n", status);
1096 status = lv1_gpu_memory_free(ps3fb.memory_handle);
1098 DPRINTK("lv1_gpu_memory_free failed: %d\n", status);
1103 EXPORT_SYMBOL_GPL(ps3fb_cleanup);
1105 static int ps3fb_remove(struct platform_device *dev)
1107 struct fb_info *info = platform_get_drvdata(dev);
1110 unregister_framebuffer(info);
1111 fb_dealloc_cmap(&info->cmap);
1112 framebuffer_release(info);
1118 static struct platform_driver ps3fb_driver = {
1119 .probe = ps3fb_probe,
1120 .remove = ps3fb_remove,
1121 .shutdown = ps3fb_shutdown,
1122 .driver = { .name = "ps3fb" }
1125 static struct platform_device ps3fb_device = {
1128 .dev = { .release = ps3fb_platform_release }
1131 int ps3fb_set_sync(void)
1136 status = lv1_gpu_context_attribute(0x0,
1137 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
1138 0, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);
1140 printk(KERN_ERR "%s: lv1_gpu_context_attribute DISPLAY_SYNC failed: %d\n",
1141 __FUNCTION__, status);
1146 status = lv1_gpu_context_attribute(0x0,
1147 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
1148 1, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);
1151 printk(KERN_ERR "%s: lv1_gpu_context_attribute DISPLAY_MODE failed: %d\n",
1152 __FUNCTION__, status);
1159 EXPORT_SYMBOL_GPL(ps3fb_set_sync);
1161 static int __init ps3fb_init(void)
1166 char *option = NULL;
1168 if (fb_get_options("ps3fb", &option))
1172 if (!ps3fb_videomemory.address)
1175 error = ps3av_dev_open();
1177 printk(KERN_ERR "%s: ps3av_dev_open failed\n", __FUNCTION__);
1181 ps3fb_mode = ps3av_get_mode();
1182 DPRINTK("ps3av_mode:%d\n", ps3fb_mode);
1184 mode = ps3fb_setup(option); /* check boot option */
1188 if (ps3fb_mode > 0) {
1190 ps3av_video_mode2res(ps3fb_mode, &xres, &yres);
1191 ps3fb.res_index = ps3fb_get_res_table(xres, yres);
1192 DPRINTK("res_index:%d\n", ps3fb.res_index);
1194 ps3fb.res_index = GPU_RES_INDEX;
1196 atomic_set(&ps3fb.f_count, -1); /* fbcon opens ps3fb */
1197 atomic_set(&ps3fb.ext_flip, 0); /* for flip with vsync */
1198 init_MUTEX(&ps3fb.sem);
1199 init_waitqueue_head(&ps3fb.wait_vsync);
1200 ps3fb.num_frames = 1;
1202 error = platform_driver_register(&ps3fb_driver);
1204 error = platform_device_register(&ps3fb_device);
1206 platform_driver_unregister(&ps3fb_driver);
1217 module_init(ps3fb_init);
1220 static void __exit ps3fb_exit(void)
1222 platform_device_unregister(&ps3fb_device);
1223 platform_driver_unregister(&ps3fb_driver);
1226 module_exit(ps3fb_exit);
1228 MODULE_LICENSE("GPL");