Skip to contentSkip to conversation

accel/ivpu: Expose NPU frequency into userspace

PATCH Applied Maciej Falkowski 3 participants 10 messages Apr 1, 2025 – Apr 17, 2025

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover accel/ivpu: Expose NPU frequency into userspace

This patchset introduces the capability to measure the NPU frequency and makes it accessible to a userspace via sysfs. The initial patch in the series addresses the inconsistency in retrieving the clock frequency from the device across the Meteor Lake and Lunar Lake generations.

Subsequently, the latter patch exposes the driver's frequency through sysfs to facilitate frequency sampling.

Andrzej Kacprowski (2):
accel/ivpu: Fix the NPU's DPU frequency calculation accel/ivpu: Show NPU frequency in sysfs

 drivers/accel/ivpu/ivpu_drv.c     |   4 +-
 drivers/accel/ivpu/ivpu_fw.c      |   3 +-
 drivers/accel/ivpu/ivpu_hw.h      |  14 ++--
 drivers/accel/ivpu/ivpu_hw_btrs.c | 134 ++++++++++++++----------------
 drivers/accel/ivpu/ivpu_hw_btrs.h |   7 +-
 drivers/accel/ivpu/ivpu_sysfs.c   |  49 ++++++++++-
 include/uapi/drm/ivpu_accel.h     |   4 +-
 7 files changed, 127 insertions(+), 88 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 accel/ivpu: Fix the NPU's DPU frequency calculation

From: Andrzej Kacprowski <[email protected]>

Fix the frequency returned to the user space by the DRM_IVPU_PARAM_CORE_CLOCK_RATE GET_PARAM IOCTL. The kernel driver returned CPU frequency for MTL and bare PLL frequency for LNL - this was inconsistent and incorrect for both platforms. With this fix the driver returns maximum frequency of the NPU data processing unit (DPU) for all HW generations. This is what user space always expected.

Also do not set CPU frequency in boot params - the firmware does not use frequency passed from the driver, it was only used by the early pre-production firmware.
With that we can remove CPU frequency calculation code.

Show NPU frequency in FREQ_CHANGE interrupt when frequency tracking is enabled.

Fixes 8a27ad81f7d3 ("accel/ivpu: Split IP and buttress code")
Cc <[email protected]> # v6.11+
---
 drivers/accel/ivpu/ivpu_drv.c     |   4 +-
 drivers/accel/ivpu/ivpu_fw.c      |   3 +-
 drivers/accel/ivpu/ivpu_hw.h      |  11 +--
 drivers/accel/ivpu/ivpu_hw_btrs.c | 126 +++++++++++++-----------------
 drivers/accel/ivpu/ivpu_hw_btrs.h |   6 +-
 include/uapi/drm/ivpu_accel.h     |   4 +-
 6 files changed, 66 insertions(+), 88 deletions(-)
drivers/accel/ivpu/ivpu_drv.c +2 -2
index 5e3888ff1022..eff1d3ca075f 100644
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /*
- * Copyright (C) 2020-2024 Intel Corporation
+ * Copyright (C) 2020-2025 Intel Corporation
  */
 
 #include <linux/firmware.h>
@@ -164,7 +164,7 @@ static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_f
 		args->value = vdev->platform;
 		break;
 	case DRM_IVPU_PARAM_CORE_CLOCK_RATE:
-		args->value = ivpu_hw_ratio_to_freq(vdev, vdev->hw->pll.max_ratio);
+		args->value = ivpu_hw_dpu_max_freq_get(vdev);
 		break;
 	case DRM_IVPU_PARAM_NUM_CONTEXTS:
 		args->value = ivpu_get_context_count(vdev);
drivers/accel/ivpu/ivpu_fw.c +1 -2
index 3799231b39e7..5e1d709c6a46 100644
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /*
- * Copyright (C) 2020-2024 Intel Corporation
+ * Copyright (C) 2020-2025 Intel Corporation
  */
 
 #include <linux/firmware.h>
