vmwgfx_ttm_buffer.c 14.9 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0 OR MIT
2 3
/**************************************************************************
 *
4
 * Copyright 2009-2023 VMware, Inc., Palo Alto, CA., USA
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

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 34
	.fpfn = 0,
	.lpfn = 0,
35
	.mem_type = TTM_PL_VRAM,
36
	.flags = 0
37
};
38

39
static const struct ttm_place sys_placement_flags = {
40 41
	.fpfn = 0,
	.lpfn = 0,
42
	.mem_type = TTM_PL_SYSTEM,
43
	.flags = 0
44
};
45

46
struct ttm_placement vmw_vram_placement = {
47 48 49 50 51 52 53 54 55
	.num_placement = 1,
	.placement = &vram_placement_flags,
};

struct ttm_placement vmw_sys_placement = {
	.num_placement = 1,
	.placement = &sys_placement_flags,
};

56 57
const size_t vmw_tt_size = sizeof(struct vmw_ttm_tt);

58
/**
59 60
 * __vmw_piter_non_sg_next: Helper functions to advance
 * a struct vmw_piter iterator.
61 62 63 64 65 66 67 68 69 70 71 72 73 74
 *
 * @viter: Pointer to the iterator.
 *
 * These functions return false if past the end of the list,
 * true otherwise. Functions are selected depending on the current
 * DMA mapping mode.
 */
static bool __vmw_piter_non_sg_next(struct vmw_piter *viter)
{
	return ++(viter->i) < viter->num_pages;
}

static bool __vmw_piter_sg_next(struct vmw_piter *viter)
{
75 76 77
	bool ret = __vmw_piter_non_sg_next(viter);

	return __sg_page_iter_dma_next(&viter->iter) && ret;
78 79 80 81 82 83 84 85 86 87
}


static dma_addr_t __vmw_piter_dma_addr(struct vmw_piter *viter)
{
	return viter->addrs[viter->i];
}

static dma_addr_t __vmw_piter_sg_addr(struct vmw_piter *viter)
{
88
	return sg_page_iter_dma_address(&viter->iter);
89 90 91 92 93 94 95 96
}


/**
 * vmw_piter_start - Initialize a struct vmw_piter.
 *
 * @viter: Pointer to the iterator to initialize
 * @vsgt: Pointer to a struct vmw_sg_table to initialize from
97
 * @p_offset: Pointer offset used to update current array position
98 99 100 101 102 103 104 105 106 107
 *
 * Note that we're following the convention of __sg_page_iter_start, so that
 * the iterator doesn't point to a valid page after initialization; it has
 * to be advanced one step first.
 */
void vmw_piter_start(struct vmw_piter *viter, const struct vmw_sg_table *vsgt,
		     unsigned long p_offset)
{
	viter->i = p_offset - 1;
	viter->num_pages = vsgt->num_pages;
108
	viter->pages = vsgt->pages;
109 110 111 112 113 114 115 116 117 118
	switch (vsgt->mode) {
	case vmw_dma_alloc_coherent:
		viter->next = &__vmw_piter_non_sg_next;
		viter->dma_address = &__vmw_piter_dma_addr;
		viter->addrs = vsgt->addrs;
		break;
	case vmw_dma_map_populate:
	case vmw_dma_map_bind:
		viter->next = &__vmw_piter_sg_next;
		viter->dma_address = &__vmw_piter_sg_addr;
119
		__sg_page_iter_start(&viter->iter.base, vsgt->sgt->sgl,
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
				     vsgt->sgt->orig_nents, p_offset);
		break;
	default:
		BUG();
	}
}

/**
 * vmw_ttm_unmap_from_dma - unmap  device addresses previsouly mapped for
 * TTM pages
 *
 * @vmw_tt: Pointer to a struct vmw_ttm_backend
 *
 * Used to free dma mappings previously mapped by vmw_ttm_map_for_dma.
 */
static void vmw_ttm_unmap_from_dma(struct vmw_ttm_tt *vmw_tt)
{
137
	struct device *dev = vmw_tt->dev_priv->drm.dev;
138

139
	dma_unmap_sgtable(dev, &vmw_tt->sgt, DMA_BIDIRECTIONAL, 0);
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
	vmw_tt->sgt.nents = vmw_tt->sgt.orig_nents;
}

