1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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"
45 * SECTION:intel_batchbuffer
46 * @short_description: Batchbuffer and blitter support
47 * @title: intel batchbuffer
48 * @include: intel_batchbuffer.h
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().
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.
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.
65 * intel_batchbuffer_reset:
66 * @batch: batchbuffer object
68 * Resets @batch by allocating a new gem buffer object as backing storage.
71 intel_batchbuffer_reset(struct intel_batchbuffer *batch)
73 if (batch->bo != NULL) {
74 drm_intel_bo_unreference(batch->bo);
78 batch->bo = drm_intel_bo_alloc(batch->bufmgr, "batchbuffer",
81 memset(batch->buffer, 0, sizeof(batch->buffer));
83 batch->ptr = batch->buffer;
87 * intel_batchbuffer_reset:
88 * @bufmgr: libdrm buffer manager
89 * @devid: pci device id of the drm device
91 * Allocates a new batchbuffer object. @devid must be supplied since libdrm
92 * doesn't expose it directly.
94 * Returns: The allocated and initialized batchbuffer object.
96 struct intel_batchbuffer *
97 intel_batchbuffer_alloc(drm_intel_bufmgr *bufmgr, uint32_t devid)
99 struct intel_batchbuffer *batch = calloc(sizeof(*batch), 1);
101 batch->bufmgr = bufmgr;
102 batch->devid = devid;
103 batch->gen = intel_gen(devid);
104 intel_batchbuffer_reset(batch);
110 * intel_batchbuffer_reset:
111 * @batch: batchbuffer object
113 * Releases all resource of the batchbuffer object @batch.
116 intel_batchbuffer_free(struct intel_batchbuffer *batch)
118 drm_intel_bo_unreference(batch->bo);
123 #define CMD_POLY_STIPPLE_OFFSET 0x7906
126 flush_on_ring_common(struct intel_batchbuffer *batch, int ring)
128 unsigned int used = batch->ptr - batch->buffer;
133 if (IS_GEN5(batch->devid)) {
134 /* emit gen5 w/a without batch space checks - we reserve that
136 *(uint32_t *) (batch->ptr) = CMD_POLY_STIPPLE_OFFSET << 16;
138 *(uint32_t *) (batch->ptr) = 0;
142 /* Round batchbuffer usage to 2 DWORDs. */
143 if ((used & 4) == 0) {
144 *(uint32_t *) (batch->ptr) = 0; /* noop */
148 /* Mark the end of the buffer. */
149 *(uint32_t *)(batch->ptr) = MI_BATCH_BUFFER_END; /* noop */
151 return batch->ptr - batch->buffer;
155 * intel_batchbuffer_flush_on_ring:
156 * @batch: batchbuffer object
157 * @ring: execbuf ring flag
159 * Submits the batch for execution on @ring.
162 intel_batchbuffer_flush_on_ring(struct intel_batchbuffer *batch, int ring)
164 unsigned int used = flush_on_ring_common(batch, ring);
169 do_or_die(drm_intel_bo_subdata(batch->bo, 0, used, batch->buffer));
173 do_or_die(drm_intel_bo_mrb_exec(batch->bo, used, NULL, 0, 0, ring));
175 intel_batchbuffer_reset(batch);
179 * intel_batchbuffer_flush_with_context:
180 * @batch: batchbuffer object
181 * @context: libdrm hardware context object
183 * Submits the batch for execution on the render engine with the supplied
187 intel_batchbuffer_flush_with_context(struct intel_batchbuffer *batch,
188 drm_intel_context *context)
191 unsigned int used = flush_on_ring_common(batch, I915_EXEC_RENDER);
196 ret = drm_intel_bo_subdata(batch->bo, 0, used, batch->buffer);
197 igt_assert(ret == 0);
201 ret = drm_intel_gem_bo_context_exec(batch->bo, context, used,
203 igt_assert(ret == 0);
205 intel_batchbuffer_reset(batch);
209 * intel_batchbuffer_flush:
210 * @batch: batchbuffer object
212 * Submits the batch for execution on the blitter engine, selecting the right
213 * ring depending upon the hardware platform.
216 intel_batchbuffer_flush(struct intel_batchbuffer *batch)
219 if (HAS_BLT_RING(batch->devid))
220 ring = I915_EXEC_BLT;
221 intel_batchbuffer_flush_on_ring(batch, ring);
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
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.
237 * Note that @fenced is only relevant if @buffer is actually tiled.
239 * This is the only way buffers get added to the validate list.
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,
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);
256 ret = drm_intel_bo_emit_reloc_fence(batch->bo, batch->ptr - batch->buffer,
258 read_domains, write_domain);
260 ret = drm_intel_bo_emit_reloc(batch->bo, batch->ptr - batch->buffer,
262 read_domains, write_domain);
264 offset = buffer->offset64;
266 intel_batchbuffer_emit_dword(batch, offset);
268 intel_batchbuffer_emit_dword(batch, offset >> 32);
269 igt_assert(ret == 0);
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
278 * This transfers the given @data into the batchbuffer. Note that the length
279 * must be DWORD aligned, i.e. multiples of 32bits.
282 intel_batchbuffer_data(struct intel_batchbuffer *batch,
283 const void *data, unsigned int bytes)
285 igt_assert((bytes & 3) == 0);
286 intel_batchbuffer_require_space(batch, bytes);
287 memcpy(batch->ptr, data, bytes);
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
306 * This emits a 2D copy operation using blitter commands into the supplied batch
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)
315 const int gen = batch->gen;
316 uint32_t src_tiling, dst_tiling, swizzle;
317 uint32_t cmd_bits = 0;
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 +
327 CHECK_RANGE(src_pitch) && CHECK_RANGE(dst_pitch));
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);
334 drm_intel_bo_get_tiling(src_bo, &src_tiling, &swizzle);
335 drm_intel_bo_get_tiling(dst_bo, &dst_tiling, &swizzle);
337 if (gen >= 4 && src_tiling != I915_TILING_NONE) {
339 cmd_bits |= XY_SRC_COPY_BLT_SRC_TILED;
342 if (gen >= 4 && dst_tiling != I915_TILING_NONE) {
344 cmd_bits |= XY_SRC_COPY_BLT_DST_TILED;
351 case 16: /* supporting only RGB565, not ARGB1555 */
352 br13_bits |= 1 << 24;
355 br13_bits |= 3 << 24;
356 cmd_bits |= XY_SRC_COPY_BLT_WRITE_ALPHA |
357 XY_SRC_COPY_BLT_WRITE_RGB;
363 BEGIN_BATCH(gen >= 8 ? 10 : 8);
364 OUT_BATCH(XY_SRC_COPY_BLT_CMD | cmd_bits |
366 OUT_BATCH((br13_bits) |
367 (0xcc << 16) | /* copy ROP */
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);
377 #define CMD_POLY_STIPPLE_OFFSET 0x7906
379 OUT_BATCH(CMD_POLY_STIPPLE_OFFSET << 16);
383 if (gen >= 6 && src_bo == dst_bo) {
385 OUT_BATCH(XY_SETUP_CLIP_BLT_CMD);
391 intel_batchbuffer_flush(batch);
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
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.
406 intel_copy_bo(struct intel_batchbuffer *batch,
407 drm_intel_bo *dst_bo, drm_intel_bo *src_bo,
410 igt_assert(size % 4096 == 0);
412 intel_blt_copy(batch,
415 4096/4, size/4096, 32);
420 * @buf: the i-g-t buffer object
422 * Computes the widht in 32-bit pixels of the given buffer.
425 * The width of the buffer.
427 unsigned igt_buf_width(struct igt_buf *buf)
429 return buf->stride/sizeof(uint32_t);
434 * @buf: the i-g-t buffer object
436 * Computes the height in 32-bit pixels of the given buffer.
439 * The height of the buffer.
441 unsigned igt_buf_height(struct igt_buf *buf)
443 return buf->size/buf->stride;
447 * igt_get_render_copyfunc:
448 * @devid: pci device id
452 * The platform-specific render copy function pointer for the device
453 * specified with @devid. Will return NULL when no render copy function is
456 igt_render_copyfunc_t igt_get_render_copyfunc(int devid)
458 igt_render_copyfunc_t copy = NULL;
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;
475 * igt_get_media_fillfunc:
476 * @devid: pci device id
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.
483 igt_media_fillfunc_t igt_get_media_fillfunc(int devid)
485 igt_media_fillfunc_t fill = NULL;
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;