AMD-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] drm/amdgpu: add AMDGPU_INFO_CU_BITMAP query for per-XCC CU masks
@ 2026-10-22 16:18 Siwei He
  2026-10-26 18:37 ` Alex Deucher
  0 siblings, 1 reply; 4+ messages in thread
From: Siwei He @ 2026-10-22 16:18 UTC (permalink / raw)
  To: amd-gfx; +Cc: felix.kuehling, harish.kasiviswanathan, mukul.joshi, Siwei He

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 <siwei.he@amd.com>
---
 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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] drm/amdgpu: add AMDGPU_INFO_CU_BITMAP query for per-XCC CU masks
  2026-10-22 16:18 Siwei He
@ 2026-10-26 18:37 ` Alex Deucher
  0 siblings, 0 replies; 4+ messages in thread
From: Alex Deucher @ 2026-10-26 18:37 UTC (permalink / raw)
  To: Siwei He; +Cc: amd-gfx, felix.kuehling, harish.kasiviswanathan, mukul.joshi

On Thu, Oct 22, 2026 at 12:18 PM Siwei He <siwei.he@amd.com> wrote:
>
> 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].
>

Please provide a link in the commit message to the proposed userspace
side which uses this new query.  Other than that, looks good to me.

Alex


> Signed-off-by: Siwei He <siwei.he@amd.com>
> ---
>  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
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] drm/amdgpu: add AMDGPU_INFO_CU_BITMAP query for per-XCC CU masks
@ 2026-12-03 14:53 Siwei He
  2026-12-03 15:20 ` sashiko-bot
  0 siblings, 1 reply; 4+ messages in thread
From: Siwei He @ 2026-12-03 14:53 UTC (permalink / raw)
  To: amd-gfx; +Cc: siwei.he, alexdeucher

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].

Userspace for this can be found at:
https://github.com/ROCm/rocm-systems/pull/14570

Signed-off-by: Siwei He <siwei.he@amd.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 75 +++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h |  2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c | 51 +++++++++++++++++
 include/uapi/drm/amdgpu_drm.h           | 32 +++++++++++
 4 files changed, 160 insertions(+)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c
index 0ae6e990c310..850de3be1659 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c
@@ -2050,6 +2050,81 @@ 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;
+
+	if (!inst_mask)
+		return -ENODEV;
+
+	/* cu_info.bitmap[] only has AMDGPU_MAX_GC_INSTANCES slots */
+	if (inst_mask & ~GENMASK(AMDGPU_MAX_GC_INSTANCES - 1, 0)) {
+		dev_warn_once(adev->dev,
+			      "CU bitmap: %u XCCs exceeds %u slots\n",
+			      hweight32(inst_mask), AMDGPU_MAX_GC_INSTANCES);
+		return -EOPNOTSUPP;
+	}
+
+	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..50943ec18b27 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c
@@ -1453,6 +1453,57 @@ 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 is a logical mask */
+			inst_mask = adev->gfx.xcc_mask ?
+				GENMASK(NUM_XCC(adev->gfx.xcc_mask) - 1, 0) : 1;
+		}
+
+		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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] drm/amdgpu: add AMDGPU_INFO_CU_BITMAP query for per-XCC CU masks
  2026-12-03 14:53 [PATCH] drm/amdgpu: add AMDGPU_INFO_CU_BITMAP query for per-XCC CU masks Siwei He
@ 2026-12-03 15:20 ` sashiko-bot
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-03 15:20 UTC (permalink / raw)
  To: Siwei He, amd-gfx

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] drm/amdgpu: unlocked xcp_mgr and xcp_id read in
  amdgpu_info_ioctl()
- [Medium] drm/amdgpu: stale partition fd exposes all XCCs in
  amdgpu_info_ioctl()
- [Medium] drm/amdgpu: unsynchronized cu_info.bitmap read in
  AMDGPU_INFO_CU_BITMAP
- [Low] drm/amdgpu: misleading XCC count logged in
  amdgpu_gfx_get_cu_bitmap()
- [Low] drm/amdgpu: commit message lacks rationale for
  AMDGPU_INFO_CU_BITMAP
--

commit 820593577ceaf63f676ad746f421ebe4e32b41a4
Author: Siwei He <siwei.he@amd.com>

> drm/amdgpu: add AMDGPU_INFO_CU_BITMAP query for per-XCC CU masks
>
> 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].
>
> Userspace for this can be found at:
> https://github.com/ROCm/rocm-systems/pull/14570
>
> Signed-off-by: Siwei He <siwei.he@amd.com>

[Severity: Low]
Could the commit message explain why a new permanent UAPI query is needed
instead of relying on the external PR link?

Briefly stating the limits of AMDGPU_INFO_DEV_INFO (its cu_bitmap[4][4]
array only covers XCC 0, folds >4 SEs with ASIC-specific packing, and
cannot report per-XCC or XCP-partition-scoped masks) would make the
rationale self-contained in git history.

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c
> index 0ae6e990c3102..850de3be16599 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c

[ ... ]

