hwspinlock_core.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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 (hwlock->bank->dev->of_node == args.np) {
  286. ret = 0;
  287. break;
  288. }
  289. }
  290. rcu_read_unlock();
  291. if (ret < 0)
  292. goto out;
  293. id = of_hwspin_lock_simple_xlate(&args);
  294. if (id < 0 || id >= hwlock->bank->num_locks) {
  295. ret = -EINVAL;
  296. goto out;
  297. }
  298. id += hwlock->bank->base_id;
  299. out:
  300. of_node_put(args.np);
  301. return ret ? ret : id;
  302. }
  303. EXPORT_SYMBOL_GPL(of_hwspin_lock_get_id);
  304. static int hwspin_lock_register_single(struct hwspinlock *hwlock, int id)
  305. {
  306. struct hwspinlock *tmp;
  307. int ret;
  308. mutex_lock(&hwspinlock_tree_lock);
  309. ret = radix_tree_insert(&hwspinlock_tree, id, hwlock);
  310. if (ret) {
  311. if (ret == -EEXIST)
  312. pr_err("hwspinlock id %d already exists!\n", id);
  313. goto out;
  314. }
  315. /* mark this hwspinlock as available */
  316. tmp = radix_tree_tag_set(&hwspinlock_tree, id, HWSPINLOCK_UNUSED);
  317. /* self-sanity check which should never fail */
  318. WARN_ON(tmp != hwlock);
  319. out:
  320. mutex_unlock(&hwspinlock_tree_lock);
  321. return 0;
  322. }
  323. static struct hwspinlock *hwspin_lock_unregister_single(unsigned int id)
  324. {
  325. struct hwspinlock *hwlock = NULL;
  326. int ret;
  327. mutex_lock(&hwspinlock_tree_lock);
  328. /* make sure the hwspinlock is not in use (tag is set) */
  329. ret = radix_tree_tag_get(&hwspinlock_tree, id, HWSPINLOCK_UNUSED);
  330. if (ret == 0) {
  331. pr_err("hwspinlock %d still in use (or not present)\n", id);
  332. goto out;
  333. }
  334. hwlock = radix_tree_delete(&hwspinlock_tree, id);
  335. if (!hwlock) {
  336. pr_err("failed to delete hwspinlock %d\n", id);
  337. goto out;
  338. }
  339. out:
  340. mutex_unlock(&hwspinlock_tree_lock);
  341. return hwlock;
  342. }
  343. /**
  344. * hwspin_lock_register() - register a new hw spinlock device
  345. * @bank: the hwspinlock device, which usually provides numerous hw locks
  346. * @dev: the backing device
  347. * @ops: hwspinlock handlers for this device
  348. * @base_id: id of the first hardware spinlock in this bank
  349. * @num_locks: number of hwspinlocks provided by this device
  350. *
  351. * This function should be called from the underlying platform-specific
  352. * implementation, to register a new hwspinlock device instance.
  353. *
  354. * Should be called from a process context (might sleep)
  355. *
  356. * Returns 0 on success, or an appropriate error code on failure
  357. */
  358. int hwspin_lock_register(struct hwspinlock_device *bank, struct device *dev,
  359. const struct hwspinlock_ops *ops, int base_id, int num_locks)
  360. {
  361. struct hwspinlock *hwlock;
  362. int ret = 0, i;
  363. if (!bank || !ops || !dev || !num_locks || !ops->trylock ||
  364. !ops->unlock) {
  365. pr_err("invalid parameters\n");
  366. return -EINVAL;
  367. }
  368. bank->dev = dev;
  369. bank->ops = ops;
  370. bank->base_id = base_id;
  371. bank->num_locks = num_locks;
  372. for (i = 0; i < num_locks; i++) {
  373. hwlock = &bank->lock[i];
  374. spin_lock_init(&hwlock->lock);
  375. hwlock->bank = bank;
  376. ret = hwspin_lock_register_single(hwlock, base_id + i);
  377. if (ret)
  378. goto reg_failed;
  379. }
  380. return 0;
  381. reg_failed:
  382. while (--i >= 0)
  383. hwspin_lock_unregister_single(base_id + i);
  384. return ret;
  385. }
  386. EXPORT_SYMBOL_GPL(hwspin_lock_register);
  387. /**
  388. * hwspin_lock_unregister() - unregister an hw spinlock device
  389. * @bank: the hwspinlock device, which usually provides numerous hw locks
  390. *
  391. * This function should be called from the underlying platform-specific
  392. * implementation, to unregister an existing (and unused) hwspinlock.
  393. *
  394. * Should be called from a process context (might sleep)
  395. *
  396. * Returns 0 on success, or an appropriate error code on failure
  397. */
  398. int hwspin_lock_unregister(struct hwspinlock_device *bank)
  399. {
  400. struct hwspinlock *hwlock, *tmp;
  401. int i;
  402. for (i = 0; i < bank->num_locks; i++) {
  403. hwlock = &bank->lock[i];
  404. tmp = hwspin_lock_unregister_single(bank->base_id + i);
  405. if (!tmp)
  406. return -EBUSY;
  407. /* self-sanity check that should never fail */
  408. WARN_ON(tmp != hwlock);
  409. }
  410. return 0;
  411. }
  412. EXPORT_SYMBOL_GPL(hwspin_lock_unregister);
  413. /**
  414. * __hwspin_lock_request() - tag an hwspinlock as used and power it up
  415. *
  416. * This is an internal function that prepares an hwspinlock instance
  417. * before it is given to the user. The function assumes that
  418. * hwspinlock_tree_lock is taken.
  419. *
  420. * Returns 0 or positive to indicate success, and a negative value to
  421. * indicate an error (with the appropriate error code)
  422. */
  423. static int __hwspin_lock_request(struct hwspinlock *hwlock)
  424. {
  425. struct device *dev = hwlock->bank->dev;
  426. struct hwspinlock *tmp;
  427. int ret;
  428. /* prevent underlying implementation from being removed */
  429. if (!try_module_get(dev->driver->owner)) {
  430. dev_err(dev, "%s: can't get owner\n", __func__);
  431. return -EINVAL;
  432. }
  433. /* notify PM core that power is now needed */
  434. ret = pm_runtime_get_sync(dev);
  435. if (ret < 0) {
  436. dev_err(dev, "%s: can't power on device\n", __func__);
  437. pm_runtime_put_noidle(dev);
  438. module_put(dev->driver->owner);
  439. return ret;
  440. }
  441. /* mark hwspinlock as used, should not fail */
  442. tmp = radix_tree_tag_clear(&hwspinlock_tree, hwlock_to_id(hwlock),
  443. HWSPINLOCK_UNUSED);
  444. /* self-sanity check that should never fail */
  445. WARN_ON(tmp != hwlock);
  446. return ret;
  447. }
  448. /**
  449. * hwspin_lock_get_id() - retrieve id number of a given hwspinlock
  450. * @hwlock: a valid hwspinlock instance
  451. *
  452. * Returns the id number of a given @hwlock, or -EINVAL if @hwlock is invalid.
  453. */
  454. int hwspin_lock_get_id(struct hwspinlock *hwlock)
  455. {
  456. if (!hwlock) {
  457. pr_err("invalid hwlock\n");
  458. return -EINVAL;
  459. }
  460. return hwlock_to_id(hwlock);
  461. }
  462. EXPORT_SYMBOL_GPL(hwspin_lock_get_id);
  463. /**
  464. * hwspin_lock_request() - request an hwspinlock
  465. *
  466. * This function should be called by users of the hwspinlock device,
  467. * in order to dynamically assign them an unused hwspinlock.
  468. * Usually the user of this lock will then have to communicate the lock's id
  469. * to the remote core before it can be used for synchronization (to get the
  470. * id of a given hwlock, use hwspin_lock_get_id()).
  471. *
  472. * Should be called from a process context (might sleep)
  473. *
  474. * Returns the address of the assigned hwspinlock, or NULL on error
  475. */
  476. struct hwspinlock *hwspin_lock_request(void)
  477. {
  478. struct hwspinlock *hwlock;
  479. int ret;
  480. mutex_lock(&hwspinlock_tree_lock);
  481. /* look for an unused lock */
  482. ret = radix_tree_gang_lookup_tag(&hwspinlock_tree, (void **)&hwlock,
  483. 0, 1, HWSPINLOCK_UNUSED);
  484. if (ret == 0) {
  485. pr_warn("a free hwspinlock is not available\n");
  486. hwlock = NULL;
  487. goto out;
  488. }
  489. /* sanity check that should never fail */
  490. WARN_ON(ret > 1);
  491. /* mark as used and power up */
  492. ret = __hwspin_lock_request(hwlock);
  493. if (ret < 0)
  494. hwlock = NULL;
  495. out:
  496. mutex_unlock(&hwspinlock_tree_lock);
  497. return hwlock;
  498. }
  499. EXPORT_SYMBOL_GPL(hwspin_lock_request);
  500. /**
  501. * hwspin_lock_request_specific() - request for a specific hwspinlock
  502. * @id: index of the specific hwspinlock that is requested
  503. *
  504. * This function should be called by users of the hwspinlock module,
  505. * in order to assign them a specific hwspinlock.
  506. * Usually early board code will be calling this function in order to
  507. * reserve specific hwspinlock ids for predefined purposes.
  508. *
  509. * Should be called from a process context (might sleep)
  510. *
  511. * Returns the address of the assigned hwspinlock, or NULL on error
  512. */
  513. struct hwspinlock *hwspin_lock_request_specific(unsigned int id)
  514. {
  515. struct hwspinlock *hwlock;
  516. int ret;
  517. mutex_lock(&hwspinlock_tree_lock);
  518. /* make sure this hwspinlock exists */
  519. hwlock = radix_tree_lookup(&hwspinlock_tree, id);
  520. if (!hwlock) {
  521. pr_warn("hwspinlock %u does not exist\n", id);
  522. goto out;
  523. }
  524. /* sanity check (this shouldn't happen) */
  525. WARN_ON(hwlock_to_id(hwlock) != id);
  526. /* make sure this hwspinlock is unused */
  527. ret = radix_tree_tag_get(&hwspinlock_tree, id, HWSPINLOCK_UNUSED);
  528. if (ret == 0) {
  529. pr_warn("hwspinlock %u is already in use\n", id);
  530. hwlock = NULL;
  531. goto out;
  532. }
  533. /* mark as used and power up */
  534. ret = __hwspin_lock_request(hwlock);
  535. if (ret < 0)
  536. hwlock = NULL;
  537. out:
  538. mutex_unlock(&hwspinlock_tree_lock);
  539. return hwlock;
  540. }
  541. EXPORT_SYMBOL_GPL(hwspin_lock_request_specific);
  542. /**
  543. * hwspin_lock_free() - free a specific hwspinlock
  544. * @hwlock: the specific hwspinlock to free
  545. *
  546. * This function mark @hwlock as free again.
  547. * Should only be called with an @hwlock that was retrieved from
  548. * an earlier call to omap_hwspin_lock_request{_specific}.
  549. *
  550. * Should be called from a process context (might sleep)
  551. *
  552. * Returns 0 on success, or an appropriate error code on failure
  553. */
  554. int hwspin_lock_free(struct hwspinlock *hwlock)
  555. {
  556. struct device *dev;
  557. struct hwspinlock *tmp;
  558. int ret;
  559. if (!hwlock) {
  560. pr_err("invalid hwlock\n");
  561. return -EINVAL;
  562. }
  563. dev = hwlock->bank->dev;
  564. mutex_lock(&hwspinlock_tree_lock);
  565. /* make sure the hwspinlock is used */
  566. ret = radix_tree_tag_get(&hwspinlock_tree, hwlock_to_id(hwlock),
  567. HWSPINLOCK_UNUSED);
  568. if (ret == 1) {
  569. dev_err(dev, "%s: hwlock is already free\n", __func__);
  570. dump_stack();
  571. ret = -EINVAL;
  572. goto out;
  573. }
  574. /* notify the underlying device that power is not needed */
  575. ret = pm_runtime_put(dev);
  576. if (ret < 0)
  577. goto out;
  578. /* mark this hwspinlock as available */
  579. tmp = radix_tree_tag_set(&hwspinlock_tree, hwlock_to_id(hwlock),
  580. HWSPINLOCK_UNUSED);
  581. /* sanity check (this shouldn't happen) */
  582. WARN_ON(tmp != hwlock);
  583. module_put(dev->driver->owner);
  584. out:
  585. mutex_unlock(&hwspinlock_tree_lock);
  586. return ret;
  587. }
  588. EXPORT_SYMBOL_GPL(hwspin_lock_free);
  589. MODULE_LICENSE("GPL v2");
  590. MODULE_DESCRIPTION("Hardware spinlock interface");
  591. MODULE_AUTHOR("Ohad Ben-Cohen <ohad@wizery.com>");