Skip to contentSkip to conversation

Get real time power input via AMD PMF

PATCH Superseded Lizhi Hou 3 participants 11 messages Jan 5 – Jan 15

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Lizhi Hou

author maintainer
#0 lore raw

v3 cover Get real time power input via AMD PMF

Adding new NPU metrics API to AMD PMF driver is pending because of lacking real case.
https://lore.kernel.org/all/[email protected]/

Create xdna driver patch to fetch real time power input via PMF API. Here is an example output with xrt-smi(1) tool.

# xrt-smi examine -r all | grep Power
  Power Mode             : Default
Estimated Power          : 0.563 Watts

v3:
Save return value of is_npu_metrics_supported() and return it

V2:
Including header file for struct mutex will be added by
https://patchwork.kernel.org/project/platform-driver-x86/patch/[email protected]/
Add include for U32_MAX
Replace snprintf by scnprintf
Fix coding style

Lizhi Hou (1):
accel/amdxdna: Add IOCTL to retrieve realtime NPU power estimate

Shyam Sundar S K (1):
platform/x86/amd/pmf: Introduce new interface to export NPU metrics

 drivers/accel/amdxdna/aie2_pci.c        | 29 ++++++++++
 drivers/accel/amdxdna/aie2_pci.h        | 20 +++++++
 drivers/accel/amdxdna/amdxdna_pci_drv.c |  3 +-
 drivers/platform/x86/amd/pmf/core.c     | 76 +++++++++++++++++++++++++
 drivers/platform/x86/amd/pmf/pmf.h      |  2 +
 include/linux/amd-pmf-io.h              | 21 +++++++
 6 files changed, 150 insertions(+), 1 deletion(-)
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH v3 1/2 platform/x86/amd/pmf: Introduce new interface to export NPU metrics

From: Shyam Sundar S K <[email protected]>

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.

[lizhi: save return value of is_npu_metrics_supported() and return it]
---
 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(+)
drivers/platform/x86/amd/pmf/core.c +76 -0
index 8fc293c9c538..d180a39e03bf 100644
@@ -8,12 +8,15 @@
  * Author: Shyam Sundar S K <[email protected]>
  */
 
+#include <linux/array_size.h>
+#include <linux/cleanup.h>
 #include <linux/debugfs.h>
 #include <linux/iopoll.h>
 #include <linux/module.h>
 #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"
 
@@ -53,6 +56,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);
@@ -314,6 +319,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_GPL(amd_pmf_get_npu_data);
+
 static int amd_pmf_suspend_handler(struct device *dev)
 {
 	struct amd_pmf_dev *pdev = dev_get_drvdata(dev);
@@ -477,6 +547,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);
@@ -485,6 +559,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;
drivers/platform/x86/amd/pmf/pmf.h +2 -0
index 9144c8c3bbaf..1bf896c5b92c 100644
@@ -12,6 +12,7 @@
 #define PMF_H
 
 #include <linux/acpi.h>
+#include <linux/amd-pmf-io.h>
 #include <linux/input.h>
 #include <linux/platform_device.h>
 #include <linux/platform_profile.h>
@@ -413,6 +414,7 @@ struct amd_pmf_dev {
 	struct apmf_sbios_req_v1 req1;
 	struct pmf_bios_inputs_prev cb_prev; /* To preserve custom BIOS inputs */
 	bool cb_flag;			     /* To handle first custom BIOS input */
+	struct mutex metrics_mutex;
 };
 
 struct apmf_sps_prop_granular_v2 {
include/linux/amd-pmf-io.h +21 -0
index 6fa510f419c0..55198d2875cc 100644
@@ -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
Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [PATCH V3 1/2] platform/x86/amd/pmf: Introduce new interface to export NPU metrics

Quoted 6 lines · Lizhi Hou

From: Shyam Sundar S K <[email protected]>

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.

Quoted 6 lines
[lizhi: save return value of is_npu_metrics_supported() and return it]

Lizhi, just noticied that this patch may not apply cleanly on Ilpo-next (which tree does this change land? drm-misc or platform-x86)? If platform-x86, I can share you a rebased version offline so that you can wrap into XDNA series.

Thanks,
Shyam

Quoted 178 lines
---
 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(+)
drivers/platform/x86/amd/pmf/core.c +76 -0
index 8fc293c9c538..d180a39e03bf 100644
@@ -8,12 +8,15 @@
  * Author: Shyam Sundar S K <[email protected]>
  */
 
+#include <linux/array_size.h>
+#include <linux/cleanup.h>
 #include <linux/debugfs.h>
 #include <linux/iopoll.h>
 #include <linux/module.h>
 #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"
 
@@ -53,6 +56,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);
@@ -314,6 +319,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_GPL(amd_pmf_get_npu_data);
+
 static int amd_pmf_suspend_handler(struct device *dev)
 {
 	struct amd_pmf_dev *pdev = dev_get_drvdata(dev);
@@ -477,6 +547,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);
@@ -485,6 +559,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;
drivers/platform/x86/amd/pmf/pmf.h +2 -0
index 9144c8c3bbaf..1bf896c5b92c 100644
@@ -12,6 +12,7 @@
 #define PMF_H
 
 #include <linux/acpi.h>