/**
 * vmw_ttm_map_for_dma - map TTM pages to get device addresses
 *
 * @vmw_tt: Pointer to a struct vmw_ttm_backend
 *
 * This function is used to get device addresses from the kernel DMA layer.
 * However, it's violating the DMA API in that when this operation has been
 * performed, it's illegal for the CPU to write to the pages without first
 * unmapping the DMA mappings, or calling dma_sync_sg_for_cpu(). It is
 * therefore only legal to call this function if we know that the function
 * dma_sync_sg_for_cpu() is a NOP, and dma_sync_sg_for_device() is at most
 * a CPU write buffer flush.
 */
static int vmw_ttm_map_for_dma(struct vmw_ttm_tt *vmw_tt)
{
158
	struct device *dev = vmw_tt->dev_priv->drm.dev;
159

160
	return dma_map_sgtable(dev, &vmw_tt->sgt, DMA_BIDIRECTIONAL, 0);
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
}

/**
 * vmw_ttm_map_dma - Make sure TTM pages are visible to the device
 *
 * @vmw_tt: Pointer to a struct vmw_ttm_tt
 *
 * Select the correct function for and make sure the TTM pages are
 * visible to the device. Allocate storage for the device mappings.
 * If a mapping has already been performed, indicated by the storage
 * pointer being non NULL, the function returns success.
 */
static int vmw_ttm_map_dma(struct vmw_ttm_tt *vmw_tt)
{
	struct vmw_private *dev_priv = vmw_tt->dev_priv;
	struct vmw_sg_table *vsgt = &vmw_tt->vsgt;
	int ret = 0;

	if (vmw_tt->mapped)
		return 0;

	vsgt->mode = dev_priv->map_mode;
183 184
	vsgt->pages = vmw_tt->dma_ttm.pages;
	vsgt->num_pages = vmw_tt->dma_ttm.num_pages;
185
	vsgt->addrs = vmw_tt->dma_ttm.dma_address;
186
	vsgt->sgt = NULL;
187 188 189 190

	switch (dev_priv->map_mode) {
	case vmw_dma_map_bind:
	case vmw_dma_map_populate:
191 192 193 194 195 196 197 198 199 200 201 202
		if (vmw_tt->dma_ttm.page_flags  & TTM_TT_FLAG_EXTERNAL) {
			vsgt->sgt = vmw_tt->dma_ttm.sg;
		} else {
			vsgt->sgt = &vmw_tt->sgt;
			ret = sg_alloc_table_from_pages_segment(&vmw_tt->sgt,
				vsgt->pages, vsgt->num_pages, 0,
				(unsigned long)vsgt->num_pages << PAGE_SHIFT,
				dma_get_max_seg_size(dev_priv->drm.dev),
				GFP_KERNEL);
			if (ret)
				goto out_sg_alloc_fail;
		}
203 204 205 206 207 208 209 210 211 212 213 214 215 216

		ret = vmw_ttm_map_for_dma(vmw_tt);
		if (unlikely(ret != 0))
			goto out_map_fail;

		break;
	default:
		break;
	}

	vmw_tt->mapped = true;
	return 0;

out_map_fail:
217 218 219
	drm_warn(&dev_priv->drm, "VSG table map failed!");
	sg_free_table(vsgt->sgt);
	vsgt->sgt = NULL;
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
out_sg_alloc_fail:
	return ret;
}

/**
 * vmw_ttm_unmap_dma - Tear down any TTM page device mappings
 *
 * @vmw_tt: Pointer to a struct vmw_ttm_tt
 *
 * Tear down any previously set up device DMA mappings and free
 * any storage space allocated for them. If there are no mappings set up,
 * this function is a NOP.
 */
static void vmw_ttm_unmap_dma(struct vmw_ttm_tt *vmw_tt)
{
	struct vmw_private *dev_priv = vmw_tt->dev_priv;

	if (!vmw_tt->vsgt.sgt)
		return;

	switch (dev_priv->map_mode) {
	case vmw_dma_map_bind:
	case vmw_dma_map_populate:
		vmw_ttm_unmap_from_dma(vmw_tt);
		sg_free_table(vmw_tt->vsgt.sgt);
		vmw_tt->vsgt.sgt = NULL;
		break;
	default:
		break;
	}
	vmw_tt->mapped = false;
}

