hwspinlock.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Hardware spinlock public header
  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. #ifndef __LINUX_HWSPINLOCK_H
  18. #define __LINUX_HWSPINLOCK_H
  19. #include <linux/err.h>
  20. #include <linux/sched.h>
  21. /* hwspinlock mode argument */
  22. #define HWLOCK_IRQSTATE 0x01 /* Disable interrupts, save state */
  23. #define HWLOCK_IRQ 0x02 /* Disable interrupts, don't save state */
  24. struct device;
  25. struct device_node;
  26. struct hwspinlock;
  27. struct hwspinlock_device;
  28. struct hwspinlock_ops;
  29. /**
  30. * struct hwspinlock_pdata - platform data for hwspinlock drivers
  31. * @base_id: base id for this hwspinlock device
  32. *
  33. * hwspinlock devices provide system-wide hardware locks that are used
  34. * by remote processors that have no other way to achieve synchronization.
  35. *
  36. * To achieve that, each physical lock must have a system-wide id number
  37. * that is agreed upon, otherwise remote processors can't possibly assume
  38. * they're using the same hardware lock.
  39. *
  40. * Usually boards have a single hwspinlock device, which provides several
  41. * hwspinlocks, and in this case, they can be trivially numbered 0 to
  42. * (num-of-locks - 1).
  43. *
  44. * In case boards have several hwspinlocks devices, a different base id
  45. * should be used for each hwspinlock device (they can't all use 0 as
  46. * a starting id!).
  47. *
  48. * This platform data structure should be used to provide the base id
  49. * for each device (which is trivially 0 when only a single hwspinlock
  50. * device exists). It can be shared between different platforms, hence
  51. * its location.
  52. */
  53. struct hwspinlock_pdata {
  54. int base_id;
  55. };
  56. #if defined(CONFIG_HWSPINLOCK) || defined(CONFIG_HWSPINLOCK_MODULE)
  57. int hwspin_lock_register(struct hwspinlock_device *bank, struct device *dev,
  58. const struct hwspinlock_ops *ops, int base_id, int num_locks);
  59. int hwspin_lock_unregister(struct hwspinlock_device *bank);
  60. struct hwspinlock *hwspin_lock_request(void);
  61. struct hwspinlock *hwspin_lock_request_specific(unsigned int id);
  62. int hwspin_lock_free(struct hwspinlock *hwlock);
  63. int of_hwspin_lock_get_id(struct device_node *np, int index);
  64. int hwspin_lock_get_id(struct hwspinlock *hwlock);
  65. int __hwspin_lock_timeout(struct hwspinlock *, unsigned int, int,
  66. unsigned long *);
  67. int __hwspin_trylock(struct hwspinlock *, int, unsigned long *);
  68. void __hwspin_unlock(struct hwspinlock *, int, unsigned long *);
  69. #else /* !CONFIG_HWSPINLOCK */
  70. /*
  71. * We don't want these functions to fail if CONFIG_HWSPINLOCK is not
  72. * enabled. We prefer to silently succeed in this case, and let the
  73. * code path get compiled away. This way, if CONFIG_HWSPINLOCK is not
  74. * required on a given setup, users will still work.
  75. *
  76. * The only exception is hwspin_lock_register/hwspin_lock_unregister, with which
  77. * we _do_ want users to fail (no point in registering hwspinlock instances if
  78. * the framework is not available).
  79. *
  80. * Note: ERR_PTR(-ENODEV) will still be considered a success for NULL-checking
  81. * users. Others, which care, can still check this with IS_ERR.
  82. */
  83. static inline struct hwspinlock *hwspin_lock_request(void)
  84. {
  85. return ERR_PTR(-ENODEV);
  86. }
  87. static inline struct hwspinlock *hwspin_lock_request_specific(unsigned int id)
  88. {
  89. return ERR_PTR(-ENODEV);
  90. }
  91. static inline int hwspin_lock_free(struct hwspinlock *hwlock)
  92. {
  93. return 0;
  94. }
  95. static inline
  96. int __hwspin_lock_timeout(struct hwspinlock *hwlock, unsigned int to,
  97. int mode, unsigned long *flags)
  98. {
  99. return 0;
  100. }
  101. static inline
  102. int __hwspin_trylock(struct hwspinlock *hwlock, int mode, unsigned long *flags)
  103. {
  104. return 0;
  105. }
  106. static inline
  107. void __hwspin_unlock(struct hwspinlock *hwlock, int mode, unsigned long *flags)
  108. {
  109. }
  110. static inline int of_hwspin_lock_get_id(struct device_node *np, int index)
  111. {
  112. return 0;
  113. }
  114. static inline int hwspin_lock_get_id(struct hwspinlock *hwlock)
  115. {
  116. return 0;
  117. }
  118. #endif /* !CONFIG_HWSPINLOCK */
  119. /**
  120. * hwspin_trylock_irqsave() - try to lock an hwspinlock, disable interrupts
  121. * @hwlock: an hwspinlock which we want to trylock
  122. * @flags: a pointer to where the caller's interrupt state will be saved at
  123. *
  124. * This function attempts to lock the underlying hwspinlock, and will
  125. * immediately fail if the hwspinlock is already locked.
  126. *
  127. * Upon a successful return from this function, preemption and local
  128. * interrupts are disabled (previous interrupts state is saved at @flags),
  129. * so the caller must not sleep, and is advised to release the hwspinlock
  130. * as soon as possible.
  131. *
  132. * Returns 0 if we successfully locked the hwspinlock, -EBUSY if
  133. * the hwspinlock was already taken, and -EINVAL if @hwlock is invalid.
  134. */
  135. static inline
  136. int hwspin_trylock_irqsave(struct hwspinlock *hwlock, unsigned long *flags)
  137. {
  138. return __hwspin_trylock(hwlock, HWLOCK_IRQSTATE, flags);
  139. }
  140. /**
  141. * hwspin_trylock_irq() - try to lock an hwspinlock, disable interrupts
  142. * @hwlock: an hwspinlock which we want to trylock
  143. *
  144. * This function attempts to lock the underlying hwspinlock, and will
  145. * immediately fail if the hwspinlock is already locked.
  146. *
  147. * Upon a successful return from this function, preemption and local
  148. * interrupts are disabled, so the caller must not sleep, and is advised
  149. * to release the hwspinlock as soon as possible.
  150. *
  151. * Returns 0 if we successfully locked the hwspinlock, -EBUSY if
  152. * the hwspinlock was already taken, and -EINVAL if @hwlock is invalid.
  153. */
  154. static inline int hwspin_trylock_irq(struct hwspinlock *hwlock)
  155. {
  156. return __hwspin_trylock(hwlock, HWLOCK_IRQ, NULL);
  157. }
  158. /**
  159. * hwspin_trylock() - attempt to lock a specific hwspinlock
  160. * @hwlock: an hwspinlock which we want to trylock
  161. *
  162. * This function attempts to lock an hwspinlock, and will immediately fail
  163. * if the hwspinlock is already taken.
  164. *
  165. * Upon a successful return from this function, preemption is disabled,
  166. * so the caller must not sleep, and is advised to release the hwspinlock
  167. * as soon as possible. This is required in order to minimize remote cores
  168. * polling on the hardware interconnect.
  169. *
  170. * Returns 0 if we successfully locked the hwspinlock, -EBUSY if
  171. * the hwspinlock was already taken, and -EINVAL if @hwlock is invalid.
  172. */
  173. static inline int hwspin_trylock(struct hwspinlock *hwlock)
  174. {
  175. return __hwspin_trylock(hwlock, 0, NULL);
  176. }
  177. /**
  178. * hwspin_lock_timeout_irqsave() - lock hwspinlock, with timeout, disable irqs
  179. * @hwlock: the hwspinlock to be locked
  180. * @to: timeout value in msecs
  181. * @flags: a pointer to where the caller's interrupt state will be saved at
  182. *
  183. * This function locks the underlying @hwlock. If the @hwlock
  184. * is already taken, the function will busy loop waiting for it to
  185. * be released, but give up when @timeout msecs have elapsed.
  186. *
  187. * Upon a successful return from this function, preemption and local interrupts
  188. * are disabled (plus previous interrupt state is saved), so the caller must
  189. * not sleep, and is advised to release the hwspinlock as soon as possible.
  190. *
  191. * Returns 0 when the @hwlock was successfully taken, and an appropriate
  192. * error code otherwise (most notably an -ETIMEDOUT if the @hwlock is still
  193. * busy after @timeout msecs). The function will never sleep.
  194. */
  195. static inline int hwspin_lock_timeout_irqsave(struct hwspinlock *hwlock,
  196. unsigned int to, unsigned long *flags)
  197. {
  198. return __hwspin_lock_timeout(hwlock, to, HWLOCK_IRQSTATE, flags);
  199. }
  200. /**
  201. * hwspin_lock_timeout_irq() - lock hwspinlock, with timeout, disable irqs
  202. * @hwlock: the hwspinlock to be locked
  203. * @to: timeout value in msecs
  204. *
  205. * This function locks the underlying @hwlock. If the @hwlock
  206. * is already taken, the function will busy loop waiting for it to
  207. * be released, but give up when @timeout msecs have elapsed.
  208. *
  209. * Upon a successful return from this function, preemption and local interrupts
  210. * are disabled so the caller must not sleep, and is advised to release the
  211. * hwspinlock as soon as possible.
  212. *
  213. * Returns 0 when the @hwlock was successfully taken, and an appropriate
  214. * error code otherwise (most notably an -ETIMEDOUT if the @hwlock is still
  215. * busy after @timeout msecs). The function will never sleep.
  216. */
  217. static inline
  218. int hwspin_lock_timeout_irq(struct hwspinlock *hwlock, unsigned int to)
  219. {
  220. return __hwspin_lock_timeout(hwlock, to, HWLOCK_IRQ, NULL);
  221. }
  222. /**
  223. * hwspin_lock_timeout() - lock an hwspinlock with timeout limit
  224. * @hwlock: the hwspinlock to be locked
  225. * @to: timeout value in msecs
  226. *
  227. * This function locks the underlying @hwlock. If the @hwlock
  228. * is already taken, the function will busy loop waiting for it to
  229. * be released, but give up when @timeout msecs have elapsed.
  230. *
  231. * Upon a successful return from this function, preemption is disabled
  232. * so the caller must not sleep, and is advised to release the hwspinlock
  233. * as soon as possible.
  234. * This is required in order to minimize remote cores polling on the
  235. * hardware interconnect.
  236. *
  237. * Returns 0 when the @hwlock was successfully taken, and an appropriate
  238. * error code otherwise (most notably an -ETIMEDOUT if the @hwlock is still
  239. * busy after @timeout msecs). The function will never sleep.
  240. */
  241. static inline
  242. int hwspin_lock_timeout(struct hwspinlock *hwlock, unsigned int to)
  243. {
  244. return __hwspin_lock_timeout(hwlock, to, 0, NULL);
  245. }
  246. /**
  247. * hwspin_unlock_irqrestore() - unlock hwspinlock, restore irq state
  248. * @hwlock: a previously-acquired hwspinlock which we want to unlock
  249. * @flags: previous caller's interrupt state to restore
  250. *
  251. * This function will unlock a specific hwspinlock, enable preemption and
  252. * restore the previous state of the local interrupts. It should be used
  253. * to undo, e.g., hwspin_trylock_irqsave().
  254. *
  255. * @hwlock must be already locked before calling this function: it is a bug
  256. * to call unlock on a @hwlock that is already unlocked.
  257. */
  258. static inline void hwspin_unlock_irqrestore(struct hwspinlock *hwlock,
  259. unsigned long *flags)
  260. {
  261. __hwspin_unlock(hwlock, HWLOCK_IRQSTATE, flags);
  262. }
  263. /**
  264. * hwspin_unlock_irq() - unlock hwspinlock, enable interrupts
  265. * @hwlock: a previously-acquired hwspinlock which we want to unlock
  266. *
  267. * This function will unlock a specific hwspinlock, enable preemption and
  268. * enable local interrupts. Should be used to undo hwspin_lock_irq().
  269. *
  270. * @hwlock must be already locked (e.g. by hwspin_trylock_irq()) before
  271. * calling this function: it is a bug to call unlock on a @hwlock that is
  272. * already unlocked.
  273. */
  274. static inline void hwspin_unlock_irq(struct hwspinlock *hwlock)
  275. {
  276. __hwspin_unlock(hwlock, HWLOCK_IRQ, NULL);
  277. }
  278. /**
  279. * hwspin_unlock() - unlock hwspinlock
  280. * @hwlock: a previously-acquired hwspinlock which we want to unlock
  281. *
  282. * This function will unlock a specific hwspinlock and enable preemption
  283. * back.
  284. *
  285. * @hwlock must be already locked (e.g. by hwspin_trylock()) before calling
  286. * this function: it is a bug to call unlock on a @hwlock that is already
  287. * unlocked.
  288. */
  289. static inline void hwspin_unlock(struct hwspinlock *hwlock)
  290. {
  291. __hwspin_unlock(hwlock, 0, NULL);
  292. }
  293. #endif /* __LINUX_HWSPINLOCK_H */