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