@@ -576,7 +576,6 @@ void ivpu_fw_boot_params_setup(struct ivpu_device *vdev, struct vpu_boot_params
 
 	boot_params->magic = VPU_BOOT_PARAMS_MAGIC;
 	boot_params->vpu_id = to_pci_dev(vdev->drm.dev)->bus->number;
-	boot_params->frequency = ivpu_hw_pll_freq_get(vdev);
 
 	/*
 	 * This param is a debug firmware feature.  It switches default clock
drivers/accel/ivpu/ivpu_hw.h +3 -8
index 16435f2756d0..6e67e736ef8e 100644
@@ -1,6 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 /*
- * Copyright (C) 2020-2024 Intel Corporation
+ * Copyright (C) 2020-2025 Intel Corporation
  */
 
 #ifndef __IVPU_HW_H__
@@ -82,9 +82,9 @@ static inline u64 ivpu_hw_range_size(const struct ivpu_addr_range *range)
 	return range->end - range->start;
 }
 
-static inline u32 ivpu_hw_ratio_to_freq(struct ivpu_device *vdev, u32 ratio)
+static inline u32 ivpu_hw_dpu_max_freq_get(struct ivpu_device *vdev)
 {
-	return ivpu_hw_btrs_ratio_to_freq(vdev, ratio);
+	return ivpu_hw_btrs_dpu_max_freq_get(vdev);
 }
 
 static inline void ivpu_hw_irq_clear(struct ivpu_device *vdev)
@@ -92,11 +92,6 @@ static inline void ivpu_hw_irq_clear(struct ivpu_device *vdev)
 	ivpu_hw_ip_irq_clear(vdev);
 }
 
