Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright 2014 Advanced Micro Devices, Inc.
0003  * All Rights Reserved.
0004  *
0005  * Permission is hereby granted, free of charge, to any person obtaining a
0006  * copy of this software and associated documentation files (the
0007  * "Software"), to deal in the Software without restriction, including
0008  * without limitation the rights to use, copy, modify, merge, publish,
0009  * distribute, sub license, and/or sell copies of the Software, and to
0010  * permit persons to whom the Software is furnished to do so, subject to
0011  * the following conditions:
0012  *
0013  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0014  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0015  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
0016  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
0017  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
0018  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
0019  * USE OR OTHER DEALINGS IN THE SOFTWARE.
0020  *
0021  * The above copyright notice and this permission notice (including the
0022  * next paragraph) shall be included in all copies or substantial portions
0023  * of the Software.
0024  *
0025  * Authors: Christian König <christian.koenig@amd.com>
0026  */
0027 
0028 #include <linux/firmware.h>
0029 
0030 #include "amdgpu.h"
0031 #include "amdgpu_vce.h"
0032 #include "vid.h"
0033 #include "vce/vce_3_0_d.h"
0034 #include "vce/vce_3_0_sh_mask.h"
0035 #include "oss/oss_3_0_d.h"
0036 #include "oss/oss_3_0_sh_mask.h"
0037 #include "gca/gfx_8_0_d.h"
0038 #include "smu/smu_7_1_2_d.h"
0039 #include "smu/smu_7_1_2_sh_mask.h"
0040 #include "gca/gfx_8_0_sh_mask.h"
0041 #include "ivsrcid/ivsrcid_vislands30.h"
0042 
0043 
0044 #define GRBM_GFX_INDEX__VCE_INSTANCE__SHIFT 0x04
0045 #define GRBM_GFX_INDEX__VCE_INSTANCE_MASK   0x10
0046 #define GRBM_GFX_INDEX__VCE_ALL_PIPE        0x07
0047 
0048 #define mmVCE_LMI_VCPU_CACHE_40BIT_BAR0 0x8616
0049 #define mmVCE_LMI_VCPU_CACHE_40BIT_BAR1 0x8617
0050 #define mmVCE_LMI_VCPU_CACHE_40BIT_BAR2 0x8618
0051 #define mmGRBM_GFX_INDEX_DEFAULT 0xE0000000
0052 
0053 #define VCE_STATUS_VCPU_REPORT_FW_LOADED_MASK   0x02
0054 
0055 #define VCE_V3_0_FW_SIZE    (384 * 1024)
0056 #define VCE_V3_0_STACK_SIZE (64 * 1024)
0057 #define VCE_V3_0_DATA_SIZE  ((16 * 1024 * AMDGPU_MAX_VCE_HANDLES) + (52 * 1024))
0058 
0059 #define FW_52_8_3   ((52 << 24) | (8 << 16) | (3 << 8))
0060 
0061 #define GET_VCE_INSTANCE(i)  ((i) << GRBM_GFX_INDEX__VCE_INSTANCE__SHIFT \
0062                     | GRBM_GFX_INDEX__VCE_ALL_PIPE)
0063 
0064 static void vce_v3_0_mc_resume(struct amdgpu_device *adev, int idx);
0065 static void vce_v3_0_set_ring_funcs(struct amdgpu_device *adev);
0066 static void vce_v3_0_set_irq_funcs(struct amdgpu_device *adev);
0067 static int vce_v3_0_wait_for_idle(void *handle);
0068 static int vce_v3_0_set_clockgating_state(void *handle,
0069                       enum amd_clockgating_state state);
0070 /**
0071  * vce_v3_0_ring_get_rptr - get read pointer
0072  *
0073  * @ring: amdgpu_ring pointer
0074  *
0075  * Returns the current hardware read pointer
0076  */
0077 static uint64_t vce_v3_0_ring_get_rptr(struct amdgpu_ring *ring)
0078 {
0079     struct amdgpu_device *adev = ring->adev;
0080     u32 v;
0081 
0082     mutex_lock(&adev->grbm_idx_mutex);
0083     if (adev->vce.harvest_config == 0 ||
0084         adev->vce.harvest_config == AMDGPU_VCE_HARVEST_VCE1)
0085         WREG32(mmGRBM_GFX_INDEX, GET_VCE_INSTANCE(0));
0086     else if (adev->vce.harvest_config == AMDGPU_VCE_HARVEST_VCE0)
0087         WREG32(mmGRBM_GFX_INDEX, GET_VCE_INSTANCE(1));
0088 
0089     if (ring->me == 0)
0090         v = RREG32(mmVCE_RB_RPTR);
0091     else if (ring->me == 1)
0092         v = RREG32(mmVCE_RB_RPTR2);
0093     else
0094         v = RREG32(mmVCE_RB_RPTR3);
0095 
0096     WREG32(mmGRBM_GFX_INDEX, mmGRBM_GFX_INDEX_DEFAULT);
0097     mutex_unlock(&adev->grbm_idx_mutex);
0098 
0099     return v;
0100 }
0101 
0102 /**
0103  * vce_v3_0_ring_get_wptr - get write pointer
0104  *
0105  * @ring: amdgpu_ring pointer
0106  *
0107  * Returns the current hardware write pointer
0108  */
0109 static uint64_t vce_v3_0_ring_get_wptr(struct amdgpu_ring *ring)
0110 {
0111     struct amdgpu_device *adev = ring->adev;
0112     u32 v;
0113 
0114     mutex_lock(&adev->grbm_idx_mutex);
0115     if (adev->vce.harvest_config == 0 ||
0116         adev->vce.harvest_config == AMDGPU_VCE_HARVEST_VCE1)
0117         WREG32(mmGRBM_GFX_INDEX, GET_VCE_INSTANCE(0));
0118     else if (adev->vce.harvest_config == AMDGPU_VCE_HARVEST_VCE0)
0119         WREG32(mmGRBM_GFX_INDEX, GET_VCE_INSTANCE(1));
0120 
0121     if (ring->me == 0)
0122         v = RREG32(mmVCE_RB_WPTR);
0123     else if (ring->me == 1)
0124         v = RREG32(mmVCE_RB_WPTR2);
0125     else
0126         v = RREG32(mmVCE_RB_WPTR3);
0127 
0128     WREG32(mmGRBM_GFX_INDEX, mmGRBM_GFX_INDEX_DEFAULT);
0129     mutex_unlock(&adev->grbm_idx_mutex);
0130 
0131     return v;
0132 }
0133 
0134 /**
0135  * vce_v3_0_ring_set_wptr - set write pointer
0136  *
0137  * @ring: amdgpu_ring pointer
0138  *
0139  * Commits the write pointer to the hardware
0140  */
0141 static void vce_v3_0_ring_set_wptr(struct amdgpu_ring *ring)
0142 {
0143     struct amdgpu_device *adev = ring->adev;
0144 
0145     mutex_lock(&adev->grbm_idx_mutex);
0146     if (adev->vce.harvest_config == 0 ||
0147         adev->vce.harvest_config == AMDGPU_VCE_HARVEST_VCE1)
0148         WREG32(mmGRBM_GFX_INDEX, GET_VCE_INSTANCE(0));
0149     else if (adev->vce.harvest_config == AMDGPU_VCE_HARVEST_VCE0)
0150         WREG32(mmGRBM_GFX_INDEX, GET_VCE_INSTANCE(1));
0151 
0152     if (ring->me == 0)
0153         WREG32(mmVCE_RB_WPTR, lower_32_bits(ring->wptr));
0154     else if (ring->me == 1)
0155         WREG32(mmVCE_RB_WPTR2, lower_32_bits(ring->wptr));
0156     else
0157         WREG32(mmVCE_RB_WPTR3, lower_32_bits(ring->wptr));
0158 
0159     WREG32(mmGRBM_GFX_INDEX, mmGRBM_GFX_INDEX_DEFAULT);
0160     mutex_unlock(&adev->grbm_idx_mutex);
0161 }
0162 
0163 static void vce_v3_0_override_vce_clock_gating(struct amdgpu_device *adev, bool override)
0164 {
0165     WREG32_FIELD(VCE_RB_ARB_CTRL, VCE_CGTT_OVERRIDE, override ? 1 : 0);
0166 }
0167 
0168 static void vce_v3_0_set_vce_sw_clock_gating(struct amdgpu_device *adev,
0169                          bool gated)
0170 {
0171     u32 data;
0172 
0173     /* Set Override to disable Clock Gating */
0174     vce_v3_0_override_vce_clock_gating(adev, true);
0175 
0176     /* This function enables MGCG which is controlled by firmware.
0177        With the clocks in the gated state the core is still
0178        accessible but the firmware will throttle the clocks on the
0179        fly as necessary.
0180     */
0181     if (!gated) {
0182         data = RREG32(mmVCE_CLOCK_GATING_B);
0183         data |= 0x1ff;
0184         data &= ~0xef0000;
0185         WREG32(mmVCE_CLOCK_GATING_B, data);
0186 
0187         data = RREG32(mmVCE_UENC_CLOCK_GATING);
0188         data |= 0x3ff000;
0189         data &= ~0xffc00000;
0190         WREG32(mmVCE_UENC_CLOCK_GATING, data);
0191 
0192         data = RREG32(mmVCE_UENC_CLOCK_GATING_2);
0193         data |= 0x2;
0194         data &= ~0x00010000;
0195         WREG32(mmVCE_UENC_CLOCK_GATING_2, data);
0196 
0197         data = RREG32(mmVCE_UENC_REG_CLOCK_GATING);
0198         data |= 0x37f;
0199         WREG32(mmVCE_UENC_REG_CLOCK_GATING, data);
0200 
0201         data = RREG32(mmVCE_UENC_DMA_DCLK_CTRL);
0202         data |= VCE_UENC_DMA_DCLK_CTRL__WRDMCLK_FORCEON_MASK |
0203             VCE_UENC_DMA_DCLK_CTRL__RDDMCLK_FORCEON_MASK |
0204             VCE_UENC_DMA_DCLK_CTRL__REGCLK_FORCEON_MASK  |
0205             0x8;
0206         WREG32(mmVCE_UENC_DMA_DCLK_CTRL, data);
0207     } else {
0208         data = RREG32(mmVCE_CLOCK_GATING_B);
0209         data &= ~0x80010;
0210         data |= 0xe70008;
0211         WREG32(mmVCE_CLOCK_GATING_B, data);
0212 
0213         data = RREG32(mmVCE_UENC_CLOCK_GATING);
0214         data |= 0xffc00000;
0215         WREG32(mmVCE_UENC_CLOCK_GATING, data);
0216 
0217         data = RREG32(mmVCE_UENC_CLOCK_GATING_2);
0218         data |= 0x10000;
0219         WREG32(mmVCE_UENC_CLOCK_GATING_2, data);
0220 
0221         data = RREG32(mmVCE_UENC_REG_CLOCK_GATING);
0222         data &= ~0x3ff;
0223         WREG32(mmVCE_UENC_REG_CLOCK_GATING, data);
0224 
0225         data = RREG32(mmVCE_UENC_DMA_DCLK_CTRL);
0226         data &= ~(VCE_UENC_DMA_DCLK_CTRL__WRDMCLK_FORCEON_MASK |
0227               VCE_UENC_DMA_DCLK_CTRL__RDDMCLK_FORCEON_MASK |
0228               VCE_UENC_DMA_DCLK_CTRL__REGCLK_FORCEON_MASK  |
0229               0x8);
0230         WREG32(mmVCE_UENC_DMA_DCLK_CTRL, data);
0231     }
0232     vce_v3_0_override_vce_clock_gating(adev, false);
0233 }
0234 
0235 static int vce_v3_0_firmware_loaded(struct amdgpu_device *adev)
0236 {
0237     int i, j;
0238 
0239     for (i = 0; i < 10; ++i) {
0240         for (j = 0; j < 100; ++j) {
0241             uint32_t status = RREG32(mmVCE_STATUS);
0242 
0243             if (status & VCE_STATUS_VCPU_REPORT_FW_LOADED_MASK)
0244                 return 0;
0245             mdelay(10);
0246         }
0247 
0248         DRM_ERROR("VCE not responding, trying to reset the ECPU!!!\n");
0249         WREG32_FIELD(VCE_SOFT_RESET, ECPU_SOFT_RESET, 1);
0250         mdelay(10);
0251         WREG32_FIELD(VCE_SOFT_RESET, ECPU_SOFT_RESET, 0);
0252         mdelay(10);
0253     }
0254 
0255     return -ETIMEDOUT;
0256 }
0257 
0258 /**
0259  * vce_v3_0_start - start VCE block
0260  *
0261  * @adev: amdgpu_device pointer
0262  *
0263  * Setup and start the VCE block
0264  */
0265 static int vce_v3_0_start(struct amdgpu_device *adev)
0266 {
0267     struct amdgpu_ring *ring;
0268     int idx, r;
0269 
0270     mutex_lock(&adev->grbm_idx_mutex);
0271     for (idx = 0; idx < 2; ++idx) {
0272         if (adev->vce.harvest_config & (1 << idx))
0273             continue;
0274 
0275         WREG32(mmGRBM_GFX_INDEX, GET_VCE_INSTANCE(idx));
0276 
0277         /* Program instance 0 reg space for two instances or instance 0 case
0278         program instance 1 reg space for only instance 1 available case */
0279         if (idx != 1 || adev->vce.harvest_config == AMDGPU_VCE_HARVEST_VCE0) {
0280             ring = &adev->vce.ring[0];
0281             WREG32(mmVCE_RB_RPTR, lower_32_bits(ring->wptr));
0282             WREG32(mmVCE_RB_WPTR, lower_32_bits(ring->wptr));
0283             WREG32(mmVCE_RB_BASE_LO, ring->gpu_addr);
0284             WREG32(mmVCE_RB_BASE_HI, upper_32_bits(ring->gpu_addr));
0285             WREG32(mmVCE_RB_SIZE, ring->ring_size / 4);
0286 
0287             ring = &adev->vce.ring[1];
0288             WREG32(mmVCE_RB_RPTR2, lower_32_bits(ring->wptr));
0289             WREG32(mmVCE_RB_WPTR2, lower_32_bits(ring->wptr));
0290             WREG32(mmVCE_RB_BASE_LO2, ring->gpu_addr);
0291             WREG32(mmVCE_RB_BASE_HI2, upper_32_bits(ring->gpu_addr));
0292             WREG32(mmVCE_RB_SIZE2, ring->ring_size / 4);
0293 
0294             ring = &adev->vce.ring[2];
0295             WREG32(mmVCE_RB_RPTR3, lower_32_bits(ring->wptr));
0296             WREG32(mmVCE_RB_WPTR3, lower_32_bits(ring->wptr));
0297             WREG32(mmVCE_RB_BASE_LO3, ring->gpu_addr);
0298             WREG32(mmVCE_RB_BASE_HI3, upper_32_bits(ring->gpu_addr));
0299             WREG32(mmVCE_RB_SIZE3, ring->ring_size / 4);
0300         }
0301 
0302         vce_v3_0_mc_resume(adev, idx);
0303         WREG32_FIELD(VCE_STATUS, JOB_BUSY, 1);
0304 
0305         if (adev->asic_type >= CHIP_STONEY)
0306             WREG32_P(mmVCE_VCPU_CNTL, 1, ~0x200001);
0307         else
0308             WREG32_FIELD(VCE_VCPU_CNTL, CLK_EN, 1);
0309 
0310         WREG32_FIELD(VCE_SOFT_RESET, ECPU_SOFT_RESET, 0);
0311         mdelay(100);
0312 
0313         r = vce_v3_0_firmware_loaded(adev);
0314 
0315         /* clear BUSY flag */
0316         WREG32_FIELD(VCE_STATUS, JOB_BUSY, 0);
0317 
0318         if (r) {
0319             DRM_ERROR("VCE not responding, giving up!!!\n");
0320             mutex_unlock(&adev->grbm_idx_mutex);
0321             return r;
0322         }
0323     }
0324 
0325     WREG32(mmGRBM_GFX_INDEX, mmGRBM_GFX_INDEX_DEFAULT);
0326     mutex_unlock(&adev->grbm_idx_mutex);
0327 
0328     return 0;
0329 }
0330 
0331 static int vce_v3_0_stop(struct amdgpu_device *adev)
0332 {
0333     int idx;
0334 
0335     mutex_lock(&adev->grbm_idx_mutex);
0336     for (idx = 0; idx < 2; ++idx) {
0337         if (adev->vce.harvest_config & (1 << idx))
0338             continue;
0339 
0340         WREG32(mmGRBM_GFX_INDEX, GET_VCE_INSTANCE(idx));
0341 
0342         if (adev->asic_type >= CHIP_STONEY)
0343             WREG32_P(mmVCE_VCPU_CNTL, 0, ~0x200001);
0344         else
0345             WREG32_FIELD(VCE_VCPU_CNTL, CLK_EN, 0);
0346 
0347         /* hold on ECPU */
0348         WREG32_FIELD(VCE_SOFT_RESET, ECPU_SOFT_RESET, 1);
0349 
0350         /* clear VCE STATUS */
0351         WREG32(mmVCE_STATUS, 0);
0352     }
0353 
0354     WREG32(mmGRBM_GFX_INDEX, mmGRBM_GFX_INDEX_DEFAULT);
0355     mutex_unlock(&adev->grbm_idx_mutex);
0356 
0357     return 0;
0358 }
0359 
0360 #define ixVCE_HARVEST_FUSE_MACRO__ADDRESS     0xC0014074
0361 #define VCE_HARVEST_FUSE_MACRO__SHIFT       27
0362 #define VCE_HARVEST_FUSE_MACRO__MASK        0x18000000
0363 
0364 static unsigned vce_v3_0_get_harvest_config(struct amdgpu_device *adev)
0365 {
0366     u32 tmp;
0367 
0368     if ((adev->asic_type == CHIP_FIJI) ||
0369         (adev->asic_type == CHIP_STONEY))
0370         return AMDGPU_VCE_HARVEST_VCE1;
0371 
0372     if (adev->flags & AMD_IS_APU)
0373         tmp = (RREG32_SMC(ixVCE_HARVEST_FUSE_MACRO__ADDRESS) &
0374                VCE_HARVEST_FUSE_MACRO__MASK) >>
0375             VCE_HARVEST_FUSE_MACRO__SHIFT;
0376     else
0377         tmp = (RREG32_SMC(ixCC_HARVEST_FUSES) &
0378                CC_HARVEST_FUSES__VCE_DISABLE_MASK) >>
0379             CC_HARVEST_FUSES__VCE_DISABLE__SHIFT;
0380 
0381     switch (tmp) {
0382     case 1:
0383         return AMDGPU_VCE_HARVEST_VCE0;
0384     case 2:
0385         return AMDGPU_VCE_HARVEST_VCE1;
0386     case 3:
0387         return AMDGPU_VCE_HARVEST_VCE0 | AMDGPU_VCE_HARVEST_VCE1;
0388     default:
0389         if ((adev->asic_type == CHIP_POLARIS10) ||
0390             (adev->asic_type == CHIP_POLARIS11) ||
0391             (adev->asic_type == CHIP_POLARIS12) ||
0392             (adev->asic_type == CHIP_VEGAM))
0393             return AMDGPU_VCE_HARVEST_VCE1;
0394 
0395         return 0;
0396     }
0397 }
0398 
0399 static int vce_v3_0_early_init(void *handle)
0400 {
0401     struct amdgpu_device *adev = (struct amdgpu_device *)handle;
0402 
0403     adev->vce.harvest_config = vce_v3_0_get_harvest_config(adev);
0404 
0405     if ((adev->vce.harvest_config &
0406          (AMDGPU_VCE_HARVEST_VCE0 | AMDGPU_VCE_HARVEST_VCE1)) ==
0407         (AMDGPU_VCE_HARVEST_VCE0 | AMDGPU_VCE_HARVEST_VCE1))
0408         return -ENOENT;
0409 
0410     adev->vce.num_rings = 3;
0411 
0412     vce_v3_0_set_ring_funcs(adev);
0413     vce_v3_0_set_irq_funcs(adev);
0414 
0415     return 0;
0416 }
0417 
0418 static int vce_v3_0_sw_init(void *handle)
0419 {
0420     struct amdgpu_device *adev = (struct amdgpu_device *)handle;
0421     struct amdgpu_ring *ring;
0422     int r, i;
0423 
0424     /* VCE */
0425     r = amdgpu_irq_add_id(adev, AMDGPU_IRQ_CLIENTID_LEGACY, VISLANDS30_IV_SRCID_VCE_TRAP, &adev->vce.irq);
0426     if (r)
0427         return r;
0428 
0429     r = amdgpu_vce_sw_init(adev, VCE_V3_0_FW_SIZE +
0430         (VCE_V3_0_STACK_SIZE + VCE_V3_0_DATA_SIZE) * 2);
0431     if (r)
0432         return r;
0433 
0434     /* 52.8.3 required for 3 ring support */
0435     if (adev->vce.fw_version < FW_52_8_3)
0436         adev->vce.num_rings = 2;
0437 
0438     r = amdgpu_vce_resume(adev);
0439     if (r)
0440         return r;
0441 
0442     for (i = 0; i < adev->vce.num_rings; i++) {
0443         enum amdgpu_ring_priority_level hw_prio = amdgpu_vce_get_ring_prio(i);
0444 
0445         ring = &adev->vce.ring[i];
0446         sprintf(ring->name, "vce%d", i);
0447         r = amdgpu_ring_init(adev, ring, 512, &adev->vce.irq, 0,
0448                      hw_prio, NULL);
0449         if (r)
0450             return r;
0451     }
0452 
0453     r = amdgpu_vce_entity_init(adev);
0454 
0455     return r;
0456 }
0457 
0458 static int vce_v3_0_sw_fini(void *handle)
0459 {
0460     int r;
0461     struct amdgpu_device *adev = (struct amdgpu_device *)handle;
0462 
0463     r = amdgpu_vce_suspend(adev);
0464     if (r)
0465         return r;
0466 
0467     return amdgpu_vce_sw_fini(adev);
0468 }
0469 
0470 static int vce_v3_0_hw_init(void *handle)
0471 {
0472     int r, i;
0473     struct amdgpu_device *adev = (struct amdgpu_device *)handle;
0474 
0475     vce_v3_0_override_vce_clock_gating(adev, true);
0476 
0477     amdgpu_asic_set_vce_clocks(adev, 10000, 10000);
0478 
0479     for (i = 0; i < adev->vce.num_rings; i++) {
0480         r = amdgpu_ring_test_helper(&adev->vce.ring[i]);
0481         if (r)
0482             return r;
0483     }
0484 
0485     DRM_INFO("VCE initialized successfully.\n");
0486 
0487     return 0;
0488 }
0489 
0490 static int vce_v3_0_hw_fini(void *handle)
0491 {
0492     int r;
0493     struct amdgpu_device *adev = (struct amdgpu_device *)handle;
0494 
0495     cancel_delayed_work_sync(&adev->vce.idle_work);
0496 
0497     r = vce_v3_0_wait_for_idle(handle);
0498     if (r)
0499         return r;
0500 
0501     vce_v3_0_stop(adev);
0502     return vce_v3_0_set_clockgating_state(adev, AMD_CG_STATE_GATE);
0503 }
0504 
0505 static int vce_v3_0_suspend(void *handle)
0506 {
0507     int r;
0508     struct amdgpu_device *adev = (struct amdgpu_device *)handle;
0509 
0510     /*
0511      * Proper cleanups before halting the HW engine:
0512      *   - cancel the delayed idle work
0513      *   - enable powergating
0514      *   - enable clockgating
0515      *   - disable dpm
0516      *
0517      * TODO: to align with the VCN implementation, move the
0518      * jobs for clockgating/powergating/dpm setting to
0519      * ->set_powergating_state().
0520      */
0521     cancel_delayed_work_sync(&adev->vce.idle_work);
0522 
0523     if (adev->pm.dpm_enabled) {
0524         amdgpu_dpm_enable_vce(adev, false);
0525     } else {
0526         amdgpu_asic_set_vce_clocks(adev, 0, 0);
0527         amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
0528                                AMD_PG_STATE_GATE);
0529         amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
0530                                AMD_CG_STATE_GATE);
0531     }
0532 
0533     r = vce_v3_0_hw_fini(adev);
0534     if (r)
0535         return r;
0536 
0537     return amdgpu_vce_suspend(adev);
0538 }
0539 
0540 static int vce_v3_0_resume(void *handle)
0541 {
0542     int r;
0543     struct amdgpu_device *adev = (struct amdgpu_device *)handle;
0544 
0545     r = amdgpu_vce_resume(adev);
0546     if (r)
0547         return r;
0548 
0549     return vce_v3_0_hw_init(adev);
0550 }
0551 
0552 static void vce_v3_0_mc_resume(struct amdgpu_device *adev, int idx)
0553 {
0554     uint32_t offset, size;
0555 
0556     WREG32_P(mmVCE_CLOCK_GATING_A, 0, ~(1 << 16));
0557     WREG32_P(mmVCE_UENC_CLOCK_GATING, 0x1FF000, ~0xFF9FF000);
0558     WREG32_P(mmVCE_UENC_REG_CLOCK_GATING, 0x3F, ~0x3F);
0559     WREG32(mmVCE_CLOCK_GATING_B, 0x1FF);
0560 
0561     WREG32(mmVCE_LMI_CTRL, 0x00398000);
0562     WREG32_P(mmVCE_LMI_CACHE_CTRL, 0x0, ~0x1);
0563     WREG32(mmVCE_LMI_SWAP_CNTL, 0);
0564     WREG32(mmVCE_LMI_SWAP_CNTL1, 0);
0565     WREG32(mmVCE_LMI_VM_CTRL, 0);
0566     WREG32_OR(mmVCE_VCPU_CNTL, 0x00100000);
0567 
0568     if (adev->asic_type >= CHIP_STONEY) {
0569         WREG32(mmVCE_LMI_VCPU_CACHE_40BIT_BAR0, (adev->vce.gpu_addr >> 8));
0570         WREG32(mmVCE_LMI_VCPU_CACHE_40BIT_BAR1, (adev->vce.gpu_addr >> 8));
0571         WREG32(mmVCE_LMI_VCPU_CACHE_40BIT_BAR2, (adev->vce.gpu_addr >> 8));
0572     } else
0573         WREG32(mmVCE_LMI_VCPU_CACHE_40BIT_BAR, (adev->vce.gpu_addr >> 8));
0574     offset = AMDGPU_VCE_FIRMWARE_OFFSET;
0575     size = VCE_V3_0_FW_SIZE;
0576     WREG32(mmVCE_VCPU_CACHE_OFFSET0, offset & 0x7fffffff);
0577     WREG32(mmVCE_VCPU_CACHE_SIZE0, size);
0578 
0579     if (idx == 0) {
0580         offset += size;
0581         size = VCE_V3_0_STACK_SIZE;
0582         WREG32(mmVCE_VCPU_CACHE_OFFSET1, offset & 0x7fffffff);
0583         WREG32(mmVCE_VCPU_CACHE_SIZE1, size);
0584         offset += size;
0585         size = VCE_V3_0_DATA_SIZE;
0586         WREG32(mmVCE_VCPU_CACHE_OFFSET2, offset & 0x7fffffff);
0587         WREG32(mmVCE_VCPU_CACHE_SIZE2, size);
0588     } else {
0589         offset += size + VCE_V3_0_STACK_SIZE + VCE_V3_0_DATA_SIZE;
0590         size = VCE_V3_0_STACK_SIZE;
0591         WREG32(mmVCE_VCPU_CACHE_OFFSET1, offset & 0xfffffff);
0592         WREG32(mmVCE_VCPU_CACHE_SIZE1, size);
0593         offset += size;
0594         size = VCE_V3_0_DATA_SIZE;
0595         WREG32(mmVCE_VCPU_CACHE_OFFSET2, offset & 0xfffffff);
0596         WREG32(mmVCE_VCPU_CACHE_SIZE2, size);
0597     }
0598 
0599     WREG32_P(mmVCE_LMI_CTRL2, 0x0, ~0x100);
0600     WREG32_FIELD(VCE_SYS_INT_EN, VCE_SYS_INT_TRAP_INTERRUPT_EN, 1);
0601 }
0602 
0603 static bool vce_v3_0_is_idle(void *handle)
0604 {
0605     struct amdgpu_device *adev = (struct amdgpu_device *)handle;
0606     u32 mask = 0;
0607 
0608     mask |= (adev->vce.harvest_config & AMDGPU_VCE_HARVEST_VCE0) ? 0 : SRBM_STATUS2__VCE0_BUSY_MASK;
0609     mask |= (adev->vce.harvest_config & AMDGPU_VCE_HARVEST_VCE1) ? 0 : SRBM_STATUS2__VCE1_BUSY_MASK;
0610 
0611     return !(RREG32(mmSRBM_STATUS2) & mask);
0612 }
0613 
0614 static int vce_v3_0_wait_for_idle(void *handle)
0615 {
0616     unsigned i;
0617     struct amdgpu_device *adev = (struct amdgpu_device *)handle;
0618 
0619     for (i = 0; i < adev->usec_timeout; i++)
0620         if (vce_v3_0_is_idle(handle))
0621             return 0;
0622 
0623     return -ETIMEDOUT;
0624 }
0625 
0626 #define  VCE_STATUS_VCPU_REPORT_AUTO_BUSY_MASK  0x00000008L   /* AUTO_BUSY */
0627 #define  VCE_STATUS_VCPU_REPORT_RB0_BUSY_MASK   0x00000010L   /* RB0_BUSY */
0628 #define  VCE_STATUS_VCPU_REPORT_RB1_BUSY_MASK   0x00000020L   /* RB1_BUSY */
0629 #define  AMDGPU_VCE_STATUS_BUSY_MASK (VCE_STATUS_VCPU_REPORT_AUTO_BUSY_MASK | \
0630                       VCE_STATUS_VCPU_REPORT_RB0_BUSY_MASK)
0631 
0632 static bool vce_v3_0_check_soft_reset(void *handle)
0633 {
0634     struct amdgpu_device *adev = (struct amdgpu_device *)handle;
0635     u32 srbm_soft_reset = 0;
0636 
0637     /* According to VCE team , we should use VCE_STATUS instead
0638      * SRBM_STATUS.VCE_BUSY bit for busy status checking.
0639      * GRBM_GFX_INDEX.INSTANCE_INDEX is used to specify which VCE
0640      * instance's registers are accessed
0641      * (0 for 1st instance, 10 for 2nd instance).
0642      *
0643      *VCE_STATUS
0644      *|UENC|ACPI|AUTO ACTIVE|RB1 |RB0 |RB2 |          |FW_LOADED|JOB |
0645      *|----+----+-----------+----+----+----+----------+---------+----|
0646      *|bit8|bit7|    bit6   |bit5|bit4|bit3|   bit2   |  bit1   |bit0|
0647      *
0648      * VCE team suggest use bit 3--bit 6 for busy status check
0649      */
0650     mutex_lock(&adev->grbm_idx_mutex);
0651     WREG32(mmGRBM_GFX_INDEX, GET_VCE_INSTANCE(0));
0652     if (RREG32(mmVCE_STATUS) & AMDGPU_VCE_STATUS_BUSY_MASK) {
0653         srbm_soft_reset = REG_SET_FIELD(srbm_soft_reset, SRBM_SOFT_RESET, SOFT_RESET_VCE0, 1);
0654         srbm_soft_reset = REG_SET_FIELD(srbm_soft_reset, SRBM_SOFT_RESET, SOFT_RESET_VCE1, 1);
0655     }
0656     WREG32(mmGRBM_GFX_INDEX, GET_VCE_INSTANCE(1));
0657     if (RREG32(mmVCE_STATUS) & AMDGPU_VCE_STATUS_BUSY_MASK) {
0658         srbm_soft_reset = REG_SET_FIELD(srbm_soft_reset, SRBM_SOFT_RESET, SOFT_RESET_VCE0, 1);
0659         srbm_soft_reset = REG_SET_FIELD(srbm_soft_reset, SRBM_SOFT_RESET, SOFT_RESET_VCE1, 1);
0660     }
0661     WREG32(mmGRBM_GFX_INDEX, GET_VCE_INSTANCE(0));
0662     mutex_unlock(&adev->grbm_idx_mutex);
0663 
0664     if (srbm_soft_reset) {
0665         adev->vce.srbm_soft_reset = srbm_soft_reset;
0666         return true;
0667     } else {
0668         adev->vce.srbm_soft_reset = 0;
0669         return false;
0670     }
0671 }
0672 
0673 static int vce_v3_0_soft_reset(void *handle)
0674 {
0675     struct amdgpu_device *adev = (struct amdgpu_device *)handle;
0676     u32 srbm_soft_reset;
0677 
0678     if (!adev->vce.srbm_soft_reset)
0679         return 0;
0680     srbm_soft_reset = adev->vce.srbm_soft_reset;
0681 
0682     if (srbm_soft_reset) {
0683         u32 tmp;
0684 
0685         tmp = RREG32(mmSRBM_SOFT_RESET);
0686         tmp |= srbm_soft_reset;
0687         dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
0688         WREG32(mmSRBM_SOFT_RESET, tmp);
0689         tmp = RREG32(mmSRBM_SOFT_RESET);
0690 
0691         udelay(50);
0692 
0693         tmp &= ~srbm_soft_reset;
0694         WREG32(mmSRBM_SOFT_RESET, tmp);
0695         tmp = RREG32(mmSRBM_SOFT_RESET);
0696 
0697         /* Wait a little for things to settle down */
0698         udelay(50);
0699     }
0700 
0701     return 0;
0702 }
0703 
0704 static int vce_v3_0_pre_soft_reset(void *handle)
0705 {
0706     struct amdgpu_device *adev = (struct amdgpu_device *)handle;
0707 
0708     if (!adev->vce.srbm_soft_reset)
0709         return 0;
0710 
0711     mdelay(5);
0712 
0713     return vce_v3_0_suspend(adev);
0714 }
0715 
0716 
0717 static int vce_v3_0_post_soft_reset(void *handle)
0718 {
0719     struct amdgpu_device *adev = (struct amdgpu_device *)handle;
0720 
0721     if (!adev->vce.srbm_soft_reset)
0722         return 0;
0723 
0724     mdelay(5);
0725 
0726     return vce_v3_0_resume(adev);
0727 }
0728 
0729 static int vce_v3_0_set_interrupt_state(struct amdgpu_device *adev,
0730                     struct amdgpu_irq_src *source,
0731                     unsigned type,
0732                     enum amdgpu_interrupt_state state)
0733 {
0734     uint32_t val = 0;
0735 
0736     if (state == AMDGPU_IRQ_STATE_ENABLE)
0737         val |= VCE_SYS_INT_EN__VCE_SYS_INT_TRAP_INTERRUPT_EN_MASK;
0738 
0739     WREG32_P(mmVCE_SYS_INT_EN, val, ~VCE_SYS_INT_EN__VCE_SYS_INT_TRAP_INTERRUPT_EN_MASK);
0740     return 0;
0741 }
0742 
0743 static int vce_v3_0_process_interrupt(struct amdgpu_device *adev,
0744                       struct amdgpu_irq_src *source,
0745                       struct amdgpu_iv_entry *entry)
0746 {
0747     DRM_DEBUG("IH: VCE\n");
0748 
0749     WREG32_FIELD(VCE_SYS_INT_STATUS, VCE_SYS_INT_TRAP_INTERRUPT_INT, 1);
0750 
0751     switch (entry->src_data[0]) {
0752     case 0:
0753     case 1:
0754     case 2:
0755         amdgpu_fence_process(&adev->vce.ring[entry->src_data[0]]);
0756         break;
0757     default:
0758         DRM_ERROR("Unhandled interrupt: %d %d\n",
0759               entry->src_id, entry->src_data[0]);
0760         break;
0761     }
0762 
0763     return 0;
0764 }
0765 
0766 static int vce_v3_0_set_clockgating_state(void *handle,
0767                       enum amd_clockgating_state state)
0768 {
0769     struct amdgpu_device *adev = (struct amdgpu_device *)handle;
0770     bool enable = (state == AMD_CG_STATE_GATE);
0771     int i;
0772 
0773     if (!(adev->cg_flags & AMD_CG_SUPPORT_VCE_MGCG))
0774         return 0;
0775 
0776     mutex_lock(&adev->grbm_idx_mutex);
0777     for (i = 0; i < 2; i++) {
0778         /* Program VCE Instance 0 or 1 if not harvested */
0779         if (adev->vce.harvest_config & (1 << i))
0780             continue;
0781 
0782         WREG32(mmGRBM_GFX_INDEX, GET_VCE_INSTANCE(i));
0783 
0784         if (!enable) {
0785             /* initialize VCE_CLOCK_GATING_A: Clock ON/OFF delay */
0786             uint32_t data = RREG32(mmVCE_CLOCK_GATING_A);
0787             data &= ~(0xf | 0xff0);
0788             data |= ((0x0 << 0) | (0x04 << 4));
0789             WREG32(mmVCE_CLOCK_GATING_A, data);
0790 
0791             /* initialize VCE_UENC_CLOCK_GATING: Clock ON/OFF delay */
0792             data = RREG32(mmVCE_UENC_CLOCK_GATING);
0793             data &= ~(0xf | 0xff0);
0794             data |= ((0x0 << 0) | (0x04 << 4));
0795             WREG32(mmVCE_UENC_CLOCK_GATING, data);
0796         }
0797 
0798         vce_v3_0_set_vce_sw_clock_gating(adev, enable);
0799     }
0800 
0801     WREG32(mmGRBM_GFX_INDEX, mmGRBM_GFX_INDEX_DEFAULT);
0802     mutex_unlock(&adev->grbm_idx_mutex);
0803 
0804     return 0;
0805 }
0806 
0807 static int vce_v3_0_set_powergating_state(void *handle,
0808                       enum amd_powergating_state state)
0809 {
0810     /* This doesn't actually powergate the VCE block.
0811      * That's done in the dpm code via the SMC.  This
0812      * just re-inits the block as necessary.  The actual
0813      * gating still happens in the dpm code.  We should
0814      * revisit this when there is a cleaner line between
0815      * the smc and the hw blocks
0816      */
0817     struct amdgpu_device *adev = (struct amdgpu_device *)handle;
0818     int ret = 0;
0819 
0820     if (state == AMD_PG_STATE_GATE) {
0821         ret = vce_v3_0_stop(adev);
0822         if (ret)
0823             goto out;
0824     } else {
0825         ret = vce_v3_0_start(adev);
0826         if (ret)
0827             goto out;
0828     }
0829 
0830 out:
0831     return ret;
0832 }
0833 
0834 static void vce_v3_0_get_clockgating_state(void *handle, u64 *flags)
0835 {
0836     struct amdgpu_device *adev = (struct amdgpu_device *)handle;
0837     int data;
0838 
0839     mutex_lock(&adev->pm.mutex);
0840 
0841     if (adev->flags & AMD_IS_APU)
0842         data = RREG32_SMC(ixCURRENT_PG_STATUS_APU);
0843     else
0844         data = RREG32_SMC(ixCURRENT_PG_STATUS);
0845 
0846     if (data & CURRENT_PG_STATUS__VCE_PG_STATUS_MASK) {
0847         DRM_INFO("Cannot get clockgating state when VCE is powergated.\n");
0848         goto out;
0849     }
0850 
0851     WREG32_FIELD(GRBM_GFX_INDEX, VCE_INSTANCE, 0);
0852 
0853     /* AMD_CG_SUPPORT_VCE_MGCG */
0854     data = RREG32(mmVCE_CLOCK_GATING_A);
0855     if (data & (0x04 << 4))
0856         *flags |= AMD_CG_SUPPORT_VCE_MGCG;
0857 
0858 out:
0859     mutex_unlock(&adev->pm.mutex);
0860 }
0861 
0862 static void vce_v3_0_ring_emit_ib(struct amdgpu_ring *ring,
0863                   struct amdgpu_job *job,
0864                   struct amdgpu_ib *ib,
0865                   uint32_t flags)
0866 {
0867     unsigned vmid = AMDGPU_JOB_GET_VMID(job);
0868 
0869     amdgpu_ring_write(ring, VCE_CMD_IB_VM);
0870     amdgpu_ring_write(ring, vmid);
0871     amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr));
0872     amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
0873     amdgpu_ring_write(ring, ib->length_dw);
0874 }
0875 
0876 static void vce_v3_0_emit_vm_flush(struct amdgpu_ring *ring,
0877                    unsigned int vmid, uint64_t pd_addr)
0878 {
0879     amdgpu_ring_write(ring, VCE_CMD_UPDATE_PTB);
0880     amdgpu_ring_write(ring, vmid);
0881     amdgpu_ring_write(ring, pd_addr >> 12);
0882 
0883     amdgpu_ring_write(ring, VCE_CMD_FLUSH_TLB);
0884     amdgpu_ring_write(ring, vmid);
0885     amdgpu_ring_write(ring, VCE_CMD_END);
0886 }
0887 
0888 static void vce_v3_0_emit_pipeline_sync(struct amdgpu_ring *ring)
0889 {
0890     uint32_t seq = ring->fence_drv.sync_seq;
0891     uint64_t addr = ring->fence_drv.gpu_addr;
0892 
0893     amdgpu_ring_write(ring, VCE_CMD_WAIT_GE);
0894     amdgpu_ring_write(ring, lower_32_bits(addr));
0895     amdgpu_ring_write(ring, upper_32_bits(addr));
0896     amdgpu_ring_write(ring, seq);
0897 }
0898 
0899 static const struct amd_ip_funcs vce_v3_0_ip_funcs = {
0900     .name = "vce_v3_0",
0901     .early_init = vce_v3_0_early_init,
0902     .late_init = NULL,
0903     .sw_init = vce_v3_0_sw_init,
0904     .sw_fini = vce_v3_0_sw_fini,
0905     .hw_init = vce_v3_0_hw_init,
0906     .hw_fini = vce_v3_0_hw_fini,
0907     .suspend = vce_v3_0_suspend,
0908     .resume = vce_v3_0_resume,
0909     .is_idle = vce_v3_0_is_idle,
0910     .wait_for_idle = vce_v3_0_wait_for_idle,
0911     .check_soft_reset = vce_v3_0_check_soft_reset,
0912     .pre_soft_reset = vce_v3_0_pre_soft_reset,
0913     .soft_reset = vce_v3_0_soft_reset,
0914     .post_soft_reset = vce_v3_0_post_soft_reset,
0915     .set_clockgating_state = vce_v3_0_set_clockgating_state,
0916     .set_powergating_state = vce_v3_0_set_powergating_state,
0917     .get_clockgating_state = vce_v3_0_get_clockgating_state,
0918 };
0919 
0920 static const struct amdgpu_ring_funcs vce_v3_0_ring_phys_funcs = {
0921     .type = AMDGPU_RING_TYPE_VCE,
0922     .align_mask = 0xf,
0923     .nop = VCE_CMD_NO_OP,
0924     .support_64bit_ptrs = false,
0925     .no_user_fence = true,
0926     .get_rptr = vce_v3_0_ring_get_rptr,
0927     .get_wptr = vce_v3_0_ring_get_wptr,
0928     .set_wptr = vce_v3_0_ring_set_wptr,
0929     .parse_cs = amdgpu_vce_ring_parse_cs,
0930     .emit_frame_size =
0931         4 + /* vce_v3_0_emit_pipeline_sync */
0932         6, /* amdgpu_vce_ring_emit_fence x1 no user fence */
0933     .emit_ib_size = 4, /* amdgpu_vce_ring_emit_ib */
0934     .emit_ib = amdgpu_vce_ring_emit_ib,
0935     .emit_fence = amdgpu_vce_ring_emit_fence,
0936     .test_ring = amdgpu_vce_ring_test_ring,
0937     .test_ib = amdgpu_vce_ring_test_ib,
0938     .insert_nop = amdgpu_ring_insert_nop,
0939     .pad_ib = amdgpu_ring_generic_pad_ib,
0940     .begin_use = amdgpu_vce_ring_begin_use,
0941     .end_use = amdgpu_vce_ring_end_use,
0942 };
0943 
0944 static const struct amdgpu_ring_funcs vce_v3_0_ring_vm_funcs = {
0945     .type = AMDGPU_RING_TYPE_VCE,
0946     .align_mask = 0xf,
0947     .nop = VCE_CMD_NO_OP,
0948     .support_64bit_ptrs = false,
0949     .no_user_fence = true,
0950     .get_rptr = vce_v3_0_ring_get_rptr,
0951     .get_wptr = vce_v3_0_ring_get_wptr,
0952     .set_wptr = vce_v3_0_ring_set_wptr,
0953     .parse_cs = amdgpu_vce_ring_parse_cs_vm,
0954     .emit_frame_size =
0955         6 + /* vce_v3_0_emit_vm_flush */
0956         4 + /* vce_v3_0_emit_pipeline_sync */
0957         6 + 6, /* amdgpu_vce_ring_emit_fence x2 vm fence */
0958     .emit_ib_size = 5, /* vce_v3_0_ring_emit_ib */
0959     .emit_ib = vce_v3_0_ring_emit_ib,
0960     .emit_vm_flush = vce_v3_0_emit_vm_flush,
0961     .emit_pipeline_sync = vce_v3_0_emit_pipeline_sync,
0962     .emit_fence = amdgpu_vce_ring_emit_fence,
0963     .test_ring = amdgpu_vce_ring_test_ring,
0964     .test_ib = amdgpu_vce_ring_test_ib,
0965     .insert_nop = amdgpu_ring_insert_nop,
0966     .pad_ib = amdgpu_ring_generic_pad_ib,
0967     .begin_use = amdgpu_vce_ring_begin_use,
0968     .end_use = amdgpu_vce_ring_end_use,
0969 };
0970 
0971 static void vce_v3_0_set_ring_funcs(struct amdgpu_device *adev)
0972 {
0973     int i;
0974 
0975     if (adev->asic_type >= CHIP_STONEY) {
0976         for (i = 0; i < adev->vce.num_rings; i++) {
0977             adev->vce.ring[i].funcs = &vce_v3_0_ring_vm_funcs;
0978             adev->vce.ring[i].me = i;
0979         }
0980         DRM_INFO("VCE enabled in VM mode\n");
0981     } else {
0982         for (i = 0; i < adev->vce.num_rings; i++) {
0983             adev->vce.ring[i].funcs = &vce_v3_0_ring_phys_funcs;
0984             adev->vce.ring[i].me = i;
0985         }
0986         DRM_INFO("VCE enabled in physical mode\n");
0987     }
0988 }
0989 
0990 static const struct amdgpu_irq_src_funcs vce_v3_0_irq_funcs = {
0991     .set = vce_v3_0_set_interrupt_state,
0992     .process = vce_v3_0_process_interrupt,
0993 };
0994 
0995 static void vce_v3_0_set_irq_funcs(struct amdgpu_device *adev)
0996 {
0997     adev->vce.irq.num_types = 1;
0998     adev->vce.irq.funcs = &vce_v3_0_irq_funcs;
0999 };
1000 
1001 const struct amdgpu_ip_block_version vce_v3_0_ip_block =
1002 {
1003     .type = AMD_IP_BLOCK_TYPE_VCE,
1004     .major = 3,
1005     .minor = 0,
1006     .rev = 0,
1007     .funcs = &vce_v3_0_ip_funcs,
1008 };
1009 
1010 const struct amdgpu_ip_block_version vce_v3_1_ip_block =
1011 {
1012     .type = AMD_IP_BLOCK_TYPE_VCE,
1013     .major = 3,
1014     .minor = 1,
1015     .rev = 0,
1016     .funcs = &vce_v3_0_ip_funcs,
1017 };
1018 
1019 const struct amdgpu_ip_block_version vce_v3_4_ip_block =
1020 {
1021     .type = AMD_IP_BLOCK_TYPE_VCE,
1022     .major = 3,
1023     .minor = 4,
1024     .rev = 0,
1025     .funcs = &vce_v3_0_ip_funcs,
1026 };