2 * Copyright 2013 Advanced Micro Devices, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
25 * Authors: Christian König <christian.koenig@amd.com>
28 #include <linux/firmware.h>
29 #include <linux/module.h>
34 #include "radeon_asic.h"
38 #define FIRMWARE_BONAIRE "radeon/BONAIRE_vce.bin"
40 MODULE_FIRMWARE(FIRMWARE_BONAIRE);
43 * radeon_vce_init - allocate memory, load vce firmware
45 * @rdev: radeon_device pointer
47 * First step to get VCE online, allocate memory and load the firmware
49 int radeon_vce_init(struct radeon_device *rdev)
51 static const char *fw_version = "[ATI LIB=VCEFW,";
52 static const char *fb_version = "[ATI LIB=VCEFWSTATS,";
54 const char *fw_name, *c;
55 uint8_t start, mid, end;
58 switch (rdev->family) {
62 fw_name = FIRMWARE_BONAIRE;
69 r = request_firmware(&rdev->vce_fw, fw_name, rdev->dev);
71 dev_err(rdev->dev, "radeon_vce: Can't load firmware \"%s\"\n",
76 /* search for firmware version */
78 size = rdev->vce_fw->size - strlen(fw_version) - 9;
79 c = rdev->vce_fw->data;
80 for (;size > 0; --size, ++c)
81 if (strncmp(c, fw_version, strlen(fw_version)) == 0)
87 c += strlen(fw_version);
88 if (sscanf(c, "%2hhd.%2hhd.%2hhd]", &start, &mid, &end) != 3)
91 /* search for feedback version */
93 size = rdev->vce_fw->size - strlen(fb_version) - 3;
94 c = rdev->vce_fw->data;
95 for (;size > 0; --size, ++c)
96 if (strncmp(c, fb_version, strlen(fb_version)) == 0)
102 c += strlen(fb_version);
103 if (sscanf(c, "%2u]", &rdev->vce.fb_version) != 1)
106 DRM_INFO("Found VCE firmware/feedback version %hhd.%hhd.%hhd / %d!\n",
107 start, mid, end, rdev->vce.fb_version);
109 rdev->vce.fw_version = (start << 24) | (mid << 16) | (end << 8);
111 /* we can only work with this fw version for now */
112 if (rdev->vce.fw_version != ((40 << 24) | (2 << 16) | (2 << 8)))
115 /* load firmware into VRAM */
117 size = RADEON_GPU_PAGE_ALIGN(rdev->vce_fw->size) +
118 RADEON_VCE_STACK_SIZE + RADEON_VCE_HEAP_SIZE;
119 r = radeon_bo_create(rdev, size, PAGE_SIZE, true,
120 RADEON_GEM_DOMAIN_VRAM, NULL, &rdev->vce.vcpu_bo);
122 dev_err(rdev->dev, "(%d) failed to allocate VCE bo\n", r);
126 r = radeon_vce_resume(rdev);
130 memset(rdev->vce.cpu_addr, 0, size);
131 memcpy(rdev->vce.cpu_addr, rdev->vce_fw->data, rdev->vce_fw->size);
133 r = radeon_vce_suspend(rdev);
137 for (i = 0; i < RADEON_MAX_VCE_HANDLES; ++i) {
138 atomic_set(&rdev->vce.handles[i], 0);
139 rdev->vce.filp[i] = NULL;
146 * radeon_vce_fini - free memory
148 * @rdev: radeon_device pointer
150 * Last step on VCE teardown, free firmware memory
152 void radeon_vce_fini(struct radeon_device *rdev)
154 radeon_vce_suspend(rdev);
155 radeon_bo_unref(&rdev->vce.vcpu_bo);
159 * radeon_vce_suspend - unpin VCE fw memory
161 * @rdev: radeon_device pointer
163 * TODO: Test VCE suspend/resume
165 int radeon_vce_suspend(struct radeon_device *rdev)
169 if (rdev->vce.vcpu_bo == NULL)
172 r = radeon_bo_reserve(rdev->vce.vcpu_bo, false);
174 radeon_bo_kunmap(rdev->vce.vcpu_bo);
175 radeon_bo_unpin(rdev->vce.vcpu_bo);
176 radeon_bo_unreserve(rdev->vce.vcpu_bo);
182 * radeon_vce_resume - pin VCE fw memory
184 * @rdev: radeon_device pointer
186 * TODO: Test VCE suspend/resume
188 int radeon_vce_resume(struct radeon_device *rdev)
192 if (rdev->vce.vcpu_bo == NULL)
195 r = radeon_bo_reserve(rdev->vce.vcpu_bo, false);
197 radeon_bo_unref(&rdev->vce.vcpu_bo);
198 dev_err(rdev->dev, "(%d) failed to reserve VCE bo\n", r);
202 r = radeon_bo_pin(rdev->vce.vcpu_bo, RADEON_GEM_DOMAIN_VRAM,
203 &rdev->vce.gpu_addr);
205 radeon_bo_unreserve(rdev->vce.vcpu_bo);
206 radeon_bo_unref(&rdev->vce.vcpu_bo);
207 dev_err(rdev->dev, "(%d) VCE bo pin failed\n", r);
211 r = radeon_bo_kmap(rdev->vce.vcpu_bo, &rdev->vce.cpu_addr);
213 dev_err(rdev->dev, "(%d) VCE map failed\n", r);
217 radeon_bo_unreserve(rdev->vce.vcpu_bo);
223 * radeon_vce_free_handles - free still open VCE handles
225 * @rdev: radeon_device pointer
226 * @filp: drm file pointer
228 * Close all VCE handles still open by this file pointer
230 void radeon_vce_free_handles(struct radeon_device *rdev, struct drm_file *filp)
233 for (i = 0; i < RADEON_MAX_VCE_HANDLES; ++i) {
234 uint32_t handle = atomic_read(&rdev->vce.handles[i]);
235 if (!handle || rdev->vce.filp[i] != filp)
238 r = radeon_vce_get_destroy_msg(rdev, TN_RING_TYPE_VCE1_INDEX,
241 DRM_ERROR("Error destroying VCE handle (%d)!\n", r);
243 rdev->vce.filp[i] = NULL;
244 atomic_set(&rdev->vce.handles[i], 0);
249 * radeon_vce_get_create_msg - generate a VCE create msg
251 * @rdev: radeon_device pointer
252 * @ring: ring we should submit the msg to
253 * @handle: VCE session handle to use
254 * @fence: optional fence to return
256 * Open up a stream for HW test
258 int radeon_vce_get_create_msg(struct radeon_device *rdev, int ring,
259 uint32_t handle, struct radeon_fence **fence)
261 const unsigned ib_size_dw = 1024;
266 r = radeon_ib_get(rdev, ring, &ib, NULL, ib_size_dw * 4);
268 DRM_ERROR("radeon: failed to get ib (%d).\n", r);
272 dummy = ib.gpu_addr + 1024;
274 /* stitch together an VCE create msg */
276 ib.ptr[ib.length_dw++] = 0x0000000c; /* len */
277 ib.ptr[ib.length_dw++] = 0x00000001; /* session cmd */
278 ib.ptr[ib.length_dw++] = handle;
280 ib.ptr[ib.length_dw++] = 0x00000030; /* len */
281 ib.ptr[ib.length_dw++] = 0x01000001; /* create cmd */
282 ib.ptr[ib.length_dw++] = 0x00000000;
283 ib.ptr[ib.length_dw++] = 0x00000042;
284 ib.ptr[ib.length_dw++] = 0x0000000a;
285 ib.ptr[ib.length_dw++] = 0x00000001;
286 ib.ptr[ib.length_dw++] = 0x00000080;
287 ib.ptr[ib.length_dw++] = 0x00000060;
288 ib.ptr[ib.length_dw++] = 0x00000100;
289 ib.ptr[ib.length_dw++] = 0x00000100;
290 ib.ptr[ib.length_dw++] = 0x0000000c;
291 ib.ptr[ib.length_dw++] = 0x00000000;
293 ib.ptr[ib.length_dw++] = 0x00000014; /* len */
294 ib.ptr[ib.length_dw++] = 0x05000005; /* feedback buffer */
295 ib.ptr[ib.length_dw++] = upper_32_bits(dummy);
296 ib.ptr[ib.length_dw++] = dummy;
297 ib.ptr[ib.length_dw++] = 0x00000001;
299 for (i = ib.length_dw; i < ib_size_dw; ++i)
302 r = radeon_ib_schedule(rdev, &ib, NULL);
304 DRM_ERROR("radeon: failed to schedule ib (%d).\n", r);
308 *fence = radeon_fence_ref(ib.fence);
310 radeon_ib_free(rdev, &ib);
316 * radeon_vce_get_destroy_msg - generate a VCE destroy msg
318 * @rdev: radeon_device pointer
319 * @ring: ring we should submit the msg to
320 * @handle: VCE session handle to use
321 * @fence: optional fence to return
323 * Close up a stream for HW test or if userspace failed to do so
325 int radeon_vce_get_destroy_msg(struct radeon_device *rdev, int ring,
326 uint32_t handle, struct radeon_fence **fence)
328 const unsigned ib_size_dw = 1024;
333 r = radeon_ib_get(rdev, ring, &ib, NULL, ib_size_dw * 4);
335 DRM_ERROR("radeon: failed to get ib (%d).\n", r);
339 dummy = ib.gpu_addr + 1024;
341 /* stitch together an VCE destroy msg */
343 ib.ptr[ib.length_dw++] = 0x0000000c; /* len */
344 ib.ptr[ib.length_dw++] = 0x00000001; /* session cmd */
345 ib.ptr[ib.length_dw++] = handle;
347 ib.ptr[ib.length_dw++] = 0x00000014; /* len */
348 ib.ptr[ib.length_dw++] = 0x05000005; /* feedback buffer */
349 ib.ptr[ib.length_dw++] = upper_32_bits(dummy);
350 ib.ptr[ib.length_dw++] = dummy;
351 ib.ptr[ib.length_dw++] = 0x00000001;
353 ib.ptr[ib.length_dw++] = 0x00000008; /* len */
354 ib.ptr[ib.length_dw++] = 0x02000001; /* destroy cmd */
356 for (i = ib.length_dw; i < ib_size_dw; ++i)
359 r = radeon_ib_schedule(rdev, &ib, NULL);
361 DRM_ERROR("radeon: failed to schedule ib (%d).\n", r);
365 *fence = radeon_fence_ref(ib.fence);
367 radeon_ib_free(rdev, &ib);
373 * radeon_vce_cs_reloc - command submission relocation
376 * @lo: address of lower dword
377 * @hi: address of higher dword
379 * Patch relocation inside command stream with real buffer address
381 int radeon_vce_cs_reloc(struct radeon_cs_parser *p, int lo, int hi)
383 struct radeon_cs_chunk *relocs_chunk;
387 relocs_chunk = &p->chunks[p->chunk_relocs_idx];
388 offset = radeon_get_ib_value(p, lo);
389 idx = radeon_get_ib_value(p, hi);
391 if (idx >= relocs_chunk->length_dw) {
392 DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
393 idx, relocs_chunk->length_dw);
397 offset += p->relocs_ptr[(idx / 4)]->lobj.gpu_offset;
399 p->ib.ptr[lo] = offset & 0xFFFFFFFF;
400 p->ib.ptr[hi] = offset >> 32;
406 * radeon_vce_cs_parse - parse and validate the command stream
411 int radeon_vce_cs_parse(struct radeon_cs_parser *p)
414 bool destroy = false;
417 while (p->idx < p->chunks[p->chunk_ib_idx].length_dw) {
418 uint32_t len = radeon_get_ib_value(p, p->idx);
419 uint32_t cmd = radeon_get_ib_value(p, p->idx + 1);
421 if ((len < 8) || (len & 3)) {
422 DRM_ERROR("invalid VCE command length (%d)!\n", len);
427 case 0x00000001: // session
428 handle = radeon_get_ib_value(p, p->idx + 2);
431 case 0x00000002: // task info
432 case 0x01000001: // create
433 case 0x04000001: // config extension
434 case 0x04000002: // pic control
435 case 0x04000005: // rate control
436 case 0x04000007: // motion estimation
437 case 0x04000008: // rdo
440 case 0x03000001: // encode
441 r = radeon_vce_cs_reloc(p, p->idx + 10, p->idx + 9);
445 r = radeon_vce_cs_reloc(p, p->idx + 12, p->idx + 11);
450 case 0x02000001: // destroy
454 case 0x05000001: // context buffer
455 case 0x05000004: // video bitstream buffer
456 case 0x05000005: // feedback buffer
457 r = radeon_vce_cs_reloc(p, p->idx + 3, p->idx + 2);
463 DRM_ERROR("invalid VCE command (0x%x)!\n", cmd);
471 /* IB contains a destroy msg, free the handle */
472 for (i = 0; i < RADEON_MAX_VCE_HANDLES; ++i)
473 atomic_cmpxchg(&p->rdev->vce.handles[i], handle, 0);
478 /* create or encode, validate the handle */
479 for (i = 0; i < RADEON_MAX_VCE_HANDLES; ++i) {
480 if (atomic_read(&p->rdev->vce.handles[i]) == handle)
484 /* handle not found try to alloc a new one */
485 for (i = 0; i < RADEON_MAX_VCE_HANDLES; ++i) {
486 if (!atomic_cmpxchg(&p->rdev->vce.handles[i], 0, handle)) {
487 p->rdev->vce.filp[i] = p->filp;
492 DRM_ERROR("No more free VCE handles!\n");
497 * radeon_vce_semaphore_emit - emit a semaphore command
499 * @rdev: radeon_device pointer
500 * @ring: engine to use
501 * @semaphore: address of semaphore
502 * @emit_wait: true=emit wait, false=emit signal
505 bool radeon_vce_semaphore_emit(struct radeon_device *rdev,
506 struct radeon_ring *ring,
507 struct radeon_semaphore *semaphore,
510 uint64_t addr = semaphore->gpu_addr;
512 radeon_ring_write(ring, VCE_CMD_SEMAPHORE);
513 radeon_ring_write(ring, (addr >> 3) & 0x000FFFFF);
514 radeon_ring_write(ring, (addr >> 23) & 0x000FFFFF);
515 radeon_ring_write(ring, 0x01003000 | (emit_wait ? 1 : 0));
517 radeon_ring_write(ring, VCE_CMD_END);
523 * radeon_vce_ib_execute - execute indirect buffer
525 * @rdev: radeon_device pointer
526 * @ib: the IB to execute
529 void radeon_vce_ib_execute(struct radeon_device *rdev, struct radeon_ib *ib)
531 struct radeon_ring *ring = &rdev->ring[ib->ring];
532 radeon_ring_write(ring, VCE_CMD_IB);
533 radeon_ring_write(ring, ib->gpu_addr);
534 radeon_ring_write(ring, upper_32_bits(ib->gpu_addr));
535 radeon_ring_write(ring, ib->length_dw);
539 * radeon_vce_fence_emit - add a fence command to the ring
541 * @rdev: radeon_device pointer
545 void radeon_vce_fence_emit(struct radeon_device *rdev,
546 struct radeon_fence *fence)
548 struct radeon_ring *ring = &rdev->ring[fence->ring];
549 uint32_t addr = rdev->fence_drv[fence->ring].gpu_addr;
551 radeon_ring_write(ring, VCE_CMD_FENCE);
552 radeon_ring_write(ring, addr);
553 radeon_ring_write(ring, upper_32_bits(addr));
554 radeon_ring_write(ring, fence->seq);
555 radeon_ring_write(ring, VCE_CMD_TRAP);
556 radeon_ring_write(ring, VCE_CMD_END);
560 * radeon_vce_ring_test - test if VCE ring is working
562 * @rdev: radeon_device pointer
563 * @ring: the engine to test on
566 int radeon_vce_ring_test(struct radeon_device *rdev, struct radeon_ring *ring)
568 uint32_t rptr = vce_v1_0_get_rptr(rdev, ring);
572 r = radeon_ring_lock(rdev, ring, 16);
574 DRM_ERROR("radeon: vce failed to lock ring %d (%d).\n",
578 radeon_ring_write(ring, VCE_CMD_END);
579 radeon_ring_unlock_commit(rdev, ring);
581 for (i = 0; i < rdev->usec_timeout; i++) {
582 if (vce_v1_0_get_rptr(rdev, ring) != rptr)
587 if (i < rdev->usec_timeout) {
588 DRM_INFO("ring test on %d succeeded in %d usecs\n",
591 DRM_ERROR("radeon: ring %d test failed\n",
600 * radeon_vce_ib_test - test if VCE IBs are working
602 * @rdev: radeon_device pointer
603 * @ring: the engine to test on
606 int radeon_vce_ib_test(struct radeon_device *rdev, struct radeon_ring *ring)
608 struct radeon_fence *fence = NULL;
611 r = radeon_vce_get_create_msg(rdev, ring->idx, 1, NULL);
613 DRM_ERROR("radeon: failed to get create msg (%d).\n", r);
617 r = radeon_vce_get_destroy_msg(rdev, ring->idx, 1, &fence);
619 DRM_ERROR("radeon: failed to get destroy ib (%d).\n", r);
623 r = radeon_fence_wait(fence, false);
625 DRM_ERROR("radeon: fence wait failed (%d).\n", r);
627 DRM_INFO("ib test on ring %d succeeded\n", ring->idx);
630 radeon_fence_unref(&fence);