tm.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright 2015, Michael Ellerman, IBM Corp.
  4. */
  5. #ifndef _SELFTESTS_POWERPC_TM_TM_H
  6. #define _SELFTESTS_POWERPC_TM_TM_H
  7. #include <stdbool.h>
  8. #include <asm/tm.h>
  9. #include "utils.h"
  10. #include "reg.h"
  11. #define TM_RETRIES 100
  12. static inline bool have_htm(void)
  13. {
  14. #ifdef PPC_FEATURE2_HTM
  15. return have_hwcap2(PPC_FEATURE2_HTM);
  16. #else
  17. printf("PPC_FEATURE2_HTM not defined, can't check AT_HWCAP2\n");
  18. return false;
  19. #endif
  20. }
  21. static inline bool have_htm_nosc(void)
  22. {
  23. #ifdef PPC_FEATURE2_HTM_NOSC
  24. return have_hwcap2(PPC_FEATURE2_HTM_NOSC);
  25. #else
  26. printf("PPC_FEATURE2_HTM_NOSC not defined, can't check AT_HWCAP2\n");
  27. return false;
  28. #endif
  29. }
  30. /*
  31. * Transactional Memory was removed in ISA 3.1. A synthetic TM implementation
  32. * is provided on P10 for threads running in P8/P9 compatibility mode. The
  33. * synthetic implementation immediately fails after tbegin. This failure sets
  34. * Bit 7 (Failure Persistent) and Bit 15 (Implementation-specific).
  35. */
  36. static inline bool htm_is_synthetic(void)
  37. {
  38. int i;
  39. /*
  40. * Per the ISA, the Failure Persistent bit may be incorrect. Try a few
  41. * times in case we got an Implementation-specific failure on a non ISA
  42. * v3.1 system. On these systems the Implementation-specific failure
  43. * should not be persistent.
  44. */
  45. for (i = 0; i < TM_RETRIES; i++) {
  46. asm volatile(
  47. "tbegin.;"
  48. "beq 1f;"
  49. "tend.;"
  50. "1:"
  51. :
  52. :
  53. : "memory");
  54. if ((__builtin_get_texasr() & (TEXASR_FP | TEXASR_IC)) !=
  55. (TEXASR_FP | TEXASR_IC))
  56. break;
  57. }
  58. return i == TM_RETRIES;
  59. }
  60. static inline long failure_code(void)
  61. {
  62. return __builtin_get_texasru() >> 24;
  63. }
  64. static inline bool failure_is_persistent(void)
  65. {
  66. return (failure_code() & TM_CAUSE_PERSISTENT) == TM_CAUSE_PERSISTENT;
  67. }
  68. static inline bool failure_is_syscall(void)
  69. {
  70. return (failure_code() & TM_CAUSE_SYSCALL) == TM_CAUSE_SYSCALL;
  71. }
  72. static inline bool failure_is_unavailable(void)
  73. {
  74. return (failure_code() & TM_CAUSE_FAC_UNAV) == TM_CAUSE_FAC_UNAV;
  75. }
  76. static inline bool failure_is_reschedule(void)
  77. {
  78. if ((failure_code() & TM_CAUSE_RESCHED) == TM_CAUSE_RESCHED ||
  79. (failure_code() & TM_CAUSE_KVM_RESCHED) == TM_CAUSE_KVM_RESCHED ||
  80. (failure_code() & TM_CAUSE_KVM_FAC_UNAV) == TM_CAUSE_KVM_FAC_UNAV)
  81. return true;
  82. return false;
  83. }
  84. static inline bool failure_is_nesting(void)
  85. {
  86. return (__builtin_get_texasru() & 0x400000);
  87. }
  88. static inline int tcheck(void)
  89. {
  90. long cr;
  91. asm volatile ("tcheck 0" : "=r"(cr) : : "cr0");
  92. return (cr >> 28) & 4;
  93. }
  94. static inline bool tcheck_doomed(void)
  95. {
  96. return tcheck() & 8;
  97. }
  98. static inline bool tcheck_active(void)
  99. {
  100. return tcheck() & 4;
  101. }
  102. static inline bool tcheck_suspended(void)
  103. {
  104. return tcheck() & 2;
  105. }
  106. static inline bool tcheck_transactional(void)
  107. {
  108. return tcheck() & 6;
  109. }
  110. #endif /* _SELFTESTS_POWERPC_TM_TM_H */