Replace the geometry and size calculation in armada's fbdev emulation
with DRM format helpers. This consists of a 4CC lookup from the fbdev
parameters, format lookup, pitch calculation and size calculation.

Then allocate the GEM buffer object for the framebuffer memory from
the calculated size. Mmap provides the allocated buffer to user space,
so align the buffer size to PAGE_SIZE.

Slightly rearrange cleaning up the GEM object and rolling back on
errors. This will simplify error handling when the code uses client
buffers.

v2:
- add more error checks to geometry calculations

Signed-off-by: Thomas Zimmermann <[email protected]>
---
 drivers/gpu/drm/armada/armada_fbdev.c | 65 ++++++++++++++++-----------
 1 file changed, 39 insertions(+), 26 deletions(-)

diff --git a/drivers/gpu/drm/armada/armada_fbdev.c 
b/drivers/gpu/drm/armada/armada_fbdev.c
index 8bbae94804f8..f95658091acf 100644
--- a/drivers/gpu/drm/armada/armada_fbdev.c
+++ b/drivers/gpu/drm/armada/armada_fbdev.c
@@ -45,20 +45,30 @@ int armada_fbdev_driver_fbdev_probe(struct drm_fb_helper 
*fbh,
 {
        struct drm_device *dev = fbh->dev;
        struct fb_info *info = fbh->info;
+       u32 fourcc, pitch;
+       u64 size;
+       const struct drm_format_info *format;
        struct drm_mode_fb_cmd2 mode;
        struct armada_framebuffer *dfb;
        struct armada_gem_object *obj;
-       int size, ret;
+       int ret;
        void *ptr;
 
-       memset(&mode, 0, sizeof(mode));
-       mode.width = sizes->surface_width;
-       mode.height = sizes->surface_height;
-       mode.pitches[0] = armada_pitch(mode.width, sizes->surface_bpp);
-       mode.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
-                                       sizes->surface_depth);
+       fourcc = drm_mode_legacy_fb_format(sizes->surface_bpp, 
sizes->surface_depth);
+       if (fourcc == DRM_FORMAT_INVALID)
+               return -EINVAL;
+       format = drm_get_format_info(dev, fourcc, DRM_FORMAT_MOD_LINEAR);
+       if (!format)
+               return -EINVAL;
+       pitch = armada_pitch(sizes->surface_width, drm_format_info_bpp(format, 
0));
+       if (!pitch)
+               return -EINVAL;
+       if (check_mul_overflow(pitch, sizes->surface_height, &size))
+               return -EINVAL;
+       size = ALIGN(size, PAGE_SIZE);
+       if (size < PAGE_SIZE)
+               return -EINVAL;
 
-       size = mode.pitches[0] * mode.height;
        obj = armada_gem_alloc_private_object(dev, size);
        if (!obj) {
                DRM_ERROR("failed to allocate fb memory\n");
@@ -66,30 +76,26 @@ int armada_fbdev_driver_fbdev_probe(struct drm_fb_helper 
*fbh,
        }
 
        ret = armada_gem_linear_back(dev, obj);
-       if (ret) {
-               drm_gem_object_put(&obj->obj);
-               return ret;
-       }
+       if (ret)
+               goto err_drm_gem_object_put;
 
        ptr = armada_gem_map_object(dev, obj);
        if (!ptr) {
-               drm_gem_object_put(&obj->obj);
-               return -ENOMEM;
+               ret = -ENOMEM;
+               goto err_drm_gem_object_put;
        }
 
-       dfb = armada_framebuffer_create(dev,
-                                       drm_get_format_info(dev, 
mode.pixel_format,
-                                                           mode.modifier[0]),
-                                       &mode, obj);
-
-       /*
-        * A reference is now held by the framebuffer object if
-        * successful, otherwise this drops the ref for the error path.
-        */
-       drm_gem_object_put(&obj->obj);
+       memset(&mode, 0, sizeof(mode));
+       mode.width = sizes->surface_width;
+       mode.height = sizes->surface_height;
+       mode.pitches[0] = pitch;
+       mode.pixel_format = fourcc;
 
-       if (IS_ERR(dfb))
-               return PTR_ERR(dfb);
+       dfb = armada_framebuffer_create(dev, format, &mode, obj);
+       if (IS_ERR(dfb)) {
+               ret = PTR_ERR(dfb);
+               goto err_drm_gem_object_put;
+       }
 
        info->fbops = &armada_fb_ops;
        info->fix.smem_start = obj->phys_addr;
@@ -105,5 +111,12 @@ int armada_fbdev_driver_fbdev_probe(struct drm_fb_helper 
*fbh,
                dfb->fb.width, dfb->fb.height, dfb->fb.format->cpp[0] * 8,
                (unsigned long long)obj->phys_addr);
 
+       /* The framebuffer still holds a reference on the GEM object. */
+       drm_gem_object_put(&obj->obj);
+
        return 0;
+
+err_drm_gem_object_put:
+       drm_gem_object_put(&obj->obj);
+       return ret;
 }
-- 
2.54.0

Reply via email to