• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2016 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 <stdio.h>
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <getopt.h>
28 
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include <signal.h>
33 #include <errno.h>
34 #include <inttypes.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/wait.h>
38 #include <sys/mman.h>
39 
40 #include "util/macros.h"
41 
42 #include "decoder.h"
43 #include "intel_aub.h"
44 #include "gen_disasm.h"
45 
46 /* Below is the only command missing from intel_aub.h in libdrm
47  * So, reuse intel_aub.h from libdrm and #define the
48  * AUB_MI_BATCH_BUFFER_END as below
49  */
50 #define AUB_MI_BATCH_BUFFER_END (0x0500 << 16)
51 
52 #define CSI "\e["
53 #define BLUE_HEADER  CSI "0;44m"
54 #define GREEN_HEADER CSI "1;42m"
55 #define NORMAL       CSI "0m"
56 
57 /* options */
58 
59 static bool option_full_decode = true;
60 static bool option_print_offsets = true;
61 static enum { COLOR_AUTO, COLOR_ALWAYS, COLOR_NEVER } option_color;
62 
63 /* state */
64 
65 uint16_t pci_id = 0;
66 char *input_file = NULL, *xml_path = NULL;
67 struct gen_spec *spec;
68 struct gen_disasm *disasm;
69 
70 uint64_t gtt_size, gtt_end;
71 void *gtt;
72 uint64_t general_state_base;
73 uint64_t surface_state_base;
74 uint64_t dynamic_state_base;
75 uint64_t instruction_base;
76 uint64_t instruction_bound;
77 
78 static inline uint32_t
field(uint32_t value,int start,int end)79 field(uint32_t value, int start, int end)
80 {
81    uint32_t mask;
82 
83    mask = ~0U >> (31 - end + start);
84 
85    return (value >> start) & mask;
86 }
87 
88 struct brw_instruction;
89 
90 static inline int
valid_offset(uint32_t offset)91 valid_offset(uint32_t offset)
92 {
93    return offset < gtt_end;
94 }
95 
96 static void
print_dword_val(struct gen_field_iterator * iter,uint64_t offset,int * dword_num)97 print_dword_val(struct gen_field_iterator *iter, uint64_t offset,
98                 int *dword_num)
99 {
100    struct gen_field *f;
101 
102    f = iter->group->fields[iter->i - 1];
103    const int dword = f->start / 32;
104 
105    if (*dword_num != dword) {
106       printf("0x%08"PRIx64":  0x%08x : Dword %d\n",
107              offset + 4 * dword,  iter->p[dword], dword);
108       *dword_num = dword;
109    }
110 }
111 
112 static char *
print_iterator_values(struct gen_field_iterator * iter,int * idx)113 print_iterator_values(struct gen_field_iterator *iter, int *idx)
114 {
115     char *token = NULL;
116     if (strstr(iter->value, "struct") == NULL) {
117        if (strlen(iter->description) > 0) {
118           printf("    %s: %s (%s)\n",
119                  iter->name, iter->value, iter->description);
120        } else {
121           printf("    %s: %s\n", iter->name, iter->value);
122        }
123     } else {
124         token = strtok(iter->value, " ");
125         if (token != NULL) {
126             token = strtok(NULL, " ");
127             *idx = atoi(strtok(NULL, ">"));
128         } else {
129             token = NULL;
130         }
131         printf("    %s:<struct %s>\n", iter->name, token);
132     }
133     return token;
134 }
135 
136 static void
decode_structure(struct gen_spec * spec,struct gen_group * strct,const uint32_t * p)137 decode_structure(struct gen_spec *spec, struct gen_group *strct,
138                  const uint32_t *p)
139 {
140    struct gen_field_iterator iter;
141    char *token = NULL;
142    int idx = 0, dword_num = 0;
143    uint64_t offset = 0;
144 
145    if (option_print_offsets)
146       offset = (void *) p - gtt;
147    else
148       offset = 0;
149 
150    gen_field_iterator_init(&iter, strct, p,
151                            option_color == COLOR_ALWAYS);
152    while (gen_field_iterator_next(&iter)) {
153       idx = 0;
154       print_dword_val(&iter, offset, &dword_num);
155       token = print_iterator_values(&iter, &idx);
156       if (token != NULL) {
157          struct gen_group *struct_val = gen_spec_find_struct(spec, token);
158          decode_structure(spec, struct_val, &p[idx]);
159          token = NULL;
160       }
161    }
162 }
163 
164 static void
handle_struct_decode(struct gen_spec * spec,char * struct_name,uint32_t * p)165 handle_struct_decode(struct gen_spec *spec, char *struct_name, uint32_t *p)
166 {
167    if (struct_name == NULL)
168       return;
169    struct gen_group *struct_val = gen_spec_find_struct(spec, struct_name);
170    decode_structure(spec, struct_val, p);
171 }
172 
173 static void
dump_binding_table(struct gen_spec * spec,uint32_t offset)174 dump_binding_table(struct gen_spec *spec, uint32_t offset)
175 {
176    uint32_t *pointers, i;
177    uint64_t start;
178    struct gen_group *surface_state;
179 
180    surface_state = gen_spec_find_struct(spec, "RENDER_SURFACE_STATE");
181    if (surface_state == NULL) {
182       printf("did not find RENDER_SURFACE_STATE info\n");
183       return;
184    }
185 
186    start = surface_state_base + offset;
187    pointers = gtt + start;
188    for (i = 0; i < 16; i++) {
189       if (pointers[i] == 0)
190          continue;
191       start = pointers[i] + surface_state_base;
192       if (!valid_offset(start)) {
193          printf("pointer %u: %08x <not valid>\n",
194                 i, pointers[i]);
195          continue;
196       } else {
197          printf("pointer %u: %08x\n", i, pointers[i]);
198       }
199 
200       decode_structure(spec, surface_state, gtt + start);
201    }
202 }
203 
204 static void
handle_3dstate_index_buffer(struct gen_spec * spec,uint32_t * p)205 handle_3dstate_index_buffer(struct gen_spec *spec, uint32_t *p)
206 {
207    void *start;
208    uint32_t length, i, type, size;
209 
210    start = gtt + p[2];
211    type = (p[1] >> 8) & 3;
212    size = 1 << type;
213    length = p[4] / size;
214    if (length > 10)
215       length = 10;
216 
217    printf("\t");
218 
219    for (i = 0; i < length; i++) {
220       switch (type) {
221       case 0:
222          printf("%3d ", ((uint8_t *)start)[i]);
223          break;
224       case 1:
225          printf("%3d ", ((uint16_t *)start)[i]);
226          break;
227       case 2:
228          printf("%3d ", ((uint32_t *)start)[i]);
229          break;
230       }
231    }
232    if (length < p[4] / size)
233       printf("...\n");
234    else
235       printf("\n");
236 }
237 
238 static inline uint64_t
get_address(struct gen_spec * spec,uint32_t * p)239 get_address(struct gen_spec *spec, uint32_t *p)
240 {
241    /* Addresses are always guaranteed to be page-aligned and sometimes
242     * hardware packets have extra stuff stuffed in the bottom 12 bits.
243     */
244    uint64_t addr = p[0] & ~0xfffu;
245 
246    if (gen_spec_get_gen(spec) >= gen_make_gen(8,0)) {
247       /* On Broadwell and above, we have 48-bit addresses which consume two
248        * dwords.  Some packets require that these get stored in a "canonical
249        * form" which means that bit 47 is sign-extended through the upper
250        * bits. In order to correctly handle those aub dumps, we need to mask
251        * off the top 16 bits.
252        */
253       addr |= ((uint64_t)p[1] & 0xffff) << 32;
254    }
255 
256    return addr;
257 }
258 
259 static inline uint64_t
get_offset(uint32_t * p,uint32_t start,uint32_t end)260 get_offset(uint32_t *p, uint32_t start, uint32_t end)
261 {
262    assert(start <= end);
263    assert(end < 64);
264 
265    uint64_t mask = (~0ull >> (64 - (end - start + 1))) << start;
266 
267    uint64_t offset = p[0];
268    if (end >= 32)
269       offset |= (uint64_t) p[1] << 32;
270 
271    return offset & mask;
272 }
273 
274 static void
handle_state_base_address(struct gen_spec * spec,uint32_t * p)275 handle_state_base_address(struct gen_spec *spec, uint32_t *p)
276 {
277    if (gen_spec_get_gen(spec) >= gen_make_gen(8,0)) {
278       if (p[1] & 1)
279          general_state_base = get_address(spec, &p[1]);
280       if (p[4] & 1)
281          surface_state_base = get_address(spec, &p[4]);
282       if (p[6] & 1)
283          dynamic_state_base = get_address(spec, &p[6]);
284       if (p[10] & 1)
285          instruction_base = get_address(spec, &p[10]);
286       if (p[15] & 1)
287          instruction_bound = p[15] & 0xfff;
288    } else {
289       if (p[2] & 1)
290          surface_state_base = get_address(spec, &p[2]);
291       if (p[3] & 1)
292          dynamic_state_base = get_address(spec, &p[3]);
293       if (p[5] & 1)
294          instruction_base = get_address(spec, &p[5]);
295       if (p[9] & 1)
296          instruction_bound = get_address(spec, &p[9]);
297    }
298 }
299 
300 static void
dump_samplers(struct gen_spec * spec,uint32_t offset)301 dump_samplers(struct gen_spec *spec, uint32_t offset)
302 {
303    uint32_t i;
304    uint64_t start;
305    struct gen_group *sampler_state;
306 
307    sampler_state = gen_spec_find_struct(spec, "SAMPLER_STATE");
308 
309    start = dynamic_state_base + offset;
310    for (i = 0; i < 4; i++) {
311       printf("sampler state %d\n", i);
312       decode_structure(spec, sampler_state, gtt + start + i * 16);
313    }
314 }
315 
316 static void
handle_media_interface_descriptor_load(struct gen_spec * spec,uint32_t * p)317 handle_media_interface_descriptor_load(struct gen_spec *spec, uint32_t *p)
318 {
319    int i, length = p[2] / 32;
320    struct gen_group *descriptor_structure;
321    uint32_t *descriptors;
322    uint64_t start;
323    struct brw_instruction *insns;
324 
325    descriptor_structure =
326       gen_spec_find_struct(spec, "INTERFACE_DESCRIPTOR_DATA");
327    if (descriptor_structure == NULL) {
328       printf("did not find INTERFACE_DESCRIPTOR_DATA info\n");
329       return;
330    }
331 
332    start = dynamic_state_base + p[3];
333    descriptors = gtt + start;
334    for (i = 0; i < length; i++, descriptors += 8) {
335       printf("descriptor %u: %08x\n", i, *descriptors);
336       decode_structure(spec, descriptor_structure, descriptors);
337 
338       start = instruction_base + descriptors[0];
339       if (!valid_offset(start)) {
340          printf("kernel: %08"PRIx64" <not valid>\n", start);
341          continue;
342       } else {
343          printf("kernel: %08"PRIx64"\n", start);
344       }
345 
346       insns = (struct brw_instruction *) (gtt + start);
347       gen_disasm_disassemble(disasm, insns, 0, stdout);
348 
349       dump_samplers(spec, descriptors[3] & ~0x1f);
350       dump_binding_table(spec, descriptors[4] & ~0x1f);
351    }
352 }
353 
354 /* Heuristic to determine whether a uint32_t is probably actually a float
355  * (http://stackoverflow.com/a/2953466)
356  */
357 
358 static bool
probably_float(uint32_t bits)359 probably_float(uint32_t bits)
360 {
361    int exp = ((bits & 0x7f800000U) >> 23) - 127;
362    uint32_t mant = bits & 0x007fffff;
363 
364    /* +- 0.0 */
365    if (exp == -127 && mant == 0)
366       return true;
367 
368    /* +- 1 billionth to 1 billion */
369    if (-30 <= exp && exp <= 30)
370       return true;
371 
372    /* some value with only a few binary digits */
373    if ((mant & 0x0000ffff) == 0)
374       return true;
375 
376    return false;
377 }
378 
379 static void
handle_3dstate_vertex_buffers(struct gen_spec * spec,uint32_t * p)380 handle_3dstate_vertex_buffers(struct gen_spec *spec, uint32_t *p)
381 {
382    uint32_t *end, *s, *dw, *dwend;
383    uint64_t offset;
384    int n, i, count, stride;
385 
386    end = (p[0] & 0xff) + p + 2;
387    for (s = &p[1], n = 0; s < end; s += 4, n++) {
388       if (gen_spec_get_gen(spec) >= gen_make_gen(8, 0)) {
389          offset = *(uint64_t *) &s[1];
390          dwend = gtt + offset + s[3];
391       } else {
392          offset = s[1];
393          dwend = gtt + s[2] + 1;
394       }
395 
396       stride = field(s[0], 0, 11);
397       count = 0;
398       printf("vertex buffer %d, size %d\n", n, s[3]);
399       for (dw = gtt + offset, i = 0; dw < dwend && i < 256; dw++) {
400          if (count == 0 && count % (8 * 4) == 0)
401             printf("  ");
402 
403          if (probably_float(*dw))
404             printf("  %8.2f", *(float *) dw);
405          else
406             printf("  0x%08x", *dw);
407 
408          i++;
409          count += 4;
410 
411          if (count == stride) {
412             printf("\n");
413             count = 0;
414          } else if (count % (8 * 4) == 0) {
415             printf("\n");
416          } else {
417             printf(" ");
418          }
419       }
420       if (count > 0 && count % (8 * 4) != 0)
421          printf("\n");
422    }
423 }
424 
425 static void
handle_3dstate_vs(struct gen_spec * spec,uint32_t * p)426 handle_3dstate_vs(struct gen_spec *spec, uint32_t *p)
427 {
428    uint64_t start;
429    struct brw_instruction *insns;
430    int vs_enable;
431 
432    if (gen_spec_get_gen(spec) >= gen_make_gen(8, 0)) {
433       start = get_offset(&p[1], 6, 63);
434       vs_enable = p[7] & 1;
435    } else {
436       start = get_offset(&p[1], 6, 31);
437       vs_enable = p[5] & 1;
438    }
439 
440    if (vs_enable) {
441       printf("instruction_base %08"PRIx64", start %08"PRIx64"\n",
442              instruction_base, start);
443 
444       insns = (struct brw_instruction *) (gtt + instruction_base + start);
445       gen_disasm_disassemble(disasm, insns, 0, stdout);
446    }
447 }
448 
449 static void
handle_3dstate_hs(struct gen_spec * spec,uint32_t * p)450 handle_3dstate_hs(struct gen_spec *spec, uint32_t *p)
451 {
452    uint64_t start;
453    struct brw_instruction *insns;
454    int hs_enable;
455 
456    if (gen_spec_get_gen(spec) >= gen_make_gen(8, 0)) {
457       start = get_offset(&p[3], 6, 63);
458    } else {
459       start = get_offset(&p[3], 6, 31);
460    }
461 
462    hs_enable = p[2] & 0x80000000;
463 
464    if (hs_enable) {
465       printf("instruction_base %08"PRIx64", start %08"PRIx64"\n",
466              instruction_base, start);
467 
468       insns = (struct brw_instruction *) (gtt + instruction_base + start);
469       gen_disasm_disassemble(disasm, insns, 0, stdout);
470    }
471 }
472 
473 static void
handle_3dstate_constant(struct gen_spec * spec,uint32_t * p)474 handle_3dstate_constant(struct gen_spec *spec, uint32_t *p)
475 {
476    int i, j, length;
477    uint32_t *dw;
478    float *f;
479 
480    for (i = 0; i < 4; i++) {
481       length = (p[1 + i / 2] >> (i & 1) * 16) & 0xffff;
482       f = (float *) (gtt + p[3 + i * 2] + dynamic_state_base);
483       dw = (uint32_t *) f;
484       for (j = 0; j < length * 8; j++) {
485          if (probably_float(dw[j]))
486             printf("  %04.3f", f[j]);
487          else
488             printf("  0x%08x", dw[j]);
489 
490          if ((j & 7) == 7)
491             printf("\n");
492       }
493    }
494 }
495 
496 static void
handle_3dstate_ps(struct gen_spec * spec,uint32_t * p)497 handle_3dstate_ps(struct gen_spec *spec, uint32_t *p)
498 {
499    uint32_t mask = ~((1 << 6) - 1);
500    uint64_t start;
501    struct brw_instruction *insns;
502    static const char unused[] = "unused";
503    static const char *pixel_type[3] = {"8 pixel", "16 pixel", "32 pixel"};
504    const char *k0, *k1, *k2;
505    uint32_t k_mask, k1_offset, k2_offset;
506 
507    if (gen_spec_get_gen(spec) >= gen_make_gen(8, 0)) {
508       k_mask = p[6] & 7;
509       k1_offset = 8;
510       k2_offset = 10;
511    } else {
512       k_mask = p[4] & 7;
513       k1_offset = 6;
514       k2_offset = 7;
515    }
516 
517 #define DISPATCH_8 1
518 #define DISPATCH_16 2
519 #define DISPATCH_32 4
520 
521    switch (k_mask) {
522    case DISPATCH_8:
523       k0 = pixel_type[0];
524       k1 = unused;
525       k2 = unused;
526       break;
527    case DISPATCH_16:
528       k0 = pixel_type[1];
529       k1 = unused;
530       k2 = unused;
531       break;
532    case DISPATCH_8 | DISPATCH_16:
533       k0 = pixel_type[0];
534       k1 = unused;
535       k2 = pixel_type[1];
536       break;
537    case DISPATCH_32:
538       k0 = pixel_type[2];
539       k1 = unused;
540       k2 = unused;
541       break;
542    case DISPATCH_16 | DISPATCH_32:
543       k0 = unused;
544       k1 = pixel_type[2];
545       k2 = pixel_type[1];
546       break;
547    case DISPATCH_8 | DISPATCH_16 | DISPATCH_32:
548       k0 = pixel_type[0];
549       k1 = pixel_type[2];
550       k2 = pixel_type[1];
551       break;
552    default:
553       k0 = unused;
554       k1 = unused;
555       k2 = unused;
556       break;
557    }
558 
559    start = instruction_base + (p[1] & mask);
560    printf("  Kernel[0] %s\n", k0);
561    if (k0 != unused) {
562       insns = (struct brw_instruction *) (gtt + start);
563       gen_disasm_disassemble(disasm, insns, 0, stdout);
564    }
565 
566    start = instruction_base + (p[k1_offset] & mask);
567    printf("  Kernel[1] %s\n", k1);
568    if (k1 != unused) {
569       insns = (struct brw_instruction *) (gtt + start);
570       gen_disasm_disassemble(disasm, insns, 0, stdout);
571    }
572 
573    start = instruction_base + (p[k2_offset] & mask);
574    printf("  Kernel[2] %s\n", k2);
575    if (k2 != unused) {
576       insns = (struct brw_instruction *) (gtt + start);
577       gen_disasm_disassemble(disasm, insns, 0, stdout);
578    }
579 }
580 
581 static void
handle_3dstate_binding_table_pointers(struct gen_spec * spec,uint32_t * p)582 handle_3dstate_binding_table_pointers(struct gen_spec *spec, uint32_t *p)
583 {
584    dump_binding_table(spec, p[1]);
585 }
586 
587 static void
handle_3dstate_sampler_state_pointers(struct gen_spec * spec,uint32_t * p)588 handle_3dstate_sampler_state_pointers(struct gen_spec *spec, uint32_t *p)
589 {
590    dump_samplers(spec, p[1]);
591 }
592 
593 static void
handle_3dstate_viewport_state_pointers_cc(struct gen_spec * spec,uint32_t * p)594 handle_3dstate_viewport_state_pointers_cc(struct gen_spec *spec, uint32_t *p)
595 {
596    uint64_t start;
597    struct gen_group *cc_viewport;
598 
599    cc_viewport = gen_spec_find_struct(spec, "CC_VIEWPORT");
600 
601    start = dynamic_state_base + (p[1] & ~0x1fu);
602    for (uint32_t i = 0; i < 4; i++) {
603       printf("viewport %d\n", i);
604       decode_structure(spec, cc_viewport, gtt + start + i * 8);
605    }
606 }
607 
608 static void
handle_3dstate_viewport_state_pointers_sf_clip(struct gen_spec * spec,uint32_t * p)609 handle_3dstate_viewport_state_pointers_sf_clip(struct gen_spec *spec,
610                                                uint32_t *p)
611 {
612    uint64_t start;
613    struct gen_group *sf_clip_viewport;
614 
615    sf_clip_viewport = gen_spec_find_struct(spec, "SF_CLIP_VIEWPORT");
616 
617    start = dynamic_state_base + (p[1] & ~0x3fu);
618    for (uint32_t i = 0; i < 4; i++) {
619       printf("viewport %d\n", i);
620       decode_structure(spec, sf_clip_viewport, gtt + start + i * 64);
621    }
622 }
623 
624 static void
handle_3dstate_blend_state_pointers(struct gen_spec * spec,uint32_t * p)625 handle_3dstate_blend_state_pointers(struct gen_spec *spec, uint32_t *p)
626 {
627    uint64_t start;
628    struct gen_group *blend_state;
629 
630    blend_state = gen_spec_find_struct(spec, "BLEND_STATE");
631 
632    start = dynamic_state_base + (p[1] & ~0x3fu);
633    decode_structure(spec, blend_state, gtt + start);
634 }
635 
636 static void
handle_3dstate_cc_state_pointers(struct gen_spec * spec,uint32_t * p)637 handle_3dstate_cc_state_pointers(struct gen_spec *spec, uint32_t *p)
638 {
639    uint64_t start;
640    struct gen_group *cc_state;
641 
642    cc_state = gen_spec_find_struct(spec, "COLOR_CALC_STATE");
643 
644    start = dynamic_state_base + (p[1] & ~0x3fu);
645    decode_structure(spec, cc_state, gtt + start);
646 }
647 
648 static void
handle_3dstate_scissor_state_pointers(struct gen_spec * spec,uint32_t * p)649 handle_3dstate_scissor_state_pointers(struct gen_spec *spec, uint32_t *p)
650 {
651    uint64_t start;
652    struct gen_group *scissor_rect;
653 
654    scissor_rect = gen_spec_find_struct(spec, "SCISSOR_RECT");
655 
656    start = dynamic_state_base + (p[1] & ~0x1fu);
657    decode_structure(spec, scissor_rect, gtt + start);
658 }
659 
660 static void
handle_load_register_imm(struct gen_spec * spec,uint32_t * p)661 handle_load_register_imm(struct gen_spec *spec, uint32_t *p)
662 {
663    struct gen_group *reg = gen_spec_find_register(spec, p[1]);
664 
665    if (reg != NULL) {
666       printf("register %s (0x%x): 0x%x\n",
667              reg->name, reg->register_offset, p[2]);
668       decode_structure(spec, reg, &p[2]);
669    }
670 }
671 
672 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
673 
674 #define STATE_BASE_ADDRESS                  0x61010000
675 
676 #define MEDIA_INTERFACE_DESCRIPTOR_LOAD     0x70020000
677 
678 #define _3DSTATE_INDEX_BUFFER               0x780a0000
679 #define _3DSTATE_VERTEX_BUFFERS             0x78080000
680 
681 #define _3DSTATE_VS                         0x78100000
682 #define _3DSTATE_GS                         0x78110000
683 #define _3DSTATE_HS                         0x781b0000
684 #define _3DSTATE_DS                         0x781d0000
685 
686 #define _3DSTATE_CONSTANT_VS                0x78150000
687 #define _3DSTATE_CONSTANT_GS                0x78160000
688 #define _3DSTATE_CONSTANT_PS                0x78170000
689 #define _3DSTATE_CONSTANT_HS                0x78190000
690 #define _3DSTATE_CONSTANT_DS                0x781A0000
691 
692 #define _3DSTATE_PS                         0x78200000
693 
694 #define _3DSTATE_BINDING_TABLE_POINTERS_VS  0x78260000
695 #define _3DSTATE_BINDING_TABLE_POINTERS_HS  0x78270000
696 #define _3DSTATE_BINDING_TABLE_POINTERS_DS  0x78280000
697 #define _3DSTATE_BINDING_TABLE_POINTERS_GS  0x78290000
698 #define _3DSTATE_BINDING_TABLE_POINTERS_PS  0x782a0000
699 
700 #define _3DSTATE_SAMPLER_STATE_POINTERS_VS  0x782b0000
701 #define _3DSTATE_SAMPLER_STATE_POINTERS_GS  0x782e0000
702 #define _3DSTATE_SAMPLER_STATE_POINTERS_PS  0x782f0000
703 
704 #define _3DSTATE_VIEWPORT_STATE_POINTERS_CC 0x78230000
705 #define _3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP 0x78210000
706 #define _3DSTATE_BLEND_STATE_POINTERS       0x78240000
707 #define _3DSTATE_CC_STATE_POINTERS          0x780e0000
708 #define _3DSTATE_SCISSOR_STATE_POINTERS     0x780f0000
709 
710 #define _MI_LOAD_REGISTER_IMM               0x11000000
711 
712 struct custom_handler {
713    uint32_t opcode;
714    void (*handle)(struct gen_spec *spec, uint32_t *p);
715 } custom_handlers[] = {
716    { STATE_BASE_ADDRESS, handle_state_base_address },
717    { MEDIA_INTERFACE_DESCRIPTOR_LOAD, handle_media_interface_descriptor_load },
718    { _3DSTATE_VERTEX_BUFFERS, handle_3dstate_vertex_buffers },
719    { _3DSTATE_INDEX_BUFFER, handle_3dstate_index_buffer },
720    { _3DSTATE_VS, handle_3dstate_vs },
721    { _3DSTATE_GS, handle_3dstate_vs },
722    { _3DSTATE_DS, handle_3dstate_vs },
723    { _3DSTATE_HS, handle_3dstate_hs },
724    { _3DSTATE_CONSTANT_VS, handle_3dstate_constant },
725    { _3DSTATE_CONSTANT_GS, handle_3dstate_constant },
726    { _3DSTATE_CONSTANT_PS, handle_3dstate_constant },
727    { _3DSTATE_CONSTANT_HS, handle_3dstate_constant },
728    { _3DSTATE_CONSTANT_DS, handle_3dstate_constant },
729    { _3DSTATE_PS, handle_3dstate_ps },
730 
731    { _3DSTATE_BINDING_TABLE_POINTERS_VS, handle_3dstate_binding_table_pointers },
732    { _3DSTATE_BINDING_TABLE_POINTERS_HS, handle_3dstate_binding_table_pointers },
733    { _3DSTATE_BINDING_TABLE_POINTERS_DS, handle_3dstate_binding_table_pointers },
734    { _3DSTATE_BINDING_TABLE_POINTERS_GS, handle_3dstate_binding_table_pointers },
735    { _3DSTATE_BINDING_TABLE_POINTERS_PS, handle_3dstate_binding_table_pointers },
736 
737    { _3DSTATE_SAMPLER_STATE_POINTERS_VS, handle_3dstate_sampler_state_pointers },
738    { _3DSTATE_SAMPLER_STATE_POINTERS_GS, handle_3dstate_sampler_state_pointers },
739    { _3DSTATE_SAMPLER_STATE_POINTERS_PS, handle_3dstate_sampler_state_pointers },
740 
741    { _3DSTATE_VIEWPORT_STATE_POINTERS_CC, handle_3dstate_viewport_state_pointers_cc },
742    { _3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP, handle_3dstate_viewport_state_pointers_sf_clip },
743    { _3DSTATE_BLEND_STATE_POINTERS, handle_3dstate_blend_state_pointers },
744    { _3DSTATE_CC_STATE_POINTERS, handle_3dstate_cc_state_pointers },
745    { _3DSTATE_SCISSOR_STATE_POINTERS, handle_3dstate_scissor_state_pointers },
746    { _MI_LOAD_REGISTER_IMM, handle_load_register_imm }
747 };
748 
749 static void
parse_commands(struct gen_spec * spec,uint32_t * cmds,int size,int engine)750 parse_commands(struct gen_spec *spec, uint32_t *cmds, int size, int engine)
751 {
752    uint32_t *p, *end = cmds + size / 4;
753    unsigned int length, i;
754    struct gen_group *inst;
755 
756    for (p = cmds; p < end; p += length) {
757       inst = gen_spec_find_instruction(spec, p);
758       if (inst == NULL) {
759          printf("unknown instruction %08x\n", p[0]);
760          length = (p[0] & 0xff) + 2;
761          continue;
762       }
763       length = gen_group_get_length(inst, p);
764 
765       const char *color, *reset_color = NORMAL;
766       uint64_t offset;
767 
768       if (option_full_decode) {
769          if ((p[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_START ||
770              (p[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_END)
771             color = GREEN_HEADER;
772          else
773             color = BLUE_HEADER;
774       } else
775          color = NORMAL;
776 
777       if (option_color == COLOR_NEVER) {
778          color = "";
779          reset_color = "";
780       }
781 
782       if (option_print_offsets)
783          offset = (void *) p - gtt;
784       else
785          offset = 0;
786 
787       printf("%s0x%08"PRIx64":  0x%08x:  %-80s%s\n",
788              color, offset, p[0],
789              gen_group_get_name(inst), reset_color);
790 
791       if (option_full_decode) {
792          struct gen_field_iterator iter;
793          char *token = NULL;
794          int idx = 0, dword_num = 0;
795          gen_field_iterator_init(&iter, inst, p,
796                                  option_color == COLOR_ALWAYS);
797          while (gen_field_iterator_next(&iter)) {
798             idx = 0;
799             print_dword_val(&iter, offset, &dword_num);
800             if (dword_num > 0)
801                 token = print_iterator_values(&iter, &idx);
802             if (token != NULL) {
803                 printf("0x%08"PRIx64":  0x%08x : Dword %d\n",
804                        offset + 4 * idx, p[idx], idx);
805                 handle_struct_decode(spec,token, &p[idx]);
806                 token = NULL;
807             }
808          }
809 
810          for (i = 0; i < ARRAY_LENGTH(custom_handlers); i++) {
811             if (gen_group_get_opcode(inst) ==
812                 custom_handlers[i].opcode)
813                custom_handlers[i].handle(spec, p);
814          }
815       }
816 
817       if ((p[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_START) {
818          uint64_t start = get_address(spec, &p[1]);
819 
820          if (p[0] & (1 << 22)) {
821             /* MI_BATCH_BUFFER_START with "2nd Level Batch Buffer" set acts
822              * like a subroutine call.  Commands that come afterwards get
823              * processed once the 2nd level batch buffer returns with
824              * MI_BATCH_BUFFER_END.
825              */
826             parse_commands(spec, gtt + start, gtt_end - start, engine);
827          } else {
828             /* MI_BATCH_BUFFER_START with "2nd Level Batch Buffer" unset acts
829              * like a goto.  Nothing after it will ever get processed.  In
830              * order to prevent the recursion from growing, we just reset the
831              * loop and continue;
832              */
833             p = gtt + start;
834             /* We don't know where secondaries end so use the GTT end */
835             end = gtt + gtt_end;
836             length = 0;
837             continue;
838          }
839       } else if ((p[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_END) {
840          break;
841       }
842    }
843 }
844 
845 #define GEN_ENGINE_RENDER 1
846 #define GEN_ENGINE_BLITTER 2
847 
848 static void
handle_trace_block(uint32_t * p)849 handle_trace_block(uint32_t *p)
850 {
851    int operation = p[1] & AUB_TRACE_OPERATION_MASK;
852    int type = p[1] & AUB_TRACE_TYPE_MASK;
853    int address_space = p[1] & AUB_TRACE_ADDRESS_SPACE_MASK;
854    uint64_t offset = p[3];
855    uint32_t size = p[4];
856    int header_length = p[0] & 0xffff;
857    uint32_t *data = p + header_length + 2;
858    int engine = GEN_ENGINE_RENDER;
859 
860    if (gen_spec_get_gen(spec) >= gen_make_gen(8,0))
861       offset += (uint64_t) p[5] << 32;
862 
863    switch (operation) {
864    case AUB_TRACE_OP_DATA_WRITE:
865       if (address_space != AUB_TRACE_MEMTYPE_GTT)
866          break;
867       if (gtt_size < offset + size) {
868          fprintf(stderr, "overflow gtt space: %s\n", strerror(errno));
869          exit(EXIT_FAILURE);
870       }
871       memcpy((char *) gtt + offset, data, size);
872       if (gtt_end < offset + size)
873          gtt_end = offset + size;
874       break;
875    case AUB_TRACE_OP_COMMAND_WRITE:
876       switch (type) {
877       case AUB_TRACE_TYPE_RING_PRB0:
878          engine = GEN_ENGINE_RENDER;
879          break;
880       case AUB_TRACE_TYPE_RING_PRB2:
881          engine = GEN_ENGINE_BLITTER;
882          break;
883       default:
884          printf("command write to unknown ring %d\n", type);
885          break;
886       }
887 
888       parse_commands(spec, data, size, engine);
889       gtt_end = 0;
890       break;
891    }
892 }
893 
894 static void
handle_trace_header(uint32_t * p)895 handle_trace_header(uint32_t *p)
896 {
897    /* The intel_aubdump tool from IGT is kind enough to put a PCI-ID= tag in
898     * the AUB header comment.  If the user hasn't specified a hardware
899     * generation, try to use the one from the AUB file.
900     */
901    uint32_t *end = p + (p[0] & 0xffff) + 2;
902    int aub_pci_id = 0;
903    if (end > &p[12] && p[12] > 0)
904       sscanf((char *)&p[13], "PCI-ID=%i", &aub_pci_id);
905 
906    if (pci_id == 0)
907       pci_id = aub_pci_id;
908 
909    struct gen_device_info devinfo;
910    if (!gen_get_device_info(pci_id, &devinfo)) {
911       fprintf(stderr, "can't find device information: pci_id=0x%x\n", pci_id);
912       exit(EXIT_FAILURE);
913    }
914 
915    if (xml_path == NULL)
916       spec = gen_spec_load(&devinfo);
917    else
918       spec = gen_spec_load_from_path(&devinfo, xml_path);
919    disasm = gen_disasm_create(pci_id);
920 
921    if (spec == NULL || disasm == NULL)
922       exit(EXIT_FAILURE);
923 
924    printf("%sAubinator: Intel AUB file decoder.%-80s%s\n",
925           GREEN_HEADER, "", NORMAL);
926 
927    if (input_file)
928       printf("File name:        %s\n", input_file);
929 
930    if (aub_pci_id)
931       printf("PCI ID:           0x%x\n", aub_pci_id);
932 
933    char app_name[33];
934    strncpy(app_name, (char *)&p[2], 32);
935    app_name[32] = 0;
936    printf("Application name: %s\n", app_name);
937 
938    printf("Decoding as:      %s\n", gen_get_device_name(pci_id));
939 
940    /* Throw in a new line before the first batch */
941    printf("\n");
942 }
943 
944 struct aub_file {
945    FILE *stream;
946 
947    uint32_t *map, *end, *cursor;
948    uint32_t *mem_end;
949 };
950 
951 static struct aub_file *
aub_file_open(const char * filename)952 aub_file_open(const char *filename)
953 {
954    struct aub_file *file;
955    struct stat sb;
956    int fd;
957 
958    file = calloc(1, sizeof *file);
959    fd = open(filename, O_RDONLY);
960    if (fd == -1) {
961       fprintf(stderr, "open %s failed: %s\n", filename, strerror(errno));
962       exit(EXIT_FAILURE);
963    }
964 
965    if (fstat(fd, &sb) == -1) {
966       fprintf(stderr, "stat failed: %s\n", strerror(errno));
967       exit(EXIT_FAILURE);
968    }
969 
970    file->map = mmap(NULL, sb.st_size,
971                     PROT_READ, MAP_SHARED, fd, 0);
972    if (file->map == MAP_FAILED) {
973       fprintf(stderr, "mmap failed: %s\n", strerror(errno));
974       exit(EXIT_FAILURE);
975    }
976 
977    file->cursor = file->map;
978    file->end = file->map + sb.st_size / 4;
979 
980    return file;
981 }
982 
983 static struct aub_file *
aub_file_stdin(void)984 aub_file_stdin(void)
985 {
986    struct aub_file *file;
987 
988    file = calloc(1, sizeof *file);
989    file->stream = stdin;
990 
991    return file;
992 }
993 
994 #define TYPE(dw)       (((dw) >> 29) & 7)
995 #define OPCODE(dw)     (((dw) >> 23) & 0x3f)
996 #define SUBOPCODE(dw)  (((dw) >> 16) & 0x7f)
997 
998 #define MAKE_HEADER(type, opcode, subopcode) \
999                    (((type) << 29) | ((opcode) << 23) | ((subopcode) << 16))
1000 
1001 #define TYPE_AUB            0x7
1002 
1003 /* Classic AUB opcodes */
1004 #define OPCODE_AUB          0x01
1005 #define SUBOPCODE_HEADER    0x05
1006 #define SUBOPCODE_BLOCK     0x41
1007 #define SUBOPCODE_BMP       0x1e
1008 
1009 /* Newer version AUB opcode */
1010 #define OPCODE_NEW_AUB      0x2e
1011 #define SUBOPCODE_VERSION   0x00
1012 #define SUBOPCODE_REG_WRITE 0x03
1013 #define SUBOPCODE_MEM_POLL  0x05
1014 #define SUBOPCODE_MEM_WRITE 0x06
1015 
1016 #define MAKE_GEN(major, minor) ( ((major) << 8) | (minor) )
1017 
1018 struct {
1019    const char *name;
1020    uint32_t gen;
1021 } device_map[] = {
1022    { "bwr", MAKE_GEN(4, 0) },
1023    { "cln", MAKE_GEN(4, 0) },
1024    { "blc", MAKE_GEN(4, 0) },
1025    { "ctg", MAKE_GEN(4, 0) },
1026    { "el", MAKE_GEN(4, 0) },
1027    { "il", MAKE_GEN(4, 0) },
1028    { "sbr", MAKE_GEN(6, 0) },
1029    { "ivb", MAKE_GEN(7, 0) },
1030    { "lrb2", MAKE_GEN(0, 0) },
1031    { "hsw", MAKE_GEN(7, 5) },
1032    { "vlv", MAKE_GEN(7, 0) },
1033    { "bdw", MAKE_GEN(8, 0) },
1034    { "skl", MAKE_GEN(9, 0) },
1035    { "chv", MAKE_GEN(8, 0) },
1036    { "bxt", MAKE_GEN(9, 0) }
1037 };
1038 
1039 enum {
1040    AUB_ITEM_DECODE_OK,
1041    AUB_ITEM_DECODE_FAILED,
1042    AUB_ITEM_DECODE_NEED_MORE_DATA,
1043 };
1044 
1045 static int
aub_file_decode_batch(struct aub_file * file)1046 aub_file_decode_batch(struct aub_file *file)
1047 {
1048    uint32_t *p, h, device, data_type, *new_cursor;
1049    int header_length, bias;
1050 
1051    if (file->end - file->cursor < 1)
1052       return AUB_ITEM_DECODE_NEED_MORE_DATA;
1053 
1054    p = file->cursor;
1055    h = *p;
1056    header_length = h & 0xffff;
1057 
1058    switch (OPCODE(h)) {
1059    case OPCODE_AUB:
1060       bias = 2;
1061       break;
1062    case OPCODE_NEW_AUB:
1063       bias = 1;
1064       break;
1065    default:
1066       printf("unknown opcode %d at %td/%td\n",
1067              OPCODE(h), file->cursor - file->map,
1068              file->end - file->map);
1069       return AUB_ITEM_DECODE_FAILED;
1070    }
1071 
1072    new_cursor = p + header_length + bias;
1073    if ((h & 0xffff0000) == MAKE_HEADER(TYPE_AUB, OPCODE_AUB, SUBOPCODE_BLOCK)) {
1074       if (file->end - file->cursor < 4)
1075          return AUB_ITEM_DECODE_NEED_MORE_DATA;
1076       new_cursor += p[4] / 4;
1077    }
1078 
1079    if (new_cursor > file->end)
1080       return AUB_ITEM_DECODE_NEED_MORE_DATA;
1081 
1082    switch (h & 0xffff0000) {
1083    case MAKE_HEADER(TYPE_AUB, OPCODE_AUB, SUBOPCODE_HEADER):
1084       handle_trace_header(p);
1085       break;
1086    case MAKE_HEADER(TYPE_AUB, OPCODE_AUB, SUBOPCODE_BLOCK):
1087       handle_trace_block(p);
1088       break;
1089    case MAKE_HEADER(TYPE_AUB, OPCODE_AUB, SUBOPCODE_BMP):
1090       break;
1091    case MAKE_HEADER(TYPE_AUB, OPCODE_NEW_AUB, SUBOPCODE_VERSION):
1092       printf("version block: dw1 %08x\n", p[1]);
1093       device = (p[1] >> 8) & 0xff;
1094       printf("  device %s\n", device_map[device].name);
1095       break;
1096    case MAKE_HEADER(TYPE_AUB, OPCODE_NEW_AUB, SUBOPCODE_REG_WRITE):
1097       printf("register write block: (dwords %d)\n", h & 0xffff);
1098       printf("  reg 0x%x, data 0x%x\n", p[1], p[5]);
1099       break;
1100    case MAKE_HEADER(TYPE_AUB, OPCODE_NEW_AUB, SUBOPCODE_MEM_WRITE):
1101       printf("memory write block (dwords %d):\n", h & 0xffff);
1102       printf("  address 0x%"PRIx64"\n", *(uint64_t *) &p[1]);
1103       data_type = (p[3] >> 20) & 0xff;
1104       if (data_type != 0)
1105          printf("  data type 0x%x\n", data_type);
1106       printf("  address space 0x%x\n", (p[3] >> 28) & 0xf);
1107       break;
1108    case MAKE_HEADER(TYPE_AUB, OPCODE_NEW_AUB, SUBOPCODE_MEM_POLL):
1109       printf("memory poll block (dwords %d):\n", h & 0xffff);
1110       break;
1111    default:
1112       printf("unknown block type=0x%x, opcode=0x%x, "
1113              "subopcode=0x%x (%08x)\n", TYPE(h), OPCODE(h), SUBOPCODE(h), h);
1114       break;
1115    }
1116    file->cursor = new_cursor;
1117 
1118    return AUB_ITEM_DECODE_OK;
1119 }
1120 
1121 static int
aub_file_more_stuff(struct aub_file * file)1122 aub_file_more_stuff(struct aub_file *file)
1123 {
1124    return file->cursor < file->end || (file->stream && !feof(file->stream));
1125 }
1126 
1127 #define AUB_READ_BUFFER_SIZE (4096)
1128 #define MAX(a, b) ((a) < (b) ? (b) : (a))
1129 
1130 static void
aub_file_data_grow(struct aub_file * file)1131 aub_file_data_grow(struct aub_file *file)
1132 {
1133    size_t old_size = (file->mem_end - file->map) * 4;
1134    size_t new_size = MAX(old_size * 2, AUB_READ_BUFFER_SIZE);
1135    uint32_t *new_start = realloc(file->map, new_size);
1136 
1137    file->cursor = new_start + (file->cursor - file->map);
1138    file->end = new_start + (file->end - file->map);
1139    file->map = new_start;
1140    file->mem_end = file->map + (new_size / 4);
1141 }
1142 
1143 static bool
aub_file_data_load(struct aub_file * file)1144 aub_file_data_load(struct aub_file *file)
1145 {
1146    size_t r;
1147 
1148    if (file->stream == NULL)
1149       return false;
1150 
1151    /* First remove any consumed data */
1152    if (file->cursor > file->map) {
1153       memmove(file->map, file->cursor,
1154               (file->end - file->cursor) * 4);
1155       file->end -= file->cursor - file->map;
1156       file->cursor = file->map;
1157    }
1158 
1159    /* Then load some new data in */
1160    if ((file->mem_end - file->end) < (AUB_READ_BUFFER_SIZE / 4))
1161       aub_file_data_grow(file);
1162 
1163    r = fread(file->end, 1, (file->mem_end - file->end) * 4, file->stream);
1164    file->end += r / 4;
1165 
1166    return r != 0;
1167 }
1168 
1169 static void
setup_pager(void)1170 setup_pager(void)
1171 {
1172    int fds[2];
1173    pid_t pid;
1174 
1175    if (!isatty(1))
1176       return;
1177 
1178    if (pipe(fds) == -1)
1179       return;
1180 
1181    pid = fork();
1182    if (pid == -1)
1183       return;
1184 
1185    if (pid == 0) {
1186       close(fds[1]);
1187       dup2(fds[0], 0);
1188       execlp("less", "less", "-FRSi", NULL);
1189    }
1190 
1191    close(fds[0]);
1192    dup2(fds[1], 1);
1193    close(fds[1]);
1194 }
1195 
1196 static void
print_help(const char * progname,FILE * file)1197 print_help(const char *progname, FILE *file)
1198 {
1199    fprintf(file,
1200            "Usage: %s [OPTION]... [FILE]\n"
1201            "Decode aub file contents from either FILE or the standard input.\n\n"
1202            "A valid --gen option must be provided.\n\n"
1203            "      --help          display this help and exit\n"
1204            "      --gen=platform  decode for given platform (ivb, byt, hsw, bdw, chv, skl, kbl or bxt)\n"
1205            "      --headers       decode only command headers\n"
1206            "      --color[=WHEN]  colorize the output; WHEN can be 'auto' (default\n"
1207            "                        if omitted), 'always', or 'never'\n"
1208            "      --no-pager      don't launch pager\n"
1209            "      --no-offsets    don't print instruction offsets\n"
1210            "      --xml=DIR       load hardware xml description from directory DIR\n",
1211            progname);
1212 }
1213 
main(int argc,char * argv[])1214 int main(int argc, char *argv[])
1215 {
1216    struct aub_file *file;
1217    int c, i;
1218    bool help = false, pager = true;
1219    const struct {
1220       const char *name;
1221       int pci_id;
1222    } gens[] = {
1223       { "ivb", 0x0166 }, /* Intel(R) Ivybridge Mobile GT2 */
1224       { "hsw", 0x0416 }, /* Intel(R) Haswell Mobile GT2 */
1225       { "byt", 0x0155 }, /* Intel(R) Bay Trail */
1226       { "bdw", 0x1616 }, /* Intel(R) HD Graphics 5500 (Broadwell GT2) */
1227       { "chv", 0x22B3 }, /* Intel(R) HD Graphics (Cherryview) */
1228       { "skl", 0x1912 }, /* Intel(R) HD Graphics 530 (Skylake GT2) */
1229       { "kbl", 0x591D }, /* Intel(R) Kabylake GT2 */
1230       { "bxt", 0x0A84 }  /* Intel(R) HD Graphics (Broxton) */
1231    };
1232    const struct option aubinator_opts[] = {
1233       { "help",       no_argument,       (int *) &help,                 true },
1234       { "no-pager",   no_argument,       (int *) &pager,                false },
1235       { "no-offsets", no_argument,       (int *) &option_print_offsets, false },
1236       { "gen",        required_argument, NULL,                          'g' },
1237       { "headers",    no_argument,       (int *) &option_full_decode,   false },
1238       { "color",      required_argument, NULL,                          'c' },
1239       { "xml",        required_argument, NULL,                          'x' },
1240       { NULL,         0,                 NULL,                          0 }
1241    };
1242 
1243    i = 0;
1244    while ((c = getopt_long(argc, argv, "", aubinator_opts, &i)) != -1) {
1245       switch (c) {
1246       case 'g':
1247          for (i = 0; i < ARRAY_SIZE(gens); i++) {
1248             if (!strcmp(optarg, gens[i].name)) {
1249                pci_id = gens[i].pci_id;
1250                break;
1251             }
1252          }
1253          if (i == ARRAY_SIZE(gens)) {
1254             fprintf(stderr, "can't parse gen: '%s', expected ivb, byt, hsw, "
1255                                    "bdw, chv, skl, kbl or bxt\n", optarg);
1256             exit(EXIT_FAILURE);
1257          }
1258          break;
1259       case 'c':
1260          if (optarg == NULL || strcmp(optarg, "always") == 0)
1261             option_color = COLOR_ALWAYS;
1262          else if (strcmp(optarg, "never") == 0)
1263             option_color = COLOR_NEVER;
1264          else if (strcmp(optarg, "auto") == 0)
1265             option_color = COLOR_AUTO;
1266          else {
1267             fprintf(stderr, "invalid value for --color: %s", optarg);
1268             exit(EXIT_FAILURE);
1269          }
1270          break;
1271       case 'x':
1272          xml_path = strdup(optarg);
1273          break;
1274       default:
1275          break;
1276       }
1277    }
1278 
1279    if (help || argc == 1) {
1280       print_help(argv[0], stderr);
1281       exit(0);
1282    }
1283 
1284    if (optind < argc)
1285       input_file = argv[optind];
1286 
1287    /* Do this before we redirect stdout to pager. */
1288    if (option_color == COLOR_AUTO)
1289       option_color = isatty(1) ? COLOR_ALWAYS : COLOR_NEVER;
1290 
1291    if (isatty(1) && pager)
1292       setup_pager();
1293 
1294    if (input_file == NULL)
1295       file = aub_file_stdin();
1296    else
1297       file = aub_file_open(input_file);
1298 
1299    /* mmap a terabyte for our gtt space. */
1300    gtt_size = 1ull << 40;
1301    gtt = mmap(NULL, gtt_size, PROT_READ | PROT_WRITE,
1302               MAP_PRIVATE | MAP_ANONYMOUS |  MAP_NORESERVE, -1, 0);
1303    if (gtt == MAP_FAILED) {
1304       fprintf(stderr, "failed to alloc gtt space: %s\n", strerror(errno));
1305       exit(EXIT_FAILURE);
1306    }
1307 
1308    while (aub_file_more_stuff(file)) {
1309       switch (aub_file_decode_batch(file)) {
1310       case AUB_ITEM_DECODE_OK:
1311          break;
1312       case AUB_ITEM_DECODE_NEED_MORE_DATA:
1313          if (!file->stream) {
1314             file->cursor = file->end;
1315             break;
1316          }
1317          if (aub_file_more_stuff(file) && !aub_file_data_load(file)) {
1318             fprintf(stderr, "failed to load data from stdin\n");
1319             exit(EXIT_FAILURE);
1320          }
1321          break;
1322       default:
1323          fprintf(stderr, "failed to parse aubdump data\n");
1324          exit(EXIT_FAILURE);
1325       }
1326    }
1327 
1328 
1329    fflush(stdout);
1330    /* close the stdout which is opened to write the output */
1331    close(1);
1332    free(xml_path);
1333 
1334    wait(NULL);
1335 
1336    return EXIT_SUCCESS;
1337 }
1338