]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/radeon/r600_dma.c
Merge tag 'drm/for-3.14-rc1-20140123' of git://anongit.freedesktop.org/tegra/linux...
[karo-tx-linux.git] / drivers / gpu / drm / radeon / r600_dma.c
1 /*
2  * Copyright 2013 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Alex Deucher
23  */
24 #include <drm/drmP.h>
25 #include "radeon.h"
26 #include "radeon_asic.h"
27 #include "r600d.h"
28
29 u32 r600_gpu_check_soft_reset(struct radeon_device *rdev);
30
31 /*
32  * DMA
33  * Starting with R600, the GPU has an asynchronous
34  * DMA engine.  The programming model is very similar
35  * to the 3D engine (ring buffer, IBs, etc.), but the
36  * DMA controller has it's own packet format that is
37  * different form the PM4 format used by the 3D engine.
38  * It supports copying data, writing embedded data,
39  * solid fills, and a number of other things.  It also
40  * has support for tiling/detiling of buffers.
41  */
42
43 /**
44  * r600_dma_get_rptr - get the current read pointer
45  *
46  * @rdev: radeon_device pointer
47  * @ring: radeon ring pointer
48  *
49  * Get the current rptr from the hardware (r6xx+).
50  */
51 uint32_t r600_dma_get_rptr(struct radeon_device *rdev,
52                            struct radeon_ring *ring)
53 {
54         u32 rptr;
55
56         if (rdev->wb.enabled)
57                 rptr = rdev->wb.wb[ring->rptr_offs/4];
58         else
59                 rptr = RREG32(DMA_RB_RPTR);
60
61         return (rptr & 0x3fffc) >> 2;
62 }
63
64 /**
65  * r600_dma_get_wptr - get the current write pointer
66  *
67  * @rdev: radeon_device pointer
68  * @ring: radeon ring pointer
69  *
70  * Get the current wptr from the hardware (r6xx+).
71  */
72 uint32_t r600_dma_get_wptr(struct radeon_device *rdev,
73                            struct radeon_ring *ring)
74 {
75         return (RREG32(DMA_RB_WPTR) & 0x3fffc) >> 2;
76 }
77
78 /**
79  * r600_dma_set_wptr - commit the write pointer
80  *
81  * @rdev: radeon_device pointer
82  * @ring: radeon ring pointer
83  *
84  * Write the wptr back to the hardware (r6xx+).
85  */
86 void r600_dma_set_wptr(struct radeon_device *rdev,
87                        struct radeon_ring *ring)
88 {
89         WREG32(DMA_RB_WPTR, (ring->wptr << 2) & 0x3fffc);
90 }
91
92 /**
93  * r600_dma_stop - stop the async dma engine
94  *
95  * @rdev: radeon_device pointer
96  *
97  * Stop the async dma engine (r6xx-evergreen).
98  */
99 void r600_dma_stop(struct radeon_device *rdev)
100 {
101         u32 rb_cntl = RREG32(DMA_RB_CNTL);
102
103         radeon_ttm_set_active_vram_size(rdev, rdev->mc.visible_vram_size);
104
105         rb_cntl &= ~DMA_RB_ENABLE;
106         WREG32(DMA_RB_CNTL, rb_cntl);
107
108         rdev->ring[R600_RING_TYPE_DMA_INDEX].ready = false;
109 }
110
111 /**
112  * r600_dma_resume - setup and start the async dma engine
113  *
114  * @rdev: radeon_device pointer
115  *
116  * Set up the DMA ring buffer and enable it. (r6xx-evergreen).
117  * Returns 0 for success, error for failure.
118  */
119 int r600_dma_resume(struct radeon_device *rdev)
120 {
121         struct radeon_ring *ring = &rdev->ring[R600_RING_TYPE_DMA_INDEX];
122         u32 rb_cntl, dma_cntl, ib_cntl;
123         u32 rb_bufsz;
124         int r;
125
126         /* Reset dma */
127         if (rdev->family >= CHIP_RV770)
128                 WREG32(SRBM_SOFT_RESET, RV770_SOFT_RESET_DMA);
129         else
130                 WREG32(SRBM_SOFT_RESET, SOFT_RESET_DMA);
131         RREG32(SRBM_SOFT_RESET);
132         udelay(50);
133         WREG32(SRBM_SOFT_RESET, 0);
134
135         WREG32(DMA_SEM_INCOMPLETE_TIMER_CNTL, 0);
136         WREG32(DMA_SEM_WAIT_FAIL_TIMER_CNTL, 0);
137
138         /* Set ring buffer size in dwords */
139         rb_bufsz = order_base_2(ring->ring_size / 4);
140         rb_cntl = rb_bufsz << 1;
141 #ifdef __BIG_ENDIAN
142         rb_cntl |= DMA_RB_SWAP_ENABLE | DMA_RPTR_WRITEBACK_SWAP_ENABLE;
143 #endif
144         WREG32(DMA_RB_CNTL, rb_cntl);
145
146         /* Initialize the ring buffer's read and write pointers */
147         WREG32(DMA_RB_RPTR, 0);
148         WREG32(DMA_RB_WPTR, 0);
149
150         /* set the wb address whether it's enabled or not */
151         WREG32(DMA_RB_RPTR_ADDR_HI,
152                upper_32_bits(rdev->wb.gpu_addr + R600_WB_DMA_RPTR_OFFSET) & 0xFF);
153         WREG32(DMA_RB_RPTR_ADDR_LO,
154                ((rdev->wb.gpu_addr + R600_WB_DMA_RPTR_OFFSET) & 0xFFFFFFFC));
155
156         if (rdev->wb.enabled)
157                 rb_cntl |= DMA_RPTR_WRITEBACK_ENABLE;
158
159         WREG32(DMA_RB_BASE, ring->gpu_addr >> 8);
160
161         /* enable DMA IBs */
162         ib_cntl = DMA_IB_ENABLE;
163 #ifdef __BIG_ENDIAN
164         ib_cntl |= DMA_IB_SWAP_ENABLE;
165 #endif
166         WREG32(DMA_IB_CNTL, ib_cntl);
167
168         dma_cntl = RREG32(DMA_CNTL);
169         dma_cntl &= ~CTXEMPTY_INT_ENABLE;
170         WREG32(DMA_CNTL, dma_cntl);
171
172         if (rdev->family >= CHIP_RV770)
173                 WREG32(DMA_MODE, 1);
174
175         ring->wptr = 0;
176         WREG32(DMA_RB_WPTR, ring->wptr << 2);
177
178         ring->rptr = RREG32(DMA_RB_RPTR) >> 2;
179
180         WREG32(DMA_RB_CNTL, rb_cntl | DMA_RB_ENABLE);
181
182         ring->ready = true;
183
184         r = radeon_ring_test(rdev, R600_RING_TYPE_DMA_INDEX, ring);
185         if (r) {
186                 ring->ready = false;
187                 return r;
188         }
189
190         radeon_ttm_set_active_vram_size(rdev, rdev->mc.real_vram_size);
191
192         return 0;
193 }
194
195 /**
196  * r600_dma_fini - tear down the async dma engine
197  *
198  * @rdev: radeon_device pointer
199  *
200  * Stop the async dma engine and free the ring (r6xx-evergreen).
201  */
202 void r600_dma_fini(struct radeon_device *rdev)
203 {
204         r600_dma_stop(rdev);
205         radeon_ring_fini(rdev, &rdev->ring[R600_RING_TYPE_DMA_INDEX]);
206 }
207
208 /**
209  * r600_dma_is_lockup - Check if the DMA engine is locked up
210  *
211  * @rdev: radeon_device pointer
212  * @ring: radeon_ring structure holding ring information
213  *
214  * Check if the async DMA engine is locked up.
215  * Returns true if the engine appears to be locked up, false if not.
216  */
217 bool r600_dma_is_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
218 {
219         u32 reset_mask = r600_gpu_check_soft_reset(rdev);
220
221         if (!(reset_mask & RADEON_RESET_DMA)) {
222                 radeon_ring_lockup_update(ring);
223                 return false;
224         }
225         /* force ring activities */
226         radeon_ring_force_activity(rdev, ring);
227         return radeon_ring_test_lockup(rdev, ring);
228 }
229
230
231 /**
232  * r600_dma_ring_test - simple async dma engine test
233  *
234  * @rdev: radeon_device pointer
235  * @ring: radeon_ring structure holding ring information
236  *
237  * Test the DMA engine by writing using it to write an
238  * value to memory. (r6xx-SI).
239  * Returns 0 for success, error for failure.
240  */
241 int r600_dma_ring_test(struct radeon_device *rdev,
242                        struct radeon_ring *ring)
243 {
244         unsigned i;
245         int r;
246         void __iomem *ptr = (void *)rdev->vram_scratch.ptr;
247         u32 tmp;
248
249         if (!ptr) {
250                 DRM_ERROR("invalid vram scratch pointer\n");
251                 return -EINVAL;
252         }
253
254         tmp = 0xCAFEDEAD;
255         writel(tmp, ptr);
256
257         r = radeon_ring_lock(rdev, ring, 4);
258         if (r) {
259                 DRM_ERROR("radeon: dma failed to lock ring %d (%d).\n", ring->idx, r);
260                 return r;
261         }
262         radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_WRITE, 0, 0, 1));
263         radeon_ring_write(ring, rdev->vram_scratch.gpu_addr & 0xfffffffc);
264         radeon_ring_write(ring, upper_32_bits(rdev->vram_scratch.gpu_addr) & 0xff);
265         radeon_ring_write(ring, 0xDEADBEEF);
266         radeon_ring_unlock_commit(rdev, ring);
267
268         for (i = 0; i < rdev->usec_timeout; i++) {
269                 tmp = readl(ptr);
270                 if (tmp == 0xDEADBEEF)
271                         break;
272                 DRM_UDELAY(1);
273         }
274
275         if (i < rdev->usec_timeout) {
276                 DRM_INFO("ring test on %d succeeded in %d usecs\n", ring->idx, i);
277         } else {
278                 DRM_ERROR("radeon: ring %d test failed (0x%08X)\n",
279                           ring->idx, tmp);
280                 r = -EINVAL;
281         }
282         return r;
283 }
284
285 /**
286  * r600_dma_fence_ring_emit - emit a fence on the DMA ring
287  *
288  * @rdev: radeon_device pointer
289  * @fence: radeon fence object
290  *
291  * Add a DMA fence packet to the ring to write
292  * the fence seq number and DMA trap packet to generate
293  * an interrupt if needed (r6xx-r7xx).
294  */
295 void r600_dma_fence_ring_emit(struct radeon_device *rdev,
296                               struct radeon_fence *fence)
297 {
298         struct radeon_ring *ring = &rdev->ring[fence->ring];
299         u64 addr = rdev->fence_drv[fence->ring].gpu_addr;
300
301         /* write the fence */
302         radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_FENCE, 0, 0, 0));
303         radeon_ring_write(ring, addr & 0xfffffffc);
304         radeon_ring_write(ring, (upper_32_bits(addr) & 0xff));
305         radeon_ring_write(ring, lower_32_bits(fence->seq));
306         /* generate an interrupt */
307         radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_TRAP, 0, 0, 0));
308 }
309
310 /**
311  * r600_dma_semaphore_ring_emit - emit a semaphore on the dma ring
312  *
313  * @rdev: radeon_device pointer
314  * @ring: radeon_ring structure holding ring information
315  * @semaphore: radeon semaphore object
316  * @emit_wait: wait or signal semaphore
317  *
318  * Add a DMA semaphore packet to the ring wait on or signal
319  * other rings (r6xx-SI).
320  */
321 bool r600_dma_semaphore_ring_emit(struct radeon_device *rdev,
322                                   struct radeon_ring *ring,
323                                   struct radeon_semaphore *semaphore,
324                                   bool emit_wait)
325 {
326         u64 addr = semaphore->gpu_addr;
327         u32 s = emit_wait ? 0 : 1;
328
329         radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_SEMAPHORE, 0, s, 0));
330         radeon_ring_write(ring, addr & 0xfffffffc);
331         radeon_ring_write(ring, upper_32_bits(addr) & 0xff);
332
333         return true;
334 }
335
336 /**
337  * r600_dma_ib_test - test an IB on the DMA engine
338  *
339  * @rdev: radeon_device pointer
340  * @ring: radeon_ring structure holding ring information
341  *
342  * Test a simple IB in the DMA ring (r6xx-SI).
343  * Returns 0 on success, error on failure.
344  */
345 int r600_dma_ib_test(struct radeon_device *rdev, struct radeon_ring *ring)
346 {
347         struct radeon_ib ib;
348         unsigned i;
349         int r;
350         void __iomem *ptr = (void *)rdev->vram_scratch.ptr;
351         u32 tmp = 0;
352
353         if (!ptr) {
354                 DRM_ERROR("invalid vram scratch pointer\n");
355                 return -EINVAL;
356         }
357
358         tmp = 0xCAFEDEAD;
359         writel(tmp, ptr);
360
361         r = radeon_ib_get(rdev, ring->idx, &ib, NULL, 256);
362         if (r) {
363                 DRM_ERROR("radeon: failed to get ib (%d).\n", r);
364                 return r;
365         }
366
367         ib.ptr[0] = DMA_PACKET(DMA_PACKET_WRITE, 0, 0, 1);
368         ib.ptr[1] = rdev->vram_scratch.gpu_addr & 0xfffffffc;
369         ib.ptr[2] = upper_32_bits(rdev->vram_scratch.gpu_addr) & 0xff;
370         ib.ptr[3] = 0xDEADBEEF;
371         ib.length_dw = 4;
372
373         r = radeon_ib_schedule(rdev, &ib, NULL);
374         if (r) {
375                 radeon_ib_free(rdev, &ib);
376                 DRM_ERROR("radeon: failed to schedule ib (%d).\n", r);
377                 return r;
378         }
379         r = radeon_fence_wait(ib.fence, false);
380         if (r) {
381                 DRM_ERROR("radeon: fence wait failed (%d).\n", r);
382                 return r;
383         }
384         for (i = 0; i < rdev->usec_timeout; i++) {
385                 tmp = readl(ptr);
386                 if (tmp == 0xDEADBEEF)
387                         break;
388                 DRM_UDELAY(1);
389         }
390         if (i < rdev->usec_timeout) {
391                 DRM_INFO("ib test on ring %d succeeded in %u usecs\n", ib.fence->ring, i);
392         } else {
393                 DRM_ERROR("radeon: ib test failed (0x%08X)\n", tmp);
394                 r = -EINVAL;
395         }
396         radeon_ib_free(rdev, &ib);
397         return r;
398 }
399
400 /**
401  * r600_dma_ring_ib_execute - Schedule an IB on the DMA engine
402  *
403  * @rdev: radeon_device pointer
404  * @ib: IB object to schedule
405  *
406  * Schedule an IB in the DMA ring (r6xx-r7xx).
407  */
408 void r600_dma_ring_ib_execute(struct radeon_device *rdev, struct radeon_ib *ib)
409 {
410         struct radeon_ring *ring = &rdev->ring[ib->ring];
411
412         if (rdev->wb.enabled) {
413                 u32 next_rptr = ring->wptr + 4;
414                 while ((next_rptr & 7) != 5)
415                         next_rptr++;
416                 next_rptr += 3;
417                 radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_WRITE, 0, 0, 1));
418                 radeon_ring_write(ring, ring->next_rptr_gpu_addr & 0xfffffffc);
419                 radeon_ring_write(ring, upper_32_bits(ring->next_rptr_gpu_addr) & 0xff);
420                 radeon_ring_write(ring, next_rptr);
421         }
422
423         /* The indirect buffer packet must end on an 8 DW boundary in the DMA ring.
424          * Pad as necessary with NOPs.
425          */
426         while ((ring->wptr & 7) != 5)
427                 radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_NOP, 0, 0, 0));
428         radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_INDIRECT_BUFFER, 0, 0, 0));
429         radeon_ring_write(ring, (ib->gpu_addr & 0xFFFFFFE0));
430         radeon_ring_write(ring, (ib->length_dw << 16) | (upper_32_bits(ib->gpu_addr) & 0xFF));
431
432 }
433
434 /**
435  * r600_copy_dma - copy pages using the DMA engine
436  *
437  * @rdev: radeon_device pointer
438  * @src_offset: src GPU address
439  * @dst_offset: dst GPU address
440  * @num_gpu_pages: number of GPU pages to xfer
441  * @fence: radeon fence object
442  *
443  * Copy GPU paging using the DMA engine (r6xx).
444  * Used by the radeon ttm implementation to move pages if
445  * registered as the asic copy callback.
446  */
447 int r600_copy_dma(struct radeon_device *rdev,
448                   uint64_t src_offset, uint64_t dst_offset,
449                   unsigned num_gpu_pages,
450                   struct radeon_fence **fence)
451 {
452         struct radeon_semaphore *sem = NULL;
453         int ring_index = rdev->asic->copy.dma_ring_index;
454         struct radeon_ring *ring = &rdev->ring[ring_index];
455         u32 size_in_dw, cur_size_in_dw;
456         int i, num_loops;
457         int r = 0;
458
459         r = radeon_semaphore_create(rdev, &sem);
460         if (r) {
461                 DRM_ERROR("radeon: moving bo (%d).\n", r);
462                 return r;
463         }
464
465         size_in_dw = (num_gpu_pages << RADEON_GPU_PAGE_SHIFT) / 4;
466         num_loops = DIV_ROUND_UP(size_in_dw, 0xFFFE);
467         r = radeon_ring_lock(rdev, ring, num_loops * 4 + 8);
468         if (r) {
469                 DRM_ERROR("radeon: moving bo (%d).\n", r);
470                 radeon_semaphore_free(rdev, &sem, NULL);
471                 return r;
472         }
473
474         radeon_semaphore_sync_to(sem, *fence);
475         radeon_semaphore_sync_rings(rdev, sem, ring->idx);
476
477         for (i = 0; i < num_loops; i++) {
478                 cur_size_in_dw = size_in_dw;
479                 if (cur_size_in_dw > 0xFFFE)
480                         cur_size_in_dw = 0xFFFE;
481                 size_in_dw -= cur_size_in_dw;
482                 radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_COPY, 0, 0, cur_size_in_dw));
483                 radeon_ring_write(ring, dst_offset & 0xfffffffc);
484                 radeon_ring_write(ring, src_offset & 0xfffffffc);
485                 radeon_ring_write(ring, (((upper_32_bits(dst_offset) & 0xff) << 16) |
486                                          (upper_32_bits(src_offset) & 0xff)));
487                 src_offset += cur_size_in_dw * 4;
488                 dst_offset += cur_size_in_dw * 4;
489         }
490
491         r = radeon_fence_emit(rdev, fence, ring->idx);
492         if (r) {
493                 radeon_ring_unlock_undo(rdev, ring);
494                 return r;
495         }
496
497         radeon_ring_unlock_commit(rdev, ring);
498         radeon_semaphore_free(rdev, &sem, *fence);
499
500         return r;
501 }