lib/igt_kms: Clean up the other _name functions/macros
[platform/upstream/intel-gpu-tools.git] / lib / intel_batchbuffer.h
1 #ifndef INTEL_BATCHBUFFER_H
2 #define INTEL_BATCHBUFFER_H
3
4 #include <stdint.h>
5 #include <intel_bufmgr.h>
6 #include "igt_core.h"
7 #include "intel_reg.h"
8
9 #define BATCH_SZ 4096
10 #define BATCH_RESERVED 16
11
12 struct intel_batchbuffer {
13         drm_intel_bufmgr *bufmgr;
14         uint32_t devid;
15
16         drm_intel_bo *bo;
17
18         uint8_t buffer[BATCH_SZ];
19         uint8_t *ptr;
20         uint8_t *state;
21 };
22
23 struct intel_batchbuffer *intel_batchbuffer_alloc(drm_intel_bufmgr *bufmgr,
24                                                   uint32_t devid);
25
26 void intel_batchbuffer_free(struct intel_batchbuffer *batch);
27
28
29 void intel_batchbuffer_flush(struct intel_batchbuffer *batch);
30 void intel_batchbuffer_flush_on_ring(struct intel_batchbuffer *batch, int ring);
31 void intel_batchbuffer_flush_with_context(struct intel_batchbuffer *batch,
32                                           drm_intel_context *context);
33
34 void intel_batchbuffer_reset(struct intel_batchbuffer *batch);
35
36 void intel_batchbuffer_data(struct intel_batchbuffer *batch,
37                             const void *data, unsigned int bytes);
38
39 void intel_batchbuffer_emit_reloc(struct intel_batchbuffer *batch,
40                                   drm_intel_bo *buffer,
41                                   uint32_t delta,
42                                   uint32_t read_domains,
43                                   uint32_t write_domain,
44                                   int fenced);
45
46 /* Inline functions - might actually be better off with these
47  * non-inlined.  Certainly better off switching all command packets to
48  * be passed as structs rather than dwords, but that's a little bit of
49  * work...
50  */
51 #pragma GCC diagnostic ignored "-Winline"
52 static inline unsigned int
53 intel_batchbuffer_space(struct intel_batchbuffer *batch)
54 {
55         return (BATCH_SZ - BATCH_RESERVED) - (batch->ptr - batch->buffer);
56 }
57
58
59 static inline void
60 intel_batchbuffer_emit_dword(struct intel_batchbuffer *batch, uint32_t dword)
61 {
62         igt_assert(intel_batchbuffer_space(batch) >= 4);
63         *(uint32_t *) (batch->ptr) = dword;
64         batch->ptr += 4;
65 }
66
67 static inline void
68 intel_batchbuffer_require_space(struct intel_batchbuffer *batch,
69                                 unsigned int sz)
70 {
71         igt_assert(sz < BATCH_SZ - BATCH_RESERVED);
72         if (intel_batchbuffer_space(batch) < sz)
73                 intel_batchbuffer_flush(batch);
74 }
75
76 /**
77  * BEGIN_BATCH:
78  * @n: number of DWORDS to emit
79  *
80  * Prepares a batch to emit @n DWORDS, flushing it if there's not enough space
81  * available.
82  *
83  * This macro needs a pointer to an #intel_batchbuffer structure called batch in
84  * scope.
85  */
86 #define BEGIN_BATCH(n) do {                                             \
87         intel_batchbuffer_require_space(batch, (n)*4);                  \
88 } while (0)
89
90 /**
91  * OUT_BATCH:
92  * @d: DWORD to emit
93  *
94  * Emits @d into a batch.
95  *
96  * This macro needs a pointer to an #intel_batchbuffer structure called batch in
97  * scope.
98  */
99 #define OUT_BATCH(d) intel_batchbuffer_emit_dword(batch, d)
100
101 /**
102  * OUT_RELOC_FENCED:
103  * @buf: relocation target libdrm buffer object
104  * @read_domains: gem domain bits for the relocation
105  * @write_domain: gem domain bit for the relocation
106  * @delta: delta value to add to @buffer's gpu address
107  *
108  * Emits a fenced relocation into a batch.
109  *
110  * This macro needs a pointer to an #intel_batchbuffer structure called batch in
111  * scope.
112  */
113 #define OUT_RELOC_FENCED(buf, read_domains, write_domain, delta) do {           \
114         igt_assert((delta) >= 0);                                               \
115         intel_batchbuffer_emit_reloc(batch, buf, delta,                 \
116                                      read_domains, write_domain, 1);    \
117 } while (0)
118
119 /**
120  * OUT_RELOC:
121  * @buf: relocation target libdrm buffer object
122  * @read_domains: gem domain bits for the relocation
123  * @write_domain: gem domain bit for the relocation
124  * @delta: delta value to add to @buffer's gpu address
125  *
126  * Emits a normal, unfenced relocation into a batch.
127  *
128  * This macro needs a pointer to an #intel_batchbuffer structure called batch in
129  * scope.
130  */
131 #define OUT_RELOC(buf, read_domains, write_domain, delta) do {          \
132         igt_assert((delta) >= 0);                                               \
133         intel_batchbuffer_emit_reloc(batch, buf, delta,                 \
134                                      read_domains, write_domain, 0);    \
135 } while (0)
136
137 /**
138  * ADVANCE_BATCH:
139  *
140  * Completes the batch command emission sequence started with #BEGIN_BATCH.
141  *
142  * This macro needs a pointer to an #intel_batchbuffer structure called batch in
143  * scope.
144  */
145 #define ADVANCE_BATCH() do {                                            \
146 } while(0)
147
148 #define BLIT_COPY_BATCH_START(devid, flags) do { \
149         if (intel_gen(devid) >= 8) { \
150                 BEGIN_BATCH(10); \
151                 OUT_BATCH(XY_SRC_COPY_BLT_CMD | \
152                                 XY_SRC_COPY_BLT_WRITE_ALPHA | \
153                                 XY_SRC_COPY_BLT_WRITE_RGB | \
154                                 (flags) | 8); \
155         } else { \
156                 BEGIN_BATCH(8); \
157                 OUT_BATCH(XY_SRC_COPY_BLT_CMD | \
158                                 XY_SRC_COPY_BLT_WRITE_ALPHA | \
159                                 XY_SRC_COPY_BLT_WRITE_RGB | \
160                                 (flags) | 6); \
161         } \
162 } while(0)
163
164 #define COLOR_BLIT_COPY_BATCH_START(devid, flags) do { \
165         if (intel_gen(devid) >= 8) { \
166                 BEGIN_BATCH(8); \
167                 OUT_BATCH(MI_NOOP); \
168                 OUT_BATCH(XY_COLOR_BLT_CMD_NOLEN | 0x5 | \
169                                 COLOR_BLT_WRITE_ALPHA | \
170                                 XY_COLOR_BLT_WRITE_RGB); \
171         } else { \
172                 BEGIN_BATCH(6); \
173                 OUT_BATCH(XY_COLOR_BLT_CMD_NOLEN | 0x4 | \
174                                 COLOR_BLT_WRITE_ALPHA | \
175                                 XY_COLOR_BLT_WRITE_RGB); \
176         } \
177 } while(0)
178
179 /**
180  * BLIT_RELOC_UDW:
181  * @devid: pci device id of the drm device
182  *
183  * Emits the upper relocation DWORD on gen8+ and nothing on earlier generations.
184  */
185 #define BLIT_RELOC_UDW(devid) do { \
186         if (intel_gen(devid) >= 8) { \
187                 OUT_BATCH(0); \
188         } \
189 } while(0)
190
191 void
192 intel_blt_copy(struct intel_batchbuffer *batch,
193               drm_intel_bo *src_bo, int src_x1, int src_y1, int src_pitch,
194               drm_intel_bo *dst_bo, int dst_x1, int dst_y1, int dst_pitch,
195               int width, int height, int bpp);
196 void intel_copy_bo(struct intel_batchbuffer *batch,
197                    drm_intel_bo *dst_bo, drm_intel_bo *src_bo,
198                    long int size);
199
200 /**
201  * igt_buf:
202  * @bo: underlying libdrm buffer object
203  * @stride: stride of the buffer
204  * @tiling: tiling mode bits
205  * @data: pointer to the memory mapping of the buffer
206  * @size: size of the buffer object
207  *
208  * This is a i-g-t buffer object wrapper structure which augments the baseline
209  * libdrm buffer object with suitable data needed by the render copy and the
210  * media fill functions.
211  */
212 struct igt_buf {
213     drm_intel_bo *bo;
214     uint32_t stride;
215     uint32_t tiling;
216     uint32_t *data;
217     uint32_t size;
218     /*< private >*/
219     unsigned num_tiles;
220 };
221
222 unsigned igt_buf_width(struct igt_buf *buf);
223 unsigned igt_buf_height(struct igt_buf *buf);
224
225 /**
226  * igt_render_copyfunc_t:
227  * @batch: batchbuffer object
228  * @context: libdrm hardware context to use
229  * @src: source i-g-t buffer object
230  * @src_x: source pixel x-coordination
231  * @src_y: source pixel y-coordination
232  * @width: width of the copied rectangle
233  * @height: height of the copied rectangle
234  * @dst: destination i-g-t buffer object
235  * @dst_x: destination pixel x-coordination
236  * @dst_y: destination pixel y-coordination
237  *
238  * This is the type of the per-platform render copy functions. The
239  * platform-specific implementation can be obtained by calling
240  * igt_get_render_copyfunc().
241  *
242  * A render copy function will emit a batchbuffer to the kernel which executes
243  * the specified blit copy operation using the render engine. @context is
244  * optional and can be NULL.
245  */
246 typedef void (*igt_render_copyfunc_t)(struct intel_batchbuffer *batch,
247                                       drm_intel_context *context,
248                                       struct igt_buf *src, unsigned src_x, unsigned src_y,
249                                       unsigned width, unsigned height,
250                                       struct igt_buf *dst, unsigned dst_x, unsigned dst_y);
251
252 igt_render_copyfunc_t igt_get_render_copyfunc(int devid);
253
254 /**
255  * igt_media_fillfunc_t:
256  * @batch: batchbuffer object
257  * @dst: destination i-g-t buffer object
258  * @x: destination pixel x-coordination
259  * @y: destination pixel y-coordination
260  * @width: width of the filled rectangle
261  * @height: height of the filled rectangle
262  * @color: fill color to use
263  *
264  * This is the type of the per-platform media fill functions. The
265  * platform-specific implementation can be obtained by calling
266  * igt_get_media_fillfunc().
267  *
268  * A media fill function will emit a batchbuffer to the kernel which executes
269  * the specified blit fill operation using the media engine.
270  */
271 typedef void (*igt_media_fillfunc_t)(struct intel_batchbuffer *batch,
272                                      struct igt_buf *dst,
273                                      unsigned x, unsigned y,
274                                      unsigned width, unsigned height,
275                                      uint8_t color);
276
277 igt_media_fillfunc_t igt_get_media_fillfunc(int devid);
278
279 #endif