hwspinlock_core.c 16 KB

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