blob: 266995d14bb2b06e9682040ca9fb78278eabef66 [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;
Chris Wilson8f393102017-01-02 11:23:33 +0000246 free(from_user_pointer(arg.user_ptr));
Chris Wilson0143d4f2016-01-21 09:53:50 +0000247 }
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);
Chris Wilson4de67b22017-01-02 11:05:21 +0000266 userptr.user_ptr = to_user_pointer(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
Chris Wilson8f393102017-01-02 11:23:33 +0000278 bo->virtual = from_user_pointer(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;
Daniel Vetter43779e32013-08-14 14:50:50 +0200488
489 drm_intel_gem_bo_start_gtt_access(bo, true);
Chris Wilson94ca1702016-09-26 09:23:42 +0100490 for (int y = 0; y < b->height; y++)
491 vaddr[pixel(y, b->width)] = val;
Daniel Vetter43779e32013-08-14 14:50:50 +0200492}
493
494static void
Chris Wilson37f4da02016-01-27 13:02:35 +0000495gtt_cmp_bo(struct buffers *b, drm_intel_bo *bo, uint32_t val)
Daniel Vetter43779e32013-08-14 14:50:50 +0200496{
Chris Wilson3e766b82014-09-26 07:55:49 +0100497 uint32_t *vaddr = bo->virtual;
Daniel Vetter43779e32013-08-14 14:50:50 +0200498
Chris Wilson3e766b82014-09-26 07:55:49 +0100499 /* GTT access is slow. So we just compare a few points */
Daniel Vetter43779e32013-08-14 14:50:50 +0200500 drm_intel_gem_bo_start_gtt_access(bo, false);
Chris Wilson37f4da02016-01-27 13:02:35 +0000501 for (int y = 0; y < b->height; y++)
502 igt_assert_eq_u32(vaddr[pixel(y, b->width)], val);
Daniel Vetter43779e32013-08-14 14:50:50 +0200503}
504
505static drm_intel_bo *
Chris Wilson86055df2014-08-29 17:36:29 +0100506map_bo(drm_intel_bo *bo)
Daniel Vetter43779e32013-08-14 14:50:50 +0200507{
Daniel Vetter43779e32013-08-14 14:50:50 +0200508 /* gtt map doesn't have a write parameter, so just keep the mapping
509 * around (to avoid the set_domain with the gtt write domain set) and
510 * manually tell the kernel when we start access the gtt. */
511 do_or_die(drm_intel_gem_bo_map_gtt(bo));
512
513 return bo;
514}
515
Chris Wilson86055df2014-08-29 17:36:29 +0100516static drm_intel_bo *
517tile_bo(drm_intel_bo *bo, int width)
518{
519 uint32_t tiling = I915_TILING_X;
520 uint32_t stride = width * 4;
521
522 do_or_die(drm_intel_bo_set_tiling(bo, &tiling, stride));
523
524 return bo;
525}
526
527static drm_intel_bo *
Chris Wilson4eba8e22016-03-18 10:44:31 +0000528gtt_create_bo(const struct buffers *b)
Chris Wilson86055df2014-08-29 17:36:29 +0100529{
Chris Wilson4eba8e22016-03-18 10:44:31 +0000530 return map_bo(unmapped_create_bo(b));
Chris Wilson86055df2014-08-29 17:36:29 +0100531}
532
533static drm_intel_bo *
Chris Wilson4eba8e22016-03-18 10:44:31 +0000534gttX_create_bo(const struct buffers *b)
Chris Wilson86055df2014-08-29 17:36:29 +0100535{
Chris Wilson4eba8e22016-03-18 10:44:31 +0000536 return tile_bo(gtt_create_bo(b), b->width);
Chris Wilson86055df2014-08-29 17:36:29 +0100537}
538
Chris Wilson5d669bf2016-03-18 14:44:53 +0000539static void bit17_require(void)
540{
541 static struct drm_i915_gem_get_tiling2 {
542 uint32_t handle;
543 uint32_t tiling_mode;
544 uint32_t swizzle_mode;
545 uint32_t phys_swizzle_mode;
546 } arg;
547#define DRM_IOCTL_I915_GEM_GET_TILING2 DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_GEM_GET_TILING, struct drm_i915_gem_get_tiling2)
548
549 if (arg.handle == 0) {
550 arg.handle = gem_create(fd, 4096);
551 gem_set_tiling(fd, arg.handle, I915_TILING_X, 512);
552
553 do_ioctl(fd, DRM_IOCTL_I915_GEM_GET_TILING2, &arg);
554 gem_close(fd, arg.handle);
555 }
556 igt_require(arg.phys_swizzle_mode == arg.swizzle_mode);
557}
558
559static void wc_require(void)
560{
561 bit17_require();
562 gem_require_mmap_wc(fd);
563}
564
565static void
Chris Wilsone85613b2016-03-19 14:01:38 +0000566wc_create_require(const struct create *create, unsigned count)
Chris Wilson5d669bf2016-03-18 14:44:53 +0000567{
568 wc_require();
569}
570
Chris Wilson86055df2014-08-29 17:36:29 +0100571static drm_intel_bo *
Chris Wilson4eba8e22016-03-18 10:44:31 +0000572wc_create_bo(const struct buffers *b)
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530573{
574 drm_intel_bo *bo;
575
Chris Wilson4eba8e22016-03-18 10:44:31 +0000576 bo = unmapped_create_bo(b);
Chris Wilson5d669bf2016-03-18 14:44:53 +0000577 bo->virtual = gem_mmap__wc(fd, bo->handle, 0, bo->size, PROT_READ | PROT_WRITE);
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530578 return bo;
579}
580
581static void
582wc_release_bo(drm_intel_bo *bo)
583{
Chris Wilson094e0cb2016-03-01 13:22:03 +0000584 igt_assert(bo->virtual);
585
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530586 munmap(bo->virtual, bo->size);
587 bo->virtual = NULL;
588
589 nop_release_bo(bo);
590}
591
592static drm_intel_bo *
Chris Wilson4eba8e22016-03-18 10:44:31 +0000593gpu_create_bo(const struct buffers *b)
Chris Wilson86055df2014-08-29 17:36:29 +0100594{
Chris Wilson4eba8e22016-03-18 10:44:31 +0000595 return unmapped_create_bo(b);
Chris Wilson86055df2014-08-29 17:36:29 +0100596}
597
Chris Wilson86055df2014-08-29 17:36:29 +0100598static drm_intel_bo *
Chris Wilson4eba8e22016-03-18 10:44:31 +0000599gpuX_create_bo(const struct buffers *b)
Chris Wilson86055df2014-08-29 17:36:29 +0100600{
Chris Wilson4eba8e22016-03-18 10:44:31 +0000601 return tile_bo(gpu_create_bo(b), b->width);
Chris Wilson86055df2014-08-29 17:36:29 +0100602}
603
Daniel Vetter43779e32013-08-14 14:50:50 +0200604static void
Chris Wilson37f4da02016-01-27 13:02:35 +0000605cpu_set_bo(struct buffers *b, drm_intel_bo *bo, uint32_t val)
Daniel Vetter43779e32013-08-14 14:50:50 +0200606{
Chris Wilson5d669bf2016-03-18 14:44:53 +0000607 int size = b->npixels;
Daniel Vetter43779e32013-08-14 14:50:50 +0200608 uint32_t *vaddr;
609
610 do_or_die(drm_intel_bo_map(bo, true));
611 vaddr = bo->virtual;
612 while (size--)
613 *vaddr++ = val;
614 drm_intel_bo_unmap(bo);
615}
616
617static void
Chris Wilson37f4da02016-01-27 13:02:35 +0000618cpu_cmp_bo(struct buffers *b, drm_intel_bo *bo, uint32_t val)
Daniel Vetter43779e32013-08-14 14:50:50 +0200619{
Chris Wilson5d669bf2016-03-18 14:44:53 +0000620 int size = b->npixels;
Daniel Vetter43779e32013-08-14 14:50:50 +0200621 uint32_t *vaddr;
622
623 do_or_die(drm_intel_bo_map(bo, false));
624 vaddr = bo->virtual;
625 while (size--)
Chris Wilson6c428a62014-08-29 13:11:37 +0100626 igt_assert_eq_u32(*vaddr++, val);
Daniel Vetter43779e32013-08-14 14:50:50 +0200627 drm_intel_bo_unmap(bo);
628}
629
Chris Wilson6c428a62014-08-29 13:11:37 +0100630static void
Chris Wilson37f4da02016-01-27 13:02:35 +0000631gpu_set_bo(struct buffers *buffers, drm_intel_bo *bo, uint32_t val)
Chris Wilson6c428a62014-08-29 13:11:37 +0100632{
633 struct drm_i915_gem_relocation_entry reloc[1];
634 struct drm_i915_gem_exec_object2 gem_exec[2];
635 struct drm_i915_gem_execbuffer2 execbuf;
Chris Wilson6c428a62014-08-29 13:11:37 +0100636 uint32_t buf[10], *b;
Chris Wilson86055df2014-08-29 17:36:29 +0100637 uint32_t tiling, swizzle;
638
639 drm_intel_bo_get_tiling(bo, &tiling, &swizzle);
Chris Wilson6c428a62014-08-29 13:11:37 +0100640
641 memset(reloc, 0, sizeof(reloc));
642 memset(gem_exec, 0, sizeof(gem_exec));
643 memset(&execbuf, 0, sizeof(execbuf));
644
645 b = buf;
646 *b++ = XY_COLOR_BLT_CMD_NOLEN |
647 ((gen >= 8) ? 5 : 4) |
648 COLOR_BLT_WRITE_ALPHA | XY_COLOR_BLT_WRITE_RGB;
Chris Wilson86055df2014-08-29 17:36:29 +0100649 if (gen >= 4 && tiling) {
650 b[-1] |= XY_COLOR_BLT_TILED;
Chris Wilson37f4da02016-01-27 13:02:35 +0000651 *b = buffers->width;
Chris Wilson86055df2014-08-29 17:36:29 +0100652 } else
Chris Wilson37f4da02016-01-27 13:02:35 +0000653 *b = buffers->width << 2;
Chris Wilson86055df2014-08-29 17:36:29 +0100654 *b++ |= 0xf0 << 16 | 1 << 25 | 1 << 24;
Chris Wilson6c428a62014-08-29 13:11:37 +0100655 *b++ = 0;
Chris Wilson37f4da02016-01-27 13:02:35 +0000656 *b++ = buffers->height << 16 | buffers->width;
Chris Wilson6c428a62014-08-29 13:11:37 +0100657 reloc[0].offset = (b - buf) * sizeof(uint32_t);
658 reloc[0].target_handle = bo->handle;
659 reloc[0].read_domains = I915_GEM_DOMAIN_RENDER;
660 reloc[0].write_domain = I915_GEM_DOMAIN_RENDER;
661 *b++ = 0;
662 if (gen >= 8)
663 *b++ = 0;
664 *b++ = val;
665 *b++ = MI_BATCH_BUFFER_END;
666 if ((b - buf) & 1)
667 *b++ = 0;
668
669 gem_exec[0].handle = bo->handle;
670 gem_exec[0].flags = EXEC_OBJECT_NEEDS_FENCE;
671
Chris Wilson6f759902016-01-27 11:17:03 +0000672 gem_exec[1].handle = gem_create(fd, 4096);
Chris Wilson6c428a62014-08-29 13:11:37 +0100673 gem_exec[1].relocation_count = 1;
Chris Wilson4de67b22017-01-02 11:05:21 +0000674 gem_exec[1].relocs_ptr = to_user_pointer(reloc);
Chris Wilson6c428a62014-08-29 13:11:37 +0100675
Chris Wilson4de67b22017-01-02 11:05:21 +0000676 execbuf.buffers_ptr = to_user_pointer(gem_exec);
Chris Wilson6c428a62014-08-29 13:11:37 +0100677 execbuf.buffer_count = 2;
678 execbuf.batch_len = (b - buf) * sizeof(buf[0]);
Chris Wilson86055df2014-08-29 17:36:29 +0100679 if (gen >= 6)
680 execbuf.flags = I915_EXEC_BLT;
Chris Wilson6c428a62014-08-29 13:11:37 +0100681
Chris Wilson6f759902016-01-27 11:17:03 +0000682 gem_write(fd, gem_exec[1].handle, 0, buf, execbuf.batch_len);
683 gem_execbuf(fd, &execbuf);
Chris Wilson6c428a62014-08-29 13:11:37 +0100684
Chris Wilson6f759902016-01-27 11:17:03 +0000685 gem_close(fd, gem_exec[1].handle);
Chris Wilson6c428a62014-08-29 13:11:37 +0100686}
687
688static void
Chris Wilson37f4da02016-01-27 13:02:35 +0000689gpu_cmp_bo(struct buffers *b, drm_intel_bo *bo, uint32_t val)
Chris Wilson6c428a62014-08-29 13:11:37 +0100690{
Chris Wilson37f4da02016-01-27 13:02:35 +0000691 blt_copy_bo(b, b->snoop, bo);
692 cpu_cmp_bo(b, b->snoop, val);
Chris Wilson6c428a62014-08-29 13:11:37 +0100693}
694
Chris Wilsonc2248ef2016-03-19 13:10:17 +0000695struct access_mode {
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530696 const char *name;
Chris Wilsone85613b2016-03-19 14:01:38 +0000697 void (*require)(const struct create *, unsigned);
Chris Wilson4eba8e22016-03-18 10:44:31 +0000698 drm_intel_bo *(*create_bo)(const struct buffers *b);
Chris Wilson37f4da02016-01-27 13:02:35 +0000699 void (*set_bo)(struct buffers *b, drm_intel_bo *bo, uint32_t val);
700 void (*cmp_bo)(struct buffers *b, drm_intel_bo *bo, uint32_t val);
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530701 void (*release_bo)(drm_intel_bo *bo);
Daniel Vetter43779e32013-08-14 14:50:50 +0200702};
Chris Wilson59c55622014-08-29 13:11:37 +0100703igt_render_copyfunc_t rendercopy;
704
Chris Wilson5d669bf2016-03-18 14:44:53 +0000705static int read_sysctl(const char *path)
706{
707 FILE *file = fopen(path, "r");
708 int max = 0;
709 if (file) {
Chris Wilsonc46f3c32016-04-10 20:44:58 +0100710 if (fscanf(file, "%d", &max) != 1)
711 max = 0; /* silence! */
Chris Wilson5d669bf2016-03-18 14:44:53 +0000712 fclose(file);
713 }
714 return max;
715}
716
717static int write_sysctl(const char *path, int value)
718{
719 FILE *file = fopen(path, "w");
720 if (file) {
721 fprintf(file, "%d", value);
722 fclose(file);
723 }
724 return read_sysctl(path);
725}
726
727static bool set_max_map_count(int num_buffers)
728{
729 int max = read_sysctl("/proc/sys/vm/max_map_count");
730 if (max < num_buffers + 1024)
731 max = write_sysctl("/proc/sys/vm/max_map_count",
732 num_buffers + 1024);
733 return max > num_buffers;
734}
735
Chris Wilson4eba8e22016-03-18 10:44:31 +0000736static void buffers_init(struct buffers *b,
737 const char *name,
738 const struct create *create,
Chris Wilson094e0cb2016-03-01 13:22:03 +0000739 const struct access_mode *mode,
Chris Wilson5d669bf2016-03-18 14:44:53 +0000740 const struct size *size,
Chris Wilson094e0cb2016-03-01 13:22:03 +0000741 int num_buffers,
Chris Wilson094e0cb2016-03-01 13:22:03 +0000742 int _fd, int enable_reuse)
Chris Wilson99b5ee82015-01-22 10:03:45 +0000743{
Chris Wilson4eba8e22016-03-18 10:44:31 +0000744 memset(b, 0, sizeof(*b));
745 b->name = name;
746 b->create = create;
747 b->mode = mode;
Chris Wilson5d669bf2016-03-18 14:44:53 +0000748 b->size = size;
Chris Wilson4eba8e22016-03-18 10:44:31 +0000749 b->num_buffers = num_buffers;
Chris Wilson4eba8e22016-03-18 10:44:31 +0000750 b->count = 0;
Chris Wilson37f4da02016-01-27 13:02:35 +0000751
Chris Wilson5d669bf2016-03-18 14:44:53 +0000752 b->width = size->width;
753 b->height = size->height;
754 b->npixels = size->width * size->height;
Chris Wilsond199ad82016-07-27 23:19:41 +0100755 b->page_size = 4*b->npixels;
756 b->page_size = (b->page_size + 4095) & -4096;
757 b->tmp = malloc(b->page_size);
Chris Wilson4eba8e22016-03-18 10:44:31 +0000758 igt_assert(b->tmp);
Chris Wilson99b5ee82015-01-22 10:03:45 +0000759
Chris Wilson4eba8e22016-03-18 10:44:31 +0000760 b->bufmgr = drm_intel_bufmgr_gem_init(_fd, 4096);
761 igt_assert(b->bufmgr);
762
763 b->src = malloc(2*sizeof(drm_intel_bo *)*num_buffers);
764 igt_assert(b->src);
765 b->dst = b->src + num_buffers;
Chris Wilson2d08e9e2015-12-11 09:25:03 +0000766
Chris Wilsona1b47ef2016-01-27 19:44:16 +0000767 if (enable_reuse)
Chris Wilson4eba8e22016-03-18 10:44:31 +0000768 drm_intel_bufmgr_gem_enable_reuse(b->bufmgr);
769 b->batch = intel_batchbuffer_alloc(b->bufmgr, devid);
770 igt_assert(b->batch);
Chris Wilson99b5ee82015-01-22 10:03:45 +0000771}
772
Chris Wilson4eba8e22016-03-18 10:44:31 +0000773static void buffers_destroy(struct buffers *b)
Chris Wilson99b5ee82015-01-22 10:03:45 +0000774{
Chris Wilson4eba8e22016-03-18 10:44:31 +0000775 int count = b->count;
Chris Wilson094e0cb2016-03-01 13:22:03 +0000776 if (count == 0)
Chris Wilson99b5ee82015-01-22 10:03:45 +0000777 return;
778
Chris Wilsonc19b0492016-03-20 11:13:30 +0000779 /* Be safe so that we can clean up a partial creation */
Chris Wilson4eba8e22016-03-18 10:44:31 +0000780 b->count = 0;
Chris Wilsonc19b0492016-03-20 11:13:30 +0000781 for (int i = 0; i < count; i++) {
782 if (b->src[i]) {
783 b->mode->release_bo(b->src[i]);
784 b->src[i] = NULL;
785 } else
786 break;
787
788 if (b->dst[i]) {
789 b->mode->release_bo(b->dst[i]);
790 b->dst[i] = NULL;
791 }
792 }
793 if (b->snoop) {
794 nop_release_bo(b->snoop);
795 b->snoop = NULL;
796 }
797 if (b->spare) {
798 b->mode->release_bo(b->spare);
799 b->spare = NULL;
800 }
Chris Wilson99b5ee82015-01-22 10:03:45 +0000801}
802
Chris Wilson4eba8e22016-03-18 10:44:31 +0000803static void buffers_create(struct buffers *b)
Chris Wilson99b5ee82015-01-22 10:03:45 +0000804{
Chris Wilson4eba8e22016-03-18 10:44:31 +0000805 int count = b->num_buffers;
806 igt_assert(b->bufmgr);
Chris Wilson99b5ee82015-01-22 10:03:45 +0000807
Chris Wilson4eba8e22016-03-18 10:44:31 +0000808 buffers_destroy(b);
809 igt_assert(b->count == 0);
Chris Wilsonc19b0492016-03-20 11:13:30 +0000810 b->count = count;
Chris Wilson99b5ee82015-01-22 10:03:45 +0000811
812 for (int i = 0; i < count; i++) {
Chris Wilson4eba8e22016-03-18 10:44:31 +0000813 b->src[i] = b->mode->create_bo(b);
814 b->dst[i] = b->mode->create_bo(b);
Chris Wilson99b5ee82015-01-22 10:03:45 +0000815 }
Chris Wilson4eba8e22016-03-18 10:44:31 +0000816 b->spare = b->mode->create_bo(b);
817 b->snoop = snoop_create_bo(b);
Chris Wilson99b5ee82015-01-22 10:03:45 +0000818}
819
Chris Wilsonc46f3c32016-04-10 20:44:58 +0100820static void buffers_reset(struct buffers *b, bool enable_reuse)
821{
Chris Wilsonc46f3c32016-04-10 20:44:58 +0100822 b->bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
823 igt_assert(b->bufmgr);
824
825 if (enable_reuse)
826 drm_intel_bufmgr_gem_enable_reuse(b->bufmgr);
827 b->batch = intel_batchbuffer_alloc(b->bufmgr, devid);
828 igt_assert(b->batch);
829}
830
Chris Wilson4eba8e22016-03-18 10:44:31 +0000831static void buffers_fini(struct buffers *b)
Chris Wilson99b5ee82015-01-22 10:03:45 +0000832{
Chris Wilson4eba8e22016-03-18 10:44:31 +0000833 if (b->bufmgr == NULL)
Chris Wilson99b5ee82015-01-22 10:03:45 +0000834 return;
835
Chris Wilson4eba8e22016-03-18 10:44:31 +0000836 buffers_destroy(b);
Chris Wilson99b5ee82015-01-22 10:03:45 +0000837
Chris Wilson4eba8e22016-03-18 10:44:31 +0000838 free(b->tmp);
839 free(b->src);
Chris Wilson2d08e9e2015-12-11 09:25:03 +0000840
Chris Wilson4eba8e22016-03-18 10:44:31 +0000841 intel_batchbuffer_free(b->batch);
842 drm_intel_bufmgr_destroy(b->bufmgr);
Chris Wilson094e0cb2016-03-01 13:22:03 +0000843
Chris Wilson4eba8e22016-03-18 10:44:31 +0000844 memset(b, 0, sizeof(*b));
Chris Wilson99b5ee82015-01-22 10:03:45 +0000845}
846
Chris Wilson37f4da02016-01-27 13:02:35 +0000847typedef void (*do_copy)(struct buffers *b, drm_intel_bo *dst, drm_intel_bo *src);
Chris Wilson0a1fc452016-09-13 11:13:14 +0100848typedef igt_hang_t (*do_hang)(void);
Chris Wilson59c55622014-08-29 13:11:37 +0100849
Chris Wilson37f4da02016-01-27 13:02:35 +0000850static void render_copy_bo(struct buffers *b, drm_intel_bo *dst, drm_intel_bo *src)
Chris Wilson59c55622014-08-29 13:11:37 +0100851{
852 struct igt_buf d = {
853 .bo = dst,
Chris Wilson5d669bf2016-03-18 14:44:53 +0000854 .size = b->npixels * 4,
855 .num_tiles = b->npixels * 4,
Chris Wilson37f4da02016-01-27 13:02:35 +0000856 .stride = b->width * 4,
Maarten Lankhorst10c98302018-11-16 15:18:24 +0100857 .bpp = 32,
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,
Maarten Lankhorst10c98302018-11-16 15:18:24 +0100863 .bpp = 32,
Chris Wilson59c55622014-08-29 13:11:37 +0100864 };
Chris Wilson86055df2014-08-29 17:36:29 +0100865 uint32_t swizzle;
866
867 drm_intel_bo_get_tiling(dst, &d.tiling, &swizzle);
868 drm_intel_bo_get_tiling(src, &s.tiling, &swizzle);
869
Chris Wilson094e0cb2016-03-01 13:22:03 +0000870 rendercopy(b->batch, NULL,
Chris Wilson59c55622014-08-29 13:11:37 +0100871 &s, 0, 0,
Chris Wilson37f4da02016-01-27 13:02:35 +0000872 b->width, b->height,
Chris Wilson59c55622014-08-29 13:11:37 +0100873 &d, 0, 0);
874}
875
Chris Wilson37f4da02016-01-27 13:02:35 +0000876static void blt_copy_bo(struct buffers *b, drm_intel_bo *dst, drm_intel_bo *src)
Chris Wilson59c55622014-08-29 13:11:37 +0100877{
Chris Wilson094e0cb2016-03-01 13:22:03 +0000878 intel_blt_copy(b->batch,
Chris Wilson37f4da02016-01-27 13:02:35 +0000879 src, 0, 0, 4*b->width,
880 dst, 0, 0, 4*b->width,
881 b->width, b->height, 32);
Chris Wilson59c55622014-08-29 13:11:37 +0100882}
Daniel Vetter5a598c92013-08-14 15:08:05 +0200883
Chris Wilson37f4da02016-01-27 13:02:35 +0000884static void cpu_copy_bo(struct buffers *b, drm_intel_bo *dst, drm_intel_bo *src)
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530885{
Chris Wilsond199ad82016-07-27 23:19:41 +0100886 const int size = b->page_size;
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530887 void *d, *s;
888
889 gem_set_domain(fd, src->handle, I915_GEM_DOMAIN_CPU, 0);
890 gem_set_domain(fd, dst->handle, I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
Ville Syrjäläf52e7ec2015-10-09 19:11:39 +0300891 s = gem_mmap__cpu(fd, src->handle, 0, size, PROT_READ);
892 d = gem_mmap__cpu(fd, dst->handle, 0, size, PROT_WRITE);
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530893
894 memcpy(d, s, size);
895
896 munmap(d, size);
897 munmap(s, size);
898}
899
Chris Wilson37f4da02016-01-27 13:02:35 +0000900static void gtt_copy_bo(struct buffers *b, drm_intel_bo *dst, drm_intel_bo *src)
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530901{
Chris Wilsond199ad82016-07-27 23:19:41 +0100902 const int size = b->page_size;
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530903 void *d, *s;
904
905 gem_set_domain(fd, src->handle, I915_GEM_DOMAIN_GTT, 0);
906 gem_set_domain(fd, dst->handle, I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
907
Ville Syrjäläf52e7ec2015-10-09 19:11:39 +0300908 s = gem_mmap__gtt(fd, src->handle, size, PROT_READ);
909 d = gem_mmap__gtt(fd, dst->handle, size, PROT_WRITE);
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530910
911 memcpy(d, s, size);
912
913 munmap(d, size);
914 munmap(s, size);
915}
916
Chris Wilson37f4da02016-01-27 13:02:35 +0000917static void wc_copy_bo(struct buffers *b, drm_intel_bo *dst, drm_intel_bo *src)
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530918{
Chris Wilsond199ad82016-07-27 23:19:41 +0100919 const int size = b->page_size;
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530920 void *d, *s;
921
Chris Wilsone9cd7d52017-04-13 10:08:09 +0100922 gem_set_domain(fd, src->handle, I915_GEM_DOMAIN_WC, 0);
923 gem_set_domain(fd, dst->handle, I915_GEM_DOMAIN_WC, I915_GEM_DOMAIN_WC);
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530924
Ville Syrjäläf52e7ec2015-10-09 19:11:39 +0300925 s = gem_mmap__wc(fd, src->handle, 0, size, PROT_READ);
926 d = gem_mmap__wc(fd, dst->handle, 0, size, PROT_WRITE);
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530927
928 memcpy(d, s, size);
929
930 munmap(d, size);
931 munmap(s, size);
932}
933
Chris Wilson0a1fc452016-09-13 11:13:14 +0100934static igt_hang_t no_hang(void)
Chris Wilson16bafdf2014-09-04 09:26:24 +0100935{
Chris Wilson0a1fc452016-09-13 11:13:14 +0100936 return (igt_hang_t){0, 0};
Chris Wilson16bafdf2014-09-04 09:26:24 +0100937}
938
Chris Wilson0a1fc452016-09-13 11:13:14 +0100939static igt_hang_t bcs_hang(void)
Chris Wilson16bafdf2014-09-04 09:26:24 +0100940{
Daniel Vetter3cd45de2015-02-10 17:46:43 +0100941 return igt_hang_ring(fd, I915_EXEC_BLT);
Chris Wilson16bafdf2014-09-04 09:26:24 +0100942}
943
Chris Wilson0a1fc452016-09-13 11:13:14 +0100944static igt_hang_t rcs_hang(void)
Chris Wilson16bafdf2014-09-04 09:26:24 +0100945{
Daniel Vetter3cd45de2015-02-10 17:46:43 +0100946 return igt_hang_ring(fd, I915_EXEC_RENDER);
Chris Wilson16bafdf2014-09-04 09:26:24 +0100947}
948
Chris Wilson0a1fc452016-09-13 11:13:14 +0100949static igt_hang_t all_hang(void)
Chris Wilsonfcb4cfa2016-03-20 11:38:26 +0000950{
Antonio Argenziano140a67c2018-07-10 16:45:26 -0700951 igt_hang_t hang = igt_hang_ring(fd, I915_EXEC_RENDER);
Chris Wilsonfcb4cfa2016-03-20 11:38:26 +0000952 unsigned engine;
953
Chris Wilson305ebce2018-02-21 14:13:16 +0000954 for_each_physical_engine(fd, engine) {
Antonio Argenziano140a67c2018-07-10 16:45:26 -0700955 struct drm_i915_gem_execbuffer2 eb = hang.spin->execbuf;
Chris Wilsonfcb4cfa2016-03-20 11:38:26 +0000956
Antonio Argenziano140a67c2018-07-10 16:45:26 -0700957 if (engine == I915_EXEC_RENDER)
958 continue;
Chris Wilsonfcb4cfa2016-03-20 11:38:26 +0000959
Antonio Argenziano140a67c2018-07-10 16:45:26 -0700960 eb.flags = engine;
961 __gem_execbuf(fd, &eb);
Chris Wilsonfcb4cfa2016-03-20 11:38:26 +0000962 }
963
Chris Wilsonfcb4cfa2016-03-20 11:38:26 +0000964 return hang;
965}
966
Chris Wilson8bf09f32015-12-17 09:16:42 +0000967static void do_basic0(struct buffers *buffers,
968 do_copy do_copy_func,
969 do_hang do_hang_func)
970{
Chris Wilson37f4da02016-01-27 13:02:35 +0000971 buffers->mode->set_bo(buffers, buffers->src[0], 0xdeadbeef);
Chris Wilson8bf09f32015-12-17 09:16:42 +0000972 for (int i = 0; i < buffers->count; i++) {
Chris Wilson0a1fc452016-09-13 11:13:14 +0100973 igt_hang_t hang = do_hang_func();
Chris Wilson8bf09f32015-12-17 09:16:42 +0000974
Chris Wilson37f4da02016-01-27 13:02:35 +0000975 do_copy_func(buffers, buffers->dst[i], buffers->src[0]);
976 buffers->mode->cmp_bo(buffers, buffers->dst[i], 0xdeadbeef);
Chris Wilson8bf09f32015-12-17 09:16:42 +0000977
978 igt_post_hang_ring(fd, hang);
979 }
980}
981
982static void do_basic1(struct buffers *buffers,
983 do_copy do_copy_func,
984 do_hang do_hang_func)
Chris Wilson197db862015-12-09 20:54:10 +0000985{
Chris Wilson197db862015-12-09 20:54:10 +0000986 for (int i = 0; i < buffers->count; i++) {
Chris Wilson0a1fc452016-09-13 11:13:14 +0100987 igt_hang_t hang = do_hang_func();
Chris Wilson197db862015-12-09 20:54:10 +0000988
Chris Wilson37f4da02016-01-27 13:02:35 +0000989 buffers->mode->set_bo(buffers, buffers->src[i], i);
990 buffers->mode->set_bo(buffers, buffers->dst[i], ~i);
Chris Wilson8bf09f32015-12-17 09:16:42 +0000991
Chris Wilson37f4da02016-01-27 13:02:35 +0000992 do_copy_func(buffers, buffers->dst[i], buffers->src[i]);
Chris Wilson8bf09f32015-12-17 09:16:42 +0000993 usleep(0); /* let someone else claim the mutex */
Chris Wilson37f4da02016-01-27 13:02:35 +0000994 buffers->mode->cmp_bo(buffers, buffers->dst[i], i);
Chris Wilson197db862015-12-09 20:54:10 +0000995
996 igt_post_hang_ring(fd, hang);
997 }
998}
999
Chris Wilson8bf09f32015-12-17 09:16:42 +00001000static void do_basicN(struct buffers *buffers,
1001 do_copy do_copy_func,
1002 do_hang do_hang_func)
1003{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001004 igt_hang_t hang;
Chris Wilson8bf09f32015-12-17 09:16:42 +00001005
Chris Wilson8bf09f32015-12-17 09:16:42 +00001006 for (int i = 0; i < buffers->count; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001007 buffers->mode->set_bo(buffers, buffers->src[i], i);
1008 buffers->mode->set_bo(buffers, buffers->dst[i], ~i);
Chris Wilson8bf09f32015-12-17 09:16:42 +00001009 }
1010
1011 hang = do_hang_func();
1012
1013 for (int i = 0; i < buffers->count; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001014 do_copy_func(buffers, buffers->dst[i], buffers->src[i]);
Chris Wilson8bf09f32015-12-17 09:16:42 +00001015 usleep(0); /* let someone else claim the mutex */
1016 }
1017
1018 for (int i = 0; i < buffers->count; i++)
Chris Wilson37f4da02016-01-27 13:02:35 +00001019 buffers->mode->cmp_bo(buffers, buffers->dst[i], i);
Chris Wilson8bf09f32015-12-17 09:16:42 +00001020
1021 igt_post_hang_ring(fd, hang);
1022}
1023
Chris Wilson99b5ee82015-01-22 10:03:45 +00001024static void do_overwrite_source(struct buffers *buffers,
Chris Wilson16bafdf2014-09-04 09:26:24 +01001025 do_copy do_copy_func,
1026 do_hang do_hang_func)
Daniel Vetter5a598c92013-08-14 15:08:05 +02001027{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001028 igt_hang_t hang;
Daniel Vetter5a598c92013-08-14 15:08:05 +02001029 int i;
1030
Chris Wilson99b5ee82015-01-22 10:03:45 +00001031 for (i = 0; i < buffers->count; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001032 buffers->mode->set_bo(buffers, buffers->src[i], i);
1033 buffers->mode->set_bo(buffers, buffers->dst[i], ~i);
Daniel Vetter5a598c92013-08-14 15:08:05 +02001034 }
Chris Wilson99b5ee82015-01-22 10:03:45 +00001035 for (i = 0; i < buffers->count; i++)
Chris Wilson37f4da02016-01-27 13:02:35 +00001036 do_copy_func(buffers, buffers->dst[i], buffers->src[i]);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001037 hang = do_hang_func();
Chris Wilson99b5ee82015-01-22 10:03:45 +00001038 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001039 buffers->mode->set_bo(buffers, buffers->src[i], 0xdeadbeef);
Chris Wilson99b5ee82015-01-22 10:03:45 +00001040 for (i = 0; i < buffers->count; i++)
Chris Wilson37f4da02016-01-27 13:02:35 +00001041 buffers->mode->cmp_bo(buffers, buffers->dst[i], i);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001042 igt_post_hang_ring(fd, hang);
1043}
1044
Chris Wilsona1726762015-03-16 16:29:57 +00001045static void do_overwrite_source_read(struct buffers *buffers,
1046 do_copy do_copy_func,
1047 do_hang do_hang_func,
1048 int do_rcs)
1049{
1050 const int half = buffers->count/2;
Chris Wilson0a1fc452016-09-13 11:13:14 +01001051 igt_hang_t hang;
Chris Wilsona1726762015-03-16 16:29:57 +00001052 int i;
1053
Chris Wilsona1726762015-03-16 16:29:57 +00001054 for (i = 0; i < half; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001055 buffers->mode->set_bo(buffers, buffers->src[i], i);
1056 buffers->mode->set_bo(buffers, buffers->dst[i], ~i);
1057 buffers->mode->set_bo(buffers, buffers->dst[i+half], ~i);
Chris Wilsona1726762015-03-16 16:29:57 +00001058 }
1059 for (i = 0; i < half; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001060 do_copy_func(buffers, buffers->dst[i], buffers->src[i]);
Chris Wilsona1726762015-03-16 16:29:57 +00001061 if (do_rcs)
Chris Wilson37f4da02016-01-27 13:02:35 +00001062 render_copy_bo(buffers, buffers->dst[i+half], buffers->src[i]);
Chris Wilsona1726762015-03-16 16:29:57 +00001063 else
Chris Wilson37f4da02016-01-27 13:02:35 +00001064 blt_copy_bo(buffers, buffers->dst[i+half], buffers->src[i]);
Chris Wilsona1726762015-03-16 16:29:57 +00001065 }
1066 hang = do_hang_func();
1067 for (i = half; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001068 buffers->mode->set_bo(buffers, buffers->src[i], 0xdeadbeef);
Chris Wilsona1726762015-03-16 16:29:57 +00001069 for (i = 0; i < half; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001070 buffers->mode->cmp_bo(buffers, buffers->dst[i], i);
1071 buffers->mode->cmp_bo(buffers, buffers->dst[i+half], i);
Chris Wilsona1726762015-03-16 16:29:57 +00001072 }
1073 igt_post_hang_ring(fd, hang);
1074}
1075
1076static void do_overwrite_source_read_bcs(struct buffers *buffers,
1077 do_copy do_copy_func,
1078 do_hang do_hang_func)
1079{
1080 do_overwrite_source_read(buffers, do_copy_func, do_hang_func, 0);
1081}
1082
1083static void do_overwrite_source_read_rcs(struct buffers *buffers,
1084 do_copy do_copy_func,
1085 do_hang do_hang_func)
1086{
1087 do_overwrite_source_read(buffers, do_copy_func, do_hang_func, 1);
1088}
1089
Chris Wilson99b5ee82015-01-22 10:03:45 +00001090static void do_overwrite_source__rev(struct buffers *buffers,
Chris Wilson16bafdf2014-09-04 09:26:24 +01001091 do_copy do_copy_func,
1092 do_hang do_hang_func)
1093{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001094 igt_hang_t hang;
Chris Wilson16bafdf2014-09-04 09:26:24 +01001095 int i;
1096
Chris Wilson99b5ee82015-01-22 10:03:45 +00001097 for (i = 0; i < buffers->count; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001098 buffers->mode->set_bo(buffers, buffers->src[i], i);
1099 buffers->mode->set_bo(buffers, buffers->dst[i], ~i);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001100 }
Chris Wilson99b5ee82015-01-22 10:03:45 +00001101 for (i = 0; i < buffers->count; i++)
Chris Wilson37f4da02016-01-27 13:02:35 +00001102 do_copy_func(buffers, buffers->dst[i], buffers->src[i]);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001103 hang = do_hang_func();
Chris Wilson99b5ee82015-01-22 10:03:45 +00001104 for (i = 0; i < buffers->count; i++)
Chris Wilson37f4da02016-01-27 13:02:35 +00001105 buffers->mode->set_bo(buffers, buffers->src[i], 0xdeadbeef);
Chris Wilson99b5ee82015-01-22 10:03:45 +00001106 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001107 buffers->mode->cmp_bo(buffers, buffers->dst[i], i);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001108 igt_post_hang_ring(fd, hang);
1109}
1110
Chris Wilson99b5ee82015-01-22 10:03:45 +00001111static void do_overwrite_source__one(struct buffers *buffers,
Chris Wilson16bafdf2014-09-04 09:26:24 +01001112 do_copy do_copy_func,
1113 do_hang do_hang_func)
1114{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001115 igt_hang_t hang;
Chris Wilson16bafdf2014-09-04 09:26:24 +01001116
Chris Wilson37f4da02016-01-27 13:02:35 +00001117 buffers->mode->set_bo(buffers, buffers->src[0], 0);
1118 buffers->mode->set_bo(buffers, buffers->dst[0], ~0);
1119 do_copy_func(buffers, buffers->dst[0], buffers->src[0]);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001120 hang = do_hang_func();
Chris Wilson37f4da02016-01-27 13:02:35 +00001121 buffers->mode->set_bo(buffers, buffers->src[0], 0xdeadbeef);
1122 buffers->mode->cmp_bo(buffers, buffers->dst[0], 0);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001123 igt_post_hang_ring(fd, hang);
Daniel Vetter5a598c92013-08-14 15:08:05 +02001124}
1125
Chris Wilsona72d4052015-03-18 14:15:22 +00001126static void do_intermix(struct buffers *buffers,
1127 do_copy do_copy_func,
1128 do_hang do_hang_func,
1129 int do_rcs)
1130{
1131 const int half = buffers->count/2;
Chris Wilson0a1fc452016-09-13 11:13:14 +01001132 igt_hang_t hang;
Chris Wilsona72d4052015-03-18 14:15:22 +00001133 int i;
1134
Chris Wilsona72d4052015-03-18 14:15:22 +00001135 for (i = 0; i < buffers->count; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001136 buffers->mode->set_bo(buffers, buffers->src[i], 0xdeadbeef^~i);
1137 buffers->mode->set_bo(buffers, buffers->dst[i], i);
Chris Wilsona72d4052015-03-18 14:15:22 +00001138 }
1139 for (i = 0; i < half; i++) {
1140 if (do_rcs == 1 || (do_rcs == -1 && i & 1))
Chris Wilson37f4da02016-01-27 13:02:35 +00001141 render_copy_bo(buffers, buffers->dst[i], buffers->src[i]);
Chris Wilsona72d4052015-03-18 14:15:22 +00001142 else
Chris Wilson37f4da02016-01-27 13:02:35 +00001143 blt_copy_bo(buffers, buffers->dst[i], buffers->src[i]);
Chris Wilsona72d4052015-03-18 14:15:22 +00001144
Chris Wilson37f4da02016-01-27 13:02:35 +00001145 do_copy_func(buffers, buffers->dst[i+half], buffers->src[i]);
Chris Wilsona72d4052015-03-18 14:15:22 +00001146
1147 if (do_rcs == 1 || (do_rcs == -1 && (i & 1) == 0))
Chris Wilson37f4da02016-01-27 13:02:35 +00001148 render_copy_bo(buffers, buffers->dst[i], buffers->dst[i+half]);
Chris Wilsona72d4052015-03-18 14:15:22 +00001149 else
Chris Wilson37f4da02016-01-27 13:02:35 +00001150 blt_copy_bo(buffers, buffers->dst[i], buffers->dst[i+half]);
Chris Wilsona72d4052015-03-18 14:15:22 +00001151
Chris Wilson37f4da02016-01-27 13:02:35 +00001152 do_copy_func(buffers, buffers->dst[i+half], buffers->src[i+half]);
Chris Wilsona72d4052015-03-18 14:15:22 +00001153 }
1154 hang = do_hang_func();
1155 for (i = 0; i < 2*half; i++)
Chris Wilson37f4da02016-01-27 13:02:35 +00001156 buffers->mode->cmp_bo(buffers, buffers->dst[i], 0xdeadbeef^~i);
Chris Wilsona72d4052015-03-18 14:15:22 +00001157 igt_post_hang_ring(fd, hang);
1158}
1159
1160static void do_intermix_rcs(struct buffers *buffers,
1161 do_copy do_copy_func,
1162 do_hang do_hang_func)
1163{
1164 do_intermix(buffers, do_copy_func, do_hang_func, 1);
1165}
1166
1167static void do_intermix_bcs(struct buffers *buffers,
1168 do_copy do_copy_func,
1169 do_hang do_hang_func)
1170{
1171 do_intermix(buffers, do_copy_func, do_hang_func, 0);
1172}
1173
1174static void do_intermix_both(struct buffers *buffers,
1175 do_copy do_copy_func,
1176 do_hang do_hang_func)
1177{
1178 do_intermix(buffers, do_copy_func, do_hang_func, -1);
1179}
1180
Chris Wilson99b5ee82015-01-22 10:03:45 +00001181static void do_early_read(struct buffers *buffers,
Chris Wilson16bafdf2014-09-04 09:26:24 +01001182 do_copy do_copy_func,
1183 do_hang do_hang_func)
Daniel Vetter5a598c92013-08-14 15:08:05 +02001184{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001185 igt_hang_t hang;
Daniel Vetter5a598c92013-08-14 15:08:05 +02001186 int i;
1187
Chris Wilson99b5ee82015-01-22 10:03:45 +00001188 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001189 buffers->mode->set_bo(buffers, buffers->src[i], 0xdeadbeef);
Chris Wilson99b5ee82015-01-22 10:03:45 +00001190 for (i = 0; i < buffers->count; i++)
Chris Wilson37f4da02016-01-27 13:02:35 +00001191 do_copy_func(buffers, buffers->dst[i], buffers->src[i]);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001192 hang = do_hang_func();
Chris Wilson99b5ee82015-01-22 10:03:45 +00001193 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001194 buffers->mode->cmp_bo(buffers, buffers->dst[i], 0xdeadbeef);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001195 igt_post_hang_ring(fd, hang);
Daniel Vetter5a598c92013-08-14 15:08:05 +02001196}
1197
Chris Wilson35b0ac92015-03-16 11:55:46 +00001198static void do_read_read_bcs(struct buffers *buffers,
1199 do_copy do_copy_func,
1200 do_hang do_hang_func)
1201{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001202 igt_hang_t hang;
Chris Wilson35b0ac92015-03-16 11:55:46 +00001203 int i;
1204
Chris Wilson35b0ac92015-03-16 11:55:46 +00001205 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001206 buffers->mode->set_bo(buffers, buffers->src[i], 0xdeadbeef ^ i);
Chris Wilson35b0ac92015-03-16 11:55:46 +00001207 for (i = 0; i < buffers->count; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001208 do_copy_func(buffers, buffers->dst[i], buffers->src[i]);
1209 blt_copy_bo(buffers, buffers->spare, buffers->src[i]);
Chris Wilson35b0ac92015-03-16 11:55:46 +00001210 }
Chris Wilson37f4da02016-01-27 13:02:35 +00001211 buffers->mode->cmp_bo(buffers, buffers->spare, 0xdeadbeef^(buffers->count-1));
Chris Wilson35b0ac92015-03-16 11:55:46 +00001212 hang = do_hang_func();
1213 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001214 buffers->mode->cmp_bo(buffers, buffers->dst[i], 0xdeadbeef ^ i);
Chris Wilson35b0ac92015-03-16 11:55:46 +00001215 igt_post_hang_ring(fd, hang);
1216}
1217
Chris Wilson0c266522015-11-11 16:37:16 +00001218static void do_write_read_bcs(struct buffers *buffers,
1219 do_copy do_copy_func,
1220 do_hang do_hang_func)
1221{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001222 igt_hang_t hang;
Chris Wilson0c266522015-11-11 16:37:16 +00001223 int i;
1224
Chris Wilson0c266522015-11-11 16:37:16 +00001225 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001226 buffers->mode->set_bo(buffers, buffers->src[i], 0xdeadbeef ^ i);
Chris Wilson0c266522015-11-11 16:37:16 +00001227 for (i = 0; i < buffers->count; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001228 blt_copy_bo(buffers, buffers->spare, buffers->src[i]);
1229 do_copy_func(buffers, buffers->dst[i], buffers->spare);
Chris Wilson0c266522015-11-11 16:37:16 +00001230 }
1231 hang = do_hang_func();
1232 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001233 buffers->mode->cmp_bo(buffers, buffers->dst[i], 0xdeadbeef ^ i);
Chris Wilson0c266522015-11-11 16:37:16 +00001234 igt_post_hang_ring(fd, hang);
1235}
1236
Chris Wilson35b0ac92015-03-16 11:55:46 +00001237static void do_read_read_rcs(struct buffers *buffers,
1238 do_copy do_copy_func,
1239 do_hang do_hang_func)
1240{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001241 igt_hang_t hang;
Chris Wilson35b0ac92015-03-16 11:55:46 +00001242 int i;
1243
Chris Wilson35b0ac92015-03-16 11:55:46 +00001244 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001245 buffers->mode->set_bo(buffers, buffers->src[i], 0xdeadbeef ^ i);
Chris Wilson35b0ac92015-03-16 11:55:46 +00001246 for (i = 0; i < buffers->count; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001247 do_copy_func(buffers, buffers->dst[i], buffers->src[i]);
1248 render_copy_bo(buffers, buffers->spare, buffers->src[i]);
Chris Wilson35b0ac92015-03-16 11:55:46 +00001249 }
Chris Wilson37f4da02016-01-27 13:02:35 +00001250 buffers->mode->cmp_bo(buffers, buffers->spare, 0xdeadbeef^(buffers->count-1));
Chris Wilson35b0ac92015-03-16 11:55:46 +00001251 hang = do_hang_func();
1252 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001253 buffers->mode->cmp_bo(buffers, buffers->dst[i], 0xdeadbeef ^ i);
Chris Wilson35b0ac92015-03-16 11:55:46 +00001254 igt_post_hang_ring(fd, hang);
1255}
1256
Chris Wilson0c266522015-11-11 16:37:16 +00001257static void do_write_read_rcs(struct buffers *buffers,
1258 do_copy do_copy_func,
1259 do_hang do_hang_func)
1260{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001261 igt_hang_t hang;
Chris Wilson0c266522015-11-11 16:37:16 +00001262 int i;
1263
Chris Wilson0c266522015-11-11 16:37:16 +00001264 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001265 buffers->mode->set_bo(buffers, buffers->src[i], 0xdeadbeef ^ i);
Chris Wilson0c266522015-11-11 16:37:16 +00001266 for (i = 0; i < buffers->count; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001267 render_copy_bo(buffers, buffers->spare, buffers->src[i]);
1268 do_copy_func(buffers, buffers->dst[i], buffers->spare);
Chris Wilson0c266522015-11-11 16:37:16 +00001269 }
1270 hang = do_hang_func();
1271 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001272 buffers->mode->cmp_bo(buffers, buffers->dst[i], 0xdeadbeef ^ i);
Chris Wilson0c266522015-11-11 16:37:16 +00001273 igt_post_hang_ring(fd, hang);
1274}
1275
Chris Wilson99b5ee82015-01-22 10:03:45 +00001276static void do_gpu_read_after_write(struct buffers *buffers,
Chris Wilson16bafdf2014-09-04 09:26:24 +01001277 do_copy do_copy_func,
1278 do_hang do_hang_func)
Daniel Vetter5a598c92013-08-14 15:08:05 +02001279{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001280 igt_hang_t hang;
Daniel Vetter5a598c92013-08-14 15:08:05 +02001281 int i;
1282
Chris Wilson99b5ee82015-01-22 10:03:45 +00001283 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001284 buffers->mode->set_bo(buffers, buffers->src[i], 0xabcdabcd);
Chris Wilson99b5ee82015-01-22 10:03:45 +00001285 for (i = 0; i < buffers->count; i++)
Chris Wilson37f4da02016-01-27 13:02:35 +00001286 do_copy_func(buffers, buffers->dst[i], buffers->src[i]);
Chris Wilson99b5ee82015-01-22 10:03:45 +00001287 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001288 do_copy_func(buffers, buffers->spare, buffers->dst[i]);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001289 hang = do_hang_func();
Chris Wilson99b5ee82015-01-22 10:03:45 +00001290 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001291 buffers->mode->cmp_bo(buffers, buffers->dst[i], 0xabcdabcd);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001292 igt_post_hang_ring(fd, hang);
Daniel Vetter5a598c92013-08-14 15:08:05 +02001293}
1294
Chris Wilson99b5ee82015-01-22 10:03:45 +00001295typedef void (*do_test)(struct buffers *buffers,
Chris Wilson16bafdf2014-09-04 09:26:24 +01001296 do_copy do_copy_func,
1297 do_hang do_hang_func);
Daniel Vetterec283d62013-08-14 15:18:37 +02001298
Chris Wilson99b5ee82015-01-22 10:03:45 +00001299typedef void (*run_wrap)(struct buffers *buffers,
Chris Wilson59c55622014-08-29 13:11:37 +01001300 do_test do_test_func,
Chris Wilson16bafdf2014-09-04 09:26:24 +01001301 do_copy do_copy_func,
1302 do_hang do_hang_func);
Daniel Vetterec283d62013-08-14 15:18:37 +02001303
Chris Wilson99b5ee82015-01-22 10:03:45 +00001304static void run_single(struct buffers *buffers,
Chris Wilson59c55622014-08-29 13:11:37 +01001305 do_test do_test_func,
Chris Wilson16bafdf2014-09-04 09:26:24 +01001306 do_copy do_copy_func,
1307 do_hang do_hang_func)
Daniel Vetterec283d62013-08-14 15:18:37 +02001308{
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001309 pass = 0;
Chris Wilson99b5ee82015-01-22 10:03:45 +00001310 do_test_func(buffers, do_copy_func, do_hang_func);
Chris Wilson5b675f72016-01-22 17:33:40 +00001311 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
Daniel Vetterec283d62013-08-14 15:18:37 +02001312}
1313
Chris Wilson99b5ee82015-01-22 10:03:45 +00001314static void run_interruptible(struct buffers *buffers,
Chris Wilson59c55622014-08-29 13:11:37 +01001315 do_test do_test_func,
Chris Wilson16bafdf2014-09-04 09:26:24 +01001316 do_copy do_copy_func,
1317 do_hang do_hang_func)
Daniel Vetterec283d62013-08-14 15:18:37 +02001318{
Chris Wilson1c61c0f2016-01-08 10:51:09 +00001319 pass = 0;
Daniel Vetterd7050f92016-05-11 17:06:28 +02001320 igt_while_interruptible(true)
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001321 do_test_func(buffers, do_copy_func, do_hang_func);
Chris Wilson5b675f72016-01-22 17:33:40 +00001322 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
Daniel Vetterec283d62013-08-14 15:18:37 +02001323}
1324
Chris Wilson46456302016-01-22 19:29:07 +00001325static void run_child(struct buffers *buffers,
1326 do_test do_test_func,
1327 do_copy do_copy_func,
1328 do_hang do_hang_func)
1329
1330{
Chris Wilson69ecede2016-01-22 22:14:33 +00001331 /* We inherit the buffers from the parent, but the bufmgr/batch
1332 * needs to be local as the cache of reusable itself will be COWed,
1333 * leading to the child closing an object without the parent knowing.
1334 */
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001335 pass = 0;
Chris Wilsona1b47ef2016-01-27 19:44:16 +00001336 igt_fork(child, 1)
Chris Wilson46456302016-01-22 19:29:07 +00001337 do_test_func(buffers, do_copy_func, do_hang_func);
Chris Wilson46456302016-01-22 19:29:07 +00001338 igt_waitchildren();
1339 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
1340}
1341
Chris Wilson2d08e9e2015-12-11 09:25:03 +00001342static void __run_forked(struct buffers *buffers,
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001343 int num_children, int loops, bool interrupt,
Chris Wilson2d08e9e2015-12-11 09:25:03 +00001344 do_test do_test_func,
1345 do_copy do_copy_func,
1346 do_hang do_hang_func)
1347
Daniel Vetterec283d62013-08-14 15:18:37 +02001348{
Chris Wilsonc46f3c32016-04-10 20:44:58 +01001349 /* purge the libdrm caches before cloing the process */
Chris Wilsonbca3ab22016-09-23 07:39:57 +01001350 buffers_destroy(buffers);
1351 intel_batchbuffer_free(buffers->batch);
1352 drm_intel_bufmgr_destroy(buffers->bufmgr);
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001353
Chris Wilson2d08e9e2015-12-11 09:25:03 +00001354 igt_fork(child, num_children) {
Chris Wilson459ff6b2016-04-20 07:49:02 +01001355 int num_buffers;
1356
Daniel Vettercd1f2202013-08-29 10:06:51 +02001357 /* recreate process local variables */
Micah Fedkec81d2932015-07-22 21:54:02 +00001358 fd = drm_open_driver(DRIVER_INTEL);
Chris Wilsonf2a045f2015-01-02 16:33:33 +05301359
Chris Wilson459ff6b2016-04-20 07:49:02 +01001360 num_buffers = buffers->num_buffers / num_children;
1361 num_buffers += MIN_BUFFERS;
1362 if (num_buffers < buffers->num_buffers)
1363 buffers->num_buffers = num_buffers;
Chris Wilsonc46f3c32016-04-10 20:44:58 +01001364
1365 buffers_reset(buffers, true);
Chris Wilson094e0cb2016-03-01 13:22:03 +00001366 buffers_create(buffers);
Daniel Vettercd1f2202013-08-29 10:06:51 +02001367
Daniel Vetterd7050f92016-05-11 17:06:28 +02001368 igt_while_interruptible(interrupt) {
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001369 for (pass = 0; pass < loops; pass++)
1370 do_test_func(buffers,
1371 do_copy_func,
1372 do_hang_func);
1373 }
1374 }
Daniel Vettercd1f2202013-08-29 10:06:51 +02001375 igt_waitchildren();
Chris Wilson5b675f72016-01-22 17:33:40 +00001376 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
Chris Wilsonbca3ab22016-09-23 07:39:57 +01001377
1378 buffers_reset(buffers, true);
Daniel Vetterec283d62013-08-14 15:18:37 +02001379}
Daniel Vetter5a598c92013-08-14 15:08:05 +02001380
Chris Wilson2d08e9e2015-12-11 09:25:03 +00001381static void run_forked(struct buffers *buffers,
1382 do_test do_test_func,
1383 do_copy do_copy_func,
1384 do_hang do_hang_func)
1385{
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001386 const int ncpus = sysconf(_SC_NPROCESSORS_ONLN);
1387 __run_forked(buffers, ncpus, ncpus, false,
Chris Wilson2d08e9e2015-12-11 09:25:03 +00001388 do_test_func, do_copy_func, do_hang_func);
1389}
1390
1391static void run_bomb(struct buffers *buffers,
1392 do_test do_test_func,
1393 do_copy do_copy_func,
1394 do_hang do_hang_func)
1395{
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001396 const int ncpus = sysconf(_SC_NPROCESSORS_ONLN);
1397 __run_forked(buffers, 8*ncpus, 2, true,
Chris Wilson2d08e9e2015-12-11 09:25:03 +00001398 do_test_func, do_copy_func, do_hang_func);
1399}
1400
Chris Wilsonf2a045f2015-01-02 16:33:33 +05301401static void cpu_require(void)
1402{
1403 bit17_require();
1404}
1405
1406static void gtt_require(void)
1407{
1408}
1409
Chris Wilson08188752014-09-03 13:38:30 +01001410static void bcs_require(void)
1411{
1412}
1413
1414static void rcs_require(void)
1415{
1416 igt_require(rendercopy);
1417}
1418
Daniel Vetter5a598c92013-08-14 15:08:05 +02001419static void
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001420run_mode(const char *prefix,
1421 const struct create *create,
1422 const struct access_mode *mode,
1423 const struct size *size,
1424 const int num_buffers,
1425 const char *suffix,
1426 run_wrap run_wrap_func)
Daniel Vetter5a598c92013-08-14 15:08:05 +02001427{
Chris Wilsonf2a045f2015-01-02 16:33:33 +05301428 const struct {
Chris Wilson59c55622014-08-29 13:11:37 +01001429 const char *prefix;
1430 do_copy copy;
Chris Wilson08188752014-09-03 13:38:30 +01001431 void (*require)(void);
Chris Wilson59c55622014-08-29 13:11:37 +01001432 } pipelines[] = {
Chris Wilsonf2a045f2015-01-02 16:33:33 +05301433 { "cpu", cpu_copy_bo, cpu_require },
1434 { "gtt", gtt_copy_bo, gtt_require },
1435 { "wc", wc_copy_bo, wc_require },
Daniel Vetter3e9b4e32015-02-06 23:10:26 +01001436 { "blt", blt_copy_bo, bcs_require },
1437 { "render", render_copy_bo, rcs_require },
Chris Wilson59c55622014-08-29 13:11:37 +01001438 { NULL, NULL }
Chris Wilson77633492015-03-26 08:11:43 +00001439 }, *pskip = pipelines + 3, *p;
Chris Wilson16bafdf2014-09-04 09:26:24 +01001440 const struct {
1441 const char *suffix;
1442 do_hang hang;
Chris Wilson16bafdf2014-09-04 09:26:24 +01001443 } hangs[] = {
Chris Wilson92caf132015-12-16 09:23:56 +00001444 { "", no_hang },
1445 { "-hang-blt", bcs_hang },
1446 { "-hang-render", rcs_hang },
Chris Wilsonfcb4cfa2016-03-20 11:38:26 +00001447 { "-hang-all", all_hang },
Chris Wilson16bafdf2014-09-04 09:26:24 +01001448 { NULL, NULL },
1449 }, *h;
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001450 struct buffers buffers;
1451
1452 igt_fixture
1453 buffers_init(&buffers, prefix, create, mode,
1454 size, num_buffers,
1455 fd, run_wrap_func != run_child);
Daniel Vetter5a598c92013-08-14 15:08:05 +02001456
Chris Wilson16bafdf2014-09-04 09:26:24 +01001457 for (h = hangs; h->suffix; h++) {
Chris Wilson77633492015-03-26 08:11:43 +00001458 if (!all && *h->suffix)
1459 continue;
1460
Chris Wilson6867b872016-03-24 07:57:30 +00001461 if (!*h->suffix)
Daniel Vetterbe21fc02016-06-17 16:04:09 +02001462 igt_fixture
1463 igt_fork_hang_detector(fd);
Chris Wilson6867b872016-03-24 07:57:30 +00001464
Chris Wilson77633492015-03-26 08:11:43 +00001465 for (p = all ? pipelines : pskip; p->prefix; p++) {
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001466 igt_subtest_group {
1467 igt_fixture p->require();
Chris Wilson16bafdf2014-09-04 09:26:24 +01001468
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001469 igt_subtest_f("%s-%s-%s-sanitycheck0%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1470 buffers_create(&buffers);
1471 run_wrap_func(&buffers, do_basic0,
1472 p->copy, h->hang);
1473 }
Chris Wilson8bf09f32015-12-17 09:16:42 +00001474
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001475 igt_subtest_f("%s-%s-%s-sanitycheck1%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1476 buffers_create(&buffers);
1477 run_wrap_func(&buffers, do_basic1,
1478 p->copy, h->hang);
1479 }
Chris Wilson8bf09f32015-12-17 09:16:42 +00001480
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001481 igt_subtest_f("%s-%s-%s-sanitycheckN%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1482 buffers_create(&buffers);
1483 run_wrap_func(&buffers, do_basicN,
1484 p->copy, h->hang);
1485 }
Chris Wilson197db862015-12-09 20:54:10 +00001486
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001487 /* try to overwrite the source values */
1488 igt_subtest_f("%s-%s-%s-overwrite-source-one%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1489 buffers_create(&buffers);
1490 run_wrap_func(&buffers,
1491 do_overwrite_source__one,
1492 p->copy, h->hang);
1493 }
Chris Wilson16bafdf2014-09-04 09:26:24 +01001494
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001495 igt_subtest_f("%s-%s-%s-overwrite-source%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1496 buffers_create(&buffers);
1497 run_wrap_func(&buffers,
1498 do_overwrite_source,
1499 p->copy, h->hang);
1500 }
Chris Wilsona1726762015-03-16 16:29:57 +00001501
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001502 igt_subtest_f("%s-%s-%s-overwrite-source-read-bcs%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1503 buffers_create(&buffers);
1504 run_wrap_func(&buffers,
1505 do_overwrite_source_read_bcs,
1506 p->copy, h->hang);
1507 }
Chris Wilsona1726762015-03-16 16:29:57 +00001508
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001509 igt_subtest_f("%s-%s-%s-overwrite-source-read-rcs%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1510 igt_require(rendercopy);
1511 buffers_create(&buffers);
1512 run_wrap_func(&buffers,
1513 do_overwrite_source_read_rcs,
1514 p->copy, h->hang);
1515 }
Chris Wilsona1726762015-03-16 16:29:57 +00001516
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001517 igt_subtest_f("%s-%s-%s-overwrite-source-rev%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1518 buffers_create(&buffers);
1519 run_wrap_func(&buffers,
1520 do_overwrite_source__rev,
1521 p->copy, h->hang);
1522 }
Chris Wilson16bafdf2014-09-04 09:26:24 +01001523
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001524 /* try to intermix copies with GPU copies*/
1525 igt_subtest_f("%s-%s-%s-intermix-rcs%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1526 igt_require(rendercopy);
1527 buffers_create(&buffers);
1528 run_wrap_func(&buffers,
1529 do_intermix_rcs,
1530 p->copy, h->hang);
1531 }
1532 igt_subtest_f("%s-%s-%s-intermix-bcs%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1533 igt_require(rendercopy);
1534 buffers_create(&buffers);
1535 run_wrap_func(&buffers,
1536 do_intermix_bcs,
1537 p->copy, h->hang);
1538 }
1539 igt_subtest_f("%s-%s-%s-intermix-both%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1540 igt_require(rendercopy);
1541 buffers_create(&buffers);
1542 run_wrap_func(&buffers,
1543 do_intermix_both,
1544 p->copy, h->hang);
1545 }
Chris Wilsona72d4052015-03-18 14:15:22 +00001546
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001547 /* try to read the results before the copy completes */
1548 igt_subtest_f("%s-%s-%s-early-read%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1549 buffers_create(&buffers);
1550 run_wrap_func(&buffers,
1551 do_early_read,
1552 p->copy, h->hang);
1553 }
Chris Wilson16bafdf2014-09-04 09:26:24 +01001554
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001555 /* concurrent reads */
1556 igt_subtest_f("%s-%s-%s-read-read-bcs%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1557 buffers_create(&buffers);
1558 run_wrap_func(&buffers,
1559 do_read_read_bcs,
1560 p->copy, h->hang);
1561 }
1562 igt_subtest_f("%s-%s-%s-read-read-rcs%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1563 igt_require(rendercopy);
1564 buffers_create(&buffers);
1565 run_wrap_func(&buffers,
1566 do_read_read_rcs,
1567 p->copy, h->hang);
1568 }
Chris Wilson35b0ac92015-03-16 11:55:46 +00001569
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001570 /* split copying between rings */
1571 igt_subtest_f("%s-%s-%s-write-read-bcs%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1572 buffers_create(&buffers);
1573 run_wrap_func(&buffers,
1574 do_write_read_bcs,
1575 p->copy, h->hang);
1576 }
1577 igt_subtest_f("%s-%s-%s-write-read-rcs%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1578 igt_require(rendercopy);
1579 buffers_create(&buffers);
1580 run_wrap_func(&buffers,
1581 do_write_read_rcs,
1582 p->copy, h->hang);
1583 }
Chris Wilson0c266522015-11-11 16:37:16 +00001584
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001585 /* and finally try to trick the kernel into loosing the pending write */
1586 igt_subtest_f("%s-%s-%s-gpu-read-after-write%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1587 buffers_create(&buffers);
1588 run_wrap_func(&buffers,
1589 do_gpu_read_after_write,
1590 p->copy, h->hang);
1591 }
Chris Wilson16bafdf2014-09-04 09:26:24 +01001592 }
Chris Wilson08188752014-09-03 13:38:30 +01001593 }
Chris Wilson6867b872016-03-24 07:57:30 +00001594
1595 if (!*h->suffix)
Daniel Vetterbe21fc02016-06-17 16:04:09 +02001596 igt_fixture
1597 igt_stop_hang_detector();
Chris Wilson59c55622014-08-29 13:11:37 +01001598 }
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001599
1600 igt_fixture
1601 buffers_fini(&buffers);
Daniel Vetter5a598c92013-08-14 15:08:05 +02001602}
Daniel Vetter43779e32013-08-14 14:50:50 +02001603
1604static void
Chris Wilson094e0cb2016-03-01 13:22:03 +00001605run_modes(const char *style,
Chris Wilson4eba8e22016-03-18 10:44:31 +00001606 const struct create *create,
Chris Wilson094e0cb2016-03-01 13:22:03 +00001607 const struct access_mode *mode,
Chris Wilson5d669bf2016-03-18 14:44:53 +00001608 const struct size *size,
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001609 const int num)
Daniel Vetter43779e32013-08-14 14:50:50 +02001610{
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001611 const struct wrap {
1612 const char *suffix;
1613 run_wrap func;
1614 } wrappers[] = {
1615 { "", run_single },
1616 { "-child", run_child },
1617 { "-forked", run_forked },
1618 { "-interruptible", run_interruptible },
1619 { "-bomb", run_bomb },
1620 { NULL },
1621 };
Daniel Vetter3dba47e2013-08-06 22:27:37 +02001622
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001623 while (mode->name) {
1624 igt_subtest_group {
1625 igt_fixture {
1626 if (mode->require)
Chris Wilsone85613b2016-03-19 14:01:38 +00001627 mode->require(create, num);
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001628 }
1629
1630 for (const struct wrap *w = wrappers; w->suffix; w++) {
1631 run_mode(style, create, mode, size, num,
1632 w->suffix, w->func);
1633 }
Daniel Vetter96650732016-03-18 21:55:00 +01001634 }
1635
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001636 mode++;
Daniel Vetter96650732016-03-18 21:55:00 +01001637 }
Daniel Vetter43779e32013-08-14 14:50:50 +02001638}
1639
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001640static unsigned
Chris Wilsonf338e982016-03-19 13:10:17 +00001641num_buffers(uint64_t max,
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001642 const struct size *s,
1643 const struct create *c,
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001644 unsigned allow_mem)
1645{
1646 unsigned size = 4*s->width*s->height;
Chris Wilson9e7e7c32016-04-11 09:17:33 +01001647 uint64_t n;
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001648
Chris Wilson9e7e7c32016-04-11 09:17:33 +01001649 igt_assert(size);
1650 n = max / (2*size);
1651 n += MIN_BUFFERS;
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001652
Chris Wilson9e7e7c32016-04-11 09:17:33 +01001653 igt_require(n < INT32_MAX);
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001654 igt_require(set_max_map_count(2*n));
1655
Chris Wilsone85613b2016-03-19 14:01:38 +00001656 if (c->require)
1657 c->require(c, n);
1658
Chris Wilson6867b872016-03-24 07:57:30 +00001659 intel_require_memory(2*n, size, allow_mem);
1660
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001661 return n;
1662}
1663
Daniel Vetter071e9ca2013-10-31 16:23:26 +01001664igt_main
Daniel Vetter43779e32013-08-14 14:50:50 +02001665{
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001666 const struct access_mode modes[] = {
1667 {
1668 .name = "prw",
1669 .create_bo = unmapped_create_bo,
1670 .set_bo = prw_set_bo,
1671 .cmp_bo = prw_cmp_bo,
1672 .release_bo = nop_release_bo,
1673 },
1674 {
1675 .name = "partial",
1676 .create_bo = unmapped_create_bo,
1677 .set_bo = partial_set_bo,
1678 .cmp_bo = partial_cmp_bo,
1679 .release_bo = nop_release_bo,
1680 },
1681 {
1682 .name = "cpu",
1683 .create_bo = unmapped_create_bo,
1684 .require = create_cpu_require,
1685 .set_bo = cpu_set_bo,
1686 .cmp_bo = cpu_cmp_bo,
1687 .release_bo = nop_release_bo,
1688 },
1689 {
1690 .name = "snoop",
1691 .create_bo = snoop_create_bo,
1692 .require = create_snoop_require,
1693 .set_bo = cpu_set_bo,
1694 .cmp_bo = cpu_cmp_bo,
1695 .release_bo = nop_release_bo,
1696 },
1697 {
1698 .name = "userptr",
1699 .create_bo = userptr_create_bo,
1700 .require = create_userptr_require,
1701 .set_bo = userptr_set_bo,
1702 .cmp_bo = userptr_cmp_bo,
1703 .release_bo = userptr_release_bo,
1704 },
1705 {
1706 .name = "dmabuf",
1707 .create_bo = dmabuf_create_bo,
1708 .require = create_dmabuf_require,
1709 .set_bo = dmabuf_set_bo,
1710 .cmp_bo = dmabuf_cmp_bo,
1711 .release_bo = dmabuf_release_bo,
1712 },
1713 {
Chris Wilsone7ed4ef2016-07-18 11:38:09 +01001714 .name = "vgem",
1715 .create_bo = vgem_create_bo,
1716 .require = create_vgem_require,
1717 .set_bo = dmabuf_set_bo,
1718 .cmp_bo = dmabuf_cmp_bo,
1719 .release_bo = dmabuf_release_bo,
1720 },
1721 {
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001722 .name = "gtt",
1723 .create_bo = gtt_create_bo,
1724 .set_bo = gtt_set_bo,
1725 .cmp_bo = gtt_cmp_bo,
1726 .release_bo = nop_release_bo,
1727 },
1728 {
1729 .name = "gttX",
1730 .create_bo = gttX_create_bo,
1731 .set_bo = gtt_set_bo,
1732 .cmp_bo = gtt_cmp_bo,
1733 .release_bo = nop_release_bo,
1734 },
1735 {
1736 .name = "wc",
1737 .require = wc_create_require,
1738 .create_bo = wc_create_bo,
1739 .set_bo = gtt_set_bo,
1740 .cmp_bo = gtt_cmp_bo,
1741 .release_bo = wc_release_bo,
1742 },
1743 {
1744 .name = "gpu",
1745 .create_bo = gpu_create_bo,
1746 .set_bo = gpu_set_bo,
1747 .cmp_bo = gpu_cmp_bo,
1748 .release_bo = nop_release_bo,
1749 },
1750 {
1751 .name = "gpuX",
1752 .create_bo = gpuX_create_bo,
1753 .set_bo = gpu_set_bo,
1754 .cmp_bo = gpu_cmp_bo,
1755 .release_bo = nop_release_bo,
1756 },
1757 { NULL },
1758 };
Chris Wilson4eba8e22016-03-18 10:44:31 +00001759 const struct create create[] = {
1760 { "", can_create_normal, create_normal_bo},
1761#if HAVE_CREATE_PRIVATE
1762 { "private-", can_create_private, create_private_bo},
1763#endif
1764#if HAVE_CREATE_STOLEN
1765 { "stolen-", can_create_stolen, create_stolen_bo},
1766#endif
Chris Wilson1d6e5d32016-01-03 13:44:17 +00001767 { NULL, NULL }
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001768 };
Chris Wilson5d669bf2016-03-18 14:44:53 +00001769 const struct size sizes[] = {
1770 { "4KiB", 128, 8 },
1771 { "256KiB", 128, 128 },
1772 { "1MiB", 512, 512 },
1773 { "16MiB", 2048, 2048 },
1774 { NULL}
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001775 };
Chris Wilson42291f22016-01-07 11:19:26 +00001776 uint64_t pin_sz = 0;
1777 void *pinned = NULL;
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001778 char name[80];
1779 int count = 0;
Daniel Vetter43779e32013-08-14 14:50:50 +02001780
Daniel Vetter43779e32013-08-14 14:50:50 +02001781 igt_skip_on_simulation();
1782
Chris Wilson77633492015-03-26 08:11:43 +00001783 if (strstr(igt_test_name(), "all"))
1784 all = true;
1785
Daniel Vetter2dbd9982013-08-14 15:48:54 +02001786 igt_fixture {
Lucas De Marchi8bc80862019-04-04 15:16:11 -07001787 igt_allow_unlimited_files();
Chris Wilson3d8af562016-03-20 10:49:54 +00001788
Micah Fedkec81d2932015-07-22 21:54:02 +00001789 fd = drm_open_driver(DRIVER_INTEL);
Chris Wilson9518cb52017-02-22 15:24:54 +00001790 igt_require_gem(fd);
Chris Wilson5b675f72016-01-22 17:33:40 +00001791 intel_detect_and_clear_missed_interrupts(fd);
Chris Wilson6c428a62014-08-29 13:11:37 +01001792 devid = intel_get_drm_devid(fd);
1793 gen = intel_gen(devid);
Chris Wilson59c55622014-08-29 13:11:37 +01001794 rendercopy = igt_get_render_copyfunc(devid);
Chris Wilsone7ed4ef2016-07-18 11:38:09 +01001795
1796 vgem_drv = __drm_open_driver(DRIVER_VGEM);
Chris Wilson2d08e9e2015-12-11 09:25:03 +00001797 }
Daniel Vetter43779e32013-08-14 14:50:50 +02001798
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001799 for (const struct create *c = create; c->name; c++) {
1800 for (const struct size *s = sizes; s->name; s++) {
1801 /* Minimum test set */
Chris Wilsonf338e982016-03-19 13:10:17 +00001802 snprintf(name, sizeof(name), "%s%s-%s",
1803 c->name, s->name, "tiny");
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001804 igt_subtest_group {
1805 igt_fixture {
Chris Wilsonf338e982016-03-19 13:10:17 +00001806 count = num_buffers(0, s, c, CHECK_RAM);
Chris Wilson5d669bf2016-03-18 14:44:53 +00001807 }
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001808 run_modes(name, c, modes, s, count);
Chris Wilson5d669bf2016-03-18 14:44:53 +00001809 }
1810
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001811 /* "Average" test set */
Chris Wilsonf338e982016-03-19 13:10:17 +00001812 snprintf(name, sizeof(name), "%s%s-%s",
1813 c->name, s->name, "small");
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001814 igt_subtest_group {
1815 igt_fixture {
Ville Syrjäläe408d562019-03-27 20:52:52 +02001816 count = num_buffers(gem_mappable_aperture_size()/4,
Chris Wilsonf338e982016-03-19 13:10:17 +00001817 s, c, CHECK_RAM);
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001818 }
1819 run_modes(name, c, modes, s, count);
1820 }
Chris Wilson5d669bf2016-03-18 14:44:53 +00001821
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001822 /* Use the entire mappable aperture */
Chris Wilsonf338e982016-03-19 13:10:17 +00001823 snprintf(name, sizeof(name), "%s%s-%s",
1824 c->name, s->name, "thrash");
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001825 igt_subtest_group {
1826 igt_fixture {
Ville Syrjäläe408d562019-03-27 20:52:52 +02001827 count = num_buffers(gem_mappable_aperture_size(),
Chris Wilsonf338e982016-03-19 13:10:17 +00001828 s, c, CHECK_RAM);
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001829 }
1830 run_modes(name, c, modes, s, count);
1831 }
1832
1833 /* Use the entire global GTT */
Chris Wilsonf338e982016-03-19 13:10:17 +00001834 snprintf(name, sizeof(name), "%s%s-%s",
1835 c->name, s->name, "global");
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001836 igt_subtest_group {
1837 igt_fixture {
Chris Wilsonf338e982016-03-19 13:10:17 +00001838 count = num_buffers(gem_global_aperture_size(fd),
1839 s, c, CHECK_RAM);
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001840 }
1841 run_modes(name, c, modes, s, count);
1842 }
1843
1844 /* Use the entire per-process GTT */
Chris Wilsonf338e982016-03-19 13:10:17 +00001845 snprintf(name, sizeof(name), "%s%s-%s",
1846 c->name, s->name, "full");
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001847 igt_subtest_group {
1848 igt_fixture {
Chris Wilsonf338e982016-03-19 13:10:17 +00001849 count = num_buffers(gem_aperture_size(fd),
1850 s, c, CHECK_RAM);
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001851 }
1852 run_modes(name, c, modes, s, count);
1853 }
1854
Chris Wilsone8eb9af2017-03-08 13:09:00 +00001855 snprintf(name, sizeof(name), "%s%s-%s",
1856 c->name, s->name, "shrink");
1857 igt_subtest_group {
1858 igt_fixture {
Ville Syrjäläe408d562019-03-27 20:52:52 +02001859 count = num_buffers(gem_mappable_aperture_size(),
Chris Wilsone8eb9af2017-03-08 13:09:00 +00001860 s, c, CHECK_RAM);
1861
Chris Wilson83884e92017-03-21 17:16:03 +00001862 igt_fork_shrink_helper(fd);
Chris Wilsone8eb9af2017-03-08 13:09:00 +00001863 }
1864 run_modes(name, c, modes, s, count);
1865
1866 igt_fixture
1867 igt_stop_shrink_helper();
1868 }
1869
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001870 /* Use the entire mappable aperture, force swapping */
Chris Wilsonf338e982016-03-19 13:10:17 +00001871 snprintf(name, sizeof(name), "%s%s-%s",
1872 c->name, s->name, "swap");
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001873 igt_subtest_group {
1874 igt_fixture {
Ville Syrjäläe408d562019-03-27 20:52:52 +02001875 if (intel_get_avail_ram_mb() > gem_mappable_aperture_size()/(1024*1024)) {
1876 pin_sz = intel_get_avail_ram_mb() - gem_mappable_aperture_size()/(1024*1024);
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001877
1878 igt_debug("Pinning %lld MiB\n", (long long)pin_sz);
1879 pin_sz *= 1024 * 1024;
1880
1881 if (posix_memalign(&pinned, 4096, pin_sz) ||
1882 mlock(pinned, pin_sz) ||
1883 madvise(pinned, pin_sz, MADV_DONTFORK)) {
1884 free(pinned);
1885 pinned = NULL;
1886 }
1887 igt_require(pinned);
1888 }
1889
Ville Syrjäläe408d562019-03-27 20:52:52 +02001890 count = num_buffers(gem_mappable_aperture_size(),
Chris Wilsonf338e982016-03-19 13:10:17 +00001891 s, c, CHECK_RAM | CHECK_SWAP);
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001892 }
1893 run_modes(name, c, modes, s, count);
1894
1895 igt_fixture {
1896 if (pinned) {
1897 munlock(pinned, pin_sz);
1898 free(pinned);
1899 pinned = NULL;
1900 }
Chris Wilson5d669bf2016-03-18 14:44:53 +00001901 }
Chris Wilson42291f22016-01-07 11:19:26 +00001902 }
Chris Wilson1d6e5d32016-01-03 13:44:17 +00001903 }
Daniel Vetter2dbd9982013-08-14 15:48:54 +02001904 }
Daniel Vetter3dba47e2013-08-06 22:27:37 +02001905}