From: Alexandru Dadu <[email protected]>

The "gpuid" module parameter is used to override the gpuid read from a
hardware register and is useful for testing the loading of different
firmware (including processing of the firmware header) without having
the hardware to hand.

Signed-off-by: Alexandru Dadu <[email protected]>
Signed-off-by: Matt Coster <[email protected]>
---
 drivers/gpu/drm/imagination/pvr_device.c | 117 ++++++++++++++++++++++++++++---
 drivers/gpu/drm/imagination/pvr_device.h |   7 +-
 2 files changed, 114 insertions(+), 10 deletions(-)

diff --git a/drivers/gpu/drm/imagination/pvr_device.c 
b/drivers/gpu/drm/imagination/pvr_device.c
index abe8ad1d447ac..db844e4e2e945 100644
--- a/drivers/gpu/drm/imagination/pvr_device.c
+++ b/drivers/gpu/drm/imagination/pvr_device.c
@@ -421,23 +421,21 @@ pvr_request_firmware(struct pvr_device *pvr_dev)
 }
 
 /**
- * pvr_load_gpu_id() - Load a PowerVR device's GPU ID (BVNC) from control 
registers.
+ * pvr_gpuid_decode_reg() - Decode the GPU ID from GPU register
  *
- * Sets struct pvr_dev.gpu_id.
+ * Sets the b, v, n, c fields of struct pvr_dev.gpu_id.
  *
  * @pvr_dev: Target PowerVR device.
+ * @gpu_id: Output to be updated with the GPU ID.
  */
 static void
-pvr_load_gpu_id(struct pvr_device *pvr_dev)
+pvr_gpuid_decode_reg(const struct pvr_device *pvr_dev, struct pvr_gpu_id 
*gpu_id)
 {
-       struct pvr_gpu_id *gpu_id = &pvr_dev->gpu_id;
-       u64 bvnc;
-
        /*
         * Try reading the BVNC using the newer (cleaner) method first. If the
         * B value is zero, fall back to the older method.
         */
-       bvnc = pvr_cr_read64(pvr_dev, ROGUE_CR_CORE_ID__PBVNC);
+       u64 bvnc = pvr_cr_read64(pvr_dev, ROGUE_CR_CORE_ID__PBVNC);
 
        gpu_id->b = PVR_CR_FIELD_GET(bvnc, CORE_ID__PBVNC__BRANCH_ID);
        if (gpu_id->b != 0) {
@@ -456,6 +454,107 @@ pvr_load_gpu_id(struct pvr_device *pvr_dev)
        }
 }
 
