xref: /linux/drivers/gpu/drm/i915/display/intel_display_jiffies.h (revision 815e260a18a3af4dab59025ee99a7156c0e8b5e0)
1 /* SPDX-License-Identifier: MIT */
2 /* Copyright © 2025 Intel Corporation */
3 
4 #ifndef __INTEL_DISPLAY_JIFFIES_H__
5 #define __INTEL_DISPLAY_JIFFIES_H__
6 
7 #include <linux/jiffies.h>
8 
9 static inline unsigned long msecs_to_jiffies_timeout(const unsigned int m)
10 {
11 	unsigned long j = msecs_to_jiffies(m);
12 
13 	return min_t(unsigned long, MAX_JIFFY_OFFSET, j + 1);
14 }
15 
16 /*
17  * If you need to wait X milliseconds between events A and B, but event B
18  * doesn't happen exactly after event A, you record the timestamp (jiffies) of
19  * when event A happened, then just before event B you call this function and
20  * pass the timestamp as the first argument, and X as the second argument.
21  */
22 static inline void
23 wait_remaining_ms_from_jiffies(unsigned long timestamp_jiffies, int to_wait_ms)
24 {
25 	unsigned long target_jiffies, tmp_jiffies, remaining_jiffies;
26 
27 	/*
28 	 * Don't re-read the value of "jiffies" every time since it may change
29 	 * behind our back and break the math.
30 	 */
31 	tmp_jiffies = jiffies;
32 	target_jiffies = timestamp_jiffies +
33 			 msecs_to_jiffies_timeout(to_wait_ms);
34 
35 	if (time_after(target_jiffies, tmp_jiffies)) {
36 		remaining_jiffies = target_jiffies - tmp_jiffies;
37 		while (remaining_jiffies)
38 			remaining_jiffies =
39 			    schedule_timeout_uninterruptible(remaining_jiffies);
40 	}
41 }
42 
43 #endif /* __INTEL_DISPLAY_JIFFIES_H__ */
44