summaryrefslogtreecommitdiff
path: root/src/radeondb.c
blob: 6e5ec4defb29cdaea14b0f6cc28b60947fb019fb (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
376
377
/*
 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
 *
 * 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
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, 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 NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS 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:
 *      Jerome Glisse
 */
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/timeb.h>
#include <getopt.h>
#include <jansson.h>
#include "radeon_priv.h"

void usage(void)
{
	printf("usage: radeondb [arguments] [file ..]\n\n");
	printf("arguments:\n");
	printf(" -j      replay json command stream\n");
	printf(" -p      replay command stream\n");
	printf(" -d      dump command stream in raw hex\n");
	printf(" -r      register dump (block to dump DISP, MC, ...)\n");
	printf(" -c      convert bof file to json\n");
	printf(" -b      benchmark (valid with -j or -p)\n");
	printf(" -s      disassemble shader\n");
}

static int conv(const char *file);
static int json(const char *file, int bench);
static int play(const char *file, int bench);
static int dump(const char *file);
void radeon_register_dump(const char *bname);
static int shader(const char *file);

struct param {
	char	file_json[256];
	char	file_play[256];
	char	file_conv[256];
	char	file_dump[256];
	char	file_shader[256];
	char	block[256];
	int	bench;
	int	json;
	int	shader;
	int	play;
	int	conv;
	int	dump;
	int	reg;
};

int main(int argc, char *argv[])
{
	static struct option options[] = {
		{"json",    required_argument, 0, 'j'},
		{"play",    required_argument, 0, 'p'},
		{"dump",    required_argument, 0, 'd'},
		{"conv",    required_argument, 0, 'c'},
		{"reg",     required_argument, 0, 'r'},
		{"shader",  required_argument, 0, 's'},
		{0, 0, 0, 0}
	};
	int option_id;
	struct param p;
	int c;

	memset(&p, 0, sizeof(struct param));
	while (1) {
		c = getopt_long(argc, argv, "p:d:j:c:r:bs:", options, &option_id);
		if (c == -1)
			break;
		switch (c) {
		case 'd':
			strcpy(p.file_dump, optarg);
			p.dump = 1;
			break;
		case 'b':
			p.bench = 1;
			break;
		case 's':
			strcpy(p.file_shader, optarg);
			p.shader = 1;
			break;
		case 'j':
			strcpy(p.file_json, optarg);
			p.json = 1;
			break;
		case 'c':
			strcpy(p.file_conv, optarg);
			p.conv = 1;
			break;
		case 'p':
			strcpy(p.file_play, optarg);
			p.play = 1;
			break;
		case 'r':
			strcpy(p.block, optarg);
			p.reg = 1;
			break;
		case '?':
			/* getopt_long already printed an error message. */
			break;
		default:
			abort ();
		}
	}
	if (p.reg) {
		radeon_register_dump(p.block);
	}
	if (p.play) {
		return play(p.file_play, p.bench);
	}
	if (p.dump) {
		return dump(p.file_dump);
	}
	if (p.json) {
		return json(p.file_json, p.bench);
	}
	if (p.conv) {
		return conv(p.file_conv);
	}
	if (p.shader) {
		return shader(p.file_shader);
	}
}

static int play(const char *file, int bench)
{
	struct radeon *radeon;
	struct radeon_ctx *ctx = NULL;
	struct radeon_surface dst;
	struct radeon_surface src;
	struct timeb fstart, fend;
	double time_elapsed;
	int r, fd, i;

	if (bench) {
		bench = 10000;
	} else {
		bench = 1;
	}
	fd = radeon_open_fd();
	if (fd < 0) {
		fprintf(stderr, "failed to open radeon drm device\n");
		return -1;
	}
	radeon = radeon_new(fd, 0);
	if (radeon == NULL) {
		fprintf(stderr, "failed to initialize radeon\n");
		return -1;
	}
	/* setup framebuffer surface */
	dst.bo = radeon->mode.bo;
	dst.width = radeon->mode.pitch / 4;
	dst.pitch = radeon->mode.pitch / 4;
	dst.bpe = 4;
	dst.height = radeon->mode.height;
	/* load bof */
	ctx = radeon_ctx_load_bof(radeon, file);
	if (ctx == NULL) {
		fprintf(stderr, "failed to read bof file %s\n", file);
		r = -EINVAL;
		goto out;
	}
	if (!radeon_is_family_compatible(radeon->family, ctx->radeon->family)) {
		fprintf(stderr, "Incompatible hardware\n");
		r = -EINVAL;
		goto out;
	}
	r = radeon_ctx_draw(ctx);
	if (r) {
		fprintf(stderr, "failed to reconstruct draw sequences from %s\n", file);
		goto out;
	}
	if (radeon_ctx_get_dst_surface(ctx, &src)) {
		ctx->radeon = NULL;
		r = -EINVAL;
		goto out;
	}
	radeon_blit(radeon, &dst, &src, 0, 0, 0, 0, src.width, src.height);
	printf("%d execution of %d dwords\n", bench, ctx->cpm4);
	ftime(&fstart);
	for (i = 0; i < bench; i++) {
		r = radeon_ctx_submit(ctx);
		if (r)
			goto out;
	}
	radeon_bo_wait(radeon, src.bo);
	ftime(&fend);
	time_elapsed = fend.time - fstart.time;
	time_elapsed += ((double)(fend.millitm - fstart.millitm)) / 1000;
	printf("%d runs in %f seconds\n", bench, time_elapsed);
	getchar();
	radeon_blit(radeon, &dst, &src, 0, 0, 0, 0, src.width, src.height);
	getchar();
out:
	ctx = radeon_ctx_decref(ctx);
	radeon_decref(radeon);
	return r;
}

static int dump(const char *file)
{
	struct radeon *radeon;
	struct radeon_ctx *ctx;
	unsigned i;
	int fd;

	radeon = radeon_new(-1, 0);
	ctx = radeon_ctx_load_bof(radeon, file);
	if (ctx == NULL) {
		fprintf(stderr, "failed to read bof file %s\n", file);
		return -1;
	}
	for (i = 0; i < ctx->cpm4; i++) {
		printf("0x%08X\n", ctx->pm4[i]);
	}
	return 0;
}

static int json(const char *file, int bench)
{
	struct radeon *radeon;
	struct radeon_ctx *ctx = NULL;
	struct radeon_surface dst;
	struct radeon_surface src;
	struct timeb fstart, fend;
	double time_elapsed;
	int r, fd, i, j;

	if (bench) {
		bench = 10000;
	} else {
		bench = 1;
	}
	fd = radeon_open_fd();
	if (fd < 0) {
		fprintf(stderr, "failed to open radeon drm device\n");
		return -1;
	}
	radeon = radeon_new(fd, 0);
	if (radeon == NULL) {
		fprintf(stderr, "failed to initialize radeon\n");
		return -1;
	}
	/* setup framebuffer surface */
	dst.bo = radeon->mode.bo;
	dst.width = radeon->mode.pitch / 4;
	dst.pitch = radeon->mode.pitch / 4;
	dst.bpe = 4;
	dst.height = radeon->mode.height;
	/* load bof */
	ctx = radeon_ctx_load_json(radeon, file);
	if (ctx == NULL) {
		fprintf(stderr, "failed to read bof file %s\n", file);
		r = -EINVAL;
		goto out;
	}
	if (!radeon_is_family_compatible(radeon->family, ctx->radeon->family)) {
		fprintf(stderr, "Incompatible hardware\n");
		r = -EINVAL;
		goto out;
	}
	r = radeon_ctx_pm4(ctx);
	if (r) {
		fprintf(stderr, "failed to reconstruct pm4 sequences from %s\n", file);
		goto out;
	}
	if (radeon_ctx_get_dst_surface(ctx, &src)) {
		ctx->radeon = NULL;
		r = -EINVAL;
		goto out;
	}
	radeon_blit(radeon, &dst, &src, 0, 0, 0, 0, src.width, src.height);
	printf("%d execution of %d dwords\n", bench, ctx->cpm4);
	ftime(&fstart);
	for (i = 0; i < bench; i++) {
		if (radeon_ctx_submit(ctx)) {
			fprintf(stderr, "failed to flush\n");
			goto out;
		}
	}
	radeon_bo_wait(radeon, src.bo);
	ftime(&fend);
	time_elapsed = fend.time - fstart.time;
	time_elapsed += ((double)(fend.millitm - fstart.millitm)) / 1000;
	printf("%d runs in %f seconds\n", bench, time_elapsed);
	getchar();
	radeon_blit(radeon, &dst, &src, 0, 0, 0, 0, src.width, src.height);
	getchar();
out:
	ctx = radeon_ctx_decref(ctx);
	radeon_decref(radeon);
	return r;
}

static int conv(const char *file)
{
	struct radeon_ctx *ctx = NULL;
	struct radeon *radeon;
	char *tmp;
	char name[256];
	int r;

	radeon = radeon_new(-1, 0);
	if (radeon == NULL) {
		fprintf(stderr, "failed to initialize radeon\n");
		return -1;
	}
	/* load bof */
	ctx = radeon_ctx_load_bof(radeon, file);
	if (ctx == NULL) {
		fprintf(stderr, "failed to read bof file %s\n", file);
		return -EINVAL;
	}
	r = radeon_ctx_draw(ctx);
	if (r) {
		fprintf(stderr, "failed to reconstruct draw sequences from %s\n", file);
		return -EINVAL;
	}
	tmp = strstr(file, ".bof");
	if (tmp == NULL) {
		return -EINVAL;
	}
	*tmp = 0;
	sprintf(name, "%s.json", file);
	r = radeon_ctx_dump_json(ctx, name);
	if (r) {
		fprintf(stderr, "writting %s failed\n", name);
	}
	ctx = radeon_ctx_decref(ctx);
	return r;
}

static int shader(const char *file)
{
	struct radeon *radeon;
	struct radeon_ctx *ctx = NULL;
	int r = 0, i;

	radeon = radeon_new(-1, 0);
	if (radeon == NULL) {
		fprintf(stderr, "failed to initialize radeon\n");
		return -1;
	}
	ctx = radeon_ctx_load_json(radeon, file);
	if (ctx == NULL) {
		fprintf(stderr, "failed to read bof file %s\n", file);
		r = -EINVAL;
		goto out;
	}
	radeon_ctx_disassemble(ctx);
out:
	ctx = radeon_ctx_decref(ctx);
	radeon_decref(radeon);
	return r;
}