1 1.1 riastrad /* $NetBSD: radeon_si_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 2013 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_si_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 "radeon_trace.h" 33 1.1 riastrad #include "sid.h" 34 1.1 riastrad 35 1.1 riastrad u32 si_gpu_check_soft_reset(struct radeon_device *rdev); 36 1.1 riastrad 37 1.1 riastrad /** 38 1.1 riastrad * si_dma_is_lockup - Check if the DMA engine is locked up 39 1.1 riastrad * 40 1.1 riastrad * @rdev: radeon_device pointer 41 1.1 riastrad * @ring: radeon_ring structure holding ring information 42 1.1 riastrad * 43 1.1 riastrad * Check if the async DMA engine is locked up. 44 1.1 riastrad * Returns true if the engine appears to be locked up, false if not. 45 1.1 riastrad */ 46 1.1 riastrad bool si_dma_is_lockup(struct radeon_device *rdev, struct radeon_ring *ring) 47 1.1 riastrad { 48 1.1 riastrad u32 reset_mask = si_gpu_check_soft_reset(rdev); 49 1.1 riastrad u32 mask; 50 1.1 riastrad 51 1.1 riastrad if (ring->idx == R600_RING_TYPE_DMA_INDEX) 52 1.1 riastrad mask = RADEON_RESET_DMA; 53 1.1 riastrad else 54 1.1 riastrad mask = RADEON_RESET_DMA1; 55 1.1 riastrad 56 1.1 riastrad if (!(reset_mask & mask)) { 57 1.1 riastrad radeon_ring_lockup_update(rdev, ring); 58 1.1 riastrad return false; 59 1.1 riastrad } 60 1.1 riastrad return radeon_ring_test_lockup(rdev, ring); 61 1.1 riastrad } 62 1.1 riastrad 63 1.1 riastrad /** 64 1.1 riastrad * si_dma_vm_copy_pages - update PTEs by copying them from the GART 65 1.1 riastrad * 66 1.1 riastrad * @rdev: radeon_device pointer 67 1.1 riastrad * @ib: indirect buffer to fill with commands 68 1.1 riastrad * @pe: addr of the page entry 69 1.1 riastrad * @src: src addr where to copy from 70 1.1 riastrad * @count: number of page entries to update 71 1.1 riastrad * 72 1.1 riastrad * Update PTEs by copying them from the GART using the DMA (SI). 73 1.1 riastrad */ 74 1.1 riastrad void si_dma_vm_copy_pages(struct radeon_device *rdev, 75 1.1 riastrad struct radeon_ib *ib, 76 1.1 riastrad uint64_t pe, uint64_t src, 77 1.1 riastrad unsigned count) 78 1.1 riastrad { 79 1.1 riastrad while (count) { 80 1.1 riastrad unsigned bytes = count * 8; 81 1.1 riastrad if (bytes > 0xFFFF8) 82 1.1 riastrad bytes = 0xFFFF8; 83 1.1 riastrad 84 1.1 riastrad ib->ptr[ib->length_dw++] = DMA_PACKET(DMA_PACKET_COPY, 85 1.1 riastrad 1, 0, 0, bytes); 86 1.1 riastrad ib->ptr[ib->length_dw++] = lower_32_bits(pe); 87 1.1 riastrad ib->ptr[ib->length_dw++] = lower_32_bits(src); 88 1.1 riastrad ib->ptr[ib->length_dw++] = upper_32_bits(pe) & 0xff; 89 1.1 riastrad ib->ptr[ib->length_dw++] = upper_32_bits(src) & 0xff; 90 1.1 riastrad 91 1.1 riastrad pe += bytes; 92 1.1 riastrad src += bytes; 93 1.1 riastrad count -= bytes / 8; 94 1.1 riastrad } 95 1.1 riastrad } 96 1.1 riastrad 97 1.1 riastrad /** 98 1.1 riastrad * si_dma_vm_write_pages - update PTEs by writing them manually 99 1.1 riastrad * 100 1.1 riastrad * @rdev: radeon_device pointer 101 1.1 riastrad * @ib: indirect buffer to fill with commands 102 1.1 riastrad * @pe: addr of the page entry 103 1.1 riastrad * @addr: dst addr to write into pe 104 1.1 riastrad * @count: number of page entries to update 105 1.1 riastrad * @incr: increase next addr by incr bytes 106 1.1 riastrad * @flags: access flags 107 1.1 riastrad * 108 1.1 riastrad * Update PTEs by writing them manually using the DMA (SI). 109 1.1 riastrad */ 110 1.1 riastrad void si_dma_vm_write_pages(struct radeon_device *rdev, 111 1.1 riastrad struct radeon_ib *ib, 112 1.1 riastrad uint64_t pe, 113 1.1 riastrad uint64_t addr, unsigned count, 114 1.1 riastrad uint32_t incr, uint32_t flags) 115 1.1 riastrad { 116 1.1 riastrad uint64_t value; 117 1.1 riastrad unsigned ndw; 118 1.1 riastrad 119 1.1 riastrad while (count) { 120 1.1 riastrad ndw = count * 2; 121 1.1 riastrad if (ndw > 0xFFFFE) 122 1.1 riastrad ndw = 0xFFFFE; 123 1.1 riastrad 124 1.1 riastrad /* for non-physically contiguous pages (system) */ 125 1.1 riastrad ib->ptr[ib->length_dw++] = DMA_PACKET(DMA_PACKET_WRITE, 0, 0, 0, ndw); 126 1.1 riastrad ib->ptr[ib->length_dw++] = pe; 127 1.1 riastrad ib->ptr[ib->length_dw++] = upper_32_bits(pe) & 0xff; 128 1.1 riastrad for (; ndw > 0; ndw -= 2, --count, pe += 8) { 129 1.1 riastrad if (flags & R600_PTE_SYSTEM) { 130 1.1 riastrad value = radeon_vm_map_gart(rdev, addr); 131 1.1 riastrad } else if (flags & R600_PTE_VALID) { 132 1.1 riastrad value = addr; 133 1.1 riastrad } else { 134 1.1 riastrad value = 0; 135 1.1 riastrad } 136 1.1 riastrad addr += incr; 137 1.1 riastrad value |= flags; 138 1.1 riastrad ib->ptr[ib->length_dw++] = value; 139 1.1 riastrad ib->ptr[ib->length_dw++] = upper_32_bits(value); 140 1.1 riastrad } 141 1.1 riastrad } 142 1.1 riastrad } 143 1.1 riastrad 144 1.1 riastrad /** 145 1.1 riastrad * si_dma_vm_set_pages - update the page tables using the DMA 146 1.1 riastrad * 147 1.1 riastrad * @rdev: radeon_device pointer 148 1.1 riastrad * @ib: indirect buffer to fill with commands 149 1.1 riastrad * @pe: addr of the page entry 150 1.1 riastrad * @addr: dst addr to write into pe 151 1.1 riastrad * @count: number of page entries to update 152 1.1 riastrad * @incr: increase next addr by incr bytes 153 1.1 riastrad * @flags: access flags 154 1.1 riastrad * 155 1.1 riastrad * Update the page tables using the DMA (SI). 156 1.1 riastrad */ 157 1.1 riastrad void si_dma_vm_set_pages(struct radeon_device *rdev, 158 1.1 riastrad struct radeon_ib *ib, 159 1.1 riastrad uint64_t pe, 160 1.1 riastrad uint64_t addr, unsigned count, 161 1.1 riastrad uint32_t incr, uint32_t flags) 162 1.1 riastrad { 163 1.1 riastrad uint64_t value; 164 1.1 riastrad unsigned ndw; 165 1.1 riastrad 166 1.1 riastrad while (count) { 167 1.1 riastrad ndw = count * 2; 168 1.1 riastrad if (ndw > 0xFFFFE) 169 1.1 riastrad ndw = 0xFFFFE; 170 1.1 riastrad 171 1.1 riastrad if (flags & R600_PTE_VALID) 172 1.1 riastrad value = addr; 173 1.1 riastrad else 174 1.1 riastrad value = 0; 175 1.1 riastrad 176 1.1 riastrad /* for physically contiguous pages (vram) */ 177 1.1 riastrad ib->ptr[ib->length_dw++] = DMA_PTE_PDE_PACKET(ndw); 178 1.1 riastrad ib->ptr[ib->length_dw++] = pe; /* dst addr */ 179 1.1 riastrad ib->ptr[ib->length_dw++] = upper_32_bits(pe) & 0xff; 180 1.1 riastrad ib->ptr[ib->length_dw++] = flags; /* mask */ 181 1.1 riastrad ib->ptr[ib->length_dw++] = 0; 182 1.1 riastrad ib->ptr[ib->length_dw++] = value; /* value */ 183 1.1 riastrad ib->ptr[ib->length_dw++] = upper_32_bits(value); 184 1.1 riastrad ib->ptr[ib->length_dw++] = incr; /* increment size */ 185 1.1 riastrad ib->ptr[ib->length_dw++] = 0; 186 1.1 riastrad pe += ndw * 4; 187 1.1 riastrad addr += (ndw / 2) * incr; 188 1.1 riastrad count -= ndw / 2; 189 1.1 riastrad } 190 1.1 riastrad } 191 1.1 riastrad 192 1.1 riastrad void si_dma_vm_flush(struct radeon_device *rdev, struct radeon_ring *ring, 193 1.1 riastrad unsigned vm_id, uint64_t pd_addr) 194 1.1 riastrad 195 1.1 riastrad { 196 1.1 riastrad radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_SRBM_WRITE, 0, 0, 0, 0)); 197 1.1 riastrad if (vm_id < 8) { 198 1.1 riastrad radeon_ring_write(ring, (0xf << 16) | ((VM_CONTEXT0_PAGE_TABLE_BASE_ADDR + (vm_id << 2)) >> 2)); 199 1.1 riastrad } else { 200 1.1 riastrad radeon_ring_write(ring, (0xf << 16) | ((VM_CONTEXT8_PAGE_TABLE_BASE_ADDR + ((vm_id - 8) << 2)) >> 2)); 201 1.1 riastrad } 202 1.1 riastrad radeon_ring_write(ring, pd_addr >> 12); 203 1.1 riastrad 204 1.1 riastrad /* flush hdp cache */ 205 1.1 riastrad radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_SRBM_WRITE, 0, 0, 0, 0)); 206 1.1 riastrad radeon_ring_write(ring, (0xf << 16) | (HDP_MEM_COHERENCY_FLUSH_CNTL >> 2)); 207 1.1 riastrad radeon_ring_write(ring, 1); 208 1.1 riastrad 209 1.1 riastrad /* bits 0-7 are the VM contexts0-7 */ 210 1.1 riastrad radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_SRBM_WRITE, 0, 0, 0, 0)); 211 1.1 riastrad radeon_ring_write(ring, (0xf << 16) | (VM_INVALIDATE_REQUEST >> 2)); 212 1.1 riastrad radeon_ring_write(ring, 1 << vm_id); 213 1.1 riastrad 214 1.1 riastrad /* wait for invalidate to complete */ 215 1.1 riastrad radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_POLL_REG_MEM, 0, 0, 0, 0)); 216 1.1 riastrad radeon_ring_write(ring, VM_INVALIDATE_REQUEST); 217 1.1 riastrad radeon_ring_write(ring, 0xff << 16); /* retry */ 218 1.1 riastrad radeon_ring_write(ring, 1 << vm_id); /* mask */ 219 1.1 riastrad radeon_ring_write(ring, 0); /* value */ 220 1.1 riastrad radeon_ring_write(ring, (0 << 28) | 0x20); /* func(always) | poll interval */ 221 1.1 riastrad } 222 1.1 riastrad 223 1.1 riastrad /** 224 1.1 riastrad * si_copy_dma - copy pages using the DMA engine 225 1.1 riastrad * 226 1.1 riastrad * @rdev: radeon_device pointer 227 1.1 riastrad * @src_offset: src GPU address 228 1.1 riastrad * @dst_offset: dst GPU address 229 1.1 riastrad * @num_gpu_pages: number of GPU pages to xfer 230 1.1 riastrad * @resv: reservation object to sync to 231 1.1 riastrad * 232 1.1 riastrad * Copy GPU paging using the DMA engine (SI). 233 1.1 riastrad * Used by the radeon ttm implementation to move pages if 234 1.1 riastrad * registered as the asic copy callback. 235 1.1 riastrad */ 236 1.1 riastrad struct radeon_fence *si_copy_dma(struct radeon_device *rdev, 237 1.1 riastrad uint64_t src_offset, uint64_t dst_offset, 238 1.1 riastrad unsigned num_gpu_pages, 239 1.2 riastrad struct dma_resv *resv) 240 1.1 riastrad { 241 1.1 riastrad struct radeon_fence *fence; 242 1.1 riastrad struct radeon_sync sync; 243 1.1 riastrad int ring_index = rdev->asic->copy.dma_ring_index; 244 1.1 riastrad struct radeon_ring *ring = &rdev->ring[ring_index]; 245 1.1 riastrad u32 size_in_bytes, cur_size_in_bytes; 246 1.1 riastrad int i, num_loops; 247 1.1 riastrad int r = 0; 248 1.1 riastrad 249 1.1 riastrad radeon_sync_create(&sync); 250 1.1 riastrad 251 1.1 riastrad size_in_bytes = (num_gpu_pages << RADEON_GPU_PAGE_SHIFT); 252 1.1 riastrad num_loops = DIV_ROUND_UP(size_in_bytes, 0xfffff); 253 1.1 riastrad r = radeon_ring_lock(rdev, ring, num_loops * 5 + 11); 254 1.1 riastrad if (r) { 255 1.1 riastrad DRM_ERROR("radeon: moving bo (%d).\n", r); 256 1.1 riastrad radeon_sync_free(rdev, &sync, NULL); 257 1.1 riastrad return ERR_PTR(r); 258 1.1 riastrad } 259 1.1 riastrad 260 1.1 riastrad radeon_sync_resv(rdev, &sync, resv, false); 261 1.1 riastrad radeon_sync_rings(rdev, &sync, ring->idx); 262 1.1 riastrad 263 1.1 riastrad for (i = 0; i < num_loops; i++) { 264 1.1 riastrad cur_size_in_bytes = size_in_bytes; 265 1.1 riastrad if (cur_size_in_bytes > 0xFFFFF) 266 1.1 riastrad cur_size_in_bytes = 0xFFFFF; 267 1.1 riastrad size_in_bytes -= cur_size_in_bytes; 268 1.1 riastrad radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_COPY, 1, 0, 0, cur_size_in_bytes)); 269 1.1 riastrad radeon_ring_write(ring, lower_32_bits(dst_offset)); 270 1.1 riastrad radeon_ring_write(ring, lower_32_bits(src_offset)); 271 1.1 riastrad radeon_ring_write(ring, upper_32_bits(dst_offset) & 0xff); 272 1.1 riastrad radeon_ring_write(ring, upper_32_bits(src_offset) & 0xff); 273 1.1 riastrad src_offset += cur_size_in_bytes; 274 1.1 riastrad dst_offset += cur_size_in_bytes; 275 1.1 riastrad } 276 1.1 riastrad 277 1.1 riastrad r = radeon_fence_emit(rdev, &fence, ring->idx); 278 1.1 riastrad if (r) { 279 1.1 riastrad radeon_ring_unlock_undo(rdev, ring); 280 1.1 riastrad radeon_sync_free(rdev, &sync, NULL); 281 1.1 riastrad return ERR_PTR(r); 282 1.1 riastrad } 283 1.1 riastrad 284 1.1 riastrad radeon_ring_unlock_commit(rdev, ring, false); 285 1.1 riastrad radeon_sync_free(rdev, &sync, fence); 286 1.1 riastrad 287 1.1 riastrad return fence; 288 1.1 riastrad } 289 1.1 riastrad 290