Merge branch 'omap-for-v5.13/genpd-cleanup' into omap-for-v5.14/cleanup
[linux-2.6-microblaze.git] / drivers / gpu / drm / vmwgfx / vmwgfx_mob.c
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
3  *
4  * Copyright 2012-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 <linux/highmem.h>
29
30 #include "vmwgfx_drv.h"
31
32 /*
33  * If we set up the screen target otable, screen objects stop working.
34  */
35
36 #define VMW_OTABLE_SETUP_SUB ((VMWGFX_ENABLE_SCREEN_TARGET_OTABLE ? 0 : 1))
37
38 #ifdef CONFIG_64BIT
39 #define VMW_PPN_SIZE 8
40 #define VMW_MOBFMT_PTDEPTH_0 SVGA3D_MOBFMT_PTDEPTH64_0
41 #define VMW_MOBFMT_PTDEPTH_1 SVGA3D_MOBFMT_PTDEPTH64_1
42 #define VMW_MOBFMT_PTDEPTH_2 SVGA3D_MOBFMT_PTDEPTH64_2
43 #else
44 #define VMW_PPN_SIZE 4
45 #define VMW_MOBFMT_PTDEPTH_0 SVGA3D_MOBFMT_PTDEPTH_0
46 #define VMW_MOBFMT_PTDEPTH_1 SVGA3D_MOBFMT_PTDEPTH_1
47 #define VMW_MOBFMT_PTDEPTH_2 SVGA3D_MOBFMT_PTDEPTH_2
48 #endif
49
50 /*
51  * struct vmw_mob - Structure containing page table and metadata for a
52  * Guest Memory OBject.
53  *
54  * @num_pages       Number of pages that make up the page table.
55  * @pt_level        The indirection level of the page table. 0-2.
56  * @pt_root_page    DMA address of the level 0 page of the page table.
57  */
58 struct vmw_mob {
59         struct ttm_buffer_object *pt_bo;
60         unsigned long num_pages;
61         unsigned pt_level;
62         dma_addr_t pt_root_page;
63         uint32_t id;
64 };
65
66 /*
67  * struct vmw_otable - Guest Memory OBject table metadata
68  *
69  * @size:           Size of the table (page-aligned).
70  * @page_table:     Pointer to a struct vmw_mob holding the page table.
71  */
72 static const struct vmw_otable pre_dx_tables[] = {
73         {VMWGFX_NUM_MOB * SVGA3D_OTABLE_MOB_ENTRY_SIZE, NULL, true},
74         {VMWGFX_NUM_GB_SURFACE * SVGA3D_OTABLE_SURFACE_ENTRY_SIZE, NULL, true},
75         {VMWGFX_NUM_GB_CONTEXT * SVGA3D_OTABLE_CONTEXT_ENTRY_SIZE, NULL, true},
76         {VMWGFX_NUM_GB_SHADER * SVGA3D_OTABLE_SHADER_ENTRY_SIZE, NULL, true},
77         {VMWGFX_NUM_GB_SCREEN_TARGET * SVGA3D_OTABLE_SCREEN_TARGET_ENTRY_SIZE,
78          NULL, VMWGFX_ENABLE_SCREEN_TARGET_OTABLE}
79 };
80
81 static const struct vmw_otable dx_tables[] = {
82         {VMWGFX_NUM_MOB * SVGA3D_OTABLE_MOB_ENTRY_SIZE, NULL, true},
83         {VMWGFX_NUM_GB_SURFACE * SVGA3D_OTABLE_SURFACE_ENTRY_SIZE, NULL, true},
84         {VMWGFX_NUM_GB_CONTEXT * SVGA3D_OTABLE_CONTEXT_ENTRY_SIZE, NULL, true},
85         {VMWGFX_NUM_GB_SHADER * SVGA3D_OTABLE_SHADER_ENTRY_SIZE, NULL, true},
86         {VMWGFX_NUM_GB_SCREEN_TARGET * SVGA3D_OTABLE_SCREEN_TARGET_ENTRY_SIZE,
87          NULL, VMWGFX_ENABLE_SCREEN_TARGET_OTABLE},
88         {VMWGFX_NUM_DXCONTEXT * sizeof(SVGAOTableDXContextEntry), NULL, true},
89 };
90
91 static int vmw_mob_pt_populate(struct vmw_private *dev_priv,
92                                struct vmw_mob *mob);
93 static void vmw_mob_pt_setup(struct vmw_mob *mob,
94                              struct vmw_piter data_iter,
95                              unsigned long num_data_pages);
96
97
98 static inline void vmw_bo_unpin_unlocked(struct ttm_buffer_object *bo)
99 {
100         int ret = ttm_bo_reserve(bo, false, true, NULL);
101         BUG_ON(ret != 0);
102         ttm_bo_unpin(bo);
103         ttm_bo_unreserve(bo);
104 }
105
106
107 /*
108  * vmw_setup_otable_base - Issue an object table base setup command to
109  * the device
110  *
111  * @dev_priv:       Pointer to a device private structure
112  * @type:           Type of object table base
113  * @offset          Start of table offset into dev_priv::otable_bo
114  * @otable          Pointer to otable metadata;
115  *
116  * This function returns -ENOMEM if it fails to reserve fifo space,
117  * and may block waiting for fifo space.
118  */
119 static int vmw_setup_otable_base(struct vmw_private *dev_priv,
120                                  SVGAOTableType type,
121                                  struct ttm_buffer_object *otable_bo,
122                                  unsigned long offset,
123                                  struct vmw_otable *otable)
124 {
125         struct {
126                 SVGA3dCmdHeader header;
127                 SVGA3dCmdSetOTableBase64 body;
128         } *cmd;
129         struct vmw_mob *mob;
130         const struct vmw_sg_table *vsgt;
131         struct vmw_piter iter;
132         int ret;
133
134         BUG_ON(otable->page_table != NULL);
135
136         vsgt = vmw_bo_sg_table(otable_bo);
137         vmw_piter_start(&iter, vsgt, offset >> PAGE_SHIFT);
138         WARN_ON(!vmw_piter_next(&iter));
139
140         mob = vmw_mob_create(otable->size >> PAGE_SHIFT);
141         if (unlikely(mob == NULL)) {
142                 DRM_ERROR("Failed creating OTable page table.\n");
143                 return -ENOMEM;
144         }
145
146         if (otable->size <= PAGE_SIZE) {
147                 mob->pt_level = VMW_MOBFMT_PTDEPTH_0;
148                 mob->pt_root_page = vmw_piter_dma_addr(&iter);
149         } else if (vsgt->num_regions == 1) {
150                 mob->pt_level = SVGA3D_MOBFMT_RANGE;
151                 mob->pt_root_page = vmw_piter_dma_addr(&iter);
152         } else {
153                 ret = vmw_mob_pt_populate(dev_priv, mob);
154                 if (unlikely(ret != 0))
155                         goto out_no_populate;
156
157                 vmw_mob_pt_setup(mob, iter, otable->size >> PAGE_SHIFT);
158                 mob->pt_level += VMW_MOBFMT_PTDEPTH_1 - SVGA3D_MOBFMT_PTDEPTH_1;
159         }
160
161         cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
162         if (unlikely(cmd == NULL)) {
163                 ret = -ENOMEM;
164                 goto out_no_fifo;
165         }
166
167         memset(cmd, 0, sizeof(*cmd));
168         cmd->header.id = SVGA_3D_CMD_SET_OTABLE_BASE64;
169         cmd->header.size = sizeof(cmd->body);
170         cmd->body.type = type;
171         cmd->body.baseAddress = mob->pt_root_page >> PAGE_SHIFT;
172         cmd->body.sizeInBytes = otable->size;
173         cmd->body.validSizeInBytes = 0;
174         cmd->body.ptDepth = mob->pt_level;
175
176         /*
177          * The device doesn't support this, But the otable size is
178          * determined at compile-time, so this BUG shouldn't trigger
179          * randomly.
180          */
181         BUG_ON(mob->pt_level == VMW_MOBFMT_PTDEPTH_2);
182
183         vmw_cmd_commit(dev_priv, sizeof(*cmd));
184         otable->page_table = mob;
185
186         return 0;
187
188 out_no_fifo:
189 out_no_populate:
190         vmw_mob_destroy(mob);
191         return ret;
192 }
193
194 /*
195  * vmw_takedown_otable_base - Issue an object table base takedown command
196  * to the device
197  *
198  * @dev_priv:       Pointer to a device private structure
199  * @type:           Type of object table base
200  *
201  */
202 static void vmw_takedown_otable_base(struct vmw_private *dev_priv,
203                                      SVGAOTableType type,
204                                      struct vmw_otable *otable)
205 {
206         struct {
207                 SVGA3dCmdHeader header;
208                 SVGA3dCmdSetOTableBase body;
209         } *cmd;
210         struct ttm_buffer_object *bo;
211
212         if (otable->page_table == NULL)
213                 return;
214
215         bo = otable->page_table->pt_bo;
216         cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
217         if (unlikely(cmd == NULL))
218                 return;
219
220         memset(cmd, 0, sizeof(*cmd));
221         cmd->header.id = SVGA_3D_CMD_SET_OTABLE_BASE;
222         cmd->header.size = sizeof(cmd->body);
223         cmd->body.type = type;
224         cmd->body.baseAddress = 0;
225         cmd->body.sizeInBytes = 0;
226         cmd->body.validSizeInBytes = 0;
227         cmd->body.ptDepth = SVGA3D_MOBFMT_INVALID;
228         vmw_cmd_commit(dev_priv, sizeof(*cmd));
229
230         if (bo) {
231                 int ret;
232
233                 ret = ttm_bo_reserve(bo, false, true, NULL);
234                 BUG_ON(ret != 0);
235
236                 vmw_bo_fence_single(bo, NULL);
237                 ttm_bo_unreserve(bo);
238         }
239
240         vmw_mob_destroy(otable->page_table);
241         otable->page_table = NULL;
242 }
243
244
245 static int vmw_otable_batch_setup(struct vmw_private *dev_priv,
246                                   struct vmw_otable_batch *batch)
247 {
248         unsigned long offset;
249         unsigned long bo_size;
250         struct vmw_otable *otables = batch->otables;
251         SVGAOTableType i;
252         int ret;
253
254         bo_size = 0;
255         for (i = 0; i < batch->num_otables; ++i) {
256                 if (!otables[i].enabled)
257                         continue;
258
259                 otables[i].size =
260                         (otables[i].size + PAGE_SIZE - 1) & PAGE_MASK;
261                 bo_size += otables[i].size;
262         }
263
264         ret = vmw_bo_create_and_populate(dev_priv, bo_size, &batch->otable_bo);
265         if (unlikely(ret != 0))
266                 return ret;
267
268         offset = 0;
269         for (i = 0; i < batch->num_otables; ++i) {
270                 if (!batch->otables[i].enabled)
271                         continue;
272
273                 ret = vmw_setup_otable_base(dev_priv, i, batch->otable_bo,
274                                             offset,
275                                             &otables[i]);
276                 if (unlikely(ret != 0))
277                         goto out_no_setup;
278                 offset += otables[i].size;
279         }
280
281         return 0;
282
283 out_no_setup:
284         for (i = 0; i < batch->num_otables; ++i) {
285                 if (batch->otables[i].enabled)
286                         vmw_takedown_otable_base(dev_priv, i,
287                                                  &batch->otables[i]);
288         }
289
290         vmw_bo_unpin_unlocked(batch->otable_bo);
291         ttm_bo_put(batch->otable_bo);
292         batch->otable_bo = NULL;
293         return ret;
294 }
295
296 /*
297  * vmw_otables_setup - Set up guest backed memory object tables
298  *
299  * @dev_priv:       Pointer to a device private structure
300  *
301  * Takes care of the device guest backed surface
302  * initialization, by setting up the guest backed memory object tables.
303  * Returns 0 on success and various error codes on failure. A successful return
304  * means the object tables can be taken down using the vmw_otables_takedown
305  * function.
306  */
307 int vmw_otables_setup(struct vmw_private *dev_priv)
308 {
309         struct vmw_otable **otables = &dev_priv->otable_batch.otables;
310         int ret;
311
312         if (has_sm4_context(dev_priv)) {
313                 *otables = kmemdup(dx_tables, sizeof(dx_tables), GFP_KERNEL);
314                 if (!(*otables))
315                         return -ENOMEM;
316
317                 dev_priv->otable_batch.num_otables = ARRAY_SIZE(dx_tables);
318         } else {
319                 *otables = kmemdup(pre_dx_tables, sizeof(pre_dx_tables),
320                                    GFP_KERNEL);
321                 if (!(*otables))
322                         return -ENOMEM;
323
324                 dev_priv->otable_batch.num_otables = ARRAY_SIZE(pre_dx_tables);
325         }
326
327         ret = vmw_otable_batch_setup(dev_priv, &dev_priv->otable_batch);
328         if (unlikely(ret != 0))
329                 goto out_setup;
330
331         return 0;
332
333 out_setup:
334         kfree(*otables);
335         return ret;
336 }
337
338 static void vmw_otable_batch_takedown(struct vmw_private *dev_priv,
339                                struct vmw_otable_batch *batch)
340 {
341         SVGAOTableType i;
342         struct ttm_buffer_object *bo = batch->otable_bo;
343         int ret;
344
345         for (i = 0; i < batch->num_otables; ++i)
346                 if (batch->otables[i].enabled)
347                         vmw_takedown_otable_base(dev_priv, i,
348                                                  &batch->otables[i]);
349
350         ret = ttm_bo_reserve(bo, false, true, NULL);
351         BUG_ON(ret != 0);
352
353         vmw_bo_fence_single(bo, NULL);
354         ttm_bo_unpin(bo);
355         ttm_bo_unreserve(bo);
356
357         ttm_bo_unpin(batch->otable_bo);
358         ttm_bo_put(batch->otable_bo);
359         batch->otable_bo = NULL;
360 }
361
362 /*
363  * vmw_otables_takedown - Take down guest backed memory object tables
364  *
365  * @dev_priv:       Pointer to a device private structure
366  *
367  * Take down the Guest Memory Object tables.
368  */
369 void vmw_otables_takedown(struct vmw_private *dev_priv)
370 {
371         vmw_otable_batch_takedown(dev_priv, &dev_priv->otable_batch);
372         kfree(dev_priv->otable_batch.otables);
373 }
374
375 /*
376  * vmw_mob_calculate_pt_pages - Calculate the number of page table pages
377  * needed for a guest backed memory object.
378  *
379  * @data_pages:  Number of data pages in the memory object buffer.
380  */
381 static unsigned long vmw_mob_calculate_pt_pages(unsigned long data_pages)
382 {
383         unsigned long data_size = data_pages * PAGE_SIZE;
384         unsigned long tot_size = 0;
385
386         while (likely(data_size > PAGE_SIZE)) {
387                 data_size = DIV_ROUND_UP(data_size, PAGE_SIZE);
388                 data_size *= VMW_PPN_SIZE;
389                 tot_size += (data_size + PAGE_SIZE - 1) & PAGE_MASK;
390         }
391
392         return tot_size >> PAGE_SHIFT;
393 }
394
395 /*
396  * vmw_mob_create - Create a mob, but don't populate it.
397  *
398  * @data_pages:  Number of data pages of the underlying buffer object.
399  */
400 struct vmw_mob *vmw_mob_create(unsigned long data_pages)
401 {
402         struct vmw_mob *mob = kzalloc(sizeof(*mob), GFP_KERNEL);
403
404         if (unlikely(!mob))
405                 return NULL;
406
407         mob->num_pages = vmw_mob_calculate_pt_pages(data_pages);
408
409         return mob;
410 }
411
412 /*
413  * vmw_mob_pt_populate - Populate the mob pagetable
414  *
415  * @mob:         Pointer to the mob the pagetable of which we want to
416  *               populate.
417  *
418  * This function allocates memory to be used for the pagetable, and
419  * adjusts TTM memory accounting accordingly. Returns ENOMEM if
420  * memory resources aren't sufficient and may cause TTM buffer objects
421  * to be swapped out by using the TTM memory accounting function.
422  */
423 static int vmw_mob_pt_populate(struct vmw_private *dev_priv,
424                                struct vmw_mob *mob)
425 {
426         BUG_ON(mob->pt_bo != NULL);
427
428         return vmw_bo_create_and_populate(dev_priv, mob->num_pages * PAGE_SIZE, &mob->pt_bo);
429 }
430
431 /**
432  * vmw_mob_assign_ppn - Assign a value to a page table entry
433  *
434  * @addr: Pointer to pointer to page table entry.
435  * @val: The page table entry
436  *
437  * Assigns a value to a page table entry pointed to by *@addr and increments
438  * *@addr according to the page table entry size.
439  */
440 #if (VMW_PPN_SIZE == 8)
441 static void vmw_mob_assign_ppn(u32 **addr, dma_addr_t val)
442 {
443         *((u64 *) *addr) = val >> PAGE_SHIFT;
444         *addr += 2;
445 }
446 #else
447 static void vmw_mob_assign_ppn(u32 **addr, dma_addr_t val)
448 {
449         *(*addr)++ = val >> PAGE_SHIFT;
450 }
451 #endif
452
453 /*
454  * vmw_mob_build_pt - Build a pagetable
455  *
456  * @data_addr:      Array of DMA addresses to the underlying buffer
457  *                  object's data pages.
458  * @num_data_pages: Number of buffer object data pages.
459  * @pt_pages:       Array of page pointers to the page table pages.
460  *
461  * Returns the number of page table pages actually used.
462  * Uses atomic kmaps of highmem pages to avoid TLB thrashing.
463  */
464 static unsigned long vmw_mob_build_pt(struct vmw_piter *data_iter,
465                                       unsigned long num_data_pages,
466                                       struct vmw_piter *pt_iter)
467 {
468         unsigned long pt_size = num_data_pages * VMW_PPN_SIZE;
469         unsigned long num_pt_pages = DIV_ROUND_UP(pt_size, PAGE_SIZE);
470         unsigned long pt_page;
471         u32 *addr, *save_addr;
472         unsigned long i;
473         struct page *page;
474
475         for (pt_page = 0; pt_page < num_pt_pages; ++pt_page) {
476                 page = vmw_piter_page(pt_iter);
477
478                 save_addr = addr = kmap_atomic(page);
479
480                 for (i = 0; i < PAGE_SIZE / VMW_PPN_SIZE; ++i) {
481                         vmw_mob_assign_ppn(&addr,
482                                            vmw_piter_dma_addr(data_iter));
483                         if (unlikely(--num_data_pages == 0))
484                                 break;
485                         WARN_ON(!vmw_piter_next(data_iter));
486                 }
487                 kunmap_atomic(save_addr);
488                 vmw_piter_next(pt_iter);
489         }
490
491         return num_pt_pages;
492 }
493
494 /*
495  * vmw_mob_build_pt - Set up a multilevel mob pagetable
496  *
497  * @mob:            Pointer to a mob whose page table needs setting up.
498  * @data_addr       Array of DMA addresses to the buffer object's data
499  *                  pages.
500  * @num_data_pages: Number of buffer object data pages.
501  *
502  * Uses tail recursion to set up a multilevel mob page table.
503  */
504 static void vmw_mob_pt_setup(struct vmw_mob *mob,
505                              struct vmw_piter data_iter,
506                              unsigned long num_data_pages)
507 {
508         unsigned long num_pt_pages = 0;
509         struct ttm_buffer_object *bo = mob->pt_bo;
510         struct vmw_piter save_pt_iter;
511         struct vmw_piter pt_iter;
512         const struct vmw_sg_table *vsgt;
513         int ret;
514
515         ret = ttm_bo_reserve(bo, false, true, NULL);
516         BUG_ON(ret != 0);
517
518         vsgt = vmw_bo_sg_table(bo);
519         vmw_piter_start(&pt_iter, vsgt, 0);
520         BUG_ON(!vmw_piter_next(&pt_iter));
521         mob->pt_level = 0;
522         while (likely(num_data_pages > 1)) {
523                 ++mob->pt_level;
524                 BUG_ON(mob->pt_level > 2);
525                 save_pt_iter = pt_iter;
526                 num_pt_pages = vmw_mob_build_pt(&data_iter, num_data_pages,
527                                                 &pt_iter);
528                 data_iter = save_pt_iter;
529                 num_data_pages = num_pt_pages;
530         }
531
532         mob->pt_root_page = vmw_piter_dma_addr(&save_pt_iter);
533         ttm_bo_unreserve(bo);
534 }
535
536 /*
537  * vmw_mob_destroy - Destroy a mob, unpopulating first if necessary.
538  *
539  * @mob:            Pointer to a mob to destroy.
540  */
541 void vmw_mob_destroy(struct vmw_mob *mob)
542 {
543         if (mob->pt_bo) {
544                 vmw_bo_unpin_unlocked(mob->pt_bo);
545                 ttm_bo_put(mob->pt_bo);
546                 mob->pt_bo = NULL;
547         }
548         kfree(mob);
549 }
550
551 /*
552  * vmw_mob_unbind - Hide a mob from the device.
553  *
554  * @dev_priv:       Pointer to a device private.
555  * @mob_id:         Device id of the mob to unbind.
556  */
557 void vmw_mob_unbind(struct vmw_private *dev_priv,
558                     struct vmw_mob *mob)
559 {
560         struct {
561                 SVGA3dCmdHeader header;
562                 SVGA3dCmdDestroyGBMob body;
563         } *cmd;
564         int ret;
565         struct ttm_buffer_object *bo = mob->pt_bo;
566
567         if (bo) {
568                 ret = ttm_bo_reserve(bo, false, true, NULL);
569                 /*
570                  * Noone else should be using this buffer.
571                  */
572                 BUG_ON(ret != 0);
573         }
574
575         cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
576         if (cmd) {
577                 cmd->header.id = SVGA_3D_CMD_DESTROY_GB_MOB;
578                 cmd->header.size = sizeof(cmd->body);
579                 cmd->body.mobid = mob->id;
580                 vmw_cmd_commit(dev_priv, sizeof(*cmd));
581         }
582
583         if (bo) {
584                 vmw_bo_fence_single(bo, NULL);
585                 ttm_bo_unreserve(bo);
586         }
587         vmw_fifo_resource_dec(dev_priv);
588 }
589
590 /*
591  * vmw_mob_bind - Make a mob visible to the device after first
592  *                populating it if necessary.
593  *
594  * @dev_priv:       Pointer to a device private.
595  * @mob:            Pointer to the mob we're making visible.
596  * @data_addr:      Array of DMA addresses to the data pages of the underlying
597  *                  buffer object.
598  * @num_data_pages: Number of data pages of the underlying buffer
599  *                  object.
600  * @mob_id:         Device id of the mob to bind
601  *
602  * This function is intended to be interfaced with the ttm_tt backend
603  * code.
604  */
605 int vmw_mob_bind(struct vmw_private *dev_priv,
606                  struct vmw_mob *mob,
607                  const struct vmw_sg_table *vsgt,
608                  unsigned long num_data_pages,
609                  int32_t mob_id)
610 {
611         int ret;
612         bool pt_set_up = false;
613         struct vmw_piter data_iter;
614         struct {
615                 SVGA3dCmdHeader header;
616                 SVGA3dCmdDefineGBMob64 body;
617         } *cmd;
618
619         mob->id = mob_id;
620         vmw_piter_start(&data_iter, vsgt, 0);
621         if (unlikely(!vmw_piter_next(&data_iter)))
622                 return 0;
623
624         if (likely(num_data_pages == 1)) {
625                 mob->pt_level = VMW_MOBFMT_PTDEPTH_0;
626                 mob->pt_root_page = vmw_piter_dma_addr(&data_iter);
627         } else if (vsgt->num_regions == 1) {
628                 mob->pt_level = SVGA3D_MOBFMT_RANGE;
629                 mob->pt_root_page = vmw_piter_dma_addr(&data_iter);
630         } else if (unlikely(mob->pt_bo == NULL)) {
631                 ret = vmw_mob_pt_populate(dev_priv, mob);
632                 if (unlikely(ret != 0))
633                         return ret;
634
635                 vmw_mob_pt_setup(mob, data_iter, num_data_pages);
636                 pt_set_up = true;
637                 mob->pt_level += VMW_MOBFMT_PTDEPTH_1 - SVGA3D_MOBFMT_PTDEPTH_1;
638         }
639
640         vmw_fifo_resource_inc(dev_priv);
641
642         cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
643         if (unlikely(cmd == NULL))
644                 goto out_no_cmd_space;
645
646         cmd->header.id = SVGA_3D_CMD_DEFINE_GB_MOB64;
647         cmd->header.size = sizeof(cmd->body);
648         cmd->body.mobid = mob_id;
649         cmd->body.ptDepth = mob->pt_level;
650         cmd->body.base = mob->pt_root_page >> PAGE_SHIFT;
651         cmd->body.sizeInBytes = num_data_pages * PAGE_SIZE;
652
653         vmw_cmd_commit(dev_priv, sizeof(*cmd));
654
655         return 0;
656
657 out_no_cmd_space:
658         vmw_fifo_resource_dec(dev_priv);
659         if (pt_set_up) {
660                 vmw_bo_unpin_unlocked(mob->pt_bo);
661                 ttm_bo_put(mob->pt_bo);
662                 mob->pt_bo = NULL;
663         }
664
665         return -ENOMEM;
666 }