Merge tag 'drm-misc-next-2020-10-27' of git://anongit.freedesktop.org/drm/drm-misc...
[sfrench/cifs-2.6.git] / drivers / gpu / drm / vmwgfx / vmwgfx_ttm_buffer.c
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
3  *
4  * Copyright 2009-2015 VMware, Inc., Palo Alto, CA., USA
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 const struct ttm_place vram_placement_flags = {
34         .fpfn = 0,
35         .lpfn = 0,
36         .mem_type = TTM_PL_VRAM,
37         .flags = 0
38 };
39
40 static const struct ttm_place sys_placement_flags = {
41         .fpfn = 0,
42         .lpfn = 0,
43         .mem_type = TTM_PL_SYSTEM,
44         .flags = 0
45 };
46
47 static const struct ttm_place gmr_placement_flags = {
48         .fpfn = 0,
49         .lpfn = 0,
50         .mem_type = VMW_PL_GMR,
51         .flags = 0
52 };
53
54 static const struct ttm_place mob_placement_flags = {
55         .fpfn = 0,
56         .lpfn = 0,
57         .mem_type = VMW_PL_MOB,
58         .flags = 0
59 };
60
61 struct ttm_placement vmw_vram_placement = {
62         .num_placement = 1,
63         .placement = &vram_placement_flags,
64         .num_busy_placement = 1,
65         .busy_placement = &vram_placement_flags
66 };
67
68 static const struct ttm_place vram_gmr_placement_flags[] = {
69         {
70                 .fpfn = 0,
71                 .lpfn = 0,
72                 .mem_type = TTM_PL_VRAM,
73                 .flags = 0
74         }, {
75                 .fpfn = 0,
76                 .lpfn = 0,
77                 .mem_type = VMW_PL_GMR,
78                 .flags = 0
79         }
80 };
81
82 static const struct ttm_place gmr_vram_placement_flags[] = {
83         {
84                 .fpfn = 0,
85                 .lpfn = 0,
86                 .mem_type = VMW_PL_GMR,
87                 .flags = 0
88         }, {
89                 .fpfn = 0,
90                 .lpfn = 0,
91                 .mem_type = TTM_PL_VRAM,
92                 .flags = 0
93         }
94 };
95
96 struct ttm_placement vmw_vram_gmr_placement = {
97         .num_placement = 2,
98         .placement = vram_gmr_placement_flags,
99         .num_busy_placement = 1,
100         .busy_placement = &gmr_placement_flags
101 };
102
103 struct ttm_placement vmw_vram_sys_placement = {
104         .num_placement = 1,
105         .placement = &vram_placement_flags,
106         .num_busy_placement = 1,
107         .busy_placement = &sys_placement_flags
108 };
109
110 struct ttm_placement vmw_sys_placement = {
111         .num_placement = 1,
112         .placement = &sys_placement_flags,
113         .num_busy_placement = 1,
114         .busy_placement = &sys_placement_flags
115 };
116
117 static const struct ttm_place evictable_placement_flags[] = {
118         {
119                 .fpfn = 0,
120                 .lpfn = 0,
121                 .mem_type = TTM_PL_SYSTEM,
122                 .flags = 0
123         }, {
124                 .fpfn = 0,
125                 .lpfn = 0,
126                 .mem_type = TTM_PL_VRAM,
127                 .flags = 0
128         }, {
129                 .fpfn = 0,
130                 .lpfn = 0,
131                 .mem_type = VMW_PL_GMR,
132                 .flags = 0
133         }, {
134                 .fpfn = 0,
135                 .lpfn = 0,
136                 .mem_type = VMW_PL_MOB,
137                 .flags = 0
138         }
139 };
140
141 static const struct ttm_place nonfixed_placement_flags[] = {
142         {
143                 .fpfn = 0,
144                 .lpfn = 0,
145                 .mem_type = TTM_PL_SYSTEM,
146                 .flags = 0
147         }, {
148                 .fpfn = 0,
149                 .lpfn = 0,
150                 .mem_type = VMW_PL_GMR,
151                 .flags = 0
152         }, {
153                 .fpfn = 0,
154                 .lpfn = 0,
155                 .mem_type = VMW_PL_MOB,
156                 .flags = 0
157         }
158 };
159
160 struct ttm_placement vmw_evictable_placement = {
161         .num_placement = 4,
162         .placement = evictable_placement_flags,
163         .num_busy_placement = 1,
164         .busy_placement = &sys_placement_flags
165 };
166
167 struct ttm_placement vmw_srf_placement = {
168         .num_placement = 1,
169         .num_busy_placement = 2,
170         .placement = &gmr_placement_flags,
171         .busy_placement = gmr_vram_placement_flags
172 };
173
174 struct ttm_placement vmw_mob_placement = {
175         .num_placement = 1,
176         .num_busy_placement = 1,
177         .placement = &mob_placement_flags,
178         .busy_placement = &mob_placement_flags
179 };
180
181 struct ttm_placement vmw_nonfixed_placement = {
182         .num_placement = 3,
183         .placement = nonfixed_placement_flags,
184         .num_busy_placement = 1,
185         .busy_placement = &sys_placement_flags
186 };
187
188 struct vmw_ttm_tt {
189         struct ttm_tt dma_ttm;
190         struct vmw_private *dev_priv;
191         int gmr_id;
192         struct vmw_mob *mob;
193         int mem_type;
194         struct sg_table sgt;
195         struct vmw_sg_table vsgt;
196         uint64_t sg_alloc_size;
197         bool mapped;
198         bool bound;
199 };
200
201 const size_t vmw_tt_size = sizeof(struct vmw_ttm_tt);
202
203 /**
204  * Helper functions to advance a struct vmw_piter iterator.
205  *
206  * @viter: Pointer to the iterator.
207  *
208  * These functions return false if past the end of the list,
209  * true otherwise. Functions are selected depending on the current
210  * DMA mapping mode.
211  */
212 static bool __vmw_piter_non_sg_next(struct vmw_piter *viter)
213 {
214         return ++(viter->i) < viter->num_pages;
215 }
216
217 static bool __vmw_piter_sg_next(struct vmw_piter *viter)
218 {
219         bool ret = __vmw_piter_non_sg_next(viter);
220
221         return __sg_page_iter_dma_next(&viter->iter) && ret;
222 }
223
224
225 /**
226  * Helper functions to return a pointer to the current page.
227  *
228  * @viter: Pointer to the iterator
229  *
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.
233  */
234 static struct page *__vmw_piter_non_sg_page(struct vmw_piter *viter)
235 {
236         return viter->pages[viter->i];
237 }
238
239 /**
240  * Helper functions to return the DMA address of the current page.
241  *
242  * @viter: Pointer to the iterator
243  *
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.
247  */
248 static dma_addr_t __vmw_piter_phys_addr(struct vmw_piter *viter)
249 {
250         return page_to_phys(viter->pages[viter->i]);
251 }
252
253 static dma_addr_t __vmw_piter_dma_addr(struct vmw_piter *viter)
254 {
255         return viter->addrs[viter->i];
256 }
257
258 static dma_addr_t __vmw_piter_sg_addr(struct vmw_piter *viter)
259 {
260         return sg_page_iter_dma_address(&viter->iter);
261 }
262
263
264 /**
265  * vmw_piter_start - Initialize a struct vmw_piter.
266  *
267  * @viter: Pointer to the iterator to initialize
268  * @vsgt: Pointer to a struct vmw_sg_table to initialize from
269  *
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.
273  */
274 void vmw_piter_start(struct vmw_piter *viter, const struct vmw_sg_table *vsgt,
275                      unsigned long p_offset)
276 {
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) {
282         case vmw_dma_phys:
283                 viter->next = &__vmw_piter_non_sg_next;
284                 viter->dma_address = &__vmw_piter_phys_addr;
285                 break;
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;
290                 break;
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);
297                 break;
298         default:
299                 BUG();
300         }
301 }
302
303 /**
304  * vmw_ttm_unmap_from_dma - unmap  device addresses previsouly mapped for
305  * TTM pages
306  *
307  * @vmw_tt: Pointer to a struct vmw_ttm_backend
308  *
309  * Used to free dma mappings previously mapped by vmw_ttm_map_for_dma.
310  */
311 static void vmw_ttm_unmap_from_dma(struct vmw_ttm_tt *vmw_tt)
312 {
313         struct device *dev = vmw_tt->dev_priv->dev->dev;
314
315         dma_unmap_sgtable(dev, &vmw_tt->sgt, DMA_BIDIRECTIONAL, 0);
316         vmw_tt->sgt.nents = vmw_tt->sgt.orig_nents;
317 }
318
319 /**
320  * vmw_ttm_map_for_dma - map TTM pages to get device addresses
321  *
322  * @vmw_tt: Pointer to a struct vmw_ttm_backend
323  *
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.
331  */
332 static int vmw_ttm_map_for_dma(struct vmw_ttm_tt *vmw_tt)
333 {
334         struct device *dev = vmw_tt->dev_priv->dev->dev;
335
336         return dma_map_sgtable(dev, &vmw_tt->sgt, DMA_BIDIRECTIONAL, 0);
337 }
338
339 /**
340  * vmw_ttm_map_dma - Make sure TTM pages are visible to the device
341  *
342  * @vmw_tt: Pointer to a struct vmw_ttm_tt
343  *
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.
348  */
349 static int vmw_ttm_map_dma(struct vmw_ttm_tt *vmw_tt)
350 {
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,
356                 .no_wait_gpu = false
357         };
358         struct vmw_piter iter;
359         dma_addr_t old;
360         int ret = 0;
361         static size_t sgl_size;
362         static size_t sgt_size;
363         struct scatterlist *sg;
364
365         if (vmw_tt->mapped)
366                 return 0;
367
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;
373
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));
380                 }
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))
384                         return ret;
385
386                 sg = __sg_alloc_table_from_pages(&vmw_tt->sgt, vsgt->pages,
387                                 vsgt->num_pages, 0,
388                                 (unsigned long) vsgt->num_pages << PAGE_SHIFT,
389                                 dma_get_max_seg_size(dev_priv->dev->dev),
390                                 NULL, 0, GFP_KERNEL);
391                 if (IS_ERR(sg)) {
392                         ret = PTR_ERR(sg);
393                         goto out_sg_alloc_fail;
394                 }
395
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);
400
401                         ttm_mem_global_free(glob, over_alloc);
402                         vmw_tt->sg_alloc_size -= over_alloc;
403                 }
404
405                 ret = vmw_ttm_map_for_dma(vmw_tt);
406                 if (unlikely(ret != 0))
407                         goto out_map_fail;
408
409                 break;
410         default:
411                 break;
412         }
413
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);
418
419                 if (cur != old + PAGE_SIZE)
420                         vmw_tt->vsgt.num_regions++;
421                 old = cur;
422         }
423
424         vmw_tt->mapped = true;
425         return 0;
426
427 out_map_fail:
428         sg_free_table(vmw_tt->vsgt.sgt);
429         vmw_tt->vsgt.sgt = NULL;
430 out_sg_alloc_fail:
431         ttm_mem_global_free(glob, vmw_tt->sg_alloc_size);
432         return ret;
433 }
434
435 /**
436  * vmw_ttm_unmap_dma - Tear down any TTM page device mappings
437  *
438  * @vmw_tt: Pointer to a struct vmw_ttm_tt
439  *
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.
443  */
444 static void vmw_ttm_unmap_dma(struct vmw_ttm_tt *vmw_tt)
445 {
446         struct vmw_private *dev_priv = vmw_tt->dev_priv;
447
448         if (!vmw_tt->vsgt.sgt)
449                 return;
450
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);
459                 break;
460         default:
461                 break;
462         }
463         vmw_tt->mapped = false;
464 }
465
466 /**
467  * vmw_bo_sg_table - Return a struct vmw_sg_table object for a
468  * TTM buffer object
469  *
470  * @bo: Pointer to a struct ttm_buffer_object
471  *
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.
476  */
477 const struct vmw_sg_table *vmw_bo_sg_table(struct ttm_buffer_object *bo)
478 {
479         struct vmw_ttm_tt *vmw_tt =
480                 container_of(bo->ttm, struct vmw_ttm_tt, dma_ttm);
481
482         return &vmw_tt->vsgt;
483 }
484
485
486 static int vmw_ttm_bind(struct ttm_bo_device *bdev,
487                         struct ttm_tt *ttm, struct ttm_resource *bo_mem)
488 {
489         struct vmw_ttm_tt *vmw_be =
490                 container_of(ttm, struct vmw_ttm_tt, dma_ttm);
491         int ret = 0;
492
493         if (!bo_mem)
494                 return -EINVAL;
495
496         if (vmw_be->bound)
497                 return 0;
498
499         ret = vmw_ttm_map_dma(vmw_be);
500         if (unlikely(ret != 0))
501                 return ret;
502
503         vmw_be->gmr_id = bo_mem->start;
504         vmw_be->mem_type = bo_mem->mem_type;
505
506         switch (bo_mem->mem_type) {
507         case VMW_PL_GMR:
508                 ret = vmw_gmr_bind(vmw_be->dev_priv, &vmw_be->vsgt,
509                                     ttm->num_pages, vmw_be->gmr_id);
510                 break;
511         case VMW_PL_MOB:
512                 if (unlikely(vmw_be->mob == NULL)) {
513                         vmw_be->mob =
514                                 vmw_mob_create(ttm->num_pages);
515                         if (unlikely(vmw_be->mob == NULL))
516                                 return -ENOMEM;
517                 }
518
519                 ret = vmw_mob_bind(vmw_be->dev_priv, vmw_be->mob,
520                                     &vmw_be->vsgt, ttm->num_pages,
521                                     vmw_be->gmr_id);
522                 break;
523         default:
524                 BUG();
525         }
526         vmw_be->bound = true;
527         return ret;
528 }
529
530 static void vmw_ttm_unbind(struct ttm_bo_device *bdev,
531                            struct ttm_tt *ttm)
532 {
533         struct vmw_ttm_tt *vmw_be =
534                 container_of(ttm, struct vmw_ttm_tt, dma_ttm);
535
536         if (!vmw_be->bound)
537                 return;
538
539         switch (vmw_be->mem_type) {
540         case VMW_PL_GMR:
541                 vmw_gmr_unbind(vmw_be->dev_priv, vmw_be->gmr_id);
542                 break;
543         case VMW_PL_MOB:
544                 vmw_mob_unbind(vmw_be->dev_priv, vmw_be->mob);
545                 break;
546         default:
547                 BUG();
548         }
549
550         if (vmw_be->dev_priv->map_mode == vmw_dma_map_bind)
551                 vmw_ttm_unmap_dma(vmw_be);
552         vmw_be->bound = false;
553 }
554
555
556 static void vmw_ttm_destroy(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
557 {
558         struct vmw_ttm_tt *vmw_be =
559                 container_of(ttm, struct vmw_ttm_tt, dma_ttm);
560
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);
566         else
567                 ttm_tt_fini(ttm);
568
569         if (vmw_be->mob)
570                 vmw_mob_destroy(vmw_be->mob);
571
572         kfree(vmw_be);
573 }
574
575
576 static int vmw_ttm_populate(struct ttm_bo_device *bdev,
577                             struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
578 {
579         struct vmw_ttm_tt *vmw_tt =
580                 container_of(ttm, struct vmw_ttm_tt, dma_ttm);
581         struct vmw_private *dev_priv = vmw_tt->dev_priv;
582         struct ttm_mem_global *glob = vmw_mem_glob(dev_priv);
583         int ret;
584
585         if (ttm_tt_is_populated(ttm))
586                 return 0;
587
588         if (dev_priv->map_mode == vmw_dma_alloc_coherent) {
589                 size_t size =
590                         ttm_round_pot(ttm->num_pages * sizeof(dma_addr_t));
591                 ret = ttm_mem_global_alloc(glob, size, ctx);
592                 if (unlikely(ret != 0))
593                         return ret;
594
595                 ret = ttm_dma_populate(&vmw_tt->dma_ttm, dev_priv->dev->dev,
596                                         ctx);
597                 if (unlikely(ret != 0))
598                         ttm_mem_global_free(glob, size);
599         } else
600                 ret = ttm_pool_populate(ttm, ctx);
601
602         return ret;
603 }
604
605 static void vmw_ttm_unpopulate(struct ttm_bo_device *bdev,
606                                struct ttm_tt *ttm)
607 {
608         struct vmw_ttm_tt *vmw_tt = container_of(ttm, struct vmw_ttm_tt,
609                                                  dma_ttm);
610         struct vmw_private *dev_priv = vmw_tt->dev_priv;
611         struct ttm_mem_global *glob = vmw_mem_glob(dev_priv);
612
613
614         if (vmw_tt->mob) {
615                 vmw_mob_destroy(vmw_tt->mob);
616                 vmw_tt->mob = NULL;
617         }
618
619         vmw_ttm_unmap_dma(vmw_tt);
620         if (dev_priv->map_mode == vmw_dma_alloc_coherent) {
621                 size_t size =
622                         ttm_round_pot(ttm->num_pages * sizeof(dma_addr_t));
623
624                 ttm_dma_unpopulate(&vmw_tt->dma_ttm, dev_priv->dev->dev);
625                 ttm_mem_global_free(glob, size);
626         } else
627                 ttm_pool_unpopulate(ttm);
628 }
629
630 static struct ttm_tt *vmw_ttm_tt_create(struct ttm_buffer_object *bo,
631                                         uint32_t page_flags)
632 {
633         struct vmw_ttm_tt *vmw_be;
634         int ret;
635
636         vmw_be = kzalloc(sizeof(*vmw_be), GFP_KERNEL);
637         if (!vmw_be)
638                 return NULL;
639
640         vmw_be->dev_priv = container_of(bo->bdev, struct vmw_private, bdev);
641         vmw_be->mob = NULL;
642
643         if (vmw_be->dev_priv->map_mode == vmw_dma_alloc_coherent)
644                 ret = ttm_dma_tt_init(&vmw_be->dma_ttm, bo, page_flags,
645                                       ttm_cached);
646         else
647                 ret = ttm_tt_init(&vmw_be->dma_ttm, bo, page_flags,
648                                   ttm_cached);
649         if (unlikely(ret != 0))
650                 goto out_no_init;
651
652         return &vmw_be->dma_ttm;
653 out_no_init:
654         kfree(vmw_be);
655         return NULL;
656 }
657
658 static void vmw_evict_flags(struct ttm_buffer_object *bo,
659                      struct ttm_placement *placement)
660 {
661         *placement = vmw_sys_placement;
662 }
663
664 static int vmw_verify_access(struct ttm_buffer_object *bo, struct file *filp)
665 {
666         struct ttm_object_file *tfile =
667                 vmw_fpriv((struct drm_file *)filp->private_data)->tfile;
668
669         return vmw_user_bo_verify_access(bo, tfile);
670 }
671
672 static int vmw_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_resource *mem)
673 {
674         struct vmw_private *dev_priv = container_of(bdev, struct vmw_private, bdev);
675
676         switch (mem->mem_type) {
677         case TTM_PL_SYSTEM:
678         case VMW_PL_GMR:
679         case VMW_PL_MOB:
680                 return 0;
681         case TTM_PL_VRAM:
682                 mem->bus.offset = (mem->start << PAGE_SHIFT) +
683                         dev_priv->vram_start;
684                 mem->bus.is_iomem = true;
685                 mem->bus.caching = ttm_cached;
686                 break;
687         default:
688                 return -EINVAL;
689         }
690         return 0;
691 }
692
693 /**
694  * vmw_move_notify - TTM move_notify_callback
695  *
696  * @bo: The TTM buffer object about to move.
697  * @mem: The struct ttm_resource indicating to what memory
698  *       region the move is taking place.
699  *
700  * Calls move_notify for all subsystems needing it.
701  * (currently only resources).
702  */
703 static void vmw_move_notify(struct ttm_buffer_object *bo,
704                             bool evict,
705                             struct ttm_resource *mem)
706 {
707         if (!mem)
708                 return;
709         vmw_bo_move_notify(bo, mem);
710         vmw_query_move_notify(bo, mem);
711 }
712
713
714 /**
715  * vmw_swap_notify - TTM move_notify_callback
716  *
717  * @bo: The TTM buffer object about to be swapped out.
718  */
719 static void vmw_swap_notify(struct ttm_buffer_object *bo)
720 {
721         vmw_bo_swap_notify(bo);
722         (void) ttm_bo_wait(bo, false, false);
723 }
724
725 static int vmw_move(struct ttm_buffer_object *bo,
726                     bool evict,
727                     struct ttm_operation_ctx *ctx,
728                     struct ttm_resource *new_mem)
729 {
730         struct ttm_resource_manager *old_man = ttm_manager_type(bo->bdev, bo->mem.mem_type);
731         struct ttm_resource_manager *new_man = ttm_manager_type(bo->bdev, new_mem->mem_type);
732         int ret;
733
734         if (new_man->use_tt && new_mem->mem_type != TTM_PL_SYSTEM) {
735                 ret = vmw_ttm_bind(bo->bdev, bo->ttm, new_mem);
736                 if (ret)
737                         return ret;
738         }
739
740         vmw_move_notify(bo, evict, new_mem);
741
742         if (old_man->use_tt && new_man->use_tt) {
743                 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
744                         ttm_bo_assign_mem(bo, new_mem);
745                         return 0;
746                 }
747                 ret = ttm_bo_wait_ctx(bo, ctx);
748                 if (ret)
749                         goto fail;
750
751                 vmw_ttm_unbind(bo->bdev, bo->ttm);
752                 ttm_resource_free(bo, &bo->mem);
753                 ttm_bo_assign_mem(bo, new_mem);
754                 return 0;
755         } else {
756                 ret = ttm_bo_move_memcpy(bo, ctx, new_mem);
757                 if (ret)
758                         goto fail;
759         }
760         return 0;
761 fail:
762         swap(*new_mem, bo->mem);
763         vmw_move_notify(bo, false, new_mem);
764         swap(*new_mem, bo->mem);
765         return ret;
766 }
767
768 static void
769 vmw_delete_mem_notify(struct ttm_buffer_object *bo)
770 {
771         vmw_move_notify(bo, false, NULL);
772 }
773
774 struct ttm_bo_driver vmw_bo_driver = {
775         .ttm_tt_create = &vmw_ttm_tt_create,
776         .ttm_tt_populate = &vmw_ttm_populate,
777         .ttm_tt_unpopulate = &vmw_ttm_unpopulate,
778         .ttm_tt_destroy = &vmw_ttm_destroy,
779         .eviction_valuable = ttm_bo_eviction_valuable,
780         .evict_flags = vmw_evict_flags,
781         .move = vmw_move,
782         .verify_access = vmw_verify_access,
783         .delete_mem_notify = vmw_delete_mem_notify,
784         .swap_notify = vmw_swap_notify,
785         .io_mem_reserve = &vmw_ttm_io_mem_reserve,
786 };
787
788 int vmw_bo_create_and_populate(struct vmw_private *dev_priv,
789                                unsigned long bo_size,
790                                struct ttm_buffer_object **bo_p)
791 {
792         struct ttm_operation_ctx ctx = {
793                 .interruptible = false,
794                 .no_wait_gpu = false
795         };
796         struct ttm_buffer_object *bo;
797         int ret;
798
799         ret = vmw_bo_create_kernel(dev_priv, bo_size,
800                                    &vmw_sys_placement,
801                                    &bo);
802         if (unlikely(ret != 0))
803                 return ret;
804
805         ret = ttm_bo_reserve(bo, false, true, NULL);
806         BUG_ON(ret != 0);
807         ret = vmw_ttm_populate(bo->bdev, bo->ttm, &ctx);
808         if (likely(ret == 0)) {
809                 struct vmw_ttm_tt *vmw_tt =
810                         container_of(bo->ttm, struct vmw_ttm_tt, dma_ttm);
811                 ret = vmw_ttm_map_dma(vmw_tt);
812         }
813
814         ttm_bo_unreserve(bo);
815
816         if (likely(ret == 0))
817                 *bo_p = bo;
818         return ret;
819 }