The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
To: Lizhi Hou <lizhi.hou@amd.com>
Cc: Hans de Goede <hansg@kernel.org>,
	ogabbay@kernel.org,  quic_jhugo@quicinc.com,
	maciej.falkowski@linux.intel.com,
	 LKML <linux-kernel@vger.kernel.org>,
	max.zhen@amd.com,  sonal.santan@amd.com,
	mario.limonciello@amd.com,  platform-driver-x86@vger.kernel.org,
	dri-devel@lists.freedesktop.org,  Shyam-sundar.S-k@amd.com,
	VinitKumar.Shukla@amd.com
Subject: Re: [PATCH V2 2/2] accel/amdxdna: Add IOCTL to retrieve realtime NPU power estimate
Date: Thu, 11 Dec 2025 20:24:23 +0200 (EET)	[thread overview]
Message-ID: <9dd39a22-608b-12a8-ffa1-3937be62f47d@linux.intel.com> (raw)
In-Reply-To: <20251211175802.1760860-3-lizhi.hou@amd.com>

On Thu, 11 Dec 2025, Lizhi Hou wrote:

> 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.
> 
> Signed-off-by: Lizhi Hou <lizhi.hou@amd.com>
> ---
>  drivers/accel/amdxdna/aie2_pci.c        | 29 +++++++++++++++++++++++++
>  drivers/accel/amdxdna/aie2_pci.h        | 18 +++++++++++++++
>  drivers/accel/amdxdna/amdxdna_pci_drv.c |  3 ++-
>  3 files changed, 49 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/accel/amdxdna/aie2_pci.c b/drivers/accel/amdxdna/aie2_pci.c
> index 81a8e4137bfd..4a2c7addcd79 100644
> --- a/drivers/accel/amdxdna/aie2_pci.c
> +++ b/drivers/accel/amdxdna/aie2_pci.c
> @@ -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>
> @@ -777,6 +778,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 = { 0 };

= {} is enough to initialize to defaults.

> +	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;
> +	snprintf(sensor.label, sizeof(sensor.label), "Total Power");
> +	snprintf(sensor.units, sizeof(sensor.units), "mW");

scnprintf() x2

> +
> +	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;
> @@ -980,6 +1006,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;
> diff --git a/drivers/accel/amdxdna/aie2_pci.h b/drivers/accel/amdxdna/aie2_pci.h
> index c6b5cf4ae5c4..edf6f2e00dea 100644
> --- a/drivers/accel/amdxdna/aie2_pci.h
> +++ b/drivers/accel/amdxdna/aie2_pci.h
> @@ -46,6 +46,24 @@
>  	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; \

#include needed for U32_MAX.

Unrelated to this patch, this files is also missing include for at least 
SZ_* (so likely works by chance) and types.h.

> +	(_ret); \
> +})

Please try to align the backslashed to right so that they don't mix up 
with the code.

Add an empty lines after variables declaration (with the backslash in 
the end obviously) so these almost looks like "normal" coding style.

> +#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,
> diff --git a/drivers/accel/amdxdna/amdxdna_pci_drv.c b/drivers/accel/amdxdna/amdxdna_pci_drv.c
> index 1973ab67721b..643ebd387074 100644
> --- a/drivers/accel/amdxdna/amdxdna_pci_drv.c
> +++ b/drivers/accel/amdxdna/amdxdna_pci_drv.c
> @@ -32,9 +32,10 @@ MODULE_FIRMWARE("amdnpu/17f0_20/npu.sbin");
>   * 0.4: Support getting resource information
>   * 0.5: Support getting telemetry data
>   * 0.6: Support preemption
> + * 0.7: Support getting power 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
> 

-- 
 i.


  parent reply	other threads:[~2025-12-11 18:24 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-11 17:58 [PATCH V1 0/2] Get real time power input via AMD PMF Lizhi Hou
2025-12-11 17:58 ` [PATCH V1 1/2] platform/x86/amd/pmf: Introduce new interface to export NPU metrics Lizhi Hou
2025-12-11 18:28   ` Ilpo Järvinen
2025-12-11 19:07     ` Lizhi Hou
2025-12-12  6:00     ` Shyam Sundar S K
2025-12-11 17:58 ` [PATCH V2 2/2] accel/amdxdna: Add IOCTL to retrieve realtime NPU power estimate Lizhi Hou
2025-12-11 18:20   ` Mario Limonciello
2025-12-11 18:24   ` Ilpo Järvinen [this message]
2025-12-11 19:06     ` Lizhi Hou
2025-12-12  6:01   ` Shyam Sundar S K
2025-12-11 18:22 ` [PATCH V1 0/2] Get real time power input via AMD PMF Mario Limonciello
2025-12-11 19:16   ` Lizhi Hou
  -- strict thread matches above, loose matches on Subject: below --
2025-12-12 18:18 [PATCH V2 " Lizhi Hou
2025-12-12 18:18 ` [PATCH V2 2/2] accel/amdxdna: Add IOCTL to retrieve realtime NPU power estimate Lizhi Hou

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9dd39a22-608b-12a8-ffa1-3937be62f47d@linux.intel.com \
    --to=ilpo.jarvinen@linux.intel.com \
    --cc=Shyam-sundar.S-k@amd.com \
    --cc=VinitKumar.Shukla@amd.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=hansg@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lizhi.hou@amd.com \
    --cc=maciej.falkowski@linux.intel.com \
    --cc=mario.limonciello@amd.com \
    --cc=max.zhen@amd.com \
    --cc=ogabbay@kernel.org \
    --cc=platform-driver-x86@vger.kernel.org \
    --cc=quic_jhugo@quicinc.com \
    --cc=sonal.santan@amd.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox