igt/gem_pwrite_pread: Fix relocation offsets for gen8+
[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 "i915_drm.h"
41 #include "drmtest.h"
42 #include "intel_bufmgr.h"
43 #include "intel_batchbuffer.h"
44 #include "intel_gpu_tools.h"
45
46 #define OBJECT_SIZE 16384
47
48 #define COPY_BLT_CMD            (2<<29|0x53<<22)
49 #define BLT_WRITE_ALPHA         (1<<21)
50 #define BLT_WRITE_RGB           (1<<20)
51 #define BLT_SRC_TILED           (1<<15)
52 #define BLT_DST_TILED           (1<<11)
53
54 uint32_t is_64bit;
55 uint32_t exec_flags;
56
57 static inline void build_batch(uint32_t *batch, int len, uint32_t *batch_len)
58 {
59         unsigned int i = 0;
60
61         batch[i++] = COPY_BLT_CMD | BLT_WRITE_ALPHA | BLT_WRITE_RGB | (is_64bit ? 8 : 6);
62         batch[i++] = 0xcc << 16 | 1 << 25 | 1 << 24 | len;
63         batch[i++] = 0;
64         batch[i++] = 1 << 16 | (len / 4);
65         batch[i++] = 0; /* dst */
66         if (is_64bit)
67                 batch[i++] = 0; /* FIXME */
68         batch[i++] = 0;
69         batch[i++] = len;
70         batch[i++] = 0; /* src */
71         if (is_64bit)
72                 batch[i++] = 0; /* FIXME */
73         batch[i++] = MI_BATCH_BUFFER_END;
74         batch[i++] = 0;
75
76         *batch_len = i * 4;
77 }
78
79 #define GPP_BATCH_SIZE (12 * 4)
80
81 static void copy(int fd, uint32_t src, uint32_t dst, void *buf, int len, int loops)
82 {
83         uint32_t batch[GPP_BATCH_SIZE] = {0};
84
85         struct drm_i915_gem_relocation_entry reloc[] = {
86                 { dst, 0, 4*sizeof(uint32_t), 0, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER },
87                 { src, 0, (is_64bit ? 8 : 7)*sizeof(uint32_t), 0, I915_GEM_DOMAIN_RENDER, 0 },
88         };
89         struct drm_i915_gem_exec_object2 exec[] = {
90                 { src },
91                 { dst },
92                 { gem_create(fd, 4096), 2, (uintptr_t)reloc }
93         };
94         struct drm_i915_gem_execbuffer2 execbuf = {
95                 (uintptr_t)exec, 3,
96                 0, 0,
97                 0, 0, 0, 0,
98                 exec_flags,
99         };
100
101         build_batch(batch, len, &execbuf.batch_len);
102
103         gem_write(fd, exec[2].handle, 0, batch, execbuf.batch_len);
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 batch[GPP_BATCH_SIZE] = {0};
117
118         struct drm_i915_gem_relocation_entry reloc[] = {
119                 { dst, 0, 4*sizeof(uint32_t), 0, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER },
120                 { src, 0, 7*sizeof(uint32_t), 0, I915_GEM_DOMAIN_RENDER, 0 },
121         };
122         struct drm_i915_gem_exec_object2 exec[] = {
123                 { src },
124                 { dst },
125                 { gem_create(fd, 4096), 2, (uintptr_t)reloc }
126         };
127         struct drm_i915_gem_execbuffer2 execbuf = {
128                 (uintptr_t)exec, 3,
129                 0, GPP_BATCH_SIZE,
130                 0, 0, 0, 0,
131                 exec_flags,
132         };
133         uint32_t *src_ptr, *dst_ptr;
134
135         build_batch(batch, len, &execbuf.batch_len);
136
137         gem_write(fd, exec[2].handle, 0, batch, execbuf.batch_len);
138
139         src_ptr = gem_mmap__gtt(fd, src, OBJECT_SIZE, PROT_WRITE);
140         dst_ptr = gem_mmap__gtt(fd, dst, OBJECT_SIZE, PROT_READ);
141
142         while (loops--) {
143                 gem_set_domain(fd, src,
144                                I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
145                 memcpy(src_ptr, buf, len);
146
147                 do_or_die(drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf));
148                 gem_set_domain(fd, dst,
149                                I915_GEM_DOMAIN_GTT, 0);
150                 memcpy(buf, dst_ptr, len);
151         }
152
153         munmap(dst_ptr, len);
154         munmap(src_ptr, len);
155         gem_close(fd, exec[2].handle);
156 }
157
158
159 static void as_cpu_mmap(int fd, uint32_t src, uint32_t dst, void *buf, int len, int loops)
160 {
161         uint32_t batch[GPP_BATCH_SIZE] = {0};
162
163         struct drm_i915_gem_relocation_entry reloc[] = {
164                 { dst, 0, 4*sizeof(uint32_t), 0, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER },
165                 { src, 0, 7*sizeof(uint32_t), 0, I915_GEM_DOMAIN_RENDER, 0 },
166         };
167         struct drm_i915_gem_exec_object2 exec[] = {
168                 { src },
169                 { dst },
170                 { gem_create(fd, 4096), 2, (uintptr_t)reloc }
171         };
172         struct drm_i915_gem_execbuffer2 execbuf = {
173                 (uintptr_t)exec, 3,
174                 0, GPP_BATCH_SIZE,
175                 0, 0, 0, 0,
176                 exec_flags,
177         };
178         uint32_t *src_ptr, *dst_ptr;
179
180         build_batch(batch, len, &execbuf.batch_len);
181
182         gem_write(fd, exec[2].handle, 0, batch, execbuf.batch_len);
183
184         src_ptr = gem_mmap__cpu(fd, src, OBJECT_SIZE, PROT_WRITE);
185         dst_ptr = gem_mmap__cpu(fd, dst, OBJECT_SIZE, PROT_READ);
186
187         while (loops--) {
188                 gem_set_domain(fd, src,
189                                I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
190                 memcpy(src_ptr, buf, len);
191
192                 do_or_die(drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf));
193                 gem_set_domain(fd, dst,
194                                I915_GEM_DOMAIN_CPU, 0);
195                 memcpy(buf, dst_ptr, len);
196         }
197
198         munmap(dst_ptr, len);
199         munmap(src_ptr, len);
200         gem_close(fd, exec[2].handle);
201 }
202
203 static void test_copy(int fd, uint32_t src, uint32_t dst, uint32_t *buf, int len)
204 {
205         uint32_t batch[GPP_BATCH_SIZE] = {0};
206
207         struct drm_i915_gem_relocation_entry reloc[] = {
208                 { dst, 0, 4*sizeof(uint32_t), 0, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER },
209                 { src, 0, 7*sizeof(uint32_t), 0, I915_GEM_DOMAIN_RENDER, 0 },
210         };
211         struct drm_i915_gem_exec_object2 exec[] = {
212                 { src },
213                 { dst },
214                 { gem_create(fd, 4096), 2, (uintptr_t)reloc }
215         };
216         struct drm_i915_gem_execbuffer2 execbuf = {
217                 (uintptr_t)exec, 3,
218                 0, GPP_BATCH_SIZE,
219                 0, 0, 0, 0,
220                 exec_flags,
221         };
222         int i;
223
224         build_batch(batch, len, &execbuf.batch_len);
225
226         gem_write(fd, exec[2].handle, 0, batch, execbuf.batch_len);
227
228         for (i = 0; i < len/4; i++)
229                 buf[i] = i;
230
231         gem_write(fd, src, 0, buf, len);
232         memset(buf, 0, len);
233
234         do_or_die(drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf));
235         gem_read(fd, dst, 0, buf, len);
236
237         gem_close(fd, exec[2].handle);
238
239         for (i = 0; i < len/4; i++)
240                 igt_assert(buf[i] == i);
241 }
242
243 static void test_as_gtt_mmap(int fd, uint32_t src, uint32_t dst, int len)
244 {
245         uint32_t batch[GPP_BATCH_SIZE] = {0};
246
247         struct drm_i915_gem_relocation_entry reloc[] = {
248                 { dst, 0, 4*sizeof(uint32_t), 0, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER },
249                 { src, 0, 7*sizeof(uint32_t), 0, I915_GEM_DOMAIN_RENDER, 0 },
250         };
251         struct drm_i915_gem_exec_object2 exec[] = {
252                 { src },
253                 { dst },
254                 { gem_create(fd, 4096), 2, (uintptr_t)reloc }
255         };
256         struct drm_i915_gem_execbuffer2 execbuf = {
257                 (uintptr_t)exec, 3,
258                 0, GPP_BATCH_SIZE,
259                 0, 0, 0, 0,
260                 exec_flags,
261         };
262         uint32_t *src_ptr, *dst_ptr;
263         int i;
264
265         build_batch(batch, len, &execbuf.batch_len);
266
267         gem_write(fd, exec[2].handle, 0, batch, execbuf.batch_len);
268
269         src_ptr = gem_mmap__gtt(fd, src, OBJECT_SIZE, PROT_WRITE);
270         dst_ptr = gem_mmap__gtt(fd, dst, OBJECT_SIZE, PROT_READ);
271
272         gem_set_domain(fd, src, I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
273         for (i = 0; i < len/4; i++)
274                 src_ptr[i] = i;
275
276         do_or_die(drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf));
277         gem_close(fd, exec[2].handle);
278
279         gem_set_domain(fd, dst, I915_GEM_DOMAIN_GTT, 0);
280         for (i = 0; i < len/4; i++)
281                 igt_assert(dst_ptr[i] == i);
282
283         munmap(dst_ptr, len);
284         munmap(src_ptr, len);
285 }
286
287 static void test_as_cpu_mmap(int fd, uint32_t src, uint32_t dst, int len)
288 {
289         uint32_t batch[GPP_BATCH_SIZE] = {0};
290
291         struct drm_i915_gem_relocation_entry reloc[] = {
292                 { dst, 0, 4*sizeof(uint32_t), 0, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER },
293                 { src, 0, 7*sizeof(uint32_t), 0, I915_GEM_DOMAIN_RENDER, 0 },
294         };
295         struct drm_i915_gem_exec_object2 exec[] = {
296                 { src },
297                 { dst },
298                 { gem_create(fd, 4096), 2, (uintptr_t)reloc }
299         };
300         struct drm_i915_gem_execbuffer2 execbuf = {
301                 (uintptr_t)exec, 3,
302                 0, GPP_BATCH_SIZE,
303                 0, 0, 0, 0,
304                 exec_flags,
305         };
306         uint32_t *src_ptr, *dst_ptr;
307         int i;
308
309         build_batch(batch, len, &execbuf.batch_len);
310
311         gem_write(fd, exec[2].handle, 0, batch, execbuf.batch_len);
312
313         src_ptr = gem_mmap__cpu(fd, src, OBJECT_SIZE, PROT_WRITE);
314         dst_ptr = gem_mmap__cpu(fd, dst, OBJECT_SIZE, PROT_READ);
315
316         gem_set_domain(fd, src, I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
317         for (i = 0; i < len/4; i++)
318                 src_ptr[i] = i;
319
320         do_or_die(drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf));
321         gem_close(fd, exec[2].handle);
322
323         gem_set_domain(fd, dst, I915_GEM_DOMAIN_CPU, 0);
324         for (i = 0; i < len/4; i++)
325                 igt_assert(dst_ptr[i] == i);
326
327         munmap(dst_ptr, len);
328         munmap(src_ptr, len);
329 }
330
331 static double elapsed(const struct timeval *start,
332                       const struct timeval *end,
333                       int loop)
334 {
335         return (1e6*(end->tv_sec - start->tv_sec) + (end->tv_usec - start->tv_usec))/loop;
336 }
337
338 static const char *bytes_per_sec(char *buf, double v)
339 {
340         const char *order[] = {
341                 "",
342                 "KiB",
343                 "MiB",
344                 "GiB",
345                 "TiB",
346                 NULL,
347         }, **o = order;
348
349         while (v > 1000 && o[1]) {
350                 v /= 1000;
351                 o++;
352         }
353         sprintf(buf, "%.1f%s/s", v, *o);
354         return buf;
355 }
356
357 uint32_t *tmp, src, dst;
358 int fd;
359
360 int main(int argc, char **argv)
361 {
362         int object_size = 0;
363         uint32_t buf[20];
364         int count;
365
366         igt_subtest_init(argc, argv);
367         igt_skip_on_simulation();
368
369         if (argc > 1)
370                 object_size = atoi(argv[1]);
371         if (object_size == 0)
372                 object_size = OBJECT_SIZE;
373         object_size = (object_size + 3) & -4;
374
375         igt_fixture {
376                 uint32_t devid;
377
378                 fd = drm_open_any();
379
380                 dst = gem_create(fd, object_size);
381                 src = gem_create(fd, object_size);
382                 tmp = malloc(object_size);
383
384                 gem_set_caching(fd, src, 0);
385                 gem_set_caching(fd, dst, 0);
386
387                 devid = intel_get_drm_devid(fd);
388                 is_64bit = intel_gen(devid) >= 8;
389                 exec_flags = HAS_BLT_RING(devid) ? I915_EXEC_BLT : 0;
390         }
391
392         igt_subtest("uncached-copy-correctness")
393                 test_copy(fd, src, dst, tmp, object_size);
394         igt_subtest("uncached-copy-performance") {
395                 for (count = 1; count <= 1<<17; count <<= 1) {
396                         struct timeval start, end;
397
398                         gettimeofday(&start, NULL);
399                         copy(fd, src, dst, tmp, object_size, count);
400                         gettimeofday(&end, NULL);
401                         printf("Time to uncached copy %d bytes x %6d:   %7.3fµs, %s\n",
402                                object_size, count,
403                                elapsed(&start, &end, count),
404                                bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
405                         fflush(stdout);
406                 }
407         }
408
409         igt_subtest("uncached-pwrite-blt-gtt_mmap-correctness")
410                 test_as_gtt_mmap(fd, src, dst, object_size);
411         igt_subtest("uncached-pwrite-blt-gtt_mmap-performance") {
412                 for (count = 1; count <= 1<<17; count <<= 1) {
413                         struct timeval start, end;
414
415                         gettimeofday(&start, NULL);
416                         as_gtt_mmap(fd, src, dst, tmp, object_size, count);
417                         gettimeofday(&end, NULL);
418                         printf("** mmap uncached copy %d bytes x %6d:   %7.3fµs, %s\n",
419                                object_size, count,
420                                elapsed(&start, &end, count),
421                                bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
422                         fflush(stdout);
423                 }
424         }
425
426         igt_fixture {
427                 gem_set_caching(fd, src, 1);
428                 gem_set_caching(fd, dst, 1);
429         }
430
431         igt_subtest("snooped-copy-correctness")
432                 test_copy(fd, src, dst, tmp, object_size);
433         igt_subtest("snooped-copy-performance") {
434                 for (count = 1; count <= 1<<17; count <<= 1) {
435                         struct timeval start, end;
436
437                         gettimeofday(&start, NULL);
438                         copy(fd, src, dst, tmp, object_size, count);
439                         gettimeofday(&end, NULL);
440                         printf("Time to snooped copy %d bytes x %6d:    %7.3fµs, %s\n",
441                                object_size, count,
442                                elapsed(&start, &end, count),
443                                bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
444                         fflush(stdout);
445                 }
446         }
447
448         igt_subtest("snooped-pwrite-blt-cpu_mmap-correctness")
449                 test_as_cpu_mmap(fd, src, dst, object_size);
450         igt_subtest("snooped-pwrite-blt-cpu_mmap-performance") {
451                 for (count = 1; count <= 1<<17; count <<= 1) {
452                         struct timeval start, end;
453
454                         gettimeofday(&start, NULL);
455                         as_cpu_mmap(fd, src, dst, tmp, object_size, count);
456                         gettimeofday(&end, NULL);
457                         printf("** mmap snooped copy %d bytes x %6d:    %7.3fµs, %s\n",
458                                object_size, count,
459                                elapsed(&start, &end, count),
460                                bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
461                         fflush(stdout);
462                 }
463         }
464
465         igt_fixture {
466                 gem_set_caching(fd, src, 2);
467                 gem_set_caching(fd, dst, 2);
468         }
469
470         igt_subtest("display-copy-correctness")
471                 test_copy(fd, src, dst, tmp, object_size);
472         igt_subtest("display-copy-performance") {
473                 for (count = 1; count <= 1<<17; count <<= 1) {
474                         struct timeval start, end;
475
476                         gettimeofday(&start, NULL);
477                         copy(fd, src, dst, tmp, object_size, count);
478                         gettimeofday(&end, NULL);
479                         printf("Time to display copy %d bytes x %6d:    %7.3fµs, %s\n",
480                                object_size, count,
481                                elapsed(&start, &end, count),
482                                bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
483                         fflush(stdout);
484                 }
485         }
486
487         igt_subtest("display-pwrite-blt-gtt_mmap-correctness")
488                 test_as_gtt_mmap(fd, src, dst, object_size);
489         igt_subtest("display-pwrite-blt-gtt_mmap-performance") {
490                 for (count = 1; count <= 1<<17; count <<= 1) {
491                         struct timeval start, end;
492
493                         gettimeofday(&start, NULL);
494                         as_gtt_mmap(fd, src, dst, tmp, object_size, count);
495                         gettimeofday(&end, NULL);
496                         printf("** mmap display copy %d bytes x %6d:    %7.3fµs, %s\n",
497                                object_size, count,
498                                elapsed(&start, &end, count),
499                                bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
500                         fflush(stdout);
501                 }
502         }
503
504         igt_fixture {
505                 free(tmp);
506                 gem_close(fd, src);
507                 gem_close(fd, dst);
508
509                 close(fd);
510         }
511
512         igt_exit();
513 }