xref: /dragonfly/sys/dev/drm/amd/amdgpu/gfx_v9_0.c (revision 809f3802)
1b843c749SSergey Zigachev /*
2b843c749SSergey Zigachev  * Copyright 2016 Advanced Micro Devices, Inc.
3b843c749SSergey Zigachev  *
4b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
5b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
6b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
7b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
9b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
10b843c749SSergey Zigachev  *
11b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
12b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
13b843c749SSergey Zigachev  *
14b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
21b843c749SSergey Zigachev  *
22b843c749SSergey Zigachev  */
23b843c749SSergey Zigachev #include <linux/kernel.h>
24b843c749SSergey Zigachev #include <linux/firmware.h>
25b843c749SSergey Zigachev #include <drm/drmP.h>
26b843c749SSergey Zigachev #include "amdgpu.h"
27b843c749SSergey Zigachev #include "amdgpu_gfx.h"
28b843c749SSergey Zigachev #include "soc15.h"
29b843c749SSergey Zigachev #include "soc15d.h"
30b843c749SSergey Zigachev #include "amdgpu_atomfirmware.h"
31b843c749SSergey Zigachev 
32b843c749SSergey Zigachev #include "gc/gc_9_0_offset.h"
33b843c749SSergey Zigachev #include "gc/gc_9_0_sh_mask.h"
34b843c749SSergey Zigachev #include "vega10_enum.h"
35b843c749SSergey Zigachev #include "hdp/hdp_4_0_offset.h"
36b843c749SSergey Zigachev 
37b843c749SSergey Zigachev #include "soc15_common.h"
38b843c749SSergey Zigachev #include "clearstate_gfx9.h"
39b843c749SSergey Zigachev #include "v9_structs.h"
40b843c749SSergey Zigachev 
41b843c749SSergey Zigachev #include "ivsrcid/gfx/irqsrcs_gfx_9_0.h"
42b843c749SSergey Zigachev 
43b843c749SSergey Zigachev #define GFX9_NUM_GFX_RINGS     1
44b843c749SSergey Zigachev #define GFX9_MEC_HPD_SIZE 2048
45b843c749SSergey Zigachev #define RLCG_UCODE_LOADING_START_ADDRESS 0x00002000L
46b843c749SSergey Zigachev #define RLC_SAVE_RESTORE_ADDR_STARTING_OFFSET 0x00000000L
47b843c749SSergey Zigachev 
48b843c749SSergey Zigachev #define mmPWR_MISC_CNTL_STATUS					0x0183
49b843c749SSergey Zigachev #define mmPWR_MISC_CNTL_STATUS_BASE_IDX				0
50b843c749SSergey Zigachev #define PWR_MISC_CNTL_STATUS__PWR_GFX_RLC_CGPG_EN__SHIFT	0x0
51b843c749SSergey Zigachev #define PWR_MISC_CNTL_STATUS__PWR_GFXOFF_STATUS__SHIFT		0x1
52b843c749SSergey Zigachev #define PWR_MISC_CNTL_STATUS__PWR_GFX_RLC_CGPG_EN_MASK		0x00000001L
53b843c749SSergey Zigachev #define PWR_MISC_CNTL_STATUS__PWR_GFXOFF_STATUS_MASK		0x00000006L
54b843c749SSergey Zigachev 
55*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_vega10_ce");
56*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_vega10_pfp");
57*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_vega10_me");
58*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_vega10_mec");
59*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_vega10_mec2");
60*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_vega10_rlc");
61b843c749SSergey Zigachev 
62*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_vega12_ce");
63*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_vega12_pfp");
64*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_vega12_me");
65*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_vega12_mec");
66*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_vega12_mec2");
67*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_vega12_rlc");
68b843c749SSergey Zigachev 
69*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_vega20_ce");
70*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_vega20_pfp");
71*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_vega20_me");
72*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_vega20_mec");
73*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_vega20_mec2");
74*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_vega20_rlc");
75b843c749SSergey Zigachev 
76*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_raven_ce");
77*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_raven_pfp");
78*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_raven_me");
79*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_raven_mec");
80*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_raven_mec2");
81*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_raven_rlc");
82b843c749SSergey Zigachev 
83b843c749SSergey Zigachev static const struct soc15_reg_golden golden_settings_gc_9_0[] =
84b843c749SSergey Zigachev {
85b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmDB_DEBUG2, 0xf00fffff, 0x00000400),
86b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmDB_DEBUG3, 0x80000000, 0x80000000),
87b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmGB_GPU_ID, 0x0000000f, 0x00000000),
88b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_BINNER_EVENT_CNTL_3, 0x00000003, 0x82400024),
89b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_ENHANCE, 0x3fffffff, 0x00000001),
90b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_LINE_STIPPLE_STATE, 0x0000ff0f, 0x00000000),
91b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmSH_MEM_CONFIG, 0x00001000, 0x00001000),
92b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmSPI_RESOURCE_RESERVE_CU_0, 0x0007ffff, 0x00000800),
93b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmSPI_RESOURCE_RESERVE_CU_1, 0x0007ffff, 0x00000800),
94b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmSPI_RESOURCE_RESERVE_EN_CU_0, 0x01ffffff, 0x0000ff87),
95b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmSPI_RESOURCE_RESERVE_EN_CU_1, 0x01ffffff, 0x0000ff8f),
96b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmSQC_CONFIG, 0x03000000, 0x020a2000),
97b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmTA_CNTL_AUX, 0xfffffeef, 0x010b0000),
98b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmTCP_CHAN_STEER_HI, 0xffffffff, 0x4a2c0e68),
99b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmTCP_CHAN_STEER_LO, 0xffffffff, 0xb5d3f197),
100b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmVGT_CACHE_INVALIDATION, 0x3fff3af3, 0x19200000),
101b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmVGT_GS_MAX_WAVE_ID, 0x00000fff, 0x000003ff)
102b843c749SSergey Zigachev };
103b843c749SSergey Zigachev 
104b843c749SSergey Zigachev static const struct soc15_reg_golden golden_settings_gc_9_0_vg10[] =
105b843c749SSergey Zigachev {
106b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmCB_HW_CONTROL, 0x0000f000, 0x00012107),
107b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmCB_HW_CONTROL_3, 0x30000000, 0x10000000),
108b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmCPC_UTCL1_CNTL, 0x08000000, 0x08000080),
109b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmCPF_UTCL1_CNTL, 0x08000000, 0x08000080),
110b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmCPG_UTCL1_CNTL, 0x08000000, 0x08000080),
111b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmGB_ADDR_CONFIG, 0xffff77ff, 0x2a114042),
112b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmGB_ADDR_CONFIG_READ, 0xffff77ff, 0x2a114042),
113b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmIA_UTCL1_CNTL, 0x08000000, 0x08000080),
114b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_ENHANCE_1, 0x00008000, 0x00048000),
115b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmRLC_GPM_UTCL1_CNTL_0, 0x08000000, 0x08000080),
116b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmRLC_GPM_UTCL1_CNTL_1, 0x08000000, 0x08000080),
117b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmRLC_GPM_UTCL1_CNTL_2, 0x08000000, 0x08000080),
118b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmRLC_PREWALKER_UTCL1_CNTL, 0x08000000, 0x08000080),
119b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmRLC_SPM_UTCL1_CNTL, 0x08000000, 0x08000080),
120b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmRMI_UTCL1_CNTL2, 0x00030000, 0x00020000),
121b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmSPI_CONFIG_CNTL_1, 0x0000000f, 0x01000107),
122b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmTD_CNTL, 0x00001800, 0x00000800),
123b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmWD_UTCL1_CNTL, 0x08000000, 0x08000080)
124b843c749SSergey Zigachev };
125b843c749SSergey Zigachev 
126b843c749SSergey Zigachev static const struct soc15_reg_golden golden_settings_gc_9_0_vg20[] =
127b843c749SSergey Zigachev {
128b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmCB_DCC_CONFIG, 0x0f000080, 0x04000080),
129b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmCB_HW_CONTROL_2, 0x0f000000, 0x0a000000),
130b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmCB_HW_CONTROL_3, 0x30000000, 0x10000000),
131b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmGB_ADDR_CONFIG, 0xf3e777ff, 0x22014042),
132b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmGB_ADDR_CONFIG_READ, 0xf3e777ff, 0x22014042),
133b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmDB_DEBUG2, 0x00003e00, 0x00000400),
134b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_ENHANCE_1, 0xff840000, 0x04040000),
135b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmRMI_UTCL1_CNTL2, 0x00030000, 0x00030000),
136b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmSPI_CONFIG_CNTL_1, 0xffff010f, 0x01000107),
137b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmTA_CNTL_AUX, 0x000b0000, 0x000b0000),
138b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmTD_CNTL, 0x01000000, 0x01000000)
139b843c749SSergey Zigachev };
140b843c749SSergey Zigachev 
141b843c749SSergey Zigachev static const struct soc15_reg_golden golden_settings_gc_9_1[] =
142b843c749SSergey Zigachev {
143b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmCB_HW_CONTROL, 0xfffdf3cf, 0x00014104),
144b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmCPC_UTCL1_CNTL, 0x08000000, 0x08000080),
145b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmCPF_UTCL1_CNTL, 0x08000000, 0x08000080),
146b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmCPG_UTCL1_CNTL, 0x08000000, 0x08000080),
147b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmDB_DEBUG2, 0xf00fffff, 0x00000420),
148b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmGB_GPU_ID, 0x0000000f, 0x00000000),
149b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmIA_UTCL1_CNTL, 0x08000000, 0x08000080),
150b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_BINNER_EVENT_CNTL_3, 0x00000003, 0x82400024),
151b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_ENHANCE, 0x3fffffff, 0x00000001),
152b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_LINE_STIPPLE_STATE, 0x0000ff0f, 0x00000000),
153b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmRLC_GPM_UTCL1_CNTL_0, 0x08000000, 0x08000080),
154b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmRLC_GPM_UTCL1_CNTL_1, 0x08000000, 0x08000080),
155b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmRLC_GPM_UTCL1_CNTL_2, 0x08000000, 0x08000080),
156b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmRLC_PREWALKER_UTCL1_CNTL, 0x08000000, 0x08000080),
157b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmRLC_SPM_UTCL1_CNTL, 0x08000000, 0x08000080),
158b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmTA_CNTL_AUX, 0xfffffeef, 0x010b0000),
159b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmTCP_CHAN_STEER_HI, 0xffffffff, 0x00000000),
160b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmTCP_CHAN_STEER_LO, 0xffffffff, 0x00003120),
161b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmVGT_CACHE_INVALIDATION, 0x3fff3af3, 0x19200000),
162b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmVGT_GS_MAX_WAVE_ID, 0x00000fff, 0x000000ff),
163b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmWD_UTCL1_CNTL, 0x08000000, 0x08000080)
164b843c749SSergey Zigachev };
165b843c749SSergey Zigachev 
166b843c749SSergey Zigachev static const struct soc15_reg_golden golden_settings_gc_9_1_rv1[] =
167b843c749SSergey Zigachev {
168b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmCB_HW_CONTROL_3, 0x30000000, 0x10000000),
169b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmGB_ADDR_CONFIG, 0xffff77ff, 0x24000042),
170b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmGB_ADDR_CONFIG_READ, 0xffff77ff, 0x24000042),
171b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_ENHANCE_1, 0xffffffff, 0x04048000),
172b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_MODE_CNTL_1, 0x06000000, 0x06000000),
173b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmRMI_UTCL1_CNTL2, 0x00030000, 0x00020000),
174b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmTD_CNTL, 0x01bd9f33, 0x00000800)
175b843c749SSergey Zigachev };
176b843c749SSergey Zigachev 
177b843c749SSergey Zigachev static const struct soc15_reg_golden golden_settings_gc_9_x_common[] =
178b843c749SSergey Zigachev {
179b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmGRBM_CAM_INDEX, 0xffffffff, 0x00000000),
180b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmGRBM_CAM_DATA, 0xffffffff, 0x2544c382)
181b843c749SSergey Zigachev };
182b843c749SSergey Zigachev 
183b843c749SSergey Zigachev static const struct soc15_reg_golden golden_settings_gc_9_2_1[] =
184b843c749SSergey Zigachev {
185b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmDB_DEBUG2, 0xf00fffff, 0x00000420),
186b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmGB_GPU_ID, 0x0000000f, 0x00000000),
187b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_BINNER_EVENT_CNTL_3, 0x00000003, 0x82400024),
188b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_ENHANCE, 0x3fffffff, 0x00000001),
189b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_LINE_STIPPLE_STATE, 0x0000ff0f, 0x00000000),
190b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmSH_MEM_CONFIG, 0x00001000, 0x00001000),
191b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmSPI_RESOURCE_RESERVE_CU_0, 0x0007ffff, 0x00000800),
192b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmSPI_RESOURCE_RESERVE_CU_1, 0x0007ffff, 0x00000800),
193b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmSPI_RESOURCE_RESERVE_EN_CU_0, 0x01ffffff, 0x0000ff87),
194b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmSPI_RESOURCE_RESERVE_EN_CU_1, 0x01ffffff, 0x0000ff8f),
195b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmSQC_CONFIG, 0x03000000, 0x020a2000),
196b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmTA_CNTL_AUX, 0xfffffeef, 0x010b0000),
197b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmTCP_CHAN_STEER_HI, 0xffffffff, 0x4a2c0e68),
198b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmTCP_CHAN_STEER_LO, 0xffffffff, 0xb5d3f197),
199b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmVGT_CACHE_INVALIDATION, 0x3fff3af3, 0x19200000),
200b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmVGT_GS_MAX_WAVE_ID, 0x00000fff, 0x000003ff)
201b843c749SSergey Zigachev };
202b843c749SSergey Zigachev 
203b843c749SSergey Zigachev static const struct soc15_reg_golden golden_settings_gc_9_2_1_vg12[] =
204b843c749SSergey Zigachev {
205b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmCB_DCC_CONFIG, 0x00000080, 0x04000080),
206b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmCB_HW_CONTROL, 0xfffdf3cf, 0x00014104),
207b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmCB_HW_CONTROL_2, 0x0f000000, 0x0a000000),
208b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmGB_ADDR_CONFIG, 0xffff77ff, 0x24104041),
209b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmGB_ADDR_CONFIG_READ, 0xffff77ff, 0x24104041),
210b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_ENHANCE_1, 0xffffffff, 0x04040000),
211b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmSPI_CONFIG_CNTL_1, 0xffff03ff, 0x01000107),
212b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmTCP_CHAN_STEER_HI, 0xffffffff, 0x00000000),
213b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmTCP_CHAN_STEER_LO, 0xffffffff, 0x76325410),
214b843c749SSergey Zigachev 	SOC15_REG_GOLDEN_VALUE(GC, 0, mmTD_CNTL, 0x01bd9f33, 0x01000000)
215b843c749SSergey Zigachev };
216b843c749SSergey Zigachev 
217b843c749SSergey Zigachev static const u32 GFX_RLC_SRM_INDEX_CNTL_ADDR_OFFSETS[] =
218b843c749SSergey Zigachev {
219b843c749SSergey Zigachev 	mmRLC_SRM_INDEX_CNTL_ADDR_0 - mmRLC_SRM_INDEX_CNTL_ADDR_0,
220b843c749SSergey Zigachev 	mmRLC_SRM_INDEX_CNTL_ADDR_1 - mmRLC_SRM_INDEX_CNTL_ADDR_0,
221b843c749SSergey Zigachev 	mmRLC_SRM_INDEX_CNTL_ADDR_2 - mmRLC_SRM_INDEX_CNTL_ADDR_0,
222b843c749SSergey Zigachev 	mmRLC_SRM_INDEX_CNTL_ADDR_3 - mmRLC_SRM_INDEX_CNTL_ADDR_0,
223b843c749SSergey Zigachev 	mmRLC_SRM_INDEX_CNTL_ADDR_4 - mmRLC_SRM_INDEX_CNTL_ADDR_0,
224b843c749SSergey Zigachev 	mmRLC_SRM_INDEX_CNTL_ADDR_5 - mmRLC_SRM_INDEX_CNTL_ADDR_0,
225b843c749SSergey Zigachev 	mmRLC_SRM_INDEX_CNTL_ADDR_6 - mmRLC_SRM_INDEX_CNTL_ADDR_0,
226b843c749SSergey Zigachev 	mmRLC_SRM_INDEX_CNTL_ADDR_7 - mmRLC_SRM_INDEX_CNTL_ADDR_0,
227b843c749SSergey Zigachev };
228b843c749SSergey Zigachev 
229b843c749SSergey Zigachev static const u32 GFX_RLC_SRM_INDEX_CNTL_DATA_OFFSETS[] =
230b843c749SSergey Zigachev {
231b843c749SSergey Zigachev 	mmRLC_SRM_INDEX_CNTL_DATA_0 - mmRLC_SRM_INDEX_CNTL_DATA_0,
232b843c749SSergey Zigachev 	mmRLC_SRM_INDEX_CNTL_DATA_1 - mmRLC_SRM_INDEX_CNTL_DATA_0,
233b843c749SSergey Zigachev 	mmRLC_SRM_INDEX_CNTL_DATA_2 - mmRLC_SRM_INDEX_CNTL_DATA_0,
234b843c749SSergey Zigachev 	mmRLC_SRM_INDEX_CNTL_DATA_3 - mmRLC_SRM_INDEX_CNTL_DATA_0,
235b843c749SSergey Zigachev 	mmRLC_SRM_INDEX_CNTL_DATA_4 - mmRLC_SRM_INDEX_CNTL_DATA_0,
236b843c749SSergey Zigachev 	mmRLC_SRM_INDEX_CNTL_DATA_5 - mmRLC_SRM_INDEX_CNTL_DATA_0,
237b843c749SSergey Zigachev 	mmRLC_SRM_INDEX_CNTL_DATA_6 - mmRLC_SRM_INDEX_CNTL_DATA_0,
238b843c749SSergey Zigachev 	mmRLC_SRM_INDEX_CNTL_DATA_7 - mmRLC_SRM_INDEX_CNTL_DATA_0,
239b843c749SSergey Zigachev };
240b843c749SSergey Zigachev 
241b843c749SSergey Zigachev #define VEGA10_GB_ADDR_CONFIG_GOLDEN 0x2a114042
242b843c749SSergey Zigachev #define VEGA12_GB_ADDR_CONFIG_GOLDEN 0x24104041
243b843c749SSergey Zigachev #define RAVEN_GB_ADDR_CONFIG_GOLDEN 0x24000042
244b843c749SSergey Zigachev 
245b843c749SSergey Zigachev static void gfx_v9_0_set_ring_funcs(struct amdgpu_device *adev);
246b843c749SSergey Zigachev static void gfx_v9_0_set_irq_funcs(struct amdgpu_device *adev);
247b843c749SSergey Zigachev static void gfx_v9_0_set_gds_init(struct amdgpu_device *adev);
248b843c749SSergey Zigachev static void gfx_v9_0_set_rlc_funcs(struct amdgpu_device *adev);
249b843c749SSergey Zigachev static int gfx_v9_0_get_cu_info(struct amdgpu_device *adev,
250b843c749SSergey Zigachev                                  struct amdgpu_cu_info *cu_info);
251b843c749SSergey Zigachev static uint64_t gfx_v9_0_get_gpu_clock_counter(struct amdgpu_device *adev);
252b843c749SSergey Zigachev static void gfx_v9_0_select_se_sh(struct amdgpu_device *adev, u32 se_num, u32 sh_num, u32 instance);
253b843c749SSergey Zigachev static void gfx_v9_0_ring_emit_de_meta(struct amdgpu_ring *ring);
254b843c749SSergey Zigachev 
gfx_v9_0_init_golden_registers(struct amdgpu_device * adev)255b843c749SSergey Zigachev static void gfx_v9_0_init_golden_registers(struct amdgpu_device *adev)
256b843c749SSergey Zigachev {
257b843c749SSergey Zigachev 	switch (adev->asic_type) {
258b843c749SSergey Zigachev 	case CHIP_VEGA10:
259b843c749SSergey Zigachev 		soc15_program_register_sequence(adev,
260b843c749SSergey Zigachev 						 golden_settings_gc_9_0,
261b843c749SSergey Zigachev 						 ARRAY_SIZE(golden_settings_gc_9_0));
262b843c749SSergey Zigachev 		soc15_program_register_sequence(adev,
263b843c749SSergey Zigachev 						 golden_settings_gc_9_0_vg10,
264b843c749SSergey Zigachev 						 ARRAY_SIZE(golden_settings_gc_9_0_vg10));
265b843c749SSergey Zigachev 		break;
266b843c749SSergey Zigachev 	case CHIP_VEGA12:
267b843c749SSergey Zigachev 		soc15_program_register_sequence(adev,
268b843c749SSergey Zigachev 						golden_settings_gc_9_2_1,
269b843c749SSergey Zigachev 						ARRAY_SIZE(golden_settings_gc_9_2_1));
270b843c749SSergey Zigachev 		soc15_program_register_sequence(adev,
271b843c749SSergey Zigachev 						golden_settings_gc_9_2_1_vg12,
272b843c749SSergey Zigachev 						ARRAY_SIZE(golden_settings_gc_9_2_1_vg12));
273b843c749SSergey Zigachev 		break;
274b843c749SSergey Zigachev 	case CHIP_VEGA20:
275b843c749SSergey Zigachev 		soc15_program_register_sequence(adev,
276b843c749SSergey Zigachev 						golden_settings_gc_9_0,
277b843c749SSergey Zigachev 						ARRAY_SIZE(golden_settings_gc_9_0));
278b843c749SSergey Zigachev 		soc15_program_register_sequence(adev,
279b843c749SSergey Zigachev 						golden_settings_gc_9_0_vg20,
280b843c749SSergey Zigachev 						ARRAY_SIZE(golden_settings_gc_9_0_vg20));
281b843c749SSergey Zigachev 		break;
282b843c749SSergey Zigachev 	case CHIP_RAVEN:
283b843c749SSergey Zigachev 		soc15_program_register_sequence(adev,
284b843c749SSergey Zigachev 						 golden_settings_gc_9_1,
285b843c749SSergey Zigachev 						 ARRAY_SIZE(golden_settings_gc_9_1));
286b843c749SSergey Zigachev 		soc15_program_register_sequence(adev,
287b843c749SSergey Zigachev 						 golden_settings_gc_9_1_rv1,
288b843c749SSergey Zigachev 						 ARRAY_SIZE(golden_settings_gc_9_1_rv1));
289b843c749SSergey Zigachev 		break;
290b843c749SSergey Zigachev 	default:
291b843c749SSergey Zigachev 		break;
292b843c749SSergey Zigachev 	}
293b843c749SSergey Zigachev 
294b843c749SSergey Zigachev 	soc15_program_register_sequence(adev, golden_settings_gc_9_x_common,
295b843c749SSergey Zigachev 					(const u32)ARRAY_SIZE(golden_settings_gc_9_x_common));
296b843c749SSergey Zigachev }
297b843c749SSergey Zigachev 
gfx_v9_0_scratch_init(struct amdgpu_device * adev)298b843c749SSergey Zigachev static void gfx_v9_0_scratch_init(struct amdgpu_device *adev)
299b843c749SSergey Zigachev {
300b843c749SSergey Zigachev 	adev->gfx.scratch.num_reg = 8;
301b843c749SSergey Zigachev 	adev->gfx.scratch.reg_base = SOC15_REG_OFFSET(GC, 0, mmSCRATCH_REG0);
302b843c749SSergey Zigachev 	adev->gfx.scratch.free_mask = (1u << adev->gfx.scratch.num_reg) - 1;
303b843c749SSergey Zigachev }
304b843c749SSergey Zigachev 
gfx_v9_0_write_data_to_reg(struct amdgpu_ring * ring,int eng_sel,bool wc,uint32_t reg,uint32_t val)305b843c749SSergey Zigachev static void gfx_v9_0_write_data_to_reg(struct amdgpu_ring *ring, int eng_sel,
306b843c749SSergey Zigachev 				       bool wc, uint32_t reg, uint32_t val)
307b843c749SSergey Zigachev {
308b843c749SSergey Zigachev 	amdgpu_ring_write(ring, PACKET3(PACKET3_WRITE_DATA, 3));
309b843c749SSergey Zigachev 	amdgpu_ring_write(ring, WRITE_DATA_ENGINE_SEL(eng_sel) |
310b843c749SSergey Zigachev 				WRITE_DATA_DST_SEL(0) |
311b843c749SSergey Zigachev 				(wc ? WR_CONFIRM : 0));
312b843c749SSergey Zigachev 	amdgpu_ring_write(ring, reg);
313b843c749SSergey Zigachev 	amdgpu_ring_write(ring, 0);
314b843c749SSergey Zigachev 	amdgpu_ring_write(ring, val);
315b843c749SSergey Zigachev }
316b843c749SSergey Zigachev 
gfx_v9_0_wait_reg_mem(struct amdgpu_ring * ring,int eng_sel,int mem_space,int opt,uint32_t addr0,uint32_t addr1,uint32_t ref,uint32_t mask,uint32_t inv)317b843c749SSergey Zigachev static void gfx_v9_0_wait_reg_mem(struct amdgpu_ring *ring, int eng_sel,
318b843c749SSergey Zigachev 				  int mem_space, int opt, uint32_t addr0,
319b843c749SSergey Zigachev 				  uint32_t addr1, uint32_t ref, uint32_t mask,
320b843c749SSergey Zigachev 				  uint32_t inv)
321b843c749SSergey Zigachev {
322b843c749SSergey Zigachev 	amdgpu_ring_write(ring, PACKET3(PACKET3_WAIT_REG_MEM, 5));
323b843c749SSergey Zigachev 	amdgpu_ring_write(ring,
324b843c749SSergey Zigachev 				 /* memory (1) or register (0) */
325b843c749SSergey Zigachev 				 (WAIT_REG_MEM_MEM_SPACE(mem_space) |
326b843c749SSergey Zigachev 				 WAIT_REG_MEM_OPERATION(opt) | /* wait */
327b843c749SSergey Zigachev 				 WAIT_REG_MEM_FUNCTION(3) |  /* equal */
328b843c749SSergey Zigachev 				 WAIT_REG_MEM_ENGINE(eng_sel)));
329b843c749SSergey Zigachev 
330b843c749SSergey Zigachev 	if (mem_space)
331b843c749SSergey Zigachev 		BUG_ON(addr0 & 0x3); /* Dword align */
332b843c749SSergey Zigachev 	amdgpu_ring_write(ring, addr0);
333b843c749SSergey Zigachev 	amdgpu_ring_write(ring, addr1);
334b843c749SSergey Zigachev 	amdgpu_ring_write(ring, ref);
335b843c749SSergey Zigachev 	amdgpu_ring_write(ring, mask);
336b843c749SSergey Zigachev 	amdgpu_ring_write(ring, inv); /* poll interval */
337b843c749SSergey Zigachev }
338b843c749SSergey Zigachev 
gfx_v9_0_ring_test_ring(struct amdgpu_ring * ring)339b843c749SSergey Zigachev static int gfx_v9_0_ring_test_ring(struct amdgpu_ring *ring)
340b843c749SSergey Zigachev {
341b843c749SSergey Zigachev 	struct amdgpu_device *adev = ring->adev;
342b843c749SSergey Zigachev 	uint32_t scratch;
343b843c749SSergey Zigachev 	uint32_t tmp = 0;
344b843c749SSergey Zigachev 	unsigned i;
345b843c749SSergey Zigachev 	int r;
346b843c749SSergey Zigachev 
347b843c749SSergey Zigachev 	r = amdgpu_gfx_scratch_get(adev, &scratch);
348b843c749SSergey Zigachev 	if (r) {
349b843c749SSergey Zigachev 		DRM_ERROR("amdgpu: cp failed to get scratch reg (%d).\n", r);
350b843c749SSergey Zigachev 		return r;
351b843c749SSergey Zigachev 	}
352b843c749SSergey Zigachev 	WREG32(scratch, 0xCAFEDEAD);
353b843c749SSergey Zigachev 	r = amdgpu_ring_alloc(ring, 3);
354b843c749SSergey Zigachev 	if (r) {
355b843c749SSergey Zigachev 		DRM_ERROR("amdgpu: cp failed to lock ring %d (%d).\n",
356b843c749SSergey Zigachev 			  ring->idx, r);
357b843c749SSergey Zigachev 		amdgpu_gfx_scratch_free(adev, scratch);
358b843c749SSergey Zigachev 		return r;
359b843c749SSergey Zigachev 	}
360b843c749SSergey Zigachev 	amdgpu_ring_write(ring, PACKET3(PACKET3_SET_UCONFIG_REG, 1));
361b843c749SSergey Zigachev 	amdgpu_ring_write(ring, (scratch - PACKET3_SET_UCONFIG_REG_START));
362b843c749SSergey Zigachev 	amdgpu_ring_write(ring, 0xDEADBEEF);
363b843c749SSergey Zigachev 	amdgpu_ring_commit(ring);
364b843c749SSergey Zigachev 
365b843c749SSergey Zigachev 	for (i = 0; i < adev->usec_timeout; i++) {
366b843c749SSergey Zigachev 		tmp = RREG32(scratch);
367b843c749SSergey Zigachev 		if (tmp == 0xDEADBEEF)
368b843c749SSergey Zigachev 			break;
369b843c749SSergey Zigachev 		DRM_UDELAY(1);
370b843c749SSergey Zigachev 	}
371b843c749SSergey Zigachev 	if (i < adev->usec_timeout) {
372b843c749SSergey Zigachev 		DRM_DEBUG("ring test on %d succeeded in %d usecs\n",
373b843c749SSergey Zigachev 			 ring->idx, i);
374b843c749SSergey Zigachev 	} else {
375b843c749SSergey Zigachev 		DRM_ERROR("amdgpu: ring %d test failed (scratch(0x%04X)=0x%08X)\n",
376b843c749SSergey Zigachev 			  ring->idx, scratch, tmp);
377b843c749SSergey Zigachev 		r = -EINVAL;
378b843c749SSergey Zigachev 	}
379b843c749SSergey Zigachev 	amdgpu_gfx_scratch_free(adev, scratch);
380b843c749SSergey Zigachev 	return r;
381b843c749SSergey Zigachev }
382b843c749SSergey Zigachev 
gfx_v9_0_ring_test_ib(struct amdgpu_ring * ring,long timeout)383b843c749SSergey Zigachev static int gfx_v9_0_ring_test_ib(struct amdgpu_ring *ring, long timeout)
384b843c749SSergey Zigachev {
385b843c749SSergey Zigachev 	struct amdgpu_device *adev = ring->adev;
386b843c749SSergey Zigachev 	struct amdgpu_ib ib;
387b843c749SSergey Zigachev 	struct dma_fence *f = NULL;
388b843c749SSergey Zigachev 
389b843c749SSergey Zigachev 	unsigned index;
390b843c749SSergey Zigachev 	uint64_t gpu_addr;
391b843c749SSergey Zigachev 	uint32_t tmp;
392b843c749SSergey Zigachev 	long r;
393b843c749SSergey Zigachev 
394b843c749SSergey Zigachev 	r = amdgpu_device_wb_get(adev, &index);
395b843c749SSergey Zigachev 	if (r) {
396b843c749SSergey Zigachev 		dev_err(adev->dev, "(%ld) failed to allocate wb slot\n", r);
397b843c749SSergey Zigachev 		return r;
398b843c749SSergey Zigachev 	}
399b843c749SSergey Zigachev 
400b843c749SSergey Zigachev 	gpu_addr = adev->wb.gpu_addr + (index * 4);
401b843c749SSergey Zigachev 	adev->wb.wb[index] = cpu_to_le32(0xCAFEDEAD);
402b843c749SSergey Zigachev 	memset(&ib, 0, sizeof(ib));
403b843c749SSergey Zigachev 	r = amdgpu_ib_get(adev, NULL, 16, &ib);
404b843c749SSergey Zigachev 	if (r) {
405b843c749SSergey Zigachev 		DRM_ERROR("amdgpu: failed to get ib (%ld).\n", r);
406b843c749SSergey Zigachev 		goto err1;
407b843c749SSergey Zigachev 	}
408b843c749SSergey Zigachev 	ib.ptr[0] = PACKET3(PACKET3_WRITE_DATA, 3);
409b843c749SSergey Zigachev 	ib.ptr[1] = WRITE_DATA_DST_SEL(5) | WR_CONFIRM;
410b843c749SSergey Zigachev 	ib.ptr[2] = lower_32_bits(gpu_addr);
411b843c749SSergey Zigachev 	ib.ptr[3] = upper_32_bits(gpu_addr);
412b843c749SSergey Zigachev 	ib.ptr[4] = 0xDEADBEEF;
413b843c749SSergey Zigachev 	ib.length_dw = 5;
414b843c749SSergey Zigachev 
415b843c749SSergey Zigachev 	r = amdgpu_ib_schedule(ring, 1, &ib, NULL, &f);
416b843c749SSergey Zigachev 	if (r)
417b843c749SSergey Zigachev 		goto err2;
418b843c749SSergey Zigachev 
419b843c749SSergey Zigachev 	r = dma_fence_wait_timeout(f, false, timeout);
420b843c749SSergey Zigachev 	if (r == 0) {
421b843c749SSergey Zigachev 			DRM_ERROR("amdgpu: IB test timed out.\n");
422b843c749SSergey Zigachev 			r = -ETIMEDOUT;
423b843c749SSergey Zigachev 			goto err2;
424b843c749SSergey Zigachev 	} else if (r < 0) {
425b843c749SSergey Zigachev 			DRM_ERROR("amdgpu: fence wait failed (%ld).\n", r);
426b843c749SSergey Zigachev 			goto err2;
427b843c749SSergey Zigachev 	}
428b843c749SSergey Zigachev 
429b843c749SSergey Zigachev 	tmp = adev->wb.wb[index];
430b843c749SSergey Zigachev 	if (tmp == 0xDEADBEEF) {
431b843c749SSergey Zigachev 			DRM_DEBUG("ib test on ring %d succeeded\n", ring->idx);
432b843c749SSergey Zigachev 			r = 0;
433b843c749SSergey Zigachev 	} else {
434b843c749SSergey Zigachev 			DRM_ERROR("ib test on ring %d failed\n", ring->idx);
435b843c749SSergey Zigachev 			r = -EINVAL;
436b843c749SSergey Zigachev 	}
437b843c749SSergey Zigachev 
438b843c749SSergey Zigachev err2:
439b843c749SSergey Zigachev 	amdgpu_ib_free(adev, &ib, NULL);
440b843c749SSergey Zigachev 	dma_fence_put(f);
441b843c749SSergey Zigachev err1:
442b843c749SSergey Zigachev 	amdgpu_device_wb_free(adev, index);
443b843c749SSergey Zigachev 	return r;
444b843c749SSergey Zigachev }
445b843c749SSergey Zigachev 
446b843c749SSergey Zigachev 
gfx_v9_0_free_microcode(struct amdgpu_device * adev)447b843c749SSergey Zigachev static void gfx_v9_0_free_microcode(struct amdgpu_device *adev)
448b843c749SSergey Zigachev {
449b843c749SSergey Zigachev 	release_firmware(adev->gfx.pfp_fw);
450b843c749SSergey Zigachev 	adev->gfx.pfp_fw = NULL;
451b843c749SSergey Zigachev 	release_firmware(adev->gfx.me_fw);
452b843c749SSergey Zigachev 	adev->gfx.me_fw = NULL;
453b843c749SSergey Zigachev 	release_firmware(adev->gfx.ce_fw);
454b843c749SSergey Zigachev 	adev->gfx.ce_fw = NULL;
455b843c749SSergey Zigachev 	release_firmware(adev->gfx.rlc_fw);
456b843c749SSergey Zigachev 	adev->gfx.rlc_fw = NULL;
457b843c749SSergey Zigachev 	release_firmware(adev->gfx.mec_fw);
458b843c749SSergey Zigachev 	adev->gfx.mec_fw = NULL;
459b843c749SSergey Zigachev 	release_firmware(adev->gfx.mec2_fw);
460b843c749SSergey Zigachev 	adev->gfx.mec2_fw = NULL;
461b843c749SSergey Zigachev 
462b843c749SSergey Zigachev 	kfree(adev->gfx.rlc.register_list_format);
463b843c749SSergey Zigachev }
464b843c749SSergey Zigachev 
gfx_v9_0_init_rlc_ext_microcode(struct amdgpu_device * adev)465b843c749SSergey Zigachev static void gfx_v9_0_init_rlc_ext_microcode(struct amdgpu_device *adev)
466b843c749SSergey Zigachev {
467b843c749SSergey Zigachev 	const struct rlc_firmware_header_v2_1 *rlc_hdr;
468b843c749SSergey Zigachev 
469b843c749SSergey Zigachev 	rlc_hdr = (const struct rlc_firmware_header_v2_1 *)adev->gfx.rlc_fw->data;
470b843c749SSergey Zigachev 	adev->gfx.rlc_srlc_fw_version = le32_to_cpu(rlc_hdr->save_restore_list_cntl_ucode_ver);
471b843c749SSergey Zigachev 	adev->gfx.rlc_srlc_feature_version = le32_to_cpu(rlc_hdr->save_restore_list_cntl_feature_ver);
472b843c749SSergey Zigachev 	adev->gfx.rlc.save_restore_list_cntl_size_bytes = le32_to_cpu(rlc_hdr->save_restore_list_cntl_size_bytes);
473b843c749SSergey Zigachev 	adev->gfx.rlc.save_restore_list_cntl = (u8 *)rlc_hdr + le32_to_cpu(rlc_hdr->save_restore_list_cntl_offset_bytes);
474b843c749SSergey Zigachev 	adev->gfx.rlc_srlg_fw_version = le32_to_cpu(rlc_hdr->save_restore_list_gpm_ucode_ver);
475b843c749SSergey Zigachev 	adev->gfx.rlc_srlg_feature_version = le32_to_cpu(rlc_hdr->save_restore_list_gpm_feature_ver);
476b843c749SSergey Zigachev 	adev->gfx.rlc.save_restore_list_gpm_size_bytes = le32_to_cpu(rlc_hdr->save_restore_list_gpm_size_bytes);
477b843c749SSergey Zigachev 	adev->gfx.rlc.save_restore_list_gpm = (u8 *)rlc_hdr + le32_to_cpu(rlc_hdr->save_restore_list_gpm_offset_bytes);
478b843c749SSergey Zigachev 	adev->gfx.rlc_srls_fw_version = le32_to_cpu(rlc_hdr->save_restore_list_srm_ucode_ver);
479b843c749SSergey Zigachev 	adev->gfx.rlc_srls_feature_version = le32_to_cpu(rlc_hdr->save_restore_list_srm_feature_ver);
480b843c749SSergey Zigachev 	adev->gfx.rlc.save_restore_list_srm_size_bytes = le32_to_cpu(rlc_hdr->save_restore_list_srm_size_bytes);
481b843c749SSergey Zigachev 	adev->gfx.rlc.save_restore_list_srm = (u8 *)rlc_hdr + le32_to_cpu(rlc_hdr->save_restore_list_srm_offset_bytes);
482b843c749SSergey Zigachev 	adev->gfx.rlc.reg_list_format_direct_reg_list_length =
483b843c749SSergey Zigachev 			le32_to_cpu(rlc_hdr->reg_list_format_direct_reg_list_length);
484b843c749SSergey Zigachev }
485b843c749SSergey Zigachev 
gfx_v9_0_init_microcode(struct amdgpu_device * adev)486b843c749SSergey Zigachev static int gfx_v9_0_init_microcode(struct amdgpu_device *adev)
487b843c749SSergey Zigachev {
488b843c749SSergey Zigachev 	const char *chip_name;
489b843c749SSergey Zigachev 	char fw_name[30];
490b843c749SSergey Zigachev 	int err;
491b843c749SSergey Zigachev 	struct amdgpu_firmware_info *info = NULL;
492b843c749SSergey Zigachev 	const struct common_firmware_header *header = NULL;
493b843c749SSergey Zigachev 	const struct gfx_firmware_header_v1_0 *cp_hdr;
494b843c749SSergey Zigachev 	const struct rlc_firmware_header_v2_0 *rlc_hdr;
495b843c749SSergey Zigachev 	unsigned int *tmp = NULL;
496b843c749SSergey Zigachev 	unsigned int i = 0;
497b843c749SSergey Zigachev 	uint16_t version_major;
498b843c749SSergey Zigachev 	uint16_t version_minor;
499b843c749SSergey Zigachev 
500b843c749SSergey Zigachev 	DRM_DEBUG("\n");
501b843c749SSergey Zigachev 
502b843c749SSergey Zigachev 	switch (adev->asic_type) {
503b843c749SSergey Zigachev 	case CHIP_VEGA10:
504b843c749SSergey Zigachev 		chip_name = "vega10";
505b843c749SSergey Zigachev 		break;
506b843c749SSergey Zigachev 	case CHIP_VEGA12:
507b843c749SSergey Zigachev 		chip_name = "vega12";
508b843c749SSergey Zigachev 		break;
509b843c749SSergey Zigachev 	case CHIP_VEGA20:
510b843c749SSergey Zigachev 		chip_name = "vega20";
511b843c749SSergey Zigachev 		break;
512b843c749SSergey Zigachev 	case CHIP_RAVEN:
513b843c749SSergey Zigachev 		chip_name = "raven";
514b843c749SSergey Zigachev 		break;
515b843c749SSergey Zigachev 	default:
516b843c749SSergey Zigachev 		BUG();
517b843c749SSergey Zigachev 	}
518b843c749SSergey Zigachev 
519*809f3802SSergey Zigachev 	snprintf(fw_name, sizeof(fw_name), "amdgpufw_%s_pfp", chip_name);
520b843c749SSergey Zigachev 	err = request_firmware(&adev->gfx.pfp_fw, fw_name, adev->dev);
521b843c749SSergey Zigachev 	if (err)
522b843c749SSergey Zigachev 		goto out;
523b843c749SSergey Zigachev 	err = amdgpu_ucode_validate(adev->gfx.pfp_fw);
524b843c749SSergey Zigachev 	if (err)
525b843c749SSergey Zigachev 		goto out;
526b843c749SSergey Zigachev 	cp_hdr = (const struct gfx_firmware_header_v1_0 *)adev->gfx.pfp_fw->data;
527b843c749SSergey Zigachev 	adev->gfx.pfp_fw_version = le32_to_cpu(cp_hdr->header.ucode_version);
528b843c749SSergey Zigachev 	adev->gfx.pfp_feature_version = le32_to_cpu(cp_hdr->ucode_feature_version);
529b843c749SSergey Zigachev 
530*809f3802SSergey Zigachev 	snprintf(fw_name, sizeof(fw_name), "amdgpufw_%s_me", chip_name);
531b843c749SSergey Zigachev 	err = request_firmware(&adev->gfx.me_fw, fw_name, adev->dev);
532b843c749SSergey Zigachev 	if (err)
533b843c749SSergey Zigachev 		goto out;
534b843c749SSergey Zigachev 	err = amdgpu_ucode_validate(adev->gfx.me_fw);
535b843c749SSergey Zigachev 	if (err)
536b843c749SSergey Zigachev 		goto out;
537b843c749SSergey Zigachev 	cp_hdr = (const struct gfx_firmware_header_v1_0 *)adev->gfx.me_fw->data;
538b843c749SSergey Zigachev 	adev->gfx.me_fw_version = le32_to_cpu(cp_hdr->header.ucode_version);
539b843c749SSergey Zigachev 	adev->gfx.me_feature_version = le32_to_cpu(cp_hdr->ucode_feature_version);
540b843c749SSergey Zigachev 
541*809f3802SSergey Zigachev 	snprintf(fw_name, sizeof(fw_name), "amdgpufw_%s_ce", chip_name);
542b843c749SSergey Zigachev 	err = request_firmware(&adev->gfx.ce_fw, fw_name, adev->dev);
543b843c749SSergey Zigachev 	if (err)
544b843c749SSergey Zigachev 		goto out;
545b843c749SSergey Zigachev 	err = amdgpu_ucode_validate(adev->gfx.ce_fw);
546b843c749SSergey Zigachev 	if (err)
547b843c749SSergey Zigachev 		goto out;
548b843c749SSergey Zigachev 	cp_hdr = (const struct gfx_firmware_header_v1_0 *)adev->gfx.ce_fw->data;
549b843c749SSergey Zigachev 	adev->gfx.ce_fw_version = le32_to_cpu(cp_hdr->header.ucode_version);
550b843c749SSergey Zigachev 	adev->gfx.ce_feature_version = le32_to_cpu(cp_hdr->ucode_feature_version);
551b843c749SSergey Zigachev 
552*809f3802SSergey Zigachev 	snprintf(fw_name, sizeof(fw_name), "amdgpufw_%s_rlc", chip_name);
553b843c749SSergey Zigachev 	err = request_firmware(&adev->gfx.rlc_fw, fw_name, adev->dev);
554b843c749SSergey Zigachev 	if (err)
555b843c749SSergey Zigachev 		goto out;
556b843c749SSergey Zigachev 	err = amdgpu_ucode_validate(adev->gfx.rlc_fw);
557b843c749SSergey Zigachev 	rlc_hdr = (const struct rlc_firmware_header_v2_0 *)adev->gfx.rlc_fw->data;
558b843c749SSergey Zigachev 
559b843c749SSergey Zigachev 	version_major = le16_to_cpu(rlc_hdr->header.header_version_major);
560b843c749SSergey Zigachev 	version_minor = le16_to_cpu(rlc_hdr->header.header_version_minor);
561b843c749SSergey Zigachev 	if (version_major == 2 && version_minor == 1)
562b843c749SSergey Zigachev 		adev->gfx.rlc.is_rlc_v2_1 = true;
563b843c749SSergey Zigachev 
564b843c749SSergey Zigachev 	adev->gfx.rlc_fw_version = le32_to_cpu(rlc_hdr->header.ucode_version);
565b843c749SSergey Zigachev 	adev->gfx.rlc_feature_version = le32_to_cpu(rlc_hdr->ucode_feature_version);
566b843c749SSergey Zigachev 	adev->gfx.rlc.save_and_restore_offset =
567b843c749SSergey Zigachev 			le32_to_cpu(rlc_hdr->save_and_restore_offset);
568b843c749SSergey Zigachev 	adev->gfx.rlc.clear_state_descriptor_offset =
569b843c749SSergey Zigachev 			le32_to_cpu(rlc_hdr->clear_state_descriptor_offset);
570b843c749SSergey Zigachev 	adev->gfx.rlc.avail_scratch_ram_locations =
571b843c749SSergey Zigachev 			le32_to_cpu(rlc_hdr->avail_scratch_ram_locations);
572b843c749SSergey Zigachev 	adev->gfx.rlc.reg_restore_list_size =
573b843c749SSergey Zigachev 			le32_to_cpu(rlc_hdr->reg_restore_list_size);
574b843c749SSergey Zigachev 	adev->gfx.rlc.reg_list_format_start =
575b843c749SSergey Zigachev 			le32_to_cpu(rlc_hdr->reg_list_format_start);
576b843c749SSergey Zigachev 	adev->gfx.rlc.reg_list_format_separate_start =
577b843c749SSergey Zigachev 			le32_to_cpu(rlc_hdr->reg_list_format_separate_start);
578b843c749SSergey Zigachev 	adev->gfx.rlc.starting_offsets_start =
579b843c749SSergey Zigachev 			le32_to_cpu(rlc_hdr->starting_offsets_start);
580b843c749SSergey Zigachev 	adev->gfx.rlc.reg_list_format_size_bytes =
581b843c749SSergey Zigachev 			le32_to_cpu(rlc_hdr->reg_list_format_size_bytes);
582b843c749SSergey Zigachev 	adev->gfx.rlc.reg_list_size_bytes =
583b843c749SSergey Zigachev 			le32_to_cpu(rlc_hdr->reg_list_size_bytes);
584b843c749SSergey Zigachev 	adev->gfx.rlc.register_list_format =
585b843c749SSergey Zigachev 			kmalloc(adev->gfx.rlc.reg_list_format_size_bytes +
58678973132SSergey Zigachev 				adev->gfx.rlc.reg_list_size_bytes, M_DRM, GFP_KERNEL);
587b843c749SSergey Zigachev 	if (!adev->gfx.rlc.register_list_format) {
588b843c749SSergey Zigachev 		err = -ENOMEM;
589b843c749SSergey Zigachev 		goto out;
590b843c749SSergey Zigachev 	}
591b843c749SSergey Zigachev 
592b843c749SSergey Zigachev 	tmp = (unsigned int *)((uintptr_t)rlc_hdr +
593b843c749SSergey Zigachev 			le32_to_cpu(rlc_hdr->reg_list_format_array_offset_bytes));
594b843c749SSergey Zigachev 	for (i = 0 ; i < (rlc_hdr->reg_list_format_size_bytes >> 2); i++)
595b843c749SSergey Zigachev 		adev->gfx.rlc.register_list_format[i] =	le32_to_cpu(tmp[i]);
596b843c749SSergey Zigachev 
597b843c749SSergey Zigachev 	adev->gfx.rlc.register_restore = adev->gfx.rlc.register_list_format + i;
598b843c749SSergey Zigachev 
599b843c749SSergey Zigachev 	tmp = (unsigned int *)((uintptr_t)rlc_hdr +
600b843c749SSergey Zigachev 			le32_to_cpu(rlc_hdr->reg_list_array_offset_bytes));
601b843c749SSergey Zigachev 	for (i = 0 ; i < (rlc_hdr->reg_list_size_bytes >> 2); i++)
602b843c749SSergey Zigachev 		adev->gfx.rlc.register_restore[i] = le32_to_cpu(tmp[i]);
603b843c749SSergey Zigachev 
604b843c749SSergey Zigachev 	if (adev->gfx.rlc.is_rlc_v2_1)
605b843c749SSergey Zigachev 		gfx_v9_0_init_rlc_ext_microcode(adev);
606b843c749SSergey Zigachev 
607*809f3802SSergey Zigachev 	snprintf(fw_name, sizeof(fw_name), "amdgpufw_%s_mec", chip_name);
608b843c749SSergey Zigachev 	err = request_firmware(&adev->gfx.mec_fw, fw_name, adev->dev);
609b843c749SSergey Zigachev 	if (err)
610b843c749SSergey Zigachev 		goto out;
611b843c749SSergey Zigachev 	err = amdgpu_ucode_validate(adev->gfx.mec_fw);
612b843c749SSergey Zigachev 	if (err)
613b843c749SSergey Zigachev 		goto out;
614b843c749SSergey Zigachev 	cp_hdr = (const struct gfx_firmware_header_v1_0 *)adev->gfx.mec_fw->data;
615b843c749SSergey Zigachev 	adev->gfx.mec_fw_version = le32_to_cpu(cp_hdr->header.ucode_version);
616b843c749SSergey Zigachev 	adev->gfx.mec_feature_version = le32_to_cpu(cp_hdr->ucode_feature_version);
617b843c749SSergey Zigachev 
618b843c749SSergey Zigachev 
619*809f3802SSergey Zigachev 	snprintf(fw_name, sizeof(fw_name), "amdgpufw_%s_mec2", chip_name);
620b843c749SSergey Zigachev 	err = request_firmware(&adev->gfx.mec2_fw, fw_name, adev->dev);
621b843c749SSergey Zigachev 	if (!err) {
622b843c749SSergey Zigachev 		err = amdgpu_ucode_validate(adev->gfx.mec2_fw);
623b843c749SSergey Zigachev 		if (err)
624b843c749SSergey Zigachev 			goto out;
625b843c749SSergey Zigachev 		cp_hdr = (const struct gfx_firmware_header_v1_0 *)
626b843c749SSergey Zigachev 		adev->gfx.mec2_fw->data;
627b843c749SSergey Zigachev 		adev->gfx.mec2_fw_version =
628b843c749SSergey Zigachev 		le32_to_cpu(cp_hdr->header.ucode_version);
629b843c749SSergey Zigachev 		adev->gfx.mec2_feature_version =
630b843c749SSergey Zigachev 		le32_to_cpu(cp_hdr->ucode_feature_version);
631b843c749SSergey Zigachev 	} else {
632b843c749SSergey Zigachev 		err = 0;
633b843c749SSergey Zigachev 		adev->gfx.mec2_fw = NULL;
634b843c749SSergey Zigachev 	}
635b843c749SSergey Zigachev 
636b843c749SSergey Zigachev 	if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
637b843c749SSergey Zigachev 		info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CP_PFP];
638b843c749SSergey Zigachev 		info->ucode_id = AMDGPU_UCODE_ID_CP_PFP;
639b843c749SSergey Zigachev 		info->fw = adev->gfx.pfp_fw;
640b843c749SSergey Zigachev 		header = (const struct common_firmware_header *)info->fw->data;
641b843c749SSergey Zigachev 		adev->firmware.fw_size +=
642b843c749SSergey Zigachev 			ALIGN(le32_to_cpu(header->ucode_size_bytes), PAGE_SIZE);
643b843c749SSergey Zigachev 
644b843c749SSergey Zigachev 		info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CP_ME];
645b843c749SSergey Zigachev 		info->ucode_id = AMDGPU_UCODE_ID_CP_ME;
646b843c749SSergey Zigachev 		info->fw = adev->gfx.me_fw;
647b843c749SSergey Zigachev 		header = (const struct common_firmware_header *)info->fw->data;
648b843c749SSergey Zigachev 		adev->firmware.fw_size +=
649b843c749SSergey Zigachev 			ALIGN(le32_to_cpu(header->ucode_size_bytes), PAGE_SIZE);
650b843c749SSergey Zigachev 
651b843c749SSergey Zigachev 		info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CP_CE];
652b843c749SSergey Zigachev 		info->ucode_id = AMDGPU_UCODE_ID_CP_CE;
653b843c749SSergey Zigachev 		info->fw = adev->gfx.ce_fw;
654b843c749SSergey Zigachev 		header = (const struct common_firmware_header *)info->fw->data;
655b843c749SSergey Zigachev 		adev->firmware.fw_size +=
656b843c749SSergey Zigachev 			ALIGN(le32_to_cpu(header->ucode_size_bytes), PAGE_SIZE);
657b843c749SSergey Zigachev 
658b843c749SSergey Zigachev 		info = &adev->firmware.ucode[AMDGPU_UCODE_ID_RLC_G];
659b843c749SSergey Zigachev 		info->ucode_id = AMDGPU_UCODE_ID_RLC_G;
660b843c749SSergey Zigachev 		info->fw = adev->gfx.rlc_fw;
661b843c749SSergey Zigachev 		header = (const struct common_firmware_header *)info->fw->data;
662b843c749SSergey Zigachev 		adev->firmware.fw_size +=
663b843c749SSergey Zigachev 			ALIGN(le32_to_cpu(header->ucode_size_bytes), PAGE_SIZE);
664b843c749SSergey Zigachev 
665b843c749SSergey Zigachev 		if (adev->gfx.rlc.is_rlc_v2_1 &&
666b843c749SSergey Zigachev 		    adev->gfx.rlc.save_restore_list_cntl_size_bytes &&
667b843c749SSergey Zigachev 		    adev->gfx.rlc.save_restore_list_gpm_size_bytes &&
668b843c749SSergey Zigachev 		    adev->gfx.rlc.save_restore_list_srm_size_bytes) {
669b843c749SSergey Zigachev 			info = &adev->firmware.ucode[AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL];
670b843c749SSergey Zigachev 			info->ucode_id = AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL;
671b843c749SSergey Zigachev 			info->fw = adev->gfx.rlc_fw;
672b843c749SSergey Zigachev 			adev->firmware.fw_size +=
673b843c749SSergey Zigachev 				ALIGN(adev->gfx.rlc.save_restore_list_cntl_size_bytes, PAGE_SIZE);
674b843c749SSergey Zigachev 
675b843c749SSergey Zigachev 			info = &adev->firmware.ucode[AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM];
676b843c749SSergey Zigachev 			info->ucode_id = AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM;
677b843c749SSergey Zigachev 			info->fw = adev->gfx.rlc_fw;
678b843c749SSergey Zigachev 			adev->firmware.fw_size +=
679b843c749SSergey Zigachev 				ALIGN(adev->gfx.rlc.save_restore_list_gpm_size_bytes, PAGE_SIZE);
680b843c749SSergey Zigachev 
681b843c749SSergey Zigachev 			info = &adev->firmware.ucode[AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM];
682b843c749SSergey Zigachev 			info->ucode_id = AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM;
683b843c749SSergey Zigachev 			info->fw = adev->gfx.rlc_fw;
684b843c749SSergey Zigachev 			adev->firmware.fw_size +=
685b843c749SSergey Zigachev 				ALIGN(adev->gfx.rlc.save_restore_list_srm_size_bytes, PAGE_SIZE);
686b843c749SSergey Zigachev 		}
687b843c749SSergey Zigachev 
688b843c749SSergey Zigachev 		info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CP_MEC1];
689b843c749SSergey Zigachev 		info->ucode_id = AMDGPU_UCODE_ID_CP_MEC1;
690b843c749SSergey Zigachev 		info->fw = adev->gfx.mec_fw;
691b843c749SSergey Zigachev 		header = (const struct common_firmware_header *)info->fw->data;
692b843c749SSergey Zigachev 		cp_hdr = (const struct gfx_firmware_header_v1_0 *)info->fw->data;
693b843c749SSergey Zigachev 		adev->firmware.fw_size +=
694b843c749SSergey Zigachev 			ALIGN(le32_to_cpu(header->ucode_size_bytes) - le32_to_cpu(cp_hdr->jt_size) * 4, PAGE_SIZE);
695b843c749SSergey Zigachev 
696b843c749SSergey Zigachev 		info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CP_MEC1_JT];
697b843c749SSergey Zigachev 		info->ucode_id = AMDGPU_UCODE_ID_CP_MEC1_JT;
698b843c749SSergey Zigachev 		info->fw = adev->gfx.mec_fw;
699b843c749SSergey Zigachev 		adev->firmware.fw_size +=
700b843c749SSergey Zigachev 			ALIGN(le32_to_cpu(cp_hdr->jt_size) * 4, PAGE_SIZE);
701b843c749SSergey Zigachev 
702b843c749SSergey Zigachev 		if (adev->gfx.mec2_fw) {
703b843c749SSergey Zigachev 			info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CP_MEC2];
704b843c749SSergey Zigachev 			info->ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
705b843c749SSergey Zigachev 			info->fw = adev->gfx.mec2_fw;
706b843c749SSergey Zigachev 			header = (const struct common_firmware_header *)info->fw->data;
707b843c749SSergey Zigachev 			cp_hdr = (const struct gfx_firmware_header_v1_0 *)info->fw->data;
708b843c749SSergey Zigachev 			adev->firmware.fw_size +=
709b843c749SSergey Zigachev 				ALIGN(le32_to_cpu(header->ucode_size_bytes) - le32_to_cpu(cp_hdr->jt_size) * 4, PAGE_SIZE);
710b843c749SSergey Zigachev 			info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CP_MEC2_JT];
711b843c749SSergey Zigachev 			info->ucode_id = AMDGPU_UCODE_ID_CP_MEC2_JT;
712b843c749SSergey Zigachev 			info->fw = adev->gfx.mec2_fw;
713b843c749SSergey Zigachev 			adev->firmware.fw_size +=
714b843c749SSergey Zigachev 				ALIGN(le32_to_cpu(cp_hdr->jt_size) * 4, PAGE_SIZE);
715b843c749SSergey Zigachev 		}
716b843c749SSergey Zigachev 
717b843c749SSergey Zigachev 	}
718b843c749SSergey Zigachev 
719b843c749SSergey Zigachev out:
720b843c749SSergey Zigachev 	if (err) {
721b843c749SSergey Zigachev 		dev_err(adev->dev,
722b843c749SSergey Zigachev 			"gfx9: Failed to load firmware \"%s\"\n",
723b843c749SSergey Zigachev 			fw_name);
724b843c749SSergey Zigachev 		release_firmware(adev->gfx.pfp_fw);
725b843c749SSergey Zigachev 		adev->gfx.pfp_fw = NULL;
726b843c749SSergey Zigachev 		release_firmware(adev->gfx.me_fw);
727b843c749SSergey Zigachev 		adev->gfx.me_fw = NULL;
728b843c749SSergey Zigachev 		release_firmware(adev->gfx.ce_fw);
729b843c749SSergey Zigachev 		adev->gfx.ce_fw = NULL;
730b843c749SSergey Zigachev 		release_firmware(adev->gfx.rlc_fw);
731b843c749SSergey Zigachev 		adev->gfx.rlc_fw = NULL;
732b843c749SSergey Zigachev 		release_firmware(adev->gfx.mec_fw);
733b843c749SSergey Zigachev 		adev->gfx.mec_fw = NULL;
734b843c749SSergey Zigachev 		release_firmware(adev->gfx.mec2_fw);
735b843c749SSergey Zigachev 		adev->gfx.mec2_fw = NULL;
736b843c749SSergey Zigachev 	}
737b843c749SSergey Zigachev 	return err;
738b843c749SSergey Zigachev }
739b843c749SSergey Zigachev 
gfx_v9_0_get_csb_size(struct amdgpu_device * adev)740b843c749SSergey Zigachev static u32 gfx_v9_0_get_csb_size(struct amdgpu_device *adev)
741b843c749SSergey Zigachev {
742b843c749SSergey Zigachev 	u32 count = 0;
743b843c749SSergey Zigachev 	const struct cs_section_def *sect = NULL;
744b843c749SSergey Zigachev 	const struct cs_extent_def *ext = NULL;
745b843c749SSergey Zigachev 
746b843c749SSergey Zigachev 	/* begin clear state */
747b843c749SSergey Zigachev 	count += 2;
748b843c749SSergey Zigachev 	/* context control state */
749b843c749SSergey Zigachev 	count += 3;
750b843c749SSergey Zigachev 
751b843c749SSergey Zigachev 	for (sect = gfx9_cs_data; sect->section != NULL; ++sect) {
752b843c749SSergey Zigachev 		for (ext = sect->section; ext->extent != NULL; ++ext) {
753b843c749SSergey Zigachev 			if (sect->id == SECT_CONTEXT)
754b843c749SSergey Zigachev 				count += 2 + ext->reg_count;
755b843c749SSergey Zigachev 			else
756b843c749SSergey Zigachev 				return 0;
757b843c749SSergey Zigachev 		}
758b843c749SSergey Zigachev 	}
759b843c749SSergey Zigachev 
760b843c749SSergey Zigachev 	/* end clear state */
761b843c749SSergey Zigachev 	count += 2;
762b843c749SSergey Zigachev 	/* clear state */
763b843c749SSergey Zigachev 	count += 2;
764b843c749SSergey Zigachev 
765b843c749SSergey Zigachev 	return count;
766b843c749SSergey Zigachev }
767b843c749SSergey Zigachev 
gfx_v9_0_get_csb_buffer(struct amdgpu_device * adev,volatile u32 * buffer)768b843c749SSergey Zigachev static void gfx_v9_0_get_csb_buffer(struct amdgpu_device *adev,
769b843c749SSergey Zigachev 				    volatile u32 *buffer)
770b843c749SSergey Zigachev {
771b843c749SSergey Zigachev 	u32 count = 0, i;
772b843c749SSergey Zigachev 	const struct cs_section_def *sect = NULL;
773b843c749SSergey Zigachev 	const struct cs_extent_def *ext = NULL;
774b843c749SSergey Zigachev 
775b843c749SSergey Zigachev 	if (adev->gfx.rlc.cs_data == NULL)
776b843c749SSergey Zigachev 		return;
777b843c749SSergey Zigachev 	if (buffer == NULL)
778b843c749SSergey Zigachev 		return;
779b843c749SSergey Zigachev 
780b843c749SSergey Zigachev 	buffer[count++] = cpu_to_le32(PACKET3(PACKET3_PREAMBLE_CNTL, 0));
781b843c749SSergey Zigachev 	buffer[count++] = cpu_to_le32(PACKET3_PREAMBLE_BEGIN_CLEAR_STATE);
782b843c749SSergey Zigachev 
783b843c749SSergey Zigachev 	buffer[count++] = cpu_to_le32(PACKET3(PACKET3_CONTEXT_CONTROL, 1));
784b843c749SSergey Zigachev 	buffer[count++] = cpu_to_le32(0x80000000);
785b843c749SSergey Zigachev 	buffer[count++] = cpu_to_le32(0x80000000);
786b843c749SSergey Zigachev 
787b843c749SSergey Zigachev 	for (sect = adev->gfx.rlc.cs_data; sect->section != NULL; ++sect) {
788b843c749SSergey Zigachev 		for (ext = sect->section; ext->extent != NULL; ++ext) {
789b843c749SSergey Zigachev 			if (sect->id == SECT_CONTEXT) {
790b843c749SSergey Zigachev 				buffer[count++] =
791b843c749SSergey Zigachev 					cpu_to_le32(PACKET3(PACKET3_SET_CONTEXT_REG, ext->reg_count));
792b843c749SSergey Zigachev 				buffer[count++] = cpu_to_le32(ext->reg_index -
793b843c749SSergey Zigachev 						PACKET3_SET_CONTEXT_REG_START);
794b843c749SSergey Zigachev 				for (i = 0; i < ext->reg_count; i++)
795b843c749SSergey Zigachev 					buffer[count++] = cpu_to_le32(ext->extent[i]);
796b843c749SSergey Zigachev 			} else {
797b843c749SSergey Zigachev 				return;
798b843c749SSergey Zigachev 			}
799b843c749SSergey Zigachev 		}
800b843c749SSergey Zigachev 	}
801b843c749SSergey Zigachev 
802b843c749SSergey Zigachev 	buffer[count++] = cpu_to_le32(PACKET3(PACKET3_PREAMBLE_CNTL, 0));
803b843c749SSergey Zigachev 	buffer[count++] = cpu_to_le32(PACKET3_PREAMBLE_END_CLEAR_STATE);
804b843c749SSergey Zigachev 
805b843c749SSergey Zigachev 	buffer[count++] = cpu_to_le32(PACKET3(PACKET3_CLEAR_STATE, 0));
806b843c749SSergey Zigachev 	buffer[count++] = cpu_to_le32(0);
807b843c749SSergey Zigachev }
808b843c749SSergey Zigachev 
gfx_v9_0_init_lbpw(struct amdgpu_device * adev)809b843c749SSergey Zigachev static void gfx_v9_0_init_lbpw(struct amdgpu_device *adev)
810b843c749SSergey Zigachev {
811b843c749SSergey Zigachev 	uint32_t data;
812b843c749SSergey Zigachev 
813b843c749SSergey Zigachev 	/* set mmRLC_LB_THR_CONFIG_1/2/3/4 */
814b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmRLC_LB_THR_CONFIG_1, 0x0000007F);
815b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmRLC_LB_THR_CONFIG_2, 0x0333A5A7);
816b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmRLC_LB_THR_CONFIG_3, 0x00000077);
817b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmRLC_LB_THR_CONFIG_4, (0x30 | 0x40 << 8 | 0x02FA << 16));
818b843c749SSergey Zigachev 
819b843c749SSergey Zigachev 	/* set mmRLC_LB_CNTR_INIT = 0x0000_0000 */
820b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmRLC_LB_CNTR_INIT, 0x00000000);
821b843c749SSergey Zigachev 
822b843c749SSergey Zigachev 	/* set mmRLC_LB_CNTR_MAX = 0x0000_0500 */
823b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmRLC_LB_CNTR_MAX, 0x00000500);
824b843c749SSergey Zigachev 
825b843c749SSergey Zigachev 	mutex_lock(&adev->grbm_idx_mutex);
826b843c749SSergey Zigachev 	/* set mmRLC_LB_INIT_CU_MASK thru broadcast mode to enable all SE/SH*/
827b843c749SSergey Zigachev 	gfx_v9_0_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
828b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmRLC_LB_INIT_CU_MASK, 0xffffffff);
829b843c749SSergey Zigachev 
830b843c749SSergey Zigachev 	/* set mmRLC_LB_PARAMS = 0x003F_1006 */
831b843c749SSergey Zigachev 	data = REG_SET_FIELD(0, RLC_LB_PARAMS, FIFO_SAMPLES, 0x0003);
832b843c749SSergey Zigachev 	data |= REG_SET_FIELD(data, RLC_LB_PARAMS, PG_IDLE_SAMPLES, 0x0010);
833b843c749SSergey Zigachev 	data |= REG_SET_FIELD(data, RLC_LB_PARAMS, PG_IDLE_SAMPLE_INTERVAL, 0x033F);
834b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmRLC_LB_PARAMS, data);
835b843c749SSergey Zigachev 
836b843c749SSergey Zigachev 	/* set mmRLC_GPM_GENERAL_7[31-16] = 0x00C0 */
837b843c749SSergey Zigachev 	data = RREG32_SOC15(GC, 0, mmRLC_GPM_GENERAL_7);
838b843c749SSergey Zigachev 	data &= 0x0000FFFF;
839b843c749SSergey Zigachev 	data |= 0x00C00000;
840b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmRLC_GPM_GENERAL_7, data);
841b843c749SSergey Zigachev 
842b843c749SSergey Zigachev 	/* set RLC_LB_ALWAYS_ACTIVE_CU_MASK = 0xFFF */
843b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmRLC_LB_ALWAYS_ACTIVE_CU_MASK, 0xFFF);
844b843c749SSergey Zigachev 
845b843c749SSergey Zigachev 	/* set RLC_LB_CNTL = 0x8000_0095, 31 bit is reserved,
846b843c749SSergey Zigachev 	 * but used for RLC_LB_CNTL configuration */
847b843c749SSergey Zigachev 	data = RLC_LB_CNTL__LB_CNT_SPIM_ACTIVE_MASK;
848b843c749SSergey Zigachev 	data |= REG_SET_FIELD(data, RLC_LB_CNTL, CU_MASK_USED_OFF_HYST, 0x09);
849b843c749SSergey Zigachev 	data |= REG_SET_FIELD(data, RLC_LB_CNTL, RESERVED, 0x80000);
850b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmRLC_LB_CNTL, data);
851b843c749SSergey Zigachev 	mutex_unlock(&adev->grbm_idx_mutex);
852b843c749SSergey Zigachev }
853b843c749SSergey Zigachev 
gfx_v9_0_enable_lbpw(struct amdgpu_device * adev,bool enable)854b843c749SSergey Zigachev static void gfx_v9_0_enable_lbpw(struct amdgpu_device *adev, bool enable)
855b843c749SSergey Zigachev {
856b843c749SSergey Zigachev 	WREG32_FIELD15(GC, 0, RLC_LB_CNTL, LOAD_BALANCE_ENABLE, enable ? 1 : 0);
857b843c749SSergey Zigachev }
858b843c749SSergey Zigachev 
rv_init_cp_jump_table(struct amdgpu_device * adev)859b843c749SSergey Zigachev static void rv_init_cp_jump_table(struct amdgpu_device *adev)
860b843c749SSergey Zigachev {
86178973132SSergey Zigachev 	const __le32 *fw_data = NULL;
862b843c749SSergey Zigachev 	volatile u32 *dst_ptr;
863b843c749SSergey Zigachev 	int me, i, max_me = 5;
864b843c749SSergey Zigachev 	u32 bo_offset = 0;
86578973132SSergey Zigachev 	u32 table_offset = 0, table_size = 0;
866b843c749SSergey Zigachev 
867b843c749SSergey Zigachev 	/* write the cp table buffer */
868b843c749SSergey Zigachev 	dst_ptr = adev->gfx.rlc.cp_table_ptr;
869b843c749SSergey Zigachev 	for (me = 0; me < max_me; me++) {
870b843c749SSergey Zigachev 		if (me == 0) {
871b843c749SSergey Zigachev 			const struct gfx_firmware_header_v1_0 *hdr =
872b843c749SSergey Zigachev 				(const struct gfx_firmware_header_v1_0 *)adev->gfx.ce_fw->data;
873b843c749SSergey Zigachev 			fw_data = (const __le32 *)
874b843c749SSergey Zigachev 				(adev->gfx.ce_fw->data +
875b843c749SSergey Zigachev 				 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
876b843c749SSergey Zigachev 			table_offset = le32_to_cpu(hdr->jt_offset);
877b843c749SSergey Zigachev 			table_size = le32_to_cpu(hdr->jt_size);
878b843c749SSergey Zigachev 		} else if (me == 1) {
879b843c749SSergey Zigachev 			const struct gfx_firmware_header_v1_0 *hdr =
880b843c749SSergey Zigachev 				(const struct gfx_firmware_header_v1_0 *)adev->gfx.pfp_fw->data;
881b843c749SSergey Zigachev 			fw_data = (const __le32 *)
882b843c749SSergey Zigachev 				(adev->gfx.pfp_fw->data +
883b843c749SSergey Zigachev 				 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
884b843c749SSergey Zigachev 			table_offset = le32_to_cpu(hdr->jt_offset);
885b843c749SSergey Zigachev 			table_size = le32_to_cpu(hdr->jt_size);
886b843c749SSergey Zigachev 		} else if (me == 2) {
887b843c749SSergey Zigachev 			const struct gfx_firmware_header_v1_0 *hdr =
888b843c749SSergey Zigachev 				(const struct gfx_firmware_header_v1_0 *)adev->gfx.me_fw->data;
889b843c749SSergey Zigachev 			fw_data = (const __le32 *)
890b843c749SSergey Zigachev 				(adev->gfx.me_fw->data +
891b843c749SSergey Zigachev 				 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
892b843c749SSergey Zigachev 			table_offset = le32_to_cpu(hdr->jt_offset);
893b843c749SSergey Zigachev 			table_size = le32_to_cpu(hdr->jt_size);
894b843c749SSergey Zigachev 		} else if (me == 3) {
895b843c749SSergey Zigachev 			const struct gfx_firmware_header_v1_0 *hdr =
896b843c749SSergey Zigachev 				(const struct gfx_firmware_header_v1_0 *)adev->gfx.mec_fw->data;
897b843c749SSergey Zigachev 			fw_data = (const __le32 *)
898b843c749SSergey Zigachev 				(adev->gfx.mec_fw->data +
899b843c749SSergey Zigachev 				 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
900b843c749SSergey Zigachev 			table_offset = le32_to_cpu(hdr->jt_offset);
901b843c749SSergey Zigachev 			table_size = le32_to_cpu(hdr->jt_size);
902b843c749SSergey Zigachev 		} else  if (me == 4) {
903b843c749SSergey Zigachev 			const struct gfx_firmware_header_v1_0 *hdr =
904b843c749SSergey Zigachev 				(const struct gfx_firmware_header_v1_0 *)adev->gfx.mec2_fw->data;
905b843c749SSergey Zigachev 			fw_data = (const __le32 *)
906b843c749SSergey Zigachev 				(adev->gfx.mec2_fw->data +
907b843c749SSergey Zigachev 				 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
908b843c749SSergey Zigachev 			table_offset = le32_to_cpu(hdr->jt_offset);
909b843c749SSergey Zigachev 			table_size = le32_to_cpu(hdr->jt_size);
910b843c749SSergey Zigachev 		}
911b843c749SSergey Zigachev 
912b843c749SSergey Zigachev 		for (i = 0; i < table_size; i ++) {
913b843c749SSergey Zigachev 			dst_ptr[bo_offset + i] =
914b843c749SSergey Zigachev 				cpu_to_le32(le32_to_cpu(fw_data[table_offset + i]));
915b843c749SSergey Zigachev 		}
916b843c749SSergey Zigachev 
917b843c749SSergey Zigachev 		bo_offset += table_size;
918b843c749SSergey Zigachev 	}
919b843c749SSergey Zigachev }
920b843c749SSergey Zigachev 
gfx_v9_0_rlc_fini(struct amdgpu_device * adev)921b843c749SSergey Zigachev static void gfx_v9_0_rlc_fini(struct amdgpu_device *adev)
922b843c749SSergey Zigachev {
923b843c749SSergey Zigachev 	/* clear state block */
924b843c749SSergey Zigachev 	amdgpu_bo_free_kernel(&adev->gfx.rlc.clear_state_obj,
92578973132SSergey Zigachev 			(u64 *)&adev->gfx.rlc.clear_state_gpu_addr,
926b843c749SSergey Zigachev 			(void **)&adev->gfx.rlc.cs_ptr);
927b843c749SSergey Zigachev 
928b843c749SSergey Zigachev 	/* jump table block */
929b843c749SSergey Zigachev 	amdgpu_bo_free_kernel(&adev->gfx.rlc.cp_table_obj,
93078973132SSergey Zigachev 			(u64 *)&adev->gfx.rlc.cp_table_gpu_addr,
931b843c749SSergey Zigachev 			(void **)&adev->gfx.rlc.cp_table_ptr);
932b843c749SSergey Zigachev }
933b843c749SSergey Zigachev 
gfx_v9_0_rlc_init(struct amdgpu_device * adev)934b843c749SSergey Zigachev static int gfx_v9_0_rlc_init(struct amdgpu_device *adev)
935b843c749SSergey Zigachev {
936b843c749SSergey Zigachev 	volatile u32 *dst_ptr;
937b843c749SSergey Zigachev 	u32 dws;
938b843c749SSergey Zigachev 	const struct cs_section_def *cs_data;
939b843c749SSergey Zigachev 	int r;
940b843c749SSergey Zigachev 
941b843c749SSergey Zigachev 	adev->gfx.rlc.cs_data = gfx9_cs_data;
942b843c749SSergey Zigachev 
943b843c749SSergey Zigachev 	cs_data = adev->gfx.rlc.cs_data;
944b843c749SSergey Zigachev 
945b843c749SSergey Zigachev 	if (cs_data) {
946b843c749SSergey Zigachev 		/* clear state block */
947b843c749SSergey Zigachev 		adev->gfx.rlc.clear_state_size = dws = gfx_v9_0_get_csb_size(adev);
948b843c749SSergey Zigachev 		r = amdgpu_bo_create_reserved(adev, dws * 4, PAGE_SIZE,
949b843c749SSergey Zigachev 					      AMDGPU_GEM_DOMAIN_VRAM,
950b843c749SSergey Zigachev 					      &adev->gfx.rlc.clear_state_obj,
95178973132SSergey Zigachev 					      (u64 *)&adev->gfx.rlc.clear_state_gpu_addr,
952b843c749SSergey Zigachev 					      (void **)&adev->gfx.rlc.cs_ptr);
953b843c749SSergey Zigachev 		if (r) {
954b843c749SSergey Zigachev 			dev_err(adev->dev, "(%d) failed to create rlc csb bo\n",
955b843c749SSergey Zigachev 				r);
956b843c749SSergey Zigachev 			gfx_v9_0_rlc_fini(adev);
957b843c749SSergey Zigachev 			return r;
958b843c749SSergey Zigachev 		}
959b843c749SSergey Zigachev 		/* set up the cs buffer */
960b843c749SSergey Zigachev 		dst_ptr = adev->gfx.rlc.cs_ptr;
961b843c749SSergey Zigachev 		gfx_v9_0_get_csb_buffer(adev, dst_ptr);
962b843c749SSergey Zigachev 		amdgpu_bo_kunmap(adev->gfx.rlc.clear_state_obj);
963b843c749SSergey Zigachev 		amdgpu_bo_unpin(adev->gfx.rlc.clear_state_obj);
964b843c749SSergey Zigachev 		amdgpu_bo_unreserve(adev->gfx.rlc.clear_state_obj);
965b843c749SSergey Zigachev 	}
966b843c749SSergey Zigachev 
967b843c749SSergey Zigachev 	if (adev->asic_type == CHIP_RAVEN) {
968b843c749SSergey Zigachev 		/* TODO: double check the cp_table_size for RV */
969b843c749SSergey Zigachev 		adev->gfx.rlc.cp_table_size = ALIGN(96 * 5 * 4, 2048) + (64 * 1024); /* JT + GDS */
970b843c749SSergey Zigachev 		r = amdgpu_bo_create_reserved(adev, adev->gfx.rlc.cp_table_size,
971b843c749SSergey Zigachev 					      PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
972b843c749SSergey Zigachev 					      &adev->gfx.rlc.cp_table_obj,
97378973132SSergey Zigachev 					      (u64 *)&adev->gfx.rlc.cp_table_gpu_addr,
974b843c749SSergey Zigachev 					      (void **)&adev->gfx.rlc.cp_table_ptr);
975b843c749SSergey Zigachev 		if (r) {
976b843c749SSergey Zigachev 			dev_err(adev->dev,
977b843c749SSergey Zigachev 				"(%d) failed to create cp table bo\n", r);
978b843c749SSergey Zigachev 			gfx_v9_0_rlc_fini(adev);
979b843c749SSergey Zigachev 			return r;
980b843c749SSergey Zigachev 		}
981b843c749SSergey Zigachev 
982b843c749SSergey Zigachev 		rv_init_cp_jump_table(adev);
983b843c749SSergey Zigachev 		amdgpu_bo_kunmap(adev->gfx.rlc.cp_table_obj);
984b843c749SSergey Zigachev 		amdgpu_bo_unreserve(adev->gfx.rlc.cp_table_obj);
985b843c749SSergey Zigachev 
986b843c749SSergey Zigachev 		gfx_v9_0_init_lbpw(adev);
987b843c749SSergey Zigachev 	}
988b843c749SSergey Zigachev 
989b843c749SSergey Zigachev 	return 0;
990b843c749SSergey Zigachev }
991b843c749SSergey Zigachev 
gfx_v9_0_csb_vram_pin(struct amdgpu_device * adev)992b843c749SSergey Zigachev static int gfx_v9_0_csb_vram_pin(struct amdgpu_device *adev)
993b843c749SSergey Zigachev {
994b843c749SSergey Zigachev 	int r;
995b843c749SSergey Zigachev 
996b843c749SSergey Zigachev 	r = amdgpu_bo_reserve(adev->gfx.rlc.clear_state_obj, false);
997b843c749SSergey Zigachev 	if (unlikely(r != 0))
998b843c749SSergey Zigachev 		return r;
999b843c749SSergey Zigachev 
1000b843c749SSergey Zigachev 	r = amdgpu_bo_pin(adev->gfx.rlc.clear_state_obj,
1001b843c749SSergey Zigachev 			AMDGPU_GEM_DOMAIN_VRAM);
1002b843c749SSergey Zigachev 	if (!r)
1003b843c749SSergey Zigachev 		adev->gfx.rlc.clear_state_gpu_addr =
1004b843c749SSergey Zigachev 			amdgpu_bo_gpu_offset(adev->gfx.rlc.clear_state_obj);
1005b843c749SSergey Zigachev 
1006b843c749SSergey Zigachev 	amdgpu_bo_unreserve(adev->gfx.rlc.clear_state_obj);
1007b843c749SSergey Zigachev 
1008b843c749SSergey Zigachev 	return r;
1009b843c749SSergey Zigachev }
1010b843c749SSergey Zigachev 
gfx_v9_0_csb_vram_unpin(struct amdgpu_device * adev)1011b843c749SSergey Zigachev static void gfx_v9_0_csb_vram_unpin(struct amdgpu_device *adev)
1012b843c749SSergey Zigachev {
1013b843c749SSergey Zigachev 	int r;
1014b843c749SSergey Zigachev 
1015b843c749SSergey Zigachev 	if (!adev->gfx.rlc.clear_state_obj)
1016b843c749SSergey Zigachev 		return;
1017b843c749SSergey Zigachev 
1018b843c749SSergey Zigachev 	r = amdgpu_bo_reserve(adev->gfx.rlc.clear_state_obj, true);
1019b843c749SSergey Zigachev 	if (likely(r == 0)) {
1020b843c749SSergey Zigachev 		amdgpu_bo_unpin(adev->gfx.rlc.clear_state_obj);
1021b843c749SSergey Zigachev 		amdgpu_bo_unreserve(adev->gfx.rlc.clear_state_obj);
1022b843c749SSergey Zigachev 	}
1023b843c749SSergey Zigachev }
1024b843c749SSergey Zigachev 
gfx_v9_0_mec_fini(struct amdgpu_device * adev)1025b843c749SSergey Zigachev static void gfx_v9_0_mec_fini(struct amdgpu_device *adev)
1026b843c749SSergey Zigachev {
1027b843c749SSergey Zigachev 	amdgpu_bo_free_kernel(&adev->gfx.mec.hpd_eop_obj, NULL, NULL);
1028b843c749SSergey Zigachev 	amdgpu_bo_free_kernel(&adev->gfx.mec.mec_fw_obj, NULL, NULL);
1029b843c749SSergey Zigachev }
1030b843c749SSergey Zigachev 
gfx_v9_0_mec_init(struct amdgpu_device * adev)1031b843c749SSergey Zigachev static int gfx_v9_0_mec_init(struct amdgpu_device *adev)
1032b843c749SSergey Zigachev {
1033b843c749SSergey Zigachev 	int r;
1034b843c749SSergey Zigachev 	u32 *hpd;
1035b843c749SSergey Zigachev 	const __le32 *fw_data;
1036b843c749SSergey Zigachev 	unsigned fw_size;
1037b843c749SSergey Zigachev 	u32 *fw;
1038b843c749SSergey Zigachev 	size_t mec_hpd_size;
1039b843c749SSergey Zigachev 
1040b843c749SSergey Zigachev 	const struct gfx_firmware_header_v1_0 *mec_hdr;
1041b843c749SSergey Zigachev 
1042b843c749SSergey Zigachev 	bitmap_zero(adev->gfx.mec.queue_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
1043b843c749SSergey Zigachev 
1044b843c749SSergey Zigachev 	/* take ownership of the relevant compute queues */
1045b843c749SSergey Zigachev 	amdgpu_gfx_compute_queue_acquire(adev);
1046b843c749SSergey Zigachev 	mec_hpd_size = adev->gfx.num_compute_rings * GFX9_MEC_HPD_SIZE;
1047b843c749SSergey Zigachev 
1048b843c749SSergey Zigachev 	r = amdgpu_bo_create_reserved(adev, mec_hpd_size, PAGE_SIZE,
1049b843c749SSergey Zigachev 				      AMDGPU_GEM_DOMAIN_GTT,
1050b843c749SSergey Zigachev 				      &adev->gfx.mec.hpd_eop_obj,
1051b843c749SSergey Zigachev 				      &adev->gfx.mec.hpd_eop_gpu_addr,
1052b843c749SSergey Zigachev 				      (void **)&hpd);
1053b843c749SSergey Zigachev 	if (r) {
1054b843c749SSergey Zigachev 		dev_warn(adev->dev, "(%d) create HDP EOP bo failed\n", r);
1055b843c749SSergey Zigachev 		gfx_v9_0_mec_fini(adev);
1056b843c749SSergey Zigachev 		return r;
1057b843c749SSergey Zigachev 	}
1058b843c749SSergey Zigachev 
1059b843c749SSergey Zigachev 	memset(hpd, 0, adev->gfx.mec.hpd_eop_obj->tbo.mem.size);
1060b843c749SSergey Zigachev 
1061b843c749SSergey Zigachev 	amdgpu_bo_kunmap(adev->gfx.mec.hpd_eop_obj);
1062b843c749SSergey Zigachev 	amdgpu_bo_unreserve(adev->gfx.mec.hpd_eop_obj);
1063b843c749SSergey Zigachev 
1064b843c749SSergey Zigachev 	mec_hdr = (const struct gfx_firmware_header_v1_0 *)adev->gfx.mec_fw->data;
1065b843c749SSergey Zigachev 
1066b843c749SSergey Zigachev 	fw_data = (const __le32 *)
1067b843c749SSergey Zigachev 		(adev->gfx.mec_fw->data +
1068b843c749SSergey Zigachev 		 le32_to_cpu(mec_hdr->header.ucode_array_offset_bytes));
1069b843c749SSergey Zigachev 	fw_size = le32_to_cpu(mec_hdr->header.ucode_size_bytes) / 4;
1070b843c749SSergey Zigachev 
1071b843c749SSergey Zigachev 	r = amdgpu_bo_create_reserved(adev, mec_hdr->header.ucode_size_bytes,
1072b843c749SSergey Zigachev 				      PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
1073b843c749SSergey Zigachev 				      &adev->gfx.mec.mec_fw_obj,
1074b843c749SSergey Zigachev 				      &adev->gfx.mec.mec_fw_gpu_addr,
1075b843c749SSergey Zigachev 				      (void **)&fw);
1076b843c749SSergey Zigachev 	if (r) {
1077b843c749SSergey Zigachev 		dev_warn(adev->dev, "(%d) create mec firmware bo failed\n", r);
1078b843c749SSergey Zigachev 		gfx_v9_0_mec_fini(adev);
1079b843c749SSergey Zigachev 		return r;
1080b843c749SSergey Zigachev 	}
1081b843c749SSergey Zigachev 
1082b843c749SSergey Zigachev 	memcpy(fw, fw_data, fw_size);
1083b843c749SSergey Zigachev 
1084b843c749SSergey Zigachev 	amdgpu_bo_kunmap(adev->gfx.mec.mec_fw_obj);
1085b843c749SSergey Zigachev 	amdgpu_bo_unreserve(adev->gfx.mec.mec_fw_obj);
1086b843c749SSergey Zigachev 
1087b843c749SSergey Zigachev 	return 0;
1088b843c749SSergey Zigachev }
1089b843c749SSergey Zigachev 
wave_read_ind(struct amdgpu_device * adev,uint32_t simd,uint32_t wave,uint32_t address)1090b843c749SSergey Zigachev static uint32_t wave_read_ind(struct amdgpu_device *adev, uint32_t simd, uint32_t wave, uint32_t address)
1091b843c749SSergey Zigachev {
1092b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmSQ_IND_INDEX,
1093b843c749SSergey Zigachev 		(wave << SQ_IND_INDEX__WAVE_ID__SHIFT) |
1094b843c749SSergey Zigachev 		(simd << SQ_IND_INDEX__SIMD_ID__SHIFT) |
1095b843c749SSergey Zigachev 		(address << SQ_IND_INDEX__INDEX__SHIFT) |
1096b843c749SSergey Zigachev 		(SQ_IND_INDEX__FORCE_READ_MASK));
1097b843c749SSergey Zigachev 	return RREG32_SOC15(GC, 0, mmSQ_IND_DATA);
1098b843c749SSergey Zigachev }
1099b843c749SSergey Zigachev 
wave_read_regs(struct amdgpu_device * adev,uint32_t simd,uint32_t wave,uint32_t thread,uint32_t regno,uint32_t num,uint32_t * out)1100b843c749SSergey Zigachev static void wave_read_regs(struct amdgpu_device *adev, uint32_t simd,
1101b843c749SSergey Zigachev 			   uint32_t wave, uint32_t thread,
1102b843c749SSergey Zigachev 			   uint32_t regno, uint32_t num, uint32_t *out)
1103b843c749SSergey Zigachev {
1104b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmSQ_IND_INDEX,
1105b843c749SSergey Zigachev 		(wave << SQ_IND_INDEX__WAVE_ID__SHIFT) |
1106b843c749SSergey Zigachev 		(simd << SQ_IND_INDEX__SIMD_ID__SHIFT) |
1107b843c749SSergey Zigachev 		(regno << SQ_IND_INDEX__INDEX__SHIFT) |
1108b843c749SSergey Zigachev 		(thread << SQ_IND_INDEX__THREAD_ID__SHIFT) |
1109b843c749SSergey Zigachev 		(SQ_IND_INDEX__FORCE_READ_MASK) |
1110b843c749SSergey Zigachev 		(SQ_IND_INDEX__AUTO_INCR_MASK));
1111b843c749SSergey Zigachev 	while (num--)
1112b843c749SSergey Zigachev 		*(out++) = RREG32_SOC15(GC, 0, mmSQ_IND_DATA);
1113b843c749SSergey Zigachev }
1114b843c749SSergey Zigachev 
gfx_v9_0_read_wave_data(struct amdgpu_device * adev,uint32_t simd,uint32_t wave,uint32_t * dst,int * no_fields)1115b843c749SSergey Zigachev static void gfx_v9_0_read_wave_data(struct amdgpu_device *adev, uint32_t simd, uint32_t wave, uint32_t *dst, int *no_fields)
1116b843c749SSergey Zigachev {
1117b843c749SSergey Zigachev 	/* type 1 wave data */
1118b843c749SSergey Zigachev 	dst[(*no_fields)++] = 1;
1119b843c749SSergey Zigachev 	dst[(*no_fields)++] = wave_read_ind(adev, simd, wave, ixSQ_WAVE_STATUS);
1120b843c749SSergey Zigachev 	dst[(*no_fields)++] = wave_read_ind(adev, simd, wave, ixSQ_WAVE_PC_LO);
1121b843c749SSergey Zigachev 	dst[(*no_fields)++] = wave_read_ind(adev, simd, wave, ixSQ_WAVE_PC_HI);
1122b843c749SSergey Zigachev 	dst[(*no_fields)++] = wave_read_ind(adev, simd, wave, ixSQ_WAVE_EXEC_LO);
1123b843c749SSergey Zigachev 	dst[(*no_fields)++] = wave_read_ind(adev, simd, wave, ixSQ_WAVE_EXEC_HI);
1124b843c749SSergey Zigachev 	dst[(*no_fields)++] = wave_read_ind(adev, simd, wave, ixSQ_WAVE_HW_ID);
1125b843c749SSergey Zigachev 	dst[(*no_fields)++] = wave_read_ind(adev, simd, wave, ixSQ_WAVE_INST_DW0);
1126b843c749SSergey Zigachev 	dst[(*no_fields)++] = wave_read_ind(adev, simd, wave, ixSQ_WAVE_INST_DW1);
1127b843c749SSergey Zigachev 	dst[(*no_fields)++] = wave_read_ind(adev, simd, wave, ixSQ_WAVE_GPR_ALLOC);
1128b843c749SSergey Zigachev 	dst[(*no_fields)++] = wave_read_ind(adev, simd, wave, ixSQ_WAVE_LDS_ALLOC);
1129b843c749SSergey Zigachev 	dst[(*no_fields)++] = wave_read_ind(adev, simd, wave, ixSQ_WAVE_TRAPSTS);
1130b843c749SSergey Zigachev 	dst[(*no_fields)++] = wave_read_ind(adev, simd, wave, ixSQ_WAVE_IB_STS);
1131b843c749SSergey Zigachev 	dst[(*no_fields)++] = wave_read_ind(adev, simd, wave, ixSQ_WAVE_IB_DBG0);
1132b843c749SSergey Zigachev 	dst[(*no_fields)++] = wave_read_ind(adev, simd, wave, ixSQ_WAVE_M0);
1133b843c749SSergey Zigachev }
1134b843c749SSergey Zigachev 
gfx_v9_0_read_wave_sgprs(struct amdgpu_device * adev,uint32_t simd,uint32_t wave,uint32_t start,uint32_t size,uint32_t * dst)1135b843c749SSergey Zigachev static void gfx_v9_0_read_wave_sgprs(struct amdgpu_device *adev, uint32_t simd,
1136b843c749SSergey Zigachev 				     uint32_t wave, uint32_t start,
1137b843c749SSergey Zigachev 				     uint32_t size, uint32_t *dst)
1138b843c749SSergey Zigachev {
1139b843c749SSergey Zigachev 	wave_read_regs(
1140b843c749SSergey Zigachev 		adev, simd, wave, 0,
1141b843c749SSergey Zigachev 		start + SQIND_WAVE_SGPRS_OFFSET, size, dst);
1142b843c749SSergey Zigachev }
1143b843c749SSergey Zigachev 
gfx_v9_0_read_wave_vgprs(struct amdgpu_device * adev,uint32_t simd,uint32_t wave,uint32_t thread,uint32_t start,uint32_t size,uint32_t * dst)1144b843c749SSergey Zigachev static void gfx_v9_0_read_wave_vgprs(struct amdgpu_device *adev, uint32_t simd,
1145b843c749SSergey Zigachev 				     uint32_t wave, uint32_t thread,
1146b843c749SSergey Zigachev 				     uint32_t start, uint32_t size,
1147b843c749SSergey Zigachev 				     uint32_t *dst)
1148b843c749SSergey Zigachev {
1149b843c749SSergey Zigachev 	wave_read_regs(
1150b843c749SSergey Zigachev 		adev, simd, wave, thread,
1151b843c749SSergey Zigachev 		start + SQIND_WAVE_VGPRS_OFFSET, size, dst);
1152b843c749SSergey Zigachev }
1153b843c749SSergey Zigachev 
gfx_v9_0_select_me_pipe_q(struct amdgpu_device * adev,u32 me,u32 pipe,u32 q)1154b843c749SSergey Zigachev static void gfx_v9_0_select_me_pipe_q(struct amdgpu_device *adev,
1155b843c749SSergey Zigachev 				  u32 me, u32 pipe, u32 q)
1156b843c749SSergey Zigachev {
1157b843c749SSergey Zigachev 	soc15_grbm_select(adev, me, pipe, q, 0);
1158b843c749SSergey Zigachev }
1159b843c749SSergey Zigachev 
1160b843c749SSergey Zigachev static const struct amdgpu_gfx_funcs gfx_v9_0_gfx_funcs = {
1161b843c749SSergey Zigachev 	.get_gpu_clock_counter = &gfx_v9_0_get_gpu_clock_counter,
1162b843c749SSergey Zigachev 	.select_se_sh = &gfx_v9_0_select_se_sh,
1163b843c749SSergey Zigachev 	.read_wave_data = &gfx_v9_0_read_wave_data,
1164b843c749SSergey Zigachev 	.read_wave_sgprs = &gfx_v9_0_read_wave_sgprs,
1165b843c749SSergey Zigachev 	.read_wave_vgprs = &gfx_v9_0_read_wave_vgprs,
1166b843c749SSergey Zigachev 	.select_me_pipe_q = &gfx_v9_0_select_me_pipe_q
1167b843c749SSergey Zigachev };
1168b843c749SSergey Zigachev 
gfx_v9_0_gpu_early_init(struct amdgpu_device * adev)1169b843c749SSergey Zigachev static int gfx_v9_0_gpu_early_init(struct amdgpu_device *adev)
1170b843c749SSergey Zigachev {
1171b843c749SSergey Zigachev 	u32 gb_addr_config;
1172b843c749SSergey Zigachev 	int err;
1173b843c749SSergey Zigachev 
1174b843c749SSergey Zigachev 	adev->gfx.funcs = &gfx_v9_0_gfx_funcs;
1175b843c749SSergey Zigachev 
1176b843c749SSergey Zigachev 	switch (adev->asic_type) {
1177b843c749SSergey Zigachev 	case CHIP_VEGA10:
1178b843c749SSergey Zigachev 		adev->gfx.config.max_hw_contexts = 8;
1179b843c749SSergey Zigachev 		adev->gfx.config.sc_prim_fifo_size_frontend = 0x20;
1180b843c749SSergey Zigachev 		adev->gfx.config.sc_prim_fifo_size_backend = 0x100;
1181b843c749SSergey Zigachev 		adev->gfx.config.sc_hiz_tile_fifo_size = 0x30;
1182b843c749SSergey Zigachev 		adev->gfx.config.sc_earlyz_tile_fifo_size = 0x4C0;
1183b843c749SSergey Zigachev 		gb_addr_config = VEGA10_GB_ADDR_CONFIG_GOLDEN;
1184b843c749SSergey Zigachev 		break;
1185b843c749SSergey Zigachev 	case CHIP_VEGA12:
1186b843c749SSergey Zigachev 		adev->gfx.config.max_hw_contexts = 8;
1187b843c749SSergey Zigachev 		adev->gfx.config.sc_prim_fifo_size_frontend = 0x20;
1188b843c749SSergey Zigachev 		adev->gfx.config.sc_prim_fifo_size_backend = 0x100;
1189b843c749SSergey Zigachev 		adev->gfx.config.sc_hiz_tile_fifo_size = 0x30;
1190b843c749SSergey Zigachev 		adev->gfx.config.sc_earlyz_tile_fifo_size = 0x4C0;
1191b843c749SSergey Zigachev 		gb_addr_config = VEGA12_GB_ADDR_CONFIG_GOLDEN;
1192b843c749SSergey Zigachev 		DRM_INFO("fix gfx.config for vega12\n");
1193b843c749SSergey Zigachev 		break;
1194b843c749SSergey Zigachev 	case CHIP_VEGA20:
1195b843c749SSergey Zigachev 		adev->gfx.config.max_hw_contexts = 8;
1196b843c749SSergey Zigachev 		adev->gfx.config.sc_prim_fifo_size_frontend = 0x20;
1197b843c749SSergey Zigachev 		adev->gfx.config.sc_prim_fifo_size_backend = 0x100;
1198b843c749SSergey Zigachev 		adev->gfx.config.sc_hiz_tile_fifo_size = 0x30;
1199b843c749SSergey Zigachev 		adev->gfx.config.sc_earlyz_tile_fifo_size = 0x4C0;
1200b843c749SSergey Zigachev 		gb_addr_config = RREG32_SOC15(GC, 0, mmGB_ADDR_CONFIG);
1201b843c749SSergey Zigachev 		gb_addr_config &= ~0xf3e777ff;
1202b843c749SSergey Zigachev 		gb_addr_config |= 0x22014042;
1203b843c749SSergey Zigachev 		/* check vbios table if gpu info is not available */
1204b843c749SSergey Zigachev 		err = amdgpu_atomfirmware_get_gfx_info(adev);
1205b843c749SSergey Zigachev 		if (err)
1206b843c749SSergey Zigachev 			return err;
1207b843c749SSergey Zigachev 		break;
1208b843c749SSergey Zigachev 	case CHIP_RAVEN:
1209b843c749SSergey Zigachev 		adev->gfx.config.max_hw_contexts = 8;
1210b843c749SSergey Zigachev 		adev->gfx.config.sc_prim_fifo_size_frontend = 0x20;
1211b843c749SSergey Zigachev 		adev->gfx.config.sc_prim_fifo_size_backend = 0x100;
1212b843c749SSergey Zigachev 		adev->gfx.config.sc_hiz_tile_fifo_size = 0x30;
1213b843c749SSergey Zigachev 		adev->gfx.config.sc_earlyz_tile_fifo_size = 0x4C0;
1214b843c749SSergey Zigachev 		gb_addr_config = RAVEN_GB_ADDR_CONFIG_GOLDEN;
1215b843c749SSergey Zigachev 		break;
1216b843c749SSergey Zigachev 	default:
1217b843c749SSergey Zigachev 		BUG();
1218b843c749SSergey Zigachev 		break;
1219b843c749SSergey Zigachev 	}
1220b843c749SSergey Zigachev 
1221b843c749SSergey Zigachev 	adev->gfx.config.gb_addr_config = gb_addr_config;
1222b843c749SSergey Zigachev 
1223b843c749SSergey Zigachev 	adev->gfx.config.gb_addr_config_fields.num_pipes = 1 <<
1224b843c749SSergey Zigachev 			REG_GET_FIELD(
1225b843c749SSergey Zigachev 					adev->gfx.config.gb_addr_config,
1226b843c749SSergey Zigachev 					GB_ADDR_CONFIG,
1227b843c749SSergey Zigachev 					NUM_PIPES);
1228b843c749SSergey Zigachev 
1229b843c749SSergey Zigachev 	adev->gfx.config.max_tile_pipes =
1230b843c749SSergey Zigachev 		adev->gfx.config.gb_addr_config_fields.num_pipes;
1231b843c749SSergey Zigachev 
1232b843c749SSergey Zigachev 	adev->gfx.config.gb_addr_config_fields.num_banks = 1 <<
1233b843c749SSergey Zigachev 			REG_GET_FIELD(
1234b843c749SSergey Zigachev 					adev->gfx.config.gb_addr_config,
1235b843c749SSergey Zigachev 					GB_ADDR_CONFIG,
1236b843c749SSergey Zigachev 					NUM_BANKS);
1237b843c749SSergey Zigachev 	adev->gfx.config.gb_addr_config_fields.max_compress_frags = 1 <<
1238b843c749SSergey Zigachev 			REG_GET_FIELD(
1239b843c749SSergey Zigachev 					adev->gfx.config.gb_addr_config,
1240b843c749SSergey Zigachev 					GB_ADDR_CONFIG,
1241b843c749SSergey Zigachev 					MAX_COMPRESSED_FRAGS);
1242b843c749SSergey Zigachev 	adev->gfx.config.gb_addr_config_fields.num_rb_per_se = 1 <<
1243b843c749SSergey Zigachev 			REG_GET_FIELD(
1244b843c749SSergey Zigachev 					adev->gfx.config.gb_addr_config,
1245b843c749SSergey Zigachev 					GB_ADDR_CONFIG,
1246b843c749SSergey Zigachev 					NUM_RB_PER_SE);
1247b843c749SSergey Zigachev 	adev->gfx.config.gb_addr_config_fields.num_se = 1 <<
1248b843c749SSergey Zigachev 			REG_GET_FIELD(
1249b843c749SSergey Zigachev 					adev->gfx.config.gb_addr_config,
1250b843c749SSergey Zigachev 					GB_ADDR_CONFIG,
1251b843c749SSergey Zigachev 					NUM_SHADER_ENGINES);
1252b843c749SSergey Zigachev 	adev->gfx.config.gb_addr_config_fields.pipe_interleave_size = 1 << (8 +
1253b843c749SSergey Zigachev 			REG_GET_FIELD(
1254b843c749SSergey Zigachev 					adev->gfx.config.gb_addr_config,
1255b843c749SSergey Zigachev 					GB_ADDR_CONFIG,
1256b843c749SSergey Zigachev 					PIPE_INTERLEAVE_SIZE));
1257b843c749SSergey Zigachev 
1258b843c749SSergey Zigachev 	return 0;
1259b843c749SSergey Zigachev }
1260b843c749SSergey Zigachev 
gfx_v9_0_ngg_create_buf(struct amdgpu_device * adev,struct amdgpu_ngg_buf * ngg_buf,int size_se,int default_size_se)1261b843c749SSergey Zigachev static int gfx_v9_0_ngg_create_buf(struct amdgpu_device *adev,
1262b843c749SSergey Zigachev 				   struct amdgpu_ngg_buf *ngg_buf,
1263b843c749SSergey Zigachev 				   int size_se,
1264b843c749SSergey Zigachev 				   int default_size_se)
1265b843c749SSergey Zigachev {
1266b843c749SSergey Zigachev 	int r;
1267b843c749SSergey Zigachev 
1268b843c749SSergey Zigachev 	if (size_se < 0) {
1269b843c749SSergey Zigachev 		dev_err(adev->dev, "Buffer size is invalid: %d\n", size_se);
1270b843c749SSergey Zigachev 		return -EINVAL;
1271b843c749SSergey Zigachev 	}
1272b843c749SSergey Zigachev 	size_se = size_se ? size_se : default_size_se;
1273b843c749SSergey Zigachev 
1274b843c749SSergey Zigachev 	ngg_buf->size = size_se * adev->gfx.config.max_shader_engines;
1275b843c749SSergey Zigachev 	r = amdgpu_bo_create_kernel(adev, ngg_buf->size,
1276b843c749SSergey Zigachev 				    PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
1277b843c749SSergey Zigachev 				    &ngg_buf->bo,
127878973132SSergey Zigachev 				    (u64 *)&ngg_buf->gpu_addr,
1279b843c749SSergey Zigachev 				    NULL);
1280b843c749SSergey Zigachev 	if (r) {
1281b843c749SSergey Zigachev 		dev_err(adev->dev, "(%d) failed to create NGG buffer\n", r);
1282b843c749SSergey Zigachev 		return r;
1283b843c749SSergey Zigachev 	}
1284b843c749SSergey Zigachev 	ngg_buf->bo_size = amdgpu_bo_size(ngg_buf->bo);
1285b843c749SSergey Zigachev 
1286b843c749SSergey Zigachev 	return r;
1287b843c749SSergey Zigachev }
1288b843c749SSergey Zigachev 
gfx_v9_0_ngg_fini(struct amdgpu_device * adev)1289b843c749SSergey Zigachev static int gfx_v9_0_ngg_fini(struct amdgpu_device *adev)
1290b843c749SSergey Zigachev {
1291b843c749SSergey Zigachev 	int i;
1292b843c749SSergey Zigachev 
1293b843c749SSergey Zigachev 	for (i = 0; i < NGG_BUF_MAX; i++)
1294b843c749SSergey Zigachev 		amdgpu_bo_free_kernel(&adev->gfx.ngg.buf[i].bo,
129578973132SSergey Zigachev 				      (u64 *)&adev->gfx.ngg.buf[i].gpu_addr,
1296b843c749SSergey Zigachev 				      NULL);
1297b843c749SSergey Zigachev 
1298b843c749SSergey Zigachev 	memset(&adev->gfx.ngg.buf[0], 0,
1299b843c749SSergey Zigachev 			sizeof(struct amdgpu_ngg_buf) * NGG_BUF_MAX);
1300b843c749SSergey Zigachev 
1301b843c749SSergey Zigachev 	adev->gfx.ngg.init = false;
1302b843c749SSergey Zigachev 
1303b843c749SSergey Zigachev 	return 0;
1304b843c749SSergey Zigachev }
1305b843c749SSergey Zigachev 
gfx_v9_0_ngg_init(struct amdgpu_device * adev)1306b843c749SSergey Zigachev static int gfx_v9_0_ngg_init(struct amdgpu_device *adev)
1307b843c749SSergey Zigachev {
1308b843c749SSergey Zigachev 	int r;
1309b843c749SSergey Zigachev 
1310b843c749SSergey Zigachev 	if (!amdgpu_ngg || adev->gfx.ngg.init == true)
1311b843c749SSergey Zigachev 		return 0;
1312b843c749SSergey Zigachev 
1313b843c749SSergey Zigachev 	/* GDS reserve memory: 64 bytes alignment */
1314b843c749SSergey Zigachev 	adev->gfx.ngg.gds_reserve_size = ALIGN(5 * 4, 0x40);
1315b843c749SSergey Zigachev 	adev->gds.mem.total_size -= adev->gfx.ngg.gds_reserve_size;
1316b843c749SSergey Zigachev 	adev->gds.mem.gfx_partition_size -= adev->gfx.ngg.gds_reserve_size;
1317b843c749SSergey Zigachev 	adev->gfx.ngg.gds_reserve_addr = RREG32_SOC15(GC, 0, mmGDS_VMID0_BASE);
1318b843c749SSergey Zigachev 	adev->gfx.ngg.gds_reserve_addr += RREG32_SOC15(GC, 0, mmGDS_VMID0_SIZE);
1319b843c749SSergey Zigachev 
1320b843c749SSergey Zigachev 	/* Primitive Buffer */
1321b843c749SSergey Zigachev 	r = gfx_v9_0_ngg_create_buf(adev, &adev->gfx.ngg.buf[NGG_PRIM],
1322b843c749SSergey Zigachev 				    amdgpu_prim_buf_per_se,
1323b843c749SSergey Zigachev 				    64 * 1024);
1324b843c749SSergey Zigachev 	if (r) {
1325b843c749SSergey Zigachev 		dev_err(adev->dev, "Failed to create Primitive Buffer\n");
1326b843c749SSergey Zigachev 		goto err;
1327b843c749SSergey Zigachev 	}
1328b843c749SSergey Zigachev 
1329b843c749SSergey Zigachev 	/* Position Buffer */
1330b843c749SSergey Zigachev 	r = gfx_v9_0_ngg_create_buf(adev, &adev->gfx.ngg.buf[NGG_POS],
1331b843c749SSergey Zigachev 				    amdgpu_pos_buf_per_se,
1332b843c749SSergey Zigachev 				    256 * 1024);
1333b843c749SSergey Zigachev 	if (r) {
1334b843c749SSergey Zigachev 		dev_err(adev->dev, "Failed to create Position Buffer\n");
1335b843c749SSergey Zigachev 		goto err;
1336b843c749SSergey Zigachev 	}
1337b843c749SSergey Zigachev 
1338b843c749SSergey Zigachev 	/* Control Sideband */
1339b843c749SSergey Zigachev 	r = gfx_v9_0_ngg_create_buf(adev, &adev->gfx.ngg.buf[NGG_CNTL],
1340b843c749SSergey Zigachev 				    amdgpu_cntl_sb_buf_per_se,
1341b843c749SSergey Zigachev 				    256);
1342b843c749SSergey Zigachev 	if (r) {
1343b843c749SSergey Zigachev 		dev_err(adev->dev, "Failed to create Control Sideband Buffer\n");
1344b843c749SSergey Zigachev 		goto err;
1345b843c749SSergey Zigachev 	}
1346b843c749SSergey Zigachev 
1347b843c749SSergey Zigachev 	/* Parameter Cache, not created by default */
1348b843c749SSergey Zigachev 	if (amdgpu_param_buf_per_se <= 0)
1349b843c749SSergey Zigachev 		goto out;
1350b843c749SSergey Zigachev 
1351b843c749SSergey Zigachev 	r = gfx_v9_0_ngg_create_buf(adev, &adev->gfx.ngg.buf[NGG_PARAM],
1352b843c749SSergey Zigachev 				    amdgpu_param_buf_per_se,
1353b843c749SSergey Zigachev 				    512 * 1024);
1354b843c749SSergey Zigachev 	if (r) {
1355b843c749SSergey Zigachev 		dev_err(adev->dev, "Failed to create Parameter Cache\n");
1356b843c749SSergey Zigachev 		goto err;
1357b843c749SSergey Zigachev 	}
1358b843c749SSergey Zigachev 
1359b843c749SSergey Zigachev out:
1360b843c749SSergey Zigachev 	adev->gfx.ngg.init = true;
1361b843c749SSergey Zigachev 	return 0;
1362b843c749SSergey Zigachev err:
1363b843c749SSergey Zigachev 	gfx_v9_0_ngg_fini(adev);
1364b843c749SSergey Zigachev 	return r;
1365b843c749SSergey Zigachev }
1366b843c749SSergey Zigachev 
gfx_v9_0_ngg_en(struct amdgpu_device * adev)1367b843c749SSergey Zigachev static int gfx_v9_0_ngg_en(struct amdgpu_device *adev)
1368b843c749SSergey Zigachev {
1369b843c749SSergey Zigachev 	struct amdgpu_ring *ring = &adev->gfx.gfx_ring[0];
1370b843c749SSergey Zigachev 	int r;
1371b843c749SSergey Zigachev 	u32 data, base;
1372b843c749SSergey Zigachev 
1373b843c749SSergey Zigachev 	if (!amdgpu_ngg)
1374b843c749SSergey Zigachev 		return 0;
1375b843c749SSergey Zigachev 
1376b843c749SSergey Zigachev 	/* Program buffer size */
1377b843c749SSergey Zigachev 	data = REG_SET_FIELD(0, WD_BUF_RESOURCE_1, INDEX_BUF_SIZE,
1378b843c749SSergey Zigachev 			     adev->gfx.ngg.buf[NGG_PRIM].size >> 8);
1379b843c749SSergey Zigachev 	data = REG_SET_FIELD(data, WD_BUF_RESOURCE_1, POS_BUF_SIZE,
1380b843c749SSergey Zigachev 			     adev->gfx.ngg.buf[NGG_POS].size >> 8);
1381b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmWD_BUF_RESOURCE_1, data);
1382b843c749SSergey Zigachev 
1383b843c749SSergey Zigachev 	data = REG_SET_FIELD(0, WD_BUF_RESOURCE_2, CNTL_SB_BUF_SIZE,
1384b843c749SSergey Zigachev 			     adev->gfx.ngg.buf[NGG_CNTL].size >> 8);
1385b843c749SSergey Zigachev 	data = REG_SET_FIELD(data, WD_BUF_RESOURCE_2, PARAM_BUF_SIZE,
1386b843c749SSergey Zigachev 			     adev->gfx.ngg.buf[NGG_PARAM].size >> 10);
1387b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmWD_BUF_RESOURCE_2, data);
1388b843c749SSergey Zigachev 
1389b843c749SSergey Zigachev 	/* Program buffer base address */
1390b843c749SSergey Zigachev 	base = lower_32_bits(adev->gfx.ngg.buf[NGG_PRIM].gpu_addr);
1391b843c749SSergey Zigachev 	data = REG_SET_FIELD(0, WD_INDEX_BUF_BASE, BASE, base);
1392b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmWD_INDEX_BUF_BASE, data);
1393b843c749SSergey Zigachev 
1394b843c749SSergey Zigachev 	base = upper_32_bits(adev->gfx.ngg.buf[NGG_PRIM].gpu_addr);
1395b843c749SSergey Zigachev 	data = REG_SET_FIELD(0, WD_INDEX_BUF_BASE_HI, BASE_HI, base);
1396b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmWD_INDEX_BUF_BASE_HI, data);
1397b843c749SSergey Zigachev 
1398b843c749SSergey Zigachev 	base = lower_32_bits(adev->gfx.ngg.buf[NGG_POS].gpu_addr);
1399b843c749SSergey Zigachev 	data = REG_SET_FIELD(0, WD_POS_BUF_BASE, BASE, base);
1400b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmWD_POS_BUF_BASE, data);
1401b843c749SSergey Zigachev 
1402b843c749SSergey Zigachev 	base = upper_32_bits(adev->gfx.ngg.buf[NGG_POS].gpu_addr);
1403b843c749SSergey Zigachev 	data = REG_SET_FIELD(0, WD_POS_BUF_BASE_HI, BASE_HI, base);
1404b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmWD_POS_BUF_BASE_HI, data);
1405b843c749SSergey Zigachev 
1406b843c749SSergey Zigachev 	base = lower_32_bits(adev->gfx.ngg.buf[NGG_CNTL].gpu_addr);
1407b843c749SSergey Zigachev 	data = REG_SET_FIELD(0, WD_CNTL_SB_BUF_BASE, BASE, base);
1408b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmWD_CNTL_SB_BUF_BASE, data);
1409b843c749SSergey Zigachev 
1410b843c749SSergey Zigachev 	base = upper_32_bits(adev->gfx.ngg.buf[NGG_CNTL].gpu_addr);
1411b843c749SSergey Zigachev 	data = REG_SET_FIELD(0, WD_CNTL_SB_BUF_BASE_HI, BASE_HI, base);
1412b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmWD_CNTL_SB_BUF_BASE_HI, data);
1413b843c749SSergey Zigachev 
1414b843c749SSergey Zigachev 	/* Clear GDS reserved memory */
1415b843c749SSergey Zigachev 	r = amdgpu_ring_alloc(ring, 17);
1416b843c749SSergey Zigachev 	if (r) {
1417b843c749SSergey Zigachev 		DRM_ERROR("amdgpu: NGG failed to lock ring %d (%d).\n",
1418b843c749SSergey Zigachev 			  ring->idx, r);
1419b843c749SSergey Zigachev 		return r;
1420b843c749SSergey Zigachev 	}
1421b843c749SSergey Zigachev 
1422b843c749SSergey Zigachev 	gfx_v9_0_write_data_to_reg(ring, 0, false,
1423b843c749SSergey Zigachev 				   SOC15_REG_OFFSET(GC, 0, mmGDS_VMID0_SIZE),
1424b843c749SSergey Zigachev 			           (adev->gds.mem.total_size +
1425b843c749SSergey Zigachev 				    adev->gfx.ngg.gds_reserve_size) >>
1426b843c749SSergey Zigachev 				   AMDGPU_GDS_SHIFT);
1427b843c749SSergey Zigachev 
1428b843c749SSergey Zigachev 	amdgpu_ring_write(ring, PACKET3(PACKET3_DMA_DATA, 5));
1429b843c749SSergey Zigachev 	amdgpu_ring_write(ring, (PACKET3_DMA_DATA_CP_SYNC |
1430b843c749SSergey Zigachev 				PACKET3_DMA_DATA_DST_SEL(1) |
1431b843c749SSergey Zigachev 				PACKET3_DMA_DATA_SRC_SEL(2)));
1432b843c749SSergey Zigachev 	amdgpu_ring_write(ring, 0);
1433b843c749SSergey Zigachev 	amdgpu_ring_write(ring, 0);
1434b843c749SSergey Zigachev 	amdgpu_ring_write(ring, adev->gfx.ngg.gds_reserve_addr);
1435b843c749SSergey Zigachev 	amdgpu_ring_write(ring, 0);
1436b843c749SSergey Zigachev 	amdgpu_ring_write(ring, PACKET3_DMA_DATA_CMD_RAW_WAIT |
1437b843c749SSergey Zigachev 				adev->gfx.ngg.gds_reserve_size);
1438b843c749SSergey Zigachev 
1439b843c749SSergey Zigachev 	gfx_v9_0_write_data_to_reg(ring, 0, false,
1440b843c749SSergey Zigachev 				   SOC15_REG_OFFSET(GC, 0, mmGDS_VMID0_SIZE), 0);
1441b843c749SSergey Zigachev 
1442b843c749SSergey Zigachev 	amdgpu_ring_commit(ring);
1443b843c749SSergey Zigachev 
1444b843c749SSergey Zigachev 	return 0;
1445b843c749SSergey Zigachev }
1446b843c749SSergey Zigachev 
gfx_v9_0_compute_ring_init(struct amdgpu_device * adev,int ring_id,int mec,int pipe,int queue)1447b843c749SSergey Zigachev static int gfx_v9_0_compute_ring_init(struct amdgpu_device *adev, int ring_id,
1448b843c749SSergey Zigachev 				      int mec, int pipe, int queue)
1449b843c749SSergey Zigachev {
1450b843c749SSergey Zigachev 	int r;
1451b843c749SSergey Zigachev 	unsigned irq_type;
1452b843c749SSergey Zigachev 	struct amdgpu_ring *ring = &adev->gfx.compute_ring[ring_id];
1453b843c749SSergey Zigachev 
1454b843c749SSergey Zigachev 	ring = &adev->gfx.compute_ring[ring_id];
1455b843c749SSergey Zigachev 
1456b843c749SSergey Zigachev 	/* mec0 is me1 */
1457b843c749SSergey Zigachev 	ring->me = mec + 1;
1458b843c749SSergey Zigachev 	ring->pipe = pipe;
1459b843c749SSergey Zigachev 	ring->queue = queue;
1460b843c749SSergey Zigachev 
1461b843c749SSergey Zigachev 	ring->ring_obj = NULL;
1462b843c749SSergey Zigachev 	ring->use_doorbell = true;
1463b843c749SSergey Zigachev 	ring->doorbell_index = (AMDGPU_DOORBELL_MEC_RING0 + ring_id) << 1;
1464b843c749SSergey Zigachev 	ring->eop_gpu_addr = adev->gfx.mec.hpd_eop_gpu_addr
1465b843c749SSergey Zigachev 				+ (ring_id * GFX9_MEC_HPD_SIZE);
1466b843c749SSergey Zigachev 	sprintf(ring->name, "comp_%d.%d.%d", ring->me, ring->pipe, ring->queue);
1467b843c749SSergey Zigachev 
1468b843c749SSergey Zigachev 	irq_type = AMDGPU_CP_IRQ_COMPUTE_MEC1_PIPE0_EOP
1469b843c749SSergey Zigachev 		+ ((ring->me - 1) * adev->gfx.mec.num_pipe_per_mec)
1470b843c749SSergey Zigachev 		+ ring->pipe;
1471b843c749SSergey Zigachev 
1472b843c749SSergey Zigachev 	/* type-2 packets are deprecated on MEC, use type-3 instead */
1473b843c749SSergey Zigachev 	r = amdgpu_ring_init(adev, ring, 1024,
1474b843c749SSergey Zigachev 			     &adev->gfx.eop_irq, irq_type);
1475b843c749SSergey Zigachev 	if (r)
1476b843c749SSergey Zigachev 		return r;
1477b843c749SSergey Zigachev 
1478b843c749SSergey Zigachev 
1479b843c749SSergey Zigachev 	return 0;
1480b843c749SSergey Zigachev }
1481b843c749SSergey Zigachev 
gfx_v9_0_sw_init(void * handle)1482b843c749SSergey Zigachev static int gfx_v9_0_sw_init(void *handle)
1483b843c749SSergey Zigachev {
1484b843c749SSergey Zigachev 	int i, j, k, r, ring_id;
1485b843c749SSergey Zigachev 	struct amdgpu_ring *ring;
1486b843c749SSergey Zigachev 	struct amdgpu_kiq *kiq;
1487b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1488b843c749SSergey Zigachev 
1489b843c749SSergey Zigachev 	switch (adev->asic_type) {
1490b843c749SSergey Zigachev 	case CHIP_VEGA10:
1491b843c749SSergey Zigachev 	case CHIP_VEGA12:
1492b843c749SSergey Zigachev 	case CHIP_VEGA20:
1493b843c749SSergey Zigachev 	case CHIP_RAVEN:
1494b843c749SSergey Zigachev 		adev->gfx.mec.num_mec = 2;
1495b843c749SSergey Zigachev 		break;
1496b843c749SSergey Zigachev 	default:
1497b843c749SSergey Zigachev 		adev->gfx.mec.num_mec = 1;
1498b843c749SSergey Zigachev 		break;
1499b843c749SSergey Zigachev 	}
1500b843c749SSergey Zigachev 
1501b843c749SSergey Zigachev 	adev->gfx.mec.num_pipe_per_mec = 4;
1502b843c749SSergey Zigachev 	adev->gfx.mec.num_queue_per_pipe = 8;
1503b843c749SSergey Zigachev 
1504b843c749SSergey Zigachev 	/* KIQ event */
1505b843c749SSergey Zigachev 	r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_GRBM_CP, GFX_9_0__SRCID__CP_IB2_INTERRUPT_PKT, &adev->gfx.kiq.irq);
1506b843c749SSergey Zigachev 	if (r)
1507b843c749SSergey Zigachev 		return r;
1508b843c749SSergey Zigachev 
1509b843c749SSergey Zigachev 	/* EOP Event */
1510b843c749SSergey Zigachev 	r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_GRBM_CP, GFX_9_0__SRCID__CP_EOP_INTERRUPT, &adev->gfx.eop_irq);
1511b843c749SSergey Zigachev 	if (r)
1512b843c749SSergey Zigachev 		return r;
1513b843c749SSergey Zigachev 
1514b843c749SSergey Zigachev 	/* Privileged reg */
1515b843c749SSergey Zigachev 	r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_GRBM_CP, GFX_9_0__SRCID__CP_PRIV_REG_FAULT,
1516b843c749SSergey Zigachev 			      &adev->gfx.priv_reg_irq);
1517b843c749SSergey Zigachev 	if (r)
1518b843c749SSergey Zigachev 		return r;
1519b843c749SSergey Zigachev 
1520b843c749SSergey Zigachev 	/* Privileged inst */
1521b843c749SSergey Zigachev 	r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_GRBM_CP, GFX_9_0__SRCID__CP_PRIV_INSTR_FAULT,
1522b843c749SSergey Zigachev 			      &adev->gfx.priv_inst_irq);
1523b843c749SSergey Zigachev 	if (r)
1524b843c749SSergey Zigachev 		return r;
1525b843c749SSergey Zigachev 
1526b843c749SSergey Zigachev 	adev->gfx.gfx_current_status = AMDGPU_GFX_NORMAL_MODE;
1527b843c749SSergey Zigachev 
1528b843c749SSergey Zigachev 	gfx_v9_0_scratch_init(adev);
1529b843c749SSergey Zigachev 
1530b843c749SSergey Zigachev 	r = gfx_v9_0_init_microcode(adev);
1531b843c749SSergey Zigachev 	if (r) {
1532b843c749SSergey Zigachev 		DRM_ERROR("Failed to load gfx firmware!\n");
1533b843c749SSergey Zigachev 		return r;
1534b843c749SSergey Zigachev 	}
1535b843c749SSergey Zigachev 
1536b843c749SSergey Zigachev 	r = gfx_v9_0_rlc_init(adev);
1537b843c749SSergey Zigachev 	if (r) {
1538b843c749SSergey Zigachev 		DRM_ERROR("Failed to init rlc BOs!\n");
1539b843c749SSergey Zigachev 		return r;
1540b843c749SSergey Zigachev 	}
1541b843c749SSergey Zigachev 
1542b843c749SSergey Zigachev 	r = gfx_v9_0_mec_init(adev);
1543b843c749SSergey Zigachev 	if (r) {
1544b843c749SSergey Zigachev 		DRM_ERROR("Failed to init MEC BOs!\n");
1545b843c749SSergey Zigachev 		return r;
1546b843c749SSergey Zigachev 	}
1547b843c749SSergey Zigachev 
1548b843c749SSergey Zigachev 	/* set up the gfx ring */
1549b843c749SSergey Zigachev 	for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
1550b843c749SSergey Zigachev 		ring = &adev->gfx.gfx_ring[i];
1551b843c749SSergey Zigachev 		ring->ring_obj = NULL;
1552b843c749SSergey Zigachev 		if (!i)
1553b843c749SSergey Zigachev 			sprintf(ring->name, "gfx");
1554b843c749SSergey Zigachev 		else
1555b843c749SSergey Zigachev 			sprintf(ring->name, "gfx_%d", i);
1556b843c749SSergey Zigachev 		ring->use_doorbell = true;
1557b843c749SSergey Zigachev 		ring->doorbell_index = AMDGPU_DOORBELL64_GFX_RING0 << 1;
1558b843c749SSergey Zigachev 		r = amdgpu_ring_init(adev, ring, 1024,
1559b843c749SSergey Zigachev 				     &adev->gfx.eop_irq, AMDGPU_CP_IRQ_GFX_EOP);
1560b843c749SSergey Zigachev 		if (r)
1561b843c749SSergey Zigachev 			return r;
1562b843c749SSergey Zigachev 	}
1563b843c749SSergey Zigachev 
1564b843c749SSergey Zigachev 	/* set up the compute queues - allocate horizontally across pipes */
1565b843c749SSergey Zigachev 	ring_id = 0;
1566b843c749SSergey Zigachev 	for (i = 0; i < adev->gfx.mec.num_mec; ++i) {
1567b843c749SSergey Zigachev 		for (j = 0; j < adev->gfx.mec.num_queue_per_pipe; j++) {
1568b843c749SSergey Zigachev 			for (k = 0; k < adev->gfx.mec.num_pipe_per_mec; k++) {
1569b843c749SSergey Zigachev 				if (!amdgpu_gfx_is_mec_queue_enabled(adev, i, k, j))
1570b843c749SSergey Zigachev 					continue;
1571b843c749SSergey Zigachev 
1572b843c749SSergey Zigachev 				r = gfx_v9_0_compute_ring_init(adev,
1573b843c749SSergey Zigachev 							       ring_id,
1574b843c749SSergey Zigachev 							       i, k, j);
1575b843c749SSergey Zigachev 				if (r)
1576b843c749SSergey Zigachev 					return r;
1577b843c749SSergey Zigachev 
1578b843c749SSergey Zigachev 				ring_id++;
1579b843c749SSergey Zigachev 			}
1580b843c749SSergey Zigachev 		}
1581b843c749SSergey Zigachev 	}
1582b843c749SSergey Zigachev 
1583b843c749SSergey Zigachev 	r = amdgpu_gfx_kiq_init(adev, GFX9_MEC_HPD_SIZE);
1584b843c749SSergey Zigachev 	if (r) {
1585b843c749SSergey Zigachev 		DRM_ERROR("Failed to init KIQ BOs!\n");
1586b843c749SSergey Zigachev 		return r;
1587b843c749SSergey Zigachev 	}
1588b843c749SSergey Zigachev 
1589b843c749SSergey Zigachev 	kiq = &adev->gfx.kiq;
1590b843c749SSergey Zigachev 	r = amdgpu_gfx_kiq_init_ring(adev, &kiq->ring, &kiq->irq);
1591b843c749SSergey Zigachev 	if (r)
1592b843c749SSergey Zigachev 		return r;
1593b843c749SSergey Zigachev 
1594b843c749SSergey Zigachev 	/* create MQD for all compute queues as wel as KIQ for SRIOV case */
1595b843c749SSergey Zigachev 	r = amdgpu_gfx_compute_mqd_sw_init(adev, sizeof(struct v9_mqd_allocation));
1596b843c749SSergey Zigachev 	if (r)
1597b843c749SSergey Zigachev 		return r;
1598b843c749SSergey Zigachev 
1599b843c749SSergey Zigachev 	/* reserve GDS, GWS and OA resource for gfx */
1600b843c749SSergey Zigachev 	r = amdgpu_bo_create_kernel(adev, adev->gds.mem.gfx_partition_size,
1601b843c749SSergey Zigachev 				    PAGE_SIZE, AMDGPU_GEM_DOMAIN_GDS,
1602b843c749SSergey Zigachev 				    &adev->gds.gds_gfx_bo, NULL, NULL);
1603b843c749SSergey Zigachev 	if (r)
1604b843c749SSergey Zigachev 		return r;
1605b843c749SSergey Zigachev 
1606b843c749SSergey Zigachev 	r = amdgpu_bo_create_kernel(adev, adev->gds.gws.gfx_partition_size,
1607b843c749SSergey Zigachev 				    PAGE_SIZE, AMDGPU_GEM_DOMAIN_GWS,
1608b843c749SSergey Zigachev 				    &adev->gds.gws_gfx_bo, NULL, NULL);
1609b843c749SSergey Zigachev 	if (r)
1610b843c749SSergey Zigachev 		return r;
1611b843c749SSergey Zigachev 
1612b843c749SSergey Zigachev 	r = amdgpu_bo_create_kernel(adev, adev->gds.oa.gfx_partition_size,
1613b843c749SSergey Zigachev 				    PAGE_SIZE, AMDGPU_GEM_DOMAIN_OA,
1614b843c749SSergey Zigachev 				    &adev->gds.oa_gfx_bo, NULL, NULL);
1615b843c749SSergey Zigachev 	if (r)
1616b843c749SSergey Zigachev 		return r;
1617b843c749SSergey Zigachev 
1618b843c749SSergey Zigachev 	adev->gfx.ce_ram_size = 0x8000;
1619b843c749SSergey Zigachev 
1620b843c749SSergey Zigachev 	r = gfx_v9_0_gpu_early_init(adev);
1621b843c749SSergey Zigachev 	if (r)
1622b843c749SSergey Zigachev 		return r;
1623b843c749SSergey Zigachev 
1624b843c749SSergey Zigachev 	r = gfx_v9_0_ngg_init(adev);
1625b843c749SSergey Zigachev 	if (r)
1626b843c749SSergey Zigachev 		return r;
1627b843c749SSergey Zigachev 
1628b843c749SSergey Zigachev 	return 0;
1629b843c749SSergey Zigachev }
1630b843c749SSergey Zigachev 
1631b843c749SSergey Zigachev 
gfx_v9_0_sw_fini(void * handle)1632b843c749SSergey Zigachev static int gfx_v9_0_sw_fini(void *handle)
1633b843c749SSergey Zigachev {
1634b843c749SSergey Zigachev 	int i;
1635b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1636b843c749SSergey Zigachev 
1637b843c749SSergey Zigachev 	amdgpu_bo_free_kernel(&adev->gds.oa_gfx_bo, NULL, NULL);
1638b843c749SSergey Zigachev 	amdgpu_bo_free_kernel(&adev->gds.gws_gfx_bo, NULL, NULL);
1639b843c749SSergey Zigachev 	amdgpu_bo_free_kernel(&adev->gds.gds_gfx_bo, NULL, NULL);
1640b843c749SSergey Zigachev 
1641b843c749SSergey Zigachev 	for (i = 0; i < adev->gfx.num_gfx_rings; i++)
1642b843c749SSergey Zigachev 		amdgpu_ring_fini(&adev->gfx.gfx_ring[i]);
1643b843c749SSergey Zigachev 	for (i = 0; i < adev->gfx.num_compute_rings; i++)
1644b843c749SSergey Zigachev 		amdgpu_ring_fini(&adev->gfx.compute_ring[i]);
1645b843c749SSergey Zigachev 
1646b843c749SSergey Zigachev 	amdgpu_gfx_compute_mqd_sw_fini(adev);
1647b843c749SSergey Zigachev 	amdgpu_gfx_kiq_free_ring(&adev->gfx.kiq.ring, &adev->gfx.kiq.irq);
1648b843c749SSergey Zigachev 	amdgpu_gfx_kiq_fini(adev);
1649b843c749SSergey Zigachev 
1650b843c749SSergey Zigachev 	gfx_v9_0_mec_fini(adev);
1651b843c749SSergey Zigachev 	gfx_v9_0_ngg_fini(adev);
1652b843c749SSergey Zigachev 	amdgpu_bo_free_kernel(&adev->gfx.rlc.clear_state_obj,
165378973132SSergey Zigachev 				(u64 *)&adev->gfx.rlc.clear_state_gpu_addr,
1654b843c749SSergey Zigachev 				(void **)&adev->gfx.rlc.cs_ptr);
1655b843c749SSergey Zigachev 	if (adev->asic_type == CHIP_RAVEN) {
1656b843c749SSergey Zigachev 		amdgpu_bo_free_kernel(&adev->gfx.rlc.cp_table_obj,
165778973132SSergey Zigachev 				(u64 *)&adev->gfx.rlc.cp_table_gpu_addr,
1658b843c749SSergey Zigachev 				(void **)&adev->gfx.rlc.cp_table_ptr);
1659b843c749SSergey Zigachev 	}
1660b843c749SSergey Zigachev 	gfx_v9_0_free_microcode(adev);
1661b843c749SSergey Zigachev 
1662b843c749SSergey Zigachev 	return 0;
1663b843c749SSergey Zigachev }
1664b843c749SSergey Zigachev 
1665b843c749SSergey Zigachev 
gfx_v9_0_tiling_mode_table_init(struct amdgpu_device * adev)1666b843c749SSergey Zigachev static void gfx_v9_0_tiling_mode_table_init(struct amdgpu_device *adev)
1667b843c749SSergey Zigachev {
1668b843c749SSergey Zigachev 	/* TODO */
1669b843c749SSergey Zigachev }
1670b843c749SSergey Zigachev 
gfx_v9_0_select_se_sh(struct amdgpu_device * adev,u32 se_num,u32 sh_num,u32 instance)1671b843c749SSergey Zigachev static void gfx_v9_0_select_se_sh(struct amdgpu_device *adev, u32 se_num, u32 sh_num, u32 instance)
1672b843c749SSergey Zigachev {
1673b843c749SSergey Zigachev 	u32 data;
1674b843c749SSergey Zigachev 
1675b843c749SSergey Zigachev 	if (instance == 0xffffffff)
1676b843c749SSergey Zigachev 		data = REG_SET_FIELD(0, GRBM_GFX_INDEX, INSTANCE_BROADCAST_WRITES, 1);
1677b843c749SSergey Zigachev 	else
1678b843c749SSergey Zigachev 		data = REG_SET_FIELD(0, GRBM_GFX_INDEX, INSTANCE_INDEX, instance);
1679b843c749SSergey Zigachev 
1680b843c749SSergey Zigachev 	if (se_num == 0xffffffff)
1681b843c749SSergey Zigachev 		data = REG_SET_FIELD(data, GRBM_GFX_INDEX, SE_BROADCAST_WRITES, 1);
1682b843c749SSergey Zigachev 	else
1683b843c749SSergey Zigachev 		data = REG_SET_FIELD(data, GRBM_GFX_INDEX, SE_INDEX, se_num);
1684b843c749SSergey Zigachev 
1685b843c749SSergey Zigachev 	if (sh_num == 0xffffffff)
1686b843c749SSergey Zigachev 		data = REG_SET_FIELD(data, GRBM_GFX_INDEX, SH_BROADCAST_WRITES, 1);
1687b843c749SSergey Zigachev 	else
1688b843c749SSergey Zigachev 		data = REG_SET_FIELD(data, GRBM_GFX_INDEX, SH_INDEX, sh_num);
1689b843c749SSergey Zigachev 
1690b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmGRBM_GFX_INDEX, data);
1691b843c749SSergey Zigachev }
1692b843c749SSergey Zigachev 
gfx_v9_0_get_rb_active_bitmap(struct amdgpu_device * adev)1693b843c749SSergey Zigachev static u32 gfx_v9_0_get_rb_active_bitmap(struct amdgpu_device *adev)
1694b843c749SSergey Zigachev {
1695b843c749SSergey Zigachev 	u32 data, mask;
1696b843c749SSergey Zigachev 
1697b843c749SSergey Zigachev 	data = RREG32_SOC15(GC, 0, mmCC_RB_BACKEND_DISABLE);
1698b843c749SSergey Zigachev 	data |= RREG32_SOC15(GC, 0, mmGC_USER_RB_BACKEND_DISABLE);
1699b843c749SSergey Zigachev 
1700b843c749SSergey Zigachev 	data &= CC_RB_BACKEND_DISABLE__BACKEND_DISABLE_MASK;
1701b843c749SSergey Zigachev 	data >>= GC_USER_RB_BACKEND_DISABLE__BACKEND_DISABLE__SHIFT;
1702b843c749SSergey Zigachev 
1703b843c749SSergey Zigachev 	mask = amdgpu_gfx_create_bitmask(adev->gfx.config.max_backends_per_se /
1704b843c749SSergey Zigachev 					 adev->gfx.config.max_sh_per_se);
1705b843c749SSergey Zigachev 
1706b843c749SSergey Zigachev 	return (~data) & mask;
1707b843c749SSergey Zigachev }
1708b843c749SSergey Zigachev 
gfx_v9_0_setup_rb(struct amdgpu_device * adev)1709b843c749SSergey Zigachev static void gfx_v9_0_setup_rb(struct amdgpu_device *adev)
1710b843c749SSergey Zigachev {
1711b843c749SSergey Zigachev 	int i, j;
1712b843c749SSergey Zigachev 	u32 data;
1713b843c749SSergey Zigachev 	u32 active_rbs = 0;
1714b843c749SSergey Zigachev 	u32 rb_bitmap_width_per_sh = adev->gfx.config.max_backends_per_se /
1715b843c749SSergey Zigachev 					adev->gfx.config.max_sh_per_se;
1716b843c749SSergey Zigachev 
1717b843c749SSergey Zigachev 	mutex_lock(&adev->grbm_idx_mutex);
1718b843c749SSergey Zigachev 	for (i = 0; i < adev->gfx.config.max_shader_engines; i++) {
1719b843c749SSergey Zigachev 		for (j = 0; j < adev->gfx.config.max_sh_per_se; j++) {
1720b843c749SSergey Zigachev 			gfx_v9_0_select_se_sh(adev, i, j, 0xffffffff);
1721b843c749SSergey Zigachev 			data = gfx_v9_0_get_rb_active_bitmap(adev);
1722b843c749SSergey Zigachev 			active_rbs |= data << ((i * adev->gfx.config.max_sh_per_se + j) *
1723b843c749SSergey Zigachev 					       rb_bitmap_width_per_sh);
1724b843c749SSergey Zigachev 		}
1725b843c749SSergey Zigachev 	}
1726b843c749SSergey Zigachev 	gfx_v9_0_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
1727b843c749SSergey Zigachev 	mutex_unlock(&adev->grbm_idx_mutex);
1728b843c749SSergey Zigachev 
1729b843c749SSergey Zigachev 	adev->gfx.config.backend_enable_mask = active_rbs;
1730b843c749SSergey Zigachev 	adev->gfx.config.num_rbs = hweight32(active_rbs);
1731b843c749SSergey Zigachev }
1732b843c749SSergey Zigachev 
1733b843c749SSergey Zigachev #define DEFAULT_SH_MEM_BASES	(0x6000)
1734b843c749SSergey Zigachev #define FIRST_COMPUTE_VMID	(8)
1735b843c749SSergey Zigachev #define LAST_COMPUTE_VMID	(16)
gfx_v9_0_init_compute_vmid(struct amdgpu_device * adev)1736b843c749SSergey Zigachev static void gfx_v9_0_init_compute_vmid(struct amdgpu_device *adev)
1737b843c749SSergey Zigachev {
1738b843c749SSergey Zigachev 	int i;
1739b843c749SSergey Zigachev 	uint32_t sh_mem_config;
1740b843c749SSergey Zigachev 	uint32_t sh_mem_bases;
1741b843c749SSergey Zigachev 
1742b843c749SSergey Zigachev 	/*
1743b843c749SSergey Zigachev 	 * Configure apertures:
1744b843c749SSergey Zigachev 	 * LDS:         0x60000000'00000000 - 0x60000001'00000000 (4GB)
1745b843c749SSergey Zigachev 	 * Scratch:     0x60000001'00000000 - 0x60000002'00000000 (4GB)
1746b843c749SSergey Zigachev 	 * GPUVM:       0x60010000'00000000 - 0x60020000'00000000 (1TB)
1747b843c749SSergey Zigachev 	 */
1748b843c749SSergey Zigachev 	sh_mem_bases = DEFAULT_SH_MEM_BASES | (DEFAULT_SH_MEM_BASES << 16);
1749b843c749SSergey Zigachev 
1750b843c749SSergey Zigachev 	sh_mem_config = SH_MEM_ADDRESS_MODE_64 |
1751b843c749SSergey Zigachev 			SH_MEM_ALIGNMENT_MODE_UNALIGNED <<
1752b843c749SSergey Zigachev 			SH_MEM_CONFIG__ALIGNMENT_MODE__SHIFT;
1753b843c749SSergey Zigachev 
1754b843c749SSergey Zigachev 	mutex_lock(&adev->srbm_mutex);
1755b843c749SSergey Zigachev 	for (i = FIRST_COMPUTE_VMID; i < LAST_COMPUTE_VMID; i++) {
1756b843c749SSergey Zigachev 		soc15_grbm_select(adev, 0, 0, 0, i);
1757b843c749SSergey Zigachev 		/* CP and shaders */
1758b843c749SSergey Zigachev 		WREG32_SOC15(GC, 0, mmSH_MEM_CONFIG, sh_mem_config);
1759b843c749SSergey Zigachev 		WREG32_SOC15(GC, 0, mmSH_MEM_BASES, sh_mem_bases);
1760b843c749SSergey Zigachev 	}
1761b843c749SSergey Zigachev 	soc15_grbm_select(adev, 0, 0, 0, 0);
1762b843c749SSergey Zigachev 	mutex_unlock(&adev->srbm_mutex);
1763b843c749SSergey Zigachev }
1764b843c749SSergey Zigachev 
gfx_v9_0_gpu_init(struct amdgpu_device * adev)1765b843c749SSergey Zigachev static void gfx_v9_0_gpu_init(struct amdgpu_device *adev)
1766b843c749SSergey Zigachev {
1767b843c749SSergey Zigachev 	u32 tmp;
1768b843c749SSergey Zigachev 	int i;
1769b843c749SSergey Zigachev 
1770b843c749SSergey Zigachev 	WREG32_FIELD15(GC, 0, GRBM_CNTL, READ_TIMEOUT, 0xff);
1771b843c749SSergey Zigachev 
1772b843c749SSergey Zigachev 	gfx_v9_0_tiling_mode_table_init(adev);
1773b843c749SSergey Zigachev 
1774b843c749SSergey Zigachev 	gfx_v9_0_setup_rb(adev);
1775b843c749SSergey Zigachev 	gfx_v9_0_get_cu_info(adev, &adev->gfx.cu_info);
1776b843c749SSergey Zigachev 	adev->gfx.config.db_debug2 = RREG32_SOC15(GC, 0, mmDB_DEBUG2);
1777b843c749SSergey Zigachev 
1778b843c749SSergey Zigachev 	/* XXX SH_MEM regs */
1779b843c749SSergey Zigachev 	/* where to put LDS, scratch, GPUVM in FSA64 space */
1780b843c749SSergey Zigachev 	mutex_lock(&adev->srbm_mutex);
1781b843c749SSergey Zigachev 	for (i = 0; i < adev->vm_manager.id_mgr[AMDGPU_GFXHUB].num_ids; i++) {
1782b843c749SSergey Zigachev 		soc15_grbm_select(adev, 0, 0, 0, i);
1783b843c749SSergey Zigachev 		/* CP and shaders */
1784b843c749SSergey Zigachev 		if (i == 0) {
1785b843c749SSergey Zigachev 			tmp = REG_SET_FIELD(0, SH_MEM_CONFIG, ALIGNMENT_MODE,
1786b843c749SSergey Zigachev 					    SH_MEM_ALIGNMENT_MODE_UNALIGNED);
1787b843c749SSergey Zigachev 			WREG32_SOC15(GC, 0, mmSH_MEM_CONFIG, tmp);
1788b843c749SSergey Zigachev 			WREG32_SOC15(GC, 0, mmSH_MEM_BASES, 0);
1789b843c749SSergey Zigachev 		} else {
1790b843c749SSergey Zigachev 			tmp = REG_SET_FIELD(0, SH_MEM_CONFIG, ALIGNMENT_MODE,
1791b843c749SSergey Zigachev 					    SH_MEM_ALIGNMENT_MODE_UNALIGNED);
1792b843c749SSergey Zigachev 			WREG32_SOC15(GC, 0, mmSH_MEM_CONFIG, tmp);
1793b843c749SSergey Zigachev 			tmp = REG_SET_FIELD(0, SH_MEM_BASES, PRIVATE_BASE,
1794b843c749SSergey Zigachev 				(adev->gmc.private_aperture_start >> 48));
1795b843c749SSergey Zigachev 			tmp = REG_SET_FIELD(tmp, SH_MEM_BASES, SHARED_BASE,
1796b843c749SSergey Zigachev 				(adev->gmc.shared_aperture_start >> 48));
1797b843c749SSergey Zigachev 			WREG32_SOC15(GC, 0, mmSH_MEM_BASES, tmp);
1798b843c749SSergey Zigachev 		}
1799b843c749SSergey Zigachev 	}
1800b843c749SSergey Zigachev 	soc15_grbm_select(adev, 0, 0, 0, 0);
1801b843c749SSergey Zigachev 
1802b843c749SSergey Zigachev 	mutex_unlock(&adev->srbm_mutex);
1803b843c749SSergey Zigachev 
1804b843c749SSergey Zigachev 	gfx_v9_0_init_compute_vmid(adev);
1805b843c749SSergey Zigachev }
1806b843c749SSergey Zigachev 
gfx_v9_0_wait_for_rlc_serdes(struct amdgpu_device * adev)1807b843c749SSergey Zigachev static void gfx_v9_0_wait_for_rlc_serdes(struct amdgpu_device *adev)
1808b843c749SSergey Zigachev {
1809b843c749SSergey Zigachev 	u32 i, j, k;
1810b843c749SSergey Zigachev 	u32 mask;
1811b843c749SSergey Zigachev 
1812b843c749SSergey Zigachev 	mutex_lock(&adev->grbm_idx_mutex);
1813b843c749SSergey Zigachev 	for (i = 0; i < adev->gfx.config.max_shader_engines; i++) {
1814b843c749SSergey Zigachev 		for (j = 0; j < adev->gfx.config.max_sh_per_se; j++) {
1815b843c749SSergey Zigachev 			gfx_v9_0_select_se_sh(adev, i, j, 0xffffffff);
1816b843c749SSergey Zigachev 			for (k = 0; k < adev->usec_timeout; k++) {
1817b843c749SSergey Zigachev 				if (RREG32_SOC15(GC, 0, mmRLC_SERDES_CU_MASTER_BUSY) == 0)
1818b843c749SSergey Zigachev 					break;
1819b843c749SSergey Zigachev 				udelay(1);
1820b843c749SSergey Zigachev 			}
1821b843c749SSergey Zigachev 			if (k == adev->usec_timeout) {
1822b843c749SSergey Zigachev 				gfx_v9_0_select_se_sh(adev, 0xffffffff,
1823b843c749SSergey Zigachev 						      0xffffffff, 0xffffffff);
1824b843c749SSergey Zigachev 				mutex_unlock(&adev->grbm_idx_mutex);
1825b843c749SSergey Zigachev 				DRM_INFO("Timeout wait for RLC serdes %u,%u\n",
1826b843c749SSergey Zigachev 					 i, j);
1827b843c749SSergey Zigachev 				return;
1828b843c749SSergey Zigachev 			}
1829b843c749SSergey Zigachev 		}
1830b843c749SSergey Zigachev 	}
1831b843c749SSergey Zigachev 	gfx_v9_0_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
1832b843c749SSergey Zigachev 	mutex_unlock(&adev->grbm_idx_mutex);
1833b843c749SSergey Zigachev 
1834b843c749SSergey Zigachev 	mask = RLC_SERDES_NONCU_MASTER_BUSY__SE_MASTER_BUSY_MASK |
1835b843c749SSergey Zigachev 		RLC_SERDES_NONCU_MASTER_BUSY__GC_MASTER_BUSY_MASK |
1836b843c749SSergey Zigachev 		RLC_SERDES_NONCU_MASTER_BUSY__TC0_MASTER_BUSY_MASK |
1837b843c749SSergey Zigachev 		RLC_SERDES_NONCU_MASTER_BUSY__TC1_MASTER_BUSY_MASK;
1838b843c749SSergey Zigachev 	for (k = 0; k < adev->usec_timeout; k++) {
1839b843c749SSergey Zigachev 		if ((RREG32_SOC15(GC, 0, mmRLC_SERDES_NONCU_MASTER_BUSY) & mask) == 0)
1840b843c749SSergey Zigachev 			break;
1841b843c749SSergey Zigachev 		udelay(1);
1842b843c749SSergey Zigachev 	}
1843b843c749SSergey Zigachev }
1844b843c749SSergey Zigachev 
gfx_v9_0_enable_gui_idle_interrupt(struct amdgpu_device * adev,bool enable)1845b843c749SSergey Zigachev static void gfx_v9_0_enable_gui_idle_interrupt(struct amdgpu_device *adev,
1846b843c749SSergey Zigachev 					       bool enable)
1847b843c749SSergey Zigachev {
1848b843c749SSergey Zigachev 	u32 tmp = RREG32_SOC15(GC, 0, mmCP_INT_CNTL_RING0);
1849b843c749SSergey Zigachev 
1850b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, CP_INT_CNTL_RING0, CNTX_BUSY_INT_ENABLE, enable ? 1 : 0);
1851b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, CP_INT_CNTL_RING0, CNTX_EMPTY_INT_ENABLE, enable ? 1 : 0);
1852b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, CP_INT_CNTL_RING0, CMP_BUSY_INT_ENABLE, enable ? 1 : 0);
1853b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, CP_INT_CNTL_RING0, GFX_IDLE_INT_ENABLE, enable ? 1 : 0);
1854b843c749SSergey Zigachev 
1855b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_INT_CNTL_RING0, tmp);
1856b843c749SSergey Zigachev }
1857b843c749SSergey Zigachev 
gfx_v9_0_init_csb(struct amdgpu_device * adev)1858b843c749SSergey Zigachev static void gfx_v9_0_init_csb(struct amdgpu_device *adev)
1859b843c749SSergey Zigachev {
1860b843c749SSergey Zigachev 	/* csib */
1861b843c749SSergey Zigachev 	WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_CSIB_ADDR_HI),
1862b843c749SSergey Zigachev 			adev->gfx.rlc.clear_state_gpu_addr >> 32);
1863b843c749SSergey Zigachev 	WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_CSIB_ADDR_LO),
1864b843c749SSergey Zigachev 			adev->gfx.rlc.clear_state_gpu_addr & 0xfffffffc);
1865b843c749SSergey Zigachev 	WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_CSIB_LENGTH),
1866b843c749SSergey Zigachev 			adev->gfx.rlc.clear_state_size);
1867b843c749SSergey Zigachev }
1868b843c749SSergey Zigachev 
gfx_v9_1_parse_ind_reg_list(int * register_list_format,int indirect_offset,int list_size,int * unique_indirect_regs,int unique_indirect_reg_count,int * indirect_start_offsets,int * indirect_start_offsets_count,int max_start_offsets_count)1869b843c749SSergey Zigachev static void gfx_v9_1_parse_ind_reg_list(int *register_list_format,
1870b843c749SSergey Zigachev 				int indirect_offset,
1871b843c749SSergey Zigachev 				int list_size,
1872b843c749SSergey Zigachev 				int *unique_indirect_regs,
1873b843c749SSergey Zigachev 				int unique_indirect_reg_count,
1874b843c749SSergey Zigachev 				int *indirect_start_offsets,
1875b843c749SSergey Zigachev 				int *indirect_start_offsets_count,
1876b843c749SSergey Zigachev 				int max_start_offsets_count)
1877b843c749SSergey Zigachev {
1878b843c749SSergey Zigachev 	int idx;
1879b843c749SSergey Zigachev 
1880b843c749SSergey Zigachev 	for (; indirect_offset < list_size; indirect_offset++) {
1881b843c749SSergey Zigachev 		WARN_ON(*indirect_start_offsets_count >= max_start_offsets_count);
1882b843c749SSergey Zigachev 		indirect_start_offsets[*indirect_start_offsets_count] = indirect_offset;
1883b843c749SSergey Zigachev 		*indirect_start_offsets_count = *indirect_start_offsets_count + 1;
1884b843c749SSergey Zigachev 
1885b843c749SSergey Zigachev 		while (register_list_format[indirect_offset] != 0xFFFFFFFF) {
1886b843c749SSergey Zigachev 			indirect_offset += 2;
1887b843c749SSergey Zigachev 
1888b843c749SSergey Zigachev 			/* look for the matching indice */
1889b843c749SSergey Zigachev 			for (idx = 0; idx < unique_indirect_reg_count; idx++) {
1890b843c749SSergey Zigachev 				if (unique_indirect_regs[idx] ==
1891b843c749SSergey Zigachev 					register_list_format[indirect_offset] ||
1892b843c749SSergey Zigachev 					!unique_indirect_regs[idx])
1893b843c749SSergey Zigachev 					break;
1894b843c749SSergey Zigachev 			}
1895b843c749SSergey Zigachev 
1896b843c749SSergey Zigachev 			BUG_ON(idx >= unique_indirect_reg_count);
1897b843c749SSergey Zigachev 
1898b843c749SSergey Zigachev 			if (!unique_indirect_regs[idx])
1899b843c749SSergey Zigachev 				unique_indirect_regs[idx] = register_list_format[indirect_offset];
1900b843c749SSergey Zigachev 
1901b843c749SSergey Zigachev 			indirect_offset++;
1902b843c749SSergey Zigachev 		}
1903b843c749SSergey Zigachev 	}
1904b843c749SSergey Zigachev }
1905b843c749SSergey Zigachev 
gfx_v9_1_init_rlc_save_restore_list(struct amdgpu_device * adev)1906b843c749SSergey Zigachev static int gfx_v9_1_init_rlc_save_restore_list(struct amdgpu_device *adev)
1907b843c749SSergey Zigachev {
1908b843c749SSergey Zigachev 	int unique_indirect_regs[] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
1909b843c749SSergey Zigachev 	int unique_indirect_reg_count = 0;
1910b843c749SSergey Zigachev 
1911b843c749SSergey Zigachev 	int indirect_start_offsets[] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
1912b843c749SSergey Zigachev 	int indirect_start_offsets_count = 0;
1913b843c749SSergey Zigachev 
1914b843c749SSergey Zigachev 	int list_size = 0;
1915b843c749SSergey Zigachev 	int i = 0, j = 0;
1916b843c749SSergey Zigachev 	u32 tmp = 0;
1917b843c749SSergey Zigachev 
1918b843c749SSergey Zigachev 	u32 *register_list_format =
191978973132SSergey Zigachev 		kmalloc(adev->gfx.rlc.reg_list_format_size_bytes, M_DRM, GFP_KERNEL);
1920b843c749SSergey Zigachev 	if (!register_list_format)
1921b843c749SSergey Zigachev 		return -ENOMEM;
1922b843c749SSergey Zigachev 	memcpy(register_list_format, adev->gfx.rlc.register_list_format,
1923b843c749SSergey Zigachev 		adev->gfx.rlc.reg_list_format_size_bytes);
1924b843c749SSergey Zigachev 
1925b843c749SSergey Zigachev 	/* setup unique_indirect_regs array and indirect_start_offsets array */
1926b843c749SSergey Zigachev 	unique_indirect_reg_count = ARRAY_SIZE(unique_indirect_regs);
1927b843c749SSergey Zigachev 	gfx_v9_1_parse_ind_reg_list(register_list_format,
1928b843c749SSergey Zigachev 				    adev->gfx.rlc.reg_list_format_direct_reg_list_length,
1929b843c749SSergey Zigachev 				    adev->gfx.rlc.reg_list_format_size_bytes >> 2,
1930b843c749SSergey Zigachev 				    unique_indirect_regs,
1931b843c749SSergey Zigachev 				    unique_indirect_reg_count,
1932b843c749SSergey Zigachev 				    indirect_start_offsets,
1933b843c749SSergey Zigachev 				    &indirect_start_offsets_count,
1934b843c749SSergey Zigachev 				    ARRAY_SIZE(indirect_start_offsets));
1935b843c749SSergey Zigachev 
1936b843c749SSergey Zigachev 	/* enable auto inc in case it is disabled */
1937b843c749SSergey Zigachev 	tmp = RREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_SRM_CNTL));
1938b843c749SSergey Zigachev 	tmp |= RLC_SRM_CNTL__AUTO_INCR_ADDR_MASK;
1939b843c749SSergey Zigachev 	WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_SRM_CNTL), tmp);
1940b843c749SSergey Zigachev 
1941b843c749SSergey Zigachev 	/* write register_restore table to offset 0x0 using RLC_SRM_ARAM_ADDR/DATA */
1942b843c749SSergey Zigachev 	WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_SRM_ARAM_ADDR),
1943b843c749SSergey Zigachev 		RLC_SAVE_RESTORE_ADDR_STARTING_OFFSET);
1944b843c749SSergey Zigachev 	for (i = 0; i < adev->gfx.rlc.reg_list_size_bytes >> 2; i++)
1945b843c749SSergey Zigachev 		WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_SRM_ARAM_DATA),
1946b843c749SSergey Zigachev 			adev->gfx.rlc.register_restore[i]);
1947b843c749SSergey Zigachev 
1948b843c749SSergey Zigachev 	/* load indirect register */
1949b843c749SSergey Zigachev 	WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_GPM_SCRATCH_ADDR),
1950b843c749SSergey Zigachev 		adev->gfx.rlc.reg_list_format_start);
1951b843c749SSergey Zigachev 
1952b843c749SSergey Zigachev 	/* direct register portion */
1953b843c749SSergey Zigachev 	for (i = 0; i < adev->gfx.rlc.reg_list_format_direct_reg_list_length; i++)
1954b843c749SSergey Zigachev 		WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_GPM_SCRATCH_DATA),
1955b843c749SSergey Zigachev 			register_list_format[i]);
1956b843c749SSergey Zigachev 
1957b843c749SSergey Zigachev 	/* indirect register portion */
1958b843c749SSergey Zigachev 	while (i < (adev->gfx.rlc.reg_list_format_size_bytes >> 2)) {
1959b843c749SSergey Zigachev 		if (register_list_format[i] == 0xFFFFFFFF) {
1960b843c749SSergey Zigachev 			WREG32_SOC15(GC, 0, mmRLC_GPM_SCRATCH_DATA, register_list_format[i++]);
1961b843c749SSergey Zigachev 			continue;
1962b843c749SSergey Zigachev 		}
1963b843c749SSergey Zigachev 
1964b843c749SSergey Zigachev 		WREG32_SOC15(GC, 0, mmRLC_GPM_SCRATCH_DATA, register_list_format[i++]);
1965b843c749SSergey Zigachev 		WREG32_SOC15(GC, 0, mmRLC_GPM_SCRATCH_DATA, register_list_format[i++]);
1966b843c749SSergey Zigachev 
1967b843c749SSergey Zigachev 		for (j = 0; j < unique_indirect_reg_count; j++) {
1968b843c749SSergey Zigachev 			if (register_list_format[i] == unique_indirect_regs[j]) {
1969b843c749SSergey Zigachev 				WREG32_SOC15(GC, 0, mmRLC_GPM_SCRATCH_DATA, j);
1970b843c749SSergey Zigachev 				break;
1971b843c749SSergey Zigachev 			}
1972b843c749SSergey Zigachev 		}
1973b843c749SSergey Zigachev 
1974b843c749SSergey Zigachev 		BUG_ON(j >= unique_indirect_reg_count);
1975b843c749SSergey Zigachev 
1976b843c749SSergey Zigachev 		i++;
1977b843c749SSergey Zigachev 	}
1978b843c749SSergey Zigachev 
1979b843c749SSergey Zigachev 	/* set save/restore list size */
1980b843c749SSergey Zigachev 	list_size = adev->gfx.rlc.reg_list_size_bytes >> 2;
1981b843c749SSergey Zigachev 	list_size = list_size >> 1;
1982b843c749SSergey Zigachev 	WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_GPM_SCRATCH_ADDR),
1983b843c749SSergey Zigachev 		adev->gfx.rlc.reg_restore_list_size);
1984b843c749SSergey Zigachev 	WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_GPM_SCRATCH_DATA), list_size);
1985b843c749SSergey Zigachev 
1986b843c749SSergey Zigachev 	/* write the starting offsets to RLC scratch ram */
1987b843c749SSergey Zigachev 	WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_GPM_SCRATCH_ADDR),
1988b843c749SSergey Zigachev 		adev->gfx.rlc.starting_offsets_start);
1989b843c749SSergey Zigachev 	for (i = 0; i < ARRAY_SIZE(indirect_start_offsets); i++)
1990b843c749SSergey Zigachev 		WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_GPM_SCRATCH_DATA),
1991b843c749SSergey Zigachev 		       indirect_start_offsets[i]);
1992b843c749SSergey Zigachev 
1993b843c749SSergey Zigachev 	/* load unique indirect regs*/
1994b843c749SSergey Zigachev 	for (i = 0; i < ARRAY_SIZE(unique_indirect_regs); i++) {
1995b843c749SSergey Zigachev 		if (unique_indirect_regs[i] != 0) {
1996b843c749SSergey Zigachev 			WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_SRM_INDEX_CNTL_ADDR_0)
1997b843c749SSergey Zigachev 			       + GFX_RLC_SRM_INDEX_CNTL_ADDR_OFFSETS[i],
1998b843c749SSergey Zigachev 			       unique_indirect_regs[i] & 0x3FFFF);
1999b843c749SSergey Zigachev 
2000b843c749SSergey Zigachev 			WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_SRM_INDEX_CNTL_DATA_0)
2001b843c749SSergey Zigachev 			       + GFX_RLC_SRM_INDEX_CNTL_DATA_OFFSETS[i],
2002b843c749SSergey Zigachev 			       unique_indirect_regs[i] >> 20);
2003b843c749SSergey Zigachev 		}
2004b843c749SSergey Zigachev 	}
2005b843c749SSergey Zigachev 
2006b843c749SSergey Zigachev 	kfree(register_list_format);
2007b843c749SSergey Zigachev 	return 0;
2008b843c749SSergey Zigachev }
2009b843c749SSergey Zigachev 
gfx_v9_0_enable_save_restore_machine(struct amdgpu_device * adev)2010b843c749SSergey Zigachev static void gfx_v9_0_enable_save_restore_machine(struct amdgpu_device *adev)
2011b843c749SSergey Zigachev {
2012b843c749SSergey Zigachev 	WREG32_FIELD15(GC, 0, RLC_SRM_CNTL, SRM_ENABLE, 1);
2013b843c749SSergey Zigachev }
2014b843c749SSergey Zigachev 
pwr_10_0_gfxip_control_over_cgpg(struct amdgpu_device * adev,bool enable)2015b843c749SSergey Zigachev static void pwr_10_0_gfxip_control_over_cgpg(struct amdgpu_device *adev,
2016b843c749SSergey Zigachev 					     bool enable)
2017b843c749SSergey Zigachev {
2018b843c749SSergey Zigachev 	uint32_t data = 0;
2019b843c749SSergey Zigachev 	uint32_t default_data = 0;
2020b843c749SSergey Zigachev 
2021b843c749SSergey Zigachev 	default_data = data = RREG32(SOC15_REG_OFFSET(PWR, 0, mmPWR_MISC_CNTL_STATUS));
2022b843c749SSergey Zigachev 	if (enable == true) {
2023b843c749SSergey Zigachev 		/* enable GFXIP control over CGPG */
2024b843c749SSergey Zigachev 		data |= PWR_MISC_CNTL_STATUS__PWR_GFX_RLC_CGPG_EN_MASK;
2025b843c749SSergey Zigachev 		if(default_data != data)
2026b843c749SSergey Zigachev 			WREG32(SOC15_REG_OFFSET(PWR, 0, mmPWR_MISC_CNTL_STATUS), data);
2027b843c749SSergey Zigachev 
2028b843c749SSergey Zigachev 		/* update status */
2029b843c749SSergey Zigachev 		data &= ~PWR_MISC_CNTL_STATUS__PWR_GFXOFF_STATUS_MASK;
2030b843c749SSergey Zigachev 		data |= (2 << PWR_MISC_CNTL_STATUS__PWR_GFXOFF_STATUS__SHIFT);
2031b843c749SSergey Zigachev 		if(default_data != data)
2032b843c749SSergey Zigachev 			WREG32(SOC15_REG_OFFSET(PWR, 0, mmPWR_MISC_CNTL_STATUS), data);
2033b843c749SSergey Zigachev 	} else {
2034b843c749SSergey Zigachev 		/* restore GFXIP control over GCPG */
2035b843c749SSergey Zigachev 		data &= ~PWR_MISC_CNTL_STATUS__PWR_GFX_RLC_CGPG_EN_MASK;
2036b843c749SSergey Zigachev 		if(default_data != data)
2037b843c749SSergey Zigachev 			WREG32(SOC15_REG_OFFSET(PWR, 0, mmPWR_MISC_CNTL_STATUS), data);
2038b843c749SSergey Zigachev 	}
2039b843c749SSergey Zigachev }
2040b843c749SSergey Zigachev 
gfx_v9_0_init_gfx_power_gating(struct amdgpu_device * adev)2041b843c749SSergey Zigachev static void gfx_v9_0_init_gfx_power_gating(struct amdgpu_device *adev)
2042b843c749SSergey Zigachev {
2043b843c749SSergey Zigachev 	uint32_t data = 0;
2044b843c749SSergey Zigachev 
2045b843c749SSergey Zigachev 	if (adev->pg_flags & (AMD_PG_SUPPORT_GFX_PG |
2046b843c749SSergey Zigachev 			      AMD_PG_SUPPORT_GFX_SMG |
2047b843c749SSergey Zigachev 			      AMD_PG_SUPPORT_GFX_DMG)) {
2048b843c749SSergey Zigachev 		/* init IDLE_POLL_COUNT = 60 */
2049b843c749SSergey Zigachev 		data = RREG32(SOC15_REG_OFFSET(GC, 0, mmCP_RB_WPTR_POLL_CNTL));
2050b843c749SSergey Zigachev 		data &= ~CP_RB_WPTR_POLL_CNTL__IDLE_POLL_COUNT_MASK;
2051b843c749SSergey Zigachev 		data |= (0x60 << CP_RB_WPTR_POLL_CNTL__IDLE_POLL_COUNT__SHIFT);
2052b843c749SSergey Zigachev 		WREG32(SOC15_REG_OFFSET(GC, 0, mmCP_RB_WPTR_POLL_CNTL), data);
2053b843c749SSergey Zigachev 
2054b843c749SSergey Zigachev 		/* init RLC PG Delay */
2055b843c749SSergey Zigachev 		data = 0;
2056b843c749SSergey Zigachev 		data |= (0x10 << RLC_PG_DELAY__POWER_UP_DELAY__SHIFT);
2057b843c749SSergey Zigachev 		data |= (0x10 << RLC_PG_DELAY__POWER_DOWN_DELAY__SHIFT);
2058b843c749SSergey Zigachev 		data |= (0x10 << RLC_PG_DELAY__CMD_PROPAGATE_DELAY__SHIFT);
2059b843c749SSergey Zigachev 		data |= (0x40 << RLC_PG_DELAY__MEM_SLEEP_DELAY__SHIFT);
2060b843c749SSergey Zigachev 		WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_DELAY), data);
2061b843c749SSergey Zigachev 
2062b843c749SSergey Zigachev 		data = RREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_DELAY_2));
2063b843c749SSergey Zigachev 		data &= ~RLC_PG_DELAY_2__SERDES_CMD_DELAY_MASK;
2064b843c749SSergey Zigachev 		data |= (0x4 << RLC_PG_DELAY_2__SERDES_CMD_DELAY__SHIFT);
2065b843c749SSergey Zigachev 		WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_DELAY_2), data);
2066b843c749SSergey Zigachev 
2067b843c749SSergey Zigachev 		data = RREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_DELAY_3));
2068b843c749SSergey Zigachev 		data &= ~RLC_PG_DELAY_3__CGCG_ACTIVE_BEFORE_CGPG_MASK;
2069b843c749SSergey Zigachev 		data |= (0xff << RLC_PG_DELAY_3__CGCG_ACTIVE_BEFORE_CGPG__SHIFT);
2070b843c749SSergey Zigachev 		WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_DELAY_3), data);
2071b843c749SSergey Zigachev 
2072b843c749SSergey Zigachev 		data = RREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_AUTO_PG_CTRL));
2073b843c749SSergey Zigachev 		data &= ~RLC_AUTO_PG_CTRL__GRBM_REG_SAVE_GFX_IDLE_THRESHOLD_MASK;
2074b843c749SSergey Zigachev 
2075b843c749SSergey Zigachev 		/* program GRBM_REG_SAVE_GFX_IDLE_THRESHOLD to 0x55f0 */
2076b843c749SSergey Zigachev 		data |= (0x55f0 << RLC_AUTO_PG_CTRL__GRBM_REG_SAVE_GFX_IDLE_THRESHOLD__SHIFT);
2077b843c749SSergey Zigachev 		WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_AUTO_PG_CTRL), data);
2078b843c749SSergey Zigachev 
2079b843c749SSergey Zigachev 		pwr_10_0_gfxip_control_over_cgpg(adev, true);
2080b843c749SSergey Zigachev 	}
2081b843c749SSergey Zigachev }
2082b843c749SSergey Zigachev 
gfx_v9_0_enable_sck_slow_down_on_power_up(struct amdgpu_device * adev,bool enable)2083b843c749SSergey Zigachev static void gfx_v9_0_enable_sck_slow_down_on_power_up(struct amdgpu_device *adev,
2084b843c749SSergey Zigachev 						bool enable)
2085b843c749SSergey Zigachev {
2086b843c749SSergey Zigachev 	uint32_t data = 0;
2087b843c749SSergey Zigachev 	uint32_t default_data = 0;
2088b843c749SSergey Zigachev 
2089b843c749SSergey Zigachev 	default_data = data = RREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_CNTL));
2090b843c749SSergey Zigachev 	data = REG_SET_FIELD(data, RLC_PG_CNTL,
2091b843c749SSergey Zigachev 			     SMU_CLK_SLOWDOWN_ON_PU_ENABLE,
2092b843c749SSergey Zigachev 			     enable ? 1 : 0);
2093b843c749SSergey Zigachev 	if (default_data != data)
2094b843c749SSergey Zigachev 		WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_CNTL), data);
2095b843c749SSergey Zigachev }
2096b843c749SSergey Zigachev 
gfx_v9_0_enable_sck_slow_down_on_power_down(struct amdgpu_device * adev,bool enable)2097b843c749SSergey Zigachev static void gfx_v9_0_enable_sck_slow_down_on_power_down(struct amdgpu_device *adev,
2098b843c749SSergey Zigachev 						bool enable)
2099b843c749SSergey Zigachev {
2100b843c749SSergey Zigachev 	uint32_t data = 0;
2101b843c749SSergey Zigachev 	uint32_t default_data = 0;
2102b843c749SSergey Zigachev 
2103b843c749SSergey Zigachev 	default_data = data = RREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_CNTL));
2104b843c749SSergey Zigachev 	data = REG_SET_FIELD(data, RLC_PG_CNTL,
2105b843c749SSergey Zigachev 			     SMU_CLK_SLOWDOWN_ON_PD_ENABLE,
2106b843c749SSergey Zigachev 			     enable ? 1 : 0);
2107b843c749SSergey Zigachev 	if(default_data != data)
2108b843c749SSergey Zigachev 		WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_CNTL), data);
2109b843c749SSergey Zigachev }
2110b843c749SSergey Zigachev 
gfx_v9_0_enable_cp_power_gating(struct amdgpu_device * adev,bool enable)2111b843c749SSergey Zigachev static void gfx_v9_0_enable_cp_power_gating(struct amdgpu_device *adev,
2112b843c749SSergey Zigachev 					bool enable)
2113b843c749SSergey Zigachev {
2114b843c749SSergey Zigachev 	uint32_t data = 0;
2115b843c749SSergey Zigachev 	uint32_t default_data = 0;
2116b843c749SSergey Zigachev 
2117b843c749SSergey Zigachev 	default_data = data = RREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_CNTL));
2118b843c749SSergey Zigachev 	data = REG_SET_FIELD(data, RLC_PG_CNTL,
2119b843c749SSergey Zigachev 			     CP_PG_DISABLE,
2120b843c749SSergey Zigachev 			     enable ? 0 : 1);
2121b843c749SSergey Zigachev 	if(default_data != data)
2122b843c749SSergey Zigachev 		WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_CNTL), data);
2123b843c749SSergey Zigachev }
2124b843c749SSergey Zigachev 
gfx_v9_0_enable_gfx_cg_power_gating(struct amdgpu_device * adev,bool enable)2125b843c749SSergey Zigachev static void gfx_v9_0_enable_gfx_cg_power_gating(struct amdgpu_device *adev,
2126b843c749SSergey Zigachev 						bool enable)
2127b843c749SSergey Zigachev {
2128b843c749SSergey Zigachev 	uint32_t data, default_data;
2129b843c749SSergey Zigachev 
2130b843c749SSergey Zigachev 	default_data = data = RREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_CNTL));
2131b843c749SSergey Zigachev 	data = REG_SET_FIELD(data, RLC_PG_CNTL,
2132b843c749SSergey Zigachev 			     GFX_POWER_GATING_ENABLE,
2133b843c749SSergey Zigachev 			     enable ? 1 : 0);
2134b843c749SSergey Zigachev 	if(default_data != data)
2135b843c749SSergey Zigachev 		WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_CNTL), data);
2136b843c749SSergey Zigachev }
2137b843c749SSergey Zigachev 
gfx_v9_0_enable_gfx_pipeline_powergating(struct amdgpu_device * adev,bool enable)2138b843c749SSergey Zigachev static void gfx_v9_0_enable_gfx_pipeline_powergating(struct amdgpu_device *adev,
2139b843c749SSergey Zigachev 						bool enable)
2140b843c749SSergey Zigachev {
2141b843c749SSergey Zigachev 	uint32_t data, default_data;
2142b843c749SSergey Zigachev 
2143b843c749SSergey Zigachev 	default_data = data = RREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_CNTL));
2144b843c749SSergey Zigachev 	data = REG_SET_FIELD(data, RLC_PG_CNTL,
2145b843c749SSergey Zigachev 			     GFX_PIPELINE_PG_ENABLE,
2146b843c749SSergey Zigachev 			     enable ? 1 : 0);
2147b843c749SSergey Zigachev 	if(default_data != data)
2148b843c749SSergey Zigachev 		WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_CNTL), data);
2149b843c749SSergey Zigachev 
2150b843c749SSergey Zigachev 	if (!enable)
2151b843c749SSergey Zigachev 		/* read any GFX register to wake up GFX */
2152b843c749SSergey Zigachev 		data = RREG32(SOC15_REG_OFFSET(GC, 0, mmDB_RENDER_CONTROL));
2153b843c749SSergey Zigachev }
2154b843c749SSergey Zigachev 
gfx_v9_0_enable_gfx_static_mg_power_gating(struct amdgpu_device * adev,bool enable)2155b843c749SSergey Zigachev static void gfx_v9_0_enable_gfx_static_mg_power_gating(struct amdgpu_device *adev,
2156b843c749SSergey Zigachev 						       bool enable)
2157b843c749SSergey Zigachev {
2158b843c749SSergey Zigachev 	uint32_t data, default_data;
2159b843c749SSergey Zigachev 
2160b843c749SSergey Zigachev 	default_data = data = RREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_CNTL));
2161b843c749SSergey Zigachev 	data = REG_SET_FIELD(data, RLC_PG_CNTL,
2162b843c749SSergey Zigachev 			     STATIC_PER_CU_PG_ENABLE,
2163b843c749SSergey Zigachev 			     enable ? 1 : 0);
2164b843c749SSergey Zigachev 	if(default_data != data)
2165b843c749SSergey Zigachev 		WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_CNTL), data);
2166b843c749SSergey Zigachev }
2167b843c749SSergey Zigachev 
gfx_v9_0_enable_gfx_dynamic_mg_power_gating(struct amdgpu_device * adev,bool enable)2168b843c749SSergey Zigachev static void gfx_v9_0_enable_gfx_dynamic_mg_power_gating(struct amdgpu_device *adev,
2169b843c749SSergey Zigachev 						bool enable)
2170b843c749SSergey Zigachev {
2171b843c749SSergey Zigachev 	uint32_t data, default_data;
2172b843c749SSergey Zigachev 
2173b843c749SSergey Zigachev 	default_data = data = RREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_CNTL));
2174b843c749SSergey Zigachev 	data = REG_SET_FIELD(data, RLC_PG_CNTL,
2175b843c749SSergey Zigachev 			     DYN_PER_CU_PG_ENABLE,
2176b843c749SSergey Zigachev 			     enable ? 1 : 0);
2177b843c749SSergey Zigachev 	if(default_data != data)
2178b843c749SSergey Zigachev 		WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_CNTL), data);
2179b843c749SSergey Zigachev }
2180b843c749SSergey Zigachev 
gfx_v9_0_init_pg(struct amdgpu_device * adev)2181b843c749SSergey Zigachev static void gfx_v9_0_init_pg(struct amdgpu_device *adev)
2182b843c749SSergey Zigachev {
2183b843c749SSergey Zigachev 	gfx_v9_0_init_csb(adev);
2184b843c749SSergey Zigachev 
2185b843c749SSergey Zigachev 	/*
2186b843c749SSergey Zigachev 	 * Rlc save restore list is workable since v2_1.
2187b843c749SSergey Zigachev 	 * And it's needed by gfxoff feature.
2188b843c749SSergey Zigachev 	 */
2189b843c749SSergey Zigachev 	if (adev->gfx.rlc.is_rlc_v2_1) {
2190b843c749SSergey Zigachev 		if (adev->asic_type == CHIP_VEGA12)
2191b843c749SSergey Zigachev 			gfx_v9_1_init_rlc_save_restore_list(adev);
2192b843c749SSergey Zigachev 		gfx_v9_0_enable_save_restore_machine(adev);
2193b843c749SSergey Zigachev 	}
2194b843c749SSergey Zigachev 
2195b843c749SSergey Zigachev 	if (adev->pg_flags & (AMD_PG_SUPPORT_GFX_PG |
2196b843c749SSergey Zigachev 			      AMD_PG_SUPPORT_GFX_SMG |
2197b843c749SSergey Zigachev 			      AMD_PG_SUPPORT_GFX_DMG |
2198b843c749SSergey Zigachev 			      AMD_PG_SUPPORT_CP |
2199b843c749SSergey Zigachev 			      AMD_PG_SUPPORT_GDS |
2200b843c749SSergey Zigachev 			      AMD_PG_SUPPORT_RLC_SMU_HS)) {
2201b843c749SSergey Zigachev 		WREG32(mmRLC_JUMP_TABLE_RESTORE,
2202b843c749SSergey Zigachev 		       adev->gfx.rlc.cp_table_gpu_addr >> 8);
2203b843c749SSergey Zigachev 		gfx_v9_0_init_gfx_power_gating(adev);
2204b843c749SSergey Zigachev 	}
2205b843c749SSergey Zigachev }
2206b843c749SSergey Zigachev 
220778973132SSergey Zigachev void gfx_v9_0_rlc_stop(struct amdgpu_device *adev);
gfx_v9_0_rlc_stop(struct amdgpu_device * adev)2208b843c749SSergey Zigachev void gfx_v9_0_rlc_stop(struct amdgpu_device *adev)
2209b843c749SSergey Zigachev {
2210b843c749SSergey Zigachev 	WREG32_FIELD15(GC, 0, RLC_CNTL, RLC_ENABLE_F32, 0);
2211b843c749SSergey Zigachev 	gfx_v9_0_enable_gui_idle_interrupt(adev, false);
2212b843c749SSergey Zigachev 	gfx_v9_0_wait_for_rlc_serdes(adev);
2213b843c749SSergey Zigachev }
2214b843c749SSergey Zigachev 
gfx_v9_0_rlc_reset(struct amdgpu_device * adev)2215b843c749SSergey Zigachev static void gfx_v9_0_rlc_reset(struct amdgpu_device *adev)
2216b843c749SSergey Zigachev {
2217b843c749SSergey Zigachev 	WREG32_FIELD15(GC, 0, GRBM_SOFT_RESET, SOFT_RESET_RLC, 1);
2218b843c749SSergey Zigachev 	udelay(50);
2219b843c749SSergey Zigachev 	WREG32_FIELD15(GC, 0, GRBM_SOFT_RESET, SOFT_RESET_RLC, 0);
2220b843c749SSergey Zigachev 	udelay(50);
2221b843c749SSergey Zigachev }
2222b843c749SSergey Zigachev 
gfx_v9_0_rlc_start(struct amdgpu_device * adev)2223b843c749SSergey Zigachev static void gfx_v9_0_rlc_start(struct amdgpu_device *adev)
2224b843c749SSergey Zigachev {
2225b843c749SSergey Zigachev #ifdef AMDGPU_RLC_DEBUG_RETRY
2226b843c749SSergey Zigachev 	u32 rlc_ucode_ver;
2227b843c749SSergey Zigachev #endif
2228b843c749SSergey Zigachev 
2229b843c749SSergey Zigachev 	WREG32_FIELD15(GC, 0, RLC_CNTL, RLC_ENABLE_F32, 1);
2230b843c749SSergey Zigachev 	udelay(50);
2231b843c749SSergey Zigachev 
2232b843c749SSergey Zigachev 	/* carrizo do enable cp interrupt after cp inited */
2233b843c749SSergey Zigachev 	if (!(adev->flags & AMD_IS_APU)) {
2234b843c749SSergey Zigachev 		gfx_v9_0_enable_gui_idle_interrupt(adev, true);
2235b843c749SSergey Zigachev 		udelay(50);
2236b843c749SSergey Zigachev 	}
2237b843c749SSergey Zigachev 
2238b843c749SSergey Zigachev #ifdef AMDGPU_RLC_DEBUG_RETRY
2239b843c749SSergey Zigachev 	/* RLC_GPM_GENERAL_6 : RLC Ucode version */
2240b843c749SSergey Zigachev 	rlc_ucode_ver = RREG32_SOC15(GC, 0, mmRLC_GPM_GENERAL_6);
2241b843c749SSergey Zigachev 	if(rlc_ucode_ver == 0x108) {
2242b843c749SSergey Zigachev 		DRM_INFO("Using rlc debug ucode. mmRLC_GPM_GENERAL_6 ==0x08%x / fw_ver == %i \n",
2243b843c749SSergey Zigachev 				rlc_ucode_ver, adev->gfx.rlc_fw_version);
2244b843c749SSergey Zigachev 		/* RLC_GPM_TIMER_INT_3 : Timer interval in RefCLK cycles,
2245b843c749SSergey Zigachev 		 * default is 0x9C4 to create a 100us interval */
2246b843c749SSergey Zigachev 		WREG32_SOC15(GC, 0, mmRLC_GPM_TIMER_INT_3, 0x9C4);
2247b843c749SSergey Zigachev 		/* RLC_GPM_GENERAL_12 : Minimum gap between wptr and rptr
2248b843c749SSergey Zigachev 		 * to disable the page fault retry interrupts, default is
2249b843c749SSergey Zigachev 		 * 0x100 (256) */
2250b843c749SSergey Zigachev 		WREG32_SOC15(GC, 0, mmRLC_GPM_GENERAL_12, 0x100);
2251b843c749SSergey Zigachev 	}
2252b843c749SSergey Zigachev #endif
2253b843c749SSergey Zigachev }
2254b843c749SSergey Zigachev 
gfx_v9_0_rlc_load_microcode(struct amdgpu_device * adev)2255b843c749SSergey Zigachev static int gfx_v9_0_rlc_load_microcode(struct amdgpu_device *adev)
2256b843c749SSergey Zigachev {
2257b843c749SSergey Zigachev 	const struct rlc_firmware_header_v2_0 *hdr;
2258b843c749SSergey Zigachev 	const __le32 *fw_data;
2259b843c749SSergey Zigachev 	unsigned i, fw_size;
2260b843c749SSergey Zigachev 
2261b843c749SSergey Zigachev 	if (!adev->gfx.rlc_fw)
2262b843c749SSergey Zigachev 		return -EINVAL;
2263b843c749SSergey Zigachev 
2264b843c749SSergey Zigachev 	hdr = (const struct rlc_firmware_header_v2_0 *)adev->gfx.rlc_fw->data;
2265b843c749SSergey Zigachev 	amdgpu_ucode_print_rlc_hdr(&hdr->header);
2266b843c749SSergey Zigachev 
2267b843c749SSergey Zigachev 	fw_data = (const __le32 *)(adev->gfx.rlc_fw->data +
2268b843c749SSergey Zigachev 			   le32_to_cpu(hdr->header.ucode_array_offset_bytes));
2269b843c749SSergey Zigachev 	fw_size = le32_to_cpu(hdr->header.ucode_size_bytes) / 4;
2270b843c749SSergey Zigachev 
2271b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmRLC_GPM_UCODE_ADDR,
2272b843c749SSergey Zigachev 			RLCG_UCODE_LOADING_START_ADDRESS);
2273b843c749SSergey Zigachev 	for (i = 0; i < fw_size; i++)
2274b843c749SSergey Zigachev 		WREG32_SOC15(GC, 0, mmRLC_GPM_UCODE_DATA, le32_to_cpup(fw_data++));
2275b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmRLC_GPM_UCODE_ADDR, adev->gfx.rlc_fw_version);
2276b843c749SSergey Zigachev 
2277b843c749SSergey Zigachev 	return 0;
2278b843c749SSergey Zigachev }
2279b843c749SSergey Zigachev 
gfx_v9_0_rlc_resume(struct amdgpu_device * adev)2280b843c749SSergey Zigachev static int gfx_v9_0_rlc_resume(struct amdgpu_device *adev)
2281b843c749SSergey Zigachev {
2282b843c749SSergey Zigachev 	int r;
2283b843c749SSergey Zigachev 
2284b843c749SSergey Zigachev 	if (amdgpu_sriov_vf(adev)) {
2285b843c749SSergey Zigachev 		gfx_v9_0_init_csb(adev);
2286b843c749SSergey Zigachev 		return 0;
2287b843c749SSergey Zigachev 	}
2288b843c749SSergey Zigachev 
2289b843c749SSergey Zigachev 	gfx_v9_0_rlc_stop(adev);
2290b843c749SSergey Zigachev 
2291b843c749SSergey Zigachev 	/* disable CG */
2292b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmRLC_CGCG_CGLS_CTRL, 0);
2293b843c749SSergey Zigachev 
2294b843c749SSergey Zigachev 	gfx_v9_0_rlc_reset(adev);
2295b843c749SSergey Zigachev 
2296b843c749SSergey Zigachev 	gfx_v9_0_init_pg(adev);
2297b843c749SSergey Zigachev 
2298b843c749SSergey Zigachev 	if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
2299b843c749SSergey Zigachev 		/* legacy rlc firmware loading */
2300b843c749SSergey Zigachev 		r = gfx_v9_0_rlc_load_microcode(adev);
2301b843c749SSergey Zigachev 		if (r)
2302b843c749SSergey Zigachev 			return r;
2303b843c749SSergey Zigachev 	}
2304b843c749SSergey Zigachev 
2305b843c749SSergey Zigachev 	if (adev->asic_type == CHIP_RAVEN) {
2306b843c749SSergey Zigachev 		if (amdgpu_lbpw != 0)
2307b843c749SSergey Zigachev 			gfx_v9_0_enable_lbpw(adev, true);
2308b843c749SSergey Zigachev 		else
2309b843c749SSergey Zigachev 			gfx_v9_0_enable_lbpw(adev, false);
2310b843c749SSergey Zigachev 	}
2311b843c749SSergey Zigachev 
2312b843c749SSergey Zigachev 	gfx_v9_0_rlc_start(adev);
2313b843c749SSergey Zigachev 
2314b843c749SSergey Zigachev 	return 0;
2315b843c749SSergey Zigachev }
2316b843c749SSergey Zigachev 
gfx_v9_0_cp_gfx_enable(struct amdgpu_device * adev,bool enable)2317b843c749SSergey Zigachev static void gfx_v9_0_cp_gfx_enable(struct amdgpu_device *adev, bool enable)
2318b843c749SSergey Zigachev {
2319b843c749SSergey Zigachev 	int i;
2320b843c749SSergey Zigachev 	u32 tmp = RREG32_SOC15(GC, 0, mmCP_ME_CNTL);
2321b843c749SSergey Zigachev 
2322b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, ME_HALT, enable ? 0 : 1);
2323b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, PFP_HALT, enable ? 0 : 1);
2324b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, CE_HALT, enable ? 0 : 1);
2325b843c749SSergey Zigachev 	if (!enable) {
2326b843c749SSergey Zigachev 		for (i = 0; i < adev->gfx.num_gfx_rings; i++)
2327b843c749SSergey Zigachev 			adev->gfx.gfx_ring[i].ready = false;
2328b843c749SSergey Zigachev 	}
2329b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_ME_CNTL, tmp);
2330b843c749SSergey Zigachev 	udelay(50);
2331b843c749SSergey Zigachev }
2332b843c749SSergey Zigachev 
gfx_v9_0_cp_gfx_load_microcode(struct amdgpu_device * adev)2333b843c749SSergey Zigachev static int gfx_v9_0_cp_gfx_load_microcode(struct amdgpu_device *adev)
2334b843c749SSergey Zigachev {
2335b843c749SSergey Zigachev 	const struct gfx_firmware_header_v1_0 *pfp_hdr;
2336b843c749SSergey Zigachev 	const struct gfx_firmware_header_v1_0 *ce_hdr;
2337b843c749SSergey Zigachev 	const struct gfx_firmware_header_v1_0 *me_hdr;
2338b843c749SSergey Zigachev 	const __le32 *fw_data;
2339b843c749SSergey Zigachev 	unsigned i, fw_size;
2340b843c749SSergey Zigachev 
2341b843c749SSergey Zigachev 	if (!adev->gfx.me_fw || !adev->gfx.pfp_fw || !adev->gfx.ce_fw)
2342b843c749SSergey Zigachev 		return -EINVAL;
2343b843c749SSergey Zigachev 
2344b843c749SSergey Zigachev 	pfp_hdr = (const struct gfx_firmware_header_v1_0 *)
2345b843c749SSergey Zigachev 		adev->gfx.pfp_fw->data;
2346b843c749SSergey Zigachev 	ce_hdr = (const struct gfx_firmware_header_v1_0 *)
2347b843c749SSergey Zigachev 		adev->gfx.ce_fw->data;
2348b843c749SSergey Zigachev 	me_hdr = (const struct gfx_firmware_header_v1_0 *)
2349b843c749SSergey Zigachev 		adev->gfx.me_fw->data;
2350b843c749SSergey Zigachev 
2351b843c749SSergey Zigachev 	amdgpu_ucode_print_gfx_hdr(&pfp_hdr->header);
2352b843c749SSergey Zigachev 	amdgpu_ucode_print_gfx_hdr(&ce_hdr->header);
2353b843c749SSergey Zigachev 	amdgpu_ucode_print_gfx_hdr(&me_hdr->header);
2354b843c749SSergey Zigachev 
2355b843c749SSergey Zigachev 	gfx_v9_0_cp_gfx_enable(adev, false);
2356b843c749SSergey Zigachev 
2357b843c749SSergey Zigachev 	/* PFP */
2358b843c749SSergey Zigachev 	fw_data = (const __le32 *)
2359b843c749SSergey Zigachev 		(adev->gfx.pfp_fw->data +
2360b843c749SSergey Zigachev 		 le32_to_cpu(pfp_hdr->header.ucode_array_offset_bytes));
2361b843c749SSergey Zigachev 	fw_size = le32_to_cpu(pfp_hdr->header.ucode_size_bytes) / 4;
2362b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_PFP_UCODE_ADDR, 0);
2363b843c749SSergey Zigachev 	for (i = 0; i < fw_size; i++)
2364b843c749SSergey Zigachev 		WREG32_SOC15(GC, 0, mmCP_PFP_UCODE_DATA, le32_to_cpup(fw_data++));
2365b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_PFP_UCODE_ADDR, adev->gfx.pfp_fw_version);
2366b843c749SSergey Zigachev 
2367b843c749SSergey Zigachev 	/* CE */
2368b843c749SSergey Zigachev 	fw_data = (const __le32 *)
2369b843c749SSergey Zigachev 		(adev->gfx.ce_fw->data +
2370b843c749SSergey Zigachev 		 le32_to_cpu(ce_hdr->header.ucode_array_offset_bytes));
2371b843c749SSergey Zigachev 	fw_size = le32_to_cpu(ce_hdr->header.ucode_size_bytes) / 4;
2372b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_CE_UCODE_ADDR, 0);
2373b843c749SSergey Zigachev 	for (i = 0; i < fw_size; i++)
2374b843c749SSergey Zigachev 		WREG32_SOC15(GC, 0, mmCP_CE_UCODE_DATA, le32_to_cpup(fw_data++));
2375b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_CE_UCODE_ADDR, adev->gfx.ce_fw_version);
2376b843c749SSergey Zigachev 
2377b843c749SSergey Zigachev 	/* ME */
2378b843c749SSergey Zigachev 	fw_data = (const __le32 *)
2379b843c749SSergey Zigachev 		(adev->gfx.me_fw->data +
2380b843c749SSergey Zigachev 		 le32_to_cpu(me_hdr->header.ucode_array_offset_bytes));
2381b843c749SSergey Zigachev 	fw_size = le32_to_cpu(me_hdr->header.ucode_size_bytes) / 4;
2382b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_ME_RAM_WADDR, 0);
2383b843c749SSergey Zigachev 	for (i = 0; i < fw_size; i++)
2384b843c749SSergey Zigachev 		WREG32_SOC15(GC, 0, mmCP_ME_RAM_DATA, le32_to_cpup(fw_data++));
2385b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_ME_RAM_WADDR, adev->gfx.me_fw_version);
2386b843c749SSergey Zigachev 
2387b843c749SSergey Zigachev 	return 0;
2388b843c749SSergey Zigachev }
2389b843c749SSergey Zigachev 
gfx_v9_0_cp_gfx_start(struct amdgpu_device * adev)2390b843c749SSergey Zigachev static int gfx_v9_0_cp_gfx_start(struct amdgpu_device *adev)
2391b843c749SSergey Zigachev {
2392b843c749SSergey Zigachev 	struct amdgpu_ring *ring = &adev->gfx.gfx_ring[0];
2393b843c749SSergey Zigachev 	const struct cs_section_def *sect = NULL;
2394b843c749SSergey Zigachev 	const struct cs_extent_def *ext = NULL;
2395b843c749SSergey Zigachev 	int r, i, tmp;
2396b843c749SSergey Zigachev 
2397b843c749SSergey Zigachev 	/* init the CP */
2398b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_MAX_CONTEXT, adev->gfx.config.max_hw_contexts - 1);
2399b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_DEVICE_ID, 1);
2400b843c749SSergey Zigachev 
2401b843c749SSergey Zigachev 	gfx_v9_0_cp_gfx_enable(adev, true);
2402b843c749SSergey Zigachev 
2403b843c749SSergey Zigachev 	r = amdgpu_ring_alloc(ring, gfx_v9_0_get_csb_size(adev) + 4 + 3);
2404b843c749SSergey Zigachev 	if (r) {
2405b843c749SSergey Zigachev 		DRM_ERROR("amdgpu: cp failed to lock ring (%d).\n", r);
2406b843c749SSergey Zigachev 		return r;
2407b843c749SSergey Zigachev 	}
2408b843c749SSergey Zigachev 
2409b843c749SSergey Zigachev 	amdgpu_ring_write(ring, PACKET3(PACKET3_PREAMBLE_CNTL, 0));
2410b843c749SSergey Zigachev 	amdgpu_ring_write(ring, PACKET3_PREAMBLE_BEGIN_CLEAR_STATE);
2411b843c749SSergey Zigachev 
2412b843c749SSergey Zigachev 	amdgpu_ring_write(ring, PACKET3(PACKET3_CONTEXT_CONTROL, 1));
2413b843c749SSergey Zigachev 	amdgpu_ring_write(ring, 0x80000000);
2414b843c749SSergey Zigachev 	amdgpu_ring_write(ring, 0x80000000);
2415b843c749SSergey Zigachev 
2416b843c749SSergey Zigachev 	for (sect = gfx9_cs_data; sect->section != NULL; ++sect) {
2417b843c749SSergey Zigachev 		for (ext = sect->section; ext->extent != NULL; ++ext) {
2418b843c749SSergey Zigachev 			if (sect->id == SECT_CONTEXT) {
2419b843c749SSergey Zigachev 				amdgpu_ring_write(ring,
2420b843c749SSergey Zigachev 				       PACKET3(PACKET3_SET_CONTEXT_REG,
2421b843c749SSergey Zigachev 					       ext->reg_count));
2422b843c749SSergey Zigachev 				amdgpu_ring_write(ring,
2423b843c749SSergey Zigachev 				       ext->reg_index - PACKET3_SET_CONTEXT_REG_START);
2424b843c749SSergey Zigachev 				for (i = 0; i < ext->reg_count; i++)
2425b843c749SSergey Zigachev 					amdgpu_ring_write(ring, ext->extent[i]);
2426b843c749SSergey Zigachev 			}
2427b843c749SSergey Zigachev 		}
2428b843c749SSergey Zigachev 	}
2429b843c749SSergey Zigachev 
2430b843c749SSergey Zigachev 	amdgpu_ring_write(ring, PACKET3(PACKET3_PREAMBLE_CNTL, 0));
2431b843c749SSergey Zigachev 	amdgpu_ring_write(ring, PACKET3_PREAMBLE_END_CLEAR_STATE);
2432b843c749SSergey Zigachev 
2433b843c749SSergey Zigachev 	amdgpu_ring_write(ring, PACKET3(PACKET3_CLEAR_STATE, 0));
2434b843c749SSergey Zigachev 	amdgpu_ring_write(ring, 0);
2435b843c749SSergey Zigachev 
2436b843c749SSergey Zigachev 	amdgpu_ring_write(ring, PACKET3(PACKET3_SET_BASE, 2));
2437b843c749SSergey Zigachev 	amdgpu_ring_write(ring, PACKET3_BASE_INDEX(CE_PARTITION_BASE));
2438b843c749SSergey Zigachev 	amdgpu_ring_write(ring, 0x8000);
2439b843c749SSergey Zigachev 	amdgpu_ring_write(ring, 0x8000);
2440b843c749SSergey Zigachev 
2441b843c749SSergey Zigachev 	amdgpu_ring_write(ring, PACKET3(PACKET3_SET_UCONFIG_REG,1));
2442b843c749SSergey Zigachev 	tmp = (PACKET3_SET_UCONFIG_REG_INDEX_TYPE |
2443b843c749SSergey Zigachev 		(SOC15_REG_OFFSET(GC, 0, mmVGT_INDEX_TYPE) - PACKET3_SET_UCONFIG_REG_START));
2444b843c749SSergey Zigachev 	amdgpu_ring_write(ring, tmp);
2445b843c749SSergey Zigachev 	amdgpu_ring_write(ring, 0);
2446b843c749SSergey Zigachev 
2447b843c749SSergey Zigachev 	amdgpu_ring_commit(ring);
2448b843c749SSergey Zigachev 
2449b843c749SSergey Zigachev 	return 0;
2450b843c749SSergey Zigachev }
2451b843c749SSergey Zigachev 
gfx_v9_0_cp_gfx_resume(struct amdgpu_device * adev)2452b843c749SSergey Zigachev static int gfx_v9_0_cp_gfx_resume(struct amdgpu_device *adev)
2453b843c749SSergey Zigachev {
2454b843c749SSergey Zigachev 	struct amdgpu_ring *ring;
2455b843c749SSergey Zigachev 	u32 tmp;
2456b843c749SSergey Zigachev 	u32 rb_bufsz;
2457b843c749SSergey Zigachev 	u64 rb_addr, rptr_addr, wptr_gpu_addr;
2458b843c749SSergey Zigachev 
2459b843c749SSergey Zigachev 	/* Set the write pointer delay */
2460b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_RB_WPTR_DELAY, 0);
2461b843c749SSergey Zigachev 
2462b843c749SSergey Zigachev 	/* set the RB to use vmid 0 */
2463b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_RB_VMID, 0);
2464b843c749SSergey Zigachev 
2465b843c749SSergey Zigachev 	/* Set ring buffer size */
2466b843c749SSergey Zigachev 	ring = &adev->gfx.gfx_ring[0];
2467b843c749SSergey Zigachev 	rb_bufsz = order_base_2(ring->ring_size / 8);
2468b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(0, CP_RB0_CNTL, RB_BUFSZ, rb_bufsz);
2469b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, CP_RB0_CNTL, RB_BLKSZ, rb_bufsz - 2);
2470b843c749SSergey Zigachev #ifdef __BIG_ENDIAN
2471b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, CP_RB0_CNTL, BUF_SWAP, 1);
2472b843c749SSergey Zigachev #endif
2473b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_RB0_CNTL, tmp);
2474b843c749SSergey Zigachev 
2475b843c749SSergey Zigachev 	/* Initialize the ring buffer's write pointers */
2476b843c749SSergey Zigachev 	ring->wptr = 0;
2477b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_RB0_WPTR, lower_32_bits(ring->wptr));
2478b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_RB0_WPTR_HI, upper_32_bits(ring->wptr));
2479b843c749SSergey Zigachev 
2480b843c749SSergey Zigachev 	/* set the wb address wether it's enabled or not */
2481b843c749SSergey Zigachev 	rptr_addr = adev->wb.gpu_addr + (ring->rptr_offs * 4);
2482b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_RB0_RPTR_ADDR, lower_32_bits(rptr_addr));
2483b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_RB0_RPTR_ADDR_HI, upper_32_bits(rptr_addr) & CP_RB_RPTR_ADDR_HI__RB_RPTR_ADDR_HI_MASK);
2484b843c749SSergey Zigachev 
2485b843c749SSergey Zigachev 	wptr_gpu_addr = adev->wb.gpu_addr + (ring->wptr_offs * 4);
2486b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_RB_WPTR_POLL_ADDR_LO, lower_32_bits(wptr_gpu_addr));
2487b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_RB_WPTR_POLL_ADDR_HI, upper_32_bits(wptr_gpu_addr));
2488b843c749SSergey Zigachev 
2489b843c749SSergey Zigachev 	mdelay(1);
2490b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_RB0_CNTL, tmp);
2491b843c749SSergey Zigachev 
2492b843c749SSergey Zigachev 	rb_addr = ring->gpu_addr >> 8;
2493b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_RB0_BASE, rb_addr);
2494b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_RB0_BASE_HI, upper_32_bits(rb_addr));
2495b843c749SSergey Zigachev 
2496b843c749SSergey Zigachev 	tmp = RREG32_SOC15(GC, 0, mmCP_RB_DOORBELL_CONTROL);
2497b843c749SSergey Zigachev 	if (ring->use_doorbell) {
2498b843c749SSergey Zigachev 		tmp = REG_SET_FIELD(tmp, CP_RB_DOORBELL_CONTROL,
2499b843c749SSergey Zigachev 				    DOORBELL_OFFSET, ring->doorbell_index);
2500b843c749SSergey Zigachev 		tmp = REG_SET_FIELD(tmp, CP_RB_DOORBELL_CONTROL,
2501b843c749SSergey Zigachev 				    DOORBELL_EN, 1);
2502b843c749SSergey Zigachev 	} else {
2503b843c749SSergey Zigachev 		tmp = REG_SET_FIELD(tmp, CP_RB_DOORBELL_CONTROL, DOORBELL_EN, 0);
2504b843c749SSergey Zigachev 	}
2505b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_RB_DOORBELL_CONTROL, tmp);
2506b843c749SSergey Zigachev 
2507b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(0, CP_RB_DOORBELL_RANGE_LOWER,
2508b843c749SSergey Zigachev 			DOORBELL_RANGE_LOWER, ring->doorbell_index);
2509b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_RB_DOORBELL_RANGE_LOWER, tmp);
2510b843c749SSergey Zigachev 
2511b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_RB_DOORBELL_RANGE_UPPER,
2512b843c749SSergey Zigachev 		       CP_RB_DOORBELL_RANGE_UPPER__DOORBELL_RANGE_UPPER_MASK);
2513b843c749SSergey Zigachev 
2514b843c749SSergey Zigachev 
2515b843c749SSergey Zigachev 	/* start the ring */
2516b843c749SSergey Zigachev 	gfx_v9_0_cp_gfx_start(adev);
2517b843c749SSergey Zigachev 	ring->ready = true;
2518b843c749SSergey Zigachev 
2519b843c749SSergey Zigachev 	return 0;
2520b843c749SSergey Zigachev }
2521b843c749SSergey Zigachev 
gfx_v9_0_cp_compute_enable(struct amdgpu_device * adev,bool enable)2522b843c749SSergey Zigachev static void gfx_v9_0_cp_compute_enable(struct amdgpu_device *adev, bool enable)
2523b843c749SSergey Zigachev {
2524b843c749SSergey Zigachev 	int i;
2525b843c749SSergey Zigachev 
2526b843c749SSergey Zigachev 	if (enable) {
2527b843c749SSergey Zigachev 		WREG32_SOC15(GC, 0, mmCP_MEC_CNTL, 0);
2528b843c749SSergey Zigachev 	} else {
2529b843c749SSergey Zigachev 		WREG32_SOC15(GC, 0, mmCP_MEC_CNTL,
2530b843c749SSergey Zigachev 			(CP_MEC_CNTL__MEC_ME1_HALT_MASK | CP_MEC_CNTL__MEC_ME2_HALT_MASK));
2531b843c749SSergey Zigachev 		for (i = 0; i < adev->gfx.num_compute_rings; i++)
2532b843c749SSergey Zigachev 			adev->gfx.compute_ring[i].ready = false;
2533b843c749SSergey Zigachev 		adev->gfx.kiq.ring.ready = false;
2534b843c749SSergey Zigachev 	}
2535b843c749SSergey Zigachev 	udelay(50);
2536b843c749SSergey Zigachev }
2537b843c749SSergey Zigachev 
gfx_v9_0_cp_compute_load_microcode(struct amdgpu_device * adev)2538b843c749SSergey Zigachev static int gfx_v9_0_cp_compute_load_microcode(struct amdgpu_device *adev)
2539b843c749SSergey Zigachev {
2540b843c749SSergey Zigachev 	const struct gfx_firmware_header_v1_0 *mec_hdr;
2541b843c749SSergey Zigachev 	const __le32 *fw_data;
2542b843c749SSergey Zigachev 	unsigned i;
2543b843c749SSergey Zigachev 	u32 tmp;
2544b843c749SSergey Zigachev 
2545b843c749SSergey Zigachev 	if (!adev->gfx.mec_fw)
2546b843c749SSergey Zigachev 		return -EINVAL;
2547b843c749SSergey Zigachev 
2548b843c749SSergey Zigachev 	gfx_v9_0_cp_compute_enable(adev, false);
2549b843c749SSergey Zigachev 
2550b843c749SSergey Zigachev 	mec_hdr = (const struct gfx_firmware_header_v1_0 *)adev->gfx.mec_fw->data;
2551b843c749SSergey Zigachev 	amdgpu_ucode_print_gfx_hdr(&mec_hdr->header);
2552b843c749SSergey Zigachev 
2553b843c749SSergey Zigachev 	fw_data = (const __le32 *)
2554b843c749SSergey Zigachev 		(adev->gfx.mec_fw->data +
2555b843c749SSergey Zigachev 		 le32_to_cpu(mec_hdr->header.ucode_array_offset_bytes));
2556b843c749SSergey Zigachev 	tmp = 0;
2557b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, CP_CPC_IC_BASE_CNTL, VMID, 0);
2558b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, CP_CPC_IC_BASE_CNTL, CACHE_POLICY, 0);
2559b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_CPC_IC_BASE_CNTL, tmp);
2560b843c749SSergey Zigachev 
2561b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_CPC_IC_BASE_LO,
2562b843c749SSergey Zigachev 		adev->gfx.mec.mec_fw_gpu_addr & 0xFFFFF000);
2563b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_CPC_IC_BASE_HI,
2564b843c749SSergey Zigachev 		upper_32_bits(adev->gfx.mec.mec_fw_gpu_addr));
2565b843c749SSergey Zigachev 
2566b843c749SSergey Zigachev 	/* MEC1 */
2567b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_MEC_ME1_UCODE_ADDR,
2568b843c749SSergey Zigachev 			 mec_hdr->jt_offset);
2569b843c749SSergey Zigachev 	for (i = 0; i < mec_hdr->jt_size; i++)
2570b843c749SSergey Zigachev 		WREG32_SOC15(GC, 0, mmCP_MEC_ME1_UCODE_DATA,
2571b843c749SSergey Zigachev 			le32_to_cpup(fw_data + mec_hdr->jt_offset + i));
2572b843c749SSergey Zigachev 
2573b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_MEC_ME1_UCODE_ADDR,
2574b843c749SSergey Zigachev 			adev->gfx.mec_fw_version);
2575b843c749SSergey Zigachev 	/* Todo : Loading MEC2 firmware is only necessary if MEC2 should run different microcode than MEC1. */
2576b843c749SSergey Zigachev 
2577b843c749SSergey Zigachev 	return 0;
2578b843c749SSergey Zigachev }
2579b843c749SSergey Zigachev 
2580b843c749SSergey Zigachev /* KIQ functions */
gfx_v9_0_kiq_setting(struct amdgpu_ring * ring)2581b843c749SSergey Zigachev static void gfx_v9_0_kiq_setting(struct amdgpu_ring *ring)
2582b843c749SSergey Zigachev {
2583b843c749SSergey Zigachev 	uint32_t tmp;
2584b843c749SSergey Zigachev 	struct amdgpu_device *adev = ring->adev;
2585b843c749SSergey Zigachev 
2586b843c749SSergey Zigachev 	/* tell RLC which is KIQ queue */
2587b843c749SSergey Zigachev 	tmp = RREG32_SOC15(GC, 0, mmRLC_CP_SCHEDULERS);
2588b843c749SSergey Zigachev 	tmp &= 0xffffff00;
2589b843c749SSergey Zigachev 	tmp |= (ring->me << 5) | (ring->pipe << 3) | (ring->queue);
2590b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmRLC_CP_SCHEDULERS, tmp);
2591b843c749SSergey Zigachev 	tmp |= 0x80;
2592b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmRLC_CP_SCHEDULERS, tmp);
2593b843c749SSergey Zigachev }
2594b843c749SSergey Zigachev 
gfx_v9_0_kiq_kcq_enable(struct amdgpu_device * adev)2595b843c749SSergey Zigachev static int gfx_v9_0_kiq_kcq_enable(struct amdgpu_device *adev)
2596b843c749SSergey Zigachev {
2597b843c749SSergey Zigachev 	struct amdgpu_ring *kiq_ring = &adev->gfx.kiq.ring;
2598b843c749SSergey Zigachev 	uint32_t scratch, tmp = 0;
2599b843c749SSergey Zigachev 	uint64_t queue_mask = 0;
2600b843c749SSergey Zigachev 	int r, i;
2601b843c749SSergey Zigachev 
2602b843c749SSergey Zigachev 	for (i = 0; i < AMDGPU_MAX_COMPUTE_QUEUES; ++i) {
2603b843c749SSergey Zigachev 		if (!test_bit(i, adev->gfx.mec.queue_bitmap))
2604b843c749SSergey Zigachev 			continue;
2605b843c749SSergey Zigachev 
2606b843c749SSergey Zigachev 		/* This situation may be hit in the future if a new HW
2607b843c749SSergey Zigachev 		 * generation exposes more than 64 queues. If so, the
2608b843c749SSergey Zigachev 		 * definition of queue_mask needs updating */
2609b843c749SSergey Zigachev 		if (WARN_ON(i >= (sizeof(queue_mask)*8))) {
2610b843c749SSergey Zigachev 			DRM_ERROR("Invalid KCQ enabled: %d\n", i);
2611b843c749SSergey Zigachev 			break;
2612b843c749SSergey Zigachev 		}
2613b843c749SSergey Zigachev 
2614b843c749SSergey Zigachev 		queue_mask |= (1ull << i);
2615b843c749SSergey Zigachev 	}
2616b843c749SSergey Zigachev 
2617b843c749SSergey Zigachev 	r = amdgpu_gfx_scratch_get(adev, &scratch);
2618b843c749SSergey Zigachev 	if (r) {
2619b843c749SSergey Zigachev 		DRM_ERROR("Failed to get scratch reg (%d).\n", r);
2620b843c749SSergey Zigachev 		return r;
2621b843c749SSergey Zigachev 	}
2622b843c749SSergey Zigachev 	WREG32(scratch, 0xCAFEDEAD);
2623b843c749SSergey Zigachev 
2624b843c749SSergey Zigachev 	r = amdgpu_ring_alloc(kiq_ring, (7 * adev->gfx.num_compute_rings) + 11);
2625b843c749SSergey Zigachev 	if (r) {
2626b843c749SSergey Zigachev 		DRM_ERROR("Failed to lock KIQ (%d).\n", r);
2627b843c749SSergey Zigachev 		amdgpu_gfx_scratch_free(adev, scratch);
2628b843c749SSergey Zigachev 		return r;
2629b843c749SSergey Zigachev 	}
2630b843c749SSergey Zigachev 
2631b843c749SSergey Zigachev 	/* set resources */
2632b843c749SSergey Zigachev 	amdgpu_ring_write(kiq_ring, PACKET3(PACKET3_SET_RESOURCES, 6));
2633b843c749SSergey Zigachev 	amdgpu_ring_write(kiq_ring, PACKET3_SET_RESOURCES_VMID_MASK(0) |
2634b843c749SSergey Zigachev 			  PACKET3_SET_RESOURCES_QUEUE_TYPE(0));	/* vmid_mask:0 queue_type:0 (KIQ) */
2635b843c749SSergey Zigachev 	amdgpu_ring_write(kiq_ring, lower_32_bits(queue_mask));	/* queue mask lo */
2636b843c749SSergey Zigachev 	amdgpu_ring_write(kiq_ring, upper_32_bits(queue_mask));	/* queue mask hi */
2637b843c749SSergey Zigachev 	amdgpu_ring_write(kiq_ring, 0);	/* gws mask lo */
2638b843c749SSergey Zigachev 	amdgpu_ring_write(kiq_ring, 0);	/* gws mask hi */
2639b843c749SSergey Zigachev 	amdgpu_ring_write(kiq_ring, 0);	/* oac mask */
2640b843c749SSergey Zigachev 	amdgpu_ring_write(kiq_ring, 0);	/* gds heap base:0, gds heap size:0 */
2641b843c749SSergey Zigachev 	for (i = 0; i < adev->gfx.num_compute_rings; i++) {
2642b843c749SSergey Zigachev 		struct amdgpu_ring *ring = &adev->gfx.compute_ring[i];
2643b843c749SSergey Zigachev 		uint64_t mqd_addr = amdgpu_bo_gpu_offset(ring->mqd_obj);
2644b843c749SSergey Zigachev 		uint64_t wptr_addr = adev->wb.gpu_addr + (ring->wptr_offs * 4);
2645b843c749SSergey Zigachev 
2646b843c749SSergey Zigachev 		amdgpu_ring_write(kiq_ring, PACKET3(PACKET3_MAP_QUEUES, 5));
2647b843c749SSergey Zigachev 		/* Q_sel:0, vmid:0, vidmem: 1, engine:0, num_Q:1*/
2648b843c749SSergey Zigachev 		amdgpu_ring_write(kiq_ring, /* Q_sel: 0, vmid: 0, engine: 0, num_Q: 1 */
2649b843c749SSergey Zigachev 				  PACKET3_MAP_QUEUES_QUEUE_SEL(0) | /* Queue_Sel */
2650b843c749SSergey Zigachev 				  PACKET3_MAP_QUEUES_VMID(0) | /* VMID */
2651b843c749SSergey Zigachev 				  PACKET3_MAP_QUEUES_QUEUE(ring->queue) |
2652b843c749SSergey Zigachev 				  PACKET3_MAP_QUEUES_PIPE(ring->pipe) |
2653b843c749SSergey Zigachev 				  PACKET3_MAP_QUEUES_ME((ring->me == 1 ? 0 : 1)) |
2654b843c749SSergey Zigachev 				  PACKET3_MAP_QUEUES_QUEUE_TYPE(0) | /*queue_type: normal compute queue */
2655b843c749SSergey Zigachev 				  PACKET3_MAP_QUEUES_ALLOC_FORMAT(0) | /* alloc format: all_on_one_pipe */
2656b843c749SSergey Zigachev 				  PACKET3_MAP_QUEUES_ENGINE_SEL(0) | /* engine_sel: compute */
2657b843c749SSergey Zigachev 				  PACKET3_MAP_QUEUES_NUM_QUEUES(1)); /* num_queues: must be 1 */
2658b843c749SSergey Zigachev 		amdgpu_ring_write(kiq_ring, PACKET3_MAP_QUEUES_DOORBELL_OFFSET(ring->doorbell_index));
2659b843c749SSergey Zigachev 		amdgpu_ring_write(kiq_ring, lower_32_bits(mqd_addr));
2660b843c749SSergey Zigachev 		amdgpu_ring_write(kiq_ring, upper_32_bits(mqd_addr));
2661b843c749SSergey Zigachev 		amdgpu_ring_write(kiq_ring, lower_32_bits(wptr_addr));
2662b843c749SSergey Zigachev 		amdgpu_ring_write(kiq_ring, upper_32_bits(wptr_addr));
2663b843c749SSergey Zigachev 	}
2664b843c749SSergey Zigachev 	/* write to scratch for completion */
2665b843c749SSergey Zigachev 	amdgpu_ring_write(kiq_ring, PACKET3(PACKET3_SET_UCONFIG_REG, 1));
2666b843c749SSergey Zigachev 	amdgpu_ring_write(kiq_ring, (scratch - PACKET3_SET_UCONFIG_REG_START));
2667b843c749SSergey Zigachev 	amdgpu_ring_write(kiq_ring, 0xDEADBEEF);
2668b843c749SSergey Zigachev 	amdgpu_ring_commit(kiq_ring);
2669b843c749SSergey Zigachev 
2670b843c749SSergey Zigachev 	for (i = 0; i < adev->usec_timeout; i++) {
2671b843c749SSergey Zigachev 		tmp = RREG32(scratch);
2672b843c749SSergey Zigachev 		if (tmp == 0xDEADBEEF)
2673b843c749SSergey Zigachev 			break;
2674b843c749SSergey Zigachev 		DRM_UDELAY(1);
2675b843c749SSergey Zigachev 	}
2676b843c749SSergey Zigachev 	if (i >= adev->usec_timeout) {
2677b843c749SSergey Zigachev 		DRM_ERROR("KCQ enable failed (scratch(0x%04X)=0x%08X)\n",
2678b843c749SSergey Zigachev 			  scratch, tmp);
2679b843c749SSergey Zigachev 		r = -EINVAL;
2680b843c749SSergey Zigachev 	}
2681b843c749SSergey Zigachev 	amdgpu_gfx_scratch_free(adev, scratch);
2682b843c749SSergey Zigachev 
2683b843c749SSergey Zigachev 	return r;
2684b843c749SSergey Zigachev }
2685b843c749SSergey Zigachev 
gfx_v9_0_mqd_init(struct amdgpu_ring * ring)2686b843c749SSergey Zigachev static int gfx_v9_0_mqd_init(struct amdgpu_ring *ring)
2687b843c749SSergey Zigachev {
2688b843c749SSergey Zigachev 	struct amdgpu_device *adev = ring->adev;
2689b843c749SSergey Zigachev 	struct v9_mqd *mqd = ring->mqd_ptr;
2690b843c749SSergey Zigachev 	uint64_t hqd_gpu_addr, wb_gpu_addr, eop_base_addr;
2691b843c749SSergey Zigachev 	uint32_t tmp;
2692b843c749SSergey Zigachev 
2693b843c749SSergey Zigachev 	mqd->header = 0xC0310800;
2694b843c749SSergey Zigachev 	mqd->compute_pipelinestat_enable = 0x00000001;
2695b843c749SSergey Zigachev 	mqd->compute_static_thread_mgmt_se0 = 0xffffffff;
2696b843c749SSergey Zigachev 	mqd->compute_static_thread_mgmt_se1 = 0xffffffff;
2697b843c749SSergey Zigachev 	mqd->compute_static_thread_mgmt_se2 = 0xffffffff;
2698b843c749SSergey Zigachev 	mqd->compute_static_thread_mgmt_se3 = 0xffffffff;
2699b843c749SSergey Zigachev 	mqd->compute_misc_reserved = 0x00000003;
2700b843c749SSergey Zigachev 
2701b843c749SSergey Zigachev 	mqd->dynamic_cu_mask_addr_lo =
2702b843c749SSergey Zigachev 		lower_32_bits(ring->mqd_gpu_addr
2703b843c749SSergey Zigachev 			      + offsetof(struct v9_mqd_allocation, dynamic_cu_mask));
2704b843c749SSergey Zigachev 	mqd->dynamic_cu_mask_addr_hi =
2705b843c749SSergey Zigachev 		upper_32_bits(ring->mqd_gpu_addr
2706b843c749SSergey Zigachev 			      + offsetof(struct v9_mqd_allocation, dynamic_cu_mask));
2707b843c749SSergey Zigachev 
2708b843c749SSergey Zigachev 	eop_base_addr = ring->eop_gpu_addr >> 8;
2709b843c749SSergey Zigachev 	mqd->cp_hqd_eop_base_addr_lo = eop_base_addr;
2710b843c749SSergey Zigachev 	mqd->cp_hqd_eop_base_addr_hi = upper_32_bits(eop_base_addr);
2711b843c749SSergey Zigachev 
2712b843c749SSergey Zigachev 	/* set the EOP size, register value is 2^(EOP_SIZE+1) dwords */
2713b843c749SSergey Zigachev 	tmp = RREG32_SOC15(GC, 0, mmCP_HQD_EOP_CONTROL);
2714b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, CP_HQD_EOP_CONTROL, EOP_SIZE,
2715b843c749SSergey Zigachev 			(order_base_2(GFX9_MEC_HPD_SIZE / 4) - 1));
2716b843c749SSergey Zigachev 
2717b843c749SSergey Zigachev 	mqd->cp_hqd_eop_control = tmp;
2718b843c749SSergey Zigachev 
2719b843c749SSergey Zigachev 	/* enable doorbell? */
2720b843c749SSergey Zigachev 	tmp = RREG32_SOC15(GC, 0, mmCP_HQD_PQ_DOORBELL_CONTROL);
2721b843c749SSergey Zigachev 
2722b843c749SSergey Zigachev 	if (ring->use_doorbell) {
2723b843c749SSergey Zigachev 		tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
2724b843c749SSergey Zigachev 				    DOORBELL_OFFSET, ring->doorbell_index);
2725b843c749SSergey Zigachev 		tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
2726b843c749SSergey Zigachev 				    DOORBELL_EN, 1);
2727b843c749SSergey Zigachev 		tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
2728b843c749SSergey Zigachev 				    DOORBELL_SOURCE, 0);
2729b843c749SSergey Zigachev 		tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
2730b843c749SSergey Zigachev 				    DOORBELL_HIT, 0);
2731b843c749SSergey Zigachev 	} else {
2732b843c749SSergey Zigachev 		tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
2733b843c749SSergey Zigachev 					 DOORBELL_EN, 0);
2734b843c749SSergey Zigachev 	}
2735b843c749SSergey Zigachev 
2736b843c749SSergey Zigachev 	mqd->cp_hqd_pq_doorbell_control = tmp;
2737b843c749SSergey Zigachev 
2738b843c749SSergey Zigachev 	/* disable the queue if it's active */
2739b843c749SSergey Zigachev 	ring->wptr = 0;
2740b843c749SSergey Zigachev 	mqd->cp_hqd_dequeue_request = 0;
2741b843c749SSergey Zigachev 	mqd->cp_hqd_pq_rptr = 0;
2742b843c749SSergey Zigachev 	mqd->cp_hqd_pq_wptr_lo = 0;
2743b843c749SSergey Zigachev 	mqd->cp_hqd_pq_wptr_hi = 0;
2744b843c749SSergey Zigachev 
2745b843c749SSergey Zigachev 	/* set the pointer to the MQD */
2746b843c749SSergey Zigachev 	mqd->cp_mqd_base_addr_lo = ring->mqd_gpu_addr & 0xfffffffc;
2747b843c749SSergey Zigachev 	mqd->cp_mqd_base_addr_hi = upper_32_bits(ring->mqd_gpu_addr);
2748b843c749SSergey Zigachev 
2749b843c749SSergey Zigachev 	/* set MQD vmid to 0 */
2750b843c749SSergey Zigachev 	tmp = RREG32_SOC15(GC, 0, mmCP_MQD_CONTROL);
2751b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, CP_MQD_CONTROL, VMID, 0);
2752b843c749SSergey Zigachev 	mqd->cp_mqd_control = tmp;
2753b843c749SSergey Zigachev 
2754b843c749SSergey Zigachev 	/* set the pointer to the HQD, this is similar CP_RB0_BASE/_HI */
2755b843c749SSergey Zigachev 	hqd_gpu_addr = ring->gpu_addr >> 8;
2756b843c749SSergey Zigachev 	mqd->cp_hqd_pq_base_lo = hqd_gpu_addr;
2757b843c749SSergey Zigachev 	mqd->cp_hqd_pq_base_hi = upper_32_bits(hqd_gpu_addr);
2758b843c749SSergey Zigachev 
2759b843c749SSergey Zigachev 	/* set up the HQD, this is similar to CP_RB0_CNTL */
2760b843c749SSergey Zigachev 	tmp = RREG32_SOC15(GC, 0, mmCP_HQD_PQ_CONTROL);
2761b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, QUEUE_SIZE,
2762b843c749SSergey Zigachev 			    (order_base_2(ring->ring_size / 4) - 1));
2763b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, RPTR_BLOCK_SIZE,
2764b843c749SSergey Zigachev 			((order_base_2(AMDGPU_GPU_PAGE_SIZE / 4) - 1) << 8));
2765b843c749SSergey Zigachev #ifdef __BIG_ENDIAN
2766b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, ENDIAN_SWAP, 1);
2767b843c749SSergey Zigachev #endif
2768b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, UNORD_DISPATCH, 0);
2769b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, ROQ_PQ_IB_FLIP, 0);
2770b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, PRIV_STATE, 1);
2771b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, KMD_QUEUE, 1);
2772b843c749SSergey Zigachev 	mqd->cp_hqd_pq_control = tmp;
2773b843c749SSergey Zigachev 
2774b843c749SSergey Zigachev 	/* set the wb address whether it's enabled or not */
2775b843c749SSergey Zigachev 	wb_gpu_addr = adev->wb.gpu_addr + (ring->rptr_offs * 4);
2776b843c749SSergey Zigachev 	mqd->cp_hqd_pq_rptr_report_addr_lo = wb_gpu_addr & 0xfffffffc;
2777b843c749SSergey Zigachev 	mqd->cp_hqd_pq_rptr_report_addr_hi =
2778b843c749SSergey Zigachev 		upper_32_bits(wb_gpu_addr) & 0xffff;
2779b843c749SSergey Zigachev 
2780b843c749SSergey Zigachev 	/* only used if CP_PQ_WPTR_POLL_CNTL.CP_PQ_WPTR_POLL_CNTL__EN_MASK=1 */
2781b843c749SSergey Zigachev 	wb_gpu_addr = adev->wb.gpu_addr + (ring->wptr_offs * 4);
2782b843c749SSergey Zigachev 	mqd->cp_hqd_pq_wptr_poll_addr_lo = wb_gpu_addr & 0xfffffffc;
2783b843c749SSergey Zigachev 	mqd->cp_hqd_pq_wptr_poll_addr_hi = upper_32_bits(wb_gpu_addr) & 0xffff;
2784b843c749SSergey Zigachev 
2785b843c749SSergey Zigachev 	tmp = 0;
2786b843c749SSergey Zigachev 	/* enable the doorbell if requested */
2787b843c749SSergey Zigachev 	if (ring->use_doorbell) {
2788b843c749SSergey Zigachev 		tmp = RREG32_SOC15(GC, 0, mmCP_HQD_PQ_DOORBELL_CONTROL);
2789b843c749SSergey Zigachev 		tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
2790b843c749SSergey Zigachev 				DOORBELL_OFFSET, ring->doorbell_index);
2791b843c749SSergey Zigachev 
2792b843c749SSergey Zigachev 		tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
2793b843c749SSergey Zigachev 					 DOORBELL_EN, 1);
2794b843c749SSergey Zigachev 		tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
2795b843c749SSergey Zigachev 					 DOORBELL_SOURCE, 0);
2796b843c749SSergey Zigachev 		tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
2797b843c749SSergey Zigachev 					 DOORBELL_HIT, 0);
2798b843c749SSergey Zigachev 	}
2799b843c749SSergey Zigachev 
2800b843c749SSergey Zigachev 	mqd->cp_hqd_pq_doorbell_control = tmp;
2801b843c749SSergey Zigachev 
2802b843c749SSergey Zigachev 	/* reset read and write pointers, similar to CP_RB0_WPTR/_RPTR */
2803b843c749SSergey Zigachev 	ring->wptr = 0;
2804b843c749SSergey Zigachev 	mqd->cp_hqd_pq_rptr = RREG32_SOC15(GC, 0, mmCP_HQD_PQ_RPTR);
2805b843c749SSergey Zigachev 
2806b843c749SSergey Zigachev 	/* set the vmid for the queue */
2807b843c749SSergey Zigachev 	mqd->cp_hqd_vmid = 0;
2808b843c749SSergey Zigachev 
2809b843c749SSergey Zigachev 	tmp = RREG32_SOC15(GC, 0, mmCP_HQD_PERSISTENT_STATE);
2810b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, CP_HQD_PERSISTENT_STATE, PRELOAD_SIZE, 0x53);
2811b843c749SSergey Zigachev 	mqd->cp_hqd_persistent_state = tmp;
2812b843c749SSergey Zigachev 
2813b843c749SSergey Zigachev 	/* set MIN_IB_AVAIL_SIZE */
2814b843c749SSergey Zigachev 	tmp = RREG32_SOC15(GC, 0, mmCP_HQD_IB_CONTROL);
2815b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, CP_HQD_IB_CONTROL, MIN_IB_AVAIL_SIZE, 3);
2816b843c749SSergey Zigachev 	mqd->cp_hqd_ib_control = tmp;
2817b843c749SSergey Zigachev 
2818b843c749SSergey Zigachev 	/* activate the queue */
2819b843c749SSergey Zigachev 	mqd->cp_hqd_active = 1;
2820b843c749SSergey Zigachev 
2821b843c749SSergey Zigachev 	return 0;
2822b843c749SSergey Zigachev }
2823b843c749SSergey Zigachev 
gfx_v9_0_kiq_init_register(struct amdgpu_ring * ring)2824b843c749SSergey Zigachev static int gfx_v9_0_kiq_init_register(struct amdgpu_ring *ring)
2825b843c749SSergey Zigachev {
2826b843c749SSergey Zigachev 	struct amdgpu_device *adev = ring->adev;
2827b843c749SSergey Zigachev 	struct v9_mqd *mqd = ring->mqd_ptr;
2828b843c749SSergey Zigachev 	int j;
2829b843c749SSergey Zigachev 
2830b843c749SSergey Zigachev 	/* disable wptr polling */
2831b843c749SSergey Zigachev 	WREG32_FIELD15(GC, 0, CP_PQ_WPTR_POLL_CNTL, EN, 0);
2832b843c749SSergey Zigachev 
2833b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_HQD_EOP_BASE_ADDR,
2834b843c749SSergey Zigachev 	       mqd->cp_hqd_eop_base_addr_lo);
2835b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_HQD_EOP_BASE_ADDR_HI,
2836b843c749SSergey Zigachev 	       mqd->cp_hqd_eop_base_addr_hi);
2837b843c749SSergey Zigachev 
2838b843c749SSergey Zigachev 	/* set the EOP size, register value is 2^(EOP_SIZE+1) dwords */
2839b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_HQD_EOP_CONTROL,
2840b843c749SSergey Zigachev 	       mqd->cp_hqd_eop_control);
2841b843c749SSergey Zigachev 
2842b843c749SSergey Zigachev 	/* enable doorbell? */
2843b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_HQD_PQ_DOORBELL_CONTROL,
2844b843c749SSergey Zigachev 	       mqd->cp_hqd_pq_doorbell_control);
2845b843c749SSergey Zigachev 
2846b843c749SSergey Zigachev 	/* disable the queue if it's active */
2847b843c749SSergey Zigachev 	if (RREG32_SOC15(GC, 0, mmCP_HQD_ACTIVE) & 1) {
2848b843c749SSergey Zigachev 		WREG32_SOC15(GC, 0, mmCP_HQD_DEQUEUE_REQUEST, 1);
2849b843c749SSergey Zigachev 		for (j = 0; j < adev->usec_timeout; j++) {
2850b843c749SSergey Zigachev 			if (!(RREG32_SOC15(GC, 0, mmCP_HQD_ACTIVE) & 1))
2851b843c749SSergey Zigachev 				break;
2852b843c749SSergey Zigachev 			udelay(1);
2853b843c749SSergey Zigachev 		}
2854b843c749SSergey Zigachev 		WREG32_SOC15(GC, 0, mmCP_HQD_DEQUEUE_REQUEST,
2855b843c749SSergey Zigachev 		       mqd->cp_hqd_dequeue_request);
2856b843c749SSergey Zigachev 		WREG32_SOC15(GC, 0, mmCP_HQD_PQ_RPTR,
2857b843c749SSergey Zigachev 		       mqd->cp_hqd_pq_rptr);
2858b843c749SSergey Zigachev 		WREG32_SOC15(GC, 0, mmCP_HQD_PQ_WPTR_LO,
2859b843c749SSergey Zigachev 		       mqd->cp_hqd_pq_wptr_lo);
2860b843c749SSergey Zigachev 		WREG32_SOC15(GC, 0, mmCP_HQD_PQ_WPTR_HI,
2861b843c749SSergey Zigachev 		       mqd->cp_hqd_pq_wptr_hi);
2862b843c749SSergey Zigachev 	}
2863b843c749SSergey Zigachev 
2864b843c749SSergey Zigachev 	/* set the pointer to the MQD */
2865b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_MQD_BASE_ADDR,
2866b843c749SSergey Zigachev 	       mqd->cp_mqd_base_addr_lo);
2867b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_MQD_BASE_ADDR_HI,
2868b843c749SSergey Zigachev 	       mqd->cp_mqd_base_addr_hi);
2869b843c749SSergey Zigachev 
2870b843c749SSergey Zigachev 	/* set MQD vmid to 0 */
2871b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_MQD_CONTROL,
2872b843c749SSergey Zigachev 	       mqd->cp_mqd_control);
2873b843c749SSergey Zigachev 
2874b843c749SSergey Zigachev 	/* set the pointer to the HQD, this is similar CP_RB0_BASE/_HI */
2875b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_HQD_PQ_BASE,
2876b843c749SSergey Zigachev 	       mqd->cp_hqd_pq_base_lo);
2877b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_HQD_PQ_BASE_HI,
2878b843c749SSergey Zigachev 	       mqd->cp_hqd_pq_base_hi);
2879b843c749SSergey Zigachev 
2880b843c749SSergey Zigachev 	/* set up the HQD, this is similar to CP_RB0_CNTL */
2881b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_HQD_PQ_CONTROL,
2882b843c749SSergey Zigachev 	       mqd->cp_hqd_pq_control);
2883b843c749SSergey Zigachev 
2884b843c749SSergey Zigachev 	/* set the wb address whether it's enabled or not */
2885b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_HQD_PQ_RPTR_REPORT_ADDR,
2886b843c749SSergey Zigachev 				mqd->cp_hqd_pq_rptr_report_addr_lo);
2887b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_HQD_PQ_RPTR_REPORT_ADDR_HI,
2888b843c749SSergey Zigachev 				mqd->cp_hqd_pq_rptr_report_addr_hi);
2889b843c749SSergey Zigachev 
2890b843c749SSergey Zigachev 	/* only used if CP_PQ_WPTR_POLL_CNTL.CP_PQ_WPTR_POLL_CNTL__EN_MASK=1 */
2891b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_HQD_PQ_WPTR_POLL_ADDR,
2892b843c749SSergey Zigachev 	       mqd->cp_hqd_pq_wptr_poll_addr_lo);
2893b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_HQD_PQ_WPTR_POLL_ADDR_HI,
2894b843c749SSergey Zigachev 	       mqd->cp_hqd_pq_wptr_poll_addr_hi);
2895b843c749SSergey Zigachev 
2896b843c749SSergey Zigachev 	/* enable the doorbell if requested */
2897b843c749SSergey Zigachev 	if (ring->use_doorbell) {
2898b843c749SSergey Zigachev 		WREG32_SOC15(GC, 0, mmCP_MEC_DOORBELL_RANGE_LOWER,
2899b843c749SSergey Zigachev 					(AMDGPU_DOORBELL64_KIQ *2) << 2);
2900b843c749SSergey Zigachev 		WREG32_SOC15(GC, 0, mmCP_MEC_DOORBELL_RANGE_UPPER,
2901b843c749SSergey Zigachev 					(AMDGPU_DOORBELL64_USERQUEUE_END * 2) << 2);
2902b843c749SSergey Zigachev 	}
2903b843c749SSergey Zigachev 
2904b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_HQD_PQ_DOORBELL_CONTROL,
2905b843c749SSergey Zigachev 	       mqd->cp_hqd_pq_doorbell_control);
2906b843c749SSergey Zigachev 
2907b843c749SSergey Zigachev 	/* reset read and write pointers, similar to CP_RB0_WPTR/_RPTR */
2908b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_HQD_PQ_WPTR_LO,
2909b843c749SSergey Zigachev 	       mqd->cp_hqd_pq_wptr_lo);
2910b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_HQD_PQ_WPTR_HI,
2911b843c749SSergey Zigachev 	       mqd->cp_hqd_pq_wptr_hi);
2912b843c749SSergey Zigachev 
2913b843c749SSergey Zigachev 	/* set the vmid for the queue */
2914b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_HQD_VMID, mqd->cp_hqd_vmid);
2915b843c749SSergey Zigachev 
2916b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_HQD_PERSISTENT_STATE,
2917b843c749SSergey Zigachev 	       mqd->cp_hqd_persistent_state);
2918b843c749SSergey Zigachev 
2919b843c749SSergey Zigachev 	/* activate the queue */
2920b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_HQD_ACTIVE,
2921b843c749SSergey Zigachev 	       mqd->cp_hqd_active);
2922b843c749SSergey Zigachev 
2923b843c749SSergey Zigachev 	if (ring->use_doorbell)
2924b843c749SSergey Zigachev 		WREG32_FIELD15(GC, 0, CP_PQ_STATUS, DOORBELL_ENABLE, 1);
2925b843c749SSergey Zigachev 
2926b843c749SSergey Zigachev 	return 0;
2927b843c749SSergey Zigachev }
2928b843c749SSergey Zigachev 
gfx_v9_0_kiq_fini_register(struct amdgpu_ring * ring)2929b843c749SSergey Zigachev static int gfx_v9_0_kiq_fini_register(struct amdgpu_ring *ring)
2930b843c749SSergey Zigachev {
2931b843c749SSergey Zigachev 	struct amdgpu_device *adev = ring->adev;
2932b843c749SSergey Zigachev 	int j;
2933b843c749SSergey Zigachev 
2934b843c749SSergey Zigachev 	/* disable the queue if it's active */
2935b843c749SSergey Zigachev 	if (RREG32_SOC15(GC, 0, mmCP_HQD_ACTIVE) & 1) {
2936b843c749SSergey Zigachev 
2937b843c749SSergey Zigachev 		WREG32_SOC15(GC, 0, mmCP_HQD_DEQUEUE_REQUEST, 1);
2938b843c749SSergey Zigachev 
2939b843c749SSergey Zigachev 		for (j = 0; j < adev->usec_timeout; j++) {
2940b843c749SSergey Zigachev 			if (!(RREG32_SOC15(GC, 0, mmCP_HQD_ACTIVE) & 1))
2941b843c749SSergey Zigachev 				break;
2942b843c749SSergey Zigachev 			udelay(1);
2943b843c749SSergey Zigachev 		}
2944b843c749SSergey Zigachev 
2945b843c749SSergey Zigachev 		if (j == AMDGPU_MAX_USEC_TIMEOUT) {
2946b843c749SSergey Zigachev 			DRM_DEBUG("KIQ dequeue request failed.\n");
2947b843c749SSergey Zigachev 
2948b843c749SSergey Zigachev 			/* Manual disable if dequeue request times out */
2949b843c749SSergey Zigachev 			WREG32_SOC15(GC, 0, mmCP_HQD_ACTIVE, 0);
2950b843c749SSergey Zigachev 		}
2951b843c749SSergey Zigachev 
2952b843c749SSergey Zigachev 		WREG32_SOC15(GC, 0, mmCP_HQD_DEQUEUE_REQUEST,
2953b843c749SSergey Zigachev 		      0);
2954b843c749SSergey Zigachev 	}
2955b843c749SSergey Zigachev 
2956b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_HQD_IQ_TIMER, 0);
2957b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_HQD_IB_CONTROL, 0);
2958b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_HQD_PERSISTENT_STATE, 0);
2959b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_HQD_PQ_DOORBELL_CONTROL, 0x40000000);
2960b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_HQD_PQ_DOORBELL_CONTROL, 0);
2961b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_HQD_PQ_RPTR, 0);
2962b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_HQD_PQ_WPTR_HI, 0);
2963b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_HQD_PQ_WPTR_LO, 0);
2964b843c749SSergey Zigachev 
2965b843c749SSergey Zigachev 	return 0;
2966b843c749SSergey Zigachev }
2967b843c749SSergey Zigachev 
gfx_v9_0_kiq_init_queue(struct amdgpu_ring * ring)2968b843c749SSergey Zigachev static int gfx_v9_0_kiq_init_queue(struct amdgpu_ring *ring)
2969b843c749SSergey Zigachev {
2970b843c749SSergey Zigachev 	struct amdgpu_device *adev = ring->adev;
2971b843c749SSergey Zigachev 	struct v9_mqd *mqd = ring->mqd_ptr;
2972b843c749SSergey Zigachev 	int mqd_idx = AMDGPU_MAX_COMPUTE_RINGS;
2973b843c749SSergey Zigachev 
2974b843c749SSergey Zigachev 	gfx_v9_0_kiq_setting(ring);
2975b843c749SSergey Zigachev 
2976b843c749SSergey Zigachev 	if (adev->in_gpu_reset) { /* for GPU_RESET case */
2977b843c749SSergey Zigachev 		/* reset MQD to a clean status */
2978b843c749SSergey Zigachev 		if (adev->gfx.mec.mqd_backup[mqd_idx])
2979b843c749SSergey Zigachev 			memcpy(mqd, adev->gfx.mec.mqd_backup[mqd_idx], sizeof(struct v9_mqd_allocation));
2980b843c749SSergey Zigachev 
2981b843c749SSergey Zigachev 		/* reset ring buffer */
2982b843c749SSergey Zigachev 		ring->wptr = 0;
2983b843c749SSergey Zigachev 		amdgpu_ring_clear_ring(ring);
2984b843c749SSergey Zigachev 
2985b843c749SSergey Zigachev 		mutex_lock(&adev->srbm_mutex);
2986b843c749SSergey Zigachev 		soc15_grbm_select(adev, ring->me, ring->pipe, ring->queue, 0);
2987b843c749SSergey Zigachev 		gfx_v9_0_kiq_init_register(ring);
2988b843c749SSergey Zigachev 		soc15_grbm_select(adev, 0, 0, 0, 0);
2989b843c749SSergey Zigachev 		mutex_unlock(&adev->srbm_mutex);
2990b843c749SSergey Zigachev 	} else {
2991b843c749SSergey Zigachev 		memset((void *)mqd, 0, sizeof(struct v9_mqd_allocation));
2992b843c749SSergey Zigachev 		((struct v9_mqd_allocation *)mqd)->dynamic_cu_mask = 0xFFFFFFFF;
2993b843c749SSergey Zigachev 		((struct v9_mqd_allocation *)mqd)->dynamic_rb_mask = 0xFFFFFFFF;
2994b843c749SSergey Zigachev 		mutex_lock(&adev->srbm_mutex);
2995b843c749SSergey Zigachev 		soc15_grbm_select(adev, ring->me, ring->pipe, ring->queue, 0);
2996b843c749SSergey Zigachev 		gfx_v9_0_mqd_init(ring);
2997b843c749SSergey Zigachev 		gfx_v9_0_kiq_init_register(ring);
2998b843c749SSergey Zigachev 		soc15_grbm_select(adev, 0, 0, 0, 0);
2999b843c749SSergey Zigachev 		mutex_unlock(&adev->srbm_mutex);
3000b843c749SSergey Zigachev 
3001b843c749SSergey Zigachev 		if (adev->gfx.mec.mqd_backup[mqd_idx])
3002b843c749SSergey Zigachev 			memcpy(adev->gfx.mec.mqd_backup[mqd_idx], mqd, sizeof(struct v9_mqd_allocation));
3003b843c749SSergey Zigachev 	}
3004b843c749SSergey Zigachev 
3005b843c749SSergey Zigachev 	return 0;
3006b843c749SSergey Zigachev }
3007b843c749SSergey Zigachev 
gfx_v9_0_kcq_init_queue(struct amdgpu_ring * ring)3008b843c749SSergey Zigachev static int gfx_v9_0_kcq_init_queue(struct amdgpu_ring *ring)
3009b843c749SSergey Zigachev {
3010b843c749SSergey Zigachev 	struct amdgpu_device *adev = ring->adev;
3011b843c749SSergey Zigachev 	struct v9_mqd *mqd = ring->mqd_ptr;
3012b843c749SSergey Zigachev 	int mqd_idx = ring - &adev->gfx.compute_ring[0];
3013b843c749SSergey Zigachev 
3014b843c749SSergey Zigachev 	if (!adev->in_gpu_reset && !adev->gfx.in_suspend) {
3015b843c749SSergey Zigachev 		memset((void *)mqd, 0, sizeof(struct v9_mqd_allocation));
3016b843c749SSergey Zigachev 		((struct v9_mqd_allocation *)mqd)->dynamic_cu_mask = 0xFFFFFFFF;
3017b843c749SSergey Zigachev 		((struct v9_mqd_allocation *)mqd)->dynamic_rb_mask = 0xFFFFFFFF;
3018b843c749SSergey Zigachev 		mutex_lock(&adev->srbm_mutex);
3019b843c749SSergey Zigachev 		soc15_grbm_select(adev, ring->me, ring->pipe, ring->queue, 0);
3020b843c749SSergey Zigachev 		gfx_v9_0_mqd_init(ring);
3021b843c749SSergey Zigachev 		soc15_grbm_select(adev, 0, 0, 0, 0);
3022b843c749SSergey Zigachev 		mutex_unlock(&adev->srbm_mutex);
3023b843c749SSergey Zigachev 
3024b843c749SSergey Zigachev 		if (adev->gfx.mec.mqd_backup[mqd_idx])
3025b843c749SSergey Zigachev 			memcpy(adev->gfx.mec.mqd_backup[mqd_idx], mqd, sizeof(struct v9_mqd_allocation));
3026b843c749SSergey Zigachev 	} else if (adev->in_gpu_reset) { /* for GPU_RESET case */
3027b843c749SSergey Zigachev 		/* reset MQD to a clean status */
3028b843c749SSergey Zigachev 		if (adev->gfx.mec.mqd_backup[mqd_idx])
3029b843c749SSergey Zigachev 			memcpy(mqd, adev->gfx.mec.mqd_backup[mqd_idx], sizeof(struct v9_mqd_allocation));
3030b843c749SSergey Zigachev 
3031b843c749SSergey Zigachev 		/* reset ring buffer */
3032b843c749SSergey Zigachev 		ring->wptr = 0;
3033b843c749SSergey Zigachev 		amdgpu_ring_clear_ring(ring);
3034b843c749SSergey Zigachev 	} else {
3035b843c749SSergey Zigachev 		amdgpu_ring_clear_ring(ring);
3036b843c749SSergey Zigachev 	}
3037b843c749SSergey Zigachev 
3038b843c749SSergey Zigachev 	return 0;
3039b843c749SSergey Zigachev }
3040b843c749SSergey Zigachev 
gfx_v9_0_kiq_resume(struct amdgpu_device * adev)3041b843c749SSergey Zigachev static int gfx_v9_0_kiq_resume(struct amdgpu_device *adev)
3042b843c749SSergey Zigachev {
3043b843c749SSergey Zigachev 	struct amdgpu_ring *ring = NULL;
3044b843c749SSergey Zigachev 	int r = 0, i;
3045b843c749SSergey Zigachev 
3046b843c749SSergey Zigachev 	gfx_v9_0_cp_compute_enable(adev, true);
3047b843c749SSergey Zigachev 
3048b843c749SSergey Zigachev 	ring = &adev->gfx.kiq.ring;
3049b843c749SSergey Zigachev 
3050b843c749SSergey Zigachev 	r = amdgpu_bo_reserve(ring->mqd_obj, false);
3051b843c749SSergey Zigachev 	if (unlikely(r != 0))
3052b843c749SSergey Zigachev 		goto done;
3053b843c749SSergey Zigachev 
3054b843c749SSergey Zigachev 	r = amdgpu_bo_kmap(ring->mqd_obj, (void **)&ring->mqd_ptr);
3055b843c749SSergey Zigachev 	if (!r) {
3056b843c749SSergey Zigachev 		r = gfx_v9_0_kiq_init_queue(ring);
3057b843c749SSergey Zigachev 		amdgpu_bo_kunmap(ring->mqd_obj);
3058b843c749SSergey Zigachev 		ring->mqd_ptr = NULL;
3059b843c749SSergey Zigachev 	}
3060b843c749SSergey Zigachev 	amdgpu_bo_unreserve(ring->mqd_obj);
3061b843c749SSergey Zigachev 	if (r)
3062b843c749SSergey Zigachev 		goto done;
3063b843c749SSergey Zigachev 
3064b843c749SSergey Zigachev 	for (i = 0; i < adev->gfx.num_compute_rings; i++) {
3065b843c749SSergey Zigachev 		ring = &adev->gfx.compute_ring[i];
3066b843c749SSergey Zigachev 
3067b843c749SSergey Zigachev 		r = amdgpu_bo_reserve(ring->mqd_obj, false);
3068b843c749SSergey Zigachev 		if (unlikely(r != 0))
3069b843c749SSergey Zigachev 			goto done;
3070b843c749SSergey Zigachev 		r = amdgpu_bo_kmap(ring->mqd_obj, (void **)&ring->mqd_ptr);
3071b843c749SSergey Zigachev 		if (!r) {
3072b843c749SSergey Zigachev 			r = gfx_v9_0_kcq_init_queue(ring);
3073b843c749SSergey Zigachev 			amdgpu_bo_kunmap(ring->mqd_obj);
3074b843c749SSergey Zigachev 			ring->mqd_ptr = NULL;
3075b843c749SSergey Zigachev 		}
3076b843c749SSergey Zigachev 		amdgpu_bo_unreserve(ring->mqd_obj);
3077b843c749SSergey Zigachev 		if (r)
3078b843c749SSergey Zigachev 			goto done;
3079b843c749SSergey Zigachev 	}
3080b843c749SSergey Zigachev 
3081b843c749SSergey Zigachev 	r = gfx_v9_0_kiq_kcq_enable(adev);
3082b843c749SSergey Zigachev done:
3083b843c749SSergey Zigachev 	return r;
3084b843c749SSergey Zigachev }
3085b843c749SSergey Zigachev 
gfx_v9_0_cp_resume(struct amdgpu_device * adev)3086b843c749SSergey Zigachev static int gfx_v9_0_cp_resume(struct amdgpu_device *adev)
3087b843c749SSergey Zigachev {
3088b843c749SSergey Zigachev 	int r, i;
3089b843c749SSergey Zigachev 	struct amdgpu_ring *ring;
3090b843c749SSergey Zigachev 
3091b843c749SSergey Zigachev 	if (!(adev->flags & AMD_IS_APU))
3092b843c749SSergey Zigachev 		gfx_v9_0_enable_gui_idle_interrupt(adev, false);
3093b843c749SSergey Zigachev 
3094b843c749SSergey Zigachev 	if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
3095b843c749SSergey Zigachev 		/* legacy firmware loading */
3096b843c749SSergey Zigachev 		r = gfx_v9_0_cp_gfx_load_microcode(adev);
3097b843c749SSergey Zigachev 		if (r)
3098b843c749SSergey Zigachev 			return r;
3099b843c749SSergey Zigachev 
3100b843c749SSergey Zigachev 		r = gfx_v9_0_cp_compute_load_microcode(adev);
3101b843c749SSergey Zigachev 		if (r)
3102b843c749SSergey Zigachev 			return r;
3103b843c749SSergey Zigachev 	}
3104b843c749SSergey Zigachev 
3105b843c749SSergey Zigachev 	r = gfx_v9_0_cp_gfx_resume(adev);
3106b843c749SSergey Zigachev 	if (r)
3107b843c749SSergey Zigachev 		return r;
3108b843c749SSergey Zigachev 
3109b843c749SSergey Zigachev 	r = gfx_v9_0_kiq_resume(adev);
3110b843c749SSergey Zigachev 	if (r)
3111b843c749SSergey Zigachev 		return r;
3112b843c749SSergey Zigachev 
3113b843c749SSergey Zigachev 	ring = &adev->gfx.gfx_ring[0];
3114b843c749SSergey Zigachev 	r = amdgpu_ring_test_ring(ring);
3115b843c749SSergey Zigachev 	if (r) {
3116b843c749SSergey Zigachev 		ring->ready = false;
3117b843c749SSergey Zigachev 		return r;
3118b843c749SSergey Zigachev 	}
3119b843c749SSergey Zigachev 
3120b843c749SSergey Zigachev 	ring = &adev->gfx.kiq.ring;
3121b843c749SSergey Zigachev 	ring->ready = true;
3122b843c749SSergey Zigachev 	r = amdgpu_ring_test_ring(ring);
3123b843c749SSergey Zigachev 	if (r)
3124b843c749SSergey Zigachev 		ring->ready = false;
3125b843c749SSergey Zigachev 
3126b843c749SSergey Zigachev 	for (i = 0; i < adev->gfx.num_compute_rings; i++) {
3127b843c749SSergey Zigachev 		ring = &adev->gfx.compute_ring[i];
3128b843c749SSergey Zigachev 
3129b843c749SSergey Zigachev 		ring->ready = true;
3130b843c749SSergey Zigachev 		r = amdgpu_ring_test_ring(ring);
3131b843c749SSergey Zigachev 		if (r)
3132b843c749SSergey Zigachev 			ring->ready = false;
3133b843c749SSergey Zigachev 	}
3134b843c749SSergey Zigachev 
3135b843c749SSergey Zigachev 	gfx_v9_0_enable_gui_idle_interrupt(adev, true);
3136b843c749SSergey Zigachev 
3137b843c749SSergey Zigachev 	return 0;
3138b843c749SSergey Zigachev }
3139b843c749SSergey Zigachev 
gfx_v9_0_cp_enable(struct amdgpu_device * adev,bool enable)3140b843c749SSergey Zigachev static void gfx_v9_0_cp_enable(struct amdgpu_device *adev, bool enable)
3141b843c749SSergey Zigachev {
3142b843c749SSergey Zigachev 	gfx_v9_0_cp_gfx_enable(adev, enable);
3143b843c749SSergey Zigachev 	gfx_v9_0_cp_compute_enable(adev, enable);
3144b843c749SSergey Zigachev }
3145b843c749SSergey Zigachev 
gfx_v9_0_hw_init(void * handle)3146b843c749SSergey Zigachev static int gfx_v9_0_hw_init(void *handle)
3147b843c749SSergey Zigachev {
3148b843c749SSergey Zigachev 	int r;
3149b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3150b843c749SSergey Zigachev 
3151b843c749SSergey Zigachev 	gfx_v9_0_init_golden_registers(adev);
3152b843c749SSergey Zigachev 
3153b843c749SSergey Zigachev 	gfx_v9_0_gpu_init(adev);
3154b843c749SSergey Zigachev 
3155b843c749SSergey Zigachev 	r = gfx_v9_0_csb_vram_pin(adev);
3156b843c749SSergey Zigachev 	if (r)
3157b843c749SSergey Zigachev 		return r;
3158b843c749SSergey Zigachev 
3159b843c749SSergey Zigachev 	r = gfx_v9_0_rlc_resume(adev);
3160b843c749SSergey Zigachev 	if (r)
3161b843c749SSergey Zigachev 		return r;
3162b843c749SSergey Zigachev 
3163b843c749SSergey Zigachev 	r = gfx_v9_0_cp_resume(adev);
3164b843c749SSergey Zigachev 	if (r)
3165b843c749SSergey Zigachev 		return r;
3166b843c749SSergey Zigachev 
3167b843c749SSergey Zigachev 	r = gfx_v9_0_ngg_en(adev);
3168b843c749SSergey Zigachev 	if (r)
3169b843c749SSergey Zigachev 		return r;
3170b843c749SSergey Zigachev 
3171b843c749SSergey Zigachev 	return r;
3172b843c749SSergey Zigachev }
3173b843c749SSergey Zigachev 
gfx_v9_0_kcq_disable(struct amdgpu_ring * kiq_ring,struct amdgpu_ring * ring)3174b843c749SSergey Zigachev static int gfx_v9_0_kcq_disable(struct amdgpu_ring *kiq_ring,struct amdgpu_ring *ring)
3175b843c749SSergey Zigachev {
3176b843c749SSergey Zigachev 	struct amdgpu_device *adev = kiq_ring->adev;
3177b843c749SSergey Zigachev 	uint32_t scratch, tmp = 0;
3178b843c749SSergey Zigachev 	int r, i;
3179b843c749SSergey Zigachev 
3180b843c749SSergey Zigachev 	r = amdgpu_gfx_scratch_get(adev, &scratch);
3181b843c749SSergey Zigachev 	if (r) {
3182b843c749SSergey Zigachev 		DRM_ERROR("Failed to get scratch reg (%d).\n", r);
3183b843c749SSergey Zigachev 		return r;
3184b843c749SSergey Zigachev 	}
3185b843c749SSergey Zigachev 	WREG32(scratch, 0xCAFEDEAD);
3186b843c749SSergey Zigachev 
3187b843c749SSergey Zigachev 	r = amdgpu_ring_alloc(kiq_ring, 10);
3188b843c749SSergey Zigachev 	if (r) {
3189b843c749SSergey Zigachev 		DRM_ERROR("Failed to lock KIQ (%d).\n", r);
3190b843c749SSergey Zigachev 		amdgpu_gfx_scratch_free(adev, scratch);
3191b843c749SSergey Zigachev 		return r;
3192b843c749SSergey Zigachev 	}
3193b843c749SSergey Zigachev 
3194b843c749SSergey Zigachev 	/* unmap queues */
3195b843c749SSergey Zigachev 	amdgpu_ring_write(kiq_ring, PACKET3(PACKET3_UNMAP_QUEUES, 4));
3196b843c749SSergey Zigachev 	amdgpu_ring_write(kiq_ring, /* Q_sel: 0, vmid: 0, engine: 0, num_Q: 1 */
3197b843c749SSergey Zigachev 						PACKET3_UNMAP_QUEUES_ACTION(1) | /* RESET_QUEUES */
3198b843c749SSergey Zigachev 						PACKET3_UNMAP_QUEUES_QUEUE_SEL(0) |
3199b843c749SSergey Zigachev 						PACKET3_UNMAP_QUEUES_ENGINE_SEL(0) |
3200b843c749SSergey Zigachev 						PACKET3_UNMAP_QUEUES_NUM_QUEUES(1));
3201b843c749SSergey Zigachev 	amdgpu_ring_write(kiq_ring, PACKET3_UNMAP_QUEUES_DOORBELL_OFFSET0(ring->doorbell_index));
3202b843c749SSergey Zigachev 	amdgpu_ring_write(kiq_ring, 0);
3203b843c749SSergey Zigachev 	amdgpu_ring_write(kiq_ring, 0);
3204b843c749SSergey Zigachev 	amdgpu_ring_write(kiq_ring, 0);
3205b843c749SSergey Zigachev 	/* write to scratch for completion */
3206b843c749SSergey Zigachev 	amdgpu_ring_write(kiq_ring, PACKET3(PACKET3_SET_UCONFIG_REG, 1));
3207b843c749SSergey Zigachev 	amdgpu_ring_write(kiq_ring, (scratch - PACKET3_SET_UCONFIG_REG_START));
3208b843c749SSergey Zigachev 	amdgpu_ring_write(kiq_ring, 0xDEADBEEF);
3209b843c749SSergey Zigachev 	amdgpu_ring_commit(kiq_ring);
3210b843c749SSergey Zigachev 
3211b843c749SSergey Zigachev 	for (i = 0; i < adev->usec_timeout; i++) {
3212b843c749SSergey Zigachev 		tmp = RREG32(scratch);
3213b843c749SSergey Zigachev 		if (tmp == 0xDEADBEEF)
3214b843c749SSergey Zigachev 			break;
3215b843c749SSergey Zigachev 		DRM_UDELAY(1);
3216b843c749SSergey Zigachev 	}
3217b843c749SSergey Zigachev 	if (i >= adev->usec_timeout) {
3218b843c749SSergey Zigachev 		DRM_ERROR("KCQ disabled failed (scratch(0x%04X)=0x%08X)\n", scratch, tmp);
3219b843c749SSergey Zigachev 		r = -EINVAL;
3220b843c749SSergey Zigachev 	}
3221b843c749SSergey Zigachev 	amdgpu_gfx_scratch_free(adev, scratch);
3222b843c749SSergey Zigachev 	return r;
3223b843c749SSergey Zigachev }
3224b843c749SSergey Zigachev 
gfx_v9_0_hw_fini(void * handle)3225b843c749SSergey Zigachev static int gfx_v9_0_hw_fini(void *handle)
3226b843c749SSergey Zigachev {
3227b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3228b843c749SSergey Zigachev 	int i;
3229b843c749SSergey Zigachev 
3230b843c749SSergey Zigachev 	amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_GFX,
3231b843c749SSergey Zigachev 					       AMD_PG_STATE_UNGATE);
3232b843c749SSergey Zigachev 
3233b843c749SSergey Zigachev 	amdgpu_irq_put(adev, &adev->gfx.priv_reg_irq, 0);
3234b843c749SSergey Zigachev 	amdgpu_irq_put(adev, &adev->gfx.priv_inst_irq, 0);
3235b843c749SSergey Zigachev 
3236b843c749SSergey Zigachev 	/* disable KCQ to avoid CPC touch memory not valid anymore */
3237b843c749SSergey Zigachev 	for (i = 0; i < adev->gfx.num_compute_rings; i++)
3238b843c749SSergey Zigachev 		gfx_v9_0_kcq_disable(&adev->gfx.kiq.ring, &adev->gfx.compute_ring[i]);
3239b843c749SSergey Zigachev 
3240b843c749SSergey Zigachev 	if (amdgpu_sriov_vf(adev)) {
3241b843c749SSergey Zigachev 		gfx_v9_0_cp_gfx_enable(adev, false);
3242b843c749SSergey Zigachev 		/* must disable polling for SRIOV when hw finished, otherwise
3243b843c749SSergey Zigachev 		 * CPC engine may still keep fetching WB address which is already
3244b843c749SSergey Zigachev 		 * invalid after sw finished and trigger DMAR reading error in
3245b843c749SSergey Zigachev 		 * hypervisor side.
3246b843c749SSergey Zigachev 		 */
3247b843c749SSergey Zigachev 		WREG32_FIELD15(GC, 0, CP_PQ_WPTR_POLL_CNTL, EN, 0);
3248b843c749SSergey Zigachev 		return 0;
3249b843c749SSergey Zigachev 	}
3250b843c749SSergey Zigachev 
3251b843c749SSergey Zigachev 	/* Use deinitialize sequence from CAIL when unbinding device from driver,
3252b843c749SSergey Zigachev 	 * otherwise KIQ is hanging when binding back
3253b843c749SSergey Zigachev 	 */
3254b843c749SSergey Zigachev 	if (!adev->in_gpu_reset && !adev->gfx.in_suspend) {
3255b843c749SSergey Zigachev 		mutex_lock(&adev->srbm_mutex);
3256b843c749SSergey Zigachev 		soc15_grbm_select(adev, adev->gfx.kiq.ring.me,
3257b843c749SSergey Zigachev 				adev->gfx.kiq.ring.pipe,
3258b843c749SSergey Zigachev 				adev->gfx.kiq.ring.queue, 0);
3259b843c749SSergey Zigachev 		gfx_v9_0_kiq_fini_register(&adev->gfx.kiq.ring);
3260b843c749SSergey Zigachev 		soc15_grbm_select(adev, 0, 0, 0, 0);
3261b843c749SSergey Zigachev 		mutex_unlock(&adev->srbm_mutex);
3262b843c749SSergey Zigachev 	}
3263b843c749SSergey Zigachev 
3264b843c749SSergey Zigachev 	gfx_v9_0_cp_enable(adev, false);
3265b843c749SSergey Zigachev 	gfx_v9_0_rlc_stop(adev);
3266b843c749SSergey Zigachev 
3267b843c749SSergey Zigachev 	gfx_v9_0_csb_vram_unpin(adev);
3268b843c749SSergey Zigachev 
3269b843c749SSergey Zigachev 	return 0;
3270b843c749SSergey Zigachev }
3271b843c749SSergey Zigachev 
gfx_v9_0_suspend(void * handle)3272b843c749SSergey Zigachev static int gfx_v9_0_suspend(void *handle)
3273b843c749SSergey Zigachev {
3274b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3275b843c749SSergey Zigachev 
3276b843c749SSergey Zigachev 	adev->gfx.in_suspend = true;
3277b843c749SSergey Zigachev 	return gfx_v9_0_hw_fini(adev);
3278b843c749SSergey Zigachev }
3279b843c749SSergey Zigachev 
gfx_v9_0_resume(void * handle)3280b843c749SSergey Zigachev static int gfx_v9_0_resume(void *handle)
3281b843c749SSergey Zigachev {
3282b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3283b843c749SSergey Zigachev 	int r;
3284b843c749SSergey Zigachev 
3285b843c749SSergey Zigachev 	r = gfx_v9_0_hw_init(adev);
3286b843c749SSergey Zigachev 	adev->gfx.in_suspend = false;
3287b843c749SSergey Zigachev 	return r;
3288b843c749SSergey Zigachev }
3289b843c749SSergey Zigachev 
gfx_v9_0_is_idle(void * handle)3290b843c749SSergey Zigachev static bool gfx_v9_0_is_idle(void *handle)
3291b843c749SSergey Zigachev {
3292b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3293b843c749SSergey Zigachev 
3294b843c749SSergey Zigachev 	if (REG_GET_FIELD(RREG32_SOC15(GC, 0, mmGRBM_STATUS),
3295b843c749SSergey Zigachev 				GRBM_STATUS, GUI_ACTIVE))
3296b843c749SSergey Zigachev 		return false;
3297b843c749SSergey Zigachev 	else
3298b843c749SSergey Zigachev 		return true;
3299b843c749SSergey Zigachev }
3300b843c749SSergey Zigachev 
gfx_v9_0_wait_for_idle(void * handle)3301b843c749SSergey Zigachev static int gfx_v9_0_wait_for_idle(void *handle)
3302b843c749SSergey Zigachev {
3303b843c749SSergey Zigachev 	unsigned i;
3304b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3305b843c749SSergey Zigachev 
3306b843c749SSergey Zigachev 	for (i = 0; i < adev->usec_timeout; i++) {
3307b843c749SSergey Zigachev 		if (gfx_v9_0_is_idle(handle))
3308b843c749SSergey Zigachev 			return 0;
3309b843c749SSergey Zigachev 		udelay(1);
3310b843c749SSergey Zigachev 	}
3311b843c749SSergey Zigachev 	return -ETIMEDOUT;
3312b843c749SSergey Zigachev }
3313b843c749SSergey Zigachev 
gfx_v9_0_soft_reset(void * handle)3314b843c749SSergey Zigachev static int gfx_v9_0_soft_reset(void *handle)
3315b843c749SSergey Zigachev {
3316b843c749SSergey Zigachev 	u32 grbm_soft_reset = 0;
3317b843c749SSergey Zigachev 	u32 tmp;
3318b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3319b843c749SSergey Zigachev 
3320b843c749SSergey Zigachev 	/* GRBM_STATUS */
3321b843c749SSergey Zigachev 	tmp = RREG32_SOC15(GC, 0, mmGRBM_STATUS);
3322b843c749SSergey Zigachev 	if (tmp & (GRBM_STATUS__PA_BUSY_MASK | GRBM_STATUS__SC_BUSY_MASK |
3323b843c749SSergey Zigachev 		   GRBM_STATUS__BCI_BUSY_MASK | GRBM_STATUS__SX_BUSY_MASK |
3324b843c749SSergey Zigachev 		   GRBM_STATUS__TA_BUSY_MASK | GRBM_STATUS__VGT_BUSY_MASK |
3325b843c749SSergey Zigachev 		   GRBM_STATUS__DB_BUSY_MASK | GRBM_STATUS__CB_BUSY_MASK |
3326b843c749SSergey Zigachev 		   GRBM_STATUS__GDS_BUSY_MASK | GRBM_STATUS__SPI_BUSY_MASK |
3327b843c749SSergey Zigachev 		   GRBM_STATUS__IA_BUSY_MASK | GRBM_STATUS__IA_BUSY_NO_DMA_MASK)) {
3328b843c749SSergey Zigachev 		grbm_soft_reset = REG_SET_FIELD(grbm_soft_reset,
3329b843c749SSergey Zigachev 						GRBM_SOFT_RESET, SOFT_RESET_CP, 1);
3330b843c749SSergey Zigachev 		grbm_soft_reset = REG_SET_FIELD(grbm_soft_reset,
3331b843c749SSergey Zigachev 						GRBM_SOFT_RESET, SOFT_RESET_GFX, 1);
3332b843c749SSergey Zigachev 	}
3333b843c749SSergey Zigachev 
3334b843c749SSergey Zigachev 	if (tmp & (GRBM_STATUS__CP_BUSY_MASK | GRBM_STATUS__CP_COHERENCY_BUSY_MASK)) {
3335b843c749SSergey Zigachev 		grbm_soft_reset = REG_SET_FIELD(grbm_soft_reset,
3336b843c749SSergey Zigachev 						GRBM_SOFT_RESET, SOFT_RESET_CP, 1);
3337b843c749SSergey Zigachev 	}
3338b843c749SSergey Zigachev 
3339b843c749SSergey Zigachev 	/* GRBM_STATUS2 */
3340b843c749SSergey Zigachev 	tmp = RREG32_SOC15(GC, 0, mmGRBM_STATUS2);
3341b843c749SSergey Zigachev 	if (REG_GET_FIELD(tmp, GRBM_STATUS2, RLC_BUSY))
3342b843c749SSergey Zigachev 		grbm_soft_reset = REG_SET_FIELD(grbm_soft_reset,
3343b843c749SSergey Zigachev 						GRBM_SOFT_RESET, SOFT_RESET_RLC, 1);
3344b843c749SSergey Zigachev 
3345b843c749SSergey Zigachev 
3346b843c749SSergey Zigachev 	if (grbm_soft_reset) {
3347b843c749SSergey Zigachev 		/* stop the rlc */
3348b843c749SSergey Zigachev 		gfx_v9_0_rlc_stop(adev);
3349b843c749SSergey Zigachev 
3350b843c749SSergey Zigachev 		/* Disable GFX parsing/prefetching */
3351b843c749SSergey Zigachev 		gfx_v9_0_cp_gfx_enable(adev, false);
3352b843c749SSergey Zigachev 
3353b843c749SSergey Zigachev 		/* Disable MEC parsing/prefetching */
3354b843c749SSergey Zigachev 		gfx_v9_0_cp_compute_enable(adev, false);
3355b843c749SSergey Zigachev 
3356b843c749SSergey Zigachev 		if (grbm_soft_reset) {
3357b843c749SSergey Zigachev 			tmp = RREG32_SOC15(GC, 0, mmGRBM_SOFT_RESET);
3358b843c749SSergey Zigachev 			tmp |= grbm_soft_reset;
3359b843c749SSergey Zigachev 			dev_info(adev->dev, "GRBM_SOFT_RESET=0x%08X\n", tmp);
3360b843c749SSergey Zigachev 			WREG32_SOC15(GC, 0, mmGRBM_SOFT_RESET, tmp);
3361b843c749SSergey Zigachev 			tmp = RREG32_SOC15(GC, 0, mmGRBM_SOFT_RESET);
3362b843c749SSergey Zigachev 
3363b843c749SSergey Zigachev 			udelay(50);
3364b843c749SSergey Zigachev 
3365b843c749SSergey Zigachev 			tmp &= ~grbm_soft_reset;
3366b843c749SSergey Zigachev 			WREG32_SOC15(GC, 0, mmGRBM_SOFT_RESET, tmp);
3367b843c749SSergey Zigachev 			tmp = RREG32_SOC15(GC, 0, mmGRBM_SOFT_RESET);
3368b843c749SSergey Zigachev 		}
3369b843c749SSergey Zigachev 
3370b843c749SSergey Zigachev 		/* Wait a little for things to settle down */
3371b843c749SSergey Zigachev 		udelay(50);
3372b843c749SSergey Zigachev 	}
3373b843c749SSergey Zigachev 	return 0;
3374b843c749SSergey Zigachev }
3375b843c749SSergey Zigachev 
gfx_v9_0_get_gpu_clock_counter(struct amdgpu_device * adev)3376b843c749SSergey Zigachev static uint64_t gfx_v9_0_get_gpu_clock_counter(struct amdgpu_device *adev)
3377b843c749SSergey Zigachev {
3378b843c749SSergey Zigachev 	uint64_t clock;
3379b843c749SSergey Zigachev 
3380b843c749SSergey Zigachev 	mutex_lock(&adev->gfx.gpu_clock_mutex);
3381b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmRLC_CAPTURE_GPU_CLOCK_COUNT, 1);
3382b843c749SSergey Zigachev 	clock = (uint64_t)RREG32_SOC15(GC, 0, mmRLC_GPU_CLOCK_COUNT_LSB) |
3383b843c749SSergey Zigachev 		((uint64_t)RREG32_SOC15(GC, 0, mmRLC_GPU_CLOCK_COUNT_MSB) << 32ULL);
3384b843c749SSergey Zigachev 	mutex_unlock(&adev->gfx.gpu_clock_mutex);
3385b843c749SSergey Zigachev 	return clock;
3386b843c749SSergey Zigachev }
3387b843c749SSergey Zigachev 
gfx_v9_0_ring_emit_gds_switch(struct amdgpu_ring * ring,uint32_t vmid,uint32_t gds_base,uint32_t gds_size,uint32_t gws_base,uint32_t gws_size,uint32_t oa_base,uint32_t oa_size)3388b843c749SSergey Zigachev static void gfx_v9_0_ring_emit_gds_switch(struct amdgpu_ring *ring,
3389b843c749SSergey Zigachev 					  uint32_t vmid,
3390b843c749SSergey Zigachev 					  uint32_t gds_base, uint32_t gds_size,
3391b843c749SSergey Zigachev 					  uint32_t gws_base, uint32_t gws_size,
3392b843c749SSergey Zigachev 					  uint32_t oa_base, uint32_t oa_size)
3393b843c749SSergey Zigachev {
3394b843c749SSergey Zigachev 	struct amdgpu_device *adev = ring->adev;
3395b843c749SSergey Zigachev 
3396b843c749SSergey Zigachev 	gds_base = gds_base >> AMDGPU_GDS_SHIFT;
3397b843c749SSergey Zigachev 	gds_size = gds_size >> AMDGPU_GDS_SHIFT;
3398b843c749SSergey Zigachev 
3399b843c749SSergey Zigachev 	gws_base = gws_base >> AMDGPU_GWS_SHIFT;
3400b843c749SSergey Zigachev 	gws_size = gws_size >> AMDGPU_GWS_SHIFT;
3401b843c749SSergey Zigachev 
3402b843c749SSergey Zigachev 	oa_base = oa_base >> AMDGPU_OA_SHIFT;
3403b843c749SSergey Zigachev 	oa_size = oa_size >> AMDGPU_OA_SHIFT;
3404b843c749SSergey Zigachev 
3405b843c749SSergey Zigachev 	/* GDS Base */
3406b843c749SSergey Zigachev 	gfx_v9_0_write_data_to_reg(ring, 0, false,
3407b843c749SSergey Zigachev 				   SOC15_REG_OFFSET(GC, 0, mmGDS_VMID0_BASE) + 2 * vmid,
3408b843c749SSergey Zigachev 				   gds_base);
3409b843c749SSergey Zigachev 
3410b843c749SSergey Zigachev 	/* GDS Size */
3411b843c749SSergey Zigachev 	gfx_v9_0_write_data_to_reg(ring, 0, false,
3412b843c749SSergey Zigachev 				   SOC15_REG_OFFSET(GC, 0, mmGDS_VMID0_SIZE) + 2 * vmid,
3413b843c749SSergey Zigachev 				   gds_size);
3414b843c749SSergey Zigachev 
3415b843c749SSergey Zigachev 	/* GWS */
3416b843c749SSergey Zigachev 	gfx_v9_0_write_data_to_reg(ring, 0, false,
3417b843c749SSergey Zigachev 				   SOC15_REG_OFFSET(GC, 0, mmGDS_GWS_VMID0) + vmid,
3418b843c749SSergey Zigachev 				   gws_size << GDS_GWS_VMID0__SIZE__SHIFT | gws_base);
3419b843c749SSergey Zigachev 
3420b843c749SSergey Zigachev 	/* OA */
3421b843c749SSergey Zigachev 	gfx_v9_0_write_data_to_reg(ring, 0, false,
3422b843c749SSergey Zigachev 				   SOC15_REG_OFFSET(GC, 0, mmGDS_OA_VMID0) + vmid,
3423b843c749SSergey Zigachev 				   (1 << (oa_size + oa_base)) - (1 << oa_base));
3424b843c749SSergey Zigachev }
3425b843c749SSergey Zigachev 
gfx_v9_0_early_init(void * handle)3426b843c749SSergey Zigachev static int gfx_v9_0_early_init(void *handle)
3427b843c749SSergey Zigachev {
3428b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3429b843c749SSergey Zigachev 
3430b843c749SSergey Zigachev 	adev->gfx.num_gfx_rings = GFX9_NUM_GFX_RINGS;
3431b843c749SSergey Zigachev 	adev->gfx.num_compute_rings = AMDGPU_MAX_COMPUTE_RINGS;
3432b843c749SSergey Zigachev 	gfx_v9_0_set_ring_funcs(adev);
3433b843c749SSergey Zigachev 	gfx_v9_0_set_irq_funcs(adev);
3434b843c749SSergey Zigachev 	gfx_v9_0_set_gds_init(adev);
3435b843c749SSergey Zigachev 	gfx_v9_0_set_rlc_funcs(adev);
3436b843c749SSergey Zigachev 
3437b843c749SSergey Zigachev 	return 0;
3438b843c749SSergey Zigachev }
3439b843c749SSergey Zigachev 
gfx_v9_0_late_init(void * handle)3440b843c749SSergey Zigachev static int gfx_v9_0_late_init(void *handle)
3441b843c749SSergey Zigachev {
3442b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3443b843c749SSergey Zigachev 	int r;
3444b843c749SSergey Zigachev 
3445b843c749SSergey Zigachev 	r = amdgpu_irq_get(adev, &adev->gfx.priv_reg_irq, 0);
3446b843c749SSergey Zigachev 	if (r)
3447b843c749SSergey Zigachev 		return r;
3448b843c749SSergey Zigachev 
3449b843c749SSergey Zigachev 	r = amdgpu_irq_get(adev, &adev->gfx.priv_inst_irq, 0);
3450b843c749SSergey Zigachev 	if (r)
3451b843c749SSergey Zigachev 		return r;
3452b843c749SSergey Zigachev 
3453b843c749SSergey Zigachev 	return 0;
3454b843c749SSergey Zigachev }
3455b843c749SSergey Zigachev 
gfx_v9_0_enter_rlc_safe_mode(struct amdgpu_device * adev)3456b843c749SSergey Zigachev static void gfx_v9_0_enter_rlc_safe_mode(struct amdgpu_device *adev)
3457b843c749SSergey Zigachev {
3458b843c749SSergey Zigachev 	uint32_t rlc_setting, data;
3459b843c749SSergey Zigachev 	unsigned i;
3460b843c749SSergey Zigachev 
3461b843c749SSergey Zigachev 	if (adev->gfx.rlc.in_safe_mode)
3462b843c749SSergey Zigachev 		return;
3463b843c749SSergey Zigachev 
3464b843c749SSergey Zigachev 	/* if RLC is not enabled, do nothing */
3465b843c749SSergey Zigachev 	rlc_setting = RREG32_SOC15(GC, 0, mmRLC_CNTL);
3466b843c749SSergey Zigachev 	if (!(rlc_setting & RLC_CNTL__RLC_ENABLE_F32_MASK))
3467b843c749SSergey Zigachev 		return;
3468b843c749SSergey Zigachev 
3469b843c749SSergey Zigachev 	if (adev->cg_flags &
3470b843c749SSergey Zigachev 	    (AMD_CG_SUPPORT_GFX_CGCG | AMD_CG_SUPPORT_GFX_MGCG |
3471b843c749SSergey Zigachev 	     AMD_CG_SUPPORT_GFX_3D_CGCG)) {
3472b843c749SSergey Zigachev 		data = RLC_SAFE_MODE__CMD_MASK;
3473b843c749SSergey Zigachev 		data |= (1 << RLC_SAFE_MODE__MESSAGE__SHIFT);
3474b843c749SSergey Zigachev 		WREG32_SOC15(GC, 0, mmRLC_SAFE_MODE, data);
3475b843c749SSergey Zigachev 
3476b843c749SSergey Zigachev 		/* wait for RLC_SAFE_MODE */
3477b843c749SSergey Zigachev 		for (i = 0; i < adev->usec_timeout; i++) {
3478b843c749SSergey Zigachev 			if (!REG_GET_FIELD(RREG32_SOC15(GC, 0, mmRLC_SAFE_MODE), RLC_SAFE_MODE, CMD))
3479b843c749SSergey Zigachev 				break;
3480b843c749SSergey Zigachev 			udelay(1);
3481b843c749SSergey Zigachev 		}
3482b843c749SSergey Zigachev 		adev->gfx.rlc.in_safe_mode = true;
3483b843c749SSergey Zigachev 	}
3484b843c749SSergey Zigachev }
3485b843c749SSergey Zigachev 
gfx_v9_0_exit_rlc_safe_mode(struct amdgpu_device * adev)3486b843c749SSergey Zigachev static void gfx_v9_0_exit_rlc_safe_mode(struct amdgpu_device *adev)
3487b843c749SSergey Zigachev {
3488b843c749SSergey Zigachev 	uint32_t rlc_setting, data;
3489b843c749SSergey Zigachev 
3490b843c749SSergey Zigachev 	if (!adev->gfx.rlc.in_safe_mode)
3491b843c749SSergey Zigachev 		return;
3492b843c749SSergey Zigachev 
3493b843c749SSergey Zigachev 	/* if RLC is not enabled, do nothing */
3494b843c749SSergey Zigachev 	rlc_setting = RREG32_SOC15(GC, 0, mmRLC_CNTL);
3495b843c749SSergey Zigachev 	if (!(rlc_setting & RLC_CNTL__RLC_ENABLE_F32_MASK))
3496b843c749SSergey Zigachev 		return;
3497b843c749SSergey Zigachev 
3498b843c749SSergey Zigachev 	if (adev->cg_flags &
3499b843c749SSergey Zigachev 	    (AMD_CG_SUPPORT_GFX_CGCG | AMD_CG_SUPPORT_GFX_MGCG)) {
3500b843c749SSergey Zigachev 		/*
3501b843c749SSergey Zigachev 		 * Try to exit safe mode only if it is already in safe
3502b843c749SSergey Zigachev 		 * mode.
3503b843c749SSergey Zigachev 		 */
3504b843c749SSergey Zigachev 		data = RLC_SAFE_MODE__CMD_MASK;
3505b843c749SSergey Zigachev 		WREG32_SOC15(GC, 0, mmRLC_SAFE_MODE, data);
3506b843c749SSergey Zigachev 		adev->gfx.rlc.in_safe_mode = false;
3507b843c749SSergey Zigachev 	}
3508b843c749SSergey Zigachev }
3509b843c749SSergey Zigachev 
gfx_v9_0_update_gfx_cg_power_gating(struct amdgpu_device * adev,bool enable)3510b843c749SSergey Zigachev static void gfx_v9_0_update_gfx_cg_power_gating(struct amdgpu_device *adev,
3511b843c749SSergey Zigachev 						bool enable)
3512b843c749SSergey Zigachev {
3513b843c749SSergey Zigachev 	gfx_v9_0_enter_rlc_safe_mode(adev);
3514b843c749SSergey Zigachev 
3515b843c749SSergey Zigachev 	if ((adev->pg_flags & AMD_PG_SUPPORT_GFX_PG) && enable) {
3516b843c749SSergey Zigachev 		gfx_v9_0_enable_gfx_cg_power_gating(adev, true);
3517b843c749SSergey Zigachev 		if (adev->pg_flags & AMD_PG_SUPPORT_GFX_PIPELINE)
3518b843c749SSergey Zigachev 			gfx_v9_0_enable_gfx_pipeline_powergating(adev, true);
3519b843c749SSergey Zigachev 	} else {
3520b843c749SSergey Zigachev 		gfx_v9_0_enable_gfx_cg_power_gating(adev, false);
3521b843c749SSergey Zigachev 		gfx_v9_0_enable_gfx_pipeline_powergating(adev, false);
3522b843c749SSergey Zigachev 	}
3523b843c749SSergey Zigachev 
3524b843c749SSergey Zigachev 	gfx_v9_0_exit_rlc_safe_mode(adev);
3525b843c749SSergey Zigachev }
3526b843c749SSergey Zigachev 
gfx_v9_0_update_gfx_mg_power_gating(struct amdgpu_device * adev,bool enable)3527b843c749SSergey Zigachev static void gfx_v9_0_update_gfx_mg_power_gating(struct amdgpu_device *adev,
3528b843c749SSergey Zigachev 						bool enable)
3529b843c749SSergey Zigachev {
3530b843c749SSergey Zigachev 	/* TODO: double check if we need to perform under safe mode */
3531b843c749SSergey Zigachev 	/* gfx_v9_0_enter_rlc_safe_mode(adev); */
3532b843c749SSergey Zigachev 
3533b843c749SSergey Zigachev 	if ((adev->pg_flags & AMD_PG_SUPPORT_GFX_SMG) && enable)
3534b843c749SSergey Zigachev 		gfx_v9_0_enable_gfx_static_mg_power_gating(adev, true);
3535b843c749SSergey Zigachev 	else
3536b843c749SSergey Zigachev 		gfx_v9_0_enable_gfx_static_mg_power_gating(adev, false);
3537b843c749SSergey Zigachev 
3538b843c749SSergey Zigachev 	if ((adev->pg_flags & AMD_PG_SUPPORT_GFX_DMG) && enable)
3539b843c749SSergey Zigachev 		gfx_v9_0_enable_gfx_dynamic_mg_power_gating(adev, true);
3540b843c749SSergey Zigachev 	else
3541b843c749SSergey Zigachev 		gfx_v9_0_enable_gfx_dynamic_mg_power_gating(adev, false);
3542b843c749SSergey Zigachev 
3543b843c749SSergey Zigachev 	/* gfx_v9_0_exit_rlc_safe_mode(adev); */
3544b843c749SSergey Zigachev }
3545b843c749SSergey Zigachev 
gfx_v9_0_update_medium_grain_clock_gating(struct amdgpu_device * adev,bool enable)3546b843c749SSergey Zigachev static void gfx_v9_0_update_medium_grain_clock_gating(struct amdgpu_device *adev,
3547b843c749SSergey Zigachev 						      bool enable)
3548b843c749SSergey Zigachev {
3549b843c749SSergey Zigachev 	uint32_t data, def;
3550b843c749SSergey Zigachev 
3551b843c749SSergey Zigachev 	/* It is disabled by HW by default */
3552b843c749SSergey Zigachev 	if (enable && (adev->cg_flags & AMD_CG_SUPPORT_GFX_MGCG)) {
3553b843c749SSergey Zigachev 		/* 1 - RLC_CGTT_MGCG_OVERRIDE */
3554b843c749SSergey Zigachev 		def = data = RREG32_SOC15(GC, 0, mmRLC_CGTT_MGCG_OVERRIDE);
3555b843c749SSergey Zigachev 
3556b843c749SSergey Zigachev 		if (adev->asic_type != CHIP_VEGA12)
3557b843c749SSergey Zigachev 			data &= ~RLC_CGTT_MGCG_OVERRIDE__CPF_CGTT_SCLK_OVERRIDE_MASK;
3558b843c749SSergey Zigachev 
3559b843c749SSergey Zigachev 		data &= ~(RLC_CGTT_MGCG_OVERRIDE__GRBM_CGTT_SCLK_OVERRIDE_MASK |
3560b843c749SSergey Zigachev 			  RLC_CGTT_MGCG_OVERRIDE__GFXIP_MGCG_OVERRIDE_MASK |
3561b843c749SSergey Zigachev 			  RLC_CGTT_MGCG_OVERRIDE__GFXIP_MGLS_OVERRIDE_MASK);
3562b843c749SSergey Zigachev 
3563b843c749SSergey Zigachev 		/* only for Vega10 & Raven1 */
3564b843c749SSergey Zigachev 		data |= RLC_CGTT_MGCG_OVERRIDE__RLC_CGTT_SCLK_OVERRIDE_MASK;
3565b843c749SSergey Zigachev 
3566b843c749SSergey Zigachev 		if (def != data)
3567b843c749SSergey Zigachev 			WREG32_SOC15(GC, 0, mmRLC_CGTT_MGCG_OVERRIDE, data);
3568b843c749SSergey Zigachev 
3569b843c749SSergey Zigachev 		/* MGLS is a global flag to control all MGLS in GFX */
3570b843c749SSergey Zigachev 		if (adev->cg_flags & AMD_CG_SUPPORT_GFX_MGLS) {
3571b843c749SSergey Zigachev 			/* 2 - RLC memory Light sleep */
3572b843c749SSergey Zigachev 			if (adev->cg_flags & AMD_CG_SUPPORT_GFX_RLC_LS) {
3573b843c749SSergey Zigachev 				def = data = RREG32_SOC15(GC, 0, mmRLC_MEM_SLP_CNTL);
3574b843c749SSergey Zigachev 				data |= RLC_MEM_SLP_CNTL__RLC_MEM_LS_EN_MASK;
3575b843c749SSergey Zigachev 				if (def != data)
3576b843c749SSergey Zigachev 					WREG32_SOC15(GC, 0, mmRLC_MEM_SLP_CNTL, data);
3577b843c749SSergey Zigachev 			}
3578b843c749SSergey Zigachev 			/* 3 - CP memory Light sleep */
3579b843c749SSergey Zigachev 			if (adev->cg_flags & AMD_CG_SUPPORT_GFX_CP_LS) {
3580b843c749SSergey Zigachev 				def = data = RREG32_SOC15(GC, 0, mmCP_MEM_SLP_CNTL);
3581b843c749SSergey Zigachev 				data |= CP_MEM_SLP_CNTL__CP_MEM_LS_EN_MASK;
3582b843c749SSergey Zigachev 				if (def != data)
3583b843c749SSergey Zigachev 					WREG32_SOC15(GC, 0, mmCP_MEM_SLP_CNTL, data);
3584b843c749SSergey Zigachev 			}
3585b843c749SSergey Zigachev 		}
3586b843c749SSergey Zigachev 	} else {
3587b843c749SSergey Zigachev 		/* 1 - MGCG_OVERRIDE */
3588b843c749SSergey Zigachev 		def = data = RREG32_SOC15(GC, 0, mmRLC_CGTT_MGCG_OVERRIDE);
3589b843c749SSergey Zigachev 
3590b843c749SSergey Zigachev 		if (adev->asic_type != CHIP_VEGA12)
3591b843c749SSergey Zigachev 			data |= RLC_CGTT_MGCG_OVERRIDE__CPF_CGTT_SCLK_OVERRIDE_MASK;
3592b843c749SSergey Zigachev 
3593b843c749SSergey Zigachev 		data |= (RLC_CGTT_MGCG_OVERRIDE__RLC_CGTT_SCLK_OVERRIDE_MASK |
3594b843c749SSergey Zigachev 			 RLC_CGTT_MGCG_OVERRIDE__GRBM_CGTT_SCLK_OVERRIDE_MASK |
3595b843c749SSergey Zigachev 			 RLC_CGTT_MGCG_OVERRIDE__GFXIP_MGCG_OVERRIDE_MASK |
3596b843c749SSergey Zigachev 			 RLC_CGTT_MGCG_OVERRIDE__GFXIP_MGLS_OVERRIDE_MASK);
3597b843c749SSergey Zigachev 
3598b843c749SSergey Zigachev 		if (def != data)
3599b843c749SSergey Zigachev 			WREG32_SOC15(GC, 0, mmRLC_CGTT_MGCG_OVERRIDE, data);
3600b843c749SSergey Zigachev 
3601b843c749SSergey Zigachev 		/* 2 - disable MGLS in RLC */
3602b843c749SSergey Zigachev 		data = RREG32_SOC15(GC, 0, mmRLC_MEM_SLP_CNTL);
3603b843c749SSergey Zigachev 		if (data & RLC_MEM_SLP_CNTL__RLC_MEM_LS_EN_MASK) {
3604b843c749SSergey Zigachev 			data &= ~RLC_MEM_SLP_CNTL__RLC_MEM_LS_EN_MASK;
3605b843c749SSergey Zigachev 			WREG32_SOC15(GC, 0, mmRLC_MEM_SLP_CNTL, data);
3606b843c749SSergey Zigachev 		}
3607b843c749SSergey Zigachev 
3608b843c749SSergey Zigachev 		/* 3 - disable MGLS in CP */
3609b843c749SSergey Zigachev 		data = RREG32_SOC15(GC, 0, mmCP_MEM_SLP_CNTL);
3610b843c749SSergey Zigachev 		if (data & CP_MEM_SLP_CNTL__CP_MEM_LS_EN_MASK) {
3611b843c749SSergey Zigachev 			data &= ~CP_MEM_SLP_CNTL__CP_MEM_LS_EN_MASK;
3612b843c749SSergey Zigachev 			WREG32_SOC15(GC, 0, mmCP_MEM_SLP_CNTL, data);
3613b843c749SSergey Zigachev 		}
3614b843c749SSergey Zigachev 	}
3615b843c749SSergey Zigachev }
3616b843c749SSergey Zigachev 
gfx_v9_0_update_3d_clock_gating(struct amdgpu_device * adev,bool enable)3617b843c749SSergey Zigachev static void gfx_v9_0_update_3d_clock_gating(struct amdgpu_device *adev,
3618b843c749SSergey Zigachev 					   bool enable)
3619b843c749SSergey Zigachev {
3620b843c749SSergey Zigachev 	uint32_t data, def;
3621b843c749SSergey Zigachev 
3622b843c749SSergey Zigachev 	adev->gfx.rlc.funcs->enter_safe_mode(adev);
3623b843c749SSergey Zigachev 
3624b843c749SSergey Zigachev 	/* Enable 3D CGCG/CGLS */
3625b843c749SSergey Zigachev 	if (enable && (adev->cg_flags & AMD_CG_SUPPORT_GFX_3D_CGCG)) {
3626b843c749SSergey Zigachev 		/* write cmd to clear cgcg/cgls ov */
3627b843c749SSergey Zigachev 		def = data = RREG32_SOC15(GC, 0, mmRLC_CGTT_MGCG_OVERRIDE);
3628b843c749SSergey Zigachev 		/* unset CGCG override */
3629b843c749SSergey Zigachev 		data &= ~RLC_CGTT_MGCG_OVERRIDE__GFXIP_GFX3D_CG_OVERRIDE_MASK;
3630b843c749SSergey Zigachev 		/* update CGCG and CGLS override bits */
3631b843c749SSergey Zigachev 		if (def != data)
3632b843c749SSergey Zigachev 			WREG32_SOC15(GC, 0, mmRLC_CGTT_MGCG_OVERRIDE, data);
3633b843c749SSergey Zigachev 
3634b843c749SSergey Zigachev 		/* enable 3Dcgcg FSM(0x0000363f) */
3635b843c749SSergey Zigachev 		def = RREG32_SOC15(GC, 0, mmRLC_CGCG_CGLS_CTRL_3D);
3636b843c749SSergey Zigachev 
3637b843c749SSergey Zigachev 		data = (0x36 << RLC_CGCG_CGLS_CTRL_3D__CGCG_GFX_IDLE_THRESHOLD__SHIFT) |
3638b843c749SSergey Zigachev 			RLC_CGCG_CGLS_CTRL_3D__CGCG_EN_MASK;
3639b843c749SSergey Zigachev 		if (adev->cg_flags & AMD_CG_SUPPORT_GFX_3D_CGLS)
3640b843c749SSergey Zigachev 			data |= (0x000F << RLC_CGCG_CGLS_CTRL_3D__CGLS_REP_COMPANSAT_DELAY__SHIFT) |
3641b843c749SSergey Zigachev 				RLC_CGCG_CGLS_CTRL_3D__CGLS_EN_MASK;
3642b843c749SSergey Zigachev 		if (def != data)
3643b843c749SSergey Zigachev 			WREG32_SOC15(GC, 0, mmRLC_CGCG_CGLS_CTRL_3D, data);
3644b843c749SSergey Zigachev 
3645b843c749SSergey Zigachev 		/* set IDLE_POLL_COUNT(0x00900100) */
3646b843c749SSergey Zigachev 		def = RREG32_SOC15(GC, 0, mmCP_RB_WPTR_POLL_CNTL);
3647b843c749SSergey Zigachev 		data = (0x0100 << CP_RB_WPTR_POLL_CNTL__POLL_FREQUENCY__SHIFT) |
3648b843c749SSergey Zigachev 			(0x0090 << CP_RB_WPTR_POLL_CNTL__IDLE_POLL_COUNT__SHIFT);
3649b843c749SSergey Zigachev 		if (def != data)
3650b843c749SSergey Zigachev 			WREG32_SOC15(GC, 0, mmCP_RB_WPTR_POLL_CNTL, data);
3651b843c749SSergey Zigachev 	} else {
3652b843c749SSergey Zigachev 		/* Disable CGCG/CGLS */
3653b843c749SSergey Zigachev 		def = data = RREG32_SOC15(GC, 0, mmRLC_CGCG_CGLS_CTRL_3D);
3654b843c749SSergey Zigachev 		/* disable cgcg, cgls should be disabled */
3655b843c749SSergey Zigachev 		data &= ~(RLC_CGCG_CGLS_CTRL_3D__CGCG_EN_MASK |
3656b843c749SSergey Zigachev 			  RLC_CGCG_CGLS_CTRL_3D__CGLS_EN_MASK);
3657b843c749SSergey Zigachev 		/* disable cgcg and cgls in FSM */
3658b843c749SSergey Zigachev 		if (def != data)
3659b843c749SSergey Zigachev 			WREG32_SOC15(GC, 0, mmRLC_CGCG_CGLS_CTRL_3D, data);
3660b843c749SSergey Zigachev 	}
3661b843c749SSergey Zigachev 
3662b843c749SSergey Zigachev 	adev->gfx.rlc.funcs->exit_safe_mode(adev);
3663b843c749SSergey Zigachev }
3664b843c749SSergey Zigachev 
gfx_v9_0_update_coarse_grain_clock_gating(struct amdgpu_device * adev,bool enable)3665b843c749SSergey Zigachev static void gfx_v9_0_update_coarse_grain_clock_gating(struct amdgpu_device *adev,
3666b843c749SSergey Zigachev 						      bool enable)
3667b843c749SSergey Zigachev {
3668b843c749SSergey Zigachev 	uint32_t def, data;
3669b843c749SSergey Zigachev 
3670b843c749SSergey Zigachev 	adev->gfx.rlc.funcs->enter_safe_mode(adev);
3671b843c749SSergey Zigachev 
3672b843c749SSergey Zigachev 	if (enable && (adev->cg_flags & AMD_CG_SUPPORT_GFX_CGCG)) {
3673b843c749SSergey Zigachev 		def = data = RREG32_SOC15(GC, 0, mmRLC_CGTT_MGCG_OVERRIDE);
3674b843c749SSergey Zigachev 		/* unset CGCG override */
3675b843c749SSergey Zigachev 		data &= ~RLC_CGTT_MGCG_OVERRIDE__GFXIP_CGCG_OVERRIDE_MASK;
3676b843c749SSergey Zigachev 		if (adev->cg_flags & AMD_CG_SUPPORT_GFX_CGLS)
3677b843c749SSergey Zigachev 			data &= ~RLC_CGTT_MGCG_OVERRIDE__GFXIP_CGLS_OVERRIDE_MASK;
3678b843c749SSergey Zigachev 		else
3679b843c749SSergey Zigachev 			data |= RLC_CGTT_MGCG_OVERRIDE__GFXIP_CGLS_OVERRIDE_MASK;
3680b843c749SSergey Zigachev 		/* update CGCG and CGLS override bits */
3681b843c749SSergey Zigachev 		if (def != data)
3682b843c749SSergey Zigachev 			WREG32_SOC15(GC, 0, mmRLC_CGTT_MGCG_OVERRIDE, data);
3683b843c749SSergey Zigachev 
3684b843c749SSergey Zigachev 		/* enable cgcg FSM(0x0000363F) */
3685b843c749SSergey Zigachev 		def = RREG32_SOC15(GC, 0, mmRLC_CGCG_CGLS_CTRL);
3686b843c749SSergey Zigachev 
3687b843c749SSergey Zigachev 		data = (0x36 << RLC_CGCG_CGLS_CTRL__CGCG_GFX_IDLE_THRESHOLD__SHIFT) |
3688b843c749SSergey Zigachev 			RLC_CGCG_CGLS_CTRL__CGCG_EN_MASK;
3689b843c749SSergey Zigachev 		if (adev->cg_flags & AMD_CG_SUPPORT_GFX_CGLS)
3690b843c749SSergey Zigachev 			data |= (0x000F << RLC_CGCG_CGLS_CTRL__CGLS_REP_COMPANSAT_DELAY__SHIFT) |
3691b843c749SSergey Zigachev 				RLC_CGCG_CGLS_CTRL__CGLS_EN_MASK;
3692b843c749SSergey Zigachev 		if (def != data)
3693b843c749SSergey Zigachev 			WREG32_SOC15(GC, 0, mmRLC_CGCG_CGLS_CTRL, data);
3694b843c749SSergey Zigachev 
3695b843c749SSergey Zigachev 		/* set IDLE_POLL_COUNT(0x00900100) */
3696b843c749SSergey Zigachev 		def = RREG32_SOC15(GC, 0, mmCP_RB_WPTR_POLL_CNTL);
3697b843c749SSergey Zigachev 		data = (0x0100 << CP_RB_WPTR_POLL_CNTL__POLL_FREQUENCY__SHIFT) |
3698b843c749SSergey Zigachev 			(0x0090 << CP_RB_WPTR_POLL_CNTL__IDLE_POLL_COUNT__SHIFT);
3699b843c749SSergey Zigachev 		if (def != data)
3700b843c749SSergey Zigachev 			WREG32_SOC15(GC, 0, mmCP_RB_WPTR_POLL_CNTL, data);
3701b843c749SSergey Zigachev 	} else {
3702b843c749SSergey Zigachev 		def = data = RREG32_SOC15(GC, 0, mmRLC_CGCG_CGLS_CTRL);
3703b843c749SSergey Zigachev 		/* reset CGCG/CGLS bits */
3704b843c749SSergey Zigachev 		data &= ~(RLC_CGCG_CGLS_CTRL__CGCG_EN_MASK | RLC_CGCG_CGLS_CTRL__CGLS_EN_MASK);
3705b843c749SSergey Zigachev 		/* disable cgcg and cgls in FSM */
3706b843c749SSergey Zigachev 		if (def != data)
3707b843c749SSergey Zigachev 			WREG32_SOC15(GC, 0, mmRLC_CGCG_CGLS_CTRL, data);
3708b843c749SSergey Zigachev 	}
3709b843c749SSergey Zigachev 
3710b843c749SSergey Zigachev 	adev->gfx.rlc.funcs->exit_safe_mode(adev);
3711b843c749SSergey Zigachev }
3712b843c749SSergey Zigachev 
gfx_v9_0_update_gfx_clock_gating(struct amdgpu_device * adev,bool enable)3713b843c749SSergey Zigachev static int gfx_v9_0_update_gfx_clock_gating(struct amdgpu_device *adev,
3714b843c749SSergey Zigachev 					    bool enable)
3715b843c749SSergey Zigachev {
3716b843c749SSergey Zigachev 	if (enable) {
3717b843c749SSergey Zigachev 		/* CGCG/CGLS should be enabled after MGCG/MGLS
3718b843c749SSergey Zigachev 		 * ===  MGCG + MGLS ===
3719b843c749SSergey Zigachev 		 */
3720b843c749SSergey Zigachev 		gfx_v9_0_update_medium_grain_clock_gating(adev, enable);
3721b843c749SSergey Zigachev 		/* ===  CGCG /CGLS for GFX 3D Only === */
3722b843c749SSergey Zigachev 		gfx_v9_0_update_3d_clock_gating(adev, enable);
3723b843c749SSergey Zigachev 		/* ===  CGCG + CGLS === */
3724b843c749SSergey Zigachev 		gfx_v9_0_update_coarse_grain_clock_gating(adev, enable);
3725b843c749SSergey Zigachev 	} else {
3726b843c749SSergey Zigachev 		/* CGCG/CGLS should be disabled before MGCG/MGLS
3727b843c749SSergey Zigachev 		 * ===  CGCG + CGLS ===
3728b843c749SSergey Zigachev 		 */
3729b843c749SSergey Zigachev 		gfx_v9_0_update_coarse_grain_clock_gating(adev, enable);
3730b843c749SSergey Zigachev 		/* ===  CGCG /CGLS for GFX 3D Only === */
3731b843c749SSergey Zigachev 		gfx_v9_0_update_3d_clock_gating(adev, enable);
3732b843c749SSergey Zigachev 		/* ===  MGCG + MGLS === */
3733b843c749SSergey Zigachev 		gfx_v9_0_update_medium_grain_clock_gating(adev, enable);
3734b843c749SSergey Zigachev 	}
3735b843c749SSergey Zigachev 	return 0;
3736b843c749SSergey Zigachev }
3737b843c749SSergey Zigachev 
3738b843c749SSergey Zigachev static const struct amdgpu_rlc_funcs gfx_v9_0_rlc_funcs = {
3739b843c749SSergey Zigachev 	.enter_safe_mode = gfx_v9_0_enter_rlc_safe_mode,
3740b843c749SSergey Zigachev 	.exit_safe_mode = gfx_v9_0_exit_rlc_safe_mode
3741b843c749SSergey Zigachev };
3742b843c749SSergey Zigachev 
gfx_v9_0_set_powergating_state(void * handle,enum amd_powergating_state state)3743b843c749SSergey Zigachev static int gfx_v9_0_set_powergating_state(void *handle,
3744b843c749SSergey Zigachev 					  enum amd_powergating_state state)
3745b843c749SSergey Zigachev {
3746b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3747b843c749SSergey Zigachev 	bool enable = (state == AMD_PG_STATE_GATE) ? true : false;
3748b843c749SSergey Zigachev 
3749b843c749SSergey Zigachev 	switch (adev->asic_type) {
3750b843c749SSergey Zigachev 	case CHIP_RAVEN:
3751b843c749SSergey Zigachev 		if (adev->pg_flags & AMD_PG_SUPPORT_RLC_SMU_HS) {
3752b843c749SSergey Zigachev 			gfx_v9_0_enable_sck_slow_down_on_power_up(adev, true);
3753b843c749SSergey Zigachev 			gfx_v9_0_enable_sck_slow_down_on_power_down(adev, true);
3754b843c749SSergey Zigachev 		} else {
3755b843c749SSergey Zigachev 			gfx_v9_0_enable_sck_slow_down_on_power_up(adev, false);
3756b843c749SSergey Zigachev 			gfx_v9_0_enable_sck_slow_down_on_power_down(adev, false);
3757b843c749SSergey Zigachev 		}
3758b843c749SSergey Zigachev 
3759b843c749SSergey Zigachev 		if (adev->pg_flags & AMD_PG_SUPPORT_CP)
3760b843c749SSergey Zigachev 			gfx_v9_0_enable_cp_power_gating(adev, true);
3761b843c749SSergey Zigachev 		else
3762b843c749SSergey Zigachev 			gfx_v9_0_enable_cp_power_gating(adev, false);
3763b843c749SSergey Zigachev 
3764b843c749SSergey Zigachev 		/* update gfx cgpg state */
3765b843c749SSergey Zigachev 		gfx_v9_0_update_gfx_cg_power_gating(adev, enable);
3766b843c749SSergey Zigachev 
3767b843c749SSergey Zigachev 		/* update mgcg state */
3768b843c749SSergey Zigachev 		gfx_v9_0_update_gfx_mg_power_gating(adev, enable);
3769b843c749SSergey Zigachev 
3770b843c749SSergey Zigachev 		/* set gfx off through smu */
3771b843c749SSergey Zigachev 		if (enable && adev->powerplay.pp_funcs->set_powergating_by_smu)
3772b843c749SSergey Zigachev 			amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, true);
3773b843c749SSergey Zigachev 		break;
3774b843c749SSergey Zigachev 	case CHIP_VEGA12:
3775b843c749SSergey Zigachev 		/* set gfx off through smu */
3776b843c749SSergey Zigachev 		if (enable && adev->powerplay.pp_funcs->set_powergating_by_smu)
3777b843c749SSergey Zigachev 			amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, true);
3778b843c749SSergey Zigachev 		break;
3779b843c749SSergey Zigachev 	default:
3780b843c749SSergey Zigachev 		break;
3781b843c749SSergey Zigachev 	}
3782b843c749SSergey Zigachev 
3783b843c749SSergey Zigachev 	return 0;
3784b843c749SSergey Zigachev }
3785b843c749SSergey Zigachev 
gfx_v9_0_set_clockgating_state(void * handle,enum amd_clockgating_state state)3786b843c749SSergey Zigachev static int gfx_v9_0_set_clockgating_state(void *handle,
3787b843c749SSergey Zigachev 					  enum amd_clockgating_state state)
3788b843c749SSergey Zigachev {
3789b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3790b843c749SSergey Zigachev 
3791b843c749SSergey Zigachev 	if (amdgpu_sriov_vf(adev))
3792b843c749SSergey Zigachev 		return 0;
3793b843c749SSergey Zigachev 
3794b843c749SSergey Zigachev 	switch (adev->asic_type) {
3795b843c749SSergey Zigachev 	case CHIP_VEGA10:
3796b843c749SSergey Zigachev 	case CHIP_VEGA12:
3797b843c749SSergey Zigachev 	case CHIP_VEGA20:
3798b843c749SSergey Zigachev 	case CHIP_RAVEN:
3799b843c749SSergey Zigachev 		gfx_v9_0_update_gfx_clock_gating(adev,
3800b843c749SSergey Zigachev 						 state == AMD_CG_STATE_GATE ? true : false);
3801b843c749SSergey Zigachev 		break;
3802b843c749SSergey Zigachev 	default:
3803b843c749SSergey Zigachev 		break;
3804b843c749SSergey Zigachev 	}
3805b843c749SSergey Zigachev 	return 0;
3806b843c749SSergey Zigachev }
3807b843c749SSergey Zigachev 
gfx_v9_0_get_clockgating_state(void * handle,u32 * flags)3808b843c749SSergey Zigachev static void gfx_v9_0_get_clockgating_state(void *handle, u32 *flags)
3809b843c749SSergey Zigachev {
3810b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3811b843c749SSergey Zigachev 	int data;
3812b843c749SSergey Zigachev 
3813b843c749SSergey Zigachev 	if (amdgpu_sriov_vf(adev))
3814b843c749SSergey Zigachev 		*flags = 0;
3815b843c749SSergey Zigachev 
3816b843c749SSergey Zigachev 	/* AMD_CG_SUPPORT_GFX_MGCG */
3817b843c749SSergey Zigachev 	data = RREG32_SOC15(GC, 0, mmRLC_CGTT_MGCG_OVERRIDE);
3818b843c749SSergey Zigachev 	if (!(data & RLC_CGTT_MGCG_OVERRIDE__GFXIP_MGCG_OVERRIDE_MASK))
3819b843c749SSergey Zigachev 		*flags |= AMD_CG_SUPPORT_GFX_MGCG;
3820b843c749SSergey Zigachev 
3821b843c749SSergey Zigachev 	/* AMD_CG_SUPPORT_GFX_CGCG */
3822b843c749SSergey Zigachev 	data = RREG32_SOC15(GC, 0, mmRLC_CGCG_CGLS_CTRL);
3823b843c749SSergey Zigachev 	if (data & RLC_CGCG_CGLS_CTRL__CGCG_EN_MASK)
3824b843c749SSergey Zigachev 		*flags |= AMD_CG_SUPPORT_GFX_CGCG;
3825b843c749SSergey Zigachev 
3826b843c749SSergey Zigachev 	/* AMD_CG_SUPPORT_GFX_CGLS */
3827b843c749SSergey Zigachev 	if (data & RLC_CGCG_CGLS_CTRL__CGLS_EN_MASK)
3828b843c749SSergey Zigachev 		*flags |= AMD_CG_SUPPORT_GFX_CGLS;
3829b843c749SSergey Zigachev 
3830b843c749SSergey Zigachev 	/* AMD_CG_SUPPORT_GFX_RLC_LS */
3831b843c749SSergey Zigachev 	data = RREG32_SOC15(GC, 0, mmRLC_MEM_SLP_CNTL);
3832b843c749SSergey Zigachev 	if (data & RLC_MEM_SLP_CNTL__RLC_MEM_LS_EN_MASK)
3833b843c749SSergey Zigachev 		*flags |= AMD_CG_SUPPORT_GFX_RLC_LS | AMD_CG_SUPPORT_GFX_MGLS;
3834b843c749SSergey Zigachev 
3835b843c749SSergey Zigachev 	/* AMD_CG_SUPPORT_GFX_CP_LS */
3836b843c749SSergey Zigachev 	data = RREG32_SOC15(GC, 0, mmCP_MEM_SLP_CNTL);
3837b843c749SSergey Zigachev 	if (data & CP_MEM_SLP_CNTL__CP_MEM_LS_EN_MASK)
3838b843c749SSergey Zigachev 		*flags |= AMD_CG_SUPPORT_GFX_CP_LS | AMD_CG_SUPPORT_GFX_MGLS;
3839b843c749SSergey Zigachev 
3840b843c749SSergey Zigachev 	/* AMD_CG_SUPPORT_GFX_3D_CGCG */
3841b843c749SSergey Zigachev 	data = RREG32_SOC15(GC, 0, mmRLC_CGCG_CGLS_CTRL_3D);
3842b843c749SSergey Zigachev 	if (data & RLC_CGCG_CGLS_CTRL_3D__CGCG_EN_MASK)
3843b843c749SSergey Zigachev 		*flags |= AMD_CG_SUPPORT_GFX_3D_CGCG;
3844b843c749SSergey Zigachev 
3845b843c749SSergey Zigachev 	/* AMD_CG_SUPPORT_GFX_3D_CGLS */
3846b843c749SSergey Zigachev 	if (data & RLC_CGCG_CGLS_CTRL_3D__CGLS_EN_MASK)
3847b843c749SSergey Zigachev 		*flags |= AMD_CG_SUPPORT_GFX_3D_CGLS;
3848b843c749SSergey Zigachev }
3849b843c749SSergey Zigachev 
gfx_v9_0_ring_get_rptr_gfx(struct amdgpu_ring * ring)385078973132SSergey Zigachev static uint64_t gfx_v9_0_ring_get_rptr_gfx(struct amdgpu_ring *ring)
3851b843c749SSergey Zigachev {
3852b843c749SSergey Zigachev 	return ring->adev->wb.wb[ring->rptr_offs]; /* gfx9 is 32bit rptr*/
3853b843c749SSergey Zigachev }
3854b843c749SSergey Zigachev 
gfx_v9_0_ring_get_wptr_gfx(struct amdgpu_ring * ring)385578973132SSergey Zigachev static uint64_t gfx_v9_0_ring_get_wptr_gfx(struct amdgpu_ring *ring)
3856b843c749SSergey Zigachev {
3857b843c749SSergey Zigachev 	struct amdgpu_device *adev = ring->adev;
3858b843c749SSergey Zigachev 	u64 wptr;
3859b843c749SSergey Zigachev 
3860b843c749SSergey Zigachev 	/* XXX check if swapping is necessary on BE */
3861b843c749SSergey Zigachev 	if (ring->use_doorbell) {
3862b843c749SSergey Zigachev 		wptr = atomic64_read((atomic64_t *)&adev->wb.wb[ring->wptr_offs]);
3863b843c749SSergey Zigachev 	} else {
3864b843c749SSergey Zigachev 		wptr = RREG32_SOC15(GC, 0, mmCP_RB0_WPTR);
3865b843c749SSergey Zigachev 		wptr += (u64)RREG32_SOC15(GC, 0, mmCP_RB0_WPTR_HI) << 32;
3866b843c749SSergey Zigachev 	}
3867b843c749SSergey Zigachev 
3868b843c749SSergey Zigachev 	return wptr;
3869b843c749SSergey Zigachev }
3870b843c749SSergey Zigachev 
gfx_v9_0_ring_set_wptr_gfx(struct amdgpu_ring * ring)3871b843c749SSergey Zigachev static void gfx_v9_0_ring_set_wptr_gfx(struct amdgpu_ring *ring)
3872b843c749SSergey Zigachev {
3873b843c749SSergey Zigachev 	struct amdgpu_device *adev = ring->adev;
3874b843c749SSergey Zigachev 
3875b843c749SSergey Zigachev 	if (ring->use_doorbell) {
3876b843c749SSergey Zigachev 		/* XXX check if swapping is necessary on BE */
3877b843c749SSergey Zigachev 		atomic64_set((atomic64_t*)&adev->wb.wb[ring->wptr_offs], ring->wptr);
3878b843c749SSergey Zigachev 		WDOORBELL64(ring->doorbell_index, ring->wptr);
3879b843c749SSergey Zigachev 	} else {
3880b843c749SSergey Zigachev 		WREG32_SOC15(GC, 0, mmCP_RB0_WPTR, lower_32_bits(ring->wptr));
3881b843c749SSergey Zigachev 		WREG32_SOC15(GC, 0, mmCP_RB0_WPTR_HI, upper_32_bits(ring->wptr));
3882b843c749SSergey Zigachev 	}
3883b843c749SSergey Zigachev }
3884b843c749SSergey Zigachev 
gfx_v9_0_ring_emit_hdp_flush(struct amdgpu_ring * ring)3885b843c749SSergey Zigachev static void gfx_v9_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
3886b843c749SSergey Zigachev {
3887b843c749SSergey Zigachev 	struct amdgpu_device *adev = ring->adev;
3888b843c749SSergey Zigachev 	u32 ref_and_mask, reg_mem_engine;
3889b843c749SSergey Zigachev 	const struct nbio_hdp_flush_reg *nbio_hf_reg = adev->nbio_funcs->hdp_flush_reg;
3890b843c749SSergey Zigachev 
3891b843c749SSergey Zigachev 	if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE) {
3892b843c749SSergey Zigachev 		switch (ring->me) {
3893b843c749SSergey Zigachev 		case 1:
3894b843c749SSergey Zigachev 			ref_and_mask = nbio_hf_reg->ref_and_mask_cp2 << ring->pipe;
3895b843c749SSergey Zigachev 			break;
3896b843c749SSergey Zigachev 		case 2:
3897b843c749SSergey Zigachev 			ref_and_mask = nbio_hf_reg->ref_and_mask_cp6 << ring->pipe;
3898b843c749SSergey Zigachev 			break;
3899b843c749SSergey Zigachev 		default:
3900b843c749SSergey Zigachev 			return;
3901b843c749SSergey Zigachev 		}
3902b843c749SSergey Zigachev 		reg_mem_engine = 0;
3903b843c749SSergey Zigachev 	} else {
3904b843c749SSergey Zigachev 		ref_and_mask = nbio_hf_reg->ref_and_mask_cp0;
3905b843c749SSergey Zigachev 		reg_mem_engine = 1; /* pfp */
3906b843c749SSergey Zigachev 	}
3907b843c749SSergey Zigachev 
3908b843c749SSergey Zigachev 	gfx_v9_0_wait_reg_mem(ring, reg_mem_engine, 0, 1,
3909b843c749SSergey Zigachev 			      adev->nbio_funcs->get_hdp_flush_req_offset(adev),
3910b843c749SSergey Zigachev 			      adev->nbio_funcs->get_hdp_flush_done_offset(adev),
3911b843c749SSergey Zigachev 			      ref_and_mask, ref_and_mask, 0x20);
3912b843c749SSergey Zigachev }
3913b843c749SSergey Zigachev 
gfx_v9_0_ring_emit_ib_gfx(struct amdgpu_ring * ring,struct amdgpu_ib * ib,unsigned vmid,bool ctx_switch)3914b843c749SSergey Zigachev static void gfx_v9_0_ring_emit_ib_gfx(struct amdgpu_ring *ring,
3915b843c749SSergey Zigachev                                       struct amdgpu_ib *ib,
3916b843c749SSergey Zigachev                                       unsigned vmid, bool ctx_switch)
3917b843c749SSergey Zigachev {
3918b843c749SSergey Zigachev 	u32 header, control = 0;
3919b843c749SSergey Zigachev 
3920b843c749SSergey Zigachev 	if (ib->flags & AMDGPU_IB_FLAG_CE)
3921b843c749SSergey Zigachev 		header = PACKET3(PACKET3_INDIRECT_BUFFER_CONST, 2);
3922b843c749SSergey Zigachev 	else
3923b843c749SSergey Zigachev 		header = PACKET3(PACKET3_INDIRECT_BUFFER, 2);
3924b843c749SSergey Zigachev 
3925b843c749SSergey Zigachev 	control |= ib->length_dw | (vmid << 24);
3926b843c749SSergey Zigachev 
3927b843c749SSergey Zigachev 	if (amdgpu_sriov_vf(ring->adev) && (ib->flags & AMDGPU_IB_FLAG_PREEMPT)) {
3928b843c749SSergey Zigachev 		control |= INDIRECT_BUFFER_PRE_ENB(1);
3929b843c749SSergey Zigachev 
3930b843c749SSergey Zigachev 		if (!(ib->flags & AMDGPU_IB_FLAG_CE))
3931b843c749SSergey Zigachev 			gfx_v9_0_ring_emit_de_meta(ring);
3932b843c749SSergey Zigachev 	}
3933b843c749SSergey Zigachev 
3934b843c749SSergey Zigachev 	amdgpu_ring_write(ring, header);
3935b843c749SSergey Zigachev 	BUG_ON(ib->gpu_addr & 0x3); /* Dword align */
3936b843c749SSergey Zigachev 	amdgpu_ring_write(ring,
3937b843c749SSergey Zigachev #ifdef __BIG_ENDIAN
3938b843c749SSergey Zigachev 		(2 << 0) |
3939b843c749SSergey Zigachev #endif
3940b843c749SSergey Zigachev 		lower_32_bits(ib->gpu_addr));
3941b843c749SSergey Zigachev 	amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
3942b843c749SSergey Zigachev 	amdgpu_ring_write(ring, control);
3943b843c749SSergey Zigachev }
3944b843c749SSergey Zigachev 
gfx_v9_0_ring_emit_ib_compute(struct amdgpu_ring * ring,struct amdgpu_ib * ib,unsigned vmid,bool ctx_switch)3945b843c749SSergey Zigachev static void gfx_v9_0_ring_emit_ib_compute(struct amdgpu_ring *ring,
3946b843c749SSergey Zigachev                                           struct amdgpu_ib *ib,
3947b843c749SSergey Zigachev                                           unsigned vmid, bool ctx_switch)
3948b843c749SSergey Zigachev {
3949b843c749SSergey Zigachev         u32 control = INDIRECT_BUFFER_VALID | ib->length_dw | (vmid << 24);
3950b843c749SSergey Zigachev 
3951b843c749SSergey Zigachev         amdgpu_ring_write(ring, PACKET3(PACKET3_INDIRECT_BUFFER, 2));
3952b843c749SSergey Zigachev 	BUG_ON(ib->gpu_addr & 0x3); /* Dword align */
3953b843c749SSergey Zigachev         amdgpu_ring_write(ring,
3954b843c749SSergey Zigachev #ifdef __BIG_ENDIAN
3955b843c749SSergey Zigachev                                 (2 << 0) |
3956b843c749SSergey Zigachev #endif
3957b843c749SSergey Zigachev                                 lower_32_bits(ib->gpu_addr));
3958b843c749SSergey Zigachev         amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
3959b843c749SSergey Zigachev         amdgpu_ring_write(ring, control);
3960b843c749SSergey Zigachev }
3961b843c749SSergey Zigachev 
gfx_v9_0_ring_emit_fence(struct amdgpu_ring * ring,uint64_t addr,uint64_t seq,unsigned flags)396278973132SSergey Zigachev static void gfx_v9_0_ring_emit_fence(struct amdgpu_ring *ring, uint64_t addr,
396378973132SSergey Zigachev 				     uint64_t seq, unsigned flags)
3964b843c749SSergey Zigachev {
3965b843c749SSergey Zigachev 	bool write64bit = flags & AMDGPU_FENCE_FLAG_64BIT;
3966b843c749SSergey Zigachev 	bool int_sel = flags & AMDGPU_FENCE_FLAG_INT;
3967b843c749SSergey Zigachev 	bool writeback = flags & AMDGPU_FENCE_FLAG_TC_WB_ONLY;
3968b843c749SSergey Zigachev 
3969b843c749SSergey Zigachev 	/* RELEASE_MEM - flush caches, send int */
3970b843c749SSergey Zigachev 	amdgpu_ring_write(ring, PACKET3(PACKET3_RELEASE_MEM, 6));
3971b843c749SSergey Zigachev 	amdgpu_ring_write(ring, ((writeback ? (EOP_TC_WB_ACTION_EN |
3972b843c749SSergey Zigachev 					       EOP_TC_NC_ACTION_EN) :
3973b843c749SSergey Zigachev 					      (EOP_TCL1_ACTION_EN |
3974b843c749SSergey Zigachev 					       EOP_TC_ACTION_EN |
3975b843c749SSergey Zigachev 					       EOP_TC_WB_ACTION_EN |
3976b843c749SSergey Zigachev 					       EOP_TC_MD_ACTION_EN)) |
3977b843c749SSergey Zigachev 				 EVENT_TYPE(CACHE_FLUSH_AND_INV_TS_EVENT) |
3978b843c749SSergey Zigachev 				 EVENT_INDEX(5)));
3979b843c749SSergey Zigachev 	amdgpu_ring_write(ring, DATA_SEL(write64bit ? 2 : 1) | INT_SEL(int_sel ? 2 : 0));
3980b843c749SSergey Zigachev 
3981b843c749SSergey Zigachev 	/*
3982b843c749SSergey Zigachev 	 * the address should be Qword aligned if 64bit write, Dword
3983b843c749SSergey Zigachev 	 * aligned if only send 32bit data low (discard data high)
3984b843c749SSergey Zigachev 	 */
3985b843c749SSergey Zigachev 	if (write64bit)
3986b843c749SSergey Zigachev 		BUG_ON(addr & 0x7);
3987b843c749SSergey Zigachev 	else
3988b843c749SSergey Zigachev 		BUG_ON(addr & 0x3);
3989b843c749SSergey Zigachev 	amdgpu_ring_write(ring, lower_32_bits(addr));
3990b843c749SSergey Zigachev 	amdgpu_ring_write(ring, upper_32_bits(addr));
3991b843c749SSergey Zigachev 	amdgpu_ring_write(ring, lower_32_bits(seq));
3992b843c749SSergey Zigachev 	amdgpu_ring_write(ring, upper_32_bits(seq));
3993b843c749SSergey Zigachev 	amdgpu_ring_write(ring, 0);
3994b843c749SSergey Zigachev }
3995b843c749SSergey Zigachev 
gfx_v9_0_ring_emit_pipeline_sync(struct amdgpu_ring * ring)3996b843c749SSergey Zigachev static void gfx_v9_0_ring_emit_pipeline_sync(struct amdgpu_ring *ring)
3997b843c749SSergey Zigachev {
3998b843c749SSergey Zigachev 	int usepfp = (ring->funcs->type == AMDGPU_RING_TYPE_GFX);
3999b843c749SSergey Zigachev 	uint32_t seq = ring->fence_drv.sync_seq;
4000b843c749SSergey Zigachev 	uint64_t addr = ring->fence_drv.gpu_addr;
4001b843c749SSergey Zigachev 
4002b843c749SSergey Zigachev 	gfx_v9_0_wait_reg_mem(ring, usepfp, 1, 0,
4003b843c749SSergey Zigachev 			      lower_32_bits(addr), upper_32_bits(addr),
4004b843c749SSergey Zigachev 			      seq, 0xffffffff, 4);
4005b843c749SSergey Zigachev }
4006b843c749SSergey Zigachev 
gfx_v9_0_ring_emit_vm_flush(struct amdgpu_ring * ring,unsigned vmid,uint64_t pd_addr)4007b843c749SSergey Zigachev static void gfx_v9_0_ring_emit_vm_flush(struct amdgpu_ring *ring,
4008b843c749SSergey Zigachev 					unsigned vmid, uint64_t pd_addr)
4009b843c749SSergey Zigachev {
4010b843c749SSergey Zigachev 	amdgpu_gmc_emit_flush_gpu_tlb(ring, vmid, pd_addr);
4011b843c749SSergey Zigachev 
4012b843c749SSergey Zigachev 	/* compute doesn't have PFP */
4013b843c749SSergey Zigachev 	if (ring->funcs->type == AMDGPU_RING_TYPE_GFX) {
4014b843c749SSergey Zigachev 		/* sync PFP to ME, otherwise we might get invalid PFP reads */
4015b843c749SSergey Zigachev 		amdgpu_ring_write(ring, PACKET3(PACKET3_PFP_SYNC_ME, 0));
4016b843c749SSergey Zigachev 		amdgpu_ring_write(ring, 0x0);
4017b843c749SSergey Zigachev 	}
4018b843c749SSergey Zigachev }
4019b843c749SSergey Zigachev 
gfx_v9_0_ring_get_rptr_compute(struct amdgpu_ring * ring)402078973132SSergey Zigachev static uint64_t gfx_v9_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
4021b843c749SSergey Zigachev {
4022b843c749SSergey Zigachev 	return ring->adev->wb.wb[ring->rptr_offs]; /* gfx9 hardware is 32bit rptr */
4023b843c749SSergey Zigachev }
4024b843c749SSergey Zigachev 
gfx_v9_0_ring_get_wptr_compute(struct amdgpu_ring * ring)402578973132SSergey Zigachev static uint64_t gfx_v9_0_ring_get_wptr_compute(struct amdgpu_ring *ring)
4026b843c749SSergey Zigachev {
402778973132SSergey Zigachev 	uint64_t wptr;
4028b843c749SSergey Zigachev 
4029b843c749SSergey Zigachev 	/* XXX check if swapping is necessary on BE */
4030b843c749SSergey Zigachev 	if (ring->use_doorbell)
4031b843c749SSergey Zigachev 		wptr = atomic64_read((atomic64_t *)&ring->adev->wb.wb[ring->wptr_offs]);
4032b843c749SSergey Zigachev 	else
4033b843c749SSergey Zigachev 		BUG();
4034b843c749SSergey Zigachev 	return wptr;
4035b843c749SSergey Zigachev }
4036b843c749SSergey Zigachev 
gfx_v9_0_ring_set_pipe_percent(struct amdgpu_ring * ring,bool acquire)4037b843c749SSergey Zigachev static void gfx_v9_0_ring_set_pipe_percent(struct amdgpu_ring *ring,
4038b843c749SSergey Zigachev 					   bool acquire)
4039b843c749SSergey Zigachev {
4040b843c749SSergey Zigachev 	struct amdgpu_device *adev = ring->adev;
4041b843c749SSergey Zigachev 	int pipe_num, tmp, reg;
4042b843c749SSergey Zigachev 	int pipe_percent = acquire ? SPI_WCL_PIPE_PERCENT_GFX__VALUE_MASK : 0x1;
4043b843c749SSergey Zigachev 
4044b843c749SSergey Zigachev 	pipe_num = ring->me * adev->gfx.mec.num_pipe_per_mec + ring->pipe;
4045b843c749SSergey Zigachev 
4046b843c749SSergey Zigachev 	/* first me only has 2 entries, GFX and HP3D */
4047b843c749SSergey Zigachev 	if (ring->me > 0)
4048b843c749SSergey Zigachev 		pipe_num -= 2;
4049b843c749SSergey Zigachev 
4050b843c749SSergey Zigachev 	reg = SOC15_REG_OFFSET(GC, 0, mmSPI_WCL_PIPE_PERCENT_GFX) + pipe_num;
4051b843c749SSergey Zigachev 	tmp = RREG32(reg);
4052b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, SPI_WCL_PIPE_PERCENT_GFX, VALUE, pipe_percent);
4053b843c749SSergey Zigachev 	WREG32(reg, tmp);
4054b843c749SSergey Zigachev }
4055b843c749SSergey Zigachev 
gfx_v9_0_pipe_reserve_resources(struct amdgpu_device * adev,struct amdgpu_ring * ring,bool acquire)4056b843c749SSergey Zigachev static void gfx_v9_0_pipe_reserve_resources(struct amdgpu_device *adev,
4057b843c749SSergey Zigachev 					    struct amdgpu_ring *ring,
4058b843c749SSergey Zigachev 					    bool acquire)
4059b843c749SSergey Zigachev {
4060b843c749SSergey Zigachev 	int i, pipe;
4061b843c749SSergey Zigachev 	bool reserve;
4062b843c749SSergey Zigachev 	struct amdgpu_ring *iring;
4063b843c749SSergey Zigachev 
4064b843c749SSergey Zigachev 	mutex_lock(&adev->gfx.pipe_reserve_mutex);
4065b843c749SSergey Zigachev 	pipe = amdgpu_gfx_queue_to_bit(adev, ring->me, ring->pipe, 0);
4066b843c749SSergey Zigachev 	if (acquire)
4067b843c749SSergey Zigachev 		set_bit(pipe, adev->gfx.pipe_reserve_bitmap);
4068b843c749SSergey Zigachev 	else
4069b843c749SSergey Zigachev 		clear_bit(pipe, adev->gfx.pipe_reserve_bitmap);
4070b843c749SSergey Zigachev 
4071b843c749SSergey Zigachev 	if (!bitmap_weight(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES)) {
4072b843c749SSergey Zigachev 		/* Clear all reservations - everyone reacquires all resources */
4073b843c749SSergey Zigachev 		for (i = 0; i < adev->gfx.num_gfx_rings; ++i)
4074b843c749SSergey Zigachev 			gfx_v9_0_ring_set_pipe_percent(&adev->gfx.gfx_ring[i],
4075b843c749SSergey Zigachev 						       true);
4076b843c749SSergey Zigachev 
4077b843c749SSergey Zigachev 		for (i = 0; i < adev->gfx.num_compute_rings; ++i)
4078b843c749SSergey Zigachev 			gfx_v9_0_ring_set_pipe_percent(&adev->gfx.compute_ring[i],
4079b843c749SSergey Zigachev 						       true);
4080b843c749SSergey Zigachev 	} else {
4081b843c749SSergey Zigachev 		/* Lower all pipes without a current reservation */
4082b843c749SSergey Zigachev 		for (i = 0; i < adev->gfx.num_gfx_rings; ++i) {
4083b843c749SSergey Zigachev 			iring = &adev->gfx.gfx_ring[i];
4084b843c749SSergey Zigachev 			pipe = amdgpu_gfx_queue_to_bit(adev,
4085b843c749SSergey Zigachev 						       iring->me,
4086b843c749SSergey Zigachev 						       iring->pipe,
4087b843c749SSergey Zigachev 						       0);
4088b843c749SSergey Zigachev 			reserve = test_bit(pipe, adev->gfx.pipe_reserve_bitmap);
4089b843c749SSergey Zigachev 			gfx_v9_0_ring_set_pipe_percent(iring, reserve);
4090b843c749SSergey Zigachev 		}
4091b843c749SSergey Zigachev 
4092b843c749SSergey Zigachev 		for (i = 0; i < adev->gfx.num_compute_rings; ++i) {
4093b843c749SSergey Zigachev 			iring = &adev->gfx.compute_ring[i];
4094b843c749SSergey Zigachev 			pipe = amdgpu_gfx_queue_to_bit(adev,
4095b843c749SSergey Zigachev 						       iring->me,
4096b843c749SSergey Zigachev 						       iring->pipe,
4097b843c749SSergey Zigachev 						       0);
4098b843c749SSergey Zigachev 			reserve = test_bit(pipe, adev->gfx.pipe_reserve_bitmap);
4099b843c749SSergey Zigachev 			gfx_v9_0_ring_set_pipe_percent(iring, reserve);
4100b843c749SSergey Zigachev 		}
4101b843c749SSergey Zigachev 	}
4102b843c749SSergey Zigachev 
4103b843c749SSergey Zigachev 	mutex_unlock(&adev->gfx.pipe_reserve_mutex);
4104b843c749SSergey Zigachev }
4105b843c749SSergey Zigachev 
gfx_v9_0_hqd_set_priority(struct amdgpu_device * adev,struct amdgpu_ring * ring,bool acquire)4106b843c749SSergey Zigachev static void gfx_v9_0_hqd_set_priority(struct amdgpu_device *adev,
4107b843c749SSergey Zigachev 				      struct amdgpu_ring *ring,
4108b843c749SSergey Zigachev 				      bool acquire)
4109b843c749SSergey Zigachev {
4110b843c749SSergey Zigachev 	uint32_t pipe_priority = acquire ? 0x2 : 0x0;
4111b843c749SSergey Zigachev 	uint32_t queue_priority = acquire ? 0xf : 0x0;
4112b843c749SSergey Zigachev 
4113b843c749SSergey Zigachev 	mutex_lock(&adev->srbm_mutex);
4114b843c749SSergey Zigachev 	soc15_grbm_select(adev, ring->me, ring->pipe, ring->queue, 0);
4115b843c749SSergey Zigachev 
4116b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_HQD_PIPE_PRIORITY, pipe_priority);
4117b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmCP_HQD_QUEUE_PRIORITY, queue_priority);
4118b843c749SSergey Zigachev 
4119b843c749SSergey Zigachev 	soc15_grbm_select(adev, 0, 0, 0, 0);
4120b843c749SSergey Zigachev 	mutex_unlock(&adev->srbm_mutex);
4121b843c749SSergey Zigachev }
4122b843c749SSergey Zigachev 
gfx_v9_0_ring_set_priority_compute(struct amdgpu_ring * ring,enum drm_sched_priority priority)4123b843c749SSergey Zigachev static void gfx_v9_0_ring_set_priority_compute(struct amdgpu_ring *ring,
4124b843c749SSergey Zigachev 					       enum drm_sched_priority priority)
4125b843c749SSergey Zigachev {
4126b843c749SSergey Zigachev 	struct amdgpu_device *adev = ring->adev;
4127b843c749SSergey Zigachev 	bool acquire = priority == DRM_SCHED_PRIORITY_HIGH_HW;
4128b843c749SSergey Zigachev 
4129b843c749SSergey Zigachev 	if (ring->funcs->type != AMDGPU_RING_TYPE_COMPUTE)
4130b843c749SSergey Zigachev 		return;
4131b843c749SSergey Zigachev 
4132b843c749SSergey Zigachev 	gfx_v9_0_hqd_set_priority(adev, ring, acquire);
4133b843c749SSergey Zigachev 	gfx_v9_0_pipe_reserve_resources(adev, ring, acquire);
4134b843c749SSergey Zigachev }
4135b843c749SSergey Zigachev 
gfx_v9_0_ring_set_wptr_compute(struct amdgpu_ring * ring)4136b843c749SSergey Zigachev static void gfx_v9_0_ring_set_wptr_compute(struct amdgpu_ring *ring)
4137b843c749SSergey Zigachev {
4138b843c749SSergey Zigachev 	struct amdgpu_device *adev = ring->adev;
4139b843c749SSergey Zigachev 
4140b843c749SSergey Zigachev 	/* XXX check if swapping is necessary on BE */
4141b843c749SSergey Zigachev 	if (ring->use_doorbell) {
4142b843c749SSergey Zigachev 		atomic64_set((atomic64_t*)&adev->wb.wb[ring->wptr_offs], ring->wptr);
4143b843c749SSergey Zigachev 		WDOORBELL64(ring->doorbell_index, ring->wptr);
4144b843c749SSergey Zigachev 	} else{
4145b843c749SSergey Zigachev 		BUG(); /* only DOORBELL method supported on gfx9 now */
4146b843c749SSergey Zigachev 	}
4147b843c749SSergey Zigachev }
4148b843c749SSergey Zigachev 
gfx_v9_0_ring_emit_fence_kiq(struct amdgpu_ring * ring,uint64_t addr,uint64_t seq,unsigned int flags)414978973132SSergey Zigachev static void gfx_v9_0_ring_emit_fence_kiq(struct amdgpu_ring *ring, uint64_t addr,
415078973132SSergey Zigachev 					 uint64_t seq, unsigned int flags)
4151b843c749SSergey Zigachev {
4152b843c749SSergey Zigachev 	struct amdgpu_device *adev = ring->adev;
4153b843c749SSergey Zigachev 
4154b843c749SSergey Zigachev 	/* we only allocate 32bit for each seq wb address */
4155b843c749SSergey Zigachev 	BUG_ON(flags & AMDGPU_FENCE_FLAG_64BIT);
4156b843c749SSergey Zigachev 
4157b843c749SSergey Zigachev 	/* write fence seq to the "addr" */
4158b843c749SSergey Zigachev 	amdgpu_ring_write(ring, PACKET3(PACKET3_WRITE_DATA, 3));
4159b843c749SSergey Zigachev 	amdgpu_ring_write(ring, (WRITE_DATA_ENGINE_SEL(0) |
4160b843c749SSergey Zigachev 				 WRITE_DATA_DST_SEL(5) | WR_CONFIRM));
4161b843c749SSergey Zigachev 	amdgpu_ring_write(ring, lower_32_bits(addr));
4162b843c749SSergey Zigachev 	amdgpu_ring_write(ring, upper_32_bits(addr));
4163b843c749SSergey Zigachev 	amdgpu_ring_write(ring, lower_32_bits(seq));
4164b843c749SSergey Zigachev 
4165b843c749SSergey Zigachev 	if (flags & AMDGPU_FENCE_FLAG_INT) {
4166b843c749SSergey Zigachev 		/* set register to trigger INT */
4167b843c749SSergey Zigachev 		amdgpu_ring_write(ring, PACKET3(PACKET3_WRITE_DATA, 3));
4168b843c749SSergey Zigachev 		amdgpu_ring_write(ring, (WRITE_DATA_ENGINE_SEL(0) |
4169b843c749SSergey Zigachev 					 WRITE_DATA_DST_SEL(0) | WR_CONFIRM));
4170b843c749SSergey Zigachev 		amdgpu_ring_write(ring, SOC15_REG_OFFSET(GC, 0, mmCPC_INT_STATUS));
4171b843c749SSergey Zigachev 		amdgpu_ring_write(ring, 0);
4172b843c749SSergey Zigachev 		amdgpu_ring_write(ring, 0x20000000); /* src_id is 178 */
4173b843c749SSergey Zigachev 	}
4174b843c749SSergey Zigachev }
4175b843c749SSergey Zigachev 
gfx_v9_ring_emit_sb(struct amdgpu_ring * ring)4176b843c749SSergey Zigachev static void gfx_v9_ring_emit_sb(struct amdgpu_ring *ring)
4177b843c749SSergey Zigachev {
4178b843c749SSergey Zigachev 	amdgpu_ring_write(ring, PACKET3(PACKET3_SWITCH_BUFFER, 0));
4179b843c749SSergey Zigachev 	amdgpu_ring_write(ring, 0);
4180b843c749SSergey Zigachev }
4181b843c749SSergey Zigachev 
gfx_v9_0_ring_emit_ce_meta(struct amdgpu_ring * ring)4182b843c749SSergey Zigachev static void gfx_v9_0_ring_emit_ce_meta(struct amdgpu_ring *ring)
4183b843c749SSergey Zigachev {
4184b843c749SSergey Zigachev 	struct v9_ce_ib_state ce_payload = {0};
4185b843c749SSergey Zigachev 	uint64_t csa_addr;
4186b843c749SSergey Zigachev 	int cnt;
4187b843c749SSergey Zigachev 
4188b843c749SSergey Zigachev 	cnt = (sizeof(ce_payload) >> 2) + 4 - 2;
4189b843c749SSergey Zigachev 	csa_addr = amdgpu_csa_vaddr(ring->adev);
4190b843c749SSergey Zigachev 
4191b843c749SSergey Zigachev 	amdgpu_ring_write(ring, PACKET3(PACKET3_WRITE_DATA, cnt));
4192b843c749SSergey Zigachev 	amdgpu_ring_write(ring, (WRITE_DATA_ENGINE_SEL(2) |
4193b843c749SSergey Zigachev 				 WRITE_DATA_DST_SEL(8) |
4194b843c749SSergey Zigachev 				 WR_CONFIRM) |
4195b843c749SSergey Zigachev 				 WRITE_DATA_CACHE_POLICY(0));
4196b843c749SSergey Zigachev 	amdgpu_ring_write(ring, lower_32_bits(csa_addr + offsetof(struct v9_gfx_meta_data, ce_payload)));
4197b843c749SSergey Zigachev 	amdgpu_ring_write(ring, upper_32_bits(csa_addr + offsetof(struct v9_gfx_meta_data, ce_payload)));
4198b843c749SSergey Zigachev 	amdgpu_ring_write_multiple(ring, (void *)&ce_payload, sizeof(ce_payload) >> 2);
4199b843c749SSergey Zigachev }
4200b843c749SSergey Zigachev 
gfx_v9_0_ring_emit_de_meta(struct amdgpu_ring * ring)4201b843c749SSergey Zigachev static void gfx_v9_0_ring_emit_de_meta(struct amdgpu_ring *ring)
4202b843c749SSergey Zigachev {
4203b843c749SSergey Zigachev 	struct v9_de_ib_state de_payload = {0};
4204b843c749SSergey Zigachev 	uint64_t csa_addr, gds_addr;
4205b843c749SSergey Zigachev 	int cnt;
4206b843c749SSergey Zigachev 
4207b843c749SSergey Zigachev 	csa_addr = amdgpu_csa_vaddr(ring->adev);
4208b843c749SSergey Zigachev 	gds_addr = csa_addr + 4096;
4209b843c749SSergey Zigachev 	de_payload.gds_backup_addrlo = lower_32_bits(gds_addr);
4210b843c749SSergey Zigachev 	de_payload.gds_backup_addrhi = upper_32_bits(gds_addr);
4211b843c749SSergey Zigachev 
4212b843c749SSergey Zigachev 	cnt = (sizeof(de_payload) >> 2) + 4 - 2;
4213b843c749SSergey Zigachev 	amdgpu_ring_write(ring, PACKET3(PACKET3_WRITE_DATA, cnt));
4214b843c749SSergey Zigachev 	amdgpu_ring_write(ring, (WRITE_DATA_ENGINE_SEL(1) |
4215b843c749SSergey Zigachev 				 WRITE_DATA_DST_SEL(8) |
4216b843c749SSergey Zigachev 				 WR_CONFIRM) |
4217b843c749SSergey Zigachev 				 WRITE_DATA_CACHE_POLICY(0));
4218b843c749SSergey Zigachev 	amdgpu_ring_write(ring, lower_32_bits(csa_addr + offsetof(struct v9_gfx_meta_data, de_payload)));
4219b843c749SSergey Zigachev 	amdgpu_ring_write(ring, upper_32_bits(csa_addr + offsetof(struct v9_gfx_meta_data, de_payload)));
4220b843c749SSergey Zigachev 	amdgpu_ring_write_multiple(ring, (void *)&de_payload, sizeof(de_payload) >> 2);
4221b843c749SSergey Zigachev }
4222b843c749SSergey Zigachev 
gfx_v9_0_ring_emit_tmz(struct amdgpu_ring * ring,bool start)4223b843c749SSergey Zigachev static void gfx_v9_0_ring_emit_tmz(struct amdgpu_ring *ring, bool start)
4224b843c749SSergey Zigachev {
4225b843c749SSergey Zigachev 	amdgpu_ring_write(ring, PACKET3(PACKET3_FRAME_CONTROL, 0));
4226b843c749SSergey Zigachev 	amdgpu_ring_write(ring, FRAME_CMD(start ? 0 : 1)); /* frame_end */
4227b843c749SSergey Zigachev }
4228b843c749SSergey Zigachev 
gfx_v9_ring_emit_cntxcntl(struct amdgpu_ring * ring,uint32_t flags)4229b843c749SSergey Zigachev static void gfx_v9_ring_emit_cntxcntl(struct amdgpu_ring *ring, uint32_t flags)
4230b843c749SSergey Zigachev {
4231b843c749SSergey Zigachev 	uint32_t dw2 = 0;
4232b843c749SSergey Zigachev 
4233b843c749SSergey Zigachev 	if (amdgpu_sriov_vf(ring->adev))
4234b843c749SSergey Zigachev 		gfx_v9_0_ring_emit_ce_meta(ring);
4235b843c749SSergey Zigachev 
4236b843c749SSergey Zigachev 	gfx_v9_0_ring_emit_tmz(ring, true);
4237b843c749SSergey Zigachev 
4238b843c749SSergey Zigachev 	dw2 |= 0x80000000; /* set load_enable otherwise this package is just NOPs */
4239b843c749SSergey Zigachev 	if (flags & AMDGPU_HAVE_CTX_SWITCH) {
4240b843c749SSergey Zigachev 		/* set load_global_config & load_global_uconfig */
4241b843c749SSergey Zigachev 		dw2 |= 0x8001;
4242b843c749SSergey Zigachev 		/* set load_cs_sh_regs */
4243b843c749SSergey Zigachev 		dw2 |= 0x01000000;
4244b843c749SSergey Zigachev 		/* set load_per_context_state & load_gfx_sh_regs for GFX */
4245b843c749SSergey Zigachev 		dw2 |= 0x10002;
4246b843c749SSergey Zigachev 
4247b843c749SSergey Zigachev 		/* set load_ce_ram if preamble presented */
4248b843c749SSergey Zigachev 		if (AMDGPU_PREAMBLE_IB_PRESENT & flags)
4249b843c749SSergey Zigachev 			dw2 |= 0x10000000;
4250b843c749SSergey Zigachev 	} else {
4251b843c749SSergey Zigachev 		/* still load_ce_ram if this is the first time preamble presented
4252b843c749SSergey Zigachev 		 * although there is no context switch happens.
4253b843c749SSergey Zigachev 		 */
4254b843c749SSergey Zigachev 		if (AMDGPU_PREAMBLE_IB_PRESENT_FIRST & flags)
4255b843c749SSergey Zigachev 			dw2 |= 0x10000000;
4256b843c749SSergey Zigachev 	}
4257b843c749SSergey Zigachev 
4258b843c749SSergey Zigachev 	amdgpu_ring_write(ring, PACKET3(PACKET3_CONTEXT_CONTROL, 1));
4259b843c749SSergey Zigachev 	amdgpu_ring_write(ring, dw2);
4260b843c749SSergey Zigachev 	amdgpu_ring_write(ring, 0);
4261b843c749SSergey Zigachev }
4262b843c749SSergey Zigachev 
gfx_v9_0_ring_emit_init_cond_exec(struct amdgpu_ring * ring)4263b843c749SSergey Zigachev static unsigned gfx_v9_0_ring_emit_init_cond_exec(struct amdgpu_ring *ring)
4264b843c749SSergey Zigachev {
4265b843c749SSergey Zigachev 	unsigned ret;
4266b843c749SSergey Zigachev 	amdgpu_ring_write(ring, PACKET3(PACKET3_COND_EXEC, 3));
4267b843c749SSergey Zigachev 	amdgpu_ring_write(ring, lower_32_bits(ring->cond_exe_gpu_addr));
4268b843c749SSergey Zigachev 	amdgpu_ring_write(ring, upper_32_bits(ring->cond_exe_gpu_addr));
4269b843c749SSergey Zigachev 	amdgpu_ring_write(ring, 0); /* discard following DWs if *cond_exec_gpu_addr==0 */
4270b843c749SSergey Zigachev 	ret = ring->wptr & ring->buf_mask;
4271b843c749SSergey Zigachev 	amdgpu_ring_write(ring, 0x55aa55aa); /* patch dummy value later */
4272b843c749SSergey Zigachev 	return ret;
4273b843c749SSergey Zigachev }
4274b843c749SSergey Zigachev 
gfx_v9_0_ring_emit_patch_cond_exec(struct amdgpu_ring * ring,unsigned offset)4275b843c749SSergey Zigachev static void gfx_v9_0_ring_emit_patch_cond_exec(struct amdgpu_ring *ring, unsigned offset)
4276b843c749SSergey Zigachev {
4277b843c749SSergey Zigachev 	unsigned cur;
4278b843c749SSergey Zigachev 	BUG_ON(offset > ring->buf_mask);
4279b843c749SSergey Zigachev 	BUG_ON(ring->ring[offset] != 0x55aa55aa);
4280b843c749SSergey Zigachev 
4281b843c749SSergey Zigachev 	cur = (ring->wptr & ring->buf_mask) - 1;
4282b843c749SSergey Zigachev 	if (likely(cur > offset))
4283b843c749SSergey Zigachev 		ring->ring[offset] = cur - offset;
4284b843c749SSergey Zigachev 	else
4285b843c749SSergey Zigachev 		ring->ring[offset] = (ring->ring_size>>2) - offset + cur;
4286b843c749SSergey Zigachev }
4287b843c749SSergey Zigachev 
gfx_v9_0_ring_emit_rreg(struct amdgpu_ring * ring,uint32_t reg)4288b843c749SSergey Zigachev static void gfx_v9_0_ring_emit_rreg(struct amdgpu_ring *ring, uint32_t reg)
4289b843c749SSergey Zigachev {
4290b843c749SSergey Zigachev 	struct amdgpu_device *adev = ring->adev;
4291b843c749SSergey Zigachev 
4292b843c749SSergey Zigachev 	amdgpu_ring_write(ring, PACKET3(PACKET3_COPY_DATA, 4));
4293b843c749SSergey Zigachev 	amdgpu_ring_write(ring, 0 |	/* src: register*/
4294b843c749SSergey Zigachev 				(5 << 8) |	/* dst: memory */
4295b843c749SSergey Zigachev 				(1 << 20));	/* write confirm */
4296b843c749SSergey Zigachev 	amdgpu_ring_write(ring, reg);
4297b843c749SSergey Zigachev 	amdgpu_ring_write(ring, 0);
4298b843c749SSergey Zigachev 	amdgpu_ring_write(ring, lower_32_bits(adev->wb.gpu_addr +
4299b843c749SSergey Zigachev 				adev->virt.reg_val_offs * 4));
4300b843c749SSergey Zigachev 	amdgpu_ring_write(ring, upper_32_bits(adev->wb.gpu_addr +
4301b843c749SSergey Zigachev 				adev->virt.reg_val_offs * 4));
4302b843c749SSergey Zigachev }
4303b843c749SSergey Zigachev 
gfx_v9_0_ring_emit_wreg(struct amdgpu_ring * ring,uint32_t reg,uint32_t val)4304b843c749SSergey Zigachev static void gfx_v9_0_ring_emit_wreg(struct amdgpu_ring *ring, uint32_t reg,
4305b843c749SSergey Zigachev 				    uint32_t val)
4306b843c749SSergey Zigachev {
4307b843c749SSergey Zigachev 	uint32_t cmd = 0;
4308b843c749SSergey Zigachev 
4309b843c749SSergey Zigachev 	switch (ring->funcs->type) {
4310b843c749SSergey Zigachev 	case AMDGPU_RING_TYPE_GFX:
4311b843c749SSergey Zigachev 		cmd = WRITE_DATA_ENGINE_SEL(1) | WR_CONFIRM;
4312b843c749SSergey Zigachev 		break;
4313b843c749SSergey Zigachev 	case AMDGPU_RING_TYPE_KIQ:
4314b843c749SSergey Zigachev 		cmd = (1 << 16); /* no inc addr */
4315b843c749SSergey Zigachev 		break;
4316b843c749SSergey Zigachev 	default:
4317b843c749SSergey Zigachev 		cmd = WR_CONFIRM;
4318b843c749SSergey Zigachev 		break;
4319b843c749SSergey Zigachev 	}
4320b843c749SSergey Zigachev 	amdgpu_ring_write(ring, PACKET3(PACKET3_WRITE_DATA, 3));
4321b843c749SSergey Zigachev 	amdgpu_ring_write(ring, cmd);
4322b843c749SSergey Zigachev 	amdgpu_ring_write(ring, reg);
4323b843c749SSergey Zigachev 	amdgpu_ring_write(ring, 0);
4324b843c749SSergey Zigachev 	amdgpu_ring_write(ring, val);
4325b843c749SSergey Zigachev }
4326b843c749SSergey Zigachev 
gfx_v9_0_ring_emit_reg_wait(struct amdgpu_ring * ring,uint32_t reg,uint32_t val,uint32_t mask)4327b843c749SSergey Zigachev static void gfx_v9_0_ring_emit_reg_wait(struct amdgpu_ring *ring, uint32_t reg,
4328b843c749SSergey Zigachev 					uint32_t val, uint32_t mask)
4329b843c749SSergey Zigachev {
4330b843c749SSergey Zigachev 	gfx_v9_0_wait_reg_mem(ring, 0, 0, 0, reg, 0, val, mask, 0x20);
4331b843c749SSergey Zigachev }
4332b843c749SSergey Zigachev 
gfx_v9_0_ring_emit_reg_write_reg_wait(struct amdgpu_ring * ring,uint32_t reg0,uint32_t reg1,uint32_t ref,uint32_t mask)4333b843c749SSergey Zigachev static void gfx_v9_0_ring_emit_reg_write_reg_wait(struct amdgpu_ring *ring,
4334b843c749SSergey Zigachev 						  uint32_t reg0, uint32_t reg1,
4335b843c749SSergey Zigachev 						  uint32_t ref, uint32_t mask)
4336b843c749SSergey Zigachev {
4337b843c749SSergey Zigachev 	int usepfp = (ring->funcs->type == AMDGPU_RING_TYPE_GFX);
4338b843c749SSergey Zigachev 
4339b843c749SSergey Zigachev 	if (amdgpu_sriov_vf(ring->adev))
4340b843c749SSergey Zigachev 		gfx_v9_0_wait_reg_mem(ring, usepfp, 0, 1, reg0, reg1,
4341b843c749SSergey Zigachev 				      ref, mask, 0x20);
4342b843c749SSergey Zigachev 	else
4343b843c749SSergey Zigachev 		amdgpu_ring_emit_reg_write_reg_wait_helper(ring, reg0, reg1,
4344b843c749SSergey Zigachev 							   ref, mask);
4345b843c749SSergey Zigachev }
4346b843c749SSergey Zigachev 
gfx_v9_0_set_gfx_eop_interrupt_state(struct amdgpu_device * adev,enum amdgpu_interrupt_state state)4347b843c749SSergey Zigachev static void gfx_v9_0_set_gfx_eop_interrupt_state(struct amdgpu_device *adev,
4348b843c749SSergey Zigachev 						 enum amdgpu_interrupt_state state)
4349b843c749SSergey Zigachev {
4350b843c749SSergey Zigachev 	switch (state) {
4351b843c749SSergey Zigachev 	case AMDGPU_IRQ_STATE_DISABLE:
4352b843c749SSergey Zigachev 	case AMDGPU_IRQ_STATE_ENABLE:
4353b843c749SSergey Zigachev 		WREG32_FIELD15(GC, 0, CP_INT_CNTL_RING0,
4354b843c749SSergey Zigachev 			       TIME_STAMP_INT_ENABLE,
4355b843c749SSergey Zigachev 			       state == AMDGPU_IRQ_STATE_ENABLE ? 1 : 0);
4356b843c749SSergey Zigachev 		break;
4357b843c749SSergey Zigachev 	default:
4358b843c749SSergey Zigachev 		break;
4359b843c749SSergey Zigachev 	}
4360b843c749SSergey Zigachev }
4361b843c749SSergey Zigachev 
gfx_v9_0_set_compute_eop_interrupt_state(struct amdgpu_device * adev,int me,int pipe,enum amdgpu_interrupt_state state)4362b843c749SSergey Zigachev static void gfx_v9_0_set_compute_eop_interrupt_state(struct amdgpu_device *adev,
4363b843c749SSergey Zigachev 						     int me, int pipe,
4364b843c749SSergey Zigachev 						     enum amdgpu_interrupt_state state)
4365b843c749SSergey Zigachev {
4366b843c749SSergey Zigachev 	u32 mec_int_cntl, mec_int_cntl_reg;
4367b843c749SSergey Zigachev 
4368b843c749SSergey Zigachev 	/*
4369b843c749SSergey Zigachev 	 * amdgpu controls only the first MEC. That's why this function only
4370b843c749SSergey Zigachev 	 * handles the setting of interrupts for this specific MEC. All other
4371b843c749SSergey Zigachev 	 * pipes' interrupts are set by amdkfd.
4372b843c749SSergey Zigachev 	 */
4373b843c749SSergey Zigachev 
4374b843c749SSergey Zigachev 	if (me == 1) {
4375b843c749SSergey Zigachev 		switch (pipe) {
4376b843c749SSergey Zigachev 		case 0:
4377b843c749SSergey Zigachev 			mec_int_cntl_reg = SOC15_REG_OFFSET(GC, 0, mmCP_ME1_PIPE0_INT_CNTL);
4378b843c749SSergey Zigachev 			break;
4379b843c749SSergey Zigachev 		case 1:
4380b843c749SSergey Zigachev 			mec_int_cntl_reg = SOC15_REG_OFFSET(GC, 0, mmCP_ME1_PIPE1_INT_CNTL);
4381b843c749SSergey Zigachev 			break;
4382b843c749SSergey Zigachev 		case 2:
4383b843c749SSergey Zigachev 			mec_int_cntl_reg = SOC15_REG_OFFSET(GC, 0, mmCP_ME1_PIPE2_INT_CNTL);
4384b843c749SSergey Zigachev 			break;
4385b843c749SSergey Zigachev 		case 3:
4386b843c749SSergey Zigachev 			mec_int_cntl_reg = SOC15_REG_OFFSET(GC, 0, mmCP_ME1_PIPE3_INT_CNTL);
4387b843c749SSergey Zigachev 			break;
4388b843c749SSergey Zigachev 		default:
4389b843c749SSergey Zigachev 			DRM_DEBUG("invalid pipe %d\n", pipe);
4390b843c749SSergey Zigachev 			return;
4391b843c749SSergey Zigachev 		}
4392b843c749SSergey Zigachev 	} else {
4393b843c749SSergey Zigachev 		DRM_DEBUG("invalid me %d\n", me);
4394b843c749SSergey Zigachev 		return;
4395b843c749SSergey Zigachev 	}
4396b843c749SSergey Zigachev 
4397b843c749SSergey Zigachev 	switch (state) {
4398b843c749SSergey Zigachev 	case AMDGPU_IRQ_STATE_DISABLE:
4399b843c749SSergey Zigachev 		mec_int_cntl = RREG32(mec_int_cntl_reg);
4400b843c749SSergey Zigachev 		mec_int_cntl = REG_SET_FIELD(mec_int_cntl, CP_ME1_PIPE0_INT_CNTL,
4401b843c749SSergey Zigachev 					     TIME_STAMP_INT_ENABLE, 0);
4402b843c749SSergey Zigachev 		WREG32(mec_int_cntl_reg, mec_int_cntl);
4403b843c749SSergey Zigachev 		break;
4404b843c749SSergey Zigachev 	case AMDGPU_IRQ_STATE_ENABLE:
4405b843c749SSergey Zigachev 		mec_int_cntl = RREG32(mec_int_cntl_reg);
4406b843c749SSergey Zigachev 		mec_int_cntl = REG_SET_FIELD(mec_int_cntl, CP_ME1_PIPE0_INT_CNTL,
4407b843c749SSergey Zigachev 					     TIME_STAMP_INT_ENABLE, 1);
4408b843c749SSergey Zigachev 		WREG32(mec_int_cntl_reg, mec_int_cntl);
4409b843c749SSergey Zigachev 		break;
4410b843c749SSergey Zigachev 	default:
4411b843c749SSergey Zigachev 		break;
4412b843c749SSergey Zigachev 	}
4413b843c749SSergey Zigachev }
4414b843c749SSergey Zigachev 
gfx_v9_0_set_priv_reg_fault_state(struct amdgpu_device * adev,struct amdgpu_irq_src * source,unsigned type,enum amdgpu_interrupt_state state)4415b843c749SSergey Zigachev static int gfx_v9_0_set_priv_reg_fault_state(struct amdgpu_device *adev,
4416b843c749SSergey Zigachev 					     struct amdgpu_irq_src *source,
4417b843c749SSergey Zigachev 					     unsigned type,
4418b843c749SSergey Zigachev 					     enum amdgpu_interrupt_state state)
4419b843c749SSergey Zigachev {
4420b843c749SSergey Zigachev 	switch (state) {
4421b843c749SSergey Zigachev 	case AMDGPU_IRQ_STATE_DISABLE:
4422b843c749SSergey Zigachev 	case AMDGPU_IRQ_STATE_ENABLE:
4423b843c749SSergey Zigachev 		WREG32_FIELD15(GC, 0, CP_INT_CNTL_RING0,
4424b843c749SSergey Zigachev 			       PRIV_REG_INT_ENABLE,
4425b843c749SSergey Zigachev 			       state == AMDGPU_IRQ_STATE_ENABLE ? 1 : 0);
4426b843c749SSergey Zigachev 		break;
4427b843c749SSergey Zigachev 	default:
4428b843c749SSergey Zigachev 		break;
4429b843c749SSergey Zigachev 	}
4430b843c749SSergey Zigachev 
4431b843c749SSergey Zigachev 	return 0;
4432b843c749SSergey Zigachev }
4433b843c749SSergey Zigachev 
gfx_v9_0_set_priv_inst_fault_state(struct amdgpu_device * adev,struct amdgpu_irq_src * source,unsigned type,enum amdgpu_interrupt_state state)4434b843c749SSergey Zigachev static int gfx_v9_0_set_priv_inst_fault_state(struct amdgpu_device *adev,
4435b843c749SSergey Zigachev 					      struct amdgpu_irq_src *source,
4436b843c749SSergey Zigachev 					      unsigned type,
4437b843c749SSergey Zigachev 					      enum amdgpu_interrupt_state state)
4438b843c749SSergey Zigachev {
4439b843c749SSergey Zigachev 	switch (state) {
4440b843c749SSergey Zigachev 	case AMDGPU_IRQ_STATE_DISABLE:
4441b843c749SSergey Zigachev 	case AMDGPU_IRQ_STATE_ENABLE:
4442b843c749SSergey Zigachev 		WREG32_FIELD15(GC, 0, CP_INT_CNTL_RING0,
4443b843c749SSergey Zigachev 			       PRIV_INSTR_INT_ENABLE,
4444b843c749SSergey Zigachev 			       state == AMDGPU_IRQ_STATE_ENABLE ? 1 : 0);
4445b843c749SSergey Zigachev 	default:
4446b843c749SSergey Zigachev 		break;
4447b843c749SSergey Zigachev 	}
4448b843c749SSergey Zigachev 
4449b843c749SSergey Zigachev 	return 0;
4450b843c749SSergey Zigachev }
4451b843c749SSergey Zigachev 
gfx_v9_0_set_eop_interrupt_state(struct amdgpu_device * adev,struct amdgpu_irq_src * src,unsigned type,enum amdgpu_interrupt_state state)4452b843c749SSergey Zigachev static int gfx_v9_0_set_eop_interrupt_state(struct amdgpu_device *adev,
4453b843c749SSergey Zigachev 					    struct amdgpu_irq_src *src,
4454b843c749SSergey Zigachev 					    unsigned type,
4455b843c749SSergey Zigachev 					    enum amdgpu_interrupt_state state)
4456b843c749SSergey Zigachev {
4457b843c749SSergey Zigachev 	switch (type) {
4458b843c749SSergey Zigachev 	case AMDGPU_CP_IRQ_GFX_EOP:
4459b843c749SSergey Zigachev 		gfx_v9_0_set_gfx_eop_interrupt_state(adev, state);
4460b843c749SSergey Zigachev 		break;
4461b843c749SSergey Zigachev 	case AMDGPU_CP_IRQ_COMPUTE_MEC1_PIPE0_EOP:
4462b843c749SSergey Zigachev 		gfx_v9_0_set_compute_eop_interrupt_state(adev, 1, 0, state);
4463b843c749SSergey Zigachev 		break;
4464b843c749SSergey Zigachev 	case AMDGPU_CP_IRQ_COMPUTE_MEC1_PIPE1_EOP:
4465b843c749SSergey Zigachev 		gfx_v9_0_set_compute_eop_interrupt_state(adev, 1, 1, state);
4466b843c749SSergey Zigachev 		break;
4467b843c749SSergey Zigachev 	case AMDGPU_CP_IRQ_COMPUTE_MEC1_PIPE2_EOP:
4468b843c749SSergey Zigachev 		gfx_v9_0_set_compute_eop_interrupt_state(adev, 1, 2, state);
4469b843c749SSergey Zigachev 		break;
4470b843c749SSergey Zigachev 	case AMDGPU_CP_IRQ_COMPUTE_MEC1_PIPE3_EOP:
4471b843c749SSergey Zigachev 		gfx_v9_0_set_compute_eop_interrupt_state(adev, 1, 3, state);
4472b843c749SSergey Zigachev 		break;
4473b843c749SSergey Zigachev 	case AMDGPU_CP_IRQ_COMPUTE_MEC2_PIPE0_EOP:
4474b843c749SSergey Zigachev 		gfx_v9_0_set_compute_eop_interrupt_state(adev, 2, 0, state);
4475b843c749SSergey Zigachev 		break;
4476b843c749SSergey Zigachev 	case AMDGPU_CP_IRQ_COMPUTE_MEC2_PIPE1_EOP:
4477b843c749SSergey Zigachev 		gfx_v9_0_set_compute_eop_interrupt_state(adev, 2, 1, state);
4478b843c749SSergey Zigachev 		break;
4479b843c749SSergey Zigachev 	case AMDGPU_CP_IRQ_COMPUTE_MEC2_PIPE2_EOP:
4480b843c749SSergey Zigachev 		gfx_v9_0_set_compute_eop_interrupt_state(adev, 2, 2, state);
4481b843c749SSergey Zigachev 		break;
4482b843c749SSergey Zigachev 	case AMDGPU_CP_IRQ_COMPUTE_MEC2_PIPE3_EOP:
4483b843c749SSergey Zigachev 		gfx_v9_0_set_compute_eop_interrupt_state(adev, 2, 3, state);
4484b843c749SSergey Zigachev 		break;
4485b843c749SSergey Zigachev 	default:
4486b843c749SSergey Zigachev 		break;
4487b843c749SSergey Zigachev 	}
4488b843c749SSergey Zigachev 	return 0;
4489b843c749SSergey Zigachev }
4490b843c749SSergey Zigachev 
gfx_v9_0_eop_irq(struct amdgpu_device * adev,struct amdgpu_irq_src * source,struct amdgpu_iv_entry * entry)4491b843c749SSergey Zigachev static int gfx_v9_0_eop_irq(struct amdgpu_device *adev,
4492b843c749SSergey Zigachev 			    struct amdgpu_irq_src *source,
4493b843c749SSergey Zigachev 			    struct amdgpu_iv_entry *entry)
4494b843c749SSergey Zigachev {
4495b843c749SSergey Zigachev 	int i;
4496b843c749SSergey Zigachev 	u8 me_id, pipe_id, queue_id;
4497b843c749SSergey Zigachev 	struct amdgpu_ring *ring;
4498b843c749SSergey Zigachev 
4499b843c749SSergey Zigachev 	DRM_DEBUG("IH: CP EOP\n");
4500b843c749SSergey Zigachev 	me_id = (entry->ring_id & 0x0c) >> 2;
4501b843c749SSergey Zigachev 	pipe_id = (entry->ring_id & 0x03) >> 0;
4502b843c749SSergey Zigachev 	queue_id = (entry->ring_id & 0x70) >> 4;
4503b843c749SSergey Zigachev 
4504b843c749SSergey Zigachev 	switch (me_id) {
4505b843c749SSergey Zigachev 	case 0:
4506b843c749SSergey Zigachev 		amdgpu_fence_process(&adev->gfx.gfx_ring[0]);
4507b843c749SSergey Zigachev 		break;
4508b843c749SSergey Zigachev 	case 1:
4509b843c749SSergey Zigachev 	case 2:
4510b843c749SSergey Zigachev 		for (i = 0; i < adev->gfx.num_compute_rings; i++) {
4511b843c749SSergey Zigachev 			ring = &adev->gfx.compute_ring[i];
4512b843c749SSergey Zigachev 			/* Per-queue interrupt is supported for MEC starting from VI.
4513b843c749SSergey Zigachev 			  * The interrupt can only be enabled/disabled per pipe instead of per queue.
4514b843c749SSergey Zigachev 			  */
4515b843c749SSergey Zigachev 			if ((ring->me == me_id) && (ring->pipe == pipe_id) && (ring->queue == queue_id))
4516b843c749SSergey Zigachev 				amdgpu_fence_process(ring);
4517b843c749SSergey Zigachev 		}
4518b843c749SSergey Zigachev 		break;
4519b843c749SSergey Zigachev 	}
4520b843c749SSergey Zigachev 	return 0;
4521b843c749SSergey Zigachev }
4522b843c749SSergey Zigachev 
gfx_v9_0_priv_reg_irq(struct amdgpu_device * adev,struct amdgpu_irq_src * source,struct amdgpu_iv_entry * entry)4523b843c749SSergey Zigachev static int gfx_v9_0_priv_reg_irq(struct amdgpu_device *adev,
4524b843c749SSergey Zigachev 				 struct amdgpu_irq_src *source,
4525b843c749SSergey Zigachev 				 struct amdgpu_iv_entry *entry)
4526b843c749SSergey Zigachev {
4527b843c749SSergey Zigachev 	DRM_ERROR("Illegal register access in command stream\n");
4528b843c749SSergey Zigachev 	schedule_work(&adev->reset_work);
4529b843c749SSergey Zigachev 	return 0;
4530b843c749SSergey Zigachev }
4531b843c749SSergey Zigachev 
gfx_v9_0_priv_inst_irq(struct amdgpu_device * adev,struct amdgpu_irq_src * source,struct amdgpu_iv_entry * entry)4532b843c749SSergey Zigachev static int gfx_v9_0_priv_inst_irq(struct amdgpu_device *adev,
4533b843c749SSergey Zigachev 				  struct amdgpu_irq_src *source,
4534b843c749SSergey Zigachev 				  struct amdgpu_iv_entry *entry)
4535b843c749SSergey Zigachev {
4536b843c749SSergey Zigachev 	DRM_ERROR("Illegal instruction in command stream\n");
4537b843c749SSergey Zigachev 	schedule_work(&adev->reset_work);
4538b843c749SSergey Zigachev 	return 0;
4539b843c749SSergey Zigachev }
4540b843c749SSergey Zigachev 
gfx_v9_0_kiq_set_interrupt_state(struct amdgpu_device * adev,struct amdgpu_irq_src * src,unsigned int type,enum amdgpu_interrupt_state state)4541b843c749SSergey Zigachev static int gfx_v9_0_kiq_set_interrupt_state(struct amdgpu_device *adev,
4542b843c749SSergey Zigachev 					    struct amdgpu_irq_src *src,
4543b843c749SSergey Zigachev 					    unsigned int type,
4544b843c749SSergey Zigachev 					    enum amdgpu_interrupt_state state)
4545b843c749SSergey Zigachev {
4546b843c749SSergey Zigachev 	uint32_t tmp, target;
4547b843c749SSergey Zigachev 	struct amdgpu_ring *ring = &(adev->gfx.kiq.ring);
4548b843c749SSergey Zigachev 
4549b843c749SSergey Zigachev 	if (ring->me == 1)
4550b843c749SSergey Zigachev 		target = SOC15_REG_OFFSET(GC, 0, mmCP_ME1_PIPE0_INT_CNTL);
4551b843c749SSergey Zigachev 	else
4552b843c749SSergey Zigachev 		target = SOC15_REG_OFFSET(GC, 0, mmCP_ME2_PIPE0_INT_CNTL);
4553b843c749SSergey Zigachev 	target += ring->pipe;
4554b843c749SSergey Zigachev 
4555b843c749SSergey Zigachev 	switch (type) {
4556b843c749SSergey Zigachev 	case AMDGPU_CP_KIQ_IRQ_DRIVER0:
4557b843c749SSergey Zigachev 		if (state == AMDGPU_IRQ_STATE_DISABLE) {
4558b843c749SSergey Zigachev 			tmp = RREG32_SOC15(GC, 0, mmCPC_INT_CNTL);
4559b843c749SSergey Zigachev 			tmp = REG_SET_FIELD(tmp, CPC_INT_CNTL,
4560b843c749SSergey Zigachev 						 GENERIC2_INT_ENABLE, 0);
4561b843c749SSergey Zigachev 			WREG32_SOC15(GC, 0, mmCPC_INT_CNTL, tmp);
4562b843c749SSergey Zigachev 
4563b843c749SSergey Zigachev 			tmp = RREG32(target);
4564b843c749SSergey Zigachev 			tmp = REG_SET_FIELD(tmp, CP_ME2_PIPE0_INT_CNTL,
4565b843c749SSergey Zigachev 						 GENERIC2_INT_ENABLE, 0);
4566b843c749SSergey Zigachev 			WREG32(target, tmp);
4567b843c749SSergey Zigachev 		} else {
4568b843c749SSergey Zigachev 			tmp = RREG32_SOC15(GC, 0, mmCPC_INT_CNTL);
4569b843c749SSergey Zigachev 			tmp = REG_SET_FIELD(tmp, CPC_INT_CNTL,
4570b843c749SSergey Zigachev 						 GENERIC2_INT_ENABLE, 1);
4571b843c749SSergey Zigachev 			WREG32_SOC15(GC, 0, mmCPC_INT_CNTL, tmp);
4572b843c749SSergey Zigachev 
4573b843c749SSergey Zigachev 			tmp = RREG32(target);
4574b843c749SSergey Zigachev 			tmp = REG_SET_FIELD(tmp, CP_ME2_PIPE0_INT_CNTL,
4575b843c749SSergey Zigachev 						 GENERIC2_INT_ENABLE, 1);
4576b843c749SSergey Zigachev 			WREG32(target, tmp);
4577b843c749SSergey Zigachev 		}
4578b843c749SSergey Zigachev 		break;
4579b843c749SSergey Zigachev 	default:
4580b843c749SSergey Zigachev 		BUG(); /* kiq only support GENERIC2_INT now */
4581b843c749SSergey Zigachev 		break;
4582b843c749SSergey Zigachev 	}
4583b843c749SSergey Zigachev 	return 0;
4584b843c749SSergey Zigachev }
4585b843c749SSergey Zigachev 
gfx_v9_0_kiq_irq(struct amdgpu_device * adev,struct amdgpu_irq_src * source,struct amdgpu_iv_entry * entry)4586b843c749SSergey Zigachev static int gfx_v9_0_kiq_irq(struct amdgpu_device *adev,
4587b843c749SSergey Zigachev 			    struct amdgpu_irq_src *source,
4588b843c749SSergey Zigachev 			    struct amdgpu_iv_entry *entry)
4589b843c749SSergey Zigachev {
4590b843c749SSergey Zigachev 	u8 me_id, pipe_id, queue_id;
4591b843c749SSergey Zigachev 	struct amdgpu_ring *ring = &(adev->gfx.kiq.ring);
4592b843c749SSergey Zigachev 
4593b843c749SSergey Zigachev 	me_id = (entry->ring_id & 0x0c) >> 2;
4594b843c749SSergey Zigachev 	pipe_id = (entry->ring_id & 0x03) >> 0;
4595b843c749SSergey Zigachev 	queue_id = (entry->ring_id & 0x70) >> 4;
4596b843c749SSergey Zigachev 	DRM_DEBUG("IH: CPC GENERIC2_INT, me:%d, pipe:%d, queue:%d\n",
4597b843c749SSergey Zigachev 		   me_id, pipe_id, queue_id);
4598b843c749SSergey Zigachev 
4599b843c749SSergey Zigachev 	amdgpu_fence_process(ring);
4600b843c749SSergey Zigachev 	return 0;
4601b843c749SSergey Zigachev }
4602b843c749SSergey Zigachev 
4603b843c749SSergey Zigachev static const struct amd_ip_funcs gfx_v9_0_ip_funcs = {
4604b843c749SSergey Zigachev 	.name = "gfx_v9_0",
4605b843c749SSergey Zigachev 	.early_init = gfx_v9_0_early_init,
4606b843c749SSergey Zigachev 	.late_init = gfx_v9_0_late_init,
4607b843c749SSergey Zigachev 	.sw_init = gfx_v9_0_sw_init,
4608b843c749SSergey Zigachev 	.sw_fini = gfx_v9_0_sw_fini,
4609b843c749SSergey Zigachev 	.hw_init = gfx_v9_0_hw_init,
4610b843c749SSergey Zigachev 	.hw_fini = gfx_v9_0_hw_fini,
4611b843c749SSergey Zigachev 	.suspend = gfx_v9_0_suspend,
4612b843c749SSergey Zigachev 	.resume = gfx_v9_0_resume,
4613b843c749SSergey Zigachev 	.is_idle = gfx_v9_0_is_idle,
4614b843c749SSergey Zigachev 	.wait_for_idle = gfx_v9_0_wait_for_idle,
4615b843c749SSergey Zigachev 	.soft_reset = gfx_v9_0_soft_reset,
4616b843c749SSergey Zigachev 	.set_clockgating_state = gfx_v9_0_set_clockgating_state,
4617b843c749SSergey Zigachev 	.set_powergating_state = gfx_v9_0_set_powergating_state,
4618b843c749SSergey Zigachev 	.get_clockgating_state = gfx_v9_0_get_clockgating_state,
4619b843c749SSergey Zigachev };
4620b843c749SSergey Zigachev 
4621b843c749SSergey Zigachev static const struct amdgpu_ring_funcs gfx_v9_0_ring_funcs_gfx = {
4622b843c749SSergey Zigachev 	.type = AMDGPU_RING_TYPE_GFX,
4623b843c749SSergey Zigachev 	.align_mask = 0xff,
4624b843c749SSergey Zigachev 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
4625b843c749SSergey Zigachev 	.support_64bit_ptrs = true,
4626b843c749SSergey Zigachev 	.vmhub = AMDGPU_GFXHUB,
4627b843c749SSergey Zigachev 	.get_rptr = gfx_v9_0_ring_get_rptr_gfx,
4628b843c749SSergey Zigachev 	.get_wptr = gfx_v9_0_ring_get_wptr_gfx,
4629b843c749SSergey Zigachev 	.set_wptr = gfx_v9_0_ring_set_wptr_gfx,
4630b843c749SSergey Zigachev 	.emit_frame_size = /* totally 242 maximum if 16 IBs */
4631b843c749SSergey Zigachev 		5 +  /* COND_EXEC */
4632b843c749SSergey Zigachev 		7 +  /* PIPELINE_SYNC */
4633b843c749SSergey Zigachev 		SOC15_FLUSH_GPU_TLB_NUM_WREG * 5 +
4634b843c749SSergey Zigachev 		SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 7 +
4635b843c749SSergey Zigachev 		2 + /* VM_FLUSH */
4636b843c749SSergey Zigachev 		8 +  /* FENCE for VM_FLUSH */
4637b843c749SSergey Zigachev 		20 + /* GDS switch */
4638b843c749SSergey Zigachev 		4 + /* double SWITCH_BUFFER,
4639b843c749SSergey Zigachev 		       the first COND_EXEC jump to the place just
4640b843c749SSergey Zigachev 			   prior to this double SWITCH_BUFFER  */
4641b843c749SSergey Zigachev 		5 + /* COND_EXEC */
4642b843c749SSergey Zigachev 		7 +	 /*	HDP_flush */
4643b843c749SSergey Zigachev 		4 +	 /*	VGT_flush */
4644b843c749SSergey Zigachev 		14 + /*	CE_META */
4645b843c749SSergey Zigachev 		31 + /*	DE_META */
4646b843c749SSergey Zigachev 		3 + /* CNTX_CTRL */
4647b843c749SSergey Zigachev 		5 + /* HDP_INVL */
4648b843c749SSergey Zigachev 		8 + 8 + /* FENCE x2 */
4649b843c749SSergey Zigachev 		2, /* SWITCH_BUFFER */
4650b843c749SSergey Zigachev 	.emit_ib_size =	4, /* gfx_v9_0_ring_emit_ib_gfx */
4651b843c749SSergey Zigachev 	.emit_ib = gfx_v9_0_ring_emit_ib_gfx,
4652b843c749SSergey Zigachev 	.emit_fence = gfx_v9_0_ring_emit_fence,
4653b843c749SSergey Zigachev 	.emit_pipeline_sync = gfx_v9_0_ring_emit_pipeline_sync,
4654b843c749SSergey Zigachev 	.emit_vm_flush = gfx_v9_0_ring_emit_vm_flush,
4655b843c749SSergey Zigachev 	.emit_gds_switch = gfx_v9_0_ring_emit_gds_switch,
4656b843c749SSergey Zigachev 	.emit_hdp_flush = gfx_v9_0_ring_emit_hdp_flush,
4657b843c749SSergey Zigachev 	.test_ring = gfx_v9_0_ring_test_ring,
4658b843c749SSergey Zigachev 	.test_ib = gfx_v9_0_ring_test_ib,
4659b843c749SSergey Zigachev 	.insert_nop = amdgpu_ring_insert_nop,
4660b843c749SSergey Zigachev 	.pad_ib = amdgpu_ring_generic_pad_ib,
4661b843c749SSergey Zigachev 	.emit_switch_buffer = gfx_v9_ring_emit_sb,
4662b843c749SSergey Zigachev 	.emit_cntxcntl = gfx_v9_ring_emit_cntxcntl,
4663b843c749SSergey Zigachev 	.init_cond_exec = gfx_v9_0_ring_emit_init_cond_exec,
4664b843c749SSergey Zigachev 	.patch_cond_exec = gfx_v9_0_ring_emit_patch_cond_exec,
4665b843c749SSergey Zigachev 	.emit_tmz = gfx_v9_0_ring_emit_tmz,
4666b843c749SSergey Zigachev 	.emit_wreg = gfx_v9_0_ring_emit_wreg,
4667b843c749SSergey Zigachev 	.emit_reg_wait = gfx_v9_0_ring_emit_reg_wait,
4668b843c749SSergey Zigachev 	.emit_reg_write_reg_wait = gfx_v9_0_ring_emit_reg_write_reg_wait,
4669b843c749SSergey Zigachev };
4670b843c749SSergey Zigachev 
4671b843c749SSergey Zigachev static const struct amdgpu_ring_funcs gfx_v9_0_ring_funcs_compute = {
4672b843c749SSergey Zigachev 	.type = AMDGPU_RING_TYPE_COMPUTE,
4673b843c749SSergey Zigachev 	.align_mask = 0xff,
4674b843c749SSergey Zigachev 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
4675b843c749SSergey Zigachev 	.support_64bit_ptrs = true,
4676b843c749SSergey Zigachev 	.vmhub = AMDGPU_GFXHUB,
4677b843c749SSergey Zigachev 	.get_rptr = gfx_v9_0_ring_get_rptr_compute,
4678b843c749SSergey Zigachev 	.get_wptr = gfx_v9_0_ring_get_wptr_compute,
4679b843c749SSergey Zigachev 	.set_wptr = gfx_v9_0_ring_set_wptr_compute,
4680b843c749SSergey Zigachev 	.emit_frame_size =
4681b843c749SSergey Zigachev 		20 + /* gfx_v9_0_ring_emit_gds_switch */
4682b843c749SSergey Zigachev 		7 + /* gfx_v9_0_ring_emit_hdp_flush */
4683b843c749SSergey Zigachev 		5 + /* hdp invalidate */
4684b843c749SSergey Zigachev 		7 + /* gfx_v9_0_ring_emit_pipeline_sync */
4685b843c749SSergey Zigachev 		SOC15_FLUSH_GPU_TLB_NUM_WREG * 5 +
4686b843c749SSergey Zigachev 		SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 7 +
4687b843c749SSergey Zigachev 		2 + /* gfx_v9_0_ring_emit_vm_flush */
4688b843c749SSergey Zigachev 		8 + 8 + 8, /* gfx_v9_0_ring_emit_fence x3 for user fence, vm fence */
4689b843c749SSergey Zigachev 	.emit_ib_size =	4, /* gfx_v9_0_ring_emit_ib_compute */
4690b843c749SSergey Zigachev 	.emit_ib = gfx_v9_0_ring_emit_ib_compute,
4691b843c749SSergey Zigachev 	.emit_fence = gfx_v9_0_ring_emit_fence,
4692b843c749SSergey Zigachev 	.emit_pipeline_sync = gfx_v9_0_ring_emit_pipeline_sync,
4693b843c749SSergey Zigachev 	.emit_vm_flush = gfx_v9_0_ring_emit_vm_flush,
4694b843c749SSergey Zigachev 	.emit_gds_switch = gfx_v9_0_ring_emit_gds_switch,
4695b843c749SSergey Zigachev 	.emit_hdp_flush = gfx_v9_0_ring_emit_hdp_flush,
4696b843c749SSergey Zigachev 	.test_ring = gfx_v9_0_ring_test_ring,
4697b843c749SSergey Zigachev 	.test_ib = gfx_v9_0_ring_test_ib,
4698b843c749SSergey Zigachev 	.insert_nop = amdgpu_ring_insert_nop,
4699b843c749SSergey Zigachev 	.pad_ib = amdgpu_ring_generic_pad_ib,
4700b843c749SSergey Zigachev 	.set_priority = gfx_v9_0_ring_set_priority_compute,
4701b843c749SSergey Zigachev 	.emit_wreg = gfx_v9_0_ring_emit_wreg,
4702b843c749SSergey Zigachev 	.emit_reg_wait = gfx_v9_0_ring_emit_reg_wait,
4703b843c749SSergey Zigachev 	.emit_reg_write_reg_wait = gfx_v9_0_ring_emit_reg_write_reg_wait,
4704b843c749SSergey Zigachev };
4705b843c749SSergey Zigachev 
4706b843c749SSergey Zigachev static const struct amdgpu_ring_funcs gfx_v9_0_ring_funcs_kiq = {
4707b843c749SSergey Zigachev 	.type = AMDGPU_RING_TYPE_KIQ,
4708b843c749SSergey Zigachev 	.align_mask = 0xff,
4709b843c749SSergey Zigachev 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
4710b843c749SSergey Zigachev 	.support_64bit_ptrs = true,
4711b843c749SSergey Zigachev 	.vmhub = AMDGPU_GFXHUB,
4712b843c749SSergey Zigachev 	.get_rptr = gfx_v9_0_ring_get_rptr_compute,
4713b843c749SSergey Zigachev 	.get_wptr = gfx_v9_0_ring_get_wptr_compute,
4714b843c749SSergey Zigachev 	.set_wptr = gfx_v9_0_ring_set_wptr_compute,
4715b843c749SSergey Zigachev 	.emit_frame_size =
4716b843c749SSergey Zigachev 		20 + /* gfx_v9_0_ring_emit_gds_switch */
4717b843c749SSergey Zigachev 		7 + /* gfx_v9_0_ring_emit_hdp_flush */
4718b843c749SSergey Zigachev 		5 + /* hdp invalidate */
4719b843c749SSergey Zigachev 		7 + /* gfx_v9_0_ring_emit_pipeline_sync */
4720b843c749SSergey Zigachev 		SOC15_FLUSH_GPU_TLB_NUM_WREG * 5 +
4721b843c749SSergey Zigachev 		SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 7 +
4722b843c749SSergey Zigachev 		2 + /* gfx_v9_0_ring_emit_vm_flush */
4723b843c749SSergey Zigachev 		8 + 8 + 8, /* gfx_v9_0_ring_emit_fence_kiq x3 for user fence, vm fence */
4724b843c749SSergey Zigachev 	.emit_ib_size =	4, /* gfx_v9_0_ring_emit_ib_compute */
4725b843c749SSergey Zigachev 	.emit_ib = gfx_v9_0_ring_emit_ib_compute,
4726b843c749SSergey Zigachev 	.emit_fence = gfx_v9_0_ring_emit_fence_kiq,
4727b843c749SSergey Zigachev 	.test_ring = gfx_v9_0_ring_test_ring,
4728b843c749SSergey Zigachev 	.test_ib = gfx_v9_0_ring_test_ib,
4729b843c749SSergey Zigachev 	.insert_nop = amdgpu_ring_insert_nop,
4730b843c749SSergey Zigachev 	.pad_ib = amdgpu_ring_generic_pad_ib,
4731b843c749SSergey Zigachev 	.emit_rreg = gfx_v9_0_ring_emit_rreg,
4732b843c749SSergey Zigachev 	.emit_wreg = gfx_v9_0_ring_emit_wreg,
4733b843c749SSergey Zigachev 	.emit_reg_wait = gfx_v9_0_ring_emit_reg_wait,
4734b843c749SSergey Zigachev 	.emit_reg_write_reg_wait = gfx_v9_0_ring_emit_reg_write_reg_wait,
4735b843c749SSergey Zigachev };
4736b843c749SSergey Zigachev 
gfx_v9_0_set_ring_funcs(struct amdgpu_device * adev)4737b843c749SSergey Zigachev static void gfx_v9_0_set_ring_funcs(struct amdgpu_device *adev)
4738b843c749SSergey Zigachev {
4739b843c749SSergey Zigachev 	int i;
4740b843c749SSergey Zigachev 
4741b843c749SSergey Zigachev 	adev->gfx.kiq.ring.funcs = &gfx_v9_0_ring_funcs_kiq;
4742b843c749SSergey Zigachev 
4743b843c749SSergey Zigachev 	for (i = 0; i < adev->gfx.num_gfx_rings; i++)
4744b843c749SSergey Zigachev 		adev->gfx.gfx_ring[i].funcs = &gfx_v9_0_ring_funcs_gfx;
4745b843c749SSergey Zigachev 
4746b843c749SSergey Zigachev 	for (i = 0; i < adev->gfx.num_compute_rings; i++)
4747b843c749SSergey Zigachev 		adev->gfx.compute_ring[i].funcs = &gfx_v9_0_ring_funcs_compute;
4748b843c749SSergey Zigachev }
4749b843c749SSergey Zigachev 
4750b843c749SSergey Zigachev static const struct amdgpu_irq_src_funcs gfx_v9_0_kiq_irq_funcs = {
4751b843c749SSergey Zigachev 	.set = gfx_v9_0_kiq_set_interrupt_state,
4752b843c749SSergey Zigachev 	.process = gfx_v9_0_kiq_irq,
4753b843c749SSergey Zigachev };
4754b843c749SSergey Zigachev 
4755b843c749SSergey Zigachev static const struct amdgpu_irq_src_funcs gfx_v9_0_eop_irq_funcs = {
4756b843c749SSergey Zigachev 	.set = gfx_v9_0_set_eop_interrupt_state,
4757b843c749SSergey Zigachev 	.process = gfx_v9_0_eop_irq,
4758b843c749SSergey Zigachev };
4759b843c749SSergey Zigachev 
4760b843c749SSergey Zigachev static const struct amdgpu_irq_src_funcs gfx_v9_0_priv_reg_irq_funcs = {
4761b843c749SSergey Zigachev 	.set = gfx_v9_0_set_priv_reg_fault_state,
4762b843c749SSergey Zigachev 	.process = gfx_v9_0_priv_reg_irq,
4763b843c749SSergey Zigachev };
4764b843c749SSergey Zigachev 
4765b843c749SSergey Zigachev static const struct amdgpu_irq_src_funcs gfx_v9_0_priv_inst_irq_funcs = {
4766b843c749SSergey Zigachev 	.set = gfx_v9_0_set_priv_inst_fault_state,
4767b843c749SSergey Zigachev 	.process = gfx_v9_0_priv_inst_irq,
4768b843c749SSergey Zigachev };
4769b843c749SSergey Zigachev 
gfx_v9_0_set_irq_funcs(struct amdgpu_device * adev)4770b843c749SSergey Zigachev static void gfx_v9_0_set_irq_funcs(struct amdgpu_device *adev)
4771b843c749SSergey Zigachev {
4772b843c749SSergey Zigachev 	adev->gfx.eop_irq.num_types = AMDGPU_CP_IRQ_LAST;
4773b843c749SSergey Zigachev 	adev->gfx.eop_irq.funcs = &gfx_v9_0_eop_irq_funcs;
4774b843c749SSergey Zigachev 
4775b843c749SSergey Zigachev 	adev->gfx.priv_reg_irq.num_types = 1;
4776b843c749SSergey Zigachev 	adev->gfx.priv_reg_irq.funcs = &gfx_v9_0_priv_reg_irq_funcs;
4777b843c749SSergey Zigachev 
4778b843c749SSergey Zigachev 	adev->gfx.priv_inst_irq.num_types = 1;
4779b843c749SSergey Zigachev 	adev->gfx.priv_inst_irq.funcs = &gfx_v9_0_priv_inst_irq_funcs;
4780b843c749SSergey Zigachev 
4781b843c749SSergey Zigachev 	adev->gfx.kiq.irq.num_types = AMDGPU_CP_KIQ_IRQ_LAST;
4782b843c749SSergey Zigachev 	adev->gfx.kiq.irq.funcs = &gfx_v9_0_kiq_irq_funcs;
4783b843c749SSergey Zigachev }
4784b843c749SSergey Zigachev 
gfx_v9_0_set_rlc_funcs(struct amdgpu_device * adev)4785b843c749SSergey Zigachev static void gfx_v9_0_set_rlc_funcs(struct amdgpu_device *adev)
4786b843c749SSergey Zigachev {
4787b843c749SSergey Zigachev 	switch (adev->asic_type) {
4788b843c749SSergey Zigachev 	case CHIP_VEGA10:
4789b843c749SSergey Zigachev 	case CHIP_VEGA12:
4790b843c749SSergey Zigachev 	case CHIP_VEGA20:
4791b843c749SSergey Zigachev 	case CHIP_RAVEN:
4792b843c749SSergey Zigachev 		adev->gfx.rlc.funcs = &gfx_v9_0_rlc_funcs;
4793b843c749SSergey Zigachev 		break;
4794b843c749SSergey Zigachev 	default:
4795b843c749SSergey Zigachev 		break;
4796b843c749SSergey Zigachev 	}
4797b843c749SSergey Zigachev }
4798b843c749SSergey Zigachev 
gfx_v9_0_set_gds_init(struct amdgpu_device * adev)4799b843c749SSergey Zigachev static void gfx_v9_0_set_gds_init(struct amdgpu_device *adev)
4800b843c749SSergey Zigachev {
4801b843c749SSergey Zigachev 	/* init asci gds info */
4802b843c749SSergey Zigachev 	adev->gds.mem.total_size = RREG32_SOC15(GC, 0, mmGDS_VMID0_SIZE);
4803b843c749SSergey Zigachev 	adev->gds.gws.total_size = 64;
4804b843c749SSergey Zigachev 	adev->gds.oa.total_size = 16;
4805b843c749SSergey Zigachev 
4806b843c749SSergey Zigachev 	if (adev->gds.mem.total_size == 64 * 1024) {
4807b843c749SSergey Zigachev 		adev->gds.mem.gfx_partition_size = 4096;
4808b843c749SSergey Zigachev 		adev->gds.mem.cs_partition_size = 4096;
4809b843c749SSergey Zigachev 
4810b843c749SSergey Zigachev 		adev->gds.gws.gfx_partition_size = 4;
4811b843c749SSergey Zigachev 		adev->gds.gws.cs_partition_size = 4;
4812b843c749SSergey Zigachev 
4813b843c749SSergey Zigachev 		adev->gds.oa.gfx_partition_size = 4;
4814b843c749SSergey Zigachev 		adev->gds.oa.cs_partition_size = 1;
4815b843c749SSergey Zigachev 	} else {
4816b843c749SSergey Zigachev 		adev->gds.mem.gfx_partition_size = 1024;
4817b843c749SSergey Zigachev 		adev->gds.mem.cs_partition_size = 1024;
4818b843c749SSergey Zigachev 
4819b843c749SSergey Zigachev 		adev->gds.gws.gfx_partition_size = 16;
4820b843c749SSergey Zigachev 		adev->gds.gws.cs_partition_size = 16;
4821b843c749SSergey Zigachev 
4822b843c749SSergey Zigachev 		adev->gds.oa.gfx_partition_size = 4;
4823b843c749SSergey Zigachev 		adev->gds.oa.cs_partition_size = 4;
4824b843c749SSergey Zigachev 	}
4825b843c749SSergey Zigachev }
4826b843c749SSergey Zigachev 
gfx_v9_0_set_user_cu_inactive_bitmap(struct amdgpu_device * adev,u32 bitmap)4827b843c749SSergey Zigachev static void gfx_v9_0_set_user_cu_inactive_bitmap(struct amdgpu_device *adev,
4828b843c749SSergey Zigachev 						 u32 bitmap)
4829b843c749SSergey Zigachev {
4830b843c749SSergey Zigachev 	u32 data;
4831b843c749SSergey Zigachev 
4832b843c749SSergey Zigachev 	if (!bitmap)
4833b843c749SSergey Zigachev 		return;
4834b843c749SSergey Zigachev 
4835b843c749SSergey Zigachev 	data = bitmap << GC_USER_SHADER_ARRAY_CONFIG__INACTIVE_CUS__SHIFT;
4836b843c749SSergey Zigachev 	data &= GC_USER_SHADER_ARRAY_CONFIG__INACTIVE_CUS_MASK;
4837b843c749SSergey Zigachev 
4838b843c749SSergey Zigachev 	WREG32_SOC15(GC, 0, mmGC_USER_SHADER_ARRAY_CONFIG, data);
4839b843c749SSergey Zigachev }
4840b843c749SSergey Zigachev 
gfx_v9_0_get_cu_active_bitmap(struct amdgpu_device * adev)4841b843c749SSergey Zigachev static u32 gfx_v9_0_get_cu_active_bitmap(struct amdgpu_device *adev)
4842b843c749SSergey Zigachev {
4843b843c749SSergey Zigachev 	u32 data, mask;
4844b843c749SSergey Zigachev 
4845b843c749SSergey Zigachev 	data = RREG32_SOC15(GC, 0, mmCC_GC_SHADER_ARRAY_CONFIG);
4846b843c749SSergey Zigachev 	data |= RREG32_SOC15(GC, 0, mmGC_USER_SHADER_ARRAY_CONFIG);
4847b843c749SSergey Zigachev 
4848b843c749SSergey Zigachev 	data &= CC_GC_SHADER_ARRAY_CONFIG__INACTIVE_CUS_MASK;
4849b843c749SSergey Zigachev 	data >>= CC_GC_SHADER_ARRAY_CONFIG__INACTIVE_CUS__SHIFT;
4850b843c749SSergey Zigachev 
4851b843c749SSergey Zigachev 	mask = amdgpu_gfx_create_bitmask(adev->gfx.config.max_cu_per_sh);
4852b843c749SSergey Zigachev 
4853b843c749SSergey Zigachev 	return (~data) & mask;
4854b843c749SSergey Zigachev }
4855b843c749SSergey Zigachev 
gfx_v9_0_get_cu_info(struct amdgpu_device * adev,struct amdgpu_cu_info * cu_info)4856b843c749SSergey Zigachev static int gfx_v9_0_get_cu_info(struct amdgpu_device *adev,
4857b843c749SSergey Zigachev 				 struct amdgpu_cu_info *cu_info)
4858b843c749SSergey Zigachev {
4859b843c749SSergey Zigachev 	int i, j, k, counter, active_cu_number = 0;
4860b843c749SSergey Zigachev 	u32 mask, bitmap, ao_bitmap, ao_cu_mask = 0;
4861b843c749SSergey Zigachev 	unsigned disable_masks[4 * 2];
4862b843c749SSergey Zigachev 
4863b843c749SSergey Zigachev 	if (!adev || !cu_info)
4864b843c749SSergey Zigachev 		return -EINVAL;
4865b843c749SSergey Zigachev 
4866b843c749SSergey Zigachev 	amdgpu_gfx_parse_disable_cu(disable_masks, 4, 2);
4867b843c749SSergey Zigachev 
4868b843c749SSergey Zigachev 	mutex_lock(&adev->grbm_idx_mutex);
4869b843c749SSergey Zigachev 	for (i = 0; i < adev->gfx.config.max_shader_engines; i++) {
4870b843c749SSergey Zigachev 		for (j = 0; j < adev->gfx.config.max_sh_per_se; j++) {
4871b843c749SSergey Zigachev 			mask = 1;
4872b843c749SSergey Zigachev 			ao_bitmap = 0;
4873b843c749SSergey Zigachev 			counter = 0;
4874b843c749SSergey Zigachev 			gfx_v9_0_select_se_sh(adev, i, j, 0xffffffff);
4875b843c749SSergey Zigachev 			if (i < 4 && j < 2)
4876b843c749SSergey Zigachev 				gfx_v9_0_set_user_cu_inactive_bitmap(
4877b843c749SSergey Zigachev 					adev, disable_masks[i * 2 + j]);
4878b843c749SSergey Zigachev 			bitmap = gfx_v9_0_get_cu_active_bitmap(adev);
4879b843c749SSergey Zigachev 			cu_info->bitmap[i][j] = bitmap;
4880b843c749SSergey Zigachev 
4881b843c749SSergey Zigachev 			for (k = 0; k < adev->gfx.config.max_cu_per_sh; k ++) {
4882b843c749SSergey Zigachev 				if (bitmap & mask) {
4883b843c749SSergey Zigachev 					if (counter < adev->gfx.config.max_cu_per_sh)
4884b843c749SSergey Zigachev 						ao_bitmap |= mask;
4885b843c749SSergey Zigachev 					counter ++;
4886b843c749SSergey Zigachev 				}
4887b843c749SSergey Zigachev 				mask <<= 1;
4888b843c749SSergey Zigachev 			}
4889b843c749SSergey Zigachev 			active_cu_number += counter;
4890b843c749SSergey Zigachev 			if (i < 2 && j < 2)
4891b843c749SSergey Zigachev 				ao_cu_mask |= (ao_bitmap << (i * 16 + j * 8));
4892b843c749SSergey Zigachev 			cu_info->ao_cu_bitmap[i][j] = ao_bitmap;
4893b843c749SSergey Zigachev 		}
4894b843c749SSergey Zigachev 	}
4895b843c749SSergey Zigachev 	gfx_v9_0_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
4896b843c749SSergey Zigachev 	mutex_unlock(&adev->grbm_idx_mutex);
4897b843c749SSergey Zigachev 
4898b843c749SSergey Zigachev 	cu_info->number = active_cu_number;
4899b843c749SSergey Zigachev 	cu_info->ao_cu_mask = ao_cu_mask;
4900b843c749SSergey Zigachev 	cu_info->simd_per_cu = NUM_SIMD_PER_CU;
4901b843c749SSergey Zigachev 
4902b843c749SSergey Zigachev 	return 0;
4903b843c749SSergey Zigachev }
4904b843c749SSergey Zigachev 
4905b843c749SSergey Zigachev const struct amdgpu_ip_block_version gfx_v9_0_ip_block =
4906b843c749SSergey Zigachev {
4907b843c749SSergey Zigachev 	.type = AMD_IP_BLOCK_TYPE_GFX,
4908b843c749SSergey Zigachev 	.major = 9,
4909b843c749SSergey Zigachev 	.minor = 0,
4910b843c749SSergey Zigachev 	.rev = 0,
4911b843c749SSergey Zigachev 	.funcs = &gfx_v9_0_ip_funcs,
4912b843c749SSergey Zigachev };
4913