drm/amdgpu: Embed drm_device into amdgpu_device (v3)
authorLuben Tuikov <luben.tuikov@amd.com>
Sat, 15 Aug 2020 00:41:55 +0000 (20:41 -0400)
committerAlex Deucher <alexander.deucher@amd.com>
Wed, 26 Aug 2020 20:40:17 +0000 (16:40 -0400)
a) Embed struct drm_device into struct amdgpu_device.
b) Modify the inline-f drm_to_adev() accordingly.
c) Modify the inline-f adev_to_drm() accordingly.
d) Eliminate the use of drm_device.dev_private,
   in amdgpu.
e) Switch from using drm_dev_alloc() to
   drm_dev_init().
f) Add a DRM driver release function, which frees
   the container amdgpu_device after all krefs on
   the contained drm_device have been released.

v2: Split out adding adev_to_drm() into its own
    patch (previous commit), making this patch
    more succinct and clear. More detailed commit
    description.
v3: squash in fix to call drmm_add_final_kfree()
    to avoid a warning.

Signed-off-by: Luben Tuikov <luben.tuikov@amd.com>
Reviewed-by: Alex Deucher <alexander.deucher@amd.com>
Signed-off-by: Alex Deucher <alexander.deucher@amd.com>
drivers/gpu/drm/amd/amdgpu/amdgpu.h
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c

