blob: 5572695a815ab12b4af52d6c46330f35c0f62916 [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 Crouse17d6d8b2012-04-18 09:31:09 -0600457 int i, ret, rem = dwords;
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 Crouse17d6d8b2012-04-18 09:31:09 -0600464 for (i = 0; rem != 0; rem--, i++) {
465 int pktsize;
466
467 if (!pkt_is_type0(src[i]) && !pkt_is_type3(src[i]))
468 continue;
469
470 pktsize = type3_pkt_size(src[i]);
471
472 if ((pktsize + 1) > rem)
473 break;
Jordan Crousee0879b12012-03-16 14:53:43 -0600474
Jordan Crousef99f5a662012-03-16 14:53:43 -0600475 if (pkt_is_type3(src[i])) {
Jordan Crousef99f5a662012-03-16 14:53:43 -0600476 if (adreno_cmd_is_ib(src[i]))
477 ib_add_gpu_object(device, ptbase,
478 src[i + 1], src[i + 2]);
479 else
480 ib_parse_type3(device, &src[i], ptbase);
Jordan Crousee0879b12012-03-16 14:53:43 -0600481 } else if (pkt_is_type0(src[i])) {
482 ib_parse_type0(device, &src[i], ptbase);
Jordan Crousef99f5a662012-03-16 14:53:43 -0600483 }
Jordan Crousee0879b12012-03-16 14:53:43 -0600484
Jordan Crouse17d6d8b2012-04-18 09:31:09 -0600485 i += pktsize;
486 rem -= pktsize;
Jordan Crousef99f5a662012-03-16 14:53:43 -0600487 }
488
489 ret = kgsl_snapshot_get_object(device, ptbase, gpuaddr, dwords << 2,
490 SNAPSHOT_GPU_OBJECT_IB);
491
492 snapshot_frozen_objsize += ret;
493}
494
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700495/* Snapshot the istore memory */
496static int snapshot_istore(struct kgsl_device *device, void *snapshot,
497 int remain, void *priv)
498{
499 struct kgsl_snapshot_istore *header = snapshot;
500 unsigned int *data = snapshot + sizeof(*header);
501 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
502 int count, i;
503
Jordan Crousec6b3a992012-02-04 10:23:51 -0700504 count = adreno_dev->istore_size * adreno_dev->instruction_size;
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700505
506 if (remain < (count * 4) + sizeof(*header)) {
507 KGSL_DRV_ERR(device,
508 "snapshot: Not enough memory for the istore section");
509 return 0;
510 }
511
512 header->count = adreno_dev->istore_size;
513
514 for (i = 0; i < count; i++)
515 kgsl_regread(device, ADRENO_ISTORE_START + i, &data[i]);
516
517 return (count * 4) + sizeof(*header);
518}
519
520/* Snapshot the ringbuffer memory */
521static int snapshot_rb(struct kgsl_device *device, void *snapshot,
522 int remain, void *priv)
523{
524 struct kgsl_snapshot_rb *header = snapshot;
525 unsigned int *data = snapshot + sizeof(*header);
526 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
527 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Jordan Crousee6b77622012-04-05 16:55:54 -0600528 unsigned int ptbase, rptr, *rbptr, ibbase;
529 int index, size, i;
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700530 int parse_ibs = 0, ib_parse_start;
Jordan Crousee6b77622012-04-05 16:55:54 -0600531 int skip_pktsize = 1;
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700532
533 /* Get the physical address of the MMU pagetable */
Shubhraprakash Das79447952012-04-26 18:12:23 -0600534 ptbase = kgsl_mmu_get_current_ptbase(&device->mmu);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700535
536 /* Get the current read pointers for the RB */
537 kgsl_regread(device, REG_CP_RB_RPTR, &rptr);
538
Jordan Crousee6b77622012-04-05 16:55:54 -0600539 /* Address of the last processed IB */
Jordan Crousef99f5a662012-03-16 14:53:43 -0600540 kgsl_regread(device, REG_CP_IB1_BASE, &ibbase);
541
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700542 /*
Jordan Crousee6b77622012-04-05 16:55:54 -0600543 * Figure out the window of ringbuffer data to dump. First we need to
544 * find where the last processed IB ws submitted
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700545 */
546
Jordan Crousee6b77622012-04-05 16:55:54 -0600547 index = rptr;
548 rbptr = rb->buffer_desc.hostptr;
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700549
Jordan Crousee6b77622012-04-05 16:55:54 -0600550 while (index != rb->wptr) {
551 index--;
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700552
Jordan Crousee6b77622012-04-05 16:55:54 -0600553 if (index < 0) {
554 index = rb->sizedwords - 3;
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700555
Jordan Crousee6b77622012-04-05 16:55:54 -0600556 /* We wrapped without finding what we wanted */
557 if (index < rb->wptr) {
558 index = rb->wptr;
559 break;
560 }
561 }
562
563 if (adreno_cmd_is_ib(rbptr[index]) &&
564 rbptr[index + 1] == ibbase)
565 break;
566 }
567
568 /*
569 * index points at the last submitted IB. We can only trust that the
570 * memory between the context switch and the hanging IB is valid, so
571 * the next step is to find the context switch before the submission
572 */
573
574 while (index != rb->wptr) {
575 index--;
576
Jordan Crouse17d6d8b2012-04-18 09:31:09 -0600577 if (index < 0) {
Jordan Crousee6b77622012-04-05 16:55:54 -0600578 index = rb->sizedwords - 2;
579
580 /*
581 * Wrapped without finding the context switch. This is
582 * harmless - we should still have enough data to dump a
583 * valid state
584 */
585
586 if (index < rb->wptr) {
587 index = rb->wptr;
588 break;
589 }
590 }
591
592 /* Break if the current packet is a context switch identifier */
Jordan Crouse17d6d8b2012-04-18 09:31:09 -0600593 if ((rbptr[index] == cp_nop_packet(1)) &&
594 (rbptr[index + 1] == KGSL_CONTEXT_TO_MEM_IDENTIFIER))
Jordan Crousee6b77622012-04-05 16:55:54 -0600595 break;
596 }
597
598 /*
599 * Index represents the start of the window of interest. We will try
600 * to dump all buffers between here and the rptr
601 */
602
603 ib_parse_start = index;
604
605 /*
606 * Dump the entire ringbuffer - the parser can choose how much of it to
607 * process
608 */
609
610 size = (rb->sizedwords << 2);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700611
612 if (remain < size + sizeof(*header)) {
613 KGSL_DRV_ERR(device,
614 "snapshot: Not enough memory for the rb section");
615 return 0;
616 }
617
618 /* Write the sub-header for the section */
Jordan Crousee6b77622012-04-05 16:55:54 -0600619 header->start = rb->wptr;
620 header->end = rb->wptr;
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700621 header->wptr = rb->wptr;
622 header->rbsize = rb->sizedwords;
Jordan Crousee6b77622012-04-05 16:55:54 -0600623 header->count = rb->sizedwords;
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700624
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700625 /*
626 * Loop through the RB, copying the data and looking for indirect
627 * buffers and MMU pagetable changes
628 */
629
Jordan Crousee6b77622012-04-05 16:55:54 -0600630 index = rb->wptr;
631 for (i = 0; i < rb->sizedwords; i++) {
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700632 *data = rbptr[index];
633
Jordan Crousee6b77622012-04-05 16:55:54 -0600634 /*
635 * Sometimes the rptr is located in the middle of a packet.
636 * try to adust for that by modifying the rptr to match a
637 * packet boundary. Unfortunately for us, it is hard to tell
638 * which dwords are legitimate type0 header and which are just
639 * random data so just walk over type0 packets until we get
640 * to the first type3, and from that point on start checking the
641 * size of the packet and adjusting accordingly
642 */
643
644 if (skip_pktsize && pkt_is_type3(rbptr[index]))
645 skip_pktsize = 0;
646
647 if (skip_pktsize == 0) {
648 unsigned int pktsize = type3_pkt_size(rbptr[index]);
649 if (index + pktsize > rptr)
650 rptr = (index + pktsize) % rb->sizedwords;
651 }
652
653 /*
654 * Only parse IBs between the start and the rptr or the next
655 * context switch, whichever comes first
656 */
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700657
658 if (index == ib_parse_start)
659 parse_ibs = 1;
Jordan Crousee6b77622012-04-05 16:55:54 -0600660 else if (index == rptr || adreno_rb_ctxtswitch(&rbptr[index]))
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700661 parse_ibs = 0;
662
Jordan Crousef99f5a662012-03-16 14:53:43 -0600663 if (parse_ibs && adreno_cmd_is_ib(rbptr[index])) {
Jordan Crouse233b2092012-04-18 09:31:09 -0600664 unsigned int ibaddr = rbptr[index + 1];
665 unsigned int ibsize = rbptr[index + 2];
666
Jordan Crousef99f5a662012-03-16 14:53:43 -0600667 /*
Jordan Crouse233b2092012-04-18 09:31:09 -0600668 * This will return non NULL if the IB happens to be
669 * part of the context memory (i.e - context switch
670 * command buffers)
671 */
672
673 struct kgsl_memdesc *memdesc =
674 adreno_find_ctxtmem(device, ptbase, ibaddr,
675 ibsize);
676
Shubhraprakash Das3d784f52012-06-06 23:12:11 -0600677 /* IOMMU uses a NOP IB placed in setsate memory */
678 if (NULL == memdesc)
679 if (kgsl_gpuaddr_in_memdesc(
680 &device->mmu.setstate_memory,
681 ibaddr, ibsize))
682 memdesc = &device->mmu.setstate_memory;
Jordan Crouse233b2092012-04-18 09:31:09 -0600683 /*
684 * The IB from CP_IB1_BASE and the IBs for legacy
685 * context switch go into the snapshot all
Jordan Crousef99f5a662012-03-16 14:53:43 -0600686 * others get marked at GPU objects
687 */
Jordan Crouse233b2092012-04-18 09:31:09 -0600688
689 if (ibaddr == ibbase || memdesc != NULL)
Jordan Crousef99f5a662012-03-16 14:53:43 -0600690 push_object(device, SNAPSHOT_OBJ_TYPE_IB,
Jordan Crouse233b2092012-04-18 09:31:09 -0600691 ptbase, ibaddr, ibsize);
Jordan Crousef99f5a662012-03-16 14:53:43 -0600692 else
Jordan Crouse233b2092012-04-18 09:31:09 -0600693 ib_add_gpu_object(device, ptbase, ibaddr,
694 ibsize);
Jordan Crousef99f5a662012-03-16 14:53:43 -0600695 }
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700696
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700697 index = index + 1;
698
699 if (index == rb->sizedwords)
700 index = 0;
701
702 data++;
703 }
704
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700705 /* Return the size of the section */
706 return size + sizeof(*header);
707}
708
Jordan Crousee0879b12012-03-16 14:53:43 -0600709/* Snapshot the memory for an indirect buffer */
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700710static int snapshot_ib(struct kgsl_device *device, void *snapshot,
711 int remain, void *priv)
712{
713 struct kgsl_snapshot_ib *header = snapshot;
714 struct kgsl_snapshot_obj *obj = priv;
715 unsigned int *src = obj->ptr;
716 unsigned int *dst = snapshot + sizeof(*header);
717 int i;
718
719 if (remain < (obj->dwords << 2) + sizeof(*header)) {
720 KGSL_DRV_ERR(device,
721 "snapshot: Not enough memory for the ib section");
722 return 0;
723 }
724
725 /* Write the sub-header for the section */
726 header->gpuaddr = obj->gpuaddr;
727 header->ptbase = obj->ptbase;
728 header->size = obj->dwords;
729
730 /* Write the contents of the ib */
Jordan Crouseea2c6382012-03-16 14:53:42 -0600731 for (i = 0; i < obj->dwords; i++, src++, dst++) {
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700732 *dst = *src;
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700733
Jordan Crouseea2c6382012-03-16 14:53:42 -0600734 if (pkt_is_type3(*src)) {
735 if ((obj->dwords - i) < type3_pkt_size(*src) + 1)
736 continue;
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700737
Jordan Crouseea2c6382012-03-16 14:53:42 -0600738 if (adreno_cmd_is_ib(*src))
739 push_object(device, SNAPSHOT_OBJ_TYPE_IB,
740 obj->ptbase, src[1], src[2]);
741 else
742 ib_parse_type3(device, src, obj->ptbase);
743 }
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700744 }
745
746 return (obj->dwords << 2) + sizeof(*header);
747}
748
749/* Dump another item on the current pending list */
750static void *dump_object(struct kgsl_device *device, int obj, void *snapshot,
751 int *remain)
752{
753 switch (objbuf[obj].type) {
754 case SNAPSHOT_OBJ_TYPE_IB:
755 snapshot = kgsl_snapshot_add_section(device,
756 KGSL_SNAPSHOT_SECTION_IB, snapshot, remain,
757 snapshot_ib, &objbuf[obj]);
758 break;
759 default:
760 KGSL_DRV_ERR(device,
761 "snapshot: Invalid snapshot object type: %d\n",
762 objbuf[obj].type);
763 break;
764 }
765
766 return snapshot;
767}
768
769/* adreno_snapshot - Snapshot the Adreno GPU state
770 * @device - KGSL device to snapshot
771 * @snapshot - Pointer to the start of memory to write into
772 * @remain - A pointer to how many bytes of memory are remaining in the snapshot
773 * @hang - set if this snapshot was automatically triggered by a GPU hang
774 * This is a hook function called by kgsl_snapshot to snapshot the
775 * Adreno specific information for the GPU snapshot. In turn, this function
776 * calls the GPU specific snapshot function to get core specific information.
777 */
778
779void *adreno_snapshot(struct kgsl_device *device, void *snapshot, int *remain,
780 int hang)
781{
782 int i;
783 uint32_t ptbase, ibbase, ibsize;
784 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
785
786 /* Reset the list of objects */
787 objbufptr = 0;
788
Jordan Crouse9610b6b2012-03-16 14:53:42 -0600789 snapshot_frozen_objsize = 0;
790
Jordan Crousee0879b12012-03-16 14:53:43 -0600791 /* Clear the caches for the visibilty stream and VBO parsing */
792
793 vfd_control_0 = 0;
794 vfd_index_max = 0;
795 vsc_size_address = 0;
796
797 memset(vsc_pipe, 0, sizeof(vsc_pipe));
798 memset(vbo, 0, sizeof(vbo));
799
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700800 /* Get the physical address of the MMU pagetable */
Shubhraprakash Das79447952012-04-26 18:12:23 -0600801 ptbase = kgsl_mmu_get_current_ptbase(&device->mmu);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700802
803 /* Dump the ringbuffer */
804 snapshot = kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_RB,
805 snapshot, remain, snapshot_rb, NULL);
806
807 /*
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700808 * Make sure that the last IB1 that was being executed is dumped.
809 * Since this was the last IB1 that was processed, we should have
810 * already added it to the list during the ringbuffer parse but we
811 * want to be double plus sure.
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700812 */
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700813
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700814 kgsl_regread(device, REG_CP_IB1_BASE, &ibbase);
815 kgsl_regread(device, REG_CP_IB1_BUFSZ, &ibsize);
816
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700817 /*
818 * The problem is that IB size from the register is the unprocessed size
819 * of the buffer not the original size, so if we didn't catch this
820 * buffer being directly used in the RB, then we might not be able to
821 * dump the whle thing. Print a warning message so we can try to
822 * figure how often this really happens.
823 */
824
825 if (!find_object(SNAPSHOT_OBJ_TYPE_IB, ibbase, ptbase) && ibsize) {
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700826 push_object(device, SNAPSHOT_OBJ_TYPE_IB, ptbase,
827 ibbase, ibsize);
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700828 KGSL_DRV_ERR(device, "CP_IB1_BASE not found in the ringbuffer. "
829 "Dumping %x dwords of the buffer.\n", ibsize);
830 }
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700831
832 kgsl_regread(device, REG_CP_IB2_BASE, &ibbase);
833 kgsl_regread(device, REG_CP_IB2_BUFSZ, &ibsize);
834
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700835 /*
836 * Add the last parsed IB2 to the list. The IB2 should be found as we
837 * parse the objects below, but we try to add it to the list first, so
838 * it too can be parsed. Don't print an error message in this case - if
839 * the IB2 is found during parsing, the list will be updated with the
840 * correct size.
841 */
842
843 if (!find_object(SNAPSHOT_OBJ_TYPE_IB, ibbase, ptbase) && ibsize) {
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700844 push_object(device, SNAPSHOT_OBJ_TYPE_IB, ptbase,
845 ibbase, ibsize);
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700846 }
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700847
848 /*
849 * Go through the list of found objects and dump each one. As the IBs
850 * are parsed, more objects might be found, and objbufptr will increase
851 */
852 for (i = 0; i < objbufptr; i++)
853 snapshot = dump_object(device, i, snapshot, remain);
854
855 /*
856 * Only dump the istore on a hang - reading it on a running system
857 * has a non 0 chance of hanging the GPU
858 */
859
860 if (hang) {
861 snapshot = kgsl_snapshot_add_section(device,
862 KGSL_SNAPSHOT_SECTION_ISTORE, snapshot, remain,
863 snapshot_istore, NULL);
864 }
865
866 /* Add GPU specific sections - registers mainly, but other stuff too */
867 if (adreno_dev->gpudev->snapshot)
868 snapshot = adreno_dev->gpudev->snapshot(adreno_dev, snapshot,
869 remain, hang);
870
Jordan Crouse9610b6b2012-03-16 14:53:42 -0600871 if (snapshot_frozen_objsize)
872 KGSL_DRV_ERR(device, "GPU snapshot froze %dKb of GPU buffers\n",
873 snapshot_frozen_objsize / 1024);
874
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700875 return snapshot;
876}