Skip to contentSkip to conversation

drm/i915: Fix conversion between clock ticks and nanoseconds

PATCH New Umesh Nerlige Ramappa 2 participants 2 messages Oct 9, 2025

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

PATCH 1/1 drm/i915: Fix conversion between clock ticks and nanoseconds

When tick values are large, the multiplication by NSEC_PER_SEC is larger than 64 bits and results in bad conversions.

The issue is seen in PMU busyness counters that look like they have wrapped around due to bad conversion. i915 PMU implementation returns monotonically increasing counters. If a count is lesser than previous one, it will only return the larger value until the smaller value catches up. The user will see this as zero delta between two measurements even though the engines are busy.

Fix it by using a scaling factor to do the conversion. Add the same fix for reverse conversion as well.

---
v2:
- Fix divide by zero for Gen11 (Andi)
- Update commit message

v3:
- Warn if gt->clock_frequency is 0 (Andi)

---
 .../gpu/drm/i915/gt/intel_gt_clock_utils.c    | 21 ++++++++++++++-----
 drivers/gpu/drm/i915/gt/intel_gt_types.h      |  2 ++
 2 files changed, 18 insertions(+), 5 deletions(-)
drivers/gpu/drm/i915/gt/intel_gt_clock_utils.c +16 -5
index 88b147fa5cb1..52e47cdcf0ce 100644
@@ -3,6 +3,8 @@
  * Copyright © 2020 Intel Corporation
  */
 
+#include <linux/gcd.h>
+
 #include "i915_drv.h"
 #include "i915_reg.h"
 #include "intel_gt.h"
@@ -171,7 +173,14 @@ static u32 read_clock_frequency(struct intel_uncore *uncore)
 
 void intel_gt_init_clock_frequency(struct intel_gt *gt)
 {
+	unsigned long clock_period_scale;
+
 	gt->clock_frequency = read_clock_frequency(gt->uncore);
+	GEM_WARN_ON(!gt->clock_frequency);
+
+	clock_period_scale = gcd(NSEC_PER_SEC, gt->clock_frequency);
+	gt->clock_nsec_scaled = NSEC_PER_SEC / clock_period_scale;
+	gt->clock_freq_scaled = gt->clock_frequency / clock_period_scale;
 
 	/* Icelake appears to use another fixed frequency for CTX_TIMESTAMP */
 	if (GRAPHICS_VER(gt->i915) == 11)
@@ -180,11 +189,11 @@ void intel_gt_init_clock_frequency(struct intel_gt *gt)
 		gt->clock_period_ns = intel_gt_clock_interval_to_ns(gt, 1);
 
 	GT_TRACE(gt,
-		 "Using clock frequency: %dkHz, period: %dns, wrap: %lldms\n",
+		 "Using clock frequency: %dkHz, period: %dns, wrap: %lldms, scale %lu\n",
 		 gt->clock_frequency / 1000,
 		 gt->clock_period_ns,
-		 div_u64(mul_u32_u32(gt->clock_period_ns, S32_MAX),
-			 USEC_PER_SEC));
+		 div_u64(mul_u32_u32(gt->clock_period_ns, S32_MAX), USEC_PER_SEC),
+		 clock_period_scale);
 }
 
 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)
@@ -205,7 +214,8 @@ static u64 div_u64_roundup(u64 nom, u32 den)
 
 u64 intel_gt_clock_interval_to_ns(const struct intel_gt *gt, u64 count)
 {
-	return div_u64_roundup(count * NSEC_PER_SEC, gt->clock_frequency);
+	return div_u64_roundup(count * gt->clock_nsec_scaled,
+			       gt->clock_freq_scaled);
 }
 
 u64 intel_gt_pm_interval_to_ns(const struct intel_gt *gt, u64 count)
@@ -215,7 +225,8 @@ u64 intel_gt_pm_interval_to_ns(const struct intel_gt *gt, u64 count)
 
 u64 intel_gt_ns_to_clock_interval(const struct intel_gt *gt, u64 ns)
 {
-	return div_u64_roundup(gt->clock_frequency * ns, NSEC_PER_SEC);
+	return div_u64_roundup(gt->clock_freq_scaled * ns,
+			       gt->clock_nsec_scaled);
 }
 
 u64 intel_gt_ns_to_pm_interval(const struct intel_gt *gt, u64 ns)
