hwspinlock.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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(void);
  33. Dynamically assign an hwspinlock and return its address, or NULL
  34. in case an unused hwspinlock isn't available. Users of this
  35. API will usually want to communicate the lock's id to the remote core
  36. before it can be used to achieve synchronization.
  37. Should be called from a process context (might sleep).
  38. ::
  39. struct hwspinlock *hwspin_lock_request_specific(unsigned int id);
  40. Assign a specific hwspinlock id and return its address, or NULL
  41. if that hwspinlock is already in use. Usually board code will
  42. be calling this function in order to reserve specific hwspinlock
  43. ids for predefined purposes.
  44. Should be called from a process context (might sleep).
  45. ::
  46. int of_hwspin_lock_get_id(struct device_node *np, int index);
  47. Retrieve the global lock id for an OF phandle-based specific lock.
  48. This function provides a means for DT users of a hwspinlock module
  49. to get the global lock id of a specific hwspinlock, so that it can
  50. be requested using the normal hwspin_lock_request_specific() API.
  51. The function returns a lock id number on success, -EPROBE_DEFER if
  52. the hwspinlock device is not yet registered with the core, or other
  53. error values.
  54. Should be called from a process context (might sleep).
  55. ::
  56. int hwspin_lock_free(struct hwspinlock *hwlock);
  57. Free a previously-assigned hwspinlock; returns 0 on success, or an
  58. appropriate error code on failure (e.g. -EINVAL if the hwspinlock
  59. is already free).
  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. ::
  204. int hwspin_lock_get_id(struct hwspinlock *hwlock);
  205. Retrieve id number of a given hwspinlock. This is needed when an
  206. hwspinlock is dynamically assigned: before it can be used to achieve
  207. mutual exclusion with a remote cpu, the id number should be communicated
  208. to the remote task with which we want to synchronize.
  209. Returns the hwspinlock id number, or -EINVAL if hwlock is null.
  210. Typical usage
  211. =============
  212. ::
  213. #include <linux/hwspinlock.h>
  214. #include <linux/err.h>
  215. int hwspinlock_example1(void)
  216. {
  217. struct hwspinlock *hwlock;
  218. int ret;
  219. /* dynamically assign a hwspinlock */
  220. hwlock = hwspin_lock_request();
  221. if (!hwlock)
  222. ...
  223. id = hwspin_lock_get_id(hwlock);
  224. /* probably need to communicate id to a remote processor now */
  225. /* take the lock, spin for 1 sec if it's already taken */
  226. ret = hwspin_lock_timeout(hwlock, 1000);
  227. if (ret)
  228. ...
  229. /*
  230. * we took the lock, do our thing now, but do NOT sleep
  231. */
  232. /* release the lock */
  233. hwspin_unlock(hwlock);
  234. /* free the lock */
  235. ret = hwspin_lock_free(hwlock);
  236. if (ret)
  237. ...
  238. return ret;
  239. }
  240. int hwspinlock_example2(void)
  241. {
  242. struct hwspinlock *hwlock;
  243. int ret;
  244. /*
  245. * assign a specific hwspinlock id - this should be called early
  246. * by board init code.
  247. */
  248. hwlock = hwspin_lock_request_specific(PREDEFINED_LOCK_ID);
  249. if (!hwlock)
  250. ...
  251. /* try to take it, but don't spin on it */
  252. ret = hwspin_trylock(hwlock);
  253. if (!ret) {
  254. pr_info("lock is already taken\n");
  255. return -EBUSY;
  256. }
  257. /*
  258. * we took the lock, do our thing now, but do NOT sleep
  259. */
  260. /* release the lock */
  261. hwspin_unlock(hwlock);
  262. /* free the lock */
  263. ret = hwspin_lock_free(hwlock);
  264. if (ret)
  265. ...
  266. return ret;
  267. }
  268. API for implementors
  269. ====================
  270. ::
  271. int hwspin_lock_register(struct hwspinlock_device *bank, struct device *dev,
  272. const struct hwspinlock_ops *ops, int base_id, int num_locks);
  273. To be called from the underlying platform-specific implementation, in
  274. order to register a new hwspinlock device (which is usually a bank of
  275. numerous locks). Should be called from a process context (this function
  276. might sleep).
  277. Returns 0 on success, or appropriate error code on failure.
  278. ::
  279. int hwspin_lock_unregister(struct hwspinlock_device *bank);
  280. To be called from the underlying vendor-specific implementation, in order
  281. to unregister an hwspinlock device (which is usually a bank of numerous
  282. locks).
  283. Should be called from a process context (this function might sleep).
  284. Returns the address of hwspinlock on success, or NULL on error (e.g.
  285. if the hwspinlock is still in use).
  286. Important structs
  287. =================
  288. struct hwspinlock_device is a device which usually contains a bank
  289. of hardware locks. It is registered by the underlying hwspinlock
  290. implementation using the hwspin_lock_register() API.
  291. ::
  292. /**
  293. * struct hwspinlock_device - a device which usually spans numerous hwspinlocks
  294. * @dev: underlying device, will be used to invoke runtime PM api
  295. * @ops: platform-specific hwspinlock handlers
  296. * @base_id: id index of the first lock in this device
  297. * @num_locks: number of locks in this device
  298. * @lock: dynamically allocated array of 'struct hwspinlock'
  299. */
  300. struct hwspinlock_device {
  301. struct device *dev;
  302. const struct hwspinlock_ops *ops;
  303. int base_id;
  304. int num_locks;
  305. struct hwspinlock lock[0];
  306. };
  307. struct hwspinlock_device contains an array of hwspinlock structs, each
  308. of which represents a single hardware lock::
  309. /**
  310. * struct hwspinlock - this struct represents a single hwspinlock instance
  311. * @bank: the hwspinlock_device structure which owns this lock
  312. * @lock: initialized and used by hwspinlock core
  313. * @priv: private data, owned by the underlying platform-specific hwspinlock drv
  314. */
  315. struct hwspinlock {
  316. struct hwspinlock_device *bank;
  317. spinlock_t lock;
  318. void *priv;
  319. };
  320. When registering a bank of locks, the hwspinlock driver only needs to
  321. set the priv members of the locks. The rest of the members are set and
  322. initialized by the hwspinlock core itself.
  323. Implementation callbacks
  324. ========================
  325. There are three possible callbacks defined in 'struct hwspinlock_ops'::
  326. struct hwspinlock_ops {
  327. int (*trylock)(struct hwspinlock *lock);
  328. void (*unlock)(struct hwspinlock *lock);
  329. void (*relax)(struct hwspinlock *lock);
  330. };
  331. The first two callbacks are mandatory:
  332. The ->trylock() callback should make a single attempt to take the lock, and
  333. return 0 on failure and 1 on success. This callback may **not** sleep.
  334. The ->unlock() callback releases the lock. It always succeed, and it, too,
  335. may **not** sleep.
  336. The ->relax() callback is optional. It is called by hwspinlock core while
  337. spinning on a lock, and can be used by the underlying implementation to force
  338. a delay between two successive invocations of ->trylock(). It may **not** sleep.