semaphore.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 __DECLARE_SEMAPHORE_GENERIC(name,count) \
  30. struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
  31. #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
  32. #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
  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. extern spinlock_t semaphore_wake_lock;
  54. /*
  55. * This is ugly, but we want the default case to fall through.
  56. * "down_failed" is a special asm handler that calls the C
  57. * routine that actually waits. See arch/m68k/lib/semaphore.S
  58. */
  59. static inline void down(struct semaphore * sem)
  60. {
  61. might_sleep();
  62. __asm__ __volatile__(
  63. "| atomic down operation\n\t"
  64. "movel %0, %%a1\n\t"
  65. "lea %%pc@(1f), %%a0\n\t"
  66. "subql #1, %%a1@\n\t"
  67. "jmi __down_failed\n"
  68. "1:"
  69. : /* no outputs */
  70. : "g" (sem)
  71. : "cc", "%a0", "%a1", "memory");
  72. }
  73. static inline int down_interruptible(struct semaphore * sem)
  74. {
  75. int ret;
  76. might_sleep();
  77. __asm__ __volatile__(
  78. "| atomic down operation\n\t"
  79. "movel %1, %%a1\n\t"
  80. "lea %%pc@(1f), %%a0\n\t"
  81. "subql #1, %%a1@\n\t"
  82. "jmi __down_failed_interruptible\n\t"
  83. "clrl %%d0\n"
  84. "1: movel %%d0, %0\n"
  85. : "=d" (ret)
  86. : "g" (sem)
  87. : "cc", "%d0", "%a0", "%a1", "memory");
  88. return(ret);
  89. }
  90. static inline int down_trylock(struct semaphore * sem)
  91. {
  92. register struct semaphore *sem1 __asm__ ("%a1") = sem;
  93. register int result __asm__ ("%d0");
  94. __asm__ __volatile__(
  95. "| atomic down trylock operation\n\t"
  96. "subql #1,%1@\n\t"
  97. "jmi 2f\n\t"
  98. "clrl %0\n"
  99. "1:\n"
  100. ".section .text.lock,\"ax\"\n"
  101. ".even\n"
  102. "2:\tpea 1b\n\t"
  103. "jbra __down_failed_trylock\n"
  104. ".previous"
  105. : "=d" (result)
  106. : "a" (sem1)
  107. : "memory");
  108. return result;
  109. }
  110. /*
  111. * Note! This is subtle. We jump to wake people up only if
  112. * the semaphore was negative (== somebody was waiting on it).
  113. * The default case (no contention) will result in NO
  114. * jumps for both down() and up().
  115. */
  116. static inline void up(struct semaphore * sem)
  117. {
  118. __asm__ __volatile__(
  119. "| atomic up operation\n\t"
  120. "movel %0, %%a1\n\t"
  121. "lea %%pc@(1f), %%a0\n\t"
  122. "addql #1, %%a1@\n\t"
  123. "jle __up_wakeup\n"
  124. "1:"
  125. : /* no outputs */
  126. : "g" (sem)
  127. : "cc", "%a0", "%a1", "memory");
  128. }
  129. #endif /* __ASSEMBLY__ */
  130. #endif