1 /****************************************************************************** 2 * ring.h 3 * 4 * Shared producer-consumer ring macros. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to 8 * deal in the Software without restriction, including without limitation the 9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10 * sell copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 * DEALINGS IN THE SOFTWARE. 23 * 24 * Tim Deegan and Andrew Warfield November 2004. 25 */ 26 27 #ifndef __XEN_PUBLIC_IO_RING_H__ 28 #define __XEN_PUBLIC_IO_RING_H__ 29 30 #include "../xen-compat.h" 31 32 #if __XEN_INTERFACE_VERSION__ < 0x00030208 33 #define xen_mb() mb() 34 #define xen_rmb() rmb() 35 #define xen_wmb() wmb() 36 #endif 37 38 typedef unsigned int RING_IDX; 39 40 /* Round a 32-bit unsigned constant down to the nearest power of two. */ 41 #define __RD2(_x) (((_x) & 0x00000002) ? 0x2 : ((_x) & 0x1)) 42 #define __RD4(_x) (((_x) & 0x0000000c) ? __RD2((_x)>>2)<<2 : __RD2(_x)) 43 #define __RD8(_x) (((_x) & 0x000000f0) ? __RD4((_x)>>4)<<4 : __RD4(_x)) 44 #define __RD16(_x) (((_x) & 0x0000ff00) ? __RD8((_x)>>8)<<8 : __RD8(_x)) 45 #define __RD32(_x) (((_x) & 0xffff0000) ? __RD16((_x)>>16)<<16 : __RD16(_x)) 46 47 /* 48 * Calculate size of a shared ring, given the total available space for the 49 * ring and indexes (_sz), and the name tag of the request/response structure. 50 * A ring contains as many entries as will fit, rounded down to the nearest 51 * power of two (so we can mask with (size-1) to loop around). 52 */ 53 #define __RING_SIZE(_s, _sz) \ 54 (__RD32(((_sz) - (long)(_s)->ring + (long)(_s)) / sizeof((_s)->ring[0]))) 55 56 /* 57 * Macros to make the correct C datatypes for a new kind of ring. 58 * 59 * To make a new ring datatype, you need to have two message structures, 60 * let's say request_t, and response_t already defined. 61 * 62 * In a header where you want the ring datatype declared, you then do: 63 * 64 * DEFINE_RING_TYPES(mytag, request_t, response_t); 65 * 66 * These expand out to give you a set of types, as you can see below. 67 * The most important of these are: 68 * 69 * mytag_sring_t - The shared ring. 70 * mytag_front_ring_t - The 'front' half of the ring. 71 * mytag_back_ring_t - The 'back' half of the ring. 72 * 73 * To initialize a ring in your code you need to know the location and size 74 * of the shared memory area (PAGE_SIZE, for instance). To initialise 75 * the front half: 76 * 77 * mytag_front_ring_t front_ring; 78 * SHARED_RING_INIT((mytag_sring_t *)shared_page); 79 * FRONT_RING_INIT(&front_ring, (mytag_sring_t *)shared_page, PAGE_SIZE); 80 * 81 * Initializing the back follows similarly (note that only the front 82 * initializes the shared ring): 83 * 84 * mytag_back_ring_t back_ring; 85 * BACK_RING_INIT(&back_ring, (mytag_sring_t *)shared_page, PAGE_SIZE); 86 */ 87 88 #define DEFINE_RING_TYPES(__name, __req_t, __rsp_t) \ 89 \ 90 /* Shared ring entry */ \ 91 union __name##_sring_entry { \ 92 __req_t req; \ 93 __rsp_t rsp; \ 94 }; \ 95 \ 96 /* Shared ring page */ \ 97 struct __name##_sring { \ 98 RING_IDX req_prod, req_event; \ 99 RING_IDX rsp_prod, rsp_event; \ 100 uint8_t pad[48]; \ 101 union __name##_sring_entry ring[1]; /* variable-length */ \ 102 }; \ 103 \ 104 /* "Front" end's private variables */ \ 105 struct __name##_front_ring { \ 106 RING_IDX req_prod_pvt; \ 107 RING_IDX rsp_cons; \ 108 unsigned int nr_ents; \ 109 struct __name##_sring *sring; \ 110 }; \ 111 \ 112 /* "Back" end's private variables */ \ 113 struct __name##_back_ring { \ 114 RING_IDX rsp_prod_pvt; \ 115 RING_IDX req_cons; \ 116 unsigned int nr_ents; \ 117 struct __name##_sring *sring; \ 118 }; \ 119 \ 120 /* Syntactic sugar */ \ 121 typedef struct __name##_sring __name##_sring_t; \ 122 typedef struct __name##_front_ring __name##_front_ring_t; \ 123 typedef struct __name##_back_ring __name##_back_ring_t 124 125 /* 126 * Macros for manipulating rings. 127 * 128 * FRONT_RING_whatever works on the "front end" of a ring: here 129 * requests are pushed on to the ring and responses taken off it. 130 * 131 * BACK_RING_whatever works on the "back end" of a ring: here 132 * requests are taken off the ring and responses put on. 133 * 134 * N.B. these macros do NO INTERLOCKS OR FLOW CONTROL. 135 * This is OK in 1-for-1 request-response situations where the 136 * requestor (front end) never has more than RING_SIZE()-1 137 * outstanding requests. 138 */ 139 140 /* Initialising empty rings */ 141 #define SHARED_RING_INIT(_s) do { \ 142 (_s)->req_prod = (_s)->rsp_prod = 0; \ 143 (_s)->req_event = (_s)->rsp_event = 1; \ 144 (void)memset((_s)->pad, 0, sizeof((_s)->pad)); \ 145 } while(0) 146 147 #define FRONT_RING_INIT(_r, _s, __size) do { \ 148 (_r)->req_prod_pvt = 0; \ 149 (_r)->rsp_cons = 0; \ 150 (_r)->nr_ents = __RING_SIZE(_s, __size); \ 151 (_r)->sring = (_s); \ 152 } while (0) 153 154 #define BACK_RING_INIT(_r, _s, __size) do { \ 155 (_r)->rsp_prod_pvt = 0; \ 156 (_r)->req_cons = 0; \ 157 (_r)->nr_ents = __RING_SIZE(_s, __size); \ 158 (_r)->sring = (_s); \ 159 } while (0) 160 161 /* Initialize to existing shared indexes -- for recovery */ 162 #define FRONT_RING_ATTACH(_r, _s, __size) do { \ 163 (_r)->sring = (_s); \ 164 (_r)->req_prod_pvt = (_s)->req_prod; \ 165 (_r)->rsp_cons = (_s)->rsp_prod; \ 166 (_r)->nr_ents = __RING_SIZE(_s, __size); \ 167 } while (0) 168 169 #define BACK_RING_ATTACH(_r, _s, __size) do { \ 170 (_r)->sring = (_s); \ 171 (_r)->rsp_prod_pvt = (_s)->rsp_prod; \ 172 (_r)->req_cons = (_s)->req_prod; \ 173 (_r)->nr_ents = __RING_SIZE(_s, __size); \ 174 } while (0) 175 176 /* How big is this ring? */ 177 #define RING_SIZE(_r) \ 178 ((_r)->nr_ents) 179 180 /* Number of free requests (for use on front side only). */ 181 #define RING_FREE_REQUESTS(_r) \ 182 (RING_SIZE(_r) - ((_r)->req_prod_pvt - (_r)->rsp_cons)) 183 184 /* Test if there is an empty slot available on the front ring. 185 * (This is only meaningful from the front. ) 186 */ 187 #define RING_FULL(_r) \ 188 (RING_FREE_REQUESTS(_r) == 0) 189 190 /* Test if there are outstanding messages to be processed on a ring. */ 191 #define RING_HAS_UNCONSUMED_RESPONSES(_r) \ 192 ((_r)->sring->rsp_prod - (_r)->rsp_cons) 193 194 #ifdef __GNUC__ 195 #define RING_HAS_UNCONSUMED_REQUESTS(_r) ({ \ 196 unsigned int req = (_r)->sring->req_prod - (_r)->req_cons; \ 197 unsigned int rsp = RING_SIZE(_r) - \ 198 ((_r)->req_cons - (_r)->rsp_prod_pvt); \ 199 req < rsp ? req : rsp; \ 200 }) 201 #else 202 /* Same as above, but without the nice GCC ({ ... }) syntax. */ 203 #define RING_HAS_UNCONSUMED_REQUESTS(_r) \ 204 ((((_r)->sring->req_prod - (_r)->req_cons) < \ 205 (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt))) ? \ 206 ((_r)->sring->req_prod - (_r)->req_cons) : \ 207 (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt))) 208 #endif 209 210 /* Direct access to individual ring elements, by index. */ 211 #define RING_GET_REQUEST(_r, _idx) \ 212 (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req)) 213 214 #define RING_GET_RESPONSE(_r, _idx) \ 215 (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp)) 216 217 /* Loop termination condition: Would the specified index overflow the ring? */ 218 #define RING_REQUEST_CONS_OVERFLOW(_r, _cons) \ 219 (((_cons) - (_r)->rsp_prod_pvt) >= RING_SIZE(_r)) 220 221 #define RING_PUSH_REQUESTS(_r) do { \ 222 xen_wmb(); /* back sees requests /before/ updated producer index */ \ 223 (_r)->sring->req_prod = (_r)->req_prod_pvt; \ 224 } while (0) 225 226 #define RING_PUSH_RESPONSES(_r) do { \ 227 xen_wmb(); /* front sees resps /before/ updated producer index */ \ 228 (_r)->sring->rsp_prod = (_r)->rsp_prod_pvt; \ 229 } while (0) 230 231 /* 232 * Notification hold-off (req_event and rsp_event): 233 * 234 * When queueing requests or responses on a shared ring, it may not always be 235 * necessary to notify the remote end. For example, if requests are in flight 236 * in a backend, the front may be able to queue further requests without 237 * notifying the back (if the back checks for new requests when it queues 238 * responses). 239 * 240 * When enqueuing requests or responses: 241 * 242 * Use RING_PUSH_{REQUESTS,RESPONSES}_AND_CHECK_NOTIFY(). The second argument 243 * is a boolean return value. True indicates that the receiver requires an 244 * asynchronous notification. 245 * 246 * After dequeuing requests or responses (before sleeping the connection): 247 * 248 * Use RING_FINAL_CHECK_FOR_REQUESTS() or RING_FINAL_CHECK_FOR_RESPONSES(). 249 * The second argument is a boolean return value. True indicates that there 250 * are pending messages on the ring (i.e., the connection should not be put 251 * to sleep). 252 * 253 * These macros will set the req_event/rsp_event field to trigger a 254 * notification on the very next message that is enqueued. If you want to 255 * create batches of work (i.e., only receive a notification after several 256 * messages have been enqueued) then you will need to create a customised 257 * version of the FINAL_CHECK macro in your own code, which sets the event 258 * field appropriately. 259 */ 260 261 #define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do { \ 262 RING_IDX __old = (_r)->sring->req_prod; \ 263 RING_IDX __new = (_r)->req_prod_pvt; \ 264 xen_wmb(); /* back sees requests /before/ updated producer index */ \ 265 (_r)->sring->req_prod = __new; \ 266 xen_mb(); /* back sees new requests /before/ we check req_event */ \ 267 (_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) < \ 268 (RING_IDX)(__new - __old)); \ 269 } while (0) 270 271 #define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do { \ 272 RING_IDX __old = (_r)->sring->rsp_prod; \ 273 RING_IDX __new = (_r)->rsp_prod_pvt; \ 274 xen_wmb(); /* front sees resps /before/ updated producer index */ \ 275 (_r)->sring->rsp_prod = __new; \ 276 xen_mb(); /* front sees new resps /before/ we check rsp_event */ \ 277 (_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) < \ 278 (RING_IDX)(__new - __old)); \ 279 } while (0) 280 281 #define RING_FINAL_CHECK_FOR_REQUESTS(_r, _work_to_do) do { \ 282 (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \ 283 if (_work_to_do) break; \ 284 (_r)->sring->req_event = (_r)->req_cons + 1; \ 285 xen_mb(); \ 286 (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \ 287 } while (0) 288 289 #define RING_FINAL_CHECK_FOR_RESPONSES(_r, _work_to_do) do { \ 290 (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \ 291 if (_work_to_do) break; \ 292 (_r)->sring->rsp_event = (_r)->rsp_cons + 1; \ 293 xen_mb(); \ 294 (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \ 295 } while (0) 296 297 #endif /* __XEN_PUBLIC_IO_RING_H__ */ 298 299 /* 300 * Local variables: 301 * mode: C 302 * c-set-style: "BSD" 303 * c-basic-offset: 4 304 * tab-width: 4 305 * indent-tabs-mode: nil 306 * End: 307 */ 308