Prepare for 64bit relocation addresses
[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, uint64_t delta,
244                              uint32_t read_domains, uint32_t write_domain,
245                              int fenced)
246 {
247         uint64_t offset;
248         int ret;
249
250         if (batch->ptr - batch->buffer > BATCH_SZ)
251                 igt_info("bad relocation ptr %p map %p offset %d size %d\n",
252                          batch->ptr, batch->buffer,
253                          (int)(batch->ptr - batch->buffer), BATCH_SZ);
254
255         if (fenced)
256                 ret = drm_intel_bo_emit_reloc_fence(batch->bo, batch->ptr - batch->buffer,
257                                                     buffer, delta,
258                                                     read_domains, write_domain);
259         else
260                 ret = drm_intel_bo_emit_reloc(batch->bo, batch->ptr - batch->buffer,
261                                               buffer, delta,
262                                               read_domains, write_domain);
263
264         offset = buffer->offset64;
265         offset += delta;
266         intel_batchbuffer_emit_dword(batch, offset);
267         if (batch->gen >= 8)
268                 intel_batchbuffer_emit_dword(batch, offset >> 32);
269         igt_assert(ret == 0);
270 }
271
272 /**
273  * intel_batchbuffer_data:
274  * @batch: batchbuffer object
275  * @data: pointer to the data to write into the batchbuffer
276  * @bytes: number of bytes to write into the batchbuffer
277  *
278  * This transfers the given @data into the batchbuffer. Note that the length
279  * must be DWORD aligned, i.e. multiples of 32bits.
280  */
281 void
282 intel_batchbuffer_data(struct intel_batchbuffer *batch,
283                        const void *data, unsigned int bytes)
284 {
285         igt_assert((bytes & 3) == 0);
286         intel_batchbuffer_require_space(batch, bytes);
287         memcpy(batch->ptr, data, bytes);
288         batch->ptr += bytes;
289 }
290
291 /**
292  * intel_blt_copy:
293  * @batch: batchbuffer object
294  * @src_bo: source libdrm buffer object
295  * @src_x1: source pixel x-coordination
296  * @src_y1: source pixel y-coordination
297  * @src_pitch: @src_bo's pitch in bytes
298  * @dst_bo: destination libdrm buffer object
299  * @dst_x1: destination pixel x-coordination
300  * @dst_y1: destination pixel y-coordination
301  * @dst_pitch: @dst_bo's pitch in bytes
302  * @width: width of the copied rectangle
303  * @height: height of the copied rectangle
304  * @bpp: bits per pixel
305  *
306  * This emits a 2D copy operation using blitter commands into the supplied batch
307  * buffer object.
308  */
309 void
310 intel_blt_copy(struct intel_batchbuffer *batch,
311                drm_intel_bo *src_bo, int src_x1, int src_y1, int src_pitch,
312                drm_intel_bo *dst_bo, int dst_x1, int dst_y1, int dst_pitch,
313                int width, int height, int bpp)
314 {
315         const int gen = batch->gen;
316         uint32_t src_tiling, dst_tiling, swizzle;
317         uint32_t cmd_bits = 0;
318         uint32_t br13_bits;
319
320 #define CHECK_RANGE(x)  ((x) >= 0 && (x) < (1 << 15))
321         igt_assert(CHECK_RANGE(src_x1) && CHECK_RANGE(src_y1) &&
322                    CHECK_RANGE(dst_x1) && CHECK_RANGE(dst_y1) &&
323                    CHECK_RANGE(width) && CHECK_RANGE(height) &&
324                    CHECK_RANGE(src_x1 + width) && CHECK_RANGE(src_y1 + height)
325                    && CHECK_RANGE(dst_x1 + width) && CHECK_RANGE(dst_y1 +
326                                                                  height) &&
327                    CHECK_RANGE(src_pitch) && CHECK_RANGE(dst_pitch));
328 #undef CHECK_RANGE
329         igt_assert(bpp*(src_x1 + width) <= 8*src_pitch);
330         igt_assert(bpp*(dst_x1 + width) <= 8*dst_pitch);
331         igt_assert(src_pitch * (src_y1 + height) <= src_bo->size);
332         igt_assert(dst_pitch * (dst_y1 + height) <= dst_bo->size);
333
334         drm_intel_bo_get_tiling(src_bo, &src_tiling, &swizzle);
335         drm_intel_bo_get_tiling(dst_bo, &dst_tiling, &swizzle);
336
337         if (gen >= 4 && src_tiling != I915_TILING_NONE) {
338                 src_pitch /= 4;
339                 cmd_bits |= XY_SRC_COPY_BLT_SRC_TILED;
340         }
341
342         if (gen >= 4 && dst_tiling != I915_TILING_NONE) {
343                 dst_pitch /= 4;
344                 cmd_bits |= XY_SRC_COPY_BLT_DST_TILED;
345         }
346
347         br13_bits = 0;
348         switch (bpp) {
349         case 8:
350                 break;
351         case 16:                /* supporting only RGB565, not ARGB1555 */
352                 br13_bits |= 1 << 24;
353                 break;
354         case 32:
355                 br13_bits |= 3 << 24;
356                 cmd_bits |= XY_SRC_COPY_BLT_WRITE_ALPHA |
357                             XY_SRC_COPY_BLT_WRITE_RGB;
358                 break;
359         default:
360                 igt_fail(1);
361         }
362
363         BEGIN_BATCH(gen >= 8 ? 10 : 8);
364         OUT_BATCH(XY_SRC_COPY_BLT_CMD | cmd_bits |
365                   (gen >= 8 ? 8 : 6));
366         OUT_BATCH((br13_bits) |
367                   (0xcc << 16) | /* copy ROP */
368                   dst_pitch);
369         OUT_BATCH((dst_y1 << 16) | dst_x1); /* dst x1,y1 */
370         OUT_BATCH(((dst_y1 + height) << 16) | (dst_x1 + width)); /* dst x2,y2 */
371         OUT_RELOC_FENCED(dst_bo, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER, 0);
372         OUT_BATCH((src_y1 << 16) | src_x1); /* src x1,y1 */
373         OUT_BATCH(src_pitch);
374         OUT_RELOC_FENCED(src_bo, I915_GEM_DOMAIN_RENDER, 0, 0);
375         ADVANCE_BATCH();
376
377 #define CMD_POLY_STIPPLE_OFFSET       0x7906
378         if (gen == 5) {
379                 OUT_BATCH(CMD_POLY_STIPPLE_OFFSET << 16);
380                 OUT_BATCH(0);
381         }
382
383         if (gen >= 6 && src_bo == dst_bo) {
384                 BEGIN_BATCH(3);
385                 OUT_BATCH(XY_SETUP_CLIP_BLT_CMD);
386                 OUT_BATCH(0);
387                 OUT_BATCH(0);
388                 ADVANCE_BATCH();
389         }
390
391         intel_batchbuffer_flush(batch);
392 }
393
394 /**
395  * intel_copy_bo:
396  * @batch: batchbuffer object
397  * @src_bo: source libdrm buffer object
398  * @dst_bo: destination libdrm buffer object
399  * @size: size of the copy range in bytes
400  *
401  * This emits a copy operation using blitter commands into the supplied batch
402  * buffer object. A total of @size bytes from the start of @src_bo is copied
403  * over to @dst_bo. Note that @size must be page-aligned.
404  */
405 void
406 intel_copy_bo(struct intel_batchbuffer *batch,
407               drm_intel_bo *dst_bo, drm_intel_bo *src_bo,
408               long int size)
409 {
410         igt_assert(size % 4096 == 0);
411
412         intel_blt_copy(batch,
413                        src_bo, 0, 0, 4096,
414                        dst_bo, 0, 0, 4096,
415                        4096/4, size/4096, 32);
416 }
417
418 /**
419  * igt_buf_width:
420  * @buf: the i-g-t buffer object
421  *
422  * Computes the widht in 32-bit pixels of the given buffer.
423  *
424  * Returns:
425  * The width of the buffer.
426  */
427 unsigned igt_buf_width(struct igt_buf *buf)
428 {
429         return buf->stride/sizeof(uint32_t);
430 }
431
432 /**
433  * igt_buf_height:
434  * @buf: the i-g-t buffer object
435  *
436  * Computes the height in 32-bit pixels of the given buffer.
437  *
438  * Returns:
439  * The height of the buffer.
440  */
441 unsigned igt_buf_height(struct igt_buf *buf)
442 {
443         return buf->size/buf->stride;
444 }
445
446 /**
447  * igt_get_render_copyfunc:
448  * @devid: pci device id
449  *
450  * Returns:
451  *
452  * The platform-specific render copy function pointer for the device
453  * specified with @devid. Will return NULL when no render copy function is
454  * implemented.
455  */
456 igt_render_copyfunc_t igt_get_render_copyfunc(int devid)
457 {
458         igt_render_copyfunc_t copy = NULL;
459
460         if (IS_GEN2(devid))
461                 copy = gen2_render_copyfunc;
462         else if (IS_GEN3(devid))
463                 copy = gen3_render_copyfunc;
464         else if (IS_GEN6(devid))
465                 copy = gen6_render_copyfunc;
466         else if (IS_GEN7(devid))
467                 copy = gen7_render_copyfunc;
468         else if (IS_GEN8(devid))
469                 copy = gen8_render_copyfunc;
470
471         return copy;
472 }
473
474 /**
475  * igt_get_media_fillfunc:
476  * @devid: pci device id
477  *
478  * Returns:
479  *
480  * The platform-specific media fill function pointer for the device specified
481  * with @devid. Will return NULL when no media fill function is implemented.
482  */
483 igt_media_fillfunc_t igt_get_media_fillfunc(int devid)
484 {
485         igt_media_fillfunc_t fill = NULL;
486
487         if (IS_BROADWELL(devid))
488                 fill = gen8_media_fillfunc;
489         else if (IS_GEN7(devid))
490                 fill = gen7_media_fillfunc;
491         else if (IS_CHERRYVIEW(devid))
492                 fill = gen8lp_media_fillfunc;
493
494         return fill;
495 }