processor.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. #ifndef spin_end
  32. #define spin_end()
  33. #endif
  34. /*
  35. * spin_until_cond can be used to wait for a condition to become true. It
  36. * may be expected that the first iteration will true in the common case
  37. * (no spinning), so that callers should not require a first "likely" test
  38. * for the uncontended case before using this primitive.
  39. *
  40. * Usage and implementation guidelines are the same as for the spin_begin
  41. * primitives, above.
  42. */
  43. #ifndef spin_until_cond
  44. #define spin_until_cond(cond) \
  45. do { \
  46. if (unlikely(!(cond))) { \
  47. spin_begin(); \
  48. do { \
  49. spin_cpu_relax(); \
  50. } while (!(cond)); \
  51. spin_end(); \
  52. } \
  53. } while (0)
  54. #endif
  55. #endif /* _LINUX_PROCESSOR_H */