diff mbox series

[05/10] drm/i915 & drm/xe: save struct drm_device to drvdata

Message ID c479244bb25e915c6467f76e48a3260455454dd9.1722263308.git.jani.nikula@intel.com (mailing list archive)
State New, archived
Headers show
Series drm/xe & drm/i915: drvdata usage changes | expand

Commit Message

Jani Nikula July 29, 2024, 2:30 p.m. UTC
In the future, the display code shall not have any idea about struct
xe_device or struct drm_i915_private, but will need to get at the struct
drm_device via drvdata. Store the struct drm_device pointer to drvdata
instead of the driver specific pointer.

Even though struct drm_device is embedded in both struct xe_device and
struct drm_i915_private at offset 0, take care to check for NULL before
using container_of() to allow for other offsets.

Signed-off-by: Jani Nikula <jani.nikula@intel.com>
---
 drivers/gpu/drm/i915/i915_driver.c                | 2 +-
 drivers/gpu/drm/i915/i915_drv.h                   | 8 ++++++--
 drivers/gpu/drm/i915/selftests/mock_gem_device.c  | 2 +-
 drivers/gpu/drm/xe/compat-i915-headers/i915_drv.h | 4 +++-
 drivers/gpu/drm/xe/xe_device.h                    | 8 ++++++--
 drivers/gpu/drm/xe/xe_pci.c                       | 2 +-
 6 files changed, 18 insertions(+), 8 deletions(-)

Comments

Gustavo Sousa Aug. 1, 2024, 5:38 p.m. UTC | #1
Quoting Jani Nikula (2024-07-29 11:30:06-03:00)
>In the future, the display code shall not have any idea about struct
>xe_device or struct drm_i915_private, but will need to get at the struct
>drm_device via drvdata. Store the struct drm_device pointer to drvdata
>instead of the driver specific pointer.
>
>Even though struct drm_device is embedded in both struct xe_device and
>struct drm_i915_private at offset 0, take care to check for NULL before
>using container_of() to allow for other offsets.

I think the second half of this paragraph could be rephrased. One might
think that the text is suggesting that checking for NULL has something
to do with allowing other offsets.

I would jump directly to mention using container_of() and would assume
that it is implied that NULL check is necessary before using it. :-)

Reviewed-by: Gustavo Sousa <gustavo.sousa@intel.com>

