]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
accel/amdxdna: Add IOCTL to retrieve realtime NPU power estimate
authorLizhi Hou <lizhi.hou@amd.com>
Sat, 28 Feb 2026 06:10:57 +0000 (00:10 -0600)
committerLizhi Hou <lizhi.hou@amd.com>
Wed, 11 Mar 2026 18:36:51 +0000 (11:36 -0700)
The AMD PMF driver provides an interface to obtain realtime power
estimates for the NPU. Expose this information to userspace through a
new DRM_IOCTL_AMDXDNA_GET_INFO parameter, allowing applications to query
the current NPU power level.

Reviewed-by: Mario Limonciello (AMD) <superm1@kernel.org>
Reviewed-by: Shyam Sundar S K <Shyam-sundar.S-k@amd.com>
(Update comment to indicate power and utilization)
Signed-off-by: Mario Limonciello (AMD) <superm1@kernel.org>
Signed-off-by: Lizhi Hou <lizhi.hou@amd.com>
Link: https://patch.msgid.link/20260228061109.361239-2-superm1@kernel.org
drivers/accel/amdxdna/aie2_pci.c
drivers/accel/amdxdna/aie2_pci.h
drivers/accel/amdxdna/amdxdna_pci_drv.c

index 85079b6fc5d90a1cc0111043ba1f4e2f49b1bc75..a2e586512e26cc84c1d21fa321dd6027636b7360 100644 (file)
@@ -10,6 +10,7 @@
 #include <drm/drm_managed.h>
 #include <drm/drm_print.h>
 #include <drm/gpu_scheduler.h>
+#include <linux/amd-pmf-io.h>
 #include <linux/cleanup.h>
 #include <linux/errno.h>
 #include <linux/firmware.h>
@@ -783,6 +784,31 @@ static int aie2_get_clock_metadata(struct amdxdna_client *client,
        return ret;
 }
 
+static int aie2_get_sensors(struct amdxdna_client *client,
+                           struct amdxdna_drm_get_info *args)
+{
+       struct amdxdna_drm_query_sensor sensor = {};
+       int ret;
+
+       if (args->buffer_size < sizeof(sensor))
+               return -EINVAL;
+
+       ret = AIE2_GET_PMF_NPU_DATA(npu_power, sensor.input);
+       if (ret)
+               return ret;
+       sensor.type = AMDXDNA_SENSOR_TYPE_POWER;
+       sensor.unitm = -3;
+       scnprintf(sensor.label, sizeof(sensor.label), "Total Power");
+       scnprintf(sensor.units, sizeof(sensor.units), "mW");
+
+       if (copy_to_user(u64_to_user_ptr(args->buffer), &sensor, sizeof(sensor)))
+               return -EFAULT;
+
+       args->buffer_size = sizeof(sensor);
+
+       return 0;
+}
+
 static int aie2_hwctx_status_cb(struct amdxdna_hwctx *hwctx, void *arg)
 {
        struct amdxdna_drm_hwctx_entry *tmp __free(kfree) = NULL;
@@ -986,6 +1012,9 @@ static int aie2_get_info(struct amdxdna_client *client, struct amdxdna_drm_get_i
        case DRM_AMDXDNA_QUERY_CLOCK_METADATA:
                ret = aie2_get_clock_metadata(client, args);
                break;
+       case DRM_AMDXDNA_QUERY_SENSORS:
+               ret = aie2_get_sensors(client, args);
+               break;
        case DRM_AMDXDNA_QUERY_HW_CONTEXTS:
                ret = aie2_get_hwctx_status(client, args);
                break;
index b20a3661078cc1b27547501ae363568358beb651..1bb88711bedb3bb7c01a22dd74cb96b0e2184992 100644 (file)
@@ -7,6 +7,7 @@
 #define _AIE2_PCI_H_
 
 #include <drm/amdxdna_accel.h>
+#include <linux/limits.h>
 #include <linux/semaphore.h>
 
 #include "amdxdna_mailbox.h"
        pci_resource_len(NDEV2PDEV(_ndev), (_ndev)->xdna->dev_info->mbox_bar); \
 })
 
+#if IS_ENABLED(CONFIG_AMD_PMF)
+#define AIE2_GET_PMF_NPU_DATA(field, val)                              \
+({                                                                     \
+       struct amd_pmf_npu_metrics _npu_metrics;                        \
+       int _ret;                                                       \
+                                                                       \
+       _ret = amd_pmf_get_npu_data(&_npu_metrics);                     \
+       val = _ret ? U32_MAX : _npu_metrics.field;                      \
+       (_ret);                                                         \
+})
+#else
+#define SENSOR_DEFAULT_npu_power       U32_MAX
+#define AIE2_GET_PMF_NPU_DATA(field, val)                              \
+({                                                                     \
+       val = SENSOR_DEFAULT_##field;                                   \
+       (-EOPNOTSUPP);                                                  \
+})
+#endif
+
 enum aie2_smu_reg_idx {
        SMU_CMD_REG = 0,
        SMU_ARG_REG,
index 0bfd2b487ac2e18ab29e7026ccf78b1aea0bb1f2..d6d12355bd2b7fde229811850fa5b1bb8a81af2e 100644 (file)
@@ -35,9 +35,10 @@ MODULE_FIRMWARE("amdnpu/17f0_11/npu_7.sbin");
  * 0.4: Support getting resource information
  * 0.5: Support getting telemetry data
  * 0.6: Support preemption
+ * 0.7: Support getting power and utilization data
  */
 #define AMDXDNA_DRIVER_MAJOR           0
-#define AMDXDNA_DRIVER_MINOR           6
+#define AMDXDNA_DRIVER_MINOR           7
 
 /*
  * Bind the driver base on (vendor_id, device_id) pair and later use the