xref: /linux/drivers/gpu/drm/xe/xe_sleep.h (revision 0fc8f6200d2313278fbf4539bbab74677c685531)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright © 2026 Intel Corporation
4  */
5 
6 #ifndef _XE_SLEEP_H_
7 #define _XE_SLEEP_H_
8 
9 #include <linux/delay.h>
10 #include <linux/math64.h>
11 
12 /**
13  * xe_sleep_relaxed_ms() - Sleep for an approximate time.
14  * @delay_ms: time in msec to sleep
15  *
16  * For smaller timeouts, sleep with 0.5ms accuracy.
17  */
18 static inline void xe_sleep_relaxed_ms(unsigned int delay_ms)
19 {
20 	unsigned long min_us, max_us;
21 
22 	if (!delay_ms)
23 		return;
24 
25 	if (delay_ms > 20) {
26 		msleep(delay_ms);
27 		return;
28 	}
29 
30 	min_us = mul_u32_u32(delay_ms, 1000);
31 	max_us = min_us + 500;
32 
33 	usleep_range(min_us, max_us);
34 }
35 
36 /**
37  * xe_sleep_exponential_ms() - Sleep for a exponentially increased time.
38  * @sleep_period_ms: current time in msec to sleep
39  * @max_sleep_ms: maximum time in msec to sleep
40  *
41  * Sleep for the @sleep_period_ms and exponentially increase this time for the
42  * next loop, unless reaching the @max_sleep_ms limit.
43  *
44  * Return: approximate time in msec the task was delayed.
45  */
46 static inline unsigned int xe_sleep_exponential_ms(unsigned int *sleep_period_ms,
47 						   unsigned int max_sleep_ms)
48 {
49 	unsigned int delay_ms = *sleep_period_ms;
50 	unsigned int next_delay_ms = 2 * delay_ms;
51 
52 	xe_sleep_relaxed_ms(delay_ms);
53 	*sleep_period_ms = min(next_delay_ms, max_sleep_ms);
54 	return delay_ms;
55 }
56 
57 #endif
58