[PATCH V4] platform/x86/amd/pmf: Introduce new interface to export NPU metrics

Lizhi Hou posted 1 patch 3 weeks, 2 days ago
drivers/platform/x86/amd/pmf/core.c | 76 +++++++++++++++++++++++++++++
drivers/platform/x86/amd/pmf/pmf.h  |  2 +
include/linux/amd-pmf-io.h          | 21 ++++++++
3 files changed, 99 insertions(+)
[PATCH V4] platform/x86/amd/pmf: Introduce new interface to export NPU metrics
Posted by Lizhi Hou 3 weeks, 2 days ago
From: Shyam Sundar S K <Shyam-sundar.S-k@amd.com>

The PMF driver retrieves NPU metrics data from the PMFW. Introduce a new
interface to make NPU metrics accessible to other drivers like AMDXDNA
driver, which can access and utilize this information as needed.

Reviewed-by: Mario Limonciello <mario.limonciello@amd.com>
Co-developed-by: Patil Rajesh Reddy <Patil.Reddy@amd.com>
Signed-off-by: Patil Rajesh Reddy <Patil.Reddy@amd.com>
Signed-off-by: Shyam Sundar S K <Shyam-sundar.S-k@amd.com>
[lizhi: save return value of is_npu_metrics_supported() and return it]
Signed-off-by: Lizhi Hou <lizhi.hou@amd.com>
---
 drivers/platform/x86/amd/pmf/core.c | 76 +++++++++++++++++++++++++++++
 drivers/platform/x86/amd/pmf/pmf.h  |  2 +
 include/linux/amd-pmf-io.h          | 21 ++++++++
 3 files changed, 99 insertions(+)

diff --git a/drivers/platform/x86/amd/pmf/core.c b/drivers/platform/x86/amd/pmf/core.c
index 9f4a1f79459a..b53be1d9cb45 100644
--- a/drivers/platform/x86/amd/pmf/core.c
+++ b/drivers/platform/x86/amd/pmf/core.c
@@ -8,6 +8,8 @@
  * Author: Shyam Sundar S K <Shyam-sundar.S-k@amd.com>
  */
 
+#include <linux/array_size.h>
+#include <linux/cleanup.h>
 #include <linux/debugfs.h>
 #include <linux/iopoll.h>
 #include <linux/module.h>
@@ -15,6 +17,7 @@
 #include <linux/pci.h>
 #include <linux/platform_device.h>
 #include <linux/power_supply.h>
+#include <linux/string.h>
 #include <asm/amd/node.h>
 #include "pmf.h"
 
@@ -54,6 +57,8 @@ static bool force_load;
 module_param(force_load, bool, 0444);
 MODULE_PARM_DESC(force_load, "Force load this driver on supported older platforms (experimental)");
 
+static struct device *pmf_device;
+
 static int amd_pmf_pwr_src_notify_call(struct notifier_block *nb, unsigned long event, void *data)
 {
 	struct amd_pmf_dev *pmf = container_of(nb, struct amd_pmf_dev, pwr_src_notifier);
@@ -315,6 +320,71 @@ int amd_pmf_init_metrics_table(struct amd_pmf_dev *dev)
 	return 0;
 }
 
