hwspinlock_core.c 26 KB

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