semaphore.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 <asm/system.h>
  10. #include <asm/atomic.h>
  11. /*
  12. * Interrupt-safe semaphores..
  13. *
  14. * (C) Copyright 1996 Linus Torvalds
  15. *
  16. * m68k version by Andreas Schwab
  17. */
  18. struct semaphore {
  19. atomic_t count;
  20. atomic_t waking;
  21. wait_queue_head_t wait;
  22. };
  23. #define __SEMAPHORE_INITIALIZER(name, n) \
  24. { \
  25. .count = ATOMIC_INIT(n), \
  26. .waking = ATOMIC_INIT(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. extern 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. extern inline void down(struct semaphore * sem)
  62. {
  63. might_sleep();
  64. __asm__ __volatile__(
  65. "| atomic down operation\n\t"
  66. "movel %0, %%a1\n\t"
  67. "lea %%pc@(1f), %%a0\n\t"
  68. "subql #1, %%a1@\n\t"
  69. "jmi __down_failed\n"
  70. "1:"
  71. : /* no outputs */
  72. : "g" (sem)
  73. : "cc", "%a0", "%a1", "memory");
  74. }
  75. extern inline int down_interruptible(struct semaphore * sem)
  76. {
  77. int ret;
  78. might_sleep();
  79. __asm__ __volatile__(
  80. "| atomic down operation\n\t"
  81. "movel %1, %%a1\n\t"
  82. "lea %%pc@(1f), %%a0\n\t"
  83. "subql #1, %%a1@\n\t"
  84. "jmi __down_failed_interruptible\n\t"
  85. "clrl %%d0\n"
  86. "1: movel %%d0, %0\n"
  87. : "=d" (ret)
  88. : "g" (sem)
  89. : "cc", "%d0", "%a0", "%a1", "memory");
  90. return(ret);
  91. }
  92. extern inline int down_trylock(struct semaphore * sem)
  93. {
  94. register struct semaphore *sem1 __asm__ ("%a1") = sem;
  95. register int result __asm__ ("%d0");
  96. __asm__ __volatile__(
  97. "| atomic down trylock operation\n\t"
  98. "subql #1,%1@\n\t"
  99. "jmi 2f\n\t"
  100. "clrl %0\n"
  101. "1:\n"
  102. ".section .text.lock,\"ax\"\n"
  103. ".even\n"
  104. "2:\tpea 1b\n\t"
  105. "jbra __down_failed_trylock\n"
  106. ".previous"
  107. : "=d" (result)
  108. : "a" (sem1)
  109. : "memory");
  110. return result;
  111. }
  112. /*
  113. * Note! This is subtle. We jump to wake people up only if
  114. * the semaphore was negative (== somebody was waiting on it).
  115. * The default case (no contention) will result in NO
  116. * jumps for both down() and up().
  117. */
  118. extern inline void up(struct semaphore * sem)
  119. {
  120. __asm__ __volatile__(
  121. "| atomic up operation\n\t"
  122. "movel %0, %%a1\n\t"
  123. "lea %%pc@(1f), %%a0\n\t"
  124. "addql #1, %%a1@\n\t"
  125. "jle __up_wakeup\n"
  126. "1:"
  127. : /* no outputs */
  128. : "g" (sem)
  129. : "cc", "%a0", "%a1", "memory");
  130. }
  131. #endif /* __ASSEMBLY__ */
  132. #endif