igt/gem_userptr_blits: Fix forked access test
[platform/upstream/intel-gpu-tools.git] / tests / gem_pwrite_pread.c
1 /*
2  * Copyright © 2011 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 "ioctl_wrappers.h"
41 #include "drmtest.h"
42 #include "intel_chipset.h"
43 #include "intel_bufmgr.h"
44 #include "intel_batchbuffer.h"
45 #include "intel_io.h"
46
47 #define OBJECT_SIZE 16384
48
49 #define COPY_BLT_CMD            (2<<29|0x53<<22)
50 #define BLT_WRITE_ALPHA         (1<<21)
51 #define BLT_WRITE_RGB           (1<<20)
52 #define BLT_SRC_TILED           (1<<15)
53 #define BLT_DST_TILED           (1<<11)
54
55 uint32_t is_64bit;
56 uint32_t exec_flags;
57
58 static inline void build_batch(uint32_t *batch, int len, uint32_t *batch_len)
59 {
60         unsigned int i = 0;
61
62         batch[i++] = COPY_BLT_CMD | BLT_WRITE_ALPHA | BLT_WRITE_RGB | (is_64bit ? 8 : 6);
63         batch[i++] = 0xcc << 16 | 1 << 25 | 1 << 24 | len;
64         batch[i++] = 0;
65         batch[i++] = 1 << 16 | (len / 4);
66         batch[i++] = 0; /* dst */
67         if (is_64bit)
68                 batch[i++] = 0;
69         batch[i++] = 0;
70         batch[i++] = len;
71         batch[i++] = 0; /* src */
72         if (is_64bit)
73                 batch[i++] = 0;
74         batch[i++] = MI_BATCH_BUFFER_END;
75         batch[i++] = 0;
76
77         *batch_len = i * 4;
78 }
79
80 #define BUILD_EXEC \
81         uint32_t batch[12]; \
82         struct drm_i915_gem_relocation_entry reloc[] = { \
83                 { dst, 0, 4*sizeof(uint32_t), 0, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER }, \
84                 { src, 0, (is_64bit ? 8 : 7)*sizeof(uint32_t), 0, I915_GEM_DOMAIN_RENDER, 0 }, \
85         }; \
86         struct drm_i915_gem_exec_object2 exec[] = { \
87                 { src }, \
88                 { dst }, \
89                 { gem_create(fd, 4096), 2, (uintptr_t)reloc } \
90         }; \
91         struct drm_i915_gem_execbuffer2 execbuf = { \
92                 (uintptr_t)exec, 3, \
93                 0, 0, \
94                 0, 0, 0, 0, \
95                 exec_flags, \
96         }; \
97         build_batch(batch, len, &execbuf.batch_len); \
98         gem_write(fd, exec[2].handle, 0, batch, execbuf.batch_len);
99
100
101 static void copy(int fd, uint32_t src, uint32_t dst, void *buf, int len, int loops)
102 {
103         BUILD_EXEC;
104
105         while (loops--) {
106                 gem_write(fd, src, 0, buf, len);
107                 do_or_die(drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf));
108                 gem_read(fd, dst, 0, buf, len);
109         }
110
111         gem_close(fd, exec[2].handle);
112 }
113
114 static void as_gtt_mmap(int fd, uint32_t src, uint32_t dst, void *buf, int len, int loops)
115 {
116         uint32_t *src_ptr, *dst_ptr;
117         BUILD_EXEC;
118
119         src_ptr = gem_mmap__gtt(fd, src, OBJECT_SIZE, PROT_WRITE);
120         dst_ptr = gem_mmap__gtt(fd, dst, OBJECT_SIZE, PROT_READ);
121
122         while (loops--) {
123                 gem_set_domain(fd, src,
124                                I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
125                 memcpy(src_ptr, buf, len);
126
127                 do_or_die(drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf));
128                 gem_set_domain(fd, dst,
129                                I915_GEM_DOMAIN_GTT, 0);
130                 memcpy(buf, dst_ptr, len);
131         }
132
133         munmap(dst_ptr, len);
134         munmap(src_ptr, len);
135         gem_close(fd, exec[2].handle);
136 }
137
138
139 static void as_cpu_mmap(int fd, uint32_t src, uint32_t dst, void *buf, int len, int loops)
140 {
141         uint32_t *src_ptr, *dst_ptr;
142         BUILD_EXEC;
143
144         src_ptr = gem_mmap__cpu(fd, src, OBJECT_SIZE, PROT_WRITE);
145         dst_ptr = gem_mmap__cpu(fd, dst, OBJECT_SIZE, PROT_READ);
146
147         while (loops--) {
148                 gem_set_domain(fd, src,
149                                I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
150                 memcpy(src_ptr, buf, len);
151
152                 do_or_die(drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf));
153                 gem_set_domain(fd, dst,
154                                I915_GEM_DOMAIN_CPU, 0);
155                 memcpy(buf, dst_ptr, len);
156         }
157
158         munmap(dst_ptr, len);
159         munmap(src_ptr, len);
160         gem_close(fd, exec[2].handle);
161 }
162
163 static void test_copy(int fd, uint32_t src, uint32_t dst, uint32_t *buf, int len)
164 {
165         int i;
166         BUILD_EXEC;
167
168         for (i = 0; i < len/4; i++)
169                 buf[i] = i;
170
171         gem_write(fd, src, 0, buf, len);
172         memset(buf, 0, len);
173
174         do_or_die(drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf));
175         gem_read(fd, dst, 0, buf, len);
176
177         gem_close(fd, exec[2].handle);
178
179         for (i = 0; i < len/4; i++)
180                 igt_assert(buf[i] == i);
181 }
182
183 static void test_as_gtt_mmap(int fd, uint32_t src, uint32_t dst, int len)
184 {
185         uint32_t *src_ptr, *dst_ptr;
186         int i;
187         BUILD_EXEC;
188
189         src_ptr = gem_mmap__gtt(fd, src, OBJECT_SIZE, PROT_WRITE);
190         dst_ptr = gem_mmap__gtt(fd, dst, OBJECT_SIZE, PROT_READ);
191
192         gem_set_domain(fd, src, I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
193         for (i = 0; i < len/4; i++)
194                 src_ptr[i] = i;
195
196         do_or_die(drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf));
197         gem_close(fd, exec[2].handle);
198
199         gem_set_domain(fd, dst, I915_GEM_DOMAIN_GTT, 0);
200         for (i = 0; i < len/4; i++)
201                 igt_assert(dst_ptr[i] == i);
202
203         munmap(dst_ptr, len);
204         munmap(src_ptr, len);
205 }
206
207 static void test_as_cpu_mmap(int fd, uint32_t src, uint32_t dst, int len)
208 {
209         uint32_t *src_ptr, *dst_ptr;
210         int i;
211         BUILD_EXEC;
212
213         src_ptr = gem_mmap__cpu(fd, src, OBJECT_SIZE, PROT_WRITE);
214         dst_ptr = gem_mmap__cpu(fd, dst, OBJECT_SIZE, PROT_READ);
215
216         gem_set_domain(fd, src, I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
217         for (i = 0; i < len/4; i++)
218                 src_ptr[i] = i;
219
220         do_or_die(drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf));
221         gem_close(fd, exec[2].handle);
222
223         gem_set_domain(fd, dst, I915_GEM_DOMAIN_CPU, 0);
224         for (i = 0; i < len/4; i++)
225                 igt_assert(dst_ptr[i] == i);
226
227         munmap(dst_ptr, len);
228         munmap(src_ptr, len);
229 }
230
231 static double elapsed(const struct timeval *start,
232                       const struct timeval *end,
233                       int loop)
234 {
235         return (1e6*(end->tv_sec - start->tv_sec) + (end->tv_usec - start->tv_usec))/loop;
236 }
237
238 static const char *bytes_per_sec(char *buf, double v)
239 {
240         const char *order[] = {
241                 "",
242                 "KiB",
243                 "MiB",
244                 "GiB",
245                 "TiB",
246                 NULL,
247         }, **o = order;
248
249         while (v > 1000 && o[1]) {
250                 v /= 1000;
251                 o++;
252         }
253         sprintf(buf, "%.1f%s/s", v, *o);
254         return buf;
255 }
256
257 uint32_t *tmp, src, dst;
258 int fd;
259
260 int main(int argc, char **argv)
261 {
262         int object_size = 0;
263         uint32_t buf[20];
264         int count;
265
266         igt_subtest_init(argc, argv);
267         igt_skip_on_simulation();
268
269         if (argc > 1)
270                 object_size = atoi(argv[1]);
271         if (object_size == 0)
272                 object_size = OBJECT_SIZE;
273         object_size = (object_size + 3) & -4;
274
275         igt_fixture {
276                 uint32_t devid;
277
278                 fd = drm_open_any();
279
280                 dst = gem_create(fd, object_size);
281                 src = gem_create(fd, object_size);
282                 tmp = malloc(object_size);
283
284                 gem_set_caching(fd, src, 0);
285                 gem_set_caching(fd, dst, 0);
286
287                 devid = intel_get_drm_devid(fd);
288                 is_64bit = intel_gen(devid) >= 8;
289                 exec_flags = HAS_BLT_RING(devid) ? I915_EXEC_BLT : 0;
290         }
291
292         igt_subtest("uncached-copy-correctness")
293                 test_copy(fd, src, dst, tmp, object_size);
294         igt_subtest("uncached-copy-performance") {
295                 for (count = 1; count <= 1<<17; count <<= 1) {
296                         struct timeval start, end;
297
298                         gettimeofday(&start, NULL);
299                         copy(fd, src, dst, tmp, object_size, count);
300                         gettimeofday(&end, NULL);
301                         igt_info("Time to uncached copy %d bytes x %6d: %7.3fµs, %s\n",
302                                  object_size, count,
303                                  elapsed(&start, &end, count),
304                                  bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
305                         fflush(stdout);
306                 }
307         }
308
309         igt_subtest("uncached-pwrite-blt-gtt_mmap-correctness")
310                 test_as_gtt_mmap(fd, src, dst, object_size);
311         igt_subtest("uncached-pwrite-blt-gtt_mmap-performance") {
312                 for (count = 1; count <= 1<<17; count <<= 1) {
313                         struct timeval start, end;
314
315                         gettimeofday(&start, NULL);
316                         as_gtt_mmap(fd, src, dst, tmp, object_size, count);
317                         gettimeofday(&end, NULL);
318                         igt_info("** mmap uncached copy %d bytes x %6d: %7.3fµs, %s\n",
319                                  object_size, count,
320                                  elapsed(&start, &end, count),
321                                  bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
322                         fflush(stdout);
323                 }
324         }
325
326         igt_fixture {
327                 gem_set_caching(fd, src, 1);
328                 gem_set_caching(fd, dst, 1);
329         }
330
331         igt_subtest("snooped-copy-correctness")
332                 test_copy(fd, src, dst, tmp, object_size);
333         igt_subtest("snooped-copy-performance") {
334                 for (count = 1; count <= 1<<17; count <<= 1) {
335                         struct timeval start, end;
336
337                         gettimeofday(&start, NULL);
338                         copy(fd, src, dst, tmp, object_size, count);
339                         gettimeofday(&end, NULL);
340                         igt_info("Time to snooped copy %d bytes x %6d:  %7.3fµs, %s\n",
341                                  object_size, count,
342                                  elapsed(&start, &end, count),
343                                  bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
344                         fflush(stdout);
345                 }
346         }
347
348         igt_subtest("snooped-pwrite-blt-cpu_mmap-correctness")
349                 test_as_cpu_mmap(fd, src, dst, object_size);
350         igt_subtest("snooped-pwrite-blt-cpu_mmap-performance") {
351                 for (count = 1; count <= 1<<17; count <<= 1) {
352                         struct timeval start, end;
353
354                         gettimeofday(&start, NULL);
355                         as_cpu_mmap(fd, src, dst, tmp, object_size, count);
356                         gettimeofday(&end, NULL);
357                         igt_info("** mmap snooped copy %d bytes x %6d:  %7.3fµs, %s\n",
358                                  object_size, count,
359                                  elapsed(&start, &end, count),
360                                  bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
361                         fflush(stdout);
362                 }
363         }
364
365         igt_fixture {
366                 gem_set_caching(fd, src, 2);
367                 gem_set_caching(fd, dst, 2);
368         }
369
370         igt_subtest("display-copy-correctness")
371                 test_copy(fd, src, dst, tmp, object_size);
372         igt_subtest("display-copy-performance") {
373                 for (count = 1; count <= 1<<17; count <<= 1) {
374                         struct timeval start, end;
375
376                         gettimeofday(&start, NULL);
377                         copy(fd, src, dst, tmp, object_size, count);
378                         gettimeofday(&end, NULL);
379                         igt_info("Time to display copy %d bytes x %6d:  %7.3fµs, %s\n",
380                                  object_size, count,
381                                  elapsed(&start, &end, count),
382                                  bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
383                         fflush(stdout);
384                 }
385         }
386
387         igt_subtest("display-pwrite-blt-gtt_mmap-correctness")
388                 test_as_gtt_mmap(fd, src, dst, object_size);
389         igt_subtest("display-pwrite-blt-gtt_mmap-performance") {
390                 for (count = 1; count <= 1<<17; count <<= 1) {
391                         struct timeval start, end;
392
393                         gettimeofday(&start, NULL);
394                         as_gtt_mmap(fd, src, dst, tmp, object_size, count);
395                         gettimeofday(&end, NULL);
396                         igt_info("** mmap display copy %d bytes x %6d:  %7.3fµs, %s\n",
397                                  object_size, count,
398                                  elapsed(&start, &end, count),
399                                  bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
400                         fflush(stdout);
401                 }
402         }
403
404         igt_fixture {
405                 free(tmp);
406                 gem_close(fd, src);
407                 gem_close(fd, dst);
408
409                 close(fd);
410         }
411
412         igt_exit();
413 }