blob: 81750edc8916267e035dcf47484afe001910c74a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _ASM_M32R_SEMAPHORE_H
2#define _ASM_M32R_SEMAPHORE_H
3
4#include <linux/linkage.h>
5
6#ifdef __KERNEL__
7
8/*
9 * SMP- and interrupt-safe semaphores..
10 *
11 * Copyright (C) 1996 Linus Torvalds
Hirokazu Takatafa372812006-04-18 22:21:25 -070012 * Copyright (C) 2004, 2006 Hirokazu Takata <takata at linux-m32r.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
14
15#include <linux/config.h>
16#include <linux/wait.h>
17#include <linux/rwsem.h>
18#include <asm/assembler.h>
19#include <asm/system.h>
20#include <asm/atomic.h>
21
22struct semaphore {
23 atomic_t count;
24 int sleepers;
25 wait_queue_head_t wait;
26};
27
28#define __SEMAPHORE_INITIALIZER(name, n) \
29{ \
30 .count = ATOMIC_INIT(n), \
31 .sleepers = 0, \
32 .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
33}
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define __DECLARE_SEMAPHORE_GENERIC(name,count) \
36 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
37
38#define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
39#define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
40
41static inline void sema_init (struct semaphore *sem, int val)
42{
43/*
44 * *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
45 *
46 * i'd rather use the more flexible initialization above, but sadly
47 * GCC 2.7.2.3 emits a bogus warning. EGCS doesnt. Oh well.
48 */
49 atomic_set(&sem->count, val);
50 sem->sleepers = 0;
51 init_waitqueue_head(&sem->wait);
52}
53
54static inline void init_MUTEX (struct semaphore *sem)
55{
56 sema_init(sem, 1);
57}
58
59static inline void init_MUTEX_LOCKED (struct semaphore *sem)
60{
61 sema_init(sem, 0);
62}
63
64asmlinkage void __down_failed(void /* special register calling convention */);
65asmlinkage int __down_failed_interruptible(void /* params in registers */);
66asmlinkage int __down_failed_trylock(void /* params in registers */);
67asmlinkage void __up_wakeup(void /* special register calling convention */);
68
69asmlinkage void __down(struct semaphore * sem);
70asmlinkage int __down_interruptible(struct semaphore * sem);
71asmlinkage int __down_trylock(struct semaphore * sem);
72asmlinkage void __up(struct semaphore * sem);
73
74/*
75 * Atomically decrement the semaphore's count. If it goes negative,
76 * block the calling thread in the TASK_UNINTERRUPTIBLE state.
77 */
78static inline void down(struct semaphore * sem)
79{
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 might_sleep();
Hirokazu Takatafa372812006-04-18 22:21:25 -070081 if (unlikely(atomic_dec_return(&sem->count) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 __down(sem);
83}
84
85/*
86 * Interruptible try to acquire a semaphore. If we obtained
87 * it, return zero. If we were interrupted, returns -EINTR
88 */
89static inline int down_interruptible(struct semaphore * sem)
90{
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 int result = 0;
92
93 might_sleep();
Hirokazu Takatafa372812006-04-18 22:21:25 -070094 if (unlikely(atomic_dec_return(&sem->count) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 result = __down_interruptible(sem);
96
97 return result;
98}
99
100/*
101 * Non-blockingly attempt to down() a semaphore.
102 * Returns zero if we acquired it
103 */
104static inline int down_trylock(struct semaphore * sem)
105{
106 unsigned long flags;
107 long count;
108 int result = 0;
109
110 local_irq_save(flags);
111 __asm__ __volatile__ (
112 "# down_trylock \n\t"
113 DCACHE_CLEAR("%0", "r4", "%1")
114 M32R_LOCK" %0, @%1; \n\t"
115 "addi %0, #-1; \n\t"
116 M32R_UNLOCK" %0, @%1; \n\t"
117 : "=&r" (count)
118 : "r" (&sem->count)
119 : "memory"
120#ifdef CONFIG_CHIP_M32700_TS1
121 , "r4"
122#endif /* CONFIG_CHIP_M32700_TS1 */
123 );
124 local_irq_restore(flags);
125
126 if (unlikely(count < 0))
127 result = __down_trylock(sem);
128
129 return result;
130}
131
132/*
133 * Note! This is subtle. We jump to wake people up only if
134 * the semaphore was negative (== somebody was waiting on it).
135 * The default case (no contention) will result in NO
136 * jumps for both down() and up().
137 */
138static inline void up(struct semaphore * sem)
139{
Hirokazu Takatafa372812006-04-18 22:21:25 -0700140 if (unlikely(atomic_inc_return(&sem->count) <= 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 __up(sem);
142}
143
144#endif /* __KERNEL__ */
145
146#endif /* _ASM_M32R_SEMAPHORE_H */