1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
3  *
4  * Copyright 2009-2023 VMware, Inc., Palo Alto, CA., USA
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28 #include "vmwgfx_bo.h"
29 #include "vmwgfx_drv.h"
30 #include <drm/ttm/ttm_placement.h>
31 
32 static const struct ttm_place vram_placement_flags = {
33 	.fpfn = 0,
34 	.lpfn = 0,
35 	.mem_type = TTM_PL_VRAM,
36 	.flags = 0
37 };
38 
39 static const struct ttm_place sys_placement_flags = {
40 	.fpfn = 0,
41 	.lpfn = 0,
42 	.mem_type = TTM_PL_SYSTEM,
43 	.flags = 0
44 };
45 
46 static const struct ttm_place gmr_placement_flags = {
47 	.fpfn = 0,
48 	.lpfn = 0,
49 	.mem_type = VMW_PL_GMR,
50 	.flags = 0
51 };
52 
53 struct ttm_placement vmw_vram_placement = {
54 	.num_placement = 1,
55 	.placement = &vram_placement_flags,
56 	.num_busy_placement = 1,
57 	.busy_placement = &vram_placement_flags
58 };
59 
60 static const struct ttm_place vram_gmr_placement_flags[] = {
61 	{
62 		.fpfn = 0,
63 		.lpfn = 0,
64 		.mem_type = TTM_PL_VRAM,
65 		.flags = 0
66 	}, {
67 		.fpfn = 0,
68 		.lpfn = 0,
69 		.mem_type = VMW_PL_GMR,
70 		.flags = 0
71 	}
72 };
73 
74 struct ttm_placement vmw_vram_gmr_placement = {
75 	.num_placement = 2,
76 	.placement = vram_gmr_placement_flags,
77 	.num_busy_placement = 1,
78 	.busy_placement = &gmr_placement_flags
79 };
80 
81 struct ttm_placement vmw_sys_placement = {
82 	.num_placement = 1,
83 	.placement = &sys_placement_flags,
84 	.num_busy_placement = 1,
85 	.busy_placement = &sys_placement_flags
86 };
87 
88 const size_t vmw_tt_size = sizeof(struct vmw_ttm_tt);
89 
90 /**
91  * __vmw_piter_non_sg_next: Helper functions to advance
92  * a struct vmw_piter iterator.
93  *
94  * @viter: Pointer to the iterator.
95  *
96  * These functions return false if past the end of the list,
97  * true otherwise. Functions are selected depending on the current
98  * DMA mapping mode.
99  */
100 static bool __vmw_piter_non_sg_next(struct vmw_piter *viter)
101 {
102 	return ++(viter->i) < viter->num_pages;
103 }
104 
105 static bool __vmw_piter_sg_next(struct vmw_piter *viter)
106 {
107 	bool ret = __vmw_piter_non_sg_next(viter);
108 
109 	return __sg_page_iter_dma_next(&viter->iter) && ret;
110 }
111 
112 
113 static dma_addr_t __vmw_piter_dma_addr(struct vmw_piter *viter)
114 {
115 	return viter->addrs[viter->i];
116 }
117 
118 static dma_addr_t __vmw_piter_sg_addr(struct vmw_piter *viter)
119 {
120 	return sg_page_iter_dma_address(&viter->iter);
121 }
122 
123 
124 /**
125  * vmw_piter_start - Initialize a struct vmw_piter.
126  *
127  * @viter: Pointer to the iterator to initialize
128  * @vsgt: Pointer to a struct vmw_sg_table to initialize from
129  * @p_offset: Pointer offset used to update current array position
130  *
131  * Note that we're following the convention of __sg_page_iter_start, so that
132  * the iterator doesn't point to a valid page after initialization; it has
133  * to be advanced one step first.
134  */
135 void vmw_piter_start(struct vmw_piter *viter, const struct vmw_sg_table *vsgt,
136 		     unsigned long p_offset)
137 {
138 	viter->i = p_offset - 1;
139 	viter->num_pages = vsgt->num_pages;
140 	viter->pages = vsgt->pages;
141 	switch (vsgt->mode) {
142 	case vmw_dma_alloc_coherent:
143 		viter->next = &__vmw_piter_non_sg_next;
144 		viter->dma_address = &__vmw_piter_dma_addr;
145 		viter->addrs = vsgt->addrs;
146 		break;
147 	case vmw_dma_map_populate:
148 	case vmw_dma_map_bind:
149 		viter->next = &__vmw_piter_sg_next;
150 		viter->dma_address = &__vmw_piter_sg_addr;
151 		__sg_page_iter_start(&viter->iter.base, vsgt->sgt->sgl,
152 				     vsgt->sgt->orig_nents, p_offset);
153 		break;
154 	default:
155 		BUG();
156 	}
157 }
158 
159 /**
160  * vmw_ttm_unmap_from_dma - unmap  device addresses previsouly mapped for
161  * TTM pages
162  *
163  * @vmw_tt: Pointer to a struct vmw_ttm_backend
164  *
165  * Used to free dma mappings previously mapped by vmw_ttm_map_for_dma.
166  */
167 static void vmw_ttm_unmap_from_dma(struct vmw_ttm_tt *vmw_tt)
168 {
169 	struct device *dev = vmw_tt->dev_priv->drm.dev;
170 
171 	dma_unmap_sgtable(dev, &vmw_tt->sgt, DMA_BIDIRECTIONAL, 0);
172 	vmw_tt->sgt.nents = vmw_tt->sgt.orig_nents;
173 }
174 
175 /**
176  * vmw_ttm_map_for_dma - map TTM pages to get device addresses
177  *
178  * @vmw_tt: Pointer to a struct vmw_ttm_backend
179  *
180  * This function is used to get device addresses from the kernel DMA layer.
181  * However, it's violating the DMA API in that when this operation has been
182  * performed, it's illegal for the CPU to write to the pages without first
183  * unmapping the DMA mappings, or calling dma_sync_sg_for_cpu(). It is
184  * therefore only legal to call this function if we know that the function
185  * dma_sync_sg_for_cpu() is a NOP, and dma_sync_sg_for_device() is at most
186  * a CPU write buffer flush.
187  */
188 static int vmw_ttm_map_for_dma(struct vmw_ttm_tt *vmw_tt)
189 {
190 	struct device *dev = vmw_tt->dev_priv->drm.dev;
191 
192 	return dma_map_sgtable(dev, &vmw_tt->sgt, DMA_BIDIRECTIONAL, 0);
193 }
194 
195 /**
196  * vmw_ttm_map_dma - Make sure TTM pages are visible to the device
197  *
198  * @vmw_tt: Pointer to a struct vmw_ttm_tt
199  *
200  * Select the correct function for and make sure the TTM pages are
201  * visible to the device. Allocate storage for the device mappings.
202  * If a mapping has already been performed, indicated by the storage
203  * pointer being non NULL, the function returns success.
204  */
205 static int vmw_ttm_map_dma(struct vmw_ttm_tt *vmw_tt)
206 {
207 	struct vmw_private *dev_priv = vmw_tt->dev_priv;
208 	struct vmw_sg_table *vsgt = &vmw_tt->vsgt;
209 	int ret = 0;
210 
211 	if (vmw_tt->mapped)
212 		return 0;
213 
214 	vsgt->mode = dev_priv->map_mode;
215 	vsgt->pages = vmw_tt->dma_ttm.pages;
216 	vsgt->num_pages = vmw_tt->dma_ttm.num_pages;
217 	vsgt->addrs = vmw_tt->dma_ttm.dma_address;
218 	vsgt->sgt = NULL;
219 
220 	switch (dev_priv->map_mode) {
221 	case vmw_dma_map_bind:
222 	case vmw_dma_map_populate:
223 		if (vmw_tt->dma_ttm.page_flags  & TTM_TT_FLAG_EXTERNAL) {
224 			vsgt->sgt = vmw_tt->dma_ttm.sg;
225 		} else {
226 			vsgt->sgt = &vmw_tt->sgt;
227 			ret = sg_alloc_table_from_pages_segment(&vmw_tt->sgt,
228 				vsgt->pages, vsgt->num_pages, 0,
229 				(unsigned long)vsgt->num_pages << PAGE_SHIFT,
230 				dma_get_max_seg_size(dev_priv->drm.dev),
231 				GFP_KERNEL);
232 			if (ret)
233 				goto out_sg_alloc_fail;
234 		}
235 
236 		ret = vmw_ttm_map_for_dma(vmw_tt);
237 		if (unlikely(ret != 0))
238 			goto out_map_fail;
239 
240 		break;
241 	default:
242 		break;
243 	}
244 
245 	vmw_tt->mapped = true;
246 	return 0;
247 
248 out_map_fail:
249 	drm_warn(&dev_priv->drm, "VSG table map failed!");
250 	sg_free_table(vsgt->sgt);
251 	vsgt->sgt = NULL;
252 out_sg_alloc_fail:
253 	return ret;
254 }
255 
256 /**
257  * vmw_ttm_unmap_dma - Tear down any TTM page device mappings
258  *
259  * @vmw_tt: Pointer to a struct vmw_ttm_tt
260  *
261  * Tear down any previously set up device DMA mappings and free
262  * any storage space allocated for them. If there are no mappings set up,
263  * this function is a NOP.
264  */
265 static void vmw_ttm_unmap_dma(struct vmw_ttm_tt *vmw_tt)
266 {
267 	struct vmw_private *dev_priv = vmw_tt->dev_priv;
268 
269 	if (!vmw_tt->vsgt.sgt)
270 		return;
271 
272 	switch (dev_priv->map_mode) {
273 	case vmw_dma_map_bind:
274 	case vmw_dma_map_populate:
275 		vmw_ttm_unmap_from_dma(vmw_tt);
276 		sg_free_table(vmw_tt->vsgt.sgt);
277 		vmw_tt->vsgt.sgt = NULL;
278 		break;
279 	default:
280 		break;
281 	}
282 	vmw_tt->mapped = false;
283 }
284 
285 /**
286  * vmw_bo_sg_table - Return a struct vmw_sg_table object for a
287  * TTM buffer object
288  *
289  * @bo: Pointer to a struct ttm_buffer_object
290  *
291  * Returns a pointer to a struct vmw_sg_table object. The object should
292  * not be freed after use.
293  * Note that for the device addresses to be valid, the buffer object must
294  * either be reserved or pinned.
295  */
296 const struct vmw_sg_table *vmw_bo_sg_table(struct ttm_buffer_object *bo)
297 {
298 	struct vmw_ttm_tt *vmw_tt =
299 		container_of(bo->ttm, struct vmw_ttm_tt, dma_ttm);
300 
301 	return &vmw_tt->vsgt;
302 }
303 
304 
305 static int vmw_ttm_bind(struct ttm_device *bdev,
306 			struct ttm_tt *ttm, struct ttm_resource *bo_mem)
307 {
308 	struct vmw_ttm_tt *vmw_be =
309 		container_of(ttm, struct vmw_ttm_tt, dma_ttm);
310 	int ret = 0;
311 
312 	if (!bo_mem)
313 		return -EINVAL;
314 
315 	if (vmw_be->bound)
316 		return 0;
317 
318 	ret = vmw_ttm_map_dma(vmw_be);
319 	if (unlikely(ret != 0))
320 		return ret;
321 
322 	vmw_be->gmr_id = bo_mem->start;
323 	vmw_be->mem_type = bo_mem->mem_type;
324 
325 	switch (bo_mem->mem_type) {
326 	case VMW_PL_GMR:
327 		ret = vmw_gmr_bind(vmw_be->dev_priv, &vmw_be->vsgt,
328 				    ttm->num_pages, vmw_be->gmr_id);
329 		break;
330 	case VMW_PL_MOB:
331 		if (unlikely(vmw_be->mob == NULL)) {
332 			vmw_be->mob =
333 				vmw_mob_create(ttm->num_pages);
334 			if (unlikely(vmw_be->mob == NULL))
335 				return -ENOMEM;
336 		}
337 
338 		ret = vmw_mob_bind(vmw_be->dev_priv, vmw_be->mob,
339 				    &vmw_be->vsgt, ttm->num_pages,
340 				    vmw_be->gmr_id);
341 		break;
342 	case VMW_PL_SYSTEM:
343 		/* Nothing to be done for a system bind */
344 		break;
345 	default:
346 		BUG();
347 	}
348 	vmw_be->bound = true;
349 	return ret;
350 }
351 
352 static void vmw_ttm_unbind(struct ttm_device *bdev,
353 			   struct ttm_tt *ttm)
354 {
355 	struct vmw_ttm_tt *vmw_be =
356 		container_of(ttm, struct vmw_ttm_tt, dma_ttm);
357 
358 	if (!vmw_be->bound)
359 		return;
360 
361 	switch (vmw_be->mem_type) {
362 	case VMW_PL_GMR:
363 		vmw_gmr_unbind(vmw_be->dev_priv, vmw_be->gmr_id);
364 		break;
365 	case VMW_PL_MOB:
366 		vmw_mob_unbind(vmw_be->dev_priv, vmw_be->mob);
367 		break;
368 	case VMW_PL_SYSTEM:
369 		break;
370 	default:
371 		BUG();
372 	}
373 
374 	if (vmw_be->dev_priv->map_mode == vmw_dma_map_bind)
375 		vmw_ttm_unmap_dma(vmw_be);
376 	vmw_be->bound = false;
377 }
378 
379 
380 static void vmw_ttm_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
381 {
382 	struct vmw_ttm_tt *vmw_be =
383 		container_of(ttm, struct vmw_ttm_tt, dma_ttm);
384 
385 	vmw_ttm_unmap_dma(vmw_be);
386 	ttm_tt_fini(ttm);
387 	if (vmw_be->mob)
388 		vmw_mob_destroy(vmw_be->mob);
389 
390 	kfree(vmw_be);
391 }
392 
393 
394 static int vmw_ttm_populate(struct ttm_device *bdev,
395 			    struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
396 {
397 	bool external = (ttm->page_flags & TTM_TT_FLAG_EXTERNAL) != 0;
398 
399 	if (ttm_tt_is_populated(ttm))
400 		return 0;
401 
402 	if (external && ttm->sg)
403 		return  drm_prime_sg_to_dma_addr_array(ttm->sg,
404 						       ttm->dma_address,
405 						       ttm->num_pages);
406 
407 	return ttm_pool_alloc(&bdev->pool, ttm, ctx);
408 }
409 
410 static void vmw_ttm_unpopulate(struct ttm_device *bdev,
411 			       struct ttm_tt *ttm)
412 {
413 	struct vmw_ttm_tt *vmw_tt = container_of(ttm, struct vmw_ttm_tt,
414 						 dma_ttm);
415 	bool external = (ttm->page_flags & TTM_TT_FLAG_EXTERNAL) != 0;
416 
417 	if (external)
418 		return;
419 
420 	vmw_ttm_unbind(bdev, ttm);
421 
422 	if (vmw_tt->mob) {
423 		vmw_mob_destroy(vmw_tt->mob);
424 		vmw_tt->mob = NULL;
425 	}
426 
427 	vmw_ttm_unmap_dma(vmw_tt);
428 
429 	ttm_pool_free(&bdev->pool, ttm);
430 }
431 
432 static struct ttm_tt *vmw_ttm_tt_create(struct ttm_buffer_object *bo,
433 					uint32_t page_flags)
434 {
435 	struct vmw_ttm_tt *vmw_be;
436 	int ret;
437 	bool external = bo->type == ttm_bo_type_sg;
438 
439 	vmw_be = kzalloc(sizeof(*vmw_be), GFP_KERNEL);
440 	if (!vmw_be)
441 		return NULL;
442 
443 	vmw_be->dev_priv = vmw_priv_from_ttm(bo->bdev);
444 	vmw_be->mob = NULL;
445 
446 	if (external)
447 		page_flags |= TTM_TT_FLAG_EXTERNAL | TTM_TT_FLAG_EXTERNAL_MAPPABLE;
448 
449 	if (vmw_be->dev_priv->map_mode == vmw_dma_alloc_coherent || external)
450 		ret = ttm_sg_tt_init(&vmw_be->dma_ttm, bo, page_flags,
451 				     ttm_cached);
452 	else
453 		ret = ttm_tt_init(&vmw_be->dma_ttm, bo, page_flags,
454 				  ttm_cached, 0);
455 	if (unlikely(ret != 0))
456 		goto out_no_init;
457 
458 	return &vmw_be->dma_ttm;
459 out_no_init:
460 	kfree(vmw_be);
461 	return NULL;
462 }
463 
464 static void vmw_evict_flags(struct ttm_buffer_object *bo,
465 		     struct ttm_placement *placement)
466 {
467 	*placement = vmw_sys_placement;
468 }
469 
470 static int vmw_ttm_io_mem_reserve(struct ttm_device *bdev, struct ttm_resource *mem)
471 {
472 	struct vmw_private *dev_priv = vmw_priv_from_ttm(bdev);
473 
474 	switch (mem->mem_type) {
475 	case TTM_PL_SYSTEM:
476 	case VMW_PL_SYSTEM:
477 	case VMW_PL_GMR:
478 	case VMW_PL_MOB:
479 		return 0;
480 	case TTM_PL_VRAM:
481 		mem->bus.offset = (mem->start << PAGE_SHIFT) +
482 			dev_priv->vram_start;
483 		mem->bus.is_iomem = true;
484 		mem->bus.caching = ttm_cached;
485 		break;
486 	default:
487 		return -EINVAL;
488 	}
489 	return 0;
490 }
491 
492 /**
493  * vmw_move_notify - TTM move_notify_callback
494  *
495  * @bo: The TTM buffer object about to move.
496  * @old_mem: The old memory where we move from
497  * @new_mem: The struct ttm_resource indicating to what memory
498  *       region the move is taking place.
499  *
500  * Calls move_notify for all subsystems needing it.
501  * (currently only resources).
502  */
503 static void vmw_move_notify(struct ttm_buffer_object *bo,
504 			    struct ttm_resource *old_mem,
505 			    struct ttm_resource *new_mem)
506 {
507 	vmw_bo_move_notify(bo, new_mem);
508 	vmw_query_move_notify(bo, old_mem, new_mem);
509 }
510 
511 
512 /**
513  * vmw_swap_notify - TTM move_notify_callback
514  *
515  * @bo: The TTM buffer object about to be swapped out.
516  */
517 static void vmw_swap_notify(struct ttm_buffer_object *bo)
518 {
519 	vmw_bo_swap_notify(bo);
520 	(void) ttm_bo_wait(bo, false, false);
521 }
522 
523 static bool vmw_memtype_is_system(uint32_t mem_type)
524 {
525 	return mem_type == TTM_PL_SYSTEM || mem_type == VMW_PL_SYSTEM;
526 }
527 
528 static int vmw_move(struct ttm_buffer_object *bo,
529 		    bool evict,
530 		    struct ttm_operation_ctx *ctx,
531 		    struct ttm_resource *new_mem,
532 		    struct ttm_place *hop)
533 {
534 	struct ttm_resource_manager *new_man;
535 	struct ttm_resource_manager *old_man = NULL;
536 	int ret = 0;
537 
538 	new_man = ttm_manager_type(bo->bdev, new_mem->mem_type);
539 	if (bo->resource)
540 		old_man = ttm_manager_type(bo->bdev, bo->resource->mem_type);
541 
542 	if (new_man->use_tt && !vmw_memtype_is_system(new_mem->mem_type)) {
543 		ret = vmw_ttm_bind(bo->bdev, bo->ttm, new_mem);
544 		if (ret)
545 			return ret;
546 	}
547 
548 	if (!bo->resource || (bo->resource->mem_type == TTM_PL_SYSTEM &&
549 			      bo->ttm == NULL)) {
550 		ttm_bo_move_null(bo, new_mem);
551 		return 0;
552 	}
553 
554 	vmw_move_notify(bo, bo->resource, new_mem);
555 
556 	if (old_man && old_man->use_tt && new_man->use_tt) {
557 		if (vmw_memtype_is_system(bo->resource->mem_type)) {
558 			ttm_bo_move_null(bo, new_mem);
559 			return 0;
560 		}
561 		ret = ttm_bo_wait_ctx(bo, ctx);
562 		if (ret)
563 			goto fail;
564 
565 		vmw_ttm_unbind(bo->bdev, bo->ttm);
566 		ttm_resource_free(bo, &bo->resource);
567 		ttm_bo_assign_mem(bo, new_mem);
568 		return 0;
569 	} else {
570 		ret = ttm_bo_move_memcpy(bo, ctx, new_mem);
571 		if (ret)
572 			goto fail;
573 	}
574 	return 0;
575 fail:
576 	vmw_move_notify(bo, new_mem, bo->resource);
577 	return ret;
578 }
579 
580 struct ttm_device_funcs vmw_bo_driver = {
581 	.ttm_tt_create = &vmw_ttm_tt_create,
582 	.ttm_tt_populate = &vmw_ttm_populate,
583 	.ttm_tt_unpopulate = &vmw_ttm_unpopulate,
584 	.ttm_tt_destroy = &vmw_ttm_destroy,
585 	.eviction_valuable = ttm_bo_eviction_valuable,
586 	.evict_flags = vmw_evict_flags,
587 	.move = vmw_move,
588 	.swap_notify = vmw_swap_notify,
589 	.io_mem_reserve = &vmw_ttm_io_mem_reserve,
590 };
591 
592 int vmw_bo_create_and_populate(struct vmw_private *dev_priv,
593 			       size_t bo_size, u32 domain,
594 			       struct vmw_bo **bo_p)
595 {
596 	struct ttm_operation_ctx ctx = {
597 		.interruptible = false,
598 		.no_wait_gpu = false
599 	};
600 	struct vmw_bo *vbo;
601 	int ret;
602 	struct vmw_bo_params bo_params = {
603 		.domain = domain,
604 		.busy_domain = domain,
605 		.bo_type = ttm_bo_type_kernel,
606 		.size = bo_size,
607 		.pin = true
608 	};
609 
610 	ret = vmw_bo_create(dev_priv, &bo_params, &vbo);
611 	if (unlikely(ret != 0))
612 		return ret;
613 
614 	ret = ttm_bo_reserve(&vbo->tbo, false, true, NULL);
615 	BUG_ON(ret != 0);
616 	ret = vmw_ttm_populate(vbo->tbo.bdev, vbo->tbo.ttm, &ctx);
617 	if (likely(ret == 0)) {
618 		struct vmw_ttm_tt *vmw_tt =
619 			container_of(vbo->tbo.ttm, struct vmw_ttm_tt, dma_ttm);
620 		ret = vmw_ttm_map_dma(vmw_tt);
621 	}
622 
623 	ttm_bo_unreserve(&vbo->tbo);
624 
625 	if (likely(ret == 0))
626 		*bo_p = vbo;
627 	return ret;
628 }
629