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>
31 #include <drm/ttm/ttm_page_alloc.h>
33 static const struct ttm_place vram_placement_flags = {
36 .mem_type = TTM_PL_VRAM,
37 .flags = TTM_PL_FLAG_CACHED
40 static const struct ttm_place vram_ne_placement_flags = {
43 .mem_type = TTM_PL_VRAM,
44 .flags = TTM_PL_FLAG_CACHED | TTM_PL_FLAG_NO_EVICT
47 static const struct ttm_place sys_placement_flags = {
50 .mem_type = TTM_PL_SYSTEM,
51 .flags = TTM_PL_FLAG_CACHED
54 static const struct ttm_place sys_ne_placement_flags = {
57 .mem_type = TTM_PL_SYSTEM,
58 .flags = TTM_PL_FLAG_CACHED | TTM_PL_FLAG_NO_EVICT
61 static const struct ttm_place gmr_placement_flags = {
64 .mem_type = VMW_PL_GMR,
65 .flags = TTM_PL_FLAG_CACHED
68 static const struct ttm_place gmr_ne_placement_flags = {
71 .mem_type = VMW_PL_GMR,
72 .flags = TTM_PL_FLAG_CACHED | TTM_PL_FLAG_NO_EVICT
75 static const struct ttm_place mob_placement_flags = {
78 .mem_type = VMW_PL_MOB,
79 .flags = TTM_PL_FLAG_CACHED
82 static const struct ttm_place mob_ne_placement_flags = {
85 .mem_type = VMW_PL_MOB,
86 .flags = TTM_PL_FLAG_CACHED | TTM_PL_FLAG_NO_EVICT
89 struct ttm_placement vmw_vram_placement = {
91 .placement = &vram_placement_flags,
92 .num_busy_placement = 1,
93 .busy_placement = &vram_placement_flags
96 static const struct ttm_place vram_gmr_placement_flags[] = {
100 .mem_type = TTM_PL_VRAM,
101 .flags = TTM_PL_FLAG_CACHED
105 .mem_type = VMW_PL_GMR,
106 .flags = TTM_PL_FLAG_CACHED
110 static const struct ttm_place gmr_vram_placement_flags[] = {
114 .mem_type = VMW_PL_GMR,
115 .flags = TTM_PL_FLAG_CACHED
119 .mem_type = TTM_PL_VRAM,
120 .flags = TTM_PL_FLAG_CACHED
124 struct ttm_placement vmw_vram_gmr_placement = {
126 .placement = vram_gmr_placement_flags,
127 .num_busy_placement = 1,
128 .busy_placement = &gmr_placement_flags
131 static const struct ttm_place vram_gmr_ne_placement_flags[] = {
135 .mem_type = TTM_PL_VRAM,
136 .flags = TTM_PL_FLAG_CACHED |
141 .mem_type = VMW_PL_GMR,
142 .flags = TTM_PL_FLAG_CACHED |
147 struct ttm_placement vmw_vram_gmr_ne_placement = {
149 .placement = vram_gmr_ne_placement_flags,
150 .num_busy_placement = 1,
151 .busy_placement = &gmr_ne_placement_flags
154 struct ttm_placement vmw_vram_sys_placement = {
156 .placement = &vram_placement_flags,
157 .num_busy_placement = 1,
158 .busy_placement = &sys_placement_flags
161 struct ttm_placement vmw_vram_ne_placement = {
163 .placement = &vram_ne_placement_flags,
164 .num_busy_placement = 1,
165 .busy_placement = &vram_ne_placement_flags
168 struct ttm_placement vmw_sys_placement = {
170 .placement = &sys_placement_flags,
171 .num_busy_placement = 1,
172 .busy_placement = &sys_placement_flags
175 struct ttm_placement vmw_sys_ne_placement = {
177 .placement = &sys_ne_placement_flags,
178 .num_busy_placement = 1,
179 .busy_placement = &sys_ne_placement_flags
182 static const struct ttm_place evictable_placement_flags[] = {
186 .mem_type = TTM_PL_SYSTEM,
187 .flags = TTM_PL_FLAG_CACHED
191 .mem_type = TTM_PL_VRAM,
192 .flags = TTM_PL_FLAG_CACHED
196 .mem_type = VMW_PL_GMR,
197 .flags = TTM_PL_FLAG_CACHED
201 .mem_type = VMW_PL_MOB,
202 .flags = TTM_PL_FLAG_CACHED
206 static const struct ttm_place nonfixed_placement_flags[] = {
210 .mem_type = TTM_PL_SYSTEM,
211 .flags = TTM_PL_FLAG_CACHED
215 .mem_type = VMW_PL_GMR,
216 .flags = TTM_PL_FLAG_CACHED
220 .mem_type = VMW_PL_MOB,
221 .flags = TTM_PL_FLAG_CACHED
225 struct ttm_placement vmw_evictable_placement = {
227 .placement = evictable_placement_flags,
228 .num_busy_placement = 1,
229 .busy_placement = &sys_placement_flags
232 struct ttm_placement vmw_srf_placement = {
234 .num_busy_placement = 2,
235 .placement = &gmr_placement_flags,
236 .busy_placement = gmr_vram_placement_flags
239 struct ttm_placement vmw_mob_placement = {
241 .num_busy_placement = 1,
242 .placement = &mob_placement_flags,
243 .busy_placement = &mob_placement_flags
246 struct ttm_placement vmw_mob_ne_placement = {
248 .num_busy_placement = 1,
249 .placement = &mob_ne_placement_flags,
250 .busy_placement = &mob_ne_placement_flags
253 struct ttm_placement vmw_nonfixed_placement = {
255 .placement = nonfixed_placement_flags,
256 .num_busy_placement = 1,
257 .busy_placement = &sys_placement_flags
261 struct ttm_dma_tt dma_ttm;
262 struct vmw_private *dev_priv;
267 struct vmw_sg_table vsgt;
268 uint64_t sg_alloc_size;
273 const size_t vmw_tt_size = sizeof(struct vmw_ttm_tt);
276 * Helper functions to advance a struct vmw_piter iterator.
278 * @viter: Pointer to the iterator.
280 * These functions return false if past the end of the list,
281 * true otherwise. Functions are selected depending on the current
284 static bool __vmw_piter_non_sg_next(struct vmw_piter *viter)
286 return ++(viter->i) < viter->num_pages;
289 static bool __vmw_piter_sg_next(struct vmw_piter *viter)
291 bool ret = __vmw_piter_non_sg_next(viter);
293 return __sg_page_iter_dma_next(&viter->iter) && ret;
298 * Helper functions to return a pointer to the current page.
300 * @viter: Pointer to the iterator
302 * These functions return a pointer to the page currently
303 * pointed to by @viter. Functions are selected depending on the
304 * current mapping mode.
306 static struct page *__vmw_piter_non_sg_page(struct vmw_piter *viter)
308 return viter->pages[viter->i];
312 * Helper functions to return the DMA address of the current page.
314 * @viter: Pointer to the iterator
316 * These functions return the DMA address of the page currently
317 * pointed to by @viter. Functions are selected depending on the
318 * current mapping mode.
320 static dma_addr_t __vmw_piter_phys_addr(struct vmw_piter *viter)
322 return page_to_phys(viter->pages[viter->i]);
325 static dma_addr_t __vmw_piter_dma_addr(struct vmw_piter *viter)
327 return viter->addrs[viter->i];
330 static dma_addr_t __vmw_piter_sg_addr(struct vmw_piter *viter)
332 return sg_page_iter_dma_address(&viter->iter);
337 * vmw_piter_start - Initialize a struct vmw_piter.
339 * @viter: Pointer to the iterator to initialize
340 * @vsgt: Pointer to a struct vmw_sg_table to initialize from
342 * Note that we're following the convention of __sg_page_iter_start, so that
343 * the iterator doesn't point to a valid page after initialization; it has
344 * to be advanced one step first.
346 void vmw_piter_start(struct vmw_piter *viter, const struct vmw_sg_table *vsgt,
347 unsigned long p_offset)
349 viter->i = p_offset - 1;
350 viter->num_pages = vsgt->num_pages;
351 viter->page = &__vmw_piter_non_sg_page;
352 viter->pages = vsgt->pages;
353 switch (vsgt->mode) {
355 viter->next = &__vmw_piter_non_sg_next;
356 viter->dma_address = &__vmw_piter_phys_addr;
358 case vmw_dma_alloc_coherent:
359 viter->next = &__vmw_piter_non_sg_next;
360 viter->dma_address = &__vmw_piter_dma_addr;
361 viter->addrs = vsgt->addrs;
363 case vmw_dma_map_populate:
364 case vmw_dma_map_bind:
365 viter->next = &__vmw_piter_sg_next;
366 viter->dma_address = &__vmw_piter_sg_addr;
367 __sg_page_iter_start(&viter->iter.base, vsgt->sgt->sgl,
368 vsgt->sgt->orig_nents, p_offset);
376 * vmw_ttm_unmap_from_dma - unmap device addresses previsouly mapped for
379 * @vmw_tt: Pointer to a struct vmw_ttm_backend
381 * Used to free dma mappings previously mapped by vmw_ttm_map_for_dma.
383 static void vmw_ttm_unmap_from_dma(struct vmw_ttm_tt *vmw_tt)
385 struct device *dev = vmw_tt->dev_priv->dev->dev;
387 dma_unmap_sgtable(dev, &vmw_tt->sgt, DMA_BIDIRECTIONAL, 0);
388 vmw_tt->sgt.nents = vmw_tt->sgt.orig_nents;
392 * vmw_ttm_map_for_dma - map TTM pages to get device addresses
394 * @vmw_tt: Pointer to a struct vmw_ttm_backend
396 * This function is used to get device addresses from the kernel DMA layer.
397 * However, it's violating the DMA API in that when this operation has been
398 * performed, it's illegal for the CPU to write to the pages without first
399 * unmapping the DMA mappings, or calling dma_sync_sg_for_cpu(). It is
400 * therefore only legal to call this function if we know that the function
401 * dma_sync_sg_for_cpu() is a NOP, and dma_sync_sg_for_device() is at most
402 * a CPU write buffer flush.
404 static int vmw_ttm_map_for_dma(struct vmw_ttm_tt *vmw_tt)
406 struct device *dev = vmw_tt->dev_priv->dev->dev;
408 return dma_map_sgtable(dev, &vmw_tt->sgt, DMA_BIDIRECTIONAL, 0);
412 * vmw_ttm_map_dma - Make sure TTM pages are visible to the device
414 * @vmw_tt: Pointer to a struct vmw_ttm_tt
416 * Select the correct function for and make sure the TTM pages are
417 * visible to the device. Allocate storage for the device mappings.
418 * If a mapping has already been performed, indicated by the storage
419 * pointer being non NULL, the function returns success.
421 static int vmw_ttm_map_dma(struct vmw_ttm_tt *vmw_tt)
423 struct vmw_private *dev_priv = vmw_tt->dev_priv;
424 struct ttm_mem_global *glob = vmw_mem_glob(dev_priv);
425 struct vmw_sg_table *vsgt = &vmw_tt->vsgt;
426 struct ttm_operation_ctx ctx = {
427 .interruptible = true,
430 struct vmw_piter iter;
433 static size_t sgl_size;
434 static size_t sgt_size;
435 struct scatterlist *sg;
440 vsgt->mode = dev_priv->map_mode;
441 vsgt->pages = vmw_tt->dma_ttm.ttm.pages;
442 vsgt->num_pages = vmw_tt->dma_ttm.ttm.num_pages;
443 vsgt->addrs = vmw_tt->dma_ttm.dma_address;
444 vsgt->sgt = &vmw_tt->sgt;
446 switch (dev_priv->map_mode) {
447 case vmw_dma_map_bind:
448 case vmw_dma_map_populate:
449 if (unlikely(!sgl_size)) {
450 sgl_size = ttm_round_pot(sizeof(struct scatterlist));
451 sgt_size = ttm_round_pot(sizeof(struct sg_table));
453 vmw_tt->sg_alloc_size = sgt_size + sgl_size * vsgt->num_pages;
454 ret = ttm_mem_global_alloc(glob, vmw_tt->sg_alloc_size, &ctx);
455 if (unlikely(ret != 0))
458 sg = __sg_alloc_table_from_pages(&vmw_tt->sgt, vsgt->pages,
460 (unsigned long) vsgt->num_pages << PAGE_SHIFT,
461 dma_get_max_seg_size(dev_priv->dev->dev),
462 NULL, 0, GFP_KERNEL);
465 goto out_sg_alloc_fail;
468 if (vsgt->num_pages > vmw_tt->sgt.orig_nents) {
469 uint64_t over_alloc =
470 sgl_size * (vsgt->num_pages -
471 vmw_tt->sgt.orig_nents);
473 ttm_mem_global_free(glob, over_alloc);
474 vmw_tt->sg_alloc_size -= over_alloc;
477 ret = vmw_ttm_map_for_dma(vmw_tt);
478 if (unlikely(ret != 0))
486 old = ~((dma_addr_t) 0);
487 vmw_tt->vsgt.num_regions = 0;
488 for (vmw_piter_start(&iter, vsgt, 0); vmw_piter_next(&iter);) {
489 dma_addr_t cur = vmw_piter_dma_addr(&iter);
491 if (cur != old + PAGE_SIZE)
492 vmw_tt->vsgt.num_regions++;
496 vmw_tt->mapped = true;
500 sg_free_table(vmw_tt->vsgt.sgt);
501 vmw_tt->vsgt.sgt = NULL;
503 ttm_mem_global_free(glob, vmw_tt->sg_alloc_size);
508 * vmw_ttm_unmap_dma - Tear down any TTM page device mappings
510 * @vmw_tt: Pointer to a struct vmw_ttm_tt
512 * Tear down any previously set up device DMA mappings and free
513 * any storage space allocated for them. If there are no mappings set up,
514 * this function is a NOP.
516 static void vmw_ttm_unmap_dma(struct vmw_ttm_tt *vmw_tt)
518 struct vmw_private *dev_priv = vmw_tt->dev_priv;
520 if (!vmw_tt->vsgt.sgt)
523 switch (dev_priv->map_mode) {
524 case vmw_dma_map_bind:
525 case vmw_dma_map_populate:
526 vmw_ttm_unmap_from_dma(vmw_tt);
527 sg_free_table(vmw_tt->vsgt.sgt);
528 vmw_tt->vsgt.sgt = NULL;
529 ttm_mem_global_free(vmw_mem_glob(dev_priv),
530 vmw_tt->sg_alloc_size);
535 vmw_tt->mapped = false;
539 * vmw_bo_sg_table - Return a struct vmw_sg_table object for a
542 * @bo: Pointer to a struct ttm_buffer_object
544 * Returns a pointer to a struct vmw_sg_table object. The object should
545 * not be freed after use.
546 * Note that for the device addresses to be valid, the buffer object must
547 * either be reserved or pinned.
549 const struct vmw_sg_table *vmw_bo_sg_table(struct ttm_buffer_object *bo)
551 struct vmw_ttm_tt *vmw_tt =
552 container_of(bo->ttm, struct vmw_ttm_tt, dma_ttm.ttm);
554 return &vmw_tt->vsgt;
558 static int vmw_ttm_bind(struct ttm_bo_device *bdev,
559 struct ttm_tt *ttm, struct ttm_resource *bo_mem)
561 struct vmw_ttm_tt *vmw_be =
562 container_of(ttm, struct vmw_ttm_tt, dma_ttm.ttm);
571 ret = vmw_ttm_map_dma(vmw_be);
572 if (unlikely(ret != 0))
575 vmw_be->gmr_id = bo_mem->start;
576 vmw_be->mem_type = bo_mem->mem_type;
578 switch (bo_mem->mem_type) {
580 ret = vmw_gmr_bind(vmw_be->dev_priv, &vmw_be->vsgt,
581 ttm->num_pages, vmw_be->gmr_id);
584 if (unlikely(vmw_be->mob == NULL)) {
586 vmw_mob_create(ttm->num_pages);
587 if (unlikely(vmw_be->mob == NULL))
591 ret = vmw_mob_bind(vmw_be->dev_priv, vmw_be->mob,
592 &vmw_be->vsgt, ttm->num_pages,
598 vmw_be->bound = true;
602 static void vmw_ttm_unbind(struct ttm_bo_device *bdev,
605 struct vmw_ttm_tt *vmw_be =
606 container_of(ttm, struct vmw_ttm_tt, dma_ttm.ttm);
611 switch (vmw_be->mem_type) {
613 vmw_gmr_unbind(vmw_be->dev_priv, vmw_be->gmr_id);
616 vmw_mob_unbind(vmw_be->dev_priv, vmw_be->mob);
622 if (vmw_be->dev_priv->map_mode == vmw_dma_map_bind)
623 vmw_ttm_unmap_dma(vmw_be);
624 vmw_be->bound = false;
628 static void vmw_ttm_destroy(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
630 struct vmw_ttm_tt *vmw_be =
631 container_of(ttm, struct vmw_ttm_tt, dma_ttm.ttm);
633 vmw_ttm_unbind(bdev, ttm);
634 ttm_tt_destroy_common(bdev, ttm);
635 vmw_ttm_unmap_dma(vmw_be);
636 if (vmw_be->dev_priv->map_mode == vmw_dma_alloc_coherent)
637 ttm_dma_tt_fini(&vmw_be->dma_ttm);
642 vmw_mob_destroy(vmw_be->mob);
648 static int vmw_ttm_populate(struct ttm_bo_device *bdev,
649 struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
651 struct vmw_ttm_tt *vmw_tt =
652 container_of(ttm, struct vmw_ttm_tt, dma_ttm.ttm);
653 struct vmw_private *dev_priv = vmw_tt->dev_priv;
654 struct ttm_mem_global *glob = vmw_mem_glob(dev_priv);
657 if (ttm_tt_is_populated(ttm))
660 if (dev_priv->map_mode == vmw_dma_alloc_coherent) {
662 ttm_round_pot(ttm->num_pages * sizeof(dma_addr_t));
663 ret = ttm_mem_global_alloc(glob, size, ctx);
664 if (unlikely(ret != 0))
667 ret = ttm_dma_populate(&vmw_tt->dma_ttm, dev_priv->dev->dev,
669 if (unlikely(ret != 0))
670 ttm_mem_global_free(glob, size);
672 ret = ttm_pool_populate(ttm, ctx);
677 static void vmw_ttm_unpopulate(struct ttm_bo_device *bdev,
680 struct vmw_ttm_tt *vmw_tt = container_of(ttm, struct vmw_ttm_tt,
682 struct vmw_private *dev_priv = vmw_tt->dev_priv;
683 struct ttm_mem_global *glob = vmw_mem_glob(dev_priv);
687 vmw_mob_destroy(vmw_tt->mob);
691 vmw_ttm_unmap_dma(vmw_tt);
692 if (dev_priv->map_mode == vmw_dma_alloc_coherent) {
694 ttm_round_pot(ttm->num_pages * sizeof(dma_addr_t));
696 ttm_dma_unpopulate(&vmw_tt->dma_ttm, dev_priv->dev->dev);
697 ttm_mem_global_free(glob, size);
699 ttm_pool_unpopulate(ttm);
702 static struct ttm_tt *vmw_ttm_tt_create(struct ttm_buffer_object *bo,
705 struct vmw_ttm_tt *vmw_be;
708 vmw_be = kzalloc(sizeof(*vmw_be), GFP_KERNEL);
712 vmw_be->dev_priv = container_of(bo->bdev, struct vmw_private, bdev);
715 if (vmw_be->dev_priv->map_mode == vmw_dma_alloc_coherent)
716 ret = ttm_dma_tt_init(&vmw_be->dma_ttm, bo, page_flags);
718 ret = ttm_tt_init(&vmw_be->dma_ttm.ttm, bo, page_flags);
719 if (unlikely(ret != 0))
722 return &vmw_be->dma_ttm.ttm;
728 static void vmw_evict_flags(struct ttm_buffer_object *bo,
729 struct ttm_placement *placement)
731 *placement = vmw_sys_placement;
734 static int vmw_verify_access(struct ttm_buffer_object *bo, struct file *filp)
736 struct ttm_object_file *tfile =
737 vmw_fpriv((struct drm_file *)filp->private_data)->tfile;
739 return vmw_user_bo_verify_access(bo, tfile);
742 static int vmw_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_resource *mem)
744 struct vmw_private *dev_priv = container_of(bdev, struct vmw_private, bdev);
746 switch (mem->mem_type) {
752 mem->bus.offset = (mem->start << PAGE_SHIFT) +
753 dev_priv->vram_start;
754 mem->bus.is_iomem = true;
763 * vmw_move_notify - TTM move_notify_callback
765 * @bo: The TTM buffer object about to move.
766 * @mem: The struct ttm_resource indicating to what memory
767 * region the move is taking place.
769 * Calls move_notify for all subsystems needing it.
770 * (currently only resources).
772 static void vmw_move_notify(struct ttm_buffer_object *bo,
774 struct ttm_resource *mem)
776 vmw_bo_move_notify(bo, mem);
777 vmw_query_move_notify(bo, mem);
782 * vmw_swap_notify - TTM move_notify_callback
784 * @bo: The TTM buffer object about to be swapped out.
786 static void vmw_swap_notify(struct ttm_buffer_object *bo)
788 vmw_bo_swap_notify(bo);
789 (void) ttm_bo_wait(bo, false, false);
793 struct ttm_bo_driver vmw_bo_driver = {
794 .ttm_tt_create = &vmw_ttm_tt_create,
795 .ttm_tt_populate = &vmw_ttm_populate,
796 .ttm_tt_unpopulate = &vmw_ttm_unpopulate,
797 .ttm_tt_bind = &vmw_ttm_bind,
798 .ttm_tt_unbind = &vmw_ttm_unbind,
799 .ttm_tt_destroy = &vmw_ttm_destroy,
800 .eviction_valuable = ttm_bo_eviction_valuable,
801 .evict_flags = vmw_evict_flags,
803 .verify_access = vmw_verify_access,
804 .move_notify = vmw_move_notify,
805 .swap_notify = vmw_swap_notify,
806 .io_mem_reserve = &vmw_ttm_io_mem_reserve,
809 int vmw_bo_create_and_populate(struct vmw_private *dev_priv,
810 unsigned long bo_size,
811 struct ttm_buffer_object **bo_p)
813 struct ttm_operation_ctx ctx = {
814 .interruptible = false,
817 struct ttm_buffer_object *bo;
820 ret = ttm_bo_create(&dev_priv->bdev, bo_size,
822 &vmw_sys_ne_placement,
825 if (unlikely(ret != 0))
828 ret = ttm_bo_reserve(bo, false, true, NULL);
830 ret = vmw_ttm_populate(bo->bdev, bo->ttm, &ctx);
831 if (likely(ret == 0)) {
832 struct vmw_ttm_tt *vmw_tt =
833 container_of(bo->ttm, struct vmw_ttm_tt, dma_ttm.ttm);
834 ret = vmw_ttm_map_dma(vmw_tt);
837 ttm_bo_unreserve(bo);
839 if (likely(ret == 0))