preempt.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __ASM_PREEMPT_H
  3. #define __ASM_PREEMPT_H
  4. #include <asm/current.h>
  5. #include <linux/thread_info.h>
  6. #include <asm/atomic_ops.h>
  7. #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
  8. #define PREEMPT_ENABLED (0 + PREEMPT_NEED_RESCHED)
  9. static inline int preempt_count(void)
  10. {
  11. return READ_ONCE(S390_lowcore.preempt_count) & ~PREEMPT_NEED_RESCHED;
  12. }
  13. static inline void preempt_count_set(int pc)
  14. {
  15. int old, new;
  16. do {
  17. old = READ_ONCE(S390_lowcore.preempt_count);
  18. new = (old & PREEMPT_NEED_RESCHED) |
  19. (pc & ~PREEMPT_NEED_RESCHED);
  20. } while (__atomic_cmpxchg(&S390_lowcore.preempt_count,
  21. old, new) != old);
  22. }
  23. #define init_task_preempt_count(p) do { } while (0)
  24. #define init_idle_preempt_count(p, cpu) do { \
  25. S390_lowcore.preempt_count = PREEMPT_ENABLED; \
  26. } while (0)
  27. static inline void set_preempt_need_resched(void)
  28. {
  29. __atomic_and(~PREEMPT_NEED_RESCHED, &S390_lowcore.preempt_count);
  30. }
  31. static inline void clear_preempt_need_resched(void)
  32. {
  33. __atomic_or(PREEMPT_NEED_RESCHED, &S390_lowcore.preempt_count);
  34. }
  35. static inline bool test_preempt_need_resched(void)
  36. {
  37. return !(READ_ONCE(S390_lowcore.preempt_count) & PREEMPT_NEED_RESCHED);
  38. }
  39. static inline void __preempt_count_add(int val)
  40. {
  41. if (__builtin_constant_p(val) && (val >= -128) && (val <= 127))
  42. __atomic_add_const(val, &S390_lowcore.preempt_count);
  43. else
  44. __atomic_add(val, &S390_lowcore.preempt_count);
  45. }
  46. static inline void __preempt_count_sub(int val)
  47. {
  48. __preempt_count_add(-val);
  49. }
  50. static inline bool __preempt_count_dec_and_test(void)
  51. {
  52. return __atomic_add(-1, &S390_lowcore.preempt_count) == 1;
  53. }
  54. static inline bool should_resched(int preempt_offset)
  55. {
  56. return unlikely(READ_ONCE(S390_lowcore.preempt_count) ==
  57. preempt_offset);
  58. }
  59. #else /* CONFIG_HAVE_MARCH_Z196_FEATURES */
  60. #define PREEMPT_ENABLED (0)
  61. static inline int preempt_count(void)
  62. {
  63. return READ_ONCE(S390_lowcore.preempt_count);
  64. }
  65. static inline void preempt_count_set(int pc)
  66. {
  67. S390_lowcore.preempt_count = pc;
  68. }
  69. #define init_task_preempt_count(p) do { } while (0)
  70. #define init_idle_preempt_count(p, cpu) do { \
  71. S390_lowcore.preempt_count = PREEMPT_ENABLED; \
  72. } while (0)
  73. static inline void set_preempt_need_resched(void)
  74. {
  75. }
  76. static inline void clear_preempt_need_resched(void)
  77. {
  78. }
  79. static inline bool test_preempt_need_resched(void)
  80. {
  81. return false;
  82. }
  83. static inline void __preempt_count_add(int val)
  84. {
  85. S390_lowcore.preempt_count += val;
  86. }
  87. static inline void __preempt_count_sub(int val)
  88. {
  89. S390_lowcore.preempt_count -= val;
  90. }
  91. static inline bool __preempt_count_dec_and_test(void)
  92. {
  93. return !--S390_lowcore.preempt_count && tif_need_resched();
  94. }
  95. static inline bool should_resched(int preempt_offset)
  96. {
  97. return unlikely(preempt_count() == preempt_offset &&
  98. tif_need_resched());
  99. }
  100. #endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */
  101. #ifdef CONFIG_PREEMPT
  102. extern asmlinkage void preempt_schedule(void);
  103. #define __preempt_schedule() preempt_schedule()
  104. extern asmlinkage void preempt_schedule_notrace(void);
  105. #define __preempt_schedule_notrace() preempt_schedule_notrace()
  106. #endif /* CONFIG_PREEMPT */
  107. #endif /* __ASM_PREEMPT_H */