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 "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 "/*(DEBLOBBED)*/"
44 #define FIRMWARE_KABINI "/*(DEBLOBBED)*/"
45 #define FIRMWARE_KAVERI "/*(DEBLOBBED)*/"
46 #define FIRMWARE_HAWAII "/*(DEBLOBBED)*/"
47 #define FIRMWARE_MULLINS "/*(DEBLOBBED)*/"
49 #define FIRMWARE_TONGA "/*(DEBLOBBED)*/"
50 #define FIRMWARE_CARRIZO "/*(DEBLOBBED)*/"
51 #define FIRMWARE_FIJI "/*(DEBLOBBED)*/"
52 #define FIRMWARE_STONEY "/*(DEBLOBBED)*/"
53 #define FIRMWARE_POLARIS10 "/*(DEBLOBBED)*/"
54 #define FIRMWARE_POLARIS11 "/*(DEBLOBBED)*/"
55 #define FIRMWARE_POLARIS12 "/*(DEBLOBBED)*/"
56 #define FIRMWARE_VEGAM "/*(DEBLOBBED)*/"
58 #define FIRMWARE_VEGA10 "/*(DEBLOBBED)*/"
59 #define FIRMWARE_VEGA12 "/*(DEBLOBBED)*/"
60 #define FIRMWARE_VEGA20 "/*(DEBLOBBED)*/"
62 #ifdef CONFIG_DRM_AMDGPU_CIK
67 static void amdgpu_vce_idle_work_handler(struct work_struct *work);
68 static int amdgpu_vce_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
70 struct dma_fence **fence);
71 static int amdgpu_vce_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
72 bool direct, struct dma_fence **fence);
75 * amdgpu_vce_init - allocate memory, load vce firmware
77 * @adev: amdgpu_device pointer
79 * First step to get VCE online, allocate memory and load the firmware
81 int amdgpu_vce_sw_init(struct amdgpu_device *adev, unsigned long size)
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;
125 fw_name = FIRMWARE_POLARIS12;
128 fw_name = FIRMWARE_VEGAM;
131 fw_name = FIRMWARE_VEGA10;
134 fw_name = FIRMWARE_VEGA12;
137 fw_name = FIRMWARE_VEGA20;
144 r = reject_firmware(&adev->vce.fw, fw_name, adev->dev);
146 dev_err(adev->dev, "amdgpu_vce: Can't load firmware \"%s\"\n",
151 r = amdgpu_ucode_validate(adev->vce.fw);
153 dev_err(adev->dev, "amdgpu_vce: Can't validate firmware \"%s\"\n",
155 release_firmware(adev->vce.fw);
160 hdr = (const struct common_firmware_header *)adev->vce.fw->data;
162 ucode_version = le32_to_cpu(hdr->ucode_version);
163 version_major = (ucode_version >> 20) & 0xfff;
164 version_minor = (ucode_version >> 8) & 0xfff;
165 binary_id = ucode_version & 0xff;
166 DRM_INFO("Found VCE firmware Version: %hhd.%hhd Binary ID: %hhd\n",
167 version_major, version_minor, binary_id);
168 adev->vce.fw_version = ((version_major << 24) | (version_minor << 16) |
171 r = amdgpu_bo_create_kernel(adev, size, PAGE_SIZE,
172 AMDGPU_GEM_DOMAIN_VRAM, &adev->vce.vcpu_bo,
173 &adev->vce.gpu_addr, &adev->vce.cpu_addr);
175 dev_err(adev->dev, "(%d) failed to allocate VCE bo\n", r);
179 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
180 atomic_set(&adev->vce.handles[i], 0);
181 adev->vce.filp[i] = NULL;
184 INIT_DELAYED_WORK(&adev->vce.idle_work, amdgpu_vce_idle_work_handler);
185 mutex_init(&adev->vce.idle_mutex);
191 * amdgpu_vce_fini - free memory
193 * @adev: amdgpu_device pointer
195 * Last step on VCE teardown, free firmware memory
197 int amdgpu_vce_sw_fini(struct amdgpu_device *adev)
201 if (adev->vce.vcpu_bo == NULL)
204 cancel_delayed_work_sync(&adev->vce.idle_work);
205 drm_sched_entity_destroy(&adev->vce.entity);
207 amdgpu_bo_free_kernel(&adev->vce.vcpu_bo, &adev->vce.gpu_addr,
208 (void **)&adev->vce.cpu_addr);
210 for (i = 0; i < adev->vce.num_rings; i++)
211 amdgpu_ring_fini(&adev->vce.ring[i]);
213 release_firmware(adev->vce.fw);
214 mutex_destroy(&adev->vce.idle_mutex);
220 * amdgpu_vce_entity_init - init entity
222 * @adev: amdgpu_device pointer
225 int amdgpu_vce_entity_init(struct amdgpu_device *adev)
227 struct amdgpu_ring *ring;
228 struct drm_gpu_scheduler *sched;
231 ring = &adev->vce.ring[0];
232 sched = &ring->sched;
233 r = drm_sched_entity_init(&adev->vce.entity, DRM_SCHED_PRIORITY_NORMAL,
236 DRM_ERROR("Failed setting up VCE run queue.\n");
244 * amdgpu_vce_suspend - unpin VCE fw memory
246 * @adev: amdgpu_device pointer
249 int amdgpu_vce_suspend(struct amdgpu_device *adev)
253 cancel_delayed_work_sync(&adev->vce.idle_work);
255 if (adev->vce.vcpu_bo == NULL)
258 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
259 if (atomic_read(&adev->vce.handles[i]))
262 if (i == AMDGPU_MAX_VCE_HANDLES)
265 /* TODO: suspending running encoding sessions isn't supported */
270 * amdgpu_vce_resume - pin VCE fw memory
272 * @adev: amdgpu_device pointer
275 int amdgpu_vce_resume(struct amdgpu_device *adev)
278 const struct common_firmware_header *hdr;
282 if (adev->vce.vcpu_bo == NULL)
285 r = amdgpu_bo_reserve(adev->vce.vcpu_bo, false);
287 dev_err(adev->dev, "(%d) failed to reserve VCE bo\n", r);
291 r = amdgpu_bo_kmap(adev->vce.vcpu_bo, &cpu_addr);
293 amdgpu_bo_unreserve(adev->vce.vcpu_bo);
294 dev_err(adev->dev, "(%d) VCE map failed\n", r);
298 hdr = (const struct common_firmware_header *)adev->vce.fw->data;
299 offset = le32_to_cpu(hdr->ucode_array_offset_bytes);
300 memcpy_toio(cpu_addr, adev->vce.fw->data + offset,
301 adev->vce.fw->size - offset);
303 amdgpu_bo_kunmap(adev->vce.vcpu_bo);
305 amdgpu_bo_unreserve(adev->vce.vcpu_bo);
311 * amdgpu_vce_idle_work_handler - power off VCE
313 * @work: pointer to work structure
315 * power of VCE when it's not used any more
317 static void amdgpu_vce_idle_work_handler(struct work_struct *work)
319 struct amdgpu_device *adev =
320 container_of(work, struct amdgpu_device, vce.idle_work.work);
321 unsigned i, count = 0;
323 for (i = 0; i < adev->vce.num_rings; i++)
324 count += amdgpu_fence_count_emitted(&adev->vce.ring[i]);
327 if (adev->pm.dpm_enabled) {
328 amdgpu_dpm_enable_vce(adev, false);
330 amdgpu_asic_set_vce_clocks(adev, 0, 0);
331 amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
333 amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
337 schedule_delayed_work(&adev->vce.idle_work, VCE_IDLE_TIMEOUT);
342 * amdgpu_vce_ring_begin_use - power up VCE
346 * Make sure VCE is powerd up when we want to use it
348 void amdgpu_vce_ring_begin_use(struct amdgpu_ring *ring)
350 struct amdgpu_device *adev = ring->adev;
353 if (amdgpu_sriov_vf(adev))
356 mutex_lock(&adev->vce.idle_mutex);
357 set_clocks = !cancel_delayed_work_sync(&adev->vce.idle_work);
359 if (adev->pm.dpm_enabled) {
360 amdgpu_dpm_enable_vce(adev, true);
362 amdgpu_asic_set_vce_clocks(adev, 53300, 40000);
363 amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
364 AMD_CG_STATE_UNGATE);
365 amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
366 AMD_PG_STATE_UNGATE);
370 mutex_unlock(&adev->vce.idle_mutex);
374 * amdgpu_vce_ring_end_use - power VCE down
378 * Schedule work to power VCE down again
380 void amdgpu_vce_ring_end_use(struct amdgpu_ring *ring)
382 if (!amdgpu_sriov_vf(ring->adev))
383 schedule_delayed_work(&ring->adev->vce.idle_work, VCE_IDLE_TIMEOUT);
387 * amdgpu_vce_free_handles - free still open VCE handles
389 * @adev: amdgpu_device pointer
390 * @filp: drm file pointer
392 * Close all VCE handles still open by this file pointer
394 void amdgpu_vce_free_handles(struct amdgpu_device *adev, struct drm_file *filp)
396 struct amdgpu_ring *ring = &adev->vce.ring[0];
398 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
399 uint32_t handle = atomic_read(&adev->vce.handles[i]);
401 if (!handle || adev->vce.filp[i] != filp)
404 r = amdgpu_vce_get_destroy_msg(ring, handle, false, NULL);
406 DRM_ERROR("Error destroying VCE handle (%d)!\n", r);
408 adev->vce.filp[i] = NULL;
409 atomic_set(&adev->vce.handles[i], 0);
414 * amdgpu_vce_get_create_msg - generate a VCE create msg
416 * @adev: amdgpu_device pointer
417 * @ring: ring we should submit the msg to
418 * @handle: VCE session handle to use
419 * @fence: optional fence to return
421 * Open up a stream for HW test
423 static int amdgpu_vce_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
424 struct amdgpu_bo *bo,
425 struct dma_fence **fence)
427 const unsigned ib_size_dw = 1024;
428 struct amdgpu_job *job;
429 struct amdgpu_ib *ib;
430 struct dma_fence *f = NULL;
434 r = amdgpu_job_alloc_with_ib(ring->adev, ib_size_dw * 4, &job);
440 addr = amdgpu_bo_gpu_offset(bo);
442 /* stitch together an VCE create msg */
444 ib->ptr[ib->length_dw++] = 0x0000000c; /* len */
445 ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */
446 ib->ptr[ib->length_dw++] = handle;
448 if ((ring->adev->vce.fw_version >> 24) >= 52)
449 ib->ptr[ib->length_dw++] = 0x00000040; /* len */
451 ib->ptr[ib->length_dw++] = 0x00000030; /* len */
452 ib->ptr[ib->length_dw++] = 0x01000001; /* create cmd */
453 ib->ptr[ib->length_dw++] = 0x00000000;
454 ib->ptr[ib->length_dw++] = 0x00000042;
455 ib->ptr[ib->length_dw++] = 0x0000000a;
456 ib->ptr[ib->length_dw++] = 0x00000001;
457 ib->ptr[ib->length_dw++] = 0x00000080;
458 ib->ptr[ib->length_dw++] = 0x00000060;
459 ib->ptr[ib->length_dw++] = 0x00000100;
460 ib->ptr[ib->length_dw++] = 0x00000100;
461 ib->ptr[ib->length_dw++] = 0x0000000c;
462 ib->ptr[ib->length_dw++] = 0x00000000;
463 if ((ring->adev->vce.fw_version >> 24) >= 52) {
464 ib->ptr[ib->length_dw++] = 0x00000000;
465 ib->ptr[ib->length_dw++] = 0x00000000;
466 ib->ptr[ib->length_dw++] = 0x00000000;
467 ib->ptr[ib->length_dw++] = 0x00000000;
470 ib->ptr[ib->length_dw++] = 0x00000014; /* len */
471 ib->ptr[ib->length_dw++] = 0x05000005; /* feedback buffer */
472 ib->ptr[ib->length_dw++] = upper_32_bits(addr);
473 ib->ptr[ib->length_dw++] = addr;
474 ib->ptr[ib->length_dw++] = 0x00000001;
476 for (i = ib->length_dw; i < ib_size_dw; ++i)
479 r = amdgpu_job_submit_direct(job, ring, &f);
484 *fence = dma_fence_get(f);
489 amdgpu_job_free(job);
494 * amdgpu_vce_get_destroy_msg - generate a VCE destroy msg
496 * @adev: amdgpu_device pointer
497 * @ring: ring we should submit the msg to
498 * @handle: VCE session handle to use
499 * @fence: optional fence to return
501 * Close up a stream for HW test or if userspace failed to do so
503 static int amdgpu_vce_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
504 bool direct, struct dma_fence **fence)
506 const unsigned ib_size_dw = 1024;
507 struct amdgpu_job *job;
508 struct amdgpu_ib *ib;
509 struct dma_fence *f = NULL;
512 r = amdgpu_job_alloc_with_ib(ring->adev, ib_size_dw * 4, &job);
518 /* stitch together an VCE destroy msg */
520 ib->ptr[ib->length_dw++] = 0x0000000c; /* len */
521 ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */
522 ib->ptr[ib->length_dw++] = handle;
524 ib->ptr[ib->length_dw++] = 0x00000020; /* len */
525 ib->ptr[ib->length_dw++] = 0x00000002; /* task info */
526 ib->ptr[ib->length_dw++] = 0xffffffff; /* next task info, set to 0xffffffff if no */
527 ib->ptr[ib->length_dw++] = 0x00000001; /* destroy session */
528 ib->ptr[ib->length_dw++] = 0x00000000;
529 ib->ptr[ib->length_dw++] = 0x00000000;
530 ib->ptr[ib->length_dw++] = 0xffffffff; /* feedback is not needed, set to 0xffffffff and firmware will not output feedback */
531 ib->ptr[ib->length_dw++] = 0x00000000;
533 ib->ptr[ib->length_dw++] = 0x00000008; /* len */
534 ib->ptr[ib->length_dw++] = 0x02000001; /* destroy cmd */
536 for (i = ib->length_dw; i < ib_size_dw; ++i)
540 r = amdgpu_job_submit_direct(job, ring, &f);
542 r = amdgpu_job_submit(job, &ring->adev->vce.entity,
543 AMDGPU_FENCE_OWNER_UNDEFINED, &f);
548 *fence = dma_fence_get(f);
553 amdgpu_job_free(job);
558 * amdgpu_vce_cs_validate_bo - make sure not to cross 4GB boundary
561 * @lo: address of lower dword
562 * @hi: address of higher dword
563 * @size: minimum size
564 * @index: bs/fb index
566 * Make sure that no BO cross a 4GB boundary.
568 static int amdgpu_vce_validate_bo(struct amdgpu_cs_parser *p, uint32_t ib_idx,
569 int lo, int hi, unsigned size, int32_t index)
571 int64_t offset = ((uint64_t)size) * ((int64_t)index);
572 struct ttm_operation_ctx ctx = { false, false };
573 struct amdgpu_bo_va_mapping *mapping;
574 unsigned i, fpfn, lpfn;
575 struct amdgpu_bo *bo;
579 addr = ((uint64_t)amdgpu_get_ib_value(p, ib_idx, lo)) |
580 ((uint64_t)amdgpu_get_ib_value(p, ib_idx, hi)) << 32;
583 fpfn = PAGE_ALIGN(offset) >> PAGE_SHIFT;
584 lpfn = 0x100000000ULL >> PAGE_SHIFT;
587 lpfn = (0x100000000ULL - PAGE_ALIGN(offset)) >> PAGE_SHIFT;
590 r = amdgpu_cs_find_mapping(p, addr, &bo, &mapping);
592 DRM_ERROR("Can't find BO for addr 0x%010Lx %d %d %d %d\n",
593 addr, lo, hi, size, index);
597 for (i = 0; i < bo->placement.num_placement; ++i) {
598 bo->placements[i].fpfn = max(bo->placements[i].fpfn, fpfn);
599 bo->placements[i].lpfn = bo->placements[i].lpfn ?
600 min(bo->placements[i].lpfn, lpfn) : lpfn;
602 return ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
607 * amdgpu_vce_cs_reloc - command submission relocation
610 * @lo: address of lower dword
611 * @hi: address of higher dword
612 * @size: minimum size
614 * Patch relocation inside command stream with real buffer address
616 static int amdgpu_vce_cs_reloc(struct amdgpu_cs_parser *p, uint32_t ib_idx,
617 int lo, int hi, unsigned size, uint32_t index)
619 struct amdgpu_bo_va_mapping *mapping;
620 struct amdgpu_bo *bo;
624 if (index == 0xffffffff)
627 addr = ((uint64_t)amdgpu_get_ib_value(p, ib_idx, lo)) |
628 ((uint64_t)amdgpu_get_ib_value(p, ib_idx, hi)) << 32;
629 addr += ((uint64_t)size) * ((uint64_t)index);
631 r = amdgpu_cs_find_mapping(p, addr, &bo, &mapping);
633 DRM_ERROR("Can't find BO for addr 0x%010Lx %d %d %d %d\n",
634 addr, lo, hi, size, index);
638 if ((addr + (uint64_t)size) >
639 (mapping->last + 1) * AMDGPU_GPU_PAGE_SIZE) {
640 DRM_ERROR("BO too small for addr 0x%010Lx %d %d\n",
645 addr -= mapping->start * AMDGPU_GPU_PAGE_SIZE;
646 addr += amdgpu_bo_gpu_offset(bo);
647 addr -= ((uint64_t)size) * ((uint64_t)index);
649 amdgpu_set_ib_value(p, ib_idx, lo, lower_32_bits(addr));
650 amdgpu_set_ib_value(p, ib_idx, hi, upper_32_bits(addr));
656 * amdgpu_vce_validate_handle - validate stream handle
659 * @handle: handle to validate
660 * @allocated: allocated a new handle?
662 * Validates the handle and return the found session index or -EINVAL
663 * we we don't have another free session index.
665 static int amdgpu_vce_validate_handle(struct amdgpu_cs_parser *p,
666 uint32_t handle, uint32_t *allocated)
670 /* validate the handle */
671 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
672 if (atomic_read(&p->adev->vce.handles[i]) == handle) {
673 if (p->adev->vce.filp[i] != p->filp) {
674 DRM_ERROR("VCE handle collision detected!\n");
681 /* handle not found try to alloc a new one */
682 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
683 if (!atomic_cmpxchg(&p->adev->vce.handles[i], 0, handle)) {
684 p->adev->vce.filp[i] = p->filp;
685 p->adev->vce.img_size[i] = 0;
686 *allocated |= 1 << i;
691 DRM_ERROR("No more free VCE handles!\n");
696 * amdgpu_vce_cs_parse - parse and validate the command stream
701 int amdgpu_vce_ring_parse_cs(struct amdgpu_cs_parser *p, uint32_t ib_idx)
703 struct amdgpu_ib *ib = &p->job->ibs[ib_idx];
704 unsigned fb_idx = 0, bs_idx = 0;
705 int session_idx = -1;
706 uint32_t destroyed = 0;
707 uint32_t created = 0;
708 uint32_t allocated = 0;
709 uint32_t tmp, handle = 0;
710 uint32_t *size = &tmp;
715 ib->gpu_addr = amdgpu_sa_bo_gpu_addr(ib->sa_bo);
717 for (idx = 0; idx < ib->length_dw;) {
718 uint32_t len = amdgpu_get_ib_value(p, ib_idx, idx);
719 uint32_t cmd = amdgpu_get_ib_value(p, ib_idx, idx + 1);
721 if ((len < 8) || (len & 3)) {
722 DRM_ERROR("invalid VCE command length (%d)!\n", len);
728 case 0x00000002: /* task info */
729 fb_idx = amdgpu_get_ib_value(p, ib_idx, idx + 6);
730 bs_idx = amdgpu_get_ib_value(p, ib_idx, idx + 7);
733 case 0x03000001: /* encode */
734 r = amdgpu_vce_validate_bo(p, ib_idx, idx + 10,
739 r = amdgpu_vce_validate_bo(p, ib_idx, idx + 12,
745 case 0x05000001: /* context buffer */
746 r = amdgpu_vce_validate_bo(p, ib_idx, idx + 3,
752 case 0x05000004: /* video bitstream buffer */
753 tmp = amdgpu_get_ib_value(p, ib_idx, idx + 4);
754 r = amdgpu_vce_validate_bo(p, ib_idx, idx + 3, idx + 2,
760 case 0x05000005: /* feedback buffer */
761 r = amdgpu_vce_validate_bo(p, ib_idx, idx + 3, idx + 2,
767 case 0x0500000d: /* MV buffer */
768 r = amdgpu_vce_validate_bo(p, ib_idx, idx + 3,
773 r = amdgpu_vce_validate_bo(p, ib_idx, idx + 8,
783 for (idx = 0; idx < ib->length_dw;) {
784 uint32_t len = amdgpu_get_ib_value(p, ib_idx, idx);
785 uint32_t cmd = amdgpu_get_ib_value(p, ib_idx, idx + 1);
788 case 0x00000001: /* session */
789 handle = amdgpu_get_ib_value(p, ib_idx, idx + 2);
790 session_idx = amdgpu_vce_validate_handle(p, handle,
792 if (session_idx < 0) {
796 size = &p->adev->vce.img_size[session_idx];
799 case 0x00000002: /* task info */
800 fb_idx = amdgpu_get_ib_value(p, ib_idx, idx + 6);
801 bs_idx = amdgpu_get_ib_value(p, ib_idx, idx + 7);
804 case 0x01000001: /* create */
805 created |= 1 << session_idx;
806 if (destroyed & (1 << session_idx)) {
807 destroyed &= ~(1 << session_idx);
808 allocated |= 1 << session_idx;
810 } else if (!(allocated & (1 << session_idx))) {
811 DRM_ERROR("Handle already in use!\n");
816 *size = amdgpu_get_ib_value(p, ib_idx, idx + 8) *
817 amdgpu_get_ib_value(p, ib_idx, idx + 10) *
821 case 0x04000001: /* config extension */
822 case 0x04000002: /* pic control */
823 case 0x04000005: /* rate control */
824 case 0x04000007: /* motion estimation */
825 case 0x04000008: /* rdo */
826 case 0x04000009: /* vui */
827 case 0x05000002: /* auxiliary buffer */
828 case 0x05000009: /* clock table */
831 case 0x0500000c: /* hw config */
832 switch (p->adev->asic_type) {
833 #ifdef CONFIG_DRM_AMDGPU_CIK
845 case 0x03000001: /* encode */
846 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 10, idx + 9,
851 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 12, idx + 11,
857 case 0x02000001: /* destroy */
858 destroyed |= 1 << session_idx;
861 case 0x05000001: /* context buffer */
862 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
868 case 0x05000004: /* video bitstream buffer */
869 tmp = amdgpu_get_ib_value(p, ib_idx, idx + 4);
870 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
876 case 0x05000005: /* feedback buffer */
877 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
883 case 0x0500000d: /* MV buffer */
884 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3,
889 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 8,
890 idx + 7, *size / 12, 0);
896 DRM_ERROR("invalid VCE command (0x%x)!\n", cmd);
901 if (session_idx == -1) {
902 DRM_ERROR("no session command at start of IB\n");
910 if (allocated & ~created) {
911 DRM_ERROR("New session without create command!\n");
917 /* No error, free all destroyed handle slots */
920 /* Error during parsing, free all allocated handle slots */
924 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
926 atomic_set(&p->adev->vce.handles[i], 0);
932 * amdgpu_vce_cs_parse_vm - parse the command stream in VM mode
937 int amdgpu_vce_ring_parse_cs_vm(struct amdgpu_cs_parser *p, uint32_t ib_idx)
939 struct amdgpu_ib *ib = &p->job->ibs[ib_idx];
940 int session_idx = -1;
941 uint32_t destroyed = 0;
942 uint32_t created = 0;
943 uint32_t allocated = 0;
944 uint32_t tmp, handle = 0;
945 int i, r = 0, idx = 0;
947 while (idx < ib->length_dw) {
948 uint32_t len = amdgpu_get_ib_value(p, ib_idx, idx);
949 uint32_t cmd = amdgpu_get_ib_value(p, ib_idx, idx + 1);
951 if ((len < 8) || (len & 3)) {
952 DRM_ERROR("invalid VCE command length (%d)!\n", len);
958 case 0x00000001: /* session */
959 handle = amdgpu_get_ib_value(p, ib_idx, idx + 2);
960 session_idx = amdgpu_vce_validate_handle(p, handle,
962 if (session_idx < 0) {
968 case 0x01000001: /* create */
969 created |= 1 << session_idx;
970 if (destroyed & (1 << session_idx)) {
971 destroyed &= ~(1 << session_idx);
972 allocated |= 1 << session_idx;
974 } else if (!(allocated & (1 << session_idx))) {
975 DRM_ERROR("Handle already in use!\n");
982 case 0x02000001: /* destroy */
983 destroyed |= 1 << session_idx;
990 if (session_idx == -1) {
991 DRM_ERROR("no session command at start of IB\n");
999 if (allocated & ~created) {
1000 DRM_ERROR("New session without create command!\n");
1006 /* No error, free all destroyed handle slots */
1008 amdgpu_ib_free(p->adev, ib, NULL);
1010 /* Error during parsing, free all allocated handle slots */
1014 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
1016 atomic_set(&p->adev->vce.handles[i], 0);
1022 * amdgpu_vce_ring_emit_ib - execute indirect buffer
1024 * @ring: engine to use
1025 * @ib: the IB to execute
1028 void amdgpu_vce_ring_emit_ib(struct amdgpu_ring *ring,
1029 struct amdgpu_job *job,
1030 struct amdgpu_ib *ib,
1033 amdgpu_ring_write(ring, VCE_CMD_IB);
1034 amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr));
1035 amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
1036 amdgpu_ring_write(ring, ib->length_dw);
1040 * amdgpu_vce_ring_emit_fence - add a fence command to the ring
1042 * @ring: engine to use
1046 void amdgpu_vce_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
1049 WARN_ON(flags & AMDGPU_FENCE_FLAG_64BIT);
1051 amdgpu_ring_write(ring, VCE_CMD_FENCE);
1052 amdgpu_ring_write(ring, addr);
1053 amdgpu_ring_write(ring, upper_32_bits(addr));
1054 amdgpu_ring_write(ring, seq);
1055 amdgpu_ring_write(ring, VCE_CMD_TRAP);
1056 amdgpu_ring_write(ring, VCE_CMD_END);
1060 * amdgpu_vce_ring_test_ring - test if VCE ring is working
1062 * @ring: the engine to test on
1065 int amdgpu_vce_ring_test_ring(struct amdgpu_ring *ring)
1067 struct amdgpu_device *adev = ring->adev;
1070 int r, timeout = adev->usec_timeout;
1072 /* skip ring test for sriov*/
1073 if (amdgpu_sriov_vf(adev))
1076 r = amdgpu_ring_alloc(ring, 16);
1080 rptr = amdgpu_ring_get_rptr(ring);
1082 amdgpu_ring_write(ring, VCE_CMD_END);
1083 amdgpu_ring_commit(ring);
1085 for (i = 0; i < timeout; i++) {
1086 if (amdgpu_ring_get_rptr(ring) != rptr)
1098 * amdgpu_vce_ring_test_ib - test if VCE IBs are working
1100 * @ring: the engine to test on
1103 int amdgpu_vce_ring_test_ib(struct amdgpu_ring *ring, long timeout)
1105 struct dma_fence *fence = NULL;
1106 struct amdgpu_bo *bo = NULL;
1109 /* skip vce ring1/2 ib test for now, since it's not reliable */
1110 if (ring != &ring->adev->vce.ring[0])
1113 r = amdgpu_bo_create_reserved(ring->adev, 512, PAGE_SIZE,
1114 AMDGPU_GEM_DOMAIN_VRAM,
1119 r = amdgpu_vce_get_create_msg(ring, 1, bo, NULL);
1123 r = amdgpu_vce_get_destroy_msg(ring, 1, true, &fence);
1127 r = dma_fence_wait_timeout(fence, false, timeout);
1134 dma_fence_put(fence);
1135 amdgpu_bo_unreserve(bo);
1136 amdgpu_bo_unref(&bo);