+#include <linux/amd-pmf-io.h>
 #include <linux/input.h>
 #include <linux/platform_device.h>
 #include <linux/platform_profile.h>
@@ -413,6 +414,7 @@ struct amd_pmf_dev {
 	struct apmf_sbios_req_v1 req1;
 	struct pmf_bios_inputs_prev cb_prev; /* To preserve custom BIOS inputs */
 	bool cb_flag;			     /* To handle first custom BIOS input */
+	struct mutex metrics_mutex;
 };
 
 struct apmf_sps_prop_granular_v2 {
include/linux/amd-pmf-io.h +21 -0
index 6fa510f419c0..55198d2875cc 100644
@@ -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
author maintainer
#3 lore raw

Re: [PATCH V3 1/2] platform/x86/amd/pmf: Introduce new interface to export NPU metrics

Quoted 14 lines · Shyam Sundar S K
Quoted 6 lines · Lizhi Hou

From: Shyam Sundar S K <[email protected]>

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.

Quoted 6 lines
[lizhi: save return value of is_npu_metrics_supported() and return it]
Quoted 3 lines

Lizhi, just noticied that this patch may not apply cleanly on Ilpo-next (which tree does this change land? drm-misc or platform-x86)? If platform-x86, I can share you a rebased version

I used drm-misc.

Lizhi

Quoted 183 lines

offline so that you can wrap into XDNA series.

Thanks,
Shyam

Quoted 178 lines
---
  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(+)
drivers/platform/x86/amd/pmf/core.c +76 -0
index 8fc293c9c538..d180a39e03bf 100644
@@ -8,12 +8,15 @@
   * Author: Shyam Sundar S K <[email protected]>
   */
 
+#include <linux/array_size.h>
+#include <linux/cleanup.h>
  #include <linux/debugfs.h>
  #include <linux/iopoll.h>
  #include <linux/module.h>
  #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"
 
@@ -53,6 +56,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);
@@ -314,6 +319,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_GPL(amd_pmf_get_npu_data);
+
  static int amd_pmf_suspend_handler(struct device *dev)
  {
  	struct amd_pmf_dev *pdev = dev_get_drvdata(dev);
@@ -477,6 +547,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);
@@ -485,6 +559,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;
drivers/platform/x86/amd/pmf/pmf.h +2 -0
index 9144c8c3bbaf..1bf896c5b92c 100644
@@ -12,6 +12,7 @@
  #define PMF_H
 
  #include <linux/acpi.h>
+#include <linux/amd-pmf-io.h>
  #include <linux/input.h>
  #include <linux/platform_device.h>
  #include <linux/platform_profile.h>
@@ -413,6 +414,7 @@ struct amd_pmf_dev {
  	struct apmf_sbios_req_v1 req1;
  	struct pmf_bios_inputs_prev cb_prev; /* To preserve custom BIOS inputs */
  	bool cb_flag;			     /* To handle first custom BIOS input */
+	struct mutex metrics_mutex;
  };
 
  struct apmf_sps_prop_granular_v2 {
include/linux/amd-pmf-io.h +21 -0
index 6fa510f419c0..55198d2875cc 100644
@@ -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
maintainer
#4 lore raw

Re: [PATCH V3 1/2] platform/x86/amd/pmf: Introduce new interface to export NPU metrics

Quoted 16 lines · Lizhi Hou
Quoted 14 lines · Shyam Sundar S K
Quoted 12 lines · Lizhi Hou

From: Shyam Sundar S K <[email protected]>

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.

[lizhi: save return value of is_npu_metrics_supported() and return it]
Quoted 5 lines
Quoted 3 lines

Lizhi, just noticied that this patch may not apply cleanly on Ilpo-next (which tree does this change land? drm-misc or platform-x86)? If platform-x86, I can share you a rebased version

I used drm-misc.

Hi,

I'm not expecting you to rebase it as I'll need to do an IB to handle the conflict cleanly.

--
i.

Quoted 190 lines

Lizhi

Quoted 186 lines

offline so that you can wrap into XDNA series.

Thanks,
Shyam

Quoted 181 lines
---
  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(+)
drivers/platform/x86/amd/pmf/core.c +2 -0
index 8fc293c9c538..d180a39e03bf 100644
@@ -8,12 +8,15 @@
   * Author: Shyam Sundar S K <[email protected]>
   */
  +#include <linux/array_size.h>
+#include <linux/cleanup.h>
  #include <linux/debugfs.h>
  #include <linux/iopoll.h>
  #include <linux/module.h>
  #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"
  @@ -53,6 +56,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);

