blob: 9171f912014372c54a2dec5b154b648d40dc7369 [file] [log] [blame]
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -08001Hardware Spinlock Framework
2
31. Introduction
4
5Hardware spinlock modules provide hardware assistance for synchronization
6and mutual exclusion between heterogeneous processors and those not operating
7under a single, shared operating system.
8
9For example, OMAP4 has dual Cortex-A9, dual Cortex-M3 and a C64x+ DSP,
10each of which is running a different Operating System (the master, A9,
11is usually running Linux and the slave processors, the M3 and the DSP,
12are running some flavor of RTOS).
13
14A generic hwspinlock framework allows platform-independent drivers to use
15the hwspinlock device in order to access data structures that are shared
16between remote processors, that otherwise have no alternative mechanism
17to accomplish synchronization and mutual exclusion operations.
18
19This is necessary, for example, for Inter-processor communications:
20on OMAP4, cpu-intensive multimedia tasks are offloaded by the host to the
21remote M3 and/or C64x+ slave processors (by an IPC subsystem called Syslink).
22
23To achieve fast message-based communications, a minimal kernel support
24is needed to deliver messages arriving from a remote processor to the
25appropriate user process.
26
27This communication is based on simple data structures that is shared between
28the remote processors, and access to it is synchronized using the hwspinlock
29module (remote processor directly places new messages in this shared data
30structure).
31
32A common hwspinlock interface makes it possible to have generic, platform-
33independent, drivers.
34
352. User API
36
37 struct hwspinlock *hwspin_lock_request(void);
38 - dynamically assign an hwspinlock and return its address, or NULL
39 in case an unused hwspinlock isn't available. Users of this
40 API will usually want to communicate the lock's id to the remote core
41 before it can be used to achieve synchronization.
Juan Gutierrez93b465c2011-09-06 09:30:16 +030042 Should be called from a process context (might sleep).
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -080043
44 struct hwspinlock *hwspin_lock_request_specific(unsigned int id);
45 - assign a specific hwspinlock id and return its address, or NULL
46 if that hwspinlock is already in use. Usually board code will
47 be calling this function in order to reserve specific hwspinlock
48 ids for predefined purposes.
Juan Gutierrez93b465c2011-09-06 09:30:16 +030049 Should be called from a process context (might sleep).
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -080050
51 int hwspin_lock_free(struct hwspinlock *hwlock);
52 - free a previously-assigned hwspinlock; returns 0 on success, or an
53 appropriate error code on failure (e.g. -EINVAL if the hwspinlock
54 is already free).
Juan Gutierrez93b465c2011-09-06 09:30:16 +030055 Should be called from a process context (might sleep).
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -080056
57 int hwspin_lock_timeout(struct hwspinlock *hwlock, unsigned int timeout);
58 - lock a previously-assigned hwspinlock with a timeout limit (specified in
59 msecs). If the hwspinlock is already taken, the function will busy loop
60 waiting for it to be released, but give up when the timeout elapses.
61 Upon a successful return from this function, preemption is disabled so
62 the caller must not sleep, and is advised to release the hwspinlock as
63 soon as possible, in order to minimize remote cores polling on the
64 hardware interconnect.
65 Returns 0 when successful and an appropriate error code otherwise (most
66 notably -ETIMEDOUT if the hwspinlock is still busy after timeout msecs).
67 The function will never sleep.
68
69 int hwspin_lock_timeout_irq(struct hwspinlock *hwlock, unsigned int timeout);
70 - lock a previously-assigned hwspinlock with a timeout limit (specified in
71 msecs). If the hwspinlock is already taken, the function will busy loop
72 waiting for it to be released, but give up when the timeout elapses.
73 Upon a successful return from this function, preemption and the local
74 interrupts are disabled, so the caller must not sleep, and is advised to
75 release the hwspinlock as soon as possible.
76 Returns 0 when successful and an appropriate error code otherwise (most
77 notably -ETIMEDOUT if the hwspinlock is still busy after timeout msecs).
78 The function will never sleep.
79
80 int hwspin_lock_timeout_irqsave(struct hwspinlock *hwlock, unsigned int to,
81 unsigned long *flags);
82 - lock a previously-assigned hwspinlock with a timeout limit (specified in
83 msecs). If the hwspinlock is already taken, the function will busy loop
84 waiting for it to be released, but give up when the timeout elapses.
85 Upon a successful return from this function, preemption is disabled,
86 local interrupts are disabled and their previous state is saved at the
87 given flags placeholder. The caller must not sleep, and is advised to
88 release the hwspinlock as soon as possible.
89 Returns 0 when successful and an appropriate error code otherwise (most
90 notably -ETIMEDOUT if the hwspinlock is still busy after timeout msecs).
91 The function will never sleep.
92
93 int hwspin_trylock(struct hwspinlock *hwlock);
94 - attempt to lock a previously-assigned hwspinlock, but immediately fail if
95 it is already taken.
96 Upon a successful return from this function, preemption is disabled so
97 caller must not sleep, and is advised to release the hwspinlock as soon as
98 possible, in order to minimize remote cores polling on the hardware
99 interconnect.
100 Returns 0 on success and an appropriate error code otherwise (most
101 notably -EBUSY if the hwspinlock was already taken).
102 The function will never sleep.
103
104 int hwspin_trylock_irq(struct hwspinlock *hwlock);
105 - attempt to lock a previously-assigned hwspinlock, but immediately fail if
106 it is already taken.
107 Upon a successful return from this function, preemption and the local
108 interrupts are disabled so caller must not sleep, and is advised to
109 release the hwspinlock as soon as possible.
110 Returns 0 on success and an appropriate error code otherwise (most
111 notably -EBUSY if the hwspinlock was already taken).
112 The function will never sleep.
113
114 int hwspin_trylock_irqsave(struct hwspinlock *hwlock, unsigned long *flags);
115 - attempt to lock a previously-assigned hwspinlock, but immediately fail if
116 it is already taken.
117 Upon a successful return from this function, preemption is disabled,
118 the local interrupts are disabled and their previous state is saved
119 at the given flags placeholder. The caller must not sleep, and is advised
120 to release the hwspinlock as soon as possible.
121 Returns 0 on success and an appropriate error code otherwise (most
122 notably -EBUSY if the hwspinlock was already taken).
123 The function will never sleep.
124
125 void hwspin_unlock(struct hwspinlock *hwlock);
126 - unlock a previously-locked hwspinlock. Always succeed, and can be called
127 from any context (the function never sleeps). Note: code should _never_
128 unlock an hwspinlock which is already unlocked (there is no protection
129 against this).
130
131 void hwspin_unlock_irq(struct hwspinlock *hwlock);
132 - unlock a previously-locked hwspinlock and enable local interrupts.
133 The caller should _never_ unlock an hwspinlock which is already unlocked.
134 Doing so is considered a bug (there is no protection against this).
135 Upon a successful return from this function, preemption and local
136 interrupts are enabled. This function will never sleep.
137
138 void
139 hwspin_unlock_irqrestore(struct hwspinlock *hwlock, unsigned long *flags);
140 - unlock a previously-locked hwspinlock.
141 The caller should _never_ unlock an hwspinlock which is already unlocked.
142 Doing so is considered a bug (there is no protection against this).
143 Upon a successful return from this function, preemption is reenabled,
144 and the state of the local interrupts is restored to the state saved at
145 the given flags. This function will never sleep.
146
147 int hwspin_lock_get_id(struct hwspinlock *hwlock);
148 - retrieve id number of a given hwspinlock. This is needed when an
149 hwspinlock is dynamically assigned: before it can be used to achieve
150 mutual exclusion with a remote cpu, the id number should be communicated
151 to the remote task with which we want to synchronize.
152 Returns the hwspinlock id number, or -EINVAL if hwlock is null.
153
1543. Typical usage
155
156#include <linux/hwspinlock.h>
157#include <linux/err.h>
158
159int hwspinlock_example1(void)
160{
161 struct hwspinlock *hwlock;
162 int ret;
163
164 /* dynamically assign a hwspinlock */
165 hwlock = hwspin_lock_request();
166 if (!hwlock)
167 ...
168
169 id = hwspin_lock_get_id(hwlock);
170 /* probably need to communicate id to a remote processor now */
171
172 /* take the lock, spin for 1 sec if it's already taken */
173 ret = hwspin_lock_timeout(hwlock, 1000);
174 if (ret)
175 ...
176
177 /*
178 * we took the lock, do our thing now, but do NOT sleep
179 */
180
181 /* release the lock */
182 hwspin_unlock(hwlock);
183
184 /* free the lock */
185 ret = hwspin_lock_free(hwlock);
186 if (ret)
187 ...
188
189 return ret;
190}
191
192int hwspinlock_example2(void)
193{
194 struct hwspinlock *hwlock;
195 int ret;
196
197 /*
198 * assign a specific hwspinlock id - this should be called early
199 * by board init code.
200 */
201 hwlock = hwspin_lock_request_specific(PREDEFINED_LOCK_ID);
202 if (!hwlock)
203 ...
204
205 /* try to take it, but don't spin on it */
206 ret = hwspin_trylock(hwlock);
207 if (!ret) {
208 pr_info("lock is already taken\n");
209 return -EBUSY;
210 }
211
212 /*
213 * we took the lock, do our thing now, but do NOT sleep
214 */
215
216 /* release the lock */
217 hwspin_unlock(hwlock);
218
219 /* free the lock */
220 ret = hwspin_lock_free(hwlock);
221 if (ret)
222 ...
223
224 return ret;
225}
226
227
2284. API for implementors
229
230 int hwspin_lock_register(struct hwspinlock *hwlock);
231 - to be called from the underlying platform-specific implementation, in
Juan Gutierrez93b465c2011-09-06 09:30:16 +0300232 order to register a new hwspinlock instance. Should be called from
233 a process context (this function might sleep).
234 Returns 0 on success, or appropriate error code on failure.
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800235
236 struct hwspinlock *hwspin_lock_unregister(unsigned int id);
237 - to be called from the underlying vendor-specific implementation, in order
238 to unregister an existing (and unused) hwspinlock instance.
Juan Gutierrez93b465c2011-09-06 09:30:16 +0300239 Should be called from a process context (this function might sleep).
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800240 Returns the address of hwspinlock on success, or NULL on error (e.g.
241 if the hwspinlock is sill in use).
242
2435. struct hwspinlock
244
245This struct represents an hwspinlock instance. It is registered by the
246underlying hwspinlock implementation using the hwspin_lock_register() API.
247
248/**
249 * struct hwspinlock - vendor-specific hwspinlock implementation
250 *
251 * @dev: underlying device, will be used with runtime PM api
252 * @ops: vendor-specific hwspinlock handlers
253 * @id: a global, unique, system-wide, index of the lock.
254 * @lock: initialized and used by hwspinlock core
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800255 */
256struct hwspinlock {
257 struct device *dev;
258 const struct hwspinlock_ops *ops;
259 int id;
260 spinlock_t lock;
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800261};
262
Ohad Ben-Cohene467b642011-09-05 16:42:36 +0300263The underlying implementation is responsible to assign the dev, ops and id
264members. The lock member, OTOH, is initialized and used by the hwspinlock
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800265core.
266
2676. Implementation callbacks
268
269There are three possible callbacks defined in 'struct hwspinlock_ops':
270
271struct hwspinlock_ops {
272 int (*trylock)(struct hwspinlock *lock);
273 void (*unlock)(struct hwspinlock *lock);
274 void (*relax)(struct hwspinlock *lock);
275};
276
277The first two callbacks are mandatory:
278
279The ->trylock() callback should make a single attempt to take the lock, and
280return 0 on failure and 1 on success. This callback may _not_ sleep.
281
282The ->unlock() callback releases the lock. It always succeed, and it, too,
283may _not_ sleep.
284
285The ->relax() callback is optional. It is called by hwspinlock core while
286spinning on a lock, and can be used by the underlying implementation to force
287a delay between two successive invocations of ->trylock(). It may _not_ sleep.