hwspinlock.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. Hardware Spinlock Framework
  2. 1. Introduction
  3. Hardware spinlock modules provide hardware assistance for synchronization
  4. and mutual exclusion between heterogeneous processors and those not operating
  5. under a single, shared operating system.
  6. For example, OMAP4 has dual Cortex-A9, dual Cortex-M3 and a C64x+ DSP,
  7. each of which is running a different Operating System (the master, A9,
  8. is usually running Linux and the slave processors, the M3 and the DSP,
  9. are running some flavor of RTOS).
  10. A generic hwspinlock framework allows platform-independent drivers to use
  11. the hwspinlock device in order to access data structures that are shared
  12. between remote processors, that otherwise have no alternative mechanism
  13. to accomplish synchronization and mutual exclusion operations.
  14. This is necessary, for example, for Inter-processor communications:
  15. on OMAP4, cpu-intensive multimedia tasks are offloaded by the host to the
  16. remote M3 and/or C64x+ slave processors (by an IPC subsystem called Syslink).
  17. To achieve fast message-based communications, a minimal kernel support
  18. is needed to deliver messages arriving from a remote processor to the
  19. appropriate user process.
  20. This communication is based on simple data structures that is shared between
  21. the remote processors, and access to it is synchronized using the hwspinlock
  22. module (remote processor directly places new messages in this shared data
  23. structure).
  24. A common hwspinlock interface makes it possible to have generic, platform-
  25. independent, drivers.
  26. 2. User API
  27. struct hwspinlock *hwspin_lock_request(void);
  28. - dynamically assign an hwspinlock and return its address, or NULL
  29. in case an unused hwspinlock isn't available. Users of this
  30. API will usually want to communicate the lock's id to the remote core
  31. before it can be used to achieve synchronization.
  32. Can be called from an atomic context (this function will not sleep) but
  33. not from within interrupt context.
  34. struct hwspinlock *hwspin_lock_request_specific(unsigned int id);
  35. - assign a specific hwspinlock id and return its address, or NULL
  36. if that hwspinlock is already in use. Usually board code will
  37. be calling this function in order to reserve specific hwspinlock
  38. ids for predefined purposes.
  39. Can be called from an atomic context (this function will not sleep) but
  40. not from within interrupt context.
  41. int hwspin_lock_free(struct hwspinlock *hwlock);
  42. - free a previously-assigned hwspinlock; returns 0 on success, or an
  43. appropriate error code on failure (e.g. -EINVAL if the hwspinlock
  44. is already free).
  45. Can be called from an atomic context (this function will not sleep) but
  46. not from within interrupt context.
  47. int hwspin_lock_timeout(struct hwspinlock *hwlock, unsigned int timeout);
  48. - lock a previously-assigned hwspinlock with a timeout limit (specified in
  49. msecs). If the hwspinlock is already taken, the function will busy loop
  50. waiting for it to be released, but give up when the timeout elapses.
  51. Upon a successful return from this function, preemption is disabled so
  52. the caller must not sleep, and is advised to release the hwspinlock as
  53. soon as possible, in order to minimize remote cores polling on the
  54. hardware interconnect.
  55. Returns 0 when successful and an appropriate error code otherwise (most
  56. notably -ETIMEDOUT if the hwspinlock is still busy after timeout msecs).
  57. The function will never sleep.
  58. int hwspin_lock_timeout_irq(struct hwspinlock *hwlock, unsigned int timeout);
  59. - lock a previously-assigned hwspinlock with a timeout limit (specified in
  60. msecs). If the hwspinlock is already taken, the function will busy loop
  61. waiting for it to be released, but give up when the timeout elapses.
  62. Upon a successful return from this function, preemption and the local
  63. interrupts are disabled, so the caller must not sleep, and is advised to
  64. release the hwspinlock as soon as possible.
  65. Returns 0 when successful and an appropriate error code otherwise (most
  66. notably -ETIMEDOUT if the hwspinlock is still busy after timeout msecs).
  67. The function will never sleep.
  68. int hwspin_lock_timeout_irqsave(struct hwspinlock *hwlock, unsigned int to,
  69. unsigned long *flags);
  70. - lock a previously-assigned hwspinlock with a timeout limit (specified in
  71. msecs). If the hwspinlock is already taken, the function will busy loop
  72. waiting for it to be released, but give up when the timeout elapses.
  73. Upon a successful return from this function, preemption is disabled,
  74. local interrupts are disabled and their previous state is saved at the
  75. given flags placeholder. The caller must not sleep, and is advised to
  76. release the hwspinlock as soon as possible.
  77. Returns 0 when successful and an appropriate error code otherwise (most
  78. notably -ETIMEDOUT if the hwspinlock is still busy after timeout msecs).
  79. The function will never sleep.
  80. int hwspin_trylock(struct hwspinlock *hwlock);
  81. - attempt to lock a previously-assigned hwspinlock, but immediately fail if
  82. it is already taken.
  83. Upon a successful return from this function, preemption is disabled so
  84. caller must not sleep, and is advised to release the hwspinlock as soon as
  85. possible, in order to minimize remote cores polling on the hardware
  86. interconnect.
  87. Returns 0 on success and an appropriate error code otherwise (most
  88. notably -EBUSY if the hwspinlock was already taken).
  89. The function will never sleep.
  90. int hwspin_trylock_irq(struct hwspinlock *hwlock);
  91. - attempt to lock a previously-assigned hwspinlock, but immediately fail if
  92. it is already taken.
  93. Upon a successful return from this function, preemption and the local
  94. interrupts are disabled so caller must not sleep, and is advised to
  95. release the hwspinlock as soon as possible.
  96. Returns 0 on success and an appropriate error code otherwise (most
  97. notably -EBUSY if the hwspinlock was already taken).
  98. The function will never sleep.
  99. int hwspin_trylock_irqsave(struct hwspinlock *hwlock, unsigned long *flags);
  100. - attempt to lock a previously-assigned hwspinlock, but immediately fail if
  101. it is already taken.
  102. Upon a successful return from this function, preemption is disabled,
  103. the local interrupts are disabled and their previous state is saved
  104. at the given flags placeholder. The caller must not sleep, and is advised
  105. to release the hwspinlock as soon as possible.
  106. Returns 0 on success and an appropriate error code otherwise (most
  107. notably -EBUSY if the hwspinlock was already taken).
  108. The function will never sleep.
  109. void hwspin_unlock(struct hwspinlock *hwlock);
  110. - unlock a previously-locked hwspinlock. Always succeed, and can be called
  111. from any context (the function never sleeps). Note: code should _never_
  112. unlock an hwspinlock which is already unlocked (there is no protection
  113. against this).
  114. void hwspin_unlock_irq(struct hwspinlock *hwlock);
  115. - unlock a previously-locked hwspinlock and enable local interrupts.
  116. The caller should _never_ unlock an hwspinlock which is already unlocked.
  117. Doing so is considered a bug (there is no protection against this).
  118. Upon a successful return from this function, preemption and local
  119. interrupts are enabled. This function will never sleep.
  120. void
  121. hwspin_unlock_irqrestore(struct hwspinlock *hwlock, unsigned long *flags);
  122. - unlock a previously-locked hwspinlock.
  123. The caller should _never_ unlock an hwspinlock which is already unlocked.
  124. Doing so is considered a bug (there is no protection against this).
  125. Upon a successful return from this function, preemption is reenabled,
  126. and the state of the local interrupts is restored to the state saved at
  127. the given flags. This function will never sleep.
  128. int hwspin_lock_get_id(struct hwspinlock *hwlock);
  129. - retrieve id number of a given hwspinlock. This is needed when an
  130. hwspinlock is dynamically assigned: before it can be used to achieve
  131. mutual exclusion with a remote cpu, the id number should be communicated
  132. to the remote task with which we want to synchronize.
  133. Returns the hwspinlock id number, or -EINVAL if hwlock is null.
  134. 3. Typical usage
  135. #include <linux/hwspinlock.h>
  136. #include <linux/err.h>
  137. int hwspinlock_example1(void)
  138. {
  139. struct hwspinlock *hwlock;
  140. int ret;
  141. /* dynamically assign a hwspinlock */
  142. hwlock = hwspin_lock_request();
  143. if (!hwlock)
  144. ...
  145. id = hwspin_lock_get_id(hwlock);
  146. /* probably need to communicate id to a remote processor now */
  147. /* take the lock, spin for 1 sec if it's already taken */
  148. ret = hwspin_lock_timeout(hwlock, 1000);
  149. if (ret)
  150. ...
  151. /*
  152. * we took the lock, do our thing now, but do NOT sleep
  153. */
  154. /* release the lock */
  155. hwspin_unlock(hwlock);
  156. /* free the lock */
  157. ret = hwspin_lock_free(hwlock);
  158. if (ret)
  159. ...
  160. return ret;
  161. }
  162. int hwspinlock_example2(void)
  163. {
  164. struct hwspinlock *hwlock;
  165. int ret;
  166. /*
  167. * assign a specific hwspinlock id - this should be called early
  168. * by board init code.
  169. */
  170. hwlock = hwspin_lock_request_specific(PREDEFINED_LOCK_ID);
  171. if (!hwlock)
  172. ...
  173. /* try to take it, but don't spin on it */
  174. ret = hwspin_trylock(hwlock);
  175. if (!ret) {
  176. pr_info("lock is already taken\n");
  177. return -EBUSY;
  178. }
  179. /*
  180. * we took the lock, do our thing now, but do NOT sleep
  181. */
  182. /* release the lock */
  183. hwspin_unlock(hwlock);
  184. /* free the lock */
  185. ret = hwspin_lock_free(hwlock);
  186. if (ret)
  187. ...
  188. return ret;
  189. }
  190. 4. API for implementors
  191. int hwspin_lock_register(struct hwspinlock *hwlock);
  192. - to be called from the underlying platform-specific implementation, in
  193. order to register a new hwspinlock instance. Can be called from an atomic
  194. context (this function will not sleep) but not from within interrupt
  195. context. Returns 0 on success, or appropriate error code on failure.
  196. struct hwspinlock *hwspin_lock_unregister(unsigned int id);
  197. - to be called from the underlying vendor-specific implementation, in order
  198. to unregister an existing (and unused) hwspinlock instance.
  199. Can be called from an atomic context (will not sleep) but not from
  200. within interrupt context.
  201. Returns the address of hwspinlock on success, or NULL on error (e.g.
  202. if the hwspinlock is sill in use).
  203. 5. struct hwspinlock
  204. This struct represents an hwspinlock instance. It is registered by the
  205. underlying hwspinlock implementation using the hwspin_lock_register() API.
  206. /**
  207. * struct hwspinlock - vendor-specific hwspinlock implementation
  208. *
  209. * @dev: underlying device, will be used with runtime PM api
  210. * @ops: vendor-specific hwspinlock handlers
  211. * @id: a global, unique, system-wide, index of the lock.
  212. * @lock: initialized and used by hwspinlock core
  213. * @owner: underlying implementation module, used to maintain module ref count
  214. */
  215. struct hwspinlock {
  216. struct device *dev;
  217. const struct hwspinlock_ops *ops;
  218. int id;
  219. spinlock_t lock;
  220. struct module *owner;
  221. };
  222. The underlying implementation is responsible to assign the dev, ops, id and
  223. owner members. The lock member, OTOH, is initialized and used by the hwspinlock
  224. core.
  225. 6. Implementation callbacks
  226. There are three possible callbacks defined in 'struct hwspinlock_ops':
  227. struct hwspinlock_ops {
  228. int (*trylock)(struct hwspinlock *lock);
  229. void (*unlock)(struct hwspinlock *lock);
  230. void (*relax)(struct hwspinlock *lock);
  231. };
  232. The first two callbacks are mandatory:
  233. The ->trylock() callback should make a single attempt to take the lock, and
  234. return 0 on failure and 1 on success. This callback may _not_ sleep.
  235. The ->unlock() callback releases the lock. It always succeed, and it, too,
  236. may _not_ sleep.
  237. The ->relax() callback is optional. It is called by hwspinlock core while
  238. spinning on a lock, and can be used by the underlying implementation to force
  239. a delay between two successive invocations of ->trylock(). It may _not_ sleep.