> +int amdgpu_gfx_get_cu_bitmap(struct amdgpu_device *adev, u32 inst_mask,
> +			     u32 *out, u32 max_entries)
> +{

[ ... ]

> +	/* cu_info.bitmap[] only has AMDGPU_MAX_GC_INSTANCES slots */
> +	if (inst_mask & ~GENMASK(AMDGPU_MAX_GC_INSTANCES - 1, 0)) {
> +		dev_warn_once(adev->dev,
> +			      "CU bitmap: %u XCCs exceeds %u slots\n",
> +			      hweight32(inst_mask), AMDGPU_MAX_GC_INSTANCES);
> +		return -EOPNOTSUPP;
> +	}

[Severity: Low]
Does this warning print the wrong value when a single-XCC partition sits
above AMDGPU_MAX_GC_INSTANCES?

The check rejects any set bit at or above AMDGPU_MAX_GC_INSTANCES, but
hweight32(inst_mask) counts set bits.  For inst_mask == BIT(8), this logs
"CU bitmap: 1 XCCs exceeds 8 slots".  Logging fls(inst_mask) or the mask
itself would match the condition being checked.

[ ... ]

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c
> index d442a2d963945..05b0702ac59cc 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c
> @@ -1452,6 +1452,57 @@ int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)

[ ... ]

> +	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;

[Severity: Medium]
Can this race with a concurrent partition switch or fpriv->xcp_id
assignment because adev->xcp_mgr->xcp_lock is not held?

CPU0                              CPU1
amdgpu_info_ioctl()               amdgpu_gfx_set_compute_partition()
                                    mutex_lock(&xcp_mgr->xcp_lock)
                                    amdgpu_xcp_init()
                                      xcp[i].valid = false
  fpriv->xcp_id < num_xcps
  amdgpu_xcp_get_inst_details()
    if (!xcp->valid)  <-- -EINVAL
                                      __amdgpu_xcp_add_block(GFXHUB)
                                        xcp->valid = true
                                      __amdgpu_xcp_add_block(GFX)
                                      num_xcps = new_num
                                    mutex_unlock(&xcp_mgr->xcp_lock)

Because __amdgpu_xcp_add_block() sets xcp->valid on AMDGPU_XCP_GFXHUB
before updating AMDGPU_XCP_GFX, an unlocked reader can also see a stale
inst_mask.  On a shared primary-node fd, amdgpu_xcp_select_scheds() also
steps fpriv->xcp_id through intermediate values under xcp_lock.

Acquiring adev->xcp_mgr->xcp_lock across the fpriv->xcp_id, num_xcps, and
amdgpu_xcp_get_inst_details() reads and releasing it before kzalloc()
would avoid the race.

> +		} else {
> +			/* inst_mask is a logical mask */
> +			inst_mask = adev->gfx.xcc_mask ?
> +				GENMASK(NUM_XCC(adev->gfx.xcc_mask) - 1, 0) : 1;
> +		}

[Severity: Medium]
Does falling into the else branch expose every XCC on the device to a
stale partition fd?

amdgpu_xcp_open_device() sets fpriv->xcp_id to the partition index for a
partition render node and AMDGPU_XCP_NO_PARTITION otherwise.  If a sysfs
mode switch (such as CPX to SPX) reduces num_xcps below fpriv->xcp_id,
this falls into the else branch and returns all XCCs instead of -ENODEV.

Checking fpriv->xcp_id == AMDGPU_XCP_NO_PARTITION separately from an
out-of-range partition index would preserve the partition-scoping
contract.

[ ... ]

> +		ret = amdgpu_gfx_get_cu_bitmap(adev, inst_mask,
> +					       cu_bitmap->bitmap, nent);

[Severity: Medium]
Can amdgpu_gfx_get_cu_bitmap() read a zeroed or partially populated
cu_info.bitmap during runtime resume or GPU reset on GFX6-8?

CPU0                              CPU1
amdgpu_info_ioctl()               amdgpu_pmops_runtime_resume()
                                    ...
                                    gfx_v8_0_get_cu_info()
                                      memset(cu_info, 0, ...)
  amdgpu_gfx_get_cu_bitmap()
    reads cu_info.bitmap  <-- all-zero or partial mask
                                      repopulates cu_info->bitmap via MMIO

Because amdgpu_driver_open_kms() drops its runtime PM reference on open
and AMDGPU_INFO_CU_BITMAP neither acquires runtime PM nor takes
adev->reset_domain->sem, it can overlap gfx_v{6,7,8}_0_get_cu_info() and
return 0 with zeroed CU masks.

Acquiring runtime PM (PM_RUNTIME_ACQUIRE_IF_ENABLED_AUTOSUSPEND) and
down_read(&adev->reset_domain->sem), as AMDGPU_INFO_READ_MMR_REG does,
would serialize against both paths.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203145332.802558-1-siwei.he@amd.com?part=1

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-03 15:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 14:53 [PATCH] drm/amdgpu: add AMDGPU_INFO_CU_BITMAP query for per-XCC CU masks Siwei He
2026-12-03 15:20 ` sashiko-bot
  -- strict thread matches above, loose matches on Subject: below --
2026-10-22 16:18 Siwei He
2026-10-26 18:37 ` Alex Deucher

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox