hwspinlock_core.c 20 KB

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