Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

platform/x86/amd/pmf: Introduce new interface to export NPU metrics

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>
Link: https://patch.msgid.link/20260115173448.403826-1-lizhi.hou@amd.com
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>

authored by

Shyam Sundar S K and committed by
Ilpo Järvinen
118222e2 7b85137c

+99
+76
drivers/platform/x86/amd/pmf/core.c
··· 8 8 * Author: Shyam Sundar S K <Shyam-sundar.S-k@amd.com> 9 9 */ 10 10 11 + #include <linux/array_size.h> 12 + #include <linux/cleanup.h> 11 13 #include <linux/debugfs.h> 12 14 #include <linux/iopoll.h> 13 15 #include <linux/module.h> ··· 17 15 #include <linux/pci.h> 18 16 #include <linux/platform_device.h> 19 17 #include <linux/power_supply.h> 18 + #include <linux/string.h> 20 19 #include <asm/amd/node.h> 21 20 #include "pmf.h" 22 21 ··· 56 53 static bool force_load; 57 54 module_param(force_load, bool, 0444); 58 55 MODULE_PARM_DESC(force_load, "Force load this driver on supported older platforms (experimental)"); 56 + 57 + static struct device *pmf_device; 59 58 60 59 static int amd_pmf_pwr_src_notify_call(struct notifier_block *nb, unsigned long event, void *data) 61 60 { ··· 320 315 return 0; 321 316 } 322 317 318 + static int is_npu_metrics_supported(struct amd_pmf_dev *pdev) 319 + { 320 + switch (pdev->cpu_id) { 321 + case PCI_DEVICE_ID_AMD_1AH_M20H_ROOT: 322 + case PCI_DEVICE_ID_AMD_1AH_M60H_ROOT: 323 + return 0; 324 + default: 325 + return -EOPNOTSUPP; 326 + } 327 + } 328 + 329 + static int amd_pmf_get_smu_metrics(struct amd_pmf_dev *dev, struct amd_pmf_npu_metrics *data) 330 + { 331 + int ret, i; 332 + 333 + guard(mutex)(&dev->metrics_mutex); 334 + 335 + ret = is_npu_metrics_supported(dev); 336 + if (ret) 337 + return ret; 338 + 339 + ret = amd_pmf_set_dram_addr(dev, true); 340 + if (ret) 341 + return ret; 342 + 343 + memset(dev->buf, 0, dev->mtable_size); 344 + 345 + /* Send SMU command to get NPU metrics */ 346 + ret = amd_pmf_send_cmd(dev, SET_TRANSFER_TABLE, SET_CMD, METRICS_TABLE_ID, NULL); 347 + if (ret) { 348 + dev_err(dev->dev, "SMU command failed to get NPU metrics: %d\n", ret); 349 + return ret; 350 + } 351 + 352 + memcpy(&dev->m_table_v2, dev->buf, dev->mtable_size); 353 + 354 + data->npuclk_freq = dev->m_table_v2.npuclk_freq; 355 + for (i = 0; i < ARRAY_SIZE(data->npu_busy); i++) 356 + data->npu_busy[i] = dev->m_table_v2.npu_busy[i]; 357 + data->npu_power = dev->m_table_v2.npu_power; 358 + data->mpnpuclk_freq = dev->m_table_v2.mpnpuclk_freq; 359 + data->npu_reads = dev->m_table_v2.npu_reads; 360 + data->npu_writes = dev->m_table_v2.npu_writes; 361 + 362 + return 0; 363 + } 364 + 365 + int amd_pmf_get_npu_data(struct amd_pmf_npu_metrics *info) 366 + { 367 + struct amd_pmf_dev *pdev; 368 + 369 + if (!info) 370 + return -EINVAL; 371 + 372 + if (!pmf_device) 373 + return -ENODEV; 374 + 375 + pdev = dev_get_drvdata(pmf_device); 376 + if (!pdev) 377 + return -ENODEV; 378 + 379 + return amd_pmf_get_smu_metrics(pdev, info); 380 + } 381 + EXPORT_SYMBOL_NS_GPL(amd_pmf_get_npu_data, "AMD_PMF"); 382 + 323 383 static int amd_pmf_reinit_ta(struct amd_pmf_dev *pdev) 324 384 { 325 385 bool status; ··· 612 542 if (err) 613 543 return err; 614 544 545 + err = devm_mutex_init(dev->dev, &dev->metrics_mutex); 546 + if (err) 547 + return err; 548 + 615 549 apmf_acpi_init(dev); 616 550 platform_set_drvdata(pdev, dev); 617 551 amd_pmf_dbgfs_register(dev); ··· 623 549 apmf_install_handler(dev); 624 550 if (is_apmf_func_supported(dev, APMF_FUNC_SBIOS_HEARTBEAT_V2)) 625 551 amd_pmf_notify_sbios_heartbeat_event_v2(dev, ON_LOAD); 552 + 553 + pmf_device = dev->dev; 626 554 627 555 dev_info(dev->dev, "registered PMF device successfully\n"); 628 556
+2
drivers/platform/x86/amd/pmf/pmf.h
··· 12 12 #define PMF_H 13 13 14 14 #include <linux/acpi.h> 15 + #include <linux/amd-pmf-io.h> 15 16 #include <linux/circ_buf.h> 16 17 #include <linux/input.h> 17 18 #include <linux/mutex_types.h> ··· 441 440 bool cb_flag; /* To handle first custom BIOS input */ 442 441 struct pmf_cbi_ring_buffer cbi_buf; 443 442 struct mutex cbi_mutex; /* Protects ring buffer access */ 443 + struct mutex metrics_mutex; 444 444 }; 445 445 446 446 struct apmf_sps_prop_granular_v2 {
+21
include/linux/amd-pmf-io.h
··· 61 61 LP_UNDEFINED, 62 62 }; 63 63 64 + /** 65 + * struct amd_pmf_npu_metrics: Get NPU metrics data from PMF driver 66 + * @npuclk_freq: NPU clock frequency [MHz] 67 + * @npu_busy: NPU busy % [0-100] 68 + * @npu_power: NPU power [mW] 69 + * @mpnpuclk_freq: MPNPU [MHz] 70 + * @npu_reads: NPU read bandwidth [MB/sec] 71 + * @npu_writes: NPU write bandwidth [MB/sec] 72 + */ 73 + struct amd_pmf_npu_metrics { 74 + u16 npuclk_freq; 75 + u16 npu_busy[8]; 76 + u16 npu_power; 77 + u16 mpnpuclk_freq; 78 + u16 npu_reads; 79 + u16 npu_writes; 80 + }; 81 + 64 82 int amd_get_sfh_info(struct amd_sfh_info *sfh_info, enum sfh_message_type op); 83 + 84 + /* AMD PMF and NPU interface */ 85 + int amd_pmf_get_npu_data(struct amd_pmf_npu_metrics *info); 65 86 #endif