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