xref: /linux/drivers/gpu/drm/i915/i915_timer_util.c (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
1 // SPDX-License-Identifier: MIT
2 /* Copyright © 2025 Intel Corporation */
3 
4 #include <linux/jiffies.h>
5 
6 #include "i915_timer_util.h"
7 
8 void cancel_timer(struct timer_list *t)
9 {
10 	if (!timer_active(t))
11 		return;
12 
13 	timer_delete(t);
14 	WRITE_ONCE(t->expires, 0);
15 }
16 
17 void set_timer_ms(struct timer_list *t, unsigned long timeout)
18 {
19 	if (!timeout) {
20 		cancel_timer(t);
21 		return;
22 	}
23 
24 	timeout = msecs_to_jiffies(timeout);
25 
26 	/*
27 	 * Paranoia to make sure the compiler computes the timeout before
28 	 * loading 'jiffies' as jiffies is volatile and may be updated in
29 	 * the background by a timer tick. All to reduce the complexity
30 	 * of the addition and reduce the risk of losing a jiffy.
31 	 */
32 	barrier();
33 
34 	/* Keep t->expires = 0 reserved to indicate a canceled timer. */
35 	mod_timer(t, jiffies + timeout ?: 1);
36 }
37