ring.h 19 KB

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