hwspinlock.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. #endif /* !CONFIG_HWSPINLOCK */
  110. /**
  111. * hwspin_trylock_irqsave() - try to lock an hwspinlock, disable interrupts
  112. * @hwlock: an hwspinlock which we want to trylock
  113. * @flags: a pointer to where the caller's interrupt state will be saved at
  114. *
  115. * This function attempts to lock the underlying hwspinlock, and will
  116. * immediately fail if the hwspinlock is already locked.
  117. *
  118. * Upon a successful return from this function, preemption and local
  119. * interrupts are disabled (previous interrupts state is saved at @flags),
  120. * so the caller must not sleep, and is advised to release the hwspinlock
  121. * as soon as possible.
  122. *
  123. * Returns 0 if we successfully locked the hwspinlock, -EBUSY if
  124. * the hwspinlock was already taken, and -EINVAL if @hwlock is invalid.
  125. */
  126. static inline
  127. int hwspin_trylock_irqsave(struct hwspinlock *hwlock, unsigned long *flags)
  128. {
  129. return __hwspin_trylock(hwlock, HWLOCK_IRQSTATE, flags);
  130. }
  131. /**
  132. * hwspin_trylock_irq() - try to lock an hwspinlock, disable interrupts
  133. * @hwlock: an hwspinlock which we want to trylock
  134. *
  135. * This function attempts to lock the underlying hwspinlock, and will
  136. * immediately fail if the hwspinlock is already locked.
  137. *
  138. * Upon a successful return from this function, preemption and local
  139. * interrupts are disabled, so the caller must not sleep, and is advised
  140. * to release the hwspinlock as soon as possible.
  141. *
  142. * Returns 0 if we successfully locked the hwspinlock, -EBUSY if
  143. * the hwspinlock was already taken, and -EINVAL if @hwlock is invalid.
  144. */
  145. static inline int hwspin_trylock_irq(struct hwspinlock *hwlock)
  146. {
  147. return __hwspin_trylock(hwlock, HWLOCK_IRQ, NULL);
  148. }
  149. /**
  150. * hwspin_trylock() - attempt to lock a specific hwspinlock
  151. * @hwlock: an hwspinlock which we want to trylock
  152. *
  153. * This function attempts to lock an hwspinlock, and will immediately fail
  154. * if the hwspinlock is already taken.
  155. *
  156. * Upon a successful return from this function, preemption is disabled,
  157. * so the caller must not sleep, and is advised to release the hwspinlock
  158. * as soon as possible. This is required in order to minimize remote cores
  159. * polling on the hardware interconnect.
  160. *
  161. * Returns 0 if we successfully locked the hwspinlock, -EBUSY if
  162. * the hwspinlock was already taken, and -EINVAL if @hwlock is invalid.
  163. */
  164. static inline int hwspin_trylock(struct hwspinlock *hwlock)
  165. {
  166. return __hwspin_trylock(hwlock, 0, NULL);
  167. }
  168. /**
  169. * hwspin_lock_timeout_irqsave() - lock hwspinlock, with timeout, disable irqs
  170. * @hwlock: the hwspinlock to be locked
  171. * @to: timeout value in msecs
  172. * @flags: a pointer to where the caller's interrupt state will be saved at
  173. *
  174. * This function locks the underlying @hwlock. If the @hwlock
  175. * is already taken, the function will busy loop waiting for it to
  176. * be released, but give up when @timeout msecs have elapsed.
  177. *
  178. * Upon a successful return from this function, preemption and local interrupts
  179. * are disabled (plus previous interrupt state is saved), so the caller must
  180. * not sleep, and is advised to release the hwspinlock as soon as possible.
  181. *
  182. * Returns 0 when the @hwlock was successfully taken, and an appropriate
  183. * error code otherwise (most notably an -ETIMEDOUT if the @hwlock is still
  184. * busy after @timeout msecs). The function will never sleep.
  185. */
  186. static inline int hwspin_lock_timeout_irqsave(struct hwspinlock *hwlock,
  187. unsigned int to, unsigned long *flags)
  188. {
  189. return __hwspin_lock_timeout(hwlock, to, HWLOCK_IRQSTATE, flags);
  190. }
  191. /**
  192. * hwspin_lock_timeout_irq() - lock hwspinlock, with timeout, disable irqs
  193. * @hwlock: the hwspinlock to be locked
  194. * @to: timeout value in msecs
  195. *
  196. * This function locks the underlying @hwlock. If the @hwlock
  197. * is already taken, the function will busy loop waiting for it to
  198. * be released, but give up when @timeout msecs have elapsed.
  199. *
  200. * Upon a successful return from this function, preemption and local interrupts
  201. * are disabled so the caller must not sleep, and is advised to release the
  202. * hwspinlock as soon as possible.
  203. *
  204. * Returns 0 when the @hwlock was successfully taken, and an appropriate
  205. * error code otherwise (most notably an -ETIMEDOUT if the @hwlock is still
  206. * busy after @timeout msecs). The function will never sleep.
  207. */
  208. static inline
  209. int hwspin_lock_timeout_irq(struct hwspinlock *hwlock, unsigned int to)
  210. {
  211. return __hwspin_lock_timeout(hwlock, to, HWLOCK_IRQ, NULL);
  212. }
  213. /**
  214. * hwspin_lock_timeout() - lock an hwspinlock with timeout limit
  215. * @hwlock: the hwspinlock to be locked
  216. * @to: timeout value in msecs
  217. *
  218. * This function locks the underlying @hwlock. If the @hwlock
  219. * is already taken, the function will busy loop waiting for it to
  220. * be released, but give up when @timeout msecs have elapsed.
  221. *
  222. * Upon a successful return from this function, preemption is disabled
  223. * so the caller must not sleep, and is advised to release the hwspinlock
  224. * as soon as possible.
  225. * This is required in order to minimize remote cores polling on the
  226. * hardware interconnect.
  227. *
  228. * Returns 0 when the @hwlock was successfully taken, and an appropriate
  229. * error code otherwise (most notably an -ETIMEDOUT if the @hwlock is still
  230. * busy after @timeout msecs). The function will never sleep.
  231. */
  232. static inline
  233. int hwspin_lock_timeout(struct hwspinlock *hwlock, unsigned int to)
  234. {
  235. return __hwspin_lock_timeout(hwlock, to, 0, NULL);
  236. }
  237. /**
  238. * hwspin_unlock_irqrestore() - unlock hwspinlock, restore irq state
  239. * @hwlock: a previously-acquired hwspinlock which we want to unlock
  240. * @flags: previous caller's interrupt state to restore
  241. *
  242. * This function will unlock a specific hwspinlock, enable preemption and
  243. * restore the previous state of the local interrupts. It should be used
  244. * to undo, e.g., hwspin_trylock_irqsave().
  245. *
  246. * @hwlock must be already locked before calling this function: it is a bug
  247. * to call unlock on a @hwlock that is already unlocked.
  248. */
  249. static inline void hwspin_unlock_irqrestore(struct hwspinlock *hwlock,
  250. unsigned long *flags)
  251. {
  252. __hwspin_unlock(hwlock, HWLOCK_IRQSTATE, flags);
  253. }
  254. /**
  255. * hwspin_unlock_irq() - unlock hwspinlock, enable interrupts
  256. * @hwlock: a previously-acquired hwspinlock which we want to unlock
  257. *
  258. * This function will unlock a specific hwspinlock, enable preemption and
  259. * enable local interrupts. Should be used to undo hwspin_lock_irq().
  260. *
  261. * @hwlock must be already locked (e.g. by hwspin_trylock_irq()) before
  262. * calling this function: it is a bug to call unlock on a @hwlock that is
  263. * already unlocked.
  264. */
  265. static inline void hwspin_unlock_irq(struct hwspinlock *hwlock)
  266. {
  267. __hwspin_unlock(hwlock, HWLOCK_IRQ, NULL);
  268. }
  269. /**
  270. * hwspin_unlock() - unlock hwspinlock
  271. * @hwlock: a previously-acquired hwspinlock which we want to unlock
  272. *
  273. * This function will unlock a specific hwspinlock and enable preemption
  274. * back.
  275. *
  276. * @hwlock must be already locked (e.g. by hwspin_trylock()) before calling
  277. * this function: it is a bug to call unlock on a @hwlock that is already
  278. * unlocked.
  279. */
  280. static inline void hwspin_unlock(struct hwspinlock *hwlock)
  281. {
  282. __hwspin_unlock(hwlock, 0, NULL);
  283. }
  284. #endif /* __LINUX_HWSPINLOCK_H */