+/**
+ * pvr_gpuid_decode_string() - Decode the GPU ID from a module input string
+ *
+ * Sets the b, v, n, c fields of struct pvr_dev.gpu_id.
+ *
+ * @pvr_dev: Target PowerVR device.
+ * @param_bvnc: GPU ID (BVNC) module parameter.
+ * @gpu_id: Output to be updated with the GPU ID.
+ */
+static int
+pvr_gpuid_decode_string(const struct pvr_device *pvr_dev,
+                       const char *param_bvnc, struct pvr_gpu_id *gpu_id)
+{
+       const struct drm_device *drm_dev = &pvr_dev->base;
+       char str_cpy[PVR_GPUID_STRING_MAX_LENGTH];
+       char *pos, *tkn;
+       int ret, idx = 0;
+       u16 user_bvnc_u16[4];
+       u8 dot_cnt = 0;
+
+       ret = strscpy(str_cpy, param_bvnc);
+
+       /*
+        * strscpy() should return at least a size 7 for the input to be valid.
+        * Returns -E2BIG for the case when the string is empty or too long.
+        */
+       if (ret < PVR_GPUID_STRING_MIN_LENGTH) {
+               drm_info(drm_dev,
+                        "Invalid size of the input GPU ID (BVNC): %s",
+                        str_cpy);
+               return -EINVAL;
+       }
+
+       while (*param_bvnc) {
+               if (*param_bvnc == '.')
+                       dot_cnt++;
+               param_bvnc++;
+       }
+
+       if (dot_cnt != 3) {
+               drm_info(drm_dev,
+                        "Invalid format of the input GPU ID (BVNC): %s",
+                        str_cpy);
+               return -EINVAL;
+       }
+
+       pos = str_cpy;
+
+       while ((tkn = strsep(&pos, ".")) != NULL && idx < 4) {
+               /* kstrtou16() will also handle the case of consecutive dots */
+               ret = kstrtou16(tkn, 10, &user_bvnc_u16[idx]);
+               if (ret) {
+                       drm_info(drm_dev,
+                                "Invalid format of the input GPU ID (BVNC): 
%s",
+                                str_cpy);
+                       return -EINVAL;
+               }
+               idx++;
+       }
+
+       gpu_id->b = user_bvnc_u16[0];
+       gpu_id->v = user_bvnc_u16[1];
+       gpu_id->n = user_bvnc_u16[2];
+       gpu_id->c = user_bvnc_u16[3];
+
+       return 0;
+}
+
+static char *pvr_gpuid_override;
+module_param_named(gpuid, pvr_gpuid_override, charp, 0400);
+MODULE_PARM_DESC(gpuid, "GPU ID (BVNC) to be used instead of the value read 
from hardware.");
+
+/**
+ * pvr_load_gpu_id() - Load a PowerVR device's GPU ID (BVNC) from control
+ * registers or input parameter. The input parameter is processed instead
+ * of the GPU register if provided.
+ *
+ * Sets the arch field of struct pvr_dev.gpu_id.
+ *
+ * @pvr_dev: Target PowerVR device.
+ */
+static int
+pvr_load_gpu_id(struct pvr_device *pvr_dev)
+{
+       struct pvr_gpu_id *gpu_id = &pvr_dev->gpu_id;
+
+       if (!pvr_gpuid_override || !pvr_gpuid_override[0]) {
+               pvr_gpuid_decode_reg(pvr_dev, gpu_id);
+       } else {
+               drm_warn(from_pvr_device(pvr_dev),
+                        "Using custom GPU ID (BVNC) provided by the user!");
+
+               int err = pvr_gpuid_decode_string(pvr_dev, pvr_gpuid_override,
+                                                 gpu_id);
+               if (err)
+                       return err;
+       }
+
+       return 0;
+}
+
 /**
  * pvr_set_dma_info() - Set PowerVR device DMA information
  * @pvr_dev: Target PowerVR device.
@@ -516,7 +615,9 @@ pvr_device_gpu_init(struct pvr_device *pvr_dev)
 {
        int err;
 
-       pvr_load_gpu_id(pvr_dev);
+       err = pvr_load_gpu_id(pvr_dev);
+       if (err)
+               return err;
 
        err = pvr_request_firmware(pvr_dev);
        if (err)
diff --git a/drivers/gpu/drm/imagination/pvr_device.h 
b/drivers/gpu/drm/imagination/pvr_device.h
index d0e61923fd9b4..5608a977f6d21 100644
--- a/drivers/gpu/drm/imagination/pvr_device.h
+++ b/drivers/gpu/drm/imagination/pvr_device.h
@@ -39,6 +39,9 @@ struct firmware;
 /* Forward declaration from <linux/pwrseq/consumer.h> */
 struct pwrseq_desc;
 
+#define PVR_GPUID_STRING_MIN_LENGTH 7U
+#define PVR_GPUID_STRING_MAX_LENGTH 32U
+
 /**
  * struct pvr_gpu_id - Hardware GPU ID information for a PowerVR device
  * @b: Branch ID.
@@ -558,7 +561,7 @@ pvr_device_has_feature(struct pvr_device *pvr_dev, u32 
feature);
  * Return: The value of the requested register.
  */
 static __always_inline u32
-pvr_cr_read32(struct pvr_device *pvr_dev, u32 reg)
+pvr_cr_read32(const struct pvr_device *pvr_dev, u32 reg)
 {
        return ioread32(pvr_dev->regs + reg);
 }
@@ -571,7 +574,7 @@ pvr_cr_read32(struct pvr_device *pvr_dev, u32 reg)
  * Return: The value of the requested register.
  */
 static __always_inline u64
-pvr_cr_read64(struct pvr_device *pvr_dev, u32 reg)
+pvr_cr_read64(const struct pvr_device *pvr_dev, u32 reg)
 {
        return ioread64(pvr_dev->regs + reg);
 }

-- 
2.52.0

Reply via email to