Add AMDGPU_INFO_CU_BITMAP, returning struct drm_amdgpu_info_cu_bitmap:
a header carrying num_xcc, num_se, num_sa and xcc_mask, followed by a
flexible array of num_xcc * num_se * num_sa active CU masks in un-folded
[xcc][se][sa] order, indexed bitmap[(xcc * num_se + se) * num_sa + sa].

Signed-off-by: Siwei He <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 69 +++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h |  2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c | 50 ++++++++++++++++++
 include/uapi/drm/amdgpu_drm.h           | 32 ++++++++++++
 4 files changed, 153 insertions(+)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c
index 0ae6e990c310..7a0a1d5542ee 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c
@@ -2050,6 +2050,75 @@ static ssize_t amdgpu_gfx_get_gfx_reset_mask(struct 
device *dev,
        return amdgpu_show_reset_mask(buf, adev->gfx.gfx_supported_reset);
 }
 
+/**
+ * amdgpu_gfx_get_cu_bitmap - un-fold the active CU bitmap for a set of XCCs
+ *
+ * @adev: amdgpu_device pointer
+ * @inst_mask: mask of logical GC (XCC) instances to report
+ * @out: destination, filled in [xcc][se][sa] row-major order
+ * @max_entries: capacity of @out, in u32 entries
+ *
+ * XCCs are written in ascending set-bit order of @inst_mask, densely packed,
+ * so entry (xcc * num_se + se) * num_sa + sa holds shader array @sa of
+ * shader engine @se on the @xcc'th set bit of @inst_mask.
+ *
+ * Returns the number of XCCs written, or a negative error code.
+ */
+int amdgpu_gfx_get_cu_bitmap(struct amdgpu_device *adev, u32 inst_mask,
+                            u32 *out, u32 max_entries)
+{
+       struct amdgpu_gfx_config *config = &adev->gfx.config;
+       u32 gc_version = amdgpu_ip_version(adev, GC_HWIP, 0);
+       u32 num_se, num_sa, sh_mul, se, sa, n = 0;
+       int xcc;
+
+       /* cu_info.bitmap[] only has AMDGPU_MAX_GC_INSTANCES slots */
+       inst_mask &= GENMASK(AMDGPU_MAX_GC_INSTANCES - 1, 0);
+       if (!inst_mask)
+               return -ENODEV;
+
+       num_se = config->max_shader_engines;
+       num_sa = config->max_sh_per_se;
+       if (!num_se || !num_sa)
+               return -ENODEV;
+
+       if (hweight32(inst_mask) * num_se * num_sa > max_entries)
+               return -EINVAL;
+
+       /* gfx11/gfx12_0 fold SE4-7 into shader array columns 2 and 3 */
+       sh_mul = (gc_version >= IP_VERSION(11, 0, 0) &&
+                 gc_version < IP_VERSION(13, 0, 0)) ? 2 : 1;
+
+       /*
+        * cu_info.bitmap[] stores a [4][4] tile per XCC, and the IP backend
+        * packed (se, sa) into it with the fold above. The row index se % 4 is
+        * in range by construction; the column must both stay inside the tile
+        * and not alias another shader engine's slot. Shader engines 4 apart
+        * share a row and are separated by sh_mul columns, so num_sa > sh_mul
+        * overlaps them. A topology failing either test lost data when the
+        * backend wrote the tile, and un-folding cannot recover it -- fail
+        * rather than report some other slot's mask as this shader array's.
+        */
+       if ((num_se > 4 && num_sa > sh_mul) ||
+           (num_sa - 1) + ((num_se - 1) / 4) * sh_mul > 3) {
+               dev_warn_once(adev->dev,
+                             "unsupported CU bitmap topology %u SE x %u SA\n",
+                             num_se, num_sa);
+               return -EOPNOTSUPP;
+       }
+
+       for_each_inst(xcc, inst_mask) {
+               for (se = 0; se < num_se; se++)
+                       for (sa = 0; sa < num_sa; sa++)
+                               out[(n * num_se + se) * num_sa + sa] =
+                                       adev->gfx.cu_info.bitmap[xcc]
+                                               [se % 4][sa + (se / 4) * 
sh_mul];
+               n++;
+       }
+
+       return n;
+}
+
 static ssize_t amdgpu_gfx_get_compute_reset_mask(struct device *dev,
                                                struct device_attribute *attr,
                                                char *buf)
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h
index 5939e1fb3b24..bd2617dd4c56 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h
@@ -663,6 +663,8 @@ int amdgpu_gfx_poison_consumption_handler(struct 
amdgpu_device *adev,
 bool amdgpu_gfx_is_master_xcc(struct amdgpu_device *adev, int xcc_id);
 int amdgpu_gfx_sysfs_init(struct amdgpu_device *adev);
 void amdgpu_gfx_sysfs_fini(struct amdgpu_device *adev);
+int amdgpu_gfx_get_cu_bitmap(struct amdgpu_device *adev, u32 inst_mask,
+                            u32 *out, u32 max_entries);
 int amdgpu_gfx_reset_mes_compute(struct amdgpu_device *adev,
                                 struct amdgpu_ring *ring,
                                 struct amdgpu_fence *guilty_fence,
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c
index 14a72a406d37..a8ebc85c9b88 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c
@@ -1453,6 +1453,56 @@ int amdgpu_info_ioctl(struct drm_device *dev, void 
*data, struct drm_file *filp)
                        return -EINVAL;
                }
        }
