Commit f3bffef0 authored by Lizhi Hou's avatar Lizhi Hou
Browse files

accel/amdxdna: Add IOCTL to retrieve realtime NPU power estimate



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: default avatarMario Limonciello (AMD) <superm1@kernel.org>
Reviewed-by: default avatarShyam Sundar S K <Shyam-sundar.S-k@amd.com>
(Update comment to indicate power and utilization)
Signed-off-by: default avatarMario Limonciello (AMD) <superm1@kernel.org>
Signed-off-by: default avatarLizhi Hou <lizhi.hou@amd.com>
Link: https://patch.msgid.link/20260228061109.361239-2-superm1@kernel.org
parent efa3382a
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
@@ -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;
+20 −0
Original line number Diff line number Diff line
@@ -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"
@@ -46,6 +47,25 @@
	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,
+2 −1
Original line number Diff line number Diff line
@@ -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