-static inline u32 ivpu_hw_pll_freq_get(struct ivpu_device *vdev)
-{
-	return ivpu_hw_btrs_pll_freq_get(vdev);
-}
-
 static inline u32 ivpu_hw_profiling_freq_get(struct ivpu_device *vdev)
 {
 	return vdev->hw->pll.profiling_freq;
drivers/accel/ivpu/ivpu_hw_btrs.c +56 -70

185 lines not on this page · raw message

drivers/accel/ivpu/ivpu_hw_btrs.h +2 -4
index 1fd71b4d4ab0..a2f0877237e8 100644
@@ -1,6 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 /*
- * Copyright (C) 2020-2024 Intel Corporation
+ * Copyright (C) 2020-2025 Intel Corporation
  */
 
 #ifndef __IVPU_HW_BTRS_H__
@@ -13,7 +13,6 @@
 
 #define PLL_PROFILING_FREQ_DEFAULT   38400000
 #define PLL_PROFILING_FREQ_HIGH      400000000
-#define PLL_RATIO_TO_FREQ(x)         ((x) * PLL_REF_CLK_FREQ)
 
 #define DCT_DEFAULT_ACTIVE_PERCENT 15u
 #define DCT_PERIOD_US		   35300u
@@ -32,12 +31,11 @@ int ivpu_hw_btrs_ip_reset(struct ivpu_device *vdev);
 void ivpu_hw_btrs_profiling_freq_reg_set_lnl(struct ivpu_device *vdev);
 void ivpu_hw_btrs_ats_print_lnl(struct ivpu_device *vdev);
 void ivpu_hw_btrs_clock_relinquish_disable_lnl(struct ivpu_device *vdev);
+u32 ivpu_hw_btrs_dpu_max_freq_get(struct ivpu_device *vdev);
 bool ivpu_hw_btrs_irq_handler_mtl(struct ivpu_device *vdev, int irq);
 bool ivpu_hw_btrs_irq_handler_lnl(struct ivpu_device *vdev, int irq);
 int ivpu_hw_btrs_dct_get_request(struct ivpu_device *vdev, bool *enable);
 void ivpu_hw_btrs_dct_set_status(struct ivpu_device *vdev, bool enable, u32 dct_percent);
-u32 ivpu_hw_btrs_pll_freq_get(struct ivpu_device *vdev);
-u32 ivpu_hw_btrs_ratio_to_freq(struct ivpu_device *vdev, u32 ratio);
 u32 ivpu_hw_btrs_telemetry_offset_get(struct ivpu_device *vdev);
 u32 ivpu_hw_btrs_telemetry_size_get(struct ivpu_device *vdev);
 u32 ivpu_hw_btrs_telemetry_enable_get(struct ivpu_device *vdev);
include/uapi/drm/ivpu_accel.h +2 -2
index 746c43bd3eb6..2f24103f4533 100644
@@ -1,6 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0-only WITH Linux-syscall-note */
 /*
- * Copyright (C) 2020-2024 Intel Corporation
+ * Copyright (C) 2020-2025 Intel Corporation
  */
 
 #ifndef __UAPI_IVPU_DRM_H__
@@ -147,7 +147,7 @@ struct drm_ivpu_param {
 	 * platform type when executing on a simulator or emulator (read-only)
 	 *
 	 * %DRM_IVPU_PARAM_CORE_CLOCK_RATE:
-	 * Current PLL frequency (read-only)
+	 * Maximum frequency of the NPU data processing unit clock (read-only)
 	 *
 	 * %DRM_IVPU_PARAM_NUM_CONTEXTS:
 	 * Maximum number of simultaneously existing contexts (read-only)
Signature · 1 line
2.43.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] accel/ivpu: Fix the NPU's DPU frequency calculation

Quoted 15 lines · Maciej Falkowski

From: Andrzej Kacprowski <[email protected]>

Fix the frequency returned to the user space by the DRM_IVPU_PARAM_CORE_CLOCK_RATE GET_PARAM IOCTL. The kernel driver returned CPU frequency for MTL and bare PLL frequency for LNL - this was inconsistent and incorrect for both platforms. With this fix the driver returns maximum frequency of the NPU data processing unit (DPU) for all HW generations. This is what user space always expected.

Also do not set CPU frequency in boot params - the firmware does not use frequency passed from the driver, it was only used by the early pre-production firmware.
With that we can remove CPU frequency calculation code.

Quoted 7 lines

Show NPU frequency in FREQ_CHANGE interrupt when frequency tracking is enabled.

Fixes 8a27ad81f7d3 ("accel/ivpu: Split IP and buttress code")
Cc <[email protected]> # v6.11+
#3 lore raw

PATCH 2/2 accel/ivpu: Show NPU frequency in sysfs

In reply to Maciej Falkowski

From: Andrzej Kacprowski <[email protected]>

Add sysfs files that show maximum and current
frequency of the NPU's data processing unit.
New sysfs entries:
- npu_max_frequency_mhz
- npu_current_frequency_mhz

---
 drivers/accel/ivpu/ivpu_hw.h      |  5 ++++
 drivers/accel/ivpu/ivpu_hw_btrs.c |  8 +++++
 drivers/accel/ivpu/ivpu_hw_btrs.h |  1 +
 drivers/accel/ivpu/ivpu_sysfs.c   | 49 ++++++++++++++++++++++++++++++-
 4 files changed, 62 insertions(+), 1 deletion(-)
drivers/accel/ivpu/ivpu_hw.h +5 -0
index 6e67e736ef8e..d79668fe1609 100644
@@ -87,6 +87,11 @@ static inline u32 ivpu_hw_dpu_max_freq_get(struct ivpu_device *vdev)
 	return ivpu_hw_btrs_dpu_max_freq_get(vdev);
 }
 
