semaphore.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #ifndef _M68K_SEMAPHORE_H
  2. #define _M68K_SEMAPHORE_H
  3. #define RW_LOCK_BIAS 0x01000000
  4. #ifndef __ASSEMBLY__
  5. #include <linux/linkage.h>
  6. #include <linux/wait.h>
  7. #include <linux/spinlock.h>
  8. #include <linux/rwsem.h>
  9. #include <linux/stringify.h>
  10. #include <asm/system.h>
  11. #include <asm/atomic.h>
  12. /*
  13. * Interrupt-safe semaphores..
  14. *
  15. * (C) Copyright 1996 Linus Torvalds
  16. *
  17. * m68k version by Andreas Schwab
  18. */
  19. struct semaphore {
  20. atomic_t count;
  21. atomic_t waking;
  22. wait_queue_head_t wait;
  23. };
  24. #define __SEMAPHORE_INITIALIZER(name, n) \
  25. { \
  26. .count = ATOMIC_INIT(n), \
  27. .waking = ATOMIC_INIT(0), \
  28. .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
  29. }
  30. #define __MUTEX_INITIALIZER(name) \
  31. __SEMAPHORE_INITIALIZER(name,1)
  32. #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
  33. struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
  34. #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
  35. #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
  36. static inline void sema_init(struct semaphore *sem, int val)
  37. {
  38. *sem = (struct semaphore)__SEMAPHORE_INITIALIZER(*sem, val);
  39. }
  40. static inline void init_MUTEX (struct semaphore *sem)
  41. {
  42. sema_init(sem, 1);
  43. }
  44. static inline void init_MUTEX_LOCKED (struct semaphore *sem)
  45. {
  46. sema_init(sem, 0);
  47. }
  48. asmlinkage void __down_failed(void /* special register calling convention */);
  49. asmlinkage int __down_failed_interruptible(void /* params in registers */);
  50. asmlinkage int __down_failed_trylock(void /* params in registers */);
  51. asmlinkage void __up_wakeup(void /* special register calling convention */);
  52. asmlinkage void __down(struct semaphore * sem);
  53. asmlinkage int __down_interruptible(struct semaphore * sem);
  54. asmlinkage int __down_trylock(struct semaphore * sem);
  55. asmlinkage void __up(struct semaphore * sem);
  56. /*
  57. * This is ugly, but we want the default case to fall through.
  58. * "down_failed" is a special asm handler that calls the C
  59. * routine that actually waits. See arch/m68k/lib/semaphore.S
  60. */
  61. static inline void down(struct semaphore *sem)
  62. {
  63. register struct semaphore *sem1 __asm__ ("%a1") = sem;
  64. might_sleep();
  65. __asm__ __volatile__(
  66. "| atomic down operation\n\t"
  67. "subql #1,%0@\n\t"
  68. "jmi 2f\n\t"
  69. "1:\n"
  70. LOCK_SECTION_START(".even\n\t")
  71. "2:\tpea 1b\n\t"
  72. "jbra __down_failed\n"
  73. LOCK_SECTION_END
  74. : /* no outputs */
  75. : "a" (sem1)
  76. : "memory");
  77. }
  78. static inline int down_interruptible(struct semaphore *sem)
  79. {
  80. register struct semaphore *sem1 __asm__ ("%a1") = sem;
  81. register int result __asm__ ("%d0");
  82. might_sleep();
  83. __asm__ __volatile__(
  84. "| atomic interruptible down operation\n\t"
  85. "subql #1,%1@\n\t"
  86. "jmi 2f\n\t"
  87. "clrl %0\n"
  88. "1:\n"
  89. LOCK_SECTION_START(".even\n\t")
  90. "2:\tpea 1b\n\t"
  91. "jbra __down_failed_interruptible\n"
  92. LOCK_SECTION_END
  93. : "=d" (result)
  94. : "a" (sem1)
  95. : "memory");
  96. return result;
  97. }
  98. static inline int down_trylock(struct semaphore *sem)
  99. {
  100. register struct semaphore *sem1 __asm__ ("%a1") = sem;
  101. register int result __asm__ ("%d0");
  102. __asm__ __volatile__(
  103. "| atomic down trylock operation\n\t"
  104. "subql #1,%1@\n\t"
  105. "jmi 2f\n\t"
  106. "clrl %0\n"
  107. "1:\n"
  108. LOCK_SECTION_START(".even\n\t")
  109. "2:\tpea 1b\n\t"
  110. "jbra __down_failed_trylock\n"
  111. LOCK_SECTION_END
  112. : "=d" (result)
  113. : "a" (sem1)
  114. : "memory");
  115. return result;
  116. }
  117. /*
  118. * Note! This is subtle. We jump to wake people up only if
  119. * the semaphore was negative (== somebody was waiting on it).
  120. * The default case (no contention) will result in NO
  121. * jumps for both down() and up().
  122. */
  123. static inline void up(struct semaphore *sem)
  124. {
  125. register struct semaphore *sem1 __asm__ ("%a1") = sem;
  126. __asm__ __volatile__(
  127. "| atomic up operation\n\t"
  128. "addql #1,%0@\n\t"
  129. "jle 2f\n"
  130. "1:\n"
  131. LOCK_SECTION_START(".even\n\t")
  132. "2:\t"
  133. "pea 1b\n\t"
  134. "jbra __up_wakeup\n"
  135. LOCK_SECTION_END
  136. : /* no outputs */
  137. : "a" (sem1)
  138. : "memory");
  139. }
  140. #endif /* __ASSEMBLY__ */
  141. #endif