>
>Signed-off-by: Jani Nikula <jani.nikula@intel.com>
>---
> drivers/gpu/drm/i915/i915_driver.c                | 2 +-
> drivers/gpu/drm/i915/i915_drv.h                   | 8 ++++++--
> drivers/gpu/drm/i915/selftests/mock_gem_device.c  | 2 +-
> drivers/gpu/drm/xe/compat-i915-headers/i915_drv.h | 4 +++-
> drivers/gpu/drm/xe/xe_device.h                    | 8 ++++++--
> drivers/gpu/drm/xe/xe_pci.c                       | 2 +-
> 6 files changed, 18 insertions(+), 8 deletions(-)
>
>diff --git a/drivers/gpu/drm/i915/i915_driver.c b/drivers/gpu/drm/i915/i915_driver.c
>index fb8e9c2fcea5..176c13c2e191 100644
>--- a/drivers/gpu/drm/i915/i915_driver.c
>+++ b/drivers/gpu/drm/i915/i915_driver.c
>@@ -723,7 +723,7 @@ i915_driver_create(struct pci_dev *pdev, const struct pci_device_id *ent)
>         if (IS_ERR(i915))
>                 return i915;
> 
>-        pci_set_drvdata(pdev, i915);
>+        pci_set_drvdata(pdev, &i915->drm);
> 
>         /* Device parameters start as a copy of module parameters. */
>         i915_params_copy(&i915->params, &i915_modparams);
>diff --git a/drivers/gpu/drm/i915/i915_drv.h b/drivers/gpu/drm/i915/i915_drv.h
>index d7723dd11c80..f6edaef73db5 100644
>--- a/drivers/gpu/drm/i915/i915_drv.h
>+++ b/drivers/gpu/drm/i915/i915_drv.h
>@@ -365,12 +365,16 @@ static inline struct drm_i915_private *to_i915(const struct drm_device *dev)
> 
> static inline struct drm_i915_private *kdev_to_i915(struct device *kdev)
> {
>-        return dev_get_drvdata(kdev);
>+        struct drm_device *drm = dev_get_drvdata(kdev);
>+
>+        return drm ? to_i915(drm) : NULL;
> }
> 
> static inline struct drm_i915_private *pdev_to_i915(struct pci_dev *pdev)
> {
>-        return pci_get_drvdata(pdev);
>+        struct drm_device *drm = pci_get_drvdata(pdev);
>+
>+        return drm ? to_i915(drm) : NULL;
> }
> 
> static inline struct intel_gt *to_gt(const struct drm_i915_private *i915)
>diff --git a/drivers/gpu/drm/i915/selftests/mock_gem_device.c b/drivers/gpu/drm/i915/selftests/mock_gem_device.c
>index 0bd29846873b..91794ca17a58 100644
>--- a/drivers/gpu/drm/i915/selftests/mock_gem_device.c
>+++ b/drivers/gpu/drm/i915/selftests/mock_gem_device.c
>@@ -172,7 +172,7 @@ struct drm_i915_private *mock_gem_device(void)
>                 return NULL;
>         }
> 
>-        pci_set_drvdata(pdev, i915);
>+        pci_set_drvdata(pdev, &i915->drm);
> 
>         /* Device parameters start as a copy of module parameters. */
>         i915_params_copy(&i915->params, &i915_modparams);
>diff --git a/drivers/gpu/drm/xe/compat-i915-headers/i915_drv.h b/drivers/gpu/drm/xe/compat-i915-headers/i915_drv.h
>index 2feedddf1e40..766fba88a3c8 100644
>--- a/drivers/gpu/drm/xe/compat-i915-headers/i915_drv.h
>+++ b/drivers/gpu/drm/xe/compat-i915-headers/i915_drv.h
>@@ -23,7 +23,9 @@ static inline struct drm_i915_private *to_i915(const struct drm_device *dev)
> 
> static inline struct drm_i915_private *kdev_to_i915(struct device *kdev)
> {
>-        return dev_get_drvdata(kdev);
>+        struct drm_device *drm = dev_get_drvdata(kdev);
>+
>+        return drm ? to_i915(drm) : NULL;
> }
> 
> #define IS_PLATFORM(xe, x) ((xe)->info.platform == x)
>diff --git a/drivers/gpu/drm/xe/xe_device.h b/drivers/gpu/drm/xe/xe_device.h
>index 2c96f1b2aafd..022876eebfd5 100644
>--- a/drivers/gpu/drm/xe/xe_device.h
>+++ b/drivers/gpu/drm/xe/xe_device.h
>@@ -17,12 +17,16 @@ static inline struct xe_device *to_xe_device(const struct drm_device *dev)
> 
> static inline struct xe_device *kdev_to_xe_device(struct device *kdev)
> {
>-        return dev_get_drvdata(kdev);
>+        struct drm_device *drm = dev_get_drvdata(kdev);
>+
>+        return drm ? to_xe_device(drm) : NULL;
> }
> 
> static inline struct xe_device *pdev_to_xe_device(struct pci_dev *pdev)
> {
>-        return pci_get_drvdata(pdev);
>+        struct drm_device *drm = pci_get_drvdata(pdev);
>+
>+        return drm ? to_xe_device(drm) : NULL;
> }
> 
> static inline struct xe_device *xe_device_const_cast(const struct xe_device *xe)
>diff --git a/drivers/gpu/drm/xe/xe_pci.c b/drivers/gpu/drm/xe/xe_pci.c
>index 7bb811b4a057..f861b8cf931a 100644
>--- a/drivers/gpu/drm/xe/xe_pci.c
>+++ b/drivers/gpu/drm/xe/xe_pci.c
>@@ -800,7 +800,7 @@ static int xe_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
>         if (IS_ERR(xe))
>                 return PTR_ERR(xe);
> 
>-        pci_set_drvdata(pdev, xe);
>+        pci_set_drvdata(pdev, &xe->drm);
> 
>         xe_pm_assert_unbounded_bridge(xe);
>         subplatform_desc = find_subplatform(xe, desc);
>-- 
>2.39.2
>
Jani Nikula Aug. 2, 2024, 8:18 a.m. UTC | #2
On Thu, 01 Aug 2024, Gustavo Sousa <gustavo.sousa@intel.com> wrote:
> Quoting Jani Nikula (2024-07-29 11:30:06-03:00)
>>In the future, the display code shall not have any idea about struct
>>xe_device or struct drm_i915_private, but will need to get at the struct
>>drm_device via drvdata. Store the struct drm_device pointer to drvdata
>>instead of the driver specific pointer.
>>
>>Even though struct drm_device is embedded in both struct xe_device and
>>struct drm_i915_private at offset 0, take care to check for NULL before
>>using container_of() to allow for other offsets.
>
> I think the second half of this paragraph could be rephrased. One might
> think that the text is suggesting that checking for NULL has something
> to do with allowing other offsets.
>
> I would jump directly to mention using container_of() and would assume
> that it is implied that NULL check is necessary before using it. :-)

