Skip to contentSkip to conversation

drm/xe/mmio: Make xe_mmio_wait32() aware of interrupts

PATCH Applied Gustavo Sousa 2 participants 5 messages Nov 16, 2023 – Nov 22, 2023

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

v3 cover drm/xe/mmio: Make xe_mmio_wait32() aware of interrupts

The current implementation of xe_mmio_wait32() is susceptible to preemption/interrupts causing false timeouts. This series proposes a fix for that.

Changes in v2:
- Add a patch moving xe_mmio_wait32() to xe_mmio.c
- Use a barrier()-free implementation.

Changes in v3:
- Tweak documentation for @timeout_us.
- Add note in the commit message to better justify the move of xe_mmio_wait32() to xe_mmio.c.

Gustavo Sousa (2):
drm/xe/mmio: Move xe_mmio_wait32() to xe_mmio.c drm/xe/mmio: Make xe_mmio_wait32() aware of interrupts

 drivers/gpu/drm/xe/xe_mmio.c | 60 ++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_mmio.h | 39 ++---------------------
 2 files changed, 62 insertions(+), 37 deletions(-)
Signature · 1 line
2.42.0
#1 lore raw

PATCH v3 1/2 drm/xe/mmio: Move xe_mmio_wait32() to xe_mmio.c

This function is big enough, let's move it to a shared compilation unit.

While at it, document it.

Here is the output of running bloat-o-metter on the new and old module (execution provided by Lucas):

    $ ./scripts/bloat-o-meter build64/drivers/gpu/drm/xe/xe.ko{.old,}
    add/remove: 2/0 grow/shrink: 0/58 up/down: 554/-15645 (-15091)
    (...) # Lines in between omitted
    Total: Before=2181322, After=2166231, chg -0.69%

The overall reduction in the size is not that significant. Nevertheless, keeping the function as inline arguably does not bring too much benefit as well.

As noted by Lucas, we would probably benefit from an inline function that did the fast-path check: do an optimistic first check before entering the wait-logic, which itself would go to a compilation unit. We might come back to implement this in the future if we have data to justify it.

v2:
- Add note in documentation for @timeout_us regarding the exponential backoff strategy. (Lucas)
- Share output of bloat-o-meter in the commit message. (Lucas)

---
 drivers/gpu/drm/xe/xe_mmio.c | 54 ++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_mmio.h | 39 ++------------------------
 2 files changed, 56 insertions(+), 37 deletions(-)
drivers/gpu/drm/xe/xe_mmio.c +54 -0
index d8f9fabf715e..46140425ee2e 100644
@@ -488,3 +488,57 @@ u64 xe_mmio_read64_2x32(struct xe_gt *gt, struct xe_reg reg)
 	return (u64)udw << 32 | ldw;
 }
 
+/**
+ * xe_mmio_wait32() - Wait for a register to match the desired masked value
+ * @gt: MMIO target GT
+ * @reg: register to read value from
+ * @mask: mask to be applied to the value read from the register
+ * @val: desired value after applying the mask
+ * @timeout_us: time out after this period of time. Wait logic tries to be
+ * smart, applying an exponential backoff until @timeout_us is reached.
+ * @out_val: if not NULL, points where to store the last unmasked value
+ * @atomic: needs to be true if calling from an atomic context
+ *
+ * This function polls for the desired masked value and returns zero on success
+ * or -ETIMEDOUT if timed out.
+ *
+ * Note that @timeout_us represents the minimum amount of time to wait before
+ * giving up. The actual time taken by this function can be a little more than
+ * @timeout_us for different reasons, specially in non-atomic contexts. Thus,
+ * it is possible that this function succeeds even after @timeout_us has passed.
+ */
+int xe_mmio_wait32(struct xe_gt *gt, struct xe_reg reg, u32 mask, u32 val, u32 timeout_us,
+		   u32 *out_val, bool atomic)
+{
+	ktime_t cur = ktime_get_raw();
+	const ktime_t end = ktime_add_us(cur, timeout_us);
+	int ret = -ETIMEDOUT;
+	s64 wait = 10;
+	u32 read;
+
+	for (;;) {
+		read = xe_mmio_read32(gt, reg);
+		if ((read & mask) == val) {
+			ret = 0;
+			break;
+		}
+
+		cur = ktime_get_raw();
+		if (!ktime_before(cur, end))
+			break;
+
+		if (ktime_after(ktime_add_us(cur, wait), end))
+			wait = ktime_us_delta(end, cur);
+
+		if (atomic)
+			udelay(wait);
+		else
+			usleep_range(wait, wait << 1);
+		wait <<= 1;
+	}
+
+	if (out_val)
+		*out_val = read;
+
+	return ret;
+}
drivers/gpu/drm/xe/xe_mmio.h +2 -37
index ae09f777d711..51905aff600c 100644
@@ -87,43 +87,6 @@ static inline int xe_mmio_write32_and_verify(struct xe_gt *gt,
 	return (reg_val & mask) != eval ? -EINVAL : 0;
 }
 