+static int is_npu_metrics_supported(struct amd_pmf_dev *pdev)
+{
+	switch (pdev->cpu_id) {
+	case PCI_DEVICE_ID_AMD_1AH_M20H_ROOT:
+	case PCI_DEVICE_ID_AMD_1AH_M60H_ROOT:
+		return 0;
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
+static int amd_pmf_get_smu_metrics(struct amd_pmf_dev *dev, struct amd_pmf_npu_metrics *data)
+{
+	int ret, i;
+
+	guard(mutex)(&dev->metrics_mutex);
+
+	ret = is_npu_metrics_supported(dev);
+	if (ret)
+		return ret;
+
+	ret = amd_pmf_set_dram_addr(dev, true);
+	if (ret)
+		return ret;
+
+	memset(dev->buf, 0, dev->mtable_size);
+
+	/* Send SMU command to get NPU metrics */
+	ret = amd_pmf_send_cmd(dev, SET_TRANSFER_TABLE, SET_CMD, METRICS_TABLE_ID, NULL);
+	if (ret) {
+		dev_err(dev->dev, "SMU command failed to get NPU metrics: %d\n", ret);
+		return ret;
+	}
+
+	memcpy(&dev->m_table_v2, dev->buf, dev->mtable_size);
+
+	data->npuclk_freq = dev->m_table_v2.npuclk_freq;
+	for (i = 0; i < ARRAY_SIZE(data->npu_busy); i++)
+		data->npu_busy[i] = dev->m_table_v2.npu_busy[i];
+	data->npu_power = dev->m_table_v2.npu_power;
+	data->mpnpuclk_freq = dev->m_table_v2.mpnpuclk_freq;
+	data->npu_reads = dev->m_table_v2.npu_reads;
+	data->npu_writes = dev->m_table_v2.npu_writes;
+
+	return 0;
+}
+
+int amd_pmf_get_npu_data(struct amd_pmf_npu_metrics *info)
+{
+	struct amd_pmf_dev *pdev;
+
+	if (!info)
+		return -EINVAL;
+
+	if (!pmf_device)
+		return -ENODEV;
+
+	pdev = dev_get_drvdata(pmf_device);
+	if (!pdev)
+		return -ENODEV;
+
+	return amd_pmf_get_smu_metrics(pdev, info);
+}
+EXPORT_SYMBOL_NS_GPL(amd_pmf_get_npu_data, "AMD_PMF");
+
 static int amd_pmf_suspend_handler(struct device *dev)
 {
 	struct amd_pmf_dev *pdev = dev_get_drvdata(dev);
@@ -482,6 +552,10 @@ static int amd_pmf_probe(struct platform_device *pdev)
 	if (err)
 		return err;
 
+	err = devm_mutex_init(dev->dev, &dev->metrics_mutex);
+	if (err)
+		return err;
+
 	apmf_acpi_init(dev);
 	platform_set_drvdata(pdev, dev);
 	amd_pmf_dbgfs_register(dev);
@@ -490,6 +564,8 @@ static int amd_pmf_probe(struct platform_device *pdev)
 	if (is_apmf_func_supported(dev, APMF_FUNC_SBIOS_HEARTBEAT_V2))
 		amd_pmf_notify_sbios_heartbeat_event_v2(dev, ON_LOAD);
 
+	pmf_device = dev->dev;
+
 	dev_info(dev->dev, "registered PMF device successfully\n");
 
 	return 0;
diff --git a/drivers/platform/x86/amd/pmf/pmf.h b/drivers/platform/x86/amd/pmf/pmf.h
index e65a7eca0508..d76894c9c822 100644
--- a/drivers/platform/x86/amd/pmf/pmf.h
+++ b/drivers/platform/x86/amd/pmf/pmf.h
@@ -12,6 +12,7 @@
 #define PMF_H
 
 #include <linux/acpi.h>
+#include <linux/amd-pmf-io.h>
 #include <linux/circ_buf.h>
 #include <linux/input.h>
 #include <linux/mutex_types.h>
@@ -434,6 +435,7 @@ struct amd_pmf_dev {
 	bool cb_flag;			     /* To handle first custom BIOS input */
 	struct pmf_cbi_ring_buffer cbi_buf;
 	struct mutex cbi_mutex;		     /* Protects ring buffer access */
+	struct mutex metrics_mutex;
 };
 
 struct apmf_sps_prop_granular_v2 {
diff --git a/include/linux/amd-pmf-io.h b/include/linux/amd-pmf-io.h
index 6fa510f419c0..55198d2875cc 100644
--- a/include/linux/amd-pmf-io.h
+++ b/include/linux/amd-pmf-io.h
@@ -61,5 +61,26 @@ enum laptop_placement {
 	LP_UNDEFINED,
 };
 
+/**
+ * struct amd_pmf_npu_metrics: Get NPU metrics data from PMF driver
+ * @npuclk_freq: NPU clock frequency [MHz]
+ * @npu_busy: NPU busy % [0-100]
+ * @npu_power: NPU power [mW]
+ * @mpnpuclk_freq: MPNPU [MHz]
+ * @npu_reads: NPU read bandwidth [MB/sec]
+ * @npu_writes: NPU write bandwidth [MB/sec]
+ */
+struct amd_pmf_npu_metrics {
+	u16 npuclk_freq;
+	u16 npu_busy[8];
+	u16 npu_power;
+	u16 mpnpuclk_freq;
+	u16 npu_reads;
+	u16 npu_writes;
+};
+
 int amd_get_sfh_info(struct amd_sfh_info *sfh_info, enum sfh_message_type op);
+
+/* AMD PMF and NPU interface */
+int amd_pmf_get_npu_data(struct amd_pmf_npu_metrics *info);
 #endif
-- 
2.34.1
Re: [PATCH V4] platform/x86/amd/pmf: Introduce new interface to export NPU metrics
Posted by Ilpo Järvinen 2 weeks, 4 days ago
On Thu, 15 Jan 2026 09:34:48 -0800, Lizhi Hou wrote:

> The PMF driver retrieves NPU metrics data from the PMFW. Introduce a new
> interface to make NPU metrics accessible to other drivers like AMDXDNA
> driver, which can access and utilize this information as needed.
> 
> 


Thank you for your contribution, it has been applied to my local
review-ilpo-next branch. Note it will show up in the public
platform-drivers-x86/review-ilpo-next branch only once I've pushed my
local branch there, which might take a while.

The list of commits applied:
[1/1] platform/x86/amd/pmf: Introduce new interface to export NPU metrics
      commit: 15e19bd95be5c79f2f98b727edda002ba86f8d03

--
 i.
Re: [PATCH V4] platform/x86/amd/pmf: Introduce new interface to export NPU metrics
Posted by Mario Limonciello 1 week, 4 days ago

On 1/20/2026 8:54 AM, Ilpo Järvinen wrote:
> On Thu, 15 Jan 2026 09:34:48 -0800, Lizhi Hou wrote:
> 
>> The PMF driver retrieves NPU metrics data from the PMFW. Introduce a new
>> interface to make NPU metrics accessible to other drivers like AMDXDNA
>> driver, which can access and utilize this information as needed.
>>
>>
> 
> 
> Thank you for your contribution, it has been applied to my local
> review-ilpo-next branch. Note it will show up in the public
> platform-drivers-x86/review-ilpo-next branch only once I've pushed my
> local branch there, which might take a while.
> 
> The list of commits applied:
> [1/1] platform/x86/amd/pmf: Introduce new interface to export NPU metrics
>        commit: 15e19bd95be5c79f2f98b727edda002ba86f8d03
> 
> --
>   i.
> 

Lizhi,

One clarification.  What about the XDNA driver side of this change, are 
you still going to do it this cycle?

If so; this either needs an immutable branch from Ilpo or that side of 
the change needs to go through pdx86 as well.

Thanks,
Re: [PATCH V4] platform/x86/amd/pmf: Introduce new interface to export NPU metrics
Posted by Ilpo Järvinen 1 week, 4 days ago
On Mon, 26 Jan 2026, Mario Limonciello wrote:

> 
> 
> On 1/20/2026 8:54 AM, Ilpo Järvinen wrote:
> > On Thu, 15 Jan 2026 09:34:48 -0800, Lizhi Hou wrote:
> > 
> > > The PMF driver retrieves NPU metrics data from the PMFW. Introduce a new
> > > interface to make NPU metrics accessible to other drivers like AMDXDNA
> > > driver, which can access and utilize this information as needed.
> > > 
> > > 
> > 
> > 
> > Thank you for your contribution, it has been applied to my local
> > review-ilpo-next branch. Note it will show up in the public
> > platform-drivers-x86/review-ilpo-next branch only once I've pushed my
> > local branch there, which might take a while.
> > 
> > The list of commits applied:
> > [1/1] platform/x86/amd/pmf: Introduce new interface to export NPU metrics
> >        commit: 15e19bd95be5c79f2f98b727edda002ba86f8d03
> > 
> > --
> >   i.
> > 
> 
> Lizhi,
> 
> One clarification.  What about the XDNA driver side of this change, are you
> still going to do it this cycle?
> 
> If so; this either needs an immutable branch from Ilpo or that side of the
> change needs to go through pdx86 as well.

Hi Mario,

I was told to me by Lizhi the xdna side patch not going to pursued in 
this cycle [1] so I went with that and changing that now is not realistic 
because of merges I've performed to for-next (I could have easily done 
IB for the change back when applying the patch which would have been the 
default but given that mention, I didn't bother).

[1] https://lore.kernel.org/all/93210832-aa75-7639-c9d2-99ec802d88e4@amd.com/

-- 
 i.
Re: [PATCH V4] platform/x86/amd/pmf: Introduce new interface to export NPU metrics
Posted by Mario Limonciello 1 week, 4 days ago

On 1/27/2026 5:46 AM, Ilpo Järvinen wrote:
> On Mon, 26 Jan 2026, Mario Limonciello wrote:
> 
>>
>>
>> On 1/20/2026 8:54 AM, Ilpo Järvinen wrote:
>>> On Thu, 15 Jan 2026 09:34:48 -0800, Lizhi Hou wrote:
>>>
>>>> The PMF driver retrieves NPU metrics data from the PMFW. Introduce a new
>>>> interface to make NPU metrics accessible to other drivers like AMDXDNA
>>>> driver, which can access and utilize this information as needed.
>>>>
>>>>
>>>
>>>
>>> Thank you for your contribution, it has been applied to my local
>>> review-ilpo-next branch. Note it will show up in the public
>>> platform-drivers-x86/review-ilpo-next branch only once I've pushed my
>>> local branch there, which might take a while.
>>>
>>> The list of commits applied:
>>> [1/1] platform/x86/amd/pmf: Introduce new interface to export NPU metrics
>>>         commit: 15e19bd95be5c79f2f98b727edda002ba86f8d03
>>>
>>> --
>>>    i.
>>>
>>
>> Lizhi,
>>
>> One clarification.  What about the XDNA driver side of this change, are you
>> still going to do it this cycle?
>>
>> If so; this either needs an immutable branch from Ilpo or that side of the
>> change needs to go through pdx86 as well.
> 
> Hi Mario,
> 
> I was told to me by Lizhi the xdna side patch not going to pursued in
> this cycle [1] so I went with that and changing that now is not realistic
> because of merges I've performed to for-next (I could have easily done
> IB for the change back when applying the patch which would have been the
> default but given that mention, I didn't bother).
> 
> [1] https://lore.kernel.org/all/93210832-aa75-7639-c9d2-99ec802d88e4@amd.com/
> 

Ah, thanks for confirming, understood.