backoff.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #ifndef _SPARC64_BACKOFF_H
  2. #define _SPARC64_BACKOFF_H
  3. /* The macros in this file implement an exponential backoff facility
  4. * for atomic operations.
  5. *
  6. * When multiple threads compete on an atomic operation, it is
  7. * possible for one thread to be continually denied a successful
  8. * completion of the compare-and-swap instruction. Heavily
  9. * threaded cpu implementations like Niagara can compound this
  10. * problem even further.
  11. *
  12. * When an atomic operation fails and needs to be retried, we spin a
  13. * certain number of times. At each subsequent failure of the same
  14. * operation we double the spin count, realizing an exponential
  15. * backoff.
  16. *
  17. * When we spin, we try to use an operation that will cause the
  18. * current cpu strand to block, and therefore make the core fully
  19. * available to any other other runnable strands. There are two
  20. * options, based upon cpu capabilities.
  21. *
  22. * On all cpus prior to SPARC-T4 we do three dummy reads of the
  23. * condition code register. Each read blocks the strand for something
  24. * between 40 and 50 cpu cycles.
  25. *
  26. * For SPARC-T4 and later we have a special "pause" instruction
  27. * available. This is implemented using writes to register %asr27.
  28. * The cpu will block the number of cycles written into the register,
  29. * unless a disrupting trap happens first. SPARC-T4 specifically
  30. * implements pause with a granularity of 8 cycles. Each strand has
  31. * an internal pause counter which decrements every 8 cycles. So the
  32. * chip shifts the %asr27 value down by 3 bits, and writes the result
  33. * into the pause counter. If a value smaller than 8 is written, the
  34. * chip blocks for 1 cycle.
  35. *
  36. * To achieve the same amount of backoff as the three %ccr reads give
  37. * on earlier chips, we shift the backoff value up by 7 bits. (Three
  38. * %ccr reads block for about 128 cycles, 1 << 7 == 128) We write the
  39. * whole amount we want to block into the pause register, rather than
  40. * loop writing 128 each time.
  41. */
  42. #define BACKOFF_LIMIT (4 * 1024)
  43. #ifdef CONFIG_SMP
  44. #define BACKOFF_SETUP(reg) \
  45. mov 1, reg
  46. #define BACKOFF_LABEL(spin_label, continue_label) \
  47. spin_label
  48. #define BACKOFF_SPIN(reg, tmp, label) \
  49. mov reg, tmp; \
  50. 88: rd %ccr, %g0; \
  51. rd %ccr, %g0; \
  52. rd %ccr, %g0; \
  53. .section .pause_3insn_patch,"ax";\
  54. .word 88b; \
  55. sllx tmp, 7, tmp; \
  56. wr tmp, 0, %asr27; \
  57. clr tmp; \
  58. .previous; \
  59. brnz,pt tmp, 88b; \
  60. sub tmp, 1, tmp; \
  61. set BACKOFF_LIMIT, tmp; \
  62. cmp reg, tmp; \
  63. bg,pn %xcc, label; \
  64. nop; \
  65. ba,pt %xcc, label; \
  66. sllx reg, 1, reg;
  67. #else
  68. #define BACKOFF_SETUP(reg)
  69. #define BACKOFF_LABEL(spin_label, continue_label) \
  70. continue_label
  71. #define BACKOFF_SPIN(reg, tmp, label)
  72. #endif
  73. #endif /* _SPARC64_BACKOFF_H */