]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/video/ps3fb.c
ps3fb: kill PS3FB_FULL_MODE_BIT
[linux-2.6-omap-h63xx.git] / drivers / video / ps3fb.c
index 4885978dd5be3f9ba8555ef2a1fe54059dc94e5e..069a6ed6e75be0351c80bd5f2d94cc1612fd5277 100644 (file)
 #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 */
@@ -117,13 +117,12 @@ struct ps3fb_priv {
 
        u64 context_handle, memory_handle;
        void *xdr_ea;
+       size_t xdr_size;
        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;
@@ -132,6 +131,18 @@ struct ps3fb_priv {
 };
 static struct ps3fb_priv ps3fb;
 
+struct ps3fb_par {
+       u32 pseudo_palette[16];
+       int mode_id, new_mode_id;
+       int res_index;
+       unsigned int num_frames;        /* num of frame buffers */
+       unsigned int width;
+       unsigned int height;
+       unsigned long full_offset;      /* start of fullscreen DDR fb */
+       unsigned long fb_offset;        /* start of actual DDR fb */
+       unsigned long pan_offset;
+};
+
 struct ps3fb_res_table {
        u32 xres;
        u32 yres;
@@ -280,9 +291,11 @@ static const struct fb_videomode ps3fb_modedb[] = {
 #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 */
+
+/* Start of the virtual frame buffer (relative to fullscreen ) */
+#define VP_OFF(i)      ((WIDTH(i) * Y_OFF(i) + X_OFF(i)) * BPP)
+
 
 static int ps3fb_mode;
 module_param(ps3fb_mode, int, 0);
@@ -295,7 +308,7 @@ static int ps3fb_get_res_table(u32 xres, u32 yres, int mode)
        unsigned int i;
        u32 x, y, f;
 
-       full_mode = (mode & PS3FB_FULL_MODE_BIT) ? PS3FB_RES_FULL : 0;
+       full_mode = (mode & PS3AV_MODE_FULL) ? PS3FB_RES_FULL : 0;
        for (i = 0;; i++) {
                x = ps3fb_res[i].xres;
                y = ps3fb_res[i].yres;
@@ -321,9 +334,9 @@ static int ps3fb_get_res_table(u32 xres, u32 yres, int mode)
 }
 
 static unsigned int ps3fb_find_mode(const struct fb_var_screeninfo *var,
-                                   u32 *line_length)
+                                   u32 *ddr_line_length, u32 *xdr_line_length)
 {
-       unsigned int i, mode;
+       unsigned int i, fi, mode;
 
        for (i = 0; i < ARRAY_SIZE(ps3fb_modedb); i++)
                if (var->xres == ps3fb_modedb[i].xres &&
@@ -336,89 +349,122 @@ static unsigned int ps3fb_find_mode(const struct fb_var_screeninfo *var,
                    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;
-
-                       pr_debug("ps3fb_find_mode: mode %u\n", mode);
-                       return mode;
-               }
+                   (var->vmode & FB_VMODE_MASK) == ps3fb_modedb[i].vmode)
+                       goto found;
 
        pr_debug("ps3fb_find_mode: mode not found\n");
        return 0;
 
+found:
+       /* Cropped broadcast modes use the full line length */
+       fi = i < PS3AV_MODE_1080P50 ? i + PS3AV_MODE_WUXGA : i;
+       *ddr_line_length = ps3fb_modedb[fi].xres * BPP;
+
+       if (ps3_compare_firmware_version(1, 9, 0) >= 0) {
+               *xdr_line_length = GPU_ALIGN_UP(max(var->xres,
+                                                   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;
+
+       /* Full broadcast modes have the full mode bit set */
+       mode = i+1;
+       if (mode > PS3AV_MODE_WUXGA)
+               mode = (mode - PS3AV_MODE_WUXGA) | PS3AV_MODE_FULL;
+
+       pr_debug("ps3fb_find_mode: mode %u\n", mode);
+
+       return mode;
 }
 
-static const struct fb_videomode *ps3fb_default_mode(void)
+static const struct fb_videomode *ps3fb_default_mode(int id)
 {
-       u32 mode = ps3fb_mode & PS3AV_MODE_MASK;
+       u32 mode = id & PS3AV_MODE_MASK;
        u32 flags;
 
-       if (mode < 1 || mode > 13)
+       if (mode < PS3AV_MODE_480I || mode > PS3AV_MODE_WUXGA)
                return NULL;
 
-       flags = ps3fb_mode & ~PS3AV_MODE_MASK;
+       flags = id & ~PS3AV_MODE_MASK;
 
-       if (mode <= 10 && flags & PS3FB_FULL_MODE_BIT) {
+       if (mode <= PS3AV_MODE_1080P50 && flags & PS3AV_MODE_FULL) {
                /* Full broadcast mode */
-               return &ps3fb_modedb[mode + 12];
+               return &ps3fb_modedb[mode + PS3AV_MODE_WUXGA - 1];
        }
 
        return &ps3fb_modedb[mode - 1];
 }
 
-static int ps3fb_sync(struct fb_info *info, 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;
 
-       if (frame > ps3fb.num_frames - 1) {
-               dev_dbg(info->device, "%s: invalid frame number (%u)\n",
-                       __func__, frame);
-               return -EINVAL;
-       }
-       offset = xres * yres * BPP * frame;
-
-       fb_ioif = GPU_IOIF + FB_OFF(i) + offset;
+       src_offset += GPU_FB_START;
        status = lv1_gpu_context_attribute(ps3fb.context_handle,
                                           L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT,
-                                          offset, fb_ioif,
+                                          dst_offset, GPU_IOIF + src_offset,
                                           L1GPU_FB_BLIT_WAIT_FOR_COMPLETION |
-                                          (xres << 16) | yres,
-                                          xres * BPP); /* line_length */
+                                          (width << 16) | height,
+                                          line_length);
        if (status)
-               dev_err(info->device,
+               dev_err(dev,
                        "%s: lv1_gpu_context_attribute 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);
+                                          0, frame_offset, 0, 0);
        if (status)
-               dev_err(info->device,
-                       "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
+               dev_err(dev, "%s: lv1_gpu_context_attribute 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);
+                                          1, frame_offset, 0, 0);
        if (status)
-               dev_err(info->device,
-                       "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
+               dev_err(dev, "%s: lv1_gpu_context_attribute 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 i, error = 0;
+       u32 ddr_line_length, xdr_line_length;
+       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;
+       }
+
+       i = par->res_index;
+       xdr_line_length = info->fix.line_length;
+       ddr_line_length = ps3fb_res[i].xres * BPP;
+       xdr_base = frame * info->var.yres_virtual * xdr_line_length;
+       ddr_base = frame * ps3fb_res[i].yres * ddr_line_length;
+
+       ps3fb_sync_image(info->device, ddr_base + par->full_offset,
+                        ddr_base + par->fb_offset, xdr_base + par->pan_offset,
+                        par->width, par->height, ddr_line_length,
+                        xdr_line_length);
+
+out:
+       return error;
+}
 
 static int ps3fb_open(struct fb_info *info, int user)
 {
@@ -431,7 +477,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(info, 0);    /* single buffer */
+                       if (!try_acquire_console_sem()) {
+                               ps3fb_sync(info, 0);    /* single buffer */
+                               release_console_sem();
+                       }
                }
        }
        return 0;
@@ -447,9 +496,8 @@ 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;
 
        dev_dbg(info->device, "var->xres:%u info->var.xres:%u\n", var->xres,
                info->var.xres);
@@ -457,31 +505,27 @@ static int ps3fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
                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 < var->xres)
+               var->xres_virtual = var->xres;
+       if (var->yres_virtual < var->yres)
+               var->yres_virtual = var->yres;
 
-       /* Virtual screen and panning are not supported */
-       if (var->xres_virtual > var->xres || var->yres_virtual > var->yres ||
-           var->xoffset || var->yoffset) {
+       if (var->xres_virtual > xdr_line_length / BPP) {
                dev_dbg(info->device,
-                       "Virtual screen and panning are not supported\n");
+                       "Horizontal virtual screen size too large\n");
                return -EINVAL;
        }
 
-       var->xres_virtual = var->xres;
-       var->yres_virtual = var->yres;
+       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;
+       }
 
        /* We support ARGB8888 only */
        if (var->bits_per_pixel > 32 || var->grayscale ||
@@ -516,8 +560,7 @@ static int ps3fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
        }
 
        /* Memory limit */
-       i = ps3fb_get_res_table(var->xres, var->yres, mode);
-       if (ps3fb_res[i].xres*ps3fb_res[i].yres*BPP > ps3fb_videomemory.size) {
+       if (var->yres_virtual * xdr_line_length > ps3fb.xdr_size) {
                dev_dbg(info->device, "Not enough memory\n");
                return -ENOMEM;
        }
@@ -534,34 +577,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;
+       struct ps3fb_par *par = info->par;
+       unsigned int mode, ddr_line_length, xdr_line_length, lines, maxlines;
        int i;
        unsigned long offset;
+       u64 dst;
 
        dev_dbg(info->device, "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);
 
-       mode = ps3fb_find_mode(&info->var, &info->fix.line_length);
+       mode = ps3fb_find_mode(&info->var, &ddr_line_length, &xdr_line_length);
        if (!mode)
                return -EINVAL;
 
        i = ps3fb_get_res_table(info->var.xres, info->var.yres, mode);
-       ps3fb.res_index = i;
+       par->res_index = i;
 
-       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);
+       info->fix.smem_start = virt_to_abs(ps3fb.xdr_ea);
+       info->fix.smem_len = ps3fb.xdr_size;
+       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);
+       info->screen_base = (char __iomem *)ps3fb.xdr_ea;
+
+       par->num_frames = ps3fb.xdr_size /
+                         max(ps3fb_res[i].yres * ddr_line_length,
+                             info->var.yres_virtual * xdr_line_length);
 
        /* 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;
+
+       par->width = info->var.xres;
+       par->height = info->var.yres;
+       offset = VP_OFF(i);
+       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;
+       }
 
-       if (ps3av_set_video_mode(ps3fb_mode))
-               return -EINVAL;
+       /* Clear XDR frame buffer memory */
+       memset(ps3fb.xdr_ea, 0, ps3fb.xdr_size);
+
+       /* Clear DDR frame buffer memory */
+       lines = ps3fb_res[i].yres * par->num_frames;
+       if (par->full_offset)
+               lines++;
+       maxlines = ps3fb.xdr_size / 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, ps3fb_res[i].xres, l,
+                                ddr_line_length, ddr_line_length);
+               lines -= l;
+       }
 
        return 0;
 }
@@ -589,6 +666,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
      */
@@ -596,18 +683,13 @@ 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, ps3fb_mode);
-       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;
@@ -647,7 +729,7 @@ 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;
 }
