summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Zimmermann <tzimmermann@suse.de>2025-04-10 10:37:24 +0200
committerThomas Zimmermann <tzimmermann@suse.de>2025-04-14 10:16:13 +0200
commit6046b49bafff47726a377ef05dc55ef7dec01cbd (patch)
tree07e8e6677459919d93e19e6edaedc63a0c2f281b
parent314c45e39e9abcaf2fe5449a11b6d9ad3b2c7dbc (diff)
drm/sysfb: Share helpers for integer validation
Provide sysfb helpers for validating framebuffer integer values against limits. Update drivers. If a driver did not specify a limit for a certain value, use INT_MAX. v2: - declare module information near EOF (Javier) Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de> Reviewed-by: Javier Martinez Canillas <javierm@redhat.com> Link: https://lore.kernel.org/r/20250410083834.10810-3-tzimmermann@suse.de
-rw-r--r--drivers/gpu/drm/sysfb/drm_sysfb.c27
-rw-r--r--drivers/gpu/drm/sysfb/drm_sysfb_helper.h9
-rw-r--r--drivers/gpu/drm/sysfb/efidrm.c29
-rw-r--r--drivers/gpu/drm/sysfb/ofdrm.c12
-rw-r--r--drivers/gpu/drm/sysfb/simpledrm.c14
-rw-r--r--drivers/gpu/drm/sysfb/vesadrm.c29
6 files changed, 48 insertions, 72 deletions
diff --git a/drivers/gpu/drm/sysfb/drm_sysfb.c b/drivers/gpu/drm/sysfb/drm_sysfb.c
index c083d21fd9ca..308f82153b15 100644
--- a/drivers/gpu/drm/sysfb/drm_sysfb.c
+++ b/drivers/gpu/drm/sysfb/drm_sysfb.c
@@ -1,8 +1,35 @@
// SPDX-License-Identifier: GPL-2.0-only
+#include <linux/export.h>
+#include <linux/limits.h>
+#include <linux/minmax.h>
#include <linux/module.h>
+#include <drm/drm_print.h>
+
#include "drm_sysfb_helper.h"
+int drm_sysfb_get_validated_int(struct drm_device *dev, const char *name,
+ u64 value, u32 max)
+{
+ if (value > min(max, INT_MAX)) {
+ drm_warn(dev, "%s of %llu exceeds maximum of %u\n", name, value, max);
+ return -EINVAL;
+ }
+ return value;
+}
+EXPORT_SYMBOL(drm_sysfb_get_validated_int);
+
+int drm_sysfb_get_validated_int0(struct drm_device *dev, const char *name,
+ u64 value, u32 max)
+{
+ if (!value) {
+ drm_warn(dev, "%s of 0 not allowed\n", name);
+ return -EINVAL;
+ }
+ return drm_sysfb_get_validated_int(dev, name, value, max);
+}
+EXPORT_SYMBOL(drm_sysfb_get_validated_int0);
+
MODULE_DESCRIPTION("Helpers for DRM sysfb drivers");
MODULE_LICENSE("GPL");
diff --git a/drivers/gpu/drm/sysfb/drm_sysfb_helper.h b/drivers/gpu/drm/sysfb/drm_sysfb_helper.h
index ee94d6199b60..1697cf7ace97 100644
--- a/drivers/gpu/drm/sysfb/drm_sysfb_helper.h
+++ b/drivers/gpu/drm/sysfb/drm_sysfb_helper.h
@@ -14,6 +14,15 @@ struct drm_format_info;
struct drm_scanout_buffer;
/*
+ * Input parsing
+ */
+
+int drm_sysfb_get_validated_int(struct drm_device *dev, const char *name,
+ u64 value, u32 max);
+int drm_sysfb_get_validated_int0(struct drm_device *dev, const char *name,
+ u64 value, u32 max);
+
+/*
* Display modes
*/
diff --git a/drivers/gpu/drm/sysfb/efidrm.c b/drivers/gpu/drm/sysfb/efidrm.c
index 3cfd5d2cbf48..e3c7c930e5ad 100644
--- a/drivers/gpu/drm/sysfb/efidrm.c
+++ b/drivers/gpu/drm/sysfb/efidrm.c
@@ -33,28 +33,6 @@
#define DRIVER_MAJOR 1
#define DRIVER_MINOR 0
-static int efidrm_get_validated_int(struct drm_device *dev, const char *name,
- u64 value, u32 max)
-{
- if (max > INT_MAX)
- max = INT_MAX;
- if (value > max) {
- drm_err(dev, "%s of %llu exceeds maximum of %u\n", name, value, max);
- return -EINVAL;
- }
- return value;
-}
-
-static int efidrm_get_validated_int0(struct drm_device *dev, const char *name,
- u64 value, u32 max)
-{
- if (!value) {
- drm_err(dev, "%s of 0 not allowed\n", name);
- return -EINVAL;
- }
- return efidrm_get_validated_int(dev, name, value, max);
-}
-
static s64 efidrm_get_validated_size0(struct drm_device *dev, const char *name,
u64 value, u64 max)
{
@@ -70,12 +48,12 @@ static s64 efidrm_get_validated_size0(struct drm_device *dev, const char *name,
static int efidrm_get_width_si(struct drm_device *dev, const struct screen_info *si)
{
- return efidrm_get_validated_int0(dev, "width", si->lfb_width, U16_MAX);
+ return drm_sysfb_get_validated_int0(dev, "width", si->lfb_width, U16_MAX);
}
static int efidrm_get_height_si(struct drm_device *dev, const struct screen_info *si)
{
- return efidrm_get_validated_int0(dev, "height", si->lfb_height, U16_MAX);
+ return drm_sysfb_get_validated_int0(dev, "height", si->lfb_height, U16_MAX);
}
static struct resource *efidrm_get_memory_si(struct drm_device *dev,
@@ -102,7 +80,8 @@ static int efidrm_get_stride_si(struct drm_device *dev, const struct screen_info
if (!lfb_linelength)
lfb_linelength = drm_format_info_min_pitch(format, 0, width);
- return efidrm_get_validated_int0(dev, "stride", lfb_linelength, div64_u64(size, height));
+ return drm_sysfb_get_validated_int0(dev, "stride", lfb_linelength,
+ div64_u64(size, height));
}
static u64 efidrm_get_visible_size_si(struct drm_device *dev, const struct screen_info *si,
diff --git a/drivers/gpu/drm/sysfb/ofdrm.c b/drivers/gpu/drm/sysfb/ofdrm.c
index 86c1a0c80ceb..fddfe8bea9f7 100644
--- a/drivers/gpu/drm/sysfb/ofdrm.c
+++ b/drivers/gpu/drm/sysfb/ofdrm.c
@@ -78,20 +78,12 @@ enum ofdrm_model {
static int display_get_validated_int(struct drm_device *dev, const char *name, uint32_t value)
{
- if (value > INT_MAX) {
- drm_err(dev, "invalid framebuffer %s of %u\n", name, value);
- return -EINVAL;
- }
- return (int)value;
+ return drm_sysfb_get_validated_int(dev, name, value, INT_MAX);
}
static int display_get_validated_int0(struct drm_device *dev, const char *name, uint32_t value)
{
- if (!value) {
- drm_err(dev, "invalid framebuffer %s of %u\n", name, value);
- return -EINVAL;
- }
- return display_get_validated_int(dev, name, value);
+ return drm_sysfb_get_validated_int0(dev, name, value, INT_MAX);
}
static const struct drm_format_info *display_get_validated_format(struct drm_device *dev,
diff --git a/drivers/gpu/drm/sysfb/simpledrm.c b/drivers/gpu/drm/sysfb/simpledrm.c
index f37b1994de71..a1c3119330de 100644
--- a/drivers/gpu/drm/sysfb/simpledrm.c
+++ b/drivers/gpu/drm/sysfb/simpledrm.c
@@ -42,24 +42,14 @@ static int
simplefb_get_validated_int(struct drm_device *dev, const char *name,
uint32_t value)
{
- if (value > INT_MAX) {
- drm_err(dev, "simplefb: invalid framebuffer %s of %u\n",
- name, value);
- return -EINVAL;
- }
- return (int)value;
+ return drm_sysfb_get_validated_int(dev, name, value, INT_MAX);
}
static int
simplefb_get_validated_int0(struct drm_device *dev, const char *name,
uint32_t value)
{
- if (!value) {
- drm_err(dev, "simplefb: invalid framebuffer %s of %u\n",
- name, value);
- return -EINVAL;
- }
- return simplefb_get_validated_int(dev, name, value);
+ return drm_sysfb_get_validated_int0(dev, name, value, INT_MAX);
}
static const struct drm_format_info *
diff --git a/drivers/gpu/drm/sysfb/vesadrm.c b/drivers/gpu/drm/sysfb/vesadrm.c
index 9cc50e3072ea..d87ff77be20d 100644
--- a/drivers/gpu/drm/sysfb/vesadrm.c
+++ b/drivers/gpu/drm/sysfb/vesadrm.c
@@ -36,28 +36,6 @@
#define VESADRM_GAMMA_LUT_SIZE 256
-static int vesadrm_get_validated_int(struct drm_device *dev, const char *name,
- u64 value, u32 max)
-{
- if (max > INT_MAX)
- max = INT_MAX;
- if (value > max) {
- drm_err(dev, "%s of %llu exceeds maximum of %u\n", name, value, max);
- return -EINVAL;
- }
- return value;
-}
-
-static int vesadrm_get_validated_int0(struct drm_device *dev, const char *name,
- u64 value, u32 max)
-{
- if (!value) {
- drm_err(dev, "%s of 0 not allowed\n", name);
- return -EINVAL;
- }
- return vesadrm_get_validated_int(dev, name, value, max);
-}
-
static s64 vesadrm_get_validated_size0(struct drm_device *dev, const char *name,
u64 value, u64 max)
{
@@ -73,12 +51,12 @@ static s64 vesadrm_get_validated_size0(struct drm_device *dev, const char *name,
static int vesadrm_get_width_si(struct drm_device *dev, const struct screen_info *si)
{
- return vesadrm_get_validated_int0(dev, "width", si->lfb_width, U16_MAX);
+ return drm_sysfb_get_validated_int0(dev, "width", si->lfb_width, U16_MAX);
}
static int vesadrm_get_height_si(struct drm_device *dev, const struct screen_info *si)
{
- return vesadrm_get_validated_int0(dev, "height", si->lfb_height, U16_MAX);
+ return drm_sysfb_get_validated_int0(dev, "height", si->lfb_height, U16_MAX);
}
static struct resource *vesadrm_get_memory_si(struct drm_device *dev,
@@ -105,7 +83,8 @@ static int vesadrm_get_stride_si(struct drm_device *dev, const struct screen_inf
if (!lfb_linelength)
lfb_linelength = drm_format_info_min_pitch(format, 0, width);
- return vesadrm_get_validated_int0(dev, "stride", lfb_linelength, div64_u64(size, height));
+ return drm_sysfb_get_validated_int0(dev, "stride", lfb_linelength,
+ div64_u64(size, height));
}
static u64 vesadrm_get_visible_size_si(struct drm_device *dev, const struct screen_info *si,