blob: 7681341fe32b8725840d70b137782f5f1f316bc0 [file] [log] [blame]
Thomas Hellstrom543831c2012-11-20 12:19:36 +00001/**************************************************************************
2 *
Sinclair Yeh54fbde82015-07-29 12:38:02 -07003 * Copyright © 2009-2015 VMware, Inc., Palo Alto, CA., USA
Thomas Hellstrom543831c2012-11-20 12:19:36 +00004 * 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#include "vmwgfx_resource_priv.h"
Thomas Hellstromd80efd52015-08-10 10:39:35 -070030#include "vmwgfx_so.h"
31#include "vmwgfx_binding.h"
Thomas Hellstrom543831c2012-11-20 12:19:36 +000032#include <ttm/ttm_placement.h>
Sinclair Yeh8ce75f82015-07-08 21:20:39 -070033#include "device_include/svga3d_surfacedefs.h"
Thomas Hellstrom543831c2012-11-20 12:19:36 +000034
Thomas Hellstromd80efd52015-08-10 10:39:35 -070035
Thomas Hellstrom543831c2012-11-20 12:19:36 +000036/**
37 * struct vmw_user_surface - User-space visible surface resource
38 *
39 * @base: The TTM base object handling user-space visibility.
40 * @srf: The surface metadata.
41 * @size: TTM accounting size for the surface.
Sinclair Yeh233826a2015-03-05 01:06:13 -080042 * @master: master of the creating client. Used for security check.
Thomas Hellstrom543831c2012-11-20 12:19:36 +000043 */
44struct vmw_user_surface {
Thomas Hellstrom79e5f812013-11-08 02:12:51 -080045 struct ttm_prime_object prime;
Thomas Hellstrom543831c2012-11-20 12:19:36 +000046 struct vmw_surface srf;
47 uint32_t size;
Thomas Hellstrom6d10aab2014-03-19 10:45:11 +010048 struct drm_master *master;
Thomas Hellstrom54c12bc2015-09-14 01:13:11 -070049 struct ttm_base_object *backup_base;
Thomas Hellstrom543831c2012-11-20 12:19:36 +000050};
51
52/**
53 * struct vmw_surface_offset - Backing store mip level offset info
54 *
55 * @face: Surface face.
56 * @mip: Mip level.
57 * @bo_offset: Offset into backing store of this mip level.
58 *
59 */
60struct vmw_surface_offset {
61 uint32_t face;
62 uint32_t mip;
63 uint32_t bo_offset;
64};
65
66static void vmw_user_surface_free(struct vmw_resource *res);
67static struct vmw_resource *
68vmw_user_surface_base_to_res(struct ttm_base_object *base);
69static int vmw_legacy_srf_bind(struct vmw_resource *res,
70 struct ttm_validate_buffer *val_buf);
71static int vmw_legacy_srf_unbind(struct vmw_resource *res,
72 bool readback,
73 struct ttm_validate_buffer *val_buf);
74static int vmw_legacy_srf_create(struct vmw_resource *res);
75static int vmw_legacy_srf_destroy(struct vmw_resource *res);
Thomas Hellstroma97e2192012-11-21 11:45:13 +010076static int vmw_gb_surface_create(struct vmw_resource *res);
77static int vmw_gb_surface_bind(struct vmw_resource *res,
78 struct ttm_validate_buffer *val_buf);
79static int vmw_gb_surface_unbind(struct vmw_resource *res,
80 bool readback,
81 struct ttm_validate_buffer *val_buf);
82static int vmw_gb_surface_destroy(struct vmw_resource *res);
83
Thomas Hellstrom543831c2012-11-20 12:19:36 +000084
85static const struct vmw_user_resource_conv user_surface_conv = {
86 .object_type = VMW_RES_SURFACE,
87 .base_obj_to_res = vmw_user_surface_base_to_res,
88 .res_free = vmw_user_surface_free
89};
90
91const struct vmw_user_resource_conv *user_surface_converter =
92 &user_surface_conv;
93
94
95static uint64_t vmw_user_surface_size;
96
97static const struct vmw_res_func vmw_legacy_surface_func = {
98 .res_type = vmw_res_surface,
99 .needs_backup = false,
100 .may_evict = true,
101 .type_name = "legacy surfaces",
102 .backup_placement = &vmw_srf_placement,
103 .create = &vmw_legacy_srf_create,
104 .destroy = &vmw_legacy_srf_destroy,
105 .bind = &vmw_legacy_srf_bind,
106 .unbind = &vmw_legacy_srf_unbind
107};
108
Thomas Hellstroma97e2192012-11-21 11:45:13 +0100109static const struct vmw_res_func vmw_gb_surface_func = {
110 .res_type = vmw_res_surface,
111 .needs_backup = true,
112 .may_evict = true,
113 .type_name = "guest backed surfaces",
114 .backup_placement = &vmw_mob_placement,
115 .create = vmw_gb_surface_create,
116 .destroy = vmw_gb_surface_destroy,
117 .bind = vmw_gb_surface_bind,
118 .unbind = vmw_gb_surface_unbind
119};
120
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000121/**
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000122 * struct vmw_surface_dma - SVGA3D DMA command
123 */
124struct vmw_surface_dma {
125 SVGA3dCmdHeader header;
126 SVGA3dCmdSurfaceDMA body;
127 SVGA3dCopyBox cb;
128 SVGA3dCmdSurfaceDMASuffix suffix;
129};
130
131/**
132 * struct vmw_surface_define - SVGA3D Surface Define command
133 */
134struct vmw_surface_define {
135 SVGA3dCmdHeader header;
136 SVGA3dCmdDefineSurface body;
137};
138
139/**
140 * struct vmw_surface_destroy - SVGA3D Surface Destroy command
141 */
142struct vmw_surface_destroy {
143 SVGA3dCmdHeader header;
144 SVGA3dCmdDestroySurface body;
145};
146
147
148/**
149 * vmw_surface_dma_size - Compute fifo size for a dma command.
150 *
151 * @srf: Pointer to a struct vmw_surface
152 *
153 * Computes the required size for a surface dma command for backup or
154 * restoration of the surface represented by @srf.
155 */
156static inline uint32_t vmw_surface_dma_size(const struct vmw_surface *srf)
157{
158 return srf->num_sizes * sizeof(struct vmw_surface_dma);
159}
160
161
162/**
163 * vmw_surface_define_size - Compute fifo size for a surface define command.
164 *
165 * @srf: Pointer to a struct vmw_surface
166 *
167 * Computes the required size for a surface define command for the definition
168 * of the surface represented by @srf.
169 */
170static inline uint32_t vmw_surface_define_size(const struct vmw_surface *srf)
171{
172 return sizeof(struct vmw_surface_define) + srf->num_sizes *
173 sizeof(SVGA3dSize);
174}
175
176
177/**
178 * vmw_surface_destroy_size - Compute fifo size for a surface destroy command.
179 *
180 * Computes the required size for a surface destroy command for the destruction
181 * of a hw surface.
182 */
183static inline uint32_t vmw_surface_destroy_size(void)
184{
185 return sizeof(struct vmw_surface_destroy);
186}
187
188/**
189 * vmw_surface_destroy_encode - Encode a surface_destroy command.
190 *
191 * @id: The surface id
192 * @cmd_space: Pointer to memory area in which the commands should be encoded.
193 */
194static void vmw_surface_destroy_encode(uint32_t id,
195 void *cmd_space)
196{
197 struct vmw_surface_destroy *cmd = (struct vmw_surface_destroy *)
198 cmd_space;
199
200 cmd->header.id = SVGA_3D_CMD_SURFACE_DESTROY;
201 cmd->header.size = sizeof(cmd->body);
202 cmd->body.sid = id;
203}
204
205/**
206 * vmw_surface_define_encode - Encode a surface_define command.
207 *
208 * @srf: Pointer to a struct vmw_surface object.
209 * @cmd_space: Pointer to memory area in which the commands should be encoded.
210 */
211static void vmw_surface_define_encode(const struct vmw_surface *srf,
212 void *cmd_space)
213{
214 struct vmw_surface_define *cmd = (struct vmw_surface_define *)
215 cmd_space;
216 struct drm_vmw_size *src_size;
217 SVGA3dSize *cmd_size;
218 uint32_t cmd_len;
219 int i;
220
221 cmd_len = sizeof(cmd->body) + srf->num_sizes * sizeof(SVGA3dSize);
222
223 cmd->header.id = SVGA_3D_CMD_SURFACE_DEFINE;
224 cmd->header.size = cmd_len;
225 cmd->body.sid = srf->res.id;
226 cmd->body.surfaceFlags = srf->flags;
Thomas Hellstromb9eb1a62015-04-02 02:39:45 -0700227 cmd->body.format = srf->format;
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000228 for (i = 0; i < DRM_VMW_MAX_SURFACE_FACES; ++i)
229 cmd->body.face[i].numMipLevels = srf->mip_levels[i];
230
231 cmd += 1;
232 cmd_size = (SVGA3dSize *) cmd;
233 src_size = srf->sizes;
234
235 for (i = 0; i < srf->num_sizes; ++i, cmd_size++, src_size++) {
236 cmd_size->width = src_size->width;
237 cmd_size->height = src_size->height;
238 cmd_size->depth = src_size->depth;
239 }
240}
241
242/**
243 * vmw_surface_dma_encode - Encode a surface_dma command.
244 *
245 * @srf: Pointer to a struct vmw_surface object.
246 * @cmd_space: Pointer to memory area in which the commands should be encoded.
247 * @ptr: Pointer to an SVGAGuestPtr indicating where the surface contents
248 * should be placed or read from.
249 * @to_surface: Boolean whether to DMA to the surface or from the surface.
250 */
251static void vmw_surface_dma_encode(struct vmw_surface *srf,
252 void *cmd_space,
253 const SVGAGuestPtr *ptr,
254 bool to_surface)
255{
256 uint32_t i;
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000257 struct vmw_surface_dma *cmd = (struct vmw_surface_dma *)cmd_space;
Thomas Hellstrom7e8d9da2012-11-20 12:19:37 +0000258 const struct svga3d_surface_desc *desc =
259 svga3dsurface_get_desc(srf->format);
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000260
261 for (i = 0; i < srf->num_sizes; ++i) {
262 SVGA3dCmdHeader *header = &cmd->header;
263 SVGA3dCmdSurfaceDMA *body = &cmd->body;
264 SVGA3dCopyBox *cb = &cmd->cb;
265 SVGA3dCmdSurfaceDMASuffix *suffix = &cmd->suffix;
266 const struct vmw_surface_offset *cur_offset = &srf->offsets[i];
267 const struct drm_vmw_size *cur_size = &srf->sizes[i];
268
269 header->id = SVGA_3D_CMD_SURFACE_DMA;
270 header->size = sizeof(*body) + sizeof(*cb) + sizeof(*suffix);
271
272 body->guest.ptr = *ptr;
273 body->guest.ptr.offset += cur_offset->bo_offset;
Thomas Hellstrom7e8d9da2012-11-20 12:19:37 +0000274 body->guest.pitch = svga3dsurface_calculate_pitch(desc,
275 cur_size);
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000276 body->host.sid = srf->res.id;
277 body->host.face = cur_offset->face;
278 body->host.mipmap = cur_offset->mip;
279 body->transfer = ((to_surface) ? SVGA3D_WRITE_HOST_VRAM :
280 SVGA3D_READ_HOST_VRAM);
281 cb->x = 0;
282 cb->y = 0;
283 cb->z = 0;
284 cb->srcx = 0;
285 cb->srcy = 0;
286 cb->srcz = 0;
287 cb->w = cur_size->width;
288 cb->h = cur_size->height;
289 cb->d = cur_size->depth;
290
291 suffix->suffixSize = sizeof(*suffix);
Thomas Hellstrom7e8d9da2012-11-20 12:19:37 +0000292 suffix->maximumOffset =
293 svga3dsurface_get_image_buffer_size(desc, cur_size,
294 body->guest.pitch);
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000295 suffix->flags.discard = 0;
296 suffix->flags.unsynchronized = 0;
297 suffix->flags.reserved = 0;
298 ++cmd;
299 }
300};
301
302
303/**
304 * vmw_hw_surface_destroy - destroy a Device surface
305 *
306 * @res: Pointer to a struct vmw_resource embedded in a struct
307 * vmw_surface.
308 *
309 * Destroys a the device surface associated with a struct vmw_surface if
310 * any, and adjusts accounting and resource count accordingly.
311 */
312static void vmw_hw_surface_destroy(struct vmw_resource *res)
313{
314
315 struct vmw_private *dev_priv = res->dev_priv;
316 struct vmw_surface *srf;
317 void *cmd;
318
Thomas Hellstroma97e2192012-11-21 11:45:13 +0100319 if (res->func->destroy == vmw_gb_surface_destroy) {
320 (void) vmw_gb_surface_destroy(res);
321 return;
322 }
323
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000324 if (res->id != -1) {
325
326 cmd = vmw_fifo_reserve(dev_priv, vmw_surface_destroy_size());
Markus Elfring862f6152016-09-23 17:53:49 +0200327 if (unlikely(!cmd)) {
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000328 DRM_ERROR("Failed reserving FIFO space for surface "
329 "destruction.\n");
330 return;
331 }
332
333 vmw_surface_destroy_encode(res->id, cmd);
334 vmw_fifo_commit(dev_priv, vmw_surface_destroy_size());
335
336 /*
337 * used_memory_size_atomic, or separate lock
338 * to avoid taking dev_priv::cmdbuf_mutex in
339 * the destroy path.
340 */
341
342 mutex_lock(&dev_priv->cmdbuf_mutex);
343 srf = vmw_res_to_srf(res);
344 dev_priv->used_memory_size -= res->backup_size;
345 mutex_unlock(&dev_priv->cmdbuf_mutex);
346 }
Thomas Hellstrom153b3d52015-06-25 10:47:43 -0700347 vmw_fifo_resource_dec(dev_priv);
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000348}
349
350/**
351 * vmw_legacy_srf_create - Create a device surface as part of the
352 * resource validation process.
353 *
354 * @res: Pointer to a struct vmw_surface.
355 *
356 * If the surface doesn't have a hw id.
357 *
358 * Returns -EBUSY if there wasn't sufficient device resources to
359 * complete the validation. Retry after freeing up resources.
360 *
361 * May return other errors if the kernel is out of guest resources.
362 */
363static int vmw_legacy_srf_create(struct vmw_resource *res)
364{
365 struct vmw_private *dev_priv = res->dev_priv;
366 struct vmw_surface *srf;
367 uint32_t submit_size;
368 uint8_t *cmd;
369 int ret;
370
371 if (likely(res->id != -1))
372 return 0;
373
374 srf = vmw_res_to_srf(res);
375 if (unlikely(dev_priv->used_memory_size + res->backup_size >=
376 dev_priv->memory_size))
377 return -EBUSY;
378
379 /*
380 * Alloc id for the resource.
381 */
382
383 ret = vmw_resource_alloc_id(res);
384 if (unlikely(ret != 0)) {
385 DRM_ERROR("Failed to allocate a surface id.\n");
386 goto out_no_id;
387 }
388
389 if (unlikely(res->id >= SVGA3D_MAX_SURFACE_IDS)) {
390 ret = -EBUSY;
391 goto out_no_fifo;
392 }
393
394 /*
395 * Encode surface define- commands.
396 */
397
398 submit_size = vmw_surface_define_size(srf);
399 cmd = vmw_fifo_reserve(dev_priv, submit_size);
Markus Elfring862f6152016-09-23 17:53:49 +0200400 if (unlikely(!cmd)) {
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000401 DRM_ERROR("Failed reserving FIFO space for surface "
402 "creation.\n");
403 ret = -ENOMEM;
404 goto out_no_fifo;
405 }
406
407 vmw_surface_define_encode(srf, cmd);
408 vmw_fifo_commit(dev_priv, submit_size);
409 /*
410 * Surface memory usage accounting.
411 */
412
413 dev_priv->used_memory_size += res->backup_size;
414 return 0;
415
416out_no_fifo:
417 vmw_resource_release_id(res);
418out_no_id:
419 return ret;
420}
421
422/**
423 * vmw_legacy_srf_dma - Copy backup data to or from a legacy surface.
424 *
425 * @res: Pointer to a struct vmw_res embedded in a struct
426 * vmw_surface.
427 * @val_buf: Pointer to a struct ttm_validate_buffer containing
428 * information about the backup buffer.
429 * @bind: Boolean wether to DMA to the surface.
430 *
431 * Transfer backup data to or from a legacy surface as part of the
432 * validation process.
433 * May return other errors if the kernel is out of guest resources.
434 * The backup buffer will be fenced or idle upon successful completion,
435 * and if the surface needs persistent backup storage, the backup buffer
436 * will also be returned reserved iff @bind is true.
437 */
438static int vmw_legacy_srf_dma(struct vmw_resource *res,
439 struct ttm_validate_buffer *val_buf,
440 bool bind)
441{
442 SVGAGuestPtr ptr;
443 struct vmw_fence_obj *fence;
444 uint32_t submit_size;
445 struct vmw_surface *srf = vmw_res_to_srf(res);
446 uint8_t *cmd;
447 struct vmw_private *dev_priv = res->dev_priv;
448
Markus Elfring862f6152016-09-23 17:53:49 +0200449 BUG_ON(!val_buf->bo);
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000450 submit_size = vmw_surface_dma_size(srf);
451 cmd = vmw_fifo_reserve(dev_priv, submit_size);
Markus Elfring862f6152016-09-23 17:53:49 +0200452 if (unlikely(!cmd)) {
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000453 DRM_ERROR("Failed reserving FIFO space for surface "
454 "DMA.\n");
455 return -ENOMEM;
456 }
457 vmw_bo_get_guest_ptr(val_buf->bo, &ptr);
458 vmw_surface_dma_encode(srf, cmd, &ptr, bind);
459
460 vmw_fifo_commit(dev_priv, submit_size);
461
462 /*
463 * Create a fence object and fence the backup buffer.
464 */
465
466 (void) vmw_execbuf_fence_commands(NULL, dev_priv,
467 &fence, NULL);
468
469 vmw_fence_single_bo(val_buf->bo, fence);
470
471 if (likely(fence != NULL))
472 vmw_fence_obj_unreference(&fence);
473
474 return 0;
475}
476
477/**
478 * vmw_legacy_srf_bind - Perform a legacy surface bind as part of the
479 * surface validation process.
480 *
481 * @res: Pointer to a struct vmw_res embedded in a struct
482 * vmw_surface.
483 * @val_buf: Pointer to a struct ttm_validate_buffer containing
484 * information about the backup buffer.
485 *
486 * This function will copy backup data to the surface if the
487 * backup buffer is dirty.
488 */
489static int vmw_legacy_srf_bind(struct vmw_resource *res,
490 struct ttm_validate_buffer *val_buf)
491{
492 if (!res->backup_dirty)
493 return 0;
494
495 return vmw_legacy_srf_dma(res, val_buf, true);
496}
497
498
499/**
500 * vmw_legacy_srf_unbind - Perform a legacy surface unbind as part of the
501 * surface eviction process.
502 *
503 * @res: Pointer to a struct vmw_res embedded in a struct
504 * vmw_surface.
505 * @val_buf: Pointer to a struct ttm_validate_buffer containing
506 * information about the backup buffer.
507 *
508 * This function will copy backup data from the surface.
509 */
510static int vmw_legacy_srf_unbind(struct vmw_resource *res,
511 bool readback,
512 struct ttm_validate_buffer *val_buf)
513{
514 if (unlikely(readback))
515 return vmw_legacy_srf_dma(res, val_buf, false);
516 return 0;
517}
518
519/**
520 * vmw_legacy_srf_destroy - Destroy a device surface as part of a
521 * resource eviction process.
522 *
523 * @res: Pointer to a struct vmw_res embedded in a struct
524 * vmw_surface.
525 */
526static int vmw_legacy_srf_destroy(struct vmw_resource *res)
527{
528 struct vmw_private *dev_priv = res->dev_priv;
529 uint32_t submit_size;
530 uint8_t *cmd;
531
532 BUG_ON(res->id == -1);
533
534 /*
535 * Encode the dma- and surface destroy commands.
536 */
537
538 submit_size = vmw_surface_destroy_size();
539 cmd = vmw_fifo_reserve(dev_priv, submit_size);
Markus Elfring862f6152016-09-23 17:53:49 +0200540 if (unlikely(!cmd)) {
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000541 DRM_ERROR("Failed reserving FIFO space for surface "
542 "eviction.\n");
543 return -ENOMEM;
544 }
545
546 vmw_surface_destroy_encode(res->id, cmd);
547 vmw_fifo_commit(dev_priv, submit_size);
548
549 /*
550 * Surface memory usage accounting.
551 */
552
553 dev_priv->used_memory_size -= res->backup_size;
554
555 /*
556 * Release the surface ID.
557 */
558
559 vmw_resource_release_id(res);
560
561 return 0;
562}
563
564
565/**
566 * vmw_surface_init - initialize a struct vmw_surface
567 *
568 * @dev_priv: Pointer to a device private struct.
569 * @srf: Pointer to the struct vmw_surface to initialize.
570 * @res_free: Pointer to a resource destructor used to free
571 * the object.
572 */
573static int vmw_surface_init(struct vmw_private *dev_priv,
574 struct vmw_surface *srf,
575 void (*res_free) (struct vmw_resource *res))
576{
577 int ret;
578 struct vmw_resource *res = &srf->res;
579
Markus Elfring862f6152016-09-23 17:53:49 +0200580 BUG_ON(!res_free);
Thomas Hellstroma97e2192012-11-21 11:45:13 +0100581 if (!dev_priv->has_mob)
Thomas Hellstrom153b3d52015-06-25 10:47:43 -0700582 vmw_fifo_resource_inc(dev_priv);
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000583 ret = vmw_resource_init(dev_priv, res, true, res_free,
Thomas Hellstroma97e2192012-11-21 11:45:13 +0100584 (dev_priv->has_mob) ? &vmw_gb_surface_func :
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000585 &vmw_legacy_surface_func);
586
587 if (unlikely(ret != 0)) {
Thomas Hellstroma97e2192012-11-21 11:45:13 +0100588 if (!dev_priv->has_mob)
Thomas Hellstrom153b3d52015-06-25 10:47:43 -0700589 vmw_fifo_resource_dec(dev_priv);
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000590 res_free(res);
591 return ret;
592 }
593
594 /*
595 * The surface won't be visible to hardware until a
596 * surface validate.
597 */
598
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700599 INIT_LIST_HEAD(&srf->view_list);
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000600 vmw_resource_activate(res, vmw_hw_surface_destroy);
601 return ret;
602}
603
604/**
605 * vmw_user_surface_base_to_res - TTM base object to resource converter for
606 * user visible surfaces
607 *
608 * @base: Pointer to a TTM base object
609 *
610 * Returns the struct vmw_resource embedded in a struct vmw_surface
611 * for the user-visible object identified by the TTM base object @base.
612 */
613static struct vmw_resource *
614vmw_user_surface_base_to_res(struct ttm_base_object *base)
615{
Thomas Hellstrom79e5f812013-11-08 02:12:51 -0800616 return &(container_of(base, struct vmw_user_surface,
617 prime.base)->srf.res);
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000618}
619
620/**
621 * vmw_user_surface_free - User visible surface resource destructor
622 *
623 * @res: A struct vmw_resource embedded in a struct vmw_surface.
624 */
625static void vmw_user_surface_free(struct vmw_resource *res)
626{
627 struct vmw_surface *srf = vmw_res_to_srf(res);
628 struct vmw_user_surface *user_srf =
629 container_of(srf, struct vmw_user_surface, srf);
630 struct vmw_private *dev_priv = srf->res.dev_priv;
631 uint32_t size = user_srf->size;
632
Thomas Hellstrom6d10aab2014-03-19 10:45:11 +0100633 if (user_srf->master)
634 drm_master_put(&user_srf->master);
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000635 kfree(srf->offsets);
636 kfree(srf->sizes);
637 kfree(srf->snooper.image);
Thomas Hellstrom79e5f812013-11-08 02:12:51 -0800638 ttm_prime_object_kfree(user_srf, prime);
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000639 ttm_mem_global_free(vmw_mem_glob(dev_priv), size);
640}
641
642/**
643 * vmw_user_surface_free - User visible surface TTM base object destructor
644 *
645 * @p_base: Pointer to a pointer to a TTM base object
646 * embedded in a struct vmw_user_surface.
647 *
648 * Drops the base object's reference on its resource, and the
649 * pointer pointed to by *p_base is set to NULL.
650 */
651static void vmw_user_surface_base_release(struct ttm_base_object **p_base)
652{
653 struct ttm_base_object *base = *p_base;
654 struct vmw_user_surface *user_srf =
Thomas Hellstrom79e5f812013-11-08 02:12:51 -0800655 container_of(base, struct vmw_user_surface, prime.base);
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000656 struct vmw_resource *res = &user_srf->srf.res;
657
658 *p_base = NULL;
Thomas Hellstromed7d78b2015-10-12 01:49:39 -0700659 if (user_srf->backup_base)
660 ttm_base_object_unref(&user_srf->backup_base);
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000661 vmw_resource_unreference(&res);
662}
663
664/**
665 * vmw_user_surface_destroy_ioctl - Ioctl function implementing
666 * the user surface destroy functionality.
667 *
668 * @dev: Pointer to a struct drm_device.
669 * @data: Pointer to data copied from / to user-space.
670 * @file_priv: Pointer to a drm file private structure.
671 */
672int vmw_surface_destroy_ioctl(struct drm_device *dev, void *data,
673 struct drm_file *file_priv)
674{
675 struct drm_vmw_surface_arg *arg = (struct drm_vmw_surface_arg *)data;
676 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
677
678 return ttm_ref_object_base_unref(tfile, arg->sid, TTM_REF_USAGE);
679}
680
681/**
682 * vmw_user_surface_define_ioctl - Ioctl function implementing
683 * the user surface define functionality.
684 *
685 * @dev: Pointer to a struct drm_device.
686 * @data: Pointer to data copied from / to user-space.
687 * @file_priv: Pointer to a drm file private structure.
688 */
689int vmw_surface_define_ioctl(struct drm_device *dev, void *data,
690 struct drm_file *file_priv)
691{
692 struct vmw_private *dev_priv = vmw_priv(dev);
693 struct vmw_user_surface *user_srf;
694 struct vmw_surface *srf;
695 struct vmw_resource *res;
696 struct vmw_resource *tmp;
697 union drm_vmw_surface_create_arg *arg =
698 (union drm_vmw_surface_create_arg *)data;
699 struct drm_vmw_surface_create_req *req = &arg->req;
700 struct drm_vmw_surface_arg *rep = &arg->rep;
701 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000702 int ret;
703 int i, j;
704 uint32_t cur_bo_offset;
705 struct drm_vmw_size *cur_size;
706 struct vmw_surface_offset *cur_offset;
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000707 uint32_t num_sizes;
708 uint32_t size;
Thomas Hellstrom7e8d9da2012-11-20 12:19:37 +0000709 const struct svga3d_surface_desc *desc;
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000710
711 if (unlikely(vmw_user_surface_size == 0))
712 vmw_user_surface_size = ttm_round_pot(sizeof(*user_srf)) +
713 128;
714
715 num_sizes = 0;
Li Qiange7e11f92017-03-27 20:10:53 -0700716 for (i = 0; i < DRM_VMW_MAX_SURFACE_FACES; ++i) {
717 if (req->mip_levels[i] > DRM_VMW_MAX_MIP_LEVELS)
718 return -EINVAL;
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000719 num_sizes += req->mip_levels[i];
Li Qiange7e11f92017-03-27 20:10:53 -0700720 }
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000721
Murray McAllister36274ab2017-03-27 11:12:53 +0200722 if (num_sizes > DRM_VMW_MAX_SURFACE_FACES * DRM_VMW_MAX_MIP_LEVELS ||
723 num_sizes == 0)
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000724 return -EINVAL;
725
726 size = vmw_user_surface_size + 128 +
727 ttm_round_pot(num_sizes * sizeof(struct drm_vmw_size)) +
728 ttm_round_pot(num_sizes * sizeof(struct vmw_surface_offset));
729
730
Thomas Hellstrom7e8d9da2012-11-20 12:19:37 +0000731 desc = svga3dsurface_get_desc(req->format);
732 if (unlikely(desc->block_desc == SVGA3DBLOCKDESC_NONE)) {
733 DRM_ERROR("Invalid surface format for surface creation.\n");
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700734 DRM_ERROR("Format requested is: %d\n", req->format);
Thomas Hellstrom7e8d9da2012-11-20 12:19:37 +0000735 return -EINVAL;
736 }
737
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100738 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000739 if (unlikely(ret != 0))
740 return ret;
741
742 ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
743 size, false, true);
744 if (unlikely(ret != 0)) {
745 if (ret != -ERESTARTSYS)
746 DRM_ERROR("Out of graphics memory for surface"
747 " creation.\n");
748 goto out_unlock;
749 }
750
751 user_srf = kzalloc(sizeof(*user_srf), GFP_KERNEL);
Markus Elfring862f6152016-09-23 17:53:49 +0200752 if (unlikely(!user_srf)) {
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000753 ret = -ENOMEM;
754 goto out_no_user_srf;
755 }
756
757 srf = &user_srf->srf;
758 res = &srf->res;
759
760 srf->flags = req->flags;
761 srf->format = req->format;
762 srf->scanout = req->scanout;
763
764 memcpy(srf->mip_levels, req->mip_levels, sizeof(srf->mip_levels));
765 srf->num_sizes = num_sizes;
766 user_srf->size = size;
Markus Elfringc138d032016-09-23 17:26:02 +0200767 srf->sizes = memdup_user((struct drm_vmw_size __user *)(unsigned long)
768 req->size_addr,
769 sizeof(*srf->sizes) * srf->num_sizes);
770 if (IS_ERR(srf->sizes)) {
771 ret = PTR_ERR(srf->sizes);
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000772 goto out_no_sizes;
773 }
Markus Elfring7ed3b392016-09-22 21:54:33 +0200774 srf->offsets = kmalloc_array(srf->num_sizes,
775 sizeof(*srf->offsets),
776 GFP_KERNEL);
Markus Elfring862f6152016-09-23 17:53:49 +0200777 if (unlikely(!srf->offsets)) {
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000778 ret = -ENOMEM;
779 goto out_no_offsets;
780 }
781
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000782 srf->base_size = *srf->sizes;
783 srf->autogen_filter = SVGA3D_TEX_FILTER_NONE;
Zack Rusin15c6f652012-11-21 12:25:33 +0100784 srf->multisample_count = 0;
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000785
786 cur_bo_offset = 0;
787 cur_offset = srf->offsets;
788 cur_size = srf->sizes;
789
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000790 for (i = 0; i < DRM_VMW_MAX_SURFACE_FACES; ++i) {
791 for (j = 0; j < srf->mip_levels[i]; ++j) {
Thomas Hellstrom7e8d9da2012-11-20 12:19:37 +0000792 uint32_t stride = svga3dsurface_calculate_pitch
793 (desc, cur_size);
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000794
795 cur_offset->face = i;
796 cur_offset->mip = j;
797 cur_offset->bo_offset = cur_bo_offset;
Thomas Hellstrom7e8d9da2012-11-20 12:19:37 +0000798 cur_bo_offset += svga3dsurface_get_image_buffer_size
799 (desc, cur_size, stride);
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000800 ++cur_offset;
801 ++cur_size;
802 }
803 }
804 res->backup_size = cur_bo_offset;
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000805 if (srf->scanout &&
806 srf->num_sizes == 1 &&
807 srf->sizes[0].width == 64 &&
808 srf->sizes[0].height == 64 &&
809 srf->format == SVGA3D_A8R8G8B8) {
810
Rasmus Villemoes9e266122015-12-15 12:20:54 +0100811 srf->snooper.image = kzalloc(64 * 64 * 4, GFP_KERNEL);
812 if (!srf->snooper.image) {
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000813 DRM_ERROR("Failed to allocate cursor_image\n");
814 ret = -ENOMEM;
815 goto out_no_copy;
816 }
817 } else {
818 srf->snooper.image = NULL;
819 }
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000820
Thomas Hellstrom79e5f812013-11-08 02:12:51 -0800821 user_srf->prime.base.shareable = false;
822 user_srf->prime.base.tfile = NULL;
Thomas Hellstrom6d10aab2014-03-19 10:45:11 +0100823 if (drm_is_primary_client(file_priv))
824 user_srf->master = drm_master_get(file_priv->master);
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000825
826 /**
827 * From this point, the generic resource management functions
828 * destroy the object on failure.
829 */
830
831 ret = vmw_surface_init(dev_priv, srf, vmw_user_surface_free);
832 if (unlikely(ret != 0))
833 goto out_unlock;
834
Thomas Hellstrom4b0c8252014-03-12 20:42:31 +0100835 /*
836 * A gb-aware client referencing a shared surface will
837 * expect a backup buffer to be present.
838 */
839 if (dev_priv->has_mob && req->shareable) {
840 uint32_t backup_handle;
841
842 ret = vmw_user_dmabuf_alloc(dev_priv, tfile,
843 res->backup_size,
844 true,
845 &backup_handle,
Thomas Hellstrom54c12bc2015-09-14 01:13:11 -0700846 &res->backup,
847 &user_srf->backup_base);
Thomas Hellstrom4b0c8252014-03-12 20:42:31 +0100848 if (unlikely(ret != 0)) {
849 vmw_resource_unreference(&res);
850 goto out_unlock;
851 }
852 }
853
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000854 tmp = vmw_resource_reference(&srf->res);
Thomas Hellstrom79e5f812013-11-08 02:12:51 -0800855 ret = ttm_prime_object_init(tfile, res->backup_size, &user_srf->prime,
856 req->shareable, VMW_RES_SURFACE,
857 &vmw_user_surface_base_release, NULL);
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000858
859 if (unlikely(ret != 0)) {
860 vmw_resource_unreference(&tmp);
861 vmw_resource_unreference(&res);
862 goto out_unlock;
863 }
864
Thomas Hellstrom79e5f812013-11-08 02:12:51 -0800865 rep->sid = user_srf->prime.base.hash.key;
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000866 vmw_resource_unreference(&res);
867
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100868 ttm_read_unlock(&dev_priv->reservation_sem);
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000869 return 0;
870out_no_copy:
871 kfree(srf->offsets);
872out_no_offsets:
873 kfree(srf->sizes);
874out_no_sizes:
Thomas Hellstrom79e5f812013-11-08 02:12:51 -0800875 ttm_prime_object_kfree(user_srf, prime);
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000876out_no_user_srf:
877 ttm_mem_global_free(vmw_mem_glob(dev_priv), size);
878out_unlock:
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100879 ttm_read_unlock(&dev_priv->reservation_sem);
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000880 return ret;
881}
882
Thomas Hellstromadebcb22014-03-18 15:00:56 +0100883
884static int
885vmw_surface_handle_reference(struct vmw_private *dev_priv,
886 struct drm_file *file_priv,
887 uint32_t u_handle,
888 enum drm_vmw_handle_type handle_type,
889 struct ttm_base_object **base_p)
890{
891 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
Thomas Hellstrom6d10aab2014-03-19 10:45:11 +0100892 struct vmw_user_surface *user_srf;
Thomas Hellstromadebcb22014-03-18 15:00:56 +0100893 uint32_t handle;
894 struct ttm_base_object *base;
895 int ret;
Thomas Hellstromfe25deb2017-03-27 11:21:25 +0200896 bool require_exist = false;
Thomas Hellstromadebcb22014-03-18 15:00:56 +0100897
898 if (handle_type == DRM_VMW_HANDLE_PRIME) {
899 ret = ttm_prime_fd_to_handle(tfile, u_handle, &handle);
900 if (unlikely(ret != 0))
901 return ret;
902 } else {
Thomas Hellstromfe25deb2017-03-27 11:21:25 +0200903 if (unlikely(drm_is_render_client(file_priv)))
904 require_exist = true;
905
Thomas Hellstromaa3469c2015-08-27 10:06:24 -0700906 if (ACCESS_ONCE(vmw_fpriv(file_priv)->locked_master)) {
907 DRM_ERROR("Locked master refused legacy "
908 "surface reference.\n");
909 return -EACCES;
910 }
911
Thomas Hellstromadebcb22014-03-18 15:00:56 +0100912 handle = u_handle;
913 }
914
915 ret = -EINVAL;
916 base = ttm_base_object_lookup_for_ref(dev_priv->tdev, handle);
Markus Elfring862f6152016-09-23 17:53:49 +0200917 if (unlikely(!base)) {
Thomas Hellstromadebcb22014-03-18 15:00:56 +0100918 DRM_ERROR("Could not find surface to reference.\n");
919 goto out_no_lookup;
920 }
921
922 if (unlikely(ttm_base_object_type(base) != VMW_RES_SURFACE)) {
923 DRM_ERROR("Referenced object is not a surface.\n");
924 goto out_bad_resource;
925 }
926
927 if (handle_type != DRM_VMW_HANDLE_PRIME) {
Thomas Hellstrom6d10aab2014-03-19 10:45:11 +0100928 user_srf = container_of(base, struct vmw_user_surface,
929 prime.base);
930
931 /*
932 * Make sure the surface creator has the same
Thomas Hellstromfe25deb2017-03-27 11:21:25 +0200933 * authenticating master, or is already registered with us.
Thomas Hellstrom6d10aab2014-03-19 10:45:11 +0100934 */
935 if (drm_is_primary_client(file_priv) &&
Thomas Hellstromfe25deb2017-03-27 11:21:25 +0200936 user_srf->master != file_priv->master)
937 require_exist = true;
Thomas Hellstrom6d10aab2014-03-19 10:45:11 +0100938
Thomas Hellstromfe25deb2017-03-27 11:21:25 +0200939 ret = ttm_ref_object_add(tfile, base, TTM_REF_USAGE, NULL,
940 require_exist);
Thomas Hellstromadebcb22014-03-18 15:00:56 +0100941 if (unlikely(ret != 0)) {
942 DRM_ERROR("Could not add a reference to a surface.\n");
943 goto out_bad_resource;
944 }
945 }
946
947 *base_p = base;
948 return 0;
949
950out_bad_resource:
951 ttm_base_object_unref(&base);
952out_no_lookup:
953 if (handle_type == DRM_VMW_HANDLE_PRIME)
954 (void) ttm_ref_object_base_unref(tfile, handle, TTM_REF_USAGE);
955
956 return ret;
957}
958
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000959/**
960 * vmw_user_surface_define_ioctl - Ioctl function implementing
961 * the user surface reference functionality.
962 *
963 * @dev: Pointer to a struct drm_device.
964 * @data: Pointer to data copied from / to user-space.
965 * @file_priv: Pointer to a drm file private structure.
966 */
967int vmw_surface_reference_ioctl(struct drm_device *dev, void *data,
968 struct drm_file *file_priv)
969{
Thomas Hellstrom05efb1a2013-12-18 14:13:29 +0100970 struct vmw_private *dev_priv = vmw_priv(dev);
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000971 union drm_vmw_surface_reference_arg *arg =
972 (union drm_vmw_surface_reference_arg *)data;
973 struct drm_vmw_surface_arg *req = &arg->req;
974 struct drm_vmw_surface_create_req *rep = &arg->rep;
975 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
976 struct vmw_surface *srf;
977 struct vmw_user_surface *user_srf;
978 struct drm_vmw_size __user *user_sizes;
979 struct ttm_base_object *base;
Thomas Hellstromadebcb22014-03-18 15:00:56 +0100980 int ret;
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000981
Thomas Hellstromadebcb22014-03-18 15:00:56 +0100982 ret = vmw_surface_handle_reference(dev_priv, file_priv, req->sid,
983 req->handle_type, &base);
984 if (unlikely(ret != 0))
985 return ret;
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000986
Thomas Hellstrom79e5f812013-11-08 02:12:51 -0800987 user_srf = container_of(base, struct vmw_user_surface, prime.base);
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000988 srf = &user_srf->srf;
989
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000990 rep->flags = srf->flags;
991 rep->format = srf->format;
992 memcpy(rep->mip_levels, srf->mip_levels, sizeof(srf->mip_levels));
993 user_sizes = (struct drm_vmw_size __user *)(unsigned long)
994 rep->size_addr;
995
996 if (user_sizes)
Thomas Hellstromc1a21372014-01-30 11:18:38 +0100997 ret = copy_to_user(user_sizes, &srf->base_size,
998 sizeof(srf->base_size));
Thomas Hellstrom543831c2012-11-20 12:19:36 +0000999 if (unlikely(ret != 0)) {
1000 DRM_ERROR("copy_to_user failed %p %u\n",
1001 user_sizes, srf->num_sizes);
Thomas Hellstromadebcb22014-03-18 15:00:56 +01001002 ttm_ref_object_base_unref(tfile, base->hash.key, TTM_REF_USAGE);
Thomas Hellstrom543831c2012-11-20 12:19:36 +00001003 ret = -EFAULT;
1004 }
Thomas Hellstromadebcb22014-03-18 15:00:56 +01001005
Thomas Hellstrom543831c2012-11-20 12:19:36 +00001006 ttm_base_object_unref(&base);
1007
1008 return ret;
1009}
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001010
1011/**
1012 * vmw_surface_define_encode - Encode a surface_define command.
1013 *
1014 * @srf: Pointer to a struct vmw_surface object.
1015 * @cmd_space: Pointer to memory area in which the commands should be encoded.
1016 */
1017static int vmw_gb_surface_create(struct vmw_resource *res)
1018{
1019 struct vmw_private *dev_priv = res->dev_priv;
1020 struct vmw_surface *srf = vmw_res_to_srf(res);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001021 uint32_t cmd_len, cmd_id, submit_len;
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001022 int ret;
1023 struct {
1024 SVGA3dCmdHeader header;
1025 SVGA3dCmdDefineGBSurface body;
1026 } *cmd;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001027 struct {
1028 SVGA3dCmdHeader header;
1029 SVGA3dCmdDefineGBSurface_v2 body;
1030 } *cmd2;
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001031
1032 if (likely(res->id != -1))
1033 return 0;
1034
Thomas Hellstrom153b3d52015-06-25 10:47:43 -07001035 vmw_fifo_resource_inc(dev_priv);
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001036 ret = vmw_resource_alloc_id(res);
1037 if (unlikely(ret != 0)) {
1038 DRM_ERROR("Failed to allocate a surface id.\n");
1039 goto out_no_id;
1040 }
1041
1042 if (unlikely(res->id >= VMWGFX_NUM_GB_SURFACE)) {
1043 ret = -EBUSY;
1044 goto out_no_fifo;
1045 }
1046
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001047 if (srf->array_size > 0) {
1048 /* has_dx checked on creation time. */
1049 cmd_id = SVGA_3D_CMD_DEFINE_GB_SURFACE_V2;
1050 cmd_len = sizeof(cmd2->body);
1051 submit_len = sizeof(*cmd2);
1052 } else {
1053 cmd_id = SVGA_3D_CMD_DEFINE_GB_SURFACE;
1054 cmd_len = sizeof(cmd->body);
1055 submit_len = sizeof(*cmd);
1056 }
1057
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001058 cmd = vmw_fifo_reserve(dev_priv, submit_len);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001059 cmd2 = (typeof(cmd2))cmd;
Markus Elfring862f6152016-09-23 17:53:49 +02001060 if (unlikely(!cmd)) {
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001061 DRM_ERROR("Failed reserving FIFO space for surface "
1062 "creation.\n");
1063 ret = -ENOMEM;
1064 goto out_no_fifo;
1065 }
1066
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001067 if (srf->array_size > 0) {
1068 cmd2->header.id = cmd_id;
1069 cmd2->header.size = cmd_len;
1070 cmd2->body.sid = srf->res.id;
1071 cmd2->body.surfaceFlags = srf->flags;
1072 cmd2->body.format = cpu_to_le32(srf->format);
1073 cmd2->body.numMipLevels = srf->mip_levels[0];
1074 cmd2->body.multisampleCount = srf->multisample_count;
1075 cmd2->body.autogenFilter = srf->autogen_filter;
1076 cmd2->body.size.width = srf->base_size.width;
1077 cmd2->body.size.height = srf->base_size.height;
1078 cmd2->body.size.depth = srf->base_size.depth;
1079 cmd2->body.arraySize = srf->array_size;
1080 } else {
1081 cmd->header.id = cmd_id;
1082 cmd->header.size = cmd_len;
1083 cmd->body.sid = srf->res.id;
1084 cmd->body.surfaceFlags = srf->flags;
1085 cmd->body.format = cpu_to_le32(srf->format);
1086 cmd->body.numMipLevels = srf->mip_levels[0];
1087 cmd->body.multisampleCount = srf->multisample_count;
1088 cmd->body.autogenFilter = srf->autogen_filter;
1089 cmd->body.size.width = srf->base_size.width;
1090 cmd->body.size.height = srf->base_size.height;
1091 cmd->body.size.depth = srf->base_size.depth;
1092 }
1093
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001094 vmw_fifo_commit(dev_priv, submit_len);
1095
1096 return 0;
1097
1098out_no_fifo:
1099 vmw_resource_release_id(res);
1100out_no_id:
Thomas Hellstrom153b3d52015-06-25 10:47:43 -07001101 vmw_fifo_resource_dec(dev_priv);
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001102 return ret;
1103}
1104
1105
1106static int vmw_gb_surface_bind(struct vmw_resource *res,
1107 struct ttm_validate_buffer *val_buf)
1108{
1109 struct vmw_private *dev_priv = res->dev_priv;
1110 struct {
1111 SVGA3dCmdHeader header;
1112 SVGA3dCmdBindGBSurface body;
1113 } *cmd1;
1114 struct {
1115 SVGA3dCmdHeader header;
1116 SVGA3dCmdUpdateGBSurface body;
1117 } *cmd2;
1118 uint32_t submit_size;
1119 struct ttm_buffer_object *bo = val_buf->bo;
1120
1121 BUG_ON(bo->mem.mem_type != VMW_PL_MOB);
1122
1123 submit_size = sizeof(*cmd1) + (res->backup_dirty ? sizeof(*cmd2) : 0);
1124
1125 cmd1 = vmw_fifo_reserve(dev_priv, submit_size);
Markus Elfring862f6152016-09-23 17:53:49 +02001126 if (unlikely(!cmd1)) {
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001127 DRM_ERROR("Failed reserving FIFO space for surface "
1128 "binding.\n");
1129 return -ENOMEM;
1130 }
1131
1132 cmd1->header.id = SVGA_3D_CMD_BIND_GB_SURFACE;
1133 cmd1->header.size = sizeof(cmd1->body);
1134 cmd1->body.sid = res->id;
1135 cmd1->body.mobid = bo->mem.start;
1136 if (res->backup_dirty) {
1137 cmd2 = (void *) &cmd1[1];
1138 cmd2->header.id = SVGA_3D_CMD_UPDATE_GB_SURFACE;
1139 cmd2->header.size = sizeof(cmd2->body);
1140 cmd2->body.sid = res->id;
1141 res->backup_dirty = false;
1142 }
1143 vmw_fifo_commit(dev_priv, submit_size);
1144
1145 return 0;
1146}
1147
1148static int vmw_gb_surface_unbind(struct vmw_resource *res,
1149 bool readback,
1150 struct ttm_validate_buffer *val_buf)
1151{
1152 struct vmw_private *dev_priv = res->dev_priv;
1153 struct ttm_buffer_object *bo = val_buf->bo;
1154 struct vmw_fence_obj *fence;
1155
1156 struct {
1157 SVGA3dCmdHeader header;
1158 SVGA3dCmdReadbackGBSurface body;
1159 } *cmd1;
1160 struct {
1161 SVGA3dCmdHeader header;
Jakob Bornecrantz1985f992014-01-17 09:12:26 +01001162 SVGA3dCmdInvalidateGBSurface body;
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001163 } *cmd2;
Jakob Bornecrantz1985f992014-01-17 09:12:26 +01001164 struct {
1165 SVGA3dCmdHeader header;
1166 SVGA3dCmdBindGBSurface body;
1167 } *cmd3;
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001168 uint32_t submit_size;
1169 uint8_t *cmd;
1170
1171
1172 BUG_ON(bo->mem.mem_type != VMW_PL_MOB);
1173
Jakob Bornecrantz1985f992014-01-17 09:12:26 +01001174 submit_size = sizeof(*cmd3) + (readback ? sizeof(*cmd1) : sizeof(*cmd2));
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001175 cmd = vmw_fifo_reserve(dev_priv, submit_size);
Markus Elfring862f6152016-09-23 17:53:49 +02001176 if (unlikely(!cmd)) {
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001177 DRM_ERROR("Failed reserving FIFO space for surface "
1178 "unbinding.\n");
1179 return -ENOMEM;
1180 }
1181
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001182 if (readback) {
1183 cmd1 = (void *) cmd;
1184 cmd1->header.id = SVGA_3D_CMD_READBACK_GB_SURFACE;
1185 cmd1->header.size = sizeof(cmd1->body);
1186 cmd1->body.sid = res->id;
Jakob Bornecrantz1985f992014-01-17 09:12:26 +01001187 cmd3 = (void *) &cmd1[1];
1188 } else {
1189 cmd2 = (void *) cmd;
1190 cmd2->header.id = SVGA_3D_CMD_INVALIDATE_GB_SURFACE;
1191 cmd2->header.size = sizeof(cmd2->body);
1192 cmd2->body.sid = res->id;
1193 cmd3 = (void *) &cmd2[1];
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001194 }
Jakob Bornecrantz1985f992014-01-17 09:12:26 +01001195
1196 cmd3->header.id = SVGA_3D_CMD_BIND_GB_SURFACE;
1197 cmd3->header.size = sizeof(cmd3->body);
1198 cmd3->body.sid = res->id;
1199 cmd3->body.mobid = SVGA3D_INVALID_ID;
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001200
1201 vmw_fifo_commit(dev_priv, submit_size);
1202
1203 /*
1204 * Create a fence object and fence the backup buffer.
1205 */
1206
1207 (void) vmw_execbuf_fence_commands(NULL, dev_priv,
1208 &fence, NULL);
1209
1210 vmw_fence_single_bo(val_buf->bo, fence);
1211
1212 if (likely(fence != NULL))
1213 vmw_fence_obj_unreference(&fence);
1214
1215 return 0;
1216}
1217
1218static int vmw_gb_surface_destroy(struct vmw_resource *res)
1219{
1220 struct vmw_private *dev_priv = res->dev_priv;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001221 struct vmw_surface *srf = vmw_res_to_srf(res);
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001222 struct {
1223 SVGA3dCmdHeader header;
1224 SVGA3dCmdDestroyGBSurface body;
1225 } *cmd;
1226
1227 if (likely(res->id == -1))
1228 return 0;
1229
Thomas Hellstrom173fb7d2013-10-08 02:32:36 -07001230 mutex_lock(&dev_priv->binding_mutex);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001231 vmw_view_surface_list_destroy(dev_priv, &srf->view_list);
1232 vmw_binding_res_list_scrub(&res->binding_head);
Thomas Hellstrom173fb7d2013-10-08 02:32:36 -07001233
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001234 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
Markus Elfring862f6152016-09-23 17:53:49 +02001235 if (unlikely(!cmd)) {
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001236 DRM_ERROR("Failed reserving FIFO space for surface "
1237 "destruction.\n");
Thomas Hellstrom3e894a62014-01-20 11:33:04 +01001238 mutex_unlock(&dev_priv->binding_mutex);
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001239 return -ENOMEM;
1240 }
1241
1242 cmd->header.id = SVGA_3D_CMD_DESTROY_GB_SURFACE;
1243 cmd->header.size = sizeof(cmd->body);
1244 cmd->body.sid = res->id;
1245 vmw_fifo_commit(dev_priv, sizeof(*cmd));
Thomas Hellstrom173fb7d2013-10-08 02:32:36 -07001246 mutex_unlock(&dev_priv->binding_mutex);
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001247 vmw_resource_release_id(res);
Thomas Hellstrom153b3d52015-06-25 10:47:43 -07001248 vmw_fifo_resource_dec(dev_priv);
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001249
1250 return 0;
1251}
1252
Sinclair Yeh233826a2015-03-05 01:06:13 -08001253
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001254/**
1255 * vmw_gb_surface_define_ioctl - Ioctl function implementing
1256 * the user surface define functionality.
1257 *
1258 * @dev: Pointer to a struct drm_device.
1259 * @data: Pointer to data copied from / to user-space.
1260 * @file_priv: Pointer to a drm file private structure.
1261 */
1262int vmw_gb_surface_define_ioctl(struct drm_device *dev, void *data,
1263 struct drm_file *file_priv)
1264{
1265 struct vmw_private *dev_priv = vmw_priv(dev);
1266 struct vmw_user_surface *user_srf;
1267 struct vmw_surface *srf;
1268 struct vmw_resource *res;
1269 struct vmw_resource *tmp;
1270 union drm_vmw_gb_surface_create_arg *arg =
1271 (union drm_vmw_gb_surface_create_arg *)data;
1272 struct drm_vmw_gb_surface_create_req *req = &arg->req;
1273 struct drm_vmw_gb_surface_create_rep *rep = &arg->rep;
1274 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1275 int ret;
1276 uint32_t size;
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001277 uint32_t backup_handle;
1278
Thomas Hellstrom53c1e532015-10-26 04:42:31 -07001279 if (req->multisample_count != 0)
1280 return -EINVAL;
Sinclair Yeh233826a2015-03-05 01:06:13 -08001281
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001282 if (unlikely(vmw_user_surface_size == 0))
1283 vmw_user_surface_size = ttm_round_pot(sizeof(*user_srf)) +
1284 128;
1285
1286 size = vmw_user_surface_size + 128;
1287
Sinclair Yeh233826a2015-03-05 01:06:13 -08001288 /* Define a surface based on the parameters. */
1289 ret = vmw_surface_gb_priv_define(dev,
1290 size,
1291 req->svga3d_flags,
1292 req->format,
1293 req->drm_surface_flags & drm_vmw_surface_flag_scanout,
1294 req->mip_levels,
1295 req->multisample_count,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001296 req->array_size,
Sinclair Yeh233826a2015-03-05 01:06:13 -08001297 req->base_size,
1298 &srf);
1299 if (unlikely(ret != 0))
1300 return ret;
1301
1302 user_srf = container_of(srf, struct vmw_user_surface, srf);
1303 if (drm_is_primary_client(file_priv))
1304 user_srf->master = drm_master_get(file_priv->master);
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001305
Thomas Hellstrom294adf72014-02-27 12:34:51 +01001306 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001307 if (unlikely(ret != 0))
1308 return ret;
1309
Sinclair Yeh233826a2015-03-05 01:06:13 -08001310 res = &user_srf->srf.res;
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001311
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001312
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001313 if (req->buffer_handle != SVGA3D_INVALID_ID) {
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001314 ret = vmw_user_dmabuf_lookup(tfile, req->buffer_handle,
Thomas Hellstrom54c12bc2015-09-14 01:13:11 -07001315 &res->backup,
1316 &user_srf->backup_base);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001317 if (ret == 0 && res->backup->base.num_pages * PAGE_SIZE <
1318 res->backup_size) {
1319 DRM_ERROR("Surface backup buffer is too small.\n");
1320 vmw_dmabuf_unreference(&res->backup);
1321 ret = -EINVAL;
1322 goto out_unlock;
1323 }
1324 } else if (req->drm_surface_flags & drm_vmw_surface_flag_create_buffer)
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001325 ret = vmw_user_dmabuf_alloc(dev_priv, tfile,
1326 res->backup_size,
1327 req->drm_surface_flags &
1328 drm_vmw_surface_flag_shareable,
1329 &backup_handle,
Thomas Hellstrom54c12bc2015-09-14 01:13:11 -07001330 &res->backup,
1331 &user_srf->backup_base);
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001332
1333 if (unlikely(ret != 0)) {
1334 vmw_resource_unreference(&res);
1335 goto out_unlock;
1336 }
1337
Sinclair Yeh233826a2015-03-05 01:06:13 -08001338 tmp = vmw_resource_reference(res);
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001339 ret = ttm_prime_object_init(tfile, res->backup_size, &user_srf->prime,
1340 req->drm_surface_flags &
1341 drm_vmw_surface_flag_shareable,
1342 VMW_RES_SURFACE,
1343 &vmw_user_surface_base_release, NULL);
1344
1345 if (unlikely(ret != 0)) {
1346 vmw_resource_unreference(&tmp);
1347 vmw_resource_unreference(&res);
1348 goto out_unlock;
1349 }
1350
Sinclair Yeh233826a2015-03-05 01:06:13 -08001351 rep->handle = user_srf->prime.base.hash.key;
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001352 rep->backup_size = res->backup_size;
1353 if (res->backup) {
1354 rep->buffer_map_handle =
1355 drm_vma_node_offset_addr(&res->backup->base.vma_node);
1356 rep->buffer_size = res->backup->base.num_pages * PAGE_SIZE;
1357 rep->buffer_handle = backup_handle;
1358 } else {
1359 rep->buffer_map_handle = 0;
1360 rep->buffer_size = 0;
1361 rep->buffer_handle = SVGA3D_INVALID_ID;
1362 }
1363
1364 vmw_resource_unreference(&res);
1365
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001366out_unlock:
Thomas Hellstrom294adf72014-02-27 12:34:51 +01001367 ttm_read_unlock(&dev_priv->reservation_sem);
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001368 return ret;
1369}
1370
1371/**
1372 * vmw_gb_surface_reference_ioctl - Ioctl function implementing
1373 * the user surface reference functionality.
1374 *
1375 * @dev: Pointer to a struct drm_device.
1376 * @data: Pointer to data copied from / to user-space.
1377 * @file_priv: Pointer to a drm file private structure.
1378 */
1379int vmw_gb_surface_reference_ioctl(struct drm_device *dev, void *data,
1380 struct drm_file *file_priv)
1381{
1382 struct vmw_private *dev_priv = vmw_priv(dev);
1383 union drm_vmw_gb_surface_reference_arg *arg =
1384 (union drm_vmw_gb_surface_reference_arg *)data;
1385 struct drm_vmw_surface_arg *req = &arg->req;
1386 struct drm_vmw_gb_surface_ref_rep *rep = &arg->rep;
1387 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1388 struct vmw_surface *srf;
1389 struct vmw_user_surface *user_srf;
1390 struct ttm_base_object *base;
1391 uint32_t backup_handle;
1392 int ret = -EINVAL;
1393
Thomas Hellstromadebcb22014-03-18 15:00:56 +01001394 ret = vmw_surface_handle_reference(dev_priv, file_priv, req->sid,
1395 req->handle_type, &base);
1396 if (unlikely(ret != 0))
1397 return ret;
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001398
1399 user_srf = container_of(base, struct vmw_user_surface, prime.base);
1400 srf = &user_srf->srf;
Markus Elfring862f6152016-09-23 17:53:49 +02001401 if (!srf->res.backup) {
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001402 DRM_ERROR("Shared GB surface is missing a backup buffer.\n");
1403 goto out_bad_resource;
1404 }
1405
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001406 mutex_lock(&dev_priv->cmdbuf_mutex); /* Protect res->backup */
1407 ret = vmw_user_dmabuf_reference(tfile, srf->res.backup,
1408 &backup_handle);
1409 mutex_unlock(&dev_priv->cmdbuf_mutex);
1410
1411 if (unlikely(ret != 0)) {
1412 DRM_ERROR("Could not add a reference to a GB surface "
1413 "backup buffer.\n");
Thomas Hellstromadebcb22014-03-18 15:00:56 +01001414 (void) ttm_ref_object_base_unref(tfile, base->hash.key,
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001415 TTM_REF_USAGE);
1416 goto out_bad_resource;
1417 }
1418
1419 rep->creq.svga3d_flags = srf->flags;
1420 rep->creq.format = srf->format;
1421 rep->creq.mip_levels = srf->mip_levels[0];
1422 rep->creq.drm_surface_flags = 0;
1423 rep->creq.multisample_count = srf->multisample_count;
1424 rep->creq.autogen_filter = srf->autogen_filter;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001425 rep->creq.array_size = srf->array_size;
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001426 rep->creq.buffer_handle = backup_handle;
1427 rep->creq.base_size = srf->base_size;
1428 rep->crep.handle = user_srf->prime.base.hash.key;
1429 rep->crep.backup_size = srf->res.backup_size;
1430 rep->crep.buffer_handle = backup_handle;
1431 rep->crep.buffer_map_handle =
1432 drm_vma_node_offset_addr(&srf->res.backup->base.vma_node);
1433 rep->crep.buffer_size = srf->res.backup->base.num_pages * PAGE_SIZE;
1434
1435out_bad_resource:
1436 ttm_base_object_unref(&base);
1437
1438 return ret;
1439}
Sinclair Yeh233826a2015-03-05 01:06:13 -08001440
1441/**
1442 * vmw_surface_gb_priv_define - Define a private GB surface
1443 *
1444 * @dev: Pointer to a struct drm_device
1445 * @user_accounting_size: Used to track user-space memory usage, set
1446 * to 0 for kernel mode only memory
1447 * @svga3d_flags: SVGA3d surface flags for the device
1448 * @format: requested surface format
1449 * @for_scanout: true if inteded to be used for scanout buffer
1450 * @num_mip_levels: number of MIP levels
1451 * @multisample_count:
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001452 * @array_size: Surface array size.
Sinclair Yeh233826a2015-03-05 01:06:13 -08001453 * @size: width, heigh, depth of the surface requested
1454 * @user_srf_out: allocated user_srf. Set to NULL on failure.
1455 *
1456 * GB surfaces allocated by this function will not have a user mode handle, and
1457 * thus will only be visible to vmwgfx. For optimization reasons the
1458 * surface may later be given a user mode handle by another function to make
1459 * it available to user mode drivers.
1460 */
1461int vmw_surface_gb_priv_define(struct drm_device *dev,
1462 uint32_t user_accounting_size,
1463 uint32_t svga3d_flags,
1464 SVGA3dSurfaceFormat format,
1465 bool for_scanout,
1466 uint32_t num_mip_levels,
1467 uint32_t multisample_count,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001468 uint32_t array_size,
Sinclair Yeh233826a2015-03-05 01:06:13 -08001469 struct drm_vmw_size size,
1470 struct vmw_surface **srf_out)
1471{
1472 struct vmw_private *dev_priv = vmw_priv(dev);
1473 struct vmw_user_surface *user_srf;
1474 struct vmw_surface *srf;
1475 int ret;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001476 u32 num_layers;
Sinclair Yeh233826a2015-03-05 01:06:13 -08001477
1478 *srf_out = NULL;
1479
1480 if (for_scanout) {
Sinclair Yeh28c95422017-03-27 11:12:27 -07001481 uint32_t max_width, max_height;
1482
Sinclair Yeh233826a2015-03-05 01:06:13 -08001483 if (!svga3dsurface_is_screen_target_format(format)) {
1484 DRM_ERROR("Invalid Screen Target surface format.");
1485 return -EINVAL;
1486 }
Sinclair Yeh28c95422017-03-27 11:12:27 -07001487
1488 max_width = min(dev_priv->texture_max_width,
1489 dev_priv->stdu_max_width);
1490 max_height = min(dev_priv->texture_max_height,
1491 dev_priv->stdu_max_height);
1492
1493 if (size.width > max_width || size.height > max_height) {
1494 DRM_ERROR("%ux%u\n, exeeds max surface size %ux%u",
1495 size.width, size.height,
1496 max_width, max_height);
1497 return -EINVAL;
1498 }
Sinclair Yeh233826a2015-03-05 01:06:13 -08001499 } else {
1500 const struct svga3d_surface_desc *desc;
1501
1502 desc = svga3dsurface_get_desc(format);
1503 if (unlikely(desc->block_desc == SVGA3DBLOCKDESC_NONE)) {
1504 DRM_ERROR("Invalid surface format.\n");
1505 return -EINVAL;
1506 }
1507 }
1508
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001509 /* array_size must be null for non-GL3 host. */
1510 if (array_size > 0 && !dev_priv->has_dx) {
1511 DRM_ERROR("Tried to create DX surface on non-DX host.\n");
1512 return -EINVAL;
1513 }
1514
Sinclair Yeh233826a2015-03-05 01:06:13 -08001515 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
1516 if (unlikely(ret != 0))
1517 return ret;
1518
1519 ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
1520 user_accounting_size, false, true);
1521 if (unlikely(ret != 0)) {
1522 if (ret != -ERESTARTSYS)
1523 DRM_ERROR("Out of graphics memory for surface"
1524 " creation.\n");
1525 goto out_unlock;
1526 }
1527
1528 user_srf = kzalloc(sizeof(*user_srf), GFP_KERNEL);
Markus Elfring862f6152016-09-23 17:53:49 +02001529 if (unlikely(!user_srf)) {
Sinclair Yeh233826a2015-03-05 01:06:13 -08001530 ret = -ENOMEM;
1531 goto out_no_user_srf;
1532 }
1533
1534 *srf_out = &user_srf->srf;
1535 user_srf->size = user_accounting_size;
1536 user_srf->prime.base.shareable = false;
1537 user_srf->prime.base.tfile = NULL;
1538
1539 srf = &user_srf->srf;
1540 srf->flags = svga3d_flags;
1541 srf->format = format;
1542 srf->scanout = for_scanout;
1543 srf->mip_levels[0] = num_mip_levels;
1544 srf->num_sizes = 1;
1545 srf->sizes = NULL;
1546 srf->offsets = NULL;
1547 srf->base_size = size;
1548 srf->autogen_filter = SVGA3D_TEX_FILTER_NONE;
Charmaine Lee2f633e52015-08-10 10:45:11 -07001549 srf->array_size = array_size;
Sinclair Yeh233826a2015-03-05 01:06:13 -08001550 srf->multisample_count = multisample_count;
1551
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001552 if (array_size)
1553 num_layers = array_size;
1554 else if (svga3d_flags & SVGA3D_SURFACE_CUBEMAP)
1555 num_layers = SVGA3D_MAX_SURFACE_FACES;
1556 else
1557 num_layers = 1;
1558
1559 srf->res.backup_size =
1560 svga3dsurface_get_serialized_size(srf->format,
1561 srf->base_size,
1562 srf->mip_levels[0],
1563 num_layers);
1564
1565 if (srf->flags & SVGA3D_SURFACE_BIND_STREAM_OUTPUT)
1566 srf->res.backup_size += sizeof(SVGA3dDXSOState);
Sinclair Yeh233826a2015-03-05 01:06:13 -08001567
Sinclair Yeh35c05122015-06-26 01:42:06 -07001568 if (dev_priv->active_display_unit == vmw_du_screen_target &&
1569 for_scanout)
1570 srf->flags |= SVGA3D_SURFACE_SCREENTARGET;
1571
Sinclair Yeh233826a2015-03-05 01:06:13 -08001572 /*
1573 * From this point, the generic resource management functions
1574 * destroy the object on failure.
1575 */
1576 ret = vmw_surface_init(dev_priv, srf, vmw_user_surface_free);
1577
1578 ttm_read_unlock(&dev_priv->reservation_sem);
1579 return ret;
1580
1581out_no_user_srf:
1582 ttm_mem_global_free(vmw_mem_glob(dev_priv), user_accounting_size);
1583
1584out_unlock:
1585 ttm_read_unlock(&dev_priv->reservation_sem);
1586 return ret;
1587}