1 /* 2 * Copyright 2019 Advanced Micro Devices, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 */ 22 23 #include "amdgpu_vm.h" 24 #include "amdgpu_vm_internal.h" 25 #include "amdgpu_job.h" 26 #include "amdgpu_object.h" 27 #include "amdgpu_trace.h" 28 29 #define AMDGPU_VM_SDMA_MIN_NUM_DW 256u 30 #define AMDGPU_VM_SDMA_MAX_NUM_DW (16u * 1024u) 31 32 /** 33 * amdgpu_vm_sdma_map_table - make sure new PDs/PTs are GTT mapped 34 * 35 * @table: newly allocated or validated PD/PT 36 */ 37 static int amdgpu_vm_sdma_map_table(struct amdgpu_bo_vm *table) 38 { 39 return amdgpu_ttm_alloc_gart(&table->bo.tbo); 40 } 41 42 /* Allocate a new job for @count PTE updates */ 43 static int amdgpu_vm_sdma_alloc_job(struct amdgpu_vm_update_params *p, 44 unsigned int count, u64 k_job_id) 45 { 46 enum amdgpu_ib_pool_type pool = p->immediate ? AMDGPU_IB_POOL_IMMEDIATE 47 : AMDGPU_IB_POOL_DELAYED; 48 struct drm_sched_entity *entity = p->immediate ? &p->vm->immediate 49 : &p->vm->delayed; 50 unsigned int ndw; 51 int r; 52 53 /* estimate how many dw we need */ 54 ndw = AMDGPU_VM_SDMA_MIN_NUM_DW; 55 if (p->pages_addr) 56 ndw += count * 2; 57 ndw = min(ndw, AMDGPU_VM_SDMA_MAX_NUM_DW); 58 59 r = amdgpu_job_alloc_with_ib(p->adev, entity, AMDGPU_FENCE_OWNER_VM, 60 ndw * 4, pool, k_job_id, &p->job); 61 if (r) 62 return r; 63 64 p->num_dw_left = ndw; 65 return 0; 66 } 67 68 /** 69 * amdgpu_vm_sdma_prepare - prepare SDMA command submission 70 * 71 * @p: see amdgpu_vm_update_params definition 72 * @sync: amdgpu_sync object with fences to wait for 73 * @k_job_id: identifier of the job, for tracing purpose 74 * 75 * Returns: 76 * Negativ errno, 0 for success. 77 */ 78 static int amdgpu_vm_sdma_prepare(struct amdgpu_vm_update_params *p, 79 struct amdgpu_sync *sync, u64 k_job_id) 80 { 81 int r; 82 83 r = amdgpu_vm_sdma_alloc_job(p, 0, k_job_id); 84 if (r) 85 return r; 86 87 if (!sync) 88 return 0; 89 90 r = amdgpu_sync_push_to_job(sync, p->job); 91 if (r) { 92 p->num_dw_left = 0; 93 amdgpu_job_free(p->job); 94 } 95 return r; 96 } 97 98 /** 99 * amdgpu_vm_sdma_commit - commit SDMA command submission 100 * 101 * @p: see amdgpu_vm_update_params definition 102 * @fence: resulting fence 103 * 104 * Returns: 105 * Negativ errno, 0 for success. 106 */ 107 static int amdgpu_vm_sdma_commit(struct amdgpu_vm_update_params *p, 108 struct dma_fence **fence) 109 { 110 struct amdgpu_ib *ib = p->job->ibs; 111 struct amdgpu_ring *ring; 112 struct dma_fence *f; 113 114 ring = container_of(p->vm->delayed.rq->sched, struct amdgpu_ring, 115 sched); 116 117 WARN_ON(ib->length_dw == 0); 118 amdgpu_ring_pad_ib(ring, ib); 119 120 if (p->needs_flush) 121 atomic64_inc(&p->vm->tlb_seq); 122 123 WARN_ON(ib->length_dw > p->num_dw_left); 124 f = amdgpu_job_submit(p->job); 125 126 if (p->unlocked) { 127 struct dma_fence *tmp = dma_fence_get(f); 128 129 swap(p->vm->last_unlocked, tmp); 130 dma_fence_put(tmp); 131 } else { 132 dma_resv_add_fence(p->vm->root.bo->tbo.base.resv, f, 133 DMA_RESV_USAGE_BOOKKEEP); 134 } 135 136 if (fence && !p->immediate) { 137 /* 138 * Most hw generations now have a separate queue for page table 139 * updates, but when the queue is shared with userspace we need 140 * the extra CPU round trip to correctly flush the TLB. 141 */ 142 set_bit(DRM_SCHED_FENCE_DONT_PIPELINE, &f->flags); 143 swap(*fence, f); 144 } 145 dma_fence_put(f); 146 return 0; 147 } 148 149 /** 150 * amdgpu_vm_sdma_copy_ptes - copy the PTEs from mapping 151 * 152 * @p: see amdgpu_vm_update_params definition 153 * @bo: PD/PT to update 154 * @pe: addr of the page entry 155 * @count: number of page entries to copy 156 * 157 * Traces the parameters and calls the DMA function to copy the PTEs. 158 */ 159 static void amdgpu_vm_sdma_copy_ptes(struct amdgpu_vm_update_params *p, 160 struct amdgpu_bo *bo, uint64_t pe, 161 unsigned count) 162 { 163 struct amdgpu_ib *ib = p->job->ibs; 164 uint64_t src = ib->gpu_addr; 165 166 src += p->num_dw_left * 4; 167 168 pe += amdgpu_bo_gpu_offset_no_check(bo); 169 trace_amdgpu_vm_copy_ptes(pe, src, count, p->immediate); 170 171 amdgpu_vm_copy_pte(p->adev, ib, pe, src, count); 172 } 173 174 /** 175 * amdgpu_vm_sdma_set_ptes - helper to call the right asic function 176 * 177 * @p: see amdgpu_vm_update_params definition 178 * @bo: PD/PT to update 179 * @pe: byte offset of the PDE/PTE, relative to start of PDB/PTB 180 * @addr: dst addr to write into pe 181 * @count: number of page entries to update 182 * @incr: increase next addr by incr bytes 183 * @flags: hw access flags 184 * 185 * Traces the parameters and calls the right asic functions 186 * to setup the page table using the DMA. 187 */ 188 static void amdgpu_vm_sdma_set_ptes(struct amdgpu_vm_update_params *p, 189 struct amdgpu_bo *bo, uint64_t pe, 190 uint64_t addr, unsigned count, 191 uint32_t incr, uint64_t flags) 192 { 193 struct amdgpu_ib *ib = p->job->ibs; 194 195 pe += amdgpu_bo_gpu_offset_no_check(bo); 196 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags, p->immediate); 197 if (count < 3) { 198 amdgpu_vm_write_pte(p->adev, ib, pe, addr | flags, 199 count, incr); 200 } else { 201 amdgpu_vm_set_pte_pde(p->adev, ib, pe, addr, 202 count, incr, flags); 203 } 204 } 205 206 /** 207 * amdgpu_vm_sdma_update - execute VM update 208 * 209 * @p: see amdgpu_vm_update_params definition 210 * @vmbo: PD/PT to update 211 * @pe: byte offset of the PDE/PTE, relative to start of PDB/PTB 212 * @addr: dst addr to write into pe 213 * @count: number of page entries to update 214 * @incr: increase next addr by incr bytes 215 * @flags: hw access flags 216 * 217 * Reserve space in the IB, setup mapping buffer on demand and write commands to 218 * the IB. 219 */ 220 static int amdgpu_vm_sdma_update(struct amdgpu_vm_update_params *p, 221 struct amdgpu_bo_vm *vmbo, uint64_t pe, 222 uint64_t addr, unsigned count, uint32_t incr, 223 uint64_t flags) 224 { 225 struct amdgpu_bo *bo = &vmbo->bo; 226 struct dma_resv_iter cursor; 227 unsigned int i, ndw, nptes; 228 struct dma_fence *fence; 229 uint64_t *pte; 230 int r; 231 232 /* Wait for PD/PT moves to be completed */ 233 dma_resv_iter_begin(&cursor, bo->tbo.base.resv, DMA_RESV_USAGE_KERNEL); 234 dma_resv_for_each_fence_unlocked(&cursor, fence) { 235 dma_fence_get(fence); 236 r = drm_sched_job_add_dependency(&p->job->base, fence); 237 if (r) { 238 dma_fence_put(fence); 239 dma_resv_iter_end(&cursor); 240 return r; 241 } 242 } 243 dma_resv_iter_end(&cursor); 244 245 do { 246 ndw = p->num_dw_left; 247 ndw -= p->job->ibs->length_dw; 248 249 if (ndw < 32) { 250 r = amdgpu_vm_sdma_commit(p, NULL); 251 if (r) 252 return r; 253 254 r = amdgpu_vm_sdma_alloc_job(p, count, 255 AMDGPU_KERNEL_JOB_ID_VM_UPDATE); 256 if (r) 257 return r; 258 259 /* The estimate above describes the job which was just 260 * submitted, take the budget of the newly allocated one. 261 */ 262 ndw = p->num_dw_left; 263 } 264 265 if (!p->pages_addr) { 266 if (p->override_pte) 267 amdgpu_gmc_override_vm_pte_flags(p->adev, p->vm, addr, &flags); 268 269 /* set page commands needed */ 270 amdgpu_vm_sdma_set_ptes(p, bo, pe, addr, count, 271 incr, flags); 272 return 0; 273 } 274 275 /* copy commands needed */ 276 ndw -= p->adev->vm_manager.vm_pte_funcs->copy_pte_num_dw; 277 278 /* for padding */ 279 ndw -= 7; 280 281 nptes = min(count, ndw / 2); 282 283 /* Put the PTEs at the end of the IB. */ 284 p->num_dw_left -= nptes * 2; 285 pte = (uint64_t *)&(p->job->ibs->ptr[p->num_dw_left]); 286 for (i = 0; i < nptes; ++i, addr += incr) { 287 u64 oflags = flags; 288 289 pte[i] = amdgpu_vm_map_gart(p->pages_addr, addr); 290 291 if (p->override_pte) 292 amdgpu_gmc_override_vm_pte_flags(p->adev, p->vm, pte[i], &oflags); 293 294 pte[i] |= oflags; 295 } 296 297 amdgpu_vm_sdma_copy_ptes(p, bo, pe, nptes); 298 299 pe += nptes * 8; 300 count -= nptes; 301 } while (count); 302 303 return 0; 304 } 305 306 const struct amdgpu_vm_update_funcs amdgpu_vm_sdma_funcs = { 307 .map_table = amdgpu_vm_sdma_map_table, 308 .prepare = amdgpu_vm_sdma_prepare, 309 .update = amdgpu_vm_sdma_update, 310 .commit = amdgpu_vm_sdma_commit 311 }; 312