blob: 76ca669f0c8ee1555eb9e8c705f1e74e89f47961 [file] [log] [blame]
Christian Könige409b122013-08-13 11:56:53 +02001/*
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: Christian König <christian.koenig@amd.com>
23 */
24
25#include <drm/drmP.h>
26#include "radeon.h"
27#include "radeon_asic.h"
28#include "r600d.h"
29
30/**
31 * uvd_v1_0_get_rptr - get read pointer
32 *
33 * @rdev: radeon_device pointer
34 * @ring: radeon_ring pointer
35 *
36 * Returns the current hardware read pointer
37 */
38uint32_t uvd_v1_0_get_rptr(struct radeon_device *rdev,
39 struct radeon_ring *ring)
40{
41 return RREG32(UVD_RBC_RB_RPTR);
42}
43
44/**
45 * uvd_v1_0_get_wptr - get write pointer
46 *
47 * @rdev: radeon_device pointer
48 * @ring: radeon_ring pointer
49 *
50 * Returns the current hardware write pointer
51 */
52uint32_t uvd_v1_0_get_wptr(struct radeon_device *rdev,
53 struct radeon_ring *ring)
54{
55 return RREG32(UVD_RBC_RB_WPTR);
56}
57
58/**
59 * uvd_v1_0_set_wptr - set write pointer
60 *
61 * @rdev: radeon_device pointer
62 * @ring: radeon_ring pointer
63 *
64 * Commits the write pointer to the hardware
65 */
66void uvd_v1_0_set_wptr(struct radeon_device *rdev,
67 struct radeon_ring *ring)
68{
69 WREG32(UVD_RBC_RB_WPTR, ring->wptr);
70}
71
72/**
73 * uvd_v1_0_init - start and test UVD block
74 *
75 * @rdev: radeon_device pointer
76 *
77 * Initialize the hardware, boot up the VCPU and do some testing
78 */
79int uvd_v1_0_init(struct radeon_device *rdev)
80{
81 struct radeon_ring *ring = &rdev->ring[R600_RING_TYPE_UVD_INDEX];
82 uint32_t tmp;
83 int r;
84
85 /* raise clocks while booting up the VCPU */
86 radeon_set_uvd_clocks(rdev, 53300, 40000);
87
88 uvd_v1_0_start(rdev);
89
90 ring->ready = true;
91 r = radeon_ring_test(rdev, R600_RING_TYPE_UVD_INDEX, ring);
92 if (r) {
93 ring->ready = false;
94 goto done;
95 }
96
97 r = radeon_ring_lock(rdev, ring, 10);
98 if (r) {
99 DRM_ERROR("radeon: ring failed to lock UVD ring (%d).\n", r);
100 goto done;
101 }
102
103 tmp = PACKET0(UVD_SEMA_WAIT_FAULT_TIMEOUT_CNTL, 0);
104 radeon_ring_write(ring, tmp);
105 radeon_ring_write(ring, 0xFFFFF);
106
107 tmp = PACKET0(UVD_SEMA_WAIT_INCOMPLETE_TIMEOUT_CNTL, 0);
108 radeon_ring_write(ring, tmp);
109 radeon_ring_write(ring, 0xFFFFF);
110
111 tmp = PACKET0(UVD_SEMA_SIGNAL_INCOMPLETE_TIMEOUT_CNTL, 0);
112 radeon_ring_write(ring, tmp);
113 radeon_ring_write(ring, 0xFFFFF);
114
115 /* Clear timeout status bits */
116 radeon_ring_write(ring, PACKET0(UVD_SEMA_TIMEOUT_STATUS, 0));
117 radeon_ring_write(ring, 0x8);
118
119 radeon_ring_write(ring, PACKET0(UVD_SEMA_CNTL, 0));
120 radeon_ring_write(ring, 3);
121
122 radeon_ring_unlock_commit(rdev, ring);
123
124done:
125 /* lower clocks again */
126 radeon_set_uvd_clocks(rdev, 0, 0);
127
128 if (!r)
129 DRM_INFO("UVD initialized successfully.\n");
130
131 return r;
132}
133
134/**
135 * uvd_v1_0_fini - stop the hardware block
136 *
137 * @rdev: radeon_device pointer
138 *
139 * Stop the UVD block, mark ring as not ready any more
140 */
141void uvd_v1_0_fini(struct radeon_device *rdev)
142{
143 struct radeon_ring *ring = &rdev->ring[R600_RING_TYPE_UVD_INDEX];
144
145 uvd_v1_0_stop(rdev);
146 ring->ready = false;
147}
148
149/**
150 * uvd_v1_0_start - start UVD block
151 *
152 * @rdev: radeon_device pointer
153 *
154 * Setup and start the UVD block
155 */
156int uvd_v1_0_start(struct radeon_device *rdev)
157{
158 struct radeon_ring *ring = &rdev->ring[R600_RING_TYPE_UVD_INDEX];
159 uint32_t rb_bufsz;
160 int i, j, r;
161
162 /* disable byte swapping */
163 u32 lmi_swap_cntl = 0;
164 u32 mp_swap_cntl = 0;
165
166 /* disable clock gating */
167 WREG32(UVD_CGC_GATE, 0);
168
169 /* disable interupt */
170 WREG32_P(UVD_MASTINT_EN, 0, ~(1 << 1));
171
172 /* Stall UMC and register bus before resetting VCPU */
173 WREG32_P(UVD_LMI_CTRL2, 1 << 8, ~(1 << 8));
174 WREG32_P(UVD_RB_ARB_CTRL, 1 << 3, ~(1 << 3));
175 mdelay(1);
176
177 /* put LMI, VCPU, RBC etc... into reset */
178 WREG32(UVD_SOFT_RESET, LMI_SOFT_RESET | VCPU_SOFT_RESET |
179 LBSI_SOFT_RESET | RBC_SOFT_RESET | CSM_SOFT_RESET |
180 CXW_SOFT_RESET | TAP_SOFT_RESET | LMI_UMC_SOFT_RESET);
181 mdelay(5);
182
183 /* take UVD block out of reset */
184 WREG32_P(SRBM_SOFT_RESET, 0, ~SOFT_RESET_UVD);
185 mdelay(5);
186
187 /* initialize UVD memory controller */
188 WREG32(UVD_LMI_CTRL, 0x40 | (1 << 8) | (1 << 13) |
189 (1 << 21) | (1 << 9) | (1 << 20));
190
191#ifdef __BIG_ENDIAN
192 /* swap (8 in 32) RB and IB */
193 lmi_swap_cntl = 0xa;
194 mp_swap_cntl = 0;
195#endif
196 WREG32(UVD_LMI_SWAP_CNTL, lmi_swap_cntl);
197 WREG32(UVD_MP_SWAP_CNTL, mp_swap_cntl);
198
199 WREG32(UVD_MPC_SET_MUXA0, 0x40c2040);
200 WREG32(UVD_MPC_SET_MUXA1, 0x0);
201 WREG32(UVD_MPC_SET_MUXB0, 0x40c2040);
202 WREG32(UVD_MPC_SET_MUXB1, 0x0);
203 WREG32(UVD_MPC_SET_ALU, 0);
204 WREG32(UVD_MPC_SET_MUX, 0x88);
205
206 /* take all subblocks out of reset, except VCPU */
207 WREG32(UVD_SOFT_RESET, VCPU_SOFT_RESET);
208 mdelay(5);
209
210 /* enable VCPU clock */
211 WREG32(UVD_VCPU_CNTL, 1 << 9);
212
213 /* enable UMC */
214 WREG32_P(UVD_LMI_CTRL2, 0, ~(1 << 8));
215
216 /* boot up the VCPU */
217 WREG32(UVD_SOFT_RESET, 0);
218 mdelay(10);
219
220 WREG32_P(UVD_RB_ARB_CTRL, 0, ~(1 << 3));
221
222 for (i = 0; i < 10; ++i) {
223 uint32_t status;
224 for (j = 0; j < 100; ++j) {
225 status = RREG32(UVD_STATUS);
226 if (status & 2)
227 break;
228 mdelay(10);
229 }
230 r = 0;
231 if (status & 2)
232 break;
233
234 DRM_ERROR("UVD not responding, trying to reset the VCPU!!!\n");
235 WREG32_P(UVD_SOFT_RESET, VCPU_SOFT_RESET, ~VCPU_SOFT_RESET);
236 mdelay(10);
237 WREG32_P(UVD_SOFT_RESET, 0, ~VCPU_SOFT_RESET);
238 mdelay(10);
239 r = -1;
240 }
241
242 if (r) {
243 DRM_ERROR("UVD not responding, giving up!!!\n");
244 return r;
245 }
246
247 /* enable interupt */
248 WREG32_P(UVD_MASTINT_EN, 3<<1, ~(3 << 1));
249
250 /* force RBC into idle state */
251 WREG32(UVD_RBC_RB_CNTL, 0x11010101);
252
253 /* Set the write pointer delay */
254 WREG32(UVD_RBC_RB_WPTR_CNTL, 0);
255
256 /* programm the 4GB memory segment for rptr and ring buffer */
257 WREG32(UVD_LMI_EXT40_ADDR, upper_32_bits(ring->gpu_addr) |
258 (0x7 << 16) | (0x1 << 31));
259
260 /* Initialize the ring buffer's read and write pointers */
261 WREG32(UVD_RBC_RB_RPTR, 0x0);
262
263 ring->wptr = ring->rptr = RREG32(UVD_RBC_RB_RPTR);
264 WREG32(UVD_RBC_RB_WPTR, ring->wptr);
265
266 /* set the ring address */
267 WREG32(UVD_RBC_RB_BASE, ring->gpu_addr);
268
269 /* Set ring buffer size */
270 rb_bufsz = drm_order(ring->ring_size);
271 rb_bufsz = (0x1 << 8) | rb_bufsz;
272 WREG32_P(UVD_RBC_RB_CNTL, rb_bufsz, ~0x11f1f);
273
274 return 0;
275}
276
277/**
278 * uvd_v1_0_stop - stop UVD block
279 *
280 * @rdev: radeon_device pointer
281 *
282 * stop the UVD block
283 */
284void uvd_v1_0_stop(struct radeon_device *rdev)
285{
286 /* force RBC into idle state */
287 WREG32(UVD_RBC_RB_CNTL, 0x11010101);
288
289 /* Stall UMC and register bus before resetting VCPU */
290 WREG32_P(UVD_LMI_CTRL2, 1 << 8, ~(1 << 8));
291 WREG32_P(UVD_RB_ARB_CTRL, 1 << 3, ~(1 << 3));
292 mdelay(1);
293
294 /* put VCPU into reset */
295 WREG32(UVD_SOFT_RESET, VCPU_SOFT_RESET);
296 mdelay(5);
297
298 /* disable VCPU clock */
299 WREG32(UVD_VCPU_CNTL, 0x0);
300
301 /* Unstall UMC and register bus */
302 WREG32_P(UVD_LMI_CTRL2, 0, ~(1 << 8));
303 WREG32_P(UVD_RB_ARB_CTRL, 0, ~(1 << 3));
304}
305
306/**
307 * uvd_v1_0_ring_test - register write test
308 *
309 * @rdev: radeon_device pointer
310 * @ring: radeon_ring pointer
311 *
312 * Test if we can successfully write to the context register
313 */
314int uvd_v1_0_ring_test(struct radeon_device *rdev, struct radeon_ring *ring)
315{
316 uint32_t tmp = 0;
317 unsigned i;
318 int r;
319
320 WREG32(UVD_CONTEXT_ID, 0xCAFEDEAD);
321 r = radeon_ring_lock(rdev, ring, 3);
322 if (r) {
323 DRM_ERROR("radeon: cp failed to lock ring %d (%d).\n",
324 ring->idx, r);
325 return r;
326 }
327 radeon_ring_write(ring, PACKET0(UVD_CONTEXT_ID, 0));
328 radeon_ring_write(ring, 0xDEADBEEF);
329 radeon_ring_unlock_commit(rdev, ring);
330 for (i = 0; i < rdev->usec_timeout; i++) {
331 tmp = RREG32(UVD_CONTEXT_ID);
332 if (tmp == 0xDEADBEEF)
333 break;
334 DRM_UDELAY(1);
335 }
336
337 if (i < rdev->usec_timeout) {
338 DRM_INFO("ring test on %d succeeded in %d usecs\n",
339 ring->idx, i);
340 } else {
341 DRM_ERROR("radeon: ring %d test failed (0x%08X)\n",
342 ring->idx, tmp);
343 r = -EINVAL;
344 }
345 return r;
346}
347
348/**
349 * uvd_v1_0_semaphore_emit - emit semaphore command
350 *
351 * @rdev: radeon_device pointer
352 * @ring: radeon_ring pointer
353 * @semaphore: semaphore to emit commands for
354 * @emit_wait: true if we should emit a wait command
355 *
356 * Emit a semaphore command (either wait or signal) to the UVD ring.
357 */
358void uvd_v1_0_semaphore_emit(struct radeon_device *rdev,
359 struct radeon_ring *ring,
360 struct radeon_semaphore *semaphore,
361 bool emit_wait)
362{
363 uint64_t addr = semaphore->gpu_addr;
364
365 radeon_ring_write(ring, PACKET0(UVD_SEMA_ADDR_LOW, 0));
366 radeon_ring_write(ring, (addr >> 3) & 0x000FFFFF);
367
368 radeon_ring_write(ring, PACKET0(UVD_SEMA_ADDR_HIGH, 0));
369 radeon_ring_write(ring, (addr >> 23) & 0x000FFFFF);
370
371 radeon_ring_write(ring, PACKET0(UVD_SEMA_CMD, 0));
372 radeon_ring_write(ring, emit_wait ? 1 : 0);
373}
374
375/**
376 * uvd_v1_0_ib_execute - execute indirect buffer
377 *
378 * @rdev: radeon_device pointer
379 * @ib: indirect buffer to execute
380 *
381 * Write ring commands to execute the indirect buffer
382 */
383void uvd_v1_0_ib_execute(struct radeon_device *rdev, struct radeon_ib *ib)
384{
385 struct radeon_ring *ring = &rdev->ring[ib->ring];
386
387 radeon_ring_write(ring, PACKET0(UVD_RBC_IB_BASE, 0));
388 radeon_ring_write(ring, ib->gpu_addr);
389 radeon_ring_write(ring, PACKET0(UVD_RBC_IB_SIZE, 0));
390 radeon_ring_write(ring, ib->length_dw);
391}
392
393/**
394 * uvd_v1_0_ib_test - test ib execution
395 *
396 * @rdev: radeon_device pointer
397 * @ring: radeon_ring pointer
398 *
399 * Test if we can successfully execute an IB
400 */
401int uvd_v1_0_ib_test(struct radeon_device *rdev, struct radeon_ring *ring)
402{
403 struct radeon_fence *fence = NULL;
404 int r;
405
406 r = radeon_set_uvd_clocks(rdev, 53300, 40000);
407 if (r) {
408 DRM_ERROR("radeon: failed to raise UVD clocks (%d).\n", r);
409 return r;
410 }
411
412 r = radeon_uvd_get_create_msg(rdev, ring->idx, 1, NULL);
413 if (r) {
414 DRM_ERROR("radeon: failed to get create msg (%d).\n", r);
415 goto error;
416 }
417
418 r = radeon_uvd_get_destroy_msg(rdev, ring->idx, 1, &fence);
419 if (r) {
420 DRM_ERROR("radeon: failed to get destroy ib (%d).\n", r);
421 goto error;
422 }
423
424 r = radeon_fence_wait(fence, false);
425 if (r) {
426 DRM_ERROR("radeon: fence wait failed (%d).\n", r);
427 goto error;
428 }
429 DRM_INFO("ib test on ring %d succeeded\n", ring->idx);
430error:
431 radeon_fence_unref(&fence);
432 radeon_set_uvd_clocks(rdev, 0, 0);
433 return r;
434}