Lines Matching full:mem
77 struct kgd_mem *mem) in check_if_add_bo_to_vm() argument
81 list_for_each_entry(entry, &mem->bo_va_list, bo_list) in check_if_add_bo_to_vm()
95 uint64_t mem; in amdgpu_amdkfd_gpuvm_init_mem_limits() local
98 mem = si.totalram - si.totalhigh; in amdgpu_amdkfd_gpuvm_init_mem_limits()
99 mem *= si.mem_unit; in amdgpu_amdkfd_gpuvm_init_mem_limits()
102 kfd_mem_limit.max_system_mem_limit = mem - (mem >> 4); in amdgpu_amdkfd_gpuvm_init_mem_limits()
103 kfd_mem_limit.max_ttm_mem_limit = (mem >> 1) - (mem >> 3); in amdgpu_amdkfd_gpuvm_init_mem_limits()
403 static uint64_t get_pte_flags(struct amdgpu_device *adev, struct kgd_mem *mem) in get_pte_flags() argument
405 struct amdgpu_device *bo_adev = amdgpu_ttm_adev(mem->bo->tbo.bdev); in get_pte_flags()
406 bool coherent = mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_COHERENT; in get_pte_flags()
410 if (mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE) in get_pte_flags()
412 if (mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_EXECUTABLE) in get_pte_flags()
417 if (mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_VRAM) { in get_pte_flags()
448 static int add_bo_to_vm(struct amdgpu_device *adev, struct kgd_mem *mem, in add_bo_to_vm() argument
454 struct amdgpu_bo *bo = mem->bo; in add_bo_to_vm()
455 uint64_t va = mem->va; in add_bo_to_vm()
456 struct list_head *list_bo_va = &mem->bo_va_list; in add_bo_to_vm()
457 unsigned long bo_size = bo->tbo.mem.size; in add_bo_to_vm()
484 bo_va_entry->pte_flags = get_pte_flags(adev, mem); in add_bo_to_vm()
519 static void add_kgd_mem_to_kfd_bo_list(struct kgd_mem *mem, in add_kgd_mem_to_kfd_bo_list() argument
523 struct ttm_validate_buffer *entry = &mem->validate_list; in add_kgd_mem_to_kfd_bo_list()
524 struct amdgpu_bo *bo = mem->bo; in add_kgd_mem_to_kfd_bo_list()
537 static void remove_kgd_mem_from_kfd_bo_list(struct kgd_mem *mem, in remove_kgd_mem_from_kfd_bo_list() argument
542 bo_list_entry = &mem->validate_list; in remove_kgd_mem_from_kfd_bo_list()
560 static int init_user_pages(struct kgd_mem *mem, uint64_t user_addr) in init_user_pages() argument
562 struct amdkfd_process_info *process_info = mem->process_info; in init_user_pages()
563 struct amdgpu_bo *bo = mem->bo; in init_user_pages()
593 amdgpu_bo_placement_from_domain(bo, mem->domain); in init_user_pages()
632 * @mem: KFD BO structure.
636 static int reserve_bo_and_vm(struct kgd_mem *mem, in reserve_bo_and_vm() argument
640 struct amdgpu_bo *bo = mem->bo; in reserve_bo_and_vm()
647 ctx->sync = &mem->sync; in reserve_bo_and_vm()
678 * @mem: KFD BO structure.
686 static int reserve_bo_and_cond_vms(struct kgd_mem *mem, in reserve_bo_and_cond_vms() argument
690 struct amdgpu_bo *bo = mem->bo; in reserve_bo_and_cond_vms()
698 ctx->sync = &mem->sync; in reserve_bo_and_cond_vms()
703 list_for_each_entry(entry, &mem->bo_va_list, bo_list) { in reserve_bo_and_cond_vms()
725 list_for_each_entry(entry, &mem->bo_va_list, bo_list) { in reserve_bo_and_cond_vms()
1147 void *vm, struct kgd_mem **mem, in amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu() argument
1195 *mem = kzalloc(sizeof(struct kgd_mem), GFP_KERNEL); in amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu()
1196 if (!*mem) { in amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu()
1200 INIT_LIST_HEAD(&(*mem)->bo_va_list); in amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu()
1201 mutex_init(&(*mem)->lock); in amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu()
1202 (*mem)->aql_queue = !!(flags & KFD_IOC_ALLOC_MEM_FLAGS_AQL_QUEUE_MEM); in amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu()
1208 if ((*mem)->aql_queue) in amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu()
1211 (*mem)->alloc_flags = flags; in amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu()
1213 amdgpu_sync_create(&(*mem)->sync); in amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu()
1241 bo->kfd_bo = *mem; in amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu()
1242 (*mem)->bo = bo; in amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu()
1246 (*mem)->va = va; in amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu()
1247 (*mem)->domain = domain; in amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu()
1248 (*mem)->mapped_to_gpu_memory = 0; in amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu()
1249 (*mem)->process_info = avm->process_info; in amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu()
1250 add_kgd_mem_to_kfd_bo_list(*mem, avm->process_info, user_addr); in amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu()
1253 ret = init_user_pages(*mem, user_addr); in amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu()
1264 remove_kgd_mem_from_kfd_bo_list(*mem, avm->process_info); in amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu()
1266 /* Don't unreserve system mem limit twice */ in amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu()
1271 mutex_destroy(&(*mem)->lock); in amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu()
1272 kfree(*mem); in amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu()
1282 struct kgd_dev *kgd, struct kgd_mem *mem, uint64_t *size) in amdgpu_amdkfd_gpuvm_free_memory_of_gpu() argument
1284 struct amdkfd_process_info *process_info = mem->process_info; in amdgpu_amdkfd_gpuvm_free_memory_of_gpu()
1285 unsigned long bo_size = mem->bo->tbo.mem.size; in amdgpu_amdkfd_gpuvm_free_memory_of_gpu()
1293 mutex_lock(&mem->lock); in amdgpu_amdkfd_gpuvm_free_memory_of_gpu()
1294 mapped_to_gpu_memory = mem->mapped_to_gpu_memory; in amdgpu_amdkfd_gpuvm_free_memory_of_gpu()
1295 is_imported = mem->is_imported; in amdgpu_amdkfd_gpuvm_free_memory_of_gpu()
1296 mutex_unlock(&mem->lock); in amdgpu_amdkfd_gpuvm_free_memory_of_gpu()
1297 /* lock is not needed after this, since mem is unused and will in amdgpu_amdkfd_gpuvm_free_memory_of_gpu()
1303 mem->va, bo_size); in amdgpu_amdkfd_gpuvm_free_memory_of_gpu()
1308 bo_list_entry = &mem->validate_list; in amdgpu_amdkfd_gpuvm_free_memory_of_gpu()
1314 amdgpu_mn_unregister(mem->bo); in amdgpu_amdkfd_gpuvm_free_memory_of_gpu()
1316 ret = reserve_bo_and_cond_vms(mem, NULL, BO_VM_ALL, &ctx); in amdgpu_amdkfd_gpuvm_free_memory_of_gpu()
1324 amdgpu_amdkfd_remove_eviction_fence(mem->bo, in amdgpu_amdkfd_gpuvm_free_memory_of_gpu()
1326 pr_debug("Release VA 0x%llx - 0x%llx\n", mem->va, in amdgpu_amdkfd_gpuvm_free_memory_of_gpu()
1327 mem->va + bo_size * (1 + mem->aql_queue)); in amdgpu_amdkfd_gpuvm_free_memory_of_gpu()
1330 list_for_each_entry_safe(entry, tmp, &mem->bo_va_list, bo_list) in amdgpu_amdkfd_gpuvm_free_memory_of_gpu()
1337 amdgpu_sync_free(&mem->sync); in amdgpu_amdkfd_gpuvm_free_memory_of_gpu()
1342 if (mem->bo->tbo.sg) { in amdgpu_amdkfd_gpuvm_free_memory_of_gpu()
1343 sg_free_table(mem->bo->tbo.sg); in amdgpu_amdkfd_gpuvm_free_memory_of_gpu()
1344 kfree(mem->bo->tbo.sg); in amdgpu_amdkfd_gpuvm_free_memory_of_gpu()
1351 if ((mem->bo->preferred_domains == AMDGPU_GEM_DOMAIN_VRAM) && in amdgpu_amdkfd_gpuvm_free_memory_of_gpu()
1359 drm_gem_object_put(&mem->bo->tbo.base); in amdgpu_amdkfd_gpuvm_free_memory_of_gpu()
1360 mutex_destroy(&mem->lock); in amdgpu_amdkfd_gpuvm_free_memory_of_gpu()
1361 kfree(mem); in amdgpu_amdkfd_gpuvm_free_memory_of_gpu()
1367 struct kgd_dev *kgd, struct kgd_mem *mem, void *vm) in amdgpu_amdkfd_gpuvm_map_memory_to_gpu() argument
1381 bo = mem->bo; in amdgpu_amdkfd_gpuvm_map_memory_to_gpu()
1391 mutex_lock(&mem->process_info->lock); in amdgpu_amdkfd_gpuvm_map_memory_to_gpu()
1399 is_invalid_userptr = atomic_read(&mem->invalid); in amdgpu_amdkfd_gpuvm_map_memory_to_gpu()
1403 mutex_lock(&mem->lock); in amdgpu_amdkfd_gpuvm_map_memory_to_gpu()
1405 domain = mem->domain; in amdgpu_amdkfd_gpuvm_map_memory_to_gpu()
1406 bo_size = bo->tbo.mem.size; in amdgpu_amdkfd_gpuvm_map_memory_to_gpu()
1409 mem->va, in amdgpu_amdkfd_gpuvm_map_memory_to_gpu()
1410 mem->va + bo_size * (1 + mem->aql_queue), in amdgpu_amdkfd_gpuvm_map_memory_to_gpu()
1413 ret = reserve_bo_and_vm(mem, vm, &ctx); in amdgpu_amdkfd_gpuvm_map_memory_to_gpu()
1423 bo->tbo.mem.mem_type == TTM_PL_SYSTEM) in amdgpu_amdkfd_gpuvm_map_memory_to_gpu()
1426 if (check_if_add_bo_to_vm(avm, mem)) { in amdgpu_amdkfd_gpuvm_map_memory_to_gpu()
1427 ret = add_bo_to_vm(adev, mem, avm, false, in amdgpu_amdkfd_gpuvm_map_memory_to_gpu()
1431 if (mem->aql_queue) { in amdgpu_amdkfd_gpuvm_map_memory_to_gpu()
1432 ret = add_bo_to_vm(adev, mem, avm, in amdgpu_amdkfd_gpuvm_map_memory_to_gpu()
1443 if (mem->mapped_to_gpu_memory == 0 && in amdgpu_amdkfd_gpuvm_map_memory_to_gpu()
1456 list_for_each_entry(entry, &mem->bo_va_list, bo_list) { in amdgpu_amdkfd_gpuvm_map_memory_to_gpu()
1476 mem->mapped_to_gpu_memory++; in amdgpu_amdkfd_gpuvm_map_memory_to_gpu()
1478 mem->mapped_to_gpu_memory); in amdgpu_amdkfd_gpuvm_map_memory_to_gpu()
1499 mutex_unlock(&mem->process_info->lock); in amdgpu_amdkfd_gpuvm_map_memory_to_gpu()
1500 mutex_unlock(&mem->lock); in amdgpu_amdkfd_gpuvm_map_memory_to_gpu()
1505 struct kgd_dev *kgd, struct kgd_mem *mem, void *vm) in amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu() argument
1510 unsigned long bo_size = mem->bo->tbo.mem.size; in amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu()
1515 mutex_lock(&mem->lock); in amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu()
1517 ret = reserve_bo_and_cond_vms(mem, vm, BO_VM_MAPPED, &ctx); in amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu()
1531 mem->va, in amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu()
1532 mem->va + bo_size * (1 + mem->aql_queue), in amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu()
1535 list_for_each_entry(entry, &mem->bo_va_list, bo_list) { in amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu()
1547 mem->va); in amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu()
1551 mem->mapped_to_gpu_memory--; in amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu()
1553 mem->mapped_to_gpu_memory); in amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu()
1560 if (mem->mapped_to_gpu_memory == 0 && in amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu()
1561 !amdgpu_ttm_tt_get_usermm(mem->bo->tbo.ttm) && !mem->bo->pin_count) in amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu()
1562 amdgpu_amdkfd_remove_eviction_fence(mem->bo, in amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu()
1568 mutex_unlock(&mem->lock); in amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu()
1573 struct kgd_dev *kgd, struct kgd_mem *mem, bool intr) in amdgpu_amdkfd_gpuvm_sync_memory() argument
1580 mutex_lock(&mem->lock); in amdgpu_amdkfd_gpuvm_sync_memory()
1581 amdgpu_sync_clone(&mem->sync, &sync); in amdgpu_amdkfd_gpuvm_sync_memory()
1582 mutex_unlock(&mem->lock); in amdgpu_amdkfd_gpuvm_sync_memory()
1590 struct kgd_mem *mem, void **kptr, uint64_t *size) in amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel() argument
1593 struct amdgpu_bo *bo = mem->bo; in amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel()
1603 mutex_lock(&mem->process_info->lock); in amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel()
1624 bo, mem->process_info->eviction_fence); in amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel()
1625 list_del_init(&mem->validate_list.head); in amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel()
1632 mutex_unlock(&mem->process_info->lock); in amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel()
1640 mutex_unlock(&mem->process_info->lock); in amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel()
1646 struct kfd_vm_fault_info *mem) in amdgpu_amdkfd_gpuvm_get_vm_fault_info() argument
1652 *mem = *adev->gmc.vm_fault_info; in amdgpu_amdkfd_gpuvm_get_vm_fault_info()
1662 struct kgd_mem **mem, uint64_t *size, in amdgpu_amdkfd_gpuvm_import_dmabuf() argument
1685 *mem = kzalloc(sizeof(struct kgd_mem), GFP_KERNEL); in amdgpu_amdkfd_gpuvm_import_dmabuf()
1686 if (!*mem) in amdgpu_amdkfd_gpuvm_import_dmabuf()
1695 INIT_LIST_HEAD(&(*mem)->bo_va_list); in amdgpu_amdkfd_gpuvm_import_dmabuf()
1696 mutex_init(&(*mem)->lock); in amdgpu_amdkfd_gpuvm_import_dmabuf()
1698 (*mem)->alloc_flags = in amdgpu_amdkfd_gpuvm_import_dmabuf()
1705 (*mem)->bo = bo; in amdgpu_amdkfd_gpuvm_import_dmabuf()
1706 (*mem)->va = va; in amdgpu_amdkfd_gpuvm_import_dmabuf()
1707 (*mem)->domain = (bo->preferred_domains & AMDGPU_GEM_DOMAIN_VRAM) ? in amdgpu_amdkfd_gpuvm_import_dmabuf()
1709 (*mem)->mapped_to_gpu_memory = 0; in amdgpu_amdkfd_gpuvm_import_dmabuf()
1710 (*mem)->process_info = avm->process_info; in amdgpu_amdkfd_gpuvm_import_dmabuf()
1711 add_kgd_mem_to_kfd_bo_list(*mem, avm->process_info, false); in amdgpu_amdkfd_gpuvm_import_dmabuf()
1712 amdgpu_sync_create(&(*mem)->sync); in amdgpu_amdkfd_gpuvm_import_dmabuf()
1713 (*mem)->is_imported = true; in amdgpu_amdkfd_gpuvm_import_dmabuf()
1729 int amdgpu_amdkfd_evict_userptr(struct kgd_mem *mem, in amdgpu_amdkfd_evict_userptr() argument
1732 struct amdkfd_process_info *process_info = mem->process_info; in amdgpu_amdkfd_evict_userptr()
1736 atomic_inc(&mem->invalid); in amdgpu_amdkfd_evict_userptr()
1759 struct kgd_mem *mem, *tmp_mem; in update_invalid_user_pages() local
1767 list_for_each_entry_safe(mem, tmp_mem, in update_invalid_user_pages()
1770 if (!atomic_read(&mem->invalid)) in update_invalid_user_pages()
1773 bo = mem->bo; in update_invalid_user_pages()
1786 list_move_tail(&mem->validate_list.head, in update_invalid_user_pages()
1794 list_for_each_entry(mem, &process_info->userptr_inval_list, in update_invalid_user_pages()
1796 invalid = atomic_read(&mem->invalid); in update_invalid_user_pages()
1803 bo = mem->bo; in update_invalid_user_pages()
1824 if (atomic_cmpxchg(&mem->invalid, invalid, 0) != invalid) in update_invalid_user_pages()
1845 struct kgd_mem *mem, *tmp_mem; in validate_invalid_user_pages() local
1869 list_for_each_entry(mem, &process_info->userptr_inval_list, in validate_invalid_user_pages()
1871 list_add_tail(&mem->resv_list.head, &resv_list); in validate_invalid_user_pages()
1872 mem->resv_list.bo = mem->validate_list.bo; in validate_invalid_user_pages()
1873 mem->resv_list.num_shared = mem->validate_list.num_shared; in validate_invalid_user_pages()
1889 list_for_each_entry_safe(mem, tmp_mem, in validate_invalid_user_pages()
1894 bo = mem->bo; in validate_invalid_user_pages()
1898 amdgpu_bo_placement_from_domain(bo, mem->domain); in validate_invalid_user_pages()
1906 list_move_tail(&mem->validate_list.head, in validate_invalid_user_pages()
1915 list_for_each_entry(bo_va_entry, &mem->bo_va_list, bo_list) { in validate_invalid_user_pages()
1925 atomic_inc(&mem->invalid); in validate_invalid_user_pages()
2040 struct kgd_mem *mem; in amdgpu_amdkfd_gpuvm_restore_process_bos() local
2066 list_for_each_entry(mem, &process_info->kfd_bo_list, in amdgpu_amdkfd_gpuvm_restore_process_bos()
2069 list_add_tail(&mem->resv_list.head, &ctx.list); in amdgpu_amdkfd_gpuvm_restore_process_bos()
2070 mem->resv_list.bo = mem->validate_list.bo; in amdgpu_amdkfd_gpuvm_restore_process_bos()
2071 mem->resv_list.num_shared = mem->validate_list.num_shared; in amdgpu_amdkfd_gpuvm_restore_process_bos()
2095 list_for_each_entry(mem, &process_info->kfd_bo_list, in amdgpu_amdkfd_gpuvm_restore_process_bos()
2098 struct amdgpu_bo *bo = mem->bo; in amdgpu_amdkfd_gpuvm_restore_process_bos()
2099 uint32_t domain = mem->domain; in amdgpu_amdkfd_gpuvm_restore_process_bos()
2112 list_for_each_entry(bo_va_entry, &mem->bo_va_list, in amdgpu_amdkfd_gpuvm_restore_process_bos()
2152 list_for_each_entry(mem, &process_info->kfd_bo_list, in amdgpu_amdkfd_gpuvm_restore_process_bos()
2154 amdgpu_bo_fence(mem->bo, in amdgpu_amdkfd_gpuvm_restore_process_bos()
2174 int amdgpu_amdkfd_add_gws_to_process(void *info, void *gws, struct kgd_mem **mem) in amdgpu_amdkfd_add_gws_to_process() argument
2183 *mem = kzalloc(sizeof(struct kgd_mem), GFP_KERNEL); in amdgpu_amdkfd_add_gws_to_process()
2184 if (!*mem) in amdgpu_amdkfd_add_gws_to_process()
2187 mutex_init(&(*mem)->lock); in amdgpu_amdkfd_add_gws_to_process()
2188 INIT_LIST_HEAD(&(*mem)->bo_va_list); in amdgpu_amdkfd_add_gws_to_process()
2189 (*mem)->bo = amdgpu_bo_ref(gws_bo); in amdgpu_amdkfd_add_gws_to_process()
2190 (*mem)->domain = AMDGPU_GEM_DOMAIN_GWS; in amdgpu_amdkfd_add_gws_to_process()
2191 (*mem)->process_info = process_info; in amdgpu_amdkfd_add_gws_to_process()
2192 add_kgd_mem_to_kfd_bo_list(*mem, process_info, false); in amdgpu_amdkfd_add_gws_to_process()
2193 amdgpu_sync_create(&(*mem)->sync); in amdgpu_amdkfd_add_gws_to_process()
2197 mutex_lock(&(*mem)->process_info->lock); in amdgpu_amdkfd_add_gws_to_process()
2218 mutex_unlock(&(*mem)->process_info->lock); in amdgpu_amdkfd_add_gws_to_process()
2226 mutex_unlock(&(*mem)->process_info->lock); in amdgpu_amdkfd_add_gws_to_process()
2227 amdgpu_sync_free(&(*mem)->sync); in amdgpu_amdkfd_add_gws_to_process()
2228 remove_kgd_mem_from_kfd_bo_list(*mem, process_info); in amdgpu_amdkfd_add_gws_to_process()
2230 mutex_destroy(&(*mem)->lock); in amdgpu_amdkfd_add_gws_to_process()
2231 kfree(*mem); in amdgpu_amdkfd_add_gws_to_process()
2232 *mem = NULL; in amdgpu_amdkfd_add_gws_to_process()
2236 int amdgpu_amdkfd_remove_gws_from_process(void *info, void *mem) in amdgpu_amdkfd_remove_gws_from_process() argument
2240 struct kgd_mem *kgd_mem = (struct kgd_mem *)mem; in amdgpu_amdkfd_remove_gws_from_process()
2260 kfree(mem); in amdgpu_amdkfd_remove_gws_from_process()