-static inline int xe_mmio_wait32(struct xe_gt *gt, struct xe_reg reg, u32 mask,
-				 u32 val, u32 timeout_us, u32 *out_val,
-				 bool atomic)
-{
-	ktime_t cur = ktime_get_raw();
-	const ktime_t end = ktime_add_us(cur, timeout_us);
-	int ret = -ETIMEDOUT;
-	s64 wait = 10;
-	u32 read;
-
-	for (;;) {
-		read = xe_mmio_read32(gt, reg);
-		if ((read & mask) == val) {
-			ret = 0;
-			break;
-		}
-
-		cur = ktime_get_raw();
-		if (!ktime_before(cur, end))
-			break;
-
-		if (ktime_after(ktime_add_us(cur, wait), end))
-			wait = ktime_us_delta(end, cur);
-
-		if (atomic)
-			udelay(wait);
-		else
-			usleep_range(wait, wait << 1);
-		wait <<= 1;
-	}
-
-	if (out_val)
-		*out_val = read;
-
-	return ret;
-}
-
 int xe_mmio_ioctl(struct drm_device *dev, void *data,
 		  struct drm_file *file);
 
@@ -140,5 +103,7 @@ static inline bool xe_mmio_in_range(const struct xe_gt *gt,
 int xe_mmio_probe_vram(struct xe_device *xe);
 int xe_mmio_tile_vram_size(struct xe_tile *tile, u64 *vram_size, u64 *tile_size, u64 *tile_base);
 u64 xe_mmio_read64_2x32(struct xe_gt *gt, struct xe_reg reg);
+int xe_mmio_wait32(struct xe_gt *gt, struct xe_reg reg, u32 mask, u32 val, u32 timeout_us,
+		   u32 *out_val, bool atomic);
 
 #endif
Signature · 1 line
2.42.0
#2 lore raw

PATCH v3 2/2 drm/xe/mmio: Make xe_mmio_wait32() aware of interrupts

In reply to Gustavo Sousa

With the current implementation, a preemption or other kind of interrupt might happen between xe_mmio_read32() and ktime_get_raw(). Such an interruption (specially in the case of preemption) might be long enough to cause a timeout without giving a chance of a new check on the register value on a next iteration, which would have happened otherwise.

This issue causes some sporadic timeouts in some code paths. As an example, we were experiencing some rare timeouts when waiting for PLL unlock for C10/C20 PHYs (see intel_cx0pll_disable()). After debugging, we found out that the PLL unlock was happening within the expected time period (20us), which suggested a bug in xe_mmio_wait32().

To fix the issue, ensure that we do a last check out of the loop if necessary.

This change was tested with the aforementioned PLL unlocking code path. Experiments showed that, before this change, we observed reported timeouts in 54 of 5000 runs; and, after this change, no timeouts were reported in 5000 runs.

v2:
- Prefer an implementation without a barrier (v1 switched the order of xe_mmio_read32() and ktime_get_raw() calls and added a barrier() in between). (Lucas, Rodrigo)

---
 drivers/gpu/drm/xe/xe_mmio.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/gpu/drm/xe/xe_mmio.c +6 -0
index 46140425ee2e..e29c2fee709a 100644
@@ -537,6 +537,12 @@ int xe_mmio_wait32(struct xe_gt *gt, struct xe_reg reg, u32 mask, u32 val, u32 t
 		wait <<= 1;
 	}
 
+	if (ret != 0) {
+		read = xe_mmio_read32(gt, reg);
+		if ((read & mask) == val)
+			ret = 0;
+	}
+
 	if (out_val)
 		*out_val = read;
 
Signature · 1 line
2.42.0
#3 lore raw

[Intel-xe] ✗ CI.Patch_applied: failure for drm/xe/mmio: Make xe_mmio_wait32() aware of interrupts (rev5)

In reply to Gustavo Sousa
== Series Details ==

== Series Details ==

Series: drm/xe/mmio: Make xe_mmio_wait32() aware of interrupts (rev5)
URL : https://patchwork.freedesktop.org/series/126428/
State : failure

== Summary ==

=== Applying kernel patches on branch 'drm-xe-next' with base: === Base commit: b25aa17bf drm/xe/guc: Use valid scratch register for posting read === git am output follows ===
error: patch failed: drivers/gpu/drm/xe/xe_mmio.h:140
error: drivers/gpu/drm/xe/xe_mmio.h: patch does not apply
hint: Use 'git am --show-current-patch' to see the failed patch
Applying: drm/xe/mmio: Move xe_mmio_wait32() to xe_mmio.c
Patch failed at 0001 drm/xe/mmio: Move xe_mmio_wait32() to xe_mmio.c When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".

author Applied
#4 lore raw
In reply to Gustavo Sousa

Quoting Gustavo Sousa (2023-11-16 18:39:58-03:00)

Quoted 4 lines

The current implementation of xe_mmio_wait32() is susceptible to preemption/interrupts causing false timeouts. This series proposes a fix for that.

Quoted 8 lines

Changes in v2:
- Add a patch moving xe_mmio_wait32() to xe_mmio.c
- Use a barrier()-free implementation.

Changes in v3:
- Tweak documentation for @timeout_us.
- Add note in the commit message to better justify the move of xe_mmio_wait32() to xe_mmio.c.

CI results for this are avaiable at https://patchwork.freedesktop.org/series/126428/#rev6

There are no regressions seen in BAT results.

Pushed to drm-xe-next. Thanks Lucas and Rodrigo for the review!

--
Gustavo Sousa

Quoted 12 lines

Gustavo Sousa (2):
drm/xe/mmio: Move xe_mmio_wait32() to xe_mmio.c drm/xe/mmio: Make xe_mmio_wait32() aware of interrupts

drivers/gpu/drm/xe/xe_mmio.c | 60 ++++++++++++++++++++++++++++++++++++ drivers/gpu/drm/xe/xe_mmio.h | 39 ++---------------------

2 files changed, 62 insertions(+), 37 deletions(-)
Signature · 1 line
2.42.0