OSDN Git Service

Merge android-4.4.157 (c139ea66) into msm-4.4
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / drivers / gpu / msm / adreno_snapshot.c
1 /* Copyright (c) 2012-2018 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 "kgsl.h"
14 #include "kgsl_sharedmem.h"
15 #include "kgsl_snapshot.h"
16
17 #include "adreno.h"
18 #include "adreno_pm4types.h"
19 #include "a3xx_reg.h"
20 #include "adreno_cp_parser.h"
21 #include "adreno_snapshot.h"
22 #include "adreno_a5xx.h"
23
24 #define VPC_MEMORY_BANKS 4
25
26 /* Maintain a list of the objects we see during parsing */
27
28 #define SNAPSHOT_OBJ_BUFSIZE 64
29
30 /* Used to print error message if an IB has too many objects in it */
31 static int ib_max_objs;
32
33 struct snapshot_rb_params {
34         struct kgsl_snapshot *snapshot;
35         struct adreno_ringbuffer *rb;
36 };
37
38 /* Keep track of how many bytes are frozen after a snapshot and tell the user */
39 static size_t snapshot_frozen_objsize;
40
41 static struct kgsl_snapshot_object objbuf[SNAPSHOT_OBJ_BUFSIZE];
42
43 /* Pointer to the next open entry in the object list */
44 static unsigned int objbufptr;
45
46 static inline int adreno_rb_ctxtswitch(struct adreno_device *adreno_dev,
47                                    unsigned int *cmd)
48 {
49         return cmd[0] == cp_packet(adreno_dev, CP_NOP, 1) &&
50                 cmd[1] == KGSL_CONTEXT_TO_MEM_IDENTIFIER;
51 }
52
53 /* Push a new buffer object onto the list */
54 void kgsl_snapshot_push_object(struct kgsl_process_private *process,
55         uint64_t gpuaddr, uint64_t dwords)
56 {
57         int index;
58         struct kgsl_mem_entry *entry;
59
60         if (process == NULL)
61                 return;
62
63         /*
64          * Sometimes IBs can be reused in the same dump.  Because we parse from
65          * oldest to newest, if we come across an IB that has already been used,
66          * assume that it has been reused and update the list with the newest
67          * size.
68          */
69
70         for (index = 0; index < objbufptr; index++) {
71                 if (objbuf[index].gpuaddr == gpuaddr &&
72                         objbuf[index].entry->priv == process) {
73
74                         objbuf[index].size = max_t(uint64_t,
75                                                 objbuf[index].size,
76                                                 dwords << 2);
77                         return;
78                 }
79         }
80
81         if (objbufptr == SNAPSHOT_OBJ_BUFSIZE) {
82                 KGSL_CORE_ERR("snapshot: too many snapshot objects\n");
83                 return;
84         }
85
86         entry = kgsl_sharedmem_find(process, gpuaddr);
87         if (entry == NULL) {
88                 KGSL_CORE_ERR("snapshot: Can't find entry for 0x%016llX\n",
89                         gpuaddr);
90                 return;
91         }
92
93         if (!kgsl_gpuaddr_in_memdesc(&entry->memdesc, gpuaddr, dwords << 2)) {
94                 KGSL_CORE_ERR("snapshot: Mem entry 0x%016llX is too small\n",
95                         gpuaddr);
96                 kgsl_mem_entry_put(entry);
97                 return;
98         }
99
100         /* Put it on the list of things to parse */
101         objbuf[objbufptr].gpuaddr = gpuaddr;
102         objbuf[objbufptr].size = dwords << 2;
103         objbuf[objbufptr++].entry = entry;
104 }
105
106 /*
107  * Returns index of the specified object is already on the list of buffers
108  * to be dumped
109  */
110
111 static int find_object(uint64_t gpuaddr, struct kgsl_process_private *process)
112 {
113         int index;
114
115         for (index = 0; index < objbufptr; index++) {
116                 if (objbuf[index].gpuaddr == gpuaddr &&
117                         objbuf[index].entry->priv == process)
118                         return index;
119         }
120         return -ENOENT;
121 }
122
123 /*
124  * snapshot_freeze_obj_list() - Take a list of ib objects and freeze their
125  * memory for snapshot
126  * @snapshot: The snapshot data.
127  * @process: The process to which the IB belongs
128  * @ib_obj_list: List of the IB objects
129  *
130  * Returns 0 on success else error code
131  */
132 static int snapshot_freeze_obj_list(struct kgsl_snapshot *snapshot,
133                 struct kgsl_process_private *process,
134                 struct adreno_ib_object_list *ib_obj_list)
135 {
136         int ret = 0;
137         struct adreno_ib_object *ib_objs;
138         int i;
139
140         for (i = 0; i < ib_obj_list->num_objs; i++) {
141                 int temp_ret;
142                 int index;
143                 int freeze = 1;
144
145                 ib_objs = &(ib_obj_list->obj_list[i]);
146                 /* Make sure this object is not going to be saved statically */
147                 for (index = 0; index < objbufptr; index++) {
148                         if ((objbuf[index].gpuaddr <= ib_objs->gpuaddr) &&
149                                 ((objbuf[index].gpuaddr +
150                                 (objbuf[index].size)) >=
151                                 (ib_objs->gpuaddr + ib_objs->size)) &&
152                                 (objbuf[index].entry->priv == process)) {
153                                 freeze = 0;
154                                 break;
155                         }
156                 }
157
158                 if (freeze) {
159                         temp_ret = kgsl_snapshot_get_object(snapshot,
160                                             process, ib_objs->gpuaddr,
161                                             ib_objs->size,
162                                             ib_objs->snapshot_obj_type);
163                         if (temp_ret < 0) {
164                                 if (ret >= 0)
165                                         ret = temp_ret;
166                         } else {
167                                 snapshot_frozen_objsize += temp_ret;
168                         }
169                 }
170         }
171         return ret;
172 }
173
174 /*
175  * We want to store the last executed IB1 and IB2 in the static region to ensure
176  * that we get at least some information out of the snapshot even if we can't
177  * access the dynamic data from the sysfs file.  Push all other IBs on the
178  * dynamic list
179  */
180 static inline void parse_ib(struct kgsl_device *device,
181                 struct kgsl_snapshot *snapshot,
182                 struct kgsl_process_private *process,
183                 uint64_t gpuaddr, uint64_t dwords)
184 {
185         struct adreno_ib_object_list *ib_obj_list;
186
187         /*
188          * Check the IB address - if it is either the last executed IB1
189          * then push it into the static blob otherwise put it in the dynamic
190          * list
191          */
192         if (gpuaddr == snapshot->ib1base) {
193                 kgsl_snapshot_push_object(process, gpuaddr, dwords);
194                 return;
195         }
196
197         if (kgsl_snapshot_have_object(snapshot, process,
198                                         gpuaddr, dwords << 2))
199                 return;
200
201         if (-E2BIG == adreno_ib_create_object_list(device, process,
202                                 gpuaddr, dwords, snapshot->ib2base,
203                                 &ib_obj_list))
204                 ib_max_objs = 1;
205
206         if (ib_obj_list)
207                 kgsl_snapshot_add_ib_obj_list(snapshot, ib_obj_list);
208
209 }
210
211 static inline bool iommu_is_setstate_addr(struct kgsl_device *device,
212                 uint64_t gpuaddr, uint64_t size)
213 {
214         struct kgsl_iommu *iommu = KGSL_IOMMU_PRIV(device);
215
216         if (kgsl_mmu_get_mmutype(device) != KGSL_MMU_TYPE_IOMMU)
217                 return false;
218
219         return kgsl_gpuaddr_in_memdesc(&iommu->setstate, gpuaddr,
220                         size);
221 }
222
223 static void dump_all_ibs(struct kgsl_device *device,
224                         struct adreno_ringbuffer *rb,
225                         struct kgsl_snapshot *snapshot)
226 {
227         int index = 0;
228         unsigned int *rbptr;
229         struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
230
231         rbptr = rb->buffer_desc.hostptr;
232
233         for (index = 0; index < KGSL_RB_DWORDS;) {
234
235                 if (adreno_cmd_is_ib(adreno_dev, rbptr[index])) {
236                         uint64_t ibaddr;
237                         uint64_t ibsize;
238
239                         if (ADRENO_LEGACY_PM4(adreno_dev)) {
240                                 ibaddr = rbptr[index + 1];
241                                 ibsize = rbptr[index + 2];
242                                 index += 3;
243                         } else {
244                                 ibaddr = rbptr[index + 2];
245                                 ibaddr = ibaddr << 32 | rbptr[index + 1];
246                                 ibsize = rbptr[index + 3];
247                                 index += 4;
248                         }
249
250                         /* Don't parse known global IBs */
251                         if (iommu_is_setstate_addr(device, ibaddr, ibsize))
252                                 continue;
253
254                         if (kgsl_gpuaddr_in_memdesc(&adreno_dev->pwron_fixup,
255                                 ibaddr, ibsize))
256                                 continue;
257
258                         parse_ib(device, snapshot, snapshot->process, ibaddr,
259                                 ibsize);
260                 } else
261                         index = index + 1;
262         }
263 }
264
265 /**
266  * snapshot_rb_ibs() - Dump rb data and capture the IB's in the RB as well
267  * @device: Pointer to a KGSL device
268  * @rb: The RB to dump
269  * @data: Pointer to memory where the RB data is to be dumped
270  * @snapshot: Pointer to information about the current snapshot being taken
271  */
272 static void snapshot_rb_ibs(struct kgsl_device *device,
273                 struct adreno_ringbuffer *rb,
274                 struct kgsl_snapshot *snapshot)
275 {
276         struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
277         unsigned int rptr, *rbptr;
278         int index, i;
279         int parse_ibs = 0, ib_parse_start;
280
281         /* Get the current read pointers for the RB */
282         adreno_readreg(adreno_dev, ADRENO_REG_CP_RB_RPTR, &rptr);
283
284         /*
285          * Figure out the window of ringbuffer data to dump.  First we need to
286          * find where the last processed IB ws submitted.  Start walking back
287          * from the rptr
288          */
289
290         index = rptr;
291         rbptr = rb->buffer_desc.hostptr;
292
293         do {
294                 index--;
295
296                 if (index < 0) {
297                         if (ADRENO_LEGACY_PM4(adreno_dev))
298                                 index = KGSL_RB_DWORDS - 3;
299                         else
300                                 index = KGSL_RB_DWORDS - 4;
301
302                         /* We wrapped without finding what we wanted */
303                         if (index < rb->wptr) {
304                                 index = rb->wptr;
305                                 break;
306                         }
307                 }
308
309                 if (adreno_cmd_is_ib(adreno_dev, rbptr[index])) {
310                         if (ADRENO_LEGACY_PM4(adreno_dev)) {
311                                 if (rbptr[index + 1] == snapshot->ib1base)
312                                         break;
313                         } else {
314                                 uint64_t ibaddr;
315
316                                 ibaddr = rbptr[index + 2];
317                                 ibaddr = ibaddr << 32 | rbptr[index + 1];
318                                 if (ibaddr == snapshot->ib1base)
319                                         break;
320                         }
321                 }
322         } while (index != rb->wptr);
323
324         /*
325          * If the ib1 was not found, for example, if ib1base was restored
326          * incorrectly after preemption, then simply dump the entire
327          * ringbuffer along with all the IBs in the ringbuffer.
328          */
329
330         if (index == rb->wptr) {
331                 dump_all_ibs(device, rb, snapshot);
332                 return;
333         }
334
335         /*
336          * index points at the last submitted IB. We can only trust that the
337          * memory between the context switch and the hanging IB is valid, so
338          * the next step is to find the context switch before the submission
339          */
340
341         while (index != rb->wptr) {
342                 index--;
343
344                 if (index < 0) {
345                         index = KGSL_RB_DWORDS - 2;
346
347                         /*
348                          * Wrapped without finding the context switch. This is
349                          * harmless - we should still have enough data to dump a
350                          * valid state
351                          */
352
353                         if (index < rb->wptr) {
354                                 index = rb->wptr;
355                                 break;
356                         }
357                 }
358
359                 /* Break if the current packet is a context switch identifier */
360                 if ((rbptr[index] == cp_packet(adreno_dev, CP_NOP, 1)) &&
361                         (rbptr[index + 1] == KGSL_CONTEXT_TO_MEM_IDENTIFIER))
362                         break;
363         }
364
365         /*
366          * Index represents the start of the window of interest.  We will try
367          * to dump all buffers between here and the rptr
368          */
369
370         ib_parse_start = index;
371
372         /*
373          * Loop through the RB, looking for indirect buffers and MMU pagetable
374          * changes
375          */
376
377         index = rb->wptr;
378         for (i = 0; i < KGSL_RB_DWORDS; i++) {
379                 /*
380                  * Only parse IBs between the start and the rptr or the next
381                  * context switch, whichever comes first
382                  */
383
384                 if (parse_ibs == 0 && index == ib_parse_start)
385                         parse_ibs = 1;
386                 else if (index == rptr || adreno_rb_ctxtswitch(adreno_dev,
387                                                         &rbptr[index]))
388                         parse_ibs = 0;
389
390                 if (parse_ibs && adreno_cmd_is_ib(adreno_dev, rbptr[index])) {
391                         uint64_t ibaddr;
392                         uint64_t ibsize;
393
394                         if (ADRENO_LEGACY_PM4(adreno_dev)) {
395                                 ibaddr = rbptr[index + 1];
396                                 ibsize = rbptr[index + 2];
397                         } else {
398                                 ibaddr = rbptr[index + 2];
399                                 ibaddr = ibaddr << 32 | rbptr[index + 1];
400                                 ibsize = rbptr[index + 3];
401                         }
402
403                         index = (index + 1) % KGSL_RB_DWORDS;
404
405                         /* Don't parse known global IBs */
406                         if (iommu_is_setstate_addr(device, ibaddr, ibsize))
407                                 continue;
408
409                         if (kgsl_gpuaddr_in_memdesc(&adreno_dev->pwron_fixup,
410                                 ibaddr, ibsize))
411                                 continue;
412
413                         parse_ib(device, snapshot, snapshot->process,
414                                 ibaddr, ibsize);
415                 } else
416                         index = (index + 1) % KGSL_RB_DWORDS;
417         }
418
419 }
420
421 /* Snapshot the ringbuffer memory */
422 static size_t snapshot_rb(struct kgsl_device *device, u8 *buf,
423         size_t remain, void *priv)
424 {
425         struct kgsl_snapshot_rb_v2 *header = (struct kgsl_snapshot_rb_v2 *)buf;
426         unsigned int *data = (unsigned int *)(buf + sizeof(*header));
427         struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
428         struct snapshot_rb_params *snap_rb_params = priv;
429         struct kgsl_snapshot *snapshot = snap_rb_params->snapshot;
430         struct adreno_ringbuffer *rb = snap_rb_params->rb;
431
432         /*
433          * Dump the entire ringbuffer - the parser can choose how much of it to
434          * process
435          */
436
437         if (remain < KGSL_RB_SIZE + sizeof(*header)) {
438                 KGSL_CORE_ERR("snapshot: Not enough memory for the rb section");
439                 return 0;
440         }
441
442         /* Write the sub-header for the section */
443         header->start = 0;
444         header->end = KGSL_RB_DWORDS;
445         header->wptr = rb->wptr;
446         header->rptr = adreno_get_rptr(rb);
447         header->rbsize = KGSL_RB_DWORDS;
448         header->count = KGSL_RB_DWORDS;
449         adreno_rb_readtimestamp(adreno_dev, rb, KGSL_TIMESTAMP_QUEUED,
450                                         &header->timestamp_queued);
451         adreno_rb_readtimestamp(adreno_dev, rb, KGSL_TIMESTAMP_RETIRED,
452                                         &header->timestamp_retired);
453         header->gpuaddr = rb->buffer_desc.gpuaddr;
454         header->id = rb->id;
455
456         if (rb == adreno_dev->cur_rb)
457                 snapshot_rb_ibs(device, rb, snapshot);
458
459         /* Just copy the ringbuffer, there are no active IBs */
460         memcpy(data, rb->buffer_desc.hostptr, KGSL_RB_SIZE);
461
462         /* Return the size of the section */
463         return KGSL_RB_SIZE + sizeof(*header);
464 }
465
466 static int _count_mem_entries(int id, void *ptr, void *data)
467 {
468         int *count = data;
469         *count = *count + 1;
470         return 0;
471 }
472
473 struct mem_entry {
474         uint64_t gpuaddr;
475         uint64_t size;
476         unsigned int type;
477 } __packed;
478
479 static int _save_mem_entries(int id, void *ptr, void *data)
480 {
481         struct kgsl_mem_entry *entry = ptr;
482         struct mem_entry *m = (struct mem_entry *) data;
483         unsigned int index = id - 1;
484
485         m[index].gpuaddr = entry->memdesc.gpuaddr;
486         m[index].size = entry->memdesc.size;
487         m[index].type = kgsl_memdesc_get_memtype(&entry->memdesc);
488
489         return 0;
490 }
491
492 static size_t snapshot_capture_mem_list(struct kgsl_device *device,
493                 u8 *buf, size_t remain, void *priv)
494 {
495         struct kgsl_snapshot_mem_list_v2 *header =
496                 (struct kgsl_snapshot_mem_list_v2 *)buf;
497         int num_mem = 0;
498         int ret = 0;
499         unsigned int *data = (unsigned int *)(buf + sizeof(*header));
500         struct kgsl_process_private *process = priv;
501
502         /* we need a process to search! */
503         if (process == NULL)
504                 return 0;
505
506         spin_lock(&process->mem_lock);
507
508         /* We need to know the number of memory objects that the process has */
509         idr_for_each(&process->mem_idr, _count_mem_entries, &num_mem);
510
511         if (num_mem == 0)
512                 goto out;
513
514         if (remain < ((num_mem * sizeof(struct mem_entry)) + sizeof(*header))) {
515                 KGSL_CORE_ERR("snapshot: Not enough memory for the mem list");
516                 goto out;
517         }
518
519         header->num_entries = num_mem;
520         header->ptbase = kgsl_mmu_pagetable_get_ttbr0(process->pagetable);
521         /*
522          * Walk throught the memory list and store the
523          * tuples(gpuaddr, size, memtype) in snapshot
524          */
525
526         idr_for_each(&process->mem_idr, _save_mem_entries, data);
527
528         ret = sizeof(*header) + (num_mem * sizeof(struct mem_entry));
529 out:
530         spin_unlock(&process->mem_lock);
531         return ret;
532 }
533
534 struct snapshot_ib_meta {
535         struct kgsl_snapshot *snapshot;
536         struct kgsl_snapshot_object *obj;
537         uint64_t ib1base;
538         uint64_t ib1size;
539         uint64_t ib2base;
540         uint64_t ib2size;
541 };
542
543 void kgsl_snapshot_add_active_ib_obj_list(struct kgsl_device *device,
544                 struct kgsl_snapshot *snapshot)
545 {
546         struct adreno_ib_object_list *ib_obj_list;
547         int index = -ENOENT;
548
549         if (!snapshot->ib1dumped)
550                 index = find_object(snapshot->ib1base, snapshot->process);
551
552         /* only do this for IB1 because the IB2's are part of IB1 objects */
553         if ((index != -ENOENT) &&
554                         (snapshot->ib1base == objbuf[index].gpuaddr)) {
555                 if (-E2BIG == adreno_ib_create_object_list(device,
556                                         objbuf[index].entry->priv,
557                                         objbuf[index].gpuaddr,
558                                         objbuf[index].size >> 2,
559                                         snapshot->ib2base,
560                                         &ib_obj_list))
561                         ib_max_objs = 1;
562                 if (ib_obj_list) {
563                         /* freeze the IB objects in the IB */
564                         snapshot_freeze_obj_list(snapshot,
565                                         objbuf[index].entry->priv,
566                                         ib_obj_list);
567                         adreno_ib_destroy_obj_list(ib_obj_list);
568                 }
569         } else {
570                 /* Get the IB2 index from parsed object */
571                 index = find_object(snapshot->ib2base, snapshot->process);
572
573                 if (index != -ENOENT)
574                         parse_ib(device, snapshot, snapshot->process,
575                                 snapshot->ib2base, objbuf[index].size >> 2);
576         }
577 }
578
579 /*
580  * active_ib_is_parsed() - Checks if active ib is already parsed
581  * @gpuaddr: Active IB base address at the time of fault
582  * @size: Active IB size
583  * @process: The process to which the IB belongs
584  *
585  * Function returns true if the active is already is parsed
586  * else false
587  */
588 static bool active_ib_is_parsed(uint64_t gpuaddr, uint64_t size,
589                 struct kgsl_process_private *process)
590 {
591         int  index;
592         /* go through the static list for gpuaddr is in list or not */
593         for (index = 0; index < objbufptr; index++) {
594                 if ((objbuf[index].gpuaddr <= gpuaddr) &&
595                                 ((objbuf[index].gpuaddr +
596                                   (objbuf[index].size)) >=
597                                  (gpuaddr + size)) &&
598                                 (objbuf[index].entry->priv == process))
599                         return true;
600         }
601         return false;
602 }
603 /* Snapshot the memory for an indirect buffer */
604 static size_t snapshot_ib(struct kgsl_device *device, u8 *buf,
605         size_t remain, void *priv)
606 {
607         struct kgsl_snapshot_ib_v2 *header = (struct kgsl_snapshot_ib_v2 *)buf;
608         struct snapshot_ib_meta *meta = priv;
609         unsigned int *src;
610         unsigned int *dst = (unsigned int *)(buf + sizeof(*header));
611         struct adreno_ib_object_list *ib_obj_list;
612         struct kgsl_snapshot *snapshot;
613         struct kgsl_snapshot_object *obj;
614         struct kgsl_memdesc *memdesc;
615
616         if (meta == NULL || meta->snapshot == NULL || meta->obj == NULL) {
617                 KGSL_CORE_ERR("snapshot: bad metadata");
618                 return 0;
619         }
620         snapshot = meta->snapshot;
621         obj = meta->obj;
622         memdesc = &obj->entry->memdesc;
623
624         /* If size is zero get it from the medesc size */
625         if (!obj->size)
626                 obj->size = (memdesc->size - (obj->gpuaddr - memdesc->gpuaddr));
627
628         if (remain < (obj->size + sizeof(*header))) {
629                 KGSL_CORE_ERR("snapshot: Not enough memory for the ib\n");
630                 return 0;
631         }
632
633         src = kgsl_gpuaddr_to_vaddr(memdesc, obj->gpuaddr);
634         if (src == NULL) {
635                 KGSL_DRV_ERR(device,
636                         "snapshot: Unable to map GPU memory object 0x%016llX into the kernel\n",
637                         obj->gpuaddr);
638                 return 0;
639         }
640
641         /* only do this for IB1 because the IB2's are part of IB1 objects */
642         if (meta->ib1base == obj->gpuaddr) {
643
644                 snapshot->ib1dumped = active_ib_is_parsed(obj->gpuaddr,
645                                         obj->size, obj->entry->priv);
646                 if (-E2BIG == adreno_ib_create_object_list(device,
647                                 obj->entry->priv,
648                                 obj->gpuaddr, obj->size >> 2,
649                                 snapshot->ib2base,
650                                 &ib_obj_list))
651                         ib_max_objs = 1;
652                 if (ib_obj_list) {
653                         /* freeze the IB objects in the IB */
654                         snapshot_freeze_obj_list(snapshot,
655                                                 obj->entry->priv,
656                                                 ib_obj_list);
657                         adreno_ib_destroy_obj_list(ib_obj_list);
658                 }
659         }
660
661
662         if (meta->ib2base == obj->gpuaddr)
663                 snapshot->ib2dumped = active_ib_is_parsed(obj->gpuaddr,
664                                         obj->size, obj->entry->priv);
665
666         /* Write the sub-header for the section */
667         header->gpuaddr = obj->gpuaddr;
668         header->ptbase =
669                 kgsl_mmu_pagetable_get_ttbr0(obj->entry->priv->pagetable);
670         header->size = obj->size >> 2;
671
672         /* Write the contents of the ib */
673         memcpy((void *)dst, (void *)src, (size_t) obj->size);
674         /* Write the contents of the ib */
675
676         return obj->size + sizeof(*header);
677 }
678
679 /* Dump another item on the current pending list */
680 static void dump_object(struct kgsl_device *device, int obj,
681                 struct kgsl_snapshot *snapshot)
682 {
683         struct snapshot_ib_meta meta;
684
685         meta.snapshot = snapshot;
686         meta.obj = &objbuf[obj];
687         meta.ib1base = snapshot->ib1base;
688         meta.ib1size = snapshot->ib1size;
689         meta.ib2base = snapshot->ib2base;
690         meta.ib2size = snapshot->ib2size;
691
692         kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_IB_V2,
693                         snapshot, snapshot_ib, &meta);
694         if (objbuf[obj].entry) {
695                 kgsl_memdesc_unmap(&(objbuf[obj].entry->memdesc));
696                 kgsl_mem_entry_put(objbuf[obj].entry);
697         }
698 }
699
700 /* setup_fault process - Find kgsl_process_private struct that caused the fault
701  *
702  * Find the faulting process based what the dispatcher thinks happened and
703  * what the hardware is using for the current pagetable. The process struct
704  * will be used to look up GPU addresses that are encountered while parsing
705  * the GPU state.
706  */
707 static void setup_fault_process(struct kgsl_device *device,
708                                 struct kgsl_snapshot *snapshot,
709                                 struct kgsl_process_private *process)
710 {
711         u64 hw_ptbase, proc_ptbase;
712
713         if (process != NULL && !kgsl_process_private_get(process))
714                 process = NULL;
715
716         /* Get the physical address of the MMU pagetable */
717         hw_ptbase = kgsl_mmu_get_current_ttbr0(&device->mmu);
718
719         /* if we have an input process, make sure the ptbases match */
720         if (process) {
721                 proc_ptbase = kgsl_mmu_pagetable_get_ttbr0(process->pagetable);
722                 /* agreement! No need to check further */
723                 if (hw_ptbase == proc_ptbase)
724                         goto done;
725
726                 kgsl_process_private_put(process);
727                 process = NULL;
728                 KGSL_CORE_ERR("snapshot: ptbase mismatch hw %llx sw %llx\n",
729                                 hw_ptbase, proc_ptbase);
730         }
731
732         /* try to find the right pagetable by walking the process list */
733         if (kgsl_mmu_is_perprocess(&device->mmu)) {
734                 struct kgsl_process_private *tmp;
735
736                 mutex_lock(&kgsl_driver.process_mutex);
737                 list_for_each_entry(tmp, &kgsl_driver.process_list, list) {
738                         u64 pt_ttbr0;
739
740                         pt_ttbr0 = kgsl_mmu_pagetable_get_ttbr0(tmp->pagetable);
741                         if ((pt_ttbr0 == hw_ptbase)
742                             && kgsl_process_private_get(tmp)) {
743                                 process = tmp;
744                                 break;
745                         }
746                 }
747                 mutex_unlock(&kgsl_driver.process_mutex);
748         }
749 done:
750         snapshot->process = process;
751 }
752
753 /* Snapshot a global memory buffer */
754 static size_t snapshot_global(struct kgsl_device *device, u8 *buf,
755         size_t remain, void *priv)
756 {
757         struct kgsl_memdesc *memdesc = priv;
758
759         struct kgsl_snapshot_gpu_object_v2 *header =
760                 (struct kgsl_snapshot_gpu_object_v2 *)buf;
761
762         u8 *ptr = buf + sizeof(*header);
763
764         if (memdesc->size == 0)
765                 return 0;
766
767         if (remain < (memdesc->size + sizeof(*header))) {
768                 KGSL_CORE_ERR("snapshot: Not enough memory for the memdesc\n");
769                 return 0;
770         }
771
772         if (memdesc->hostptr == NULL) {
773                 KGSL_CORE_ERR("snapshot: no kernel mapping for global object 0x%016llX\n",
774                                 memdesc->gpuaddr);
775                 return 0;
776         }
777
778         header->size = memdesc->size >> 2;
779         header->gpuaddr = memdesc->gpuaddr;
780         header->ptbase = MMU_DEFAULT_TTBR0(device);
781         header->type = SNAPSHOT_GPU_OBJECT_GLOBAL;
782
783         memcpy(ptr, memdesc->hostptr, memdesc->size);
784
785         return memdesc->size + sizeof(*header);
786 }
787
788 /* Snapshot IOMMU specific buffers */
789 static void adreno_snapshot_iommu(struct kgsl_device *device,
790                 struct kgsl_snapshot *snapshot)
791 {
792         struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
793         struct kgsl_iommu *iommu = KGSL_IOMMU_PRIV(device);
794
795         kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_GPU_OBJECT_V2,
796                 snapshot, snapshot_global, &iommu->setstate);
797
798         if (ADRENO_FEATURE(adreno_dev, ADRENO_PREEMPTION))
799                 kgsl_snapshot_add_section(device,
800                         KGSL_SNAPSHOT_SECTION_GPU_OBJECT_V2,
801                         snapshot, snapshot_global, &iommu->smmu_info);
802 }
803
804 static void adreno_snapshot_ringbuffer(struct kgsl_device *device,
805                 struct kgsl_snapshot *snapshot, struct adreno_ringbuffer *rb)
806 {
807         struct snapshot_rb_params params = {
808                 .snapshot = snapshot,
809                 .rb = rb,
810         };
811
812         if (rb == NULL)
813                 return;
814
815         kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_RB_V2, snapshot,
816                 snapshot_rb, &params);
817 }
818
819 /* adreno_snapshot - Snapshot the Adreno GPU state
820  * @device - KGSL device to snapshot
821  * @snapshot - Pointer to the snapshot instance
822  * @context - context that caused the fault, if known by the driver
823  * This is a hook function called by kgsl_snapshot to snapshot the
824  * Adreno specific information for the GPU snapshot.  In turn, this function
825  * calls the GPU specific snapshot function to get core specific information.
826  */
827 void adreno_snapshot(struct kgsl_device *device, struct kgsl_snapshot *snapshot,
828                         struct kgsl_context *context)
829 {
830         unsigned int i;
831         struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
832         struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
833
834         ib_max_objs = 0;
835         /* Reset the list of objects */
836         objbufptr = 0;
837
838         snapshot_frozen_objsize = 0;
839
840         setup_fault_process(device, snapshot,
841                         context ? context->proc_priv : NULL);
842
843         adreno_readreg64(adreno_dev, ADRENO_REG_CP_IB1_BASE,
844                         ADRENO_REG_CP_IB1_BASE_HI, &snapshot->ib1base);
845         adreno_readreg(adreno_dev, ADRENO_REG_CP_IB1_BUFSZ, &snapshot->ib1size);
846         adreno_readreg64(adreno_dev, ADRENO_REG_CP_IB2_BASE,
847                         ADRENO_REG_CP_IB2_BASE_HI, &snapshot->ib2base);
848         adreno_readreg(adreno_dev, ADRENO_REG_CP_IB2_BUFSZ, &snapshot->ib2size);
849
850         snapshot->ib1dumped = false;
851         snapshot->ib2dumped = false;
852
853         adreno_snapshot_ringbuffer(device, snapshot, adreno_dev->cur_rb);
854
855         /* Dump the prev ringbuffer */
856         if (adreno_dev->prev_rb != adreno_dev->cur_rb)
857                 adreno_snapshot_ringbuffer(device, snapshot,
858                         adreno_dev->prev_rb);
859
860         if ((adreno_dev->next_rb != adreno_dev->prev_rb) &&
861                  (adreno_dev->next_rb != adreno_dev->cur_rb))
862                 adreno_snapshot_ringbuffer(device, snapshot,
863                         adreno_dev->next_rb);
864
865         /* Add GPU specific sections - registers mainly, but other stuff too */
866         if (gpudev->snapshot)
867                 gpudev->snapshot(adreno_dev, snapshot);
868
869         /* Dump selected global buffers */
870         kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_GPU_OBJECT_V2,
871                         snapshot, snapshot_global, &device->memstore);
872
873         kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_GPU_OBJECT_V2,
874                         snapshot, snapshot_global,
875                         &adreno_dev->pwron_fixup);
876
877         if (kgsl_mmu_get_mmutype(device) == KGSL_MMU_TYPE_IOMMU)
878                 adreno_snapshot_iommu(device, snapshot);
879
880         /*
881          * Add a section that lists (gpuaddr, size, memtype) tuples of the
882          * hanging process
883          */
884         kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_MEMLIST_V2,
885                         snapshot, snapshot_capture_mem_list, snapshot->process);
886         /*
887          * Make sure that the last IB1 that was being executed is dumped.
888          * Since this was the last IB1 that was processed, we should have
889          * already added it to the list during the ringbuffer parse but we
890          * want to be double plus sure.
891          * The problem is that IB size from the register is the unprocessed size
892          * of the buffer not the original size, so if we didn't catch this
893          * buffer being directly used in the RB, then we might not be able to
894          * dump the whole thing. Print a warning message so we can try to
895          * figure how often this really happens.
896          */
897
898         if (-ENOENT == find_object(snapshot->ib1base, snapshot->process) &&
899                         snapshot->ib1size) {
900                 kgsl_snapshot_push_object(snapshot->process, snapshot->ib1base,
901                                 snapshot->ib1size);
902                 KGSL_CORE_ERR(
903                 "CP_IB1_BASE not found in the ringbuffer.Dumping %x dwords of the buffer.\n",
904                 snapshot->ib1size);
905         }
906
907         /*
908          * Add the last parsed IB2 to the list. The IB2 should be found as we
909          * parse the objects below, but we try to add it to the list first, so
910          * it too can be parsed.  Don't print an error message in this case - if
911          * the IB2 is found during parsing, the list will be updated with the
912          * correct size.
913          */
914
915         if (-ENOENT == find_object(snapshot->ib2base, snapshot->process)) {
916                 kgsl_snapshot_push_object(snapshot->process, snapshot->ib2base,
917                                 snapshot->ib2size);
918         }
919
920         /*
921          * Go through the list of found objects and dump each one.  As the IBs
922          * are parsed, more objects might be found, and objbufptr will increase
923          */
924         for (i = 0; i < objbufptr; i++)
925                 dump_object(device, i, snapshot);
926
927         /*
928          * Incase snapshot static blob is running out of memory, Add Active IB1
929          * and IB2 entries to obj_list so that active ib's can be dumped to
930          * snapshot dynamic blob.
931          */
932         if (!snapshot->ib1dumped || !snapshot->ib2dumped)
933                 kgsl_snapshot_add_active_ib_obj_list(device, snapshot);
934
935         if (ib_max_objs)
936                 KGSL_CORE_ERR("Max objects found in IB\n");
937         if (snapshot_frozen_objsize)
938                 KGSL_CORE_ERR("GPU snapshot froze %zdKb of GPU buffers\n",
939                         snapshot_frozen_objsize / 1024);
940
941 }
942
943 /*
944  * adreno_snapshot_cp_roq - Dump CP merciu data in snapshot
945  * @device: Device being snapshotted
946  * @remain: Bytes remaining in snapshot memory
947  * @priv: Size of merciu data in Dwords
948  */
949 size_t adreno_snapshot_cp_merciu(struct kgsl_device *device, u8 *buf,
950                 size_t remain, void *priv)
951 {
952         struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
953         struct kgsl_snapshot_debug *header = (struct kgsl_snapshot_debug *)buf;
954         unsigned int *data = (unsigned int *)(buf + sizeof(*header));
955         int i, size = *((int *)priv);
956
957         /* The MERCIU data is two dwords per entry */
958         size = size << 1;
959
960         if (remain < DEBUG_SECTION_SZ(size)) {
961                 SNAPSHOT_ERR_NOMEM(device, "CP MERCIU DEBUG");
962                 return 0;
963         }
964
965         header->type = SNAPSHOT_DEBUG_CP_MERCIU;
966         header->size = size;
967
968         adreno_writereg(adreno_dev, ADRENO_REG_CP_MERCIU_ADDR, 0x0);
969
970         for (i = 0; i < size; i++) {
971                 adreno_readreg(adreno_dev, ADRENO_REG_CP_MERCIU_DATA,
972                         &data[(i * 2)]);
973                 adreno_readreg(adreno_dev, ADRENO_REG_CP_MERCIU_DATA2,
974                         &data[(i * 2) + 1]);
975         }
976
977         return DEBUG_SECTION_SZ(size);
978 }
979
980 /*
981  * adreno_snapshot_cp_roq - Dump ROQ data in snapshot
982  * @device: Device being snapshotted
983  * @remain: Bytes remaining in snapshot memory
984  * @priv: Size of ROQ data in Dwords
985  */
986 size_t adreno_snapshot_cp_roq(struct kgsl_device *device, u8 *buf,
987                 size_t remain, void *priv)
988 {
989         struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
990         struct kgsl_snapshot_debug *header = (struct kgsl_snapshot_debug *)buf;
991         unsigned int *data = (unsigned int *)(buf + sizeof(*header));
992         int i, size = *((int *)priv);
993
994         if (remain < DEBUG_SECTION_SZ(size)) {
995                 SNAPSHOT_ERR_NOMEM(device, "CP ROQ DEBUG");
996                 return 0;
997         }
998
999         header->type = SNAPSHOT_DEBUG_CP_ROQ;
1000         header->size = size;
1001
1002         adreno_writereg(adreno_dev, ADRENO_REG_CP_ROQ_ADDR, 0x0);
1003         for (i = 0; i < size; i++)
1004                 adreno_readreg(adreno_dev, ADRENO_REG_CP_ROQ_DATA, &data[i]);
1005
1006         return DEBUG_SECTION_SZ(size);
1007 }
1008
1009 /*
1010  * adreno_snapshot_cp_pm4_ram() - Dump PM4 data in snapshot
1011  * @device: Device being snapshotted
1012  * @buf: Snapshot memory
1013  * @remain: Number of bytes left in snapshot memory
1014  * @priv: Unused
1015  */
1016 size_t adreno_snapshot_cp_pm4_ram(struct kgsl_device *device, u8 *buf,
1017                 size_t remain, void *priv)
1018 {
1019         struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1020         struct kgsl_snapshot_debug *header = (struct kgsl_snapshot_debug *)buf;
1021         unsigned int *data = (unsigned int *)(buf + sizeof(*header));
1022         int i;
1023         size_t size = adreno_dev->pm4_fw_size - 1;
1024
1025         if (remain < DEBUG_SECTION_SZ(size)) {
1026                 SNAPSHOT_ERR_NOMEM(device, "CP PM4 RAM DEBUG");
1027                 return 0;
1028         }
1029
1030         header->type = SNAPSHOT_DEBUG_CP_PM4_RAM;
1031         header->size = size;
1032
1033         /*
1034          * Read the firmware from the GPU rather than use our cache in order to
1035          * try to catch mis-programming or corruption in the hardware.  We do
1036          * use the cached version of the size, however, instead of trying to
1037          * maintain always changing hardcoded constants
1038          */
1039
1040         adreno_writereg(adreno_dev, ADRENO_REG_CP_ME_RAM_RADDR, 0x0);
1041         for (i = 0; i < size; i++)
1042                 adreno_readreg(adreno_dev, ADRENO_REG_CP_ME_RAM_DATA, &data[i]);
1043
1044         return DEBUG_SECTION_SZ(size);
1045 }
1046
1047 /*
1048  * adreno_snapshot_cp_pfp_ram() - Dump the PFP data on snapshot
1049  * @device: Device being snapshotted
1050  * @buf: Snapshot memory
1051  * @remain: Amount of butes left in snapshot memory
1052  * @priv: Unused
1053  */
1054 size_t adreno_snapshot_cp_pfp_ram(struct kgsl_device *device, u8 *buf,
1055                 size_t remain, void *priv)
1056 {
1057         struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1058         struct kgsl_snapshot_debug *header = (struct kgsl_snapshot_debug *)buf;
1059         unsigned int *data = (unsigned int *)(buf + sizeof(*header));
1060         int i, size = adreno_dev->pfp_fw_size - 1;
1061
1062         if (remain < DEBUG_SECTION_SZ(size)) {
1063                 SNAPSHOT_ERR_NOMEM(device, "CP PFP RAM DEBUG");
1064                 return 0;
1065         }
1066
1067         header->type = SNAPSHOT_DEBUG_CP_PFP_RAM;
1068         header->size = size;
1069
1070         /*
1071          * Read the firmware from the GPU rather than use our cache in order to
1072          * try to catch mis-programming or corruption in the hardware.  We do
1073          * use the cached version of the size, however, instead of trying to
1074          * maintain always changing hardcoded constants
1075          */
1076         adreno_writereg(adreno_dev, ADRENO_REG_CP_PFP_UCODE_ADDR, 0x0);
1077         for (i = 0; i < size; i++)
1078                 adreno_readreg(adreno_dev, ADRENO_REG_CP_PFP_UCODE_DATA,
1079                                 &data[i]);
1080
1081         return DEBUG_SECTION_SZ(size);
1082 }
1083
1084 /*
1085  * adreno_snapshot_vpc_memory() - Save VPC data in snapshot
1086  * @device: Device being snapshotted
1087  * @buf: Snapshot memory
1088  * @remain: Number of bytes left in snapshot memory
1089  * @priv: Private data for VPC if any
1090  */
1091 size_t adreno_snapshot_vpc_memory(struct kgsl_device *device, u8 *buf,
1092                 size_t remain, void *priv)
1093 {
1094         struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1095         struct kgsl_snapshot_debug *header = (struct kgsl_snapshot_debug *)buf;
1096         unsigned int *data = (unsigned int *)(buf + sizeof(*header));
1097         int vpc_mem_size = *((int *)priv);
1098         size_t size = VPC_MEMORY_BANKS * vpc_mem_size;
1099         int bank, addr, i = 0;
1100
1101         if (remain < DEBUG_SECTION_SZ(size)) {
1102                 SNAPSHOT_ERR_NOMEM(device, "VPC MEMORY");
1103                 return 0;
1104         }
1105
1106         header->type = SNAPSHOT_DEBUG_VPC_MEMORY;
1107         header->size = size;
1108
1109         for (bank = 0; bank < VPC_MEMORY_BANKS; bank++) {
1110                 for (addr = 0; addr < vpc_mem_size; addr++) {
1111                         unsigned int val = bank | (addr << 4);
1112                         adreno_writereg(adreno_dev,
1113                                 ADRENO_REG_VPC_DEBUG_RAM_SEL, val);
1114                         adreno_readreg(adreno_dev,
1115                                 ADRENO_REG_VPC_DEBUG_RAM_READ, &data[i++]);
1116                 }
1117         }
1118
1119         return DEBUG_SECTION_SZ(size);
1120 }
1121
1122 /*
1123  * adreno_snapshot_cp_meq() - Save CP MEQ data in snapshot
1124  * @device: Device being snapshotted
1125  * @buf: Snapshot memory
1126  * @remain: Number of bytes left in snapshot memory
1127  * @priv: Contains the size of MEQ data
1128  */
1129 size_t adreno_snapshot_cp_meq(struct kgsl_device *device, u8 *buf,
1130                 size_t remain, void *priv)
1131 {
1132         struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1133         struct kgsl_snapshot_debug *header = (struct kgsl_snapshot_debug *)buf;
1134         unsigned int *data = (unsigned int *)(buf + sizeof(*header));
1135         int i;
1136         int cp_meq_sz = *((int *)priv);
1137
1138         if (remain < DEBUG_SECTION_SZ(cp_meq_sz)) {
1139                 SNAPSHOT_ERR_NOMEM(device, "CP MEQ DEBUG");
1140                 return 0;
1141         }
1142
1143         header->type = SNAPSHOT_DEBUG_CP_MEQ;
1144         header->size = cp_meq_sz;
1145
1146         adreno_writereg(adreno_dev, ADRENO_REG_CP_MEQ_ADDR, 0x0);
1147         for (i = 0; i < cp_meq_sz; i++)
1148                 adreno_readreg(adreno_dev, ADRENO_REG_CP_MEQ_DATA, &data[i]);
1149
1150         return DEBUG_SECTION_SZ(cp_meq_sz);
1151 }
1152
1153 static const struct adreno_vbif_snapshot_registers *vbif_registers(
1154                 struct adreno_device *adreno_dev,
1155                 const struct adreno_vbif_snapshot_registers *list,
1156                 unsigned int count)
1157 {
1158         unsigned int version;
1159         unsigned int i;
1160
1161         adreno_readreg(adreno_dev, ADRENO_REG_VBIF_VERSION, &version);
1162
1163         for (i = 0; i < count; i++) {
1164                 if ((list[i].version & list[i].mask) ==
1165                                 (version & list[i].mask))
1166                         return &list[i];
1167         }
1168
1169         KGSL_CORE_ERR(
1170                 "snapshot: Registers for VBIF version %X register were not dumped\n",
1171                 version);
1172
1173         return NULL;
1174 }
1175
1176 void adreno_snapshot_registers(struct kgsl_device *device,
1177                 struct kgsl_snapshot *snapshot,
1178                 const unsigned int *regs, unsigned int count)
1179 {
1180         struct kgsl_snapshot_registers r;
1181
1182         r.regs = regs;
1183         r.count = count;
1184
1185         kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_REGS, snapshot,
1186                 kgsl_snapshot_dump_registers, &r);
1187 }
1188
1189 void adreno_snapshot_vbif_registers(struct kgsl_device *device,
1190                 struct kgsl_snapshot *snapshot,
1191                 const struct adreno_vbif_snapshot_registers *list,
1192                 unsigned int count)
1193 {
1194         struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1195         struct kgsl_snapshot_registers regs;
1196         const struct adreno_vbif_snapshot_registers *vbif;
1197
1198         vbif = vbif_registers(adreno_dev, list, count);
1199
1200         if (vbif != NULL) {
1201                 regs.regs = vbif->registers;
1202                 regs.count = vbif->count;
1203
1204                 kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_REGS,
1205                         snapshot, kgsl_snapshot_dump_registers, &regs);
1206         }
1207 }