]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
drm/fourcc: Add warning for bad bpp
authorTomi Valkeinen <tomi.valkeinen@ideasonboard.com>
Thu, 23 Apr 2026 14:21:12 +0000 (17:21 +0300)
committerTomi Valkeinen <tomi.valkeinen@ideasonboard.com>
Tue, 5 May 2026 11:04:29 +0000 (14:04 +0300)
drm_format_info_bpp() cannot be used for formats which do not have an
integer bits-per-pixel in a pixel block.

E.g. DRM_FORMAT_P030's plane 0 has three 10-bit pixels (Y components),
and two padding bits, in a 4 byte block. That is 10.666... bits per
pixel when considering the whole 4 byte block, which is what
drm_format_info_bpp() does. Thus a driver that supports such formats
cannot use drm_format_info_bpp(),

It is a driver bug if this happens, but so handle wrong calls by
printing a warning and returning 0.

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Dmitry Baryshkov <dmitry.baryshkov@oss.qualcomm.com>
Acked-by: Simon Ser <contact@emersion.fr>
Link: https://patch.msgid.link/20260423-xilinx-formats-v10-1-c690c2b8ea89@ideasonboard.com
Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
drivers/gpu/drm/drm_fourcc.c

index e0d5336110404c96c138fe27349463bc852e001a..e662aea9d1057ad529fba9250f6dc39b2e5fd39b 100644 (file)
@@ -491,12 +491,20 @@ EXPORT_SYMBOL(drm_format_info_block_height);
  */
 unsigned int drm_format_info_bpp(const struct drm_format_info *info, int plane)
 {
+       unsigned int block_size;
+
        if (!info || plane < 0 || plane >= info->num_planes)
                return 0;
 
-       return info->char_per_block[plane] * 8 /
-              (drm_format_info_block_width(info, plane) *
-               drm_format_info_block_height(info, plane));
+       block_size = drm_format_info_block_width(info, plane) *
+                    drm_format_info_block_height(info, plane);
+
+       if (info->char_per_block[plane] * 8 % block_size) {
+               pr_warn("unable to return an integer bpp\n");
+               return 0;
+       }
+
+       return info->char_per_block[plane] * 8 / block_size;
 }
 EXPORT_SYMBOL(drm_format_info_bpp);