2 * Copyright 2014 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
22 * Authors: Alex Deucher
24 #include <linux/firmware.h>
27 #include "amdgpu_ucode.h"
28 #include "amdgpu_trace.h"
32 #include "oss/oss_3_0_d.h"
33 #include "oss/oss_3_0_sh_mask.h"
35 #include "gmc/gmc_8_1_d.h"
36 #include "gmc/gmc_8_1_sh_mask.h"
38 #include "gca/gfx_8_0_d.h"
39 #include "gca/gfx_8_0_enum.h"
40 #include "gca/gfx_8_0_sh_mask.h"
42 #include "bif/bif_5_0_d.h"
43 #include "bif/bif_5_0_sh_mask.h"
45 #include "tonga_sdma_pkt_open.h"
47 #include "ivsrcid/ivsrcid_vislands30.h"
49 static void sdma_v3_0_set_ring_funcs(struct amdgpu_device *adev);
50 static void sdma_v3_0_set_buffer_funcs(struct amdgpu_device *adev);
51 static void sdma_v3_0_set_vm_pte_funcs(struct amdgpu_device *adev);
52 static void sdma_v3_0_set_irq_funcs(struct amdgpu_device *adev);
54 MODULE_FIRMWARE("amdgpu/tonga_sdma.bin");
55 MODULE_FIRMWARE("amdgpu/tonga_sdma1.bin");
56 MODULE_FIRMWARE("amdgpu/carrizo_sdma.bin");
57 MODULE_FIRMWARE("amdgpu/carrizo_sdma1.bin");
58 MODULE_FIRMWARE("amdgpu/fiji_sdma.bin");
59 MODULE_FIRMWARE("amdgpu/fiji_sdma1.bin");
60 MODULE_FIRMWARE("amdgpu/stoney_sdma.bin");
61 MODULE_FIRMWARE("amdgpu/polaris10_sdma.bin");
62 MODULE_FIRMWARE("amdgpu/polaris10_sdma1.bin");
63 MODULE_FIRMWARE("amdgpu/polaris11_sdma.bin");
64 MODULE_FIRMWARE("amdgpu/polaris11_sdma1.bin");
65 MODULE_FIRMWARE("amdgpu/polaris12_sdma.bin");
66 MODULE_FIRMWARE("amdgpu/polaris12_sdma1.bin");
67 MODULE_FIRMWARE("amdgpu/vegam_sdma.bin");
68 MODULE_FIRMWARE("amdgpu/vegam_sdma1.bin");
71 static const u32 sdma_offsets[SDMA_MAX_INSTANCE] =
73 SDMA0_REGISTER_OFFSET,
77 static const u32 golden_settings_tonga_a11[] =
79 mmSDMA0_CHICKEN_BITS, 0xfc910007, 0x00810007,
80 mmSDMA0_CLK_CTRL, 0xff000fff, 0x00000000,
81 mmSDMA0_GFX_IB_CNTL, 0x800f0111, 0x00000100,
82 mmSDMA0_RLC0_IB_CNTL, 0x800f0111, 0x00000100,
83 mmSDMA0_RLC1_IB_CNTL, 0x800f0111, 0x00000100,
84 mmSDMA1_CHICKEN_BITS, 0xfc910007, 0x00810007,
85 mmSDMA1_CLK_CTRL, 0xff000fff, 0x00000000,
86 mmSDMA1_GFX_IB_CNTL, 0x800f0111, 0x00000100,
87 mmSDMA1_RLC0_IB_CNTL, 0x800f0111, 0x00000100,
88 mmSDMA1_RLC1_IB_CNTL, 0x800f0111, 0x00000100,
91 static const u32 tonga_mgcg_cgcg_init[] =
93 mmSDMA0_CLK_CTRL, 0xff000ff0, 0x00000100,
94 mmSDMA1_CLK_CTRL, 0xff000ff0, 0x00000100
97 static const u32 golden_settings_fiji_a10[] =
99 mmSDMA0_CHICKEN_BITS, 0xfc910007, 0x00810007,
100 mmSDMA0_GFX_IB_CNTL, 0x800f0111, 0x00000100,
101 mmSDMA0_RLC0_IB_CNTL, 0x800f0111, 0x00000100,
102 mmSDMA0_RLC1_IB_CNTL, 0x800f0111, 0x00000100,
103 mmSDMA1_CHICKEN_BITS, 0xfc910007, 0x00810007,
104 mmSDMA1_GFX_IB_CNTL, 0x800f0111, 0x00000100,
105 mmSDMA1_RLC0_IB_CNTL, 0x800f0111, 0x00000100,
106 mmSDMA1_RLC1_IB_CNTL, 0x800f0111, 0x00000100,
109 static const u32 fiji_mgcg_cgcg_init[] =
111 mmSDMA0_CLK_CTRL, 0xff000ff0, 0x00000100,
112 mmSDMA1_CLK_CTRL, 0xff000ff0, 0x00000100
115 static const u32 golden_settings_polaris11_a11[] =
117 mmSDMA0_CHICKEN_BITS, 0xfc910007, 0x00810007,
118 mmSDMA0_CLK_CTRL, 0xff000fff, 0x00000000,
119 mmSDMA0_GFX_IB_CNTL, 0x800f0111, 0x00000100,
120 mmSDMA0_RLC0_IB_CNTL, 0x800f0111, 0x00000100,
121 mmSDMA0_RLC1_IB_CNTL, 0x800f0111, 0x00000100,
122 mmSDMA1_CHICKEN_BITS, 0xfc910007, 0x00810007,
123 mmSDMA1_CLK_CTRL, 0xff000fff, 0x00000000,
124 mmSDMA1_GFX_IB_CNTL, 0x800f0111, 0x00000100,
125 mmSDMA1_RLC0_IB_CNTL, 0x800f0111, 0x00000100,
126 mmSDMA1_RLC1_IB_CNTL, 0x800f0111, 0x00000100,
129 static const u32 golden_settings_polaris10_a11[] =
131 mmSDMA0_CHICKEN_BITS, 0xfc910007, 0x00810007,
132 mmSDMA0_CLK_CTRL, 0xff000fff, 0x00000000,
133 mmSDMA0_GFX_IB_CNTL, 0x800f0111, 0x00000100,
134 mmSDMA0_RLC0_IB_CNTL, 0x800f0111, 0x00000100,
135 mmSDMA0_RLC1_IB_CNTL, 0x800f0111, 0x00000100,
136 mmSDMA1_CHICKEN_BITS, 0xfc910007, 0x00810007,
137 mmSDMA1_CLK_CTRL, 0xff000fff, 0x00000000,
138 mmSDMA1_GFX_IB_CNTL, 0x800f0111, 0x00000100,
139 mmSDMA1_RLC0_IB_CNTL, 0x800f0111, 0x00000100,
140 mmSDMA1_RLC1_IB_CNTL, 0x800f0111, 0x00000100,
143 static const u32 cz_golden_settings_a11[] =
145 mmSDMA0_CHICKEN_BITS, 0xfc910007, 0x00810007,
146 mmSDMA0_CLK_CTRL, 0xff000fff, 0x00000000,
147 mmSDMA0_GFX_IB_CNTL, 0x00000100, 0x00000100,
148 mmSDMA0_POWER_CNTL, 0x00000800, 0x0003c800,
149 mmSDMA0_RLC0_IB_CNTL, 0x00000100, 0x00000100,
150 mmSDMA0_RLC1_IB_CNTL, 0x00000100, 0x00000100,
151 mmSDMA1_CHICKEN_BITS, 0xfc910007, 0x00810007,
152 mmSDMA1_CLK_CTRL, 0xff000fff, 0x00000000,
153 mmSDMA1_GFX_IB_CNTL, 0x00000100, 0x00000100,
154 mmSDMA1_POWER_CNTL, 0x00000800, 0x0003c800,
155 mmSDMA1_RLC0_IB_CNTL, 0x00000100, 0x00000100,
156 mmSDMA1_RLC1_IB_CNTL, 0x00000100, 0x00000100,
159 static const u32 cz_mgcg_cgcg_init[] =
161 mmSDMA0_CLK_CTRL, 0xff000ff0, 0x00000100,
162 mmSDMA1_CLK_CTRL, 0xff000ff0, 0x00000100
165 static const u32 stoney_golden_settings_a11[] =
167 mmSDMA0_GFX_IB_CNTL, 0x00000100, 0x00000100,
168 mmSDMA0_POWER_CNTL, 0x00000800, 0x0003c800,
169 mmSDMA0_RLC0_IB_CNTL, 0x00000100, 0x00000100,
170 mmSDMA0_RLC1_IB_CNTL, 0x00000100, 0x00000100,
173 static const u32 stoney_mgcg_cgcg_init[] =
175 mmSDMA0_CLK_CTRL, 0xffffffff, 0x00000100,
180 * Starting with CIK, the GPU has new asynchronous
181 * DMA engines. These engines are used for compute
182 * and gfx. There are two DMA engines (SDMA0, SDMA1)
183 * and each one supports 1 ring buffer used for gfx
184 * and 2 queues used for compute.
186 * The programming model is very similar to the CP
187 * (ring buffer, IBs, etc.), but sDMA has it's own
188 * packet format that is different from the PM4 format
189 * used by the CP. sDMA supports copying data, writing
190 * embedded data, solid fills, and a number of other
191 * things. It also has support for tiling/detiling of
195 static void sdma_v3_0_init_golden_registers(struct amdgpu_device *adev)
197 switch (adev->asic_type) {
199 amdgpu_device_program_register_sequence(adev,
201 ARRAY_SIZE(fiji_mgcg_cgcg_init));
202 amdgpu_device_program_register_sequence(adev,
203 golden_settings_fiji_a10,
204 ARRAY_SIZE(golden_settings_fiji_a10));
207 amdgpu_device_program_register_sequence(adev,
208 tonga_mgcg_cgcg_init,
209 ARRAY_SIZE(tonga_mgcg_cgcg_init));
210 amdgpu_device_program_register_sequence(adev,
211 golden_settings_tonga_a11,
212 ARRAY_SIZE(golden_settings_tonga_a11));
217 amdgpu_device_program_register_sequence(adev,
218 golden_settings_polaris11_a11,
219 ARRAY_SIZE(golden_settings_polaris11_a11));
222 amdgpu_device_program_register_sequence(adev,
223 golden_settings_polaris10_a11,
224 ARRAY_SIZE(golden_settings_polaris10_a11));
227 amdgpu_device_program_register_sequence(adev,
229 ARRAY_SIZE(cz_mgcg_cgcg_init));
230 amdgpu_device_program_register_sequence(adev,
231 cz_golden_settings_a11,
232 ARRAY_SIZE(cz_golden_settings_a11));
235 amdgpu_device_program_register_sequence(adev,
236 stoney_mgcg_cgcg_init,
237 ARRAY_SIZE(stoney_mgcg_cgcg_init));
238 amdgpu_device_program_register_sequence(adev,
239 stoney_golden_settings_a11,
240 ARRAY_SIZE(stoney_golden_settings_a11));
247 static void sdma_v3_0_free_microcode(struct amdgpu_device *adev)
250 for (i = 0; i < adev->sdma.num_instances; i++) {
251 release_firmware(adev->sdma.instance[i].fw);
252 adev->sdma.instance[i].fw = NULL;
257 * sdma_v3_0_init_microcode - load ucode images from disk
259 * @adev: amdgpu_device pointer
261 * Use the firmware interface to load the ucode images into
262 * the driver (not loaded into hw).
263 * Returns 0 on success, error on failure.
265 static int sdma_v3_0_init_microcode(struct amdgpu_device *adev)
267 const char *chip_name;
270 struct amdgpu_firmware_info *info = NULL;
271 const struct common_firmware_header *header = NULL;
272 const struct sdma_firmware_header_v1_0 *hdr;
276 switch (adev->asic_type) {
284 chip_name = "polaris10";
287 chip_name = "polaris11";
290 chip_name = "polaris12";
296 chip_name = "carrizo";
299 chip_name = "stoney";
304 for (i = 0; i < adev->sdma.num_instances; i++) {
306 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_sdma.bin", chip_name);
308 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_sdma1.bin", chip_name);
309 err = request_firmware(&adev->sdma.instance[i].fw, fw_name, adev->dev);
312 err = amdgpu_ucode_validate(adev->sdma.instance[i].fw);
315 hdr = (const struct sdma_firmware_header_v1_0 *)adev->sdma.instance[i].fw->data;
316 adev->sdma.instance[i].fw_version = le32_to_cpu(hdr->header.ucode_version);
317 adev->sdma.instance[i].feature_version = le32_to_cpu(hdr->ucode_feature_version);
318 if (adev->sdma.instance[i].feature_version >= 20)
319 adev->sdma.instance[i].burst_nop = true;
321 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_SDMA0 + i];
322 info->ucode_id = AMDGPU_UCODE_ID_SDMA0 + i;
323 info->fw = adev->sdma.instance[i].fw;
324 header = (const struct common_firmware_header *)info->fw->data;
325 adev->firmware.fw_size +=
326 ALIGN(le32_to_cpu(header->ucode_size_bytes), PAGE_SIZE);
331 pr_err("sdma_v3_0: Failed to load firmware \"%s\"\n", fw_name);
332 for (i = 0; i < adev->sdma.num_instances; i++) {
333 release_firmware(adev->sdma.instance[i].fw);
334 adev->sdma.instance[i].fw = NULL;
341 * sdma_v3_0_ring_get_rptr - get the current read pointer
343 * @ring: amdgpu ring pointer
345 * Get the current rptr from the hardware (VI+).
347 static uint64_t sdma_v3_0_ring_get_rptr(struct amdgpu_ring *ring)
349 /* XXX check if swapping is necessary on BE */
350 return ring->adev->wb.wb[ring->rptr_offs] >> 2;
354 * sdma_v3_0_ring_get_wptr - get the current write pointer
356 * @ring: amdgpu ring pointer
358 * Get the current wptr from the hardware (VI+).
360 static uint64_t sdma_v3_0_ring_get_wptr(struct amdgpu_ring *ring)
362 struct amdgpu_device *adev = ring->adev;
365 if (ring->use_doorbell || ring->use_pollmem) {
366 /* XXX check if swapping is necessary on BE */
367 wptr = ring->adev->wb.wb[ring->wptr_offs] >> 2;
369 wptr = RREG32(mmSDMA0_GFX_RB_WPTR + sdma_offsets[ring->me]) >> 2;
376 * sdma_v3_0_ring_set_wptr - commit the write pointer
378 * @ring: amdgpu ring pointer
380 * Write the wptr back to the hardware (VI+).
382 static void sdma_v3_0_ring_set_wptr(struct amdgpu_ring *ring)
384 struct amdgpu_device *adev = ring->adev;
386 if (ring->use_doorbell) {
387 u32 *wb = (u32 *)&adev->wb.wb[ring->wptr_offs];
388 /* XXX check if swapping is necessary on BE */
389 WRITE_ONCE(*wb, (lower_32_bits(ring->wptr) << 2));
390 WDOORBELL32(ring->doorbell_index, lower_32_bits(ring->wptr) << 2);
391 } else if (ring->use_pollmem) {
392 u32 *wb = (u32 *)&adev->wb.wb[ring->wptr_offs];
394 WRITE_ONCE(*wb, (lower_32_bits(ring->wptr) << 2));
396 WREG32(mmSDMA0_GFX_RB_WPTR + sdma_offsets[ring->me], lower_32_bits(ring->wptr) << 2);
400 static void sdma_v3_0_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count)
402 struct amdgpu_sdma_instance *sdma = amdgpu_get_sdma_instance(ring);
405 for (i = 0; i < count; i++)
406 if (sdma && sdma->burst_nop && (i == 0))
407 amdgpu_ring_write(ring, ring->funcs->nop |
408 SDMA_PKT_NOP_HEADER_COUNT(count - 1));
410 amdgpu_ring_write(ring, ring->funcs->nop);
414 * sdma_v3_0_ring_emit_ib - Schedule an IB on the DMA engine
416 * @ring: amdgpu ring pointer
417 * @ib: IB object to schedule
419 * Schedule an IB in the DMA ring (VI).
421 static void sdma_v3_0_ring_emit_ib(struct amdgpu_ring *ring,
422 struct amdgpu_ib *ib,
423 unsigned vmid, bool ctx_switch)
425 /* IB packet must end on a 8 DW boundary */
426 sdma_v3_0_ring_insert_nop(ring, (10 - (lower_32_bits(ring->wptr) & 7)) % 8);
428 amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_INDIRECT) |
429 SDMA_PKT_INDIRECT_HEADER_VMID(vmid & 0xf));
430 /* base must be 32 byte aligned */
431 amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr) & 0xffffffe0);
432 amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
433 amdgpu_ring_write(ring, ib->length_dw);
434 amdgpu_ring_write(ring, 0);
435 amdgpu_ring_write(ring, 0);
440 * sdma_v3_0_ring_emit_hdp_flush - emit an hdp flush on the DMA ring
442 * @ring: amdgpu ring pointer
444 * Emit an hdp flush packet on the requested DMA ring.
446 static void sdma_v3_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
448 u32 ref_and_mask = 0;
451 ref_and_mask = REG_SET_FIELD(ref_and_mask, GPU_HDP_FLUSH_DONE, SDMA0, 1);
453 ref_and_mask = REG_SET_FIELD(ref_and_mask, GPU_HDP_FLUSH_DONE, SDMA1, 1);
455 amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_POLL_REGMEM) |
456 SDMA_PKT_POLL_REGMEM_HEADER_HDP_FLUSH(1) |
457 SDMA_PKT_POLL_REGMEM_HEADER_FUNC(3)); /* == */
458 amdgpu_ring_write(ring, mmGPU_HDP_FLUSH_DONE << 2);
459 amdgpu_ring_write(ring, mmGPU_HDP_FLUSH_REQ << 2);
460 amdgpu_ring_write(ring, ref_and_mask); /* reference */
461 amdgpu_ring_write(ring, ref_and_mask); /* mask */
462 amdgpu_ring_write(ring, SDMA_PKT_POLL_REGMEM_DW5_RETRY_COUNT(0xfff) |
463 SDMA_PKT_POLL_REGMEM_DW5_INTERVAL(10)); /* retry count, poll interval */
467 * sdma_v3_0_ring_emit_fence - emit a fence on the DMA ring
469 * @ring: amdgpu ring pointer
470 * @fence: amdgpu fence object
472 * Add a DMA fence packet to the ring to write
473 * the fence seq number and DMA trap packet to generate
474 * an interrupt if needed (VI).
476 static void sdma_v3_0_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
479 bool write64bit = flags & AMDGPU_FENCE_FLAG_64BIT;
480 /* write the fence */
481 amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_FENCE));
482 amdgpu_ring_write(ring, lower_32_bits(addr));
483 amdgpu_ring_write(ring, upper_32_bits(addr));
484 amdgpu_ring_write(ring, lower_32_bits(seq));
486 /* optionally write high bits as well */
489 amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_FENCE));
490 amdgpu_ring_write(ring, lower_32_bits(addr));
491 amdgpu_ring_write(ring, upper_32_bits(addr));
492 amdgpu_ring_write(ring, upper_32_bits(seq));
495 /* generate an interrupt */
496 amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_TRAP));
497 amdgpu_ring_write(ring, SDMA_PKT_TRAP_INT_CONTEXT_INT_CONTEXT(0));
501 * sdma_v3_0_gfx_stop - stop the gfx async dma engines
503 * @adev: amdgpu_device pointer
505 * Stop the gfx async dma ring buffers (VI).
507 static void sdma_v3_0_gfx_stop(struct amdgpu_device *adev)
509 struct amdgpu_ring *sdma0 = &adev->sdma.instance[0].ring;
510 struct amdgpu_ring *sdma1 = &adev->sdma.instance[1].ring;
511 u32 rb_cntl, ib_cntl;
514 if ((adev->mman.buffer_funcs_ring == sdma0) ||
515 (adev->mman.buffer_funcs_ring == sdma1))
516 amdgpu_ttm_set_buffer_funcs_status(adev, false);
518 for (i = 0; i < adev->sdma.num_instances; i++) {
519 rb_cntl = RREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i]);
520 rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL, RB_ENABLE, 0);
521 WREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i], rb_cntl);
522 ib_cntl = RREG32(mmSDMA0_GFX_IB_CNTL + sdma_offsets[i]);
523 ib_cntl = REG_SET_FIELD(ib_cntl, SDMA0_GFX_IB_CNTL, IB_ENABLE, 0);
524 WREG32(mmSDMA0_GFX_IB_CNTL + sdma_offsets[i], ib_cntl);
526 sdma0->ready = false;
527 sdma1->ready = false;
531 * sdma_v3_0_rlc_stop - stop the compute async dma engines
533 * @adev: amdgpu_device pointer
535 * Stop the compute async dma queues (VI).
537 static void sdma_v3_0_rlc_stop(struct amdgpu_device *adev)
543 * sdma_v3_0_ctx_switch_enable - stop the async dma engines context switch
545 * @adev: amdgpu_device pointer
546 * @enable: enable/disable the DMA MEs context switch.
548 * Halt or unhalt the async dma engines context switch (VI).
550 static void sdma_v3_0_ctx_switch_enable(struct amdgpu_device *adev, bool enable)
552 u32 f32_cntl, phase_quantum = 0;
555 if (amdgpu_sdma_phase_quantum) {
556 unsigned value = amdgpu_sdma_phase_quantum;
559 while (value > (SDMA0_PHASE0_QUANTUM__VALUE_MASK >>
560 SDMA0_PHASE0_QUANTUM__VALUE__SHIFT)) {
561 value = (value + 1) >> 1;
564 if (unit > (SDMA0_PHASE0_QUANTUM__UNIT_MASK >>
565 SDMA0_PHASE0_QUANTUM__UNIT__SHIFT)) {
566 value = (SDMA0_PHASE0_QUANTUM__VALUE_MASK >>
567 SDMA0_PHASE0_QUANTUM__VALUE__SHIFT);
568 unit = (SDMA0_PHASE0_QUANTUM__UNIT_MASK >>
569 SDMA0_PHASE0_QUANTUM__UNIT__SHIFT);
571 "clamping sdma_phase_quantum to %uK clock cycles\n",
575 value << SDMA0_PHASE0_QUANTUM__VALUE__SHIFT |
576 unit << SDMA0_PHASE0_QUANTUM__UNIT__SHIFT;
579 for (i = 0; i < adev->sdma.num_instances; i++) {
580 f32_cntl = RREG32(mmSDMA0_CNTL + sdma_offsets[i]);
582 f32_cntl = REG_SET_FIELD(f32_cntl, SDMA0_CNTL,
583 AUTO_CTXSW_ENABLE, 1);
584 f32_cntl = REG_SET_FIELD(f32_cntl, SDMA0_CNTL,
586 if (amdgpu_sdma_phase_quantum) {
587 WREG32(mmSDMA0_PHASE0_QUANTUM + sdma_offsets[i],
589 WREG32(mmSDMA0_PHASE1_QUANTUM + sdma_offsets[i],
593 f32_cntl = REG_SET_FIELD(f32_cntl, SDMA0_CNTL,
594 AUTO_CTXSW_ENABLE, 0);
595 f32_cntl = REG_SET_FIELD(f32_cntl, SDMA0_CNTL,
599 WREG32(mmSDMA0_CNTL + sdma_offsets[i], f32_cntl);
604 * sdma_v3_0_enable - stop the async dma engines
606 * @adev: amdgpu_device pointer
607 * @enable: enable/disable the DMA MEs.
609 * Halt or unhalt the async dma engines (VI).
611 static void sdma_v3_0_enable(struct amdgpu_device *adev, bool enable)
617 sdma_v3_0_gfx_stop(adev);
618 sdma_v3_0_rlc_stop(adev);
621 for (i = 0; i < adev->sdma.num_instances; i++) {
622 f32_cntl = RREG32(mmSDMA0_F32_CNTL + sdma_offsets[i]);
624 f32_cntl = REG_SET_FIELD(f32_cntl, SDMA0_F32_CNTL, HALT, 0);
626 f32_cntl = REG_SET_FIELD(f32_cntl, SDMA0_F32_CNTL, HALT, 1);
627 WREG32(mmSDMA0_F32_CNTL + sdma_offsets[i], f32_cntl);
632 * sdma_v3_0_gfx_resume - setup and start the async dma engines
634 * @adev: amdgpu_device pointer
636 * Set up the gfx DMA ring buffers and enable them (VI).
637 * Returns 0 for success, error for failure.
639 static int sdma_v3_0_gfx_resume(struct amdgpu_device *adev)
641 struct amdgpu_ring *ring;
642 u32 rb_cntl, ib_cntl, wptr_poll_cntl;
649 for (i = 0; i < adev->sdma.num_instances; i++) {
650 ring = &adev->sdma.instance[i].ring;
651 amdgpu_ring_clear_ring(ring);
652 wb_offset = (ring->rptr_offs * 4);
654 mutex_lock(&adev->srbm_mutex);
655 for (j = 0; j < 16; j++) {
656 vi_srbm_select(adev, 0, 0, 0, j);
658 WREG32(mmSDMA0_GFX_VIRTUAL_ADDR + sdma_offsets[i], 0);
659 WREG32(mmSDMA0_GFX_APE1_CNTL + sdma_offsets[i], 0);
661 vi_srbm_select(adev, 0, 0, 0, 0);
662 mutex_unlock(&adev->srbm_mutex);
664 WREG32(mmSDMA0_TILING_CONFIG + sdma_offsets[i],
665 adev->gfx.config.gb_addr_config & 0x70);
667 WREG32(mmSDMA0_SEM_WAIT_FAIL_TIMER_CNTL + sdma_offsets[i], 0);
669 /* Set ring buffer size in dwords */
670 rb_bufsz = order_base_2(ring->ring_size / 4);
671 rb_cntl = RREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i]);
672 rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL, RB_SIZE, rb_bufsz);
674 rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL, RB_SWAP_ENABLE, 1);
675 rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL,
676 RPTR_WRITEBACK_SWAP_ENABLE, 1);
678 WREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i], rb_cntl);
680 /* Initialize the ring buffer's read and write pointers */
682 WREG32(mmSDMA0_GFX_RB_RPTR + sdma_offsets[i], 0);
683 sdma_v3_0_ring_set_wptr(ring);
684 WREG32(mmSDMA0_GFX_IB_RPTR + sdma_offsets[i], 0);
685 WREG32(mmSDMA0_GFX_IB_OFFSET + sdma_offsets[i], 0);
687 /* set the wb address whether it's enabled or not */
688 WREG32(mmSDMA0_GFX_RB_RPTR_ADDR_HI + sdma_offsets[i],
689 upper_32_bits(adev->wb.gpu_addr + wb_offset) & 0xFFFFFFFF);
690 WREG32(mmSDMA0_GFX_RB_RPTR_ADDR_LO + sdma_offsets[i],
691 lower_32_bits(adev->wb.gpu_addr + wb_offset) & 0xFFFFFFFC);
693 rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL, RPTR_WRITEBACK_ENABLE, 1);
695 WREG32(mmSDMA0_GFX_RB_BASE + sdma_offsets[i], ring->gpu_addr >> 8);
696 WREG32(mmSDMA0_GFX_RB_BASE_HI + sdma_offsets[i], ring->gpu_addr >> 40);
698 doorbell = RREG32(mmSDMA0_GFX_DOORBELL + sdma_offsets[i]);
700 if (ring->use_doorbell) {
701 doorbell = REG_SET_FIELD(doorbell, SDMA0_GFX_DOORBELL,
702 OFFSET, ring->doorbell_index);
703 doorbell = REG_SET_FIELD(doorbell, SDMA0_GFX_DOORBELL, ENABLE, 1);
705 doorbell = REG_SET_FIELD(doorbell, SDMA0_GFX_DOORBELL, ENABLE, 0);
707 WREG32(mmSDMA0_GFX_DOORBELL + sdma_offsets[i], doorbell);
709 /* setup the wptr shadow polling */
710 wptr_gpu_addr = adev->wb.gpu_addr + (ring->wptr_offs * 4);
712 WREG32(mmSDMA0_GFX_RB_WPTR_POLL_ADDR_LO + sdma_offsets[i],
713 lower_32_bits(wptr_gpu_addr));
714 WREG32(mmSDMA0_GFX_RB_WPTR_POLL_ADDR_HI + sdma_offsets[i],
715 upper_32_bits(wptr_gpu_addr));
716 wptr_poll_cntl = RREG32(mmSDMA0_GFX_RB_WPTR_POLL_CNTL + sdma_offsets[i]);
717 if (ring->use_pollmem) {
718 /*wptr polling is not enogh fast, directly clean the wptr register */
719 WREG32(mmSDMA0_GFX_RB_WPTR + sdma_offsets[i], 0);
720 wptr_poll_cntl = REG_SET_FIELD(wptr_poll_cntl,
721 SDMA0_GFX_RB_WPTR_POLL_CNTL,
724 wptr_poll_cntl = REG_SET_FIELD(wptr_poll_cntl,
725 SDMA0_GFX_RB_WPTR_POLL_CNTL,
728 WREG32(mmSDMA0_GFX_RB_WPTR_POLL_CNTL + sdma_offsets[i], wptr_poll_cntl);
731 rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL, RB_ENABLE, 1);
732 WREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i], rb_cntl);
734 ib_cntl = RREG32(mmSDMA0_GFX_IB_CNTL + sdma_offsets[i]);
735 ib_cntl = REG_SET_FIELD(ib_cntl, SDMA0_GFX_IB_CNTL, IB_ENABLE, 1);
737 ib_cntl = REG_SET_FIELD(ib_cntl, SDMA0_GFX_IB_CNTL, IB_SWAP_ENABLE, 1);
740 WREG32(mmSDMA0_GFX_IB_CNTL + sdma_offsets[i], ib_cntl);
746 sdma_v3_0_enable(adev, true);
747 /* enable sdma ring preemption */
748 sdma_v3_0_ctx_switch_enable(adev, true);
750 for (i = 0; i < adev->sdma.num_instances; i++) {
751 ring = &adev->sdma.instance[i].ring;
752 r = amdgpu_ring_test_ring(ring);
758 if (adev->mman.buffer_funcs_ring == ring)
759 amdgpu_ttm_set_buffer_funcs_status(adev, true);
766 * sdma_v3_0_rlc_resume - setup and start the async dma engines
768 * @adev: amdgpu_device pointer
770 * Set up the compute DMA queues and enable them (VI).
771 * Returns 0 for success, error for failure.
773 static int sdma_v3_0_rlc_resume(struct amdgpu_device *adev)
780 * sdma_v3_0_start - setup and start the async dma engines
782 * @adev: amdgpu_device pointer
784 * Set up the DMA engines and enable them (VI).
785 * Returns 0 for success, error for failure.
787 static int sdma_v3_0_start(struct amdgpu_device *adev)
791 /* disable sdma engine before programing it */
792 sdma_v3_0_ctx_switch_enable(adev, false);
793 sdma_v3_0_enable(adev, false);
795 /* start the gfx rings and rlc compute queues */
796 r = sdma_v3_0_gfx_resume(adev);
799 r = sdma_v3_0_rlc_resume(adev);
807 * sdma_v3_0_ring_test_ring - simple async dma engine test
809 * @ring: amdgpu_ring structure holding ring information
811 * Test the DMA engine by writing using it to write an
812 * value to memory. (VI).
813 * Returns 0 for success, error for failure.
815 static int sdma_v3_0_ring_test_ring(struct amdgpu_ring *ring)
817 struct amdgpu_device *adev = ring->adev;
824 r = amdgpu_device_wb_get(adev, &index);
826 dev_err(adev->dev, "(%d) failed to allocate wb slot\n", r);
830 gpu_addr = adev->wb.gpu_addr + (index * 4);
832 adev->wb.wb[index] = cpu_to_le32(tmp);
834 r = amdgpu_ring_alloc(ring, 5);
836 DRM_ERROR("amdgpu: dma failed to lock ring %d (%d).\n", ring->idx, r);
837 amdgpu_device_wb_free(adev, index);
841 amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_WRITE) |
842 SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR));
843 amdgpu_ring_write(ring, lower_32_bits(gpu_addr));
844 amdgpu_ring_write(ring, upper_32_bits(gpu_addr));
845 amdgpu_ring_write(ring, SDMA_PKT_WRITE_UNTILED_DW_3_COUNT(1));
846 amdgpu_ring_write(ring, 0xDEADBEEF);
847 amdgpu_ring_commit(ring);
849 for (i = 0; i < adev->usec_timeout; i++) {
850 tmp = le32_to_cpu(adev->wb.wb[index]);
851 if (tmp == 0xDEADBEEF)
856 if (i < adev->usec_timeout) {
857 DRM_DEBUG("ring test on %d succeeded in %d usecs\n", ring->idx, i);
859 DRM_ERROR("amdgpu: ring %d test failed (0x%08X)\n",
863 amdgpu_device_wb_free(adev, index);
869 * sdma_v3_0_ring_test_ib - test an IB on the DMA engine
871 * @ring: amdgpu_ring structure holding ring information
873 * Test a simple IB in the DMA ring (VI).
874 * Returns 0 on success, error on failure.
876 static int sdma_v3_0_ring_test_ib(struct amdgpu_ring *ring, long timeout)
878 struct amdgpu_device *adev = ring->adev;
880 struct dma_fence *f = NULL;
886 r = amdgpu_device_wb_get(adev, &index);
888 dev_err(adev->dev, "(%ld) failed to allocate wb slot\n", r);
892 gpu_addr = adev->wb.gpu_addr + (index * 4);
894 adev->wb.wb[index] = cpu_to_le32(tmp);
895 memset(&ib, 0, sizeof(ib));
896 r = amdgpu_ib_get(adev, NULL, 256, &ib);
898 DRM_ERROR("amdgpu: failed to get ib (%ld).\n", r);
902 ib.ptr[0] = SDMA_PKT_HEADER_OP(SDMA_OP_WRITE) |
903 SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR);
904 ib.ptr[1] = lower_32_bits(gpu_addr);
905 ib.ptr[2] = upper_32_bits(gpu_addr);
906 ib.ptr[3] = SDMA_PKT_WRITE_UNTILED_DW_3_COUNT(1);
907 ib.ptr[4] = 0xDEADBEEF;
908 ib.ptr[5] = SDMA_PKT_NOP_HEADER_OP(SDMA_OP_NOP);
909 ib.ptr[6] = SDMA_PKT_NOP_HEADER_OP(SDMA_OP_NOP);
910 ib.ptr[7] = SDMA_PKT_NOP_HEADER_OP(SDMA_OP_NOP);
913 r = amdgpu_ib_schedule(ring, 1, &ib, NULL, &f);
917 r = dma_fence_wait_timeout(f, false, timeout);
919 DRM_ERROR("amdgpu: IB test timed out\n");
923 DRM_ERROR("amdgpu: fence wait failed (%ld).\n", r);
926 tmp = le32_to_cpu(adev->wb.wb[index]);
927 if (tmp == 0xDEADBEEF) {
928 DRM_DEBUG("ib test on ring %d succeeded\n", ring->idx);
931 DRM_ERROR("amdgpu: ib test failed (0x%08X)\n", tmp);
935 amdgpu_ib_free(adev, &ib, NULL);
938 amdgpu_device_wb_free(adev, index);
943 * sdma_v3_0_vm_copy_pte - update PTEs by copying them from the GART
945 * @ib: indirect buffer to fill with commands
946 * @pe: addr of the page entry
947 * @src: src addr to copy from
948 * @count: number of page entries to update
950 * Update PTEs by copying them from the GART using sDMA (CIK).
952 static void sdma_v3_0_vm_copy_pte(struct amdgpu_ib *ib,
953 uint64_t pe, uint64_t src,
956 unsigned bytes = count * 8;
958 ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
959 SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR);
960 ib->ptr[ib->length_dw++] = bytes;
961 ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
962 ib->ptr[ib->length_dw++] = lower_32_bits(src);
963 ib->ptr[ib->length_dw++] = upper_32_bits(src);
964 ib->ptr[ib->length_dw++] = lower_32_bits(pe);
965 ib->ptr[ib->length_dw++] = upper_32_bits(pe);
969 * sdma_v3_0_vm_write_pte - update PTEs by writing them manually
971 * @ib: indirect buffer to fill with commands
972 * @pe: addr of the page entry
973 * @value: dst addr to write into pe
974 * @count: number of page entries to update
975 * @incr: increase next addr by incr bytes
977 * Update PTEs by writing them manually using sDMA (CIK).
979 static void sdma_v3_0_vm_write_pte(struct amdgpu_ib *ib, uint64_t pe,
980 uint64_t value, unsigned count,
983 unsigned ndw = count * 2;
985 ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_WRITE) |
986 SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR);
987 ib->ptr[ib->length_dw++] = lower_32_bits(pe);
988 ib->ptr[ib->length_dw++] = upper_32_bits(pe);
989 ib->ptr[ib->length_dw++] = ndw;
990 for (; ndw > 0; ndw -= 2) {
991 ib->ptr[ib->length_dw++] = lower_32_bits(value);
992 ib->ptr[ib->length_dw++] = upper_32_bits(value);
998 * sdma_v3_0_vm_set_pte_pde - update the page tables using sDMA
1000 * @ib: indirect buffer to fill with commands
1001 * @pe: addr of the page entry
1002 * @addr: dst addr to write into pe
1003 * @count: number of page entries to update
1004 * @incr: increase next addr by incr bytes
1005 * @flags: access flags
1007 * Update the page tables using sDMA (CIK).
1009 static void sdma_v3_0_vm_set_pte_pde(struct amdgpu_ib *ib, uint64_t pe,
1010 uint64_t addr, unsigned count,
1011 uint32_t incr, uint64_t flags)
1013 /* for physically contiguous pages (vram) */
1014 ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_GEN_PTEPDE);
1015 ib->ptr[ib->length_dw++] = lower_32_bits(pe); /* dst addr */
1016 ib->ptr[ib->length_dw++] = upper_32_bits(pe);
1017 ib->ptr[ib->length_dw++] = lower_32_bits(flags); /* mask */
1018 ib->ptr[ib->length_dw++] = upper_32_bits(flags);
1019 ib->ptr[ib->length_dw++] = lower_32_bits(addr); /* value */
1020 ib->ptr[ib->length_dw++] = upper_32_bits(addr);
1021 ib->ptr[ib->length_dw++] = incr; /* increment size */
1022 ib->ptr[ib->length_dw++] = 0;
1023 ib->ptr[ib->length_dw++] = count; /* number of entries */
1027 * sdma_v3_0_ring_pad_ib - pad the IB to the required number of dw
1029 * @ib: indirect buffer to fill with padding
1032 static void sdma_v3_0_ring_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib)
1034 struct amdgpu_sdma_instance *sdma = amdgpu_get_sdma_instance(ring);
1038 pad_count = (8 - (ib->length_dw & 0x7)) % 8;
1039 for (i = 0; i < pad_count; i++)
1040 if (sdma && sdma->burst_nop && (i == 0))
1041 ib->ptr[ib->length_dw++] =
1042 SDMA_PKT_HEADER_OP(SDMA_OP_NOP) |
1043 SDMA_PKT_NOP_HEADER_COUNT(pad_count - 1);
1045 ib->ptr[ib->length_dw++] =
1046 SDMA_PKT_HEADER_OP(SDMA_OP_NOP);
1050 * sdma_v3_0_ring_emit_pipeline_sync - sync the pipeline
1052 * @ring: amdgpu_ring pointer
1054 * Make sure all previous operations are completed (CIK).
1056 static void sdma_v3_0_ring_emit_pipeline_sync(struct amdgpu_ring *ring)
1058 uint32_t seq = ring->fence_drv.sync_seq;
1059 uint64_t addr = ring->fence_drv.gpu_addr;
1062 amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_POLL_REGMEM) |
1063 SDMA_PKT_POLL_REGMEM_HEADER_HDP_FLUSH(0) |
1064 SDMA_PKT_POLL_REGMEM_HEADER_FUNC(3) | /* equal */
1065 SDMA_PKT_POLL_REGMEM_HEADER_MEM_POLL(1));
1066 amdgpu_ring_write(ring, addr & 0xfffffffc);
1067 amdgpu_ring_write(ring, upper_32_bits(addr) & 0xffffffff);
1068 amdgpu_ring_write(ring, seq); /* reference */
1069 amdgpu_ring_write(ring, 0xffffffff); /* mask */
1070 amdgpu_ring_write(ring, SDMA_PKT_POLL_REGMEM_DW5_RETRY_COUNT(0xfff) |
1071 SDMA_PKT_POLL_REGMEM_DW5_INTERVAL(4)); /* retry count, poll interval */
1075 * sdma_v3_0_ring_emit_vm_flush - cik vm flush using sDMA
1077 * @ring: amdgpu_ring pointer
1078 * @vm: amdgpu_vm pointer
1080 * Update the page table base and flush the VM TLB
1083 static void sdma_v3_0_ring_emit_vm_flush(struct amdgpu_ring *ring,
1084 unsigned vmid, uint64_t pd_addr)
1086 amdgpu_gmc_emit_flush_gpu_tlb(ring, vmid, pd_addr);
1088 /* wait for flush */
1089 amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_POLL_REGMEM) |
1090 SDMA_PKT_POLL_REGMEM_HEADER_HDP_FLUSH(0) |
1091 SDMA_PKT_POLL_REGMEM_HEADER_FUNC(0)); /* always */
1092 amdgpu_ring_write(ring, mmVM_INVALIDATE_REQUEST << 2);
1093 amdgpu_ring_write(ring, 0);
1094 amdgpu_ring_write(ring, 0); /* reference */
1095 amdgpu_ring_write(ring, 0); /* mask */
1096 amdgpu_ring_write(ring, SDMA_PKT_POLL_REGMEM_DW5_RETRY_COUNT(0xfff) |
1097 SDMA_PKT_POLL_REGMEM_DW5_INTERVAL(10)); /* retry count, poll interval */
1100 static void sdma_v3_0_ring_emit_wreg(struct amdgpu_ring *ring,
1101 uint32_t reg, uint32_t val)
1103 amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_SRBM_WRITE) |
1104 SDMA_PKT_SRBM_WRITE_HEADER_BYTE_EN(0xf));
1105 amdgpu_ring_write(ring, reg);
1106 amdgpu_ring_write(ring, val);
1109 static int sdma_v3_0_early_init(void *handle)
1111 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1113 switch (adev->asic_type) {
1115 adev->sdma.num_instances = 1;
1118 adev->sdma.num_instances = SDMA_MAX_INSTANCE;
1122 sdma_v3_0_set_ring_funcs(adev);
1123 sdma_v3_0_set_buffer_funcs(adev);
1124 sdma_v3_0_set_vm_pte_funcs(adev);
1125 sdma_v3_0_set_irq_funcs(adev);
1130 static int sdma_v3_0_sw_init(void *handle)
1132 struct amdgpu_ring *ring;
1134 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1136 /* SDMA trap event */
1137 r = amdgpu_irq_add_id(adev, AMDGPU_IRQ_CLIENTID_LEGACY, VISLANDS30_IV_SRCID_SDMA_TRAP,
1138 &adev->sdma.trap_irq);
1142 /* SDMA Privileged inst */
1143 r = amdgpu_irq_add_id(adev, AMDGPU_IRQ_CLIENTID_LEGACY, 241,
1144 &adev->sdma.illegal_inst_irq);
1148 /* SDMA Privileged inst */
1149 r = amdgpu_irq_add_id(adev, AMDGPU_IRQ_CLIENTID_LEGACY, VISLANDS30_IV_SRCID_SDMA_SRBM_WRITE,
1150 &adev->sdma.illegal_inst_irq);
1154 r = sdma_v3_0_init_microcode(adev);
1156 DRM_ERROR("Failed to load sdma firmware!\n");
1160 for (i = 0; i < adev->sdma.num_instances; i++) {
1161 ring = &adev->sdma.instance[i].ring;
1162 ring->ring_obj = NULL;
1163 if (!amdgpu_sriov_vf(adev)) {
1164 ring->use_doorbell = true;
1165 ring->doorbell_index = (i == 0) ?
1166 AMDGPU_DOORBELL_sDMA_ENGINE0 : AMDGPU_DOORBELL_sDMA_ENGINE1;
1168 ring->use_pollmem = true;
1171 sprintf(ring->name, "sdma%d", i);
1172 r = amdgpu_ring_init(adev, ring, 1024,
1173 &adev->sdma.trap_irq,
1175 AMDGPU_SDMA_IRQ_TRAP0 :
1176 AMDGPU_SDMA_IRQ_TRAP1);
1184 static int sdma_v3_0_sw_fini(void *handle)
1186 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1189 for (i = 0; i < adev->sdma.num_instances; i++)
1190 amdgpu_ring_fini(&adev->sdma.instance[i].ring);
1192 sdma_v3_0_free_microcode(adev);
1196 static int sdma_v3_0_hw_init(void *handle)
1199 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1201 sdma_v3_0_init_golden_registers(adev);
1203 r = sdma_v3_0_start(adev);
1210 static int sdma_v3_0_hw_fini(void *handle)
1212 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1214 sdma_v3_0_ctx_switch_enable(adev, false);
1215 sdma_v3_0_enable(adev, false);
1220 static int sdma_v3_0_suspend(void *handle)
1222 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1224 return sdma_v3_0_hw_fini(adev);
1227 static int sdma_v3_0_resume(void *handle)
1229 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1231 return sdma_v3_0_hw_init(adev);
1234 static bool sdma_v3_0_is_idle(void *handle)
1236 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1237 u32 tmp = RREG32(mmSRBM_STATUS2);
1239 if (tmp & (SRBM_STATUS2__SDMA_BUSY_MASK |
1240 SRBM_STATUS2__SDMA1_BUSY_MASK))
1246 static int sdma_v3_0_wait_for_idle(void *handle)
1250 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1252 for (i = 0; i < adev->usec_timeout; i++) {
1253 tmp = RREG32(mmSRBM_STATUS2) & (SRBM_STATUS2__SDMA_BUSY_MASK |
1254 SRBM_STATUS2__SDMA1_BUSY_MASK);
1263 static bool sdma_v3_0_check_soft_reset(void *handle)
1265 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1266 u32 srbm_soft_reset = 0;
1267 u32 tmp = RREG32(mmSRBM_STATUS2);
1269 if ((tmp & SRBM_STATUS2__SDMA_BUSY_MASK) ||
1270 (tmp & SRBM_STATUS2__SDMA1_BUSY_MASK)) {
1271 srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_SDMA_MASK;
1272 srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_SDMA1_MASK;
1275 if (srbm_soft_reset) {
1276 adev->sdma.srbm_soft_reset = srbm_soft_reset;
1279 adev->sdma.srbm_soft_reset = 0;
1284 static int sdma_v3_0_pre_soft_reset(void *handle)
1286 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1287 u32 srbm_soft_reset = 0;
1289 if (!adev->sdma.srbm_soft_reset)
1292 srbm_soft_reset = adev->sdma.srbm_soft_reset;
1294 if (REG_GET_FIELD(srbm_soft_reset, SRBM_SOFT_RESET, SOFT_RESET_SDMA) ||
1295 REG_GET_FIELD(srbm_soft_reset, SRBM_SOFT_RESET, SOFT_RESET_SDMA1)) {
1296 sdma_v3_0_ctx_switch_enable(adev, false);
1297 sdma_v3_0_enable(adev, false);
1303 static int sdma_v3_0_post_soft_reset(void *handle)
1305 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1306 u32 srbm_soft_reset = 0;
1308 if (!adev->sdma.srbm_soft_reset)
1311 srbm_soft_reset = adev->sdma.srbm_soft_reset;
1313 if (REG_GET_FIELD(srbm_soft_reset, SRBM_SOFT_RESET, SOFT_RESET_SDMA) ||
1314 REG_GET_FIELD(srbm_soft_reset, SRBM_SOFT_RESET, SOFT_RESET_SDMA1)) {
1315 sdma_v3_0_gfx_resume(adev);
1316 sdma_v3_0_rlc_resume(adev);
1322 static int sdma_v3_0_soft_reset(void *handle)
1324 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1325 u32 srbm_soft_reset = 0;
1328 if (!adev->sdma.srbm_soft_reset)
1331 srbm_soft_reset = adev->sdma.srbm_soft_reset;
1333 if (srbm_soft_reset) {
1334 tmp = RREG32(mmSRBM_SOFT_RESET);
1335 tmp |= srbm_soft_reset;
1336 dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
1337 WREG32(mmSRBM_SOFT_RESET, tmp);
1338 tmp = RREG32(mmSRBM_SOFT_RESET);
1342 tmp &= ~srbm_soft_reset;
1343 WREG32(mmSRBM_SOFT_RESET, tmp);
1344 tmp = RREG32(mmSRBM_SOFT_RESET);
1346 /* Wait a little for things to settle down */
1353 static int sdma_v3_0_set_trap_irq_state(struct amdgpu_device *adev,
1354 struct amdgpu_irq_src *source,
1356 enum amdgpu_interrupt_state state)
1361 case AMDGPU_SDMA_IRQ_TRAP0:
1363 case AMDGPU_IRQ_STATE_DISABLE:
1364 sdma_cntl = RREG32(mmSDMA0_CNTL + SDMA0_REGISTER_OFFSET);
1365 sdma_cntl = REG_SET_FIELD(sdma_cntl, SDMA0_CNTL, TRAP_ENABLE, 0);
1366 WREG32(mmSDMA0_CNTL + SDMA0_REGISTER_OFFSET, sdma_cntl);
1368 case AMDGPU_IRQ_STATE_ENABLE:
1369 sdma_cntl = RREG32(mmSDMA0_CNTL + SDMA0_REGISTER_OFFSET);
1370 sdma_cntl = REG_SET_FIELD(sdma_cntl, SDMA0_CNTL, TRAP_ENABLE, 1);
1371 WREG32(mmSDMA0_CNTL + SDMA0_REGISTER_OFFSET, sdma_cntl);
1377 case AMDGPU_SDMA_IRQ_TRAP1:
1379 case AMDGPU_IRQ_STATE_DISABLE:
1380 sdma_cntl = RREG32(mmSDMA0_CNTL + SDMA1_REGISTER_OFFSET);
1381 sdma_cntl = REG_SET_FIELD(sdma_cntl, SDMA0_CNTL, TRAP_ENABLE, 0);
1382 WREG32(mmSDMA0_CNTL + SDMA1_REGISTER_OFFSET, sdma_cntl);
1384 case AMDGPU_IRQ_STATE_ENABLE:
1385 sdma_cntl = RREG32(mmSDMA0_CNTL + SDMA1_REGISTER_OFFSET);
1386 sdma_cntl = REG_SET_FIELD(sdma_cntl, SDMA0_CNTL, TRAP_ENABLE, 1);
1387 WREG32(mmSDMA0_CNTL + SDMA1_REGISTER_OFFSET, sdma_cntl);
1399 static int sdma_v3_0_process_trap_irq(struct amdgpu_device *adev,
1400 struct amdgpu_irq_src *source,
1401 struct amdgpu_iv_entry *entry)
1403 u8 instance_id, queue_id;
1405 instance_id = (entry->ring_id & 0x3) >> 0;
1406 queue_id = (entry->ring_id & 0xc) >> 2;
1407 DRM_DEBUG("IH: SDMA trap\n");
1408 switch (instance_id) {
1412 amdgpu_fence_process(&adev->sdma.instance[0].ring);
1425 amdgpu_fence_process(&adev->sdma.instance[1].ring);
1439 static int sdma_v3_0_process_illegal_inst_irq(struct amdgpu_device *adev,
1440 struct amdgpu_irq_src *source,
1441 struct amdgpu_iv_entry *entry)
1443 u8 instance_id, queue_id;
1445 DRM_ERROR("Illegal instruction in SDMA command stream\n");
1446 instance_id = (entry->ring_id & 0x3) >> 0;
1447 queue_id = (entry->ring_id & 0xc) >> 2;
1449 if (instance_id <= 1 && queue_id == 0)
1450 drm_sched_fault(&adev->sdma.instance[instance_id].ring.sched);
1454 static void sdma_v3_0_update_sdma_medium_grain_clock_gating(
1455 struct amdgpu_device *adev,
1458 uint32_t temp, data;
1461 if (enable && (adev->cg_flags & AMD_CG_SUPPORT_SDMA_MGCG)) {
1462 for (i = 0; i < adev->sdma.num_instances; i++) {
1463 temp = data = RREG32(mmSDMA0_CLK_CTRL + sdma_offsets[i]);
1464 data &= ~(SDMA0_CLK_CTRL__SOFT_OVERRIDE7_MASK |
1465 SDMA0_CLK_CTRL__SOFT_OVERRIDE6_MASK |
1466 SDMA0_CLK_CTRL__SOFT_OVERRIDE5_MASK |
1467 SDMA0_CLK_CTRL__SOFT_OVERRIDE4_MASK |
1468 SDMA0_CLK_CTRL__SOFT_OVERRIDE3_MASK |
1469 SDMA0_CLK_CTRL__SOFT_OVERRIDE2_MASK |
1470 SDMA0_CLK_CTRL__SOFT_OVERRIDE1_MASK |
1471 SDMA0_CLK_CTRL__SOFT_OVERRIDE0_MASK);
1473 WREG32(mmSDMA0_CLK_CTRL + sdma_offsets[i], data);
1476 for (i = 0; i < adev->sdma.num_instances; i++) {
1477 temp = data = RREG32(mmSDMA0_CLK_CTRL + sdma_offsets[i]);
1478 data |= SDMA0_CLK_CTRL__SOFT_OVERRIDE7_MASK |
1479 SDMA0_CLK_CTRL__SOFT_OVERRIDE6_MASK |
1480 SDMA0_CLK_CTRL__SOFT_OVERRIDE5_MASK |
1481 SDMA0_CLK_CTRL__SOFT_OVERRIDE4_MASK |
1482 SDMA0_CLK_CTRL__SOFT_OVERRIDE3_MASK |
1483 SDMA0_CLK_CTRL__SOFT_OVERRIDE2_MASK |
1484 SDMA0_CLK_CTRL__SOFT_OVERRIDE1_MASK |
1485 SDMA0_CLK_CTRL__SOFT_OVERRIDE0_MASK;
1488 WREG32(mmSDMA0_CLK_CTRL + sdma_offsets[i], data);
1493 static void sdma_v3_0_update_sdma_medium_grain_light_sleep(
1494 struct amdgpu_device *adev,
1497 uint32_t temp, data;
1500 if (enable && (adev->cg_flags & AMD_CG_SUPPORT_SDMA_LS)) {
1501 for (i = 0; i < adev->sdma.num_instances; i++) {
1502 temp = data = RREG32(mmSDMA0_POWER_CNTL + sdma_offsets[i]);
1503 data |= SDMA0_POWER_CNTL__MEM_POWER_OVERRIDE_MASK;
1506 WREG32(mmSDMA0_POWER_CNTL + sdma_offsets[i], data);
1509 for (i = 0; i < adev->sdma.num_instances; i++) {
1510 temp = data = RREG32(mmSDMA0_POWER_CNTL + sdma_offsets[i]);
1511 data &= ~SDMA0_POWER_CNTL__MEM_POWER_OVERRIDE_MASK;
1514 WREG32(mmSDMA0_POWER_CNTL + sdma_offsets[i], data);
1519 static int sdma_v3_0_set_clockgating_state(void *handle,
1520 enum amd_clockgating_state state)
1522 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1524 if (amdgpu_sriov_vf(adev))
1527 switch (adev->asic_type) {
1531 sdma_v3_0_update_sdma_medium_grain_clock_gating(adev,
1532 state == AMD_CG_STATE_GATE);
1533 sdma_v3_0_update_sdma_medium_grain_light_sleep(adev,
1534 state == AMD_CG_STATE_GATE);
1542 static int sdma_v3_0_set_powergating_state(void *handle,
1543 enum amd_powergating_state state)
1548 static void sdma_v3_0_get_clockgating_state(void *handle, u32 *flags)
1550 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1553 if (amdgpu_sriov_vf(adev))
1556 /* AMD_CG_SUPPORT_SDMA_MGCG */
1557 data = RREG32(mmSDMA0_CLK_CTRL + sdma_offsets[0]);
1558 if (!(data & SDMA0_CLK_CTRL__SOFT_OVERRIDE0_MASK))
1559 *flags |= AMD_CG_SUPPORT_SDMA_MGCG;
1561 /* AMD_CG_SUPPORT_SDMA_LS */
1562 data = RREG32(mmSDMA0_POWER_CNTL + sdma_offsets[0]);
1563 if (data & SDMA0_POWER_CNTL__MEM_POWER_OVERRIDE_MASK)
1564 *flags |= AMD_CG_SUPPORT_SDMA_LS;
1567 static const struct amd_ip_funcs sdma_v3_0_ip_funcs = {
1568 .name = "sdma_v3_0",
1569 .early_init = sdma_v3_0_early_init,
1571 .sw_init = sdma_v3_0_sw_init,
1572 .sw_fini = sdma_v3_0_sw_fini,
1573 .hw_init = sdma_v3_0_hw_init,
1574 .hw_fini = sdma_v3_0_hw_fini,
1575 .suspend = sdma_v3_0_suspend,
1576 .resume = sdma_v3_0_resume,
1577 .is_idle = sdma_v3_0_is_idle,
1578 .wait_for_idle = sdma_v3_0_wait_for_idle,
1579 .check_soft_reset = sdma_v3_0_check_soft_reset,
1580 .pre_soft_reset = sdma_v3_0_pre_soft_reset,
1581 .post_soft_reset = sdma_v3_0_post_soft_reset,
1582 .soft_reset = sdma_v3_0_soft_reset,
1583 .set_clockgating_state = sdma_v3_0_set_clockgating_state,
1584 .set_powergating_state = sdma_v3_0_set_powergating_state,
1585 .get_clockgating_state = sdma_v3_0_get_clockgating_state,
1588 static const struct amdgpu_ring_funcs sdma_v3_0_ring_funcs = {
1589 .type = AMDGPU_RING_TYPE_SDMA,
1591 .nop = SDMA_PKT_NOP_HEADER_OP(SDMA_OP_NOP),
1592 .support_64bit_ptrs = false,
1593 .get_rptr = sdma_v3_0_ring_get_rptr,
1594 .get_wptr = sdma_v3_0_ring_get_wptr,
1595 .set_wptr = sdma_v3_0_ring_set_wptr,
1597 6 + /* sdma_v3_0_ring_emit_hdp_flush */
1598 3 + /* hdp invalidate */
1599 6 + /* sdma_v3_0_ring_emit_pipeline_sync */
1600 VI_FLUSH_GPU_TLB_NUM_WREG * 3 + 6 + /* sdma_v3_0_ring_emit_vm_flush */
1601 10 + 10 + 10, /* sdma_v3_0_ring_emit_fence x3 for user fence, vm fence */
1602 .emit_ib_size = 7 + 6, /* sdma_v3_0_ring_emit_ib */
1603 .emit_ib = sdma_v3_0_ring_emit_ib,
1604 .emit_fence = sdma_v3_0_ring_emit_fence,
1605 .emit_pipeline_sync = sdma_v3_0_ring_emit_pipeline_sync,
1606 .emit_vm_flush = sdma_v3_0_ring_emit_vm_flush,
1607 .emit_hdp_flush = sdma_v3_0_ring_emit_hdp_flush,
1608 .test_ring = sdma_v3_0_ring_test_ring,
1609 .test_ib = sdma_v3_0_ring_test_ib,
1610 .insert_nop = sdma_v3_0_ring_insert_nop,
1611 .pad_ib = sdma_v3_0_ring_pad_ib,
1612 .emit_wreg = sdma_v3_0_ring_emit_wreg,
1615 static void sdma_v3_0_set_ring_funcs(struct amdgpu_device *adev)
1619 for (i = 0; i < adev->sdma.num_instances; i++) {
1620 adev->sdma.instance[i].ring.funcs = &sdma_v3_0_ring_funcs;
1621 adev->sdma.instance[i].ring.me = i;
1625 static const struct amdgpu_irq_src_funcs sdma_v3_0_trap_irq_funcs = {
1626 .set = sdma_v3_0_set_trap_irq_state,
1627 .process = sdma_v3_0_process_trap_irq,
1630 static const struct amdgpu_irq_src_funcs sdma_v3_0_illegal_inst_irq_funcs = {
1631 .process = sdma_v3_0_process_illegal_inst_irq,
1634 static void sdma_v3_0_set_irq_funcs(struct amdgpu_device *adev)
1636 adev->sdma.trap_irq.num_types = AMDGPU_SDMA_IRQ_LAST;
1637 adev->sdma.trap_irq.funcs = &sdma_v3_0_trap_irq_funcs;
1638 adev->sdma.illegal_inst_irq.funcs = &sdma_v3_0_illegal_inst_irq_funcs;
1642 * sdma_v3_0_emit_copy_buffer - copy buffer using the sDMA engine
1644 * @ring: amdgpu_ring structure holding ring information
1645 * @src_offset: src GPU address
1646 * @dst_offset: dst GPU address
1647 * @byte_count: number of bytes to xfer
1649 * Copy GPU buffers using the DMA engine (VI).
1650 * Used by the amdgpu ttm implementation to move pages if
1651 * registered as the asic copy callback.
1653 static void sdma_v3_0_emit_copy_buffer(struct amdgpu_ib *ib,
1654 uint64_t src_offset,
1655 uint64_t dst_offset,
1656 uint32_t byte_count)
1658 ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
1659 SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR);
1660 ib->ptr[ib->length_dw++] = byte_count;
1661 ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
1662 ib->ptr[ib->length_dw++] = lower_32_bits(src_offset);
1663 ib->ptr[ib->length_dw++] = upper_32_bits(src_offset);
1664 ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
1665 ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
1669 * sdma_v3_0_emit_fill_buffer - fill buffer using the sDMA engine
1671 * @ring: amdgpu_ring structure holding ring information
1672 * @src_data: value to write to buffer
1673 * @dst_offset: dst GPU address
1674 * @byte_count: number of bytes to xfer
1676 * Fill GPU buffers using the DMA engine (VI).
1678 static void sdma_v3_0_emit_fill_buffer(struct amdgpu_ib *ib,
1680 uint64_t dst_offset,
1681 uint32_t byte_count)
1683 ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_CONST_FILL);
1684 ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
1685 ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
1686 ib->ptr[ib->length_dw++] = src_data;
1687 ib->ptr[ib->length_dw++] = byte_count;
1690 static const struct amdgpu_buffer_funcs sdma_v3_0_buffer_funcs = {
1691 .copy_max_bytes = 0x3fffe0, /* not 0x3fffff due to HW limitation */
1693 .emit_copy_buffer = sdma_v3_0_emit_copy_buffer,
1695 .fill_max_bytes = 0x3fffe0, /* not 0x3fffff due to HW limitation */
1697 .emit_fill_buffer = sdma_v3_0_emit_fill_buffer,
1700 static void sdma_v3_0_set_buffer_funcs(struct amdgpu_device *adev)
1702 adev->mman.buffer_funcs = &sdma_v3_0_buffer_funcs;
1703 adev->mman.buffer_funcs_ring = &adev->sdma.instance[0].ring;
1706 static const struct amdgpu_vm_pte_funcs sdma_v3_0_vm_pte_funcs = {
1707 .copy_pte_num_dw = 7,
1708 .copy_pte = sdma_v3_0_vm_copy_pte,
1710 .write_pte = sdma_v3_0_vm_write_pte,
1711 .set_pte_pde = sdma_v3_0_vm_set_pte_pde,
1714 static void sdma_v3_0_set_vm_pte_funcs(struct amdgpu_device *adev)
1716 struct drm_gpu_scheduler *sched;
1719 adev->vm_manager.vm_pte_funcs = &sdma_v3_0_vm_pte_funcs;
1720 for (i = 0; i < adev->sdma.num_instances; i++) {
1721 sched = &adev->sdma.instance[i].ring.sched;
1722 adev->vm_manager.vm_pte_rqs[i] =
1723 &sched->sched_rq[DRM_SCHED_PRIORITY_KERNEL];
1725 adev->vm_manager.vm_pte_num_rqs = adev->sdma.num_instances;
1728 const struct amdgpu_ip_block_version sdma_v3_0_ip_block =
1730 .type = AMD_IP_BLOCK_TYPE_SDMA,
1734 .funcs = &sdma_v3_0_ip_funcs,
1737 const struct amdgpu_ip_block_version sdma_v3_1_ip_block =
1739 .type = AMD_IP_BLOCK_TYPE_SDMA,
1743 .funcs = &sdma_v3_0_ip_funcs,