hwspinlock_core.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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. static void devm_hwspin_lock_unreg(struct device *dev, void *res)
  470. {
  471. hwspin_lock_unregister(*(struct hwspinlock_device **)res);
  472. }
  473. static int devm_hwspin_lock_device_match(struct device *dev, void *res,
  474. void *data)
  475. {
  476. struct hwspinlock_device **bank = res;
  477. if (WARN_ON(!bank || !*bank))
  478. return 0;
  479. return *bank == data;
  480. }
  481. /**
  482. * devm_hwspin_lock_unregister() - unregister an hw spinlock device for
  483. * a managed device
  484. * @dev: the backing device
  485. * @bank: the hwspinlock device, which usually provides numerous hw locks
  486. *
  487. * This function should be called from the underlying platform-specific
  488. * implementation, to unregister an existing (and unused) hwspinlock.
  489. *
  490. * Should be called from a process context (might sleep)
  491. *
  492. * Returns 0 on success, or an appropriate error code on failure
  493. */
  494. int devm_hwspin_lock_unregister(struct device *dev,
  495. struct hwspinlock_device *bank)
  496. {
  497. int ret;
  498. ret = devres_release(dev, devm_hwspin_lock_unreg,
  499. devm_hwspin_lock_device_match, bank);
  500. WARN_ON(ret);
  501. return ret;
  502. }
  503. EXPORT_SYMBOL_GPL(devm_hwspin_lock_unregister);
  504. /**
  505. * devm_hwspin_lock_register() - register a new hw spinlock device for
  506. * a managed device
  507. * @dev: the backing device
  508. * @bank: the hwspinlock device, which usually provides numerous hw locks
  509. * @ops: hwspinlock handlers for this device
  510. * @base_id: id of the first hardware spinlock in this bank
  511. * @num_locks: number of hwspinlocks provided by this device
  512. *
  513. * This function should be called from the underlying platform-specific
  514. * implementation, to register a new hwspinlock device instance.
  515. *
  516. * Should be called from a process context (might sleep)
  517. *
  518. * Returns 0 on success, or an appropriate error code on failure
  519. */
  520. int devm_hwspin_lock_register(struct device *dev,
  521. struct hwspinlock_device *bank,
  522. const struct hwspinlock_ops *ops,
  523. int base_id, int num_locks)
  524. {
  525. struct hwspinlock_device **ptr;
  526. int ret;
  527. ptr = devres_alloc(devm_hwspin_lock_unreg, sizeof(*ptr), GFP_KERNEL);
  528. if (!ptr)
  529. return -ENOMEM;
  530. ret = hwspin_lock_register(bank, dev, ops, base_id, num_locks);
  531. if (!ret) {
  532. *ptr = bank;
  533. devres_add(dev, ptr);
  534. } else {
  535. devres_free(ptr);
  536. }
  537. return ret;
  538. }
  539. EXPORT_SYMBOL_GPL(devm_hwspin_lock_register);
  540. /**
  541. * __hwspin_lock_request() - tag an hwspinlock as used and power it up
  542. *
  543. * This is an internal function that prepares an hwspinlock instance
  544. * before it is given to the user. The function assumes that
  545. * hwspinlock_tree_lock is taken.
  546. *
  547. * Returns 0 or positive to indicate success, and a negative value to
  548. * indicate an error (with the appropriate error code)
  549. */
  550. static int __hwspin_lock_request(struct hwspinlock *hwlock)
  551. {
  552. struct device *dev = hwlock->bank->dev;
  553. struct hwspinlock *tmp;
  554. int ret;
  555. /* prevent underlying implementation from being removed */
  556. if (!try_module_get(dev->driver->owner)) {
  557. dev_err(dev, "%s: can't get owner\n", __func__);
  558. return -EINVAL;
  559. }
  560. /* notify PM core that power is now needed */
  561. ret = pm_runtime_get_sync(dev);
  562. if (ret < 0) {
  563. dev_err(dev, "%s: can't power on device\n", __func__);
  564. pm_runtime_put_noidle(dev);
  565. module_put(dev->driver->owner);
  566. return ret;
  567. }
  568. /* mark hwspinlock as used, should not fail */
  569. tmp = radix_tree_tag_clear(&hwspinlock_tree, hwlock_to_id(hwlock),
  570. HWSPINLOCK_UNUSED);
  571. /* self-sanity check that should never fail */
  572. WARN_ON(tmp != hwlock);
  573. return ret;
  574. }
  575. /**
  576. * hwspin_lock_get_id() - retrieve id number of a given hwspinlock
  577. * @hwlock: a valid hwspinlock instance
  578. *
  579. * Returns the id number of a given @hwlock, or -EINVAL if @hwlock is invalid.
  580. */
  581. int hwspin_lock_get_id(struct hwspinlock *hwlock)
  582. {
  583. if (!hwlock) {
  584. pr_err("invalid hwlock\n");
  585. return -EINVAL;
  586. }
  587. return hwlock_to_id(hwlock);
  588. }
  589. EXPORT_SYMBOL_GPL(hwspin_lock_get_id);
  590. /**
  591. * hwspin_lock_request() - request an hwspinlock
  592. *
  593. * This function should be called by users of the hwspinlock device,
  594. * in order to dynamically assign them an unused hwspinlock.
  595. * Usually the user of this lock will then have to communicate the lock's id
  596. * to the remote core before it can be used for synchronization (to get the
  597. * id of a given hwlock, use hwspin_lock_get_id()).
  598. *
  599. * Should be called from a process context (might sleep)
  600. *
  601. * Returns the address of the assigned hwspinlock, or NULL on error
  602. */
  603. struct hwspinlock *hwspin_lock_request(void)
  604. {
  605. struct hwspinlock *hwlock;
  606. int ret;
  607. mutex_lock(&hwspinlock_tree_lock);
  608. /* look for an unused lock */
  609. ret = radix_tree_gang_lookup_tag(&hwspinlock_tree, (void **)&hwlock,
  610. 0, 1, HWSPINLOCK_UNUSED);
  611. if (ret == 0) {
  612. pr_warn("a free hwspinlock is not available\n");
  613. hwlock = NULL;
  614. goto out;
  615. }
  616. /* sanity check that should never fail */
  617. WARN_ON(ret > 1);
  618. /* mark as used and power up */
  619. ret = __hwspin_lock_request(hwlock);
  620. if (ret < 0)
  621. hwlock = NULL;
  622. out:
  623. mutex_unlock(&hwspinlock_tree_lock);
  624. return hwlock;
  625. }
  626. EXPORT_SYMBOL_GPL(hwspin_lock_request);
  627. /**
  628. * hwspin_lock_request_specific() - request for a specific hwspinlock
  629. * @id: index of the specific hwspinlock that is requested
  630. *
  631. * This function should be called by users of the hwspinlock module,
  632. * in order to assign them a specific hwspinlock.
  633. * Usually early board code will be calling this function in order to
  634. * reserve specific hwspinlock ids for predefined purposes.
  635. *
  636. * Should be called from a process context (might sleep)
  637. *
  638. * Returns the address of the assigned hwspinlock, or NULL on error
  639. */
  640. struct hwspinlock *hwspin_lock_request_specific(unsigned int id)
  641. {
  642. struct hwspinlock *hwlock;
  643. int ret;
  644. mutex_lock(&hwspinlock_tree_lock);
  645. /* make sure this hwspinlock exists */
  646. hwlock = radix_tree_lookup(&hwspinlock_tree, id);
  647. if (!hwlock) {
  648. pr_warn("hwspinlock %u does not exist\n", id);
  649. goto out;
  650. }
  651. /* sanity check (this shouldn't happen) */
  652. WARN_ON(hwlock_to_id(hwlock) != id);
  653. /* make sure this hwspinlock is unused */
  654. ret = radix_tree_tag_get(&hwspinlock_tree, id, HWSPINLOCK_UNUSED);
  655. if (ret == 0) {
  656. pr_warn("hwspinlock %u is already in use\n", id);
  657. hwlock = NULL;
  658. goto out;
  659. }
  660. /* mark as used and power up */
  661. ret = __hwspin_lock_request(hwlock);
  662. if (ret < 0)
  663. hwlock = NULL;
  664. out:
  665. mutex_unlock(&hwspinlock_tree_lock);
  666. return hwlock;
  667. }
  668. EXPORT_SYMBOL_GPL(hwspin_lock_request_specific);
  669. /**
  670. * hwspin_lock_free() - free a specific hwspinlock
  671. * @hwlock: the specific hwspinlock to free
  672. *
  673. * This function mark @hwlock as free again.
  674. * Should only be called with an @hwlock that was retrieved from
  675. * an earlier call to hwspin_lock_request{_specific}.
  676. *
  677. * Should be called from a process context (might sleep)
  678. *
  679. * Returns 0 on success, or an appropriate error code on failure
  680. */
  681. int hwspin_lock_free(struct hwspinlock *hwlock)
  682. {
  683. struct device *dev;
  684. struct hwspinlock *tmp;
  685. int ret;
  686. if (!hwlock) {
  687. pr_err("invalid hwlock\n");
  688. return -EINVAL;
  689. }
  690. dev = hwlock->bank->dev;
  691. mutex_lock(&hwspinlock_tree_lock);
  692. /* make sure the hwspinlock is used */
  693. ret = radix_tree_tag_get(&hwspinlock_tree, hwlock_to_id(hwlock),
  694. HWSPINLOCK_UNUSED);
  695. if (ret == 1) {
  696. dev_err(dev, "%s: hwlock is already free\n", __func__);
  697. dump_stack();
  698. ret = -EINVAL;
  699. goto out;
  700. }
  701. /* notify the underlying device that power is not needed */
  702. ret = pm_runtime_put(dev);
  703. if (ret < 0)
  704. goto out;
  705. /* mark this hwspinlock as available */
  706. tmp = radix_tree_tag_set(&hwspinlock_tree, hwlock_to_id(hwlock),
  707. HWSPINLOCK_UNUSED);
  708. /* sanity check (this shouldn't happen) */
  709. WARN_ON(tmp != hwlock);
  710. module_put(dev->driver->owner);
  711. out:
  712. mutex_unlock(&hwspinlock_tree_lock);
  713. return ret;
  714. }
  715. EXPORT_SYMBOL_GPL(hwspin_lock_free);
  716. static int devm_hwspin_lock_match(struct device *dev, void *res, void *data)
  717. {
  718. struct hwspinlock **hwlock = res;
  719. if (WARN_ON(!hwlock || !*hwlock))
  720. return 0;
  721. return *hwlock == data;
  722. }
  723. static void devm_hwspin_lock_release(struct device *dev, void *res)
  724. {
  725. hwspin_lock_free(*(struct hwspinlock **)res);
  726. }
  727. /**
  728. * devm_hwspin_lock_free() - free a specific hwspinlock for a managed device
  729. * @dev: the device to free the specific hwspinlock
  730. * @hwlock: the specific hwspinlock to free
  731. *
  732. * This function mark @hwlock as free again.
  733. * Should only be called with an @hwlock that was retrieved from
  734. * an earlier call to hwspin_lock_request{_specific}.
  735. *
  736. * Should be called from a process context (might sleep)
  737. *
  738. * Returns 0 on success, or an appropriate error code on failure
  739. */
  740. int devm_hwspin_lock_free(struct device *dev, struct hwspinlock *hwlock)
  741. {
  742. int ret;
  743. ret = devres_release(dev, devm_hwspin_lock_release,
  744. devm_hwspin_lock_match, hwlock);
  745. WARN_ON(ret);
  746. return ret;
  747. }
  748. EXPORT_SYMBOL_GPL(devm_hwspin_lock_free);
  749. /**
  750. * devm_hwspin_lock_request() - request an hwspinlock for a managed device
  751. * @dev: the device to request an hwspinlock
  752. *
  753. * This function should be called by users of the hwspinlock device,
  754. * in order to dynamically assign them an unused hwspinlock.
  755. * Usually the user of this lock will then have to communicate the lock's id
  756. * to the remote core before it can be used for synchronization (to get the
  757. * id of a given hwlock, use hwspin_lock_get_id()).
  758. *
  759. * Should be called from a process context (might sleep)
  760. *
  761. * Returns the address of the assigned hwspinlock, or NULL on error
  762. */
  763. struct hwspinlock *devm_hwspin_lock_request(struct device *dev)
  764. {
  765. struct hwspinlock **ptr, *hwlock;
  766. ptr = devres_alloc(devm_hwspin_lock_release, sizeof(*ptr), GFP_KERNEL);
  767. if (!ptr)
  768. return NULL;
  769. hwlock = hwspin_lock_request();
  770. if (hwlock) {
  771. *ptr = hwlock;
  772. devres_add(dev, ptr);
  773. } else {
  774. devres_free(ptr);
  775. }
  776. return hwlock;
  777. }
  778. EXPORT_SYMBOL_GPL(devm_hwspin_lock_request);
  779. /**
  780. * devm_hwspin_lock_request_specific() - request for a specific hwspinlock for
  781. * a managed device
  782. * @dev: the device to request the specific hwspinlock
  783. * @id: index of the specific hwspinlock that is requested
  784. *
  785. * This function should be called by users of the hwspinlock module,
  786. * in order to assign them a specific hwspinlock.
  787. * Usually early board code will be calling this function in order to
  788. * reserve specific hwspinlock ids for predefined purposes.
  789. *
  790. * Should be called from a process context (might sleep)
  791. *
  792. * Returns the address of the assigned hwspinlock, or NULL on error
  793. */
  794. struct hwspinlock *devm_hwspin_lock_request_specific(struct device *dev,
  795. unsigned int id)
  796. {
  797. struct hwspinlock **ptr, *hwlock;
  798. ptr = devres_alloc(devm_hwspin_lock_release, sizeof(*ptr), GFP_KERNEL);
  799. if (!ptr)
  800. return NULL;
  801. hwlock = hwspin_lock_request_specific(id);
  802. if (hwlock) {
  803. *ptr = hwlock;
  804. devres_add(dev, ptr);
  805. } else {
  806. devres_free(ptr);
  807. }
  808. return hwlock;
  809. }
  810. EXPORT_SYMBOL_GPL(devm_hwspin_lock_request_specific);
  811. MODULE_LICENSE("GPL v2");
  812. MODULE_DESCRIPTION("Hardware spinlock interface");
  813. MODULE_AUTHOR("Ohad Ben-Cohen <ohad@wizery.com>");