Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / gpu / drm / vmwgfx / vmwgfx_buffer.c
blob22231bc9e84511ebea9b50a806971b3fa3a95437
1 /**************************************************************************
3 * Copyright © 2009-2015 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
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
16 * of the Software.
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 = {
34 .fpfn = 0,
35 .lpfn = 0,
36 .flags = TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED
39 static const struct ttm_place vram_ne_placement_flags = {
40 .fpfn = 0,
41 .lpfn = 0,
42 .flags = TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED | TTM_PL_FLAG_NO_EVICT
45 static const struct ttm_place sys_placement_flags = {
46 .fpfn = 0,
47 .lpfn = 0,
48 .flags = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED
51 static const struct ttm_place sys_ne_placement_flags = {
52 .fpfn = 0,
53 .lpfn = 0,
54 .flags = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED | TTM_PL_FLAG_NO_EVICT
57 static const struct ttm_place gmr_placement_flags = {
58 .fpfn = 0,
59 .lpfn = 0,
60 .flags = VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED
63 static const struct ttm_place gmr_ne_placement_flags = {
64 .fpfn = 0,
65 .lpfn = 0,
66 .flags = VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED | TTM_PL_FLAG_NO_EVICT
69 static const struct ttm_place mob_placement_flags = {
70 .fpfn = 0,
71 .lpfn = 0,
72 .flags = VMW_PL_FLAG_MOB | TTM_PL_FLAG_CACHED
75 static const struct ttm_place mob_ne_placement_flags = {
76 .fpfn = 0,
77 .lpfn = 0,
78 .flags = VMW_PL_FLAG_MOB | TTM_PL_FLAG_CACHED | TTM_PL_FLAG_NO_EVICT
81 struct ttm_placement vmw_vram_placement = {
82 .num_placement = 1,
83 .placement = &vram_placement_flags,
84 .num_busy_placement = 1,
85 .busy_placement = &vram_placement_flags
88 static const struct ttm_place vram_gmr_placement_flags[] = {
90 .fpfn = 0,
91 .lpfn = 0,
92 .flags = TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED
93 }, {
94 .fpfn = 0,
95 .lpfn = 0,
96 .flags = VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED
100 static const struct ttm_place gmr_vram_placement_flags[] = {
102 .fpfn = 0,
103 .lpfn = 0,
104 .flags = VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED
105 }, {
106 .fpfn = 0,
107 .lpfn = 0,
108 .flags = TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED
112 struct ttm_placement vmw_vram_gmr_placement = {
113 .num_placement = 2,
114 .placement = vram_gmr_placement_flags,
115 .num_busy_placement = 1,
116 .busy_placement = &gmr_placement_flags
119 static const struct ttm_place vram_gmr_ne_placement_flags[] = {
121 .fpfn = 0,
122 .lpfn = 0,
123 .flags = TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED |
124 TTM_PL_FLAG_NO_EVICT
125 }, {
126 .fpfn = 0,
127 .lpfn = 0,
128 .flags = VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED |
129 TTM_PL_FLAG_NO_EVICT
133 struct ttm_placement vmw_vram_gmr_ne_placement = {
134 .num_placement = 2,
135 .placement = vram_gmr_ne_placement_flags,
136 .num_busy_placement = 1,
137 .busy_placement = &gmr_ne_placement_flags
140 struct ttm_placement vmw_vram_sys_placement = {
141 .num_placement = 1,
142 .placement = &vram_placement_flags,
143 .num_busy_placement = 1,
144 .busy_placement = &sys_placement_flags
147 struct ttm_placement vmw_vram_ne_placement = {
148 .num_placement = 1,
149 .placement = &vram_ne_placement_flags,
150 .num_busy_placement = 1,
151 .busy_placement = &vram_ne_placement_flags
154 struct ttm_placement vmw_sys_placement = {
155 .num_placement = 1,
156 .placement = &sys_placement_flags,
157 .num_busy_placement = 1,
158 .busy_placement = &sys_placement_flags
161 struct ttm_placement vmw_sys_ne_placement = {
162 .num_placement = 1,
163 .placement = &sys_ne_placement_flags,
164 .num_busy_placement = 1,
165 .busy_placement = &sys_ne_placement_flags
168 static const struct ttm_place evictable_placement_flags[] = {
170 .fpfn = 0,
171 .lpfn = 0,
172 .flags = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED
173 }, {
174 .fpfn = 0,
175 .lpfn = 0,
176 .flags = TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED
177 }, {
178 .fpfn = 0,
179 .lpfn = 0,
180 .flags = VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED
181 }, {
182 .fpfn = 0,
183 .lpfn = 0,
184 .flags = VMW_PL_FLAG_MOB | TTM_PL_FLAG_CACHED
188 struct ttm_placement vmw_evictable_placement = {
189 .num_placement = 4,
190 .placement = evictable_placement_flags,
191 .num_busy_placement = 1,
192 .busy_placement = &sys_placement_flags
195 struct ttm_placement vmw_srf_placement = {
196 .num_placement = 1,
197 .num_busy_placement = 2,
198 .placement = &gmr_placement_flags,
199 .busy_placement = gmr_vram_placement_flags
202 struct ttm_placement vmw_mob_placement = {
203 .num_placement = 1,
204 .num_busy_placement = 1,
205 .placement = &mob_placement_flags,
206 .busy_placement = &mob_placement_flags
209 struct ttm_placement vmw_mob_ne_placement = {
210 .num_placement = 1,
211 .num_busy_placement = 1,
212 .placement = &mob_ne_placement_flags,
213 .busy_placement = &mob_ne_placement_flags
216 struct vmw_ttm_tt {
217 struct ttm_dma_tt dma_ttm;
218 struct vmw_private *dev_priv;
219 int gmr_id;
220 struct vmw_mob *mob;
221 int mem_type;
222 struct sg_table sgt;
223 struct vmw_sg_table vsgt;
224 uint64_t sg_alloc_size;
225 bool mapped;
228 const size_t vmw_tt_size = sizeof(struct vmw_ttm_tt);
231 * Helper functions to advance a struct vmw_piter iterator.
233 * @viter: Pointer to the iterator.
235 * These functions return false if past the end of the list,
236 * true otherwise. Functions are selected depending on the current
237 * DMA mapping mode.
239 static bool __vmw_piter_non_sg_next(struct vmw_piter *viter)
241 return ++(viter->i) < viter->num_pages;
244 static bool __vmw_piter_sg_next(struct vmw_piter *viter)
246 return __sg_page_iter_next(&viter->iter);
251 * Helper functions to return a pointer to the current page.
253 * @viter: Pointer to the iterator
255 * These functions return a pointer to the page currently
256 * pointed to by @viter. Functions are selected depending on the
257 * current mapping mode.
259 static struct page *__vmw_piter_non_sg_page(struct vmw_piter *viter)
261 return viter->pages[viter->i];
264 static struct page *__vmw_piter_sg_page(struct vmw_piter *viter)
266 return sg_page_iter_page(&viter->iter);
271 * Helper functions to return the DMA address of the current page.
273 * @viter: Pointer to the iterator
275 * These functions return the DMA address of the page currently
276 * pointed to by @viter. Functions are selected depending on the
277 * current mapping mode.
279 static dma_addr_t __vmw_piter_phys_addr(struct vmw_piter *viter)
281 return page_to_phys(viter->pages[viter->i]);
284 static dma_addr_t __vmw_piter_dma_addr(struct vmw_piter *viter)
286 return viter->addrs[viter->i];
289 static dma_addr_t __vmw_piter_sg_addr(struct vmw_piter *viter)
291 return sg_page_iter_dma_address(&viter->iter);
296 * vmw_piter_start - Initialize a struct vmw_piter.
298 * @viter: Pointer to the iterator to initialize
299 * @vsgt: Pointer to a struct vmw_sg_table to initialize from
301 * Note that we're following the convention of __sg_page_iter_start, so that
302 * the iterator doesn't point to a valid page after initialization; it has
303 * to be advanced one step first.
305 void vmw_piter_start(struct vmw_piter *viter, const struct vmw_sg_table *vsgt,
306 unsigned long p_offset)
308 viter->i = p_offset - 1;
309 viter->num_pages = vsgt->num_pages;
310 switch (vsgt->mode) {
311 case vmw_dma_phys:
312 viter->next = &__vmw_piter_non_sg_next;
313 viter->dma_address = &__vmw_piter_phys_addr;
314 viter->page = &__vmw_piter_non_sg_page;
315 viter->pages = vsgt->pages;
316 break;
317 case vmw_dma_alloc_coherent:
318 viter->next = &__vmw_piter_non_sg_next;
319 viter->dma_address = &__vmw_piter_dma_addr;
320 viter->page = &__vmw_piter_non_sg_page;
321 viter->addrs = vsgt->addrs;
322 viter->pages = vsgt->pages;
323 break;
324 case vmw_dma_map_populate:
325 case vmw_dma_map_bind:
326 viter->next = &__vmw_piter_sg_next;
327 viter->dma_address = &__vmw_piter_sg_addr;
328 viter->page = &__vmw_piter_sg_page;
329 __sg_page_iter_start(&viter->iter, vsgt->sgt->sgl,
330 vsgt->sgt->orig_nents, p_offset);
331 break;
332 default:
333 BUG();
338 * vmw_ttm_unmap_from_dma - unmap device addresses previsouly mapped for
339 * TTM pages
341 * @vmw_tt: Pointer to a struct vmw_ttm_backend
343 * Used to free dma mappings previously mapped by vmw_ttm_map_for_dma.
345 static void vmw_ttm_unmap_from_dma(struct vmw_ttm_tt *vmw_tt)
347 struct device *dev = vmw_tt->dev_priv->dev->dev;
349 dma_unmap_sg(dev, vmw_tt->sgt.sgl, vmw_tt->sgt.nents,
350 DMA_BIDIRECTIONAL);
351 vmw_tt->sgt.nents = vmw_tt->sgt.orig_nents;
355 * vmw_ttm_map_for_dma - map TTM pages to get device addresses
357 * @vmw_tt: Pointer to a struct vmw_ttm_backend
359 * This function is used to get device addresses from the kernel DMA layer.
360 * However, it's violating the DMA API in that when this operation has been
361 * performed, it's illegal for the CPU to write to the pages without first
362 * unmapping the DMA mappings, or calling dma_sync_sg_for_cpu(). It is
363 * therefore only legal to call this function if we know that the function
364 * dma_sync_sg_for_cpu() is a NOP, and dma_sync_sg_for_device() is at most
365 * a CPU write buffer flush.
367 static int vmw_ttm_map_for_dma(struct vmw_ttm_tt *vmw_tt)
369 struct device *dev = vmw_tt->dev_priv->dev->dev;
370 int ret;
372 ret = dma_map_sg(dev, vmw_tt->sgt.sgl, vmw_tt->sgt.orig_nents,
373 DMA_BIDIRECTIONAL);
374 if (unlikely(ret == 0))
375 return -ENOMEM;
377 vmw_tt->sgt.nents = ret;
379 return 0;
383 * vmw_ttm_map_dma - Make sure TTM pages are visible to the device
385 * @vmw_tt: Pointer to a struct vmw_ttm_tt
387 * Select the correct function for and make sure the TTM pages are
388 * visible to the device. Allocate storage for the device mappings.
389 * If a mapping has already been performed, indicated by the storage
390 * pointer being non NULL, the function returns success.
392 static int vmw_ttm_map_dma(struct vmw_ttm_tt *vmw_tt)
394 struct vmw_private *dev_priv = vmw_tt->dev_priv;
395 struct ttm_mem_global *glob = vmw_mem_glob(dev_priv);
396 struct vmw_sg_table *vsgt = &vmw_tt->vsgt;
397 struct ttm_operation_ctx ctx = {
398 .interruptible = true,
399 .no_wait_gpu = false
401 struct vmw_piter iter;
402 dma_addr_t old;
403 int ret = 0;
404 static size_t sgl_size;
405 static size_t sgt_size;
407 if (vmw_tt->mapped)
408 return 0;
410 vsgt->mode = dev_priv->map_mode;
411 vsgt->pages = vmw_tt->dma_ttm.ttm.pages;
412 vsgt->num_pages = vmw_tt->dma_ttm.ttm.num_pages;
413 vsgt->addrs = vmw_tt->dma_ttm.dma_address;
414 vsgt->sgt = &vmw_tt->sgt;
416 switch (dev_priv->map_mode) {
417 case vmw_dma_map_bind:
418 case vmw_dma_map_populate:
419 if (unlikely(!sgl_size)) {
420 sgl_size = ttm_round_pot(sizeof(struct scatterlist));
421 sgt_size = ttm_round_pot(sizeof(struct sg_table));
423 vmw_tt->sg_alloc_size = sgt_size + sgl_size * vsgt->num_pages;
424 ret = ttm_mem_global_alloc(glob, vmw_tt->sg_alloc_size, &ctx);
425 if (unlikely(ret != 0))
426 return ret;
428 ret = sg_alloc_table_from_pages(&vmw_tt->sgt, vsgt->pages,
429 vsgt->num_pages, 0,
430 (unsigned long)
431 vsgt->num_pages << PAGE_SHIFT,
432 GFP_KERNEL);
433 if (unlikely(ret != 0))
434 goto out_sg_alloc_fail;
436 if (vsgt->num_pages > vmw_tt->sgt.nents) {
437 uint64_t over_alloc =
438 sgl_size * (vsgt->num_pages -
439 vmw_tt->sgt.nents);
441 ttm_mem_global_free(glob, over_alloc);
442 vmw_tt->sg_alloc_size -= over_alloc;
445 ret = vmw_ttm_map_for_dma(vmw_tt);
446 if (unlikely(ret != 0))
447 goto out_map_fail;
449 break;
450 default:
451 break;
454 old = ~((dma_addr_t) 0);
455 vmw_tt->vsgt.num_regions = 0;
456 for (vmw_piter_start(&iter, vsgt, 0); vmw_piter_next(&iter);) {
457 dma_addr_t cur = vmw_piter_dma_addr(&iter);
459 if (cur != old + PAGE_SIZE)
460 vmw_tt->vsgt.num_regions++;
461 old = cur;
464 vmw_tt->mapped = true;
465 return 0;
467 out_map_fail:
468 sg_free_table(vmw_tt->vsgt.sgt);
469 vmw_tt->vsgt.sgt = NULL;
470 out_sg_alloc_fail:
471 ttm_mem_global_free(glob, vmw_tt->sg_alloc_size);
472 return ret;
476 * vmw_ttm_unmap_dma - Tear down any TTM page device mappings
478 * @vmw_tt: Pointer to a struct vmw_ttm_tt
480 * Tear down any previously set up device DMA mappings and free
481 * any storage space allocated for them. If there are no mappings set up,
482 * this function is a NOP.
484 static void vmw_ttm_unmap_dma(struct vmw_ttm_tt *vmw_tt)
486 struct vmw_private *dev_priv = vmw_tt->dev_priv;
488 if (!vmw_tt->vsgt.sgt)
489 return;
491 switch (dev_priv->map_mode) {
492 case vmw_dma_map_bind:
493 case vmw_dma_map_populate:
494 vmw_ttm_unmap_from_dma(vmw_tt);
495 sg_free_table(vmw_tt->vsgt.sgt);
496 vmw_tt->vsgt.sgt = NULL;
497 ttm_mem_global_free(vmw_mem_glob(dev_priv),
498 vmw_tt->sg_alloc_size);
499 break;
500 default:
501 break;
503 vmw_tt->mapped = false;
508 * vmw_bo_map_dma - Make sure buffer object pages are visible to the device
510 * @bo: Pointer to a struct ttm_buffer_object
512 * Wrapper around vmw_ttm_map_dma, that takes a TTM buffer object pointer
513 * instead of a pointer to a struct vmw_ttm_backend as argument.
514 * Note that the buffer object must be either pinned or reserved before
515 * calling this function.
517 int vmw_bo_map_dma(struct ttm_buffer_object *bo)
519 struct vmw_ttm_tt *vmw_tt =
520 container_of(bo->ttm, struct vmw_ttm_tt, dma_ttm.ttm);
522 return vmw_ttm_map_dma(vmw_tt);
527 * vmw_bo_unmap_dma - Make sure buffer object pages are visible to the device
529 * @bo: Pointer to a struct ttm_buffer_object
531 * Wrapper around vmw_ttm_unmap_dma, that takes a TTM buffer object pointer
532 * instead of a pointer to a struct vmw_ttm_backend as argument.
534 void vmw_bo_unmap_dma(struct ttm_buffer_object *bo)
536 struct vmw_ttm_tt *vmw_tt =
537 container_of(bo->ttm, struct vmw_ttm_tt, dma_ttm.ttm);
539 vmw_ttm_unmap_dma(vmw_tt);
544 * vmw_bo_sg_table - Return a struct vmw_sg_table object for a
545 * TTM buffer object
547 * @bo: Pointer to a struct ttm_buffer_object
549 * Returns a pointer to a struct vmw_sg_table object. The object should
550 * not be freed after use.
551 * Note that for the device addresses to be valid, the buffer object must
552 * either be reserved or pinned.
554 const struct vmw_sg_table *vmw_bo_sg_table(struct ttm_buffer_object *bo)
556 struct vmw_ttm_tt *vmw_tt =
557 container_of(bo->ttm, struct vmw_ttm_tt, dma_ttm.ttm);
559 return &vmw_tt->vsgt;
563 static int vmw_ttm_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
565 struct vmw_ttm_tt *vmw_be =
566 container_of(ttm, struct vmw_ttm_tt, dma_ttm.ttm);
567 int ret;
569 ret = vmw_ttm_map_dma(vmw_be);
570 if (unlikely(ret != 0))
571 return ret;
573 vmw_be->gmr_id = bo_mem->start;
574 vmw_be->mem_type = bo_mem->mem_type;
576 switch (bo_mem->mem_type) {
577 case VMW_PL_GMR:
578 return vmw_gmr_bind(vmw_be->dev_priv, &vmw_be->vsgt,
579 ttm->num_pages, vmw_be->gmr_id);
580 case VMW_PL_MOB:
581 if (unlikely(vmw_be->mob == NULL)) {
582 vmw_be->mob =
583 vmw_mob_create(ttm->num_pages);
584 if (unlikely(vmw_be->mob == NULL))
585 return -ENOMEM;
588 return vmw_mob_bind(vmw_be->dev_priv, vmw_be->mob,
589 &vmw_be->vsgt, ttm->num_pages,
590 vmw_be->gmr_id);
591 default:
592 BUG();
594 return 0;
597 static int vmw_ttm_unbind(struct ttm_tt *ttm)
599 struct vmw_ttm_tt *vmw_be =
600 container_of(ttm, struct vmw_ttm_tt, dma_ttm.ttm);
602 switch (vmw_be->mem_type) {
603 case VMW_PL_GMR:
604 vmw_gmr_unbind(vmw_be->dev_priv, vmw_be->gmr_id);
605 break;
606 case VMW_PL_MOB:
607 vmw_mob_unbind(vmw_be->dev_priv, vmw_be->mob);
608 break;
609 default:
610 BUG();
613 if (vmw_be->dev_priv->map_mode == vmw_dma_map_bind)
614 vmw_ttm_unmap_dma(vmw_be);
616 return 0;
620 static void vmw_ttm_destroy(struct ttm_tt *ttm)
622 struct vmw_ttm_tt *vmw_be =
623 container_of(ttm, struct vmw_ttm_tt, dma_ttm.ttm);
625 vmw_ttm_unmap_dma(vmw_be);
626 if (vmw_be->dev_priv->map_mode == vmw_dma_alloc_coherent)
627 ttm_dma_tt_fini(&vmw_be->dma_ttm);
628 else
629 ttm_tt_fini(ttm);
631 if (vmw_be->mob)
632 vmw_mob_destroy(vmw_be->mob);
634 kfree(vmw_be);
638 static int vmw_ttm_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
640 struct vmw_ttm_tt *vmw_tt =
641 container_of(ttm, struct vmw_ttm_tt, dma_ttm.ttm);
642 struct vmw_private *dev_priv = vmw_tt->dev_priv;
643 struct ttm_mem_global *glob = vmw_mem_glob(dev_priv);
644 int ret;
646 if (ttm->state != tt_unpopulated)
647 return 0;
649 if (dev_priv->map_mode == vmw_dma_alloc_coherent) {
650 size_t size =
651 ttm_round_pot(ttm->num_pages * sizeof(dma_addr_t));
652 ret = ttm_mem_global_alloc(glob, size, ctx);
653 if (unlikely(ret != 0))
654 return ret;
656 ret = ttm_dma_populate(&vmw_tt->dma_ttm, dev_priv->dev->dev,
657 ctx);
658 if (unlikely(ret != 0))
659 ttm_mem_global_free(glob, size);
660 } else
661 ret = ttm_pool_populate(ttm, ctx);
663 return ret;
666 static void vmw_ttm_unpopulate(struct ttm_tt *ttm)
668 struct vmw_ttm_tt *vmw_tt = container_of(ttm, struct vmw_ttm_tt,
669 dma_ttm.ttm);
670 struct vmw_private *dev_priv = vmw_tt->dev_priv;
671 struct ttm_mem_global *glob = vmw_mem_glob(dev_priv);
674 if (vmw_tt->mob) {
675 vmw_mob_destroy(vmw_tt->mob);
676 vmw_tt->mob = NULL;
679 vmw_ttm_unmap_dma(vmw_tt);
680 if (dev_priv->map_mode == vmw_dma_alloc_coherent) {
681 size_t size =
682 ttm_round_pot(ttm->num_pages * sizeof(dma_addr_t));
684 ttm_dma_unpopulate(&vmw_tt->dma_ttm, dev_priv->dev->dev);
685 ttm_mem_global_free(glob, size);
686 } else
687 ttm_pool_unpopulate(ttm);
690 static struct ttm_backend_func vmw_ttm_func = {
691 .bind = vmw_ttm_bind,
692 .unbind = vmw_ttm_unbind,
693 .destroy = vmw_ttm_destroy,
696 static struct ttm_tt *vmw_ttm_tt_create(struct ttm_bo_device *bdev,
697 unsigned long size, uint32_t page_flags,
698 struct page *dummy_read_page)
700 struct vmw_ttm_tt *vmw_be;
701 int ret;
703 vmw_be = kzalloc(sizeof(*vmw_be), GFP_KERNEL);
704 if (!vmw_be)
705 return NULL;
707 vmw_be->dma_ttm.ttm.func = &vmw_ttm_func;
708 vmw_be->dev_priv = container_of(bdev, struct vmw_private, bdev);
709 vmw_be->mob = NULL;
711 if (vmw_be->dev_priv->map_mode == vmw_dma_alloc_coherent)
712 ret = ttm_dma_tt_init(&vmw_be->dma_ttm, bdev, size, page_flags,
713 dummy_read_page);
714 else
715 ret = ttm_tt_init(&vmw_be->dma_ttm.ttm, bdev, size, page_flags,
716 dummy_read_page);
717 if (unlikely(ret != 0))
718 goto out_no_init;
720 return &vmw_be->dma_ttm.ttm;
721 out_no_init:
722 kfree(vmw_be);
723 return NULL;
726 static int vmw_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
728 return 0;
731 static int vmw_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
732 struct ttm_mem_type_manager *man)
734 switch (type) {
735 case TTM_PL_SYSTEM:
736 /* System memory */
738 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
739 man->available_caching = TTM_PL_FLAG_CACHED;
740 man->default_caching = TTM_PL_FLAG_CACHED;
741 break;
742 case TTM_PL_VRAM:
743 /* "On-card" video ram */
744 man->func = &ttm_bo_manager_func;
745 man->gpu_offset = 0;
746 man->flags = TTM_MEMTYPE_FLAG_FIXED | TTM_MEMTYPE_FLAG_MAPPABLE;
747 man->available_caching = TTM_PL_FLAG_CACHED;
748 man->default_caching = TTM_PL_FLAG_CACHED;
749 break;
750 case VMW_PL_GMR:
751 case VMW_PL_MOB:
753 * "Guest Memory Regions" is an aperture like feature with
754 * one slot per bo. There is an upper limit of the number of
755 * slots as well as the bo size.
757 man->func = &vmw_gmrid_manager_func;
758 man->gpu_offset = 0;
759 man->flags = TTM_MEMTYPE_FLAG_CMA | TTM_MEMTYPE_FLAG_MAPPABLE;
760 man->available_caching = TTM_PL_FLAG_CACHED;
761 man->default_caching = TTM_PL_FLAG_CACHED;
762 break;
763 default:
764 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
765 return -EINVAL;
767 return 0;
770 static void vmw_evict_flags(struct ttm_buffer_object *bo,
771 struct ttm_placement *placement)
773 *placement = vmw_sys_placement;
776 static int vmw_verify_access(struct ttm_buffer_object *bo, struct file *filp)
778 struct ttm_object_file *tfile =
779 vmw_fpriv((struct drm_file *)filp->private_data)->tfile;
781 return vmw_user_dmabuf_verify_access(bo, tfile);
784 static int vmw_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
786 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
787 struct vmw_private *dev_priv = container_of(bdev, struct vmw_private, bdev);
789 mem->bus.addr = NULL;
790 mem->bus.is_iomem = false;
791 mem->bus.offset = 0;
792 mem->bus.size = mem->num_pages << PAGE_SHIFT;
793 mem->bus.base = 0;
794 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
795 return -EINVAL;
796 switch (mem->mem_type) {
797 case TTM_PL_SYSTEM:
798 case VMW_PL_GMR:
799 case VMW_PL_MOB:
800 return 0;
801 case TTM_PL_VRAM:
802 mem->bus.offset = mem->start << PAGE_SHIFT;
803 mem->bus.base = dev_priv->vram_start;
804 mem->bus.is_iomem = true;
805 break;
806 default:
807 return -EINVAL;
809 return 0;
812 static void vmw_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
816 static int vmw_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
818 return 0;
822 * vmw_move_notify - TTM move_notify_callback
824 * @bo: The TTM buffer object about to move.
825 * @mem: The struct ttm_mem_reg indicating to what memory
826 * region the move is taking place.
828 * Calls move_notify for all subsystems needing it.
829 * (currently only resources).
831 static void vmw_move_notify(struct ttm_buffer_object *bo,
832 bool evict,
833 struct ttm_mem_reg *mem)
835 vmw_resource_move_notify(bo, mem);
836 vmw_query_move_notify(bo, mem);
841 * vmw_swap_notify - TTM move_notify_callback
843 * @bo: The TTM buffer object about to be swapped out.
845 static void vmw_swap_notify(struct ttm_buffer_object *bo)
847 (void) ttm_bo_wait(bo, false, false);
851 struct ttm_bo_driver vmw_bo_driver = {
852 .ttm_tt_create = &vmw_ttm_tt_create,
853 .ttm_tt_populate = &vmw_ttm_populate,
854 .ttm_tt_unpopulate = &vmw_ttm_unpopulate,
855 .invalidate_caches = vmw_invalidate_caches,
856 .init_mem_type = vmw_init_mem_type,
857 .eviction_valuable = ttm_bo_eviction_valuable,
858 .evict_flags = vmw_evict_flags,
859 .move = NULL,
860 .verify_access = vmw_verify_access,
861 .move_notify = vmw_move_notify,
862 .swap_notify = vmw_swap_notify,
863 .fault_reserve_notify = &vmw_ttm_fault_reserve_notify,
864 .io_mem_reserve = &vmw_ttm_io_mem_reserve,
865 .io_mem_free = &vmw_ttm_io_mem_free,