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