1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
4 * Copyright 2012-2015 VMware, Inc., Palo Alto, CA., USA
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "vmwgfx_drv.h"
31 * If we set up the screen target otable, screen objects stop working.
34 #define VMW_OTABLE_SETUP_SUB ((VMWGFX_ENABLE_SCREEN_TARGET_OTABLE ? 0 : 1))
37 #define VMW_PPN_SIZE 8
38 #define VMW_MOBFMT_PTDEPTH_0 SVGA3D_MOBFMT_PTDEPTH64_0
39 #define VMW_MOBFMT_PTDEPTH_1 SVGA3D_MOBFMT_PTDEPTH64_1
40 #define VMW_MOBFMT_PTDEPTH_2 SVGA3D_MOBFMT_PTDEPTH64_2
42 #define VMW_PPN_SIZE 4
43 #define VMW_MOBFMT_PTDEPTH_0 SVGA3D_MOBFMT_PTDEPTH_0
44 #define VMW_MOBFMT_PTDEPTH_1 SVGA3D_MOBFMT_PTDEPTH_1
45 #define VMW_MOBFMT_PTDEPTH_2 SVGA3D_MOBFMT_PTDEPTH_2
49 * struct vmw_mob - Structure containing page table and metadata for a
50 * Guest Memory OBject.
52 * @num_pages Number of pages that make up the page table.
53 * @pt_level The indirection level of the page table. 0-2.
54 * @pt_root_page DMA address of the level 0 page of the page table.
57 struct ttm_buffer_object
*pt_bo
;
58 unsigned long num_pages
;
60 dma_addr_t pt_root_page
;
65 * struct vmw_otable - Guest Memory OBject table metadata
67 * @size: Size of the table (page-aligned).
68 * @page_table: Pointer to a struct vmw_mob holding the page table.
70 static const struct vmw_otable pre_dx_tables
[] = {
71 {VMWGFX_NUM_MOB
* SVGA3D_OTABLE_MOB_ENTRY_SIZE
, NULL
, true},
72 {VMWGFX_NUM_GB_SURFACE
* SVGA3D_OTABLE_SURFACE_ENTRY_SIZE
, NULL
, true},
73 {VMWGFX_NUM_GB_CONTEXT
* SVGA3D_OTABLE_CONTEXT_ENTRY_SIZE
, NULL
, true},
74 {VMWGFX_NUM_GB_SHADER
* SVGA3D_OTABLE_SHADER_ENTRY_SIZE
, NULL
, true},
75 {VMWGFX_NUM_GB_SCREEN_TARGET
* SVGA3D_OTABLE_SCREEN_TARGET_ENTRY_SIZE
,
76 NULL
, VMWGFX_ENABLE_SCREEN_TARGET_OTABLE
}
79 static const struct vmw_otable dx_tables
[] = {
80 {VMWGFX_NUM_MOB
* SVGA3D_OTABLE_MOB_ENTRY_SIZE
, NULL
, true},
81 {VMWGFX_NUM_GB_SURFACE
* SVGA3D_OTABLE_SURFACE_ENTRY_SIZE
, NULL
, true},
82 {VMWGFX_NUM_GB_CONTEXT
* SVGA3D_OTABLE_CONTEXT_ENTRY_SIZE
, NULL
, true},
83 {VMWGFX_NUM_GB_SHADER
* SVGA3D_OTABLE_SHADER_ENTRY_SIZE
, NULL
, true},
84 {VMWGFX_NUM_GB_SCREEN_TARGET
* SVGA3D_OTABLE_SCREEN_TARGET_ENTRY_SIZE
,
85 NULL
, VMWGFX_ENABLE_SCREEN_TARGET_OTABLE
},
86 {VMWGFX_NUM_DXCONTEXT
* sizeof(SVGAOTableDXContextEntry
), NULL
, true},
89 static int vmw_mob_pt_populate(struct vmw_private
*dev_priv
,
91 static void vmw_mob_pt_setup(struct vmw_mob
*mob
,
92 struct vmw_piter data_iter
,
93 unsigned long num_data_pages
);
96 * vmw_setup_otable_base - Issue an object table base setup command to
99 * @dev_priv: Pointer to a device private structure
100 * @type: Type of object table base
101 * @offset Start of table offset into dev_priv::otable_bo
102 * @otable Pointer to otable metadata;
104 * This function returns -ENOMEM if it fails to reserve fifo space,
105 * and may block waiting for fifo space.
107 static int vmw_setup_otable_base(struct vmw_private
*dev_priv
,
109 struct ttm_buffer_object
*otable_bo
,
110 unsigned long offset
,
111 struct vmw_otable
*otable
)
114 SVGA3dCmdHeader header
;
115 SVGA3dCmdSetOTableBase64 body
;
118 const struct vmw_sg_table
*vsgt
;
119 struct vmw_piter iter
;
122 BUG_ON(otable
->page_table
!= NULL
);
124 vsgt
= vmw_bo_sg_table(otable_bo
);
125 vmw_piter_start(&iter
, vsgt
, offset
>> PAGE_SHIFT
);
126 WARN_ON(!vmw_piter_next(&iter
));
128 mob
= vmw_mob_create(otable
->size
>> PAGE_SHIFT
);
129 if (unlikely(mob
== NULL
)) {
130 DRM_ERROR("Failed creating OTable page table.\n");
134 if (otable
->size
<= PAGE_SIZE
) {
135 mob
->pt_level
= VMW_MOBFMT_PTDEPTH_0
;
136 mob
->pt_root_page
= vmw_piter_dma_addr(&iter
);
137 } else if (vsgt
->num_regions
== 1) {
138 mob
->pt_level
= SVGA3D_MOBFMT_RANGE
;
139 mob
->pt_root_page
= vmw_piter_dma_addr(&iter
);
141 ret
= vmw_mob_pt_populate(dev_priv
, mob
);
142 if (unlikely(ret
!= 0))
143 goto out_no_populate
;
145 vmw_mob_pt_setup(mob
, iter
, otable
->size
>> PAGE_SHIFT
);
146 mob
->pt_level
+= VMW_MOBFMT_PTDEPTH_1
- SVGA3D_MOBFMT_PTDEPTH_1
;
149 cmd
= vmw_fifo_reserve(dev_priv
, sizeof(*cmd
));
150 if (unlikely(cmd
== NULL
)) {
151 DRM_ERROR("Failed reserving FIFO space for OTable setup.\n");
156 memset(cmd
, 0, sizeof(*cmd
));
157 cmd
->header
.id
= SVGA_3D_CMD_SET_OTABLE_BASE64
;
158 cmd
->header
.size
= sizeof(cmd
->body
);
159 cmd
->body
.type
= type
;
160 cmd
->body
.baseAddress
= mob
->pt_root_page
>> PAGE_SHIFT
;
161 cmd
->body
.sizeInBytes
= otable
->size
;
162 cmd
->body
.validSizeInBytes
= 0;
163 cmd
->body
.ptDepth
= mob
->pt_level
;
166 * The device doesn't support this, But the otable size is
167 * determined at compile-time, so this BUG shouldn't trigger
170 BUG_ON(mob
->pt_level
== VMW_MOBFMT_PTDEPTH_2
);
172 vmw_fifo_commit(dev_priv
, sizeof(*cmd
));
173 otable
->page_table
= mob
;
179 vmw_mob_destroy(mob
);
184 * vmw_takedown_otable_base - Issue an object table base takedown command
187 * @dev_priv: Pointer to a device private structure
188 * @type: Type of object table base
191 static void vmw_takedown_otable_base(struct vmw_private
*dev_priv
,
193 struct vmw_otable
*otable
)
196 SVGA3dCmdHeader header
;
197 SVGA3dCmdSetOTableBase body
;
199 struct ttm_buffer_object
*bo
;
201 if (otable
->page_table
== NULL
)
204 bo
= otable
->page_table
->pt_bo
;
205 cmd
= vmw_fifo_reserve(dev_priv
, sizeof(*cmd
));
206 if (unlikely(cmd
== NULL
)) {
207 DRM_ERROR("Failed reserving FIFO space for OTable "
212 memset(cmd
, 0, sizeof(*cmd
));
213 cmd
->header
.id
= SVGA_3D_CMD_SET_OTABLE_BASE
;
214 cmd
->header
.size
= sizeof(cmd
->body
);
215 cmd
->body
.type
= type
;
216 cmd
->body
.baseAddress
= 0;
217 cmd
->body
.sizeInBytes
= 0;
218 cmd
->body
.validSizeInBytes
= 0;
219 cmd
->body
.ptDepth
= SVGA3D_MOBFMT_INVALID
;
220 vmw_fifo_commit(dev_priv
, sizeof(*cmd
));
225 ret
= ttm_bo_reserve(bo
, false, true, NULL
);
228 vmw_bo_fence_single(bo
, NULL
);
229 ttm_bo_unreserve(bo
);
232 vmw_mob_destroy(otable
->page_table
);
233 otable
->page_table
= NULL
;
237 static int vmw_otable_batch_setup(struct vmw_private
*dev_priv
,
238 struct vmw_otable_batch
*batch
)
240 unsigned long offset
;
241 unsigned long bo_size
;
242 struct vmw_otable
*otables
= batch
->otables
;
243 struct ttm_operation_ctx ctx
= {
244 .interruptible
= false,
251 for (i
= 0; i
< batch
->num_otables
; ++i
) {
252 if (!otables
[i
].enabled
)
256 (otables
[i
].size
+ PAGE_SIZE
- 1) & PAGE_MASK
;
257 bo_size
+= otables
[i
].size
;
260 ret
= ttm_bo_create(&dev_priv
->bdev
, bo_size
,
262 &vmw_sys_ne_placement
,
263 0, false, &batch
->otable_bo
);
265 if (unlikely(ret
!= 0))
268 ret
= ttm_bo_reserve(batch
->otable_bo
, false, true, NULL
);
270 ret
= vmw_bo_driver
.ttm_tt_populate(batch
->otable_bo
->ttm
, &ctx
);
271 if (unlikely(ret
!= 0))
273 ret
= vmw_bo_map_dma(batch
->otable_bo
);
274 if (unlikely(ret
!= 0))
277 ttm_bo_unreserve(batch
->otable_bo
);
280 for (i
= 0; i
< batch
->num_otables
; ++i
) {
281 if (!batch
->otables
[i
].enabled
)
284 ret
= vmw_setup_otable_base(dev_priv
, i
, batch
->otable_bo
,
287 if (unlikely(ret
!= 0))
289 offset
+= otables
[i
].size
;
295 ttm_bo_unreserve(batch
->otable_bo
);
297 for (i
= 0; i
< batch
->num_otables
; ++i
) {
298 if (batch
->otables
[i
].enabled
)
299 vmw_takedown_otable_base(dev_priv
, i
,
303 ttm_bo_unref(&batch
->otable_bo
);
309 * vmw_otables_setup - Set up guest backed memory object tables
311 * @dev_priv: Pointer to a device private structure
313 * Takes care of the device guest backed surface
314 * initialization, by setting up the guest backed memory object tables.
315 * Returns 0 on success and various error codes on failure. A successful return
316 * means the object tables can be taken down using the vmw_otables_takedown
319 int vmw_otables_setup(struct vmw_private
*dev_priv
)
321 struct vmw_otable
**otables
= &dev_priv
->otable_batch
.otables
;
324 if (dev_priv
->has_dx
) {
325 *otables
= kmemdup(dx_tables
, sizeof(dx_tables
), GFP_KERNEL
);
329 dev_priv
->otable_batch
.num_otables
= ARRAY_SIZE(dx_tables
);
331 *otables
= kmemdup(pre_dx_tables
, sizeof(pre_dx_tables
),
336 dev_priv
->otable_batch
.num_otables
= ARRAY_SIZE(pre_dx_tables
);
339 ret
= vmw_otable_batch_setup(dev_priv
, &dev_priv
->otable_batch
);
340 if (unlikely(ret
!= 0))
350 static void vmw_otable_batch_takedown(struct vmw_private
*dev_priv
,
351 struct vmw_otable_batch
*batch
)
354 struct ttm_buffer_object
*bo
= batch
->otable_bo
;
357 for (i
= 0; i
< batch
->num_otables
; ++i
)
358 if (batch
->otables
[i
].enabled
)
359 vmw_takedown_otable_base(dev_priv
, i
,
362 ret
= ttm_bo_reserve(bo
, false, true, NULL
);
365 vmw_bo_fence_single(bo
, NULL
);
366 ttm_bo_unreserve(bo
);
368 ttm_bo_unref(&batch
->otable_bo
);
372 * vmw_otables_takedown - Take down guest backed memory object tables
374 * @dev_priv: Pointer to a device private structure
376 * Take down the Guest Memory Object tables.
378 void vmw_otables_takedown(struct vmw_private
*dev_priv
)
380 vmw_otable_batch_takedown(dev_priv
, &dev_priv
->otable_batch
);
381 kfree(dev_priv
->otable_batch
.otables
);
385 * vmw_mob_calculate_pt_pages - Calculate the number of page table pages
386 * needed for a guest backed memory object.
388 * @data_pages: Number of data pages in the memory object buffer.
390 static unsigned long vmw_mob_calculate_pt_pages(unsigned long data_pages
)
392 unsigned long data_size
= data_pages
* PAGE_SIZE
;
393 unsigned long tot_size
= 0;
395 while (likely(data_size
> PAGE_SIZE
)) {
396 data_size
= DIV_ROUND_UP(data_size
, PAGE_SIZE
);
397 data_size
*= VMW_PPN_SIZE
;
398 tot_size
+= (data_size
+ PAGE_SIZE
- 1) & PAGE_MASK
;
401 return tot_size
>> PAGE_SHIFT
;
405 * vmw_mob_create - Create a mob, but don't populate it.
407 * @data_pages: Number of data pages of the underlying buffer object.
409 struct vmw_mob
*vmw_mob_create(unsigned long data_pages
)
411 struct vmw_mob
*mob
= kzalloc(sizeof(*mob
), GFP_KERNEL
);
416 mob
->num_pages
= vmw_mob_calculate_pt_pages(data_pages
);
422 * vmw_mob_pt_populate - Populate the mob pagetable
424 * @mob: Pointer to the mob the pagetable of which we want to
427 * This function allocates memory to be used for the pagetable, and
428 * adjusts TTM memory accounting accordingly. Returns ENOMEM if
429 * memory resources aren't sufficient and may cause TTM buffer objects
430 * to be swapped out by using the TTM memory accounting function.
432 static int vmw_mob_pt_populate(struct vmw_private
*dev_priv
,
436 struct ttm_operation_ctx ctx
= {
437 .interruptible
= false,
441 BUG_ON(mob
->pt_bo
!= NULL
);
443 ret
= ttm_bo_create(&dev_priv
->bdev
, mob
->num_pages
* PAGE_SIZE
,
445 &vmw_sys_ne_placement
,
446 0, false, &mob
->pt_bo
);
447 if (unlikely(ret
!= 0))
450 ret
= ttm_bo_reserve(mob
->pt_bo
, false, true, NULL
);
453 ret
= vmw_bo_driver
.ttm_tt_populate(mob
->pt_bo
->ttm
, &ctx
);
454 if (unlikely(ret
!= 0))
456 ret
= vmw_bo_map_dma(mob
->pt_bo
);
457 if (unlikely(ret
!= 0))
460 ttm_bo_unreserve(mob
->pt_bo
);
465 ttm_bo_unreserve(mob
->pt_bo
);
466 ttm_bo_unref(&mob
->pt_bo
);
472 * vmw_mob_assign_ppn - Assign a value to a page table entry
474 * @addr: Pointer to pointer to page table entry.
475 * @val: The page table entry
477 * Assigns a value to a page table entry pointed to by *@addr and increments
478 * *@addr according to the page table entry size.
480 #if (VMW_PPN_SIZE == 8)
481 static void vmw_mob_assign_ppn(u32
**addr
, dma_addr_t val
)
483 *((u64
*) *addr
) = val
>> PAGE_SHIFT
;
487 static void vmw_mob_assign_ppn(u32
**addr
, dma_addr_t val
)
489 *(*addr
)++ = val
>> PAGE_SHIFT
;
494 * vmw_mob_build_pt - Build a pagetable
496 * @data_addr: Array of DMA addresses to the underlying buffer
497 * object's data pages.
498 * @num_data_pages: Number of buffer object data pages.
499 * @pt_pages: Array of page pointers to the page table pages.
501 * Returns the number of page table pages actually used.
502 * Uses atomic kmaps of highmem pages to avoid TLB thrashing.
504 static unsigned long vmw_mob_build_pt(struct vmw_piter
*data_iter
,
505 unsigned long num_data_pages
,
506 struct vmw_piter
*pt_iter
)
508 unsigned long pt_size
= num_data_pages
* VMW_PPN_SIZE
;
509 unsigned long num_pt_pages
= DIV_ROUND_UP(pt_size
, PAGE_SIZE
);
510 unsigned long pt_page
;
511 u32
*addr
, *save_addr
;
515 for (pt_page
= 0; pt_page
< num_pt_pages
; ++pt_page
) {
516 page
= vmw_piter_page(pt_iter
);
518 save_addr
= addr
= kmap_atomic(page
);
520 for (i
= 0; i
< PAGE_SIZE
/ VMW_PPN_SIZE
; ++i
) {
521 vmw_mob_assign_ppn(&addr
,
522 vmw_piter_dma_addr(data_iter
));
523 if (unlikely(--num_data_pages
== 0))
525 WARN_ON(!vmw_piter_next(data_iter
));
527 kunmap_atomic(save_addr
);
528 vmw_piter_next(pt_iter
);
535 * vmw_mob_build_pt - Set up a multilevel mob pagetable
537 * @mob: Pointer to a mob whose page table needs setting up.
538 * @data_addr Array of DMA addresses to the buffer object's data
540 * @num_data_pages: Number of buffer object data pages.
542 * Uses tail recursion to set up a multilevel mob page table.
544 static void vmw_mob_pt_setup(struct vmw_mob
*mob
,
545 struct vmw_piter data_iter
,
546 unsigned long num_data_pages
)
548 unsigned long num_pt_pages
= 0;
549 struct ttm_buffer_object
*bo
= mob
->pt_bo
;
550 struct vmw_piter save_pt_iter
;
551 struct vmw_piter pt_iter
;
552 const struct vmw_sg_table
*vsgt
;
555 ret
= ttm_bo_reserve(bo
, false, true, NULL
);
558 vsgt
= vmw_bo_sg_table(bo
);
559 vmw_piter_start(&pt_iter
, vsgt
, 0);
560 BUG_ON(!vmw_piter_next(&pt_iter
));
562 while (likely(num_data_pages
> 1)) {
564 BUG_ON(mob
->pt_level
> 2);
565 save_pt_iter
= pt_iter
;
566 num_pt_pages
= vmw_mob_build_pt(&data_iter
, num_data_pages
,
568 data_iter
= save_pt_iter
;
569 num_data_pages
= num_pt_pages
;
572 mob
->pt_root_page
= vmw_piter_dma_addr(&save_pt_iter
);
573 ttm_bo_unreserve(bo
);
577 * vmw_mob_destroy - Destroy a mob, unpopulating first if necessary.
579 * @mob: Pointer to a mob to destroy.
581 void vmw_mob_destroy(struct vmw_mob
*mob
)
584 ttm_bo_unref(&mob
->pt_bo
);
589 * vmw_mob_unbind - Hide a mob from the device.
591 * @dev_priv: Pointer to a device private.
592 * @mob_id: Device id of the mob to unbind.
594 void vmw_mob_unbind(struct vmw_private
*dev_priv
,
598 SVGA3dCmdHeader header
;
599 SVGA3dCmdDestroyGBMob body
;
602 struct ttm_buffer_object
*bo
= mob
->pt_bo
;
605 ret
= ttm_bo_reserve(bo
, false, true, NULL
);
607 * Noone else should be using this buffer.
612 cmd
= vmw_fifo_reserve(dev_priv
, sizeof(*cmd
));
613 if (unlikely(cmd
== NULL
)) {
614 DRM_ERROR("Failed reserving FIFO space for Memory "
615 "Object unbinding.\n");
617 cmd
->header
.id
= SVGA_3D_CMD_DESTROY_GB_MOB
;
618 cmd
->header
.size
= sizeof(cmd
->body
);
619 cmd
->body
.mobid
= mob
->id
;
620 vmw_fifo_commit(dev_priv
, sizeof(*cmd
));
623 vmw_bo_fence_single(bo
, NULL
);
624 ttm_bo_unreserve(bo
);
626 vmw_fifo_resource_dec(dev_priv
);
630 * vmw_mob_bind - Make a mob visible to the device after first
631 * populating it if necessary.
633 * @dev_priv: Pointer to a device private.
634 * @mob: Pointer to the mob we're making visible.
635 * @data_addr: Array of DMA addresses to the data pages of the underlying
637 * @num_data_pages: Number of data pages of the underlying buffer
639 * @mob_id: Device id of the mob to bind
641 * This function is intended to be interfaced with the ttm_tt backend
644 int vmw_mob_bind(struct vmw_private
*dev_priv
,
646 const struct vmw_sg_table
*vsgt
,
647 unsigned long num_data_pages
,
651 bool pt_set_up
= false;
652 struct vmw_piter data_iter
;
654 SVGA3dCmdHeader header
;
655 SVGA3dCmdDefineGBMob64 body
;
659 vmw_piter_start(&data_iter
, vsgt
, 0);
660 if (unlikely(!vmw_piter_next(&data_iter
)))
663 if (likely(num_data_pages
== 1)) {
664 mob
->pt_level
= VMW_MOBFMT_PTDEPTH_0
;
665 mob
->pt_root_page
= vmw_piter_dma_addr(&data_iter
);
666 } else if (vsgt
->num_regions
== 1) {
667 mob
->pt_level
= SVGA3D_MOBFMT_RANGE
;
668 mob
->pt_root_page
= vmw_piter_dma_addr(&data_iter
);
669 } else if (unlikely(mob
->pt_bo
== NULL
)) {
670 ret
= vmw_mob_pt_populate(dev_priv
, mob
);
671 if (unlikely(ret
!= 0))
674 vmw_mob_pt_setup(mob
, data_iter
, num_data_pages
);
676 mob
->pt_level
+= VMW_MOBFMT_PTDEPTH_1
- SVGA3D_MOBFMT_PTDEPTH_1
;
679 vmw_fifo_resource_inc(dev_priv
);
681 cmd
= vmw_fifo_reserve(dev_priv
, sizeof(*cmd
));
682 if (unlikely(cmd
== NULL
)) {
683 DRM_ERROR("Failed reserving FIFO space for Memory "
684 "Object binding.\n");
685 goto out_no_cmd_space
;
688 cmd
->header
.id
= SVGA_3D_CMD_DEFINE_GB_MOB64
;
689 cmd
->header
.size
= sizeof(cmd
->body
);
690 cmd
->body
.mobid
= mob_id
;
691 cmd
->body
.ptDepth
= mob
->pt_level
;
692 cmd
->body
.base
= mob
->pt_root_page
>> PAGE_SHIFT
;
693 cmd
->body
.sizeInBytes
= num_data_pages
* PAGE_SIZE
;
695 vmw_fifo_commit(dev_priv
, sizeof(*cmd
));
700 vmw_fifo_resource_dec(dev_priv
);
702 ttm_bo_unref(&mob
->pt_bo
);