summaryrefslogtreecommitdiff
path: root/tests/i915/gem_ring_sync_copy.c
blob: 1e5728bce740ca8b3da837ecb503d7e5aa45a5c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
 * Copyright © 2013 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Authors:
 *    Damien Lespiau <damien.lespiau@intel.com>
 */

/*
 * The goal of this test is to ensure that we respect inter ring dependencies
 *
 * For each pair of rings R1, R2 where we have copy support (i.e. blt,
 * rendercpy and mediafill) do:
 *  - Throw a busy load onto R1. gem_concurrent_blt just uses lots of buffers
 *    for this effect.
 *  - Fill three buffers A, B, C with unique data.
 *  - Copy A to B on ring R1
 *
 * Then come the three different variants.
 *  - Copy B to C on ring R2, check that C now contains what A originally
 *    contained. This is the write->read hazard. gem_concurrent_blt calls this
 *    early read.
 *  - Copy C to A on ring R2, check that B now contains what A originally
 *    contained. This is the read->write hazard, gem_concurrent_blt calls it
 *    overwrite_source.
 *  - Copy C to B on ring R2 and check that B contains what C originally
 *    contained. This is the write/write hazard. gem_concurrent_blt doesn't
 *    have that since for the cpu case it's too boring.
 *
 */

#include "igt.h"
#include <stdlib.h>
#include <stdbool.h>


IGT_TEST_DESCRIPTION("Ensure inter-ring dependencies are respected.");

#define WIDTH	512
#define HEIGHT	512
#define NUM_BUSY_BUFFERS 32

typedef struct {
	int drm_fd;
	uint32_t devid;
	drm_intel_bufmgr *bufmgr;
	struct intel_batchbuffer *batch;

	/* number of buffers to keep the ring busy for a while */
	unsigned int n_buffers_load;

	uint32_t linear[WIDTH * HEIGHT];

	struct {
		igt_render_copyfunc_t copy;
		struct igt_buf *srcs;
		struct igt_buf *dsts;
	} render;

	struct {
		drm_intel_bo **srcs;
		drm_intel_bo **dsts;
	} blitter;

} data_t;

enum ring {
	RENDER,
	BLITTER,
};

enum test {
	TEST_WRITE_READ,
	TEST_READ_WRITE,
	TEST_WRITE_WRITE,
};

static const char *ring_name(enum ring ring)
{
	const char *names[] = {
		"render",
		"blitter",
	};

	return names[ring];
}

static drm_intel_bo *bo_create(data_t *data, int width, int height, int val)
{
	drm_intel_bo *bo;
	int i;

	bo = drm_intel_bo_alloc(data->bufmgr, "", 4 * width * height, 4096);
	igt_assert(bo);

	for (i = 0; i < width * height; i++)
		data->linear[i] = val;
	gem_write(data->drm_fd, bo->handle, 0, data->linear,
		  sizeof(data->linear));

	return bo;
}

static void bo_check(data_t *data, drm_intel_bo *bo, uint32_t val)
{
	int i;

	gem_read(data->drm_fd, bo->handle, 0,
		 data->linear, sizeof(data->linear));
	for (i = 0; i < WIDTH * HEIGHT; i++)
		igt_assert_eq_u32(data->linear[i], val);
}

static void scratch_buf_init_from_bo(struct igt_buf *buf, drm_intel_bo *bo)
{
	memset(buf, 0, sizeof(*buf));

	buf->bo = bo;
	buf->stride = 4 * WIDTH;
	buf->tiling = I915_TILING_NONE;
	buf->size = 4 * WIDTH * HEIGHT;
	buf->bpp = 32;
}

static void scratch_buf_init(data_t *data, struct igt_buf *buf,
			     int width, int height, uint32_t color)
{
	drm_intel_bo *bo;

	bo = bo_create(data, width, height, color);
	scratch_buf_init_from_bo(buf, bo);
}

/*
 * Provide a few ring specific vfuncs for run_test().
 *
 * busy()	Queue a n_buffers_load workloads onto the ring to keep it busy
 * busy_fini()	Clean up after busy
 * copy()	Copy one BO to another
 */

/*
 * Render ring
 */

static void render_busy(data_t *data)
{
	size_t array_size;
	int i;

	/* allocate 32 buffer objects and re-use them as needed */
	array_size = NUM_BUSY_BUFFERS * sizeof(struct igt_buf);

	data->render.srcs = malloc(array_size);
	data->render.dsts = malloc(array_size);

	for (i = 0; i < NUM_BUSY_BUFFERS; i++) {
		scratch_buf_init(data, &data->render.srcs[i], WIDTH, HEIGHT,
				 0xdeadbeef);
		scratch_buf_init(data, &data->render.dsts[i], WIDTH, HEIGHT,
				 0xdeadbeef);
	}

	for (i = 0; i < data->n_buffers_load; i++) {
		data->render.copy(data->batch,
				  NULL,			/* context */
				  &data->render.srcs[i % NUM_BUSY_BUFFERS],
				  0, 0,			/* src_x, src_y */
				  WIDTH, HEIGHT,
				  &data->render.dsts[i % NUM_BUSY_BUFFERS],
				  0, 0			/* dst_x, dst_y */);
	}
}

static void render_busy_fini(data_t *data)
{
	int i;

	for (i = 0; i < NUM_BUSY_BUFFERS; i++) {
		drm_intel_bo_unreference(data->render.srcs[i].bo);
		drm_intel_bo_unreference(data->render.dsts[i].bo);
	}

	free(data->render.srcs);
	free(data->render.dsts);
	data->render.srcs = NULL;
	data->render.dsts = NULL;
}