drivers/platform/x86/amd/pmf/core.c +30 -0
@@ -314,6 +319,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);

drivers/platform/x86/amd/pmf/core.c +38 -0
+		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_GPL(amd_pmf_get_npu_data);
+
  static int amd_pmf_suspend_handler(struct device *dev)
  {
  	struct amd_pmf_dev *pdev = dev_get_drvdata(dev);
@@ -477,6 +547,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);
@@ -485,6 +559,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;
drivers/platform/x86/amd/pmf/pmf.h +1 -0
index 9144c8c3bbaf..1bf896c5b92c 100644
@@ -12,6 +12,7 @@
  #define PMF_H
    #include <linux/acpi.h>
+#include <linux/amd-pmf-io.h>
  #include <linux/input.h>
  #include <linux/platform_device.h>
  #include <linux/platform_profile.h>
@@ -413,6 +414,7 @@ struct amd_pmf_dev {
  	struct apmf_sbios_req_v1 req1;
  	struct pmf_bios_inputs_prev cb_prev; /* To preserve custom BIOS inputs
 */
  	bool cb_flag;			     /* To handle first custom BIOS

input */

drivers/platform/x86/amd/pmf/pmf.h +1 -0
+	struct mutex metrics_mutex;
  };
    struct apmf_sps_prop_granular_v2 {
include/linux/amd-pmf-io.h +20 -0
index 6fa510f419c0..55198d2875cc 100644
@@ -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
Signature · 1 line
 i.
author maintainer
#5 lore raw

Re: [PATCH V3 1/2] platform/x86/amd/pmf: Introduce new interface to export NPU metrics

In reply to Lizhi Hou

Hi Ilpo,

What should be the next step? Should I generate both patches based on review-ilpo-next? Will the amdxdna change upstream through platform-x86 tree?

Thanks,

Lizhi

Quoted 204 lines · Lizhi Hou
Quoted 14 lines · Shyam Sundar S K
Quoted 6 lines · Lizhi Hou

From: Shyam Sundar S K <[email protected]>

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.

Quoted 6 lines
[lizhi: save return value of is_npu_metrics_supported() and return it]
Quoted 3 lines

Lizhi, just noticied that this patch may not apply cleanly on Ilpo-next (which tree does this change land? drm-misc or platform-x86)? If platform-x86, I can share you a rebased version

I used drm-misc.

Lizhi

Quoted 180 lines

offline so that you can wrap into XDNA series.

Thanks,
Shyam

Quoted 175 lines

---
  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(+)
drivers/platform/x86/amd/pmf/core.c +0 -0
index 8fc293c9c538..d180a39e03bf 100644
@@ -8,12 +8,15 @@
    * Author: Shyam Sundar S K <[email protected]>
    */

  +#include <linux/array_size.h>
+#include <linux/cleanup.h>
  #include <linux/debugfs.h>
  #include <linux/iopoll.h>
  #include <linux/module.h>
  #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"
  @@ -53,6 +56,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);

drivers/platform/x86/amd/pmf/core.c +30 -0
@@ -314,6 +319,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;
+    }

drivers/platform/x86/amd/pmf/core.c +32 -0
+
+    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_GPL(amd_pmf_get_npu_data);
+

  static int amd_pmf_suspend_handler(struct device *dev) {
      struct amd_pmf_dev *pdev = dev_get_drvdata(dev);

drivers/platform/x86/amd/pmf/core.c +3 -0
@@ -477,6 +547,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);

drivers/platform/x86/amd/pmf/core.c +1 -0
@@ -485,6 +559,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;
drivers/platform/x86/amd/pmf/pmf.h +1 -0
index 9144c8c3bbaf..1bf896c5b92c 100644
@@ -12,6 +12,7 @@
   #define PMF_H
     #include <linux/acpi.h>