253 254 255 256 257 258 259 260 261 262 263 264 265 266
/**
 * vmw_bo_sg_table - Return a struct vmw_sg_table object for a
 * TTM buffer object
 *
 * @bo: Pointer to a struct ttm_buffer_object
 *
 * Returns a pointer to a struct vmw_sg_table object. The object should
 * not be freed after use.
 * Note that for the device addresses to be valid, the buffer object must
 * either be reserved or pinned.
 */
const struct vmw_sg_table *vmw_bo_sg_table(struct ttm_buffer_object *bo)
{
	struct vmw_ttm_tt *vmw_tt =
267
		container_of(bo->ttm, struct vmw_ttm_tt, dma_ttm);
268 269 270 271 272

	return &vmw_tt->vsgt;
}


273
static int vmw_ttm_bind(struct ttm_device *bdev,
274
			struct ttm_tt *ttm, struct ttm_resource *bo_mem)
275
{
276
	struct vmw_ttm_tt *vmw_be =
277
		container_of(ttm, struct vmw_ttm_tt, dma_ttm);
278 279 280 281 282 283 284
	int ret = 0;

	if (!bo_mem)
		return -EINVAL;

	if (vmw_be->bound)
		return 0;
285 286 287 288

	ret = vmw_ttm_map_dma(vmw_be);
	if (unlikely(ret != 0))
		return ret;
289 290

	vmw_be->gmr_id = bo_mem->start;
291
	vmw_be->mem_type = bo_mem->mem_type;
292

293 294
	switch (bo_mem->mem_type) {
	case VMW_PL_GMR:
295
		ret = vmw_gmr_bind(vmw_be->dev_priv, &vmw_be->vsgt,
296
				    ttm->num_pages, vmw_be->gmr_id);
297
		break;
298 299 300 301 302 303 304 305
	case VMW_PL_MOB:
		if (unlikely(vmw_be->mob == NULL)) {
			vmw_be->mob =
				vmw_mob_create(ttm->num_pages);
			if (unlikely(vmw_be->mob == NULL))
				return -ENOMEM;
		}

306
		ret = vmw_mob_bind(vmw_be->dev_priv, vmw_be->mob,
307
				    &vmw_be->vsgt, ttm->num_pages,
308
				    vmw_be->gmr_id);
309
		break;
310 311 312
	case VMW_PL_SYSTEM:
		/* Nothing to be done for a system bind */
		break;
313 314 315
	default:
		BUG();
	}
316 317
	vmw_be->bound = true;
	return ret;
318 319
}

320
static void vmw_ttm_unbind(struct ttm_device *bdev,
321
			   struct ttm_tt *ttm)
322
{
323
	struct vmw_ttm_tt *vmw_be =
324
		container_of(ttm, struct vmw_ttm_tt, dma_ttm);
325

326 327 328
	if (!vmw_be->bound)
		return;

329 330 331 332 333 334 335
	switch (vmw_be->mem_type) {
	case VMW_PL_GMR:
		vmw_gmr_unbind(vmw_be->dev_priv, vmw_be->gmr_id);
		break;
	case VMW_PL_MOB:
		vmw_mob_unbind(vmw_be->dev_priv, vmw_be->mob);
		break;
336 337
	case VMW_PL_SYSTEM:
		break;
338 339 340
	default:
		BUG();
	}
341 342 343

	if (vmw_be->dev_priv->map_mode == vmw_dma_map_bind)
		vmw_ttm_unmap_dma(vmw_be);
344
	vmw_be->bound = false;
345 346
}

347

348
static void vmw_ttm_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
349
{
350
	struct vmw_ttm_tt *vmw_be =
351
		container_of(ttm, struct vmw_ttm_tt, dma_ttm);
352 353

	vmw_ttm_unmap_dma(vmw_be);
354
	ttm_tt_fini(ttm);
355 356 357
	if (vmw_be->mob)
		vmw_mob_destroy(vmw_be->mob);

358 359 360
	kfree(vmw_be);
}

361

