1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2016 Patrick Rudolph <siro@das-labor.org>
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, f, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * on the rights to use, f, copy, modify, merge, f, publish, distribute, f, sub
8bf215546Sopenharmony_ci * license, f, and/or sell copies of the Software, f, and to permit persons to whom
9bf215546Sopenharmony_ci * the Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20bf215546Sopenharmony_ci * OTHERWISe, f, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22bf215546Sopenharmony_ci
23bf215546Sopenharmony_ci/* get number of arguments with __NARG__ */
24bf215546Sopenharmony_ci#define __NARG__(...)  __NARG_I_(__VA_ARGS__,__RSEQ_N())
25bf215546Sopenharmony_ci#define __NARG_I_(...) __ARG_N(__VA_ARGS__)
26bf215546Sopenharmony_ci#define __ARG_N( \
27bf215546Sopenharmony_ci      _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
28bf215546Sopenharmony_ci     _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
29bf215546Sopenharmony_ci     _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
30bf215546Sopenharmony_ci     _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
31bf215546Sopenharmony_ci     _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
32bf215546Sopenharmony_ci     _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
33bf215546Sopenharmony_ci     _61,_62,_63,N,...) N
34bf215546Sopenharmony_ci#define __RSEQ_N() \
35bf215546Sopenharmony_ci     63,62,61,60,                   \
36bf215546Sopenharmony_ci     59,58,57,56,55,54,53,52,51,50, \
37bf215546Sopenharmony_ci     49,48,47,46,45,44,43,42,41,40, \
38bf215546Sopenharmony_ci     39,38,37,36,35,34,33,32,31,30, \
39bf215546Sopenharmony_ci     29,28,27,26,25,24,23,22,21,20, \
40bf215546Sopenharmony_ci     19,18,17,16,15,14,13,12,11,10, \
41bf215546Sopenharmony_ci     9,8,7,6,5,4,3,2,1,0
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci#define _args_for_bypass_1(a) a
45bf215546Sopenharmony_ci#define _args_for_bypass_7(a, b, c, d, e, f, g) ,g
46bf215546Sopenharmony_ci#define _args_for_bypass_14(a, b, c, d, e, f, g, ...) ,g _args_for_bypass_7(__VA_ARGS__)
47bf215546Sopenharmony_ci#define _args_for_bypass_21(a, b, c, d, e, f, g, ...) ,g _args_for_bypass_14(__VA_ARGS__)
48bf215546Sopenharmony_ci#define _args_for_bypass_28(a, b, c, d, e, f, g, ...) ,g _args_for_bypass_21(__VA_ARGS__)
49bf215546Sopenharmony_ci#define _args_for_bypass_35(a, b, c, d, e, f, g, ...) ,g _args_for_bypass_28(__VA_ARGS__)
50bf215546Sopenharmony_ci#define _args_for_bypass_42(a, b, c, d, e, f, g, ...) ,g _args_for_bypass_35(__VA_ARGS__)
51bf215546Sopenharmony_ci#define _args_for_bypass_49(a, b, c, d, e, f, g, ...) ,g _args_for_bypass_42(__VA_ARGS__)
52bf215546Sopenharmony_ci#define _args_for_bypass_56(a, b, c, d, e, f, g, ...) ,g _args_for_bypass_49(__VA_ARGS__)
53bf215546Sopenharmony_ci#define _args_for_bypass_63(a, b, c, d, e, f, g, ...) ,g _args_for_bypass_56(__VA_ARGS__)
54bf215546Sopenharmony_ci#define _args_for_bypass_70(a, b, c, d, e, f, g, ...) ,g _args_for_bypass_63(__VA_ARGS__)
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci#define _GFUNC_(n) _args_for_bypass_##n
57bf215546Sopenharmony_ci#define _GFUNC(n) _GFUNC_(n)
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ci#define ARGS_FOR_BYPASS(...) _GFUNC(__NARG__(__VA_ARGS__)) (__VA_ARGS__)
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci#define _args_for_mem_1(a) a;
62bf215546Sopenharmony_ci#define _args_for_mem_7(a, b, c, d, e, f, g) f;
63bf215546Sopenharmony_ci#define _args_for_mem_14(a, b, c, d, e, f, g, ...) f; _args_for_mem_7(__VA_ARGS__)
64bf215546Sopenharmony_ci#define _args_for_mem_21(a, b, c, d, e, f, g, ...) f; _args_for_mem_14(__VA_ARGS__)
65bf215546Sopenharmony_ci#define _args_for_mem_28(a, b, c, d, e, f, g, ...) f; _args_for_mem_21(__VA_ARGS__)
66bf215546Sopenharmony_ci#define _args_for_mem_35(a, b, c, d, e, f, g, ...) f; _args_for_mem_28(__VA_ARGS__)
67bf215546Sopenharmony_ci#define _args_for_mem_42(a, b, c, d, e, f, g, ...) f; _args_for_mem_35(__VA_ARGS__)
68bf215546Sopenharmony_ci#define _args_for_mem_49(a, b, c, d, e, f, g, ...) f; _args_for_mem_42(__VA_ARGS__)
69bf215546Sopenharmony_ci#define _args_for_mem_56(a, b, c, d, e, f, g, ...) f; _args_for_mem_49(__VA_ARGS__)
70bf215546Sopenharmony_ci#define _args_for_mem_63(a, b, c, d, e, f, g, ...) f; _args_for_mem_56(__VA_ARGS__)
71bf215546Sopenharmony_ci#define _args_for_mem_70(a, b, c, d, e, f, g, ...) f; _args_for_mem_63(__VA_ARGS__)
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci#define _FFUNC_(n) _args_for_mem_##n
74bf215546Sopenharmony_ci#define _FFUNC(n) _FFUNC_(n)
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci#define ARGS_FOR_MEM(...) _FFUNC(__NARG__(__VA_ARGS__)) (__VA_ARGS__)
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_ci#define _args_for_unbind_1(a) a;
79bf215546Sopenharmony_ci#define _args_for_unbind_7(a, b, c, d, e, f, g) e;
80bf215546Sopenharmony_ci#define _args_for_unbind_14(a, b, c, d, e, f, g, ...) e; _args_for_unbind_7(__VA_ARGS__)
81bf215546Sopenharmony_ci#define _args_for_unbind_21(a, b, c, d, e, f, g, ...) e; _args_for_unbind_14(__VA_ARGS__)
82bf215546Sopenharmony_ci#define _args_for_unbind_28(a, b, c, d, e, f, g, ...) e; _args_for_unbind_21(__VA_ARGS__)
83bf215546Sopenharmony_ci#define _args_for_unbind_35(a, b, c, d, e, f, g, ...) e; _args_for_unbind_28(__VA_ARGS__)
84bf215546Sopenharmony_ci#define _args_for_unbind_42(a, b, c, d, e, f, g, ...) e; _args_for_unbind_35(__VA_ARGS__)
85bf215546Sopenharmony_ci#define _args_for_unbind_49(a, b, c, d, e, f, g, ...) e; _args_for_unbind_42(__VA_ARGS__)
86bf215546Sopenharmony_ci#define _args_for_unbind_56(a, b, c, d, e, f, g, ...) e; _args_for_unbind_49(__VA_ARGS__)
87bf215546Sopenharmony_ci#define _args_for_unbind_63(a, b, c, d, e, f, g, ...) e; _args_for_unbind_56(__VA_ARGS__)
88bf215546Sopenharmony_ci#define _args_for_unbind_70(a, b, c, d, e, f, g, ...) e; _args_for_unbind_63(__VA_ARGS__)
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci#define _EFUNC_(n) _args_for_unbind_##n
91bf215546Sopenharmony_ci#define _EFUNC(n) _EFUNC_(n)
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci#define ARGS_FOR_UNBIND(...) _EFUNC(__NARG__(__VA_ARGS__)) (__VA_ARGS__)
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_ci#define _args_for_call_1(a) a
96bf215546Sopenharmony_ci#define _args_for_call_7(a, b, c, d, e, f, g) ,d
97bf215546Sopenharmony_ci#define _args_for_call_14(a, b, c, d, e, f, g, ...) ,d _args_for_call_7(__VA_ARGS__)
98bf215546Sopenharmony_ci#define _args_for_call_21(a, b, c, d, e, f, g, ...) ,d _args_for_call_14(__VA_ARGS__)
99bf215546Sopenharmony_ci#define _args_for_call_28(a, b, c, d, e, f, g, ...) ,d _args_for_call_21(__VA_ARGS__)
100bf215546Sopenharmony_ci#define _args_for_call_35(a, b, c, d, e, f, g, ...) ,d _args_for_call_28(__VA_ARGS__)
101bf215546Sopenharmony_ci#define _args_for_call_42(a, b, c, d, e, f, g, ...) ,d _args_for_call_35(__VA_ARGS__)
102bf215546Sopenharmony_ci#define _args_for_call_49(a, b, c, d, e, f, g, ...) ,d _args_for_call_42(__VA_ARGS__)
103bf215546Sopenharmony_ci#define _args_for_call_56(a, b, c, d, e, f, g, ...) ,d _args_for_call_49(__VA_ARGS__)
104bf215546Sopenharmony_ci#define _args_for_call_63(a, b, c, d, e, f, g, ...) ,d _args_for_call_56(__VA_ARGS__)
105bf215546Sopenharmony_ci#define _args_for_call_70(a, b, c, d, e, f, g, ...) ,d _args_for_call_63(__VA_ARGS__)
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci#define _DFUNC_(n) _args_for_call_##n
108bf215546Sopenharmony_ci#define _DFUNC(n) _DFUNC_(n)
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci#define ARGS_FOR_CALL(...) _DFUNC(__NARG__(__VA_ARGS__)) (__VA_ARGS__)
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_ci#define _args_for_decl_1(a) a
113bf215546Sopenharmony_ci#define _args_for_decl_7(a, b, c, d, e, f, g) ,c
114bf215546Sopenharmony_ci#define _args_for_decl_14(a, b, c, d, e, f, g, ...) ,c _args_for_decl_7(__VA_ARGS__)
115bf215546Sopenharmony_ci#define _args_for_decl_21(a, b, c, d, e, f, g, ...) ,c _args_for_decl_14(__VA_ARGS__)
116bf215546Sopenharmony_ci#define _args_for_decl_28(a, b, c, d, e, f, g, ...) ,c _args_for_decl_21(__VA_ARGS__)
117bf215546Sopenharmony_ci#define _args_for_decl_35(a, b, c, d, e, f, g, ...) ,c _args_for_decl_28(__VA_ARGS__)
118bf215546Sopenharmony_ci#define _args_for_decl_42(a, b, c, d, e, f, g, ...) ,c _args_for_decl_35(__VA_ARGS__)
119bf215546Sopenharmony_ci#define _args_for_decl_49(a, b, c, d, e, f, g, ...) ,c _args_for_decl_42(__VA_ARGS__)
120bf215546Sopenharmony_ci#define _args_for_decl_56(a, b, c, d, e, f, g, ...) ,c _args_for_decl_49(__VA_ARGS__)
121bf215546Sopenharmony_ci#define _args_for_decl_63(a, b, c, d, e, f, g, ...) ,c _args_for_decl_56(__VA_ARGS__)
122bf215546Sopenharmony_ci#define _args_for_decl_70(a, b, c, d, e, f, g, ...) ,c _args_for_decl_63(__VA_ARGS__)
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci#define _CFUNC_(n) _args_for_decl_##n
125bf215546Sopenharmony_ci#define _CFUNC(n) _CFUNC_(n)
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ci#define ARGS_FOR_DECLARATION(...) _CFUNC(__NARG__(__VA_ARGS__)) (__VA_ARGS__)
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci#define _args_for_assign_1(a) a
130bf215546Sopenharmony_ci#define _args_for_assign_7(a, b, c, d, e, f, g) b;
131bf215546Sopenharmony_ci#define _args_for_assign_14(a, b, c, d, e, f, g, ...) b; _args_for_assign_7(__VA_ARGS__)
132bf215546Sopenharmony_ci#define _args_for_assign_21(a, b, c, d, e, f, g, ...) b; _args_for_assign_14(__VA_ARGS__)
133bf215546Sopenharmony_ci#define _args_for_assign_28(a, b, c, d, e, f, g, ...) b; _args_for_assign_21(__VA_ARGS__)
134bf215546Sopenharmony_ci#define _args_for_assign_35(a, b, c, d, e, f, g, ...) b; _args_for_assign_28(__VA_ARGS__)
135bf215546Sopenharmony_ci#define _args_for_assign_42(a, b, c, d, e, f, g, ...) b; _args_for_assign_35(__VA_ARGS__)
136bf215546Sopenharmony_ci#define _args_for_assign_49(a, b, c, d, e, f, g, ...) b; _args_for_assign_42(__VA_ARGS__)
137bf215546Sopenharmony_ci#define _args_for_assign_56(a, b, c, d, e, f, g, ...) b; _args_for_assign_49(__VA_ARGS__)
138bf215546Sopenharmony_ci#define _args_for_assign_63(a, b, c, d, e, f, g, ...) b; _args_for_assign_56(__VA_ARGS__)
139bf215546Sopenharmony_ci#define _args_for_assign_70(a, b, c, d, e, f, g, ...) b; _args_for_assign_63(__VA_ARGS__)
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_ci#define _BFUNC_(n) _args_for_assign_##n
142bf215546Sopenharmony_ci#define _BFUNC(n) _BFUNC_(n)
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_ci#define ARGS_FOR_ASSIGN(...) _BFUNC(__NARG__(__VA_ARGS__)) (__VA_ARGS__)
145bf215546Sopenharmony_ci
146bf215546Sopenharmony_ci#define _args_for_struct_1(a) a;
147bf215546Sopenharmony_ci#define _args_for_struct_7(a, b, c, d, e, f, g) a;
148bf215546Sopenharmony_ci#define _args_for_struct_14(a, b, c, d, e, f, g, ...) a; _args_for_struct_7(__VA_ARGS__)
149bf215546Sopenharmony_ci#define _args_for_struct_21(a, b, c, d, e, f, g, ...) a; _args_for_struct_14(__VA_ARGS__)
150bf215546Sopenharmony_ci#define _args_for_struct_28(a, b, c, d, e, f, g, ...) a; _args_for_struct_21(__VA_ARGS__)
151bf215546Sopenharmony_ci#define _args_for_struct_35(a, b, c, d, e, f, g, ...) a; _args_for_struct_28(__VA_ARGS__)
152bf215546Sopenharmony_ci#define _args_for_struct_42(a, b, c, d, e, f, g, ...) a; _args_for_struct_35(__VA_ARGS__)
153bf215546Sopenharmony_ci#define _args_for_struct_49(a, b, c, d, e, f, g, ...) a; _args_for_struct_42(__VA_ARGS__)
154bf215546Sopenharmony_ci#define _args_for_struct_56(a, b, c, d, e, f, g, ...) a; _args_for_struct_49(__VA_ARGS__)
155bf215546Sopenharmony_ci#define _args_for_struct_63(a, b, c, d, e, f, g, ...) a; _args_for_struct_56(__VA_ARGS__)
156bf215546Sopenharmony_ci#define _args_for_struct_70(a, b, c, d, e, f, g, ...) a; _args_for_struct_63(__VA_ARGS__)
157bf215546Sopenharmony_ci
158bf215546Sopenharmony_ci#define _AFUNC_(n) _args_for_struct_##n
159bf215546Sopenharmony_ci#define _AFUNC(n) _AFUNC_(n)
160bf215546Sopenharmony_ci
161bf215546Sopenharmony_ci#define ARGS_FOR_STRUCT(...) _AFUNC(__NARG__(__VA_ARGS__)) (__VA_ARGS__)
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ci/* Serialization and deserialization */
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_ci#define CSMT_ITEM_NO_WAIT(name, ...) \
166bf215546Sopenharmony_ci\
167bf215546Sopenharmony_cistruct s_##name##_private { \
168bf215546Sopenharmony_ci    struct csmt_instruction instr; \
169bf215546Sopenharmony_ci    ARGS_FOR_STRUCT( __VA_ARGS__ ) \
170bf215546Sopenharmony_ci}; \
171bf215546Sopenharmony_ci\
172bf215546Sopenharmony_cistatic void \
173bf215546Sopenharmony_ciname##_priv( struct NineDevice9 *device ARGS_FOR_DECLARATION( __VA_ARGS__ ) ); \
174bf215546Sopenharmony_ci\
175bf215546Sopenharmony_cistatic int \
176bf215546Sopenharmony_ciname##_rx( struct NineDevice9 *device, struct csmt_instruction *instr ) \
177bf215546Sopenharmony_ci{ \
178bf215546Sopenharmony_ci    struct csmt_context *ctx = device->csmt_ctx; \
179bf215546Sopenharmony_ci    struct s_##name##_private *args = (struct s_##name##_private *)instr; \
180bf215546Sopenharmony_ci    \
181bf215546Sopenharmony_ci    (void) args; \
182bf215546Sopenharmony_ci    (void) ctx; \
183bf215546Sopenharmony_ci    name##_priv( \
184bf215546Sopenharmony_ci        device ARGS_FOR_CALL( __VA_ARGS__ ) \
185bf215546Sopenharmony_ci    ); \
186bf215546Sopenharmony_ci    ARGS_FOR_UNBIND( __VA_ARGS__ ) \
187bf215546Sopenharmony_ci    return 0; \
188bf215546Sopenharmony_ci} \
189bf215546Sopenharmony_ci\
190bf215546Sopenharmony_civoid \
191bf215546Sopenharmony_ciname( struct NineDevice9 *device ARGS_FOR_DECLARATION( __VA_ARGS__ ) ) \
192bf215546Sopenharmony_ci{ \
193bf215546Sopenharmony_ci    struct csmt_context *ctx = device->csmt_ctx; \
194bf215546Sopenharmony_ci    struct s_##name##_private *args; \
195bf215546Sopenharmony_ci    unsigned memsize = sizeof(struct s_##name##_private); \
196bf215546Sopenharmony_ci    unsigned memsize2 = 0; \
197bf215546Sopenharmony_ci    \
198bf215546Sopenharmony_ci    if (!device->csmt_active) { \
199bf215546Sopenharmony_ci        name##_priv( \
200bf215546Sopenharmony_ci            device ARGS_FOR_BYPASS( __VA_ARGS__ ) \
201bf215546Sopenharmony_ci        ); \
202bf215546Sopenharmony_ci        return; \
203bf215546Sopenharmony_ci    } \
204bf215546Sopenharmony_ci    ARGS_FOR_MEM ( __VA_ARGS__ ) \
205bf215546Sopenharmony_ci    args = nine_queue_alloc(ctx->pool, memsize + memsize2); \
206bf215546Sopenharmony_ci    assert(args); \
207bf215546Sopenharmony_ci    args->instr.func = &name##_rx; \
208bf215546Sopenharmony_ci    ARGS_FOR_ASSIGN( __VA_ARGS__ ) \
209bf215546Sopenharmony_ci} \
210bf215546Sopenharmony_ci\
211bf215546Sopenharmony_cistatic void \
212bf215546Sopenharmony_ciname##_priv( struct NineDevice9 *device ARGS_FOR_DECLARATION( __VA_ARGS__ ) )
213bf215546Sopenharmony_ci
214bf215546Sopenharmony_ci#define CSMT_ITEM_NO_WAIT_WITH_COUNTER(name, ...) \
215bf215546Sopenharmony_ci\
216bf215546Sopenharmony_cistruct s_##name##_private { \
217bf215546Sopenharmony_ci    struct csmt_instruction instr; \
218bf215546Sopenharmony_ci    unsigned *counter; \
219bf215546Sopenharmony_ci    ARGS_FOR_STRUCT( __VA_ARGS__ ) \
220bf215546Sopenharmony_ci}; \
221bf215546Sopenharmony_ci\
222bf215546Sopenharmony_cistatic void \
223bf215546Sopenharmony_ciname##_priv( struct NineDevice9 *device ARGS_FOR_DECLARATION( __VA_ARGS__ ) ); \
224bf215546Sopenharmony_ci\
225bf215546Sopenharmony_cistatic int \
226bf215546Sopenharmony_ciname##_rx( struct NineDevice9 *device, struct csmt_instruction *instr ) \
227bf215546Sopenharmony_ci{ \
228bf215546Sopenharmony_ci    struct csmt_context *ctx = device->csmt_ctx; \
229bf215546Sopenharmony_ci    struct s_##name##_private *args = (struct s_##name##_private *)instr; \
230bf215546Sopenharmony_ci    \
231bf215546Sopenharmony_ci    (void) args; \
232bf215546Sopenharmony_ci    (void) ctx; \
233bf215546Sopenharmony_ci    name##_priv( \
234bf215546Sopenharmony_ci        device ARGS_FOR_CALL( __VA_ARGS__ ) \
235bf215546Sopenharmony_ci    ); \
236bf215546Sopenharmony_ci    p_atomic_dec(args->counter); \
237bf215546Sopenharmony_ci    ARGS_FOR_UNBIND( __VA_ARGS__ ) \
238bf215546Sopenharmony_ci    return 0; \
239bf215546Sopenharmony_ci} \
240bf215546Sopenharmony_ci\
241bf215546Sopenharmony_civoid \
242bf215546Sopenharmony_ciname( struct NineDevice9 *device, unsigned *counter ARGS_FOR_DECLARATION( __VA_ARGS__ ) ) \
243bf215546Sopenharmony_ci{ \
244bf215546Sopenharmony_ci    struct csmt_context *ctx = device->csmt_ctx; \
245bf215546Sopenharmony_ci    struct s_##name##_private *args; \
246bf215546Sopenharmony_ci    unsigned memsize = sizeof(struct s_##name##_private); \
247bf215546Sopenharmony_ci    unsigned memsize2 = 0; \
248bf215546Sopenharmony_ci    \
249bf215546Sopenharmony_ci    if (!device->csmt_active) { \
250bf215546Sopenharmony_ci        name##_priv( \
251bf215546Sopenharmony_ci            device ARGS_FOR_BYPASS( __VA_ARGS__ ) \
252bf215546Sopenharmony_ci        ); \
253bf215546Sopenharmony_ci        return; \
254bf215546Sopenharmony_ci    } \
255bf215546Sopenharmony_ci    assert(counter); \
256bf215546Sopenharmony_ci    p_atomic_inc(counter); \
257bf215546Sopenharmony_ci    ARGS_FOR_MEM ( __VA_ARGS__ ) \
258bf215546Sopenharmony_ci    args = nine_queue_alloc(ctx->pool, memsize + memsize2); \
259bf215546Sopenharmony_ci    assert(args); \
260bf215546Sopenharmony_ci    args->instr.func = &name##_rx; \
261bf215546Sopenharmony_ci    args->counter = counter; \
262bf215546Sopenharmony_ci    ARGS_FOR_ASSIGN( __VA_ARGS__ ) \
263bf215546Sopenharmony_ci} \
264bf215546Sopenharmony_ci\
265bf215546Sopenharmony_cistatic void \
266bf215546Sopenharmony_ciname##_priv( struct NineDevice9 *device ARGS_FOR_DECLARATION( __VA_ARGS__ ) )
267bf215546Sopenharmony_ci
268bf215546Sopenharmony_ci#define CSMT_ITEM_DO_WAIT(name, ...) \
269bf215546Sopenharmony_ci\
270bf215546Sopenharmony_cistruct s_##name##_private { \
271bf215546Sopenharmony_ci    struct csmt_instruction instr; \
272bf215546Sopenharmony_ci    ARGS_FOR_STRUCT( __VA_ARGS__ ) \
273bf215546Sopenharmony_ci}; \
274bf215546Sopenharmony_cistatic void \
275bf215546Sopenharmony_ciname##_priv( struct NineDevice9 *device ARGS_FOR_DECLARATION( __VA_ARGS__ ) ); \
276bf215546Sopenharmony_ci\
277bf215546Sopenharmony_cistatic int \
278bf215546Sopenharmony_ciname##_rx( struct NineDevice9 *device, struct csmt_instruction *instr) \
279bf215546Sopenharmony_ci{ \
280bf215546Sopenharmony_ci    struct csmt_context *ctx = device->csmt_ctx; \
281bf215546Sopenharmony_ci    struct s_##name##_private *args = (struct s_##name##_private *)instr; \
282bf215546Sopenharmony_ci    \
283bf215546Sopenharmony_ci    (void) args; \
284bf215546Sopenharmony_ci    (void) ctx; \
285bf215546Sopenharmony_ci    name##_priv( \
286bf215546Sopenharmony_ci        device ARGS_FOR_CALL( __VA_ARGS__ ) \
287bf215546Sopenharmony_ci    ); \
288bf215546Sopenharmony_ci    ARGS_FOR_UNBIND( __VA_ARGS__ ) \
289bf215546Sopenharmony_ci    return 1; \
290bf215546Sopenharmony_ci} \
291bf215546Sopenharmony_ci\
292bf215546Sopenharmony_civoid \
293bf215546Sopenharmony_ciname( struct NineDevice9 *device ARGS_FOR_DECLARATION( __VA_ARGS__ ) ) \
294bf215546Sopenharmony_ci{ \
295bf215546Sopenharmony_ci    struct csmt_context *ctx = device->csmt_ctx; \
296bf215546Sopenharmony_ci    struct s_##name##_private *args; \
297bf215546Sopenharmony_ci    unsigned memsize = sizeof(struct s_##name##_private); \
298bf215546Sopenharmony_ci    unsigned memsize2 = 0; \
299bf215546Sopenharmony_ci    \
300bf215546Sopenharmony_ci    if (!device->csmt_active) { \
301bf215546Sopenharmony_ci        name##_priv( \
302bf215546Sopenharmony_ci            device ARGS_FOR_BYPASS( __VA_ARGS__ ) \
303bf215546Sopenharmony_ci        ); \
304bf215546Sopenharmony_ci        return; \
305bf215546Sopenharmony_ci    } \
306bf215546Sopenharmony_ci    ARGS_FOR_MEM ( __VA_ARGS__ ) \
307bf215546Sopenharmony_ci    args = nine_queue_alloc(ctx->pool, memsize + memsize2); \
308bf215546Sopenharmony_ci    assert(args); \
309bf215546Sopenharmony_ci    args->instr.func = &name##_rx; \
310bf215546Sopenharmony_ci    ARGS_FOR_ASSIGN( __VA_ARGS__ ) \
311bf215546Sopenharmony_ci    ctx->processed = FALSE; \
312bf215546Sopenharmony_ci    nine_queue_flush(ctx->pool); \
313bf215546Sopenharmony_ci    nine_csmt_wait_processed(ctx); \
314bf215546Sopenharmony_ci} \
315bf215546Sopenharmony_ci\
316bf215546Sopenharmony_cistatic void \
317bf215546Sopenharmony_ciname##_priv( struct NineDevice9 *device ARGS_FOR_DECLARATION( __VA_ARGS__ ) )
318bf215546Sopenharmony_ci
319bf215546Sopenharmony_ci/* ARGS_FOR_STRUCT, ARGS_FOR_ASSIGN, ARGS_FOR_DECLARATION, ARGS_FOR_CALL, ARGS_FOR_UNBIND, ARGS_FOR_MEM, ARGS_FOR_BYPASS */
320bf215546Sopenharmony_ci#define ARG_VAL(x, y) \
321bf215546Sopenharmony_ci        x _##y ; ,\
322bf215546Sopenharmony_ci        args->_##y = y ; ,\
323bf215546Sopenharmony_ci        x y ,\
324bf215546Sopenharmony_ci        args->_##y ,\
325bf215546Sopenharmony_ci        ,\
326bf215546Sopenharmony_ci        ,\
327bf215546Sopenharmony_ci        y
328bf215546Sopenharmony_ci
329bf215546Sopenharmony_ci#define ARG_REF(x, y) \
330bf215546Sopenharmony_ci        x* _##y ; ,\
331bf215546Sopenharmony_ci        args->_##y = y; ,\
332bf215546Sopenharmony_ci        x *y ,\
333bf215546Sopenharmony_ci        args->_##y ,\
334bf215546Sopenharmony_ci        ,\
335bf215546Sopenharmony_ci        ,\
336bf215546Sopenharmony_ci        y
337bf215546Sopenharmony_ci
338bf215546Sopenharmony_ci#define ARG_COPY_REF(x, y) \
339bf215546Sopenharmony_ci        x * _##y ; x __##y ; ,\
340bf215546Sopenharmony_ci        if ( y ) { args->_##y = &args->__##y ; args->__##y = *y ; } else { args->_##y = NULL; } ,\
341bf215546Sopenharmony_ci        const x *y ,\
342bf215546Sopenharmony_ci        (const x *)args->_##y ,\
343bf215546Sopenharmony_ci        ,\
344bf215546Sopenharmony_ci        ,\
345bf215546Sopenharmony_ci        (const x *)y
346bf215546Sopenharmony_ci
347bf215546Sopenharmony_ci#define ARG_BIND_REF(x, y) \
348bf215546Sopenharmony_ci        x * _##y ,\
349bf215546Sopenharmony_ci        if ( y ) \
350bf215546Sopenharmony_ci            NineUnknown_Bind( (void *)y ); \
351bf215546Sopenharmony_ci        args->_##y = y ; ,\
352bf215546Sopenharmony_ci        x *y ,\
353bf215546Sopenharmony_ci        args->_##y,\
354bf215546Sopenharmony_ci        if (args->_##y) \
355bf215546Sopenharmony_ci            NineUnknown_Unbind((void *)(args->_##y)); \
356bf215546Sopenharmony_ci        args->_##y = NULL; ,\
357bf215546Sopenharmony_ci        ,\
358bf215546Sopenharmony_ci        y
359bf215546Sopenharmony_ci
360bf215546Sopenharmony_ci#define ARG_BIND_RES(x, y) \
361bf215546Sopenharmony_ci        x * _##y ,\
362bf215546Sopenharmony_ci        args->_##y = NULL; \
363bf215546Sopenharmony_ci        if (y) \
364bf215546Sopenharmony_ci            pipe_resource_reference(&args->_##y, y); ,\
365bf215546Sopenharmony_ci        x *y ,\
366bf215546Sopenharmony_ci        args->_##y ,\
367bf215546Sopenharmony_ci        if (args->_##y) \
368bf215546Sopenharmony_ci            pipe_resource_reference(&args->_##y, NULL); ,\
369bf215546Sopenharmony_ci        ,\
370bf215546Sopenharmony_ci        y
371bf215546Sopenharmony_ci
372bf215546Sopenharmony_ci#define ARG_MEM(x, y) \
373bf215546Sopenharmony_ci        x * _##y ,\
374bf215546Sopenharmony_ci        args->_##y = (void *)args + memsize;\
375bf215546Sopenharmony_ci        memcpy(args->_##y, y, memsize2); ,\
376bf215546Sopenharmony_ci        const x *y ,\
377bf215546Sopenharmony_ci        (const x *)args->_##y ,\
378bf215546Sopenharmony_ci        ,\
379bf215546Sopenharmony_ci        ,\
380bf215546Sopenharmony_ci        (const x *)y
381bf215546Sopenharmony_ci
382bf215546Sopenharmony_ci#define ARG_MEM_SIZE(x, y) \
383bf215546Sopenharmony_ci        x _##y ,\
384bf215546Sopenharmony_ci        args->_##y = y; ,\
385bf215546Sopenharmony_ci        x y ,\
386bf215546Sopenharmony_ci        args->_##y ,\
387bf215546Sopenharmony_ci        ,\
388bf215546Sopenharmony_ci        memsize2 = y, \
389bf215546Sopenharmony_ci        y
390bf215546Sopenharmony_ci
391bf215546Sopenharmony_ci#define ARG_BIND_BLIT(x, y) \
392bf215546Sopenharmony_ci        x _##y ,\
393bf215546Sopenharmony_ci        memcpy(&args->_##y , y, sizeof(x)); \
394bf215546Sopenharmony_ci        args->_##y.src.resource = NULL; \
395bf215546Sopenharmony_ci        args->_##y.dst.resource = NULL; \
396bf215546Sopenharmony_ci        pipe_resource_reference(&args->_##y.src.resource, y->src.resource); \
397bf215546Sopenharmony_ci        pipe_resource_reference(&args->_##y.dst.resource, y->dst.resource);,\
398bf215546Sopenharmony_ci        x *y ,\
399bf215546Sopenharmony_ci        &args->_##y ,\
400bf215546Sopenharmony_ci        pipe_resource_reference(&args->_##y.src.resource, NULL); \
401bf215546Sopenharmony_ci        pipe_resource_reference(&args->_##y.dst.resource, NULL);,\
402bf215546Sopenharmony_ci        ,\
403bf215546Sopenharmony_ci        y
404bf215546Sopenharmony_ci
405bf215546Sopenharmony_ci#define ARG_BIND_VBUF(x, y) \
406bf215546Sopenharmony_ci        x _##y ,\
407bf215546Sopenharmony_ci        memcpy(&args->_##y , y, sizeof(x)); \
408bf215546Sopenharmony_ci        args->_##y.buffer.resource = NULL; \
409bf215546Sopenharmony_ci        pipe_resource_reference(&args->_##y.buffer.resource, y->buffer.resource); ,\
410bf215546Sopenharmony_ci        x *y ,\
411bf215546Sopenharmony_ci        &args->_##y ,\
412bf215546Sopenharmony_ci        pipe_resource_reference(&args->_##y.buffer.resource, NULL); ,\
413bf215546Sopenharmony_ci        ,\
414bf215546Sopenharmony_ci        y
415bf215546Sopenharmony_ci
416bf215546Sopenharmony_ci#define ARG_BIND_IBUF(x, y) \
417bf215546Sopenharmony_ci        x _##y ,\
418bf215546Sopenharmony_ci        memcpy(&args->_##y , y, sizeof(x)); \
419bf215546Sopenharmony_ci        args->_##y.buffer = NULL; \
420bf215546Sopenharmony_ci        pipe_resource_reference(&args->_##y.buffer, y->buffer); ,\
421bf215546Sopenharmony_ci        x *y ,\
422bf215546Sopenharmony_ci        &args->_##y ,\
423bf215546Sopenharmony_ci        pipe_resource_reference(&args->_##y.buffer, NULL); ,\
424bf215546Sopenharmony_ci        ,\
425bf215546Sopenharmony_ci        y
426bf215546Sopenharmony_ci
427bf215546Sopenharmony_ci#define ARG_BIND_VIEW(x, y) \
428bf215546Sopenharmony_ci        x * _##y ,\
429bf215546Sopenharmony_ci        args->_##y = NULL; \
430bf215546Sopenharmony_ci        if (y) \
431bf215546Sopenharmony_ci            pipe_sampler_view_reference(&args->_##y, y); ,\
432bf215546Sopenharmony_ci        x *y ,\
433bf215546Sopenharmony_ci        args->_##y ,\
434bf215546Sopenharmony_ci        if (args->_##y) \
435bf215546Sopenharmony_ci            pipe_sampler_view_reference(&args->_##y, NULL); ,\
436bf215546Sopenharmony_ci        ,\
437bf215546Sopenharmony_ci        y
438bf215546Sopenharmony_ci
439