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.
23 #include "amdgpu_vm.h"
24 #include "amdgpu_job.h"
25 #include "amdgpu_object.h"
26 #include "amdgpu_trace.h"
28 #define AMDGPU_VM_SDMA_MIN_NUM_DW 256u
29 #define AMDGPU_VM_SDMA_MAX_NUM_DW (16u * 1024u)
32 * amdgpu_vm_sdma_map_table - make sure new PDs/PTs are GTT mapped
34 * @table: newly allocated or validated PD/PT
36 static int amdgpu_vm_sdma_map_table(struct amdgpu_bo_vm *table)
40 r = amdgpu_ttm_alloc_gart(&table->bo.tbo);
45 r = amdgpu_ttm_alloc_gart(&table->shadow->tbo);
50 /* Allocate a new job for @count PTE updates */
51 static int amdgpu_vm_sdma_alloc_job(struct amdgpu_vm_update_params *p,
54 enum amdgpu_ib_pool_type pool = p->immediate ? AMDGPU_IB_POOL_IMMEDIATE
55 : AMDGPU_IB_POOL_DELAYED;
56 struct drm_sched_entity *entity = p->immediate ? &p->vm->immediate
61 /* estimate how many dw we need */
62 ndw = AMDGPU_VM_SDMA_MIN_NUM_DW;
65 ndw = min(ndw, AMDGPU_VM_SDMA_MAX_NUM_DW);
67 r = amdgpu_job_alloc_with_ib(p->adev, entity, AMDGPU_FENCE_OWNER_VM,
68 ndw * 4, pool, &p->job);
77 * amdgpu_vm_sdma_prepare - prepare SDMA command submission
79 * @p: see amdgpu_vm_update_params definition
80 * @resv: reservation object with embedded fence
81 * @sync_mode: synchronization mode
84 * Negativ errno, 0 for success.
86 static int amdgpu_vm_sdma_prepare(struct amdgpu_vm_update_params *p,
87 struct dma_resv *resv,
88 enum amdgpu_sync_mode sync_mode)
90 struct amdgpu_sync sync;
93 r = amdgpu_vm_sdma_alloc_job(p, 0);
100 amdgpu_sync_create(&sync);
101 r = amdgpu_sync_resv(p->adev, &sync, resv, sync_mode, p->vm);
103 r = amdgpu_sync_push_to_job(&sync, p->job);
104 amdgpu_sync_free(&sync);
108 amdgpu_job_free(p->job);
114 * amdgpu_vm_sdma_commit - commit SDMA command submission
116 * @p: see amdgpu_vm_update_params definition
117 * @fence: resulting fence
120 * Negativ errno, 0 for success.
122 static int amdgpu_vm_sdma_commit(struct amdgpu_vm_update_params *p,
123 struct dma_fence **fence)
125 struct amdgpu_ib *ib = p->job->ibs;
126 struct amdgpu_ring *ring;
129 ring = container_of(p->vm->delayed.rq->sched, struct amdgpu_ring,
132 WARN_ON(ib->length_dw == 0);
133 amdgpu_ring_pad_ib(ring, ib);
136 atomic64_inc(&p->vm->tlb_seq);
138 WARN_ON(ib->length_dw > p->num_dw_left);
139 f = amdgpu_job_submit(p->job);
142 struct dma_fence *tmp = dma_fence_get(f);
144 swap(p->vm->last_unlocked, tmp);
147 dma_resv_add_fence(p->vm->root.bo->tbo.base.resv, f,
148 DMA_RESV_USAGE_BOOKKEEP);
151 if (fence && !p->immediate) {
153 * Most hw generations now have a separate queue for page table
154 * updates, but when the queue is shared with userspace we need
155 * the extra CPU round trip to correctly flush the TLB.
157 set_bit(DRM_SCHED_FENCE_DONT_PIPELINE, &f->flags);
165 * amdgpu_vm_sdma_copy_ptes - copy the PTEs from mapping
167 * @p: see amdgpu_vm_update_params definition
168 * @bo: PD/PT to update
169 * @pe: addr of the page entry
170 * @count: number of page entries to copy
172 * Traces the parameters and calls the DMA function to copy the PTEs.
174 static void amdgpu_vm_sdma_copy_ptes(struct amdgpu_vm_update_params *p,
175 struct amdgpu_bo *bo, uint64_t pe,
178 struct amdgpu_ib *ib = p->job->ibs;
179 uint64_t src = ib->gpu_addr;
181 src += p->num_dw_left * 4;
183 pe += amdgpu_bo_gpu_offset_no_check(bo);
184 trace_amdgpu_vm_copy_ptes(pe, src, count, p->immediate);
186 amdgpu_vm_copy_pte(p->adev, ib, pe, src, count);
190 * amdgpu_vm_sdma_set_ptes - helper to call the right asic function
192 * @p: see amdgpu_vm_update_params definition
193 * @bo: PD/PT to update
194 * @pe: byte offset of the PDE/PTE, relative to start of PDB/PTB
195 * @addr: dst addr to write into pe
196 * @count: number of page entries to update
197 * @incr: increase next addr by incr bytes
198 * @flags: hw access flags
200 * Traces the parameters and calls the right asic functions
201 * to setup the page table using the DMA.
203 static void amdgpu_vm_sdma_set_ptes(struct amdgpu_vm_update_params *p,
204 struct amdgpu_bo *bo, uint64_t pe,
205 uint64_t addr, unsigned count,
206 uint32_t incr, uint64_t flags)
208 struct amdgpu_ib *ib = p->job->ibs;
210 pe += amdgpu_bo_gpu_offset_no_check(bo);
211 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags, p->immediate);
213 amdgpu_vm_write_pte(p->adev, ib, pe, addr | flags,
216 amdgpu_vm_set_pte_pde(p->adev, ib, pe, addr,
222 * amdgpu_vm_sdma_update - execute VM update
224 * @p: see amdgpu_vm_update_params definition
225 * @vmbo: PD/PT to update
226 * @pe: byte offset of the PDE/PTE, relative to start of PDB/PTB
227 * @addr: dst addr to write into pe
228 * @count: number of page entries to update
229 * @incr: increase next addr by incr bytes
230 * @flags: hw access flags
232 * Reserve space in the IB, setup mapping buffer on demand and write commands to
235 static int amdgpu_vm_sdma_update(struct amdgpu_vm_update_params *p,
236 struct amdgpu_bo_vm *vmbo, uint64_t pe,
237 uint64_t addr, unsigned count, uint32_t incr,
240 struct amdgpu_bo *bo = &vmbo->bo;
241 struct dma_resv_iter cursor;
242 unsigned int i, ndw, nptes;
243 struct dma_fence *fence;
247 /* Wait for PD/PT moves to be completed */
248 dma_resv_iter_begin(&cursor, bo->tbo.base.resv, DMA_RESV_USAGE_KERNEL);
249 dma_resv_for_each_fence_unlocked(&cursor, fence) {
250 dma_fence_get(fence);
251 r = drm_sched_job_add_dependency(&p->job->base, fence);
253 dma_fence_put(fence);
254 dma_resv_iter_end(&cursor);
258 dma_resv_iter_end(&cursor);
261 ndw = p->num_dw_left;
262 ndw -= p->job->ibs->length_dw;
265 r = amdgpu_vm_sdma_commit(p, NULL);
269 r = amdgpu_vm_sdma_alloc_job(p, count);
274 if (!p->pages_addr) {
275 /* set page commands needed */
277 amdgpu_vm_sdma_set_ptes(p, vmbo->shadow, pe, addr,
279 amdgpu_vm_sdma_set_ptes(p, bo, pe, addr, count,
284 /* copy commands needed */
285 ndw -= p->adev->vm_manager.vm_pte_funcs->copy_pte_num_dw *
286 (vmbo->shadow ? 2 : 1);
291 nptes = min(count, ndw / 2);
293 /* Put the PTEs at the end of the IB. */
294 p->num_dw_left -= nptes * 2;
295 pte = (uint64_t *)&(p->job->ibs->ptr[p->num_dw_left]);
296 for (i = 0; i < nptes; ++i, addr += incr) {
297 pte[i] = amdgpu_vm_map_gart(p->pages_addr, addr);
302 amdgpu_vm_sdma_copy_ptes(p, vmbo->shadow, pe, nptes);
303 amdgpu_vm_sdma_copy_ptes(p, bo, pe, nptes);
312 const struct amdgpu_vm_update_funcs amdgpu_vm_sdma_funcs = {
313 .map_table = amdgpu_vm_sdma_map_table,
314 .prepare = amdgpu_vm_sdma_prepare,
315 .update = amdgpu_vm_sdma_update,
316 .commit = amdgpu_vm_sdma_commit