hwspinlock_core.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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. /**
  336. * of_hwspin_lock_get_id_byname() - get lock id for an specified hwlock name
  337. * @np: device node from which to request the specific hwlock
  338. * @name: hwlock name
  339. *
  340. * This function provides a means for DT users of the hwspinlock module to
  341. * get the global lock id of a specific hwspinlock using the specified name of
  342. * the hwspinlock device, so that it can be requested using the normal
  343. * hwspin_lock_request_specific() API.
  344. *
  345. * Returns the global lock id number on success, -EPROBE_DEFER if the hwspinlock
  346. * device is not yet registered, -EINVAL on invalid args specifier value or an
  347. * appropriate error as returned from the OF parsing of the DT client node.
  348. */
  349. int of_hwspin_lock_get_id_byname(struct device_node *np, const char *name)
  350. {
  351. int index;
  352. if (!name)
  353. return -EINVAL;
  354. index = of_property_match_string(np, "hwlock-names", name);
  355. if (index < 0)
  356. return index;
  357. return of_hwspin_lock_get_id(np, index);
  358. }
  359. EXPORT_SYMBOL_GPL(of_hwspin_lock_get_id_byname);
  360. static int hwspin_lock_register_single(struct hwspinlock *hwlock, int id)
  361. {
  362. struct hwspinlock *tmp;
  363. int ret;
  364. mutex_lock(&hwspinlock_tree_lock);
  365. 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);
  369. goto out;
  370. }
  371. /* mark this hwspinlock as available */
  372. tmp = radix_tree_tag_set(&hwspinlock_tree, id, HWSPINLOCK_UNUSED);
  373. /* self-sanity check which should never fail */
  374. WARN_ON(tmp != hwlock);
  375. out:
  376. mutex_unlock(&hwspinlock_tree_lock);
  377. return 0;
  378. }
  379. static struct hwspinlock *hwspin_lock_unregister_single(unsigned int id)
  380. {
  381. struct hwspinlock *hwlock = NULL;
  382. int ret;
  383. mutex_lock(&hwspinlock_tree_lock);
  384. /* make sure the hwspinlock is not in use (tag is set) */
  385. ret = radix_tree_tag_get(&hwspinlock_tree, id, HWSPINLOCK_UNUSED);
  386. if (ret == 0) {
  387. pr_err("hwspinlock %d still in use (or not present)\n", id);
  388. goto out;
  389. }
  390. hwlock = radix_tree_delete(&hwspinlock_tree, id);
  391. if (!hwlock) {
  392. pr_err("failed to delete hwspinlock %d\n", id);
  393. goto out;
  394. }
  395. out:
  396. mutex_unlock(&hwspinlock_tree_lock);
  397. return hwlock;
  398. }
  399. /**
  400. * hwspin_lock_register() - register a new hw spinlock device
  401. * @bank: the hwspinlock device, which usually provides numerous hw locks
  402. * @dev: the backing device
  403. * @ops: hwspinlock handlers for this device
  404. * @base_id: id of the first hardware spinlock in this bank
  405. * @num_locks: number of hwspinlocks provided by this device
  406. *
  407. * This function should be called from the underlying platform-specific
  408. * implementation, to register a new hwspinlock device instance.
  409. *
  410. * Should be called from a process context (might sleep)
  411. *
  412. * Returns 0 on success, or an appropriate error code on failure
  413. */
  414. int hwspin_lock_register(struct hwspinlock_device *bank, struct device *dev,
  415. const struct hwspinlock_ops *ops, int base_id, int num_locks)
  416. {
  417. struct hwspinlock *hwlock;
  418. int ret = 0, i;
  419. if (!bank || !ops || !dev || !num_locks || !ops->trylock ||
  420. !ops->unlock) {
  421. pr_err("invalid parameters\n");
  422. return -EINVAL;
  423. }
  424. bank->dev = dev;
  425. bank->ops = ops;
  426. bank->base_id = base_id;
  427. bank->num_locks = num_locks;
  428. for (i = 0; i < num_locks; i++) {
  429. hwlock = &bank->lock[i];
  430. spin_lock_init(&hwlock->lock);
  431. hwlock->bank = bank;
  432. ret = hwspin_lock_register_single(hwlock, base_id + i);
  433. if (ret)
  434. goto reg_failed;
  435. }
  436. return 0;
  437. reg_failed:
  438. while (--i >= 0)
  439. hwspin_lock_unregister_single(base_id + i);
  440. return ret;
  441. }
  442. EXPORT_SYMBOL_GPL(hwspin_lock_register);
  443. /**
  444. * hwspin_lock_unregister() - unregister an hw spinlock device
  445. * @bank: the hwspinlock device, which usually provides numerous hw locks
  446. *
  447. * This function should be called from the underlying platform-specific
  448. * implementation, to unregister an existing (and unused) hwspinlock.
  449. *
  450. * Should be called from a process context (might sleep)
  451. *
  452. * Returns 0 on success, or an appropriate error code on failure
  453. */
  454. int hwspin_lock_unregister(struct hwspinlock_device *bank)
  455. {
  456. struct hwspinlock *hwlock, *tmp;
  457. int i;
  458. for (i = 0; i < bank->num_locks; i++) {
  459. hwlock = &bank->lock[i];
  460. tmp = hwspin_lock_unregister_single(bank->base_id + i);
  461. if (!tmp)
  462. return -EBUSY;
  463. /* self-sanity check that should never fail */
  464. WARN_ON(tmp != hwlock);
  465. }
  466. return 0;
  467. }
  468. EXPORT_SYMBOL_GPL(hwspin_lock_unregister);
  469. /**
  470. * __hwspin_lock_request() - tag an hwspinlock as used and power it up
  471. *
  472. * This is an internal function that prepares an hwspinlock instance
  473. * before it is given to the user. The function assumes that
  474. * hwspinlock_tree_lock is taken.
  475. *
  476. * Returns 0 or positive to indicate success, and a negative value to
  477. * indicate an error (with the appropriate error code)
  478. */
  479. static int __hwspin_lock_request(struct hwspinlock *hwlock)
  480. {
  481. struct device *dev = hwlock->bank->dev;
  482. struct hwspinlock *tmp;
  483. int ret;
  484. /* prevent underlying implementation from being removed */
  485. if (!try_module_get(dev->driver->owner)) {
  486. dev_err(dev, "%s: can't get owner\n", __func__);
  487. return -EINVAL;
  488. }
  489. /* notify PM core that power is now needed */
  490. ret = pm_runtime_get_sync(dev);
  491. if (ret < 0) {
  492. dev_err(dev, "%s: can't power on device\n", __func__);
  493. pm_runtime_put_noidle(dev);
  494. module_put(dev->driver->owner);
  495. return ret;
  496. }
  497. /* mark hwspinlock as used, should not fail */
  498. tmp = radix_tree_tag_clear(&hwspinlock_tree, hwlock_to_id(hwlock),
  499. HWSPINLOCK_UNUSED);
  500. /* self-sanity check that should never fail */
  501. WARN_ON(tmp != hwlock);
  502. return ret;
  503. }
  504. /**
  505. * hwspin_lock_get_id() - retrieve id number of a given hwspinlock
  506. * @hwlock: a valid hwspinlock instance
  507. *
  508. * Returns the id number of a given @hwlock, or -EINVAL if @hwlock is invalid.
  509. */
  510. int hwspin_lock_get_id(struct hwspinlock *hwlock)
  511. {
  512. if (!hwlock) {
  513. pr_err("invalid hwlock\n");
  514. return -EINVAL;
  515. }
  516. return hwlock_to_id(hwlock);
  517. }
  518. EXPORT_SYMBOL_GPL(hwspin_lock_get_id);
  519. /**
  520. * hwspin_lock_request() - request an hwspinlock
  521. *
  522. * This function should be called by users of the hwspinlock device,
  523. * in order to dynamically assign them an unused hwspinlock.
  524. * Usually the user of this lock will then have to communicate the lock's id
  525. * to the remote core before it can be used for synchronization (to get the
  526. * id of a given hwlock, use hwspin_lock_get_id()).
  527. *
  528. * Should be called from a process context (might sleep)
  529. *
  530. * Returns the address of the assigned hwspinlock, or NULL on error
  531. */
  532. struct hwspinlock *hwspin_lock_request(void)
  533. {
  534. struct hwspinlock *hwlock;
  535. int ret;
  536. mutex_lock(&hwspinlock_tree_lock);
  537. /* look for an unused lock */
  538. ret = radix_tree_gang_lookup_tag(&hwspinlock_tree, (void **)&hwlock,
  539. 0, 1, HWSPINLOCK_UNUSED);
  540. if (ret == 0) {
  541. pr_warn("a free hwspinlock is not available\n");
  542. hwlock = NULL;
  543. goto out;
  544. }
  545. /* sanity check that should never fail */
  546. WARN_ON(ret > 1);
  547. /* mark as used and power up */
  548. ret = __hwspin_lock_request(hwlock);
  549. if (ret < 0)
  550. hwlock = NULL;
  551. out:
  552. mutex_unlock(&hwspinlock_tree_lock);
  553. return hwlock;
  554. }
  555. EXPORT_SYMBOL_GPL(hwspin_lock_request);
  556. /**
  557. * hwspin_lock_request_specific() - request for a specific hwspinlock
  558. * @id: index of the specific hwspinlock that is requested
  559. *
  560. * This function should be called by users of the hwspinlock module,
  561. * in order to assign them a specific hwspinlock.
  562. * Usually early board code will be calling this function in order to
  563. * reserve specific hwspinlock ids for predefined purposes.
  564. *
  565. * Should be called from a process context (might sleep)
  566. *
  567. * Returns the address of the assigned hwspinlock, or NULL on error
  568. */
  569. struct hwspinlock *hwspin_lock_request_specific(unsigned int id)
  570. {
  571. struct hwspinlock *hwlock;
  572. int ret;
  573. mutex_lock(&hwspinlock_tree_lock);
  574. /* make sure this hwspinlock exists */
  575. hwlock = radix_tree_lookup(&hwspinlock_tree, id);
  576. if (!hwlock) {
  577. pr_warn("hwspinlock %u does not exist\n", id);
  578. goto out;
  579. }
  580. /* sanity check (this shouldn't happen) */
  581. WARN_ON(hwlock_to_id(hwlock) != id);
  582. /* make sure this hwspinlock is unused */
  583. ret = radix_tree_tag_get(&hwspinlock_tree, id, HWSPINLOCK_UNUSED);
  584. if (ret == 0) {
  585. pr_warn("hwspinlock %u is already in use\n", id);
  586. hwlock = NULL;
  587. goto out;
  588. }
  589. /* mark as used and power up */
  590. ret = __hwspin_lock_request(hwlock);
  591. if (ret < 0)
  592. hwlock = NULL;
  593. out:
  594. mutex_unlock(&hwspinlock_tree_lock);
  595. return hwlock;
  596. }
  597. EXPORT_SYMBOL_GPL(hwspin_lock_request_specific);
  598. /**
  599. * hwspin_lock_free() - free a specific hwspinlock
  600. * @hwlock: the specific hwspinlock to free
  601. *
  602. * This function mark @hwlock as free again.
  603. * Should only be called with an @hwlock that was retrieved from
  604. * an earlier call to omap_hwspin_lock_request{_specific}.
  605. *
  606. * Should be called from a process context (might sleep)
  607. *
  608. * Returns 0 on success, or an appropriate error code on failure
  609. */
  610. int hwspin_lock_free(struct hwspinlock *hwlock)
  611. {
  612. struct device *dev;
  613. struct hwspinlock *tmp;
  614. int ret;
  615. if (!hwlock) {
  616. pr_err("invalid hwlock\n");
  617. return -EINVAL;
  618. }
  619. dev = hwlock->bank->dev;
  620. mutex_lock(&hwspinlock_tree_lock);
  621. /* make sure the hwspinlock is used */
  622. ret = radix_tree_tag_get(&hwspinlock_tree, hwlock_to_id(hwlock),
  623. HWSPINLOCK_UNUSED);
  624. if (ret == 1) {
  625. dev_err(dev, "%s: hwlock is already free\n", __func__);
  626. dump_stack();
  627. ret = -EINVAL;
  628. goto out;
  629. }
  630. /* notify the underlying device that power is not needed */
  631. ret = pm_runtime_put(dev);
  632. if (ret < 0)
  633. goto out;
  634. /* mark this hwspinlock as available */
  635. tmp = radix_tree_tag_set(&hwspinlock_tree, hwlock_to_id(hwlock),
  636. HWSPINLOCK_UNUSED);
  637. /* sanity check (this shouldn't happen) */
  638. WARN_ON(tmp != hwlock);
  639. module_put(dev->driver->owner);
  640. out:
  641. mutex_unlock(&hwspinlock_tree_lock);
  642. return ret;
  643. }
  644. EXPORT_SYMBOL_GPL(hwspin_lock_free);
  645. MODULE_LICENSE("GPL v2");
  646. MODULE_DESCRIPTION("Hardware spinlock interface");
  647. MODULE_AUTHOR("Ohad Ben-Cohen <ohad@wizery.com>");