semaphore.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #ifndef _H8300_SEMAPHORE_H
  2. #define _H8300_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 <asm/system.h>
  10. #include <asm/atomic.h>
  11. /*
  12. * Interrupt-safe semaphores..
  13. *
  14. * (C) Copyright 1996 Linus Torvalds
  15. *
  16. * H8/300 version by Yoshinori Sato
  17. */
  18. struct semaphore {
  19. atomic_t count;
  20. int sleepers;
  21. wait_queue_head_t wait;
  22. };
  23. #define __SEMAPHORE_INITIALIZER(name, n) \
  24. { \
  25. .count = ATOMIC_INIT(n), \
  26. .sleepers = 0, \
  27. .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
  28. }
  29. #define __MUTEX_INITIALIZER(name) \
  30. __SEMAPHORE_INITIALIZER(name,1)
  31. #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
  32. struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
  33. #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
  34. #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
  35. static inline void sema_init (struct semaphore *sem, int val)
  36. {
  37. *sem = (struct semaphore)__SEMAPHORE_INITIALIZER(*sem, val);
  38. }
  39. static inline void init_MUTEX (struct semaphore *sem)
  40. {
  41. sema_init(sem, 1);
  42. }
  43. static inline void init_MUTEX_LOCKED (struct semaphore *sem)
  44. {
  45. sema_init(sem, 0);
  46. }
  47. asmlinkage void __down_failed(void /* special register calling convention */);
  48. asmlinkage int __down_failed_interruptible(void /* params in registers */);
  49. asmlinkage int __down_failed_trylock(void /* params in registers */);
  50. asmlinkage void __up_wakeup(void /* special register calling convention */);
  51. asmlinkage void __down(struct semaphore * sem);
  52. asmlinkage int __down_interruptible(struct semaphore * sem);
  53. asmlinkage int __down_trylock(struct semaphore * sem);
  54. asmlinkage void __up(struct semaphore * sem);
  55. extern spinlock_t semaphore_wake_lock;
  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 atomic_t *count asm("er0");
  64. might_sleep();
  65. count = &(sem->count);
  66. __asm__ __volatile__(
  67. "stc ccr,r3l\n\t"
  68. "orc #0x80,ccr\n\t"
  69. "mov.l %2, er1\n\t"
  70. "dec.l #1,er1\n\t"
  71. "mov.l er1,%0\n\t"
  72. "bpl 1f\n\t"
  73. "ldc r3l,ccr\n\t"
  74. "mov.l %1,er0\n\t"
  75. "jsr @___down\n\t"
  76. "bra 2f\n"
  77. "1:\n\t"
  78. "ldc r3l,ccr\n"
  79. "2:"
  80. : "=m"(*count)
  81. : "g"(sem),"m"(*count)
  82. : "cc", "er1", "er2", "er3");
  83. }
  84. static inline int down_interruptible(struct semaphore * sem)
  85. {
  86. register atomic_t *count asm("er0");
  87. might_sleep();
  88. count = &(sem->count);
  89. __asm__ __volatile__(
  90. "stc ccr,r1l\n\t"
  91. "orc #0x80,ccr\n\t"
  92. "mov.l %3, er2\n\t"
  93. "dec.l #1,er2\n\t"
  94. "mov.l er2,%1\n\t"
  95. "bpl 1f\n\t"
  96. "ldc r1l,ccr\n\t"
  97. "mov.l %2,er0\n\t"
  98. "jsr @___down_interruptible\n\t"
  99. "bra 2f\n"
  100. "1:\n\t"
  101. "ldc r1l,ccr\n\t"
  102. "sub.l %0,%0\n\t"
  103. "2:\n\t"
  104. : "=r" (count),"=m" (*count)
  105. : "g"(sem),"m"(*count)
  106. : "cc", "er1", "er2", "er3");
  107. return (int)count;
  108. }
  109. static inline int down_trylock(struct semaphore * sem)
  110. {
  111. register atomic_t *count asm("er0");
  112. count = &(sem->count);
  113. __asm__ __volatile__(
  114. "stc ccr,r3l\n\t"
  115. "orc #0x80,ccr\n\t"
  116. "mov.l %3,er2\n\t"
  117. "dec.l #1,er2\n\t"
  118. "mov.l er2,%0\n\t"
  119. "bpl 1f\n\t"
  120. "ldc r3l,ccr\n\t"
  121. "jmp @3f\n\t"
  122. LOCK_SECTION_START(".align 2\n\t")
  123. "3:\n\t"
  124. "mov.l %2,er0\n\t"
  125. "jsr @___down_trylock\n\t"
  126. "jmp @2f\n\t"
  127. LOCK_SECTION_END
  128. "1:\n\t"
  129. "ldc r3l,ccr\n\t"
  130. "sub.l %1,%1\n"
  131. "2:"
  132. : "=m" (*count),"=r"(count)
  133. : "g"(sem),"m"(*count)
  134. : "cc", "er1","er2", "er3");
  135. return (int)count;
  136. }
  137. /*
  138. * Note! This is subtle. We jump to wake people up only if
  139. * the semaphore was negative (== somebody was waiting on it).
  140. * The default case (no contention) will result in NO
  141. * jumps for both down() and up().
  142. */
  143. static inline void up(struct semaphore * sem)
  144. {
  145. register atomic_t *count asm("er0");
  146. count = &(sem->count);
  147. __asm__ __volatile__(
  148. "stc ccr,r3l\n\t"
  149. "orc #0x80,ccr\n\t"
  150. "mov.l %2,er1\n\t"
  151. "inc.l #1,er1\n\t"
  152. "mov.l er1,%0\n\t"
  153. "ldc r3l,ccr\n\t"
  154. "sub.l er2,er2\n\t"
  155. "cmp.l er2,er1\n\t"
  156. "bgt 1f\n\t"
  157. "mov.l %1,er0\n\t"
  158. "jsr @___up\n"
  159. "1:"
  160. : "=m"(*count)
  161. : "g"(sem),"m"(*count)
  162. : "cc", "er1", "er2", "er3");
  163. }
  164. #endif /* __ASSEMBLY__ */
  165. #endif