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