OSDN Git Service

Merge "msm/drm: Move msm_drm_config configuration into the GPUs"
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / drivers / gpu / drm / msm / adreno / adreno_gpu.c
1 /*
2  * Copyright (C) 2013 Red Hat
3  * Author: Rob Clark <robdclark@gmail.com>
4  *
5  * Copyright (c) 2014,2016-2017 The Linux Foundation. All rights reserved.
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License version 2 as published by
9  * the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <linux/utsname.h>
21 #include "adreno_gpu.h"
22 #include "msm_snapshot.h"
23 #include "msm_gem.h"
24 #include "msm_mmu.h"
25
26
27 int adreno_get_param(struct msm_gpu *gpu, uint32_t param, uint64_t *value)
28 {
29         struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
30
31         switch (param) {
32         case MSM_PARAM_GPU_ID:
33                 *value = adreno_gpu->info->revn;
34                 return 0;
35         case MSM_PARAM_GMEM_SIZE:
36                 *value = adreno_gpu->gmem;
37                 return 0;
38         case MSM_PARAM_GMEM_BASE:
39                 *value = 0x100000;
40                 return 0;
41         case MSM_PARAM_CHIP_ID:
42                 *value = adreno_gpu->rev.patchid |
43                                 (adreno_gpu->rev.minor << 8) |
44                                 (adreno_gpu->rev.major << 16) |
45                                 (adreno_gpu->rev.core << 24);
46                 return 0;
47         case MSM_PARAM_MAX_FREQ:
48                 *value = gpu->gpufreq[gpu->active_level];
49                 return 0;
50         case MSM_PARAM_TIMESTAMP:
51                 if (adreno_gpu->funcs->get_timestamp)
52                         return adreno_gpu->funcs->get_timestamp(gpu, value);
53                 return -EINVAL;
54         default:
55                 DBG("%s: invalid param: %u", gpu->name, param);
56                 return -EINVAL;
57         }
58 }
59
60 int adreno_hw_init(struct msm_gpu *gpu)
61 {
62         struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
63         int i;
64
65         DBG("%s", gpu->name);
66
67         for (i = 0; i < gpu->nr_rings; i++) {
68                 int ret = msm_gem_get_iova(gpu->rb[i]->bo, gpu->aspace,
69                         &gpu->rb[i]->iova);
70                 if (ret) {
71                         gpu->rb[i]->iova = 0;
72                         dev_err(gpu->dev->dev,
73                                 "could not map ringbuffer %d: %d\n", i, ret);
74                         return ret;
75                 }
76         }
77
78         /*
79          * Setup REG_CP_RB_CNTL.  The same value is used across targets (with
80          * the excpetion of A430 that disables the RPTR shadow) - the cacluation
81          * for the ringbuffer size and block size is moved to msm_gpu.h for the
82          * pre-processor to deal with and the A430 variant is ORed in here
83          */
84         adreno_gpu_write(adreno_gpu, REG_ADRENO_CP_RB_CNTL,
85                 MSM_GPU_RB_CNTL_DEFAULT |
86                 (adreno_is_a430(adreno_gpu) ? AXXX_CP_RB_CNTL_NO_UPDATE : 0));
87
88         /* Setup ringbuffer address - use ringbuffer[0] for GPU init */
89         adreno_gpu_write64(adreno_gpu, REG_ADRENO_CP_RB_BASE,
90                 REG_ADRENO_CP_RB_BASE_HI, gpu->rb[0]->iova);
91
92         adreno_gpu_write64(adreno_gpu, REG_ADRENO_CP_RB_RPTR_ADDR,
93                 REG_ADRENO_CP_RB_RPTR_ADDR_HI, rbmemptr(adreno_gpu, 0, rptr));
94
95         return 0;
96 }
97
98 /* Use this helper to read rptr, since a430 doesn't update rptr in memory */
99 static uint32_t get_rptr(struct adreno_gpu *adreno_gpu,
100                 struct msm_ringbuffer *ring)
101 {
102         if (adreno_is_a430(adreno_gpu)) {
103                 /*
104                  * If index is anything but 0 this will probably break horribly,
105                  * but I think that we have enough infrastructure in place to
106                  * ensure that it won't be. If not then this is why your
107                  * a430 stopped working.
108                  */
109                 return adreno_gpu->memptrs->rptr[ring->id] = adreno_gpu_read(
110                         adreno_gpu, REG_ADRENO_CP_RB_RPTR);
111         } else
112                 return adreno_gpu->memptrs->rptr[ring->id];
113 }
114
115 struct msm_ringbuffer *adreno_active_ring(struct msm_gpu *gpu)
116 {
117         return gpu->rb[0];
118 }
119
120 uint32_t adreno_submitted_fence(struct msm_gpu *gpu,
121                 struct msm_ringbuffer *ring)
122 {
123         if (!ring)
124                 return 0;
125
126         return ring->submitted_fence;
127 }
128
129 uint32_t adreno_last_fence(struct msm_gpu *gpu, struct msm_ringbuffer *ring)
130 {
131         struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
132
133         if (!ring)
134                 return 0;
135
136         return adreno_gpu->memptrs->fence[ring->id];
137 }
138
139 void adreno_recover(struct msm_gpu *gpu)
140 {
141         struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
142         struct drm_device *dev = gpu->dev;
143         struct msm_ringbuffer *ring;
144         int ret, i;
145
146         gpu->funcs->pm_suspend(gpu);
147
148         /* reset ringbuffer(s): */
149
150         FOR_EACH_RING(gpu, ring, i) {
151                 if (!ring)
152                         continue;
153
154                 /* No need for a lock here, nobody else is peeking in */
155                 ring->cur = ring->start;
156                 ring->next = ring->start;
157
158                 /* reset completed fence seqno, discard anything pending: */
159                 adreno_gpu->memptrs->fence[ring->id] =
160                         adreno_submitted_fence(gpu, ring);
161                 adreno_gpu->memptrs->rptr[ring->id]  = 0;
162         }
163
164         gpu->funcs->pm_resume(gpu);
165
166         disable_irq(gpu->irq);
167         ret = gpu->funcs->hw_init(gpu);
168         if (ret) {
169                 dev_err(dev->dev, "gpu hw init failed: %d\n", ret);
170                 /* hmm, oh well? */
171         }
172         enable_irq(gpu->irq);
173 }
174
175 int adreno_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit)
176 {
177         struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
178         struct msm_ringbuffer *ring = gpu->rb[submit->ring];
179         unsigned i, ibs = 0;
180
181         for (i = 0; i < submit->nr_cmds; i++) {
182                 switch (submit->cmd[i].type) {
183                 case MSM_SUBMIT_CMD_IB_TARGET_BUF:
184                         /* ignore IB-targets */
185                         break;
186                 case MSM_SUBMIT_CMD_PROFILE_BUF:
187                 case MSM_SUBMIT_CMD_CTX_RESTORE_BUF:
188                                 break;
189                 case MSM_SUBMIT_CMD_BUF:
190                         OUT_PKT3(ring, adreno_is_a430(adreno_gpu) ?
191                                 CP_INDIRECT_BUFFER_PFE : CP_INDIRECT_BUFFER_PFD, 2);
192                         OUT_RING(ring, lower_32_bits(submit->cmd[i].iova));
193                         OUT_RING(ring, submit->cmd[i].size);
194                         ibs++;
195                         break;
196                 }
197         }
198
199         /* on a320, at least, we seem to need to pad things out to an
200          * even number of qwords to avoid issue w/ CP hanging on wrap-
201          * around:
202          */
203         if (ibs % 2)
204                 OUT_PKT2(ring);
205
206         OUT_PKT0(ring, REG_AXXX_CP_SCRATCH_REG2, 1);
207         OUT_RING(ring, submit->fence);
208
209         if (adreno_is_a3xx(adreno_gpu) || adreno_is_a4xx(adreno_gpu)) {
210                 /* Flush HLSQ lazy updates to make sure there is nothing
211                  * pending for indirect loads after the timestamp has
212                  * passed:
213                  */
214                 OUT_PKT3(ring, CP_EVENT_WRITE, 1);
215                 OUT_RING(ring, HLSQ_FLUSH);
216
217                 OUT_PKT3(ring, CP_WAIT_FOR_IDLE, 1);
218                 OUT_RING(ring, 0x00000000);
219         }
220
221         OUT_PKT3(ring, CP_EVENT_WRITE, 3);
222         OUT_RING(ring, CACHE_FLUSH_TS);
223         OUT_RING(ring, rbmemptr(adreno_gpu, ring->id, fence));
224         OUT_RING(ring, submit->fence);
225
226         /* we could maybe be clever and only CP_COND_EXEC the interrupt: */
227         OUT_PKT3(ring, CP_INTERRUPT, 1);
228         OUT_RING(ring, 0x80000000);
229
230         /* Workaround for missing irq issue on 8x16/a306.  Unsure if the
231          * root cause is a platform issue or some a306 quirk, but this
232          * keeps things humming along:
233          */
234         if (adreno_is_a306(adreno_gpu)) {
235                 OUT_PKT3(ring, CP_WAIT_FOR_IDLE, 1);
236                 OUT_RING(ring, 0x00000000);
237                 OUT_PKT3(ring, CP_INTERRUPT, 1);
238                 OUT_RING(ring, 0x80000000);
239         }
240
241 #if 0
242         if (adreno_is_a3xx(adreno_gpu)) {
243                 /* Dummy set-constant to trigger context rollover */
244                 OUT_PKT3(ring, CP_SET_CONSTANT, 2);
245                 OUT_RING(ring, CP_REG(REG_A3XX_HLSQ_CL_KERNEL_GROUP_X_REG));
246                 OUT_RING(ring, 0x00000000);
247         }
248 #endif
249
250         gpu->funcs->flush(gpu, ring);
251
252         return 0;
253 }
254
255 void adreno_flush(struct msm_gpu *gpu, struct msm_ringbuffer *ring)
256 {
257         struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
258         uint32_t wptr;
259
260         /* Copy the shadow to the actual register */
261         ring->cur = ring->next;
262
263         /*
264          * Mask the wptr value that we calculate to fit in the HW range. This is
265          * to account for the possibility that the last command fit exactly into
266          * the ringbuffer and rb->next hasn't wrapped to zero yet
267          */
268         wptr = get_wptr(ring);
269
270         /* ensure writes to ringbuffer have hit system memory: */
271         mb();
272
273         adreno_gpu_write(adreno_gpu, REG_ADRENO_CP_RB_WPTR, wptr);
274 }
275
276 bool adreno_idle(struct msm_gpu *gpu, struct msm_ringbuffer *ring)
277 {
278         struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
279         uint32_t wptr = get_wptr(ring);
280
281         /* wait for CP to drain ringbuffer: */
282         if (!spin_until(get_rptr(adreno_gpu, ring) == wptr))
283                 return true;
284
285         /* TODO maybe we need to reset GPU here to recover from hang? */
286         DRM_ERROR("%s: timeout waiting to drain ringbuffer %d rptr/wptr = %X/%X\n",
287                 gpu->name, ring->id, get_rptr(adreno_gpu, ring), wptr);
288
289         return false;
290 }
291
292 #ifdef CONFIG_DEBUG_FS
293 void adreno_show(struct msm_gpu *gpu, struct seq_file *m)
294 {
295         struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
296         struct msm_ringbuffer *ring;
297         int i;
298
299         seq_printf(m, "revision: %d (%d.%d.%d.%d)\n",
300                         adreno_gpu->info->revn, adreno_gpu->rev.core,
301                         adreno_gpu->rev.major, adreno_gpu->rev.minor,
302                         adreno_gpu->rev.patchid);
303
304         FOR_EACH_RING(gpu, ring, i) {
305                 if (!ring)
306                         continue;
307
308                 seq_printf(m, "rb %d: fence:    %d/%d\n", i,
309                         adreno_last_fence(gpu, ring),
310                         adreno_submitted_fence(gpu, ring));
311
312                 seq_printf(m, "      rptr:     %d\n",
313                         get_rptr(adreno_gpu, ring));
314                 seq_printf(m, "rb wptr:  %d\n", get_wptr(ring));
315         }
316
317         gpu->funcs->pm_resume(gpu);
318
319         /* dump these out in a form that can be parsed by demsm: */
320         seq_printf(m, "IO:region %s 00000000 00020000\n", gpu->name);
321         for (i = 0; adreno_gpu->registers[i] != ~0; i += 2) {
322                 uint32_t start = adreno_gpu->registers[i];
323                 uint32_t end   = adreno_gpu->registers[i+1];
324                 uint32_t addr;
325
326                 for (addr = start; addr <= end; addr++) {
327                         uint32_t val = gpu_read(gpu, addr);
328                         seq_printf(m, "IO:R %08x %08x\n", addr<<2, val);
329                 }
330         }
331
332         gpu->funcs->pm_suspend(gpu);
333 }
334 #endif
335
336 /* Dump common gpu status and scratch registers on any hang, to make
337  * the hangcheck logs more useful.  The scratch registers seem always
338  * safe to read when GPU has hung (unlike some other regs, depending
339  * on how the GPU hung), and they are useful to match up to cmdstream
340  * dumps when debugging hangs:
341  */
342 void adreno_dump_info(struct msm_gpu *gpu)
343 {
344         struct drm_device *dev = gpu->dev;
345         struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
346         struct msm_ringbuffer *ring;
347         int i;
348
349         dev_err(dev->dev, "revision: %d (%d.%d.%d.%d)\n",
350                         adreno_gpu->info->revn, adreno_gpu->rev.core,
351                         adreno_gpu->rev.major, adreno_gpu->rev.minor,
352                         adreno_gpu->rev.patchid);
353
354         FOR_EACH_RING(gpu, ring, i) {
355                 if (!ring)
356                         continue;
357
358                 dev_err(dev->dev, " ring %d: fence %d/%d rptr/wptr %x/%x\n", i,
359                         adreno_last_fence(gpu, ring),
360                         adreno_submitted_fence(gpu, ring),
361                         get_rptr(adreno_gpu, ring),
362                         get_wptr(ring));
363         }
364
365         for (i = 0; i < 8; i++) {
366                 pr_err("CP_SCRATCH_REG%d: %u\n", i,
367                         gpu_read(gpu, REG_AXXX_CP_SCRATCH_REG0 + i));
368         }
369 }
370
371 /* would be nice to not have to duplicate the _show() stuff with printk(): */
372 void adreno_dump(struct msm_gpu *gpu)
373 {
374         struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
375         int i;
376
377         /* dump these out in a form that can be parsed by demsm: */
378         printk("IO:region %s 00000000 00020000\n", gpu->name);
379         for (i = 0; adreno_gpu->registers[i] != ~0; i += 2) {
380                 uint32_t start = adreno_gpu->registers[i];
381                 uint32_t end   = adreno_gpu->registers[i+1];
382                 uint32_t addr;
383
384                 for (addr = start; addr <= end; addr++) {
385                         uint32_t val = gpu_read(gpu, addr);
386                         printk("IO:R %08x %08x\n", addr<<2, val);
387                 }
388         }
389 }
390
391 static uint32_t ring_freewords(struct msm_ringbuffer *ring)
392 {
393         struct adreno_gpu *adreno_gpu = to_adreno_gpu(ring->gpu);
394         uint32_t size = MSM_GPU_RINGBUFFER_SZ >> 2;
395         /* Use ring->next to calculate free size */
396         uint32_t wptr = ring->next - ring->start;
397         uint32_t rptr = get_rptr(adreno_gpu, ring);
398         return (rptr + (size - 1) - wptr) % size;
399 }
400
401 void adreno_wait_ring(struct msm_ringbuffer *ring, uint32_t ndwords)
402 {
403         if (spin_until(ring_freewords(ring) >= ndwords))
404                 DRM_ERROR("%s: timeout waiting for space in ringubffer %d\n",
405                         ring->gpu->name, ring->id);
406 }
407
408 /* Read the set of powerlevels */
409 static int _adreno_get_pwrlevels(struct msm_gpu *gpu, struct device_node *node)
410 {
411         struct device_node *child;
412
413         gpu->active_level = 1;
414
415         /* The device tree will tell us the best clock to initialize with */
416         of_property_read_u32(node, "qcom,initial-pwrlevel", &gpu->active_level);
417
418         if (gpu->active_level >= ARRAY_SIZE(gpu->gpufreq))
419                 gpu->active_level = 1;
420
421         for_each_child_of_node(node, child) {
422                 unsigned int index;
423
424                 if (of_property_read_u32(child, "reg", &index))
425                         return -EINVAL;
426
427                 if (index >= ARRAY_SIZE(gpu->gpufreq))
428                         continue;
429
430                 gpu->nr_pwrlevels = max(gpu->nr_pwrlevels, index + 1);
431
432                 of_property_read_u32(child, "qcom,gpu-freq",
433                         &gpu->gpufreq[index]);
434                 of_property_read_u32(child, "qcom,bus-freq",
435                         &gpu->busfreq[index]);
436         }
437
438         DBG("fast_rate=%u, slow_rate=%u, bus_freq=%u",
439                 gpu->gpufreq[gpu->active_level],
440                 gpu->gpufreq[gpu->nr_pwrlevels - 1],
441                 gpu->busfreq[gpu->active_level]);
442
443         return 0;
444 }
445
446 /*
447  * Escape valve for targets that don't define the binning nodes. Get the
448  * first powerlevel node and parse it
449  */
450 static int adreno_get_legacy_pwrlevels(struct msm_gpu *gpu,
451                 struct device_node *parent)
452 {
453         struct device_node *child;
454
455         child = of_find_node_by_name(parent, "qcom,gpu-pwrlevels");
456         if (child)
457                 return _adreno_get_pwrlevels(gpu, child);
458
459         dev_err(gpu->dev->dev, "Unable to parse any powerlevels\n");
460         return -EINVAL;
461 }
462
463 /* Get the powerlevels for the target */
464 static int adreno_get_pwrlevels(struct msm_gpu *gpu, struct device_node *parent)
465 {
466         struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
467         struct device_node *node, *child;
468
469         /* See if the target has defined a number of power bins */
470         node = of_find_node_by_name(parent, "qcom,gpu-pwrlevel-bins");
471         if (!node) {
472                 /* If not look for the qcom,gpu-pwrlevels node */
473                 return adreno_get_legacy_pwrlevels(gpu, parent);
474         }
475
476         for_each_child_of_node(node, child) {
477                 unsigned int bin;
478
479                 if (of_property_read_u32(child, "qcom,speed-bin", &bin))
480                         continue;
481
482                 /*
483                  * If the bin matches the bin specified by the fuses, then we
484                  * have a winner - parse it
485                  */
486                 if (adreno_gpu->speed_bin == bin)
487                         return _adreno_get_pwrlevels(gpu, child);
488         }
489
490         return -ENODEV;
491 }
492
493 static const struct {
494         const char *str;
495         uint32_t flag;
496 } quirks[] = {
497         { "qcom,gpu-quirk-two-pass-use-wfi", ADRENO_QUIRK_TWO_PASS_USE_WFI },
498         { "qcom,gpu-quirk-fault-detect-mask", ADRENO_QUIRK_FAULT_DETECT_MASK },
499 };
500
501 /* Parse the statistics from the device tree */
502 static int adreno_of_parse(struct platform_device *pdev, struct msm_gpu *gpu)
503 {
504         struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
505         struct device_node *node = pdev->dev.of_node;
506         int i, ret;
507
508         /* Probe the powerlevels */
509         ret = adreno_get_pwrlevels(gpu, node);
510         if (ret)
511                 return ret;
512
513         /* Check to see if any quirks were specified in the device tree */
514         for (i = 0; i < ARRAY_SIZE(quirks); i++)
515                 if (of_property_read_bool(node, quirks[i].str))
516                         adreno_gpu->quirks |= quirks[i].flag;
517
518         return 0;
519 }
520
521 int adreno_gpu_init(struct drm_device *drm, struct platform_device *pdev,
522                 struct adreno_gpu *adreno_gpu,
523                 const struct adreno_gpu_funcs *funcs,
524                 struct msm_gpu_config *gpu_config)
525 {
526         struct adreno_platform_config *config = pdev->dev.platform_data;
527         struct msm_gpu *gpu = &adreno_gpu->base;
528         struct msm_mmu *mmu;
529         int ret;
530
531         adreno_gpu->funcs = funcs;
532         adreno_gpu->info = adreno_info(config->rev);
533         adreno_gpu->gmem = adreno_gpu->info->gmem;
534         adreno_gpu->revn = adreno_gpu->info->revn;
535         adreno_gpu->rev = config->rev;
536
537         /* Get the rest of the target configuration from the device tree */
538         adreno_of_parse(pdev, gpu);
539
540         ret = msm_gpu_init(drm, pdev, &adreno_gpu->base, &funcs->base,
541                         adreno_gpu->info->name, gpu_config);
542         if (ret)
543                 return ret;
544
545         ret = request_firmware(&adreno_gpu->pm4, adreno_gpu->info->pm4fw, drm->dev);
546         if (ret) {
547                 dev_err(drm->dev, "failed to load %s PM4 firmware: %d\n",
548                                 adreno_gpu->info->pm4fw, ret);
549                 return ret;
550         }
551
552         ret = request_firmware(&adreno_gpu->pfp, adreno_gpu->info->pfpfw, drm->dev);
553         if (ret) {
554                 dev_err(drm->dev, "failed to load %s PFP firmware: %d\n",
555                                 adreno_gpu->info->pfpfw, ret);
556                 return ret;
557         }
558
559         mmu = gpu->aspace->mmu;
560         if (mmu) {
561                 ret = mmu->funcs->attach(mmu, NULL, 0);
562                 if (ret)
563                         return ret;
564         }
565
566         mutex_lock(&drm->struct_mutex);
567         adreno_gpu->memptrs_bo = msm_gem_new(drm, sizeof(*adreno_gpu->memptrs),
568                         MSM_BO_UNCACHED);
569         mutex_unlock(&drm->struct_mutex);
570         if (IS_ERR(adreno_gpu->memptrs_bo)) {
571                 ret = PTR_ERR(adreno_gpu->memptrs_bo);
572                 adreno_gpu->memptrs_bo = NULL;
573                 dev_err(drm->dev, "could not allocate memptrs: %d\n", ret);
574                 return ret;
575         }
576
577         adreno_gpu->memptrs = msm_gem_vaddr(adreno_gpu->memptrs_bo);
578         if (!adreno_gpu->memptrs) {
579                 dev_err(drm->dev, "could not vmap memptrs\n");
580                 return -ENOMEM;
581         }
582
583         ret = msm_gem_get_iova(adreno_gpu->memptrs_bo, gpu->aspace,
584                         &adreno_gpu->memptrs_iova);
585         if (ret) {
586                 dev_err(drm->dev, "could not map memptrs: %d\n", ret);
587                 return ret;
588         }
589
590         return 0;
591 }
592
593 void adreno_gpu_cleanup(struct adreno_gpu *gpu)
594 {
595         struct msm_gem_address_space *aspace = gpu->base.aspace;
596
597         if (gpu->memptrs_bo) {
598                 if (gpu->memptrs_iova)
599                         msm_gem_put_iova(gpu->memptrs_bo, aspace);
600                 drm_gem_object_unreference_unlocked(gpu->memptrs_bo);
601         }
602         release_firmware(gpu->pm4);
603         release_firmware(gpu->pfp);
604
605         msm_gpu_cleanup(&gpu->base);
606
607         if (aspace) {
608                 aspace->mmu->funcs->detach(aspace->mmu);
609                 msm_gem_address_space_put(aspace);
610         }
611 }
612
613 static void adreno_snapshot_os(struct msm_gpu *gpu,
614                 struct msm_snapshot *snapshot)
615 {
616         struct msm_snapshot_linux header;
617
618         memset(&header, 0, sizeof(header));
619
620         header.osid = SNAPSHOT_OS_LINUX_V3;
621         strlcpy(header.release, utsname()->release, sizeof(header.release));
622         strlcpy(header.version, utsname()->version, sizeof(header.version));
623
624         header.seconds = get_seconds();
625         header.ctxtcount = 0;
626
627         SNAPSHOT_HEADER(snapshot, header, SNAPSHOT_SECTION_OS, 0);
628 }
629
630 static void adreno_snapshot_ringbuffer(struct msm_gpu *gpu,
631                 struct msm_snapshot *snapshot, struct msm_ringbuffer *ring)
632 {
633         struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
634         struct msm_snapshot_ringbuffer header;
635         unsigned int i, end = 0;
636         unsigned int *data = ring->start;
637
638         memset(&header, 0, sizeof(header));
639
640         /*
641          * We only want to copy the active contents of each ring, so find the
642          * last valid entry in the ringbuffer
643          */
644         for (i = 0; i < MSM_GPU_RINGBUFFER_SZ >> 2; i++) {
645                 if (data[i])
646                         end = i;
647         }
648
649         /* The dump always starts at 0 */
650         header.start = 0;
651         header.end = end;
652
653         /* This is the number of dwords being dumped */
654         header.count = end + 1;
655
656         /* This is the size of the actual ringbuffer */
657         header.rbsize = MSM_GPU_RINGBUFFER_SZ >> 2;
658
659         header.id = ring->id;
660         header.gpuaddr = ring->iova;
661         header.rptr = get_rptr(adreno_gpu, ring);
662         header.wptr = get_wptr(ring);
663         header.timestamp_queued = adreno_submitted_fence(gpu, ring);
664         header.timestamp_retired = adreno_last_fence(gpu, ring);
665
666         /* Write the header even if the ringbuffer data is empty */
667         if (!SNAPSHOT_HEADER(snapshot, header, SNAPSHOT_SECTION_RB_V2,
668                 header.count))
669                 return;
670
671         SNAPSHOT_MEMCPY(snapshot, ring->start, header.count * sizeof(u32));
672 }
673
674 static void adreno_snapshot_ringbuffers(struct msm_gpu *gpu,
675                 struct msm_snapshot *snapshot)
676 {
677         struct msm_ringbuffer *ring;
678         int i;
679
680         /* Write a new section for each ringbuffer */
681         FOR_EACH_RING(gpu, ring, i)
682                 adreno_snapshot_ringbuffer(gpu, snapshot, ring);
683 }
684
685 void adreno_snapshot(struct msm_gpu *gpu, struct msm_snapshot *snapshot)
686 {
687         adreno_snapshot_os(gpu, snapshot);
688         adreno_snapshot_ringbuffers(gpu, snapshot);
689 }
690
691 /* Return the group struct associated with the counter id */
692
693 static struct adreno_counter_group *get_counter_group(struct msm_gpu *gpu,
694                 u32 groupid)
695 {
696         struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
697
698         if (!adreno_gpu->counter_groups)
699                 return ERR_PTR(-ENODEV);
700
701         if (groupid >= adreno_gpu->nr_counter_groups)
702                 return ERR_PTR(-ENODEV);
703
704         return (struct adreno_counter_group *)
705                 adreno_gpu->counter_groups[groupid];
706 }
707
708 int adreno_get_counter(struct msm_gpu *gpu, u32 groupid, u32 countable,
709                 u32 *lo, u32 *hi)
710 {
711         struct adreno_counter_group *group =
712                 get_counter_group(gpu, groupid);
713
714         if (!IS_ERR_OR_NULL(group) && group->funcs.get)
715                 return group->funcs.get(gpu, group, countable, lo, hi);
716
717         return -ENODEV;
718 }
719
720 u64 adreno_read_counter(struct msm_gpu *gpu, u32 groupid, int counterid)
721 {
722         struct adreno_counter_group *group =
723                 get_counter_group(gpu, groupid);
724
725         if (!IS_ERR_OR_NULL(group) && group->funcs.read)
726                 return group->funcs.read(gpu, group, counterid);
727
728         return 0;
729 }
730
731 void adreno_put_counter(struct msm_gpu *gpu, u32 groupid, int counterid)
732 {
733         struct adreno_counter_group *group =
734                 get_counter_group(gpu, groupid);
735
736         if (!IS_ERR_OR_NULL(group) && group->funcs.put)
737                 group->funcs.put(gpu, group, counterid);
738 }