xref: /dflybsd-src/sys/dev/drm/amd/amdgpu/sdma_v3_0.c (revision 809f38025e6f424cb8960d509d59de3ddc7d6b98)
1b843c749SSergey Zigachev /*
2b843c749SSergey Zigachev  * Copyright 2014 Advanced Micro Devices, Inc.
3b843c749SSergey Zigachev  *
4b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
5b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
6b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
7b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
9b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
10b843c749SSergey Zigachev  *
11b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
12b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
13b843c749SSergey Zigachev  *
14b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
21b843c749SSergey Zigachev  *
22b843c749SSergey Zigachev  * Authors: Alex Deucher
23b843c749SSergey Zigachev  */
24b843c749SSergey Zigachev #include <linux/firmware.h>
25b843c749SSergey Zigachev #include <drm/drmP.h>
26b843c749SSergey Zigachev #include "amdgpu.h"
27b843c749SSergey Zigachev #include "amdgpu_ucode.h"
28b843c749SSergey Zigachev #include "amdgpu_trace.h"
29b843c749SSergey Zigachev #include "vi.h"
30b843c749SSergey Zigachev #include "vid.h"
31b843c749SSergey Zigachev 
32b843c749SSergey Zigachev #include "oss/oss_3_0_d.h"
33b843c749SSergey Zigachev #include "oss/oss_3_0_sh_mask.h"
34b843c749SSergey Zigachev 
35b843c749SSergey Zigachev #include "gmc/gmc_8_1_d.h"
36b843c749SSergey Zigachev #include "gmc/gmc_8_1_sh_mask.h"
37b843c749SSergey Zigachev 
38b843c749SSergey Zigachev #include "gca/gfx_8_0_d.h"
39b843c749SSergey Zigachev #include "gca/gfx_8_0_enum.h"
40b843c749SSergey Zigachev #include "gca/gfx_8_0_sh_mask.h"
41b843c749SSergey Zigachev 
42b843c749SSergey Zigachev #include "bif/bif_5_0_d.h"
43b843c749SSergey Zigachev #include "bif/bif_5_0_sh_mask.h"
44b843c749SSergey Zigachev 
45b843c749SSergey Zigachev #include "tonga_sdma_pkt_open.h"
46b843c749SSergey Zigachev 
47b843c749SSergey Zigachev #include "ivsrcid/ivsrcid_vislands30.h"
48b843c749SSergey Zigachev 
49b843c749SSergey Zigachev static void sdma_v3_0_set_ring_funcs(struct amdgpu_device *adev);
50b843c749SSergey Zigachev static void sdma_v3_0_set_buffer_funcs(struct amdgpu_device *adev);
51b843c749SSergey Zigachev static void sdma_v3_0_set_vm_pte_funcs(struct amdgpu_device *adev);
52b843c749SSergey Zigachev static void sdma_v3_0_set_irq_funcs(struct amdgpu_device *adev);
53b843c749SSergey Zigachev 
54*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_tonga_sdma");
55*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_tonga_sdma1");
56*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_carrizo_sdma");
57*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_carrizo_sdma1");
58*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_fiji_sdma");
59*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_fiji_sdma1");
60*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_stoney_sdma");
61*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_polaris10_sdma");
62*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_polaris10_sdma1");
63*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_polaris11_sdma");
64*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_polaris11_sdma1");
65*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_polaris12_sdma");
66*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_polaris12_sdma1");
67*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_vegam_sdma");
68*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_vegam_sdma1");
69b843c749SSergey Zigachev 
70b843c749SSergey Zigachev 
71b843c749SSergey Zigachev static const u32 sdma_offsets[SDMA_MAX_INSTANCE] =
72b843c749SSergey Zigachev {
73b843c749SSergey Zigachev 	SDMA0_REGISTER_OFFSET,
74b843c749SSergey Zigachev 	SDMA1_REGISTER_OFFSET
75b843c749SSergey Zigachev };
76b843c749SSergey Zigachev 
77b843c749SSergey Zigachev static const u32 golden_settings_tonga_a11[] =
78b843c749SSergey Zigachev {
79b843c749SSergey Zigachev 	mmSDMA0_CHICKEN_BITS, 0xfc910007, 0x00810007,
80b843c749SSergey Zigachev 	mmSDMA0_CLK_CTRL, 0xff000fff, 0x00000000,
81b843c749SSergey Zigachev 	mmSDMA0_GFX_IB_CNTL, 0x800f0111, 0x00000100,
82b843c749SSergey Zigachev 	mmSDMA0_RLC0_IB_CNTL, 0x800f0111, 0x00000100,
83b843c749SSergey Zigachev 	mmSDMA0_RLC1_IB_CNTL, 0x800f0111, 0x00000100,
84b843c749SSergey Zigachev 	mmSDMA1_CHICKEN_BITS, 0xfc910007, 0x00810007,
85b843c749SSergey Zigachev 	mmSDMA1_CLK_CTRL, 0xff000fff, 0x00000000,
86b843c749SSergey Zigachev 	mmSDMA1_GFX_IB_CNTL, 0x800f0111, 0x00000100,
87b843c749SSergey Zigachev 	mmSDMA1_RLC0_IB_CNTL, 0x800f0111, 0x00000100,
88b843c749SSergey Zigachev 	mmSDMA1_RLC1_IB_CNTL, 0x800f0111, 0x00000100,
89b843c749SSergey Zigachev };
90b843c749SSergey Zigachev 
91b843c749SSergey Zigachev static const u32 tonga_mgcg_cgcg_init[] =
92b843c749SSergey Zigachev {
93b843c749SSergey Zigachev 	mmSDMA0_CLK_CTRL, 0xff000ff0, 0x00000100,
94b843c749SSergey Zigachev 	mmSDMA1_CLK_CTRL, 0xff000ff0, 0x00000100
95b843c749SSergey Zigachev };
96b843c749SSergey Zigachev 
97b843c749SSergey Zigachev static const u32 golden_settings_fiji_a10[] =
98b843c749SSergey Zigachev {
99b843c749SSergey Zigachev 	mmSDMA0_CHICKEN_BITS, 0xfc910007, 0x00810007,
100b843c749SSergey Zigachev 	mmSDMA0_GFX_IB_CNTL, 0x800f0111, 0x00000100,
101b843c749SSergey Zigachev 	mmSDMA0_RLC0_IB_CNTL, 0x800f0111, 0x00000100,
102b843c749SSergey Zigachev 	mmSDMA0_RLC1_IB_CNTL, 0x800f0111, 0x00000100,
103b843c749SSergey Zigachev 	mmSDMA1_CHICKEN_BITS, 0xfc910007, 0x00810007,
104b843c749SSergey Zigachev 	mmSDMA1_GFX_IB_CNTL, 0x800f0111, 0x00000100,
105b843c749SSergey Zigachev 	mmSDMA1_RLC0_IB_CNTL, 0x800f0111, 0x00000100,
106b843c749SSergey Zigachev 	mmSDMA1_RLC1_IB_CNTL, 0x800f0111, 0x00000100,
107b843c749SSergey Zigachev };
108b843c749SSergey Zigachev 
109b843c749SSergey Zigachev static const u32 fiji_mgcg_cgcg_init[] =
110b843c749SSergey Zigachev {
111b843c749SSergey Zigachev 	mmSDMA0_CLK_CTRL, 0xff000ff0, 0x00000100,
112b843c749SSergey Zigachev 	mmSDMA1_CLK_CTRL, 0xff000ff0, 0x00000100
113b843c749SSergey Zigachev };
114b843c749SSergey Zigachev 
115b843c749SSergey Zigachev static const u32 golden_settings_polaris11_a11[] =
116b843c749SSergey Zigachev {
117b843c749SSergey Zigachev 	mmSDMA0_CHICKEN_BITS, 0xfc910007, 0x00810007,
118b843c749SSergey Zigachev 	mmSDMA0_CLK_CTRL, 0xff000fff, 0x00000000,
119b843c749SSergey Zigachev 	mmSDMA0_GFX_IB_CNTL, 0x800f0111, 0x00000100,
120b843c749SSergey Zigachev 	mmSDMA0_RLC0_IB_CNTL, 0x800f0111, 0x00000100,
121b843c749SSergey Zigachev 	mmSDMA0_RLC1_IB_CNTL, 0x800f0111, 0x00000100,
122b843c749SSergey Zigachev 	mmSDMA1_CHICKEN_BITS, 0xfc910007, 0x00810007,
123b843c749SSergey Zigachev 	mmSDMA1_CLK_CTRL, 0xff000fff, 0x00000000,
124b843c749SSergey Zigachev 	mmSDMA1_GFX_IB_CNTL, 0x800f0111, 0x00000100,
125b843c749SSergey Zigachev 	mmSDMA1_RLC0_IB_CNTL, 0x800f0111, 0x00000100,
126b843c749SSergey Zigachev 	mmSDMA1_RLC1_IB_CNTL, 0x800f0111, 0x00000100,
127b843c749SSergey Zigachev };
128b843c749SSergey Zigachev 
129b843c749SSergey Zigachev static const u32 golden_settings_polaris10_a11[] =
130b843c749SSergey Zigachev {
131b843c749SSergey Zigachev 	mmSDMA0_CHICKEN_BITS, 0xfc910007, 0x00810007,
132b843c749SSergey Zigachev 	mmSDMA0_CLK_CTRL, 0xff000fff, 0x00000000,
133b843c749SSergey Zigachev 	mmSDMA0_GFX_IB_CNTL, 0x800f0111, 0x00000100,
134b843c749SSergey Zigachev 	mmSDMA0_RLC0_IB_CNTL, 0x800f0111, 0x00000100,
135b843c749SSergey Zigachev 	mmSDMA0_RLC1_IB_CNTL, 0x800f0111, 0x00000100,
136b843c749SSergey Zigachev 	mmSDMA1_CHICKEN_BITS, 0xfc910007, 0x00810007,
137b843c749SSergey Zigachev 	mmSDMA1_CLK_CTRL, 0xff000fff, 0x00000000,
138b843c749SSergey Zigachev 	mmSDMA1_GFX_IB_CNTL, 0x800f0111, 0x00000100,
139b843c749SSergey Zigachev 	mmSDMA1_RLC0_IB_CNTL, 0x800f0111, 0x00000100,
140b843c749SSergey Zigachev 	mmSDMA1_RLC1_IB_CNTL, 0x800f0111, 0x00000100,
141b843c749SSergey Zigachev };
142b843c749SSergey Zigachev 
143b843c749SSergey Zigachev static const u32 cz_golden_settings_a11[] =
144b843c749SSergey Zigachev {
145b843c749SSergey Zigachev 	mmSDMA0_CHICKEN_BITS, 0xfc910007, 0x00810007,
146b843c749SSergey Zigachev 	mmSDMA0_CLK_CTRL, 0xff000fff, 0x00000000,
147b843c749SSergey Zigachev 	mmSDMA0_GFX_IB_CNTL, 0x00000100, 0x00000100,
148b843c749SSergey Zigachev 	mmSDMA0_POWER_CNTL, 0x00000800, 0x0003c800,
149b843c749SSergey Zigachev 	mmSDMA0_RLC0_IB_CNTL, 0x00000100, 0x00000100,
150b843c749SSergey Zigachev 	mmSDMA0_RLC1_IB_CNTL, 0x00000100, 0x00000100,
151b843c749SSergey Zigachev 	mmSDMA1_CHICKEN_BITS, 0xfc910007, 0x00810007,
152b843c749SSergey Zigachev 	mmSDMA1_CLK_CTRL, 0xff000fff, 0x00000000,
153b843c749SSergey Zigachev 	mmSDMA1_GFX_IB_CNTL, 0x00000100, 0x00000100,
154b843c749SSergey Zigachev 	mmSDMA1_POWER_CNTL, 0x00000800, 0x0003c800,
155b843c749SSergey Zigachev 	mmSDMA1_RLC0_IB_CNTL, 0x00000100, 0x00000100,
156b843c749SSergey Zigachev 	mmSDMA1_RLC1_IB_CNTL, 0x00000100, 0x00000100,
157b843c749SSergey Zigachev };
158b843c749SSergey Zigachev 
159b843c749SSergey Zigachev static const u32 cz_mgcg_cgcg_init[] =
160b843c749SSergey Zigachev {
161b843c749SSergey Zigachev 	mmSDMA0_CLK_CTRL, 0xff000ff0, 0x00000100,
162b843c749SSergey Zigachev 	mmSDMA1_CLK_CTRL, 0xff000ff0, 0x00000100
163b843c749SSergey Zigachev };
164b843c749SSergey Zigachev 
165b843c749SSergey Zigachev static const u32 stoney_golden_settings_a11[] =
166b843c749SSergey Zigachev {
167b843c749SSergey Zigachev 	mmSDMA0_GFX_IB_CNTL, 0x00000100, 0x00000100,
168b843c749SSergey Zigachev 	mmSDMA0_POWER_CNTL, 0x00000800, 0x0003c800,
169b843c749SSergey Zigachev 	mmSDMA0_RLC0_IB_CNTL, 0x00000100, 0x00000100,
170b843c749SSergey Zigachev 	mmSDMA0_RLC1_IB_CNTL, 0x00000100, 0x00000100,
171b843c749SSergey Zigachev };
172b843c749SSergey Zigachev 
173b843c749SSergey Zigachev static const u32 stoney_mgcg_cgcg_init[] =
174b843c749SSergey Zigachev {
175b843c749SSergey Zigachev 	mmSDMA0_CLK_CTRL, 0xffffffff, 0x00000100,
176b843c749SSergey Zigachev };
177b843c749SSergey Zigachev 
178b843c749SSergey Zigachev /*
179b843c749SSergey Zigachev  * sDMA - System DMA
180b843c749SSergey Zigachev  * Starting with CIK, the GPU has new asynchronous
181b843c749SSergey Zigachev  * DMA engines.  These engines are used for compute
182b843c749SSergey Zigachev  * and gfx.  There are two DMA engines (SDMA0, SDMA1)
183b843c749SSergey Zigachev  * and each one supports 1 ring buffer used for gfx
184b843c749SSergey Zigachev  * and 2 queues used for compute.
185b843c749SSergey Zigachev  *
186b843c749SSergey Zigachev  * The programming model is very similar to the CP
187b843c749SSergey Zigachev  * (ring buffer, IBs, etc.), but sDMA has it's own
188b843c749SSergey Zigachev  * packet format that is different from the PM4 format
189b843c749SSergey Zigachev  * used by the CP. sDMA supports copying data, writing
190b843c749SSergey Zigachev  * embedded data, solid fills, and a number of other
191b843c749SSergey Zigachev  * things.  It also has support for tiling/detiling of
192b843c749SSergey Zigachev  * buffers.
193b843c749SSergey Zigachev  */
194b843c749SSergey Zigachev 
sdma_v3_0_init_golden_registers(struct amdgpu_device * adev)195b843c749SSergey Zigachev static void sdma_v3_0_init_golden_registers(struct amdgpu_device *adev)
196b843c749SSergey Zigachev {
197b843c749SSergey Zigachev 	switch (adev->asic_type) {
198b843c749SSergey Zigachev 	case CHIP_FIJI:
199b843c749SSergey Zigachev 		amdgpu_device_program_register_sequence(adev,
200b843c749SSergey Zigachev 							fiji_mgcg_cgcg_init,
201b843c749SSergey Zigachev 							ARRAY_SIZE(fiji_mgcg_cgcg_init));
202b843c749SSergey Zigachev 		amdgpu_device_program_register_sequence(adev,
203b843c749SSergey Zigachev 							golden_settings_fiji_a10,
204b843c749SSergey Zigachev 							ARRAY_SIZE(golden_settings_fiji_a10));
205b843c749SSergey Zigachev 		break;
206b843c749SSergey Zigachev 	case CHIP_TONGA:
207b843c749SSergey Zigachev 		amdgpu_device_program_register_sequence(adev,
208b843c749SSergey Zigachev 							tonga_mgcg_cgcg_init,
209b843c749SSergey Zigachev 							ARRAY_SIZE(tonga_mgcg_cgcg_init));
210b843c749SSergey Zigachev 		amdgpu_device_program_register_sequence(adev,
211b843c749SSergey Zigachev 							golden_settings_tonga_a11,
212b843c749SSergey Zigachev 							ARRAY_SIZE(golden_settings_tonga_a11));
213b843c749SSergey Zigachev 		break;
214b843c749SSergey Zigachev 	case CHIP_POLARIS11:
215b843c749SSergey Zigachev 	case CHIP_POLARIS12:
216b843c749SSergey Zigachev 	case CHIP_VEGAM:
217b843c749SSergey Zigachev 		amdgpu_device_program_register_sequence(adev,
218b843c749SSergey Zigachev 							golden_settings_polaris11_a11,
219b843c749SSergey Zigachev 							ARRAY_SIZE(golden_settings_polaris11_a11));
220b843c749SSergey Zigachev 		break;
221b843c749SSergey Zigachev 	case CHIP_POLARIS10:
222b843c749SSergey Zigachev 		amdgpu_device_program_register_sequence(adev,
223b843c749SSergey Zigachev 							golden_settings_polaris10_a11,
224b843c749SSergey Zigachev 							ARRAY_SIZE(golden_settings_polaris10_a11));
225b843c749SSergey Zigachev 		break;
226b843c749SSergey Zigachev 	case CHIP_CARRIZO:
227b843c749SSergey Zigachev 		amdgpu_device_program_register_sequence(adev,
228b843c749SSergey Zigachev 							cz_mgcg_cgcg_init,
229b843c749SSergey Zigachev 							ARRAY_SIZE(cz_mgcg_cgcg_init));
230b843c749SSergey Zigachev 		amdgpu_device_program_register_sequence(adev,
231b843c749SSergey Zigachev 							cz_golden_settings_a11,
232b843c749SSergey Zigachev 							ARRAY_SIZE(cz_golden_settings_a11));
233b843c749SSergey Zigachev 		break;
234b843c749SSergey Zigachev 	case CHIP_STONEY:
235b843c749SSergey Zigachev 		amdgpu_device_program_register_sequence(adev,
236b843c749SSergey Zigachev 							stoney_mgcg_cgcg_init,
237b843c749SSergey Zigachev 							ARRAY_SIZE(stoney_mgcg_cgcg_init));
238b843c749SSergey Zigachev 		amdgpu_device_program_register_sequence(adev,
239b843c749SSergey Zigachev 							stoney_golden_settings_a11,
240b843c749SSergey Zigachev 							ARRAY_SIZE(stoney_golden_settings_a11));
241b843c749SSergey Zigachev 		break;
242b843c749SSergey Zigachev 	default:
243b843c749SSergey Zigachev 		break;
244b843c749SSergey Zigachev 	}
245b843c749SSergey Zigachev }
246b843c749SSergey Zigachev 
sdma_v3_0_free_microcode(struct amdgpu_device * adev)247b843c749SSergey Zigachev static void sdma_v3_0_free_microcode(struct amdgpu_device *adev)
248b843c749SSergey Zigachev {
249b843c749SSergey Zigachev 	int i;
250b843c749SSergey Zigachev 	for (i = 0; i < adev->sdma.num_instances; i++) {
251b843c749SSergey Zigachev 		release_firmware(adev->sdma.instance[i].fw);
252b843c749SSergey Zigachev 		adev->sdma.instance[i].fw = NULL;
253b843c749SSergey Zigachev 	}
254b843c749SSergey Zigachev }
255b843c749SSergey Zigachev 
256b843c749SSergey Zigachev /**
257b843c749SSergey Zigachev  * sdma_v3_0_init_microcode - load ucode images from disk
258b843c749SSergey Zigachev  *
259b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
260b843c749SSergey Zigachev  *
261b843c749SSergey Zigachev  * Use the firmware interface to load the ucode images into
262b843c749SSergey Zigachev  * the driver (not loaded into hw).
263b843c749SSergey Zigachev  * Returns 0 on success, error on failure.
264b843c749SSergey Zigachev  */
sdma_v3_0_init_microcode(struct amdgpu_device * adev)265b843c749SSergey Zigachev static int sdma_v3_0_init_microcode(struct amdgpu_device *adev)
266b843c749SSergey Zigachev {
267b843c749SSergey Zigachev 	const char *chip_name;
268b843c749SSergey Zigachev 	char fw_name[30];
269b843c749SSergey Zigachev 	int err = 0, i;
270b843c749SSergey Zigachev 	struct amdgpu_firmware_info *info = NULL;
271b843c749SSergey Zigachev 	const struct common_firmware_header *header = NULL;
272b843c749SSergey Zigachev 	const struct sdma_firmware_header_v1_0 *hdr;
273b843c749SSergey Zigachev 
274b843c749SSergey Zigachev 	DRM_DEBUG("\n");
275b843c749SSergey Zigachev 
276b843c749SSergey Zigachev 	switch (adev->asic_type) {
277b843c749SSergey Zigachev 	case CHIP_TONGA:
278b843c749SSergey Zigachev 		chip_name = "tonga";
279b843c749SSergey Zigachev 		break;
280b843c749SSergey Zigachev 	case CHIP_FIJI:
281b843c749SSergey Zigachev 		chip_name = "fiji";
282b843c749SSergey Zigachev 		break;
283b843c749SSergey Zigachev 	case CHIP_POLARIS10:
284b843c749SSergey Zigachev 		chip_name = "polaris10";
285b843c749SSergey Zigachev 		break;
286b843c749SSergey Zigachev 	case CHIP_POLARIS11:
287b843c749SSergey Zigachev 		chip_name = "polaris11";
288b843c749SSergey Zigachev 		break;
289b843c749SSergey Zigachev 	case CHIP_POLARIS12:
290b843c749SSergey Zigachev 		chip_name = "polaris12";
291b843c749SSergey Zigachev 		break;
292b843c749SSergey Zigachev 	case CHIP_VEGAM:
293b843c749SSergey Zigachev 		chip_name = "vegam";
294b843c749SSergey Zigachev 		break;
295b843c749SSergey Zigachev 	case CHIP_CARRIZO:
296b843c749SSergey Zigachev 		chip_name = "carrizo";
297b843c749SSergey Zigachev 		break;
298b843c749SSergey Zigachev 	case CHIP_STONEY:
299b843c749SSergey Zigachev 		chip_name = "stoney";
300b843c749SSergey Zigachev 		break;
301b843c749SSergey Zigachev 	default: BUG();
302b843c749SSergey Zigachev 	}
303b843c749SSergey Zigachev 
304b843c749SSergey Zigachev 	for (i = 0; i < adev->sdma.num_instances; i++) {
305b843c749SSergey Zigachev 		if (i == 0)
306*809f3802SSergey Zigachev 			snprintf(fw_name, sizeof(fw_name), "amdgpufw_%s_sdma", chip_name);
307b843c749SSergey Zigachev 		else
308*809f3802SSergey Zigachev 			snprintf(fw_name, sizeof(fw_name), "amdgpufw_%s_sdma1", chip_name);
309b843c749SSergey Zigachev 		err = request_firmware(&adev->sdma.instance[i].fw, fw_name, adev->dev);
310b843c749SSergey Zigachev 		if (err)
311b843c749SSergey Zigachev 			goto out;
312b843c749SSergey Zigachev 		err = amdgpu_ucode_validate(adev->sdma.instance[i].fw);
313b843c749SSergey Zigachev 		if (err)
314b843c749SSergey Zigachev 			goto out;
315b843c749SSergey Zigachev 		hdr = (const struct sdma_firmware_header_v1_0 *)adev->sdma.instance[i].fw->data;
316b843c749SSergey Zigachev 		adev->sdma.instance[i].fw_version = le32_to_cpu(hdr->header.ucode_version);
317b843c749SSergey Zigachev 		adev->sdma.instance[i].feature_version = le32_to_cpu(hdr->ucode_feature_version);
318b843c749SSergey Zigachev 		if (adev->sdma.instance[i].feature_version >= 20)
319b843c749SSergey Zigachev 			adev->sdma.instance[i].burst_nop = true;
320b843c749SSergey Zigachev 
321b843c749SSergey Zigachev 		if (adev->firmware.load_type == AMDGPU_FW_LOAD_SMU) {
322b843c749SSergey Zigachev 			info = &adev->firmware.ucode[AMDGPU_UCODE_ID_SDMA0 + i];
323b843c749SSergey Zigachev 			info->ucode_id = AMDGPU_UCODE_ID_SDMA0 + i;
324b843c749SSergey Zigachev 			info->fw = adev->sdma.instance[i].fw;
325b843c749SSergey Zigachev 			header = (const struct common_firmware_header *)info->fw->data;
326b843c749SSergey Zigachev 			adev->firmware.fw_size +=
327b843c749SSergey Zigachev 				ALIGN(le32_to_cpu(header->ucode_size_bytes), PAGE_SIZE);
328b843c749SSergey Zigachev 		}
329b843c749SSergey Zigachev 	}
330b843c749SSergey Zigachev out:
331b843c749SSergey Zigachev 	if (err) {
332b843c749SSergey Zigachev 		pr_err("sdma_v3_0: Failed to load firmware \"%s\"\n", fw_name);
333b843c749SSergey Zigachev 		for (i = 0; i < adev->sdma.num_instances; i++) {
334b843c749SSergey Zigachev 			release_firmware(adev->sdma.instance[i].fw);
335b843c749SSergey Zigachev 			adev->sdma.instance[i].fw = NULL;
336b843c749SSergey Zigachev 		}
337b843c749SSergey Zigachev 	}
338b843c749SSergey Zigachev 	return err;
339b843c749SSergey Zigachev }
340b843c749SSergey Zigachev 
341b843c749SSergey Zigachev /**
342b843c749SSergey Zigachev  * sdma_v3_0_ring_get_rptr - get the current read pointer
343b843c749SSergey Zigachev  *
344b843c749SSergey Zigachev  * @ring: amdgpu ring pointer
345b843c749SSergey Zigachev  *
346b843c749SSergey Zigachev  * Get the current rptr from the hardware (VI+).
347b843c749SSergey Zigachev  */
sdma_v3_0_ring_get_rptr(struct amdgpu_ring * ring)348b843c749SSergey Zigachev static uint64_t sdma_v3_0_ring_get_rptr(struct amdgpu_ring *ring)
349b843c749SSergey Zigachev {
350b843c749SSergey Zigachev 	/* XXX check if swapping is necessary on BE */
351b843c749SSergey Zigachev 	return ring->adev->wb.wb[ring->rptr_offs] >> 2;
352b843c749SSergey Zigachev }
353b843c749SSergey Zigachev 
354b843c749SSergey Zigachev /**
355b843c749SSergey Zigachev  * sdma_v3_0_ring_get_wptr - get the current write pointer
356b843c749SSergey Zigachev  *
357b843c749SSergey Zigachev  * @ring: amdgpu ring pointer
358b843c749SSergey Zigachev  *
359b843c749SSergey Zigachev  * Get the current wptr from the hardware (VI+).
360b843c749SSergey Zigachev  */
sdma_v3_0_ring_get_wptr(struct amdgpu_ring * ring)361b843c749SSergey Zigachev static uint64_t sdma_v3_0_ring_get_wptr(struct amdgpu_ring *ring)
362b843c749SSergey Zigachev {
363b843c749SSergey Zigachev 	struct amdgpu_device *adev = ring->adev;
364b843c749SSergey Zigachev 	u32 wptr;
365b843c749SSergey Zigachev 
366b843c749SSergey Zigachev 	if (ring->use_doorbell || ring->use_pollmem) {
367b843c749SSergey Zigachev 		/* XXX check if swapping is necessary on BE */
368b843c749SSergey Zigachev 		wptr = ring->adev->wb.wb[ring->wptr_offs] >> 2;
369b843c749SSergey Zigachev 	} else {
370b843c749SSergey Zigachev 		wptr = RREG32(mmSDMA0_GFX_RB_WPTR + sdma_offsets[ring->me]) >> 2;
371b843c749SSergey Zigachev 	}
372b843c749SSergey Zigachev 
373b843c749SSergey Zigachev 	return wptr;
374b843c749SSergey Zigachev }
375b843c749SSergey Zigachev 
376b843c749SSergey Zigachev /**
377b843c749SSergey Zigachev  * sdma_v3_0_ring_set_wptr - commit the write pointer
378b843c749SSergey Zigachev  *
379b843c749SSergey Zigachev  * @ring: amdgpu ring pointer
380b843c749SSergey Zigachev  *
381b843c749SSergey Zigachev  * Write the wptr back to the hardware (VI+).
382b843c749SSergey Zigachev  */
sdma_v3_0_ring_set_wptr(struct amdgpu_ring * ring)383b843c749SSergey Zigachev static void sdma_v3_0_ring_set_wptr(struct amdgpu_ring *ring)
384b843c749SSergey Zigachev {
385b843c749SSergey Zigachev 	struct amdgpu_device *adev = ring->adev;
386b843c749SSergey Zigachev 
387b843c749SSergey Zigachev 	if (ring->use_doorbell) {
388b843c749SSergey Zigachev 		u32 *wb = (u32 *)&adev->wb.wb[ring->wptr_offs];
389b843c749SSergey Zigachev 		/* XXX check if swapping is necessary on BE */
390b843c749SSergey Zigachev 		WRITE_ONCE(*wb, (lower_32_bits(ring->wptr) << 2));
391b843c749SSergey Zigachev 		WDOORBELL32(ring->doorbell_index, lower_32_bits(ring->wptr) << 2);
392b843c749SSergey Zigachev 	} else if (ring->use_pollmem) {
393b843c749SSergey Zigachev 		u32 *wb = (u32 *)&adev->wb.wb[ring->wptr_offs];
394b843c749SSergey Zigachev 
395b843c749SSergey Zigachev 		WRITE_ONCE(*wb, (lower_32_bits(ring->wptr) << 2));
396b843c749SSergey Zigachev 	} else {
397b843c749SSergey Zigachev 		WREG32(mmSDMA0_GFX_RB_WPTR + sdma_offsets[ring->me], lower_32_bits(ring->wptr) << 2);
398b843c749SSergey Zigachev 	}
399b843c749SSergey Zigachev }
400b843c749SSergey Zigachev 
sdma_v3_0_ring_insert_nop(struct amdgpu_ring * ring,uint32_t count)401b843c749SSergey Zigachev static void sdma_v3_0_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count)
402b843c749SSergey Zigachev {
403b843c749SSergey Zigachev 	struct amdgpu_sdma_instance *sdma = amdgpu_get_sdma_instance(ring);
404b843c749SSergey Zigachev 	int i;
405b843c749SSergey Zigachev 
406b843c749SSergey Zigachev 	for (i = 0; i < count; i++)
407b843c749SSergey Zigachev 		if (sdma && sdma->burst_nop && (i == 0))
408b843c749SSergey Zigachev 			amdgpu_ring_write(ring, ring->funcs->nop |
409b843c749SSergey Zigachev 				SDMA_PKT_NOP_HEADER_COUNT(count - 1));
410b843c749SSergey Zigachev 		else
411b843c749SSergey Zigachev 			amdgpu_ring_write(ring, ring->funcs->nop);
412b843c749SSergey Zigachev }
413b843c749SSergey Zigachev 
414b843c749SSergey Zigachev /**
415b843c749SSergey Zigachev  * sdma_v3_0_ring_emit_ib - Schedule an IB on the DMA engine
416b843c749SSergey Zigachev  *
417b843c749SSergey Zigachev  * @ring: amdgpu ring pointer
418b843c749SSergey Zigachev  * @ib: IB object to schedule
419b843c749SSergey Zigachev  *
420b843c749SSergey Zigachev  * Schedule an IB in the DMA ring (VI).
421b843c749SSergey Zigachev  */
sdma_v3_0_ring_emit_ib(struct amdgpu_ring * ring,struct amdgpu_ib * ib,unsigned vmid,bool ctx_switch)422b843c749SSergey Zigachev static void sdma_v3_0_ring_emit_ib(struct amdgpu_ring *ring,
423b843c749SSergey Zigachev 				   struct amdgpu_ib *ib,
424b843c749SSergey Zigachev 				   unsigned vmid, bool ctx_switch)
425b843c749SSergey Zigachev {
426b843c749SSergey Zigachev 	/* IB packet must end on a 8 DW boundary */
427b843c749SSergey Zigachev 	sdma_v3_0_ring_insert_nop(ring, (10 - (lower_32_bits(ring->wptr) & 7)) % 8);
428b843c749SSergey Zigachev 
429b843c749SSergey Zigachev 	amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_INDIRECT) |
430b843c749SSergey Zigachev 			  SDMA_PKT_INDIRECT_HEADER_VMID(vmid & 0xf));
431b843c749SSergey Zigachev 	/* base must be 32 byte aligned */
432b843c749SSergey Zigachev 	amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr) & 0xffffffe0);
433b843c749SSergey Zigachev 	amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
434b843c749SSergey Zigachev 	amdgpu_ring_write(ring, ib->length_dw);
435b843c749SSergey Zigachev 	amdgpu_ring_write(ring, 0);
436b843c749SSergey Zigachev 	amdgpu_ring_write(ring, 0);
437b843c749SSergey Zigachev 
438b843c749SSergey Zigachev }
439b843c749SSergey Zigachev 
440b843c749SSergey Zigachev /**
441b843c749SSergey Zigachev  * sdma_v3_0_ring_emit_hdp_flush - emit an hdp flush on the DMA ring
442b843c749SSergey Zigachev  *
443b843c749SSergey Zigachev  * @ring: amdgpu ring pointer
444b843c749SSergey Zigachev  *
445b843c749SSergey Zigachev  * Emit an hdp flush packet on the requested DMA ring.
446b843c749SSergey Zigachev  */
sdma_v3_0_ring_emit_hdp_flush(struct amdgpu_ring * ring)447b843c749SSergey Zigachev static void sdma_v3_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
448b843c749SSergey Zigachev {
449b843c749SSergey Zigachev 	u32 ref_and_mask = 0;
450b843c749SSergey Zigachev 
451b843c749SSergey Zigachev 	if (ring->me == 0)
452b843c749SSergey Zigachev 		ref_and_mask = REG_SET_FIELD(ref_and_mask, GPU_HDP_FLUSH_DONE, SDMA0, 1);
453b843c749SSergey Zigachev 	else
454b843c749SSergey Zigachev 		ref_and_mask = REG_SET_FIELD(ref_and_mask, GPU_HDP_FLUSH_DONE, SDMA1, 1);
455b843c749SSergey Zigachev 
456b843c749SSergey Zigachev 	amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_POLL_REGMEM) |
457b843c749SSergey Zigachev 			  SDMA_PKT_POLL_REGMEM_HEADER_HDP_FLUSH(1) |
458b843c749SSergey Zigachev 			  SDMA_PKT_POLL_REGMEM_HEADER_FUNC(3)); /* == */
459b843c749SSergey Zigachev 	amdgpu_ring_write(ring, mmGPU_HDP_FLUSH_DONE << 2);
460b843c749SSergey Zigachev 	amdgpu_ring_write(ring, mmGPU_HDP_FLUSH_REQ << 2);
461b843c749SSergey Zigachev 	amdgpu_ring_write(ring, ref_and_mask); /* reference */
462b843c749SSergey Zigachev 	amdgpu_ring_write(ring, ref_and_mask); /* mask */
463b843c749SSergey Zigachev 	amdgpu_ring_write(ring, SDMA_PKT_POLL_REGMEM_DW5_RETRY_COUNT(0xfff) |
464b843c749SSergey Zigachev 			  SDMA_PKT_POLL_REGMEM_DW5_INTERVAL(10)); /* retry count, poll interval */
465b843c749SSergey Zigachev }
466b843c749SSergey Zigachev 
467b843c749SSergey Zigachev /**
468b843c749SSergey Zigachev  * sdma_v3_0_ring_emit_fence - emit a fence on the DMA ring
469b843c749SSergey Zigachev  *
470b843c749SSergey Zigachev  * @ring: amdgpu ring pointer
471b843c749SSergey Zigachev  * @fence: amdgpu fence object
472b843c749SSergey Zigachev  *
473b843c749SSergey Zigachev  * Add a DMA fence packet to the ring to write
474b843c749SSergey Zigachev  * the fence seq number and DMA trap packet to generate
475b843c749SSergey Zigachev  * an interrupt if needed (VI).
476b843c749SSergey Zigachev  */
sdma_v3_0_ring_emit_fence(struct amdgpu_ring * ring,uint64_t addr,uint64_t seq,unsigned flags)47778973132SSergey Zigachev static void sdma_v3_0_ring_emit_fence(struct amdgpu_ring *ring, uint64_t addr, uint64_t seq,
478b843c749SSergey Zigachev 				      unsigned flags)
479b843c749SSergey Zigachev {
480b843c749SSergey Zigachev 	bool write64bit = flags & AMDGPU_FENCE_FLAG_64BIT;
481b843c749SSergey Zigachev 	/* write the fence */
482b843c749SSergey Zigachev 	amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_FENCE));
483b843c749SSergey Zigachev 	amdgpu_ring_write(ring, lower_32_bits(addr));
484b843c749SSergey Zigachev 	amdgpu_ring_write(ring, upper_32_bits(addr));
485b843c749SSergey Zigachev 	amdgpu_ring_write(ring, lower_32_bits(seq));
486b843c749SSergey Zigachev 
487b843c749SSergey Zigachev 	/* optionally write high bits as well */
488b843c749SSergey Zigachev 	if (write64bit) {
489b843c749SSergey Zigachev 		addr += 4;
490b843c749SSergey Zigachev 		amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_FENCE));
491b843c749SSergey Zigachev 		amdgpu_ring_write(ring, lower_32_bits(addr));
492b843c749SSergey Zigachev 		amdgpu_ring_write(ring, upper_32_bits(addr));
493b843c749SSergey Zigachev 		amdgpu_ring_write(ring, upper_32_bits(seq));
494b843c749SSergey Zigachev 	}
495b843c749SSergey Zigachev 
496b843c749SSergey Zigachev 	/* generate an interrupt */
497b843c749SSergey Zigachev 	amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_TRAP));
498b843c749SSergey Zigachev 	amdgpu_ring_write(ring, SDMA_PKT_TRAP_INT_CONTEXT_INT_CONTEXT(0));
499b843c749SSergey Zigachev }
500b843c749SSergey Zigachev 
501b843c749SSergey Zigachev /**
502b843c749SSergey Zigachev  * sdma_v3_0_gfx_stop - stop the gfx async dma engines
503b843c749SSergey Zigachev  *
504b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
505b843c749SSergey Zigachev  *
506b843c749SSergey Zigachev  * Stop the gfx async dma ring buffers (VI).
507b843c749SSergey Zigachev  */
sdma_v3_0_gfx_stop(struct amdgpu_device * adev)508b843c749SSergey Zigachev static void sdma_v3_0_gfx_stop(struct amdgpu_device *adev)
509b843c749SSergey Zigachev {
510b843c749SSergey Zigachev 	struct amdgpu_ring *sdma0 = &adev->sdma.instance[0].ring;
511b843c749SSergey Zigachev 	struct amdgpu_ring *sdma1 = &adev->sdma.instance[1].ring;
512b843c749SSergey Zigachev 	u32 rb_cntl, ib_cntl;
513b843c749SSergey Zigachev 	int i;
514b843c749SSergey Zigachev 
515b843c749SSergey Zigachev 	if ((adev->mman.buffer_funcs_ring == sdma0) ||
516b843c749SSergey Zigachev 	    (adev->mman.buffer_funcs_ring == sdma1))
517b843c749SSergey Zigachev 		amdgpu_ttm_set_buffer_funcs_status(adev, false);
518b843c749SSergey Zigachev 
519b843c749SSergey Zigachev 	for (i = 0; i < adev->sdma.num_instances; i++) {
520b843c749SSergey Zigachev 		rb_cntl = RREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i]);
521b843c749SSergey Zigachev 		rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL, RB_ENABLE, 0);
522b843c749SSergey Zigachev 		WREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i], rb_cntl);
523b843c749SSergey Zigachev 		ib_cntl = RREG32(mmSDMA0_GFX_IB_CNTL + sdma_offsets[i]);
524b843c749SSergey Zigachev 		ib_cntl = REG_SET_FIELD(ib_cntl, SDMA0_GFX_IB_CNTL, IB_ENABLE, 0);
525b843c749SSergey Zigachev 		WREG32(mmSDMA0_GFX_IB_CNTL + sdma_offsets[i], ib_cntl);
526b843c749SSergey Zigachev 	}
527b843c749SSergey Zigachev 	sdma0->ready = false;
528b843c749SSergey Zigachev 	sdma1->ready = false;
529b843c749SSergey Zigachev }
530b843c749SSergey Zigachev 
531b843c749SSergey Zigachev /**
532b843c749SSergey Zigachev  * sdma_v3_0_rlc_stop - stop the compute async dma engines
533b843c749SSergey Zigachev  *
534b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
535b843c749SSergey Zigachev  *
536b843c749SSergey Zigachev  * Stop the compute async dma queues (VI).
537b843c749SSergey Zigachev  */
sdma_v3_0_rlc_stop(struct amdgpu_device * adev)538b843c749SSergey Zigachev static void sdma_v3_0_rlc_stop(struct amdgpu_device *adev)
539b843c749SSergey Zigachev {
540b843c749SSergey Zigachev 	/* XXX todo */
541b843c749SSergey Zigachev }
542b843c749SSergey Zigachev 
543b843c749SSergey Zigachev /**
544b843c749SSergey Zigachev  * sdma_v3_0_ctx_switch_enable - stop the async dma engines context switch
545b843c749SSergey Zigachev  *
546b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
547b843c749SSergey Zigachev  * @enable: enable/disable the DMA MEs context switch.
548b843c749SSergey Zigachev  *
549b843c749SSergey Zigachev  * Halt or unhalt the async dma engines context switch (VI).
550b843c749SSergey Zigachev  */
sdma_v3_0_ctx_switch_enable(struct amdgpu_device * adev,bool enable)551b843c749SSergey Zigachev static void sdma_v3_0_ctx_switch_enable(struct amdgpu_device *adev, bool enable)
552b843c749SSergey Zigachev {
553b843c749SSergey Zigachev 	u32 f32_cntl, phase_quantum = 0;
554b843c749SSergey Zigachev 	int i;
555b843c749SSergey Zigachev 
556b843c749SSergey Zigachev 	if (amdgpu_sdma_phase_quantum) {
557b843c749SSergey Zigachev 		unsigned value = amdgpu_sdma_phase_quantum;
558b843c749SSergey Zigachev 		unsigned unit = 0;
559b843c749SSergey Zigachev 
560b843c749SSergey Zigachev 		while (value > (SDMA0_PHASE0_QUANTUM__VALUE_MASK >>
561b843c749SSergey Zigachev 				SDMA0_PHASE0_QUANTUM__VALUE__SHIFT)) {
562b843c749SSergey Zigachev 			value = (value + 1) >> 1;
563b843c749SSergey Zigachev 			unit++;
564b843c749SSergey Zigachev 		}
565b843c749SSergey Zigachev 		if (unit > (SDMA0_PHASE0_QUANTUM__UNIT_MASK >>
566b843c749SSergey Zigachev 			    SDMA0_PHASE0_QUANTUM__UNIT__SHIFT)) {
567b843c749SSergey Zigachev 			value = (SDMA0_PHASE0_QUANTUM__VALUE_MASK >>
568b843c749SSergey Zigachev 				 SDMA0_PHASE0_QUANTUM__VALUE__SHIFT);
569b843c749SSergey Zigachev 			unit = (SDMA0_PHASE0_QUANTUM__UNIT_MASK >>
570b843c749SSergey Zigachev 				SDMA0_PHASE0_QUANTUM__UNIT__SHIFT);
571b843c749SSergey Zigachev 			WARN_ONCE(1,
572b843c749SSergey Zigachev 			"clamping sdma_phase_quantum to %uK clock cycles\n",
573b843c749SSergey Zigachev 				  value << unit);
574b843c749SSergey Zigachev 		}
575b843c749SSergey Zigachev 		phase_quantum =
576b843c749SSergey Zigachev 			value << SDMA0_PHASE0_QUANTUM__VALUE__SHIFT |
577b843c749SSergey Zigachev 			unit  << SDMA0_PHASE0_QUANTUM__UNIT__SHIFT;
578b843c749SSergey Zigachev 	}
579b843c749SSergey Zigachev 
580b843c749SSergey Zigachev 	for (i = 0; i < adev->sdma.num_instances; i++) {
581b843c749SSergey Zigachev 		f32_cntl = RREG32(mmSDMA0_CNTL + sdma_offsets[i]);
582b843c749SSergey Zigachev 		if (enable) {
583b843c749SSergey Zigachev 			f32_cntl = REG_SET_FIELD(f32_cntl, SDMA0_CNTL,
584b843c749SSergey Zigachev 					AUTO_CTXSW_ENABLE, 1);
585b843c749SSergey Zigachev 			f32_cntl = REG_SET_FIELD(f32_cntl, SDMA0_CNTL,
586b843c749SSergey Zigachev 					ATC_L1_ENABLE, 1);
587b843c749SSergey Zigachev 			if (amdgpu_sdma_phase_quantum) {
588b843c749SSergey Zigachev 				WREG32(mmSDMA0_PHASE0_QUANTUM + sdma_offsets[i],
589b843c749SSergey Zigachev 				       phase_quantum);
590b843c749SSergey Zigachev 				WREG32(mmSDMA0_PHASE1_QUANTUM + sdma_offsets[i],
591b843c749SSergey Zigachev 				       phase_quantum);
592b843c749SSergey Zigachev 			}
593b843c749SSergey Zigachev 		} else {
594b843c749SSergey Zigachev 			f32_cntl = REG_SET_FIELD(f32_cntl, SDMA0_CNTL,
595b843c749SSergey Zigachev 					AUTO_CTXSW_ENABLE, 0);
596b843c749SSergey Zigachev 			f32_cntl = REG_SET_FIELD(f32_cntl, SDMA0_CNTL,
597b843c749SSergey Zigachev 					ATC_L1_ENABLE, 1);
598b843c749SSergey Zigachev 		}
599b843c749SSergey Zigachev 
600b843c749SSergey Zigachev 		WREG32(mmSDMA0_CNTL + sdma_offsets[i], f32_cntl);
601b843c749SSergey Zigachev 	}
602b843c749SSergey Zigachev }
603b843c749SSergey Zigachev 
604b843c749SSergey Zigachev /**
605b843c749SSergey Zigachev  * sdma_v3_0_enable - stop the async dma engines
606b843c749SSergey Zigachev  *
607b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
608b843c749SSergey Zigachev  * @enable: enable/disable the DMA MEs.
609b843c749SSergey Zigachev  *
610b843c749SSergey Zigachev  * Halt or unhalt the async dma engines (VI).
611b843c749SSergey Zigachev  */
sdma_v3_0_enable(struct amdgpu_device * adev,bool enable)612b843c749SSergey Zigachev static void sdma_v3_0_enable(struct amdgpu_device *adev, bool enable)
613b843c749SSergey Zigachev {
614b843c749SSergey Zigachev 	u32 f32_cntl;
615b843c749SSergey Zigachev 	int i;
616b843c749SSergey Zigachev 
617b843c749SSergey Zigachev 	if (!enable) {
618b843c749SSergey Zigachev 		sdma_v3_0_gfx_stop(adev);
619b843c749SSergey Zigachev 		sdma_v3_0_rlc_stop(adev);
620b843c749SSergey Zigachev 	}
621b843c749SSergey Zigachev 
622b843c749SSergey Zigachev 	for (i = 0; i < adev->sdma.num_instances; i++) {
623b843c749SSergey Zigachev 		f32_cntl = RREG32(mmSDMA0_F32_CNTL + sdma_offsets[i]);
624b843c749SSergey Zigachev 		if (enable)
625b843c749SSergey Zigachev 			f32_cntl = REG_SET_FIELD(f32_cntl, SDMA0_F32_CNTL, HALT, 0);
626b843c749SSergey Zigachev 		else
627b843c749SSergey Zigachev 			f32_cntl = REG_SET_FIELD(f32_cntl, SDMA0_F32_CNTL, HALT, 1);
628b843c749SSergey Zigachev 		WREG32(mmSDMA0_F32_CNTL + sdma_offsets[i], f32_cntl);
629b843c749SSergey Zigachev 	}
630b843c749SSergey Zigachev }
631b843c749SSergey Zigachev 
632b843c749SSergey Zigachev /**
633b843c749SSergey Zigachev  * sdma_v3_0_gfx_resume - setup and start the async dma engines
634b843c749SSergey Zigachev  *
635b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
636b843c749SSergey Zigachev  *
637b843c749SSergey Zigachev  * Set up the gfx DMA ring buffers and enable them (VI).
638b843c749SSergey Zigachev  * Returns 0 for success, error for failure.
639b843c749SSergey Zigachev  */
sdma_v3_0_gfx_resume(struct amdgpu_device * adev)640b843c749SSergey Zigachev static int sdma_v3_0_gfx_resume(struct amdgpu_device *adev)
641b843c749SSergey Zigachev {
642b843c749SSergey Zigachev 	struct amdgpu_ring *ring;
643b843c749SSergey Zigachev 	u32 rb_cntl, ib_cntl, wptr_poll_cntl;
644b843c749SSergey Zigachev 	u32 rb_bufsz;
645b843c749SSergey Zigachev 	u32 wb_offset;
646b843c749SSergey Zigachev 	u32 doorbell;
647b843c749SSergey Zigachev 	u64 wptr_gpu_addr;
648b843c749SSergey Zigachev 	int i, j, r;
649b843c749SSergey Zigachev 
650b843c749SSergey Zigachev 	for (i = 0; i < adev->sdma.num_instances; i++) {
651b843c749SSergey Zigachev 		ring = &adev->sdma.instance[i].ring;
652b843c749SSergey Zigachev 		amdgpu_ring_clear_ring(ring);
653b843c749SSergey Zigachev 		wb_offset = (ring->rptr_offs * 4);
654b843c749SSergey Zigachev 
655b843c749SSergey Zigachev 		mutex_lock(&adev->srbm_mutex);
656b843c749SSergey Zigachev 		for (j = 0; j < 16; j++) {
657b843c749SSergey Zigachev 			vi_srbm_select(adev, 0, 0, 0, j);
658b843c749SSergey Zigachev 			/* SDMA GFX */
659b843c749SSergey Zigachev 			WREG32(mmSDMA0_GFX_VIRTUAL_ADDR + sdma_offsets[i], 0);
660b843c749SSergey Zigachev 			WREG32(mmSDMA0_GFX_APE1_CNTL + sdma_offsets[i], 0);
661b843c749SSergey Zigachev 		}
662b843c749SSergey Zigachev 		vi_srbm_select(adev, 0, 0, 0, 0);
663b843c749SSergey Zigachev 		mutex_unlock(&adev->srbm_mutex);
664b843c749SSergey Zigachev 
665b843c749SSergey Zigachev 		WREG32(mmSDMA0_TILING_CONFIG + sdma_offsets[i],
666b843c749SSergey Zigachev 		       adev->gfx.config.gb_addr_config & 0x70);
667b843c749SSergey Zigachev 
668b843c749SSergey Zigachev 		WREG32(mmSDMA0_SEM_WAIT_FAIL_TIMER_CNTL + sdma_offsets[i], 0);
669b843c749SSergey Zigachev 
670b843c749SSergey Zigachev 		/* Set ring buffer size in dwords */
671b843c749SSergey Zigachev 		rb_bufsz = order_base_2(ring->ring_size / 4);
672b843c749SSergey Zigachev 		rb_cntl = RREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i]);
673b843c749SSergey Zigachev 		rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL, RB_SIZE, rb_bufsz);
674b843c749SSergey Zigachev #ifdef __BIG_ENDIAN
675b843c749SSergey Zigachev 		rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL, RB_SWAP_ENABLE, 1);
676b843c749SSergey Zigachev 		rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL,
677b843c749SSergey Zigachev 					RPTR_WRITEBACK_SWAP_ENABLE, 1);
678b843c749SSergey Zigachev #endif
679b843c749SSergey Zigachev 		WREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i], rb_cntl);
680b843c749SSergey Zigachev 
681b843c749SSergey Zigachev 		/* Initialize the ring buffer's read and write pointers */
682b843c749SSergey Zigachev 		ring->wptr = 0;
683b843c749SSergey Zigachev 		WREG32(mmSDMA0_GFX_RB_RPTR + sdma_offsets[i], 0);
684b843c749SSergey Zigachev 		sdma_v3_0_ring_set_wptr(ring);
685b843c749SSergey Zigachev 		WREG32(mmSDMA0_GFX_IB_RPTR + sdma_offsets[i], 0);
686b843c749SSergey Zigachev 		WREG32(mmSDMA0_GFX_IB_OFFSET + sdma_offsets[i], 0);
687b843c749SSergey Zigachev 
688b843c749SSergey Zigachev 		/* set the wb address whether it's enabled or not */
689b843c749SSergey Zigachev 		WREG32(mmSDMA0_GFX_RB_RPTR_ADDR_HI + sdma_offsets[i],
690b843c749SSergey Zigachev 		       upper_32_bits(adev->wb.gpu_addr + wb_offset) & 0xFFFFFFFF);
691b843c749SSergey Zigachev 		WREG32(mmSDMA0_GFX_RB_RPTR_ADDR_LO + sdma_offsets[i],
692b843c749SSergey Zigachev 		       lower_32_bits(adev->wb.gpu_addr + wb_offset) & 0xFFFFFFFC);
693b843c749SSergey Zigachev 
694b843c749SSergey Zigachev 		rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL, RPTR_WRITEBACK_ENABLE, 1);
695b843c749SSergey Zigachev 
696b843c749SSergey Zigachev 		WREG32(mmSDMA0_GFX_RB_BASE + sdma_offsets[i], ring->gpu_addr >> 8);
697b843c749SSergey Zigachev 		WREG32(mmSDMA0_GFX_RB_BASE_HI + sdma_offsets[i], ring->gpu_addr >> 40);
698b843c749SSergey Zigachev 
699b843c749SSergey Zigachev 		doorbell = RREG32(mmSDMA0_GFX_DOORBELL + sdma_offsets[i]);
700b843c749SSergey Zigachev 
701b843c749SSergey Zigachev 		if (ring->use_doorbell) {
702b843c749SSergey Zigachev 			doorbell = REG_SET_FIELD(doorbell, SDMA0_GFX_DOORBELL,
703b843c749SSergey Zigachev 						 OFFSET, ring->doorbell_index);
704b843c749SSergey Zigachev 			doorbell = REG_SET_FIELD(doorbell, SDMA0_GFX_DOORBELL, ENABLE, 1);
705b843c749SSergey Zigachev 		} else {
706b843c749SSergey Zigachev 			doorbell = REG_SET_FIELD(doorbell, SDMA0_GFX_DOORBELL, ENABLE, 0);
707b843c749SSergey Zigachev 		}
708b843c749SSergey Zigachev 		WREG32(mmSDMA0_GFX_DOORBELL + sdma_offsets[i], doorbell);
709b843c749SSergey Zigachev 
710b843c749SSergey Zigachev 		/* setup the wptr shadow polling */
711b843c749SSergey Zigachev 		wptr_gpu_addr = adev->wb.gpu_addr + (ring->wptr_offs * 4);
712b843c749SSergey Zigachev 
713b843c749SSergey Zigachev 		WREG32(mmSDMA0_GFX_RB_WPTR_POLL_ADDR_LO + sdma_offsets[i],
714b843c749SSergey Zigachev 		       lower_32_bits(wptr_gpu_addr));
715b843c749SSergey Zigachev 		WREG32(mmSDMA0_GFX_RB_WPTR_POLL_ADDR_HI + sdma_offsets[i],
716b843c749SSergey Zigachev 		       upper_32_bits(wptr_gpu_addr));
717b843c749SSergey Zigachev 		wptr_poll_cntl = RREG32(mmSDMA0_GFX_RB_WPTR_POLL_CNTL + sdma_offsets[i]);
718b843c749SSergey Zigachev 		if (ring->use_pollmem) {
719b843c749SSergey Zigachev 			/*wptr polling is not enogh fast, directly clean the wptr register */
720b843c749SSergey Zigachev 			WREG32(mmSDMA0_GFX_RB_WPTR + sdma_offsets[i], 0);
721b843c749SSergey Zigachev 			wptr_poll_cntl = REG_SET_FIELD(wptr_poll_cntl,
722b843c749SSergey Zigachev 						       SDMA0_GFX_RB_WPTR_POLL_CNTL,
723b843c749SSergey Zigachev 						       ENABLE, 1);
724b843c749SSergey Zigachev 		} else {
725b843c749SSergey Zigachev 			wptr_poll_cntl = REG_SET_FIELD(wptr_poll_cntl,
726b843c749SSergey Zigachev 						       SDMA0_GFX_RB_WPTR_POLL_CNTL,
727b843c749SSergey Zigachev 						       ENABLE, 0);
728b843c749SSergey Zigachev 		}
729b843c749SSergey Zigachev 		WREG32(mmSDMA0_GFX_RB_WPTR_POLL_CNTL + sdma_offsets[i], wptr_poll_cntl);
730b843c749SSergey Zigachev 
731b843c749SSergey Zigachev 		/* enable DMA RB */
732b843c749SSergey Zigachev 		rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL, RB_ENABLE, 1);
733b843c749SSergey Zigachev 		WREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i], rb_cntl);
734b843c749SSergey Zigachev 
735b843c749SSergey Zigachev 		ib_cntl = RREG32(mmSDMA0_GFX_IB_CNTL + sdma_offsets[i]);
736b843c749SSergey Zigachev 		ib_cntl = REG_SET_FIELD(ib_cntl, SDMA0_GFX_IB_CNTL, IB_ENABLE, 1);
737b843c749SSergey Zigachev #ifdef __BIG_ENDIAN
738b843c749SSergey Zigachev 		ib_cntl = REG_SET_FIELD(ib_cntl, SDMA0_GFX_IB_CNTL, IB_SWAP_ENABLE, 1);
739b843c749SSergey Zigachev #endif
740b843c749SSergey Zigachev 		/* enable DMA IBs */
741b843c749SSergey Zigachev 		WREG32(mmSDMA0_GFX_IB_CNTL + sdma_offsets[i], ib_cntl);
742b843c749SSergey Zigachev 
743b843c749SSergey Zigachev 		ring->ready = true;
744b843c749SSergey Zigachev 	}
745b843c749SSergey Zigachev 
746b843c749SSergey Zigachev 	/* unhalt the MEs */
747b843c749SSergey Zigachev 	sdma_v3_0_enable(adev, true);
748b843c749SSergey Zigachev 	/* enable sdma ring preemption */
749b843c749SSergey Zigachev 	sdma_v3_0_ctx_switch_enable(adev, true);
750b843c749SSergey Zigachev 
751b843c749SSergey Zigachev 	for (i = 0; i < adev->sdma.num_instances; i++) {
752b843c749SSergey Zigachev 		ring = &adev->sdma.instance[i].ring;
753b843c749SSergey Zigachev 		r = amdgpu_ring_test_ring(ring);
754b843c749SSergey Zigachev 		if (r) {
755b843c749SSergey Zigachev 			ring->ready = false;
756b843c749SSergey Zigachev 			return r;
757b843c749SSergey Zigachev 		}
758b843c749SSergey Zigachev 
759b843c749SSergey Zigachev 		if (adev->mman.buffer_funcs_ring == ring)
760b843c749SSergey Zigachev 			amdgpu_ttm_set_buffer_funcs_status(adev, true);
761b843c749SSergey Zigachev 	}
762b843c749SSergey Zigachev 
763b843c749SSergey Zigachev 	return 0;
764b843c749SSergey Zigachev }
765b843c749SSergey Zigachev 
766b843c749SSergey Zigachev /**
767b843c749SSergey Zigachev  * sdma_v3_0_rlc_resume - setup and start the async dma engines
768b843c749SSergey Zigachev  *
769b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
770b843c749SSergey Zigachev  *
771b843c749SSergey Zigachev  * Set up the compute DMA queues and enable them (VI).
772b843c749SSergey Zigachev  * Returns 0 for success, error for failure.
773b843c749SSergey Zigachev  */
sdma_v3_0_rlc_resume(struct amdgpu_device * adev)774b843c749SSergey Zigachev static int sdma_v3_0_rlc_resume(struct amdgpu_device *adev)
775b843c749SSergey Zigachev {
776b843c749SSergey Zigachev 	/* XXX todo */
777b843c749SSergey Zigachev 	return 0;
778b843c749SSergey Zigachev }
779b843c749SSergey Zigachev 
780b843c749SSergey Zigachev /**
781b843c749SSergey Zigachev  * sdma_v3_0_load_microcode - load the sDMA ME ucode
782b843c749SSergey Zigachev  *
783b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
784b843c749SSergey Zigachev  *
785b843c749SSergey Zigachev  * Loads the sDMA0/1 ucode.
786b843c749SSergey Zigachev  * Returns 0 for success, -EINVAL if the ucode is not available.
787b843c749SSergey Zigachev  */
sdma_v3_0_load_microcode(struct amdgpu_device * adev)788b843c749SSergey Zigachev static int sdma_v3_0_load_microcode(struct amdgpu_device *adev)
789b843c749SSergey Zigachev {
790b843c749SSergey Zigachev 	const struct sdma_firmware_header_v1_0 *hdr;
791b843c749SSergey Zigachev 	const __le32 *fw_data;
792b843c749SSergey Zigachev 	u32 fw_size;
793b843c749SSergey Zigachev 	int i, j;
794b843c749SSergey Zigachev 
795b843c749SSergey Zigachev 	/* halt the MEs */
796b843c749SSergey Zigachev 	sdma_v3_0_enable(adev, false);
797b843c749SSergey Zigachev 
798b843c749SSergey Zigachev 	for (i = 0; i < adev->sdma.num_instances; i++) {
799b843c749SSergey Zigachev 		if (!adev->sdma.instance[i].fw)
800b843c749SSergey Zigachev 			return -EINVAL;
801b843c749SSergey Zigachev 		hdr = (const struct sdma_firmware_header_v1_0 *)adev->sdma.instance[i].fw->data;
802b843c749SSergey Zigachev 		amdgpu_ucode_print_sdma_hdr(&hdr->header);
803b843c749SSergey Zigachev 		fw_size = le32_to_cpu(hdr->header.ucode_size_bytes) / 4;
804b843c749SSergey Zigachev 		fw_data = (const __le32 *)
805b843c749SSergey Zigachev 			(adev->sdma.instance[i].fw->data +
806b843c749SSergey Zigachev 				le32_to_cpu(hdr->header.ucode_array_offset_bytes));
807b843c749SSergey Zigachev 		WREG32(mmSDMA0_UCODE_ADDR + sdma_offsets[i], 0);
808b843c749SSergey Zigachev 		for (j = 0; j < fw_size; j++)
809b843c749SSergey Zigachev 			WREG32(mmSDMA0_UCODE_DATA + sdma_offsets[i], le32_to_cpup(fw_data++));
810b843c749SSergey Zigachev 		WREG32(mmSDMA0_UCODE_ADDR + sdma_offsets[i], adev->sdma.instance[i].fw_version);
811b843c749SSergey Zigachev 	}
812b843c749SSergey Zigachev 
813b843c749SSergey Zigachev 	return 0;
814b843c749SSergey Zigachev }
815b843c749SSergey Zigachev 
816b843c749SSergey Zigachev /**
817b843c749SSergey Zigachev  * sdma_v3_0_start - setup and start the async dma engines
818b843c749SSergey Zigachev  *
819b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
820b843c749SSergey Zigachev  *
821b843c749SSergey Zigachev  * Set up the DMA engines and enable them (VI).
822b843c749SSergey Zigachev  * Returns 0 for success, error for failure.
823b843c749SSergey Zigachev  */
sdma_v3_0_start(struct amdgpu_device * adev)824b843c749SSergey Zigachev static int sdma_v3_0_start(struct amdgpu_device *adev)
825b843c749SSergey Zigachev {
826b843c749SSergey Zigachev 	int r;
827b843c749SSergey Zigachev 
828b843c749SSergey Zigachev 	if (adev->firmware.load_type == AMDGPU_FW_LOAD_DIRECT) {
829b843c749SSergey Zigachev 		r = sdma_v3_0_load_microcode(adev);
830b843c749SSergey Zigachev 		if (r)
831b843c749SSergey Zigachev 			return r;
832b843c749SSergey Zigachev 	}
833b843c749SSergey Zigachev 
834b843c749SSergey Zigachev 	/* disable sdma engine before programing it */
835b843c749SSergey Zigachev 	sdma_v3_0_ctx_switch_enable(adev, false);
836b843c749SSergey Zigachev 	sdma_v3_0_enable(adev, false);
837b843c749SSergey Zigachev 
838b843c749SSergey Zigachev 	/* start the gfx rings and rlc compute queues */
839b843c749SSergey Zigachev 	r = sdma_v3_0_gfx_resume(adev);
840b843c749SSergey Zigachev 	if (r)
841b843c749SSergey Zigachev 		return r;
842b843c749SSergey Zigachev 	r = sdma_v3_0_rlc_resume(adev);
843b843c749SSergey Zigachev 	if (r)
844b843c749SSergey Zigachev 		return r;
845b843c749SSergey Zigachev 
846b843c749SSergey Zigachev 	return 0;
847b843c749SSergey Zigachev }
848b843c749SSergey Zigachev 
849b843c749SSergey Zigachev /**
850b843c749SSergey Zigachev  * sdma_v3_0_ring_test_ring - simple async dma engine test
851b843c749SSergey Zigachev  *
852b843c749SSergey Zigachev  * @ring: amdgpu_ring structure holding ring information
853b843c749SSergey Zigachev  *
854b843c749SSergey Zigachev  * Test the DMA engine by writing using it to write an
855b843c749SSergey Zigachev  * value to memory. (VI).
856b843c749SSergey Zigachev  * Returns 0 for success, error for failure.
857b843c749SSergey Zigachev  */
sdma_v3_0_ring_test_ring(struct amdgpu_ring * ring)858b843c749SSergey Zigachev static int sdma_v3_0_ring_test_ring(struct amdgpu_ring *ring)
859b843c749SSergey Zigachev {
860b843c749SSergey Zigachev 	struct amdgpu_device *adev = ring->adev;
861b843c749SSergey Zigachev 	unsigned i;
862b843c749SSergey Zigachev 	unsigned index;
863b843c749SSergey Zigachev 	int r;
864b843c749SSergey Zigachev 	u32 tmp;
865b843c749SSergey Zigachev 	u64 gpu_addr;
866b843c749SSergey Zigachev 
867b843c749SSergey Zigachev 	r = amdgpu_device_wb_get(adev, &index);
868b843c749SSergey Zigachev 	if (r) {
869b843c749SSergey Zigachev 		dev_err(adev->dev, "(%d) failed to allocate wb slot\n", r);
870b843c749SSergey Zigachev 		return r;
871b843c749SSergey Zigachev 	}
872b843c749SSergey Zigachev 
873b843c749SSergey Zigachev 	gpu_addr = adev->wb.gpu_addr + (index * 4);
874b843c749SSergey Zigachev 	tmp = 0xCAFEDEAD;
875b843c749SSergey Zigachev 	adev->wb.wb[index] = cpu_to_le32(tmp);
876b843c749SSergey Zigachev 
877b843c749SSergey Zigachev 	r = amdgpu_ring_alloc(ring, 5);
878b843c749SSergey Zigachev 	if (r) {
879b843c749SSergey Zigachev 		DRM_ERROR("amdgpu: dma failed to lock ring %d (%d).\n", ring->idx, r);
880b843c749SSergey Zigachev 		amdgpu_device_wb_free(adev, index);
881b843c749SSergey Zigachev 		return r;
882b843c749SSergey Zigachev 	}
883b843c749SSergey Zigachev 
884b843c749SSergey Zigachev 	amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_WRITE) |
885b843c749SSergey Zigachev 			  SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR));
886b843c749SSergey Zigachev 	amdgpu_ring_write(ring, lower_32_bits(gpu_addr));
887b843c749SSergey Zigachev 	amdgpu_ring_write(ring, upper_32_bits(gpu_addr));
888b843c749SSergey Zigachev 	amdgpu_ring_write(ring, SDMA_PKT_WRITE_UNTILED_DW_3_COUNT(1));
889b843c749SSergey Zigachev 	amdgpu_ring_write(ring, 0xDEADBEEF);
890b843c749SSergey Zigachev 	amdgpu_ring_commit(ring);
891b843c749SSergey Zigachev 
892b843c749SSergey Zigachev 	for (i = 0; i < adev->usec_timeout; i++) {
893b843c749SSergey Zigachev 		tmp = le32_to_cpu(adev->wb.wb[index]);
894b843c749SSergey Zigachev 		if (tmp == 0xDEADBEEF)
895b843c749SSergey Zigachev 			break;
896b843c749SSergey Zigachev 		DRM_UDELAY(1);
897b843c749SSergey Zigachev 	}
898b843c749SSergey Zigachev 
899b843c749SSergey Zigachev 	if (i < adev->usec_timeout) {
900b843c749SSergey Zigachev 		DRM_DEBUG("ring test on %d succeeded in %d usecs\n", ring->idx, i);
901b843c749SSergey Zigachev 	} else {
902b843c749SSergey Zigachev 		DRM_ERROR("amdgpu: ring %d test failed (0x%08X)\n",
903b843c749SSergey Zigachev 			  ring->idx, tmp);
904b843c749SSergey Zigachev 		r = -EINVAL;
905b843c749SSergey Zigachev 	}
906b843c749SSergey Zigachev 	amdgpu_device_wb_free(adev, index);
907b843c749SSergey Zigachev 
908b843c749SSergey Zigachev 	return r;
909b843c749SSergey Zigachev }
910b843c749SSergey Zigachev 
911b843c749SSergey Zigachev /**
912b843c749SSergey Zigachev  * sdma_v3_0_ring_test_ib - test an IB on the DMA engine
913b843c749SSergey Zigachev  *
914b843c749SSergey Zigachev  * @ring: amdgpu_ring structure holding ring information
915b843c749SSergey Zigachev  *
916b843c749SSergey Zigachev  * Test a simple IB in the DMA ring (VI).
917b843c749SSergey Zigachev  * Returns 0 on success, error on failure.
918b843c749SSergey Zigachev  */
sdma_v3_0_ring_test_ib(struct amdgpu_ring * ring,long timeout)919b843c749SSergey Zigachev static int sdma_v3_0_ring_test_ib(struct amdgpu_ring *ring, long timeout)
920b843c749SSergey Zigachev {
921b843c749SSergey Zigachev 	struct amdgpu_device *adev = ring->adev;
922b843c749SSergey Zigachev 	struct amdgpu_ib ib;
923b843c749SSergey Zigachev 	struct dma_fence *f = NULL;
924b843c749SSergey Zigachev 	unsigned index;
925b843c749SSergey Zigachev 	u32 tmp = 0;
926b843c749SSergey Zigachev 	u64 gpu_addr;
927b843c749SSergey Zigachev 	long r;
928b843c749SSergey Zigachev 
929b843c749SSergey Zigachev 	r = amdgpu_device_wb_get(adev, &index);
930b843c749SSergey Zigachev 	if (r) {
931b843c749SSergey Zigachev 		dev_err(adev->dev, "(%ld) failed to allocate wb slot\n", r);
932b843c749SSergey Zigachev 		return r;
933b843c749SSergey Zigachev 	}
934b843c749SSergey Zigachev 
935b843c749SSergey Zigachev 	gpu_addr = adev->wb.gpu_addr + (index * 4);
936b843c749SSergey Zigachev 	tmp = 0xCAFEDEAD;
937b843c749SSergey Zigachev 	adev->wb.wb[index] = cpu_to_le32(tmp);
938b843c749SSergey Zigachev 	memset(&ib, 0, sizeof(ib));
939b843c749SSergey Zigachev 	r = amdgpu_ib_get(adev, NULL, 256, &ib);
940b843c749SSergey Zigachev 	if (r) {
941b843c749SSergey Zigachev 		DRM_ERROR("amdgpu: failed to get ib (%ld).\n", r);
942b843c749SSergey Zigachev 		goto err0;
943b843c749SSergey Zigachev 	}
944b843c749SSergey Zigachev 
945b843c749SSergey Zigachev 	ib.ptr[0] = SDMA_PKT_HEADER_OP(SDMA_OP_WRITE) |
946b843c749SSergey Zigachev 		SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR);
947b843c749SSergey Zigachev 	ib.ptr[1] = lower_32_bits(gpu_addr);
948b843c749SSergey Zigachev 	ib.ptr[2] = upper_32_bits(gpu_addr);
949b843c749SSergey Zigachev 	ib.ptr[3] = SDMA_PKT_WRITE_UNTILED_DW_3_COUNT(1);
950b843c749SSergey Zigachev 	ib.ptr[4] = 0xDEADBEEF;
951b843c749SSergey Zigachev 	ib.ptr[5] = SDMA_PKT_NOP_HEADER_OP(SDMA_OP_NOP);
952b843c749SSergey Zigachev 	ib.ptr[6] = SDMA_PKT_NOP_HEADER_OP(SDMA_OP_NOP);
953b843c749SSergey Zigachev 	ib.ptr[7] = SDMA_PKT_NOP_HEADER_OP(SDMA_OP_NOP);
954b843c749SSergey Zigachev 	ib.length_dw = 8;
955b843c749SSergey Zigachev 
956b843c749SSergey Zigachev 	r = amdgpu_ib_schedule(ring, 1, &ib, NULL, &f);
957b843c749SSergey Zigachev 	if (r)
958b843c749SSergey Zigachev 		goto err1;
959b843c749SSergey Zigachev 
960b843c749SSergey Zigachev 	r = dma_fence_wait_timeout(f, false, timeout);
961b843c749SSergey Zigachev 	if (r == 0) {
962b843c749SSergey Zigachev 		DRM_ERROR("amdgpu: IB test timed out\n");
963b843c749SSergey Zigachev 		r = -ETIMEDOUT;
964b843c749SSergey Zigachev 		goto err1;
965b843c749SSergey Zigachev 	} else if (r < 0) {
966b843c749SSergey Zigachev 		DRM_ERROR("amdgpu: fence wait failed (%ld).\n", r);
967b843c749SSergey Zigachev 		goto err1;
968b843c749SSergey Zigachev 	}
969b843c749SSergey Zigachev 	tmp = le32_to_cpu(adev->wb.wb[index]);
970b843c749SSergey Zigachev 	if (tmp == 0xDEADBEEF) {
971b843c749SSergey Zigachev 		DRM_DEBUG("ib test on ring %d succeeded\n", ring->idx);
972b843c749SSergey Zigachev 		r = 0;
973b843c749SSergey Zigachev 	} else {
974b843c749SSergey Zigachev 		DRM_ERROR("amdgpu: ib test failed (0x%08X)\n", tmp);
975b843c749SSergey Zigachev 		r = -EINVAL;
976b843c749SSergey Zigachev 	}
977b843c749SSergey Zigachev err1:
978b843c749SSergey Zigachev 	amdgpu_ib_free(adev, &ib, NULL);
979b843c749SSergey Zigachev 	dma_fence_put(f);
980b843c749SSergey Zigachev err0:
981b843c749SSergey Zigachev 	amdgpu_device_wb_free(adev, index);
982b843c749SSergey Zigachev 	return r;
983b843c749SSergey Zigachev }
984b843c749SSergey Zigachev 
985b843c749SSergey Zigachev /**
986b843c749SSergey Zigachev  * sdma_v3_0_vm_copy_pte - update PTEs by copying them from the GART
987b843c749SSergey Zigachev  *
988b843c749SSergey Zigachev  * @ib: indirect buffer to fill with commands
989b843c749SSergey Zigachev  * @pe: addr of the page entry
990b843c749SSergey Zigachev  * @src: src addr to copy from
991b843c749SSergey Zigachev  * @count: number of page entries to update
992b843c749SSergey Zigachev  *
993b843c749SSergey Zigachev  * Update PTEs by copying them from the GART using sDMA (CIK).
994b843c749SSergey Zigachev  */
sdma_v3_0_vm_copy_pte(struct amdgpu_ib * ib,uint64_t pe,uint64_t src,unsigned count)995b843c749SSergey Zigachev static void sdma_v3_0_vm_copy_pte(struct amdgpu_ib *ib,
996b843c749SSergey Zigachev 				  uint64_t pe, uint64_t src,
997b843c749SSergey Zigachev 				  unsigned count)
998b843c749SSergey Zigachev {
999b843c749SSergey Zigachev 	unsigned bytes = count * 8;
1000b843c749SSergey Zigachev 
1001b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
1002b843c749SSergey Zigachev 		SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR);
1003b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = bytes;
1004b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
1005b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = lower_32_bits(src);
1006b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = upper_32_bits(src);
1007b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = lower_32_bits(pe);
1008b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
1009b843c749SSergey Zigachev }
1010b843c749SSergey Zigachev 
1011b843c749SSergey Zigachev /**
1012b843c749SSergey Zigachev  * sdma_v3_0_vm_write_pte - update PTEs by writing them manually
1013b843c749SSergey Zigachev  *
1014b843c749SSergey Zigachev  * @ib: indirect buffer to fill with commands
1015b843c749SSergey Zigachev  * @pe: addr of the page entry
1016b843c749SSergey Zigachev  * @value: dst addr to write into pe
1017b843c749SSergey Zigachev  * @count: number of page entries to update
1018b843c749SSergey Zigachev  * @incr: increase next addr by incr bytes
1019b843c749SSergey Zigachev  *
1020b843c749SSergey Zigachev  * Update PTEs by writing them manually using sDMA (CIK).
1021b843c749SSergey Zigachev  */
sdma_v3_0_vm_write_pte(struct amdgpu_ib * ib,uint64_t pe,uint64_t value,unsigned count,uint32_t incr)1022b843c749SSergey Zigachev static void sdma_v3_0_vm_write_pte(struct amdgpu_ib *ib, uint64_t pe,
1023b843c749SSergey Zigachev 				   uint64_t value, unsigned count,
1024b843c749SSergey Zigachev 				   uint32_t incr)
1025b843c749SSergey Zigachev {
1026b843c749SSergey Zigachev 	unsigned ndw = count * 2;
1027b843c749SSergey Zigachev 
1028b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_WRITE) |
1029b843c749SSergey Zigachev 		SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR);
1030b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = lower_32_bits(pe);
1031b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
1032b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = ndw;
1033b843c749SSergey Zigachev 	for (; ndw > 0; ndw -= 2) {
1034b843c749SSergey Zigachev 		ib->ptr[ib->length_dw++] = lower_32_bits(value);
1035b843c749SSergey Zigachev 		ib->ptr[ib->length_dw++] = upper_32_bits(value);
1036b843c749SSergey Zigachev 		value += incr;
1037b843c749SSergey Zigachev 	}
1038b843c749SSergey Zigachev }
1039b843c749SSergey Zigachev 
1040b843c749SSergey Zigachev /**
1041b843c749SSergey Zigachev  * sdma_v3_0_vm_set_pte_pde - update the page tables using sDMA
1042b843c749SSergey Zigachev  *
1043b843c749SSergey Zigachev  * @ib: indirect buffer to fill with commands
1044b843c749SSergey Zigachev  * @pe: addr of the page entry
1045b843c749SSergey Zigachev  * @addr: dst addr to write into pe
1046b843c749SSergey Zigachev  * @count: number of page entries to update
1047b843c749SSergey Zigachev  * @incr: increase next addr by incr bytes
1048b843c749SSergey Zigachev  * @flags: access flags
1049b843c749SSergey Zigachev  *
1050b843c749SSergey Zigachev  * Update the page tables using sDMA (CIK).
1051b843c749SSergey Zigachev  */
sdma_v3_0_vm_set_pte_pde(struct amdgpu_ib * ib,uint64_t pe,uint64_t addr,unsigned count,uint32_t incr,uint64_t flags)1052b843c749SSergey Zigachev static void sdma_v3_0_vm_set_pte_pde(struct amdgpu_ib *ib, uint64_t pe,
1053b843c749SSergey Zigachev 				     uint64_t addr, unsigned count,
1054b843c749SSergey Zigachev 				     uint32_t incr, uint64_t flags)
1055b843c749SSergey Zigachev {
1056b843c749SSergey Zigachev 	/* for physically contiguous pages (vram) */
1057b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_GEN_PTEPDE);
1058b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = lower_32_bits(pe); /* dst addr */
1059b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
1060b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = lower_32_bits(flags); /* mask */
1061b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = upper_32_bits(flags);
1062b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = lower_32_bits(addr); /* value */
1063b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = upper_32_bits(addr);
1064b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = incr; /* increment size */
1065b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = 0;
1066b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = count; /* number of entries */
1067b843c749SSergey Zigachev }
1068b843c749SSergey Zigachev 
1069b843c749SSergey Zigachev /**
1070b843c749SSergey Zigachev  * sdma_v3_0_ring_pad_ib - pad the IB to the required number of dw
1071b843c749SSergey Zigachev  *
1072b843c749SSergey Zigachev  * @ib: indirect buffer to fill with padding
1073b843c749SSergey Zigachev  *
1074b843c749SSergey Zigachev  */
sdma_v3_0_ring_pad_ib(struct amdgpu_ring * ring,struct amdgpu_ib * ib)1075b843c749SSergey Zigachev static void sdma_v3_0_ring_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib)
1076b843c749SSergey Zigachev {
1077b843c749SSergey Zigachev 	struct amdgpu_sdma_instance *sdma = amdgpu_get_sdma_instance(ring);
1078b843c749SSergey Zigachev 	u32 pad_count;
1079b843c749SSergey Zigachev 	int i;
1080b843c749SSergey Zigachev 
1081b843c749SSergey Zigachev 	pad_count = (8 - (ib->length_dw & 0x7)) % 8;
1082b843c749SSergey Zigachev 	for (i = 0; i < pad_count; i++)
1083b843c749SSergey Zigachev 		if (sdma && sdma->burst_nop && (i == 0))
1084b843c749SSergey Zigachev 			ib->ptr[ib->length_dw++] =
1085b843c749SSergey Zigachev 				SDMA_PKT_HEADER_OP(SDMA_OP_NOP) |
1086b843c749SSergey Zigachev 				SDMA_PKT_NOP_HEADER_COUNT(pad_count - 1);
1087b843c749SSergey Zigachev 		else
1088b843c749SSergey Zigachev 			ib->ptr[ib->length_dw++] =
1089b843c749SSergey Zigachev 				SDMA_PKT_HEADER_OP(SDMA_OP_NOP);
1090b843c749SSergey Zigachev }
1091b843c749SSergey Zigachev 
1092b843c749SSergey Zigachev /**
1093b843c749SSergey Zigachev  * sdma_v3_0_ring_emit_pipeline_sync - sync the pipeline
1094b843c749SSergey Zigachev  *
1095b843c749SSergey Zigachev  * @ring: amdgpu_ring pointer
1096b843c749SSergey Zigachev  *
1097b843c749SSergey Zigachev  * Make sure all previous operations are completed (CIK).
1098b843c749SSergey Zigachev  */
sdma_v3_0_ring_emit_pipeline_sync(struct amdgpu_ring * ring)1099b843c749SSergey Zigachev static void sdma_v3_0_ring_emit_pipeline_sync(struct amdgpu_ring *ring)
1100b843c749SSergey Zigachev {
1101b843c749SSergey Zigachev 	uint32_t seq = ring->fence_drv.sync_seq;
1102b843c749SSergey Zigachev 	uint64_t addr = ring->fence_drv.gpu_addr;
1103b843c749SSergey Zigachev 
1104b843c749SSergey Zigachev 	/* wait for idle */
1105b843c749SSergey Zigachev 	amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_POLL_REGMEM) |
1106b843c749SSergey Zigachev 			  SDMA_PKT_POLL_REGMEM_HEADER_HDP_FLUSH(0) |
1107b843c749SSergey Zigachev 			  SDMA_PKT_POLL_REGMEM_HEADER_FUNC(3) | /* equal */
1108b843c749SSergey Zigachev 			  SDMA_PKT_POLL_REGMEM_HEADER_MEM_POLL(1));
1109b843c749SSergey Zigachev 	amdgpu_ring_write(ring, addr & 0xfffffffc);
1110b843c749SSergey Zigachev 	amdgpu_ring_write(ring, upper_32_bits(addr) & 0xffffffff);
1111b843c749SSergey Zigachev 	amdgpu_ring_write(ring, seq); /* reference */
1112b843c749SSergey Zigachev 	amdgpu_ring_write(ring, 0xffffffff); /* mask */
1113b843c749SSergey Zigachev 	amdgpu_ring_write(ring, SDMA_PKT_POLL_REGMEM_DW5_RETRY_COUNT(0xfff) |
1114b843c749SSergey Zigachev 			  SDMA_PKT_POLL_REGMEM_DW5_INTERVAL(4)); /* retry count, poll interval */
1115b843c749SSergey Zigachev }
1116b843c749SSergey Zigachev 
1117b843c749SSergey Zigachev /**
1118b843c749SSergey Zigachev  * sdma_v3_0_ring_emit_vm_flush - cik vm flush using sDMA
1119b843c749SSergey Zigachev  *
1120b843c749SSergey Zigachev  * @ring: amdgpu_ring pointer
1121b843c749SSergey Zigachev  * @vm: amdgpu_vm pointer
1122b843c749SSergey Zigachev  *
1123b843c749SSergey Zigachev  * Update the page table base and flush the VM TLB
1124b843c749SSergey Zigachev  * using sDMA (VI).
1125b843c749SSergey Zigachev  */
sdma_v3_0_ring_emit_vm_flush(struct amdgpu_ring * ring,unsigned vmid,uint64_t pd_addr)1126b843c749SSergey Zigachev static void sdma_v3_0_ring_emit_vm_flush(struct amdgpu_ring *ring,
1127b843c749SSergey Zigachev 					 unsigned vmid, uint64_t pd_addr)
1128b843c749SSergey Zigachev {
1129b843c749SSergey Zigachev 	amdgpu_gmc_emit_flush_gpu_tlb(ring, vmid, pd_addr);
1130b843c749SSergey Zigachev 
1131b843c749SSergey Zigachev 	/* wait for flush */
1132b843c749SSergey Zigachev 	amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_POLL_REGMEM) |
1133b843c749SSergey Zigachev 			  SDMA_PKT_POLL_REGMEM_HEADER_HDP_FLUSH(0) |
1134b843c749SSergey Zigachev 			  SDMA_PKT_POLL_REGMEM_HEADER_FUNC(0)); /* always */
1135b843c749SSergey Zigachev 	amdgpu_ring_write(ring, mmVM_INVALIDATE_REQUEST << 2);
1136b843c749SSergey Zigachev 	amdgpu_ring_write(ring, 0);
1137b843c749SSergey Zigachev 	amdgpu_ring_write(ring, 0); /* reference */
1138b843c749SSergey Zigachev 	amdgpu_ring_write(ring, 0); /* mask */
1139b843c749SSergey Zigachev 	amdgpu_ring_write(ring, SDMA_PKT_POLL_REGMEM_DW5_RETRY_COUNT(0xfff) |
1140b843c749SSergey Zigachev 			  SDMA_PKT_POLL_REGMEM_DW5_INTERVAL(10)); /* retry count, poll interval */
1141b843c749SSergey Zigachev }
1142b843c749SSergey Zigachev 
sdma_v3_0_ring_emit_wreg(struct amdgpu_ring * ring,uint32_t reg,uint32_t val)1143b843c749SSergey Zigachev static void sdma_v3_0_ring_emit_wreg(struct amdgpu_ring *ring,
1144b843c749SSergey Zigachev 				     uint32_t reg, uint32_t val)
1145b843c749SSergey Zigachev {
1146b843c749SSergey Zigachev 	amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_SRBM_WRITE) |
1147b843c749SSergey Zigachev 			  SDMA_PKT_SRBM_WRITE_HEADER_BYTE_EN(0xf));
1148b843c749SSergey Zigachev 	amdgpu_ring_write(ring, reg);
1149b843c749SSergey Zigachev 	amdgpu_ring_write(ring, val);
1150b843c749SSergey Zigachev }
1151b843c749SSergey Zigachev 
sdma_v3_0_early_init(void * handle)1152b843c749SSergey Zigachev static int sdma_v3_0_early_init(void *handle)
1153b843c749SSergey Zigachev {
1154b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1155b843c749SSergey Zigachev 
1156b843c749SSergey Zigachev 	switch (adev->asic_type) {
1157b843c749SSergey Zigachev 	case CHIP_STONEY:
1158b843c749SSergey Zigachev 		adev->sdma.num_instances = 1;
1159b843c749SSergey Zigachev 		break;
1160b843c749SSergey Zigachev 	default:
1161b843c749SSergey Zigachev 		adev->sdma.num_instances = SDMA_MAX_INSTANCE;
1162b843c749SSergey Zigachev 		break;
1163b843c749SSergey Zigachev 	}
1164b843c749SSergey Zigachev 
1165b843c749SSergey Zigachev 	sdma_v3_0_set_ring_funcs(adev);
1166b843c749SSergey Zigachev 	sdma_v3_0_set_buffer_funcs(adev);
1167b843c749SSergey Zigachev 	sdma_v3_0_set_vm_pte_funcs(adev);
1168b843c749SSergey Zigachev 	sdma_v3_0_set_irq_funcs(adev);
1169b843c749SSergey Zigachev 
1170b843c749SSergey Zigachev 	return 0;
1171b843c749SSergey Zigachev }
1172b843c749SSergey Zigachev 
sdma_v3_0_sw_init(void * handle)1173b843c749SSergey Zigachev static int sdma_v3_0_sw_init(void *handle)
1174b843c749SSergey Zigachev {
1175b843c749SSergey Zigachev 	struct amdgpu_ring *ring;
1176b843c749SSergey Zigachev 	int r, i;
1177b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1178b843c749SSergey Zigachev 
1179b843c749SSergey Zigachev 	/* SDMA trap event */
1180b843c749SSergey Zigachev 	r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_LEGACY, VISLANDS30_IV_SRCID_SDMA_TRAP,
1181b843c749SSergey Zigachev 			      &adev->sdma.trap_irq);
1182b843c749SSergey Zigachev 	if (r)
1183b843c749SSergey Zigachev 		return r;
1184b843c749SSergey Zigachev 
1185b843c749SSergey Zigachev 	/* SDMA Privileged inst */
1186b843c749SSergey Zigachev 	r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_LEGACY, 241,
1187b843c749SSergey Zigachev 			      &adev->sdma.illegal_inst_irq);
1188b843c749SSergey Zigachev 	if (r)
1189b843c749SSergey Zigachev 		return r;
1190b843c749SSergey Zigachev 
1191b843c749SSergey Zigachev 	/* SDMA Privileged inst */
1192b843c749SSergey Zigachev 	r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_LEGACY, VISLANDS30_IV_SRCID_SDMA_SRBM_WRITE,
1193b843c749SSergey Zigachev 			      &adev->sdma.illegal_inst_irq);
1194b843c749SSergey Zigachev 	if (r)
1195b843c749SSergey Zigachev 		return r;
1196b843c749SSergey Zigachev 
1197b843c749SSergey Zigachev 	r = sdma_v3_0_init_microcode(adev);
1198b843c749SSergey Zigachev 	if (r) {
1199b843c749SSergey Zigachev 		DRM_ERROR("Failed to load sdma firmware!\n");
1200b843c749SSergey Zigachev 		return r;
1201b843c749SSergey Zigachev 	}
1202b843c749SSergey Zigachev 
1203b843c749SSergey Zigachev 	for (i = 0; i < adev->sdma.num_instances; i++) {
1204b843c749SSergey Zigachev 		ring = &adev->sdma.instance[i].ring;
1205b843c749SSergey Zigachev 		ring->ring_obj = NULL;
1206b843c749SSergey Zigachev 		if (!amdgpu_sriov_vf(adev)) {
1207b843c749SSergey Zigachev 			ring->use_doorbell = true;
1208b843c749SSergey Zigachev 			ring->doorbell_index = (i == 0) ?
1209b843c749SSergey Zigachev 				AMDGPU_DOORBELL_sDMA_ENGINE0 : AMDGPU_DOORBELL_sDMA_ENGINE1;
1210b843c749SSergey Zigachev 		} else {
1211b843c749SSergey Zigachev 			ring->use_pollmem = true;
1212b843c749SSergey Zigachev 		}
1213b843c749SSergey Zigachev 
121478973132SSergey Zigachev 		ksprintf(ring->name, "sdma%d", i);
1215b843c749SSergey Zigachev 		r = amdgpu_ring_init(adev, ring, 1024,
1216b843c749SSergey Zigachev 				     &adev->sdma.trap_irq,
1217b843c749SSergey Zigachev 				     (i == 0) ?
1218b843c749SSergey Zigachev 				     AMDGPU_SDMA_IRQ_TRAP0 :
1219b843c749SSergey Zigachev 				     AMDGPU_SDMA_IRQ_TRAP1);
1220b843c749SSergey Zigachev 		if (r)
1221b843c749SSergey Zigachev 			return r;
1222b843c749SSergey Zigachev 	}
1223b843c749SSergey Zigachev 
1224b843c749SSergey Zigachev 	return r;
1225b843c749SSergey Zigachev }
1226b843c749SSergey Zigachev 
sdma_v3_0_sw_fini(void * handle)1227b843c749SSergey Zigachev static int sdma_v3_0_sw_fini(void *handle)
1228b843c749SSergey Zigachev {
1229b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1230b843c749SSergey Zigachev 	int i;
1231b843c749SSergey Zigachev 
1232b843c749SSergey Zigachev 	for (i = 0; i < adev->sdma.num_instances; i++)
1233b843c749SSergey Zigachev 		amdgpu_ring_fini(&adev->sdma.instance[i].ring);
1234b843c749SSergey Zigachev 
1235b843c749SSergey Zigachev 	sdma_v3_0_free_microcode(adev);
1236b843c749SSergey Zigachev 	return 0;
1237b843c749SSergey Zigachev }
1238b843c749SSergey Zigachev 
sdma_v3_0_hw_init(void * handle)1239b843c749SSergey Zigachev static int sdma_v3_0_hw_init(void *handle)
1240b843c749SSergey Zigachev {
1241b843c749SSergey Zigachev 	int r;
1242b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1243b843c749SSergey Zigachev 
1244b843c749SSergey Zigachev 	sdma_v3_0_init_golden_registers(adev);
1245b843c749SSergey Zigachev 
1246b843c749SSergey Zigachev 	r = sdma_v3_0_start(adev);
1247b843c749SSergey Zigachev 	if (r)
1248b843c749SSergey Zigachev 		return r;
1249b843c749SSergey Zigachev 
1250b843c749SSergey Zigachev 	return r;
1251b843c749SSergey Zigachev }
1252b843c749SSergey Zigachev 
sdma_v3_0_hw_fini(void * handle)1253b843c749SSergey Zigachev static int sdma_v3_0_hw_fini(void *handle)
1254b843c749SSergey Zigachev {
1255b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1256b843c749SSergey Zigachev 
1257b843c749SSergey Zigachev 	sdma_v3_0_ctx_switch_enable(adev, false);
1258b843c749SSergey Zigachev 	sdma_v3_0_enable(adev, false);
1259b843c749SSergey Zigachev 
1260b843c749SSergey Zigachev 	return 0;
1261b843c749SSergey Zigachev }
1262b843c749SSergey Zigachev 
sdma_v3_0_suspend(void * handle)1263b843c749SSergey Zigachev static int sdma_v3_0_suspend(void *handle)
1264b843c749SSergey Zigachev {
1265b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1266b843c749SSergey Zigachev 
1267b843c749SSergey Zigachev 	return sdma_v3_0_hw_fini(adev);
1268b843c749SSergey Zigachev }
1269b843c749SSergey Zigachev 
sdma_v3_0_resume(void * handle)1270b843c749SSergey Zigachev static int sdma_v3_0_resume(void *handle)
1271b843c749SSergey Zigachev {
1272b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1273b843c749SSergey Zigachev 
1274b843c749SSergey Zigachev 	return sdma_v3_0_hw_init(adev);
1275b843c749SSergey Zigachev }
1276b843c749SSergey Zigachev 
sdma_v3_0_is_idle(void * handle)1277b843c749SSergey Zigachev static bool sdma_v3_0_is_idle(void *handle)
1278b843c749SSergey Zigachev {
1279b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1280b843c749SSergey Zigachev 	u32 tmp = RREG32(mmSRBM_STATUS2);
1281b843c749SSergey Zigachev 
1282b843c749SSergey Zigachev 	if (tmp & (SRBM_STATUS2__SDMA_BUSY_MASK |
1283b843c749SSergey Zigachev 		   SRBM_STATUS2__SDMA1_BUSY_MASK))
1284b843c749SSergey Zigachev 	    return false;
1285b843c749SSergey Zigachev 
1286b843c749SSergey Zigachev 	return true;
1287b843c749SSergey Zigachev }
1288b843c749SSergey Zigachev 
sdma_v3_0_wait_for_idle(void * handle)1289b843c749SSergey Zigachev static int sdma_v3_0_wait_for_idle(void *handle)
1290b843c749SSergey Zigachev {
1291b843c749SSergey Zigachev 	unsigned i;
1292b843c749SSergey Zigachev 	u32 tmp;
1293b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1294b843c749SSergey Zigachev 
1295b843c749SSergey Zigachev 	for (i = 0; i < adev->usec_timeout; i++) {
1296b843c749SSergey Zigachev 		tmp = RREG32(mmSRBM_STATUS2) & (SRBM_STATUS2__SDMA_BUSY_MASK |
1297b843c749SSergey Zigachev 				SRBM_STATUS2__SDMA1_BUSY_MASK);
1298b843c749SSergey Zigachev 
1299b843c749SSergey Zigachev 		if (!tmp)
1300b843c749SSergey Zigachev 			return 0;
1301b843c749SSergey Zigachev 		udelay(1);
1302b843c749SSergey Zigachev 	}
1303b843c749SSergey Zigachev 	return -ETIMEDOUT;
1304b843c749SSergey Zigachev }
1305b843c749SSergey Zigachev 
sdma_v3_0_check_soft_reset(void * handle)1306b843c749SSergey Zigachev static bool sdma_v3_0_check_soft_reset(void *handle)
1307b843c749SSergey Zigachev {
1308b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1309b843c749SSergey Zigachev 	u32 srbm_soft_reset = 0;
1310b843c749SSergey Zigachev 	u32 tmp = RREG32(mmSRBM_STATUS2);
1311b843c749SSergey Zigachev 
1312b843c749SSergey Zigachev 	if ((tmp & SRBM_STATUS2__SDMA_BUSY_MASK) ||
1313b843c749SSergey Zigachev 	    (tmp & SRBM_STATUS2__SDMA1_BUSY_MASK)) {
1314b843c749SSergey Zigachev 		srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_SDMA_MASK;
1315b843c749SSergey Zigachev 		srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_SDMA1_MASK;
1316b843c749SSergey Zigachev 	}
1317b843c749SSergey Zigachev 
1318b843c749SSergey Zigachev 	if (srbm_soft_reset) {
1319b843c749SSergey Zigachev 		adev->sdma.srbm_soft_reset = srbm_soft_reset;
1320b843c749SSergey Zigachev 		return true;
1321b843c749SSergey Zigachev 	} else {
1322b843c749SSergey Zigachev 		adev->sdma.srbm_soft_reset = 0;
1323b843c749SSergey Zigachev 		return false;
1324b843c749SSergey Zigachev 	}
1325b843c749SSergey Zigachev }
1326b843c749SSergey Zigachev 
sdma_v3_0_pre_soft_reset(void * handle)1327b843c749SSergey Zigachev static int sdma_v3_0_pre_soft_reset(void *handle)
1328b843c749SSergey Zigachev {
1329b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1330b843c749SSergey Zigachev 	u32 srbm_soft_reset = 0;
1331b843c749SSergey Zigachev 
1332b843c749SSergey Zigachev 	if (!adev->sdma.srbm_soft_reset)
1333b843c749SSergey Zigachev 		return 0;
1334b843c749SSergey Zigachev 
1335b843c749SSergey Zigachev 	srbm_soft_reset = adev->sdma.srbm_soft_reset;
1336b843c749SSergey Zigachev 
1337b843c749SSergey Zigachev 	if (REG_GET_FIELD(srbm_soft_reset, SRBM_SOFT_RESET, SOFT_RESET_SDMA) ||
1338b843c749SSergey Zigachev 	    REG_GET_FIELD(srbm_soft_reset, SRBM_SOFT_RESET, SOFT_RESET_SDMA1)) {
1339b843c749SSergey Zigachev 		sdma_v3_0_ctx_switch_enable(adev, false);
1340b843c749SSergey Zigachev 		sdma_v3_0_enable(adev, false);
1341b843c749SSergey Zigachev 	}
1342b843c749SSergey Zigachev 
1343b843c749SSergey Zigachev 	return 0;
1344b843c749SSergey Zigachev }
1345b843c749SSergey Zigachev 
sdma_v3_0_post_soft_reset(void * handle)1346b843c749SSergey Zigachev static int sdma_v3_0_post_soft_reset(void *handle)
1347b843c749SSergey Zigachev {
1348b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1349b843c749SSergey Zigachev 	u32 srbm_soft_reset = 0;
1350b843c749SSergey Zigachev 
1351b843c749SSergey Zigachev 	if (!adev->sdma.srbm_soft_reset)
1352b843c749SSergey Zigachev 		return 0;
1353b843c749SSergey Zigachev 
1354b843c749SSergey Zigachev 	srbm_soft_reset = adev->sdma.srbm_soft_reset;
1355b843c749SSergey Zigachev 
1356b843c749SSergey Zigachev 	if (REG_GET_FIELD(srbm_soft_reset, SRBM_SOFT_RESET, SOFT_RESET_SDMA) ||
1357b843c749SSergey Zigachev 	    REG_GET_FIELD(srbm_soft_reset, SRBM_SOFT_RESET, SOFT_RESET_SDMA1)) {
1358b843c749SSergey Zigachev 		sdma_v3_0_gfx_resume(adev);
1359b843c749SSergey Zigachev 		sdma_v3_0_rlc_resume(adev);
1360b843c749SSergey Zigachev 	}
1361b843c749SSergey Zigachev 
1362b843c749SSergey Zigachev 	return 0;
1363b843c749SSergey Zigachev }
1364b843c749SSergey Zigachev 
sdma_v3_0_soft_reset(void * handle)1365b843c749SSergey Zigachev static int sdma_v3_0_soft_reset(void *handle)
1366b843c749SSergey Zigachev {
1367b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1368b843c749SSergey Zigachev 	u32 srbm_soft_reset = 0;
1369b843c749SSergey Zigachev 	u32 tmp;
1370b843c749SSergey Zigachev 
1371b843c749SSergey Zigachev 	if (!adev->sdma.srbm_soft_reset)
1372b843c749SSergey Zigachev 		return 0;
1373b843c749SSergey Zigachev 
1374b843c749SSergey Zigachev 	srbm_soft_reset = adev->sdma.srbm_soft_reset;
1375b843c749SSergey Zigachev 
1376b843c749SSergey Zigachev 	if (srbm_soft_reset) {
1377b843c749SSergey Zigachev 		tmp = RREG32(mmSRBM_SOFT_RESET);
1378b843c749SSergey Zigachev 		tmp |= srbm_soft_reset;
1379b843c749SSergey Zigachev 		dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
1380b843c749SSergey Zigachev 		WREG32(mmSRBM_SOFT_RESET, tmp);
1381b843c749SSergey Zigachev 		tmp = RREG32(mmSRBM_SOFT_RESET);
1382b843c749SSergey Zigachev 
1383b843c749SSergey Zigachev 		udelay(50);
1384b843c749SSergey Zigachev 
1385b843c749SSergey Zigachev 		tmp &= ~srbm_soft_reset;
1386b843c749SSergey Zigachev 		WREG32(mmSRBM_SOFT_RESET, tmp);
1387b843c749SSergey Zigachev 		tmp = RREG32(mmSRBM_SOFT_RESET);
1388b843c749SSergey Zigachev 
1389b843c749SSergey Zigachev 		/* Wait a little for things to settle down */
1390b843c749SSergey Zigachev 		udelay(50);
1391b843c749SSergey Zigachev 	}
1392b843c749SSergey Zigachev 
1393b843c749SSergey Zigachev 	return 0;
1394b843c749SSergey Zigachev }
1395b843c749SSergey Zigachev 
sdma_v3_0_set_trap_irq_state(struct amdgpu_device * adev,struct amdgpu_irq_src * source,unsigned type,enum amdgpu_interrupt_state state)1396b843c749SSergey Zigachev static int sdma_v3_0_set_trap_irq_state(struct amdgpu_device *adev,
1397b843c749SSergey Zigachev 					struct amdgpu_irq_src *source,
1398b843c749SSergey Zigachev 					unsigned type,
1399b843c749SSergey Zigachev 					enum amdgpu_interrupt_state state)
1400b843c749SSergey Zigachev {
1401b843c749SSergey Zigachev 	u32 sdma_cntl;
1402b843c749SSergey Zigachev 
1403b843c749SSergey Zigachev 	switch (type) {
1404b843c749SSergey Zigachev 	case AMDGPU_SDMA_IRQ_TRAP0:
1405b843c749SSergey Zigachev 		switch (state) {
1406b843c749SSergey Zigachev 		case AMDGPU_IRQ_STATE_DISABLE:
1407b843c749SSergey Zigachev 			sdma_cntl = RREG32(mmSDMA0_CNTL + SDMA0_REGISTER_OFFSET);
1408b843c749SSergey Zigachev 			sdma_cntl = REG_SET_FIELD(sdma_cntl, SDMA0_CNTL, TRAP_ENABLE, 0);
1409b843c749SSergey Zigachev 			WREG32(mmSDMA0_CNTL + SDMA0_REGISTER_OFFSET, sdma_cntl);
1410b843c749SSergey Zigachev 			break;
1411b843c749SSergey Zigachev 		case AMDGPU_IRQ_STATE_ENABLE:
1412b843c749SSergey Zigachev 			sdma_cntl = RREG32(mmSDMA0_CNTL + SDMA0_REGISTER_OFFSET);
1413b843c749SSergey Zigachev 			sdma_cntl = REG_SET_FIELD(sdma_cntl, SDMA0_CNTL, TRAP_ENABLE, 1);
1414b843c749SSergey Zigachev 			WREG32(mmSDMA0_CNTL + SDMA0_REGISTER_OFFSET, sdma_cntl);
1415b843c749SSergey Zigachev 			break;
1416b843c749SSergey Zigachev 		default:
1417b843c749SSergey Zigachev 			break;
1418b843c749SSergey Zigachev 		}
1419b843c749SSergey Zigachev 		break;
1420b843c749SSergey Zigachev 	case AMDGPU_SDMA_IRQ_TRAP1:
1421b843c749SSergey Zigachev 		switch (state) {
1422b843c749SSergey Zigachev 		case AMDGPU_IRQ_STATE_DISABLE:
1423b843c749SSergey Zigachev 			sdma_cntl = RREG32(mmSDMA0_CNTL + SDMA1_REGISTER_OFFSET);
1424b843c749SSergey Zigachev 			sdma_cntl = REG_SET_FIELD(sdma_cntl, SDMA0_CNTL, TRAP_ENABLE, 0);
1425b843c749SSergey Zigachev 			WREG32(mmSDMA0_CNTL + SDMA1_REGISTER_OFFSET, sdma_cntl);
1426b843c749SSergey Zigachev 			break;
1427b843c749SSergey Zigachev 		case AMDGPU_IRQ_STATE_ENABLE:
1428b843c749SSergey Zigachev 			sdma_cntl = RREG32(mmSDMA0_CNTL + SDMA1_REGISTER_OFFSET);
1429b843c749SSergey Zigachev 			sdma_cntl = REG_SET_FIELD(sdma_cntl, SDMA0_CNTL, TRAP_ENABLE, 1);
1430b843c749SSergey Zigachev 			WREG32(mmSDMA0_CNTL + SDMA1_REGISTER_OFFSET, sdma_cntl);
1431b843c749SSergey Zigachev 			break;
1432b843c749SSergey Zigachev 		default:
1433b843c749SSergey Zigachev 			break;
1434b843c749SSergey Zigachev 		}
1435b843c749SSergey Zigachev 		break;
1436b843c749SSergey Zigachev 	default:
1437b843c749SSergey Zigachev 		break;
1438b843c749SSergey Zigachev 	}
1439b843c749SSergey Zigachev 	return 0;
1440b843c749SSergey Zigachev }
1441b843c749SSergey Zigachev 
sdma_v3_0_process_trap_irq(struct amdgpu_device * adev,struct amdgpu_irq_src * source,struct amdgpu_iv_entry * entry)1442b843c749SSergey Zigachev static int sdma_v3_0_process_trap_irq(struct amdgpu_device *adev,
1443b843c749SSergey Zigachev 				      struct amdgpu_irq_src *source,
1444b843c749SSergey Zigachev 				      struct amdgpu_iv_entry *entry)
1445b843c749SSergey Zigachev {
1446b843c749SSergey Zigachev 	u8 instance_id, queue_id;
1447b843c749SSergey Zigachev 
1448b843c749SSergey Zigachev 	instance_id = (entry->ring_id & 0x3) >> 0;
1449b843c749SSergey Zigachev 	queue_id = (entry->ring_id & 0xc) >> 2;
1450b843c749SSergey Zigachev 	DRM_DEBUG("IH: SDMA trap\n");
1451b843c749SSergey Zigachev 	switch (instance_id) {
1452b843c749SSergey Zigachev 	case 0:
1453b843c749SSergey Zigachev 		switch (queue_id) {
1454b843c749SSergey Zigachev 		case 0:
1455b843c749SSergey Zigachev 			amdgpu_fence_process(&adev->sdma.instance[0].ring);
1456b843c749SSergey Zigachev 			break;
1457b843c749SSergey Zigachev 		case 1:
1458b843c749SSergey Zigachev 			/* XXX compute */
1459b843c749SSergey Zigachev 			break;
1460b843c749SSergey Zigachev 		case 2:
1461b843c749SSergey Zigachev 			/* XXX compute */
1462b843c749SSergey Zigachev 			break;
1463b843c749SSergey Zigachev 		}
1464b843c749SSergey Zigachev 		break;
1465b843c749SSergey Zigachev 	case 1:
1466b843c749SSergey Zigachev 		switch (queue_id) {
1467b843c749SSergey Zigachev 		case 0:
1468b843c749SSergey Zigachev 			amdgpu_fence_process(&adev->sdma.instance[1].ring);
1469b843c749SSergey Zigachev 			break;
1470b843c749SSergey Zigachev 		case 1:
1471b843c749SSergey Zigachev 			/* XXX compute */
1472b843c749SSergey Zigachev 			break;
1473b843c749SSergey Zigachev 		case 2:
1474b843c749SSergey Zigachev 			/* XXX compute */
1475b843c749SSergey Zigachev 			break;
1476b843c749SSergey Zigachev 		}
1477b843c749SSergey Zigachev 		break;
1478b843c749SSergey Zigachev 	}
1479b843c749SSergey Zigachev 	return 0;
1480b843c749SSergey Zigachev }
1481b843c749SSergey Zigachev 
sdma_v3_0_process_illegal_inst_irq(struct amdgpu_device * adev,struct amdgpu_irq_src * source,struct amdgpu_iv_entry * entry)1482b843c749SSergey Zigachev static int sdma_v3_0_process_illegal_inst_irq(struct amdgpu_device *adev,
1483b843c749SSergey Zigachev 					      struct amdgpu_irq_src *source,
1484b843c749SSergey Zigachev 					      struct amdgpu_iv_entry *entry)
1485b843c749SSergey Zigachev {
1486b843c749SSergey Zigachev 	DRM_ERROR("Illegal instruction in SDMA command stream\n");
1487b843c749SSergey Zigachev 	schedule_work(&adev->reset_work);
1488b843c749SSergey Zigachev 	return 0;
1489b843c749SSergey Zigachev }
1490b843c749SSergey Zigachev 
sdma_v3_0_update_sdma_medium_grain_clock_gating(struct amdgpu_device * adev,bool enable)1491b843c749SSergey Zigachev static void sdma_v3_0_update_sdma_medium_grain_clock_gating(
1492b843c749SSergey Zigachev 		struct amdgpu_device *adev,
1493b843c749SSergey Zigachev 		bool enable)
1494b843c749SSergey Zigachev {
1495b843c749SSergey Zigachev 	uint32_t temp, data;
1496b843c749SSergey Zigachev 	int i;
1497b843c749SSergey Zigachev 
1498b843c749SSergey Zigachev 	if (enable && (adev->cg_flags & AMD_CG_SUPPORT_SDMA_MGCG)) {
1499b843c749SSergey Zigachev 		for (i = 0; i < adev->sdma.num_instances; i++) {
1500b843c749SSergey Zigachev 			temp = data = RREG32(mmSDMA0_CLK_CTRL + sdma_offsets[i]);
1501b843c749SSergey Zigachev 			data &= ~(SDMA0_CLK_CTRL__SOFT_OVERRIDE7_MASK |
1502b843c749SSergey Zigachev 				  SDMA0_CLK_CTRL__SOFT_OVERRIDE6_MASK |
1503b843c749SSergey Zigachev 				  SDMA0_CLK_CTRL__SOFT_OVERRIDE5_MASK |
1504b843c749SSergey Zigachev 				  SDMA0_CLK_CTRL__SOFT_OVERRIDE4_MASK |
1505b843c749SSergey Zigachev 				  SDMA0_CLK_CTRL__SOFT_OVERRIDE3_MASK |
1506b843c749SSergey Zigachev 				  SDMA0_CLK_CTRL__SOFT_OVERRIDE2_MASK |
1507b843c749SSergey Zigachev 				  SDMA0_CLK_CTRL__SOFT_OVERRIDE1_MASK |
1508b843c749SSergey Zigachev 				  SDMA0_CLK_CTRL__SOFT_OVERRIDE0_MASK);
1509b843c749SSergey Zigachev 			if (data != temp)
1510b843c749SSergey Zigachev 				WREG32(mmSDMA0_CLK_CTRL + sdma_offsets[i], data);
1511b843c749SSergey Zigachev 		}
1512b843c749SSergey Zigachev 	} else {
1513b843c749SSergey Zigachev 		for (i = 0; i < adev->sdma.num_instances; i++) {
1514b843c749SSergey Zigachev 			temp = data = RREG32(mmSDMA0_CLK_CTRL + sdma_offsets[i]);
1515b843c749SSergey Zigachev 			data |= SDMA0_CLK_CTRL__SOFT_OVERRIDE7_MASK |
1516b843c749SSergey Zigachev 				SDMA0_CLK_CTRL__SOFT_OVERRIDE6_MASK |
1517b843c749SSergey Zigachev 				SDMA0_CLK_CTRL__SOFT_OVERRIDE5_MASK |
1518b843c749SSergey Zigachev 				SDMA0_CLK_CTRL__SOFT_OVERRIDE4_MASK |
1519b843c749SSergey Zigachev 				SDMA0_CLK_CTRL__SOFT_OVERRIDE3_MASK |
1520b843c749SSergey Zigachev 				SDMA0_CLK_CTRL__SOFT_OVERRIDE2_MASK |
1521b843c749SSergey Zigachev 				SDMA0_CLK_CTRL__SOFT_OVERRIDE1_MASK |
1522b843c749SSergey Zigachev 				SDMA0_CLK_CTRL__SOFT_OVERRIDE0_MASK;
1523b843c749SSergey Zigachev 
1524b843c749SSergey Zigachev 			if (data != temp)
1525b843c749SSergey Zigachev 				WREG32(mmSDMA0_CLK_CTRL + sdma_offsets[i], data);
1526b843c749SSergey Zigachev 		}
1527b843c749SSergey Zigachev 	}
1528b843c749SSergey Zigachev }
1529b843c749SSergey Zigachev 
sdma_v3_0_update_sdma_medium_grain_light_sleep(struct amdgpu_device * adev,bool enable)1530b843c749SSergey Zigachev static void sdma_v3_0_update_sdma_medium_grain_light_sleep(
1531b843c749SSergey Zigachev 		struct amdgpu_device *adev,
1532b843c749SSergey Zigachev 		bool enable)
1533b843c749SSergey Zigachev {
1534b843c749SSergey Zigachev 	uint32_t temp, data;
1535b843c749SSergey Zigachev 	int i;
1536b843c749SSergey Zigachev 
1537b843c749SSergey Zigachev 	if (enable && (adev->cg_flags & AMD_CG_SUPPORT_SDMA_LS)) {
1538b843c749SSergey Zigachev 		for (i = 0; i < adev->sdma.num_instances; i++) {
1539b843c749SSergey Zigachev 			temp = data = RREG32(mmSDMA0_POWER_CNTL + sdma_offsets[i]);
1540b843c749SSergey Zigachev 			data |= SDMA0_POWER_CNTL__MEM_POWER_OVERRIDE_MASK;
1541b843c749SSergey Zigachev 
1542b843c749SSergey Zigachev 			if (temp != data)
1543b843c749SSergey Zigachev 				WREG32(mmSDMA0_POWER_CNTL + sdma_offsets[i], data);
1544b843c749SSergey Zigachev 		}
1545b843c749SSergey Zigachev 	} else {
1546b843c749SSergey Zigachev 		for (i = 0; i < adev->sdma.num_instances; i++) {
1547b843c749SSergey Zigachev 			temp = data = RREG32(mmSDMA0_POWER_CNTL + sdma_offsets[i]);
1548b843c749SSergey Zigachev 			data &= ~SDMA0_POWER_CNTL__MEM_POWER_OVERRIDE_MASK;
1549b843c749SSergey Zigachev 
1550b843c749SSergey Zigachev 			if (temp != data)
1551b843c749SSergey Zigachev 				WREG32(mmSDMA0_POWER_CNTL + sdma_offsets[i], data);
1552b843c749SSergey Zigachev 		}
1553b843c749SSergey Zigachev 	}
1554b843c749SSergey Zigachev }
1555b843c749SSergey Zigachev 
sdma_v3_0_set_clockgating_state(void * handle,enum amd_clockgating_state state)1556b843c749SSergey Zigachev static int sdma_v3_0_set_clockgating_state(void *handle,
1557b843c749SSergey Zigachev 					  enum amd_clockgating_state state)
1558b843c749SSergey Zigachev {
1559b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1560b843c749SSergey Zigachev 
1561b843c749SSergey Zigachev 	if (amdgpu_sriov_vf(adev))
1562b843c749SSergey Zigachev 		return 0;
1563b843c749SSergey Zigachev 
1564b843c749SSergey Zigachev 	switch (adev->asic_type) {
1565b843c749SSergey Zigachev 	case CHIP_FIJI:
1566b843c749SSergey Zigachev 	case CHIP_CARRIZO:
1567b843c749SSergey Zigachev 	case CHIP_STONEY:
1568b843c749SSergey Zigachev 		sdma_v3_0_update_sdma_medium_grain_clock_gating(adev,
1569b843c749SSergey Zigachev 				state == AMD_CG_STATE_GATE);
1570b843c749SSergey Zigachev 		sdma_v3_0_update_sdma_medium_grain_light_sleep(adev,
1571b843c749SSergey Zigachev 				state == AMD_CG_STATE_GATE);
1572b843c749SSergey Zigachev 		break;
1573b843c749SSergey Zigachev 	default:
1574b843c749SSergey Zigachev 		break;
1575b843c749SSergey Zigachev 	}
1576b843c749SSergey Zigachev 	return 0;
1577b843c749SSergey Zigachev }
1578b843c749SSergey Zigachev 
sdma_v3_0_set_powergating_state(void * handle,enum amd_powergating_state state)1579b843c749SSergey Zigachev static int sdma_v3_0_set_powergating_state(void *handle,
1580b843c749SSergey Zigachev 					  enum amd_powergating_state state)
1581b843c749SSergey Zigachev {
1582b843c749SSergey Zigachev 	return 0;
1583b843c749SSergey Zigachev }
1584b843c749SSergey Zigachev 
sdma_v3_0_get_clockgating_state(void * handle,u32 * flags)1585b843c749SSergey Zigachev static void sdma_v3_0_get_clockgating_state(void *handle, u32 *flags)
1586b843c749SSergey Zigachev {
1587b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1588b843c749SSergey Zigachev 	int data;
1589b843c749SSergey Zigachev 
1590b843c749SSergey Zigachev 	if (amdgpu_sriov_vf(adev))
1591b843c749SSergey Zigachev 		*flags = 0;
1592b843c749SSergey Zigachev 
1593b843c749SSergey Zigachev 	/* AMD_CG_SUPPORT_SDMA_MGCG */
1594b843c749SSergey Zigachev 	data = RREG32(mmSDMA0_CLK_CTRL + sdma_offsets[0]);
1595b843c749SSergey Zigachev 	if (!(data & SDMA0_CLK_CTRL__SOFT_OVERRIDE0_MASK))
1596b843c749SSergey Zigachev 		*flags |= AMD_CG_SUPPORT_SDMA_MGCG;
1597b843c749SSergey Zigachev 
1598b843c749SSergey Zigachev 	/* AMD_CG_SUPPORT_SDMA_LS */
1599b843c749SSergey Zigachev 	data = RREG32(mmSDMA0_POWER_CNTL + sdma_offsets[0]);
1600b843c749SSergey Zigachev 	if (data & SDMA0_POWER_CNTL__MEM_POWER_OVERRIDE_MASK)
1601b843c749SSergey Zigachev 		*flags |= AMD_CG_SUPPORT_SDMA_LS;
1602b843c749SSergey Zigachev }
1603b843c749SSergey Zigachev 
1604b843c749SSergey Zigachev static const struct amd_ip_funcs sdma_v3_0_ip_funcs = {
1605b843c749SSergey Zigachev 	.name = "sdma_v3_0",
1606b843c749SSergey Zigachev 	.early_init = sdma_v3_0_early_init,
1607b843c749SSergey Zigachev 	.late_init = NULL,
1608b843c749SSergey Zigachev 	.sw_init = sdma_v3_0_sw_init,
1609b843c749SSergey Zigachev 	.sw_fini = sdma_v3_0_sw_fini,
1610b843c749SSergey Zigachev 	.hw_init = sdma_v3_0_hw_init,
1611b843c749SSergey Zigachev 	.hw_fini = sdma_v3_0_hw_fini,
1612b843c749SSergey Zigachev 	.suspend = sdma_v3_0_suspend,
1613b843c749SSergey Zigachev 	.resume = sdma_v3_0_resume,
1614b843c749SSergey Zigachev 	.is_idle = sdma_v3_0_is_idle,
1615b843c749SSergey Zigachev 	.wait_for_idle = sdma_v3_0_wait_for_idle,
1616b843c749SSergey Zigachev 	.check_soft_reset = sdma_v3_0_check_soft_reset,
1617b843c749SSergey Zigachev 	.pre_soft_reset = sdma_v3_0_pre_soft_reset,
1618b843c749SSergey Zigachev 	.post_soft_reset = sdma_v3_0_post_soft_reset,
1619b843c749SSergey Zigachev 	.soft_reset = sdma_v3_0_soft_reset,
1620b843c749SSergey Zigachev 	.set_clockgating_state = sdma_v3_0_set_clockgating_state,
1621b843c749SSergey Zigachev 	.set_powergating_state = sdma_v3_0_set_powergating_state,
1622b843c749SSergey Zigachev 	.get_clockgating_state = sdma_v3_0_get_clockgating_state,
1623b843c749SSergey Zigachev };
1624b843c749SSergey Zigachev 
1625b843c749SSergey Zigachev static const struct amdgpu_ring_funcs sdma_v3_0_ring_funcs = {
1626b843c749SSergey Zigachev 	.type = AMDGPU_RING_TYPE_SDMA,
1627b843c749SSergey Zigachev 	.align_mask = 0xf,
1628b843c749SSergey Zigachev 	.nop = SDMA_PKT_NOP_HEADER_OP(SDMA_OP_NOP),
1629b843c749SSergey Zigachev 	.support_64bit_ptrs = false,
1630b843c749SSergey Zigachev 	.get_rptr = sdma_v3_0_ring_get_rptr,
1631b843c749SSergey Zigachev 	.get_wptr = sdma_v3_0_ring_get_wptr,
1632b843c749SSergey Zigachev 	.set_wptr = sdma_v3_0_ring_set_wptr,
1633b843c749SSergey Zigachev 	.emit_frame_size =
1634b843c749SSergey Zigachev 		6 + /* sdma_v3_0_ring_emit_hdp_flush */
1635b843c749SSergey Zigachev 		3 + /* hdp invalidate */
1636b843c749SSergey Zigachev 		6 + /* sdma_v3_0_ring_emit_pipeline_sync */
1637b843c749SSergey Zigachev 		VI_FLUSH_GPU_TLB_NUM_WREG * 3 + 6 + /* sdma_v3_0_ring_emit_vm_flush */
1638b843c749SSergey Zigachev 		10 + 10 + 10, /* sdma_v3_0_ring_emit_fence x3 for user fence, vm fence */
1639b843c749SSergey Zigachev 	.emit_ib_size = 7 + 6, /* sdma_v3_0_ring_emit_ib */
1640b843c749SSergey Zigachev 	.emit_ib = sdma_v3_0_ring_emit_ib,
1641b843c749SSergey Zigachev 	.emit_fence = sdma_v3_0_ring_emit_fence,
1642b843c749SSergey Zigachev 	.emit_pipeline_sync = sdma_v3_0_ring_emit_pipeline_sync,
1643b843c749SSergey Zigachev 	.emit_vm_flush = sdma_v3_0_ring_emit_vm_flush,
1644b843c749SSergey Zigachev 	.emit_hdp_flush = sdma_v3_0_ring_emit_hdp_flush,
1645b843c749SSergey Zigachev 	.test_ring = sdma_v3_0_ring_test_ring,
1646b843c749SSergey Zigachev 	.test_ib = sdma_v3_0_ring_test_ib,
1647b843c749SSergey Zigachev 	.insert_nop = sdma_v3_0_ring_insert_nop,
1648b843c749SSergey Zigachev 	.pad_ib = sdma_v3_0_ring_pad_ib,
1649b843c749SSergey Zigachev 	.emit_wreg = sdma_v3_0_ring_emit_wreg,
1650b843c749SSergey Zigachev };
1651b843c749SSergey Zigachev 
sdma_v3_0_set_ring_funcs(struct amdgpu_device * adev)1652b843c749SSergey Zigachev static void sdma_v3_0_set_ring_funcs(struct amdgpu_device *adev)
1653b843c749SSergey Zigachev {
1654b843c749SSergey Zigachev 	int i;
1655b843c749SSergey Zigachev 
1656b843c749SSergey Zigachev 	for (i = 0; i < adev->sdma.num_instances; i++) {
1657b843c749SSergey Zigachev 		adev->sdma.instance[i].ring.funcs = &sdma_v3_0_ring_funcs;
1658b843c749SSergey Zigachev 		adev->sdma.instance[i].ring.me = i;
1659b843c749SSergey Zigachev 	}
1660b843c749SSergey Zigachev }
1661b843c749SSergey Zigachev 
1662b843c749SSergey Zigachev static const struct amdgpu_irq_src_funcs sdma_v3_0_trap_irq_funcs = {
1663b843c749SSergey Zigachev 	.set = sdma_v3_0_set_trap_irq_state,
1664b843c749SSergey Zigachev 	.process = sdma_v3_0_process_trap_irq,
1665b843c749SSergey Zigachev };
1666b843c749SSergey Zigachev 
1667b843c749SSergey Zigachev static const struct amdgpu_irq_src_funcs sdma_v3_0_illegal_inst_irq_funcs = {
1668b843c749SSergey Zigachev 	.process = sdma_v3_0_process_illegal_inst_irq,
1669b843c749SSergey Zigachev };
1670b843c749SSergey Zigachev 
sdma_v3_0_set_irq_funcs(struct amdgpu_device * adev)1671b843c749SSergey Zigachev static void sdma_v3_0_set_irq_funcs(struct amdgpu_device *adev)
1672b843c749SSergey Zigachev {
1673b843c749SSergey Zigachev 	adev->sdma.trap_irq.num_types = AMDGPU_SDMA_IRQ_LAST;
1674b843c749SSergey Zigachev 	adev->sdma.trap_irq.funcs = &sdma_v3_0_trap_irq_funcs;
1675b843c749SSergey Zigachev 	adev->sdma.illegal_inst_irq.funcs = &sdma_v3_0_illegal_inst_irq_funcs;
1676b843c749SSergey Zigachev }
1677b843c749SSergey Zigachev 
1678b843c749SSergey Zigachev /**
1679b843c749SSergey Zigachev  * sdma_v3_0_emit_copy_buffer - copy buffer using the sDMA engine
1680b843c749SSergey Zigachev  *
1681b843c749SSergey Zigachev  * @ring: amdgpu_ring structure holding ring information
1682b843c749SSergey Zigachev  * @src_offset: src GPU address
1683b843c749SSergey Zigachev  * @dst_offset: dst GPU address
1684b843c749SSergey Zigachev  * @byte_count: number of bytes to xfer
1685b843c749SSergey Zigachev  *
1686b843c749SSergey Zigachev  * Copy GPU buffers using the DMA engine (VI).
1687b843c749SSergey Zigachev  * Used by the amdgpu ttm implementation to move pages if
1688b843c749SSergey Zigachev  * registered as the asic copy callback.
1689b843c749SSergey Zigachev  */
sdma_v3_0_emit_copy_buffer(struct amdgpu_ib * ib,uint64_t src_offset,uint64_t dst_offset,uint32_t byte_count)1690b843c749SSergey Zigachev static void sdma_v3_0_emit_copy_buffer(struct amdgpu_ib *ib,
1691b843c749SSergey Zigachev 				       uint64_t src_offset,
1692b843c749SSergey Zigachev 				       uint64_t dst_offset,
1693b843c749SSergey Zigachev 				       uint32_t byte_count)
1694b843c749SSergey Zigachev {
1695b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
1696b843c749SSergey Zigachev 		SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR);
1697b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = byte_count;
1698b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
1699b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = lower_32_bits(src_offset);
1700b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = upper_32_bits(src_offset);
1701b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
1702b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
1703b843c749SSergey Zigachev }
1704b843c749SSergey Zigachev 
1705b843c749SSergey Zigachev /**
1706b843c749SSergey Zigachev  * sdma_v3_0_emit_fill_buffer - fill buffer using the sDMA engine
1707b843c749SSergey Zigachev  *
1708b843c749SSergey Zigachev  * @ring: amdgpu_ring structure holding ring information
1709b843c749SSergey Zigachev  * @src_data: value to write to buffer
1710b843c749SSergey Zigachev  * @dst_offset: dst GPU address
1711b843c749SSergey Zigachev  * @byte_count: number of bytes to xfer
1712b843c749SSergey Zigachev  *
1713b843c749SSergey Zigachev  * Fill GPU buffers using the DMA engine (VI).
1714b843c749SSergey Zigachev  */
sdma_v3_0_emit_fill_buffer(struct amdgpu_ib * ib,uint32_t src_data,uint64_t dst_offset,uint32_t byte_count)1715b843c749SSergey Zigachev static void sdma_v3_0_emit_fill_buffer(struct amdgpu_ib *ib,
1716b843c749SSergey Zigachev 				       uint32_t src_data,
1717b843c749SSergey Zigachev 				       uint64_t dst_offset,
1718b843c749SSergey Zigachev 				       uint32_t byte_count)
1719b843c749SSergey Zigachev {
1720b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_CONST_FILL);
1721b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
1722b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
1723b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = src_data;
1724b843c749SSergey Zigachev 	ib->ptr[ib->length_dw++] = byte_count;
1725b843c749SSergey Zigachev }
1726b843c749SSergey Zigachev 
1727b843c749SSergey Zigachev static const struct amdgpu_buffer_funcs sdma_v3_0_buffer_funcs = {
1728b843c749SSergey Zigachev 	.copy_max_bytes = 0x3fffe0, /* not 0x3fffff due to HW limitation */
1729b843c749SSergey Zigachev 	.copy_num_dw = 7,
1730b843c749SSergey Zigachev 	.emit_copy_buffer = sdma_v3_0_emit_copy_buffer,
1731b843c749SSergey Zigachev 
1732b843c749SSergey Zigachev 	.fill_max_bytes = 0x3fffe0, /* not 0x3fffff due to HW limitation */
1733b843c749SSergey Zigachev 	.fill_num_dw = 5,
1734b843c749SSergey Zigachev 	.emit_fill_buffer = sdma_v3_0_emit_fill_buffer,
1735b843c749SSergey Zigachev };
1736b843c749SSergey Zigachev 
sdma_v3_0_set_buffer_funcs(struct amdgpu_device * adev)1737b843c749SSergey Zigachev static void sdma_v3_0_set_buffer_funcs(struct amdgpu_device *adev)
1738b843c749SSergey Zigachev {
1739b843c749SSergey Zigachev 	if (adev->mman.buffer_funcs == NULL) {
1740b843c749SSergey Zigachev 		adev->mman.buffer_funcs = &sdma_v3_0_buffer_funcs;
1741b843c749SSergey Zigachev 		adev->mman.buffer_funcs_ring = &adev->sdma.instance[0].ring;
1742b843c749SSergey Zigachev 	}
1743b843c749SSergey Zigachev }
1744b843c749SSergey Zigachev 
1745b843c749SSergey Zigachev static const struct amdgpu_vm_pte_funcs sdma_v3_0_vm_pte_funcs = {
1746b843c749SSergey Zigachev 	.copy_pte_num_dw = 7,
1747b843c749SSergey Zigachev 	.copy_pte = sdma_v3_0_vm_copy_pte,
1748b843c749SSergey Zigachev 
1749b843c749SSergey Zigachev 	.write_pte = sdma_v3_0_vm_write_pte,
1750b843c749SSergey Zigachev 	.set_pte_pde = sdma_v3_0_vm_set_pte_pde,
1751b843c749SSergey Zigachev };
1752b843c749SSergey Zigachev 
sdma_v3_0_set_vm_pte_funcs(struct amdgpu_device * adev)1753b843c749SSergey Zigachev static void sdma_v3_0_set_vm_pte_funcs(struct amdgpu_device *adev)
1754b843c749SSergey Zigachev {
1755b843c749SSergey Zigachev 	unsigned i;
1756b843c749SSergey Zigachev 
1757b843c749SSergey Zigachev 	if (adev->vm_manager.vm_pte_funcs == NULL) {
1758b843c749SSergey Zigachev 		adev->vm_manager.vm_pte_funcs = &sdma_v3_0_vm_pte_funcs;
1759b843c749SSergey Zigachev 		for (i = 0; i < adev->sdma.num_instances; i++)
1760b843c749SSergey Zigachev 			adev->vm_manager.vm_pte_rings[i] =
1761b843c749SSergey Zigachev 				&adev->sdma.instance[i].ring;
1762b843c749SSergey Zigachev 
1763b843c749SSergey Zigachev 		adev->vm_manager.vm_pte_num_rings = adev->sdma.num_instances;
1764b843c749SSergey Zigachev 	}
1765b843c749SSergey Zigachev }
1766b843c749SSergey Zigachev 
1767b843c749SSergey Zigachev const struct amdgpu_ip_block_version sdma_v3_0_ip_block =
1768b843c749SSergey Zigachev {
1769b843c749SSergey Zigachev 	.type = AMD_IP_BLOCK_TYPE_SDMA,
1770b843c749SSergey Zigachev 	.major = 3,
1771b843c749SSergey Zigachev 	.minor = 0,
1772b843c749SSergey Zigachev 	.rev = 0,
1773b843c749SSergey Zigachev 	.funcs = &sdma_v3_0_ip_funcs,
1774b843c749SSergey Zigachev };
1775b843c749SSergey Zigachev 
1776b843c749SSergey Zigachev const struct amdgpu_ip_block_version sdma_v3_1_ip_block =
1777b843c749SSergey Zigachev {
1778b843c749SSergey Zigachev 	.type = AMD_IP_BLOCK_TYPE_SDMA,
1779b843c749SSergey Zigachev 	.major = 3,
1780b843c749SSergey Zigachev 	.minor = 1,
1781b843c749SSergey Zigachev 	.rev = 0,
1782b843c749SSergey Zigachev 	.funcs = &sdma_v3_0_ip_funcs,
1783b843c749SSergey Zigachev };
1784