xref: /linux/drivers/accel/ivpu/ivpu_gem.c (revision 74ba587f402d5501af2c85e50cf1e4044263b6ca)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2020-2023 Intel Corporation
4  */
5 
6 #include <linux/dma-buf.h>
7 #include <linux/highmem.h>
8 #include <linux/module.h>
9 #include <linux/set_memory.h>
10 #include <linux/xarray.h>
11 
12 #include <drm/drm_cache.h>
13 #include <drm/drm_debugfs.h>
14 #include <drm/drm_file.h>
15 #include <drm/drm_utils.h>
16 
17 #include "ivpu_drv.h"
18 #include "ivpu_fw.h"
19 #include "ivpu_gem.h"
20 #include "ivpu_hw.h"
21 #include "ivpu_mmu.h"
22 #include "ivpu_mmu_context.h"
23 
24 MODULE_IMPORT_NS("DMA_BUF");
25 
26 static const struct drm_gem_object_funcs ivpu_gem_funcs;
27 
28 static inline void ivpu_dbg_bo(struct ivpu_device *vdev, struct ivpu_bo *bo, const char *action)
29 {
30 	ivpu_dbg(vdev, BO,
31 		 "%6s: bo %8p size %9zu ctx %d vpu_addr %9llx pages %d sgt %d mmu_mapped %d wc %d imported %d\n",
32 		 action, bo, ivpu_bo_size(bo), bo->ctx_id, bo->vpu_addr,
33 		 (bool)bo->base.pages, (bool)bo->base.sgt, bo->mmu_mapped, bo->base.map_wc,
34 		 (bool)drm_gem_is_imported(&bo->base.base));
35 }
36 
37 static inline int ivpu_bo_lock(struct ivpu_bo *bo)
38 {
39 	return dma_resv_lock(bo->base.base.resv, NULL);
40 }
41 
42 static inline void ivpu_bo_unlock(struct ivpu_bo *bo)
43 {
44 	dma_resv_unlock(bo->base.base.resv);
45 }
46 
47 static struct sg_table *ivpu_bo_map_attachment(struct ivpu_device *vdev, struct ivpu_bo *bo)
48 {
49 	struct sg_table *sgt;
50 
51 	drm_WARN_ON(&vdev->drm, !bo->base.base.import_attach);
52 
53 	ivpu_bo_lock(bo);
54 
55 	sgt = bo->base.sgt;
56 	if (!sgt) {
57 		sgt = dma_buf_map_attachment(bo->base.base.import_attach, DMA_BIDIRECTIONAL);
58 		if (IS_ERR(sgt))
59 			ivpu_err(vdev, "Failed to map BO in IOMMU: %ld\n", PTR_ERR(sgt));
60 		else
61 			bo->base.sgt = sgt;
62 	}
63 
64 	ivpu_bo_unlock(bo);
65 
66 	return sgt;
67 }
68 
69 /*
70  * ivpu_bo_bind() - pin the backing physical pages and map them to VPU.
71  *
72  * This function pins physical memory pages, then maps the physical pages
73  * to IOMMU address space and finally updates the VPU MMU page tables
74  * to allow the VPU to translate VPU address to IOMMU address.
75  */
76 int __must_check ivpu_bo_bind(struct ivpu_bo *bo)
77 {
78 	struct ivpu_device *vdev = ivpu_bo_to_vdev(bo);
79 	struct sg_table *sgt;
80 	int ret = 0;
81 
82 	ivpu_dbg_bo(vdev, bo, "bind");
83 
84 	if (bo->base.base.import_attach)
85 		sgt = ivpu_bo_map_attachment(vdev, bo);
86 	else
87 		sgt = drm_gem_shmem_get_pages_sgt(&bo->base);
88 	if (IS_ERR(sgt)) {
89 		ret = PTR_ERR(sgt);
90 		ivpu_err(vdev, "Failed to map BO in IOMMU: %d\n", ret);
91 		return ret;
92 	}
93 
94 	ivpu_bo_lock(bo);
95 
96 	if (!bo->mmu_mapped) {
97 		drm_WARN_ON(&vdev->drm, !bo->ctx);
98 		ret = ivpu_mmu_context_map_sgt(vdev, bo->ctx, bo->vpu_addr, sgt,
99 					       ivpu_bo_is_snooped(bo), ivpu_bo_is_read_only(bo));
100 		if (ret) {
101 			ivpu_err(vdev, "Failed to map BO in MMU: %d\n", ret);
102 			goto unlock;
103 		}
104 		bo->mmu_mapped = true;
105 	}
106 
107 unlock:
108 	ivpu_bo_unlock(bo);
109 
110 	return ret;
111 }
112 
113 static int
114 ivpu_bo_alloc_vpu_addr(struct ivpu_bo *bo, struct ivpu_mmu_context *ctx,
115 		       const struct ivpu_addr_range *range)
116 {
117 	struct ivpu_device *vdev = ivpu_bo_to_vdev(bo);
118 	int idx, ret;
119 
120 	if (!drm_dev_enter(&vdev->drm, &idx))
121 		return -ENODEV;
122 
123 	ivpu_bo_lock(bo);
124 
125 	ret = ivpu_mmu_context_insert_node(ctx, range, ivpu_bo_size(bo), &bo->mm_node);
126 	if (!ret) {
127 		bo->ctx = ctx;
128 		bo->ctx_id = ctx->id;
129 		bo->vpu_addr = bo->mm_node.start;
130 		ivpu_dbg_bo(vdev, bo, "vaddr");
131 	} else {
132 		ivpu_err(vdev, "Failed to add BO to context %u: %d\n", ctx->id, ret);
133 	}
134 
135 	ivpu_bo_unlock(bo);
136 
137 	drm_dev_exit(idx);
138 
139 	return ret;
140 }
141 
142 static void ivpu_bo_unbind_locked(struct ivpu_bo *bo)
143 {
144 	struct ivpu_device *vdev = ivpu_bo_to_vdev(bo);
145 
146 	dma_resv_assert_held(bo->base.base.resv);
147 
148 	if (bo->mmu_mapped) {
149 		drm_WARN_ON(&vdev->drm, !bo->ctx);
150 		drm_WARN_ON(&vdev->drm, !bo->vpu_addr);
151 		drm_WARN_ON(&vdev->drm, !bo->base.sgt);
152 		ivpu_mmu_context_unmap_sgt(vdev, bo->ctx, bo->vpu_addr, bo->base.sgt);
153 		bo->mmu_mapped = false;
154 	}
155 
156 	if (bo->ctx) {
157 		ivpu_mmu_context_remove_node(bo->ctx, &bo->mm_node);
158 		bo->ctx = NULL;
159 	}
160 
161 	if (bo->base.sgt) {
162 		if (bo->base.base.import_attach) {
163 			dma_buf_unmap_attachment(bo->base.base.import_attach,
164 						 bo->base.sgt, DMA_BIDIRECTIONAL);
165 		} else {
166 			dma_unmap_sgtable(vdev->drm.dev, bo->base.sgt, DMA_BIDIRECTIONAL, 0);
167 			sg_free_table(bo->base.sgt);
168 			kfree(bo->base.sgt);
169 		}
170 		bo->base.sgt = NULL;
171 	}
172 }
173 
174 void ivpu_bo_unbind_all_bos_from_context(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx)
175 {
176 	struct ivpu_bo *bo;
177 
178 	if (drm_WARN_ON(&vdev->drm, !ctx))
179 		return;
180 
181 	mutex_lock(&vdev->bo_list_lock);
182 	list_for_each_entry(bo, &vdev->bo_list, bo_list_node) {
183 		ivpu_bo_lock(bo);
184 		if (bo->ctx == ctx) {
185 			ivpu_dbg_bo(vdev, bo, "unbind");
186 			ivpu_bo_unbind_locked(bo);
187 		}
188 		ivpu_bo_unlock(bo);
189 	}
190 	mutex_unlock(&vdev->bo_list_lock);
191 }
192 
193 struct drm_gem_object *ivpu_gem_create_object(struct drm_device *dev, size_t size)
194 {
195 	struct ivpu_bo *bo;
196 
197 	if (size == 0 || !PAGE_ALIGNED(size))
198 		return ERR_PTR(-EINVAL);
199 
200 	bo = kzalloc(sizeof(*bo), GFP_KERNEL);
201 	if (!bo)
202 		return ERR_PTR(-ENOMEM);
203 
204 	bo->base.base.funcs = &ivpu_gem_funcs;
205 	bo->base.pages_mark_dirty_on_put = true; /* VPU can dirty a BO anytime */
206 
207 	INIT_LIST_HEAD(&bo->bo_list_node);
208 
209 	return &bo->base.base;
210 }
211 
212 struct drm_gem_object *ivpu_gem_prime_import(struct drm_device *dev,
213 					     struct dma_buf *dma_buf)
214 {
215 	struct ivpu_device *vdev = to_ivpu_device(dev);
216 	struct device *attach_dev = dev->dev;
217 	struct dma_buf_attachment *attach;
218 	struct drm_gem_object *obj;
219 	struct ivpu_bo *bo;
220 	int ret;
221 
222 	attach = dma_buf_attach(dma_buf, attach_dev);
223 	if (IS_ERR(attach))
224 		return ERR_CAST(attach);
225 
226 	get_dma_buf(dma_buf);
227 
228 	obj = drm_gem_shmem_prime_import_sg_table(dev, attach, NULL);
229 	if (IS_ERR(obj)) {
230 		ret = PTR_ERR(obj);
231 		goto fail_detach;
232 	}
233 
234 	obj->import_attach = attach;
235 	obj->resv = dma_buf->resv;
236 
237 	bo = to_ivpu_bo(obj);
238 
239 	mutex_lock(&vdev->bo_list_lock);
240 	list_add_tail(&bo->bo_list_node, &vdev->bo_list);
241 	mutex_unlock(&vdev->bo_list_lock);
242 
243 	ivpu_dbg(vdev, BO, "import: bo %8p size %9zu\n", bo, ivpu_bo_size(bo));
244 
245 	return obj;
246 
247 fail_detach:
248 	dma_buf_detach(dma_buf, attach);
249 	dma_buf_put(dma_buf);
250 
251 	return ERR_PTR(ret);
252 }
253 
254 static struct ivpu_bo *ivpu_bo_alloc(struct ivpu_device *vdev, u64 size, u32 flags)
255 {
256 	struct drm_gem_shmem_object *shmem;
257 	struct ivpu_bo *bo;
258 
259 	switch (flags & DRM_IVPU_BO_CACHE_MASK) {
260 	case DRM_IVPU_BO_CACHED:
261 	case DRM_IVPU_BO_WC:
262 		break;
263 	default:
264 		return ERR_PTR(-EINVAL);
265 	}
266 
267 	shmem = drm_gem_shmem_create(&vdev->drm, size);
268 	if (IS_ERR(shmem))
269 		return ERR_CAST(shmem);
270 
271 	bo = to_ivpu_bo(&shmem->base);
272 	bo->base.map_wc = flags & DRM_IVPU_BO_WC;
273 	bo->flags = flags;
274 
275 	mutex_lock(&vdev->bo_list_lock);
276 	list_add_tail(&bo->bo_list_node, &vdev->bo_list);
277 	mutex_unlock(&vdev->bo_list_lock);
278 
279 	ivpu_dbg(vdev, BO, " alloc: bo %8p size %9llu\n", bo, size);
280 
281 	return bo;
282 }
283 
284 static int ivpu_gem_bo_open(struct drm_gem_object *obj, struct drm_file *file)
285 {
286 	struct ivpu_file_priv *file_priv = file->driver_priv;
287 	struct ivpu_device *vdev = file_priv->vdev;
288 	struct ivpu_bo *bo = to_ivpu_bo(obj);
289 	struct ivpu_addr_range *range;
290 
291 	if (bo->ctx) {
292 		ivpu_warn(vdev, "Can't add BO to ctx %u: already in ctx %u\n",
293 			  file_priv->ctx.id, bo->ctx->id);
294 		return -EALREADY;
295 	}
296 
297 	if (bo->flags & DRM_IVPU_BO_SHAVE_MEM)
298 		range = &vdev->hw->ranges.shave;
299 	else if (bo->flags & DRM_IVPU_BO_DMA_MEM)
300 		range = &vdev->hw->ranges.dma;
301 	else
302 		range = &vdev->hw->ranges.user;
303 
304 	return ivpu_bo_alloc_vpu_addr(bo, &file_priv->ctx, range);
305 }
306 
307 static void ivpu_gem_bo_free(struct drm_gem_object *obj)
308 {
309 	struct ivpu_device *vdev = to_ivpu_device(obj->dev);
310 	struct ivpu_bo *bo = to_ivpu_bo(obj);
311 
312 	ivpu_dbg_bo(vdev, bo, "free");
313 
314 	drm_WARN_ON(&vdev->drm, list_empty(&bo->bo_list_node));
315 
316 	mutex_lock(&vdev->bo_list_lock);
317 	list_del(&bo->bo_list_node);
318 
319 	drm_WARN_ON(&vdev->drm, !drm_gem_is_imported(&bo->base.base) &&
320 		    !dma_resv_test_signaled(obj->resv, DMA_RESV_USAGE_READ));
321 	drm_WARN_ON(&vdev->drm, ivpu_bo_size(bo) == 0);
322 	drm_WARN_ON(&vdev->drm, bo->base.vaddr);
323 
324 	ivpu_bo_lock(bo);
325 	ivpu_bo_unbind_locked(bo);
326 	ivpu_bo_unlock(bo);
327 
328 	mutex_unlock(&vdev->bo_list_lock);
329 
330 	drm_WARN_ON(&vdev->drm, bo->mmu_mapped);
331 	drm_WARN_ON(&vdev->drm, bo->ctx);
332 
333 	drm_WARN_ON(obj->dev, refcount_read(&bo->base.pages_use_count) > 1);
334 	drm_WARN_ON(obj->dev, bo->base.base.vma_node.vm_files.rb_node);
335 	drm_gem_shmem_free(&bo->base);
336 }
337 
338 static const struct drm_gem_object_funcs ivpu_gem_funcs = {
339 	.free = ivpu_gem_bo_free,
340 	.open = ivpu_gem_bo_open,
341 	.print_info = drm_gem_shmem_object_print_info,
342 	.pin = drm_gem_shmem_object_pin,
343 	.unpin = drm_gem_shmem_object_unpin,
344 	.get_sg_table = drm_gem_shmem_object_get_sg_table,
345 	.vmap = drm_gem_shmem_object_vmap,
346 	.vunmap = drm_gem_shmem_object_vunmap,
347 	.mmap = drm_gem_shmem_object_mmap,
348 	.vm_ops = &drm_gem_shmem_vm_ops,
349 };
350 
351 int ivpu_bo_create_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
352 {
353 	struct ivpu_file_priv *file_priv = file->driver_priv;
354 	struct ivpu_device *vdev = file_priv->vdev;
355 	struct drm_ivpu_bo_create *args = data;
356 	u64 size = PAGE_ALIGN(args->size);
357 	struct ivpu_bo *bo;
358 	int ret;
359 
360 	if (args->flags & ~DRM_IVPU_BO_FLAGS)
361 		return -EINVAL;
362 
363 	if (size == 0)
364 		return -EINVAL;
365 
366 	bo = ivpu_bo_alloc(vdev, size, args->flags);
367 	if (IS_ERR(bo)) {
368 		ivpu_err(vdev, "Failed to allocate BO: %pe (ctx %u size %llu flags 0x%x)",
369 			 bo, file_priv->ctx.id, args->size, args->flags);
370 		return PTR_ERR(bo);
371 	}
372 
373 	drm_WARN_ON(&vdev->drm, bo->base.base.handle_count != 0);
374 
375 	ret = drm_gem_handle_create(file, &bo->base.base, &args->handle);
376 	if (ret) {
377 		ivpu_err(vdev, "Failed to create handle for BO: %pe (ctx %u size %llu flags 0x%x)",
378 			 bo, file_priv->ctx.id, args->size, args->flags);
379 	} else {
380 		args->vpu_addr = bo->vpu_addr;
381 		drm_WARN_ON(&vdev->drm, bo->base.base.handle_count != 1);
382 	}
383 
384 	drm_gem_object_put(&bo->base.base);
385 
386 	return ret;
387 }
388 
389 struct ivpu_bo *
390 ivpu_bo_create(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,
391 	       struct ivpu_addr_range *range, u64 size, u32 flags)
392 {
393 	struct iosys_map map;
394 	struct ivpu_bo *bo;
395 	int ret;
396 
397 	if (drm_WARN_ON(&vdev->drm, !range))
398 		return NULL;
399 
400 	drm_WARN_ON(&vdev->drm, !PAGE_ALIGNED(range->start));
401 	drm_WARN_ON(&vdev->drm, !PAGE_ALIGNED(range->end));
402 	drm_WARN_ON(&vdev->drm, !PAGE_ALIGNED(size));
403 
404 	bo = ivpu_bo_alloc(vdev, size, flags);
405 	if (IS_ERR(bo)) {
406 		ivpu_err(vdev, "Failed to allocate BO: %pe (vpu_addr 0x%llx size %llu flags 0x%x)",
407 			 bo, range->start, size, flags);
408 		return NULL;
409 	}
410 
411 	ret = ivpu_bo_alloc_vpu_addr(bo, ctx, range);
412 	if (ret)
413 		goto err_put;
414 
415 	ret = ivpu_bo_bind(bo);
416 	if (ret)
417 		goto err_put;
418 
419 	if (flags & DRM_IVPU_BO_MAPPABLE) {
420 		ivpu_bo_lock(bo);
421 		ret = drm_gem_shmem_vmap_locked(&bo->base, &map);
422 		ivpu_bo_unlock(bo);
423 
424 		if (ret)
425 			goto err_put;
426 	}
427 
428 	return bo;
429 
430 err_put:
431 	drm_gem_object_put(&bo->base.base);
432 	return NULL;
433 }
434 
435 struct ivpu_bo *ivpu_bo_create_runtime(struct ivpu_device *vdev, u64 addr, u64 size, u32 flags)
436 {
437 	struct ivpu_addr_range range;
438 
439 	if (!ivpu_is_within_range(addr, size, &vdev->hw->ranges.runtime)) {
440 		ivpu_err(vdev, "Invalid runtime BO address 0x%llx size %llu\n", addr, size);
441 		return NULL;
442 	}
443 
444 	if (ivpu_hw_range_init(vdev, &range, addr, size))
445 		return NULL;
446 
447 	return ivpu_bo_create(vdev, &vdev->gctx, &range, size, flags);
448 }
449 
450 struct ivpu_bo *ivpu_bo_create_global(struct ivpu_device *vdev, u64 size, u32 flags)
451 {
452 	return ivpu_bo_create(vdev, &vdev->gctx, &vdev->hw->ranges.global, size, flags);
453 }
454 
455 void ivpu_bo_free(struct ivpu_bo *bo)
456 {
457 	struct iosys_map map = IOSYS_MAP_INIT_VADDR(bo->base.vaddr);
458 
459 	if (bo->flags & DRM_IVPU_BO_MAPPABLE) {
460 		ivpu_bo_lock(bo);
461 		drm_gem_shmem_vunmap_locked(&bo->base, &map);
462 		ivpu_bo_unlock(bo);
463 	}
464 
465 	drm_gem_object_put(&bo->base.base);
466 }
467 
468 int ivpu_bo_info_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
469 {
470 	struct drm_ivpu_bo_info *args = data;
471 	struct drm_gem_object *obj;
472 	struct ivpu_bo *bo;
473 	int ret = 0;
474 
475 	obj = drm_gem_object_lookup(file, args->handle);
476 	if (!obj)
477 		return -ENOENT;
478 
479 	bo = to_ivpu_bo(obj);
480 
481 	ivpu_bo_lock(bo);
482 	args->flags = bo->flags;
483 	args->mmap_offset = drm_vma_node_offset_addr(&obj->vma_node);
484 	args->vpu_addr = bo->vpu_addr;
485 	args->size = obj->size;
486 	ivpu_bo_unlock(bo);
487 
488 	drm_gem_object_put(obj);
489 	return ret;
490 }
491 
492 int ivpu_bo_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
493 {
494 	struct drm_ivpu_bo_wait *args = data;
495 	struct drm_gem_object *obj;
496 	unsigned long timeout;
497 	long ret;
498 
499 	timeout = drm_timeout_abs_to_jiffies(args->timeout_ns);
500 
501 	/* Add 1 jiffy to ensure the wait function never times out before intended timeout_ns */
502 	timeout += 1;
503 
504 	obj = drm_gem_object_lookup(file, args->handle);
505 	if (!obj)
506 		return -EINVAL;
507 
508 	ret = dma_resv_wait_timeout(obj->resv, DMA_RESV_USAGE_READ, true, timeout);
509 	if (ret == 0) {
510 		ret = -ETIMEDOUT;
511 	} else if (ret > 0) {
512 		ret = 0;
513 		args->job_status = to_ivpu_bo(obj)->job_status;
514 	}
515 
516 	drm_gem_object_put(obj);
517 
518 	return ret;
519 }
520 
521 static void ivpu_bo_print_info(struct ivpu_bo *bo, struct drm_printer *p)
522 {
523 	ivpu_bo_lock(bo);
524 
525 	drm_printf(p, "%-9p %-3u 0x%-12llx %-10lu 0x%-8x %-4u",
526 		   bo, bo->ctx_id, bo->vpu_addr, bo->base.base.size,
527 		   bo->flags, kref_read(&bo->base.base.refcount));
528 
529 	if (bo->base.pages)
530 		drm_printf(p, " has_pages");
531 
532 	if (bo->mmu_mapped)
533 		drm_printf(p, " mmu_mapped");
534 
535 	if (drm_gem_is_imported(&bo->base.base))
536 		drm_printf(p, " imported");
537 
538 	drm_printf(p, "\n");
539 
540 	ivpu_bo_unlock(bo);
541 }
542 
543 void ivpu_bo_list(struct drm_device *dev, struct drm_printer *p)
544 {
545 	struct ivpu_device *vdev = to_ivpu_device(dev);
546 	struct ivpu_bo *bo;
547 
548 	drm_printf(p, "%-9s %-3s %-14s %-10s %-10s %-4s %s\n",
549 		   "bo", "ctx", "vpu_addr", "size", "flags", "refs", "attribs");
550 
551 	mutex_lock(&vdev->bo_list_lock);
552 	list_for_each_entry(bo, &vdev->bo_list, bo_list_node)
553 		ivpu_bo_print_info(bo, p);
554 	mutex_unlock(&vdev->bo_list_lock);
555 }
556 
557 void ivpu_bo_list_print(struct drm_device *dev)
558 {
559 	struct drm_printer p = drm_info_printer(dev->dev);
560 
561 	ivpu_bo_list(dev, &p);
562 }
563