+static inline u32 ivpu_hw_dpu_freq_get(struct ivpu_device *vdev)
+{
+	return ivpu_hw_btrs_dpu_freq_get(vdev);
+}
+
 static inline void ivpu_hw_irq_clear(struct ivpu_device *vdev)
 {
 	ivpu_hw_ip_irq_clear(vdev);
drivers/accel/ivpu/ivpu_hw_btrs.c +8 -0
index 91eb8a93c15b..b236c7234daa 100644
@@ -606,6 +606,14 @@ u32 ivpu_hw_btrs_dpu_max_freq_get(struct ivpu_device *vdev)
 	return pll_ratio_to_dpu_freq(vdev, vdev->hw->pll.max_ratio);
 }
 
+u32 ivpu_hw_btrs_dpu_freq_get(struct ivpu_device *vdev)
+{
+	if (ivpu_hw_btrs_gen(vdev) == IVPU_HW_BTRS_MTL)
+		return pll_ratio_to_dpu_freq_mtl(pll_config_get_mtl(vdev));
+	else
+		return pll_ratio_to_dpu_freq_lnl(pll_config_get_lnl(vdev));
+}
+
 /* Handler for IRQs from Buttress core (irqB) */
 bool ivpu_hw_btrs_irq_handler_mtl(struct ivpu_device *vdev, int irq)
 {
drivers/accel/ivpu/ivpu_hw_btrs.h +1 -0
index a2f0877237e8..300f749971d4 100644
@@ -32,6 +32,7 @@ void ivpu_hw_btrs_profiling_freq_reg_set_lnl(struct ivpu_device *vdev);
 void ivpu_hw_btrs_ats_print_lnl(struct ivpu_device *vdev);
 void ivpu_hw_btrs_clock_relinquish_disable_lnl(struct ivpu_device *vdev);
 u32 ivpu_hw_btrs_dpu_max_freq_get(struct ivpu_device *vdev);
+u32 ivpu_hw_btrs_dpu_freq_get(struct ivpu_device *vdev);
 bool ivpu_hw_btrs_irq_handler_mtl(struct ivpu_device *vdev, int irq);
 bool ivpu_hw_btrs_irq_handler_lnl(struct ivpu_device *vdev, int irq);
 int ivpu_hw_btrs_dct_get_request(struct ivpu_device *vdev, bool *enable);
drivers/accel/ivpu/ivpu_sysfs.c +48 -1

68 lines not on this page · raw message

Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH 2/2] accel/ivpu: Show NPU frequency in sysfs

Quoted 6 lines · Maciej Falkowski

From: Andrzej Kacprowski <[email protected]>

Add sysfs files that show maximum and current
frequency of the NPU's data processing unit.
New sysfs entries:
- npu_max_frequency_mhz

Don't you have an ioctl to get this, which is fixed in patch 1 of the series? Why duplicate that with a sysfs?

Quoted 1 line

- npu_current_frequency_mhz

Do you have userspace code that consumes these?

Quoted 8 lines
---
  drivers/accel/ivpu/ivpu_hw.h      |  5 ++++
  drivers/accel/ivpu/ivpu_hw_btrs.c |  8 +++++
  drivers/accel/ivpu/ivpu_hw_btrs.h |  1 +
  drivers/accel/ivpu/ivpu_sysfs.c   | 49 ++++++++++++++++++++++++++++++-

Missing uapi documentation.

Also, should the sysfs maintainers be included in the review?

#5 lore raw

Re: [PATCH 2/2] accel/ivpu: Show NPU frequency in sysfs

Quoted 9 lines · Jeff Hugo
Quoted 6 lines · Maciej Falkowski

From: Andrzej Kacprowski <[email protected]>

Add sysfs files that show maximum and current
frequency of the NPU's data processing unit.
New sysfs entries:
- npu_max_frequency_mhz

Don't you have an ioctl to get this, which is fixed in patch 1 of the series?  Why duplicate that with a sysfs?

We added this to keep it consistent with sysfs entry for current frequency. This is more for a convenience but ioctl is still required as user-mode driver requires this. Do you think it is justified having this consideration?

Quoted 4 lines
Quoted 2 lines

- npu_current_frequency_mhz

Do you have userspace code that consumes these?

