bcm_ring.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /*
  2. * bcm_ring.h : Ring context abstraction
  3. * The ring context tracks the WRITE and READ indices where elements may be
  4. * produced and consumed respectively. All elements in the ring need to be
  5. * fixed size.
  6. *
  7. * NOTE: A ring of size N, may only hold N-1 elements.
  8. *
  9. * Portions of this code are copyright (c) 2020 Cypress Semiconductor Corporation
  10. *
  11. * Copyright (C) 1999-2020, Broadcom Corporation
  12. *
  13. * Unless you and Broadcom execute a separate written software license
  14. * agreement governing use of this software, this software is licensed to you
  15. * under the terms of the GNU General Public License version 2 (the "GPL"),
  16. * available at http://www.broadcom.com/licenses/GPLv2.php, with the
  17. * following added to such license:
  18. *
  19. * As a special exception, the copyright holders of this software give you
  20. * permission to link this software with independent modules, and to copy and
  21. * distribute the resulting executable under terms of your choice, provided that
  22. * you also meet, for each linked independent module, the terms and conditions of
  23. * the license of that module. An independent module is a module which is not
  24. * derived from this software. The special exception does not apply to any
  25. * modifications of the software.
  26. *
  27. * Notwithstanding the above, under no circumstances may you combine this
  28. * software in any way with any other Broadcom software provided under a license
  29. * other than the GPL, without Broadcom's express prior written consent.
  30. *
  31. *
  32. * <<Broadcom-WL-IPTag/Open:>>
  33. *
  34. * $Id: bcm_ring.h 700321 2017-05-18 16:09:07Z $
  35. */
  36. #ifndef __bcm_ring_included__
  37. #define __bcm_ring_included__
  38. /*
  39. * API Notes:
  40. *
  41. * Ring manipulation API allows for:
  42. * Pending operations: Often before some work can be completed, it may be
  43. * desired that several resources are available, e.g. space for production in
  44. * a ring. Approaches such as, #1) reserve resources one by one and return them
  45. * if another required resource is not available, or #2) employ a two pass
  46. * algorithm of first testing whether all resources are available, have a
  47. * an impact on performance critical code. The approach taken here is more akin
  48. * to approach #2, where a test for resource availability essentially also
  49. * provides the index for production in an un-committed state.
  50. * The same approach is taken for the consumer side.
  51. *
  52. * - Pending production: Fetch the next index where a ring element may be
  53. * produced. The caller may not commit the WRITE of the element.
  54. * - Pending consumption: Fetch the next index where a ring element may be
  55. * consumed. The caller may not commut the READ of the element.
  56. *
  57. * Producer side API:
  58. * - bcm_ring_is_full : Test whether ring is full
  59. * - bcm_ring_prod : Fetch index where an element may be produced (commit)
  60. * - bcm_ring_prod_pend: Fetch index where an element may be produced (pending)
  61. * - bcm_ring_prod_done: Commit a previous pending produce fetch
  62. * - bcm_ring_prod_avail: Fetch total number free slots eligible for production
  63. *
  64. * Consumer side API:
  65. * - bcm_ring_is_empty : Test whether ring is empty
  66. * - bcm_ring_cons : Fetch index where an element may be consumed (commit)
  67. * - bcm_ring_cons_pend: Fetch index where an element may be consumed (pending)
  68. * - bcm_ring_cons_done: Commit a previous pending consume fetch
  69. * - bcm_ring_cons_avail: Fetch total number elements eligible for consumption
  70. *
  71. * - bcm_ring_sync_read: Sync read offset in peer ring, from local ring
  72. * - bcm_ring_sync_write: Sync write offset in peer ring, from local ring
  73. *
  74. * +----------------------------------------------------------------------------
  75. *
  76. * Design Notes:
  77. * Following items are not tracked in a ring context (design decision)
  78. * - width of a ring element.
  79. * - depth of the ring.
  80. * - base of the buffer, where the elements are stored.
  81. * - count of number of free slots in the ring
  82. *
  83. * Implementation Notes:
  84. * - When BCM_RING_DEBUG is enabled, need explicit bcm_ring_init().
  85. * - BCM_RING_EMPTY and BCM_RING_FULL are (-1)
  86. *
  87. * +----------------------------------------------------------------------------
  88. *
  89. * Usage Notes:
  90. * An application may incarnate a ring of some fixed sized elements, by defining
  91. * - a ring data buffer to store the ring elements.
  92. * - depth of the ring (max number of elements managed by ring context).
  93. * Preferrably, depth may be represented as a constant.
  94. * - width of a ring element: to be used in pointer arithmetic with the ring's
  95. * data buffer base and an index to fetch the ring element.
  96. *
  97. * Use bcm_workq_t to instantiate a pair of workq constructs, one for the
  98. * producer and the other for the consumer, both pointing to the same circular
  99. * buffer. The producer may operate on it's own local workq and flush the write
  100. * index to the consumer. Likewise the consumer may use its local workq and
  101. * flush the read index to the producer. This way we do not repeatedly access
  102. * the peer's context. The two peers may reside on different CPU cores with a
  103. * private L1 data cache.
  104. * +----------------------------------------------------------------------------
  105. *
  106. * Portions of this code are copyright (c) 2020 Cypress Semiconductor Corporation
  107. *
  108. * Copyright (C) 1999-2020, Broadcom Corporation
  109. *
  110. * Unless you and Broadcom execute a separate written software license
  111. * agreement governing use of this software, this software is licensed to you
  112. * under the terms of the GNU General Public License version 2 (the "GPL"),
  113. * available at http://www.broadcom.com/licenses/GPLv2.php, with the
  114. * following added to such license:
  115. *
  116. * As a special exception, the copyright holders of this software give you
  117. * permission to link this software with independent modules, and to copy and
  118. * distribute the resulting executable under terms of your choice, provided that
  119. * you also meet, for each linked independent module, the terms and conditions of
  120. * the license of that module. An independent module is a module which is not
  121. * derived from this software. The special exception does not apply to any
  122. * modifications of the software.
  123. *
  124. * Notwithstanding the above, under no circumstances may you combine this
  125. * software in any way with any other Broadcom software provided under a license
  126. * other than the GPL, without Broadcom's express prior written consent.
  127. *
  128. * $Id: bcm_ring.h 700321 2017-05-18 16:09:07Z $
  129. *
  130. * -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*-
  131. * vim: set ts=4 noet sw=4 tw=80:
  132. *
  133. * +----------------------------------------------------------------------------
  134. */
  135. #ifdef ____cacheline_aligned
  136. #define __ring_aligned ____cacheline_aligned
  137. #else
  138. #define __ring_aligned
  139. #endif // endif
  140. /* Conditional compile for debug */
  141. /* #define BCM_RING_DEBUG */
  142. #define BCM_RING_EMPTY (-1)
  143. #define BCM_RING_FULL (-1)
  144. #define BCM_RING_NULL ((bcm_ring_t *)NULL)
  145. #if defined(BCM_RING_DEBUG)
  146. #define RING_ASSERT(exp) ASSERT(exp)
  147. #define BCM_RING_IS_VALID(ring) (((ring) != BCM_RING_NULL) && \
  148. ((ring)->self == (ring)))
  149. #else /* ! BCM_RING_DEBUG */
  150. #define RING_ASSERT(exp) do {} while (0)
  151. #define BCM_RING_IS_VALID(ring) ((ring) != BCM_RING_NULL)
  152. #endif /* ! BCM_RING_DEBUG */
  153. #define BCM_RING_SIZE_IS_VALID(ring_size) ((ring_size) > 0)
  154. /*
  155. * +----------------------------------------------------------------------------
  156. * Ring Context
  157. * +----------------------------------------------------------------------------
  158. */
  159. typedef struct bcm_ring { /* Ring context */
  160. #if defined(BCM_RING_DEBUG)
  161. struct bcm_ring *self; /* ptr to self for IS VALID test */
  162. #endif /* BCM_RING_DEBUG */
  163. int write __ring_aligned; /* WRITE index in a circular ring */
  164. int read __ring_aligned; /* READ index in a circular ring */
  165. } bcm_ring_t;
  166. static INLINE void bcm_ring_init(bcm_ring_t *ring);
  167. static INLINE void bcm_ring_copy(bcm_ring_t *to, bcm_ring_t *from);
  168. static INLINE bool bcm_ring_is_empty(bcm_ring_t *ring);
  169. static INLINE int __bcm_ring_next_write(bcm_ring_t *ring, const int ring_size);
  170. static INLINE bool __bcm_ring_full(bcm_ring_t *ring, int next_write);
  171. static INLINE bool bcm_ring_is_full(bcm_ring_t *ring, const int ring_size);
  172. static INLINE void bcm_ring_prod_done(bcm_ring_t *ring, int write);
  173. static INLINE int bcm_ring_prod_pend(bcm_ring_t *ring, int *pend_write,
  174. const int ring_size);
  175. static INLINE int bcm_ring_prod(bcm_ring_t *ring, const int ring_size);
  176. static INLINE void bcm_ring_cons_done(bcm_ring_t *ring, int read);
  177. static INLINE int bcm_ring_cons_pend(bcm_ring_t *ring, int *pend_read,
  178. const int ring_size);
  179. static INLINE int bcm_ring_cons(bcm_ring_t *ring, const int ring_size);
  180. static INLINE void bcm_ring_sync_read(bcm_ring_t *peer, const bcm_ring_t *self);
  181. static INLINE void bcm_ring_sync_write(bcm_ring_t *peer, const bcm_ring_t *self);
  182. static INLINE int bcm_ring_prod_avail(const bcm_ring_t *ring,
  183. const int ring_size);
  184. static INLINE int bcm_ring_cons_avail(const bcm_ring_t *ring,
  185. const int ring_size);
  186. static INLINE void bcm_ring_cons_all(bcm_ring_t *ring);
  187. /**
  188. * bcm_ring_init - initialize a ring context.
  189. * @ring: pointer to a ring context
  190. */
  191. static INLINE void
  192. bcm_ring_init(bcm_ring_t *ring)
  193. {
  194. ASSERT(ring != (bcm_ring_t *)NULL);
  195. #if defined(BCM_RING_DEBUG)
  196. ring->self = ring;
  197. #endif /* BCM_RING_DEBUG */
  198. ring->write = 0;
  199. ring->read = 0;
  200. }
  201. /**
  202. * bcm_ring_copy - copy construct a ring
  203. * @to: pointer to the new ring context
  204. * @from: pointer to orig ring context
  205. */
  206. static INLINE void
  207. bcm_ring_copy(bcm_ring_t *to, bcm_ring_t *from)
  208. {
  209. bcm_ring_init(to);
  210. to->write = from->write;
  211. to->read = from->read;
  212. }
  213. /**
  214. * bcm_ring_is_empty - "Boolean" test whether ring is empty.
  215. * @ring: pointer to a ring context
  216. *
  217. * PS. does not return BCM_RING_EMPTY value.
  218. */
  219. static INLINE bool
  220. bcm_ring_is_empty(bcm_ring_t *ring)
  221. {
  222. RING_ASSERT(BCM_RING_IS_VALID(ring));
  223. return (ring->read == ring->write);
  224. }
  225. /**
  226. * __bcm_ring_next_write - determine the index where the next write may occur
  227. * (with wrap-around).
  228. * @ring: pointer to a ring context
  229. * @ring_size: size of the ring
  230. *
  231. * PRIVATE INTERNAL USE ONLY.
  232. */
  233. static INLINE int
  234. __bcm_ring_next_write(bcm_ring_t *ring, const int ring_size)
  235. {
  236. RING_ASSERT(BCM_RING_IS_VALID(ring) && BCM_RING_SIZE_IS_VALID(ring_size));
  237. return ((ring->write + 1) % ring_size);
  238. }
  239. /**
  240. * __bcm_ring_full - support function for ring full test.
  241. * @ring: pointer to a ring context
  242. * @next_write: next location in ring where an element is to be produced
  243. *
  244. * PRIVATE INTERNAL USE ONLY.
  245. */
  246. static INLINE bool
  247. __bcm_ring_full(bcm_ring_t *ring, int next_write)
  248. {
  249. return (next_write == ring->read);
  250. }
  251. /**
  252. * bcm_ring_is_full - "Boolean" test whether a ring is full.
  253. * @ring: pointer to a ring context
  254. * @ring_size: size of the ring
  255. *
  256. * PS. does not return BCM_RING_FULL value.
  257. */
  258. static INLINE bool
  259. bcm_ring_is_full(bcm_ring_t *ring, const int ring_size)
  260. {
  261. int next_write;
  262. RING_ASSERT(BCM_RING_IS_VALID(ring) && BCM_RING_SIZE_IS_VALID(ring_size));
  263. next_write = __bcm_ring_next_write(ring, ring_size);
  264. return __bcm_ring_full(ring, next_write);
  265. }
  266. /**
  267. * bcm_ring_prod_done - commit a previously pending index where production
  268. * was requested.
  269. * @ring: pointer to a ring context
  270. * @write: index into ring upto where production was done.
  271. * +----------------------------------------------------------------------------
  272. */
  273. static INLINE void
  274. bcm_ring_prod_done(bcm_ring_t *ring, int write)
  275. {
  276. RING_ASSERT(BCM_RING_IS_VALID(ring));
  277. ring->write = write;
  278. }
  279. /**
  280. * bcm_ring_prod_pend - Fetch in "pend" mode, the index where an element may be
  281. * produced.
  282. * @ring: pointer to a ring context
  283. * @pend_write: next index, after the returned index
  284. * @ring_size: size of the ring
  285. */
  286. static INLINE int
  287. bcm_ring_prod_pend(bcm_ring_t *ring, int *pend_write, const int ring_size)
  288. {
  289. int rtn;
  290. RING_ASSERT(BCM_RING_IS_VALID(ring) && BCM_RING_SIZE_IS_VALID(ring_size));
  291. *pend_write = __bcm_ring_next_write(ring, ring_size);
  292. if (__bcm_ring_full(ring, *pend_write)) {
  293. *pend_write = BCM_RING_FULL;
  294. rtn = BCM_RING_FULL;
  295. } else {
  296. /* production is not committed, caller needs to explicitly commit */
  297. rtn = ring->write;
  298. }
  299. return rtn;
  300. }
  301. /**
  302. * bcm_ring_prod - Fetch and "commit" the next index where a ring element may
  303. * be produced.
  304. * @ring: pointer to a ring context
  305. * @ring_size: size of the ring
  306. */
  307. static INLINE int
  308. bcm_ring_prod(bcm_ring_t *ring, const int ring_size)
  309. {
  310. int next_write, prod_write;
  311. RING_ASSERT(BCM_RING_IS_VALID(ring) && BCM_RING_SIZE_IS_VALID(ring_size));
  312. next_write = __bcm_ring_next_write(ring, ring_size);
  313. if (__bcm_ring_full(ring, next_write)) {
  314. prod_write = BCM_RING_FULL;
  315. } else {
  316. prod_write = ring->write;
  317. bcm_ring_prod_done(ring, next_write); /* "commit" production */
  318. }
  319. return prod_write;
  320. }
  321. /**
  322. * bcm_ring_cons_done - commit a previously pending read
  323. * @ring: pointer to a ring context
  324. * @read: index upto which elements have been consumed.
  325. */
  326. static INLINE void
  327. bcm_ring_cons_done(bcm_ring_t *ring, int read)
  328. {
  329. RING_ASSERT(BCM_RING_IS_VALID(ring));
  330. ring->read = read;
  331. }
  332. /**
  333. * bcm_ring_cons_pend - fetch in "pend" mode, the next index where a ring
  334. * element may be consumed.
  335. * @ring: pointer to a ring context
  336. * @pend_read: index into ring upto which elements may be consumed.
  337. * @ring_size: size of the ring
  338. */
  339. static INLINE int
  340. bcm_ring_cons_pend(bcm_ring_t *ring, int *pend_read, const int ring_size)
  341. {
  342. int rtn;
  343. RING_ASSERT(BCM_RING_IS_VALID(ring) && BCM_RING_SIZE_IS_VALID(ring_size));
  344. if (bcm_ring_is_empty(ring)) {
  345. *pend_read = BCM_RING_EMPTY;
  346. rtn = BCM_RING_EMPTY;
  347. } else {
  348. *pend_read = (ring->read + 1) % ring_size;
  349. /* production is not committed, caller needs to explicitly commit */
  350. rtn = ring->read;
  351. }
  352. return rtn;
  353. }
  354. /**
  355. * bcm_ring_cons - fetch and "commit" the next index where a ring element may
  356. * be consumed.
  357. * @ring: pointer to a ring context
  358. * @ring_size: size of the ring
  359. */
  360. static INLINE int
  361. bcm_ring_cons(bcm_ring_t *ring, const int ring_size)
  362. {
  363. int cons_read;
  364. RING_ASSERT(BCM_RING_IS_VALID(ring) && BCM_RING_SIZE_IS_VALID(ring_size));
  365. if (bcm_ring_is_empty(ring)) {
  366. cons_read = BCM_RING_EMPTY;
  367. } else {
  368. cons_read = ring->read;
  369. ring->read = (ring->read + 1) % ring_size; /* read is committed */
  370. }
  371. return cons_read;
  372. }
  373. /**
  374. * bcm_ring_sync_read - on consumption, update peer's read index.
  375. * @peer: pointer to peer's producer ring context
  376. * @self: pointer to consumer's ring context
  377. */
  378. static INLINE void
  379. bcm_ring_sync_read(bcm_ring_t *peer, const bcm_ring_t *self)
  380. {
  381. RING_ASSERT(BCM_RING_IS_VALID(peer));
  382. RING_ASSERT(BCM_RING_IS_VALID(self));
  383. peer->read = self->read; /* flush read update to peer producer */
  384. }
  385. /**
  386. * bcm_ring_sync_write - on consumption, update peer's write index.
  387. * @peer: pointer to peer's consumer ring context
  388. * @self: pointer to producer's ring context
  389. */
  390. static INLINE void
  391. bcm_ring_sync_write(bcm_ring_t *peer, const bcm_ring_t *self)
  392. {
  393. RING_ASSERT(BCM_RING_IS_VALID(peer));
  394. RING_ASSERT(BCM_RING_IS_VALID(self));
  395. peer->write = self->write; /* flush write update to peer consumer */
  396. }
  397. /**
  398. * bcm_ring_prod_avail - fetch total number of available empty slots in the
  399. * ring for production.
  400. * @ring: pointer to a ring context
  401. * @ring_size: size of the ring
  402. */
  403. static INLINE int
  404. bcm_ring_prod_avail(const bcm_ring_t *ring, const int ring_size)
  405. {
  406. int prod_avail;
  407. RING_ASSERT(BCM_RING_IS_VALID(ring) && BCM_RING_SIZE_IS_VALID(ring_size));
  408. if (ring->write >= ring->read) {
  409. prod_avail = (ring_size - (ring->write - ring->read) - 1);
  410. } else {
  411. prod_avail = (ring->read - (ring->write + 1));
  412. }
  413. ASSERT(prod_avail < ring_size);
  414. return prod_avail;
  415. }
  416. /**
  417. * bcm_ring_cons_avail - fetch total number of available elements for consumption.
  418. * @ring: pointer to a ring context
  419. * @ring_size: size of the ring
  420. */
  421. static INLINE int
  422. bcm_ring_cons_avail(const bcm_ring_t *ring, const int ring_size)
  423. {
  424. int cons_avail;
  425. RING_ASSERT(BCM_RING_IS_VALID(ring) && BCM_RING_SIZE_IS_VALID(ring_size));
  426. if (ring->read == ring->write) {
  427. cons_avail = 0;
  428. } else if (ring->read > ring->write) {
  429. cons_avail = ((ring_size - ring->read) + ring->write);
  430. } else {
  431. cons_avail = ring->write - ring->read;
  432. }
  433. ASSERT(cons_avail < ring_size);
  434. return cons_avail;
  435. }
  436. /**
  437. * bcm_ring_cons_all - set ring in state where all elements are consumed.
  438. * @ring: pointer to a ring context
  439. */
  440. static INLINE void
  441. bcm_ring_cons_all(bcm_ring_t *ring)
  442. {
  443. ring->read = ring->write;
  444. }
  445. /**
  446. * Work Queue
  447. * A work Queue is composed of a ring of work items, of a specified depth.
  448. * It HAS-A bcm_ring object, comprising of a RD and WR offset, to implement a
  449. * producer/consumer circular ring.
  450. */
  451. struct bcm_workq {
  452. bcm_ring_t ring; /* Ring context abstraction */
  453. struct bcm_workq *peer; /* Peer workq context */
  454. void *buffer; /* Buffer storage for work items in workQ */
  455. int ring_size; /* Depth of workQ */
  456. } __ring_aligned;
  457. typedef struct bcm_workq bcm_workq_t;
  458. /* #define BCM_WORKQ_DEBUG */
  459. #if defined(BCM_WORKQ_DEBUG)
  460. #define WORKQ_ASSERT(exp) ASSERT(exp)
  461. #else /* ! BCM_WORKQ_DEBUG */
  462. #define WORKQ_ASSERT(exp) do {} while (0)
  463. #endif /* ! BCM_WORKQ_DEBUG */
  464. #define WORKQ_AUDIT(workq) \
  465. WORKQ_ASSERT((workq) != BCM_WORKQ_NULL); \
  466. WORKQ_ASSERT(WORKQ_PEER(workq) != BCM_WORKQ_NULL); \
  467. WORKQ_ASSERT((workq)->buffer == WORKQ_PEER(workq)->buffer); \
  468. WORKQ_ASSERT((workq)->ring_size == WORKQ_PEER(workq)->ring_size);
  469. #define BCM_WORKQ_NULL ((bcm_workq_t *)NULL)
  470. #define WORKQ_PEER(workq) ((workq)->peer)
  471. #define WORKQ_RING(workq) (&((workq)->ring))
  472. #define WORKQ_PEER_RING(workq) (&((workq)->peer->ring))
  473. #define WORKQ_ELEMENT(__elem_type, __workq, __index) ({ \
  474. WORKQ_ASSERT((__workq) != BCM_WORKQ_NULL); \
  475. WORKQ_ASSERT((__index) < ((__workq)->ring_size)); \
  476. ((__elem_type *)((__workq)->buffer)) + (__index); \
  477. })
  478. static INLINE void bcm_workq_init(bcm_workq_t *workq, bcm_workq_t *workq_peer,
  479. void *buffer, int ring_size);
  480. static INLINE bool bcm_workq_is_empty(bcm_workq_t *workq_prod);
  481. static INLINE void bcm_workq_prod_sync(bcm_workq_t *workq_prod);
  482. static INLINE void bcm_workq_cons_sync(bcm_workq_t *workq_cons);
  483. static INLINE void bcm_workq_prod_refresh(bcm_workq_t *workq_prod);
  484. static INLINE void bcm_workq_cons_refresh(bcm_workq_t *workq_cons);
  485. /**
  486. * bcm_workq_init - initialize a workq
  487. * @workq: pointer to a workq context
  488. * @buffer: pointer to a pre-allocated circular buffer to serve as a ring
  489. * @ring_size: size of the ring in terms of max number of elements.
  490. */
  491. static INLINE void
  492. bcm_workq_init(bcm_workq_t *workq, bcm_workq_t *workq_peer,
  493. void *buffer, int ring_size)
  494. {
  495. ASSERT(workq != BCM_WORKQ_NULL);
  496. ASSERT(workq_peer != BCM_WORKQ_NULL);
  497. ASSERT(buffer != NULL);
  498. ASSERT(ring_size > 0);
  499. WORKQ_PEER(workq) = workq_peer;
  500. WORKQ_PEER(workq_peer) = workq;
  501. bcm_ring_init(WORKQ_RING(workq));
  502. bcm_ring_init(WORKQ_RING(workq_peer));
  503. workq->buffer = workq_peer->buffer = buffer;
  504. workq->ring_size = workq_peer->ring_size = ring_size;
  505. }
  506. /**
  507. * bcm_workq_empty - test whether there is work
  508. * @workq_prod: producer's workq
  509. */
  510. static INLINE bool
  511. bcm_workq_is_empty(bcm_workq_t *workq_prod)
  512. {
  513. return bcm_ring_is_empty(WORKQ_RING(workq_prod));
  514. }
  515. /**
  516. * bcm_workq_prod_sync - Commit the producer write index to peer workq's ring
  517. * @workq_prod: producer's workq whose write index must be synced to peer
  518. */
  519. static INLINE void
  520. bcm_workq_prod_sync(bcm_workq_t *workq_prod)
  521. {
  522. WORKQ_AUDIT(workq_prod);
  523. /* cons::write <--- prod::write */
  524. bcm_ring_sync_write(WORKQ_PEER_RING(workq_prod), WORKQ_RING(workq_prod));
  525. }
  526. /**
  527. * bcm_workq_cons_sync - Commit the consumer read index to the peer workq's ring
  528. * @workq_cons: consumer's workq whose read index must be synced to peer
  529. */
  530. static INLINE void
  531. bcm_workq_cons_sync(bcm_workq_t *workq_cons)
  532. {
  533. WORKQ_AUDIT(workq_cons);
  534. /* prod::read <--- cons::read */
  535. bcm_ring_sync_read(WORKQ_PEER_RING(workq_cons), WORKQ_RING(workq_cons));
  536. }
  537. /**
  538. * bcm_workq_prod_refresh - Fetch the updated consumer's read index
  539. * @workq_prod: producer's workq whose read index must be refreshed from peer
  540. */
  541. static INLINE void
  542. bcm_workq_prod_refresh(bcm_workq_t *workq_prod)
  543. {
  544. WORKQ_AUDIT(workq_prod);
  545. /* prod::read <--- cons::read */
  546. bcm_ring_sync_read(WORKQ_RING(workq_prod), WORKQ_PEER_RING(workq_prod));
  547. }
  548. /**
  549. * bcm_workq_cons_refresh - Fetch the updated producer's write index
  550. * @workq_cons: consumer's workq whose write index must be refreshed from peer
  551. */
  552. static INLINE void
  553. bcm_workq_cons_refresh(bcm_workq_t *workq_cons)
  554. {
  555. WORKQ_AUDIT(workq_cons);
  556. /* cons::write <--- prod::write */
  557. bcm_ring_sync_write(WORKQ_RING(workq_cons), WORKQ_PEER_RING(workq_cons));
  558. }
  559. #endif /* ! __bcm_ring_h_included__ */