blob: f4a59f5631e47921493130bd47b1b8e8c172ced0 [file] [log] [blame]
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -08001/*
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>
Juan Gutierrez93b465c2011-09-06 09:30:16 +030029#include <linux/mutex.h>
Suman Annafb7737e2015-03-04 20:01:14 -060030#include <linux/of.h>
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -080031
32#include "hwspinlock_internal.h"
33
34/* radix tree tags */
35#define HWSPINLOCK_UNUSED (0) /* tags an hwspinlock as unused */
36
37/*
38 * A radix tree is used to maintain the available hwspinlock instances.
39 * The tree associates hwspinlock pointers with their integer key id,
40 * and provides easy-to-use API which makes the hwspinlock core code simple
41 * and easy to read.
42 *
43 * Radix trees are quick on lookups, and reasonably efficient in terms of
44 * storage, especially with high density usages such as this framework
45 * requires (a continuous range of integer keys, beginning with zero, is
46 * used as the ID's of the hwspinlock instances).
47 *
48 * The radix tree API supports tagging items in the tree, which this
49 * framework uses to mark unused hwspinlock instances (see the
50 * HWSPINLOCK_UNUSED tag above). As a result, the process of querying the
51 * tree, looking for an unused hwspinlock instance, is now reduced to a
52 * single radix tree API call.
53 */
54static RADIX_TREE(hwspinlock_tree, GFP_KERNEL);
55
56/*
Juan Gutierrez93b465c2011-09-06 09:30:16 +030057 * Synchronization of access to the tree is achieved using this mutex,
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -080058 * as the radix-tree API requires that users provide all synchronisation.
Juan Gutierrez93b465c2011-09-06 09:30:16 +030059 * A mutex is needed because we're using non-atomic radix tree allocations.
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -080060 */
Juan Gutierrez93b465c2011-09-06 09:30:16 +030061static DEFINE_MUTEX(hwspinlock_tree_lock);
62
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -080063
64/**
65 * __hwspin_trylock() - attempt to lock a specific hwspinlock
66 * @hwlock: an hwspinlock which we want to trylock
67 * @mode: controls whether local interrupts are disabled or not
68 * @flags: a pointer where the caller's interrupt state will be saved at (if
69 * requested)
70 *
71 * This function attempts to lock an hwspinlock, and will immediately
72 * fail if the hwspinlock is already taken.
73 *
74 * Upon a successful return from this function, preemption (and possibly
75 * interrupts) is disabled, so the caller must not sleep, and is advised to
76 * release the hwspinlock as soon as possible. This is required in order to
77 * minimize remote cores polling on the hardware interconnect.
78 *
79 * The user decides whether local interrupts are disabled or not, and if yes,
80 * whether he wants their previous state to be saved. It is up to the user
81 * to choose the appropriate @mode of operation, exactly the same way users
82 * should decide between spin_trylock, spin_trylock_irq and
83 * spin_trylock_irqsave.
84 *
85 * Returns 0 if we successfully locked the hwspinlock or -EBUSY if
86 * the hwspinlock was already taken.
87 * This function will never sleep.
88 */
89int __hwspin_trylock(struct hwspinlock *hwlock, int mode, unsigned long *flags)
90{
91 int ret;
92
93 BUG_ON(!hwlock);
94 BUG_ON(!flags && mode == HWLOCK_IRQSTATE);
95
96 /*
97 * This spin_lock{_irq, _irqsave} serves three purposes:
98 *
99 * 1. Disable preemption, in order to minimize the period of time
100 * in which the hwspinlock is taken. This is important in order
101 * to minimize the possible polling on the hardware interconnect
102 * by a remote user of this lock.
103 * 2. Make the hwspinlock SMP-safe (so we can take it from
104 * additional contexts on the local host).
105 * 3. Ensure that in_atomic/might_sleep checks catch potential
106 * problems with hwspinlock usage (e.g. scheduler checks like
107 * 'scheduling while atomic' etc.)
108 */
Baolin Wang66742b12018-04-08 11:06:56 +0800109 switch (mode) {
110 case HWLOCK_IRQSTATE:
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800111 ret = spin_trylock_irqsave(&hwlock->lock, *flags);
Baolin Wang66742b12018-04-08 11:06:56 +0800112 break;
113 case HWLOCK_IRQ:
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800114 ret = spin_trylock_irq(&hwlock->lock);
Baolin Wang66742b12018-04-08 11:06:56 +0800115 break;
116 default:
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800117 ret = spin_trylock(&hwlock->lock);
Baolin Wang66742b12018-04-08 11:06:56 +0800118 break;
119 }
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800120
121 /* is lock already taken by another context on the local cpu ? */
122 if (!ret)
123 return -EBUSY;
124
125 /* try to take the hwspinlock device */
Ohad Ben-Cohen300bab92011-09-06 15:39:21 +0300126 ret = hwlock->bank->ops->trylock(hwlock);
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800127
128 /* if hwlock is already taken, undo spin_trylock_* and exit */
129 if (!ret) {
Baolin Wang66742b12018-04-08 11:06:56 +0800130 switch (mode) {
131 case HWLOCK_IRQSTATE:
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800132 spin_unlock_irqrestore(&hwlock->lock, *flags);
Baolin Wang66742b12018-04-08 11:06:56 +0800133 break;
134 case HWLOCK_IRQ:
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800135 spin_unlock_irq(&hwlock->lock);
Baolin Wang66742b12018-04-08 11:06:56 +0800136 break;
137 default:
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800138 spin_unlock(&hwlock->lock);
Baolin Wang66742b12018-04-08 11:06:56 +0800139 break;
140 }
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800141
142 return -EBUSY;
143 }
144
145 /*
146 * We can be sure the other core's memory operations
147 * are observable to us only _after_ we successfully take
148 * the hwspinlock, and we must make sure that subsequent memory
149 * operations (both reads and writes) will not be reordered before
150 * we actually took the hwspinlock.
151 *
152 * Note: the implicit memory barrier of the spinlock above is too
153 * early, so we need this additional explicit memory barrier.
154 */
155 mb();
156
157 return 0;
158}
159EXPORT_SYMBOL_GPL(__hwspin_trylock);
160
161/**
162 * __hwspin_lock_timeout() - lock an hwspinlock with timeout limit
163 * @hwlock: the hwspinlock to be locked
164 * @timeout: timeout value in msecs
165 * @mode: mode which controls whether local interrupts are disabled or not
166 * @flags: a pointer to where the caller's interrupt state will be saved at (if
167 * requested)
168 *
169 * This function locks the given @hwlock. If the @hwlock
170 * is already taken, the function will busy loop waiting for it to
171 * be released, but give up after @timeout msecs have elapsed.
172 *
173 * Upon a successful return from this function, preemption is disabled
174 * (and possibly local interrupts, too), so the caller must not sleep,
175 * and is advised to release the hwspinlock as soon as possible.
176 * This is required in order to minimize remote cores polling on the
177 * hardware interconnect.
178 *
179 * The user decides whether local interrupts are disabled or not, and if yes,
180 * whether he wants their previous state to be saved. It is up to the user
181 * to choose the appropriate @mode of operation, exactly the same way users
182 * should decide between spin_lock, spin_lock_irq and spin_lock_irqsave.
183 *
184 * Returns 0 when the @hwlock was successfully taken, and an appropriate
185 * error code otherwise (most notably -ETIMEDOUT if the @hwlock is still
186 * busy after @timeout msecs). The function will never sleep.
187 */
188int __hwspin_lock_timeout(struct hwspinlock *hwlock, unsigned int to,
189 int mode, unsigned long *flags)
190{
191 int ret;
192 unsigned long expire;
193
194 expire = msecs_to_jiffies(to) + jiffies;
195
196 for (;;) {
197 /* Try to take the hwspinlock */
198 ret = __hwspin_trylock(hwlock, mode, flags);
199 if (ret != -EBUSY)
200 break;
201
202 /*
203 * The lock is already taken, let's check if the user wants
204 * us to try again
205 */
206 if (time_is_before_eq_jiffies(expire))
207 return -ETIMEDOUT;
208
209 /*
210 * Allow platform-specific relax handlers to prevent
211 * hogging the interconnect (no sleeping, though)
212 */
Ohad Ben-Cohen300bab92011-09-06 15:39:21 +0300213 if (hwlock->bank->ops->relax)
214 hwlock->bank->ops->relax(hwlock);
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800215 }
216
217 return ret;
218}
219EXPORT_SYMBOL_GPL(__hwspin_lock_timeout);
220
221/**
222 * __hwspin_unlock() - unlock a specific hwspinlock
223 * @hwlock: a previously-acquired hwspinlock which we want to unlock
224 * @mode: controls whether local interrupts needs to be restored or not
225 * @flags: previous caller's interrupt state to restore (if requested)
226 *
227 * This function will unlock a specific hwspinlock, enable preemption and
228 * (possibly) enable interrupts or restore their previous state.
229 * @hwlock must be already locked before calling this function: it is a bug
230 * to call unlock on a @hwlock that is already unlocked.
231 *
232 * The user decides whether local interrupts should be enabled or not, and
233 * if yes, whether he wants their previous state to be restored. It is up
234 * to the user to choose the appropriate @mode of operation, exactly the
235 * same way users decide between spin_unlock, spin_unlock_irq and
236 * spin_unlock_irqrestore.
237 *
238 * The function will never sleep.
239 */
240void __hwspin_unlock(struct hwspinlock *hwlock, int mode, unsigned long *flags)
241{
242 BUG_ON(!hwlock);
243 BUG_ON(!flags && mode == HWLOCK_IRQSTATE);
244
245 /*
246 * We must make sure that memory operations (both reads and writes),
247 * done before unlocking the hwspinlock, will not be reordered
248 * after the lock is released.
249 *
250 * That's the purpose of this explicit memory barrier.
251 *
252 * Note: the memory barrier induced by the spin_unlock below is too
253 * late; the other core is going to access memory soon after it will
254 * take the hwspinlock, and by then we want to be sure our memory
255 * operations are already observable.
256 */
257 mb();
258
Ohad Ben-Cohen300bab92011-09-06 15:39:21 +0300259 hwlock->bank->ops->unlock(hwlock);
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800260
261 /* Undo the spin_trylock{_irq, _irqsave} called while locking */
Baolin Wang66742b12018-04-08 11:06:56 +0800262 switch (mode) {
263 case HWLOCK_IRQSTATE:
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800264 spin_unlock_irqrestore(&hwlock->lock, *flags);
Baolin Wang66742b12018-04-08 11:06:56 +0800265 break;
266 case HWLOCK_IRQ:
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800267 spin_unlock_irq(&hwlock->lock);
Baolin Wang66742b12018-04-08 11:06:56 +0800268 break;
269 default:
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800270 spin_unlock(&hwlock->lock);
Baolin Wang66742b12018-04-08 11:06:56 +0800271 break;
272 }
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800273}
274EXPORT_SYMBOL_GPL(__hwspin_unlock);
275
Suman Annafb7737e2015-03-04 20:01:14 -0600276/**
277 * of_hwspin_lock_simple_xlate - translate hwlock_spec to return a lock id
278 * @bank: the hwspinlock device bank
279 * @hwlock_spec: hwlock specifier as found in the device tree
280 *
281 * This is a simple translation function, suitable for hwspinlock platform
282 * drivers that only has a lock specifier length of 1.
283 *
284 * Returns a relative index of the lock within a specified bank on success,
285 * or -EINVAL on invalid specifier cell count.
286 */
287static inline int
288of_hwspin_lock_simple_xlate(const struct of_phandle_args *hwlock_spec)
289{
290 if (WARN_ON(hwlock_spec->args_count != 1))
291 return -EINVAL;
292
293 return hwlock_spec->args[0];
294}
295
296/**
297 * of_hwspin_lock_get_id() - get lock id for an OF phandle-based specific lock
298 * @np: device node from which to request the specific hwlock
299 * @index: index of the hwlock in the list of values
300 *
301 * This function provides a means for DT users of the hwspinlock module to
302 * get the global lock id of a specific hwspinlock using the phandle of the
303 * hwspinlock device, so that it can be requested using the normal
304 * hwspin_lock_request_specific() API.
305 *
306 * Returns the global lock id number on success, -EPROBE_DEFER if the hwspinlock
307 * device is not yet registered, -EINVAL on invalid args specifier value or an
308 * appropriate error as returned from the OF parsing of the DT client node.
309 */
310int of_hwspin_lock_get_id(struct device_node *np, int index)
311{
312 struct of_phandle_args args;
313 struct hwspinlock *hwlock;
314 struct radix_tree_iter iter;
315 void **slot;
316 int id;
317 int ret;
318
319 ret = of_parse_phandle_with_args(np, "hwlocks", "#hwlock-cells", index,
320 &args);
321 if (ret)
322 return ret;
323
324 /* Find the hwspinlock device: we need its base_id */
325 ret = -EPROBE_DEFER;
326 rcu_read_lock();
327 radix_tree_for_each_slot(slot, &hwspinlock_tree, &iter, 0) {
328 hwlock = radix_tree_deref_slot(slot);
329 if (unlikely(!hwlock))
330 continue;
Matthew Wilcoxb76ba4a2016-05-20 17:03:01 -0700331 if (radix_tree_deref_retry(hwlock)) {
Matthew Wilcoxc6400ba2016-02-02 16:57:55 -0800332 slot = radix_tree_iter_retry(&iter);
333 continue;
334 }
Suman Annafb7737e2015-03-04 20:01:14 -0600335
336 if (hwlock->bank->dev->of_node == args.np) {
337 ret = 0;
338 break;
339 }
340 }
341 rcu_read_unlock();
342 if (ret < 0)
343 goto out;
344
345 id = of_hwspin_lock_simple_xlate(&args);
346 if (id < 0 || id >= hwlock->bank->num_locks) {
347 ret = -EINVAL;
348 goto out;
349 }
350 id += hwlock->bank->base_id;
351
352out:
353 of_node_put(args.np);
354 return ret ? ret : id;
355}
356EXPORT_SYMBOL_GPL(of_hwspin_lock_get_id);
357
Ohad Ben-Cohen300bab92011-09-06 15:39:21 +0300358static int hwspin_lock_register_single(struct hwspinlock *hwlock, int id)
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800359{
360 struct hwspinlock *tmp;
361 int ret;
362
Juan Gutierrez93b465c2011-09-06 09:30:16 +0300363 mutex_lock(&hwspinlock_tree_lock);
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800364
Ohad Ben-Cohen300bab92011-09-06 15:39:21 +0300365 ret = radix_tree_insert(&hwspinlock_tree, id, hwlock);
366 if (ret) {
367 if (ret == -EEXIST)
368 pr_err("hwspinlock id %d already exists!\n", id);
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800369 goto out;
Ohad Ben-Cohen300bab92011-09-06 15:39:21 +0300370 }
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800371
372 /* mark this hwspinlock as available */
Ohad Ben-Cohen300bab92011-09-06 15:39:21 +0300373 tmp = radix_tree_tag_set(&hwspinlock_tree, id, HWSPINLOCK_UNUSED);
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800374
375 /* self-sanity check which should never fail */
376 WARN_ON(tmp != hwlock);
377
378out:
Juan Gutierrez93b465c2011-09-06 09:30:16 +0300379 mutex_unlock(&hwspinlock_tree_lock);
Ohad Ben-Cohen300bab92011-09-06 15:39:21 +0300380 return 0;
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800381}
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800382
Ohad Ben-Cohen300bab92011-09-06 15:39:21 +0300383static struct hwspinlock *hwspin_lock_unregister_single(unsigned int id)
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800384{
385 struct hwspinlock *hwlock = NULL;
386 int ret;
387
Juan Gutierrez93b465c2011-09-06 09:30:16 +0300388 mutex_lock(&hwspinlock_tree_lock);
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800389
390 /* make sure the hwspinlock is not in use (tag is set) */
391 ret = radix_tree_tag_get(&hwspinlock_tree, id, HWSPINLOCK_UNUSED);
392 if (ret == 0) {
393 pr_err("hwspinlock %d still in use (or not present)\n", id);
394 goto out;
395 }
396
397 hwlock = radix_tree_delete(&hwspinlock_tree, id);
398 if (!hwlock) {
399 pr_err("failed to delete hwspinlock %d\n", id);
400 goto out;
401 }
402
403out:
Juan Gutierrez93b465c2011-09-06 09:30:16 +0300404 mutex_unlock(&hwspinlock_tree_lock);
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800405 return hwlock;
406}
Ohad Ben-Cohen300bab92011-09-06 15:39:21 +0300407
408/**
409 * hwspin_lock_register() - register a new hw spinlock device
410 * @bank: the hwspinlock device, which usually provides numerous hw locks
411 * @dev: the backing device
412 * @ops: hwspinlock handlers for this device
413 * @base_id: id of the first hardware spinlock in this bank
414 * @num_locks: number of hwspinlocks provided by this device
415 *
416 * This function should be called from the underlying platform-specific
417 * implementation, to register a new hwspinlock device instance.
418 *
419 * Should be called from a process context (might sleep)
420 *
421 * Returns 0 on success, or an appropriate error code on failure
422 */
423int hwspin_lock_register(struct hwspinlock_device *bank, struct device *dev,
424 const struct hwspinlock_ops *ops, int base_id, int num_locks)
425{
426 struct hwspinlock *hwlock;
427 int ret = 0, i;
428
429 if (!bank || !ops || !dev || !num_locks || !ops->trylock ||
430 !ops->unlock) {
431 pr_err("invalid parameters\n");
432 return -EINVAL;
433 }
434
435 bank->dev = dev;
436 bank->ops = ops;
437 bank->base_id = base_id;
438 bank->num_locks = num_locks;
439
440 for (i = 0; i < num_locks; i++) {
441 hwlock = &bank->lock[i];
442
443 spin_lock_init(&hwlock->lock);
444 hwlock->bank = bank;
445
Shinya Kuribayashi476a7eeb2012-07-07 13:37:42 +0300446 ret = hwspin_lock_register_single(hwlock, base_id + i);
Ohad Ben-Cohen300bab92011-09-06 15:39:21 +0300447 if (ret)
448 goto reg_failed;
449 }
450
451 return 0;
452
453reg_failed:
454 while (--i >= 0)
Shinya Kuribayashi476a7eeb2012-07-07 13:37:42 +0300455 hwspin_lock_unregister_single(base_id + i);
Ohad Ben-Cohen300bab92011-09-06 15:39:21 +0300456 return ret;
457}
458EXPORT_SYMBOL_GPL(hwspin_lock_register);
459
460/**
461 * hwspin_lock_unregister() - unregister an hw spinlock device
462 * @bank: the hwspinlock device, which usually provides numerous hw locks
463 *
464 * This function should be called from the underlying platform-specific
465 * implementation, to unregister an existing (and unused) hwspinlock.
466 *
467 * Should be called from a process context (might sleep)
468 *
469 * Returns 0 on success, or an appropriate error code on failure
470 */
471int hwspin_lock_unregister(struct hwspinlock_device *bank)
472{
473 struct hwspinlock *hwlock, *tmp;
474 int i;
475
476 for (i = 0; i < bank->num_locks; i++) {
477 hwlock = &bank->lock[i];
478
479 tmp = hwspin_lock_unregister_single(bank->base_id + i);
480 if (!tmp)
481 return -EBUSY;
482
483 /* self-sanity check that should never fail */
484 WARN_ON(tmp != hwlock);
485 }
486
487 return 0;
488}
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800489EXPORT_SYMBOL_GPL(hwspin_lock_unregister);
490
491/**
492 * __hwspin_lock_request() - tag an hwspinlock as used and power it up
493 *
494 * This is an internal function that prepares an hwspinlock instance
495 * before it is given to the user. The function assumes that
496 * hwspinlock_tree_lock is taken.
497 *
498 * Returns 0 or positive to indicate success, and a negative value to
499 * indicate an error (with the appropriate error code)
500 */
501static int __hwspin_lock_request(struct hwspinlock *hwlock)
502{
Ohad Ben-Cohen300bab92011-09-06 15:39:21 +0300503 struct device *dev = hwlock->bank->dev;
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800504 struct hwspinlock *tmp;
505 int ret;
506
507 /* prevent underlying implementation from being removed */
Ohad Ben-Cohen300bab92011-09-06 15:39:21 +0300508 if (!try_module_get(dev->driver->owner)) {
509 dev_err(dev, "%s: can't get owner\n", __func__);
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800510 return -EINVAL;
511 }
512
513 /* notify PM core that power is now needed */
Ohad Ben-Cohen300bab92011-09-06 15:39:21 +0300514 ret = pm_runtime_get_sync(dev);
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800515 if (ret < 0) {
Ohad Ben-Cohen300bab92011-09-06 15:39:21 +0300516 dev_err(dev, "%s: can't power on device\n", __func__);
Li Feic10b90d2013-04-05 21:20:36 +0800517 pm_runtime_put_noidle(dev);
518 module_put(dev->driver->owner);
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800519 return ret;
520 }
521
522 /* mark hwspinlock as used, should not fail */
Ohad Ben-Cohen300bab92011-09-06 15:39:21 +0300523 tmp = radix_tree_tag_clear(&hwspinlock_tree, hwlock_to_id(hwlock),
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800524 HWSPINLOCK_UNUSED);
525
526 /* self-sanity check that should never fail */
527 WARN_ON(tmp != hwlock);
528
529 return ret;
530}
531
532/**
533 * hwspin_lock_get_id() - retrieve id number of a given hwspinlock
534 * @hwlock: a valid hwspinlock instance
535 *
536 * Returns the id number of a given @hwlock, or -EINVAL if @hwlock is invalid.
537 */
538int hwspin_lock_get_id(struct hwspinlock *hwlock)
539{
540 if (!hwlock) {
541 pr_err("invalid hwlock\n");
542 return -EINVAL;
543 }
544
Ohad Ben-Cohen300bab92011-09-06 15:39:21 +0300545 return hwlock_to_id(hwlock);
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800546}
547EXPORT_SYMBOL_GPL(hwspin_lock_get_id);
548
549/**
550 * hwspin_lock_request() - request an hwspinlock
551 *
552 * This function should be called by users of the hwspinlock device,
553 * in order to dynamically assign them an unused hwspinlock.
554 * Usually the user of this lock will then have to communicate the lock's id
555 * to the remote core before it can be used for synchronization (to get the
556 * id of a given hwlock, use hwspin_lock_get_id()).
557 *
Juan Gutierrez93b465c2011-09-06 09:30:16 +0300558 * Should be called from a process context (might sleep)
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800559 *
560 * Returns the address of the assigned hwspinlock, or NULL on error
561 */
562struct hwspinlock *hwspin_lock_request(void)
563{
564 struct hwspinlock *hwlock;
565 int ret;
566
Juan Gutierrez93b465c2011-09-06 09:30:16 +0300567 mutex_lock(&hwspinlock_tree_lock);
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800568
569 /* look for an unused lock */
570 ret = radix_tree_gang_lookup_tag(&hwspinlock_tree, (void **)&hwlock,
571 0, 1, HWSPINLOCK_UNUSED);
572 if (ret == 0) {
573 pr_warn("a free hwspinlock is not available\n");
574 hwlock = NULL;
575 goto out;
576 }
577
578 /* sanity check that should never fail */
579 WARN_ON(ret > 1);
580
581 /* mark as used and power up */
582 ret = __hwspin_lock_request(hwlock);
583 if (ret < 0)
584 hwlock = NULL;
585
586out:
Juan Gutierrez93b465c2011-09-06 09:30:16 +0300587 mutex_unlock(&hwspinlock_tree_lock);
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800588 return hwlock;
589}
590EXPORT_SYMBOL_GPL(hwspin_lock_request);
591
592/**
593 * hwspin_lock_request_specific() - request for a specific hwspinlock
594 * @id: index of the specific hwspinlock that is requested
595 *
596 * This function should be called by users of the hwspinlock module,
597 * in order to assign them a specific hwspinlock.
598 * Usually early board code will be calling this function in order to
599 * reserve specific hwspinlock ids for predefined purposes.
600 *
Juan Gutierrez93b465c2011-09-06 09:30:16 +0300601 * Should be called from a process context (might sleep)
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800602 *
603 * Returns the address of the assigned hwspinlock, or NULL on error
604 */
605struct hwspinlock *hwspin_lock_request_specific(unsigned int id)
606{
607 struct hwspinlock *hwlock;
608 int ret;
609
Juan Gutierrez93b465c2011-09-06 09:30:16 +0300610 mutex_lock(&hwspinlock_tree_lock);
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800611
612 /* make sure this hwspinlock exists */
613 hwlock = radix_tree_lookup(&hwspinlock_tree, id);
614 if (!hwlock) {
615 pr_warn("hwspinlock %u does not exist\n", id);
616 goto out;
617 }
618
619 /* sanity check (this shouldn't happen) */
Ohad Ben-Cohen300bab92011-09-06 15:39:21 +0300620 WARN_ON(hwlock_to_id(hwlock) != id);
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800621
622 /* make sure this hwspinlock is unused */
623 ret = radix_tree_tag_get(&hwspinlock_tree, id, HWSPINLOCK_UNUSED);
624 if (ret == 0) {
625 pr_warn("hwspinlock %u is already in use\n", id);
626 hwlock = NULL;
627 goto out;
628 }
629
630 /* mark as used and power up */
631 ret = __hwspin_lock_request(hwlock);
632 if (ret < 0)
633 hwlock = NULL;
634
635out:
Juan Gutierrez93b465c2011-09-06 09:30:16 +0300636 mutex_unlock(&hwspinlock_tree_lock);
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800637 return hwlock;
638}
639EXPORT_SYMBOL_GPL(hwspin_lock_request_specific);
640
641/**
642 * hwspin_lock_free() - free a specific hwspinlock
643 * @hwlock: the specific hwspinlock to free
644 *
645 * This function mark @hwlock as free again.
646 * Should only be called with an @hwlock that was retrieved from
647 * an earlier call to omap_hwspin_lock_request{_specific}.
648 *
Juan Gutierrez93b465c2011-09-06 09:30:16 +0300649 * Should be called from a process context (might sleep)
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800650 *
651 * Returns 0 on success, or an appropriate error code on failure
652 */
653int hwspin_lock_free(struct hwspinlock *hwlock)
654{
Wei Yongjune3526142012-09-10 12:52:02 +0800655 struct device *dev;
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800656 struct hwspinlock *tmp;
657 int ret;
658
659 if (!hwlock) {
660 pr_err("invalid hwlock\n");
661 return -EINVAL;
662 }
663
Wei Yongjune3526142012-09-10 12:52:02 +0800664 dev = hwlock->bank->dev;
Juan Gutierrez93b465c2011-09-06 09:30:16 +0300665 mutex_lock(&hwspinlock_tree_lock);
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800666
667 /* make sure the hwspinlock is used */
Ohad Ben-Cohen300bab92011-09-06 15:39:21 +0300668 ret = radix_tree_tag_get(&hwspinlock_tree, hwlock_to_id(hwlock),
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800669 HWSPINLOCK_UNUSED);
670 if (ret == 1) {
Ohad Ben-Cohen300bab92011-09-06 15:39:21 +0300671 dev_err(dev, "%s: hwlock is already free\n", __func__);
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800672 dump_stack();
673 ret = -EINVAL;
674 goto out;
675 }
676
677 /* notify the underlying device that power is not needed */
Ohad Ben-Cohen300bab92011-09-06 15:39:21 +0300678 ret = pm_runtime_put(dev);
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800679 if (ret < 0)
680 goto out;
681
682 /* mark this hwspinlock as available */
Ohad Ben-Cohen300bab92011-09-06 15:39:21 +0300683 tmp = radix_tree_tag_set(&hwspinlock_tree, hwlock_to_id(hwlock),
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800684 HWSPINLOCK_UNUSED);
685
686 /* sanity check (this shouldn't happen) */
687 WARN_ON(tmp != hwlock);
688
Ohad Ben-Cohen300bab92011-09-06 15:39:21 +0300689 module_put(dev->driver->owner);
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800690
691out:
Juan Gutierrez93b465c2011-09-06 09:30:16 +0300692 mutex_unlock(&hwspinlock_tree_lock);
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800693 return ret;
694}
695EXPORT_SYMBOL_GPL(hwspin_lock_free);
696
697MODULE_LICENSE("GPL v2");
698MODULE_DESCRIPTION("Hardware spinlock interface");
699MODULE_AUTHOR("Ohad Ben-Cohen <ohad@wizery.com>");