We do have a support for two device monitors in a userspace - resources[1] and  btop[2]. I develop myself a btop support and have already frequency included, I will also include it in resources.

[1]: https://github.com/nokyan/resources/blob/main/src/utils/npu/intel.rs
<https://github.com/nokyan/resources/blob/main/src/utils/npu/intel.rs>[2]: https://github.com/m-falkowski/btop

Quoted 10 lines
Quoted 9 lines

---
  drivers/accel/ivpu/ivpu_hw.h      |  5 ++++ drivers/accel/ivpu/ivpu_hw_btrs.c |  8 +++++ drivers/accel/ivpu/ivpu_hw_btrs.h |  1 + drivers/accel/ivpu/ivpu_sysfs.c   | 49 ++++++++++++++++++++++++++++++-

Quoted 4 lines

Missing uapi documentation.

Also, should the sysfs maintainers be included in the review?

Thank you for point this out. I included recipients using get_maintainers script.

Best regards,
Maciej

#6 lore raw

Re: [PATCH 2/2] accel/ivpu: Show NPU frequency in sysfs

Quoted 10 lines · Maciej
Quoted 8 lines · Jeff Hugo
Quoted 6 lines · Maciej Falkowski

From: Andrzej Kacprowski <[email protected]>

Add sysfs files that show maximum and current
frequency of the NPU's data processing unit.
New sysfs entries:
- npu_max_frequency_mhz

Quoted 5 lines
Quoted 1 line

Don't you have an ioctl to get this, which is fixed in patch 1 of the series?  Why duplicate that with a sysfs?

We added this to keep it consistent with sysfs entry for current frequency. This is more for a convenience but ioctl is still required as user-mode driver requires this. Do you think it is justified having this consideration?

The duplication is not great, but I understand the rational.

Also, I think your last message was HTML formatted instead of plain text. I suggest you check your mail client settings.

Quoted 11 lines
Quoted 5 lines
Quoted 3 lines

- npu_current_frequency_mhz

Do you have userspace code that consumes these?

We do have a support for two device monitors in a userspace - resources[1] and  btop[2]. I develop myself a btop support and have already frequency included, I will also include it in resources.

[1]: https://github.com/nokyan/resources/blob/main/src/utils/npu/intel.rs
<https://github.com/nokyan/resources/blob/main/src/utils/npu/ intel.rs>[2]: https://github.com/m-falkowski/btop

It would be good to document this. Without something in Documentation, at least have this in the commit text.

-Jeff

#7 lore raw

Re: [PATCH 2/2] accel/ivpu: Show NPU frequency in sysfs

Quoted 13 lines · Jeff Hugo
Quoted 12 lines · Maciej
Quoted 9 lines · Jeff Hugo
Quoted 6 lines · Maciej Falkowski

From: Andrzej Kacprowski <[email protected]>

Add sysfs files that show maximum and current
frequency of the NPU's data processing unit.
New sysfs entries:
- npu_max_frequency_mhz

Don't you have an ioctl to get this, which is fixed in patch 1 of the series?  Why duplicate that with a sysfs?

Quoted 7 lines
Quoted 3 lines

We added this to keep it consistent with sysfs entry for current frequency. This is more for a convenience but ioctl is still required as user-mode driver requires this. Do you think it is justified having this consideration?

The duplication is not great, but I understand the rational.

Also, I think your last message was HTML formatted instead of plain text.  I suggest you check your mail client settings.

Yes, it was set to automatic so I had not seen it before this occurence, thanks for pointing this out.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 4 lines

- npu_current_frequency_mhz

Do you have userspace code that consumes these?

Quoted 7 lines
Quoted 5 lines

We do have a support for two device monitors in a userspace - resources[1] and  btop[2]. I develop myself a btop support and have already frequency included, I will also include it in resources.

[1]: https://github.com/nokyan/resources/blob/main/src/utils/npu/intel.rs
<https://github.com/nokyan/resources/blob/main/src/utils/npu/ intel.rs>[2]: https://github.com/m-falkowski/btop