drivers/gpu/drm/i915/gt/intel_gt_types.h +2 -0
index bcee084b1f27..a19c568fcdc0 100644
@@ -166,6 +166,8 @@ struct intel_gt {
 
 	u32 clock_frequency;
 	u32 clock_period_ns;
+	u32 clock_freq_scaled;
+	u32 clock_nsec_scaled;
 
 	struct intel_llc llc;
 	struct intel_rc6 rc6;
Signature · 1 line
2.43.0
#1 lore raw

✗ i915.CI.BAT: failure for drm/i915: Fix conversion between clock ticks and nanoseconds (rev2)

== Series Details ==

== Series Details ==

Series: drm/i915: Fix conversion between clock ticks and nanoseconds (rev2)
URL : https://patchwork.freedesktop.org/series/155566/
State : failure

== Summary ==

CI Bug Log - changes from CI_DRM_17334 -> Patchwork_155566v2
====================================================

Summary
-------

**FAILURE**

Serious unknown changes coming with Patchwork_155566v2 absolutely need to be verified manually.

If you think the reported changes have nothing to do with the changes
introduced in Patchwork_155566v2, please notify your bug team ([email protected]) to allow them to document this new failure mode, which will reduce false positives in CI.

External URL: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_155566v2/index.html

Participating hosts (44 -> 42)
------------------------------

Missing (2): bat-dg2-14 fi-snb-2520m

Possible new issues
-------------------

Here are the unknown changes that may have been introduced in Patchwork_155566v2:

### IGT changes ###

#### Possible regressions ####

* igt@i915_module_load@load:
- fi-pnv-d510: [PASS][1] -> [ABORT][2]
[1]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_17334/fi-pnv-d510/igt@[email protected]
[2]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_155566v2/fi-pnv-d510/igt@[email protected]

Known issues
------------

Here are the changes found in Patchwork_155566v2 that come from known issues:

### IGT changes ###

#### Issues hit ####

* igt@dmabuf@all-tests:
- bat-apl-1: [PASS][3] -> [ABORT][4] ([i915#12904]) +1 other test abort
[3]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_17334/bat-apl-1/igt@[email protected]
[4]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_155566v2/bat-apl-1/igt@[email protected]

* igt@dmabuf@all-tests@dma_fence_chain:
- fi-bsw-nick: [PASS][5] -> [ABORT][6] ([i915#12904]) +1 other test abort
[5]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_17334/fi-bsw-nick/igt@dmabuf@all-tests@dma_fence_chain.html
[6]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_155566v2/fi-bsw-nick/igt@dmabuf@all-tests@dma_fence_chain.html

* igt@i915_selftest@live:
- bat-arlh-3: [PASS][7] -> [INCOMPLETE][8] ([i915#14764] / [i915#14818] / [i915#14837] / [i915#14838])
[7]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_17334/bat-arlh-3/igt@[email protected]
[8]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_155566v2/bat-arlh-3/igt@[email protected]

* igt@i915_selftest@live@hangcheck:
- bat-arlh-3: [PASS][9] -> [INCOMPLETE][10] ([i915#14838])
[9]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_17334/bat-arlh-3/igt@i915_selftest@[email protected]
[10]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_155566v2/bat-arlh-3/igt@i915_selftest@[email protected]

* igt@i915_selftest@live@requests:
- bat-atsm-1: [PASS][11] -> [INCOMPLETE][12] ([i915#14564] / [i915#15017])
[11]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_17334/bat-atsm-1/igt@i915_selftest@[email protected]
[12]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_155566v2/bat-atsm-1/igt@i915_selftest@[email protected]

* igt@i915_selftest@live@workarounds:
- bat-arlh-3: [PASS][13] -> [DMESG-FAIL][14] ([i915#12061])
[13]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_17334/bat-arlh-3/igt@i915_selftest@[email protected]
[14]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_155566v2/bat-arlh-3/igt@i915_selftest@[email protected]

#### Possible fixes ####

* igt@i915_selftest@live:
- bat-dg2-8: [DMESG-FAIL][15] ([i915#12061]) -> [PASS][16] +1 other test pass
[15]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_17334/bat-dg2-8/igt@[email protected]
[16]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_155566v2/bat-dg2-8/igt@[email protected]

* igt@i915_selftest@live@workarounds:
- bat-arls-6: [DMESG-FAIL][17] ([i915#12061]) -> [PASS][18] +1 other test pass
[17]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_17334/bat-arls-6/igt@i915_selftest@[email protected]
[18]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_155566v2/bat-arls-6/igt@i915_selftest@[email protected]

* igt@kms_hdmi_inject@inject-audio:
- fi-tgl-1115g4: [FAIL][19] ([i915#14867]) -> [PASS][20]
[19]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_17334/fi-tgl-1115g4/igt@[email protected]
[20]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_155566v2/fi-tgl-1115g4/igt@[email protected]

#### Warnings ####

* igt@i915_selftest@live:
- bat-atsm-1: [DMESG-FAIL][21] ([i915#12061] / [i915#13929]) -> [INCOMPLETE][22] ([i915#12061] / [i915#14564])
[21]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_17334/bat-atsm-1/igt@[email protected]
[22]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_155566v2/bat-atsm-1/igt@[email protected]

[i915#12061]: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/12061
[i915#12904]: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/12904
[i915#13929]: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/13929
[i915#14564]: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/14564
[i915#14764]: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/14764
[i915#14818]: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/14818
[i915#14837]: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/14837
[i915#14838]: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/14838
[i915#14867]: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/14867
[i915#15017]: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/15017

Build changes
-------------

* Linux: CI_DRM_17334 -> Patchwork_155566v2

CI-20190529: 20190529
CI_DRM_17334: 23e7752f903fec07634c72b778a733cf4ed17656 @ git://anongit.freedesktop.org/gfx-ci/linux
IGT_8581: 8581
Patchwork_155566v2: 23e7752f903fec07634c72b778a733cf4ed17656 @ git://anongit.freedesktop.org/gfx-ci/linux

== Logs ==

For more details see: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_155566v2/index.html