hwspinlock.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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_bust(struct hwspinlock *hwlock, unsigned int id);
  63. After verifying the owner of the hwspinlock, release a previously acquired
  64. hwspinlock; returns 0 on success, or an appropriate error code on failure
  65. (e.g. -EOPNOTSUPP if the bust operation is not defined for the specific
  66. hwspinlock).
  67. Should be called from a process context (might sleep).
  68. ::
  69. int hwspin_lock_timeout(struct hwspinlock *hwlock, unsigned int timeout);
  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 so
  74. the caller must not sleep, and is advised to release the hwspinlock as
  75. soon as possible, in order to minimize remote cores polling on the
  76. hardware interconnect.
  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. ::
  81. int hwspin_lock_timeout_irq(struct hwspinlock *hwlock, unsigned int timeout);
  82. Lock a previously-assigned hwspinlock with a timeout limit (specified in
  83. msecs). If the hwspinlock is already taken, the function will busy loop
  84. waiting for it to be released, but give up when the timeout elapses.
  85. Upon a successful return from this function, preemption and the local
  86. interrupts are disabled, so the caller must not sleep, and is advised to
  87. release the hwspinlock as soon as possible.
  88. Returns 0 when successful and an appropriate error code otherwise (most
  89. notably -ETIMEDOUT if the hwspinlock is still busy after timeout msecs).
  90. The function will never sleep.
  91. ::
  92. int hwspin_lock_timeout_irqsave(struct hwspinlock *hwlock, unsigned int to,
  93. unsigned long *flags);
  94. Lock a previously-assigned hwspinlock with a timeout limit (specified in
  95. msecs). If the hwspinlock is already taken, the function will busy loop
  96. waiting for it to be released, but give up when the timeout elapses.
  97. Upon a successful return from this function, preemption is disabled,
  98. local interrupts are disabled and their previous state is saved at the
  99. given flags placeholder. The caller must not sleep, and is advised to
  100. release the hwspinlock as soon as possible.
  101. Returns 0 when successful and an appropriate error code otherwise (most
  102. notably -ETIMEDOUT if the hwspinlock is still busy after timeout msecs).
  103. The function will never sleep.
  104. ::
  105. int hwspin_lock_timeout_raw(struct hwspinlock *hwlock, unsigned int timeout);
  106. Lock a previously-assigned hwspinlock with a timeout limit (specified in
  107. msecs). If the hwspinlock is already taken, the function will busy loop
  108. waiting for it to be released, but give up when the timeout elapses.
  109. Caution: User must protect the routine of getting hardware lock with mutex
  110. or spinlock to avoid dead-lock, that will let user can do some time-consuming
  111. or sleepable operations under the hardware lock.
  112. Returns 0 when successful and an appropriate error code otherwise (most
  113. notably -ETIMEDOUT if the hwspinlock is still busy after timeout msecs).
  114. The function will never sleep.
  115. ::
  116. int hwspin_lock_timeout_in_atomic(struct hwspinlock *hwlock, unsigned int to);
  117. Lock a previously-assigned hwspinlock with a timeout limit (specified in
  118. msecs). If the hwspinlock is already taken, the function will busy loop
  119. waiting for it to be released, but give up when the timeout elapses.
  120. This function shall be called only from an atomic context and the timeout
  121. value shall not exceed a few msecs.
  122. Returns 0 when successful and an appropriate error code otherwise (most
  123. notably -ETIMEDOUT if the hwspinlock is still busy after timeout msecs).
  124. The function will never sleep.
  125. ::
  126. int hwspin_trylock(struct hwspinlock *hwlock);
  127. Attempt to lock a previously-assigned hwspinlock, but immediately fail if
  128. it is already taken.
  129. Upon a successful return from this function, preemption is disabled so
  130. caller must not sleep, and is advised to release the hwspinlock as soon as
  131. possible, in order to minimize remote cores polling on the hardware
  132. interconnect.
  133. Returns 0 on success and an appropriate error code otherwise (most
  134. notably -EBUSY if the hwspinlock was already taken).
  135. The function will never sleep.
  136. ::
  137. int hwspin_trylock_irq(struct hwspinlock *hwlock);
  138. Attempt to lock a previously-assigned hwspinlock, but immediately fail if
  139. it is already taken.
  140. Upon a successful return from this function, preemption and the local
  141. interrupts are disabled so caller must not sleep, and is advised to
  142. release the hwspinlock as soon as possible.
  143. Returns 0 on success and an appropriate error code otherwise (most
  144. notably -EBUSY if the hwspinlock was already taken).
  145. The function will never sleep.
  146. ::
  147. int hwspin_trylock_irqsave(struct hwspinlock *hwlock, unsigned long *flags);
  148. Attempt to lock a previously-assigned hwspinlock, but immediately fail if
  149. it is already taken.
  150. Upon a successful return from this function, preemption is disabled,
  151. the local interrupts are disabled and their previous state is saved
  152. at the given flags placeholder. The caller must not sleep, and is advised
  153. to release the hwspinlock as soon as possible.
  154. Returns 0 on success and an appropriate error code otherwise (most
  155. notably -EBUSY if the hwspinlock was already taken).
  156. The function will never sleep.
  157. ::
  158. int hwspin_trylock_raw(struct hwspinlock *hwlock);
  159. Attempt to lock a previously-assigned hwspinlock, but immediately fail if
  160. it is already taken.
  161. Caution: User must protect the routine of getting hardware lock with mutex
  162. or spinlock to avoid dead-lock, that will let user can do some time-consuming
  163. or sleepable operations under the hardware lock.
  164. Returns 0 on success and an appropriate error code otherwise (most
  165. notably -EBUSY if the hwspinlock was already taken).
  166. The function will never sleep.
  167. ::
  168. int hwspin_trylock_in_atomic(struct hwspinlock *hwlock);
  169. Attempt to lock a previously-assigned hwspinlock, but immediately fail if
  170. it is already taken.
  171. This function shall be called only from an atomic context.
  172. Returns 0 on success and an appropriate error code otherwise (most
  173. notably -EBUSY if the hwspinlock was already taken).
  174. The function will never sleep.
  175. ::
  176. void hwspin_unlock(struct hwspinlock *hwlock);
  177. Unlock a previously-locked hwspinlock. Always succeed, and can be called
  178. from any context (the function never sleeps).
  179. .. note::
  180. code should **never** unlock an hwspinlock which is already unlocked
  181. (there is no protection against this).
  182. ::
  183. void hwspin_unlock_irq(struct hwspinlock *hwlock);
  184. Unlock a previously-locked hwspinlock and enable local interrupts.
  185. The caller should **never** unlock an hwspinlock which is already unlocked.
  186. Doing so is considered a bug (there is no protection against this).
  187. Upon a successful return from this function, preemption and local
  188. interrupts are enabled. This function will never sleep.
  189. ::
  190. void
  191. hwspin_unlock_irqrestore(struct hwspinlock *hwlock, unsigned long *flags);
  192. Unlock a previously-locked hwspinlock.
  193. The caller should **never** unlock an hwspinlock which is already unlocked.
  194. Doing so is considered a bug (there is no protection against this).
  195. Upon a successful return from this function, preemption is reenabled,
  196. and the state of the local interrupts is restored to the state saved at
  197. the given flags. This function will never sleep.
  198. ::
  199. void hwspin_unlock_raw(struct hwspinlock *hwlock);
  200. Unlock a previously-locked hwspinlock.
  201. The caller should **never** unlock an hwspinlock which is already unlocked.
  202. Doing so is considered a bug (there is no protection against this).
  203. This function will never sleep.
  204. ::
  205. void hwspin_unlock_in_atomic(struct hwspinlock *hwlock);
  206. Unlock a previously-locked hwspinlock.
  207. The caller should **never** unlock an hwspinlock which is already unlocked.
  208. Doing so is considered a bug (there is no protection against this).
  209. This function will never sleep.
  210. ::
  211. int hwspin_lock_get_id(struct hwspinlock *hwlock);
  212. Retrieve id number of a given hwspinlock. This is needed when an
  213. hwspinlock is dynamically assigned: before it can be used to achieve
  214. mutual exclusion with a remote cpu, the id number should be communicated
  215. to the remote task with which we want to synchronize.
  216. Returns the hwspinlock id number, or -EINVAL if hwlock is null.
  217. Typical usage
  218. =============
  219. ::
  220. #include <linux/hwspinlock.h>
  221. #include <linux/err.h>
  222. int hwspinlock_example1(void)
  223. {
  224. struct hwspinlock *hwlock;
  225. int ret;
  226. /* dynamically assign a hwspinlock */
  227. hwlock = hwspin_lock_request();
  228. if (!hwlock)
  229. ...
  230. id = hwspin_lock_get_id(hwlock);
  231. /* probably need to communicate id to a remote processor now */
  232. /* take the lock, spin for 1 sec if it's already taken */
  233. ret = hwspin_lock_timeout(hwlock, 1000);
  234. if (ret)
  235. ...
  236. /*
  237. * we took the lock, do our thing now, but do NOT sleep
  238. */
  239. /* release the lock */
  240. hwspin_unlock(hwlock);
  241. /* free the lock */
  242. ret = hwspin_lock_free(hwlock);
  243. if (ret)
  244. ...
  245. return ret;
  246. }
  247. int hwspinlock_example2(void)
  248. {
  249. struct hwspinlock *hwlock;
  250. int ret;
  251. /*
  252. * assign a specific hwspinlock id - this should be called early
  253. * by board init code.
  254. */
  255. hwlock = hwspin_lock_request_specific(PREDEFINED_LOCK_ID);
  256. if (!hwlock)
  257. ...
  258. /* try to take it, but don't spin on it */
  259. ret = hwspin_trylock(hwlock);
  260. if (!ret) {
  261. pr_info("lock is already taken\n");
  262. return -EBUSY;
  263. }
  264. /*
  265. * we took the lock, do our thing now, but do NOT sleep
  266. */
  267. /* release the lock */
  268. hwspin_unlock(hwlock);
  269. /* free the lock */
  270. ret = hwspin_lock_free(hwlock);
  271. if (ret)
  272. ...
  273. return ret;
  274. }
  275. API for implementors
  276. ====================
  277. ::
  278. int hwspin_lock_register(struct hwspinlock_device *bank, struct device *dev,
  279. const struct hwspinlock_ops *ops, int base_id, int num_locks);
  280. To be called from the underlying platform-specific implementation, in
  281. order to register a new hwspinlock device (which is usually a bank of
  282. numerous locks). Should be called from a process context (this function
  283. might sleep).
  284. Returns 0 on success, or appropriate error code on failure.
  285. ::
  286. int hwspin_lock_unregister(struct hwspinlock_device *bank);
  287. To be called from the underlying vendor-specific implementation, in order
  288. to unregister an hwspinlock device (which is usually a bank of numerous
  289. locks).
  290. Should be called from a process context (this function might sleep).
  291. Returns the address of hwspinlock on success, or NULL on error (e.g.
  292. if the hwspinlock is still in use).
  293. Important structs
  294. =================
  295. struct hwspinlock_device is a device which usually contains a bank
  296. of hardware locks. It is registered by the underlying hwspinlock
  297. implementation using the hwspin_lock_register() API.
  298. ::
  299. /**
  300. * struct hwspinlock_device - a device which usually spans numerous hwspinlocks
  301. * @dev: underlying device, will be used to invoke runtime PM api
  302. * @ops: platform-specific hwspinlock handlers
  303. * @base_id: id index of the first lock in this device
  304. * @num_locks: number of locks in this device
  305. * @lock: dynamically allocated array of 'struct hwspinlock'
  306. */
  307. struct hwspinlock_device {
  308. struct device *dev;
  309. const struct hwspinlock_ops *ops;
  310. int base_id;
  311. int num_locks;
  312. struct hwspinlock lock[0];
  313. };
  314. struct hwspinlock_device contains an array of hwspinlock structs, each
  315. of which represents a single hardware lock::
  316. /**
  317. * struct hwspinlock - this struct represents a single hwspinlock instance
  318. * @bank: the hwspinlock_device structure which owns this lock
  319. * @lock: initialized and used by hwspinlock core
  320. * @priv: private data, owned by the underlying platform-specific hwspinlock drv
  321. */
  322. struct hwspinlock {
  323. struct hwspinlock_device *bank;
  324. spinlock_t lock;
  325. void *priv;
  326. };
  327. When registering a bank of locks, the hwspinlock driver only needs to
  328. set the priv members of the locks. The rest of the members are set and
  329. initialized by the hwspinlock core itself.
  330. Implementation callbacks
  331. ========================
  332. There are three possible callbacks defined in 'struct hwspinlock_ops'::
  333. struct hwspinlock_ops {
  334. int (*trylock)(struct hwspinlock *lock);
  335. void (*unlock)(struct hwspinlock *lock);
  336. void (*relax)(struct hwspinlock *lock);
  337. };
  338. The first two callbacks are mandatory:
  339. The ->trylock() callback should make a single attempt to take the lock, and
  340. return 0 on failure and 1 on success. This callback may **not** sleep.
  341. The ->unlock() callback releases the lock. It always succeed, and it, too,
  342. may **not** sleep.
  343. The ->relax() callback is optional. It is called by hwspinlock core while
  344. spinning on a lock, and can be used by the underlying implementation to force
  345. a delay between two successive invocations of ->trylock(). It may **not** sleep.