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 * Helper functions to advance a struct vmw_piter iterator.
205 * @viter: Pointer to the iterator.
207 * These functions return false if past the end of the list,
208 * true otherwise. Functions are selected depending on the current
211 static bool __vmw_piter_non_sg_next(struct vmw_piter *viter)
213 return ++(viter->i) < viter->num_pages;
216 static bool __vmw_piter_sg_next(struct vmw_piter *viter)
218 bool ret = __vmw_piter_non_sg_next(viter);
220 return __sg_page_iter_dma_next(&viter->iter) && ret;
225 * Helper functions to return a pointer to the current page.
227 * @viter: Pointer to the iterator
229 * These functions return a pointer to the page currently
230 * pointed to by @viter. Functions are selected depending on the
231 * current mapping mode.
233 static struct page *__vmw_piter_non_sg_page(struct vmw_piter *viter)
235 return viter->pages[viter->i];
239 * Helper functions to return the DMA address of the current page.
241 * @viter: Pointer to the iterator
243 * These functions return the DMA address of the page currently
244 * pointed to by @viter. Functions are selected depending on the
245 * current mapping mode.
247 static dma_addr_t __vmw_piter_phys_addr(struct vmw_piter *viter)
249 return page_to_phys(viter->pages[viter->i]);
252 static dma_addr_t __vmw_piter_dma_addr(struct vmw_piter *viter)
254 return viter->addrs[viter->i];
257 static dma_addr_t __vmw_piter_sg_addr(struct vmw_piter *viter)
259 return sg_page_iter_dma_address(&viter->iter);
264 * vmw_piter_start - Initialize a struct vmw_piter.
266 * @viter: Pointer to the iterator to initialize
267 * @vsgt: Pointer to a struct vmw_sg_table to initialize from
268 * @p_offset: Pointer offset used to update current array position
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->drm.dev;
315 dma_unmap_sgtable(dev, &vmw_tt->sgt, DMA_BIDIRECTIONAL, 0);
316 vmw_tt->sgt.nents = vmw_tt->sgt.orig_nents;
320 * vmw_ttm_map_for_dma - map TTM pages to get device addresses
322 * @vmw_tt: Pointer to a struct vmw_ttm_backend
324 * This function is used to get device addresses from the kernel DMA layer.
325 * However, it's violating the DMA API in that when this operation has been
326 * performed, it's illegal for the CPU to write to the pages without first
327 * unmapping the DMA mappings, or calling dma_sync_sg_for_cpu(). It is
328 * therefore only legal to call this function if we know that the function
329 * dma_sync_sg_for_cpu() is a NOP, and dma_sync_sg_for_device() is at most
330 * a CPU write buffer flush.
332 static int vmw_ttm_map_for_dma(struct vmw_ttm_tt *vmw_tt)
334 struct device *dev = vmw_tt->dev_priv->drm.dev;
336 return dma_map_sgtable(dev, &vmw_tt->sgt, DMA_BIDIRECTIONAL, 0);
340 * vmw_ttm_map_dma - Make sure TTM pages are visible to the device
342 * @vmw_tt: Pointer to a struct vmw_ttm_tt
344 * Select the correct function for and make sure the TTM pages are
345 * visible to the device. Allocate storage for the device mappings.
346 * If a mapping has already been performed, indicated by the storage
347 * pointer being non NULL, the function returns success.
349 static int vmw_ttm_map_dma(struct vmw_ttm_tt *vmw_tt)
351 struct vmw_private *dev_priv = vmw_tt->dev_priv;
352 struct ttm_mem_global *glob = vmw_mem_glob(dev_priv);
353 struct vmw_sg_table *vsgt = &vmw_tt->vsgt;
354 struct ttm_operation_ctx ctx = {
355 .interruptible = true,
358 struct vmw_piter iter;
361 static size_t sgl_size;
362 static size_t sgt_size;
363 struct scatterlist *sg;
368 vsgt->mode = dev_priv->map_mode;
369 vsgt->pages = vmw_tt->dma_ttm.pages;
370 vsgt->num_pages = vmw_tt->dma_ttm.num_pages;
371 vsgt->addrs = vmw_tt->dma_ttm.dma_address;
372 vsgt->sgt = &vmw_tt->sgt;
374 switch (dev_priv->map_mode) {
375 case vmw_dma_map_bind:
376 case vmw_dma_map_populate:
377 if (unlikely(!sgl_size)) {
378 sgl_size = ttm_round_pot(sizeof(struct scatterlist));
379 sgt_size = ttm_round_pot(sizeof(struct sg_table));
381 vmw_tt->sg_alloc_size = sgt_size + sgl_size * vsgt->num_pages;
382 ret = ttm_mem_global_alloc(glob, vmw_tt->sg_alloc_size, &ctx);
383 if (unlikely(ret != 0))
386 sg = __sg_alloc_table_from_pages(&vmw_tt->sgt, vsgt->pages,
388 (unsigned long) vsgt->num_pages << PAGE_SHIFT,
389 dma_get_max_seg_size(dev_priv->drm.dev),
390 NULL, 0, GFP_KERNEL);
393 goto out_sg_alloc_fail;
396 if (vsgt->num_pages > vmw_tt->sgt.orig_nents) {
397 uint64_t over_alloc =
398 sgl_size * (vsgt->num_pages -
399 vmw_tt->sgt.orig_nents);
401 ttm_mem_global_free(glob, over_alloc);
402 vmw_tt->sg_alloc_size -= over_alloc;
405 ret = vmw_ttm_map_for_dma(vmw_tt);
406 if (unlikely(ret != 0))
414 old = ~((dma_addr_t) 0);
415 vmw_tt->vsgt.num_regions = 0;
416 for (vmw_piter_start(&iter, vsgt, 0); vmw_piter_next(&iter);) {
417 dma_addr_t cur = vmw_piter_dma_addr(&iter);
419 if (cur != old + PAGE_SIZE)
420 vmw_tt->vsgt.num_regions++;
424 vmw_tt->mapped = true;
428 sg_free_table(vmw_tt->vsgt.sgt);
429 vmw_tt->vsgt.sgt = NULL;
431 ttm_mem_global_free(glob, vmw_tt->sg_alloc_size);
436 * vmw_ttm_unmap_dma - Tear down any TTM page device mappings
438 * @vmw_tt: Pointer to a struct vmw_ttm_tt
440 * Tear down any previously set up device DMA mappings and free
441 * any storage space allocated for them. If there are no mappings set up,
442 * this function is a NOP.
444 static void vmw_ttm_unmap_dma(struct vmw_ttm_tt *vmw_tt)
446 struct vmw_private *dev_priv = vmw_tt->dev_priv;
448 if (!vmw_tt->vsgt.sgt)
451 switch (dev_priv->map_mode) {
452 case vmw_dma_map_bind:
453 case vmw_dma_map_populate:
454 vmw_ttm_unmap_from_dma(vmw_tt);
455 sg_free_table(vmw_tt->vsgt.sgt);
456 vmw_tt->vsgt.sgt = NULL;
457 ttm_mem_global_free(vmw_mem_glob(dev_priv),
458 vmw_tt->sg_alloc_size);
463 vmw_tt->mapped = false;
467 * vmw_bo_sg_table - Return a struct vmw_sg_table object for a
470 * @bo: Pointer to a struct ttm_buffer_object
472 * Returns a pointer to a struct vmw_sg_table object. The object should
473 * not be freed after use.
474 * Note that for the device addresses to be valid, the buffer object must
475 * either be reserved or pinned.
477 const struct vmw_sg_table *vmw_bo_sg_table(struct ttm_buffer_object *bo)
479 struct vmw_ttm_tt *vmw_tt =
480 container_of(bo->ttm, struct vmw_ttm_tt, dma_ttm);
482 return &vmw_tt->vsgt;
486 static int vmw_ttm_bind(struct ttm_device *bdev,
487 struct ttm_tt *ttm, struct ttm_resource *bo_mem)
489 struct vmw_ttm_tt *vmw_be =
490 container_of(ttm, struct vmw_ttm_tt, dma_ttm);
499 ret = vmw_ttm_map_dma(vmw_be);
500 if (unlikely(ret != 0))
503 vmw_be->gmr_id = bo_mem->start;
504 vmw_be->mem_type = bo_mem->mem_type;
506 switch (bo_mem->mem_type) {
508 ret = vmw_gmr_bind(vmw_be->dev_priv, &vmw_be->vsgt,
509 ttm->num_pages, vmw_be->gmr_id);
512 if (unlikely(vmw_be->mob == NULL)) {
514 vmw_mob_create(ttm->num_pages);
515 if (unlikely(vmw_be->mob == NULL))
519 ret = vmw_mob_bind(vmw_be->dev_priv, vmw_be->mob,
520 &vmw_be->vsgt, ttm->num_pages,
526 vmw_be->bound = true;
530 static void vmw_ttm_unbind(struct ttm_device *bdev,
533 struct vmw_ttm_tt *vmw_be =
534 container_of(ttm, struct vmw_ttm_tt, dma_ttm);
539 switch (vmw_be->mem_type) {
541 vmw_gmr_unbind(vmw_be->dev_priv, vmw_be->gmr_id);
544 vmw_mob_unbind(vmw_be->dev_priv, vmw_be->mob);
550 if (vmw_be->dev_priv->map_mode == vmw_dma_map_bind)
551 vmw_ttm_unmap_dma(vmw_be);
552 vmw_be->bound = false;
556 static void vmw_ttm_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
558 struct vmw_ttm_tt *vmw_be =
559 container_of(ttm, struct vmw_ttm_tt, dma_ttm);
561 vmw_ttm_unbind(bdev, ttm);
562 ttm_tt_destroy_common(bdev, ttm);
563 vmw_ttm_unmap_dma(vmw_be);
564 if (vmw_be->dev_priv->map_mode == vmw_dma_alloc_coherent)
565 ttm_tt_fini(&vmw_be->dma_ttm);
570 vmw_mob_destroy(vmw_be->mob);
576 static int vmw_ttm_populate(struct ttm_device *bdev,
577 struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
579 /* TODO: maybe completely drop this ? */
580 if (ttm_tt_is_populated(ttm))
583 return ttm_pool_alloc(&bdev->pool, ttm, ctx);
586 static void vmw_ttm_unpopulate(struct ttm_device *bdev,
589 struct vmw_ttm_tt *vmw_tt = container_of(ttm, struct vmw_ttm_tt,
593 vmw_mob_destroy(vmw_tt->mob);
597 vmw_ttm_unmap_dma(vmw_tt);
598 ttm_pool_free(&bdev->pool, ttm);
601 static struct ttm_tt *vmw_ttm_tt_create(struct ttm_buffer_object *bo,
604 struct vmw_ttm_tt *vmw_be;
607 vmw_be = kzalloc(sizeof(*vmw_be), GFP_KERNEL);
611 vmw_be->dev_priv = container_of(bo->bdev, struct vmw_private, bdev);
614 if (vmw_be->dev_priv->map_mode == vmw_dma_alloc_coherent)
615 ret = ttm_sg_tt_init(&vmw_be->dma_ttm, bo, page_flags,
618 ret = ttm_tt_init(&vmw_be->dma_ttm, bo, page_flags,
620 if (unlikely(ret != 0))
623 return &vmw_be->dma_ttm;
629 static void vmw_evict_flags(struct ttm_buffer_object *bo,
630 struct ttm_placement *placement)
632 *placement = vmw_sys_placement;
635 static int vmw_verify_access(struct ttm_buffer_object *bo, struct file *filp)
637 struct ttm_object_file *tfile =
638 vmw_fpriv((struct drm_file *)filp->private_data)->tfile;
640 return vmw_user_bo_verify_access(bo, tfile);
643 static int vmw_ttm_io_mem_reserve(struct ttm_device *bdev, struct ttm_resource *mem)
645 struct vmw_private *dev_priv = container_of(bdev, struct vmw_private, bdev);
647 switch (mem->mem_type) {
653 mem->bus.offset = (mem->start << PAGE_SHIFT) +
654 dev_priv->vram_start;
655 mem->bus.is_iomem = true;
656 mem->bus.caching = ttm_cached;
665 * vmw_move_notify - TTM move_notify_callback
667 * @bo: The TTM buffer object about to move.
669 * @mem: The struct ttm_resource indicating to what memory
670 * region the move is taking place.
672 * Calls move_notify for all subsystems needing it.
673 * (currently only resources).
675 static void vmw_move_notify(struct ttm_buffer_object *bo,
677 struct ttm_resource *mem)
681 vmw_bo_move_notify(bo, mem);
682 vmw_query_move_notify(bo, mem);
687 * vmw_swap_notify - TTM move_notify_callback
689 * @bo: The TTM buffer object about to be swapped out.
691 static void vmw_swap_notify(struct ttm_buffer_object *bo)
693 vmw_bo_swap_notify(bo);
694 (void) ttm_bo_wait(bo, false, false);
697 static int vmw_move(struct ttm_buffer_object *bo,
699 struct ttm_operation_ctx *ctx,
700 struct ttm_resource *new_mem,
701 struct ttm_place *hop)
703 struct ttm_resource_manager *old_man = ttm_manager_type(bo->bdev, bo->mem.mem_type);
704 struct ttm_resource_manager *new_man = ttm_manager_type(bo->bdev, new_mem->mem_type);
707 if (new_man->use_tt && new_mem->mem_type != TTM_PL_SYSTEM) {
708 ret = vmw_ttm_bind(bo->bdev, bo->ttm, new_mem);
713 vmw_move_notify(bo, evict, new_mem);
715 if (old_man->use_tt && new_man->use_tt) {
716 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
717 ttm_bo_assign_mem(bo, new_mem);
720 ret = ttm_bo_wait_ctx(bo, ctx);
724 vmw_ttm_unbind(bo->bdev, bo->ttm);
725 ttm_resource_free(bo, &bo->mem);
726 ttm_bo_assign_mem(bo, new_mem);
729 ret = ttm_bo_move_memcpy(bo, ctx, new_mem);
735 swap(*new_mem, bo->mem);
736 vmw_move_notify(bo, false, new_mem);
737 swap(*new_mem, bo->mem);
742 vmw_delete_mem_notify(struct ttm_buffer_object *bo)
744 vmw_move_notify(bo, false, NULL);
747 struct ttm_device_funcs vmw_bo_driver = {
748 .ttm_tt_create = &vmw_ttm_tt_create,
749 .ttm_tt_populate = &vmw_ttm_populate,
750 .ttm_tt_unpopulate = &vmw_ttm_unpopulate,
751 .ttm_tt_destroy = &vmw_ttm_destroy,
752 .eviction_valuable = ttm_bo_eviction_valuable,
753 .evict_flags = vmw_evict_flags,
755 .verify_access = vmw_verify_access,
756 .delete_mem_notify = vmw_delete_mem_notify,
757 .swap_notify = vmw_swap_notify,
758 .io_mem_reserve = &vmw_ttm_io_mem_reserve,
761 int vmw_bo_create_and_populate(struct vmw_private *dev_priv,
762 unsigned long bo_size,
763 struct ttm_buffer_object **bo_p)
765 struct ttm_operation_ctx ctx = {
766 .interruptible = false,
769 struct ttm_buffer_object *bo;
772 ret = vmw_bo_create_kernel(dev_priv, bo_size,
775 if (unlikely(ret != 0))
778 ret = ttm_bo_reserve(bo, false, true, NULL);
780 ret = vmw_ttm_populate(bo->bdev, bo->ttm, &ctx);
781 if (likely(ret == 0)) {
782 struct vmw_ttm_tt *vmw_tt =
783 container_of(bo->ttm, struct vmw_ttm_tt, dma_ttm);
784 ret = vmw_ttm_map_dma(vmw_tt);
787 ttm_bo_unreserve(bo);
789 if (likely(ret == 0))