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>
33 #include "amdgpu_pm.h"
34 #include "amdgpu_vcn.h"
36 #include "soc15_common.h"
38 #include "vcn/vcn_1_0_offset.h"
39 #include "vcn/vcn_1_0_sh_mask.h"
41 /* 1 second timeout */
42 #define VCN_IDLE_TIMEOUT msecs_to_jiffies(1000)
45 #define FIRMWARE_RAVEN "amdgpu/raven_vcn.bin"
46 #define FIRMWARE_PICASSO "amdgpu/picasso_vcn.bin"
47 #define FIRMWARE_RAVEN2 "amdgpu/raven2_vcn.bin"
48 #define FIRMWARE_NAVI10 "amdgpu/navi10_vcn.bin"
50 MODULE_FIRMWARE(FIRMWARE_RAVEN);
51 MODULE_FIRMWARE(FIRMWARE_PICASSO);
52 MODULE_FIRMWARE(FIRMWARE_RAVEN2);
53 MODULE_FIRMWARE(FIRMWARE_NAVI10);
55 static void amdgpu_vcn_idle_work_handler(struct work_struct *work);
57 int amdgpu_vcn_sw_init(struct amdgpu_device *adev)
59 unsigned long bo_size;
61 const struct common_firmware_header *hdr;
62 unsigned char fw_check;
65 INIT_DELAYED_WORK(&adev->vcn.idle_work, amdgpu_vcn_idle_work_handler);
67 switch (adev->asic_type) {
69 if (adev->rev_id >= 8)
70 fw_name = FIRMWARE_RAVEN2;
71 else if (adev->pdev->device == 0x15d8)
72 fw_name = FIRMWARE_PICASSO;
74 fw_name = FIRMWARE_RAVEN;
77 fw_name = FIRMWARE_NAVI10;
78 if ((adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) &&
79 (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG))
80 adev->vcn.indirect_sram = true;
86 r = request_firmware(&adev->vcn.fw, fw_name, adev->dev);
88 dev_err(adev->dev, "amdgpu_vcn: Can't load firmware \"%s\"\n",
93 r = amdgpu_ucode_validate(adev->vcn.fw);
95 dev_err(adev->dev, "amdgpu_vcn: Can't validate firmware \"%s\"\n",
97 release_firmware(adev->vcn.fw);
102 hdr = (const struct common_firmware_header *)adev->vcn.fw->data;
103 adev->vcn.fw_version = le32_to_cpu(hdr->ucode_version);
105 /* Bit 20-23, it is encode major and non-zero for new naming convention.
106 * This field is part of version minor and DRM_DISABLED_FLAG in old naming
107 * convention. Since the l:wq!atest version minor is 0x5B and DRM_DISABLED_FLAG
108 * is zero in old naming convention, this field is always zero so far.
109 * These four bits are used to tell which naming convention is present.
111 fw_check = (le32_to_cpu(hdr->ucode_version) >> 20) & 0xf;
113 unsigned int dec_ver, enc_major, enc_minor, vep, fw_rev;
115 fw_rev = le32_to_cpu(hdr->ucode_version) & 0xfff;
116 enc_minor = (le32_to_cpu(hdr->ucode_version) >> 12) & 0xff;
117 enc_major = fw_check;
118 dec_ver = (le32_to_cpu(hdr->ucode_version) >> 24) & 0xf;
119 vep = (le32_to_cpu(hdr->ucode_version) >> 28) & 0xf;
120 DRM_INFO("Found VCN firmware Version ENC: %hu.%hu DEC: %hu VEP: %hu Revision: %hu\n",
121 enc_major, enc_minor, dec_ver, vep, fw_rev);
123 unsigned int version_major, version_minor, family_id;
125 family_id = le32_to_cpu(hdr->ucode_version) & 0xff;
126 version_major = (le32_to_cpu(hdr->ucode_version) >> 24) & 0xff;
127 version_minor = (le32_to_cpu(hdr->ucode_version) >> 8) & 0xff;
128 DRM_INFO("Found VCN firmware Version: %hu.%hu Family ID: %hu\n",
129 version_major, version_minor, family_id);
132 bo_size = AMDGPU_VCN_STACK_SIZE + AMDGPU_VCN_CONTEXT_SIZE;
133 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
134 bo_size += AMDGPU_GPU_PAGE_ALIGN(le32_to_cpu(hdr->ucode_size_bytes) + 8);
135 r = amdgpu_bo_create_kernel(adev, bo_size, PAGE_SIZE,
136 AMDGPU_GEM_DOMAIN_VRAM, &adev->vcn.vcpu_bo,
137 &adev->vcn.gpu_addr, &adev->vcn.cpu_addr);
139 dev_err(adev->dev, "(%d) failed to allocate vcn bo\n", r);
143 if (adev->vcn.indirect_sram) {
144 r = amdgpu_bo_create_kernel(adev, 64 * 2 * 4, PAGE_SIZE,
145 AMDGPU_GEM_DOMAIN_VRAM, &adev->vcn.dpg_sram_bo,
146 &adev->vcn.dpg_sram_gpu_addr, &adev->vcn.dpg_sram_cpu_addr);
148 dev_err(adev->dev, "(%d) failed to allocate DPG bo\n", r);
156 int amdgpu_vcn_sw_fini(struct amdgpu_device *adev)
160 kvfree(adev->vcn.saved_bo);
162 if (adev->vcn.indirect_sram) {
163 amdgpu_bo_free_kernel(&adev->vcn.dpg_sram_bo,
164 &adev->vcn.dpg_sram_gpu_addr,
165 (void **)&adev->vcn.dpg_sram_cpu_addr);
168 amdgpu_bo_free_kernel(&adev->vcn.vcpu_bo,
170 (void **)&adev->vcn.cpu_addr);
172 amdgpu_ring_fini(&adev->vcn.ring_dec);
174 for (i = 0; i < adev->vcn.num_enc_rings; ++i)
175 amdgpu_ring_fini(&adev->vcn.ring_enc[i]);
177 amdgpu_ring_fini(&adev->vcn.ring_jpeg);
179 release_firmware(adev->vcn.fw);
184 int amdgpu_vcn_suspend(struct amdgpu_device *adev)
189 cancel_delayed_work_sync(&adev->vcn.idle_work);
191 if (adev->vcn.vcpu_bo == NULL)
194 size = amdgpu_bo_size(adev->vcn.vcpu_bo);
195 ptr = adev->vcn.cpu_addr;
197 adev->vcn.saved_bo = kvmalloc(size, GFP_KERNEL);
198 if (!adev->vcn.saved_bo)
201 memcpy_fromio(adev->vcn.saved_bo, ptr, size);
206 int amdgpu_vcn_resume(struct amdgpu_device *adev)
211 if (adev->vcn.vcpu_bo == NULL)
214 size = amdgpu_bo_size(adev->vcn.vcpu_bo);
215 ptr = adev->vcn.cpu_addr;
217 if (adev->vcn.saved_bo != NULL) {
218 memcpy_toio(ptr, adev->vcn.saved_bo, size);
219 kvfree(adev->vcn.saved_bo);
220 adev->vcn.saved_bo = NULL;
222 const struct common_firmware_header *hdr;
225 hdr = (const struct common_firmware_header *)adev->vcn.fw->data;
226 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
227 offset = le32_to_cpu(hdr->ucode_array_offset_bytes);
228 memcpy_toio(adev->vcn.cpu_addr, adev->vcn.fw->data + offset,
229 le32_to_cpu(hdr->ucode_size_bytes));
230 size -= le32_to_cpu(hdr->ucode_size_bytes);
231 ptr += le32_to_cpu(hdr->ucode_size_bytes);
233 memset_io(ptr, 0, size);
239 static void amdgpu_vcn_idle_work_handler(struct work_struct *work)
241 struct amdgpu_device *adev =
242 container_of(work, struct amdgpu_device, vcn.idle_work.work);
243 unsigned int fences = 0;
246 for (i = 0; i < adev->vcn.num_enc_rings; ++i) {
247 fences += amdgpu_fence_count_emitted(&adev->vcn.ring_enc[i]);
250 if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG) {
251 struct dpg_pause_state new_state;
254 new_state.fw_based = VCN_DPG_STATE__PAUSE;
256 new_state.fw_based = VCN_DPG_STATE__UNPAUSE;
258 if (amdgpu_fence_count_emitted(&adev->vcn.ring_jpeg))
259 new_state.jpeg = VCN_DPG_STATE__PAUSE;
261 new_state.jpeg = VCN_DPG_STATE__UNPAUSE;
263 adev->vcn.pause_dpg_mode(adev, &new_state);
266 fences += amdgpu_fence_count_emitted(&adev->vcn.ring_jpeg);
267 fences += amdgpu_fence_count_emitted(&adev->vcn.ring_dec);
270 amdgpu_gfx_off_ctrl(adev, true);
271 if (adev->asic_type < CHIP_NAVI10 && adev->pm.dpm_enabled)
272 amdgpu_dpm_enable_uvd(adev, false);
274 amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCN,
277 schedule_delayed_work(&adev->vcn.idle_work, VCN_IDLE_TIMEOUT);
281 void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)
283 struct amdgpu_device *adev = ring->adev;
284 bool set_clocks = !cancel_delayed_work_sync(&adev->vcn.idle_work);
287 amdgpu_gfx_off_ctrl(adev, false);
288 if (adev->asic_type < CHIP_NAVI10 && adev->pm.dpm_enabled)
289 amdgpu_dpm_enable_uvd(adev, true);
291 amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCN,
292 AMD_PG_STATE_UNGATE);
295 if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG) {
296 struct dpg_pause_state new_state;
297 unsigned int fences = 0;
300 for (i = 0; i < adev->vcn.num_enc_rings; ++i) {
301 fences += amdgpu_fence_count_emitted(&adev->vcn.ring_enc[i]);
304 new_state.fw_based = VCN_DPG_STATE__PAUSE;
306 new_state.fw_based = VCN_DPG_STATE__UNPAUSE;
308 if (amdgpu_fence_count_emitted(&adev->vcn.ring_jpeg))
309 new_state.jpeg = VCN_DPG_STATE__PAUSE;
311 new_state.jpeg = VCN_DPG_STATE__UNPAUSE;
313 if (ring->funcs->type == AMDGPU_RING_TYPE_VCN_ENC)
314 new_state.fw_based = VCN_DPG_STATE__PAUSE;
315 else if (ring->funcs->type == AMDGPU_RING_TYPE_VCN_JPEG)
316 new_state.jpeg = VCN_DPG_STATE__PAUSE;
318 adev->vcn.pause_dpg_mode(adev, &new_state);
322 void amdgpu_vcn_ring_end_use(struct amdgpu_ring *ring)
324 schedule_delayed_work(&ring->adev->vcn.idle_work, VCN_IDLE_TIMEOUT);
327 int amdgpu_vcn_dec_ring_test_ring(struct amdgpu_ring *ring)
329 struct amdgpu_device *adev = ring->adev;
334 WREG32(adev->vcn.external.scratch9, 0xCAFEDEAD);
335 r = amdgpu_ring_alloc(ring, 3);
338 amdgpu_ring_write(ring, PACKET0(adev->vcn.internal.scratch9, 0));
339 amdgpu_ring_write(ring, 0xDEADBEEF);
340 amdgpu_ring_commit(ring);
341 for (i = 0; i < adev->usec_timeout; i++) {
342 tmp = RREG32(adev->vcn.external.scratch9);
343 if (tmp == 0xDEADBEEF)
348 if (i >= adev->usec_timeout)
354 static int amdgpu_vcn_dec_send_msg(struct amdgpu_ring *ring,
355 struct amdgpu_bo *bo,
356 struct dma_fence **fence)
358 struct amdgpu_device *adev = ring->adev;
359 struct dma_fence *f = NULL;
360 struct amdgpu_job *job;
361 struct amdgpu_ib *ib;
365 r = amdgpu_job_alloc_with_ib(adev, 64, &job);
370 addr = amdgpu_bo_gpu_offset(bo);
371 ib->ptr[0] = PACKET0(adev->vcn.internal.data0, 0);
373 ib->ptr[2] = PACKET0(adev->vcn.internal.data1, 0);
374 ib->ptr[3] = addr >> 32;
375 ib->ptr[4] = PACKET0(adev->vcn.internal.cmd, 0);
377 for (i = 6; i < 16; i += 2) {
378 ib->ptr[i] = PACKET0(adev->vcn.internal.nop, 0);
383 r = amdgpu_job_submit_direct(job, ring, &f);
387 amdgpu_bo_fence(bo, f, false);
388 amdgpu_bo_unreserve(bo);
389 amdgpu_bo_unref(&bo);
392 *fence = dma_fence_get(f);
398 amdgpu_job_free(job);
401 amdgpu_bo_unreserve(bo);
402 amdgpu_bo_unref(&bo);
406 static int amdgpu_vcn_dec_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
407 struct dma_fence **fence)
409 struct amdgpu_device *adev = ring->adev;
410 struct amdgpu_bo *bo = NULL;
414 r = amdgpu_bo_create_reserved(adev, 1024, PAGE_SIZE,
415 AMDGPU_GEM_DOMAIN_VRAM,
416 &bo, NULL, (void **)&msg);
420 msg[0] = cpu_to_le32(0x00000028);
421 msg[1] = cpu_to_le32(0x00000038);
422 msg[2] = cpu_to_le32(0x00000001);
423 msg[3] = cpu_to_le32(0x00000000);
424 msg[4] = cpu_to_le32(handle);
425 msg[5] = cpu_to_le32(0x00000000);
426 msg[6] = cpu_to_le32(0x00000001);
427 msg[7] = cpu_to_le32(0x00000028);
428 msg[8] = cpu_to_le32(0x00000010);
429 msg[9] = cpu_to_le32(0x00000000);
430 msg[10] = cpu_to_le32(0x00000007);
431 msg[11] = cpu_to_le32(0x00000000);
432 msg[12] = cpu_to_le32(0x00000780);
433 msg[13] = cpu_to_le32(0x00000440);
434 for (i = 14; i < 1024; ++i)
435 msg[i] = cpu_to_le32(0x0);
437 return amdgpu_vcn_dec_send_msg(ring, bo, fence);
440 static int amdgpu_vcn_dec_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
441 struct dma_fence **fence)
443 struct amdgpu_device *adev = ring->adev;
444 struct amdgpu_bo *bo = NULL;
448 r = amdgpu_bo_create_reserved(adev, 1024, PAGE_SIZE,
449 AMDGPU_GEM_DOMAIN_VRAM,
450 &bo, NULL, (void **)&msg);
454 msg[0] = cpu_to_le32(0x00000028);
455 msg[1] = cpu_to_le32(0x00000018);
456 msg[2] = cpu_to_le32(0x00000000);
457 msg[3] = cpu_to_le32(0x00000002);
458 msg[4] = cpu_to_le32(handle);
459 msg[5] = cpu_to_le32(0x00000000);
460 for (i = 6; i < 1024; ++i)
461 msg[i] = cpu_to_le32(0x0);
463 return amdgpu_vcn_dec_send_msg(ring, bo, fence);
466 int amdgpu_vcn_dec_ring_test_ib(struct amdgpu_ring *ring, long timeout)
468 struct dma_fence *fence;
471 r = amdgpu_vcn_dec_get_create_msg(ring, 1, NULL);
475 r = amdgpu_vcn_dec_get_destroy_msg(ring, 1, &fence);
479 r = dma_fence_wait_timeout(fence, false, timeout);
485 dma_fence_put(fence);
490 int amdgpu_vcn_enc_ring_test_ring(struct amdgpu_ring *ring)
492 struct amdgpu_device *adev = ring->adev;
497 r = amdgpu_ring_alloc(ring, 16);
501 rptr = amdgpu_ring_get_rptr(ring);
503 amdgpu_ring_write(ring, VCN_ENC_CMD_END);
504 amdgpu_ring_commit(ring);
506 for (i = 0; i < adev->usec_timeout; i++) {
507 if (amdgpu_ring_get_rptr(ring) != rptr)
512 if (i >= adev->usec_timeout)
518 static int amdgpu_vcn_enc_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
519 struct dma_fence **fence)
521 const unsigned ib_size_dw = 16;
522 struct amdgpu_job *job;
523 struct amdgpu_ib *ib;
524 struct dma_fence *f = NULL;
528 r = amdgpu_job_alloc_with_ib(ring->adev, ib_size_dw * 4, &job);
533 dummy = ib->gpu_addr + 1024;
536 ib->ptr[ib->length_dw++] = 0x00000018;
537 ib->ptr[ib->length_dw++] = 0x00000001; /* session info */
538 ib->ptr[ib->length_dw++] = handle;
539 ib->ptr[ib->length_dw++] = upper_32_bits(dummy);
540 ib->ptr[ib->length_dw++] = dummy;
541 ib->ptr[ib->length_dw++] = 0x0000000b;
543 ib->ptr[ib->length_dw++] = 0x00000014;
544 ib->ptr[ib->length_dw++] = 0x00000002; /* task info */
545 ib->ptr[ib->length_dw++] = 0x0000001c;
546 ib->ptr[ib->length_dw++] = 0x00000000;
547 ib->ptr[ib->length_dw++] = 0x00000000;
549 ib->ptr[ib->length_dw++] = 0x00000008;
550 ib->ptr[ib->length_dw++] = 0x08000001; /* op initialize */
552 for (i = ib->length_dw; i < ib_size_dw; ++i)
555 r = amdgpu_job_submit_direct(job, ring, &f);
560 *fence = dma_fence_get(f);
566 amdgpu_job_free(job);
570 static int amdgpu_vcn_enc_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
571 struct dma_fence **fence)
573 const unsigned ib_size_dw = 16;
574 struct amdgpu_job *job;
575 struct amdgpu_ib *ib;
576 struct dma_fence *f = NULL;
580 r = amdgpu_job_alloc_with_ib(ring->adev, ib_size_dw * 4, &job);
585 dummy = ib->gpu_addr + 1024;
588 ib->ptr[ib->length_dw++] = 0x00000018;
589 ib->ptr[ib->length_dw++] = 0x00000001;
590 ib->ptr[ib->length_dw++] = handle;
591 ib->ptr[ib->length_dw++] = upper_32_bits(dummy);
592 ib->ptr[ib->length_dw++] = dummy;
593 ib->ptr[ib->length_dw++] = 0x0000000b;
595 ib->ptr[ib->length_dw++] = 0x00000014;
596 ib->ptr[ib->length_dw++] = 0x00000002;
597 ib->ptr[ib->length_dw++] = 0x0000001c;
598 ib->ptr[ib->length_dw++] = 0x00000000;
599 ib->ptr[ib->length_dw++] = 0x00000000;
601 ib->ptr[ib->length_dw++] = 0x00000008;
602 ib->ptr[ib->length_dw++] = 0x08000002; /* op close session */
604 for (i = ib->length_dw; i < ib_size_dw; ++i)
607 r = amdgpu_job_submit_direct(job, ring, &f);
612 *fence = dma_fence_get(f);
618 amdgpu_job_free(job);
622 int amdgpu_vcn_enc_ring_test_ib(struct amdgpu_ring *ring, long timeout)
624 struct dma_fence *fence = NULL;
627 r = amdgpu_vcn_enc_get_create_msg(ring, 1, NULL);
631 r = amdgpu_vcn_enc_get_destroy_msg(ring, 1, &fence);
635 r = dma_fence_wait_timeout(fence, false, timeout);
642 dma_fence_put(fence);
646 int amdgpu_vcn_jpeg_ring_test_ring(struct amdgpu_ring *ring)
648 struct amdgpu_device *adev = ring->adev;
653 WREG32(adev->vcn.external.jpeg_pitch, 0xCAFEDEAD);
654 r = amdgpu_ring_alloc(ring, 3);
658 amdgpu_ring_write(ring, PACKET0(adev->vcn.internal.jpeg_pitch, 0));
659 amdgpu_ring_write(ring, 0xDEADBEEF);
660 amdgpu_ring_commit(ring);
662 for (i = 0; i < adev->usec_timeout; i++) {
663 tmp = RREG32(adev->vcn.external.jpeg_pitch);
664 if (tmp == 0xDEADBEEF)
669 if (i >= adev->usec_timeout)
675 static int amdgpu_vcn_jpeg_set_reg(struct amdgpu_ring *ring, uint32_t handle,
676 struct dma_fence **fence)
678 struct amdgpu_device *adev = ring->adev;
679 struct amdgpu_job *job;
680 struct amdgpu_ib *ib;
681 struct dma_fence *f = NULL;
682 const unsigned ib_size_dw = 16;
685 r = amdgpu_job_alloc_with_ib(ring->adev, ib_size_dw * 4, &job);
691 ib->ptr[0] = PACKETJ(adev->vcn.internal.jpeg_pitch, 0, 0, PACKETJ_TYPE0);
692 ib->ptr[1] = 0xDEADBEEF;
693 for (i = 2; i < 16; i += 2) {
694 ib->ptr[i] = PACKETJ(0, 0, 0, PACKETJ_TYPE6);
699 r = amdgpu_job_submit_direct(job, ring, &f);
704 *fence = dma_fence_get(f);
710 amdgpu_job_free(job);
714 int amdgpu_vcn_jpeg_ring_test_ib(struct amdgpu_ring *ring, long timeout)
716 struct amdgpu_device *adev = ring->adev;
719 struct dma_fence *fence = NULL;
722 r = amdgpu_vcn_jpeg_set_reg(ring, 1, &fence);
726 r = dma_fence_wait_timeout(fence, false, timeout);
736 for (i = 0; i < adev->usec_timeout; i++) {
737 tmp = RREG32(adev->vcn.external.jpeg_pitch);
738 if (tmp == 0xDEADBEEF)
743 if (i >= adev->usec_timeout)
746 dma_fence_put(fence);