hwspinlock_core.c 20 KB

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