xref: /linux/drivers/accel/amdxdna/amdxdna_gem.c (revision f6e8dc9edf963dbc99085e54f6ced6da9daa6100)
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_ctx.h"
20 #include "amdxdna_gem.h"
21 #include "amdxdna_pci_drv.h"
22 #include "amdxdna_ubuf.h"
23 
24 #define XDNA_MAX_CMD_BO_SIZE	SZ_32K
25 
26 MODULE_IMPORT_NS("DMA_BUF");
27 
28 static int
29 amdxdna_gem_heap_alloc(struct amdxdna_gem_obj *abo)
30 {
31 	struct amdxdna_client *client = abo->client;
32 	struct amdxdna_dev *xdna = client->xdna;
33 	struct amdxdna_mem *mem = &abo->mem;
34 	struct amdxdna_gem_obj *heap;
35 	u64 offset;
36 	u32 align;
37 	int ret;
38 
39 	mutex_lock(&client->mm_lock);
40 
41 	heap = client->dev_heap;
42 	if (!heap) {
43 		ret = -EINVAL;
44 		goto unlock_out;
45 	}
46 
47 	if (heap->mem.userptr == AMDXDNA_INVALID_ADDR) {
48 		XDNA_ERR(xdna, "Invalid dev heap userptr");
49 		ret = -EINVAL;
50 		goto unlock_out;
51 	}
52 
53 	if (mem->size == 0 || mem->size > heap->mem.size) {
54 		XDNA_ERR(xdna, "Invalid dev bo size 0x%lx, limit 0x%lx",
55 			 mem->size, heap->mem.size);
56 		ret = -EINVAL;
57 		goto unlock_out;
58 	}
59 
60 	align = 1 << max(PAGE_SHIFT, xdna->dev_info->dev_mem_buf_shift);
61 	ret = drm_mm_insert_node_generic(&heap->mm, &abo->mm_node,
62 					 mem->size, align,
63 					 0, DRM_MM_INSERT_BEST);
64 	if (ret) {
65 		XDNA_ERR(xdna, "Failed to alloc dev bo memory, ret %d", ret);
66 		goto unlock_out;
67 	}
68 
69 	mem->dev_addr = abo->mm_node.start;
70 	offset = mem->dev_addr - heap->mem.dev_addr;
71 	mem->userptr = heap->mem.userptr + offset;
72 	mem->kva = heap->mem.kva + offset;
73 
74 	drm_gem_object_get(to_gobj(heap));
75 
76 unlock_out:
77 	mutex_unlock(&client->mm_lock);
78 
79 	return ret;
80 }
81 
82 static void
83 amdxdna_gem_destroy_obj(struct amdxdna_gem_obj *abo)
84 {
85 	mutex_destroy(&abo->lock);
86 	kfree(abo);
87 }
88 
89 static void
90 amdxdna_gem_heap_free(struct amdxdna_gem_obj *abo)
91 {
92 	struct amdxdna_gem_obj *heap;
93 
94 	mutex_lock(&abo->client->mm_lock);
95 
96 	drm_mm_remove_node(&abo->mm_node);
97 
98 	heap = abo->client->dev_heap;
99 	drm_gem_object_put(to_gobj(heap));
100 
101 	mutex_unlock(&abo->client->mm_lock);
102 }
103 
104 static bool amdxdna_hmm_invalidate(struct mmu_interval_notifier *mni,
105 				   const struct mmu_notifier_range *range,
106 				   unsigned long cur_seq)
107 {
108 	struct amdxdna_umap *mapp = container_of(mni, struct amdxdna_umap, notifier);
109 	struct amdxdna_gem_obj *abo = mapp->abo;
110 	struct amdxdna_dev *xdna;
111 
112 	xdna = to_xdna_dev(to_gobj(abo)->dev);
113 	XDNA_DBG(xdna, "Invalidating range 0x%lx, 0x%lx, type %d",
114 		 mapp->vma->vm_start, mapp->vma->vm_end, abo->type);
115 
116 	if (!mmu_notifier_range_blockable(range))
117 		return false;
118 
119 	down_write(&xdna->notifier_lock);
120 	abo->mem.map_invalid = true;
121 	mapp->invalid = true;
122 	mmu_interval_set_seq(&mapp->notifier, cur_seq);
123 	up_write(&xdna->notifier_lock);
124 
125 	xdna->dev_info->ops->hmm_invalidate(abo, cur_seq);
126 
127 	if (range->event == MMU_NOTIFY_UNMAP) {
128 		down_write(&xdna->notifier_lock);
129 		if (!mapp->unmapped) {
130 			queue_work(xdna->notifier_wq, &mapp->hmm_unreg_work);
131 			mapp->unmapped = true;
132 		}
133 		up_write(&xdna->notifier_lock);
134 	}
135 
136 	return true;
137 }
138 
139 static const struct mmu_interval_notifier_ops amdxdna_hmm_ops = {
140 	.invalidate = amdxdna_hmm_invalidate,
141 };
142 
143 static void amdxdna_hmm_unregister(struct amdxdna_gem_obj *abo,
144 				   struct vm_area_struct *vma)
145 {
146 	struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev);
147 	struct amdxdna_umap *mapp;
148 
149 	down_read(&xdna->notifier_lock);
150 	list_for_each_entry(mapp, &abo->mem.umap_list, node) {
151 		if (!vma || mapp->vma == vma) {
152 			if (!mapp->unmapped) {
153 				queue_work(xdna->notifier_wq, &mapp->hmm_unreg_work);
154 				mapp->unmapped = true;
155 			}
156 			if (vma)
157 				break;
158 		}
159 	}
160 	up_read(&xdna->notifier_lock);
161 }
162 
163 static void amdxdna_umap_release(struct kref *ref)
164 {
165 	struct amdxdna_umap *mapp = container_of(ref, struct amdxdna_umap, refcnt);
166 	struct vm_area_struct *vma = mapp->vma;
167 	struct amdxdna_dev *xdna;
168 
169 	mmu_interval_notifier_remove(&mapp->notifier);
170 	if (is_import_bo(mapp->abo) && vma->vm_file && vma->vm_file->f_mapping)
171 		mapping_clear_unevictable(vma->vm_file->f_mapping);
172 
173 	xdna = to_xdna_dev(to_gobj(mapp->abo)->dev);
174 	down_write(&xdna->notifier_lock);
175 	list_del(&mapp->node);
176 	up_write(&xdna->notifier_lock);
177 
178 	kvfree(mapp->range.hmm_pfns);
179 	kfree(mapp);
180 }
181 
182 void amdxdna_umap_put(struct amdxdna_umap *mapp)
183 {
184 	kref_put(&mapp->refcnt, amdxdna_umap_release);
185 }
186 
187 static void amdxdna_hmm_unreg_work(struct work_struct *work)
188 {
189 	struct amdxdna_umap *mapp = container_of(work, struct amdxdna_umap,
190 						 hmm_unreg_work);
191 
192 	amdxdna_umap_put(mapp);
193 }
194 
195 static int amdxdna_hmm_register(struct amdxdna_gem_obj *abo,
196 				struct vm_area_struct *vma)
197 {
198 	struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev);
199 	unsigned long len = vma->vm_end - vma->vm_start;
200 	unsigned long addr = vma->vm_start;
201 	struct amdxdna_umap *mapp;
202 	u32 nr_pages;
203 	int ret;
204 
205 	if (!xdna->dev_info->ops->hmm_invalidate)
206 		return 0;
207 
208 	mapp = kzalloc(sizeof(*mapp), GFP_KERNEL);
209 	if (!mapp)
210 		return -ENOMEM;
211 
212 	nr_pages = (PAGE_ALIGN(addr + len) - (addr & PAGE_MASK)) >> PAGE_SHIFT;
213 	mapp->range.hmm_pfns = kvcalloc(nr_pages, sizeof(*mapp->range.hmm_pfns),
214 					GFP_KERNEL);
215 	if (!mapp->range.hmm_pfns) {
216 		ret = -ENOMEM;
217 		goto free_map;
218 	}
219 
220 	ret = mmu_interval_notifier_insert_locked(&mapp->notifier,
221 						  current->mm,
222 						  addr,
223 						  len,
224 						  &amdxdna_hmm_ops);
225 	if (ret) {
226 		XDNA_ERR(xdna, "Insert mmu notifier failed, ret %d", ret);
227 		goto free_pfns;
228 	}
229 
230 	mapp->range.notifier = &mapp->notifier;
231 	mapp->range.start = vma->vm_start;
232 	mapp->range.end = vma->vm_end;
233 	mapp->range.default_flags = HMM_PFN_REQ_FAULT;
234 	mapp->vma = vma;
235 	mapp->abo = abo;
236 	kref_init(&mapp->refcnt);
237 
238 	if (abo->mem.userptr == AMDXDNA_INVALID_ADDR)
239 		abo->mem.userptr = addr;
240 	INIT_WORK(&mapp->hmm_unreg_work, amdxdna_hmm_unreg_work);
241 	if (is_import_bo(abo) && vma->vm_file && vma->vm_file->f_mapping)
242 		mapping_set_unevictable(vma->vm_file->f_mapping);
243 
244 	down_write(&xdna->notifier_lock);
245 	list_add_tail(&mapp->node, &abo->mem.umap_list);
246 	up_write(&xdna->notifier_lock);
247 
248 	return 0;
249 
250 free_pfns:
251 	kvfree(mapp->range.hmm_pfns);
252 free_map:
253 	kfree(mapp);
254 	return ret;
255 }
256 
257 static void amdxdna_gem_dev_obj_free(struct drm_gem_object *gobj)
258 {
259 	struct amdxdna_dev *xdna = to_xdna_dev(gobj->dev);
260 	struct amdxdna_gem_obj *abo = to_xdna_obj(gobj);
261 
262 	XDNA_DBG(xdna, "BO type %d xdna_addr 0x%llx", abo->type, abo->mem.dev_addr);
263 	if (abo->pinned)
264 		amdxdna_gem_unpin(abo);
265 
266 	amdxdna_gem_heap_free(abo);
267 	drm_gem_object_release(gobj);
268 	amdxdna_gem_destroy_obj(abo);
269 }
270 
271 static int amdxdna_insert_pages(struct amdxdna_gem_obj *abo,
272 				struct vm_area_struct *vma)
273 {
274 	struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev);
275 	unsigned long num_pages = vma_pages(vma);
276 	unsigned long offset = 0;
277 	int ret;
278 
279 	if (!is_import_bo(abo)) {
280 		ret = drm_gem_shmem_mmap(&abo->base, vma);
281 		if (ret) {
282 			XDNA_ERR(xdna, "Failed shmem mmap %d", ret);
283 			return ret;
284 		}
285 
286 		/* The buffer is based on memory pages. Fix the flag. */
287 		vm_flags_mod(vma, VM_MIXEDMAP, VM_PFNMAP);
288 		ret = vm_insert_pages(vma, vma->vm_start, abo->base.pages,
289 				      &num_pages);
290 		if (ret) {
291 			XDNA_ERR(xdna, "Failed insert pages %d", ret);
292 			vma->vm_ops->close(vma);
293 			return ret;
294 		}
295 
296 		return 0;
297 	}
298 
299 	vma->vm_private_data = NULL;
300 	vma->vm_ops = NULL;
301 	ret = dma_buf_mmap(abo->dma_buf, vma, 0);
302 	if (ret) {
303 		XDNA_ERR(xdna, "Failed to mmap dma buf %d", ret);
304 		return ret;
305 	}
306 
307 	do {
308 		vm_fault_t fault_ret;
309 
310 		fault_ret = handle_mm_fault(vma, vma->vm_start + offset,
311 					    FAULT_FLAG_WRITE, NULL);
312 		if (fault_ret & VM_FAULT_ERROR) {
313 			vma->vm_ops->close(vma);
314 			XDNA_ERR(xdna, "Fault in page failed");
315 			return -EFAULT;
316 		}
317 
318 		offset += PAGE_SIZE;
319 	} while (--num_pages);
320 
321 	/* Drop the reference drm_gem_mmap_obj() acquired.*/
322 	drm_gem_object_put(to_gobj(abo));
323 
324 	return 0;
325 }
326 
327 static int amdxdna_gem_obj_mmap(struct drm_gem_object *gobj,
328 				struct vm_area_struct *vma)
329 {
330 	struct amdxdna_dev *xdna = to_xdna_dev(gobj->dev);
331 	struct amdxdna_gem_obj *abo = to_xdna_obj(gobj);
332 	int ret;
333 
334 	ret = amdxdna_hmm_register(abo, vma);
335 	if (ret)
336 		return ret;
337 
338 	ret = amdxdna_insert_pages(abo, vma);
339 	if (ret) {
340 		XDNA_ERR(xdna, "Failed insert pages, ret %d", ret);
341 		goto hmm_unreg;
342 	}
343 
344 	XDNA_DBG(xdna, "BO map_offset 0x%llx type %d userptr 0x%lx size 0x%lx",
345 		 drm_vma_node_offset_addr(&gobj->vma_node), abo->type,
346 		 vma->vm_start, gobj->size);
347 	return 0;
348 
349 hmm_unreg:
350 	amdxdna_hmm_unregister(abo, vma);
351 	return ret;
352 }
353 
354 static int amdxdna_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
355 {
356 	struct drm_gem_object *gobj = dma_buf->priv;
357 	struct amdxdna_gem_obj *abo = to_xdna_obj(gobj);
358 	unsigned long num_pages = vma_pages(vma);
359 	int ret;
360 
361 	vma->vm_ops = &drm_gem_shmem_vm_ops;
362 	vma->vm_private_data = gobj;
363 
364 	drm_gem_object_get(gobj);
365 	ret = drm_gem_shmem_mmap(&abo->base, vma);
366 	if (ret)
367 		goto put_obj;
368 
369 	/* The buffer is based on memory pages. Fix the flag. */
370 	vm_flags_mod(vma, VM_MIXEDMAP, VM_PFNMAP);
371 	ret = vm_insert_pages(vma, vma->vm_start, abo->base.pages,
372 			      &num_pages);
373 	if (ret)
374 		goto close_vma;
375 
376 	return 0;
377 
378 close_vma:
379 	vma->vm_ops->close(vma);
380 put_obj:
381 	drm_gem_object_put(gobj);
382 	return ret;
383 }
384 
385 static const struct dma_buf_ops amdxdna_dmabuf_ops = {
386 	.attach = drm_gem_map_attach,
387 	.detach = drm_gem_map_detach,
388 	.map_dma_buf = drm_gem_map_dma_buf,
389 	.unmap_dma_buf = drm_gem_unmap_dma_buf,
390 	.release = drm_gem_dmabuf_release,
391 	.mmap = amdxdna_gem_dmabuf_mmap,
392 	.vmap = drm_gem_dmabuf_vmap,
393 	.vunmap = drm_gem_dmabuf_vunmap,
394 };
395 
396 static int amdxdna_gem_obj_vmap(struct amdxdna_gem_obj *abo, void **vaddr)
397 {
398 	struct iosys_map map = IOSYS_MAP_INIT_VADDR(NULL);
399 	int ret;
400 
401 	if (is_import_bo(abo))
402 		ret = dma_buf_vmap_unlocked(abo->dma_buf, &map);
403 	else
404 		ret = drm_gem_vmap(to_gobj(abo), &map);
405 
406 	*vaddr = map.vaddr;
407 	return ret;
408 }
409 
410 static void amdxdna_gem_obj_vunmap(struct amdxdna_gem_obj *abo)
411 {
412 	struct iosys_map map;
413 
414 	if (!abo->mem.kva)
415 		return;
416 
417 	iosys_map_set_vaddr(&map, abo->mem.kva);
418 
419 	if (is_import_bo(abo))
420 		dma_buf_vunmap_unlocked(abo->dma_buf, &map);
421 	else
422 		drm_gem_vunmap(to_gobj(abo), &map);
423 }
424 
425 static struct dma_buf *amdxdna_gem_prime_export(struct drm_gem_object *gobj, int flags)
426 {
427 	struct amdxdna_gem_obj *abo = to_xdna_obj(gobj);
428 	DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
429 
430 	if (abo->dma_buf) {
431 		get_dma_buf(abo->dma_buf);
432 		return abo->dma_buf;
433 	}
434 
435 	exp_info.ops = &amdxdna_dmabuf_ops;
436 	exp_info.size = gobj->size;
437 	exp_info.flags = flags;
438 	exp_info.priv = gobj;
439 	exp_info.resv = gobj->resv;
440 
441 	return drm_gem_dmabuf_export(gobj->dev, &exp_info);
442 }
443 
444 static void amdxdna_imported_obj_free(struct amdxdna_gem_obj *abo)
445 {
446 	dma_buf_unmap_attachment_unlocked(abo->attach, abo->base.sgt, DMA_BIDIRECTIONAL);
447 	dma_buf_detach(abo->dma_buf, abo->attach);
448 	dma_buf_put(abo->dma_buf);
449 	drm_gem_object_release(to_gobj(abo));
450 	kfree(abo);
451 }
452 
453 static void amdxdna_gem_obj_free(struct drm_gem_object *gobj)
454 {
455 	struct amdxdna_dev *xdna = to_xdna_dev(gobj->dev);
456 	struct amdxdna_gem_obj *abo = to_xdna_obj(gobj);
457 
458 	XDNA_DBG(xdna, "BO type %d xdna_addr 0x%llx", abo->type, abo->mem.dev_addr);
459 
460 	amdxdna_hmm_unregister(abo, NULL);
461 	flush_workqueue(xdna->notifier_wq);
462 
463 	if (abo->pinned)
464 		amdxdna_gem_unpin(abo);
465 
466 	if (abo->type == AMDXDNA_BO_DEV_HEAP)
467 		drm_mm_takedown(&abo->mm);
468 
469 	amdxdna_gem_obj_vunmap(abo);
470 	mutex_destroy(&abo->lock);
471 
472 	if (is_import_bo(abo)) {
473 		amdxdna_imported_obj_free(abo);
474 		return;
475 	}
476 
477 	drm_gem_shmem_free(&abo->base);
478 }
479 
480 static const struct drm_gem_object_funcs amdxdna_gem_dev_obj_funcs = {
481 	.free = amdxdna_gem_dev_obj_free,
482 };
483 
484 static const struct drm_gem_object_funcs amdxdna_gem_shmem_funcs = {
485 	.free = amdxdna_gem_obj_free,
486 	.print_info = drm_gem_shmem_object_print_info,
487 	.pin = drm_gem_shmem_object_pin,
488 	.unpin = drm_gem_shmem_object_unpin,
489 	.get_sg_table = drm_gem_shmem_object_get_sg_table,
490 	.vmap = drm_gem_shmem_object_vmap,
491 	.vunmap = drm_gem_shmem_object_vunmap,
492 	.mmap = amdxdna_gem_obj_mmap,
493 	.vm_ops = &drm_gem_shmem_vm_ops,
494 	.export = amdxdna_gem_prime_export,
495 };
496 
497 static struct amdxdna_gem_obj *
498 amdxdna_gem_create_obj(struct drm_device *dev, size_t size)
499 {
500 	struct amdxdna_gem_obj *abo;
501 
502 	abo = kzalloc(sizeof(*abo), GFP_KERNEL);
503 	if (!abo)
504 		return ERR_PTR(-ENOMEM);
505 
506 	abo->pinned = false;
507 	abo->assigned_hwctx = AMDXDNA_INVALID_CTX_HANDLE;
508 	mutex_init(&abo->lock);
509 
510 	abo->mem.userptr = AMDXDNA_INVALID_ADDR;
511 	abo->mem.dev_addr = AMDXDNA_INVALID_ADDR;
512 	abo->mem.size = size;
513 	INIT_LIST_HEAD(&abo->mem.umap_list);
514 
515 	return abo;
516 }
517 
518 /* For drm_driver->gem_create_object callback */
519 struct drm_gem_object *
520 amdxdna_gem_create_object_cb(struct drm_device *dev, size_t size)
521 {
522 	struct amdxdna_gem_obj *abo;
523 
524 	abo = amdxdna_gem_create_obj(dev, size);
525 	if (IS_ERR(abo))
526 		return ERR_CAST(abo);
527 
528 	to_gobj(abo)->funcs = &amdxdna_gem_shmem_funcs;
529 
530 	return to_gobj(abo);
531 }
532 
533 static struct amdxdna_gem_obj *
534 amdxdna_gem_create_shmem_object(struct drm_device *dev, size_t size)
535 {
536 	struct drm_gem_shmem_object *shmem = drm_gem_shmem_create(dev, size);
537 
538 	if (IS_ERR(shmem))
539 		return ERR_CAST(shmem);
540 
541 	shmem->map_wc = false;
542 	return to_xdna_obj(&shmem->base);
543 }
544 
545 static struct amdxdna_gem_obj *
546 amdxdna_gem_create_ubuf_object(struct drm_device *dev, struct amdxdna_drm_create_bo *args)
547 {
548 	struct amdxdna_dev *xdna = to_xdna_dev(dev);
549 	enum amdxdna_ubuf_flag flags = 0;
550 	struct amdxdna_drm_va_tbl va_tbl;
551 	struct drm_gem_object *gobj;
552 	struct dma_buf *dma_buf;
553 
554 	if (copy_from_user(&va_tbl, u64_to_user_ptr(args->vaddr), sizeof(va_tbl))) {
555 		XDNA_DBG(xdna, "Access va table failed");
556 		return ERR_PTR(-EINVAL);
557 	}
558 
559 	if (va_tbl.num_entries) {
560 		if (args->type == AMDXDNA_BO_CMD)
561 			flags |= AMDXDNA_UBUF_FLAG_MAP_DMA;
562 
563 		dma_buf = amdxdna_get_ubuf(dev, flags, va_tbl.num_entries,
564 					   u64_to_user_ptr(args->vaddr + sizeof(va_tbl)));
565 	} else {
566 		dma_buf = dma_buf_get(va_tbl.dmabuf_fd);
567 	}
568 
569 	if (IS_ERR(dma_buf))
570 		return ERR_CAST(dma_buf);
571 
572 	gobj = amdxdna_gem_prime_import(dev, dma_buf);
573 	if (IS_ERR(gobj)) {
574 		dma_buf_put(dma_buf);
575 		return ERR_CAST(gobj);
576 	}
577 
578 	dma_buf_put(dma_buf);
579 
580 	return to_xdna_obj(gobj);
581 }
582 
583 static struct amdxdna_gem_obj *
584 amdxdna_gem_create_object(struct drm_device *dev,
585 			  struct amdxdna_drm_create_bo *args)
586 {
587 	size_t aligned_sz = PAGE_ALIGN(args->size);
588 
589 	if (args->vaddr)
590 		return amdxdna_gem_create_ubuf_object(dev, args);
591 
592 	return amdxdna_gem_create_shmem_object(dev, aligned_sz);
593 }
594 
595 struct drm_gem_object *
596 amdxdna_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf)
597 {
598 	struct dma_buf_attachment *attach;
599 	struct amdxdna_gem_obj *abo;
600 	struct drm_gem_object *gobj;
601 	struct sg_table *sgt;
602 	int ret;
603 
604 	get_dma_buf(dma_buf);
605 
606 	attach = dma_buf_attach(dma_buf, dev->dev);
607 	if (IS_ERR(attach)) {
608 		ret = PTR_ERR(attach);
609 		goto put_buf;
610 	}
611 
612 	sgt = dma_buf_map_attachment_unlocked(attach, DMA_BIDIRECTIONAL);
613 	if (IS_ERR(sgt)) {
614 		ret = PTR_ERR(sgt);
615 		goto fail_detach;
616 	}
617 
618 	gobj = drm_gem_shmem_prime_import_sg_table(dev, attach, sgt);
619 	if (IS_ERR(gobj)) {
620 		ret = PTR_ERR(gobj);
621 		goto fail_unmap;
622 	}
623 
624 	abo = to_xdna_obj(gobj);
625 	abo->attach = attach;
626 	abo->dma_buf = dma_buf;
627 
628 	return gobj;
629 
630 fail_unmap:
631 	dma_buf_unmap_attachment_unlocked(attach, sgt, DMA_BIDIRECTIONAL);
632 fail_detach:
633 	dma_buf_detach(dma_buf, attach);
634 put_buf:
635 	dma_buf_put(dma_buf);
636 
637 	return ERR_PTR(ret);
638 }
639 
640 static struct amdxdna_gem_obj *
641 amdxdna_drm_alloc_shmem(struct drm_device *dev,
642 			struct amdxdna_drm_create_bo *args,
643 			struct drm_file *filp)
644 {
645 	struct amdxdna_client *client = filp->driver_priv;
646 	struct amdxdna_gem_obj *abo;
647 
648 	abo = amdxdna_gem_create_object(dev, args);
649 	if (IS_ERR(abo))
650 		return ERR_CAST(abo);
651 
652 	abo->client = client;
653 	abo->type = AMDXDNA_BO_SHMEM;
654 
655 	return abo;
656 }
657 
658 static struct amdxdna_gem_obj *
659 amdxdna_drm_create_dev_heap(struct drm_device *dev,
660 			    struct amdxdna_drm_create_bo *args,
661 			    struct drm_file *filp)
662 {
663 	struct amdxdna_client *client = filp->driver_priv;
664 	struct amdxdna_dev *xdna = to_xdna_dev(dev);
665 	struct amdxdna_gem_obj *abo;
666 	int ret;
667 
668 	if (args->size > xdna->dev_info->dev_mem_size) {
669 		XDNA_DBG(xdna, "Invalid dev heap size 0x%llx, limit 0x%lx",
670 			 args->size, xdna->dev_info->dev_mem_size);
671 		return ERR_PTR(-EINVAL);
672 	}
673 
674 	mutex_lock(&client->mm_lock);
675 	if (client->dev_heap) {
676 		XDNA_DBG(client->xdna, "dev heap is already created");
677 		ret = -EBUSY;
678 		goto mm_unlock;
679 	}
680 
681 	abo = amdxdna_gem_create_object(dev, args);
682 	if (IS_ERR(abo)) {
683 		ret = PTR_ERR(abo);
684 		goto mm_unlock;
685 	}
686 
687 	abo->type = AMDXDNA_BO_DEV_HEAP;
688 	abo->client = client;
689 	abo->mem.dev_addr = client->xdna->dev_info->dev_mem_base;
690 	drm_mm_init(&abo->mm, abo->mem.dev_addr, abo->mem.size);
691 
692 	ret = amdxdna_gem_obj_vmap(abo, &abo->mem.kva);
693 	if (ret) {
694 		XDNA_ERR(xdna, "Vmap heap bo failed, ret %d", ret);
695 		goto release_obj;
696 	}
697 
698 	client->dev_heap = abo;
699 	drm_gem_object_get(to_gobj(abo));
700 	mutex_unlock(&client->mm_lock);
701 
702 	return abo;
703 
704 release_obj:
705 	drm_gem_object_put(to_gobj(abo));
706 mm_unlock:
707 	mutex_unlock(&client->mm_lock);
708 	return ERR_PTR(ret);
709 }
710 
711 struct amdxdna_gem_obj *
712 amdxdna_drm_alloc_dev_bo(struct drm_device *dev,
713 			 struct amdxdna_drm_create_bo *args,
714 			 struct drm_file *filp)
715 {
716 	struct amdxdna_client *client = filp->driver_priv;
717 	struct amdxdna_dev *xdna = to_xdna_dev(dev);
718 	size_t aligned_sz = PAGE_ALIGN(args->size);
719 	struct amdxdna_gem_obj *abo;
720 	int ret;
721 
722 	abo = amdxdna_gem_create_obj(&xdna->ddev, aligned_sz);
723 	if (IS_ERR(abo))
724 		return abo;
725 
726 	to_gobj(abo)->funcs = &amdxdna_gem_dev_obj_funcs;
727 	abo->type = AMDXDNA_BO_DEV;
728 	abo->client = client;
729 
730 	ret = amdxdna_gem_heap_alloc(abo);
731 	if (ret) {
732 		XDNA_ERR(xdna, "Failed to alloc dev bo memory, ret %d", ret);
733 		amdxdna_gem_destroy_obj(abo);
734 		return ERR_PTR(ret);
735 	}
736 
737 	drm_gem_private_object_init(&xdna->ddev, to_gobj(abo), aligned_sz);
738 
739 	return abo;
740 }
741 
742 static struct amdxdna_gem_obj *
743 amdxdna_drm_create_cmd_bo(struct drm_device *dev,
744 			  struct amdxdna_drm_create_bo *args,
745 			  struct drm_file *filp)
746 {
747 	struct amdxdna_dev *xdna = to_xdna_dev(dev);
748 	struct amdxdna_gem_obj *abo;
749 	int ret;
750 
751 	if (args->size > XDNA_MAX_CMD_BO_SIZE) {
752 		XDNA_ERR(xdna, "Command bo size 0x%llx too large", args->size);
753 		return ERR_PTR(-EINVAL);
754 	}
755 
756 	if (args->size < sizeof(struct amdxdna_cmd)) {
757 		XDNA_DBG(xdna, "Command BO size 0x%llx too small", args->size);
758 		return ERR_PTR(-EINVAL);
759 	}
760 
761 	abo = amdxdna_gem_create_object(dev, args);
762 	if (IS_ERR(abo))
763 		return ERR_CAST(abo);
764 
765 	abo->type = AMDXDNA_BO_CMD;
766 	abo->client = filp->driver_priv;
767 
768 	ret = amdxdna_gem_obj_vmap(abo, &abo->mem.kva);
769 	if (ret) {
770 		XDNA_ERR(xdna, "Vmap cmd bo failed, ret %d", ret);
771 		goto release_obj;
772 	}
773 
774 	return abo;
775 
776 release_obj:
777 	drm_gem_object_put(to_gobj(abo));
778 	return ERR_PTR(ret);
779 }
780 
781 int amdxdna_drm_create_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
782 {
783 	struct amdxdna_dev *xdna = to_xdna_dev(dev);
784 	struct amdxdna_drm_create_bo *args = data;
785 	struct amdxdna_gem_obj *abo;
786 	int ret;
787 
788 	if (args->flags)
789 		return -EINVAL;
790 
791 	XDNA_DBG(xdna, "BO arg type %d vaddr 0x%llx size 0x%llx flags 0x%llx",
792 		 args->type, args->vaddr, args->size, args->flags);
793 	switch (args->type) {
794 	case AMDXDNA_BO_SHMEM:
795 		abo = amdxdna_drm_alloc_shmem(dev, args, filp);
796 		break;
797 	case AMDXDNA_BO_DEV_HEAP:
798 		abo = amdxdna_drm_create_dev_heap(dev, args, filp);
799 		break;
800 	case AMDXDNA_BO_DEV:
801 		abo = amdxdna_drm_alloc_dev_bo(dev, args, filp);
802 		break;
803 	case AMDXDNA_BO_CMD:
804 		abo = amdxdna_drm_create_cmd_bo(dev, args, filp);
805 		break;
806 	default:
807 		return -EINVAL;
808 	}
809 	if (IS_ERR(abo))
810 		return PTR_ERR(abo);
811 
812 	/* ready to publish object to userspace */
813 	ret = drm_gem_handle_create(filp, to_gobj(abo), &args->handle);
814 	if (ret) {
815 		XDNA_ERR(xdna, "Create handle failed");
816 		goto put_obj;
817 	}
818 
819 	XDNA_DBG(xdna, "BO hdl %d type %d userptr 0x%llx xdna_addr 0x%llx size 0x%lx",
820 		 args->handle, args->type, abo->mem.userptr,
821 		 abo->mem.dev_addr, abo->mem.size);
822 put_obj:
823 	/* Dereference object reference. Handle holds it now. */
824 	drm_gem_object_put(to_gobj(abo));
825 	return ret;
826 }
827 
828 int amdxdna_gem_pin_nolock(struct amdxdna_gem_obj *abo)
829 {
830 	struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev);
831 	int ret;
832 
833 	if (abo->type == AMDXDNA_BO_DEV)
834 		abo = abo->client->dev_heap;
835 
836 	if (is_import_bo(abo))
837 		return 0;
838 
839 	ret = drm_gem_shmem_pin(&abo->base);
840 
841 	XDNA_DBG(xdna, "BO type %d ret %d", abo->type, ret);
842 	return ret;
843 }
844 
845 int amdxdna_gem_pin(struct amdxdna_gem_obj *abo)
846 {
847 	int ret;
848 
849 	mutex_lock(&abo->lock);
850 	ret = amdxdna_gem_pin_nolock(abo);
851 	mutex_unlock(&abo->lock);
852 
853 	return ret;
854 }
855 
856 void amdxdna_gem_unpin(struct amdxdna_gem_obj *abo)
857 {
858 	if (abo->type == AMDXDNA_BO_DEV)
859 		abo = abo->client->dev_heap;
860 
861 	if (is_import_bo(abo))
862 		return;
863 
864 	mutex_lock(&abo->lock);
865 	drm_gem_shmem_unpin(&abo->base);
866 	mutex_unlock(&abo->lock);
867 }
868 
869 struct amdxdna_gem_obj *amdxdna_gem_get_obj(struct amdxdna_client *client,
870 					    u32 bo_hdl, u8 bo_type)
871 {
872 	struct amdxdna_dev *xdna = client->xdna;
873 	struct amdxdna_gem_obj *abo;
874 	struct drm_gem_object *gobj;
875 
876 	gobj = drm_gem_object_lookup(client->filp, bo_hdl);
877 	if (!gobj) {
878 		XDNA_DBG(xdna, "Can not find bo %d", bo_hdl);
879 		return NULL;
880 	}
881 
882 	abo = to_xdna_obj(gobj);
883 	if (bo_type == AMDXDNA_BO_INVALID || abo->type == bo_type)
884 		return abo;
885 
886 	drm_gem_object_put(gobj);
887 	return NULL;
888 }
889 
890 int amdxdna_drm_get_bo_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
891 {
892 	struct amdxdna_drm_get_bo_info *args = data;
893 	struct amdxdna_dev *xdna = to_xdna_dev(dev);
894 	struct amdxdna_gem_obj *abo;
895 	struct drm_gem_object *gobj;
896 	int ret = 0;
897 
898 	if (args->ext || args->ext_flags || args->pad)
899 		return -EINVAL;
900 
901 	gobj = drm_gem_object_lookup(filp, args->handle);
902 	if (!gobj) {
903 		XDNA_DBG(xdna, "Lookup GEM object %d failed", args->handle);
904 		return -ENOENT;
905 	}
906 
907 	abo = to_xdna_obj(gobj);
908 	args->vaddr = abo->mem.userptr;
909 	args->xdna_addr = abo->mem.dev_addr;
910 
911 	if (abo->type != AMDXDNA_BO_DEV)
912 		args->map_offset = drm_vma_node_offset_addr(&gobj->vma_node);
913 	else
914 		args->map_offset = AMDXDNA_INVALID_ADDR;
915 
916 	XDNA_DBG(xdna, "BO hdl %d map_offset 0x%llx vaddr 0x%llx xdna_addr 0x%llx",
917 		 args->handle, args->map_offset, args->vaddr, args->xdna_addr);
918 
919 	drm_gem_object_put(gobj);
920 	return ret;
921 }
922 
923 /*
924  * The sync bo ioctl is to make sure the CPU cache is in sync with memory.
925  * This is required because NPU is not cache coherent device. CPU cache
926  * flushing/invalidation is expensive so it is best to handle this outside
927  * of the command submission path. This ioctl allows explicit cache
928  * flushing/invalidation outside of the critical path.
929  */
930 int amdxdna_drm_sync_bo_ioctl(struct drm_device *dev,
931 			      void *data, struct drm_file *filp)
932 {
933 	struct amdxdna_dev *xdna = to_xdna_dev(dev);
934 	struct amdxdna_drm_sync_bo *args = data;
935 	struct amdxdna_gem_obj *abo;
936 	struct drm_gem_object *gobj;
937 	int ret;
938 
939 	gobj = drm_gem_object_lookup(filp, args->handle);
940 	if (!gobj) {
941 		XDNA_ERR(xdna, "Lookup GEM object failed");
942 		return -ENOENT;
943 	}
944 	abo = to_xdna_obj(gobj);
945 
946 	ret = amdxdna_gem_pin(abo);
947 	if (ret) {
948 		XDNA_ERR(xdna, "Pin BO %d failed, ret %d", args->handle, ret);
949 		goto put_obj;
950 	}
951 
952 	if (is_import_bo(abo))
953 		drm_clflush_sg(abo->base.sgt);
954 	else if (abo->mem.kva)
955 		drm_clflush_virt_range(abo->mem.kva + args->offset, args->size);
956 	else if (abo->base.pages)
957 		drm_clflush_pages(abo->base.pages, gobj->size >> PAGE_SHIFT);
958 	else
959 		drm_WARN(&xdna->ddev, 1, "Can not get flush memory");
960 
961 	amdxdna_gem_unpin(abo);
962 
963 	XDNA_DBG(xdna, "Sync bo %d offset 0x%llx, size 0x%llx\n",
964 		 args->handle, args->offset, args->size);
965 
966 	if (args->direction == SYNC_DIRECT_FROM_DEVICE)
967 		ret = amdxdna_hwctx_sync_debug_bo(abo->client, args->handle);
968 
969 put_obj:
970 	drm_gem_object_put(gobj);
971 	return ret;
972 }
973