blob: 61b1c2a002d04e9f6131d07aae803d9c487fe6d6 [file] [log] [blame]
Daniel Vetter3dba47e2013-08-06 22:27:37 +02001/*
2 * Copyright © 2009,2012,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 * Eric Anholt <eric@anholt.net>
25 * Chris Wilson <chris@chris-wilson.co.uk>
26 * Daniel Vetter <daniel.vetter@ffwll.ch>
27 *
28 */
29
Daniel Vetter43779e32013-08-14 14:50:50 +020030/** @file gem_concurrent_blit.c
Daniel Vetter3dba47e2013-08-06 22:27:37 +020031 *
32 * This is a test of pread/pwrite behavior when writing to active
33 * buffers.
34 *
35 * Based on gem_gtt_concurrent_blt.
36 */
37
38#include <stdlib.h>
39#include <stdio.h>
40#include <string.h>
Daniel Vetter3dba47e2013-08-06 22:27:37 +020041#include <fcntl.h>
42#include <inttypes.h>
43#include <errno.h>
44#include <sys/stat.h>
45#include <sys/time.h>
Chris Wilson99431a42013-08-14 11:03:34 +010046#include <sys/wait.h>
Daniel Vetterf5daeec2014-03-23 13:35:09 +010047
48#include <drm.h>
49
Daniel Vettere49ceb82014-03-22 21:07:37 +010050#include "ioctl_wrappers.h"
Daniel Vetter3dba47e2013-08-06 22:27:37 +020051#include "drmtest.h"
52#include "intel_bufmgr.h"
53#include "intel_batchbuffer.h"
Daniel Vetterc03c6ce2014-03-22 21:34:29 +010054#include "intel_io.h"
Daniel Vettere49ceb82014-03-22 21:07:37 +010055#include "intel_chipset.h"
Daniel Vetterf5daeec2014-03-23 13:35:09 +010056#include "igt_aux.h"
Daniel Vetter3dba47e2013-08-06 22:27:37 +020057
58static void
Daniel Vetter43779e32013-08-14 14:50:50 +020059prw_set_bo(drm_intel_bo *bo, uint32_t val, int width, int height)
Daniel Vetter3dba47e2013-08-06 22:27:37 +020060{
Chris Wilson0b4c33f2014-01-26 14:36:32 +000061 int size = width * height, i;
62 uint32_t *tmp;
Daniel Vetter3dba47e2013-08-06 22:27:37 +020063
Chris Wilson0b4c33f2014-01-26 14:36:32 +000064 tmp = malloc(4*size);
65 if (tmp) {
66 for (i = 0; i < size; i++)
67 tmp[i] = val;
68 drm_intel_bo_subdata(bo, 0, 4*size, tmp);
69 free(tmp);
70 } else {
71 for (i = 0; i < size; i++)
72 drm_intel_bo_subdata(bo, 4*i, 4, &val);
73 }
Daniel Vetter3dba47e2013-08-06 22:27:37 +020074}
75
76static void
Daniel Vetter43779e32013-08-14 14:50:50 +020077prw_cmp_bo(drm_intel_bo *bo, uint32_t val, int width, int height)
Daniel Vetter3dba47e2013-08-06 22:27:37 +020078{
Chris Wilson0b4c33f2014-01-26 14:36:32 +000079 int size = width * height, i;
80 uint32_t *tmp;
Daniel Vetter3dba47e2013-08-06 22:27:37 +020081
Chris Wilson0b4c33f2014-01-26 14:36:32 +000082 tmp = malloc(4*size);
83 if (tmp) {
84 memset(tmp, 0, 4*size);
85 do_or_die(drm_intel_bo_get_subdata(bo, 0, 4*size, tmp));
86 for (i = 0; i < size; i++)
87 igt_assert(tmp[i] == val);
88 free(tmp);
89 } else {
90 uint32_t t;
91 for (i = 0; i < size; i++) {
92 t = 0;
93 do_or_die(drm_intel_bo_get_subdata(bo, 4*i, 4, &t));
94 igt_assert(t == val);
95 }
96 }
Daniel Vetter3dba47e2013-08-06 22:27:37 +020097}
98
99static drm_intel_bo *
Daniel Vetter43779e32013-08-14 14:50:50 +0200100unmapped_create_bo(drm_intel_bufmgr *bufmgr, uint32_t val, int width, int height)
Daniel Vetter3dba47e2013-08-06 22:27:37 +0200101{
102 drm_intel_bo *bo;
103
104 bo = drm_intel_bo_alloc(bufmgr, "bo", 4*width*height, 0);
Daniel Vetter83440952013-08-13 12:35:58 +0200105 igt_assert(bo);
Daniel Vetter3dba47e2013-08-06 22:27:37 +0200106
107 return bo;
108}
109
Daniel Vetter43779e32013-08-14 14:50:50 +0200110static void
111gtt_set_bo(drm_intel_bo *bo, uint32_t val, int width, int height)
Daniel Vetter3dba47e2013-08-06 22:27:37 +0200112{
Daniel Vetter43779e32013-08-14 14:50:50 +0200113 int size = width * height;
114 uint32_t *vaddr;
115
116 drm_intel_gem_bo_start_gtt_access(bo, true);
117 vaddr = bo->virtual;
118 while (size--)
119 *vaddr++ = val;
120}
121
122static void
123gtt_cmp_bo(drm_intel_bo *bo, uint32_t val, int width, int height)
124{
125 int size = width * height;
126 uint32_t *vaddr;
127
128 drm_intel_gem_bo_start_gtt_access(bo, false);
129 vaddr = bo->virtual;
130 while (size--)
131 igt_assert(*vaddr++ == val);
132}
133
134static drm_intel_bo *
135gtt_create_bo(drm_intel_bufmgr *bufmgr, uint32_t val, int width, int height)
136{
137 drm_intel_bo *bo;
138
139 bo = drm_intel_bo_alloc(bufmgr, "bo", 4*width*height, 0);
140 igt_assert(bo);
141
142 /* gtt map doesn't have a write parameter, so just keep the mapping
143 * around (to avoid the set_domain with the gtt write domain set) and
144 * manually tell the kernel when we start access the gtt. */
145 do_or_die(drm_intel_gem_bo_map_gtt(bo));
146
147 return bo;
148}
149
150static void
151cpu_set_bo(drm_intel_bo *bo, uint32_t val, int width, int height)
152{
153 int size = width * height;
154 uint32_t *vaddr;
155
156 do_or_die(drm_intel_bo_map(bo, true));
157 vaddr = bo->virtual;
158 while (size--)
159 *vaddr++ = val;
160 drm_intel_bo_unmap(bo);
161}
162
163static void
164cpu_cmp_bo(drm_intel_bo *bo, uint32_t val, int width, int height)
165{
166 int size = width * height;
167 uint32_t *vaddr;
168
169 do_or_die(drm_intel_bo_map(bo, false));
170 vaddr = bo->virtual;
171 while (size--)
172 igt_assert(*vaddr++ == val);
173 drm_intel_bo_unmap(bo);
174}
175
176struct access_mode {
177 void (*set_bo)(drm_intel_bo *bo, uint32_t val, int w, int h);
178 void (*cmp_bo)(drm_intel_bo *bo, uint32_t val, int w, int h);
179 drm_intel_bo *(*create_bo)(drm_intel_bufmgr *bufmgr,
180 uint32_t val, int width, int height);
181 const char *name;
182};
183
184struct access_mode access_modes[] = {
185 { .set_bo = prw_set_bo, .cmp_bo = prw_cmp_bo,
186 .create_bo = unmapped_create_bo, .name = "prw" },
187 { .set_bo = cpu_set_bo, .cmp_bo = cpu_cmp_bo,
188 .create_bo = unmapped_create_bo, .name = "cpu" },
189 { .set_bo = gtt_set_bo, .cmp_bo = gtt_cmp_bo,
190 .create_bo = gtt_create_bo, .name = "gtt" },
191};
192
Chris Wilson1ca607b2013-08-16 09:44:13 +0100193#define MAX_NUM_BUFFERS 1024
194int num_buffers = MAX_NUM_BUFFERS, fd;
Daniel Vetter43779e32013-08-14 14:50:50 +0200195drm_intel_bufmgr *bufmgr;
196struct intel_batchbuffer *batch;
Daniel Vetter5a598c92013-08-14 15:08:05 +0200197int width = 512, height = 512;
198
199static void do_overwrite_source(struct access_mode *mode,
200 drm_intel_bo **src, drm_intel_bo **dst,
201 drm_intel_bo *dummy)
202{
203 int i;
204
205 gem_quiescent_gpu(fd);
206 for (i = 0; i < num_buffers; i++) {
207 mode->set_bo(src[i], i, width, height);
208 mode->set_bo(dst[i], i, width, height);
209 }
210 for (i = 0; i < num_buffers; i++)
Daniel Vettereaccd442014-03-13 03:35:02 +0100211 intel_copy_bo(batch, dst[i], src[i], width*height*4);
Daniel Vetter5a598c92013-08-14 15:08:05 +0200212 for (i = num_buffers; i--; )
213 mode->set_bo(src[i], 0xdeadbeef, width, height);
214 for (i = 0; i < num_buffers; i++)
215 mode->cmp_bo(dst[i], i, width, height);
216}
217
218static void do_early_read(struct access_mode *mode,
219 drm_intel_bo **src, drm_intel_bo **dst,
220 drm_intel_bo *dummy)
221{
222 int i;
223
224 gem_quiescent_gpu(fd);
225 for (i = num_buffers; i--; )
226 mode->set_bo(src[i], 0xdeadbeef, width, height);
227 for (i = 0; i < num_buffers; i++)
Daniel Vettereaccd442014-03-13 03:35:02 +0100228 intel_copy_bo(batch, dst[i], src[i], width*height*4);
Daniel Vetter5a598c92013-08-14 15:08:05 +0200229 for (i = num_buffers; i--; )
230 mode->cmp_bo(dst[i], 0xdeadbeef, width, height);
231}
232
233static void do_gpu_read_after_write(struct access_mode *mode,
234 drm_intel_bo **src, drm_intel_bo **dst,
235 drm_intel_bo *dummy)
236{
237 int i;
238
239 gem_quiescent_gpu(fd);
240 for (i = num_buffers; i--; )
241 mode->set_bo(src[i], 0xabcdabcd, width, height);
242 for (i = 0; i < num_buffers; i++)
Daniel Vettereaccd442014-03-13 03:35:02 +0100243 intel_copy_bo(batch, dst[i], src[i], width*height*4);
Daniel Vetter5a598c92013-08-14 15:08:05 +0200244 for (i = num_buffers; i--; )
Daniel Vettereaccd442014-03-13 03:35:02 +0100245 intel_copy_bo(batch, dummy, dst[i], width*height*4);
Daniel Vetter5a598c92013-08-14 15:08:05 +0200246 for (i = num_buffers; i--; )
247 mode->cmp_bo(dst[i], 0xabcdabcd, width, height);
248}
249
Daniel Vetterec283d62013-08-14 15:18:37 +0200250typedef void (*do_test)(struct access_mode *mode,
251 drm_intel_bo **src, drm_intel_bo **dst,
252 drm_intel_bo *dummy);
253
254typedef void (*run_wrap)(struct access_mode *mode,
255 drm_intel_bo **src, drm_intel_bo **dst,
256 drm_intel_bo *dummy,
257 do_test do_test_func);
258
259static void run_single(struct access_mode *mode,
260 drm_intel_bo **src, drm_intel_bo **dst,
261 drm_intel_bo *dummy,
262 do_test do_test_func)
263{
264 do_test_func(mode, src, dst, dummy);
265}
266
267
Chris Wilson1ca607b2013-08-16 09:44:13 +0100268static void run_interruptible(struct access_mode *mode,
269 drm_intel_bo **src, drm_intel_bo **dst,
270 drm_intel_bo *dummy,
271 do_test do_test_func)
Daniel Vetterec283d62013-08-14 15:18:37 +0200272{
273 int loop;
274
Chris Wilson1ca607b2013-08-16 09:44:13 +0100275 igt_fork_signal_helper();
276
Daniel Vetterec283d62013-08-14 15:18:37 +0200277 for (loop = 0; loop < 10; loop++)
278 do_test_func(mode, src, dst, dummy);
Chris Wilson1ca607b2013-08-16 09:44:13 +0100279
280 igt_stop_signal_helper();
Daniel Vetterec283d62013-08-14 15:18:37 +0200281}
282
283static void run_forked(struct access_mode *mode,
284 drm_intel_bo **src, drm_intel_bo **dst,
285 drm_intel_bo *dummy,
286 do_test do_test_func)
287{
Chris Wilson1ca607b2013-08-16 09:44:13 +0100288 const int old_num_buffers = num_buffers;
Daniel Vetterec283d62013-08-14 15:18:37 +0200289
Daniel Vettercd1f2202013-08-29 10:06:51 +0200290 num_buffers /= 16;
Chris Wilson1ca607b2013-08-16 09:44:13 +0100291 num_buffers += 2;
292
293 igt_fork_signal_helper();
294
Daniel Vettercd1f2202013-08-29 10:06:51 +0200295 igt_fork(child, 16) {
Daniel Vettercd1f2202013-08-29 10:06:51 +0200296 /* recreate process local variables */
297 bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
298 drm_intel_bufmgr_gem_enable_reuse(bufmgr);
299 batch = intel_batchbuffer_alloc(bufmgr, intel_get_drm_devid(fd));
300 for (int i = 0; i < num_buffers; i++) {
301 src[i] = mode->create_bo(bufmgr, i, width, height);
302 dst[i] = mode->create_bo(bufmgr, ~i, width, height);
Daniel Vetterec283d62013-08-14 15:18:37 +0200303 }
Daniel Vettercd1f2202013-08-29 10:06:51 +0200304 dummy = mode->create_bo(bufmgr, 0, width, height);
305 for (int loop = 0; loop < 10; loop++)
306 do_test_func(mode, src, dst, dummy);
307 /* as we borrow the fd, we need to reap our bo */
308 for (int i = 0; i < num_buffers; i++) {
309 drm_intel_bo_unreference(src[i]);
310 drm_intel_bo_unreference(dst[i]);
311 }
312 drm_intel_bo_unreference(dummy);
313 intel_batchbuffer_free(batch);
314 drm_intel_bufmgr_destroy(bufmgr);
Daniel Vetterec283d62013-08-14 15:18:37 +0200315 }
Daniel Vettercd1f2202013-08-29 10:06:51 +0200316
317 igt_waitchildren();
Chris Wilson1ca607b2013-08-16 09:44:13 +0100318
319 igt_stop_signal_helper();
320
321 num_buffers = old_num_buffers;
Daniel Vetterec283d62013-08-14 15:18:37 +0200322}
Daniel Vetter5a598c92013-08-14 15:08:05 +0200323
324static void
325run_basic_modes(struct access_mode *mode,
326 drm_intel_bo **src, drm_intel_bo **dst,
Daniel Vetterec283d62013-08-14 15:18:37 +0200327 drm_intel_bo *dummy, const char *suffix,
328 run_wrap run_wrap_func)
Daniel Vetter5a598c92013-08-14 15:08:05 +0200329{
Daniel Vetter5a598c92013-08-14 15:08:05 +0200330 /* try to overwrite the source values */
Daniel Vetterec283d62013-08-14 15:18:37 +0200331 igt_subtest_f("%s-overwrite-source%s", mode->name, suffix)
332 run_wrap_func(mode, src, dst, dummy, do_overwrite_source);
Daniel Vetter5a598c92013-08-14 15:08:05 +0200333
334 /* try to read the results before the copy completes */
Daniel Vetterec283d62013-08-14 15:18:37 +0200335 igt_subtest_f("%s-early-read%s", mode->name, suffix)
336 run_wrap_func(mode, src, dst, dummy, do_early_read);
Daniel Vetter5a598c92013-08-14 15:08:05 +0200337
338 /* and finally try to trick the kernel into loosing the pending write */
Daniel Vetterec283d62013-08-14 15:18:37 +0200339 igt_subtest_f("%s-gpu-read-after-write%s", mode->name, suffix)
340 run_wrap_func(mode, src, dst, dummy, do_gpu_read_after_write);
Daniel Vetter5a598c92013-08-14 15:08:05 +0200341}
Daniel Vetter43779e32013-08-14 14:50:50 +0200342
343static void
344run_modes(struct access_mode *mode)
345{
Daniel Vetterad0f0812013-08-26 20:41:00 +0200346 drm_intel_bo *src[MAX_NUM_BUFFERS], *dst[MAX_NUM_BUFFERS], *dummy = NULL;
347
Daniel Vetter2dbd9982013-08-14 15:48:54 +0200348 igt_fixture {
Chris Wilson0d320fd2013-08-16 12:07:56 +0100349 bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
350 drm_intel_bufmgr_gem_enable_reuse(bufmgr);
351 batch = intel_batchbuffer_alloc(bufmgr, intel_get_drm_devid(fd));
352
Chris Wilson1ca607b2013-08-16 09:44:13 +0100353 for (int i = 0; i < num_buffers; i++) {
Daniel Vetter43779e32013-08-14 14:50:50 +0200354 src[i] = mode->create_bo(bufmgr, i, width, height);
355 dst[i] = mode->create_bo(bufmgr, ~i, width, height);
Daniel Vetter3dba47e2013-08-06 22:27:37 +0200356 }
Daniel Vetter43779e32013-08-14 14:50:50 +0200357 dummy = mode->create_bo(bufmgr, 0, width, height);
Daniel Vetter3dba47e2013-08-06 22:27:37 +0200358 }
359
Daniel Vetterec283d62013-08-14 15:18:37 +0200360 run_basic_modes(mode, src, dst, dummy, "", run_single);
Chris Wilson1ca607b2013-08-16 09:44:13 +0100361 run_basic_modes(mode, src, dst, dummy, "-interruptible", run_interruptible);
Daniel Vetter3dba47e2013-08-06 22:27:37 +0200362
Daniel Vetter2dbd9982013-08-14 15:48:54 +0200363 igt_fixture {
Chris Wilson1ca607b2013-08-16 09:44:13 +0100364 for (int i = 0; i < num_buffers; i++) {
Daniel Vetter43779e32013-08-14 14:50:50 +0200365 drm_intel_bo_unreference(src[i]);
366 drm_intel_bo_unreference(dst[i]);
367 }
368 drm_intel_bo_unreference(dummy);
Chris Wilson0d320fd2013-08-16 12:07:56 +0100369 intel_batchbuffer_free(batch);
370 drm_intel_bufmgr_destroy(bufmgr);
Daniel Vetter43779e32013-08-14 14:50:50 +0200371 }
Chris Wilson1ca607b2013-08-16 09:44:13 +0100372
373 run_basic_modes(mode, src, dst, dummy, "-forked", run_forked);
Daniel Vetter43779e32013-08-14 14:50:50 +0200374}
375
Daniel Vetter071e9ca2013-10-31 16:23:26 +0100376igt_main
Daniel Vetter43779e32013-08-14 14:50:50 +0200377{
378 int max, i;
379
Daniel Vetter43779e32013-08-14 14:50:50 +0200380 igt_skip_on_simulation();
381
Daniel Vetter2dbd9982013-08-14 15:48:54 +0200382 igt_fixture {
383 fd = drm_open_any();
Daniel Vetter43779e32013-08-14 14:50:50 +0200384
Daniel Vetter2dbd9982013-08-14 15:48:54 +0200385 max = gem_aperture_size (fd) / (1024 * 1024) / 2;
386 if (num_buffers > max)
387 num_buffers = max;
Daniel Vetteraee0dcb2013-12-03 16:32:52 +0100388
389 max = intel_get_total_ram_mb() * 3 / 4;
390 if (num_buffers > max)
391 num_buffers = max;
Chris Wilson0b4c33f2014-01-26 14:36:32 +0000392 num_buffers /= 2;
Daniel Vettere624fa82014-05-14 00:36:04 +0200393 igt_info("using 2x%d buffers, each 1MiB\n", num_buffers);
Daniel Vetter2dbd9982013-08-14 15:48:54 +0200394 }
Daniel Vetter43779e32013-08-14 14:50:50 +0200395
396 for (i = 0; i < ARRAY_SIZE(access_modes); i++)
397 run_modes(&access_modes[i]);
Daniel Vetter3dba47e2013-08-06 22:27:37 +0200398}