hwspinlock_core.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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->bank->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->bank->ops->relax)
  182. hwlock->bank->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->bank->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. static int hwspin_lock_register_single(struct hwspinlock *hwlock, int id)
  234. {
  235. struct hwspinlock *tmp;
  236. int ret;
  237. mutex_lock(&hwspinlock_tree_lock);
  238. ret = radix_tree_insert(&hwspinlock_tree, id, hwlock);
  239. if (ret) {
  240. if (ret == -EEXIST)
  241. pr_err("hwspinlock id %d already exists!\n", id);
  242. goto out;
  243. }
  244. /* mark this hwspinlock as available */
  245. tmp = radix_tree_tag_set(&hwspinlock_tree, id, HWSPINLOCK_UNUSED);
  246. /* self-sanity check which should never fail */
  247. WARN_ON(tmp != hwlock);
  248. out:
  249. mutex_unlock(&hwspinlock_tree_lock);
  250. return 0;
  251. }
  252. static struct hwspinlock *hwspin_lock_unregister_single(unsigned int id)
  253. {
  254. struct hwspinlock *hwlock = NULL;
  255. int ret;
  256. mutex_lock(&hwspinlock_tree_lock);
  257. /* make sure the hwspinlock is not in use (tag is set) */
  258. ret = radix_tree_tag_get(&hwspinlock_tree, id, HWSPINLOCK_UNUSED);
  259. if (ret == 0) {
  260. pr_err("hwspinlock %d still in use (or not present)\n", id);
  261. goto out;
  262. }
  263. hwlock = radix_tree_delete(&hwspinlock_tree, id);
  264. if (!hwlock) {
  265. pr_err("failed to delete hwspinlock %d\n", id);
  266. goto out;
  267. }
  268. out:
  269. mutex_unlock(&hwspinlock_tree_lock);
  270. return hwlock;
  271. }
  272. /**
  273. * hwspin_lock_register() - register a new hw spinlock device
  274. * @bank: the hwspinlock device, which usually provides numerous hw locks
  275. * @dev: the backing device
  276. * @ops: hwspinlock handlers for this device
  277. * @base_id: id of the first hardware spinlock in this bank
  278. * @num_locks: number of hwspinlocks provided by this device
  279. *
  280. * This function should be called from the underlying platform-specific
  281. * implementation, to register a new hwspinlock device instance.
  282. *
  283. * Should be called from a process context (might sleep)
  284. *
  285. * Returns 0 on success, or an appropriate error code on failure
  286. */
  287. int hwspin_lock_register(struct hwspinlock_device *bank, struct device *dev,
  288. const struct hwspinlock_ops *ops, int base_id, int num_locks)
  289. {
  290. struct hwspinlock *hwlock;
  291. int ret = 0, i;
  292. if (!bank || !ops || !dev || !num_locks || !ops->trylock ||
  293. !ops->unlock) {
  294. pr_err("invalid parameters\n");
  295. return -EINVAL;
  296. }
  297. bank->dev = dev;
  298. bank->ops = ops;
  299. bank->base_id = base_id;
  300. bank->num_locks = num_locks;
  301. for (i = 0; i < num_locks; i++) {
  302. hwlock = &bank->lock[i];
  303. spin_lock_init(&hwlock->lock);
  304. hwlock->bank = bank;
  305. ret = hwspin_lock_register_single(hwlock, base_id + i);
  306. if (ret)
  307. goto reg_failed;
  308. }
  309. return 0;
  310. reg_failed:
  311. while (--i >= 0)
  312. hwspin_lock_unregister_single(base_id + i);
  313. return ret;
  314. }
  315. EXPORT_SYMBOL_GPL(hwspin_lock_register);
  316. /**
  317. * hwspin_lock_unregister() - unregister an hw spinlock device
  318. * @bank: the hwspinlock device, which usually provides numerous hw locks
  319. *
  320. * This function should be called from the underlying platform-specific
  321. * implementation, to unregister an existing (and unused) hwspinlock.
  322. *
  323. * Should be called from a process context (might sleep)
  324. *
  325. * Returns 0 on success, or an appropriate error code on failure
  326. */
  327. int hwspin_lock_unregister(struct hwspinlock_device *bank)
  328. {
  329. struct hwspinlock *hwlock, *tmp;
  330. int i;
  331. for (i = 0; i < bank->num_locks; i++) {
  332. hwlock = &bank->lock[i];
  333. tmp = hwspin_lock_unregister_single(bank->base_id + i);
  334. if (!tmp)
  335. return -EBUSY;
  336. /* self-sanity check that should never fail */
  337. WARN_ON(tmp != hwlock);
  338. }
  339. return 0;
  340. }
  341. EXPORT_SYMBOL_GPL(hwspin_lock_unregister);
  342. /**
  343. * __hwspin_lock_request() - tag an hwspinlock as used and power it up
  344. *
  345. * This is an internal function that prepares an hwspinlock instance
  346. * before it is given to the user. The function assumes that
  347. * hwspinlock_tree_lock is taken.
  348. *
  349. * Returns 0 or positive to indicate success, and a negative value to
  350. * indicate an error (with the appropriate error code)
  351. */
  352. static int __hwspin_lock_request(struct hwspinlock *hwlock)
  353. {
  354. struct device *dev = hwlock->bank->dev;
  355. struct hwspinlock *tmp;
  356. int ret;
  357. /* prevent underlying implementation from being removed */
  358. if (!try_module_get(dev->driver->owner)) {
  359. dev_err(dev, "%s: can't get owner\n", __func__);
  360. return -EINVAL;
  361. }
  362. /* notify PM core that power is now needed */
  363. ret = pm_runtime_get_sync(dev);
  364. if (ret < 0) {
  365. dev_err(dev, "%s: can't power on device\n", __func__);
  366. pm_runtime_put_noidle(dev);
  367. module_put(dev->driver->owner);
  368. return ret;
  369. }
  370. /* mark hwspinlock as used, should not fail */
  371. tmp = radix_tree_tag_clear(&hwspinlock_tree, hwlock_to_id(hwlock),
  372. HWSPINLOCK_UNUSED);
  373. /* self-sanity check that should never fail */
  374. WARN_ON(tmp != hwlock);
  375. return ret;
  376. }
  377. /**
  378. * hwspin_lock_get_id() - retrieve id number of a given hwspinlock
  379. * @hwlock: a valid hwspinlock instance
  380. *
  381. * Returns the id number of a given @hwlock, or -EINVAL if @hwlock is invalid.
  382. */
  383. int hwspin_lock_get_id(struct hwspinlock *hwlock)
  384. {
  385. if (!hwlock) {
  386. pr_err("invalid hwlock\n");
  387. return -EINVAL;
  388. }
  389. return hwlock_to_id(hwlock);
  390. }
  391. EXPORT_SYMBOL_GPL(hwspin_lock_get_id);
  392. /**
  393. * hwspin_lock_request() - request an hwspinlock
  394. *
  395. * This function should be called by users of the hwspinlock device,
  396. * in order to dynamically assign them an unused hwspinlock.
  397. * Usually the user of this lock will then have to communicate the lock's id
  398. * to the remote core before it can be used for synchronization (to get the
  399. * id of a given hwlock, use hwspin_lock_get_id()).
  400. *
  401. * Should be called from a process context (might sleep)
  402. *
  403. * Returns the address of the assigned hwspinlock, or NULL on error
  404. */
  405. struct hwspinlock *hwspin_lock_request(void)
  406. {
  407. struct hwspinlock *hwlock;
  408. int ret;
  409. mutex_lock(&hwspinlock_tree_lock);
  410. /* look for an unused lock */
  411. ret = radix_tree_gang_lookup_tag(&hwspinlock_tree, (void **)&hwlock,
  412. 0, 1, HWSPINLOCK_UNUSED);
  413. if (ret == 0) {
  414. pr_warn("a free hwspinlock is not available\n");
  415. hwlock = NULL;
  416. goto out;
  417. }
  418. /* sanity check that should never fail */
  419. WARN_ON(ret > 1);
  420. /* mark as used and power up */
  421. ret = __hwspin_lock_request(hwlock);
  422. if (ret < 0)
  423. hwlock = NULL;
  424. out:
  425. mutex_unlock(&hwspinlock_tree_lock);
  426. return hwlock;
  427. }
  428. EXPORT_SYMBOL_GPL(hwspin_lock_request);
  429. /**
  430. * hwspin_lock_request_specific() - request for a specific hwspinlock
  431. * @id: index of the specific hwspinlock that is requested
  432. *
  433. * This function should be called by users of the hwspinlock module,
  434. * in order to assign them a specific hwspinlock.
  435. * Usually early board code will be calling this function in order to
  436. * reserve specific hwspinlock ids for predefined purposes.
  437. *
  438. * Should be called from a process context (might sleep)
  439. *
  440. * Returns the address of the assigned hwspinlock, or NULL on error
  441. */
  442. struct hwspinlock *hwspin_lock_request_specific(unsigned int id)
  443. {
  444. struct hwspinlock *hwlock;
  445. int ret;
  446. mutex_lock(&hwspinlock_tree_lock);
  447. /* make sure this hwspinlock exists */
  448. hwlock = radix_tree_lookup(&hwspinlock_tree, id);
  449. if (!hwlock) {
  450. pr_warn("hwspinlock %u does not exist\n", id);
  451. goto out;
  452. }
  453. /* sanity check (this shouldn't happen) */
  454. WARN_ON(hwlock_to_id(hwlock) != id);
  455. /* make sure this hwspinlock is unused */
  456. ret = radix_tree_tag_get(&hwspinlock_tree, id, HWSPINLOCK_UNUSED);
  457. if (ret == 0) {
  458. pr_warn("hwspinlock %u is already in use\n", id);
  459. hwlock = NULL;
  460. goto out;
  461. }
  462. /* mark as used and power up */
  463. ret = __hwspin_lock_request(hwlock);
  464. if (ret < 0)
  465. hwlock = NULL;
  466. out:
  467. mutex_unlock(&hwspinlock_tree_lock);
  468. return hwlock;
  469. }
  470. EXPORT_SYMBOL_GPL(hwspin_lock_request_specific);
  471. /**
  472. * hwspin_lock_free() - free a specific hwspinlock
  473. * @hwlock: the specific hwspinlock to free
  474. *
  475. * This function mark @hwlock as free again.
  476. * Should only be called with an @hwlock that was retrieved from
  477. * an earlier call to omap_hwspin_lock_request{_specific}.
  478. *
  479. * Should be called from a process context (might sleep)
  480. *
  481. * Returns 0 on success, or an appropriate error code on failure
  482. */
  483. int hwspin_lock_free(struct hwspinlock *hwlock)
  484. {
  485. struct device *dev = hwlock->bank->dev;
  486. struct hwspinlock *tmp;
  487. int ret;
  488. if (!hwlock) {
  489. pr_err("invalid hwlock\n");
  490. return -EINVAL;
  491. }
  492. mutex_lock(&hwspinlock_tree_lock);
  493. /* make sure the hwspinlock is used */
  494. ret = radix_tree_tag_get(&hwspinlock_tree, hwlock_to_id(hwlock),
  495. HWSPINLOCK_UNUSED);
  496. if (ret == 1) {
  497. dev_err(dev, "%s: hwlock is already free\n", __func__);
  498. dump_stack();
  499. ret = -EINVAL;
  500. goto out;
  501. }
  502. /* notify the underlying device that power is not needed */
  503. ret = pm_runtime_put(dev);
  504. if (ret < 0)
  505. goto out;
  506. /* mark this hwspinlock as available */
  507. tmp = radix_tree_tag_set(&hwspinlock_tree, hwlock_to_id(hwlock),
  508. HWSPINLOCK_UNUSED);
  509. /* sanity check (this shouldn't happen) */
  510. WARN_ON(tmp != hwlock);
  511. module_put(dev->driver->owner);
  512. out:
  513. mutex_unlock(&hwspinlock_tree_lock);
  514. return ret;
  515. }
  516. EXPORT_SYMBOL_GPL(hwspin_lock_free);
  517. MODULE_LICENSE("GPL v2");
  518. MODULE_DESCRIPTION("Hardware spinlock interface");
  519. MODULE_AUTHOR("Ohad Ben-Cohen <ohad@wizery.com>");