blob: 4910e7b8181111c63f0ceb586249b1f740409d95 [file] [log] [blame]
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +01001/**************************************************************************
2 *
3 * Copyright © 2012 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
30/*
Thomas Hellstrom7cba9062014-01-09 11:03:18 +010031 * If we set up the screen target otable, screen objects stop working.
32 */
33
34#define VMW_OTABLE_SETUP_SUB ((VMWGFX_ENABLE_SCREEN_TARGET_OTABLE) ? 0 : 1)
35
Thomas Hellstromf2a0dcb2014-01-15 10:04:07 +010036#ifdef CONFIG_64BIT
37#define VMW_PPN_SIZE 8
Thomas Hellstromf2a0dcb2014-01-15 10:04:07 +010038#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
41#else
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +010042#define VMW_PPN_SIZE 4
Thomas Hellstromf2a0dcb2014-01-15 10:04:07 +010043#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
46#endif
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +010047
48/*
49 * struct vmw_mob - Structure containing page table and metadata for a
50 * Guest Memory OBject.
51 *
52 * @num_pages Number of pages that make up the page table.
53 * @pt_level The indirection level of the page table. 0-2.
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -070054 * @pt_root_page DMA address of the level 0 page of the page table.
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +010055 */
56struct vmw_mob {
57 struct ttm_buffer_object *pt_bo;
58 unsigned long num_pages;
59 unsigned pt_level;
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -070060 dma_addr_t pt_root_page;
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +010061 uint32_t id;
62};
63
64/*
65 * struct vmw_otable - Guest Memory OBject table metadata
66 *
67 * @size: Size of the table (page-aligned).
68 * @page_table: Pointer to a struct vmw_mob holding the page table.
69 */
70struct vmw_otable {
71 unsigned long size;
72 struct vmw_mob *page_table;
73};
74
75static int vmw_mob_pt_populate(struct vmw_private *dev_priv,
76 struct vmw_mob *mob);
77static void vmw_mob_pt_setup(struct vmw_mob *mob,
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -070078 struct vmw_piter data_iter,
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +010079 unsigned long num_data_pages);
80
81/*
82 * vmw_setup_otable_base - Issue an object table base setup command to
83 * the device
84 *
85 * @dev_priv: Pointer to a device private structure
86 * @type: Type of object table base
87 * @offset Start of table offset into dev_priv::otable_bo
88 * @otable Pointer to otable metadata;
89 *
90 * This function returns -ENOMEM if it fails to reserve fifo space,
91 * and may block waiting for fifo space.
92 */
93static int vmw_setup_otable_base(struct vmw_private *dev_priv,
94 SVGAOTableType type,
95 unsigned long offset,
96 struct vmw_otable *otable)
97{
98 struct {
99 SVGA3dCmdHeader header;
Thomas Hellstrom3e894a62014-01-20 11:33:04 +0100100 SVGA3dCmdSetOTableBase64 body;
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100101 } *cmd;
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100102 struct vmw_mob *mob;
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -0700103 const struct vmw_sg_table *vsgt;
104 struct vmw_piter iter;
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100105 int ret;
106
107 BUG_ON(otable->page_table != NULL);
108
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -0700109 vsgt = vmw_bo_sg_table(dev_priv->otable_bo);
110 vmw_piter_start(&iter, vsgt, offset >> PAGE_SHIFT);
111 WARN_ON(!vmw_piter_next(&iter));
112
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100113 mob = vmw_mob_create(otable->size >> PAGE_SHIFT);
114 if (unlikely(mob == NULL)) {
115 DRM_ERROR("Failed creating OTable page table.\n");
116 return -ENOMEM;
117 }
118
119 if (otable->size <= PAGE_SIZE) {
Thomas Hellstromf2a0dcb2014-01-15 10:04:07 +0100120 mob->pt_level = VMW_MOBFMT_PTDEPTH_0;
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -0700121 mob->pt_root_page = vmw_piter_dma_addr(&iter);
122 } else if (vsgt->num_regions == 1) {
123 mob->pt_level = SVGA3D_MOBFMT_RANGE;
124 mob->pt_root_page = vmw_piter_dma_addr(&iter);
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100125 } else {
126 ret = vmw_mob_pt_populate(dev_priv, mob);
127 if (unlikely(ret != 0))
128 goto out_no_populate;
129
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -0700130 vmw_mob_pt_setup(mob, iter, otable->size >> PAGE_SHIFT);
Thomas Hellstromf2a0dcb2014-01-15 10:04:07 +0100131 mob->pt_level += VMW_MOBFMT_PTDEPTH_1 - SVGA3D_MOBFMT_PTDEPTH_1;
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100132 }
133
134 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
135 if (unlikely(cmd == NULL)) {
136 DRM_ERROR("Failed reserving FIFO space for OTable setup.\n");
137 goto out_no_fifo;
138 }
139
140 memset(cmd, 0, sizeof(*cmd));
Thomas Hellstrom3e894a62014-01-20 11:33:04 +0100141 cmd->header.id = SVGA_3D_CMD_SET_OTABLE_BASE64;
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100142 cmd->header.size = sizeof(cmd->body);
143 cmd->body.type = type;
Thomas Hellstrom3e894a62014-01-20 11:33:04 +0100144 cmd->body.baseAddress = cpu_to_le64(mob->pt_root_page >> PAGE_SHIFT);
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100145 cmd->body.sizeInBytes = otable->size;
146 cmd->body.validSizeInBytes = 0;
147 cmd->body.ptDepth = mob->pt_level;
148
Thomas Hellstromf2a0dcb2014-01-15 10:04:07 +0100149 /*
150 * The device doesn't support this, But the otable size is
151 * determined at compile-time, so this BUG shouldn't trigger
152 * randomly.
153 */
154 BUG_ON(mob->pt_level == VMW_MOBFMT_PTDEPTH_2);
155
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100156 vmw_fifo_commit(dev_priv, sizeof(*cmd));
157 otable->page_table = mob;
158
159 return 0;
160
161out_no_fifo:
162out_no_populate:
163 vmw_mob_destroy(mob);
164 return ret;
165}
166
167/*
168 * vmw_takedown_otable_base - Issue an object table base takedown command
169 * to the device
170 *
171 * @dev_priv: Pointer to a device private structure
172 * @type: Type of object table base
173 *
174 */
175static void vmw_takedown_otable_base(struct vmw_private *dev_priv,
176 SVGAOTableType type,
177 struct vmw_otable *otable)
178{
179 struct {
180 SVGA3dCmdHeader header;
181 SVGA3dCmdSetOTableBase body;
182 } *cmd;
Thomas Hellstrom3e894a62014-01-20 11:33:04 +0100183 struct ttm_buffer_object *bo;
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100184
185 if (otable->page_table == NULL)
186 return;
187
Thomas Hellstrom3e894a62014-01-20 11:33:04 +0100188 bo = otable->page_table->pt_bo;
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100189 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
190 if (unlikely(cmd == NULL))
191 DRM_ERROR("Failed reserving FIFO space for OTable setup.\n");
192
193 memset(cmd, 0, sizeof(*cmd));
194 cmd->header.id = SVGA_3D_CMD_SET_OTABLE_BASE;
195 cmd->header.size = sizeof(cmd->body);
196 cmd->body.type = type;
197 cmd->body.baseAddress = 0;
198 cmd->body.sizeInBytes = 0;
199 cmd->body.validSizeInBytes = 0;
200 cmd->body.ptDepth = SVGA3D_MOBFMT_INVALID;
201 vmw_fifo_commit(dev_priv, sizeof(*cmd));
202
203 if (bo) {
204 int ret;
205
Thomas Hellstrom3e894a62014-01-20 11:33:04 +0100206 ret = ttm_bo_reserve(bo, false, true, false, NULL);
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100207 BUG_ON(ret != 0);
208
209 vmw_fence_single_bo(bo, NULL);
210 ttm_bo_unreserve(bo);
211 }
212
213 vmw_mob_destroy(otable->page_table);
214 otable->page_table = NULL;
215}
216
217/*
218 * vmw_otables_setup - Set up guest backed memory object tables
219 *
220 * @dev_priv: Pointer to a device private structure
221 *
222 * Takes care of the device guest backed surface
223 * initialization, by setting up the guest backed memory object tables.
224 * Returns 0 on success and various error codes on failure. A succesful return
225 * means the object tables can be taken down using the vmw_otables_takedown
226 * function.
227 */
228int vmw_otables_setup(struct vmw_private *dev_priv)
229{
230 unsigned long offset;
231 unsigned long bo_size;
232 struct vmw_otable *otables;
233 SVGAOTableType i;
234 int ret;
235
Thomas Hellstrom7cba9062014-01-09 11:03:18 +0100236 otables = kzalloc(SVGA_OTABLE_DX9_MAX * sizeof(*otables),
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100237 GFP_KERNEL);
238 if (unlikely(otables == NULL)) {
239 DRM_ERROR("Failed to allocate space for otable "
240 "metadata.\n");
241 return -ENOMEM;
242 }
243
244 otables[SVGA_OTABLE_MOB].size =
245 VMWGFX_NUM_MOB * SVGA3D_OTABLE_MOB_ENTRY_SIZE;
246 otables[SVGA_OTABLE_SURFACE].size =
247 VMWGFX_NUM_GB_SURFACE * SVGA3D_OTABLE_SURFACE_ENTRY_SIZE;
248 otables[SVGA_OTABLE_CONTEXT].size =
249 VMWGFX_NUM_GB_CONTEXT * SVGA3D_OTABLE_CONTEXT_ENTRY_SIZE;
250 otables[SVGA_OTABLE_SHADER].size =
251 VMWGFX_NUM_GB_SHADER * SVGA3D_OTABLE_SHADER_ENTRY_SIZE;
Thomas Hellstrom7cba9062014-01-09 11:03:18 +0100252 otables[SVGA_OTABLE_SCREEN_TARGET].size =
253 VMWGFX_NUM_GB_SCREEN_TARGET *
254 SVGA3D_OTABLE_SCREEN_TARGET_ENTRY_SIZE;
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100255
256 bo_size = 0;
Thomas Hellstrom7cba9062014-01-09 11:03:18 +0100257 for (i = 0; i < SVGA_OTABLE_DX9_MAX; ++i) {
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100258 otables[i].size =
259 (otables[i].size + PAGE_SIZE - 1) & PAGE_MASK;
260 bo_size += otables[i].size;
261 }
262
263 ret = ttm_bo_create(&dev_priv->bdev, bo_size,
264 ttm_bo_type_device,
265 &vmw_sys_ne_placement,
266 0, false, NULL,
267 &dev_priv->otable_bo);
268
269 if (unlikely(ret != 0))
270 goto out_no_bo;
271
Thomas Hellstrom3e894a62014-01-20 11:33:04 +0100272 ret = ttm_bo_reserve(dev_priv->otable_bo, false, true, false, NULL);
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100273 BUG_ON(ret != 0);
274 ret = vmw_bo_driver.ttm_tt_populate(dev_priv->otable_bo->ttm);
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100275 if (unlikely(ret != 0))
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -0700276 goto out_unreserve;
277 ret = vmw_bo_map_dma(dev_priv->otable_bo);
278 if (unlikely(ret != 0))
279 goto out_unreserve;
280
281 ttm_bo_unreserve(dev_priv->otable_bo);
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100282
283 offset = 0;
Thomas Hellstrom7cba9062014-01-09 11:03:18 +0100284 for (i = 0; i < SVGA_OTABLE_DX9_MAX - VMW_OTABLE_SETUP_SUB; ++i) {
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100285 ret = vmw_setup_otable_base(dev_priv, i, offset,
286 &otables[i]);
287 if (unlikely(ret != 0))
288 goto out_no_setup;
289 offset += otables[i].size;
290 }
291
292 dev_priv->otables = otables;
293 return 0;
294
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -0700295out_unreserve:
296 ttm_bo_unreserve(dev_priv->otable_bo);
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100297out_no_setup:
Thomas Hellstrom7cba9062014-01-09 11:03:18 +0100298 for (i = 0; i < SVGA_OTABLE_DX9_MAX - VMW_OTABLE_SETUP_SUB; ++i)
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100299 vmw_takedown_otable_base(dev_priv, i, &otables[i]);
300
301 ttm_bo_unref(&dev_priv->otable_bo);
302out_no_bo:
303 kfree(otables);
304 return ret;
305}
306
307
308/*
309 * vmw_otables_takedown - Take down guest backed memory object tables
310 *
311 * @dev_priv: Pointer to a device private structure
312 *
313 * Take down the Guest Memory Object tables.
314 */
315void vmw_otables_takedown(struct vmw_private *dev_priv)
316{
317 SVGAOTableType i;
318 struct ttm_buffer_object *bo = dev_priv->otable_bo;
319 int ret;
320
Thomas Hellstrom7cba9062014-01-09 11:03:18 +0100321 for (i = 0; i < SVGA_OTABLE_DX9_MAX - VMW_OTABLE_SETUP_SUB; ++i)
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100322 vmw_takedown_otable_base(dev_priv, i,
323 &dev_priv->otables[i]);
324
Thomas Hellstrom3e894a62014-01-20 11:33:04 +0100325 ret = ttm_bo_reserve(bo, false, true, false, NULL);
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100326 BUG_ON(ret != 0);
327
328 vmw_fence_single_bo(bo, NULL);
329 ttm_bo_unreserve(bo);
330
331 ttm_bo_unref(&dev_priv->otable_bo);
332 kfree(dev_priv->otables);
333 dev_priv->otables = NULL;
334}
335
336
337/*
338 * vmw_mob_calculate_pt_pages - Calculate the number of page table pages
339 * needed for a guest backed memory object.
340 *
341 * @data_pages: Number of data pages in the memory object buffer.
342 */
343static unsigned long vmw_mob_calculate_pt_pages(unsigned long data_pages)
344{
345 unsigned long data_size = data_pages * PAGE_SIZE;
346 unsigned long tot_size = 0;
347
348 while (likely(data_size > PAGE_SIZE)) {
349 data_size = DIV_ROUND_UP(data_size, PAGE_SIZE);
350 data_size *= VMW_PPN_SIZE;
351 tot_size += (data_size + PAGE_SIZE - 1) & PAGE_MASK;
352 }
353
354 return tot_size >> PAGE_SHIFT;
355}
356
357/*
358 * vmw_mob_create - Create a mob, but don't populate it.
359 *
360 * @data_pages: Number of data pages of the underlying buffer object.
361 */
362struct vmw_mob *vmw_mob_create(unsigned long data_pages)
363{
364 struct vmw_mob *mob = kzalloc(sizeof(*mob), GFP_KERNEL);
365
366 if (unlikely(mob == NULL))
367 return NULL;
368
369 mob->num_pages = vmw_mob_calculate_pt_pages(data_pages);
370
371 return mob;
372}
373
374/*
375 * vmw_mob_pt_populate - Populate the mob pagetable
376 *
377 * @mob: Pointer to the mob the pagetable of which we want to
378 * populate.
379 *
380 * This function allocates memory to be used for the pagetable, and
381 * adjusts TTM memory accounting accordingly. Returns ENOMEM if
382 * memory resources aren't sufficient and may cause TTM buffer objects
383 * to be swapped out by using the TTM memory accounting function.
384 */
385static int vmw_mob_pt_populate(struct vmw_private *dev_priv,
386 struct vmw_mob *mob)
387{
388 int ret;
389 BUG_ON(mob->pt_bo != NULL);
390
391 ret = ttm_bo_create(&dev_priv->bdev, mob->num_pages * PAGE_SIZE,
392 ttm_bo_type_device,
393 &vmw_sys_ne_placement,
394 0, false, NULL, &mob->pt_bo);
395 if (unlikely(ret != 0))
396 return ret;
397
Thomas Hellstrom3e894a62014-01-20 11:33:04 +0100398 ret = ttm_bo_reserve(mob->pt_bo, false, true, false, NULL);
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100399
400 BUG_ON(ret != 0);
401 ret = vmw_bo_driver.ttm_tt_populate(mob->pt_bo->ttm);
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100402 if (unlikely(ret != 0))
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -0700403 goto out_unreserve;
404 ret = vmw_bo_map_dma(mob->pt_bo);
405 if (unlikely(ret != 0))
406 goto out_unreserve;
407
408 ttm_bo_unreserve(mob->pt_bo);
409
410 return 0;
411
412out_unreserve:
413 ttm_bo_unreserve(mob->pt_bo);
414 ttm_bo_unref(&mob->pt_bo);
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100415
416 return ret;
417}
418
Thomas Hellstromf2a0dcb2014-01-15 10:04:07 +0100419/**
420 * vmw_mob_assign_ppn - Assign a value to a page table entry
421 *
422 * @addr: Pointer to pointer to page table entry.
423 * @val: The page table entry
424 *
425 * Assigns a value to a page table entry pointed to by *@addr and increments
426 * *@addr according to the page table entry size.
427 */
428#if (VMW_PPN_SIZE == 8)
Thomas Hellstrom3e894a62014-01-20 11:33:04 +0100429static void vmw_mob_assign_ppn(__le32 **addr, dma_addr_t val)
Thomas Hellstromf2a0dcb2014-01-15 10:04:07 +0100430{
Thomas Hellstrom3e894a62014-01-20 11:33:04 +0100431 *((__le64 *) *addr) = cpu_to_le64(val >> PAGE_SHIFT);
Thomas Hellstromf2a0dcb2014-01-15 10:04:07 +0100432 *addr += 2;
433}
434#else
Thomas Hellstrom3e894a62014-01-20 11:33:04 +0100435static void vmw_mob_assign_ppn(__le32 **addr, dma_addr_t val)
Thomas Hellstromf2a0dcb2014-01-15 10:04:07 +0100436{
Thomas Hellstrom3e894a62014-01-20 11:33:04 +0100437 *(*addr)++ = cpu_to_le32(val >> PAGE_SHIFT);
Thomas Hellstromf2a0dcb2014-01-15 10:04:07 +0100438}
439#endif
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100440
441/*
442 * vmw_mob_build_pt - Build a pagetable
443 *
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -0700444 * @data_addr: Array of DMA addresses to the underlying buffer
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100445 * object's data pages.
446 * @num_data_pages: Number of buffer object data pages.
447 * @pt_pages: Array of page pointers to the page table pages.
448 *
449 * Returns the number of page table pages actually used.
450 * Uses atomic kmaps of highmem pages to avoid TLB thrashing.
451 */
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -0700452static unsigned long vmw_mob_build_pt(struct vmw_piter *data_iter,
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100453 unsigned long num_data_pages,
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -0700454 struct vmw_piter *pt_iter)
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100455{
456 unsigned long pt_size = num_data_pages * VMW_PPN_SIZE;
457 unsigned long num_pt_pages = DIV_ROUND_UP(pt_size, PAGE_SIZE);
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -0700458 unsigned long pt_page;
Thomas Hellstrom3e894a62014-01-20 11:33:04 +0100459 __le32 *addr, *save_addr;
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100460 unsigned long i;
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -0700461 struct page *page;
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100462
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100463 for (pt_page = 0; pt_page < num_pt_pages; ++pt_page) {
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -0700464 page = vmw_piter_page(pt_iter);
465
466 save_addr = addr = kmap_atomic(page);
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100467
468 for (i = 0; i < PAGE_SIZE / VMW_PPN_SIZE; ++i) {
Thomas Hellstromf2a0dcb2014-01-15 10:04:07 +0100469 vmw_mob_assign_ppn(&addr,
470 vmw_piter_dma_addr(data_iter));
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -0700471 if (unlikely(--num_data_pages == 0))
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100472 break;
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -0700473 WARN_ON(!vmw_piter_next(data_iter));
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100474 }
475 kunmap_atomic(save_addr);
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -0700476 vmw_piter_next(pt_iter);
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100477 }
478
479 return num_pt_pages;
480}
481
482/*
483 * vmw_mob_build_pt - Set up a multilevel mob pagetable
484 *
485 * @mob: Pointer to a mob whose page table needs setting up.
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -0700486 * @data_addr Array of DMA addresses to the buffer object's data
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100487 * pages.
488 * @num_data_pages: Number of buffer object data pages.
489 *
490 * Uses tail recursion to set up a multilevel mob page table.
491 */
492static void vmw_mob_pt_setup(struct vmw_mob *mob,
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -0700493 struct vmw_piter data_iter,
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100494 unsigned long num_data_pages)
495{
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100496 unsigned long num_pt_pages = 0;
497 struct ttm_buffer_object *bo = mob->pt_bo;
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -0700498 struct vmw_piter save_pt_iter;
499 struct vmw_piter pt_iter;
500 const struct vmw_sg_table *vsgt;
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100501 int ret;
502
Thomas Hellstrom3e894a62014-01-20 11:33:04 +0100503 ret = ttm_bo_reserve(bo, false, true, false, NULL);
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100504 BUG_ON(ret != 0);
505
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -0700506 vsgt = vmw_bo_sg_table(bo);
507 vmw_piter_start(&pt_iter, vsgt, 0);
508 BUG_ON(!vmw_piter_next(&pt_iter));
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100509 mob->pt_level = 0;
510 while (likely(num_data_pages > 1)) {
511 ++mob->pt_level;
512 BUG_ON(mob->pt_level > 2);
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -0700513 save_pt_iter = pt_iter;
514 num_pt_pages = vmw_mob_build_pt(&data_iter, num_data_pages,
515 &pt_iter);
516 data_iter = save_pt_iter;
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100517 num_data_pages = num_pt_pages;
518 }
519
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -0700520 mob->pt_root_page = vmw_piter_dma_addr(&save_pt_iter);
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100521 ttm_bo_unreserve(bo);
522}
523
524/*
525 * vmw_mob_destroy - Destroy a mob, unpopulating first if necessary.
526 *
527 * @mob: Pointer to a mob to destroy.
528 */
529void vmw_mob_destroy(struct vmw_mob *mob)
530{
531 if (mob->pt_bo)
532 ttm_bo_unref(&mob->pt_bo);
533 kfree(mob);
534}
535
536/*
537 * vmw_mob_unbind - Hide a mob from the device.
538 *
539 * @dev_priv: Pointer to a device private.
540 * @mob_id: Device id of the mob to unbind.
541 */
542void vmw_mob_unbind(struct vmw_private *dev_priv,
543 struct vmw_mob *mob)
544{
545 struct {
546 SVGA3dCmdHeader header;
547 SVGA3dCmdDestroyGBMob body;
548 } *cmd;
549 int ret;
550 struct ttm_buffer_object *bo = mob->pt_bo;
551
552 if (bo) {
Thomas Hellstrom3e894a62014-01-20 11:33:04 +0100553 ret = ttm_bo_reserve(bo, false, true, false, NULL);
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100554 /*
555 * Noone else should be using this buffer.
556 */
557 BUG_ON(ret != 0);
558 }
559
560 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
561 if (unlikely(cmd == NULL)) {
562 DRM_ERROR("Failed reserving FIFO space for Memory "
563 "Object unbinding.\n");
564 }
565 cmd->header.id = SVGA_3D_CMD_DESTROY_GB_MOB;
566 cmd->header.size = sizeof(cmd->body);
567 cmd->body.mobid = mob->id;
568 vmw_fifo_commit(dev_priv, sizeof(*cmd));
569 if (bo) {
570 vmw_fence_single_bo(bo, NULL);
571 ttm_bo_unreserve(bo);
572 }
573 vmw_3d_resource_dec(dev_priv, false);
574}
575
576/*
577 * vmw_mob_bind - Make a mob visible to the device after first
578 * populating it if necessary.
579 *
580 * @dev_priv: Pointer to a device private.
581 * @mob: Pointer to the mob we're making visible.
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -0700582 * @data_addr: Array of DMA addresses to the data pages of the underlying
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100583 * buffer object.
584 * @num_data_pages: Number of data pages of the underlying buffer
585 * object.
586 * @mob_id: Device id of the mob to bind
587 *
588 * This function is intended to be interfaced with the ttm_tt backend
589 * code.
590 */
591int vmw_mob_bind(struct vmw_private *dev_priv,
592 struct vmw_mob *mob,
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -0700593 const struct vmw_sg_table *vsgt,
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100594 unsigned long num_data_pages,
595 int32_t mob_id)
596{
597 int ret;
598 bool pt_set_up = false;
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -0700599 struct vmw_piter data_iter;
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100600 struct {
601 SVGA3dCmdHeader header;
Thomas Hellstrom3e894a62014-01-20 11:33:04 +0100602 SVGA3dCmdDefineGBMob64 body;
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100603 } *cmd;
604
605 mob->id = mob_id;
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -0700606 vmw_piter_start(&data_iter, vsgt, 0);
607 if (unlikely(!vmw_piter_next(&data_iter)))
608 return 0;
609
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100610 if (likely(num_data_pages == 1)) {
Thomas Hellstromf2a0dcb2014-01-15 10:04:07 +0100611 mob->pt_level = VMW_MOBFMT_PTDEPTH_0;
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -0700612 mob->pt_root_page = vmw_piter_dma_addr(&data_iter);
613 } else if (vsgt->num_regions == 1) {
614 mob->pt_level = SVGA3D_MOBFMT_RANGE;
615 mob->pt_root_page = vmw_piter_dma_addr(&data_iter);
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100616 } else if (unlikely(mob->pt_bo == NULL)) {
617 ret = vmw_mob_pt_populate(dev_priv, mob);
618 if (unlikely(ret != 0))
619 return ret;
620
Thomas Hellstrom0fd53cf2013-10-24 13:27:38 -0700621 vmw_mob_pt_setup(mob, data_iter, num_data_pages);
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100622 pt_set_up = true;
Thomas Hellstromf2a0dcb2014-01-15 10:04:07 +0100623 mob->pt_level += VMW_MOBFMT_PTDEPTH_1 - SVGA3D_MOBFMT_PTDEPTH_1;
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100624 }
625
626 (void) vmw_3d_resource_inc(dev_priv, false);
627
628 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
629 if (unlikely(cmd == NULL)) {
630 DRM_ERROR("Failed reserving FIFO space for Memory "
631 "Object binding.\n");
632 goto out_no_cmd_space;
633 }
634
Thomas Hellstrom3e894a62014-01-20 11:33:04 +0100635 cmd->header.id = SVGA_3D_CMD_DEFINE_GB_MOB64;
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100636 cmd->header.size = sizeof(cmd->body);
637 cmd->body.mobid = mob_id;
638 cmd->body.ptDepth = mob->pt_level;
Thomas Hellstrom3e894a62014-01-20 11:33:04 +0100639 cmd->body.base = cpu_to_le64(mob->pt_root_page >> PAGE_SHIFT);
Thomas Hellstrom3530bdc2012-11-21 10:49:52 +0100640 cmd->body.sizeInBytes = num_data_pages * PAGE_SIZE;
641
642 vmw_fifo_commit(dev_priv, sizeof(*cmd));
643
644 return 0;
645
646out_no_cmd_space:
647 vmw_3d_resource_dec(dev_priv, false);
648 if (pt_set_up)
649 ttm_bo_unref(&mob->pt_bo);
650
651 return -ENOMEM;
652}