index 9d93b31..2509b1e 100644 (file)
@@ -722,8 +722,8 @@ struct amd_powerplay {
 #define AMDGPU_MAX_DF_PERFMONS 4
 struct amdgpu_device {
        struct device                   *dev;
-       struct drm_device               *ddev;
        struct pci_dev                  *pdev;
+       struct drm_device               ddev;
 
 #ifdef CONFIG_DRM_AMD_ACP
        struct amdgpu_acp               acp;
@@ -988,12 +988,12 @@ struct amdgpu_device {
 
 static inline struct amdgpu_device *drm_to_adev(struct drm_device *ddev)
 {
-       return ddev->dev_private;
+       return container_of(ddev, struct amdgpu_device, ddev);
 }
 
 static inline struct drm_device *adev_to_drm(struct amdgpu_device *adev)
 {
-       return adev->ddev;
+       return &adev->ddev;
 }
 
 static inline struct amdgpu_device *amdgpu_ttm_adev(struct ttm_bo_device *bdev)
@@ -1002,8 +1002,6 @@ static inline struct amdgpu_device *amdgpu_ttm_adev(struct ttm_bo_device *bdev)
 }
 
 int amdgpu_device_init(struct amdgpu_device *adev,
-                      struct drm_device *ddev,
-                      struct pci_dev *pdev,
                       uint32_t flags);
 void amdgpu_device_fini(struct amdgpu_device *adev);
 int amdgpu_gpu_wait_for_idle(struct amdgpu_device *adev);
@@ -1194,7 +1192,7 @@ static inline void *amdgpu_atpx_get_dhandle(void) { return NULL; }
 extern const struct drm_ioctl_desc amdgpu_ioctls_kms[];
 extern const int amdgpu_max_kms_ioctl;
 
-int amdgpu_driver_load_kms(struct drm_device *dev, unsigned long flags);
+int amdgpu_driver_load_kms(struct amdgpu_device *adev, unsigned long flags);
 void amdgpu_driver_unload_kms(struct drm_device *dev);
 void amdgpu_driver_lastclose_kms(struct drm_device *dev);
 int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv);
index afe8cb3..64d6e58 100644 (file)
@@ -1216,7 +1216,8 @@ static int amdgpu_device_check_arguments(struct amdgpu_device *adev)
  * Callback for the switcheroo driver.  Suspends or resumes the
  * the asics before or after it is powered up using ACPI methods.
  */
-static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
+static void amdgpu_switcheroo_set_state(struct pci_dev *pdev,
+                                       enum vga_switcheroo_state state)
 {
        struct drm_device *dev = pci_get_drvdata(pdev);
        int r;
@@ -2978,8 +2979,6 @@ static const struct attribute *amdgpu_dev_attributes[] = {
  * amdgpu_device_init - initialize the driver
  *
  * @adev: amdgpu_device pointer
- * @ddev: drm dev pointer
- * @pdev: pci dev pointer
  * @flags: driver flags
  *
  * Initializes the driver info and hw (all asics).
@@ -2987,18 +2986,15 @@ static const struct attribute *amdgpu_dev_attributes[] = {
  * Called at driver startup.
  */
 int amdgpu_device_init(struct amdgpu_device *adev,
-                      struct drm_device *ddev,
-                      struct pci_dev *pdev,
                       uint32_t flags)
 {
+       struct drm_device *ddev = adev_to_drm(adev);
+       struct pci_dev *pdev = adev->pdev;
        int r, i;
        bool boco = false;
        u32 max_MBps;
 
        adev->shutdown = false;
-       adev->dev = &pdev->dev;
-       adev->ddev = ddev;
-       adev->pdev = pdev;
        adev->flags = flags;
 
        if (amdgpu_force_asic_type >= 0 && amdgpu_force_asic_type < CHIP_LAST)
@@ -3453,9 +3449,8 @@ int amdgpu_device_suspend(struct drm_device *dev, bool fbcon)
        struct drm_connector_list_iter iter;
        int r;
 
-       if (dev == NULL || dev->dev_private == NULL) {
+       if (!dev)
                return -ENODEV;
-       }
 
        adev = drm_to_adev(dev);
 
index 651b51a..ed29f0c 100644 (file)
@@ -26,6 +26,7 @@
 #include <drm/drm_drv.h>
 #include <drm/drm_gem.h>
 #include <drm/drm_vblank.h>
+#include <drm/drm_managed.h>
 #include "amdgpu_drv.h"
 
 #include <drm/drm_pciids.h>
@@ -1082,7 +1083,7 @@ static struct drm_driver kms_driver;
 static int amdgpu_pci_probe(struct pci_dev *pdev,
                            const struct pci_device_id *ent)
 {
-       struct drm_device *dev;
+       struct drm_device *ddev;
        struct amdgpu_device *adev;
        unsigned long flags = ent->driver_data;
        int ret, retry = 0;
@@ -1138,36 +1139,44 @@ static int amdgpu_pci_probe(struct pci_dev *pdev,
        if (ret)
                return ret;
 
-       dev = drm_dev_alloc(&kms_driver, &pdev->dev);
-       if (IS_ERR(dev))
-               return PTR_ERR(dev);
+       adev = kzalloc(sizeof(*adev), GFP_KERNEL);
+       if (!adev)
+               return -ENOMEM;
+
+       adev->dev  = &pdev->dev;
+       adev->pdev = pdev;
+       ddev = adev_to_drm(adev);
+       ret = drm_dev_init(ddev, &kms_driver, &pdev->dev);
+       if (ret)
+               goto err_free;
+
+       drmm_add_final_kfree(ddev, ddev);
 
        if (!supports_atomic)
-               dev->driver_features &= ~DRIVER_ATOMIC;
+               ddev->driver_features &= ~DRIVER_ATOMIC;
 
        ret = pci_enable_device(pdev);
        if (ret)
                goto err_free;
 
-       dev->pdev = pdev;
-
-       pci_set_drvdata(pdev, dev);
+       ddev->pdev = pdev;
+       pci_set_drvdata(pdev, ddev);
 
-       ret = amdgpu_driver_load_kms(dev, ent->driver_data);
+       ret = amdgpu_driver_load_kms(adev, ent->driver_data);
        if (ret)
                goto err_pci;
 
 retry_init:
-       ret = drm_dev_register(dev, ent->driver_data);
+       ret = drm_dev_register(ddev, ent->driver_data);
        if (ret == -EAGAIN && ++retry <= 3) {
                DRM_INFO("retry init %d\n", retry);
                /* Don't request EX mode too frequently which is attacking */
                msleep(5000);
                goto retry_init;
-       } else if (ret)
+       } else if (ret) {
                goto err_pci;
+       }
 
-       adev = drm_to_adev(dev);
        ret = amdgpu_debugfs_init(adev);
        if (ret)
                DRM_ERROR("Creating debugfs files failed (%d).\n", ret);
@@ -1177,7 +1186,7 @@ retry_init:
 err_pci:
        pci_disable_device(pdev);
 err_free:
-       drm_dev_put(dev);
+       drm_dev_put(ddev);
        return ret;
 }
 
@@ -1197,6 +1206,13 @@ amdgpu_pci_remove(struct pci_dev *pdev)
        drm_dev_put(dev);
 }
 
+static void amdgpu_driver_release(struct drm_device *ddev)
+{
+       struct amdgpu_device *adev = drm_to_adev(ddev);
+
+       kfree(adev);
+}
+
 static void
 amdgpu_pci_shutdown(struct pci_dev *pdev)
 {
@@ -1491,6 +1507,7 @@ static struct drm_driver kms_driver = {
        .open = amdgpu_driver_open_kms,
        .postclose = amdgpu_driver_postclose_kms,
        .lastclose = amdgpu_driver_lastclose_kms,
+       .release   = amdgpu_driver_release,
        .irq_handler = amdgpu_irq_handler,
        .ioctls = amdgpu_ioctls_kms,
        .gem_free_object_unlocked = amdgpu_gem_object_free,
@@ -1525,8 +1542,6 @@ static struct pci_driver amdgpu_kms_pci_driver = {
        .driver.pm = &amdgpu_pm_ops,
 };
 
-
-
 static int __init amdgpu_init(void)
 {
        int r;
index c2a697d..d125c4b 100644 (file)
@@ -86,7 +86,7 @@ void amdgpu_driver_unload_kms(struct drm_device *dev)
        amdgpu_unregister_gpu_instance(adev);
 
        if (adev->rmmio == NULL)
-               goto done_free;
+               return;
 
        if (adev->runpm) {
                pm_runtime_get_sync(dev->dev);
@@ -94,12 +94,7 @@ void amdgpu_driver_unload_kms(struct drm_device *dev)
        }
 
        amdgpu_acpi_fini(adev);
-
        amdgpu_device_fini(adev);
-
-done_free:
-       kfree(adev);
-       dev->dev_private = NULL;
 }
 
 void amdgpu_register_gpu_instance(struct amdgpu_device *adev)
@@ -130,22 +125,18 @@ void amdgpu_register_gpu_instance(struct amdgpu_device *adev)
 /**
  * amdgpu_driver_load_kms - Main load function for KMS.
  *
- * @dev: drm dev pointer
+ * @adev: pointer to struct amdgpu_device
  * @flags: device flags
  *
  * This is the main load function for KMS (all asics).
  * Returns 0 on success, error on failure.
  */
-int amdgpu_driver_load_kms(struct drm_device *dev, unsigned long flags)
+int amdgpu_driver_load_kms(struct amdgpu_device *adev, unsigned long flags)
 {
-       struct amdgpu_device *adev;
+       struct drm_device *dev;
        int r, acpi_status;
 
-       adev = kzalloc(sizeof(struct amdgpu_device), GFP_KERNEL);
-       if (adev == NULL) {
-               return -ENOMEM;
-       }
-       dev->dev_private = (void *)adev;
+       dev = adev_to_drm(adev);
 
        if (amdgpu_has_atpx() &&
            (amdgpu_is_atpx_hybrid() ||
@@ -160,7 +151,7 @@ int amdgpu_driver_load_kms(struct drm_device *dev, unsigned long flags)
         * properly initialize the GPU MC controller and permit
         * VRAM allocation
         */
-       r = amdgpu_device_init(adev, dev, dev->pdev, flags);
+       r = amdgpu_device_init(adev, flags);
        if (r) {
                dev_err(&dev->pdev->dev, "Fatal error during GPU init\n");
                goto out;