362
static int vmw_ttm_populate(struct ttm_device *bdev,
363
			    struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
364
{
365
	bool external = (ttm->page_flags & TTM_TT_FLAG_EXTERNAL) != 0;
366

367
	if (ttm_tt_is_populated(ttm))
368 369
		return 0;

370 371 372 373
	if (external && ttm->sg)
		return  drm_prime_sg_to_dma_addr_array(ttm->sg,
						       ttm->dma_address,
						       ttm->num_pages);
374

375
	return ttm_pool_alloc(&bdev->pool, ttm, ctx);
376 377
}

378
static void vmw_ttm_unpopulate(struct ttm_device *bdev,
379
			       struct ttm_tt *ttm)
380 381
{
	struct vmw_ttm_tt *vmw_tt = container_of(ttm, struct vmw_ttm_tt,
382
						 dma_ttm);
383 384 385 386
	bool external = (ttm->page_flags & TTM_TT_FLAG_EXTERNAL) != 0;

	if (external)
		return;
387

388 389
	vmw_ttm_unbind(bdev, ttm);

390 391 392 393 394
	if (vmw_tt->mob) {
		vmw_mob_destroy(vmw_tt->mob);
		vmw_tt->mob = NULL;
	}

395
	vmw_ttm_unmap_dma(vmw_tt);
396

397
	ttm_pool_free(&bdev->pool, ttm);
398 399
}

400 401
static struct ttm_tt *vmw_ttm_tt_create(struct ttm_buffer_object *bo,
					uint32_t page_flags)
402
{
403
	struct vmw_ttm_tt *vmw_be;
404
	int ret;
405
	bool external = bo->type == ttm_bo_type_sg;
406

407
	vmw_be = kzalloc(sizeof(*vmw_be), GFP_KERNEL);
408 409 410
	if (!vmw_be)
		return NULL;

411
	vmw_be->dev_priv = vmw_priv_from_ttm(bo->bdev);
412
	vmw_be->mob = NULL;
413

414 415 416 417
	if (external)
		page_flags |= TTM_TT_FLAG_EXTERNAL | TTM_TT_FLAG_EXTERNAL_MAPPABLE;

	if (vmw_be->dev_priv->map_mode == vmw_dma_alloc_coherent || external)
418 419
		ret = ttm_sg_tt_init(&vmw_be->dma_ttm, bo, page_flags,
				     ttm_cached);
420
	else
421
		ret = ttm_tt_init(&vmw_be->dma_ttm, bo, page_flags,
422
				  ttm_cached, 0);
423 424 425
	if (unlikely(ret != 0))
		goto out_no_init;

426
	return &vmw_be->dma_ttm;
427 428 429
out_no_init:
	kfree(vmw_be);
	return NULL;
430 431
}

432
static void vmw_evict_flags(struct ttm_buffer_object *bo,
433 434 435 436 437
		     struct ttm_placement *placement)
{
	*placement = vmw_sys_placement;
}

438
static int vmw_ttm_io_mem_reserve(struct ttm_device *bdev, struct ttm_resource *mem)
439
{
440
	struct vmw_private *dev_priv = vmw_priv_from_ttm(bdev);
441 442 443

	switch (mem->mem_type) {
	case TTM_PL_SYSTEM:
444
	case VMW_PL_SYSTEM:
445
	case VMW_PL_GMR:
446
	case VMW_PL_MOB:
447 448
		return 0;
	case TTM_PL_VRAM:
449 450
		mem->bus.offset = (mem->start << PAGE_SHIFT) +
			dev_priv->vram_start;
451
		mem->bus.is_iomem = true;
452
		mem->bus.caching = ttm_cached;
453 454 455 456 457 458 459
		break;
	default:
		return -EINVAL;
	}
	return 0;
}

460 461 462
/**
 * vmw_move_notify - TTM move_notify_callback
 *
463
 * @bo: The TTM buffer object about to move.
464 465
 * @old_mem: The old memory where we move from
 * @new_mem: The struct ttm_resource indicating to what memory
466
 *       region the move is taking place.
467 468 469 470 471
 *
 * Calls move_notify for all subsystems needing it.
 * (currently only resources).
 */
static void vmw_move_notify(struct ttm_buffer_object *bo,
472 473
			    struct ttm_resource *old_mem,
			    struct ttm_resource *new_mem)
474
{
475 476
	vmw_bo_move_notify(bo, new_mem);
	vmw_query_move_notify(bo, old_mem, new_mem);
477 478 479 480 481 482
}


/**
 * vmw_swap_notify - TTM move_notify_callback
 *
483
 * @bo: The TTM buffer object about to be swapped out.
484 485 486
 */
static void vmw_swap_notify(struct ttm_buffer_object *bo)
{
487
	vmw_bo_swap_notify(bo);
488
	(void) ttm_bo_wait(bo, false, false);
489 490
}

491 492 493 494 495
static bool vmw_memtype_is_system(uint32_t mem_type)
{
	return mem_type == TTM_PL_SYSTEM || mem_type == VMW_PL_SYSTEM;
}

496 497 498
static int vmw_move(struct ttm_buffer_object *bo,
		    bool evict,
		    struct ttm_operation_ctx *ctx,
499 500
		    struct ttm_resource *new_mem,
		    struct ttm_place *hop)
501
{
502 503 504 505 506 507 508
	struct ttm_resource_manager *new_man;
	struct ttm_resource_manager *old_man = NULL;
	int ret = 0;

	new_man = ttm_manager_type(bo->bdev, new_mem->mem_type);
	if (bo->resource)
		old_man = ttm_manager_type(bo->bdev, bo->resource->mem_type);
509

510
	if (new_man->use_tt && !vmw_memtype_is_system(new_mem->mem_type)) {
511 512 513 514 515
		ret = vmw_ttm_bind(bo->bdev, bo->ttm, new_mem);
		if (ret)
			return ret;
	}

516 517 518 519 520 521
	if (!bo->resource || (bo->resource->mem_type == TTM_PL_SYSTEM &&
			      bo->ttm == NULL)) {
		ttm_bo_move_null(bo, new_mem);
		return 0;
	}

522
	vmw_move_notify(bo, bo->resource, new_mem);
523

524
	if (old_man && old_man->use_tt && new_man->use_tt) {
525
		if (vmw_memtype_is_system(bo->resource->mem_type)) {
526
			ttm_bo_move_null(bo, new_mem);
527 528
			return 0;
		}
529
		ret = ttm_bo_wait_ctx(bo, ctx);
530
		if (ret)
531
			goto fail;
532 533

		vmw_ttm_unbind(bo->bdev, bo->ttm);
534
		ttm_resource_free(bo, &bo->resource);
535 536
		ttm_bo_assign_mem(bo, new_mem);
		return 0;
537
	} else {
538 539 540
		ret = ttm_bo_move_memcpy(bo, ctx, new_mem);
		if (ret)
			goto fail;
541
	}
542 543
	return 0;
fail:
544
	vmw_move_notify(bo, new_mem, bo->resource);
545
	return ret;
546
}
547

548
struct ttm_device_funcs vmw_bo_driver = {
549
	.ttm_tt_create = &vmw_ttm_tt_create,
550 551
	.ttm_tt_populate = &vmw_ttm_populate,
	.ttm_tt_unpopulate = &vmw_ttm_unpopulate,
552
	.ttm_tt_destroy = &vmw_ttm_destroy,
553
	.eviction_valuable = ttm_bo_eviction_valuable,
554
	.evict_flags = vmw_evict_flags,
555
	.move = vmw_move,
556
	.swap_notify = vmw_swap_notify,
557
	.io_mem_reserve = &vmw_ttm_io_mem_reserve,
558
};
559 560

int vmw_bo_create_and_populate(struct vmw_private *dev_priv,
561 562
			       size_t bo_size, u32 domain,
			       struct vmw_bo **bo_p)
563 564 565 566 567
{
	struct ttm_operation_ctx ctx = {
		.interruptible = false,
		.no_wait_gpu = false
	};
568
	struct vmw_bo *vbo;
569
	int ret;
570 571 572 573 574 575 576
	struct vmw_bo_params bo_params = {
		.domain = domain,
		.busy_domain = domain,
		.bo_type = ttm_bo_type_kernel,
		.size = bo_size,
		.pin = true
	};
577

578
	ret = vmw_bo_create(dev_priv, &bo_params, &vbo);
579 580 581
	if (unlikely(ret != 0))
		return ret;

582
	ret = ttm_bo_reserve(&vbo->tbo, false, true, NULL);
583
	BUG_ON(ret != 0);
584
	ret = vmw_ttm_populate(vbo->tbo.bdev, vbo->tbo.ttm, &ctx);
585 586
	if (likely(ret == 0)) {
		struct vmw_ttm_tt *vmw_tt =
587
			container_of(vbo->tbo.ttm, struct vmw_ttm_tt, dma_ttm);
588 589
		ret = vmw_ttm_map_dma(vmw_tt);
	}
590

591
	ttm_bo_unreserve(&vbo->tbo);
592 593

	if (likely(ret == 0))
594
		*bo_p = vbo;
595 596
	return ret;
}