hwspinlock.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. Should be called from a process context (might sleep).
  33. struct hwspinlock *hwspin_lock_request_specific(unsigned int id);
  34. - assign a specific hwspinlock id and return its address, or NULL
  35. if that hwspinlock is already in use. Usually board code will
  36. be calling this function in order to reserve specific hwspinlock
  37. ids for predefined purposes.
  38. Should be called from a process context (might sleep).
  39. int hwspin_lock_free(struct hwspinlock *hwlock);
  40. - free a previously-assigned hwspinlock; returns 0 on success, or an
  41. appropriate error code on failure (e.g. -EINVAL if the hwspinlock
  42. is already free).
  43. Should be called from a process context (might sleep).
  44. int hwspin_lock_timeout(struct hwspinlock *hwlock, unsigned int timeout);
  45. - lock a previously-assigned hwspinlock with a timeout limit (specified in
  46. msecs). If the hwspinlock is already taken, the function will busy loop
  47. waiting for it to be released, but give up when the timeout elapses.
  48. Upon a successful return from this function, preemption is disabled so
  49. the caller must not sleep, and is advised to release the hwspinlock as
  50. soon as possible, in order to minimize remote cores polling on the
  51. hardware interconnect.
  52. Returns 0 when successful and an appropriate error code otherwise (most
  53. notably -ETIMEDOUT if the hwspinlock is still busy after timeout msecs).
  54. The function will never sleep.
  55. int hwspin_lock_timeout_irq(struct hwspinlock *hwlock, unsigned int timeout);
  56. - lock a previously-assigned hwspinlock with a timeout limit (specified in
  57. msecs). If the hwspinlock is already taken, the function will busy loop
  58. waiting for it to be released, but give up when the timeout elapses.
  59. Upon a successful return from this function, preemption and the local
  60. interrupts are disabled, so the caller must not sleep, and is advised to
  61. release the hwspinlock as soon as possible.
  62. Returns 0 when successful and an appropriate error code otherwise (most
  63. notably -ETIMEDOUT if the hwspinlock is still busy after timeout msecs).
  64. The function will never sleep.
  65. int hwspin_lock_timeout_irqsave(struct hwspinlock *hwlock, unsigned int to,
  66. unsigned long *flags);
  67. - lock a previously-assigned hwspinlock with a timeout limit (specified in
  68. msecs). If the hwspinlock is already taken, the function will busy loop
  69. waiting for it to be released, but give up when the timeout elapses.
  70. Upon a successful return from this function, preemption is disabled,
  71. local interrupts are disabled and their previous state is saved at the
  72. given flags placeholder. The caller must not sleep, and is advised to
  73. release the hwspinlock as soon as possible.
  74. Returns 0 when successful and an appropriate error code otherwise (most
  75. notably -ETIMEDOUT if the hwspinlock is still busy after timeout msecs).
  76. The function will never sleep.
  77. int hwspin_trylock(struct hwspinlock *hwlock);
  78. - attempt to lock a previously-assigned hwspinlock, but immediately fail if
  79. it is already taken.
  80. Upon a successful return from this function, preemption is disabled so
  81. caller must not sleep, and is advised to release the hwspinlock as soon as
  82. possible, in order to minimize remote cores polling on the hardware
  83. interconnect.
  84. Returns 0 on success and an appropriate error code otherwise (most
  85. notably -EBUSY if the hwspinlock was already taken).
  86. The function will never sleep.
  87. int hwspin_trylock_irq(struct hwspinlock *hwlock);
  88. - attempt to lock a previously-assigned hwspinlock, but immediately fail if
  89. it is already taken.
  90. Upon a successful return from this function, preemption and the local
  91. interrupts are disabled so caller must not sleep, and is advised to
  92. release the hwspinlock as soon as possible.
  93. Returns 0 on success and an appropriate error code otherwise (most
  94. notably -EBUSY if the hwspinlock was already taken).
  95. The function will never sleep.
  96. int hwspin_trylock_irqsave(struct hwspinlock *hwlock, unsigned long *flags);
  97. - attempt to lock a previously-assigned hwspinlock, but immediately fail if
  98. it is already taken.
  99. Upon a successful return from this function, preemption is disabled,
  100. the local interrupts are disabled and their previous state is saved
  101. at the given flags placeholder. The caller must not sleep, and is advised
  102. to release the hwspinlock as soon as possible.
  103. Returns 0 on success and an appropriate error code otherwise (most
  104. notably -EBUSY if the hwspinlock was already taken).
  105. The function will never sleep.
  106. void hwspin_unlock(struct hwspinlock *hwlock);
  107. - unlock a previously-locked hwspinlock. Always succeed, and can be called
  108. from any context (the function never sleeps). Note: code should _never_
  109. unlock an hwspinlock which is already unlocked (there is no protection
  110. against this).
  111. void hwspin_unlock_irq(struct hwspinlock *hwlock);
  112. - unlock a previously-locked hwspinlock and enable local interrupts.
  113. The caller should _never_ unlock an hwspinlock which is already unlocked.
  114. Doing so is considered a bug (there is no protection against this).
  115. Upon a successful return from this function, preemption and local
  116. interrupts are enabled. This function will never sleep.
  117. void
  118. hwspin_unlock_irqrestore(struct hwspinlock *hwlock, unsigned long *flags);
  119. - unlock a previously-locked hwspinlock.
  120. The caller should _never_ unlock an hwspinlock which is already unlocked.
  121. Doing so is considered a bug (there is no protection against this).
  122. Upon a successful return from this function, preemption is reenabled,
  123. and the state of the local interrupts is restored to the state saved at
  124. the given flags. This function will never sleep.
  125. int hwspin_lock_get_id(struct hwspinlock *hwlock);
  126. - retrieve id number of a given hwspinlock. This is needed when an
  127. hwspinlock is dynamically assigned: before it can be used to achieve
  128. mutual exclusion with a remote cpu, the id number should be communicated
  129. to the remote task with which we want to synchronize.
  130. Returns the hwspinlock id number, or -EINVAL if hwlock is null.
  131. 3. Typical usage
  132. #include <linux/hwspinlock.h>
  133. #include <linux/err.h>
  134. int hwspinlock_example1(void)
  135. {
  136. struct hwspinlock *hwlock;
  137. int ret;
  138. /* dynamically assign a hwspinlock */
  139. hwlock = hwspin_lock_request();
  140. if (!hwlock)
  141. ...
  142. id = hwspin_lock_get_id(hwlock);
  143. /* probably need to communicate id to a remote processor now */
  144. /* take the lock, spin for 1 sec if it's already taken */
  145. ret = hwspin_lock_timeout(hwlock, 1000);
  146. if (ret)
  147. ...
  148. /*
  149. * we took the lock, do our thing now, but do NOT sleep
  150. */
  151. /* release the lock */
  152. hwspin_unlock(hwlock);
  153. /* free the lock */
  154. ret = hwspin_lock_free(hwlock);
  155. if (ret)
  156. ...
  157. return ret;
  158. }
  159. int hwspinlock_example2(void)
  160. {
  161. struct hwspinlock *hwlock;
  162. int ret;
  163. /*
  164. * assign a specific hwspinlock id - this should be called early
  165. * by board init code.
  166. */
  167. hwlock = hwspin_lock_request_specific(PREDEFINED_LOCK_ID);
  168. if (!hwlock)
  169. ...
  170. /* try to take it, but don't spin on it */
  171. ret = hwspin_trylock(hwlock);
  172. if (!ret) {
  173. pr_info("lock is already taken\n");
  174. return -EBUSY;
  175. }
  176. /*
  177. * we took the lock, do our thing now, but do NOT sleep
  178. */
  179. /* release the lock */
  180. hwspin_unlock(hwlock);
  181. /* free the lock */
  182. ret = hwspin_lock_free(hwlock);
  183. if (ret)
  184. ...
  185. return ret;
  186. }
  187. 4. API for implementors
  188. int hwspin_lock_register(struct hwspinlock_device *bank, struct device *dev,
  189. const struct hwspinlock_ops *ops, int base_id, int num_locks);
  190. - to be called from the underlying platform-specific implementation, in
  191. order to register a new hwspinlock device (which is usually a bank of
  192. numerous locks). Should be called from a process context (this function
  193. might sleep).
  194. Returns 0 on success, or appropriate error code on failure.
  195. int hwspin_lock_unregister(struct hwspinlock_device *bank);
  196. - to be called from the underlying vendor-specific implementation, in order
  197. to unregister an hwspinlock device (which is usually a bank of numerous
  198. locks).
  199. Should be called from a process context (this function might sleep).
  200. Returns the address of hwspinlock on success, or NULL on error (e.g.
  201. if the hwspinlock is sill in use).
  202. 5. Important structs
  203. struct hwspinlock_device is a device which usually contains a bank
  204. of hardware locks. It is registered by the underlying hwspinlock
  205. implementation using the hwspin_lock_register() API.
  206. /**
  207. * struct hwspinlock_device - a device which usually spans numerous hwspinlocks
  208. * @dev: underlying device, will be used to invoke runtime PM api
  209. * @ops: platform-specific hwspinlock handlers
  210. * @base_id: id index of the first lock in this device
  211. * @num_locks: number of locks in this device
  212. * @lock: dynamically allocated array of 'struct hwspinlock'
  213. */
  214. struct hwspinlock_device {
  215. struct device *dev;
  216. const struct hwspinlock_ops *ops;
  217. int base_id;
  218. int num_locks;
  219. struct hwspinlock lock[0];
  220. };
  221. struct hwspinlock_device contains an array of hwspinlock structs, each
  222. of which represents a single hardware lock:
  223. /**
  224. * struct hwspinlock - this struct represents a single hwspinlock instance
  225. * @bank: the hwspinlock_device structure which owns this lock
  226. * @lock: initialized and used by hwspinlock core
  227. * @priv: private data, owned by the underlying platform-specific hwspinlock drv
  228. */
  229. struct hwspinlock {
  230. struct hwspinlock_device *bank;
  231. spinlock_t lock;
  232. void *priv;
  233. };
  234. When registering a bank of locks, the hwspinlock driver only needs to
  235. set the priv members of the locks. The rest of the members are set and
  236. initialized by the hwspinlock core itself.
  237. 6. Implementation callbacks
  238. There are three possible callbacks defined in 'struct hwspinlock_ops':
  239. struct hwspinlock_ops {
  240. int (*trylock)(struct hwspinlock *lock);
  241. void (*unlock)(struct hwspinlock *lock);
  242. void (*relax)(struct hwspinlock *lock);
  243. };
  244. The first two callbacks are mandatory:
  245. The ->trylock() callback should make a single attempt to take the lock, and
  246. return 0 on failure and 1 on success. This callback may _not_ sleep.
  247. The ->unlock() callback releases the lock. It always succeed, and it, too,
  248. may _not_ sleep.
  249. The ->relax() callback is optional. It is called by hwspinlock core while
  250. spinning on a lock, and can be used by the underlying implementation to force
  251. a delay between two successive invocations of ->trylock(). It may _not_ sleep.