ring.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /******************************************************************************
  3. * ring.h
  4. *
  5. * Shared producer-consumer ring macros.
  6. *
  7. * Tim Deegan and Andrew Warfield November 2004.
  8. */
  9. #ifndef __XEN_PUBLIC_IO_RING_H__
  10. #define __XEN_PUBLIC_IO_RING_H__
  11. #include <xen/interface/grant_table.h>
  12. typedef unsigned int RING_IDX;
  13. /* Round a 32-bit unsigned constant down to the nearest power of two. */
  14. #define __RD2(_x) (((_x) & 0x00000002) ? 0x2 : ((_x) & 0x1))
  15. #define __RD4(_x) (((_x) & 0x0000000c) ? __RD2((_x)>>2)<<2 : __RD2(_x))
  16. #define __RD8(_x) (((_x) & 0x000000f0) ? __RD4((_x)>>4)<<4 : __RD4(_x))
  17. #define __RD16(_x) (((_x) & 0x0000ff00) ? __RD8((_x)>>8)<<8 : __RD8(_x))
  18. #define __RD32(_x) (((_x) & 0xffff0000) ? __RD16((_x)>>16)<<16 : __RD16(_x))
  19. /*
  20. * Calculate size of a shared ring, given the total available space for the
  21. * ring and indexes (_sz), and the name tag of the request/response structure.
  22. * A ring contains as many entries as will fit, rounded down to the nearest
  23. * power of two (so we can mask with (size-1) to loop around).
  24. */
  25. #define __CONST_RING_SIZE(_s, _sz) \
  26. (__RD32(((_sz) - offsetof(struct _s##_sring, ring)) / \
  27. sizeof(((struct _s##_sring *)0)->ring[0])))
  28. /*
  29. * The same for passing in an actual pointer instead of a name tag.
  30. */
  31. #define __RING_SIZE(_s, _sz) \
  32. (__RD32(((_sz) - (long)&(_s)->ring + (long)(_s)) / sizeof((_s)->ring[0])))
  33. /*
  34. * Macros to make the correct C datatypes for a new kind of ring.
  35. *
  36. * To make a new ring datatype, you need to have two message structures,
  37. * let's say struct request, and struct response already defined.
  38. *
  39. * In a header where you want the ring datatype declared, you then do:
  40. *
  41. * DEFINE_RING_TYPES(mytag, struct request, struct response);
  42. *
  43. * These expand out to give you a set of types, as you can see below.
  44. * The most important of these are:
  45. *
  46. * struct mytag_sring - The shared ring.
  47. * struct mytag_front_ring - The 'front' half of the ring.
  48. * struct mytag_back_ring - The 'back' half of the ring.
  49. *
  50. * To initialize a ring in your code you need to know the location and size
  51. * of the shared memory area (PAGE_SIZE, for instance). To initialise
  52. * the front half:
  53. *
  54. * struct mytag_front_ring front_ring;
  55. * SHARED_RING_INIT((struct mytag_sring *)shared_page);
  56. * FRONT_RING_INIT(&front_ring, (struct mytag_sring *)shared_page,
  57. * PAGE_SIZE);
  58. *
  59. * Initializing the back follows similarly (note that only the front
  60. * initializes the shared ring):
  61. *
  62. * struct mytag_back_ring back_ring;
  63. * BACK_RING_INIT(&back_ring, (struct mytag_sring *)shared_page,
  64. * PAGE_SIZE);
  65. */
  66. #define DEFINE_RING_TYPES(__name, __req_t, __rsp_t) \
  67. \
  68. /* Shared ring entry */ \
  69. union __name##_sring_entry { \
  70. __req_t req; \
  71. __rsp_t rsp; \
  72. }; \
  73. \
  74. /* Shared ring page */ \
  75. struct __name##_sring { \
  76. RING_IDX req_prod, req_event; \
  77. RING_IDX rsp_prod, rsp_event; \
  78. uint8_t pad[48]; \
  79. union __name##_sring_entry ring[1]; /* variable-length */ \
  80. }; \
  81. \
  82. /* "Front" end's private variables */ \
  83. struct __name##_front_ring { \
  84. RING_IDX req_prod_pvt; \
  85. RING_IDX rsp_cons; \
  86. unsigned int nr_ents; \
  87. struct __name##_sring *sring; \
  88. }; \
  89. \
  90. /* "Back" end's private variables */ \
  91. struct __name##_back_ring { \
  92. RING_IDX rsp_prod_pvt; \
  93. RING_IDX req_cons; \
  94. unsigned int nr_ents; \
  95. struct __name##_sring *sring; \
  96. };
  97. /*
  98. * Macros for manipulating rings.
  99. *
  100. * FRONT_RING_whatever works on the "front end" of a ring: here
  101. * requests are pushed on to the ring and responses taken off it.
  102. *
  103. * BACK_RING_whatever works on the "back end" of a ring: here
  104. * requests are taken off the ring and responses put on.
  105. *
  106. * N.B. these macros do NO INTERLOCKS OR FLOW CONTROL.
  107. * This is OK in 1-for-1 request-response situations where the
  108. * requestor (front end) never has more than RING_SIZE()-1
  109. * outstanding requests.
  110. */
  111. /* Initialising empty rings */
  112. #define SHARED_RING_INIT(_s) do { \
  113. (_s)->req_prod = (_s)->rsp_prod = 0; \
  114. (_s)->req_event = (_s)->rsp_event = 1; \
  115. memset((_s)->pad, 0, sizeof((_s)->pad)); \
  116. } while(0)
  117. #define FRONT_RING_INIT(_r, _s, __size) do { \
  118. (_r)->req_prod_pvt = 0; \
  119. (_r)->rsp_cons = 0; \
  120. (_r)->nr_ents = __RING_SIZE(_s, __size); \
  121. (_r)->sring = (_s); \
  122. } while (0)
  123. #define BACK_RING_INIT(_r, _s, __size) do { \
  124. (_r)->rsp_prod_pvt = 0; \
  125. (_r)->req_cons = 0; \
  126. (_r)->nr_ents = __RING_SIZE(_s, __size); \
  127. (_r)->sring = (_s); \
  128. } while (0)
  129. /* Initialize to existing shared indexes -- for recovery */
  130. #define FRONT_RING_ATTACH(_r, _s, __size) do { \
  131. (_r)->sring = (_s); \
  132. (_r)->req_prod_pvt = (_s)->req_prod; \
  133. (_r)->rsp_cons = (_s)->rsp_prod; \
  134. (_r)->nr_ents = __RING_SIZE(_s, __size); \
  135. } while (0)
  136. #define BACK_RING_ATTACH(_r, _s, __size) do { \
  137. (_r)->sring = (_s); \
  138. (_r)->rsp_prod_pvt = (_s)->rsp_prod; \
  139. (_r)->req_cons = (_s)->req_prod; \
  140. (_r)->nr_ents = __RING_SIZE(_s, __size); \
  141. } while (0)
  142. /* How big is this ring? */
  143. #define RING_SIZE(_r) \
  144. ((_r)->nr_ents)
  145. /* Number of free requests (for use on front side only). */
  146. #define RING_FREE_REQUESTS(_r) \
  147. (RING_SIZE(_r) - ((_r)->req_prod_pvt - (_r)->rsp_cons))
  148. /* Test if there is an empty slot available on the front ring.
  149. * (This is only meaningful from the front. )
  150. */
  151. #define RING_FULL(_r) \
  152. (RING_FREE_REQUESTS(_r) == 0)
  153. /* Test if there are outstanding messages to be processed on a ring. */
  154. #define RING_HAS_UNCONSUMED_RESPONSES(_r) \
  155. ((_r)->sring->rsp_prod - (_r)->rsp_cons)
  156. #define RING_HAS_UNCONSUMED_REQUESTS(_r) \
  157. ({ \
  158. unsigned int req = (_r)->sring->req_prod - (_r)->req_cons; \
  159. unsigned int rsp = RING_SIZE(_r) - \
  160. ((_r)->req_cons - (_r)->rsp_prod_pvt); \
  161. req < rsp ? req : rsp; \
  162. })
  163. /* Direct access to individual ring elements, by index. */
  164. #define RING_GET_REQUEST(_r, _idx) \
  165. (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req))
  166. /*
  167. * Get a local copy of a request.
  168. *
  169. * Use this in preference to RING_GET_REQUEST() so all processing is
  170. * done on a local copy that cannot be modified by the other end.
  171. *
  172. * Note that https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58145 may cause this
  173. * to be ineffective where _req is a struct which consists of only bitfields.
  174. */
  175. #define RING_COPY_REQUEST(_r, _idx, _req) do { \
  176. /* Use volatile to force the copy into _req. */ \
  177. *(_req) = *(volatile typeof(_req))RING_GET_REQUEST(_r, _idx); \
  178. } while (0)
  179. #define RING_GET_RESPONSE(_r, _idx) \
  180. (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp))
  181. /* Loop termination condition: Would the specified index overflow the ring? */
  182. #define RING_REQUEST_CONS_OVERFLOW(_r, _cons) \
  183. (((_cons) - (_r)->rsp_prod_pvt) >= RING_SIZE(_r))
  184. /* Ill-behaved frontend determination: Can there be this many requests? */
  185. #define RING_REQUEST_PROD_OVERFLOW(_r, _prod) \
  186. (((_prod) - (_r)->rsp_prod_pvt) > RING_SIZE(_r))
  187. #define RING_PUSH_REQUESTS(_r) do { \
  188. virt_wmb(); /* back sees requests /before/ updated producer index */ \
  189. (_r)->sring->req_prod = (_r)->req_prod_pvt; \
  190. } while (0)
  191. #define RING_PUSH_RESPONSES(_r) do { \
  192. virt_wmb(); /* front sees responses /before/ updated producer index */ \
  193. (_r)->sring->rsp_prod = (_r)->rsp_prod_pvt; \
  194. } while (0)
  195. /*
  196. * Notification hold-off (req_event and rsp_event):
  197. *
  198. * When queueing requests or responses on a shared ring, it may not always be
  199. * necessary to notify the remote end. For example, if requests are in flight
  200. * in a backend, the front may be able to queue further requests without
  201. * notifying the back (if the back checks for new requests when it queues
  202. * responses).
  203. *
  204. * When enqueuing requests or responses:
  205. *
  206. * Use RING_PUSH_{REQUESTS,RESPONSES}_AND_CHECK_NOTIFY(). The second argument
  207. * is a boolean return value. True indicates that the receiver requires an
  208. * asynchronous notification.
  209. *
  210. * After dequeuing requests or responses (before sleeping the connection):
  211. *
  212. * Use RING_FINAL_CHECK_FOR_REQUESTS() or RING_FINAL_CHECK_FOR_RESPONSES().
  213. * The second argument is a boolean return value. True indicates that there
  214. * are pending messages on the ring (i.e., the connection should not be put
  215. * to sleep).
  216. *
  217. * These macros will set the req_event/rsp_event field to trigger a
  218. * notification on the very next message that is enqueued. If you want to
  219. * create batches of work (i.e., only receive a notification after several
  220. * messages have been enqueued) then you will need to create a customised
  221. * version of the FINAL_CHECK macro in your own code, which sets the event
  222. * field appropriately.
  223. */
  224. #define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do { \
  225. RING_IDX __old = (_r)->sring->req_prod; \
  226. RING_IDX __new = (_r)->req_prod_pvt; \
  227. virt_wmb(); /* back sees requests /before/ updated producer index */ \
  228. (_r)->sring->req_prod = __new; \
  229. virt_mb(); /* back sees new requests /before/ we check req_event */ \
  230. (_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) < \
  231. (RING_IDX)(__new - __old)); \
  232. } while (0)
  233. #define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do { \
  234. RING_IDX __old = (_r)->sring->rsp_prod; \
  235. RING_IDX __new = (_r)->rsp_prod_pvt; \
  236. virt_wmb(); /* front sees responses /before/ updated producer index */ \
  237. (_r)->sring->rsp_prod = __new; \
  238. virt_mb(); /* front sees new responses /before/ we check rsp_event */ \
  239. (_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) < \
  240. (RING_IDX)(__new - __old)); \
  241. } while (0)
  242. #define RING_FINAL_CHECK_FOR_REQUESTS(_r, _work_to_do) do { \
  243. (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \
  244. if (_work_to_do) break; \
  245. (_r)->sring->req_event = (_r)->req_cons + 1; \
  246. virt_mb(); \
  247. (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \
  248. } while (0)
  249. #define RING_FINAL_CHECK_FOR_RESPONSES(_r, _work_to_do) do { \
  250. (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \
  251. if (_work_to_do) break; \
  252. (_r)->sring->rsp_event = (_r)->rsp_cons + 1; \
  253. virt_mb(); \
  254. (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \
  255. } while (0)
  256. /*
  257. * DEFINE_XEN_FLEX_RING_AND_INTF defines two monodirectional rings and
  258. * functions to check if there is data on the ring, and to read and
  259. * write to them.
  260. *
  261. * DEFINE_XEN_FLEX_RING is similar to DEFINE_XEN_FLEX_RING_AND_INTF, but
  262. * does not define the indexes page. As different protocols can have
  263. * extensions to the basic format, this macro allow them to define their
  264. * own struct.
  265. *
  266. * XEN_FLEX_RING_SIZE
  267. * Convenience macro to calculate the size of one of the two rings
  268. * from the overall order.
  269. *
  270. * $NAME_mask
  271. * Function to apply the size mask to an index, to reduce the index
  272. * within the range [0-size].
  273. *
  274. * $NAME_read_packet
  275. * Function to read data from the ring. The amount of data to read is
  276. * specified by the "size" argument.
  277. *
  278. * $NAME_write_packet
  279. * Function to write data to the ring. The amount of data to write is
  280. * specified by the "size" argument.
  281. *
  282. * $NAME_get_ring_ptr
  283. * Convenience function that returns a pointer to read/write to the
  284. * ring at the right location.
  285. *
  286. * $NAME_data_intf
  287. * Indexes page, shared between frontend and backend. It also
  288. * contains the array of grant refs.
  289. *
  290. * $NAME_queued
  291. * Function to calculate how many bytes are currently on the ring,
  292. * ready to be read. It can also be used to calculate how much free
  293. * space is currently on the ring (XEN_FLEX_RING_SIZE() -
  294. * $NAME_queued()).
  295. */
  296. #ifndef XEN_PAGE_SHIFT
  297. /* The PAGE_SIZE for ring protocols and hypercall interfaces is always
  298. * 4K, regardless of the architecture, and page granularity chosen by
  299. * operating systems.
  300. */
  301. #define XEN_PAGE_SHIFT 12
  302. #endif
  303. #define XEN_FLEX_RING_SIZE(order) \
  304. (1UL << ((order) + XEN_PAGE_SHIFT - 1))
  305. #define DEFINE_XEN_FLEX_RING(name) \
  306. static inline RING_IDX name##_mask(RING_IDX idx, RING_IDX ring_size) \
  307. { \
  308. return idx & (ring_size - 1); \
  309. } \
  310. \
  311. static inline unsigned char *name##_get_ring_ptr(unsigned char *buf, \
  312. RING_IDX idx, \
  313. RING_IDX ring_size) \
  314. { \
  315. return buf + name##_mask(idx, ring_size); \
  316. } \
  317. \
  318. static inline void name##_read_packet(void *opaque, \
  319. const unsigned char *buf, \
  320. size_t size, \
  321. RING_IDX masked_prod, \
  322. RING_IDX *masked_cons, \
  323. RING_IDX ring_size) \
  324. { \
  325. if (*masked_cons < masked_prod || \
  326. size <= ring_size - *masked_cons) { \
  327. memcpy(opaque, buf + *masked_cons, size); \
  328. } else { \
  329. memcpy(opaque, buf + *masked_cons, ring_size - *masked_cons); \
  330. memcpy((unsigned char *)opaque + ring_size - *masked_cons, buf, \
  331. size - (ring_size - *masked_cons)); \
  332. } \
  333. *masked_cons = name##_mask(*masked_cons + size, ring_size); \
  334. } \
  335. \
  336. static inline void name##_write_packet(unsigned char *buf, \
  337. const void *opaque, \
  338. size_t size, \
  339. RING_IDX *masked_prod, \
  340. RING_IDX masked_cons, \
  341. RING_IDX ring_size) \
  342. { \
  343. if (*masked_prod < masked_cons || \
  344. size <= ring_size - *masked_prod) { \
  345. memcpy(buf + *masked_prod, opaque, size); \
  346. } else { \
  347. memcpy(buf + *masked_prod, opaque, ring_size - *masked_prod); \
  348. memcpy(buf, (unsigned char *)opaque + (ring_size - *masked_prod), \
  349. size - (ring_size - *masked_prod)); \
  350. } \
  351. *masked_prod = name##_mask(*masked_prod + size, ring_size); \
  352. } \
  353. \
  354. static inline RING_IDX name##_queued(RING_IDX prod, \
  355. RING_IDX cons, \
  356. RING_IDX ring_size) \
  357. { \
  358. RING_IDX size; \
  359. \
  360. if (prod == cons) \
  361. return 0; \
  362. \
  363. prod = name##_mask(prod, ring_size); \
  364. cons = name##_mask(cons, ring_size); \
  365. \
  366. if (prod == cons) \
  367. return ring_size; \
  368. \
  369. if (prod > cons) \
  370. size = prod - cons; \
  371. else \
  372. size = ring_size - (cons - prod); \
  373. return size; \
  374. } \
  375. \
  376. struct name##_data { \
  377. unsigned char *in; /* half of the allocation */ \
  378. unsigned char *out; /* half of the allocation */ \
  379. }
  380. #define DEFINE_XEN_FLEX_RING_AND_INTF(name) \
  381. struct name##_data_intf { \
  382. RING_IDX in_cons, in_prod; \
  383. \
  384. uint8_t pad1[56]; \
  385. \
  386. RING_IDX out_cons, out_prod; \
  387. \
  388. uint8_t pad2[56]; \
  389. \
  390. RING_IDX ring_order; \
  391. grant_ref_t ref[]; \
  392. }; \
  393. DEFINE_XEN_FLEX_RING(name)
  394. #endif /* __XEN_PUBLIC_IO_RING_H__ */