hwspinlock.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. ===========================
  2. Hardware Spinlock Framework
  3. ===========================
  4. Introduction
  5. ============
  6. Hardware spinlock modules provide hardware assistance for synchronization
  7. and mutual exclusion between heterogeneous processors and those not operating
  8. under a single, shared operating system.
  9. For example, OMAP4 has dual Cortex-A9, dual Cortex-M3 and a C64x+ DSP,
  10. each of which is running a different Operating System (the master, A9,
  11. is usually running Linux and the slave processors, the M3 and the DSP,
  12. are running some flavor of RTOS).
  13. A generic hwspinlock framework allows platform-independent drivers to use
  14. the hwspinlock device in order to access data structures that are shared
  15. between remote processors, that otherwise have no alternative mechanism
  16. to accomplish synchronization and mutual exclusion operations.
  17. This is necessary, for example, for Inter-processor communications:
  18. on OMAP4, cpu-intensive multimedia tasks are offloaded by the host to the
  19. remote M3 and/or C64x+ slave processors (by an IPC subsystem called Syslink).
  20. To achieve fast message-based communications, a minimal kernel support
  21. is needed to deliver messages arriving from a remote processor to the
  22. appropriate user process.
  23. This communication is based on simple data structures that is shared between
  24. the remote processors, and access to it is synchronized using the hwspinlock
  25. module (remote processor directly places new messages in this shared data
  26. structure).
  27. A common hwspinlock interface makes it possible to have generic, platform-
  28. independent, drivers.
  29. User API
  30. ========
  31. ::
  32. struct hwspinlock *hwspin_lock_request_specific(unsigned int id);
  33. Assign a specific hwspinlock id and return its address, or NULL
  34. if that hwspinlock is already in use. Usually board code will
  35. be calling this function in order to reserve specific hwspinlock
  36. ids for predefined purposes.
  37. Should be called from a process context (might sleep).
  38. ::
  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. ::
  49. int hwspin_lock_free(struct hwspinlock *hwlock);
  50. Free a previously-assigned hwspinlock; returns 0 on success, or an
  51. appropriate error code on failure (e.g. -EINVAL if the hwspinlock
  52. is already free).
  53. Should be called from a process context (might sleep).
  54. ::
  55. int hwspin_lock_bust(struct hwspinlock *hwlock, unsigned int id);
  56. After verifying the owner of the hwspinlock, release a previously acquired
  57. hwspinlock; returns 0 on success, or an appropriate error code on failure
  58. (e.g. -EOPNOTSUPP if the bust operation is not defined for the specific
  59. hwspinlock).
  60. Should be called from a process context (might sleep).
  61. ::
  62. int hwspin_lock_timeout(struct hwspinlock *hwlock, unsigned int timeout);
  63. Lock a previously-assigned hwspinlock with a timeout limit (specified in
  64. msecs). If the hwspinlock is already taken, the function will busy loop
  65. waiting for it to be released, but give up when the timeout elapses.
  66. Upon a successful return from this function, preemption is disabled so
  67. the caller must not sleep, and is advised to release the hwspinlock as
  68. soon as possible, in order to minimize remote cores polling on the
  69. hardware interconnect.
  70. Returns 0 when successful and an appropriate error code otherwise (most
  71. notably -ETIMEDOUT if the hwspinlock is still busy after timeout msecs).
  72. The function will never sleep.
  73. ::
  74. int hwspin_lock_timeout_irq(struct hwspinlock *hwlock, unsigned int timeout);
  75. Lock a previously-assigned hwspinlock with a timeout limit (specified in
  76. msecs). If the hwspinlock is already taken, the function will busy loop
  77. waiting for it to be released, but give up when the timeout elapses.
  78. Upon a successful return from this function, preemption and the local
  79. interrupts are disabled, so the caller must not sleep, and is advised to
  80. release the hwspinlock as soon as possible.
  81. Returns 0 when successful and an appropriate error code otherwise (most
  82. notably -ETIMEDOUT if the hwspinlock is still busy after timeout msecs).
  83. The function will never sleep.
  84. ::
  85. int hwspin_lock_timeout_irqsave(struct hwspinlock *hwlock, unsigned int to,
  86. unsigned long *flags);
  87. Lock a previously-assigned hwspinlock with a timeout limit (specified in
  88. msecs). If the hwspinlock is already taken, the function will busy loop
  89. waiting for it to be released, but give up when the timeout elapses.
  90. Upon a successful return from this function, preemption is disabled,
  91. local interrupts are disabled and their previous state is saved at the
  92. given flags placeholder. The caller must not sleep, and is advised to
  93. release the hwspinlock as soon as possible.
  94. Returns 0 when successful and an appropriate error code otherwise (most
  95. notably -ETIMEDOUT if the hwspinlock is still busy after timeout msecs).
  96. The function will never sleep.
  97. ::
  98. int hwspin_lock_timeout_raw(struct hwspinlock *hwlock, unsigned int timeout);
  99. Lock a previously-assigned hwspinlock with a timeout limit (specified in
  100. msecs). If the hwspinlock is already taken, the function will busy loop
  101. waiting for it to be released, but give up when the timeout elapses.
  102. Caution: User must protect the routine of getting hardware lock with mutex
  103. or spinlock to avoid dead-lock, that will let user can do some time-consuming
  104. or sleepable operations under the hardware lock.
  105. Returns 0 when successful and an appropriate error code otherwise (most
  106. notably -ETIMEDOUT if the hwspinlock is still busy after timeout msecs).
  107. The function will never sleep.
  108. ::
  109. int hwspin_lock_timeout_in_atomic(struct hwspinlock *hwlock, unsigned int to);
  110. Lock a previously-assigned hwspinlock with a timeout limit (specified in
  111. msecs). If the hwspinlock is already taken, the function will busy loop
  112. waiting for it to be released, but give up when the timeout elapses.
  113. This function shall be called only from an atomic context and the timeout
  114. value shall not exceed a few msecs.
  115. Returns 0 when successful and an appropriate error code otherwise (most
  116. notably -ETIMEDOUT if the hwspinlock is still busy after timeout msecs).
  117. The function will never sleep.
  118. ::
  119. int hwspin_trylock(struct hwspinlock *hwlock);
  120. Attempt to lock a previously-assigned hwspinlock, but immediately fail if
  121. it is already taken.
  122. Upon a successful return from this function, preemption is disabled so
  123. caller must not sleep, and is advised to release the hwspinlock as soon as
  124. possible, in order to minimize remote cores polling on the hardware
  125. interconnect.
  126. Returns 0 on success and an appropriate error code otherwise (most
  127. notably -EBUSY if the hwspinlock was already taken).
  128. The function will never sleep.
  129. ::
  130. int hwspin_trylock_irq(struct hwspinlock *hwlock);
  131. Attempt to lock a previously-assigned hwspinlock, but immediately fail if
  132. it is already taken.
  133. Upon a successful return from this function, preemption and the local
  134. interrupts are disabled so caller must not sleep, and is advised to
  135. release the hwspinlock as soon as possible.
  136. Returns 0 on success and an appropriate error code otherwise (most
  137. notably -EBUSY if the hwspinlock was already taken).
  138. The function will never sleep.
  139. ::
  140. int hwspin_trylock_irqsave(struct hwspinlock *hwlock, unsigned long *flags);
  141. Attempt to lock a previously-assigned hwspinlock, but immediately fail if
  142. it is already taken.
  143. Upon a successful return from this function, preemption is disabled,
  144. the local interrupts are disabled and their previous state is saved
  145. at the given flags placeholder. The caller must not sleep, and is advised
  146. to release the hwspinlock as soon as possible.
  147. Returns 0 on success and an appropriate error code otherwise (most
  148. notably -EBUSY if the hwspinlock was already taken).
  149. The function will never sleep.
  150. ::
  151. int hwspin_trylock_raw(struct hwspinlock *hwlock);
  152. Attempt to lock a previously-assigned hwspinlock, but immediately fail if
  153. it is already taken.
  154. Caution: User must protect the routine of getting hardware lock with mutex
  155. or spinlock to avoid dead-lock, that will let user can do some time-consuming
  156. or sleepable operations under the hardware lock.
  157. Returns 0 on success and an appropriate error code otherwise (most
  158. notably -EBUSY if the hwspinlock was already taken).
  159. The function will never sleep.
  160. ::
  161. int hwspin_trylock_in_atomic(struct hwspinlock *hwlock);
  162. Attempt to lock a previously-assigned hwspinlock, but immediately fail if
  163. it is already taken.
  164. This function shall be called only from an atomic context.
  165. Returns 0 on success and an appropriate error code otherwise (most
  166. notably -EBUSY if the hwspinlock was already taken).
  167. The function will never sleep.
  168. ::
  169. void hwspin_unlock(struct hwspinlock *hwlock);
  170. Unlock a previously-locked hwspinlock. Always succeed, and can be called
  171. from any context (the function never sleeps).
  172. .. note::
  173. code should **never** unlock an hwspinlock which is already unlocked
  174. (there is no protection against this).
  175. ::
  176. void hwspin_unlock_irq(struct hwspinlock *hwlock);
  177. Unlock a previously-locked hwspinlock and enable local interrupts.
  178. The caller should **never** unlock an hwspinlock which is already unlocked.
  179. Doing so is considered a bug (there is no protection against this).
  180. Upon a successful return from this function, preemption and local
  181. interrupts are enabled. This function will never sleep.
  182. ::
  183. void
  184. hwspin_unlock_irqrestore(struct hwspinlock *hwlock, unsigned long *flags);
  185. Unlock a previously-locked hwspinlock.
  186. The caller should **never** unlock an hwspinlock which is already unlocked.
  187. Doing so is considered a bug (there is no protection against this).
  188. Upon a successful return from this function, preemption is reenabled,
  189. and the state of the local interrupts is restored to the state saved at
  190. the given flags. This function will never sleep.
  191. ::
  192. void hwspin_unlock_raw(struct hwspinlock *hwlock);
  193. Unlock a previously-locked hwspinlock.
  194. The caller should **never** unlock an hwspinlock which is already unlocked.
  195. Doing so is considered a bug (there is no protection against this).
  196. This function will never sleep.
  197. ::
  198. void hwspin_unlock_in_atomic(struct hwspinlock *hwlock);
  199. Unlock a previously-locked hwspinlock.
  200. The caller should **never** unlock an hwspinlock which is already unlocked.
  201. Doing so is considered a bug (there is no protection against this).
  202. This function will never sleep.
  203. Typical usage
  204. =============
  205. ::
  206. #include <linux/hwspinlock.h>
  207. #include <linux/err.h>
  208. int hwspinlock_example(void)
  209. {
  210. struct hwspinlock *hwlock;
  211. int ret;
  212. /*
  213. * assign a specific hwspinlock id - this should be called early
  214. * by board init code.
  215. */
  216. hwlock = hwspin_lock_request_specific(PREDEFINED_LOCK_ID);
  217. if (!hwlock)
  218. ...
  219. /* try to take it, but don't spin on it */
  220. ret = hwspin_trylock(hwlock);
  221. if (!ret) {
  222. pr_info("lock is already taken\n");
  223. return -EBUSY;
  224. }
  225. /*
  226. * we took the lock, do our thing now, but do NOT sleep
  227. */
  228. /* release the lock */
  229. hwspin_unlock(hwlock);
  230. /* free the lock */
  231. ret = hwspin_lock_free(hwlock);
  232. if (ret)
  233. ...
  234. return ret;
  235. }
  236. API for implementors
  237. ====================
  238. ::
  239. int hwspin_lock_register(struct hwspinlock_device *bank, struct device *dev,
  240. const struct hwspinlock_ops *ops, int base_id, int num_locks);
  241. To be called from the underlying platform-specific implementation, in
  242. order to register a new hwspinlock device (which is usually a bank of
  243. numerous locks). Should be called from a process context (this function
  244. might sleep).
  245. Returns 0 on success, or appropriate error code on failure.
  246. ::
  247. int hwspin_lock_unregister(struct hwspinlock_device *bank);
  248. To be called from the underlying vendor-specific implementation, in order
  249. to unregister an hwspinlock device (which is usually a bank of numerous
  250. locks).
  251. Should be called from a process context (this function might sleep).
  252. Returns the address of hwspinlock on success, or NULL on error (e.g.
  253. if the hwspinlock is still in use).
  254. Important structs
  255. =================
  256. struct hwspinlock_device is a device which usually contains a bank
  257. of hardware locks. It is registered by the underlying hwspinlock
  258. implementation using the hwspin_lock_register() API.
  259. ::
  260. /**
  261. * struct hwspinlock_device - a device which usually spans numerous hwspinlocks
  262. * @dev: underlying device, will be used to invoke runtime PM api
  263. * @ops: platform-specific hwspinlock handlers
  264. * @base_id: id index of the first lock in this device
  265. * @num_locks: number of locks in this device
  266. * @lock: dynamically allocated array of 'struct hwspinlock'
  267. */
  268. struct hwspinlock_device {
  269. struct device *dev;
  270. const struct hwspinlock_ops *ops;
  271. int base_id;
  272. int num_locks;
  273. struct hwspinlock lock[0];
  274. };
  275. struct hwspinlock_device contains an array of hwspinlock structs, each
  276. of which represents a single hardware lock::
  277. /**
  278. * struct hwspinlock - this struct represents a single hwspinlock instance
  279. * @bank: the hwspinlock_device structure which owns this lock
  280. * @lock: initialized and used by hwspinlock core
  281. * @priv: private data, owned by the underlying platform-specific hwspinlock drv
  282. */
  283. struct hwspinlock {
  284. struct hwspinlock_device *bank;
  285. spinlock_t lock;
  286. void *priv;
  287. };
  288. When registering a bank of locks, the hwspinlock driver only needs to
  289. set the priv members of the locks. The rest of the members are set and
  290. initialized by the hwspinlock core itself.
  291. Implementation callbacks
  292. ========================
  293. There are three possible callbacks defined in 'struct hwspinlock_ops'::
  294. struct hwspinlock_ops {
  295. int (*trylock)(struct hwspinlock *lock);
  296. void (*unlock)(struct hwspinlock *lock);
  297. void (*relax)(struct hwspinlock *lock);
  298. };
  299. The first two callbacks are mandatory:
  300. The ->trylock() callback should make a single attempt to take the lock, and
  301. return 0 on failure and 1 on success. This callback may **not** sleep.
  302. The ->unlock() callback releases the lock. It always succeed, and it, too,
  303. may **not** sleep.
  304. The ->relax() callback is optional. It is called by hwspinlock core while
  305. spinning on a lock, and can be used by the underlying implementation to force
  306. a delay between two successive invocations of ->trylock(). It may **not** sleep.