162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright 2017 Advanced Micro Devices, Inc.
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
562306a36Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
662306a36Sopenharmony_ci * to deal in the Software without restriction, including without limitation
762306a36Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
862306a36Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
962306a36Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
1262306a36Sopenharmony_ci * all copies or substantial portions of the Software.
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1562306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1662306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1762306a36Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
1862306a36Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1962306a36Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2062306a36Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
2162306a36Sopenharmony_ci *
2262306a36Sopenharmony_ci * Authors: Xiangliang.Yu@amd.com
2362306a36Sopenharmony_ci */
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci#include "amdgpu.h"
2662306a36Sopenharmony_ci#include "vi.h"
2762306a36Sopenharmony_ci#include "bif/bif_5_0_d.h"
2862306a36Sopenharmony_ci#include "bif/bif_5_0_sh_mask.h"
2962306a36Sopenharmony_ci#include "vid.h"
3062306a36Sopenharmony_ci#include "gca/gfx_8_0_d.h"
3162306a36Sopenharmony_ci#include "gca/gfx_8_0_sh_mask.h"
3262306a36Sopenharmony_ci#include "gmc_v8_0.h"
3362306a36Sopenharmony_ci#include "gfx_v8_0.h"
3462306a36Sopenharmony_ci#include "sdma_v3_0.h"
3562306a36Sopenharmony_ci#include "tonga_ih.h"
3662306a36Sopenharmony_ci#include "gmc/gmc_8_2_d.h"
3762306a36Sopenharmony_ci#include "gmc/gmc_8_2_sh_mask.h"
3862306a36Sopenharmony_ci#include "oss/oss_3_0_d.h"
3962306a36Sopenharmony_ci#include "oss/oss_3_0_sh_mask.h"
4062306a36Sopenharmony_ci#include "dce/dce_10_0_d.h"
4162306a36Sopenharmony_ci#include "dce/dce_10_0_sh_mask.h"
4262306a36Sopenharmony_ci#include "smu/smu_7_1_3_d.h"
4362306a36Sopenharmony_ci#include "mxgpu_vi.h"
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci#include "amdgpu_reset.h"
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci/* VI golden setting */
4862306a36Sopenharmony_cistatic const u32 xgpu_fiji_mgcg_cgcg_init[] = {
4962306a36Sopenharmony_ci	mmRLC_CGTT_MGCG_OVERRIDE, 0xffffffff, 0xffffffff,
5062306a36Sopenharmony_ci	mmGRBM_GFX_INDEX, 0xffffffff, 0xe0000000,
5162306a36Sopenharmony_ci	mmCB_CGTT_SCLK_CTRL, 0xffffffff, 0x00000100,
5262306a36Sopenharmony_ci	mmCGTT_BCI_CLK_CTRL, 0xffffffff, 0x00000100,
5362306a36Sopenharmony_ci	mmCGTT_CP_CLK_CTRL, 0xffffffff, 0x00000100,
5462306a36Sopenharmony_ci	mmCGTT_CPC_CLK_CTRL, 0xffffffff, 0x00000100,
5562306a36Sopenharmony_ci	mmCGTT_CPF_CLK_CTRL, 0xffffffff, 0x40000100,
5662306a36Sopenharmony_ci	mmCGTT_DRM_CLK_CTRL0, 0xffffffff, 0x00600100,
5762306a36Sopenharmony_ci	mmCGTT_GDS_CLK_CTRL, 0xffffffff, 0x00000100,
5862306a36Sopenharmony_ci	mmCGTT_IA_CLK_CTRL, 0xffffffff, 0x06000100,
5962306a36Sopenharmony_ci	mmCGTT_PA_CLK_CTRL, 0xffffffff, 0x00000100,
6062306a36Sopenharmony_ci	mmCGTT_WD_CLK_CTRL, 0xffffffff, 0x06000100,
6162306a36Sopenharmony_ci	mmCGTT_PC_CLK_CTRL, 0xffffffff, 0x00000100,
6262306a36Sopenharmony_ci	mmCGTT_RLC_CLK_CTRL, 0xffffffff, 0x00000100,
6362306a36Sopenharmony_ci	mmCGTT_SC_CLK_CTRL, 0xffffffff, 0x00000100,
6462306a36Sopenharmony_ci	mmCGTT_SPI_CLK_CTRL, 0xffffffff, 0x00000100,
6562306a36Sopenharmony_ci	mmCGTT_SQ_CLK_CTRL, 0xffffffff, 0x00000100,
6662306a36Sopenharmony_ci	mmCGTT_SQG_CLK_CTRL, 0xffffffff, 0x00000100,
6762306a36Sopenharmony_ci	mmCGTT_SX_CLK_CTRL0, 0xffffffff, 0x00000100,
6862306a36Sopenharmony_ci	mmCGTT_SX_CLK_CTRL1, 0xffffffff, 0x00000100,
6962306a36Sopenharmony_ci	mmCGTT_SX_CLK_CTRL2, 0xffffffff, 0x00000100,
7062306a36Sopenharmony_ci	mmCGTT_SX_CLK_CTRL3, 0xffffffff, 0x00000100,
7162306a36Sopenharmony_ci	mmCGTT_SX_CLK_CTRL4, 0xffffffff, 0x00000100,
7262306a36Sopenharmony_ci	mmCGTT_TCI_CLK_CTRL, 0xffffffff, 0x00000100,
7362306a36Sopenharmony_ci	mmCGTT_TCP_CLK_CTRL, 0xffffffff, 0x00000100,
7462306a36Sopenharmony_ci	mmCGTT_VGT_CLK_CTRL, 0xffffffff, 0x06000100,
7562306a36Sopenharmony_ci	mmDB_CGTT_CLK_CTRL_0, 0xffffffff, 0x00000100,
7662306a36Sopenharmony_ci	mmTA_CGTT_CTRL, 0xffffffff, 0x00000100,
7762306a36Sopenharmony_ci	mmTCA_CGTT_SCLK_CTRL, 0xffffffff, 0x00000100,
7862306a36Sopenharmony_ci	mmTCC_CGTT_SCLK_CTRL, 0xffffffff, 0x00000100,
7962306a36Sopenharmony_ci	mmTD_CGTT_CTRL, 0xffffffff, 0x00000100,
8062306a36Sopenharmony_ci	mmGRBM_GFX_INDEX, 0xffffffff, 0xe0000000,
8162306a36Sopenharmony_ci	mmCGTS_SM_CTRL_REG, 0xffffffff, 0x96e00200,
8262306a36Sopenharmony_ci	mmCP_RB_WPTR_POLL_CNTL, 0xffffffff, 0x00900100,
8362306a36Sopenharmony_ci	mmRLC_CGCG_CGLS_CTRL, 0xffffffff, 0x0020003c,
8462306a36Sopenharmony_ci	mmPCIE_INDEX, 0xffffffff, 0x0140001c,
8562306a36Sopenharmony_ci	mmPCIE_DATA, 0x000f0000, 0x00000000,
8662306a36Sopenharmony_ci	mmSMC_IND_INDEX_4, 0xffffffff, 0xC060000C,
8762306a36Sopenharmony_ci	mmSMC_IND_DATA_4, 0xc0000fff, 0x00000100,
8862306a36Sopenharmony_ci	mmXDMA_CLOCK_GATING_CNTL, 0xffffffff, 0x00000100,
8962306a36Sopenharmony_ci	mmXDMA_MEM_POWER_CNTL, 0x00000101, 0x00000000,
9062306a36Sopenharmony_ci	mmMC_MEM_POWER_LS, 0xffffffff, 0x00000104,
9162306a36Sopenharmony_ci	mmCGTT_DRM_CLK_CTRL0, 0xff000fff, 0x00000100,
9262306a36Sopenharmony_ci	mmHDP_XDP_CGTT_BLK_CTRL, 0xc0000fff, 0x00000104,
9362306a36Sopenharmony_ci	mmCP_MEM_SLP_CNTL, 0x00000001, 0x00000001,
9462306a36Sopenharmony_ci	mmSDMA0_CLK_CTRL, 0xff000ff0, 0x00000100,
9562306a36Sopenharmony_ci	mmSDMA1_CLK_CTRL, 0xff000ff0, 0x00000100,
9662306a36Sopenharmony_ci};
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_cistatic const u32 xgpu_fiji_golden_settings_a10[] = {
9962306a36Sopenharmony_ci	mmCB_HW_CONTROL_3, 0x000001ff, 0x00000040,
10062306a36Sopenharmony_ci	mmDB_DEBUG2, 0xf00fffff, 0x00000400,
10162306a36Sopenharmony_ci	mmDCI_CLK_CNTL, 0x00000080, 0x00000000,
10262306a36Sopenharmony_ci	mmFBC_DEBUG_COMP, 0x000000f0, 0x00000070,
10362306a36Sopenharmony_ci	mmFBC_MISC, 0x1f311fff, 0x12300000,
10462306a36Sopenharmony_ci	mmHDMI_CONTROL, 0x31000111, 0x00000011,
10562306a36Sopenharmony_ci	mmPA_SC_ENHANCE, 0xffffffff, 0x20000001,
10662306a36Sopenharmony_ci	mmPA_SC_LINE_STIPPLE_STATE, 0x0000ff0f, 0x00000000,
10762306a36Sopenharmony_ci	mmSDMA0_CHICKEN_BITS, 0xfc910007, 0x00810007,
10862306a36Sopenharmony_ci	mmSDMA0_GFX_IB_CNTL, 0x800f0111, 0x00000100,
10962306a36Sopenharmony_ci	mmSDMA0_RLC0_IB_CNTL, 0x800f0111, 0x00000100,
11062306a36Sopenharmony_ci	mmSDMA0_RLC1_IB_CNTL, 0x800f0111, 0x00000100,
11162306a36Sopenharmony_ci	mmSDMA1_CHICKEN_BITS, 0xfc910007, 0x00810007,
11262306a36Sopenharmony_ci	mmSDMA1_GFX_IB_CNTL, 0x800f0111, 0x00000100,
11362306a36Sopenharmony_ci	mmSDMA1_RLC0_IB_CNTL, 0x800f0111, 0x00000100,
11462306a36Sopenharmony_ci	mmSDMA1_RLC1_IB_CNTL, 0x800f0111, 0x00000100,
11562306a36Sopenharmony_ci	mmSQ_RANDOM_WAVE_PRI, 0x001fffff, 0x000006fd,
11662306a36Sopenharmony_ci	mmTA_CNTL_AUX, 0x000f000f, 0x000b0000,
11762306a36Sopenharmony_ci	mmTCC_EXE_DISABLE, 0x00000002, 0x00000002,
11862306a36Sopenharmony_ci	mmTCP_ADDR_CONFIG, 0x000003ff, 0x000000ff,
11962306a36Sopenharmony_ci	mmVGT_RESET_DEBUG, 0x00000004, 0x00000004,
12062306a36Sopenharmony_ci	mmVM_PRT_APERTURE0_LOW_ADDR, 0x0fffffff, 0x0fffffff,
12162306a36Sopenharmony_ci	mmVM_PRT_APERTURE1_LOW_ADDR, 0x0fffffff, 0x0fffffff,
12262306a36Sopenharmony_ci	mmVM_PRT_APERTURE2_LOW_ADDR, 0x0fffffff, 0x0fffffff,
12362306a36Sopenharmony_ci	mmVM_PRT_APERTURE3_LOW_ADDR, 0x0fffffff, 0x0fffffff,
12462306a36Sopenharmony_ci};
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_cistatic const u32 xgpu_fiji_golden_common_all[] = {
12762306a36Sopenharmony_ci	mmGRBM_GFX_INDEX, 0xffffffff, 0xe0000000,
12862306a36Sopenharmony_ci	mmPA_SC_RASTER_CONFIG, 0xffffffff, 0x3a00161a,
12962306a36Sopenharmony_ci	mmPA_SC_RASTER_CONFIG_1, 0xffffffff, 0x0000002e,
13062306a36Sopenharmony_ci	mmGB_ADDR_CONFIG, 0xffffffff, 0x22011003,
13162306a36Sopenharmony_ci	mmSPI_RESOURCE_RESERVE_CU_0, 0xffffffff, 0x00000800,
13262306a36Sopenharmony_ci	mmSPI_RESOURCE_RESERVE_CU_1, 0xffffffff, 0x00000800,
13362306a36Sopenharmony_ci	mmSPI_RESOURCE_RESERVE_EN_CU_0, 0xffffffff, 0x00007FBF,
13462306a36Sopenharmony_ci	mmSPI_RESOURCE_RESERVE_EN_CU_1, 0xffffffff, 0x00007FAF,
13562306a36Sopenharmony_ci	mmGRBM_GFX_INDEX, 0xffffffff, 0xe0000000,
13662306a36Sopenharmony_ci	mmSPI_CONFIG_CNTL_1, 0x0000000f, 0x00000009,
13762306a36Sopenharmony_ci};
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_cistatic const u32 xgpu_tonga_mgcg_cgcg_init[] = {
14062306a36Sopenharmony_ci	mmRLC_CGTT_MGCG_OVERRIDE,   0xffffffff, 0xffffffff,
14162306a36Sopenharmony_ci	mmGRBM_GFX_INDEX,           0xffffffff, 0xe0000000,
14262306a36Sopenharmony_ci	mmCB_CGTT_SCLK_CTRL,        0xffffffff, 0x00000100,
14362306a36Sopenharmony_ci	mmCGTT_BCI_CLK_CTRL,        0xffffffff, 0x00000100,
14462306a36Sopenharmony_ci	mmCGTT_CP_CLK_CTRL,         0xffffffff, 0x00000100,
14562306a36Sopenharmony_ci	mmCGTT_CPC_CLK_CTRL,        0xffffffff, 0x00000100,
14662306a36Sopenharmony_ci	mmCGTT_CPF_CLK_CTRL,        0xffffffff, 0x40000100,
14762306a36Sopenharmony_ci	mmCGTT_DRM_CLK_CTRL0,       0xffffffff, 0x00600100,
14862306a36Sopenharmony_ci	mmCGTT_GDS_CLK_CTRL,        0xffffffff, 0x00000100,
14962306a36Sopenharmony_ci	mmCGTT_IA_CLK_CTRL,         0xffffffff, 0x06000100,
15062306a36Sopenharmony_ci	mmCGTT_PA_CLK_CTRL,         0xffffffff, 0x00000100,
15162306a36Sopenharmony_ci	mmCGTT_WD_CLK_CTRL,         0xffffffff, 0x06000100,
15262306a36Sopenharmony_ci	mmCGTT_PC_CLK_CTRL,         0xffffffff, 0x00000100,
15362306a36Sopenharmony_ci	mmCGTT_RLC_CLK_CTRL,        0xffffffff, 0x00000100,
15462306a36Sopenharmony_ci	mmCGTT_SC_CLK_CTRL,         0xffffffff, 0x00000100,
15562306a36Sopenharmony_ci	mmCGTT_SPI_CLK_CTRL,        0xffffffff, 0x00000100,
15662306a36Sopenharmony_ci	mmCGTT_SQ_CLK_CTRL,         0xffffffff, 0x00000100,
15762306a36Sopenharmony_ci	mmCGTT_SQG_CLK_CTRL,        0xffffffff, 0x00000100,
15862306a36Sopenharmony_ci	mmCGTT_SX_CLK_CTRL0,        0xffffffff, 0x00000100,
15962306a36Sopenharmony_ci	mmCGTT_SX_CLK_CTRL1,        0xffffffff, 0x00000100,
16062306a36Sopenharmony_ci	mmCGTT_SX_CLK_CTRL2,        0xffffffff, 0x00000100,
16162306a36Sopenharmony_ci	mmCGTT_SX_CLK_CTRL3,        0xffffffff, 0x00000100,
16262306a36Sopenharmony_ci	mmCGTT_SX_CLK_CTRL4,        0xffffffff, 0x00000100,
16362306a36Sopenharmony_ci	mmCGTT_TCI_CLK_CTRL,        0xffffffff, 0x00000100,
16462306a36Sopenharmony_ci	mmCGTT_TCP_CLK_CTRL,        0xffffffff, 0x00000100,
16562306a36Sopenharmony_ci	mmCGTT_VGT_CLK_CTRL,        0xffffffff, 0x06000100,
16662306a36Sopenharmony_ci	mmDB_CGTT_CLK_CTRL_0,       0xffffffff, 0x00000100,
16762306a36Sopenharmony_ci	mmTA_CGTT_CTRL,             0xffffffff, 0x00000100,
16862306a36Sopenharmony_ci	mmTCA_CGTT_SCLK_CTRL,       0xffffffff, 0x00000100,
16962306a36Sopenharmony_ci	mmTCC_CGTT_SCLK_CTRL,       0xffffffff, 0x00000100,
17062306a36Sopenharmony_ci	mmTD_CGTT_CTRL,             0xffffffff, 0x00000100,
17162306a36Sopenharmony_ci	mmGRBM_GFX_INDEX,           0xffffffff, 0xe0000000,
17262306a36Sopenharmony_ci	mmCGTS_CU0_SP0_CTRL_REG,    0xffffffff, 0x00010000,
17362306a36Sopenharmony_ci	mmCGTS_CU0_LDS_SQ_CTRL_REG, 0xffffffff, 0x00030002,
17462306a36Sopenharmony_ci	mmCGTS_CU0_TA_SQC_CTRL_REG, 0xffffffff, 0x00040007,
17562306a36Sopenharmony_ci	mmCGTS_CU0_SP1_CTRL_REG,    0xffffffff, 0x00060005,
17662306a36Sopenharmony_ci	mmCGTS_CU0_TD_TCP_CTRL_REG, 0xffffffff, 0x00090008,
17762306a36Sopenharmony_ci	mmCGTS_CU1_SP0_CTRL_REG,    0xffffffff, 0x00010000,
17862306a36Sopenharmony_ci	mmCGTS_CU1_LDS_SQ_CTRL_REG, 0xffffffff, 0x00030002,
17962306a36Sopenharmony_ci	mmCGTS_CU1_TA_CTRL_REG,     0xffffffff, 0x00040007,
18062306a36Sopenharmony_ci	mmCGTS_CU1_SP1_CTRL_REG,    0xffffffff, 0x00060005,
18162306a36Sopenharmony_ci	mmCGTS_CU1_TD_TCP_CTRL_REG, 0xffffffff, 0x00090008,
18262306a36Sopenharmony_ci	mmCGTS_CU2_SP0_CTRL_REG,    0xffffffff, 0x00010000,
18362306a36Sopenharmony_ci	mmCGTS_CU2_LDS_SQ_CTRL_REG, 0xffffffff, 0x00030002,
18462306a36Sopenharmony_ci	mmCGTS_CU2_TA_CTRL_REG,     0xffffffff, 0x00040007,
18562306a36Sopenharmony_ci	mmCGTS_CU2_SP1_CTRL_REG,    0xffffffff, 0x00060005,
18662306a36Sopenharmony_ci	mmCGTS_CU2_TD_TCP_CTRL_REG, 0xffffffff, 0x00090008,
18762306a36Sopenharmony_ci	mmCGTS_CU3_SP0_CTRL_REG,    0xffffffff, 0x00010000,
18862306a36Sopenharmony_ci	mmCGTS_CU3_LDS_SQ_CTRL_REG, 0xffffffff, 0x00030002,
18962306a36Sopenharmony_ci	mmCGTS_CU3_TA_CTRL_REG,     0xffffffff, 0x00040007,
19062306a36Sopenharmony_ci	mmCGTS_CU3_SP1_CTRL_REG,    0xffffffff, 0x00060005,
19162306a36Sopenharmony_ci	mmCGTS_CU3_TD_TCP_CTRL_REG, 0xffffffff, 0x00090008,
19262306a36Sopenharmony_ci	mmCGTS_CU4_SP0_CTRL_REG,    0xffffffff, 0x00010000,
19362306a36Sopenharmony_ci	mmCGTS_CU4_LDS_SQ_CTRL_REG, 0xffffffff, 0x00030002,
19462306a36Sopenharmony_ci	mmCGTS_CU4_TA_SQC_CTRL_REG, 0xffffffff, 0x00040007,
19562306a36Sopenharmony_ci	mmCGTS_CU4_SP1_CTRL_REG,    0xffffffff, 0x00060005,
19662306a36Sopenharmony_ci	mmCGTS_CU4_TD_TCP_CTRL_REG, 0xffffffff, 0x00090008,
19762306a36Sopenharmony_ci	mmCGTS_CU5_SP0_CTRL_REG,    0xffffffff, 0x00010000,
19862306a36Sopenharmony_ci	mmCGTS_CU5_LDS_SQ_CTRL_REG, 0xffffffff, 0x00030002,
19962306a36Sopenharmony_ci	mmCGTS_CU5_TA_CTRL_REG,     0xffffffff, 0x00040007,
20062306a36Sopenharmony_ci	mmCGTS_CU5_SP1_CTRL_REG,    0xffffffff, 0x00060005,
20162306a36Sopenharmony_ci	mmCGTS_CU5_TD_TCP_CTRL_REG, 0xffffffff, 0x00090008,
20262306a36Sopenharmony_ci	mmCGTS_CU6_SP0_CTRL_REG,    0xffffffff, 0x00010000,
20362306a36Sopenharmony_ci	mmCGTS_CU6_LDS_SQ_CTRL_REG, 0xffffffff, 0x00030002,
20462306a36Sopenharmony_ci	mmCGTS_CU6_TA_CTRL_REG,     0xffffffff, 0x00040007,
20562306a36Sopenharmony_ci	mmCGTS_CU6_SP1_CTRL_REG,    0xffffffff, 0x00060005,
20662306a36Sopenharmony_ci	mmCGTS_CU6_TD_TCP_CTRL_REG, 0xffffffff, 0x00090008,
20762306a36Sopenharmony_ci	mmCGTS_CU7_SP0_CTRL_REG,    0xffffffff, 0x00010000,
20862306a36Sopenharmony_ci	mmCGTS_CU7_LDS_SQ_CTRL_REG, 0xffffffff, 0x00030002,
20962306a36Sopenharmony_ci	mmCGTS_CU7_TA_CTRL_REG,     0xffffffff, 0x00040007,
21062306a36Sopenharmony_ci	mmCGTS_CU7_SP1_CTRL_REG,    0xffffffff, 0x00060005,
21162306a36Sopenharmony_ci	mmCGTS_CU7_TD_TCP_CTRL_REG, 0xffffffff, 0x00090008,
21262306a36Sopenharmony_ci	mmCGTS_SM_CTRL_REG,         0xffffffff, 0x96e00200,
21362306a36Sopenharmony_ci	mmCP_RB_WPTR_POLL_CNTL,     0xffffffff, 0x00900100,
21462306a36Sopenharmony_ci	mmRLC_CGCG_CGLS_CTRL,       0xffffffff, 0x0020003c,
21562306a36Sopenharmony_ci	mmPCIE_INDEX,               0xffffffff, 0x0140001c,
21662306a36Sopenharmony_ci	mmPCIE_DATA,                0x000f0000, 0x00000000,
21762306a36Sopenharmony_ci	mmSMC_IND_INDEX_4,          0xffffffff, 0xC060000C,
21862306a36Sopenharmony_ci	mmSMC_IND_DATA_4,           0xc0000fff, 0x00000100,
21962306a36Sopenharmony_ci	mmXDMA_CLOCK_GATING_CNTL,   0xffffffff, 0x00000100,
22062306a36Sopenharmony_ci	mmXDMA_MEM_POWER_CNTL,      0x00000101, 0x00000000,
22162306a36Sopenharmony_ci	mmMC_MEM_POWER_LS,          0xffffffff, 0x00000104,
22262306a36Sopenharmony_ci	mmCGTT_DRM_CLK_CTRL0,       0xff000fff, 0x00000100,
22362306a36Sopenharmony_ci	mmHDP_XDP_CGTT_BLK_CTRL,    0xc0000fff, 0x00000104,
22462306a36Sopenharmony_ci	mmCP_MEM_SLP_CNTL,          0x00000001, 0x00000001,
22562306a36Sopenharmony_ci	mmSDMA0_CLK_CTRL,           0xff000ff0, 0x00000100,
22662306a36Sopenharmony_ci	mmSDMA1_CLK_CTRL,           0xff000ff0, 0x00000100,
22762306a36Sopenharmony_ci};
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_cistatic const u32 xgpu_tonga_golden_settings_a11[] = {
23062306a36Sopenharmony_ci	mmCB_HW_CONTROL, 0xfffdf3cf, 0x00007208,
23162306a36Sopenharmony_ci	mmCB_HW_CONTROL_3, 0x00000040, 0x00000040,
23262306a36Sopenharmony_ci	mmDB_DEBUG2, 0xf00fffff, 0x00000400,
23362306a36Sopenharmony_ci	mmDCI_CLK_CNTL, 0x00000080, 0x00000000,
23462306a36Sopenharmony_ci	mmFBC_DEBUG_COMP, 0x000000f0, 0x00000070,
23562306a36Sopenharmony_ci	mmFBC_MISC, 0x1f311fff, 0x12300000,
23662306a36Sopenharmony_ci	mmGB_GPU_ID, 0x0000000f, 0x00000000,
23762306a36Sopenharmony_ci	mmHDMI_CONTROL, 0x31000111, 0x00000011,
23862306a36Sopenharmony_ci	mmMC_ARB_WTM_GRPWT_RD, 0x00000003, 0x00000000,
23962306a36Sopenharmony_ci	mmMC_HUB_RDREQ_DMIF_LIMIT, 0x0000007f, 0x00000028,
24062306a36Sopenharmony_ci	mmMC_HUB_WDP_UMC, 0x00007fb6, 0x00000991,
24162306a36Sopenharmony_ci	mmPA_SC_ENHANCE, 0xffffffff, 0x20000001,
24262306a36Sopenharmony_ci	mmPA_SC_FIFO_DEPTH_CNTL, 0x000003ff, 0x000000fc,
24362306a36Sopenharmony_ci	mmPA_SC_LINE_STIPPLE_STATE, 0x0000ff0f, 0x00000000,
24462306a36Sopenharmony_ci	mmRLC_CGCG_CGLS_CTRL, 0x00000003, 0x0000003c,
24562306a36Sopenharmony_ci	mmSDMA0_CHICKEN_BITS, 0xfc910007, 0x00810007,
24662306a36Sopenharmony_ci	mmSDMA0_CLK_CTRL, 0xff000fff, 0x00000000,
24762306a36Sopenharmony_ci	mmSDMA0_GFX_IB_CNTL, 0x800f0111, 0x00000100,
24862306a36Sopenharmony_ci	mmSDMA0_RLC0_IB_CNTL, 0x800f0111, 0x00000100,
24962306a36Sopenharmony_ci	mmSDMA0_RLC1_IB_CNTL, 0x800f0111, 0x00000100,
25062306a36Sopenharmony_ci	mmSDMA1_CHICKEN_BITS, 0xfc910007, 0x00810007,
25162306a36Sopenharmony_ci	mmSDMA1_CLK_CTRL, 0xff000fff, 0x00000000,
25262306a36Sopenharmony_ci	mmSDMA1_GFX_IB_CNTL, 0x800f0111, 0x00000100,
25362306a36Sopenharmony_ci	mmSDMA1_RLC0_IB_CNTL, 0x800f0111, 0x00000100,
25462306a36Sopenharmony_ci	mmSDMA1_RLC1_IB_CNTL, 0x800f0111, 0x00000100,
25562306a36Sopenharmony_ci	mmSQ_RANDOM_WAVE_PRI, 0x001fffff, 0x000006fd,
25662306a36Sopenharmony_ci	mmTA_CNTL_AUX, 0x000f000f, 0x000b0000,
25762306a36Sopenharmony_ci	mmTCC_CTRL, 0x00100000, 0xf31fff7f,
25862306a36Sopenharmony_ci	mmTCC_EXE_DISABLE, 0x00000002, 0x00000002,
25962306a36Sopenharmony_ci	mmTCP_ADDR_CONFIG, 0x000003ff, 0x000002fb,
26062306a36Sopenharmony_ci	mmTCP_CHAN_STEER_HI, 0xffffffff, 0x0000543b,
26162306a36Sopenharmony_ci	mmTCP_CHAN_STEER_LO, 0xffffffff, 0xa9210876,
26262306a36Sopenharmony_ci	mmVGT_RESET_DEBUG, 0x00000004, 0x00000004,
26362306a36Sopenharmony_ci	mmVM_PRT_APERTURE0_LOW_ADDR, 0x0fffffff, 0x0fffffff,
26462306a36Sopenharmony_ci	mmVM_PRT_APERTURE1_LOW_ADDR, 0x0fffffff, 0x0fffffff,
26562306a36Sopenharmony_ci	mmVM_PRT_APERTURE2_LOW_ADDR, 0x0fffffff, 0x0fffffff,
26662306a36Sopenharmony_ci	mmVM_PRT_APERTURE3_LOW_ADDR, 0x0fffffff, 0x0fffffff,
26762306a36Sopenharmony_ci};
26862306a36Sopenharmony_ci
26962306a36Sopenharmony_cistatic const u32 xgpu_tonga_golden_common_all[] = {
27062306a36Sopenharmony_ci	mmGRBM_GFX_INDEX,               0xffffffff, 0xe0000000,
27162306a36Sopenharmony_ci	mmPA_SC_RASTER_CONFIG,          0xffffffff, 0x16000012,
27262306a36Sopenharmony_ci	mmPA_SC_RASTER_CONFIG_1,        0xffffffff, 0x0000002A,
27362306a36Sopenharmony_ci	mmGB_ADDR_CONFIG,               0xffffffff, 0x22011002,
27462306a36Sopenharmony_ci	mmSPI_RESOURCE_RESERVE_CU_0,    0xffffffff, 0x00000800,
27562306a36Sopenharmony_ci	mmSPI_RESOURCE_RESERVE_CU_1,    0xffffffff, 0x00000800,
27662306a36Sopenharmony_ci	mmSPI_RESOURCE_RESERVE_EN_CU_0, 0xffffffff, 0x00007FBF,
27762306a36Sopenharmony_ci};
27862306a36Sopenharmony_ci
27962306a36Sopenharmony_civoid xgpu_vi_init_golden_registers(struct amdgpu_device *adev)
28062306a36Sopenharmony_ci{
28162306a36Sopenharmony_ci	switch (adev->asic_type) {
28262306a36Sopenharmony_ci	case CHIP_FIJI:
28362306a36Sopenharmony_ci		amdgpu_device_program_register_sequence(adev,
28462306a36Sopenharmony_ci							xgpu_fiji_mgcg_cgcg_init,
28562306a36Sopenharmony_ci							ARRAY_SIZE(
28662306a36Sopenharmony_ci								xgpu_fiji_mgcg_cgcg_init));
28762306a36Sopenharmony_ci		amdgpu_device_program_register_sequence(adev,
28862306a36Sopenharmony_ci							xgpu_fiji_golden_settings_a10,
28962306a36Sopenharmony_ci							ARRAY_SIZE(
29062306a36Sopenharmony_ci								xgpu_fiji_golden_settings_a10));
29162306a36Sopenharmony_ci		amdgpu_device_program_register_sequence(adev,
29262306a36Sopenharmony_ci							xgpu_fiji_golden_common_all,
29362306a36Sopenharmony_ci							ARRAY_SIZE(
29462306a36Sopenharmony_ci								xgpu_fiji_golden_common_all));
29562306a36Sopenharmony_ci		break;
29662306a36Sopenharmony_ci	case CHIP_TONGA:
29762306a36Sopenharmony_ci		amdgpu_device_program_register_sequence(adev,
29862306a36Sopenharmony_ci							xgpu_tonga_mgcg_cgcg_init,
29962306a36Sopenharmony_ci							ARRAY_SIZE(
30062306a36Sopenharmony_ci								xgpu_tonga_mgcg_cgcg_init));
30162306a36Sopenharmony_ci		amdgpu_device_program_register_sequence(adev,
30262306a36Sopenharmony_ci							xgpu_tonga_golden_settings_a11,
30362306a36Sopenharmony_ci							ARRAY_SIZE(
30462306a36Sopenharmony_ci								xgpu_tonga_golden_settings_a11));
30562306a36Sopenharmony_ci		amdgpu_device_program_register_sequence(adev,
30662306a36Sopenharmony_ci							xgpu_tonga_golden_common_all,
30762306a36Sopenharmony_ci							ARRAY_SIZE(
30862306a36Sopenharmony_ci								xgpu_tonga_golden_common_all));
30962306a36Sopenharmony_ci		break;
31062306a36Sopenharmony_ci	default:
31162306a36Sopenharmony_ci		BUG_ON("Doesn't support chip type.\n");
31262306a36Sopenharmony_ci		break;
31362306a36Sopenharmony_ci	}
31462306a36Sopenharmony_ci}
31562306a36Sopenharmony_ci
31662306a36Sopenharmony_ci/*
31762306a36Sopenharmony_ci * Mailbox communication between GPU hypervisor and VFs
31862306a36Sopenharmony_ci */
31962306a36Sopenharmony_cistatic void xgpu_vi_mailbox_send_ack(struct amdgpu_device *adev)
32062306a36Sopenharmony_ci{
32162306a36Sopenharmony_ci	u32 reg;
32262306a36Sopenharmony_ci	int timeout = VI_MAILBOX_TIMEDOUT;
32362306a36Sopenharmony_ci	u32 mask = REG_FIELD_MASK(MAILBOX_CONTROL, RCV_MSG_VALID);
32462306a36Sopenharmony_ci
32562306a36Sopenharmony_ci	reg = RREG32_NO_KIQ(mmMAILBOX_CONTROL);
32662306a36Sopenharmony_ci	reg = REG_SET_FIELD(reg, MAILBOX_CONTROL, RCV_MSG_ACK, 1);
32762306a36Sopenharmony_ci	WREG32_NO_KIQ(mmMAILBOX_CONTROL, reg);
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_ci	/*Wait for RCV_MSG_VALID to be 0*/
33062306a36Sopenharmony_ci	reg = RREG32_NO_KIQ(mmMAILBOX_CONTROL);
33162306a36Sopenharmony_ci	while (reg & mask) {
33262306a36Sopenharmony_ci		if (timeout <= 0) {
33362306a36Sopenharmony_ci			pr_err("RCV_MSG_VALID is not cleared\n");
33462306a36Sopenharmony_ci			break;
33562306a36Sopenharmony_ci		}
33662306a36Sopenharmony_ci		mdelay(1);
33762306a36Sopenharmony_ci		timeout -= 1;
33862306a36Sopenharmony_ci
33962306a36Sopenharmony_ci		reg = RREG32_NO_KIQ(mmMAILBOX_CONTROL);
34062306a36Sopenharmony_ci	}
34162306a36Sopenharmony_ci}
34262306a36Sopenharmony_ci
34362306a36Sopenharmony_cistatic void xgpu_vi_mailbox_set_valid(struct amdgpu_device *adev, bool val)
34462306a36Sopenharmony_ci{
34562306a36Sopenharmony_ci	u32 reg;
34662306a36Sopenharmony_ci
34762306a36Sopenharmony_ci	reg = RREG32_NO_KIQ(mmMAILBOX_CONTROL);
34862306a36Sopenharmony_ci	reg = REG_SET_FIELD(reg, MAILBOX_CONTROL,
34962306a36Sopenharmony_ci			    TRN_MSG_VALID, val ? 1 : 0);
35062306a36Sopenharmony_ci	WREG32_NO_KIQ(mmMAILBOX_CONTROL, reg);
35162306a36Sopenharmony_ci}
35262306a36Sopenharmony_ci
35362306a36Sopenharmony_cistatic void xgpu_vi_mailbox_trans_msg(struct amdgpu_device *adev,
35462306a36Sopenharmony_ci				      enum idh_request req)
35562306a36Sopenharmony_ci{
35662306a36Sopenharmony_ci	u32 reg;
35762306a36Sopenharmony_ci
35862306a36Sopenharmony_ci	reg = RREG32_NO_KIQ(mmMAILBOX_MSGBUF_TRN_DW0);
35962306a36Sopenharmony_ci	reg = REG_SET_FIELD(reg, MAILBOX_MSGBUF_TRN_DW0,
36062306a36Sopenharmony_ci			    MSGBUF_DATA, req);
36162306a36Sopenharmony_ci	WREG32_NO_KIQ(mmMAILBOX_MSGBUF_TRN_DW0, reg);
36262306a36Sopenharmony_ci
36362306a36Sopenharmony_ci	xgpu_vi_mailbox_set_valid(adev, true);
36462306a36Sopenharmony_ci}
36562306a36Sopenharmony_ci
36662306a36Sopenharmony_cistatic int xgpu_vi_mailbox_rcv_msg(struct amdgpu_device *adev,
36762306a36Sopenharmony_ci				   enum idh_event event)
36862306a36Sopenharmony_ci{
36962306a36Sopenharmony_ci	u32 reg;
37062306a36Sopenharmony_ci	u32 mask = REG_FIELD_MASK(MAILBOX_CONTROL, RCV_MSG_VALID);
37162306a36Sopenharmony_ci
37262306a36Sopenharmony_ci	/* workaround: host driver doesn't set VALID for CMPL now */
37362306a36Sopenharmony_ci	if (event != IDH_FLR_NOTIFICATION_CMPL) {
37462306a36Sopenharmony_ci		reg = RREG32_NO_KIQ(mmMAILBOX_CONTROL);
37562306a36Sopenharmony_ci		if (!(reg & mask))
37662306a36Sopenharmony_ci			return -ENOENT;
37762306a36Sopenharmony_ci	}
37862306a36Sopenharmony_ci
37962306a36Sopenharmony_ci	reg = RREG32_NO_KIQ(mmMAILBOX_MSGBUF_RCV_DW0);
38062306a36Sopenharmony_ci	if (reg != event)
38162306a36Sopenharmony_ci		return -ENOENT;
38262306a36Sopenharmony_ci
38362306a36Sopenharmony_ci	/* send ack to PF */
38462306a36Sopenharmony_ci	xgpu_vi_mailbox_send_ack(adev);
38562306a36Sopenharmony_ci
38662306a36Sopenharmony_ci	return 0;
38762306a36Sopenharmony_ci}
38862306a36Sopenharmony_ci
38962306a36Sopenharmony_cistatic int xgpu_vi_poll_ack(struct amdgpu_device *adev)
39062306a36Sopenharmony_ci{
39162306a36Sopenharmony_ci	int r = 0, timeout = VI_MAILBOX_TIMEDOUT;
39262306a36Sopenharmony_ci	u32 mask = REG_FIELD_MASK(MAILBOX_CONTROL, TRN_MSG_ACK);
39362306a36Sopenharmony_ci	u32 reg;
39462306a36Sopenharmony_ci
39562306a36Sopenharmony_ci	reg = RREG32_NO_KIQ(mmMAILBOX_CONTROL);
39662306a36Sopenharmony_ci	while (!(reg & mask)) {
39762306a36Sopenharmony_ci		if (timeout <= 0) {
39862306a36Sopenharmony_ci			pr_err("Doesn't get ack from pf.\n");
39962306a36Sopenharmony_ci			r = -ETIME;
40062306a36Sopenharmony_ci			break;
40162306a36Sopenharmony_ci		}
40262306a36Sopenharmony_ci		mdelay(5);
40362306a36Sopenharmony_ci		timeout -= 5;
40462306a36Sopenharmony_ci
40562306a36Sopenharmony_ci		reg = RREG32_NO_KIQ(mmMAILBOX_CONTROL);
40662306a36Sopenharmony_ci	}
40762306a36Sopenharmony_ci
40862306a36Sopenharmony_ci	return r;
40962306a36Sopenharmony_ci}
41062306a36Sopenharmony_ci
41162306a36Sopenharmony_cistatic int xgpu_vi_poll_msg(struct amdgpu_device *adev, enum idh_event event)
41262306a36Sopenharmony_ci{
41362306a36Sopenharmony_ci	int r = 0, timeout = VI_MAILBOX_TIMEDOUT;
41462306a36Sopenharmony_ci
41562306a36Sopenharmony_ci	r = xgpu_vi_mailbox_rcv_msg(adev, event);
41662306a36Sopenharmony_ci	while (r) {
41762306a36Sopenharmony_ci		if (timeout <= 0) {
41862306a36Sopenharmony_ci			pr_err("Doesn't get ack from pf.\n");
41962306a36Sopenharmony_ci			r = -ETIME;
42062306a36Sopenharmony_ci			break;
42162306a36Sopenharmony_ci		}
42262306a36Sopenharmony_ci		mdelay(5);
42362306a36Sopenharmony_ci		timeout -= 5;
42462306a36Sopenharmony_ci
42562306a36Sopenharmony_ci		r = xgpu_vi_mailbox_rcv_msg(adev, event);
42662306a36Sopenharmony_ci	}
42762306a36Sopenharmony_ci
42862306a36Sopenharmony_ci	return r;
42962306a36Sopenharmony_ci}
43062306a36Sopenharmony_ci
43162306a36Sopenharmony_cistatic int xgpu_vi_send_access_requests(struct amdgpu_device *adev,
43262306a36Sopenharmony_ci					enum idh_request request)
43362306a36Sopenharmony_ci{
43462306a36Sopenharmony_ci	int r;
43562306a36Sopenharmony_ci
43662306a36Sopenharmony_ci	xgpu_vi_mailbox_trans_msg(adev, request);
43762306a36Sopenharmony_ci
43862306a36Sopenharmony_ci	/* start to poll ack */
43962306a36Sopenharmony_ci	r = xgpu_vi_poll_ack(adev);
44062306a36Sopenharmony_ci	if (r)
44162306a36Sopenharmony_ci		return r;
44262306a36Sopenharmony_ci
44362306a36Sopenharmony_ci	xgpu_vi_mailbox_set_valid(adev, false);
44462306a36Sopenharmony_ci
44562306a36Sopenharmony_ci	/* start to check msg if request is idh_req_gpu_init_access */
44662306a36Sopenharmony_ci	if (request == IDH_REQ_GPU_INIT_ACCESS ||
44762306a36Sopenharmony_ci		request == IDH_REQ_GPU_FINI_ACCESS ||
44862306a36Sopenharmony_ci		request == IDH_REQ_GPU_RESET_ACCESS) {
44962306a36Sopenharmony_ci		r = xgpu_vi_poll_msg(adev, IDH_READY_TO_ACCESS_GPU);
45062306a36Sopenharmony_ci		if (r) {
45162306a36Sopenharmony_ci			pr_err("Doesn't get ack from pf, give up\n");
45262306a36Sopenharmony_ci			return r;
45362306a36Sopenharmony_ci		}
45462306a36Sopenharmony_ci	}
45562306a36Sopenharmony_ci
45662306a36Sopenharmony_ci	return 0;
45762306a36Sopenharmony_ci}
45862306a36Sopenharmony_ci
45962306a36Sopenharmony_cistatic int xgpu_vi_request_reset(struct amdgpu_device *adev)
46062306a36Sopenharmony_ci{
46162306a36Sopenharmony_ci	return xgpu_vi_send_access_requests(adev, IDH_REQ_GPU_RESET_ACCESS);
46262306a36Sopenharmony_ci}
46362306a36Sopenharmony_ci
46462306a36Sopenharmony_cistatic int xgpu_vi_wait_reset_cmpl(struct amdgpu_device *adev)
46562306a36Sopenharmony_ci{
46662306a36Sopenharmony_ci	return xgpu_vi_poll_msg(adev, IDH_FLR_NOTIFICATION_CMPL);
46762306a36Sopenharmony_ci}
46862306a36Sopenharmony_ci
46962306a36Sopenharmony_cistatic int xgpu_vi_request_full_gpu_access(struct amdgpu_device *adev,
47062306a36Sopenharmony_ci					   bool init)
47162306a36Sopenharmony_ci{
47262306a36Sopenharmony_ci	enum idh_request req;
47362306a36Sopenharmony_ci
47462306a36Sopenharmony_ci	req = init ? IDH_REQ_GPU_INIT_ACCESS : IDH_REQ_GPU_FINI_ACCESS;
47562306a36Sopenharmony_ci	return xgpu_vi_send_access_requests(adev, req);
47662306a36Sopenharmony_ci}
47762306a36Sopenharmony_ci
47862306a36Sopenharmony_cistatic int xgpu_vi_release_full_gpu_access(struct amdgpu_device *adev,
47962306a36Sopenharmony_ci					   bool init)
48062306a36Sopenharmony_ci{
48162306a36Sopenharmony_ci	enum idh_request req;
48262306a36Sopenharmony_ci	int r = 0;
48362306a36Sopenharmony_ci
48462306a36Sopenharmony_ci	req = init ? IDH_REL_GPU_INIT_ACCESS : IDH_REL_GPU_FINI_ACCESS;
48562306a36Sopenharmony_ci	r = xgpu_vi_send_access_requests(adev, req);
48662306a36Sopenharmony_ci
48762306a36Sopenharmony_ci	return r;
48862306a36Sopenharmony_ci}
48962306a36Sopenharmony_ci
49062306a36Sopenharmony_ci/* add support mailbox interrupts */
49162306a36Sopenharmony_cistatic int xgpu_vi_mailbox_ack_irq(struct amdgpu_device *adev,
49262306a36Sopenharmony_ci				   struct amdgpu_irq_src *source,
49362306a36Sopenharmony_ci				   struct amdgpu_iv_entry *entry)
49462306a36Sopenharmony_ci{
49562306a36Sopenharmony_ci	DRM_DEBUG("get ack intr and do nothing.\n");
49662306a36Sopenharmony_ci	return 0;
49762306a36Sopenharmony_ci}
49862306a36Sopenharmony_ci
49962306a36Sopenharmony_cistatic int xgpu_vi_set_mailbox_ack_irq(struct amdgpu_device *adev,
50062306a36Sopenharmony_ci				       struct amdgpu_irq_src *src,
50162306a36Sopenharmony_ci				       unsigned type,
50262306a36Sopenharmony_ci				       enum amdgpu_interrupt_state state)
50362306a36Sopenharmony_ci{
50462306a36Sopenharmony_ci	u32 tmp = RREG32_NO_KIQ(mmMAILBOX_INT_CNTL);
50562306a36Sopenharmony_ci
50662306a36Sopenharmony_ci	tmp = REG_SET_FIELD(tmp, MAILBOX_INT_CNTL, ACK_INT_EN,
50762306a36Sopenharmony_ci			    (state == AMDGPU_IRQ_STATE_ENABLE) ? 1 : 0);
50862306a36Sopenharmony_ci	WREG32_NO_KIQ(mmMAILBOX_INT_CNTL, tmp);
50962306a36Sopenharmony_ci
51062306a36Sopenharmony_ci	return 0;
51162306a36Sopenharmony_ci}
51262306a36Sopenharmony_ci
51362306a36Sopenharmony_cistatic void xgpu_vi_mailbox_flr_work(struct work_struct *work)
51462306a36Sopenharmony_ci{
51562306a36Sopenharmony_ci	struct amdgpu_virt *virt = container_of(work, struct amdgpu_virt, flr_work);
51662306a36Sopenharmony_ci	struct amdgpu_device *adev = container_of(virt, struct amdgpu_device, virt);
51762306a36Sopenharmony_ci
51862306a36Sopenharmony_ci	/* wait until RCV_MSG become 3 */
51962306a36Sopenharmony_ci	if (xgpu_vi_poll_msg(adev, IDH_FLR_NOTIFICATION_CMPL)) {
52062306a36Sopenharmony_ci		pr_err("failed to receive FLR_CMPL\n");
52162306a36Sopenharmony_ci		return;
52262306a36Sopenharmony_ci	}
52362306a36Sopenharmony_ci
52462306a36Sopenharmony_ci	/* Trigger recovery due to world switch failure */
52562306a36Sopenharmony_ci	if (amdgpu_device_should_recover_gpu(adev)) {
52662306a36Sopenharmony_ci		struct amdgpu_reset_context reset_context;
52762306a36Sopenharmony_ci		memset(&reset_context, 0, sizeof(reset_context));
52862306a36Sopenharmony_ci
52962306a36Sopenharmony_ci		reset_context.method = AMD_RESET_METHOD_NONE;
53062306a36Sopenharmony_ci		reset_context.reset_req_dev = adev;
53162306a36Sopenharmony_ci		clear_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
53262306a36Sopenharmony_ci
53362306a36Sopenharmony_ci		amdgpu_device_gpu_recover(adev, NULL, &reset_context);
53462306a36Sopenharmony_ci	}
53562306a36Sopenharmony_ci}
53662306a36Sopenharmony_ci
53762306a36Sopenharmony_cistatic int xgpu_vi_set_mailbox_rcv_irq(struct amdgpu_device *adev,
53862306a36Sopenharmony_ci				       struct amdgpu_irq_src *src,
53962306a36Sopenharmony_ci				       unsigned type,
54062306a36Sopenharmony_ci				       enum amdgpu_interrupt_state state)
54162306a36Sopenharmony_ci{
54262306a36Sopenharmony_ci	u32 tmp = RREG32_NO_KIQ(mmMAILBOX_INT_CNTL);
54362306a36Sopenharmony_ci
54462306a36Sopenharmony_ci	tmp = REG_SET_FIELD(tmp, MAILBOX_INT_CNTL, VALID_INT_EN,
54562306a36Sopenharmony_ci			    (state == AMDGPU_IRQ_STATE_ENABLE) ? 1 : 0);
54662306a36Sopenharmony_ci	WREG32_NO_KIQ(mmMAILBOX_INT_CNTL, tmp);
54762306a36Sopenharmony_ci
54862306a36Sopenharmony_ci	return 0;
54962306a36Sopenharmony_ci}
55062306a36Sopenharmony_ci
55162306a36Sopenharmony_cistatic int xgpu_vi_mailbox_rcv_irq(struct amdgpu_device *adev,
55262306a36Sopenharmony_ci				   struct amdgpu_irq_src *source,
55362306a36Sopenharmony_ci				   struct amdgpu_iv_entry *entry)
55462306a36Sopenharmony_ci{
55562306a36Sopenharmony_ci	int r;
55662306a36Sopenharmony_ci
55762306a36Sopenharmony_ci	/* trigger gpu-reset by hypervisor only if TDR disabled */
55862306a36Sopenharmony_ci	if (!amdgpu_gpu_recovery) {
55962306a36Sopenharmony_ci		/* see what event we get */
56062306a36Sopenharmony_ci		r = xgpu_vi_mailbox_rcv_msg(adev, IDH_FLR_NOTIFICATION);
56162306a36Sopenharmony_ci
56262306a36Sopenharmony_ci		/* only handle FLR_NOTIFY now */
56362306a36Sopenharmony_ci		if (!r && !amdgpu_in_reset(adev))
56462306a36Sopenharmony_ci			WARN_ONCE(!amdgpu_reset_domain_schedule(adev->reset_domain,
56562306a36Sopenharmony_ci								&adev->virt.flr_work),
56662306a36Sopenharmony_ci				  "Failed to queue work! at %s",
56762306a36Sopenharmony_ci				  __func__);
56862306a36Sopenharmony_ci	}
56962306a36Sopenharmony_ci
57062306a36Sopenharmony_ci	return 0;
57162306a36Sopenharmony_ci}
57262306a36Sopenharmony_ci
57362306a36Sopenharmony_cistatic const struct amdgpu_irq_src_funcs xgpu_vi_mailbox_ack_irq_funcs = {
57462306a36Sopenharmony_ci	.set = xgpu_vi_set_mailbox_ack_irq,
57562306a36Sopenharmony_ci	.process = xgpu_vi_mailbox_ack_irq,
57662306a36Sopenharmony_ci};
57762306a36Sopenharmony_ci
57862306a36Sopenharmony_cistatic const struct amdgpu_irq_src_funcs xgpu_vi_mailbox_rcv_irq_funcs = {
57962306a36Sopenharmony_ci	.set = xgpu_vi_set_mailbox_rcv_irq,
58062306a36Sopenharmony_ci	.process = xgpu_vi_mailbox_rcv_irq,
58162306a36Sopenharmony_ci};
58262306a36Sopenharmony_ci
58362306a36Sopenharmony_civoid xgpu_vi_mailbox_set_irq_funcs(struct amdgpu_device *adev)
58462306a36Sopenharmony_ci{
58562306a36Sopenharmony_ci	adev->virt.ack_irq.num_types = 1;
58662306a36Sopenharmony_ci	adev->virt.ack_irq.funcs = &xgpu_vi_mailbox_ack_irq_funcs;
58762306a36Sopenharmony_ci	adev->virt.rcv_irq.num_types = 1;
58862306a36Sopenharmony_ci	adev->virt.rcv_irq.funcs = &xgpu_vi_mailbox_rcv_irq_funcs;
58962306a36Sopenharmony_ci}
59062306a36Sopenharmony_ci
59162306a36Sopenharmony_ciint xgpu_vi_mailbox_add_irq_id(struct amdgpu_device *adev)
59262306a36Sopenharmony_ci{
59362306a36Sopenharmony_ci	int r;
59462306a36Sopenharmony_ci
59562306a36Sopenharmony_ci	r = amdgpu_irq_add_id(adev, AMDGPU_IRQ_CLIENTID_LEGACY, 135, &adev->virt.rcv_irq);
59662306a36Sopenharmony_ci	if (r)
59762306a36Sopenharmony_ci		return r;
59862306a36Sopenharmony_ci
59962306a36Sopenharmony_ci	r = amdgpu_irq_add_id(adev, AMDGPU_IRQ_CLIENTID_LEGACY, 138, &adev->virt.ack_irq);
60062306a36Sopenharmony_ci	if (r) {
60162306a36Sopenharmony_ci		amdgpu_irq_put(adev, &adev->virt.rcv_irq, 0);
60262306a36Sopenharmony_ci		return r;
60362306a36Sopenharmony_ci	}
60462306a36Sopenharmony_ci
60562306a36Sopenharmony_ci	return 0;
60662306a36Sopenharmony_ci}
60762306a36Sopenharmony_ci
60862306a36Sopenharmony_ciint xgpu_vi_mailbox_get_irq(struct amdgpu_device *adev)
60962306a36Sopenharmony_ci{
61062306a36Sopenharmony_ci	int r;
61162306a36Sopenharmony_ci
61262306a36Sopenharmony_ci	r = amdgpu_irq_get(adev, &adev->virt.rcv_irq, 0);
61362306a36Sopenharmony_ci	if (r)
61462306a36Sopenharmony_ci		return r;
61562306a36Sopenharmony_ci	r = amdgpu_irq_get(adev, &adev->virt.ack_irq, 0);
61662306a36Sopenharmony_ci	if (r) {
61762306a36Sopenharmony_ci		amdgpu_irq_put(adev, &adev->virt.rcv_irq, 0);
61862306a36Sopenharmony_ci		return r;
61962306a36Sopenharmony_ci	}
62062306a36Sopenharmony_ci
62162306a36Sopenharmony_ci	INIT_WORK(&adev->virt.flr_work, xgpu_vi_mailbox_flr_work);
62262306a36Sopenharmony_ci
62362306a36Sopenharmony_ci	return 0;
62462306a36Sopenharmony_ci}
62562306a36Sopenharmony_ci
62662306a36Sopenharmony_civoid xgpu_vi_mailbox_put_irq(struct amdgpu_device *adev)
62762306a36Sopenharmony_ci{
62862306a36Sopenharmony_ci	amdgpu_irq_put(adev, &adev->virt.ack_irq, 0);
62962306a36Sopenharmony_ci	amdgpu_irq_put(adev, &adev->virt.rcv_irq, 0);
63062306a36Sopenharmony_ci}
63162306a36Sopenharmony_ci
63262306a36Sopenharmony_ciconst struct amdgpu_virt_ops xgpu_vi_virt_ops = {
63362306a36Sopenharmony_ci	.req_full_gpu		= xgpu_vi_request_full_gpu_access,
63462306a36Sopenharmony_ci	.rel_full_gpu		= xgpu_vi_release_full_gpu_access,
63562306a36Sopenharmony_ci	.reset_gpu		= xgpu_vi_request_reset,
63662306a36Sopenharmony_ci	.wait_reset             = xgpu_vi_wait_reset_cmpl,
63762306a36Sopenharmony_ci	.trans_msg		= NULL, /* Does not need to trans VF errors to host. */
63862306a36Sopenharmony_ci};
639