drm/amdgpu: added AMD GPU instance counting V2
authorEvan Quan <evan.quan@amd.com>
Thu, 27 Sep 2018 05:26:58 +0000 (13:26 +0800)
committerAlex Deucher <alexander.deucher@amd.com>
Tue, 9 Oct 2018 21:59:33 +0000 (16:59 -0500)
Count all GPU instances from AMD(including iGPUs and
dGPUs) in the system.

V2: drop unnecessary initialization for other gpu_info
    members except mutex

Signed-off-by: Evan Quan <evan.quan@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_drv.c
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c

index c05b394386634b971c5402cd2612f759de9cfe7a..bd6f236ab7e768472d089eb17b77b45a79f186eb 100644 (file)
 #include "amdgpu_bo_list.h"
 #include "amdgpu_gem.h"
 
+#define MAX_GPU_INSTANCE               16
+
+struct amdgpu_gpu_instance
+{
+       struct amdgpu_device            *adev;
+       int                             mgpu_fan_enabled;
+};
+
+struct amdgpu_mgpu_info
+{
+       struct amdgpu_gpu_instance      gpu_ins[MAX_GPU_INSTANCE];
+       struct mutex                    mutex;
+       uint32_t                        num_gpu;
+       uint32_t                        num_dgpu;
+       uint32_t                        num_apu;
+};
+
 /*
  * Modules parameters.
  */
@@ -134,6 +151,7 @@ extern int amdgpu_compute_multipipe;
 extern int amdgpu_gpu_recovery;
 extern int amdgpu_emu_mode;
 extern uint amdgpu_smu_memory_pool_size;
+extern struct amdgpu_mgpu_info mgpu_info;
 
 #ifdef CONFIG_DRM_AMDGPU_SI
 extern int amdgpu_si_support;
index 723f0f7754bd8351b930048298f7806a38784696..28781414d71c85e4dc5657e7cbfb237a39f5a3c3 100644 (file)
@@ -127,6 +127,9 @@ int amdgpu_compute_multipipe = -1;
 int amdgpu_gpu_recovery = -1; /* auto */
 int amdgpu_emu_mode = 0;
 uint amdgpu_smu_memory_pool_size = 0;
+struct amdgpu_mgpu_info mgpu_info = {
+       .mutex = __MUTEX_INITIALIZER(mgpu_info.mutex),
+};
 
 /**
  * DOC: vramlimit (int)
index a64056dadc58204d75a1bb40f8b369363af6ba31..81732a84c2ab090af4e2f834e2223c0eeabe2bcc 100644 (file)
 #include "amdgpu_gem.h"
 #include "amdgpu_display.h"
 
+static void amdgpu_unregister_gpu_instance(struct amdgpu_device *adev)
+{
+       struct amdgpu_gpu_instance *gpu_instance;
+       int i;
+
+       mutex_lock(&mgpu_info.mutex);
+
+       for (i = 0; i < mgpu_info.num_gpu; i++) {
+               gpu_instance = &(mgpu_info.gpu_ins[i]);
+               if (gpu_instance->adev == adev) {
+                       mgpu_info.gpu_ins[i] =
+                               mgpu_info.gpu_ins[mgpu_info.num_gpu - 1];
+                       mgpu_info.num_gpu--;
+                       if (adev->flags & AMD_IS_APU)
+                               mgpu_info.num_apu--;
+                       else
+                               mgpu_info.num_dgpu--;
+                       break;
+               }
+       }
+
+       mutex_unlock(&mgpu_info.mutex);
+}
+
 /**
  * amdgpu_driver_unload_kms - Main unload function for KMS.
  *
@@ -55,6 +79,8 @@ void amdgpu_driver_unload_kms(struct drm_device *dev)
        if (adev == NULL)
                return;
 
+       amdgpu_unregister_gpu_instance(adev);
+
        if (adev->rmmio == NULL)
                goto done_free;
 
@@ -75,6 +101,31 @@ done_free:
        dev->dev_private = NULL;
 }
 
+static void amdgpu_register_gpu_instance(struct amdgpu_device *adev)
+{
+       struct amdgpu_gpu_instance *gpu_instance;
+
+       mutex_lock(&mgpu_info.mutex);
+
+       if (mgpu_info.num_gpu >= MAX_GPU_INSTANCE) {
+               DRM_ERROR("Cannot register more gpu instance\n");
+               mutex_unlock(&mgpu_info.mutex);
+               return;
+       }
+
+       gpu_instance = &(mgpu_info.gpu_ins[mgpu_info.num_gpu]);
+       gpu_instance->adev = adev;
+       gpu_instance->mgpu_fan_enabled = 0;
+
+       mgpu_info.num_gpu++;
+       if (adev->flags & AMD_IS_APU)
+               mgpu_info.num_apu++;
+       else
+               mgpu_info.num_dgpu++;
+
+       mutex_unlock(&mgpu_info.mutex);
+}
+
 /**
  * amdgpu_driver_load_kms - Main load function for KMS.
  *
@@ -169,6 +220,7 @@ int amdgpu_driver_load_kms(struct drm_device *dev, unsigned long flags)
                pm_runtime_put_autosuspend(dev->dev);
        }
 
+       amdgpu_register_gpu_instance(adev);
 out:
        if (r) {
                /* balance pm_runtime_get_sync in amdgpu_driver_unload_kms */