static void render_copy(data_t *data, drm_intel_bo *src, drm_intel_bo *dst)
{
	struct igt_buf src_buf, dst_buf;

	scratch_buf_init_from_bo(&src_buf, src);
	scratch_buf_init_from_bo(&dst_buf, dst);

	data->render.copy(data->batch,
			  NULL,			/* context */
			  &src_buf,
			  0, 0,			/* src_x, src_y */
			  WIDTH, HEIGHT,
			  &dst_buf,
			  0, 0			/* dst_x, dst_y */);
}

/*
 * Blitter ring
 */

static void blitter_busy(data_t *data)
{
	size_t array_size;
	int i;

	/* allocate 32 buffer objects and re-use them as needed */
	array_size = NUM_BUSY_BUFFERS * sizeof(drm_intel_bo *);

	data->blitter.srcs = malloc(array_size);
	data->blitter.dsts = malloc(array_size);

	for (i = 0; i < NUM_BUSY_BUFFERS; i++) {
		data->blitter.srcs[i] = bo_create(data,
						  WIDTH, HEIGHT,
						  0xdeadbeef);
		data->blitter.dsts[i] = bo_create(data,
						  WIDTH, HEIGHT,
						  0xdeadbeef);
	}

	for (i = 0; i < data->n_buffers_load; i++) {
		intel_copy_bo(data->batch,
			      data->blitter.srcs[i % NUM_BUSY_BUFFERS],
			      data->blitter.dsts[i % NUM_BUSY_BUFFERS],
			      WIDTH*HEIGHT*4);
	}
}

static void blitter_busy_fini(data_t *data)
{
	int i;

	for (i = 0; i < NUM_BUSY_BUFFERS; i++) {
		drm_intel_bo_unreference(data->blitter.srcs[i]);
		drm_intel_bo_unreference(data->blitter.dsts[i]);
	}

	free(data->blitter.srcs);
	free(data->blitter.dsts);
	data->blitter.srcs = NULL;
	data->blitter.dsts = NULL;
}

static void blitter_copy(data_t *data, drm_intel_bo *src, drm_intel_bo *dst)
{
	intel_copy_bo(data->batch, dst, src, WIDTH*HEIGHT*4);
}

struct ring_ops {
	void (*busy)(data_t *data);
	void (*busy_fini)(data_t *data);
	void (*copy)(data_t *data, drm_intel_bo *src, drm_intel_bo *dst);
} ops [] = {
	{
		.busy      = render_busy,
		.busy_fini = render_busy_fini,
		.copy      = render_copy,
	},
	{
		.busy      = blitter_busy,
		.busy_fini = blitter_busy_fini,
		.copy      = blitter_copy,
	},
};

static void run_test(data_t *data, enum ring r1, enum ring r2, enum test test)
{
	struct ring_ops *r1_ops = &ops[r1];
	struct ring_ops *r2_ops = &ops[r2];
	drm_intel_bo *a, *b, *c;

	a = bo_create(data, WIDTH, HEIGHT, 0xa);
	b = bo_create(data, WIDTH, HEIGHT, 0xb);
	c = bo_create(data, WIDTH, HEIGHT, 0xc);

	r1_ops->busy(data);
	r1_ops->copy(data, a, b);

	switch (test) {
	case TEST_WRITE_READ:
		r2_ops->copy(data, b, c);
		bo_check(data, c, 0xa);
		break;
	case TEST_READ_WRITE:
		r2_ops->copy(data, c, a);
		bo_check(data, b, 0xa);
		break;
	case TEST_WRITE_WRITE:
		r2_ops->copy(data, c, b);
		bo_check(data, b, 0xc);
		break;
	default:
		igt_fail(IGT_EXIT_FAILURE);
	}

	r1_ops->busy_fini(data);
}

igt_main
{
	data_t data = {0, };
	int i;
	struct combination {
		int r1, r2;
	} ring_combinations [] = {
		{ RENDER, BLITTER },
		{ BLITTER, RENDER },
	};

	igt_fixture {
		data.drm_fd = drm_open_driver_render(DRIVER_INTEL);
		data.devid = intel_get_drm_devid(data.drm_fd);

		data.n_buffers_load = 1000;

		data.bufmgr = drm_intel_bufmgr_gem_init(data.drm_fd, 4096);
		igt_assert(data.bufmgr);
		drm_intel_bufmgr_gem_enable_reuse(data.bufmgr);

		data.render.copy = igt_get_render_copyfunc(data.devid);
		igt_require_f(data.render.copy,
			      "no render-copy function\n");

		data.batch = intel_batchbuffer_alloc(data.bufmgr, data.devid);
		igt_assert(data.batch);
	}

	for (i = 0; i < ARRAY_SIZE(ring_combinations); i++) {
		struct combination *c = &ring_combinations[i];

		igt_subtest_f("sync-%s-%s-write-read",
			      ring_name(c->r1), ring_name(c->r2))
			run_test(&data, c->r1, c->r2, TEST_WRITE_READ);

		igt_subtest_f("sync-%s-%s-read-write",
			      ring_name(c->r1), ring_name(c->r2))
			run_test(&data, c->r1, c->r2, TEST_READ_WRITE);
		igt_subtest_f("sync-%s-%s-write-write",
			      ring_name(c->r1), ring_name(c->r2))
			run_test(&data, c->r1, c->r2, TEST_WRITE_WRITE);
	}

	igt_fixture {
		intel_batchbuffer_free(data.batch);
		drm_intel_bufmgr_destroy(data.bufmgr);
		close(data.drm_fd);
	}
}