1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2024, Advanced Micro Devices, Inc. 4 */ 5 6 #include <drm/amdxdna_accel.h> 7 #include <drm/drm_cache.h> 8 #include <drm/drm_device.h> 9 #include <drm/drm_gem.h> 10 #include <drm/drm_gem_shmem_helper.h> 11 #include <drm/drm_print.h> 12 #include <drm/gpu_scheduler.h> 13 #include <linux/dma-buf.h> 14 #include <linux/dma-direct.h> 15 #include <linux/iosys-map.h> 16 #include <linux/pagemap.h> 17 #include <linux/vmalloc.h> 18 19 #include "amdxdna_cbuf.h" 20 #include "amdxdna_ctx.h" 21 #include "amdxdna_gem.h" 22 #include "amdxdna_pci_drv.h" 23 #include "amdxdna_ubuf.h" 24 25 MODULE_IMPORT_NS("DMA_BUF"); 26 27 /* 28 * The dev BO could be across multiple heap BO chunks. The heap chunks should 29 * be mapped to userspace and the userspace virtual address should be 30 * contiguous. 31 */ 32 static int 33 amdxdna_init_dev_bo(struct amdxdna_gem_obj *dev_bo) 34 { 35 struct amdxdna_client *client = dev_bo->client; 36 struct amdxdna_dev *xdna = client->xdna; 37 struct amdxdna_gem_obj *heap; 38 u64 heap_addr, exp_heap_uva; 39 u32 heap_id; 40 41 if (xa_empty(&client->dev_heap_xa)) { 42 XDNA_DBG(xdna, "Empty heap xa"); 43 return -EAGAIN; 44 } 45 46 for (heap_id = 0; heap_id < client->dev_heap_nid; heap_id++) { 47 heap = xa_load(&client->dev_heap_xa, heap_id); 48 if (!heap) { 49 XDNA_ERR(xdna, "Failed to load heap %d", heap_id); 50 return -EINVAL; 51 } 52 heap_addr = amdxdna_gem_dev_addr(heap); 53 if (heap_addr > dev_bo->mm_node.start) 54 break; 55 } 56 57 heap_id--; 58 heap = xa_load(&client->dev_heap_xa, heap_id); 59 exp_heap_uva = amdxdna_gem_uva(heap); 60 heap_addr = amdxdna_gem_dev_addr(heap); 61 dev_bo->heap_start_id = heap_id; 62 dev_bo->mem.uva = dev_bo->mm_node.start - heap_addr + exp_heap_uva; 63 64 for (; heap_id < client->dev_heap_nid; heap_id++) { 65 heap = xa_load(&client->dev_heap_xa, heap_id); 66 if (!heap) { 67 XDNA_ERR(xdna, "Failed to load heap %d", heap_id); 68 return -EINVAL; 69 } 70 heap_addr = amdxdna_gem_uva(heap); 71 if (heap_addr == AMDXDNA_INVALID_ADDR) { 72 XDNA_ERR(xdna, "Heap %d is not mapped", heap_id); 73 return -EAGAIN; 74 } 75 76 if (heap_addr != exp_heap_uva) { 77 XDNA_ERR(xdna, "Heap %d uva is not contiguous", heap_id); 78 return -EINVAL; 79 } 80 81 if (heap->dev_addr + heap->mem.size >= 82 dev_bo->mm_node.start + dev_bo->mem.size) 83 break; 84 85 exp_heap_uva += heap->mem.size; 86 } 87 88 if (heap_id == client->dev_heap_nid) { 89 XDNA_DBG(xdna, "Can not find heap end"); 90 return -EAGAIN; 91 } 92 93 dev_bo->heap_end_id = heap_id; 94 95 return 0; 96 } 97 98 static int 99 amdxdna_gem_heap_alloc(struct amdxdna_gem_obj *abo) 100 { 101 struct amdxdna_client *client = abo->client; 102 struct amdxdna_dev *xdna = client->xdna; 103 struct amdxdna_mem *mem = &abo->mem; 104 struct amdxdna_gem_obj *heap; 105 unsigned long heap_id; 106 u32 align; 107 int ret; 108 109 mutex_lock(&client->mm_lock); 110 111 if (!mem->size || mem->size > xdna->dev_info->dev_heap_max_size) { 112 XDNA_ERR(xdna, "Invalid dev bo size 0x%lx, max heap 0x%lx", 113 mem->size, xdna->dev_info->dev_heap_max_size); 114 ret = -EINVAL; 115 goto unlock_out; 116 } 117 118 align = 1 << max(PAGE_SHIFT, xdna->dev_info->dev_mem_buf_shift); 119 ret = drm_mm_insert_node_generic(&client->dev_heap_mm, 120 &abo->mm_node, 121 mem->size, align, 122 0, DRM_MM_INSERT_BEST); 123 if (ret) { 124 XDNA_ERR(xdna, "Failed to alloc dev bo memory, ret %d", ret); 125 goto unlock_out; 126 } 127 128 ret = amdxdna_init_dev_bo(abo); 129 if (ret) { 130 drm_mm_remove_node(&abo->mm_node); 131 goto unlock_out; 132 } 133 134 client->heap_usage += mem->size; 135 xa_for_each_range(&client->dev_heap_xa, heap_id, heap, 136 abo->heap_start_id, abo->heap_end_id) 137 drm_gem_object_get(to_gobj(heap)); 138 139 unlock_out: 140 mutex_unlock(&client->mm_lock); 141 142 return ret; 143 } 144 145 static void 146 amdxdna_gem_heap_free(struct amdxdna_gem_obj *abo) 147 { 148 struct amdxdna_client *client = abo->client; 149 struct amdxdna_gem_obj *heap; 150 unsigned long heap_id; 151 152 mutex_lock(&client->mm_lock); 153 154 drm_mm_remove_node(&abo->mm_node); 155 client->heap_usage -= abo->mem.size; 156 157 xa_for_each_range(&client->dev_heap_xa, heap_id, heap, 158 abo->heap_start_id, abo->heap_end_id) 159 drm_gem_object_put(to_gobj(heap)); 160 161 mutex_unlock(&client->mm_lock); 162 } 163 164 static struct amdxdna_gem_obj * 165 amdxdna_gem_create_obj(struct drm_device *dev, size_t size) 166 { 167 struct amdxdna_gem_obj *abo; 168 169 abo = kzalloc_obj(*abo); 170 if (!abo) 171 return ERR_PTR(-ENOMEM); 172 173 abo->pinned = false; 174 abo->assigned_hwctx = AMDXDNA_INVALID_CTX_HANDLE; 175 mutex_init(&abo->lock); 176 177 abo->mem.dma_addr = AMDXDNA_INVALID_ADDR; 178 abo->mem.uva = AMDXDNA_INVALID_ADDR; 179 abo->mem.size = size; 180 abo->open_ref = 0; 181 abo->internal = false; 182 INIT_LIST_HEAD(&abo->mem.umap_list); 183 184 return abo; 185 } 186 187 static void 188 amdxdna_gem_destroy_obj(struct amdxdna_gem_obj *abo) 189 { 190 mutex_destroy(&abo->lock); 191 kfree(abo); 192 } 193 194 /* 195 * Obtains a kernel virtual address on the BO (usually of small size). 196 * The mapping is established on the first call and stays valid until 197 * amdxdna_gem_vunmap() is called. 198 */ 199 void *amdxdna_gem_vmap(struct amdxdna_gem_obj *abo) 200 { 201 struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev); 202 struct iosys_map map = IOSYS_MAP_INIT_VADDR(NULL); 203 int ret; 204 205 if (abo->mem.kva) 206 return abo->mem.kva; 207 208 /* The first call to get the kva, taking slow path. */ 209 guard(mutex)(&abo->lock); 210 211 if (!abo->mem.kva) { 212 ret = drm_gem_vmap(to_gobj(abo), &map); 213 if (ret) 214 XDNA_ERR(xdna, "Vmap bo failed, ret %d", ret); 215 else 216 abo->mem.kva = map.vaddr; 217 } 218 return abo->mem.kva; 219 } 220 221 /* 222 * Free mapping established through amdxdna_gem_vmap() 223 */ 224 static void amdxdna_gem_vunmap(struct amdxdna_gem_obj *abo) 225 { 226 guard(mutex)(&abo->lock); 227 228 if (abo->mem.kva) { 229 struct iosys_map map = IOSYS_MAP_INIT_VADDR(abo->mem.kva); 230 231 drm_gem_vunmap(to_gobj(abo), &map); 232 abo->mem.kva = NULL; 233 } 234 } 235 236 /* 237 * Obtain the address for device to access the BO. 238 */ 239 u64 amdxdna_gem_dev_addr(struct amdxdna_gem_obj *abo) 240 { 241 if (abo->type == AMDXDNA_BO_DEV_HEAP) 242 return abo->dev_addr; 243 if (abo->type == AMDXDNA_BO_DEV) 244 return abo->mm_node.start; 245 return amdxdna_obj_dma_addr(abo); 246 } 247 248 static bool amdxdna_hmm_invalidate(struct mmu_interval_notifier *mni, 249 const struct mmu_notifier_range *range, 250 unsigned long cur_seq) 251 { 252 struct amdxdna_umap *mapp = container_of(mni, struct amdxdna_umap, notifier); 253 struct amdxdna_gem_obj *abo = mapp->abo; 254 struct amdxdna_dev *xdna; 255 256 xdna = to_xdna_dev(to_gobj(abo)->dev); 257 XDNA_DBG(xdna, "Invalidating range 0x%lx, 0x%lx, type %d", 258 mapp->range.start, mapp->range.end, abo->type); 259 260 if (!mmu_notifier_range_blockable(range)) 261 return false; 262 263 down_write(&xdna->notifier_lock); 264 abo->mem.map_invalid = true; 265 mapp->invalid = true; 266 mmu_interval_set_seq(&mapp->notifier, cur_seq); 267 up_write(&xdna->notifier_lock); 268 269 if (xdna->dev_info->ops->hmm_invalidate) 270 xdna->dev_info->ops->hmm_invalidate(abo, cur_seq); 271 272 if (range->event == MMU_NOTIFY_UNMAP) { 273 down_write(&xdna->notifier_lock); 274 if (!mapp->unmapped) { 275 queue_work(xdna->notifier_wq, &mapp->hmm_unreg_work); 276 mapp->unmapped = true; 277 } 278 up_write(&xdna->notifier_lock); 279 } 280 281 return true; 282 } 283 284 static const struct mmu_interval_notifier_ops amdxdna_hmm_ops = { 285 .invalidate = amdxdna_hmm_invalidate, 286 }; 287 288 static inline bool compare_range(struct amdxdna_umap *mapp, 289 struct mm_struct *mm, 290 unsigned long start, unsigned long end) 291 { 292 return (!mapp->unmapped && mapp->notifier.mm == mm && 293 mapp->range.start == start && mapp->range.end == end); 294 } 295 296 static void amdxdna_hmm_unregister(struct amdxdna_gem_obj *abo, 297 struct vm_area_struct *vma) 298 { 299 struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev); 300 struct amdxdna_umap *mapp; 301 302 down_write(&xdna->notifier_lock); 303 list_for_each_entry(mapp, &abo->mem.umap_list, node) { 304 if (!vma || compare_range(mapp, vma->vm_mm, vma->vm_start, vma->vm_end)) { 305 if (!mapp->unmapped) { 306 queue_work(xdna->notifier_wq, &mapp->hmm_unreg_work); 307 mapp->unmapped = true; 308 } 309 if (vma) 310 break; 311 } 312 } 313 up_write(&xdna->notifier_lock); 314 } 315 316 static void amdxdna_umap_release(struct kref *ref) 317 { 318 struct amdxdna_umap *mapp = container_of(ref, struct amdxdna_umap, refcnt); 319 struct amdxdna_gem_obj *abo = mapp->abo; 320 struct amdxdna_dev *xdna; 321 322 mmu_interval_notifier_remove(&mapp->notifier); 323 324 xdna = to_xdna_dev(to_gobj(mapp->abo)->dev); 325 down_write(&xdna->notifier_lock); 326 list_del(&mapp->node); 327 if (list_empty(&abo->mem.umap_list)) 328 abo->mem.uva = AMDXDNA_INVALID_ADDR; 329 up_write(&xdna->notifier_lock); 330 331 kvfree(mapp->range.hmm_pfns); 332 kfree(mapp); 333 } 334 335 void amdxdna_umap_put(struct amdxdna_umap *mapp) 336 { 337 kref_put(&mapp->refcnt, amdxdna_umap_release); 338 } 339 340 static void amdxdna_hmm_unreg_work(struct work_struct *work) 341 { 342 struct amdxdna_umap *mapp = container_of(work, struct amdxdna_umap, 343 hmm_unreg_work); 344 345 amdxdna_umap_put(mapp); 346 } 347 348 static int amdxdna_hmm_register(struct amdxdna_gem_obj *abo, 349 struct vm_area_struct *vma) 350 { 351 struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev); 352 unsigned long len = vma->vm_end - vma->vm_start; 353 unsigned long addr = vma->vm_start; 354 struct amdxdna_umap *mapp; 355 unsigned long nr_pages; 356 int ret; 357 358 /* 359 * When PASID is off, amdxdna_gem_obj_open() called amdxdna_dma_map_bo() 360 * and mem.dma_addr is valid; use the DMA address directly and skip HMM. 361 * Avoid dereferencing abo->client which may be NULL (cleared in close()) 362 * while internal kernel references are still held. 363 */ 364 if (abo->mem.dma_addr != AMDXDNA_INVALID_ADDR) { 365 /* Need to set uva for heap uva validation */ 366 abo->mem.uva = addr; 367 return 0; 368 } 369 370 down_read(&xdna->notifier_lock); 371 list_for_each_entry(mapp, &abo->mem.umap_list, node) { 372 if (compare_range(mapp, current->mm, addr, addr + len)) { 373 up_read(&xdna->notifier_lock); 374 return 0; 375 } 376 } 377 up_read(&xdna->notifier_lock); 378 379 mapp = kzalloc_obj(*mapp); 380 if (!mapp) 381 return -ENOMEM; 382 383 nr_pages = (PAGE_ALIGN(addr + len) - (addr & PAGE_MASK)) >> PAGE_SHIFT; 384 mapp->range.hmm_pfns = kvzalloc_objs(*mapp->range.hmm_pfns, nr_pages); 385 if (!mapp->range.hmm_pfns) { 386 ret = -ENOMEM; 387 goto free_map; 388 } 389 390 ret = mmu_interval_notifier_insert_locked(&mapp->notifier, 391 current->mm, 392 addr, 393 len, 394 &amdxdna_hmm_ops); 395 if (ret) { 396 XDNA_ERR(xdna, "Insert mmu notifier failed, ret %d", ret); 397 goto free_pfns; 398 } 399 400 mapp->range.notifier = &mapp->notifier; 401 mapp->range.start = vma->vm_start; 402 mapp->range.end = vma->vm_end; 403 mapp->range.default_flags = HMM_PFN_REQ_FAULT; 404 mapp->abo = abo; 405 kref_init(&mapp->refcnt); 406 407 INIT_WORK(&mapp->hmm_unreg_work, amdxdna_hmm_unreg_work); 408 409 down_write(&xdna->notifier_lock); 410 if (list_empty(&abo->mem.umap_list)) 411 abo->mem.uva = addr; 412 list_add_tail(&mapp->node, &abo->mem.umap_list); 413 up_write(&xdna->notifier_lock); 414 415 return 0; 416 417 free_pfns: 418 kvfree(mapp->range.hmm_pfns); 419 free_map: 420 kfree(mapp); 421 return ret; 422 } 423 424 static void amdxdna_gem_dev_obj_free(struct drm_gem_object *gobj) 425 { 426 struct amdxdna_dev *xdna = to_xdna_dev(gobj->dev); 427 struct amdxdna_gem_obj *abo = to_xdna_obj(gobj); 428 429 XDNA_DBG(xdna, "BO type %d xdna_addr 0x%llx", abo->type, amdxdna_gem_dev_addr(abo)); 430 if (abo->pinned) 431 amdxdna_gem_unpin(abo); 432 433 amdxdna_gem_vunmap(abo); 434 amdxdna_gem_heap_free(abo); 435 drm_gem_object_release(gobj); 436 amdxdna_gem_destroy_obj(abo); 437 } 438 439 static void amdxdna_mark_mapp_invalid(struct amdxdna_gem_obj *abo, 440 struct vm_area_struct *vma) 441 { 442 struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev); 443 struct amdxdna_umap *mapp; 444 445 down_write(&xdna->notifier_lock); 446 abo->mem.map_invalid = true; 447 list_for_each_entry(mapp, &abo->mem.umap_list, node) { 448 if (compare_range(mapp, vma->vm_mm, vma->vm_start, vma->vm_end)) { 449 mapp->invalid = true; 450 break; 451 } 452 } 453 up_write(&xdna->notifier_lock); 454 } 455 456 static int amdxdna_insert_pages(struct amdxdna_gem_obj *abo, 457 struct vm_area_struct *vma) 458 { 459 struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev); 460 unsigned long num_pages = vma_pages(vma); 461 unsigned long offset = 0; 462 int ret; 463 464 if (!is_import_bo(abo)) { 465 ret = drm_gem_shmem_mmap(&abo->base, vma); 466 if (ret) { 467 XDNA_ERR(xdna, "Failed shmem mmap %d", ret); 468 return ret; 469 } 470 } else { 471 vma->vm_private_data = NULL; 472 vma->vm_ops = NULL; 473 ret = dma_buf_mmap(abo->dma_buf, vma, 0); 474 if (ret) { 475 XDNA_ERR(xdna, "Failed to mmap dma buf %d", ret); 476 return ret; 477 } 478 479 /* Drop the reference drm_gem_mmap_obj() acquired.*/ 480 drm_gem_object_put(to_gobj(abo)); 481 } 482 483 do { 484 vm_fault_t fault_ret; 485 486 fault_ret = handle_mm_fault(vma, vma->vm_start + offset, 487 FAULT_FLAG_WRITE, NULL); 488 if (fault_ret & VM_FAULT_ERROR) { 489 XDNA_ERR(xdna, "Fault in page failed"); 490 amdxdna_mark_mapp_invalid(abo, vma); 491 break; 492 } 493 494 offset += PAGE_SIZE; 495 } while (--num_pages); 496 497 return 0; 498 } 499 500 static int amdxdna_gem_obj_mmap(struct drm_gem_object *gobj, 501 struct vm_area_struct *vma) 502 { 503 struct amdxdna_dev *xdna = to_xdna_dev(gobj->dev); 504 struct amdxdna_gem_obj *abo = to_xdna_obj(gobj); 505 int ret; 506 507 ret = amdxdna_hmm_register(abo, vma); 508 if (ret) 509 return ret; 510 511 ret = amdxdna_insert_pages(abo, vma); 512 if (ret) { 513 XDNA_ERR(xdna, "Failed insert pages, ret %d", ret); 514 goto hmm_unreg; 515 } 516 517 XDNA_DBG(xdna, "BO map_offset 0x%llx type %d userptr 0x%lx size 0x%lx", 518 drm_vma_node_offset_addr(&gobj->vma_node), abo->type, 519 vma->vm_start, gobj->size); 520 return 0; 521 522 hmm_unreg: 523 amdxdna_hmm_unregister(abo, vma); 524 return ret; 525 } 526 527 static int amdxdna_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma) 528 { 529 struct drm_gem_object *gobj = dma_buf->priv; 530 struct amdxdna_gem_obj *abo = to_xdna_obj(gobj); 531 unsigned long num_pages = vma_pages(vma); 532 int ret; 533 534 vma->vm_ops = &drm_gem_shmem_vm_ops; 535 vma->vm_private_data = gobj; 536 537 drm_gem_object_get(gobj); 538 ret = drm_gem_shmem_mmap(&abo->base, vma); 539 if (ret) 540 goto put_obj; 541 542 /* The buffer is based on memory pages. Fix the flag. */ 543 vm_flags_mod(vma, VM_MIXEDMAP, VM_PFNMAP); 544 ret = vm_insert_pages(vma, vma->vm_start, abo->base.pages, 545 &num_pages); 546 if (ret) 547 goto close_vma; 548 549 return 0; 550 551 close_vma: 552 vma->vm_ops->close(vma); 553 return ret; 554 put_obj: 555 drm_gem_object_put(gobj); 556 return ret; 557 } 558 559 static const struct dma_buf_ops amdxdna_dmabuf_ops = { 560 .attach = drm_gem_map_attach, 561 .detach = drm_gem_map_detach, 562 .map_dma_buf = drm_gem_map_dma_buf, 563 .unmap_dma_buf = drm_gem_unmap_dma_buf, 564 .release = drm_gem_dmabuf_release, 565 .mmap = amdxdna_gem_dmabuf_mmap, 566 .vmap = drm_gem_dmabuf_vmap, 567 .vunmap = drm_gem_dmabuf_vunmap, 568 }; 569 570 static struct dma_buf *amdxdna_gem_prime_export(struct drm_gem_object *gobj, int flags) 571 { 572 struct amdxdna_gem_obj *abo = to_xdna_obj(gobj); 573 DEFINE_DMA_BUF_EXPORT_INFO(exp_info); 574 575 if (abo->private_buffer) 576 return ERR_PTR(-EOPNOTSUPP); 577 578 if (abo->dma_buf) { 579 get_dma_buf(abo->dma_buf); 580 return abo->dma_buf; 581 } 582 583 exp_info.ops = &amdxdna_dmabuf_ops; 584 exp_info.size = gobj->size; 585 exp_info.flags = flags; 586 exp_info.priv = gobj; 587 exp_info.resv = gobj->resv; 588 589 return drm_gem_dmabuf_export(gobj->dev, &exp_info); 590 } 591 592 static void amdxdna_imported_obj_free(struct amdxdna_gem_obj *abo) 593 { 594 dma_buf_unmap_attachment_unlocked(abo->attach, abo->base.sgt, DMA_BIDIRECTIONAL); 595 dma_buf_detach(abo->dma_buf, abo->attach); 596 dma_buf_put(abo->dma_buf); 597 drm_gem_object_release(to_gobj(abo)); 598 kfree(abo); 599 } 600 601 static inline bool 602 amdxdna_gem_skip_bo_usage(struct amdxdna_gem_obj *abo) 603 { 604 /* Already counted as part of HEAP BO */ 605 if (abo->type == AMDXDNA_BO_DEV) 606 return true; 607 608 return false; 609 } 610 611 static void 612 amdxdna_gem_add_bo_usage(struct amdxdna_gem_obj *abo) 613 { 614 struct amdxdna_client *client = abo->client; 615 616 if (amdxdna_gem_skip_bo_usage(abo)) 617 return; 618 619 guard(mutex)(&client->mm_lock); 620 621 client->total_bo_usage += abo->mem.size; 622 if (abo->internal) 623 client->total_int_bo_usage += abo->mem.size; 624 } 625 626 static void 627 amdxdna_gem_del_bo_usage(struct amdxdna_gem_obj *abo) 628 { 629 struct amdxdna_client *client = abo->client; 630 631 if (amdxdna_gem_skip_bo_usage(abo)) 632 return; 633 634 guard(mutex)(&client->mm_lock); 635 636 client->total_bo_usage -= abo->mem.size; 637 if (abo->internal) 638 client->total_int_bo_usage -= abo->mem.size; 639 } 640 641 static void amdxdna_gem_obj_free(struct drm_gem_object *gobj) 642 { 643 struct amdxdna_dev *xdna = to_xdna_dev(gobj->dev); 644 struct amdxdna_gem_obj *abo = to_xdna_obj(gobj); 645 646 amdxdna_hmm_unregister(abo, NULL); 647 flush_workqueue(xdna->notifier_wq); 648 649 if (abo->pinned) 650 amdxdna_gem_unpin(abo); 651 652 amdxdna_dma_unmap_bo(xdna, abo); 653 amdxdna_gem_vunmap(abo); 654 mutex_destroy(&abo->lock); 655 656 if (is_import_bo(abo)) 657 amdxdna_imported_obj_free(abo); 658 else 659 drm_gem_shmem_free(&abo->base); 660 } 661 662 static int amdxdna_gem_obj_open(struct drm_gem_object *gobj, struct drm_file *filp) 663 { 664 struct amdxdna_dev *xdna = to_xdna_dev(gobj->dev); 665 struct amdxdna_gem_obj *abo = to_xdna_obj(gobj); 666 int ret; 667 668 guard(mutex)(&abo->lock); 669 abo->open_ref++; 670 if (abo->open_ref > 1) 671 return 0; 672 673 /* Attached to the client when first opened by it. */ 674 abo->client = filp->driver_priv; 675 676 /* No need to set up dma addr mapping in PASID mode. */ 677 if (!amdxdna_pasid_on(abo->client)) { 678 ret = amdxdna_dma_map_bo(xdna, abo); 679 if (ret) { 680 abo->open_ref--; 681 abo->client = NULL; 682 return ret; 683 } 684 } 685 686 amdxdna_gem_add_bo_usage(abo); 687 return 0; 688 } 689 690 static void amdxdna_gem_obj_close(struct drm_gem_object *gobj, struct drm_file *filp) 691 { 692 struct amdxdna_gem_obj *abo = to_xdna_obj(gobj); 693 694 guard(mutex)(&abo->lock); 695 abo->open_ref--; 696 697 if (abo->open_ref == 0) { 698 amdxdna_gem_del_bo_usage(abo); 699 /* Detach from the client when last closed by it. */ 700 abo->client = NULL; 701 } 702 } 703 704 static int amdxdna_gem_obj_vmap(struct drm_gem_object *obj, struct iosys_map *map) 705 { 706 struct amdxdna_gem_obj *abo = to_xdna_obj(obj); 707 int ret; 708 709 iosys_map_clear(map); 710 711 dma_resv_assert_held(obj->resv); 712 713 if (is_import_bo(abo)) 714 ret = dma_buf_vmap(abo->dma_buf, map); 715 else 716 ret = drm_gem_shmem_object_vmap(obj, map); 717 if (ret) 718 return ret; 719 if (!map->vaddr) 720 return -ENOMEM; 721 722 return 0; 723 } 724 725 static void amdxdna_gem_obj_vunmap(struct drm_gem_object *obj, struct iosys_map *map) 726 { 727 struct amdxdna_gem_obj *abo = to_xdna_obj(obj); 728 729 dma_resv_assert_held(obj->resv); 730 731 if (is_import_bo(abo)) 732 dma_buf_vunmap(abo->dma_buf, map); 733 else 734 drm_gem_shmem_object_vunmap(obj, map); 735 } 736 737 static int amdxdna_gem_dev_obj_vmap(struct drm_gem_object *obj, struct iosys_map *map) 738 { 739 struct amdxdna_gem_obj *abo = to_xdna_obj(obj); 740 struct amdxdna_gem_obj *heap; 741 void *base; 742 u64 offset; 743 744 /* vmap dev bo which is across more than 1 heap is not allowed */ 745 if (abo->heap_start_id != abo->heap_end_id) 746 return -ENOMEM; 747 748 heap = xa_load(&abo->client->dev_heap_xa, abo->heap_start_id); 749 if (!heap) 750 return -ENOMEM; 751 752 base = amdxdna_gem_vmap(heap); 753 if (!base) 754 return -ENOMEM; 755 756 offset = amdxdna_gem_dev_addr(abo) - amdxdna_gem_dev_addr(heap); 757 iosys_map_set_vaddr(map, base + offset); 758 return 0; 759 } 760 761 static struct dma_buf *amdxdna_gem_dev_obj_export(struct drm_gem_object *gobj, int flags) 762 { 763 return ERR_PTR(-EOPNOTSUPP); 764 } 765 766 static const struct drm_gem_object_funcs amdxdna_gem_dev_obj_funcs = { 767 .free = amdxdna_gem_dev_obj_free, 768 .vmap = amdxdna_gem_dev_obj_vmap, 769 .export = amdxdna_gem_dev_obj_export, 770 }; 771 772 static const struct drm_gem_object_funcs amdxdna_gem_shmem_funcs = { 773 .free = amdxdna_gem_obj_free, 774 .open = amdxdna_gem_obj_open, 775 .close = amdxdna_gem_obj_close, 776 .print_info = drm_gem_shmem_object_print_info, 777 .pin = drm_gem_shmem_object_pin, 778 .unpin = drm_gem_shmem_object_unpin, 779 .get_sg_table = drm_gem_shmem_object_get_sg_table, 780 .vmap = amdxdna_gem_obj_vmap, 781 .vunmap = amdxdna_gem_obj_vunmap, 782 .mmap = amdxdna_gem_obj_mmap, 783 .vm_ops = &drm_gem_shmem_vm_ops, 784 .export = amdxdna_gem_prime_export, 785 }; 786 787 /* For drm_driver->gem_create_object callback */ 788 struct drm_gem_object * 789 amdxdna_gem_create_shmem_object_cb(struct drm_device *dev, size_t size) 790 { 791 struct amdxdna_gem_obj *abo; 792 793 abo = amdxdna_gem_create_obj(dev, size); 794 if (IS_ERR(abo)) 795 return ERR_CAST(abo); 796 797 to_gobj(abo)->funcs = &amdxdna_gem_shmem_funcs; 798 799 return to_gobj(abo); 800 } 801 802 static struct amdxdna_gem_obj * 803 amdxdna_gem_create_shmem_object(struct drm_device *dev, struct amdxdna_drm_create_bo *args) 804 { 805 size_t size = args->size; 806 struct drm_gem_shmem_object *shmem = drm_gem_shmem_create(dev, size); 807 808 if (IS_ERR(shmem)) 809 return ERR_CAST(shmem); 810 811 shmem->map_wc = false; 812 return to_xdna_obj(&shmem->base); 813 } 814 815 static struct amdxdna_gem_obj * 816 amdxdna_gem_create_ubuf_object(struct drm_device *dev, struct amdxdna_drm_create_bo *args) 817 { 818 struct amdxdna_dev *xdna = to_xdna_dev(dev); 819 struct amdxdna_drm_va_tbl va_tbl; 820 struct amdxdna_gem_obj *abo; 821 struct drm_gem_object *gobj; 822 struct dma_buf *dma_buf; 823 824 if (copy_from_user(&va_tbl, u64_to_user_ptr(args->vaddr), sizeof(va_tbl))) { 825 XDNA_DBG(xdna, "Access va table failed"); 826 return ERR_PTR(-EINVAL); 827 } 828 829 if (va_tbl.num_entries) { 830 dma_buf = amdxdna_get_ubuf(dev, va_tbl.num_entries, 831 u64_to_user_ptr(args->vaddr + sizeof(va_tbl))); 832 } else { 833 dma_buf = dma_buf_get(va_tbl.dmabuf_fd); 834 } 835 836 if (IS_ERR(dma_buf)) 837 return ERR_CAST(dma_buf); 838 839 gobj = amdxdna_gem_prime_import(dev, dma_buf); 840 if (IS_ERR(gobj)) { 841 dma_buf_put(dma_buf); 842 return ERR_CAST(gobj); 843 } 844 845 dma_buf_put(dma_buf); 846 847 abo = to_xdna_obj(gobj); 848 abo->private_buffer = true; 849 850 return abo; 851 } 852 853 static struct amdxdna_gem_obj * 854 amdxdna_gem_create_cbuf_object(struct drm_device *dev, struct amdxdna_drm_create_bo *args) 855 { 856 struct amdxdna_dev *xdna = to_xdna_dev(dev); 857 size_t size = PAGE_ALIGN(args->size); 858 struct drm_gem_object *gobj; 859 struct amdxdna_gem_obj *ret; 860 struct dma_buf *dma_buf; 861 u64 align; 862 863 if (!size) { 864 XDNA_ERR(xdna, "Invalid BO size 0x%llx", args->size); 865 return ERR_PTR(-EINVAL); 866 } 867 868 align = (args->type == AMDXDNA_BO_DEV_HEAP) ? xdna->dev_info->dev_mem_size : 0; 869 dma_buf = amdxdna_get_cbuf(dev, size, align); 870 if (IS_ERR(dma_buf)) 871 return ERR_CAST(dma_buf); 872 873 gobj = amdxdna_gem_prime_import(dev, dma_buf); 874 if (IS_ERR(gobj)) 875 ret = ERR_CAST(gobj); 876 else 877 ret = to_xdna_obj(gobj); 878 879 dma_buf_put(dma_buf); 880 return ret; 881 } 882 883 struct drm_gem_object * 884 amdxdna_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf) 885 { 886 struct dma_buf_attachment *attach; 887 struct amdxdna_gem_obj *abo; 888 struct drm_gem_object *gobj; 889 struct sg_table *sgt; 890 int ret; 891 892 get_dma_buf(dma_buf); 893 894 attach = dma_buf_attach(dma_buf, dev->dev); 895 if (IS_ERR(attach)) { 896 ret = PTR_ERR(attach); 897 goto put_buf; 898 } 899 900 sgt = dma_buf_map_attachment_unlocked(attach, DMA_BIDIRECTIONAL); 901 if (IS_ERR(sgt)) { 902 ret = PTR_ERR(sgt); 903 goto fail_detach; 904 } 905 906 gobj = drm_gem_shmem_prime_import_sg_table(dev, attach, sgt); 907 if (IS_ERR(gobj)) { 908 ret = PTR_ERR(gobj); 909 goto fail_unmap; 910 } 911 912 abo = to_xdna_obj(gobj); 913 abo->attach = attach; 914 abo->dma_buf = dma_buf; 915 abo->type = AMDXDNA_BO_SHARE; 916 gobj->resv = dma_buf->resv; 917 918 return gobj; 919 920 fail_unmap: 921 dma_buf_unmap_attachment_unlocked(attach, sgt, DMA_BIDIRECTIONAL); 922 fail_detach: 923 dma_buf_detach(dma_buf, attach); 924 put_buf: 925 dma_buf_put(dma_buf); 926 927 return ERR_PTR(ret); 928 } 929 930 static struct amdxdna_gem_obj * 931 amdxdna_drm_create_share_bo(struct drm_device *dev, 932 struct amdxdna_drm_create_bo *args, struct drm_file *filp) 933 { 934 struct amdxdna_gem_obj *abo; 935 936 if (args->vaddr) 937 abo = amdxdna_gem_create_ubuf_object(dev, args); 938 else if (amdxdna_use_carveout(to_xdna_dev(dev))) 939 abo = amdxdna_gem_create_cbuf_object(dev, args); 940 else 941 abo = amdxdna_gem_create_shmem_object(dev, args); 942 if (IS_ERR(abo)) 943 return ERR_CAST(abo); 944 945 if (args->type == AMDXDNA_BO_DEV_HEAP) { 946 abo->type = AMDXDNA_BO_DEV_HEAP; 947 abo->internal = true; 948 } else { 949 abo->type = AMDXDNA_BO_SHARE; 950 abo->internal = args->type == AMDXDNA_BO_CMD; 951 } 952 953 return abo; 954 } 955 956 static struct amdxdna_gem_obj * 957 amdxdna_drm_create_dev_heap_bo(struct drm_device *dev, 958 struct amdxdna_drm_create_bo *args, struct drm_file *filp) 959 { 960 struct amdxdna_client *client = filp->driver_priv; 961 struct amdxdna_dev *xdna = to_xdna_dev(dev); 962 struct amdxdna_gem_obj *abo; 963 int ret; 964 965 WARN_ON(!is_power_of_2(xdna->dev_info->dev_mem_size)); 966 XDNA_DBG(xdna, "Requested dev heap size 0x%llx", args->size); 967 if (!args->size || !IS_ALIGNED(args->size, xdna->dev_info->dev_mem_size)) { 968 XDNA_ERR(xdna, "The dev heap size 0x%llx is not multiple of 0x%lx", 969 args->size, xdna->dev_info->dev_mem_size); 970 return ERR_PTR(-EINVAL); 971 } 972 973 /* HEAP BO is a special case of SHARE BO. */ 974 abo = amdxdna_drm_create_share_bo(dev, args, filp); 975 if (IS_ERR(abo)) 976 return ERR_CAST(abo); 977 978 /* Set up heap for this client. */ 979 mutex_lock(&client->mm_lock); 980 981 if (!xdna->dev_info->ops->hwctx_heap_expand && 982 client->dev_heap_nid > 0) { 983 XDNA_ERR(xdna, "Heap expansion is not supported"); 984 ret = -EOPNOTSUPP; 985 goto mm_unlock; 986 } 987 988 if (client->total_heap_size + abo->mem.size > 989 xdna->dev_info->dev_heap_max_size) { 990 XDNA_ERR(xdna, "Heap size 0x%lx + 0x%lx exceeds max 0x%lx", 991 client->total_heap_size, abo->mem.size, 992 xdna->dev_info->dev_heap_max_size); 993 ret = -ENOSPC; 994 goto mm_unlock; 995 } 996 997 ret = xa_insert(&client->dev_heap_xa, client->dev_heap_nid, abo, GFP_KERNEL); 998 if (ret) { 999 XDNA_ERR(xdna, "Add heap failed %d", ret); 1000 goto mm_unlock; 1001 } 1002 1003 abo->dev_addr = xdna->dev_info->dev_mem_base + client->total_heap_size; 1004 client->total_heap_size += abo->mem.size; 1005 client->dev_heap_nid++; 1006 drm_gem_object_get(to_gobj(abo)); 1007 1008 mutex_unlock(&client->mm_lock); 1009 1010 return abo; 1011 1012 mm_unlock: 1013 mutex_unlock(&client->mm_lock); 1014 drm_gem_object_put(to_gobj(abo)); 1015 return ERR_PTR(ret); 1016 } 1017 1018 struct amdxdna_gem_obj * 1019 amdxdna_drm_create_dev_bo(struct drm_device *dev, 1020 struct amdxdna_drm_create_bo *args, struct drm_file *filp) 1021 { 1022 size_t aligned_sz = PAGE_ALIGN(args->size); 1023 struct amdxdna_client *client = filp->driver_priv; 1024 struct amdxdna_dev *xdna = to_xdna_dev(dev); 1025 struct amdxdna_gem_obj *abo; 1026 struct drm_gem_object *gobj; 1027 int ret; 1028 1029 if (!aligned_sz) { 1030 XDNA_ERR(xdna, "Invalid BO size 0x%llx", args->size); 1031 return ERR_PTR(-EINVAL); 1032 } 1033 1034 abo = amdxdna_gem_create_obj(dev, aligned_sz); 1035 if (IS_ERR(abo)) 1036 return abo; 1037 gobj = to_gobj(abo); 1038 gobj->funcs = &amdxdna_gem_dev_obj_funcs; 1039 abo->type = AMDXDNA_BO_DEV; 1040 abo->internal = true; 1041 /* 1042 * DEV BOs cannot be alive when client is gone, it's OK to 1043 * always establish the connection. 1044 */ 1045 abo->client = client; 1046 1047 ret = amdxdna_gem_heap_alloc(abo); 1048 if (ret) { 1049 amdxdna_gem_destroy_obj(abo); 1050 return ERR_PTR(ret); 1051 } 1052 1053 drm_gem_private_object_init(dev, gobj, aligned_sz); 1054 1055 return abo; 1056 } 1057 1058 int amdxdna_drm_create_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) 1059 { 1060 struct amdxdna_client *client = filp->driver_priv; 1061 struct amdxdna_dev *xdna = to_xdna_dev(dev); 1062 struct amdxdna_drm_create_bo *args = data; 1063 struct amdxdna_gem_obj *abo; 1064 int ret; 1065 1066 if (args->flags) 1067 return -EINVAL; 1068 1069 XDNA_DBG(xdna, "BO arg type %d vaddr 0x%llx size 0x%llx flags 0x%llx", 1070 args->type, args->vaddr, args->size, args->flags); 1071 switch (args->type) { 1072 case AMDXDNA_BO_CMD: 1073 case AMDXDNA_BO_SHARE: 1074 abo = amdxdna_drm_create_share_bo(dev, args, filp); 1075 break; 1076 case AMDXDNA_BO_DEV_HEAP: 1077 abo = amdxdna_drm_create_dev_heap_bo(dev, args, filp); 1078 break; 1079 case AMDXDNA_BO_DEV: 1080 abo = amdxdna_drm_create_dev_bo(dev, args, filp); 1081 if (!IS_ERR(abo)) { 1082 mutex_lock(&xdna->dev_lock); 1083 ret = amdxdna_update_heap(client, NULL); 1084 mutex_unlock(&xdna->dev_lock); 1085 if (ret) 1086 goto put_obj; 1087 } 1088 break; 1089 default: 1090 return -EINVAL; 1091 } 1092 if (IS_ERR(abo)) 1093 return PTR_ERR(abo); 1094 1095 /* Ready to publish object to userspace and count for BO usage. */ 1096 ret = drm_gem_handle_create(filp, to_gobj(abo), &args->handle); 1097 if (ret) { 1098 XDNA_ERR(xdna, "Create handle failed"); 1099 goto put_obj; 1100 } 1101 1102 XDNA_DBG(xdna, "BO hdl %d type %d userptr 0x%llx xdna_addr 0x%llx size 0x%lx", 1103 args->handle, args->type, amdxdna_gem_uva(abo), 1104 amdxdna_gem_dev_addr(abo), abo->mem.size); 1105 put_obj: 1106 /* Dereference object reference. Handle holds it now. */ 1107 drm_gem_object_put(to_gobj(abo)); 1108 return ret; 1109 } 1110 1111 static int amdxdna_bo_pin(struct amdxdna_gem_obj *abo) 1112 { 1113 struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev); 1114 int ret; 1115 1116 if (is_import_bo(abo)) 1117 return 0; 1118 1119 ret = drm_gem_shmem_pin(&abo->base); 1120 1121 XDNA_DBG(xdna, "BO type %d ret %d", abo->type, ret); 1122 return ret; 1123 } 1124 1125 static void amdxdna_bo_unpin(struct amdxdna_gem_obj *abo) 1126 { 1127 struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev); 1128 1129 if (is_import_bo(abo)) 1130 return; 1131 1132 drm_gem_shmem_unpin(&abo->base); 1133 1134 XDNA_DBG(xdna, "BO type %d", abo->type); 1135 } 1136 1137 int amdxdna_gem_pin_nolock(struct amdxdna_gem_obj *abo) 1138 { 1139 struct amdxdna_client *client = abo->client; 1140 struct amdxdna_gem_obj *heap; 1141 unsigned long heap_id, last = ULONG_MAX; 1142 int ret = 0; 1143 1144 if (abo->type != AMDXDNA_BO_DEV) 1145 return amdxdna_bo_pin(abo); 1146 1147 xa_for_each_range(&client->dev_heap_xa, heap_id, heap, 1148 abo->heap_start_id, abo->heap_end_id) { 1149 ret = amdxdna_bo_pin(heap); 1150 if (ret) 1151 break; 1152 last = heap_id; 1153 } 1154 1155 if (ret && last <= abo->heap_end_id) { 1156 xa_for_each_range(&client->dev_heap_xa, heap_id, heap, 1157 abo->heap_start_id, last) 1158 amdxdna_bo_unpin(heap); 1159 } 1160 1161 return ret; 1162 } 1163 1164 int amdxdna_gem_pin(struct amdxdna_gem_obj *abo) 1165 { 1166 int ret; 1167 1168 mutex_lock(&abo->lock); 1169 ret = amdxdna_gem_pin_nolock(abo); 1170 mutex_unlock(&abo->lock); 1171 1172 return ret; 1173 } 1174 1175 void amdxdna_gem_unpin(struct amdxdna_gem_obj *abo) 1176 { 1177 mutex_lock(&abo->lock); 1178 if (abo->type == AMDXDNA_BO_DEV) { 1179 struct amdxdna_gem_obj *heap; 1180 unsigned long heap_id; 1181 1182 xa_for_each_range(&abo->client->dev_heap_xa, heap_id, heap, 1183 abo->heap_start_id, abo->heap_end_id) 1184 amdxdna_bo_unpin(heap); 1185 } else { 1186 amdxdna_bo_unpin(abo); 1187 } 1188 1189 mutex_unlock(&abo->lock); 1190 } 1191 1192 struct amdxdna_gem_obj *amdxdna_gem_get_obj(struct amdxdna_client *client, 1193 u32 bo_hdl, u8 bo_type) 1194 { 1195 struct amdxdna_gem_obj *abo; 1196 struct drm_gem_object *gobj; 1197 1198 gobj = drm_gem_object_lookup(client->filp, bo_hdl); 1199 if (!gobj) { 1200 XDNA_DBG(client->xdna, "Can not find bo %d", bo_hdl); 1201 return NULL; 1202 } 1203 1204 abo = to_xdna_obj(gobj); 1205 if (bo_type == AMDXDNA_BO_INVALID || abo->type == bo_type) 1206 return abo; 1207 1208 drm_gem_object_put(gobj); 1209 return NULL; 1210 } 1211 1212 int amdxdna_drm_get_bo_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) 1213 { 1214 struct amdxdna_drm_get_bo_info *args = data; 1215 struct amdxdna_dev *xdna = to_xdna_dev(dev); 1216 struct amdxdna_gem_obj *abo; 1217 struct drm_gem_object *gobj; 1218 int ret = 0; 1219 1220 if (args->ext || args->ext_flags || args->pad) 1221 return -EINVAL; 1222 1223 gobj = drm_gem_object_lookup(filp, args->handle); 1224 if (!gobj) { 1225 XDNA_DBG(xdna, "Lookup GEM object %d failed", args->handle); 1226 return -ENOENT; 1227 } 1228 1229 abo = to_xdna_obj(gobj); 1230 args->vaddr = amdxdna_gem_uva(abo); 1231 args->xdna_addr = amdxdna_gem_dev_addr(abo); 1232 1233 if (abo->type != AMDXDNA_BO_DEV) 1234 args->map_offset = drm_vma_node_offset_addr(&gobj->vma_node); 1235 else 1236 args->map_offset = AMDXDNA_INVALID_ADDR; 1237 1238 XDNA_DBG(xdna, "BO hdl %d map_offset 0x%llx vaddr 0x%llx xdna_addr 0x%llx", 1239 args->handle, args->map_offset, args->vaddr, args->xdna_addr); 1240 1241 drm_gem_object_put(gobj); 1242 return ret; 1243 } 1244 1245 static int amdxdna_flush_bo(struct amdxdna_gem_obj *abo, u64 offset, u64 size) 1246 { 1247 u64 end; 1248 1249 if (offset >= abo->mem.size) 1250 return -EINVAL; 1251 1252 if (check_add_overflow(offset, size, &end)) 1253 return -EINVAL; 1254 1255 size = min(abo->mem.size, end) - offset; 1256 if (is_import_bo(abo)) 1257 drm_clflush_sg(abo->base.sgt); 1258 else if (amdxdna_gem_vmap(abo)) 1259 drm_clflush_virt_range(amdxdna_gem_vmap(abo) + offset, size); 1260 else if (abo->base.pages) 1261 drm_clflush_pages(abo->base.pages, abo->mem.size >> PAGE_SHIFT); 1262 else 1263 return -EINVAL; 1264 1265 return 0; 1266 } 1267 1268 /* 1269 * The sync bo ioctl is to make sure the CPU cache is in sync with memory. 1270 * This is required because NPU is not cache coherent device. CPU cache 1271 * flushing/invalidation is expensive so it is best to handle this outside 1272 * of the command submission path. This ioctl allows explicit cache 1273 * flushing/invalidation outside of the critical path. 1274 */ 1275 int amdxdna_drm_sync_bo_ioctl(struct drm_device *dev, 1276 void *data, struct drm_file *filp) 1277 { 1278 struct amdxdna_client *client = filp->driver_priv; 1279 struct amdxdna_dev *xdna = to_xdna_dev(dev); 1280 struct amdxdna_drm_sync_bo *args = data; 1281 struct amdxdna_gem_obj *abo; 1282 struct drm_gem_object *gobj; 1283 int ret = 0; 1284 1285 gobj = drm_gem_object_lookup(filp, args->handle); 1286 if (!gobj) { 1287 XDNA_ERR(xdna, "Lookup GEM object failed"); 1288 return -ENOENT; 1289 } 1290 abo = to_xdna_obj(gobj); 1291 1292 if (abo->type == AMDXDNA_BO_DEV) { 1293 struct amdxdna_gem_obj *heap; 1294 unsigned long heap_id; 1295 u64 bo_start = amdxdna_gem_dev_addr(abo); 1296 u64 flush_start = bo_start + args->offset; 1297 u64 flush_end = flush_start + args->size; 1298 1299 xa_for_each_range(&client->dev_heap_xa, heap_id, heap, 1300 abo->heap_start_id, abo->heap_end_id) { 1301 u64 heap_start = amdxdna_gem_dev_addr(heap); 1302 u64 heap_end = heap_start + heap->mem.size; 1303 u64 start = max(flush_start, heap_start); 1304 u64 end = min(flush_end, heap_end); 1305 1306 if (start >= end) 1307 continue; 1308 1309 ret = amdxdna_flush_bo(heap, start - heap_start, end - start); 1310 if (ret) { 1311 XDNA_ERR(xdna, "Failed to flush heap %ld ret %d", 1312 heap_id, ret); 1313 goto put_obj; 1314 } 1315 } 1316 } else { 1317 ret = amdxdna_gem_pin(abo); 1318 if (ret) { 1319 XDNA_ERR(xdna, "Pin BO %d failed, ret %d", args->handle, ret); 1320 goto put_obj; 1321 } 1322 1323 ret = amdxdna_flush_bo(abo, args->offset, args->size); 1324 amdxdna_gem_unpin(abo); 1325 1326 if (ret) { 1327 drm_WARN(&xdna->ddev, 1, "Can not get flush memory"); 1328 goto put_obj; 1329 } 1330 } 1331 1332 XDNA_DBG(xdna, "Sync bo %d offset 0x%llx, size 0x%llx\n", 1333 args->handle, args->offset, args->size); 1334 1335 if (args->direction == SYNC_DIRECT_FROM_DEVICE) 1336 ret = amdxdna_hwctx_sync_debug_bo(client, args->handle); 1337 1338 put_obj: 1339 drm_gem_object_put(gobj); 1340 return ret; 1341 } 1342 1343 int amdxdna_drm_get_bo_usage(struct drm_device *dev, struct amdxdna_drm_get_array *args) 1344 { 1345 size_t min_sz = min(args->element_size, sizeof(struct amdxdna_drm_bo_usage)); 1346 char __user *buf = u64_to_user_ptr(args->buffer); 1347 struct amdxdna_dev *xdna = to_xdna_dev(dev); 1348 struct amdxdna_client *tmp_client; 1349 struct amdxdna_drm_bo_usage tmp; 1350 1351 drm_WARN_ON(dev, !mutex_is_locked(&xdna->dev_lock)); 1352 1353 if (args->num_element != 1) 1354 return -EINVAL; 1355 1356 if (copy_from_user(&tmp, buf, min_sz)) 1357 return -EFAULT; 1358 1359 if (!tmp.pid) 1360 return -EINVAL; 1361 1362 tmp.total_usage = 0; 1363 tmp.internal_usage = 0; 1364 tmp.heap_usage = 0; 1365 1366 list_for_each_entry(tmp_client, &xdna->client_list, node) { 1367 if (tmp.pid != tmp_client->pid) 1368 continue; 1369 1370 mutex_lock(&tmp_client->mm_lock); 1371 tmp.total_usage += tmp_client->total_bo_usage; 1372 tmp.internal_usage += tmp_client->total_int_bo_usage; 1373 tmp.heap_usage += tmp_client->heap_usage; 1374 mutex_unlock(&tmp_client->mm_lock); 1375 } 1376 1377 if (copy_to_user(buf, &tmp, min_sz)) 1378 return -EFAULT; 1379 1380 return 0; 1381 } 1382