+       case AMDGPU_INFO_CU_BITMAP: {
+               struct drm_amdgpu_info_cu_bitmap *cu_bitmap;
+               u32 num_xcc, num_se, num_sa, nent;
+               size_t sz;
+
+               fpriv = (struct amdgpu_fpriv *)filp->driver_priv;
+
+               /* scope the reply to the partition this fd was opened against 
*/
+               if (adev->xcp_mgr && adev->xcp_mgr->num_xcps > 0 &&
+                   fpriv->xcp_id < adev->xcp_mgr->num_xcps) {
+                       xcp = &adev->xcp_mgr->xcp[fpriv->xcp_id];
+                       ret = amdgpu_xcp_get_inst_details(xcp, AMDGPU_XCP_GFX,
+                                                         &inst_mask);
+                       if (ret)
+                               return ret;
+               } else {
+                       inst_mask = adev->gfx.xcc_mask ? adev->gfx.xcc_mask : 1;
+               }
+
+               inst_mask &= GENMASK(AMDGPU_MAX_GC_INSTANCES - 1, 0);
+               num_xcc = hweight32(inst_mask);
+               num_se = adev->gfx.config.max_shader_engines;
+               num_sa = adev->gfx.config.max_sh_per_se;
+               if (!num_xcc || !num_se || !num_sa)
+                       return -ENODEV;
+
+               nent = num_xcc * num_se * num_sa;
+               sz = struct_size(cu_bitmap, bitmap, nent);
+
+               cu_bitmap = kzalloc(sz, GFP_KERNEL);
+               if (!cu_bitmap)
+                       return -ENOMEM;
+
+               ret = amdgpu_gfx_get_cu_bitmap(adev, inst_mask,
+                                              cu_bitmap->bitmap, nent);
+               if (ret < 0) {
+                       kfree(cu_bitmap);
+                       return ret;
+               }
+
+               cu_bitmap->num_xcc = ret;
+               cu_bitmap->num_se = num_se;
+               cu_bitmap->num_sa = num_sa;
+               cu_bitmap->xcc_mask = inst_mask;
+
+               ret = copy_to_user(out, cu_bitmap, min_t(size_t, size, sz)) ?
+                             -EFAULT : 0;
+               kfree(cu_bitmap);
+               return ret;
+       }
        default:
                DRM_DEBUG_KMS("Invalid request %d\n", info->query);
                return -EINVAL;
diff --git a/include/uapi/drm/amdgpu_drm.h b/include/uapi/drm/amdgpu_drm.h
index 29009f6aabde..b726dc511491 100644
--- a/include/uapi/drm/amdgpu_drm.h
+++ b/include/uapi/drm/amdgpu_drm.h
@@ -1289,6 +1289,8 @@ struct drm_amdgpu_cs_chunk_cp_gfx_shadow {
 #define AMDGPU_INFO_GPUVM_FAULT                        0x23
 /* query FW object size and alignment */
 #define AMDGPU_INFO_UQ_FW_AREAS                        0x24
+/* query the full per-XCC active CU bitmap */
+#define AMDGPU_INFO_CU_BITMAP                  0x25
 
 #define AMDGPU_INFO_MMR_SE_INDEX_SHIFT 0
 #define AMDGPU_INFO_MMR_SE_INDEX_MASK  0xff
@@ -1363,6 +1365,36 @@ struct drm_amdgpu_info {
        };
 };
 
+/*
+ * Reply to AMDGPU_INFO_CU_BITMAP.
+ *
+ * Reports every XCC visible to the file descriptor in
+ * un-folded [xcc][se][sa] order.
+ *
+ * On a partitioned device the reply covers only the XCCs belonging to the
+ * partition the file descriptor was opened against.
+ *
+ * The reply is variable length and carries no fixed topology limits. Query it
+ * twice: first with return_size = sizeof(struct drm_amdgpu_info_cu_bitmap) to
+ * read the counts, then with return_size large enough for the trailing array.
+ */
+struct drm_amdgpu_info_cu_bitmap {
+       /** number of XCC slices described by bitmap[] */
+       __u32 num_xcc;
+       /** number of shader engines per XCC */
+       __u32 num_se;
+       /** number of shader arrays per shader engine */
+       __u32 num_sa;
+       /** mask of logical XCC instances this reply describes */
+       __u32 xcc_mask;
+       /**
+        * active CU mask per shader array, num_xcc * num_se * num_sa entries
+        * in [xcc][se][sa] row-major order. Index as
+        * bitmap[(xcc * num_se + se) * num_sa + sa].
+        */
+       __u32 bitmap[];
+};
+
 struct drm_amdgpu_info_gds {
        /** GDS GFX partition size */
        __u32 gds_gfx_partition_size;
-- 
2.43.0

Reply via email to