hwspinlock.h 11 KB

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