cceae366ae1794dcaa77530cfa7f83b20bb6fd0c
[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 void selfcopy(int fd, uint32_t handle)
50 {
51         struct drm_i915_gem_relocation_entry reloc[2];
52         struct drm_i915_gem_exec_object2 gem_exec[2];
53         struct drm_i915_gem_execbuffer2 execbuf;
54         uint32_t buf[10];
55         uint32_t batch;
56
57         memset(reloc, 0, sizeof(reloc));
58         memset(gem_exec, 0, sizeof(gem_exec));
59         memset(&execbuf, 0, sizeof(execbuf));
60
61         buf[0] = COPY_BLT_CMD | BLT_WRITE_ALPHA | BLT_WRITE_RGB;
62         buf[1] = 0xcc << 16 | 1 << 25 | 1 << 24 | (4*1024);
63         buf[2] = 0;
64         buf[3] = 1024 << 16 | 1024;
65         buf[4] = 0;
66         reloc[0].offset = 4 * sizeof(uint32_t);
67         reloc[0].delta = 0;
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         reloc[0].presumed_offset = 0;
72
73         buf[5] = 0;
74         buf[6] = 4*1024;
75         buf[7] = 0;
76         reloc[1].offset = 7 * sizeof(uint32_t);
77         reloc[1].delta = 0;
78         reloc[1].target_handle = handle;
79         reloc[1].read_domains = I915_GEM_DOMAIN_RENDER;
80         reloc[1].write_domain = 0;
81         reloc[1].presumed_offset = 0;
82
83         buf[8] = MI_BATCH_BUFFER_END;
84         buf[9] = 0;
85
86         gem_exec[0].handle = handle;
87
88         batch = gem_create(fd, 4096);
89
90         gem_write(fd, batch, 0, buf, sizeof(buf));
91         gem_exec[1].handle = batch;
92         gem_exec[1].relocation_count = 2;
93         gem_exec[1].relocs_ptr = (uintptr_t)reloc;
94
95         execbuf.buffers_ptr = (uintptr_t)&gem_exec;
96         execbuf.buffer_count = 2;
97         execbuf.batch_len = sizeof(buf);
98         if (HAS_BLT_RING(intel_get_drm_devid(fd)))
99                 execbuf.flags |= I915_EXEC_BLT;
100
101         gem_execbuf(fd, &execbuf);
102 }
103
104 static int open_drm(void)
105 {
106         char name[80];
107
108         sprintf(name, "/dev/dri/card%d", drm_get_card());
109         return open(name, O_RDWR);
110 }
111
112 static uint32_t load(int fd)
113 {
114         uint32_t handle;
115
116         handle = gem_create(fd, OBJECT_SIZE);
117         if (handle == 0)
118                 return 0;
119
120         selfcopy(fd, handle);
121         return handle;
122 }
123
124 static void run(int child)
125 {
126         int fd;
127         uint32_t handle;
128
129         fd = open_drm();
130         igt_assert(fd != -1);
131
132         handle = load(fd);
133         if (child & 1)
134                 gem_read(fd, handle, 0, &handle, sizeof(handle));
135 }
136
137 int main(int argc, char *argv[])
138 {
139         igt_skip_on_simulation();
140         igt_subtest_init(argc, argv);
141
142         igt_subtest("gem-close-race")
143                 igt_fork(child, 100)
144                         run(child);
145
146         igt_waitchildren();
147         igt_exit();
148 }