2 * Copyright © 2013 Intel Corporation
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:
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
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
24 * Chris Wilson <chris@chris-wilson.co.uk>
38 #include <sys/ioctl.h>
41 #include "ioctl_wrappers.h"
43 #include "intel_chipset.h"
45 #define OBJECT_SIZE (256 * 1024)
47 #define COPY_BLT_CMD (2<<29|0x53<<22|0x6)
48 #define BLT_WRITE_ALPHA (1<<21)
49 #define BLT_WRITE_RGB (1<<20)
51 static char device[80];
52 static uint32_t devid;
53 static bool has_64bit_relocations;
55 static void selfcopy(int fd, uint32_t handle, int loops)
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;
64 memset(reloc, 0, sizeof(reloc));
65 memset(gem_exec, 0, sizeof(gem_exec));
66 memset(&execbuf, 0, sizeof(execbuf));
68 *b = COPY_BLT_CMD | BLT_WRITE_ALPHA | BLT_WRITE_RGB;
69 if (has_64bit_relocations)
72 *b++ = 0xcc << 16 | 1 << 25 | 1 << 24 | (4*1024);
74 *b++ = 1 << 16 | 1024;
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;
81 if (has_64bit_relocations)
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;
92 if (has_64bit_relocations)
95 *b++ = MI_BATCH_BUFFER_END;
98 gem_exec[0].handle = handle;
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;
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;
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) {
119 drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
122 drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &create.handle);
125 static uint32_t load(int fd)
129 handle = gem_create(fd, OBJECT_SIZE);
133 selfcopy(fd, handle, 100);
137 static void run(int child)
142 fd = open(device, O_RDWR);
143 igt_assert(fd != -1);
146 if ((child & 63) == 63)
147 gem_read(fd, handle, 0, &handle, sizeof(handle));
153 pthread_mutex_t mutex;
159 static void *thread_run(void *_data)
161 struct thread *t = _data;
162 uint32_t handle = gem_create(t->device, OBJECT_SIZE);
163 struct drm_gem_open arg = { gem_flink(t->device, handle) };
165 pthread_mutex_lock(&t->mutex);
167 pthread_mutex_unlock(&t->mutex);
169 for (int n = 0; n < NUM_FD; n++) {
173 drmIoctl(fd, DRM_IOCTL_GEM_OPEN, &arg);
177 selfcopy(fd, arg.handle, 100);
179 drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &arg.handle);
182 pthread_mutex_lock(&t->mutex);
184 pthread_mutex_unlock(&t->mutex);
186 gem_close(t->device, handle);
190 static void *thread_busy(void *_data)
192 struct thread *t = _data;
193 uint32_t handle = gem_create(t->device, OBJECT_SIZE);
194 struct drm_gem_open arg = { gem_flink(t->device, handle) };
196 pthread_mutex_lock(&t->mutex);
198 struct drm_i915_gem_busy busy;
199 int fd = t->fds[rand() % NUM_FD];
201 pthread_mutex_unlock(&t->mutex);
204 drmIoctl(fd, DRM_IOCTL_GEM_OPEN, &arg);
208 selfcopy(fd, arg.handle, 10);
210 busy.handle = arg.handle;
211 drmIoctl(fd, DRM_IOCTL_I915_GEM_BUSY, &busy);
213 drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &arg.handle);
217 pthread_mutex_lock(&t->mutex);
219 pthread_mutex_unlock(&t->mutex);
221 gem_close(t->device, handle);
227 igt_skip_on_simulation();
232 sprintf(device, "/dev/dri/card%d", drm_get_card());
233 fd = open(device, O_RDWR);
235 igt_assert(fd != -1);
236 devid = intel_get_drm_devid(fd);
237 has_64bit_relocations = intel_gen(devid) >= 8;
241 igt_subtest("process-exit") {
242 igt_fork(child, NUM_FD)
247 igt_subtest("gem-close-race") {
249 struct thread *data = calloc(1, sizeof(struct thread));
254 pthread_mutex_init(&data->mutex, NULL);
255 data->device = open(device, O_RDWR);
256 for (n = 0; n < NUM_FD; n++)
257 data->fds[n] = open(device, O_RDWR);
259 pthread_create(&thread[0], NULL, thread_run, data);
260 pthread_create(&thread[1], NULL, thread_busy, data);
262 for (n = 0; n < 1000*NUM_FD; n++) {
263 int i = rand() % NUM_FD;
264 if (data->fds[i] == -1) {
265 data->fds[i] = open(device, O_RDWR);
272 pthread_mutex_lock(&data->mutex);
274 pthread_mutex_unlock(&data->mutex);
276 pthread_join(thread[1], NULL);
277 pthread_join(thread[0], NULL);
279 for (n = 0; n < NUM_FD; n++)