semaphore.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 __DECLARE_SEMAPHORE_GENERIC(name,count) \
  31. struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
  32. #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
  33. static inline void sema_init(struct semaphore *sem, int val)
  34. {
  35. *sem = (struct semaphore)__SEMAPHORE_INITIALIZER(*sem, val);
  36. }
  37. static inline void init_MUTEX (struct semaphore *sem)
  38. {
  39. sema_init(sem, 1);
  40. }
  41. static inline void init_MUTEX_LOCKED (struct semaphore *sem)
  42. {
  43. sema_init(sem, 0);
  44. }
  45. asmlinkage void __down_failed(void /* special register calling convention */);
  46. asmlinkage int __down_failed_interruptible(void /* params in registers */);
  47. asmlinkage int __down_failed_trylock(void /* params in registers */);
  48. asmlinkage void __up_wakeup(void /* special register calling convention */);
  49. asmlinkage void __down(struct semaphore * sem);
  50. asmlinkage int __down_interruptible(struct semaphore * sem);
  51. asmlinkage int __down_trylock(struct semaphore * sem);
  52. asmlinkage void __up(struct semaphore * sem);
  53. /*
  54. * This is ugly, but we want the default case to fall through.
  55. * "down_failed" is a special asm handler that calls the C
  56. * routine that actually waits. See arch/m68k/lib/semaphore.S
  57. */
  58. static inline void down(struct semaphore *sem)
  59. {
  60. register struct semaphore *sem1 __asm__ ("%a1") = sem;
  61. might_sleep();
  62. __asm__ __volatile__(
  63. "| atomic down operation\n\t"
  64. "subql #1,%0@\n\t"
  65. "jmi 2f\n\t"
  66. "1:\n"
  67. LOCK_SECTION_START(".even\n\t")
  68. "2:\tpea 1b\n\t"
  69. "jbra __down_failed\n"
  70. LOCK_SECTION_END
  71. : /* no outputs */
  72. : "a" (sem1)
  73. : "memory");
  74. }
  75. static inline int down_interruptible(struct semaphore *sem)
  76. {
  77. register struct semaphore *sem1 __asm__ ("%a1") = sem;
  78. register int result __asm__ ("%d0");
  79. might_sleep();
  80. __asm__ __volatile__(
  81. "| atomic interruptible down operation\n\t"
  82. "subql #1,%1@\n\t"
  83. "jmi 2f\n\t"
  84. "clrl %0\n"
  85. "1:\n"
  86. LOCK_SECTION_START(".even\n\t")
  87. "2:\tpea 1b\n\t"
  88. "jbra __down_failed_interruptible\n"
  89. LOCK_SECTION_END
  90. : "=d" (result)
  91. : "a" (sem1)
  92. : "memory");
  93. return result;
  94. }
  95. static inline int down_trylock(struct semaphore *sem)
  96. {
  97. register struct semaphore *sem1 __asm__ ("%a1") = sem;
  98. register int result __asm__ ("%d0");
  99. __asm__ __volatile__(
  100. "| atomic down trylock operation\n\t"
  101. "subql #1,%1@\n\t"
  102. "jmi 2f\n\t"
  103. "clrl %0\n"
  104. "1:\n"
  105. LOCK_SECTION_START(".even\n\t")
  106. "2:\tpea 1b\n\t"
  107. "jbra __down_failed_trylock\n"
  108. LOCK_SECTION_END
  109. : "=d" (result)
  110. : "a" (sem1)
  111. : "memory");
  112. return result;
  113. }
  114. /*
  115. * Note! This is subtle. We jump to wake people up only if
  116. * the semaphore was negative (== somebody was waiting on it).
  117. * The default case (no contention) will result in NO
  118. * jumps for both down() and up().
  119. */
  120. static inline void up(struct semaphore *sem)
  121. {
  122. register struct semaphore *sem1 __asm__ ("%a1") = sem;
  123. __asm__ __volatile__(
  124. "| atomic up operation\n\t"
  125. "addql #1,%0@\n\t"
  126. "jle 2f\n"
  127. "1:\n"
  128. LOCK_SECTION_START(".even\n\t")
  129. "2:\t"
  130. "pea 1b\n\t"
  131. "jbra __up_wakeup\n"
  132. LOCK_SECTION_END
  133. : /* no outputs */
  134. : "a" (sem1)
  135. : "memory");
  136. }
  137. #endif /* __ASSEMBLY__ */
  138. #endif