blob: 76aecd2a668baf70e31a0d0cb514eef382615640 [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
Chris Wilson77633492015-03-26 08:11:43 +000030/** @file gem_concurrent.c
Daniel Vetter3dba47e2013-08-06 22:27:37 +020031 *
Chris Wilson77633492015-03-26 08:11:43 +000032 * This is a test of pread/pwrite/mmap behavior when writing to active
Daniel Vetter3dba47e2013-08-06 22:27:37 +020033 * buffers.
34 *
35 * Based on gem_gtt_concurrent_blt.
36 */
37
Thomas Wood804e11f2015-08-17 17:57:43 +010038#include "igt.h"
Chris Wilsone7ed4ef2016-07-18 11:38:09 +010039#include "igt_vgem.h"
Daniel Vetter3dba47e2013-08-06 22:27:37 +020040#include <stdlib.h>
41#include <stdio.h>
42#include <string.h>
Daniel Vetter3dba47e2013-08-06 22:27:37 +020043#include <fcntl.h>
44#include <inttypes.h>
45#include <errno.h>
Chris Wilson3d8af562016-03-20 10:49:54 +000046#include <sys/resource.h>
Daniel Vetter3dba47e2013-08-06 22:27:37 +020047#include <sys/stat.h>
48#include <sys/time.h>
Chris Wilson99431a42013-08-14 11:03:34 +010049#include <sys/wait.h>
Daniel Vetterf5daeec2014-03-23 13:35:09 +010050
51#include <drm.h>
52
Daniel Vetter3dba47e2013-08-06 22:27:37 +020053#include "intel_bufmgr.h"
Daniel Vetter3dba47e2013-08-06 22:27:37 +020054
Chris Wilson77633492015-03-26 08:11:43 +000055IGT_TEST_DESCRIPTION("Test of pread/pwrite/mmap behavior when writing to active"
Thomas Woodb2ac2642014-11-28 11:02:44 +000056 " buffers.");
57
Chris Wilson6c428a62014-08-29 13:11:37 +010058int fd, devid, gen;
Chris Wilsone7ed4ef2016-07-18 11:38:09 +010059int vgem_drv = -1;
Chris Wilson77633492015-03-26 08:11:43 +000060int all;
Chris Wilson1c61c0f2016-01-08 10:51:09 +000061int pass;
Chris Wilson6c428a62014-08-29 13:11:37 +010062
Chris Wilson4eba8e22016-03-18 10:44:31 +000063struct create {
64 const char *name;
Chris Wilsone85613b2016-03-19 14:01:38 +000065 void (*require)(const struct create *, unsigned);
Chris Wilson4eba8e22016-03-18 10:44:31 +000066 drm_intel_bo *(*create)(drm_intel_bufmgr *, uint64_t size);
67};
68
Chris Wilson5d669bf2016-03-18 14:44:53 +000069struct size {
70 const char *name;
71 int width, height;
72};
73
Chris Wilson37f4da02016-01-27 13:02:35 +000074struct buffers {
Chris Wilson4eba8e22016-03-18 10:44:31 +000075 const char *name;
76 const struct create *create;
Chris Wilson37f4da02016-01-27 13:02:35 +000077 const struct access_mode *mode;
Chris Wilson5d669bf2016-03-18 14:44:53 +000078 const struct size *size;
Chris Wilson37f4da02016-01-27 13:02:35 +000079 drm_intel_bufmgr *bufmgr;
Chris Wilson094e0cb2016-03-01 13:22:03 +000080 struct intel_batchbuffer *batch;
Chris Wilson37f4da02016-01-27 13:02:35 +000081 drm_intel_bo **src, **dst;
82 drm_intel_bo *snoop, *spare;
83 uint32_t *tmp;
Chris Wilsond199ad82016-07-27 23:19:41 +010084 int width, height, npixels, page_size;
Chris Wilson094e0cb2016-03-01 13:22:03 +000085 int count, num_buffers;
Chris Wilson37f4da02016-01-27 13:02:35 +000086};
87
Chris Wilson571b8762016-01-08 11:51:56 +000088#define MIN_BUFFERS 3
89
Chris Wilson37f4da02016-01-27 13:02:35 +000090static void blt_copy_bo(struct buffers *b, drm_intel_bo *dst, drm_intel_bo *src);
Chris Wilson6f759902016-01-27 11:17:03 +000091
Daniel Vetter3dba47e2013-08-06 22:27:37 +020092static void
Chris Wilsonf2a045f2015-01-02 16:33:33 +053093nop_release_bo(drm_intel_bo *bo)
94{
95 drm_intel_bo_unreference(bo);
96}
97
98static void
Chris Wilson37f4da02016-01-27 13:02:35 +000099prw_set_bo(struct buffers *b, drm_intel_bo *bo, uint32_t val)
Daniel Vetter3dba47e2013-08-06 22:27:37 +0200100{
Chris Wilson5d669bf2016-03-18 14:44:53 +0000101 for (int i = 0; i < b->npixels; i++)
Chris Wilson37f4da02016-01-27 13:02:35 +0000102 b->tmp[i] = val;
Chris Wilson5d669bf2016-03-18 14:44:53 +0000103 drm_intel_bo_subdata(bo, 0, 4*b->npixels, b->tmp);
Daniel Vetter3dba47e2013-08-06 22:27:37 +0200104}
105
106static void
Chris Wilson37f4da02016-01-27 13:02:35 +0000107prw_cmp_bo(struct buffers *b, drm_intel_bo *bo, uint32_t val)
Daniel Vetter3dba47e2013-08-06 22:27:37 +0200108{
Chris Wilsonc12f2922014-08-31 16:14:40 +0100109 uint32_t *vaddr;
Daniel Vetter3dba47e2013-08-06 22:27:37 +0200110
Chris Wilson37f4da02016-01-27 13:02:35 +0000111 vaddr = b->tmp;
Chris Wilson5d669bf2016-03-18 14:44:53 +0000112 do_or_die(drm_intel_bo_get_subdata(bo, 0, 4*b->npixels, vaddr));
113 for (int i = 0; i < b->npixels; i++)
Chris Wilsonc12f2922014-08-31 16:14:40 +0100114 igt_assert_eq_u32(vaddr[i], val);
Daniel Vetter3dba47e2013-08-06 22:27:37 +0200115}
116
Chris Wilson1c61c0f2016-01-08 10:51:09 +0000117#define pixel(y, width) ((y)*(width) + (((y) + pass)%(width)))
118
119static void
Chris Wilson37f4da02016-01-27 13:02:35 +0000120partial_set_bo(struct buffers *b, drm_intel_bo *bo, uint32_t val)
Chris Wilson1c61c0f2016-01-08 10:51:09 +0000121{
Chris Wilson37f4da02016-01-27 13:02:35 +0000122 for (int y = 0; y < b->height; y++)
123 do_or_die(drm_intel_bo_subdata(bo, 4*pixel(y, b->width), 4, &val));
Chris Wilson1c61c0f2016-01-08 10:51:09 +0000124}
125
126static void
Chris Wilson37f4da02016-01-27 13:02:35 +0000127partial_cmp_bo(struct buffers *b, drm_intel_bo *bo, uint32_t val)
Chris Wilson1c61c0f2016-01-08 10:51:09 +0000128{
Chris Wilson37f4da02016-01-27 13:02:35 +0000129 for (int y = 0; y < b->height; y++) {
Chris Wilson1c61c0f2016-01-08 10:51:09 +0000130 uint32_t buf;
Chris Wilson37f4da02016-01-27 13:02:35 +0000131 do_or_die(drm_intel_bo_get_subdata(bo, 4*pixel(y, b->width), 4, &buf));
Chris Wilson1c61c0f2016-01-08 10:51:09 +0000132 igt_assert_eq_u32(buf, val);
133 }
134}
135
Daniel Vetter3dba47e2013-08-06 22:27:37 +0200136static drm_intel_bo *
Chris Wilson1d6e5d32016-01-03 13:44:17 +0000137create_normal_bo(drm_intel_bufmgr *bufmgr, uint64_t size)
Daniel Vetter3dba47e2013-08-06 22:27:37 +0200138{
139 drm_intel_bo *bo;
140
Chris Wilson1d6e5d32016-01-03 13:44:17 +0000141 bo = drm_intel_bo_alloc(bufmgr, "bo", size, 0);
Daniel Vetter83440952013-08-13 12:35:58 +0200142 igt_assert(bo);
Daniel Vetter3dba47e2013-08-06 22:27:37 +0200143
144 return bo;
145}
146
Chris Wilsone85613b2016-03-19 14:01:38 +0000147static void can_create_normal(const struct create *create, unsigned count)
Chris Wilson1d6e5d32016-01-03 13:44:17 +0000148{
Chris Wilson1d6e5d32016-01-03 13:44:17 +0000149}
150
Chris Wilson4eba8e22016-03-18 10:44:31 +0000151#if HAVE_CREATE_PRIVATE
Chris Wilson1d6e5d32016-01-03 13:44:17 +0000152static drm_intel_bo *
153create_private_bo(drm_intel_bufmgr *bufmgr, uint64_t size)
154{
155 drm_intel_bo *bo;
156 uint32_t handle;
157
158 /* XXX gem_create_with_flags(fd, size, I915_CREATE_PRIVATE); */
159
160 handle = gem_create(fd, size);
161 bo = gem_handle_to_libdrm_bo(bufmgr, fd, "stolen", handle);
162 gem_close(fd, handle);
163
164 return bo;
165}
166
Chris Wilsone85613b2016-03-19 14:01:38 +0000167static void can_create_private(const struct create *create, unsigned count)
Chris Wilson1d6e5d32016-01-03 13:44:17 +0000168{
Chris Wilson4eba8e22016-03-18 10:44:31 +0000169 igt_require(0);
Chris Wilson1d6e5d32016-01-03 13:44:17 +0000170}
Chris Wilson4eba8e22016-03-18 10:44:31 +0000171#endif
Chris Wilson1d6e5d32016-01-03 13:44:17 +0000172
Chris Wilson4eba8e22016-03-18 10:44:31 +0000173#if HAVE_CREATE_STOLEN
Chris Wilson1d6e5d32016-01-03 13:44:17 +0000174static drm_intel_bo *
175create_stolen_bo(drm_intel_bufmgr *bufmgr, uint64_t size)
176{
177 drm_intel_bo *bo;
178 uint32_t handle;
179
180 /* XXX gem_create_with_flags(fd, size, I915_CREATE_STOLEN); */
181
182 handle = gem_create(fd, size);
183 bo = gem_handle_to_libdrm_bo(bufmgr, fd, "stolen", handle);
184 gem_close(fd, handle);
185
186 return bo;
187}
188
Chris Wilsone85613b2016-03-19 14:01:38 +0000189static void can_create_stolen(const struct create *create, unsigned count)
Chris Wilson1d6e5d32016-01-03 13:44:17 +0000190{
191 /* XXX check num_buffers against available stolen */
Chris Wilson4eba8e22016-03-18 10:44:31 +0000192 igt_require(0);
193}
194#endif
195
Chris Wilsone85613b2016-03-19 14:01:38 +0000196static void create_cpu_require(const struct create *create, unsigned count)
Chris Wilson4eba8e22016-03-18 10:44:31 +0000197{
198#if HAVE_CREATE_STOLEN
199 igt_require(create->create != create_stolen_bo);
200#endif
Chris Wilson1d6e5d32016-01-03 13:44:17 +0000201}
202
203static drm_intel_bo *
Chris Wilson4eba8e22016-03-18 10:44:31 +0000204unmapped_create_bo(const struct buffers *b)
Chris Wilson1d6e5d32016-01-03 13:44:17 +0000205{
Chris Wilson5d669bf2016-03-18 14:44:53 +0000206 return b->create->create(b->bufmgr, 4*b->npixels);
Chris Wilson4eba8e22016-03-18 10:44:31 +0000207}
208
Chris Wilsone85613b2016-03-19 14:01:38 +0000209static void create_snoop_require(const struct create *create, unsigned count)
Chris Wilson4eba8e22016-03-18 10:44:31 +0000210{
Chris Wilsone85613b2016-03-19 14:01:38 +0000211 create_cpu_require(create, count);
Chris Wilson4eba8e22016-03-18 10:44:31 +0000212 igt_require(!gem_has_llc(fd));
Chris Wilson1d6e5d32016-01-03 13:44:17 +0000213}
214
215static drm_intel_bo *
Chris Wilson4eba8e22016-03-18 10:44:31 +0000216snoop_create_bo(const struct buffers *b)
Chris Wilson46ec33e2015-10-20 14:40:50 +0100217{
218 drm_intel_bo *bo;
219
Chris Wilson4eba8e22016-03-18 10:44:31 +0000220 bo = unmapped_create_bo(b);
Chris Wilson46ec33e2015-10-20 14:40:50 +0100221 gem_set_caching(fd, bo->handle, I915_CACHING_CACHED);
222 drm_intel_bo_disable_reuse(bo);
223
224 return bo;
225}
226
Chris Wilsone85613b2016-03-19 14:01:38 +0000227static void create_userptr_require(const struct create *create, unsigned count)
Chris Wilson0143d4f2016-01-21 09:53:50 +0000228{
Chris Wilson4eba8e22016-03-18 10:44:31 +0000229 static int has_userptr = -1;
230 if (has_userptr < 0) {
Chris Wilson0143d4f2016-01-21 09:53:50 +0000231 struct drm_i915_gem_userptr arg;
232
Chris Wilson4eba8e22016-03-18 10:44:31 +0000233 has_userptr = 0;
Chris Wilson0143d4f2016-01-21 09:53:50 +0000234
235 memset(&arg, 0, sizeof(arg));
236 arg.user_ptr = -4096ULL;
237 arg.user_size = 8192;
238 errno = 0;
239 drmIoctl(fd, LOCAL_IOCTL_I915_GEM_USERPTR, &arg);
240 if (errno == EFAULT) {
241 igt_assert(posix_memalign((void **)&arg.user_ptr,
242 4096, arg.user_size) == 0);
Chris Wilson4eba8e22016-03-18 10:44:31 +0000243 has_userptr = drmIoctl(fd,
Chris Wilson0143d4f2016-01-21 09:53:50 +0000244 LOCAL_IOCTL_I915_GEM_USERPTR,
245 &arg) == 0;
246 free((void *)(uintptr_t)arg.user_ptr);
247 }
248
249 }
Chris Wilson4eba8e22016-03-18 10:44:31 +0000250 igt_require(has_userptr);
Chris Wilson0143d4f2016-01-21 09:53:50 +0000251}
252
253static drm_intel_bo *
Chris Wilson4eba8e22016-03-18 10:44:31 +0000254userptr_create_bo(const struct buffers *b)
Chris Wilson0143d4f2016-01-21 09:53:50 +0000255{
256 struct local_i915_gem_userptr userptr;
257 drm_intel_bo *bo;
Chris Wilsond4a05bc2016-01-23 09:07:12 +0000258 void *ptr;
Chris Wilson0143d4f2016-01-21 09:53:50 +0000259
260 memset(&userptr, 0, sizeof(userptr));
Chris Wilsond199ad82016-07-27 23:19:41 +0100261 userptr.user_size = b->page_size;
Chris Wilsond4a05bc2016-01-23 09:07:12 +0000262
263 ptr = mmap(NULL, userptr.user_size,
264 PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0);
265 igt_assert(ptr != (void *)-1);
266 userptr.user_ptr = (uintptr_t)ptr;
Chris Wilson0143d4f2016-01-21 09:53:50 +0000267
Chris Wilsonc2248ef2016-03-19 13:10:17 +0000268#if 0
Chris Wilson0143d4f2016-01-21 09:53:50 +0000269 do_or_die(drmIoctl(fd, LOCAL_IOCTL_I915_GEM_USERPTR, &userptr));
Chris Wilson4eba8e22016-03-18 10:44:31 +0000270 bo = gem_handle_to_libdrm_bo(b->bufmgr, fd, "userptr", userptr.handle);
Chris Wilsona64f31b2016-01-27 11:19:26 +0000271 gem_close(fd, userptr.handle);
Chris Wilsonc2248ef2016-03-19 13:10:17 +0000272#else
273 bo = drm_intel_bo_alloc_userptr(b->bufmgr, "name",
274 ptr, I915_TILING_NONE, 0,
275 userptr.user_size, 0);
276 igt_assert(bo);
277#endif
278 bo->virtual = (void *)(uintptr_t)userptr.user_ptr;
Chris Wilson0143d4f2016-01-21 09:53:50 +0000279
280 return bo;
281}
282
283static void
Chris Wilson37f4da02016-01-27 13:02:35 +0000284userptr_set_bo(struct buffers *b, drm_intel_bo *bo, uint32_t val)
Chris Wilson0143d4f2016-01-21 09:53:50 +0000285{
Chris Wilson5d669bf2016-03-18 14:44:53 +0000286 int size = b->npixels;
Chris Wilson0143d4f2016-01-21 09:53:50 +0000287 uint32_t *vaddr = bo->virtual;
288
289 gem_set_domain(fd, bo->handle,
290 I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
291 while (size--)
292 *vaddr++ = val;
293}
294
295static void
Chris Wilson37f4da02016-01-27 13:02:35 +0000296userptr_cmp_bo(struct buffers *b, drm_intel_bo *bo, uint32_t val)
Chris Wilson0143d4f2016-01-21 09:53:50 +0000297{
Chris Wilson5d669bf2016-03-18 14:44:53 +0000298 int size = b->npixels;
Chris Wilson0143d4f2016-01-21 09:53:50 +0000299 uint32_t *vaddr = bo->virtual;
300
301 gem_set_domain(fd, bo->handle,
302 I915_GEM_DOMAIN_CPU, 0);
303 while (size--)
304 igt_assert_eq_u32(*vaddr++, val);
305}
306
307static void
308userptr_release_bo(drm_intel_bo *bo)
309{
Chris Wilson094e0cb2016-03-01 13:22:03 +0000310 igt_assert(bo->virtual);
311
Chris Wilsond4a05bc2016-01-23 09:07:12 +0000312 munmap(bo->virtual, bo->size);
Chris Wilson0143d4f2016-01-21 09:53:50 +0000313 bo->virtual = NULL;
314
315 drm_intel_bo_unreference(bo);
316}
317
Chris Wilsone85613b2016-03-19 14:01:38 +0000318static void create_dmabuf_require(const struct create *create, unsigned count)
Chris Wilsoncf569c22016-02-25 17:58:24 +0000319{
Chris Wilson4eba8e22016-03-18 10:44:31 +0000320 static int has_dmabuf = -1;
321 if (has_dmabuf < 0) {
Chris Wilsoncf569c22016-02-25 17:58:24 +0000322 struct drm_prime_handle args;
323 void *ptr;
324
325 memset(&args, 0, sizeof(args));
326 args.handle = gem_create(fd, 4096);
327 args.flags = DRM_RDWR;
328 args.fd = -1;
329
330 drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
331 gem_close(fd, args.handle);
332
Chris Wilson4eba8e22016-03-18 10:44:31 +0000333 has_dmabuf = 0;
Chris Wilsoncf569c22016-02-25 17:58:24 +0000334 ptr = mmap(NULL, 4096, PROT_READ, MAP_SHARED, args.fd, 0);
335 if (ptr != MAP_FAILED) {
Chris Wilson4eba8e22016-03-18 10:44:31 +0000336 has_dmabuf = 1;
Chris Wilsoncf569c22016-02-25 17:58:24 +0000337 munmap(ptr, 4096);
338 }
339
340 close(args.fd);
341 }
Chris Wilson4eba8e22016-03-18 10:44:31 +0000342 igt_require(has_dmabuf);
Chris Wilsone85613b2016-03-19 14:01:38 +0000343 intel_require_files(2*count);
Chris Wilsoncf569c22016-02-25 17:58:24 +0000344}
345
346struct dmabuf {
347 int fd;
348 void *map;
349};
350
351static drm_intel_bo *
Chris Wilson4eba8e22016-03-18 10:44:31 +0000352dmabuf_create_bo(const struct buffers *b)
Chris Wilsoncf569c22016-02-25 17:58:24 +0000353{
354 struct drm_prime_handle args;
355 drm_intel_bo *bo;
356 struct dmabuf *dmabuf;
357 int size;
358
Chris Wilsond199ad82016-07-27 23:19:41 +0100359 size = b->page_size;
Chris Wilsoncf569c22016-02-25 17:58:24 +0000360
361 memset(&args, 0, sizeof(args));
362 args.handle = gem_create(fd, size);
363 args.flags = DRM_RDWR;
364 args.fd = -1;
365
366 do_ioctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
367 gem_close(fd, args.handle);
368
Chris Wilson4eba8e22016-03-18 10:44:31 +0000369 bo = drm_intel_bo_gem_create_from_prime(b->bufmgr, args.fd, size);
Chris Wilsoncf569c22016-02-25 17:58:24 +0000370 igt_assert(bo);
371
372 dmabuf = malloc(sizeof(*dmabuf));
373 igt_assert(dmabuf);
374
375 dmabuf->fd = args.fd;
376 dmabuf->map = mmap(NULL, size,
377 PROT_READ | PROT_WRITE, MAP_SHARED,
378 dmabuf->fd, 0);
379 igt_assert(dmabuf->map != (void *)-1);
380
381 bo->virtual = dmabuf;
382
383 return bo;
384}
385
386static void
387dmabuf_set_bo(struct buffers *b, drm_intel_bo *bo, uint32_t val)
388{
389 struct dmabuf *dmabuf = bo->virtual;
Chris Wilsonc7f6ecc2016-09-25 09:09:02 +0100390 uint32_t *v = dmabuf->map;
391 int y;
Chris Wilsoncf569c22016-02-25 17:58:24 +0000392
Chris Wilsonaed69b52016-02-25 21:43:01 +0000393 prime_sync_start(dmabuf->fd, true);
Chris Wilsonc7f6ecc2016-09-25 09:09:02 +0100394 for (y = 0; y < b->height; y++)
395 v[pixel(y, b->width)] = val;
Chris Wilsonaed69b52016-02-25 21:43:01 +0000396 prime_sync_end(dmabuf->fd, true);
Chris Wilsoncf569c22016-02-25 17:58:24 +0000397}
398
399static void
400dmabuf_cmp_bo(struct buffers *b, drm_intel_bo *bo, uint32_t val)
401{
402 struct dmabuf *dmabuf = bo->virtual;
Chris Wilsonc7f6ecc2016-09-25 09:09:02 +0100403 uint32_t *v = dmabuf->map;
404 int y;
Chris Wilsoncf569c22016-02-25 17:58:24 +0000405
Chris Wilsonaed69b52016-02-25 21:43:01 +0000406 prime_sync_start(dmabuf->fd, false);
Chris Wilsonc7f6ecc2016-09-25 09:09:02 +0100407 for (y = 0; y < b->height; y++)
408 igt_assert_eq_u32(v[pixel(y, b->width)], val);
Chris Wilsonaed69b52016-02-25 21:43:01 +0000409 prime_sync_end(dmabuf->fd, false);
Chris Wilsoncf569c22016-02-25 17:58:24 +0000410}
411
412static void
413dmabuf_release_bo(drm_intel_bo *bo)
414{
415 struct dmabuf *dmabuf = bo->virtual;
Chris Wilson094e0cb2016-03-01 13:22:03 +0000416 igt_assert(dmabuf);
Chris Wilsoncf569c22016-02-25 17:58:24 +0000417
418 munmap(dmabuf->map, bo->size);
419 close(dmabuf->fd);
420 free(dmabuf);
421
422 bo->virtual = NULL;
423 drm_intel_bo_unreference(bo);
424}
425
Chris Wilsone7ed4ef2016-07-18 11:38:09 +0100426static bool has_prime_export(int _fd)
427{
428 uint64_t value;
429
430 if (drmGetCap(_fd, DRM_CAP_PRIME, &value))
431 return false;
432
433 return value & DRM_PRIME_CAP_EXPORT;
434}
435
436static void create_vgem_require(const struct create *create, unsigned count)
437{
438 igt_require(vgem_drv != -1);
439 igt_require(has_prime_export(vgem_drv));
440 create_dmabuf_require(create, count);
441}
442
443static drm_intel_bo *
444vgem_create_bo(const struct buffers *b)
445{
446 struct drm_prime_handle args;
447 drm_intel_bo *bo;
448 struct vgem_bo vgem;
449 struct dmabuf *dmabuf;
450
451 igt_assert(vgem_drv != -1);
452
453 vgem.width = b->width;
454 vgem.height = b->height;
455 vgem.bpp = 32;
456 vgem_create(vgem_drv, &vgem);
457
458 memset(&args, 0, sizeof(args));
459 args.handle = vgem.handle;
460 args.flags = DRM_RDWR;
461 args.fd = -1;
462
463 do_ioctl(vgem_drv, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
464 gem_close(vgem_drv, args.handle);
465 igt_assert(args.fd != -1);
466
467 bo = drm_intel_bo_gem_create_from_prime(b->bufmgr, args.fd, vgem.size);
468 igt_assert(bo);
469
470 dmabuf = malloc(sizeof(*dmabuf));
471 igt_assert(dmabuf);
472
473 dmabuf->fd = args.fd;
474 dmabuf->map = mmap(NULL, vgem.size,
475 PROT_READ | PROT_WRITE, MAP_SHARED,
476 dmabuf->fd, 0);
477 igt_assert(dmabuf->map != (void *)-1);
478
479 bo->virtual = dmabuf;
480
481 return bo;
482}
483
Daniel Vetter43779e32013-08-14 14:50:50 +0200484static void
Chris Wilson37f4da02016-01-27 13:02:35 +0000485gtt_set_bo(struct buffers *b, drm_intel_bo *bo, uint32_t val)
Daniel Vetter3dba47e2013-08-06 22:27:37 +0200486{
Chris Wilson3e766b82014-09-26 07:55:49 +0100487 uint32_t *vaddr = bo->virtual;
Chris Wilson5d669bf2016-03-18 14:44:53 +0000488 int size = b->npixels;
Daniel Vetter43779e32013-08-14 14:50:50 +0200489
490 drm_intel_gem_bo_start_gtt_access(bo, true);
Daniel Vetter43779e32013-08-14 14:50:50 +0200491 while (size--)
492 *vaddr++ = val;
493}
494
495static void
Chris Wilson37f4da02016-01-27 13:02:35 +0000496gtt_cmp_bo(struct buffers *b, drm_intel_bo *bo, uint32_t val)
Daniel Vetter43779e32013-08-14 14:50:50 +0200497{
Chris Wilson3e766b82014-09-26 07:55:49 +0100498 uint32_t *vaddr = bo->virtual;
Daniel Vetter43779e32013-08-14 14:50:50 +0200499
Chris Wilson3e766b82014-09-26 07:55:49 +0100500 /* GTT access is slow. So we just compare a few points */
Daniel Vetter43779e32013-08-14 14:50:50 +0200501 drm_intel_gem_bo_start_gtt_access(bo, false);
Chris Wilson37f4da02016-01-27 13:02:35 +0000502 for (int y = 0; y < b->height; y++)
503 igt_assert_eq_u32(vaddr[pixel(y, b->width)], val);
Daniel Vetter43779e32013-08-14 14:50:50 +0200504}
505
506static drm_intel_bo *
Chris Wilson86055df2014-08-29 17:36:29 +0100507map_bo(drm_intel_bo *bo)
Daniel Vetter43779e32013-08-14 14:50:50 +0200508{
Daniel Vetter43779e32013-08-14 14:50:50 +0200509 /* gtt map doesn't have a write parameter, so just keep the mapping
510 * around (to avoid the set_domain with the gtt write domain set) and
511 * manually tell the kernel when we start access the gtt. */
512 do_or_die(drm_intel_gem_bo_map_gtt(bo));
513
514 return bo;
515}
516
Chris Wilson86055df2014-08-29 17:36:29 +0100517static drm_intel_bo *
518tile_bo(drm_intel_bo *bo, int width)
519{
520 uint32_t tiling = I915_TILING_X;
521 uint32_t stride = width * 4;
522
523 do_or_die(drm_intel_bo_set_tiling(bo, &tiling, stride));
524
525 return bo;
526}
527
528static drm_intel_bo *
Chris Wilson4eba8e22016-03-18 10:44:31 +0000529gtt_create_bo(const struct buffers *b)
Chris Wilson86055df2014-08-29 17:36:29 +0100530{
Chris Wilson4eba8e22016-03-18 10:44:31 +0000531 return map_bo(unmapped_create_bo(b));
Chris Wilson86055df2014-08-29 17:36:29 +0100532}
533
534static drm_intel_bo *
Chris Wilson4eba8e22016-03-18 10:44:31 +0000535gttX_create_bo(const struct buffers *b)
Chris Wilson86055df2014-08-29 17:36:29 +0100536{
Chris Wilson4eba8e22016-03-18 10:44:31 +0000537 return tile_bo(gtt_create_bo(b), b->width);
Chris Wilson86055df2014-08-29 17:36:29 +0100538}
539
Chris Wilson5d669bf2016-03-18 14:44:53 +0000540static void bit17_require(void)
541{
542 static struct drm_i915_gem_get_tiling2 {
543 uint32_t handle;
544 uint32_t tiling_mode;
545 uint32_t swizzle_mode;
546 uint32_t phys_swizzle_mode;
547 } arg;
548#define DRM_IOCTL_I915_GEM_GET_TILING2 DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_GEM_GET_TILING, struct drm_i915_gem_get_tiling2)
549
550 if (arg.handle == 0) {
551 arg.handle = gem_create(fd, 4096);
552 gem_set_tiling(fd, arg.handle, I915_TILING_X, 512);
553
554 do_ioctl(fd, DRM_IOCTL_I915_GEM_GET_TILING2, &arg);
555 gem_close(fd, arg.handle);
556 }
557 igt_require(arg.phys_swizzle_mode == arg.swizzle_mode);
558}
559
560static void wc_require(void)
561{
562 bit17_require();
563 gem_require_mmap_wc(fd);
564}
565
566static void
Chris Wilsone85613b2016-03-19 14:01:38 +0000567wc_create_require(const struct create *create, unsigned count)
Chris Wilson5d669bf2016-03-18 14:44:53 +0000568{
569 wc_require();
570}
571
Chris Wilson86055df2014-08-29 17:36:29 +0100572static drm_intel_bo *
Chris Wilson4eba8e22016-03-18 10:44:31 +0000573wc_create_bo(const struct buffers *b)
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530574{
575 drm_intel_bo *bo;
576
Chris Wilson4eba8e22016-03-18 10:44:31 +0000577 bo = unmapped_create_bo(b);
Chris Wilson5d669bf2016-03-18 14:44:53 +0000578 bo->virtual = gem_mmap__wc(fd, bo->handle, 0, bo->size, PROT_READ | PROT_WRITE);
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530579 return bo;
580}
581
582static void
583wc_release_bo(drm_intel_bo *bo)
584{
Chris Wilson094e0cb2016-03-01 13:22:03 +0000585 igt_assert(bo->virtual);
586
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530587 munmap(bo->virtual, bo->size);
588 bo->virtual = NULL;
589
590 nop_release_bo(bo);
591}
592
593static drm_intel_bo *
Chris Wilson4eba8e22016-03-18 10:44:31 +0000594gpu_create_bo(const struct buffers *b)
Chris Wilson86055df2014-08-29 17:36:29 +0100595{
Chris Wilson4eba8e22016-03-18 10:44:31 +0000596 return unmapped_create_bo(b);
Chris Wilson86055df2014-08-29 17:36:29 +0100597}
598
Chris Wilson86055df2014-08-29 17:36:29 +0100599static drm_intel_bo *
Chris Wilson4eba8e22016-03-18 10:44:31 +0000600gpuX_create_bo(const struct buffers *b)
Chris Wilson86055df2014-08-29 17:36:29 +0100601{
Chris Wilson4eba8e22016-03-18 10:44:31 +0000602 return tile_bo(gpu_create_bo(b), b->width);
Chris Wilson86055df2014-08-29 17:36:29 +0100603}
604
Daniel Vetter43779e32013-08-14 14:50:50 +0200605static void
Chris Wilson37f4da02016-01-27 13:02:35 +0000606cpu_set_bo(struct buffers *b, drm_intel_bo *bo, uint32_t val)
Daniel Vetter43779e32013-08-14 14:50:50 +0200607{
Chris Wilson5d669bf2016-03-18 14:44:53 +0000608 int size = b->npixels;
Daniel Vetter43779e32013-08-14 14:50:50 +0200609 uint32_t *vaddr;
610
611 do_or_die(drm_intel_bo_map(bo, true));
612 vaddr = bo->virtual;
613 while (size--)
614 *vaddr++ = val;
615 drm_intel_bo_unmap(bo);
616}
617
618static void
Chris Wilson37f4da02016-01-27 13:02:35 +0000619cpu_cmp_bo(struct buffers *b, drm_intel_bo *bo, uint32_t val)
Daniel Vetter43779e32013-08-14 14:50:50 +0200620{
Chris Wilson5d669bf2016-03-18 14:44:53 +0000621 int size = b->npixels;
Daniel Vetter43779e32013-08-14 14:50:50 +0200622 uint32_t *vaddr;
623
624 do_or_die(drm_intel_bo_map(bo, false));
625 vaddr = bo->virtual;
626 while (size--)
Chris Wilson6c428a62014-08-29 13:11:37 +0100627 igt_assert_eq_u32(*vaddr++, val);
Daniel Vetter43779e32013-08-14 14:50:50 +0200628 drm_intel_bo_unmap(bo);
629}
630
Chris Wilson6c428a62014-08-29 13:11:37 +0100631static void
Chris Wilson37f4da02016-01-27 13:02:35 +0000632gpu_set_bo(struct buffers *buffers, drm_intel_bo *bo, uint32_t val)
Chris Wilson6c428a62014-08-29 13:11:37 +0100633{
634 struct drm_i915_gem_relocation_entry reloc[1];
635 struct drm_i915_gem_exec_object2 gem_exec[2];
636 struct drm_i915_gem_execbuffer2 execbuf;
Chris Wilson6c428a62014-08-29 13:11:37 +0100637 uint32_t buf[10], *b;
Chris Wilson86055df2014-08-29 17:36:29 +0100638 uint32_t tiling, swizzle;
639
640 drm_intel_bo_get_tiling(bo, &tiling, &swizzle);
Chris Wilson6c428a62014-08-29 13:11:37 +0100641
642 memset(reloc, 0, sizeof(reloc));
643 memset(gem_exec, 0, sizeof(gem_exec));
644 memset(&execbuf, 0, sizeof(execbuf));
645
646 b = buf;
647 *b++ = XY_COLOR_BLT_CMD_NOLEN |
648 ((gen >= 8) ? 5 : 4) |
649 COLOR_BLT_WRITE_ALPHA | XY_COLOR_BLT_WRITE_RGB;
Chris Wilson86055df2014-08-29 17:36:29 +0100650 if (gen >= 4 && tiling) {
651 b[-1] |= XY_COLOR_BLT_TILED;
Chris Wilson37f4da02016-01-27 13:02:35 +0000652 *b = buffers->width;
Chris Wilson86055df2014-08-29 17:36:29 +0100653 } else
Chris Wilson37f4da02016-01-27 13:02:35 +0000654 *b = buffers->width << 2;
Chris Wilson86055df2014-08-29 17:36:29 +0100655 *b++ |= 0xf0 << 16 | 1 << 25 | 1 << 24;
Chris Wilson6c428a62014-08-29 13:11:37 +0100656 *b++ = 0;
Chris Wilson37f4da02016-01-27 13:02:35 +0000657 *b++ = buffers->height << 16 | buffers->width;
Chris Wilson6c428a62014-08-29 13:11:37 +0100658 reloc[0].offset = (b - buf) * sizeof(uint32_t);
659 reloc[0].target_handle = bo->handle;
660 reloc[0].read_domains = I915_GEM_DOMAIN_RENDER;
661 reloc[0].write_domain = I915_GEM_DOMAIN_RENDER;
662 *b++ = 0;
663 if (gen >= 8)
664 *b++ = 0;
665 *b++ = val;
666 *b++ = MI_BATCH_BUFFER_END;
667 if ((b - buf) & 1)
668 *b++ = 0;
669
670 gem_exec[0].handle = bo->handle;
671 gem_exec[0].flags = EXEC_OBJECT_NEEDS_FENCE;
672
Chris Wilson6f759902016-01-27 11:17:03 +0000673 gem_exec[1].handle = gem_create(fd, 4096);
Chris Wilson6c428a62014-08-29 13:11:37 +0100674 gem_exec[1].relocation_count = 1;
675 gem_exec[1].relocs_ptr = (uintptr_t)reloc;
676
677 execbuf.buffers_ptr = (uintptr_t)gem_exec;
678 execbuf.buffer_count = 2;
679 execbuf.batch_len = (b - buf) * sizeof(buf[0]);
Chris Wilson86055df2014-08-29 17:36:29 +0100680 if (gen >= 6)
681 execbuf.flags = I915_EXEC_BLT;
Chris Wilson6c428a62014-08-29 13:11:37 +0100682
Chris Wilson6f759902016-01-27 11:17:03 +0000683 gem_write(fd, gem_exec[1].handle, 0, buf, execbuf.batch_len);
684 gem_execbuf(fd, &execbuf);
Chris Wilson6c428a62014-08-29 13:11:37 +0100685
Chris Wilson6f759902016-01-27 11:17:03 +0000686 gem_close(fd, gem_exec[1].handle);
Chris Wilson6c428a62014-08-29 13:11:37 +0100687}
688
689static void
Chris Wilson37f4da02016-01-27 13:02:35 +0000690gpu_cmp_bo(struct buffers *b, drm_intel_bo *bo, uint32_t val)
Chris Wilson6c428a62014-08-29 13:11:37 +0100691{
Chris Wilson37f4da02016-01-27 13:02:35 +0000692 blt_copy_bo(b, b->snoop, bo);
693 cpu_cmp_bo(b, b->snoop, val);
Chris Wilson6c428a62014-08-29 13:11:37 +0100694}
695
Chris Wilsonc2248ef2016-03-19 13:10:17 +0000696struct access_mode {
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530697 const char *name;
Chris Wilsone85613b2016-03-19 14:01:38 +0000698 void (*require)(const struct create *, unsigned);
Chris Wilson4eba8e22016-03-18 10:44:31 +0000699 drm_intel_bo *(*create_bo)(const struct buffers *b);
Chris Wilson37f4da02016-01-27 13:02:35 +0000700 void (*set_bo)(struct buffers *b, drm_intel_bo *bo, uint32_t val);
701 void (*cmp_bo)(struct buffers *b, drm_intel_bo *bo, uint32_t val);
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530702 void (*release_bo)(drm_intel_bo *bo);
Daniel Vetter43779e32013-08-14 14:50:50 +0200703};
Chris Wilson59c55622014-08-29 13:11:37 +0100704igt_render_copyfunc_t rendercopy;
705
Chris Wilson5d669bf2016-03-18 14:44:53 +0000706static int read_sysctl(const char *path)
707{
708 FILE *file = fopen(path, "r");
709 int max = 0;
710 if (file) {
Chris Wilsonc46f3c32016-04-10 20:44:58 +0100711 if (fscanf(file, "%d", &max) != 1)
712 max = 0; /* silence! */
Chris Wilson5d669bf2016-03-18 14:44:53 +0000713 fclose(file);
714 }
715 return max;
716}
717
718static int write_sysctl(const char *path, int value)
719{
720 FILE *file = fopen(path, "w");
721 if (file) {
722 fprintf(file, "%d", value);
723 fclose(file);
724 }
725 return read_sysctl(path);
726}
727
728static bool set_max_map_count(int num_buffers)
729{
730 int max = read_sysctl("/proc/sys/vm/max_map_count");
731 if (max < num_buffers + 1024)
732 max = write_sysctl("/proc/sys/vm/max_map_count",
733 num_buffers + 1024);
734 return max > num_buffers;
735}
736
Chris Wilson4eba8e22016-03-18 10:44:31 +0000737static void buffers_init(struct buffers *b,
738 const char *name,
739 const struct create *create,
Chris Wilson094e0cb2016-03-01 13:22:03 +0000740 const struct access_mode *mode,
Chris Wilson5d669bf2016-03-18 14:44:53 +0000741 const struct size *size,
Chris Wilson094e0cb2016-03-01 13:22:03 +0000742 int num_buffers,
Chris Wilson094e0cb2016-03-01 13:22:03 +0000743 int _fd, int enable_reuse)
Chris Wilson99b5ee82015-01-22 10:03:45 +0000744{
Chris Wilson4eba8e22016-03-18 10:44:31 +0000745 memset(b, 0, sizeof(*b));
746 b->name = name;
747 b->create = create;
748 b->mode = mode;
Chris Wilson5d669bf2016-03-18 14:44:53 +0000749 b->size = size;
Chris Wilson4eba8e22016-03-18 10:44:31 +0000750 b->num_buffers = num_buffers;
Chris Wilson4eba8e22016-03-18 10:44:31 +0000751 b->count = 0;
Chris Wilson37f4da02016-01-27 13:02:35 +0000752
Chris Wilson5d669bf2016-03-18 14:44:53 +0000753 b->width = size->width;
754 b->height = size->height;
755 b->npixels = size->width * size->height;
Chris Wilsond199ad82016-07-27 23:19:41 +0100756 b->page_size = 4*b->npixels;
757 b->page_size = (b->page_size + 4095) & -4096;
758 b->tmp = malloc(b->page_size);
Chris Wilson4eba8e22016-03-18 10:44:31 +0000759 igt_assert(b->tmp);
Chris Wilson99b5ee82015-01-22 10:03:45 +0000760
Chris Wilson4eba8e22016-03-18 10:44:31 +0000761 b->bufmgr = drm_intel_bufmgr_gem_init(_fd, 4096);
762 igt_assert(b->bufmgr);
763
764 b->src = malloc(2*sizeof(drm_intel_bo *)*num_buffers);
765 igt_assert(b->src);
766 b->dst = b->src + num_buffers;
Chris Wilson2d08e9e2015-12-11 09:25:03 +0000767
Chris Wilsona1b47ef2016-01-27 19:44:16 +0000768 if (enable_reuse)
Chris Wilson4eba8e22016-03-18 10:44:31 +0000769 drm_intel_bufmgr_gem_enable_reuse(b->bufmgr);
770 b->batch = intel_batchbuffer_alloc(b->bufmgr, devid);
771 igt_assert(b->batch);
Chris Wilson99b5ee82015-01-22 10:03:45 +0000772}
773
Chris Wilson4eba8e22016-03-18 10:44:31 +0000774static void buffers_destroy(struct buffers *b)
Chris Wilson99b5ee82015-01-22 10:03:45 +0000775{
Chris Wilson4eba8e22016-03-18 10:44:31 +0000776 int count = b->count;
Chris Wilson094e0cb2016-03-01 13:22:03 +0000777 if (count == 0)
Chris Wilson99b5ee82015-01-22 10:03:45 +0000778 return;
779
Chris Wilsonc19b0492016-03-20 11:13:30 +0000780 /* Be safe so that we can clean up a partial creation */
Chris Wilson4eba8e22016-03-18 10:44:31 +0000781 b->count = 0;
Chris Wilsonc19b0492016-03-20 11:13:30 +0000782 for (int i = 0; i < count; i++) {
783 if (b->src[i]) {
784 b->mode->release_bo(b->src[i]);
785 b->src[i] = NULL;
786 } else
787 break;
788
789 if (b->dst[i]) {
790 b->mode->release_bo(b->dst[i]);
791 b->dst[i] = NULL;
792 }
793 }
794 if (b->snoop) {
795 nop_release_bo(b->snoop);
796 b->snoop = NULL;
797 }
798 if (b->spare) {
799 b->mode->release_bo(b->spare);
800 b->spare = NULL;
801 }
Chris Wilson99b5ee82015-01-22 10:03:45 +0000802}
803
Chris Wilson4eba8e22016-03-18 10:44:31 +0000804static void buffers_create(struct buffers *b)
Chris Wilson99b5ee82015-01-22 10:03:45 +0000805{
Chris Wilson4eba8e22016-03-18 10:44:31 +0000806 int count = b->num_buffers;
807 igt_assert(b->bufmgr);
Chris Wilson99b5ee82015-01-22 10:03:45 +0000808
Chris Wilson4eba8e22016-03-18 10:44:31 +0000809 buffers_destroy(b);
810 igt_assert(b->count == 0);
Chris Wilsonc19b0492016-03-20 11:13:30 +0000811 b->count = count;
Chris Wilson99b5ee82015-01-22 10:03:45 +0000812
813 for (int i = 0; i < count; i++) {
Chris Wilson4eba8e22016-03-18 10:44:31 +0000814 b->src[i] = b->mode->create_bo(b);
815 b->dst[i] = b->mode->create_bo(b);
Chris Wilson99b5ee82015-01-22 10:03:45 +0000816 }
Chris Wilson4eba8e22016-03-18 10:44:31 +0000817 b->spare = b->mode->create_bo(b);
818 b->snoop = snoop_create_bo(b);
Chris Wilson99b5ee82015-01-22 10:03:45 +0000819}
820
Chris Wilsonc46f3c32016-04-10 20:44:58 +0100821static void buffers_reset(struct buffers *b, bool enable_reuse)
822{
Chris Wilsonc46f3c32016-04-10 20:44:58 +0100823 b->bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
824 igt_assert(b->bufmgr);
825
826 if (enable_reuse)
827 drm_intel_bufmgr_gem_enable_reuse(b->bufmgr);
828 b->batch = intel_batchbuffer_alloc(b->bufmgr, devid);
829 igt_assert(b->batch);
830}
831
Chris Wilson4eba8e22016-03-18 10:44:31 +0000832static void buffers_fini(struct buffers *b)
Chris Wilson99b5ee82015-01-22 10:03:45 +0000833{
Chris Wilson4eba8e22016-03-18 10:44:31 +0000834 if (b->bufmgr == NULL)
Chris Wilson99b5ee82015-01-22 10:03:45 +0000835 return;
836
Chris Wilson4eba8e22016-03-18 10:44:31 +0000837 buffers_destroy(b);
Chris Wilson99b5ee82015-01-22 10:03:45 +0000838
Chris Wilson4eba8e22016-03-18 10:44:31 +0000839 free(b->tmp);
840 free(b->src);
Chris Wilson2d08e9e2015-12-11 09:25:03 +0000841
Chris Wilson4eba8e22016-03-18 10:44:31 +0000842 intel_batchbuffer_free(b->batch);
843 drm_intel_bufmgr_destroy(b->bufmgr);
Chris Wilson094e0cb2016-03-01 13:22:03 +0000844
Chris Wilson4eba8e22016-03-18 10:44:31 +0000845 memset(b, 0, sizeof(*b));
Chris Wilson99b5ee82015-01-22 10:03:45 +0000846}
847
Chris Wilson37f4da02016-01-27 13:02:35 +0000848typedef void (*do_copy)(struct buffers *b, drm_intel_bo *dst, drm_intel_bo *src);
Chris Wilson0a1fc452016-09-13 11:13:14 +0100849typedef igt_hang_t (*do_hang)(void);
Chris Wilson59c55622014-08-29 13:11:37 +0100850
Chris Wilson37f4da02016-01-27 13:02:35 +0000851static void render_copy_bo(struct buffers *b, drm_intel_bo *dst, drm_intel_bo *src)
Chris Wilson59c55622014-08-29 13:11:37 +0100852{
853 struct igt_buf d = {
854 .bo = dst,
Chris Wilson5d669bf2016-03-18 14:44:53 +0000855 .size = b->npixels * 4,
856 .num_tiles = b->npixels * 4,
Chris Wilson37f4da02016-01-27 13:02:35 +0000857 .stride = b->width * 4,
Chris Wilson59c55622014-08-29 13:11:37 +0100858 }, s = {
859 .bo = src,
Chris Wilson5d669bf2016-03-18 14:44:53 +0000860 .size = b->npixels * 4,
861 .num_tiles = b->npixels * 4,
Chris Wilson37f4da02016-01-27 13:02:35 +0000862 .stride = b->width * 4,
Chris Wilson59c55622014-08-29 13:11:37 +0100863 };
Chris Wilson86055df2014-08-29 17:36:29 +0100864 uint32_t swizzle;
865
866 drm_intel_bo_get_tiling(dst, &d.tiling, &swizzle);
867 drm_intel_bo_get_tiling(src, &s.tiling, &swizzle);
868
Chris Wilson094e0cb2016-03-01 13:22:03 +0000869 rendercopy(b->batch, NULL,
Chris Wilson59c55622014-08-29 13:11:37 +0100870 &s, 0, 0,
Chris Wilson37f4da02016-01-27 13:02:35 +0000871 b->width, b->height,
Chris Wilson59c55622014-08-29 13:11:37 +0100872 &d, 0, 0);
873}
874
Chris Wilson37f4da02016-01-27 13:02:35 +0000875static void blt_copy_bo(struct buffers *b, drm_intel_bo *dst, drm_intel_bo *src)
Chris Wilson59c55622014-08-29 13:11:37 +0100876{
Chris Wilson094e0cb2016-03-01 13:22:03 +0000877 intel_blt_copy(b->batch,
Chris Wilson37f4da02016-01-27 13:02:35 +0000878 src, 0, 0, 4*b->width,
879 dst, 0, 0, 4*b->width,
880 b->width, b->height, 32);
Chris Wilson59c55622014-08-29 13:11:37 +0100881}
Daniel Vetter5a598c92013-08-14 15:08:05 +0200882
Chris Wilson37f4da02016-01-27 13:02:35 +0000883static void cpu_copy_bo(struct buffers *b, drm_intel_bo *dst, drm_intel_bo *src)
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530884{
Chris Wilsond199ad82016-07-27 23:19:41 +0100885 const int size = b->page_size;
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530886 void *d, *s;
887
888 gem_set_domain(fd, src->handle, I915_GEM_DOMAIN_CPU, 0);
889 gem_set_domain(fd, dst->handle, I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
Ville Syrjäläf52e7ec2015-10-09 19:11:39 +0300890 s = gem_mmap__cpu(fd, src->handle, 0, size, PROT_READ);
891 d = gem_mmap__cpu(fd, dst->handle, 0, size, PROT_WRITE);
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530892
893 memcpy(d, s, size);
894
895 munmap(d, size);
896 munmap(s, size);
897}
898
Chris Wilson37f4da02016-01-27 13:02:35 +0000899static void gtt_copy_bo(struct buffers *b, drm_intel_bo *dst, drm_intel_bo *src)
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530900{
Chris Wilsond199ad82016-07-27 23:19:41 +0100901 const int size = b->page_size;
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530902 void *d, *s;
903
904 gem_set_domain(fd, src->handle, I915_GEM_DOMAIN_GTT, 0);
905 gem_set_domain(fd, dst->handle, I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
906
Ville Syrjäläf52e7ec2015-10-09 19:11:39 +0300907 s = gem_mmap__gtt(fd, src->handle, size, PROT_READ);
908 d = gem_mmap__gtt(fd, dst->handle, size, PROT_WRITE);
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530909
910 memcpy(d, s, size);
911
912 munmap(d, size);
913 munmap(s, size);
914}
915
Chris Wilson37f4da02016-01-27 13:02:35 +0000916static void wc_copy_bo(struct buffers *b, drm_intel_bo *dst, drm_intel_bo *src)
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530917{
Chris Wilsond199ad82016-07-27 23:19:41 +0100918 const int size = b->page_size;
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530919 void *d, *s;
920
921 gem_set_domain(fd, src->handle, I915_GEM_DOMAIN_GTT, 0);
922 gem_set_domain(fd, dst->handle, I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
923
Ville Syrjäläf52e7ec2015-10-09 19:11:39 +0300924 s = gem_mmap__wc(fd, src->handle, 0, size, PROT_READ);
925 d = gem_mmap__wc(fd, dst->handle, 0, size, PROT_WRITE);
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530926
927 memcpy(d, s, size);
928
929 munmap(d, size);
930 munmap(s, size);
931}
932
Chris Wilson0a1fc452016-09-13 11:13:14 +0100933static igt_hang_t no_hang(void)
Chris Wilson16bafdf2014-09-04 09:26:24 +0100934{
Chris Wilson0a1fc452016-09-13 11:13:14 +0100935 return (igt_hang_t){0, 0};
Chris Wilson16bafdf2014-09-04 09:26:24 +0100936}
937
Chris Wilson0a1fc452016-09-13 11:13:14 +0100938static igt_hang_t bcs_hang(void)
Chris Wilson16bafdf2014-09-04 09:26:24 +0100939{
Daniel Vetter3cd45de2015-02-10 17:46:43 +0100940 return igt_hang_ring(fd, I915_EXEC_BLT);
Chris Wilson16bafdf2014-09-04 09:26:24 +0100941}
942
Chris Wilson0a1fc452016-09-13 11:13:14 +0100943static igt_hang_t rcs_hang(void)
Chris Wilson16bafdf2014-09-04 09:26:24 +0100944{
Daniel Vetter3cd45de2015-02-10 17:46:43 +0100945 return igt_hang_ring(fd, I915_EXEC_RENDER);
Chris Wilson16bafdf2014-09-04 09:26:24 +0100946}
947
Chris Wilson0a1fc452016-09-13 11:13:14 +0100948static igt_hang_t all_hang(void)
Chris Wilsonfcb4cfa2016-03-20 11:38:26 +0000949{
950 uint32_t bbe = MI_BATCH_BUFFER_END;
951 struct drm_i915_gem_execbuffer2 execbuf;
952 struct drm_i915_gem_exec_object2 obj;
Chris Wilson0a1fc452016-09-13 11:13:14 +0100953 igt_hang_t hang;
Chris Wilsonfcb4cfa2016-03-20 11:38:26 +0000954 unsigned engine;
955
956 memset(&obj, 0, sizeof(obj));
957 obj.handle = gem_create(fd, 4096);
958 gem_write(fd, obj.handle, 0, &bbe, sizeof(&bbe));
959
960 memset(&execbuf, 0, sizeof(execbuf));
961 execbuf.buffers_ptr = (uintptr_t)&obj;
962 execbuf.buffer_count = 1;
963
964 for_each_engine(fd, engine) {
965 hang = igt_hang_ring(fd, engine);
966
967 execbuf.flags = engine;
968 __gem_execbuf(fd, &execbuf);
969
970 gem_close(fd, hang.handle);
971 }
972
973 hang.handle = obj.handle;
974 return hang;
975}
976
Chris Wilson8bf09f32015-12-17 09:16:42 +0000977static void do_basic0(struct buffers *buffers,
978 do_copy do_copy_func,
979 do_hang do_hang_func)
980{
981 gem_quiescent_gpu(fd);
982
Chris Wilson37f4da02016-01-27 13:02:35 +0000983 buffers->mode->set_bo(buffers, buffers->src[0], 0xdeadbeef);
Chris Wilson8bf09f32015-12-17 09:16:42 +0000984 for (int i = 0; i < buffers->count; i++) {
Chris Wilson0a1fc452016-09-13 11:13:14 +0100985 igt_hang_t hang = do_hang_func();
Chris Wilson8bf09f32015-12-17 09:16:42 +0000986
Chris Wilson37f4da02016-01-27 13:02:35 +0000987 do_copy_func(buffers, buffers->dst[i], buffers->src[0]);
988 buffers->mode->cmp_bo(buffers, buffers->dst[i], 0xdeadbeef);
Chris Wilson8bf09f32015-12-17 09:16:42 +0000989
990 igt_post_hang_ring(fd, hang);
991 }
992}
993
994static void do_basic1(struct buffers *buffers,
995 do_copy do_copy_func,
996 do_hang do_hang_func)
Chris Wilson197db862015-12-09 20:54:10 +0000997{
998 gem_quiescent_gpu(fd);
999
1000 for (int i = 0; i < buffers->count; i++) {
Chris Wilson0a1fc452016-09-13 11:13:14 +01001001 igt_hang_t hang = do_hang_func();
Chris Wilson197db862015-12-09 20:54:10 +00001002
Chris Wilson37f4da02016-01-27 13:02:35 +00001003 buffers->mode->set_bo(buffers, buffers->src[i], i);
1004 buffers->mode->set_bo(buffers, buffers->dst[i], ~i);
Chris Wilson8bf09f32015-12-17 09:16:42 +00001005
Chris Wilson37f4da02016-01-27 13:02:35 +00001006 do_copy_func(buffers, buffers->dst[i], buffers->src[i]);
Chris Wilson8bf09f32015-12-17 09:16:42 +00001007 usleep(0); /* let someone else claim the mutex */
Chris Wilson37f4da02016-01-27 13:02:35 +00001008 buffers->mode->cmp_bo(buffers, buffers->dst[i], i);
Chris Wilson197db862015-12-09 20:54:10 +00001009
1010 igt_post_hang_ring(fd, hang);
1011 }
1012}
1013
Chris Wilson8bf09f32015-12-17 09:16:42 +00001014static void do_basicN(struct buffers *buffers,
1015 do_copy do_copy_func,
1016 do_hang do_hang_func)
1017{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001018 igt_hang_t hang;
Chris Wilson8bf09f32015-12-17 09:16:42 +00001019
1020 gem_quiescent_gpu(fd);
1021
1022 for (int i = 0; i < buffers->count; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001023 buffers->mode->set_bo(buffers, buffers->src[i], i);
1024 buffers->mode->set_bo(buffers, buffers->dst[i], ~i);
Chris Wilson8bf09f32015-12-17 09:16:42 +00001025 }
1026
1027 hang = do_hang_func();
1028
1029 for (int i = 0; i < buffers->count; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001030 do_copy_func(buffers, buffers->dst[i], buffers->src[i]);
Chris Wilson8bf09f32015-12-17 09:16:42 +00001031 usleep(0); /* let someone else claim the mutex */
1032 }
1033
1034 for (int i = 0; i < buffers->count; i++)
Chris Wilson37f4da02016-01-27 13:02:35 +00001035 buffers->mode->cmp_bo(buffers, buffers->dst[i], i);
Chris Wilson8bf09f32015-12-17 09:16:42 +00001036
1037 igt_post_hang_ring(fd, hang);
1038}
1039
Chris Wilson99b5ee82015-01-22 10:03:45 +00001040static void do_overwrite_source(struct buffers *buffers,
Chris Wilson16bafdf2014-09-04 09:26:24 +01001041 do_copy do_copy_func,
1042 do_hang do_hang_func)
Daniel Vetter5a598c92013-08-14 15:08:05 +02001043{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001044 igt_hang_t hang;
Daniel Vetter5a598c92013-08-14 15:08:05 +02001045 int i;
1046
1047 gem_quiescent_gpu(fd);
Chris Wilson99b5ee82015-01-22 10:03:45 +00001048 for (i = 0; i < buffers->count; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001049 buffers->mode->set_bo(buffers, buffers->src[i], i);
1050 buffers->mode->set_bo(buffers, buffers->dst[i], ~i);
Daniel Vetter5a598c92013-08-14 15:08:05 +02001051 }
Chris Wilson99b5ee82015-01-22 10:03:45 +00001052 for (i = 0; i < buffers->count; i++)
Chris Wilson37f4da02016-01-27 13:02:35 +00001053 do_copy_func(buffers, buffers->dst[i], buffers->src[i]);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001054 hang = do_hang_func();
Chris Wilson99b5ee82015-01-22 10:03:45 +00001055 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001056 buffers->mode->set_bo(buffers, buffers->src[i], 0xdeadbeef);
Chris Wilson99b5ee82015-01-22 10:03:45 +00001057 for (i = 0; i < buffers->count; i++)
Chris Wilson37f4da02016-01-27 13:02:35 +00001058 buffers->mode->cmp_bo(buffers, buffers->dst[i], i);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001059 igt_post_hang_ring(fd, hang);
1060}
1061
Chris Wilsona1726762015-03-16 16:29:57 +00001062static void do_overwrite_source_read(struct buffers *buffers,
1063 do_copy do_copy_func,
1064 do_hang do_hang_func,
1065 int do_rcs)
1066{
1067 const int half = buffers->count/2;
Chris Wilson0a1fc452016-09-13 11:13:14 +01001068 igt_hang_t hang;
Chris Wilsona1726762015-03-16 16:29:57 +00001069 int i;
1070
1071 gem_quiescent_gpu(fd);
1072 for (i = 0; i < half; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001073 buffers->mode->set_bo(buffers, buffers->src[i], i);
1074 buffers->mode->set_bo(buffers, buffers->dst[i], ~i);
1075 buffers->mode->set_bo(buffers, buffers->dst[i+half], ~i);
Chris Wilsona1726762015-03-16 16:29:57 +00001076 }
1077 for (i = 0; i < half; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001078 do_copy_func(buffers, buffers->dst[i], buffers->src[i]);
Chris Wilsona1726762015-03-16 16:29:57 +00001079 if (do_rcs)
Chris Wilson37f4da02016-01-27 13:02:35 +00001080 render_copy_bo(buffers, buffers->dst[i+half], buffers->src[i]);
Chris Wilsona1726762015-03-16 16:29:57 +00001081 else
Chris Wilson37f4da02016-01-27 13:02:35 +00001082 blt_copy_bo(buffers, buffers->dst[i+half], buffers->src[i]);
Chris Wilsona1726762015-03-16 16:29:57 +00001083 }
1084 hang = do_hang_func();
1085 for (i = half; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001086 buffers->mode->set_bo(buffers, buffers->src[i], 0xdeadbeef);
Chris Wilsona1726762015-03-16 16:29:57 +00001087 for (i = 0; i < half; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001088 buffers->mode->cmp_bo(buffers, buffers->dst[i], i);
1089 buffers->mode->cmp_bo(buffers, buffers->dst[i+half], i);
Chris Wilsona1726762015-03-16 16:29:57 +00001090 }
1091 igt_post_hang_ring(fd, hang);
1092}
1093
1094static void do_overwrite_source_read_bcs(struct buffers *buffers,
1095 do_copy do_copy_func,
1096 do_hang do_hang_func)
1097{
1098 do_overwrite_source_read(buffers, do_copy_func, do_hang_func, 0);
1099}
1100
1101static void do_overwrite_source_read_rcs(struct buffers *buffers,
1102 do_copy do_copy_func,
1103 do_hang do_hang_func)
1104{
1105 do_overwrite_source_read(buffers, do_copy_func, do_hang_func, 1);
1106}
1107
Chris Wilson99b5ee82015-01-22 10:03:45 +00001108static void do_overwrite_source__rev(struct buffers *buffers,
Chris Wilson16bafdf2014-09-04 09:26:24 +01001109 do_copy do_copy_func,
1110 do_hang do_hang_func)
1111{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001112 igt_hang_t hang;
Chris Wilson16bafdf2014-09-04 09:26:24 +01001113 int i;
1114
1115 gem_quiescent_gpu(fd);
Chris Wilson99b5ee82015-01-22 10:03:45 +00001116 for (i = 0; i < buffers->count; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001117 buffers->mode->set_bo(buffers, buffers->src[i], i);
1118 buffers->mode->set_bo(buffers, buffers->dst[i], ~i);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001119 }
Chris Wilson99b5ee82015-01-22 10:03:45 +00001120 for (i = 0; i < buffers->count; i++)
Chris Wilson37f4da02016-01-27 13:02:35 +00001121 do_copy_func(buffers, buffers->dst[i], buffers->src[i]);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001122 hang = do_hang_func();
Chris Wilson99b5ee82015-01-22 10:03:45 +00001123 for (i = 0; i < buffers->count; i++)
Chris Wilson37f4da02016-01-27 13:02:35 +00001124 buffers->mode->set_bo(buffers, buffers->src[i], 0xdeadbeef);
Chris Wilson99b5ee82015-01-22 10:03:45 +00001125 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001126 buffers->mode->cmp_bo(buffers, buffers->dst[i], i);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001127 igt_post_hang_ring(fd, hang);
1128}
1129
Chris Wilson99b5ee82015-01-22 10:03:45 +00001130static void do_overwrite_source__one(struct buffers *buffers,
Chris Wilson16bafdf2014-09-04 09:26:24 +01001131 do_copy do_copy_func,
1132 do_hang do_hang_func)
1133{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001134 igt_hang_t hang;
Chris Wilson16bafdf2014-09-04 09:26:24 +01001135
1136 gem_quiescent_gpu(fd);
Chris Wilson37f4da02016-01-27 13:02:35 +00001137 buffers->mode->set_bo(buffers, buffers->src[0], 0);
1138 buffers->mode->set_bo(buffers, buffers->dst[0], ~0);
1139 do_copy_func(buffers, buffers->dst[0], buffers->src[0]);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001140 hang = do_hang_func();
Chris Wilson37f4da02016-01-27 13:02:35 +00001141 buffers->mode->set_bo(buffers, buffers->src[0], 0xdeadbeef);
1142 buffers->mode->cmp_bo(buffers, buffers->dst[0], 0);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001143 igt_post_hang_ring(fd, hang);
Daniel Vetter5a598c92013-08-14 15:08:05 +02001144}
1145
Chris Wilsona72d4052015-03-18 14:15:22 +00001146static void do_intermix(struct buffers *buffers,
1147 do_copy do_copy_func,
1148 do_hang do_hang_func,
1149 int do_rcs)
1150{
1151 const int half = buffers->count/2;
Chris Wilson0a1fc452016-09-13 11:13:14 +01001152 igt_hang_t hang;
Chris Wilsona72d4052015-03-18 14:15:22 +00001153 int i;
1154
1155 gem_quiescent_gpu(fd);
1156 for (i = 0; i < buffers->count; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001157 buffers->mode->set_bo(buffers, buffers->src[i], 0xdeadbeef^~i);
1158 buffers->mode->set_bo(buffers, buffers->dst[i], i);
Chris Wilsona72d4052015-03-18 14:15:22 +00001159 }
1160 for (i = 0; i < half; i++) {
1161 if (do_rcs == 1 || (do_rcs == -1 && i & 1))
Chris Wilson37f4da02016-01-27 13:02:35 +00001162 render_copy_bo(buffers, buffers->dst[i], buffers->src[i]);
Chris Wilsona72d4052015-03-18 14:15:22 +00001163 else
Chris Wilson37f4da02016-01-27 13:02:35 +00001164 blt_copy_bo(buffers, buffers->dst[i], buffers->src[i]);
Chris Wilsona72d4052015-03-18 14:15:22 +00001165
Chris Wilson37f4da02016-01-27 13:02:35 +00001166 do_copy_func(buffers, buffers->dst[i+half], buffers->src[i]);
Chris Wilsona72d4052015-03-18 14:15:22 +00001167
1168 if (do_rcs == 1 || (do_rcs == -1 && (i & 1) == 0))
Chris Wilson37f4da02016-01-27 13:02:35 +00001169 render_copy_bo(buffers, buffers->dst[i], buffers->dst[i+half]);
Chris Wilsona72d4052015-03-18 14:15:22 +00001170 else
Chris Wilson37f4da02016-01-27 13:02:35 +00001171 blt_copy_bo(buffers, buffers->dst[i], buffers->dst[i+half]);
Chris Wilsona72d4052015-03-18 14:15:22 +00001172
Chris Wilson37f4da02016-01-27 13:02:35 +00001173 do_copy_func(buffers, buffers->dst[i+half], buffers->src[i+half]);
Chris Wilsona72d4052015-03-18 14:15:22 +00001174 }
1175 hang = do_hang_func();
1176 for (i = 0; i < 2*half; i++)
Chris Wilson37f4da02016-01-27 13:02:35 +00001177 buffers->mode->cmp_bo(buffers, buffers->dst[i], 0xdeadbeef^~i);
Chris Wilsona72d4052015-03-18 14:15:22 +00001178 igt_post_hang_ring(fd, hang);
1179}
1180
1181static void do_intermix_rcs(struct buffers *buffers,
1182 do_copy do_copy_func,
1183 do_hang do_hang_func)
1184{
1185 do_intermix(buffers, do_copy_func, do_hang_func, 1);
1186}
1187
1188static void do_intermix_bcs(struct buffers *buffers,
1189 do_copy do_copy_func,
1190 do_hang do_hang_func)
1191{
1192 do_intermix(buffers, do_copy_func, do_hang_func, 0);
1193}
1194
1195static void do_intermix_both(struct buffers *buffers,
1196 do_copy do_copy_func,
1197 do_hang do_hang_func)
1198{
1199 do_intermix(buffers, do_copy_func, do_hang_func, -1);
1200}
1201
Chris Wilson99b5ee82015-01-22 10:03:45 +00001202static void do_early_read(struct buffers *buffers,
Chris Wilson16bafdf2014-09-04 09:26:24 +01001203 do_copy do_copy_func,
1204 do_hang do_hang_func)
Daniel Vetter5a598c92013-08-14 15:08:05 +02001205{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001206 igt_hang_t hang;
Daniel Vetter5a598c92013-08-14 15:08:05 +02001207 int i;
1208
1209 gem_quiescent_gpu(fd);
Chris Wilson99b5ee82015-01-22 10:03:45 +00001210 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001211 buffers->mode->set_bo(buffers, buffers->src[i], 0xdeadbeef);
Chris Wilson99b5ee82015-01-22 10:03:45 +00001212 for (i = 0; i < buffers->count; i++)
Chris Wilson37f4da02016-01-27 13:02:35 +00001213 do_copy_func(buffers, buffers->dst[i], buffers->src[i]);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001214 hang = do_hang_func();
Chris Wilson99b5ee82015-01-22 10:03:45 +00001215 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001216 buffers->mode->cmp_bo(buffers, buffers->dst[i], 0xdeadbeef);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001217 igt_post_hang_ring(fd, hang);
Daniel Vetter5a598c92013-08-14 15:08:05 +02001218}
1219
Chris Wilson35b0ac92015-03-16 11:55:46 +00001220static void do_read_read_bcs(struct buffers *buffers,
1221 do_copy do_copy_func,
1222 do_hang do_hang_func)
1223{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001224 igt_hang_t hang;
Chris Wilson35b0ac92015-03-16 11:55:46 +00001225 int i;
1226
1227 gem_quiescent_gpu(fd);
1228 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001229 buffers->mode->set_bo(buffers, buffers->src[i], 0xdeadbeef ^ i);
Chris Wilson35b0ac92015-03-16 11:55:46 +00001230 for (i = 0; i < buffers->count; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001231 do_copy_func(buffers, buffers->dst[i], buffers->src[i]);
1232 blt_copy_bo(buffers, buffers->spare, buffers->src[i]);
Chris Wilson35b0ac92015-03-16 11:55:46 +00001233 }
Chris Wilson37f4da02016-01-27 13:02:35 +00001234 buffers->mode->cmp_bo(buffers, buffers->spare, 0xdeadbeef^(buffers->count-1));
Chris Wilson35b0ac92015-03-16 11:55:46 +00001235 hang = do_hang_func();
1236 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001237 buffers->mode->cmp_bo(buffers, buffers->dst[i], 0xdeadbeef ^ i);
Chris Wilson35b0ac92015-03-16 11:55:46 +00001238 igt_post_hang_ring(fd, hang);
1239}
1240
Chris Wilson0c266522015-11-11 16:37:16 +00001241static void do_write_read_bcs(struct buffers *buffers,
1242 do_copy do_copy_func,
1243 do_hang do_hang_func)
1244{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001245 igt_hang_t hang;
Chris Wilson0c266522015-11-11 16:37:16 +00001246 int i;
1247
1248 gem_quiescent_gpu(fd);
1249 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001250 buffers->mode->set_bo(buffers, buffers->src[i], 0xdeadbeef ^ i);
Chris Wilson0c266522015-11-11 16:37:16 +00001251 for (i = 0; i < buffers->count; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001252 blt_copy_bo(buffers, buffers->spare, buffers->src[i]);
1253 do_copy_func(buffers, buffers->dst[i], buffers->spare);
Chris Wilson0c266522015-11-11 16:37:16 +00001254 }
1255 hang = do_hang_func();
1256 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001257 buffers->mode->cmp_bo(buffers, buffers->dst[i], 0xdeadbeef ^ i);
Chris Wilson0c266522015-11-11 16:37:16 +00001258 igt_post_hang_ring(fd, hang);
1259}
1260
Chris Wilson35b0ac92015-03-16 11:55:46 +00001261static void do_read_read_rcs(struct buffers *buffers,
1262 do_copy do_copy_func,
1263 do_hang do_hang_func)
1264{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001265 igt_hang_t hang;
Chris Wilson35b0ac92015-03-16 11:55:46 +00001266 int i;
1267
1268 gem_quiescent_gpu(fd);
1269 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001270 buffers->mode->set_bo(buffers, buffers->src[i], 0xdeadbeef ^ i);
Chris Wilson35b0ac92015-03-16 11:55:46 +00001271 for (i = 0; i < buffers->count; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001272 do_copy_func(buffers, buffers->dst[i], buffers->src[i]);
1273 render_copy_bo(buffers, buffers->spare, buffers->src[i]);
Chris Wilson35b0ac92015-03-16 11:55:46 +00001274 }
Chris Wilson37f4da02016-01-27 13:02:35 +00001275 buffers->mode->cmp_bo(buffers, buffers->spare, 0xdeadbeef^(buffers->count-1));
Chris Wilson35b0ac92015-03-16 11:55:46 +00001276 hang = do_hang_func();
1277 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001278 buffers->mode->cmp_bo(buffers, buffers->dst[i], 0xdeadbeef ^ i);
Chris Wilson35b0ac92015-03-16 11:55:46 +00001279 igt_post_hang_ring(fd, hang);
1280}
1281
Chris Wilson0c266522015-11-11 16:37:16 +00001282static void do_write_read_rcs(struct buffers *buffers,
1283 do_copy do_copy_func,
1284 do_hang do_hang_func)
1285{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001286 igt_hang_t hang;
Chris Wilson0c266522015-11-11 16:37:16 +00001287 int i;
1288
1289 gem_quiescent_gpu(fd);
1290 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001291 buffers->mode->set_bo(buffers, buffers->src[i], 0xdeadbeef ^ i);
Chris Wilson0c266522015-11-11 16:37:16 +00001292 for (i = 0; i < buffers->count; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001293 render_copy_bo(buffers, buffers->spare, buffers->src[i]);
1294 do_copy_func(buffers, buffers->dst[i], buffers->spare);
Chris Wilson0c266522015-11-11 16:37:16 +00001295 }
1296 hang = do_hang_func();
1297 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001298 buffers->mode->cmp_bo(buffers, buffers->dst[i], 0xdeadbeef ^ i);
Chris Wilson0c266522015-11-11 16:37:16 +00001299 igt_post_hang_ring(fd, hang);
1300}
1301
Chris Wilson99b5ee82015-01-22 10:03:45 +00001302static void do_gpu_read_after_write(struct buffers *buffers,
Chris Wilson16bafdf2014-09-04 09:26:24 +01001303 do_copy do_copy_func,
1304 do_hang do_hang_func)
Daniel Vetter5a598c92013-08-14 15:08:05 +02001305{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001306 igt_hang_t hang;
Daniel Vetter5a598c92013-08-14 15:08:05 +02001307 int i;
1308
1309 gem_quiescent_gpu(fd);
Chris Wilson99b5ee82015-01-22 10:03:45 +00001310 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001311 buffers->mode->set_bo(buffers, buffers->src[i], 0xabcdabcd);
Chris Wilson99b5ee82015-01-22 10:03:45 +00001312 for (i = 0; i < buffers->count; i++)
Chris Wilson37f4da02016-01-27 13:02:35 +00001313 do_copy_func(buffers, buffers->dst[i], buffers->src[i]);
Chris Wilson99b5ee82015-01-22 10:03:45 +00001314 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001315 do_copy_func(buffers, buffers->spare, buffers->dst[i]);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001316 hang = do_hang_func();
Chris Wilson99b5ee82015-01-22 10:03:45 +00001317 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001318 buffers->mode->cmp_bo(buffers, buffers->dst[i], 0xabcdabcd);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001319 igt_post_hang_ring(fd, hang);
Daniel Vetter5a598c92013-08-14 15:08:05 +02001320}
1321
Chris Wilson99b5ee82015-01-22 10:03:45 +00001322typedef void (*do_test)(struct buffers *buffers,
Chris Wilson16bafdf2014-09-04 09:26:24 +01001323 do_copy do_copy_func,
1324 do_hang do_hang_func);
Daniel Vetterec283d62013-08-14 15:18:37 +02001325
Chris Wilson99b5ee82015-01-22 10:03:45 +00001326typedef void (*run_wrap)(struct buffers *buffers,
Chris Wilson59c55622014-08-29 13:11:37 +01001327 do_test do_test_func,
Chris Wilson16bafdf2014-09-04 09:26:24 +01001328 do_copy do_copy_func,
1329 do_hang do_hang_func);
Daniel Vetterec283d62013-08-14 15:18:37 +02001330
Chris Wilson99b5ee82015-01-22 10:03:45 +00001331static void run_single(struct buffers *buffers,
Chris Wilson59c55622014-08-29 13:11:37 +01001332 do_test do_test_func,
Chris Wilson16bafdf2014-09-04 09:26:24 +01001333 do_copy do_copy_func,
1334 do_hang do_hang_func)
Daniel Vetterec283d62013-08-14 15:18:37 +02001335{
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001336 pass = 0;
Chris Wilson99b5ee82015-01-22 10:03:45 +00001337 do_test_func(buffers, do_copy_func, do_hang_func);
Chris Wilson5b675f72016-01-22 17:33:40 +00001338 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
Daniel Vetterec283d62013-08-14 15:18:37 +02001339}
1340
Chris Wilson99b5ee82015-01-22 10:03:45 +00001341static void run_interruptible(struct buffers *buffers,
Chris Wilson59c55622014-08-29 13:11:37 +01001342 do_test do_test_func,
Chris Wilson16bafdf2014-09-04 09:26:24 +01001343 do_copy do_copy_func,
1344 do_hang do_hang_func)
Daniel Vetterec283d62013-08-14 15:18:37 +02001345{
Chris Wilson1c61c0f2016-01-08 10:51:09 +00001346 pass = 0;
Daniel Vetterd7050f92016-05-11 17:06:28 +02001347 igt_while_interruptible(true)
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001348 do_test_func(buffers, do_copy_func, do_hang_func);
Chris Wilson5b675f72016-01-22 17:33:40 +00001349 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
Daniel Vetterec283d62013-08-14 15:18:37 +02001350}
1351
Chris Wilson46456302016-01-22 19:29:07 +00001352static void run_child(struct buffers *buffers,
1353 do_test do_test_func,
1354 do_copy do_copy_func,
1355 do_hang do_hang_func)
1356
1357{
Chris Wilson69ecede2016-01-22 22:14:33 +00001358 /* We inherit the buffers from the parent, but the bufmgr/batch
1359 * needs to be local as the cache of reusable itself will be COWed,
1360 * leading to the child closing an object without the parent knowing.
1361 */
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001362 pass = 0;
Chris Wilsona1b47ef2016-01-27 19:44:16 +00001363 igt_fork(child, 1)
Chris Wilson46456302016-01-22 19:29:07 +00001364 do_test_func(buffers, do_copy_func, do_hang_func);
Chris Wilson46456302016-01-22 19:29:07 +00001365 igt_waitchildren();
1366 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
1367}
1368
Chris Wilson2d08e9e2015-12-11 09:25:03 +00001369static void __run_forked(struct buffers *buffers,
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001370 int num_children, int loops, bool interrupt,
Chris Wilson2d08e9e2015-12-11 09:25:03 +00001371 do_test do_test_func,
1372 do_copy do_copy_func,
1373 do_hang do_hang_func)
1374
Daniel Vetterec283d62013-08-14 15:18:37 +02001375{
Chris Wilsonc46f3c32016-04-10 20:44:58 +01001376 /* purge the libdrm caches before cloing the process */
Chris Wilsonbca3ab22016-09-23 07:39:57 +01001377 buffers_destroy(buffers);
1378 intel_batchbuffer_free(buffers->batch);
1379 drm_intel_bufmgr_destroy(buffers->bufmgr);
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001380
Chris Wilson2d08e9e2015-12-11 09:25:03 +00001381 igt_fork(child, num_children) {
Chris Wilson459ff6b2016-04-20 07:49:02 +01001382 int num_buffers;
1383
Daniel Vettercd1f2202013-08-29 10:06:51 +02001384 /* recreate process local variables */
Micah Fedkec81d2932015-07-22 21:54:02 +00001385 fd = drm_open_driver(DRIVER_INTEL);
Chris Wilsonf2a045f2015-01-02 16:33:33 +05301386
Chris Wilson459ff6b2016-04-20 07:49:02 +01001387 num_buffers = buffers->num_buffers / num_children;
1388 num_buffers += MIN_BUFFERS;
1389 if (num_buffers < buffers->num_buffers)
1390 buffers->num_buffers = num_buffers;
Chris Wilsonc46f3c32016-04-10 20:44:58 +01001391
1392 buffers_reset(buffers, true);
Chris Wilson094e0cb2016-03-01 13:22:03 +00001393 buffers_create(buffers);
Daniel Vettercd1f2202013-08-29 10:06:51 +02001394
Daniel Vetterd7050f92016-05-11 17:06:28 +02001395 igt_while_interruptible(interrupt) {
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001396 for (pass = 0; pass < loops; pass++)
1397 do_test_func(buffers,
1398 do_copy_func,
1399 do_hang_func);
1400 }
1401 }
Daniel Vettercd1f2202013-08-29 10:06:51 +02001402 igt_waitchildren();
Chris Wilson5b675f72016-01-22 17:33:40 +00001403 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
Chris Wilsonbca3ab22016-09-23 07:39:57 +01001404
1405 buffers_reset(buffers, true);
Daniel Vetterec283d62013-08-14 15:18:37 +02001406}
Daniel Vetter5a598c92013-08-14 15:08:05 +02001407
Chris Wilson2d08e9e2015-12-11 09:25:03 +00001408static void run_forked(struct buffers *buffers,
1409 do_test do_test_func,
1410 do_copy do_copy_func,
1411 do_hang do_hang_func)
1412{
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001413 const int ncpus = sysconf(_SC_NPROCESSORS_ONLN);
1414 __run_forked(buffers, ncpus, ncpus, false,
Chris Wilson2d08e9e2015-12-11 09:25:03 +00001415 do_test_func, do_copy_func, do_hang_func);
1416}
1417
1418static void run_bomb(struct buffers *buffers,
1419 do_test do_test_func,
1420 do_copy do_copy_func,
1421 do_hang do_hang_func)
1422{
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001423 const int ncpus = sysconf(_SC_NPROCESSORS_ONLN);
1424 __run_forked(buffers, 8*ncpus, 2, true,
Chris Wilson2d08e9e2015-12-11 09:25:03 +00001425 do_test_func, do_copy_func, do_hang_func);
1426}
1427
Chris Wilsonf2a045f2015-01-02 16:33:33 +05301428static void cpu_require(void)
1429{
1430 bit17_require();
1431}
1432
1433static void gtt_require(void)
1434{
1435}
1436
Chris Wilson08188752014-09-03 13:38:30 +01001437static void bcs_require(void)
1438{
1439}
1440
1441static void rcs_require(void)
1442{
1443 igt_require(rendercopy);
1444}
1445
Daniel Vetter5a598c92013-08-14 15:08:05 +02001446static void
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001447run_mode(const char *prefix,
1448 const struct create *create,
1449 const struct access_mode *mode,
1450 const struct size *size,
1451 const int num_buffers,
1452 const char *suffix,
1453 run_wrap run_wrap_func)
Daniel Vetter5a598c92013-08-14 15:08:05 +02001454{
Chris Wilsonf2a045f2015-01-02 16:33:33 +05301455 const struct {
Chris Wilson59c55622014-08-29 13:11:37 +01001456 const char *prefix;
1457 do_copy copy;
Chris Wilson08188752014-09-03 13:38:30 +01001458 void (*require)(void);
Chris Wilson59c55622014-08-29 13:11:37 +01001459 } pipelines[] = {
Chris Wilsonf2a045f2015-01-02 16:33:33 +05301460 { "cpu", cpu_copy_bo, cpu_require },
1461 { "gtt", gtt_copy_bo, gtt_require },
1462 { "wc", wc_copy_bo, wc_require },
Daniel Vetter3e9b4e32015-02-06 23:10:26 +01001463 { "blt", blt_copy_bo, bcs_require },
1464 { "render", render_copy_bo, rcs_require },
Chris Wilson59c55622014-08-29 13:11:37 +01001465 { NULL, NULL }
Chris Wilson77633492015-03-26 08:11:43 +00001466 }, *pskip = pipelines + 3, *p;
Chris Wilson16bafdf2014-09-04 09:26:24 +01001467 const struct {
1468 const char *suffix;
1469 do_hang hang;
Chris Wilson16bafdf2014-09-04 09:26:24 +01001470 } hangs[] = {
Chris Wilson92caf132015-12-16 09:23:56 +00001471 { "", no_hang },
1472 { "-hang-blt", bcs_hang },
1473 { "-hang-render", rcs_hang },
Chris Wilsonfcb4cfa2016-03-20 11:38:26 +00001474 { "-hang-all", all_hang },
Chris Wilson16bafdf2014-09-04 09:26:24 +01001475 { NULL, NULL },
1476 }, *h;
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001477 struct buffers buffers;
1478
1479 igt_fixture
1480 buffers_init(&buffers, prefix, create, mode,
1481 size, num_buffers,
1482 fd, run_wrap_func != run_child);
Daniel Vetter5a598c92013-08-14 15:08:05 +02001483
Chris Wilson16bafdf2014-09-04 09:26:24 +01001484 for (h = hangs; h->suffix; h++) {
Chris Wilson77633492015-03-26 08:11:43 +00001485 if (!all && *h->suffix)
1486 continue;
1487
Chris Wilson6867b872016-03-24 07:57:30 +00001488 if (!*h->suffix)
Daniel Vetterbe21fc02016-06-17 16:04:09 +02001489 igt_fixture
1490 igt_fork_hang_detector(fd);
Chris Wilson6867b872016-03-24 07:57:30 +00001491
Chris Wilson77633492015-03-26 08:11:43 +00001492 for (p = all ? pipelines : pskip; p->prefix; p++) {
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001493 igt_subtest_group {
1494 igt_fixture p->require();
Chris Wilson16bafdf2014-09-04 09:26:24 +01001495
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001496 igt_subtest_f("%s-%s-%s-sanitycheck0%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1497 buffers_create(&buffers);
1498 run_wrap_func(&buffers, do_basic0,
1499 p->copy, h->hang);
1500 }
Chris Wilson8bf09f32015-12-17 09:16:42 +00001501
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001502 igt_subtest_f("%s-%s-%s-sanitycheck1%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1503 buffers_create(&buffers);
1504 run_wrap_func(&buffers, do_basic1,
1505 p->copy, h->hang);
1506 }
Chris Wilson8bf09f32015-12-17 09:16:42 +00001507
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001508 igt_subtest_f("%s-%s-%s-sanitycheckN%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1509 buffers_create(&buffers);
1510 run_wrap_func(&buffers, do_basicN,
1511 p->copy, h->hang);
1512 }
Chris Wilson197db862015-12-09 20:54:10 +00001513
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001514 /* try to overwrite the source values */
1515 igt_subtest_f("%s-%s-%s-overwrite-source-one%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1516 buffers_create(&buffers);
1517 run_wrap_func(&buffers,
1518 do_overwrite_source__one,
1519 p->copy, h->hang);
1520 }
Chris Wilson16bafdf2014-09-04 09:26:24 +01001521
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001522 igt_subtest_f("%s-%s-%s-overwrite-source%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1523 buffers_create(&buffers);
1524 run_wrap_func(&buffers,
1525 do_overwrite_source,
1526 p->copy, h->hang);
1527 }
Chris Wilsona1726762015-03-16 16:29:57 +00001528
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001529 igt_subtest_f("%s-%s-%s-overwrite-source-read-bcs%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1530 buffers_create(&buffers);
1531 run_wrap_func(&buffers,
1532 do_overwrite_source_read_bcs,
1533 p->copy, h->hang);
1534 }
Chris Wilsona1726762015-03-16 16:29:57 +00001535
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001536 igt_subtest_f("%s-%s-%s-overwrite-source-read-rcs%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1537 igt_require(rendercopy);
1538 buffers_create(&buffers);
1539 run_wrap_func(&buffers,
1540 do_overwrite_source_read_rcs,
1541 p->copy, h->hang);
1542 }
Chris Wilsona1726762015-03-16 16:29:57 +00001543
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001544 igt_subtest_f("%s-%s-%s-overwrite-source-rev%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1545 buffers_create(&buffers);
1546 run_wrap_func(&buffers,
1547 do_overwrite_source__rev,
1548 p->copy, h->hang);
1549 }
Chris Wilson16bafdf2014-09-04 09:26:24 +01001550
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001551 /* try to intermix copies with GPU copies*/
1552 igt_subtest_f("%s-%s-%s-intermix-rcs%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1553 igt_require(rendercopy);
1554 buffers_create(&buffers);
1555 run_wrap_func(&buffers,
1556 do_intermix_rcs,
1557 p->copy, h->hang);
1558 }
1559 igt_subtest_f("%s-%s-%s-intermix-bcs%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1560 igt_require(rendercopy);
1561 buffers_create(&buffers);
1562 run_wrap_func(&buffers,
1563 do_intermix_bcs,
1564 p->copy, h->hang);
1565 }
1566 igt_subtest_f("%s-%s-%s-intermix-both%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1567 igt_require(rendercopy);
1568 buffers_create(&buffers);
1569 run_wrap_func(&buffers,
1570 do_intermix_both,
1571 p->copy, h->hang);
1572 }
Chris Wilsona72d4052015-03-18 14:15:22 +00001573
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001574 /* try to read the results before the copy completes */
1575 igt_subtest_f("%s-%s-%s-early-read%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1576 buffers_create(&buffers);
1577 run_wrap_func(&buffers,
1578 do_early_read,
1579 p->copy, h->hang);
1580 }
Chris Wilson16bafdf2014-09-04 09:26:24 +01001581
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001582 /* concurrent reads */
1583 igt_subtest_f("%s-%s-%s-read-read-bcs%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1584 buffers_create(&buffers);
1585 run_wrap_func(&buffers,
1586 do_read_read_bcs,
1587 p->copy, h->hang);
1588 }
1589 igt_subtest_f("%s-%s-%s-read-read-rcs%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1590 igt_require(rendercopy);
1591 buffers_create(&buffers);
1592 run_wrap_func(&buffers,
1593 do_read_read_rcs,
1594 p->copy, h->hang);
1595 }
Chris Wilson35b0ac92015-03-16 11:55:46 +00001596
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001597 /* split copying between rings */
1598 igt_subtest_f("%s-%s-%s-write-read-bcs%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1599 buffers_create(&buffers);
1600 run_wrap_func(&buffers,
1601 do_write_read_bcs,
1602 p->copy, h->hang);
1603 }
1604 igt_subtest_f("%s-%s-%s-write-read-rcs%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1605 igt_require(rendercopy);
1606 buffers_create(&buffers);
1607 run_wrap_func(&buffers,
1608 do_write_read_rcs,
1609 p->copy, h->hang);
1610 }
Chris Wilson0c266522015-11-11 16:37:16 +00001611
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001612 /* and finally try to trick the kernel into loosing the pending write */
1613 igt_subtest_f("%s-%s-%s-gpu-read-after-write%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1614 buffers_create(&buffers);
1615 run_wrap_func(&buffers,
1616 do_gpu_read_after_write,
1617 p->copy, h->hang);
1618 }
Chris Wilson16bafdf2014-09-04 09:26:24 +01001619 }
Chris Wilson08188752014-09-03 13:38:30 +01001620 }
Chris Wilson6867b872016-03-24 07:57:30 +00001621
1622 if (!*h->suffix)
Daniel Vetterbe21fc02016-06-17 16:04:09 +02001623 igt_fixture
1624 igt_stop_hang_detector();
Chris Wilson59c55622014-08-29 13:11:37 +01001625 }
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001626
1627 igt_fixture
1628 buffers_fini(&buffers);
Daniel Vetter5a598c92013-08-14 15:08:05 +02001629}
Daniel Vetter43779e32013-08-14 14:50:50 +02001630
1631static void
Chris Wilson094e0cb2016-03-01 13:22:03 +00001632run_modes(const char *style,
Chris Wilson4eba8e22016-03-18 10:44:31 +00001633 const struct create *create,
Chris Wilson094e0cb2016-03-01 13:22:03 +00001634 const struct access_mode *mode,
Chris Wilson5d669bf2016-03-18 14:44:53 +00001635 const struct size *size,
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001636 const int num)
Daniel Vetter43779e32013-08-14 14:50:50 +02001637{
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001638 const struct wrap {
1639 const char *suffix;
1640 run_wrap func;
1641 } wrappers[] = {
1642 { "", run_single },
1643 { "-child", run_child },
1644 { "-forked", run_forked },
1645 { "-interruptible", run_interruptible },
1646 { "-bomb", run_bomb },
1647 { NULL },
1648 };
Daniel Vetter3dba47e2013-08-06 22:27:37 +02001649
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001650 while (mode->name) {
1651 igt_subtest_group {
1652 igt_fixture {
1653 if (mode->require)
Chris Wilsone85613b2016-03-19 14:01:38 +00001654 mode->require(create, num);
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001655 }
1656
1657 for (const struct wrap *w = wrappers; w->suffix; w++) {
1658 run_mode(style, create, mode, size, num,
1659 w->suffix, w->func);
1660 }
Daniel Vetter96650732016-03-18 21:55:00 +01001661 }
1662
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001663 mode++;
Daniel Vetter96650732016-03-18 21:55:00 +01001664 }
Daniel Vetter43779e32013-08-14 14:50:50 +02001665}
1666
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001667static unsigned
Chris Wilsonf338e982016-03-19 13:10:17 +00001668num_buffers(uint64_t max,
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001669 const struct size *s,
1670 const struct create *c,
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001671 unsigned allow_mem)
1672{
1673 unsigned size = 4*s->width*s->height;
Chris Wilson9e7e7c32016-04-11 09:17:33 +01001674 uint64_t n;
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001675
Chris Wilson9e7e7c32016-04-11 09:17:33 +01001676 igt_assert(size);
1677 n = max / (2*size);
1678 n += MIN_BUFFERS;
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001679
Chris Wilson9e7e7c32016-04-11 09:17:33 +01001680 igt_require(n < INT32_MAX);
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001681 igt_require(set_max_map_count(2*n));
1682
Chris Wilsone85613b2016-03-19 14:01:38 +00001683 if (c->require)
1684 c->require(c, n);
1685
Chris Wilson6867b872016-03-24 07:57:30 +00001686 intel_require_memory(2*n, size, allow_mem);
1687
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001688 return n;
1689}
1690
Chris Wilson3d8af562016-03-20 10:49:54 +00001691static bool allow_unlimited_files(void)
1692{
1693 struct rlimit rlim;
1694 unsigned nofile_rlim = 1024*1024;
1695
1696 FILE *file = fopen("/proc/sys/fs/file-max", "r");
1697 if (file) {
1698 igt_assert(fscanf(file, "%u", &nofile_rlim) == 1);
1699 igt_info("System limit for open files is %u\n", nofile_rlim);
1700 fclose(file);
1701 }
1702
1703 if (getrlimit(RLIMIT_NOFILE, &rlim))
1704 return false;
1705
1706 rlim.rlim_cur = nofile_rlim;
1707 rlim.rlim_max = nofile_rlim;
1708 return setrlimit(RLIMIT_NOFILE, &rlim) == 0;
1709}
1710
Daniel Vetter071e9ca2013-10-31 16:23:26 +01001711igt_main
Daniel Vetter43779e32013-08-14 14:50:50 +02001712{
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001713 const struct access_mode modes[] = {
1714 {
1715 .name = "prw",
1716 .create_bo = unmapped_create_bo,
1717 .set_bo = prw_set_bo,
1718 .cmp_bo = prw_cmp_bo,
1719 .release_bo = nop_release_bo,
1720 },
1721 {
1722 .name = "partial",
1723 .create_bo = unmapped_create_bo,
1724 .set_bo = partial_set_bo,
1725 .cmp_bo = partial_cmp_bo,
1726 .release_bo = nop_release_bo,
1727 },
1728 {
1729 .name = "cpu",
1730 .create_bo = unmapped_create_bo,
1731 .require = create_cpu_require,
1732 .set_bo = cpu_set_bo,
1733 .cmp_bo = cpu_cmp_bo,
1734 .release_bo = nop_release_bo,
1735 },
1736 {
1737 .name = "snoop",
1738 .create_bo = snoop_create_bo,
1739 .require = create_snoop_require,
1740 .set_bo = cpu_set_bo,
1741 .cmp_bo = cpu_cmp_bo,
1742 .release_bo = nop_release_bo,
1743 },
1744 {
1745 .name = "userptr",
1746 .create_bo = userptr_create_bo,
1747 .require = create_userptr_require,
1748 .set_bo = userptr_set_bo,
1749 .cmp_bo = userptr_cmp_bo,
1750 .release_bo = userptr_release_bo,
1751 },
1752 {
1753 .name = "dmabuf",
1754 .create_bo = dmabuf_create_bo,
1755 .require = create_dmabuf_require,
1756 .set_bo = dmabuf_set_bo,
1757 .cmp_bo = dmabuf_cmp_bo,
1758 .release_bo = dmabuf_release_bo,
1759 },
1760 {
Chris Wilsone7ed4ef2016-07-18 11:38:09 +01001761 .name = "vgem",
1762 .create_bo = vgem_create_bo,
1763 .require = create_vgem_require,
1764 .set_bo = dmabuf_set_bo,
1765 .cmp_bo = dmabuf_cmp_bo,
1766 .release_bo = dmabuf_release_bo,
1767 },
1768 {
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001769 .name = "gtt",
1770 .create_bo = gtt_create_bo,
1771 .set_bo = gtt_set_bo,
1772 .cmp_bo = gtt_cmp_bo,
1773 .release_bo = nop_release_bo,
1774 },
1775 {
1776 .name = "gttX",
1777 .create_bo = gttX_create_bo,
1778 .set_bo = gtt_set_bo,
1779 .cmp_bo = gtt_cmp_bo,
1780 .release_bo = nop_release_bo,
1781 },
1782 {
1783 .name = "wc",
1784 .require = wc_create_require,
1785 .create_bo = wc_create_bo,
1786 .set_bo = gtt_set_bo,
1787 .cmp_bo = gtt_cmp_bo,
1788 .release_bo = wc_release_bo,
1789 },
1790 {
1791 .name = "gpu",
1792 .create_bo = gpu_create_bo,
1793 .set_bo = gpu_set_bo,
1794 .cmp_bo = gpu_cmp_bo,
1795 .release_bo = nop_release_bo,
1796 },
1797 {
1798 .name = "gpuX",
1799 .create_bo = gpuX_create_bo,
1800 .set_bo = gpu_set_bo,
1801 .cmp_bo = gpu_cmp_bo,
1802 .release_bo = nop_release_bo,
1803 },
1804 { NULL },
1805 };
Chris Wilson4eba8e22016-03-18 10:44:31 +00001806 const struct create create[] = {
1807 { "", can_create_normal, create_normal_bo},
1808#if HAVE_CREATE_PRIVATE
1809 { "private-", can_create_private, create_private_bo},
1810#endif
1811#if HAVE_CREATE_STOLEN
1812 { "stolen-", can_create_stolen, create_stolen_bo},
1813#endif
Chris Wilson1d6e5d32016-01-03 13:44:17 +00001814 { NULL, NULL }
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001815 };
Chris Wilson5d669bf2016-03-18 14:44:53 +00001816 const struct size sizes[] = {
1817 { "4KiB", 128, 8 },
1818 { "256KiB", 128, 128 },
1819 { "1MiB", 512, 512 },
1820 { "16MiB", 2048, 2048 },
1821 { NULL}
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001822 };
Chris Wilson42291f22016-01-07 11:19:26 +00001823 uint64_t pin_sz = 0;
1824 void *pinned = NULL;
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001825 char name[80];
1826 int count = 0;
Daniel Vetter43779e32013-08-14 14:50:50 +02001827
Daniel Vetter43779e32013-08-14 14:50:50 +02001828 igt_skip_on_simulation();
1829
Chris Wilson77633492015-03-26 08:11:43 +00001830 if (strstr(igt_test_name(), "all"))
1831 all = true;
1832
Daniel Vetter2dbd9982013-08-14 15:48:54 +02001833 igt_fixture {
Chris Wilson98dcf2f2016-03-25 00:50:45 +00001834 allow_unlimited_files();
Chris Wilson3d8af562016-03-20 10:49:54 +00001835
Micah Fedkec81d2932015-07-22 21:54:02 +00001836 fd = drm_open_driver(DRIVER_INTEL);
Chris Wilson5b675f72016-01-22 17:33:40 +00001837 intel_detect_and_clear_missed_interrupts(fd);
Chris Wilson6c428a62014-08-29 13:11:37 +01001838 devid = intel_get_drm_devid(fd);
1839 gen = intel_gen(devid);
Chris Wilson59c55622014-08-29 13:11:37 +01001840 rendercopy = igt_get_render_copyfunc(devid);
Chris Wilsone7ed4ef2016-07-18 11:38:09 +01001841
1842 vgem_drv = __drm_open_driver(DRIVER_VGEM);
Chris Wilson2d08e9e2015-12-11 09:25:03 +00001843 }
Daniel Vetter43779e32013-08-14 14:50:50 +02001844
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001845 for (const struct create *c = create; c->name; c++) {
1846 for (const struct size *s = sizes; s->name; s++) {
1847 /* Minimum test set */
Chris Wilsonf338e982016-03-19 13:10:17 +00001848 snprintf(name, sizeof(name), "%s%s-%s",
1849 c->name, s->name, "tiny");
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001850 igt_subtest_group {
1851 igt_fixture {
Chris Wilsonf338e982016-03-19 13:10:17 +00001852 count = num_buffers(0, s, c, CHECK_RAM);
Chris Wilson5d669bf2016-03-18 14:44:53 +00001853 }
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001854 run_modes(name, c, modes, s, count);
Chris Wilson5d669bf2016-03-18 14:44:53 +00001855 }
1856
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001857 /* "Average" test set */
Chris Wilsonf338e982016-03-19 13:10:17 +00001858 snprintf(name, sizeof(name), "%s%s-%s",
1859 c->name, s->name, "small");
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001860 igt_subtest_group {
1861 igt_fixture {
Chris Wilsonf338e982016-03-19 13:10:17 +00001862 count = num_buffers(gem_mappable_aperture_size()/4,
1863 s, c, CHECK_RAM);
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001864 }
1865 run_modes(name, c, modes, s, count);
1866 }
Chris Wilson5d669bf2016-03-18 14:44:53 +00001867
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001868 /* Use the entire mappable aperture */
Chris Wilsonf338e982016-03-19 13:10:17 +00001869 snprintf(name, sizeof(name), "%s%s-%s",
1870 c->name, s->name, "thrash");
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001871 igt_subtest_group {
1872 igt_fixture {
Chris Wilsonf338e982016-03-19 13:10:17 +00001873 count = num_buffers(gem_mappable_aperture_size(),
1874 s, c, CHECK_RAM);
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001875 }
1876 run_modes(name, c, modes, s, count);
1877 }
1878
1879 /* Use the entire global GTT */
Chris Wilsonf338e982016-03-19 13:10:17 +00001880 snprintf(name, sizeof(name), "%s%s-%s",
1881 c->name, s->name, "global");
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001882 igt_subtest_group {
1883 igt_fixture {
Chris Wilsonf338e982016-03-19 13:10:17 +00001884 count = num_buffers(gem_global_aperture_size(fd),
1885 s, c, CHECK_RAM);
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001886 }
1887 run_modes(name, c, modes, s, count);
1888 }
1889
1890 /* Use the entire per-process GTT */
Chris Wilsonf338e982016-03-19 13:10:17 +00001891 snprintf(name, sizeof(name), "%s%s-%s",
1892 c->name, s->name, "full");
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001893 igt_subtest_group {
1894 igt_fixture {
Chris Wilsonf338e982016-03-19 13:10:17 +00001895 count = num_buffers(gem_aperture_size(fd),
1896 s, c, CHECK_RAM);
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001897 }
1898 run_modes(name, c, modes, s, count);
1899 }
1900
1901 /* Use the entire mappable aperture, force swapping */
Chris Wilsonf338e982016-03-19 13:10:17 +00001902 snprintf(name, sizeof(name), "%s%s-%s",
1903 c->name, s->name, "swap");
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001904 igt_subtest_group {
1905 igt_fixture {
1906 if (intel_get_avail_ram_mb() > gem_mappable_aperture_size()/(1024*1024)) {
1907 pin_sz = intel_get_avail_ram_mb() - gem_mappable_aperture_size()/(1024*1024);
1908
1909 igt_debug("Pinning %lld MiB\n", (long long)pin_sz);
1910 pin_sz *= 1024 * 1024;
1911
1912 if (posix_memalign(&pinned, 4096, pin_sz) ||
1913 mlock(pinned, pin_sz) ||
1914 madvise(pinned, pin_sz, MADV_DONTFORK)) {
1915 free(pinned);
1916 pinned = NULL;
1917 }
1918 igt_require(pinned);
1919 }
1920
Chris Wilsonf338e982016-03-19 13:10:17 +00001921 count = num_buffers(gem_mappable_aperture_size(),
1922 s, c, CHECK_RAM | CHECK_SWAP);
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001923 }
1924 run_modes(name, c, modes, s, count);
1925
1926 igt_fixture {
1927 if (pinned) {
1928 munlock(pinned, pin_sz);
1929 free(pinned);
1930 pinned = NULL;
1931 }
Chris Wilson5d669bf2016-03-18 14:44:53 +00001932 }
Chris Wilson42291f22016-01-07 11:19:26 +00001933 }
Chris Wilson1d6e5d32016-01-03 13:44:17 +00001934 }
Daniel Vetter2dbd9982013-08-14 15:48:54 +02001935 }
Daniel Vetter3dba47e2013-08-06 22:27:37 +02001936}