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