b4598fbf54a573f89d1c7f4c4d7737fa10802135
[platform/upstream/intel-gpu-tools.git] / lib / intel_batchbuffer.c
1 /**************************************************************************
2  * 
3  * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
4  * All Rights Reserved.
5  * 
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  * 
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  * 
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  * 
26  **************************************************************************/
27
28 #include <inttypes.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <assert.h>
33
34 #include "drm.h"
35 #include "drmtest.h"
36 #include "intel_batchbuffer.h"
37 #include "intel_bufmgr.h"
38 #include "intel_chipset.h"
39 #include "intel_reg.h"
40 #include "rendercopy.h"
41 #include "media_fill.h"
42 #include <i915_drm.h>
43
44 /**
45  * SECTION:intel_batchbuffer
46  * @short_description: Batchbuffer and blitter support
47  * @title: intel batchbuffer
48  * @include: intel_batchbuffer.h
49  *
50  * This library provides some basic support for batchbuffers and using the
51  * blitter engine based upon libdrm. A new batchbuffer is allocated with
52  * intel_batchbuffer_alloc() and for simple blitter commands submitted with
53  * intel_batchbuffer_flush().
54  *
55  * It also provides some convenient macros to easily emit commands into
56  * batchbuffers. All those macros presume that a pointer to a #intel_batchbuffer
57  * structure called batch is in scope. The basic macros are #BEGIN_BATCH,
58  * #OUT_BATCH, #OUT_RELOC and #ADVANCE_BATCH.
59  *
60  * Note that this library's header pulls in the [i-g-t core](intel-gpu-tools-i-g-t-core.html)
61  * library as a dependency.
62  */
63
64 /**
65  * intel_batchbuffer_reset:
66  * @batch: batchbuffer object
67  *
68  * Resets @batch by allocating a new gem buffer object as backing storage.
69  */
70 void
71 intel_batchbuffer_reset(struct intel_batchbuffer *batch)
72 {
73         if (batch->bo != NULL) {
74                 drm_intel_bo_unreference(batch->bo);
75                 batch->bo = NULL;
76         }
77
78         batch->bo = drm_intel_bo_alloc(batch->bufmgr, "batchbuffer",
79                                        BATCH_SZ, 4096);
80
81         memset(batch->buffer, 0, sizeof(batch->buffer));
82
83         batch->ptr = batch->buffer;
84 }
85
86 /**
87  * intel_batchbuffer_reset:
88  * @bufmgr: libdrm buffer manager
89  * @devid: pci device id of the drm device
90  *
91  * Allocates a new batchbuffer object. @devid must be supplied since libdrm
92  * doesn't expose it directly.
93  *
94  * Returns: The allocated and initialized batchbuffer object.
95  */
96 struct intel_batchbuffer *
97 intel_batchbuffer_alloc(drm_intel_bufmgr *bufmgr, uint32_t devid)
98 {
99         struct intel_batchbuffer *batch = calloc(sizeof(*batch), 1);
100
101         batch->bufmgr = bufmgr;
102         batch->devid = devid;
103         batch->gen = intel_gen(devid);
104         intel_batchbuffer_reset(batch);
105
106         return batch;
107 }
108
109 /**
110  * intel_batchbuffer_reset:
111  * @batch: batchbuffer object
112  *
113  * Releases all resource of the batchbuffer object @batch.
114  */
115 void
116 intel_batchbuffer_free(struct intel_batchbuffer *batch)
117 {
118         drm_intel_bo_unreference(batch->bo);
119         batch->bo = NULL;
120         free(batch);
121 }
122
123 #define CMD_POLY_STIPPLE_OFFSET       0x7906
124
125 static unsigned int
126 flush_on_ring_common(struct intel_batchbuffer *batch, int ring)
127 {
128         unsigned int used = batch->ptr - batch->buffer;
129
130         if (used == 0)
131                 return 0;
132
133         if (IS_GEN5(batch->devid)) {
134                 /* emit gen5 w/a without batch space checks - we reserve that
135                  * already. */
136                 *(uint32_t *) (batch->ptr) = CMD_POLY_STIPPLE_OFFSET << 16;
137                 batch->ptr += 4;
138                 *(uint32_t *) (batch->ptr) = 0;
139                 batch->ptr += 4;
140         }
141
142         /* Round batchbuffer usage to 2 DWORDs. */
143         if ((used & 4) == 0) {
144                 *(uint32_t *) (batch->ptr) = 0; /* noop */
145                 batch->ptr += 4;
146         }
147
148         /* Mark the end of the buffer. */
149         *(uint32_t *)(batch->ptr) = MI_BATCH_BUFFER_END; /* noop */
150         batch->ptr += 4;
151         return batch->ptr - batch->buffer;
152 }
153
154 /**
155  * intel_batchbuffer_flush_on_ring:
156  * @batch: batchbuffer object
157  * @ring: execbuf ring flag
158  *
159  * Submits the batch for execution on @ring.
160  */
161 void
162 intel_batchbuffer_flush_on_ring(struct intel_batchbuffer *batch, int ring)
163 {
164         unsigned int used = flush_on_ring_common(batch, ring);
165
166         if (used == 0)
167                 return;
168
169         do_or_die(drm_intel_bo_subdata(batch->bo, 0, used, batch->buffer));
170
171         batch->ptr = NULL;
172
173         do_or_die(drm_intel_bo_mrb_exec(batch->bo, used, NULL, 0, 0, ring));
174
175         intel_batchbuffer_reset(batch);
176 }
177
178 /**
179  * intel_batchbuffer_flush_with_context:
180  * @batch: batchbuffer object
181  * @context: libdrm hardware context object
182  *
183  * Submits the batch for execution on the render engine with the supplied
184  * hardware context.
185  */
186 void
187 intel_batchbuffer_flush_with_context(struct intel_batchbuffer *batch,
188                                      drm_intel_context *context)
189 {
190         int ret;
191         unsigned int used = flush_on_ring_common(batch, I915_EXEC_RENDER);
192
193         if (used == 0)
194                 return;
195
196         ret = drm_intel_bo_subdata(batch->bo, 0, used, batch->buffer);
197         igt_assert(ret == 0);
198
199         batch->ptr = NULL;
200
201         ret = drm_intel_gem_bo_context_exec(batch->bo, context, used,
202                                             I915_EXEC_RENDER);
203         igt_assert(ret == 0);
204
205         intel_batchbuffer_reset(batch);
206 }
207
208 /**
209  * intel_batchbuffer_flush:
210  * @batch: batchbuffer object
211  *
212  * Submits the batch for execution on the blitter engine, selecting the right
213  * ring depending upon the hardware platform.
214  */
215 void
216 intel_batchbuffer_flush(struct intel_batchbuffer *batch)
217 {
218         int ring = 0;
219         if (HAS_BLT_RING(batch->devid))
220                 ring = I915_EXEC_BLT;
221         intel_batchbuffer_flush_on_ring(batch, ring);
222 }
223
224
225 /**
226  * intel_batchbuffer_emit_reloc:
227  * @batch: batchbuffer object
228  * @buffer: relocation target libdrm buffer object
229  * @delta: delta value to add to @buffer's gpu address
230  * @read_domains: gem domain bits for the relocation
231  * @write_domain: gem domain bit for the relocation
232  * @fenced: whether this gpu access requires fences
233  *
234  * Emits both a libdrm relocation entry pointing at @buffer and the pre-computed
235  * DWORD of @batch's presumed gpu address plus the supplied @delta into @batch.
236  *
237  * Note that @fenced is only relevant if @buffer is actually tiled.
238  *
239  * This is the only way buffers get added to the validate list.
240  */
241 void
242 intel_batchbuffer_emit_reloc(struct intel_batchbuffer *batch,
243                              drm_intel_bo *buffer, uint32_t delta,
244                              uint32_t read_domains, uint32_t write_domain,
245                              int fenced)
246 {
247         int ret;
248
249         if (batch->ptr - batch->buffer > BATCH_SZ)
250                 igt_info("bad relocation ptr %p map %p offset %d size %d\n",
251                          batch->ptr, batch->buffer,
252                          (int)(batch->ptr - batch->buffer), BATCH_SZ);
253
254         if (fenced)
255                 ret = drm_intel_bo_emit_reloc_fence(batch->bo, batch->ptr - batch->buffer,
256                                                     buffer, delta,
257                                                     read_domains, write_domain);
258         else
259                 ret = drm_intel_bo_emit_reloc(batch->bo, batch->ptr - batch->buffer,
260                                               buffer, delta,
261                                               read_domains, write_domain);
262         intel_batchbuffer_emit_dword(batch, buffer->offset + delta);
263         igt_assert(ret == 0);
264 }
265
266 /**
267  * intel_batchbuffer_data:
268  * @batch: batchbuffer object
269  * @data: pointer to the data to write into the batchbuffer
270  * @bytes: number of bytes to write into the batchbuffer
271  *
272  * This transfers the given @data into the batchbuffer. Note that the length
273  * must be DWORD aligned, i.e. multiples of 32bits.
274  */
275 void
276 intel_batchbuffer_data(struct intel_batchbuffer *batch,
277                        const void *data, unsigned int bytes)
278 {
279         igt_assert((bytes & 3) == 0);
280         intel_batchbuffer_require_space(batch, bytes);
281         memcpy(batch->ptr, data, bytes);
282         batch->ptr += bytes;
283 }
284
285 /**
286  * intel_blt_copy:
287  * @batch: batchbuffer object
288  * @src_bo: source libdrm buffer object
289  * @src_x1: source pixel x-coordination
290  * @src_y1: source pixel y-coordination
291  * @src_pitch: @src_bo's pitch in bytes
292  * @dst_bo: destination libdrm buffer object
293  * @dst_x1: destination pixel x-coordination
294  * @dst_y1: destination pixel y-coordination
295  * @dst_pitch: @dst_bo's pitch in bytes
296  * @width: width of the copied rectangle
297  * @height: height of the copied rectangle
298  * @bpp: bits per pixel
299  *
300  * This emits a 2D copy operation using blitter commands into the supplied batch
301  * buffer object.
302  */
303 void
304 intel_blt_copy(struct intel_batchbuffer *batch,
305                drm_intel_bo *src_bo, int src_x1, int src_y1, int src_pitch,
306                drm_intel_bo *dst_bo, int dst_x1, int dst_y1, int dst_pitch,
307                int width, int height, int bpp)
308 {
309         const int gen = batch->gen;
310         uint32_t src_tiling, dst_tiling, swizzle;
311         uint32_t cmd_bits = 0;
312         uint32_t br13_bits;
313
314 #define CHECK_RANGE(x)  ((x) >= 0 && (x) < (1 << 15))
315         igt_assert(CHECK_RANGE(src_x1) && CHECK_RANGE(src_y1) &&
316                    CHECK_RANGE(dst_x1) && CHECK_RANGE(dst_y1) &&
317                    CHECK_RANGE(width) && CHECK_RANGE(height) &&
318                    CHECK_RANGE(src_x1 + width) && CHECK_RANGE(src_y1 + height)
319                    && CHECK_RANGE(dst_x1 + width) && CHECK_RANGE(dst_y1 +
320                                                                  height) &&
321                    CHECK_RANGE(src_pitch) && CHECK_RANGE(dst_pitch));
322 #undef CHECK_RANGE
323         igt_assert(bpp*(src_x1 + width) <= 8*src_pitch);
324         igt_assert(bpp*(dst_x1 + width) <= 8*dst_pitch);
325         igt_assert(src_pitch * (src_y1 + height) <= src_bo->size);
326         igt_assert(dst_pitch * (dst_y1 + height) <= dst_bo->size);
327
328         drm_intel_bo_get_tiling(src_bo, &src_tiling, &swizzle);
329         drm_intel_bo_get_tiling(dst_bo, &dst_tiling, &swizzle);
330
331         if (gen >= 4 && src_tiling != I915_TILING_NONE) {
332                 src_pitch /= 4;
333                 cmd_bits |= XY_SRC_COPY_BLT_SRC_TILED;
334         }
335
336         if (gen >= 4 && dst_tiling != I915_TILING_NONE) {
337                 dst_pitch /= 4;
338                 cmd_bits |= XY_SRC_COPY_BLT_DST_TILED;
339         }
340
341         br13_bits = 0;
342         switch (bpp) {
343         case 8:
344                 break;
345         case 16:                /* supporting only RGB565, not ARGB1555 */
346                 br13_bits |= 1 << 24;
347                 break;
348         case 32:
349                 br13_bits |= 3 << 24;
350                 cmd_bits |= XY_SRC_COPY_BLT_WRITE_ALPHA |
351                             XY_SRC_COPY_BLT_WRITE_RGB;
352                 break;
353         default:
354                 igt_fail(1);
355         }
356
357         BEGIN_BATCH(gen >= 8 ? 10 : 8);
358         OUT_BATCH(XY_SRC_COPY_BLT_CMD | cmd_bits |
359                   (gen >= 8 ? 8 : 6));
360         OUT_BATCH((br13_bits) |
361                   (0xcc << 16) | /* copy ROP */
362                   dst_pitch);
363         OUT_BATCH((dst_y1 << 16) | dst_x1); /* dst x1,y1 */
364         OUT_BATCH(((dst_y1 + height) << 16) | (dst_x1 + width)); /* dst x2,y2 */
365         OUT_RELOC(dst_bo, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER, 0);
366         BLIT_RELOC_UDW(batch->devid);
367         OUT_BATCH((src_y1 << 16) | src_x1); /* src x1,y1 */
368         OUT_BATCH(src_pitch);
369         OUT_RELOC(src_bo, I915_GEM_DOMAIN_RENDER, 0, 0);
370         BLIT_RELOC_UDW(batch->devid);
371         ADVANCE_BATCH();
372
373         intel_batchbuffer_flush(batch);
374 }
375
376 /**
377  * intel_copy_bo:
378  * @batch: batchbuffer object
379  * @src_bo: source libdrm buffer object
380  * @dst_bo: destination libdrm buffer object
381  * @size: size of the copy range in bytes
382  *
383  * This emits a copy operation using blitter commands into the supplied batch
384  * buffer object. A total of @size bytes from the start of @src_bo is copied
385  * over to @dst_bo. Note that @size must be page-aligned.
386  */
387 void
388 intel_copy_bo(struct intel_batchbuffer *batch,
389               drm_intel_bo *dst_bo, drm_intel_bo *src_bo,
390               long int size)
391 {
392         igt_assert(size % 4096 == 0);
393
394         intel_blt_copy(batch,
395                        src_bo, 0, 0, 4096,
396                        dst_bo, 0, 0, 4096,
397                        4096/4, size/4096, 32);
398 }
399
400 /**
401  * igt_buf_width:
402  * @buf: the i-g-t buffer object
403  *
404  * Computes the widht in 32-bit pixels of the given buffer.
405  *
406  * Returns:
407  * The width of the buffer.
408  */
409 unsigned igt_buf_width(struct igt_buf *buf)
410 {
411         return buf->stride/sizeof(uint32_t);
412 }
413
414 /**
415  * igt_buf_height:
416  * @buf: the i-g-t buffer object
417  *
418  * Computes the height in 32-bit pixels of the given buffer.
419  *
420  * Returns:
421  * The height of the buffer.
422  */
423 unsigned igt_buf_height(struct igt_buf *buf)
424 {
425         return buf->size/buf->stride;
426 }
427
428 /**
429  * igt_get_render_copyfunc:
430  * @devid: pci device id
431  *
432  * Returns:
433  *
434  * The platform-specific render copy function pointer for the device
435  * specified with @devid. Will return NULL when no render copy function is
436  * implemented.
437  */
438 igt_render_copyfunc_t igt_get_render_copyfunc(int devid)
439 {
440         igt_render_copyfunc_t copy = NULL;
441
442         if (IS_GEN2(devid))
443                 copy = gen2_render_copyfunc;
444         else if (IS_GEN3(devid))
445                 copy = gen3_render_copyfunc;
446         else if (IS_GEN6(devid))
447                 copy = gen6_render_copyfunc;
448         else if (IS_GEN7(devid))
449                 copy = gen7_render_copyfunc;
450         else if (IS_GEN8(devid))
451                 copy = gen8_render_copyfunc;
452
453         return copy;
454 }
455
456 /**
457  * igt_get_media_fillfunc:
458  * @devid: pci device id
459  *
460  * Returns:
461  *
462  * The platform-specific media fill function pointer for the device specified
463  * with @devid. Will return NULL when no media fill function is implemented.
464  */
465 igt_media_fillfunc_t igt_get_media_fillfunc(int devid)
466 {
467         igt_media_fillfunc_t fill = NULL;
468
469         if (IS_BROADWELL(devid))
470                 fill = gen8_media_fillfunc;
471         else if (IS_GEN7(devid))
472                 fill = gen7_media_fillfunc;
473         else if (IS_CHERRYVIEW(devid))
474                 fill = gen8lp_media_fillfunc;
475
476         return fill;
477 }