hwspinlock.txt 13 KB

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