]> Pileus Git - ~andy/linux/blob - drivers/gpu/drm/vmwgfx/vmwgfx_buffer.c
drm/vmwgfx: Hook up MOBs to TTM as a separate memory type
[~andy/linux] / drivers / gpu / drm / vmwgfx / vmwgfx_buffer.c
1 /**************************************************************************
2  *
3  * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
4  * All Rights Reserved.
5  *
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:
13  *
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.
17  *
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.
25  *
26  **************************************************************************/
27
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>
32
33 static uint32_t vram_placement_flags = TTM_PL_FLAG_VRAM |
34         TTM_PL_FLAG_CACHED;
35
36 static uint32_t vram_ne_placement_flags = TTM_PL_FLAG_VRAM |
37         TTM_PL_FLAG_CACHED |
38         TTM_PL_FLAG_NO_EVICT;
39
40 static uint32_t sys_placement_flags = TTM_PL_FLAG_SYSTEM |
41         TTM_PL_FLAG_CACHED;
42
43 static uint32_t sys_ne_placement_flags = TTM_PL_FLAG_SYSTEM |
44         TTM_PL_FLAG_CACHED |
45         TTM_PL_FLAG_NO_EVICT;
46
47 static uint32_t gmr_placement_flags = VMW_PL_FLAG_GMR |
48         TTM_PL_FLAG_CACHED;
49
50 static uint32_t gmr_ne_placement_flags = VMW_PL_FLAG_GMR |
51         TTM_PL_FLAG_CACHED |
52         TTM_PL_FLAG_NO_EVICT;
53
54 static uint32_t mob_placement_flags = VMW_PL_FLAG_MOB |
55         TTM_PL_FLAG_CACHED;
56
57 struct ttm_placement vmw_vram_placement = {
58         .fpfn = 0,
59         .lpfn = 0,
60         .num_placement = 1,
61         .placement = &vram_placement_flags,
62         .num_busy_placement = 1,
63         .busy_placement = &vram_placement_flags
64 };
65
66 static uint32_t vram_gmr_placement_flags[] = {
67         TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED,
68         VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED
69 };
70
71 static uint32_t gmr_vram_placement_flags[] = {
72         VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED,
73         TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED
74 };
75
76 struct ttm_placement vmw_vram_gmr_placement = {
77         .fpfn = 0,
78         .lpfn = 0,
79         .num_placement = 2,
80         .placement = vram_gmr_placement_flags,
81         .num_busy_placement = 1,
82         .busy_placement = &gmr_placement_flags
83 };
84
85 static uint32_t vram_gmr_ne_placement_flags[] = {
86         TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED | TTM_PL_FLAG_NO_EVICT,
87         VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED | TTM_PL_FLAG_NO_EVICT
88 };
89
90 struct ttm_placement vmw_vram_gmr_ne_placement = {
91         .fpfn = 0,
92         .lpfn = 0,
93         .num_placement = 2,
94         .placement = vram_gmr_ne_placement_flags,
95         .num_busy_placement = 1,
96         .busy_placement = &gmr_ne_placement_flags
97 };
98
99 struct ttm_placement vmw_vram_sys_placement = {
100         .fpfn = 0,
101         .lpfn = 0,
102         .num_placement = 1,
103         .placement = &vram_placement_flags,
104         .num_busy_placement = 1,
105         .busy_placement = &sys_placement_flags
106 };
107
108 struct ttm_placement vmw_vram_ne_placement = {
109         .fpfn = 0,
110         .lpfn = 0,
111         .num_placement = 1,
112         .placement = &vram_ne_placement_flags,
113         .num_busy_placement = 1,
114         .busy_placement = &vram_ne_placement_flags
115 };
116
117 struct ttm_placement vmw_sys_placement = {
118         .fpfn = 0,
119         .lpfn = 0,
120         .num_placement = 1,
121         .placement = &sys_placement_flags,
122         .num_busy_placement = 1,
123         .busy_placement = &sys_placement_flags
124 };
125
126 struct ttm_placement vmw_sys_ne_placement = {
127         .fpfn = 0,
128         .lpfn = 0,
129         .num_placement = 1,
130         .placement = &sys_ne_placement_flags,
131         .num_busy_placement = 1,
132         .busy_placement = &sys_ne_placement_flags
133 };
134
135 static uint32_t evictable_placement_flags[] = {
136         TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED,
137         TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED,
138         VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED,
139         VMW_PL_FLAG_MOB | TTM_PL_FLAG_CACHED
140 };
141
142 struct ttm_placement vmw_evictable_placement = {
143         .fpfn = 0,
144         .lpfn = 0,
145         .num_placement = 4,
146         .placement = evictable_placement_flags,
147         .num_busy_placement = 1,
148         .busy_placement = &sys_placement_flags
149 };
150
151 struct ttm_placement vmw_srf_placement = {
152         .fpfn = 0,
153         .lpfn = 0,
154         .num_placement = 1,
155         .num_busy_placement = 2,
156         .placement = &gmr_placement_flags,
157         .busy_placement = gmr_vram_placement_flags
158 };
159
160 struct ttm_placement vmw_mob_placement = {
161         .fpfn = 0,
162         .lpfn = 0,
163         .num_placement = 1,
164         .num_busy_placement = 1,
165         .placement = &mob_placement_flags,
166         .busy_placement = &mob_placement_flags
167 };
168
169 struct vmw_ttm_tt {
170         struct ttm_dma_tt dma_ttm;
171         struct vmw_private *dev_priv;
172         int gmr_id;
173         struct vmw_mob *mob;
174         int mem_type;
175         struct sg_table sgt;
176         struct vmw_sg_table vsgt;
177         uint64_t sg_alloc_size;
178         bool mapped;
179 };
180
181 const size_t vmw_tt_size = sizeof(struct vmw_ttm_tt);
182
183 /**
184  * Helper functions to advance a struct vmw_piter iterator.
185  *
186  * @viter: Pointer to the iterator.
187  *
188  * These functions return false if past the end of the list,
189  * true otherwise. Functions are selected depending on the current
190  * DMA mapping mode.
191  */
192 static bool __vmw_piter_non_sg_next(struct vmw_piter *viter)
193 {
194         return ++(viter->i) < viter->num_pages;
195 }
196
197 static bool __vmw_piter_sg_next(struct vmw_piter *viter)
198 {
199         return __sg_page_iter_next(&viter->iter);
200 }
201
202
203 /**
204  * Helper functions to return a pointer to the current page.
205  *
206  * @viter: Pointer to the iterator
207  *
208  * These functions return a pointer to the page currently
209  * pointed to by @viter. Functions are selected depending on the
210  * current mapping mode.
211  */
212 static struct page *__vmw_piter_non_sg_page(struct vmw_piter *viter)
213 {
214         return viter->pages[viter->i];
215 }
216
217 static struct page *__vmw_piter_sg_page(struct vmw_piter *viter)
218 {
219         return sg_page_iter_page(&viter->iter);
220 }
221
222
223 /**
224  * Helper functions to return the DMA address of the current page.
225  *
226  * @viter: Pointer to the iterator
227  *
228  * These functions return the DMA address of the page currently
229  * pointed to by @viter. Functions are selected depending on the
230  * current mapping mode.
231  */
232 static dma_addr_t __vmw_piter_phys_addr(struct vmw_piter *viter)
233 {
234         return page_to_phys(viter->pages[viter->i]);
235 }
236
237 static dma_addr_t __vmw_piter_dma_addr(struct vmw_piter *viter)
238 {
239         return viter->addrs[viter->i];
240 }
241
242 static dma_addr_t __vmw_piter_sg_addr(struct vmw_piter *viter)
243 {
244         return sg_page_iter_dma_address(&viter->iter);
245 }
246
247
248 /**
249  * vmw_piter_start - Initialize a struct vmw_piter.
250  *
251  * @viter: Pointer to the iterator to initialize
252  * @vsgt: Pointer to a struct vmw_sg_table to initialize from
253  *
254  * Note that we're following the convention of __sg_page_iter_start, so that
255  * the iterator doesn't point to a valid page after initialization; it has
256  * to be advanced one step first.
257  */
258 void vmw_piter_start(struct vmw_piter *viter, const struct vmw_sg_table *vsgt,
259                      unsigned long p_offset)
260 {
261         viter->i = p_offset - 1;
262         viter->num_pages = vsgt->num_pages;
263         switch (vsgt->mode) {
264         case vmw_dma_phys:
265                 viter->next = &__vmw_piter_non_sg_next;
266                 viter->dma_address = &__vmw_piter_phys_addr;
267                 viter->page = &__vmw_piter_non_sg_page;
268                 viter->pages = vsgt->pages;
269                 break;
270         case vmw_dma_alloc_coherent:
271                 viter->next = &__vmw_piter_non_sg_next;
272                 viter->dma_address = &__vmw_piter_dma_addr;
273                 viter->page = &__vmw_piter_non_sg_page;
274                 viter->addrs = vsgt->addrs;
275                 break;
276         case vmw_dma_map_populate:
277         case vmw_dma_map_bind:
278                 viter->next = &__vmw_piter_sg_next;
279                 viter->dma_address = &__vmw_piter_sg_addr;
280                 viter->page = &__vmw_piter_sg_page;
281                 __sg_page_iter_start(&viter->iter, vsgt->sgt->sgl,
282                                      vsgt->sgt->orig_nents, p_offset);
283                 break;
284         default:
285                 BUG();
286         }
287 }
288
289 /**
290  * vmw_ttm_unmap_from_dma - unmap  device addresses previsouly mapped for
291  * TTM pages
292  *
293  * @vmw_tt: Pointer to a struct vmw_ttm_backend
294  *
295  * Used to free dma mappings previously mapped by vmw_ttm_map_for_dma.
296  */
297 static void vmw_ttm_unmap_from_dma(struct vmw_ttm_tt *vmw_tt)
298 {
299         struct device *dev = vmw_tt->dev_priv->dev->dev;
300
301         dma_unmap_sg(dev, vmw_tt->sgt.sgl, vmw_tt->sgt.nents,
302                 DMA_BIDIRECTIONAL);
303         vmw_tt->sgt.nents = vmw_tt->sgt.orig_nents;
304 }
305
306 /**
307  * vmw_ttm_map_for_dma - map TTM pages to get device addresses
308  *
309  * @vmw_tt: Pointer to a struct vmw_ttm_backend
310  *
311  * This function is used to get device addresses from the kernel DMA layer.
312  * However, it's violating the DMA API in that when this operation has been
313  * performed, it's illegal for the CPU to write to the pages without first
314  * unmapping the DMA mappings, or calling dma_sync_sg_for_cpu(). It is
315  * therefore only legal to call this function if we know that the function
316  * dma_sync_sg_for_cpu() is a NOP, and dma_sync_sg_for_device() is at most
317  * a CPU write buffer flush.
318  */
319 static int vmw_ttm_map_for_dma(struct vmw_ttm_tt *vmw_tt)
320 {
321         struct device *dev = vmw_tt->dev_priv->dev->dev;
322         int ret;
323
324         ret = dma_map_sg(dev, vmw_tt->sgt.sgl, vmw_tt->sgt.orig_nents,
325                          DMA_BIDIRECTIONAL);
326         if (unlikely(ret == 0))
327                 return -ENOMEM;
328
329         vmw_tt->sgt.nents = ret;
330
331         return 0;
332 }
333
334 /**
335  * vmw_ttm_map_dma - Make sure TTM pages are visible to the device
336  *
337  * @vmw_tt: Pointer to a struct vmw_ttm_tt
338  *
339  * Select the correct function for and make sure the TTM pages are
340  * visible to the device. Allocate storage for the device mappings.
341  * If a mapping has already been performed, indicated by the storage
342  * pointer being non NULL, the function returns success.
343  */
344 static int vmw_ttm_map_dma(struct vmw_ttm_tt *vmw_tt)
345 {
346         struct vmw_private *dev_priv = vmw_tt->dev_priv;
347         struct ttm_mem_global *glob = vmw_mem_glob(dev_priv);
348         struct vmw_sg_table *vsgt = &vmw_tt->vsgt;
349         struct vmw_piter iter;
350         dma_addr_t old;
351         int ret = 0;
352         static size_t sgl_size;
353         static size_t sgt_size;
354
355         if (vmw_tt->mapped)
356                 return 0;
357
358         vsgt->mode = dev_priv->map_mode;
359         vsgt->pages = vmw_tt->dma_ttm.ttm.pages;
360         vsgt->num_pages = vmw_tt->dma_ttm.ttm.num_pages;
361         vsgt->addrs = vmw_tt->dma_ttm.dma_address;
362         vsgt->sgt = &vmw_tt->sgt;
363
364         switch (dev_priv->map_mode) {
365         case vmw_dma_map_bind:
366         case vmw_dma_map_populate:
367                 if (unlikely(!sgl_size)) {
368                         sgl_size = ttm_round_pot(sizeof(struct scatterlist));
369                         sgt_size = ttm_round_pot(sizeof(struct sg_table));
370                 }
371                 vmw_tt->sg_alloc_size = sgt_size + sgl_size * vsgt->num_pages;
372                 ret = ttm_mem_global_alloc(glob, vmw_tt->sg_alloc_size, false,
373                                            true);
374                 if (unlikely(ret != 0))
375                         return ret;
376
377                 ret = sg_alloc_table_from_pages(&vmw_tt->sgt, vsgt->pages,
378                                                 vsgt->num_pages, 0,
379                                                 (unsigned long)
380                                                 vsgt->num_pages << PAGE_SHIFT,
381                                                 GFP_KERNEL);
382                 if (unlikely(ret != 0))
383                         goto out_sg_alloc_fail;
384
385                 if (vsgt->num_pages > vmw_tt->sgt.nents) {
386                         uint64_t over_alloc =
387                                 sgl_size * (vsgt->num_pages -
388                                             vmw_tt->sgt.nents);
389
390                         ttm_mem_global_free(glob, over_alloc);
391                         vmw_tt->sg_alloc_size -= over_alloc;
392                 }
393
394                 ret = vmw_ttm_map_for_dma(vmw_tt);
395                 if (unlikely(ret != 0))
396                         goto out_map_fail;
397
398                 break;
399         default:
400                 break;
401         }
402
403         old = ~((dma_addr_t) 0);
404         vmw_tt->vsgt.num_regions = 0;
405         for (vmw_piter_start(&iter, vsgt, 0); vmw_piter_next(&iter);) {
406                 dma_addr_t cur = vmw_piter_dma_addr(&iter);
407
408                 if (cur != old + PAGE_SIZE)
409                         vmw_tt->vsgt.num_regions++;
410                 old = cur;
411         }
412
413         vmw_tt->mapped = true;
414         return 0;
415
416 out_map_fail:
417         sg_free_table(vmw_tt->vsgt.sgt);
418         vmw_tt->vsgt.sgt = NULL;
419 out_sg_alloc_fail:
420         ttm_mem_global_free(glob, vmw_tt->sg_alloc_size);
421         return ret;
422 }
423
424 /**
425  * vmw_ttm_unmap_dma - Tear down any TTM page device mappings
426  *
427  * @vmw_tt: Pointer to a struct vmw_ttm_tt
428  *
429  * Tear down any previously set up device DMA mappings and free
430  * any storage space allocated for them. If there are no mappings set up,
431  * this function is a NOP.
432  */
433 static void vmw_ttm_unmap_dma(struct vmw_ttm_tt *vmw_tt)
434 {
435         struct vmw_private *dev_priv = vmw_tt->dev_priv;
436
437         if (!vmw_tt->vsgt.sgt)
438                 return;
439
440         switch (dev_priv->map_mode) {
441         case vmw_dma_map_bind:
442         case vmw_dma_map_populate:
443                 vmw_ttm_unmap_from_dma(vmw_tt);
444                 sg_free_table(vmw_tt->vsgt.sgt);
445                 vmw_tt->vsgt.sgt = NULL;
446                 ttm_mem_global_free(vmw_mem_glob(dev_priv),
447                                     vmw_tt->sg_alloc_size);
448                 break;
449         default:
450                 break;
451         }
452         vmw_tt->mapped = false;
453 }
454
455 static int vmw_ttm_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
456 {
457         struct vmw_ttm_tt *vmw_be =
458                 container_of(ttm, struct vmw_ttm_tt, dma_ttm.ttm);
459         int ret;
460
461         ret = vmw_ttm_map_dma(vmw_be);
462         if (unlikely(ret != 0))
463                 return ret;
464
465         vmw_be->gmr_id = bo_mem->start;
466         vmw_be->mem_type = bo_mem->mem_type;
467
468         switch (bo_mem->mem_type) {
469         case VMW_PL_GMR:
470                 return vmw_gmr_bind(vmw_be->dev_priv, &vmw_be->vsgt,
471                                     ttm->num_pages, vmw_be->gmr_id);
472         case VMW_PL_MOB:
473                 if (unlikely(vmw_be->mob == NULL)) {
474                         vmw_be->mob =
475                                 vmw_mob_create(ttm->num_pages);
476                         if (unlikely(vmw_be->mob == NULL))
477                                 return -ENOMEM;
478                 }
479
480                 return vmw_mob_bind(vmw_be->dev_priv, vmw_be->mob,
481                                     ttm->pages, ttm->num_pages,
482                                     vmw_be->gmr_id);
483         default:
484                 BUG();
485         }
486         return 0;
487 }
488
489 static int vmw_ttm_unbind(struct ttm_tt *ttm)
490 {
491         struct vmw_ttm_tt *vmw_be =
492                 container_of(ttm, struct vmw_ttm_tt, dma_ttm.ttm);
493
494         switch (vmw_be->mem_type) {
495         case VMW_PL_GMR:
496                 vmw_gmr_unbind(vmw_be->dev_priv, vmw_be->gmr_id);
497                 break;
498         case VMW_PL_MOB:
499                 vmw_mob_unbind(vmw_be->dev_priv, vmw_be->mob);
500                 break;
501         default:
502                 BUG();
503         }
504
505         if (vmw_be->dev_priv->map_mode == vmw_dma_map_bind)
506                 vmw_ttm_unmap_dma(vmw_be);
507
508         return 0;
509 }
510
511
512 static void vmw_ttm_destroy(struct ttm_tt *ttm)
513 {
514         struct vmw_ttm_tt *vmw_be =
515                 container_of(ttm, struct vmw_ttm_tt, dma_ttm.ttm);
516
517         vmw_ttm_unmap_dma(vmw_be);
518         if (vmw_be->dev_priv->map_mode == vmw_dma_alloc_coherent)
519                 ttm_dma_tt_fini(&vmw_be->dma_ttm);
520         else
521                 ttm_tt_fini(ttm);
522
523         if (vmw_be->mob)
524                 vmw_mob_destroy(vmw_be->mob);
525
526         kfree(vmw_be);
527 }
528
529 static int vmw_ttm_populate(struct ttm_tt *ttm)
530 {
531         struct vmw_ttm_tt *vmw_tt =
532                 container_of(ttm, struct vmw_ttm_tt, dma_ttm.ttm);
533         struct vmw_private *dev_priv = vmw_tt->dev_priv;
534         struct ttm_mem_global *glob = vmw_mem_glob(dev_priv);
535         int ret;
536
537         if (ttm->state != tt_unpopulated)
538                 return 0;
539
540         if (dev_priv->map_mode == vmw_dma_alloc_coherent) {
541                 size_t size =
542                         ttm_round_pot(ttm->num_pages * sizeof(dma_addr_t));
543                 ret = ttm_mem_global_alloc(glob, size, false, true);
544                 if (unlikely(ret != 0))
545                         return ret;
546
547                 ret = ttm_dma_populate(&vmw_tt->dma_ttm, dev_priv->dev->dev);
548                 if (unlikely(ret != 0))
549                         ttm_mem_global_free(glob, size);
550         } else
551                 ret = ttm_pool_populate(ttm);
552
553         return ret;
554 }
555
556 static void vmw_ttm_unpopulate(struct ttm_tt *ttm)
557 {
558         struct vmw_ttm_tt *vmw_tt = container_of(ttm, struct vmw_ttm_tt,
559                                                  dma_ttm.ttm);
560         struct vmw_private *dev_priv = vmw_tt->dev_priv;
561         struct ttm_mem_global *glob = vmw_mem_glob(dev_priv);
562
563
564         if (vmw_tt->mob) {
565                 vmw_mob_destroy(vmw_tt->mob);
566                 vmw_tt->mob = NULL;
567         }
568
569         vmw_ttm_unmap_dma(vmw_tt);
570         if (dev_priv->map_mode == vmw_dma_alloc_coherent) {
571                 size_t size =
572                         ttm_round_pot(ttm->num_pages * sizeof(dma_addr_t));
573
574                 ttm_dma_unpopulate(&vmw_tt->dma_ttm, dev_priv->dev->dev);
575                 ttm_mem_global_free(glob, size);
576         } else
577                 ttm_pool_unpopulate(ttm);
578 }
579
580 static struct ttm_backend_func vmw_ttm_func = {
581         .bind = vmw_ttm_bind,
582         .unbind = vmw_ttm_unbind,
583         .destroy = vmw_ttm_destroy,
584 };
585
586 static struct ttm_tt *vmw_ttm_tt_create(struct ttm_bo_device *bdev,
587                                  unsigned long size, uint32_t page_flags,
588                                  struct page *dummy_read_page)
589 {
590         struct vmw_ttm_tt *vmw_be;
591         int ret;
592
593         vmw_be = kzalloc(sizeof(*vmw_be), GFP_KERNEL);
594         if (!vmw_be)
595                 return NULL;
596
597         vmw_be->dma_ttm.ttm.func = &vmw_ttm_func;
598         vmw_be->dev_priv = container_of(bdev, struct vmw_private, bdev);
599         vmw_be->mob = NULL;
600
601         if (vmw_be->dev_priv->map_mode == vmw_dma_alloc_coherent)
602                 ret = ttm_dma_tt_init(&vmw_be->dma_ttm, bdev, size, page_flags,
603                                       dummy_read_page);
604         else
605                 ret = ttm_tt_init(&vmw_be->dma_ttm.ttm, bdev, size, page_flags,
606                                   dummy_read_page);
607         if (unlikely(ret != 0))
608                 goto out_no_init;
609
610         return &vmw_be->dma_ttm.ttm;
611 out_no_init:
612         kfree(vmw_be);
613         return NULL;
614 }
615
616 static int vmw_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
617 {
618         return 0;
619 }
620
621 static int vmw_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
622                       struct ttm_mem_type_manager *man)
623 {
624         switch (type) {
625         case TTM_PL_SYSTEM:
626                 /* System memory */
627
628                 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
629                 man->available_caching = TTM_PL_FLAG_CACHED;
630                 man->default_caching = TTM_PL_FLAG_CACHED;
631                 break;
632         case TTM_PL_VRAM:
633                 /* "On-card" video ram */
634                 man->func = &ttm_bo_manager_func;
635                 man->gpu_offset = 0;
636                 man->flags = TTM_MEMTYPE_FLAG_FIXED | TTM_MEMTYPE_FLAG_MAPPABLE;
637                 man->available_caching = TTM_PL_FLAG_CACHED;
638                 man->default_caching = TTM_PL_FLAG_CACHED;
639                 break;
640         case VMW_PL_GMR:
641         case VMW_PL_MOB:
642                 /*
643                  * "Guest Memory Regions" is an aperture like feature with
644                  *  one slot per bo. There is an upper limit of the number of
645                  *  slots as well as the bo size.
646                  */
647                 man->func = &vmw_gmrid_manager_func;
648                 man->gpu_offset = 0;
649                 man->flags = TTM_MEMTYPE_FLAG_CMA | TTM_MEMTYPE_FLAG_MAPPABLE;
650                 man->available_caching = TTM_PL_FLAG_CACHED;
651                 man->default_caching = TTM_PL_FLAG_CACHED;
652                 break;
653         default:
654                 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
655                 return -EINVAL;
656         }
657         return 0;
658 }
659
660 static void vmw_evict_flags(struct ttm_buffer_object *bo,
661                      struct ttm_placement *placement)
662 {
663         *placement = vmw_sys_placement;
664 }
665
666 static int vmw_verify_access(struct ttm_buffer_object *bo, struct file *filp)
667 {
668         struct ttm_object_file *tfile =
669                 vmw_fpriv((struct drm_file *)filp->private_data)->tfile;
670
671         return vmw_user_dmabuf_verify_access(bo, tfile);
672 }
673
674 static int vmw_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
675 {
676         struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
677         struct vmw_private *dev_priv = container_of(bdev, struct vmw_private, bdev);
678
679         mem->bus.addr = NULL;
680         mem->bus.is_iomem = false;
681         mem->bus.offset = 0;
682         mem->bus.size = mem->num_pages << PAGE_SHIFT;
683         mem->bus.base = 0;
684         if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
685                 return -EINVAL;
686         switch (mem->mem_type) {
687         case TTM_PL_SYSTEM:
688         case VMW_PL_GMR:
689         case VMW_PL_MOB:
690                 return 0;
691         case TTM_PL_VRAM:
692                 mem->bus.offset = mem->start << PAGE_SHIFT;
693                 mem->bus.base = dev_priv->vram_start;
694                 mem->bus.is_iomem = true;
695                 break;
696         default:
697                 return -EINVAL;
698         }
699         return 0;
700 }
701
702 static void vmw_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
703 {
704 }
705
706 static int vmw_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
707 {
708         return 0;
709 }
710
711 /**
712  * FIXME: We're using the old vmware polling method to sync.
713  * Do this with fences instead.
714  */
715
716 static void *vmw_sync_obj_ref(void *sync_obj)
717 {
718
719         return (void *)
720                 vmw_fence_obj_reference((struct vmw_fence_obj *) sync_obj);
721 }
722
723 static void vmw_sync_obj_unref(void **sync_obj)
724 {
725         vmw_fence_obj_unreference((struct vmw_fence_obj **) sync_obj);
726 }
727
728 static int vmw_sync_obj_flush(void *sync_obj)
729 {
730         vmw_fence_obj_flush((struct vmw_fence_obj *) sync_obj);
731         return 0;
732 }
733
734 static bool vmw_sync_obj_signaled(void *sync_obj)
735 {
736         return  vmw_fence_obj_signaled((struct vmw_fence_obj *) sync_obj,
737                                        DRM_VMW_FENCE_FLAG_EXEC);
738
739 }
740
741 static int vmw_sync_obj_wait(void *sync_obj, bool lazy, bool interruptible)
742 {
743         return vmw_fence_obj_wait((struct vmw_fence_obj *) sync_obj,
744                                   DRM_VMW_FENCE_FLAG_EXEC,
745                                   lazy, interruptible,
746                                   VMW_FENCE_WAIT_TIMEOUT);
747 }
748
749 /**
750  * vmw_move_notify - TTM move_notify_callback
751  *
752  * @bo:             The TTM buffer object about to move.
753  * @mem:            The truct ttm_mem_reg indicating to what memory
754  *                  region the move is taking place.
755  *
756  * Calls move_notify for all subsystems needing it.
757  * (currently only resources).
758  */
759 static void vmw_move_notify(struct ttm_buffer_object *bo,
760                             struct ttm_mem_reg *mem)
761 {
762         vmw_resource_move_notify(bo, mem);
763 }
764
765
766 /**
767  * vmw_swap_notify - TTM move_notify_callback
768  *
769  * @bo:             The TTM buffer object about to be swapped out.
770  */
771 static void vmw_swap_notify(struct ttm_buffer_object *bo)
772 {
773         struct ttm_bo_device *bdev = bo->bdev;
774
775         spin_lock(&bdev->fence_lock);
776         ttm_bo_wait(bo, false, false, false);
777         spin_unlock(&bdev->fence_lock);
778 }
779
780
781 struct ttm_bo_driver vmw_bo_driver = {
782         .ttm_tt_create = &vmw_ttm_tt_create,
783         .ttm_tt_populate = &vmw_ttm_populate,
784         .ttm_tt_unpopulate = &vmw_ttm_unpopulate,
785         .invalidate_caches = vmw_invalidate_caches,
786         .init_mem_type = vmw_init_mem_type,
787         .evict_flags = vmw_evict_flags,
788         .move = NULL,
789         .verify_access = vmw_verify_access,
790         .sync_obj_signaled = vmw_sync_obj_signaled,
791         .sync_obj_wait = vmw_sync_obj_wait,
792         .sync_obj_flush = vmw_sync_obj_flush,
793         .sync_obj_unref = vmw_sync_obj_unref,
794         .sync_obj_ref = vmw_sync_obj_ref,
795         .move_notify = vmw_move_notify,
796         .swap_notify = vmw_swap_notify,
797         .fault_reserve_notify = &vmw_ttm_fault_reserve_notify,
798         .io_mem_reserve = &vmw_ttm_io_mem_reserve,
799         .io_mem_free = &vmw_ttm_io_mem_free,
800 };