It would be good to document this.  Without something in Documentation, at least have this in the commit text.

I think it will be the best if we prepare appropriate documentation just like yours. I will post kerneldoc fixes and doc in a separate series if that is not a problem. Thank you for checking this.

Best regards,
Maciej

Quoted 2 lines

-Jeff

Reviewed-by on v1 2/2
#8 lore raw

Re: [PATCH 2/2] accel/ivpu: Show NPU frequency in sysfs

In reply to Maciej Falkowski
Quoted 138 lines · Maciej Falkowski

From: Andrzej Kacprowski <[email protected]>

Add sysfs files that show maximum and current
frequency of the NPU's data processing unit.
New sysfs entries:
- npu_max_frequency_mhz
- npu_current_frequency_mhz

---
 drivers/accel/ivpu/ivpu_hw.h      |  5 ++++
 drivers/accel/ivpu/ivpu_hw_btrs.c |  8 +++++
 drivers/accel/ivpu/ivpu_hw_btrs.h |  1 +
 drivers/accel/ivpu/ivpu_sysfs.c   | 49 ++++++++++++++++++++++++++++++-
 4 files changed, 62 insertions(+), 1 deletion(-)
drivers/accel/ivpu/ivpu_hw.h +5 -0
index 6e67e736ef8e..d79668fe1609 100644
@@ -87,6 +87,11 @@ static inline u32 ivpu_hw_dpu_max_freq_get(struct ivpu_device *vdev)
 	return ivpu_hw_btrs_dpu_max_freq_get(vdev);
 }
 
+static inline u32 ivpu_hw_dpu_freq_get(struct ivpu_device *vdev)
+{
+	return ivpu_hw_btrs_dpu_freq_get(vdev);
+}
+
 static inline void ivpu_hw_irq_clear(struct ivpu_device *vdev)
 {
 	ivpu_hw_ip_irq_clear(vdev);
drivers/accel/ivpu/ivpu_hw_btrs.c +8 -0
index 91eb8a93c15b..b236c7234daa 100644
@@ -606,6 +606,14 @@ u32 ivpu_hw_btrs_dpu_max_freq_get(struct ivpu_device *vdev)
 	return pll_ratio_to_dpu_freq(vdev, vdev->hw->pll.max_ratio);
 }
 
