xref: /linux/drivers/accel/amdxdna/amdxdna_gem.c (revision 2b109eccd80b660e58d4523e8b80ce11bc3e532e)
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 int amdxdna_insert_pages(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 	unsigned long num_pages = vma_pages(vma);
444 	unsigned long offset = 0;
445 	int ret;
446 
447 	if (!is_import_bo(abo)) {
448 		ret = drm_gem_shmem_mmap(&abo->base, vma);
449 		if (ret) {
450 			XDNA_ERR(xdna, "Failed shmem mmap %d", ret);
451 			return ret;
452 		}
453 
454 		/* The buffer is based on memory pages. Fix the flag. */
455 		vm_flags_mod(vma, VM_MIXEDMAP, VM_PFNMAP);
456 		ret = vm_insert_pages(vma, vma->vm_start, abo->base.pages,
457 				      &num_pages);
458 		if (ret) {
459 			XDNA_ERR(xdna, "Failed insert pages %d", ret);
460 			vma->vm_ops->close(vma);
461 			return ret;
462 		}
463 
464 		return 0;
465 	}
466 
467 	vma->vm_private_data = NULL;
468 	vma->vm_ops = NULL;
469 	ret = dma_buf_mmap(abo->dma_buf, vma, 0);
470 	if (ret) {
471 		XDNA_ERR(xdna, "Failed to mmap dma buf %d", ret);
472 		return ret;
473 	}
474 
475 	do {
476 		vm_fault_t fault_ret;
477 
478 		fault_ret = handle_mm_fault(vma, vma->vm_start + offset,
479 					    FAULT_FLAG_WRITE, NULL);
480 		if (fault_ret & VM_FAULT_ERROR) {
481 			vma->vm_ops->close(vma);
482 			XDNA_ERR(xdna, "Fault in page failed");
483 			return -EFAULT;
484 		}
485 
486 		offset += PAGE_SIZE;
487 	} while (--num_pages);
488 
489 	/* Drop the reference drm_gem_mmap_obj() acquired.*/
490 	drm_gem_object_put(to_gobj(abo));
491 
492 	return 0;
493 }
494 
495 static int amdxdna_gem_obj_mmap(struct drm_gem_object *gobj,
496 				struct vm_area_struct *vma)
497 {
498 	struct amdxdna_dev *xdna = to_xdna_dev(gobj->dev);
499 	struct amdxdna_gem_obj *abo = to_xdna_obj(gobj);
500 	int ret;
501 
502 	ret = amdxdna_hmm_register(abo, vma);
503 	if (ret)
504 		return ret;
505 
506 	ret = amdxdna_insert_pages(abo, vma);
507 	if (ret) {
508 		XDNA_ERR(xdna, "Failed insert pages, ret %d", ret);
509 		goto hmm_unreg;
510 	}
511 
512 	XDNA_DBG(xdna, "BO map_offset 0x%llx type %d userptr 0x%lx size 0x%lx",
513 		 drm_vma_node_offset_addr(&gobj->vma_node), abo->type,
514 		 vma->vm_start, gobj->size);
515 	return 0;
516 
517 hmm_unreg:
518 	amdxdna_hmm_unregister(abo, vma);
519 	return ret;
520 }
521 
522 static int amdxdna_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
523 {
524 	struct drm_gem_object *gobj = dma_buf->priv;
525 	struct amdxdna_gem_obj *abo = to_xdna_obj(gobj);
526 	unsigned long num_pages = vma_pages(vma);
527 	int ret;
528 
529 	vma->vm_ops = &drm_gem_shmem_vm_ops;
530 	vma->vm_private_data = gobj;
531 
532 	drm_gem_object_get(gobj);
533 	ret = drm_gem_shmem_mmap(&abo->base, vma);
534 	if (ret)
535 		goto put_obj;
536 
537 	/* The buffer is based on memory pages. Fix the flag. */
538 	vm_flags_mod(vma, VM_MIXEDMAP, VM_PFNMAP);
539 	ret = vm_insert_pages(vma, vma->vm_start, abo->base.pages,
540 			      &num_pages);
541 	if (ret)
542 		goto close_vma;
543 
544 	return 0;
545 
546 close_vma:
547 	vma->vm_ops->close(vma);
548 	return ret;
549 put_obj:
550 	drm_gem_object_put(gobj);
551 	return ret;
552 }
553 
554 static const struct dma_buf_ops amdxdna_dmabuf_ops = {
555 	.attach = drm_gem_map_attach,
556 	.detach = drm_gem_map_detach,
557 	.map_dma_buf = drm_gem_map_dma_buf,
558 	.unmap_dma_buf = drm_gem_unmap_dma_buf,
559 	.release = drm_gem_dmabuf_release,
560 	.mmap = amdxdna_gem_dmabuf_mmap,
561 	.vmap = drm_gem_dmabuf_vmap,
562 	.vunmap = drm_gem_dmabuf_vunmap,
563 };
564 
565 static struct dma_buf *amdxdna_gem_prime_export(struct drm_gem_object *gobj, int flags)
566 {
567 	struct amdxdna_gem_obj *abo = to_xdna_obj(gobj);
568 	DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
569 
570 	if (abo->private_buffer)
571 		return ERR_PTR(-EOPNOTSUPP);
572 
573 	if (abo->dma_buf) {
574 		get_dma_buf(abo->dma_buf);
575 		return abo->dma_buf;
576 	}
577 
578 	exp_info.ops = &amdxdna_dmabuf_ops;
579 	exp_info.size = gobj->size;
580 	exp_info.flags = flags;
581 	exp_info.priv = gobj;
582 	exp_info.resv = gobj->resv;
583 
584 	return drm_gem_dmabuf_export(gobj->dev, &exp_info);
585 }
586 
587 static void amdxdna_imported_obj_free(struct amdxdna_gem_obj *abo)
588 {
589 	dma_buf_unmap_attachment_unlocked(abo->attach, abo->base.sgt, DMA_BIDIRECTIONAL);
590 	dma_buf_detach(abo->dma_buf, abo->attach);
591 	dma_buf_put(abo->dma_buf);
592 	drm_gem_object_release(to_gobj(abo));
593 	kfree(abo);
594 }
595 
596 static inline bool
597 amdxdna_gem_skip_bo_usage(struct amdxdna_gem_obj *abo)
598 {
599 	/* Already counted as part of HEAP BO */
600 	if (abo->type == AMDXDNA_BO_DEV)
601 		return true;
602 
603 	return false;
604 }
605 
606 static void
607 amdxdna_gem_add_bo_usage(struct amdxdna_gem_obj *abo)
608 {
609 	struct amdxdna_client *client = abo->client;
610 
611 	if (amdxdna_gem_skip_bo_usage(abo))
612 		return;
613 
614 	guard(mutex)(&client->mm_lock);
615 
616 	client->total_bo_usage += abo->mem.size;
617 	if (abo->internal)
618 		client->total_int_bo_usage += abo->mem.size;
619 }
620 
621 static void
622 amdxdna_gem_del_bo_usage(struct amdxdna_gem_obj *abo)
623 {
624 	struct amdxdna_client *client = abo->client;
625 
626 	if (amdxdna_gem_skip_bo_usage(abo))
627 		return;
628 
629 	guard(mutex)(&client->mm_lock);
630 
631 	client->total_bo_usage -= abo->mem.size;
632 	if (abo->internal)
633 		client->total_int_bo_usage -= abo->mem.size;
634 }
635 
636 static void amdxdna_gem_obj_free(struct drm_gem_object *gobj)
637 {
638 	struct amdxdna_dev *xdna = to_xdna_dev(gobj->dev);
639 	struct amdxdna_gem_obj *abo = to_xdna_obj(gobj);
640 
641 	amdxdna_hmm_unregister(abo, NULL);
642 	flush_workqueue(xdna->notifier_wq);
643 
644 	if (abo->pinned)
645 		amdxdna_gem_unpin(abo);
646 
647 	amdxdna_dma_unmap_bo(xdna, abo);
648 	amdxdna_gem_vunmap(abo);
649 	mutex_destroy(&abo->lock);
650 
651 	if (is_import_bo(abo))
652 		amdxdna_imported_obj_free(abo);
653 	else
654 		drm_gem_shmem_free(&abo->base);
655 }
656 
657 static int amdxdna_gem_obj_open(struct drm_gem_object *gobj, struct drm_file *filp)
658 {
659 	struct amdxdna_dev *xdna = to_xdna_dev(gobj->dev);
660 	struct amdxdna_gem_obj *abo = to_xdna_obj(gobj);
661 	int ret;
662 
663 	guard(mutex)(&abo->lock);
664 	abo->open_ref++;
665 	if (abo->open_ref > 1)
666 		return 0;
667 
668 	/* Attached to the client when first opened by it. */
669 	abo->client = filp->driver_priv;
670 
671 	/* No need to set up dma addr mapping in PASID mode. */
672 	if (!amdxdna_pasid_on(abo->client)) {
673 		ret = amdxdna_dma_map_bo(xdna, abo);
674 		if (ret) {
675 			abo->open_ref--;
676 			abo->client = NULL;
677 			return ret;
678 		}
679 	}
680 
681 	amdxdna_gem_add_bo_usage(abo);
682 	return 0;
683 }
684 
685 static void amdxdna_gem_obj_close(struct drm_gem_object *gobj, struct drm_file *filp)
686 {
687 	struct amdxdna_gem_obj *abo = to_xdna_obj(gobj);
688 
689 	guard(mutex)(&abo->lock);
690 	abo->open_ref--;
691 
692 	if (abo->open_ref == 0) {
693 		amdxdna_gem_del_bo_usage(abo);
694 		/* Detach from the client when last closed by it. */
695 		abo->client = NULL;
696 	}
697 }
698 
699 static int amdxdna_gem_obj_vmap(struct drm_gem_object *obj, struct iosys_map *map)
700 {
701 	struct amdxdna_gem_obj *abo = to_xdna_obj(obj);
702 	int ret;
703 
704 	iosys_map_clear(map);
705 
706 	dma_resv_assert_held(obj->resv);
707 
708 	if (is_import_bo(abo))
709 		ret = dma_buf_vmap(abo->dma_buf, map);
710 	else
711 		ret = drm_gem_shmem_object_vmap(obj, map);
712 	if (ret)
713 		return ret;
714 	if (!map->vaddr)
715 		return -ENOMEM;
716 
717 	return 0;
718 }
719 
720 static void amdxdna_gem_obj_vunmap(struct drm_gem_object *obj, struct iosys_map *map)
721 {
722 	struct amdxdna_gem_obj *abo = to_xdna_obj(obj);
723 
724 	dma_resv_assert_held(obj->resv);
725 
726 	if (is_import_bo(abo))
727 		dma_buf_vunmap(abo->dma_buf, map);
728 	else
729 		drm_gem_shmem_object_vunmap(obj, map);
730 }
731 
732 static int amdxdna_gem_dev_obj_vmap(struct drm_gem_object *obj, struct iosys_map *map)
733 {
734 	struct amdxdna_gem_obj *abo = to_xdna_obj(obj);
735 	struct amdxdna_gem_obj *heap;
736 	void *base;
737 	u64 offset;
738 
739 	/* vmap dev bo which is across more than 1 heap is not allowed */
740 	if (abo->heap_start_id != abo->heap_end_id)
741 		return -ENOMEM;
742 
743 	heap = xa_load(&abo->client->dev_heap_xa, abo->heap_start_id);
744 	if (!heap)
745 		return -ENOMEM;
746 
747 	base = amdxdna_gem_vmap(heap);
748 	if (!base)
749 		return -ENOMEM;
750 
751 	offset = amdxdna_gem_dev_addr(abo) - amdxdna_gem_dev_addr(heap);
752 	iosys_map_set_vaddr(map, base + offset);
753 	return 0;
754 }
755 
756 static const struct drm_gem_object_funcs amdxdna_gem_dev_obj_funcs = {
757 	.free = amdxdna_gem_dev_obj_free,
758 	.vmap = amdxdna_gem_dev_obj_vmap,
759 };
760 
761 static const struct drm_gem_object_funcs amdxdna_gem_shmem_funcs = {
762 	.free = amdxdna_gem_obj_free,
763 	.open = amdxdna_gem_obj_open,
764 	.close = amdxdna_gem_obj_close,
765 	.print_info = drm_gem_shmem_object_print_info,
766 	.pin = drm_gem_shmem_object_pin,
767 	.unpin = drm_gem_shmem_object_unpin,
768 	.get_sg_table = drm_gem_shmem_object_get_sg_table,
769 	.vmap = amdxdna_gem_obj_vmap,
770 	.vunmap = amdxdna_gem_obj_vunmap,
771 	.mmap = amdxdna_gem_obj_mmap,
772 	.vm_ops = &drm_gem_shmem_vm_ops,
773 	.export = amdxdna_gem_prime_export,
774 };
775 
776 /* For drm_driver->gem_create_object callback */
777 struct drm_gem_object *
778 amdxdna_gem_create_shmem_object_cb(struct drm_device *dev, size_t size)
779 {
780 	struct amdxdna_gem_obj *abo;
781 
782 	abo = amdxdna_gem_create_obj(dev, size);
783 	if (IS_ERR(abo))
784 		return ERR_CAST(abo);
785 
786 	to_gobj(abo)->funcs = &amdxdna_gem_shmem_funcs;
787 
788 	return to_gobj(abo);
789 }
790 
791 static struct amdxdna_gem_obj *
792 amdxdna_gem_create_shmem_object(struct drm_device *dev, struct amdxdna_drm_create_bo *args)
793 {
794 	size_t size = args->size;
795 	struct drm_gem_shmem_object *shmem = drm_gem_shmem_create(dev, size);
796 
797 	if (IS_ERR(shmem))
798 		return ERR_CAST(shmem);
799 
800 	shmem->map_wc = false;
801 	return to_xdna_obj(&shmem->base);
802 }
803 
804 static struct amdxdna_gem_obj *
805 amdxdna_gem_create_ubuf_object(struct drm_device *dev, struct amdxdna_drm_create_bo *args)
806 {
807 	struct amdxdna_dev *xdna = to_xdna_dev(dev);
808 	struct amdxdna_drm_va_tbl va_tbl;
809 	struct amdxdna_gem_obj *abo;
810 	struct drm_gem_object *gobj;
811 	struct dma_buf *dma_buf;
812 
813 	if (copy_from_user(&va_tbl, u64_to_user_ptr(args->vaddr), sizeof(va_tbl))) {
814 		XDNA_DBG(xdna, "Access va table failed");
815 		return ERR_PTR(-EINVAL);
816 	}
817 
818 	if (va_tbl.num_entries) {
819 		dma_buf = amdxdna_get_ubuf(dev, va_tbl.num_entries,
820 					   u64_to_user_ptr(args->vaddr + sizeof(va_tbl)));
821 	} else {
822 		dma_buf = dma_buf_get(va_tbl.dmabuf_fd);
823 	}
824 
825 	if (IS_ERR(dma_buf))
826 		return ERR_CAST(dma_buf);
827 
828 	gobj = amdxdna_gem_prime_import(dev, dma_buf);
829 	if (IS_ERR(gobj)) {
830 		dma_buf_put(dma_buf);
831 		return ERR_CAST(gobj);
832 	}
833 
834 	dma_buf_put(dma_buf);
835 
836 	abo = to_xdna_obj(gobj);
837 	abo->private_buffer = true;
838 
839 	return abo;
840 }
841 
842 static struct amdxdna_gem_obj *
843 amdxdna_gem_create_cbuf_object(struct drm_device *dev, struct amdxdna_drm_create_bo *args)
844 {
845 	struct amdxdna_dev *xdna = to_xdna_dev(dev);
846 	size_t size = PAGE_ALIGN(args->size);
847 	struct drm_gem_object *gobj;
848 	struct amdxdna_gem_obj *ret;
849 	struct dma_buf *dma_buf;
850 	u64 align;
851 
852 	if (!size) {
853 		XDNA_ERR(xdna, "Invalid BO size 0x%llx", args->size);
854 		return ERR_PTR(-EINVAL);
855 	}
856 
857 	align = (args->type == AMDXDNA_BO_DEV_HEAP) ?  xdna->dev_info->dev_mem_size : 0;
858 	dma_buf = amdxdna_get_cbuf(dev, size, align);
859 	if (IS_ERR(dma_buf))
860 		return ERR_CAST(dma_buf);
861 
862 	gobj = amdxdna_gem_prime_import(dev, dma_buf);
863 	if (IS_ERR(gobj))
864 		ret = ERR_CAST(gobj);
865 	else
866 		ret = to_xdna_obj(gobj);
867 
868 	dma_buf_put(dma_buf);
869 	return ret;
870 }
871 
872 struct drm_gem_object *
873 amdxdna_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf)
874 {
875 	struct dma_buf_attachment *attach;
876 	struct amdxdna_gem_obj *abo;
877 	struct drm_gem_object *gobj;
878 	struct sg_table *sgt;
879 	int ret;
880 
881 	get_dma_buf(dma_buf);
882 
883 	attach = dma_buf_attach(dma_buf, dev->dev);
884 	if (IS_ERR(attach)) {
885 		ret = PTR_ERR(attach);
886 		goto put_buf;
887 	}
888 
889 	sgt = dma_buf_map_attachment_unlocked(attach, DMA_BIDIRECTIONAL);
890 	if (IS_ERR(sgt)) {
891 		ret = PTR_ERR(sgt);
892 		goto fail_detach;
893 	}
894 
895 	gobj = drm_gem_shmem_prime_import_sg_table(dev, attach, sgt);
896 	if (IS_ERR(gobj)) {
897 		ret = PTR_ERR(gobj);
898 		goto fail_unmap;
899 	}
900 
901 	abo = to_xdna_obj(gobj);
902 	abo->attach = attach;
903 	abo->dma_buf = dma_buf;
904 	abo->type = AMDXDNA_BO_SHARE;
905 	gobj->resv = dma_buf->resv;
906 
907 	return gobj;
908 
909 fail_unmap:
910 	dma_buf_unmap_attachment_unlocked(attach, sgt, DMA_BIDIRECTIONAL);
911 fail_detach:
912 	dma_buf_detach(dma_buf, attach);
913 put_buf:
914 	dma_buf_put(dma_buf);
915 
916 	return ERR_PTR(ret);
917 }
918 
919 static struct amdxdna_gem_obj *
920 amdxdna_drm_create_share_bo(struct drm_device *dev,
921 			    struct amdxdna_drm_create_bo *args, struct drm_file *filp)
922 {
923 	struct amdxdna_gem_obj *abo;
924 
925 	if (args->vaddr)
926 		abo = amdxdna_gem_create_ubuf_object(dev, args);
927 	else if (amdxdna_use_carveout(to_xdna_dev(dev)))
928 		abo = amdxdna_gem_create_cbuf_object(dev, args);
929 	else
930 		abo = amdxdna_gem_create_shmem_object(dev, args);
931 	if (IS_ERR(abo))
932 		return ERR_CAST(abo);
933 
934 	if (args->type == AMDXDNA_BO_DEV_HEAP) {
935 		abo->type = AMDXDNA_BO_DEV_HEAP;
936 		abo->internal = true;
937 	} else {
938 		abo->type = AMDXDNA_BO_SHARE;
939 		abo->internal = args->type == AMDXDNA_BO_CMD;
940 	}
941 
942 	return abo;
943 }
944 
945 static struct amdxdna_gem_obj *
946 amdxdna_drm_create_dev_heap_bo(struct drm_device *dev,
947 			       struct amdxdna_drm_create_bo *args, struct drm_file *filp)
948 {
949 	struct amdxdna_client *client = filp->driver_priv;
950 	struct amdxdna_dev *xdna = to_xdna_dev(dev);
951 	struct amdxdna_gem_obj *abo;
952 	int ret;
953 
954 	WARN_ON(!is_power_of_2(xdna->dev_info->dev_mem_size));
955 	XDNA_DBG(xdna, "Requested dev heap size 0x%llx", args->size);
956 	if (!args->size || !IS_ALIGNED(args->size, xdna->dev_info->dev_mem_size)) {
957 		XDNA_ERR(xdna, "The dev heap size 0x%llx is not multiple of 0x%lx",
958 			 args->size, xdna->dev_info->dev_mem_size);
959 		return ERR_PTR(-EINVAL);
960 	}
961 
962 	/* HEAP BO is a special case of SHARE BO. */
963 	abo = amdxdna_drm_create_share_bo(dev, args, filp);
964 	if (IS_ERR(abo))
965 		return ERR_CAST(abo);
966 
967 	/* Set up heap for this client. */
968 	mutex_lock(&client->mm_lock);
969 
970 	if (!xdna->dev_info->ops->hwctx_heap_expand &&
971 	    client->dev_heap_nid > 0) {
972 		XDNA_ERR(xdna, "Heap expansion is not supported");
973 		ret = -EOPNOTSUPP;
974 		goto mm_unlock;
975 	}
976 
977 	if (client->total_heap_size + abo->mem.size >
978 	    xdna->dev_info->dev_heap_max_size) {
979 		XDNA_ERR(xdna, "Heap size 0x%lx + 0x%lx exceeds max 0x%lx",
980 			 client->total_heap_size, abo->mem.size,
981 			 xdna->dev_info->dev_heap_max_size);
982 		ret = -ENOSPC;
983 		goto mm_unlock;
984 	}
985 
986 	ret = xa_insert(&client->dev_heap_xa, client->dev_heap_nid, abo, GFP_KERNEL);
987 	if (ret) {
988 		XDNA_ERR(xdna, "Add heap failed %d", ret);
989 		goto mm_unlock;
990 	}
991 
992 	abo->dev_addr = xdna->dev_info->dev_mem_base + client->total_heap_size;
993 	client->total_heap_size += abo->mem.size;
994 	client->dev_heap_nid++;
995 	drm_gem_object_get(to_gobj(abo));
996 
997 	mutex_unlock(&client->mm_lock);
998 
999 	return abo;
1000 
1001 mm_unlock:
1002 	mutex_unlock(&client->mm_lock);
1003 	drm_gem_object_put(to_gobj(abo));
1004 	return ERR_PTR(ret);
1005 }
1006 
1007 struct amdxdna_gem_obj *
1008 amdxdna_drm_create_dev_bo(struct drm_device *dev,
1009 			  struct amdxdna_drm_create_bo *args, struct drm_file *filp)
1010 {
1011 	size_t aligned_sz = PAGE_ALIGN(args->size);
1012 	struct amdxdna_client *client = filp->driver_priv;
1013 	struct amdxdna_dev *xdna = to_xdna_dev(dev);
1014 	struct amdxdna_gem_obj *abo;
1015 	struct drm_gem_object *gobj;
1016 	int ret;
1017 
1018 	if (!aligned_sz) {
1019 		XDNA_ERR(xdna, "Invalid BO size 0x%llx", args->size);
1020 		return ERR_PTR(-EINVAL);
1021 	}
1022 
1023 	abo = amdxdna_gem_create_obj(dev, aligned_sz);
1024 	if (IS_ERR(abo))
1025 		return abo;
1026 	gobj = to_gobj(abo);
1027 	gobj->funcs = &amdxdna_gem_dev_obj_funcs;
1028 	abo->type = AMDXDNA_BO_DEV;
1029 	abo->internal = true;
1030 	/*
1031 	 * DEV BOs cannot be alive when client is gone, it's OK to
1032 	 * always establish the connection.
1033 	 */
1034 	abo->client = client;
1035 
1036 	ret = amdxdna_gem_heap_alloc(abo);
1037 	if (ret) {
1038 		amdxdna_gem_destroy_obj(abo);
1039 		return ERR_PTR(ret);
1040 	}
1041 
1042 	drm_gem_private_object_init(dev, gobj, aligned_sz);
1043 
1044 	return abo;
1045 }
1046 
1047 int amdxdna_drm_create_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
1048 {
1049 	struct amdxdna_client *client = filp->driver_priv;
1050 	struct amdxdna_dev *xdna = to_xdna_dev(dev);
1051 	struct amdxdna_drm_create_bo *args = data;
1052 	struct amdxdna_gem_obj *abo;
1053 	int ret;
1054 
1055 	if (args->flags)
1056 		return -EINVAL;
1057 
1058 	XDNA_DBG(xdna, "BO arg type %d vaddr 0x%llx size 0x%llx flags 0x%llx",
1059 		 args->type, args->vaddr, args->size, args->flags);
1060 	switch (args->type) {
1061 	case AMDXDNA_BO_CMD:
1062 	case AMDXDNA_BO_SHARE:
1063 		abo = amdxdna_drm_create_share_bo(dev, args, filp);
1064 		break;
1065 	case AMDXDNA_BO_DEV_HEAP:
1066 		abo = amdxdna_drm_create_dev_heap_bo(dev, args, filp);
1067 		break;
1068 	case AMDXDNA_BO_DEV:
1069 		abo = amdxdna_drm_create_dev_bo(dev, args, filp);
1070 		if (!IS_ERR(abo)) {
1071 			mutex_lock(&xdna->dev_lock);
1072 			ret = amdxdna_update_heap(client, NULL);
1073 			mutex_unlock(&xdna->dev_lock);
1074 			if (ret)
1075 				goto put_obj;
1076 		}
1077 		break;
1078 	default:
1079 		return -EINVAL;
1080 	}
1081 	if (IS_ERR(abo))
1082 		return PTR_ERR(abo);
1083 
1084 	/* Ready to publish object to userspace and count for BO usage. */
1085 	ret = drm_gem_handle_create(filp, to_gobj(abo), &args->handle);
1086 	if (ret) {
1087 		XDNA_ERR(xdna, "Create handle failed");
1088 		goto put_obj;
1089 	}
1090 
1091 	XDNA_DBG(xdna, "BO hdl %d type %d userptr 0x%llx xdna_addr 0x%llx size 0x%lx",
1092 		 args->handle, args->type, amdxdna_gem_uva(abo),
1093 		 amdxdna_gem_dev_addr(abo), abo->mem.size);
1094 put_obj:
1095 	/* Dereference object reference. Handle holds it now. */
1096 	drm_gem_object_put(to_gobj(abo));
1097 	return ret;
1098 }
1099 
1100 static int amdxdna_bo_pin(struct amdxdna_gem_obj *abo)
1101 {
1102 	struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev);
1103 	int ret;
1104 
1105 	if (is_import_bo(abo))
1106 		return 0;
1107 
1108 	ret = drm_gem_shmem_pin(&abo->base);
1109 
1110 	XDNA_DBG(xdna, "BO type %d ret %d", abo->type, ret);
1111 	return ret;
1112 }
1113 
1114 static void amdxdna_bo_unpin(struct amdxdna_gem_obj *abo)
1115 {
1116 	struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev);
1117 
1118 	if (is_import_bo(abo))
1119 		return;
1120 
1121 	drm_gem_shmem_unpin(&abo->base);
1122 
1123 	XDNA_DBG(xdna, "BO type %d", abo->type);
1124 }
1125 
1126 int amdxdna_gem_pin_nolock(struct amdxdna_gem_obj *abo)
1127 {
1128 	struct amdxdna_client *client = abo->client;
1129 	struct amdxdna_gem_obj *heap;
1130 	unsigned long heap_id, last = ULONG_MAX;
1131 	int ret = 0;
1132 
1133 	if (abo->type != AMDXDNA_BO_DEV)
1134 		return amdxdna_bo_pin(abo);
1135 
1136 	xa_for_each_range(&client->dev_heap_xa, heap_id, heap,
1137 			  abo->heap_start_id, abo->heap_end_id) {
1138 		ret = amdxdna_bo_pin(heap);
1139 		if (ret)
1140 			break;
1141 		last = heap_id;
1142 	}
1143 
1144 	if (ret && last <= abo->heap_end_id) {
1145 		xa_for_each_range(&client->dev_heap_xa, heap_id, heap,
1146 				  abo->heap_start_id, last)
1147 			amdxdna_bo_unpin(heap);
1148 	}
1149 
1150 	return ret;
1151 }
1152 
1153 int amdxdna_gem_pin(struct amdxdna_gem_obj *abo)
1154 {
1155 	int ret;
1156 
1157 	mutex_lock(&abo->lock);
1158 	ret = amdxdna_gem_pin_nolock(abo);
1159 	mutex_unlock(&abo->lock);
1160 
1161 	return ret;
1162 }
1163 
1164 void amdxdna_gem_unpin(struct amdxdna_gem_obj *abo)
1165 {
1166 	mutex_lock(&abo->lock);
1167 	if (abo->type == AMDXDNA_BO_DEV) {
1168 		struct amdxdna_gem_obj *heap;
1169 		unsigned long heap_id;
1170 
1171 		xa_for_each_range(&abo->client->dev_heap_xa, heap_id, heap,
1172 				  abo->heap_start_id, abo->heap_end_id)
1173 			amdxdna_bo_unpin(heap);
1174 	} else {
1175 		amdxdna_bo_unpin(abo);
1176 	}
1177 
1178 	mutex_unlock(&abo->lock);
1179 }
1180 
1181 struct amdxdna_gem_obj *amdxdna_gem_get_obj(struct amdxdna_client *client,
1182 					    u32 bo_hdl, u8 bo_type)
1183 {
1184 	struct amdxdna_gem_obj *abo;
1185 	struct drm_gem_object *gobj;
1186 
1187 	gobj = drm_gem_object_lookup(client->filp, bo_hdl);
1188 	if (!gobj) {
1189 		XDNA_DBG(client->xdna, "Can not find bo %d", bo_hdl);
1190 		return NULL;
1191 	}
1192 
1193 	abo = to_xdna_obj(gobj);
1194 	if (bo_type == AMDXDNA_BO_INVALID || abo->type == bo_type)
1195 		return abo;
1196 
1197 	drm_gem_object_put(gobj);
1198 	return NULL;
1199 }
1200 
1201 int amdxdna_drm_get_bo_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
1202 {
1203 	struct amdxdna_drm_get_bo_info *args = data;
1204 	struct amdxdna_dev *xdna = to_xdna_dev(dev);
1205 	struct amdxdna_gem_obj *abo;
1206 	struct drm_gem_object *gobj;
1207 	int ret = 0;
1208 
1209 	if (args->ext || args->ext_flags || args->pad)
1210 		return -EINVAL;
1211 
1212 	gobj = drm_gem_object_lookup(filp, args->handle);
1213 	if (!gobj) {
1214 		XDNA_DBG(xdna, "Lookup GEM object %d failed", args->handle);
1215 		return -ENOENT;
1216 	}
1217 
1218 	abo = to_xdna_obj(gobj);
1219 	args->vaddr = amdxdna_gem_uva(abo);
1220 	args->xdna_addr = amdxdna_gem_dev_addr(abo);
1221 
1222 	if (abo->type != AMDXDNA_BO_DEV)
1223 		args->map_offset = drm_vma_node_offset_addr(&gobj->vma_node);
1224 	else
1225 		args->map_offset = AMDXDNA_INVALID_ADDR;
1226 
1227 	XDNA_DBG(xdna, "BO hdl %d map_offset 0x%llx vaddr 0x%llx xdna_addr 0x%llx",
1228 		 args->handle, args->map_offset, args->vaddr, args->xdna_addr);
1229 
1230 	drm_gem_object_put(gobj);
1231 	return ret;
1232 }
1233 
1234 static int amdxdna_flush_bo(struct amdxdna_gem_obj *abo, u64 offset, u64 size)
1235 {
1236 	u64 end;
1237 
1238 	if (offset >= abo->mem.size)
1239 		return -EINVAL;
1240 
1241 	if (check_add_overflow(offset, size, &end))
1242 		return -EINVAL;
1243 
1244 	size = min(abo->mem.size, end) - offset;
1245 	if (is_import_bo(abo))
1246 		drm_clflush_sg(abo->base.sgt);
1247 	else if (amdxdna_gem_vmap(abo))
1248 		drm_clflush_virt_range(amdxdna_gem_vmap(abo) + offset, size);
1249 	else if (abo->base.pages)
1250 		drm_clflush_pages(abo->base.pages, abo->mem.size >> PAGE_SHIFT);
1251 	else
1252 		return -EINVAL;
1253 
1254 	return 0;
1255 }
1256 
1257 /*
1258  * The sync bo ioctl is to make sure the CPU cache is in sync with memory.
1259  * This is required because NPU is not cache coherent device. CPU cache
1260  * flushing/invalidation is expensive so it is best to handle this outside
1261  * of the command submission path. This ioctl allows explicit cache
1262  * flushing/invalidation outside of the critical path.
1263  */
1264 int amdxdna_drm_sync_bo_ioctl(struct drm_device *dev,
1265 			      void *data, struct drm_file *filp)
1266 {
1267 	struct amdxdna_client *client = filp->driver_priv;
1268 	struct amdxdna_dev *xdna = to_xdna_dev(dev);
1269 	struct amdxdna_drm_sync_bo *args = data;
1270 	struct amdxdna_gem_obj *abo;
1271 	struct drm_gem_object *gobj;
1272 	int ret = 0;
1273 
1274 	gobj = drm_gem_object_lookup(filp, args->handle);
1275 	if (!gobj) {
1276 		XDNA_ERR(xdna, "Lookup GEM object failed");
1277 		return -ENOENT;
1278 	}
1279 	abo = to_xdna_obj(gobj);
1280 
1281 	if (abo->type == AMDXDNA_BO_DEV) {
1282 		struct amdxdna_gem_obj *heap;
1283 		unsigned long heap_id;
1284 		u64 bo_start = amdxdna_gem_dev_addr(abo);
1285 		u64 flush_start = bo_start + args->offset;
1286 		u64 flush_end = flush_start + args->size;
1287 
1288 		xa_for_each_range(&client->dev_heap_xa, heap_id, heap,
1289 				  abo->heap_start_id, abo->heap_end_id) {
1290 			u64 heap_start = amdxdna_gem_dev_addr(heap);
1291 			u64 heap_end = heap_start + heap->mem.size;
1292 			u64 start = max(flush_start, heap_start);
1293 			u64 end = min(flush_end, heap_end);
1294 
1295 			if (start >= end)
1296 				continue;
1297 
1298 			ret = amdxdna_flush_bo(heap, start - heap_start, end - start);
1299 			if (ret) {
1300 				XDNA_ERR(xdna, "Failed to flush heap %ld ret %d",
1301 					 heap_id, ret);
1302 				goto put_obj;
1303 			}
1304 		}
1305 	} else {
1306 		ret = amdxdna_gem_pin(abo);
1307 		if (ret) {
1308 			XDNA_ERR(xdna, "Pin BO %d failed, ret %d", args->handle, ret);
1309 			goto put_obj;
1310 		}
1311 
1312 		ret = amdxdna_flush_bo(abo, args->offset, args->size);
1313 		amdxdna_gem_unpin(abo);
1314 
1315 		if (ret) {
1316 			drm_WARN(&xdna->ddev, 1, "Can not get flush memory");
1317 			goto put_obj;
1318 		}
1319 	}
1320 
1321 	XDNA_DBG(xdna, "Sync bo %d offset 0x%llx, size 0x%llx\n",
1322 		 args->handle, args->offset, args->size);
1323 
1324 	if (args->direction == SYNC_DIRECT_FROM_DEVICE)
1325 		ret = amdxdna_hwctx_sync_debug_bo(client, args->handle);
1326 
1327 put_obj:
1328 	drm_gem_object_put(gobj);
1329 	return ret;
1330 }
1331 
1332 int amdxdna_drm_get_bo_usage(struct drm_device *dev, struct amdxdna_drm_get_array *args)
1333 {
1334 	size_t min_sz = min(args->element_size, sizeof(struct amdxdna_drm_bo_usage));
1335 	char __user *buf = u64_to_user_ptr(args->buffer);
1336 	struct amdxdna_dev *xdna = to_xdna_dev(dev);
1337 	struct amdxdna_client *tmp_client;
1338 	struct amdxdna_drm_bo_usage tmp;
1339 
1340 	drm_WARN_ON(dev, !mutex_is_locked(&xdna->dev_lock));
1341 
1342 	if (args->num_element != 1)
1343 		return -EINVAL;
1344 
1345 	if (copy_from_user(&tmp, buf, min_sz))
1346 		return -EFAULT;
1347 
1348 	if (!tmp.pid)
1349 		return -EINVAL;
1350 
1351 	tmp.total_usage = 0;
1352 	tmp.internal_usage = 0;
1353 	tmp.heap_usage = 0;
1354 
1355 	list_for_each_entry(tmp_client, &xdna->client_list, node) {
1356 		if (tmp.pid != tmp_client->pid)
1357 			continue;
1358 
1359 		mutex_lock(&tmp_client->mm_lock);
1360 		tmp.total_usage += tmp_client->total_bo_usage;
1361 		tmp.internal_usage += tmp_client->total_int_bo_usage;
1362 		tmp.heap_usage += tmp_client->heap_usage;
1363 		mutex_unlock(&tmp_client->mm_lock);
1364 	}
1365 
1366 	if (copy_to_user(buf, &tmp, min_sz))
1367 		return -EFAULT;
1368 
1369 	return 0;
1370 }
1371