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,
40 static const struct ttm_place sys_placement_flags = {
43 .mem_type = TTM_PL_SYSTEM,
47 static const struct ttm_place gmr_placement_flags = {
50 .mem_type = VMW_PL_GMR,
54 static const struct ttm_place mob_placement_flags = {
57 .mem_type = VMW_PL_MOB,
61 struct ttm_placement vmw_vram_placement = {
63 .placement = &vram_placement_flags,
64 .num_busy_placement = 1,
65 .busy_placement = &vram_placement_flags
68 static const struct ttm_place vram_gmr_placement_flags[] = {
72 .mem_type = TTM_PL_VRAM,
77 .mem_type = VMW_PL_GMR,
82 static const struct ttm_place gmr_vram_placement_flags[] = {
86 .mem_type = VMW_PL_GMR,
91 .mem_type = TTM_PL_VRAM,
96 struct ttm_placement vmw_vram_gmr_placement = {
98 .placement = vram_gmr_placement_flags,
99 .num_busy_placement = 1,
100 .busy_placement = &gmr_placement_flags
103 struct ttm_placement vmw_vram_sys_placement = {
105 .placement = &vram_placement_flags,
106 .num_busy_placement = 1,
107 .busy_placement = &sys_placement_flags
110 struct ttm_placement vmw_sys_placement = {
112 .placement = &sys_placement_flags,
113 .num_busy_placement = 1,
114 .busy_placement = &sys_placement_flags
117 static const struct ttm_place evictable_placement_flags[] = {
121 .mem_type = TTM_PL_SYSTEM,
126 .mem_type = TTM_PL_VRAM,
131 .mem_type = VMW_PL_GMR,
136 .mem_type = VMW_PL_MOB,
141 static const struct ttm_place nonfixed_placement_flags[] = {
145 .mem_type = TTM_PL_SYSTEM,
150 .mem_type = VMW_PL_GMR,
155 .mem_type = VMW_PL_MOB,
160 struct ttm_placement vmw_evictable_placement = {
162 .placement = evictable_placement_flags,
163 .num_busy_placement = 1,
164 .busy_placement = &sys_placement_flags
167 struct ttm_placement vmw_srf_placement = {
169 .num_busy_placement = 2,
170 .placement = &gmr_placement_flags,
171 .busy_placement = gmr_vram_placement_flags
174 struct ttm_placement vmw_mob_placement = {
176 .num_busy_placement = 1,
177 .placement = &mob_placement_flags,
178 .busy_placement = &mob_placement_flags
181 struct ttm_placement vmw_nonfixed_placement = {
183 .placement = nonfixed_placement_flags,
184 .num_busy_placement = 1,
185 .busy_placement = &sys_placement_flags
189 struct ttm_dma_tt dma_ttm;
190 struct vmw_private *dev_priv;
195 struct vmw_sg_table vsgt;
196 uint64_t sg_alloc_size;
201 const size_t vmw_tt_size = sizeof(struct vmw_ttm_tt);
204 * Helper functions to advance 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;
226 * Helper functions to return a pointer to the current page.
228 * @viter: Pointer to the iterator
230 * These functions return a pointer to the page currently
231 * pointed to by @viter. Functions are selected depending on the
232 * current mapping mode.
234 static struct page *__vmw_piter_non_sg_page(struct vmw_piter *viter)
236 return viter->pages[viter->i];
240 * Helper functions to return the DMA address of the current page.
242 * @viter: Pointer to the iterator
244 * These functions return the DMA address of the page currently
245 * pointed to by @viter. Functions are selected depending on the
246 * current mapping mode.
248 static dma_addr_t __vmw_piter_phys_addr(struct vmw_piter *viter)
250 return page_to_phys(viter->pages[viter->i]);
253 static dma_addr_t __vmw_piter_dma_addr(struct vmw_piter *viter)
255 return viter->addrs[viter->i];
258 static dma_addr_t __vmw_piter_sg_addr(struct vmw_piter *viter)
260 return sg_page_iter_dma_address(&viter->iter);
265 * vmw_piter_start - Initialize a struct vmw_piter.
267 * @viter: Pointer to the iterator to initialize
268 * @vsgt: Pointer to a struct vmw_sg_table to initialize from
270 * Note that we're following the convention of __sg_page_iter_start, so that
271 * the iterator doesn't point to a valid page after initialization; it has
272 * to be advanced one step first.
274 void vmw_piter_start(struct vmw_piter *viter, const struct vmw_sg_table *vsgt,
275 unsigned long p_offset)
277 viter->i = p_offset - 1;
278 viter->num_pages = vsgt->num_pages;
279 viter->page = &__vmw_piter_non_sg_page;
280 viter->pages = vsgt->pages;
281 switch (vsgt->mode) {
283 viter->next = &__vmw_piter_non_sg_next;
284 viter->dma_address = &__vmw_piter_phys_addr;
286 case vmw_dma_alloc_coherent:
287 viter->next = &__vmw_piter_non_sg_next;
288 viter->dma_address = &__vmw_piter_dma_addr;
289 viter->addrs = vsgt->addrs;
291 case vmw_dma_map_populate:
292 case vmw_dma_map_bind:
293 viter->next = &__vmw_piter_sg_next;
294 viter->dma_address = &__vmw_piter_sg_addr;
295 __sg_page_iter_start(&viter->iter.base, vsgt->sgt->sgl,
296 vsgt->sgt->orig_nents, p_offset);
304 * vmw_ttm_unmap_from_dma - unmap device addresses previsouly mapped for
307 * @vmw_tt: Pointer to a struct vmw_ttm_backend
309 * Used to free dma mappings previously mapped by vmw_ttm_map_for_dma.
311 static void vmw_ttm_unmap_from_dma(struct vmw_ttm_tt *vmw_tt)
313 struct device *dev = vmw_tt->dev_priv->dev->dev;
315 dma_unmap_sg(dev, vmw_tt->sgt.sgl, vmw_tt->sgt.nents,
317 vmw_tt->sgt.nents = vmw_tt->sgt.orig_nents;
321 * vmw_ttm_map_for_dma - map TTM pages to get device addresses
323 * @vmw_tt: Pointer to a struct vmw_ttm_backend
325 * This function is used to get device addresses from the kernel DMA layer.
326 * However, it's violating the DMA API in that when this operation has been
327 * performed, it's illegal for the CPU to write to the pages without first
328 * unmapping the DMA mappings, or calling dma_sync_sg_for_cpu(). It is
329 * therefore only legal to call this function if we know that the function
330 * dma_sync_sg_for_cpu() is a NOP, and dma_sync_sg_for_device() is at most
331 * a CPU write buffer flush.
333 static int vmw_ttm_map_for_dma(struct vmw_ttm_tt *vmw_tt)
335 struct device *dev = vmw_tt->dev_priv->dev->dev;
338 ret = dma_map_sg(dev, vmw_tt->sgt.sgl, vmw_tt->sgt.orig_nents,
340 if (unlikely(ret == 0))
343 vmw_tt->sgt.nents = ret;
349 * vmw_ttm_map_dma - Make sure TTM pages are visible to the device
351 * @vmw_tt: Pointer to a struct vmw_ttm_tt
353 * Select the correct function for and make sure the TTM pages are
354 * visible to the device. Allocate storage for the device mappings.
355 * If a mapping has already been performed, indicated by the storage
356 * pointer being non NULL, the function returns success.
358 static int vmw_ttm_map_dma(struct vmw_ttm_tt *vmw_tt)
360 struct vmw_private *dev_priv = vmw_tt->dev_priv;
361 struct ttm_mem_global *glob = vmw_mem_glob(dev_priv);
362 struct vmw_sg_table *vsgt = &vmw_tt->vsgt;
363 struct ttm_operation_ctx ctx = {
364 .interruptible = true,
367 struct vmw_piter iter;
370 static size_t sgl_size;
371 static size_t sgt_size;
376 vsgt->mode = dev_priv->map_mode;
377 vsgt->pages = vmw_tt->dma_ttm.ttm.pages;
378 vsgt->num_pages = vmw_tt->dma_ttm.ttm.num_pages;
379 vsgt->addrs = vmw_tt->dma_ttm.dma_address;
380 vsgt->sgt = &vmw_tt->sgt;
382 switch (dev_priv->map_mode) {
383 case vmw_dma_map_bind:
384 case vmw_dma_map_populate:
385 if (unlikely(!sgl_size)) {
386 sgl_size = ttm_round_pot(sizeof(struct scatterlist));
387 sgt_size = ttm_round_pot(sizeof(struct sg_table));
389 vmw_tt->sg_alloc_size = sgt_size + sgl_size * vsgt->num_pages;
390 ret = ttm_mem_global_alloc(glob, vmw_tt->sg_alloc_size, &ctx);
391 if (unlikely(ret != 0))
394 ret = __sg_alloc_table_from_pages
395 (&vmw_tt->sgt, vsgt->pages, vsgt->num_pages, 0,
396 (unsigned long) vsgt->num_pages << PAGE_SHIFT,
397 dma_get_max_seg_size(dev_priv->dev->dev),
399 if (unlikely(ret != 0))
400 goto out_sg_alloc_fail;
402 if (vsgt->num_pages > vmw_tt->sgt.nents) {
403 uint64_t over_alloc =
404 sgl_size * (vsgt->num_pages -
407 ttm_mem_global_free(glob, over_alloc);
408 vmw_tt->sg_alloc_size -= over_alloc;
411 ret = vmw_ttm_map_for_dma(vmw_tt);
412 if (unlikely(ret != 0))
420 old = ~((dma_addr_t) 0);
421 vmw_tt->vsgt.num_regions = 0;
422 for (vmw_piter_start(&iter, vsgt, 0); vmw_piter_next(&iter);) {
423 dma_addr_t cur = vmw_piter_dma_addr(&iter);
425 if (cur != old + PAGE_SIZE)
426 vmw_tt->vsgt.num_regions++;
430 vmw_tt->mapped = true;
434 sg_free_table(vmw_tt->vsgt.sgt);
435 vmw_tt->vsgt.sgt = NULL;
437 ttm_mem_global_free(glob, vmw_tt->sg_alloc_size);
442 * vmw_ttm_unmap_dma - Tear down any TTM page device mappings
444 * @vmw_tt: Pointer to a struct vmw_ttm_tt
446 * Tear down any previously set up device DMA mappings and free
447 * any storage space allocated for them. If there are no mappings set up,
448 * this function is a NOP.
450 static void vmw_ttm_unmap_dma(struct vmw_ttm_tt *vmw_tt)
452 struct vmw_private *dev_priv = vmw_tt->dev_priv;
454 if (!vmw_tt->vsgt.sgt)
457 switch (dev_priv->map_mode) {
458 case vmw_dma_map_bind:
459 case vmw_dma_map_populate:
460 vmw_ttm_unmap_from_dma(vmw_tt);
461 sg_free_table(vmw_tt->vsgt.sgt);
462 vmw_tt->vsgt.sgt = NULL;
463 ttm_mem_global_free(vmw_mem_glob(dev_priv),
464 vmw_tt->sg_alloc_size);
469 vmw_tt->mapped = false;
473 * vmw_bo_sg_table - Return a struct vmw_sg_table object for a
476 * @bo: Pointer to a struct ttm_buffer_object
478 * Returns a pointer to a struct vmw_sg_table object. The object should
479 * not be freed after use.
480 * Note that for the device addresses to be valid, the buffer object must
481 * either be reserved or pinned.
483 const struct vmw_sg_table *vmw_bo_sg_table(struct ttm_buffer_object *bo)
485 struct vmw_ttm_tt *vmw_tt =
486 container_of(bo->ttm, struct vmw_ttm_tt, dma_ttm.ttm);
488 return &vmw_tt->vsgt;
492 static int vmw_ttm_bind(struct ttm_bo_device *bdev,
493 struct ttm_tt *ttm, struct ttm_resource *bo_mem)
495 struct vmw_ttm_tt *vmw_be =
496 container_of(ttm, struct vmw_ttm_tt, dma_ttm.ttm);
505 ret = vmw_ttm_map_dma(vmw_be);
506 if (unlikely(ret != 0))
509 vmw_be->gmr_id = bo_mem->start;
510 vmw_be->mem_type = bo_mem->mem_type;
512 switch (bo_mem->mem_type) {
514 ret = vmw_gmr_bind(vmw_be->dev_priv, &vmw_be->vsgt,
515 ttm->num_pages, vmw_be->gmr_id);
518 if (unlikely(vmw_be->mob == NULL)) {
520 vmw_mob_create(ttm->num_pages);
521 if (unlikely(vmw_be->mob == NULL))
525 ret = vmw_mob_bind(vmw_be->dev_priv, vmw_be->mob,
526 &vmw_be->vsgt, ttm->num_pages,
532 vmw_be->bound = true;
536 static void vmw_ttm_unbind(struct ttm_bo_device *bdev,
539 struct vmw_ttm_tt *vmw_be =
540 container_of(ttm, struct vmw_ttm_tt, dma_ttm.ttm);
545 switch (vmw_be->mem_type) {
547 vmw_gmr_unbind(vmw_be->dev_priv, vmw_be->gmr_id);
550 vmw_mob_unbind(vmw_be->dev_priv, vmw_be->mob);
556 if (vmw_be->dev_priv->map_mode == vmw_dma_map_bind)
557 vmw_ttm_unmap_dma(vmw_be);
558 vmw_be->bound = false;
562 static void vmw_ttm_destroy(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
564 struct vmw_ttm_tt *vmw_be =
565 container_of(ttm, struct vmw_ttm_tt, dma_ttm.ttm);
567 vmw_ttm_unbind(bdev, ttm);
568 ttm_tt_destroy_common(bdev, ttm);
569 vmw_ttm_unmap_dma(vmw_be);
570 if (vmw_be->dev_priv->map_mode == vmw_dma_alloc_coherent)
571 ttm_dma_tt_fini(&vmw_be->dma_ttm);
576 vmw_mob_destroy(vmw_be->mob);
582 static int vmw_ttm_populate(struct ttm_bo_device *bdev,
583 struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
585 struct vmw_ttm_tt *vmw_tt =
586 container_of(ttm, struct vmw_ttm_tt, dma_ttm.ttm);
587 struct vmw_private *dev_priv = vmw_tt->dev_priv;
588 struct ttm_mem_global *glob = vmw_mem_glob(dev_priv);
591 if (ttm_tt_is_populated(ttm))
594 if (dev_priv->map_mode == vmw_dma_alloc_coherent) {
596 ttm_round_pot(ttm->num_pages * sizeof(dma_addr_t));
597 ret = ttm_mem_global_alloc(glob, size, ctx);
598 if (unlikely(ret != 0))
601 ret = ttm_dma_populate(&vmw_tt->dma_ttm, dev_priv->dev->dev,
603 if (unlikely(ret != 0))
604 ttm_mem_global_free(glob, size);
606 ret = ttm_pool_populate(ttm, ctx);
611 static void vmw_ttm_unpopulate(struct ttm_bo_device *bdev,
614 struct vmw_ttm_tt *vmw_tt = container_of(ttm, struct vmw_ttm_tt,
616 struct vmw_private *dev_priv = vmw_tt->dev_priv;
617 struct ttm_mem_global *glob = vmw_mem_glob(dev_priv);
621 vmw_mob_destroy(vmw_tt->mob);
625 vmw_ttm_unmap_dma(vmw_tt);
626 if (dev_priv->map_mode == vmw_dma_alloc_coherent) {
628 ttm_round_pot(ttm->num_pages * sizeof(dma_addr_t));
630 ttm_dma_unpopulate(&vmw_tt->dma_ttm, dev_priv->dev->dev);
631 ttm_mem_global_free(glob, size);
633 ttm_pool_unpopulate(ttm);
636 static struct ttm_tt *vmw_ttm_tt_create(struct ttm_buffer_object *bo,
639 struct vmw_ttm_tt *vmw_be;
642 vmw_be = kzalloc(sizeof(*vmw_be), GFP_KERNEL);
646 vmw_be->dev_priv = container_of(bo->bdev, struct vmw_private, bdev);
649 if (vmw_be->dev_priv->map_mode == vmw_dma_alloc_coherent)
650 ret = ttm_dma_tt_init(&vmw_be->dma_ttm, bo, page_flags,
653 ret = ttm_tt_init(&vmw_be->dma_ttm.ttm, bo, page_flags,
655 if (unlikely(ret != 0))
658 return &vmw_be->dma_ttm.ttm;
664 static void vmw_evict_flags(struct ttm_buffer_object *bo,
665 struct ttm_placement *placement)
667 *placement = vmw_sys_placement;
670 static int vmw_verify_access(struct ttm_buffer_object *bo, struct file *filp)
672 struct ttm_object_file *tfile =
673 vmw_fpriv((struct drm_file *)filp->private_data)->tfile;
675 return vmw_user_bo_verify_access(bo, tfile);
678 static int vmw_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_resource *mem)
680 struct vmw_private *dev_priv = container_of(bdev, struct vmw_private, bdev);
682 switch (mem->mem_type) {
688 mem->bus.offset = (mem->start << PAGE_SHIFT) +
689 dev_priv->vram_start;
690 mem->bus.is_iomem = true;
691 mem->bus.caching = ttm_cached;
700 * vmw_move_notify - TTM move_notify_callback
702 * @bo: The TTM buffer object about to move.
703 * @mem: The struct ttm_resource indicating to what memory
704 * region the move is taking place.
706 * Calls move_notify for all subsystems needing it.
707 * (currently only resources).
709 static void vmw_move_notify(struct ttm_buffer_object *bo,
711 struct ttm_resource *mem)
715 vmw_bo_move_notify(bo, mem);
716 vmw_query_move_notify(bo, mem);
721 * vmw_swap_notify - TTM move_notify_callback
723 * @bo: The TTM buffer object about to be swapped out.
725 static void vmw_swap_notify(struct ttm_buffer_object *bo)
727 vmw_bo_swap_notify(bo);
728 (void) ttm_bo_wait(bo, false, false);
731 static int vmw_move(struct ttm_buffer_object *bo,
733 struct ttm_operation_ctx *ctx,
734 struct ttm_resource *new_mem)
736 struct ttm_resource_manager *old_man = ttm_manager_type(bo->bdev, bo->mem.mem_type);
737 struct ttm_resource_manager *new_man = ttm_manager_type(bo->bdev, new_mem->mem_type);
739 if (old_man->use_tt && new_man->use_tt) {
740 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
741 ttm_bo_assign_mem(bo, new_mem);
744 return ttm_bo_move_ttm(bo, ctx, new_mem);
746 return ttm_bo_move_memcpy(bo, ctx, new_mem);
750 struct ttm_bo_driver vmw_bo_driver = {
751 .ttm_tt_create = &vmw_ttm_tt_create,
752 .ttm_tt_populate = &vmw_ttm_populate,
753 .ttm_tt_unpopulate = &vmw_ttm_unpopulate,
754 .ttm_tt_bind = &vmw_ttm_bind,
755 .ttm_tt_unbind = &vmw_ttm_unbind,
756 .ttm_tt_destroy = &vmw_ttm_destroy,
757 .eviction_valuable = ttm_bo_eviction_valuable,
758 .evict_flags = vmw_evict_flags,
760 .verify_access = vmw_verify_access,
761 .move_notify = vmw_move_notify,
762 .swap_notify = vmw_swap_notify,
763 .io_mem_reserve = &vmw_ttm_io_mem_reserve,
766 int vmw_bo_create_and_populate(struct vmw_private *dev_priv,
767 unsigned long bo_size,
768 struct ttm_buffer_object **bo_p)
770 struct ttm_operation_ctx ctx = {
771 .interruptible = false,
774 struct ttm_buffer_object *bo;
777 ret = vmw_bo_create_kernel(dev_priv, bo_size,
780 if (unlikely(ret != 0))
783 ret = ttm_bo_reserve(bo, false, true, NULL);
785 ret = vmw_ttm_populate(bo->bdev, bo->ttm, &ctx);
786 if (likely(ret == 0)) {
787 struct vmw_ttm_tt *vmw_tt =
788 container_of(bo->ttm, struct vmw_ttm_tt, dma_ttm.ttm);
789 ret = vmw_ttm_map_dma(vmw_tt);
792 ttm_bo_unreserve(bo);
794 if (likely(ret == 0))