blob: ab94cf3ed4477b70b3f61c39b0dcec7d4cfffd04 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _M68K_SEMAPHORE_H
2#define _M68K_SEMAPHORE_H
3
4#define RW_LOCK_BIAS 0x01000000
5
6#ifndef __ASSEMBLY__
7
8#include <linux/linkage.h>
9#include <linux/wait.h>
10#include <linux/spinlock.h>
11#include <linux/rwsem.h>
12#include <linux/stringify.h>
13
14#include <asm/system.h>
15#include <asm/atomic.h>
16
17/*
18 * Interrupt-safe semaphores..
19 *
20 * (C) Copyright 1996 Linus Torvalds
21 *
22 * m68k version by Andreas Schwab
23 */
24
25
26struct semaphore {
27 atomic_t count;
28 atomic_t waking;
29 wait_queue_head_t wait;
30};
31
32#define __SEMAPHORE_INITIALIZER(name, n) \
33{ \
34 .count = ATOMIC_INIT(n), \
35 .waking = ATOMIC_INIT(0), \
36 .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
37}
38
39#define __MUTEX_INITIALIZER(name) \
40 __SEMAPHORE_INITIALIZER(name,1)
41
42#define __DECLARE_SEMAPHORE_GENERIC(name,count) \
43 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
44
45#define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
46#define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
47
48static inline void sema_init(struct semaphore *sem, int val)
49{
50 *sem = (struct semaphore)__SEMAPHORE_INITIALIZER(*sem, val);
51}
52
53static inline void init_MUTEX (struct semaphore *sem)
54{
55 sema_init(sem, 1);
56}
57
58static inline void init_MUTEX_LOCKED (struct semaphore *sem)
59{
60 sema_init(sem, 0);
61}
62
63asmlinkage void __down_failed(void /* special register calling convention */);
64asmlinkage int __down_failed_interruptible(void /* params in registers */);
65asmlinkage int __down_failed_trylock(void /* params in registers */);
66asmlinkage void __up_wakeup(void /* special register calling convention */);
67
68asmlinkage void __down(struct semaphore * sem);
69asmlinkage int __down_interruptible(struct semaphore * sem);
70asmlinkage int __down_trylock(struct semaphore * sem);
71asmlinkage void __up(struct semaphore * sem);
72
73/*
74 * This is ugly, but we want the default case to fall through.
75 * "down_failed" is a special asm handler that calls the C
76 * routine that actually waits. See arch/m68k/lib/semaphore.S
77 */
78static inline void down(struct semaphore *sem)
79{
80 register struct semaphore *sem1 __asm__ ("%a1") = sem;
81
82 might_sleep();
83 __asm__ __volatile__(
84 "| atomic down operation\n\t"
85 "subql #1,%0@\n\t"
86 "jmi 2f\n\t"
87 "1:\n"
88 LOCK_SECTION_START(".even\n\t")
89 "2:\tpea 1b\n\t"
90 "jbra __down_failed\n"
91 LOCK_SECTION_END
92 : /* no outputs */
93 : "a" (sem1)
94 : "memory");
95}
96
97static inline int down_interruptible(struct semaphore *sem)
98{
99 register struct semaphore *sem1 __asm__ ("%a1") = sem;
100 register int result __asm__ ("%d0");
101
102 might_sleep();
103 __asm__ __volatile__(
104 "| atomic interruptible down operation\n\t"
105 "subql #1,%1@\n\t"
106 "jmi 2f\n\t"
107 "clrl %0\n"
108 "1:\n"
109 LOCK_SECTION_START(".even\n\t")
110 "2:\tpea 1b\n\t"
111 "jbra __down_failed_interruptible\n"
112 LOCK_SECTION_END
113 : "=d" (result)
114 : "a" (sem1)
115 : "memory");
116 return result;
117}
118
119static inline int down_trylock(struct semaphore *sem)
120{
121 register struct semaphore *sem1 __asm__ ("%a1") = sem;
122 register int result __asm__ ("%d0");
123
124 __asm__ __volatile__(
125 "| atomic down trylock operation\n\t"
126 "subql #1,%1@\n\t"
127 "jmi 2f\n\t"
128 "clrl %0\n"
129 "1:\n"
130 LOCK_SECTION_START(".even\n\t")
131 "2:\tpea 1b\n\t"
132 "jbra __down_failed_trylock\n"
133 LOCK_SECTION_END
134 : "=d" (result)
135 : "a" (sem1)
136 : "memory");
137 return result;
138}
139
140/*
141 * Note! This is subtle. We jump to wake people up only if
142 * the semaphore was negative (== somebody was waiting on it).
143 * The default case (no contention) will result in NO
144 * jumps for both down() and up().
145 */
146static inline void up(struct semaphore *sem)
147{
148 register struct semaphore *sem1 __asm__ ("%a1") = sem;
149
150 __asm__ __volatile__(
151 "| atomic up operation\n\t"
152 "addql #1,%0@\n\t"
153 "jle 2f\n"
154 "1:\n"
155 LOCK_SECTION_START(".even\n\t")
156 "2:\t"
157 "pea 1b\n\t"
158 "jbra __up_wakeup\n"
159 LOCK_SECTION_END
160 : /* no outputs */
161 : "a" (sem1)
162 : "memory");
163}
164
165#endif /* __ASSEMBLY__ */
166
167#endif