1 // SPDX-License-Identifier: MIT
3 * Copyright 2014-2018 Advanced Micro Devices, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
23 #include <linux/dma-buf.h>
24 #include <linux/list.h>
25 #include <linux/pagemap.h>
26 #include <linux/sched/mm.h>
27 #include <linux/sched/task.h>
28 #include <drm/ttm/ttm_tt.h>
30 #include <drm/drm_exec.h>
32 #include "amdgpu_object.h"
33 #include "amdgpu_gem.h"
34 #include "amdgpu_vm.h"
35 #include "amdgpu_hmm.h"
36 #include "amdgpu_amdkfd.h"
37 #include "amdgpu_dma_buf.h"
38 #include <uapi/linux/kfd_ioctl.h>
39 #include "amdgpu_xgmi.h"
41 #include "kfd_smi_events.h"
43 /* Userptr restore delay, just long enough to allow consecutive VM
44 * changes to accumulate
46 #define AMDGPU_USERPTR_RESTORE_DELAY_MS 1
47 #define AMDGPU_RESERVE_MEM_LIMIT (3UL << 29)
50 * Align VRAM availability to 2MB to avoid fragmentation caused by 4K allocations in the tail 2MB
53 #define VRAM_AVAILABLITY_ALIGN (1 << 21)
55 /* Impose limit on how much memory KFD can use */
57 uint64_t max_system_mem_limit;
58 uint64_t max_ttm_mem_limit;
59 int64_t system_mem_used;
61 spinlock_t mem_limit_lock;
64 static const char * const domain_bit_to_string[] = {
73 #define domain_string(domain) domain_bit_to_string[ffs(domain)-1]
75 static void amdgpu_amdkfd_restore_userptr_worker(struct work_struct *work);
77 static bool kfd_mem_is_attached(struct amdgpu_vm *avm,
80 struct kfd_mem_attachment *entry;
82 list_for_each_entry(entry, &mem->attachments, list)
83 if (entry->bo_va->base.vm == avm)
90 * reuse_dmamap() - Check whether adev can share the original
93 * If both adev and bo_adev are in direct mapping or
94 * in the same iommu group, they can share the original BO.
96 * @adev: Device to which can or cannot share the original BO
97 * @bo_adev: Device to which allocated BO belongs to
99 * Return: returns true if adev can share original userptr BO,
102 static bool reuse_dmamap(struct amdgpu_device *adev, struct amdgpu_device *bo_adev)
104 return (adev->ram_is_direct_mapped && bo_adev->ram_is_direct_mapped) ||
105 (adev->dev->iommu_group == bo_adev->dev->iommu_group);
108 /* Set memory usage limits. Current, limits are
109 * System (TTM + userptr) memory - 15/16th System RAM
110 * TTM memory - 3/8th System RAM
112 void amdgpu_amdkfd_gpuvm_init_mem_limits(void)
117 if (kfd_mem_limit.max_system_mem_limit)
121 mem = si.totalram - si.totalhigh;
124 spin_lock_init(&kfd_mem_limit.mem_limit_lock);
125 kfd_mem_limit.max_system_mem_limit = mem - (mem >> 6);
126 if (kfd_mem_limit.max_system_mem_limit < 2 * AMDGPU_RESERVE_MEM_LIMIT)
127 kfd_mem_limit.max_system_mem_limit >>= 1;
129 kfd_mem_limit.max_system_mem_limit -= AMDGPU_RESERVE_MEM_LIMIT;
131 kfd_mem_limit.max_ttm_mem_limit = ttm_tt_pages_limit() << PAGE_SHIFT;
132 pr_debug("Kernel memory limit %lluM, TTM limit %lluM\n",
133 (kfd_mem_limit.max_system_mem_limit >> 20),
134 (kfd_mem_limit.max_ttm_mem_limit >> 20));
137 void amdgpu_amdkfd_reserve_system_mem(uint64_t size)
139 kfd_mem_limit.system_mem_used += size;
142 /* Estimate page table size needed to represent a given memory size
144 * With 4KB pages, we need one 8 byte PTE for each 4KB of memory
145 * (factor 512, >> 9). With 2MB pages, we need one 8 byte PTE for 2MB
146 * of memory (factor 256K, >> 18). ROCm user mode tries to optimize
147 * for 2MB pages for TLB efficiency. However, small allocations and
148 * fragmented system memory still need some 4KB pages. We choose a
149 * compromise that should work in most cases without reserving too
150 * much memory for page tables unnecessarily (factor 16K, >> 14).
153 #define ESTIMATE_PT_SIZE(mem_size) max(((mem_size) >> 14), AMDGPU_VM_RESERVED_VRAM)
156 * amdgpu_amdkfd_reserve_mem_limit() - Decrease available memory by size
159 * @adev: Device to which allocated BO belongs to
160 * @size: Size of buffer, in bytes, encapsulated by B0. This should be
161 * equivalent to amdgpu_bo_size(BO)
162 * @alloc_flag: Flag used in allocating a BO as noted above
163 * @xcp_id: xcp_id is used to get xcp from xcp manager, one xcp is
164 * managed as one compute node in driver for app
167 * returns -ENOMEM in case of error, ZERO otherwise
169 int amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device *adev,
170 uint64_t size, u32 alloc_flag, int8_t xcp_id)
172 uint64_t reserved_for_pt =
173 ESTIMATE_PT_SIZE(amdgpu_amdkfd_total_mem_size);
174 size_t system_mem_needed, ttm_mem_needed, vram_needed;
176 uint64_t vram_size = 0;
178 system_mem_needed = 0;
181 if (alloc_flag & KFD_IOC_ALLOC_MEM_FLAGS_GTT) {
182 system_mem_needed = size;
183 ttm_mem_needed = size;
184 } else if (alloc_flag & KFD_IOC_ALLOC_MEM_FLAGS_VRAM) {
186 * Conservatively round up the allocation requirement to 2 MB
187 * to avoid fragmentation caused by 4K allocations in the tail
192 * For GFX 9.4.3, get the VRAM size from XCP structs
194 if (WARN_ONCE(xcp_id < 0, "invalid XCP ID %d", xcp_id))
197 vram_size = KFD_XCP_MEMORY_SIZE(adev, xcp_id);
198 if (adev->gmc.is_app_apu) {
199 system_mem_needed = size;
200 ttm_mem_needed = size;
202 } else if (alloc_flag & KFD_IOC_ALLOC_MEM_FLAGS_USERPTR) {
203 system_mem_needed = size;
204 } else if (!(alloc_flag &
205 (KFD_IOC_ALLOC_MEM_FLAGS_DOORBELL |
206 KFD_IOC_ALLOC_MEM_FLAGS_MMIO_REMAP))) {
207 pr_err("%s: Invalid BO type %#x\n", __func__, alloc_flag);
211 spin_lock(&kfd_mem_limit.mem_limit_lock);
213 if (kfd_mem_limit.system_mem_used + system_mem_needed >
214 kfd_mem_limit.max_system_mem_limit)
215 pr_debug("Set no_system_mem_limit=1 if using shared memory\n");
217 if ((kfd_mem_limit.system_mem_used + system_mem_needed >
218 kfd_mem_limit.max_system_mem_limit && !no_system_mem_limit) ||
219 (kfd_mem_limit.ttm_mem_used + ttm_mem_needed >
220 kfd_mem_limit.max_ttm_mem_limit) ||
221 (adev && xcp_id >= 0 && adev->kfd.vram_used[xcp_id] + vram_needed >
222 vram_size - reserved_for_pt)) {
227 /* Update memory accounting by decreasing available system
228 * memory, TTM memory and GPU memory as computed above
230 WARN_ONCE(vram_needed && !adev,
231 "adev reference can't be null when vram is used");
232 if (adev && xcp_id >= 0) {
233 adev->kfd.vram_used[xcp_id] += vram_needed;
234 adev->kfd.vram_used_aligned[xcp_id] += adev->gmc.is_app_apu ?
236 ALIGN(vram_needed, VRAM_AVAILABLITY_ALIGN);
238 kfd_mem_limit.system_mem_used += system_mem_needed;
239 kfd_mem_limit.ttm_mem_used += ttm_mem_needed;
242 spin_unlock(&kfd_mem_limit.mem_limit_lock);
246 void amdgpu_amdkfd_unreserve_mem_limit(struct amdgpu_device *adev,
247 uint64_t size, u32 alloc_flag, int8_t xcp_id)
249 spin_lock(&kfd_mem_limit.mem_limit_lock);
251 if (alloc_flag & KFD_IOC_ALLOC_MEM_FLAGS_GTT) {
252 kfd_mem_limit.system_mem_used -= size;
253 kfd_mem_limit.ttm_mem_used -= size;
254 } else if (alloc_flag & KFD_IOC_ALLOC_MEM_FLAGS_VRAM) {
256 "adev reference can't be null when alloc mem flags vram is set");
257 if (WARN_ONCE(xcp_id < 0, "invalid XCP ID %d", xcp_id))
261 adev->kfd.vram_used[xcp_id] -= size;
262 if (adev->gmc.is_app_apu) {
263 adev->kfd.vram_used_aligned[xcp_id] -= size;
264 kfd_mem_limit.system_mem_used -= size;
265 kfd_mem_limit.ttm_mem_used -= size;
267 adev->kfd.vram_used_aligned[xcp_id] -=
268 ALIGN(size, VRAM_AVAILABLITY_ALIGN);
271 } else if (alloc_flag & KFD_IOC_ALLOC_MEM_FLAGS_USERPTR) {
272 kfd_mem_limit.system_mem_used -= size;
273 } else if (!(alloc_flag &
274 (KFD_IOC_ALLOC_MEM_FLAGS_DOORBELL |
275 KFD_IOC_ALLOC_MEM_FLAGS_MMIO_REMAP))) {
276 pr_err("%s: Invalid BO type %#x\n", __func__, alloc_flag);
279 WARN_ONCE(adev && xcp_id >= 0 && adev->kfd.vram_used[xcp_id] < 0,
280 "KFD VRAM memory accounting unbalanced for xcp: %d", xcp_id);
281 WARN_ONCE(kfd_mem_limit.ttm_mem_used < 0,
282 "KFD TTM memory accounting unbalanced");
283 WARN_ONCE(kfd_mem_limit.system_mem_used < 0,
284 "KFD system memory accounting unbalanced");
287 spin_unlock(&kfd_mem_limit.mem_limit_lock);
290 void amdgpu_amdkfd_release_notify(struct amdgpu_bo *bo)
292 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
293 u32 alloc_flags = bo->kfd_bo->alloc_flags;
294 u64 size = amdgpu_bo_size(bo);
296 amdgpu_amdkfd_unreserve_mem_limit(adev, size, alloc_flags,
303 * create_dmamap_sg_bo() - Creates a amdgpu_bo object to reflect information
304 * about USERPTR or DOOREBELL or MMIO BO.
306 * @adev: Device for which dmamap BO is being created
307 * @mem: BO of peer device that is being DMA mapped. Provides parameters
308 * in building the dmamap BO
309 * @bo_out: Output parameter updated with handle of dmamap BO
312 create_dmamap_sg_bo(struct amdgpu_device *adev,
313 struct kgd_mem *mem, struct amdgpu_bo **bo_out)
315 struct drm_gem_object *gem_obj;
319 ret = amdgpu_bo_reserve(mem->bo, false);
323 if (mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_USERPTR)
324 flags |= mem->bo->flags & (AMDGPU_GEM_CREATE_COHERENT |
325 AMDGPU_GEM_CREATE_UNCACHED);
327 ret = amdgpu_gem_object_create(adev, mem->bo->tbo.base.size, 1,
328 AMDGPU_GEM_DOMAIN_CPU, AMDGPU_GEM_CREATE_PREEMPTIBLE | flags,
329 ttm_bo_type_sg, mem->bo->tbo.base.resv, &gem_obj, 0);
331 amdgpu_bo_unreserve(mem->bo);
334 pr_err("Error in creating DMA mappable SG BO on domain: %d\n", ret);
338 *bo_out = gem_to_amdgpu_bo(gem_obj);
339 (*bo_out)->parent = amdgpu_bo_ref(mem->bo);
343 /* amdgpu_amdkfd_remove_eviction_fence - Removes eviction fence from BO's
344 * reservation object.
346 * @bo: [IN] Remove eviction fence(s) from this BO
347 * @ef: [IN] This eviction fence is removed if it
348 * is present in the shared list.
350 * NOTE: Must be called with BO reserved i.e. bo->tbo.resv->lock held.
352 static int amdgpu_amdkfd_remove_eviction_fence(struct amdgpu_bo *bo,
353 struct amdgpu_amdkfd_fence *ef)
355 struct dma_fence *replacement;
360 /* TODO: Instead of block before we should use the fence of the page
361 * table update and TLB flush here directly.
363 replacement = dma_fence_get_stub();
364 dma_resv_replace_fences(bo->tbo.base.resv, ef->base.context,
365 replacement, DMA_RESV_USAGE_BOOKKEEP);
366 dma_fence_put(replacement);
370 int amdgpu_amdkfd_remove_fence_on_pt_pd_bos(struct amdgpu_bo *bo)
372 struct amdgpu_bo *root = bo;
373 struct amdgpu_vm_bo_base *vm_bo;
374 struct amdgpu_vm *vm;
375 struct amdkfd_process_info *info;
376 struct amdgpu_amdkfd_fence *ef;
379 /* we can always get vm_bo from root PD bo.*/
391 info = vm->process_info;
392 if (!info || !info->eviction_fence)
395 ef = container_of(dma_fence_get(&info->eviction_fence->base),
396 struct amdgpu_amdkfd_fence, base);
398 BUG_ON(!dma_resv_trylock(bo->tbo.base.resv));
399 ret = amdgpu_amdkfd_remove_eviction_fence(bo, ef);
400 dma_resv_unlock(bo->tbo.base.resv);
402 dma_fence_put(&ef->base);
406 static int amdgpu_amdkfd_bo_validate(struct amdgpu_bo *bo, uint32_t domain,
409 struct ttm_operation_ctx ctx = { false, false };
412 if (WARN(amdgpu_ttm_tt_get_usermm(bo->tbo.ttm),
413 "Called with userptr BO"))
416 amdgpu_bo_placement_from_domain(bo, domain);
418 ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
422 amdgpu_bo_sync_wait(bo, AMDGPU_FENCE_OWNER_KFD, false);
428 static int amdgpu_amdkfd_bo_validate_and_fence(struct amdgpu_bo *bo,
430 struct dma_fence *fence)
432 int ret = amdgpu_bo_reserve(bo, false);
437 ret = amdgpu_amdkfd_bo_validate(bo, domain, true);
441 ret = dma_resv_reserve_fences(bo->tbo.base.resv, 1);
445 dma_resv_add_fence(bo->tbo.base.resv, fence,
446 DMA_RESV_USAGE_BOOKKEEP);
449 amdgpu_bo_unreserve(bo);
454 static int amdgpu_amdkfd_validate_vm_bo(void *_unused, struct amdgpu_bo *bo)
456 return amdgpu_amdkfd_bo_validate(bo, bo->allowed_domains, false);
459 /* vm_validate_pt_pd_bos - Validate page table and directory BOs
461 * Page directories are not updated here because huge page handling
462 * during page table updates can invalidate page directory entries
463 * again. Page directories are only updated after updating page
466 static int vm_validate_pt_pd_bos(struct amdgpu_vm *vm)
468 struct amdgpu_bo *pd = vm->root.bo;
469 struct amdgpu_device *adev = amdgpu_ttm_adev(pd->tbo.bdev);
472 ret = amdgpu_vm_validate_pt_bos(adev, vm, amdgpu_amdkfd_validate_vm_bo, NULL);
474 pr_err("failed to validate PT BOs\n");
478 vm->pd_phys_addr = amdgpu_gmc_pd_addr(vm->root.bo);
483 static int vm_update_pds(struct amdgpu_vm *vm, struct amdgpu_sync *sync)
485 struct amdgpu_bo *pd = vm->root.bo;
486 struct amdgpu_device *adev = amdgpu_ttm_adev(pd->tbo.bdev);
489 ret = amdgpu_vm_update_pdes(adev, vm, false);
493 return amdgpu_sync_fence(sync, vm->last_update);
496 static uint64_t get_pte_flags(struct amdgpu_device *adev, struct kgd_mem *mem)
498 uint32_t mapping_flags = AMDGPU_VM_PAGE_READABLE |
499 AMDGPU_VM_MTYPE_DEFAULT;
501 if (mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE)
502 mapping_flags |= AMDGPU_VM_PAGE_WRITEABLE;
503 if (mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_EXECUTABLE)
504 mapping_flags |= AMDGPU_VM_PAGE_EXECUTABLE;
506 return amdgpu_gem_va_map_flags(adev, mapping_flags);
510 * create_sg_table() - Create an sg_table for a contiguous DMA addr range
511 * @addr: The starting address to point to
512 * @size: Size of memory area in bytes being pointed to
514 * Allocates an instance of sg_table and initializes it to point to memory
515 * area specified by input parameters. The address used to build is assumed
516 * to be DMA mapped, if needed.
518 * DOORBELL or MMIO BOs use only one scatterlist node in their sg_table
519 * because they are physically contiguous.
521 * Return: Initialized instance of SG Table or NULL
523 static struct sg_table *create_sg_table(uint64_t addr, uint32_t size)
525 struct sg_table *sg = kmalloc(sizeof(*sg), GFP_KERNEL);
529 if (sg_alloc_table(sg, 1, GFP_KERNEL)) {
533 sg_dma_address(sg->sgl) = addr;
534 sg->sgl->length = size;
535 #ifdef CONFIG_NEED_SG_DMA_LENGTH
536 sg->sgl->dma_length = size;
542 kfd_mem_dmamap_userptr(struct kgd_mem *mem,
543 struct kfd_mem_attachment *attachment)
545 enum dma_data_direction direction =
546 mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE ?
547 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
548 struct ttm_operation_ctx ctx = {.interruptible = true};
549 struct amdgpu_bo *bo = attachment->bo_va->base.bo;
550 struct amdgpu_device *adev = attachment->adev;
551 struct ttm_tt *src_ttm = mem->bo->tbo.ttm;
552 struct ttm_tt *ttm = bo->tbo.ttm;
555 if (WARN_ON(ttm->num_pages != src_ttm->num_pages))
558 ttm->sg = kmalloc(sizeof(*ttm->sg), GFP_KERNEL);
559 if (unlikely(!ttm->sg))
562 /* Same sequence as in amdgpu_ttm_tt_pin_userptr */
563 ret = sg_alloc_table_from_pages(ttm->sg, src_ttm->pages,
565 (u64)ttm->num_pages << PAGE_SHIFT,
570 ret = dma_map_sgtable(adev->dev, ttm->sg, direction, 0);
574 amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
575 ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
582 dma_unmap_sgtable(adev->dev, ttm->sg, direction, 0);
584 pr_err("DMA map userptr failed: %d\n", ret);
585 sg_free_table(ttm->sg);
593 kfd_mem_dmamap_dmabuf(struct kfd_mem_attachment *attachment)
595 struct ttm_operation_ctx ctx = {.interruptible = true};
596 struct amdgpu_bo *bo = attachment->bo_va->base.bo;
599 amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_CPU);
600 ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
604 amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
605 return ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
609 * kfd_mem_dmamap_sg_bo() - Create DMA mapped sg_table to access DOORBELL or MMIO BO
610 * @mem: SG BO of the DOORBELL or MMIO resource on the owning device
611 * @attachment: Virtual address attachment of the BO on accessing device
613 * An access request from the device that owns DOORBELL does not require DMA mapping.
614 * This is because the request doesn't go through PCIe root complex i.e. it instead
615 * loops back. The need to DMA map arises only when accessing peer device's DOORBELL
617 * In contrast, all access requests for MMIO need to be DMA mapped without regard to
618 * device ownership. This is because access requests for MMIO go through PCIe root
621 * This is accomplished in two steps:
622 * - Obtain DMA mapped address of DOORBELL or MMIO memory that could be used
623 * in updating requesting device's page table
624 * - Signal TTM to mark memory pointed to by requesting device's BO as GPU
625 * accessible. This allows an update of requesting device's page table
626 * with entries associated with DOOREBELL or MMIO memory
628 * This method is invoked in the following contexts:
629 * - Mapping of DOORBELL or MMIO BO of same or peer device
630 * - Validating an evicted DOOREBELL or MMIO BO on device seeking access
632 * Return: ZERO if successful, NON-ZERO otherwise
635 kfd_mem_dmamap_sg_bo(struct kgd_mem *mem,
636 struct kfd_mem_attachment *attachment)
638 struct ttm_operation_ctx ctx = {.interruptible = true};
639 struct amdgpu_bo *bo = attachment->bo_va->base.bo;
640 struct amdgpu_device *adev = attachment->adev;
641 struct ttm_tt *ttm = bo->tbo.ttm;
642 enum dma_data_direction dir;
647 /* Expect SG Table of dmapmap BO to be NULL */
648 mmio = (mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_MMIO_REMAP);
649 if (unlikely(ttm->sg)) {
650 pr_err("SG Table of %d BO for peer device is UNEXPECTEDLY NON-NULL", mmio);
654 dir = mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE ?
655 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
656 dma_addr = mem->bo->tbo.sg->sgl->dma_address;
657 pr_debug("%d BO size: %d\n", mmio, mem->bo->tbo.sg->sgl->length);
658 pr_debug("%d BO address before DMA mapping: %llx\n", mmio, dma_addr);
659 dma_addr = dma_map_resource(adev->dev, dma_addr,
660 mem->bo->tbo.sg->sgl->length, dir, DMA_ATTR_SKIP_CPU_SYNC);
661 ret = dma_mapping_error(adev->dev, dma_addr);
664 pr_debug("%d BO address after DMA mapping: %llx\n", mmio, dma_addr);
666 ttm->sg = create_sg_table(dma_addr, mem->bo->tbo.sg->sgl->length);
667 if (unlikely(!ttm->sg)) {
672 amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
673 ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
680 sg_free_table(ttm->sg);
684 dma_unmap_resource(adev->dev, dma_addr, mem->bo->tbo.sg->sgl->length,
685 dir, DMA_ATTR_SKIP_CPU_SYNC);
690 kfd_mem_dmamap_attachment(struct kgd_mem *mem,
691 struct kfd_mem_attachment *attachment)
693 switch (attachment->type) {
694 case KFD_MEM_ATT_SHARED:
696 case KFD_MEM_ATT_USERPTR:
697 return kfd_mem_dmamap_userptr(mem, attachment);
698 case KFD_MEM_ATT_DMABUF:
699 return kfd_mem_dmamap_dmabuf(attachment);
701 return kfd_mem_dmamap_sg_bo(mem, attachment);
709 kfd_mem_dmaunmap_userptr(struct kgd_mem *mem,
710 struct kfd_mem_attachment *attachment)
712 enum dma_data_direction direction =
713 mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE ?
714 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
715 struct ttm_operation_ctx ctx = {.interruptible = false};
716 struct amdgpu_bo *bo = attachment->bo_va->base.bo;
717 struct amdgpu_device *adev = attachment->adev;
718 struct ttm_tt *ttm = bo->tbo.ttm;
720 if (unlikely(!ttm->sg))
723 amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_CPU);
724 ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
726 dma_unmap_sgtable(adev->dev, ttm->sg, direction, 0);
727 sg_free_table(ttm->sg);
733 kfd_mem_dmaunmap_dmabuf(struct kfd_mem_attachment *attachment)
735 /* This is a no-op. We don't want to trigger eviction fences when
736 * unmapping DMABufs. Therefore the invalidation (moving to system
737 * domain) is done in kfd_mem_dmamap_dmabuf.
742 * kfd_mem_dmaunmap_sg_bo() - Free DMA mapped sg_table of DOORBELL or MMIO BO
743 * @mem: SG BO of the DOORBELL or MMIO resource on the owning device
744 * @attachment: Virtual address attachment of the BO on accessing device
746 * The method performs following steps:
747 * - Signal TTM to mark memory pointed to by BO as GPU inaccessible
748 * - Free SG Table that is used to encapsulate DMA mapped memory of
749 * peer device's DOORBELL or MMIO memory
751 * This method is invoked in the following contexts:
752 * UNMapping of DOORBELL or MMIO BO on a device having access to its memory
753 * Eviction of DOOREBELL or MMIO BO on device having access to its memory
758 kfd_mem_dmaunmap_sg_bo(struct kgd_mem *mem,
759 struct kfd_mem_attachment *attachment)
761 struct ttm_operation_ctx ctx = {.interruptible = true};
762 struct amdgpu_bo *bo = attachment->bo_va->base.bo;
763 struct amdgpu_device *adev = attachment->adev;
764 struct ttm_tt *ttm = bo->tbo.ttm;
765 enum dma_data_direction dir;
767 if (unlikely(!ttm->sg)) {
768 pr_debug("SG Table of BO is NULL");
772 amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_CPU);
773 ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
775 dir = mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE ?
776 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
777 dma_unmap_resource(adev->dev, ttm->sg->sgl->dma_address,
778 ttm->sg->sgl->length, dir, DMA_ATTR_SKIP_CPU_SYNC);
779 sg_free_table(ttm->sg);
786 kfd_mem_dmaunmap_attachment(struct kgd_mem *mem,
787 struct kfd_mem_attachment *attachment)
789 switch (attachment->type) {
790 case KFD_MEM_ATT_SHARED:
792 case KFD_MEM_ATT_USERPTR:
793 kfd_mem_dmaunmap_userptr(mem, attachment);
795 case KFD_MEM_ATT_DMABUF:
796 kfd_mem_dmaunmap_dmabuf(attachment);
799 kfd_mem_dmaunmap_sg_bo(mem, attachment);
806 static int kfd_mem_export_dmabuf(struct kgd_mem *mem)
809 struct dma_buf *ret = amdgpu_gem_prime_export(
811 mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE ?
822 kfd_mem_attach_dmabuf(struct amdgpu_device *adev, struct kgd_mem *mem,
823 struct amdgpu_bo **bo)
825 struct drm_gem_object *gobj;
828 ret = kfd_mem_export_dmabuf(mem);
832 gobj = amdgpu_gem_prime_import(adev_to_drm(adev), mem->dmabuf);
834 return PTR_ERR(gobj);
836 *bo = gem_to_amdgpu_bo(gobj);
837 (*bo)->flags |= AMDGPU_GEM_CREATE_PREEMPTIBLE;
842 /* kfd_mem_attach - Add a BO to a VM
844 * Everything that needs to bo done only once when a BO is first added
845 * to a VM. It can later be mapped and unmapped many times without
846 * repeating these steps.
848 * 0. Create BO for DMA mapping, if needed
849 * 1. Allocate and initialize BO VA entry data structure
850 * 2. Add BO to the VM
851 * 3. Determine ASIC-specific PTE flags
852 * 4. Alloc page tables and directories if needed
853 * 4a. Validate new page tables and directories
855 static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
856 struct amdgpu_vm *vm, bool is_aql)
858 struct amdgpu_device *bo_adev = amdgpu_ttm_adev(mem->bo->tbo.bdev);
859 unsigned long bo_size = mem->bo->tbo.base.size;
860 uint64_t va = mem->va;
861 struct kfd_mem_attachment *attachment[2] = {NULL, NULL};
862 struct amdgpu_bo *bo[2] = {NULL, NULL};
863 struct amdgpu_bo_va *bo_va;
864 bool same_hive = false;
868 pr_err("Invalid VA when adding BO to VM\n");
872 /* Determine access to VRAM, MMIO and DOORBELL BOs of peer devices
874 * The access path of MMIO and DOORBELL BOs of is always over PCIe.
875 * In contrast the access path of VRAM BOs depens upon the type of
876 * link that connects the peer device. Access over PCIe is allowed
877 * if peer device has large BAR. In contrast, access over xGMI is
878 * allowed for both small and large BAR configurations of peer device
880 if ((adev != bo_adev && !adev->gmc.is_app_apu) &&
881 ((mem->domain == AMDGPU_GEM_DOMAIN_VRAM) ||
882 (mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_DOORBELL) ||
883 (mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_MMIO_REMAP))) {
884 if (mem->domain == AMDGPU_GEM_DOMAIN_VRAM)
885 same_hive = amdgpu_xgmi_same_hive(adev, bo_adev);
886 if (!same_hive && !amdgpu_device_is_peer_accessible(bo_adev, adev))
890 for (i = 0; i <= is_aql; i++) {
891 attachment[i] = kzalloc(sizeof(*attachment[i]), GFP_KERNEL);
892 if (unlikely(!attachment[i])) {
897 pr_debug("\t add VA 0x%llx - 0x%llx to vm %p\n", va,
900 if ((adev == bo_adev && !(mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_MMIO_REMAP)) ||
901 (amdgpu_ttm_tt_get_usermm(mem->bo->tbo.ttm) && reuse_dmamap(adev, bo_adev)) ||
902 (mem->domain == AMDGPU_GEM_DOMAIN_GTT && reuse_dmamap(adev, bo_adev)) ||
904 /* Mappings on the local GPU, or VRAM mappings in the
905 * local hive, or userptr, or GTT mapping can reuse dma map
906 * address space share the original BO
908 attachment[i]->type = KFD_MEM_ATT_SHARED;
910 drm_gem_object_get(&bo[i]->tbo.base);
912 /* Multiple mappings on the same GPU share the BO */
913 attachment[i]->type = KFD_MEM_ATT_SHARED;
915 drm_gem_object_get(&bo[i]->tbo.base);
916 } else if (amdgpu_ttm_tt_get_usermm(mem->bo->tbo.ttm)) {
917 /* Create an SG BO to DMA-map userptrs on other GPUs */
918 attachment[i]->type = KFD_MEM_ATT_USERPTR;
919 ret = create_dmamap_sg_bo(adev, mem, &bo[i]);
922 /* Handle DOORBELL BOs of peer devices and MMIO BOs of local and peer devices */
923 } else if (mem->bo->tbo.type == ttm_bo_type_sg) {
924 WARN_ONCE(!(mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_DOORBELL ||
925 mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_MMIO_REMAP),
926 "Handing invalid SG BO in ATTACH request");
927 attachment[i]->type = KFD_MEM_ATT_SG;
928 ret = create_dmamap_sg_bo(adev, mem, &bo[i]);
931 /* Enable acces to GTT and VRAM BOs of peer devices */
932 } else if (mem->domain == AMDGPU_GEM_DOMAIN_GTT ||
933 mem->domain == AMDGPU_GEM_DOMAIN_VRAM) {
934 attachment[i]->type = KFD_MEM_ATT_DMABUF;
935 ret = kfd_mem_attach_dmabuf(adev, mem, &bo[i]);
938 pr_debug("Employ DMABUF mechanism to enable peer GPU access\n");
940 WARN_ONCE(true, "Handling invalid ATTACH request");
945 /* Add BO to VM internal data structures */
946 ret = amdgpu_bo_reserve(bo[i], false);
948 pr_debug("Unable to reserve BO during memory attach");
951 bo_va = amdgpu_vm_bo_find(vm, bo[i]);
953 bo_va = amdgpu_vm_bo_add(adev, vm, bo[i]);
956 attachment[i]->bo_va = bo_va;
957 amdgpu_bo_unreserve(bo[i]);
958 if (unlikely(!attachment[i]->bo_va)) {
960 pr_err("Failed to add BO object to VM. ret == %d\n",
964 attachment[i]->va = va;
965 attachment[i]->pte_flags = get_pte_flags(adev, mem);
966 attachment[i]->adev = adev;
967 list_add(&attachment[i]->list, &mem->attachments);
975 for (; i >= 0; i--) {
978 if (attachment[i]->bo_va) {
979 amdgpu_bo_reserve(bo[i], true);
980 if (--attachment[i]->bo_va->ref_count == 0)
981 amdgpu_vm_bo_del(adev, attachment[i]->bo_va);
982 amdgpu_bo_unreserve(bo[i]);
983 list_del(&attachment[i]->list);
986 drm_gem_object_put(&bo[i]->tbo.base);
987 kfree(attachment[i]);
992 static void kfd_mem_detach(struct kfd_mem_attachment *attachment)
994 struct amdgpu_bo *bo = attachment->bo_va->base.bo;
996 pr_debug("\t remove VA 0x%llx in entry %p\n",
997 attachment->va, attachment);
998 if (--attachment->bo_va->ref_count == 0)
999 amdgpu_vm_bo_del(attachment->adev, attachment->bo_va);
1000 drm_gem_object_put(&bo->tbo.base);
1001 list_del(&attachment->list);
1005 static void add_kgd_mem_to_kfd_bo_list(struct kgd_mem *mem,
1006 struct amdkfd_process_info *process_info,
1009 mutex_lock(&process_info->lock);
1011 list_add_tail(&mem->validate_list,
1012 &process_info->userptr_valid_list);
1014 list_add_tail(&mem->validate_list, &process_info->kfd_bo_list);
1015 mutex_unlock(&process_info->lock);
1018 static void remove_kgd_mem_from_kfd_bo_list(struct kgd_mem *mem,
1019 struct amdkfd_process_info *process_info)
1021 mutex_lock(&process_info->lock);
1022 list_del(&mem->validate_list);
1023 mutex_unlock(&process_info->lock);
1026 /* Initializes user pages. It registers the MMU notifier and validates
1027 * the userptr BO in the GTT domain.
1029 * The BO must already be on the userptr_valid_list. Otherwise an
1030 * eviction and restore may happen that leaves the new BO unmapped
1031 * with the user mode queues running.
1033 * Takes the process_info->lock to protect against concurrent restore
1036 * Returns 0 for success, negative errno for errors.
1038 static int init_user_pages(struct kgd_mem *mem, uint64_t user_addr,
1041 struct amdkfd_process_info *process_info = mem->process_info;
1042 struct amdgpu_bo *bo = mem->bo;
1043 struct ttm_operation_ctx ctx = { true, false };
1044 struct hmm_range *range;
1047 mutex_lock(&process_info->lock);
1049 ret = amdgpu_ttm_tt_set_userptr(&bo->tbo, user_addr, 0);
1051 pr_err("%s: Failed to set userptr: %d\n", __func__, ret);
1055 ret = amdgpu_hmm_register(bo, user_addr);
1057 pr_err("%s: Failed to register MMU notifier: %d\n",
1064 * During a CRIU restore operation, the userptr buffer objects
1065 * will be validated in the restore_userptr_work worker at a
1066 * later stage when it is scheduled by another ioctl called by
1067 * CRIU master process for the target pid for restore.
1069 mutex_lock(&process_info->notifier_lock);
1071 mutex_unlock(&process_info->notifier_lock);
1072 mutex_unlock(&process_info->lock);
1076 ret = amdgpu_ttm_tt_get_user_pages(bo, bo->tbo.ttm->pages, &range);
1078 pr_err("%s: Failed to get user pages: %d\n", __func__, ret);
1079 goto unregister_out;
1082 ret = amdgpu_bo_reserve(bo, true);
1084 pr_err("%s: Failed to reserve BO\n", __func__);
1087 amdgpu_bo_placement_from_domain(bo, mem->domain);
1088 ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
1090 pr_err("%s: failed to validate BO\n", __func__);
1091 amdgpu_bo_unreserve(bo);
1094 amdgpu_ttm_tt_get_user_pages_done(bo->tbo.ttm, range);
1097 amdgpu_hmm_unregister(bo);
1099 mutex_unlock(&process_info->lock);
1103 /* Reserving a BO and its page table BOs must happen atomically to
1104 * avoid deadlocks. Some operations update multiple VMs at once. Track
1105 * all the reservation info in a context structure. Optionally a sync
1106 * object can track VM updates.
1108 struct bo_vm_reservation_context {
1109 /* DRM execution context for the reservation */
1110 struct drm_exec exec;
1111 /* Number of VMs reserved */
1113 /* Pointer to sync object */
1114 struct amdgpu_sync *sync;
1118 BO_VM_NOT_MAPPED = 0, /* Match VMs where a BO is not mapped */
1119 BO_VM_MAPPED, /* Match VMs where a BO is mapped */
1120 BO_VM_ALL, /* Match all VMs a BO was added to */
1124 * reserve_bo_and_vm - reserve a BO and a VM unconditionally.
1125 * @mem: KFD BO structure.
1126 * @vm: the VM to reserve.
1127 * @ctx: the struct that will be used in unreserve_bo_and_vms().
1129 static int reserve_bo_and_vm(struct kgd_mem *mem,
1130 struct amdgpu_vm *vm,
1131 struct bo_vm_reservation_context *ctx)
1133 struct amdgpu_bo *bo = mem->bo;
1139 ctx->sync = &mem->sync;
1140 drm_exec_init(&ctx->exec, DRM_EXEC_INTERRUPTIBLE_WAIT);
1141 drm_exec_until_all_locked(&ctx->exec) {
1142 ret = amdgpu_vm_lock_pd(vm, &ctx->exec, 2);
1143 drm_exec_retry_on_contention(&ctx->exec);
1147 ret = drm_exec_lock_obj(&ctx->exec, &bo->tbo.base);
1148 drm_exec_retry_on_contention(&ctx->exec);
1155 pr_err("Failed to reserve buffers in ttm.\n");
1156 drm_exec_fini(&ctx->exec);
1161 * reserve_bo_and_cond_vms - reserve a BO and some VMs conditionally
1162 * @mem: KFD BO structure.
1163 * @vm: the VM to reserve. If NULL, then all VMs associated with the BO
1164 * is used. Otherwise, a single VM associated with the BO.
1165 * @map_type: the mapping status that will be used to filter the VMs.
1166 * @ctx: the struct that will be used in unreserve_bo_and_vms().
1168 * Returns 0 for success, negative for failure.
1170 static int reserve_bo_and_cond_vms(struct kgd_mem *mem,
1171 struct amdgpu_vm *vm, enum bo_vm_match map_type,
1172 struct bo_vm_reservation_context *ctx)
1174 struct kfd_mem_attachment *entry;
1175 struct amdgpu_bo *bo = mem->bo;
1178 ctx->sync = &mem->sync;
1179 drm_exec_init(&ctx->exec, DRM_EXEC_INTERRUPTIBLE_WAIT);
1180 drm_exec_until_all_locked(&ctx->exec) {
1182 list_for_each_entry(entry, &mem->attachments, list) {
1183 if ((vm && vm != entry->bo_va->base.vm) ||
1184 (entry->is_mapped != map_type
1185 && map_type != BO_VM_ALL))
1188 ret = amdgpu_vm_lock_pd(entry->bo_va->base.vm,
1190 drm_exec_retry_on_contention(&ctx->exec);
1196 ret = drm_exec_prepare_obj(&ctx->exec, &bo->tbo.base, 1);
1197 drm_exec_retry_on_contention(&ctx->exec);
1204 pr_err("Failed to reserve buffers in ttm.\n");
1205 drm_exec_fini(&ctx->exec);
1210 * unreserve_bo_and_vms - Unreserve BO and VMs from a reservation context
1211 * @ctx: Reservation context to unreserve
1212 * @wait: Optionally wait for a sync object representing pending VM updates
1213 * @intr: Whether the wait is interruptible
1215 * Also frees any resources allocated in
1216 * reserve_bo_and_(cond_)vm(s). Returns the status from
1219 static int unreserve_bo_and_vms(struct bo_vm_reservation_context *ctx,
1220 bool wait, bool intr)
1225 ret = amdgpu_sync_wait(ctx->sync, intr);
1227 drm_exec_fini(&ctx->exec);
1232 static void unmap_bo_from_gpuvm(struct kgd_mem *mem,
1233 struct kfd_mem_attachment *entry,
1234 struct amdgpu_sync *sync)
1236 struct amdgpu_bo_va *bo_va = entry->bo_va;
1237 struct amdgpu_device *adev = entry->adev;
1238 struct amdgpu_vm *vm = bo_va->base.vm;
1240 amdgpu_vm_bo_unmap(adev, bo_va, entry->va);
1242 amdgpu_vm_clear_freed(adev, vm, &bo_va->last_pt_update);
1244 amdgpu_sync_fence(sync, bo_va->last_pt_update);
1247 static int update_gpuvm_pte(struct kgd_mem *mem,
1248 struct kfd_mem_attachment *entry,
1249 struct amdgpu_sync *sync)
1251 struct amdgpu_bo_va *bo_va = entry->bo_va;
1252 struct amdgpu_device *adev = entry->adev;
1255 ret = kfd_mem_dmamap_attachment(mem, entry);
1259 /* Update the page tables */
1260 ret = amdgpu_vm_bo_update(adev, bo_va, false);
1262 pr_err("amdgpu_vm_bo_update failed\n");
1266 return amdgpu_sync_fence(sync, bo_va->last_pt_update);
1269 static int map_bo_to_gpuvm(struct kgd_mem *mem,
1270 struct kfd_mem_attachment *entry,
1271 struct amdgpu_sync *sync,
1276 /* Set virtual address for the allocation */
1277 ret = amdgpu_vm_bo_map(entry->adev, entry->bo_va, entry->va, 0,
1278 amdgpu_bo_size(entry->bo_va->base.bo),
1281 pr_err("Failed to map VA 0x%llx in vm. ret %d\n",
1289 ret = update_gpuvm_pte(mem, entry, sync);
1291 pr_err("update_gpuvm_pte() failed\n");
1292 goto update_gpuvm_pte_failed;
1297 update_gpuvm_pte_failed:
1298 unmap_bo_from_gpuvm(mem, entry, sync);
1299 kfd_mem_dmaunmap_attachment(mem, entry);
1303 static int process_validate_vms(struct amdkfd_process_info *process_info)
1305 struct amdgpu_vm *peer_vm;
1308 list_for_each_entry(peer_vm, &process_info->vm_list_head,
1310 ret = vm_validate_pt_pd_bos(peer_vm);
1318 static int process_sync_pds_resv(struct amdkfd_process_info *process_info,
1319 struct amdgpu_sync *sync)
1321 struct amdgpu_vm *peer_vm;
1324 list_for_each_entry(peer_vm, &process_info->vm_list_head,
1326 struct amdgpu_bo *pd = peer_vm->root.bo;
1328 ret = amdgpu_sync_resv(NULL, sync, pd->tbo.base.resv,
1329 AMDGPU_SYNC_NE_OWNER,
1330 AMDGPU_FENCE_OWNER_KFD);
1338 static int process_update_pds(struct amdkfd_process_info *process_info,
1339 struct amdgpu_sync *sync)
1341 struct amdgpu_vm *peer_vm;
1344 list_for_each_entry(peer_vm, &process_info->vm_list_head,
1346 ret = vm_update_pds(peer_vm, sync);
1354 static int init_kfd_vm(struct amdgpu_vm *vm, void **process_info,
1355 struct dma_fence **ef)
1357 struct amdkfd_process_info *info = NULL;
1360 if (!*process_info) {
1361 info = kzalloc(sizeof(*info), GFP_KERNEL);
1365 mutex_init(&info->lock);
1366 mutex_init(&info->notifier_lock);
1367 INIT_LIST_HEAD(&info->vm_list_head);
1368 INIT_LIST_HEAD(&info->kfd_bo_list);
1369 INIT_LIST_HEAD(&info->userptr_valid_list);
1370 INIT_LIST_HEAD(&info->userptr_inval_list);
1372 info->eviction_fence =
1373 amdgpu_amdkfd_fence_create(dma_fence_context_alloc(1),
1376 if (!info->eviction_fence) {
1377 pr_err("Failed to create eviction fence\n");
1379 goto create_evict_fence_fail;
1382 info->pid = get_task_pid(current->group_leader, PIDTYPE_PID);
1383 INIT_DELAYED_WORK(&info->restore_userptr_work,
1384 amdgpu_amdkfd_restore_userptr_worker);
1386 *process_info = info;
1387 *ef = dma_fence_get(&info->eviction_fence->base);
1390 vm->process_info = *process_info;
1392 /* Validate page directory and attach eviction fence */
1393 ret = amdgpu_bo_reserve(vm->root.bo, true);
1395 goto reserve_pd_fail;
1396 ret = vm_validate_pt_pd_bos(vm);
1398 pr_err("validate_pt_pd_bos() failed\n");
1399 goto validate_pd_fail;
1401 ret = amdgpu_bo_sync_wait(vm->root.bo,
1402 AMDGPU_FENCE_OWNER_KFD, false);
1405 ret = dma_resv_reserve_fences(vm->root.bo->tbo.base.resv, 1);
1407 goto reserve_shared_fail;
1408 dma_resv_add_fence(vm->root.bo->tbo.base.resv,
1409 &vm->process_info->eviction_fence->base,
1410 DMA_RESV_USAGE_BOOKKEEP);
1411 amdgpu_bo_unreserve(vm->root.bo);
1413 /* Update process info */
1414 mutex_lock(&vm->process_info->lock);
1415 list_add_tail(&vm->vm_list_node,
1416 &(vm->process_info->vm_list_head));
1417 vm->process_info->n_vms++;
1418 mutex_unlock(&vm->process_info->lock);
1422 reserve_shared_fail:
1425 amdgpu_bo_unreserve(vm->root.bo);
1427 vm->process_info = NULL;
1429 /* Two fence references: one in info and one in *ef */
1430 dma_fence_put(&info->eviction_fence->base);
1433 *process_info = NULL;
1435 create_evict_fence_fail:
1436 mutex_destroy(&info->lock);
1437 mutex_destroy(&info->notifier_lock);
1444 * amdgpu_amdkfd_gpuvm_pin_bo() - Pins a BO using following criteria
1445 * @bo: Handle of buffer object being pinned
1446 * @domain: Domain into which BO should be pinned
1448 * - USERPTR BOs are UNPINNABLE and will return error
1449 * - All other BO types (GTT, VRAM, MMIO and DOORBELL) will have their
1450 * PIN count incremented. It is valid to PIN a BO multiple times
1452 * Return: ZERO if successful in pinning, Non-Zero in case of error.
1454 static int amdgpu_amdkfd_gpuvm_pin_bo(struct amdgpu_bo *bo, u32 domain)
1458 ret = amdgpu_bo_reserve(bo, false);
1462 ret = amdgpu_bo_pin_restricted(bo, domain, 0, 0);
1464 pr_err("Error in Pinning BO to domain: %d\n", domain);
1466 amdgpu_bo_sync_wait(bo, AMDGPU_FENCE_OWNER_KFD, false);
1467 amdgpu_bo_unreserve(bo);
1473 * amdgpu_amdkfd_gpuvm_unpin_bo() - Unpins BO using following criteria
1474 * @bo: Handle of buffer object being unpinned
1476 * - Is a illegal request for USERPTR BOs and is ignored
1477 * - All other BO types (GTT, VRAM, MMIO and DOORBELL) will have their
1478 * PIN count decremented. Calls to UNPIN must balance calls to PIN
1480 static void amdgpu_amdkfd_gpuvm_unpin_bo(struct amdgpu_bo *bo)
1484 ret = amdgpu_bo_reserve(bo, false);
1488 amdgpu_bo_unpin(bo);
1489 amdgpu_bo_unreserve(bo);
1492 int amdgpu_amdkfd_gpuvm_set_vm_pasid(struct amdgpu_device *adev,
1493 struct amdgpu_vm *avm, u32 pasid)
1498 /* Free the original amdgpu allocated pasid,
1499 * will be replaced with kfd allocated pasid.
1502 amdgpu_pasid_free(avm->pasid);
1503 amdgpu_vm_set_pasid(adev, avm, 0);
1506 ret = amdgpu_vm_set_pasid(adev, avm, pasid);
1513 int amdgpu_amdkfd_gpuvm_acquire_process_vm(struct amdgpu_device *adev,
1514 struct amdgpu_vm *avm,
1515 void **process_info,
1516 struct dma_fence **ef)
1520 /* Already a compute VM? */
1521 if (avm->process_info)
1524 /* Convert VM into a compute VM */
1525 ret = amdgpu_vm_make_compute(adev, avm);
1529 /* Initialize KFD part of the VM and process info */
1530 ret = init_kfd_vm(avm, process_info, ef);
1534 amdgpu_vm_set_task_info(avm);
1539 void amdgpu_amdkfd_gpuvm_destroy_cb(struct amdgpu_device *adev,
1540 struct amdgpu_vm *vm)
1542 struct amdkfd_process_info *process_info = vm->process_info;
1547 /* Update process info */
1548 mutex_lock(&process_info->lock);
1549 process_info->n_vms--;
1550 list_del(&vm->vm_list_node);
1551 mutex_unlock(&process_info->lock);
1553 vm->process_info = NULL;
1555 /* Release per-process resources when last compute VM is destroyed */
1556 if (!process_info->n_vms) {
1557 WARN_ON(!list_empty(&process_info->kfd_bo_list));
1558 WARN_ON(!list_empty(&process_info->userptr_valid_list));
1559 WARN_ON(!list_empty(&process_info->userptr_inval_list));
1561 dma_fence_put(&process_info->eviction_fence->base);
1562 cancel_delayed_work_sync(&process_info->restore_userptr_work);
1563 put_pid(process_info->pid);
1564 mutex_destroy(&process_info->lock);
1565 mutex_destroy(&process_info->notifier_lock);
1566 kfree(process_info);
1570 void amdgpu_amdkfd_gpuvm_release_process_vm(struct amdgpu_device *adev,
1573 struct amdgpu_vm *avm;
1575 if (WARN_ON(!adev || !drm_priv))
1578 avm = drm_priv_to_vm(drm_priv);
1580 pr_debug("Releasing process vm %p\n", avm);
1582 /* The original pasid of amdgpu vm has already been
1583 * released during making a amdgpu vm to a compute vm
1584 * The current pasid is managed by kfd and will be
1585 * released on kfd process destroy. Set amdgpu pasid
1586 * to 0 to avoid duplicate release.
1588 amdgpu_vm_release_compute(adev, avm);
1591 uint64_t amdgpu_amdkfd_gpuvm_get_process_page_dir(void *drm_priv)
1593 struct amdgpu_vm *avm = drm_priv_to_vm(drm_priv);
1594 struct amdgpu_bo *pd = avm->root.bo;
1595 struct amdgpu_device *adev = amdgpu_ttm_adev(pd->tbo.bdev);
1597 if (adev->asic_type < CHIP_VEGA10)
1598 return avm->pd_phys_addr >> AMDGPU_GPU_PAGE_SHIFT;
1599 return avm->pd_phys_addr;
1602 void amdgpu_amdkfd_block_mmu_notifications(void *p)
1604 struct amdkfd_process_info *pinfo = (struct amdkfd_process_info *)p;
1606 mutex_lock(&pinfo->lock);
1607 WRITE_ONCE(pinfo->block_mmu_notifications, true);
1608 mutex_unlock(&pinfo->lock);
1611 int amdgpu_amdkfd_criu_resume(void *p)
1614 struct amdkfd_process_info *pinfo = (struct amdkfd_process_info *)p;
1616 mutex_lock(&pinfo->lock);
1617 pr_debug("scheduling work\n");
1618 mutex_lock(&pinfo->notifier_lock);
1619 pinfo->evicted_bos++;
1620 mutex_unlock(&pinfo->notifier_lock);
1621 if (!READ_ONCE(pinfo->block_mmu_notifications)) {
1625 WRITE_ONCE(pinfo->block_mmu_notifications, false);
1626 schedule_delayed_work(&pinfo->restore_userptr_work, 0);
1629 mutex_unlock(&pinfo->lock);
1633 size_t amdgpu_amdkfd_get_available_memory(struct amdgpu_device *adev,
1636 uint64_t reserved_for_pt =
1637 ESTIMATE_PT_SIZE(amdgpu_amdkfd_total_mem_size);
1639 uint64_t vram_available, system_mem_available, ttm_mem_available;
1641 spin_lock(&kfd_mem_limit.mem_limit_lock);
1642 vram_available = KFD_XCP_MEMORY_SIZE(adev, xcp_id)
1643 - adev->kfd.vram_used_aligned[xcp_id]
1644 - atomic64_read(&adev->vram_pin_size)
1647 if (adev->gmc.is_app_apu) {
1648 system_mem_available = no_system_mem_limit ?
1649 kfd_mem_limit.max_system_mem_limit :
1650 kfd_mem_limit.max_system_mem_limit -
1651 kfd_mem_limit.system_mem_used;
1653 ttm_mem_available = kfd_mem_limit.max_ttm_mem_limit -
1654 kfd_mem_limit.ttm_mem_used;
1656 available = min3(system_mem_available, ttm_mem_available,
1658 available = ALIGN_DOWN(available, PAGE_SIZE);
1660 available = ALIGN_DOWN(vram_available, VRAM_AVAILABLITY_ALIGN);
1663 spin_unlock(&kfd_mem_limit.mem_limit_lock);
1671 int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
1672 struct amdgpu_device *adev, uint64_t va, uint64_t size,
1673 void *drm_priv, struct kgd_mem **mem,
1674 uint64_t *offset, uint32_t flags, bool criu_resume)
1676 struct amdgpu_vm *avm = drm_priv_to_vm(drm_priv);
1677 struct amdgpu_fpriv *fpriv = container_of(avm, struct amdgpu_fpriv, vm);
1678 enum ttm_bo_type bo_type = ttm_bo_type_device;
1679 struct sg_table *sg = NULL;
1680 uint64_t user_addr = 0;
1681 struct amdgpu_bo *bo;
1682 struct drm_gem_object *gobj = NULL;
1683 u32 domain, alloc_domain;
1684 uint64_t aligned_size;
1690 * Check on which domain to allocate BO
1692 if (flags & KFD_IOC_ALLOC_MEM_FLAGS_VRAM) {
1693 domain = alloc_domain = AMDGPU_GEM_DOMAIN_VRAM;
1695 if (adev->gmc.is_app_apu) {
1696 domain = AMDGPU_GEM_DOMAIN_GTT;
1697 alloc_domain = AMDGPU_GEM_DOMAIN_GTT;
1700 alloc_flags = AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE;
1701 alloc_flags |= (flags & KFD_IOC_ALLOC_MEM_FLAGS_PUBLIC) ?
1702 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED : 0;
1704 xcp_id = fpriv->xcp_id == AMDGPU_XCP_NO_PARTITION ?
1706 } else if (flags & KFD_IOC_ALLOC_MEM_FLAGS_GTT) {
1707 domain = alloc_domain = AMDGPU_GEM_DOMAIN_GTT;
1710 domain = AMDGPU_GEM_DOMAIN_GTT;
1711 alloc_domain = AMDGPU_GEM_DOMAIN_CPU;
1712 alloc_flags = AMDGPU_GEM_CREATE_PREEMPTIBLE;
1714 if (flags & KFD_IOC_ALLOC_MEM_FLAGS_USERPTR) {
1715 if (!offset || !*offset)
1717 user_addr = untagged_addr(*offset);
1718 } else if (flags & (KFD_IOC_ALLOC_MEM_FLAGS_DOORBELL |
1719 KFD_IOC_ALLOC_MEM_FLAGS_MMIO_REMAP)) {
1720 bo_type = ttm_bo_type_sg;
1721 if (size > UINT_MAX)
1723 sg = create_sg_table(*offset, size);
1731 if (flags & KFD_IOC_ALLOC_MEM_FLAGS_COHERENT)
1732 alloc_flags |= AMDGPU_GEM_CREATE_COHERENT;
1733 if (flags & KFD_IOC_ALLOC_MEM_FLAGS_EXT_COHERENT)
1734 alloc_flags |= AMDGPU_GEM_CREATE_EXT_COHERENT;
1735 if (flags & KFD_IOC_ALLOC_MEM_FLAGS_UNCACHED)
1736 alloc_flags |= AMDGPU_GEM_CREATE_UNCACHED;
1738 *mem = kzalloc(sizeof(struct kgd_mem), GFP_KERNEL);
1743 INIT_LIST_HEAD(&(*mem)->attachments);
1744 mutex_init(&(*mem)->lock);
1745 (*mem)->aql_queue = !!(flags & KFD_IOC_ALLOC_MEM_FLAGS_AQL_QUEUE_MEM);
1747 /* Workaround for AQL queue wraparound bug. Map the same
1748 * memory twice. That means we only actually allocate half
1751 if ((*mem)->aql_queue)
1753 aligned_size = PAGE_ALIGN(size);
1755 (*mem)->alloc_flags = flags;
1757 amdgpu_sync_create(&(*mem)->sync);
1759 ret = amdgpu_amdkfd_reserve_mem_limit(adev, aligned_size, flags,
1762 pr_debug("Insufficient memory\n");
1763 goto err_reserve_limit;
1766 pr_debug("\tcreate BO VA 0x%llx size 0x%llx domain %s xcp_id %d\n",
1767 va, (*mem)->aql_queue ? size << 1 : size,
1768 domain_string(alloc_domain), xcp_id);
1770 ret = amdgpu_gem_object_create(adev, aligned_size, 1, alloc_domain, alloc_flags,
1771 bo_type, NULL, &gobj, xcp_id + 1);
1773 pr_debug("Failed to create BO on domain %s. ret %d\n",
1774 domain_string(alloc_domain), ret);
1777 ret = drm_vma_node_allow(&gobj->vma_node, drm_priv);
1779 pr_debug("Failed to allow vma node access. ret %d\n", ret);
1780 goto err_node_allow;
1782 bo = gem_to_amdgpu_bo(gobj);
1783 if (bo_type == ttm_bo_type_sg) {
1785 bo->tbo.ttm->sg = sg;
1790 bo->flags |= AMDGPU_AMDKFD_CREATE_USERPTR_BO;
1793 (*mem)->domain = domain;
1794 (*mem)->mapped_to_gpu_memory = 0;
1795 (*mem)->process_info = avm->process_info;
1797 add_kgd_mem_to_kfd_bo_list(*mem, avm->process_info, user_addr);
1800 pr_debug("creating userptr BO for user_addr = %llx\n", user_addr);
1801 ret = init_user_pages(*mem, user_addr, criu_resume);
1803 goto allocate_init_user_pages_failed;
1804 } else if (flags & (KFD_IOC_ALLOC_MEM_FLAGS_DOORBELL |
1805 KFD_IOC_ALLOC_MEM_FLAGS_MMIO_REMAP)) {
1806 ret = amdgpu_amdkfd_gpuvm_pin_bo(bo, AMDGPU_GEM_DOMAIN_GTT);
1808 pr_err("Pinning MMIO/DOORBELL BO during ALLOC FAILED\n");
1811 bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
1812 bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
1814 mutex_lock(&avm->process_info->lock);
1815 if (avm->process_info->eviction_fence &&
1816 !dma_fence_is_signaled(&avm->process_info->eviction_fence->base))
1817 ret = amdgpu_amdkfd_bo_validate_and_fence(bo, domain,
1818 &avm->process_info->eviction_fence->base);
1819 mutex_unlock(&avm->process_info->lock);
1821 goto err_validate_bo;
1825 *offset = amdgpu_bo_mmap_offset(bo);
1829 allocate_init_user_pages_failed:
1832 remove_kgd_mem_from_kfd_bo_list(*mem, avm->process_info);
1833 drm_vma_node_revoke(&gobj->vma_node, drm_priv);
1835 /* Don't unreserve system mem limit twice */
1836 goto err_reserve_limit;
1838 amdgpu_amdkfd_unreserve_mem_limit(adev, aligned_size, flags, xcp_id);
1840 mutex_destroy(&(*mem)->lock);
1842 drm_gem_object_put(gobj);
1853 int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
1854 struct amdgpu_device *adev, struct kgd_mem *mem, void *drm_priv,
1857 struct amdkfd_process_info *process_info = mem->process_info;
1858 unsigned long bo_size = mem->bo->tbo.base.size;
1859 bool use_release_notifier = (mem->bo->kfd_bo == mem);
1860 struct kfd_mem_attachment *entry, *tmp;
1861 struct bo_vm_reservation_context ctx;
1862 unsigned int mapped_to_gpu_memory;
1864 bool is_imported = false;
1866 mutex_lock(&mem->lock);
1868 /* Unpin MMIO/DOORBELL BO's that were pinned during allocation */
1869 if (mem->alloc_flags &
1870 (KFD_IOC_ALLOC_MEM_FLAGS_DOORBELL |
1871 KFD_IOC_ALLOC_MEM_FLAGS_MMIO_REMAP)) {
1872 amdgpu_amdkfd_gpuvm_unpin_bo(mem->bo);
1875 mapped_to_gpu_memory = mem->mapped_to_gpu_memory;
1876 is_imported = mem->is_imported;
1877 mutex_unlock(&mem->lock);
1878 /* lock is not needed after this, since mem is unused and will
1882 if (mapped_to_gpu_memory > 0) {
1883 pr_debug("BO VA 0x%llx size 0x%lx is still mapped.\n",
1888 /* Make sure restore workers don't access the BO any more */
1889 mutex_lock(&process_info->lock);
1890 list_del(&mem->validate_list);
1891 mutex_unlock(&process_info->lock);
1893 /* Cleanup user pages and MMU notifiers */
1894 if (amdgpu_ttm_tt_get_usermm(mem->bo->tbo.ttm)) {
1895 amdgpu_hmm_unregister(mem->bo);
1896 mutex_lock(&process_info->notifier_lock);
1897 amdgpu_ttm_tt_discard_user_pages(mem->bo->tbo.ttm, mem->range);
1898 mutex_unlock(&process_info->notifier_lock);
1901 ret = reserve_bo_and_cond_vms(mem, NULL, BO_VM_ALL, &ctx);
1905 amdgpu_amdkfd_remove_eviction_fence(mem->bo,
1906 process_info->eviction_fence);
1907 pr_debug("Release VA 0x%llx - 0x%llx\n", mem->va,
1908 mem->va + bo_size * (1 + mem->aql_queue));
1910 /* Remove from VM internal data structures */
1911 list_for_each_entry_safe(entry, tmp, &mem->attachments, list) {
1912 kfd_mem_dmaunmap_attachment(mem, entry);
1913 kfd_mem_detach(entry);
1916 ret = unreserve_bo_and_vms(&ctx, false, false);
1918 /* Free the sync object */
1919 amdgpu_sync_free(&mem->sync);
1921 /* If the SG is not NULL, it's one we created for a doorbell or mmio
1922 * remap BO. We need to free it.
1924 if (mem->bo->tbo.sg) {
1925 sg_free_table(mem->bo->tbo.sg);
1926 kfree(mem->bo->tbo.sg);
1929 /* Update the size of the BO being freed if it was allocated from
1930 * VRAM and is not imported. For APP APU VRAM allocations are done
1935 (mem->bo->preferred_domains == AMDGPU_GEM_DOMAIN_VRAM ||
1936 (adev->gmc.is_app_apu &&
1937 mem->bo->preferred_domains == AMDGPU_GEM_DOMAIN_GTT)))
1944 drm_vma_node_revoke(&mem->bo->tbo.base.vma_node, drm_priv);
1946 dma_buf_put(mem->dmabuf);
1947 mutex_destroy(&mem->lock);
1949 /* If this releases the last reference, it will end up calling
1950 * amdgpu_amdkfd_release_notify and kfree the mem struct. That's why
1951 * this needs to be the last call here.
1953 drm_gem_object_put(&mem->bo->tbo.base);
1956 * For kgd_mem allocated in amdgpu_amdkfd_gpuvm_import_dmabuf(),
1957 * explicitly free it here.
1959 if (!use_release_notifier)
1965 int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
1966 struct amdgpu_device *adev, struct kgd_mem *mem,
1969 struct amdgpu_vm *avm = drm_priv_to_vm(drm_priv);
1971 struct amdgpu_bo *bo;
1973 struct kfd_mem_attachment *entry;
1974 struct bo_vm_reservation_context ctx;
1975 unsigned long bo_size;
1976 bool is_invalid_userptr = false;
1980 pr_err("Invalid BO when mapping memory to GPU\n");
1984 /* Make sure restore is not running concurrently. Since we
1985 * don't map invalid userptr BOs, we rely on the next restore
1986 * worker to do the mapping
1988 mutex_lock(&mem->process_info->lock);
1990 /* Lock notifier lock. If we find an invalid userptr BO, we can be
1991 * sure that the MMU notifier is no longer running
1992 * concurrently and the queues are actually stopped
1994 if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm)) {
1995 mutex_lock(&mem->process_info->notifier_lock);
1996 is_invalid_userptr = !!mem->invalid;
1997 mutex_unlock(&mem->process_info->notifier_lock);
2000 mutex_lock(&mem->lock);
2002 domain = mem->domain;
2003 bo_size = bo->tbo.base.size;
2005 pr_debug("Map VA 0x%llx - 0x%llx to vm %p domain %s\n",
2007 mem->va + bo_size * (1 + mem->aql_queue),
2008 avm, domain_string(domain));
2010 if (!kfd_mem_is_attached(avm, mem)) {
2011 ret = kfd_mem_attach(adev, mem, avm, mem->aql_queue);
2016 ret = reserve_bo_and_vm(mem, avm, &ctx);
2020 /* Userptr can be marked as "not invalid", but not actually be
2021 * validated yet (still in the system domain). In that case
2022 * the queues are still stopped and we can leave mapping for
2023 * the next restore worker
2025 if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) &&
2026 bo->tbo.resource->mem_type == TTM_PL_SYSTEM)
2027 is_invalid_userptr = true;
2029 ret = vm_validate_pt_pd_bos(avm);
2033 list_for_each_entry(entry, &mem->attachments, list) {
2034 if (entry->bo_va->base.vm != avm || entry->is_mapped)
2037 pr_debug("\t map VA 0x%llx - 0x%llx in entry %p\n",
2038 entry->va, entry->va + bo_size, entry);
2040 ret = map_bo_to_gpuvm(mem, entry, ctx.sync,
2041 is_invalid_userptr);
2043 pr_err("Failed to map bo to gpuvm\n");
2047 ret = vm_update_pds(avm, ctx.sync);
2049 pr_err("Failed to update page directories\n");
2053 entry->is_mapped = true;
2054 mem->mapped_to_gpu_memory++;
2055 pr_debug("\t INC mapping count %d\n",
2056 mem->mapped_to_gpu_memory);
2059 ret = unreserve_bo_and_vms(&ctx, false, false);
2064 unreserve_bo_and_vms(&ctx, false, false);
2066 mutex_unlock(&mem->process_info->lock);
2067 mutex_unlock(&mem->lock);
2071 void amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void *drm_priv)
2073 struct kfd_mem_attachment *entry;
2074 struct amdgpu_vm *vm;
2076 vm = drm_priv_to_vm(drm_priv);
2078 mutex_lock(&mem->lock);
2080 list_for_each_entry(entry, &mem->attachments, list) {
2081 if (entry->bo_va->base.vm == vm)
2082 kfd_mem_dmaunmap_attachment(mem, entry);
2085 mutex_unlock(&mem->lock);
2088 int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
2089 struct amdgpu_device *adev, struct kgd_mem *mem, void *drm_priv)
2091 struct amdgpu_vm *avm = drm_priv_to_vm(drm_priv);
2092 unsigned long bo_size = mem->bo->tbo.base.size;
2093 struct kfd_mem_attachment *entry;
2094 struct bo_vm_reservation_context ctx;
2097 mutex_lock(&mem->lock);
2099 ret = reserve_bo_and_cond_vms(mem, avm, BO_VM_MAPPED, &ctx);
2102 /* If no VMs were reserved, it means the BO wasn't actually mapped */
2103 if (ctx.n_vms == 0) {
2108 ret = vm_validate_pt_pd_bos(avm);
2112 pr_debug("Unmap VA 0x%llx - 0x%llx from vm %p\n",
2114 mem->va + bo_size * (1 + mem->aql_queue),
2117 list_for_each_entry(entry, &mem->attachments, list) {
2118 if (entry->bo_va->base.vm != avm || !entry->is_mapped)
2121 pr_debug("\t unmap VA 0x%llx - 0x%llx from entry %p\n",
2122 entry->va, entry->va + bo_size, entry);
2124 unmap_bo_from_gpuvm(mem, entry, ctx.sync);
2125 entry->is_mapped = false;
2127 mem->mapped_to_gpu_memory--;
2128 pr_debug("\t DEC mapping count %d\n",
2129 mem->mapped_to_gpu_memory);
2133 unreserve_bo_and_vms(&ctx, false, false);
2135 mutex_unlock(&mem->lock);
2139 int amdgpu_amdkfd_gpuvm_sync_memory(
2140 struct amdgpu_device *adev, struct kgd_mem *mem, bool intr)
2142 struct amdgpu_sync sync;
2145 amdgpu_sync_create(&sync);
2147 mutex_lock(&mem->lock);
2148 amdgpu_sync_clone(&mem->sync, &sync);
2149 mutex_unlock(&mem->lock);
2151 ret = amdgpu_sync_wait(&sync, intr);
2152 amdgpu_sync_free(&sync);
2157 * amdgpu_amdkfd_map_gtt_bo_to_gart - Map BO to GART and increment reference count
2158 * @adev: Device to which allocated BO belongs
2159 * @bo: Buffer object to be mapped
2161 * Before return, bo reference count is incremented. To release the reference and unpin/
2162 * unmap the BO, call amdgpu_amdkfd_free_gtt_mem.
2164 int amdgpu_amdkfd_map_gtt_bo_to_gart(struct amdgpu_device *adev, struct amdgpu_bo *bo)
2168 ret = amdgpu_bo_reserve(bo, true);
2170 pr_err("Failed to reserve bo. ret %d\n", ret);
2171 goto err_reserve_bo_failed;
2174 ret = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT);
2176 pr_err("Failed to pin bo. ret %d\n", ret);
2177 goto err_pin_bo_failed;
2180 ret = amdgpu_ttm_alloc_gart(&bo->tbo);
2182 pr_err("Failed to bind bo to GART. ret %d\n", ret);
2183 goto err_map_bo_gart_failed;
2186 amdgpu_amdkfd_remove_eviction_fence(
2187 bo, bo->vm_bo->vm->process_info->eviction_fence);
2189 amdgpu_bo_unreserve(bo);
2191 bo = amdgpu_bo_ref(bo);
2195 err_map_bo_gart_failed:
2196 amdgpu_bo_unpin(bo);
2198 amdgpu_bo_unreserve(bo);
2199 err_reserve_bo_failed:
2204 /** amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel() - Map a GTT BO for kernel CPU access
2206 * @mem: Buffer object to be mapped for CPU access
2207 * @kptr[out]: pointer in kernel CPU address space
2208 * @size[out]: size of the buffer
2210 * Pins the BO and maps it for kernel CPU access. The eviction fence is removed
2211 * from the BO, since pinned BOs cannot be evicted. The bo must remain on the
2212 * validate_list, so the GPU mapping can be restored after a page table was
2215 * Return: 0 on success, error code on failure
2217 int amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_mem *mem,
2218 void **kptr, uint64_t *size)
2221 struct amdgpu_bo *bo = mem->bo;
2223 if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm)) {
2224 pr_err("userptr can't be mapped to kernel\n");
2228 mutex_lock(&mem->process_info->lock);
2230 ret = amdgpu_bo_reserve(bo, true);
2232 pr_err("Failed to reserve bo. ret %d\n", ret);
2233 goto bo_reserve_failed;
2236 ret = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT);
2238 pr_err("Failed to pin bo. ret %d\n", ret);
2242 ret = amdgpu_bo_kmap(bo, kptr);
2244 pr_err("Failed to map bo to kernel. ret %d\n", ret);
2248 amdgpu_amdkfd_remove_eviction_fence(
2249 bo, mem->process_info->eviction_fence);
2252 *size = amdgpu_bo_size(bo);
2254 amdgpu_bo_unreserve(bo);
2256 mutex_unlock(&mem->process_info->lock);
2260 amdgpu_bo_unpin(bo);
2262 amdgpu_bo_unreserve(bo);
2264 mutex_unlock(&mem->process_info->lock);
2269 /** amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel() - Unmap a GTT BO for kernel CPU access
2271 * @mem: Buffer object to be unmapped for CPU access
2273 * Removes the kernel CPU mapping and unpins the BO. It does not restore the
2274 * eviction fence, so this function should only be used for cleanup before the
2277 void amdgpu_amdkfd_gpuvm_unmap_gtt_bo_from_kernel(struct kgd_mem *mem)
2279 struct amdgpu_bo *bo = mem->bo;
2281 amdgpu_bo_reserve(bo, true);
2282 amdgpu_bo_kunmap(bo);
2283 amdgpu_bo_unpin(bo);
2284 amdgpu_bo_unreserve(bo);
2287 int amdgpu_amdkfd_gpuvm_get_vm_fault_info(struct amdgpu_device *adev,
2288 struct kfd_vm_fault_info *mem)
2290 if (atomic_read(&adev->gmc.vm_fault_info_updated) == 1) {
2291 *mem = *adev->gmc.vm_fault_info;
2292 mb(); /* make sure read happened */
2293 atomic_set(&adev->gmc.vm_fault_info_updated, 0);
2298 int amdgpu_amdkfd_gpuvm_import_dmabuf(struct amdgpu_device *adev,
2299 struct dma_buf *dma_buf,
2300 uint64_t va, void *drm_priv,
2301 struct kgd_mem **mem, uint64_t *size,
2302 uint64_t *mmap_offset)
2304 struct amdgpu_vm *avm = drm_priv_to_vm(drm_priv);
2305 struct drm_gem_object *obj;
2306 struct amdgpu_bo *bo;
2309 obj = amdgpu_gem_prime_import(adev_to_drm(adev), dma_buf);
2311 return PTR_ERR(obj);
2313 bo = gem_to_amdgpu_bo(obj);
2314 if (!(bo->preferred_domains & (AMDGPU_GEM_DOMAIN_VRAM |
2315 AMDGPU_GEM_DOMAIN_GTT))) {
2316 /* Only VRAM and GTT BOs are supported */
2321 *mem = kzalloc(sizeof(struct kgd_mem), GFP_KERNEL);
2327 ret = drm_vma_node_allow(&obj->vma_node, drm_priv);
2332 *size = amdgpu_bo_size(bo);
2335 *mmap_offset = amdgpu_bo_mmap_offset(bo);
2337 INIT_LIST_HEAD(&(*mem)->attachments);
2338 mutex_init(&(*mem)->lock);
2340 (*mem)->alloc_flags =
2341 ((bo->preferred_domains & AMDGPU_GEM_DOMAIN_VRAM) ?
2342 KFD_IOC_ALLOC_MEM_FLAGS_VRAM : KFD_IOC_ALLOC_MEM_FLAGS_GTT)
2343 | KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE
2344 | KFD_IOC_ALLOC_MEM_FLAGS_EXECUTABLE;
2346 get_dma_buf(dma_buf);
2347 (*mem)->dmabuf = dma_buf;
2350 (*mem)->domain = (bo->preferred_domains & AMDGPU_GEM_DOMAIN_VRAM) && !adev->gmc.is_app_apu ?
2351 AMDGPU_GEM_DOMAIN_VRAM : AMDGPU_GEM_DOMAIN_GTT;
2353 (*mem)->mapped_to_gpu_memory = 0;
2354 (*mem)->process_info = avm->process_info;
2355 add_kgd_mem_to_kfd_bo_list(*mem, avm->process_info, false);
2356 amdgpu_sync_create(&(*mem)->sync);
2357 (*mem)->is_imported = true;
2359 mutex_lock(&avm->process_info->lock);
2360 if (avm->process_info->eviction_fence &&
2361 !dma_fence_is_signaled(&avm->process_info->eviction_fence->base))
2362 ret = amdgpu_amdkfd_bo_validate_and_fence(bo, (*mem)->domain,
2363 &avm->process_info->eviction_fence->base);
2364 mutex_unlock(&avm->process_info->lock);
2366 goto err_remove_mem;
2371 remove_kgd_mem_from_kfd_bo_list(*mem, avm->process_info);
2372 drm_vma_node_revoke(&obj->vma_node, drm_priv);
2376 drm_gem_object_put(obj);
2380 int amdgpu_amdkfd_gpuvm_export_dmabuf(struct kgd_mem *mem,
2381 struct dma_buf **dma_buf)
2385 mutex_lock(&mem->lock);
2386 ret = kfd_mem_export_dmabuf(mem);
2390 get_dma_buf(mem->dmabuf);
2391 *dma_buf = mem->dmabuf;
2393 mutex_unlock(&mem->lock);
2397 /* Evict a userptr BO by stopping the queues if necessary
2399 * Runs in MMU notifier, may be in RECLAIM_FS context. This means it
2400 * cannot do any memory allocations, and cannot take any locks that
2401 * are held elsewhere while allocating memory.
2403 * It doesn't do anything to the BO itself. The real work happens in
2404 * restore, where we get updated page addresses. This function only
2405 * ensures that GPU access to the BO is stopped.
2407 int amdgpu_amdkfd_evict_userptr(struct mmu_interval_notifier *mni,
2408 unsigned long cur_seq, struct kgd_mem *mem)
2410 struct amdkfd_process_info *process_info = mem->process_info;
2413 /* Do not process MMU notifications during CRIU restore until
2414 * KFD_CRIU_OP_RESUME IOCTL is received
2416 if (READ_ONCE(process_info->block_mmu_notifications))
2419 mutex_lock(&process_info->notifier_lock);
2420 mmu_interval_set_seq(mni, cur_seq);
2423 if (++process_info->evicted_bos == 1) {
2424 /* First eviction, stop the queues */
2425 r = kgd2kfd_quiesce_mm(mni->mm,
2426 KFD_QUEUE_EVICTION_TRIGGER_USERPTR);
2428 pr_err("Failed to quiesce KFD\n");
2429 schedule_delayed_work(&process_info->restore_userptr_work,
2430 msecs_to_jiffies(AMDGPU_USERPTR_RESTORE_DELAY_MS));
2432 mutex_unlock(&process_info->notifier_lock);
2437 /* Update invalid userptr BOs
2439 * Moves invalidated (evicted) userptr BOs from userptr_valid_list to
2440 * userptr_inval_list and updates user pages for all BOs that have
2441 * been invalidated since their last update.
2443 static int update_invalid_user_pages(struct amdkfd_process_info *process_info,
2444 struct mm_struct *mm)
2446 struct kgd_mem *mem, *tmp_mem;
2447 struct amdgpu_bo *bo;
2448 struct ttm_operation_ctx ctx = { false, false };
2452 mutex_lock(&process_info->notifier_lock);
2454 /* Move all invalidated BOs to the userptr_inval_list */
2455 list_for_each_entry_safe(mem, tmp_mem,
2456 &process_info->userptr_valid_list,
2459 list_move_tail(&mem->validate_list,
2460 &process_info->userptr_inval_list);
2462 /* Go through userptr_inval_list and update any invalid user_pages */
2463 list_for_each_entry(mem, &process_info->userptr_inval_list,
2465 invalid = mem->invalid;
2467 /* BO hasn't been invalidated since the last
2468 * revalidation attempt. Keep its page list.
2474 amdgpu_ttm_tt_discard_user_pages(bo->tbo.ttm, mem->range);
2477 /* BO reservations and getting user pages (hmm_range_fault)
2478 * must happen outside the notifier lock
2480 mutex_unlock(&process_info->notifier_lock);
2482 /* Move the BO to system (CPU) domain if necessary to unmap
2483 * and free the SG table
2485 if (bo->tbo.resource->mem_type != TTM_PL_SYSTEM) {
2486 if (amdgpu_bo_reserve(bo, true))
2488 amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_CPU);
2489 ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
2490 amdgpu_bo_unreserve(bo);
2492 pr_err("%s: Failed to invalidate userptr BO\n",
2498 /* Get updated user pages */
2499 ret = amdgpu_ttm_tt_get_user_pages(bo, bo->tbo.ttm->pages,
2502 pr_debug("Failed %d to get user pages\n", ret);
2504 /* Return -EFAULT bad address error as success. It will
2505 * fail later with a VM fault if the GPU tries to access
2506 * it. Better than hanging indefinitely with stalled
2509 * Return other error -EBUSY or -ENOMEM to retry restore
2517 mutex_lock(&process_info->notifier_lock);
2519 /* Mark the BO as valid unless it was invalidated
2520 * again concurrently.
2522 if (mem->invalid != invalid) {
2526 /* set mem valid if mem has hmm range associated */
2532 mutex_unlock(&process_info->notifier_lock);
2537 /* Validate invalid userptr BOs
2539 * Validates BOs on the userptr_inval_list. Also updates GPUVM page tables
2540 * with new page addresses and waits for the page table updates to complete.
2542 static int validate_invalid_user_pages(struct amdkfd_process_info *process_info)
2544 struct ttm_operation_ctx ctx = { false, false };
2545 struct amdgpu_sync sync;
2546 struct drm_exec exec;
2548 struct amdgpu_vm *peer_vm;
2549 struct kgd_mem *mem, *tmp_mem;
2550 struct amdgpu_bo *bo;
2553 amdgpu_sync_create(&sync);
2555 drm_exec_init(&exec, 0);
2556 /* Reserve all BOs and page tables for validation */
2557 drm_exec_until_all_locked(&exec) {
2558 /* Reserve all the page directories */
2559 list_for_each_entry(peer_vm, &process_info->vm_list_head,
2561 ret = amdgpu_vm_lock_pd(peer_vm, &exec, 2);
2562 drm_exec_retry_on_contention(&exec);
2567 /* Reserve the userptr_inval_list entries to resv_list */
2568 list_for_each_entry(mem, &process_info->userptr_inval_list,
2570 struct drm_gem_object *gobj;
2572 gobj = &mem->bo->tbo.base;
2573 ret = drm_exec_prepare_obj(&exec, gobj, 1);
2574 drm_exec_retry_on_contention(&exec);
2580 ret = process_validate_vms(process_info);
2584 /* Validate BOs and update GPUVM page tables */
2585 list_for_each_entry_safe(mem, tmp_mem,
2586 &process_info->userptr_inval_list,
2588 struct kfd_mem_attachment *attachment;
2592 /* Validate the BO if we got user pages */
2593 if (bo->tbo.ttm->pages[0]) {
2594 amdgpu_bo_placement_from_domain(bo, mem->domain);
2595 ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
2597 pr_err("%s: failed to validate BO\n", __func__);
2602 /* Update mapping. If the BO was not validated
2603 * (because we couldn't get user pages), this will
2604 * clear the page table entries, which will result in
2605 * VM faults if the GPU tries to access the invalid
2608 list_for_each_entry(attachment, &mem->attachments, list) {
2609 if (!attachment->is_mapped)
2612 kfd_mem_dmaunmap_attachment(mem, attachment);
2613 ret = update_gpuvm_pte(mem, attachment, &sync);
2615 pr_err("%s: update PTE failed\n", __func__);
2616 /* make sure this gets validated again */
2617 mutex_lock(&process_info->notifier_lock);
2619 mutex_unlock(&process_info->notifier_lock);
2625 /* Update page directories */
2626 ret = process_update_pds(process_info, &sync);
2629 drm_exec_fini(&exec);
2630 amdgpu_sync_wait(&sync, false);
2631 amdgpu_sync_free(&sync);
2636 /* Confirm that all user pages are valid while holding the notifier lock
2638 * Moves valid BOs from the userptr_inval_list back to userptr_val_list.
2640 static int confirm_valid_user_pages_locked(struct amdkfd_process_info *process_info)
2642 struct kgd_mem *mem, *tmp_mem;
2645 list_for_each_entry_safe(mem, tmp_mem,
2646 &process_info->userptr_inval_list,
2650 /* keep mem without hmm range at userptr_inval_list */
2654 /* Only check mem with hmm range associated */
2655 valid = amdgpu_ttm_tt_get_user_pages_done(
2656 mem->bo->tbo.ttm, mem->range);
2660 WARN(!mem->invalid, "Invalid BO not marked invalid");
2666 WARN(1, "Valid BO is marked invalid");
2671 list_move_tail(&mem->validate_list,
2672 &process_info->userptr_valid_list);
2678 /* Worker callback to restore evicted userptr BOs
2680 * Tries to update and validate all userptr BOs. If successful and no
2681 * concurrent evictions happened, the queues are restarted. Otherwise,
2682 * reschedule for another attempt later.
2684 static void amdgpu_amdkfd_restore_userptr_worker(struct work_struct *work)
2686 struct delayed_work *dwork = to_delayed_work(work);
2687 struct amdkfd_process_info *process_info =
2688 container_of(dwork, struct amdkfd_process_info,
2689 restore_userptr_work);
2690 struct task_struct *usertask;
2691 struct mm_struct *mm;
2692 uint32_t evicted_bos;
2694 mutex_lock(&process_info->notifier_lock);
2695 evicted_bos = process_info->evicted_bos;
2696 mutex_unlock(&process_info->notifier_lock);
2700 /* Reference task and mm in case of concurrent process termination */
2701 usertask = get_pid_task(process_info->pid, PIDTYPE_PID);
2704 mm = get_task_mm(usertask);
2706 put_task_struct(usertask);
2710 mutex_lock(&process_info->lock);
2712 if (update_invalid_user_pages(process_info, mm))
2714 /* userptr_inval_list can be empty if all evicted userptr BOs
2715 * have been freed. In that case there is nothing to validate
2716 * and we can just restart the queues.
2718 if (!list_empty(&process_info->userptr_inval_list)) {
2719 if (validate_invalid_user_pages(process_info))
2722 /* Final check for concurrent evicton and atomic update. If
2723 * another eviction happens after successful update, it will
2724 * be a first eviction that calls quiesce_mm. The eviction
2725 * reference counting inside KFD will handle this case.
2727 mutex_lock(&process_info->notifier_lock);
2728 if (process_info->evicted_bos != evicted_bos)
2729 goto unlock_notifier_out;
2731 if (confirm_valid_user_pages_locked(process_info)) {
2732 WARN(1, "User pages unexpectedly invalid");
2733 goto unlock_notifier_out;
2736 process_info->evicted_bos = evicted_bos = 0;
2738 if (kgd2kfd_resume_mm(mm)) {
2739 pr_err("%s: Failed to resume KFD\n", __func__);
2740 /* No recovery from this failure. Probably the CP is
2741 * hanging. No point trying again.
2745 unlock_notifier_out:
2746 mutex_unlock(&process_info->notifier_lock);
2748 mutex_unlock(&process_info->lock);
2750 /* If validation failed, reschedule another attempt */
2752 schedule_delayed_work(&process_info->restore_userptr_work,
2753 msecs_to_jiffies(AMDGPU_USERPTR_RESTORE_DELAY_MS));
2755 kfd_smi_event_queue_restore_rescheduled(mm);
2758 put_task_struct(usertask);
2761 /** amdgpu_amdkfd_gpuvm_restore_process_bos - Restore all BOs for the given
2762 * KFD process identified by process_info
2764 * @process_info: amdkfd_process_info of the KFD process
2766 * After memory eviction, restore thread calls this function. The function
2767 * should be called when the Process is still valid. BO restore involves -
2769 * 1. Release old eviction fence and create new one
2770 * 2. Get two copies of PD BO list from all the VMs. Keep one copy as pd_list.
2771 * 3 Use the second PD list and kfd_bo_list to create a list (ctx.list) of
2772 * BOs that need to be reserved.
2773 * 4. Reserve all the BOs
2774 * 5. Validate of PD and PT BOs.
2775 * 6. Validate all KFD BOs using kfd_bo_list and Map them and add new fence
2776 * 7. Add fence to all PD and PT BOs.
2777 * 8. Unreserve all BOs
2779 int amdgpu_amdkfd_gpuvm_restore_process_bos(void *info, struct dma_fence **ef)
2781 struct amdkfd_process_info *process_info = info;
2782 struct amdgpu_vm *peer_vm;
2783 struct kgd_mem *mem;
2784 struct amdgpu_amdkfd_fence *new_fence;
2785 struct list_head duplicate_save;
2786 struct amdgpu_sync sync_obj;
2787 unsigned long failed_size = 0;
2788 unsigned long total_size = 0;
2789 struct drm_exec exec;
2792 INIT_LIST_HEAD(&duplicate_save);
2794 mutex_lock(&process_info->lock);
2796 drm_exec_init(&exec, 0);
2797 drm_exec_until_all_locked(&exec) {
2798 list_for_each_entry(peer_vm, &process_info->vm_list_head,
2800 ret = amdgpu_vm_lock_pd(peer_vm, &exec, 2);
2801 drm_exec_retry_on_contention(&exec);
2803 goto ttm_reserve_fail;
2806 /* Reserve all BOs and page tables/directory. Add all BOs from
2807 * kfd_bo_list to ctx.list
2809 list_for_each_entry(mem, &process_info->kfd_bo_list,
2811 struct drm_gem_object *gobj;
2813 gobj = &mem->bo->tbo.base;
2814 ret = drm_exec_prepare_obj(&exec, gobj, 1);
2815 drm_exec_retry_on_contention(&exec);
2817 goto ttm_reserve_fail;
2821 amdgpu_sync_create(&sync_obj);
2823 /* Validate PDs and PTs */
2824 ret = process_validate_vms(process_info);
2826 goto validate_map_fail;
2828 ret = process_sync_pds_resv(process_info, &sync_obj);
2830 pr_debug("Memory eviction: Failed to sync to PD BO moving fence. Try again\n");
2831 goto validate_map_fail;
2834 /* Validate BOs and map them to GPUVM (update VM page tables). */
2835 list_for_each_entry(mem, &process_info->kfd_bo_list,
2838 struct amdgpu_bo *bo = mem->bo;
2839 uint32_t domain = mem->domain;
2840 struct kfd_mem_attachment *attachment;
2841 struct dma_resv_iter cursor;
2842 struct dma_fence *fence;
2844 total_size += amdgpu_bo_size(bo);
2846 ret = amdgpu_amdkfd_bo_validate(bo, domain, false);
2848 pr_debug("Memory eviction: Validate BOs failed\n");
2849 failed_size += amdgpu_bo_size(bo);
2850 ret = amdgpu_amdkfd_bo_validate(bo,
2851 AMDGPU_GEM_DOMAIN_GTT, false);
2853 pr_debug("Memory eviction: Try again\n");
2854 goto validate_map_fail;
2857 dma_resv_for_each_fence(&cursor, bo->tbo.base.resv,
2858 DMA_RESV_USAGE_KERNEL, fence) {
2859 ret = amdgpu_sync_fence(&sync_obj, fence);
2861 pr_debug("Memory eviction: Sync BO fence failed. Try again\n");
2862 goto validate_map_fail;
2865 list_for_each_entry(attachment, &mem->attachments, list) {
2866 if (!attachment->is_mapped)
2869 if (attachment->bo_va->base.bo->tbo.pin_count)
2872 kfd_mem_dmaunmap_attachment(mem, attachment);
2873 ret = update_gpuvm_pte(mem, attachment, &sync_obj);
2875 pr_debug("Memory eviction: update PTE failed. Try again\n");
2876 goto validate_map_fail;
2882 pr_debug("0x%lx/0x%lx in system\n", failed_size, total_size);
2884 /* Update page directories */
2885 ret = process_update_pds(process_info, &sync_obj);
2887 pr_debug("Memory eviction: update PDs failed. Try again\n");
2888 goto validate_map_fail;
2891 /* Wait for validate and PT updates to finish */
2892 amdgpu_sync_wait(&sync_obj, false);
2894 /* Release old eviction fence and create new one, because fence only
2895 * goes from unsignaled to signaled, fence cannot be reused.
2896 * Use context and mm from the old fence.
2898 new_fence = amdgpu_amdkfd_fence_create(
2899 process_info->eviction_fence->base.context,
2900 process_info->eviction_fence->mm,
2903 pr_err("Failed to create eviction fence\n");
2905 goto validate_map_fail;
2907 dma_fence_put(&process_info->eviction_fence->base);
2908 process_info->eviction_fence = new_fence;
2909 *ef = dma_fence_get(&new_fence->base);
2911 /* Attach new eviction fence to all BOs except pinned ones */
2912 list_for_each_entry(mem, &process_info->kfd_bo_list, validate_list) {
2913 if (mem->bo->tbo.pin_count)
2916 dma_resv_add_fence(mem->bo->tbo.base.resv,
2917 &process_info->eviction_fence->base,
2918 DMA_RESV_USAGE_BOOKKEEP);
2920 /* Attach eviction fence to PD / PT BOs */
2921 list_for_each_entry(peer_vm, &process_info->vm_list_head,
2923 struct amdgpu_bo *bo = peer_vm->root.bo;
2925 dma_resv_add_fence(bo->tbo.base.resv,
2926 &process_info->eviction_fence->base,
2927 DMA_RESV_USAGE_BOOKKEEP);
2931 amdgpu_sync_free(&sync_obj);
2933 drm_exec_fini(&exec);
2934 mutex_unlock(&process_info->lock);
2938 int amdgpu_amdkfd_add_gws_to_process(void *info, void *gws, struct kgd_mem **mem)
2940 struct amdkfd_process_info *process_info = (struct amdkfd_process_info *)info;
2941 struct amdgpu_bo *gws_bo = (struct amdgpu_bo *)gws;
2947 *mem = kzalloc(sizeof(struct kgd_mem), GFP_KERNEL);
2951 mutex_init(&(*mem)->lock);
2952 INIT_LIST_HEAD(&(*mem)->attachments);
2953 (*mem)->bo = amdgpu_bo_ref(gws_bo);
2954 (*mem)->domain = AMDGPU_GEM_DOMAIN_GWS;
2955 (*mem)->process_info = process_info;
2956 add_kgd_mem_to_kfd_bo_list(*mem, process_info, false);
2957 amdgpu_sync_create(&(*mem)->sync);
2960 /* Validate gws bo the first time it is added to process */
2961 mutex_lock(&(*mem)->process_info->lock);
2962 ret = amdgpu_bo_reserve(gws_bo, false);
2963 if (unlikely(ret)) {
2964 pr_err("Reserve gws bo failed %d\n", ret);
2965 goto bo_reservation_failure;
2968 ret = amdgpu_amdkfd_bo_validate(gws_bo, AMDGPU_GEM_DOMAIN_GWS, true);
2970 pr_err("GWS BO validate failed %d\n", ret);
2971 goto bo_validation_failure;
2973 /* GWS resource is shared b/t amdgpu and amdkfd
2974 * Add process eviction fence to bo so they can
2977 ret = dma_resv_reserve_fences(gws_bo->tbo.base.resv, 1);
2979 goto reserve_shared_fail;
2980 dma_resv_add_fence(gws_bo->tbo.base.resv,
2981 &process_info->eviction_fence->base,
2982 DMA_RESV_USAGE_BOOKKEEP);
2983 amdgpu_bo_unreserve(gws_bo);
2984 mutex_unlock(&(*mem)->process_info->lock);
2988 reserve_shared_fail:
2989 bo_validation_failure:
2990 amdgpu_bo_unreserve(gws_bo);
2991 bo_reservation_failure:
2992 mutex_unlock(&(*mem)->process_info->lock);
2993 amdgpu_sync_free(&(*mem)->sync);
2994 remove_kgd_mem_from_kfd_bo_list(*mem, process_info);
2995 amdgpu_bo_unref(&gws_bo);
2996 mutex_destroy(&(*mem)->lock);
3002 int amdgpu_amdkfd_remove_gws_from_process(void *info, void *mem)
3005 struct amdkfd_process_info *process_info = (struct amdkfd_process_info *)info;
3006 struct kgd_mem *kgd_mem = (struct kgd_mem *)mem;
3007 struct amdgpu_bo *gws_bo = kgd_mem->bo;
3009 /* Remove BO from process's validate list so restore worker won't touch
3012 remove_kgd_mem_from_kfd_bo_list(kgd_mem, process_info);
3014 ret = amdgpu_bo_reserve(gws_bo, false);
3015 if (unlikely(ret)) {
3016 pr_err("Reserve gws bo failed %d\n", ret);
3017 //TODO add BO back to validate_list?
3020 amdgpu_amdkfd_remove_eviction_fence(gws_bo,
3021 process_info->eviction_fence);
3022 amdgpu_bo_unreserve(gws_bo);
3023 amdgpu_sync_free(&kgd_mem->sync);
3024 amdgpu_bo_unref(&gws_bo);
3025 mutex_destroy(&kgd_mem->lock);
3030 /* Returns GPU-specific tiling mode information */
3031 int amdgpu_amdkfd_get_tile_config(struct amdgpu_device *adev,
3032 struct tile_config *config)
3034 config->gb_addr_config = adev->gfx.config.gb_addr_config;
3035 config->tile_config_ptr = adev->gfx.config.tile_mode_array;
3036 config->num_tile_configs =
3037 ARRAY_SIZE(adev->gfx.config.tile_mode_array);
3038 config->macro_tile_config_ptr =
3039 adev->gfx.config.macrotile_mode_array;
3040 config->num_macro_tile_configs =
3041 ARRAY_SIZE(adev->gfx.config.macrotile_mode_array);
3043 /* Those values are not set from GFX9 onwards */
3044 config->num_banks = adev->gfx.config.num_banks;
3045 config->num_ranks = adev->gfx.config.num_ranks;
3050 bool amdgpu_amdkfd_bo_mapped_to_dev(struct amdgpu_device *adev, struct kgd_mem *mem)
3052 struct kfd_mem_attachment *entry;
3054 list_for_each_entry(entry, &mem->attachments, list) {
3055 if (entry->is_mapped && entry->adev == adev)
3061 #if defined(CONFIG_DEBUG_FS)
3063 int kfd_debugfs_kfd_mem_limits(struct seq_file *m, void *data)
3066 spin_lock(&kfd_mem_limit.mem_limit_lock);
3067 seq_printf(m, "System mem used %lldM out of %lluM\n",
3068 (kfd_mem_limit.system_mem_used >> 20),
3069 (kfd_mem_limit.max_system_mem_limit >> 20));
3070 seq_printf(m, "TTM mem used %lldM out of %lluM\n",
3071 (kfd_mem_limit.ttm_mem_used >> 20),
3072 (kfd_mem_limit.max_ttm_mem_limit >> 20));
3073 spin_unlock(&kfd_mem_limit.mem_limit_lock);