But I *am* suggesting the NULL check before container_of() has
everything to do with allowing other offsets!

container_of() will return a NULL pointer for a NULL pointer when the
offset is 0, but will return a negative garbage pointer otherwise.

BR,
Jani.


>
> Reviewed-by: Gustavo Sousa <gustavo.sousa@intel.com>
>
>>
>>Signed-off-by: Jani Nikula <jani.nikula@intel.com>
>>---
>> drivers/gpu/drm/i915/i915_driver.c                | 2 +-
>> drivers/gpu/drm/i915/i915_drv.h                   | 8 ++++++--
>> drivers/gpu/drm/i915/selftests/mock_gem_device.c  | 2 +-
>> drivers/gpu/drm/xe/compat-i915-headers/i915_drv.h | 4 +++-
>> drivers/gpu/drm/xe/xe_device.h                    | 8 ++++++--
>> drivers/gpu/drm/xe/xe_pci.c                       | 2 +-
>> 6 files changed, 18 insertions(+), 8 deletions(-)
>>
>>diff --git a/drivers/gpu/drm/i915/i915_driver.c b/drivers/gpu/drm/i915/i915_driver.c
>>index fb8e9c2fcea5..176c13c2e191 100644
>>--- a/drivers/gpu/drm/i915/i915_driver.c
>>+++ b/drivers/gpu/drm/i915/i915_driver.c
>>@@ -723,7 +723,7 @@ i915_driver_create(struct pci_dev *pdev, const struct pci_device_id *ent)
>>         if (IS_ERR(i915))
>>                 return i915;
>> 
>>-        pci_set_drvdata(pdev, i915);
>>+        pci_set_drvdata(pdev, &i915->drm);
>> 
>>         /* Device parameters start as a copy of module parameters. */
>>         i915_params_copy(&i915->params, &i915_modparams);
>>diff --git a/drivers/gpu/drm/i915/i915_drv.h b/drivers/gpu/drm/i915/i915_drv.h
>>index d7723dd11c80..f6edaef73db5 100644
>>--- a/drivers/gpu/drm/i915/i915_drv.h
>>+++ b/drivers/gpu/drm/i915/i915_drv.h
>>@@ -365,12 +365,16 @@ static inline struct drm_i915_private *to_i915(const struct drm_device *dev)
>> 
>> static inline struct drm_i915_private *kdev_to_i915(struct device *kdev)
>> {
>>-        return dev_get_drvdata(kdev);
>>+        struct drm_device *drm = dev_get_drvdata(kdev);
>>+
>>+        return drm ? to_i915(drm) : NULL;
>> }
>> 
>> static inline struct drm_i915_private *pdev_to_i915(struct pci_dev *pdev)
>> {
>>-        return pci_get_drvdata(pdev);
>>+        struct drm_device *drm = pci_get_drvdata(pdev);
>>+
>>+        return drm ? to_i915(drm) : NULL;
>> }
>> 
>> static inline struct intel_gt *to_gt(const struct drm_i915_private *i915)
>>diff --git a/drivers/gpu/drm/i915/selftests/mock_gem_device.c b/drivers/gpu/drm/i915/selftests/mock_gem_device.c
>>index 0bd29846873b..91794ca17a58 100644
>>--- a/drivers/gpu/drm/i915/selftests/mock_gem_device.c
>>+++ b/drivers/gpu/drm/i915/selftests/mock_gem_device.c
>>@@ -172,7 +172,7 @@ struct drm_i915_private *mock_gem_device(void)
>>                 return NULL;
>>         }
>> 
>>-        pci_set_drvdata(pdev, i915);
>>+        pci_set_drvdata(pdev, &i915->drm);
>> 
>>         /* Device parameters start as a copy of module parameters. */
>>         i915_params_copy(&i915->params, &i915_modparams);
>>diff --git a/drivers/gpu/drm/xe/compat-i915-headers/i915_drv.h b/drivers/gpu/drm/xe/compat-i915-headers/i915_drv.h
>>index 2feedddf1e40..766fba88a3c8 100644
>>--- a/drivers/gpu/drm/xe/compat-i915-headers/i915_drv.h
>>+++ b/drivers/gpu/drm/xe/compat-i915-headers/i915_drv.h
>>@@ -23,7 +23,9 @@ static inline struct drm_i915_private *to_i915(const struct drm_device *dev)
>> 
>> static inline struct drm_i915_private *kdev_to_i915(struct device *kdev)
>> {
>>-        return dev_get_drvdata(kdev);
>>+        struct drm_device *drm = dev_get_drvdata(kdev);
>>+
>>+        return drm ? to_i915(drm) : NULL;
>> }
>> 
>> #define IS_PLATFORM(xe, x) ((xe)->info.platform == x)
>>diff --git a/drivers/gpu/drm/xe/xe_device.h b/drivers/gpu/drm/xe/xe_device.h
>>index 2c96f1b2aafd..022876eebfd5 100644
>>--- a/drivers/gpu/drm/xe/xe_device.h
>>+++ b/drivers/gpu/drm/xe/xe_device.h
>>@@ -17,12 +17,16 @@ static inline struct xe_device *to_xe_device(const struct drm_device *dev)
>> 
>> static inline struct xe_device *kdev_to_xe_device(struct device *kdev)
>> {
>>-        return dev_get_drvdata(kdev);
>>+        struct drm_device *drm = dev_get_drvdata(kdev);
>>+
>>+        return drm ? to_xe_device(drm) : NULL;
>> }
>> 
>> static inline struct xe_device *pdev_to_xe_device(struct pci_dev *pdev)
>> {
>>-        return pci_get_drvdata(pdev);
>>+        struct drm_device *drm = pci_get_drvdata(pdev);
>>+
>>+        return drm ? to_xe_device(drm) : NULL;
>> }
>> 
>> static inline struct xe_device *xe_device_const_cast(const struct xe_device *xe)
>>diff --git a/drivers/gpu/drm/xe/xe_pci.c b/drivers/gpu/drm/xe/xe_pci.c
>>index 7bb811b4a057..f861b8cf931a 100644
>>--- a/drivers/gpu/drm/xe/xe_pci.c
>>+++ b/drivers/gpu/drm/xe/xe_pci.c
>>@@ -800,7 +800,7 @@ static int xe_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
>>         if (IS_ERR(xe))
>>                 return PTR_ERR(xe);
>> 
>>-        pci_set_drvdata(pdev, xe);
>>+        pci_set_drvdata(pdev, &xe->drm);
>> 
>>         xe_pm_assert_unbounded_bridge(xe);
>>         subplatform_desc = find_subplatform(xe, desc);
>>-- 
>>2.39.2
>>
Gustavo Sousa Aug. 2, 2024, 12:08 p.m. UTC | #3
Quoting Jani Nikula (2024-08-02 05:18:39-03:00)
>On Thu, 01 Aug 2024, Gustavo Sousa <gustavo.sousa@intel.com> wrote:
>> Quoting Jani Nikula (2024-07-29 11:30:06-03:00)
>>>In the future, the display code shall not have any idea about struct
>>>xe_device or struct drm_i915_private, but will need to get at the struct
>>>drm_device via drvdata. Store the struct drm_device pointer to drvdata
>>>instead of the driver specific pointer.
>>>
>>>Even though struct drm_device is embedded in both struct xe_device and
>>>struct drm_i915_private at offset 0, take care to check for NULL before
>>>using container_of() to allow for other offsets.
>>
>> I think the second half of this paragraph could be rephrased. One might
>> think that the text is suggesting that checking for NULL has something
>> to do with allowing other offsets.
>>
>> I would jump directly to mention using container_of() and would assume
>> that it is implied that NULL check is necessary before using it. :-)
>
>But I *am* suggesting the NULL check before container_of() has
>everything to do with allowing other offsets!
>
>container_of() will return a NULL pointer for a NULL pointer when the
>offset is 0, but will return a negative garbage pointer otherwise.

