kms_rotation_crc: Remove useless comments
[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 <pthread.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <fcntl.h>
35 #include <inttypes.h>
36 #include <errno.h>
37 #include <sys/stat.h>
38 #include <sys/ioctl.h>
39 #include <sys/time.h>
40 #include "drm.h"
41 #include "ioctl_wrappers.h"
42 #include "drmtest.h"
43 #include "intel_chipset.h"
44
45 #define OBJECT_SIZE 1024*1024*4
46
47 #define COPY_BLT_CMD            (2<<29|0x53<<22|0x6)
48 #define BLT_WRITE_ALPHA         (1<<21)
49 #define BLT_WRITE_RGB           (1<<20)
50
51 static char device[80];
52 static uint32_t devid;
53 static bool has_64bit_relocations;
54
55 static void selfcopy(int fd, uint32_t handle, int loops)
56 {
57         struct drm_i915_gem_relocation_entry reloc[2];
58         struct drm_i915_gem_exec_object2 gem_exec[2];
59         struct drm_i915_gem_execbuffer2 execbuf;
60         struct drm_i915_gem_pwrite gem_pwrite;
61         struct drm_i915_gem_create create;
62         uint32_t buf[12], *b = buf;
63
64         memset(reloc, 0, sizeof(reloc));
65         memset(gem_exec, 0, sizeof(gem_exec));
66         memset(&execbuf, 0, sizeof(execbuf));
67
68         *b = COPY_BLT_CMD | BLT_WRITE_ALPHA | BLT_WRITE_RGB;
69         if (has_64bit_relocations)
70                 *b += 2;
71         b++;
72         *b++ = 0xcc << 16 | 1 << 25 | 1 << 24 | (4*1024);
73         *b++ = 0;
74         *b++ = 512 << 16 | 1024;
75
76         reloc[0].offset = (b - buf) * sizeof(*b);
77         reloc[0].target_handle = handle;
78         reloc[0].read_domains = I915_GEM_DOMAIN_RENDER;
79         reloc[0].write_domain = I915_GEM_DOMAIN_RENDER;
80         *b++ = 0;
81         if (has_64bit_relocations)
82                 *b++ = 0;
83
84         *b++ = 512 << 16;
85         *b++ = 4*1024;
86
87         reloc[1].offset = (b - buf) * sizeof(*b);
88         reloc[1].target_handle = handle;
89         reloc[1].read_domains = I915_GEM_DOMAIN_RENDER;
90         reloc[1].write_domain = 0;
91         *b++ = 0;
92         if (has_64bit_relocations)
93                 *b++ = 0;
94
95         *b++ = MI_BATCH_BUFFER_END;
96         *b++ = 0;
97
98         gem_exec[0].handle = handle;
99
100         create.handle = 0;
101         create.size = 4096;
102         drmIoctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
103         gem_exec[1].handle = create.handle;
104         gem_exec[1].relocation_count = 2;
105         gem_exec[1].relocs_ptr = (uintptr_t)reloc;
106
107         execbuf.buffers_ptr = (uintptr_t)gem_exec;
108         execbuf.buffer_count = 2;
109         execbuf.batch_len = (b - buf) * sizeof(*b);
110         if (HAS_BLT_RING(devid))
111                 execbuf.flags |= I915_EXEC_BLT;
112
113         gem_pwrite.handle = gem_exec[1].handle;
114         gem_pwrite.offset = 0;
115         gem_pwrite.size = execbuf.batch_len;
116         gem_pwrite.data_ptr = (uintptr_t)buf;
117         if (drmIoctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &gem_pwrite) == 0) {
118                 while (loops--)
119                         drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
120         }
121
122         drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &create.handle);
123 }
124
125 static uint32_t load(int fd)
126 {
127         uint32_t handle;
128
129         handle = gem_create(fd, OBJECT_SIZE);
130         if (handle == 0)
131                 return 0;
132
133         selfcopy(fd, handle, 30);
134         return handle;
135 }
136
137 static void run(int child)
138 {
139         uint32_t handle;
140         int fd;
141
142         fd = open(device, O_RDWR);
143         igt_assert(fd != -1);
144
145         handle = load(fd);
146         if ((child & 63) == 63)
147                 gem_read(fd, handle, 0, &handle, sizeof(handle));
148 }
149
150 #define NUM_FD 768
151
152 struct thread {
153         pthread_mutex_t mutex;
154         int fds[NUM_FD];
155         int done;
156 };
157
158 static void *thread_run(void *_data)
159 {
160         struct thread *t = _data;
161
162         pthread_mutex_lock(&t->mutex);
163         while (!t->done) {
164                 pthread_mutex_unlock(&t->mutex);
165
166                 for (int n = 0; n < NUM_FD; n++) {
167                         struct drm_i915_gem_create create;
168
169                         create.handle = 0;
170                         create.size = OBJECT_SIZE;
171                         drmIoctl(t->fds[n], DRM_IOCTL_I915_GEM_CREATE, &create);
172                         if (create.handle == 0)
173                                 continue;
174
175                         selfcopy(t->fds[n], create.handle, 10);
176
177                         drmIoctl(t->fds[n], DRM_IOCTL_GEM_CLOSE, &create.handle);
178                 }
179
180                 pthread_mutex_lock(&t->mutex);
181         }
182         pthread_mutex_unlock(&t->mutex);
183
184         return 0;
185 }
186
187 static void *thread_busy(void *_data)
188 {
189         struct thread *t = _data;
190         int n;
191
192         pthread_mutex_lock(&t->mutex);
193         while (!t->done) {
194                 struct drm_i915_gem_create create;
195                 struct drm_i915_gem_busy busy;
196
197                 pthread_mutex_unlock(&t->mutex);
198
199                 n  = rand() % NUM_FD;
200
201                 create.handle = 0;
202                 create.size = OBJECT_SIZE;
203                 drmIoctl(t->fds[n], DRM_IOCTL_I915_GEM_CREATE, &create);
204                 if (create.handle == 0)
205                         continue;
206
207                 selfcopy(t->fds[n], create.handle, 1);
208
209                 busy.handle = create.handle;
210                 drmIoctl(t->fds[n], DRM_IOCTL_I915_GEM_BUSY, &busy);
211
212                 drmIoctl(t->fds[n], DRM_IOCTL_GEM_CLOSE, &create.handle);
213
214                 usleep(10*1000);
215
216                 pthread_mutex_lock(&t->mutex);
217         }
218         pthread_mutex_unlock(&t->mutex);
219
220         return 0;
221 }
222
223 igt_main
224 {
225         igt_skip_on_simulation();
226
227         igt_fixture {
228                 int fd;
229
230                 sprintf(device, "/dev/dri/card%d", drm_get_card());
231                 fd = open(device, O_RDWR);
232
233                 igt_assert(fd != -1);
234                 devid = intel_get_drm_devid(fd);
235                 has_64bit_relocations = intel_gen(devid) >= 8;
236                 close(fd);
237         }
238
239         igt_subtest("process-exit") {
240                 igt_fork(child, NUM_FD)
241                         run(child);
242                 igt_waitchildren();
243         }
244
245         igt_subtest("gem-close-race") {
246                 pthread_t thread[2];
247                 struct thread *data = calloc(1, sizeof(struct thread));
248                 int n;
249
250                 igt_assert(data);
251
252                 pthread_mutex_init(&data->mutex, NULL);
253                 for (n = 0; n < NUM_FD; n++)
254                         data->fds[n] = open(device, O_RDWR);
255
256                 pthread_create(&thread[0], NULL, thread_run, data);
257                 pthread_create(&thread[1], NULL, thread_busy, data);
258
259                 for (n = 0; n < 1000*NUM_FD; n++) {
260                         int i = rand() % NUM_FD;
261                         if (data->fds[i] == -1) {
262                                 data->fds[i] = open(device, O_RDWR);
263                         } else{
264                                 close(data->fds[i]);
265                                 data->fds[i] = -1;
266                         }
267                 }
268
269                 pthread_mutex_lock(&data->mutex);
270                 data->done = 1;
271                 pthread_mutex_unlock(&data->mutex);
272
273                 pthread_join(thread[1], NULL);
274                 pthread_join(thread[0], NULL);
275
276                 for (n = 0; n < NUM_FD; n++)
277                         close(data->fds[n]);
278                 free(data);
279         }
280 }