hwspinlock.h 11 KB

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