fbdev: move FBIO_WAITFORVSYNC to linux/fb.h
[safe/jmp/linux-2.6] / drivers / video / ps3fb.c
index 9756a72..9c0144e 100644 (file)
 #include <linux/errno.h>
 #include <linux/string.h>
 #include <linux/mm.h>
-#include <linux/tty.h>
-#include <linux/slab.h>
-#include <linux/vmalloc.h>
-#include <linux/delay.h>
 #include <linux/interrupt.h>
-#include <linux/platform_device.h>
 #include <linux/console.h>
 #include <linux/ioctl.h>
-#include <linux/notifier.h>
-#include <linux/reboot.h>
 #include <linux/kthread.h>
 #include <linux/freezer.h>
-
-#include <asm/uaccess.h>
+#include <linux/uaccess.h>
 #include <linux/fb.h>
 #include <linux/init.h>
-#include <asm/time.h>
 
 #include <asm/abs_addr.h>
+#include <asm/cell-regs.h>
 #include <asm/lv1call.h>
 #include <asm/ps3av.h>
 #include <asm/ps3fb.h>
 #include <asm/ps3.h>
+#include <asm/ps3gpu.h>
 
-#ifdef PS3FB_DEBUG
-#define DPRINTK(fmt, args...) printk("%s: " fmt, __func__ , ##args)
-#else
-#define DPRINTK(fmt, args...)
-#endif
-
-#define L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC   0x101
-#define L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP   0x102
-#define L1GPU_CONTEXT_ATTRIBUTE_FB_SETUP       0x600
-#define L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT                0x601
-#define L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT_SYNC   0x602
 
-#define L1GPU_FB_BLIT_WAIT_FOR_COMPLETION      (1ULL << 32)
+#define DEVICE_NAME            "ps3fb"
 
-#define L1GPU_DISPLAY_SYNC_HSYNC               1
-#define L1GPU_DISPLAY_SYNC_VSYNC               2
-
-#define DDR_SIZE                               (0)     /* used no ddr */
-#define GPU_OFFSET                             (64 * 1024)
+#define GPU_CMD_BUF_SIZE                       (2 * 1024 * 1024)
+#define GPU_FB_START                           (64 * 1024)
 #define GPU_IOIF                               (0x0d000000UL)
-
-#define PS3FB_FULL_MODE_BIT                    0x80
+#define GPU_ALIGN_UP(x)                                _ALIGN_UP((x), 64)
+#define GPU_MAX_LINE_LENGTH                    (65536 - 64)
 
 #define GPU_INTR_STATUS_VSYNC_0                        0       /* vsync on head A */
 #define GPU_INTR_STATUS_VSYNC_1                        1       /* vsync on head B */
