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>
31 #include "amdgpu_vce.h"
33 #include "vce/vce_2_0_d.h"
34 #include "vce/vce_2_0_sh_mask.h"
35 #include "smu/smu_7_0_1_d.h"
36 #include "smu/smu_7_0_1_sh_mask.h"
37 #include "oss/oss_2_0_d.h"
38 #include "oss/oss_2_0_sh_mask.h"
40 #define VCE_V2_0_FW_SIZE (256 * 1024)
41 #define VCE_V2_0_STACK_SIZE (64 * 1024)
42 #define VCE_V2_0_DATA_SIZE (23552 * AMDGPU_MAX_VCE_HANDLES)
43 #define VCE_STATUS_VCPU_REPORT_FW_LOADED_MASK 0x02
45 static void vce_v2_0_set_ring_funcs(struct amdgpu_device *adev);
46 static void vce_v2_0_set_irq_funcs(struct amdgpu_device *adev);
49 * vce_v2_0_ring_get_rptr - get read pointer
51 * @ring: amdgpu_ring pointer
53 * Returns the current hardware read pointer
55 static uint64_t vce_v2_0_ring_get_rptr(struct amdgpu_ring *ring)
57 struct amdgpu_device *adev = ring->adev;
60 return RREG32(mmVCE_RB_RPTR);
62 return RREG32(mmVCE_RB_RPTR2);
66 * vce_v2_0_ring_get_wptr - get write pointer
68 * @ring: amdgpu_ring pointer
70 * Returns the current hardware write pointer
72 static uint64_t vce_v2_0_ring_get_wptr(struct amdgpu_ring *ring)
74 struct amdgpu_device *adev = ring->adev;
77 return RREG32(mmVCE_RB_WPTR);
79 return RREG32(mmVCE_RB_WPTR2);
83 * vce_v2_0_ring_set_wptr - set write pointer
85 * @ring: amdgpu_ring pointer
87 * Commits the write pointer to the hardware
89 static void vce_v2_0_ring_set_wptr(struct amdgpu_ring *ring)
91 struct amdgpu_device *adev = ring->adev;
94 WREG32(mmVCE_RB_WPTR, lower_32_bits(ring->wptr));
96 WREG32(mmVCE_RB_WPTR2, lower_32_bits(ring->wptr));
99 static int vce_v2_0_lmi_clean(struct amdgpu_device *adev)
103 for (i = 0; i < 10; ++i) {
104 for (j = 0; j < 100; ++j) {
105 uint32_t status = RREG32(mmVCE_LMI_STATUS);
116 static int vce_v2_0_firmware_loaded(struct amdgpu_device *adev)
120 for (i = 0; i < 10; ++i) {
121 for (j = 0; j < 100; ++j) {
122 uint32_t status = RREG32(mmVCE_STATUS);
124 if (status & VCE_STATUS_VCPU_REPORT_FW_LOADED_MASK)
129 DRM_ERROR("VCE not responding, trying to reset the ECPU!!!\n");
130 WREG32_P(mmVCE_SOFT_RESET,
131 VCE_SOFT_RESET__ECPU_SOFT_RESET_MASK,
132 ~VCE_SOFT_RESET__ECPU_SOFT_RESET_MASK);
134 WREG32_P(mmVCE_SOFT_RESET, 0,
135 ~VCE_SOFT_RESET__ECPU_SOFT_RESET_MASK);
142 static void vce_v2_0_disable_cg(struct amdgpu_device *adev)
144 WREG32(mmVCE_CGTT_CLK_OVERRIDE, 7);
147 static void vce_v2_0_init_cg(struct amdgpu_device *adev)
151 tmp = RREG32(mmVCE_CLOCK_GATING_A);
153 tmp |= ((0 << 0) | (4 << 4));
155 WREG32(mmVCE_CLOCK_GATING_A, tmp);
157 tmp = RREG32(mmVCE_UENC_CLOCK_GATING);
159 tmp |= ((0 << 0) | (4 << 4));
160 WREG32(mmVCE_UENC_CLOCK_GATING, tmp);
162 tmp = RREG32(mmVCE_CLOCK_GATING_B);
165 WREG32(mmVCE_CLOCK_GATING_B, tmp);
168 static void vce_v2_0_mc_resume(struct amdgpu_device *adev)
170 uint32_t size, offset;
172 WREG32_P(mmVCE_CLOCK_GATING_A, 0, ~(1 << 16));
173 WREG32_P(mmVCE_UENC_CLOCK_GATING, 0x1FF000, ~0xFF9FF000);
174 WREG32_P(mmVCE_UENC_REG_CLOCK_GATING, 0x3F, ~0x3F);
175 WREG32(mmVCE_CLOCK_GATING_B, 0xf7);
177 WREG32(mmVCE_LMI_CTRL, 0x00398000);
178 WREG32_P(mmVCE_LMI_CACHE_CTRL, 0x0, ~0x1);
179 WREG32(mmVCE_LMI_SWAP_CNTL, 0);
180 WREG32(mmVCE_LMI_SWAP_CNTL1, 0);
181 WREG32(mmVCE_LMI_VM_CTRL, 0);
183 WREG32(mmVCE_LMI_VCPU_CACHE_40BIT_BAR, (adev->vce.gpu_addr >> 8));
185 offset = AMDGPU_VCE_FIRMWARE_OFFSET;
186 size = VCE_V2_0_FW_SIZE;
187 WREG32(mmVCE_VCPU_CACHE_OFFSET0, offset & 0x7fffffff);
188 WREG32(mmVCE_VCPU_CACHE_SIZE0, size);
191 size = VCE_V2_0_STACK_SIZE;
192 WREG32(mmVCE_VCPU_CACHE_OFFSET1, offset & 0x7fffffff);
193 WREG32(mmVCE_VCPU_CACHE_SIZE1, size);
196 size = VCE_V2_0_DATA_SIZE;
197 WREG32(mmVCE_VCPU_CACHE_OFFSET2, offset & 0x7fffffff);
198 WREG32(mmVCE_VCPU_CACHE_SIZE2, size);
200 WREG32_P(mmVCE_LMI_CTRL2, 0x0, ~0x100);
201 WREG32_FIELD(VCE_SYS_INT_EN, VCE_SYS_INT_TRAP_INTERRUPT_EN, 1);
204 static bool vce_v2_0_is_idle(void *handle)
206 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
208 return !(RREG32(mmSRBM_STATUS2) & SRBM_STATUS2__VCE_BUSY_MASK);
211 static int vce_v2_0_wait_for_idle(struct amdgpu_ip_block *ip_block)
213 struct amdgpu_device *adev = ip_block->adev;
216 for (i = 0; i < adev->usec_timeout; i++) {
217 if (vce_v2_0_is_idle(adev))
224 * vce_v2_0_start - start VCE block
226 * @adev: amdgpu_device pointer
228 * Setup and start the VCE block
230 static int vce_v2_0_start(struct amdgpu_device *adev)
232 struct amdgpu_ring *ring;
236 WREG32_P(mmVCE_STATUS, 1, ~1);
238 vce_v2_0_init_cg(adev);
239 vce_v2_0_disable_cg(adev);
241 vce_v2_0_mc_resume(adev);
243 ring = &adev->vce.ring[0];
244 WREG32(mmVCE_RB_RPTR, lower_32_bits(ring->wptr));
245 WREG32(mmVCE_RB_WPTR, lower_32_bits(ring->wptr));
246 WREG32(mmVCE_RB_BASE_LO, ring->gpu_addr);
247 WREG32(mmVCE_RB_BASE_HI, upper_32_bits(ring->gpu_addr));
248 WREG32(mmVCE_RB_SIZE, ring->ring_size / 4);
250 ring = &adev->vce.ring[1];
251 WREG32(mmVCE_RB_RPTR2, lower_32_bits(ring->wptr));
252 WREG32(mmVCE_RB_WPTR2, lower_32_bits(ring->wptr));
253 WREG32(mmVCE_RB_BASE_LO2, ring->gpu_addr);
254 WREG32(mmVCE_RB_BASE_HI2, upper_32_bits(ring->gpu_addr));
255 WREG32(mmVCE_RB_SIZE2, ring->ring_size / 4);
257 WREG32_FIELD(VCE_VCPU_CNTL, CLK_EN, 1);
258 WREG32_FIELD(VCE_SOFT_RESET, ECPU_SOFT_RESET, 1);
260 WREG32_FIELD(VCE_SOFT_RESET, ECPU_SOFT_RESET, 0);
262 r = vce_v2_0_firmware_loaded(adev);
264 /* clear BUSY flag */
265 WREG32_P(mmVCE_STATUS, 0, ~1);
268 DRM_ERROR("VCE not responding, giving up!!!\n");
275 static int vce_v2_0_stop(struct amdgpu_device *adev)
277 struct amdgpu_ip_block *ip_block;
282 if (vce_v2_0_lmi_clean(adev)) {
283 DRM_INFO("vce is not idle \n");
287 ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_VCN);
291 if (vce_v2_0_wait_for_idle(ip_block)) {
292 DRM_INFO("VCE is busy, Can't set clock gating");
296 /* Stall UMC and register bus before resetting VCPU */
297 WREG32_P(mmVCE_LMI_CTRL2, 1 << 8, ~(1 << 8));
299 for (i = 0; i < 100; ++i) {
300 status = RREG32(mmVCE_LMI_STATUS);
306 WREG32_P(mmVCE_VCPU_CNTL, 0, ~0x80001);
308 /* put LMI, VCPU, RBC etc... into reset */
309 WREG32_P(mmVCE_SOFT_RESET, 1, ~0x1);
311 WREG32(mmVCE_STATUS, 0);
316 static void vce_v2_0_set_sw_cg(struct amdgpu_device *adev, bool gated)
321 tmp = RREG32(mmVCE_CLOCK_GATING_B);
323 WREG32(mmVCE_CLOCK_GATING_B, tmp);
325 tmp = RREG32(mmVCE_UENC_CLOCK_GATING);
327 WREG32(mmVCE_UENC_CLOCK_GATING, tmp);
329 tmp = RREG32(mmVCE_UENC_REG_CLOCK_GATING);
331 WREG32(mmVCE_UENC_REG_CLOCK_GATING, tmp);
333 WREG32(mmVCE_CGTT_CLK_OVERRIDE, 0);
335 tmp = RREG32(mmVCE_CLOCK_GATING_B);
338 WREG32(mmVCE_CLOCK_GATING_B, tmp);
340 tmp = RREG32(mmVCE_UENC_CLOCK_GATING);
343 WREG32(mmVCE_UENC_CLOCK_GATING, tmp);
345 tmp = RREG32(mmVCE_UENC_REG_CLOCK_GATING);
347 WREG32(mmVCE_UENC_REG_CLOCK_GATING, tmp);
351 static void vce_v2_0_set_dyn_cg(struct amdgpu_device *adev, bool gated)
355 /* LMI_MC/LMI_UMC always set in dynamic,
356 * set {CGC_*_GATE_MODE, CGC_*_SW_GATE} = {0, 0}
358 tmp = RREG32(mmVCE_CLOCK_GATING_B);
361 /* Exception for ECPU, IH, SEM, SYS blocks needs to be turned on/off by SW */
364 WREG32(mmVCE_CLOCK_GATING_B, tmp);
368 WREG32(mmVCE_CLOCK_GATING_B, tmp);
371 orig = tmp = RREG32(mmVCE_UENC_CLOCK_GATING);
375 WREG32(mmVCE_UENC_CLOCK_GATING, tmp);
377 orig = tmp = RREG32(mmVCE_UENC_REG_CLOCK_GATING);
380 WREG32(mmVCE_UENC_REG_CLOCK_GATING, tmp);
382 /* set VCE_UENC_REG_CLOCK_GATING always in dynamic mode */
383 WREG32(mmVCE_UENC_REG_CLOCK_GATING, 0x00);
386 WREG32(mmVCE_CGTT_CLK_OVERRIDE, 0);
389 static void vce_v2_0_enable_mgcg(struct amdgpu_device *adev, bool enable,
392 if (enable && (adev->cg_flags & AMD_CG_SUPPORT_VCE_MGCG)) {
394 vce_v2_0_set_sw_cg(adev, true);
396 vce_v2_0_set_dyn_cg(adev, true);
398 vce_v2_0_disable_cg(adev);
401 vce_v2_0_set_sw_cg(adev, false);
403 vce_v2_0_set_dyn_cg(adev, false);
407 static int vce_v2_0_early_init(struct amdgpu_ip_block *ip_block)
409 struct amdgpu_device *adev = ip_block->adev;
411 adev->vce.num_rings = 2;
413 vce_v2_0_set_ring_funcs(adev);
414 vce_v2_0_set_irq_funcs(adev);
419 static int vce_v2_0_sw_init(struct amdgpu_ip_block *ip_block)
421 struct amdgpu_ring *ring;
423 struct amdgpu_device *adev = ip_block->adev;
426 r = amdgpu_irq_add_id(adev, AMDGPU_IRQ_CLIENTID_LEGACY, 167, &adev->vce.irq);
430 r = amdgpu_vce_sw_init(adev, VCE_V2_0_FW_SIZE +
431 VCE_V2_0_STACK_SIZE + VCE_V2_0_DATA_SIZE);
435 r = amdgpu_vce_resume(adev);
439 for (i = 0; i < adev->vce.num_rings; i++) {
440 enum amdgpu_ring_priority_level hw_prio = amdgpu_vce_get_ring_prio(i);
442 ring = &adev->vce.ring[i];
443 sprintf(ring->name, "vce%d", i);
444 r = amdgpu_ring_init(adev, ring, 512, &adev->vce.irq, 0,
453 static int vce_v2_0_sw_fini(struct amdgpu_ip_block *ip_block)
456 struct amdgpu_device *adev = ip_block->adev;
458 r = amdgpu_vce_suspend(adev);
462 return amdgpu_vce_sw_fini(adev);
465 static int vce_v2_0_hw_init(struct amdgpu_ip_block *ip_block)
468 struct amdgpu_device *adev = ip_block->adev;
470 amdgpu_asic_set_vce_clocks(adev, 10000, 10000);
471 vce_v2_0_enable_mgcg(adev, true, false);
473 for (i = 0; i < adev->vce.num_rings; i++) {
474 r = amdgpu_ring_test_helper(&adev->vce.ring[i]);
479 DRM_INFO("VCE initialized successfully.\n");
484 static int vce_v2_0_hw_fini(struct amdgpu_ip_block *ip_block)
486 cancel_delayed_work_sync(&ip_block->adev->vce.idle_work);
491 static int vce_v2_0_suspend(struct amdgpu_ip_block *ip_block)
494 struct amdgpu_device *adev = ip_block->adev;
498 * Proper cleanups before halting the HW engine:
499 * - cancel the delayed idle work
500 * - enable powergating
501 * - enable clockgating
504 * TODO: to align with the VCN implementation, move the
505 * jobs for clockgating/powergating/dpm setting to
506 * ->set_powergating_state().
508 cancel_delayed_work_sync(&adev->vce.idle_work);
510 if (adev->pm.dpm_enabled) {
511 amdgpu_dpm_enable_vce(adev, false);
513 amdgpu_asic_set_vce_clocks(adev, 0, 0);
514 amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
516 amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
520 r = vce_v2_0_hw_fini(ip_block);
524 return amdgpu_vce_suspend(adev);
527 static int vce_v2_0_resume(struct amdgpu_ip_block *ip_block)
531 r = amdgpu_vce_resume(ip_block->adev);
535 return vce_v2_0_hw_init(ip_block);
538 static int vce_v2_0_soft_reset(struct amdgpu_ip_block *ip_block)
540 struct amdgpu_device *adev = ip_block->adev;
542 WREG32_FIELD(SRBM_SOFT_RESET, SOFT_RESET_VCE, 1);
545 return vce_v2_0_start(adev);
548 static int vce_v2_0_set_interrupt_state(struct amdgpu_device *adev,
549 struct amdgpu_irq_src *source,
551 enum amdgpu_interrupt_state state)
555 if (state == AMDGPU_IRQ_STATE_ENABLE)
556 val |= VCE_SYS_INT_EN__VCE_SYS_INT_TRAP_INTERRUPT_EN_MASK;
558 WREG32_P(mmVCE_SYS_INT_EN, val, ~VCE_SYS_INT_EN__VCE_SYS_INT_TRAP_INTERRUPT_EN_MASK);
562 static int vce_v2_0_process_interrupt(struct amdgpu_device *adev,
563 struct amdgpu_irq_src *source,
564 struct amdgpu_iv_entry *entry)
566 DRM_DEBUG("IH: VCE\n");
567 switch (entry->src_data[0]) {
570 amdgpu_fence_process(&adev->vce.ring[entry->src_data[0]]);
573 DRM_ERROR("Unhandled interrupt: %d %d\n",
574 entry->src_id, entry->src_data[0]);
581 static int vce_v2_0_set_clockgating_state(struct amdgpu_ip_block *ip_block,
582 enum amd_clockgating_state state)
587 struct amdgpu_device *adev = ip_block->adev;
589 if (state == AMD_CG_STATE_GATE) {
594 vce_v2_0_enable_mgcg(adev, gate, sw_cg);
599 static int vce_v2_0_set_powergating_state(struct amdgpu_ip_block *ip_block,
600 enum amd_powergating_state state)
602 /* This doesn't actually powergate the VCE block.
603 * That's done in the dpm code via the SMC. This
604 * just re-inits the block as necessary. The actual
605 * gating still happens in the dpm code. We should
606 * revisit this when there is a cleaner line between
607 * the smc and the hw blocks
609 struct amdgpu_device *adev = ip_block->adev;
611 if (state == AMD_PG_STATE_GATE)
612 return vce_v2_0_stop(adev);
614 return vce_v2_0_start(adev);
617 static const struct amd_ip_funcs vce_v2_0_ip_funcs = {
619 .early_init = vce_v2_0_early_init,
620 .sw_init = vce_v2_0_sw_init,
621 .sw_fini = vce_v2_0_sw_fini,
622 .hw_init = vce_v2_0_hw_init,
623 .hw_fini = vce_v2_0_hw_fini,
624 .suspend = vce_v2_0_suspend,
625 .resume = vce_v2_0_resume,
626 .is_idle = vce_v2_0_is_idle,
627 .wait_for_idle = vce_v2_0_wait_for_idle,
628 .soft_reset = vce_v2_0_soft_reset,
629 .set_clockgating_state = vce_v2_0_set_clockgating_state,
630 .set_powergating_state = vce_v2_0_set_powergating_state,
633 static const struct amdgpu_ring_funcs vce_v2_0_ring_funcs = {
634 .type = AMDGPU_RING_TYPE_VCE,
636 .nop = VCE_CMD_NO_OP,
637 .support_64bit_ptrs = false,
638 .no_user_fence = true,
639 .get_rptr = vce_v2_0_ring_get_rptr,
640 .get_wptr = vce_v2_0_ring_get_wptr,
641 .set_wptr = vce_v2_0_ring_set_wptr,
642 .parse_cs = amdgpu_vce_ring_parse_cs,
643 .emit_frame_size = 6, /* amdgpu_vce_ring_emit_fence x1 no user fence */
644 .emit_ib_size = 4, /* amdgpu_vce_ring_emit_ib */
645 .emit_ib = amdgpu_vce_ring_emit_ib,
646 .emit_fence = amdgpu_vce_ring_emit_fence,
647 .test_ring = amdgpu_vce_ring_test_ring,
648 .test_ib = amdgpu_vce_ring_test_ib,
649 .insert_nop = amdgpu_ring_insert_nop,
650 .pad_ib = amdgpu_ring_generic_pad_ib,
651 .begin_use = amdgpu_vce_ring_begin_use,
652 .end_use = amdgpu_vce_ring_end_use,
655 static void vce_v2_0_set_ring_funcs(struct amdgpu_device *adev)
659 for (i = 0; i < adev->vce.num_rings; i++) {
660 adev->vce.ring[i].funcs = &vce_v2_0_ring_funcs;
661 adev->vce.ring[i].me = i;
665 static const struct amdgpu_irq_src_funcs vce_v2_0_irq_funcs = {
666 .set = vce_v2_0_set_interrupt_state,
667 .process = vce_v2_0_process_interrupt,
670 static void vce_v2_0_set_irq_funcs(struct amdgpu_device *adev)
672 adev->vce.irq.num_types = 1;
673 adev->vce.irq.funcs = &vce_v2_0_irq_funcs;
676 const struct amdgpu_ip_block_version vce_v2_0_ip_block =
678 .type = AMD_IP_BLOCK_TYPE_VCE,
682 .funcs = &vce_v2_0_ip_funcs,