blob: 3a1097ba5a25f4b37bf552ac2311cf17842a0f08 [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,
Chris Wilson59c55622014-08-29 13:11:37 +0100857 }, s = {
858 .bo = src,
Chris Wilson5d669bf2016-03-18 14:44:53 +0000859 .size = b->npixels * 4,
860 .num_tiles = b->npixels * 4,
Chris Wilson37f4da02016-01-27 13:02:35 +0000861 .stride = b->width * 4,
Chris Wilson59c55622014-08-29 13:11:37 +0100862 };
Chris Wilson86055df2014-08-29 17:36:29 +0100863 uint32_t swizzle;
864
865 drm_intel_bo_get_tiling(dst, &d.tiling, &swizzle);
866 drm_intel_bo_get_tiling(src, &s.tiling, &swizzle);
867
Chris Wilson094e0cb2016-03-01 13:22:03 +0000868 rendercopy(b->batch, NULL,
Chris Wilson59c55622014-08-29 13:11:37 +0100869 &s, 0, 0,
Chris Wilson37f4da02016-01-27 13:02:35 +0000870 b->width, b->height,
Chris Wilson59c55622014-08-29 13:11:37 +0100871 &d, 0, 0);
872}
873
Chris Wilson37f4da02016-01-27 13:02:35 +0000874static void blt_copy_bo(struct buffers *b, drm_intel_bo *dst, drm_intel_bo *src)
Chris Wilson59c55622014-08-29 13:11:37 +0100875{
Chris Wilson094e0cb2016-03-01 13:22:03 +0000876 intel_blt_copy(b->batch,
Chris Wilson37f4da02016-01-27 13:02:35 +0000877 src, 0, 0, 4*b->width,
878 dst, 0, 0, 4*b->width,
879 b->width, b->height, 32);
Chris Wilson59c55622014-08-29 13:11:37 +0100880}
Daniel Vetter5a598c92013-08-14 15:08:05 +0200881
Chris Wilson37f4da02016-01-27 13:02:35 +0000882static void cpu_copy_bo(struct buffers *b, drm_intel_bo *dst, drm_intel_bo *src)
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530883{
Chris Wilsond199ad82016-07-27 23:19:41 +0100884 const int size = b->page_size;
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530885 void *d, *s;
886
887 gem_set_domain(fd, src->handle, I915_GEM_DOMAIN_CPU, 0);
888 gem_set_domain(fd, dst->handle, I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
Ville Syrjäläf52e7ec2015-10-09 19:11:39 +0300889 s = gem_mmap__cpu(fd, src->handle, 0, size, PROT_READ);
890 d = gem_mmap__cpu(fd, dst->handle, 0, size, PROT_WRITE);
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530891
892 memcpy(d, s, size);
893
894 munmap(d, size);
895 munmap(s, size);
896}
897
Chris Wilson37f4da02016-01-27 13:02:35 +0000898static void gtt_copy_bo(struct buffers *b, drm_intel_bo *dst, drm_intel_bo *src)
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530899{
Chris Wilsond199ad82016-07-27 23:19:41 +0100900 const int size = b->page_size;
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530901 void *d, *s;
902
903 gem_set_domain(fd, src->handle, I915_GEM_DOMAIN_GTT, 0);
904 gem_set_domain(fd, dst->handle, I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
905
Ville Syrjäläf52e7ec2015-10-09 19:11:39 +0300906 s = gem_mmap__gtt(fd, src->handle, size, PROT_READ);
907 d = gem_mmap__gtt(fd, dst->handle, size, PROT_WRITE);
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530908
909 memcpy(d, s, size);
910
911 munmap(d, size);
912 munmap(s, size);
913}
914
Chris Wilson37f4da02016-01-27 13:02:35 +0000915static void wc_copy_bo(struct buffers *b, drm_intel_bo *dst, drm_intel_bo *src)
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530916{
Chris Wilsond199ad82016-07-27 23:19:41 +0100917 const int size = b->page_size;
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530918 void *d, *s;
919
Chris Wilsone9cd7d52017-04-13 10:08:09 +0100920 gem_set_domain(fd, src->handle, I915_GEM_DOMAIN_WC, 0);
921 gem_set_domain(fd, dst->handle, I915_GEM_DOMAIN_WC, I915_GEM_DOMAIN_WC);
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530922
Ville Syrjäläf52e7ec2015-10-09 19:11:39 +0300923 s = gem_mmap__wc(fd, src->handle, 0, size, PROT_READ);
924 d = gem_mmap__wc(fd, dst->handle, 0, size, PROT_WRITE);
Chris Wilsonf2a045f2015-01-02 16:33:33 +0530925
926 memcpy(d, s, size);
927
928 munmap(d, size);
929 munmap(s, size);
930}
931
Chris Wilson0a1fc452016-09-13 11:13:14 +0100932static igt_hang_t no_hang(void)
Chris Wilson16bafdf2014-09-04 09:26:24 +0100933{
Chris Wilson0a1fc452016-09-13 11:13:14 +0100934 return (igt_hang_t){0, 0};
Chris Wilson16bafdf2014-09-04 09:26:24 +0100935}
936
Chris Wilson0a1fc452016-09-13 11:13:14 +0100937static igt_hang_t bcs_hang(void)
Chris Wilson16bafdf2014-09-04 09:26:24 +0100938{
Daniel Vetter3cd45de2015-02-10 17:46:43 +0100939 return igt_hang_ring(fd, I915_EXEC_BLT);
Chris Wilson16bafdf2014-09-04 09:26:24 +0100940}
941
Chris Wilson0a1fc452016-09-13 11:13:14 +0100942static igt_hang_t rcs_hang(void)
Chris Wilson16bafdf2014-09-04 09:26:24 +0100943{
Daniel Vetter3cd45de2015-02-10 17:46:43 +0100944 return igt_hang_ring(fd, I915_EXEC_RENDER);
Chris Wilson16bafdf2014-09-04 09:26:24 +0100945}
946
Chris Wilson0a1fc452016-09-13 11:13:14 +0100947static igt_hang_t all_hang(void)
Chris Wilsonfcb4cfa2016-03-20 11:38:26 +0000948{
949 uint32_t bbe = MI_BATCH_BUFFER_END;
950 struct drm_i915_gem_execbuffer2 execbuf;
951 struct drm_i915_gem_exec_object2 obj;
Chris Wilson0a1fc452016-09-13 11:13:14 +0100952 igt_hang_t hang;
Chris Wilsonfcb4cfa2016-03-20 11:38:26 +0000953 unsigned engine;
954
955 memset(&obj, 0, sizeof(obj));
956 obj.handle = gem_create(fd, 4096);
957 gem_write(fd, obj.handle, 0, &bbe, sizeof(&bbe));
958
959 memset(&execbuf, 0, sizeof(execbuf));
Chris Wilson4de67b22017-01-02 11:05:21 +0000960 execbuf.buffers_ptr = to_user_pointer(&obj);
Chris Wilsonfcb4cfa2016-03-20 11:38:26 +0000961 execbuf.buffer_count = 1;
962
Chris Wilson305ebce2018-02-21 14:13:16 +0000963 for_each_physical_engine(fd, engine) {
Chris Wilsonfcb4cfa2016-03-20 11:38:26 +0000964 hang = igt_hang_ring(fd, engine);
965
966 execbuf.flags = engine;
967 __gem_execbuf(fd, &execbuf);
968
969 gem_close(fd, hang.handle);
970 }
971
972 hang.handle = obj.handle;
973 return hang;
974}
975
Chris Wilson8bf09f32015-12-17 09:16:42 +0000976static void do_basic0(struct buffers *buffers,
977 do_copy do_copy_func,
978 do_hang do_hang_func)
979{
980 gem_quiescent_gpu(fd);
981
Chris Wilson37f4da02016-01-27 13:02:35 +0000982 buffers->mode->set_bo(buffers, buffers->src[0], 0xdeadbeef);
Chris Wilson8bf09f32015-12-17 09:16:42 +0000983 for (int i = 0; i < buffers->count; i++) {
Chris Wilson0a1fc452016-09-13 11:13:14 +0100984 igt_hang_t hang = do_hang_func();
Chris Wilson8bf09f32015-12-17 09:16:42 +0000985
Chris Wilson37f4da02016-01-27 13:02:35 +0000986 do_copy_func(buffers, buffers->dst[i], buffers->src[0]);
987 buffers->mode->cmp_bo(buffers, buffers->dst[i], 0xdeadbeef);
Chris Wilson8bf09f32015-12-17 09:16:42 +0000988
989 igt_post_hang_ring(fd, hang);
990 }
991}
992
993static void do_basic1(struct buffers *buffers,
994 do_copy do_copy_func,
995 do_hang do_hang_func)
Chris Wilson197db862015-12-09 20:54:10 +0000996{
997 gem_quiescent_gpu(fd);
998
999 for (int i = 0; i < buffers->count; i++) {
Chris Wilson0a1fc452016-09-13 11:13:14 +01001000 igt_hang_t hang = do_hang_func();
Chris Wilson197db862015-12-09 20:54:10 +00001001
Chris Wilson37f4da02016-01-27 13:02:35 +00001002 buffers->mode->set_bo(buffers, buffers->src[i], i);
1003 buffers->mode->set_bo(buffers, buffers->dst[i], ~i);
Chris Wilson8bf09f32015-12-17 09:16:42 +00001004
Chris Wilson37f4da02016-01-27 13:02:35 +00001005 do_copy_func(buffers, buffers->dst[i], buffers->src[i]);
Chris Wilson8bf09f32015-12-17 09:16:42 +00001006 usleep(0); /* let someone else claim the mutex */
Chris Wilson37f4da02016-01-27 13:02:35 +00001007 buffers->mode->cmp_bo(buffers, buffers->dst[i], i);
Chris Wilson197db862015-12-09 20:54:10 +00001008
1009 igt_post_hang_ring(fd, hang);
1010 }
1011}
1012
Chris Wilson8bf09f32015-12-17 09:16:42 +00001013static void do_basicN(struct buffers *buffers,
1014 do_copy do_copy_func,
1015 do_hang do_hang_func)
1016{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001017 igt_hang_t hang;
Chris Wilson8bf09f32015-12-17 09:16:42 +00001018
1019 gem_quiescent_gpu(fd);
1020
1021 for (int i = 0; i < buffers->count; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001022 buffers->mode->set_bo(buffers, buffers->src[i], i);
1023 buffers->mode->set_bo(buffers, buffers->dst[i], ~i);
Chris Wilson8bf09f32015-12-17 09:16:42 +00001024 }
1025
1026 hang = do_hang_func();
1027
1028 for (int i = 0; i < buffers->count; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001029 do_copy_func(buffers, buffers->dst[i], buffers->src[i]);
Chris Wilson8bf09f32015-12-17 09:16:42 +00001030 usleep(0); /* let someone else claim the mutex */
1031 }
1032
1033 for (int i = 0; i < buffers->count; i++)
Chris Wilson37f4da02016-01-27 13:02:35 +00001034 buffers->mode->cmp_bo(buffers, buffers->dst[i], i);
Chris Wilson8bf09f32015-12-17 09:16:42 +00001035
1036 igt_post_hang_ring(fd, hang);
1037}
1038
Chris Wilson99b5ee82015-01-22 10:03:45 +00001039static void do_overwrite_source(struct buffers *buffers,
Chris Wilson16bafdf2014-09-04 09:26:24 +01001040 do_copy do_copy_func,
1041 do_hang do_hang_func)
Daniel Vetter5a598c92013-08-14 15:08:05 +02001042{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001043 igt_hang_t hang;
Daniel Vetter5a598c92013-08-14 15:08:05 +02001044 int i;
1045
1046 gem_quiescent_gpu(fd);
Chris Wilson99b5ee82015-01-22 10:03:45 +00001047 for (i = 0; i < buffers->count; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001048 buffers->mode->set_bo(buffers, buffers->src[i], i);
1049 buffers->mode->set_bo(buffers, buffers->dst[i], ~i);
Daniel Vetter5a598c92013-08-14 15:08:05 +02001050 }
Chris Wilson99b5ee82015-01-22 10:03:45 +00001051 for (i = 0; i < buffers->count; i++)
Chris Wilson37f4da02016-01-27 13:02:35 +00001052 do_copy_func(buffers, buffers->dst[i], buffers->src[i]);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001053 hang = do_hang_func();
Chris Wilson99b5ee82015-01-22 10:03:45 +00001054 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001055 buffers->mode->set_bo(buffers, buffers->src[i], 0xdeadbeef);
Chris Wilson99b5ee82015-01-22 10:03:45 +00001056 for (i = 0; i < buffers->count; i++)
Chris Wilson37f4da02016-01-27 13:02:35 +00001057 buffers->mode->cmp_bo(buffers, buffers->dst[i], i);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001058 igt_post_hang_ring(fd, hang);
1059}
1060
Chris Wilsona1726762015-03-16 16:29:57 +00001061static void do_overwrite_source_read(struct buffers *buffers,
1062 do_copy do_copy_func,
1063 do_hang do_hang_func,
1064 int do_rcs)
1065{
1066 const int half = buffers->count/2;
Chris Wilson0a1fc452016-09-13 11:13:14 +01001067 igt_hang_t hang;
Chris Wilsona1726762015-03-16 16:29:57 +00001068 int i;
1069
1070 gem_quiescent_gpu(fd);
1071 for (i = 0; i < half; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001072 buffers->mode->set_bo(buffers, buffers->src[i], i);
1073 buffers->mode->set_bo(buffers, buffers->dst[i], ~i);
1074 buffers->mode->set_bo(buffers, buffers->dst[i+half], ~i);
Chris Wilsona1726762015-03-16 16:29:57 +00001075 }
1076 for (i = 0; i < half; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001077 do_copy_func(buffers, buffers->dst[i], buffers->src[i]);
Chris Wilsona1726762015-03-16 16:29:57 +00001078 if (do_rcs)
Chris Wilson37f4da02016-01-27 13:02:35 +00001079 render_copy_bo(buffers, buffers->dst[i+half], buffers->src[i]);
Chris Wilsona1726762015-03-16 16:29:57 +00001080 else
Chris Wilson37f4da02016-01-27 13:02:35 +00001081 blt_copy_bo(buffers, buffers->dst[i+half], buffers->src[i]);
Chris Wilsona1726762015-03-16 16:29:57 +00001082 }
1083 hang = do_hang_func();
1084 for (i = half; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001085 buffers->mode->set_bo(buffers, buffers->src[i], 0xdeadbeef);
Chris Wilsona1726762015-03-16 16:29:57 +00001086 for (i = 0; i < half; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001087 buffers->mode->cmp_bo(buffers, buffers->dst[i], i);
1088 buffers->mode->cmp_bo(buffers, buffers->dst[i+half], i);
Chris Wilsona1726762015-03-16 16:29:57 +00001089 }
1090 igt_post_hang_ring(fd, hang);
1091}
1092
1093static void do_overwrite_source_read_bcs(struct buffers *buffers,
1094 do_copy do_copy_func,
1095 do_hang do_hang_func)
1096{
1097 do_overwrite_source_read(buffers, do_copy_func, do_hang_func, 0);
1098}
1099
1100static void do_overwrite_source_read_rcs(struct buffers *buffers,
1101 do_copy do_copy_func,
1102 do_hang do_hang_func)
1103{
1104 do_overwrite_source_read(buffers, do_copy_func, do_hang_func, 1);
1105}
1106
Chris Wilson99b5ee82015-01-22 10:03:45 +00001107static void do_overwrite_source__rev(struct buffers *buffers,
Chris Wilson16bafdf2014-09-04 09:26:24 +01001108 do_copy do_copy_func,
1109 do_hang do_hang_func)
1110{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001111 igt_hang_t hang;
Chris Wilson16bafdf2014-09-04 09:26:24 +01001112 int i;
1113
1114 gem_quiescent_gpu(fd);
Chris Wilson99b5ee82015-01-22 10:03:45 +00001115 for (i = 0; i < buffers->count; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001116 buffers->mode->set_bo(buffers, buffers->src[i], i);
1117 buffers->mode->set_bo(buffers, buffers->dst[i], ~i);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001118 }
Chris Wilson99b5ee82015-01-22 10:03:45 +00001119 for (i = 0; i < buffers->count; i++)
Chris Wilson37f4da02016-01-27 13:02:35 +00001120 do_copy_func(buffers, buffers->dst[i], buffers->src[i]);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001121 hang = do_hang_func();
Chris Wilson99b5ee82015-01-22 10:03:45 +00001122 for (i = 0; i < buffers->count; i++)
Chris Wilson37f4da02016-01-27 13:02:35 +00001123 buffers->mode->set_bo(buffers, buffers->src[i], 0xdeadbeef);
Chris Wilson99b5ee82015-01-22 10:03:45 +00001124 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001125 buffers->mode->cmp_bo(buffers, buffers->dst[i], i);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001126 igt_post_hang_ring(fd, hang);
1127}
1128
Chris Wilson99b5ee82015-01-22 10:03:45 +00001129static void do_overwrite_source__one(struct buffers *buffers,
Chris Wilson16bafdf2014-09-04 09:26:24 +01001130 do_copy do_copy_func,
1131 do_hang do_hang_func)
1132{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001133 igt_hang_t hang;
Chris Wilson16bafdf2014-09-04 09:26:24 +01001134
1135 gem_quiescent_gpu(fd);
Chris Wilson37f4da02016-01-27 13:02:35 +00001136 buffers->mode->set_bo(buffers, buffers->src[0], 0);
1137 buffers->mode->set_bo(buffers, buffers->dst[0], ~0);
1138 do_copy_func(buffers, buffers->dst[0], buffers->src[0]);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001139 hang = do_hang_func();
Chris Wilson37f4da02016-01-27 13:02:35 +00001140 buffers->mode->set_bo(buffers, buffers->src[0], 0xdeadbeef);
1141 buffers->mode->cmp_bo(buffers, buffers->dst[0], 0);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001142 igt_post_hang_ring(fd, hang);
Daniel Vetter5a598c92013-08-14 15:08:05 +02001143}
1144
Chris Wilsona72d4052015-03-18 14:15:22 +00001145static void do_intermix(struct buffers *buffers,
1146 do_copy do_copy_func,
1147 do_hang do_hang_func,
1148 int do_rcs)
1149{
1150 const int half = buffers->count/2;
Chris Wilson0a1fc452016-09-13 11:13:14 +01001151 igt_hang_t hang;
Chris Wilsona72d4052015-03-18 14:15:22 +00001152 int i;
1153
1154 gem_quiescent_gpu(fd);
1155 for (i = 0; i < buffers->count; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001156 buffers->mode->set_bo(buffers, buffers->src[i], 0xdeadbeef^~i);
1157 buffers->mode->set_bo(buffers, buffers->dst[i], i);
Chris Wilsona72d4052015-03-18 14:15:22 +00001158 }
1159 for (i = 0; i < half; i++) {
1160 if (do_rcs == 1 || (do_rcs == -1 && i & 1))
Chris Wilson37f4da02016-01-27 13:02:35 +00001161 render_copy_bo(buffers, buffers->dst[i], buffers->src[i]);
Chris Wilsona72d4052015-03-18 14:15:22 +00001162 else
Chris Wilson37f4da02016-01-27 13:02:35 +00001163 blt_copy_bo(buffers, buffers->dst[i], buffers->src[i]);
Chris Wilsona72d4052015-03-18 14:15:22 +00001164
Chris Wilson37f4da02016-01-27 13:02:35 +00001165 do_copy_func(buffers, buffers->dst[i+half], buffers->src[i]);
Chris Wilsona72d4052015-03-18 14:15:22 +00001166
1167 if (do_rcs == 1 || (do_rcs == -1 && (i & 1) == 0))
Chris Wilson37f4da02016-01-27 13:02:35 +00001168 render_copy_bo(buffers, buffers->dst[i], buffers->dst[i+half]);
Chris Wilsona72d4052015-03-18 14:15:22 +00001169 else
Chris Wilson37f4da02016-01-27 13:02:35 +00001170 blt_copy_bo(buffers, buffers->dst[i], buffers->dst[i+half]);
Chris Wilsona72d4052015-03-18 14:15:22 +00001171
Chris Wilson37f4da02016-01-27 13:02:35 +00001172 do_copy_func(buffers, buffers->dst[i+half], buffers->src[i+half]);
Chris Wilsona72d4052015-03-18 14:15:22 +00001173 }
1174 hang = do_hang_func();
1175 for (i = 0; i < 2*half; i++)
Chris Wilson37f4da02016-01-27 13:02:35 +00001176 buffers->mode->cmp_bo(buffers, buffers->dst[i], 0xdeadbeef^~i);
Chris Wilsona72d4052015-03-18 14:15:22 +00001177 igt_post_hang_ring(fd, hang);
1178}
1179
1180static void do_intermix_rcs(struct buffers *buffers,
1181 do_copy do_copy_func,
1182 do_hang do_hang_func)
1183{
1184 do_intermix(buffers, do_copy_func, do_hang_func, 1);
1185}
1186
1187static void do_intermix_bcs(struct buffers *buffers,
1188 do_copy do_copy_func,
1189 do_hang do_hang_func)
1190{
1191 do_intermix(buffers, do_copy_func, do_hang_func, 0);
1192}
1193
1194static void do_intermix_both(struct buffers *buffers,
1195 do_copy do_copy_func,
1196 do_hang do_hang_func)
1197{
1198 do_intermix(buffers, do_copy_func, do_hang_func, -1);
1199}
1200
Chris Wilson99b5ee82015-01-22 10:03:45 +00001201static void do_early_read(struct buffers *buffers,
Chris Wilson16bafdf2014-09-04 09:26:24 +01001202 do_copy do_copy_func,
1203 do_hang do_hang_func)
Daniel Vetter5a598c92013-08-14 15:08:05 +02001204{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001205 igt_hang_t hang;
Daniel Vetter5a598c92013-08-14 15:08:05 +02001206 int i;
1207
1208 gem_quiescent_gpu(fd);
Chris Wilson99b5ee82015-01-22 10:03:45 +00001209 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001210 buffers->mode->set_bo(buffers, buffers->src[i], 0xdeadbeef);
Chris Wilson99b5ee82015-01-22 10:03:45 +00001211 for (i = 0; i < buffers->count; i++)
Chris Wilson37f4da02016-01-27 13:02:35 +00001212 do_copy_func(buffers, buffers->dst[i], buffers->src[i]);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001213 hang = do_hang_func();
Chris Wilson99b5ee82015-01-22 10:03:45 +00001214 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001215 buffers->mode->cmp_bo(buffers, buffers->dst[i], 0xdeadbeef);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001216 igt_post_hang_ring(fd, hang);
Daniel Vetter5a598c92013-08-14 15:08:05 +02001217}
1218
Chris Wilson35b0ac92015-03-16 11:55:46 +00001219static void do_read_read_bcs(struct buffers *buffers,
1220 do_copy do_copy_func,
1221 do_hang do_hang_func)
1222{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001223 igt_hang_t hang;
Chris Wilson35b0ac92015-03-16 11:55:46 +00001224 int i;
1225
1226 gem_quiescent_gpu(fd);
1227 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001228 buffers->mode->set_bo(buffers, buffers->src[i], 0xdeadbeef ^ i);
Chris Wilson35b0ac92015-03-16 11:55:46 +00001229 for (i = 0; i < buffers->count; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001230 do_copy_func(buffers, buffers->dst[i], buffers->src[i]);
1231 blt_copy_bo(buffers, buffers->spare, buffers->src[i]);
Chris Wilson35b0ac92015-03-16 11:55:46 +00001232 }
Chris Wilson37f4da02016-01-27 13:02:35 +00001233 buffers->mode->cmp_bo(buffers, buffers->spare, 0xdeadbeef^(buffers->count-1));
Chris Wilson35b0ac92015-03-16 11:55:46 +00001234 hang = do_hang_func();
1235 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001236 buffers->mode->cmp_bo(buffers, buffers->dst[i], 0xdeadbeef ^ i);
Chris Wilson35b0ac92015-03-16 11:55:46 +00001237 igt_post_hang_ring(fd, hang);
1238}
1239
Chris Wilson0c266522015-11-11 16:37:16 +00001240static void do_write_read_bcs(struct buffers *buffers,
1241 do_copy do_copy_func,
1242 do_hang do_hang_func)
1243{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001244 igt_hang_t hang;
Chris Wilson0c266522015-11-11 16:37:16 +00001245 int i;
1246
1247 gem_quiescent_gpu(fd);
1248 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001249 buffers->mode->set_bo(buffers, buffers->src[i], 0xdeadbeef ^ i);
Chris Wilson0c266522015-11-11 16:37:16 +00001250 for (i = 0; i < buffers->count; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001251 blt_copy_bo(buffers, buffers->spare, buffers->src[i]);
1252 do_copy_func(buffers, buffers->dst[i], buffers->spare);
Chris Wilson0c266522015-11-11 16:37:16 +00001253 }
1254 hang = do_hang_func();
1255 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001256 buffers->mode->cmp_bo(buffers, buffers->dst[i], 0xdeadbeef ^ i);
Chris Wilson0c266522015-11-11 16:37:16 +00001257 igt_post_hang_ring(fd, hang);
1258}
1259
Chris Wilson35b0ac92015-03-16 11:55:46 +00001260static void do_read_read_rcs(struct buffers *buffers,
1261 do_copy do_copy_func,
1262 do_hang do_hang_func)
1263{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001264 igt_hang_t hang;
Chris Wilson35b0ac92015-03-16 11:55:46 +00001265 int i;
1266
1267 gem_quiescent_gpu(fd);
1268 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001269 buffers->mode->set_bo(buffers, buffers->src[i], 0xdeadbeef ^ i);
Chris Wilson35b0ac92015-03-16 11:55:46 +00001270 for (i = 0; i < buffers->count; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001271 do_copy_func(buffers, buffers->dst[i], buffers->src[i]);
1272 render_copy_bo(buffers, buffers->spare, buffers->src[i]);
Chris Wilson35b0ac92015-03-16 11:55:46 +00001273 }
Chris Wilson37f4da02016-01-27 13:02:35 +00001274 buffers->mode->cmp_bo(buffers, buffers->spare, 0xdeadbeef^(buffers->count-1));
Chris Wilson35b0ac92015-03-16 11:55:46 +00001275 hang = do_hang_func();
1276 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001277 buffers->mode->cmp_bo(buffers, buffers->dst[i], 0xdeadbeef ^ i);
Chris Wilson35b0ac92015-03-16 11:55:46 +00001278 igt_post_hang_ring(fd, hang);
1279}
1280
Chris Wilson0c266522015-11-11 16:37:16 +00001281static void do_write_read_rcs(struct buffers *buffers,
1282 do_copy do_copy_func,
1283 do_hang do_hang_func)
1284{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001285 igt_hang_t hang;
Chris Wilson0c266522015-11-11 16:37:16 +00001286 int i;
1287
1288 gem_quiescent_gpu(fd);
1289 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001290 buffers->mode->set_bo(buffers, buffers->src[i], 0xdeadbeef ^ i);
Chris Wilson0c266522015-11-11 16:37:16 +00001291 for (i = 0; i < buffers->count; i++) {
Chris Wilson37f4da02016-01-27 13:02:35 +00001292 render_copy_bo(buffers, buffers->spare, buffers->src[i]);
1293 do_copy_func(buffers, buffers->dst[i], buffers->spare);
Chris Wilson0c266522015-11-11 16:37:16 +00001294 }
1295 hang = do_hang_func();
1296 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001297 buffers->mode->cmp_bo(buffers, buffers->dst[i], 0xdeadbeef ^ i);
Chris Wilson0c266522015-11-11 16:37:16 +00001298 igt_post_hang_ring(fd, hang);
1299}
1300
Chris Wilson99b5ee82015-01-22 10:03:45 +00001301static void do_gpu_read_after_write(struct buffers *buffers,
Chris Wilson16bafdf2014-09-04 09:26:24 +01001302 do_copy do_copy_func,
1303 do_hang do_hang_func)
Daniel Vetter5a598c92013-08-14 15:08:05 +02001304{
Chris Wilson0a1fc452016-09-13 11:13:14 +01001305 igt_hang_t hang;
Daniel Vetter5a598c92013-08-14 15:08:05 +02001306 int i;
1307
1308 gem_quiescent_gpu(fd);
Chris Wilson99b5ee82015-01-22 10:03:45 +00001309 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001310 buffers->mode->set_bo(buffers, buffers->src[i], 0xabcdabcd);
Chris Wilson99b5ee82015-01-22 10:03:45 +00001311 for (i = 0; i < buffers->count; i++)
Chris Wilson37f4da02016-01-27 13:02:35 +00001312 do_copy_func(buffers, buffers->dst[i], buffers->src[i]);
Chris Wilson99b5ee82015-01-22 10:03:45 +00001313 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001314 do_copy_func(buffers, buffers->spare, buffers->dst[i]);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001315 hang = do_hang_func();
Chris Wilson99b5ee82015-01-22 10:03:45 +00001316 for (i = buffers->count; i--; )
Chris Wilson37f4da02016-01-27 13:02:35 +00001317 buffers->mode->cmp_bo(buffers, buffers->dst[i], 0xabcdabcd);
Chris Wilson16bafdf2014-09-04 09:26:24 +01001318 igt_post_hang_ring(fd, hang);
Daniel Vetter5a598c92013-08-14 15:08:05 +02001319}
1320
Chris Wilson99b5ee82015-01-22 10:03:45 +00001321typedef void (*do_test)(struct buffers *buffers,
Chris Wilson16bafdf2014-09-04 09:26:24 +01001322 do_copy do_copy_func,
1323 do_hang do_hang_func);
Daniel Vetterec283d62013-08-14 15:18:37 +02001324
Chris Wilson99b5ee82015-01-22 10:03:45 +00001325typedef void (*run_wrap)(struct buffers *buffers,
Chris Wilson59c55622014-08-29 13:11:37 +01001326 do_test do_test_func,
Chris Wilson16bafdf2014-09-04 09:26:24 +01001327 do_copy do_copy_func,
1328 do_hang do_hang_func);
Daniel Vetterec283d62013-08-14 15:18:37 +02001329
Chris Wilson99b5ee82015-01-22 10:03:45 +00001330static void run_single(struct buffers *buffers,
Chris Wilson59c55622014-08-29 13:11:37 +01001331 do_test do_test_func,
Chris Wilson16bafdf2014-09-04 09:26:24 +01001332 do_copy do_copy_func,
1333 do_hang do_hang_func)
Daniel Vetterec283d62013-08-14 15:18:37 +02001334{
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001335 pass = 0;
Chris Wilson99b5ee82015-01-22 10:03:45 +00001336 do_test_func(buffers, do_copy_func, do_hang_func);
Chris Wilson5b675f72016-01-22 17:33:40 +00001337 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
Daniel Vetterec283d62013-08-14 15:18:37 +02001338}
1339
Chris Wilson99b5ee82015-01-22 10:03:45 +00001340static void run_interruptible(struct buffers *buffers,
Chris Wilson59c55622014-08-29 13:11:37 +01001341 do_test do_test_func,
Chris Wilson16bafdf2014-09-04 09:26:24 +01001342 do_copy do_copy_func,
1343 do_hang do_hang_func)
Daniel Vetterec283d62013-08-14 15:18:37 +02001344{
Chris Wilson1c61c0f2016-01-08 10:51:09 +00001345 pass = 0;
Daniel Vetterd7050f92016-05-11 17:06:28 +02001346 igt_while_interruptible(true)
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001347 do_test_func(buffers, do_copy_func, do_hang_func);
Chris Wilson5b675f72016-01-22 17:33:40 +00001348 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
Daniel Vetterec283d62013-08-14 15:18:37 +02001349}
1350
Chris Wilson46456302016-01-22 19:29:07 +00001351static void run_child(struct buffers *buffers,
1352 do_test do_test_func,
1353 do_copy do_copy_func,
1354 do_hang do_hang_func)
1355
1356{
Chris Wilson69ecede2016-01-22 22:14:33 +00001357 /* We inherit the buffers from the parent, but the bufmgr/batch
1358 * needs to be local as the cache of reusable itself will be COWed,
1359 * leading to the child closing an object without the parent knowing.
1360 */
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001361 pass = 0;
Chris Wilsona1b47ef2016-01-27 19:44:16 +00001362 igt_fork(child, 1)
Chris Wilson46456302016-01-22 19:29:07 +00001363 do_test_func(buffers, do_copy_func, do_hang_func);
Chris Wilson46456302016-01-22 19:29:07 +00001364 igt_waitchildren();
1365 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
1366}
1367
Chris Wilson2d08e9e2015-12-11 09:25:03 +00001368static void __run_forked(struct buffers *buffers,
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001369 int num_children, int loops, bool interrupt,
Chris Wilson2d08e9e2015-12-11 09:25:03 +00001370 do_test do_test_func,
1371 do_copy do_copy_func,
1372 do_hang do_hang_func)
1373
Daniel Vetterec283d62013-08-14 15:18:37 +02001374{
Chris Wilsonc46f3c32016-04-10 20:44:58 +01001375 /* purge the libdrm caches before cloing the process */
Chris Wilsonbca3ab22016-09-23 07:39:57 +01001376 buffers_destroy(buffers);
1377 intel_batchbuffer_free(buffers->batch);
1378 drm_intel_bufmgr_destroy(buffers->bufmgr);
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001379
Chris Wilson2d08e9e2015-12-11 09:25:03 +00001380 igt_fork(child, num_children) {
Chris Wilson459ff6b2016-04-20 07:49:02 +01001381 int num_buffers;
1382
Daniel Vettercd1f2202013-08-29 10:06:51 +02001383 /* recreate process local variables */
Micah Fedkec81d2932015-07-22 21:54:02 +00001384 fd = drm_open_driver(DRIVER_INTEL);
Chris Wilsonf2a045f2015-01-02 16:33:33 +05301385
Chris Wilson459ff6b2016-04-20 07:49:02 +01001386 num_buffers = buffers->num_buffers / num_children;
1387 num_buffers += MIN_BUFFERS;
1388 if (num_buffers < buffers->num_buffers)
1389 buffers->num_buffers = num_buffers;
Chris Wilsonc46f3c32016-04-10 20:44:58 +01001390
1391 buffers_reset(buffers, true);
Chris Wilson094e0cb2016-03-01 13:22:03 +00001392 buffers_create(buffers);
Daniel Vettercd1f2202013-08-29 10:06:51 +02001393
Daniel Vetterd7050f92016-05-11 17:06:28 +02001394 igt_while_interruptible(interrupt) {
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001395 for (pass = 0; pass < loops; pass++)
1396 do_test_func(buffers,
1397 do_copy_func,
1398 do_hang_func);
1399 }
1400 }
Daniel Vettercd1f2202013-08-29 10:06:51 +02001401 igt_waitchildren();
Chris Wilson5b675f72016-01-22 17:33:40 +00001402 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
Chris Wilsonbca3ab22016-09-23 07:39:57 +01001403
1404 buffers_reset(buffers, true);
Daniel Vetterec283d62013-08-14 15:18:37 +02001405}
Daniel Vetter5a598c92013-08-14 15:08:05 +02001406
Chris Wilson2d08e9e2015-12-11 09:25:03 +00001407static void run_forked(struct buffers *buffers,
1408 do_test do_test_func,
1409 do_copy do_copy_func,
1410 do_hang do_hang_func)
1411{
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001412 const int ncpus = sysconf(_SC_NPROCESSORS_ONLN);
1413 __run_forked(buffers, ncpus, ncpus, false,
Chris Wilson2d08e9e2015-12-11 09:25:03 +00001414 do_test_func, do_copy_func, do_hang_func);
1415}
1416
1417static void run_bomb(struct buffers *buffers,
1418 do_test do_test_func,
1419 do_copy do_copy_func,
1420 do_hang do_hang_func)
1421{
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001422 const int ncpus = sysconf(_SC_NPROCESSORS_ONLN);
1423 __run_forked(buffers, 8*ncpus, 2, true,
Chris Wilson2d08e9e2015-12-11 09:25:03 +00001424 do_test_func, do_copy_func, do_hang_func);
1425}
1426
Chris Wilsonf2a045f2015-01-02 16:33:33 +05301427static void cpu_require(void)
1428{
1429 bit17_require();
1430}
1431
1432static void gtt_require(void)
1433{
1434}
1435
Chris Wilson08188752014-09-03 13:38:30 +01001436static void bcs_require(void)
1437{
1438}
1439
1440static void rcs_require(void)
1441{
1442 igt_require(rendercopy);
1443}
1444
Daniel Vetter5a598c92013-08-14 15:08:05 +02001445static void
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001446run_mode(const char *prefix,
1447 const struct create *create,
1448 const struct access_mode *mode,
1449 const struct size *size,
1450 const int num_buffers,
1451 const char *suffix,
1452 run_wrap run_wrap_func)
Daniel Vetter5a598c92013-08-14 15:08:05 +02001453{
Chris Wilsonf2a045f2015-01-02 16:33:33 +05301454 const struct {
Chris Wilson59c55622014-08-29 13:11:37 +01001455 const char *prefix;
1456 do_copy copy;
Chris Wilson08188752014-09-03 13:38:30 +01001457 void (*require)(void);
Chris Wilson59c55622014-08-29 13:11:37 +01001458 } pipelines[] = {
Chris Wilsonf2a045f2015-01-02 16:33:33 +05301459 { "cpu", cpu_copy_bo, cpu_require },
1460 { "gtt", gtt_copy_bo, gtt_require },
1461 { "wc", wc_copy_bo, wc_require },
Daniel Vetter3e9b4e32015-02-06 23:10:26 +01001462 { "blt", blt_copy_bo, bcs_require },
1463 { "render", render_copy_bo, rcs_require },
Chris Wilson59c55622014-08-29 13:11:37 +01001464 { NULL, NULL }
Chris Wilson77633492015-03-26 08:11:43 +00001465 }, *pskip = pipelines + 3, *p;
Chris Wilson16bafdf2014-09-04 09:26:24 +01001466 const struct {
1467 const char *suffix;
1468 do_hang hang;
Chris Wilson16bafdf2014-09-04 09:26:24 +01001469 } hangs[] = {
Chris Wilson92caf132015-12-16 09:23:56 +00001470 { "", no_hang },
1471 { "-hang-blt", bcs_hang },
1472 { "-hang-render", rcs_hang },
Chris Wilsonfcb4cfa2016-03-20 11:38:26 +00001473 { "-hang-all", all_hang },
Chris Wilson16bafdf2014-09-04 09:26:24 +01001474 { NULL, NULL },
1475 }, *h;
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001476 struct buffers buffers;
1477
1478 igt_fixture
1479 buffers_init(&buffers, prefix, create, mode,
1480 size, num_buffers,
1481 fd, run_wrap_func != run_child);
Daniel Vetter5a598c92013-08-14 15:08:05 +02001482
Chris Wilson16bafdf2014-09-04 09:26:24 +01001483 for (h = hangs; h->suffix; h++) {
Chris Wilson77633492015-03-26 08:11:43 +00001484 if (!all && *h->suffix)
1485 continue;
1486
Chris Wilson6867b872016-03-24 07:57:30 +00001487 if (!*h->suffix)
Daniel Vetterbe21fc02016-06-17 16:04:09 +02001488 igt_fixture
1489 igt_fork_hang_detector(fd);
Chris Wilson6867b872016-03-24 07:57:30 +00001490
Chris Wilson77633492015-03-26 08:11:43 +00001491 for (p = all ? pipelines : pskip; p->prefix; p++) {
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001492 igt_subtest_group {
1493 igt_fixture p->require();
Chris Wilson16bafdf2014-09-04 09:26:24 +01001494
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001495 igt_subtest_f("%s-%s-%s-sanitycheck0%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1496 buffers_create(&buffers);
1497 run_wrap_func(&buffers, do_basic0,
1498 p->copy, h->hang);
1499 }
Chris Wilson8bf09f32015-12-17 09:16:42 +00001500
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001501 igt_subtest_f("%s-%s-%s-sanitycheck1%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1502 buffers_create(&buffers);
1503 run_wrap_func(&buffers, do_basic1,
1504 p->copy, h->hang);
1505 }
Chris Wilson8bf09f32015-12-17 09:16:42 +00001506
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001507 igt_subtest_f("%s-%s-%s-sanitycheckN%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1508 buffers_create(&buffers);
1509 run_wrap_func(&buffers, do_basicN,
1510 p->copy, h->hang);
1511 }
Chris Wilson197db862015-12-09 20:54:10 +00001512
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001513 /* try to overwrite the source values */
1514 igt_subtest_f("%s-%s-%s-overwrite-source-one%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1515 buffers_create(&buffers);
1516 run_wrap_func(&buffers,
1517 do_overwrite_source__one,
1518 p->copy, h->hang);
1519 }
Chris Wilson16bafdf2014-09-04 09:26:24 +01001520
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001521 igt_subtest_f("%s-%s-%s-overwrite-source%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1522 buffers_create(&buffers);
1523 run_wrap_func(&buffers,
1524 do_overwrite_source,
1525 p->copy, h->hang);
1526 }
Chris Wilsona1726762015-03-16 16:29:57 +00001527
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001528 igt_subtest_f("%s-%s-%s-overwrite-source-read-bcs%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1529 buffers_create(&buffers);
1530 run_wrap_func(&buffers,
1531 do_overwrite_source_read_bcs,
1532 p->copy, h->hang);
1533 }
Chris Wilsona1726762015-03-16 16:29:57 +00001534
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001535 igt_subtest_f("%s-%s-%s-overwrite-source-read-rcs%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1536 igt_require(rendercopy);
1537 buffers_create(&buffers);
1538 run_wrap_func(&buffers,
1539 do_overwrite_source_read_rcs,
1540 p->copy, h->hang);
1541 }
Chris Wilsona1726762015-03-16 16:29:57 +00001542
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001543 igt_subtest_f("%s-%s-%s-overwrite-source-rev%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1544 buffers_create(&buffers);
1545 run_wrap_func(&buffers,
1546 do_overwrite_source__rev,
1547 p->copy, h->hang);
1548 }
Chris Wilson16bafdf2014-09-04 09:26:24 +01001549
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001550 /* try to intermix copies with GPU copies*/
1551 igt_subtest_f("%s-%s-%s-intermix-rcs%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1552 igt_require(rendercopy);
1553 buffers_create(&buffers);
1554 run_wrap_func(&buffers,
1555 do_intermix_rcs,
1556 p->copy, h->hang);
1557 }
1558 igt_subtest_f("%s-%s-%s-intermix-bcs%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1559 igt_require(rendercopy);
1560 buffers_create(&buffers);
1561 run_wrap_func(&buffers,
1562 do_intermix_bcs,
1563 p->copy, h->hang);
1564 }
1565 igt_subtest_f("%s-%s-%s-intermix-both%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1566 igt_require(rendercopy);
1567 buffers_create(&buffers);
1568 run_wrap_func(&buffers,
1569 do_intermix_both,
1570 p->copy, h->hang);
1571 }
Chris Wilsona72d4052015-03-18 14:15:22 +00001572
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001573 /* try to read the results before the copy completes */
1574 igt_subtest_f("%s-%s-%s-early-read%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1575 buffers_create(&buffers);
1576 run_wrap_func(&buffers,
1577 do_early_read,
1578 p->copy, h->hang);
1579 }
Chris Wilson16bafdf2014-09-04 09:26:24 +01001580
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001581 /* concurrent reads */
1582 igt_subtest_f("%s-%s-%s-read-read-bcs%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1583 buffers_create(&buffers);
1584 run_wrap_func(&buffers,
1585 do_read_read_bcs,
1586 p->copy, h->hang);
1587 }
1588 igt_subtest_f("%s-%s-%s-read-read-rcs%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1589 igt_require(rendercopy);
1590 buffers_create(&buffers);
1591 run_wrap_func(&buffers,
1592 do_read_read_rcs,
1593 p->copy, h->hang);
1594 }
Chris Wilson35b0ac92015-03-16 11:55:46 +00001595
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001596 /* split copying between rings */
1597 igt_subtest_f("%s-%s-%s-write-read-bcs%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1598 buffers_create(&buffers);
1599 run_wrap_func(&buffers,
1600 do_write_read_bcs,
1601 p->copy, h->hang);
1602 }
1603 igt_subtest_f("%s-%s-%s-write-read-rcs%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1604 igt_require(rendercopy);
1605 buffers_create(&buffers);
1606 run_wrap_func(&buffers,
1607 do_write_read_rcs,
1608 p->copy, h->hang);
1609 }
Chris Wilson0c266522015-11-11 16:37:16 +00001610
Chris Wilsonb2e8d2c2016-07-27 23:21:45 +01001611 /* and finally try to trick the kernel into loosing the pending write */
1612 igt_subtest_f("%s-%s-%s-gpu-read-after-write%s%s", prefix, mode->name, p->prefix, suffix, h->suffix) {
1613 buffers_create(&buffers);
1614 run_wrap_func(&buffers,
1615 do_gpu_read_after_write,
1616 p->copy, h->hang);
1617 }
Chris Wilson16bafdf2014-09-04 09:26:24 +01001618 }
Chris Wilson08188752014-09-03 13:38:30 +01001619 }
Chris Wilson6867b872016-03-24 07:57:30 +00001620
1621 if (!*h->suffix)
Daniel Vetterbe21fc02016-06-17 16:04:09 +02001622 igt_fixture
1623 igt_stop_hang_detector();
Chris Wilson59c55622014-08-29 13:11:37 +01001624 }
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001625
1626 igt_fixture
1627 buffers_fini(&buffers);
Daniel Vetter5a598c92013-08-14 15:08:05 +02001628}
Daniel Vetter43779e32013-08-14 14:50:50 +02001629
1630static void
Chris Wilson094e0cb2016-03-01 13:22:03 +00001631run_modes(const char *style,
Chris Wilson4eba8e22016-03-18 10:44:31 +00001632 const struct create *create,
Chris Wilson094e0cb2016-03-01 13:22:03 +00001633 const struct access_mode *mode,
Chris Wilson5d669bf2016-03-18 14:44:53 +00001634 const struct size *size,
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001635 const int num)
Daniel Vetter43779e32013-08-14 14:50:50 +02001636{
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001637 const struct wrap {
1638 const char *suffix;
1639 run_wrap func;
1640 } wrappers[] = {
1641 { "", run_single },
1642 { "-child", run_child },
1643 { "-forked", run_forked },
1644 { "-interruptible", run_interruptible },
1645 { "-bomb", run_bomb },
1646 { NULL },
1647 };
Daniel Vetter3dba47e2013-08-06 22:27:37 +02001648
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001649 while (mode->name) {
1650 igt_subtest_group {
1651 igt_fixture {
1652 if (mode->require)
Chris Wilsone85613b2016-03-19 14:01:38 +00001653 mode->require(create, num);
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001654 }
1655
1656 for (const struct wrap *w = wrappers; w->suffix; w++) {
1657 run_mode(style, create, mode, size, num,
1658 w->suffix, w->func);
1659 }
Daniel Vetter96650732016-03-18 21:55:00 +01001660 }
1661
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001662 mode++;
Daniel Vetter96650732016-03-18 21:55:00 +01001663 }
Daniel Vetter43779e32013-08-14 14:50:50 +02001664}
1665
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001666static unsigned
Chris Wilsonf338e982016-03-19 13:10:17 +00001667num_buffers(uint64_t max,
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001668 const struct size *s,
1669 const struct create *c,
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001670 unsigned allow_mem)
1671{
1672 unsigned size = 4*s->width*s->height;
Chris Wilson9e7e7c32016-04-11 09:17:33 +01001673 uint64_t n;
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001674
Chris Wilson9e7e7c32016-04-11 09:17:33 +01001675 igt_assert(size);
1676 n = max / (2*size);
1677 n += MIN_BUFFERS;
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001678
Chris Wilson9e7e7c32016-04-11 09:17:33 +01001679 igt_require(n < INT32_MAX);
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001680 igt_require(set_max_map_count(2*n));
1681
Chris Wilsone85613b2016-03-19 14:01:38 +00001682 if (c->require)
1683 c->require(c, n);
1684
Chris Wilson6867b872016-03-24 07:57:30 +00001685 intel_require_memory(2*n, size, allow_mem);
1686
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001687 return n;
1688}
1689
Chris Wilson3d8af562016-03-20 10:49:54 +00001690static bool allow_unlimited_files(void)
1691{
1692 struct rlimit rlim;
1693 unsigned nofile_rlim = 1024*1024;
1694
1695 FILE *file = fopen("/proc/sys/fs/file-max", "r");
1696 if (file) {
1697 igt_assert(fscanf(file, "%u", &nofile_rlim) == 1);
1698 igt_info("System limit for open files is %u\n", nofile_rlim);
1699 fclose(file);
1700 }
1701
1702 if (getrlimit(RLIMIT_NOFILE, &rlim))
1703 return false;
1704
1705 rlim.rlim_cur = nofile_rlim;
1706 rlim.rlim_max = nofile_rlim;
1707 return setrlimit(RLIMIT_NOFILE, &rlim) == 0;
1708}
1709
Daniel Vetter071e9ca2013-10-31 16:23:26 +01001710igt_main
Daniel Vetter43779e32013-08-14 14:50:50 +02001711{
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001712 const struct access_mode modes[] = {
1713 {
1714 .name = "prw",
1715 .create_bo = unmapped_create_bo,
1716 .set_bo = prw_set_bo,
1717 .cmp_bo = prw_cmp_bo,
1718 .release_bo = nop_release_bo,
1719 },
1720 {
1721 .name = "partial",
1722 .create_bo = unmapped_create_bo,
1723 .set_bo = partial_set_bo,
1724 .cmp_bo = partial_cmp_bo,
1725 .release_bo = nop_release_bo,
1726 },
1727 {
1728 .name = "cpu",
1729 .create_bo = unmapped_create_bo,
1730 .require = create_cpu_require,
1731 .set_bo = cpu_set_bo,
1732 .cmp_bo = cpu_cmp_bo,
1733 .release_bo = nop_release_bo,
1734 },
1735 {
1736 .name = "snoop",
1737 .create_bo = snoop_create_bo,
1738 .require = create_snoop_require,
1739 .set_bo = cpu_set_bo,
1740 .cmp_bo = cpu_cmp_bo,
1741 .release_bo = nop_release_bo,
1742 },
1743 {
1744 .name = "userptr",
1745 .create_bo = userptr_create_bo,
1746 .require = create_userptr_require,
1747 .set_bo = userptr_set_bo,
1748 .cmp_bo = userptr_cmp_bo,
1749 .release_bo = userptr_release_bo,
1750 },
1751 {
1752 .name = "dmabuf",
1753 .create_bo = dmabuf_create_bo,
1754 .require = create_dmabuf_require,
1755 .set_bo = dmabuf_set_bo,
1756 .cmp_bo = dmabuf_cmp_bo,
1757 .release_bo = dmabuf_release_bo,
1758 },
1759 {
Chris Wilsone7ed4ef2016-07-18 11:38:09 +01001760 .name = "vgem",
1761 .create_bo = vgem_create_bo,
1762 .require = create_vgem_require,
1763 .set_bo = dmabuf_set_bo,
1764 .cmp_bo = dmabuf_cmp_bo,
1765 .release_bo = dmabuf_release_bo,
1766 },
1767 {
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001768 .name = "gtt",
1769 .create_bo = gtt_create_bo,
1770 .set_bo = gtt_set_bo,
1771 .cmp_bo = gtt_cmp_bo,
1772 .release_bo = nop_release_bo,
1773 },
1774 {
1775 .name = "gttX",
1776 .create_bo = gttX_create_bo,
1777 .set_bo = gtt_set_bo,
1778 .cmp_bo = gtt_cmp_bo,
1779 .release_bo = nop_release_bo,
1780 },
1781 {
1782 .name = "wc",
1783 .require = wc_create_require,
1784 .create_bo = wc_create_bo,
1785 .set_bo = gtt_set_bo,
1786 .cmp_bo = gtt_cmp_bo,
1787 .release_bo = wc_release_bo,
1788 },
1789 {
1790 .name = "gpu",
1791 .create_bo = gpu_create_bo,
1792 .set_bo = gpu_set_bo,
1793 .cmp_bo = gpu_cmp_bo,
1794 .release_bo = nop_release_bo,
1795 },
1796 {
1797 .name = "gpuX",
1798 .create_bo = gpuX_create_bo,
1799 .set_bo = gpu_set_bo,
1800 .cmp_bo = gpu_cmp_bo,
1801 .release_bo = nop_release_bo,
1802 },
1803 { NULL },
1804 };
Chris Wilson4eba8e22016-03-18 10:44:31 +00001805 const struct create create[] = {
1806 { "", can_create_normal, create_normal_bo},
1807#if HAVE_CREATE_PRIVATE
1808 { "private-", can_create_private, create_private_bo},
1809#endif
1810#if HAVE_CREATE_STOLEN
1811 { "stolen-", can_create_stolen, create_stolen_bo},
1812#endif
Chris Wilson1d6e5d32016-01-03 13:44:17 +00001813 { NULL, NULL }
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001814 };
Chris Wilson5d669bf2016-03-18 14:44:53 +00001815 const struct size sizes[] = {
1816 { "4KiB", 128, 8 },
1817 { "256KiB", 128, 128 },
1818 { "1MiB", 512, 512 },
1819 { "16MiB", 2048, 2048 },
1820 { NULL}
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001821 };
Chris Wilson42291f22016-01-07 11:19:26 +00001822 uint64_t pin_sz = 0;
1823 void *pinned = NULL;
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001824 char name[80];
1825 int count = 0;
Daniel Vetter43779e32013-08-14 14:50:50 +02001826
Daniel Vetter43779e32013-08-14 14:50:50 +02001827 igt_skip_on_simulation();
1828
Chris Wilson77633492015-03-26 08:11:43 +00001829 if (strstr(igt_test_name(), "all"))
1830 all = true;
1831
Daniel Vetter2dbd9982013-08-14 15:48:54 +02001832 igt_fixture {
Chris Wilson98dcf2f2016-03-25 00:50:45 +00001833 allow_unlimited_files();
Chris Wilson3d8af562016-03-20 10:49:54 +00001834
Micah Fedkec81d2932015-07-22 21:54:02 +00001835 fd = drm_open_driver(DRIVER_INTEL);
Chris Wilson9518cb52017-02-22 15:24:54 +00001836 igt_require_gem(fd);
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
Chris Wilsone8eb9af2017-03-08 13:09:00 +00001901 snprintf(name, sizeof(name), "%s%s-%s",
1902 c->name, s->name, "shrink");
1903 igt_subtest_group {
1904 igt_fixture {
1905 count = num_buffers(gem_mappable_aperture_size(),
1906 s, c, CHECK_RAM);
1907
Chris Wilson83884e92017-03-21 17:16:03 +00001908 igt_fork_shrink_helper(fd);
Chris Wilsone8eb9af2017-03-08 13:09:00 +00001909 }
1910 run_modes(name, c, modes, s, count);
1911
1912 igt_fixture
1913 igt_stop_shrink_helper();
1914 }
1915
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001916 /* Use the entire mappable aperture, force swapping */
Chris Wilsonf338e982016-03-19 13:10:17 +00001917 snprintf(name, sizeof(name), "%s%s-%s",
1918 c->name, s->name, "swap");
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001919 igt_subtest_group {
1920 igt_fixture {
1921 if (intel_get_avail_ram_mb() > gem_mappable_aperture_size()/(1024*1024)) {
1922 pin_sz = intel_get_avail_ram_mb() - gem_mappable_aperture_size()/(1024*1024);
1923
1924 igt_debug("Pinning %lld MiB\n", (long long)pin_sz);
1925 pin_sz *= 1024 * 1024;
1926
1927 if (posix_memalign(&pinned, 4096, pin_sz) ||
1928 mlock(pinned, pin_sz) ||
1929 madvise(pinned, pin_sz, MADV_DONTFORK)) {
1930 free(pinned);
1931 pinned = NULL;
1932 }
1933 igt_require(pinned);
1934 }
1935
Chris Wilsonf338e982016-03-19 13:10:17 +00001936 count = num_buffers(gem_mappable_aperture_size(),
1937 s, c, CHECK_RAM | CHECK_SWAP);
Chris Wilsonc2248ef2016-03-19 13:10:17 +00001938 }
1939 run_modes(name, c, modes, s, count);
1940
1941 igt_fixture {
1942 if (pinned) {
1943 munlock(pinned, pin_sz);
1944 free(pinned);
1945 pinned = NULL;
1946 }
Chris Wilson5d669bf2016-03-18 14:44:53 +00001947 }
Chris Wilson42291f22016-01-07 11:19:26 +00001948 }
Chris Wilson1d6e5d32016-01-03 13:44:17 +00001949 }
Daniel Vetter2dbd9982013-08-14 15:48:54 +02001950 }
Daniel Vetter3dba47e2013-08-06 22:27:37 +02001951}