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 GPU_CMD_BUF_SIZE                        (2 * 1024 * 1024)
 
  55 #define GPU_FB_START                            (64 * 1024)
 
  56 #define GPU_IOIF                                (0x0d000000UL)
 
  57 #define GPU_ALIGN_UP(x)                         _ALIGN_UP((x), 64)
 
  58 #define GPU_MAX_LINE_LENGTH                     (65536 - 64)
 
  60 #define PS3FB_FULL_MODE_BIT                     0x80
 
  62 #define GPU_INTR_STATUS_VSYNC_0                 0       /* vsync on head A */
 
  63 #define GPU_INTR_STATUS_VSYNC_1                 1       /* vsync on head B */
 
  64 #define GPU_INTR_STATUS_FLIP_0                  3       /* flip head A */
 
  65 #define GPU_INTR_STATUS_FLIP_1                  4       /* flip head B */
 
  66 #define GPU_INTR_STATUS_QUEUE_0                 5       /* queue head A */
 
  67 #define GPU_INTR_STATUS_QUEUE_1                 6       /* queue head B */
 
  69 #define GPU_DRIVER_INFO_VERSION                 0x211
 
 103 struct gpu_driver_info {
 
 107         u32 hardware_channel;
 
 109         u32 nvcore_frequency;
 
 110         u32 memory_frequency;
 
 113         struct display_head display_head[8];
 
 120         u64 context_handle, memory_handle;
 
 123         struct gpu_driver_info *dinfo;
 
 125         u64 vblank_count;       /* frame count */
 
 126         wait_queue_head_t wait_vsync;
 
 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;
 
 137         u32 pseudo_palette[16];
 
 138         int mode_id, new_mode_id;
 
 140         unsigned int num_frames;        /* num of frame buffers */
 
 143         unsigned long full_offset;      /* start of fullscreen DDR fb */
 
 144         unsigned long fb_offset;        /* start of actual DDR fb */
 
 145         unsigned long pan_offset;
 
 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 */
 
 298 /* Start of the virtual frame buffer (relative to fullscreen ) */
 
 299 #define VP_OFF(i)       ((WIDTH(i) * Y_OFF(i) + X_OFF(i)) * BPP)
 
 302 static int ps3fb_mode;
 
 303 module_param(ps3fb_mode, int, 0);
 
 305 static char *mode_option __devinitdata;
 
 307 static int ps3fb_get_res_table(u32 xres, u32 yres, int mode)
 
 313         full_mode = (mode & PS3FB_FULL_MODE_BIT) ? PS3FB_RES_FULL : 0;
 
 315                 x = ps3fb_res[i].xres;
 
 316                 y = ps3fb_res[i].yres;
 
 317                 f = ps3fb_res[i].type;
 
 320                         pr_debug("ERROR: ps3fb_get_res_table()\n");
 
 324                 if (full_mode == PS3FB_RES_FULL && f != PS3FB_RES_FULL)
 
 327                 if (x == xres && (yres == 0 || y == yres))
 
 330                 x = x - 2 * ps3fb_res[i].xoff;
 
 331                 y = y - 2 * ps3fb_res[i].yoff;
 
 332                 if (x == xres && (yres == 0 || y == yres))
 
 338 static unsigned int ps3fb_find_mode(const struct fb_var_screeninfo *var,
 
 339                                     u32 *ddr_line_length, u32 *xdr_line_length)
 
 341         unsigned int i, mode;
 
 343         for (i = 0; i < ARRAY_SIZE(ps3fb_modedb); i++)
 
 344                 if (var->xres == ps3fb_modedb[i].xres &&
 
 345                     var->yres == ps3fb_modedb[i].yres &&
 
 346                     var->pixclock == ps3fb_modedb[i].pixclock &&
 
 347                     var->hsync_len == ps3fb_modedb[i].hsync_len &&
 
 348                     var->vsync_len == ps3fb_modedb[i].vsync_len &&
 
 349                     var->left_margin == ps3fb_modedb[i].left_margin &&
 
 350                     var->right_margin == ps3fb_modedb[i].right_margin &&
 
 351                     var->upper_margin == ps3fb_modedb[i].upper_margin &&
 
 352                     var->lower_margin == ps3fb_modedb[i].lower_margin &&
 
 353                     var->sync == ps3fb_modedb[i].sync &&
 
 354                     (var->vmode & FB_VMODE_MASK) == ps3fb_modedb[i].vmode)
 
 357         pr_debug("ps3fb_find_mode: mode not found\n");
 
 361         /* Cropped broadcast modes use the full line length */
 
 362         *ddr_line_length = ps3fb_modedb[i < 10 ? i + 13 : i].xres * BPP;
 
 364         if (ps3_compare_firmware_version(1, 9, 0) >= 0) {
 
 365                 *xdr_line_length = GPU_ALIGN_UP(max(var->xres,
 
 366                                                     var->xres_virtual) * BPP);
 
 367                 if (*xdr_line_length > GPU_MAX_LINE_LENGTH)
 
 368                         *xdr_line_length = GPU_MAX_LINE_LENGTH;
 
 370                 *xdr_line_length = *ddr_line_length;
 
 372         /* Full broadcast modes have the full mode bit set */
 
 373         mode = i > 12 ? (i - 12) | PS3FB_FULL_MODE_BIT : i + 1;
 
 375         pr_debug("ps3fb_find_mode: mode %u\n", mode);
 
 380 static const struct fb_videomode *ps3fb_default_mode(int id)
 
 382         u32 mode = id & PS3AV_MODE_MASK;
 
 385         if (mode < 1 || mode > 13)
 
 388         flags = id & ~PS3AV_MODE_MASK;
 
 390         if (mode <= 10 && flags & PS3FB_FULL_MODE_BIT) {
 
 391                 /* Full broadcast mode */
 
 392                 return &ps3fb_modedb[mode + 12];
 
 395         return &ps3fb_modedb[mode - 1];
 
 398 static void ps3fb_sync_image(struct device *dev, u64 frame_offset,
 
 399                              u64 dst_offset, u64 src_offset, u32 width,
 
 400                              u32 height, u32 dst_line_length,
 
 406         line_length = dst_line_length;
 
 407         if (src_line_length != dst_line_length)
 
 408                 line_length |= (u64)src_line_length << 32;
 
 410         src_offset += GPU_FB_START;
 
 411         status = lv1_gpu_context_attribute(ps3fb.context_handle,
 
 412                                            L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT,
 
 413                                            dst_offset, GPU_IOIF + src_offset,
 
 414                                            L1GPU_FB_BLIT_WAIT_FOR_COMPLETION |
 
 415                                            (width << 16) | height,
 
 419                         "%s: lv1_gpu_context_attribute FB_BLIT failed: %d\n",
 
 422         status = lv1_gpu_context_attribute(ps3fb.context_handle,
 
 423                                            L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
 
 424                                            0, frame_offset, 0, 0);
 
 426                 dev_err(dev, "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
 
 430         status = lv1_gpu_context_attribute(ps3fb.context_handle,
 
 431                                            L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
 
 432                                            1, frame_offset, 0, 0);
 
 434                 dev_err(dev, "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
 
 439 static int ps3fb_sync(struct fb_info *info, u32 frame)
 
 441         struct ps3fb_par *par = info->par;
 
 443         u32 ddr_line_length, xdr_line_length;
 
 444         u64 ddr_base, xdr_base;
 
 446         acquire_console_sem();
 
 448         if (frame > par->num_frames - 1) {
 
 449                 dev_dbg(info->device, "%s: invalid frame number (%u)\n",
 
 456         xdr_line_length = info->fix.line_length;
 
 457         ddr_line_length = ps3fb_res[i].xres * BPP;
 
 458         xdr_base = frame * info->var.yres_virtual * xdr_line_length;
 
 459         ddr_base = frame * ps3fb_res[i].yres * ddr_line_length;
 
 461         ps3fb_sync_image(info->device, ddr_base + par->full_offset,
 
 462                          ddr_base + par->fb_offset, xdr_base + par->pan_offset,
 
 463                          par->width, par->height, ddr_line_length,
 
 467         release_console_sem();
 
 471 static int ps3fb_open(struct fb_info *info, int user)
 
 473         atomic_inc(&ps3fb.f_count);
 
 477 static int ps3fb_release(struct fb_info *info, int user)
 
 479         if (atomic_dec_and_test(&ps3fb.f_count)) {
 
 480                 if (atomic_read(&ps3fb.ext_flip)) {
 
 481                         atomic_set(&ps3fb.ext_flip, 0);
 
 482                         ps3fb_sync(info, 0);    /* single buffer */
 
 489      *  Setting the video mode has been split into two parts.
 
 490      *  First part, xxxfb_check_var, must not write anything
 
 491      *  to hardware, it should only verify and adjust var.
 
 492      *  This means it doesn't alter par but it does use hardware
 
 493      *  data from it to check this var.
 
 496 static int ps3fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
 
 498         u32 xdr_line_length, ddr_line_length;
 
 501         dev_dbg(info->device, "var->xres:%u info->var.xres:%u\n", var->xres,
 
 503         dev_dbg(info->device, "var->yres:%u info->var.yres:%u\n", var->yres,
 
 506         /* FIXME For now we do exact matches only */
 
 507         mode = ps3fb_find_mode(var, &ddr_line_length, &xdr_line_length);
 
 512         if (var->xres_virtual < var->xres)
 
 513                 var->xres_virtual = var->xres;
 
 514         if (var->yres_virtual < var->yres)
 
 515                 var->yres_virtual = var->yres;
 
 517         if (var->xres_virtual > xdr_line_length / BPP) {
 
 518                 dev_dbg(info->device,
 
 519                         "Horizontal virtual screen size too large\n");
 
 523         if (var->xoffset + var->xres > var->xres_virtual ||
 
 524             var->yoffset + var->yres > var->yres_virtual) {
 
 525                 dev_dbg(info->device, "panning out-of-range\n");
 
 529         /* We support ARGB8888 only */
 
 530         if (var->bits_per_pixel > 32 || var->grayscale ||
 
 531             var->red.offset > 16 || var->green.offset > 8 ||
 
 532             var->blue.offset > 0 || var->transp.offset > 24 ||
 
 533             var->red.length > 8 || var->green.length > 8 ||
 
 534             var->blue.length > 8 || var->transp.length > 8 ||
 
 535             var->red.msb_right || var->green.msb_right ||
 
 536             var->blue.msb_right || var->transp.msb_right || var->nonstd) {
 
 537                 dev_dbg(info->device, "We support ARGB8888 only\n");
 
 541         var->bits_per_pixel = 32;
 
 542         var->red.offset = 16;
 
 543         var->green.offset = 8;
 
 544         var->blue.offset = 0;
 
 545         var->transp.offset = 24;
 
 547         var->green.length = 8;
 
 548         var->blue.length = 8;
 
 549         var->transp.length = 8;
 
 550         var->red.msb_right = 0;
 
 551         var->green.msb_right = 0;
 
 552         var->blue.msb_right = 0;
 
 553         var->transp.msb_right = 0;
 
 555         /* Rotation is not supported */
 
 557                 dev_dbg(info->device, "Rotation is not supported\n");
 
 562         if (var->yres_virtual * xdr_line_length > ps3fb.xdr_size) {
 
 563                 dev_dbg(info->device, "Not enough memory\n");
 
 574      * This routine actually sets the video mode.
 
 577 static int ps3fb_set_par(struct fb_info *info)
 
 579         struct ps3fb_par *par = info->par;
 
 580         unsigned int mode, ddr_line_length, xdr_line_length, lines, maxlines;
 
 582         unsigned long offset;
 
 585         dev_dbg(info->device, "xres:%d xv:%d yres:%d yv:%d clock:%d\n",
 
 586                 info->var.xres, info->var.xres_virtual,
 
 587                 info->var.yres, info->var.yres_virtual, info->var.pixclock);
 
 589         mode = ps3fb_find_mode(&info->var, &ddr_line_length, &xdr_line_length);
 
 593         i = ps3fb_get_res_table(info->var.xres, info->var.yres, mode);
 
 596         info->fix.smem_start = virt_to_abs(ps3fb.xdr_ea);
 
 597         info->fix.smem_len = ps3fb.xdr_size;
 
 598         info->fix.xpanstep = info->var.xres_virtual > info->var.xres ? 1 : 0;
 
 599         info->fix.ypanstep = info->var.yres_virtual > info->var.yres ? 1 : 0;
 
 600         info->fix.line_length = xdr_line_length;
 
 602         info->screen_base = (char __iomem *)ps3fb.xdr_ea;
 
 604         par->num_frames = ps3fb.xdr_size /
 
 605                           max(ps3fb_res[i].yres * ddr_line_length,
 
 606                               info->var.yres_virtual * xdr_line_length);
 
 608         /* Keep the special bits we cannot set using fb_var_screeninfo */
 
 609         par->new_mode_id = (par->new_mode_id & ~PS3AV_MODE_MASK) | mode;
 
 611         par->width = info->var.xres;
 
 612         par->height = info->var.yres;
 
 614         par->fb_offset = GPU_ALIGN_UP(offset);
 
 615         par->full_offset = par->fb_offset - offset;
 
 616         par->pan_offset = info->var.yoffset * xdr_line_length +
 
 617                           info->var.xoffset * BPP;
 
 619         if (par->new_mode_id != par->mode_id) {
 
 620                 if (ps3av_set_video_mode(par->new_mode_id)) {
 
 621                         par->new_mode_id = par->mode_id;
 
 624                 par->mode_id = par->new_mode_id;
 
 627         /* Clear XDR frame buffer memory */
 
 628         memset(ps3fb.xdr_ea, 0, ps3fb.xdr_size);
 
 630         /* Clear DDR frame buffer memory */
 
 631         lines = ps3fb_res[i].yres * par->num_frames;
 
 632         if (par->full_offset)
 
 634         maxlines = ps3fb.xdr_size / ddr_line_length;
 
 635         for (dst = 0; lines; dst += maxlines * ddr_line_length) {
 
 636                 unsigned int l = min(lines, maxlines);
 
 637                 ps3fb_sync_image(info->device, 0, dst, 0, ps3fb_res[i].xres, l,
 
 638                                  ddr_line_length, ddr_line_length);
 
 646      *  Set a single color register. The values supplied are already
 
 647      *  rounded down to the hardware's capabilities (according to the
 
 648      *  entries in the var structure). Return != 0 for invalid regno.
 
 651 static int ps3fb_setcolreg(unsigned int regno, unsigned int red,
 
 652                            unsigned int green, unsigned int blue,
 
 653                            unsigned int transp, struct fb_info *info)
 
 663         ((u32 *)info->pseudo_palette)[regno] = transp << 24 | red << 16 |
 
 668 static int ps3fb_pan_display(struct fb_var_screeninfo *var,
 
 669                              struct fb_info *info)
 
 671         struct ps3fb_par *par = info->par;
 
 673         par->pan_offset = var->yoffset * info->fix.line_length +
 
 679      *  As we have a virtual frame buffer, we need our own mmap function
 
 682 static int ps3fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
 
 684         unsigned long size, offset;
 
 686         size = vma->vm_end - vma->vm_start;
 
 687         offset = vma->vm_pgoff << PAGE_SHIFT;
 
 688         if (offset + size > info->fix.smem_len)
 
 691         offset += info->fix.smem_start;
 
 692         if (remap_pfn_range(vma, vma->vm_start, offset >> PAGE_SHIFT,
 
 693                             size, vma->vm_page_prot))
 
 696         dev_dbg(info->device, "ps3fb: mmap framebuffer P(%lx)->V(%lx)\n",
 
 697                 offset, vma->vm_start);
 
 705 static int ps3fb_blank(int blank, struct fb_info *info)
 
 709         dev_dbg(info->device, "%s: blank:%d\n", __func__, blank);
 
 711         case FB_BLANK_POWERDOWN:
 
 712         case FB_BLANK_HSYNC_SUSPEND:
 
 713         case FB_BLANK_VSYNC_SUSPEND:
 
 714         case FB_BLANK_NORMAL:
 
 715                 retval = ps3av_video_mute(1);   /* mute on */
 
 717                         ps3fb.is_blanked = 1;
 
 720         default:                /* unblank */
 
 721                 retval = ps3av_video_mute(0);   /* mute off */
 
 723                         ps3fb.is_blanked = 0;
 
 729 static int ps3fb_get_vblank(struct fb_vblank *vblank)
 
 731         memset(vblank, 0, sizeof(*vblank));
 
 732         vblank->flags = FB_VBLANK_HAVE_VSYNC;
 
 736 static int ps3fb_wait_for_vsync(u32 crtc)
 
 741         count = ps3fb.vblank_count;
 
 742         ret = wait_event_interruptible_timeout(ps3fb.wait_vsync,
 
 743                                                count != ps3fb.vblank_count,
 
 751 static void ps3fb_flip_ctl(int on, void *data)
 
 753         struct ps3fb_priv *priv = data;
 
 755                 atomic_dec_if_positive(&priv->ext_flip);
 
 757                 atomic_inc(&priv->ext_flip);
 
 765 static int ps3fb_ioctl(struct fb_info *info, unsigned int cmd,
 
 768         void __user *argp = (void __user *)arg;
 
 770         int retval = -EFAULT;
 
 775                         struct fb_vblank vblank;
 
 776                         dev_dbg(info->device, "FBIOGET_VBLANK:\n");
 
 777                         retval = ps3fb_get_vblank(&vblank);
 
 781                         if (copy_to_user(argp, &vblank, sizeof(vblank)))
 
 786         case FBIO_WAITFORVSYNC:
 
 789                         dev_dbg(info->device, "FBIO_WAITFORVSYNC:\n");
 
 790                         if (get_user(crt, (u32 __user *) arg))
 
 793                         retval = ps3fb_wait_for_vsync(crt);
 
 797         case PS3FB_IOCTL_SETMODE:
 
 799                         struct ps3fb_par *par = info->par;
 
 800                         const struct fb_videomode *mode;
 
 801                         struct fb_var_screeninfo var;
 
 803                         if (copy_from_user(&val, argp, sizeof(val)))
 
 806                         if (!(val & PS3AV_MODE_MASK)) {
 
 807                                 u32 id = ps3av_get_auto_mode();
 
 809                                         val = (val & ~PS3AV_MODE_MASK) | id;
 
 811                         dev_dbg(info->device, "PS3FB_IOCTL_SETMODE:%x\n", val);
 
 813                         mode = ps3fb_default_mode(val);
 
 816                                 fb_videomode_to_var(&var, mode);
 
 817                                 acquire_console_sem();
 
 818                                 info->flags |= FBINFO_MISC_USEREVENT;
 
 819                                 /* Force, in case only special bits changed */
 
 820                                 var.activate |= FB_ACTIVATE_FORCE;
 
 821                                 par->new_mode_id = val;
 
 822                                 retval = fb_set_var(info, &var);
 
 823                                 info->flags &= ~FBINFO_MISC_USEREVENT;
 
 824                                 release_console_sem();
 
 829         case PS3FB_IOCTL_GETMODE:
 
 830                 val = ps3av_get_mode();
 
 831                 dev_dbg(info->device, "PS3FB_IOCTL_GETMODE:%x\n", val);
 
 832                 if (!copy_to_user(argp, &val, sizeof(val)))
 
 836         case PS3FB_IOCTL_SCREENINFO:
 
 838                         struct ps3fb_par *par = info->par;
 
 839                         struct ps3fb_ioctl_res res;
 
 840                         dev_dbg(info->device, "PS3FB_IOCTL_SCREENINFO:\n");
 
 841                         res.xres = info->fix.line_length / BPP;
 
 842                         res.yres = info->var.yres_virtual;
 
 843                         res.xoff = (res.xres - info->var.xres) / 2;
 
 844                         res.yoff = (res.yres - info->var.yres) / 2;
 
 845                         res.num_frames = par->num_frames;
 
 846                         if (!copy_to_user(argp, &res, sizeof(res)))
 
 852                 dev_dbg(info->device, "PS3FB_IOCTL_ON:\n");
 
 853                 atomic_inc(&ps3fb.ext_flip);
 
 857         case PS3FB_IOCTL_OFF:
 
 858                 dev_dbg(info->device, "PS3FB_IOCTL_OFF:\n");
 
 859                 atomic_dec_if_positive(&ps3fb.ext_flip);
 
 863         case PS3FB_IOCTL_FSEL:
 
 864                 if (copy_from_user(&val, argp, sizeof(val)))
 
 867                 dev_dbg(info->device, "PS3FB_IOCTL_FSEL:%d\n", val);
 
 868                 retval = ps3fb_sync(info, val);
 
 872                 retval = -ENOIOCTLCMD;
 
 878 static int ps3fbd(void *arg)
 
 880         struct fb_info *info = arg;
 
 883         while (!kthread_should_stop()) {
 
 885                 set_current_state(TASK_INTERRUPTIBLE);
 
 886                 if (ps3fb.is_kicked) {
 
 888                         ps3fb_sync(info, 0);    /* single buffer */
 
 895 static irqreturn_t ps3fb_vsync_interrupt(int irq, void *ptr)
 
 897         struct device *dev = ptr;
 
 900         struct display_head *head = &ps3fb.dinfo->display_head[1];
 
 902         status = lv1_gpu_context_intr(ps3fb.context_handle, &v1);
 
 904                 dev_err(dev, "%s: lv1_gpu_context_intr failed: %d\n", __func__,
 
 909         if (v1 & (1 << GPU_INTR_STATUS_VSYNC_1)) {
 
 911                 ps3fb.vblank_count = head->vblank_count;
 
 912                 if (ps3fb.task && !ps3fb.is_blanked &&
 
 913                     !atomic_read(&ps3fb.ext_flip)) {
 
 915                         wake_up_process(ps3fb.task);
 
 917                 wake_up_interruptible(&ps3fb.wait_vsync);
 
 924 static int ps3fb_vsync_settings(struct gpu_driver_info *dinfo,
 
 929         dev_dbg(dev, "version_driver:%x\n", dinfo->version_driver);
 
 930         dev_dbg(dev, "irq outlet:%x\n", dinfo->irq.irq_outlet);
 
 932                 "version_gpu: %x memory_size: %x ch: %x core_freq: %d "
 
 934                 dinfo->version_gpu, dinfo->memory_size, dinfo->hardware_channel,
 
 935                 dinfo->nvcore_frequency/1000000, dinfo->memory_frequency/1000000);
 
 937         if (dinfo->version_driver != GPU_DRIVER_INFO_VERSION) {
 
 938                 dev_err(dev, "%s: version_driver err:%x\n", __func__,
 
 939                         dinfo->version_driver);
 
 943         error = ps3_irq_plug_setup(PS3_BINDING_CPU_ANY, dinfo->irq.irq_outlet,
 
 946                 dev_err(dev, "%s: ps3_alloc_irq failed %d\n", __func__, error);
 
 950         error = request_irq(ps3fb.irq_no, ps3fb_vsync_interrupt, IRQF_DISABLED,
 
 953                 dev_err(dev, "%s: request_irq failed %d\n", __func__, error);
 
 954                 ps3_irq_plug_destroy(ps3fb.irq_no);
 
 958         dinfo->irq.mask = (1 << GPU_INTR_STATUS_VSYNC_1) |
 
 959                           (1 << GPU_INTR_STATUS_FLIP_1);
 
 963 static int ps3fb_xdr_settings(u64 xdr_lpar, struct device *dev)
 
 967         status = lv1_gpu_context_iomap(ps3fb.context_handle, GPU_IOIF,
 
 968                                        xdr_lpar, ps3fb_videomemory.size, 0);
 
 970                 dev_err(dev, "%s: lv1_gpu_context_iomap failed: %d\n",
 
 975                 "video:%p xdr_ea:%p ioif:%lx lpar:%lx phys:%lx size:%lx\n",
 
 976                 ps3fb_videomemory.address, ps3fb.xdr_ea, GPU_IOIF, xdr_lpar,
 
 977                 virt_to_abs(ps3fb.xdr_ea), ps3fb_videomemory.size);
 
 979         status = lv1_gpu_context_attribute(ps3fb.context_handle,
 
 980                                            L1GPU_CONTEXT_ATTRIBUTE_FB_SETUP,
 
 981                                            xdr_lpar, GPU_CMD_BUF_SIZE,
 
 985                         "%s: lv1_gpu_context_attribute FB_SETUP failed: %d\n",
 
 992 static struct fb_ops ps3fb_ops = {
 
 993         .fb_open        = ps3fb_open,
 
 994         .fb_release     = ps3fb_release,
 
 995         .fb_read        = fb_sys_read,
 
 996         .fb_write       = fb_sys_write,
 
 997         .fb_check_var   = ps3fb_check_var,
 
 998         .fb_set_par     = ps3fb_set_par,
 
 999         .fb_setcolreg   = ps3fb_setcolreg,
 
1000         .fb_pan_display = ps3fb_pan_display,
 
1001         .fb_fillrect    = sys_fillrect,
 
1002         .fb_copyarea    = sys_copyarea,
 
1003         .fb_imageblit   = sys_imageblit,
 
1004         .fb_mmap        = ps3fb_mmap,
 
1005         .fb_blank       = ps3fb_blank,
 
1006         .fb_ioctl       = ps3fb_ioctl,
 
1007         .fb_compat_ioctl = ps3fb_ioctl
 
1010 static struct fb_fix_screeninfo ps3fb_fix __initdata = {
 
1012         .type =         FB_TYPE_PACKED_PIXELS,
 
1013         .visual =       FB_VISUAL_TRUECOLOR,
 
1014         .accel =        FB_ACCEL_NONE,
 
1017 static int ps3fb_set_sync(struct device *dev)
 
1022         status = lv1_gpu_context_attribute(0x0,
 
1023                                            L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
 
1024                                            0, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);
 
1027                         "%s: lv1_gpu_context_attribute DISPLAY_SYNC failed: "
 
1034         status = lv1_gpu_context_attribute(0x0,
 
1035                                            L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
 
1036                                            1, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);
 
1040                         "%s: lv1_gpu_context_attribute DISPLAY_SYNC failed: "
 
1049 static int __devinit ps3fb_probe(struct ps3_system_bus_device *dev)
 
1051         struct fb_info *info;
 
1052         struct ps3fb_par *par;
 
1053         int retval = -ENOMEM;
 
1056         u64 lpar_dma_control = 0;
 
1057         u64 lpar_driver_info = 0;
 
1058         u64 lpar_reports = 0;
 
1059         u64 lpar_reports_size = 0;
 
1061         int status, res_index;
 
1062         struct task_struct *task;
 
1063         unsigned long max_ps3fb_size;
 
1065         if (ps3fb_videomemory.size < GPU_CMD_BUF_SIZE) {
 
1066                 dev_err(&dev->core, "%s: Not enough video memory\n", __func__);
 
1070         status = ps3_open_hv_device(dev);
 
1072                 dev_err(&dev->core, "%s: ps3_open_hv_device failed\n",
 
1078                 ps3fb_mode = ps3av_get_mode();
 
1079         dev_dbg(&dev->core, "ps3av_mode:%d\n", ps3fb_mode);
 
1081         if (ps3fb_mode > 0 &&
 
1082             !ps3av_video_mode2res(ps3fb_mode, &xres, &yres)) {
 
1083                 res_index = ps3fb_get_res_table(xres, yres, ps3fb_mode);
 
1084                 dev_dbg(&dev->core, "res_index:%d\n", res_index);
 
1086                 res_index = GPU_RES_INDEX;
 
1088         atomic_set(&ps3fb.f_count, -1); /* fbcon opens ps3fb */
 
1089         atomic_set(&ps3fb.ext_flip, 0); /* for flip with vsync */
 
1090         init_waitqueue_head(&ps3fb.wait_vsync);
 
1092         ps3fb_set_sync(&dev->core);
 
1094         max_ps3fb_size = _ALIGN_UP(GPU_IOIF, 256*1024*1024) - GPU_IOIF;
 
1095         if (ps3fb_videomemory.size > max_ps3fb_size) {
 
1096                 dev_info(&dev->core, "Limiting ps3fb mem size to %lu bytes\n",
 
1098                 ps3fb_videomemory.size = max_ps3fb_size;
 
1101         /* get gpu context handle */
 
1102         status = lv1_gpu_memory_allocate(ps3fb_videomemory.size, 0, 0, 0, 0,
 
1103                                          &ps3fb.memory_handle, &ddr_lpar);
 
1105                 dev_err(&dev->core, "%s: lv1_gpu_memory_allocate failed: %d\n",
 
1109         dev_dbg(&dev->core, "ddr:lpar:0x%lx\n", ddr_lpar);
 
1111         status = lv1_gpu_context_allocate(ps3fb.memory_handle, 0,
 
1112                                           &ps3fb.context_handle,
 
1113                                           &lpar_dma_control, &lpar_driver_info,
 
1114                                           &lpar_reports, &lpar_reports_size);
 
1117                         "%s: lv1_gpu_context_attribute failed: %d\n", __func__,
 
1119                 goto err_gpu_memory_free;
 
1122         /* vsync interrupt */
 
1123         ps3fb.dinfo = ioremap(lpar_driver_info, 128 * 1024);
 
1125                 dev_err(&dev->core, "%s: ioremap failed\n", __func__);
 
1126                 goto err_gpu_context_free;
 
1129         retval = ps3fb_vsync_settings(ps3fb.dinfo, &dev->core);
 
1131                 goto err_iounmap_dinfo;
 
1133         /* XDR frame buffer */
 
1134         ps3fb.xdr_ea = ps3fb_videomemory.address;
 
1135         xdr_lpar = ps3_mm_phys_to_lpar(__pa(ps3fb.xdr_ea));
 
1137         /* Clear memory to prevent kernel info leakage into userspace */
 
1138         memset(ps3fb.xdr_ea, 0, ps3fb_videomemory.size);
 
1141          * The GPU command buffer is at the start of video memory
 
1142          * As we don't use the full command buffer, we can put the actual
 
1143          * frame buffer at offset GPU_FB_START and save some precious XDR
 
1146         ps3fb.xdr_ea += GPU_FB_START;
 
1147         ps3fb.xdr_size = ps3fb_videomemory.size - GPU_FB_START;
 
1149         retval = ps3fb_xdr_settings(xdr_lpar, &dev->core);
 
1153         info = framebuffer_alloc(sizeof(struct ps3fb_par), &dev->core);
 
1158         par->mode_id = ~ps3fb_mode;     /* != ps3fb_mode, to trigger change */
 
1159         par->new_mode_id = ps3fb_mode;
 
1160         par->res_index = res_index;
 
1161         par->num_frames = 1;
 
1163         info->screen_base = (char __iomem *)ps3fb.xdr_ea;
 
1164         info->fbops = &ps3fb_ops;
 
1166         info->fix = ps3fb_fix;
 
1167         info->fix.smem_start = virt_to_abs(ps3fb.xdr_ea);
 
1168         info->fix.smem_len = ps3fb.xdr_size;
 
1169         info->pseudo_palette = par->pseudo_palette;
 
1170         info->flags = FBINFO_DEFAULT | FBINFO_READS_FAST |
 
1171                       FBINFO_HWACCEL_XPAN | FBINFO_HWACCEL_YPAN;
 
1173         retval = fb_alloc_cmap(&info->cmap, 256, 0);
 
1175                 goto err_framebuffer_release;
 
1177         if (!fb_find_mode(&info->var, info, mode_option, ps3fb_modedb,
 
1178                           ARRAY_SIZE(ps3fb_modedb),
 
1179                           ps3fb_default_mode(par->new_mode_id), 32)) {
 
1181                 goto err_fb_dealloc;
 
1184         fb_videomode_to_modelist(ps3fb_modedb, ARRAY_SIZE(ps3fb_modedb),
 
1187         retval = register_framebuffer(info);
 
1189                 goto err_fb_dealloc;
 
1191         dev->core.driver_data = info;
 
1193         dev_info(info->device, "%s %s, using %lu KiB of video memory\n",
 
1194                  dev_driver_string(info->dev), info->dev->bus_id,
 
1195                  ps3fb.xdr_size >> 10);
 
1197         task = kthread_run(ps3fbd, info, DEVICE_NAME);
 
1199                 retval = PTR_ERR(task);
 
1200                 goto err_unregister_framebuffer;
 
1204         ps3av_register_flip_ctl(ps3fb_flip_ctl, &ps3fb);
 
1208 err_unregister_framebuffer:
 
1209         unregister_framebuffer(info);
 
1211         fb_dealloc_cmap(&info->cmap);
 
1212 err_framebuffer_release:
 
1213         framebuffer_release(info);
 
1215         free_irq(ps3fb.irq_no, &dev->core);
 
1216         ps3_irq_plug_destroy(ps3fb.irq_no);
 
1218         iounmap((u8 __iomem *)ps3fb.dinfo);
 
1219 err_gpu_context_free:
 
1220         lv1_gpu_context_free(ps3fb.context_handle);
 
1221 err_gpu_memory_free:
 
1222         lv1_gpu_memory_free(ps3fb.memory_handle);
 
1227 static int ps3fb_shutdown(struct ps3_system_bus_device *dev)
 
1230         struct fb_info *info = dev->core.driver_data;
 
1232         dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
 
1234         ps3fb_flip_ctl(0, &ps3fb);      /* flip off */
 
1235         ps3fb.dinfo->irq.mask = 0;
 
1238                 unregister_framebuffer(info);
 
1239                 fb_dealloc_cmap(&info->cmap);
 
1240                 framebuffer_release(info);
 
1243         ps3av_register_flip_ctl(NULL, NULL);
 
1245                 struct task_struct *task = ps3fb.task;
 
1250                 free_irq(ps3fb.irq_no, &dev->core);
 
1251                 ps3_irq_plug_destroy(ps3fb.irq_no);
 
1253         iounmap((u8 __iomem *)ps3fb.dinfo);
 
1255         status = lv1_gpu_context_free(ps3fb.context_handle);
 
1257                 dev_dbg(&dev->core, "lv1_gpu_context_free failed: %d\n",
 
1260         status = lv1_gpu_memory_free(ps3fb.memory_handle);
 
1262                 dev_dbg(&dev->core, "lv1_gpu_memory_free failed: %d\n",
 
1265         ps3_close_hv_device(dev);
 
1266         dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
 
1271 static struct ps3_system_bus_driver ps3fb_driver = {
 
1272         .match_id       = PS3_MATCH_ID_GRAPHICS,
 
1273         .core.name      = DEVICE_NAME,
 
1274         .core.owner     = THIS_MODULE,
 
1275         .probe          = ps3fb_probe,
 
1276         .remove         = ps3fb_shutdown,
 
1277         .shutdown       = ps3fb_shutdown,
 
1280 static int __init ps3fb_setup(void)
 
1288         if (fb_get_options(DEVICE_NAME, &options))
 
1291         if (!options || !*options)
 
1295                 char *this_opt = strsep(&options, ",");
 
1301                 if (!strncmp(this_opt, "mode:", 5))
 
1302                         ps3fb_mode = simple_strtoul(this_opt + 5, NULL, 0);
 
1304                         mode_option = this_opt;
 
1309 static int __init ps3fb_init(void)
 
1311         if (!ps3fb_videomemory.address ||  ps3fb_setup())
 
1314         return ps3_system_bus_driver_register(&ps3fb_driver);
 
1317 static void __exit ps3fb_exit(void)
 
1319         pr_debug(" -> %s:%d\n", __func__, __LINE__);
 
1320         ps3_system_bus_driver_unregister(&ps3fb_driver);
 
1321         pr_debug(" <- %s:%d\n", __func__, __LINE__);
 
1324 module_init(ps3fb_init);
 
1325 module_exit(ps3fb_exit);
 
1327 MODULE_LICENSE("GPL");
 
1328 MODULE_DESCRIPTION("PS3 GPU Frame Buffer Driver");
 
1329 MODULE_AUTHOR("Sony Computer Entertainment Inc.");
 
1330 MODULE_ALIAS(PS3_MODULE_ALIAS_GRAPHICS);