accel/amdxdna: Support sensors for column utilization
Lizhi Hou
maintainerPATCH v2 1/1 accel/amdxdna: Support sensors for column utilization
From: "Mario Limonciello (AMD)" <[email protected]>
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.
--- drivers/accel/amdxdna/aie2_pci.c | 34 +++++++++++++++++++++++++++----- drivers/accel/amdxdna/aie2_pci.h | 8 ++++++++ include/uapi/drm/amdxdna_accel.h | 3 ++- 3 files changed, 39 insertions(+), 6 deletions(-)
drivers/accel/amdxdna/aie2_pci.c +29 -5
@@ -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 ; }
drivers/accel/amdxdna/aie2_pci.h +8 -0
@@ -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) \ ({ \
include/uapi/drm/amdxdna_accel.h +2 -1
@@ -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 };/**
Signature · 1 line
2.34.1
Quoted 6 lines · Lizhi Hou
From: "Mario Limonciello (AMD)" <[email protected]>
The AMD PMF driver provides realtime column utilization (
npu_busy) metrics for the NPU. Extend theDRM_IOCTL_AMDXDNA_GET_INFOsensor query to expose these metrics to userspace.
Quoted 8 lines
Add
AMDXDNA_SENSOR_TYPE_COLUMN_UTILIZATIONto the sensor type enum and updateaie2_get_sensors()to return both the total power and up to 8 column utilization sensors if the user buffer permits.Signed-off-by Mario Limonciello (AMD) <[email protected]>Reviewed-by Lizhi Hou <[email protected]>[lizhi: support legacy tool which uses small buffer. checkpatch cleanup]Signed-off-by Lizhi Hou <[email protected]>
Thanks, no concerns on the cleanups.
Quoted 104 lines
--- drivers/accel/amdxdna/aie2_pci.c | 34 +++++++++++++++++++++++++++----- drivers/accel/amdxdna/aie2_pci.h | 8 ++++++++ include/uapi/drm/amdxdna_accel.h | 3 ++- 3 files changed, 39 insertions(+), 6 deletions(-)drivers/accel/amdxdna/aie2_pci.c +29
-5@@ -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 ; }drivers/accel/amdxdna/aie2_pci.h +8
-0@@ -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) \ ({ \include/uapi/drm/amdxdna_accel.h +2
-1@@ -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 };/**
Applied to drm-misc-next
Quoted 123 lines · Mario Limonciello
Quoted 6 lines · Lizhi Hou
From: "Mario Limonciello (AMD)" <[email protected]>
The AMD PMF driver provides realtime column utilization (
npu_busy) metrics for the NPU. Extend theDRM_IOCTL_AMDXDNA_GET_INFOsensor query to expose these metrics to userspace.Quoted 8 lines
Add
AMDXDNA_SENSOR_TYPE_COLUMN_UTILIZATIONto the sensor type enum and updateaie2_get_sensors()to return both the total power and up to 8 column utilization sensors if the user buffer permits.Signed-off-by Mario Limonciello (AMD) <[email protected]>Reviewed-by Lizhi Hou <[email protected]>[lizhi: support legacy tool which uses small buffer. checkpatch cleanup]Signed-off-by Lizhi Hou <[email protected]>Thanks, no concerns on the cleanups.
Quoted 102 lines
---
drivers/accel/amdxdna/aie2_pci.c | 34 +++++++++++++++++++++++++++----- drivers/accel/amdxdna/aie2_pci.h | 8 ++++++++ include/uapi/drm/amdxdna_accel.h | 3 ++- 3 files changed, 39 insertions(+), 6 deletions(-)drivers/accel/amdxdna/aie2_pci.c +6
-3@@ -787,16 +787,18 @@ static int aie2_get_clock_metadata(structamdxdna_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");drivers/accel/amdxdna/aie2_pci.c +23
-0@@ -804,7 +806,29 @@ static int aie2_get_sensors(structamdxdna_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;
}drivers/accel/amdxdna/aie2_pci.h +8
-0@@ -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) \ ({ \include/uapi/drm/amdxdna_accel.h +2
-1@@ -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 }; /**