blob: 4b1b5bc4420362495726b2bc5950638376746477 [file] [log] [blame]
Shrenuj Bansala419c792016-10-20 14:05:11 -07001/* Copyright (c) 2012-2017, The Linux Foundation. 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 <linux/export.h>
14#include <linux/time.h>
15#include <linux/sysfs.h>
16#include <linux/utsname.h>
17#include <linux/sched.h>
18#include <linux/idr.h>
19
20#include "kgsl.h"
21#include "kgsl_log.h"
22#include "kgsl_device.h"
23#include "kgsl_sharedmem.h"
24#include "kgsl_snapshot.h"
25#include "adreno_cp_parser.h"
26
27/* Placeholder for list of ib objects that contain all objects in that IB */
28
29struct kgsl_snapshot_cp_obj {
30 struct adreno_ib_object_list *ib_obj_list;
31 struct list_head node;
32};
33
34struct snapshot_obj_itr {
35 u8 *buf; /* Buffer pointer to write to */
36 int pos; /* Current position in the sequence */
37 loff_t offset; /* file offset to start writing from */
38 size_t remain; /* Bytes remaining in buffer */
39 size_t write; /* Bytes written so far */
40};
41
42static void obj_itr_init(struct snapshot_obj_itr *itr, u8 *buf,
43 loff_t offset, size_t remain)
44{
45 itr->buf = buf;
46 itr->offset = offset;
47 itr->remain = remain;
48 itr->pos = 0;
49 itr->write = 0;
50}
51
52static int obj_itr_out(struct snapshot_obj_itr *itr, void *src, int size)
53{
54 if (itr->remain == 0)
55 return 0;
56
57 if ((itr->pos + size) <= itr->offset)
58 goto done;
59
60 /* Handle the case that offset is in the middle of the buffer */
61
62 if (itr->offset > itr->pos) {
63 src += (itr->offset - itr->pos);
64 size -= (itr->offset - itr->pos);
65
66 /* Advance pos to the offset start */
67 itr->pos = itr->offset;
68 }
69
70 if (size > itr->remain)
71 size = itr->remain;
72
73 memcpy(itr->buf, src, size);
74
75 itr->buf += size;
76 itr->write += size;
77 itr->remain -= size;
78
79done:
80 itr->pos += size;
81 return size;
82}
83
84/* idr_for_each function to count the number of contexts */
85
86static int snapshot_context_count(int id, void *ptr, void *data)
87{
88 int *count = data;
89 *count = *count + 1;
90
91 return 0;
92}
93
94/*
95 * To simplify the iterator loop use a global pointer instead of trying
96 * to pass around double star references to the snapshot data
97 */
98
99static u8 *_ctxtptr;
100
101static int snapshot_context_info(int id, void *ptr, void *data)
102{
103 struct kgsl_snapshot_linux_context_v2 *header =
104 (struct kgsl_snapshot_linux_context_v2 *)_ctxtptr;
105 struct kgsl_context *context = ptr;
106 struct kgsl_device *device;
107
108 device = context->device;
109
110 header->id = id;
111
112 /* Future-proof for per-context timestamps - for now, just
113 * return the global timestamp for all contexts
114 */
115
116 kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_QUEUED,
117 &header->timestamp_queued);
118 kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_CONSUMED,
119 &header->timestamp_consumed);
120 kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_RETIRED,
121 &header->timestamp_retired);
122
123 _ctxtptr += sizeof(struct kgsl_snapshot_linux_context_v2);
124
125 return 0;
126}
127
128/* Snapshot the Linux specific information */
129static size_t snapshot_os(struct kgsl_device *device,
130 u8 *buf, size_t remain, void *priv)
131{
132 struct kgsl_snapshot_linux_v2 *header =
133 (struct kgsl_snapshot_linux_v2 *)buf;
134 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
135 int ctxtcount = 0;
136 size_t size = sizeof(*header);
137 struct kgsl_context *context;
138
139 /*
140 * Figure out how many active contexts there are - these will
141 * be appended on the end of the structure
142 */
143
144 read_lock(&device->context_lock);
145 idr_for_each(&device->context_idr, snapshot_context_count, &ctxtcount);
146 read_unlock(&device->context_lock);
147
148 size += ctxtcount * sizeof(struct kgsl_snapshot_linux_context_v2);
149
150 /* Make sure there is enough room for the data */
151 if (remain < size) {
152 SNAPSHOT_ERR_NOMEM(device, "OS");
153 return 0;
154 }
155
156 memset(header, 0, sizeof(*header));
157
158 header->osid = KGSL_SNAPSHOT_OS_LINUX_V3;
159
160 /* Get the kernel build information */
161 strlcpy(header->release, utsname()->release, sizeof(header->release));
162 strlcpy(header->version, utsname()->version, sizeof(header->version));
163
164 /* Get the Unix time for the timestamp */
165 header->seconds = get_seconds();
166
167 /* Remember the power information */
168 header->power_flags = pwr->power_flags;
169 header->power_level = pwr->active_pwrlevel;
170 header->power_interval_timeout = pwr->interval_timeout;
171 header->grpclk = kgsl_get_clkrate(pwr->grp_clks[0]);
172
173 /*
174 * Save the last active context from global index since its more
175 * reliable than currrent RB index
176 */
177 kgsl_sharedmem_readl(&device->memstore, &header->current_context,
178 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL, current_context));
179
180 context = kgsl_context_get(device, header->current_context);
181
182 /* Get the current PT base */
183 header->ptbase = kgsl_mmu_get_current_ttbr0(&device->mmu);
184
185 /* And the PID for the task leader */
186 if (context) {
187 header->pid = context->tid;
188 strlcpy(header->comm, context->proc_priv->comm,
189 sizeof(header->comm));
190 kgsl_context_put(context);
191 context = NULL;
192 }
193
194 header->ctxtcount = ctxtcount;
195
196 _ctxtptr = buf + sizeof(*header);
197 /* append information for each context */
198
199 read_lock(&device->context_lock);
200 idr_for_each(&device->context_idr, snapshot_context_info, NULL);
201 read_unlock(&device->context_lock);
202
203 /* Return the size of the data segment */
204 return size;
205}
206
207static void kgsl_snapshot_put_object(struct kgsl_snapshot_object *obj)
208{
209 list_del(&obj->node);
210
211 obj->entry->memdesc.priv &= ~KGSL_MEMDESC_FROZEN;
212 kgsl_mem_entry_put(obj->entry);
213
214 kfree(obj);
215}
216
217/**
218 * kgsl_snapshot_have_object() - return 1 if the object has been processed
219 * @snapshot: the snapshot data
220 * @process: The process that owns the the object to freeze
221 * @gpuaddr: The gpu address of the object to freeze
222 * @size: the size of the object (may not always be the size of the region)
223 *
224 * Return 1 if the object is already in the list - this can save us from
225 * having to parse the same thing over again. There are 2 lists that are
226 * tracking objects so check for the object in both lists
227 */
228int kgsl_snapshot_have_object(struct kgsl_snapshot *snapshot,
229 struct kgsl_process_private *process,
230 uint64_t gpuaddr, uint64_t size)
231{
232 struct kgsl_snapshot_object *obj;
233 struct kgsl_snapshot_cp_obj *obj_cp;
234 struct adreno_ib_object *ib_obj;
235 int i;
236
237 /* Check whether the object is tracked already in ib list */
238 list_for_each_entry(obj_cp, &snapshot->cp_list, node) {
239 if (obj_cp->ib_obj_list == NULL
240 || obj_cp->ib_obj_list->num_objs == 0)
241 continue;
242
243 ib_obj = &(obj_cp->ib_obj_list->obj_list[0]);
244 if (ib_obj->entry == NULL || ib_obj->entry->priv != process)
245 continue;
246
247 for (i = 0; i < obj_cp->ib_obj_list->num_objs; i++) {
248 ib_obj = &(obj_cp->ib_obj_list->obj_list[i]);
249 if ((gpuaddr >= ib_obj->gpuaddr) &&
250 ((gpuaddr + size) <=
251 (ib_obj->gpuaddr + ib_obj->size)))
252 return 1;
253 }
254 }
255
256 list_for_each_entry(obj, &snapshot->obj_list, node) {
257 if (obj->entry == NULL || obj->entry->priv != process)
258 continue;
259
260 if ((gpuaddr >= obj->gpuaddr) &&
261 ((gpuaddr + size) <= (obj->gpuaddr + obj->size)))
262 return 1;
263 }
264
265 return 0;
266}
267EXPORT_SYMBOL(kgsl_snapshot_have_object);
268
269/**
270 * kgsl_snapshot_get_object() - Mark a GPU buffer to be frozen
271 * @snapshot: The snapshot data
272 * @process: The process that owns the object we want to freeze
273 * @gpuaddr: The gpu address of the object to freeze
274 * @size: the size of the object (may not always be the size of the region)
275 * @type: the type of object being saved (shader, vbo, etc)
276 *
277 * Mark and freeze a GPU buffer object. This will prevent it from being
278 * freed until it can be copied out as part of the snapshot dump. Returns the
279 * size of the object being frozen
280 */
281int kgsl_snapshot_get_object(struct kgsl_snapshot *snapshot,
282 struct kgsl_process_private *process, uint64_t gpuaddr,
283 uint64_t size, unsigned int type)
284{
285 struct kgsl_mem_entry *entry;
286 struct kgsl_snapshot_object *obj;
287 uint64_t offset;
288 int ret = -EINVAL;
289 unsigned int mem_type;
290
291 if (!gpuaddr)
292 return 0;
293
294 entry = kgsl_sharedmem_find(process, gpuaddr);
295
296 if (entry == NULL) {
297 KGSL_CORE_ERR("Unable to find GPU buffer 0x%016llX\n", gpuaddr);
298 return -EINVAL;
299 }
300
301 /* We can't freeze external memory, because we don't own it */
302 if (entry->memdesc.flags & KGSL_MEMFLAGS_USERMEM_MASK)
303 goto err_put;
304 /*
305 * Do not save texture and render targets in snapshot,
306 * they can be just too big
307 */
308
309 mem_type = kgsl_memdesc_get_memtype(&entry->memdesc);
310 if (mem_type == KGSL_MEMTYPE_TEXTURE ||
311 mem_type == KGSL_MEMTYPE_EGL_SURFACE ||
312 mem_type == KGSL_MEMTYPE_EGL_IMAGE) {
313 ret = 0;
314 goto err_put;
315 }
316
317 /* Do not save sparse memory */
318 if (entry->memdesc.flags & KGSL_MEMFLAGS_SPARSE_VIRT ||
319 entry->memdesc.flags & KGSL_MEMFLAGS_SPARSE_PHYS) {
320 ret = 0;
321 goto err_put;
322 }
323
324 /*
325 * size indicates the number of bytes in the region to save. This might
326 * not always be the entire size of the region because some buffers are
327 * sub-allocated from a larger region. However, if size 0 was passed
328 * thats a flag that the caller wants to capture the entire buffer
329 */
330
331 if (size == 0) {
332 size = entry->memdesc.size;
333 offset = 0;
334
335 /* Adjust the gpuaddr to the start of the object */
336 gpuaddr = entry->memdesc.gpuaddr;
337 } else {
338 offset = gpuaddr - entry->memdesc.gpuaddr;
339 }
340
341 if (size + offset > entry->memdesc.size) {
342 KGSL_CORE_ERR("Invalid size for GPU buffer 0x%016llX\n",
343 gpuaddr);
344 goto err_put;
345 }
346
347 /* If the buffer is already on the list, skip it */
348 list_for_each_entry(obj, &snapshot->obj_list, node) {
349 /* combine the range with existing object if they overlap */
350 if (obj->entry->priv == process && obj->type == type &&
351 kgsl_addr_range_overlap(obj->gpuaddr, obj->size,
352 gpuaddr, size)) {
353 uint64_t end1 = obj->gpuaddr + obj->size;
354 uint64_t end2 = gpuaddr + size;
355
356 if (obj->gpuaddr > gpuaddr)
357 obj->gpuaddr = gpuaddr;
358 if (end1 > end2)
359 obj->size = end1 - obj->gpuaddr;
360 else
361 obj->size = end2 - obj->gpuaddr;
362 obj->offset = obj->gpuaddr - entry->memdesc.gpuaddr;
363 ret = 0;
364 goto err_put;
365 }
366 }
367
368 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
369
370 if (obj == NULL)
371 goto err_put;
372
373 obj->type = type;
374 obj->entry = entry;
375 obj->gpuaddr = gpuaddr;
376 obj->size = size;
377 obj->offset = offset;
378
379 list_add(&obj->node, &snapshot->obj_list);
380
381 /*
382 * Return the size of the entire mem entry that was frozen - this gets
383 * used for tracking how much memory is frozen for a hang. Also, mark
384 * the memory entry as frozen. If the entry was already marked as
385 * frozen, then another buffer already got to it. In that case, return
386 * 0 so it doesn't get counted twice
387 */
388
389 ret = (entry->memdesc.priv & KGSL_MEMDESC_FROZEN) ? 0
390 : entry->memdesc.size;
391
392 entry->memdesc.priv |= KGSL_MEMDESC_FROZEN;
393
394 return ret;
395err_put:
396 kgsl_mem_entry_put(entry);
397 return ret;
398}
399EXPORT_SYMBOL(kgsl_snapshot_get_object);
400
401/**
402 * kgsl_snapshot_dump_registers - helper function to dump device registers
403 * @device - the device to dump registers from
404 * @snapshot - pointer to the start of the region of memory for the snapshot
405 * @remain - a pointer to the number of bytes remaining in the snapshot
406 * @priv - A pointer to the kgsl_snapshot_registers data
407 *
408 * Given an array of register ranges pairs (start,end [inclusive]), dump the
409 * registers into a snapshot register section. The snapshot region stores a
410 * part of dwords for each register - the word address of the register, and
411 * the value.
412 */
413size_t kgsl_snapshot_dump_registers(struct kgsl_device *device, u8 *buf,
414 size_t remain, void *priv)
415{
416 struct kgsl_snapshot_regs *header = (struct kgsl_snapshot_regs *)buf;
417 struct kgsl_snapshot_registers *regs = priv;
418 unsigned int *data = (unsigned int *)(buf + sizeof(*header));
419 int count = 0, j, k;
420
421 /* Figure out how many registers we are going to dump */
422
423 for (j = 0; j < regs->count; j++) {
424 int start = regs->regs[j * 2];
425 int end = regs->regs[j * 2 + 1];
426
427 count += (end - start + 1);
428 }
429
430 if (remain < (count * 8) + sizeof(*header)) {
431 SNAPSHOT_ERR_NOMEM(device, "REGISTERS");
432 return 0;
433 }
434
435 for (j = 0; j < regs->count; j++) {
436 unsigned int start = regs->regs[j * 2];
437 unsigned int end = regs->regs[j * 2 + 1];
438
439 for (k = start; k <= end; k++) {
440 unsigned int val;
441
442 kgsl_regread(device, k, &val);
443 *data++ = k;
444 *data++ = val;
445 }
446 }
447
448 header->count = count;
449
450 /* Return the size of the section */
451 return (count * 8) + sizeof(*header);
452}
453EXPORT_SYMBOL(kgsl_snapshot_dump_registers);
454
455struct kgsl_snapshot_indexed_registers {
456 unsigned int index;
457 unsigned int data;
458 unsigned int start;
459 unsigned int count;
460};
461
462static size_t kgsl_snapshot_dump_indexed_regs(struct kgsl_device *device,
463 u8 *buf, size_t remain, void *priv)
464{
465 struct kgsl_snapshot_indexed_registers *iregs = priv;
466 struct kgsl_snapshot_indexed_regs *header =
467 (struct kgsl_snapshot_indexed_regs *)buf;
468 unsigned int *data = (unsigned int *)(buf + sizeof(*header));
469 int i;
470
471 if (remain < (iregs->count * 4) + sizeof(*header)) {
472 SNAPSHOT_ERR_NOMEM(device, "INDEXED REGS");
473 return 0;
474 }
475
476 header->index_reg = iregs->index;
477 header->data_reg = iregs->data;
478 header->count = iregs->count;
479 header->start = iregs->start;
480
481 for (i = 0; i < iregs->count; i++) {
482 kgsl_regwrite(device, iregs->index, iregs->start + i);
483 kgsl_regread(device, iregs->data, &data[i]);
484 }
485
486 return (iregs->count * 4) + sizeof(*header);
487}
488
489/**
490 * kgsl_snapshot_indexed_registers - Add a set of indexed registers to the
491 * snapshot
492 * @device: Pointer to the KGSL device being snapshotted
493 * @snapshot: Snapshot instance
494 * @index: Offset for the index register
495 * @data: Offset for the data register
496 * @start: Index to start reading
497 * @count: Number of entries to read
498 *
499 * Dump the values from an indexed register group into the snapshot
500 */
501void kgsl_snapshot_indexed_registers(struct kgsl_device *device,
502 struct kgsl_snapshot *snapshot,
503 unsigned int index, unsigned int data,
504 unsigned int start,
505 unsigned int count)
506{
507 struct kgsl_snapshot_indexed_registers iregs;
508
509 iregs.index = index;
510 iregs.data = data;
511 iregs.start = start;
512 iregs.count = count;
513
514 kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_INDEXED_REGS,
515 snapshot, kgsl_snapshot_dump_indexed_regs, &iregs);
516}
517EXPORT_SYMBOL(kgsl_snapshot_indexed_registers);
518
519/**
520 * kgsl_snapshot_add_section() - Add a new section to the GPU snapshot
521 * @device: the KGSL device being snapshotted
522 * @id: the section id
523 * @snapshot: pointer to the snapshot instance
524 * @func: Function pointer to fill the section
525 * @priv: Private pointer to pass to the function
526 *
527 * Set up a KGSL snapshot header by filling the memory with the callback
528 * function and adding the standard section header
529 */
530void kgsl_snapshot_add_section(struct kgsl_device *device, u16 id,
531 struct kgsl_snapshot *snapshot,
532 size_t (*func)(struct kgsl_device *, u8 *, size_t, void *),
533 void *priv)
534{
535 struct kgsl_snapshot_section_header *header =
536 (struct kgsl_snapshot_section_header *)snapshot->ptr;
537 u8 *data = snapshot->ptr + sizeof(*header);
538 size_t ret = 0;
539
540 /*
541 * Sanity check to make sure there is enough for the header. The
542 * callback will check to make sure there is enough for the rest
543 * of the data. If there isn't enough room then don't advance the
544 * pointer.
545 */
546
547 if (snapshot->remain < sizeof(*header))
548 return;
549
550 /* It is legal to have no function (i.e. - make an empty section) */
551 if (func) {
552 ret = func(device, data, snapshot->remain - sizeof(*header),
553 priv);
554
555 /*
556 * If there wasn't enough room for the data then don't bother
557 * setting up the header.
558 */
559
560 if (ret == 0)
561 return;
562 }
563
564 header->magic = SNAPSHOT_SECTION_MAGIC;
565 header->id = id;
566 header->size = ret + sizeof(*header);
567
568 snapshot->ptr += header->size;
569 snapshot->remain -= header->size;
570 snapshot->size += header->size;
571}
572
573/**
574 * kgsl_snapshot() - construct a device snapshot
575 * @device: device to snapshot
576 * @context: the context that is hung, might be NULL if unknown.
577 *
578 * Given a device, construct a binary snapshot dump of the current device state
579 * and store it in the device snapshot memory.
580 */
581void kgsl_device_snapshot(struct kgsl_device *device,
582 struct kgsl_context *context)
583{
584 struct kgsl_snapshot_header *header = device->snapshot_memory.ptr;
585 struct kgsl_snapshot *snapshot;
586 struct timespec boot;
587 phys_addr_t pa;
588
589 if (device->snapshot_memory.ptr == NULL) {
590 KGSL_DRV_ERR(device,
591 "snapshot: no snapshot memory available\n");
592 return;
593 }
594
595 if (WARN(!kgsl_state_is_awake(device),
596 "snapshot: device is powered off\n"))
597 return;
598
599 /* increment the hang count for good book keeping */
600 device->snapshot_faultcount++;
601
602 /*
603 * The first hang is always the one we are interested in. Don't capture
604 * a new snapshot instance if the old one hasn't been grabbed yet
605 */
606 if (device->snapshot != NULL)
607 return;
608
609 /* Allocate memory for the snapshot instance */
610 snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
611 if (snapshot == NULL)
612 return;
613
614 init_completion(&snapshot->dump_gate);
615 INIT_LIST_HEAD(&snapshot->obj_list);
616 INIT_LIST_HEAD(&snapshot->cp_list);
617 INIT_WORK(&snapshot->work, kgsl_snapshot_save_frozen_objs);
618
619 snapshot->start = device->snapshot_memory.ptr;
620 snapshot->ptr = device->snapshot_memory.ptr;
621 snapshot->remain = device->snapshot_memory.size;
622 atomic_set(&snapshot->sysfs_read, 0);
623
624 header = (struct kgsl_snapshot_header *) snapshot->ptr;
625
626 header->magic = SNAPSHOT_MAGIC;
627 header->gpuid = kgsl_gpuid(device, &header->chipid);
628
629 snapshot->ptr += sizeof(*header);
630 snapshot->remain -= sizeof(*header);
631 snapshot->size += sizeof(*header);
632
633 /* Build the Linux specific header */
634 kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_OS,
635 snapshot, snapshot_os, NULL);
636
637 /* Get the device specific sections */
638 if (device->ftbl->snapshot)
639 device->ftbl->snapshot(device, snapshot, context);
640
641 /*
642 * The timestamp is the seconds since boot so it is easier to match to
643 * the kernel log
644 */
645
646 getboottime(&boot);
647 snapshot->timestamp = get_seconds() - boot.tv_sec;
648
649 /* Store the instance in the device until it gets dumped */
650 device->snapshot = snapshot;
651
652 /* log buffer info to aid in ramdump fault tolerance */
653 pa = __pa(device->snapshot_memory.ptr);
654 KGSL_DRV_ERR(device, "snapshot created at pa %pa size %zd\n",
655 &pa, snapshot->size);
656
657 sysfs_notify(&device->snapshot_kobj, NULL, "timestamp");
658
659 /*
660 * Queue a work item that will save the IB data in snapshot into
661 * static memory to prevent loss of data due to overwriting of
662 * memory.
663 *
664 */
665 kgsl_schedule_work(&snapshot->work);
666}
667EXPORT_SYMBOL(kgsl_device_snapshot);
668
669/* An attribute for showing snapshot details */
670struct kgsl_snapshot_attribute {
671 struct attribute attr;
672 ssize_t (*show)(struct kgsl_device *device, char *buf);
673 ssize_t (*store)(struct kgsl_device *device, const char *buf,
674 size_t count);
675};
676
677/**
678 * kgsl_snapshot_process_ib_obj_list() - Go through the list of IB's which need
679 * to be dumped for snapshot and move them to the global snapshot list so
680 * they will get dumped when the global list is dumped
681 * @device: device being snapshotted
682 */
683static void kgsl_snapshot_process_ib_obj_list(struct kgsl_snapshot *snapshot)
684{
685 struct kgsl_snapshot_cp_obj *obj, *obj_temp;
686 struct adreno_ib_object *ib_obj;
687 int i;
688
689 list_for_each_entry_safe(obj, obj_temp, &snapshot->cp_list,
690 node) {
691 for (i = 0; i < obj->ib_obj_list->num_objs; i++) {
692 ib_obj = &(obj->ib_obj_list->obj_list[i]);
693 kgsl_snapshot_get_object(snapshot, ib_obj->entry->priv,
694 ib_obj->gpuaddr, ib_obj->size,
695 ib_obj->snapshot_obj_type);
696 }
697 list_del(&obj->node);
698 adreno_ib_destroy_obj_list(obj->ib_obj_list);
699 kfree(obj);
700 }
701}
702
703#define to_snapshot_attr(a) \
704container_of(a, struct kgsl_snapshot_attribute, attr)
705
706#define kobj_to_device(a) \
707container_of(a, struct kgsl_device, snapshot_kobj)
708
709/* Dump the sysfs binary data to the user */
710static ssize_t snapshot_show(struct file *filep, struct kobject *kobj,
711 struct bin_attribute *attr, char *buf, loff_t off,
712 size_t count)
713{
714 struct kgsl_device *device = kobj_to_device(kobj);
715 struct kgsl_snapshot *snapshot;
716 struct kgsl_snapshot_object *obj, *tmp;
717 struct kgsl_snapshot_section_header head;
718 struct snapshot_obj_itr itr;
719 int ret;
720
721 if (device == NULL)
722 return 0;
723
724 mutex_lock(&device->mutex);
725 snapshot = device->snapshot;
726 if (snapshot != NULL)
727 atomic_inc(&snapshot->sysfs_read);
728 mutex_unlock(&device->mutex);
729
730 /* Return nothing if we haven't taken a snapshot yet */
731 if (snapshot == NULL)
732 return 0;
733
734 /*
735 * Wait for the dump worker to finish. This is interruptible
736 * to allow userspace to bail if things go horribly wrong.
737 */
738 ret = wait_for_completion_interruptible(&snapshot->dump_gate);
739 if (ret) {
740 atomic_dec(&snapshot->sysfs_read);
741 return ret;
742 }
743
744 obj_itr_init(&itr, buf, off, count);
745
746 ret = obj_itr_out(&itr, snapshot->start, snapshot->size);
747 if (ret == 0)
748 goto done;
749
750 /* Dump the memory pool if it exists */
751 if (snapshot->mempool) {
752 ret = obj_itr_out(&itr, snapshot->mempool,
753 snapshot->mempool_size);
754 if (ret == 0)
755 goto done;
756 }
757
758 {
759 head.magic = SNAPSHOT_SECTION_MAGIC;
760 head.id = KGSL_SNAPSHOT_SECTION_END;
761 head.size = sizeof(head);
762
763 obj_itr_out(&itr, &head, sizeof(head));
764 }
765
766 /*
767 * Make sure everything has been written out before destroying things.
768 * The best way to confirm this is to go all the way through without
769 * writing any bytes - so only release if we get this far and
770 * itr->write is 0 and there are no concurrent reads pending
771 */
772
773 if (itr.write == 0) {
774 bool snapshot_free = false;
775
776 mutex_lock(&device->mutex);
777 if (atomic_dec_and_test(&snapshot->sysfs_read)) {
778 device->snapshot = NULL;
779 snapshot_free = true;
780 }
781 mutex_unlock(&device->mutex);
782
783 if (snapshot_free) {
784 list_for_each_entry_safe(obj, tmp,
785 &snapshot->obj_list, node)
786 kgsl_snapshot_put_object(obj);
787
788 if (snapshot->mempool)
789 vfree(snapshot->mempool);
790
791 kfree(snapshot);
792 KGSL_CORE_ERR("snapshot: objects released\n");
793 }
794 return 0;
795 }
796
797done:
798 atomic_dec(&snapshot->sysfs_read);
799 return itr.write;
800}
801
802/* Show the total number of hangs since device boot */
803static ssize_t faultcount_show(struct kgsl_device *device, char *buf)
804{
805 return snprintf(buf, PAGE_SIZE, "%d\n", device->snapshot_faultcount);
806}
807
808/* Reset the total number of hangs since device boot */
809static ssize_t faultcount_store(struct kgsl_device *device, const char *buf,
810 size_t count)
811{
812 if (device && count > 0)
813 device->snapshot_faultcount = 0;
814
815 return count;
816}
817
818/* Show the force_panic request status */
819static ssize_t force_panic_show(struct kgsl_device *device, char *buf)
820{
821 return snprintf(buf, PAGE_SIZE, "%d\n", device->force_panic);
822}
823
824/* Store the panic request value to force_panic */
825static ssize_t force_panic_store(struct kgsl_device *device, const char *buf,
826 size_t count)
827{
828 unsigned int val = 0;
829 int ret;
830
831 if (device && count > 0)
832 device->force_panic = 0;
833
834 ret = kgsl_sysfs_store(buf, &val);
835
836 if (!ret && device)
837 device->force_panic = (bool)val;
838
839 return (ssize_t) ret < 0 ? ret : count;
840}
841
842/* Show the snapshot_crashdumper request status */
843static ssize_t snapshot_crashdumper_show(struct kgsl_device *device, char *buf)
844{
845 return snprintf(buf, PAGE_SIZE, "%d\n", device->snapshot_crashdumper);
846}
847
848
849/* Store the value to snapshot_crashdumper*/
850static ssize_t snapshot_crashdumper_store(struct kgsl_device *device,
851 const char *buf, size_t count)
852{
853 unsigned int val = 0;
854 int ret;
855
856 if (device && count > 0)
857 device->snapshot_crashdumper = 1;
858
859 ret = kgsl_sysfs_store(buf, &val);
860
861 if (!ret && device)
862 device->snapshot_crashdumper = (bool)val;
863
864 return (ssize_t) ret < 0 ? ret : count;
865}
866
867/* Show the timestamp of the last collected snapshot */
868static ssize_t timestamp_show(struct kgsl_device *device, char *buf)
869{
870 unsigned long timestamp =
871 device->snapshot ? device->snapshot->timestamp : 0;
872
873 return snprintf(buf, PAGE_SIZE, "%lu\n", timestamp);
874}
875
876static struct bin_attribute snapshot_attr = {
877 .attr.name = "dump",
878 .attr.mode = 0444,
879 .size = 0,
880 .read = snapshot_show
881};
882
883#define SNAPSHOT_ATTR(_name, _mode, _show, _store) \
884struct kgsl_snapshot_attribute attr_##_name = { \
885 .attr = { .name = __stringify(_name), .mode = _mode }, \
886 .show = _show, \
887 .store = _store, \
888}
889
890static SNAPSHOT_ATTR(timestamp, 0444, timestamp_show, NULL);
891static SNAPSHOT_ATTR(faultcount, 0644, faultcount_show, faultcount_store);
892static SNAPSHOT_ATTR(force_panic, 0644, force_panic_show, force_panic_store);
893static SNAPSHOT_ATTR(snapshot_crashdumper, 0644, snapshot_crashdumper_show,
894 snapshot_crashdumper_store);
895
896static ssize_t snapshot_sysfs_show(struct kobject *kobj,
897 struct attribute *attr, char *buf)
898{
899 struct kgsl_snapshot_attribute *pattr = to_snapshot_attr(attr);
900 struct kgsl_device *device = kobj_to_device(kobj);
901 ssize_t ret;
902
903 if (device && pattr->show)
904 ret = pattr->show(device, buf);
905 else
906 ret = -EIO;
907
908 return ret;
909}
910
911static ssize_t snapshot_sysfs_store(struct kobject *kobj,
912 struct attribute *attr, const char *buf, size_t count)
913{
914 struct kgsl_snapshot_attribute *pattr = to_snapshot_attr(attr);
915 struct kgsl_device *device = kobj_to_device(kobj);
916 ssize_t ret;
917
918 if (device && pattr->store)
919 ret = pattr->store(device, buf, count);
920 else
921 ret = -EIO;
922
923 return ret;
924}
925
926static const struct sysfs_ops snapshot_sysfs_ops = {
927 .show = snapshot_sysfs_show,
928 .store = snapshot_sysfs_store,
929};
930
931static struct kobj_type ktype_snapshot = {
932 .sysfs_ops = &snapshot_sysfs_ops,
933};
934
935/**
936 * kgsl_device_snapshot_init() - add resources for the device GPU snapshot
937 * @device: The device to initialize
938 *
939 * Allocate memory for a GPU snapshot for the specified device,
940 * and create the sysfs files to manage it
941 */
942int kgsl_device_snapshot_init(struct kgsl_device *device)
943{
944 int ret;
945
946 if (kgsl_property_read_u32(device, "qcom,snapshot-size",
947 (unsigned int *) &(device->snapshot_memory.size)))
948 device->snapshot_memory.size = KGSL_SNAPSHOT_MEMSIZE;
949
950 /*
951 * Choosing a memory size of 0 is essentially the same as disabling
952 * snapshotting
953 */
954 if (device->snapshot_memory.size == 0)
955 return 0;
956
957 /*
958 * I'm not sure why anybody would choose to do so but make sure
959 * that we can at least fit the snapshot header in the requested
960 * region
961 */
962 if (device->snapshot_memory.size < sizeof(struct kgsl_snapshot_header))
963 device->snapshot_memory.size =
964 sizeof(struct kgsl_snapshot_header);
965
966 device->snapshot_memory.ptr = kzalloc(device->snapshot_memory.size,
967 GFP_KERNEL);
968
969 if (device->snapshot_memory.ptr == NULL)
970 return -ENOMEM;
971
972 device->snapshot = NULL;
973 device->snapshot_faultcount = 0;
974 device->force_panic = 0;
975 device->snapshot_crashdumper = 1;
976
977 ret = kobject_init_and_add(&device->snapshot_kobj, &ktype_snapshot,
978 &device->dev->kobj, "snapshot");
979 if (ret)
980 goto done;
981
982 ret = sysfs_create_bin_file(&device->snapshot_kobj, &snapshot_attr);
983 if (ret)
984 goto done;
985
986 ret = sysfs_create_file(&device->snapshot_kobj, &attr_timestamp.attr);
987 if (ret)
988 goto done;
989
990 ret = sysfs_create_file(&device->snapshot_kobj, &attr_faultcount.attr);
991 if (ret)
992 goto done;
993
994 ret = sysfs_create_file(&device->snapshot_kobj,
995 &attr_force_panic.attr);
996 if (ret)
997 goto done;
998
999 ret = sysfs_create_file(&device->snapshot_kobj,
1000 &attr_snapshot_crashdumper.attr);
1001done:
1002 return ret;
1003}
1004EXPORT_SYMBOL(kgsl_device_snapshot_init);
1005
1006/**
1007 * kgsl_device_snapshot_close() - take down snapshot memory for a device
1008 * @device: Pointer to the kgsl_device
1009 *
1010 * Remove the sysfs files and free the memory allocated for the GPU
1011 * snapshot
1012 */
1013void kgsl_device_snapshot_close(struct kgsl_device *device)
1014{
1015 sysfs_remove_bin_file(&device->snapshot_kobj, &snapshot_attr);
1016 sysfs_remove_file(&device->snapshot_kobj, &attr_timestamp.attr);
1017
1018 kobject_put(&device->snapshot_kobj);
1019
1020 kfree(device->snapshot_memory.ptr);
1021
1022 device->snapshot_memory.ptr = NULL;
1023 device->snapshot_memory.size = 0;
1024 device->snapshot_faultcount = 0;
1025 device->force_panic = 0;
1026 device->snapshot_crashdumper = 1;
1027}
1028EXPORT_SYMBOL(kgsl_device_snapshot_close);
1029
1030/**
1031 * kgsl_snapshot_add_ib_obj_list() - Add a IB object list to the snapshot
1032 * object list
1033 * @device: the device that is being snapshotted
1034 * @ib_obj_list: The IB list that has objects required to execute an IB
1035 * @num_objs: Number of IB objects
1036 * @ptbase: The pagetable base in which the IB is mapped
1037 *
1038 * Adds a new IB to the list of IB objects maintained when getting snapshot
1039 * Returns 0 on success else -ENOMEM on error
1040 */
1041int kgsl_snapshot_add_ib_obj_list(struct kgsl_snapshot *snapshot,
1042 struct adreno_ib_object_list *ib_obj_list)
1043{
1044 struct kgsl_snapshot_cp_obj *obj;
1045
1046 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
1047 if (!obj)
1048 return -ENOMEM;
1049 obj->ib_obj_list = ib_obj_list;
1050 list_add(&obj->node, &snapshot->cp_list);
1051 return 0;
1052}
1053
1054static size_t _mempool_add_object(struct kgsl_snapshot *snapshot, u8 *data,
1055 struct kgsl_snapshot_object *obj)
1056{
1057 struct kgsl_snapshot_section_header *section =
1058 (struct kgsl_snapshot_section_header *)data;
1059 struct kgsl_snapshot_gpu_object_v2 *header =
1060 (struct kgsl_snapshot_gpu_object_v2 *)(data + sizeof(*section));
1061 u8 *dest = data + sizeof(*section) + sizeof(*header);
1062 uint64_t size;
1063
1064 size = obj->size;
1065
1066 if (!kgsl_memdesc_map(&obj->entry->memdesc)) {
1067 KGSL_CORE_ERR("snapshot: failed to map GPU object\n");
1068 return 0;
1069 }
1070
1071 section->magic = SNAPSHOT_SECTION_MAGIC;
1072 section->id = KGSL_SNAPSHOT_SECTION_GPU_OBJECT_V2;
1073 section->size = size + sizeof(*header) + sizeof(*section);
1074
1075 header->size = size >> 2;
1076 header->gpuaddr = obj->gpuaddr;
1077 header->ptbase =
1078 kgsl_mmu_pagetable_get_ttbr0(obj->entry->priv->pagetable);
1079 header->type = obj->type;
1080
1081 if (kgsl_addr_range_overlap(obj->gpuaddr, obj->size,
1082 snapshot->ib1base, snapshot->ib1size))
1083 snapshot->ib1dumped = true;
1084
1085 if (kgsl_addr_range_overlap(obj->gpuaddr, obj->size,
1086 snapshot->ib2base, snapshot->ib2size))
1087 snapshot->ib2dumped = true;
1088
1089 memcpy(dest, obj->entry->memdesc.hostptr + obj->offset, size);
1090 kgsl_memdesc_unmap(&obj->entry->memdesc);
1091
1092 return section->size;
1093}
1094
1095/**
1096 * kgsl_snapshot_save_frozen_objs() - Save the objects frozen in snapshot into
1097 * memory so that the data reported in these objects is correct when snapshot
1098 * is taken
1099 * @work: The work item that scheduled this work
1100 */
1101void kgsl_snapshot_save_frozen_objs(struct work_struct *work)
1102{
1103 struct kgsl_snapshot *snapshot = container_of(work,
1104 struct kgsl_snapshot, work);
1105 struct kgsl_device *device = kgsl_get_device(KGSL_DEVICE_3D0);
1106 struct kgsl_snapshot_object *obj, *tmp;
1107 size_t size = 0;
1108 void *ptr;
1109
1110 if (IS_ERR_OR_NULL(device))
1111 return;
1112
1113 kgsl_snapshot_process_ib_obj_list(snapshot);
1114
1115 list_for_each_entry(obj, &snapshot->obj_list, node) {
1116 obj->size = ALIGN(obj->size, 4);
1117
1118 size += ((size_t) obj->size +
1119 sizeof(struct kgsl_snapshot_gpu_object_v2) +
1120 sizeof(struct kgsl_snapshot_section_header));
1121 }
1122
1123 if (size == 0)
1124 goto done;
1125
1126 snapshot->mempool = vmalloc(size);
1127
1128 ptr = snapshot->mempool;
1129 snapshot->mempool_size = 0;
1130
1131 /* even if vmalloc fails, make sure we clean up the obj_list */
1132 list_for_each_entry_safe(obj, tmp, &snapshot->obj_list, node) {
1133 if (snapshot->mempool) {
1134 size_t ret = _mempool_add_object(snapshot, ptr, obj);
1135
1136 ptr += ret;
1137 snapshot->mempool_size += ret;
1138 }
1139
1140 kgsl_snapshot_put_object(obj);
1141 }
1142done:
1143 /*
1144 * Get rid of the process struct here, so that it doesn't sit
1145 * around until someone bothers to read the snapshot file.
1146 */
1147 kgsl_process_private_put(snapshot->process);
1148 snapshot->process = NULL;
1149
1150 if (snapshot->ib1base && !snapshot->ib1dumped)
1151 KGSL_DRV_ERR(device,
1152 "snapshot: Active IB1:%016llx not dumped\n",
1153 snapshot->ib1base);
1154 else if (snapshot->ib2base && !snapshot->ib2dumped)
1155 KGSL_DRV_ERR(device,
1156 "snapshot: Active IB2:%016llx not dumped\n",
1157 snapshot->ib2base);
1158
1159 complete_all(&snapshot->dump_gate);
1160 BUG_ON(device->force_panic);
1161}