semaphore.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. static inline void sema_init (struct semaphore *sem, int val)
  33. {
  34. *sem = (struct semaphore)__SEMAPHORE_INITIALIZER(*sem, val);
  35. }
  36. static inline void init_MUTEX (struct semaphore *sem)
  37. {
  38. sema_init(sem, 1);
  39. }
  40. static inline void init_MUTEX_LOCKED (struct semaphore *sem)
  41. {
  42. sema_init(sem, 0);
  43. }
  44. asmlinkage void __down_failed(void /* special register calling convention */);
  45. asmlinkage int __down_failed_interruptible(void /* params in registers */);
  46. asmlinkage int __down_failed_trylock(void /* params in registers */);
  47. asmlinkage void __up_wakeup(void /* special register calling convention */);
  48. asmlinkage void __down(struct semaphore * sem);
  49. asmlinkage int __down_interruptible(struct semaphore * sem);
  50. asmlinkage int __down_trylock(struct semaphore * sem);
  51. asmlinkage void __up(struct semaphore * sem);
  52. extern spinlock_t semaphore_wake_lock;
  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. might_sleep();
  61. __asm__ __volatile__(
  62. "| atomic down operation\n\t"
  63. "movel %0, %%a1\n\t"
  64. "lea %%pc@(1f), %%a0\n\t"
  65. "subql #1, %%a1@\n\t"
  66. "jmi __down_failed\n"
  67. "1:"
  68. : /* no outputs */
  69. : "g" (sem)
  70. : "cc", "%a0", "%a1", "memory");
  71. }
  72. static inline int down_interruptible(struct semaphore * sem)
  73. {
  74. int ret;
  75. might_sleep();
  76. __asm__ __volatile__(
  77. "| atomic down operation\n\t"
  78. "movel %1, %%a1\n\t"
  79. "lea %%pc@(1f), %%a0\n\t"
  80. "subql #1, %%a1@\n\t"
  81. "jmi __down_failed_interruptible\n\t"
  82. "clrl %%d0\n"
  83. "1: movel %%d0, %0\n"
  84. : "=d" (ret)
  85. : "g" (sem)
  86. : "cc", "%d0", "%a0", "%a1", "memory");
  87. return(ret);
  88. }
  89. static inline int down_trylock(struct semaphore * sem)
  90. {
  91. register struct semaphore *sem1 __asm__ ("%a1") = sem;
  92. register int result __asm__ ("%d0");
  93. __asm__ __volatile__(
  94. "| atomic down trylock operation\n\t"
  95. "subql #1,%1@\n\t"
  96. "jmi 2f\n\t"
  97. "clrl %0\n"
  98. "1:\n"
  99. ".section .text.lock,\"ax\"\n"
  100. ".even\n"
  101. "2:\tpea 1b\n\t"
  102. "jbra __down_failed_trylock\n"
  103. ".previous"
  104. : "=d" (result)
  105. : "a" (sem1)
  106. : "memory");
  107. return result;
  108. }
  109. /*
  110. * Note! This is subtle. We jump to wake people up only if
  111. * the semaphore was negative (== somebody was waiting on it).
  112. * The default case (no contention) will result in NO
  113. * jumps for both down() and up().
  114. */
  115. static inline void up(struct semaphore * sem)
  116. {
  117. __asm__ __volatile__(
  118. "| atomic up operation\n\t"
  119. "movel %0, %%a1\n\t"
  120. "lea %%pc@(1f), %%a0\n\t"
  121. "addql #1, %%a1@\n\t"
  122. "jle __up_wakeup\n"
  123. "1:"
  124. : /* no outputs */
  125. : "g" (sem)
  126. : "cc", "%a0", "%a1", "memory");
  127. }
  128. #endif /* __ASSEMBLY__ */
  129. #endif