blob: 2f8f28c37c1433c27e391db5eaf0671adc055bed [file] [log] [blame]
Duy Truonge833aca2013-02-12 13:35:08 -08001/* Copyright (c) 2012, The Linux Foundation. All rights reserved.
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002 *
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 Crouse361cc3d2012-06-20 08:22:14 -0600164/*
165 * Each load state block has two possible types. Each type has a different
166 * number of dwords per unit. Use this handy lookup table to make sure
167 * we dump the right amount of data from the indirect buffer
168 */
169
170static int load_state_unit_sizes[7][2] = {
171 { 2, 4 },
172 { 0, 1 },
173 { 2, 4 },
174 { 0, 1 },
175 { 8, 2 },
176 { 8, 2 },
177 { 8, 2 },
178};
179
Jordan Crouseea2c6382012-03-16 14:53:42 -0600180static void ib_parse_load_state(struct kgsl_device *device, unsigned int *pkt,
181 unsigned int ptbase)
182{
183 unsigned int block, source, type;
184
185 /*
186 * The object here is to find indirect shaders i.e - shaders loaded from
187 * GPU memory instead of directly in the command. These should be added
188 * to the list of memory objects to dump. So look at the load state
Jordan Crouse361cc3d2012-06-20 08:22:14 -0600189 * if the block is indirect (source = 4). If so then add the memory
190 * address to the list. The size of the object differs depending on the
191 * type per the load_state_unit_sizes array above.
Jordan Crouseea2c6382012-03-16 14:53:42 -0600192 */
193
194 if (type3_pkt_size(pkt[0]) < 2)
195 return;
196
197 /*
198 * pkt[1] 18:16 - source
199 * pkt[1] 21:19 - state block
200 * pkt[1] 31:22 - size in units
201 * pkt[2] 0:1 - type
202 * pkt[2] 31:2 - GPU memory address
203 */
204
205 block = (pkt[1] >> 19) & 0x07;
206 source = (pkt[1] >> 16) & 0x07;
207 type = pkt[2] & 0x03;
208
Jordan Crouse361cc3d2012-06-20 08:22:14 -0600209 if (source == 4) {
210 int unitsize, ret;
211
212 if (type == 0)
213 unitsize = load_state_unit_sizes[block][0];
214 else
215 unitsize = load_state_unit_sizes[block][1];
Jordan Crouseea2c6382012-03-16 14:53:42 -0600216
217 /* Freeze the GPU buffer containing the shader */
218
219 ret = kgsl_snapshot_get_object(device, ptbase,
220 pkt[2] & 0xFFFFFFFC,
221 (((pkt[1] >> 22) & 0x03FF) * unitsize) << 2,
222 SNAPSHOT_GPU_OBJECT_SHADER);
223 snapshot_frozen_objsize += ret;
224 }
225}
226
227/*
Jordan Crousee0879b12012-03-16 14:53:43 -0600228 * This opcode sets the base addresses for the visibilty stream buffer and the
229 * visiblity stream size buffer.
230 */
231
232static void ib_parse_set_bin_data(struct kgsl_device *device, unsigned int *pkt,
233 unsigned int ptbase)
234{
235 int ret;
236
237 if (type3_pkt_size(pkt[0]) < 2)
238 return;
239
240 /* Visiblity stream buffer */
241 ret = kgsl_snapshot_get_object(device, ptbase, pkt[1], 0,
242 SNAPSHOT_GPU_OBJECT_GENERIC);
243 snapshot_frozen_objsize += ret;
244
245 /* visiblity stream size buffer (fixed size 8 dwords) */
246 ret = kgsl_snapshot_get_object(device, ptbase, pkt[2], 32,
247 SNAPSHOT_GPU_OBJECT_GENERIC);
248 snapshot_frozen_objsize += ret;
249}
250
251/*
252 * This opcode writes to GPU memory - if the buffer is written to, there is a
253 * good chance that it would be valuable to capture in the snapshot, so mark all
254 * buffers that are written to as frozen
255 */
256
257static void ib_parse_mem_write(struct kgsl_device *device, unsigned int *pkt,
258 unsigned int ptbase)
259{
260 int ret;
261
262 if (type3_pkt_size(pkt[0]) < 1)
263 return;
264
265 /*
266 * The address is where the data in the rest of this packet is written
267 * to, but since that might be an offset into the larger buffer we need
268 * to get the whole thing. Pass a size of 0 kgsl_snapshot_get_object to
269 * capture the entire buffer.
270 */
271
272 ret = kgsl_snapshot_get_object(device, ptbase, pkt[1] & 0xFFFFFFFC, 0,
273 SNAPSHOT_GPU_OBJECT_GENERIC);
274
275 snapshot_frozen_objsize += ret;
276}
277
278/*
279 * The DRAW_INDX opcode sends a draw initator which starts a draw operation in
280 * the GPU, so this is the point where all the registers and buffers become
281 * "valid". The DRAW_INDX may also have an index buffer pointer that should be
282 * frozen with the others
283 */
284
285static void ib_parse_draw_indx(struct kgsl_device *device, unsigned int *pkt,
286 unsigned int ptbase)
287{
288 int ret, i;
289
290 if (type3_pkt_size(pkt[0]) < 3)
291 return;
292
293 /* DRAW_IDX may have a index buffer pointer */
294
295 if (type3_pkt_size(pkt[0]) > 3) {
296 ret = kgsl_snapshot_get_object(device, ptbase, pkt[4], pkt[5],
297 SNAPSHOT_GPU_OBJECT_GENERIC);
298 snapshot_frozen_objsize += ret;
299 }
300
301 /*
302 * All of the type0 writes are valid at a draw initiator, so freeze
303 * the various buffers that we are tracking
304 */
305
306 /* First up the visiblity stream buffer */
307
308 for (i = 0; i < ARRAY_SIZE(vsc_pipe); i++) {
309 if (vsc_pipe[i].base != 0 && vsc_pipe[i].size != 0) {
310 ret = kgsl_snapshot_get_object(device, ptbase,
311 vsc_pipe[i].base, vsc_pipe[i].size,
312 SNAPSHOT_GPU_OBJECT_GENERIC);
313 snapshot_frozen_objsize += ret;
314 }
315 }
316
317 /* Next the visibility stream size buffer */
318
319 if (vsc_size_address) {
320 ret = kgsl_snapshot_get_object(device, ptbase,
321 vsc_size_address, 32,
322 SNAPSHOT_GPU_OBJECT_GENERIC);
323 snapshot_frozen_objsize += ret;
324 }
325
326 /* Next private shader buffer memory */
327 if (sp_vs_pvt_mem_addr) {
328 ret = kgsl_snapshot_get_object(device, ptbase,
329 sp_vs_pvt_mem_addr, 8192,
330 SNAPSHOT_GPU_OBJECT_GENERIC);
Jordan Crousee0879b12012-03-16 14:53:43 -0600331 snapshot_frozen_objsize += ret;
Jordan Crouse21aaadf2012-09-11 16:38:15 -0600332 sp_vs_pvt_mem_addr = 0;
Jordan Crousee0879b12012-03-16 14:53:43 -0600333 }
334
335 if (sp_fs_pvt_mem_addr) {
336 ret = kgsl_snapshot_get_object(device, ptbase,
337 sp_fs_pvt_mem_addr, 8192,
338 SNAPSHOT_GPU_OBJECT_GENERIC);
339 snapshot_frozen_objsize += ret;
Jordan Crouse21aaadf2012-09-11 16:38:15 -0600340 sp_fs_pvt_mem_addr = 0;
Jordan Crousee0879b12012-03-16 14:53:43 -0600341 }
342
343 /* Finally: VBOs */
344
345 /* The number of active VBOs is stored in VFD_CONTROL_O[31:27] */
346 for (i = 0; i < (vfd_control_0) >> 27; i++) {
347 int size;
348
349 /*
350 * The size of the VBO is the stride stored in
351 * VFD_FETCH_INSTR_0_X.BUFSTRIDE * VFD_INDEX_MAX. The base
352 * is stored in VFD_FETCH_INSTR_1_X
353 */
354
355 if (vbo[i].base != 0) {
356 size = vbo[i].stride * vfd_index_max;
357
358 ret = kgsl_snapshot_get_object(device, ptbase,
359 vbo[i].base,
360 0, SNAPSHOT_GPU_OBJECT_GENERIC);
361 snapshot_frozen_objsize += ret;
362 }
Jordan Crouse21aaadf2012-09-11 16:38:15 -0600363
364 vbo[i].base = 0;
365 vbo[i].stride = 0;
Jordan Crousee0879b12012-03-16 14:53:43 -0600366 }
Jordan Crouse21aaadf2012-09-11 16:38:15 -0600367
368 vfd_control_0 = 0;
369 vfd_index_max = 0;
Jordan Crousee0879b12012-03-16 14:53:43 -0600370}
371
372/*
Jordan Crouseea2c6382012-03-16 14:53:42 -0600373 * Parse all the type3 opcode packets that may contain important information,
Jordan Crousee0879b12012-03-16 14:53:43 -0600374 * such as additional GPU buffers to grab or a draw initator
Jordan Crouseea2c6382012-03-16 14:53:42 -0600375 */
376
377static void ib_parse_type3(struct kgsl_device *device, unsigned int *ptr,
378 unsigned int ptbase)
379{
380 switch (cp_type3_opcode(*ptr)) {
381 case CP_LOAD_STATE:
382 ib_parse_load_state(device, ptr, ptbase);
383 break;
Jordan Crousee0879b12012-03-16 14:53:43 -0600384 case CP_SET_BIN_DATA:
385 ib_parse_set_bin_data(device, ptr, ptbase);
386 break;
387 case CP_MEM_WRITE:
388 ib_parse_mem_write(device, ptr, ptbase);
389 break;
390 case CP_DRAW_INDX:
391 ib_parse_draw_indx(device, ptr, ptbase);
392 break;
393 }
394}
395
396/*
397 * Parse type0 packets found in the stream. Some of the registers that are
398 * written are clues for GPU buffers that we need to freeze. Register writes
399 * are considred valid when a draw initator is called, so just cache the values
400 * here and freeze them when a CP_DRAW_INDX is seen. This protects against
401 * needlessly caching buffers that won't be used during a draw call
402 */
403
404static void ib_parse_type0(struct kgsl_device *device, unsigned int *ptr,
405 unsigned int ptbase)
406{
407 int size = type0_pkt_size(*ptr);
408 int offset = type0_pkt_offset(*ptr);
409 int i;
410
Jordan Crouse7f24c712012-04-05 16:55:55 -0600411 for (i = 0; i < size; i++, offset++) {
Jordan Crousee0879b12012-03-16 14:53:43 -0600412
413 /* Visiblity stream buffer */
414
415 if (offset >= A3XX_VSC_PIPE_DATA_ADDRESS_0 &&
416 offset <= A3XX_VSC_PIPE_DATA_LENGTH_7) {
417 int index = offset - A3XX_VSC_PIPE_DATA_ADDRESS_0;
418
419 /* Each bank of address and length registers are
420 * interleaved with an empty register:
421 *
422 * address 0
423 * length 0
424 * empty
425 * address 1
426 * length 1
427 * empty
428 * ...
429 */
430
Jordan Crouse7f24c712012-04-05 16:55:55 -0600431 if ((index % 3) == 0)
Jordan Crousee0879b12012-03-16 14:53:43 -0600432 vsc_pipe[index / 3].base = ptr[i + 1];
Jordan Crouse7f24c712012-04-05 16:55:55 -0600433 else if ((index % 3) == 1)
Jordan Crousee0879b12012-03-16 14:53:43 -0600434 vsc_pipe[index / 3].size = ptr[i + 1];
435 } else if ((offset >= A3XX_VFD_FETCH_INSTR_0_0) &&
436 (offset <= A3XX_VFD_FETCH_INSTR_1_F)) {
437 int index = offset - A3XX_VFD_FETCH_INSTR_0_0;
438
439 /*
440 * FETCH_INSTR_0_X and FETCH_INSTR_1_X banks are
441 * interleaved as above but without the empty register
442 * in between
443 */
444
Jordan Crouse7f24c712012-04-05 16:55:55 -0600445 if ((index % 2) == 0)
Jordan Crousee0879b12012-03-16 14:53:43 -0600446 vbo[index >> 1].stride =
447 (ptr[i + 1] >> 7) & 0x1FF;
448 else
449 vbo[index >> 1].base = ptr[i + 1];
450 } else {
451 /*
452 * Cache various support registers for calculating
453 * buffer sizes
454 */
455
456 switch (offset) {
457 case A3XX_VFD_CONTROL_0:
458 vfd_control_0 = ptr[i + 1];
459 break;
460 case A3XX_VFD_INDEX_MAX:
461 vfd_index_max = ptr[i + 1];
462 break;
463 case A3XX_VSC_SIZE_ADDRESS:
464 vsc_size_address = ptr[i + 1];
465 break;
466 case A3XX_SP_VS_PVT_MEM_ADDR_REG:
467 sp_vs_pvt_mem_addr = ptr[i + 1];
468 break;
469 case A3XX_SP_FS_PVT_MEM_ADDR_REG:
470 sp_fs_pvt_mem_addr = ptr[i + 1];
471 break;
472 }
473 }
Jordan Crouseea2c6382012-03-16 14:53:42 -0600474 }
475}
476
Jordan Crousef99f5a662012-03-16 14:53:43 -0600477/* Add an IB as a GPU object, but first, parse it to find more goodies within */
478
479static void ib_add_gpu_object(struct kgsl_device *device, unsigned int ptbase,
480 unsigned int gpuaddr, unsigned int dwords)
481{
Jordan Crouse17d6d8b2012-04-18 09:31:09 -0600482 int i, ret, rem = dwords;
Jordan Crouse21aaadf2012-09-11 16:38:15 -0600483 unsigned int *src;
484
485 /*
486 * If the object is already in the list, we don't need to parse it again
487 */
488
489 if (kgsl_snapshot_have_object(device, ptbase, gpuaddr, dwords << 2))
490 return;
491
492 src = (unsigned int *) adreno_convertaddr(device, ptbase, gpuaddr,
493 dwords << 2);
Jordan Crousef99f5a662012-03-16 14:53:43 -0600494
495 if (src == NULL)
496 return;
497
Jordan Crouse21aaadf2012-09-11 16:38:15 -0600498 for (i = 0; rem > 0; rem--, i++) {
Jordan Crouse17d6d8b2012-04-18 09:31:09 -0600499 int pktsize;
500
Jordan Crouse21aaadf2012-09-11 16:38:15 -0600501 /* If the packet isn't a type 1 or a type 3, then don't bother
502 * parsing it - it is likely corrupted */
503
Jordan Crouse17d6d8b2012-04-18 09:31:09 -0600504 if (!pkt_is_type0(src[i]) && !pkt_is_type3(src[i]))
Jordan Crouse21aaadf2012-09-11 16:38:15 -0600505 break;
Jordan Crouse17d6d8b2012-04-18 09:31:09 -0600506
507 pktsize = type3_pkt_size(src[i]);
508
Jordan Crouse21aaadf2012-09-11 16:38:15 -0600509 if (!pktsize || (pktsize + 1) > rem)
Jordan Crouse17d6d8b2012-04-18 09:31:09 -0600510 break;
Jordan Crousee0879b12012-03-16 14:53:43 -0600511
Jordan Crousef99f5a662012-03-16 14:53:43 -0600512 if (pkt_is_type3(src[i])) {
Jordan Crouse3bbb56e2012-10-25 09:37:40 -0600513 if (adreno_cmd_is_ib(src[i])) {
514 unsigned int gpuaddr = src[i + 1];
515 unsigned int size = src[i + 2];
516 unsigned int ibbase;
517
518 /* Address of the last processed IB2 */
519 kgsl_regread(device, REG_CP_IB2_BASE, &ibbase);
520
521 /*
522 * If this is the last IB2 that was executed,
523 * then push it to make sure it goes into the
524 * static space
525 */
526
527 if (ibbase == gpuaddr)
528 push_object(device,
529 SNAPSHOT_OBJ_TYPE_IB, ptbase,
530 gpuaddr, size);
531 else
532 ib_add_gpu_object(device, ptbase,
533 gpuaddr, size);
534 } else
Jordan Crousef99f5a662012-03-16 14:53:43 -0600535 ib_parse_type3(device, &src[i], ptbase);
Jordan Crousee0879b12012-03-16 14:53:43 -0600536 } else if (pkt_is_type0(src[i])) {
537 ib_parse_type0(device, &src[i], ptbase);
Jordan Crousef99f5a662012-03-16 14:53:43 -0600538 }
Jordan Crousee0879b12012-03-16 14:53:43 -0600539
Jordan Crouse17d6d8b2012-04-18 09:31:09 -0600540 i += pktsize;
541 rem -= pktsize;
Jordan Crousef99f5a662012-03-16 14:53:43 -0600542 }
543
544 ret = kgsl_snapshot_get_object(device, ptbase, gpuaddr, dwords << 2,
545 SNAPSHOT_GPU_OBJECT_IB);
546
547 snapshot_frozen_objsize += ret;
548}
549
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700550/* Snapshot the istore memory */
551static int snapshot_istore(struct kgsl_device *device, void *snapshot,
552 int remain, void *priv)
553{
554 struct kgsl_snapshot_istore *header = snapshot;
555 unsigned int *data = snapshot + sizeof(*header);
556 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
557 int count, i;
558
Jordan Crousec6b3a992012-02-04 10:23:51 -0700559 count = adreno_dev->istore_size * adreno_dev->instruction_size;
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700560
561 if (remain < (count * 4) + sizeof(*header)) {
562 KGSL_DRV_ERR(device,
563 "snapshot: Not enough memory for the istore section");
564 return 0;
565 }
566
567 header->count = adreno_dev->istore_size;
568
569 for (i = 0; i < count; i++)
570 kgsl_regread(device, ADRENO_ISTORE_START + i, &data[i]);
571
572 return (count * 4) + sizeof(*header);
573}
574
575/* Snapshot the ringbuffer memory */
576static int snapshot_rb(struct kgsl_device *device, void *snapshot,
577 int remain, void *priv)
578{
579 struct kgsl_snapshot_rb *header = snapshot;
580 unsigned int *data = snapshot + sizeof(*header);
581 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
582 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Jordan Crousee6b77622012-04-05 16:55:54 -0600583 unsigned int ptbase, rptr, *rbptr, ibbase;
584 int index, size, i;
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700585 int parse_ibs = 0, ib_parse_start;
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700586
587 /* Get the physical address of the MMU pagetable */
Shubhraprakash Das79447952012-04-26 18:12:23 -0600588 ptbase = kgsl_mmu_get_current_ptbase(&device->mmu);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700589
590 /* Get the current read pointers for the RB */
591 kgsl_regread(device, REG_CP_RB_RPTR, &rptr);
592
Jordan Crousee6b77622012-04-05 16:55:54 -0600593 /* Address of the last processed IB */
Jordan Crousef99f5a662012-03-16 14:53:43 -0600594 kgsl_regread(device, REG_CP_IB1_BASE, &ibbase);
595
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700596 /*
Jordan Crousee6b77622012-04-05 16:55:54 -0600597 * Figure out the window of ringbuffer data to dump. First we need to
Jordan Crouse6c214ba62012-06-20 08:22:16 -0600598 * find where the last processed IB ws submitted. Start walking back
599 * from the rptr
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700600 */
601
Jordan Crousee6b77622012-04-05 16:55:54 -0600602 index = rptr;
603 rbptr = rb->buffer_desc.hostptr;
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700604
Jordan Crouse6c214ba62012-06-20 08:22:16 -0600605 do {
Jordan Crousee6b77622012-04-05 16:55:54 -0600606 index--;
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700607
Jordan Crousee6b77622012-04-05 16:55:54 -0600608 if (index < 0) {
609 index = rb->sizedwords - 3;
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700610
Jordan Crousee6b77622012-04-05 16:55:54 -0600611 /* We wrapped without finding what we wanted */
612 if (index < rb->wptr) {
613 index = rb->wptr;
614 break;
615 }
616 }
617
618 if (adreno_cmd_is_ib(rbptr[index]) &&
619 rbptr[index + 1] == ibbase)
620 break;
Jordan Crouse6c214ba62012-06-20 08:22:16 -0600621 } while (index != rb->wptr);
Jordan Crousee6b77622012-04-05 16:55:54 -0600622
623 /*
624 * index points at the last submitted IB. We can only trust that the
625 * memory between the context switch and the hanging IB is valid, so
626 * the next step is to find the context switch before the submission
627 */
628
629 while (index != rb->wptr) {
630 index--;
631
Jordan Crouse17d6d8b2012-04-18 09:31:09 -0600632 if (index < 0) {
Jordan Crousee6b77622012-04-05 16:55:54 -0600633 index = rb->sizedwords - 2;
634
635 /*
636 * Wrapped without finding the context switch. This is
637 * harmless - we should still have enough data to dump a
638 * valid state
639 */
640
641 if (index < rb->wptr) {
642 index = rb->wptr;
643 break;
644 }
645 }
646
647 /* Break if the current packet is a context switch identifier */
Jordan Crouse17d6d8b2012-04-18 09:31:09 -0600648 if ((rbptr[index] == cp_nop_packet(1)) &&
649 (rbptr[index + 1] == KGSL_CONTEXT_TO_MEM_IDENTIFIER))
Jordan Crousee6b77622012-04-05 16:55:54 -0600650 break;
651 }
652
653 /*
654 * Index represents the start of the window of interest. We will try
655 * to dump all buffers between here and the rptr
656 */
657
658 ib_parse_start = index;
659
660 /*
661 * Dump the entire ringbuffer - the parser can choose how much of it to
662 * process
663 */
664
665 size = (rb->sizedwords << 2);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700666
667 if (remain < size + sizeof(*header)) {
668 KGSL_DRV_ERR(device,
669 "snapshot: Not enough memory for the rb section");
670 return 0;
671 }
672
673 /* Write the sub-header for the section */
Jordan Crousee6b77622012-04-05 16:55:54 -0600674 header->start = rb->wptr;
675 header->end = rb->wptr;
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700676 header->wptr = rb->wptr;
677 header->rbsize = rb->sizedwords;
Jordan Crousee6b77622012-04-05 16:55:54 -0600678 header->count = rb->sizedwords;
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700679
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700680 /*
681 * Loop through the RB, copying the data and looking for indirect
682 * buffers and MMU pagetable changes
683 */
684
Jordan Crousee6b77622012-04-05 16:55:54 -0600685 index = rb->wptr;
686 for (i = 0; i < rb->sizedwords; i++) {
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700687 *data = rbptr[index];
688
Jordan Crousee6b77622012-04-05 16:55:54 -0600689 /*
Jordan Crousee6b77622012-04-05 16:55:54 -0600690 * Only parse IBs between the start and the rptr or the next
691 * context switch, whichever comes first
692 */
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700693
Jordan Crouse21aaadf2012-09-11 16:38:15 -0600694 if (parse_ibs == 0 && index == ib_parse_start)
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700695 parse_ibs = 1;
Jordan Crousee6b77622012-04-05 16:55:54 -0600696 else if (index == rptr || adreno_rb_ctxtswitch(&rbptr[index]))
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700697 parse_ibs = 0;
698
Jordan Crousef99f5a662012-03-16 14:53:43 -0600699 if (parse_ibs && adreno_cmd_is_ib(rbptr[index])) {
Jordan Crouse233b2092012-04-18 09:31:09 -0600700 unsigned int ibaddr = rbptr[index + 1];
701 unsigned int ibsize = rbptr[index + 2];
702
Jordan Crousef99f5a662012-03-16 14:53:43 -0600703 /*
Jordan Crouse233b2092012-04-18 09:31:09 -0600704 * This will return non NULL if the IB happens to be
705 * part of the context memory (i.e - context switch
706 * command buffers)
707 */
708
709 struct kgsl_memdesc *memdesc =
710 adreno_find_ctxtmem(device, ptbase, ibaddr,
711 ibsize);
712
Shubhraprakash Das3d784f52012-06-06 23:12:11 -0600713 /* IOMMU uses a NOP IB placed in setsate memory */
714 if (NULL == memdesc)
715 if (kgsl_gpuaddr_in_memdesc(
716 &device->mmu.setstate_memory,
717 ibaddr, ibsize))
718 memdesc = &device->mmu.setstate_memory;
Jordan Crouse233b2092012-04-18 09:31:09 -0600719 /*
720 * The IB from CP_IB1_BASE and the IBs for legacy
721 * context switch go into the snapshot all
Jordan Crousef99f5a662012-03-16 14:53:43 -0600722 * others get marked at GPU objects
723 */
Jordan Crouse233b2092012-04-18 09:31:09 -0600724
725 if (ibaddr == ibbase || memdesc != NULL)
Jordan Crousef99f5a662012-03-16 14:53:43 -0600726 push_object(device, SNAPSHOT_OBJ_TYPE_IB,
Jordan Crouse233b2092012-04-18 09:31:09 -0600727 ptbase, ibaddr, ibsize);
Jordan Crousef99f5a662012-03-16 14:53:43 -0600728 else
Jordan Crouse233b2092012-04-18 09:31:09 -0600729 ib_add_gpu_object(device, ptbase, ibaddr,
730 ibsize);
Jordan Crousef99f5a662012-03-16 14:53:43 -0600731 }
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700732
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700733 index = index + 1;
734
735 if (index == rb->sizedwords)
736 index = 0;
737
738 data++;
739 }
740
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700741 /* Return the size of the section */
742 return size + sizeof(*header);
743}
744
Jordan Crousee0879b12012-03-16 14:53:43 -0600745/* Snapshot the memory for an indirect buffer */
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700746static int snapshot_ib(struct kgsl_device *device, void *snapshot,
747 int remain, void *priv)
748{
749 struct kgsl_snapshot_ib *header = snapshot;
750 struct kgsl_snapshot_obj *obj = priv;
751 unsigned int *src = obj->ptr;
752 unsigned int *dst = snapshot + sizeof(*header);
753 int i;
754
755 if (remain < (obj->dwords << 2) + sizeof(*header)) {
756 KGSL_DRV_ERR(device,
757 "snapshot: Not enough memory for the ib section");
758 return 0;
759 }
760
761 /* Write the sub-header for the section */
762 header->gpuaddr = obj->gpuaddr;
763 header->ptbase = obj->ptbase;
764 header->size = obj->dwords;
765
766 /* Write the contents of the ib */
Jordan Crouseea2c6382012-03-16 14:53:42 -0600767 for (i = 0; i < obj->dwords; i++, src++, dst++) {
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700768 *dst = *src;
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700769
Jordan Crouseea2c6382012-03-16 14:53:42 -0600770 if (pkt_is_type3(*src)) {
771 if ((obj->dwords - i) < type3_pkt_size(*src) + 1)
772 continue;
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700773
Jordan Crouseea2c6382012-03-16 14:53:42 -0600774 if (adreno_cmd_is_ib(*src))
775 push_object(device, SNAPSHOT_OBJ_TYPE_IB,
776 obj->ptbase, src[1], src[2]);
777 else
778 ib_parse_type3(device, src, obj->ptbase);
779 }
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700780 }
781
782 return (obj->dwords << 2) + sizeof(*header);
783}
784
785/* Dump another item on the current pending list */
786static void *dump_object(struct kgsl_device *device, int obj, void *snapshot,
787 int *remain)
788{
789 switch (objbuf[obj].type) {
790 case SNAPSHOT_OBJ_TYPE_IB:
791 snapshot = kgsl_snapshot_add_section(device,
792 KGSL_SNAPSHOT_SECTION_IB, snapshot, remain,
793 snapshot_ib, &objbuf[obj]);
794 break;
795 default:
796 KGSL_DRV_ERR(device,
797 "snapshot: Invalid snapshot object type: %d\n",
798 objbuf[obj].type);
799 break;
800 }
801
802 return snapshot;
803}
804
805/* adreno_snapshot - Snapshot the Adreno GPU state
806 * @device - KGSL device to snapshot
807 * @snapshot - Pointer to the start of memory to write into
808 * @remain - A pointer to how many bytes of memory are remaining in the snapshot
809 * @hang - set if this snapshot was automatically triggered by a GPU hang
810 * This is a hook function called by kgsl_snapshot to snapshot the
811 * Adreno specific information for the GPU snapshot. In turn, this function
812 * calls the GPU specific snapshot function to get core specific information.
813 */
814
815void *adreno_snapshot(struct kgsl_device *device, void *snapshot, int *remain,
816 int hang)
817{
818 int i;
819 uint32_t ptbase, ibbase, ibsize;
820 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
821
822 /* Reset the list of objects */
823 objbufptr = 0;
824
Jordan Crouse9610b6b2012-03-16 14:53:42 -0600825 snapshot_frozen_objsize = 0;
826
Jordan Crousee0879b12012-03-16 14:53:43 -0600827 /* Clear the caches for the visibilty stream and VBO parsing */
828
829 vfd_control_0 = 0;
830 vfd_index_max = 0;
831 vsc_size_address = 0;
832
833 memset(vsc_pipe, 0, sizeof(vsc_pipe));
834 memset(vbo, 0, sizeof(vbo));
835
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700836 /* Get the physical address of the MMU pagetable */
Shubhraprakash Das79447952012-04-26 18:12:23 -0600837 ptbase = kgsl_mmu_get_current_ptbase(&device->mmu);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700838
839 /* Dump the ringbuffer */
840 snapshot = kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_RB,
841 snapshot, remain, snapshot_rb, NULL);
842
843 /*
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700844 * Make sure that the last IB1 that was being executed is dumped.
845 * Since this was the last IB1 that was processed, we should have
846 * already added it to the list during the ringbuffer parse but we
847 * want to be double plus sure.
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700848 */
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700849
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700850 kgsl_regread(device, REG_CP_IB1_BASE, &ibbase);
851 kgsl_regread(device, REG_CP_IB1_BUFSZ, &ibsize);
852
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700853 /*
854 * The problem is that IB size from the register is the unprocessed size
855 * of the buffer not the original size, so if we didn't catch this
856 * buffer being directly used in the RB, then we might not be able to
857 * dump the whle thing. Print a warning message so we can try to
858 * figure how often this really happens.
859 */
860
861 if (!find_object(SNAPSHOT_OBJ_TYPE_IB, ibbase, ptbase) && ibsize) {
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700862 push_object(device, SNAPSHOT_OBJ_TYPE_IB, ptbase,
863 ibbase, ibsize);
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700864 KGSL_DRV_ERR(device, "CP_IB1_BASE not found in the ringbuffer. "
865 "Dumping %x dwords of the buffer.\n", ibsize);
866 }
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700867
868 kgsl_regread(device, REG_CP_IB2_BASE, &ibbase);
869 kgsl_regread(device, REG_CP_IB2_BUFSZ, &ibsize);
870
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700871 /*
872 * Add the last parsed IB2 to the list. The IB2 should be found as we
873 * parse the objects below, but we try to add it to the list first, so
874 * it too can be parsed. Don't print an error message in this case - if
875 * the IB2 is found during parsing, the list will be updated with the
876 * correct size.
877 */
878
879 if (!find_object(SNAPSHOT_OBJ_TYPE_IB, ibbase, ptbase) && ibsize) {
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700880 push_object(device, SNAPSHOT_OBJ_TYPE_IB, ptbase,
881 ibbase, ibsize);
Jordan Crousee9e91bf2012-02-21 09:48:36 -0700882 }
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700883
884 /*
885 * Go through the list of found objects and dump each one. As the IBs
886 * are parsed, more objects might be found, and objbufptr will increase
887 */
888 for (i = 0; i < objbufptr; i++)
889 snapshot = dump_object(device, i, snapshot, remain);
890
891 /*
892 * Only dump the istore on a hang - reading it on a running system
893 * has a non 0 chance of hanging the GPU
894 */
895
896 if (hang) {
897 snapshot = kgsl_snapshot_add_section(device,
898 KGSL_SNAPSHOT_SECTION_ISTORE, snapshot, remain,
899 snapshot_istore, NULL);
900 }
901
902 /* Add GPU specific sections - registers mainly, but other stuff too */
903 if (adreno_dev->gpudev->snapshot)
904 snapshot = adreno_dev->gpudev->snapshot(adreno_dev, snapshot,
905 remain, hang);
906
Jordan Crouse9610b6b2012-03-16 14:53:42 -0600907 if (snapshot_frozen_objsize)
908 KGSL_DRV_ERR(device, "GPU snapshot froze %dKb of GPU buffers\n",
909 snapshot_frozen_objsize / 1024);
910
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700911 return snapshot;
912}