Check for overflow in udelay/usleep_range use in __xe_mmio_wait32
and pick the correct helper according to the wait time and atomic.
Implement a similar helper to replace DIV_ROUND_UP for 32-bit CPUs.
Avoid growing delays becoming intollerably large by capping the
in-loop wait time.

v3: - Use div64_s64 kernel API for 32-bit vs 64-bit division.
v2: - Fixed checkpatch failure.
    - Added helper for 64-bit DIV_ROUND_UP on 32-bit CPU (Shasiko
      review). Fixed bug max range in usleep_range(Shasiko review)

Fixes: 5c09bd6ccd41 ("drm/xe/mmio: Move xe_mmio_wait32() to xe_mmio.c")
Signed-off-by: Alan Previn <[email protected]>
Assisted-by: Github-Copilot:Claude-Sonnet-5-0
---
 drivers/gpu/drm/xe/xe_mmio.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_mmio.c b/drivers/gpu/drm/xe/xe_mmio.c
index 7fa18dfcb5a2..507095f33cec 100644
--- a/drivers/gpu/drm/xe/xe_mmio.c
+++ b/drivers/gpu/drm/xe/xe_mmio.c
@@ -7,6 +7,7 @@
 
 #include <linux/delay.h>
 #include <linux/io-64-nonatomic-lo-hi.h>
+#include <linux/math64.h>
 #include <linux/minmax.h>
 #include <linux/pci.h>
 
@@ -349,11 +350,23 @@ static int __xe_mmio_wait32(struct xe_mmio *mmio, struct 
xe_reg reg, u32 mask, u
                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;
+#define __XE_MMIO_WAIT_MAX_INLOOP_100MS (100 * USEC_PER_MSEC)
+               if (atomic) {
+                       if (wait <= MAX_UDELAY_MS * USEC_PER_MSEC)
+                               udelay(wait);
+                       else
+                               mdelay(div64_s64(wait, USEC_PER_MSEC));
+               } else {
+                       usleep_range(wait, wait + (wait >> 2)); /* range till 
wait + 25% */
+               }
+               /*
+                * As we keep doubling the wait time for every check that 
fails, cap the
+                * in-loop delay-or-sleep to less than 2x 100 milliseconds to 
prevent from
+                * expanding 'wait' into exponentially longer wait times per 
loop that
+                * end up delaying the next completion check way later than 
tolerable.
+                */
+               wait = wait < __XE_MMIO_WAIT_MAX_INLOOP_100MS >> 1 ?
+                      wait << 1 : __XE_MMIO_WAIT_MAX_INLOOP_100MS;
        }
 
        if (ret != 0) {
-- 
2.43.0

Reply via email to