drm/i915/audio: do not mess with audio registers if port is invalid
[deliverable/linux.git] / drivers / gpu / drm / i915 / i915_gpu_error.c
CommitLineData
84734a04
MK
1/*
2 * Copyright (c) 2008 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 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 * Keith Packard <keithp@keithp.com>
26 * Mika Kuoppala <mika.kuoppala@intel.com>
27 *
28 */
29
30#include <generated/utsrelease.h>
31#include "i915_drv.h"
32
33static const char *yesno(int v)
34{
35 return v ? "yes" : "no";
36}
37
38static const char *ring_str(int ring)
39{
40 switch (ring) {
41 case RCS: return "render";
42 case VCS: return "bsd";
43 case BCS: return "blt";
44 case VECS: return "vebox";
845f74a7 45 case VCS2: return "bsd2";
84734a04
MK
46 default: return "";
47 }
48}
49
50static const char *pin_flag(int pinned)
51{
52 if (pinned > 0)
53 return " P";
54 else if (pinned < 0)
55 return " p";
56 else
57 return "";
58}
59
60static const char *tiling_flag(int tiling)
61{
62 switch (tiling) {
63 default:
64 case I915_TILING_NONE: return "";
65 case I915_TILING_X: return " X";
66 case I915_TILING_Y: return " Y";
67 }
68}
69
70static const char *dirty_flag(int dirty)
71{
72 return dirty ? " dirty" : "";
73}
74
75static const char *purgeable_flag(int purgeable)
76{
77 return purgeable ? " purgeable" : "";
78}
79
80static bool __i915_error_ok(struct drm_i915_error_state_buf *e)
81{
82
83 if (!e->err && WARN(e->bytes > (e->size - 1), "overflow")) {
84 e->err = -ENOSPC;
85 return false;
86 }
87
88 if (e->bytes == e->size - 1 || e->err)
89 return false;
90
91 return true;
92}
93
94static bool __i915_error_seek(struct drm_i915_error_state_buf *e,
95 unsigned len)
96{
97 if (e->pos + len <= e->start) {
98 e->pos += len;
99 return false;
100 }
101
102 /* First vsnprintf needs to fit in its entirety for memmove */
103 if (len >= e->size) {
104 e->err = -EIO;
105 return false;
106 }
107
108 return true;
109}
110
111static void __i915_error_advance(struct drm_i915_error_state_buf *e,
112 unsigned len)
113{
114 /* If this is first printf in this window, adjust it so that
115 * start position matches start of the buffer
116 */
117
118 if (e->pos < e->start) {
119 const size_t off = e->start - e->pos;
120
121 /* Should not happen but be paranoid */
122 if (off > len || e->bytes) {
123 e->err = -EIO;
124 return;
125 }
126
127 memmove(e->buf, e->buf + off, len - off);
128 e->bytes = len - off;
129 e->pos = e->start;
130 return;
131 }
132
133 e->bytes += len;
134 e->pos += len;
135}
136
137static void i915_error_vprintf(struct drm_i915_error_state_buf *e,
138 const char *f, va_list args)
139{
140 unsigned len;
141
142 if (!__i915_error_ok(e))
143 return;
144
145 /* Seek the first printf which is hits start position */
146 if (e->pos < e->start) {
e29bb4eb
CW
147 va_list tmp;
148
149 va_copy(tmp, args);
1d2cb9a5
MK
150 len = vsnprintf(NULL, 0, f, tmp);
151 va_end(tmp);
152
153 if (!__i915_error_seek(e, len))
84734a04
MK
154 return;
155 }
156
157 len = vsnprintf(e->buf + e->bytes, e->size - e->bytes, f, args);
158 if (len >= e->size - e->bytes)
159 len = e->size - e->bytes - 1;
160
161 __i915_error_advance(e, len);
162}
163
164static void i915_error_puts(struct drm_i915_error_state_buf *e,
165 const char *str)
166{
167 unsigned len;
168
169 if (!__i915_error_ok(e))
170 return;
171
172 len = strlen(str);
173
174 /* Seek the first printf which is hits start position */
175 if (e->pos < e->start) {
176 if (!__i915_error_seek(e, len))
177 return;
178 }
179
180 if (len >= e->size - e->bytes)
181 len = e->size - e->bytes - 1;
182 memcpy(e->buf + e->bytes, str, len);
183
184 __i915_error_advance(e, len);
185}
186
187#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
188#define err_puts(e, s) i915_error_puts(e, s)
189
190static void print_error_buffers(struct drm_i915_error_state_buf *m,
191 const char *name,
192 struct drm_i915_error_buffer *err,
193 int count)
194{
3a448734 195 err_printf(m, " %s [%d]:\n", name, count);
84734a04
MK
196
197 while (count--) {
3a448734 198 err_printf(m, " %08x %8u %02x %02x %x %x",
84734a04
MK
199 err->gtt_offset,
200 err->size,
201 err->read_domains,
202 err->write_domain,
203 err->rseqno, err->wseqno);
204 err_puts(m, pin_flag(err->pinned));
205 err_puts(m, tiling_flag(err->tiling));
206 err_puts(m, dirty_flag(err->dirty));
207 err_puts(m, purgeable_flag(err->purgeable));
5cc9ed4b 208 err_puts(m, err->userptr ? " userptr" : "");
84734a04
MK
209 err_puts(m, err->ring != -1 ? " " : "");
210 err_puts(m, ring_str(err->ring));
0a4cd7c8 211 err_puts(m, i915_cache_level_str(m->i915, err->cache_level));
84734a04
MK
212
213 if (err->name)
214 err_printf(m, " (name: %d)", err->name);
215 if (err->fence_reg != I915_FENCE_REG_NONE)
216 err_printf(m, " (fence: %d)", err->fence_reg);
217
218 err_puts(m, "\n");
219 err++;
220 }
221}
222
da661464
MK
223static const char *hangcheck_action_to_str(enum intel_ring_hangcheck_action a)
224{
225 switch (a) {
226 case HANGCHECK_IDLE:
227 return "idle";
228 case HANGCHECK_WAIT:
229 return "wait";
230 case HANGCHECK_ACTIVE:
231 return "active";
f260fe7b
MK
232 case HANGCHECK_ACTIVE_LOOP:
233 return "active (loop)";
da661464
MK
234 case HANGCHECK_KICK:
235 return "kick";
236 case HANGCHECK_HUNG:
237 return "hung";
238 }
239
240 return "unknown";
241}
242
84734a04
MK
243static void i915_ring_error_state(struct drm_i915_error_state_buf *m,
244 struct drm_device *dev,
77c1aa84
DV
245 struct drm_i915_error_state *error,
246 int ring_idx)
84734a04 247{
77c1aa84
DV
248 struct drm_i915_error_ring *ring = &error->ring[ring_idx];
249
362b8af7 250 if (!ring->valid)
372fbb8e
CW
251 return;
252
77c1aa84 253 err_printf(m, "%s command stream:\n", ring_str(ring_idx));
94f8cf10
CW
254 err_printf(m, " START: 0x%08x\n", ring->start);
255 err_printf(m, " HEAD: 0x%08x\n", ring->head);
256 err_printf(m, " TAIL: 0x%08x\n", ring->tail);
257 err_printf(m, " CTL: 0x%08x\n", ring->ctl);
258 err_printf(m, " HWS: 0x%08x\n", ring->hws);
e3243d16 259 err_printf(m, " ACTHD: 0x%08x %08x\n", (u32)(ring->acthd>>32), (u32)ring->acthd);
362b8af7
BW
260 err_printf(m, " IPEIR: 0x%08x\n", ring->ipeir);
261 err_printf(m, " IPEHR: 0x%08x\n", ring->ipehr);
262 err_printf(m, " INSTDONE: 0x%08x\n", ring->instdone);
3dda20a9 263 if (INTEL_INFO(dev)->gen >= 4) {
e3243d16 264 err_printf(m, " BBADDR: 0x%08x %08x\n", (u32)(ring->bbaddr>>32), (u32)ring->bbaddr);
362b8af7
BW
265 err_printf(m, " BB_STATE: 0x%08x\n", ring->bbstate);
266 err_printf(m, " INSTPS: 0x%08x\n", ring->instps);
3dda20a9 267 }
362b8af7 268 err_printf(m, " INSTPM: 0x%08x\n", ring->instpm);
13ffadd1
BW
269 err_printf(m, " FADDR: 0x%08x %08x\n", upper_32_bits(ring->faddr),
270 lower_32_bits(ring->faddr));
84734a04 271 if (INTEL_INFO(dev)->gen >= 6) {
362b8af7
BW
272 err_printf(m, " RC PSMI: 0x%08x\n", ring->rc_psmi);
273 err_printf(m, " FAULT_REG: 0x%08x\n", ring->fault_reg);
84734a04 274 err_printf(m, " SYNC_0: 0x%08x [last synced 0x%08x]\n",
362b8af7
BW
275 ring->semaphore_mboxes[0],
276 ring->semaphore_seqno[0]);
84734a04 277 err_printf(m, " SYNC_1: 0x%08x [last synced 0x%08x]\n",
362b8af7
BW
278 ring->semaphore_mboxes[1],
279 ring->semaphore_seqno[1]);
4e5aabfd
BW
280 if (HAS_VEBOX(dev)) {
281 err_printf(m, " SYNC_2: 0x%08x [last synced 0x%08x]\n",
362b8af7
BW
282 ring->semaphore_mboxes[2],
283 ring->semaphore_seqno[2]);
4e5aabfd 284 }
84734a04 285 }
6c7a01ec
BW
286 if (USES_PPGTT(dev)) {
287 err_printf(m, " GFX_MODE: 0x%08x\n", ring->vm_info.gfx_mode);
288
289 if (INTEL_INFO(dev)->gen >= 8) {
290 int i;
291 for (i = 0; i < 4; i++)
292 err_printf(m, " PDP%d: 0x%016llx\n",
293 i, ring->vm_info.pdp[i]);
294 } else {
295 err_printf(m, " PP_DIR_BASE: 0x%08x\n",
296 ring->vm_info.pp_dir_base);
297 }
298 }
362b8af7
BW
299 err_printf(m, " seqno: 0x%08x\n", ring->seqno);
300 err_printf(m, " waiting: %s\n", yesno(ring->waiting));
301 err_printf(m, " ring->head: 0x%08x\n", ring->cpu_ring_head);
302 err_printf(m, " ring->tail: 0x%08x\n", ring->cpu_ring_tail);
da661464 303 err_printf(m, " hangcheck: %s [%d]\n",
362b8af7
BW
304 hangcheck_action_to_str(ring->hangcheck_action),
305 ring->hangcheck_score);
84734a04
MK
306}
307
308void i915_error_printf(struct drm_i915_error_state_buf *e, const char *f, ...)
309{
310 va_list args;
311
312 va_start(args, f);
313 i915_error_vprintf(e, f, args);
314 va_end(args);
315}
316
ab0e7ff9
CW
317static void print_error_obj(struct drm_i915_error_state_buf *m,
318 struct drm_i915_error_object *obj)
319{
320 int page, offset, elt;
321
322 for (page = offset = 0; page < obj->page_count; page++) {
323 for (elt = 0; elt < PAGE_SIZE/4; elt++) {
324 err_printf(m, "%08x : %08x\n", offset,
325 obj->pages[page][elt]);
326 offset += 4;
327 }
328 }
329}
330
84734a04
MK
331int i915_error_state_to_str(struct drm_i915_error_state_buf *m,
332 const struct i915_error_state_file_priv *error_priv)
333{
334 struct drm_device *dev = error_priv->dev;
50227e1c 335 struct drm_i915_private *dev_priv = dev->dev_private;
84734a04 336 struct drm_i915_error_state *error = error_priv->error;
0ca36d78 337 struct drm_i915_error_object *obj;
ab0e7ff9
CW
338 int i, j, offset, elt;
339 int max_hangcheck_score;
84734a04
MK
340
341 if (!error) {
342 err_printf(m, "no error state collected\n");
343 goto out;
344 }
345
cb383002 346 err_printf(m, "%s\n", error->error_msg);
84734a04
MK
347 err_printf(m, "Time: %ld s %ld us\n", error->time.tv_sec,
348 error->time.tv_usec);
349 err_printf(m, "Kernel: " UTS_RELEASE "\n");
ab0e7ff9
CW
350 max_hangcheck_score = 0;
351 for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
352 if (error->ring[i].hangcheck_score > max_hangcheck_score)
353 max_hangcheck_score = error->ring[i].hangcheck_score;
354 }
355 for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
356 if (error->ring[i].hangcheck_score == max_hangcheck_score &&
357 error->ring[i].pid != -1) {
358 err_printf(m, "Active process (on ring %s): %s [%d]\n",
359 ring_str(i),
360 error->ring[i].comm,
361 error->ring[i].pid);
362 }
363 }
48b031e3 364 err_printf(m, "Reset count: %u\n", error->reset_count);
62d5d69b 365 err_printf(m, "Suspend count: %u\n", error->suspend_count);
ffbab09b 366 err_printf(m, "PCI ID: 0x%04x\n", dev->pdev->device);
84734a04
MK
367 err_printf(m, "EIR: 0x%08x\n", error->eir);
368 err_printf(m, "IER: 0x%08x\n", error->ier);
885ea5a8
RV
369 if (INTEL_INFO(dev)->gen >= 8) {
370 for (i = 0; i < 4; i++)
371 err_printf(m, "GTIER gt %d: 0x%08x\n", i,
372 error->gtier[i]);
373 } else if (HAS_PCH_SPLIT(dev) || IS_VALLEYVIEW(dev))
374 err_printf(m, "GTIER: 0x%08x\n", error->gtier[0]);
84734a04
MK
375 err_printf(m, "PGTBL_ER: 0x%08x\n", error->pgtbl_er);
376 err_printf(m, "FORCEWAKE: 0x%08x\n", error->forcewake);
377 err_printf(m, "DERRMR: 0x%08x\n", error->derrmr);
378 err_printf(m, "CCID: 0x%08x\n", error->ccid);
094f9a54 379 err_printf(m, "Missed interrupts: 0x%08lx\n", dev_priv->gpu_error.missed_irq_rings);
84734a04
MK
380
381 for (i = 0; i < dev_priv->num_fence_regs; i++)
382 err_printf(m, " fence[%d] = %08llx\n", i, error->fence[i]);
383
384 for (i = 0; i < ARRAY_SIZE(error->extra_instdone); i++)
385 err_printf(m, " INSTDONE_%d: 0x%08x\n", i,
386 error->extra_instdone[i]);
387
388 if (INTEL_INFO(dev)->gen >= 6) {
389 err_printf(m, "ERROR: 0x%08x\n", error->error);
6c826f34
MK
390
391 if (INTEL_INFO(dev)->gen >= 8)
392 err_printf(m, "FAULT_TLB_DATA: 0x%08x 0x%08x\n",
393 error->fault_data1, error->fault_data0);
394
84734a04
MK
395 err_printf(m, "DONE_REG: 0x%08x\n", error->done_reg);
396 }
397
398 if (INTEL_INFO(dev)->gen == 7)
399 err_printf(m, "ERR_INT: 0x%08x\n", error->err_int);
400
77c1aa84
DV
401 for (i = 0; i < ARRAY_SIZE(error->ring); i++)
402 i915_ring_error_state(m, dev, error, i);
84734a04 403
3a448734
CW
404 for (i = 0; i < error->vm_count; i++) {
405 err_printf(m, "vm[%d]\n", i);
406
84734a04 407 print_error_buffers(m, "Active",
3a448734
CW
408 error->active_bo[i],
409 error->active_bo_count[i]);
84734a04 410
84734a04 411 print_error_buffers(m, "Pinned",
3a448734
CW
412 error->pinned_bo[i],
413 error->pinned_bo_count[i]);
414 }
84734a04
MK
415
416 for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
ab0e7ff9
CW
417 obj = error->ring[i].batchbuffer;
418 if (obj) {
419 err_puts(m, dev_priv->ring[i].name);
420 if (error->ring[i].pid != -1)
421 err_printf(m, " (submitted by %s [%d])",
422 error->ring[i].comm,
423 error->ring[i].pid);
424 err_printf(m, " --- gtt_offset = 0x%08x\n",
84734a04 425 obj->gtt_offset);
ab0e7ff9
CW
426 print_error_obj(m, obj);
427 }
428
429 obj = error->ring[i].wa_batchbuffer;
430 if (obj) {
431 err_printf(m, "%s (w/a) --- gtt_offset = 0x%08x\n",
432 dev_priv->ring[i].name, obj->gtt_offset);
433 print_error_obj(m, obj);
84734a04
MK
434 }
435
436 if (error->ring[i].num_requests) {
437 err_printf(m, "%s --- %d requests\n",
438 dev_priv->ring[i].name,
439 error->ring[i].num_requests);
440 for (j = 0; j < error->ring[i].num_requests; j++) {
441 err_printf(m, " seqno 0x%08x, emitted %ld, tail 0x%08x\n",
442 error->ring[i].requests[j].seqno,
443 error->ring[i].requests[j].jiffies,
444 error->ring[i].requests[j].tail);
445 }
446 }
447
448 if ((obj = error->ring[i].ringbuffer)) {
449 err_printf(m, "%s --- ringbuffer = 0x%08x\n",
450 dev_priv->ring[i].name,
451 obj->gtt_offset);
ab0e7ff9 452 print_error_obj(m, obj);
84734a04
MK
453 }
454
362b8af7 455 if ((obj = error->ring[i].hws_page)) {
f3ce3821
CW
456 err_printf(m, "%s --- HW Status = 0x%08x\n",
457 dev_priv->ring[i].name,
458 obj->gtt_offset);
459 offset = 0;
460 for (elt = 0; elt < PAGE_SIZE/16; elt += 4) {
461 err_printf(m, "[%04x] %08x %08x %08x %08x\n",
462 offset,
463 obj->pages[0][elt],
464 obj->pages[0][elt+1],
465 obj->pages[0][elt+2],
466 obj->pages[0][elt+3]);
467 offset += 16;
468 }
469 }
470
372fbb8e 471 if ((obj = error->ring[i].ctx)) {
84734a04
MK
472 err_printf(m, "%s --- HW Context = 0x%08x\n",
473 dev_priv->ring[i].name,
474 obj->gtt_offset);
17d36749 475 print_error_obj(m, obj);
84734a04
MK
476 }
477 }
478
0ca36d78
BW
479 if ((obj = error->semaphore_obj)) {
480 err_printf(m, "Semaphore page = 0x%08x\n", obj->gtt_offset);
481 for (elt = 0; elt < PAGE_SIZE/16; elt += 4) {
482 err_printf(m, "[%04x] %08x %08x %08x %08x\n",
483 elt * 4,
484 obj->pages[0][elt],
485 obj->pages[0][elt+1],
486 obj->pages[0][elt+2],
487 obj->pages[0][elt+3]);
488 }
489 }
490
84734a04
MK
491 if (error->overlay)
492 intel_overlay_print_error_state(m, error->overlay);
493
494 if (error->display)
495 intel_display_print_error_state(m, dev, error->display);
496
497out:
498 if (m->bytes == 0 && m->err)
499 return m->err;
500
501 return 0;
502}
503
504int i915_error_state_buf_init(struct drm_i915_error_state_buf *ebuf,
0a4cd7c8 505 struct drm_i915_private *i915,
84734a04
MK
506 size_t count, loff_t pos)
507{
508 memset(ebuf, 0, sizeof(*ebuf));
0a4cd7c8 509 ebuf->i915 = i915;
84734a04
MK
510
511 /* We need to have enough room to store any i915_error_state printf
512 * so that we can move it to start position.
513 */
514 ebuf->size = count + 1 > PAGE_SIZE ? count + 1 : PAGE_SIZE;
515 ebuf->buf = kmalloc(ebuf->size,
516 GFP_TEMPORARY | __GFP_NORETRY | __GFP_NOWARN);
517
518 if (ebuf->buf == NULL) {
519 ebuf->size = PAGE_SIZE;
520 ebuf->buf = kmalloc(ebuf->size, GFP_TEMPORARY);
521 }
522
523 if (ebuf->buf == NULL) {
524 ebuf->size = 128;
525 ebuf->buf = kmalloc(ebuf->size, GFP_TEMPORARY);
526 }
527
528 if (ebuf->buf == NULL)
529 return -ENOMEM;
530
531 ebuf->start = pos;
532
533 return 0;
534}
535
536static void i915_error_object_free(struct drm_i915_error_object *obj)
537{
538 int page;
539
540 if (obj == NULL)
541 return;
542
543 for (page = 0; page < obj->page_count; page++)
544 kfree(obj->pages[page]);
545
546 kfree(obj);
547}
548
549static void i915_error_state_free(struct kref *error_ref)
550{
551 struct drm_i915_error_state *error = container_of(error_ref,
552 typeof(*error), ref);
553 int i;
554
555 for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
556 i915_error_object_free(error->ring[i].batchbuffer);
557 i915_error_object_free(error->ring[i].ringbuffer);
362b8af7 558 i915_error_object_free(error->ring[i].hws_page);
84734a04
MK
559 i915_error_object_free(error->ring[i].ctx);
560 kfree(error->ring[i].requests);
561 }
562
0ca36d78 563 i915_error_object_free(error->semaphore_obj);
0b37a9a9
MT
564
565 for (i = 0; i < error->vm_count; i++)
566 kfree(error->active_bo[i]);
567
84734a04 568 kfree(error->active_bo);
0b37a9a9
MT
569 kfree(error->active_bo_count);
570 kfree(error->pinned_bo);
571 kfree(error->pinned_bo_count);
84734a04
MK
572 kfree(error->overlay);
573 kfree(error->display);
574 kfree(error);
575}
576
577static struct drm_i915_error_object *
8ae62dc6
CW
578i915_error_object_create(struct drm_i915_private *dev_priv,
579 struct drm_i915_gem_object *src,
580 struct i915_address_space *vm)
84734a04
MK
581{
582 struct drm_i915_error_object *dst;
aff43766 583 struct i915_vma *vma = NULL;
8ae62dc6 584 int num_pages;
b3c3f5e6
CW
585 bool use_ggtt;
586 int i = 0;
84734a04
MK
587 u32 reloc_offset;
588
589 if (src == NULL || src->pages == NULL)
590 return NULL;
591
8ae62dc6
CW
592 num_pages = src->base.size >> PAGE_SHIFT;
593
84734a04
MK
594 dst = kmalloc(sizeof(*dst) + num_pages * sizeof(u32 *), GFP_ATOMIC);
595 if (dst == NULL)
596 return NULL;
597
87a01e82
CW
598 if (i915_gem_obj_bound(src, vm))
599 dst->gtt_offset = i915_gem_obj_offset(src, vm);
600 else
601 dst->gtt_offset = -1;
b3c3f5e6
CW
602
603 reloc_offset = dst->gtt_offset;
aff43766
TU
604 if (i915_is_ggtt(vm))
605 vma = i915_gem_obj_to_ggtt(src);
b3c3f5e6 606 use_ggtt = (src->cache_level == I915_CACHE_NONE &&
aff43766
TU
607 vma && (vma->bound & GLOBAL_BIND) &&
608 reloc_offset + num_pages * PAGE_SIZE <= dev_priv->gtt.mappable_end);
b3c3f5e6
CW
609
610 /* Cannot access stolen address directly, try to use the aperture */
611 if (src->stolen) {
612 use_ggtt = true;
613
aff43766 614 if (!(vma && vma->bound & GLOBAL_BIND))
b3c3f5e6
CW
615 goto unwind;
616
617 reloc_offset = i915_gem_obj_ggtt_offset(src);
618 if (reloc_offset + num_pages * PAGE_SIZE > dev_priv->gtt.mappable_end)
619 goto unwind;
620 }
621
622 /* Cannot access snooped pages through the aperture */
623 if (use_ggtt && src->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv->dev))
624 goto unwind;
625
626 dst->page_count = num_pages;
627 while (num_pages--) {
84734a04
MK
628 unsigned long flags;
629 void *d;
630
631 d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
632 if (d == NULL)
633 goto unwind;
634
635 local_irq_save(flags);
b3c3f5e6 636 if (use_ggtt) {
84734a04
MK
637 void __iomem *s;
638
639 /* Simply ignore tiling or any overlapping fence.
640 * It's part of the error state, and this hopefully
641 * captures what the GPU read.
642 */
643
644 s = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
645 reloc_offset);
646 memcpy_fromio(d, s, PAGE_SIZE);
647 io_mapping_unmap_atomic(s);
84734a04
MK
648 } else {
649 struct page *page;
650 void *s;
651
652 page = i915_gem_object_get_page(src, i);
653
654 drm_clflush_pages(&page, 1);
655
656 s = kmap_atomic(page);
657 memcpy(d, s, PAGE_SIZE);
658 kunmap_atomic(s);
659
660 drm_clflush_pages(&page, 1);
661 }
662 local_irq_restore(flags);
663
b3c3f5e6 664 dst->pages[i++] = d;
84734a04
MK
665 reloc_offset += PAGE_SIZE;
666 }
84734a04
MK
667
668 return dst;
669
670unwind:
671 while (i--)
672 kfree(dst->pages[i]);
673 kfree(dst);
674 return NULL;
675}
a7b91078 676#define i915_error_ggtt_object_create(dev_priv, src) \
8ae62dc6 677 i915_error_object_create((dev_priv), (src), &(dev_priv)->gtt.base)
84734a04
MK
678
679static void capture_bo(struct drm_i915_error_buffer *err,
3a448734 680 struct i915_vma *vma)
84734a04 681{
3a448734
CW
682 struct drm_i915_gem_object *obj = vma->obj;
683
84734a04
MK
684 err->size = obj->base.size;
685 err->name = obj->base.name;
97b2a6a1
JH
686 err->rseqno = i915_gem_request_get_seqno(obj->last_read_req);
687 err->wseqno = i915_gem_request_get_seqno(obj->last_write_req);
3a448734 688 err->gtt_offset = vma->node.start;
84734a04
MK
689 err->read_domains = obj->base.read_domains;
690 err->write_domain = obj->base.write_domain;
691 err->fence_reg = obj->fence_reg;
692 err->pinned = 0;
d7f46fc4 693 if (i915_gem_obj_is_pinned(obj))
84734a04 694 err->pinned = 1;
84734a04
MK
695 err->tiling = obj->tiling_mode;
696 err->dirty = obj->dirty;
697 err->purgeable = obj->madv != I915_MADV_WILLNEED;
5cc9ed4b 698 err->userptr = obj->userptr.mm != NULL;
41c52415
JH
699 err->ring = obj->last_read_req ?
700 i915_gem_request_get_ring(obj->last_read_req)->id : -1;
84734a04
MK
701 err->cache_level = obj->cache_level;
702}
703
704static u32 capture_active_bo(struct drm_i915_error_buffer *err,
705 int count, struct list_head *head)
706{
ca191b13 707 struct i915_vma *vma;
84734a04
MK
708 int i = 0;
709
ca191b13 710 list_for_each_entry(vma, head, mm_list) {
3a448734 711 capture_bo(err++, vma);
84734a04
MK
712 if (++i == count)
713 break;
714 }
715
716 return i;
717}
718
719static u32 capture_pinned_bo(struct drm_i915_error_buffer *err,
3a448734
CW
720 int count, struct list_head *head,
721 struct i915_address_space *vm)
84734a04
MK
722{
723 struct drm_i915_gem_object *obj;
3a448734
CW
724 struct drm_i915_error_buffer * const first = err;
725 struct drm_i915_error_buffer * const last = err + count;
84734a04
MK
726
727 list_for_each_entry(obj, head, global_list) {
3a448734 728 struct i915_vma *vma;
84734a04 729
3a448734 730 if (err == last)
84734a04 731 break;
3a448734
CW
732
733 list_for_each_entry(vma, &obj->vma_list, vma_link)
fe14d5f4 734 if (vma->vm == vm && vma->pin_count > 0)
3a448734 735 capture_bo(err++, vma);
84734a04
MK
736 }
737
3a448734 738 return err - first;
84734a04
MK
739}
740
011cf577
BW
741/* Generate a semi-unique error code. The code is not meant to have meaning, The
742 * code's only purpose is to try to prevent false duplicated bug reports by
743 * grossly estimating a GPU error state.
744 *
745 * TODO Ideally, hashing the batchbuffer would be a very nice way to determine
746 * the hang if we could strip the GTT offset information from it.
747 *
748 * It's only a small step better than a random number in its current form.
749 */
750static uint32_t i915_error_generate_code(struct drm_i915_private *dev_priv,
cb383002
MK
751 struct drm_i915_error_state *error,
752 int *ring_id)
011cf577
BW
753{
754 uint32_t error_code = 0;
755 int i;
756
757 /* IPEHR would be an ideal way to detect errors, as it's the gross
758 * measure of "the command that hung." However, has some very common
759 * synchronization commands which almost always appear in the case
760 * strictly a client bug. Use instdone to differentiate those some.
761 */
cb383002
MK
762 for (i = 0; i < I915_NUM_RINGS; i++) {
763 if (error->ring[i].hangcheck_action == HANGCHECK_HUNG) {
764 if (ring_id)
765 *ring_id = i;
766
011cf577 767 return error->ring[i].ipehr ^ error->ring[i].instdone;
cb383002
MK
768 }
769 }
011cf577
BW
770
771 return error_code;
772}
773
84734a04
MK
774static void i915_gem_record_fences(struct drm_device *dev,
775 struct drm_i915_error_state *error)
776{
777 struct drm_i915_private *dev_priv = dev->dev_private;
778 int i;
779
ce38ab05 780 if (IS_GEN3(dev) || IS_GEN2(dev)) {
84734a04
MK
781 for (i = 0; i < 8; i++)
782 error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
ce38ab05
RV
783 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
784 for (i = 0; i < 8; i++)
785 error->fence[i+8] = I915_READ(FENCE_REG_945_8 +
786 (i * 4));
787 } else if (IS_GEN5(dev) || IS_GEN4(dev))
788 for (i = 0; i < 16; i++)
789 error->fence[i] = I915_READ64(FENCE_REG_965_0 +
790 (i * 8));
791 else if (INTEL_INFO(dev)->gen >= 6)
792 for (i = 0; i < dev_priv->num_fence_regs; i++)
793 error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 +
794 (i * 8));
84734a04
MK
795}
796
87f85ebc 797
0ca36d78
BW
798static void gen8_record_semaphore_state(struct drm_i915_private *dev_priv,
799 struct drm_i915_error_state *error,
800 struct intel_engine_cs *ring,
801 struct drm_i915_error_ring *ering)
802{
b4558b46 803 struct intel_engine_cs *to;
0ca36d78
BW
804 int i;
805
806 if (!i915_semaphore_is_enabled(dev_priv->dev))
807 return;
808
809 if (!error->semaphore_obj)
810 error->semaphore_obj =
cc1df8a3
DV
811 i915_error_ggtt_object_create(dev_priv,
812 dev_priv->semaphore_obj);
0ca36d78 813
b4558b46
RV
814 for_each_ring(to, dev_priv, i) {
815 int idx;
816 u16 signal_offset;
817 u32 *tmp;
0ca36d78 818
b4558b46
RV
819 if (ring == to)
820 continue;
821
864c6181
RV
822 signal_offset = (GEN8_SIGNAL_OFFSET(ring, i) & (PAGE_SIZE - 1))
823 / 4;
b4558b46
RV
824 tmp = error->semaphore_obj->pages[0];
825 idx = intel_ring_sync_index(ring, to);
826
827 ering->semaphore_mboxes[idx] = tmp[signal_offset];
828 ering->semaphore_seqno[idx] = ring->semaphore.sync_seqno[idx];
0ca36d78
BW
829 }
830}
831
87f85ebc
BW
832static void gen6_record_semaphore_state(struct drm_i915_private *dev_priv,
833 struct intel_engine_cs *ring,
834 struct drm_i915_error_ring *ering)
835{
836 ering->semaphore_mboxes[0] = I915_READ(RING_SYNC_0(ring->mmio_base));
837 ering->semaphore_mboxes[1] = I915_READ(RING_SYNC_1(ring->mmio_base));
838 ering->semaphore_seqno[0] = ring->semaphore.sync_seqno[0];
839 ering->semaphore_seqno[1] = ring->semaphore.sync_seqno[1];
840
841 if (HAS_VEBOX(dev_priv->dev)) {
842 ering->semaphore_mboxes[2] =
843 I915_READ(RING_SYNC_2(ring->mmio_base));
844 ering->semaphore_seqno[2] = ring->semaphore.sync_seqno[2];
845 }
846}
847
84734a04 848static void i915_record_ring_state(struct drm_device *dev,
0ca36d78 849 struct drm_i915_error_state *error,
a4872ba6 850 struct intel_engine_cs *ring,
362b8af7 851 struct drm_i915_error_ring *ering)
84734a04
MK
852{
853 struct drm_i915_private *dev_priv = dev->dev_private;
854
855 if (INTEL_INFO(dev)->gen >= 6) {
362b8af7
BW
856 ering->rc_psmi = I915_READ(ring->mmio_base + 0x50);
857 ering->fault_reg = I915_READ(RING_FAULT_REG(ring));
0ca36d78
BW
858 if (INTEL_INFO(dev)->gen >= 8)
859 gen8_record_semaphore_state(dev_priv, error, ring, ering);
860 else
861 gen6_record_semaphore_state(dev_priv, ring, ering);
4e5aabfd
BW
862 }
863
84734a04 864 if (INTEL_INFO(dev)->gen >= 4) {
362b8af7
BW
865 ering->faddr = I915_READ(RING_DMA_FADD(ring->mmio_base));
866 ering->ipeir = I915_READ(RING_IPEIR(ring->mmio_base));
867 ering->ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
868 ering->instdone = I915_READ(RING_INSTDONE(ring->mmio_base));
869 ering->instps = I915_READ(RING_INSTPS(ring->mmio_base));
870 ering->bbaddr = I915_READ(RING_BBADDR(ring->mmio_base));
13ffadd1
BW
871 if (INTEL_INFO(dev)->gen >= 8) {
872 ering->faddr |= (u64) I915_READ(RING_DMA_FADD_UDW(ring->mmio_base)) << 32;
362b8af7 873 ering->bbaddr |= (u64) I915_READ(RING_BBADDR_UDW(ring->mmio_base)) << 32;
13ffadd1 874 }
362b8af7 875 ering->bbstate = I915_READ(RING_BBSTATE(ring->mmio_base));
84734a04 876 } else {
362b8af7
BW
877 ering->faddr = I915_READ(DMA_FADD_I8XX);
878 ering->ipeir = I915_READ(IPEIR);
879 ering->ipehr = I915_READ(IPEHR);
880 ering->instdone = I915_READ(INSTDONE);
84734a04
MK
881 }
882
362b8af7
BW
883 ering->waiting = waitqueue_active(&ring->irq_queue);
884 ering->instpm = I915_READ(RING_INSTPM(ring->mmio_base));
885 ering->seqno = ring->get_seqno(ring, false);
886 ering->acthd = intel_ring_get_active_head(ring);
94f8cf10 887 ering->start = I915_READ_START(ring);
362b8af7
BW
888 ering->head = I915_READ_HEAD(ring);
889 ering->tail = I915_READ_TAIL(ring);
890 ering->ctl = I915_READ_CTL(ring);
84734a04 891
f3ce3821
CW
892 if (I915_NEED_GFX_HWS(dev)) {
893 int mmio;
894
895 if (IS_GEN7(dev)) {
896 switch (ring->id) {
897 default:
898 case RCS:
899 mmio = RENDER_HWS_PGA_GEN7;
900 break;
901 case BCS:
902 mmio = BLT_HWS_PGA_GEN7;
903 break;
904 case VCS:
905 mmio = BSD_HWS_PGA_GEN7;
906 break;
907 case VECS:
908 mmio = VEBOX_HWS_PGA_GEN7;
909 break;
910 }
911 } else if (IS_GEN6(ring->dev)) {
912 mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
913 } else {
914 /* XXX: gen8 returns to sanity */
915 mmio = RING_HWS_PGA(ring->mmio_base);
916 }
917
362b8af7 918 ering->hws = I915_READ(mmio);
f3ce3821
CW
919 }
920
362b8af7
BW
921 ering->hangcheck_score = ring->hangcheck.score;
922 ering->hangcheck_action = ring->hangcheck.action;
6c7a01ec
BW
923
924 if (USES_PPGTT(dev)) {
925 int i;
926
927 ering->vm_info.gfx_mode = I915_READ(RING_MODE_GEN7(ring));
928
74745b09
RV
929 if (IS_GEN6(dev))
930 ering->vm_info.pp_dir_base =
931 I915_READ(RING_PP_DIR_BASE_READ(ring));
932 else if (IS_GEN7(dev))
933 ering->vm_info.pp_dir_base =
934 I915_READ(RING_PP_DIR_BASE(ring));
935 else if (INTEL_INFO(dev)->gen >= 8)
6c7a01ec
BW
936 for (i = 0; i < 4; i++) {
937 ering->vm_info.pdp[i] =
938 I915_READ(GEN8_RING_PDP_UDW(ring, i));
939 ering->vm_info.pdp[i] <<= 32;
940 ering->vm_info.pdp[i] |=
941 I915_READ(GEN8_RING_PDP_LDW(ring, i));
942 }
6c7a01ec 943 }
84734a04
MK
944}
945
946
a4872ba6 947static void i915_gem_record_active_context(struct intel_engine_cs *ring,
84734a04
MK
948 struct drm_i915_error_state *error,
949 struct drm_i915_error_ring *ering)
950{
951 struct drm_i915_private *dev_priv = ring->dev->dev_private;
952 struct drm_i915_gem_object *obj;
953
954 /* Currently render ring is the only HW context user */
955 if (ring->id != RCS || !error->ccid)
956 return;
957
958 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
36362ad3
BW
959 if (!i915_gem_obj_ggtt_bound(obj))
960 continue;
961
84734a04 962 if ((error->ccid & PAGE_MASK) == i915_gem_obj_ggtt_offset(obj)) {
17d36749 963 ering->ctx = i915_error_ggtt_object_create(dev_priv, obj);
84734a04
MK
964 break;
965 }
966 }
967}
968
969static void i915_gem_record_rings(struct drm_device *dev,
970 struct drm_i915_error_state *error)
971{
972 struct drm_i915_private *dev_priv = dev->dev_private;
84734a04
MK
973 struct drm_i915_gem_request *request;
974 int i, count;
975
372fbb8e 976 for (i = 0; i < I915_NUM_RINGS; i++) {
a4872ba6 977 struct intel_engine_cs *ring = &dev_priv->ring[i];
9075e52f 978 struct intel_ringbuffer *rbuf;
372fbb8e 979
eee73b46
CW
980 error->ring[i].pid = -1;
981
372fbb8e
CW
982 if (ring->dev == NULL)
983 continue;
984
985 error->ring[i].valid = true;
986
0ca36d78 987 i915_record_ring_state(dev, error, ring, &error->ring[i]);
84734a04 988
ab0e7ff9
CW
989 request = i915_gem_find_active_request(ring);
990 if (request) {
ae6c4806
DV
991 struct i915_address_space *vm;
992
993 vm = request->ctx && request->ctx->ppgtt ?
994 &request->ctx->ppgtt->base :
995 &dev_priv->gtt.base;
996
ab0e7ff9
CW
997 /* We need to copy these to an anonymous buffer
998 * as the simplest method to avoid being overwritten
999 * by userspace.
1000 */
1001 error->ring[i].batchbuffer =
1002 i915_error_object_create(dev_priv,
1003 request->batch_obj,
ae6c4806 1004 vm);
ab0e7ff9 1005
8ae62dc6 1006 if (HAS_BROKEN_CS_TLB(dev_priv->dev))
ab0e7ff9
CW
1007 error->ring[i].wa_batchbuffer =
1008 i915_error_ggtt_object_create(dev_priv,
1009 ring->scratch.obj);
1010
071c92de 1011 if (request->pid) {
ab0e7ff9
CW
1012 struct task_struct *task;
1013
1014 rcu_read_lock();
071c92de 1015 task = pid_task(request->pid, PIDTYPE_PID);
ab0e7ff9
CW
1016 if (task) {
1017 strcpy(error->ring[i].comm, task->comm);
1018 error->ring[i].pid = task->pid;
1019 }
1020 rcu_read_unlock();
1021 }
1022 }
84734a04 1023
9075e52f
OM
1024 if (i915.enable_execlists) {
1025 /* TODO: This is only a small fix to keep basic error
1026 * capture working, but we need to add more information
1027 * for it to be useful (e.g. dump the context being
1028 * executed).
1029 */
1030 if (request)
1031 rbuf = request->ctx->engine[ring->id].ringbuf;
1032 else
1033 rbuf = ring->default_context->engine[ring->id].ringbuf;
1034 } else
1035 rbuf = ring->buffer;
1036
1037 error->ring[i].cpu_ring_head = rbuf->head;
1038 error->ring[i].cpu_ring_tail = rbuf->tail;
1039
84734a04 1040 error->ring[i].ringbuffer =
9075e52f 1041 i915_error_ggtt_object_create(dev_priv, rbuf->obj);
84734a04 1042
8ae62dc6
CW
1043 error->ring[i].hws_page =
1044 i915_error_ggtt_object_create(dev_priv, ring->status_page.obj);
84734a04
MK
1045
1046 i915_gem_record_active_context(ring, error, &error->ring[i]);
1047
1048 count = 0;
1049 list_for_each_entry(request, &ring->request_list, list)
1050 count++;
1051
1052 error->ring[i].num_requests = count;
1053 error->ring[i].requests =
a1e22653 1054 kcalloc(count, sizeof(*error->ring[i].requests),
84734a04
MK
1055 GFP_ATOMIC);
1056 if (error->ring[i].requests == NULL) {
1057 error->ring[i].num_requests = 0;
1058 continue;
1059 }
1060
1061 count = 0;
1062 list_for_each_entry(request, &ring->request_list, list) {
1063 struct drm_i915_error_request *erq;
1064
1065 erq = &error->ring[i].requests[count++];
1066 erq->seqno = request->seqno;
1067 erq->jiffies = request->emitted_jiffies;
72f95afa 1068 erq->tail = request->postfix;
84734a04
MK
1069 }
1070 }
1071}
1072
95f5301d
BW
1073/* FIXME: Since pin count/bound list is global, we duplicate what we capture per
1074 * VM.
1075 */
1076static void i915_gem_capture_vm(struct drm_i915_private *dev_priv,
1077 struct drm_i915_error_state *error,
1078 struct i915_address_space *vm,
1079 const int ndx)
84734a04 1080{
95f5301d 1081 struct drm_i915_error_buffer *active_bo = NULL, *pinned_bo = NULL;
84734a04 1082 struct drm_i915_gem_object *obj;
95f5301d 1083 struct i915_vma *vma;
84734a04
MK
1084 int i;
1085
1086 i = 0;
ca191b13 1087 list_for_each_entry(vma, &vm->active_list, mm_list)
84734a04 1088 i++;
95f5301d 1089 error->active_bo_count[ndx] = i;
3a448734
CW
1090
1091 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
1092 list_for_each_entry(vma, &obj->vma_list, vma_link)
fe14d5f4 1093 if (vma->vm == vm && vma->pin_count > 0)
3a448734 1094 i++;
3a448734 1095 }
95f5301d 1096 error->pinned_bo_count[ndx] = i - error->active_bo_count[ndx];
84734a04
MK
1097
1098 if (i) {
a1e22653 1099 active_bo = kcalloc(i, sizeof(*active_bo), GFP_ATOMIC);
95f5301d
BW
1100 if (active_bo)
1101 pinned_bo = active_bo + error->active_bo_count[ndx];
84734a04
MK
1102 }
1103
95f5301d
BW
1104 if (active_bo)
1105 error->active_bo_count[ndx] =
1106 capture_active_bo(active_bo,
1107 error->active_bo_count[ndx],
5cef07e1 1108 &vm->active_list);
84734a04 1109
95f5301d
BW
1110 if (pinned_bo)
1111 error->pinned_bo_count[ndx] =
1112 capture_pinned_bo(pinned_bo,
1113 error->pinned_bo_count[ndx],
3a448734 1114 &dev_priv->mm.bound_list, vm);
95f5301d
BW
1115 error->active_bo[ndx] = active_bo;
1116 error->pinned_bo[ndx] = pinned_bo;
1117}
1118
1119static void i915_gem_capture_buffers(struct drm_i915_private *dev_priv,
1120 struct drm_i915_error_state *error)
1121{
1122 struct i915_address_space *vm;
1123 int cnt = 0, i = 0;
1124
1125 list_for_each_entry(vm, &dev_priv->vm_list, global_link)
1126 cnt++;
1127
95f5301d
BW
1128 error->active_bo = kcalloc(cnt, sizeof(*error->active_bo), GFP_ATOMIC);
1129 error->pinned_bo = kcalloc(cnt, sizeof(*error->pinned_bo), GFP_ATOMIC);
1130 error->active_bo_count = kcalloc(cnt, sizeof(*error->active_bo_count),
1131 GFP_ATOMIC);
1132 error->pinned_bo_count = kcalloc(cnt, sizeof(*error->pinned_bo_count),
1133 GFP_ATOMIC);
1134
3a448734
CW
1135 if (error->active_bo == NULL ||
1136 error->pinned_bo == NULL ||
1137 error->active_bo_count == NULL ||
1138 error->pinned_bo_count == NULL) {
1139 kfree(error->active_bo);
1140 kfree(error->active_bo_count);
1141 kfree(error->pinned_bo);
1142 kfree(error->pinned_bo_count);
1143
1144 error->active_bo = NULL;
1145 error->active_bo_count = NULL;
1146 error->pinned_bo = NULL;
1147 error->pinned_bo_count = NULL;
1148 } else {
1149 list_for_each_entry(vm, &dev_priv->vm_list, global_link)
1150 i915_gem_capture_vm(dev_priv, error, vm, i++);
1151
1152 error->vm_count = cnt;
1153 }
84734a04
MK
1154}
1155
1d762aad
BW
1156/* Capture all registers which don't fit into another category. */
1157static void i915_capture_reg_state(struct drm_i915_private *dev_priv,
1158 struct drm_i915_error_state *error)
84734a04 1159{
1d762aad 1160 struct drm_device *dev = dev_priv->dev;
885ea5a8 1161 int i;
84734a04 1162
654c90c6
BW
1163 /* General organization
1164 * 1. Registers specific to a single generation
1165 * 2. Registers which belong to multiple generations
1166 * 3. Feature specific registers.
1167 * 4. Everything else
1168 * Please try to follow the order.
1169 */
84734a04 1170
654c90c6
BW
1171 /* 1: Registers specific to a single generation */
1172 if (IS_VALLEYVIEW(dev)) {
885ea5a8 1173 error->gtier[0] = I915_READ(GTIER);
843db716 1174 error->ier = I915_READ(VLV_IER);
654c90c6
BW
1175 error->forcewake = I915_READ(FORCEWAKE_VLV);
1176 }
84734a04 1177
654c90c6
BW
1178 if (IS_GEN7(dev))
1179 error->err_int = I915_READ(GEN7_ERR_INT);
84734a04 1180
6c826f34
MK
1181 if (INTEL_INFO(dev)->gen >= 8) {
1182 error->fault_data0 = I915_READ(GEN8_FAULT_TLB_DATA0);
1183 error->fault_data1 = I915_READ(GEN8_FAULT_TLB_DATA1);
1184 }
1185
91ec5d11 1186 if (IS_GEN6(dev)) {
84734a04 1187 error->forcewake = I915_READ(FORCEWAKE);
91ec5d11
BW
1188 error->gab_ctl = I915_READ(GAB_CTL);
1189 error->gfx_mode = I915_READ(GFX_MODE);
1190 }
84734a04 1191
654c90c6
BW
1192 /* 2: Registers which belong to multiple generations */
1193 if (INTEL_INFO(dev)->gen >= 7)
1194 error->forcewake = I915_READ(FORCEWAKE_MT);
84734a04
MK
1195
1196 if (INTEL_INFO(dev)->gen >= 6) {
654c90c6 1197 error->derrmr = I915_READ(DERRMR);
84734a04
MK
1198 error->error = I915_READ(ERROR_GEN6);
1199 error->done_reg = I915_READ(DONE_REG);
1200 }
1201
654c90c6 1202 /* 3: Feature specific registers */
91ec5d11
BW
1203 if (IS_GEN6(dev) || IS_GEN7(dev)) {
1204 error->gam_ecochk = I915_READ(GAM_ECOCHK);
1205 error->gac_eco = I915_READ(GAC_ECO_BITS);
1206 }
1207
1208 /* 4: Everything else */
654c90c6
BW
1209 if (HAS_HW_CONTEXTS(dev))
1210 error->ccid = I915_READ(CCID);
1211
885ea5a8
RV
1212 if (INTEL_INFO(dev)->gen >= 8) {
1213 error->ier = I915_READ(GEN8_DE_MISC_IER);
1214 for (i = 0; i < 4; i++)
1215 error->gtier[i] = I915_READ(GEN8_GT_IER(i));
1216 } else if (HAS_PCH_SPLIT(dev)) {
843db716 1217 error->ier = I915_READ(DEIER);
885ea5a8 1218 error->gtier[0] = I915_READ(GTIER);
843db716
RV
1219 } else if (IS_GEN2(dev)) {
1220 error->ier = I915_READ16(IER);
1221 } else if (!IS_VALLEYVIEW(dev)) {
1222 error->ier = I915_READ(IER);
654c90c6 1223 }
654c90c6
BW
1224 error->eir = I915_READ(EIR);
1225 error->pgtbl_er = I915_READ(PGTBL_ER);
84734a04
MK
1226
1227 i915_get_extra_instdone(dev, error->extra_instdone);
1d762aad
BW
1228}
1229
cb383002 1230static void i915_error_capture_msg(struct drm_device *dev,
58174462
MK
1231 struct drm_i915_error_state *error,
1232 bool wedged,
1233 const char *error_msg)
cb383002
MK
1234{
1235 struct drm_i915_private *dev_priv = dev->dev_private;
1236 u32 ecode;
58174462 1237 int ring_id = -1, len;
cb383002
MK
1238
1239 ecode = i915_error_generate_code(dev_priv, error, &ring_id);
1240
58174462 1241 len = scnprintf(error->error_msg, sizeof(error->error_msg),
0b5492d6
MK
1242 "GPU HANG: ecode %d:%d:0x%08x",
1243 INTEL_INFO(dev)->gen, ring_id, ecode);
58174462
MK
1244
1245 if (ring_id != -1 && error->ring[ring_id].pid != -1)
1246 len += scnprintf(error->error_msg + len,
1247 sizeof(error->error_msg) - len,
1248 ", in %s [%d]",
1249 error->ring[ring_id].comm,
1250 error->ring[ring_id].pid);
1251
1252 scnprintf(error->error_msg + len, sizeof(error->error_msg) - len,
1253 ", reason: %s, action: %s",
1254 error_msg,
1255 wedged ? "reset" : "continue");
cb383002
MK
1256}
1257
48b031e3
MK
1258static void i915_capture_gen_state(struct drm_i915_private *dev_priv,
1259 struct drm_i915_error_state *error)
1260{
1261 error->reset_count = i915_reset_count(&dev_priv->gpu_error);
62d5d69b 1262 error->suspend_count = dev_priv->suspend_count;
48b031e3
MK
1263}
1264
1d762aad
BW
1265/**
1266 * i915_capture_error_state - capture an error record for later analysis
1267 * @dev: drm device
1268 *
1269 * Should be called when an error is detected (either a hang or an error
1270 * interrupt) to capture error state from the time of the error. Fills
1271 * out a structure which becomes available in debugfs for user level tools
1272 * to pick up.
1273 */
58174462
MK
1274void i915_capture_error_state(struct drm_device *dev, bool wedged,
1275 const char *error_msg)
1d762aad 1276{
53a4c6b2 1277 static bool warned;
1d762aad
BW
1278 struct drm_i915_private *dev_priv = dev->dev_private;
1279 struct drm_i915_error_state *error;
1280 unsigned long flags;
1d762aad
BW
1281
1282 /* Account for pipe specific data like PIPE*STAT */
1283 error = kzalloc(sizeof(*error), GFP_ATOMIC);
1284 if (!error) {
1285 DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
1286 return;
1287 }
1288
011cf577
BW
1289 kref_init(&error->ref);
1290
48b031e3 1291 i915_capture_gen_state(dev_priv, error);
011cf577
BW
1292 i915_capture_reg_state(dev_priv, error);
1293 i915_gem_capture_buffers(dev_priv, error);
1294 i915_gem_record_fences(dev, error);
1295 i915_gem_record_rings(dev, error);
1d762aad 1296
84734a04
MK
1297 do_gettimeofday(&error->time);
1298
1299 error->overlay = intel_overlay_capture_error_state(dev);
1300 error->display = intel_display_capture_error_state(dev);
1301
58174462 1302 i915_error_capture_msg(dev, error, wedged, error_msg);
cb383002
MK
1303 DRM_INFO("%s\n", error->error_msg);
1304
84734a04
MK
1305 spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
1306 if (dev_priv->gpu_error.first_error == NULL) {
1307 dev_priv->gpu_error.first_error = error;
1308 error = NULL;
1309 }
1310 spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
1311
cb383002 1312 if (error) {
84734a04 1313 i915_error_state_free(&error->ref);
cb383002
MK
1314 return;
1315 }
1316
1317 if (!warned) {
1318 DRM_INFO("GPU hangs can indicate a bug anywhere in the entire gfx stack, including userspace.\n");
1319 DRM_INFO("Please file a _new_ bug report on bugs.freedesktop.org against DRI -> DRM/Intel\n");
1320 DRM_INFO("drm/i915 developers can then reassign to the right component if it's not a kernel issue.\n");
1321 DRM_INFO("The gpu crash dump is required to analyze gpu hangs, so please always attach it.\n");
1322 DRM_INFO("GPU crash dump saved to /sys/class/drm/card%d/error\n", dev->primary->index);
1323 warned = true;
1324 }
84734a04
MK
1325}
1326
1327void i915_error_state_get(struct drm_device *dev,
1328 struct i915_error_state_file_priv *error_priv)
1329{
1330 struct drm_i915_private *dev_priv = dev->dev_private;
84734a04 1331
5b254c59 1332 spin_lock_irq(&dev_priv->gpu_error.lock);
84734a04
MK
1333 error_priv->error = dev_priv->gpu_error.first_error;
1334 if (error_priv->error)
1335 kref_get(&error_priv->error->ref);
5b254c59 1336 spin_unlock_irq(&dev_priv->gpu_error.lock);
84734a04
MK
1337
1338}
1339
1340void i915_error_state_put(struct i915_error_state_file_priv *error_priv)
1341{
1342 if (error_priv->error)
1343 kref_put(&error_priv->error->ref, i915_error_state_free);
1344}
1345
1346void i915_destroy_error_state(struct drm_device *dev)
1347{
1348 struct drm_i915_private *dev_priv = dev->dev_private;
1349 struct drm_i915_error_state *error;
84734a04 1350
5b254c59 1351 spin_lock_irq(&dev_priv->gpu_error.lock);
84734a04
MK
1352 error = dev_priv->gpu_error.first_error;
1353 dev_priv->gpu_error.first_error = NULL;
5b254c59 1354 spin_unlock_irq(&dev_priv->gpu_error.lock);
84734a04
MK
1355
1356 if (error)
1357 kref_put(&error->ref, i915_error_state_free);
1358}
1359
0a4cd7c8 1360const char *i915_cache_level_str(struct drm_i915_private *i915, int type)
84734a04
MK
1361{
1362 switch (type) {
1363 case I915_CACHE_NONE: return " uncached";
0a4cd7c8 1364 case I915_CACHE_LLC: return HAS_LLC(i915) ? " LLC" : " snooped";
350ec881 1365 case I915_CACHE_L3_LLC: return " L3+LLC";
f56383cb 1366 case I915_CACHE_WT: return " WT";
84734a04
MK
1367 default: return "";
1368 }
1369}
1370
1371/* NB: please notice the memset */
1372void i915_get_extra_instdone(struct drm_device *dev, uint32_t *instdone)
1373{
1374 struct drm_i915_private *dev_priv = dev->dev_private;
1375 memset(instdone, 0, sizeof(*instdone) * I915_NUM_INSTDONE_REG);
1376
563f94f6 1377 if (IS_GEN2(dev) || IS_GEN3(dev))
84734a04 1378 instdone[0] = I915_READ(INSTDONE);
563f94f6 1379 else if (IS_GEN4(dev) || IS_GEN5(dev) || IS_GEN6(dev)) {
84734a04
MK
1380 instdone[0] = I915_READ(INSTDONE_I965);
1381 instdone[1] = I915_READ(INSTDONE1);
563f94f6 1382 } else if (INTEL_INFO(dev)->gen >= 7) {
84734a04
MK
1383 instdone[0] = I915_READ(GEN7_INSTDONE_1);
1384 instdone[1] = I915_READ(GEN7_SC_INSTDONE);
1385 instdone[2] = I915_READ(GEN7_SAMPLER_INSTDONE);
1386 instdone[3] = I915_READ(GEN7_ROW_INSTDONE);
84734a04
MK
1387 }
1388}
This page took 0.212391 seconds and 5 git commands to generate.