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