hwspinlock/core: remove stubs for register/unregister
[deliverable/linux.git] / drivers / hwspinlock / hwspinlock_core.c
CommitLineData
bd9a4c7d
OBC
1/*
2 * Hardware spinlock framework
3 *
4 * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com
5 *
6 * Contact: Ohad Ben-Cohen <ohad@wizery.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published
10 * by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#define pr_fmt(fmt) "%s: " fmt, __func__
19
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/spinlock.h>
23#include <linux/types.h>
24#include <linux/err.h>
25#include <linux/jiffies.h>
26#include <linux/radix-tree.h>
27#include <linux/hwspinlock.h>
28#include <linux/pm_runtime.h>
93b465c2 29#include <linux/mutex.h>
bd9a4c7d
OBC
30
31#include "hwspinlock_internal.h"
32
33/* radix tree tags */
34#define HWSPINLOCK_UNUSED (0) /* tags an hwspinlock as unused */
35
36/*
37 * A radix tree is used to maintain the available hwspinlock instances.
38 * The tree associates hwspinlock pointers with their integer key id,
39 * and provides easy-to-use API which makes the hwspinlock core code simple
40 * and easy to read.
41 *
42 * Radix trees are quick on lookups, and reasonably efficient in terms of
43 * storage, especially with high density usages such as this framework
44 * requires (a continuous range of integer keys, beginning with zero, is
45 * used as the ID's of the hwspinlock instances).
46 *
47 * The radix tree API supports tagging items in the tree, which this
48 * framework uses to mark unused hwspinlock instances (see the
49 * HWSPINLOCK_UNUSED tag above). As a result, the process of querying the
50 * tree, looking for an unused hwspinlock instance, is now reduced to a
51 * single radix tree API call.
52 */
53static RADIX_TREE(hwspinlock_tree, GFP_KERNEL);
54
55/*
93b465c2 56 * Synchronization of access to the tree is achieved using this mutex,
bd9a4c7d 57 * as the radix-tree API requires that users provide all synchronisation.
93b465c2 58 * A mutex is needed because we're using non-atomic radix tree allocations.
bd9a4c7d 59 */
93b465c2
JG
60static DEFINE_MUTEX(hwspinlock_tree_lock);
61
bd9a4c7d
OBC
62
63/**
64 * __hwspin_trylock() - attempt to lock a specific hwspinlock
65 * @hwlock: an hwspinlock which we want to trylock
66 * @mode: controls whether local interrupts are disabled or not
67 * @flags: a pointer where the caller's interrupt state will be saved at (if
68 * requested)
69 *
70 * This function attempts to lock an hwspinlock, and will immediately
71 * fail if the hwspinlock is already taken.
72 *
73 * Upon a successful return from this function, preemption (and possibly
74 * interrupts) is disabled, so the caller must not sleep, and is advised to
75 * release the hwspinlock as soon as possible. This is required in order to
76 * minimize remote cores polling on the hardware interconnect.
77 *
78 * The user decides whether local interrupts are disabled or not, and if yes,
79 * whether he wants their previous state to be saved. It is up to the user
80 * to choose the appropriate @mode of operation, exactly the same way users
81 * should decide between spin_trylock, spin_trylock_irq and
82 * spin_trylock_irqsave.
83 *
84 * Returns 0 if we successfully locked the hwspinlock or -EBUSY if
85 * the hwspinlock was already taken.
86 * This function will never sleep.
87 */
88int __hwspin_trylock(struct hwspinlock *hwlock, int mode, unsigned long *flags)
89{
90 int ret;
91
92 BUG_ON(!hwlock);
93 BUG_ON(!flags && mode == HWLOCK_IRQSTATE);
94
95 /*
96 * This spin_lock{_irq, _irqsave} serves three purposes:
97 *
98 * 1. Disable preemption, in order to minimize the period of time
99 * in which the hwspinlock is taken. This is important in order
100 * to minimize the possible polling on the hardware interconnect
101 * by a remote user of this lock.
102 * 2. Make the hwspinlock SMP-safe (so we can take it from
103 * additional contexts on the local host).
104 * 3. Ensure that in_atomic/might_sleep checks catch potential
105 * problems with hwspinlock usage (e.g. scheduler checks like
106 * 'scheduling while atomic' etc.)
107 */
108 if (mode == HWLOCK_IRQSTATE)
109 ret = spin_trylock_irqsave(&hwlock->lock, *flags);
110 else if (mode == HWLOCK_IRQ)
111 ret = spin_trylock_irq(&hwlock->lock);
112 else
113 ret = spin_trylock(&hwlock->lock);
114
115 /* is lock already taken by another context on the local cpu ? */
116 if (!ret)
117 return -EBUSY;
118
119 /* try to take the hwspinlock device */
120 ret = hwlock->ops->trylock(hwlock);
121
122 /* if hwlock is already taken, undo spin_trylock_* and exit */
123 if (!ret) {
124 if (mode == HWLOCK_IRQSTATE)
125 spin_unlock_irqrestore(&hwlock->lock, *flags);
126 else if (mode == HWLOCK_IRQ)
127 spin_unlock_irq(&hwlock->lock);
128 else
129 spin_unlock(&hwlock->lock);
130
131 return -EBUSY;
132 }
133
134 /*
135 * We can be sure the other core's memory operations
136 * are observable to us only _after_ we successfully take
137 * the hwspinlock, and we must make sure that subsequent memory
138 * operations (both reads and writes) will not be reordered before
139 * we actually took the hwspinlock.
140 *
141 * Note: the implicit memory barrier of the spinlock above is too
142 * early, so we need this additional explicit memory barrier.
143 */
144 mb();
145
146 return 0;
147}
148EXPORT_SYMBOL_GPL(__hwspin_trylock);
149
150/**
151 * __hwspin_lock_timeout() - lock an hwspinlock with timeout limit
152 * @hwlock: the hwspinlock to be locked
153 * @timeout: timeout value in msecs
154 * @mode: mode which controls whether local interrupts are disabled or not
155 * @flags: a pointer to where the caller's interrupt state will be saved at (if
156 * requested)
157 *
158 * This function locks the given @hwlock. If the @hwlock
159 * is already taken, the function will busy loop waiting for it to
160 * be released, but give up after @timeout msecs have elapsed.
161 *
162 * Upon a successful return from this function, preemption is disabled
163 * (and possibly local interrupts, too), so the caller must not sleep,
164 * and is advised to release the hwspinlock as soon as possible.
165 * This is required in order to minimize remote cores polling on the
166 * hardware interconnect.
167 *
168 * The user decides whether local interrupts are disabled or not, and if yes,
169 * whether he wants their previous state to be saved. It is up to the user
170 * to choose the appropriate @mode of operation, exactly the same way users
171 * should decide between spin_lock, spin_lock_irq and spin_lock_irqsave.
172 *
173 * Returns 0 when the @hwlock was successfully taken, and an appropriate
174 * error code otherwise (most notably -ETIMEDOUT if the @hwlock is still
175 * busy after @timeout msecs). The function will never sleep.
176 */
177int __hwspin_lock_timeout(struct hwspinlock *hwlock, unsigned int to,
178 int mode, unsigned long *flags)
179{
180 int ret;
181 unsigned long expire;
182
183 expire = msecs_to_jiffies(to) + jiffies;
184
185 for (;;) {
186 /* Try to take the hwspinlock */
187 ret = __hwspin_trylock(hwlock, mode, flags);
188 if (ret != -EBUSY)
189 break;
190
191 /*
192 * The lock is already taken, let's check if the user wants
193 * us to try again
194 */
195 if (time_is_before_eq_jiffies(expire))
196 return -ETIMEDOUT;
197
198 /*
199 * Allow platform-specific relax handlers to prevent
200 * hogging the interconnect (no sleeping, though)
201 */
202 if (hwlock->ops->relax)
203 hwlock->ops->relax(hwlock);
204 }
205
206 return ret;
207}
208EXPORT_SYMBOL_GPL(__hwspin_lock_timeout);
209
210/**
211 * __hwspin_unlock() - unlock a specific hwspinlock
212 * @hwlock: a previously-acquired hwspinlock which we want to unlock
213 * @mode: controls whether local interrupts needs to be restored or not
214 * @flags: previous caller's interrupt state to restore (if requested)
215 *
216 * This function will unlock a specific hwspinlock, enable preemption and
217 * (possibly) enable interrupts or restore their previous state.
218 * @hwlock must be already locked before calling this function: it is a bug
219 * to call unlock on a @hwlock that is already unlocked.
220 *
221 * The user decides whether local interrupts should be enabled or not, and
222 * if yes, whether he wants their previous state to be restored. It is up
223 * to the user to choose the appropriate @mode of operation, exactly the
224 * same way users decide between spin_unlock, spin_unlock_irq and
225 * spin_unlock_irqrestore.
226 *
227 * The function will never sleep.
228 */
229void __hwspin_unlock(struct hwspinlock *hwlock, int mode, unsigned long *flags)
230{
231 BUG_ON(!hwlock);
232 BUG_ON(!flags && mode == HWLOCK_IRQSTATE);
233
234 /*
235 * We must make sure that memory operations (both reads and writes),
236 * done before unlocking the hwspinlock, will not be reordered
237 * after the lock is released.
238 *
239 * That's the purpose of this explicit memory barrier.
240 *
241 * Note: the memory barrier induced by the spin_unlock below is too
242 * late; the other core is going to access memory soon after it will
243 * take the hwspinlock, and by then we want to be sure our memory
244 * operations are already observable.
245 */
246 mb();
247
248 hwlock->ops->unlock(hwlock);
249
250 /* Undo the spin_trylock{_irq, _irqsave} called while locking */
251 if (mode == HWLOCK_IRQSTATE)
252 spin_unlock_irqrestore(&hwlock->lock, *flags);
253 else if (mode == HWLOCK_IRQ)
254 spin_unlock_irq(&hwlock->lock);
255 else
256 spin_unlock(&hwlock->lock);
257}
258EXPORT_SYMBOL_GPL(__hwspin_unlock);
259
260/**
261 * hwspin_lock_register() - register a new hw spinlock
262 * @hwlock: hwspinlock to register.
263 *
264 * This function should be called from the underlying platform-specific
265 * implementation, to register a new hwspinlock instance.
266 *
93b465c2 267 * Should be called from a process context (might sleep)
bd9a4c7d
OBC
268 *
269 * Returns 0 on success, or an appropriate error code on failure
270 */
271int hwspin_lock_register(struct hwspinlock *hwlock)
272{
273 struct hwspinlock *tmp;
274 int ret;
275
276 if (!hwlock || !hwlock->ops ||
277 !hwlock->ops->trylock || !hwlock->ops->unlock) {
278 pr_err("invalid parameters\n");
279 return -EINVAL;
280 }
281
282 spin_lock_init(&hwlock->lock);
283
93b465c2 284 mutex_lock(&hwspinlock_tree_lock);
bd9a4c7d
OBC
285
286 ret = radix_tree_insert(&hwspinlock_tree, hwlock->id, hwlock);
c3c1250e
OBC
287 if (ret == -EEXIST)
288 pr_err("hwspinlock id %d already exists!\n", hwlock->id);
bd9a4c7d
OBC
289 if (ret)
290 goto out;
291
292 /* mark this hwspinlock as available */
293 tmp = radix_tree_tag_set(&hwspinlock_tree, hwlock->id,
294 HWSPINLOCK_UNUSED);
295
296 /* self-sanity check which should never fail */
297 WARN_ON(tmp != hwlock);
298
299out:
93b465c2 300 mutex_unlock(&hwspinlock_tree_lock);
bd9a4c7d
OBC
301 return ret;
302}
303EXPORT_SYMBOL_GPL(hwspin_lock_register);
304
305/**
306 * hwspin_lock_unregister() - unregister an hw spinlock
307 * @id: index of the specific hwspinlock to unregister
308 *
309 * This function should be called from the underlying platform-specific
310 * implementation, to unregister an existing (and unused) hwspinlock.
311 *
93b465c2 312 * Should be called from a process context (might sleep)
bd9a4c7d
OBC
313 *
314 * Returns the address of hwspinlock @id on success, or NULL on failure
315 */
316struct hwspinlock *hwspin_lock_unregister(unsigned int id)
317{
318 struct hwspinlock *hwlock = NULL;
319 int ret;
320
93b465c2 321 mutex_lock(&hwspinlock_tree_lock);
bd9a4c7d
OBC
322
323 /* make sure the hwspinlock is not in use (tag is set) */
324 ret = radix_tree_tag_get(&hwspinlock_tree, id, HWSPINLOCK_UNUSED);
325 if (ret == 0) {
326 pr_err("hwspinlock %d still in use (or not present)\n", id);
327 goto out;
328 }
329
330 hwlock = radix_tree_delete(&hwspinlock_tree, id);
331 if (!hwlock) {
332 pr_err("failed to delete hwspinlock %d\n", id);
333 goto out;
334 }
335
336out:
93b465c2 337 mutex_unlock(&hwspinlock_tree_lock);
bd9a4c7d
OBC
338 return hwlock;
339}
340EXPORT_SYMBOL_GPL(hwspin_lock_unregister);
341
342/**
343 * __hwspin_lock_request() - tag an hwspinlock as used and power it up
344 *
345 * This is an internal function that prepares an hwspinlock instance
346 * before it is given to the user. The function assumes that
347 * hwspinlock_tree_lock is taken.
348 *
349 * Returns 0 or positive to indicate success, and a negative value to
350 * indicate an error (with the appropriate error code)
351 */
352static int __hwspin_lock_request(struct hwspinlock *hwlock)
353{
354 struct hwspinlock *tmp;
355 int ret;
356
357 /* prevent underlying implementation from being removed */
e467b642 358 if (!try_module_get(hwlock->dev->driver->owner)) {
bd9a4c7d
OBC
359 dev_err(hwlock->dev, "%s: can't get owner\n", __func__);
360 return -EINVAL;
361 }
362
363 /* notify PM core that power is now needed */
364 ret = pm_runtime_get_sync(hwlock->dev);
365 if (ret < 0) {
366 dev_err(hwlock->dev, "%s: can't power on device\n", __func__);
367 return ret;
368 }
369
370 /* mark hwspinlock as used, should not fail */
371 tmp = radix_tree_tag_clear(&hwspinlock_tree, hwlock->id,
372 HWSPINLOCK_UNUSED);
373
374 /* self-sanity check that should never fail */
375 WARN_ON(tmp != hwlock);
376
377 return ret;
378}
379
380/**
381 * hwspin_lock_get_id() - retrieve id number of a given hwspinlock
382 * @hwlock: a valid hwspinlock instance
383 *
384 * Returns the id number of a given @hwlock, or -EINVAL if @hwlock is invalid.
385 */
386int hwspin_lock_get_id(struct hwspinlock *hwlock)
387{
388 if (!hwlock) {
389 pr_err("invalid hwlock\n");
390 return -EINVAL;
391 }
392
393 return hwlock->id;
394}
395EXPORT_SYMBOL_GPL(hwspin_lock_get_id);
396
397/**
398 * hwspin_lock_request() - request an hwspinlock
399 *
400 * This function should be called by users of the hwspinlock device,
401 * in order to dynamically assign them an unused hwspinlock.
402 * Usually the user of this lock will then have to communicate the lock's id
403 * to the remote core before it can be used for synchronization (to get the
404 * id of a given hwlock, use hwspin_lock_get_id()).
405 *
93b465c2 406 * Should be called from a process context (might sleep)
bd9a4c7d
OBC
407 *
408 * Returns the address of the assigned hwspinlock, or NULL on error
409 */
410struct hwspinlock *hwspin_lock_request(void)
411{
412 struct hwspinlock *hwlock;
413 int ret;
414
93b465c2 415 mutex_lock(&hwspinlock_tree_lock);
bd9a4c7d
OBC
416
417 /* look for an unused lock */
418 ret = radix_tree_gang_lookup_tag(&hwspinlock_tree, (void **)&hwlock,
419 0, 1, HWSPINLOCK_UNUSED);
420 if (ret == 0) {
421 pr_warn("a free hwspinlock is not available\n");
422 hwlock = NULL;
423 goto out;
424 }
425
426 /* sanity check that should never fail */
427 WARN_ON(ret > 1);
428
429 /* mark as used and power up */
430 ret = __hwspin_lock_request(hwlock);
431 if (ret < 0)
432 hwlock = NULL;
433
434out:
93b465c2 435 mutex_unlock(&hwspinlock_tree_lock);
bd9a4c7d
OBC
436 return hwlock;
437}
438EXPORT_SYMBOL_GPL(hwspin_lock_request);
439
440/**
441 * hwspin_lock_request_specific() - request for a specific hwspinlock
442 * @id: index of the specific hwspinlock that is requested
443 *
444 * This function should be called by users of the hwspinlock module,
445 * in order to assign them a specific hwspinlock.
446 * Usually early board code will be calling this function in order to
447 * reserve specific hwspinlock ids for predefined purposes.
448 *
93b465c2 449 * Should be called from a process context (might sleep)
bd9a4c7d
OBC
450 *
451 * Returns the address of the assigned hwspinlock, or NULL on error
452 */
453struct hwspinlock *hwspin_lock_request_specific(unsigned int id)
454{
455 struct hwspinlock *hwlock;
456 int ret;
457
93b465c2 458 mutex_lock(&hwspinlock_tree_lock);
bd9a4c7d
OBC
459
460 /* make sure this hwspinlock exists */
461 hwlock = radix_tree_lookup(&hwspinlock_tree, id);
462 if (!hwlock) {
463 pr_warn("hwspinlock %u does not exist\n", id);
464 goto out;
465 }
466
467 /* sanity check (this shouldn't happen) */
468 WARN_ON(hwlock->id != id);
469
470 /* make sure this hwspinlock is unused */
471 ret = radix_tree_tag_get(&hwspinlock_tree, id, HWSPINLOCK_UNUSED);
472 if (ret == 0) {
473 pr_warn("hwspinlock %u is already in use\n", id);
474 hwlock = NULL;
475 goto out;
476 }
477
478 /* mark as used and power up */
479 ret = __hwspin_lock_request(hwlock);
480 if (ret < 0)
481 hwlock = NULL;
482
483out:
93b465c2 484 mutex_unlock(&hwspinlock_tree_lock);
bd9a4c7d
OBC
485 return hwlock;
486}
487EXPORT_SYMBOL_GPL(hwspin_lock_request_specific);
488
489/**
490 * hwspin_lock_free() - free a specific hwspinlock
491 * @hwlock: the specific hwspinlock to free
492 *
493 * This function mark @hwlock as free again.
494 * Should only be called with an @hwlock that was retrieved from
495 * an earlier call to omap_hwspin_lock_request{_specific}.
496 *
93b465c2 497 * Should be called from a process context (might sleep)
bd9a4c7d
OBC
498 *
499 * Returns 0 on success, or an appropriate error code on failure
500 */
501int hwspin_lock_free(struct hwspinlock *hwlock)
502{
503 struct hwspinlock *tmp;
504 int ret;
505
506 if (!hwlock) {
507 pr_err("invalid hwlock\n");
508 return -EINVAL;
509 }
510
93b465c2 511 mutex_lock(&hwspinlock_tree_lock);
bd9a4c7d
OBC
512
513 /* make sure the hwspinlock is used */
514 ret = radix_tree_tag_get(&hwspinlock_tree, hwlock->id,
515 HWSPINLOCK_UNUSED);
516 if (ret == 1) {
517 dev_err(hwlock->dev, "%s: hwlock is already free\n", __func__);
518 dump_stack();
519 ret = -EINVAL;
520 goto out;
521 }
522
523 /* notify the underlying device that power is not needed */
524 ret = pm_runtime_put(hwlock->dev);
525 if (ret < 0)
526 goto out;
527
528 /* mark this hwspinlock as available */
529 tmp = radix_tree_tag_set(&hwspinlock_tree, hwlock->id,
530 HWSPINLOCK_UNUSED);
531
532 /* sanity check (this shouldn't happen) */
533 WARN_ON(tmp != hwlock);
534
e467b642 535 module_put(hwlock->dev->driver->owner);
bd9a4c7d
OBC
536
537out:
93b465c2 538 mutex_unlock(&hwspinlock_tree_lock);
bd9a4c7d
OBC
539 return ret;
540}
541EXPORT_SYMBOL_GPL(hwspin_lock_free);
542
543MODULE_LICENSE("GPL v2");
544MODULE_DESCRIPTION("Hardware spinlock interface");
545MODULE_AUTHOR("Ohad Ben-Cohen <ohad@wizery.com>");
This page took 0.101181 seconds and 5 git commands to generate.