2 * Copyright 2016 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
23 #include <linux/kernel.h>
24 #include <linux/firmware.h>
27 #include "amdgpu_gfx.h"
30 #include "amdgpu_atomfirmware.h"
32 #include "gc/gc_9_0_offset.h"
33 #include "gc/gc_9_0_sh_mask.h"
34 #include "vega10_enum.h"
35 #include "hdp/hdp_4_0_offset.h"
37 #include "soc15_common.h"
38 #include "clearstate_gfx9.h"
39 #include "v9_structs.h"
41 #include "ivsrcid/gfx/irqsrcs_gfx_9_0.h"
43 #define GFX9_NUM_GFX_RINGS 1
44 #define GFX9_MEC_HPD_SIZE 4096
45 #define RLCG_UCODE_LOADING_START_ADDRESS 0x00002000L
46 #define RLC_SAVE_RESTORE_ADDR_STARTING_OFFSET 0x00000000L
48 #define mmPWR_MISC_CNTL_STATUS 0x0183
49 #define mmPWR_MISC_CNTL_STATUS_BASE_IDX 0
50 #define PWR_MISC_CNTL_STATUS__PWR_GFX_RLC_CGPG_EN__SHIFT 0x0
51 #define PWR_MISC_CNTL_STATUS__PWR_GFXOFF_STATUS__SHIFT 0x1
52 #define PWR_MISC_CNTL_STATUS__PWR_GFX_RLC_CGPG_EN_MASK 0x00000001L
53 #define PWR_MISC_CNTL_STATUS__PWR_GFXOFF_STATUS_MASK 0x00000006L
55 MODULE_FIRMWARE("amdgpu/vega10_ce.bin");
56 MODULE_FIRMWARE("amdgpu/vega10_pfp.bin");
57 MODULE_FIRMWARE("amdgpu/vega10_me.bin");
58 MODULE_FIRMWARE("amdgpu/vega10_mec.bin");
59 MODULE_FIRMWARE("amdgpu/vega10_mec2.bin");
60 MODULE_FIRMWARE("amdgpu/vega10_rlc.bin");
62 MODULE_FIRMWARE("amdgpu/vega12_ce.bin");
63 MODULE_FIRMWARE("amdgpu/vega12_pfp.bin");
64 MODULE_FIRMWARE("amdgpu/vega12_me.bin");
65 MODULE_FIRMWARE("amdgpu/vega12_mec.bin");
66 MODULE_FIRMWARE("amdgpu/vega12_mec2.bin");
67 MODULE_FIRMWARE("amdgpu/vega12_rlc.bin");
69 MODULE_FIRMWARE("amdgpu/vega20_ce.bin");
70 MODULE_FIRMWARE("amdgpu/vega20_pfp.bin");
71 MODULE_FIRMWARE("amdgpu/vega20_me.bin");
72 MODULE_FIRMWARE("amdgpu/vega20_mec.bin");
73 MODULE_FIRMWARE("amdgpu/vega20_mec2.bin");
74 MODULE_FIRMWARE("amdgpu/vega20_rlc.bin");
76 MODULE_FIRMWARE("amdgpu/raven_ce.bin");
77 MODULE_FIRMWARE("amdgpu/raven_pfp.bin");
78 MODULE_FIRMWARE("amdgpu/raven_me.bin");
79 MODULE_FIRMWARE("amdgpu/raven_mec.bin");
80 MODULE_FIRMWARE("amdgpu/raven_mec2.bin");
81 MODULE_FIRMWARE("amdgpu/raven_rlc.bin");
83 MODULE_FIRMWARE("amdgpu/picasso_ce.bin");
84 MODULE_FIRMWARE("amdgpu/picasso_pfp.bin");
85 MODULE_FIRMWARE("amdgpu/picasso_me.bin");
86 MODULE_FIRMWARE("amdgpu/picasso_mec.bin");
87 MODULE_FIRMWARE("amdgpu/picasso_mec2.bin");
88 MODULE_FIRMWARE("amdgpu/picasso_rlc.bin");
90 MODULE_FIRMWARE("amdgpu/raven2_ce.bin");
91 MODULE_FIRMWARE("amdgpu/raven2_pfp.bin");
92 MODULE_FIRMWARE("amdgpu/raven2_me.bin");
93 MODULE_FIRMWARE("amdgpu/raven2_mec.bin");
94 MODULE_FIRMWARE("amdgpu/raven2_mec2.bin");
95 MODULE_FIRMWARE("amdgpu/raven2_rlc.bin");
97 static const struct soc15_reg_golden golden_settings_gc_9_0[] =
99 SOC15_REG_GOLDEN_VALUE(GC, 0, mmDB_DEBUG2, 0xf00fffff, 0x00000400),
100 SOC15_REG_GOLDEN_VALUE(GC, 0, mmDB_DEBUG3, 0x80000000, 0x80000000),
101 SOC15_REG_GOLDEN_VALUE(GC, 0, mmGB_GPU_ID, 0x0000000f, 0x00000000),
102 SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_BINNER_EVENT_CNTL_3, 0x00000003, 0x82400024),
103 SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_ENHANCE, 0x3fffffff, 0x00000001),
104 SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_LINE_STIPPLE_STATE, 0x0000ff0f, 0x00000000),
105 SOC15_REG_GOLDEN_VALUE(GC, 0, mmSH_MEM_CONFIG, 0x00001000, 0x00001000),
106 SOC15_REG_GOLDEN_VALUE(GC, 0, mmSPI_RESOURCE_RESERVE_CU_0, 0x0007ffff, 0x00000800),
107 SOC15_REG_GOLDEN_VALUE(GC, 0, mmSPI_RESOURCE_RESERVE_CU_1, 0x0007ffff, 0x00000800),
108 SOC15_REG_GOLDEN_VALUE(GC, 0, mmSPI_RESOURCE_RESERVE_EN_CU_0, 0x01ffffff, 0x0000ff87),
109 SOC15_REG_GOLDEN_VALUE(GC, 0, mmSPI_RESOURCE_RESERVE_EN_CU_1, 0x01ffffff, 0x0000ff8f),
110 SOC15_REG_GOLDEN_VALUE(GC, 0, mmSQC_CONFIG, 0x03000000, 0x020a2000),
111 SOC15_REG_GOLDEN_VALUE(GC, 0, mmTA_CNTL_AUX, 0xfffffeef, 0x010b0000),
112 SOC15_REG_GOLDEN_VALUE(GC, 0, mmTCP_CHAN_STEER_HI, 0xffffffff, 0x4a2c0e68),
113 SOC15_REG_GOLDEN_VALUE(GC, 0, mmTCP_CHAN_STEER_LO, 0xffffffff, 0xb5d3f197),
114 SOC15_REG_GOLDEN_VALUE(GC, 0, mmVGT_CACHE_INVALIDATION, 0x3fff3af3, 0x19200000),
115 SOC15_REG_GOLDEN_VALUE(GC, 0, mmVGT_GS_MAX_WAVE_ID, 0x00000fff, 0x000003ff)
118 static const struct soc15_reg_golden golden_settings_gc_9_0_vg10[] =
120 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCB_HW_CONTROL, 0x0000f000, 0x00012107),
121 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCB_HW_CONTROL_3, 0x30000000, 0x10000000),
122 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCPC_UTCL1_CNTL, 0x08000000, 0x08000080),
123 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCPF_UTCL1_CNTL, 0x08000000, 0x08000080),
124 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCPG_UTCL1_CNTL, 0x08000000, 0x08000080),
125 SOC15_REG_GOLDEN_VALUE(GC, 0, mmGB_ADDR_CONFIG, 0xffff77ff, 0x2a114042),
126 SOC15_REG_GOLDEN_VALUE(GC, 0, mmGB_ADDR_CONFIG_READ, 0xffff77ff, 0x2a114042),
127 SOC15_REG_GOLDEN_VALUE(GC, 0, mmIA_UTCL1_CNTL, 0x08000000, 0x08000080),
128 SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_ENHANCE_1, 0x00008000, 0x00048000),
129 SOC15_REG_GOLDEN_VALUE(GC, 0, mmRLC_GPM_UTCL1_CNTL_0, 0x08000000, 0x08000080),
130 SOC15_REG_GOLDEN_VALUE(GC, 0, mmRLC_GPM_UTCL1_CNTL_1, 0x08000000, 0x08000080),
131 SOC15_REG_GOLDEN_VALUE(GC, 0, mmRLC_GPM_UTCL1_CNTL_2, 0x08000000, 0x08000080),
132 SOC15_REG_GOLDEN_VALUE(GC, 0, mmRLC_PREWALKER_UTCL1_CNTL, 0x08000000, 0x08000080),
133 SOC15_REG_GOLDEN_VALUE(GC, 0, mmRLC_SPM_UTCL1_CNTL, 0x08000000, 0x08000080),
134 SOC15_REG_GOLDEN_VALUE(GC, 0, mmRMI_UTCL1_CNTL2, 0x00030000, 0x00020000),
135 SOC15_REG_GOLDEN_VALUE(GC, 0, mmSPI_CONFIG_CNTL_1, 0x0000000f, 0x01000107),
136 SOC15_REG_GOLDEN_VALUE(GC, 0, mmTD_CNTL, 0x00001800, 0x00000800),
137 SOC15_REG_GOLDEN_VALUE(GC, 0, mmWD_UTCL1_CNTL, 0x08000000, 0x08000080),
138 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCP_MEC1_F32_INT_DIS, 0x00000000, 0x00000800),
139 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCP_MEC2_F32_INT_DIS, 0x00000000, 0x00000800),
140 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCP_DEBUG, 0x00000000, 0x00008000)
143 static const struct soc15_reg_golden golden_settings_gc_9_0_vg20[] =
145 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCB_DCC_CONFIG, 0x0f000080, 0x04000080),
146 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCB_HW_CONTROL_2, 0x0f000000, 0x0a000000),
147 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCB_HW_CONTROL_3, 0x30000000, 0x10000000),
148 SOC15_REG_GOLDEN_VALUE(GC, 0, mmGB_ADDR_CONFIG, 0xf3e777ff, 0x22014042),
149 SOC15_REG_GOLDEN_VALUE(GC, 0, mmGB_ADDR_CONFIG_READ, 0xf3e777ff, 0x22014042),
150 SOC15_REG_GOLDEN_VALUE(GC, 0, mmDB_DEBUG2, 0x00003e00, 0x00000400),
151 SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_ENHANCE_1, 0xff840000, 0x04040000),
152 SOC15_REG_GOLDEN_VALUE(GC, 0, mmRMI_UTCL1_CNTL2, 0x00030000, 0x00030000),
153 SOC15_REG_GOLDEN_VALUE(GC, 0, mmSPI_CONFIG_CNTL_1, 0xffff010f, 0x01000107),
154 SOC15_REG_GOLDEN_VALUE(GC, 0, mmTA_CNTL_AUX, 0x000b0000, 0x000b0000),
155 SOC15_REG_GOLDEN_VALUE(GC, 0, mmTD_CNTL, 0x01000000, 0x01000000)
158 static const struct soc15_reg_golden golden_settings_gc_9_1[] =
160 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCB_HW_CONTROL, 0xfffdf3cf, 0x00014104),
161 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCPC_UTCL1_CNTL, 0x08000000, 0x08000080),
162 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCPF_UTCL1_CNTL, 0x08000000, 0x08000080),
163 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCPG_UTCL1_CNTL, 0x08000000, 0x08000080),
164 SOC15_REG_GOLDEN_VALUE(GC, 0, mmDB_DEBUG2, 0xf00fffff, 0x00000420),
165 SOC15_REG_GOLDEN_VALUE(GC, 0, mmGB_GPU_ID, 0x0000000f, 0x00000000),
166 SOC15_REG_GOLDEN_VALUE(GC, 0, mmIA_UTCL1_CNTL, 0x08000000, 0x08000080),
167 SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_BINNER_EVENT_CNTL_3, 0x00000003, 0x82400024),
168 SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_ENHANCE, 0x3fffffff, 0x00000001),
169 SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_LINE_STIPPLE_STATE, 0x0000ff0f, 0x00000000),
170 SOC15_REG_GOLDEN_VALUE(GC, 0, mmRLC_GPM_UTCL1_CNTL_0, 0x08000000, 0x08000080),
171 SOC15_REG_GOLDEN_VALUE(GC, 0, mmRLC_GPM_UTCL1_CNTL_1, 0x08000000, 0x08000080),
172 SOC15_REG_GOLDEN_VALUE(GC, 0, mmRLC_GPM_UTCL1_CNTL_2, 0x08000000, 0x08000080),
173 SOC15_REG_GOLDEN_VALUE(GC, 0, mmRLC_PREWALKER_UTCL1_CNTL, 0x08000000, 0x08000080),
174 SOC15_REG_GOLDEN_VALUE(GC, 0, mmRLC_SPM_UTCL1_CNTL, 0x08000000, 0x08000080),
175 SOC15_REG_GOLDEN_VALUE(GC, 0, mmTA_CNTL_AUX, 0xfffffeef, 0x010b0000),
176 SOC15_REG_GOLDEN_VALUE(GC, 0, mmTCP_CHAN_STEER_HI, 0xffffffff, 0x00000000),
177 SOC15_REG_GOLDEN_VALUE(GC, 0, mmTCP_CHAN_STEER_LO, 0xffffffff, 0x00003120),
178 SOC15_REG_GOLDEN_VALUE(GC, 0, mmVGT_CACHE_INVALIDATION, 0x3fff3af3, 0x19200000),
179 SOC15_REG_GOLDEN_VALUE(GC, 0, mmVGT_GS_MAX_WAVE_ID, 0x00000fff, 0x000000ff),
180 SOC15_REG_GOLDEN_VALUE(GC, 0, mmWD_UTCL1_CNTL, 0x08000000, 0x08000080),
181 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCP_MEC1_F32_INT_DIS, 0x00000000, 0x00000800),
182 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCP_MEC2_F32_INT_DIS, 0x00000000, 0x00000800),
183 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCP_DEBUG, 0x00000000, 0x00008000)
186 static const struct soc15_reg_golden golden_settings_gc_9_1_rv1[] =
188 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCB_HW_CONTROL_3, 0x30000000, 0x10000000),
189 SOC15_REG_GOLDEN_VALUE(GC, 0, mmGB_ADDR_CONFIG, 0xffff77ff, 0x24000042),
190 SOC15_REG_GOLDEN_VALUE(GC, 0, mmGB_ADDR_CONFIG_READ, 0xffff77ff, 0x24000042),
191 SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_ENHANCE_1, 0xffffffff, 0x04048000),
192 SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_MODE_CNTL_1, 0x06000000, 0x06000000),
193 SOC15_REG_GOLDEN_VALUE(GC, 0, mmRMI_UTCL1_CNTL2, 0x00030000, 0x00020000),
194 SOC15_REG_GOLDEN_VALUE(GC, 0, mmTD_CNTL, 0x01bd9f33, 0x00000800)
197 static const struct soc15_reg_golden golden_settings_gc_9_1_rv2[] =
199 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCB_DCC_CONFIG, 0xff7fffff, 0x04000000),
200 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCB_HW_CONTROL, 0xfffdf3cf, 0x00014104),
201 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCB_HW_CONTROL_2, 0xff7fffff, 0x0a000000),
202 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCPC_UTCL1_CNTL, 0x7f0fffff, 0x08000080),
203 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCPF_UTCL1_CNTL, 0xff8fffff, 0x08000080),
204 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCPG_UTCL1_CNTL, 0x7f8fffff, 0x08000080),
205 SOC15_REG_GOLDEN_VALUE(GC, 0, mmGB_ADDR_CONFIG, 0xffff77ff, 0x26013041),
206 SOC15_REG_GOLDEN_VALUE(GC, 0, mmGB_ADDR_CONFIG_READ, 0xffff77ff, 0x26013041),
207 SOC15_REG_GOLDEN_VALUE(GC, 0, mmIA_UTCL1_CNTL, 0x3f8fffff, 0x08000080),
208 SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_ENHANCE_1, 0xffffffff, 0x04040000),
209 SOC15_REG_GOLDEN_VALUE(GC, 0, mmRLC_GPM_UTCL1_CNTL_0, 0xff0fffff, 0x08000080),
210 SOC15_REG_GOLDEN_VALUE(GC, 0, mmRLC_GPM_UTCL1_CNTL_1, 0xff0fffff, 0x08000080),
211 SOC15_REG_GOLDEN_VALUE(GC, 0, mmRLC_GPM_UTCL1_CNTL_2, 0xff0fffff, 0x08000080),
212 SOC15_REG_GOLDEN_VALUE(GC, 0, mmRLC_PREWALKER_UTCL1_CNTL, 0xff0fffff, 0x08000080),
213 SOC15_REG_GOLDEN_VALUE(GC, 0, mmRLC_SPM_UTCL1_CNTL, 0xff0fffff, 0x08000080),
214 SOC15_REG_GOLDEN_VALUE(GC, 0, mmTCP_CHAN_STEER_HI, 0xffffffff, 0x00000000),
215 SOC15_REG_GOLDEN_VALUE(GC, 0, mmTCP_CHAN_STEER_LO, 0xffffffff, 0x00000010),
216 SOC15_REG_GOLDEN_VALUE(GC, 0, mmTD_CNTL, 0x01bd9f33, 0x01000000),
217 SOC15_REG_GOLDEN_VALUE(GC, 0, mmWD_UTCL1_CNTL, 0x3f8fffff, 0x08000080),
220 static const struct soc15_reg_golden golden_settings_gc_9_x_common[] =
222 SOC15_REG_GOLDEN_VALUE(GC, 0, mmGRBM_CAM_INDEX, 0xffffffff, 0x00000000),
223 SOC15_REG_GOLDEN_VALUE(GC, 0, mmGRBM_CAM_DATA, 0xffffffff, 0x2544c382)
226 static const struct soc15_reg_golden golden_settings_gc_9_2_1[] =
228 SOC15_REG_GOLDEN_VALUE(GC, 0, mmDB_DEBUG2, 0xf00fffff, 0x00000420),
229 SOC15_REG_GOLDEN_VALUE(GC, 0, mmGB_GPU_ID, 0x0000000f, 0x00000000),
230 SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_BINNER_EVENT_CNTL_3, 0x00000003, 0x82400024),
231 SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_ENHANCE, 0x3fffffff, 0x00000001),
232 SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_LINE_STIPPLE_STATE, 0x0000ff0f, 0x00000000),
233 SOC15_REG_GOLDEN_VALUE(GC, 0, mmSH_MEM_CONFIG, 0x00001000, 0x00001000),
234 SOC15_REG_GOLDEN_VALUE(GC, 0, mmSPI_RESOURCE_RESERVE_CU_0, 0x0007ffff, 0x00000800),
235 SOC15_REG_GOLDEN_VALUE(GC, 0, mmSPI_RESOURCE_RESERVE_CU_1, 0x0007ffff, 0x00000800),
236 SOC15_REG_GOLDEN_VALUE(GC, 0, mmSPI_RESOURCE_RESERVE_EN_CU_0, 0x01ffffff, 0x0000ff87),
237 SOC15_REG_GOLDEN_VALUE(GC, 0, mmSPI_RESOURCE_RESERVE_EN_CU_1, 0x01ffffff, 0x0000ff8f),
238 SOC15_REG_GOLDEN_VALUE(GC, 0, mmSQC_CONFIG, 0x03000000, 0x020a2000),
239 SOC15_REG_GOLDEN_VALUE(GC, 0, mmTA_CNTL_AUX, 0xfffffeef, 0x010b0000),
240 SOC15_REG_GOLDEN_VALUE(GC, 0, mmTCP_CHAN_STEER_HI, 0xffffffff, 0x4a2c0e68),
241 SOC15_REG_GOLDEN_VALUE(GC, 0, mmTCP_CHAN_STEER_LO, 0xffffffff, 0xb5d3f197),
242 SOC15_REG_GOLDEN_VALUE(GC, 0, mmVGT_CACHE_INVALIDATION, 0x3fff3af3, 0x19200000),
243 SOC15_REG_GOLDEN_VALUE(GC, 0, mmVGT_GS_MAX_WAVE_ID, 0x00000fff, 0x000003ff)
246 static const struct soc15_reg_golden golden_settings_gc_9_2_1_vg12[] =
248 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCB_DCC_CONFIG, 0x00000080, 0x04000080),
249 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCB_HW_CONTROL, 0xfffdf3cf, 0x00014104),
250 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCB_HW_CONTROL_2, 0x0f000000, 0x0a000000),
251 SOC15_REG_GOLDEN_VALUE(GC, 0, mmGB_ADDR_CONFIG, 0xffff77ff, 0x24104041),
252 SOC15_REG_GOLDEN_VALUE(GC, 0, mmGB_ADDR_CONFIG_READ, 0xffff77ff, 0x24104041),
253 SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_ENHANCE_1, 0xffffffff, 0x04040000),
254 SOC15_REG_GOLDEN_VALUE(GC, 0, mmSPI_CONFIG_CNTL_1, 0xffff03ff, 0x01000107),
255 SOC15_REG_GOLDEN_VALUE(GC, 0, mmTCP_CHAN_STEER_HI, 0xffffffff, 0x00000000),
256 SOC15_REG_GOLDEN_VALUE(GC, 0, mmTCP_CHAN_STEER_LO, 0xffffffff, 0x76325410),
257 SOC15_REG_GOLDEN_VALUE(GC, 0, mmTD_CNTL, 0x01bd9f33, 0x01000000),
258 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCP_MEC1_F32_INT_DIS, 0x00000000, 0x00000800),
259 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCP_MEC2_F32_INT_DIS, 0x00000000, 0x00000800),
260 SOC15_REG_GOLDEN_VALUE(GC, 0, mmCP_DEBUG, 0x00000000, 0x00008000)
263 static const u32 GFX_RLC_SRM_INDEX_CNTL_ADDR_OFFSETS[] =
265 mmRLC_SRM_INDEX_CNTL_ADDR_0 - mmRLC_SRM_INDEX_CNTL_ADDR_0,
266 mmRLC_SRM_INDEX_CNTL_ADDR_1 - mmRLC_SRM_INDEX_CNTL_ADDR_0,
267 mmRLC_SRM_INDEX_CNTL_ADDR_2 - mmRLC_SRM_INDEX_CNTL_ADDR_0,
268 mmRLC_SRM_INDEX_CNTL_ADDR_3 - mmRLC_SRM_INDEX_CNTL_ADDR_0,
269 mmRLC_SRM_INDEX_CNTL_ADDR_4 - mmRLC_SRM_INDEX_CNTL_ADDR_0,
270 mmRLC_SRM_INDEX_CNTL_ADDR_5 - mmRLC_SRM_INDEX_CNTL_ADDR_0,
271 mmRLC_SRM_INDEX_CNTL_ADDR_6 - mmRLC_SRM_INDEX_CNTL_ADDR_0,
272 mmRLC_SRM_INDEX_CNTL_ADDR_7 - mmRLC_SRM_INDEX_CNTL_ADDR_0,
275 static const u32 GFX_RLC_SRM_INDEX_CNTL_DATA_OFFSETS[] =
277 mmRLC_SRM_INDEX_CNTL_DATA_0 - mmRLC_SRM_INDEX_CNTL_DATA_0,
278 mmRLC_SRM_INDEX_CNTL_DATA_1 - mmRLC_SRM_INDEX_CNTL_DATA_0,
279 mmRLC_SRM_INDEX_CNTL_DATA_2 - mmRLC_SRM_INDEX_CNTL_DATA_0,
280 mmRLC_SRM_INDEX_CNTL_DATA_3 - mmRLC_SRM_INDEX_CNTL_DATA_0,
281 mmRLC_SRM_INDEX_CNTL_DATA_4 - mmRLC_SRM_INDEX_CNTL_DATA_0,
282 mmRLC_SRM_INDEX_CNTL_DATA_5 - mmRLC_SRM_INDEX_CNTL_DATA_0,
283 mmRLC_SRM_INDEX_CNTL_DATA_6 - mmRLC_SRM_INDEX_CNTL_DATA_0,
284 mmRLC_SRM_INDEX_CNTL_DATA_7 - mmRLC_SRM_INDEX_CNTL_DATA_0,
287 #define VEGA10_GB_ADDR_CONFIG_GOLDEN 0x2a114042
288 #define VEGA12_GB_ADDR_CONFIG_GOLDEN 0x24104041
289 #define RAVEN_GB_ADDR_CONFIG_GOLDEN 0x24000042
290 #define RAVEN2_GB_ADDR_CONFIG_GOLDEN 0x26013041
292 static void gfx_v9_0_set_ring_funcs(struct amdgpu_device *adev);
293 static void gfx_v9_0_set_irq_funcs(struct amdgpu_device *adev);
294 static void gfx_v9_0_set_gds_init(struct amdgpu_device *adev);
295 static void gfx_v9_0_set_rlc_funcs(struct amdgpu_device *adev);
296 static int gfx_v9_0_get_cu_info(struct amdgpu_device *adev,
297 struct amdgpu_cu_info *cu_info);
298 static uint64_t gfx_v9_0_get_gpu_clock_counter(struct amdgpu_device *adev);
299 static void gfx_v9_0_select_se_sh(struct amdgpu_device *adev, u32 se_num, u32 sh_num, u32 instance);
300 static void gfx_v9_0_ring_emit_de_meta(struct amdgpu_ring *ring);
302 static void gfx_v9_0_init_golden_registers(struct amdgpu_device *adev)
304 switch (adev->asic_type) {
306 soc15_program_register_sequence(adev,
307 golden_settings_gc_9_0,
308 ARRAY_SIZE(golden_settings_gc_9_0));
309 soc15_program_register_sequence(adev,
310 golden_settings_gc_9_0_vg10,
311 ARRAY_SIZE(golden_settings_gc_9_0_vg10));
314 soc15_program_register_sequence(adev,
315 golden_settings_gc_9_2_1,
316 ARRAY_SIZE(golden_settings_gc_9_2_1));
317 soc15_program_register_sequence(adev,
318 golden_settings_gc_9_2_1_vg12,
319 ARRAY_SIZE(golden_settings_gc_9_2_1_vg12));
322 soc15_program_register_sequence(adev,
323 golden_settings_gc_9_0,
324 ARRAY_SIZE(golden_settings_gc_9_0));
325 soc15_program_register_sequence(adev,
326 golden_settings_gc_9_0_vg20,
327 ARRAY_SIZE(golden_settings_gc_9_0_vg20));
330 soc15_program_register_sequence(adev, golden_settings_gc_9_1,
331 ARRAY_SIZE(golden_settings_gc_9_1));
332 if (adev->rev_id >= 8)
333 soc15_program_register_sequence(adev,
334 golden_settings_gc_9_1_rv2,
335 ARRAY_SIZE(golden_settings_gc_9_1_rv2));
337 soc15_program_register_sequence(adev,
338 golden_settings_gc_9_1_rv1,
339 ARRAY_SIZE(golden_settings_gc_9_1_rv1));
345 soc15_program_register_sequence(adev, golden_settings_gc_9_x_common,
346 (const u32)ARRAY_SIZE(golden_settings_gc_9_x_common));
349 static void gfx_v9_0_scratch_init(struct amdgpu_device *adev)
351 adev->gfx.scratch.num_reg = 8;
352 adev->gfx.scratch.reg_base = SOC15_REG_OFFSET(GC, 0, mmSCRATCH_REG0);
353 adev->gfx.scratch.free_mask = (1u << adev->gfx.scratch.num_reg) - 1;
356 static void gfx_v9_0_write_data_to_reg(struct amdgpu_ring *ring, int eng_sel,
357 bool wc, uint32_t reg, uint32_t val)
359 amdgpu_ring_write(ring, PACKET3(PACKET3_WRITE_DATA, 3));
360 amdgpu_ring_write(ring, WRITE_DATA_ENGINE_SEL(eng_sel) |
361 WRITE_DATA_DST_SEL(0) |
362 (wc ? WR_CONFIRM : 0));
363 amdgpu_ring_write(ring, reg);
364 amdgpu_ring_write(ring, 0);
365 amdgpu_ring_write(ring, val);
368 static void gfx_v9_0_wait_reg_mem(struct amdgpu_ring *ring, int eng_sel,
369 int mem_space, int opt, uint32_t addr0,
370 uint32_t addr1, uint32_t ref, uint32_t mask,
373 amdgpu_ring_write(ring, PACKET3(PACKET3_WAIT_REG_MEM, 5));
374 amdgpu_ring_write(ring,
375 /* memory (1) or register (0) */
376 (WAIT_REG_MEM_MEM_SPACE(mem_space) |
377 WAIT_REG_MEM_OPERATION(opt) | /* wait */
378 WAIT_REG_MEM_FUNCTION(3) | /* equal */
379 WAIT_REG_MEM_ENGINE(eng_sel)));
382 BUG_ON(addr0 & 0x3); /* Dword align */
383 amdgpu_ring_write(ring, addr0);
384 amdgpu_ring_write(ring, addr1);
385 amdgpu_ring_write(ring, ref);
386 amdgpu_ring_write(ring, mask);
387 amdgpu_ring_write(ring, inv); /* poll interval */
390 static int gfx_v9_0_ring_test_ring(struct amdgpu_ring *ring)
392 struct amdgpu_device *adev = ring->adev;
398 r = amdgpu_gfx_scratch_get(adev, &scratch);
400 DRM_ERROR("amdgpu: cp failed to get scratch reg (%d).\n", r);
403 WREG32(scratch, 0xCAFEDEAD);
404 r = amdgpu_ring_alloc(ring, 3);
406 DRM_ERROR("amdgpu: cp failed to lock ring %d (%d).\n",
408 amdgpu_gfx_scratch_free(adev, scratch);
411 amdgpu_ring_write(ring, PACKET3(PACKET3_SET_UCONFIG_REG, 1));
412 amdgpu_ring_write(ring, (scratch - PACKET3_SET_UCONFIG_REG_START));
413 amdgpu_ring_write(ring, 0xDEADBEEF);
414 amdgpu_ring_commit(ring);
416 for (i = 0; i < adev->usec_timeout; i++) {
417 tmp = RREG32(scratch);
418 if (tmp == 0xDEADBEEF)
422 if (i < adev->usec_timeout) {
423 DRM_DEBUG("ring test on %d succeeded in %d usecs\n",
426 DRM_ERROR("amdgpu: ring %d test failed (scratch(0x%04X)=0x%08X)\n",
427 ring->idx, scratch, tmp);
430 amdgpu_gfx_scratch_free(adev, scratch);
434 static int gfx_v9_0_ring_test_ib(struct amdgpu_ring *ring, long timeout)
436 struct amdgpu_device *adev = ring->adev;
438 struct dma_fence *f = NULL;
445 r = amdgpu_device_wb_get(adev, &index);
447 dev_err(adev->dev, "(%ld) failed to allocate wb slot\n", r);
451 gpu_addr = adev->wb.gpu_addr + (index * 4);
452 adev->wb.wb[index] = cpu_to_le32(0xCAFEDEAD);
453 memset(&ib, 0, sizeof(ib));
454 r = amdgpu_ib_get(adev, NULL, 16, &ib);
456 DRM_ERROR("amdgpu: failed to get ib (%ld).\n", r);
459 ib.ptr[0] = PACKET3(PACKET3_WRITE_DATA, 3);
460 ib.ptr[1] = WRITE_DATA_DST_SEL(5) | WR_CONFIRM;
461 ib.ptr[2] = lower_32_bits(gpu_addr);
462 ib.ptr[3] = upper_32_bits(gpu_addr);
463 ib.ptr[4] = 0xDEADBEEF;
466 r = amdgpu_ib_schedule(ring, 1, &ib, NULL, &f);
470 r = dma_fence_wait_timeout(f, false, timeout);
472 DRM_ERROR("amdgpu: IB test timed out.\n");
476 DRM_ERROR("amdgpu: fence wait failed (%ld).\n", r);
480 tmp = adev->wb.wb[index];
481 if (tmp == 0xDEADBEEF) {
482 DRM_DEBUG("ib test on ring %d succeeded\n", ring->idx);
485 DRM_ERROR("ib test on ring %d failed\n", ring->idx);
490 amdgpu_ib_free(adev, &ib, NULL);
493 amdgpu_device_wb_free(adev, index);
498 static void gfx_v9_0_free_microcode(struct amdgpu_device *adev)
500 release_firmware(adev->gfx.pfp_fw);
501 adev->gfx.pfp_fw = NULL;
502 release_firmware(adev->gfx.me_fw);
503 adev->gfx.me_fw = NULL;
504 release_firmware(adev->gfx.ce_fw);
505 adev->gfx.ce_fw = NULL;
506 release_firmware(adev->gfx.rlc_fw);
507 adev->gfx.rlc_fw = NULL;
508 release_firmware(adev->gfx.mec_fw);
509 adev->gfx.mec_fw = NULL;
510 release_firmware(adev->gfx.mec2_fw);
511 adev->gfx.mec2_fw = NULL;
513 kfree(adev->gfx.rlc.register_list_format);
516 static void gfx_v9_0_init_rlc_ext_microcode(struct amdgpu_device *adev)
518 const struct rlc_firmware_header_v2_1 *rlc_hdr;
520 rlc_hdr = (const struct rlc_firmware_header_v2_1 *)adev->gfx.rlc_fw->data;
521 adev->gfx.rlc_srlc_fw_version = le32_to_cpu(rlc_hdr->save_restore_list_cntl_ucode_ver);
522 adev->gfx.rlc_srlc_feature_version = le32_to_cpu(rlc_hdr->save_restore_list_cntl_feature_ver);
523 adev->gfx.rlc.save_restore_list_cntl_size_bytes = le32_to_cpu(rlc_hdr->save_restore_list_cntl_size_bytes);
524 adev->gfx.rlc.save_restore_list_cntl = (u8 *)rlc_hdr + le32_to_cpu(rlc_hdr->save_restore_list_cntl_offset_bytes);
525 adev->gfx.rlc_srlg_fw_version = le32_to_cpu(rlc_hdr->save_restore_list_gpm_ucode_ver);
526 adev->gfx.rlc_srlg_feature_version = le32_to_cpu(rlc_hdr->save_restore_list_gpm_feature_ver);
527 adev->gfx.rlc.save_restore_list_gpm_size_bytes = le32_to_cpu(rlc_hdr->save_restore_list_gpm_size_bytes);
528 adev->gfx.rlc.save_restore_list_gpm = (u8 *)rlc_hdr + le32_to_cpu(rlc_hdr->save_restore_list_gpm_offset_bytes);
529 adev->gfx.rlc_srls_fw_version = le32_to_cpu(rlc_hdr->save_restore_list_srm_ucode_ver);
530 adev->gfx.rlc_srls_feature_version = le32_to_cpu(rlc_hdr->save_restore_list_srm_feature_ver);
531 adev->gfx.rlc.save_restore_list_srm_size_bytes = le32_to_cpu(rlc_hdr->save_restore_list_srm_size_bytes);
532 adev->gfx.rlc.save_restore_list_srm = (u8 *)rlc_hdr + le32_to_cpu(rlc_hdr->save_restore_list_srm_offset_bytes);
533 adev->gfx.rlc.reg_list_format_direct_reg_list_length =
534 le32_to_cpu(rlc_hdr->reg_list_format_direct_reg_list_length);
537 static void gfx_v9_0_check_fw_write_wait(struct amdgpu_device *adev)
539 adev->gfx.me_fw_write_wait = false;
540 adev->gfx.mec_fw_write_wait = false;
542 switch (adev->asic_type) {
544 if ((adev->gfx.me_fw_version >= 0x0000009c) &&
545 (adev->gfx.me_feature_version >= 42) &&
546 (adev->gfx.pfp_fw_version >= 0x000000b1) &&
547 (adev->gfx.pfp_feature_version >= 42))
548 adev->gfx.me_fw_write_wait = true;
550 if ((adev->gfx.mec_fw_version >= 0x00000193) &&
551 (adev->gfx.mec_feature_version >= 42))
552 adev->gfx.mec_fw_write_wait = true;
555 if ((adev->gfx.me_fw_version >= 0x0000009c) &&
556 (adev->gfx.me_feature_version >= 44) &&
557 (adev->gfx.pfp_fw_version >= 0x000000b2) &&
558 (adev->gfx.pfp_feature_version >= 44))
559 adev->gfx.me_fw_write_wait = true;
561 if ((adev->gfx.mec_fw_version >= 0x00000196) &&
562 (adev->gfx.mec_feature_version >= 44))
563 adev->gfx.mec_fw_write_wait = true;
566 if ((adev->gfx.me_fw_version >= 0x0000009c) &&
567 (adev->gfx.me_feature_version >= 44) &&
568 (adev->gfx.pfp_fw_version >= 0x000000b2) &&
569 (adev->gfx.pfp_feature_version >= 44))
570 adev->gfx.me_fw_write_wait = true;
572 if ((adev->gfx.mec_fw_version >= 0x00000197) &&
573 (adev->gfx.mec_feature_version >= 44))
574 adev->gfx.mec_fw_write_wait = true;
577 if ((adev->gfx.me_fw_version >= 0x0000009c) &&
578 (adev->gfx.me_feature_version >= 42) &&
579 (adev->gfx.pfp_fw_version >= 0x000000b1) &&
580 (adev->gfx.pfp_feature_version >= 42))
581 adev->gfx.me_fw_write_wait = true;
583 if ((adev->gfx.mec_fw_version >= 0x00000192) &&
584 (adev->gfx.mec_feature_version >= 42))
585 adev->gfx.mec_fw_write_wait = true;
592 static int gfx_v9_0_init_microcode(struct amdgpu_device *adev)
594 const char *chip_name;
597 struct amdgpu_firmware_info *info = NULL;
598 const struct common_firmware_header *header = NULL;
599 const struct gfx_firmware_header_v1_0 *cp_hdr;
600 const struct rlc_firmware_header_v2_0 *rlc_hdr;
601 unsigned int *tmp = NULL;
603 uint16_t version_major;
604 uint16_t version_minor;
608 switch (adev->asic_type) {
610 chip_name = "vega10";
613 chip_name = "vega12";
616 chip_name = "vega20";
619 if (adev->rev_id >= 8)
620 chip_name = "raven2";
621 else if (adev->pdev->device == 0x15d8)
622 chip_name = "picasso";
630 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_pfp.bin", chip_name);
631 err = request_firmware(&adev->gfx.pfp_fw, fw_name, adev->dev);
634 err = amdgpu_ucode_validate(adev->gfx.pfp_fw);
637 cp_hdr = (const struct gfx_firmware_header_v1_0 *)adev->gfx.pfp_fw->data;
638 adev->gfx.pfp_fw_version = le32_to_cpu(cp_hdr->header.ucode_version);
639 adev->gfx.pfp_feature_version = le32_to_cpu(cp_hdr->ucode_feature_version);
641 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_me.bin", chip_name);
642 err = request_firmware(&adev->gfx.me_fw, fw_name, adev->dev);
645 err = amdgpu_ucode_validate(adev->gfx.me_fw);
648 cp_hdr = (const struct gfx_firmware_header_v1_0 *)adev->gfx.me_fw->data;
649 adev->gfx.me_fw_version = le32_to_cpu(cp_hdr->header.ucode_version);
650 adev->gfx.me_feature_version = le32_to_cpu(cp_hdr->ucode_feature_version);
652 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_ce.bin", chip_name);
653 err = request_firmware(&adev->gfx.ce_fw, fw_name, adev->dev);
656 err = amdgpu_ucode_validate(adev->gfx.ce_fw);
659 cp_hdr = (const struct gfx_firmware_header_v1_0 *)adev->gfx.ce_fw->data;
660 adev->gfx.ce_fw_version = le32_to_cpu(cp_hdr->header.ucode_version);
661 adev->gfx.ce_feature_version = le32_to_cpu(cp_hdr->ucode_feature_version);
663 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_rlc.bin", chip_name);
664 err = request_firmware(&adev->gfx.rlc_fw, fw_name, adev->dev);
667 err = amdgpu_ucode_validate(adev->gfx.rlc_fw);
668 rlc_hdr = (const struct rlc_firmware_header_v2_0 *)adev->gfx.rlc_fw->data;
670 version_major = le16_to_cpu(rlc_hdr->header.header_version_major);
671 version_minor = le16_to_cpu(rlc_hdr->header.header_version_minor);
672 if (version_major == 2 && version_minor == 1)
673 adev->gfx.rlc.is_rlc_v2_1 = true;
675 adev->gfx.rlc_fw_version = le32_to_cpu(rlc_hdr->header.ucode_version);
676 adev->gfx.rlc_feature_version = le32_to_cpu(rlc_hdr->ucode_feature_version);
677 adev->gfx.rlc.save_and_restore_offset =
678 le32_to_cpu(rlc_hdr->save_and_restore_offset);
679 adev->gfx.rlc.clear_state_descriptor_offset =
680 le32_to_cpu(rlc_hdr->clear_state_descriptor_offset);
681 adev->gfx.rlc.avail_scratch_ram_locations =
682 le32_to_cpu(rlc_hdr->avail_scratch_ram_locations);
683 adev->gfx.rlc.reg_restore_list_size =
684 le32_to_cpu(rlc_hdr->reg_restore_list_size);
685 adev->gfx.rlc.reg_list_format_start =
686 le32_to_cpu(rlc_hdr->reg_list_format_start);
687 adev->gfx.rlc.reg_list_format_separate_start =
688 le32_to_cpu(rlc_hdr->reg_list_format_separate_start);
689 adev->gfx.rlc.starting_offsets_start =
690 le32_to_cpu(rlc_hdr->starting_offsets_start);
691 adev->gfx.rlc.reg_list_format_size_bytes =
692 le32_to_cpu(rlc_hdr->reg_list_format_size_bytes);
693 adev->gfx.rlc.reg_list_size_bytes =
694 le32_to_cpu(rlc_hdr->reg_list_size_bytes);
695 adev->gfx.rlc.register_list_format =
696 kmalloc(adev->gfx.rlc.reg_list_format_size_bytes +
697 adev->gfx.rlc.reg_list_size_bytes, GFP_KERNEL);
698 if (!adev->gfx.rlc.register_list_format) {
703 tmp = (unsigned int *)((uintptr_t)rlc_hdr +
704 le32_to_cpu(rlc_hdr->reg_list_format_array_offset_bytes));
705 for (i = 0 ; i < (adev->gfx.rlc.reg_list_format_size_bytes >> 2); i++)
706 adev->gfx.rlc.register_list_format[i] = le32_to_cpu(tmp[i]);
708 adev->gfx.rlc.register_restore = adev->gfx.rlc.register_list_format + i;
710 tmp = (unsigned int *)((uintptr_t)rlc_hdr +
711 le32_to_cpu(rlc_hdr->reg_list_array_offset_bytes));
712 for (i = 0 ; i < (adev->gfx.rlc.reg_list_size_bytes >> 2); i++)
713 adev->gfx.rlc.register_restore[i] = le32_to_cpu(tmp[i]);
715 if (adev->gfx.rlc.is_rlc_v2_1)
716 gfx_v9_0_init_rlc_ext_microcode(adev);
718 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_mec.bin", chip_name);
719 err = request_firmware(&adev->gfx.mec_fw, fw_name, adev->dev);
722 err = amdgpu_ucode_validate(adev->gfx.mec_fw);
725 cp_hdr = (const struct gfx_firmware_header_v1_0 *)adev->gfx.mec_fw->data;
726 adev->gfx.mec_fw_version = le32_to_cpu(cp_hdr->header.ucode_version);
727 adev->gfx.mec_feature_version = le32_to_cpu(cp_hdr->ucode_feature_version);
730 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_mec2.bin", chip_name);
731 err = request_firmware(&adev->gfx.mec2_fw, fw_name, adev->dev);
733 err = amdgpu_ucode_validate(adev->gfx.mec2_fw);
736 cp_hdr = (const struct gfx_firmware_header_v1_0 *)
737 adev->gfx.mec2_fw->data;
738 adev->gfx.mec2_fw_version =
739 le32_to_cpu(cp_hdr->header.ucode_version);
740 adev->gfx.mec2_feature_version =
741 le32_to_cpu(cp_hdr->ucode_feature_version);
744 adev->gfx.mec2_fw = NULL;
747 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
748 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CP_PFP];
749 info->ucode_id = AMDGPU_UCODE_ID_CP_PFP;
750 info->fw = adev->gfx.pfp_fw;
751 header = (const struct common_firmware_header *)info->fw->data;
752 adev->firmware.fw_size +=
753 ALIGN(le32_to_cpu(header->ucode_size_bytes), PAGE_SIZE);
755 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CP_ME];
756 info->ucode_id = AMDGPU_UCODE_ID_CP_ME;
757 info->fw = adev->gfx.me_fw;
758 header = (const struct common_firmware_header *)info->fw->data;
759 adev->firmware.fw_size +=
760 ALIGN(le32_to_cpu(header->ucode_size_bytes), PAGE_SIZE);
762 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CP_CE];
763 info->ucode_id = AMDGPU_UCODE_ID_CP_CE;
764 info->fw = adev->gfx.ce_fw;
765 header = (const struct common_firmware_header *)info->fw->data;
766 adev->firmware.fw_size +=
767 ALIGN(le32_to_cpu(header->ucode_size_bytes), PAGE_SIZE);
769 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_RLC_G];
770 info->ucode_id = AMDGPU_UCODE_ID_RLC_G;
771 info->fw = adev->gfx.rlc_fw;
772 header = (const struct common_firmware_header *)info->fw->data;
773 adev->firmware.fw_size +=
774 ALIGN(le32_to_cpu(header->ucode_size_bytes), PAGE_SIZE);
776 if (adev->gfx.rlc.is_rlc_v2_1 &&
777 adev->gfx.rlc.save_restore_list_cntl_size_bytes &&
778 adev->gfx.rlc.save_restore_list_gpm_size_bytes &&
779 adev->gfx.rlc.save_restore_list_srm_size_bytes) {
780 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL];
781 info->ucode_id = AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL;
782 info->fw = adev->gfx.rlc_fw;
783 adev->firmware.fw_size +=
784 ALIGN(adev->gfx.rlc.save_restore_list_cntl_size_bytes, PAGE_SIZE);
786 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM];
787 info->ucode_id = AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM;
788 info->fw = adev->gfx.rlc_fw;
789 adev->firmware.fw_size +=
790 ALIGN(adev->gfx.rlc.save_restore_list_gpm_size_bytes, PAGE_SIZE);
792 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM];
793 info->ucode_id = AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM;
794 info->fw = adev->gfx.rlc_fw;
795 adev->firmware.fw_size +=
796 ALIGN(adev->gfx.rlc.save_restore_list_srm_size_bytes, PAGE_SIZE);
799 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CP_MEC1];
800 info->ucode_id = AMDGPU_UCODE_ID_CP_MEC1;
801 info->fw = adev->gfx.mec_fw;
802 header = (const struct common_firmware_header *)info->fw->data;
803 cp_hdr = (const struct gfx_firmware_header_v1_0 *)info->fw->data;
804 adev->firmware.fw_size +=
805 ALIGN(le32_to_cpu(header->ucode_size_bytes) - le32_to_cpu(cp_hdr->jt_size) * 4, PAGE_SIZE);
807 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CP_MEC1_JT];
808 info->ucode_id = AMDGPU_UCODE_ID_CP_MEC1_JT;
809 info->fw = adev->gfx.mec_fw;
810 adev->firmware.fw_size +=
811 ALIGN(le32_to_cpu(cp_hdr->jt_size) * 4, PAGE_SIZE);
813 if (adev->gfx.mec2_fw) {
814 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CP_MEC2];
815 info->ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
816 info->fw = adev->gfx.mec2_fw;
817 header = (const struct common_firmware_header *)info->fw->data;
818 cp_hdr = (const struct gfx_firmware_header_v1_0 *)info->fw->data;
819 adev->firmware.fw_size +=
820 ALIGN(le32_to_cpu(header->ucode_size_bytes) - le32_to_cpu(cp_hdr->jt_size) * 4, PAGE_SIZE);
821 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CP_MEC2_JT];
822 info->ucode_id = AMDGPU_UCODE_ID_CP_MEC2_JT;
823 info->fw = adev->gfx.mec2_fw;
824 adev->firmware.fw_size +=
825 ALIGN(le32_to_cpu(cp_hdr->jt_size) * 4, PAGE_SIZE);
831 gfx_v9_0_check_fw_write_wait(adev);
834 "gfx9: Failed to load firmware \"%s\"\n",
836 release_firmware(adev->gfx.pfp_fw);
837 adev->gfx.pfp_fw = NULL;
838 release_firmware(adev->gfx.me_fw);
839 adev->gfx.me_fw = NULL;
840 release_firmware(adev->gfx.ce_fw);
841 adev->gfx.ce_fw = NULL;
842 release_firmware(adev->gfx.rlc_fw);
843 adev->gfx.rlc_fw = NULL;
844 release_firmware(adev->gfx.mec_fw);
845 adev->gfx.mec_fw = NULL;
846 release_firmware(adev->gfx.mec2_fw);
847 adev->gfx.mec2_fw = NULL;
852 static u32 gfx_v9_0_get_csb_size(struct amdgpu_device *adev)
855 const struct cs_section_def *sect = NULL;
856 const struct cs_extent_def *ext = NULL;
858 /* begin clear state */
860 /* context control state */
863 for (sect = gfx9_cs_data; sect->section != NULL; ++sect) {
864 for (ext = sect->section; ext->extent != NULL; ++ext) {
865 if (sect->id == SECT_CONTEXT)
866 count += 2 + ext->reg_count;
872 /* end clear state */
880 static void gfx_v9_0_get_csb_buffer(struct amdgpu_device *adev,
881 volatile u32 *buffer)
884 const struct cs_section_def *sect = NULL;
885 const struct cs_extent_def *ext = NULL;
887 if (adev->gfx.rlc.cs_data == NULL)
892 buffer[count++] = cpu_to_le32(PACKET3(PACKET3_PREAMBLE_CNTL, 0));
893 buffer[count++] = cpu_to_le32(PACKET3_PREAMBLE_BEGIN_CLEAR_STATE);
895 buffer[count++] = cpu_to_le32(PACKET3(PACKET3_CONTEXT_CONTROL, 1));
896 buffer[count++] = cpu_to_le32(0x80000000);
897 buffer[count++] = cpu_to_le32(0x80000000);
899 for (sect = adev->gfx.rlc.cs_data; sect->section != NULL; ++sect) {
900 for (ext = sect->section; ext->extent != NULL; ++ext) {
901 if (sect->id == SECT_CONTEXT) {
903 cpu_to_le32(PACKET3(PACKET3_SET_CONTEXT_REG, ext->reg_count));
904 buffer[count++] = cpu_to_le32(ext->reg_index -
905 PACKET3_SET_CONTEXT_REG_START);
906 for (i = 0; i < ext->reg_count; i++)
907 buffer[count++] = cpu_to_le32(ext->extent[i]);
914 buffer[count++] = cpu_to_le32(PACKET3(PACKET3_PREAMBLE_CNTL, 0));
915 buffer[count++] = cpu_to_le32(PACKET3_PREAMBLE_END_CLEAR_STATE);
917 buffer[count++] = cpu_to_le32(PACKET3(PACKET3_CLEAR_STATE, 0));
918 buffer[count++] = cpu_to_le32(0);
921 static void gfx_v9_0_init_always_on_cu_mask(struct amdgpu_device *adev)
923 struct amdgpu_cu_info *cu_info = &adev->gfx.cu_info;
924 uint32_t pg_always_on_cu_num = 2;
925 uint32_t always_on_cu_num;
927 uint32_t mask, cu_bitmap, counter;
929 if (adev->flags & AMD_IS_APU)
930 always_on_cu_num = 4;
931 else if (adev->asic_type == CHIP_VEGA12)
932 always_on_cu_num = 8;
934 always_on_cu_num = 12;
936 mutex_lock(&adev->grbm_idx_mutex);
937 for (i = 0; i < adev->gfx.config.max_shader_engines; i++) {
938 for (j = 0; j < adev->gfx.config.max_sh_per_se; j++) {
942 gfx_v9_0_select_se_sh(adev, i, j, 0xffffffff);
944 for (k = 0; k < adev->gfx.config.max_cu_per_sh; k ++) {
945 if (cu_info->bitmap[i][j] & mask) {
946 if (counter == pg_always_on_cu_num)
947 WREG32_SOC15(GC, 0, mmRLC_PG_ALWAYS_ON_CU_MASK, cu_bitmap);
948 if (counter < always_on_cu_num)
957 WREG32_SOC15(GC, 0, mmRLC_LB_ALWAYS_ACTIVE_CU_MASK, cu_bitmap);
958 cu_info->ao_cu_bitmap[i][j] = cu_bitmap;
961 gfx_v9_0_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
962 mutex_unlock(&adev->grbm_idx_mutex);
965 static void gfx_v9_0_init_lbpw(struct amdgpu_device *adev)
969 /* set mmRLC_LB_THR_CONFIG_1/2/3/4 */
970 WREG32_SOC15(GC, 0, mmRLC_LB_THR_CONFIG_1, 0x0000007F);
971 WREG32_SOC15(GC, 0, mmRLC_LB_THR_CONFIG_2, 0x0333A5A7);
972 WREG32_SOC15(GC, 0, mmRLC_LB_THR_CONFIG_3, 0x00000077);
973 WREG32_SOC15(GC, 0, mmRLC_LB_THR_CONFIG_4, (0x30 | 0x40 << 8 | 0x02FA << 16));
975 /* set mmRLC_LB_CNTR_INIT = 0x0000_0000 */
976 WREG32_SOC15(GC, 0, mmRLC_LB_CNTR_INIT, 0x00000000);
978 /* set mmRLC_LB_CNTR_MAX = 0x0000_0500 */
979 WREG32_SOC15(GC, 0, mmRLC_LB_CNTR_MAX, 0x00000500);
981 mutex_lock(&adev->grbm_idx_mutex);
982 /* set mmRLC_LB_INIT_CU_MASK thru broadcast mode to enable all SE/SH*/
983 gfx_v9_0_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
984 WREG32_SOC15(GC, 0, mmRLC_LB_INIT_CU_MASK, 0xffffffff);
986 /* set mmRLC_LB_PARAMS = 0x003F_1006 */
987 data = REG_SET_FIELD(0, RLC_LB_PARAMS, FIFO_SAMPLES, 0x0003);
988 data |= REG_SET_FIELD(data, RLC_LB_PARAMS, PG_IDLE_SAMPLES, 0x0010);
989 data |= REG_SET_FIELD(data, RLC_LB_PARAMS, PG_IDLE_SAMPLE_INTERVAL, 0x033F);
990 WREG32_SOC15(GC, 0, mmRLC_LB_PARAMS, data);
992 /* set mmRLC_GPM_GENERAL_7[31-16] = 0x00C0 */
993 data = RREG32_SOC15(GC, 0, mmRLC_GPM_GENERAL_7);
996 WREG32_SOC15(GC, 0, mmRLC_GPM_GENERAL_7, data);
999 * RLC_LB_ALWAYS_ACTIVE_CU_MASK = 0xF (4 CUs AON for Raven),
1000 * programmed in gfx_v9_0_init_always_on_cu_mask()
1003 /* set RLC_LB_CNTL = 0x8000_0095, 31 bit is reserved,
1004 * but used for RLC_LB_CNTL configuration */
1005 data = RLC_LB_CNTL__LB_CNT_SPIM_ACTIVE_MASK;
1006 data |= REG_SET_FIELD(data, RLC_LB_CNTL, CU_MASK_USED_OFF_HYST, 0x09);
1007 data |= REG_SET_FIELD(data, RLC_LB_CNTL, RESERVED, 0x80000);
1008 WREG32_SOC15(GC, 0, mmRLC_LB_CNTL, data);
1009 mutex_unlock(&adev->grbm_idx_mutex);
1011 gfx_v9_0_init_always_on_cu_mask(adev);
1014 static void gfx_v9_4_init_lbpw(struct amdgpu_device *adev)
1018 /* set mmRLC_LB_THR_CONFIG_1/2/3/4 */
1019 WREG32_SOC15(GC, 0, mmRLC_LB_THR_CONFIG_1, 0x0000007F);
1020 WREG32_SOC15(GC, 0, mmRLC_LB_THR_CONFIG_2, 0x033388F8);
1021 WREG32_SOC15(GC, 0, mmRLC_LB_THR_CONFIG_3, 0x00000077);
1022 WREG32_SOC15(GC, 0, mmRLC_LB_THR_CONFIG_4, (0x10 | 0x27 << 8 | 0x02FA << 16));
1024 /* set mmRLC_LB_CNTR_INIT = 0x0000_0000 */
1025 WREG32_SOC15(GC, 0, mmRLC_LB_CNTR_INIT, 0x00000000);
1027 /* set mmRLC_LB_CNTR_MAX = 0x0000_0500 */
1028 WREG32_SOC15(GC, 0, mmRLC_LB_CNTR_MAX, 0x00000800);
1030 mutex_lock(&adev->grbm_idx_mutex);
1031 /* set mmRLC_LB_INIT_CU_MASK thru broadcast mode to enable all SE/SH*/
1032 gfx_v9_0_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
1033 WREG32_SOC15(GC, 0, mmRLC_LB_INIT_CU_MASK, 0xffffffff);
1035 /* set mmRLC_LB_PARAMS = 0x003F_1006 */
1036 data = REG_SET_FIELD(0, RLC_LB_PARAMS, FIFO_SAMPLES, 0x0003);
1037 data |= REG_SET_FIELD(data, RLC_LB_PARAMS, PG_IDLE_SAMPLES, 0x0010);
1038 data |= REG_SET_FIELD(data, RLC_LB_PARAMS, PG_IDLE_SAMPLE_INTERVAL, 0x033F);
1039 WREG32_SOC15(GC, 0, mmRLC_LB_PARAMS, data);
1041 /* set mmRLC_GPM_GENERAL_7[31-16] = 0x00C0 */
1042 data = RREG32_SOC15(GC, 0, mmRLC_GPM_GENERAL_7);
1045 WREG32_SOC15(GC, 0, mmRLC_GPM_GENERAL_7, data);
1048 * RLC_LB_ALWAYS_ACTIVE_CU_MASK = 0xFFF (12 CUs AON),
1049 * programmed in gfx_v9_0_init_always_on_cu_mask()
1052 /* set RLC_LB_CNTL = 0x8000_0095, 31 bit is reserved,
1053 * but used for RLC_LB_CNTL configuration */
1054 data = RLC_LB_CNTL__LB_CNT_SPIM_ACTIVE_MASK;
1055 data |= REG_SET_FIELD(data, RLC_LB_CNTL, CU_MASK_USED_OFF_HYST, 0x09);
1056 data |= REG_SET_FIELD(data, RLC_LB_CNTL, RESERVED, 0x80000);
1057 WREG32_SOC15(GC, 0, mmRLC_LB_CNTL, data);
1058 mutex_unlock(&adev->grbm_idx_mutex);
1060 gfx_v9_0_init_always_on_cu_mask(adev);
1063 static void gfx_v9_0_enable_lbpw(struct amdgpu_device *adev, bool enable)
1065 WREG32_FIELD15(GC, 0, RLC_LB_CNTL, LOAD_BALANCE_ENABLE, enable ? 1 : 0);
1068 static void rv_init_cp_jump_table(struct amdgpu_device *adev)
1070 const __le32 *fw_data;
1071 volatile u32 *dst_ptr;
1072 int me, i, max_me = 5;
1074 u32 table_offset, table_size;
1076 /* write the cp table buffer */
1077 dst_ptr = adev->gfx.rlc.cp_table_ptr;
1078 for (me = 0; me < max_me; me++) {
1080 const struct gfx_firmware_header_v1_0 *hdr =
1081 (const struct gfx_firmware_header_v1_0 *)adev->gfx.ce_fw->data;
1082 fw_data = (const __le32 *)
1083 (adev->gfx.ce_fw->data +
1084 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1085 table_offset = le32_to_cpu(hdr->jt_offset);
1086 table_size = le32_to_cpu(hdr->jt_size);
1087 } else if (me == 1) {
1088 const struct gfx_firmware_header_v1_0 *hdr =
1089 (const struct gfx_firmware_header_v1_0 *)adev->gfx.pfp_fw->data;
1090 fw_data = (const __le32 *)
1091 (adev->gfx.pfp_fw->data +
1092 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1093 table_offset = le32_to_cpu(hdr->jt_offset);
1094 table_size = le32_to_cpu(hdr->jt_size);
1095 } else if (me == 2) {
1096 const struct gfx_firmware_header_v1_0 *hdr =
1097 (const struct gfx_firmware_header_v1_0 *)adev->gfx.me_fw->data;
1098 fw_data = (const __le32 *)
1099 (adev->gfx.me_fw->data +
1100 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1101 table_offset = le32_to_cpu(hdr->jt_offset);
1102 table_size = le32_to_cpu(hdr->jt_size);
1103 } else if (me == 3) {
1104 const struct gfx_firmware_header_v1_0 *hdr =
1105 (const struct gfx_firmware_header_v1_0 *)adev->gfx.mec_fw->data;
1106 fw_data = (const __le32 *)
1107 (adev->gfx.mec_fw->data +
1108 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1109 table_offset = le32_to_cpu(hdr->jt_offset);
1110 table_size = le32_to_cpu(hdr->jt_size);
1111 } else if (me == 4) {
1112 const struct gfx_firmware_header_v1_0 *hdr =
1113 (const struct gfx_firmware_header_v1_0 *)adev->gfx.mec2_fw->data;
1114 fw_data = (const __le32 *)
1115 (adev->gfx.mec2_fw->data +
1116 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1117 table_offset = le32_to_cpu(hdr->jt_offset);
1118 table_size = le32_to_cpu(hdr->jt_size);
1121 for (i = 0; i < table_size; i ++) {
1122 dst_ptr[bo_offset + i] =
1123 cpu_to_le32(le32_to_cpu(fw_data[table_offset + i]));
1126 bo_offset += table_size;
1130 static void gfx_v9_0_rlc_fini(struct amdgpu_device *adev)
1132 /* clear state block */
1133 amdgpu_bo_free_kernel(&adev->gfx.rlc.clear_state_obj,
1134 &adev->gfx.rlc.clear_state_gpu_addr,
1135 (void **)&adev->gfx.rlc.cs_ptr);
1137 /* jump table block */
1138 amdgpu_bo_free_kernel(&adev->gfx.rlc.cp_table_obj,
1139 &adev->gfx.rlc.cp_table_gpu_addr,
1140 (void **)&adev->gfx.rlc.cp_table_ptr);
1143 static int gfx_v9_0_rlc_init(struct amdgpu_device *adev)
1145 volatile u32 *dst_ptr;
1147 const struct cs_section_def *cs_data;
1150 adev->gfx.rlc.cs_data = gfx9_cs_data;
1152 cs_data = adev->gfx.rlc.cs_data;
1155 /* clear state block */
1156 adev->gfx.rlc.clear_state_size = dws = gfx_v9_0_get_csb_size(adev);
1157 r = amdgpu_bo_create_reserved(adev, dws * 4, PAGE_SIZE,
1158 AMDGPU_GEM_DOMAIN_VRAM,
1159 &adev->gfx.rlc.clear_state_obj,
1160 &adev->gfx.rlc.clear_state_gpu_addr,
1161 (void **)&adev->gfx.rlc.cs_ptr);
1163 dev_err(adev->dev, "(%d) failed to create rlc csb bo\n",
1165 gfx_v9_0_rlc_fini(adev);
1168 /* set up the cs buffer */
1169 dst_ptr = adev->gfx.rlc.cs_ptr;
1170 gfx_v9_0_get_csb_buffer(adev, dst_ptr);
1171 amdgpu_bo_kunmap(adev->gfx.rlc.clear_state_obj);
1172 amdgpu_bo_unpin(adev->gfx.rlc.clear_state_obj);
1173 amdgpu_bo_unreserve(adev->gfx.rlc.clear_state_obj);
1176 if (adev->asic_type == CHIP_RAVEN) {
1177 /* TODO: double check the cp_table_size for RV */
1178 adev->gfx.rlc.cp_table_size = ALIGN(96 * 5 * 4, 2048) + (64 * 1024); /* JT + GDS */
1179 r = amdgpu_bo_create_reserved(adev, adev->gfx.rlc.cp_table_size,
1180 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
1181 &adev->gfx.rlc.cp_table_obj,
1182 &adev->gfx.rlc.cp_table_gpu_addr,
1183 (void **)&adev->gfx.rlc.cp_table_ptr);
1186 "(%d) failed to create cp table bo\n", r);
1187 gfx_v9_0_rlc_fini(adev);
1191 rv_init_cp_jump_table(adev);
1192 amdgpu_bo_kunmap(adev->gfx.rlc.cp_table_obj);
1193 amdgpu_bo_unreserve(adev->gfx.rlc.cp_table_obj);
1196 switch (adev->asic_type) {
1198 gfx_v9_0_init_lbpw(adev);
1201 gfx_v9_4_init_lbpw(adev);
1210 static int gfx_v9_0_csb_vram_pin(struct amdgpu_device *adev)
1214 r = amdgpu_bo_reserve(adev->gfx.rlc.clear_state_obj, false);
1215 if (unlikely(r != 0))
1218 r = amdgpu_bo_pin(adev->gfx.rlc.clear_state_obj,
1219 AMDGPU_GEM_DOMAIN_VRAM);
1221 adev->gfx.rlc.clear_state_gpu_addr =
1222 amdgpu_bo_gpu_offset(adev->gfx.rlc.clear_state_obj);
1224 amdgpu_bo_unreserve(adev->gfx.rlc.clear_state_obj);
1229 static void gfx_v9_0_csb_vram_unpin(struct amdgpu_device *adev)
1233 if (!adev->gfx.rlc.clear_state_obj)
1236 r = amdgpu_bo_reserve(adev->gfx.rlc.clear_state_obj, true);
1237 if (likely(r == 0)) {
1238 amdgpu_bo_unpin(adev->gfx.rlc.clear_state_obj);
1239 amdgpu_bo_unreserve(adev->gfx.rlc.clear_state_obj);
1243 static void gfx_v9_0_mec_fini(struct amdgpu_device *adev)
1245 amdgpu_bo_free_kernel(&adev->gfx.mec.hpd_eop_obj, NULL, NULL);
1246 amdgpu_bo_free_kernel(&adev->gfx.mec.mec_fw_obj, NULL, NULL);
1249 static int gfx_v9_0_mec_init(struct amdgpu_device *adev)
1253 const __le32 *fw_data;
1256 size_t mec_hpd_size;
1258 const struct gfx_firmware_header_v1_0 *mec_hdr;
1260 bitmap_zero(adev->gfx.mec.queue_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
1262 /* take ownership of the relevant compute queues */
1263 amdgpu_gfx_compute_queue_acquire(adev);
1264 mec_hpd_size = adev->gfx.num_compute_rings * GFX9_MEC_HPD_SIZE;
1266 r = amdgpu_bo_create_reserved(adev, mec_hpd_size, PAGE_SIZE,
1267 AMDGPU_GEM_DOMAIN_VRAM,
1268 &adev->gfx.mec.hpd_eop_obj,
1269 &adev->gfx.mec.hpd_eop_gpu_addr,
1272 dev_warn(adev->dev, "(%d) create HDP EOP bo failed\n", r);
1273 gfx_v9_0_mec_fini(adev);
1277 memset(hpd, 0, adev->gfx.mec.hpd_eop_obj->tbo.mem.size);
1279 amdgpu_bo_kunmap(adev->gfx.mec.hpd_eop_obj);
1280 amdgpu_bo_unreserve(adev->gfx.mec.hpd_eop_obj);
1282 mec_hdr = (const struct gfx_firmware_header_v1_0 *)adev->gfx.mec_fw->data;
1284 fw_data = (const __le32 *)
1285 (adev->gfx.mec_fw->data +
1286 le32_to_cpu(mec_hdr->header.ucode_array_offset_bytes));
1287 fw_size = le32_to_cpu(mec_hdr->header.ucode_size_bytes) / 4;
1289 r = amdgpu_bo_create_reserved(adev, mec_hdr->header.ucode_size_bytes,
1290 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
1291 &adev->gfx.mec.mec_fw_obj,
1292 &adev->gfx.mec.mec_fw_gpu_addr,
1295 dev_warn(adev->dev, "(%d) create mec firmware bo failed\n", r);
1296 gfx_v9_0_mec_fini(adev);
1300 memcpy(fw, fw_data, fw_size);
1302 amdgpu_bo_kunmap(adev->gfx.mec.mec_fw_obj);
1303 amdgpu_bo_unreserve(adev->gfx.mec.mec_fw_obj);
1308 static uint32_t wave_read_ind(struct amdgpu_device *adev, uint32_t simd, uint32_t wave, uint32_t address)
1310 WREG32_SOC15(GC, 0, mmSQ_IND_INDEX,
1311 (wave << SQ_IND_INDEX__WAVE_ID__SHIFT) |
1312 (simd << SQ_IND_INDEX__SIMD_ID__SHIFT) |
1313 (address << SQ_IND_INDEX__INDEX__SHIFT) |
1314 (SQ_IND_INDEX__FORCE_READ_MASK));
1315 return RREG32_SOC15(GC, 0, mmSQ_IND_DATA);
1318 static void wave_read_regs(struct amdgpu_device *adev, uint32_t simd,
1319 uint32_t wave, uint32_t thread,
1320 uint32_t regno, uint32_t num, uint32_t *out)
1322 WREG32_SOC15(GC, 0, mmSQ_IND_INDEX,
1323 (wave << SQ_IND_INDEX__WAVE_ID__SHIFT) |
1324 (simd << SQ_IND_INDEX__SIMD_ID__SHIFT) |
1325 (regno << SQ_IND_INDEX__INDEX__SHIFT) |
1326 (thread << SQ_IND_INDEX__THREAD_ID__SHIFT) |
1327 (SQ_IND_INDEX__FORCE_READ_MASK) |
1328 (SQ_IND_INDEX__AUTO_INCR_MASK));
1330 *(out++) = RREG32_SOC15(GC, 0, mmSQ_IND_DATA);
1333 static void gfx_v9_0_read_wave_data(struct amdgpu_device *adev, uint32_t simd, uint32_t wave, uint32_t *dst, int *no_fields)
1335 /* type 1 wave data */
1336 dst[(*no_fields)++] = 1;
1337 dst[(*no_fields)++] = wave_read_ind(adev, simd, wave, ixSQ_WAVE_STATUS);
1338 dst[(*no_fields)++] = wave_read_ind(adev, simd, wave, ixSQ_WAVE_PC_LO);
1339 dst[(*no_fields)++] = wave_read_ind(adev, simd, wave, ixSQ_WAVE_PC_HI);
1340 dst[(*no_fields)++] = wave_read_ind(adev, simd, wave, ixSQ_WAVE_EXEC_LO);
1341 dst[(*no_fields)++] = wave_read_ind(adev, simd, wave, ixSQ_WAVE_EXEC_HI);
1342 dst[(*no_fields)++] = wave_read_ind(adev, simd, wave, ixSQ_WAVE_HW_ID);
1343 dst[(*no_fields)++] = wave_read_ind(adev, simd, wave, ixSQ_WAVE_INST_DW0);
1344 dst[(*no_fields)++] = wave_read_ind(adev, simd, wave, ixSQ_WAVE_INST_DW1);
1345 dst[(*no_fields)++] = wave_read_ind(adev, simd, wave, ixSQ_WAVE_GPR_ALLOC);
1346 dst[(*no_fields)++] = wave_read_ind(adev, simd, wave, ixSQ_WAVE_LDS_ALLOC);
1347 dst[(*no_fields)++] = wave_read_ind(adev, simd, wave, ixSQ_WAVE_TRAPSTS);
1348 dst[(*no_fields)++] = wave_read_ind(adev, simd, wave, ixSQ_WAVE_IB_STS);
1349 dst[(*no_fields)++] = wave_read_ind(adev, simd, wave, ixSQ_WAVE_IB_DBG0);
1350 dst[(*no_fields)++] = wave_read_ind(adev, simd, wave, ixSQ_WAVE_M0);
1353 static void gfx_v9_0_read_wave_sgprs(struct amdgpu_device *adev, uint32_t simd,
1354 uint32_t wave, uint32_t start,
1355 uint32_t size, uint32_t *dst)
1358 adev, simd, wave, 0,
1359 start + SQIND_WAVE_SGPRS_OFFSET, size, dst);
1362 static void gfx_v9_0_read_wave_vgprs(struct amdgpu_device *adev, uint32_t simd,
1363 uint32_t wave, uint32_t thread,
1364 uint32_t start, uint32_t size,
1368 adev, simd, wave, thread,
1369 start + SQIND_WAVE_VGPRS_OFFSET, size, dst);
1372 static void gfx_v9_0_select_me_pipe_q(struct amdgpu_device *adev,
1373 u32 me, u32 pipe, u32 q)
1375 soc15_grbm_select(adev, me, pipe, q, 0);
1378 static const struct amdgpu_gfx_funcs gfx_v9_0_gfx_funcs = {
1379 .get_gpu_clock_counter = &gfx_v9_0_get_gpu_clock_counter,
1380 .select_se_sh = &gfx_v9_0_select_se_sh,
1381 .read_wave_data = &gfx_v9_0_read_wave_data,
1382 .read_wave_sgprs = &gfx_v9_0_read_wave_sgprs,
1383 .read_wave_vgprs = &gfx_v9_0_read_wave_vgprs,
1384 .select_me_pipe_q = &gfx_v9_0_select_me_pipe_q
1387 static int gfx_v9_0_gpu_early_init(struct amdgpu_device *adev)
1392 adev->gfx.funcs = &gfx_v9_0_gfx_funcs;
1394 switch (adev->asic_type) {
1396 adev->gfx.config.max_hw_contexts = 8;
1397 adev->gfx.config.sc_prim_fifo_size_frontend = 0x20;
1398 adev->gfx.config.sc_prim_fifo_size_backend = 0x100;
1399 adev->gfx.config.sc_hiz_tile_fifo_size = 0x30;
1400 adev->gfx.config.sc_earlyz_tile_fifo_size = 0x4C0;
1401 gb_addr_config = VEGA10_GB_ADDR_CONFIG_GOLDEN;
1404 adev->gfx.config.max_hw_contexts = 8;
1405 adev->gfx.config.sc_prim_fifo_size_frontend = 0x20;
1406 adev->gfx.config.sc_prim_fifo_size_backend = 0x100;
1407 adev->gfx.config.sc_hiz_tile_fifo_size = 0x30;
1408 adev->gfx.config.sc_earlyz_tile_fifo_size = 0x4C0;
1409 gb_addr_config = VEGA12_GB_ADDR_CONFIG_GOLDEN;
1410 DRM_INFO("fix gfx.config for vega12\n");
1413 adev->gfx.config.max_hw_contexts = 8;
1414 adev->gfx.config.sc_prim_fifo_size_frontend = 0x20;
1415 adev->gfx.config.sc_prim_fifo_size_backend = 0x100;
1416 adev->gfx.config.sc_hiz_tile_fifo_size = 0x30;
1417 adev->gfx.config.sc_earlyz_tile_fifo_size = 0x4C0;
1418 gb_addr_config = RREG32_SOC15(GC, 0, mmGB_ADDR_CONFIG);
1419 gb_addr_config &= ~0xf3e777ff;
1420 gb_addr_config |= 0x22014042;
1421 /* check vbios table if gpu info is not available */
1422 err = amdgpu_atomfirmware_get_gfx_info(adev);
1427 adev->gfx.config.max_hw_contexts = 8;
1428 adev->gfx.config.sc_prim_fifo_size_frontend = 0x20;
1429 adev->gfx.config.sc_prim_fifo_size_backend = 0x100;
1430 adev->gfx.config.sc_hiz_tile_fifo_size = 0x30;
1431 adev->gfx.config.sc_earlyz_tile_fifo_size = 0x4C0;
1432 if (adev->rev_id >= 8)
1433 gb_addr_config = RAVEN2_GB_ADDR_CONFIG_GOLDEN;
1435 gb_addr_config = RAVEN_GB_ADDR_CONFIG_GOLDEN;
1442 adev->gfx.config.gb_addr_config = gb_addr_config;
1444 adev->gfx.config.gb_addr_config_fields.num_pipes = 1 <<
1446 adev->gfx.config.gb_addr_config,
1450 adev->gfx.config.max_tile_pipes =
1451 adev->gfx.config.gb_addr_config_fields.num_pipes;
1453 adev->gfx.config.gb_addr_config_fields.num_banks = 1 <<
1455 adev->gfx.config.gb_addr_config,
1458 adev->gfx.config.gb_addr_config_fields.max_compress_frags = 1 <<
1460 adev->gfx.config.gb_addr_config,
1462 MAX_COMPRESSED_FRAGS);
1463 adev->gfx.config.gb_addr_config_fields.num_rb_per_se = 1 <<
1465 adev->gfx.config.gb_addr_config,
1468 adev->gfx.config.gb_addr_config_fields.num_se = 1 <<
1470 adev->gfx.config.gb_addr_config,
1472 NUM_SHADER_ENGINES);
1473 adev->gfx.config.gb_addr_config_fields.pipe_interleave_size = 1 << (8 +
1475 adev->gfx.config.gb_addr_config,
1477 PIPE_INTERLEAVE_SIZE));
1482 static int gfx_v9_0_ngg_create_buf(struct amdgpu_device *adev,
1483 struct amdgpu_ngg_buf *ngg_buf,
1485 int default_size_se)
1490 dev_err(adev->dev, "Buffer size is invalid: %d\n", size_se);
1493 size_se = size_se ? size_se : default_size_se;
1495 ngg_buf->size = size_se * adev->gfx.config.max_shader_engines;
1496 r = amdgpu_bo_create_kernel(adev, ngg_buf->size,
1497 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
1502 dev_err(adev->dev, "(%d) failed to create NGG buffer\n", r);
1505 ngg_buf->bo_size = amdgpu_bo_size(ngg_buf->bo);
1510 static int gfx_v9_0_ngg_fini(struct amdgpu_device *adev)
1514 for (i = 0; i < NGG_BUF_MAX; i++)
1515 amdgpu_bo_free_kernel(&adev->gfx.ngg.buf[i].bo,
1516 &adev->gfx.ngg.buf[i].gpu_addr,
1519 memset(&adev->gfx.ngg.buf[0], 0,
1520 sizeof(struct amdgpu_ngg_buf) * NGG_BUF_MAX);
1522 adev->gfx.ngg.init = false;
1527 static int gfx_v9_0_ngg_init(struct amdgpu_device *adev)
1531 if (!amdgpu_ngg || adev->gfx.ngg.init == true)
1534 /* GDS reserve memory: 64 bytes alignment */
1535 adev->gfx.ngg.gds_reserve_size = ALIGN(5 * 4, 0x40);
1536 adev->gds.mem.total_size -= adev->gfx.ngg.gds_reserve_size;
1537 adev->gds.mem.gfx_partition_size -= adev->gfx.ngg.gds_reserve_size;
1538 adev->gfx.ngg.gds_reserve_addr = RREG32_SOC15(GC, 0, mmGDS_VMID0_BASE);
1539 adev->gfx.ngg.gds_reserve_addr += RREG32_SOC15(GC, 0, mmGDS_VMID0_SIZE);
1541 /* Primitive Buffer */
1542 r = gfx_v9_0_ngg_create_buf(adev, &adev->gfx.ngg.buf[NGG_PRIM],
1543 amdgpu_prim_buf_per_se,
1546 dev_err(adev->dev, "Failed to create Primitive Buffer\n");
1550 /* Position Buffer */
1551 r = gfx_v9_0_ngg_create_buf(adev, &adev->gfx.ngg.buf[NGG_POS],
1552 amdgpu_pos_buf_per_se,
1555 dev_err(adev->dev, "Failed to create Position Buffer\n");
1559 /* Control Sideband */
1560 r = gfx_v9_0_ngg_create_buf(adev, &adev->gfx.ngg.buf[NGG_CNTL],
1561 amdgpu_cntl_sb_buf_per_se,
1564 dev_err(adev->dev, "Failed to create Control Sideband Buffer\n");
1568 /* Parameter Cache, not created by default */
1569 if (amdgpu_param_buf_per_se <= 0)
1572 r = gfx_v9_0_ngg_create_buf(adev, &adev->gfx.ngg.buf[NGG_PARAM],
1573 amdgpu_param_buf_per_se,
1576 dev_err(adev->dev, "Failed to create Parameter Cache\n");
1581 adev->gfx.ngg.init = true;
1584 gfx_v9_0_ngg_fini(adev);
1588 static int gfx_v9_0_ngg_en(struct amdgpu_device *adev)
1590 struct amdgpu_ring *ring = &adev->gfx.gfx_ring[0];
1597 /* Program buffer size */
1598 data = REG_SET_FIELD(0, WD_BUF_RESOURCE_1, INDEX_BUF_SIZE,
1599 adev->gfx.ngg.buf[NGG_PRIM].size >> 8);
1600 data = REG_SET_FIELD(data, WD_BUF_RESOURCE_1, POS_BUF_SIZE,
1601 adev->gfx.ngg.buf[NGG_POS].size >> 8);
1602 WREG32_SOC15(GC, 0, mmWD_BUF_RESOURCE_1, data);
1604 data = REG_SET_FIELD(0, WD_BUF_RESOURCE_2, CNTL_SB_BUF_SIZE,
1605 adev->gfx.ngg.buf[NGG_CNTL].size >> 8);
1606 data = REG_SET_FIELD(data, WD_BUF_RESOURCE_2, PARAM_BUF_SIZE,
1607 adev->gfx.ngg.buf[NGG_PARAM].size >> 10);
1608 WREG32_SOC15(GC, 0, mmWD_BUF_RESOURCE_2, data);
1610 /* Program buffer base address */
1611 base = lower_32_bits(adev->gfx.ngg.buf[NGG_PRIM].gpu_addr);
1612 data = REG_SET_FIELD(0, WD_INDEX_BUF_BASE, BASE, base);
1613 WREG32_SOC15(GC, 0, mmWD_INDEX_BUF_BASE, data);
1615 base = upper_32_bits(adev->gfx.ngg.buf[NGG_PRIM].gpu_addr);
1616 data = REG_SET_FIELD(0, WD_INDEX_BUF_BASE_HI, BASE_HI, base);
1617 WREG32_SOC15(GC, 0, mmWD_INDEX_BUF_BASE_HI, data);
1619 base = lower_32_bits(adev->gfx.ngg.buf[NGG_POS].gpu_addr);
1620 data = REG_SET_FIELD(0, WD_POS_BUF_BASE, BASE, base);
1621 WREG32_SOC15(GC, 0, mmWD_POS_BUF_BASE, data);
1623 base = upper_32_bits(adev->gfx.ngg.buf[NGG_POS].gpu_addr);
1624 data = REG_SET_FIELD(0, WD_POS_BUF_BASE_HI, BASE_HI, base);
1625 WREG32_SOC15(GC, 0, mmWD_POS_BUF_BASE_HI, data);
1627 base = lower_32_bits(adev->gfx.ngg.buf[NGG_CNTL].gpu_addr);
1628 data = REG_SET_FIELD(0, WD_CNTL_SB_BUF_BASE, BASE, base);
1629 WREG32_SOC15(GC, 0, mmWD_CNTL_SB_BUF_BASE, data);
1631 base = upper_32_bits(adev->gfx.ngg.buf[NGG_CNTL].gpu_addr);
1632 data = REG_SET_FIELD(0, WD_CNTL_SB_BUF_BASE_HI, BASE_HI, base);
1633 WREG32_SOC15(GC, 0, mmWD_CNTL_SB_BUF_BASE_HI, data);
1635 /* Clear GDS reserved memory */
1636 r = amdgpu_ring_alloc(ring, 17);
1638 DRM_ERROR("amdgpu: NGG failed to lock ring %d (%d).\n",
1643 gfx_v9_0_write_data_to_reg(ring, 0, false,
1644 SOC15_REG_OFFSET(GC, 0, mmGDS_VMID0_SIZE),
1645 (adev->gds.mem.total_size +
1646 adev->gfx.ngg.gds_reserve_size));
1648 amdgpu_ring_write(ring, PACKET3(PACKET3_DMA_DATA, 5));
1649 amdgpu_ring_write(ring, (PACKET3_DMA_DATA_CP_SYNC |
1650 PACKET3_DMA_DATA_DST_SEL(1) |
1651 PACKET3_DMA_DATA_SRC_SEL(2)));
1652 amdgpu_ring_write(ring, 0);
1653 amdgpu_ring_write(ring, 0);
1654 amdgpu_ring_write(ring, adev->gfx.ngg.gds_reserve_addr);
1655 amdgpu_ring_write(ring, 0);
1656 amdgpu_ring_write(ring, PACKET3_DMA_DATA_CMD_RAW_WAIT |
1657 adev->gfx.ngg.gds_reserve_size);
1659 gfx_v9_0_write_data_to_reg(ring, 0, false,
1660 SOC15_REG_OFFSET(GC, 0, mmGDS_VMID0_SIZE), 0);
1662 amdgpu_ring_commit(ring);
1667 static int gfx_v9_0_compute_ring_init(struct amdgpu_device *adev, int ring_id,
1668 int mec, int pipe, int queue)
1672 struct amdgpu_ring *ring = &adev->gfx.compute_ring[ring_id];
1674 ring = &adev->gfx.compute_ring[ring_id];
1679 ring->queue = queue;
1681 ring->ring_obj = NULL;
1682 ring->use_doorbell = true;
1683 ring->doorbell_index = (AMDGPU_DOORBELL_MEC_RING0 + ring_id) << 1;
1684 ring->eop_gpu_addr = adev->gfx.mec.hpd_eop_gpu_addr
1685 + (ring_id * GFX9_MEC_HPD_SIZE);
1686 sprintf(ring->name, "comp_%d.%d.%d", ring->me, ring->pipe, ring->queue);
1688 irq_type = AMDGPU_CP_IRQ_COMPUTE_MEC1_PIPE0_EOP
1689 + ((ring->me - 1) * adev->gfx.mec.num_pipe_per_mec)
1692 /* type-2 packets are deprecated on MEC, use type-3 instead */
1693 r = amdgpu_ring_init(adev, ring, 1024,
1694 &adev->gfx.eop_irq, irq_type);
1702 static int gfx_v9_0_sw_init(void *handle)
1704 int i, j, k, r, ring_id;
1705 struct amdgpu_ring *ring;
1706 struct amdgpu_kiq *kiq;
1707 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1709 switch (adev->asic_type) {
1714 adev->gfx.mec.num_mec = 2;
1717 adev->gfx.mec.num_mec = 1;
1721 adev->gfx.mec.num_pipe_per_mec = 4;
1722 adev->gfx.mec.num_queue_per_pipe = 8;
1725 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_GRBM_CP, GFX_9_0__SRCID__CP_EOP_INTERRUPT, &adev->gfx.eop_irq);
1729 /* Privileged reg */
1730 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_GRBM_CP, GFX_9_0__SRCID__CP_PRIV_REG_FAULT,
1731 &adev->gfx.priv_reg_irq);
1735 /* Privileged inst */
1736 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_GRBM_CP, GFX_9_0__SRCID__CP_PRIV_INSTR_FAULT,
1737 &adev->gfx.priv_inst_irq);
1741 adev->gfx.gfx_current_status = AMDGPU_GFX_NORMAL_MODE;
1743 gfx_v9_0_scratch_init(adev);
1745 r = gfx_v9_0_init_microcode(adev);
1747 DRM_ERROR("Failed to load gfx firmware!\n");
1751 r = gfx_v9_0_rlc_init(adev);
1753 DRM_ERROR("Failed to init rlc BOs!\n");
1757 r = gfx_v9_0_mec_init(adev);
1759 DRM_ERROR("Failed to init MEC BOs!\n");
1763 /* set up the gfx ring */
1764 for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
1765 ring = &adev->gfx.gfx_ring[i];
1766 ring->ring_obj = NULL;
1768 sprintf(ring->name, "gfx");
1770 sprintf(ring->name, "gfx_%d", i);
1771 ring->use_doorbell = true;
1772 ring->doorbell_index = AMDGPU_DOORBELL64_GFX_RING0 << 1;
1773 r = amdgpu_ring_init(adev, ring, 1024,
1774 &adev->gfx.eop_irq, AMDGPU_CP_IRQ_GFX_EOP);
1779 /* set up the compute queues - allocate horizontally across pipes */
1781 for (i = 0; i < adev->gfx.mec.num_mec; ++i) {
1782 for (j = 0; j < adev->gfx.mec.num_queue_per_pipe; j++) {
1783 for (k = 0; k < adev->gfx.mec.num_pipe_per_mec; k++) {
1784 if (!amdgpu_gfx_is_mec_queue_enabled(adev, i, k, j))
1787 r = gfx_v9_0_compute_ring_init(adev,
1798 r = amdgpu_gfx_kiq_init(adev, GFX9_MEC_HPD_SIZE);
1800 DRM_ERROR("Failed to init KIQ BOs!\n");
1804 kiq = &adev->gfx.kiq;
1805 r = amdgpu_gfx_kiq_init_ring(adev, &kiq->ring, &kiq->irq);
1809 /* create MQD for all compute queues as wel as KIQ for SRIOV case */
1810 r = amdgpu_gfx_compute_mqd_sw_init(adev, sizeof(struct v9_mqd_allocation));
1814 adev->gfx.ce_ram_size = 0x8000;
1816 r = gfx_v9_0_gpu_early_init(adev);
1820 r = gfx_v9_0_ngg_init(adev);
1828 static int gfx_v9_0_sw_fini(void *handle)
1831 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1833 amdgpu_bo_free_kernel(&adev->gds.oa_gfx_bo, NULL, NULL);
1834 amdgpu_bo_free_kernel(&adev->gds.gws_gfx_bo, NULL, NULL);
1835 amdgpu_bo_free_kernel(&adev->gds.gds_gfx_bo, NULL, NULL);
1837 for (i = 0; i < adev->gfx.num_gfx_rings; i++)
1838 amdgpu_ring_fini(&adev->gfx.gfx_ring[i]);
1839 for (i = 0; i < adev->gfx.num_compute_rings; i++)
1840 amdgpu_ring_fini(&adev->gfx.compute_ring[i]);
1842 amdgpu_gfx_compute_mqd_sw_fini(adev);
1843 amdgpu_gfx_kiq_free_ring(&adev->gfx.kiq.ring, &adev->gfx.kiq.irq);
1844 amdgpu_gfx_kiq_fini(adev);
1846 gfx_v9_0_mec_fini(adev);
1847 gfx_v9_0_ngg_fini(adev);
1848 amdgpu_bo_free_kernel(&adev->gfx.rlc.clear_state_obj,
1849 &adev->gfx.rlc.clear_state_gpu_addr,
1850 (void **)&adev->gfx.rlc.cs_ptr);
1851 if (adev->asic_type == CHIP_RAVEN) {
1852 amdgpu_bo_free_kernel(&adev->gfx.rlc.cp_table_obj,
1853 &adev->gfx.rlc.cp_table_gpu_addr,
1854 (void **)&adev->gfx.rlc.cp_table_ptr);
1856 gfx_v9_0_free_microcode(adev);
1862 static void gfx_v9_0_tiling_mode_table_init(struct amdgpu_device *adev)
1867 static void gfx_v9_0_select_se_sh(struct amdgpu_device *adev, u32 se_num, u32 sh_num, u32 instance)
1871 if (instance == 0xffffffff)
1872 data = REG_SET_FIELD(0, GRBM_GFX_INDEX, INSTANCE_BROADCAST_WRITES, 1);
1874 data = REG_SET_FIELD(0, GRBM_GFX_INDEX, INSTANCE_INDEX, instance);
1876 if (se_num == 0xffffffff)
1877 data = REG_SET_FIELD(data, GRBM_GFX_INDEX, SE_BROADCAST_WRITES, 1);
1879 data = REG_SET_FIELD(data, GRBM_GFX_INDEX, SE_INDEX, se_num);
1881 if (sh_num == 0xffffffff)
1882 data = REG_SET_FIELD(data, GRBM_GFX_INDEX, SH_BROADCAST_WRITES, 1);
1884 data = REG_SET_FIELD(data, GRBM_GFX_INDEX, SH_INDEX, sh_num);
1886 WREG32_SOC15(GC, 0, mmGRBM_GFX_INDEX, data);
1889 static u32 gfx_v9_0_get_rb_active_bitmap(struct amdgpu_device *adev)
1893 data = RREG32_SOC15(GC, 0, mmCC_RB_BACKEND_DISABLE);
1894 data |= RREG32_SOC15(GC, 0, mmGC_USER_RB_BACKEND_DISABLE);
1896 data &= CC_RB_BACKEND_DISABLE__BACKEND_DISABLE_MASK;
1897 data >>= GC_USER_RB_BACKEND_DISABLE__BACKEND_DISABLE__SHIFT;
1899 mask = amdgpu_gfx_create_bitmask(adev->gfx.config.max_backends_per_se /
1900 adev->gfx.config.max_sh_per_se);
1902 return (~data) & mask;
1905 static void gfx_v9_0_setup_rb(struct amdgpu_device *adev)
1910 u32 rb_bitmap_width_per_sh = adev->gfx.config.max_backends_per_se /
1911 adev->gfx.config.max_sh_per_se;
1913 mutex_lock(&adev->grbm_idx_mutex);
1914 for (i = 0; i < adev->gfx.config.max_shader_engines; i++) {
1915 for (j = 0; j < adev->gfx.config.max_sh_per_se; j++) {
1916 gfx_v9_0_select_se_sh(adev, i, j, 0xffffffff);
1917 data = gfx_v9_0_get_rb_active_bitmap(adev);
1918 active_rbs |= data << ((i * adev->gfx.config.max_sh_per_se + j) *
1919 rb_bitmap_width_per_sh);
1922 gfx_v9_0_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
1923 mutex_unlock(&adev->grbm_idx_mutex);
1925 adev->gfx.config.backend_enable_mask = active_rbs;
1926 adev->gfx.config.num_rbs = hweight32(active_rbs);
1929 #define DEFAULT_SH_MEM_BASES (0x6000)
1930 #define FIRST_COMPUTE_VMID (8)
1931 #define LAST_COMPUTE_VMID (16)
1932 static void gfx_v9_0_init_compute_vmid(struct amdgpu_device *adev)
1935 uint32_t sh_mem_config;
1936 uint32_t sh_mem_bases;
1939 * Configure apertures:
1940 * LDS: 0x60000000'00000000 - 0x60000001'00000000 (4GB)
1941 * Scratch: 0x60000001'00000000 - 0x60000002'00000000 (4GB)
1942 * GPUVM: 0x60010000'00000000 - 0x60020000'00000000 (1TB)
1944 sh_mem_bases = DEFAULT_SH_MEM_BASES | (DEFAULT_SH_MEM_BASES << 16);
1946 sh_mem_config = SH_MEM_ADDRESS_MODE_64 |
1947 SH_MEM_ALIGNMENT_MODE_UNALIGNED <<
1948 SH_MEM_CONFIG__ALIGNMENT_MODE__SHIFT;
1950 mutex_lock(&adev->srbm_mutex);
1951 for (i = FIRST_COMPUTE_VMID; i < LAST_COMPUTE_VMID; i++) {
1952 soc15_grbm_select(adev, 0, 0, 0, i);
1953 /* CP and shaders */
1954 WREG32_SOC15(GC, 0, mmSH_MEM_CONFIG, sh_mem_config);
1955 WREG32_SOC15(GC, 0, mmSH_MEM_BASES, sh_mem_bases);
1957 soc15_grbm_select(adev, 0, 0, 0, 0);
1958 mutex_unlock(&adev->srbm_mutex);
1961 static void gfx_v9_0_constants_init(struct amdgpu_device *adev)
1966 WREG32_FIELD15(GC, 0, GRBM_CNTL, READ_TIMEOUT, 0xff);
1968 gfx_v9_0_tiling_mode_table_init(adev);
1970 gfx_v9_0_setup_rb(adev);
1971 gfx_v9_0_get_cu_info(adev, &adev->gfx.cu_info);
1972 adev->gfx.config.db_debug2 = RREG32_SOC15(GC, 0, mmDB_DEBUG2);
1974 /* XXX SH_MEM regs */
1975 /* where to put LDS, scratch, GPUVM in FSA64 space */
1976 mutex_lock(&adev->srbm_mutex);
1977 for (i = 0; i < adev->vm_manager.id_mgr[AMDGPU_GFXHUB].num_ids; i++) {
1978 soc15_grbm_select(adev, 0, 0, 0, i);
1979 /* CP and shaders */
1981 tmp = REG_SET_FIELD(0, SH_MEM_CONFIG, ALIGNMENT_MODE,
1982 SH_MEM_ALIGNMENT_MODE_UNALIGNED);
1983 WREG32_SOC15(GC, 0, mmSH_MEM_CONFIG, tmp);
1984 WREG32_SOC15(GC, 0, mmSH_MEM_BASES, 0);
1986 tmp = REG_SET_FIELD(0, SH_MEM_CONFIG, ALIGNMENT_MODE,
1987 SH_MEM_ALIGNMENT_MODE_UNALIGNED);
1988 WREG32_SOC15(GC, 0, mmSH_MEM_CONFIG, tmp);
1989 tmp = REG_SET_FIELD(0, SH_MEM_BASES, PRIVATE_BASE,
1990 (adev->gmc.private_aperture_start >> 48));
1991 tmp = REG_SET_FIELD(tmp, SH_MEM_BASES, SHARED_BASE,
1992 (adev->gmc.shared_aperture_start >> 48));
1993 WREG32_SOC15(GC, 0, mmSH_MEM_BASES, tmp);
1996 soc15_grbm_select(adev, 0, 0, 0, 0);
1998 mutex_unlock(&adev->srbm_mutex);
2000 gfx_v9_0_init_compute_vmid(adev);
2002 mutex_lock(&adev->grbm_idx_mutex);
2004 * making sure that the following register writes will be broadcasted
2005 * to all the shaders
2007 gfx_v9_0_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
2009 WREG32_SOC15(GC, 0, mmPA_SC_FIFO_SIZE,
2010 (adev->gfx.config.sc_prim_fifo_size_frontend <<
2011 PA_SC_FIFO_SIZE__SC_FRONTEND_PRIM_FIFO_SIZE__SHIFT) |
2012 (adev->gfx.config.sc_prim_fifo_size_backend <<
2013 PA_SC_FIFO_SIZE__SC_BACKEND_PRIM_FIFO_SIZE__SHIFT) |
2014 (adev->gfx.config.sc_hiz_tile_fifo_size <<
2015 PA_SC_FIFO_SIZE__SC_HIZ_TILE_FIFO_SIZE__SHIFT) |
2016 (adev->gfx.config.sc_earlyz_tile_fifo_size <<
2017 PA_SC_FIFO_SIZE__SC_EARLYZ_TILE_FIFO_SIZE__SHIFT));
2018 mutex_unlock(&adev->grbm_idx_mutex);
2022 static void gfx_v9_0_wait_for_rlc_serdes(struct amdgpu_device *adev)
2027 mutex_lock(&adev->grbm_idx_mutex);
2028 for (i = 0; i < adev->gfx.config.max_shader_engines; i++) {
2029 for (j = 0; j < adev->gfx.config.max_sh_per_se; j++) {
2030 gfx_v9_0_select_se_sh(adev, i, j, 0xffffffff);
2031 for (k = 0; k < adev->usec_timeout; k++) {
2032 if (RREG32_SOC15(GC, 0, mmRLC_SERDES_CU_MASTER_BUSY) == 0)
2036 if (k == adev->usec_timeout) {
2037 gfx_v9_0_select_se_sh(adev, 0xffffffff,
2038 0xffffffff, 0xffffffff);
2039 mutex_unlock(&adev->grbm_idx_mutex);
2040 DRM_INFO("Timeout wait for RLC serdes %u,%u\n",
2046 gfx_v9_0_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
2047 mutex_unlock(&adev->grbm_idx_mutex);
2049 mask = RLC_SERDES_NONCU_MASTER_BUSY__SE_MASTER_BUSY_MASK |
2050 RLC_SERDES_NONCU_MASTER_BUSY__GC_MASTER_BUSY_MASK |
2051 RLC_SERDES_NONCU_MASTER_BUSY__TC0_MASTER_BUSY_MASK |
2052 RLC_SERDES_NONCU_MASTER_BUSY__TC1_MASTER_BUSY_MASK;
2053 for (k = 0; k < adev->usec_timeout; k++) {
2054 if ((RREG32_SOC15(GC, 0, mmRLC_SERDES_NONCU_MASTER_BUSY) & mask) == 0)
2060 static void gfx_v9_0_enable_gui_idle_interrupt(struct amdgpu_device *adev,
2063 u32 tmp = RREG32_SOC15(GC, 0, mmCP_INT_CNTL_RING0);
2065 tmp = REG_SET_FIELD(tmp, CP_INT_CNTL_RING0, CNTX_BUSY_INT_ENABLE, enable ? 1 : 0);
2066 tmp = REG_SET_FIELD(tmp, CP_INT_CNTL_RING0, CNTX_EMPTY_INT_ENABLE, enable ? 1 : 0);
2067 tmp = REG_SET_FIELD(tmp, CP_INT_CNTL_RING0, CMP_BUSY_INT_ENABLE, enable ? 1 : 0);
2068 tmp = REG_SET_FIELD(tmp, CP_INT_CNTL_RING0, GFX_IDLE_INT_ENABLE, enable ? 1 : 0);
2070 WREG32_SOC15(GC, 0, mmCP_INT_CNTL_RING0, tmp);
2073 static void gfx_v9_0_init_csb(struct amdgpu_device *adev)
2076 WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_CSIB_ADDR_HI),
2077 adev->gfx.rlc.clear_state_gpu_addr >> 32);
2078 WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_CSIB_ADDR_LO),
2079 adev->gfx.rlc.clear_state_gpu_addr & 0xfffffffc);
2080 WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_CSIB_LENGTH),
2081 adev->gfx.rlc.clear_state_size);
2084 static void gfx_v9_1_parse_ind_reg_list(int *register_list_format,
2085 int indirect_offset,
2087 int *unique_indirect_regs,
2088 int unique_indirect_reg_count,
2089 int *indirect_start_offsets,
2090 int *indirect_start_offsets_count,
2091 int max_start_offsets_count)
2095 for (; indirect_offset < list_size; indirect_offset++) {
2096 WARN_ON(*indirect_start_offsets_count >= max_start_offsets_count);
2097 indirect_start_offsets[*indirect_start_offsets_count] = indirect_offset;
2098 *indirect_start_offsets_count = *indirect_start_offsets_count + 1;
2100 while (register_list_format[indirect_offset] != 0xFFFFFFFF) {
2101 indirect_offset += 2;
2103 /* look for the matching indice */
2104 for (idx = 0; idx < unique_indirect_reg_count; idx++) {
2105 if (unique_indirect_regs[idx] ==
2106 register_list_format[indirect_offset] ||
2107 !unique_indirect_regs[idx])
2111 BUG_ON(idx >= unique_indirect_reg_count);
2113 if (!unique_indirect_regs[idx])
2114 unique_indirect_regs[idx] = register_list_format[indirect_offset];
2121 static int gfx_v9_1_init_rlc_save_restore_list(struct amdgpu_device *adev)
2123 int unique_indirect_regs[] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
2124 int unique_indirect_reg_count = 0;
2126 int indirect_start_offsets[] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
2127 int indirect_start_offsets_count = 0;
2133 u32 *register_list_format =
2134 kmalloc(adev->gfx.rlc.reg_list_format_size_bytes, GFP_KERNEL);
2135 if (!register_list_format)
2137 memcpy(register_list_format, adev->gfx.rlc.register_list_format,
2138 adev->gfx.rlc.reg_list_format_size_bytes);
2140 /* setup unique_indirect_regs array and indirect_start_offsets array */
2141 unique_indirect_reg_count = ARRAY_SIZE(unique_indirect_regs);
2142 gfx_v9_1_parse_ind_reg_list(register_list_format,
2143 adev->gfx.rlc.reg_list_format_direct_reg_list_length,
2144 adev->gfx.rlc.reg_list_format_size_bytes >> 2,
2145 unique_indirect_regs,
2146 unique_indirect_reg_count,
2147 indirect_start_offsets,
2148 &indirect_start_offsets_count,
2149 ARRAY_SIZE(indirect_start_offsets));
2151 /* enable auto inc in case it is disabled */
2152 tmp = RREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_SRM_CNTL));
2153 tmp |= RLC_SRM_CNTL__AUTO_INCR_ADDR_MASK;
2154 WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_SRM_CNTL), tmp);
2156 /* write register_restore table to offset 0x0 using RLC_SRM_ARAM_ADDR/DATA */
2157 WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_SRM_ARAM_ADDR),
2158 RLC_SAVE_RESTORE_ADDR_STARTING_OFFSET);
2159 for (i = 0; i < adev->gfx.rlc.reg_list_size_bytes >> 2; i++)
2160 WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_SRM_ARAM_DATA),
2161 adev->gfx.rlc.register_restore[i]);
2163 /* load indirect register */
2164 WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_GPM_SCRATCH_ADDR),
2165 adev->gfx.rlc.reg_list_format_start);
2167 /* direct register portion */
2168 for (i = 0; i < adev->gfx.rlc.reg_list_format_direct_reg_list_length; i++)
2169 WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_GPM_SCRATCH_DATA),
2170 register_list_format[i]);
2172 /* indirect register portion */
2173 while (i < (adev->gfx.rlc.reg_list_format_size_bytes >> 2)) {
2174 if (register_list_format[i] == 0xFFFFFFFF) {
2175 WREG32_SOC15(GC, 0, mmRLC_GPM_SCRATCH_DATA, register_list_format[i++]);
2179 WREG32_SOC15(GC, 0, mmRLC_GPM_SCRATCH_DATA, register_list_format[i++]);
2180 WREG32_SOC15(GC, 0, mmRLC_GPM_SCRATCH_DATA, register_list_format[i++]);
2182 for (j = 0; j < unique_indirect_reg_count; j++) {
2183 if (register_list_format[i] == unique_indirect_regs[j]) {
2184 WREG32_SOC15(GC, 0, mmRLC_GPM_SCRATCH_DATA, j);
2189 BUG_ON(j >= unique_indirect_reg_count);
2194 /* set save/restore list size */
2195 list_size = adev->gfx.rlc.reg_list_size_bytes >> 2;
2196 list_size = list_size >> 1;
2197 WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_GPM_SCRATCH_ADDR),
2198 adev->gfx.rlc.reg_restore_list_size);
2199 WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_GPM_SCRATCH_DATA), list_size);
2201 /* write the starting offsets to RLC scratch ram */
2202 WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_GPM_SCRATCH_ADDR),
2203 adev->gfx.rlc.starting_offsets_start);
2204 for (i = 0; i < ARRAY_SIZE(indirect_start_offsets); i++)
2205 WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_GPM_SCRATCH_DATA),
2206 indirect_start_offsets[i]);
2208 /* load unique indirect regs*/
2209 for (i = 0; i < ARRAY_SIZE(unique_indirect_regs); i++) {
2210 if (unique_indirect_regs[i] != 0) {
2211 WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_SRM_INDEX_CNTL_ADDR_0)
2212 + GFX_RLC_SRM_INDEX_CNTL_ADDR_OFFSETS[i],
2213 unique_indirect_regs[i] & 0x3FFFF);
2215 WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_SRM_INDEX_CNTL_DATA_0)
2216 + GFX_RLC_SRM_INDEX_CNTL_DATA_OFFSETS[i],
2217 unique_indirect_regs[i] >> 20);
2221 kfree(register_list_format);
2225 static void gfx_v9_0_enable_save_restore_machine(struct amdgpu_device *adev)
2227 WREG32_FIELD15(GC, 0, RLC_SRM_CNTL, SRM_ENABLE, 1);
2230 static void pwr_10_0_gfxip_control_over_cgpg(struct amdgpu_device *adev,
2234 uint32_t default_data = 0;
2236 default_data = data = RREG32(SOC15_REG_OFFSET(PWR, 0, mmPWR_MISC_CNTL_STATUS));
2237 if (enable == true) {
2238 /* enable GFXIP control over CGPG */
2239 data |= PWR_MISC_CNTL_STATUS__PWR_GFX_RLC_CGPG_EN_MASK;
2240 if(default_data != data)
2241 WREG32(SOC15_REG_OFFSET(PWR, 0, mmPWR_MISC_CNTL_STATUS), data);
2244 data &= ~PWR_MISC_CNTL_STATUS__PWR_GFXOFF_STATUS_MASK;
2245 data |= (2 << PWR_MISC_CNTL_STATUS__PWR_GFXOFF_STATUS__SHIFT);
2246 if(default_data != data)
2247 WREG32(SOC15_REG_OFFSET(PWR, 0, mmPWR_MISC_CNTL_STATUS), data);
2249 /* restore GFXIP control over GCPG */
2250 data &= ~PWR_MISC_CNTL_STATUS__PWR_GFX_RLC_CGPG_EN_MASK;
2251 if(default_data != data)
2252 WREG32(SOC15_REG_OFFSET(PWR, 0, mmPWR_MISC_CNTL_STATUS), data);
2256 static void gfx_v9_0_init_gfx_power_gating(struct amdgpu_device *adev)
2260 if (adev->pg_flags & (AMD_PG_SUPPORT_GFX_PG |
2261 AMD_PG_SUPPORT_GFX_SMG |
2262 AMD_PG_SUPPORT_GFX_DMG)) {
2263 /* init IDLE_POLL_COUNT = 60 */
2264 data = RREG32(SOC15_REG_OFFSET(GC, 0, mmCP_RB_WPTR_POLL_CNTL));
2265 data &= ~CP_RB_WPTR_POLL_CNTL__IDLE_POLL_COUNT_MASK;
2266 data |= (0x60 << CP_RB_WPTR_POLL_CNTL__IDLE_POLL_COUNT__SHIFT);
2267 WREG32(SOC15_REG_OFFSET(GC, 0, mmCP_RB_WPTR_POLL_CNTL), data);
2269 /* init RLC PG Delay */
2271 data |= (0x10 << RLC_PG_DELAY__POWER_UP_DELAY__SHIFT);
2272 data |= (0x10 << RLC_PG_DELAY__POWER_DOWN_DELAY__SHIFT);
2273 data |= (0x10 << RLC_PG_DELAY__CMD_PROPAGATE_DELAY__SHIFT);
2274 data |= (0x40 << RLC_PG_DELAY__MEM_SLEEP_DELAY__SHIFT);
2275 WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_DELAY), data);
2277 data = RREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_DELAY_2));
2278 data &= ~RLC_PG_DELAY_2__SERDES_CMD_DELAY_MASK;
2279 data |= (0x4 << RLC_PG_DELAY_2__SERDES_CMD_DELAY__SHIFT);
2280 WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_DELAY_2), data);
2282 data = RREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_DELAY_3));
2283 data &= ~RLC_PG_DELAY_3__CGCG_ACTIVE_BEFORE_CGPG_MASK;
2284 data |= (0xff << RLC_PG_DELAY_3__CGCG_ACTIVE_BEFORE_CGPG__SHIFT);
2285 WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_DELAY_3), data);
2287 data = RREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_AUTO_PG_CTRL));
2288 data &= ~RLC_AUTO_PG_CTRL__GRBM_REG_SAVE_GFX_IDLE_THRESHOLD_MASK;
2290 /* program GRBM_REG_SAVE_GFX_IDLE_THRESHOLD to 0x55f0 */
2291 data |= (0x55f0 << RLC_AUTO_PG_CTRL__GRBM_REG_SAVE_GFX_IDLE_THRESHOLD__SHIFT);
2292 WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_AUTO_PG_CTRL), data);
2294 pwr_10_0_gfxip_control_over_cgpg(adev, true);
2298 static void gfx_v9_0_enable_sck_slow_down_on_power_up(struct amdgpu_device *adev,
2302 uint32_t default_data = 0;
2304 default_data = data = RREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_CNTL));
2305 data = REG_SET_FIELD(data, RLC_PG_CNTL,
2306 SMU_CLK_SLOWDOWN_ON_PU_ENABLE,
2308 if (default_data != data)
2309 WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_CNTL), data);
2312 static void gfx_v9_0_enable_sck_slow_down_on_power_down(struct amdgpu_device *adev,
2316 uint32_t default_data = 0;
2318 default_data = data = RREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_CNTL));
2319 data = REG_SET_FIELD(data, RLC_PG_CNTL,
2320 SMU_CLK_SLOWDOWN_ON_PD_ENABLE,
2322 if(default_data != data)
2323 WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_CNTL), data);
2326 static void gfx_v9_0_enable_cp_power_gating(struct amdgpu_device *adev,
2330 uint32_t default_data = 0;
2332 default_data = data = RREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_CNTL));
2333 data = REG_SET_FIELD(data, RLC_PG_CNTL,
2336 if(default_data != data)
2337 WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_CNTL), data);
2340 static void gfx_v9_0_enable_gfx_cg_power_gating(struct amdgpu_device *adev,
2343 uint32_t data, default_data;
2345 default_data = data = RREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_CNTL));
2346 data = REG_SET_FIELD(data, RLC_PG_CNTL,
2347 GFX_POWER_GATING_ENABLE,
2349 if(default_data != data)
2350 WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_CNTL), data);
2353 static void gfx_v9_0_enable_gfx_pipeline_powergating(struct amdgpu_device *adev,
2356 uint32_t data, default_data;
2358 default_data = data = RREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_CNTL));
2359 data = REG_SET_FIELD(data, RLC_PG_CNTL,
2360 GFX_PIPELINE_PG_ENABLE,
2362 if(default_data != data)
2363 WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_CNTL), data);
2366 /* read any GFX register to wake up GFX */
2367 data = RREG32(SOC15_REG_OFFSET(GC, 0, mmDB_RENDER_CONTROL));
2370 static void gfx_v9_0_enable_gfx_static_mg_power_gating(struct amdgpu_device *adev,
2373 uint32_t data, default_data;
2375 default_data = data = RREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_CNTL));
2376 data = REG_SET_FIELD(data, RLC_PG_CNTL,
2377 STATIC_PER_CU_PG_ENABLE,
2379 if(default_data != data)
2380 WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_CNTL), data);
2383 static void gfx_v9_0_enable_gfx_dynamic_mg_power_gating(struct amdgpu_device *adev,
2386 uint32_t data, default_data;
2388 default_data = data = RREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_CNTL));
2389 data = REG_SET_FIELD(data, RLC_PG_CNTL,
2390 DYN_PER_CU_PG_ENABLE,
2392 if(default_data != data)
2393 WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_PG_CNTL), data);
2396 static void gfx_v9_0_init_pg(struct amdgpu_device *adev)
2398 gfx_v9_0_init_csb(adev);
2401 * Rlc save restore list is workable since v2_1.
2402 * And it's needed by gfxoff feature.
2404 if (adev->gfx.rlc.is_rlc_v2_1) {
2405 gfx_v9_1_init_rlc_save_restore_list(adev);
2406 gfx_v9_0_enable_save_restore_machine(adev);
2409 if (adev->pg_flags & (AMD_PG_SUPPORT_GFX_PG |
2410 AMD_PG_SUPPORT_GFX_SMG |
2411 AMD_PG_SUPPORT_GFX_DMG |
2413 AMD_PG_SUPPORT_GDS |
2414 AMD_PG_SUPPORT_RLC_SMU_HS)) {
2415 WREG32(mmRLC_JUMP_TABLE_RESTORE,
2416 adev->gfx.rlc.cp_table_gpu_addr >> 8);
2417 gfx_v9_0_init_gfx_power_gating(adev);
2421 void gfx_v9_0_rlc_stop(struct amdgpu_device *adev)
2423 WREG32_FIELD15(GC, 0, RLC_CNTL, RLC_ENABLE_F32, 0);
2424 gfx_v9_0_enable_gui_idle_interrupt(adev, false);
2425 gfx_v9_0_wait_for_rlc_serdes(adev);
2428 static void gfx_v9_0_rlc_reset(struct amdgpu_device *adev)
2430 WREG32_FIELD15(GC, 0, GRBM_SOFT_RESET, SOFT_RESET_RLC, 1);
2432 WREG32_FIELD15(GC, 0, GRBM_SOFT_RESET, SOFT_RESET_RLC, 0);
2436 static void gfx_v9_0_rlc_start(struct amdgpu_device *adev)
2438 #ifdef AMDGPU_RLC_DEBUG_RETRY
2442 WREG32_FIELD15(GC, 0, RLC_CNTL, RLC_ENABLE_F32, 1);
2444 /* carrizo do enable cp interrupt after cp inited */
2445 if (!(adev->flags & AMD_IS_APU))
2446 gfx_v9_0_enable_gui_idle_interrupt(adev, true);
2450 #ifdef AMDGPU_RLC_DEBUG_RETRY
2451 /* RLC_GPM_GENERAL_6 : RLC Ucode version */
2452 rlc_ucode_ver = RREG32_SOC15(GC, 0, mmRLC_GPM_GENERAL_6);
2453 if(rlc_ucode_ver == 0x108) {
2454 DRM_INFO("Using rlc debug ucode. mmRLC_GPM_GENERAL_6 ==0x08%x / fw_ver == %i \n",
2455 rlc_ucode_ver, adev->gfx.rlc_fw_version);
2456 /* RLC_GPM_TIMER_INT_3 : Timer interval in RefCLK cycles,
2457 * default is 0x9C4 to create a 100us interval */
2458 WREG32_SOC15(GC, 0, mmRLC_GPM_TIMER_INT_3, 0x9C4);
2459 /* RLC_GPM_GENERAL_12 : Minimum gap between wptr and rptr
2460 * to disable the page fault retry interrupts, default is
2462 WREG32_SOC15(GC, 0, mmRLC_GPM_GENERAL_12, 0x100);
2467 static int gfx_v9_0_rlc_load_microcode(struct amdgpu_device *adev)
2469 const struct rlc_firmware_header_v2_0 *hdr;
2470 const __le32 *fw_data;
2471 unsigned i, fw_size;
2473 if (!adev->gfx.rlc_fw)
2476 hdr = (const struct rlc_firmware_header_v2_0 *)adev->gfx.rlc_fw->data;
2477 amdgpu_ucode_print_rlc_hdr(&hdr->header);
2479 fw_data = (const __le32 *)(adev->gfx.rlc_fw->data +
2480 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
2481 fw_size = le32_to_cpu(hdr->header.ucode_size_bytes) / 4;
2483 WREG32_SOC15(GC, 0, mmRLC_GPM_UCODE_ADDR,
2484 RLCG_UCODE_LOADING_START_ADDRESS);
2485 for (i = 0; i < fw_size; i++)
2486 WREG32_SOC15(GC, 0, mmRLC_GPM_UCODE_DATA, le32_to_cpup(fw_data++));
2487 WREG32_SOC15(GC, 0, mmRLC_GPM_UCODE_ADDR, adev->gfx.rlc_fw_version);
2492 static int gfx_v9_0_rlc_resume(struct amdgpu_device *adev)
2496 if (amdgpu_sriov_vf(adev)) {
2497 gfx_v9_0_init_csb(adev);
2501 gfx_v9_0_rlc_stop(adev);
2504 WREG32_SOC15(GC, 0, mmRLC_CGCG_CGLS_CTRL, 0);
2506 gfx_v9_0_rlc_reset(adev);
2508 gfx_v9_0_init_pg(adev);
2510 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
2511 /* legacy rlc firmware loading */
2512 r = gfx_v9_0_rlc_load_microcode(adev);
2517 if (adev->asic_type == CHIP_RAVEN ||
2518 adev->asic_type == CHIP_VEGA20) {
2519 if (amdgpu_lbpw != 0)
2520 gfx_v9_0_enable_lbpw(adev, true);
2522 gfx_v9_0_enable_lbpw(adev, false);
2525 gfx_v9_0_rlc_start(adev);
2530 static void gfx_v9_0_cp_gfx_enable(struct amdgpu_device *adev, bool enable)
2533 u32 tmp = RREG32_SOC15(GC, 0, mmCP_ME_CNTL);
2535 tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, ME_HALT, enable ? 0 : 1);
2536 tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, PFP_HALT, enable ? 0 : 1);
2537 tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, CE_HALT, enable ? 0 : 1);
2539 for (i = 0; i < adev->gfx.num_gfx_rings; i++)
2540 adev->gfx.gfx_ring[i].ready = false;
2542 WREG32_SOC15(GC, 0, mmCP_ME_CNTL, tmp);
2546 static int gfx_v9_0_cp_gfx_load_microcode(struct amdgpu_device *adev)
2548 const struct gfx_firmware_header_v1_0 *pfp_hdr;
2549 const struct gfx_firmware_header_v1_0 *ce_hdr;
2550 const struct gfx_firmware_header_v1_0 *me_hdr;
2551 const __le32 *fw_data;
2552 unsigned i, fw_size;
2554 if (!adev->gfx.me_fw || !adev->gfx.pfp_fw || !adev->gfx.ce_fw)
2557 pfp_hdr = (const struct gfx_firmware_header_v1_0 *)
2558 adev->gfx.pfp_fw->data;
2559 ce_hdr = (const struct gfx_firmware_header_v1_0 *)
2560 adev->gfx.ce_fw->data;
2561 me_hdr = (const struct gfx_firmware_header_v1_0 *)
2562 adev->gfx.me_fw->data;
2564 amdgpu_ucode_print_gfx_hdr(&pfp_hdr->header);
2565 amdgpu_ucode_print_gfx_hdr(&ce_hdr->header);
2566 amdgpu_ucode_print_gfx_hdr(&me_hdr->header);
2568 gfx_v9_0_cp_gfx_enable(adev, false);
2571 fw_data = (const __le32 *)
2572 (adev->gfx.pfp_fw->data +
2573 le32_to_cpu(pfp_hdr->header.ucode_array_offset_bytes));
2574 fw_size = le32_to_cpu(pfp_hdr->header.ucode_size_bytes) / 4;
2575 WREG32_SOC15(GC, 0, mmCP_PFP_UCODE_ADDR, 0);
2576 for (i = 0; i < fw_size; i++)
2577 WREG32_SOC15(GC, 0, mmCP_PFP_UCODE_DATA, le32_to_cpup(fw_data++));
2578 WREG32_SOC15(GC, 0, mmCP_PFP_UCODE_ADDR, adev->gfx.pfp_fw_version);
2581 fw_data = (const __le32 *)
2582 (adev->gfx.ce_fw->data +
2583 le32_to_cpu(ce_hdr->header.ucode_array_offset_bytes));
2584 fw_size = le32_to_cpu(ce_hdr->header.ucode_size_bytes) / 4;
2585 WREG32_SOC15(GC, 0, mmCP_CE_UCODE_ADDR, 0);
2586 for (i = 0; i < fw_size; i++)
2587 WREG32_SOC15(GC, 0, mmCP_CE_UCODE_DATA, le32_to_cpup(fw_data++));
2588 WREG32_SOC15(GC, 0, mmCP_CE_UCODE_ADDR, adev->gfx.ce_fw_version);
2591 fw_data = (const __le32 *)
2592 (adev->gfx.me_fw->data +
2593 le32_to_cpu(me_hdr->header.ucode_array_offset_bytes));
2594 fw_size = le32_to_cpu(me_hdr->header.ucode_size_bytes) / 4;
2595 WREG32_SOC15(GC, 0, mmCP_ME_RAM_WADDR, 0);
2596 for (i = 0; i < fw_size; i++)
2597 WREG32_SOC15(GC, 0, mmCP_ME_RAM_DATA, le32_to_cpup(fw_data++));
2598 WREG32_SOC15(GC, 0, mmCP_ME_RAM_WADDR, adev->gfx.me_fw_version);
2603 static int gfx_v9_0_cp_gfx_start(struct amdgpu_device *adev)
2605 struct amdgpu_ring *ring = &adev->gfx.gfx_ring[0];
2606 const struct cs_section_def *sect = NULL;
2607 const struct cs_extent_def *ext = NULL;
2611 WREG32_SOC15(GC, 0, mmCP_MAX_CONTEXT, adev->gfx.config.max_hw_contexts - 1);
2612 WREG32_SOC15(GC, 0, mmCP_DEVICE_ID, 1);
2614 gfx_v9_0_cp_gfx_enable(adev, true);
2616 r = amdgpu_ring_alloc(ring, gfx_v9_0_get_csb_size(adev) + 4 + 3);
2618 DRM_ERROR("amdgpu: cp failed to lock ring (%d).\n", r);
2622 amdgpu_ring_write(ring, PACKET3(PACKET3_PREAMBLE_CNTL, 0));
2623 amdgpu_ring_write(ring, PACKET3_PREAMBLE_BEGIN_CLEAR_STATE);
2625 amdgpu_ring_write(ring, PACKET3(PACKET3_CONTEXT_CONTROL, 1));
2626 amdgpu_ring_write(ring, 0x80000000);
2627 amdgpu_ring_write(ring, 0x80000000);
2629 for (sect = gfx9_cs_data; sect->section != NULL; ++sect) {
2630 for (ext = sect->section; ext->extent != NULL; ++ext) {
2631 if (sect->id == SECT_CONTEXT) {
2632 amdgpu_ring_write(ring,
2633 PACKET3(PACKET3_SET_CONTEXT_REG,
2635 amdgpu_ring_write(ring,
2636 ext->reg_index - PACKET3_SET_CONTEXT_REG_START);
2637 for (i = 0; i < ext->reg_count; i++)
2638 amdgpu_ring_write(ring, ext->extent[i]);
2643 amdgpu_ring_write(ring, PACKET3(PACKET3_PREAMBLE_CNTL, 0));
2644 amdgpu_ring_write(ring, PACKET3_PREAMBLE_END_CLEAR_STATE);
2646 amdgpu_ring_write(ring, PACKET3(PACKET3_CLEAR_STATE, 0));
2647 amdgpu_ring_write(ring, 0);
2649 amdgpu_ring_write(ring, PACKET3(PACKET3_SET_BASE, 2));
2650 amdgpu_ring_write(ring, PACKET3_BASE_INDEX(CE_PARTITION_BASE));
2651 amdgpu_ring_write(ring, 0x8000);
2652 amdgpu_ring_write(ring, 0x8000);
2654 amdgpu_ring_write(ring, PACKET3(PACKET3_SET_UCONFIG_REG,1));
2655 tmp = (PACKET3_SET_UCONFIG_REG_INDEX_TYPE |
2656 (SOC15_REG_OFFSET(GC, 0, mmVGT_INDEX_TYPE) - PACKET3_SET_UCONFIG_REG_START));
2657 amdgpu_ring_write(ring, tmp);
2658 amdgpu_ring_write(ring, 0);
2660 amdgpu_ring_commit(ring);
2665 static int gfx_v9_0_cp_gfx_resume(struct amdgpu_device *adev)
2667 struct amdgpu_ring *ring;
2670 u64 rb_addr, rptr_addr, wptr_gpu_addr;
2672 /* Set the write pointer delay */
2673 WREG32_SOC15(GC, 0, mmCP_RB_WPTR_DELAY, 0);
2675 /* set the RB to use vmid 0 */
2676 WREG32_SOC15(GC, 0, mmCP_RB_VMID, 0);
2678 /* Set ring buffer size */
2679 ring = &adev->gfx.gfx_ring[0];
2680 rb_bufsz = order_base_2(ring->ring_size / 8);
2681 tmp = REG_SET_FIELD(0, CP_RB0_CNTL, RB_BUFSZ, rb_bufsz);
2682 tmp = REG_SET_FIELD(tmp, CP_RB0_CNTL, RB_BLKSZ, rb_bufsz - 2);
2684 tmp = REG_SET_FIELD(tmp, CP_RB0_CNTL, BUF_SWAP, 1);
2686 WREG32_SOC15(GC, 0, mmCP_RB0_CNTL, tmp);
2688 /* Initialize the ring buffer's write pointers */
2690 WREG32_SOC15(GC, 0, mmCP_RB0_WPTR, lower_32_bits(ring->wptr));
2691 WREG32_SOC15(GC, 0, mmCP_RB0_WPTR_HI, upper_32_bits(ring->wptr));
2693 /* set the wb address wether it's enabled or not */
2694 rptr_addr = adev->wb.gpu_addr + (ring->rptr_offs * 4);
2695 WREG32_SOC15(GC, 0, mmCP_RB0_RPTR_ADDR, lower_32_bits(rptr_addr));
2696 WREG32_SOC15(GC, 0, mmCP_RB0_RPTR_ADDR_HI, upper_32_bits(rptr_addr) & CP_RB_RPTR_ADDR_HI__RB_RPTR_ADDR_HI_MASK);
2698 wptr_gpu_addr = adev->wb.gpu_addr + (ring->wptr_offs * 4);
2699 WREG32_SOC15(GC, 0, mmCP_RB_WPTR_POLL_ADDR_LO, lower_32_bits(wptr_gpu_addr));
2700 WREG32_SOC15(GC, 0, mmCP_RB_WPTR_POLL_ADDR_HI, upper_32_bits(wptr_gpu_addr));
2703 WREG32_SOC15(GC, 0, mmCP_RB0_CNTL, tmp);
2705 rb_addr = ring->gpu_addr >> 8;
2706 WREG32_SOC15(GC, 0, mmCP_RB0_BASE, rb_addr);
2707 WREG32_SOC15(GC, 0, mmCP_RB0_BASE_HI, upper_32_bits(rb_addr));
2709 tmp = RREG32_SOC15(GC, 0, mmCP_RB_DOORBELL_CONTROL);
2710 if (ring->use_doorbell) {
2711 tmp = REG_SET_FIELD(tmp, CP_RB_DOORBELL_CONTROL,
2712 DOORBELL_OFFSET, ring->doorbell_index);
2713 tmp = REG_SET_FIELD(tmp, CP_RB_DOORBELL_CONTROL,
2716 tmp = REG_SET_FIELD(tmp, CP_RB_DOORBELL_CONTROL, DOORBELL_EN, 0);
2718 WREG32_SOC15(GC, 0, mmCP_RB_DOORBELL_CONTROL, tmp);
2720 tmp = REG_SET_FIELD(0, CP_RB_DOORBELL_RANGE_LOWER,
2721 DOORBELL_RANGE_LOWER, ring->doorbell_index);
2722 WREG32_SOC15(GC, 0, mmCP_RB_DOORBELL_RANGE_LOWER, tmp);
2724 WREG32_SOC15(GC, 0, mmCP_RB_DOORBELL_RANGE_UPPER,
2725 CP_RB_DOORBELL_RANGE_UPPER__DOORBELL_RANGE_UPPER_MASK);
2728 /* start the ring */
2729 gfx_v9_0_cp_gfx_start(adev);
2735 static void gfx_v9_0_cp_compute_enable(struct amdgpu_device *adev, bool enable)
2740 WREG32_SOC15(GC, 0, mmCP_MEC_CNTL, 0);
2742 WREG32_SOC15(GC, 0, mmCP_MEC_CNTL,
2743 (CP_MEC_CNTL__MEC_ME1_HALT_MASK | CP_MEC_CNTL__MEC_ME2_HALT_MASK));
2744 for (i = 0; i < adev->gfx.num_compute_rings; i++)
2745 adev->gfx.compute_ring[i].ready = false;
2746 adev->gfx.kiq.ring.ready = false;
2751 static int gfx_v9_0_cp_compute_load_microcode(struct amdgpu_device *adev)
2753 const struct gfx_firmware_header_v1_0 *mec_hdr;
2754 const __le32 *fw_data;
2758 if (!adev->gfx.mec_fw)
2761 gfx_v9_0_cp_compute_enable(adev, false);
2763 mec_hdr = (const struct gfx_firmware_header_v1_0 *)adev->gfx.mec_fw->data;
2764 amdgpu_ucode_print_gfx_hdr(&mec_hdr->header);
2766 fw_data = (const __le32 *)
2767 (adev->gfx.mec_fw->data +
2768 le32_to_cpu(mec_hdr->header.ucode_array_offset_bytes));
2770 tmp = REG_SET_FIELD(tmp, CP_CPC_IC_BASE_CNTL, VMID, 0);
2771 tmp = REG_SET_FIELD(tmp, CP_CPC_IC_BASE_CNTL, CACHE_POLICY, 0);
2772 WREG32_SOC15(GC, 0, mmCP_CPC_IC_BASE_CNTL, tmp);
2774 WREG32_SOC15(GC, 0, mmCP_CPC_IC_BASE_LO,
2775 adev->gfx.mec.mec_fw_gpu_addr & 0xFFFFF000);
2776 WREG32_SOC15(GC, 0, mmCP_CPC_IC_BASE_HI,
2777 upper_32_bits(adev->gfx.mec.mec_fw_gpu_addr));
2780 WREG32_SOC15(GC, 0, mmCP_MEC_ME1_UCODE_ADDR,
2781 mec_hdr->jt_offset);
2782 for (i = 0; i < mec_hdr->jt_size; i++)
2783 WREG32_SOC15(GC, 0, mmCP_MEC_ME1_UCODE_DATA,
2784 le32_to_cpup(fw_data + mec_hdr->jt_offset + i));
2786 WREG32_SOC15(GC, 0, mmCP_MEC_ME1_UCODE_ADDR,
2787 adev->gfx.mec_fw_version);
2788 /* Todo : Loading MEC2 firmware is only necessary if MEC2 should run different microcode than MEC1. */
2794 static void gfx_v9_0_kiq_setting(struct amdgpu_ring *ring)
2797 struct amdgpu_device *adev = ring->adev;
2799 /* tell RLC which is KIQ queue */
2800 tmp = RREG32_SOC15(GC, 0, mmRLC_CP_SCHEDULERS);
2802 tmp |= (ring->me << 5) | (ring->pipe << 3) | (ring->queue);
2803 WREG32_SOC15(GC, 0, mmRLC_CP_SCHEDULERS, tmp);
2805 WREG32_SOC15(GC, 0, mmRLC_CP_SCHEDULERS, tmp);
2808 static int gfx_v9_0_kiq_kcq_enable(struct amdgpu_device *adev)
2810 struct amdgpu_ring *kiq_ring = &adev->gfx.kiq.ring;
2811 uint64_t queue_mask = 0;
2814 for (i = 0; i < AMDGPU_MAX_COMPUTE_QUEUES; ++i) {
2815 if (!test_bit(i, adev->gfx.mec.queue_bitmap))
2818 /* This situation may be hit in the future if a new HW
2819 * generation exposes more than 64 queues. If so, the
2820 * definition of queue_mask needs updating */
2821 if (WARN_ON(i >= (sizeof(queue_mask)*8))) {
2822 DRM_ERROR("Invalid KCQ enabled: %d\n", i);
2826 queue_mask |= (1ull << i);
2829 r = amdgpu_ring_alloc(kiq_ring, (7 * adev->gfx.num_compute_rings) + 8);
2831 DRM_ERROR("Failed to lock KIQ (%d).\n", r);
2836 amdgpu_ring_write(kiq_ring, PACKET3(PACKET3_SET_RESOURCES, 6));
2837 amdgpu_ring_write(kiq_ring, PACKET3_SET_RESOURCES_VMID_MASK(0) |
2838 PACKET3_SET_RESOURCES_QUEUE_TYPE(0)); /* vmid_mask:0 queue_type:0 (KIQ) */
2839 amdgpu_ring_write(kiq_ring, lower_32_bits(queue_mask)); /* queue mask lo */
2840 amdgpu_ring_write(kiq_ring, upper_32_bits(queue_mask)); /* queue mask hi */
2841 amdgpu_ring_write(kiq_ring, 0); /* gws mask lo */
2842 amdgpu_ring_write(kiq_ring, 0); /* gws mask hi */
2843 amdgpu_ring_write(kiq_ring, 0); /* oac mask */
2844 amdgpu_ring_write(kiq_ring, 0); /* gds heap base:0, gds heap size:0 */
2845 for (i = 0; i < adev->gfx.num_compute_rings; i++) {
2846 struct amdgpu_ring *ring = &adev->gfx.compute_ring[i];
2847 uint64_t mqd_addr = amdgpu_bo_gpu_offset(ring->mqd_obj);
2848 uint64_t wptr_addr = adev->wb.gpu_addr + (ring->wptr_offs * 4);
2850 amdgpu_ring_write(kiq_ring, PACKET3(PACKET3_MAP_QUEUES, 5));
2851 /* Q_sel:0, vmid:0, vidmem: 1, engine:0, num_Q:1*/
2852 amdgpu_ring_write(kiq_ring, /* Q_sel: 0, vmid: 0, engine: 0, num_Q: 1 */
2853 PACKET3_MAP_QUEUES_QUEUE_SEL(0) | /* Queue_Sel */
2854 PACKET3_MAP_QUEUES_VMID(0) | /* VMID */
2855 PACKET3_MAP_QUEUES_QUEUE(ring->queue) |
2856 PACKET3_MAP_QUEUES_PIPE(ring->pipe) |
2857 PACKET3_MAP_QUEUES_ME((ring->me == 1 ? 0 : 1)) |
2858 PACKET3_MAP_QUEUES_QUEUE_TYPE(0) | /*queue_type: normal compute queue */
2859 PACKET3_MAP_QUEUES_ALLOC_FORMAT(0) | /* alloc format: all_on_one_pipe */
2860 PACKET3_MAP_QUEUES_ENGINE_SEL(0) | /* engine_sel: compute */
2861 PACKET3_MAP_QUEUES_NUM_QUEUES(1)); /* num_queues: must be 1 */
2862 amdgpu_ring_write(kiq_ring, PACKET3_MAP_QUEUES_DOORBELL_OFFSET(ring->doorbell_index));
2863 amdgpu_ring_write(kiq_ring, lower_32_bits(mqd_addr));
2864 amdgpu_ring_write(kiq_ring, upper_32_bits(mqd_addr));
2865 amdgpu_ring_write(kiq_ring, lower_32_bits(wptr_addr));
2866 amdgpu_ring_write(kiq_ring, upper_32_bits(wptr_addr));
2869 r = amdgpu_ring_test_ring(kiq_ring);
2871 DRM_ERROR("KCQ enable failed\n");
2872 kiq_ring->ready = false;
2878 static int gfx_v9_0_mqd_init(struct amdgpu_ring *ring)
2880 struct amdgpu_device *adev = ring->adev;
2881 struct v9_mqd *mqd = ring->mqd_ptr;
2882 uint64_t hqd_gpu_addr, wb_gpu_addr, eop_base_addr;
2885 mqd->header = 0xC0310800;
2886 mqd->compute_pipelinestat_enable = 0x00000001;
2887 mqd->compute_static_thread_mgmt_se0 = 0xffffffff;
2888 mqd->compute_static_thread_mgmt_se1 = 0xffffffff;
2889 mqd->compute_static_thread_mgmt_se2 = 0xffffffff;
2890 mqd->compute_static_thread_mgmt_se3 = 0xffffffff;
2891 mqd->compute_misc_reserved = 0x00000003;
2893 mqd->dynamic_cu_mask_addr_lo =
2894 lower_32_bits(ring->mqd_gpu_addr
2895 + offsetof(struct v9_mqd_allocation, dynamic_cu_mask));
2896 mqd->dynamic_cu_mask_addr_hi =
2897 upper_32_bits(ring->mqd_gpu_addr
2898 + offsetof(struct v9_mqd_allocation, dynamic_cu_mask));
2900 eop_base_addr = ring->eop_gpu_addr >> 8;
2901 mqd->cp_hqd_eop_base_addr_lo = eop_base_addr;
2902 mqd->cp_hqd_eop_base_addr_hi = upper_32_bits(eop_base_addr);
2904 /* set the EOP size, register value is 2^(EOP_SIZE+1) dwords */
2905 tmp = RREG32_SOC15(GC, 0, mmCP_HQD_EOP_CONTROL);
2906 tmp = REG_SET_FIELD(tmp, CP_HQD_EOP_CONTROL, EOP_SIZE,
2907 (order_base_2(GFX9_MEC_HPD_SIZE / 4) - 1));
2909 mqd->cp_hqd_eop_control = tmp;
2911 /* enable doorbell? */
2912 tmp = RREG32_SOC15(GC, 0, mmCP_HQD_PQ_DOORBELL_CONTROL);
2914 if (ring->use_doorbell) {
2915 tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
2916 DOORBELL_OFFSET, ring->doorbell_index);
2917 tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
2919 tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
2920 DOORBELL_SOURCE, 0);
2921 tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
2924 tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
2928 mqd->cp_hqd_pq_doorbell_control = tmp;
2930 /* disable the queue if it's active */
2932 mqd->cp_hqd_dequeue_request = 0;
2933 mqd->cp_hqd_pq_rptr = 0;
2934 mqd->cp_hqd_pq_wptr_lo = 0;
2935 mqd->cp_hqd_pq_wptr_hi = 0;
2937 /* set the pointer to the MQD */
2938 mqd->cp_mqd_base_addr_lo = ring->mqd_gpu_addr & 0xfffffffc;
2939 mqd->cp_mqd_base_addr_hi = upper_32_bits(ring->mqd_gpu_addr);
2941 /* set MQD vmid to 0 */
2942 tmp = RREG32_SOC15(GC, 0, mmCP_MQD_CONTROL);
2943 tmp = REG_SET_FIELD(tmp, CP_MQD_CONTROL, VMID, 0);
2944 mqd->cp_mqd_control = tmp;
2946 /* set the pointer to the HQD, this is similar CP_RB0_BASE/_HI */
2947 hqd_gpu_addr = ring->gpu_addr >> 8;
2948 mqd->cp_hqd_pq_base_lo = hqd_gpu_addr;
2949 mqd->cp_hqd_pq_base_hi = upper_32_bits(hqd_gpu_addr);
2951 /* set up the HQD, this is similar to CP_RB0_CNTL */
2952 tmp = RREG32_SOC15(GC, 0, mmCP_HQD_PQ_CONTROL);
2953 tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, QUEUE_SIZE,
2954 (order_base_2(ring->ring_size / 4) - 1));
2955 tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, RPTR_BLOCK_SIZE,
2956 ((order_base_2(AMDGPU_GPU_PAGE_SIZE / 4) - 1) << 8));
2958 tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, ENDIAN_SWAP, 1);
2960 tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, UNORD_DISPATCH, 0);
2961 tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, ROQ_PQ_IB_FLIP, 0);
2962 tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, PRIV_STATE, 1);
2963 tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, KMD_QUEUE, 1);
2964 mqd->cp_hqd_pq_control = tmp;
2966 /* set the wb address whether it's enabled or not */
2967 wb_gpu_addr = adev->wb.gpu_addr + (ring->rptr_offs * 4);
2968 mqd->cp_hqd_pq_rptr_report_addr_lo = wb_gpu_addr & 0xfffffffc;
2969 mqd->cp_hqd_pq_rptr_report_addr_hi =
2970 upper_32_bits(wb_gpu_addr) & 0xffff;
2972 /* only used if CP_PQ_WPTR_POLL_CNTL.CP_PQ_WPTR_POLL_CNTL__EN_MASK=1 */
2973 wb_gpu_addr = adev->wb.gpu_addr + (ring->wptr_offs * 4);
2974 mqd->cp_hqd_pq_wptr_poll_addr_lo = wb_gpu_addr & 0xfffffffc;
2975 mqd->cp_hqd_pq_wptr_poll_addr_hi = upper_32_bits(wb_gpu_addr) & 0xffff;
2978 /* enable the doorbell if requested */
2979 if (ring->use_doorbell) {
2980 tmp = RREG32_SOC15(GC, 0, mmCP_HQD_PQ_DOORBELL_CONTROL);
2981 tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
2982 DOORBELL_OFFSET, ring->doorbell_index);
2984 tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
2986 tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
2987 DOORBELL_SOURCE, 0);
2988 tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
2992 mqd->cp_hqd_pq_doorbell_control = tmp;
2994 /* reset read and write pointers, similar to CP_RB0_WPTR/_RPTR */
2996 mqd->cp_hqd_pq_rptr = RREG32_SOC15(GC, 0, mmCP_HQD_PQ_RPTR);
2998 /* set the vmid for the queue */
2999 mqd->cp_hqd_vmid = 0;
3001 tmp = RREG32_SOC15(GC, 0, mmCP_HQD_PERSISTENT_STATE);
3002 tmp = REG_SET_FIELD(tmp, CP_HQD_PERSISTENT_STATE, PRELOAD_SIZE, 0x53);
3003 mqd->cp_hqd_persistent_state = tmp;
3005 /* set MIN_IB_AVAIL_SIZE */
3006 tmp = RREG32_SOC15(GC, 0, mmCP_HQD_IB_CONTROL);
3007 tmp = REG_SET_FIELD(tmp, CP_HQD_IB_CONTROL, MIN_IB_AVAIL_SIZE, 3);
3008 mqd->cp_hqd_ib_control = tmp;
3010 /* activate the queue */
3011 mqd->cp_hqd_active = 1;
3016 static int gfx_v9_0_kiq_init_register(struct amdgpu_ring *ring)
3018 struct amdgpu_device *adev = ring->adev;
3019 struct v9_mqd *mqd = ring->mqd_ptr;
3022 /* disable wptr polling */
3023 WREG32_FIELD15(GC, 0, CP_PQ_WPTR_POLL_CNTL, EN, 0);
3025 WREG32_SOC15(GC, 0, mmCP_HQD_EOP_BASE_ADDR,
3026 mqd->cp_hqd_eop_base_addr_lo);
3027 WREG32_SOC15(GC, 0, mmCP_HQD_EOP_BASE_ADDR_HI,
3028 mqd->cp_hqd_eop_base_addr_hi);
3030 /* set the EOP size, register value is 2^(EOP_SIZE+1) dwords */
3031 WREG32_SOC15(GC, 0, mmCP_HQD_EOP_CONTROL,
3032 mqd->cp_hqd_eop_control);
3034 /* enable doorbell? */
3035 WREG32_SOC15(GC, 0, mmCP_HQD_PQ_DOORBELL_CONTROL,
3036 mqd->cp_hqd_pq_doorbell_control);
3038 /* disable the queue if it's active */
3039 if (RREG32_SOC15(GC, 0, mmCP_HQD_ACTIVE) & 1) {
3040 WREG32_SOC15(GC, 0, mmCP_HQD_DEQUEUE_REQUEST, 1);
3041 for (j = 0; j < adev->usec_timeout; j++) {
3042 if (!(RREG32_SOC15(GC, 0, mmCP_HQD_ACTIVE) & 1))
3046 WREG32_SOC15(GC, 0, mmCP_HQD_DEQUEUE_REQUEST,
3047 mqd->cp_hqd_dequeue_request);
3048 WREG32_SOC15(GC, 0, mmCP_HQD_PQ_RPTR,
3049 mqd->cp_hqd_pq_rptr);
3050 WREG32_SOC15(GC, 0, mmCP_HQD_PQ_WPTR_LO,
3051 mqd->cp_hqd_pq_wptr_lo);
3052 WREG32_SOC15(GC, 0, mmCP_HQD_PQ_WPTR_HI,
3053 mqd->cp_hqd_pq_wptr_hi);
3056 /* set the pointer to the MQD */
3057 WREG32_SOC15(GC, 0, mmCP_MQD_BASE_ADDR,
3058 mqd->cp_mqd_base_addr_lo);
3059 WREG32_SOC15(GC, 0, mmCP_MQD_BASE_ADDR_HI,
3060 mqd->cp_mqd_base_addr_hi);
3062 /* set MQD vmid to 0 */
3063 WREG32_SOC15(GC, 0, mmCP_MQD_CONTROL,
3064 mqd->cp_mqd_control);
3066 /* set the pointer to the HQD, this is similar CP_RB0_BASE/_HI */
3067 WREG32_SOC15(GC, 0, mmCP_HQD_PQ_BASE,
3068 mqd->cp_hqd_pq_base_lo);
3069 WREG32_SOC15(GC, 0, mmCP_HQD_PQ_BASE_HI,
3070 mqd->cp_hqd_pq_base_hi);
3072 /* set up the HQD, this is similar to CP_RB0_CNTL */
3073 WREG32_SOC15(GC, 0, mmCP_HQD_PQ_CONTROL,
3074 mqd->cp_hqd_pq_control);
3076 /* set the wb address whether it's enabled or not */
3077 WREG32_SOC15(GC, 0, mmCP_HQD_PQ_RPTR_REPORT_ADDR,
3078 mqd->cp_hqd_pq_rptr_report_addr_lo);
3079 WREG32_SOC15(GC, 0, mmCP_HQD_PQ_RPTR_REPORT_ADDR_HI,
3080 mqd->cp_hqd_pq_rptr_report_addr_hi);
3082 /* only used if CP_PQ_WPTR_POLL_CNTL.CP_PQ_WPTR_POLL_CNTL__EN_MASK=1 */
3083 WREG32_SOC15(GC, 0, mmCP_HQD_PQ_WPTR_POLL_ADDR,
3084 mqd->cp_hqd_pq_wptr_poll_addr_lo);
3085 WREG32_SOC15(GC, 0, mmCP_HQD_PQ_WPTR_POLL_ADDR_HI,
3086 mqd->cp_hqd_pq_wptr_poll_addr_hi);
3088 /* enable the doorbell if requested */
3089 if (ring->use_doorbell) {
3090 WREG32_SOC15(GC, 0, mmCP_MEC_DOORBELL_RANGE_LOWER,
3091 (AMDGPU_DOORBELL64_KIQ *2) << 2);
3092 WREG32_SOC15(GC, 0, mmCP_MEC_DOORBELL_RANGE_UPPER,
3093 (AMDGPU_DOORBELL64_USERQUEUE_END * 2) << 2);
3096 WREG32_SOC15(GC, 0, mmCP_HQD_PQ_DOORBELL_CONTROL,
3097 mqd->cp_hqd_pq_doorbell_control);
3099 /* reset read and write pointers, similar to CP_RB0_WPTR/_RPTR */
3100 WREG32_SOC15(GC, 0, mmCP_HQD_PQ_WPTR_LO,
3101 mqd->cp_hqd_pq_wptr_lo);
3102 WREG32_SOC15(GC, 0, mmCP_HQD_PQ_WPTR_HI,
3103 mqd->cp_hqd_pq_wptr_hi);
3105 /* set the vmid for the queue */
3106 WREG32_SOC15(GC, 0, mmCP_HQD_VMID, mqd->cp_hqd_vmid);
3108 WREG32_SOC15(GC, 0, mmCP_HQD_PERSISTENT_STATE,
3109 mqd->cp_hqd_persistent_state);
3111 /* activate the queue */
3112 WREG32_SOC15(GC, 0, mmCP_HQD_ACTIVE,
3113 mqd->cp_hqd_active);
3115 if (ring->use_doorbell)
3116 WREG32_FIELD15(GC, 0, CP_PQ_STATUS, DOORBELL_ENABLE, 1);
3121 static int gfx_v9_0_kiq_fini_register(struct amdgpu_ring *ring)
3123 struct amdgpu_device *adev = ring->adev;
3126 /* disable the queue if it's active */
3127 if (RREG32_SOC15(GC, 0, mmCP_HQD_ACTIVE) & 1) {
3129 WREG32_SOC15(GC, 0, mmCP_HQD_DEQUEUE_REQUEST, 1);
3131 for (j = 0; j < adev->usec_timeout; j++) {
3132 if (!(RREG32_SOC15(GC, 0, mmCP_HQD_ACTIVE) & 1))
3137 if (j == AMDGPU_MAX_USEC_TIMEOUT) {
3138 DRM_DEBUG("KIQ dequeue request failed.\n");
3140 /* Manual disable if dequeue request times out */
3141 WREG32_SOC15(GC, 0, mmCP_HQD_ACTIVE, 0);
3144 WREG32_SOC15(GC, 0, mmCP_HQD_DEQUEUE_REQUEST,
3148 WREG32_SOC15(GC, 0, mmCP_HQD_IQ_TIMER, 0);
3149 WREG32_SOC15(GC, 0, mmCP_HQD_IB_CONTROL, 0);
3150 WREG32_SOC15(GC, 0, mmCP_HQD_PERSISTENT_STATE, 0);
3151 WREG32_SOC15(GC, 0, mmCP_HQD_PQ_DOORBELL_CONTROL, 0x40000000);
3152 WREG32_SOC15(GC, 0, mmCP_HQD_PQ_DOORBELL_CONTROL, 0);
3153 WREG32_SOC15(GC, 0, mmCP_HQD_PQ_RPTR, 0);
3154 WREG32_SOC15(GC, 0, mmCP_HQD_PQ_WPTR_HI, 0);
3155 WREG32_SOC15(GC, 0, mmCP_HQD_PQ_WPTR_LO, 0);
3160 static int gfx_v9_0_kiq_init_queue(struct amdgpu_ring *ring)
3162 struct amdgpu_device *adev = ring->adev;
3163 struct v9_mqd *mqd = ring->mqd_ptr;
3164 int mqd_idx = AMDGPU_MAX_COMPUTE_RINGS;
3166 gfx_v9_0_kiq_setting(ring);
3168 if (adev->in_gpu_reset) { /* for GPU_RESET case */
3169 /* reset MQD to a clean status */
3170 if (adev->gfx.mec.mqd_backup[mqd_idx])
3171 memcpy(mqd, adev->gfx.mec.mqd_backup[mqd_idx], sizeof(struct v9_mqd_allocation));
3173 /* reset ring buffer */
3175 amdgpu_ring_clear_ring(ring);
3177 mutex_lock(&adev->srbm_mutex);
3178 soc15_grbm_select(adev, ring->me, ring->pipe, ring->queue, 0);
3179 gfx_v9_0_kiq_init_register(ring);
3180 soc15_grbm_select(adev, 0, 0, 0, 0);
3181 mutex_unlock(&adev->srbm_mutex);
3183 memset((void *)mqd, 0, sizeof(struct v9_mqd_allocation));
3184 ((struct v9_mqd_allocation *)mqd)->dynamic_cu_mask = 0xFFFFFFFF;
3185 ((struct v9_mqd_allocation *)mqd)->dynamic_rb_mask = 0xFFFFFFFF;
3186 mutex_lock(&adev->srbm_mutex);
3187 soc15_grbm_select(adev, ring->me, ring->pipe, ring->queue, 0);
3188 gfx_v9_0_mqd_init(ring);
3189 gfx_v9_0_kiq_init_register(ring);
3190 soc15_grbm_select(adev, 0, 0, 0, 0);
3191 mutex_unlock(&adev->srbm_mutex);
3193 if (adev->gfx.mec.mqd_backup[mqd_idx])
3194 memcpy(adev->gfx.mec.mqd_backup[mqd_idx], mqd, sizeof(struct v9_mqd_allocation));
3200 static int gfx_v9_0_kcq_init_queue(struct amdgpu_ring *ring)
3202 struct amdgpu_device *adev = ring->adev;
3203 struct v9_mqd *mqd = ring->mqd_ptr;
3204 int mqd_idx = ring - &adev->gfx.compute_ring[0];
3206 if (!adev->in_gpu_reset && !adev->in_suspend) {
3207 memset((void *)mqd, 0, sizeof(struct v9_mqd_allocation));
3208 ((struct v9_mqd_allocation *)mqd)->dynamic_cu_mask = 0xFFFFFFFF;
3209 ((struct v9_mqd_allocation *)mqd)->dynamic_rb_mask = 0xFFFFFFFF;
3210 mutex_lock(&adev->srbm_mutex);
3211 soc15_grbm_select(adev, ring->me, ring->pipe, ring->queue, 0);
3212 gfx_v9_0_mqd_init(ring);
3213 soc15_grbm_select(adev, 0, 0, 0, 0);
3214 mutex_unlock(&adev->srbm_mutex);
3216 if (adev->gfx.mec.mqd_backup[mqd_idx])
3217 memcpy(adev->gfx.mec.mqd_backup[mqd_idx], mqd, sizeof(struct v9_mqd_allocation));
3218 } else if (adev->in_gpu_reset) { /* for GPU_RESET case */
3219 /* reset MQD to a clean status */
3220 if (adev->gfx.mec.mqd_backup[mqd_idx])
3221 memcpy(mqd, adev->gfx.mec.mqd_backup[mqd_idx], sizeof(struct v9_mqd_allocation));
3223 /* reset ring buffer */
3225 amdgpu_ring_clear_ring(ring);
3227 amdgpu_ring_clear_ring(ring);
3233 static int gfx_v9_0_kiq_resume(struct amdgpu_device *adev)
3235 struct amdgpu_ring *ring;
3238 ring = &adev->gfx.kiq.ring;
3240 r = amdgpu_bo_reserve(ring->mqd_obj, false);
3241 if (unlikely(r != 0))
3244 r = amdgpu_bo_kmap(ring->mqd_obj, (void **)&ring->mqd_ptr);
3245 if (unlikely(r != 0))
3248 gfx_v9_0_kiq_init_queue(ring);
3249 amdgpu_bo_kunmap(ring->mqd_obj);
3250 ring->mqd_ptr = NULL;
3251 amdgpu_bo_unreserve(ring->mqd_obj);
3256 static int gfx_v9_0_kcq_resume(struct amdgpu_device *adev)
3258 struct amdgpu_ring *ring = NULL;
3261 gfx_v9_0_cp_compute_enable(adev, true);
3263 for (i = 0; i < adev->gfx.num_compute_rings; i++) {
3264 ring = &adev->gfx.compute_ring[i];
3266 r = amdgpu_bo_reserve(ring->mqd_obj, false);
3267 if (unlikely(r != 0))
3269 r = amdgpu_bo_kmap(ring->mqd_obj, (void **)&ring->mqd_ptr);
3271 r = gfx_v9_0_kcq_init_queue(ring);
3272 amdgpu_bo_kunmap(ring->mqd_obj);
3273 ring->mqd_ptr = NULL;
3275 amdgpu_bo_unreserve(ring->mqd_obj);
3280 r = gfx_v9_0_kiq_kcq_enable(adev);
3285 static int gfx_v9_0_cp_resume(struct amdgpu_device *adev)
3288 struct amdgpu_ring *ring;
3290 if (!(adev->flags & AMD_IS_APU))
3291 gfx_v9_0_enable_gui_idle_interrupt(adev, false);
3293 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
3294 /* legacy firmware loading */
3295 r = gfx_v9_0_cp_gfx_load_microcode(adev);
3299 r = gfx_v9_0_cp_compute_load_microcode(adev);
3304 r = gfx_v9_0_kiq_resume(adev);
3308 r = gfx_v9_0_cp_gfx_resume(adev);
3312 r = gfx_v9_0_kcq_resume(adev);
3316 ring = &adev->gfx.gfx_ring[0];
3317 r = amdgpu_ring_test_ring(ring);
3319 ring->ready = false;
3323 for (i = 0; i < adev->gfx.num_compute_rings; i++) {
3324 ring = &adev->gfx.compute_ring[i];
3327 r = amdgpu_ring_test_ring(ring);
3329 ring->ready = false;
3332 gfx_v9_0_enable_gui_idle_interrupt(adev, true);
3337 static void gfx_v9_0_cp_enable(struct amdgpu_device *adev, bool enable)
3339 gfx_v9_0_cp_gfx_enable(adev, enable);
3340 gfx_v9_0_cp_compute_enable(adev, enable);
3343 static int gfx_v9_0_hw_init(void *handle)
3346 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3348 gfx_v9_0_init_golden_registers(adev);
3350 gfx_v9_0_constants_init(adev);
3352 r = gfx_v9_0_csb_vram_pin(adev);
3356 r = gfx_v9_0_rlc_resume(adev);
3360 r = gfx_v9_0_cp_resume(adev);
3364 r = gfx_v9_0_ngg_en(adev);
3371 static int gfx_v9_0_kcq_disable(struct amdgpu_device *adev)
3374 struct amdgpu_ring *kiq_ring = &adev->gfx.kiq.ring;
3376 r = amdgpu_ring_alloc(kiq_ring, 6 * adev->gfx.num_compute_rings);
3378 DRM_ERROR("Failed to lock KIQ (%d).\n", r);
3380 for (i = 0; i < adev->gfx.num_compute_rings; i++) {
3381 struct amdgpu_ring *ring = &adev->gfx.compute_ring[i];
3383 amdgpu_ring_write(kiq_ring, PACKET3(PACKET3_UNMAP_QUEUES, 4));
3384 amdgpu_ring_write(kiq_ring, /* Q_sel: 0, vmid: 0, engine: 0, num_Q: 1 */
3385 PACKET3_UNMAP_QUEUES_ACTION(1) | /* RESET_QUEUES */
3386 PACKET3_UNMAP_QUEUES_QUEUE_SEL(0) |
3387 PACKET3_UNMAP_QUEUES_ENGINE_SEL(0) |
3388 PACKET3_UNMAP_QUEUES_NUM_QUEUES(1));
3389 amdgpu_ring_write(kiq_ring, PACKET3_UNMAP_QUEUES_DOORBELL_OFFSET0(ring->doorbell_index));
3390 amdgpu_ring_write(kiq_ring, 0);
3391 amdgpu_ring_write(kiq_ring, 0);
3392 amdgpu_ring_write(kiq_ring, 0);
3394 r = amdgpu_ring_test_ring(kiq_ring);
3396 DRM_ERROR("KCQ disable failed\n");
3401 static int gfx_v9_0_hw_fini(void *handle)
3403 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3405 amdgpu_irq_put(adev, &adev->gfx.priv_reg_irq, 0);
3406 amdgpu_irq_put(adev, &adev->gfx.priv_inst_irq, 0);
3408 /* disable KCQ to avoid CPC touch memory not valid anymore */
3409 gfx_v9_0_kcq_disable(adev);
3411 if (amdgpu_sriov_vf(adev)) {
3412 gfx_v9_0_cp_gfx_enable(adev, false);
3413 /* must disable polling for SRIOV when hw finished, otherwise
3414 * CPC engine may still keep fetching WB address which is already
3415 * invalid after sw finished and trigger DMAR reading error in
3418 WREG32_FIELD15(GC, 0, CP_PQ_WPTR_POLL_CNTL, EN, 0);
3422 /* Use deinitialize sequence from CAIL when unbinding device from driver,
3423 * otherwise KIQ is hanging when binding back
3425 if (!adev->in_gpu_reset && !adev->in_suspend) {
3426 mutex_lock(&adev->srbm_mutex);
3427 soc15_grbm_select(adev, adev->gfx.kiq.ring.me,
3428 adev->gfx.kiq.ring.pipe,
3429 adev->gfx.kiq.ring.queue, 0);
3430 gfx_v9_0_kiq_fini_register(&adev->gfx.kiq.ring);
3431 soc15_grbm_select(adev, 0, 0, 0, 0);
3432 mutex_unlock(&adev->srbm_mutex);
3435 gfx_v9_0_cp_enable(adev, false);
3436 gfx_v9_0_rlc_stop(adev);
3438 gfx_v9_0_csb_vram_unpin(adev);
3443 static int gfx_v9_0_suspend(void *handle)
3445 return gfx_v9_0_hw_fini(handle);
3448 static int gfx_v9_0_resume(void *handle)
3450 return gfx_v9_0_hw_init(handle);
3453 static bool gfx_v9_0_is_idle(void *handle)
3455 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3457 if (REG_GET_FIELD(RREG32_SOC15(GC, 0, mmGRBM_STATUS),
3458 GRBM_STATUS, GUI_ACTIVE))
3464 static int gfx_v9_0_wait_for_idle(void *handle)
3467 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3469 for (i = 0; i < adev->usec_timeout; i++) {
3470 if (gfx_v9_0_is_idle(handle))
3477 static int gfx_v9_0_soft_reset(void *handle)
3479 u32 grbm_soft_reset = 0;
3481 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3484 tmp = RREG32_SOC15(GC, 0, mmGRBM_STATUS);
3485 if (tmp & (GRBM_STATUS__PA_BUSY_MASK | GRBM_STATUS__SC_BUSY_MASK |
3486 GRBM_STATUS__BCI_BUSY_MASK | GRBM_STATUS__SX_BUSY_MASK |
3487 GRBM_STATUS__TA_BUSY_MASK | GRBM_STATUS__VGT_BUSY_MASK |
3488 GRBM_STATUS__DB_BUSY_MASK | GRBM_STATUS__CB_BUSY_MASK |
3489 GRBM_STATUS__GDS_BUSY_MASK | GRBM_STATUS__SPI_BUSY_MASK |
3490 GRBM_STATUS__IA_BUSY_MASK | GRBM_STATUS__IA_BUSY_NO_DMA_MASK)) {
3491 grbm_soft_reset = REG_SET_FIELD(grbm_soft_reset,
3492 GRBM_SOFT_RESET, SOFT_RESET_CP, 1);
3493 grbm_soft_reset = REG_SET_FIELD(grbm_soft_reset,
3494 GRBM_SOFT_RESET, SOFT_RESET_GFX, 1);
3497 if (tmp & (GRBM_STATUS__CP_BUSY_MASK | GRBM_STATUS__CP_COHERENCY_BUSY_MASK)) {
3498 grbm_soft_reset = REG_SET_FIELD(grbm_soft_reset,
3499 GRBM_SOFT_RESET, SOFT_RESET_CP, 1);
3503 tmp = RREG32_SOC15(GC, 0, mmGRBM_STATUS2);
3504 if (REG_GET_FIELD(tmp, GRBM_STATUS2, RLC_BUSY))
3505 grbm_soft_reset = REG_SET_FIELD(grbm_soft_reset,
3506 GRBM_SOFT_RESET, SOFT_RESET_RLC, 1);
3509 if (grbm_soft_reset) {
3511 gfx_v9_0_rlc_stop(adev);
3513 /* Disable GFX parsing/prefetching */
3514 gfx_v9_0_cp_gfx_enable(adev, false);
3516 /* Disable MEC parsing/prefetching */
3517 gfx_v9_0_cp_compute_enable(adev, false);
3519 if (grbm_soft_reset) {
3520 tmp = RREG32_SOC15(GC, 0, mmGRBM_SOFT_RESET);
3521 tmp |= grbm_soft_reset;
3522 dev_info(adev->dev, "GRBM_SOFT_RESET=0x%08X\n", tmp);
3523 WREG32_SOC15(GC, 0, mmGRBM_SOFT_RESET, tmp);
3524 tmp = RREG32_SOC15(GC, 0, mmGRBM_SOFT_RESET);
3528 tmp &= ~grbm_soft_reset;
3529 WREG32_SOC15(GC, 0, mmGRBM_SOFT_RESET, tmp);
3530 tmp = RREG32_SOC15(GC, 0, mmGRBM_SOFT_RESET);
3533 /* Wait a little for things to settle down */
3539 static uint64_t gfx_v9_0_get_gpu_clock_counter(struct amdgpu_device *adev)
3543 mutex_lock(&adev->gfx.gpu_clock_mutex);
3544 WREG32_SOC15(GC, 0, mmRLC_CAPTURE_GPU_CLOCK_COUNT, 1);
3545 clock = (uint64_t)RREG32_SOC15(GC, 0, mmRLC_GPU_CLOCK_COUNT_LSB) |
3546 ((uint64_t)RREG32_SOC15(GC, 0, mmRLC_GPU_CLOCK_COUNT_MSB) << 32ULL);
3547 mutex_unlock(&adev->gfx.gpu_clock_mutex);
3551 static void gfx_v9_0_ring_emit_gds_switch(struct amdgpu_ring *ring,
3553 uint32_t gds_base, uint32_t gds_size,
3554 uint32_t gws_base, uint32_t gws_size,
3555 uint32_t oa_base, uint32_t oa_size)
3557 struct amdgpu_device *adev = ring->adev;
3560 gfx_v9_0_write_data_to_reg(ring, 0, false,
3561 SOC15_REG_OFFSET(GC, 0, mmGDS_VMID0_BASE) + 2 * vmid,
3565 gfx_v9_0_write_data_to_reg(ring, 0, false,
3566 SOC15_REG_OFFSET(GC, 0, mmGDS_VMID0_SIZE) + 2 * vmid,
3570 gfx_v9_0_write_data_to_reg(ring, 0, false,
3571 SOC15_REG_OFFSET(GC, 0, mmGDS_GWS_VMID0) + vmid,
3572 gws_size << GDS_GWS_VMID0__SIZE__SHIFT | gws_base);
3575 gfx_v9_0_write_data_to_reg(ring, 0, false,
3576 SOC15_REG_OFFSET(GC, 0, mmGDS_OA_VMID0) + vmid,
3577 (1 << (oa_size + oa_base)) - (1 << oa_base));
3580 static int gfx_v9_0_early_init(void *handle)
3582 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3584 adev->gfx.num_gfx_rings = GFX9_NUM_GFX_RINGS;
3585 adev->gfx.num_compute_rings = AMDGPU_MAX_COMPUTE_RINGS;
3586 gfx_v9_0_set_ring_funcs(adev);
3587 gfx_v9_0_set_irq_funcs(adev);
3588 gfx_v9_0_set_gds_init(adev);
3589 gfx_v9_0_set_rlc_funcs(adev);
3594 static int gfx_v9_0_late_init(void *handle)
3596 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3599 r = amdgpu_irq_get(adev, &adev->gfx.priv_reg_irq, 0);
3603 r = amdgpu_irq_get(adev, &adev->gfx.priv_inst_irq, 0);
3610 static void gfx_v9_0_enter_rlc_safe_mode(struct amdgpu_device *adev)
3612 uint32_t rlc_setting, data;
3615 if (adev->gfx.rlc.in_safe_mode)
3618 /* if RLC is not enabled, do nothing */
3619 rlc_setting = RREG32_SOC15(GC, 0, mmRLC_CNTL);
3620 if (!(rlc_setting & RLC_CNTL__RLC_ENABLE_F32_MASK))
3623 if (adev->cg_flags &
3624 (AMD_CG_SUPPORT_GFX_CGCG | AMD_CG_SUPPORT_GFX_MGCG |
3625 AMD_CG_SUPPORT_GFX_3D_CGCG)) {
3626 data = RLC_SAFE_MODE__CMD_MASK;
3627 data |= (1 << RLC_SAFE_MODE__MESSAGE__SHIFT);
3628 WREG32_SOC15(GC, 0, mmRLC_SAFE_MODE, data);
3630 /* wait for RLC_SAFE_MODE */
3631 for (i = 0; i < adev->usec_timeout; i++) {
3632 if (!REG_GET_FIELD(RREG32_SOC15(GC, 0, mmRLC_SAFE_MODE), RLC_SAFE_MODE, CMD))
3636 adev->gfx.rlc.in_safe_mode = true;
3640 static void gfx_v9_0_exit_rlc_safe_mode(struct amdgpu_device *adev)
3642 uint32_t rlc_setting, data;
3644 if (!adev->gfx.rlc.in_safe_mode)
3647 /* if RLC is not enabled, do nothing */
3648 rlc_setting = RREG32_SOC15(GC, 0, mmRLC_CNTL);
3649 if (!(rlc_setting & RLC_CNTL__RLC_ENABLE_F32_MASK))
3652 if (adev->cg_flags &
3653 (AMD_CG_SUPPORT_GFX_CGCG | AMD_CG_SUPPORT_GFX_MGCG)) {
3655 * Try to exit safe mode only if it is already in safe
3658 data = RLC_SAFE_MODE__CMD_MASK;
3659 WREG32_SOC15(GC, 0, mmRLC_SAFE_MODE, data);
3660 adev->gfx.rlc.in_safe_mode = false;
3664 static void gfx_v9_0_update_gfx_cg_power_gating(struct amdgpu_device *adev,
3667 gfx_v9_0_enter_rlc_safe_mode(adev);
3669 if ((adev->pg_flags & AMD_PG_SUPPORT_GFX_PG) && enable) {
3670 gfx_v9_0_enable_gfx_cg_power_gating(adev, true);
3671 if (adev->pg_flags & AMD_PG_SUPPORT_GFX_PIPELINE)
3672 gfx_v9_0_enable_gfx_pipeline_powergating(adev, true);
3674 gfx_v9_0_enable_gfx_cg_power_gating(adev, false);
3675 gfx_v9_0_enable_gfx_pipeline_powergating(adev, false);
3678 gfx_v9_0_exit_rlc_safe_mode(adev);
3681 static void gfx_v9_0_update_gfx_mg_power_gating(struct amdgpu_device *adev,
3684 /* TODO: double check if we need to perform under safe mode */
3685 /* gfx_v9_0_enter_rlc_safe_mode(adev); */
3687 if ((adev->pg_flags & AMD_PG_SUPPORT_GFX_SMG) && enable)
3688 gfx_v9_0_enable_gfx_static_mg_power_gating(adev, true);
3690 gfx_v9_0_enable_gfx_static_mg_power_gating(adev, false);
3692 if ((adev->pg_flags & AMD_PG_SUPPORT_GFX_DMG) && enable)
3693 gfx_v9_0_enable_gfx_dynamic_mg_power_gating(adev, true);
3695 gfx_v9_0_enable_gfx_dynamic_mg_power_gating(adev, false);
3697 /* gfx_v9_0_exit_rlc_safe_mode(adev); */
3700 static void gfx_v9_0_update_medium_grain_clock_gating(struct amdgpu_device *adev,
3705 /* It is disabled by HW by default */
3706 if (enable && (adev->cg_flags & AMD_CG_SUPPORT_GFX_MGCG)) {
3707 /* 1 - RLC_CGTT_MGCG_OVERRIDE */
3708 def = data = RREG32_SOC15(GC, 0, mmRLC_CGTT_MGCG_OVERRIDE);
3710 if (adev->asic_type != CHIP_VEGA12)
3711 data &= ~RLC_CGTT_MGCG_OVERRIDE__CPF_CGTT_SCLK_OVERRIDE_MASK;
3713 data &= ~(RLC_CGTT_MGCG_OVERRIDE__GRBM_CGTT_SCLK_OVERRIDE_MASK |
3714 RLC_CGTT_MGCG_OVERRIDE__GFXIP_MGCG_OVERRIDE_MASK |
3715 RLC_CGTT_MGCG_OVERRIDE__GFXIP_MGLS_OVERRIDE_MASK);
3717 /* only for Vega10 & Raven1 */
3718 data |= RLC_CGTT_MGCG_OVERRIDE__RLC_CGTT_SCLK_OVERRIDE_MASK;
3721 WREG32_SOC15(GC, 0, mmRLC_CGTT_MGCG_OVERRIDE, data);
3723 /* MGLS is a global flag to control all MGLS in GFX */
3724 if (adev->cg_flags & AMD_CG_SUPPORT_GFX_MGLS) {
3725 /* 2 - RLC memory Light sleep */
3726 if (adev->cg_flags & AMD_CG_SUPPORT_GFX_RLC_LS) {
3727 def = data = RREG32_SOC15(GC, 0, mmRLC_MEM_SLP_CNTL);
3728 data |= RLC_MEM_SLP_CNTL__RLC_MEM_LS_EN_MASK;
3730 WREG32_SOC15(GC, 0, mmRLC_MEM_SLP_CNTL, data);
3732 /* 3 - CP memory Light sleep */
3733 if (adev->cg_flags & AMD_CG_SUPPORT_GFX_CP_LS) {
3734 def = data = RREG32_SOC15(GC, 0, mmCP_MEM_SLP_CNTL);
3735 data |= CP_MEM_SLP_CNTL__CP_MEM_LS_EN_MASK;
3737 WREG32_SOC15(GC, 0, mmCP_MEM_SLP_CNTL, data);
3741 /* 1 - MGCG_OVERRIDE */
3742 def = data = RREG32_SOC15(GC, 0, mmRLC_CGTT_MGCG_OVERRIDE);
3744 if (adev->asic_type != CHIP_VEGA12)
3745 data |= RLC_CGTT_MGCG_OVERRIDE__CPF_CGTT_SCLK_OVERRIDE_MASK;
3747 data |= (RLC_CGTT_MGCG_OVERRIDE__RLC_CGTT_SCLK_OVERRIDE_MASK |
3748 RLC_CGTT_MGCG_OVERRIDE__GRBM_CGTT_SCLK_OVERRIDE_MASK |
3749 RLC_CGTT_MGCG_OVERRIDE__GFXIP_MGCG_OVERRIDE_MASK |
3750 RLC_CGTT_MGCG_OVERRIDE__GFXIP_MGLS_OVERRIDE_MASK);
3753 WREG32_SOC15(GC, 0, mmRLC_CGTT_MGCG_OVERRIDE, data);
3755 /* 2 - disable MGLS in RLC */
3756 data = RREG32_SOC15(GC, 0, mmRLC_MEM_SLP_CNTL);
3757 if (data & RLC_MEM_SLP_CNTL__RLC_MEM_LS_EN_MASK) {
3758 data &= ~RLC_MEM_SLP_CNTL__RLC_MEM_LS_EN_MASK;
3759 WREG32_SOC15(GC, 0, mmRLC_MEM_SLP_CNTL, data);
3762 /* 3 - disable MGLS in CP */
3763 data = RREG32_SOC15(GC, 0, mmCP_MEM_SLP_CNTL);
3764 if (data & CP_MEM_SLP_CNTL__CP_MEM_LS_EN_MASK) {
3765 data &= ~CP_MEM_SLP_CNTL__CP_MEM_LS_EN_MASK;
3766 WREG32_SOC15(GC, 0, mmCP_MEM_SLP_CNTL, data);
3771 static void gfx_v9_0_update_3d_clock_gating(struct amdgpu_device *adev,
3776 adev->gfx.rlc.funcs->enter_safe_mode(adev);
3778 /* Enable 3D CGCG/CGLS */
3779 if (enable && (adev->cg_flags & AMD_CG_SUPPORT_GFX_3D_CGCG)) {
3780 /* write cmd to clear cgcg/cgls ov */
3781 def = data = RREG32_SOC15(GC, 0, mmRLC_CGTT_MGCG_OVERRIDE);
3782 /* unset CGCG override */
3783 data &= ~RLC_CGTT_MGCG_OVERRIDE__GFXIP_GFX3D_CG_OVERRIDE_MASK;
3784 /* update CGCG and CGLS override bits */
3786 WREG32_SOC15(GC, 0, mmRLC_CGTT_MGCG_OVERRIDE, data);
3788 /* enable 3Dcgcg FSM(0x0000363f) */
3789 def = RREG32_SOC15(GC, 0, mmRLC_CGCG_CGLS_CTRL_3D);
3791 data = (0x36 << RLC_CGCG_CGLS_CTRL_3D__CGCG_GFX_IDLE_THRESHOLD__SHIFT) |
3792 RLC_CGCG_CGLS_CTRL_3D__CGCG_EN_MASK;
3793 if (adev->cg_flags & AMD_CG_SUPPORT_GFX_3D_CGLS)
3794 data |= (0x000F << RLC_CGCG_CGLS_CTRL_3D__CGLS_REP_COMPANSAT_DELAY__SHIFT) |
3795 RLC_CGCG_CGLS_CTRL_3D__CGLS_EN_MASK;
3797 WREG32_SOC15(GC, 0, mmRLC_CGCG_CGLS_CTRL_3D, data);
3799 /* set IDLE_POLL_COUNT(0x00900100) */
3800 def = RREG32_SOC15(GC, 0, mmCP_RB_WPTR_POLL_CNTL);
3801 data = (0x0100 << CP_RB_WPTR_POLL_CNTL__POLL_FREQUENCY__SHIFT) |
3802 (0x0090 << CP_RB_WPTR_POLL_CNTL__IDLE_POLL_COUNT__SHIFT);
3804 WREG32_SOC15(GC, 0, mmCP_RB_WPTR_POLL_CNTL, data);
3806 /* Disable CGCG/CGLS */
3807 def = data = RREG32_SOC15(GC, 0, mmRLC_CGCG_CGLS_CTRL_3D);
3808 /* disable cgcg, cgls should be disabled */
3809 data &= ~(RLC_CGCG_CGLS_CTRL_3D__CGCG_EN_MASK |
3810 RLC_CGCG_CGLS_CTRL_3D__CGLS_EN_MASK);
3811 /* disable cgcg and cgls in FSM */
3813 WREG32_SOC15(GC, 0, mmRLC_CGCG_CGLS_CTRL_3D, data);
3816 adev->gfx.rlc.funcs->exit_safe_mode(adev);
3819 static void gfx_v9_0_update_coarse_grain_clock_gating(struct amdgpu_device *adev,
3824 adev->gfx.rlc.funcs->enter_safe_mode(adev);
3826 if (enable && (adev->cg_flags & AMD_CG_SUPPORT_GFX_CGCG)) {
3827 def = data = RREG32_SOC15(GC, 0, mmRLC_CGTT_MGCG_OVERRIDE);
3828 /* unset CGCG override */
3829 data &= ~RLC_CGTT_MGCG_OVERRIDE__GFXIP_CGCG_OVERRIDE_MASK;
3830 if (adev->cg_flags & AMD_CG_SUPPORT_GFX_CGLS)
3831 data &= ~RLC_CGTT_MGCG_OVERRIDE__GFXIP_CGLS_OVERRIDE_MASK;
3833 data |= RLC_CGTT_MGCG_OVERRIDE__GFXIP_CGLS_OVERRIDE_MASK;
3834 /* update CGCG and CGLS override bits */
3836 WREG32_SOC15(GC, 0, mmRLC_CGTT_MGCG_OVERRIDE, data);
3838 /* enable cgcg FSM(0x0000363F) */
3839 def = RREG32_SOC15(GC, 0, mmRLC_CGCG_CGLS_CTRL);
3841 data = (0x36 << RLC_CGCG_CGLS_CTRL__CGCG_GFX_IDLE_THRESHOLD__SHIFT) |
3842 RLC_CGCG_CGLS_CTRL__CGCG_EN_MASK;
3843 if (adev->cg_flags & AMD_CG_SUPPORT_GFX_CGLS)
3844 data |= (0x000F << RLC_CGCG_CGLS_CTRL__CGLS_REP_COMPANSAT_DELAY__SHIFT) |
3845 RLC_CGCG_CGLS_CTRL__CGLS_EN_MASK;
3847 WREG32_SOC15(GC, 0, mmRLC_CGCG_CGLS_CTRL, data);
3849 /* set IDLE_POLL_COUNT(0x00900100) */
3850 def = RREG32_SOC15(GC, 0, mmCP_RB_WPTR_POLL_CNTL);
3851 data = (0x0100 << CP_RB_WPTR_POLL_CNTL__POLL_FREQUENCY__SHIFT) |
3852 (0x0090 << CP_RB_WPTR_POLL_CNTL__IDLE_POLL_COUNT__SHIFT);
3854 WREG32_SOC15(GC, 0, mmCP_RB_WPTR_POLL_CNTL, data);
3856 def = data = RREG32_SOC15(GC, 0, mmRLC_CGCG_CGLS_CTRL);
3857 /* reset CGCG/CGLS bits */
3858 data &= ~(RLC_CGCG_CGLS_CTRL__CGCG_EN_MASK | RLC_CGCG_CGLS_CTRL__CGLS_EN_MASK);
3859 /* disable cgcg and cgls in FSM */
3861 WREG32_SOC15(GC, 0, mmRLC_CGCG_CGLS_CTRL, data);
3864 adev->gfx.rlc.funcs->exit_safe_mode(adev);
3867 static int gfx_v9_0_update_gfx_clock_gating(struct amdgpu_device *adev,
3871 /* CGCG/CGLS should be enabled after MGCG/MGLS
3872 * === MGCG + MGLS ===
3874 gfx_v9_0_update_medium_grain_clock_gating(adev, enable);
3875 /* === CGCG /CGLS for GFX 3D Only === */
3876 gfx_v9_0_update_3d_clock_gating(adev, enable);
3877 /* === CGCG + CGLS === */
3878 gfx_v9_0_update_coarse_grain_clock_gating(adev, enable);
3880 /* CGCG/CGLS should be disabled before MGCG/MGLS
3881 * === CGCG + CGLS ===
3883 gfx_v9_0_update_coarse_grain_clock_gating(adev, enable);
3884 /* === CGCG /CGLS for GFX 3D Only === */
3885 gfx_v9_0_update_3d_clock_gating(adev, enable);
3886 /* === MGCG + MGLS === */
3887 gfx_v9_0_update_medium_grain_clock_gating(adev, enable);
3892 static const struct amdgpu_rlc_funcs gfx_v9_0_rlc_funcs = {
3893 .enter_safe_mode = gfx_v9_0_enter_rlc_safe_mode,
3894 .exit_safe_mode = gfx_v9_0_exit_rlc_safe_mode
3897 static int gfx_v9_0_set_powergating_state(void *handle,
3898 enum amd_powergating_state state)
3900 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3901 bool enable = (state == AMD_PG_STATE_GATE) ? true : false;
3903 switch (adev->asic_type) {
3906 amdgpu_gfx_off_ctrl(adev, false);
3907 cancel_delayed_work_sync(&adev->gfx.gfx_off_delay_work);
3909 if (adev->pg_flags & AMD_PG_SUPPORT_RLC_SMU_HS) {
3910 gfx_v9_0_enable_sck_slow_down_on_power_up(adev, true);
3911 gfx_v9_0_enable_sck_slow_down_on_power_down(adev, true);
3913 gfx_v9_0_enable_sck_slow_down_on_power_up(adev, false);
3914 gfx_v9_0_enable_sck_slow_down_on_power_down(adev, false);
3917 if (adev->pg_flags & AMD_PG_SUPPORT_CP)
3918 gfx_v9_0_enable_cp_power_gating(adev, true);
3920 gfx_v9_0_enable_cp_power_gating(adev, false);
3922 /* update gfx cgpg state */
3923 gfx_v9_0_update_gfx_cg_power_gating(adev, enable);
3925 /* update mgcg state */
3926 gfx_v9_0_update_gfx_mg_power_gating(adev, enable);
3929 amdgpu_gfx_off_ctrl(adev, true);
3933 amdgpu_gfx_off_ctrl(adev, false);
3934 cancel_delayed_work_sync(&adev->gfx.gfx_off_delay_work);
3936 amdgpu_gfx_off_ctrl(adev, true);
3946 static int gfx_v9_0_set_clockgating_state(void *handle,
3947 enum amd_clockgating_state state)
3949 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3951 if (amdgpu_sriov_vf(adev))
3954 switch (adev->asic_type) {
3959 gfx_v9_0_update_gfx_clock_gating(adev,
3960 state == AMD_CG_STATE_GATE ? true : false);
3968 static void gfx_v9_0_get_clockgating_state(void *handle, u32 *flags)
3970 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3973 if (amdgpu_sriov_vf(adev))
3976 /* AMD_CG_SUPPORT_GFX_MGCG */
3977 data = RREG32_SOC15(GC, 0, mmRLC_CGTT_MGCG_OVERRIDE);
3978 if (!(data & RLC_CGTT_MGCG_OVERRIDE__GFXIP_MGCG_OVERRIDE_MASK))
3979 *flags |= AMD_CG_SUPPORT_GFX_MGCG;
3981 /* AMD_CG_SUPPORT_GFX_CGCG */
3982 data = RREG32_SOC15(GC, 0, mmRLC_CGCG_CGLS_CTRL);
3983 if (data & RLC_CGCG_CGLS_CTRL__CGCG_EN_MASK)
3984 *flags |= AMD_CG_SUPPORT_GFX_CGCG;
3986 /* AMD_CG_SUPPORT_GFX_CGLS */
3987 if (data & RLC_CGCG_CGLS_CTRL__CGLS_EN_MASK)
3988 *flags |= AMD_CG_SUPPORT_GFX_CGLS;
3990 /* AMD_CG_SUPPORT_GFX_RLC_LS */
3991 data = RREG32_SOC15(GC, 0, mmRLC_MEM_SLP_CNTL);
3992 if (data & RLC_MEM_SLP_CNTL__RLC_MEM_LS_EN_MASK)
3993 *flags |= AMD_CG_SUPPORT_GFX_RLC_LS | AMD_CG_SUPPORT_GFX_MGLS;
3995 /* AMD_CG_SUPPORT_GFX_CP_LS */
3996 data = RREG32_SOC15(GC, 0, mmCP_MEM_SLP_CNTL);
3997 if (data & CP_MEM_SLP_CNTL__CP_MEM_LS_EN_MASK)
3998 *flags |= AMD_CG_SUPPORT_GFX_CP_LS | AMD_CG_SUPPORT_GFX_MGLS;
4000 /* AMD_CG_SUPPORT_GFX_3D_CGCG */
4001 data = RREG32_SOC15(GC, 0, mmRLC_CGCG_CGLS_CTRL_3D);
4002 if (data & RLC_CGCG_CGLS_CTRL_3D__CGCG_EN_MASK)
4003 *flags |= AMD_CG_SUPPORT_GFX_3D_CGCG;
4005 /* AMD_CG_SUPPORT_GFX_3D_CGLS */
4006 if (data & RLC_CGCG_CGLS_CTRL_3D__CGLS_EN_MASK)
4007 *flags |= AMD_CG_SUPPORT_GFX_3D_CGLS;
4010 static u64 gfx_v9_0_ring_get_rptr_gfx(struct amdgpu_ring *ring)
4012 return ring->adev->wb.wb[ring->rptr_offs]; /* gfx9 is 32bit rptr*/
4015 static u64 gfx_v9_0_ring_get_wptr_gfx(struct amdgpu_ring *ring)
4017 struct amdgpu_device *adev = ring->adev;
4020 /* XXX check if swapping is necessary on BE */
4021 if (ring->use_doorbell) {
4022 wptr = atomic64_read((atomic64_t *)&adev->wb.wb[ring->wptr_offs]);
4024 wptr = RREG32_SOC15(GC, 0, mmCP_RB0_WPTR);
4025 wptr += (u64)RREG32_SOC15(GC, 0, mmCP_RB0_WPTR_HI) << 32;
4031 static void gfx_v9_0_ring_set_wptr_gfx(struct amdgpu_ring *ring)
4033 struct amdgpu_device *adev = ring->adev;
4035 if (ring->use_doorbell) {
4036 /* XXX check if swapping is necessary on BE */
4037 atomic64_set((atomic64_t*)&adev->wb.wb[ring->wptr_offs], ring->wptr);
4038 WDOORBELL64(ring->doorbell_index, ring->wptr);
4040 WREG32_SOC15(GC, 0, mmCP_RB0_WPTR, lower_32_bits(ring->wptr));
4041 WREG32_SOC15(GC, 0, mmCP_RB0_WPTR_HI, upper_32_bits(ring->wptr));
4045 static void gfx_v9_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
4047 struct amdgpu_device *adev = ring->adev;
4048 u32 ref_and_mask, reg_mem_engine;
4049 const struct nbio_hdp_flush_reg *nbio_hf_reg = adev->nbio_funcs->hdp_flush_reg;
4051 if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE) {
4054 ref_and_mask = nbio_hf_reg->ref_and_mask_cp2 << ring->pipe;
4057 ref_and_mask = nbio_hf_reg->ref_and_mask_cp6 << ring->pipe;
4064 ref_and_mask = nbio_hf_reg->ref_and_mask_cp0;
4065 reg_mem_engine = 1; /* pfp */
4068 gfx_v9_0_wait_reg_mem(ring, reg_mem_engine, 0, 1,
4069 adev->nbio_funcs->get_hdp_flush_req_offset(adev),
4070 adev->nbio_funcs->get_hdp_flush_done_offset(adev),
4071 ref_and_mask, ref_and_mask, 0x20);
4074 static void gfx_v9_0_ring_emit_ib_gfx(struct amdgpu_ring *ring,
4075 struct amdgpu_ib *ib,
4076 unsigned vmid, bool ctx_switch)
4078 u32 header, control = 0;
4080 if (ib->flags & AMDGPU_IB_FLAG_CE)
4081 header = PACKET3(PACKET3_INDIRECT_BUFFER_CONST, 2);
4083 header = PACKET3(PACKET3_INDIRECT_BUFFER, 2);
4085 control |= ib->length_dw | (vmid << 24);
4087 if (amdgpu_sriov_vf(ring->adev) && (ib->flags & AMDGPU_IB_FLAG_PREEMPT)) {
4088 control |= INDIRECT_BUFFER_PRE_ENB(1);
4090 if (!(ib->flags & AMDGPU_IB_FLAG_CE))
4091 gfx_v9_0_ring_emit_de_meta(ring);
4094 amdgpu_ring_write(ring, header);
4095 BUG_ON(ib->gpu_addr & 0x3); /* Dword align */
4096 amdgpu_ring_write(ring,
4100 lower_32_bits(ib->gpu_addr));
4101 amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
4102 amdgpu_ring_write(ring, control);
4105 static void gfx_v9_0_ring_emit_ib_compute(struct amdgpu_ring *ring,
4106 struct amdgpu_ib *ib,
4107 unsigned vmid, bool ctx_switch)
4109 u32 control = INDIRECT_BUFFER_VALID | ib->length_dw | (vmid << 24);
4111 amdgpu_ring_write(ring, PACKET3(PACKET3_INDIRECT_BUFFER, 2));
4112 BUG_ON(ib->gpu_addr & 0x3); /* Dword align */
4113 amdgpu_ring_write(ring,
4117 lower_32_bits(ib->gpu_addr));
4118 amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
4119 amdgpu_ring_write(ring, control);
4122 static void gfx_v9_0_ring_emit_fence(struct amdgpu_ring *ring, u64 addr,
4123 u64 seq, unsigned flags)
4125 bool write64bit = flags & AMDGPU_FENCE_FLAG_64BIT;
4126 bool int_sel = flags & AMDGPU_FENCE_FLAG_INT;
4127 bool writeback = flags & AMDGPU_FENCE_FLAG_TC_WB_ONLY;
4129 /* RELEASE_MEM - flush caches, send int */
4130 amdgpu_ring_write(ring, PACKET3(PACKET3_RELEASE_MEM, 6));
4131 amdgpu_ring_write(ring, ((writeback ? (EOP_TC_WB_ACTION_EN |
4132 EOP_TC_NC_ACTION_EN) :
4133 (EOP_TCL1_ACTION_EN |
4135 EOP_TC_WB_ACTION_EN |
4136 EOP_TC_MD_ACTION_EN)) |
4137 EVENT_TYPE(CACHE_FLUSH_AND_INV_TS_EVENT) |
4139 amdgpu_ring_write(ring, DATA_SEL(write64bit ? 2 : 1) | INT_SEL(int_sel ? 2 : 0));
4142 * the address should be Qword aligned if 64bit write, Dword
4143 * aligned if only send 32bit data low (discard data high)
4149 amdgpu_ring_write(ring, lower_32_bits(addr));
4150 amdgpu_ring_write(ring, upper_32_bits(addr));
4151 amdgpu_ring_write(ring, lower_32_bits(seq));
4152 amdgpu_ring_write(ring, upper_32_bits(seq));
4153 amdgpu_ring_write(ring, 0);
4156 static void gfx_v9_0_ring_emit_pipeline_sync(struct amdgpu_ring *ring)
4158 int usepfp = (ring->funcs->type == AMDGPU_RING_TYPE_GFX);
4159 uint32_t seq = ring->fence_drv.sync_seq;
4160 uint64_t addr = ring->fence_drv.gpu_addr;
4162 gfx_v9_0_wait_reg_mem(ring, usepfp, 1, 0,
4163 lower_32_bits(addr), upper_32_bits(addr),
4164 seq, 0xffffffff, 4);
4167 static void gfx_v9_0_ring_emit_vm_flush(struct amdgpu_ring *ring,
4168 unsigned vmid, uint64_t pd_addr)
4170 amdgpu_gmc_emit_flush_gpu_tlb(ring, vmid, pd_addr);
4172 /* compute doesn't have PFP */
4173 if (ring->funcs->type == AMDGPU_RING_TYPE_GFX) {
4174 /* sync PFP to ME, otherwise we might get invalid PFP reads */
4175 amdgpu_ring_write(ring, PACKET3(PACKET3_PFP_SYNC_ME, 0));
4176 amdgpu_ring_write(ring, 0x0);
4180 static u64 gfx_v9_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
4182 return ring->adev->wb.wb[ring->rptr_offs]; /* gfx9 hardware is 32bit rptr */
4185 static u64 gfx_v9_0_ring_get_wptr_compute(struct amdgpu_ring *ring)
4189 /* XXX check if swapping is necessary on BE */
4190 if (ring->use_doorbell)
4191 wptr = atomic64_read((atomic64_t *)&ring->adev->wb.wb[ring->wptr_offs]);
4197 static void gfx_v9_0_ring_set_pipe_percent(struct amdgpu_ring *ring,
4200 struct amdgpu_device *adev = ring->adev;
4201 int pipe_num, tmp, reg;
4202 int pipe_percent = acquire ? SPI_WCL_PIPE_PERCENT_GFX__VALUE_MASK : 0x1;
4204 pipe_num = ring->me * adev->gfx.mec.num_pipe_per_mec + ring->pipe;
4206 /* first me only has 2 entries, GFX and HP3D */
4210 reg = SOC15_REG_OFFSET(GC, 0, mmSPI_WCL_PIPE_PERCENT_GFX) + pipe_num;
4212 tmp = REG_SET_FIELD(tmp, SPI_WCL_PIPE_PERCENT_GFX, VALUE, pipe_percent);
4216 static void gfx_v9_0_pipe_reserve_resources(struct amdgpu_device *adev,
4217 struct amdgpu_ring *ring,
4222 struct amdgpu_ring *iring;
4224 mutex_lock(&adev->gfx.pipe_reserve_mutex);
4225 pipe = amdgpu_gfx_queue_to_bit(adev, ring->me, ring->pipe, 0);
4227 set_bit(pipe, adev->gfx.pipe_reserve_bitmap);
4229 clear_bit(pipe, adev->gfx.pipe_reserve_bitmap);
4231 if (!bitmap_weight(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES)) {
4232 /* Clear all reservations - everyone reacquires all resources */
4233 for (i = 0; i < adev->gfx.num_gfx_rings; ++i)
4234 gfx_v9_0_ring_set_pipe_percent(&adev->gfx.gfx_ring[i],
4237 for (i = 0; i < adev->gfx.num_compute_rings; ++i)
4238 gfx_v9_0_ring_set_pipe_percent(&adev->gfx.compute_ring[i],
4241 /* Lower all pipes without a current reservation */
4242 for (i = 0; i < adev->gfx.num_gfx_rings; ++i) {
4243 iring = &adev->gfx.gfx_ring[i];
4244 pipe = amdgpu_gfx_queue_to_bit(adev,
4248 reserve = test_bit(pipe, adev->gfx.pipe_reserve_bitmap);
4249 gfx_v9_0_ring_set_pipe_percent(iring, reserve);
4252 for (i = 0; i < adev->gfx.num_compute_rings; ++i) {
4253 iring = &adev->gfx.compute_ring[i];
4254 pipe = amdgpu_gfx_queue_to_bit(adev,
4258 reserve = test_bit(pipe, adev->gfx.pipe_reserve_bitmap);
4259 gfx_v9_0_ring_set_pipe_percent(iring, reserve);
4263 mutex_unlock(&adev->gfx.pipe_reserve_mutex);
4266 static void gfx_v9_0_hqd_set_priority(struct amdgpu_device *adev,
4267 struct amdgpu_ring *ring,
4270 uint32_t pipe_priority = acquire ? 0x2 : 0x0;
4271 uint32_t queue_priority = acquire ? 0xf : 0x0;
4273 mutex_lock(&adev->srbm_mutex);
4274 soc15_grbm_select(adev, ring->me, ring->pipe, ring->queue, 0);
4276 WREG32_SOC15(GC, 0, mmCP_HQD_PIPE_PRIORITY, pipe_priority);
4277 WREG32_SOC15(GC, 0, mmCP_HQD_QUEUE_PRIORITY, queue_priority);
4279 soc15_grbm_select(adev, 0, 0, 0, 0);
4280 mutex_unlock(&adev->srbm_mutex);
4283 static void gfx_v9_0_ring_set_priority_compute(struct amdgpu_ring *ring,
4284 enum drm_sched_priority priority)
4286 struct amdgpu_device *adev = ring->adev;
4287 bool acquire = priority == DRM_SCHED_PRIORITY_HIGH_HW;
4289 if (ring->funcs->type != AMDGPU_RING_TYPE_COMPUTE)
4292 gfx_v9_0_hqd_set_priority(adev, ring, acquire);
4293 gfx_v9_0_pipe_reserve_resources(adev, ring, acquire);
4296 static void gfx_v9_0_ring_set_wptr_compute(struct amdgpu_ring *ring)
4298 struct amdgpu_device *adev = ring->adev;
4300 /* XXX check if swapping is necessary on BE */
4301 if (ring->use_doorbell) {
4302 atomic64_set((atomic64_t*)&adev->wb.wb[ring->wptr_offs], ring->wptr);
4303 WDOORBELL64(ring->doorbell_index, ring->wptr);
4305 BUG(); /* only DOORBELL method supported on gfx9 now */
4309 static void gfx_v9_0_ring_emit_fence_kiq(struct amdgpu_ring *ring, u64 addr,
4310 u64 seq, unsigned int flags)
4312 struct amdgpu_device *adev = ring->adev;
4314 /* we only allocate 32bit for each seq wb address */
4315 BUG_ON(flags & AMDGPU_FENCE_FLAG_64BIT);
4317 /* write fence seq to the "addr" */
4318 amdgpu_ring_write(ring, PACKET3(PACKET3_WRITE_DATA, 3));
4319 amdgpu_ring_write(ring, (WRITE_DATA_ENGINE_SEL(0) |
4320 WRITE_DATA_DST_SEL(5) | WR_CONFIRM));
4321 amdgpu_ring_write(ring, lower_32_bits(addr));
4322 amdgpu_ring_write(ring, upper_32_bits(addr));
4323 amdgpu_ring_write(ring, lower_32_bits(seq));
4325 if (flags & AMDGPU_FENCE_FLAG_INT) {
4326 /* set register to trigger INT */
4327 amdgpu_ring_write(ring, PACKET3(PACKET3_WRITE_DATA, 3));
4328 amdgpu_ring_write(ring, (WRITE_DATA_ENGINE_SEL(0) |
4329 WRITE_DATA_DST_SEL(0) | WR_CONFIRM));
4330 amdgpu_ring_write(ring, SOC15_REG_OFFSET(GC, 0, mmCPC_INT_STATUS));
4331 amdgpu_ring_write(ring, 0);
4332 amdgpu_ring_write(ring, 0x20000000); /* src_id is 178 */
4336 static void gfx_v9_ring_emit_sb(struct amdgpu_ring *ring)
4338 amdgpu_ring_write(ring, PACKET3(PACKET3_SWITCH_BUFFER, 0));
4339 amdgpu_ring_write(ring, 0);
4342 static void gfx_v9_0_ring_emit_ce_meta(struct amdgpu_ring *ring)
4344 struct v9_ce_ib_state ce_payload = {0};
4348 cnt = (sizeof(ce_payload) >> 2) + 4 - 2;
4349 csa_addr = amdgpu_csa_vaddr(ring->adev);
4351 amdgpu_ring_write(ring, PACKET3(PACKET3_WRITE_DATA, cnt));
4352 amdgpu_ring_write(ring, (WRITE_DATA_ENGINE_SEL(2) |
4353 WRITE_DATA_DST_SEL(8) |
4355 WRITE_DATA_CACHE_POLICY(0));
4356 amdgpu_ring_write(ring, lower_32_bits(csa_addr + offsetof(struct v9_gfx_meta_data, ce_payload)));
4357 amdgpu_ring_write(ring, upper_32_bits(csa_addr + offsetof(struct v9_gfx_meta_data, ce_payload)));
4358 amdgpu_ring_write_multiple(ring, (void *)&ce_payload, sizeof(ce_payload) >> 2);
4361 static void gfx_v9_0_ring_emit_de_meta(struct amdgpu_ring *ring)
4363 struct v9_de_ib_state de_payload = {0};
4364 uint64_t csa_addr, gds_addr;
4367 csa_addr = amdgpu_csa_vaddr(ring->adev);
4368 gds_addr = csa_addr + 4096;
4369 de_payload.gds_backup_addrlo = lower_32_bits(gds_addr);
4370 de_payload.gds_backup_addrhi = upper_32_bits(gds_addr);
4372 cnt = (sizeof(de_payload) >> 2) + 4 - 2;
4373 amdgpu_ring_write(ring, PACKET3(PACKET3_WRITE_DATA, cnt));
4374 amdgpu_ring_write(ring, (WRITE_DATA_ENGINE_SEL(1) |
4375 WRITE_DATA_DST_SEL(8) |
4377 WRITE_DATA_CACHE_POLICY(0));
4378 amdgpu_ring_write(ring, lower_32_bits(csa_addr + offsetof(struct v9_gfx_meta_data, de_payload)));
4379 amdgpu_ring_write(ring, upper_32_bits(csa_addr + offsetof(struct v9_gfx_meta_data, de_payload)));
4380 amdgpu_ring_write_multiple(ring, (void *)&de_payload, sizeof(de_payload) >> 2);
4383 static void gfx_v9_0_ring_emit_tmz(struct amdgpu_ring *ring, bool start)
4385 amdgpu_ring_write(ring, PACKET3(PACKET3_FRAME_CONTROL, 0));
4386 amdgpu_ring_write(ring, FRAME_CMD(start ? 0 : 1)); /* frame_end */
4389 static void gfx_v9_ring_emit_cntxcntl(struct amdgpu_ring *ring, uint32_t flags)
4393 if (amdgpu_sriov_vf(ring->adev))
4394 gfx_v9_0_ring_emit_ce_meta(ring);
4396 gfx_v9_0_ring_emit_tmz(ring, true);
4398 dw2 |= 0x80000000; /* set load_enable otherwise this package is just NOPs */
4399 if (flags & AMDGPU_HAVE_CTX_SWITCH) {
4400 /* set load_global_config & load_global_uconfig */
4402 /* set load_cs_sh_regs */
4404 /* set load_per_context_state & load_gfx_sh_regs for GFX */
4407 /* set load_ce_ram if preamble presented */
4408 if (AMDGPU_PREAMBLE_IB_PRESENT & flags)
4411 /* still load_ce_ram if this is the first time preamble presented
4412 * although there is no context switch happens.
4414 if (AMDGPU_PREAMBLE_IB_PRESENT_FIRST & flags)
4418 amdgpu_ring_write(ring, PACKET3(PACKET3_CONTEXT_CONTROL, 1));
4419 amdgpu_ring_write(ring, dw2);
4420 amdgpu_ring_write(ring, 0);
4423 static unsigned gfx_v9_0_ring_emit_init_cond_exec(struct amdgpu_ring *ring)
4426 amdgpu_ring_write(ring, PACKET3(PACKET3_COND_EXEC, 3));
4427 amdgpu_ring_write(ring, lower_32_bits(ring->cond_exe_gpu_addr));
4428 amdgpu_ring_write(ring, upper_32_bits(ring->cond_exe_gpu_addr));
4429 amdgpu_ring_write(ring, 0); /* discard following DWs if *cond_exec_gpu_addr==0 */
4430 ret = ring->wptr & ring->buf_mask;
4431 amdgpu_ring_write(ring, 0x55aa55aa); /* patch dummy value later */
4435 static void gfx_v9_0_ring_emit_patch_cond_exec(struct amdgpu_ring *ring, unsigned offset)
4438 BUG_ON(offset > ring->buf_mask);
4439 BUG_ON(ring->ring[offset] != 0x55aa55aa);
4441 cur = (ring->wptr & ring->buf_mask) - 1;
4442 if (likely(cur > offset))
4443 ring->ring[offset] = cur - offset;
4445 ring->ring[offset] = (ring->ring_size>>2) - offset + cur;
4448 static void gfx_v9_0_ring_emit_rreg(struct amdgpu_ring *ring, uint32_t reg)
4450 struct amdgpu_device *adev = ring->adev;
4452 amdgpu_ring_write(ring, PACKET3(PACKET3_COPY_DATA, 4));
4453 amdgpu_ring_write(ring, 0 | /* src: register*/
4454 (5 << 8) | /* dst: memory */
4455 (1 << 20)); /* write confirm */
4456 amdgpu_ring_write(ring, reg);
4457 amdgpu_ring_write(ring, 0);
4458 amdgpu_ring_write(ring, lower_32_bits(adev->wb.gpu_addr +
4459 adev->virt.reg_val_offs * 4));
4460 amdgpu_ring_write(ring, upper_32_bits(adev->wb.gpu_addr +
4461 adev->virt.reg_val_offs * 4));
4464 static void gfx_v9_0_ring_emit_wreg(struct amdgpu_ring *ring, uint32_t reg,
4469 switch (ring->funcs->type) {
4470 case AMDGPU_RING_TYPE_GFX:
4471 cmd = WRITE_DATA_ENGINE_SEL(1) | WR_CONFIRM;
4473 case AMDGPU_RING_TYPE_KIQ:
4474 cmd = (1 << 16); /* no inc addr */
4480 amdgpu_ring_write(ring, PACKET3(PACKET3_WRITE_DATA, 3));
4481 amdgpu_ring_write(ring, cmd);
4482 amdgpu_ring_write(ring, reg);
4483 amdgpu_ring_write(ring, 0);
4484 amdgpu_ring_write(ring, val);
4487 static void gfx_v9_0_ring_emit_reg_wait(struct amdgpu_ring *ring, uint32_t reg,
4488 uint32_t val, uint32_t mask)
4490 gfx_v9_0_wait_reg_mem(ring, 0, 0, 0, reg, 0, val, mask, 0x20);
4493 static void gfx_v9_0_ring_emit_reg_write_reg_wait(struct amdgpu_ring *ring,
4494 uint32_t reg0, uint32_t reg1,
4495 uint32_t ref, uint32_t mask)
4497 int usepfp = (ring->funcs->type == AMDGPU_RING_TYPE_GFX);
4498 struct amdgpu_device *adev = ring->adev;
4499 bool fw_version_ok = (ring->funcs->type == AMDGPU_RING_TYPE_GFX) ?
4500 adev->gfx.me_fw_write_wait : adev->gfx.mec_fw_write_wait;
4503 gfx_v9_0_wait_reg_mem(ring, usepfp, 0, 1, reg0, reg1,
4506 amdgpu_ring_emit_reg_write_reg_wait_helper(ring, reg0, reg1,
4510 static void gfx_v9_0_ring_soft_recovery(struct amdgpu_ring *ring, unsigned vmid)
4512 struct amdgpu_device *adev = ring->adev;
4515 value = REG_SET_FIELD(value, SQ_CMD, CMD, 0x03);
4516 value = REG_SET_FIELD(value, SQ_CMD, MODE, 0x01);
4517 value = REG_SET_FIELD(value, SQ_CMD, CHECK_VMID, 1);
4518 value = REG_SET_FIELD(value, SQ_CMD, VM_ID, vmid);
4519 WREG32(mmSQ_CMD, value);
4522 static void gfx_v9_0_set_gfx_eop_interrupt_state(struct amdgpu_device *adev,
4523 enum amdgpu_interrupt_state state)
4526 case AMDGPU_IRQ_STATE_DISABLE:
4527 case AMDGPU_IRQ_STATE_ENABLE:
4528 WREG32_FIELD15(GC, 0, CP_INT_CNTL_RING0,
4529 TIME_STAMP_INT_ENABLE,
4530 state == AMDGPU_IRQ_STATE_ENABLE ? 1 : 0);
4537 static void gfx_v9_0_set_compute_eop_interrupt_state(struct amdgpu_device *adev,
4539 enum amdgpu_interrupt_state state)
4541 u32 mec_int_cntl, mec_int_cntl_reg;
4544 * amdgpu controls only the first MEC. That's why this function only
4545 * handles the setting of interrupts for this specific MEC. All other
4546 * pipes' interrupts are set by amdkfd.
4552 mec_int_cntl_reg = SOC15_REG_OFFSET(GC, 0, mmCP_ME1_PIPE0_INT_CNTL);
4555 mec_int_cntl_reg = SOC15_REG_OFFSET(GC, 0, mmCP_ME1_PIPE1_INT_CNTL);
4558 mec_int_cntl_reg = SOC15_REG_OFFSET(GC, 0, mmCP_ME1_PIPE2_INT_CNTL);
4561 mec_int_cntl_reg = SOC15_REG_OFFSET(GC, 0, mmCP_ME1_PIPE3_INT_CNTL);
4564 DRM_DEBUG("invalid pipe %d\n", pipe);
4568 DRM_DEBUG("invalid me %d\n", me);
4573 case AMDGPU_IRQ_STATE_DISABLE:
4574 mec_int_cntl = RREG32(mec_int_cntl_reg);
4575 mec_int_cntl = REG_SET_FIELD(mec_int_cntl, CP_ME1_PIPE0_INT_CNTL,
4576 TIME_STAMP_INT_ENABLE, 0);
4577 WREG32(mec_int_cntl_reg, mec_int_cntl);
4579 case AMDGPU_IRQ_STATE_ENABLE:
4580 mec_int_cntl = RREG32(mec_int_cntl_reg);
4581 mec_int_cntl = REG_SET_FIELD(mec_int_cntl, CP_ME1_PIPE0_INT_CNTL,
4582 TIME_STAMP_INT_ENABLE, 1);
4583 WREG32(mec_int_cntl_reg, mec_int_cntl);
4590 static int gfx_v9_0_set_priv_reg_fault_state(struct amdgpu_device *adev,
4591 struct amdgpu_irq_src *source,
4593 enum amdgpu_interrupt_state state)
4596 case AMDGPU_IRQ_STATE_DISABLE:
4597 case AMDGPU_IRQ_STATE_ENABLE:
4598 WREG32_FIELD15(GC, 0, CP_INT_CNTL_RING0,
4599 PRIV_REG_INT_ENABLE,
4600 state == AMDGPU_IRQ_STATE_ENABLE ? 1 : 0);
4609 static int gfx_v9_0_set_priv_inst_fault_state(struct amdgpu_device *adev,
4610 struct amdgpu_irq_src *source,
4612 enum amdgpu_interrupt_state state)
4615 case AMDGPU_IRQ_STATE_DISABLE:
4616 case AMDGPU_IRQ_STATE_ENABLE:
4617 WREG32_FIELD15(GC, 0, CP_INT_CNTL_RING0,
4618 PRIV_INSTR_INT_ENABLE,
4619 state == AMDGPU_IRQ_STATE_ENABLE ? 1 : 0);
4627 static int gfx_v9_0_set_eop_interrupt_state(struct amdgpu_device *adev,
4628 struct amdgpu_irq_src *src,
4630 enum amdgpu_interrupt_state state)
4633 case AMDGPU_CP_IRQ_GFX_EOP:
4634 gfx_v9_0_set_gfx_eop_interrupt_state(adev, state);
4636 case AMDGPU_CP_IRQ_COMPUTE_MEC1_PIPE0_EOP:
4637 gfx_v9_0_set_compute_eop_interrupt_state(adev, 1, 0, state);
4639 case AMDGPU_CP_IRQ_COMPUTE_MEC1_PIPE1_EOP:
4640 gfx_v9_0_set_compute_eop_interrupt_state(adev, 1, 1, state);
4642 case AMDGPU_CP_IRQ_COMPUTE_MEC1_PIPE2_EOP:
4643 gfx_v9_0_set_compute_eop_interrupt_state(adev, 1, 2, state);
4645 case AMDGPU_CP_IRQ_COMPUTE_MEC1_PIPE3_EOP:
4646 gfx_v9_0_set_compute_eop_interrupt_state(adev, 1, 3, state);
4648 case AMDGPU_CP_IRQ_COMPUTE_MEC2_PIPE0_EOP:
4649 gfx_v9_0_set_compute_eop_interrupt_state(adev, 2, 0, state);
4651 case AMDGPU_CP_IRQ_COMPUTE_MEC2_PIPE1_EOP:
4652 gfx_v9_0_set_compute_eop_interrupt_state(adev, 2, 1, state);
4654 case AMDGPU_CP_IRQ_COMPUTE_MEC2_PIPE2_EOP:
4655 gfx_v9_0_set_compute_eop_interrupt_state(adev, 2, 2, state);
4657 case AMDGPU_CP_IRQ_COMPUTE_MEC2_PIPE3_EOP:
4658 gfx_v9_0_set_compute_eop_interrupt_state(adev, 2, 3, state);
4666 static int gfx_v9_0_eop_irq(struct amdgpu_device *adev,
4667 struct amdgpu_irq_src *source,
4668 struct amdgpu_iv_entry *entry)
4671 u8 me_id, pipe_id, queue_id;
4672 struct amdgpu_ring *ring;
4674 DRM_DEBUG("IH: CP EOP\n");
4675 me_id = (entry->ring_id & 0x0c) >> 2;
4676 pipe_id = (entry->ring_id & 0x03) >> 0;
4677 queue_id = (entry->ring_id & 0x70) >> 4;
4681 amdgpu_fence_process(&adev->gfx.gfx_ring[0]);
4685 for (i = 0; i < adev->gfx.num_compute_rings; i++) {
4686 ring = &adev->gfx.compute_ring[i];
4687 /* Per-queue interrupt is supported for MEC starting from VI.
4688 * The interrupt can only be enabled/disabled per pipe instead of per queue.
4690 if ((ring->me == me_id) && (ring->pipe == pipe_id) && (ring->queue == queue_id))
4691 amdgpu_fence_process(ring);
4698 static void gfx_v9_0_fault(struct amdgpu_device *adev,
4699 struct amdgpu_iv_entry *entry)
4701 u8 me_id, pipe_id, queue_id;
4702 struct amdgpu_ring *ring;
4705 me_id = (entry->ring_id & 0x0c) >> 2;
4706 pipe_id = (entry->ring_id & 0x03) >> 0;
4707 queue_id = (entry->ring_id & 0x70) >> 4;
4711 drm_sched_fault(&adev->gfx.gfx_ring[0].sched);
4715 for (i = 0; i < adev->gfx.num_compute_rings; i++) {
4716 ring = &adev->gfx.compute_ring[i];
4717 if (ring->me == me_id && ring->pipe == pipe_id &&
4718 ring->queue == queue_id)
4719 drm_sched_fault(&ring->sched);
4725 static int gfx_v9_0_priv_reg_irq(struct amdgpu_device *adev,
4726 struct amdgpu_irq_src *source,
4727 struct amdgpu_iv_entry *entry)
4729 DRM_ERROR("Illegal register access in command stream\n");
4730 gfx_v9_0_fault(adev, entry);
4734 static int gfx_v9_0_priv_inst_irq(struct amdgpu_device *adev,
4735 struct amdgpu_irq_src *source,
4736 struct amdgpu_iv_entry *entry)
4738 DRM_ERROR("Illegal instruction in command stream\n");
4739 gfx_v9_0_fault(adev, entry);
4743 static const struct amd_ip_funcs gfx_v9_0_ip_funcs = {
4745 .early_init = gfx_v9_0_early_init,
4746 .late_init = gfx_v9_0_late_init,
4747 .sw_init = gfx_v9_0_sw_init,
4748 .sw_fini = gfx_v9_0_sw_fini,
4749 .hw_init = gfx_v9_0_hw_init,
4750 .hw_fini = gfx_v9_0_hw_fini,
4751 .suspend = gfx_v9_0_suspend,
4752 .resume = gfx_v9_0_resume,
4753 .is_idle = gfx_v9_0_is_idle,
4754 .wait_for_idle = gfx_v9_0_wait_for_idle,
4755 .soft_reset = gfx_v9_0_soft_reset,
4756 .set_clockgating_state = gfx_v9_0_set_clockgating_state,
4757 .set_powergating_state = gfx_v9_0_set_powergating_state,
4758 .get_clockgating_state = gfx_v9_0_get_clockgating_state,
4761 static const struct amdgpu_ring_funcs gfx_v9_0_ring_funcs_gfx = {
4762 .type = AMDGPU_RING_TYPE_GFX,
4764 .nop = PACKET3(PACKET3_NOP, 0x3FFF),
4765 .support_64bit_ptrs = true,
4766 .vmhub = AMDGPU_GFXHUB,
4767 .get_rptr = gfx_v9_0_ring_get_rptr_gfx,
4768 .get_wptr = gfx_v9_0_ring_get_wptr_gfx,
4769 .set_wptr = gfx_v9_0_ring_set_wptr_gfx,
4770 .emit_frame_size = /* totally 242 maximum if 16 IBs */
4772 7 + /* PIPELINE_SYNC */
4773 SOC15_FLUSH_GPU_TLB_NUM_WREG * 5 +
4774 SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 7 +
4776 8 + /* FENCE for VM_FLUSH */
4777 20 + /* GDS switch */
4778 4 + /* double SWITCH_BUFFER,
4779 the first COND_EXEC jump to the place just
4780 prior to this double SWITCH_BUFFER */
4788 8 + 8 + /* FENCE x2 */
4789 2, /* SWITCH_BUFFER */
4790 .emit_ib_size = 4, /* gfx_v9_0_ring_emit_ib_gfx */
4791 .emit_ib = gfx_v9_0_ring_emit_ib_gfx,
4792 .emit_fence = gfx_v9_0_ring_emit_fence,
4793 .emit_pipeline_sync = gfx_v9_0_ring_emit_pipeline_sync,
4794 .emit_vm_flush = gfx_v9_0_ring_emit_vm_flush,
4795 .emit_gds_switch = gfx_v9_0_ring_emit_gds_switch,
4796 .emit_hdp_flush = gfx_v9_0_ring_emit_hdp_flush,
4797 .test_ring = gfx_v9_0_ring_test_ring,
4798 .test_ib = gfx_v9_0_ring_test_ib,
4799 .insert_nop = amdgpu_ring_insert_nop,
4800 .pad_ib = amdgpu_ring_generic_pad_ib,
4801 .emit_switch_buffer = gfx_v9_ring_emit_sb,
4802 .emit_cntxcntl = gfx_v9_ring_emit_cntxcntl,
4803 .init_cond_exec = gfx_v9_0_ring_emit_init_cond_exec,
4804 .patch_cond_exec = gfx_v9_0_ring_emit_patch_cond_exec,
4805 .emit_tmz = gfx_v9_0_ring_emit_tmz,
4806 .emit_wreg = gfx_v9_0_ring_emit_wreg,
4807 .emit_reg_wait = gfx_v9_0_ring_emit_reg_wait,
4808 .emit_reg_write_reg_wait = gfx_v9_0_ring_emit_reg_write_reg_wait,
4809 .soft_recovery = gfx_v9_0_ring_soft_recovery,
4812 static const struct amdgpu_ring_funcs gfx_v9_0_ring_funcs_compute = {
4813 .type = AMDGPU_RING_TYPE_COMPUTE,
4815 .nop = PACKET3(PACKET3_NOP, 0x3FFF),
4816 .support_64bit_ptrs = true,
4817 .vmhub = AMDGPU_GFXHUB,
4818 .get_rptr = gfx_v9_0_ring_get_rptr_compute,
4819 .get_wptr = gfx_v9_0_ring_get_wptr_compute,
4820 .set_wptr = gfx_v9_0_ring_set_wptr_compute,
4822 20 + /* gfx_v9_0_ring_emit_gds_switch */
4823 7 + /* gfx_v9_0_ring_emit_hdp_flush */
4824 5 + /* hdp invalidate */
4825 7 + /* gfx_v9_0_ring_emit_pipeline_sync */
4826 SOC15_FLUSH_GPU_TLB_NUM_WREG * 5 +
4827 SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 7 +
4828 2 + /* gfx_v9_0_ring_emit_vm_flush */
4829 8 + 8 + 8, /* gfx_v9_0_ring_emit_fence x3 for user fence, vm fence */
4830 .emit_ib_size = 4, /* gfx_v9_0_ring_emit_ib_compute */
4831 .emit_ib = gfx_v9_0_ring_emit_ib_compute,
4832 .emit_fence = gfx_v9_0_ring_emit_fence,
4833 .emit_pipeline_sync = gfx_v9_0_ring_emit_pipeline_sync,
4834 .emit_vm_flush = gfx_v9_0_ring_emit_vm_flush,
4835 .emit_gds_switch = gfx_v9_0_ring_emit_gds_switch,
4836 .emit_hdp_flush = gfx_v9_0_ring_emit_hdp_flush,
4837 .test_ring = gfx_v9_0_ring_test_ring,
4838 .test_ib = gfx_v9_0_ring_test_ib,
4839 .insert_nop = amdgpu_ring_insert_nop,
4840 .pad_ib = amdgpu_ring_generic_pad_ib,
4841 .set_priority = gfx_v9_0_ring_set_priority_compute,
4842 .emit_wreg = gfx_v9_0_ring_emit_wreg,
4843 .emit_reg_wait = gfx_v9_0_ring_emit_reg_wait,
4844 .emit_reg_write_reg_wait = gfx_v9_0_ring_emit_reg_write_reg_wait,
4847 static const struct amdgpu_ring_funcs gfx_v9_0_ring_funcs_kiq = {
4848 .type = AMDGPU_RING_TYPE_KIQ,
4850 .nop = PACKET3(PACKET3_NOP, 0x3FFF),
4851 .support_64bit_ptrs = true,
4852 .vmhub = AMDGPU_GFXHUB,
4853 .get_rptr = gfx_v9_0_ring_get_rptr_compute,
4854 .get_wptr = gfx_v9_0_ring_get_wptr_compute,
4855 .set_wptr = gfx_v9_0_ring_set_wptr_compute,
4857 20 + /* gfx_v9_0_ring_emit_gds_switch */
4858 7 + /* gfx_v9_0_ring_emit_hdp_flush */
4859 5 + /* hdp invalidate */
4860 7 + /* gfx_v9_0_ring_emit_pipeline_sync */
4861 SOC15_FLUSH_GPU_TLB_NUM_WREG * 5 +
4862 SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 7 +
4863 2 + /* gfx_v9_0_ring_emit_vm_flush */
4864 8 + 8 + 8, /* gfx_v9_0_ring_emit_fence_kiq x3 for user fence, vm fence */
4865 .emit_ib_size = 4, /* gfx_v9_0_ring_emit_ib_compute */
4866 .emit_ib = gfx_v9_0_ring_emit_ib_compute,
4867 .emit_fence = gfx_v9_0_ring_emit_fence_kiq,
4868 .test_ring = gfx_v9_0_ring_test_ring,
4869 .test_ib = gfx_v9_0_ring_test_ib,
4870 .insert_nop = amdgpu_ring_insert_nop,
4871 .pad_ib = amdgpu_ring_generic_pad_ib,
4872 .emit_rreg = gfx_v9_0_ring_emit_rreg,
4873 .emit_wreg = gfx_v9_0_ring_emit_wreg,
4874 .emit_reg_wait = gfx_v9_0_ring_emit_reg_wait,
4875 .emit_reg_write_reg_wait = gfx_v9_0_ring_emit_reg_write_reg_wait,
4878 static void gfx_v9_0_set_ring_funcs(struct amdgpu_device *adev)
4882 adev->gfx.kiq.ring.funcs = &gfx_v9_0_ring_funcs_kiq;
4884 for (i = 0; i < adev->gfx.num_gfx_rings; i++)
4885 adev->gfx.gfx_ring[i].funcs = &gfx_v9_0_ring_funcs_gfx;
4887 for (i = 0; i < adev->gfx.num_compute_rings; i++)
4888 adev->gfx.compute_ring[i].funcs = &gfx_v9_0_ring_funcs_compute;
4891 static const struct amdgpu_irq_src_funcs gfx_v9_0_eop_irq_funcs = {
4892 .set = gfx_v9_0_set_eop_interrupt_state,
4893 .process = gfx_v9_0_eop_irq,
4896 static const struct amdgpu_irq_src_funcs gfx_v9_0_priv_reg_irq_funcs = {
4897 .set = gfx_v9_0_set_priv_reg_fault_state,
4898 .process = gfx_v9_0_priv_reg_irq,
4901 static const struct amdgpu_irq_src_funcs gfx_v9_0_priv_inst_irq_funcs = {
4902 .set = gfx_v9_0_set_priv_inst_fault_state,
4903 .process = gfx_v9_0_priv_inst_irq,
4906 static void gfx_v9_0_set_irq_funcs(struct amdgpu_device *adev)
4908 adev->gfx.eop_irq.num_types = AMDGPU_CP_IRQ_LAST;
4909 adev->gfx.eop_irq.funcs = &gfx_v9_0_eop_irq_funcs;
4911 adev->gfx.priv_reg_irq.num_types = 1;
4912 adev->gfx.priv_reg_irq.funcs = &gfx_v9_0_priv_reg_irq_funcs;
4914 adev->gfx.priv_inst_irq.num_types = 1;
4915 adev->gfx.priv_inst_irq.funcs = &gfx_v9_0_priv_inst_irq_funcs;
4918 static void gfx_v9_0_set_rlc_funcs(struct amdgpu_device *adev)
4920 switch (adev->asic_type) {
4925 adev->gfx.rlc.funcs = &gfx_v9_0_rlc_funcs;
4932 static void gfx_v9_0_set_gds_init(struct amdgpu_device *adev)
4934 /* init asci gds info */
4935 switch (adev->asic_type) {
4939 adev->gds.mem.total_size = 0x10000;
4942 adev->gds.mem.total_size = 0x1000;
4945 adev->gds.mem.total_size = 0x10000;
4949 adev->gds.gws.total_size = 64;
4950 adev->gds.oa.total_size = 16;
4952 if (adev->gds.mem.total_size == 64 * 1024) {
4953 adev->gds.mem.gfx_partition_size = 4096;
4954 adev->gds.mem.cs_partition_size = 4096;
4956 adev->gds.gws.gfx_partition_size = 4;
4957 adev->gds.gws.cs_partition_size = 4;
4959 adev->gds.oa.gfx_partition_size = 4;
4960 adev->gds.oa.cs_partition_size = 1;
4962 adev->gds.mem.gfx_partition_size = 1024;
4963 adev->gds.mem.cs_partition_size = 1024;
4965 adev->gds.gws.gfx_partition_size = 16;
4966 adev->gds.gws.cs_partition_size = 16;
4968 adev->gds.oa.gfx_partition_size = 4;
4969 adev->gds.oa.cs_partition_size = 4;
4973 static void gfx_v9_0_set_user_cu_inactive_bitmap(struct amdgpu_device *adev,
4981 data = bitmap << GC_USER_SHADER_ARRAY_CONFIG__INACTIVE_CUS__SHIFT;
4982 data &= GC_USER_SHADER_ARRAY_CONFIG__INACTIVE_CUS_MASK;
4984 WREG32_SOC15(GC, 0, mmGC_USER_SHADER_ARRAY_CONFIG, data);
4987 static u32 gfx_v9_0_get_cu_active_bitmap(struct amdgpu_device *adev)
4991 data = RREG32_SOC15(GC, 0, mmCC_GC_SHADER_ARRAY_CONFIG);
4992 data |= RREG32_SOC15(GC, 0, mmGC_USER_SHADER_ARRAY_CONFIG);
4994 data &= CC_GC_SHADER_ARRAY_CONFIG__INACTIVE_CUS_MASK;
4995 data >>= CC_GC_SHADER_ARRAY_CONFIG__INACTIVE_CUS__SHIFT;
4997 mask = amdgpu_gfx_create_bitmask(adev->gfx.config.max_cu_per_sh);
4999 return (~data) & mask;
5002 static int gfx_v9_0_get_cu_info(struct amdgpu_device *adev,
5003 struct amdgpu_cu_info *cu_info)
5005 int i, j, k, counter, active_cu_number = 0;
5006 u32 mask, bitmap, ao_bitmap, ao_cu_mask = 0;
5007 unsigned disable_masks[4 * 2];
5009 if (!adev || !cu_info)
5012 amdgpu_gfx_parse_disable_cu(disable_masks, 4, 2);
5014 mutex_lock(&adev->grbm_idx_mutex);
5015 for (i = 0; i < adev->gfx.config.max_shader_engines; i++) {
5016 for (j = 0; j < adev->gfx.config.max_sh_per_se; j++) {
5020 gfx_v9_0_select_se_sh(adev, i, j, 0xffffffff);
5022 gfx_v9_0_set_user_cu_inactive_bitmap(
5023 adev, disable_masks[i * 2 + j]);
5024 bitmap = gfx_v9_0_get_cu_active_bitmap(adev);
5025 cu_info->bitmap[i][j] = bitmap;
5027 for (k = 0; k < adev->gfx.config.max_cu_per_sh; k ++) {
5028 if (bitmap & mask) {
5029 if (counter < adev->gfx.config.max_cu_per_sh)
5035 active_cu_number += counter;
5037 ao_cu_mask |= (ao_bitmap << (i * 16 + j * 8));
5038 cu_info->ao_cu_bitmap[i][j] = ao_bitmap;
5041 gfx_v9_0_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
5042 mutex_unlock(&adev->grbm_idx_mutex);
5044 cu_info->number = active_cu_number;
5045 cu_info->ao_cu_mask = ao_cu_mask;
5046 cu_info->simd_per_cu = NUM_SIMD_PER_CU;
5051 const struct amdgpu_ip_block_version gfx_v9_0_ip_block =
5053 .type = AMD_IP_BLOCK_TYPE_GFX,
5057 .funcs = &gfx_v9_0_ip_funcs,