Commit f66d6cc6 authored by Mario Limonciello (AMD)'s avatar Mario Limonciello (AMD) Committed by Lizhi Hou
Browse files

accel/amdxdna: Support sensors for column utilization



The AMD PMF driver provides realtime column utilization (npu_busy)
metrics for the NPU. Extend the DRM_IOCTL_AMDXDNA_GET_INFO sensor
query to expose these metrics to userspace.

Add AMDXDNA_SENSOR_TYPE_COLUMN_UTILIZATION to the sensor type enum
and update aie2_get_sensors() to return both the total power and up
to 8 column utilization sensors if the user buffer permits.

Signed-off-by: default avatarMario Limonciello (AMD) <superm1@kernel.org>
Reviewed-by: default avatarLizhi Hou <lizhi.hou@amd.com>
[lizhi: support legacy tool which uses small buffer. checkpatch cleanup]
Signed-off-by: default avatarLizhi Hou <lizhi.hou@amd.com>
Link: https://patch.msgid.link/20260311171842.473453-1-lizhi.hou@amd.com
parent f3bffef0
Loading
Loading
Loading
Loading
+29 −5
Original line number Diff line number Diff line
@@ -787,16 +787,18 @@ static int aie2_get_clock_metadata(struct amdxdna_client *client,
static int aie2_get_sensors(struct amdxdna_client *client,
			    struct amdxdna_drm_get_info *args)
{
	struct amdxdna_dev_hdl *ndev = client->xdna->dev_handle;
	struct amdxdna_drm_query_sensor sensor = {};
	struct amd_pmf_npu_metrics npu_metrics;
	u32 sensors_count = 0, i;
	int ret;

	if (args->buffer_size < sizeof(sensor))
		return -EINVAL;

	ret = AIE2_GET_PMF_NPU_DATA(npu_power, sensor.input);
	ret = AIE2_GET_PMF_NPU_METRICS(&npu_metrics);
	if (ret)
		return ret;

	sensor.type = AMDXDNA_SENSOR_TYPE_POWER;
	sensor.input = npu_metrics.npu_power;
	sensor.unitm = -3;
	scnprintf(sensor.label, sizeof(sensor.label), "Total Power");
	scnprintf(sensor.units, sizeof(sensor.units), "mW");
@@ -804,7 +806,29 @@ static int aie2_get_sensors(struct amdxdna_client *client,
	if (copy_to_user(u64_to_user_ptr(args->buffer), &sensor, sizeof(sensor)))
		return -EFAULT;

	args->buffer_size = sizeof(sensor);
	sensors_count++;
	if (args->buffer_size <= sensors_count * sizeof(sensor))
		goto out;

	for (i = 0; i < min_t(u32, ndev->total_col, 8); i++) {
		memset(&sensor, 0, sizeof(sensor));
		sensor.input = npu_metrics.npu_busy[i];
		sensor.type = AMDXDNA_SENSOR_TYPE_COLUMN_UTILIZATION;
		sensor.unitm = 0;
		scnprintf(sensor.label, sizeof(sensor.label), "Column %d Utilization", i);
		scnprintf(sensor.units, sizeof(sensor.units), "%%");

		if (copy_to_user(u64_to_user_ptr(args->buffer) + sensors_count * sizeof(sensor),
				 &sensor, sizeof(sensor)))
			return -EFAULT;

		sensors_count++;
		if (args->buffer_size <= sensors_count * sizeof(sensor))
			goto out;
	}

out:
	args->buffer_size = sensors_count * sizeof(sensor);

	return 0;
}
+8 −0
Original line number Diff line number Diff line
@@ -48,6 +48,7 @@
})

#if IS_ENABLED(CONFIG_AMD_PMF)
#define AIE2_GET_PMF_NPU_METRICS(metrics) amd_pmf_get_npu_data(metrics)
#define AIE2_GET_PMF_NPU_DATA(field, val)				\
({									\
	struct amd_pmf_npu_metrics _npu_metrics;			\
@@ -58,6 +59,13 @@
	(_ret);								\
})
#else
#define AIE2_GET_PMF_NPU_METRICS(metrics)				\
({									\
	typeof(metrics) _m = metrics;					\
	memset(_m, 0xff, sizeof(*_m));					\
	(-EOPNOTSUPP);							\
})

#define SENSOR_DEFAULT_npu_power	U32_MAX
#define AIE2_GET_PMF_NPU_DATA(field, val)				\
({									\
+2 −1
Original line number Diff line number Diff line
@@ -353,7 +353,8 @@ struct amdxdna_drm_query_clock_metadata {
};

enum amdxdna_sensor_type {
	AMDXDNA_SENSOR_TYPE_POWER
	AMDXDNA_SENSOR_TYPE_POWER,
	AMDXDNA_SENSOR_TYPE_COLUMN_UTILIZATION
};

/**