+#include <linux/amd-pmf-io.h>
   #include <linux/input.h>
   #include <linux/platform_device.h>
   #include <linux/platform_profile.h>
@@ -413,6 +414,7 @@ struct amd_pmf_dev {
       struct apmf_sbios_req_v1 req1;

      struct pmf_bios_inputs_prev cb_prev; /* To preserve custom BIOS inputs */ bool cb_flag;                 /* To handle first custom BIOS input */ +    struct mutex metrics_mutex;
  };
    struct apmf_sps_prop_granular_v2 {

include/linux/amd-pmf-io.h +0 -0
index 6fa510f419c0..55198d2875cc 100644
@@ -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]
+ */

include/linux/amd-pmf-io.h +9 -0
+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);

include/linux/amd-pmf-io.h +3 -0
+
+/* AMD PMF and NPU interface */
+int amd_pmf_get_npu_data(struct amd_pmf_npu_metrics *info);

  #endif

maintainer
#6 lore raw

Re: [PATCH V3 1/2] platform/x86/amd/pmf: Introduce new interface to export NPU metrics

Quoted 2 lines · Lizhi Hou

What should be the next step? Should I generate both patches based on review-ilpo-next? Will the amdxdna change upstream through platform-x86 tree?

(We seemed to have crossed emails.)

That would work for me too, but I thought it was wanted that patch 2 to go through accel?

Signature · 1 line
 i.
Quoted 213 lines · Lizhi Hou
Quoted 211 lines · Shyam Sundar S K
Quoted 14 lines · Lizhi Hou
Quoted 6 lines

From: Shyam Sundar S K <[email protected]>

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.

Quoted 6 lines
[lizhi: save return value of is_npu_metrics_supported() and return it]
Quoted 3 lines

Lizhi, just noticied that this patch may not apply cleanly on Ilpo-next (which tree does this change land? drm-misc or platform-x86)? If platform-x86, I can share you a rebased version

I used drm-misc.

Lizhi

Quoted 187 lines

offline so that you can wrap into XDNA series.

Thanks,
Shyam

Quoted 182 lines

---
  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(+)
drivers/platform/x86/amd/pmf/core.c +0 -0
index 8fc293c9c538..d180a39e03bf 100644
@@ -8,12 +8,15 @@
    * Author: Shyam Sundar S K <[email protected]>
    */

  +#include <linux/array_size.h>
+#include <linux/cleanup.h>
  #include <linux/debugfs.h>
  #include <linux/iopoll.h>
  #include <linux/module.h>
  #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"
  @@ -53,6 +56,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);

drivers/platform/x86/amd/pmf/core.c +30 -0
@@ -314,6 +319,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;
+    }

drivers/platform/x86/amd/pmf/core.c +32 -0
+
+    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_GPL(amd_pmf_get_npu_data);
+

  static int amd_pmf_suspend_handler(struct device *dev) {
      struct amd_pmf_dev *pdev = dev_get_drvdata(dev);

drivers/platform/x86/amd/pmf/core.c +3 -0
@@ -477,6 +547,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);

drivers/platform/x86/amd/pmf/core.c +1 -0
@@ -485,6 +559,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;
drivers/platform/x86/amd/pmf/pmf.h +1 -0
index 9144c8c3bbaf..1bf896c5b92c 100644
@@ -12,6 +12,7 @@
   #define PMF_H
     #include <linux/acpi.h>
+#include <linux/amd-pmf-io.h>
   #include <linux/input.h>
   #include <linux/platform_device.h>
   #include <linux/platform_profile.h>
@@ -413,6 +414,7 @@ struct amd_pmf_dev {
       struct apmf_sbios_req_v1 req1;

      struct pmf_bios_inputs_prev cb_prev; /* To preserve custom BIOS inputs */
      bool cb_flag;                 /* To handle first custom BIOS input */
+    struct mutex metrics_mutex;
  };
    struct apmf_sps_prop_granular_v2 {

include/linux/amd-pmf-io.h +0 -0
index 6fa510f419c0..55198d2875cc 100644
@@ -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]
+ */

include/linux/amd-pmf-io.h +9 -0
+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);

include/linux/amd-pmf-io.h +3 -0
+
+/* AMD PMF and NPU interface */
+int amd_pmf_get_npu_data(struct amd_pmf_npu_metrics *info);

  #endif

author maintainer
#7 lore raw

