blob: 035b17f119b5f239c002ccd5048f12834e311527 [file] [log] [blame]
Chris Wilson9eb4de12010-02-12 13:28:40 +00001/*
2 * Copyright © 2007 Intel Corporation
3 * Copyright © 2009 Intel Corporation
4 * Copyright © 2010 Intel Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE.
24 *
25 * Authors:
26 * Eric Anholt <eric@anholt.net>
27 * Carl Worth <cworth@cworth.org>
28 * Chris Wilson <chris@chris-wilson.co.uk>
29 *
30 */
31
32/** @file intel_decode.c
33 * This file contains code to print out batchbuffer contents in a
34 * human-readable format.
35 *
36 * The current version only supports i915 packets, and only pretty-prints a
37 * subset of them. The intention is for it to make just a best attempt to
38 * decode, but never crash in the process.
39 */
40
41#define _GNU_SOURCE
Damien Lespiau1d2ef952013-12-16 11:28:39 +000042#include <stdbool.h>
Chris Wilson9eb4de12010-02-12 13:28:40 +000043#include <stdio.h>
44#include <stdlib.h>
45#include <stdarg.h>
46#include <string.h>
Chris Wilsona3a78632010-12-01 21:51:59 +000047#include <unistd.h>
Chris Wilson9eb4de12010-02-12 13:28:40 +000048#include <inttypes.h>
49#include <errno.h>
50#include <sys/stat.h>
51#include <err.h>
Imre Deak5efd3d32012-10-10 16:04:39 +030052#include <assert.h>
Daniel Vetter7ee9f162012-01-09 23:38:17 +010053#include <intel_bufmgr.h>
Chris Wilson9eb4de12010-02-12 13:28:40 +000054
Chris Wilson9eb4de12010-02-12 13:28:40 +000055#include "intel_chipset.h"
Daniel Vetterc03c6ce2014-03-22 21:34:29 +010056#include "intel_io.h"
Chris Wilson9eb4de12010-02-12 13:28:40 +000057#include "instdone.h"
Daniel Vetter6cfcd712014-03-22 20:07:35 +010058#include "intel_reg.h"
Zhao Yakuif04bf002014-01-22 09:37:20 +080059#include "drmtest.h"
Chris Wilson9eb4de12010-02-12 13:28:40 +000060
Ben Widawskyafeb4312013-04-22 11:36:03 -070061static uint32_t
62print_head(unsigned int reg)
63{
64 printf(" head = 0x%08x, wraps = %d\n", reg & (0x7ffff<<2), reg >> 21);
65 return reg & (0x7ffff<<2);
66}
67
Mika Kuoppalaedee9522013-07-12 17:02:04 +030068static uint32_t
69print_ctl(unsigned int reg)
70{
71 uint32_t ring_length = (((reg & (0x1ff << 12)) >> 12) + 1) * 4096;
72
73#define BIT_STR(reg, x, on, off) ((1 << (x)) & reg) ? on : off
74
75 printf(" len=%d%s%s%s\n", ring_length,
76 BIT_STR(reg, 0, ", enabled", ", disabled"),
77 BIT_STR(reg, 10, ", semaphore wait ", ""),
78 BIT_STR(reg, 11, ", rb wait ", "")
79 );
80#undef BIT_STR
81 return ring_length;
82}
83
84static void
85print_acthd(unsigned int reg, unsigned int ring_length)
86{
87 if ((reg & (0x7ffff << 2)) < ring_length)
88 printf(" at ring: 0x%08x\n", reg & (0x7ffff << 2));
89 else
90 printf(" at batch: 0x%08x\n", reg);
91}
92
Chris Wilson9eb4de12010-02-12 13:28:40 +000093static void
Ben Widawsky155aa1e2013-04-22 11:07:35 -070094print_instdone(uint32_t devid, unsigned int instdone, unsigned int instdone1)
Chris Wilson9eb4de12010-02-12 13:28:40 +000095{
Ben Widawsky155aa1e2013-04-22 11:07:35 -070096 int i;
97 static int once;
Chris Wilson95374222010-04-08 11:56:57 +010098
Ben Widawsky155aa1e2013-04-22 11:07:35 -070099 if (!once) {
100 init_instdone_definitions(devid);
101 once = 1;
Chris Wilson9eb4de12010-02-12 13:28:40 +0000102 }
103
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700104 for (i = 0; i < num_instdone_bits; i++) {
105 int busy = 0;
106
Damien Lespiaud8b1dee2013-06-04 05:41:36 +0100107 if (instdone_bits[i].reg == INSTDONE_1) {
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700108 if (!(instdone1 & instdone_bits[i].bit))
109 busy = 1;
110 } else {
111 if (!(instdone & instdone_bits[i].bit))
112 busy = 1;
113 }
114
115 if (busy)
116 printf(" busy: %s\n", instdone_bits[i].name);
117 }
Chris Wilson9eb4de12010-02-12 13:28:40 +0000118}
119
120static void
Chris Wilsonbfc2b532010-03-04 21:56:04 +0000121print_i830_pgtbl_err(unsigned int reg)
122{
123 const char *str;
124
125 switch((reg >> 3) & 0xf) {
126 case 0x1: str = "Overlay TLB"; break;
127 case 0x2: str = "Display A TLB"; break;
128 case 0x3: str = "Host TLB"; break;
129 case 0x4: str = "Render TLB"; break;
130 case 0x5: str = "Display C TLB"; break;
131 case 0x6: str = "Mapping TLB"; break;
132 case 0x7: str = "Command Stream TLB"; break;
133 case 0x8: str = "Vertex Buffer TLB"; break;
134 case 0x9: str = "Display B TLB"; break;
135 case 0xa: str = "Reserved System Memory"; break;
136 case 0xb: str = "Compressor TLB"; break;
137 case 0xc: str = "Binner TLB"; break;
138 default: str = "unknown"; break;
139 }
140
141 if (str)
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700142 printf(" source = %s\n", str);
Chris Wilsonbfc2b532010-03-04 21:56:04 +0000143
144 switch(reg & 0x7) {
145 case 0x0: str = "Invalid GTT"; break;
146 case 0x1: str = "Invalid GTT PTE"; break;
147 case 0x2: str = "Invalid Memory"; break;
148 case 0x3: str = "Invalid TLB miss"; break;
149 case 0x4: str = "Invalid PTE data"; break;
150 case 0x5: str = "Invalid LocalMemory not present"; break;
151 case 0x6: str = "Invalid Tiling"; break;
152 case 0x7: str = "Host to CAM"; break;
153 }
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700154 printf(" error = %s\n", str);
Chris Wilsonbfc2b532010-03-04 21:56:04 +0000155}
156
157static void
Chris Wilson2d1ad952010-07-15 19:18:39 +0100158print_i915_pgtbl_err(unsigned int reg)
159{
160 if (reg & (1 << 29))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700161 printf(" Cursor A: Invalid GTT PTE\n");
Chris Wilson2d1ad952010-07-15 19:18:39 +0100162 if (reg & (1 << 28))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700163 printf(" Cursor B: Invalid GTT PTE\n");
Chris Wilson2d1ad952010-07-15 19:18:39 +0100164 if (reg & (1 << 27))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700165 printf(" MT: Invalid tiling\n");
Chris Wilson2d1ad952010-07-15 19:18:39 +0100166 if (reg & (1 << 26))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700167 printf(" MT: Invalid GTT PTE\n");
Chris Wilson2d1ad952010-07-15 19:18:39 +0100168 if (reg & (1 << 25))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700169 printf(" LC: Invalid tiling\n");
Chris Wilson2d1ad952010-07-15 19:18:39 +0100170 if (reg & (1 << 24))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700171 printf(" LC: Invalid GTT PTE\n");
Chris Wilson2d1ad952010-07-15 19:18:39 +0100172 if (reg & (1 << 23))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700173 printf(" BIN VertexData: Invalid GTT PTE\n");
Chris Wilson2d1ad952010-07-15 19:18:39 +0100174 if (reg & (1 << 22))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700175 printf(" BIN Instruction: Invalid GTT PTE\n");
Chris Wilson2d1ad952010-07-15 19:18:39 +0100176 if (reg & (1 << 21))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700177 printf(" CS VertexData: Invalid GTT PTE\n");
Chris Wilson2d1ad952010-07-15 19:18:39 +0100178 if (reg & (1 << 20))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700179 printf(" CS Instruction: Invalid GTT PTE\n");
Chris Wilson2d1ad952010-07-15 19:18:39 +0100180 if (reg & (1 << 19))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700181 printf(" CS: Invalid GTT\n");
Chris Wilson2d1ad952010-07-15 19:18:39 +0100182 if (reg & (1 << 18))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700183 printf(" Overlay: Invalid tiling\n");
Chris Wilson2d1ad952010-07-15 19:18:39 +0100184 if (reg & (1 << 16))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700185 printf(" Overlay: Invalid GTT PTE\n");
Chris Wilson2d1ad952010-07-15 19:18:39 +0100186 if (reg & (1 << 14))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700187 printf(" Display C: Invalid tiling\n");
Chris Wilson2d1ad952010-07-15 19:18:39 +0100188 if (reg & (1 << 12))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700189 printf(" Display C: Invalid GTT PTE\n");
Chris Wilson2d1ad952010-07-15 19:18:39 +0100190 if (reg & (1 << 10))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700191 printf(" Display B: Invalid tiling\n");
Chris Wilson2d1ad952010-07-15 19:18:39 +0100192 if (reg & (1 << 8))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700193 printf(" Display B: Invalid GTT PTE\n");
Chris Wilson2d1ad952010-07-15 19:18:39 +0100194 if (reg & (1 << 6))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700195 printf(" Display A: Invalid tiling\n");
Chris Wilson2d1ad952010-07-15 19:18:39 +0100196 if (reg & (1 << 4))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700197 printf(" Display A: Invalid GTT PTE\n");
Chris Wilson2d1ad952010-07-15 19:18:39 +0100198 if (reg & (1 << 1))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700199 printf(" Host Invalid PTE data\n");
Chris Wilson2d1ad952010-07-15 19:18:39 +0100200 if (reg & (1 << 0))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700201 printf(" Host Invalid GTT PTE\n");
Chris Wilson2d1ad952010-07-15 19:18:39 +0100202}
203
204static void
Chris Wilson89343952010-10-22 11:33:08 +0100205print_i965_pgtbl_err(unsigned int reg)
206{
207 if (reg & (1 << 26))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700208 printf(" Invalid Sampler Cache GTT entry\n");
Chris Wilson89343952010-10-22 11:33:08 +0100209 if (reg & (1 << 24))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700210 printf(" Invalid Render Cache GTT entry\n");
Chris Wilson89343952010-10-22 11:33:08 +0100211 if (reg & (1 << 23))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700212 printf(" Invalid Instruction/State Cache GTT entry\n");
Chris Wilson89343952010-10-22 11:33:08 +0100213 if (reg & (1 << 22))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700214 printf(" There is no ROC, this cannot occur!\n");
Chris Wilson89343952010-10-22 11:33:08 +0100215 if (reg & (1 << 21))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700216 printf(" Invalid GTT entry during Vertex Fetch\n");
Chris Wilson89343952010-10-22 11:33:08 +0100217 if (reg & (1 << 20))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700218 printf(" Invalid GTT entry during Command Fetch\n");
Chris Wilson89343952010-10-22 11:33:08 +0100219 if (reg & (1 << 19))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700220 printf(" Invalid GTT entry during CS\n");
Chris Wilson89343952010-10-22 11:33:08 +0100221 if (reg & (1 << 18))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700222 printf(" Invalid GTT entry during Cursor Fetch\n");
Chris Wilson89343952010-10-22 11:33:08 +0100223 if (reg & (1 << 17))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700224 printf(" Invalid GTT entry during Overlay Fetch\n");
Chris Wilson89343952010-10-22 11:33:08 +0100225 if (reg & (1 << 8))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700226 printf(" Invalid GTT entry during Display B Fetch\n");
Chris Wilson89343952010-10-22 11:33:08 +0100227 if (reg & (1 << 4))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700228 printf(" Invalid GTT entry during Display A Fetch\n");
Chris Wilson89343952010-10-22 11:33:08 +0100229 if (reg & (1 << 1))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700230 printf(" Valid PTE references illegal memory\n");
Chris Wilson89343952010-10-22 11:33:08 +0100231 if (reg & (1 << 0))
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700232 printf(" Invalid GTT entry during fetch for host\n");
Chris Wilson89343952010-10-22 11:33:08 +0100233}
234
235static void
Chris Wilsonbfc2b532010-03-04 21:56:04 +0000236print_pgtbl_err(unsigned int reg, unsigned int devid)
237{
238 if (IS_965(devid)) {
Chris Wilson89343952010-10-22 11:33:08 +0100239 return print_i965_pgtbl_err(reg);
Chris Wilson41570d92011-02-14 15:56:14 +0000240 } else if (IS_GEN3(devid)) {
Chris Wilson2d1ad952010-07-15 19:18:39 +0100241 return print_i915_pgtbl_err(reg);
Chris Wilsonbfc2b532010-03-04 21:56:04 +0000242 } else {
243 return print_i830_pgtbl_err(reg);
244 }
245}
246
Chris Wilsonb6470462014-12-05 11:04:42 +0000247static void print_ivb_error(unsigned int reg, unsigned int devid)
248{
249 if (reg & (1 << 0))
250 printf(" TLB page fault error (GTT entry not valid)\n");
251 if (reg & (1 << 1))
252 printf(" Invalid physical address in RSTRM interface (PAVP)\n");
253 if (reg & (1 << 2))
254 printf(" Invalid page directory entry error\n");
255 if (reg & (1 << 3))
256 printf(" Invalid physical address in ROSTRM interface (PAVP)\n");
257 if (reg & (1 << 4))
258 printf(" TLB page VTD translation generated an error\n");
259 if (reg & (1 << 5))
260 printf(" Invalid physical address in WRITE interface (PAVP)\n");
261 if (reg & (1 << 6))
262 printf(" Page directory VTD translation generated error\n");
263 if (reg & (1 << 8))
264 printf(" Cacheline containing a PD was marked as invalid\n");
265 if (IS_HASWELL(devid) && (reg >> 10) & 0x1f)
266 printf(" %d pending page faults\n", (reg >> 10) & 0x1f);
267}
268
269static void print_snb_error(unsigned int reg)
270{
271 if (reg & (1 << 0))
272 printf(" TLB page fault error (GTT entry not valid)\n");
273 if (reg & (1 << 1))
274 printf(" Context page GTT translation generated a fault (GTT entry not valid)\n");
275 if (reg & (1 << 2))
276 printf(" Invalid page directory entry error\n");
277 if (reg & (1 << 3))
278 printf(" HWS page GTT translation generated a page fault (GTT entry not valid)\n");
279 if (reg & (1 << 4))
280 printf(" TLB page VTD translation generated an error\n");
281 if (reg & (1 << 5))
282 printf(" Context page VTD translation generated an error\n");
283 if (reg & (1 << 6))
284 printf(" Page directory VTD translation generated error\n");
285 if (reg & (1 << 7))
286 printf(" HWS page VTD translation generated an error\n");
287 if (reg & (1 << 8))
288 printf(" Cacheline containing a PD was marked as invalid\n");
289}
290
291static void
292print_error(unsigned int reg, unsigned int devid)
293{
294 switch (intel_gen(devid)) {
295 case 7: return print_ivb_error(reg, devid);
296 case 6: return print_snb_error(reg);
297 }
298}
299
Chris Wilsonbfc2b532010-03-04 21:56:04 +0000300static void
Daniel Vetterd5109532011-10-21 19:06:08 +0200301print_snb_fence(unsigned int devid, uint64_t fence)
302{
303 printf(" %svalid, %c-tiled, pitch: %i, start: 0x%08x, size: %u\n",
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700304 fence & 1 ? "" : "in",
305 fence & (1<<1) ? 'y' : 'x',
306 (int)(((fence>>32)&0xfff)+1)*128,
307 (uint32_t)fence & 0xfffff000,
308 (uint32_t)(((fence>>32)&0xfffff000) - (fence&0xfffff000) + 4096));
Daniel Vetterd5109532011-10-21 19:06:08 +0200309}
310
311static void
312print_i965_fence(unsigned int devid, uint64_t fence)
313{
314 printf(" %svalid, %c-tiled, pitch: %i, start: 0x%08x, size: %u\n",
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700315 fence & 1 ? "" : "in",
316 fence & (1<<1) ? 'y' : 'x',
317 (int)(((fence>>2)&0x1ff)+1)*128,
318 (uint32_t)fence & 0xfffff000,
319 (uint32_t)(((fence>>32)&0xfffff000) - (fence&0xfffff000) + 4096));
Daniel Vetterd5109532011-10-21 19:06:08 +0200320}
321
322static void
Daniel Vettercf5db112011-03-14 11:20:17 +0100323print_i915_fence(unsigned int devid, uint64_t fence)
324{
325 unsigned tile_width;
326 if ((fence & 12) && !IS_915(devid))
327 tile_width = 128;
328 else
329 tile_width = 512;
330
331 printf(" %svalid, %c-tiled, pitch: %i, start: 0x%08x, size: %i\n",
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700332 fence & 1 ? "" : "in",
Chris Wilson58ac17d2013-10-21 09:31:18 +0100333 fence & (1<<12) ? 'y' : 'x',
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700334 (1<<((fence>>4)&0xf))*tile_width,
335 (uint32_t)fence & 0xff00000,
336 1<<(20 + ((fence>>8)&0xf)));
Daniel Vettercf5db112011-03-14 11:20:17 +0100337}
338
339static void
340print_i830_fence(unsigned int devid, uint64_t fence)
341{
342 printf(" %svalid, %c-tiled, pitch: %i, start: 0x%08x, size: %i\n",
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700343 fence & 1 ? "" : "in",
Chris Wilson58ac17d2013-10-21 09:31:18 +0100344 fence & (1<<12) ? 'y' : 'x',
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700345 (1<<((fence>>4)&0xf))*128,
346 (uint32_t)fence & 0x7f80000,
347 1<<(19 + ((fence>>8)&0xf)));
Daniel Vettercf5db112011-03-14 11:20:17 +0100348}
349
350static void
351print_fence(unsigned int devid, uint64_t fence)
352{
Daniel Vetterd5109532011-10-21 19:06:08 +0200353 if (IS_GEN6(devid) || IS_GEN7(devid)) {
354 return print_snb_fence(devid, fence);
355 } else if (IS_GEN4(devid) || IS_GEN5(devid)) {
356 return print_i965_fence(devid, fence);
Daniel Vettercf5db112011-03-14 11:20:17 +0100357 } else if (IS_GEN3(devid)) {
358 return print_i915_fence(devid, fence);
359 } else {
360 return print_i830_fence(devid, fence);
361 }
362}
363
Ben Widawskyafeb4312013-04-22 11:36:03 -0700364#define MAX_RINGS 10 /* I really hope this never... */
365uint32_t head[MAX_RINGS];
366int head_ndx = 0;
Chris Wilsonb5109e62014-01-23 21:50:36 +0000367int num_rings = 0;
Ben Widawskyafeb4312013-04-22 11:36:03 -0700368static void print_batch(int is_batch, const char *ring_name, uint32_t gtt_offset)
369{
370 const char *buffer_type[2] = { "ringbuffer", "batchbuffer" };
Chris Wilsonb5109e62014-01-23 21:50:36 +0000371 if (is_batch || !num_rings)
Ben Widawskyafeb4312013-04-22 11:36:03 -0700372 printf("%s (%s) at 0x%08x\n", buffer_type[is_batch], ring_name, gtt_offset);
Chris Wilsonb5109e62014-01-23 21:50:36 +0000373 else
Ben Widawskyafeb4312013-04-22 11:36:03 -0700374 printf("%s (%s) at 0x%08x; HEAD points to: 0x%08x\n", buffer_type[is_batch], ring_name, gtt_offset, head[head_ndx++ % num_rings] + gtt_offset);
Ben Widawskyafeb4312013-04-22 11:36:03 -0700375}
376
Damien Lespiau1d2ef952013-12-16 11:28:39 +0000377static void decode(struct drm_intel_decode *ctx, bool is_batch,
378 const char *ring_name, uint32_t gtt_offset, uint32_t *data,
379 int *count)
380{
381 if (!*count)
382 return;
383
384 print_batch(is_batch, ring_name, gtt_offset);
385 drm_intel_decode_set_batch_pointer(ctx, data, gtt_offset, *count);
386 drm_intel_decode(ctx);
387 *count = 0;
388}
389
Daniel Vettercf5db112011-03-14 11:20:17 +0100390static void
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700391read_data_file(FILE *file)
Chris Wilson9eb4de12010-02-12 13:28:40 +0000392{
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700393 struct drm_intel_decode *decode_ctx = NULL;
394 uint32_t devid = PCI_CHIP_I855_GM;
395 uint32_t *data = NULL;
396 long long unsigned fence;
397 int data_size = 0, count = 0, line_number = 0, matched;
398 char *line = NULL;
399 size_t line_size;
Mika Kuoppalaedee9522013-07-12 17:02:04 +0300400 uint32_t offset, value, ring_length = 0;
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700401 uint32_t gtt_offset = 0, new_gtt_offset;
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700402 char *ring_name = NULL;
403 int is_batch = 1;
Chris Wilson9eb4de12010-02-12 13:28:40 +0000404
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700405 while (getline(&line, &line_size, file) > 0) {
406 char *dashes;
407 line_number++;
Chris Wilson9eb4de12010-02-12 13:28:40 +0000408
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700409 dashes = strstr(line, "---");
410 if (dashes) {
411 char *new_ring_name = malloc(dashes - line);
412 strncpy(new_ring_name, line, dashes - line);
413 new_ring_name[dashes - line - 1] = '\0';
Daniel Vetter73c21042011-10-11 13:14:02 +0200414
Ben Widawskyafeb4312013-04-22 11:36:03 -0700415 if (num_rings == -1)
416 num_rings = head_ndx;
417
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700418 matched = sscanf(dashes, "--- gtt_offset = 0x%08x\n",
419 &new_gtt_offset);
420 if (matched == 1) {
Damien Lespiau1d2ef952013-12-16 11:28:39 +0000421 decode(decode_ctx, is_batch, ring_name,
422 gtt_offset, data, &count);
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700423 gtt_offset = new_gtt_offset;
424 is_batch = 1;
425 free(ring_name);
426 ring_name = new_ring_name;
427 continue;
428 }
429
430 matched = sscanf(dashes, "--- ringbuffer = 0x%08x\n",
431 &new_gtt_offset);
432 if (matched == 1) {
Damien Lespiau1d2ef952013-12-16 11:28:39 +0000433 decode(decode_ctx, is_batch, ring_name,
434 gtt_offset, data, &count);
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700435 gtt_offset = new_gtt_offset;
436 is_batch = 0;
437 free(ring_name);
438 ring_name = new_ring_name;
439 continue;
440 }
441 }
442
443 matched = sscanf(line, "%08x : %08x", &offset, &value);
444 if (matched != 2) {
445 unsigned int reg;
446
447 /* display reg section is after the ringbuffers, don't mix them */
Damien Lespiau1d2ef952013-12-16 11:28:39 +0000448 decode(decode_ctx, is_batch, ring_name, gtt_offset,
449 data, &count);
Chris Wilson9eb4de12010-02-12 13:28:40 +0000450
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700451 printf("%s", line);
452
453 matched = sscanf(line, "PCI ID: 0x%04x\n", &reg);
454 if (matched == 0)
455 matched = sscanf(line, " PCI ID: 0x%04x\n", &reg);
456 if (matched == 0) {
457 const char *pci_id_start = strstr(line, "PCI ID");
458 if (pci_id_start)
459 matched = sscanf(pci_id_start, "PCI ID: 0x%04x\n", &reg);
Chris Wilson98eb5a52011-01-09 13:26:50 +0000460 }
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700461 if (matched == 1) {
462 devid = reg;
463 printf("Detected GEN%i chipset\n",
464 intel_gen(devid));
465
466 decode_ctx = drm_intel_decode_context_alloc(devid);
467 }
468
Mika Kuoppalaedee9522013-07-12 17:02:04 +0300469 matched = sscanf(line, " CTL: 0x%08x\n", &reg);
470 if (matched == 1)
471 ring_length = print_ctl(reg);
472
Ben Widawskyafeb4312013-04-22 11:36:03 -0700473 matched = sscanf(line, " HEAD: 0x%08x\n", &reg);
474 if (matched == 1) {
Chris Wilsonb5109e62014-01-23 21:50:36 +0000475 head[num_rings++] = print_head(reg);
Ben Widawskyafeb4312013-04-22 11:36:03 -0700476 }
477
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700478 matched = sscanf(line, " ACTHD: 0x%08x\n", &reg);
Mika Kuoppalaedee9522013-07-12 17:02:04 +0300479 if (matched == 1) {
480 print_acthd(reg, ring_length);
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700481 drm_intel_decode_set_head_tail(decode_ctx, reg, 0xffffffff);
Mika Kuoppalaedee9522013-07-12 17:02:04 +0300482 }
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700483
484 matched = sscanf(line, " PGTBL_ER: 0x%08x\n", &reg);
485 if (matched == 1 && reg)
486 print_pgtbl_err(reg, devid);
487
Chris Wilsonb6470462014-12-05 11:04:42 +0000488 matched = sscanf(line, " ERROR: 0x%08x\n", &reg);
489 if (matched == 1 && reg)
490 print_error(reg, devid);
491
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700492 matched = sscanf(line, " INSTDONE: 0x%08x\n", &reg);
493 if (matched == 1)
494 print_instdone(devid, reg, -1);
495
496 matched = sscanf(line, " INSTDONE1: 0x%08x\n", &reg);
497 if (matched == 1)
498 print_instdone(devid, -1, reg);
499
500 matched = sscanf(line, " fence[%i] = %Lx\n", &reg, &fence);
501 if (matched == 2)
502 print_fence(devid, fence);
503
Chris Wilson98eb5a52011-01-09 13:26:50 +0000504 continue;
505 }
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700506
507 count++;
508
509 if (count > data_size) {
510 data_size = data_size ? data_size * 2 : 1024;
511 data = realloc(data, data_size * sizeof (uint32_t));
512 if (data == NULL) {
513 fprintf(stderr, "Out of memory.\n");
514 exit(1);
515 }
516 }
517
518 data[count-1] = value;
Chris Wilson9eb4de12010-02-12 13:28:40 +0000519 }
520
Damien Lespiau1d2ef952013-12-16 11:28:39 +0000521 decode(decode_ctx, is_batch, ring_name, gtt_offset, data, &count);
Chris Wilson9eb4de12010-02-12 13:28:40 +0000522
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700523 free(data);
524 free(line);
525 free(ring_name);
Chris Wilson9eb4de12010-02-12 13:28:40 +0000526}
527
528int
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700529main(int argc, char *argv[])
Chris Wilson9eb4de12010-02-12 13:28:40 +0000530{
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700531 FILE *file;
532 const char *path;
533 char *filename = NULL;
534 struct stat st;
535 int error;
Chris Wilson9eb4de12010-02-12 13:28:40 +0000536
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700537 if (argc > 2) {
538 fprintf(stderr,
539 "intel_gpu_decode: Parse an Intel GPU i915_error_state\n"
540 "Usage:\n"
541 "\t%s [<file>]\n"
542 "\n"
543 "With no arguments, debugfs-dri-directory is probed for in "
544 "/debug and \n"
545 "/sys/kernel/debug. Otherwise, it may be "
546 "specified. If a file is given,\n"
547 "it is parsed as an GPU dump in the format of "
548 "/debug/dri/0/i915_error_state.\n",
549 argv[0]);
550 return 1;
551 }
Chris Wilson9eb4de12010-02-12 13:28:40 +0000552
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700553 if (argc == 1) {
554 if (isatty(0)) {
Chris Wilsondbbf2e92013-08-07 23:41:54 +0100555 path = "/sys/class/drm/card0/error";
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700556 error = stat(path, &st);
557 if (error != 0) {
Chris Wilsondbbf2e92013-08-07 23:41:54 +0100558 path = "/debug/dri";
559 error = stat(path, &st);
560 }
561 if (error != 0) {
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700562 path = "/sys/kernel/debug/dri";
563 error = stat(path, &st);
Chris Wilsondbbf2e92013-08-07 23:41:54 +0100564 }
565 if (error != 0) {
566 errx(1,
567 "Couldn't find i915 debugfs directory.\n\n"
568 "Is debugfs mounted? You might try mounting it with a command such as:\n\n"
569 "\tsudo mount -t debugfs debugfs /sys/kernel/debug\n");
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700570 }
571 } else {
572 read_data_file(stdin);
573 exit(0);
Chris Wilsona3a78632010-12-01 21:51:59 +0000574 }
Chris Wilsona3a78632010-12-01 21:51:59 +0000575 } else {
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700576 path = argv[1];
577 error = stat(path, &st);
578 if (error != 0) {
579 fprintf(stderr, "Error opening %s: %s\n",
580 path, strerror(errno));
581 exit(1);
582 }
Chris Wilson9eb4de12010-02-12 13:28:40 +0000583 }
Chris Wilson9eb4de12010-02-12 13:28:40 +0000584
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700585 if (S_ISDIR(st.st_mode)) {
586 int ret;
Imre Deak5efd3d32012-10-10 16:04:39 +0300587
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700588 ret = asprintf(&filename, "%s/i915_error_state", path);
Imre Deak5efd3d32012-10-10 16:04:39 +0300589 assert(ret > 0);
Chris Wilson68a95f02011-01-25 16:34:08 +0000590 file = fopen(filename, "r");
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700591 if (!file) {
592 int minor;
593 for (minor = 0; minor < 64; minor++) {
594 free(filename);
595 ret = asprintf(&filename, "%s/%d/i915_error_state", path, minor);
596 assert(ret > 0);
Chris Wilson9eb4de12010-02-12 13:28:40 +0000597
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700598 file = fopen(filename, "r");
599 if (file)
600 break;
601 }
602 }
603 if (!file) {
604 fprintf(stderr, "Failed to find i915_error_state beneath %s\n",
605 path);
606 exit (1);
607 }
608 } else {
609 file = fopen(path, "r");
610 if (!file) {
611 fprintf(stderr, "Failed to open %s: %s\n",
612 path, strerror(errno));
613 exit (1);
614 }
615 }
Chris Wilson68a95f02011-01-25 16:34:08 +0000616
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700617 read_data_file(file);
618 fclose(file);
Chris Wilsona3a78632010-12-01 21:51:59 +0000619
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700620 if (filename != path)
621 free(filename);
622
623 return 0;
Chris Wilson9eb4de12010-02-12 13:28:40 +0000624}
Ben Widawsky155aa1e2013-04-22 11:07:35 -0700625
626/* vim: set ts=8 sw=8 tw=0 noet :*/