2 * Copyright 2019 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.
24 #include <linux/delay.h>
25 #include <linux/firmware.h>
26 #include <linux/module.h>
27 #include <linux/pci.h>
30 #include "amdgpu_ucode.h"
31 #include "amdgpu_trace.h"
33 #include "gc/gc_10_3_0_offset.h"
34 #include "gc/gc_10_3_0_sh_mask.h"
35 #include "ivsrcid/sdma0/irqsrcs_sdma0_5_0.h"
36 #include "ivsrcid/sdma1/irqsrcs_sdma1_5_0.h"
37 #include "ivsrcid/sdma2/irqsrcs_sdma2_5_0.h"
38 #include "ivsrcid/sdma3/irqsrcs_sdma3_5_0.h"
40 #include "soc15_common.h"
42 #include "navi10_sdma_pkt_open.h"
43 #include "nbio_v2_3.h"
44 #include "sdma_common.h"
45 #include "sdma_v5_2.h"
47 MODULE_FIRMWARE("amdgpu/sienna_cichlid_sdma.bin");
48 MODULE_FIRMWARE("amdgpu/navy_flounder_sdma.bin");
50 #define SDMA1_REG_OFFSET 0x600
51 #define SDMA3_REG_OFFSET 0x400
52 #define SDMA0_HYP_DEC_REG_START 0x5880
53 #define SDMA0_HYP_DEC_REG_END 0x5893
54 #define SDMA1_HYP_DEC_REG_OFFSET 0x20
56 static void sdma_v5_2_set_ring_funcs(struct amdgpu_device *adev);
57 static void sdma_v5_2_set_buffer_funcs(struct amdgpu_device *adev);
58 static void sdma_v5_2_set_vm_pte_funcs(struct amdgpu_device *adev);
59 static void sdma_v5_2_set_irq_funcs(struct amdgpu_device *adev);
61 static u32 sdma_v5_2_get_reg_offset(struct amdgpu_device *adev, u32 instance, u32 internal_offset)
65 if (internal_offset >= SDMA0_HYP_DEC_REG_START &&
66 internal_offset <= SDMA0_HYP_DEC_REG_END) {
67 base = adev->reg_offset[GC_HWIP][0][1];
69 internal_offset += SDMA1_HYP_DEC_REG_OFFSET * instance;
72 base = adev->reg_offset[GC_HWIP][0][0];
74 internal_offset += SDMA1_REG_OFFSET;
76 base = adev->reg_offset[GC_HWIP][0][2];
78 internal_offset += SDMA3_REG_OFFSET;
82 return base + internal_offset;
85 static void sdma_v5_2_init_golden_registers(struct amdgpu_device *adev)
87 switch (adev->asic_type) {
88 case CHIP_SIENNA_CICHLID:
89 case CHIP_NAVY_FLOUNDER:
96 static int sdma_v5_2_init_inst_ctx(struct amdgpu_sdma_instance *sdma_inst)
99 const struct sdma_firmware_header_v1_0 *hdr;
101 err = amdgpu_ucode_validate(sdma_inst->fw);
105 hdr = (const struct sdma_firmware_header_v1_0 *)sdma_inst->fw->data;
106 sdma_inst->fw_version = le32_to_cpu(hdr->header.ucode_version);
107 sdma_inst->feature_version = le32_to_cpu(hdr->ucode_feature_version);
109 if (sdma_inst->feature_version >= 20)
110 sdma_inst->burst_nop = true;
115 static void sdma_v5_2_destroy_inst_ctx(struct amdgpu_device *adev)
119 for (i = 0; i < adev->sdma.num_instances; i++) {
120 release_firmware(adev->sdma.instance[i].fw);
121 adev->sdma.instance[i].fw = NULL;
123 if (adev->asic_type == CHIP_SIENNA_CICHLID)
127 memset((void*)adev->sdma.instance, 0,
128 sizeof(struct amdgpu_sdma_instance) * AMDGPU_MAX_SDMA_INSTANCES);
132 * sdma_v5_2_init_microcode - load ucode images from disk
134 * @adev: amdgpu_device pointer
136 * Use the firmware interface to load the ucode images into
137 * the driver (not loaded into hw).
138 * Returns 0 on success, error on failure.
141 // emulation only, won't work on real chip
142 // navi10 real chip need to use PSP to load firmware
143 static int sdma_v5_2_init_microcode(struct amdgpu_device *adev)
145 const char *chip_name;
148 struct amdgpu_firmware_info *info = NULL;
149 const struct common_firmware_header *header = NULL;
153 switch (adev->asic_type) {
154 case CHIP_SIENNA_CICHLID:
155 chip_name = "sienna_cichlid";
157 case CHIP_NAVY_FLOUNDER:
158 chip_name = "navy_flounder";
164 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_sdma.bin", chip_name);
166 err = request_firmware(&adev->sdma.instance[0].fw, fw_name, adev->dev);
170 err = sdma_v5_2_init_inst_ctx(&adev->sdma.instance[0]);
174 for (i = 1; i < adev->sdma.num_instances; i++) {
175 if (adev->asic_type == CHIP_SIENNA_CICHLID ||
176 adev->asic_type == CHIP_NAVY_FLOUNDER) {
177 memcpy((void*)&adev->sdma.instance[i],
178 (void*)&adev->sdma.instance[0],
179 sizeof(struct amdgpu_sdma_instance));
181 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_sdma%d.bin", chip_name, i);
182 err = request_firmware(&adev->sdma.instance[i].fw, fw_name, adev->dev);
186 err = sdma_v5_2_init_inst_ctx(&adev->sdma.instance[0]);
192 DRM_DEBUG("psp_load == '%s'\n",
193 adev->firmware.load_type == AMDGPU_FW_LOAD_PSP ? "true" : "false");
195 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
196 for (i = 0; i < adev->sdma.num_instances; i++) {
197 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_SDMA0 + i];
198 info->ucode_id = AMDGPU_UCODE_ID_SDMA0 + i;
199 info->fw = adev->sdma.instance[i].fw;
200 header = (const struct common_firmware_header *)info->fw->data;
201 adev->firmware.fw_size +=
202 ALIGN(le32_to_cpu(header->ucode_size_bytes), PAGE_SIZE);
208 DRM_ERROR("sdma_v5_2: Failed to load firmware \"%s\"\n", fw_name);
209 sdma_v5_2_destroy_inst_ctx(adev);
214 static unsigned sdma_v5_2_ring_init_cond_exec(struct amdgpu_ring *ring)
218 amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_COND_EXE));
219 amdgpu_ring_write(ring, lower_32_bits(ring->cond_exe_gpu_addr));
220 amdgpu_ring_write(ring, upper_32_bits(ring->cond_exe_gpu_addr));
221 amdgpu_ring_write(ring, 1);
222 ret = ring->wptr & ring->buf_mask;/* this is the offset we need patch later */
223 amdgpu_ring_write(ring, 0x55aa55aa);/* insert dummy here and patch it later */
228 static void sdma_v5_2_ring_patch_cond_exec(struct amdgpu_ring *ring,
233 BUG_ON(offset > ring->buf_mask);
234 BUG_ON(ring->ring[offset] != 0x55aa55aa);
236 cur = (ring->wptr - 1) & ring->buf_mask;
238 ring->ring[offset] = cur - offset;
240 ring->ring[offset] = (ring->buf_mask + 1) - offset + cur;
244 * sdma_v5_2_ring_get_rptr - get the current read pointer
246 * @ring: amdgpu ring pointer
248 * Get the current rptr from the hardware (NAVI10+).
250 static uint64_t sdma_v5_2_ring_get_rptr(struct amdgpu_ring *ring)
254 /* XXX check if swapping is necessary on BE */
255 rptr = ((u64 *)&ring->adev->wb.wb[ring->rptr_offs]);
257 DRM_DEBUG("rptr before shift == 0x%016llx\n", *rptr);
258 return ((*rptr) >> 2);
262 * sdma_v5_2_ring_get_wptr - get the current write pointer
264 * @ring: amdgpu ring pointer
266 * Get the current wptr from the hardware (NAVI10+).
268 static uint64_t sdma_v5_2_ring_get_wptr(struct amdgpu_ring *ring)
270 struct amdgpu_device *adev = ring->adev;
273 if (ring->use_doorbell) {
274 /* XXX check if swapping is necessary on BE */
275 wptr = READ_ONCE(*((u64 *)&adev->wb.wb[ring->wptr_offs]));
276 DRM_DEBUG("wptr/doorbell before shift == 0x%016llx\n", wptr);
278 wptr = RREG32(sdma_v5_2_get_reg_offset(adev, ring->me, mmSDMA0_GFX_RB_WPTR_HI));
280 wptr |= RREG32(sdma_v5_2_get_reg_offset(adev, ring->me, mmSDMA0_GFX_RB_WPTR));
281 DRM_DEBUG("wptr before shift [%i] wptr == 0x%016llx\n", ring->me, wptr);
288 * sdma_v5_2_ring_set_wptr - commit the write pointer
290 * @ring: amdgpu ring pointer
292 * Write the wptr back to the hardware (NAVI10+).
294 static void sdma_v5_2_ring_set_wptr(struct amdgpu_ring *ring)
296 struct amdgpu_device *adev = ring->adev;
298 DRM_DEBUG("Setting write pointer\n");
299 if (ring->use_doorbell) {
300 DRM_DEBUG("Using doorbell -- "
301 "wptr_offs == 0x%08x "
302 "lower_32_bits(ring->wptr) << 2 == 0x%08x "
303 "upper_32_bits(ring->wptr) << 2 == 0x%08x\n",
305 lower_32_bits(ring->wptr << 2),
306 upper_32_bits(ring->wptr << 2));
307 /* XXX check if swapping is necessary on BE */
308 adev->wb.wb[ring->wptr_offs] = lower_32_bits(ring->wptr << 2);
309 adev->wb.wb[ring->wptr_offs + 1] = upper_32_bits(ring->wptr << 2);
310 DRM_DEBUG("calling WDOORBELL64(0x%08x, 0x%016llx)\n",
311 ring->doorbell_index, ring->wptr << 2);
312 WDOORBELL64(ring->doorbell_index, ring->wptr << 2);
314 DRM_DEBUG("Not using doorbell -- "
315 "mmSDMA%i_GFX_RB_WPTR == 0x%08x "
316 "mmSDMA%i_GFX_RB_WPTR_HI == 0x%08x\n",
318 lower_32_bits(ring->wptr << 2),
320 upper_32_bits(ring->wptr << 2));
321 WREG32(sdma_v5_2_get_reg_offset(adev, ring->me, mmSDMA0_GFX_RB_WPTR),
322 lower_32_bits(ring->wptr << 2));
323 WREG32(sdma_v5_2_get_reg_offset(adev, ring->me, mmSDMA0_GFX_RB_WPTR_HI),
324 upper_32_bits(ring->wptr << 2));
328 static void sdma_v5_2_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count)
330 struct amdgpu_sdma_instance *sdma = amdgpu_sdma_get_instance_from_ring(ring);
333 for (i = 0; i < count; i++)
334 if (sdma && sdma->burst_nop && (i == 0))
335 amdgpu_ring_write(ring, ring->funcs->nop |
336 SDMA_PKT_NOP_HEADER_COUNT(count - 1));
338 amdgpu_ring_write(ring, ring->funcs->nop);
342 * sdma_v5_2_ring_emit_ib - Schedule an IB on the DMA engine
344 * @ring: amdgpu ring pointer
345 * @ib: IB object to schedule
347 * Schedule an IB in the DMA ring.
349 static void sdma_v5_2_ring_emit_ib(struct amdgpu_ring *ring,
350 struct amdgpu_job *job,
351 struct amdgpu_ib *ib,
354 unsigned vmid = AMDGPU_JOB_GET_VMID(job);
355 uint64_t csa_mc_addr = amdgpu_sdma_get_csa_mc_addr(ring, vmid);
357 /* An IB packet must end on a 8 DW boundary--the next dword
358 * must be on a 8-dword boundary. Our IB packet below is 6
359 * dwords long, thus add x number of NOPs, such that, in
360 * modular arithmetic,
361 * wptr + 6 + x = 8k, k >= 0, which in C is,
362 * (wptr + 6 + x) % 8 = 0.
363 * The expression below, is a solution of x.
365 sdma_v5_2_ring_insert_nop(ring, (2 - lower_32_bits(ring->wptr)) & 7);
367 amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_INDIRECT) |
368 SDMA_PKT_INDIRECT_HEADER_VMID(vmid & 0xf));
369 /* base must be 32 byte aligned */
370 amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr) & 0xffffffe0);
371 amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
372 amdgpu_ring_write(ring, ib->length_dw);
373 amdgpu_ring_write(ring, lower_32_bits(csa_mc_addr));
374 amdgpu_ring_write(ring, upper_32_bits(csa_mc_addr));
378 * sdma_v5_2_ring_emit_hdp_flush - emit an hdp flush on the DMA ring
380 * @ring: amdgpu ring pointer
382 * Emit an hdp flush packet on the requested DMA ring.
384 static void sdma_v5_2_ring_emit_hdp_flush(struct amdgpu_ring *ring)
386 struct amdgpu_device *adev = ring->adev;
387 u32 ref_and_mask = 0;
388 const struct nbio_hdp_flush_reg *nbio_hf_reg = adev->nbio.hdp_flush_reg;
390 ref_and_mask = nbio_hf_reg->ref_and_mask_sdma0 << ring->me;
392 amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_POLL_REGMEM) |
393 SDMA_PKT_POLL_REGMEM_HEADER_HDP_FLUSH(1) |
394 SDMA_PKT_POLL_REGMEM_HEADER_FUNC(3)); /* == */
395 amdgpu_ring_write(ring, (adev->nbio.funcs->get_hdp_flush_done_offset(adev)) << 2);
396 amdgpu_ring_write(ring, (adev->nbio.funcs->get_hdp_flush_req_offset(adev)) << 2);
397 amdgpu_ring_write(ring, ref_and_mask); /* reference */
398 amdgpu_ring_write(ring, ref_and_mask); /* mask */
399 amdgpu_ring_write(ring, SDMA_PKT_POLL_REGMEM_DW5_RETRY_COUNT(0xfff) |
400 SDMA_PKT_POLL_REGMEM_DW5_INTERVAL(10)); /* retry count, poll interval */
404 * sdma_v5_2_ring_emit_fence - emit a fence on the DMA ring
406 * @ring: amdgpu ring pointer
407 * @fence: amdgpu fence object
409 * Add a DMA fence packet to the ring to write
410 * the fence seq number and DMA trap packet to generate
411 * an interrupt if needed.
413 static void sdma_v5_2_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
416 bool write64bit = flags & AMDGPU_FENCE_FLAG_64BIT;
417 /* write the fence */
418 amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_FENCE) |
419 SDMA_PKT_FENCE_HEADER_MTYPE(0x3)); /* Ucached(UC) */
420 /* zero in first two bits */
422 amdgpu_ring_write(ring, lower_32_bits(addr));
423 amdgpu_ring_write(ring, upper_32_bits(addr));
424 amdgpu_ring_write(ring, lower_32_bits(seq));
426 /* optionally write high bits as well */
429 amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_FENCE) |
430 SDMA_PKT_FENCE_HEADER_MTYPE(0x3));
431 /* zero in first two bits */
433 amdgpu_ring_write(ring, lower_32_bits(addr));
434 amdgpu_ring_write(ring, upper_32_bits(addr));
435 amdgpu_ring_write(ring, upper_32_bits(seq));
438 if (flags & AMDGPU_FENCE_FLAG_INT) {
439 /* generate an interrupt */
440 amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_TRAP));
441 amdgpu_ring_write(ring, SDMA_PKT_TRAP_INT_CONTEXT_INT_CONTEXT(0));
447 * sdma_v5_2_gfx_stop - stop the gfx async dma engines
449 * @adev: amdgpu_device pointer
451 * Stop the gfx async dma ring buffers.
453 static void sdma_v5_2_gfx_stop(struct amdgpu_device *adev)
455 struct amdgpu_ring *sdma0 = &adev->sdma.instance[0].ring;
456 struct amdgpu_ring *sdma1 = &adev->sdma.instance[1].ring;
457 struct amdgpu_ring *sdma2 = &adev->sdma.instance[2].ring;
458 struct amdgpu_ring *sdma3 = &adev->sdma.instance[3].ring;
459 u32 rb_cntl, ib_cntl;
462 if ((adev->mman.buffer_funcs_ring == sdma0) ||
463 (adev->mman.buffer_funcs_ring == sdma1) ||
464 (adev->mman.buffer_funcs_ring == sdma2) ||
465 (adev->mman.buffer_funcs_ring == sdma3))
466 amdgpu_ttm_set_buffer_funcs_status(adev, false);
468 for (i = 0; i < adev->sdma.num_instances; i++) {
469 rb_cntl = RREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_GFX_RB_CNTL));
470 rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL, RB_ENABLE, 0);
471 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_GFX_RB_CNTL), rb_cntl);
472 ib_cntl = RREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_GFX_IB_CNTL));
473 ib_cntl = REG_SET_FIELD(ib_cntl, SDMA0_GFX_IB_CNTL, IB_ENABLE, 0);
474 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_GFX_IB_CNTL), ib_cntl);
477 sdma0->sched.ready = false;
478 sdma1->sched.ready = false;
479 sdma2->sched.ready = false;
480 sdma3->sched.ready = false;
484 * sdma_v5_2_rlc_stop - stop the compute async dma engines
486 * @adev: amdgpu_device pointer
488 * Stop the compute async dma queues.
490 static void sdma_v5_2_rlc_stop(struct amdgpu_device *adev)
496 * sdma_v_0_ctx_switch_enable - stop the async dma engines context switch
498 * @adev: amdgpu_device pointer
499 * @enable: enable/disable the DMA MEs context switch.
501 * Halt or unhalt the async dma engines context switch.
503 static void sdma_v5_2_ctx_switch_enable(struct amdgpu_device *adev, bool enable)
505 u32 f32_cntl, phase_quantum = 0;
508 if (amdgpu_sdma_phase_quantum) {
509 unsigned value = amdgpu_sdma_phase_quantum;
512 while (value > (SDMA0_PHASE0_QUANTUM__VALUE_MASK >>
513 SDMA0_PHASE0_QUANTUM__VALUE__SHIFT)) {
514 value = (value + 1) >> 1;
517 if (unit > (SDMA0_PHASE0_QUANTUM__UNIT_MASK >>
518 SDMA0_PHASE0_QUANTUM__UNIT__SHIFT)) {
519 value = (SDMA0_PHASE0_QUANTUM__VALUE_MASK >>
520 SDMA0_PHASE0_QUANTUM__VALUE__SHIFT);
521 unit = (SDMA0_PHASE0_QUANTUM__UNIT_MASK >>
522 SDMA0_PHASE0_QUANTUM__UNIT__SHIFT);
524 "clamping sdma_phase_quantum to %uK clock cycles\n",
528 value << SDMA0_PHASE0_QUANTUM__VALUE__SHIFT |
529 unit << SDMA0_PHASE0_QUANTUM__UNIT__SHIFT;
532 for (i = 0; i < adev->sdma.num_instances; i++) {
533 f32_cntl = RREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_CNTL));
534 f32_cntl = REG_SET_FIELD(f32_cntl, SDMA0_CNTL,
535 AUTO_CTXSW_ENABLE, enable ? 1 : 0);
536 if (enable && amdgpu_sdma_phase_quantum) {
537 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_PHASE0_QUANTUM),
539 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_PHASE1_QUANTUM),
541 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_PHASE2_QUANTUM),
544 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_CNTL), f32_cntl);
550 * sdma_v5_2_enable - stop the async dma engines
552 * @adev: amdgpu_device pointer
553 * @enable: enable/disable the DMA MEs.
555 * Halt or unhalt the async dma engines.
557 static void sdma_v5_2_enable(struct amdgpu_device *adev, bool enable)
562 if (enable == false) {
563 sdma_v5_2_gfx_stop(adev);
564 sdma_v5_2_rlc_stop(adev);
567 for (i = 0; i < adev->sdma.num_instances; i++) {
568 f32_cntl = RREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_F32_CNTL));
569 f32_cntl = REG_SET_FIELD(f32_cntl, SDMA0_F32_CNTL, HALT, enable ? 0 : 1);
570 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_F32_CNTL), f32_cntl);
575 * sdma_v5_2_gfx_resume - setup and start the async dma engines
577 * @adev: amdgpu_device pointer
579 * Set up the gfx DMA ring buffers and enable them.
580 * Returns 0 for success, error for failure.
582 static int sdma_v5_2_gfx_resume(struct amdgpu_device *adev)
584 struct amdgpu_ring *ring;
585 u32 rb_cntl, ib_cntl;
595 for (i = 0; i < adev->sdma.num_instances; i++) {
596 ring = &adev->sdma.instance[i].ring;
597 wb_offset = (ring->rptr_offs * 4);
599 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_SEM_WAIT_FAIL_TIMER_CNTL), 0);
601 /* Set ring buffer size in dwords */
602 rb_bufsz = order_base_2(ring->ring_size / 4);
603 rb_cntl = RREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_GFX_RB_CNTL));
604 rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL, RB_SIZE, rb_bufsz);
606 rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL, RB_SWAP_ENABLE, 1);
607 rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL,
608 RPTR_WRITEBACK_SWAP_ENABLE, 1);
610 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_GFX_RB_CNTL), rb_cntl);
612 /* Initialize the ring buffer's read and write pointers */
613 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_GFX_RB_RPTR), 0);
614 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_GFX_RB_RPTR_HI), 0);
615 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_GFX_RB_WPTR), 0);
616 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_GFX_RB_WPTR_HI), 0);
618 /* setup the wptr shadow polling */
619 wptr_gpu_addr = adev->wb.gpu_addr + (ring->wptr_offs * 4);
620 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_GFX_RB_WPTR_POLL_ADDR_LO),
621 lower_32_bits(wptr_gpu_addr));
622 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_GFX_RB_WPTR_POLL_ADDR_HI),
623 upper_32_bits(wptr_gpu_addr));
624 wptr_poll_cntl = RREG32(sdma_v5_2_get_reg_offset(adev, i,
625 mmSDMA0_GFX_RB_WPTR_POLL_CNTL));
626 wptr_poll_cntl = REG_SET_FIELD(wptr_poll_cntl,
627 SDMA0_GFX_RB_WPTR_POLL_CNTL,
629 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_GFX_RB_WPTR_POLL_CNTL),
632 /* set the wb address whether it's enabled or not */
633 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_GFX_RB_RPTR_ADDR_HI),
634 upper_32_bits(adev->wb.gpu_addr + wb_offset) & 0xFFFFFFFF);
635 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_GFX_RB_RPTR_ADDR_LO),
636 lower_32_bits(adev->wb.gpu_addr + wb_offset) & 0xFFFFFFFC);
638 rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL, RPTR_WRITEBACK_ENABLE, 1);
640 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_GFX_RB_BASE), ring->gpu_addr >> 8);
641 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_GFX_RB_BASE_HI), ring->gpu_addr >> 40);
645 /* before programing wptr to a less value, need set minor_ptr_update first */
646 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_GFX_MINOR_PTR_UPDATE), 1);
648 if (!amdgpu_sriov_vf(adev)) { /* only bare-metal use register write for wptr */
649 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_GFX_RB_WPTR), lower_32_bits(ring->wptr) << 2);
650 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_GFX_RB_WPTR_HI), upper_32_bits(ring->wptr) << 2);
653 doorbell = RREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_GFX_DOORBELL));
654 doorbell_offset = RREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_GFX_DOORBELL_OFFSET));
656 if (ring->use_doorbell) {
657 doorbell = REG_SET_FIELD(doorbell, SDMA0_GFX_DOORBELL, ENABLE, 1);
658 doorbell_offset = REG_SET_FIELD(doorbell_offset, SDMA0_GFX_DOORBELL_OFFSET,
659 OFFSET, ring->doorbell_index);
661 doorbell = REG_SET_FIELD(doorbell, SDMA0_GFX_DOORBELL, ENABLE, 0);
663 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_GFX_DOORBELL), doorbell);
664 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_GFX_DOORBELL_OFFSET), doorbell_offset);
666 adev->nbio.funcs->sdma_doorbell_range(adev, i, ring->use_doorbell,
667 ring->doorbell_index,
668 adev->doorbell_index.sdma_doorbell_range);
670 if (amdgpu_sriov_vf(adev))
671 sdma_v5_2_ring_set_wptr(ring);
673 /* set minor_ptr_update to 0 after wptr programed */
674 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_GFX_MINOR_PTR_UPDATE), 0);
676 /* set utc l1 enable flag always to 1 */
677 temp = RREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_CNTL));
678 temp = REG_SET_FIELD(temp, SDMA0_CNTL, UTC_L1_ENABLE, 1);
681 temp = REG_SET_FIELD(temp, SDMA0_CNTL, MIDCMD_PREEMPT_ENABLE, 1);
682 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_CNTL), temp);
684 /* Set up RESP_MODE to non-copy addresses */
685 temp = RREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_UTCL1_CNTL));
686 temp = REG_SET_FIELD(temp, SDMA0_UTCL1_CNTL, RESP_MODE, 3);
687 temp = REG_SET_FIELD(temp, SDMA0_UTCL1_CNTL, REDO_DELAY, 9);
688 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_UTCL1_CNTL), temp);
690 /* program default cache read and write policy */
691 temp = RREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_UTCL1_PAGE));
692 /* clean read policy and write policy bits */
694 temp |= ((CACHE_READ_POLICY_L2__DEFAULT << 12) |
695 (CACHE_WRITE_POLICY_L2__DEFAULT << 14) |
697 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_UTCL1_PAGE), temp);
699 if (!amdgpu_sriov_vf(adev)) {
701 temp = RREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_F32_CNTL));
702 temp = REG_SET_FIELD(temp, SDMA0_F32_CNTL, HALT, 0);
703 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_F32_CNTL), temp);
707 rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL, RB_ENABLE, 1);
708 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_GFX_RB_CNTL), rb_cntl);
710 ib_cntl = RREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_GFX_IB_CNTL));
711 ib_cntl = REG_SET_FIELD(ib_cntl, SDMA0_GFX_IB_CNTL, IB_ENABLE, 1);
713 ib_cntl = REG_SET_FIELD(ib_cntl, SDMA0_GFX_IB_CNTL, IB_SWAP_ENABLE, 1);
716 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_GFX_IB_CNTL), ib_cntl);
718 ring->sched.ready = true;
720 if (amdgpu_sriov_vf(adev)) { /* bare-metal sequence doesn't need below to lines */
721 sdma_v5_2_ctx_switch_enable(adev, true);
722 sdma_v5_2_enable(adev, true);
725 r = amdgpu_ring_test_ring(ring);
727 ring->sched.ready = false;
731 if (adev->mman.buffer_funcs_ring == ring)
732 amdgpu_ttm_set_buffer_funcs_status(adev, true);
739 * sdma_v5_2_rlc_resume - setup and start the async dma engines
741 * @adev: amdgpu_device pointer
743 * Set up the compute DMA queues and enable them.
744 * Returns 0 for success, error for failure.
746 static int sdma_v5_2_rlc_resume(struct amdgpu_device *adev)
752 * sdma_v5_2_load_microcode - load the sDMA ME ucode
754 * @adev: amdgpu_device pointer
756 * Loads the sDMA0/1/2/3 ucode.
757 * Returns 0 for success, -EINVAL if the ucode is not available.
759 static int sdma_v5_2_load_microcode(struct amdgpu_device *adev)
761 const struct sdma_firmware_header_v1_0 *hdr;
762 const __le32 *fw_data;
767 sdma_v5_2_enable(adev, false);
769 for (i = 0; i < adev->sdma.num_instances; i++) {
770 if (!adev->sdma.instance[i].fw)
773 hdr = (const struct sdma_firmware_header_v1_0 *)adev->sdma.instance[i].fw->data;
774 amdgpu_ucode_print_sdma_hdr(&hdr->header);
775 fw_size = le32_to_cpu(hdr->header.ucode_size_bytes) / 4;
777 fw_data = (const __le32 *)
778 (adev->sdma.instance[i].fw->data +
779 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
781 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_UCODE_ADDR), 0);
783 for (j = 0; j < fw_size; j++) {
784 if (amdgpu_emu_mode == 1 && j % 500 == 0)
786 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_UCODE_DATA), le32_to_cpup(fw_data++));
789 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_UCODE_ADDR), adev->sdma.instance[i].fw_version);
796 * sdma_v5_2_start - setup and start the async dma engines
798 * @adev: amdgpu_device pointer
800 * Set up the DMA engines and enable them.
801 * Returns 0 for success, error for failure.
803 static int sdma_v5_2_start(struct amdgpu_device *adev)
807 if (amdgpu_sriov_vf(adev)) {
808 sdma_v5_2_ctx_switch_enable(adev, false);
809 sdma_v5_2_enable(adev, false);
811 /* set RB registers */
812 r = sdma_v5_2_gfx_resume(adev);
816 if (adev->firmware.load_type == AMDGPU_FW_LOAD_DIRECT) {
817 r = sdma_v5_2_load_microcode(adev);
821 /* The value of mmSDMA_F32_CNTL is invalid the moment after loading fw */
822 if (amdgpu_emu_mode == 1)
827 sdma_v5_2_enable(adev, true);
828 /* enable sdma ring preemption */
829 sdma_v5_2_ctx_switch_enable(adev, true);
831 /* start the gfx rings and rlc compute queues */
832 r = sdma_v5_2_gfx_resume(adev);
835 r = sdma_v5_2_rlc_resume(adev);
841 * sdma_v5_2_ring_test_ring - simple async dma engine test
843 * @ring: amdgpu_ring structure holding ring information
845 * Test the DMA engine by writing using it to write an
847 * Returns 0 for success, error for failure.
849 static int sdma_v5_2_ring_test_ring(struct amdgpu_ring *ring)
851 struct amdgpu_device *adev = ring->adev;
858 r = amdgpu_device_wb_get(adev, &index);
860 dev_err(adev->dev, "(%d) failed to allocate wb slot\n", r);
864 gpu_addr = adev->wb.gpu_addr + (index * 4);
866 adev->wb.wb[index] = cpu_to_le32(tmp);
868 r = amdgpu_ring_alloc(ring, 5);
870 DRM_ERROR("amdgpu: dma failed to lock ring %d (%d).\n", ring->idx, r);
871 amdgpu_device_wb_free(adev, index);
875 amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_WRITE) |
876 SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR));
877 amdgpu_ring_write(ring, lower_32_bits(gpu_addr));
878 amdgpu_ring_write(ring, upper_32_bits(gpu_addr));
879 amdgpu_ring_write(ring, SDMA_PKT_WRITE_UNTILED_DW_3_COUNT(0));
880 amdgpu_ring_write(ring, 0xDEADBEEF);
881 amdgpu_ring_commit(ring);
883 for (i = 0; i < adev->usec_timeout; i++) {
884 tmp = le32_to_cpu(adev->wb.wb[index]);
885 if (tmp == 0xDEADBEEF)
887 if (amdgpu_emu_mode == 1)
893 if (i >= adev->usec_timeout)
896 amdgpu_device_wb_free(adev, index);
902 * sdma_v5_2_ring_test_ib - test an IB on the DMA engine
904 * @ring: amdgpu_ring structure holding ring information
906 * Test a simple IB in the DMA ring.
907 * Returns 0 on success, error on failure.
909 static int sdma_v5_2_ring_test_ib(struct amdgpu_ring *ring, long timeout)
911 struct amdgpu_device *adev = ring->adev;
913 struct dma_fence *f = NULL;
919 r = amdgpu_device_wb_get(adev, &index);
921 dev_err(adev->dev, "(%ld) failed to allocate wb slot\n", r);
925 gpu_addr = adev->wb.gpu_addr + (index * 4);
927 adev->wb.wb[index] = cpu_to_le32(tmp);
928 memset(&ib, 0, sizeof(ib));
929 r = amdgpu_ib_get(adev, NULL, 256, AMDGPU_IB_POOL_DIRECT, &ib);
931 DRM_ERROR("amdgpu: failed to get ib (%ld).\n", r);
935 ib.ptr[0] = SDMA_PKT_HEADER_OP(SDMA_OP_WRITE) |
936 SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR);
937 ib.ptr[1] = lower_32_bits(gpu_addr);
938 ib.ptr[2] = upper_32_bits(gpu_addr);
939 ib.ptr[3] = SDMA_PKT_WRITE_UNTILED_DW_3_COUNT(0);
940 ib.ptr[4] = 0xDEADBEEF;
941 ib.ptr[5] = SDMA_PKT_NOP_HEADER_OP(SDMA_OP_NOP);
942 ib.ptr[6] = SDMA_PKT_NOP_HEADER_OP(SDMA_OP_NOP);
943 ib.ptr[7] = SDMA_PKT_NOP_HEADER_OP(SDMA_OP_NOP);
946 r = amdgpu_ib_schedule(ring, 1, &ib, NULL, &f);
950 r = dma_fence_wait_timeout(f, false, timeout);
952 DRM_ERROR("amdgpu: IB test timed out\n");
956 DRM_ERROR("amdgpu: fence wait failed (%ld).\n", r);
959 tmp = le32_to_cpu(adev->wb.wb[index]);
960 if (tmp == 0xDEADBEEF)
966 amdgpu_ib_free(adev, &ib, NULL);
969 amdgpu_device_wb_free(adev, index);
975 * sdma_v5_2_vm_copy_pte - update PTEs by copying them from the GART
977 * @ib: indirect buffer to fill with commands
978 * @pe: addr of the page entry
979 * @src: src addr to copy from
980 * @count: number of page entries to update
982 * Update PTEs by copying them from the GART using sDMA.
984 static void sdma_v5_2_vm_copy_pte(struct amdgpu_ib *ib,
985 uint64_t pe, uint64_t src,
988 unsigned bytes = count * 8;
990 ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
991 SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR);
992 ib->ptr[ib->length_dw++] = bytes - 1;
993 ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
994 ib->ptr[ib->length_dw++] = lower_32_bits(src);
995 ib->ptr[ib->length_dw++] = upper_32_bits(src);
996 ib->ptr[ib->length_dw++] = lower_32_bits(pe);
997 ib->ptr[ib->length_dw++] = upper_32_bits(pe);
1002 * sdma_v5_2_vm_write_pte - update PTEs by writing them manually
1004 * @ib: indirect buffer to fill with commands
1005 * @pe: addr of the page entry
1006 * @addr: dst addr to write into pe
1007 * @count: number of page entries to update
1008 * @incr: increase next addr by incr bytes
1009 * @flags: access flags
1011 * Update PTEs by writing them manually using sDMA.
1013 static void sdma_v5_2_vm_write_pte(struct amdgpu_ib *ib, uint64_t pe,
1014 uint64_t value, unsigned count,
1017 unsigned ndw = count * 2;
1019 ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_WRITE) |
1020 SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR);
1021 ib->ptr[ib->length_dw++] = lower_32_bits(pe);
1022 ib->ptr[ib->length_dw++] = upper_32_bits(pe);
1023 ib->ptr[ib->length_dw++] = ndw - 1;
1024 for (; ndw > 0; ndw -= 2) {
1025 ib->ptr[ib->length_dw++] = lower_32_bits(value);
1026 ib->ptr[ib->length_dw++] = upper_32_bits(value);
1032 * sdma_v5_2_vm_set_pte_pde - update the page tables using sDMA
1034 * @ib: indirect buffer to fill with commands
1035 * @pe: addr of the page entry
1036 * @addr: dst addr to write into pe
1037 * @count: number of page entries to update
1038 * @incr: increase next addr by incr bytes
1039 * @flags: access flags
1041 * Update the page tables using sDMA.
1043 static void sdma_v5_2_vm_set_pte_pde(struct amdgpu_ib *ib,
1045 uint64_t addr, unsigned count,
1046 uint32_t incr, uint64_t flags)
1048 /* for physically contiguous pages (vram) */
1049 ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_PTEPDE);
1050 ib->ptr[ib->length_dw++] = lower_32_bits(pe); /* dst addr */
1051 ib->ptr[ib->length_dw++] = upper_32_bits(pe);
1052 ib->ptr[ib->length_dw++] = lower_32_bits(flags); /* mask */
1053 ib->ptr[ib->length_dw++] = upper_32_bits(flags);
1054 ib->ptr[ib->length_dw++] = lower_32_bits(addr); /* value */
1055 ib->ptr[ib->length_dw++] = upper_32_bits(addr);
1056 ib->ptr[ib->length_dw++] = incr; /* increment size */
1057 ib->ptr[ib->length_dw++] = 0;
1058 ib->ptr[ib->length_dw++] = count - 1; /* number of entries */
1062 * sdma_v5_2_ring_pad_ib - pad the IB
1064 * @ib: indirect buffer to fill with padding
1066 * Pad the IB with NOPs to a boundary multiple of 8.
1068 static void sdma_v5_2_ring_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib)
1070 struct amdgpu_sdma_instance *sdma = amdgpu_sdma_get_instance_from_ring(ring);
1074 pad_count = (-ib->length_dw) & 0x7;
1075 for (i = 0; i < pad_count; i++)
1076 if (sdma && sdma->burst_nop && (i == 0))
1077 ib->ptr[ib->length_dw++] =
1078 SDMA_PKT_HEADER_OP(SDMA_OP_NOP) |
1079 SDMA_PKT_NOP_HEADER_COUNT(pad_count - 1);
1081 ib->ptr[ib->length_dw++] =
1082 SDMA_PKT_HEADER_OP(SDMA_OP_NOP);
1087 * sdma_v5_2_ring_emit_pipeline_sync - sync the pipeline
1089 * @ring: amdgpu_ring pointer
1091 * Make sure all previous operations are completed (CIK).
1093 static void sdma_v5_2_ring_emit_pipeline_sync(struct amdgpu_ring *ring)
1095 uint32_t seq = ring->fence_drv.sync_seq;
1096 uint64_t addr = ring->fence_drv.gpu_addr;
1099 amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_POLL_REGMEM) |
1100 SDMA_PKT_POLL_REGMEM_HEADER_HDP_FLUSH(0) |
1101 SDMA_PKT_POLL_REGMEM_HEADER_FUNC(3) | /* equal */
1102 SDMA_PKT_POLL_REGMEM_HEADER_MEM_POLL(1));
1103 amdgpu_ring_write(ring, addr & 0xfffffffc);
1104 amdgpu_ring_write(ring, upper_32_bits(addr) & 0xffffffff);
1105 amdgpu_ring_write(ring, seq); /* reference */
1106 amdgpu_ring_write(ring, 0xffffffff); /* mask */
1107 amdgpu_ring_write(ring, SDMA_PKT_POLL_REGMEM_DW5_RETRY_COUNT(0xfff) |
1108 SDMA_PKT_POLL_REGMEM_DW5_INTERVAL(4)); /* retry count, poll interval */
1113 * sdma_v5_2_ring_emit_vm_flush - vm flush using sDMA
1115 * @ring: amdgpu_ring pointer
1116 * @vm: amdgpu_vm pointer
1118 * Update the page table base and flush the VM TLB
1121 static void sdma_v5_2_ring_emit_vm_flush(struct amdgpu_ring *ring,
1122 unsigned vmid, uint64_t pd_addr)
1124 amdgpu_gmc_emit_flush_gpu_tlb(ring, vmid, pd_addr);
1127 static void sdma_v5_2_ring_emit_wreg(struct amdgpu_ring *ring,
1128 uint32_t reg, uint32_t val)
1130 amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_SRBM_WRITE) |
1131 SDMA_PKT_SRBM_WRITE_HEADER_BYTE_EN(0xf));
1132 amdgpu_ring_write(ring, reg);
1133 amdgpu_ring_write(ring, val);
1136 static void sdma_v5_2_ring_emit_reg_wait(struct amdgpu_ring *ring, uint32_t reg,
1137 uint32_t val, uint32_t mask)
1139 amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_POLL_REGMEM) |
1140 SDMA_PKT_POLL_REGMEM_HEADER_HDP_FLUSH(0) |
1141 SDMA_PKT_POLL_REGMEM_HEADER_FUNC(3)); /* equal */
1142 amdgpu_ring_write(ring, reg << 2);
1143 amdgpu_ring_write(ring, 0);
1144 amdgpu_ring_write(ring, val); /* reference */
1145 amdgpu_ring_write(ring, mask); /* mask */
1146 amdgpu_ring_write(ring, SDMA_PKT_POLL_REGMEM_DW5_RETRY_COUNT(0xfff) |
1147 SDMA_PKT_POLL_REGMEM_DW5_INTERVAL(10));
1150 static void sdma_v5_2_ring_emit_reg_write_reg_wait(struct amdgpu_ring *ring,
1151 uint32_t reg0, uint32_t reg1,
1152 uint32_t ref, uint32_t mask)
1154 amdgpu_ring_emit_wreg(ring, reg0, ref);
1155 /* wait for a cycle to reset vm_inv_eng*_ack */
1156 amdgpu_ring_emit_reg_wait(ring, reg0, 0, 0);
1157 amdgpu_ring_emit_reg_wait(ring, reg1, mask, mask);
1160 static int sdma_v5_2_early_init(void *handle)
1162 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1164 switch (adev->asic_type) {
1165 case CHIP_SIENNA_CICHLID:
1166 adev->sdma.num_instances = 4;
1168 case CHIP_NAVY_FLOUNDER:
1169 adev->sdma.num_instances = 2;
1175 sdma_v5_2_set_ring_funcs(adev);
1176 sdma_v5_2_set_buffer_funcs(adev);
1177 sdma_v5_2_set_vm_pte_funcs(adev);
1178 sdma_v5_2_set_irq_funcs(adev);
1183 static unsigned sdma_v5_2_seq_to_irq_id(int seq_num)
1187 return SOC15_IH_CLIENTID_SDMA0;
1189 return SOC15_IH_CLIENTID_SDMA1;
1191 return SOC15_IH_CLIENTID_SDMA2;
1193 return SOC15_IH_CLIENTID_SDMA3_Sienna_Cichlid;
1200 static unsigned sdma_v5_2_seq_to_trap_id(int seq_num)
1204 return SDMA0_5_0__SRCID__SDMA_TRAP;
1206 return SDMA1_5_0__SRCID__SDMA_TRAP;
1208 return SDMA2_5_0__SRCID__SDMA_TRAP;
1210 return SDMA3_5_0__SRCID__SDMA_TRAP;
1217 static int sdma_v5_2_sw_init(void *handle)
1219 struct amdgpu_ring *ring;
1221 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1223 /* SDMA trap event */
1224 for (i = 0; i < adev->sdma.num_instances; i++) {
1225 r = amdgpu_irq_add_id(adev, sdma_v5_2_seq_to_irq_id(i),
1226 sdma_v5_2_seq_to_trap_id(i),
1227 &adev->sdma.trap_irq);
1232 r = sdma_v5_2_init_microcode(adev);
1234 DRM_ERROR("Failed to load sdma firmware!\n");
1238 for (i = 0; i < adev->sdma.num_instances; i++) {
1239 ring = &adev->sdma.instance[i].ring;
1240 ring->ring_obj = NULL;
1241 ring->use_doorbell = true;
1244 DRM_INFO("use_doorbell being set to: [%s]\n",
1245 ring->use_doorbell?"true":"false");
1247 ring->doorbell_index =
1248 (adev->doorbell_index.sdma_engine[i] << 1); //get DWORD offset
1250 sprintf(ring->name, "sdma%d", i);
1251 r = amdgpu_ring_init(adev, ring, 1024,
1252 &adev->sdma.trap_irq,
1253 AMDGPU_SDMA_IRQ_INSTANCE0 + i,
1254 AMDGPU_RING_PRIO_DEFAULT);
1262 static int sdma_v5_2_sw_fini(void *handle)
1264 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1267 for (i = 0; i < adev->sdma.num_instances; i++)
1268 amdgpu_ring_fini(&adev->sdma.instance[i].ring);
1270 sdma_v5_2_destroy_inst_ctx(adev);
1275 static int sdma_v5_2_hw_init(void *handle)
1278 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1280 sdma_v5_2_init_golden_registers(adev);
1282 r = sdma_v5_2_start(adev);
1287 static int sdma_v5_2_hw_fini(void *handle)
1289 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1291 if (amdgpu_sriov_vf(adev))
1294 sdma_v5_2_ctx_switch_enable(adev, false);
1295 sdma_v5_2_enable(adev, false);
1300 static int sdma_v5_2_suspend(void *handle)
1302 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1304 return sdma_v5_2_hw_fini(adev);
1307 static int sdma_v5_2_resume(void *handle)
1309 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1311 return sdma_v5_2_hw_init(adev);
1314 static bool sdma_v5_2_is_idle(void *handle)
1316 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1319 for (i = 0; i < adev->sdma.num_instances; i++) {
1320 u32 tmp = RREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_STATUS_REG));
1322 if (!(tmp & SDMA0_STATUS_REG__IDLE_MASK))
1329 static int sdma_v5_2_wait_for_idle(void *handle)
1332 u32 sdma0, sdma1, sdma2, sdma3;
1333 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1335 for (i = 0; i < adev->usec_timeout; i++) {
1336 sdma0 = RREG32(sdma_v5_2_get_reg_offset(adev, 0, mmSDMA0_STATUS_REG));
1337 sdma1 = RREG32(sdma_v5_2_get_reg_offset(adev, 1, mmSDMA0_STATUS_REG));
1338 sdma2 = RREG32(sdma_v5_2_get_reg_offset(adev, 2, mmSDMA0_STATUS_REG));
1339 sdma3 = RREG32(sdma_v5_2_get_reg_offset(adev, 3, mmSDMA0_STATUS_REG));
1341 if (sdma0 & sdma1 & sdma2 & sdma3 & SDMA0_STATUS_REG__IDLE_MASK)
1348 static int sdma_v5_2_soft_reset(void *handle)
1355 static int sdma_v5_2_ring_preempt_ib(struct amdgpu_ring *ring)
1358 struct amdgpu_device *adev = ring->adev;
1360 u64 sdma_gfx_preempt;
1362 amdgpu_sdma_get_index_from_ring(ring, &index);
1364 sdma_v5_2_get_reg_offset(adev, index, mmSDMA0_GFX_PREEMPT);
1366 /* assert preemption condition */
1367 amdgpu_ring_set_preempt_cond_exec(ring, false);
1369 /* emit the trailing fence */
1370 ring->trail_seq += 1;
1371 amdgpu_ring_alloc(ring, 10);
1372 sdma_v5_2_ring_emit_fence(ring, ring->trail_fence_gpu_addr,
1373 ring->trail_seq, 0);
1374 amdgpu_ring_commit(ring);
1376 /* assert IB preemption */
1377 WREG32(sdma_gfx_preempt, 1);
1379 /* poll the trailing fence */
1380 for (i = 0; i < adev->usec_timeout; i++) {
1381 if (ring->trail_seq ==
1382 le32_to_cpu(*(ring->trail_fence_cpu_addr)))
1387 if (i >= adev->usec_timeout) {
1389 DRM_ERROR("ring %d failed to be preempted\n", ring->idx);
1392 /* deassert IB preemption */
1393 WREG32(sdma_gfx_preempt, 0);
1395 /* deassert the preemption condition */
1396 amdgpu_ring_set_preempt_cond_exec(ring, true);
1400 static int sdma_v5_2_set_trap_irq_state(struct amdgpu_device *adev,
1401 struct amdgpu_irq_src *source,
1403 enum amdgpu_interrupt_state state)
1407 u32 reg_offset = sdma_v5_2_get_reg_offset(adev, type, mmSDMA0_CNTL);
1409 sdma_cntl = RREG32(reg_offset);
1410 sdma_cntl = REG_SET_FIELD(sdma_cntl, SDMA0_CNTL, TRAP_ENABLE,
1411 state == AMDGPU_IRQ_STATE_ENABLE ? 1 : 0);
1412 WREG32(reg_offset, sdma_cntl);
1417 static int sdma_v5_2_process_trap_irq(struct amdgpu_device *adev,
1418 struct amdgpu_irq_src *source,
1419 struct amdgpu_iv_entry *entry)
1421 DRM_DEBUG("IH: SDMA trap\n");
1422 switch (entry->client_id) {
1423 case SOC15_IH_CLIENTID_SDMA0:
1424 switch (entry->ring_id) {
1426 amdgpu_fence_process(&adev->sdma.instance[0].ring);
1439 case SOC15_IH_CLIENTID_SDMA1:
1440 switch (entry->ring_id) {
1442 amdgpu_fence_process(&adev->sdma.instance[1].ring);
1455 case SOC15_IH_CLIENTID_SDMA2:
1456 switch (entry->ring_id) {
1458 amdgpu_fence_process(&adev->sdma.instance[2].ring);
1471 case SOC15_IH_CLIENTID_SDMA3_Sienna_Cichlid:
1472 switch (entry->ring_id) {
1474 amdgpu_fence_process(&adev->sdma.instance[3].ring);
1491 static int sdma_v5_2_process_illegal_inst_irq(struct amdgpu_device *adev,
1492 struct amdgpu_irq_src *source,
1493 struct amdgpu_iv_entry *entry)
1498 static void sdma_v5_2_update_medium_grain_clock_gating(struct amdgpu_device *adev,
1504 for (i = 0; i < adev->sdma.num_instances; i++) {
1505 if (enable && (adev->cg_flags & AMD_CG_SUPPORT_SDMA_MGCG)) {
1506 /* Enable sdma clock gating */
1507 def = data = RREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_CLK_CTRL));
1508 data &= ~(SDMA0_CLK_CTRL__SOFT_OVERRIDE4_MASK |
1509 SDMA0_CLK_CTRL__SOFT_OVERRIDE3_MASK |
1510 SDMA0_CLK_CTRL__SOFT_OVERRIDE2_MASK |
1511 SDMA0_CLK_CTRL__SOFT_OVERRIDE1_MASK |
1512 SDMA0_CLK_CTRL__SOFT_OVERRIDE0_MASK |
1513 SDMA0_CLK_CTRL__SOFT_OVERRIDER_REG_MASK);
1515 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_CLK_CTRL), data);
1517 /* Disable sdma clock gating */
1518 def = data = RREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_CLK_CTRL));
1519 data |= (SDMA0_CLK_CTRL__SOFT_OVERRIDE4_MASK |
1520 SDMA0_CLK_CTRL__SOFT_OVERRIDE3_MASK |
1521 SDMA0_CLK_CTRL__SOFT_OVERRIDE2_MASK |
1522 SDMA0_CLK_CTRL__SOFT_OVERRIDE1_MASK |
1523 SDMA0_CLK_CTRL__SOFT_OVERRIDE0_MASK |
1524 SDMA0_CLK_CTRL__SOFT_OVERRIDER_REG_MASK);
1526 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_CLK_CTRL), data);
1531 static void sdma_v5_2_update_medium_grain_light_sleep(struct amdgpu_device *adev,
1537 for (i = 0; i < adev->sdma.num_instances; i++) {
1538 if (enable && (adev->cg_flags & AMD_CG_SUPPORT_SDMA_LS)) {
1539 /* Enable sdma mem light sleep */
1540 def = data = RREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_POWER_CNTL));
1541 data |= SDMA0_POWER_CNTL__MEM_POWER_OVERRIDE_MASK;
1543 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_POWER_CNTL), data);
1546 /* Disable sdma mem light sleep */
1547 def = data = RREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_POWER_CNTL));
1548 data &= ~SDMA0_POWER_CNTL__MEM_POWER_OVERRIDE_MASK;
1550 WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_POWER_CNTL), data);
1556 static int sdma_v5_2_set_clockgating_state(void *handle,
1557 enum amd_clockgating_state state)
1559 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1561 if (amdgpu_sriov_vf(adev))
1564 switch (adev->asic_type) {
1565 case CHIP_SIENNA_CICHLID:
1566 case CHIP_NAVY_FLOUNDER:
1567 sdma_v5_2_update_medium_grain_clock_gating(adev,
1568 state == AMD_CG_STATE_GATE ? true : false);
1569 sdma_v5_2_update_medium_grain_light_sleep(adev,
1570 state == AMD_CG_STATE_GATE ? true : false);
1579 static int sdma_v5_2_set_powergating_state(void *handle,
1580 enum amd_powergating_state state)
1585 static void sdma_v5_2_get_clockgating_state(void *handle, u32 *flags)
1587 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1590 if (amdgpu_sriov_vf(adev))
1593 /* AMD_CG_SUPPORT_SDMA_LS */
1594 data = RREG32_KIQ(sdma_v5_2_get_reg_offset(adev, 0, mmSDMA0_POWER_CNTL));
1595 if (data & SDMA0_POWER_CNTL__MEM_POWER_OVERRIDE_MASK)
1596 *flags |= AMD_CG_SUPPORT_SDMA_LS;
1599 const struct amd_ip_funcs sdma_v5_2_ip_funcs = {
1600 .name = "sdma_v5_2",
1601 .early_init = sdma_v5_2_early_init,
1603 .sw_init = sdma_v5_2_sw_init,
1604 .sw_fini = sdma_v5_2_sw_fini,
1605 .hw_init = sdma_v5_2_hw_init,
1606 .hw_fini = sdma_v5_2_hw_fini,
1607 .suspend = sdma_v5_2_suspend,
1608 .resume = sdma_v5_2_resume,
1609 .is_idle = sdma_v5_2_is_idle,
1610 .wait_for_idle = sdma_v5_2_wait_for_idle,
1611 .soft_reset = sdma_v5_2_soft_reset,
1612 .set_clockgating_state = sdma_v5_2_set_clockgating_state,
1613 .set_powergating_state = sdma_v5_2_set_powergating_state,
1614 .get_clockgating_state = sdma_v5_2_get_clockgating_state,
1617 static const struct amdgpu_ring_funcs sdma_v5_2_ring_funcs = {
1618 .type = AMDGPU_RING_TYPE_SDMA,
1620 .nop = SDMA_PKT_NOP_HEADER_OP(SDMA_OP_NOP),
1621 .support_64bit_ptrs = true,
1622 .vmhub = AMDGPU_GFXHUB_0,
1623 .get_rptr = sdma_v5_2_ring_get_rptr,
1624 .get_wptr = sdma_v5_2_ring_get_wptr,
1625 .set_wptr = sdma_v5_2_ring_set_wptr,
1627 5 + /* sdma_v5_2_ring_init_cond_exec */
1628 6 + /* sdma_v5_2_ring_emit_hdp_flush */
1629 3 + /* hdp_invalidate */
1630 6 + /* sdma_v5_2_ring_emit_pipeline_sync */
1631 /* sdma_v5_2_ring_emit_vm_flush */
1632 SOC15_FLUSH_GPU_TLB_NUM_WREG * 3 +
1633 SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 6 +
1634 10 + 10 + 10, /* sdma_v5_2_ring_emit_fence x3 for user fence, vm fence */
1635 .emit_ib_size = 7 + 6, /* sdma_v5_2_ring_emit_ib */
1636 .emit_ib = sdma_v5_2_ring_emit_ib,
1637 .emit_fence = sdma_v5_2_ring_emit_fence,
1638 .emit_pipeline_sync = sdma_v5_2_ring_emit_pipeline_sync,
1639 .emit_vm_flush = sdma_v5_2_ring_emit_vm_flush,
1640 .emit_hdp_flush = sdma_v5_2_ring_emit_hdp_flush,
1641 .test_ring = sdma_v5_2_ring_test_ring,
1642 .test_ib = sdma_v5_2_ring_test_ib,
1643 .insert_nop = sdma_v5_2_ring_insert_nop,
1644 .pad_ib = sdma_v5_2_ring_pad_ib,
1645 .emit_wreg = sdma_v5_2_ring_emit_wreg,
1646 .emit_reg_wait = sdma_v5_2_ring_emit_reg_wait,
1647 .emit_reg_write_reg_wait = sdma_v5_2_ring_emit_reg_write_reg_wait,
1648 .init_cond_exec = sdma_v5_2_ring_init_cond_exec,
1649 .patch_cond_exec = sdma_v5_2_ring_patch_cond_exec,
1650 .preempt_ib = sdma_v5_2_ring_preempt_ib,
1653 static void sdma_v5_2_set_ring_funcs(struct amdgpu_device *adev)
1657 for (i = 0; i < adev->sdma.num_instances; i++) {
1658 adev->sdma.instance[i].ring.funcs = &sdma_v5_2_ring_funcs;
1659 adev->sdma.instance[i].ring.me = i;
1663 static const struct amdgpu_irq_src_funcs sdma_v5_2_trap_irq_funcs = {
1664 .set = sdma_v5_2_set_trap_irq_state,
1665 .process = sdma_v5_2_process_trap_irq,
1668 static const struct amdgpu_irq_src_funcs sdma_v5_2_illegal_inst_irq_funcs = {
1669 .process = sdma_v5_2_process_illegal_inst_irq,
1672 static void sdma_v5_2_set_irq_funcs(struct amdgpu_device *adev)
1674 adev->sdma.trap_irq.num_types = AMDGPU_SDMA_IRQ_INSTANCE0 +
1675 adev->sdma.num_instances;
1676 adev->sdma.trap_irq.funcs = &sdma_v5_2_trap_irq_funcs;
1677 adev->sdma.illegal_inst_irq.funcs = &sdma_v5_2_illegal_inst_irq_funcs;
1681 * sdma_v5_2_emit_copy_buffer - copy buffer using the sDMA engine
1683 * @ring: amdgpu_ring structure holding ring information
1684 * @src_offset: src GPU address
1685 * @dst_offset: dst GPU address
1686 * @byte_count: number of bytes to xfer
1688 * Copy GPU buffers using the DMA engine.
1689 * Used by the amdgpu ttm implementation to move pages if
1690 * registered as the asic copy callback.
1692 static void sdma_v5_2_emit_copy_buffer(struct amdgpu_ib *ib,
1693 uint64_t src_offset,
1694 uint64_t dst_offset,
1695 uint32_t byte_count,
1698 ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
1699 SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR) |
1700 SDMA_PKT_COPY_LINEAR_HEADER_TMZ(tmz ? 1 : 0);
1701 ib->ptr[ib->length_dw++] = byte_count - 1;
1702 ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
1703 ib->ptr[ib->length_dw++] = lower_32_bits(src_offset);
1704 ib->ptr[ib->length_dw++] = upper_32_bits(src_offset);
1705 ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
1706 ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
1710 * sdma_v5_2_emit_fill_buffer - fill buffer using the sDMA engine
1712 * @ring: amdgpu_ring structure holding ring information
1713 * @src_data: value to write to buffer
1714 * @dst_offset: dst GPU address
1715 * @byte_count: number of bytes to xfer
1717 * Fill GPU buffers using the DMA engine.
1719 static void sdma_v5_2_emit_fill_buffer(struct amdgpu_ib *ib,
1721 uint64_t dst_offset,
1722 uint32_t byte_count)
1724 ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_CONST_FILL);
1725 ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
1726 ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
1727 ib->ptr[ib->length_dw++] = src_data;
1728 ib->ptr[ib->length_dw++] = byte_count - 1;
1731 static const struct amdgpu_buffer_funcs sdma_v5_2_buffer_funcs = {
1732 .copy_max_bytes = 0x400000,
1734 .emit_copy_buffer = sdma_v5_2_emit_copy_buffer,
1736 .fill_max_bytes = 0x400000,
1738 .emit_fill_buffer = sdma_v5_2_emit_fill_buffer,
1741 static void sdma_v5_2_set_buffer_funcs(struct amdgpu_device *adev)
1743 if (adev->mman.buffer_funcs == NULL) {
1744 adev->mman.buffer_funcs = &sdma_v5_2_buffer_funcs;
1745 adev->mman.buffer_funcs_ring = &adev->sdma.instance[0].ring;
1749 static const struct amdgpu_vm_pte_funcs sdma_v5_2_vm_pte_funcs = {
1750 .copy_pte_num_dw = 7,
1751 .copy_pte = sdma_v5_2_vm_copy_pte,
1752 .write_pte = sdma_v5_2_vm_write_pte,
1753 .set_pte_pde = sdma_v5_2_vm_set_pte_pde,
1756 static void sdma_v5_2_set_vm_pte_funcs(struct amdgpu_device *adev)
1760 if (adev->vm_manager.vm_pte_funcs == NULL) {
1761 adev->vm_manager.vm_pte_funcs = &sdma_v5_2_vm_pte_funcs;
1762 for (i = 0; i < adev->sdma.num_instances; i++) {
1763 adev->vm_manager.vm_pte_scheds[i] =
1764 &adev->sdma.instance[i].ring.sched;
1766 adev->vm_manager.vm_pte_num_scheds = adev->sdma.num_instances;
1770 const struct amdgpu_ip_block_version sdma_v5_2_ip_block = {
1771 .type = AMD_IP_BLOCK_TYPE_SDMA,
1775 .funcs = &sdma_v5_2_ip_funcs,