Re: [PATCH V3 1/2] platform/x86/amd/pmf: Introduce new interface to export NPU metrics

Quoted 8 lines · Ilpo Järvinen
Quoted 2 lines · Lizhi Hou

What should be the next step? Should I generate both patches based on review-ilpo-next? Will the amdxdna change upstream through platform-x86 tree?

(We seemed to have crossed emails.)

That would work for me too, but I thought it was wanted that patch 2 to go through accel?

Thanks a lot. Glad to know patch 1 is good and I will not post another one. :)

The patch 2 depends on patch1. I agree that patch 2 to go through accel  which can avoid conflict with other recent amdxdna driver changes. So I can wait the patch 1 to be upstream and reach to drm-misc, then submit patch 2 there. Does it sound a right plan?

Thanks,

Lizhi

Quoted 1 line
maintainer
#8 lore raw

Re: [PATCH V3 1/2] platform/x86/amd/pmf: Introduce new interface to export NPU metrics

In reply to Lizhi Hou
Quoted 105 lines · Lizhi Hou

From: Shyam Sundar S K <[email protected]>

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.

[lizhi: save return value of is_npu_metrics_supported() and return it]
---
 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(+)
drivers/platform/x86/amd/pmf/core.c +59 -0
index 8fc293c9c538..d180a39e03bf 100644
@@ -8,12 +8,15 @@
  * Author: Shyam Sundar S K <[email protected]>
  */
 
+#include <linux/array_size.h>
+#include <linux/cleanup.h>
 #include <linux/debugfs.h>
 #include <linux/iopoll.h>
 #include <linux/module.h>
 #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"
 
@@ -53,6 +56,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);
@@ -314,6 +319,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;
+

drivers/platform/x86/amd/pmf/core.c int amd_pmf_init_metrics_table(struct amd_pmf_dev *dev) · line 385

Quoted 10 lines
drivers/platform/x86/amd/pmf/core.c +10 -0
+	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_GPL(amd_pmf_get_npu_data);

One more thing, the export should be namespaced.

And since I'll be applying only this patch and not do an IB, can you also rebase it on top for the for-next branch.

Signature · 1 line
 i.
author maintainer
#9 lore raw

Re: [PATCH V3 1/2] platform/x86/amd/pmf: Introduce new interface to export NPU metrics

Quoted 119 lines · Ilpo Järvinen
Quoted 105 lines · Lizhi Hou

From: Shyam Sundar S K <[email protected]>

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.

[lizhi: save return value of is_npu_metrics_supported() and return it]
---
  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(+)
drivers/platform/x86/amd/pmf/core.c +59 -0
index 8fc293c9c538..d180a39e03bf 100644
@@ -8,12 +8,15 @@
   * Author: Shyam Sundar S K <[email protected]>
   */
 
+#include <linux/array_size.h>
+#include <linux/cleanup.h>
  #include <linux/debugfs.h>
  #include <linux/iopoll.h>
  #include <linux/module.h>
  #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"
 
@@ -53,6 +56,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);
@@ -314,6 +319,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;
+

drivers/platform/x86/amd/pmf/core.c int amd_pmf_init_metrics_table(struct amd_pmf_dev *dev) · line 385

Quoted 10 lines
drivers/platform/x86/amd/pmf/core.c +10 -0
+	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_GPL(amd_pmf_get_npu_data);

One more thing, the export should be namespaced.

Quoted 2 lines

And since I'll be applying only this patch and not do an IB, can you also rebase it on top for the for-next branch.

Sure, I will send a V4 patch.

Lizhi

Quoted 1 line
author maintainer
#10 lore raw

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

In reply to Lizhi Hou

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.

---
 drivers/accel/amdxdna/aie2_pci.c        | 29 +++++++++++++++++++++++++
 drivers/accel/amdxdna/aie2_pci.h        | 20 +++++++++++++++++
 drivers/accel/amdxdna/amdxdna_pci_drv.c |  3 ++-
 3 files changed, 51 insertions(+), 1 deletion(-)
drivers/accel/amdxdna/aie2_pci.c +29 -0
index 81a8e4137bfd..55fbc8f0f455 100644
@@ -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 = {};
+	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;
@@ -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;
drivers/accel/amdxdna/aie2_pci.h +20 -0
index a929fa98a121..08122c249009 100644
@@ -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,
drivers/accel/amdxdna/amdxdna_pci_drv.c +2 -1
index 7d59764d7869..053f72a31a5f 100644
@@ -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
Signature · 1 line
2.34.1