lib/igt_kms: Unify pipe name helpers
[platform/upstream/intel-gpu-tools.git] / tests / gem_userptr_blits.c
1 /*
2  * Copyright © 2009-2014 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  *    Tvrtko Ursulin <tvrtko.ursulin@intel.com>
27  *
28  */
29
30 /** @file gem_userptr_blits.c
31  *
32  * This is a test of doing many blits using a mixture of normal system pages
33  * and uncached linear buffers, with a working set larger than the
34  * aperture size.
35  *
36  * The goal is to simply ensure the basics work.
37  */
38
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <fcntl.h>
43 #include <inttypes.h>
44 #include <errno.h>
45 #include <sys/stat.h>
46 #include <sys/time.h>
47 #include <sys/mman.h>
48 #include <signal.h>
49 #include <pthread.h>
50
51 #include "drm.h"
52 #include "i915_drm.h"
53
54 #include "drmtest.h"
55 #include "intel_bufmgr.h"
56 #include "intel_batchbuffer.h"
57 #include "intel_chipset.h"
58 #include "ioctl_wrappers.h"
59
60 #include "eviction_common.c"
61
62 #ifndef PAGE_SIZE
63 #define PAGE_SIZE 4096
64 #endif
65
66 #define LOCAL_I915_GEM_USERPTR       0x33
67 #define LOCAL_IOCTL_I915_GEM_USERPTR DRM_IOWR (DRM_COMMAND_BASE + LOCAL_I915_GEM_USERPTR, struct local_i915_gem_userptr)
68 struct local_i915_gem_userptr {
69         uint64_t user_ptr;
70         uint64_t user_size;
71         uint32_t flags;
72 #define LOCAL_I915_USERPTR_READ_ONLY (1<<0)
73 #define LOCAL_I915_USERPTR_UNSYNCHRONIZED (1<<31)
74         uint32_t handle;
75 };
76
77 static uint32_t userptr_flags = LOCAL_I915_USERPTR_UNSYNCHRONIZED;
78
79 #define WIDTH 512
80 #define HEIGHT 512
81
82 static uint32_t linear[WIDTH*HEIGHT];
83
84 static void gem_userptr_test_unsynchronized(void)
85 {
86         userptr_flags = LOCAL_I915_USERPTR_UNSYNCHRONIZED;
87 }
88
89 static void gem_userptr_test_synchronized(void)
90 {
91         userptr_flags = 0;
92 }
93
94 static int gem_userptr(int fd, void *ptr, int size, int read_only, uint32_t *handle)
95 {
96         struct local_i915_gem_userptr userptr;
97         int ret;
98
99         memset(&userptr, 0, sizeof(userptr));
100         userptr.user_ptr = (uintptr_t)ptr;
101         userptr.user_size = size;
102         userptr.flags = userptr_flags;
103         if (read_only)
104                 userptr.flags |= LOCAL_I915_USERPTR_READ_ONLY;
105
106         ret = drmIoctl(fd, LOCAL_IOCTL_I915_GEM_USERPTR, &userptr);
107         if (ret)
108                 ret = errno;
109         igt_skip_on_f(ret == ENODEV &&
110                       (userptr_flags & LOCAL_I915_USERPTR_UNSYNCHRONIZED) == 0 &&
111                       !read_only,
112                       "Skipping, synchronized mappings with no kernel CONFIG_MMU_NOTIFIER?");
113         if (ret == 0)
114                 *handle = userptr.handle;
115
116         return ret;
117 }
118
119
120 static void gem_userptr_sync(int fd, uint32_t handle)
121 {
122         gem_set_domain(fd, handle, I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
123 }
124
125 static void
126 copy(int fd, uint32_t dst, uint32_t src, unsigned int error)
127 {
128         uint32_t batch[12];
129         struct drm_i915_gem_relocation_entry reloc[2];
130         struct drm_i915_gem_exec_object2 obj[3];
131         struct drm_i915_gem_execbuffer2 exec;
132         uint32_t handle;
133         int ret, i=0;
134
135         batch[i++] = XY_SRC_COPY_BLT_CMD |
136                   XY_SRC_COPY_BLT_WRITE_ALPHA |
137                   XY_SRC_COPY_BLT_WRITE_RGB;
138         if (intel_gen(intel_get_drm_devid(fd)) >= 8)
139                 batch[i - 1] |= 8;
140         else
141                 batch[i - 1] |= 6;
142
143         batch[i++] = (3 << 24) | /* 32 bits */
144                   (0xcc << 16) | /* copy ROP */
145                   WIDTH*4;
146         batch[i++] = 0; /* dst x1,y1 */
147         batch[i++] = (HEIGHT << 16) | WIDTH; /* dst x2,y2 */
148         batch[i++] = 0; /* dst reloc */
149         if (intel_gen(intel_get_drm_devid(fd)) >= 8)
150                 batch[i++] = 0;
151         batch[i++] = 0; /* src x1,y1 */
152         batch[i++] = WIDTH*4;
153         batch[i++] = 0; /* src reloc */
154         if (intel_gen(intel_get_drm_devid(fd)) >= 8)
155                 batch[i++] = 0;
156         batch[i++] = MI_BATCH_BUFFER_END;
157         batch[i++] = MI_NOOP;
158
159         handle = gem_create(fd, 4096);
160         gem_write(fd, handle, 0, batch, sizeof(batch));
161
162         reloc[0].target_handle = dst;
163         reloc[0].delta = 0;
164         reloc[0].offset = 4 * sizeof(batch[0]);
165         reloc[0].presumed_offset = 0;
166         reloc[0].read_domains = I915_GEM_DOMAIN_RENDER;
167         reloc[0].write_domain = I915_GEM_DOMAIN_RENDER;
168
169         reloc[1].target_handle = src;
170         reloc[1].delta = 0;
171         reloc[1].offset = 7 * sizeof(batch[0]);
172         if (intel_gen(intel_get_drm_devid(fd)) >= 8)
173                 reloc[1].offset += sizeof(batch[0]);
174         reloc[1].presumed_offset = 0;
175         reloc[1].read_domains = I915_GEM_DOMAIN_RENDER;
176         reloc[1].write_domain = 0;
177
178         memset(obj, 0, sizeof(obj));
179         exec.buffer_count = 0;
180         obj[exec.buffer_count++].handle = dst;
181         if (src != dst)
182                 obj[exec.buffer_count++].handle = src;
183         obj[exec.buffer_count].handle = handle;
184         obj[exec.buffer_count].relocation_count = 2;
185         obj[exec.buffer_count].relocs_ptr = (uintptr_t)reloc;
186         exec.buffer_count++;
187         exec.buffers_ptr = (uintptr_t)obj;
188
189         exec.batch_start_offset = 0;
190         exec.batch_len = i * 4;
191         exec.DR1 = exec.DR4 = 0;
192         exec.num_cliprects = 0;
193         exec.cliprects_ptr = 0;
194         exec.flags = HAS_BLT_RING(intel_get_drm_devid(fd)) ? I915_EXEC_BLT : 0;
195         i915_execbuffer2_set_context_id(exec, 0);
196         exec.rsvd2 = 0;
197
198         ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &exec);
199         if (ret)
200                 ret = errno;
201
202         if (error == ~0)
203                 igt_assert(ret != 0);
204         else
205                 igt_assert(ret == error);
206
207         gem_close(fd, handle);
208 }
209
210 static int
211 blit(int fd, uint32_t dst, uint32_t src, uint32_t *all_bo, int n_bo)
212 {
213         uint32_t batch[12];
214         struct drm_i915_gem_relocation_entry reloc[2];
215         struct drm_i915_gem_exec_object2 *obj;
216         struct drm_i915_gem_execbuffer2 exec;
217         uint32_t handle;
218         int n, ret, i=0;
219
220         batch[i++] = XY_SRC_COPY_BLT_CMD |
221                   XY_SRC_COPY_BLT_WRITE_ALPHA |
222                   XY_SRC_COPY_BLT_WRITE_RGB;
223         if (intel_gen(intel_get_drm_devid(fd)) >= 8)
224                 batch[i - 1] |= 8;
225         else
226                 batch[i - 1] |= 6;
227         batch[i++] = (3 << 24) | /* 32 bits */
228                   (0xcc << 16) | /* copy ROP */
229                   WIDTH*4;
230         batch[i++] = 0; /* dst x1,y1 */
231         batch[i++] = (HEIGHT << 16) | WIDTH; /* dst x2,y2 */
232         batch[i++] = 0; /* dst reloc */
233         if (intel_gen(intel_get_drm_devid(fd)) >= 8)
234                 batch[i++] = 0;
235         batch[i++] = 0; /* src x1,y1 */
236         batch[i++] = WIDTH*4;
237         batch[i++] = 0; /* src reloc */
238         if (intel_gen(intel_get_drm_devid(fd)) >= 8)
239                 batch[i++] = 0;
240         batch[i++] = MI_BATCH_BUFFER_END;
241         batch[i++] = MI_NOOP;
242
243         handle = gem_create(fd, 4096);
244         gem_write(fd, handle, 0, batch, sizeof(batch));
245
246         reloc[0].target_handle = dst;
247         reloc[0].delta = 0;
248         reloc[0].offset = 4 * sizeof(batch[0]);
249         reloc[0].presumed_offset = 0;
250         reloc[0].read_domains = I915_GEM_DOMAIN_RENDER;
251         reloc[0].write_domain = I915_GEM_DOMAIN_RENDER;
252
253         reloc[1].target_handle = src;
254         reloc[1].delta = 0;
255         reloc[1].offset = 7 * sizeof(batch[0]);
256         if (intel_gen(intel_get_drm_devid(fd)) >= 8)
257                 reloc[1].offset += sizeof(batch[0]);
258         reloc[1].presumed_offset = 0;
259         reloc[1].read_domains = I915_GEM_DOMAIN_RENDER;
260         reloc[1].write_domain = 0;
261
262         obj = calloc(n_bo + 1, sizeof(*obj));
263         for (n = 0; n < n_bo; n++)
264                 obj[n].handle = all_bo[n];
265         obj[n].handle = handle;
266         obj[n].relocation_count = 2;
267         obj[n].relocs_ptr = (uintptr_t)reloc;
268
269         exec.buffers_ptr = (uintptr_t)obj;
270         exec.buffer_count = n_bo + 1;
271         exec.batch_start_offset = 0;
272         exec.batch_len = i * 4;
273         exec.DR1 = exec.DR4 = 0;
274         exec.num_cliprects = 0;
275         exec.cliprects_ptr = 0;
276         exec.flags = HAS_BLT_RING(intel_get_drm_devid(fd)) ? I915_EXEC_BLT : 0;
277         i915_execbuffer2_set_context_id(exec, 0);
278         exec.rsvd2 = 0;
279
280         ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &exec);
281         if (ret)
282                 ret = errno;
283
284         gem_close(fd, handle);
285         free(obj);
286
287         return ret;
288 }
289
290 static uint32_t
291 create_userptr(int fd, uint32_t val, uint32_t *ptr)
292 {
293         uint32_t handle;
294         int i, ret;
295
296         ret = gem_userptr(fd, ptr, sizeof(linear), 0, &handle);
297         igt_assert(ret == 0);
298         igt_assert(handle != 0);
299
300         /* Fill the BO with dwords starting at val */
301         for (i = 0; i < WIDTH*HEIGHT; i++)
302                 ptr[i] = val++;
303
304         return handle;
305 }
306
307 static void **handle_ptr_map;
308 static unsigned *handle_size_map;
309 static unsigned int num_handle_map;
310
311 static void reset_handle_ptr(void)
312 {
313         if (num_handle_map == 0)
314                 return;
315
316         free(handle_ptr_map);
317         handle_ptr_map = NULL;
318
319         free(handle_size_map);
320         handle_size_map = NULL;
321
322         num_handle_map = 0;
323 }
324
325 static void add_handle_ptr(uint32_t handle, void *ptr, int size)
326 {
327         if (handle >= num_handle_map) {
328                 int max = (4096 + handle) & -4096;
329
330                 handle_ptr_map = realloc(handle_ptr_map,
331                                          max * sizeof(void*));
332                 igt_assert(handle_ptr_map);
333                 memset(handle_ptr_map + num_handle_map, 0,
334                        (max - num_handle_map) * sizeof(void*));
335
336                 handle_size_map = realloc(handle_size_map,
337                                           max * sizeof(unsigned));
338                 igt_assert(handle_size_map);
339                 memset(handle_ptr_map + num_handle_map, 0,
340                        (max - num_handle_map) * sizeof(unsigned));
341
342                 num_handle_map = max;
343         }
344
345         handle_ptr_map[handle] = ptr;
346         handle_size_map[handle] = size;
347 }
348
349 static void *get_handle_ptr(uint32_t handle)
350 {
351         igt_assert(handle < num_handle_map);
352         return handle_ptr_map[handle];
353 }
354
355 static void free_handle_ptr(uint32_t handle)
356 {
357         igt_assert(handle < num_handle_map);
358         igt_assert(handle_ptr_map[handle]);
359
360         munmap(handle_ptr_map[handle], handle_size_map[handle]);
361         handle_ptr_map[handle] = NULL;
362 }
363
364 static uint32_t create_userptr_bo(int fd, int size)
365 {
366         void *ptr;
367         uint32_t handle;
368         int ret;
369
370         ptr = mmap(NULL, size,
371                    PROT_READ | PROT_WRITE,
372                    MAP_ANONYMOUS | MAP_SHARED,
373                    -1, 0);
374         igt_assert(ptr != MAP_FAILED);
375
376         ret = gem_userptr(fd, (uint32_t *)ptr, size, 0, &handle);
377         igt_assert(ret == 0);
378         add_handle_ptr(handle, ptr, size);
379
380         return handle;
381 }
382
383 static void flink_userptr_bo(uint32_t old_handle, uint32_t new_handle)
384 {
385         igt_assert(old_handle < num_handle_map);
386         igt_assert(handle_ptr_map[old_handle]);
387
388         add_handle_ptr(new_handle,
389                        handle_ptr_map[old_handle],
390                        handle_size_map[old_handle]);
391 }
392
393 static void clear(int fd, uint32_t handle, int size)
394 {
395         void *ptr = get_handle_ptr(handle);
396
397         igt_assert(ptr != NULL);
398
399         memset(ptr, 0, size);
400 }
401
402 static void free_userptr_bo(int fd, uint32_t handle)
403 {
404         gem_close(fd, handle);
405         free_handle_ptr(handle);
406 }
407
408 static uint32_t
409 create_bo(int fd, uint32_t val)
410 {
411         uint32_t handle;
412         int i;
413
414         handle = gem_create(fd, sizeof(linear));
415
416         /* Fill the BO with dwords starting at val */
417         for (i = 0; i < WIDTH*HEIGHT; i++)
418                 linear[i] = val++;
419         gem_write(fd, handle, 0, linear, sizeof(linear));
420
421         return handle;
422 }
423
424 static void
425 check_cpu(uint32_t *ptr, uint32_t val)
426 {
427         int i;
428
429         for (i = 0; i < WIDTH*HEIGHT; i++) {
430                 igt_assert_f(ptr[i] == val,
431                              "Expected 0x%08x, found 0x%08x "
432                              "at offset 0x%08x\n",
433                              val, ptr[i], i * 4);
434                 val++;
435         }
436 }
437
438 static void
439 check_gpu(int fd, uint32_t handle, uint32_t val)
440 {
441         gem_read(fd, handle, 0, linear, sizeof(linear));
442         check_cpu(linear, val);
443 }
444
445 static int has_userptr(int fd)
446 {
447         uint32_t handle = 0;
448         void *ptr;
449         uint32_t oldflags;
450         int ret;
451
452         igt_assert(posix_memalign(&ptr, PAGE_SIZE, PAGE_SIZE) == 0);
453         oldflags = userptr_flags;
454         gem_userptr_test_unsynchronized();
455         ret = gem_userptr(fd, ptr, PAGE_SIZE, 0, &handle);
456         userptr_flags = oldflags;
457         if (ret != 0) {
458                 free(ptr);
459                 return 0;
460         }
461
462         gem_close(fd, handle);
463         free(ptr);
464
465         return handle != 0;
466 }
467
468 static int test_input_checking(int fd)
469 {
470         struct local_i915_gem_userptr userptr;
471         int ret;
472
473         /* Invalid flags. */
474         memset(&userptr, 0, sizeof(userptr));
475         userptr.user_ptr = 0;
476         userptr.user_size = 0;
477         userptr.flags = ~0;
478         ret = drmIoctl(fd, LOCAL_IOCTL_I915_GEM_USERPTR, &userptr);
479         igt_assert(ret != 0);
480
481         /* Too big. */
482         memset(&userptr, 0, sizeof(userptr));
483         userptr.user_ptr = 0;
484         userptr.user_size = ~0;
485         userptr.flags = 0;
486         ret = drmIoctl(fd, LOCAL_IOCTL_I915_GEM_USERPTR, &userptr);
487         igt_assert(ret != 0);
488
489         /* Both wrong. */
490         memset(&userptr, 0, sizeof(userptr));
491         userptr.user_ptr = 0;
492         userptr.user_size = ~0;
493         userptr.flags = ~0;
494         ret = drmIoctl(fd, LOCAL_IOCTL_I915_GEM_USERPTR, &userptr);
495         igt_assert(ret != 0);
496
497         return 0;
498 }
499
500 static int test_access_control(int fd)
501 {
502         igt_fork(child, 1) {
503                 void *ptr;
504                 int ret;
505                 uint32_t handle;
506
507                 igt_drop_root();
508
509                 /* CAP_SYS_ADMIN is needed for UNSYNCHRONIZED mappings. */
510                 gem_userptr_test_unsynchronized();
511
512                 igt_assert(posix_memalign(&ptr, PAGE_SIZE, PAGE_SIZE) == 0);
513
514                 ret = gem_userptr(fd, ptr, PAGE_SIZE, 0, &handle);
515                 if (ret == 0)
516                         gem_close(fd, handle);
517                 free(ptr);
518                 igt_assert(ret == EPERM);
519         }
520
521         igt_waitchildren();
522
523         return 0;
524 }
525
526 static int test_invalid_null_pointer(int fd)
527 {
528         uint32_t handle;
529         int ret;
530
531         /* NULL pointer. */
532         ret = gem_userptr(fd, NULL, PAGE_SIZE, 0, &handle);
533         igt_assert(ret == 0);
534
535         copy(fd, handle, handle, ~0); /* QQQ Precise errno? */
536         gem_close(fd, handle);
537
538         return 0;
539 }
540
541 static int test_invalid_gtt_mapping(int fd)
542 {
543         uint32_t handle, handle2;
544         void *ptr;
545         int ret;
546
547         /* GTT mapping */
548         handle = create_bo(fd, 0);
549         ptr = gem_mmap__gtt(fd, handle, sizeof(linear), PROT_READ | PROT_WRITE);
550         gem_close(fd, handle);
551         igt_assert(ptr != NULL);
552         igt_assert(((unsigned long)ptr & (PAGE_SIZE - 1)) == 0);
553         igt_assert((sizeof(linear) & (PAGE_SIZE - 1)) == 0);
554
555         ret = gem_userptr(fd, ptr, sizeof(linear), 0, &handle2);
556         igt_assert(ret == 0);
557         copy(fd, handle2, handle2, ~0); /* QQQ Precise errno? */
558         gem_close(fd, handle2);
559
560         munmap(ptr, sizeof(linear));
561
562         return 0;
563 }
564
565 #define PE_GTT_MAP 0x1
566 #define PE_BUSY 0x2
567 static void test_process_exit(int fd, int flags)
568 {
569         igt_fork(child, 1) {
570                 uint32_t handle;
571
572                 handle = create_userptr_bo(fd, sizeof(linear));
573
574                 if (flags & PE_GTT_MAP) {
575                         uint32_t *ptr = gem_mmap__gtt(fd, handle, sizeof(linear), PROT_READ | PROT_WRITE);
576                         if (ptr)
577                                 *ptr = 0;
578                 }
579
580                 if (flags & PE_BUSY)
581                         copy(fd, handle, handle, 0);
582         }
583         igt_waitchildren();
584 }
585
586 static void test_forked_access(int fd)
587 {
588         uint32_t handle1 = 0, handle2 = 0;
589         void *ptr1 = NULL, *ptr2 = NULL;
590         int ret;
591
592         ret = posix_memalign(&ptr1, PAGE_SIZE, sizeof(linear));
593         ret |= madvise(ptr1, sizeof(linear), MADV_DONTFORK);
594         ret |= gem_userptr(fd, ptr1, sizeof(linear), 0, &handle1);
595         igt_assert(ret == 0);
596         igt_assert(ptr1);
597         igt_assert(handle1);
598
599         ret = posix_memalign(&ptr2, PAGE_SIZE, sizeof(linear));
600         ret |= madvise(ptr2, sizeof(linear), MADV_DONTFORK);
601         ret |= gem_userptr(fd, ptr2, sizeof(linear), 0, &handle2);
602         igt_assert(ret == 0);
603         igt_assert(ptr2);
604         igt_assert(handle2);
605
606         memset(ptr1, 0x1, sizeof(linear));
607         memset(ptr2, 0x2, sizeof(linear));
608
609         igt_fork(child, 1) {
610                 copy(fd, handle1, handle2, 0);
611         }
612         igt_waitchildren();
613
614         gem_userptr_sync(fd, handle1);
615         gem_userptr_sync(fd, handle2);
616
617         gem_close(fd, handle1);
618         gem_close(fd, handle2);
619
620         igt_assert(memcmp(ptr1, ptr2, sizeof(linear)) == 0);
621
622         ret = madvise(ptr1, sizeof(linear), MADV_DOFORK);
623         igt_assert(ret == 0);
624         free(ptr1);
625
626         ret = madvise(ptr2, sizeof(linear), MADV_DOFORK);
627         igt_assert(ret == 0);
628         free(ptr2);
629 }
630
631 static int test_forbidden_ops(int fd)
632 {
633         struct drm_i915_gem_pread gem_pread;
634         struct drm_i915_gem_pwrite gem_pwrite;
635         void *ptr;
636         int ret;
637         uint32_t handle;
638         char buf[PAGE_SIZE];
639
640         memset(&gem_pread, 0, sizeof(gem_pread));
641         memset(&gem_pwrite, 0, sizeof(gem_pwrite));
642
643         igt_assert(posix_memalign(&ptr, PAGE_SIZE, PAGE_SIZE) == 0);
644
645         ret = gem_userptr(fd, ptr, PAGE_SIZE, 0, &handle);
646         igt_assert(ret == 0);
647
648         /* pread/pwrite are not always forbidden, but when they
649          * are they should fail with EINVAL.
650          */
651
652         gem_pread.handle = handle;
653         gem_pread.offset = 0;
654         gem_pread.size = PAGE_SIZE;
655         gem_pread.data_ptr = (uintptr_t)buf;
656         ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_PREAD, &gem_pread);
657         igt_assert(ret == 0 || errno == EINVAL);
658
659         gem_pwrite.handle = handle;
660         gem_pwrite.offset = 0;
661         gem_pwrite.size = PAGE_SIZE;
662         gem_pwrite.data_ptr = (uintptr_t)buf;
663         ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &gem_pwrite);
664         igt_assert(ret == 0 || errno == EINVAL);
665         gem_close(fd, handle);
666         free(ptr);
667
668         return 0;
669 }
670
671 static unsigned char counter;
672
673 static void (* volatile orig_sigbus)(int sig, siginfo_t *info, void *param);
674 static volatile unsigned long sigbus_start;
675 static volatile long sigbus_cnt = -1;
676
677 static void
678 check_bo(int fd1, uint32_t handle1, int is_userptr, int fd2, uint32_t handle2)
679 {
680         unsigned char *ptr1, *ptr2;
681         unsigned long size = sizeof(linear);
682
683         ptr2 = gem_mmap(fd2, handle2, sizeof(linear), PROT_READ | PROT_WRITE);
684
685         if (is_userptr)
686                 ptr1 = is_userptr > 0 ? get_handle_ptr(handle1) : ptr2;
687         else
688                 ptr1 = gem_mmap(fd1, handle1, sizeof(linear), PROT_READ | PROT_WRITE);
689
690         igt_assert(ptr1);
691         igt_assert(ptr2);
692
693         sigbus_start = (unsigned long)ptr2;
694         igt_assert(memcmp(ptr1, ptr2, sizeof(linear)) == 0);
695
696         counter++;
697
698         memset(ptr1, counter, size);
699         memset(ptr2, counter, size);
700
701         if (!is_userptr)
702                 munmap(ptr1, sizeof(linear));
703         munmap(ptr2, sizeof(linear));
704 }
705
706 static int export_handle(int fd, uint32_t handle, int *outfd)
707 {
708         struct drm_prime_handle args;
709         int ret;
710
711         args.handle = handle;
712         args.flags = DRM_CLOEXEC;
713         args.fd = -1;
714
715         ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
716         if (ret)
717                 ret = errno;
718         *outfd = args.fd;
719
720         return ret;
721 }
722
723 static void sigbus(int sig, siginfo_t *info, void *param)
724 {
725         unsigned long ptr = (unsigned long)info->si_addr;
726         void *addr;
727
728         if (ptr >= sigbus_start &&
729             ptr < sigbus_start + sizeof(linear)) {
730                 /* replace mapping to allow progress */
731                 munmap((void *)sigbus_start, sizeof(linear));
732                 addr = mmap((void *)sigbus_start, sizeof(linear),
733                             PROT_READ | PROT_WRITE,
734                             MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
735                 igt_assert((unsigned long)addr == sigbus_start);
736                 memset(addr, counter, sizeof(linear));
737
738                 sigbus_cnt++;
739                 return;
740         }
741
742         if (orig_sigbus)
743                 orig_sigbus(sig, info, param);
744         igt_assert(0);
745 }
746
747 static int test_dmabuf(void)
748 {
749         int fd1, fd2;
750         uint32_t handle, handle_import;
751         int dma_buf_fd = -1;
752         int ret;
753         struct sigaction sigact, orig_sigact;
754
755         fd1 = drm_open_any();
756
757         handle = create_userptr_bo(fd1, sizeof(linear));
758         memset(get_handle_ptr(handle), counter, sizeof(linear));
759
760         ret = export_handle(fd1, handle, &dma_buf_fd);
761         if (userptr_flags & LOCAL_I915_USERPTR_UNSYNCHRONIZED && ret) {
762                 igt_assert(ret == EINVAL || ret == ENODEV);
763                 free_userptr_bo(fd1, handle);
764                 close(fd1);
765                 return 0;
766         } else {
767                 igt_assert(ret == 0);
768                 igt_assert(dma_buf_fd >= 0);
769         }
770
771         fd2 = drm_open_any();
772         handle_import = prime_fd_to_handle(fd2, dma_buf_fd);
773         check_bo(fd1, handle, 1, fd2, handle_import);
774
775         /* close dma_buf, check whether nothing disappears. */
776         close(dma_buf_fd);
777         check_bo(fd1, handle, 1, fd2, handle_import);
778
779         /* destroy userptr object and expect SIGBUS */
780         free_userptr_bo(fd1, handle);
781         close(fd1);
782
783         memset(&sigact, 0, sizeof(sigact));
784         sigact.sa_sigaction = sigbus;
785         sigact.sa_flags = SA_SIGINFO;
786         ret = sigaction(SIGBUS, &sigact, &orig_sigact);
787         igt_assert(ret == 0);
788
789         orig_sigbus = orig_sigact.sa_sigaction;
790
791         sigbus_cnt = 0;
792         check_bo(fd2, handle_import, -1, fd2, handle_import);
793         igt_assert(sigbus_cnt > 0);
794
795         ret = sigaction(SIGBUS, &orig_sigact, NULL);
796         igt_assert(ret == 0);
797
798         close(fd2);
799         reset_handle_ptr();
800
801         return 0;
802 }
803
804 static int test_usage_restrictions(int fd)
805 {
806         void *ptr;
807         int ret;
808         uint32_t handle;
809
810         igt_assert(posix_memalign(&ptr, PAGE_SIZE, PAGE_SIZE * 2) == 0);
811
812         /* Address not aligned. */
813         ret = gem_userptr(fd, (char *)ptr + 1, PAGE_SIZE, 0, &handle);
814         igt_assert(ret != 0);
815
816         /* Size not rounded to page size. */
817         ret = gem_userptr(fd, ptr, PAGE_SIZE - 1, 0, &handle);
818         igt_assert(ret != 0);
819
820         /* Both wrong. */
821         ret = gem_userptr(fd, (char *)ptr + 1, PAGE_SIZE - 1, 0, &handle);
822         igt_assert(ret != 0);
823
824         /* Read-only not supported. */
825         ret = gem_userptr(fd, (char *)ptr, PAGE_SIZE, 1, &handle);
826         igt_assert(ret != 0);
827
828         free(ptr);
829
830         return 0;
831 }
832
833 static int test_create_destroy(int fd)
834 {
835         void *ptr;
836         int ret;
837         uint32_t handle;
838
839         igt_assert(posix_memalign(&ptr, PAGE_SIZE, PAGE_SIZE) == 0);
840
841         ret = gem_userptr(fd, ptr, PAGE_SIZE, 0, &handle);
842         igt_assert(ret == 0);
843
844         gem_close(fd, handle);
845         free(ptr);
846
847         return 0;
848 }
849
850 static int test_coherency(int fd, int count)
851 {
852         uint32_t *memory;
853         uint32_t *cpu, *cpu_val;
854         uint32_t *gpu, *gpu_val;
855         uint32_t start = 0;
856         int i, ret;
857
858         igt_require(intel_check_memory(count, sizeof(linear), CHECK_RAM));
859         igt_info("Using 2x%d 1MiB buffers\n", count);
860
861         ret = posix_memalign((void **)&memory, PAGE_SIZE, count*sizeof(linear));
862         igt_assert(ret == 0 && memory);
863
864         gpu = malloc(sizeof(uint32_t)*count*4);
865         gpu_val = gpu + count;
866         cpu = gpu_val + count;
867         cpu_val = cpu + count;
868
869         for (i = 0; i < count; i++) {
870                 gpu[i] = create_bo(fd, start);
871                 gpu_val[i] = start;
872                 start += WIDTH*HEIGHT;
873         }
874
875         for (i = 0; i < count; i++) {
876                 cpu[i] = create_userptr(fd, start, memory+i*WIDTH*HEIGHT);
877                 cpu_val[i] = start;
878                 start += WIDTH*HEIGHT;
879         }
880
881         igt_info("Verifying initialisation...\n");
882         for (i = 0; i < count; i++) {
883                 check_gpu(fd, gpu[i], gpu_val[i]);
884                 check_cpu(memory+i*WIDTH*HEIGHT, cpu_val[i]);
885         }
886
887         igt_info("Cyclic blits cpu->gpu, forward...\n");
888         for (i = 0; i < count * 4; i++) {
889                 int src = i % count;
890                 int dst = (i + 1) % count;
891
892                 copy(fd, gpu[dst], cpu[src], 0);
893                 gpu_val[dst] = cpu_val[src];
894         }
895         for (i = 0; i < count; i++)
896                 check_gpu(fd, gpu[i], gpu_val[i]);
897
898         igt_info("Cyclic blits gpu->cpu, backward...\n");
899         for (i = 0; i < count * 4; i++) {
900                 int src = (i + 1) % count;
901                 int dst = i % count;
902
903                 copy(fd, cpu[dst], gpu[src], 0);
904                 cpu_val[dst] = gpu_val[src];
905         }
906         for (i = 0; i < count; i++) {
907                 gem_userptr_sync(fd, cpu[i]);
908                 check_cpu(memory+i*WIDTH*HEIGHT, cpu_val[i]);
909         }
910
911         igt_info("Random blits...\n");
912         for (i = 0; i < count * 4; i++) {
913                 int src = random() % count;
914                 int dst = random() % count;
915
916                 if (random() & 1) {
917                         copy(fd, gpu[dst], cpu[src], 0);
918                         gpu_val[dst] = cpu_val[src];
919                 } else {
920                         copy(fd, cpu[dst], gpu[src], 0);
921                         cpu_val[dst] = gpu_val[src];
922                 }
923         }
924         for (i = 0; i < count; i++) {
925                 check_gpu(fd, gpu[i], gpu_val[i]);
926                 gem_close(fd, gpu[i]);
927
928                 gem_userptr_sync(fd, cpu[i]);
929                 check_cpu(memory+i*WIDTH*HEIGHT, cpu_val[i]);
930                 gem_close(fd, cpu[i]);
931         }
932
933         free(gpu);
934         free(memory);
935
936         return 0;
937 }
938
939 static struct igt_eviction_test_ops fault_ops = {
940         .create = create_userptr_bo,
941         .flink = flink_userptr_bo,
942         .close = free_userptr_bo,
943         .copy = blit,
944         .clear = clear,
945 };
946
947 static int can_swap(void)
948 {
949         unsigned long as, ram;
950
951         /* Cannot swap if not enough address space */
952
953         /* FIXME: Improve check criteria. */
954         if (sizeof(void*) < 8)
955                 as = 3 * 1024;
956         else
957                 as = 256 * 1024; /* Just a big number */
958
959         ram = intel_get_total_ram_mb();
960
961         if ((as - 128) < (ram - 256))
962                 return 0;
963
964         return 1;
965 }
966
967 #define min(a, b) ((a) < (b) ? (a) : (b))
968
969 static void test_forking_evictions(int fd, int size, int count,
970                              unsigned flags)
971 {
972         int trash_count;
973         int num_threads;
974
975         trash_count = intel_get_total_ram_mb() * 11 / 10;
976         /* Use the fact test will spawn a number of child
977          * processes meaning swapping will be triggered system
978          * wide even if one process on it's own can't do it.
979          */
980         num_threads = min(sysconf(_SC_NPROCESSORS_ONLN) * 4, 12);
981         trash_count /= num_threads;
982         if (count > trash_count)
983                 count = trash_count;
984
985         forking_evictions(fd, &fault_ops, size, count, trash_count, flags);
986         reset_handle_ptr();
987 }
988
989 static void test_swapping_evictions(int fd, int size, int count)
990 {
991         int trash_count;
992
993         igt_skip_on_f(!can_swap(),
994                 "Not enough process address space for swapping tests.\n");
995
996         trash_count = intel_get_total_ram_mb() * 11 / 10;
997
998         swapping_evictions(fd, &fault_ops, size, count, trash_count);
999         reset_handle_ptr();
1000 }
1001
1002 static void test_minor_evictions(int fd, int size, int count)
1003 {
1004         minor_evictions(fd, &fault_ops, size, count);
1005         reset_handle_ptr();
1006 }
1007
1008 static void test_major_evictions(int fd, int size, int count)
1009 {
1010         major_evictions(fd, &fault_ops, size, count);
1011         reset_handle_ptr();
1012 }
1013
1014 static void test_overlap(int fd, int expected)
1015 {
1016         char *ptr;
1017         int ret;
1018         uint32_t handle, handle2;
1019
1020         igt_assert(posix_memalign((void *)&ptr, PAGE_SIZE, PAGE_SIZE * 3) == 0);
1021
1022         ret = gem_userptr(fd, ptr + PAGE_SIZE, PAGE_SIZE, 0, &handle);
1023         igt_assert(ret == 0);
1024
1025         /* before, no overlap */
1026         ret = gem_userptr(fd, ptr, PAGE_SIZE, 0, &handle2);
1027         if (ret == 0)
1028                 gem_close(fd, handle2);
1029         igt_assert(ret == 0);
1030
1031         /* after, no overlap */
1032         ret = gem_userptr(fd, ptr + PAGE_SIZE * 2, PAGE_SIZE, 0, &handle2);
1033         if (ret == 0)
1034                 gem_close(fd, handle2);
1035         igt_assert(ret == 0);
1036
1037         /* exactly overlapping */
1038         ret = gem_userptr(fd, ptr + PAGE_SIZE, PAGE_SIZE, 0, &handle2);
1039         if (ret == 0)
1040                 gem_close(fd, handle2);
1041         igt_assert(ret == 0 || ret == expected);
1042
1043         /* start overlaps */
1044         ret = gem_userptr(fd, ptr, PAGE_SIZE * 2, 0, &handle2);
1045         if (ret == 0)
1046                 gem_close(fd, handle2);
1047         igt_assert(ret == 0 || ret == expected);
1048
1049         /* end overlaps */
1050         ret = gem_userptr(fd, ptr + PAGE_SIZE, PAGE_SIZE * 2, 0, &handle2);
1051         if (ret == 0)
1052                 gem_close(fd, handle2);
1053         igt_assert(ret == 0 || ret == expected);
1054
1055         /* subsumes */
1056         ret = gem_userptr(fd, ptr, PAGE_SIZE * 3, 0, &handle2);
1057         if (ret == 0)
1058                 gem_close(fd, handle2);
1059         igt_assert(ret == 0 || ret == expected);
1060
1061         gem_close(fd, handle);
1062         free(ptr);
1063 }
1064
1065 static void test_unmap(int fd, int expected)
1066 {
1067         char *ptr, *bo_ptr;
1068         const unsigned int num_obj = 3;
1069         unsigned int i;
1070         uint32_t bo[num_obj + 1];
1071         size_t map_size = sizeof(linear) * num_obj + (PAGE_SIZE - 1);
1072         int ret;
1073
1074         ptr = mmap(NULL, map_size, PROT_READ | PROT_WRITE,
1075                                 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
1076         igt_assert(ptr != MAP_FAILED);
1077
1078         bo_ptr = (char *)ALIGN((unsigned long)ptr, PAGE_SIZE);
1079
1080         for (i = 0; i < num_obj; i++, bo_ptr += sizeof(linear)) {
1081                 ret = gem_userptr(fd, bo_ptr, sizeof(linear), 0, &bo[i]);
1082                 igt_assert(ret == 0);
1083         }
1084
1085         bo[num_obj] = create_bo(fd, 0);
1086
1087         for (i = 0; i < num_obj; i++)
1088                 copy(fd, bo[num_obj], bo[i], 0);
1089
1090         ret = munmap(ptr, map_size);
1091         igt_assert(ret == 0);
1092
1093         for (i = 0; i < num_obj; i++)
1094                 copy(fd, bo[num_obj], bo[i], expected);
1095
1096         for (i = 0; i < (num_obj + 1); i++)
1097                 gem_close(fd, bo[i]);
1098 }
1099
1100 static void test_unmap_after_close(int fd)
1101 {
1102         char *ptr, *bo_ptr;
1103         const unsigned int num_obj = 3;
1104         unsigned int i;
1105         uint32_t bo[num_obj + 1];
1106         size_t map_size = sizeof(linear) * num_obj + (PAGE_SIZE - 1);
1107         int ret;
1108
1109         ptr = mmap(NULL, map_size, PROT_READ | PROT_WRITE,
1110                                 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
1111         igt_assert(ptr != MAP_FAILED);
1112
1113         bo_ptr = (char *)ALIGN((unsigned long)ptr, PAGE_SIZE);
1114
1115         for (i = 0; i < num_obj; i++, bo_ptr += sizeof(linear)) {
1116                 ret = gem_userptr(fd, bo_ptr, sizeof(linear), 0, &bo[i]);
1117                 igt_assert(ret == 0);
1118         }
1119
1120         bo[num_obj] = create_bo(fd, 0);
1121
1122         for (i = 0; i < num_obj; i++)
1123                 copy(fd, bo[num_obj], bo[i], 0);
1124
1125         for (i = 0; i < (num_obj + 1); i++)
1126                 gem_close(fd, bo[i]);
1127
1128         ret = munmap(ptr, map_size);
1129         igt_assert(ret == 0);
1130 }
1131
1132 static void test_unmap_cycles(int fd, int expected)
1133 {
1134         int i;
1135
1136         for (i = 0; i < 1000; i++)
1137                 test_unmap(fd, expected);
1138 }
1139
1140 struct stress_thread_data {
1141         unsigned int stop;
1142         int exit_code;
1143 };
1144
1145 static void *mm_stress_thread(void *data)
1146 {
1147         struct stress_thread_data *stdata = (struct stress_thread_data *)data;
1148         void *ptr;
1149         int ret;
1150
1151         while (!stdata->stop) {
1152                 ptr = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE,
1153                                 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
1154                 if (ptr == MAP_FAILED) {
1155                         stdata->exit_code = -EFAULT;
1156                         break;
1157                 }
1158                 ret = munmap(ptr, PAGE_SIZE);
1159                 if (ret) {
1160                         stdata->exit_code = errno;
1161                         break;
1162                 }
1163         }
1164
1165         return NULL;
1166 }
1167
1168 static void test_stress_mm(int fd)
1169 {
1170         int ret;
1171         pthread_t t;
1172         unsigned int loops = 100000;
1173         uint32_t handle;
1174         void *ptr;
1175         struct stress_thread_data stdata;
1176
1177         memset(&stdata, 0, sizeof(stdata));
1178
1179         igt_assert(posix_memalign(&ptr, PAGE_SIZE, PAGE_SIZE) == 0);
1180
1181         ret = pthread_create(&t, NULL, mm_stress_thread, &stdata);
1182         igt_assert(ret == 0);
1183
1184         while (loops--) {
1185                 ret = gem_userptr(fd, ptr, PAGE_SIZE, 0, &handle);
1186                 igt_assert(ret == 0);
1187
1188                 gem_close(fd, handle);
1189         }
1190
1191         free(ptr);
1192
1193         stdata.stop = 1;
1194         ret = pthread_join(t, NULL);
1195         igt_assert(ret == 0);
1196
1197         igt_assert(stdata.exit_code == 0);
1198 }
1199
1200 unsigned int total_ram;
1201 uint64_t aperture_size;
1202 int fd, count;
1203
1204
1205 int main(int argc, char **argv)
1206 {
1207         int size = sizeof(linear);
1208
1209         igt_skip_on_simulation();
1210
1211         igt_subtest_init(argc, argv);
1212
1213         igt_fixture {
1214                 int ret;
1215
1216                 fd = drm_open_any();
1217                 igt_assert(fd >= 0);
1218
1219                 ret = has_userptr(fd);
1220                 igt_skip_on_f(ret == 0, "No userptr support - %s (%d)\n",
1221                               strerror(errno), ret);
1222
1223                 size = sizeof(linear);
1224
1225                 aperture_size = gem_aperture_size(fd);
1226                 igt_info("Aperture size is %lu MiB\n", (long)(aperture_size / (1024*1024)));
1227
1228                 if (argc > 1)
1229                         count = atoi(argv[1]);
1230                 if (count == 0)
1231                         count = 2 * aperture_size / (1024*1024) / 3;
1232
1233                 total_ram = intel_get_total_ram_mb();
1234                 igt_info("Total RAM is %u MiB\n", total_ram);
1235
1236                 if (count > total_ram * 3 / 4) {
1237                         count = intel_get_total_ram_mb() * 3 / 4;
1238                         igt_info("Not enough RAM to run test, reducing buffer count.\n");
1239                 }
1240         }
1241
1242         igt_subtest("input-checking")
1243                 test_input_checking(fd);
1244
1245         igt_subtest("usage-restrictions")
1246                 test_usage_restrictions(fd);
1247
1248         igt_subtest("invalid-null-pointer")
1249                 test_invalid_null_pointer(fd);
1250
1251         igt_subtest("invalid-gtt-mapping")
1252                 test_invalid_gtt_mapping(fd);
1253
1254         igt_subtest("forked-access")
1255                 test_forked_access(fd);
1256
1257         igt_subtest("forbidden-operations")
1258                 test_forbidden_ops(fd);
1259
1260         igt_info("Testing unsynchronized mappings...\n");
1261         gem_userptr_test_unsynchronized();
1262
1263         igt_subtest("create-destroy-unsync")
1264                 test_create_destroy(fd);
1265
1266         igt_subtest("unsync-overlap")
1267                 test_overlap(fd, 0);
1268
1269         igt_subtest("unsync-unmap")
1270                 test_unmap(fd, 0);
1271
1272         igt_subtest("unsync-unmap-cycles")
1273                 test_unmap_cycles(fd, 0);
1274
1275         igt_subtest("unsync-unmap-after-close")
1276                 test_unmap_after_close(fd);
1277
1278         igt_subtest("coherency-unsync")
1279                 test_coherency(fd, count);
1280
1281         igt_subtest("dmabuf-unsync")
1282                 test_dmabuf();
1283
1284         for (unsigned flags = 0; flags < ALL_FORKING_EVICTIONS + 1; flags++) {
1285                 igt_subtest_f("forked-unsync%s%s%s-%s",
1286                     flags & FORKING_EVICTIONS_SWAPPING ? "-swapping" : "",
1287                     flags & FORKING_EVICTIONS_DUP_DRMFD ? "-multifd" : "",
1288                     flags & FORKING_EVICTIONS_MEMORY_PRESSURE ?
1289                                 "-mempressure" : "",
1290                     flags & FORKING_EVICTIONS_INTERRUPTIBLE ?
1291                                 "interruptible" : "normal") {
1292                         test_forking_evictions(fd, size, count, flags);
1293                 }
1294         }
1295
1296         igt_subtest("swapping-unsync-normal")
1297                 test_swapping_evictions(fd, size, count);
1298
1299         igt_subtest("minor-unsync-normal")
1300                 test_minor_evictions(fd, size, count);
1301
1302         igt_subtest("major-unsync-normal") {
1303                 size = 200 * 1024 * 1024;
1304                 count = (gem_aperture_size(fd) / size) + 2;
1305                 test_major_evictions(fd, size, count);
1306         }
1307
1308         igt_fixture {
1309                 size = sizeof(linear);
1310                 count = 2 * gem_aperture_size(fd) / (1024*1024) / 3;
1311                 if (count > total_ram * 3 / 4)
1312                         count = intel_get_total_ram_mb() * 3 / 4;
1313         }
1314
1315         igt_fork_signal_helper();
1316
1317         igt_subtest("swapping-unsync-interruptible")
1318                 test_swapping_evictions(fd, size, count);
1319
1320         igt_subtest("minor-unsync-interruptible")
1321                 test_minor_evictions(fd, size, count);
1322
1323         igt_subtest("major-unsync-interruptible") {
1324                 size = 200 * 1024 * 1024;
1325                 count = (gem_aperture_size(fd) / size) + 2;
1326                 test_major_evictions(fd, size, count);
1327         }
1328
1329         igt_stop_signal_helper();
1330
1331         igt_info("Testing synchronized mappings...\n");
1332
1333         igt_fixture {
1334                 size = sizeof(linear);
1335                 count = 2 * gem_aperture_size(fd) / (1024*1024) / 3;
1336                 if (count > total_ram * 3 / 4)
1337                         count = intel_get_total_ram_mb() * 3 / 4;
1338         }
1339
1340         gem_userptr_test_synchronized();
1341
1342         igt_subtest("process-exit")
1343                 test_process_exit(fd, 0);
1344
1345         igt_subtest("process-exit-gtt")
1346                 test_process_exit(fd, PE_GTT_MAP);
1347
1348         igt_subtest("process-exit-busy")
1349                 test_process_exit(fd, PE_BUSY);
1350
1351         igt_subtest("process-exit-gtt-busy")
1352                 test_process_exit(fd, PE_GTT_MAP | PE_BUSY);
1353
1354         igt_subtest("create-destroy-sync")
1355                 test_create_destroy(fd);
1356
1357         igt_subtest("sync-overlap")
1358                 test_overlap(fd, EINVAL);
1359
1360         igt_subtest("sync-unmap")
1361                 test_unmap(fd, EFAULT);
1362
1363         igt_subtest("sync-unmap-cycles")
1364                 test_unmap_cycles(fd, EFAULT);
1365
1366         igt_subtest("sync-unmap-after-close")
1367                 test_unmap_after_close(fd);
1368
1369         igt_subtest("stress-mm")
1370                 test_stress_mm(fd);
1371
1372         igt_subtest("coherency-sync")
1373                 test_coherency(fd, count);
1374
1375         igt_subtest("dmabuf-sync")
1376                 test_dmabuf();
1377
1378         for (unsigned flags = 0; flags < ALL_FORKING_EVICTIONS + 1; flags++) {
1379                 igt_subtest_f("forked-sync%s%s%s-%s",
1380                     flags & FORKING_EVICTIONS_SWAPPING ? "-swapping" : "",
1381                     flags & FORKING_EVICTIONS_DUP_DRMFD ? "-multifd" : "",
1382                     flags & FORKING_EVICTIONS_MEMORY_PRESSURE ?
1383                                 "-mempressure" : "",
1384                     flags & FORKING_EVICTIONS_INTERRUPTIBLE ?
1385                                 "interruptible" : "normal") {
1386                         test_forking_evictions(fd, size, count, flags);
1387                 }
1388         }
1389
1390         igt_subtest("swapping-normal-sync")
1391                 test_swapping_evictions(fd, size, count);
1392
1393         igt_subtest("minor-normal-sync")
1394                 test_minor_evictions(fd, size, count);
1395
1396         igt_subtest("major-normal-sync") {
1397                 size = 200 * 1024 * 1024;
1398                 count = (gem_aperture_size(fd) / size) + 2;
1399                 test_major_evictions(fd, size, count);
1400         }
1401
1402         igt_fixture {
1403                 size = 1024 * 1024;
1404                 count = 2 * gem_aperture_size(fd) / (1024*1024) / 3;
1405                 if (count > total_ram * 3 / 4)
1406                         count = intel_get_total_ram_mb() * 3 / 4;
1407         }
1408
1409         igt_fork_signal_helper();
1410
1411         igt_subtest("swapping-sync-interruptible")
1412                 test_swapping_evictions(fd, size, count);
1413
1414         igt_subtest("minor-sync-interruptible")
1415                 test_minor_evictions(fd, size, count);
1416
1417         igt_subtest("major-sync-interruptible") {
1418                 size = 200 * 1024 * 1024;
1419                 count = (gem_aperture_size(fd) / size) + 2;
1420                 test_major_evictions(fd, size, count);
1421         }
1422
1423         igt_stop_signal_helper();
1424
1425         igt_subtest("access-control")
1426                 test_access_control(fd);
1427
1428         igt_exit();
1429
1430         return 0;
1431 }