2 * Copyright 2016 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
27 #include <linux/firmware.h>
28 #include <linux/module.h>
29 #include <linux/pci.h>
34 #include "amdgpu_pm.h"
35 #include "amdgpu_vcn.h"
37 #include "soc15_common.h"
39 #include "vcn/vcn_1_0_offset.h"
40 #include "vcn/vcn_1_0_sh_mask.h"
42 /* 1 second timeout */
43 #define VCN_IDLE_TIMEOUT msecs_to_jiffies(1000)
46 #define FIRMWARE_RAVEN "amdgpu/raven_vcn.bin"
47 #define FIRMWARE_PICASSO "amdgpu/picasso_vcn.bin"
48 #define FIRMWARE_RAVEN2 "amdgpu/raven2_vcn.bin"
49 #define FIRMWARE_NAVI10 "amdgpu/navi10_vcn.bin"
51 MODULE_FIRMWARE(FIRMWARE_RAVEN);
52 MODULE_FIRMWARE(FIRMWARE_PICASSO);
53 MODULE_FIRMWARE(FIRMWARE_RAVEN2);
54 MODULE_FIRMWARE(FIRMWARE_NAVI10);
56 static void amdgpu_vcn_idle_work_handler(struct work_struct *work);
58 int amdgpu_vcn_sw_init(struct amdgpu_device *adev)
60 unsigned long bo_size;
62 const struct common_firmware_header *hdr;
63 unsigned char fw_check;
66 INIT_DELAYED_WORK(&adev->vcn.idle_work, amdgpu_vcn_idle_work_handler);
68 switch (adev->asic_type) {
70 if (adev->rev_id >= 8)
71 fw_name = FIRMWARE_RAVEN2;
72 else if (adev->pdev->device == 0x15d8)
73 fw_name = FIRMWARE_PICASSO;
75 fw_name = FIRMWARE_RAVEN;
78 fw_name = FIRMWARE_NAVI10;
79 if ((adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) &&
80 (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG))
81 adev->vcn.indirect_sram = true;
87 r = request_firmware(&adev->vcn.fw, fw_name, adev->dev);
89 dev_err(adev->dev, "amdgpu_vcn: Can't load firmware \"%s\"\n",
94 r = amdgpu_ucode_validate(adev->vcn.fw);
96 dev_err(adev->dev, "amdgpu_vcn: Can't validate firmware \"%s\"\n",
98 release_firmware(adev->vcn.fw);
103 hdr = (const struct common_firmware_header *)adev->vcn.fw->data;
104 adev->vcn.fw_version = le32_to_cpu(hdr->ucode_version);
106 /* Bit 20-23, it is encode major and non-zero for new naming convention.
107 * This field is part of version minor and DRM_DISABLED_FLAG in old naming
108 * convention. Since the l:wq!atest version minor is 0x5B and DRM_DISABLED_FLAG
109 * is zero in old naming convention, this field is always zero so far.
110 * These four bits are used to tell which naming convention is present.
112 fw_check = (le32_to_cpu(hdr->ucode_version) >> 20) & 0xf;
114 unsigned int dec_ver, enc_major, enc_minor, vep, fw_rev;
116 fw_rev = le32_to_cpu(hdr->ucode_version) & 0xfff;
117 enc_minor = (le32_to_cpu(hdr->ucode_version) >> 12) & 0xff;
118 enc_major = fw_check;
119 dec_ver = (le32_to_cpu(hdr->ucode_version) >> 24) & 0xf;
120 vep = (le32_to_cpu(hdr->ucode_version) >> 28) & 0xf;
121 DRM_INFO("Found VCN firmware Version ENC: %hu.%hu DEC: %hu VEP: %hu Revision: %hu\n",
122 enc_major, enc_minor, dec_ver, vep, fw_rev);
124 unsigned int version_major, version_minor, family_id;
126 family_id = le32_to_cpu(hdr->ucode_version) & 0xff;
127 version_major = (le32_to_cpu(hdr->ucode_version) >> 24) & 0xff;
128 version_minor = (le32_to_cpu(hdr->ucode_version) >> 8) & 0xff;
129 DRM_INFO("Found VCN firmware Version: %hu.%hu Family ID: %hu\n",
130 version_major, version_minor, family_id);
133 bo_size = AMDGPU_VCN_STACK_SIZE + AMDGPU_VCN_CONTEXT_SIZE;
134 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
135 bo_size += AMDGPU_GPU_PAGE_ALIGN(le32_to_cpu(hdr->ucode_size_bytes) + 8);
136 r = amdgpu_bo_create_kernel(adev, bo_size, PAGE_SIZE,
137 AMDGPU_GEM_DOMAIN_VRAM, &adev->vcn.vcpu_bo,
138 &adev->vcn.gpu_addr, &adev->vcn.cpu_addr);
140 dev_err(adev->dev, "(%d) failed to allocate vcn bo\n", r);
144 if (adev->vcn.indirect_sram) {
145 r = amdgpu_bo_create_kernel(adev, 64 * 2 * 4, PAGE_SIZE,
146 AMDGPU_GEM_DOMAIN_VRAM, &adev->vcn.dpg_sram_bo,
147 &adev->vcn.dpg_sram_gpu_addr, &adev->vcn.dpg_sram_cpu_addr);
149 dev_err(adev->dev, "(%d) failed to allocate DPG bo\n", r);
157 int amdgpu_vcn_sw_fini(struct amdgpu_device *adev)
161 kvfree(adev->vcn.saved_bo);
163 if (adev->vcn.indirect_sram) {
164 amdgpu_bo_free_kernel(&adev->vcn.dpg_sram_bo,
165 &adev->vcn.dpg_sram_gpu_addr,
166 (void **)&adev->vcn.dpg_sram_cpu_addr);
169 amdgpu_bo_free_kernel(&adev->vcn.vcpu_bo,
171 (void **)&adev->vcn.cpu_addr);
173 amdgpu_ring_fini(&adev->vcn.ring_dec);
175 for (i = 0; i < adev->vcn.num_enc_rings; ++i)
176 amdgpu_ring_fini(&adev->vcn.ring_enc[i]);
178 amdgpu_ring_fini(&adev->vcn.ring_jpeg);
180 release_firmware(adev->vcn.fw);
185 int amdgpu_vcn_suspend(struct amdgpu_device *adev)
190 cancel_delayed_work_sync(&adev->vcn.idle_work);
192 if (adev->vcn.vcpu_bo == NULL)
195 size = amdgpu_bo_size(adev->vcn.vcpu_bo);
196 ptr = adev->vcn.cpu_addr;
198 adev->vcn.saved_bo = kvmalloc(size, GFP_KERNEL);
199 if (!adev->vcn.saved_bo)
202 memcpy_fromio(adev->vcn.saved_bo, ptr, size);
207 int amdgpu_vcn_resume(struct amdgpu_device *adev)
212 if (adev->vcn.vcpu_bo == NULL)
215 size = amdgpu_bo_size(adev->vcn.vcpu_bo);
216 ptr = adev->vcn.cpu_addr;
218 if (adev->vcn.saved_bo != NULL) {
219 memcpy_toio(ptr, adev->vcn.saved_bo, size);
220 kvfree(adev->vcn.saved_bo);
221 adev->vcn.saved_bo = NULL;
223 const struct common_firmware_header *hdr;
226 hdr = (const struct common_firmware_header *)adev->vcn.fw->data;
227 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
228 offset = le32_to_cpu(hdr->ucode_array_offset_bytes);
229 memcpy_toio(adev->vcn.cpu_addr, adev->vcn.fw->data + offset,
230 le32_to_cpu(hdr->ucode_size_bytes));
231 size -= le32_to_cpu(hdr->ucode_size_bytes);
232 ptr += le32_to_cpu(hdr->ucode_size_bytes);
234 memset_io(ptr, 0, size);
240 static void amdgpu_vcn_idle_work_handler(struct work_struct *work)
242 struct amdgpu_device *adev =
243 container_of(work, struct amdgpu_device, vcn.idle_work.work);
244 unsigned int fences = 0;
247 for (i = 0; i < adev->vcn.num_enc_rings; ++i) {
248 fences += amdgpu_fence_count_emitted(&adev->vcn.ring_enc[i]);
251 if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG) {
252 struct dpg_pause_state new_state;
255 new_state.fw_based = VCN_DPG_STATE__PAUSE;
257 new_state.fw_based = VCN_DPG_STATE__UNPAUSE;
259 if (amdgpu_fence_count_emitted(&adev->vcn.ring_jpeg))
260 new_state.jpeg = VCN_DPG_STATE__PAUSE;
262 new_state.jpeg = VCN_DPG_STATE__UNPAUSE;
264 adev->vcn.pause_dpg_mode(adev, &new_state);
267 fences += amdgpu_fence_count_emitted(&adev->vcn.ring_jpeg);
268 fences += amdgpu_fence_count_emitted(&adev->vcn.ring_dec);
271 amdgpu_gfx_off_ctrl(adev, true);
272 if (adev->asic_type < CHIP_NAVI10 && adev->pm.dpm_enabled)
273 amdgpu_dpm_enable_uvd(adev, false);
275 amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCN,
278 schedule_delayed_work(&adev->vcn.idle_work, VCN_IDLE_TIMEOUT);
282 void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)
284 struct amdgpu_device *adev = ring->adev;
285 bool set_clocks = !cancel_delayed_work_sync(&adev->vcn.idle_work);
288 amdgpu_gfx_off_ctrl(adev, false);
289 if (adev->asic_type < CHIP_NAVI10 && adev->pm.dpm_enabled)
290 amdgpu_dpm_enable_uvd(adev, true);
292 amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCN,
293 AMD_PG_STATE_UNGATE);
296 if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG) {
297 struct dpg_pause_state new_state;
298 unsigned int fences = 0;
301 for (i = 0; i < adev->vcn.num_enc_rings; ++i) {
302 fences += amdgpu_fence_count_emitted(&adev->vcn.ring_enc[i]);
305 new_state.fw_based = VCN_DPG_STATE__PAUSE;
307 new_state.fw_based = VCN_DPG_STATE__UNPAUSE;
309 if (amdgpu_fence_count_emitted(&adev->vcn.ring_jpeg))
310 new_state.jpeg = VCN_DPG_STATE__PAUSE;
312 new_state.jpeg = VCN_DPG_STATE__UNPAUSE;
314 if (ring->funcs->type == AMDGPU_RING_TYPE_VCN_ENC)
315 new_state.fw_based = VCN_DPG_STATE__PAUSE;
316 else if (ring->funcs->type == AMDGPU_RING_TYPE_VCN_JPEG)
317 new_state.jpeg = VCN_DPG_STATE__PAUSE;
319 adev->vcn.pause_dpg_mode(adev, &new_state);
323 void amdgpu_vcn_ring_end_use(struct amdgpu_ring *ring)
325 schedule_delayed_work(&ring->adev->vcn.idle_work, VCN_IDLE_TIMEOUT);
328 int amdgpu_vcn_dec_ring_test_ring(struct amdgpu_ring *ring)
330 struct amdgpu_device *adev = ring->adev;
335 WREG32(adev->vcn.external.scratch9, 0xCAFEDEAD);
336 r = amdgpu_ring_alloc(ring, 3);
339 amdgpu_ring_write(ring, PACKET0(adev->vcn.internal.scratch9, 0));
340 amdgpu_ring_write(ring, 0xDEADBEEF);
341 amdgpu_ring_commit(ring);
342 for (i = 0; i < adev->usec_timeout; i++) {
343 tmp = RREG32(adev->vcn.external.scratch9);
344 if (tmp == 0xDEADBEEF)
349 if (i >= adev->usec_timeout)
355 static int amdgpu_vcn_dec_send_msg(struct amdgpu_ring *ring,
356 struct amdgpu_bo *bo,
357 struct dma_fence **fence)
359 struct amdgpu_device *adev = ring->adev;
360 struct dma_fence *f = NULL;
361 struct amdgpu_job *job;
362 struct amdgpu_ib *ib;
366 r = amdgpu_job_alloc_with_ib(adev, 64, &job);
371 addr = amdgpu_bo_gpu_offset(bo);
372 ib->ptr[0] = PACKET0(adev->vcn.internal.data0, 0);
374 ib->ptr[2] = PACKET0(adev->vcn.internal.data1, 0);
375 ib->ptr[3] = addr >> 32;
376 ib->ptr[4] = PACKET0(adev->vcn.internal.cmd, 0);
378 for (i = 6; i < 16; i += 2) {
379 ib->ptr[i] = PACKET0(adev->vcn.internal.nop, 0);
384 r = amdgpu_job_submit_direct(job, ring, &f);
388 amdgpu_bo_fence(bo, f, false);
389 amdgpu_bo_unreserve(bo);
390 amdgpu_bo_unref(&bo);
393 *fence = dma_fence_get(f);
399 amdgpu_job_free(job);
402 amdgpu_bo_unreserve(bo);
403 amdgpu_bo_unref(&bo);
407 static int amdgpu_vcn_dec_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
408 struct dma_fence **fence)
410 struct amdgpu_device *adev = ring->adev;
411 struct amdgpu_bo *bo = NULL;
415 r = amdgpu_bo_create_reserved(adev, 1024, PAGE_SIZE,
416 AMDGPU_GEM_DOMAIN_VRAM,
417 &bo, NULL, (void **)&msg);
421 msg[0] = cpu_to_le32(0x00000028);
422 msg[1] = cpu_to_le32(0x00000038);
423 msg[2] = cpu_to_le32(0x00000001);
424 msg[3] = cpu_to_le32(0x00000000);
425 msg[4] = cpu_to_le32(handle);
426 msg[5] = cpu_to_le32(0x00000000);
427 msg[6] = cpu_to_le32(0x00000001);
428 msg[7] = cpu_to_le32(0x00000028);
429 msg[8] = cpu_to_le32(0x00000010);
430 msg[9] = cpu_to_le32(0x00000000);
431 msg[10] = cpu_to_le32(0x00000007);
432 msg[11] = cpu_to_le32(0x00000000);
433 msg[12] = cpu_to_le32(0x00000780);
434 msg[13] = cpu_to_le32(0x00000440);
435 for (i = 14; i < 1024; ++i)
436 msg[i] = cpu_to_le32(0x0);
438 return amdgpu_vcn_dec_send_msg(ring, bo, fence);
441 static int amdgpu_vcn_dec_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
442 struct dma_fence **fence)
444 struct amdgpu_device *adev = ring->adev;
445 struct amdgpu_bo *bo = NULL;
449 r = amdgpu_bo_create_reserved(adev, 1024, PAGE_SIZE,
450 AMDGPU_GEM_DOMAIN_VRAM,
451 &bo, NULL, (void **)&msg);
455 msg[0] = cpu_to_le32(0x00000028);
456 msg[1] = cpu_to_le32(0x00000018);
457 msg[2] = cpu_to_le32(0x00000000);
458 msg[3] = cpu_to_le32(0x00000002);
459 msg[4] = cpu_to_le32(handle);
460 msg[5] = cpu_to_le32(0x00000000);
461 for (i = 6; i < 1024; ++i)
462 msg[i] = cpu_to_le32(0x0);
464 return amdgpu_vcn_dec_send_msg(ring, bo, fence);
467 int amdgpu_vcn_dec_ring_test_ib(struct amdgpu_ring *ring, long timeout)
469 struct dma_fence *fence;
472 r = amdgpu_vcn_dec_get_create_msg(ring, 1, NULL);
476 r = amdgpu_vcn_dec_get_destroy_msg(ring, 1, &fence);
480 r = dma_fence_wait_timeout(fence, false, timeout);
486 dma_fence_put(fence);
491 int amdgpu_vcn_enc_ring_test_ring(struct amdgpu_ring *ring)
493 struct amdgpu_device *adev = ring->adev;
498 r = amdgpu_ring_alloc(ring, 16);
502 rptr = amdgpu_ring_get_rptr(ring);
504 amdgpu_ring_write(ring, VCN_ENC_CMD_END);
505 amdgpu_ring_commit(ring);
507 for (i = 0; i < adev->usec_timeout; i++) {
508 if (amdgpu_ring_get_rptr(ring) != rptr)
513 if (i >= adev->usec_timeout)
519 static int amdgpu_vcn_enc_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
520 struct dma_fence **fence)
522 const unsigned ib_size_dw = 16;
523 struct amdgpu_job *job;
524 struct amdgpu_ib *ib;
525 struct dma_fence *f = NULL;
529 r = amdgpu_job_alloc_with_ib(ring->adev, ib_size_dw * 4, &job);
534 dummy = ib->gpu_addr + 1024;
537 ib->ptr[ib->length_dw++] = 0x00000018;
538 ib->ptr[ib->length_dw++] = 0x00000001; /* session info */
539 ib->ptr[ib->length_dw++] = handle;
540 ib->ptr[ib->length_dw++] = upper_32_bits(dummy);
541 ib->ptr[ib->length_dw++] = dummy;
542 ib->ptr[ib->length_dw++] = 0x0000000b;
544 ib->ptr[ib->length_dw++] = 0x00000014;
545 ib->ptr[ib->length_dw++] = 0x00000002; /* task info */
546 ib->ptr[ib->length_dw++] = 0x0000001c;
547 ib->ptr[ib->length_dw++] = 0x00000000;
548 ib->ptr[ib->length_dw++] = 0x00000000;
550 ib->ptr[ib->length_dw++] = 0x00000008;
551 ib->ptr[ib->length_dw++] = 0x08000001; /* op initialize */
553 for (i = ib->length_dw; i < ib_size_dw; ++i)
556 r = amdgpu_job_submit_direct(job, ring, &f);
561 *fence = dma_fence_get(f);
567 amdgpu_job_free(job);
571 static int amdgpu_vcn_enc_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
572 struct dma_fence **fence)
574 const unsigned ib_size_dw = 16;
575 struct amdgpu_job *job;
576 struct amdgpu_ib *ib;
577 struct dma_fence *f = NULL;
581 r = amdgpu_job_alloc_with_ib(ring->adev, ib_size_dw * 4, &job);
586 dummy = ib->gpu_addr + 1024;
589 ib->ptr[ib->length_dw++] = 0x00000018;
590 ib->ptr[ib->length_dw++] = 0x00000001;
591 ib->ptr[ib->length_dw++] = handle;
592 ib->ptr[ib->length_dw++] = upper_32_bits(dummy);
593 ib->ptr[ib->length_dw++] = dummy;
594 ib->ptr[ib->length_dw++] = 0x0000000b;
596 ib->ptr[ib->length_dw++] = 0x00000014;
597 ib->ptr[ib->length_dw++] = 0x00000002;
598 ib->ptr[ib->length_dw++] = 0x0000001c;
599 ib->ptr[ib->length_dw++] = 0x00000000;
600 ib->ptr[ib->length_dw++] = 0x00000000;
602 ib->ptr[ib->length_dw++] = 0x00000008;
603 ib->ptr[ib->length_dw++] = 0x08000002; /* op close session */
605 for (i = ib->length_dw; i < ib_size_dw; ++i)
608 r = amdgpu_job_submit_direct(job, ring, &f);
613 *fence = dma_fence_get(f);
619 amdgpu_job_free(job);
623 int amdgpu_vcn_enc_ring_test_ib(struct amdgpu_ring *ring, long timeout)
625 struct dma_fence *fence = NULL;
628 r = amdgpu_vcn_enc_get_create_msg(ring, 1, NULL);
632 r = amdgpu_vcn_enc_get_destroy_msg(ring, 1, &fence);
636 r = dma_fence_wait_timeout(fence, false, timeout);
643 dma_fence_put(fence);
647 int amdgpu_vcn_jpeg_ring_test_ring(struct amdgpu_ring *ring)
649 struct amdgpu_device *adev = ring->adev;
654 WREG32(adev->vcn.external.jpeg_pitch, 0xCAFEDEAD);
655 r = amdgpu_ring_alloc(ring, 3);
659 amdgpu_ring_write(ring, PACKET0(adev->vcn.internal.jpeg_pitch, 0));
660 amdgpu_ring_write(ring, 0xDEADBEEF);
661 amdgpu_ring_commit(ring);
663 for (i = 0; i < adev->usec_timeout; i++) {
664 tmp = RREG32(adev->vcn.external.jpeg_pitch);
665 if (tmp == 0xDEADBEEF)
670 if (i >= adev->usec_timeout)
676 static int amdgpu_vcn_jpeg_set_reg(struct amdgpu_ring *ring, uint32_t handle,
677 struct dma_fence **fence)
679 struct amdgpu_device *adev = ring->adev;
680 struct amdgpu_job *job;
681 struct amdgpu_ib *ib;
682 struct dma_fence *f = NULL;
683 const unsigned ib_size_dw = 16;
686 r = amdgpu_job_alloc_with_ib(ring->adev, ib_size_dw * 4, &job);
692 ib->ptr[0] = PACKETJ(adev->vcn.internal.jpeg_pitch, 0, 0, PACKETJ_TYPE0);
693 ib->ptr[1] = 0xDEADBEEF;
694 for (i = 2; i < 16; i += 2) {
695 ib->ptr[i] = PACKETJ(0, 0, 0, PACKETJ_TYPE6);
700 r = amdgpu_job_submit_direct(job, ring, &f);
705 *fence = dma_fence_get(f);
711 amdgpu_job_free(job);
715 int amdgpu_vcn_jpeg_ring_test_ib(struct amdgpu_ring *ring, long timeout)
717 struct amdgpu_device *adev = ring->adev;
720 struct dma_fence *fence = NULL;
723 r = amdgpu_vcn_jpeg_set_reg(ring, 1, &fence);
727 r = dma_fence_wait_timeout(fence, false, timeout);
737 for (i = 0; i < adev->usec_timeout; i++) {
738 tmp = RREG32(adev->vcn.external.jpeg_pitch);
739 if (tmp == 0xDEADBEEF)
744 if (i >= adev->usec_timeout)
747 dma_fence_put(fence);