Okay, I think I got a better understanding of what you mean.

That said, the following makes more sense *to me*:

- The use of container_of() is what actually allows the use of non-zero
  offsets.

- container_of() shouldn't be used with NULL pointers, it just doesn't
  make sense. So, if we receive a NULL pointer, don't bother using
  container_of() and simply return NULL.

--
Gustavo Sousa

>
>BR,
>Jani.
>
>
>>
>> Reviewed-by: Gustavo Sousa <gustavo.sousa@intel.com>
>>
>>>
>>>Signed-off-by: Jani Nikula <jani.nikula@intel.com>
>>>---
>>> drivers/gpu/drm/i915/i915_driver.c                | 2 +-
>>> drivers/gpu/drm/i915/i915_drv.h                   | 8 ++++++--
>>> drivers/gpu/drm/i915/selftests/mock_gem_device.c  | 2 +-
>>> drivers/gpu/drm/xe/compat-i915-headers/i915_drv.h | 4 +++-
>>> drivers/gpu/drm/xe/xe_device.h                    | 8 ++++++--
>>> drivers/gpu/drm/xe/xe_pci.c                       | 2 +-
>>> 6 files changed, 18 insertions(+), 8 deletions(-)
>>>
>>>diff --git a/drivers/gpu/drm/i915/i915_driver.c b/drivers/gpu/drm/i915/i915_driver.c
>>>index fb8e9c2fcea5..176c13c2e191 100644
>>>--- a/drivers/gpu/drm/i915/i915_driver.c
>>>+++ b/drivers/gpu/drm/i915/i915_driver.c
>>>@@ -723,7 +723,7 @@ i915_driver_create(struct pci_dev *pdev, const struct pci_device_id *ent)
>>>         if (IS_ERR(i915))
>>>                 return i915;
>>> 
>>>-        pci_set_drvdata(pdev, i915);
>>>+        pci_set_drvdata(pdev, &i915->drm);
>>> 
>>>         /* Device parameters start as a copy of module parameters. */
>>>         i915_params_copy(&i915->params, &i915_modparams);
>>>diff --git a/drivers/gpu/drm/i915/i915_drv.h b/drivers/gpu/drm/i915/i915_drv.h
>>>index d7723dd11c80..f6edaef73db5 100644
>>>--- a/drivers/gpu/drm/i915/i915_drv.h
>>>+++ b/drivers/gpu/drm/i915/i915_drv.h
>>>@@ -365,12 +365,16 @@ static inline struct drm_i915_private *to_i915(const struct drm_device *dev)
>>> 
>>> static inline struct drm_i915_private *kdev_to_i915(struct device *kdev)
>>> {
>>>-        return dev_get_drvdata(kdev);
>>>+        struct drm_device *drm = dev_get_drvdata(kdev);
>>>+
>>>+        return drm ? to_i915(drm) : NULL;
>>> }
>>> 
>>> static inline struct drm_i915_private *pdev_to_i915(struct pci_dev *pdev)
>>> {
>>>-        return pci_get_drvdata(pdev);
>>>+        struct drm_device *drm = pci_get_drvdata(pdev);
>>>+
>>>+        return drm ? to_i915(drm) : NULL;
>>> }
>>> 
>>> static inline struct intel_gt *to_gt(const struct drm_i915_private *i915)
>>>diff --git a/drivers/gpu/drm/i915/selftests/mock_gem_device.c b/drivers/gpu/drm/i915/selftests/mock_gem_device.c
>>>index 0bd29846873b..91794ca17a58 100644
>>>--- a/drivers/gpu/drm/i915/selftests/mock_gem_device.c
>>>+++ b/drivers/gpu/drm/i915/selftests/mock_gem_device.c
>>>@@ -172,7 +172,7 @@ struct drm_i915_private *mock_gem_device(void)
>>>                 return NULL;
>>>         }
>>> 
>>>-        pci_set_drvdata(pdev, i915);
>>>+        pci_set_drvdata(pdev, &i915->drm);
>>> 
>>>         /* Device parameters start as a copy of module parameters. */
>>>         i915_params_copy(&i915->params, &i915_modparams);
>>>diff --git a/drivers/gpu/drm/xe/compat-i915-headers/i915_drv.h b/drivers/gpu/drm/xe/compat-i915-headers/i915_drv.h
>>>index 2feedddf1e40..766fba88a3c8 100644
>>>--- a/drivers/gpu/drm/xe/compat-i915-headers/i915_drv.h
>>>+++ b/drivers/gpu/drm/xe/compat-i915-headers/i915_drv.h
>>>@@ -23,7 +23,9 @@ static inline struct drm_i915_private *to_i915(const struct drm_device *dev)
>>> 
>>> static inline struct drm_i915_private *kdev_to_i915(struct device *kdev)
>>> {
>>>-        return dev_get_drvdata(kdev);
>>>+        struct drm_device *drm = dev_get_drvdata(kdev);
>>>+
>>>+        return drm ? to_i915(drm) : NULL;
>>> }
>>> 
>>> #define IS_PLATFORM(xe, x) ((xe)->info.platform == x)
>>>diff --git a/drivers/gpu/drm/xe/xe_device.h b/drivers/gpu/drm/xe/xe_device.h
>>>index 2c96f1b2aafd..022876eebfd5 100644
>>>--- a/drivers/gpu/drm/xe/xe_device.h
>>>+++ b/drivers/gpu/drm/xe/xe_device.h
>>>@@ -17,12 +17,16 @@ static inline struct xe_device *to_xe_device(const struct drm_device *dev)
>>> 
>>> static inline struct xe_device *kdev_to_xe_device(struct device *kdev)
>>> {
>>>-        return dev_get_drvdata(kdev);
>>>+        struct drm_device *drm = dev_get_drvdata(kdev);
>>>+
>>>+        return drm ? to_xe_device(drm) : NULL;
>>> }
>>> 
>>> static inline struct xe_device *pdev_to_xe_device(struct pci_dev *pdev)
>>> {
>>>-        return pci_get_drvdata(pdev);
>>>+        struct drm_device *drm = pci_get_drvdata(pdev);
>>>+
>>>+        return drm ? to_xe_device(drm) : NULL;
>>> }
>>> 
>>> static inline struct xe_device *xe_device_const_cast(const struct xe_device *xe)
>>>diff --git a/drivers/gpu/drm/xe/xe_pci.c b/drivers/gpu/drm/xe/xe_pci.c
>>>index 7bb811b4a057..f861b8cf931a 100644
>>>--- a/drivers/gpu/drm/xe/xe_pci.c
>>>+++ b/drivers/gpu/drm/xe/xe_pci.c
>>>@@ -800,7 +800,7 @@ static int xe_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
>>>         if (IS_ERR(xe))
>>>                 return PTR_ERR(xe);
>>> 
>>>-        pci_set_drvdata(pdev, xe);
>>>+        pci_set_drvdata(pdev, &xe->drm);
>>> 
>>>         xe_pm_assert_unbounded_bridge(xe);
>>>         subplatform_desc = find_subplatform(xe, desc);
>>>-- 
>>>2.39.2
>>>
>
>-- 
>Jani Nikula, Intel
diff mbox series