+u32 ivpu_hw_btrs_dpu_freq_get(struct ivpu_device *vdev)
+{
+	if (ivpu_hw_btrs_gen(vdev) == IVPU_HW_BTRS_MTL)
+		return pll_ratio_to_dpu_freq_mtl(pll_config_get_mtl(vdev));
+	else
+		return pll_ratio_to_dpu_freq_lnl(pll_config_get_lnl(vdev));
+}
+
 /* Handler for IRQs from Buttress core (irqB) */
 bool ivpu_hw_btrs_irq_handler_mtl(struct ivpu_device *vdev, int irq)
 {
drivers/accel/ivpu/ivpu_hw_btrs.h +1 -0
index a2f0877237e8..300f749971d4 100644
@@ -32,6 +32,7 @@ void ivpu_hw_btrs_profiling_freq_reg_set_lnl(struct ivpu_device *vdev);
 void ivpu_hw_btrs_ats_print_lnl(struct ivpu_device *vdev);
 void ivpu_hw_btrs_clock_relinquish_disable_lnl(struct ivpu_device *vdev);
 u32 ivpu_hw_btrs_dpu_max_freq_get(struct ivpu_device *vdev);
+u32 ivpu_hw_btrs_dpu_freq_get(struct ivpu_device *vdev);
 bool ivpu_hw_btrs_irq_handler_mtl(struct ivpu_device *vdev, int irq);
 bool ivpu_hw_btrs_irq_handler_lnl(struct ivpu_device *vdev, int irq);
 int ivpu_hw_btrs_dct_get_request(struct ivpu_device *vdev, bool *enable);
drivers/accel/ivpu/ivpu_sysfs.c +48 -1
index 97102feaf8dd..268ab7744a8b 100644
@@ -1,10 +1,12 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /*
- * Copyright (C) 2024 Intel Corporation
+ * Copyright (C) 2024-2025 Intel Corporation
  */
 
 #include <linux/device.h>
 #include <linux/err.h>
+#include <linux/pm_runtime.h>
+#include <linux/units.h>
 
 #include "ivpu_drv.h"
 #include "ivpu_gem.h"
@@ -90,10 +92,55 @@ sched_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
 
 static DEVICE_ATTR_RO(sched_mode);
 
+/**
+ * DOC: npu_max_frequency
+ *
+ * The npu_max_frequency shows maximum frequency in MHz of the NPU's data
+ * processing unit
+ */
+static ssize_t
+npu_max_frequency_mhz_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+	struct drm_device *drm = dev_get_drvdata(dev);
+	struct ivpu_device *vdev = to_ivpu_device(drm);
+	u32 freq = ivpu_hw_dpu_max_freq_get(vdev);
+
+	return sysfs_emit(buf, "%lu\n", freq / HZ_PER_MHZ);
+}
+
+static DEVICE_ATTR_RO(npu_max_frequency_mhz);
+
+/**
+ * DOC: npu_current_frequency_mhz
+ *
+ * The npu_current_frequency_mhz shows current frequency in MHz of the NPU's
+ * data processing unit
+ */
+static ssize_t
+npu_current_frequency_mhz_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+	struct drm_device *drm = dev_get_drvdata(dev);
+	struct ivpu_device *vdev = to_ivpu_device(drm);
+	u32 freq = 0;
+
+	/* Read frequency only if device is active, otherwise frequency is 0 */
+	if (pm_runtime_get_if_active(vdev->drm.dev) > 0) {
+		freq = ivpu_hw_dpu_freq_get(vdev);
+
+		pm_runtime_put_autosuspend(vdev->drm.dev);
+	}
+
+	return sysfs_emit(buf, "%lu\n", freq / HZ_PER_MHZ);
+}
+
+static DEVICE_ATTR_RO(npu_current_frequency_mhz);
+
 static struct attribute *ivpu_dev_attrs[] = {
 	&dev_attr_npu_busy_time_us.attr,
 	&dev_attr_npu_memory_utilization.attr,
 	&dev_attr_sched_mode.attr,
+	&dev_attr_npu_max_frequency_mhz.attr,
+	&dev_attr_npu_current_frequency_mhz.attr,
 	NULL,
 };
 
#9 lore raw
In reply to Maciej Falkowski

Applied to drm-misc-fixes

Quoted 21 lines · Maciej Falkowski

This patchset introduces the capability to measure the NPU frequency and makes it accessible to a userspace via sysfs. The initial patch in the series addresses the inconsistency in retrieving the clock frequency from the device across the Meteor Lake and Lunar Lake generations.

Subsequently, the latter patch exposes the driver's frequency through sysfs to facilitate frequency sampling.

Andrzej Kacprowski (2):
accel/ivpu: Fix the NPU's DPU frequency calculation accel/ivpu: Show NPU frequency in sysfs

 drivers/accel/ivpu/ivpu_drv.c     |   4 +-
 drivers/accel/ivpu/ivpu_fw.c      |   3 +-
 drivers/accel/ivpu/ivpu_hw.h      |  14 ++--
 drivers/accel/ivpu/ivpu_hw_btrs.c | 134 ++++++++++++++----------------
 drivers/accel/ivpu/ivpu_hw_btrs.h |   7 +-
 drivers/accel/ivpu/ivpu_sysfs.c   |  49 ++++++++++-
 include/uapi/drm/ivpu_accel.h     |   4 +-
 7 files changed, 127 insertions(+), 88 deletions(-)