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 1024*1024*4
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++ = 512 << 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, 30);
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;
158 static void *thread_run(void *_data)
160 struct thread *t = _data;
162 pthread_mutex_lock(&t->mutex);
164 pthread_mutex_unlock(&t->mutex);
166 for (int n = 0; n < NUM_FD; n++) {
167 struct drm_i915_gem_create create;
170 create.size = OBJECT_SIZE;
171 drmIoctl(t->fds[n], DRM_IOCTL_I915_GEM_CREATE, &create);
172 if (create.handle == 0)
175 selfcopy(t->fds[n], create.handle, 10);
177 drmIoctl(t->fds[n], DRM_IOCTL_GEM_CLOSE, &create.handle);
180 pthread_mutex_lock(&t->mutex);
182 pthread_mutex_unlock(&t->mutex);
187 static void *thread_busy(void *_data)
189 struct thread *t = _data;
192 pthread_mutex_lock(&t->mutex);
194 struct drm_i915_gem_create create;
195 struct drm_i915_gem_busy busy;
197 pthread_mutex_unlock(&t->mutex);
202 create.size = OBJECT_SIZE;
203 drmIoctl(t->fds[n], DRM_IOCTL_I915_GEM_CREATE, &create);
204 if (create.handle == 0)
207 selfcopy(t->fds[n], create.handle, 1);
209 busy.handle = create.handle;
210 drmIoctl(t->fds[n], DRM_IOCTL_I915_GEM_BUSY, &busy);
212 drmIoctl(t->fds[n], DRM_IOCTL_GEM_CLOSE, &create.handle);
216 pthread_mutex_lock(&t->mutex);
218 pthread_mutex_unlock(&t->mutex);
225 igt_skip_on_simulation();
230 sprintf(device, "/dev/dri/card%d", drm_get_card());
231 fd = open(device, O_RDWR);
233 igt_assert(fd != -1);
234 devid = intel_get_drm_devid(fd);
235 has_64bit_relocations = intel_gen(devid) >= 8;
239 igt_subtest("process-exit") {
240 igt_fork(child, NUM_FD)
245 igt_subtest("gem-close-race") {
247 struct thread *data = calloc(1, sizeof(struct thread));
252 pthread_mutex_init(&data->mutex, NULL);
253 for (n = 0; n < NUM_FD; n++)
254 data->fds[n] = open(device, O_RDWR);
256 pthread_create(&thread[0], NULL, thread_run, data);
257 pthread_create(&thread[1], NULL, thread_busy, data);
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);
269 pthread_mutex_lock(&data->mutex);
271 pthread_mutex_unlock(&data->mutex);
273 pthread_join(thread[1], NULL);
274 pthread_join(thread[0], NULL);
276 for (n = 0; n < NUM_FD; n++)