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
amdxdna_init_dev_bo(struct amdxdna_gem_obj * dev_bo)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
amdxdna_gem_heap_alloc(struct amdxdna_gem_obj * abo)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
amdxdna_gem_heap_free(struct amdxdna_gem_obj * abo)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 *
amdxdna_gem_create_obj(struct drm_device * dev,size_t size)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
amdxdna_gem_destroy_obj(struct amdxdna_gem_obj * abo)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 */
amdxdna_gem_vmap(struct amdxdna_gem_obj * abo)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 */
amdxdna_gem_vunmap(struct amdxdna_gem_obj * abo)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 */
amdxdna_gem_dev_addr(struct amdxdna_gem_obj * abo)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
amdxdna_hmm_invalidate(struct mmu_interval_notifier * mni,const struct mmu_notifier_range * range,unsigned long cur_seq)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
compare_range(struct amdxdna_umap * mapp,struct mm_struct * mm,unsigned long start,unsigned long end)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
amdxdna_hmm_unregister(struct amdxdna_gem_obj * abo,struct vm_area_struct * vma)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
amdxdna_umap_release(struct kref * ref)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
amdxdna_umap_put(struct amdxdna_umap * mapp)335 void amdxdna_umap_put(struct amdxdna_umap *mapp)
336 {
337 kref_put(&mapp->refcnt, amdxdna_umap_release);
338 }
339
amdxdna_hmm_unreg_work(struct work_struct * work)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
amdxdna_hmm_register(struct amdxdna_gem_obj * abo,struct vm_area_struct * vma)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
amdxdna_gem_dev_obj_free(struct drm_gem_object * gobj)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
amdxdna_mark_mapp_invalid(struct amdxdna_gem_obj * abo,struct vm_area_struct * vma)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
amdxdna_insert_pages(struct amdxdna_gem_obj * abo,struct vm_area_struct * vma)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
amdxdna_gem_obj_mmap(struct drm_gem_object * gobj,struct vm_area_struct * vma)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
amdxdna_gem_dmabuf_mmap(struct dma_buf * dma_buf,struct vm_area_struct * vma)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
amdxdna_gem_prime_export(struct drm_gem_object * gobj,int flags)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
amdxdna_imported_obj_free(struct amdxdna_gem_obj * abo)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
amdxdna_gem_skip_bo_usage(struct amdxdna_gem_obj * abo)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
amdxdna_gem_add_bo_usage(struct amdxdna_gem_obj * abo)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
amdxdna_gem_del_bo_usage(struct amdxdna_gem_obj * abo)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
amdxdna_gem_obj_free(struct drm_gem_object * gobj)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
amdxdna_gem_obj_open(struct drm_gem_object * gobj,struct drm_file * filp)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
amdxdna_gem_obj_close(struct drm_gem_object * gobj,struct drm_file * filp)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
amdxdna_gem_obj_vmap(struct drm_gem_object * obj,struct iosys_map * map)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
amdxdna_gem_obj_vunmap(struct drm_gem_object * obj,struct iosys_map * map)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
amdxdna_gem_dev_obj_vmap(struct drm_gem_object * obj,struct iosys_map * map)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 const struct drm_gem_object_funcs amdxdna_gem_dev_obj_funcs = {
762 .free = amdxdna_gem_dev_obj_free,
763 .vmap = amdxdna_gem_dev_obj_vmap,
764 };
765
766 static const struct drm_gem_object_funcs amdxdna_gem_shmem_funcs = {
767 .free = amdxdna_gem_obj_free,
768 .open = amdxdna_gem_obj_open,
769 .close = amdxdna_gem_obj_close,
770 .print_info = drm_gem_shmem_object_print_info,
771 .pin = drm_gem_shmem_object_pin,
772 .unpin = drm_gem_shmem_object_unpin,
773 .get_sg_table = drm_gem_shmem_object_get_sg_table,
774 .vmap = amdxdna_gem_obj_vmap,
775 .vunmap = amdxdna_gem_obj_vunmap,
776 .mmap = amdxdna_gem_obj_mmap,
777 .vm_ops = &drm_gem_shmem_vm_ops,
778 .export = amdxdna_gem_prime_export,
779 };
780
781 /* For drm_driver->gem_create_object callback */
782 struct drm_gem_object *
amdxdna_gem_create_shmem_object_cb(struct drm_device * dev,size_t size)783 amdxdna_gem_create_shmem_object_cb(struct drm_device *dev, size_t size)
784 {
785 struct amdxdna_gem_obj *abo;
786
787 abo = amdxdna_gem_create_obj(dev, size);
788 if (IS_ERR(abo))
789 return ERR_CAST(abo);
790
791 to_gobj(abo)->funcs = &amdxdna_gem_shmem_funcs;
792
793 return to_gobj(abo);
794 }
795
796 static struct amdxdna_gem_obj *
amdxdna_gem_create_shmem_object(struct drm_device * dev,struct amdxdna_drm_create_bo * args)797 amdxdna_gem_create_shmem_object(struct drm_device *dev, struct amdxdna_drm_create_bo *args)
798 {
799 size_t size = args->size;
800 struct drm_gem_shmem_object *shmem = drm_gem_shmem_create(dev, size);
801
802 if (IS_ERR(shmem))
803 return ERR_CAST(shmem);
804
805 shmem->map_wc = false;
806 return to_xdna_obj(&shmem->base);
807 }
808
809 static struct amdxdna_gem_obj *
amdxdna_gem_create_ubuf_object(struct drm_device * dev,struct amdxdna_drm_create_bo * args)810 amdxdna_gem_create_ubuf_object(struct drm_device *dev, struct amdxdna_drm_create_bo *args)
811 {
812 struct amdxdna_dev *xdna = to_xdna_dev(dev);
813 struct amdxdna_drm_va_tbl va_tbl;
814 struct amdxdna_gem_obj *abo;
815 struct drm_gem_object *gobj;
816 struct dma_buf *dma_buf;
817
818 if (copy_from_user(&va_tbl, u64_to_user_ptr(args->vaddr), sizeof(va_tbl))) {
819 XDNA_DBG(xdna, "Access va table failed");
820 return ERR_PTR(-EINVAL);
821 }
822
823 if (va_tbl.num_entries) {
824 dma_buf = amdxdna_get_ubuf(dev, va_tbl.num_entries,
825 u64_to_user_ptr(args->vaddr + sizeof(va_tbl)));
826 } else {
827 dma_buf = dma_buf_get(va_tbl.dmabuf_fd);
828 }
829
830 if (IS_ERR(dma_buf))
831 return ERR_CAST(dma_buf);
832
833 gobj = amdxdna_gem_prime_import(dev, dma_buf);
834 if (IS_ERR(gobj)) {
835 dma_buf_put(dma_buf);
836 return ERR_CAST(gobj);
837 }
838
839 dma_buf_put(dma_buf);
840
841 abo = to_xdna_obj(gobj);
842 abo->private_buffer = true;
843
844 return abo;
845 }
846
847 static struct amdxdna_gem_obj *
amdxdna_gem_create_cbuf_object(struct drm_device * dev,struct amdxdna_drm_create_bo * args)848 amdxdna_gem_create_cbuf_object(struct drm_device *dev, struct amdxdna_drm_create_bo *args)
849 {
850 struct amdxdna_dev *xdna = to_xdna_dev(dev);
851 size_t size = PAGE_ALIGN(args->size);
852 struct drm_gem_object *gobj;
853 struct amdxdna_gem_obj *ret;
854 struct dma_buf *dma_buf;
855 u64 align;
856
857 if (!size) {
858 XDNA_ERR(xdna, "Invalid BO size 0x%llx", args->size);
859 return ERR_PTR(-EINVAL);
860 }
861
862 align = (args->type == AMDXDNA_BO_DEV_HEAP) ? xdna->dev_info->dev_mem_size : 0;
863 dma_buf = amdxdna_get_cbuf(dev, size, align);
864 if (IS_ERR(dma_buf))
865 return ERR_CAST(dma_buf);
866
867 gobj = amdxdna_gem_prime_import(dev, dma_buf);
868 if (IS_ERR(gobj))
869 ret = ERR_CAST(gobj);
870 else
871 ret = to_xdna_obj(gobj);
872
873 dma_buf_put(dma_buf);
874 return ret;
875 }
876
877 struct drm_gem_object *
amdxdna_gem_prime_import(struct drm_device * dev,struct dma_buf * dma_buf)878 amdxdna_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf)
879 {
880 struct dma_buf_attachment *attach;
881 struct amdxdna_gem_obj *abo;
882 struct drm_gem_object *gobj;
883 struct sg_table *sgt;
884 int ret;
885
886 get_dma_buf(dma_buf);
887
888 attach = dma_buf_attach(dma_buf, dev->dev);
889 if (IS_ERR(attach)) {
890 ret = PTR_ERR(attach);
891 goto put_buf;
892 }
893
894 sgt = dma_buf_map_attachment_unlocked(attach, DMA_BIDIRECTIONAL);
895 if (IS_ERR(sgt)) {
896 ret = PTR_ERR(sgt);
897 goto fail_detach;
898 }
899
900 gobj = drm_gem_shmem_prime_import_sg_table(dev, attach, sgt);
901 if (IS_ERR(gobj)) {
902 ret = PTR_ERR(gobj);
903 goto fail_unmap;
904 }
905
906 abo = to_xdna_obj(gobj);
907 abo->attach = attach;
908 abo->dma_buf = dma_buf;
909 abo->type = AMDXDNA_BO_SHARE;
910 gobj->resv = dma_buf->resv;
911
912 return gobj;
913
914 fail_unmap:
915 dma_buf_unmap_attachment_unlocked(attach, sgt, DMA_BIDIRECTIONAL);
916 fail_detach:
917 dma_buf_detach(dma_buf, attach);
918 put_buf:
919 dma_buf_put(dma_buf);
920
921 return ERR_PTR(ret);
922 }
923
924 static struct amdxdna_gem_obj *
amdxdna_drm_create_share_bo(struct drm_device * dev,struct amdxdna_drm_create_bo * args,struct drm_file * filp)925 amdxdna_drm_create_share_bo(struct drm_device *dev,
926 struct amdxdna_drm_create_bo *args, struct drm_file *filp)
927 {
928 struct amdxdna_gem_obj *abo;
929
930 if (args->vaddr)
931 abo = amdxdna_gem_create_ubuf_object(dev, args);
932 else if (amdxdna_use_carveout(to_xdna_dev(dev)))
933 abo = amdxdna_gem_create_cbuf_object(dev, args);
934 else
935 abo = amdxdna_gem_create_shmem_object(dev, args);
936 if (IS_ERR(abo))
937 return ERR_CAST(abo);
938
939 if (args->type == AMDXDNA_BO_DEV_HEAP) {
940 abo->type = AMDXDNA_BO_DEV_HEAP;
941 abo->internal = true;
942 } else {
943 abo->type = AMDXDNA_BO_SHARE;
944 abo->internal = args->type == AMDXDNA_BO_CMD;
945 }
946
947 return abo;
948 }
949
950 static struct amdxdna_gem_obj *
amdxdna_drm_create_dev_heap_bo(struct drm_device * dev,struct amdxdna_drm_create_bo * args,struct drm_file * filp)951 amdxdna_drm_create_dev_heap_bo(struct drm_device *dev,
952 struct amdxdna_drm_create_bo *args, struct drm_file *filp)
953 {
954 struct amdxdna_client *client = filp->driver_priv;
955 struct amdxdna_dev *xdna = to_xdna_dev(dev);
956 struct amdxdna_gem_obj *abo;
957 int ret;
958
959 WARN_ON(!is_power_of_2(xdna->dev_info->dev_mem_size));
960 XDNA_DBG(xdna, "Requested dev heap size 0x%llx", args->size);
961 if (!args->size || !IS_ALIGNED(args->size, xdna->dev_info->dev_mem_size)) {
962 XDNA_ERR(xdna, "The dev heap size 0x%llx is not multiple of 0x%lx",
963 args->size, xdna->dev_info->dev_mem_size);
964 return ERR_PTR(-EINVAL);
965 }
966
967 /* HEAP BO is a special case of SHARE BO. */
968 abo = amdxdna_drm_create_share_bo(dev, args, filp);
969 if (IS_ERR(abo))
970 return ERR_CAST(abo);
971
972 /* Set up heap for this client. */
973 mutex_lock(&client->mm_lock);
974
975 if (!xdna->dev_info->ops->hwctx_heap_expand &&
976 client->dev_heap_nid > 0) {
977 XDNA_ERR(xdna, "Heap expansion is not supported");
978 ret = -EOPNOTSUPP;
979 goto mm_unlock;
980 }
981
982 if (client->total_heap_size + abo->mem.size >
983 xdna->dev_info->dev_heap_max_size) {
984 XDNA_ERR(xdna, "Heap size 0x%lx + 0x%lx exceeds max 0x%lx",
985 client->total_heap_size, abo->mem.size,
986 xdna->dev_info->dev_heap_max_size);
987 ret = -ENOSPC;
988 goto mm_unlock;
989 }
990
991 ret = xa_insert(&client->dev_heap_xa, client->dev_heap_nid, abo, GFP_KERNEL);
992 if (ret) {
993 XDNA_ERR(xdna, "Add heap failed %d", ret);
994 goto mm_unlock;
995 }
996
997 abo->dev_addr = xdna->dev_info->dev_mem_base + client->total_heap_size;
998 client->total_heap_size += abo->mem.size;
999 client->dev_heap_nid++;
1000 drm_gem_object_get(to_gobj(abo));
1001
1002 mutex_unlock(&client->mm_lock);
1003
1004 return abo;
1005
1006 mm_unlock:
1007 mutex_unlock(&client->mm_lock);
1008 drm_gem_object_put(to_gobj(abo));
1009 return ERR_PTR(ret);
1010 }
1011
1012 struct amdxdna_gem_obj *
amdxdna_drm_create_dev_bo(struct drm_device * dev,struct amdxdna_drm_create_bo * args,struct drm_file * filp)1013 amdxdna_drm_create_dev_bo(struct drm_device *dev,
1014 struct amdxdna_drm_create_bo *args, struct drm_file *filp)
1015 {
1016 size_t aligned_sz = PAGE_ALIGN(args->size);
1017 struct amdxdna_client *client = filp->driver_priv;
1018 struct amdxdna_dev *xdna = to_xdna_dev(dev);
1019 struct amdxdna_gem_obj *abo;
1020 struct drm_gem_object *gobj;
1021 int ret;
1022
1023 if (!aligned_sz) {
1024 XDNA_ERR(xdna, "Invalid BO size 0x%llx", args->size);
1025 return ERR_PTR(-EINVAL);
1026 }
1027
1028 abo = amdxdna_gem_create_obj(dev, aligned_sz);
1029 if (IS_ERR(abo))
1030 return abo;
1031 gobj = to_gobj(abo);
1032 gobj->funcs = &amdxdna_gem_dev_obj_funcs;
1033 abo->type = AMDXDNA_BO_DEV;
1034 abo->internal = true;
1035 /*
1036 * DEV BOs cannot be alive when client is gone, it's OK to
1037 * always establish the connection.
1038 */
1039 abo->client = client;
1040
1041 ret = amdxdna_gem_heap_alloc(abo);
1042 if (ret) {
1043 amdxdna_gem_destroy_obj(abo);
1044 return ERR_PTR(ret);
1045 }
1046
1047 drm_gem_private_object_init(dev, gobj, aligned_sz);
1048
1049 return abo;
1050 }
1051
amdxdna_drm_create_bo_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)1052 int amdxdna_drm_create_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
1053 {
1054 struct amdxdna_client *client = filp->driver_priv;
1055 struct amdxdna_dev *xdna = to_xdna_dev(dev);
1056 struct amdxdna_drm_create_bo *args = data;
1057 struct amdxdna_gem_obj *abo;
1058 int ret;
1059
1060 if (args->flags)
1061 return -EINVAL;
1062
1063 XDNA_DBG(xdna, "BO arg type %d vaddr 0x%llx size 0x%llx flags 0x%llx",
1064 args->type, args->vaddr, args->size, args->flags);
1065 switch (args->type) {
1066 case AMDXDNA_BO_CMD:
1067 case AMDXDNA_BO_SHARE:
1068 abo = amdxdna_drm_create_share_bo(dev, args, filp);
1069 break;
1070 case AMDXDNA_BO_DEV_HEAP:
1071 abo = amdxdna_drm_create_dev_heap_bo(dev, args, filp);
1072 break;
1073 case AMDXDNA_BO_DEV:
1074 abo = amdxdna_drm_create_dev_bo(dev, args, filp);
1075 if (!IS_ERR(abo)) {
1076 mutex_lock(&xdna->dev_lock);
1077 ret = amdxdna_update_heap(client, NULL);
1078 mutex_unlock(&xdna->dev_lock);
1079 if (ret)
1080 goto put_obj;
1081 }
1082 break;
1083 default:
1084 return -EINVAL;
1085 }
1086 if (IS_ERR(abo))
1087 return PTR_ERR(abo);
1088
1089 /* Ready to publish object to userspace and count for BO usage. */
1090 ret = drm_gem_handle_create(filp, to_gobj(abo), &args->handle);
1091 if (ret) {
1092 XDNA_ERR(xdna, "Create handle failed");
1093 goto put_obj;
1094 }
1095
1096 XDNA_DBG(xdna, "BO hdl %d type %d userptr 0x%llx xdna_addr 0x%llx size 0x%lx",
1097 args->handle, args->type, amdxdna_gem_uva(abo),
1098 amdxdna_gem_dev_addr(abo), abo->mem.size);
1099 put_obj:
1100 /* Dereference object reference. Handle holds it now. */
1101 drm_gem_object_put(to_gobj(abo));
1102 return ret;
1103 }
1104
amdxdna_bo_pin(struct amdxdna_gem_obj * abo)1105 static int amdxdna_bo_pin(struct amdxdna_gem_obj *abo)
1106 {
1107 struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev);
1108 int ret;
1109
1110 if (is_import_bo(abo))
1111 return 0;
1112
1113 ret = drm_gem_shmem_pin(&abo->base);
1114
1115 XDNA_DBG(xdna, "BO type %d ret %d", abo->type, ret);
1116 return ret;
1117 }
1118
amdxdna_bo_unpin(struct amdxdna_gem_obj * abo)1119 static void amdxdna_bo_unpin(struct amdxdna_gem_obj *abo)
1120 {
1121 struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev);
1122
1123 if (is_import_bo(abo))
1124 return;
1125
1126 drm_gem_shmem_unpin(&abo->base);
1127
1128 XDNA_DBG(xdna, "BO type %d", abo->type);
1129 }
1130
amdxdna_gem_pin_nolock(struct amdxdna_gem_obj * abo)1131 int amdxdna_gem_pin_nolock(struct amdxdna_gem_obj *abo)
1132 {
1133 struct amdxdna_client *client = abo->client;
1134 struct amdxdna_gem_obj *heap;
1135 unsigned long heap_id, last = ULONG_MAX;
1136 int ret = 0;
1137
1138 if (abo->type != AMDXDNA_BO_DEV)
1139 return amdxdna_bo_pin(abo);
1140
1141 xa_for_each_range(&client->dev_heap_xa, heap_id, heap,
1142 abo->heap_start_id, abo->heap_end_id) {
1143 ret = amdxdna_bo_pin(heap);
1144 if (ret)
1145 break;
1146 last = heap_id;
1147 }
1148
1149 if (ret && last <= abo->heap_end_id) {
1150 xa_for_each_range(&client->dev_heap_xa, heap_id, heap,
1151 abo->heap_start_id, last)
1152 amdxdna_bo_unpin(heap);
1153 }
1154
1155 return ret;
1156 }
1157
amdxdna_gem_pin(struct amdxdna_gem_obj * abo)1158 int amdxdna_gem_pin(struct amdxdna_gem_obj *abo)
1159 {
1160 int ret;
1161
1162 mutex_lock(&abo->lock);
1163 ret = amdxdna_gem_pin_nolock(abo);
1164 mutex_unlock(&abo->lock);
1165
1166 return ret;
1167 }
1168
amdxdna_gem_unpin(struct amdxdna_gem_obj * abo)1169 void amdxdna_gem_unpin(struct amdxdna_gem_obj *abo)
1170 {
1171 mutex_lock(&abo->lock);
1172 if (abo->type == AMDXDNA_BO_DEV) {
1173 struct amdxdna_gem_obj *heap;
1174 unsigned long heap_id;
1175
1176 xa_for_each_range(&abo->client->dev_heap_xa, heap_id, heap,
1177 abo->heap_start_id, abo->heap_end_id)
1178 amdxdna_bo_unpin(heap);
1179 } else {
1180 amdxdna_bo_unpin(abo);
1181 }
1182
1183 mutex_unlock(&abo->lock);
1184 }
1185
amdxdna_gem_get_obj(struct amdxdna_client * client,u32 bo_hdl,u8 bo_type)1186 struct amdxdna_gem_obj *amdxdna_gem_get_obj(struct amdxdna_client *client,
1187 u32 bo_hdl, u8 bo_type)
1188 {
1189 struct amdxdna_gem_obj *abo;
1190 struct drm_gem_object *gobj;
1191
1192 gobj = drm_gem_object_lookup(client->filp, bo_hdl);
1193 if (!gobj) {
1194 XDNA_DBG(client->xdna, "Can not find bo %d", bo_hdl);
1195 return NULL;
1196 }
1197
1198 abo = to_xdna_obj(gobj);
1199 if (bo_type == AMDXDNA_BO_INVALID || abo->type == bo_type)
1200 return abo;
1201
1202 drm_gem_object_put(gobj);
1203 return NULL;
1204 }
1205
amdxdna_drm_get_bo_info_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)1206 int amdxdna_drm_get_bo_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
1207 {
1208 struct amdxdna_drm_get_bo_info *args = data;
1209 struct amdxdna_dev *xdna = to_xdna_dev(dev);
1210 struct amdxdna_gem_obj *abo;
1211 struct drm_gem_object *gobj;
1212 int ret = 0;
1213
1214 if (args->ext || args->ext_flags || args->pad)
1215 return -EINVAL;
1216
1217 gobj = drm_gem_object_lookup(filp, args->handle);
1218 if (!gobj) {
1219 XDNA_DBG(xdna, "Lookup GEM object %d failed", args->handle);
1220 return -ENOENT;
1221 }
1222
1223 abo = to_xdna_obj(gobj);
1224 args->vaddr = amdxdna_gem_uva(abo);
1225 args->xdna_addr = amdxdna_gem_dev_addr(abo);
1226
1227 if (abo->type != AMDXDNA_BO_DEV)
1228 args->map_offset = drm_vma_node_offset_addr(&gobj->vma_node);
1229 else
1230 args->map_offset = AMDXDNA_INVALID_ADDR;
1231
1232 XDNA_DBG(xdna, "BO hdl %d map_offset 0x%llx vaddr 0x%llx xdna_addr 0x%llx",
1233 args->handle, args->map_offset, args->vaddr, args->xdna_addr);
1234
1235 drm_gem_object_put(gobj);
1236 return ret;
1237 }
1238
amdxdna_flush_bo(struct amdxdna_gem_obj * abo,u64 offset,u64 size)1239 static int amdxdna_flush_bo(struct amdxdna_gem_obj *abo, u64 offset, u64 size)
1240 {
1241 u64 end;
1242
1243 if (offset >= abo->mem.size)
1244 return -EINVAL;
1245
1246 if (check_add_overflow(offset, size, &end))
1247 return -EINVAL;
1248
1249 size = min(abo->mem.size, end) - offset;
1250 if (is_import_bo(abo))
1251 drm_clflush_sg(abo->base.sgt);
1252 else if (amdxdna_gem_vmap(abo))
1253 drm_clflush_virt_range(amdxdna_gem_vmap(abo) + offset, size);
1254 else if (abo->base.pages)
1255 drm_clflush_pages(abo->base.pages, abo->mem.size >> PAGE_SHIFT);
1256 else
1257 return -EINVAL;
1258
1259 return 0;
1260 }
1261
1262 /*
1263 * The sync bo ioctl is to make sure the CPU cache is in sync with memory.
1264 * This is required because NPU is not cache coherent device. CPU cache
1265 * flushing/invalidation is expensive so it is best to handle this outside
1266 * of the command submission path. This ioctl allows explicit cache
1267 * flushing/invalidation outside of the critical path.
1268 */
amdxdna_drm_sync_bo_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)1269 int amdxdna_drm_sync_bo_ioctl(struct drm_device *dev,
1270 void *data, struct drm_file *filp)
1271 {
1272 struct amdxdna_client *client = filp->driver_priv;
1273 struct amdxdna_dev *xdna = to_xdna_dev(dev);
1274 struct amdxdna_drm_sync_bo *args = data;
1275 struct amdxdna_gem_obj *abo;
1276 struct drm_gem_object *gobj;
1277 int ret = 0;
1278
1279 gobj = drm_gem_object_lookup(filp, args->handle);
1280 if (!gobj) {
1281 XDNA_ERR(xdna, "Lookup GEM object failed");
1282 return -ENOENT;
1283 }
1284 abo = to_xdna_obj(gobj);
1285
1286 if (abo->type == AMDXDNA_BO_DEV) {
1287 struct amdxdna_gem_obj *heap;
1288 unsigned long heap_id;
1289 u64 bo_start = amdxdna_gem_dev_addr(abo);
1290 u64 flush_start = bo_start + args->offset;
1291 u64 flush_end = flush_start + args->size;
1292
1293 xa_for_each_range(&client->dev_heap_xa, heap_id, heap,
1294 abo->heap_start_id, abo->heap_end_id) {
1295 u64 heap_start = amdxdna_gem_dev_addr(heap);
1296 u64 heap_end = heap_start + heap->mem.size;
1297 u64 start = max(flush_start, heap_start);
1298 u64 end = min(flush_end, heap_end);
1299
1300 if (start >= end)
1301 continue;
1302
1303 ret = amdxdna_flush_bo(heap, start - heap_start, end - start);
1304 if (ret) {
1305 XDNA_ERR(xdna, "Failed to flush heap %ld ret %d",
1306 heap_id, ret);
1307 goto put_obj;
1308 }
1309 }
1310 } else {
1311 ret = amdxdna_gem_pin(abo);
1312 if (ret) {
1313 XDNA_ERR(xdna, "Pin BO %d failed, ret %d", args->handle, ret);
1314 goto put_obj;
1315 }
1316
1317 ret = amdxdna_flush_bo(abo, args->offset, args->size);
1318 amdxdna_gem_unpin(abo);
1319
1320 if (ret) {
1321 drm_WARN(&xdna->ddev, 1, "Can not get flush memory");
1322 goto put_obj;
1323 }
1324 }
1325
1326 XDNA_DBG(xdna, "Sync bo %d offset 0x%llx, size 0x%llx\n",
1327 args->handle, args->offset, args->size);
1328
1329 if (args->direction == SYNC_DIRECT_FROM_DEVICE)
1330 ret = amdxdna_hwctx_sync_debug_bo(client, args->handle);
1331
1332 put_obj:
1333 drm_gem_object_put(gobj);
1334 return ret;
1335 }
1336
amdxdna_drm_get_bo_usage(struct drm_device * dev,struct amdxdna_drm_get_array * args)1337 int amdxdna_drm_get_bo_usage(struct drm_device *dev, struct amdxdna_drm_get_array *args)
1338 {
1339 size_t min_sz = min(args->element_size, sizeof(struct amdxdna_drm_bo_usage));
1340 char __user *buf = u64_to_user_ptr(args->buffer);
1341 struct amdxdna_dev *xdna = to_xdna_dev(dev);
1342 struct amdxdna_client *tmp_client;
1343 struct amdxdna_drm_bo_usage tmp;
1344
1345 drm_WARN_ON(dev, !mutex_is_locked(&xdna->dev_lock));
1346
1347 if (args->num_element != 1)
1348 return -EINVAL;
1349
1350 if (copy_from_user(&tmp, buf, min_sz))
1351 return -EFAULT;
1352
1353 if (!tmp.pid)
1354 return -EINVAL;
1355
1356 tmp.total_usage = 0;
1357 tmp.internal_usage = 0;
1358 tmp.heap_usage = 0;
1359
1360 list_for_each_entry(tmp_client, &xdna->client_list, node) {
1361 if (tmp.pid != tmp_client->pid)
1362 continue;
1363
1364 mutex_lock(&tmp_client->mm_lock);
1365 tmp.total_usage += tmp_client->total_bo_usage;
1366 tmp.internal_usage += tmp_client->total_int_bo_usage;
1367 tmp.heap_usage += tmp_client->heap_usage;
1368 mutex_unlock(&tmp_client->mm_lock);
1369 }
1370
1371 if (copy_to_user(buf, &tmp, min_sz))
1372 return -EFAULT;
1373
1374 return 0;
1375 }
1376