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