blob: 0b0adce7892ccacb6991da65626897ce7477b6d4 [file] [log] [blame]
Daniel Vetter3dba47e2013-08-06 22:27:37 +02001/*
2 * Copyright © 2009,2012,2013 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 * Chris Wilson <chris@chris-wilson.co.uk>
26 * Daniel Vetter <daniel.vetter@ffwll.ch>
27 *
28 */
29
Daniel Vetter43779e32013-08-14 14:50:50 +020030/** @file gem_concurrent_blit.c
Daniel Vetter3dba47e2013-08-06 22:27:37 +020031 *
32 * This is a test of pread/pwrite behavior when writing to active
33 * buffers.
34 *
35 * Based on gem_gtt_concurrent_blt.
36 */
37
38#include <stdlib.h>
39#include <stdio.h>
40#include <string.h>
Daniel Vetter3dba47e2013-08-06 22:27:37 +020041#include <fcntl.h>
42#include <inttypes.h>
43#include <errno.h>
44#include <sys/stat.h>
45#include <sys/time.h>
Chris Wilson99431a42013-08-14 11:03:34 +010046#include <sys/wait.h>
Daniel Vetterf5daeec2014-03-23 13:35:09 +010047
48#include <drm.h>
49
Daniel Vettere49ceb82014-03-22 21:07:37 +010050#include "ioctl_wrappers.h"
Daniel Vetter3dba47e2013-08-06 22:27:37 +020051#include "drmtest.h"
Chris Wilson16bafdf2014-09-04 09:26:24 +010052#include "igt_aux.h"
53#include "igt_core.h"
54#include "igt_gt.h"
Daniel Vetter3dba47e2013-08-06 22:27:37 +020055#include "intel_bufmgr.h"
56#include "intel_batchbuffer.h"
Daniel Vetterc03c6ce2014-03-22 21:34:29 +010057#include "intel_io.h"
Daniel Vettere49ceb82014-03-22 21:07:37 +010058#include "intel_chipset.h"
Daniel Vetter3dba47e2013-08-06 22:27:37 +020059
Thomas Woodb2ac2642014-11-28 11:02:44 +000060IGT_TEST_DESCRIPTION("Test of pread/pwrite behavior when writing to active"
61 " buffers.");
62
Chris Wilson6c428a62014-08-29 13:11:37 +010063int fd, devid, gen;
64struct intel_batchbuffer *batch;
65
Daniel Vetter3dba47e2013-08-06 22:27:37 +020066static void
Chris Wilsonf2a045f2015-01-02 16:33:33 +053067nop_release_bo(drm_intel_bo *bo)
68{
69 drm_intel_bo_unreference(bo);
70}
71
72static void
Daniel Vetter43779e32013-08-14 14:50:50 +020073prw_set_bo(drm_intel_bo *bo, uint32_t val, int width, int height)
Daniel Vetter3dba47e2013-08-06 22:27:37 +020074{
Chris Wilson0b4c33f2014-01-26 14:36:32 +000075 int size = width * height, i;
76 uint32_t *tmp;
Daniel Vetter3dba47e2013-08-06 22:27:37 +020077
Chris Wilson0b4c33f2014-01-26 14:36:32 +000078 tmp = malloc(4*size);
79 if (tmp) {
80 for (i = 0; i < size; i++)
81 tmp[i] = val;
82 drm_intel_bo_subdata(bo, 0, 4*size, tmp);
83 free(tmp);
84 } else {
85 for (i = 0; i < size; i++)
86 drm_intel_bo_subdata(bo, 4*i, 4, &val);
87 }
Daniel Vetter3dba47e2013-08-06 22:27:37 +020088}
89
90static void
Chris Wilsonc12f2922014-08-31 16:14:40 +010091prw_cmp_bo(drm_intel_bo *bo, uint32_t val, int width, int height, drm_intel_bo *tmp)
Daniel Vetter3dba47e2013-08-06 22:27:37 +020092{
Chris Wilson0b4c33f2014-01-26 14:36:32 +000093 int size = width * height, i;
Chris Wilsonc12f2922014-08-31 16:14:40 +010094 uint32_t *vaddr;
Daniel Vetter3dba47e2013-08-06 22:27:37 +020095
Chris Wilsonc12f2922014-08-31 16:14:40 +010096 do_or_die(drm_intel_bo_map(tmp, true));
97 do_or_die(drm_intel_bo_get_subdata(bo, 0, 4*size, tmp->virtual));
98 vaddr = tmp->virtual;
99 for (i = 0; i < size; i++)
100 igt_assert_eq_u32(vaddr[i], val);
101 drm_intel_bo_unmap(tmp);
Daniel Vetter3dba47e2013-08-06 22:27:37 +0200102}
103
104static drm_intel_bo *
Chris Wilson86055df2014-08-29 17:36:29 +0100105unmapped_create_bo(drm_intel_bufmgr *bufmgr, int width, int height)
Daniel Vetter3dba47e2013-08-06 22:27:37 +0200106{
107 drm_intel_bo *bo;
108
109 bo = drm_intel_bo_alloc(bufmgr, "bo", 4*width*height, 0);
Daniel Vetter83440952013-08-13 12:35:58 +0200110 igt_assert(bo);
Daniel Vetter3dba47e2013-08-06 22:27:37 +0200111
112 return bo;
113}
114
Daniel Vetter43779e32013-08-14 14:50:50 +0200115static void
116gtt_set_bo(drm_intel_bo *bo, uint32_t val, int width, int height)
Daniel Vetter3dba47e2013-08-06 22:27:37 +0200117{
Chris Wilson3e766b82014-09-26 07:55:49 +0100118 uint32_t *vaddr = bo->virtual;
Daniel Vetter43779e32013-08-14 14:50:50 +0200119 int size = width * height;
Daniel Vetter43779e32013-08-14 14:50:50 +0200120
121 drm_intel_gem_bo_start_gtt_access(bo, true);
Daniel Vetter43779e32013-08-14 14:50:50 +0200122 while (size--)
123 *vaddr++ = val;
124}
125
126static void
Chris Wilsonc12f2922014-08-31 16:14:40 +0100127gtt_cmp_bo(drm_intel_bo *bo, uint32_t val, int width, int height, drm_intel_bo *tmp)
Daniel Vetter43779e32013-08-14 14:50:50 +0200128{
Chris Wilson3e766b82014-09-26 07:55:49 +0100129 uint32_t *vaddr = bo->virtual;
130 int y;
Daniel Vetter43779e32013-08-14 14:50:50 +0200131
Chris Wilson3e766b82014-09-26 07:55:49 +0100132 /* GTT access is slow. So we just compare a few points */
Daniel Vetter43779e32013-08-14 14:50:50 +0200133 drm_intel_gem_bo_start_gtt_access(bo, false);
Chris Wilson3e766b82014-09-26 07:55:49 +0100134 for (y = 0; y < height; y++)
135 igt_assert_eq_u32(vaddr[y*width+y], val);
Daniel Vetter43779e32013-08-14 14:50:50 +0200136}
137
138static drm_intel_bo *
Chris Wilson86055df2014-08-29 17:36:29 +0100139map_bo(drm_intel_bo *bo)
Daniel Vetter43779e32013-08-14 14:50:50 +0200140{
Daniel Vetter43779e32013-08-14 14:50:50 +0200141 /* gtt map doesn't have a write parameter, so just keep the mapping
142 * around (to avoid the set_domain with the gtt write domain set) and
143 * manually tell the kernel when we start access the gtt. */
144 do_or_die(drm_intel_gem_bo_map_gtt(bo));
145
146 return bo;
147}
148
Chris Wilson86055df2014-08-29 17:36:29 +0100149static drm_intel_bo *
150tile_bo(drm_intel_bo *bo, int width)
151{
152 uint32_t tiling = I915_TILING_X;
153 uint32_t stride = width * 4;
154
155 do_or_die(drm_intel_bo_set_tiling(bo, &tiling, stride));
156
157 return bo;
158}
159
160static drm_intel_bo *
161gtt_create_bo(drm_intel_bufmgr *bufmgr, int width, int height)
162{
163 return map_bo(unmapped_create_bo(bufmgr, width, height));
164}
165
166static drm_intel_bo *
167gttX_create_bo(drm_intel_bufmgr *bufmgr, int width, int height)
168{
169 return tile_bo(gtt_create_bo(bufmgr, width, height), width);
170}
171
172static drm_intel_bo *
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530173wc_create_bo(drm_intel_bufmgr *bufmgr, int width, int height)
174{
175 drm_intel_bo *bo;
176
Daniel Vettera3e34ce2015-02-06 11:05:28 +0100177 gem_require_mmap_wc(fd);
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530178
179 bo = unmapped_create_bo(bufmgr, width, height);
180 bo->virtual = gem_mmap__wc(fd, bo->handle, 0, bo->size, PROT_READ | PROT_WRITE);
181 return bo;
182}
183
184static void
185wc_release_bo(drm_intel_bo *bo)
186{
187 munmap(bo->virtual, bo->size);
188 bo->virtual = NULL;
189
190 nop_release_bo(bo);
191}
192
193static drm_intel_bo *
Chris Wilson86055df2014-08-29 17:36:29 +0100194gpu_create_bo(drm_intel_bufmgr *bufmgr, int width, int height)
195{
196 return unmapped_create_bo(bufmgr, width, height);
197}
198
199
200static drm_intel_bo *
201gpuX_create_bo(drm_intel_bufmgr *bufmgr, int width, int height)
202{
203 return tile_bo(gpu_create_bo(bufmgr, width, height), width);
204}
205
Daniel Vetter43779e32013-08-14 14:50:50 +0200206static void
207cpu_set_bo(drm_intel_bo *bo, uint32_t val, int width, int height)
208{
209 int size = width * height;
210 uint32_t *vaddr;
211
212 do_or_die(drm_intel_bo_map(bo, true));
213 vaddr = bo->virtual;
214 while (size--)
215 *vaddr++ = val;
216 drm_intel_bo_unmap(bo);
217}
218
219static void
Chris Wilsonc12f2922014-08-31 16:14:40 +0100220cpu_cmp_bo(drm_intel_bo *bo, uint32_t val, int width, int height, drm_intel_bo *tmp)
Daniel Vetter43779e32013-08-14 14:50:50 +0200221{
222 int size = width * height;
223 uint32_t *vaddr;
224
225 do_or_die(drm_intel_bo_map(bo, false));
226 vaddr = bo->virtual;
227 while (size--)
Chris Wilson6c428a62014-08-29 13:11:37 +0100228 igt_assert_eq_u32(*vaddr++, val);
Daniel Vetter43779e32013-08-14 14:50:50 +0200229 drm_intel_bo_unmap(bo);
230}
231
Chris Wilson6c428a62014-08-29 13:11:37 +0100232static void
233gpu_set_bo(drm_intel_bo *bo, uint32_t val, int width, int height)
234{
235 struct drm_i915_gem_relocation_entry reloc[1];
236 struct drm_i915_gem_exec_object2 gem_exec[2];
237 struct drm_i915_gem_execbuffer2 execbuf;
238 struct drm_i915_gem_pwrite gem_pwrite;
239 struct drm_i915_gem_create create;
240 uint32_t buf[10], *b;
Chris Wilson86055df2014-08-29 17:36:29 +0100241 uint32_t tiling, swizzle;
242
243 drm_intel_bo_get_tiling(bo, &tiling, &swizzle);
Chris Wilson6c428a62014-08-29 13:11:37 +0100244
245 memset(reloc, 0, sizeof(reloc));
246 memset(gem_exec, 0, sizeof(gem_exec));
247 memset(&execbuf, 0, sizeof(execbuf));
248
249 b = buf;
250 *b++ = XY_COLOR_BLT_CMD_NOLEN |
251 ((gen >= 8) ? 5 : 4) |
252 COLOR_BLT_WRITE_ALPHA | XY_COLOR_BLT_WRITE_RGB;
Chris Wilson86055df2014-08-29 17:36:29 +0100253 if (gen >= 4 && tiling) {
254 b[-1] |= XY_COLOR_BLT_TILED;
255 *b = width;
256 } else
257 *b = width << 2;
258 *b++ |= 0xf0 << 16 | 1 << 25 | 1 << 24;
Chris Wilson6c428a62014-08-29 13:11:37 +0100259 *b++ = 0;
260 *b++ = height << 16 | width;
261 reloc[0].offset = (b - buf) * sizeof(uint32_t);
262 reloc[0].target_handle = bo->handle;
263 reloc[0].read_domains = I915_GEM_DOMAIN_RENDER;
264 reloc[0].write_domain = I915_GEM_DOMAIN_RENDER;
265 *b++ = 0;
266 if (gen >= 8)
267 *b++ = 0;
268 *b++ = val;
269 *b++ = MI_BATCH_BUFFER_END;
270 if ((b - buf) & 1)
271 *b++ = 0;
272
273 gem_exec[0].handle = bo->handle;
274 gem_exec[0].flags = EXEC_OBJECT_NEEDS_FENCE;
275
276 create.handle = 0;
277 create.size = 4096;
278 drmIoctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
279 gem_exec[1].handle = create.handle;
280 gem_exec[1].relocation_count = 1;
281 gem_exec[1].relocs_ptr = (uintptr_t)reloc;
282
283 execbuf.buffers_ptr = (uintptr_t)gem_exec;
284 execbuf.buffer_count = 2;
285 execbuf.batch_len = (b - buf) * sizeof(buf[0]);
Chris Wilson86055df2014-08-29 17:36:29 +0100286 if (gen >= 6)
287 execbuf.flags = I915_EXEC_BLT;
Chris Wilson6c428a62014-08-29 13:11:37 +0100288
289 gem_pwrite.handle = gem_exec[1].handle;
290 gem_pwrite.offset = 0;
291 gem_pwrite.size = execbuf.batch_len;
292 gem_pwrite.data_ptr = (uintptr_t)buf;
Chris Wilsonc12f2922014-08-31 16:14:40 +0100293 do_or_die(drmIoctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &gem_pwrite));
294 do_or_die(drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf));
Chris Wilson6c428a62014-08-29 13:11:37 +0100295
296 drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &create.handle);
297}
298
299static void
Chris Wilsonc12f2922014-08-31 16:14:40 +0100300gpu_cmp_bo(drm_intel_bo *bo, uint32_t val, int width, int height, drm_intel_bo *tmp)
Chris Wilson6c428a62014-08-29 13:11:37 +0100301{
Chris Wilson6c428a62014-08-29 13:11:37 +0100302 intel_copy_bo(batch, tmp, bo, width*height*4);
Chris Wilsonc12f2922014-08-31 16:14:40 +0100303 cpu_cmp_bo(tmp, val, width, height, NULL);
Chris Wilson6c428a62014-08-29 13:11:37 +0100304}
305
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530306const struct access_mode {
307 const char *name;
Daniel Vetter43779e32013-08-14 14:50:50 +0200308 void (*set_bo)(drm_intel_bo *bo, uint32_t val, int w, int h);
Chris Wilsonc12f2922014-08-31 16:14:40 +0100309 void (*cmp_bo)(drm_intel_bo *bo, uint32_t val, int w, int h, drm_intel_bo *tmp);
Chris Wilson86055df2014-08-29 17:36:29 +0100310 drm_intel_bo *(*create_bo)(drm_intel_bufmgr *bufmgr, int width, int height);
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530311 void (*release_bo)(drm_intel_bo *bo);
312} access_modes[] = {
313 {
314 .name = "prw",
315 .set_bo = prw_set_bo,
316 .cmp_bo = prw_cmp_bo,
317 .create_bo = unmapped_create_bo,
318 .release_bo = nop_release_bo,
319 },
320 {
321 .name = "cpu",
322 .set_bo = cpu_set_bo,
323 .cmp_bo = cpu_cmp_bo,
324 .create_bo = unmapped_create_bo,
325 .release_bo = nop_release_bo,
326 },
327 {
328 .name = "gtt",
329 .set_bo = gtt_set_bo,
330 .cmp_bo = gtt_cmp_bo,
331 .create_bo = gtt_create_bo,
332 .release_bo = nop_release_bo,
333 },
334 {
335 .name = "gttX",
336 .set_bo = gtt_set_bo,
337 .cmp_bo = gtt_cmp_bo,
338 .create_bo = gttX_create_bo,
339 .release_bo = nop_release_bo,
340 },
341 {
342 .name = "wc",
343 .set_bo = gtt_set_bo,
344 .cmp_bo = gtt_cmp_bo,
345 .create_bo = wc_create_bo,
346 .release_bo = wc_release_bo,
347 },
348 {
349 .name = "gpu",
350 .set_bo = gpu_set_bo,
351 .cmp_bo = gpu_cmp_bo,
352 .create_bo = gpu_create_bo,
353 .release_bo = nop_release_bo,
354 },
355 {
356 .name = "gpuX",
357 .set_bo = gpu_set_bo,
358 .cmp_bo = gpu_cmp_bo,
359 .create_bo = gpuX_create_bo,
360 .release_bo = nop_release_bo,
361 },
Daniel Vetter43779e32013-08-14 14:50:50 +0200362};
363
Chris Wilson1ca607b2013-08-16 09:44:13 +0100364#define MAX_NUM_BUFFERS 1024
Chris Wilson6c428a62014-08-29 13:11:37 +0100365int num_buffers = MAX_NUM_BUFFERS;
Chris Wilson86055df2014-08-29 17:36:29 +0100366const int width = 512, height = 512;
Chris Wilson59c55622014-08-29 13:11:37 +0100367igt_render_copyfunc_t rendercopy;
368
369typedef void (*do_copy)(drm_intel_bo *dst, drm_intel_bo *src);
Chris Wilson16bafdf2014-09-04 09:26:24 +0100370typedef struct igt_hang_ring (*do_hang)(void);
Chris Wilson59c55622014-08-29 13:11:37 +0100371
372static void render_copy_bo(drm_intel_bo *dst, drm_intel_bo *src)
373{
374 struct igt_buf d = {
375 .bo = dst,
376 .size = width * height * 4,
Chris Wilson59c55622014-08-29 13:11:37 +0100377 .num_tiles = width * height * 4,
378 .stride = width * 4,
379 }, s = {
380 .bo = src,
381 .size = width * height * 4,
Chris Wilson59c55622014-08-29 13:11:37 +0100382 .num_tiles = width * height * 4,
383 .stride = width * 4,
384 };
Chris Wilson86055df2014-08-29 17:36:29 +0100385 uint32_t swizzle;
386
387 drm_intel_bo_get_tiling(dst, &d.tiling, &swizzle);
388 drm_intel_bo_get_tiling(src, &s.tiling, &swizzle);
389
Chris Wilson59c55622014-08-29 13:11:37 +0100390 rendercopy(batch, NULL,
391 &s, 0, 0,
392 width, height,
393 &d, 0, 0);
394}
395
396static void blt_copy_bo(drm_intel_bo *dst, drm_intel_bo *src)
397{
Chris Wilson86055df2014-08-29 17:36:29 +0100398 intel_blt_copy(batch,
399 src, 0, 0, 4*width,
400 dst, 0, 0, 4*width,
401 width, height, 32);
Chris Wilson59c55622014-08-29 13:11:37 +0100402}
Daniel Vetter5a598c92013-08-14 15:08:05 +0200403
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530404static void cpu_copy_bo(drm_intel_bo *dst, drm_intel_bo *src)
405{
406 const int size = width * height * sizeof(uint32_t);
407 void *d, *s;
408
409 gem_set_domain(fd, src->handle, I915_GEM_DOMAIN_CPU, 0);
410 gem_set_domain(fd, dst->handle, I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
411 s = gem_mmap__cpu(fd, src->handle, 0, size, PROT_READ);
412 igt_assert(s != NULL);
413 d = gem_mmap__cpu(fd, dst->handle, 0, size, PROT_WRITE);
414 igt_assert(d != NULL);
415
416 memcpy(d, s, size);
417
418 munmap(d, size);
419 munmap(s, size);
420}
421
422static void gtt_copy_bo(drm_intel_bo *dst, drm_intel_bo *src)
423{
424 const int size = width * height * sizeof(uint32_t);
425 void *d, *s;
426
427 gem_set_domain(fd, src->handle, I915_GEM_DOMAIN_GTT, 0);
428 gem_set_domain(fd, dst->handle, I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
429
430 s = gem_mmap__gtt(fd, src->handle, size, PROT_READ);
431 igt_assert(s != NULL);
432 d = gem_mmap__gtt(fd, dst->handle, size, PROT_WRITE);
433 igt_assert(d != NULL);
434
435 memcpy(d, s, size);
436
437 munmap(d, size);
438 munmap(s, size);
439}
440
441static void wc_copy_bo(drm_intel_bo *dst, drm_intel_bo *src)
442{
443 const int size = width * height * sizeof(uint32_t);
444 void *d, *s;
445
446 gem_set_domain(fd, src->handle, I915_GEM_DOMAIN_GTT, 0);
447 gem_set_domain(fd, dst->handle, I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
448
449 s = gem_mmap__wc(fd, src->handle, 0, size, PROT_READ);
450 igt_assert(s != NULL);
451 d = gem_mmap__wc(fd, dst->handle, 0, size, PROT_WRITE);
452 igt_assert(d != NULL);
453
454 memcpy(d, s, size);
455
456 munmap(d, size);
457 munmap(s, size);
458}
459
Chris Wilson16bafdf2014-09-04 09:26:24 +0100460static struct igt_hang_ring no_hang(void)
461{
462 return (struct igt_hang_ring){0, 0};
463}
464
465static struct igt_hang_ring bcs_hang(void)
466{
467 return igt_hang_ring(fd, gen, I915_EXEC_BLT);
468}
469
470static struct igt_hang_ring rcs_hang(void)
471{
472 return igt_hang_ring(fd, gen, I915_EXEC_RENDER);
473}
474
475static void hang_require(void)
476{
Daniel Vetterc66b2422015-02-06 10:49:20 +0100477 igt_require_hang_ring(fd, -1);
Chris Wilson16bafdf2014-09-04 09:26:24 +0100478}
479
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530480static void do_overwrite_source(const struct access_mode *mode,
Daniel Vetter5a598c92013-08-14 15:08:05 +0200481 drm_intel_bo **src, drm_intel_bo **dst,
Chris Wilson59c55622014-08-29 13:11:37 +0100482 drm_intel_bo *dummy,
Chris Wilson16bafdf2014-09-04 09:26:24 +0100483 do_copy do_copy_func,
484 do_hang do_hang_func)
Daniel Vetter5a598c92013-08-14 15:08:05 +0200485{
Chris Wilson16bafdf2014-09-04 09:26:24 +0100486 struct igt_hang_ring hang;
Daniel Vetter5a598c92013-08-14 15:08:05 +0200487 int i;
488
489 gem_quiescent_gpu(fd);
490 for (i = 0; i < num_buffers; i++) {
491 mode->set_bo(src[i], i, width, height);
Chris Wilsonc12f2922014-08-31 16:14:40 +0100492 mode->set_bo(dst[i], ~i, width, height);
Daniel Vetter5a598c92013-08-14 15:08:05 +0200493 }
494 for (i = 0; i < num_buffers; i++)
Chris Wilson59c55622014-08-29 13:11:37 +0100495 do_copy_func(dst[i], src[i]);
Chris Wilson16bafdf2014-09-04 09:26:24 +0100496 hang = do_hang_func();
Daniel Vetter5a598c92013-08-14 15:08:05 +0200497 for (i = num_buffers; i--; )
498 mode->set_bo(src[i], 0xdeadbeef, width, height);
499 for (i = 0; i < num_buffers; i++)
Chris Wilsonc12f2922014-08-31 16:14:40 +0100500 mode->cmp_bo(dst[i], i, width, height, dummy);
Chris Wilson16bafdf2014-09-04 09:26:24 +0100501 igt_post_hang_ring(fd, hang);
502}
503
504static void do_overwrite_source__rev(const struct access_mode *mode,
505 drm_intel_bo **src, drm_intel_bo **dst,
506 drm_intel_bo *dummy,
507 do_copy do_copy_func,
508 do_hang do_hang_func)
509{
510 struct igt_hang_ring hang;
511 int i;
512
513 gem_quiescent_gpu(fd);
514 for (i = 0; i < num_buffers; i++) {
515 mode->set_bo(src[i], i, width, height);
516 mode->set_bo(dst[i], ~i, width, height);
517 }
518 for (i = 0; i < num_buffers; i++)
519 do_copy_func(dst[i], src[i]);
520 hang = do_hang_func();
521 for (i = 0; i < num_buffers; i++)
522 mode->set_bo(src[i], 0xdeadbeef, width, height);
523 for (i = num_buffers; i--; )
524 mode->cmp_bo(dst[i], i, width, height, dummy);
525 igt_post_hang_ring(fd, hang);
526}
527
528static void do_overwrite_source__one(const struct access_mode *mode,
529 drm_intel_bo **src, drm_intel_bo **dst,
530 drm_intel_bo *dummy,
531 do_copy do_copy_func,
532 do_hang do_hang_func)
533{
534 struct igt_hang_ring hang;
535
536 gem_quiescent_gpu(fd);
537 mode->set_bo(src[0], 0, width, height);
538 mode->set_bo(dst[0], ~0, width, height);
539 do_copy_func(dst[0], src[0]);
540 hang = do_hang_func();
541 mode->set_bo(src[0], 0xdeadbeef, width, height);
542 mode->cmp_bo(dst[0], 0, width, height, dummy);
543 igt_post_hang_ring(fd, hang);
Daniel Vetter5a598c92013-08-14 15:08:05 +0200544}
545
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530546static void do_early_read(const struct access_mode *mode,
Daniel Vetter5a598c92013-08-14 15:08:05 +0200547 drm_intel_bo **src, drm_intel_bo **dst,
Chris Wilson59c55622014-08-29 13:11:37 +0100548 drm_intel_bo *dummy,
Chris Wilson16bafdf2014-09-04 09:26:24 +0100549 do_copy do_copy_func,
550 do_hang do_hang_func)
Daniel Vetter5a598c92013-08-14 15:08:05 +0200551{
Chris Wilson16bafdf2014-09-04 09:26:24 +0100552 struct igt_hang_ring hang;
Daniel Vetter5a598c92013-08-14 15:08:05 +0200553 int i;
554
555 gem_quiescent_gpu(fd);
556 for (i = num_buffers; i--; )
557 mode->set_bo(src[i], 0xdeadbeef, width, height);
558 for (i = 0; i < num_buffers; i++)
Chris Wilson59c55622014-08-29 13:11:37 +0100559 do_copy_func(dst[i], src[i]);
Chris Wilson16bafdf2014-09-04 09:26:24 +0100560 hang = do_hang_func();
Daniel Vetter5a598c92013-08-14 15:08:05 +0200561 for (i = num_buffers; i--; )
Chris Wilsonc12f2922014-08-31 16:14:40 +0100562 mode->cmp_bo(dst[i], 0xdeadbeef, width, height, dummy);
Chris Wilson16bafdf2014-09-04 09:26:24 +0100563 igt_post_hang_ring(fd, hang);
Daniel Vetter5a598c92013-08-14 15:08:05 +0200564}
565
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530566static void do_gpu_read_after_write(const struct access_mode *mode,
Daniel Vetter5a598c92013-08-14 15:08:05 +0200567 drm_intel_bo **src, drm_intel_bo **dst,
Chris Wilson59c55622014-08-29 13:11:37 +0100568 drm_intel_bo *dummy,
Chris Wilson16bafdf2014-09-04 09:26:24 +0100569 do_copy do_copy_func,
570 do_hang do_hang_func)
Daniel Vetter5a598c92013-08-14 15:08:05 +0200571{
Chris Wilson16bafdf2014-09-04 09:26:24 +0100572 struct igt_hang_ring hang;
Daniel Vetter5a598c92013-08-14 15:08:05 +0200573 int i;
574
575 gem_quiescent_gpu(fd);
576 for (i = num_buffers; i--; )
577 mode->set_bo(src[i], 0xabcdabcd, width, height);
578 for (i = 0; i < num_buffers; i++)
Chris Wilson59c55622014-08-29 13:11:37 +0100579 do_copy_func(dst[i], src[i]);
Daniel Vetter5a598c92013-08-14 15:08:05 +0200580 for (i = num_buffers; i--; )
Chris Wilson59c55622014-08-29 13:11:37 +0100581 do_copy_func(dummy, dst[i]);
Chris Wilson16bafdf2014-09-04 09:26:24 +0100582 hang = do_hang_func();
Daniel Vetter5a598c92013-08-14 15:08:05 +0200583 for (i = num_buffers; i--; )
Chris Wilsonc12f2922014-08-31 16:14:40 +0100584 mode->cmp_bo(dst[i], 0xabcdabcd, width, height, dummy);
Chris Wilson16bafdf2014-09-04 09:26:24 +0100585 igt_post_hang_ring(fd, hang);
Daniel Vetter5a598c92013-08-14 15:08:05 +0200586}
587
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530588typedef void (*do_test)(const struct access_mode *mode,
Daniel Vetterec283d62013-08-14 15:18:37 +0200589 drm_intel_bo **src, drm_intel_bo **dst,
Chris Wilson59c55622014-08-29 13:11:37 +0100590 drm_intel_bo *dummy,
Chris Wilson16bafdf2014-09-04 09:26:24 +0100591 do_copy do_copy_func,
592 do_hang do_hang_func);
Daniel Vetterec283d62013-08-14 15:18:37 +0200593
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530594typedef void (*run_wrap)(const struct access_mode *mode,
Daniel Vetterec283d62013-08-14 15:18:37 +0200595 drm_intel_bo **src, drm_intel_bo **dst,
596 drm_intel_bo *dummy,
Chris Wilson59c55622014-08-29 13:11:37 +0100597 do_test do_test_func,
Chris Wilson16bafdf2014-09-04 09:26:24 +0100598 do_copy do_copy_func,
599 do_hang do_hang_func);
Daniel Vetterec283d62013-08-14 15:18:37 +0200600
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530601static void run_single(const struct access_mode *mode,
Daniel Vetterec283d62013-08-14 15:18:37 +0200602 drm_intel_bo **src, drm_intel_bo **dst,
603 drm_intel_bo *dummy,
Chris Wilson59c55622014-08-29 13:11:37 +0100604 do_test do_test_func,
Chris Wilson16bafdf2014-09-04 09:26:24 +0100605 do_copy do_copy_func,
606 do_hang do_hang_func)
Daniel Vetterec283d62013-08-14 15:18:37 +0200607{
Chris Wilson16bafdf2014-09-04 09:26:24 +0100608 do_test_func(mode, src, dst, dummy, do_copy_func, do_hang_func);
Daniel Vetterec283d62013-08-14 15:18:37 +0200609}
610
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530611static void run_interruptible(const struct access_mode *mode,
Chris Wilson1ca607b2013-08-16 09:44:13 +0100612 drm_intel_bo **src, drm_intel_bo **dst,
613 drm_intel_bo *dummy,
Chris Wilson59c55622014-08-29 13:11:37 +0100614 do_test do_test_func,
Chris Wilson16bafdf2014-09-04 09:26:24 +0100615 do_copy do_copy_func,
616 do_hang do_hang_func)
Daniel Vetterec283d62013-08-14 15:18:37 +0200617{
618 int loop;
619
620 for (loop = 0; loop < 10; loop++)
Chris Wilson16bafdf2014-09-04 09:26:24 +0100621 do_test_func(mode, src, dst, dummy, do_copy_func, do_hang_func);
Daniel Vetterec283d62013-08-14 15:18:37 +0200622}
623
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530624static void run_forked(const struct access_mode *mode,
Daniel Vetterec283d62013-08-14 15:18:37 +0200625 drm_intel_bo **src, drm_intel_bo **dst,
626 drm_intel_bo *dummy,
Chris Wilson59c55622014-08-29 13:11:37 +0100627 do_test do_test_func,
Chris Wilson16bafdf2014-09-04 09:26:24 +0100628 do_copy do_copy_func,
629 do_hang do_hang_func)
Daniel Vetterec283d62013-08-14 15:18:37 +0200630{
Chris Wilson1ca607b2013-08-16 09:44:13 +0100631 const int old_num_buffers = num_buffers;
Daniel Vetterec283d62013-08-14 15:18:37 +0200632
Daniel Vettercd1f2202013-08-29 10:06:51 +0200633 num_buffers /= 16;
Chris Wilson1ca607b2013-08-16 09:44:13 +0100634 num_buffers += 2;
635
Daniel Vettercd1f2202013-08-29 10:06:51 +0200636 igt_fork(child, 16) {
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530637 drm_intel_bufmgr *bufmgr;
638
Daniel Vettercd1f2202013-08-29 10:06:51 +0200639 /* recreate process local variables */
Chris Wilson16bafdf2014-09-04 09:26:24 +0100640 fd = drm_open_any();
Daniel Vettercd1f2202013-08-29 10:06:51 +0200641 bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
642 drm_intel_bufmgr_gem_enable_reuse(bufmgr);
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530643
Chris Wilson86055df2014-08-29 17:36:29 +0100644 batch = intel_batchbuffer_alloc(bufmgr, devid);
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530645
Daniel Vettercd1f2202013-08-29 10:06:51 +0200646 for (int i = 0; i < num_buffers; i++) {
Chris Wilson86055df2014-08-29 17:36:29 +0100647 src[i] = mode->create_bo(bufmgr, width, height);
648 dst[i] = mode->create_bo(bufmgr, width, height);
Daniel Vetterec283d62013-08-14 15:18:37 +0200649 }
Chris Wilson86055df2014-08-29 17:36:29 +0100650 dummy = mode->create_bo(bufmgr, width, height);
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530651
Daniel Vettercd1f2202013-08-29 10:06:51 +0200652 for (int loop = 0; loop < 10; loop++)
Chris Wilson16bafdf2014-09-04 09:26:24 +0100653 do_test_func(mode, src, dst, dummy,
654 do_copy_func, do_hang_func);
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530655
Daniel Vettercd1f2202013-08-29 10:06:51 +0200656 for (int i = 0; i < num_buffers; i++) {
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530657 mode->release_bo(src[i]);
658 mode->release_bo(dst[i]);
Daniel Vettercd1f2202013-08-29 10:06:51 +0200659 }
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530660 mode->release_bo(dummy);
Daniel Vetterec283d62013-08-14 15:18:37 +0200661 }
Daniel Vettercd1f2202013-08-29 10:06:51 +0200662
663 igt_waitchildren();
Chris Wilson1ca607b2013-08-16 09:44:13 +0100664
Chris Wilson1ca607b2013-08-16 09:44:13 +0100665 num_buffers = old_num_buffers;
Daniel Vetterec283d62013-08-14 15:18:37 +0200666}
Daniel Vetter5a598c92013-08-14 15:08:05 +0200667
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530668static void bit17_require(void)
669{
670 struct drm_i915_gem_get_tiling2 {
671 uint32_t handle;
672 uint32_t tiling_mode;
673 uint32_t swizzle_mode;
674 uint32_t phys_swizzle_mode;
675 } arg;
676#define DRM_IOCTL_I915_GEM_GET_TILING2 DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_GEM_GET_TILING, struct drm_i915_gem_get_tiling2)
677
678 memset(&arg, 0, sizeof(arg));
679 arg.handle = gem_create(fd, 4096);
680 gem_set_tiling(fd, arg.handle, I915_TILING_X, 512);
681
682 do_or_die(drmIoctl(fd, DRM_IOCTL_I915_GEM_GET_TILING2, &arg));
683 gem_close(fd, arg.handle);
684 igt_require(arg.phys_swizzle_mode == arg.swizzle_mode);
685}
686
687static void cpu_require(void)
688{
689 bit17_require();
690}
691
692static void gtt_require(void)
693{
694}
695
696static void wc_require(void)
697{
698 bit17_require();
Daniel Vettera3e34ce2015-02-06 11:05:28 +0100699 gem_require_mmap_wc(fd);
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530700}
701
Chris Wilson08188752014-09-03 13:38:30 +0100702static void bcs_require(void)
703{
704}
705
706static void rcs_require(void)
707{
708 igt_require(rendercopy);
709}
710
Chris Wilson16bafdf2014-09-04 09:26:24 +0100711static void no_require(void)
712{
713}
714
Daniel Vetter5a598c92013-08-14 15:08:05 +0200715static void
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530716run_basic_modes(const struct access_mode *mode,
Chris Wilson16bafdf2014-09-04 09:26:24 +0100717 const char *suffix,
Daniel Vetterec283d62013-08-14 15:18:37 +0200718 run_wrap run_wrap_func)
Daniel Vetter5a598c92013-08-14 15:08:05 +0200719{
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530720 const struct {
Chris Wilson59c55622014-08-29 13:11:37 +0100721 const char *prefix;
722 do_copy copy;
Chris Wilson08188752014-09-03 13:38:30 +0100723 void (*require)(void);
Chris Wilson59c55622014-08-29 13:11:37 +0100724 } pipelines[] = {
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530725 { "cpu", cpu_copy_bo, cpu_require },
726 { "gtt", gtt_copy_bo, gtt_require },
727 { "wc", wc_copy_bo, wc_require },
Daniel Vetter3e9b4e32015-02-06 23:10:26 +0100728 { "blt", blt_copy_bo, bcs_require },
729 { "render", render_copy_bo, rcs_require },
Chris Wilson59c55622014-08-29 13:11:37 +0100730 { NULL, NULL }
731 }, *p;
Chris Wilson16bafdf2014-09-04 09:26:24 +0100732 const struct {
733 const char *suffix;
734 do_hang hang;
735 void (*require)(void);
736 } hangs[] = {
737 { "", no_hang, no_require },
Daniel Vetterfbcc7ba2015-01-22 09:43:10 +0100738 { "-hang-blt", bcs_hang, hang_require },
739 { "-hang-render", rcs_hang, hang_require },
Chris Wilson16bafdf2014-09-04 09:26:24 +0100740 { NULL, NULL },
741 }, *h;
742 drm_intel_bo *src[MAX_NUM_BUFFERS], *dst[MAX_NUM_BUFFERS], *dummy = NULL;
743 drm_intel_bufmgr *bufmgr;
Daniel Vetter5a598c92013-08-14 15:08:05 +0200744
Daniel Vetter5a598c92013-08-14 15:08:05 +0200745
Chris Wilson16bafdf2014-09-04 09:26:24 +0100746 for (h = hangs; h->suffix; h++) {
747 for (p = pipelines; p->prefix; p++) {
748 igt_fixture {
749 bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
750 drm_intel_bufmgr_gem_enable_reuse(bufmgr);
751 batch = intel_batchbuffer_alloc(bufmgr, devid);
Chris Wilson59c55622014-08-29 13:11:37 +0100752
Chris Wilson16bafdf2014-09-04 09:26:24 +0100753 for (int i = 0; i < num_buffers; i++) {
754 src[i] = mode->create_bo(bufmgr, width, height);
755 dst[i] = mode->create_bo(bufmgr, width, height);
756 }
757 dummy = mode->create_bo(bufmgr, width, height);
Daniel Vetter60115082015-01-22 10:01:28 +0100758 }
Chris Wilson16bafdf2014-09-04 09:26:24 +0100759
760 /* try to overwrite the source values */
761 igt_subtest_f("%s-%s-overwrite-source-one%s%s", mode->name, p->prefix, suffix, h->suffix) {
762 h->require();
763 p->require();
764 run_wrap_func(mode, src, dst, dummy,
765 do_overwrite_source__one,
766 p->copy, h->hang);
767 }
768
769 igt_subtest_f("%s-%s-overwrite-source%s%s", mode->name, p->prefix, suffix, h->suffix) {
770 h->require();
771 p->require();
772 run_wrap_func(mode, src, dst, dummy,
773 do_overwrite_source,
774 p->copy, h->hang);
775 }
776 igt_subtest_f("%s-%s-overwrite-source-rev%s%s", mode->name, p->prefix, suffix, h->suffix) {
777 h->require();
778 p->require();
779 run_wrap_func(mode, src, dst, dummy,
780 do_overwrite_source__rev,
781 p->copy, h->hang);
782 }
783
784 /* try to read the results before the copy completes */
785 igt_subtest_f("%s-%s-early-read%s%s", mode->name, p->prefix, suffix, h->suffix) {
786 h->require();
787 p->require();
788 run_wrap_func(mode, src, dst, dummy,
789 do_early_read,
790 p->copy, h->hang);
791 }
792
793 /* and finally try to trick the kernel into loosing the pending write */
794 igt_subtest_f("%s-%s-gpu-read-after-write%s%s", mode->name, p->prefix, suffix, h->suffix) {
795 h->require();
796 p->require();
797 run_wrap_func(mode, src, dst, dummy,
798 do_gpu_read_after_write,
799 p->copy, h->hang);
800 }
801
802 igt_fixture {
803 for (int i = 0; i < num_buffers; i++) {
804 mode->release_bo(src[i]);
805 mode->release_bo(dst[i]);
806 }
807 mode->release_bo(dummy);
808 intel_batchbuffer_free(batch);
809 drm_intel_bufmgr_destroy(bufmgr);
810 }
Chris Wilson08188752014-09-03 13:38:30 +0100811 }
Chris Wilson59c55622014-08-29 13:11:37 +0100812 }
Daniel Vetter5a598c92013-08-14 15:08:05 +0200813}
Daniel Vetter43779e32013-08-14 14:50:50 +0200814
815static void
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530816run_modes(const struct access_mode *mode)
Daniel Vetter43779e32013-08-14 14:50:50 +0200817{
Chris Wilson16bafdf2014-09-04 09:26:24 +0100818 run_basic_modes(mode, "", run_single);
Chris Wilson6c428a62014-08-29 13:11:37 +0100819
820 igt_fork_signal_helper();
Chris Wilson16bafdf2014-09-04 09:26:24 +0100821 run_basic_modes(mode, "-interruptible", run_interruptible);
Chris Wilson6c428a62014-08-29 13:11:37 +0100822 igt_stop_signal_helper();
Daniel Vetter3dba47e2013-08-06 22:27:37 +0200823
Chris Wilson6c428a62014-08-29 13:11:37 +0100824 igt_fork_signal_helper();
Chris Wilson16bafdf2014-09-04 09:26:24 +0100825 run_basic_modes(mode, "-forked", run_forked);
Chris Wilson6c428a62014-08-29 13:11:37 +0100826 igt_stop_signal_helper();
Daniel Vetter43779e32013-08-14 14:50:50 +0200827}
828
Daniel Vetter071e9ca2013-10-31 16:23:26 +0100829igt_main
Daniel Vetter43779e32013-08-14 14:50:50 +0200830{
831 int max, i;
832
Daniel Vetter43779e32013-08-14 14:50:50 +0200833 igt_skip_on_simulation();
834
Daniel Vetter2dbd9982013-08-14 15:48:54 +0200835 igt_fixture {
836 fd = drm_open_any();
Chris Wilson6c428a62014-08-29 13:11:37 +0100837 devid = intel_get_drm_devid(fd);
838 gen = intel_gen(devid);
Chris Wilson59c55622014-08-29 13:11:37 +0100839 rendercopy = igt_get_render_copyfunc(devid);
Daniel Vetter43779e32013-08-14 14:50:50 +0200840
Daniel Vetter2dbd9982013-08-14 15:48:54 +0200841 max = gem_aperture_size (fd) / (1024 * 1024) / 2;
842 if (num_buffers > max)
843 num_buffers = max;
Daniel Vetteraee0dcb2013-12-03 16:32:52 +0100844
845 max = intel_get_total_ram_mb() * 3 / 4;
846 if (num_buffers > max)
847 num_buffers = max;
Chris Wilson0b4c33f2014-01-26 14:36:32 +0000848 num_buffers /= 2;
Daniel Vettere624fa82014-05-14 00:36:04 +0200849 igt_info("using 2x%d buffers, each 1MiB\n", num_buffers);
Daniel Vetter2dbd9982013-08-14 15:48:54 +0200850 }
Daniel Vetter43779e32013-08-14 14:50:50 +0200851
852 for (i = 0; i < ARRAY_SIZE(access_modes); i++)
853 run_modes(&access_modes[i]);
Daniel Vetter3dba47e2013-08-06 22:27:37 +0200854}