@@ -685,7 +767,7 @@ 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) {
@@ -715,6 +797,7 @@ static int ps3fb_ioctl(struct fb_info *info, unsigned int cmd,
 
        case PS3FB_IOCTL_SETMODE:
                {
+                       struct ps3fb_par *par = info->par;
                        const struct fb_videomode *mode;
                        struct fb_var_screeninfo var;
 
@@ -728,9 +811,7 @@ static int ps3fb_ioctl(struct fb_info *info, unsigned int cmd,
                        }
                        dev_dbg(info->device, "PS3FB_IOCTL_SETMODE:%x\n", val);
                        retval = -EINVAL;
-                       old_mode = ps3fb_mode;
-                       ps3fb_mode = val;
-                       mode = ps3fb_default_mode();
+                       mode = ps3fb_default_mode(val);
                        if (mode) {
                                var = info->var;
                                fb_videomode_to_var(&var, mode);
@@ -738,12 +819,11 @@ static int ps3fb_ioctl(struct fb_info *info, unsigned int cmd,
                                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;
                }
 
@@ -756,14 +836,14 @@ static int ps3fb_ioctl(struct fb_info *info, unsigned int cmd,
 
        case PS3FB_IOCTL_SCREENINFO:
                {
+                       struct ps3fb_par *par = info->par;
                        struct ps3fb_ioctl_res res;
-                       int i = ps3fb.res_index;
                        dev_dbg(info->device, "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;
+                       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;
@@ -786,7 +866,9 @@ static int ps3fb_ioctl(struct fb_info *info, unsigned int cmd,
                        break;
 
                dev_dbg(info->device, "PS3FB_IOCTL_FSEL:%d\n", val);
+               acquire_console_sem();
                retval = ps3fb_sync(info, val);
+               release_console_sem();
                break;
 
        default:
@@ -806,7 +888,9 @@ static int ps3fbd(void *arg)
                set_current_state(TASK_INTERRUPTIBLE);
                if (ps3fb.is_kicked) {
                        ps3fb.is_kicked = 0;
+                       acquire_console_sem();
                        ps3fb_sync(info, 0);    /* single buffer */
+                       release_console_sem();
                }
                schedule();
        }
@@ -899,7 +983,7 @@ static int ps3fb_xdr_settings(u64 xdr_lpar, struct device *dev)
 
        status = lv1_gpu_context_attribute(ps3fb.context_handle,
                                           L1GPU_CONTEXT_ATTRIBUTE_FB_SETUP,
-                                          xdr_lpar, ps3fb_videomemory.size,
+                                          xdr_lpar, GPU_CMD_BUF_SIZE,
                                           GPU_IOIF, 0);
        if (status) {
                dev_err(dev,
@@ -918,6 +1002,7 @@ static struct fb_ops ps3fb_ops = {
        .fb_check_var   = ps3fb_check_var,
        .fb_set_par     = ps3fb_set_par,
        .fb_setcolreg   = ps3fb_setcolreg,
+       .fb_pan_display = ps3fb_pan_display,
        .fb_fillrect    = sys_fillrect,
        .fb_copyarea    = sys_copyarea,
        .fb_imageblit   = sys_imageblit,
@@ -969,6 +1054,7 @@ static int ps3fb_set_sync(struct device *dev)
 static int __devinit ps3fb_probe(struct ps3_system_bus_device *dev)
 {
        struct fb_info *info;
+       struct ps3fb_par *par;
        int retval = -ENOMEM;
        u32 xres, yres;
        u64 ddr_lpar = 0;
@@ -977,9 +1063,14 @@ static int __devinit ps3fb_probe(struct ps3_system_bus_device *dev)
        u64 lpar_reports = 0;
        u64 lpar_reports_size = 0;
        u64 xdr_lpar;
-       int status;
-       unsigned long offset;
+       int status, res_index;
        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;
+       }
 
        status = ps3_open_hv_device(dev);
        if (status) {
@@ -990,24 +1081,30 @@ static int __devinit ps3fb_probe(struct ps3_system_bus_device *dev)
 
        if (!ps3fb_mode)
                ps3fb_mode = ps3av_get_mode();
-       dev_dbg(&dev->core, "ps3av_mode:%d\n", ps3fb_mode);
+       dev_dbg(&dev->core, "ps3fb_mode: %d\n", ps3fb_mode);
 
        if (ps3fb_mode > 0 &&
            !ps3av_video_mode2res(ps3fb_mode, &xres, &yres)) {
-               ps3fb.res_index = ps3fb_get_res_table(xres, yres, ps3fb_mode);
-               dev_dbg(&dev->core, "res_index:%d\n", ps3fb.res_index);
+               res_index = ps3fb_get_res_table(xres, yres, ps3fb_mode);
+               dev_dbg(&dev->core, "res_index:%d\n", res_index);
        } else
-               ps3fb.res_index = GPU_RES_INDEX;
+               res_index = GPU_RES_INDEX;
 
        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;
 
        ps3fb_set_sync(&dev->core);
 
+       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) {
                dev_err(&dev->core, "%s: lv1_gpu_memory_allocate failed: %d\n",
@@ -1038,39 +1135,53 @@ static int __devinit ps3fb_probe(struct ps3_system_bus_device *dev)
        if (retval)
                goto err_iounmap_dinfo;
 
-       /* xdr frame buffer */
+       /* XDR frame buffer */
        ps3fb.xdr_ea = ps3fb_videomemory.address;
        xdr_lpar = ps3_mm_phys_to_lpar(__pa(ps3fb.xdr_ea));
+
+       /* Clear memory to prevent kernel info leakage into userspace */
+       memset(ps3fb.xdr_ea, 0, ps3fb_videomemory.size);
+
+       /*
+        * 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
+        */
+       ps3fb.xdr_ea += GPU_FB_START;
+       ps3fb.xdr_size = ps3fb_videomemory.size - GPU_FB_START;
+
        retval = ps3fb_xdr_settings(xdr_lpar, &dev->core);
        if (retval)
                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->core);
+       info = framebuffer_alloc(sizeof(struct ps3fb_par), &dev->core);
        if (!info)
                goto err_free_irq;
 
-       offset = FB_OFF(ps3fb.res_index) + VP_OFF(ps3fb.res_index);
-       info->screen_base = (char __iomem *)ps3fb.xdr_ea + offset;
+       par = info->par;
+       par->mode_id = ~ps3fb_mode;     /* != ps3fb_mode, to trigger change */
+       par->new_mode_id = ps3fb_mode;
+       par->res_index = res_index;
+       par->num_frames = 1;
+
+       info->screen_base = (char __iomem *)ps3fb.xdr_ea;
        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_DEFAULT | FBINFO_READS_FAST;
+       info->fix.smem_len = ps3fb.xdr_size;
+       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_default_mode(par->new_mode_id), 32)) {
                retval = -EINVAL;
                goto err_fb_dealloc;
        }
@@ -1086,7 +1197,7 @@ static int __devinit ps3fb_probe(struct ps3_system_bus_device *dev)
 
        dev_info(info->device, "%s %s, using %lu KiB of video memory\n",
                 dev_driver_string(info->dev), info->dev->bus_id,
-                ps3fb_videomemory.size >> 10);
+                ps3fb.xdr_size >> 10);
 
        task = kthread_run(ps3fbd, info, DEVICE_NAME);
        if (IS_ERR(task)) {
@@ -1106,7 +1217,7 @@ err_fb_dealloc:
 err_framebuffer_release:
        framebuffer_release(info);
 err_free_irq:
-       free_irq(ps3fb.irq_no, dev);
+       free_irq(ps3fb.irq_no, &dev->core);
        ps3_irq_plug_destroy(ps3fb.irq_no);
 err_iounmap_dinfo:
        iounmap((u8 __iomem *)ps3fb.dinfo);
@@ -1128,12 +1239,6 @@ static int ps3fb_shutdown(struct ps3_system_bus_device *dev)
        ps3fb_flip_ctl(0, &ps3fb);      /* flip off */
        ps3fb.dinfo->irq.mask = 0;
 
-       if (info) {
-               unregister_framebuffer(info);
-               fb_dealloc_cmap(&info->cmap);
-               framebuffer_release(info);
-       }
-
        ps3av_register_flip_ctl(NULL, NULL);
        if (ps3fb.task) {
                struct task_struct *task = ps3fb.task;
@@ -1141,9 +1246,15 @@ static int ps3fb_shutdown(struct ps3_system_bus_device *dev)
                kthread_stop(task);
        }
        if (ps3fb.irq_no) {
-               free_irq(ps3fb.irq_no, dev);
+               free_irq(ps3fb.irq_no, &dev->core);
                ps3_irq_plug_destroy(ps3fb.irq_no);
        }
+       if (info) {
+               unregister_framebuffer(info);
+               fb_dealloc_cmap(&info->cmap);
+               framebuffer_release(info);
+               info = dev->core.driver_data = NULL;
+       }
        iounmap((u8 __iomem *)ps3fb.dinfo);
 
        status = lv1_gpu_context_free(ps3fb.context_handle);