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;
83 r = request_firmware(&adev->vcn.fw, fw_name, adev->dev);
85 dev_err(adev->dev, "amdgpu_vcn: Can't load firmware \"%s\"\n",
90 r = amdgpu_ucode_validate(adev->vcn.fw);
92 dev_err(adev->dev, "amdgpu_vcn: Can't validate firmware \"%s\"\n",
94 release_firmware(adev->vcn.fw);
99 hdr = (const struct common_firmware_header *)adev->vcn.fw->data;
100 adev->vcn.fw_version = le32_to_cpu(hdr->ucode_version);
102 /* Bit 20-23, it is encode major and non-zero for new naming convention.
103 * This field is part of version minor and DRM_DISABLED_FLAG in old naming
104 * convention. Since the l:wq!atest version minor is 0x5B and DRM_DISABLED_FLAG
105 * is zero in old naming convention, this field is always zero so far.
106 * These four bits are used to tell which naming convention is present.
108 fw_check = (le32_to_cpu(hdr->ucode_version) >> 20) & 0xf;
110 unsigned int dec_ver, enc_major, enc_minor, vep, fw_rev;
112 fw_rev = le32_to_cpu(hdr->ucode_version) & 0xfff;
113 enc_minor = (le32_to_cpu(hdr->ucode_version) >> 12) & 0xff;
114 enc_major = fw_check;
115 dec_ver = (le32_to_cpu(hdr->ucode_version) >> 24) & 0xf;
116 vep = (le32_to_cpu(hdr->ucode_version) >> 28) & 0xf;
117 DRM_INFO("Found VCN firmware Version ENC: %hu.%hu DEC: %hu VEP: %hu Revision: %hu\n",
118 enc_major, enc_minor, dec_ver, vep, fw_rev);
120 unsigned int version_major, version_minor, family_id;
122 family_id = le32_to_cpu(hdr->ucode_version) & 0xff;
123 version_major = (le32_to_cpu(hdr->ucode_version) >> 24) & 0xff;
124 version_minor = (le32_to_cpu(hdr->ucode_version) >> 8) & 0xff;
125 DRM_INFO("Found VCN firmware Version: %hu.%hu Family ID: %hu\n",
126 version_major, version_minor, family_id);
129 bo_size = AMDGPU_VCN_STACK_SIZE + AMDGPU_VCN_CONTEXT_SIZE;
130 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
131 bo_size += AMDGPU_GPU_PAGE_ALIGN(le32_to_cpu(hdr->ucode_size_bytes) + 8);
132 r = amdgpu_bo_create_kernel(adev, bo_size, PAGE_SIZE,
133 AMDGPU_GEM_DOMAIN_VRAM, &adev->vcn.vcpu_bo,
134 &adev->vcn.gpu_addr, &adev->vcn.cpu_addr);
136 dev_err(adev->dev, "(%d) failed to allocate vcn bo\n", r);
143 int amdgpu_vcn_sw_fini(struct amdgpu_device *adev)
147 kvfree(adev->vcn.saved_bo);
149 amdgpu_bo_free_kernel(&adev->vcn.vcpu_bo,
151 (void **)&adev->vcn.cpu_addr);
153 amdgpu_ring_fini(&adev->vcn.ring_dec);
155 for (i = 0; i < adev->vcn.num_enc_rings; ++i)
156 amdgpu_ring_fini(&adev->vcn.ring_enc[i]);
158 amdgpu_ring_fini(&adev->vcn.ring_jpeg);
160 release_firmware(adev->vcn.fw);
165 int amdgpu_vcn_suspend(struct amdgpu_device *adev)
170 cancel_delayed_work_sync(&adev->vcn.idle_work);
172 if (adev->vcn.vcpu_bo == NULL)
175 size = amdgpu_bo_size(adev->vcn.vcpu_bo);
176 ptr = adev->vcn.cpu_addr;
178 adev->vcn.saved_bo = kvmalloc(size, GFP_KERNEL);
179 if (!adev->vcn.saved_bo)
182 memcpy_fromio(adev->vcn.saved_bo, ptr, size);
187 int amdgpu_vcn_resume(struct amdgpu_device *adev)
192 if (adev->vcn.vcpu_bo == NULL)
195 size = amdgpu_bo_size(adev->vcn.vcpu_bo);
196 ptr = adev->vcn.cpu_addr;
198 if (adev->vcn.saved_bo != NULL) {
199 memcpy_toio(ptr, adev->vcn.saved_bo, size);
200 kvfree(adev->vcn.saved_bo);
201 adev->vcn.saved_bo = NULL;
203 const struct common_firmware_header *hdr;
206 hdr = (const struct common_firmware_header *)adev->vcn.fw->data;
207 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
208 offset = le32_to_cpu(hdr->ucode_array_offset_bytes);
209 memcpy_toio(adev->vcn.cpu_addr, adev->vcn.fw->data + offset,
210 le32_to_cpu(hdr->ucode_size_bytes));
211 size -= le32_to_cpu(hdr->ucode_size_bytes);
212 ptr += le32_to_cpu(hdr->ucode_size_bytes);
214 memset_io(ptr, 0, size);
220 static void amdgpu_vcn_idle_work_handler(struct work_struct *work)
222 struct amdgpu_device *adev =
223 container_of(work, struct amdgpu_device, vcn.idle_work.work);
224 unsigned int fences = 0;
227 for (i = 0; i < adev->vcn.num_enc_rings; ++i) {
228 fences += amdgpu_fence_count_emitted(&adev->vcn.ring_enc[i]);
231 if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG) {
232 struct dpg_pause_state new_state;
235 new_state.fw_based = VCN_DPG_STATE__PAUSE;
237 new_state.fw_based = VCN_DPG_STATE__UNPAUSE;
239 if (amdgpu_fence_count_emitted(&adev->vcn.ring_jpeg))
240 new_state.jpeg = VCN_DPG_STATE__PAUSE;
242 new_state.jpeg = VCN_DPG_STATE__UNPAUSE;
244 adev->vcn.pause_dpg_mode(adev, &new_state);
247 fences += amdgpu_fence_count_emitted(&adev->vcn.ring_jpeg);
248 fences += amdgpu_fence_count_emitted(&adev->vcn.ring_dec);
251 amdgpu_gfx_off_ctrl(adev, true);
252 if (adev->pm.dpm_enabled)
253 amdgpu_dpm_enable_uvd(adev, false);
255 amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCN,
258 schedule_delayed_work(&adev->vcn.idle_work, VCN_IDLE_TIMEOUT);
262 void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)
264 struct amdgpu_device *adev = ring->adev;
265 bool set_clocks = !cancel_delayed_work_sync(&adev->vcn.idle_work);
268 amdgpu_gfx_off_ctrl(adev, false);
269 if (adev->pm.dpm_enabled)
270 amdgpu_dpm_enable_uvd(adev, true);
272 amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCN,
273 AMD_PG_STATE_UNGATE);
276 if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG) {
277 struct dpg_pause_state new_state;
278 unsigned int fences = 0;
281 for (i = 0; i < adev->vcn.num_enc_rings; ++i) {
282 fences += amdgpu_fence_count_emitted(&adev->vcn.ring_enc[i]);
285 new_state.fw_based = VCN_DPG_STATE__PAUSE;
287 new_state.fw_based = VCN_DPG_STATE__UNPAUSE;
289 if (amdgpu_fence_count_emitted(&adev->vcn.ring_jpeg))
290 new_state.jpeg = VCN_DPG_STATE__PAUSE;
292 new_state.jpeg = VCN_DPG_STATE__UNPAUSE;
294 if (ring->funcs->type == AMDGPU_RING_TYPE_VCN_ENC)
295 new_state.fw_based = VCN_DPG_STATE__PAUSE;
296 else if (ring->funcs->type == AMDGPU_RING_TYPE_VCN_JPEG)
297 new_state.jpeg = VCN_DPG_STATE__PAUSE;
299 adev->vcn.pause_dpg_mode(adev, &new_state);
303 void amdgpu_vcn_ring_end_use(struct amdgpu_ring *ring)
305 schedule_delayed_work(&ring->adev->vcn.idle_work, VCN_IDLE_TIMEOUT);
308 int amdgpu_vcn_dec_ring_test_ring(struct amdgpu_ring *ring)
310 struct amdgpu_device *adev = ring->adev;
315 WREG32(SOC15_REG_OFFSET(UVD, 0, mmUVD_SCRATCH9), 0xCAFEDEAD);
316 r = amdgpu_ring_alloc(ring, 3);
320 amdgpu_ring_write(ring,
321 PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_SCRATCH9), 0));
322 amdgpu_ring_write(ring, 0xDEADBEEF);
323 amdgpu_ring_commit(ring);
324 for (i = 0; i < adev->usec_timeout; i++) {
325 tmp = RREG32(SOC15_REG_OFFSET(UVD, 0, mmUVD_SCRATCH9));
326 if (tmp == 0xDEADBEEF)
331 if (i >= adev->usec_timeout)
337 static int amdgpu_vcn_dec_send_msg(struct amdgpu_ring *ring,
338 struct amdgpu_bo *bo,
339 struct dma_fence **fence)
341 struct amdgpu_device *adev = ring->adev;
342 struct dma_fence *f = NULL;
343 struct amdgpu_job *job;
344 struct amdgpu_ib *ib;
348 r = amdgpu_job_alloc_with_ib(adev, 64, &job);
353 addr = amdgpu_bo_gpu_offset(bo);
354 ib->ptr[0] = PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_GPCOM_VCPU_DATA0), 0);
356 ib->ptr[2] = PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_GPCOM_VCPU_DATA1), 0);
357 ib->ptr[3] = addr >> 32;
358 ib->ptr[4] = PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_GPCOM_VCPU_CMD), 0);
360 for (i = 6; i < 16; i += 2) {
361 ib->ptr[i] = PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_NO_OP), 0);
366 r = amdgpu_job_submit_direct(job, ring, &f);
370 amdgpu_bo_fence(bo, f, false);
371 amdgpu_bo_unreserve(bo);
372 amdgpu_bo_unref(&bo);
375 *fence = dma_fence_get(f);
381 amdgpu_job_free(job);
384 amdgpu_bo_unreserve(bo);
385 amdgpu_bo_unref(&bo);
389 static int amdgpu_vcn_dec_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
390 struct dma_fence **fence)
392 struct amdgpu_device *adev = ring->adev;
393 struct amdgpu_bo *bo = NULL;
397 r = amdgpu_bo_create_reserved(adev, 1024, PAGE_SIZE,
398 AMDGPU_GEM_DOMAIN_VRAM,
399 &bo, NULL, (void **)&msg);
403 msg[0] = cpu_to_le32(0x00000028);
404 msg[1] = cpu_to_le32(0x00000038);
405 msg[2] = cpu_to_le32(0x00000001);
406 msg[3] = cpu_to_le32(0x00000000);
407 msg[4] = cpu_to_le32(handle);
408 msg[5] = cpu_to_le32(0x00000000);
409 msg[6] = cpu_to_le32(0x00000001);
410 msg[7] = cpu_to_le32(0x00000028);
411 msg[8] = cpu_to_le32(0x00000010);
412 msg[9] = cpu_to_le32(0x00000000);
413 msg[10] = cpu_to_le32(0x00000007);
414 msg[11] = cpu_to_le32(0x00000000);
415 msg[12] = cpu_to_le32(0x00000780);
416 msg[13] = cpu_to_le32(0x00000440);
417 for (i = 14; i < 1024; ++i)
418 msg[i] = cpu_to_le32(0x0);
420 return amdgpu_vcn_dec_send_msg(ring, bo, fence);
423 static int amdgpu_vcn_dec_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
424 struct dma_fence **fence)
426 struct amdgpu_device *adev = ring->adev;
427 struct amdgpu_bo *bo = NULL;
431 r = amdgpu_bo_create_reserved(adev, 1024, PAGE_SIZE,
432 AMDGPU_GEM_DOMAIN_VRAM,
433 &bo, NULL, (void **)&msg);
437 msg[0] = cpu_to_le32(0x00000028);
438 msg[1] = cpu_to_le32(0x00000018);
439 msg[2] = cpu_to_le32(0x00000000);
440 msg[3] = cpu_to_le32(0x00000002);
441 msg[4] = cpu_to_le32(handle);
442 msg[5] = cpu_to_le32(0x00000000);
443 for (i = 6; i < 1024; ++i)
444 msg[i] = cpu_to_le32(0x0);
446 return amdgpu_vcn_dec_send_msg(ring, bo, fence);
449 int amdgpu_vcn_dec_ring_test_ib(struct amdgpu_ring *ring, long timeout)
451 struct dma_fence *fence;
454 r = amdgpu_vcn_dec_get_create_msg(ring, 1, NULL);
458 r = amdgpu_vcn_dec_get_destroy_msg(ring, 1, &fence);
462 r = dma_fence_wait_timeout(fence, false, timeout);
468 dma_fence_put(fence);
473 int amdgpu_vcn_enc_ring_test_ring(struct amdgpu_ring *ring)
475 struct amdgpu_device *adev = ring->adev;
480 r = amdgpu_ring_alloc(ring, 16);
484 rptr = amdgpu_ring_get_rptr(ring);
486 amdgpu_ring_write(ring, VCN_ENC_CMD_END);
487 amdgpu_ring_commit(ring);
489 for (i = 0; i < adev->usec_timeout; i++) {
490 if (amdgpu_ring_get_rptr(ring) != rptr)
495 if (i >= adev->usec_timeout)
501 static int amdgpu_vcn_enc_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
502 struct dma_fence **fence)
504 const unsigned ib_size_dw = 16;
505 struct amdgpu_job *job;
506 struct amdgpu_ib *ib;
507 struct dma_fence *f = NULL;
511 r = amdgpu_job_alloc_with_ib(ring->adev, ib_size_dw * 4, &job);
516 dummy = ib->gpu_addr + 1024;
519 ib->ptr[ib->length_dw++] = 0x00000018;
520 ib->ptr[ib->length_dw++] = 0x00000001; /* session info */
521 ib->ptr[ib->length_dw++] = handle;
522 ib->ptr[ib->length_dw++] = upper_32_bits(dummy);
523 ib->ptr[ib->length_dw++] = dummy;
524 ib->ptr[ib->length_dw++] = 0x0000000b;
526 ib->ptr[ib->length_dw++] = 0x00000014;
527 ib->ptr[ib->length_dw++] = 0x00000002; /* task info */
528 ib->ptr[ib->length_dw++] = 0x0000001c;
529 ib->ptr[ib->length_dw++] = 0x00000000;
530 ib->ptr[ib->length_dw++] = 0x00000000;
532 ib->ptr[ib->length_dw++] = 0x00000008;
533 ib->ptr[ib->length_dw++] = 0x08000001; /* op initialize */
535 for (i = ib->length_dw; i < ib_size_dw; ++i)
538 r = amdgpu_job_submit_direct(job, ring, &f);
543 *fence = dma_fence_get(f);
549 amdgpu_job_free(job);
553 static int amdgpu_vcn_enc_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
554 struct dma_fence **fence)
556 const unsigned ib_size_dw = 16;
557 struct amdgpu_job *job;
558 struct amdgpu_ib *ib;
559 struct dma_fence *f = NULL;
563 r = amdgpu_job_alloc_with_ib(ring->adev, ib_size_dw * 4, &job);
568 dummy = ib->gpu_addr + 1024;
571 ib->ptr[ib->length_dw++] = 0x00000018;
572 ib->ptr[ib->length_dw++] = 0x00000001;
573 ib->ptr[ib->length_dw++] = handle;
574 ib->ptr[ib->length_dw++] = upper_32_bits(dummy);
575 ib->ptr[ib->length_dw++] = dummy;
576 ib->ptr[ib->length_dw++] = 0x0000000b;
578 ib->ptr[ib->length_dw++] = 0x00000014;
579 ib->ptr[ib->length_dw++] = 0x00000002;
580 ib->ptr[ib->length_dw++] = 0x0000001c;
581 ib->ptr[ib->length_dw++] = 0x00000000;
582 ib->ptr[ib->length_dw++] = 0x00000000;
584 ib->ptr[ib->length_dw++] = 0x00000008;
585 ib->ptr[ib->length_dw++] = 0x08000002; /* op close session */
587 for (i = ib->length_dw; i < ib_size_dw; ++i)
590 r = amdgpu_job_submit_direct(job, ring, &f);
595 *fence = dma_fence_get(f);
601 amdgpu_job_free(job);
605 int amdgpu_vcn_enc_ring_test_ib(struct amdgpu_ring *ring, long timeout)
607 struct dma_fence *fence = NULL;
610 r = amdgpu_vcn_enc_get_create_msg(ring, 1, NULL);
614 r = amdgpu_vcn_enc_get_destroy_msg(ring, 1, &fence);
618 r = dma_fence_wait_timeout(fence, false, timeout);
625 dma_fence_put(fence);
629 int amdgpu_vcn_jpeg_ring_test_ring(struct amdgpu_ring *ring)
631 struct amdgpu_device *adev = ring->adev;
636 WREG32(SOC15_REG_OFFSET(UVD, 0, mmUVD_SCRATCH9), 0xCAFEDEAD);
637 r = amdgpu_ring_alloc(ring, 3);
642 amdgpu_ring_write(ring,
643 PACKETJ(SOC15_REG_OFFSET(UVD, 0, mmUVD_SCRATCH9), 0, 0, 0));
644 amdgpu_ring_write(ring, 0xDEADBEEF);
645 amdgpu_ring_commit(ring);
647 for (i = 0; i < adev->usec_timeout; i++) {
648 tmp = RREG32(SOC15_REG_OFFSET(UVD, 0, mmUVD_SCRATCH9));
649 if (tmp == 0xDEADBEEF)
654 if (i >= adev->usec_timeout)
660 static int amdgpu_vcn_jpeg_set_reg(struct amdgpu_ring *ring, uint32_t handle,
661 struct dma_fence **fence)
663 struct amdgpu_device *adev = ring->adev;
664 struct amdgpu_job *job;
665 struct amdgpu_ib *ib;
666 struct dma_fence *f = NULL;
667 const unsigned ib_size_dw = 16;
670 r = amdgpu_job_alloc_with_ib(ring->adev, ib_size_dw * 4, &job);
676 ib->ptr[0] = PACKETJ(SOC15_REG_OFFSET(UVD, 0, mmUVD_SCRATCH9), 0, 0, PACKETJ_TYPE0);
677 ib->ptr[1] = 0xDEADBEEF;
678 for (i = 2; i < 16; i += 2) {
679 ib->ptr[i] = PACKETJ(0, 0, 0, PACKETJ_TYPE6);
684 r = amdgpu_job_submit_direct(job, ring, &f);
689 *fence = dma_fence_get(f);
695 amdgpu_job_free(job);
699 int amdgpu_vcn_jpeg_ring_test_ib(struct amdgpu_ring *ring, long timeout)
701 struct amdgpu_device *adev = ring->adev;
704 struct dma_fence *fence = NULL;
707 r = amdgpu_vcn_jpeg_set_reg(ring, 1, &fence);
711 r = dma_fence_wait_timeout(fence, false, timeout);
721 for (i = 0; i < adev->usec_timeout; i++) {
722 tmp = RREG32(SOC15_REG_OFFSET(UVD, 0, mmUVD_SCRATCH9));
723 if (tmp == 0xDEADBEEF)
728 if (i >= adev->usec_timeout)
731 dma_fence_put(fence);