xref: /linux-6.15/include/linux/processor.h (revision fd851a3c)
1*fd851a3cSNicholas Piggin /* Misc low level processor primitives */
2*fd851a3cSNicholas Piggin #ifndef _LINUX_PROCESSOR_H
3*fd851a3cSNicholas Piggin #define _LINUX_PROCESSOR_H
4*fd851a3cSNicholas Piggin 
5*fd851a3cSNicholas Piggin #include <asm/processor.h>
6*fd851a3cSNicholas Piggin 
7*fd851a3cSNicholas Piggin /*
8*fd851a3cSNicholas Piggin  * spin_begin is used before beginning a busy-wait loop, and must be paired
9*fd851a3cSNicholas Piggin  * with spin_end when the loop is exited. spin_cpu_relax must be called
10*fd851a3cSNicholas Piggin  * within the loop.
11*fd851a3cSNicholas Piggin  *
12*fd851a3cSNicholas Piggin  * The loop body should be as small and fast as possible, on the order of
13*fd851a3cSNicholas Piggin  * tens of instructions/cycles as a guide. It should and avoid calling
14*fd851a3cSNicholas Piggin  * cpu_relax, or any "spin" or sleep type of primitive including nested uses
15*fd851a3cSNicholas Piggin  * of these primitives. It should not lock or take any other resource.
16*fd851a3cSNicholas Piggin  * Violations of these guidelies will not cause a bug, but may cause sub
17*fd851a3cSNicholas Piggin  * optimal performance.
18*fd851a3cSNicholas Piggin  *
19*fd851a3cSNicholas Piggin  * These loops are optimized to be used where wait times are expected to be
20*fd851a3cSNicholas Piggin  * less than the cost of a context switch (and associated overhead).
21*fd851a3cSNicholas Piggin  *
22*fd851a3cSNicholas Piggin  * Detection of resource owner and decision to spin or sleep or guest-yield
23*fd851a3cSNicholas Piggin  * (e.g., spin lock holder vcpu preempted, or mutex owner not on CPU) can be
24*fd851a3cSNicholas Piggin  * tested within the loop body.
25*fd851a3cSNicholas Piggin  */
26*fd851a3cSNicholas Piggin #ifndef spin_begin
27*fd851a3cSNicholas Piggin #define spin_begin()
28*fd851a3cSNicholas Piggin #endif
29*fd851a3cSNicholas Piggin 
30*fd851a3cSNicholas Piggin #ifndef spin_cpu_relax
31*fd851a3cSNicholas Piggin #define spin_cpu_relax() cpu_relax()
32*fd851a3cSNicholas Piggin #endif
33*fd851a3cSNicholas Piggin 
34*fd851a3cSNicholas Piggin /*
35*fd851a3cSNicholas Piggin  * spin_cpu_yield may be called to yield (undirected) to the hypervisor if
36*fd851a3cSNicholas Piggin  * necessary. This should be used if the wait is expected to take longer
37*fd851a3cSNicholas Piggin  * than context switch overhead, but we can't sleep or do a directed yield.
38*fd851a3cSNicholas Piggin  */
39*fd851a3cSNicholas Piggin #ifndef spin_cpu_yield
40*fd851a3cSNicholas Piggin #define spin_cpu_yield() cpu_relax_yield()
41*fd851a3cSNicholas Piggin #endif
42*fd851a3cSNicholas Piggin 
43*fd851a3cSNicholas Piggin #ifndef spin_end
44*fd851a3cSNicholas Piggin #define spin_end()
45*fd851a3cSNicholas Piggin #endif
46*fd851a3cSNicholas Piggin 
47*fd851a3cSNicholas Piggin /*
48*fd851a3cSNicholas Piggin  * spin_until_cond can be used to wait for a condition to become true. It
49*fd851a3cSNicholas Piggin  * may be expected that the first iteration will true in the common case
50*fd851a3cSNicholas Piggin  * (no spinning), so that callers should not require a first "likely" test
51*fd851a3cSNicholas Piggin  * for the uncontended case before using this primitive.
52*fd851a3cSNicholas Piggin  *
53*fd851a3cSNicholas Piggin  * Usage and implementation guidelines are the same as for the spin_begin
54*fd851a3cSNicholas Piggin  * primitives, above.
55*fd851a3cSNicholas Piggin  */
56*fd851a3cSNicholas Piggin #ifndef spin_until_cond
57*fd851a3cSNicholas Piggin #define spin_until_cond(cond)					\
58*fd851a3cSNicholas Piggin do {								\
59*fd851a3cSNicholas Piggin 	if (unlikely(!(cond))) {				\
60*fd851a3cSNicholas Piggin 		spin_begin();					\
61*fd851a3cSNicholas Piggin 		do {						\
62*fd851a3cSNicholas Piggin 			spin_cpu_relax();			\
63*fd851a3cSNicholas Piggin 		} while (!(cond));				\
64*fd851a3cSNicholas Piggin 		spin_end();					\
65*fd851a3cSNicholas Piggin 	}							\
66*fd851a3cSNicholas Piggin } while (0)
67*fd851a3cSNicholas Piggin 
68*fd851a3cSNicholas Piggin #endif
69*fd851a3cSNicholas Piggin 
70*fd851a3cSNicholas Piggin #endif /* _LINUX_PROCESSOR_H */
71