1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
4 * Copyright 2009-2015 VMware, Inc., Palo Alto, CA., USA
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "vmwgfx_drv.h"
29 #include <drm/ttm/ttm_bo_driver.h>
30 #include <drm/ttm/ttm_placement.h>
32 static const struct ttm_place vram_placement_flags = {
35 .mem_type = TTM_PL_VRAM,
39 static const struct ttm_place sys_placement_flags = {
42 .mem_type = TTM_PL_SYSTEM,
46 static const struct ttm_place gmr_placement_flags = {
49 .mem_type = VMW_PL_GMR,
53 static const struct ttm_place mob_placement_flags = {
56 .mem_type = VMW_PL_MOB,
60 struct ttm_placement vmw_vram_placement = {
62 .placement = &vram_placement_flags,
63 .num_busy_placement = 1,
64 .busy_placement = &vram_placement_flags
67 static const struct ttm_place vram_gmr_placement_flags[] = {
71 .mem_type = TTM_PL_VRAM,
76 .mem_type = VMW_PL_GMR,
81 static const struct ttm_place gmr_vram_placement_flags[] = {
85 .mem_type = VMW_PL_GMR,
90 .mem_type = TTM_PL_VRAM,
95 struct ttm_placement vmw_vram_gmr_placement = {
97 .placement = vram_gmr_placement_flags,
98 .num_busy_placement = 1,
99 .busy_placement = &gmr_placement_flags
102 struct ttm_placement vmw_vram_sys_placement = {
104 .placement = &vram_placement_flags,
105 .num_busy_placement = 1,
106 .busy_placement = &sys_placement_flags
109 struct ttm_placement vmw_sys_placement = {
111 .placement = &sys_placement_flags,
112 .num_busy_placement = 1,
113 .busy_placement = &sys_placement_flags
116 static const struct ttm_place evictable_placement_flags[] = {
120 .mem_type = TTM_PL_SYSTEM,
125 .mem_type = TTM_PL_VRAM,
130 .mem_type = VMW_PL_GMR,
135 .mem_type = VMW_PL_MOB,
140 static const struct ttm_place nonfixed_placement_flags[] = {
144 .mem_type = TTM_PL_SYSTEM,
149 .mem_type = VMW_PL_GMR,
154 .mem_type = VMW_PL_MOB,
159 struct ttm_placement vmw_evictable_placement = {
161 .placement = evictable_placement_flags,
162 .num_busy_placement = 1,
163 .busy_placement = &sys_placement_flags
166 struct ttm_placement vmw_srf_placement = {
168 .num_busy_placement = 2,
169 .placement = &gmr_placement_flags,
170 .busy_placement = gmr_vram_placement_flags
173 struct ttm_placement vmw_mob_placement = {
175 .num_busy_placement = 1,
176 .placement = &mob_placement_flags,
177 .busy_placement = &mob_placement_flags
180 struct ttm_placement vmw_nonfixed_placement = {
182 .placement = nonfixed_placement_flags,
183 .num_busy_placement = 1,
184 .busy_placement = &sys_placement_flags
188 struct ttm_tt dma_ttm;
189 struct vmw_private *dev_priv;
194 struct vmw_sg_table vsgt;
195 uint64_t sg_alloc_size;
200 const size_t vmw_tt_size = sizeof(struct vmw_ttm_tt);
203 * __vmw_piter_non_sg_next: Helper functions to advance
204 * a struct vmw_piter iterator.
206 * @viter: Pointer to the iterator.
208 * These functions return false if past the end of the list,
209 * true otherwise. Functions are selected depending on the current
212 static bool __vmw_piter_non_sg_next(struct vmw_piter *viter)
214 return ++(viter->i) < viter->num_pages;
217 static bool __vmw_piter_sg_next(struct vmw_piter *viter)
219 bool ret = __vmw_piter_non_sg_next(viter);
221 return __sg_page_iter_dma_next(&viter->iter) && ret;
225 static dma_addr_t __vmw_piter_dma_addr(struct vmw_piter *viter)
227 return viter->addrs[viter->i];
230 static dma_addr_t __vmw_piter_sg_addr(struct vmw_piter *viter)
232 return sg_page_iter_dma_address(&viter->iter);
237 * vmw_piter_start - Initialize a struct vmw_piter.
239 * @viter: Pointer to the iterator to initialize
240 * @vsgt: Pointer to a struct vmw_sg_table to initialize from
241 * @p_offset: Pointer offset used to update current array position
243 * Note that we're following the convention of __sg_page_iter_start, so that
244 * the iterator doesn't point to a valid page after initialization; it has
245 * to be advanced one step first.
247 void vmw_piter_start(struct vmw_piter *viter, const struct vmw_sg_table *vsgt,
248 unsigned long p_offset)
250 viter->i = p_offset - 1;
251 viter->num_pages = vsgt->num_pages;
252 viter->pages = vsgt->pages;
253 switch (vsgt->mode) {
254 case vmw_dma_alloc_coherent:
255 viter->next = &__vmw_piter_non_sg_next;
256 viter->dma_address = &__vmw_piter_dma_addr;
257 viter->addrs = vsgt->addrs;
259 case vmw_dma_map_populate:
260 case vmw_dma_map_bind:
261 viter->next = &__vmw_piter_sg_next;
262 viter->dma_address = &__vmw_piter_sg_addr;
263 __sg_page_iter_start(&viter->iter.base, vsgt->sgt->sgl,
264 vsgt->sgt->orig_nents, p_offset);
272 * vmw_ttm_unmap_from_dma - unmap device addresses previsouly mapped for
275 * @vmw_tt: Pointer to a struct vmw_ttm_backend
277 * Used to free dma mappings previously mapped by vmw_ttm_map_for_dma.
279 static void vmw_ttm_unmap_from_dma(struct vmw_ttm_tt *vmw_tt)
281 struct device *dev = vmw_tt->dev_priv->drm.dev;
283 dma_unmap_sgtable(dev, &vmw_tt->sgt, DMA_BIDIRECTIONAL, 0);
284 vmw_tt->sgt.nents = vmw_tt->sgt.orig_nents;
288 * vmw_ttm_map_for_dma - map TTM pages to get device addresses
290 * @vmw_tt: Pointer to a struct vmw_ttm_backend
292 * This function is used to get device addresses from the kernel DMA layer.
293 * However, it's violating the DMA API in that when this operation has been
294 * performed, it's illegal for the CPU to write to the pages without first
295 * unmapping the DMA mappings, or calling dma_sync_sg_for_cpu(). It is
296 * therefore only legal to call this function if we know that the function
297 * dma_sync_sg_for_cpu() is a NOP, and dma_sync_sg_for_device() is at most
298 * a CPU write buffer flush.
300 static int vmw_ttm_map_for_dma(struct vmw_ttm_tt *vmw_tt)
302 struct device *dev = vmw_tt->dev_priv->drm.dev;
304 return dma_map_sgtable(dev, &vmw_tt->sgt, DMA_BIDIRECTIONAL, 0);
308 * vmw_ttm_map_dma - Make sure TTM pages are visible to the device
310 * @vmw_tt: Pointer to a struct vmw_ttm_tt
312 * Select the correct function for and make sure the TTM pages are
313 * visible to the device. Allocate storage for the device mappings.
314 * If a mapping has already been performed, indicated by the storage
315 * pointer being non NULL, the function returns success.
317 static int vmw_ttm_map_dma(struct vmw_ttm_tt *vmw_tt)
319 struct vmw_private *dev_priv = vmw_tt->dev_priv;
320 struct ttm_mem_global *glob = vmw_mem_glob(dev_priv);
321 struct vmw_sg_table *vsgt = &vmw_tt->vsgt;
322 struct ttm_operation_ctx ctx = {
323 .interruptible = true,
326 struct vmw_piter iter;
329 static size_t sgl_size;
330 static size_t sgt_size;
335 vsgt->mode = dev_priv->map_mode;
336 vsgt->pages = vmw_tt->dma_ttm.pages;
337 vsgt->num_pages = vmw_tt->dma_ttm.num_pages;
338 vsgt->addrs = vmw_tt->dma_ttm.dma_address;
339 vsgt->sgt = &vmw_tt->sgt;
341 switch (dev_priv->map_mode) {
342 case vmw_dma_map_bind:
343 case vmw_dma_map_populate:
344 if (unlikely(!sgl_size)) {
345 sgl_size = ttm_round_pot(sizeof(struct scatterlist));
346 sgt_size = ttm_round_pot(sizeof(struct sg_table));
348 vmw_tt->sg_alloc_size = sgt_size + sgl_size * vsgt->num_pages;
349 ret = ttm_mem_global_alloc(glob, vmw_tt->sg_alloc_size, &ctx);
350 if (unlikely(ret != 0))
353 ret = sg_alloc_table_from_pages_segment(
354 &vmw_tt->sgt, vsgt->pages, vsgt->num_pages, 0,
355 (unsigned long)vsgt->num_pages << PAGE_SHIFT,
356 dma_get_max_seg_size(dev_priv->drm.dev), GFP_KERNEL);
358 goto out_sg_alloc_fail;
360 if (vsgt->num_pages > vmw_tt->sgt.orig_nents) {
361 uint64_t over_alloc =
362 sgl_size * (vsgt->num_pages -
363 vmw_tt->sgt.orig_nents);
365 ttm_mem_global_free(glob, over_alloc);
366 vmw_tt->sg_alloc_size -= over_alloc;
369 ret = vmw_ttm_map_for_dma(vmw_tt);
370 if (unlikely(ret != 0))
378 old = ~((dma_addr_t) 0);
379 vmw_tt->vsgt.num_regions = 0;
380 for (vmw_piter_start(&iter, vsgt, 0); vmw_piter_next(&iter);) {
381 dma_addr_t cur = vmw_piter_dma_addr(&iter);
383 if (cur != old + PAGE_SIZE)
384 vmw_tt->vsgt.num_regions++;
388 vmw_tt->mapped = true;
392 sg_free_table(vmw_tt->vsgt.sgt);
393 vmw_tt->vsgt.sgt = NULL;
395 ttm_mem_global_free(glob, vmw_tt->sg_alloc_size);
400 * vmw_ttm_unmap_dma - Tear down any TTM page device mappings
402 * @vmw_tt: Pointer to a struct vmw_ttm_tt
404 * Tear down any previously set up device DMA mappings and free
405 * any storage space allocated for them. If there are no mappings set up,
406 * this function is a NOP.
408 static void vmw_ttm_unmap_dma(struct vmw_ttm_tt *vmw_tt)
410 struct vmw_private *dev_priv = vmw_tt->dev_priv;
412 if (!vmw_tt->vsgt.sgt)
415 switch (dev_priv->map_mode) {
416 case vmw_dma_map_bind:
417 case vmw_dma_map_populate:
418 vmw_ttm_unmap_from_dma(vmw_tt);
419 sg_free_table(vmw_tt->vsgt.sgt);
420 vmw_tt->vsgt.sgt = NULL;
421 ttm_mem_global_free(vmw_mem_glob(dev_priv),
422 vmw_tt->sg_alloc_size);
427 vmw_tt->mapped = false;
431 * vmw_bo_sg_table - Return a struct vmw_sg_table object for a
434 * @bo: Pointer to a struct ttm_buffer_object
436 * Returns a pointer to a struct vmw_sg_table object. The object should
437 * not be freed after use.
438 * Note that for the device addresses to be valid, the buffer object must
439 * either be reserved or pinned.
441 const struct vmw_sg_table *vmw_bo_sg_table(struct ttm_buffer_object *bo)
443 struct vmw_ttm_tt *vmw_tt =
444 container_of(bo->ttm, struct vmw_ttm_tt, dma_ttm);
446 return &vmw_tt->vsgt;
450 static int vmw_ttm_bind(struct ttm_device *bdev,
451 struct ttm_tt *ttm, struct ttm_resource *bo_mem)
453 struct vmw_ttm_tt *vmw_be =
454 container_of(ttm, struct vmw_ttm_tt, dma_ttm);
463 ret = vmw_ttm_map_dma(vmw_be);
464 if (unlikely(ret != 0))
467 vmw_be->gmr_id = bo_mem->start;
468 vmw_be->mem_type = bo_mem->mem_type;
470 switch (bo_mem->mem_type) {
472 ret = vmw_gmr_bind(vmw_be->dev_priv, &vmw_be->vsgt,
473 ttm->num_pages, vmw_be->gmr_id);
476 if (unlikely(vmw_be->mob == NULL)) {
478 vmw_mob_create(ttm->num_pages);
479 if (unlikely(vmw_be->mob == NULL))
483 ret = vmw_mob_bind(vmw_be->dev_priv, vmw_be->mob,
484 &vmw_be->vsgt, ttm->num_pages,
490 vmw_be->bound = true;
494 static void vmw_ttm_unbind(struct ttm_device *bdev,
497 struct vmw_ttm_tt *vmw_be =
498 container_of(ttm, struct vmw_ttm_tt, dma_ttm);
503 switch (vmw_be->mem_type) {
505 vmw_gmr_unbind(vmw_be->dev_priv, vmw_be->gmr_id);
508 vmw_mob_unbind(vmw_be->dev_priv, vmw_be->mob);
514 if (vmw_be->dev_priv->map_mode == vmw_dma_map_bind)
515 vmw_ttm_unmap_dma(vmw_be);
516 vmw_be->bound = false;
520 static void vmw_ttm_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
522 struct vmw_ttm_tt *vmw_be =
523 container_of(ttm, struct vmw_ttm_tt, dma_ttm);
525 vmw_ttm_unbind(bdev, ttm);
526 ttm_tt_destroy_common(bdev, ttm);
527 vmw_ttm_unmap_dma(vmw_be);
528 if (vmw_be->dev_priv->map_mode == vmw_dma_alloc_coherent)
529 ttm_tt_fini(&vmw_be->dma_ttm);
534 vmw_mob_destroy(vmw_be->mob);
540 static int vmw_ttm_populate(struct ttm_device *bdev,
541 struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
546 /* TODO: maybe completely drop this ? */
547 if (ttm_tt_is_populated(ttm))
550 ret = ttm_pool_alloc(&bdev->pool, ttm, ctx);
554 for (i = 0; i < ttm->num_pages; ++i) {
555 ret = ttm_mem_global_alloc_page(&ttm_mem_glob, ttm->pages[i],
564 ttm_mem_global_free_page(&ttm_mem_glob, ttm->pages[i],
566 ttm_pool_free(&bdev->pool, ttm);
570 static void vmw_ttm_unpopulate(struct ttm_device *bdev,
573 struct vmw_ttm_tt *vmw_tt = container_of(ttm, struct vmw_ttm_tt,
578 vmw_mob_destroy(vmw_tt->mob);
582 vmw_ttm_unmap_dma(vmw_tt);
584 for (i = 0; i < ttm->num_pages; ++i)
585 ttm_mem_global_free_page(&ttm_mem_glob, ttm->pages[i],
588 ttm_pool_free(&bdev->pool, ttm);
591 static struct ttm_tt *vmw_ttm_tt_create(struct ttm_buffer_object *bo,
594 struct vmw_ttm_tt *vmw_be;
597 vmw_be = kzalloc(sizeof(*vmw_be), GFP_KERNEL);
601 vmw_be->dev_priv = container_of(bo->bdev, struct vmw_private, bdev);
604 if (vmw_be->dev_priv->map_mode == vmw_dma_alloc_coherent)
605 ret = ttm_sg_tt_init(&vmw_be->dma_ttm, bo, page_flags,
608 ret = ttm_tt_init(&vmw_be->dma_ttm, bo, page_flags,
610 if (unlikely(ret != 0))
613 return &vmw_be->dma_ttm;
619 static void vmw_evict_flags(struct ttm_buffer_object *bo,
620 struct ttm_placement *placement)
622 *placement = vmw_sys_placement;
625 static int vmw_ttm_io_mem_reserve(struct ttm_device *bdev, struct ttm_resource *mem)
627 struct vmw_private *dev_priv = container_of(bdev, struct vmw_private, bdev);
629 switch (mem->mem_type) {
635 mem->bus.offset = (mem->start << PAGE_SHIFT) +
636 dev_priv->vram_start;
637 mem->bus.is_iomem = true;
638 mem->bus.caching = ttm_cached;
647 * vmw_move_notify - TTM move_notify_callback
649 * @bo: The TTM buffer object about to move.
650 * @old_mem: The old memory where we move from
651 * @new_mem: The struct ttm_resource indicating to what memory
652 * region the move is taking place.
654 * Calls move_notify for all subsystems needing it.
655 * (currently only resources).
657 static void vmw_move_notify(struct ttm_buffer_object *bo,
658 struct ttm_resource *old_mem,
659 struct ttm_resource *new_mem)
661 vmw_bo_move_notify(bo, new_mem);
662 vmw_query_move_notify(bo, old_mem, new_mem);
667 * vmw_swap_notify - TTM move_notify_callback
669 * @bo: The TTM buffer object about to be swapped out.
671 static void vmw_swap_notify(struct ttm_buffer_object *bo)
673 vmw_bo_swap_notify(bo);
674 (void) ttm_bo_wait(bo, false, false);
677 static int vmw_move(struct ttm_buffer_object *bo,
679 struct ttm_operation_ctx *ctx,
680 struct ttm_resource *new_mem,
681 struct ttm_place *hop)
683 struct ttm_resource_manager *old_man = ttm_manager_type(bo->bdev, bo->resource->mem_type);
684 struct ttm_resource_manager *new_man = ttm_manager_type(bo->bdev, new_mem->mem_type);
687 if (new_man->use_tt && new_mem->mem_type != TTM_PL_SYSTEM) {
688 ret = vmw_ttm_bind(bo->bdev, bo->ttm, new_mem);
693 vmw_move_notify(bo, bo->resource, new_mem);
695 if (old_man->use_tt && new_man->use_tt) {
696 if (bo->resource->mem_type == TTM_PL_SYSTEM) {
697 ttm_bo_move_null(bo, new_mem);
700 ret = ttm_bo_wait_ctx(bo, ctx);
704 vmw_ttm_unbind(bo->bdev, bo->ttm);
705 ttm_resource_free(bo, &bo->resource);
706 ttm_bo_assign_mem(bo, new_mem);
709 ret = ttm_bo_move_memcpy(bo, ctx, new_mem);
715 vmw_move_notify(bo, new_mem, bo->resource);
719 struct ttm_device_funcs vmw_bo_driver = {
720 .ttm_tt_create = &vmw_ttm_tt_create,
721 .ttm_tt_populate = &vmw_ttm_populate,
722 .ttm_tt_unpopulate = &vmw_ttm_unpopulate,
723 .ttm_tt_destroy = &vmw_ttm_destroy,
724 .eviction_valuable = ttm_bo_eviction_valuable,
725 .evict_flags = vmw_evict_flags,
727 .swap_notify = vmw_swap_notify,
728 .io_mem_reserve = &vmw_ttm_io_mem_reserve,
731 int vmw_bo_create_and_populate(struct vmw_private *dev_priv,
732 unsigned long bo_size,
733 struct ttm_buffer_object **bo_p)
735 struct ttm_operation_ctx ctx = {
736 .interruptible = false,
739 struct ttm_buffer_object *bo;
742 ret = vmw_bo_create_kernel(dev_priv, bo_size,
745 if (unlikely(ret != 0))
748 ret = ttm_bo_reserve(bo, false, true, NULL);
750 ret = vmw_ttm_populate(bo->bdev, bo->ttm, &ctx);
751 if (likely(ret == 0)) {
752 struct vmw_ttm_tt *vmw_tt =
753 container_of(bo->ttm, struct vmw_ttm_tt, dma_ttm);
754 ret = vmw_ttm_map_dma(vmw_tt);
757 ttm_bo_unreserve(bo);
759 if (likely(ret == 0))