intel: Add a regression test program for intel_decode.c.
[profile/ivi/libdrm.git] / intel / test_decode.c
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 <sys/mman.h>
32 #include <err.h>
33
34 #include "config.h"
35 #include "intel_bufmgr.h"
36 #include "intel_chipset.h"
37
38 #define HW_OFFSET 0x12300000
39
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 }
48
49 static void
50 read_file(const char *filename, void **ptr, size_t *size)
51 {
52         int fd, ret;
53         struct stat st;
54
55         fd = open(filename, O_RDONLY);
56         if (fd == -1)
57                 errx(1, "couldn't open `%s'", filename);
58
59         ret = fstat(fd, &st);
60         if (ret)
61                 errx(1, "couldn't stat `%s'", filename);
62
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);
67
68         close(fd);
69 }
70
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;
76
77         read_file(batch_filename, &batch_ptr, &batch_size);
78
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);
82
83         drm_intel_decode(ctx);
84 }
85
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;
94
95         ref_filename = malloc(strlen(batch_filename) + strlen(ref_suffix) + 1);
96         sprintf(ref_filename, "%s%s", batch_filename, ref_suffix);
97
98         /* Read the batch and reference. */
99         read_file(batch_filename, &batch_ptr, &batch_size);
100         read_file(ref_filename, &ref_ptr, &ref_size);
101
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
112
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);
116
117         drm_intel_decode(ctx);
118
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         }
126
127         fclose(out);
128         free(ref_filename);
129         free(ptr);
130 }
131
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         };
148         int i;
149
150         for (i = 0; chipsets[i].name != NULL; i++) {
151                 if (strstr(batch_filename, chipsets[i].name))
152                         return chipsets[i].devid;
153         }
154
155         fprintf(stderr, "Couldn't guess chipset id from batch filename `%s'.\n",
156                 batch_filename);
157         fprintf(stderr, "Must be contain one of:\n");
158         for (i = 0; chipsets[i].name != NULL; i++) {
159                 fprintf(stderr, "  %s\n", chipsets[i].name);
160         }
161         exit(1);
162 }
163
164 int
165 main(int argc, char **argv)
166 {
167         uint16_t devid;
168         struct drm_intel_decode *ctx;
169
170         if (argc < 2)
171                 usage();
172
173
174         devid = infer_devid(argv[1]);
175
176         ctx = drm_intel_decode_context_alloc(devid);
177
178         if (argc == 3) {
179                 if (strcmp(argv[2], "-dump") == 0)
180                         dump_batch(ctx, argv[1]);
181                 else
182                         usage();
183         } else {
184                 compare_batch(ctx, argv[1]);
185         }
186
187         drm_intel_decode_context_free(ctx);
188
189         return 0;
190 }