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