test_decode.c revision e88f27b3
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
24#define _GNU_SOURCE
25
26#include <string.h>
27#include <stdlib.h>
28#include <stdio.h>
29#include <unistd.h>
30#include <fcntl.h>
31#include <sys/types.h>
32#include <sys/stat.h>
33#include <sys/mman.h>
34#include <err.h>
35
36#include "config.h"
37#include "intel_bufmgr.h"
38#include "intel_chipset.h"
39
40#define HW_OFFSET 0x12300000
41
42static void
43usage(void)
44{
45	fprintf(stderr, "usage:\n");
46	fprintf(stderr, "  test_decode <batch>\n");
47	fprintf(stderr, "  test_decode <batch> -dump\n");
48	exit(1);
49}
50
51static void
52read_file(const char *filename, void **ptr, size_t *size)
53{
54	int fd, ret;
55	struct stat st;
56
57	fd = open(filename, O_RDONLY);
58	if (fd == -1)
59		errx(1, "couldn't open `%s'", filename);
60
61	ret = fstat(fd, &st);
62	if (ret)
63		errx(1, "couldn't stat `%s'", filename);
64
65	*size = st.st_size;
66	*ptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
67	if (*ptr == MAP_FAILED)
68		errx(1, "couldn't map `%s'", filename);
69
70	close(fd);
71}
72
73static void
74dump_batch(struct drm_intel_decode *ctx, const char *batch_filename)
75{
76	void *batch_ptr;
77	size_t batch_size;
78
79	read_file(batch_filename, &batch_ptr, &batch_size);
80
81	drm_intel_decode_set_batch_pointer(ctx, batch_ptr, HW_OFFSET,
82					   batch_size / 4);
83	drm_intel_decode_set_output_file(ctx, stdout);
84
85	drm_intel_decode(ctx);
86}
87
88static void
89compare_batch(struct drm_intel_decode *ctx, const char *batch_filename)
90{
91	FILE *out = NULL;
92	void *ptr, *ref_ptr, *batch_ptr;
93	size_t size, ref_size, batch_size;
94	const char *ref_suffix = "-ref.txt";
95	char *ref_filename;
96
97	ref_filename = malloc(strlen(batch_filename) + strlen(ref_suffix) + 1);
98	sprintf(ref_filename, "%s%s", batch_filename, ref_suffix);
99
100	/* Read the batch and reference. */
101	read_file(batch_filename, &batch_ptr, &batch_size);
102	read_file(ref_filename, &ref_ptr, &ref_size);
103
104	/* Set up our decode output in memory, because I don't want to
105	 * figure out how to output to a file in a safe and sane way
106	 * inside of an automake project's test infrastructure.
107	 */
108#ifdef HAVE_OPEN_MEMSTREAM
109	out = open_memstream((char **)&ptr, &size);
110#else
111	fprintf(stderr, "platform lacks open_memstream, skipping.\n");
112	exit(77);
113#endif
114
115	drm_intel_decode_set_batch_pointer(ctx, batch_ptr, HW_OFFSET,
116					   batch_size / 4);
117	drm_intel_decode_set_output_file(ctx, out);
118
119	drm_intel_decode(ctx);
120
121	if (strcmp(ref_ptr, ptr) != 0) {
122		fprintf(stderr, "Decode mismatch with reference `%s'.\n",
123			ref_filename);
124		fprintf(stderr, "You can dump the new output using:\n");
125		fprintf(stderr, "  test_decode \"%s\" -dump\n", batch_filename);
126		exit(1);
127	}
128
129	fclose(out);
130	free(ref_filename);
131	free(ptr);
132}
133
134static uint16_t
135infer_devid(const char *batch_filename)
136{
137	struct {
138		const char *name;
139		uint16_t devid;
140	} chipsets[] = {
141		{ "830",  0x3577},
142		{ "855",  0x3582},
143		{ "945",  0x2772},
144		{ "gen4", 0x2a02 },
145		{ "gm45", 0x2a42 },
146		{ "gen5", PCI_CHIP_ILD_G },
147		{ "gen6", PCI_CHIP_SANDYBRIDGE_GT2 },
148		{ "gen7", PCI_CHIP_IVYBRIDGE_GT2 },
149		{ "gen8", 0x1616 },
150		{ NULL, 0 },
151	};
152	int i;
153
154	for (i = 0; chipsets[i].name != NULL; i++) {
155		if (strstr(batch_filename, chipsets[i].name))
156			return chipsets[i].devid;
157	}
158
159	fprintf(stderr, "Couldn't guess chipset id from batch filename `%s'.\n",
160		batch_filename);
161	fprintf(stderr, "Must be contain one of:\n");
162	for (i = 0; chipsets[i].name != NULL; i++) {
163		fprintf(stderr, "  %s\n", chipsets[i].name);
164	}
165	exit(1);
166}
167
168int
169main(int argc, char **argv)
170{
171	uint16_t devid;
172	struct drm_intel_decode *ctx;
173
174	if (argc < 2)
175		usage();
176
177
178	devid = infer_devid(argv[1]);
179
180	ctx = drm_intel_decode_context_alloc(devid);
181
182	if (argc == 3) {
183		if (strcmp(argv[2], "-dump") == 0)
184			dump_batch(ctx, argv[1]);
185		else
186			usage();
187	} else {
188		compare_batch(ctx, argv[1]);
189	}
190
191	drm_intel_decode_context_free(ctx);
192
193	return 0;
194}
195