]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/msm/adreno/adreno_gpu.c
8caa7f1f012d3c32304786b8c51defa534bcc266
[linux.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 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 "adreno_gpu.h"
21 #include "msm_gem.h"
22 #include "msm_mmu.h"
23
24 #define RB_SIZE    SZ_32K
25 #define RB_BLKSIZE 32
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 = adreno_gpu->base.fast_rate;
49                 return 0;
50         case MSM_PARAM_TIMESTAMP:
51                 if (adreno_gpu->funcs->get_timestamp) {
52                         int ret;
53
54                         pm_runtime_get_sync(&gpu->pdev->dev);
55                         ret = adreno_gpu->funcs->get_timestamp(gpu, value);
56                         pm_runtime_put_autosuspend(&gpu->pdev->dev);
57
58                         return ret;
59                 }
60                 return -EINVAL;
61         default:
62                 DBG("%s: invalid param: %u", gpu->name, param);
63                 return -EINVAL;
64         }
65 }
66
67 static int adreno_load_fw(struct adreno_gpu *adreno_gpu)
68 {
69         struct drm_device *drm = adreno_gpu->base.dev;
70         int ret;
71
72         if (adreno_gpu->pm4)
73                 return 0;
74
75         ret = request_firmware(&adreno_gpu->pm4, adreno_gpu->info->pm4fw, drm->dev);
76         if (ret) {
77                 dev_err(drm->dev, "failed to load %s PM4 firmware: %d\n",
78                                 adreno_gpu->info->pm4fw, ret);
79                 return ret;
80         }
81
82         ret = request_firmware(&adreno_gpu->pfp, adreno_gpu->info->pfpfw, drm->dev);
83         if (ret) {
84                 dev_err(drm->dev, "failed to load %s PFP firmware: %d\n",
85                                 adreno_gpu->info->pfpfw, ret);
86                 release_firmware(adreno_gpu->pm4);
87                 adreno_gpu->pm4 = NULL;
88                 return ret;
89         }
90
91         return 0;
92 }
93
94 int adreno_hw_init(struct msm_gpu *gpu)
95 {
96         struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
97         int ret;
98
99         DBG("%s", gpu->name);
100
101         ret = adreno_load_fw(adreno_gpu);
102         if (ret)
103                 return ret;
104
105         ret = msm_gem_get_iova(gpu->rb->bo, gpu->aspace, &gpu->rb_iova);
106         if (ret) {
107                 gpu->rb_iova = 0;
108                 dev_err(gpu->dev->dev, "could not map ringbuffer: %d\n", ret);
109                 return ret;
110         }
111
112         /* reset ringbuffer: */
113         gpu->rb->cur = gpu->rb->start;
114
115         /* reset completed fence seqno: */
116         adreno_gpu->memptrs->fence = gpu->fctx->completed_fence;
117         adreno_gpu->memptrs->rptr  = 0;
118
119         /* Setup REG_CP_RB_CNTL: */
120         adreno_gpu_write(adreno_gpu, REG_ADRENO_CP_RB_CNTL,
121                         /* size is log2(quad-words): */
122                         AXXX_CP_RB_CNTL_BUFSZ(ilog2(gpu->rb->size / 8)) |
123                         AXXX_CP_RB_CNTL_BLKSZ(ilog2(RB_BLKSIZE / 8)) |
124                         (adreno_is_a430(adreno_gpu) ? AXXX_CP_RB_CNTL_NO_UPDATE : 0));
125
126         /* Setup ringbuffer address: */
127         adreno_gpu_write64(adreno_gpu, REG_ADRENO_CP_RB_BASE,
128                 REG_ADRENO_CP_RB_BASE_HI, gpu->rb_iova);
129
130         if (!adreno_is_a430(adreno_gpu)) {
131                 adreno_gpu_write64(adreno_gpu, REG_ADRENO_CP_RB_RPTR_ADDR,
132                         REG_ADRENO_CP_RB_RPTR_ADDR_HI,
133                         rbmemptr(adreno_gpu, rptr));
134         }
135
136         return 0;
137 }
138
139 static uint32_t get_wptr(struct msm_ringbuffer *ring)
140 {
141         return ring->cur - ring->start;
142 }
143
144 /* Use this helper to read rptr, since a430 doesn't update rptr in memory */
145 static uint32_t get_rptr(struct adreno_gpu *adreno_gpu)
146 {
147         if (adreno_is_a430(adreno_gpu))
148                 return adreno_gpu->memptrs->rptr = adreno_gpu_read(
149                         adreno_gpu, REG_ADRENO_CP_RB_RPTR);
150         else
151                 return adreno_gpu->memptrs->rptr;
152 }
153
154 uint32_t adreno_last_fence(struct msm_gpu *gpu)
155 {
156         struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
157         return adreno_gpu->memptrs->fence;
158 }
159
160 void adreno_recover(struct msm_gpu *gpu)
161 {
162         struct drm_device *dev = gpu->dev;
163         int ret;
164
165         // XXX pm-runtime??  we *need* the device to be off after this
166         // so maybe continuing to call ->pm_suspend/resume() is better?
167
168         gpu->funcs->pm_suspend(gpu);
169         gpu->funcs->pm_resume(gpu);
170
171         ret = msm_gpu_hw_init(gpu);
172         if (ret) {
173                 dev_err(dev->dev, "gpu hw init failed: %d\n", ret);
174                 /* hmm, oh well? */
175         }
176 }
177
178 void adreno_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit,
179                 struct msm_file_private *ctx)
180 {
181         struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
182         struct msm_drm_private *priv = gpu->dev->dev_private;
183         struct msm_ringbuffer *ring = gpu->rb;
184         unsigned i;
185
186         for (i = 0; i < submit->nr_cmds; i++) {
187                 switch (submit->cmd[i].type) {
188                 case MSM_SUBMIT_CMD_IB_TARGET_BUF:
189                         /* ignore IB-targets */
190                         break;
191                 case MSM_SUBMIT_CMD_CTX_RESTORE_BUF:
192                         /* ignore if there has not been a ctx switch: */
193                         if (priv->lastctx == ctx)
194                                 break;
195                 case MSM_SUBMIT_CMD_BUF:
196                         OUT_PKT3(ring, adreno_is_a430(adreno_gpu) ?
197                                 CP_INDIRECT_BUFFER_PFE : CP_INDIRECT_BUFFER_PFD, 2);
198                         OUT_RING(ring, submit->cmd[i].iova);
199                         OUT_RING(ring, submit->cmd[i].size);
200                         OUT_PKT2(ring);
201                         break;
202                 }
203         }
204
205         OUT_PKT0(ring, REG_AXXX_CP_SCRATCH_REG2, 1);
206         OUT_RING(ring, submit->fence->seqno);
207
208         if (adreno_is_a3xx(adreno_gpu) || adreno_is_a4xx(adreno_gpu)) {
209                 /* Flush HLSQ lazy updates to make sure there is nothing
210                  * pending for indirect loads after the timestamp has
211                  * passed:
212                  */
213                 OUT_PKT3(ring, CP_EVENT_WRITE, 1);
214                 OUT_RING(ring, HLSQ_FLUSH);
215
216                 OUT_PKT3(ring, CP_WAIT_FOR_IDLE, 1);
217                 OUT_RING(ring, 0x00000000);
218         }
219
220         OUT_PKT3(ring, CP_EVENT_WRITE, 3);
221         OUT_RING(ring, CACHE_FLUSH_TS);
222         OUT_RING(ring, rbmemptr(adreno_gpu, fence));
223         OUT_RING(ring, submit->fence->seqno);
224
225         /* we could maybe be clever and only CP_COND_EXEC the interrupt: */
226         OUT_PKT3(ring, CP_INTERRUPT, 1);
227         OUT_RING(ring, 0x80000000);
228
229         /* Workaround for missing irq issue on 8x16/a306.  Unsure if the
230          * root cause is a platform issue or some a306 quirk, but this
231          * keeps things humming along:
232          */
233         if (adreno_is_a306(adreno_gpu)) {
234                 OUT_PKT3(ring, CP_WAIT_FOR_IDLE, 1);
235                 OUT_RING(ring, 0x00000000);
236                 OUT_PKT3(ring, CP_INTERRUPT, 1);
237                 OUT_RING(ring, 0x80000000);
238         }
239
240 #if 0
241         if (adreno_is_a3xx(adreno_gpu)) {
242                 /* Dummy set-constant to trigger context rollover */
243                 OUT_PKT3(ring, CP_SET_CONSTANT, 2);
244                 OUT_RING(ring, CP_REG(REG_A3XX_HLSQ_CL_KERNEL_GROUP_X_REG));
245                 OUT_RING(ring, 0x00000000);
246         }
247 #endif
248
249         gpu->funcs->flush(gpu);
250 }
251
252 void adreno_flush(struct msm_gpu *gpu)
253 {
254         struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
255         uint32_t wptr;
256
257         /*
258          * Mask wptr value that we calculate to fit in the HW range. This is
259          * to account for the possibility that the last command fit exactly into
260          * the ringbuffer and rb->next hasn't wrapped to zero yet
261          */
262         wptr = get_wptr(gpu->rb) & ((gpu->rb->size / 4) - 1);
263
264         /* ensure writes to ringbuffer have hit system memory: */
265         mb();
266
267         adreno_gpu_write(adreno_gpu, REG_ADRENO_CP_RB_WPTR, wptr);
268 }
269
270 bool adreno_idle(struct msm_gpu *gpu)
271 {
272         struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
273         uint32_t wptr = get_wptr(gpu->rb);
274
275         /* wait for CP to drain ringbuffer: */
276         if (!spin_until(get_rptr(adreno_gpu) == wptr))
277                 return true;
278
279         /* TODO maybe we need to reset GPU here to recover from hang? */
280         DRM_ERROR("%s: timeout waiting to drain ringbuffer!\n", gpu->name);
281         return false;
282 }
283
284 #ifdef CONFIG_DEBUG_FS
285 void adreno_show(struct msm_gpu *gpu, struct seq_file *m)
286 {
287         struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
288         int i;
289
290         seq_printf(m, "revision: %d (%d.%d.%d.%d)\n",
291                         adreno_gpu->info->revn, adreno_gpu->rev.core,
292                         adreno_gpu->rev.major, adreno_gpu->rev.minor,
293                         adreno_gpu->rev.patchid);
294
295         seq_printf(m, "fence:    %d/%d\n", adreno_gpu->memptrs->fence,
296                         gpu->fctx->last_fence);
297         seq_printf(m, "rptr:     %d\n", get_rptr(adreno_gpu));
298         seq_printf(m, "rb wptr:  %d\n", get_wptr(gpu->rb));
299
300         /* dump these out in a form that can be parsed by demsm: */
301         seq_printf(m, "IO:region %s 00000000 00020000\n", gpu->name);
302         for (i = 0; adreno_gpu->registers[i] != ~0; i += 2) {
303                 uint32_t start = adreno_gpu->registers[i];
304                 uint32_t end   = adreno_gpu->registers[i+1];
305                 uint32_t addr;
306
307                 for (addr = start; addr <= end; addr++) {
308                         uint32_t val = gpu_read(gpu, addr);
309                         seq_printf(m, "IO:R %08x %08x\n", addr<<2, val);
310                 }
311         }
312 }
313 #endif
314
315 /* Dump common gpu status and scratch registers on any hang, to make
316  * the hangcheck logs more useful.  The scratch registers seem always
317  * safe to read when GPU has hung (unlike some other regs, depending
318  * on how the GPU hung), and they are useful to match up to cmdstream
319  * dumps when debugging hangs:
320  */
321 void adreno_dump_info(struct msm_gpu *gpu)
322 {
323         struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
324
325         printk("revision: %d (%d.%d.%d.%d)\n",
326                         adreno_gpu->info->revn, adreno_gpu->rev.core,
327                         adreno_gpu->rev.major, adreno_gpu->rev.minor,
328                         adreno_gpu->rev.patchid);
329
330         printk("fence:    %d/%d\n", adreno_gpu->memptrs->fence,
331                         gpu->fctx->last_fence);
332         printk("rptr:     %d\n", get_rptr(adreno_gpu));
333         printk("rb wptr:  %d\n", get_wptr(gpu->rb));
334 }
335
336 /* would be nice to not have to duplicate the _show() stuff with printk(): */
337 void adreno_dump(struct msm_gpu *gpu)
338 {
339         struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
340         int i;
341
342         /* dump these out in a form that can be parsed by demsm: */
343         printk("IO:region %s 00000000 00020000\n", gpu->name);
344         for (i = 0; adreno_gpu->registers[i] != ~0; i += 2) {
345                 uint32_t start = adreno_gpu->registers[i];
346                 uint32_t end   = adreno_gpu->registers[i+1];
347                 uint32_t addr;
348
349                 for (addr = start; addr <= end; addr++) {
350                         uint32_t val = gpu_read(gpu, addr);
351                         printk("IO:R %08x %08x\n", addr<<2, val);
352                 }
353         }
354 }
355
356 static uint32_t ring_freewords(struct msm_gpu *gpu)
357 {
358         struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
359         uint32_t size = gpu->rb->size / 4;
360         uint32_t wptr = get_wptr(gpu->rb);
361         uint32_t rptr = get_rptr(adreno_gpu);
362         return (rptr + (size - 1) - wptr) % size;
363 }
364
365 void adreno_wait_ring(struct msm_gpu *gpu, uint32_t ndwords)
366 {
367         if (spin_until(ring_freewords(gpu) >= ndwords))
368                 DRM_ERROR("%s: timeout waiting for ringbuffer space\n", gpu->name);
369 }
370
371 int adreno_gpu_init(struct drm_device *drm, struct platform_device *pdev,
372                 struct adreno_gpu *adreno_gpu, const struct adreno_gpu_funcs *funcs)
373 {
374         struct adreno_platform_config *config = pdev->dev.platform_data;
375         struct msm_gpu_config adreno_gpu_config  = { 0 };
376         struct msm_gpu *gpu = &adreno_gpu->base;
377         int ret;
378
379         adreno_gpu->funcs = funcs;
380         adreno_gpu->info = adreno_info(config->rev);
381         adreno_gpu->gmem = adreno_gpu->info->gmem;
382         adreno_gpu->revn = adreno_gpu->info->revn;
383         adreno_gpu->rev = config->rev;
384
385         gpu->fast_rate = config->fast_rate;
386         gpu->bus_freq  = config->bus_freq;
387 #ifdef DOWNSTREAM_CONFIG_MSM_BUS_SCALING
388         gpu->bus_scale_table = config->bus_scale_table;
389 #endif
390
391         DBG("fast_rate=%u, slow_rate=27000000, bus_freq=%u",
392                         gpu->fast_rate, gpu->bus_freq);
393
394         adreno_gpu_config.ioname = "kgsl_3d0_reg_memory";
395         adreno_gpu_config.irqname = "kgsl_3d0_irq";
396
397         adreno_gpu_config.va_start = SZ_16M;
398         adreno_gpu_config.va_end = 0xffffffff;
399
400         adreno_gpu_config.ringsz = RB_SIZE;
401
402         pm_runtime_set_autosuspend_delay(&pdev->dev, DRM_MSM_INACTIVE_PERIOD);
403         pm_runtime_use_autosuspend(&pdev->dev);
404         pm_runtime_enable(&pdev->dev);
405
406         ret = msm_gpu_init(drm, pdev, &adreno_gpu->base, &funcs->base,
407                         adreno_gpu->info->name, &adreno_gpu_config);
408         if (ret)
409                 return ret;
410
411         adreno_gpu->memptrs = msm_gem_kernel_new(drm,
412                 sizeof(*adreno_gpu->memptrs), MSM_BO_UNCACHED, gpu->aspace,
413                 &adreno_gpu->memptrs_bo, &adreno_gpu->memptrs_iova);
414
415         if (IS_ERR(adreno_gpu->memptrs)) {
416                 ret = PTR_ERR(adreno_gpu->memptrs);
417                 adreno_gpu->memptrs = NULL;
418                 dev_err(drm->dev, "could not allocate memptrs: %d\n", ret);
419         }
420
421         return ret;
422 }
423
424 void adreno_gpu_cleanup(struct adreno_gpu *adreno_gpu)
425 {
426         struct msm_gpu *gpu = &adreno_gpu->base;
427
428         if (adreno_gpu->memptrs_bo) {
429                 if (adreno_gpu->memptrs)
430                         msm_gem_put_vaddr(adreno_gpu->memptrs_bo);
431
432                 if (adreno_gpu->memptrs_iova)
433                         msm_gem_put_iova(adreno_gpu->memptrs_bo, gpu->aspace);
434
435                 drm_gem_object_unreference_unlocked(adreno_gpu->memptrs_bo);
436         }
437         release_firmware(adreno_gpu->pm4);
438         release_firmware(adreno_gpu->pfp);
439
440         msm_gpu_cleanup(gpu);
441 }