ring.h 23 KB

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