gem_close_race: Tidy up call to execbuffer
[platform/upstream/intel-gpu-tools.git] / tests / gem_close_race.c
1 /*
2  * Copyright © 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  *    Chris Wilson <chris@chris-wilson.co.uk>
25  *
26  */
27
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <inttypes.h>
35 #include <errno.h>
36 #include <sys/stat.h>
37 #include <sys/ioctl.h>
38 #include <sys/time.h>
39 #include "drm.h"
40 #include "i915_drm.h"
41 #include "drmtest.h"
42
43 #define OBJECT_SIZE 1024*1024*4
44
45 #define COPY_BLT_CMD            (2<<29|0x53<<22|0x6)
46 #define BLT_WRITE_ALPHA         (1<<21)
47 #define BLT_WRITE_RGB           (1<<20)
48
49 static char device[80];
50
51 static void selfcopy(int fd, uint32_t handle)
52 {
53         struct drm_i915_gem_relocation_entry reloc[2];
54         struct drm_i915_gem_exec_object2 gem_exec[2];
55         struct drm_i915_gem_execbuffer2 execbuf;
56         uint32_t buf[10];
57
58         memset(reloc, 0, sizeof(reloc));
59         memset(gem_exec, 0, sizeof(gem_exec));
60         memset(&execbuf, 0, sizeof(execbuf));
61
62         buf[0] = COPY_BLT_CMD | BLT_WRITE_ALPHA | BLT_WRITE_RGB;
63         buf[1] = 0xcc << 16 | 1 << 25 | 1 << 24 | (4*1024);
64         buf[2] = 0;
65         buf[3] = 1024 << 16 | 1024;
66         buf[4] = 0;
67         reloc[0].offset = 4 * sizeof(uint32_t);
68         reloc[0].target_handle = handle;
69         reloc[0].read_domains = I915_GEM_DOMAIN_RENDER;
70         reloc[0].write_domain = I915_GEM_DOMAIN_RENDER;
71
72         buf[5] = 0;
73         buf[6] = 4*1024;
74         buf[7] = 0;
75         reloc[1].offset = 7 * sizeof(uint32_t);
76         reloc[1].target_handle = handle;
77         reloc[1].read_domains = I915_GEM_DOMAIN_RENDER;
78         reloc[1].write_domain = 0;
79
80         buf[8] = MI_BATCH_BUFFER_END;
81         buf[9] = 0;
82
83         gem_exec[0].handle = handle;
84
85         gem_exec[1].handle = gem_create(fd, 4096);
86         gem_exec[1].relocation_count = 2;
87         gem_exec[1].relocs_ptr = (uintptr_t)reloc;
88
89         gem_write(fd, gem_exec[1].handle, 0, buf, sizeof(buf));
90
91         execbuf.buffers_ptr = (uintptr_t)gem_exec;
92         execbuf.buffer_count = 2;
93         execbuf.batch_len = sizeof(buf);
94         if (HAS_BLT_RING(intel_get_drm_devid(fd)))
95                 execbuf.flags |= I915_EXEC_BLT;
96
97         gem_execbuf(fd, &execbuf);
98 }
99
100 static uint32_t load(int fd)
101 {
102         uint32_t handle;
103
104         handle = gem_create(fd, OBJECT_SIZE);
105         if (handle == 0)
106                 return 0;
107
108         selfcopy(fd, handle);
109         return handle;
110 }
111
112 static void run(int child)
113 {
114         uint32_t handle;
115         int fd;
116
117         fd = open(device, O_RDWR);
118         igt_assert(fd != -1);
119
120         handle = load(fd);
121         if (child & 1)
122                 gem_read(fd, handle, 0, &handle, sizeof(handle));
123 }
124
125 int main(int argc, char *argv[])
126 {
127         igt_skip_on_simulation();
128         igt_subtest_init(argc, argv);
129
130         sprintf(device, "/dev/dri/card%d", drm_get_card());
131
132         igt_subtest("gem-close-race") {
133                 igt_fork(child, 100)
134                         run(child);
135                 igt_waitchildren();
136         }
137
138         igt_exit();
139 }