Patch

diff --git a/drivers/gpu/drm/i915/i915_driver.c b/drivers/gpu/drm/i915/i915_driver.c
index fb8e9c2fcea5..176c13c2e191 100644
--- a/drivers/gpu/drm/i915/i915_driver.c
+++ b/drivers/gpu/drm/i915/i915_driver.c
@@ -723,7 +723,7 @@  i915_driver_create(struct pci_dev *pdev, const struct pci_device_id *ent)
 	if (IS_ERR(i915))
 		return i915;
 
-	pci_set_drvdata(pdev, i915);
+	pci_set_drvdata(pdev, &i915->drm);
 
 	/* Device parameters start as a copy of module parameters. */
 	i915_params_copy(&i915->params, &i915_modparams);
diff --git a/drivers/gpu/drm/i915/i915_drv.h b/drivers/gpu/drm/i915/i915_drv.h
index d7723dd11c80..f6edaef73db5 100644
--- a/drivers/gpu/drm/i915/i915_drv.h
+++ b/drivers/gpu/drm/i915/i915_drv.h
@@ -365,12 +365,16 @@  static inline struct drm_i915_private *to_i915(const struct drm_device *dev)
 
 static inline struct drm_i915_private *kdev_to_i915(struct device *kdev)
 {
-	return dev_get_drvdata(kdev);
+	struct drm_device *drm = dev_get_drvdata(kdev);
+
+	return drm ? to_i915(drm) : NULL;
 }
 
 static inline struct drm_i915_private *pdev_to_i915(struct pci_dev *pdev)
 {
-	return pci_get_drvdata(pdev);
+	struct drm_device *drm = pci_get_drvdata(pdev);
+
+	return drm ? to_i915(drm) : NULL;
 }
 
 static inline struct intel_gt *to_gt(const struct drm_i915_private *i915)
