blob: 33f8a421c67b392603a1d26b36febcceff644fd8 [file] [log] [blame]
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001/* Copyright (c) 2012, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include "kgsl.h"
14#include "kgsl_sharedmem.h"
15#include "kgsl_snapshot.h"
16
17#include "adreno.h"
18#include "adreno_pm4types.h"
19#include "a2xx_reg.h"
20
21/* Number of dwords of ringbuffer history to record */
22#define NUM_DWORDS_OF_RINGBUFFER_HISTORY 100
23
24/* Maintain a list of the objects we see during parsing */
25
26#define SNAPSHOT_OBJ_BUFSIZE 64
27
28#define SNAPSHOT_OBJ_TYPE_IB 0
29
Jordan Crouse9610b6b2012-03-16 14:53:42 -060030/* Keep track of how many bytes are frozen after a snapshot and tell the user */
31static int snapshot_frozen_objsize;
32
Jordan Crouse156cfbc2012-01-24 09:32:04 -070033static struct kgsl_snapshot_obj {
34 int type;
35 uint32_t gpuaddr;
36 uint32_t ptbase;
37 void *ptr;
38 int dwords;
39} objbuf[SNAPSHOT_OBJ_BUFSIZE];
40
41/* Pointer to the next open entry in the object list */
42static int objbufptr;
43
44/* Push a new buffer object onto the list */
45static void push_object(struct kgsl_device *device, int type, uint32_t ptbase,
46 uint32_t gpuaddr, int dwords)
47{
48 int index;
49 void *ptr;
50
Jordan Crousee9e91bf2012-02-21 09:48:36 -070051 /*
52 * Sometimes IBs can be reused in the same dump. Because we parse from
53 * oldest to newest, if we come across an IB that has already been used,
54 * assume that it has been reused and update the list with the newest
55 * size.
56 */
57
Jordan Crouse156cfbc2012-01-24 09:32:04 -070058 for (index = 0; index < objbufptr; index++) {
59 if (objbuf[index].gpuaddr == gpuaddr &&
Jordan Crousee9e91bf2012-02-21 09:48:36 -070060 objbuf[index].ptbase == ptbase) {
61 objbuf[index].dwords = dwords;
62 return;
63 }
Jordan Crouse156cfbc2012-01-24 09:32:04 -070064 }
65
66 if (objbufptr == SNAPSHOT_OBJ_BUFSIZE) {
67 KGSL_DRV_ERR(device, "snapshot: too many snapshot objects\n");
68 return;
69 }
70
71 /*
72 * adreno_convertaddr verifies that the IB size is valid - at least in
73 * the context of it being smaller then the allocated memory space
74 */
75 ptr = adreno_convertaddr(device, ptbase, gpuaddr, dwords << 2);
76
77 if (ptr == NULL) {
78 KGSL_DRV_ERR(device,
79 "snapshot: Can't find GPU address for %x\n", gpuaddr);
80 return;
81 }
82
83 /* Put it on the list of things to parse */
84 objbuf[objbufptr].type = type;
85 objbuf[objbufptr].gpuaddr = gpuaddr;
86 objbuf[objbufptr].ptbase = ptbase;
87 objbuf[objbufptr].dwords = dwords;
88 objbuf[objbufptr++].ptr = ptr;
89}
90
Jordan Crousee9e91bf2012-02-21 09:48:36 -070091/*
92 * Return a 1 if the specified object is already on the list of buffers
93 * to be dumped
94 */
95
96static int find_object(int type, unsigned int gpuaddr, unsigned int ptbase)
97{
98 int index;
99
100 for (index = 0; index < objbufptr; index++) {
101 if (objbuf[index].gpuaddr == gpuaddr &&
102 objbuf[index].ptbase == ptbase &&
103 objbuf[index].type == type)
104 return 1;
105 }
106
107 return 0;
108}
109
Jordan Crouseea2c6382012-03-16 14:53:42 -0600110static void ib_parse_load_state(struct kgsl_device *device, unsigned int *pkt,
111 unsigned int ptbase)
112{
113 unsigned int block, source, type;
114
115 /*
116 * The object here is to find indirect shaders i.e - shaders loaded from
117 * GPU memory instead of directly in the command. These should be added
118 * to the list of memory objects to dump. So look at the load state
119 * call and see if 1) the shader block is a shader (block = 4, 5 or 6)
120 * 2) that the block is indirect (source = 4). If these all match then
121 * add the memory address to the list. The size of the object will
122 * differ depending on the type. Type 0 (instructions) are 8 dwords per
123 * unit and type 1 (constants) are 2 dwords per unit.
124 */
125
126 if (type3_pkt_size(pkt[0]) < 2)
127 return;
128
129 /*
130 * pkt[1] 18:16 - source
131 * pkt[1] 21:19 - state block
132 * pkt[1] 31:22 - size in units
133 * pkt[2] 0:1 - type
134 * pkt[2] 31:2 - GPU memory address
135 */
136
137 block = (pkt[1] >> 19) & 0x07;
138 source = (pkt[1] >> 16) & 0x07;
139 type = pkt[2] & 0x03;
140
141 if ((block == 4 || block == 5 || block == 6) && source == 4) {
142 int unitsize = (type == 0) ? 8 : 2;
143 int ret;
144
145 /* Freeze the GPU buffer containing the shader */
146
147 ret = kgsl_snapshot_get_object(device, ptbase,
148 pkt[2] & 0xFFFFFFFC,
149 (((pkt[1] >> 22) & 0x03FF) * unitsize) << 2,
150 SNAPSHOT_GPU_OBJECT_SHADER);
151 snapshot_frozen_objsize += ret;
152 }
153}
154
155/*
156 * Parse all the type3 opcode packets that may contain important information,
157 * such as additional GPU buffers to grab
158 */
159
160static void ib_parse_type3(struct kgsl_device *device, unsigned int *ptr,
161 unsigned int ptbase)
162{
163 switch (cp_type3_opcode(*ptr)) {
164 case CP_LOAD_STATE:
165 ib_parse_load_state(device, ptr, ptbase);
166 break;
167 }
168}
169
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700170/* Snapshot the istore memory */
171static int snapshot_istore(struct kgsl_device *device, void *snapshot,
172 int remain, void *priv)
173{
174 struct kgsl_snapshot_istore *header = snapshot;
175 unsigned int *data = snapshot + sizeof(*header);
176 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
177 int count, i;
178
Jordan Crousec6b3a992012-02-04 10:23:51 -0700179 count = adreno_dev->istore_size * adreno_dev->instruction_size;
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700180
181 if (remain < (count * 4) + sizeof(*header)) {
182 KGSL_DRV_ERR(device,
183 "snapshot: Not enough memory for the istore section");
184 return 0;
185 }
186
187 header->count = adreno_dev->istore_size;
188
189 for (i = 0; i < count; i++)
190 kgsl_regread(device, ADRENO_ISTORE_START + i, &data[i]);
191
192 return (count * 4) + sizeof(*header);
193}
194
195/* Snapshot the ringbuffer memory */
196static int snapshot_rb(struct kgsl_device *device, void *snapshot,
197 int remain, void *priv)
198{
199 struct kgsl_snapshot_rb *header = snapshot;
200 unsigned int *data = snapshot + sizeof(*header);
201 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
202 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
203 unsigned int rbbase, ptbase, rptr, *rbptr;
204 int start, stop, index;
205 int numitems, size;
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700206 int parse_ibs = 0, ib_parse_start;
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700207
208 /* Get the GPU address of the ringbuffer */
209 kgsl_regread(device, REG_CP_RB_BASE, &rbbase);
210
211 /* Get the physical address of the MMU pagetable */
212 ptbase = kgsl_mmu_get_current_ptbase(device);
213
214 /* Get the current read pointers for the RB */
215 kgsl_regread(device, REG_CP_RB_RPTR, &rptr);
216
217 /* start the dump at the rptr minus some history */
218 start = (int) rptr - NUM_DWORDS_OF_RINGBUFFER_HISTORY;
219 if (start < 0)
220 start += rb->sizedwords;
221
222 /*
223 * Stop the dump at the point where the software last wrote. Don't use
224 * the hardware value here on the chance that it didn't get properly
225 * updated
226 */
227
228 stop = (int) rb->wptr + 16;
229 if (stop > rb->sizedwords)
230 stop -= rb->sizedwords;
231
232 /* Set up the header for the section */
233
234 numitems = (stop > start) ? stop - start :
235 (rb->sizedwords - start) + stop;
236
237 size = (numitems << 2);
238
239 if (remain < size + sizeof(*header)) {
240 KGSL_DRV_ERR(device,
241 "snapshot: Not enough memory for the rb section");
242 return 0;
243 }
244
245 /* Write the sub-header for the section */
246 header->start = start;
247 header->end = stop;
248 header->wptr = rb->wptr;
249 header->rbsize = rb->sizedwords;
250 header->count = numitems;
251
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700252 /*
253 * We can only reliably dump IBs from the beginning of the context,
254 * and it turns out that for the vast majority of the time we really
255 * only care about the current context when it comes to diagnosing
256 * a hang. So, with an eye to limiting the buffer dumping to what is
257 * really useful find the beginning of the context and only dump
258 * IBs from that point
259 */
260
261 index = rptr;
262 ib_parse_start = start;
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700263 rbptr = rb->buffer_desc.hostptr;
264
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700265 while (index != start) {
266 index--;
267
268 if (index < 0) {
269 /*
270 * The marker we are looking for is 2 dwords long, so
271 * when wrapping, go back 2 from the end so we don't
272 * access out of range in the if statement below
273 */
274 index = rb->sizedwords - 2;
275
276 /*
277 * Account for the possibility that start might be at
278 * rb->sizedwords - 1
279 */
280
281 if (start == rb->sizedwords - 1)
282 break;
283 }
284
285 /*
286 * Look for a NOP packet with the context switch identifier in
287 * the second dword
288 */
289
290 if (rbptr[index] == cp_nop_packet(1) &&
291 rbptr[index + 1] == KGSL_CONTEXT_TO_MEM_IDENTIFIER) {
292 ib_parse_start = index;
293 break;
294 }
295 }
296
297 index = start;
298
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700299 /*
300 * Loop through the RB, copying the data and looking for indirect
301 * buffers and MMU pagetable changes
302 */
303
304 while (index != rb->wptr) {
305 *data = rbptr[index];
306
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700307 /* Only parse IBs between the context start and the rptr */
308
309 if (index == ib_parse_start)
310 parse_ibs = 1;
311
312 if (index == rptr)
313 parse_ibs = 0;
314
Jeremy Gebben6ca1d152012-03-05 14:19:01 -0700315 if (parse_ibs && adreno_cmd_is_ib(rbptr[index]))
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700316 push_object(device, SNAPSHOT_OBJ_TYPE_IB, ptbase,
317 rbptr[index + 1], rbptr[index + 2]);
318
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700319 index = index + 1;
320
321 if (index == rb->sizedwords)
322 index = 0;
323
324 data++;
325 }
326
327 /* Dump 16 dwords past the wptr, but don't bother interpeting it */
328
329 while (index != stop) {
330 *data = rbptr[index];
331 index = index + 1;
332
333 if (index == rb->sizedwords)
334 index = 0;
335
336 data++;
337 }
338
339 /* Return the size of the section */
340 return size + sizeof(*header);
341}
342
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700343static int snapshot_ib(struct kgsl_device *device, void *snapshot,
344 int remain, void *priv)
345{
346 struct kgsl_snapshot_ib *header = snapshot;
347 struct kgsl_snapshot_obj *obj = priv;
348 unsigned int *src = obj->ptr;
349 unsigned int *dst = snapshot + sizeof(*header);
350 int i;
351
352 if (remain < (obj->dwords << 2) + sizeof(*header)) {
353 KGSL_DRV_ERR(device,
354 "snapshot: Not enough memory for the ib section");
355 return 0;
356 }
357
358 /* Write the sub-header for the section */
359 header->gpuaddr = obj->gpuaddr;
360 header->ptbase = obj->ptbase;
361 header->size = obj->dwords;
362
363 /* Write the contents of the ib */
Jordan Crouseea2c6382012-03-16 14:53:42 -0600364 for (i = 0; i < obj->dwords; i++, src++, dst++) {
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700365 *dst = *src;
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700366
Jordan Crouseea2c6382012-03-16 14:53:42 -0600367 if (pkt_is_type3(*src)) {
368 if ((obj->dwords - i) < type3_pkt_size(*src) + 1)
369 continue;
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700370
Jordan Crouseea2c6382012-03-16 14:53:42 -0600371 if (adreno_cmd_is_ib(*src))
372 push_object(device, SNAPSHOT_OBJ_TYPE_IB,
373 obj->ptbase, src[1], src[2]);
374 else
375 ib_parse_type3(device, src, obj->ptbase);
376 }
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700377 }
378
379 return (obj->dwords << 2) + sizeof(*header);
380}
381
382/* Dump another item on the current pending list */
383static void *dump_object(struct kgsl_device *device, int obj, void *snapshot,
384 int *remain)
385{
386 switch (objbuf[obj].type) {
387 case SNAPSHOT_OBJ_TYPE_IB:
388 snapshot = kgsl_snapshot_add_section(device,
389 KGSL_SNAPSHOT_SECTION_IB, snapshot, remain,
390 snapshot_ib, &objbuf[obj]);
391 break;
392 default:
393 KGSL_DRV_ERR(device,
394 "snapshot: Invalid snapshot object type: %d\n",
395 objbuf[obj].type);
396 break;
397 }
398
399 return snapshot;
400}
401
402/* adreno_snapshot - Snapshot the Adreno GPU state
403 * @device - KGSL device to snapshot
404 * @snapshot - Pointer to the start of memory to write into
405 * @remain - A pointer to how many bytes of memory are remaining in the snapshot
406 * @hang - set if this snapshot was automatically triggered by a GPU hang
407 * This is a hook function called by kgsl_snapshot to snapshot the
408 * Adreno specific information for the GPU snapshot. In turn, this function
409 * calls the GPU specific snapshot function to get core specific information.
410 */
411
412void *adreno_snapshot(struct kgsl_device *device, void *snapshot, int *remain,
413 int hang)
414{
415 int i;
416 uint32_t ptbase, ibbase, ibsize;
417 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
418
419 /* Reset the list of objects */
420 objbufptr = 0;
421
Jordan Crouse9610b6b2012-03-16 14:53:42 -0600422 snapshot_frozen_objsize = 0;
423
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700424 /* Get the physical address of the MMU pagetable */
425 ptbase = kgsl_mmu_get_current_ptbase(device);
426
427 /* Dump the ringbuffer */
428 snapshot = kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_RB,
429 snapshot, remain, snapshot_rb, NULL);
430
431 /*
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700432 * Make sure that the last IB1 that was being executed is dumped.
433 * Since this was the last IB1 that was processed, we should have
434 * already added it to the list during the ringbuffer parse but we
435 * want to be double plus sure.
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700436 */
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700437
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700438 kgsl_regread(device, REG_CP_IB1_BASE, &ibbase);
439 kgsl_regread(device, REG_CP_IB1_BUFSZ, &ibsize);
440
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700441 /*
442 * The problem is that IB size from the register is the unprocessed size
443 * of the buffer not the original size, so if we didn't catch this
444 * buffer being directly used in the RB, then we might not be able to
445 * dump the whle thing. Print a warning message so we can try to
446 * figure how often this really happens.
447 */
448
449 if (!find_object(SNAPSHOT_OBJ_TYPE_IB, ibbase, ptbase) && ibsize) {
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700450 push_object(device, SNAPSHOT_OBJ_TYPE_IB, ptbase,
451 ibbase, ibsize);
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700452 KGSL_DRV_ERR(device, "CP_IB1_BASE not found in the ringbuffer. "
453 "Dumping %x dwords of the buffer.\n", ibsize);
454 }
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700455
456 kgsl_regread(device, REG_CP_IB2_BASE, &ibbase);
457 kgsl_regread(device, REG_CP_IB2_BUFSZ, &ibsize);
458
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700459 /*
460 * Add the last parsed IB2 to the list. The IB2 should be found as we
461 * parse the objects below, but we try to add it to the list first, so
462 * it too can be parsed. Don't print an error message in this case - if
463 * the IB2 is found during parsing, the list will be updated with the
464 * correct size.
465 */
466
467 if (!find_object(SNAPSHOT_OBJ_TYPE_IB, ibbase, ptbase) && ibsize) {
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700468 push_object(device, SNAPSHOT_OBJ_TYPE_IB, ptbase,
469 ibbase, ibsize);
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700470 }
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700471
472 /*
473 * Go through the list of found objects and dump each one. As the IBs
474 * are parsed, more objects might be found, and objbufptr will increase
475 */
476 for (i = 0; i < objbufptr; i++)
477 snapshot = dump_object(device, i, snapshot, remain);
478
479 /*
480 * Only dump the istore on a hang - reading it on a running system
481 * has a non 0 chance of hanging the GPU
482 */
483
484 if (hang) {
485 snapshot = kgsl_snapshot_add_section(device,
486 KGSL_SNAPSHOT_SECTION_ISTORE, snapshot, remain,
487 snapshot_istore, NULL);
488 }
489
490 /* Add GPU specific sections - registers mainly, but other stuff too */
491 if (adreno_dev->gpudev->snapshot)
492 snapshot = adreno_dev->gpudev->snapshot(adreno_dev, snapshot,
493 remain, hang);
494
Jordan Crouse9610b6b2012-03-16 14:53:42 -0600495 if (snapshot_frozen_objsize)
496 KGSL_DRV_ERR(device, "GPU snapshot froze %dKb of GPU buffers\n",
497 snapshot_frozen_objsize / 1024);
498
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700499 return snapshot;
500}