processor.h 2.1 KB

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