diff --git a/drivers/gpu/drm/i915/selftests/mock_gem_device.c b/drivers/gpu/drm/i915/selftests/mock_gem_device.c
index 0bd29846873b..91794ca17a58 100644
--- a/drivers/gpu/drm/i915/selftests/mock_gem_device.c
+++ b/drivers/gpu/drm/i915/selftests/mock_gem_device.c
@@ -172,7 +172,7 @@  struct drm_i915_private *mock_gem_device(void)
 		return NULL;
 	}
 
-	pci_set_drvdata(pdev, i915);
+	pci_set_drvdata(pdev, &i915->drm);
 
 	/* Device parameters start as a copy of module parameters. */
 	i915_params_copy(&i915->params, &i915_modparams);
diff --git a/drivers/gpu/drm/xe/compat-i915-headers/i915_drv.h b/drivers/gpu/drm/xe/compat-i915-headers/i915_drv.h
index 2feedddf1e40..766fba88a3c8 100644
--- a/drivers/gpu/drm/xe/compat-i915-headers/i915_drv.h
+++ b/drivers/gpu/drm/xe/compat-i915-headers/i915_drv.h
@@ -23,7 +23,9 @@  static inline struct drm_i915_private *to_i915(const struct drm_device *dev)
 
 static inline struct drm_i915_private *kdev_to_i915(struct device *kdev)
 {
-	return dev_get_drvdata(kdev);
+	struct drm_device *drm = dev_get_drvdata(kdev);
+
+	return drm ? to_i915(drm) : NULL;
 }
 
 #define IS_PLATFORM(xe, x) ((xe)->info.platform == x)
