1 1.1 riastrad /* $NetBSD: radeon_evergreen_dma.c,v 1.2 2021/12/18 23:45:43 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /* 4 1.1 riastrad * Copyright 2010 Advanced Micro Devices, Inc. 5 1.1 riastrad * 6 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 7 1.1 riastrad * copy of this software and associated documentation files (the "Software"), 8 1.1 riastrad * to deal in the Software without restriction, including without limitation 9 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the 11 1.1 riastrad * Software is furnished to do so, subject to the following conditions: 12 1.1 riastrad * 13 1.1 riastrad * The above copyright notice and this permission notice shall be included in 14 1.1 riastrad * all copies or substantial portions of the Software. 15 1.1 riastrad * 16 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 1.1 riastrad * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 1.1 riastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 1.1 riastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 1.1 riastrad * OTHER DEALINGS IN THE SOFTWARE. 23 1.1 riastrad * 24 1.1 riastrad * Authors: Alex Deucher 25 1.1 riastrad */ 26 1.2 riastrad 27 1.1 riastrad #include <sys/cdefs.h> 28 1.1 riastrad __KERNEL_RCSID(0, "$NetBSD: radeon_evergreen_dma.c,v 1.2 2021/12/18 23:45:43 riastradh Exp $"); 29 1.1 riastrad 30 1.1 riastrad #include "radeon.h" 31 1.1 riastrad #include "radeon_asic.h" 32 1.1 riastrad #include "evergreend.h" 33 1.1 riastrad 34 1.1 riastrad u32 evergreen_gpu_check_soft_reset(struct radeon_device *rdev); 35 1.1 riastrad 36 1.1 riastrad /** 37 1.1 riastrad * evergreen_dma_fence_ring_emit - emit a fence on the DMA ring 38 1.1 riastrad * 39 1.1 riastrad * @rdev: radeon_device pointer 40 1.1 riastrad * @fence: radeon fence object 41 1.1 riastrad * 42 1.1 riastrad * Add a DMA fence packet to the ring to write 43 1.1 riastrad * the fence seq number and DMA trap packet to generate 44 1.1 riastrad * an interrupt if needed (evergreen-SI). 45 1.1 riastrad */ 46 1.1 riastrad void evergreen_dma_fence_ring_emit(struct radeon_device *rdev, 47 1.1 riastrad struct radeon_fence *fence) 48 1.1 riastrad { 49 1.1 riastrad struct radeon_ring *ring = &rdev->ring[fence->ring]; 50 1.1 riastrad u64 addr = rdev->fence_drv[fence->ring].gpu_addr; 51 1.1 riastrad /* write the fence */ 52 1.1 riastrad radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_FENCE, 0, 0)); 53 1.1 riastrad radeon_ring_write(ring, addr & 0xfffffffc); 54 1.1 riastrad radeon_ring_write(ring, (upper_32_bits(addr) & 0xff)); 55 1.1 riastrad radeon_ring_write(ring, fence->seq); 56 1.1 riastrad /* generate an interrupt */ 57 1.1 riastrad radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_TRAP, 0, 0)); 58 1.1 riastrad /* flush HDP */ 59 1.1 riastrad radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_SRBM_WRITE, 0, 0)); 60 1.1 riastrad radeon_ring_write(ring, (0xf << 16) | (HDP_MEM_COHERENCY_FLUSH_CNTL >> 2)); 61 1.1 riastrad radeon_ring_write(ring, 1); 62 1.1 riastrad } 63 1.1 riastrad 64 1.1 riastrad /** 65 1.1 riastrad * evergreen_dma_ring_ib_execute - schedule an IB on the DMA engine 66 1.1 riastrad * 67 1.1 riastrad * @rdev: radeon_device pointer 68 1.1 riastrad * @ib: IB object to schedule 69 1.1 riastrad * 70 1.1 riastrad * Schedule an IB in the DMA ring (evergreen). 71 1.1 riastrad */ 72 1.1 riastrad void evergreen_dma_ring_ib_execute(struct radeon_device *rdev, 73 1.1 riastrad struct radeon_ib *ib) 74 1.1 riastrad { 75 1.1 riastrad struct radeon_ring *ring = &rdev->ring[ib->ring]; 76 1.1 riastrad 77 1.1 riastrad if (rdev->wb.enabled) { 78 1.1 riastrad u32 next_rptr = ring->wptr + 4; 79 1.1 riastrad while ((next_rptr & 7) != 5) 80 1.1 riastrad next_rptr++; 81 1.1 riastrad next_rptr += 3; 82 1.1 riastrad radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_WRITE, 0, 1)); 83 1.1 riastrad radeon_ring_write(ring, ring->next_rptr_gpu_addr & 0xfffffffc); 84 1.1 riastrad radeon_ring_write(ring, upper_32_bits(ring->next_rptr_gpu_addr) & 0xff); 85 1.1 riastrad radeon_ring_write(ring, next_rptr); 86 1.1 riastrad } 87 1.1 riastrad 88 1.1 riastrad /* The indirect buffer packet must end on an 8 DW boundary in the DMA ring. 89 1.1 riastrad * Pad as necessary with NOPs. 90 1.1 riastrad */ 91 1.1 riastrad while ((ring->wptr & 7) != 5) 92 1.1 riastrad radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_NOP, 0, 0)); 93 1.1 riastrad radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_INDIRECT_BUFFER, 0, 0)); 94 1.1 riastrad radeon_ring_write(ring, (ib->gpu_addr & 0xFFFFFFE0)); 95 1.1 riastrad radeon_ring_write(ring, (ib->length_dw << 12) | (upper_32_bits(ib->gpu_addr) & 0xFF)); 96 1.1 riastrad 97 1.1 riastrad } 98 1.1 riastrad 99 1.1 riastrad /** 100 1.1 riastrad * evergreen_copy_dma - copy pages using the DMA engine 101 1.1 riastrad * 102 1.1 riastrad * @rdev: radeon_device pointer 103 1.1 riastrad * @src_offset: src GPU address 104 1.1 riastrad * @dst_offset: dst GPU address 105 1.1 riastrad * @num_gpu_pages: number of GPU pages to xfer 106 1.1 riastrad * @fence: radeon fence object 107 1.1 riastrad * 108 1.1 riastrad * Copy GPU paging using the DMA engine (evergreen-cayman). 109 1.1 riastrad * Used by the radeon ttm implementation to move pages if 110 1.1 riastrad * registered as the asic copy callback. 111 1.1 riastrad */ 112 1.1 riastrad struct radeon_fence *evergreen_copy_dma(struct radeon_device *rdev, 113 1.1 riastrad uint64_t src_offset, 114 1.1 riastrad uint64_t dst_offset, 115 1.1 riastrad unsigned num_gpu_pages, 116 1.2 riastrad struct dma_resv *resv) 117 1.1 riastrad { 118 1.1 riastrad struct radeon_fence *fence; 119 1.1 riastrad struct radeon_sync sync; 120 1.1 riastrad int ring_index = rdev->asic->copy.dma_ring_index; 121 1.1 riastrad struct radeon_ring *ring = &rdev->ring[ring_index]; 122 1.1 riastrad u32 size_in_dw, cur_size_in_dw; 123 1.1 riastrad int i, num_loops; 124 1.1 riastrad int r = 0; 125 1.1 riastrad 126 1.1 riastrad radeon_sync_create(&sync); 127 1.1 riastrad 128 1.1 riastrad size_in_dw = (num_gpu_pages << RADEON_GPU_PAGE_SHIFT) / 4; 129 1.1 riastrad num_loops = DIV_ROUND_UP(size_in_dw, 0xfffff); 130 1.1 riastrad r = radeon_ring_lock(rdev, ring, num_loops * 5 + 11); 131 1.1 riastrad if (r) { 132 1.1 riastrad DRM_ERROR("radeon: moving bo (%d).\n", r); 133 1.1 riastrad radeon_sync_free(rdev, &sync, NULL); 134 1.1 riastrad return ERR_PTR(r); 135 1.1 riastrad } 136 1.1 riastrad 137 1.1 riastrad radeon_sync_resv(rdev, &sync, resv, false); 138 1.1 riastrad radeon_sync_rings(rdev, &sync, ring->idx); 139 1.1 riastrad 140 1.1 riastrad for (i = 0; i < num_loops; i++) { 141 1.1 riastrad cur_size_in_dw = size_in_dw; 142 1.1 riastrad if (cur_size_in_dw > 0xFFFFF) 143 1.1 riastrad cur_size_in_dw = 0xFFFFF; 144 1.1 riastrad size_in_dw -= cur_size_in_dw; 145 1.1 riastrad radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_COPY, 0, cur_size_in_dw)); 146 1.1 riastrad radeon_ring_write(ring, dst_offset & 0xfffffffc); 147 1.1 riastrad radeon_ring_write(ring, src_offset & 0xfffffffc); 148 1.1 riastrad radeon_ring_write(ring, upper_32_bits(dst_offset) & 0xff); 149 1.1 riastrad radeon_ring_write(ring, upper_32_bits(src_offset) & 0xff); 150 1.1 riastrad src_offset += cur_size_in_dw * 4; 151 1.1 riastrad dst_offset += cur_size_in_dw * 4; 152 1.1 riastrad } 153 1.1 riastrad 154 1.1 riastrad r = radeon_fence_emit(rdev, &fence, ring->idx); 155 1.1 riastrad if (r) { 156 1.1 riastrad radeon_ring_unlock_undo(rdev, ring); 157 1.1 riastrad radeon_sync_free(rdev, &sync, NULL); 158 1.1 riastrad return ERR_PTR(r); 159 1.1 riastrad } 160 1.1 riastrad 161 1.1 riastrad radeon_ring_unlock_commit(rdev, ring, false); 162 1.1 riastrad radeon_sync_free(rdev, &sync, fence); 163 1.1 riastrad 164 1.1 riastrad return fence; 165 1.1 riastrad } 166 1.1 riastrad 167 1.1 riastrad /** 168 1.1 riastrad * evergreen_dma_is_lockup - Check if the DMA engine is locked up 169 1.1 riastrad * 170 1.1 riastrad * @rdev: radeon_device pointer 171 1.1 riastrad * @ring: radeon_ring structure holding ring information 172 1.1 riastrad * 173 1.1 riastrad * Check if the async DMA engine is locked up. 174 1.1 riastrad * Returns true if the engine appears to be locked up, false if not. 175 1.1 riastrad */ 176 1.1 riastrad bool evergreen_dma_is_lockup(struct radeon_device *rdev, struct radeon_ring *ring) 177 1.1 riastrad { 178 1.1 riastrad u32 reset_mask = evergreen_gpu_check_soft_reset(rdev); 179 1.1 riastrad 180 1.1 riastrad if (!(reset_mask & RADEON_RESET_DMA)) { 181 1.1 riastrad radeon_ring_lockup_update(rdev, ring); 182 1.1 riastrad return false; 183 1.1 riastrad } 184 1.1 riastrad return radeon_ring_test_lockup(rdev, ring); 185 1.1 riastrad } 186 1.1 riastrad 187 1.1 riastrad 188