2 * Copyright 2017 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_ids.h"
25 #include <linux/idr.h>
26 #include <linux/dma-fence-array.h>
30 #include "amdgpu_trace.h"
35 * PASIDs are global address space identifiers that can be shared
36 * between the GPU, an IOMMU and the driver. VMs on different devices
37 * may use the same PASID if they share the same address
38 * space. Therefore PASIDs are allocated using a global IDA. VMs are
39 * looked up from the PASID per amdgpu_device.
41 static DEFINE_IDA(amdgpu_pasid_ida);
43 /* Helper to free pasid from a fence callback */
44 struct amdgpu_pasid_cb {
45 struct dma_fence_cb cb;
50 * amdgpu_pasid_alloc - Allocate a PASID
51 * @bits: Maximum width of the PASID in bits, must be at least 1
53 * Allocates a PASID of the given width while keeping smaller PASIDs
54 * available if possible.
56 * Returns a positive integer on success. Returns %-EINVAL if bits==0.
57 * Returns %-ENOSPC if no PASID was available. Returns %-ENOMEM on
58 * memory allocation failure.
60 int amdgpu_pasid_alloc(unsigned int bits)
64 for (bits = min(bits, 31U); bits > 0; bits--) {
65 pasid = ida_simple_get(&amdgpu_pasid_ida,
66 1U << (bits - 1), 1U << bits,
73 trace_amdgpu_pasid_allocated(pasid);
79 * amdgpu_pasid_free - Free a PASID
80 * @pasid: PASID to free
82 void amdgpu_pasid_free(u32 pasid)
84 trace_amdgpu_pasid_freed(pasid);
85 ida_simple_remove(&amdgpu_pasid_ida, pasid);
88 static void amdgpu_pasid_free_cb(struct dma_fence *fence,
89 struct dma_fence_cb *_cb)
91 struct amdgpu_pasid_cb *cb =
92 container_of(_cb, struct amdgpu_pasid_cb, cb);
94 amdgpu_pasid_free(cb->pasid);
100 * amdgpu_pasid_free_delayed - free pasid when fences signal
102 * @resv: reservation object with the fences to wait for
103 * @pasid: pasid to free
105 * Free the pasid only after all the fences in resv are signaled.
107 void amdgpu_pasid_free_delayed(struct dma_resv *resv,
110 struct amdgpu_pasid_cb *cb;
111 struct dma_fence *fence;
114 r = dma_resv_get_singleton(resv, DMA_RESV_USAGE_BOOKKEEP, &fence);
119 amdgpu_pasid_free(pasid);
123 cb = kmalloc(sizeof(*cb), GFP_KERNEL);
125 /* Last resort when we are OOM */
126 dma_fence_wait(fence, false);
127 dma_fence_put(fence);
128 amdgpu_pasid_free(pasid);
131 if (dma_fence_add_callback(fence, &cb->cb,
132 amdgpu_pasid_free_cb))
133 amdgpu_pasid_free_cb(fence, &cb->cb);
139 /* Not enough memory for the delayed delete, as last resort
140 * block for all the fences to complete.
142 dma_resv_wait_timeout(resv, DMA_RESV_USAGE_BOOKKEEP,
143 false, MAX_SCHEDULE_TIMEOUT);
144 amdgpu_pasid_free(pasid);
150 * VMIDs are a per VMHUB identifier for page tables handling.
154 * amdgpu_vmid_had_gpu_reset - check if reset occured since last use
156 * @adev: amdgpu_device pointer
157 * @id: VMID structure
159 * Check if GPU reset occured since last use of the VMID.
161 bool amdgpu_vmid_had_gpu_reset(struct amdgpu_device *adev,
162 struct amdgpu_vmid *id)
164 return id->current_gpu_reset_count !=
165 atomic_read(&adev->gpu_reset_counter);
168 /* Check if we need to switch to another set of resources */
169 static bool amdgpu_vmid_gds_switch_needed(struct amdgpu_vmid *id,
170 struct amdgpu_job *job)
172 return id->gds_base != job->gds_base ||
173 id->gds_size != job->gds_size ||
174 id->gws_base != job->gws_base ||
175 id->gws_size != job->gws_size ||
176 id->oa_base != job->oa_base ||
177 id->oa_size != job->oa_size;
180 /* Check if the id is compatible with the job */
181 static bool amdgpu_vmid_compatible(struct amdgpu_vmid *id,
182 struct amdgpu_job *job)
184 return id->pd_gpu_addr == job->vm_pd_addr &&
185 !amdgpu_vmid_gds_switch_needed(id, job);
189 * amdgpu_vmid_grab_idle - grab idle VMID
191 * @vm: vm to allocate id for
192 * @ring: ring we want to submit job to
193 * @idle: resulting idle VMID
194 * @fence: fence to wait for if no id could be grabbed
196 * Try to find an idle VMID, if none is idle add a fence to wait to the sync
197 * object. Returns -ENOMEM when we are out of memory.
199 static int amdgpu_vmid_grab_idle(struct amdgpu_vm *vm,
200 struct amdgpu_ring *ring,
201 struct amdgpu_vmid **idle,
202 struct dma_fence **fence)
204 struct amdgpu_device *adev = ring->adev;
205 unsigned vmhub = ring->vm_hub;
206 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
207 struct dma_fence **fences;
210 if (!dma_fence_is_signaled(ring->vmid_wait)) {
211 *fence = dma_fence_get(ring->vmid_wait);
215 fences = kmalloc_array(id_mgr->num_ids, sizeof(void *), GFP_KERNEL);
219 /* Check if we have an idle VMID */
221 list_for_each_entry((*idle), &id_mgr->ids_lru, list) {
222 /* Don't use per engine and per process VMID at the same time */
223 struct amdgpu_ring *r = adev->vm_manager.concurrent_flush ?
226 fences[i] = amdgpu_sync_peek_fence(&(*idle)->active, r);
232 /* If we can't find a idle VMID to use, wait till one becomes available */
233 if (&(*idle)->list == &id_mgr->ids_lru) {
234 u64 fence_context = adev->vm_manager.fence_context + ring->idx;
235 unsigned seqno = ++adev->vm_manager.seqno[ring->idx];
236 struct dma_fence_array *array;
240 for (j = 0; j < i; ++j)
241 dma_fence_get(fences[j]);
243 array = dma_fence_array_create(i, fences, fence_context,
246 for (j = 0; j < i; ++j)
247 dma_fence_put(fences[j]);
252 *fence = dma_fence_get(&array->base);
253 dma_fence_put(ring->vmid_wait);
254 ring->vmid_wait = &array->base;
263 * amdgpu_vmid_grab_reserved - try to assign reserved VMID
265 * @vm: vm to allocate id for
266 * @ring: ring we want to submit job to
267 * @job: job who wants to use the VMID
268 * @id: resulting VMID
269 * @fence: fence to wait for if no id could be grabbed
271 * Try to assign a reserved VMID.
273 static int amdgpu_vmid_grab_reserved(struct amdgpu_vm *vm,
274 struct amdgpu_ring *ring,
275 struct amdgpu_job *job,
276 struct amdgpu_vmid **id,
277 struct dma_fence **fence)
279 struct amdgpu_device *adev = ring->adev;
280 unsigned vmhub = ring->vm_hub;
281 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
282 uint64_t fence_context = adev->fence_context + ring->idx;
283 bool needs_flush = vm->use_cpu_for_update;
284 uint64_t updates = amdgpu_vm_tlb_seq(vm);
287 *id = id_mgr->reserved;
288 if ((*id)->owner != vm->immediate.fence_context ||
289 !amdgpu_vmid_compatible(*id, job) ||
290 (*id)->flushed_updates < updates ||
291 !(*id)->last_flush ||
292 ((*id)->last_flush->context != fence_context &&
293 !dma_fence_is_signaled((*id)->last_flush))) {
294 struct dma_fence *tmp;
296 /* Don't use per engine and per process VMID at the same time */
297 if (adev->vm_manager.concurrent_flush)
300 /* to prevent one context starved by another context */
301 (*id)->pd_gpu_addr = 0;
302 tmp = amdgpu_sync_peek_fence(&(*id)->active, ring);
305 *fence = dma_fence_get(tmp);
311 /* Good we can use this VMID. Remember this submission as
314 r = amdgpu_sync_fence(&(*id)->active, &job->base.s_fence->finished);
318 job->vm_needs_flush = needs_flush;
319 job->spm_update_needed = true;
324 * amdgpu_vmid_grab_used - try to reuse a VMID
326 * @vm: vm to allocate id for
327 * @ring: ring we want to submit job to
328 * @job: job who wants to use the VMID
329 * @id: resulting VMID
330 * @fence: fence to wait for if no id could be grabbed
332 * Try to reuse a VMID for this submission.
334 static int amdgpu_vmid_grab_used(struct amdgpu_vm *vm,
335 struct amdgpu_ring *ring,
336 struct amdgpu_job *job,
337 struct amdgpu_vmid **id,
338 struct dma_fence **fence)
340 struct amdgpu_device *adev = ring->adev;
341 unsigned vmhub = ring->vm_hub;
342 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
343 uint64_t fence_context = adev->fence_context + ring->idx;
344 uint64_t updates = amdgpu_vm_tlb_seq(vm);
347 job->vm_needs_flush = vm->use_cpu_for_update;
349 /* Check if we can use a VMID already assigned to this VM */
350 list_for_each_entry_reverse((*id), &id_mgr->ids_lru, list) {
351 bool needs_flush = vm->use_cpu_for_update;
353 /* Check all the prerequisites to using this VMID */
354 if ((*id)->owner != vm->immediate.fence_context)
357 if (!amdgpu_vmid_compatible(*id, job))
360 if (!(*id)->last_flush ||
361 ((*id)->last_flush->context != fence_context &&
362 !dma_fence_is_signaled((*id)->last_flush)))
365 if ((*id)->flushed_updates < updates)
368 if (needs_flush && !adev->vm_manager.concurrent_flush)
371 /* Good, we can use this VMID. Remember this submission as
374 r = amdgpu_sync_fence(&(*id)->active,
375 &job->base.s_fence->finished);
379 job->vm_needs_flush |= needs_flush;
388 * amdgpu_vmid_grab - allocate the next free VMID
390 * @vm: vm to allocate id for
391 * @ring: ring we want to submit job to
392 * @job: job who wants to use the VMID
393 * @fence: fence to wait for if no id could be grabbed
395 * Allocate an id for the vm, adding fences to the sync obj as necessary.
397 int amdgpu_vmid_grab(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
398 struct amdgpu_job *job, struct dma_fence **fence)
400 struct amdgpu_device *adev = ring->adev;
401 unsigned vmhub = ring->vm_hub;
402 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
403 struct amdgpu_vmid *idle = NULL;
404 struct amdgpu_vmid *id = NULL;
407 mutex_lock(&id_mgr->lock);
408 r = amdgpu_vmid_grab_idle(vm, ring, &idle, fence);
412 if (vm->reserved_vmid[vmhub]) {
413 r = amdgpu_vmid_grab_reserved(vm, ring, job, &id, fence);
417 r = amdgpu_vmid_grab_used(vm, ring, job, &id, fence);
422 /* Still no ID to use? Then use the idle one found earlier */
425 /* Remember this submission as user of the VMID */
426 r = amdgpu_sync_fence(&id->active,
427 &job->base.s_fence->finished);
431 job->vm_needs_flush = true;
434 list_move_tail(&id->list, &id_mgr->ids_lru);
437 job->gds_switch_needed = amdgpu_vmid_gds_switch_needed(id, job);
438 if (job->vm_needs_flush) {
439 id->flushed_updates = amdgpu_vm_tlb_seq(vm);
440 dma_fence_put(id->last_flush);
441 id->last_flush = NULL;
443 job->vmid = id - id_mgr->ids;
444 job->pasid = vm->pasid;
446 id->gds_base = job->gds_base;
447 id->gds_size = job->gds_size;
448 id->gws_base = job->gws_base;
449 id->gws_size = job->gws_size;
450 id->oa_base = job->oa_base;
451 id->oa_size = job->oa_size;
452 id->pd_gpu_addr = job->vm_pd_addr;
453 id->owner = vm->immediate.fence_context;
455 trace_amdgpu_vm_grab_id(vm, ring, job);
458 mutex_unlock(&id_mgr->lock);
462 int amdgpu_vmid_alloc_reserved(struct amdgpu_device *adev,
463 struct amdgpu_vm *vm,
466 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
468 mutex_lock(&id_mgr->lock);
469 if (vm->reserved_vmid[vmhub])
472 ++id_mgr->reserved_use_count;
473 if (!id_mgr->reserved) {
474 struct amdgpu_vmid *id;
476 id = list_first_entry(&id_mgr->ids_lru, struct amdgpu_vmid,
478 /* Remove from normal round robin handling */
479 list_del_init(&id->list);
480 id_mgr->reserved = id;
482 vm->reserved_vmid[vmhub] = true;
485 mutex_unlock(&id_mgr->lock);
489 void amdgpu_vmid_free_reserved(struct amdgpu_device *adev,
490 struct amdgpu_vm *vm,
493 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
495 mutex_lock(&id_mgr->lock);
496 if (vm->reserved_vmid[vmhub] &&
497 !--id_mgr->reserved_use_count) {
498 /* give the reserved ID back to normal round robin */
499 list_add(&id_mgr->reserved->list, &id_mgr->ids_lru);
500 id_mgr->reserved = NULL;
502 vm->reserved_vmid[vmhub] = false;
503 mutex_unlock(&id_mgr->lock);
507 * amdgpu_vmid_reset - reset VMID to zero
509 * @adev: amdgpu device structure
511 * @vmid: vmid number to use
513 * Reset saved GDW, GWS and OA to force switch on next flush.
515 void amdgpu_vmid_reset(struct amdgpu_device *adev, unsigned vmhub,
518 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
519 struct amdgpu_vmid *id = &id_mgr->ids[vmid];
521 mutex_lock(&id_mgr->lock);
529 mutex_unlock(&id_mgr->lock);
533 * amdgpu_vmid_reset_all - reset VMID to zero
535 * @adev: amdgpu device structure
537 * Reset VMID to force flush on next use
539 void amdgpu_vmid_reset_all(struct amdgpu_device *adev)
543 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
544 struct amdgpu_vmid_mgr *id_mgr =
545 &adev->vm_manager.id_mgr[i];
547 for (j = 1; j < id_mgr->num_ids; ++j)
548 amdgpu_vmid_reset(adev, i, j);
553 * amdgpu_vmid_mgr_init - init the VMID manager
555 * @adev: amdgpu_device pointer
557 * Initialize the VM manager structures
559 void amdgpu_vmid_mgr_init(struct amdgpu_device *adev)
563 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
564 struct amdgpu_vmid_mgr *id_mgr =
565 &adev->vm_manager.id_mgr[i];
567 mutex_init(&id_mgr->lock);
568 INIT_LIST_HEAD(&id_mgr->ids_lru);
569 id_mgr->reserved_use_count = 0;
571 /* manage only VMIDs not used by KFD */
572 id_mgr->num_ids = adev->vm_manager.first_kfd_vmid;
574 /* skip over VMID 0, since it is the system VM */
575 for (j = 1; j < id_mgr->num_ids; ++j) {
576 amdgpu_vmid_reset(adev, i, j);
577 amdgpu_sync_create(&id_mgr->ids[j].active);
578 list_add_tail(&id_mgr->ids[j].list, &id_mgr->ids_lru);
584 * amdgpu_vmid_mgr_fini - cleanup VM manager
586 * @adev: amdgpu_device pointer
588 * Cleanup the VM manager and free resources.
590 void amdgpu_vmid_mgr_fini(struct amdgpu_device *adev)
594 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
595 struct amdgpu_vmid_mgr *id_mgr =
596 &adev->vm_manager.id_mgr[i];
598 mutex_destroy(&id_mgr->lock);
599 for (j = 0; j < AMDGPU_NUM_VMID; ++j) {
600 struct amdgpu_vmid *id = &id_mgr->ids[j];
602 amdgpu_sync_free(&id->active);
603 dma_fence_put(id->last_flush);
604 dma_fence_put(id->pasid_mapping);