blob: bca70401942f277f760574224aeebb09a66b362e [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"
Jordan Crousee0879b12012-03-16 14:53:43 -060020#include "a3xx_reg.h"
Jordan Crouse156cfbc2012-01-24 09:32:04 -070021
22/* Number of dwords of ringbuffer history to record */
23#define NUM_DWORDS_OF_RINGBUFFER_HISTORY 100
24
25/* Maintain a list of the objects we see during parsing */
26
27#define SNAPSHOT_OBJ_BUFSIZE 64
28
29#define SNAPSHOT_OBJ_TYPE_IB 0
30
Jordan Crouse9610b6b2012-03-16 14:53:42 -060031/* Keep track of how many bytes are frozen after a snapshot and tell the user */
32static int snapshot_frozen_objsize;
33
Jordan Crouse156cfbc2012-01-24 09:32:04 -070034static struct kgsl_snapshot_obj {
35 int type;
36 uint32_t gpuaddr;
37 uint32_t ptbase;
38 void *ptr;
39 int dwords;
40} objbuf[SNAPSHOT_OBJ_BUFSIZE];
41
42/* Pointer to the next open entry in the object list */
43static int objbufptr;
44
45/* Push a new buffer object onto the list */
46static void push_object(struct kgsl_device *device, int type, uint32_t ptbase,
47 uint32_t gpuaddr, int dwords)
48{
49 int index;
50 void *ptr;
51
Jordan Crousee9e91bf2012-02-21 09:48:36 -070052 /*
53 * Sometimes IBs can be reused in the same dump. Because we parse from
54 * oldest to newest, if we come across an IB that has already been used,
55 * assume that it has been reused and update the list with the newest
56 * size.
57 */
58
Jordan Crouse156cfbc2012-01-24 09:32:04 -070059 for (index = 0; index < objbufptr; index++) {
60 if (objbuf[index].gpuaddr == gpuaddr &&
Jordan Crousee9e91bf2012-02-21 09:48:36 -070061 objbuf[index].ptbase == ptbase) {
62 objbuf[index].dwords = dwords;
63 return;
64 }
Jordan Crouse156cfbc2012-01-24 09:32:04 -070065 }
66
67 if (objbufptr == SNAPSHOT_OBJ_BUFSIZE) {
68 KGSL_DRV_ERR(device, "snapshot: too many snapshot objects\n");
69 return;
70 }
71
72 /*
73 * adreno_convertaddr verifies that the IB size is valid - at least in
74 * the context of it being smaller then the allocated memory space
75 */
76 ptr = adreno_convertaddr(device, ptbase, gpuaddr, dwords << 2);
77
78 if (ptr == NULL) {
79 KGSL_DRV_ERR(device,
80 "snapshot: Can't find GPU address for %x\n", gpuaddr);
81 return;
82 }
83
84 /* Put it on the list of things to parse */
85 objbuf[objbufptr].type = type;
86 objbuf[objbufptr].gpuaddr = gpuaddr;
87 objbuf[objbufptr].ptbase = ptbase;
88 objbuf[objbufptr].dwords = dwords;
89 objbuf[objbufptr++].ptr = ptr;
90}
91
Jordan Crousee9e91bf2012-02-21 09:48:36 -070092/*
93 * Return a 1 if the specified object is already on the list of buffers
94 * to be dumped
95 */
96
97static int find_object(int type, unsigned int gpuaddr, unsigned int ptbase)
98{
99 int index;
100
101 for (index = 0; index < objbufptr; index++) {
102 if (objbuf[index].gpuaddr == gpuaddr &&
103 objbuf[index].ptbase == ptbase &&
104 objbuf[index].type == type)
105 return 1;
106 }
107
108 return 0;
109}
110
Jordan Crousee0879b12012-03-16 14:53:43 -0600111/*
112 * This structure keeps track of type0 writes to VSC_PIPE_DATA_ADDRESS_x and
113 * VSC_PIPE_DATA_LENGTH_x. When a draw initator is called these registers
114 * point to buffers that we need to freeze for a snapshot
115 */
116
117static struct {
118 unsigned int base;
119 unsigned int size;
120} vsc_pipe[8];
121
122/*
123 * This is the cached value of type0 writes to the VSC_SIZE_ADDRESS which
124 * contains the buffer address of the visiblity stream size buffer during a
125 * binning pass
126 */
127
128static unsigned int vsc_size_address;
129
130/*
131 * This struct keeps track of type0 writes to VFD_FETCH_INSTR_0_X and
132 * VFD_FETCH_INSTR_1_X registers. When a draw initator is called the addresses
133 * and sizes in these registers point to VBOs that we need to freeze for a
134 * snapshot
135 */
136
137static struct {
138 unsigned int base;
139 unsigned int stride;
140} vbo[16];
141
142/*
143 * This is the cached value of type0 writes to VFD_INDEX_MAX. This will be used
144 * to calculate the size of the VBOs when the draw initator is called
145 */
146
147static unsigned int vfd_index_max;
148
149/*
150 * This is the cached value of type0 writes to VFD_CONTROL_0 which tells us how
151 * many VBOs are active when the draw initator is called
152 */
153
154static unsigned int vfd_control_0;
155
156/*
157 * Cached value of type0 writes to SP_VS_PVT_MEM_ADDR and SP_FS_PVT_MEM_ADDR.
158 * This is a buffer that contains private stack information for the shader
159 */
160
161static unsigned int sp_vs_pvt_mem_addr;
162static unsigned int sp_fs_pvt_mem_addr;
163
Jordan Crouseea2c6382012-03-16 14:53:42 -0600164static void ib_parse_load_state(struct kgsl_device *device, unsigned int *pkt,
165 unsigned int ptbase)
166{
167 unsigned int block, source, type;
168
169 /*
170 * The object here is to find indirect shaders i.e - shaders loaded from
171 * GPU memory instead of directly in the command. These should be added
172 * to the list of memory objects to dump. So look at the load state
173 * call and see if 1) the shader block is a shader (block = 4, 5 or 6)
174 * 2) that the block is indirect (source = 4). If these all match then
175 * add the memory address to the list. The size of the object will
176 * differ depending on the type. Type 0 (instructions) are 8 dwords per
177 * unit and type 1 (constants) are 2 dwords per unit.
178 */
179
180 if (type3_pkt_size(pkt[0]) < 2)
181 return;
182
183 /*
184 * pkt[1] 18:16 - source
185 * pkt[1] 21:19 - state block
186 * pkt[1] 31:22 - size in units
187 * pkt[2] 0:1 - type
188 * pkt[2] 31:2 - GPU memory address
189 */
190
191 block = (pkt[1] >> 19) & 0x07;
192 source = (pkt[1] >> 16) & 0x07;
193 type = pkt[2] & 0x03;
194
195 if ((block == 4 || block == 5 || block == 6) && source == 4) {
196 int unitsize = (type == 0) ? 8 : 2;
197 int ret;
198
199 /* Freeze the GPU buffer containing the shader */
200
201 ret = kgsl_snapshot_get_object(device, ptbase,
202 pkt[2] & 0xFFFFFFFC,
203 (((pkt[1] >> 22) & 0x03FF) * unitsize) << 2,
204 SNAPSHOT_GPU_OBJECT_SHADER);
205 snapshot_frozen_objsize += ret;
206 }
207}
208
209/*
Jordan Crousee0879b12012-03-16 14:53:43 -0600210 * This opcode sets the base addresses for the visibilty stream buffer and the
211 * visiblity stream size buffer.
212 */
213
214static void ib_parse_set_bin_data(struct kgsl_device *device, unsigned int *pkt,
215 unsigned int ptbase)
216{
217 int ret;
218
219 if (type3_pkt_size(pkt[0]) < 2)
220 return;
221
222 /* Visiblity stream buffer */
223 ret = kgsl_snapshot_get_object(device, ptbase, pkt[1], 0,
224 SNAPSHOT_GPU_OBJECT_GENERIC);
225 snapshot_frozen_objsize += ret;
226
227 /* visiblity stream size buffer (fixed size 8 dwords) */
228 ret = kgsl_snapshot_get_object(device, ptbase, pkt[2], 32,
229 SNAPSHOT_GPU_OBJECT_GENERIC);
230 snapshot_frozen_objsize += ret;
231}
232
233/*
234 * This opcode writes to GPU memory - if the buffer is written to, there is a
235 * good chance that it would be valuable to capture in the snapshot, so mark all
236 * buffers that are written to as frozen
237 */
238
239static void ib_parse_mem_write(struct kgsl_device *device, unsigned int *pkt,
240 unsigned int ptbase)
241{
242 int ret;
243
244 if (type3_pkt_size(pkt[0]) < 1)
245 return;
246
247 /*
248 * The address is where the data in the rest of this packet is written
249 * to, but since that might be an offset into the larger buffer we need
250 * to get the whole thing. Pass a size of 0 kgsl_snapshot_get_object to
251 * capture the entire buffer.
252 */
253
254 ret = kgsl_snapshot_get_object(device, ptbase, pkt[1] & 0xFFFFFFFC, 0,
255 SNAPSHOT_GPU_OBJECT_GENERIC);
256
257 snapshot_frozen_objsize += ret;
258}
259
260/*
261 * The DRAW_INDX opcode sends a draw initator which starts a draw operation in
262 * the GPU, so this is the point where all the registers and buffers become
263 * "valid". The DRAW_INDX may also have an index buffer pointer that should be
264 * frozen with the others
265 */
266
267static void ib_parse_draw_indx(struct kgsl_device *device, unsigned int *pkt,
268 unsigned int ptbase)
269{
270 int ret, i;
271
272 if (type3_pkt_size(pkt[0]) < 3)
273 return;
274
275 /* DRAW_IDX may have a index buffer pointer */
276
277 if (type3_pkt_size(pkt[0]) > 3) {
278 ret = kgsl_snapshot_get_object(device, ptbase, pkt[4], pkt[5],
279 SNAPSHOT_GPU_OBJECT_GENERIC);
280 snapshot_frozen_objsize += ret;
281 }
282
283 /*
284 * All of the type0 writes are valid at a draw initiator, so freeze
285 * the various buffers that we are tracking
286 */
287
288 /* First up the visiblity stream buffer */
289
290 for (i = 0; i < ARRAY_SIZE(vsc_pipe); i++) {
291 if (vsc_pipe[i].base != 0 && vsc_pipe[i].size != 0) {
292 ret = kgsl_snapshot_get_object(device, ptbase,
293 vsc_pipe[i].base, vsc_pipe[i].size,
294 SNAPSHOT_GPU_OBJECT_GENERIC);
295 snapshot_frozen_objsize += ret;
296 }
297 }
298
299 /* Next the visibility stream size buffer */
300
301 if (vsc_size_address) {
302 ret = kgsl_snapshot_get_object(device, ptbase,
303 vsc_size_address, 32,
304 SNAPSHOT_GPU_OBJECT_GENERIC);
305 snapshot_frozen_objsize += ret;
306 }
307
308 /* Next private shader buffer memory */
309 if (sp_vs_pvt_mem_addr) {
310 ret = kgsl_snapshot_get_object(device, ptbase,
311 sp_vs_pvt_mem_addr, 8192,
312 SNAPSHOT_GPU_OBJECT_GENERIC);
313
314 snapshot_frozen_objsize += ret;
315 }
316
317 if (sp_fs_pvt_mem_addr) {
318 ret = kgsl_snapshot_get_object(device, ptbase,
319 sp_fs_pvt_mem_addr, 8192,
320 SNAPSHOT_GPU_OBJECT_GENERIC);
321 snapshot_frozen_objsize += ret;
322 }
323
324 /* Finally: VBOs */
325
326 /* The number of active VBOs is stored in VFD_CONTROL_O[31:27] */
327 for (i = 0; i < (vfd_control_0) >> 27; i++) {
328 int size;
329
330 /*
331 * The size of the VBO is the stride stored in
332 * VFD_FETCH_INSTR_0_X.BUFSTRIDE * VFD_INDEX_MAX. The base
333 * is stored in VFD_FETCH_INSTR_1_X
334 */
335
336 if (vbo[i].base != 0) {
337 size = vbo[i].stride * vfd_index_max;
338
339 ret = kgsl_snapshot_get_object(device, ptbase,
340 vbo[i].base,
341 0, SNAPSHOT_GPU_OBJECT_GENERIC);
342 snapshot_frozen_objsize += ret;
343 }
344 }
345}
346
347/*
Jordan Crouseea2c6382012-03-16 14:53:42 -0600348 * Parse all the type3 opcode packets that may contain important information,
Jordan Crousee0879b12012-03-16 14:53:43 -0600349 * such as additional GPU buffers to grab or a draw initator
Jordan Crouseea2c6382012-03-16 14:53:42 -0600350 */
351
352static void ib_parse_type3(struct kgsl_device *device, unsigned int *ptr,
353 unsigned int ptbase)
354{
355 switch (cp_type3_opcode(*ptr)) {
356 case CP_LOAD_STATE:
357 ib_parse_load_state(device, ptr, ptbase);
358 break;
Jordan Crousee0879b12012-03-16 14:53:43 -0600359 case CP_SET_BIN_DATA:
360 ib_parse_set_bin_data(device, ptr, ptbase);
361 break;
362 case CP_MEM_WRITE:
363 ib_parse_mem_write(device, ptr, ptbase);
364 break;
365 case CP_DRAW_INDX:
366 ib_parse_draw_indx(device, ptr, ptbase);
367 break;
368 }
369}
370
371/*
372 * Parse type0 packets found in the stream. Some of the registers that are
373 * written are clues for GPU buffers that we need to freeze. Register writes
374 * are considred valid when a draw initator is called, so just cache the values
375 * here and freeze them when a CP_DRAW_INDX is seen. This protects against
376 * needlessly caching buffers that won't be used during a draw call
377 */
378
379static void ib_parse_type0(struct kgsl_device *device, unsigned int *ptr,
380 unsigned int ptbase)
381{
382 int size = type0_pkt_size(*ptr);
383 int offset = type0_pkt_offset(*ptr);
384 int i;
385
Jordan Crouse7f24c712012-04-05 16:55:55 -0600386 for (i = 0; i < size; i++, offset++) {
Jordan Crousee0879b12012-03-16 14:53:43 -0600387
388 /* Visiblity stream buffer */
389
390 if (offset >= A3XX_VSC_PIPE_DATA_ADDRESS_0 &&
391 offset <= A3XX_VSC_PIPE_DATA_LENGTH_7) {
392 int index = offset - A3XX_VSC_PIPE_DATA_ADDRESS_0;
393
394 /* Each bank of address and length registers are
395 * interleaved with an empty register:
396 *
397 * address 0
398 * length 0
399 * empty
400 * address 1
401 * length 1
402 * empty
403 * ...
404 */
405
Jordan Crouse7f24c712012-04-05 16:55:55 -0600406 if ((index % 3) == 0)
Jordan Crousee0879b12012-03-16 14:53:43 -0600407 vsc_pipe[index / 3].base = ptr[i + 1];
Jordan Crouse7f24c712012-04-05 16:55:55 -0600408 else if ((index % 3) == 1)
Jordan Crousee0879b12012-03-16 14:53:43 -0600409 vsc_pipe[index / 3].size = ptr[i + 1];
410 } else if ((offset >= A3XX_VFD_FETCH_INSTR_0_0) &&
411 (offset <= A3XX_VFD_FETCH_INSTR_1_F)) {
412 int index = offset - A3XX_VFD_FETCH_INSTR_0_0;
413
414 /*
415 * FETCH_INSTR_0_X and FETCH_INSTR_1_X banks are
416 * interleaved as above but without the empty register
417 * in between
418 */
419
Jordan Crouse7f24c712012-04-05 16:55:55 -0600420 if ((index % 2) == 0)
Jordan Crousee0879b12012-03-16 14:53:43 -0600421 vbo[index >> 1].stride =
422 (ptr[i + 1] >> 7) & 0x1FF;
423 else
424 vbo[index >> 1].base = ptr[i + 1];
425 } else {
426 /*
427 * Cache various support registers for calculating
428 * buffer sizes
429 */
430
431 switch (offset) {
432 case A3XX_VFD_CONTROL_0:
433 vfd_control_0 = ptr[i + 1];
434 break;
435 case A3XX_VFD_INDEX_MAX:
436 vfd_index_max = ptr[i + 1];
437 break;
438 case A3XX_VSC_SIZE_ADDRESS:
439 vsc_size_address = ptr[i + 1];
440 break;
441 case A3XX_SP_VS_PVT_MEM_ADDR_REG:
442 sp_vs_pvt_mem_addr = ptr[i + 1];
443 break;
444 case A3XX_SP_FS_PVT_MEM_ADDR_REG:
445 sp_fs_pvt_mem_addr = ptr[i + 1];
446 break;
447 }
448 }
Jordan Crouseea2c6382012-03-16 14:53:42 -0600449 }
450}
451
Jordan Crousef99f5a662012-03-16 14:53:43 -0600452/* Add an IB as a GPU object, but first, parse it to find more goodies within */
453
454static void ib_add_gpu_object(struct kgsl_device *device, unsigned int ptbase,
455 unsigned int gpuaddr, unsigned int dwords)
456{
Jordan Crousee0879b12012-03-16 14:53:43 -0600457 int i = 0, ret;
Jordan Crousef99f5a662012-03-16 14:53:43 -0600458 unsigned int *src = (unsigned int *) adreno_convertaddr(device, ptbase,
459 gpuaddr, dwords << 2);
460
461 if (src == NULL)
462 return;
463
Jordan Crousee0879b12012-03-16 14:53:43 -0600464 while (i < dwords) {
465
Jordan Crousef99f5a662012-03-16 14:53:43 -0600466 if (pkt_is_type3(src[i])) {
467 if ((dwords - i) < type3_pkt_size(src[i]) + 1)
Jordan Crousee0879b12012-03-16 14:53:43 -0600468 goto skip;
Jordan Crousef99f5a662012-03-16 14:53:43 -0600469
470 if (adreno_cmd_is_ib(src[i]))
471 ib_add_gpu_object(device, ptbase,
472 src[i + 1], src[i + 2]);
473 else
474 ib_parse_type3(device, &src[i], ptbase);
Jordan Crousee0879b12012-03-16 14:53:43 -0600475
476 i += type3_pkt_size(src[i]);
477 } else if (pkt_is_type0(src[i])) {
478 ib_parse_type0(device, &src[i], ptbase);
479 i += type0_pkt_size(src[i]);
Jordan Crousef99f5a662012-03-16 14:53:43 -0600480 }
Jordan Crousee0879b12012-03-16 14:53:43 -0600481
482skip:
483 i++;
Jordan Crousef99f5a662012-03-16 14:53:43 -0600484 }
485
486 ret = kgsl_snapshot_get_object(device, ptbase, gpuaddr, dwords << 2,
487 SNAPSHOT_GPU_OBJECT_IB);
488
489 snapshot_frozen_objsize += ret;
490}
491
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700492/* Snapshot the istore memory */
493static int snapshot_istore(struct kgsl_device *device, void *snapshot,
494 int remain, void *priv)
495{
496 struct kgsl_snapshot_istore *header = snapshot;
497 unsigned int *data = snapshot + sizeof(*header);
498 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
499 int count, i;
500
Jordan Crousec6b3a992012-02-04 10:23:51 -0700501 count = adreno_dev->istore_size * adreno_dev->instruction_size;
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700502
503 if (remain < (count * 4) + sizeof(*header)) {
504 KGSL_DRV_ERR(device,
505 "snapshot: Not enough memory for the istore section");
506 return 0;
507 }
508
509 header->count = adreno_dev->istore_size;
510
511 for (i = 0; i < count; i++)
512 kgsl_regread(device, ADRENO_ISTORE_START + i, &data[i]);
513
514 return (count * 4) + sizeof(*header);
515}
516
517/* Snapshot the ringbuffer memory */
518static int snapshot_rb(struct kgsl_device *device, void *snapshot,
519 int remain, void *priv)
520{
521 struct kgsl_snapshot_rb *header = snapshot;
522 unsigned int *data = snapshot + sizeof(*header);
523 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
524 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Jordan Crousee6b77622012-04-05 16:55:54 -0600525 unsigned int ptbase, rptr, *rbptr, ibbase;
526 int index, size, i;
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700527 int parse_ibs = 0, ib_parse_start;
Jordan Crousee6b77622012-04-05 16:55:54 -0600528 int skip_pktsize = 1;
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700529
530 /* Get the physical address of the MMU pagetable */
531 ptbase = kgsl_mmu_get_current_ptbase(device);
532
533 /* Get the current read pointers for the RB */
534 kgsl_regread(device, REG_CP_RB_RPTR, &rptr);
535
Jordan Crousee6b77622012-04-05 16:55:54 -0600536 /* Address of the last processed IB */
Jordan Crousef99f5a662012-03-16 14:53:43 -0600537 kgsl_regread(device, REG_CP_IB1_BASE, &ibbase);
538
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700539 /*
Jordan Crousee6b77622012-04-05 16:55:54 -0600540 * Figure out the window of ringbuffer data to dump. First we need to
541 * find where the last processed IB ws submitted
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700542 */
543
Jordan Crousee6b77622012-04-05 16:55:54 -0600544 index = rptr;
545 rbptr = rb->buffer_desc.hostptr;
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700546
Jordan Crousee6b77622012-04-05 16:55:54 -0600547 while (index != rb->wptr) {
548 index--;
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700549
Jordan Crousee6b77622012-04-05 16:55:54 -0600550 if (index < 0) {
551 index = rb->sizedwords - 3;
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700552
Jordan Crousee6b77622012-04-05 16:55:54 -0600553 /* We wrapped without finding what we wanted */
554 if (index < rb->wptr) {
555 index = rb->wptr;
556 break;
557 }
558 }
559
560 if (adreno_cmd_is_ib(rbptr[index]) &&
561 rbptr[index + 1] == ibbase)
562 break;
563 }
564
565 /*
566 * index points at the last submitted IB. We can only trust that the
567 * memory between the context switch and the hanging IB is valid, so
568 * the next step is to find the context switch before the submission
569 */
570
571 while (index != rb->wptr) {
572 index--;
573
574 if (index < 0) {
575 index = rb->sizedwords - 2;
576
577 /*
578 * Wrapped without finding the context switch. This is
579 * harmless - we should still have enough data to dump a
580 * valid state
581 */
582
583 if (index < rb->wptr) {
584 index = rb->wptr;
585 break;
586 }
587 }
588
589 /* Break if the current packet is a context switch identifier */
590 if (adreno_rb_ctxtswitch(&rbptr[index]))
591 break;
592 }
593
594 /*
595 * Index represents the start of the window of interest. We will try
596 * to dump all buffers between here and the rptr
597 */
598
599 ib_parse_start = index;
600
601 /*
602 * Dump the entire ringbuffer - the parser can choose how much of it to
603 * process
604 */
605
606 size = (rb->sizedwords << 2);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700607
608 if (remain < size + sizeof(*header)) {
609 KGSL_DRV_ERR(device,
610 "snapshot: Not enough memory for the rb section");
611 return 0;
612 }
613
614 /* Write the sub-header for the section */
Jordan Crousee6b77622012-04-05 16:55:54 -0600615 header->start = rb->wptr;
616 header->end = rb->wptr;
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700617 header->wptr = rb->wptr;
618 header->rbsize = rb->sizedwords;
Jordan Crousee6b77622012-04-05 16:55:54 -0600619 header->count = rb->sizedwords;
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700620
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700621 /*
622 * Loop through the RB, copying the data and looking for indirect
623 * buffers and MMU pagetable changes
624 */
625
Jordan Crousee6b77622012-04-05 16:55:54 -0600626 index = rb->wptr;
627 for (i = 0; i < rb->sizedwords; i++) {
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700628 *data = rbptr[index];
629
Jordan Crousee6b77622012-04-05 16:55:54 -0600630 /*
631 * Sometimes the rptr is located in the middle of a packet.
632 * try to adust for that by modifying the rptr to match a
633 * packet boundary. Unfortunately for us, it is hard to tell
634 * which dwords are legitimate type0 header and which are just
635 * random data so just walk over type0 packets until we get
636 * to the first type3, and from that point on start checking the
637 * size of the packet and adjusting accordingly
638 */
639
640 if (skip_pktsize && pkt_is_type3(rbptr[index]))
641 skip_pktsize = 0;
642
643 if (skip_pktsize == 0) {
644 unsigned int pktsize = type3_pkt_size(rbptr[index]);
645 if (index + pktsize > rptr)
646 rptr = (index + pktsize) % rb->sizedwords;
647 }
648
649 /*
650 * Only parse IBs between the start and the rptr or the next
651 * context switch, whichever comes first
652 */
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700653
654 if (index == ib_parse_start)
655 parse_ibs = 1;
Jordan Crousee6b77622012-04-05 16:55:54 -0600656 else if (index == rptr || adreno_rb_ctxtswitch(&rbptr[index]))
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700657 parse_ibs = 0;
658
Jordan Crousef99f5a662012-03-16 14:53:43 -0600659 if (parse_ibs && adreno_cmd_is_ib(rbptr[index])) {
660 /*
661 * The IB from CP_IB1_BASE goes into the snapshot, all
662 * others get marked at GPU objects
663 */
664 if (rbptr[index + 1] == ibbase)
665 push_object(device, SNAPSHOT_OBJ_TYPE_IB,
666 ptbase, rbptr[index + 1],
667 rbptr[index + 2]);
668 else
669 ib_add_gpu_object(device, ptbase,
670 rbptr[index + 1], rbptr[index + 2]);
671 }
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700672
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700673 index = index + 1;
674
675 if (index == rb->sizedwords)
676 index = 0;
677
678 data++;
679 }
680
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700681 /* Return the size of the section */
682 return size + sizeof(*header);
683}
684
Jordan Crousee0879b12012-03-16 14:53:43 -0600685/* Snapshot the memory for an indirect buffer */
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700686static int snapshot_ib(struct kgsl_device *device, void *snapshot,
687 int remain, void *priv)
688{
689 struct kgsl_snapshot_ib *header = snapshot;
690 struct kgsl_snapshot_obj *obj = priv;
691 unsigned int *src = obj->ptr;
692 unsigned int *dst = snapshot + sizeof(*header);
693 int i;
694
695 if (remain < (obj->dwords << 2) + sizeof(*header)) {
696 KGSL_DRV_ERR(device,
697 "snapshot: Not enough memory for the ib section");
698 return 0;
699 }
700
701 /* Write the sub-header for the section */
702 header->gpuaddr = obj->gpuaddr;
703 header->ptbase = obj->ptbase;
704 header->size = obj->dwords;
705
706 /* Write the contents of the ib */
Jordan Crouseea2c6382012-03-16 14:53:42 -0600707 for (i = 0; i < obj->dwords; i++, src++, dst++) {
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700708 *dst = *src;
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700709
Jordan Crouseea2c6382012-03-16 14:53:42 -0600710 if (pkt_is_type3(*src)) {
711 if ((obj->dwords - i) < type3_pkt_size(*src) + 1)
712 continue;
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700713
Jordan Crouseea2c6382012-03-16 14:53:42 -0600714 if (adreno_cmd_is_ib(*src))
715 push_object(device, SNAPSHOT_OBJ_TYPE_IB,
716 obj->ptbase, src[1], src[2]);
717 else
718 ib_parse_type3(device, src, obj->ptbase);
719 }
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700720 }
721
722 return (obj->dwords << 2) + sizeof(*header);
723}
724
725/* Dump another item on the current pending list */
726static void *dump_object(struct kgsl_device *device, int obj, void *snapshot,
727 int *remain)
728{
729 switch (objbuf[obj].type) {
730 case SNAPSHOT_OBJ_TYPE_IB:
731 snapshot = kgsl_snapshot_add_section(device,
732 KGSL_SNAPSHOT_SECTION_IB, snapshot, remain,
733 snapshot_ib, &objbuf[obj]);
734 break;
735 default:
736 KGSL_DRV_ERR(device,
737 "snapshot: Invalid snapshot object type: %d\n",
738 objbuf[obj].type);
739 break;
740 }
741
742 return snapshot;
743}
744
745/* adreno_snapshot - Snapshot the Adreno GPU state
746 * @device - KGSL device to snapshot
747 * @snapshot - Pointer to the start of memory to write into
748 * @remain - A pointer to how many bytes of memory are remaining in the snapshot
749 * @hang - set if this snapshot was automatically triggered by a GPU hang
750 * This is a hook function called by kgsl_snapshot to snapshot the
751 * Adreno specific information for the GPU snapshot. In turn, this function
752 * calls the GPU specific snapshot function to get core specific information.
753 */
754
755void *adreno_snapshot(struct kgsl_device *device, void *snapshot, int *remain,
756 int hang)
757{
758 int i;
759 uint32_t ptbase, ibbase, ibsize;
760 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
761
762 /* Reset the list of objects */
763 objbufptr = 0;
764
Jordan Crouse9610b6b2012-03-16 14:53:42 -0600765 snapshot_frozen_objsize = 0;
766
Jordan Crousee0879b12012-03-16 14:53:43 -0600767 /* Clear the caches for the visibilty stream and VBO parsing */
768
769 vfd_control_0 = 0;
770 vfd_index_max = 0;
771 vsc_size_address = 0;
772
773 memset(vsc_pipe, 0, sizeof(vsc_pipe));
774 memset(vbo, 0, sizeof(vbo));
775
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700776 /* Get the physical address of the MMU pagetable */
777 ptbase = kgsl_mmu_get_current_ptbase(device);
778
779 /* Dump the ringbuffer */
780 snapshot = kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_RB,
781 snapshot, remain, snapshot_rb, NULL);
782
783 /*
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700784 * Make sure that the last IB1 that was being executed is dumped.
785 * Since this was the last IB1 that was processed, we should have
786 * already added it to the list during the ringbuffer parse but we
787 * want to be double plus sure.
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700788 */
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700789
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700790 kgsl_regread(device, REG_CP_IB1_BASE, &ibbase);
791 kgsl_regread(device, REG_CP_IB1_BUFSZ, &ibsize);
792
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700793 /*
794 * The problem is that IB size from the register is the unprocessed size
795 * of the buffer not the original size, so if we didn't catch this
796 * buffer being directly used in the RB, then we might not be able to
797 * dump the whle thing. Print a warning message so we can try to
798 * figure how often this really happens.
799 */
800
801 if (!find_object(SNAPSHOT_OBJ_TYPE_IB, ibbase, ptbase) && ibsize) {
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700802 push_object(device, SNAPSHOT_OBJ_TYPE_IB, ptbase,
803 ibbase, ibsize);
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700804 KGSL_DRV_ERR(device, "CP_IB1_BASE not found in the ringbuffer. "
805 "Dumping %x dwords of the buffer.\n", ibsize);
806 }
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700807
808 kgsl_regread(device, REG_CP_IB2_BASE, &ibbase);
809 kgsl_regread(device, REG_CP_IB2_BUFSZ, &ibsize);
810
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700811 /*
812 * Add the last parsed IB2 to the list. The IB2 should be found as we
813 * parse the objects below, but we try to add it to the list first, so
814 * it too can be parsed. Don't print an error message in this case - if
815 * the IB2 is found during parsing, the list will be updated with the
816 * correct size.
817 */
818
819 if (!find_object(SNAPSHOT_OBJ_TYPE_IB, ibbase, ptbase) && ibsize) {
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700820 push_object(device, SNAPSHOT_OBJ_TYPE_IB, ptbase,
821 ibbase, ibsize);
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700822 }
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700823
824 /*
825 * Go through the list of found objects and dump each one. As the IBs
826 * are parsed, more objects might be found, and objbufptr will increase
827 */
828 for (i = 0; i < objbufptr; i++)
829 snapshot = dump_object(device, i, snapshot, remain);
830
831 /*
832 * Only dump the istore on a hang - reading it on a running system
833 * has a non 0 chance of hanging the GPU
834 */
835
836 if (hang) {
837 snapshot = kgsl_snapshot_add_section(device,
838 KGSL_SNAPSHOT_SECTION_ISTORE, snapshot, remain,
839 snapshot_istore, NULL);
840 }
841
842 /* Add GPU specific sections - registers mainly, but other stuff too */
843 if (adreno_dev->gpudev->snapshot)
844 snapshot = adreno_dev->gpudev->snapshot(adreno_dev, snapshot,
845 remain, hang);
846
Jordan Crouse9610b6b2012-03-16 14:53:42 -0600847 if (snapshot_frozen_objsize)
848 KGSL_DRV_ERR(device, "GPU snapshot froze %dKb of GPU buffers\n",
849 snapshot_frozen_objsize / 1024);
850
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700851 return snapshot;
852}