diff --git a/drivers/gpu/drm/xe/xe_device.h b/drivers/gpu/drm/xe/xe_device.h
index 2c96f1b2aafd..022876eebfd5 100644
--- a/drivers/gpu/drm/xe/xe_device.h
+++ b/drivers/gpu/drm/xe/xe_device.h
@@ -17,12 +17,16 @@  static inline struct xe_device *to_xe_device(const struct drm_device *dev)
 
 static inline struct xe_device *kdev_to_xe_device(struct device *kdev)
 {
-	return dev_get_drvdata(kdev);
+	struct drm_device *drm = dev_get_drvdata(kdev);
+
+	return drm ? to_xe_device(drm) : NULL;
 }
 
 static inline struct xe_device *pdev_to_xe_device(struct pci_dev *pdev)
 {
-	return pci_get_drvdata(pdev);
+	struct drm_device *drm = pci_get_drvdata(pdev);
+
+	return drm ? to_xe_device(drm) : NULL;
 }
 
 static inline struct xe_device *xe_device_const_cast(const struct xe_device *xe)
diff --git a/drivers/gpu/drm/xe/xe_pci.c b/drivers/gpu/drm/xe/xe_pci.c
index 7bb811b4a057..f861b8cf931a 100644
--- a/drivers/gpu/drm/xe/xe_pci.c
+++ b/drivers/gpu/drm/xe/xe_pci.c
@@ -800,7 +800,7 @@  static int xe_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 	if (IS_ERR(xe))
 		return PTR_ERR(xe);
 
-	pci_set_drvdata(pdev, xe);
+	pci_set_drvdata(pdev, &xe->drm);
 
 	xe_pm_assert_unbounded_bridge(xe);
 	subplatform_desc = find_subplatform(xe, desc);