@@ -126,17 +105,13 @@ struct gpu_driver_info {
 
 struct ps3fb_priv {
        unsigned int irq_no;
-       void *dev;
 
        u64 context_handle, memory_handle;
-       void *xdr_ea;
        struct gpu_driver_info *dinfo;
-       u32 res_index;
 
        u64 vblank_count;       /* frame count */
        wait_queue_head_t wait_vsync;
 
-       u32 num_frames;         /* num of frame buffers */
        atomic_t ext_flip;      /* on/off flip with vsync */
        atomic_t f_count;       /* fb_open count */
        int is_blanked;
@@ -145,33 +120,22 @@ struct ps3fb_priv {
 };
 static struct ps3fb_priv ps3fb;
 
-struct ps3fb_res_table {
-       u32 xres;
-       u32 yres;
-       u32 xoff;
-       u32 yoff;
-       u32 type;
+struct ps3fb_par {
+       u32 pseudo_palette[16];
+       int mode_id, new_mode_id;
+       unsigned int num_frames;        /* num of frame buffers */
+       unsigned int width;
+       unsigned int height;
+       unsigned int ddr_line_length;
+       unsigned int ddr_frame_size;
+       unsigned int xdr_frame_size;
+       unsigned int full_offset;       /* start of fullscreen DDR fb */
+       unsigned int fb_offset;         /* start of actual DDR fb */
+       unsigned int pan_offset;
 };
-#define PS3FB_RES_FULL 1
-static const struct ps3fb_res_table ps3fb_res[] = {
-       /* res_x,y   margin_x,y  full */
-       {  720,  480,  72,  48 , 0},
-       {  720,  576,  72,  58 , 0},
-       { 1280,  720,  78,  38 , 0},
-       { 1920, 1080, 116,  58 , 0},
-       /* full mode */
-       {  720,  480,   0,   0 , PS3FB_RES_FULL},
-       {  720,  576,   0,   0 , PS3FB_RES_FULL},
-       { 1280,  720,   0,   0 , PS3FB_RES_FULL},
-       { 1920, 1080,   0,   0 , PS3FB_RES_FULL},
-       /* vesa: normally full mode */
-       { 1280,  768,   0,   0 , 0},
-       { 1280, 1024,   0,   0 , 0},
-       { 1920, 1200,   0,   0 , 0},
-       {    0,    0,   0,   0 , 0} };
-
-/* default resolution */
-#define GPU_RES_INDEX 0                /* 720 x 480 */
+
+
+#define FIRST_NATIVE_MODE_INDEX        10
 
 static const struct fb_videomode ps3fb_modedb[] = {
     /* 60 Hz broadcast modes (modes "1" to "5") */
@@ -211,7 +175,7 @@ static const struct fb_videomode ps3fb_modedb[] = {
         "720p", 50, 1124, 644, 13468, 298, 478, 57, 44, 80, 5,
         FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
     },    {
-        /* 1080 */
+        /* 1080i */
         "1080i", 50, 1688, 964, 13468, 264, 600, 94, 62, 88, 5,
         FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
     },    {
@@ -220,24 +184,7 @@ static const struct fb_videomode ps3fb_modedb[] = {
         FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
     },
 
-    /* VESA modes (modes "11" to "13") */
-    {
-       /* WXGA */
-       "wxga", 60, 1280, 768, 12924, 160, 24, 29, 3, 136, 6,
-       0, FB_VMODE_NONINTERLACED,
-       FB_MODE_IS_VESA
-    }, {
-       /* SXGA */
-       "sxga", 60, 1280, 1024, 9259, 248, 48, 38, 1, 112, 3,
-       FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED,
-       FB_MODE_IS_VESA
-    }, {
-       /* WUXGA */
-       "wuxga", 60, 1920, 1200, 6494, 80, 48, 26, 3, 32, 6,
-       FB_SYNC_HOR_HIGH_ACT, FB_VMODE_NONINTERLACED,
-       FB_MODE_IS_VESA
-    },
-
+    [FIRST_NATIVE_MODE_INDEX] =
     /* 60 Hz broadcast modes (full resolution versions of modes "1" to "5") */
     {
        /* 480if */
@@ -276,12 +223,30 @@ static const struct fb_videomode ps3fb_modedb[] = {
        FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
     }, {
        /* 1080if */
-       "1080f", 50, 1920, 1080, 13468, 148, 484, 36, 4, 88, 5,
+       "1080if", 50, 1920, 1080, 13468, 148, 484, 36, 4, 88, 5,
        FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
     }, {
        /* 1080pf */
        "1080pf", 50, 1920, 1080, 6734, 148, 484, 36, 4, 88, 5,
        FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
+    },
+
+    /* VESA modes (modes "11" to "13") */
+    {
+       /* WXGA */
+       "wxga", 60, 1280, 768, 12924, 160, 24, 29, 3, 136, 6,
+       0, FB_VMODE_NONINTERLACED,
+       FB_MODE_IS_VESA
+    }, {
+       /* SXGA */
+       "sxga", 60, 1280, 1024, 9259, 248, 48, 38, 1, 112, 3,
+       FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED,
+       FB_MODE_IS_VESA
+    }, {
+       /* WUXGA */
+       "wuxga", 60, 1920, 1200, 6494, 80, 48, 26, 3, 32, 6,
+       FB_SYNC_HOR_HIGH_ACT, FB_VMODE_NONINTERLACED,
+       FB_MODE_IS_VESA
     }
 };
 
@@ -289,150 +254,253 @@ static const struct fb_videomode ps3fb_modedb[] = {
 #define HEAD_A
 #define HEAD_B
 
-#define X_OFF(i)       (ps3fb_res[i].xoff)     /* left/right margin (pixel) */
-#define Y_OFF(i)       (ps3fb_res[i].yoff)     /* top/bottom margin (pixel) */
-#define WIDTH(i)       (ps3fb_res[i].xres)     /* width of FB */
-#define HEIGHT(i)      (ps3fb_res[i].yres)     /* height of FB */
-#define BPP    4               /* number of bytes per pixel */
-#define VP_OFF(i)      (WIDTH(i) * Y_OFF(i) * BPP + X_OFF(i) * BPP)
-#define FB_OFF(i)      (GPU_OFFSET - VP_OFF(i) % GPU_OFFSET)
+#define BPP            4                       /* number of bytes per pixel */
+
 
 static int ps3fb_mode;
-module_param(ps3fb_mode, bool, 0);
+module_param(ps3fb_mode, int, 0);
 
-static char *mode_option __initdata;
+static char *mode_option __devinitdata;
 
+static int ps3fb_cmp_mode(const struct fb_videomode *vmode,
+                         const struct fb_var_screeninfo *var)
+{
+       long xres, yres, left_margin, right_margin, upper_margin, lower_margin;
+       long dx, dy;
+
+       /* maximum values */
+       if (var->xres > vmode->xres || var->yres > vmode->yres ||
+           var->pixclock > vmode->pixclock ||
+           var->hsync_len > vmode->hsync_len ||
+           var->vsync_len > vmode->vsync_len)
+               return -1;
+
+       /* progressive/interlaced must match */
+       if ((var->vmode & FB_VMODE_MASK) != vmode->vmode)
+               return -1;
+
+       /* minimum resolution */
+       xres = max(var->xres, 1U);
+       yres = max(var->yres, 1U);
+
+       /* minimum margins */
+       left_margin = max(var->left_margin, vmode->left_margin);
+       right_margin = max(var->right_margin, vmode->right_margin);
+       upper_margin = max(var->upper_margin, vmode->upper_margin);
+       lower_margin = max(var->lower_margin, vmode->lower_margin);
+
+       /* resolution + margins may not exceed native parameters */
+       dx = ((long)vmode->left_margin + (long)vmode->xres +
+             (long)vmode->right_margin) -
+            (left_margin + xres + right_margin);
+       if (dx < 0)
+               return -1;
+
+       dy = ((long)vmode->upper_margin + (long)vmode->yres +
+             (long)vmode->lower_margin) -
+            (upper_margin + yres + lower_margin);
+       if (dy < 0)
+               return -1;
+
+       /* exact match */
+       if (!dx && !dy)
+               return 0;
+
+       /* resolution difference */
+       return (vmode->xres - xres) * (vmode->yres - yres);
+}
 
-static int ps3fb_get_res_table(u32 xres, u32 yres)
+static const struct fb_videomode *ps3fb_native_vmode(enum ps3av_mode_num id)
 {
-       int full_mode;
-       unsigned int i;
-       u32 x, y, f;
-
-       full_mode = (ps3fb_mode & PS3FB_FULL_MODE_BIT) ? PS3FB_RES_FULL : 0;
-       for (i = 0;; i++) {
-               x = ps3fb_res[i].xres;
-               y = ps3fb_res[i].yres;
-               f = ps3fb_res[i].type;
-
-               if (!x) {
-                       DPRINTK("ERROR: ps3fb_get_res_table()\n");
-                       return -1;
-               }
+       return &ps3fb_modedb[FIRST_NATIVE_MODE_INDEX + id - 1];
+}
 
-               if (full_mode == PS3FB_RES_FULL && f != PS3FB_RES_FULL)
-                       continue;
+static const struct fb_videomode *ps3fb_vmode(int id)
+{
+       u32 mode = id & PS3AV_MODE_MASK;
 
-               if (x == xres && (yres == 0 || y == yres))
-                       break;
+       if (mode < PS3AV_MODE_480I || mode > PS3AV_MODE_WUXGA)
+               return NULL;
 
-               x = x - 2 * ps3fb_res[i].xoff;
-               y = y - 2 * ps3fb_res[i].yoff;
-               if (x == xres && (yres == 0 || y == yres))
-                       break;
+       if (mode <= PS3AV_MODE_1080P50 && !(id & PS3AV_MODE_FULL)) {
+               /* Non-fullscreen broadcast mode */
+               return &ps3fb_modedb[mode - 1];
        }
-       return i;
+
+       return ps3fb_native_vmode(mode);
 }
 
-static unsigned int ps3fb_find_mode(const struct fb_var_screeninfo *var,
-                                   u32 *line_length)
+static unsigned int ps3fb_find_mode(struct fb_var_screeninfo *var,
+                                   u32 *ddr_line_length, u32 *xdr_line_length)
 {
-       unsigned int i, mode;
-
-       for (i = 0; i < ARRAY_SIZE(ps3fb_modedb); i++)
-               if (var->xres == ps3fb_modedb[i].xres &&
-                   var->yres == ps3fb_modedb[i].yres &&
-                   var->pixclock == ps3fb_modedb[i].pixclock &&
-                   var->hsync_len == ps3fb_modedb[i].hsync_len &&
-                   var->vsync_len == ps3fb_modedb[i].vsync_len &&
-                   var->left_margin == ps3fb_modedb[i].left_margin &&
-                   var->right_margin == ps3fb_modedb[i].right_margin &&
-                   var->upper_margin == ps3fb_modedb[i].upper_margin &&
-                   var->lower_margin == ps3fb_modedb[i].lower_margin &&
-                   var->sync == ps3fb_modedb[i].sync &&
-                   (var->vmode & FB_VMODE_MASK) == ps3fb_modedb[i].vmode) {
-                       /* Cropped broadcast modes use the full line_length */
-                       *line_length =
-                           ps3fb_modedb[i < 10 ? i + 13 : i].xres * 4;
-                       /* Full broadcast modes have the full mode bit set */
-                       mode = i > 12 ? (i - 12) | PS3FB_FULL_MODE_BIT : i + 1;
-
-                       DPRINTK("ps3fb_find_mode: mode %u\n", mode);
-                       return mode;
+       unsigned int id, best_id;
+       int diff, best_diff;
+       const struct fb_videomode *vmode;
+       long gap;
+
+       best_id = 0;
+       best_diff = INT_MAX;
+       pr_debug("%s: wanted %u [%u] %u x %u [%u] %u\n", __func__,
+                var->left_margin, var->xres, var->right_margin,
+                var->upper_margin, var->yres, var->lower_margin);
+       for (id = PS3AV_MODE_480I; id <= PS3AV_MODE_WUXGA; id++) {
+               vmode = ps3fb_native_vmode(id);
+               diff = ps3fb_cmp_mode(vmode, var);
+               pr_debug("%s: mode %u: %u [%u] %u x %u [%u] %u: diff = %d\n",
+                        __func__, id, vmode->left_margin, vmode->xres,
+                        vmode->right_margin, vmode->upper_margin,
+                        vmode->yres, vmode->lower_margin, diff);
+               if (diff < 0)
+                       continue;
+               if (diff < best_diff) {
+                       best_id = id;
+                       if (!diff)
+                               break;
+                       best_diff = diff;
                }
+       }
 
-       DPRINTK("ps3fb_find_mode: mode not found\n");
-       return 0;
+       if (!best_id) {
+               pr_debug("%s: no suitable mode found\n", __func__);
+               return 0;
+       }
 
-}
+       id = best_id;
+       vmode = ps3fb_native_vmode(id);
+
+       *ddr_line_length = vmode->xres * BPP;
+
+       /* minimum resolution */
+       if (!var->xres)
+               var->xres = 1;
+       if (!var->yres)
+               var->yres = 1;
+
+       /* minimum virtual resolution */
+       if (var->xres_virtual < var->xres)
+               var->xres_virtual = var->xres;
+       if (var->yres_virtual < var->yres)
+               var->yres_virtual = var->yres;
+
+       /* minimum margins */
+       if (var->left_margin < vmode->left_margin)
+               var->left_margin = vmode->left_margin;
+       if (var->right_margin < vmode->right_margin)
+               var->right_margin = vmode->right_margin;
+       if (var->upper_margin < vmode->upper_margin)
+               var->upper_margin = vmode->upper_margin;
+       if (var->lower_margin < vmode->lower_margin)
+               var->lower_margin = vmode->lower_margin;
+
+       /* extra margins */
+       gap = ((long)vmode->left_margin + (long)vmode->xres +
+              (long)vmode->right_margin) -
+             ((long)var->left_margin + (long)var->xres +
+              (long)var->right_margin);
+       if (gap > 0) {
+               var->left_margin += gap/2;
+               var->right_margin += (gap+1)/2;
+               pr_debug("%s: rounded up H to %u [%u] %u\n", __func__,
+                        var->left_margin, var->xres, var->right_margin);
+       }
 
-static const struct fb_videomode *ps3fb_default_mode(void)
-{
-       u32 mode = ps3fb_mode & PS3AV_MODE_MASK;
-       u32 flags;
+       gap = ((long)vmode->upper_margin + (long)vmode->yres +
+              (long)vmode->lower_margin) -
+             ((long)var->upper_margin + (long)var->yres +
+              (long)var->lower_margin);
+       if (gap > 0) {
+               var->upper_margin += gap/2;
+               var->lower_margin += (gap+1)/2;
+               pr_debug("%s: rounded up V to %u [%u] %u\n", __func__,
+                        var->upper_margin, var->yres, var->lower_margin);
+       }
 
-       if (mode < 1 || mode > 13)
-               return NULL;
+       /* fixed fields */
+       var->pixclock = vmode->pixclock;
+       var->hsync_len = vmode->hsync_len;
+       var->vsync_len = vmode->vsync_len;
+       var->sync = vmode->sync;
 
-       flags = ps3fb_mode & ~PS3AV_MODE_MASK;
+       if (ps3_compare_firmware_version(1, 9, 0) >= 0) {
+               *xdr_line_length = GPU_ALIGN_UP(var->xres_virtual * BPP);
+               if (*xdr_line_length > GPU_MAX_LINE_LENGTH)
+                       *xdr_line_length = GPU_MAX_LINE_LENGTH;
+       } else
+               *xdr_line_length = *ddr_line_length;
 
-       if (mode <= 10 && flags & PS3FB_FULL_MODE_BIT) {
-               /* Full broadcast mode */
-               return &ps3fb_modedb[mode + 12];
+       if (vmode->sync & FB_SYNC_BROADCAST) {
+               /* Full broadcast modes have the full mode bit set */
+               if (vmode->xres == var->xres && vmode->yres == var->yres)
+                       id |= PS3AV_MODE_FULL;
        }
 
-       return &ps3fb_modedb[mode - 1];
+       pr_debug("%s: mode %u\n", __func__, id);
+       return id;
 }
 
-static int ps3fb_sync(u32 frame)
+static void ps3fb_sync_image(struct device *dev, u64 frame_offset,
+                            u64 dst_offset, u64 src_offset, u32 width,
+                            u32 height, u32 dst_line_length,
+                            u32 src_line_length)
 {
-       int i, status;
-       u32 xres, yres;
-       u64 fb_ioif, offset;
+       int status;
+       u64 line_length;
 
-       i = ps3fb.res_index;
-       xres = ps3fb_res[i].xres;
-       yres = ps3fb_res[i].yres;
+       line_length = dst_line_length;
+       if (src_line_length != dst_line_length)
+               line_length |= (u64)src_line_length << 32;
+
+       src_offset += GPU_FB_START;
+
+       mutex_lock(&ps3_gpu_mutex);
+       status = lv1_gpu_fb_blit(ps3fb.context_handle, dst_offset,
+                                GPU_IOIF + src_offset,
+                                L1GPU_FB_BLIT_WAIT_FOR_COMPLETION |
+                                (width << 16) | height,
+                                line_length);
+       mutex_unlock(&ps3_gpu_mutex);
 
-       if (frame > ps3fb.num_frames - 1) {
-               printk(KERN_WARNING "%s: invalid frame number (%u)\n",
-                      __func__, frame);
-               return -EINVAL;
-       }
-       offset = xres * yres * BPP * frame;
-
-       fb_ioif = GPU_IOIF + FB_OFF(i) + offset;
-       status = lv1_gpu_context_attribute(ps3fb.context_handle,
-                                          L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT,
-                                          offset, fb_ioif,
-                                          L1GPU_FB_BLIT_WAIT_FOR_COMPLETION |
-                                          (xres << 16) | yres,
-                                          xres * BPP); /* line_length */
        if (status)
-               printk(KERN_ERR
-                      "%s: lv1_gpu_context_attribute FB_BLIT failed: %d\n",
-                      __func__, status);
+               dev_err(dev, "%s: lv1_gpu_fb_blit failed: %d\n", __func__,
+                       status);
 #ifdef HEAD_A
-       status = lv1_gpu_context_attribute(ps3fb.context_handle,
-                                          L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
-                                          0, offset, 0, 0);
+       status = lv1_gpu_display_flip(ps3fb.context_handle, 0, frame_offset);
        if (status)
-               printk(KERN_ERR
-                      "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
-                      __func__, status);
+               dev_err(dev, "%s: lv1_gpu_display_flip failed: %d\n", __func__,
+                       status);
 #endif
 #ifdef HEAD_B
-       status = lv1_gpu_context_attribute(ps3fb.context_handle,
-                                          L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
-                                          1, offset, 0, 0);
+       status = lv1_gpu_display_flip(ps3fb.context_handle, 1, frame_offset);
        if (status)
-               printk(KERN_ERR
-                      "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
-                      __func__, status);
+               dev_err(dev, "%s: lv1_gpu_display_flip failed: %d\n", __func__,
+                       status);
 #endif
-       return 0;
 }
 
+static int ps3fb_sync(struct fb_info *info, u32 frame)
+{
+       struct ps3fb_par *par = info->par;
+       int error = 0;
+       u64 ddr_base, xdr_base;
+
+       if (frame > par->num_frames - 1) {
+               dev_dbg(info->device, "%s: invalid frame number (%u)\n",
+                       __func__, frame);
+               error = -EINVAL;
+               goto out;
+       }
+
+       xdr_base = frame * par->xdr_frame_size;
+       ddr_base = frame * par->ddr_frame_size;
+
+       ps3fb_sync_image(info->device, ddr_base + par->full_offset,
+                        ddr_base + par->fb_offset, xdr_base + par->pan_offset,
+                        par->width, par->height, par->ddr_line_length,
+                        info->fix.line_length);
+
+out:
+       return error;
+}
 
 static int ps3fb_open(struct fb_info *info, int user)
 {
@@ -445,7 +513,10 @@ static int ps3fb_release(struct fb_info *info, int user)
        if (atomic_dec_and_test(&ps3fb.f_count)) {
                if (atomic_read(&ps3fb.ext_flip)) {
                        atomic_set(&ps3fb.ext_flip, 0);
-                       ps3fb_sync(0);  /* single buffer */
+                       if (!try_acquire_console_sem()) {
+                               ps3fb_sync(info, 0);    /* single buffer */
+                               release_console_sem();
+                       }
                }
        }
        return 0;
@@ -461,39 +532,26 @@ static int ps3fb_release(struct fb_info *info, int user)
 
 static int ps3fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
 {
-       u32 line_length;
+       u32 xdr_line_length, ddr_line_length;
        int mode;
-       int i;
-
-       DPRINTK("var->xres:%u info->var.xres:%u\n", var->xres, info->var.xres);
-       DPRINTK("var->yres:%u info->var.yres:%u\n", var->yres, info->var.yres);
 
-       /* FIXME For now we do exact matches only */
-       mode = ps3fb_find_mode(var, &line_length);
+       mode = ps3fb_find_mode(var, &ddr_line_length, &xdr_line_length);
        if (!mode)
                return -EINVAL;
 
-       /*
-        *  FB_VMODE_CONUPDATE and FB_VMODE_SMOOTH_XPAN are equal!
-        *  as FB_VMODE_SMOOTH_XPAN is only used internally
-        */
-
-       if (var->vmode & FB_VMODE_CONUPDATE) {
-               var->vmode |= FB_VMODE_YWRAP;
-               var->xoffset = info->var.xoffset;
-               var->yoffset = info->var.yoffset;
+       /* Virtual screen */
+       if (var->xres_virtual > xdr_line_length / BPP) {
+               dev_dbg(info->device,
+                       "Horizontal virtual screen size too large\n");
+               return -EINVAL;
        }
 
-       /* Virtual screen and panning are not supported */
-       if (var->xres_virtual > var->xres || var->yres_virtual > var->yres ||
-           var->xoffset || var->yoffset) {
-               DPRINTK("Virtual screen and panning are not supported\n");
+       if (var->xoffset + var->xres > var->xres_virtual ||
+           var->yoffset + var->yres > var->yres_virtual) {
+               dev_dbg(info->device, "panning out-of-range\n");
                return -EINVAL;
        }
 
-       var->xres_virtual = var->xres;
-       var->yres_virtual = var->yres;
-
        /* We support ARGB8888 only */
        if (var->bits_per_pixel > 32 || var->grayscale ||
            var->red.offset > 16 || var->green.offset > 8 ||
@@ -502,7 +560,7 @@ static int ps3fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
            var->blue.length > 8 || var->transp.length > 8 ||
            var->red.msb_right || var->green.msb_right ||
            var->blue.msb_right || var->transp.msb_right || var->nonstd) {
-               DPRINTK("We support ARGB8888 only\n");
+               dev_dbg(info->device, "We support ARGB8888 only\n");
                return -EINVAL;
        }
 
@@ -522,14 +580,13 @@ static int ps3fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
 
        /* Rotation is not supported */
        if (var->rotate) {
-               DPRINTK("Rotation is not supported\n");
+               dev_dbg(info->device, "Rotation is not supported\n");
                return -EINVAL;
        }
 
        /* Memory limit */
-       i = ps3fb_get_res_table(var->xres, var->yres);
-       if (ps3fb_res[i].xres*ps3fb_res[i].yres*BPP > ps3fb_videomemory.size) {
-               DPRINTK("Not enough memory\n");
+       if (var->yres_virtual * xdr_line_length > info->fix.smem_len) {
+               dev_dbg(info->device, "Not enough memory\n");
                return -ENOMEM;
        }
 
@@ -545,36 +602,68 @@ static int ps3fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
 
 static int ps3fb_set_par(struct fb_info *info)
 {
-       unsigned int mode;
-       int i;
-       unsigned long offset;
-       static int first = 1;
-
-       DPRINTK("xres:%d xv:%d yres:%d yv:%d clock:%d\n",
-               info->var.xres, info->var.xres_virtual,
-               info->var.yres, info->var.yres_virtual, info->var.pixclock);
-       i = ps3fb_get_res_table(info->var.xres, info->var.yres);
-       ps3fb.res_index = i;
-
-       mode = ps3fb_find_mode(&info->var, &info->fix.line_length);
+       struct ps3fb_par *par = info->par;
+       unsigned int mode, ddr_line_length, xdr_line_length, lines, maxlines;
+       unsigned int ddr_xoff, ddr_yoff, offset;
+       const struct fb_videomode *vmode;
+       u64 dst;
+
+       mode = ps3fb_find_mode(&info->var, &ddr_line_length, &xdr_line_length);
        if (!mode)
                return -EINVAL;
 
-       offset = FB_OFF(i) + VP_OFF(i);
-       info->fix.smem_len = ps3fb_videomemory.size - offset;
-       info->screen_base = (char __iomem *)ps3fb.xdr_ea + offset;
-       memset(ps3fb.xdr_ea, 0, ps3fb_videomemory.size);
+       vmode = ps3fb_native_vmode(mode & PS3AV_MODE_MASK);
+
+       info->fix.xpanstep = info->var.xres_virtual > info->var.xres ? 1 : 0;
+       info->fix.ypanstep = info->var.yres_virtual > info->var.yres ? 1 : 0;
+       info->fix.line_length = xdr_line_length;
 
-       ps3fb.num_frames = ps3fb_videomemory.size/
-                          (ps3fb_res[i].xres*ps3fb_res[i].yres*BPP);
+       par->ddr_line_length = ddr_line_length;
+       par->ddr_frame_size = vmode->yres * ddr_line_length;
+       par->xdr_frame_size = info->var.yres_virtual * xdr_line_length;
+
+       par->num_frames = info->fix.smem_len /
+                         max(par->ddr_frame_size, par->xdr_frame_size);
 
        /* Keep the special bits we cannot set using fb_var_screeninfo */
-       ps3fb_mode = (ps3fb_mode & ~PS3AV_MODE_MASK) | mode;
+       par->new_mode_id = (par->new_mode_id & ~PS3AV_MODE_MASK) | mode;
 
-       if (ps3av_set_video_mode(ps3fb_mode, first))
-               return -EINVAL;
+       par->width = info->var.xres;
+       par->height = info->var.yres;
+
+       /* Start of the virtual frame buffer (relative to fullscreen) */
+       ddr_xoff = info->var.left_margin - vmode->left_margin;
+       ddr_yoff = info->var.upper_margin - vmode->upper_margin;
+       offset = ddr_yoff * ddr_line_length + ddr_xoff * BPP;
+
+       par->fb_offset = GPU_ALIGN_UP(offset);
+       par->full_offset = par->fb_offset - offset;
+       par->pan_offset = info->var.yoffset * xdr_line_length +
+                         info->var.xoffset * BPP;
+
+       if (par->new_mode_id != par->mode_id) {
+               if (ps3av_set_video_mode(par->new_mode_id)) {
+                       par->new_mode_id = par->mode_id;
+                       return -EINVAL;
+               }
+               par->mode_id = par->new_mode_id;
+       }
+
+       /* Clear XDR frame buffer memory */
+       memset((void __force *)info->screen_base, 0, info->fix.smem_len);
+
+       /* Clear DDR frame buffer memory */
+       lines = vmode->yres * par->num_frames;
+       if (par->full_offset)
+               lines++;
+       maxlines = info->fix.smem_len / ddr_line_length;
+       for (dst = 0; lines; dst += maxlines * ddr_line_length) {
+               unsigned int l = min(lines, maxlines);
+               ps3fb_sync_image(info->device, 0, dst, 0, vmode->xres, l,
+                                ddr_line_length, ddr_line_length);
+               lines -= l;
+       }
 
-       first = 0;
        return 0;
 }
 
@@ -601,6 +690,16 @@ static int ps3fb_setcolreg(unsigned int regno, unsigned int red,
        return 0;
 }
 
+static int ps3fb_pan_display(struct fb_var_screeninfo *var,
+                            struct fb_info *info)
+{
+       struct ps3fb_par *par = info->par;
+
+       par->pan_offset = var->yoffset * info->fix.line_length +
+                         var->xoffset * BPP;
+       return 0;
+}
+
     /*
      *  As we have a virtual frame buffer, we need our own mmap function
      */
@@ -608,24 +707,19 @@ static int ps3fb_setcolreg(unsigned int regno, unsigned int red,
 static int ps3fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
 {
        unsigned long size, offset;
-       int i;
-
-       i = ps3fb_get_res_table(info->var.xres, info->var.yres);
-       if (i == -1)
-               return -EINVAL;
 
        size = vma->vm_end - vma->vm_start;
        offset = vma->vm_pgoff << PAGE_SHIFT;
        if (offset + size > info->fix.smem_len)
                return -EINVAL;
 
-       offset += info->fix.smem_start + FB_OFF(i) + VP_OFF(i);
+       offset += info->fix.smem_start;
        if (remap_pfn_range(vma, vma->vm_start, offset >> PAGE_SHIFT,
                            size, vma->vm_page_prot))
                return -EAGAIN;
 
-       printk(KERN_DEBUG "ps3fb: mmap framebuffer P(%lx)->V(%lx)\n", offset,
-              vma->vm_start);
+       dev_dbg(info->device, "ps3fb: mmap framebuffer P(%lx)->V(%lx)\n",
+               offset, vma->vm_start);
        return 0;
 }
 
@@ -637,7 +731,7 @@ static int ps3fb_blank(int blank, struct fb_info *info)
 {
        int retval;
 
-       DPRINTK("%s: blank:%d\n", __func__, blank);
+       dev_dbg(info->device, "%s: blank:%d\n", __func__, blank);
        switch (blank) {
        case FB_BLANK_POWERDOWN:
        case FB_BLANK_HSYNC_SUSPEND:
@@ -659,12 +753,12 @@ static int ps3fb_blank(int blank, struct fb_info *info)
 
 static int ps3fb_get_vblank(struct fb_vblank *vblank)
 {
-       memset(vblank, 0, sizeof(&vblank));
+       memset(vblank, 0, sizeof(*vblank));
        vblank->flags = FB_VBLANK_HAVE_VSYNC;
        return 0;
 }
 
-int ps3fb_wait_for_vsync(u32 crtc)
+static int ps3fb_wait_for_vsync(u32 crtc)
 {
        int ret;
        u64 count;
@@ -679,17 +773,6 @@ int ps3fb_wait_for_vsync(u32 crtc)
        return 0;
 }
 
-EXPORT_SYMBOL_GPL(ps3fb_wait_for_vsync);
-
-void ps3fb_flip_ctl(int on)
-{
-       if (on)
-               atomic_dec_if_positive(&ps3fb.ext_flip);
-       else
-               atomic_inc(&ps3fb.ext_flip);
-}
-
-EXPORT_SYMBOL_GPL(ps3fb_flip_ctl);
 
     /*
      * ioctl
@@ -699,14 +782,14 @@ static int ps3fb_ioctl(struct fb_info *info, unsigned int cmd,
                       unsigned long arg)
 {
        void __user *argp = (void __user *)arg;
-       u32 val, old_mode;
+       u32 val;
        int retval = -EFAULT;
 
        switch (cmd) {
        case FBIOGET_VBLANK:
                {
                        struct fb_vblank vblank;
-                       DPRINTK("FBIOGET_VBLANK:\n");
+                       dev_dbg(info->device, "FBIOGET_VBLANK:\n");
                        retval = ps3fb_get_vblank(&vblank);
                        if (retval)
                                break;
@@ -719,7 +802,7 @@ static int ps3fb_ioctl(struct fb_info *info, unsigned int cmd,
        case FBIO_WAITFORVSYNC:
                {
                        u32 crt;
-                       DPRINTK("FBIO_WAITFORVSYNC:\n");
+                       dev_dbg(info->device, "FBIO_WAITFORVSYNC:\n");
                        if (get_user(crt, (u32 __user *) arg))
                                break;
 
@@ -729,68 +812,66 @@ static int ps3fb_ioctl(struct fb_info *info, unsigned int cmd,
 
        case PS3FB_IOCTL_SETMODE:
                {
-                       const struct fb_videomode *mode;
+                       struct ps3fb_par *par = info->par;
+                       const struct fb_videomode *vmode;
                        struct fb_var_screeninfo var;
 
                        if (copy_from_user(&val, argp, sizeof(val)))
                                break;
 
                        if (!(val & PS3AV_MODE_MASK)) {
-                               u32 id = ps3av_get_auto_mode(0);
+                               u32 id = ps3av_get_auto_mode();
                                if (id > 0)
                                        val = (val & ~PS3AV_MODE_MASK) | id;
                        }
-                       DPRINTK("PS3FB_IOCTL_SETMODE:%x\n", val);
+                       dev_dbg(info->device, "PS3FB_IOCTL_SETMODE:%x\n", val);
                        retval = -EINVAL;
-                       old_mode = ps3fb_mode;
-                       ps3fb_mode = val;
-                       mode = ps3fb_default_mode();
-                       if (mode) {
+                       vmode = ps3fb_vmode(val);
+                       if (vmode) {
                                var = info->var;
-                               fb_videomode_to_var(&var, mode);
+                               fb_videomode_to_var(&var, vmode);
                                acquire_console_sem();
                                info->flags |= FBINFO_MISC_USEREVENT;
                                /* Force, in case only special bits changed */
                                var.activate |= FB_ACTIVATE_FORCE;
+                               par->new_mode_id = val;
                                retval = fb_set_var(info, &var);
                                info->flags &= ~FBINFO_MISC_USEREVENT;
                                release_console_sem();
                        }
-                       if (retval)
-                               ps3fb_mode = old_mode;
                        break;
                }
 
        case PS3FB_IOCTL_GETMODE:
                val = ps3av_get_mode();
-               DPRINTK("PS3FB_IOCTL_GETMODE:%x\n", val);
+               dev_dbg(info->device, "PS3FB_IOCTL_GETMODE:%x\n", val);
                if (!copy_to_user(argp, &val, sizeof(val)))
                        retval = 0;
                break;
 
        case PS3FB_IOCTL_SCREENINFO:
                {
+                       struct ps3fb_par *par = info->par;
                        struct ps3fb_ioctl_res res;
-                       int i = ps3fb.res_index;
-                       DPRINTK("PS3FB_IOCTL_SCREENINFO:\n");
-                       res.xres = ps3fb_res[i].xres;
-                       res.yres = ps3fb_res[i].yres;
-                       res.xoff = ps3fb_res[i].xoff;
-                       res.yoff = ps3fb_res[i].yoff;
-                       res.num_frames = ps3fb.num_frames;
+                       dev_dbg(info->device, "PS3FB_IOCTL_SCREENINFO:\n");
+                       res.xres = info->fix.line_length / BPP;
+                       res.yres = info->var.yres_virtual;
+                       res.xoff = (res.xres - info->var.xres) / 2;
+                       res.yoff = (res.yres - info->var.yres) / 2;
+                       res.num_frames = par->num_frames;
                        if (!copy_to_user(argp, &res, sizeof(res)))
                                retval = 0;
                        break;
                }
 
        case PS3FB_IOCTL_ON:
-               DPRINTK("PS3FB_IOCTL_ON:\n");
+               dev_dbg(info->device, "PS3FB_IOCTL_ON:\n");
                atomic_inc(&ps3fb.ext_flip);
                retval = 0;
                break;
 
        case PS3FB_IOCTL_OFF:
-               DPRINTK("PS3FB_IOCTL_OFF:\n");
+               dev_dbg(info->device, "PS3FB_IOCTL_OFF:\n");
                atomic_dec_if_positive(&ps3fb.ext_flip);
                retval = 0;
                break;
@@ -799,8 +880,10 @@ static int ps3fb_ioctl(struct fb_info *info, unsigned int cmd,
                if (copy_from_user(&val, argp, sizeof(val)))
                        break;
 
-               DPRINTK("PS3FB_IOCTL_FSEL:%d\n", val);
-               retval = ps3fb_sync(val);
+               dev_dbg(info->device, "PS3FB_IOCTL_FSEL:%d\n", val);
+               acquire_console_sem();
+               retval = ps3fb_sync(info, val);
+               release_console_sem();
                break;
 
        default:
@@ -812,12 +895,17 @@ static int ps3fb_ioctl(struct fb_info *info, unsigned int cmd,
 
 static int ps3fbd(void *arg)
 {
+       struct fb_info *info = arg;
+
+       set_freezable();
        while (!kthread_should_stop()) {
                try_to_freeze();
                set_current_state(TASK_INTERRUPTIBLE);
                if (ps3fb.is_kicked) {
                        ps3fb.is_kicked = 0;
-                       ps3fb_sync(0);  /* single buffer */
+                       acquire_console_sem();
+                       ps3fb_sync(info, 0);    /* single buffer */
+                       release_console_sem();
                }
                schedule();
        }
@@ -826,14 +914,15 @@ static int ps3fbd(void *arg)
 
 static irqreturn_t ps3fb_vsync_interrupt(int irq, void *ptr)
 {
+       struct device *dev = ptr;
        u64 v1;
        int status;
        struct display_head *head = &ps3fb.dinfo->display_head[1];
 
        status = lv1_gpu_context_intr(ps3fb.context_handle, &v1);
        if (status) {
-               printk(KERN_ERR "%s: lv1_gpu_context_intr failed: %d\n",
-                      __func__, status);
+               dev_err(dev, "%s: lv1_gpu_context_intr failed: %d\n", __func__,
+                       status);
                return IRQ_NONE;
        }
 
@@ -851,112 +940,19 @@ static irqreturn_t ps3fb_vsync_interrupt(int irq, void *ptr)
        return IRQ_HANDLED;
 }
 
-#ifndef MODULE
-static int __init ps3fb_setup(char *options)
-{
-       char *this_opt;
-       int mode = 0;
-
-       if (!options || !*options)
-               return 0;       /* no options */
-
-       while ((this_opt = strsep(&options, ",")) != NULL) {
-               if (!*this_opt)
-                       continue;
-               if (!strncmp(this_opt, "mode:", 5))
-                       mode = simple_strtoul(this_opt + 5, NULL, 0);
-               else
-                       mode_option = this_opt;
-       }
-       return mode;
-}
-#endif /* MODULE */
-
-    /*
-     *  Initialisation
-     */
-
-static void ps3fb_platform_release(struct device *device)
-{
-       /* This is called when the reference count goes to zero. */
-}
-
-static int ps3fb_vsync_settings(struct gpu_driver_info *dinfo, void *dev)
-{
-       int error;
-
-       DPRINTK("version_driver:%x\n", dinfo->version_driver);
-       DPRINTK("irq outlet:%x\n", dinfo->irq.irq_outlet);
-       DPRINTK("version_gpu:%x memory_size:%x ch:%x core_freq:%d mem_freq:%d\n",
-               dinfo->version_gpu, dinfo->memory_size, dinfo->hardware_channel,
-               dinfo->nvcore_frequency/1000000, dinfo->memory_frequency/1000000);
-
-       if (dinfo->version_driver != GPU_DRIVER_INFO_VERSION) {
-               printk(KERN_ERR "%s: version_driver err:%x\n", __func__,
-                      dinfo->version_driver);
-               return -EINVAL;
-       }
-
-       ps3fb.dev = dev;
-       error = ps3_irq_plug_setup(PS3_BINDING_CPU_ANY, dinfo->irq.irq_outlet,
-                                  &ps3fb.irq_no);
-       if (error) {
-               printk(KERN_ERR "%s: ps3_alloc_irq failed %d\n", __func__,
-                      error);
-               return error;
-       }
-
-       error = request_irq(ps3fb.irq_no, ps3fb_vsync_interrupt, IRQF_DISABLED,
-                           "ps3fb vsync", ps3fb.dev);
-       if (error) {
-               printk(KERN_ERR "%s: request_irq failed %d\n", __func__,
-                      error);
-               ps3_irq_plug_destroy(ps3fb.irq_no);
-               return error;
-       }
-
-       dinfo->irq.mask = (1 << GPU_INTR_STATUS_VSYNC_1) |
-                         (1 << GPU_INTR_STATUS_FLIP_1);
-       return 0;
-}
-
-static int ps3fb_xdr_settings(u64 xdr_lpar)
-{
-       int status;
-
-       status = lv1_gpu_context_iomap(ps3fb.context_handle, GPU_IOIF,
-                                      xdr_lpar, ps3fb_videomemory.size, 0);
-       if (status) {
-               printk(KERN_ERR "%s: lv1_gpu_context_iomap failed: %d\n",
-                      __func__, status);
-               return -ENXIO;
-       }
-       DPRINTK("video:%p xdr_ea:%p ioif:%lx lpar:%lx phys:%lx size:%lx\n",
-               ps3fb_videomemory.address, ps3fb.xdr_ea, GPU_IOIF, xdr_lpar,
-               virt_to_abs(ps3fb.xdr_ea), ps3fb_videomemory.size);
-
-       status = lv1_gpu_context_attribute(ps3fb.context_handle,
-                                          L1GPU_CONTEXT_ATTRIBUTE_FB_SETUP,
-                                          xdr_lpar, ps3fb_videomemory.size,
-                                          GPU_IOIF, 0);
-       if (status) {
-               printk(KERN_ERR
-                      "%s: lv1_gpu_context_attribute FB_SETUP failed: %d\n",
-                      __func__, status);
-               return -ENXIO;
-       }
-       return 0;
-}
 
 static struct fb_ops ps3fb_ops = {
        .fb_open        = ps3fb_open,
        .fb_release     = ps3fb_release,
+       .fb_read        = fb_sys_read,
+       .fb_write       = fb_sys_write,
        .fb_check_var   = ps3fb_check_var,
        .fb_set_par     = ps3fb_set_par,
        .fb_setcolreg   = ps3fb_setcolreg,
-       .fb_fillrect    = cfb_fillrect,
-       .fb_copyarea    = cfb_copyarea,
-       .fb_imageblit   = cfb_imageblit,
+       .fb_pan_display = ps3fb_pan_display,
+       .fb_fillrect    = sys_fillrect,
+       .fb_copyarea    = sys_copyarea,
+       .fb_imageblit   = sys_imageblit,
        .fb_mmap        = ps3fb_mmap,
        .fb_blank       = ps3fb_blank,
        .fb_ioctl       = ps3fb_ioctl,
@@ -964,90 +960,201 @@ static struct fb_ops ps3fb_ops = {
 };
 
 static struct fb_fix_screeninfo ps3fb_fix __initdata = {
-       .id =           "PS3 FB",
+       .id =           DEVICE_NAME,
        .type =         FB_TYPE_PACKED_PIXELS,
        .visual =       FB_VISUAL_TRUECOLOR,
        .accel =        FB_ACCEL_NONE,
 };
 
-static int __init ps3fb_probe(struct platform_device *dev)
+static int __devinit ps3fb_probe(struct ps3_system_bus_device *dev)
 {
        struct fb_info *info;
-       int retval = -ENOMEM;
+       struct ps3fb_par *par;
+       int retval;
        u64 ddr_lpar = 0;
        u64 lpar_dma_control = 0;
        u64 lpar_driver_info = 0;
        u64 lpar_reports = 0;
        u64 lpar_reports_size = 0;
        u64 xdr_lpar;
+       struct gpu_driver_info *dinfo;
+       void *fb_start;
        int status;
-       unsigned long offset;
        struct task_struct *task;
+       unsigned long max_ps3fb_size;
+
+       if (ps3fb_videomemory.size < GPU_CMD_BUF_SIZE) {
+               dev_err(&dev->core, "%s: Not enough video memory\n", __func__);
+               return -ENOMEM;
+       }
+
+       retval = ps3_open_hv_device(dev);
+       if (retval) {
+               dev_err(&dev->core, "%s: ps3_open_hv_device failed\n",
+                       __func__);
+               goto err;
+       }
+
+       if (!ps3fb_mode)
+               ps3fb_mode = ps3av_get_mode();
+       dev_dbg(&dev->core, "ps3fb_mode: %d\n", ps3fb_mode);
+
+       atomic_set(&ps3fb.f_count, -1); /* fbcon opens ps3fb */
+       atomic_set(&ps3fb.ext_flip, 0); /* for flip with vsync */
+       init_waitqueue_head(&ps3fb.wait_vsync);
+
+#ifdef HEAD_A
+       status = lv1_gpu_display_sync(0x0, 0, L1GPU_DISPLAY_SYNC_VSYNC);
+       if (status) {
+               dev_err(&dev->core, "%s: lv1_gpu_display_sync failed: %d\n",
+                       __func__, status);
+               retval = -ENODEV;
+               goto err_close_device;
+       }
+#endif
+#ifdef HEAD_B
+       status = lv1_gpu_display_sync(0x0, 1, L1GPU_DISPLAY_SYNC_VSYNC);
+       if (status) {
+               dev_err(&dev->core, "%s: lv1_gpu_display_sync failed: %d\n",
+                       __func__, status);
+               retval = -ENODEV;
+               goto err_close_device;
+       }
+#endif
+
+       max_ps3fb_size = _ALIGN_UP(GPU_IOIF, 256*1024*1024) - GPU_IOIF;
+       if (ps3fb_videomemory.size > max_ps3fb_size) {
+               dev_info(&dev->core, "Limiting ps3fb mem size to %lu bytes\n",
+                        max_ps3fb_size);
+               ps3fb_videomemory.size = max_ps3fb_size;
+       }
 
        /* get gpu context handle */
-       status = lv1_gpu_memory_allocate(DDR_SIZE, 0, 0, 0, 0,
+       status = lv1_gpu_memory_allocate(ps3fb_videomemory.size, 0, 0, 0, 0,
                                         &ps3fb.memory_handle, &ddr_lpar);
        if (status) {
-               printk(KERN_ERR "%s: lv1_gpu_memory_allocate failed: %d\n",
-                      __func__, status);
-               goto err;
+               dev_err(&dev->core, "%s: lv1_gpu_memory_allocate failed: %d\n",
+                       __func__, status);
+               goto err_close_device;
        }
-       DPRINTK("ddr:lpar:0x%lx\n", ddr_lpar);
+       dev_dbg(&dev->core, "ddr:lpar:0x%llx\n", ddr_lpar);
 
        status = lv1_gpu_context_allocate(ps3fb.memory_handle, 0,
                                          &ps3fb.context_handle,
                                          &lpar_dma_control, &lpar_driver_info,
                                          &lpar_reports, &lpar_reports_size);
        if (status) {
-               printk(KERN_ERR "%s: lv1_gpu_context_attribute failed: %d\n",
-                      __func__, status);
+               dev_err(&dev->core,
+                       "%s: lv1_gpu_context_allocate failed: %d\n", __func__,
+                       status);
                goto err_gpu_memory_free;
        }
 
        /* vsync interrupt */
-       ps3fb.dinfo = ioremap(lpar_driver_info, 128 * 1024);
-       if (!ps3fb.dinfo) {
-               printk(KERN_ERR "%s: ioremap failed\n", __func__);
+       dinfo = (void __force *)ioremap(lpar_driver_info, 128 * 1024);
+       if (!dinfo) {
+               dev_err(&dev->core, "%s: ioremap failed\n", __func__);
                goto err_gpu_context_free;
        }
 
-       retval = ps3fb_vsync_settings(ps3fb.dinfo, dev);
-       if (retval)
+       ps3fb.dinfo = dinfo;
+       dev_dbg(&dev->core, "version_driver:%x\n", dinfo->version_driver);
+       dev_dbg(&dev->core, "irq outlet:%x\n", dinfo->irq.irq_outlet);
+       dev_dbg(&dev->core, "version_gpu: %x memory_size: %x ch: %x "
+               "core_freq: %d mem_freq:%d\n", dinfo->version_gpu,
+               dinfo->memory_size, dinfo->hardware_channel,
+               dinfo->nvcore_frequency/1000000,
+               dinfo->memory_frequency/1000000);
+
+       if (dinfo->version_driver != GPU_DRIVER_INFO_VERSION) {
+               dev_err(&dev->core, "%s: version_driver err:%x\n", __func__,
+                       dinfo->version_driver);
+               retval = -EINVAL;
                goto err_iounmap_dinfo;
+       }
 
-       /* xdr frame buffer */
-       ps3fb.xdr_ea = ps3fb_videomemory.address;
-       xdr_lpar = ps3_mm_phys_to_lpar(__pa(ps3fb.xdr_ea));
-       retval = ps3fb_xdr_settings(xdr_lpar);
-       if (retval)
+       retval = ps3_irq_plug_setup(PS3_BINDING_CPU_ANY, dinfo->irq.irq_outlet,
+                                   &ps3fb.irq_no);
+       if (retval) {
+               dev_err(&dev->core, "%s: ps3_alloc_irq failed %d\n", __func__,
+                       retval);
+               goto err_iounmap_dinfo;
+       }
+
+       retval = request_irq(ps3fb.irq_no, ps3fb_vsync_interrupt,
+                            IRQF_DISABLED, DEVICE_NAME, &dev->core);
+       if (retval) {
+               dev_err(&dev->core, "%s: request_irq failed %d\n", __func__,
+                       retval);
+               goto err_destroy_plug;
+       }
+
+       dinfo->irq.mask = (1 << GPU_INTR_STATUS_VSYNC_1) |
+                         (1 << GPU_INTR_STATUS_FLIP_1);
+
+       /* Clear memory to prevent kernel info leakage into userspace */
+       memset(ps3fb_videomemory.address, 0, ps3fb_videomemory.size);
+
+       xdr_lpar = ps3_mm_phys_to_lpar(__pa(ps3fb_videomemory.address));
+
+       status = lv1_gpu_context_iomap(ps3fb.context_handle, GPU_IOIF,
+                                      xdr_lpar, ps3fb_videomemory.size,
+                                      CBE_IOPTE_PP_W | CBE_IOPTE_PP_R |
+                                      CBE_IOPTE_M);
+       if (status) {
+               dev_err(&dev->core, "%s: lv1_gpu_context_iomap failed: %d\n",
+                       __func__, status);
+               retval =  -ENXIO;
                goto err_free_irq;
+       }
 
-       /*
-        * ps3fb must clear memory to prevent kernel info
-        * leakage into userspace
-        */
-       memset(ps3fb.xdr_ea, 0, ps3fb_videomemory.size);
-       info = framebuffer_alloc(sizeof(u32) * 16, &dev->dev);
+       dev_dbg(&dev->core, "video:%p ioif:%lx lpar:%llx size:%lx\n",
+               ps3fb_videomemory.address, GPU_IOIF, xdr_lpar,
+               ps3fb_videomemory.size);
+
+       status = lv1_gpu_fb_setup(ps3fb.context_handle, xdr_lpar,
+                                 GPU_CMD_BUF_SIZE, GPU_IOIF);
+       if (status) {
+               dev_err(&dev->core, "%s: lv1_gpu_fb_setup failed: %d\n",
+                       __func__, status);
+               retval = -ENXIO;
+               goto err_context_unmap;
+       }
+
+       info = framebuffer_alloc(sizeof(struct ps3fb_par), &dev->core);
        if (!info)
-               goto err_free_irq;
+               goto err_context_fb_close;
 
-       offset = FB_OFF(ps3fb.res_index) + VP_OFF(ps3fb.res_index);
-       info->screen_base = (char __iomem *)ps3fb.xdr_ea + offset;
-       info->fbops = &ps3fb_ops;
+       par = info->par;
+       par->mode_id = ~ps3fb_mode;     /* != ps3fb_mode, to trigger change */
+       par->new_mode_id = ps3fb_mode;
+       par->num_frames = 1;
 
+       info->fbops = &ps3fb_ops;
        info->fix = ps3fb_fix;
-       info->fix.smem_start = virt_to_abs(ps3fb.xdr_ea);
-       info->fix.smem_len = ps3fb_videomemory.size - offset;
-       info->pseudo_palette = info->par;
-       info->par = NULL;
-       info->flags = FBINFO_FLAG_DEFAULT;
+
+       /*
+        * The GPU command buffer is at the start of video memory
+        * As we don't use the full command buffer, we can put the actual
+        * frame buffer at offset GPU_FB_START and save some precious XDR
+        * memory
+        */
+       fb_start = ps3fb_videomemory.address + GPU_FB_START;
+       info->screen_base = (char __force __iomem *)fb_start;
+       info->fix.smem_start = virt_to_abs(fb_start);
+       info->fix.smem_len = ps3fb_videomemory.size - GPU_FB_START;
+
+       info->pseudo_palette = par->pseudo_palette;
+       info->flags = FBINFO_DEFAULT | FBINFO_READS_FAST |
+                     FBINFO_HWACCEL_XPAN | FBINFO_HWACCEL_YPAN;
 
        retval = fb_alloc_cmap(&info->cmap, 256, 0);
        if (retval < 0)
                goto err_framebuffer_release;
 
        if (!fb_find_mode(&info->var, info, mode_option, ps3fb_modedb,
-                         ARRAY_SIZE(ps3fb_modedb), ps3fb_default_mode(), 32)) {
+                         ARRAY_SIZE(ps3fb_modedb),
+                         ps3fb_vmode(par->new_mode_id), 32)) {
                retval = -EINVAL;
                goto err_fb_dealloc;
        }
@@ -1059,13 +1166,13 @@ static int __init ps3fb_probe(struct platform_device *dev)
        if (retval < 0)
                goto err_fb_dealloc;
 
-       platform_set_drvdata(dev, info);
+       ps3_system_bus_set_drvdata(dev, info);
 
-       printk(KERN_INFO
-              "fb%d: PS3 frame buffer device, using %ld KiB of video memory\n",
-              info->node, ps3fb_videomemory.size >> 10);
+       dev_info(info->device, "%s %s, using %u KiB of video memory\n",
+                dev_driver_string(info->dev), dev_name(info->dev),
+                info->fix.smem_len >> 10);
 
-       task = kthread_run(ps3fbd, info, "ps3fbd");
+       task = kthread_run(ps3fbd, info, DEVICE_NAME);
        if (IS_ERR(task)) {
                retval = PTR_ERR(task);
                goto err_unregister_framebuffer;
@@ -1081,31 +1188,36 @@ err_fb_dealloc:
        fb_dealloc_cmap(&info->cmap);
 err_framebuffer_release:
        framebuffer_release(info);
+err_context_fb_close:
+       lv1_gpu_fb_close(ps3fb.context_handle);
+err_context_unmap:
+       lv1_gpu_context_iomap(ps3fb.context_handle, GPU_IOIF, xdr_lpar,
+                             ps3fb_videomemory.size, CBE_IOPTE_M);
 err_free_irq:
-       free_irq(ps3fb.irq_no, ps3fb.dev);
+       free_irq(ps3fb.irq_no, &dev->core);
+err_destroy_plug:
        ps3_irq_plug_destroy(ps3fb.irq_no);
 err_iounmap_dinfo:
-       iounmap((u8 __iomem *)ps3fb.dinfo);
+       iounmap((u8 __force __iomem *)ps3fb.dinfo);
 err_gpu_context_free:
        lv1_gpu_context_free(ps3fb.context_handle);
 err_gpu_memory_free:
        lv1_gpu_memory_free(ps3fb.memory_handle);
+err_close_device:
+       ps3_close_hv_device(dev);
 err:
        return retval;
 }
 
-static void ps3fb_shutdown(struct platform_device *dev)
+static int ps3fb_shutdown(struct ps3_system_bus_device *dev)
 {
-       ps3fb_flip_ctl(0);      /* flip off */
-       ps3fb.dinfo->irq.mask = 0;
-       free_irq(ps3fb.irq_no, ps3fb.dev);
-       ps3_irq_plug_destroy(ps3fb.irq_no);
-       iounmap((u8 __iomem *)ps3fb.dinfo);
-}
+       struct fb_info *info = ps3_system_bus_get_drvdata(dev);
+       u64 xdr_lpar = ps3_mm_phys_to_lpar(__pa(ps3fb_videomemory.address));
 
-void ps3fb_cleanup(void)
-{
-       int status;
+       dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
+
+       atomic_inc(&ps3fb.ext_flip);    /* flip off */
+       ps3fb.dinfo->irq.mask = 0;
 
        if (ps3fb.task) {
                struct task_struct *task = ps3fb.task;
@@ -1113,147 +1225,85 @@ void ps3fb_cleanup(void)
                kthread_stop(task);
        }
        if (ps3fb.irq_no) {
-               free_irq(ps3fb.irq_no, ps3fb.dev);
+               free_irq(ps3fb.irq_no, &dev->core);
                ps3_irq_plug_destroy(ps3fb.irq_no);
        }
-       iounmap((u8 __iomem *)ps3fb.dinfo);
-
-       status = lv1_gpu_context_free(ps3fb.context_handle);
-       if (status)
-               DPRINTK("lv1_gpu_context_free failed: %d\n", status);
-
-       status = lv1_gpu_memory_free(ps3fb.memory_handle);
-       if (status)
-               DPRINTK("lv1_gpu_memory_free failed: %d\n", status);
-
-       ps3av_dev_close();
-}
-
-EXPORT_SYMBOL_GPL(ps3fb_cleanup);
-
-static int ps3fb_remove(struct platform_device *dev)
-{
-       struct fb_info *info = platform_get_drvdata(dev);
-
        if (info) {
                unregister_framebuffer(info);
                fb_dealloc_cmap(&info->cmap);
                framebuffer_release(info);
+               ps3_system_bus_set_drvdata(dev, NULL);
        }
-       ps3fb_cleanup();
+       iounmap((u8 __force __iomem *)ps3fb.dinfo);
+       lv1_gpu_fb_close(ps3fb.context_handle);
+       lv1_gpu_context_iomap(ps3fb.context_handle, GPU_IOIF, xdr_lpar,
+                             ps3fb_videomemory.size, CBE_IOPTE_M);
+       lv1_gpu_context_free(ps3fb.context_handle);
+       lv1_gpu_memory_free(ps3fb.memory_handle);
+       ps3_close_hv_device(dev);
+       dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
+
        return 0;
 }
 
-static struct platform_driver ps3fb_driver = {
-       .probe  = ps3fb_probe,
-       .remove = ps3fb_remove,
-       .shutdown = ps3fb_shutdown,
-       .driver = { .name = "ps3fb" }
-};
-
-static struct platform_device ps3fb_device = {
-       .name   = "ps3fb",
-       .id     = 0,
-       .dev    = { .release = ps3fb_platform_release }
+static struct ps3_system_bus_driver ps3fb_driver = {
+       .match_id       = PS3_MATCH_ID_GPU,
+       .match_sub_id   = PS3_MATCH_SUB_ID_GPU_FB,
+       .core.name      = DEVICE_NAME,
+       .core.owner     = THIS_MODULE,
+       .probe          = ps3fb_probe,
+       .remove         = ps3fb_shutdown,
+       .shutdown       = ps3fb_shutdown,
 };
 
-int ps3fb_set_sync(void)
+static int __init ps3fb_setup(void)
 {
-       int status;
-
-#ifdef HEAD_A
-       status = lv1_gpu_context_attribute(0x0,
-                                          L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
-                                          0, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);
-       if (status) {
-               printk(KERN_ERR
-                      "%s: lv1_gpu_context_attribute DISPLAY_SYNC failed: %d\n",
-                      __func__, status);
-               return -1;
-       }
-#endif
-#ifdef HEAD_B
-       status = lv1_gpu_context_attribute(0x0,
-                                          L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
-                                          1, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);
+       char *options;
 
-       if (status) {
-               printk(KERN_ERR
-                      "%s: lv1_gpu_context_attribute DISPLAY_MODE failed: %d\n",
-                      __func__, status);
-               return -1;
-       }
-#endif
+#ifdef MODULE
        return 0;
-}
-
-EXPORT_SYMBOL_GPL(ps3fb_set_sync);
-
-static int __init ps3fb_init(void)
-{
-       int error;
-#ifndef MODULE
-       int mode;
-       char *option = NULL;
-
-       if (fb_get_options("ps3fb", &option))
-               goto err;
 #endif
 
-       if (!ps3fb_videomemory.address)
-               goto err;
-
-       error = ps3av_dev_open();
-       if (error) {
-               printk(KERN_ERR "%s: ps3av_dev_open failed\n", __func__);
-               goto err;
-       }
+       if (fb_get_options(DEVICE_NAME, &options))
+               return -ENXIO;
 
-       ps3fb_mode = ps3av_get_mode();
-       DPRINTK("ps3av_mode:%d\n", ps3fb_mode);
-#ifndef MODULE
-       mode = ps3fb_setup(option);     /* check boot option */
-       if (mode)
-               ps3fb_mode = mode;
-#endif
-       if (ps3fb_mode > 0) {
-               u32 xres, yres;
-               ps3av_video_mode2res(ps3fb_mode, &xres, &yres);
-               ps3fb.res_index = ps3fb_get_res_table(xres, yres);
-               DPRINTK("res_index:%d\n", ps3fb.res_index);
-       } else
-               ps3fb.res_index = GPU_RES_INDEX;
+       if (!options || !*options)
+               return 0;
 
-       atomic_set(&ps3fb.f_count, -1); /* fbcon opens ps3fb */
-       atomic_set(&ps3fb.ext_flip, 0); /* for flip with vsync */
-       init_waitqueue_head(&ps3fb.wait_vsync);
-       ps3fb.num_frames = 1;
+       while (1) {
+               char *this_opt = strsep(&options, ",");
 
-       error = platform_driver_register(&ps3fb_driver);
-       if (!error) {
-               error = platform_device_register(&ps3fb_device);
-               if (error)
-                       platform_driver_unregister(&ps3fb_driver);
+               if (!this_opt)
+                       break;
+               if (!*this_opt)
+                       continue;
+               if (!strncmp(this_opt, "mode:", 5))
+                       ps3fb_mode = simple_strtoul(this_opt + 5, NULL, 0);
+               else
+                       mode_option = this_opt;
        }
+       return 0;
+}
 
-       ps3fb_set_sync();
-
-       return error;
+static int __init ps3fb_init(void)
+{
+       if (!ps3fb_videomemory.address ||  ps3fb_setup())
+               return -ENXIO;
 
-err:
-       return -ENXIO;
+       return ps3_system_bus_driver_register(&ps3fb_driver);
 }
 
-module_init(ps3fb_init);
-
-#ifdef MODULE
 static void __exit ps3fb_exit(void)
 {
-       platform_device_unregister(&ps3fb_device);
-       platform_driver_unregister(&ps3fb_driver);
+       pr_debug(" -> %s:%d\n", __func__, __LINE__);
+       ps3_system_bus_driver_unregister(&ps3fb_driver);
+       pr_debug(" <- %s:%d\n", __func__, __LINE__);
 }
 
+module_init(ps3fb_init);
 module_exit(ps3fb_exit);
 
 MODULE_LICENSE("GPL");
-#endif                         /* MODULE */
+MODULE_DESCRIPTION("PS3 GPU Frame Buffer Driver");
+MODULE_AUTHOR("Sony Computer Entertainment Inc.");
+MODULE_ALIAS(PS3_MODULE_ALIAS_GPU_FB);