1193156Snwhitehorn/* SPDX-License-Identifier: GPL-2.0 */
2193156Snwhitehorn/* Misc low level processor primitives */
3193156Snwhitehorn#ifndef _LINUX_PROCESSOR_H
4193156Snwhitehorn#define _LINUX_PROCESSOR_H
5193156Snwhitehorn
6193156Snwhitehorn#include <asm/processor.h>
7193156Snwhitehorn
8193156Snwhitehorn/*
9193156Snwhitehorn * spin_begin is used before beginning a busy-wait loop, and must be paired
10193156Snwhitehorn * with spin_end when the loop is exited. spin_cpu_relax must be called
11193156Snwhitehorn * within the loop.
12193156Snwhitehorn *
13193156Snwhitehorn * The loop body should be as small and fast as possible, on the order of
14193156Snwhitehorn * tens of instructions/cycles as a guide. It should and avoid calling
15193156Snwhitehorn * cpu_relax, or any "spin" or sleep type of primitive including nested uses
16193156Snwhitehorn * of these primitives. It should not lock or take any other resource.
17193156Snwhitehorn * Violations of these guidelies will not cause a bug, but may cause sub
18193156Snwhitehorn * optimal performance.
19193156Snwhitehorn *
20193156Snwhitehorn * These loops are optimized to be used where wait times are expected to be
21193156Snwhitehorn * less than the cost of a context switch (and associated overhead).
22193156Snwhitehorn *
23193156Snwhitehorn * Detection of resource owner and decision to spin or sleep or guest-yield
24193156Snwhitehorn * (e.g., spin lock holder vcpu preempted, or mutex owner not on CPU) can be
25193156Snwhitehorn * tested within the loop body.
26193156Snwhitehorn */
27193156Snwhitehorn#ifndef spin_begin
28193156Snwhitehorn#define spin_begin()
29193156Snwhitehorn#endif
30193156Snwhitehorn
31193156Snwhitehorn#ifndef spin_cpu_relax
32193156Snwhitehorn#define spin_cpu_relax() cpu_relax()
33193156Snwhitehorn#endif
34193156Snwhitehorn
35193156Snwhitehorn#ifndef spin_end
36193156Snwhitehorn#define spin_end()
37193156Snwhitehorn#endif
38193156Snwhitehorn
39193156Snwhitehorn/*
40193156Snwhitehorn * spin_until_cond can be used to wait for a condition to become true. It
41193156Snwhitehorn * may be expected that the first iteration will true in the common case
42193156Snwhitehorn * (no spinning), so that callers should not require a first "likely" test
43193156Snwhitehorn * for the uncontended case before using this primitive.
44193156Snwhitehorn *
45193156Snwhitehorn * Usage and implementation guidelines are the same as for the spin_begin
46193156Snwhitehorn * primitives, above.
47193156Snwhitehorn */
48193156Snwhitehorn#ifndef spin_until_cond
49193156Snwhitehorn#define spin_until_cond(cond)					\
50193156Snwhitehorndo {								\
51193156Snwhitehorn	if (unlikely(!(cond))) {				\
52193156Snwhitehorn		spin_begin();					\
53193156Snwhitehorn		do {						\
54193156Snwhitehorn			spin_cpu_relax();			\
55193156Snwhitehorn		} while (!(cond));				\
56193156Snwhitehorn		spin_end();					\
57193156Snwhitehorn	}							\
58193156Snwhitehorn} while (0)
59193156Snwhitehorn
60193156Snwhitehorn#endif
61193156Snwhitehorn
62193156Snwhitehorn#endif /* _LINUX_PROCESSOR_H */
63193156Snwhitehorn