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
28 #include <linux/firmware.h>
29 #include <linux/module.h>
34 #include "amdgpu_pm.h"
35 #include "amdgpu_vce.h"
38 /* 1 second timeout */
39 #define VCE_IDLE_TIMEOUT msecs_to_jiffies(1000)
42 #ifdef CONFIG_DRM_AMDGPU_CIK
43 #define FIRMWARE_BONAIRE "radeon/bonaire_vce.bin"
44 #define FIRMWARE_KABINI "radeon/kabini_vce.bin"
45 #define FIRMWARE_KAVERI "radeon/kaveri_vce.bin"
46 #define FIRMWARE_HAWAII "radeon/hawaii_vce.bin"
47 #define FIRMWARE_MULLINS "radeon/mullins_vce.bin"
49 #define FIRMWARE_TONGA "amdgpu/tonga_vce.bin"
50 #define FIRMWARE_CARRIZO "amdgpu/carrizo_vce.bin"
51 #define FIRMWARE_FIJI "amdgpu/fiji_vce.bin"
52 #define FIRMWARE_STONEY "amdgpu/stoney_vce.bin"
53 #define FIRMWARE_POLARIS10 "amdgpu/polaris10_vce.bin"
54 #define FIRMWARE_POLARIS11 "amdgpu/polaris11_vce.bin"
56 #ifdef CONFIG_DRM_AMDGPU_CIK
57 MODULE_FIRMWARE(FIRMWARE_BONAIRE);
58 MODULE_FIRMWARE(FIRMWARE_KABINI);
59 MODULE_FIRMWARE(FIRMWARE_KAVERI);
60 MODULE_FIRMWARE(FIRMWARE_HAWAII);
61 MODULE_FIRMWARE(FIRMWARE_MULLINS);
63 MODULE_FIRMWARE(FIRMWARE_TONGA);
64 MODULE_FIRMWARE(FIRMWARE_CARRIZO);
65 MODULE_FIRMWARE(FIRMWARE_FIJI);
66 MODULE_FIRMWARE(FIRMWARE_STONEY);
67 MODULE_FIRMWARE(FIRMWARE_POLARIS10);
68 MODULE_FIRMWARE(FIRMWARE_POLARIS11);
70 static void amdgpu_vce_idle_work_handler(struct work_struct *work);
73 * amdgpu_vce_init - allocate memory, load vce firmware
75 * @adev: amdgpu_device pointer
77 * First step to get VCE online, allocate memory and load the firmware
79 int amdgpu_vce_sw_init(struct amdgpu_device *adev, unsigned long size)
81 struct amdgpu_ring *ring;
82 struct amd_sched_rq *rq;
84 const struct common_firmware_header *hdr;
85 unsigned ucode_version, version_major, version_minor, binary_id;
88 switch (adev->asic_type) {
89 #ifdef CONFIG_DRM_AMDGPU_CIK
91 fw_name = FIRMWARE_BONAIRE;
94 fw_name = FIRMWARE_KAVERI;
97 fw_name = FIRMWARE_KABINI;
100 fw_name = FIRMWARE_HAWAII;
103 fw_name = FIRMWARE_MULLINS;
107 fw_name = FIRMWARE_TONGA;
110 fw_name = FIRMWARE_CARRIZO;
113 fw_name = FIRMWARE_FIJI;
116 fw_name = FIRMWARE_STONEY;
119 fw_name = FIRMWARE_POLARIS10;
122 fw_name = FIRMWARE_POLARIS11;
129 r = request_firmware(&adev->vce.fw, fw_name, adev->dev);
131 dev_err(adev->dev, "amdgpu_vce: Can't load firmware \"%s\"\n",
136 r = amdgpu_ucode_validate(adev->vce.fw);
138 dev_err(adev->dev, "amdgpu_vce: Can't validate firmware \"%s\"\n",
140 release_firmware(adev->vce.fw);
145 hdr = (const struct common_firmware_header *)adev->vce.fw->data;
147 ucode_version = le32_to_cpu(hdr->ucode_version);
148 version_major = (ucode_version >> 20) & 0xfff;
149 version_minor = (ucode_version >> 8) & 0xfff;
150 binary_id = ucode_version & 0xff;
151 DRM_INFO("Found VCE firmware Version: %hhd.%hhd Binary ID: %hhd\n",
152 version_major, version_minor, binary_id);
153 adev->vce.fw_version = ((version_major << 24) | (version_minor << 16) |
156 /* allocate firmware, stack and heap BO */
158 r = amdgpu_bo_create(adev, size, PAGE_SIZE, true,
159 AMDGPU_GEM_DOMAIN_VRAM,
160 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
161 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
162 NULL, NULL, &adev->vce.vcpu_bo);
164 dev_err(adev->dev, "(%d) failed to allocate VCE bo\n", r);
168 r = amdgpu_bo_reserve(adev->vce.vcpu_bo, false);
170 amdgpu_bo_unref(&adev->vce.vcpu_bo);
171 dev_err(adev->dev, "(%d) failed to reserve VCE bo\n", r);
175 r = amdgpu_bo_pin(adev->vce.vcpu_bo, AMDGPU_GEM_DOMAIN_VRAM,
176 &adev->vce.gpu_addr);
177 amdgpu_bo_unreserve(adev->vce.vcpu_bo);
179 amdgpu_bo_unref(&adev->vce.vcpu_bo);
180 dev_err(adev->dev, "(%d) VCE bo pin failed\n", r);
185 ring = &adev->vce.ring[0];
186 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_NORMAL];
187 r = amd_sched_entity_init(&ring->sched, &adev->vce.entity,
188 rq, amdgpu_sched_jobs);
190 DRM_ERROR("Failed setting up VCE run queue.\n");
194 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
195 atomic_set(&adev->vce.handles[i], 0);
196 adev->vce.filp[i] = NULL;
199 INIT_DELAYED_WORK(&adev->vce.idle_work, amdgpu_vce_idle_work_handler);
200 mutex_init(&adev->vce.idle_mutex);
206 * amdgpu_vce_fini - free memory
208 * @adev: amdgpu_device pointer
210 * Last step on VCE teardown, free firmware memory
212 int amdgpu_vce_sw_fini(struct amdgpu_device *adev)
216 if (adev->vce.vcpu_bo == NULL)
219 amd_sched_entity_fini(&adev->vce.ring[0].sched, &adev->vce.entity);
221 amdgpu_bo_unref(&adev->vce.vcpu_bo);
223 for (i = 0; i < adev->vce.num_rings; i++)
224 amdgpu_ring_fini(&adev->vce.ring[i]);
226 release_firmware(adev->vce.fw);
227 mutex_destroy(&adev->vce.idle_mutex);
233 * amdgpu_vce_suspend - unpin VCE fw memory
235 * @adev: amdgpu_device pointer
238 int amdgpu_vce_suspend(struct amdgpu_device *adev)
242 if (adev->vce.vcpu_bo == NULL)
245 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
246 if (atomic_read(&adev->vce.handles[i]))
249 if (i == AMDGPU_MAX_VCE_HANDLES)
252 cancel_delayed_work_sync(&adev->vce.idle_work);
253 /* TODO: suspending running encoding sessions isn't supported */
258 * amdgpu_vce_resume - pin VCE fw memory
260 * @adev: amdgpu_device pointer
263 int amdgpu_vce_resume(struct amdgpu_device *adev)
266 const struct common_firmware_header *hdr;
270 if (adev->vce.vcpu_bo == NULL)
273 r = amdgpu_bo_reserve(adev->vce.vcpu_bo, false);
275 dev_err(adev->dev, "(%d) failed to reserve VCE bo\n", r);
279 r = amdgpu_bo_kmap(adev->vce.vcpu_bo, &cpu_addr);
281 amdgpu_bo_unreserve(adev->vce.vcpu_bo);
282 dev_err(adev->dev, "(%d) VCE map failed\n", r);
286 hdr = (const struct common_firmware_header *)adev->vce.fw->data;
287 offset = le32_to_cpu(hdr->ucode_array_offset_bytes);
288 memcpy_toio(cpu_addr, adev->vce.fw->data + offset,
289 adev->vce.fw->size - offset);
291 amdgpu_bo_kunmap(adev->vce.vcpu_bo);
293 amdgpu_bo_unreserve(adev->vce.vcpu_bo);
299 * amdgpu_vce_idle_work_handler - power off VCE
301 * @work: pointer to work structure
303 * power of VCE when it's not used any more
305 static void amdgpu_vce_idle_work_handler(struct work_struct *work)
307 struct amdgpu_device *adev =
308 container_of(work, struct amdgpu_device, vce.idle_work.work);
309 unsigned i, count = 0;
311 for (i = 0; i < adev->vce.num_rings; i++)
312 count += amdgpu_fence_count_emitted(&adev->vce.ring[i]);
315 if (adev->pm.dpm_enabled) {
316 amdgpu_dpm_enable_vce(adev, false);
318 amdgpu_asic_set_vce_clocks(adev, 0, 0);
321 schedule_delayed_work(&adev->vce.idle_work, VCE_IDLE_TIMEOUT);
326 * amdgpu_vce_ring_begin_use - power up VCE
330 * Make sure VCE is powerd up when we want to use it
332 void amdgpu_vce_ring_begin_use(struct amdgpu_ring *ring)
334 struct amdgpu_device *adev = ring->adev;
337 mutex_lock(&adev->vce.idle_mutex);
338 set_clocks = !cancel_delayed_work_sync(&adev->vce.idle_work);
340 if (adev->pm.dpm_enabled) {
341 amdgpu_dpm_enable_vce(adev, true);
343 amdgpu_asic_set_vce_clocks(adev, 53300, 40000);
346 mutex_unlock(&adev->vce.idle_mutex);
350 * amdgpu_vce_ring_end_use - power VCE down
354 * Schedule work to power VCE down again
356 void amdgpu_vce_ring_end_use(struct amdgpu_ring *ring)
358 schedule_delayed_work(&ring->adev->vce.idle_work, VCE_IDLE_TIMEOUT);
362 * amdgpu_vce_free_handles - free still open VCE handles
364 * @adev: amdgpu_device pointer
365 * @filp: drm file pointer
367 * Close all VCE handles still open by this file pointer
369 void amdgpu_vce_free_handles(struct amdgpu_device *adev, struct drm_file *filp)
371 struct amdgpu_ring *ring = &adev->vce.ring[0];
373 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
374 uint32_t handle = atomic_read(&adev->vce.handles[i]);
376 if (!handle || adev->vce.filp[i] != filp)
379 r = amdgpu_vce_get_destroy_msg(ring, handle, false, NULL);
381 DRM_ERROR("Error destroying VCE handle (%d)!\n", r);
383 adev->vce.filp[i] = NULL;
384 atomic_set(&adev->vce.handles[i], 0);
389 * amdgpu_vce_get_create_msg - generate a VCE create msg
391 * @adev: amdgpu_device pointer
392 * @ring: ring we should submit the msg to
393 * @handle: VCE session handle to use
394 * @fence: optional fence to return
396 * Open up a stream for HW test
398 int amdgpu_vce_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
399 struct dma_fence **fence)
401 const unsigned ib_size_dw = 1024;
402 struct amdgpu_job *job;
403 struct amdgpu_ib *ib;
404 struct dma_fence *f = NULL;
408 r = amdgpu_job_alloc_with_ib(ring->adev, ib_size_dw * 4, &job);
414 dummy = ib->gpu_addr + 1024;
416 /* stitch together an VCE create msg */
418 ib->ptr[ib->length_dw++] = 0x0000000c; /* len */
419 ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */
420 ib->ptr[ib->length_dw++] = handle;
422 if ((ring->adev->vce.fw_version >> 24) >= 52)
423 ib->ptr[ib->length_dw++] = 0x00000040; /* len */
425 ib->ptr[ib->length_dw++] = 0x00000030; /* len */
426 ib->ptr[ib->length_dw++] = 0x01000001; /* create cmd */
427 ib->ptr[ib->length_dw++] = 0x00000000;
428 ib->ptr[ib->length_dw++] = 0x00000042;
429 ib->ptr[ib->length_dw++] = 0x0000000a;
430 ib->ptr[ib->length_dw++] = 0x00000001;
431 ib->ptr[ib->length_dw++] = 0x00000080;
432 ib->ptr[ib->length_dw++] = 0x00000060;
433 ib->ptr[ib->length_dw++] = 0x00000100;
434 ib->ptr[ib->length_dw++] = 0x00000100;
435 ib->ptr[ib->length_dw++] = 0x0000000c;
436 ib->ptr[ib->length_dw++] = 0x00000000;
437 if ((ring->adev->vce.fw_version >> 24) >= 52) {
438 ib->ptr[ib->length_dw++] = 0x00000000;
439 ib->ptr[ib->length_dw++] = 0x00000000;
440 ib->ptr[ib->length_dw++] = 0x00000000;
441 ib->ptr[ib->length_dw++] = 0x00000000;
444 ib->ptr[ib->length_dw++] = 0x00000014; /* len */
445 ib->ptr[ib->length_dw++] = 0x05000005; /* feedback buffer */
446 ib->ptr[ib->length_dw++] = upper_32_bits(dummy);
447 ib->ptr[ib->length_dw++] = dummy;
448 ib->ptr[ib->length_dw++] = 0x00000001;
450 for (i = ib->length_dw; i < ib_size_dw; ++i)
453 r = amdgpu_ib_schedule(ring, 1, ib, NULL, NULL, &f);
454 job->fence = dma_fence_get(f);
458 amdgpu_job_free(job);
460 *fence = dma_fence_get(f);
465 amdgpu_job_free(job);
470 * amdgpu_vce_get_destroy_msg - generate a VCE destroy msg
472 * @adev: amdgpu_device pointer
473 * @ring: ring we should submit the msg to
474 * @handle: VCE session handle to use
475 * @fence: optional fence to return
477 * Close up a stream for HW test or if userspace failed to do so
479 int amdgpu_vce_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
480 bool direct, struct dma_fence **fence)
482 const unsigned ib_size_dw = 1024;
483 struct amdgpu_job *job;
484 struct amdgpu_ib *ib;
485 struct dma_fence *f = NULL;
488 r = amdgpu_job_alloc_with_ib(ring->adev, ib_size_dw * 4, &job);
494 /* stitch together an VCE destroy msg */
496 ib->ptr[ib->length_dw++] = 0x0000000c; /* len */
497 ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */
498 ib->ptr[ib->length_dw++] = handle;
500 ib->ptr[ib->length_dw++] = 0x00000020; /* len */
501 ib->ptr[ib->length_dw++] = 0x00000002; /* task info */
502 ib->ptr[ib->length_dw++] = 0xffffffff; /* next task info, set to 0xffffffff if no */
503 ib->ptr[ib->length_dw++] = 0x00000001; /* destroy session */
504 ib->ptr[ib->length_dw++] = 0x00000000;
505 ib->ptr[ib->length_dw++] = 0x00000000;
506 ib->ptr[ib->length_dw++] = 0xffffffff; /* feedback is not needed, set to 0xffffffff and firmware will not output feedback */
507 ib->ptr[ib->length_dw++] = 0x00000000;
509 ib->ptr[ib->length_dw++] = 0x00000008; /* len */
510 ib->ptr[ib->length_dw++] = 0x02000001; /* destroy cmd */
512 for (i = ib->length_dw; i < ib_size_dw; ++i)
516 r = amdgpu_ib_schedule(ring, 1, ib, NULL, NULL, &f);
517 job->fence = dma_fence_get(f);
521 amdgpu_job_free(job);
523 r = amdgpu_job_submit(job, ring, &ring->adev->vce.entity,
524 AMDGPU_FENCE_OWNER_UNDEFINED, &f);
530 *fence = dma_fence_get(f);
535 amdgpu_job_free(job);
540 * amdgpu_vce_cs_reloc - command submission relocation
543 * @lo: address of lower dword
544 * @hi: address of higher dword
545 * @size: minimum size
547 * Patch relocation inside command stream with real buffer address
549 static int amdgpu_vce_cs_reloc(struct amdgpu_cs_parser *p, uint32_t ib_idx,
550 int lo, int hi, unsigned size, uint32_t index)
552 struct amdgpu_bo_va_mapping *mapping;
553 struct amdgpu_bo *bo;
556 if (index == 0xffffffff)
559 addr = ((uint64_t)amdgpu_get_ib_value(p, ib_idx, lo)) |
560 ((uint64_t)amdgpu_get_ib_value(p, ib_idx, hi)) << 32;
561 addr += ((uint64_t)size) * ((uint64_t)index);
563 mapping = amdgpu_cs_find_mapping(p, addr, &bo);
564 if (mapping == NULL) {
565 DRM_ERROR("Can't find BO for addr 0x%010Lx %d %d %d %d\n",
566 addr, lo, hi, size, index);
570 if ((addr + (uint64_t)size) >
571 ((uint64_t)mapping->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
572 DRM_ERROR("BO to small for addr 0x%010Lx %d %d\n",
577 addr -= ((uint64_t)mapping->it.start) * AMDGPU_GPU_PAGE_SIZE;
578 addr += amdgpu_bo_gpu_offset(bo);
579 addr -= ((uint64_t)size) * ((uint64_t)index);
581 amdgpu_set_ib_value(p, ib_idx, lo, lower_32_bits(addr));
582 amdgpu_set_ib_value(p, ib_idx, hi, upper_32_bits(addr));
588 * amdgpu_vce_validate_handle - validate stream handle
591 * @handle: handle to validate
592 * @allocated: allocated a new handle?
594 * Validates the handle and return the found session index or -EINVAL
595 * we we don't have another free session index.
597 static int amdgpu_vce_validate_handle(struct amdgpu_cs_parser *p,
598 uint32_t handle, uint32_t *allocated)
602 /* validate the handle */
603 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
604 if (atomic_read(&p->adev->vce.handles[i]) == handle) {
605 if (p->adev->vce.filp[i] != p->filp) {
606 DRM_ERROR("VCE handle collision detected!\n");
613 /* handle not found try to alloc a new one */
614 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
615 if (!atomic_cmpxchg(&p->adev->vce.handles[i], 0, handle)) {
616 p->adev->vce.filp[i] = p->filp;
617 p->adev->vce.img_size[i] = 0;
618 *allocated |= 1 << i;
623 DRM_ERROR("No more free VCE handles!\n");
628 * amdgpu_vce_cs_parse - parse and validate the command stream
633 int amdgpu_vce_ring_parse_cs(struct amdgpu_cs_parser *p, uint32_t ib_idx)
635 struct amdgpu_ib *ib = &p->job->ibs[ib_idx];
636 unsigned fb_idx = 0, bs_idx = 0;
637 int session_idx = -1;
638 uint32_t destroyed = 0;
639 uint32_t created = 0;
640 uint32_t allocated = 0;
641 uint32_t tmp, handle = 0;
642 uint32_t *size = &tmp;
646 ib->gpu_addr = amdgpu_sa_bo_gpu_addr(ib->sa_bo);
648 r = amdgpu_cs_sysvm_access_required(p);
652 while (idx < ib->length_dw) {
653 uint32_t len = amdgpu_get_ib_value(p, ib_idx, idx);
654 uint32_t cmd = amdgpu_get_ib_value(p, ib_idx, idx + 1);
656 if ((len < 8) || (len & 3)) {
657 DRM_ERROR("invalid VCE command length (%d)!\n", len);
663 case 0x00000001: /* session */
664 handle = amdgpu_get_ib_value(p, ib_idx, idx + 2);
665 session_idx = amdgpu_vce_validate_handle(p, handle,
667 if (session_idx < 0) {
671 size = &p->adev->vce.img_size[session_idx];
674 case 0x00000002: /* task info */
675 fb_idx = amdgpu_get_ib_value(p, ib_idx, idx + 6);
676 bs_idx = amdgpu_get_ib_value(p, ib_idx, idx + 7);
679 case 0x01000001: /* create */
680 created |= 1 << session_idx;
681 if (destroyed & (1 << session_idx)) {
682 destroyed &= ~(1 << session_idx);
683 allocated |= 1 << session_idx;
685 } else if (!(allocated & (1 << session_idx))) {
686 DRM_ERROR("Handle already in use!\n");
691 *size = amdgpu_get_ib_value(p, ib_idx, idx + 8) *
692 amdgpu_get_ib_value(p, ib_idx, idx + 10) *
696 case 0x04000001: /* config extension */
697 case 0x04000002: /* pic control */
698 case 0x04000005: /* rate control */
699 case 0x04000007: /* motion estimation */
700 case 0x04000008: /* rdo */
701 case 0x04000009: /* vui */
702 case 0x05000002: /* auxiliary buffer */
703 case 0x05000009: /* clock table */
706 case 0x0500000c: /* hw config */
707 switch (p->adev->asic_type) {
708 #ifdef CONFIG_DRM_AMDGPU_CIK
720 case 0x03000001: /* encode */
721 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 10, idx + 9,
726 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 12, idx + 11,
732 case 0x02000001: /* destroy */
733 destroyed |= 1 << session_idx;
736 case 0x05000001: /* context buffer */
737 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
743 case 0x05000004: /* video bitstream buffer */
744 tmp = amdgpu_get_ib_value(p, ib_idx, idx + 4);
745 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
751 case 0x05000005: /* feedback buffer */
752 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
759 DRM_ERROR("invalid VCE command (0x%x)!\n", cmd);
764 if (session_idx == -1) {
765 DRM_ERROR("no session command at start of IB\n");
773 if (allocated & ~created) {
774 DRM_ERROR("New session without create command!\n");
780 /* No error, free all destroyed handle slots */
783 /* Error during parsing, free all allocated handle slots */
787 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
789 atomic_set(&p->adev->vce.handles[i], 0);
795 * amdgpu_vce_cs_parse_vm - parse the command stream in VM mode
800 int amdgpu_vce_ring_parse_cs_vm(struct amdgpu_cs_parser *p, uint32_t ib_idx)
802 struct amdgpu_ib *ib = &p->job->ibs[ib_idx];
803 int session_idx = -1;
804 uint32_t destroyed = 0;
805 uint32_t created = 0;
806 uint32_t allocated = 0;
807 uint32_t tmp, handle = 0;
808 int i, r = 0, idx = 0;
810 while (idx < ib->length_dw) {
811 uint32_t len = amdgpu_get_ib_value(p, ib_idx, idx);
812 uint32_t cmd = amdgpu_get_ib_value(p, ib_idx, idx + 1);
814 if ((len < 8) || (len & 3)) {
815 DRM_ERROR("invalid VCE command length (%d)!\n", len);
821 case 0x00000001: /* session */
822 handle = amdgpu_get_ib_value(p, ib_idx, idx + 2);
823 session_idx = amdgpu_vce_validate_handle(p, handle,
825 if (session_idx < 0) {
831 case 0x01000001: /* create */
832 created |= 1 << session_idx;
833 if (destroyed & (1 << session_idx)) {
834 destroyed &= ~(1 << session_idx);
835 allocated |= 1 << session_idx;
837 } else if (!(allocated & (1 << session_idx))) {
838 DRM_ERROR("Handle already in use!\n");
845 case 0x02000001: /* destroy */
846 destroyed |= 1 << session_idx;
853 if (session_idx == -1) {
854 DRM_ERROR("no session command at start of IB\n");
862 if (allocated & ~created) {
863 DRM_ERROR("New session without create command!\n");
869 /* No error, free all destroyed handle slots */
871 amdgpu_ib_free(p->adev, ib, NULL);
873 /* Error during parsing, free all allocated handle slots */
877 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
879 atomic_set(&p->adev->vce.handles[i], 0);
885 * amdgpu_vce_ring_emit_ib - execute indirect buffer
887 * @ring: engine to use
888 * @ib: the IB to execute
891 void amdgpu_vce_ring_emit_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib,
892 unsigned vm_id, bool ctx_switch)
894 amdgpu_ring_write(ring, VCE_CMD_IB);
895 amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr));
896 amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
897 amdgpu_ring_write(ring, ib->length_dw);
901 * amdgpu_vce_ring_emit_fence - add a fence command to the ring
903 * @ring: engine to use
907 void amdgpu_vce_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
910 WARN_ON(flags & AMDGPU_FENCE_FLAG_64BIT);
912 amdgpu_ring_write(ring, VCE_CMD_FENCE);
913 amdgpu_ring_write(ring, addr);
914 amdgpu_ring_write(ring, upper_32_bits(addr));
915 amdgpu_ring_write(ring, seq);
916 amdgpu_ring_write(ring, VCE_CMD_TRAP);
917 amdgpu_ring_write(ring, VCE_CMD_END);
921 * amdgpu_vce_ring_test_ring - test if VCE ring is working
923 * @ring: the engine to test on
926 int amdgpu_vce_ring_test_ring(struct amdgpu_ring *ring)
928 struct amdgpu_device *adev = ring->adev;
929 uint32_t rptr = amdgpu_ring_get_rptr(ring);
933 r = amdgpu_ring_alloc(ring, 16);
935 DRM_ERROR("amdgpu: vce failed to lock ring %d (%d).\n",
939 amdgpu_ring_write(ring, VCE_CMD_END);
940 amdgpu_ring_commit(ring);
942 for (i = 0; i < adev->usec_timeout; i++) {
943 if (amdgpu_ring_get_rptr(ring) != rptr)
948 if (i < adev->usec_timeout) {
949 DRM_INFO("ring test on %d succeeded in %d usecs\n",
952 DRM_ERROR("amdgpu: ring %d test failed\n",
961 * amdgpu_vce_ring_test_ib - test if VCE IBs are working
963 * @ring: the engine to test on
966 int amdgpu_vce_ring_test_ib(struct amdgpu_ring *ring, long timeout)
968 struct dma_fence *fence = NULL;
971 /* skip vce ring1/2 ib test for now, since it's not reliable */
972 if (ring != &ring->adev->vce.ring[0])
975 r = amdgpu_vce_get_create_msg(ring, 1, NULL);
977 DRM_ERROR("amdgpu: failed to get create msg (%ld).\n", r);
981 r = amdgpu_vce_get_destroy_msg(ring, 1, true, &fence);
983 DRM_ERROR("amdgpu: failed to get destroy ib (%ld).\n", r);
987 r = dma_fence_wait_timeout(fence, false, timeout);
989 DRM_ERROR("amdgpu: IB test timed out.\n");
992 DRM_ERROR("amdgpu: fence wait failed (%ld).\n", r);
994 DRM_INFO("ib test on ring %d succeeded\n", ring->idx);
998 dma_fence_put(fence);