1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
4 * Copyright 2012-2023 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_bo.h"
29 #include "vmwgfx_drv.h"
31 #include <linux/highmem.h>
34 #define VMW_PPN_SIZE 8
35 #define VMW_MOBFMT_PTDEPTH_0 SVGA3D_MOBFMT_PT64_0
36 #define VMW_MOBFMT_PTDEPTH_1 SVGA3D_MOBFMT_PT64_1
37 #define VMW_MOBFMT_PTDEPTH_2 SVGA3D_MOBFMT_PT64_2
39 #define VMW_PPN_SIZE 4
40 #define VMW_MOBFMT_PTDEPTH_0 SVGA3D_MOBFMT_PT_0
41 #define VMW_MOBFMT_PTDEPTH_1 SVGA3D_MOBFMT_PT_1
42 #define VMW_MOBFMT_PTDEPTH_2 SVGA3D_MOBFMT_PT_2
46 * struct vmw_mob - Structure containing page table and metadata for a
47 * Guest Memory OBject.
49 * @num_pages Number of pages that make up the page table.
50 * @pt_level The indirection level of the page table. 0-2.
51 * @pt_root_page DMA address of the level 0 page of the page table.
54 struct ttm_buffer_object *pt_bo;
55 unsigned long num_pages;
57 dma_addr_t pt_root_page;
62 * struct vmw_otable - Guest Memory OBject table metadata
64 * @size: Size of the table (page-aligned).
65 * @page_table: Pointer to a struct vmw_mob holding the page table.
67 static const struct vmw_otable pre_dx_tables[] = {
68 {VMWGFX_NUM_MOB * sizeof(SVGAOTableMobEntry), NULL, true},
69 {VMWGFX_NUM_GB_SURFACE * sizeof(SVGAOTableSurfaceEntry), NULL, true},
70 {VMWGFX_NUM_GB_CONTEXT * sizeof(SVGAOTableContextEntry), NULL, true},
71 {VMWGFX_NUM_GB_SHADER * sizeof(SVGAOTableShaderEntry), NULL, true},
72 {VMWGFX_NUM_GB_SCREEN_TARGET * sizeof(SVGAOTableScreenTargetEntry),
76 static const struct vmw_otable dx_tables[] = {
77 {VMWGFX_NUM_MOB * sizeof(SVGAOTableMobEntry), NULL, true},
78 {VMWGFX_NUM_GB_SURFACE * sizeof(SVGAOTableSurfaceEntry), NULL, true},
79 {VMWGFX_NUM_GB_CONTEXT * sizeof(SVGAOTableContextEntry), NULL, true},
80 {VMWGFX_NUM_GB_SHADER * sizeof(SVGAOTableShaderEntry), NULL, true},
81 {VMWGFX_NUM_GB_SCREEN_TARGET * sizeof(SVGAOTableScreenTargetEntry),
83 {VMWGFX_NUM_DXCONTEXT * sizeof(SVGAOTableDXContextEntry), NULL, true},
86 static int vmw_mob_pt_populate(struct vmw_private *dev_priv,
88 static void vmw_mob_pt_setup(struct vmw_mob *mob,
89 struct vmw_piter data_iter,
90 unsigned long num_data_pages);
93 static inline void vmw_bo_unpin_unlocked(struct ttm_buffer_object *bo)
95 int ret = ttm_bo_reserve(bo, false, true, NULL);
103 * vmw_setup_otable_base - Issue an object table base setup command to
106 * @dev_priv: Pointer to a device private structure
107 * @type: Type of object table base
108 * @offset Start of table offset into dev_priv::otable_bo
109 * @otable Pointer to otable metadata;
111 * This function returns -ENOMEM if it fails to reserve fifo space,
112 * and may block waiting for fifo space.
114 static int vmw_setup_otable_base(struct vmw_private *dev_priv,
116 struct ttm_buffer_object *otable_bo,
117 unsigned long offset,
118 struct vmw_otable *otable)
121 SVGA3dCmdHeader header;
122 SVGA3dCmdSetOTableBase64 body;
125 const struct vmw_sg_table *vsgt;
126 struct vmw_piter iter;
129 BUG_ON(otable->page_table != NULL);
131 vsgt = vmw_bo_sg_table(otable_bo);
132 vmw_piter_start(&iter, vsgt, offset >> PAGE_SHIFT);
133 WARN_ON(!vmw_piter_next(&iter));
135 mob = vmw_mob_create(otable->size >> PAGE_SHIFT);
136 if (unlikely(mob == NULL)) {
137 DRM_ERROR("Failed creating OTable page table.\n");
141 if (otable->size <= PAGE_SIZE) {
142 mob->pt_level = VMW_MOBFMT_PTDEPTH_0;
143 mob->pt_root_page = vmw_piter_dma_addr(&iter);
145 ret = vmw_mob_pt_populate(dev_priv, mob);
146 if (unlikely(ret != 0))
147 goto out_no_populate;
149 vmw_mob_pt_setup(mob, iter, otable->size >> PAGE_SHIFT);
150 mob->pt_level += VMW_MOBFMT_PTDEPTH_1 - SVGA3D_MOBFMT_PT_1;
153 cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
154 if (unlikely(cmd == NULL)) {
159 memset(cmd, 0, sizeof(*cmd));
160 cmd->header.id = SVGA_3D_CMD_SET_OTABLE_BASE64;
161 cmd->header.size = sizeof(cmd->body);
162 cmd->body.type = type;
163 cmd->body.baseAddress = mob->pt_root_page >> PAGE_SHIFT;
164 cmd->body.sizeInBytes = otable->size;
165 cmd->body.validSizeInBytes = 0;
166 cmd->body.ptDepth = mob->pt_level;
169 * The device doesn't support this, But the otable size is
170 * determined at compile-time, so this BUG shouldn't trigger
173 BUG_ON(mob->pt_level == VMW_MOBFMT_PTDEPTH_2);
175 vmw_cmd_commit(dev_priv, sizeof(*cmd));
176 otable->page_table = mob;
182 vmw_mob_destroy(mob);
187 * vmw_takedown_otable_base - Issue an object table base takedown command
190 * @dev_priv: Pointer to a device private structure
191 * @type: Type of object table base
194 static void vmw_takedown_otable_base(struct vmw_private *dev_priv,
196 struct vmw_otable *otable)
199 SVGA3dCmdHeader header;
200 SVGA3dCmdSetOTableBase body;
202 struct ttm_buffer_object *bo;
204 if (otable->page_table == NULL)
207 bo = otable->page_table->pt_bo;
208 cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
209 if (unlikely(cmd == NULL))
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_cmd_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;
247 for (i = 0; i < batch->num_otables; ++i) {
248 if (!otables[i].enabled)
251 otables[i].size = PFN_ALIGN(otables[i].size);
252 bo_size += otables[i].size;
255 ret = vmw_bo_create_and_populate(dev_priv, bo_size, &batch->otable_bo);
256 if (unlikely(ret != 0))
260 for (i = 0; i < batch->num_otables; ++i) {
261 if (!batch->otables[i].enabled)
264 ret = vmw_setup_otable_base(dev_priv, i, batch->otable_bo,
267 if (unlikely(ret != 0))
269 offset += otables[i].size;
275 for (i = 0; i < batch->num_otables; ++i) {
276 if (batch->otables[i].enabled)
277 vmw_takedown_otable_base(dev_priv, i,
281 vmw_bo_unpin_unlocked(batch->otable_bo);
282 ttm_bo_put(batch->otable_bo);
283 batch->otable_bo = NULL;
288 * vmw_otables_setup - Set up guest backed memory object tables
290 * @dev_priv: Pointer to a device private structure
292 * Takes care of the device guest backed surface
293 * initialization, by setting up the guest backed memory object tables.
294 * Returns 0 on success and various error codes on failure. A successful return
295 * means the object tables can be taken down using the vmw_otables_takedown
298 int vmw_otables_setup(struct vmw_private *dev_priv)
300 struct vmw_otable **otables = &dev_priv->otable_batch.otables;
303 if (has_sm4_context(dev_priv)) {
304 *otables = kmemdup(dx_tables, sizeof(dx_tables), GFP_KERNEL);
308 dev_priv->otable_batch.num_otables = ARRAY_SIZE(dx_tables);
310 *otables = kmemdup(pre_dx_tables, sizeof(pre_dx_tables),
315 dev_priv->otable_batch.num_otables = ARRAY_SIZE(pre_dx_tables);
318 ret = vmw_otable_batch_setup(dev_priv, &dev_priv->otable_batch);
319 if (unlikely(ret != 0))
329 static void vmw_otable_batch_takedown(struct vmw_private *dev_priv,
330 struct vmw_otable_batch *batch)
333 struct ttm_buffer_object *bo = batch->otable_bo;
336 for (i = 0; i < batch->num_otables; ++i)
337 if (batch->otables[i].enabled)
338 vmw_takedown_otable_base(dev_priv, i,
341 ret = ttm_bo_reserve(bo, false, true, NULL);
344 vmw_bo_fence_single(bo, NULL);
346 ttm_bo_unreserve(bo);
348 ttm_bo_put(batch->otable_bo);
349 batch->otable_bo = NULL;
353 * vmw_otables_takedown - Take down guest backed memory object tables
355 * @dev_priv: Pointer to a device private structure
357 * Take down the Guest Memory Object tables.
359 void vmw_otables_takedown(struct vmw_private *dev_priv)
361 vmw_otable_batch_takedown(dev_priv, &dev_priv->otable_batch);
362 kfree(dev_priv->otable_batch.otables);
366 * vmw_mob_calculate_pt_pages - Calculate the number of page table pages
367 * needed for a guest backed memory object.
369 * @data_pages: Number of data pages in the memory object buffer.
371 static unsigned long vmw_mob_calculate_pt_pages(unsigned long data_pages)
373 unsigned long data_size = data_pages * PAGE_SIZE;
374 unsigned long tot_size = 0;
376 while (likely(data_size > PAGE_SIZE)) {
377 data_size = DIV_ROUND_UP(data_size, PAGE_SIZE);
378 data_size *= VMW_PPN_SIZE;
379 tot_size += PFN_ALIGN(data_size);
382 return tot_size >> PAGE_SHIFT;
386 * vmw_mob_create - Create a mob, but don't populate it.
388 * @data_pages: Number of data pages of the underlying buffer object.
390 struct vmw_mob *vmw_mob_create(unsigned long data_pages)
392 struct vmw_mob *mob = kzalloc(sizeof(*mob), GFP_KERNEL);
397 mob->num_pages = vmw_mob_calculate_pt_pages(data_pages);
403 * vmw_mob_pt_populate - Populate the mob pagetable
405 * @mob: Pointer to the mob the pagetable of which we want to
408 * This function allocates memory to be used for the pagetable.
409 * Returns ENOMEM if memory resources aren't sufficient and may
410 * cause TTM buffer objects to be swapped out.
412 static int vmw_mob_pt_populate(struct vmw_private *dev_priv,
415 BUG_ON(mob->pt_bo != NULL);
417 return vmw_bo_create_and_populate(dev_priv, mob->num_pages * PAGE_SIZE, &mob->pt_bo);
421 * vmw_mob_assign_ppn - Assign a value to a page table entry
423 * @addr: Pointer to pointer to page table entry.
424 * @val: The page table entry
426 * Assigns a value to a page table entry pointed to by *@addr and increments
427 * *@addr according to the page table entry size.
429 #if (VMW_PPN_SIZE == 8)
430 static void vmw_mob_assign_ppn(u32 **addr, dma_addr_t val)
432 *((u64 *) *addr) = val >> PAGE_SHIFT;
436 static void vmw_mob_assign_ppn(u32 **addr, dma_addr_t val)
438 *(*addr)++ = val >> PAGE_SHIFT;
443 * vmw_mob_build_pt - Build a pagetable
445 * @data_addr: Array of DMA addresses to the underlying buffer
446 * object's data pages.
447 * @num_data_pages: Number of buffer object data pages.
448 * @pt_pages: Array of page pointers to the page table pages.
450 * Returns the number of page table pages actually used.
451 * Uses atomic kmaps of highmem pages to avoid TLB thrashing.
453 static unsigned long vmw_mob_build_pt(struct vmw_piter *data_iter,
454 unsigned long num_data_pages,
455 struct vmw_piter *pt_iter)
457 unsigned long pt_size = num_data_pages * VMW_PPN_SIZE;
458 unsigned long num_pt_pages = DIV_ROUND_UP(pt_size, PAGE_SIZE);
459 unsigned long pt_page;
460 u32 *addr, *save_addr;
464 for (pt_page = 0; pt_page < num_pt_pages; ++pt_page) {
465 page = vmw_piter_page(pt_iter);
467 save_addr = addr = kmap_atomic(page);
469 for (i = 0; i < PAGE_SIZE / VMW_PPN_SIZE; ++i) {
470 vmw_mob_assign_ppn(&addr,
471 vmw_piter_dma_addr(data_iter));
472 if (unlikely(--num_data_pages == 0))
474 WARN_ON(!vmw_piter_next(data_iter));
476 kunmap_atomic(save_addr);
477 vmw_piter_next(pt_iter);
484 * vmw_mob_build_pt - Set up a multilevel mob pagetable
486 * @mob: Pointer to a mob whose page table needs setting up.
487 * @data_addr Array of DMA addresses to the buffer object's data
489 * @num_data_pages: Number of buffer object data pages.
491 * Uses tail recursion to set up a multilevel mob page table.
493 static void vmw_mob_pt_setup(struct vmw_mob *mob,
494 struct vmw_piter data_iter,
495 unsigned long num_data_pages)
497 unsigned long num_pt_pages = 0;
498 struct ttm_buffer_object *bo = mob->pt_bo;
499 struct vmw_piter save_pt_iter = {0};
500 struct vmw_piter pt_iter;
501 const struct vmw_sg_table *vsgt;
504 BUG_ON(num_data_pages == 0);
506 ret = ttm_bo_reserve(bo, false, true, NULL);
509 vsgt = vmw_bo_sg_table(bo);
510 vmw_piter_start(&pt_iter, vsgt, 0);
511 BUG_ON(!vmw_piter_next(&pt_iter));
513 while (likely(num_data_pages > 1)) {
515 BUG_ON(mob->pt_level > 2);
516 save_pt_iter = pt_iter;
517 num_pt_pages = vmw_mob_build_pt(&data_iter, num_data_pages,
519 data_iter = save_pt_iter;
520 num_data_pages = num_pt_pages;
523 mob->pt_root_page = vmw_piter_dma_addr(&save_pt_iter);
524 ttm_bo_unreserve(bo);
528 * vmw_mob_destroy - Destroy a mob, unpopulating first if necessary.
530 * @mob: Pointer to a mob to destroy.
532 void vmw_mob_destroy(struct vmw_mob *mob)
535 vmw_bo_unpin_unlocked(mob->pt_bo);
536 ttm_bo_put(mob->pt_bo);
543 * vmw_mob_unbind - Hide a mob from the device.
545 * @dev_priv: Pointer to a device private.
546 * @mob_id: Device id of the mob to unbind.
548 void vmw_mob_unbind(struct vmw_private *dev_priv,
552 SVGA3dCmdHeader header;
553 SVGA3dCmdDestroyGBMob body;
556 struct ttm_buffer_object *bo = mob->pt_bo;
559 ret = ttm_bo_reserve(bo, false, true, NULL);
561 * Noone else should be using this buffer.
566 cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
568 cmd->header.id = SVGA_3D_CMD_DESTROY_GB_MOB;
569 cmd->header.size = sizeof(cmd->body);
570 cmd->body.mobid = mob->id;
571 vmw_cmd_commit(dev_priv, sizeof(*cmd));
575 vmw_bo_fence_single(bo, NULL);
576 ttm_bo_unreserve(bo);
578 vmw_fifo_resource_dec(dev_priv);
582 * vmw_mob_bind - Make a mob visible to the device after first
583 * populating it if necessary.
585 * @dev_priv: Pointer to a device private.
586 * @mob: Pointer to the mob we're making visible.
587 * @data_addr: Array of DMA addresses to the data pages of the underlying
589 * @num_data_pages: Number of data pages of the underlying buffer
591 * @mob_id: Device id of the mob to bind
593 * This function is intended to be interfaced with the ttm_tt backend
596 int vmw_mob_bind(struct vmw_private *dev_priv,
598 const struct vmw_sg_table *vsgt,
599 unsigned long num_data_pages,
603 bool pt_set_up = false;
604 struct vmw_piter data_iter;
606 SVGA3dCmdHeader header;
607 SVGA3dCmdDefineGBMob64 body;
611 vmw_piter_start(&data_iter, vsgt, 0);
612 if (unlikely(!vmw_piter_next(&data_iter)))
615 if (likely(num_data_pages == 1)) {
616 mob->pt_level = VMW_MOBFMT_PTDEPTH_0;
617 mob->pt_root_page = vmw_piter_dma_addr(&data_iter);
618 } else if (unlikely(mob->pt_bo == NULL)) {
619 ret = vmw_mob_pt_populate(dev_priv, mob);
620 if (unlikely(ret != 0))
623 vmw_mob_pt_setup(mob, data_iter, num_data_pages);
625 mob->pt_level += VMW_MOBFMT_PTDEPTH_1 - SVGA3D_MOBFMT_PT_1;
628 vmw_fifo_resource_inc(dev_priv);
630 cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
631 if (unlikely(cmd == NULL))
632 goto out_no_cmd_space;
634 cmd->header.id = SVGA_3D_CMD_DEFINE_GB_MOB64;
635 cmd->header.size = sizeof(cmd->body);
636 cmd->body.mobid = mob_id;
637 cmd->body.ptDepth = mob->pt_level;
638 cmd->body.base = mob->pt_root_page >> PAGE_SHIFT;
639 cmd->body.sizeInBytes = num_data_pages * PAGE_SIZE;
641 vmw_cmd_commit(dev_priv, sizeof(*cmd));
646 vmw_fifo_resource_dec(dev_priv);
648 vmw_bo_unpin_unlocked(mob->pt_bo);
649 ttm_bo_put(mob->pt_bo);