1 /**************************************************************************
3 * Copyright © 2009-2015 VMware, Inc., Palo Alto, CA., USA
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "vmwgfx_drv.h"
29 #include "vmwgfx_resource_priv.h"
30 #include "vmwgfx_so.h"
31 #include "vmwgfx_binding.h"
32 #include <ttm/ttm_placement.h>
33 #include "device_include/svga3d_surfacedefs.h"
37 * struct vmw_user_surface - User-space visible surface resource
39 * @base: The TTM base object handling user-space visibility.
40 * @srf: The surface metadata.
41 * @size: TTM accounting size for the surface.
42 * @master: master of the creating client. Used for security check.
44 struct vmw_user_surface
{
45 struct ttm_prime_object prime
;
46 struct vmw_surface srf
;
48 struct drm_master
*master
;
49 struct ttm_base_object
*backup_base
;
53 * struct vmw_surface_offset - Backing store mip level offset info
55 * @face: Surface face.
57 * @bo_offset: Offset into backing store of this mip level.
60 struct vmw_surface_offset
{
66 static void vmw_user_surface_free(struct vmw_resource
*res
);
67 static struct vmw_resource
*
68 vmw_user_surface_base_to_res(struct ttm_base_object
*base
);
69 static int vmw_legacy_srf_bind(struct vmw_resource
*res
,
70 struct ttm_validate_buffer
*val_buf
);
71 static int vmw_legacy_srf_unbind(struct vmw_resource
*res
,
73 struct ttm_validate_buffer
*val_buf
);
74 static int vmw_legacy_srf_create(struct vmw_resource
*res
);
75 static int vmw_legacy_srf_destroy(struct vmw_resource
*res
);
76 static int vmw_gb_surface_create(struct vmw_resource
*res
);
77 static int vmw_gb_surface_bind(struct vmw_resource
*res
,
78 struct ttm_validate_buffer
*val_buf
);
79 static int vmw_gb_surface_unbind(struct vmw_resource
*res
,
81 struct ttm_validate_buffer
*val_buf
);
82 static int vmw_gb_surface_destroy(struct vmw_resource
*res
);
85 static 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
91 const struct vmw_user_resource_conv
*user_surface_converter
=
95 static uint64_t vmw_user_surface_size
;
97 static const struct vmw_res_func vmw_legacy_surface_func
= {
98 .res_type
= vmw_res_surface
,
99 .needs_backup
= false,
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
109 static const struct vmw_res_func vmw_gb_surface_func
= {
110 .res_type
= vmw_res_surface
,
111 .needs_backup
= 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
122 * struct vmw_surface_dma - SVGA3D DMA command
124 struct vmw_surface_dma
{
125 SVGA3dCmdHeader header
;
126 SVGA3dCmdSurfaceDMA body
;
128 SVGA3dCmdSurfaceDMASuffix suffix
;
132 * struct vmw_surface_define - SVGA3D Surface Define command
134 struct vmw_surface_define
{
135 SVGA3dCmdHeader header
;
136 SVGA3dCmdDefineSurface body
;
140 * struct vmw_surface_destroy - SVGA3D Surface Destroy command
142 struct vmw_surface_destroy
{
143 SVGA3dCmdHeader header
;
144 SVGA3dCmdDestroySurface body
;
149 * vmw_surface_dma_size - Compute fifo size for a dma command.
151 * @srf: Pointer to a struct vmw_surface
153 * Computes the required size for a surface dma command for backup or
154 * restoration of the surface represented by @srf.
156 static inline uint32_t vmw_surface_dma_size(const struct vmw_surface
*srf
)
158 return srf
->num_sizes
* sizeof(struct vmw_surface_dma
);
163 * vmw_surface_define_size - Compute fifo size for a surface define command.
165 * @srf: Pointer to a struct vmw_surface
167 * Computes the required size for a surface define command for the definition
168 * of the surface represented by @srf.
170 static inline uint32_t vmw_surface_define_size(const struct vmw_surface
*srf
)
172 return sizeof(struct vmw_surface_define
) + srf
->num_sizes
*
178 * vmw_surface_destroy_size - Compute fifo size for a surface destroy command.
180 * Computes the required size for a surface destroy command for the destruction
183 static inline uint32_t vmw_surface_destroy_size(void)
185 return sizeof(struct vmw_surface_destroy
);
189 * vmw_surface_destroy_encode - Encode a surface_destroy command.
191 * @id: The surface id
192 * @cmd_space: Pointer to memory area in which the commands should be encoded.
194 static void vmw_surface_destroy_encode(uint32_t id
,
197 struct vmw_surface_destroy
*cmd
= (struct vmw_surface_destroy
*)
200 cmd
->header
.id
= SVGA_3D_CMD_SURFACE_DESTROY
;
201 cmd
->header
.size
= sizeof(cmd
->body
);
206 * vmw_surface_define_encode - Encode a surface_define command.
208 * @srf: Pointer to a struct vmw_surface object.
209 * @cmd_space: Pointer to memory area in which the commands should be encoded.
211 static void vmw_surface_define_encode(const struct vmw_surface
*srf
,
214 struct vmw_surface_define
*cmd
= (struct vmw_surface_define
*)
216 struct drm_vmw_size
*src_size
;
217 SVGA3dSize
*cmd_size
;
221 cmd_len
= sizeof(cmd
->body
) + srf
->num_sizes
* sizeof(SVGA3dSize
);
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
;
227 cmd
->body
.format
= srf
->format
;
228 for (i
= 0; i
< DRM_VMW_MAX_SURFACE_FACES
; ++i
)
229 cmd
->body
.face
[i
].numMipLevels
= srf
->mip_levels
[i
];
232 cmd_size
= (SVGA3dSize
*) cmd
;
233 src_size
= srf
->sizes
;
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
;
243 * vmw_surface_dma_encode - Encode a surface_dma command.
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.
251 static void vmw_surface_dma_encode(struct vmw_surface
*srf
,
253 const SVGAGuestPtr
*ptr
,
257 struct vmw_surface_dma
*cmd
= (struct vmw_surface_dma
*)cmd_space
;
258 const struct svga3d_surface_desc
*desc
=
259 svga3dsurface_get_desc(srf
->format
);
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
];
269 header
->id
= SVGA_3D_CMD_SURFACE_DMA
;
270 header
->size
= sizeof(*body
) + sizeof(*cb
) + sizeof(*suffix
);
272 body
->guest
.ptr
= *ptr
;
273 body
->guest
.ptr
.offset
+= cur_offset
->bo_offset
;
274 body
->guest
.pitch
= svga3dsurface_calculate_pitch(desc
,
276 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
);
287 cb
->w
= cur_size
->width
;
288 cb
->h
= cur_size
->height
;
289 cb
->d
= cur_size
->depth
;
291 suffix
->suffixSize
= sizeof(*suffix
);
292 suffix
->maximumOffset
=
293 svga3dsurface_get_image_buffer_size(desc
, cur_size
,
295 suffix
->flags
.discard
= 0;
296 suffix
->flags
.unsynchronized
= 0;
297 suffix
->flags
.reserved
= 0;
304 * vmw_hw_surface_destroy - destroy a Device surface
306 * @res: Pointer to a struct vmw_resource embedded in a struct
309 * Destroys a the device surface associated with a struct vmw_surface if
310 * any, and adjusts accounting and resource count accordingly.
312 static void vmw_hw_surface_destroy(struct vmw_resource
*res
)
315 struct vmw_private
*dev_priv
= res
->dev_priv
;
316 struct vmw_surface
*srf
;
319 if (res
->func
->destroy
== vmw_gb_surface_destroy
) {
320 (void) vmw_gb_surface_destroy(res
);
326 cmd
= vmw_fifo_reserve(dev_priv
, vmw_surface_destroy_size());
327 if (unlikely(!cmd
)) {
328 DRM_ERROR("Failed reserving FIFO space for surface "
333 vmw_surface_destroy_encode(res
->id
, cmd
);
334 vmw_fifo_commit(dev_priv
, vmw_surface_destroy_size());
337 * used_memory_size_atomic, or separate lock
338 * to avoid taking dev_priv::cmdbuf_mutex in
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
);
347 vmw_fifo_resource_dec(dev_priv
);
351 * vmw_legacy_srf_create - Create a device surface as part of the
352 * resource validation process.
354 * @res: Pointer to a struct vmw_surface.
356 * If the surface doesn't have a hw id.
358 * Returns -EBUSY if there wasn't sufficient device resources to
359 * complete the validation. Retry after freeing up resources.
361 * May return other errors if the kernel is out of guest resources.
363 static int vmw_legacy_srf_create(struct vmw_resource
*res
)
365 struct vmw_private
*dev_priv
= res
->dev_priv
;
366 struct vmw_surface
*srf
;
367 uint32_t submit_size
;
371 if (likely(res
->id
!= -1))
374 srf
= vmw_res_to_srf(res
);
375 if (unlikely(dev_priv
->used_memory_size
+ res
->backup_size
>=
376 dev_priv
->memory_size
))
380 * Alloc id for the resource.
383 ret
= vmw_resource_alloc_id(res
);
384 if (unlikely(ret
!= 0)) {
385 DRM_ERROR("Failed to allocate a surface id.\n");
389 if (unlikely(res
->id
>= SVGA3D_MAX_SURFACE_IDS
)) {
395 * Encode surface define- commands.
398 submit_size
= vmw_surface_define_size(srf
);
399 cmd
= vmw_fifo_reserve(dev_priv
, submit_size
);
400 if (unlikely(!cmd
)) {
401 DRM_ERROR("Failed reserving FIFO space for surface "
407 vmw_surface_define_encode(srf
, cmd
);
408 vmw_fifo_commit(dev_priv
, submit_size
);
410 * Surface memory usage accounting.
413 dev_priv
->used_memory_size
+= res
->backup_size
;
417 vmw_resource_release_id(res
);
423 * vmw_legacy_srf_dma - Copy backup data to or from a legacy surface.
425 * @res: Pointer to a struct vmw_res embedded in a struct
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.
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.
438 static int vmw_legacy_srf_dma(struct vmw_resource
*res
,
439 struct ttm_validate_buffer
*val_buf
,
443 struct vmw_fence_obj
*fence
;
444 uint32_t submit_size
;
445 struct vmw_surface
*srf
= vmw_res_to_srf(res
);
447 struct vmw_private
*dev_priv
= res
->dev_priv
;
449 BUG_ON(!val_buf
->bo
);
450 submit_size
= vmw_surface_dma_size(srf
);
451 cmd
= vmw_fifo_reserve(dev_priv
, submit_size
);
452 if (unlikely(!cmd
)) {
453 DRM_ERROR("Failed reserving FIFO space for surface "
457 vmw_bo_get_guest_ptr(val_buf
->bo
, &ptr
);
458 vmw_surface_dma_encode(srf
, cmd
, &ptr
, bind
);
460 vmw_fifo_commit(dev_priv
, submit_size
);
463 * Create a fence object and fence the backup buffer.
466 (void) vmw_execbuf_fence_commands(NULL
, dev_priv
,
469 vmw_fence_single_bo(val_buf
->bo
, fence
);
471 if (likely(fence
!= NULL
))
472 vmw_fence_obj_unreference(&fence
);
478 * vmw_legacy_srf_bind - Perform a legacy surface bind as part of the
479 * surface validation process.
481 * @res: Pointer to a struct vmw_res embedded in a struct
483 * @val_buf: Pointer to a struct ttm_validate_buffer containing
484 * information about the backup buffer.
486 * This function will copy backup data to the surface if the
487 * backup buffer is dirty.
489 static int vmw_legacy_srf_bind(struct vmw_resource
*res
,
490 struct ttm_validate_buffer
*val_buf
)
492 if (!res
->backup_dirty
)
495 return vmw_legacy_srf_dma(res
, val_buf
, true);
500 * vmw_legacy_srf_unbind - Perform a legacy surface unbind as part of the
501 * surface eviction process.
503 * @res: Pointer to a struct vmw_res embedded in a struct
505 * @val_buf: Pointer to a struct ttm_validate_buffer containing
506 * information about the backup buffer.
508 * This function will copy backup data from the surface.
510 static int vmw_legacy_srf_unbind(struct vmw_resource
*res
,
512 struct ttm_validate_buffer
*val_buf
)
514 if (unlikely(readback
))
515 return vmw_legacy_srf_dma(res
, val_buf
, false);
520 * vmw_legacy_srf_destroy - Destroy a device surface as part of a
521 * resource eviction process.
523 * @res: Pointer to a struct vmw_res embedded in a struct
526 static int vmw_legacy_srf_destroy(struct vmw_resource
*res
)
528 struct vmw_private
*dev_priv
= res
->dev_priv
;
529 uint32_t submit_size
;
532 BUG_ON(res
->id
== -1);
535 * Encode the dma- and surface destroy commands.
538 submit_size
= vmw_surface_destroy_size();
539 cmd
= vmw_fifo_reserve(dev_priv
, submit_size
);
540 if (unlikely(!cmd
)) {
541 DRM_ERROR("Failed reserving FIFO space for surface "
546 vmw_surface_destroy_encode(res
->id
, cmd
);
547 vmw_fifo_commit(dev_priv
, submit_size
);
550 * Surface memory usage accounting.
553 dev_priv
->used_memory_size
-= res
->backup_size
;
556 * Release the surface ID.
559 vmw_resource_release_id(res
);
566 * vmw_surface_init - initialize a struct vmw_surface
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
573 static int vmw_surface_init(struct vmw_private
*dev_priv
,
574 struct vmw_surface
*srf
,
575 void (*res_free
) (struct vmw_resource
*res
))
578 struct vmw_resource
*res
= &srf
->res
;
581 if (!dev_priv
->has_mob
)
582 vmw_fifo_resource_inc(dev_priv
);
583 ret
= vmw_resource_init(dev_priv
, res
, true, res_free
,
584 (dev_priv
->has_mob
) ? &vmw_gb_surface_func
:
585 &vmw_legacy_surface_func
);
587 if (unlikely(ret
!= 0)) {
588 if (!dev_priv
->has_mob
)
589 vmw_fifo_resource_dec(dev_priv
);
595 * The surface won't be visible to hardware until a
599 INIT_LIST_HEAD(&srf
->view_list
);
600 vmw_resource_activate(res
, vmw_hw_surface_destroy
);
605 * vmw_user_surface_base_to_res - TTM base object to resource converter for
606 * user visible surfaces
608 * @base: Pointer to a TTM base object
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.
613 static struct vmw_resource
*
614 vmw_user_surface_base_to_res(struct ttm_base_object
*base
)
616 return &(container_of(base
, struct vmw_user_surface
,
617 prime
.base
)->srf
.res
);
621 * vmw_user_surface_free - User visible surface resource destructor
623 * @res: A struct vmw_resource embedded in a struct vmw_surface.
625 static void vmw_user_surface_free(struct vmw_resource
*res
)
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
;
633 if (user_srf
->master
)
634 drm_master_put(&user_srf
->master
);
637 kfree(srf
->snooper
.image
);
638 ttm_prime_object_kfree(user_srf
, prime
);
639 ttm_mem_global_free(vmw_mem_glob(dev_priv
), size
);
643 * vmw_user_surface_free - User visible surface TTM base object destructor
645 * @p_base: Pointer to a pointer to a TTM base object
646 * embedded in a struct vmw_user_surface.
648 * Drops the base object's reference on its resource, and the
649 * pointer pointed to by *p_base is set to NULL.
651 static void vmw_user_surface_base_release(struct ttm_base_object
**p_base
)
653 struct ttm_base_object
*base
= *p_base
;
654 struct vmw_user_surface
*user_srf
=
655 container_of(base
, struct vmw_user_surface
, prime
.base
);
656 struct vmw_resource
*res
= &user_srf
->srf
.res
;
659 if (user_srf
->backup_base
)
660 ttm_base_object_unref(&user_srf
->backup_base
);
661 vmw_resource_unreference(&res
);
665 * vmw_user_surface_destroy_ioctl - Ioctl function implementing
666 * the user surface destroy functionality.
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.
672 int vmw_surface_destroy_ioctl(struct drm_device
*dev
, void *data
,
673 struct drm_file
*file_priv
)
675 struct drm_vmw_surface_arg
*arg
= (struct drm_vmw_surface_arg
*)data
;
676 struct ttm_object_file
*tfile
= vmw_fpriv(file_priv
)->tfile
;
678 return ttm_ref_object_base_unref(tfile
, arg
->sid
, TTM_REF_USAGE
);
682 * vmw_user_surface_define_ioctl - Ioctl function implementing
683 * the user surface define functionality.
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.
689 int vmw_surface_define_ioctl(struct drm_device
*dev
, void *data
,
690 struct drm_file
*file_priv
)
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
;
704 uint32_t cur_bo_offset
;
705 struct drm_vmw_size
*cur_size
;
706 struct vmw_surface_offset
*cur_offset
;
709 const struct svga3d_surface_desc
*desc
;
711 if (unlikely(vmw_user_surface_size
== 0))
712 vmw_user_surface_size
= ttm_round_pot(sizeof(*user_srf
)) +
716 for (i
= 0; i
< DRM_VMW_MAX_SURFACE_FACES
; ++i
) {
717 if (req
->mip_levels
[i
] > DRM_VMW_MAX_MIP_LEVELS
)
719 num_sizes
+= req
->mip_levels
[i
];
722 if (num_sizes
> DRM_VMW_MAX_SURFACE_FACES
* DRM_VMW_MAX_MIP_LEVELS
||
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
));
731 desc
= svga3dsurface_get_desc(req
->format
);
732 if (unlikely(desc
->block_desc
== SVGA3DBLOCKDESC_NONE
)) {
733 DRM_ERROR("Invalid surface format for surface creation.\n");
734 DRM_ERROR("Format requested is: %d\n", req
->format
);
738 ret
= ttm_read_lock(&dev_priv
->reservation_sem
, true);
739 if (unlikely(ret
!= 0))
742 ret
= ttm_mem_global_alloc(vmw_mem_glob(dev_priv
),
744 if (unlikely(ret
!= 0)) {
745 if (ret
!= -ERESTARTSYS
)
746 DRM_ERROR("Out of graphics memory for surface"
751 user_srf
= kzalloc(sizeof(*user_srf
), GFP_KERNEL
);
752 if (unlikely(!user_srf
)) {
754 goto out_no_user_srf
;
757 srf
= &user_srf
->srf
;
760 srf
->flags
= req
->flags
;
761 srf
->format
= req
->format
;
762 srf
->scanout
= req
->scanout
;
764 memcpy(srf
->mip_levels
, req
->mip_levels
, sizeof(srf
->mip_levels
));
765 srf
->num_sizes
= num_sizes
;
766 user_srf
->size
= size
;
767 srf
->sizes
= memdup_user((struct drm_vmw_size __user
*)(unsigned long)
769 sizeof(*srf
->sizes
) * srf
->num_sizes
);
770 if (IS_ERR(srf
->sizes
)) {
771 ret
= PTR_ERR(srf
->sizes
);
774 srf
->offsets
= kmalloc_array(srf
->num_sizes
,
775 sizeof(*srf
->offsets
),
777 if (unlikely(!srf
->offsets
)) {
782 srf
->base_size
= *srf
->sizes
;
783 srf
->autogen_filter
= SVGA3D_TEX_FILTER_NONE
;
784 srf
->multisample_count
= 0;
787 cur_offset
= srf
->offsets
;
788 cur_size
= srf
->sizes
;
790 for (i
= 0; i
< DRM_VMW_MAX_SURFACE_FACES
; ++i
) {
791 for (j
= 0; j
< srf
->mip_levels
[i
]; ++j
) {
792 uint32_t stride
= svga3dsurface_calculate_pitch
795 cur_offset
->face
= i
;
797 cur_offset
->bo_offset
= cur_bo_offset
;
798 cur_bo_offset
+= svga3dsurface_get_image_buffer_size
799 (desc
, cur_size
, stride
);
804 res
->backup_size
= cur_bo_offset
;
806 srf
->num_sizes
== 1 &&
807 srf
->sizes
[0].width
== 64 &&
808 srf
->sizes
[0].height
== 64 &&
809 srf
->format
== SVGA3D_A8R8G8B8
) {
811 srf
->snooper
.image
= kzalloc(64 * 64 * 4, GFP_KERNEL
);
812 if (!srf
->snooper
.image
) {
813 DRM_ERROR("Failed to allocate cursor_image\n");
818 srf
->snooper
.image
= NULL
;
821 user_srf
->prime
.base
.shareable
= false;
822 user_srf
->prime
.base
.tfile
= NULL
;
823 if (drm_is_primary_client(file_priv
))
824 user_srf
->master
= drm_master_get(file_priv
->master
);
827 * From this point, the generic resource management functions
828 * destroy the object on failure.
831 ret
= vmw_surface_init(dev_priv
, srf
, vmw_user_surface_free
);
832 if (unlikely(ret
!= 0))
836 * A gb-aware client referencing a shared surface will
837 * expect a backup buffer to be present.
839 if (dev_priv
->has_mob
&& req
->shareable
) {
840 uint32_t backup_handle
;
842 ret
= vmw_user_dmabuf_alloc(dev_priv
, tfile
,
847 &user_srf
->backup_base
);
848 if (unlikely(ret
!= 0)) {
849 vmw_resource_unreference(&res
);
854 tmp
= vmw_resource_reference(&srf
->res
);
855 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
);
859 if (unlikely(ret
!= 0)) {
860 vmw_resource_unreference(&tmp
);
861 vmw_resource_unreference(&res
);
865 rep
->sid
= user_srf
->prime
.base
.hash
.key
;
866 vmw_resource_unreference(&res
);
868 ttm_read_unlock(&dev_priv
->reservation_sem
);
875 ttm_prime_object_kfree(user_srf
, prime
);
877 ttm_mem_global_free(vmw_mem_glob(dev_priv
), size
);
879 ttm_read_unlock(&dev_priv
->reservation_sem
);
885 vmw_surface_handle_reference(struct vmw_private
*dev_priv
,
886 struct drm_file
*file_priv
,
888 enum drm_vmw_handle_type handle_type
,
889 struct ttm_base_object
**base_p
)
891 struct ttm_object_file
*tfile
= vmw_fpriv(file_priv
)->tfile
;
892 struct vmw_user_surface
*user_srf
;
894 struct ttm_base_object
*base
;
896 bool require_exist
= false;
898 if (handle_type
== DRM_VMW_HANDLE_PRIME
) {
899 ret
= ttm_prime_fd_to_handle(tfile
, u_handle
, &handle
);
900 if (unlikely(ret
!= 0))
903 if (unlikely(drm_is_render_client(file_priv
)))
904 require_exist
= true;
906 if (ACCESS_ONCE(vmw_fpriv(file_priv
)->locked_master
)) {
907 DRM_ERROR("Locked master refused legacy "
908 "surface reference.\n");
916 base
= ttm_base_object_lookup_for_ref(dev_priv
->tdev
, handle
);
917 if (unlikely(!base
)) {
918 DRM_ERROR("Could not find surface to reference.\n");
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
;
927 if (handle_type
!= DRM_VMW_HANDLE_PRIME
) {
928 user_srf
= container_of(base
, struct vmw_user_surface
,
932 * Make sure the surface creator has the same
933 * authenticating master, or is already registered with us.
935 if (drm_is_primary_client(file_priv
) &&
936 user_srf
->master
!= file_priv
->master
)
937 require_exist
= true;
939 ret
= ttm_ref_object_add(tfile
, base
, TTM_REF_USAGE
, NULL
,
941 if (unlikely(ret
!= 0)) {
942 DRM_ERROR("Could not add a reference to a surface.\n");
943 goto out_bad_resource
;
951 ttm_base_object_unref(&base
);
953 if (handle_type
== DRM_VMW_HANDLE_PRIME
)
954 (void) ttm_ref_object_base_unref(tfile
, handle
, TTM_REF_USAGE
);
960 * vmw_user_surface_define_ioctl - Ioctl function implementing
961 * the user surface reference functionality.
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.
967 int vmw_surface_reference_ioctl(struct drm_device
*dev
, void *data
,
968 struct drm_file
*file_priv
)
970 struct vmw_private
*dev_priv
= vmw_priv(dev
);
971 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
;
982 ret
= vmw_surface_handle_reference(dev_priv
, file_priv
, req
->sid
,
983 req
->handle_type
, &base
);
984 if (unlikely(ret
!= 0))
987 user_srf
= container_of(base
, struct vmw_user_surface
, prime
.base
);
988 srf
= &user_srf
->srf
;
990 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)
997 ret
= copy_to_user(user_sizes
, &srf
->base_size
,
998 sizeof(srf
->base_size
));
999 if (unlikely(ret
!= 0)) {
1000 DRM_ERROR("copy_to_user failed %p %u\n",
1001 user_sizes
, srf
->num_sizes
);
1002 ttm_ref_object_base_unref(tfile
, base
->hash
.key
, TTM_REF_USAGE
);
1006 ttm_base_object_unref(&base
);
1012 * vmw_surface_define_encode - Encode a surface_define command.
1014 * @srf: Pointer to a struct vmw_surface object.
1015 * @cmd_space: Pointer to memory area in which the commands should be encoded.
1017 static int vmw_gb_surface_create(struct vmw_resource
*res
)
1019 struct vmw_private
*dev_priv
= res
->dev_priv
;
1020 struct vmw_surface
*srf
= vmw_res_to_srf(res
);
1021 uint32_t cmd_len
, cmd_id
, submit_len
;
1024 SVGA3dCmdHeader header
;
1025 SVGA3dCmdDefineGBSurface body
;
1028 SVGA3dCmdHeader header
;
1029 SVGA3dCmdDefineGBSurface_v2 body
;
1032 if (likely(res
->id
!= -1))
1035 vmw_fifo_resource_inc(dev_priv
);
1036 ret
= vmw_resource_alloc_id(res
);
1037 if (unlikely(ret
!= 0)) {
1038 DRM_ERROR("Failed to allocate a surface id.\n");
1042 if (unlikely(res
->id
>= VMWGFX_NUM_GB_SURFACE
)) {
1047 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
);
1053 cmd_id
= SVGA_3D_CMD_DEFINE_GB_SURFACE
;
1054 cmd_len
= sizeof(cmd
->body
);
1055 submit_len
= sizeof(*cmd
);
1058 cmd
= vmw_fifo_reserve(dev_priv
, submit_len
);
1059 cmd2
= (typeof(cmd2
))cmd
;
1060 if (unlikely(!cmd
)) {
1061 DRM_ERROR("Failed reserving FIFO space for surface "
1067 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
;
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
;
1094 vmw_fifo_commit(dev_priv
, submit_len
);
1099 vmw_resource_release_id(res
);
1101 vmw_fifo_resource_dec(dev_priv
);
1106 static int vmw_gb_surface_bind(struct vmw_resource
*res
,
1107 struct ttm_validate_buffer
*val_buf
)
1109 struct vmw_private
*dev_priv
= res
->dev_priv
;
1111 SVGA3dCmdHeader header
;
1112 SVGA3dCmdBindGBSurface body
;
1115 SVGA3dCmdHeader header
;
1116 SVGA3dCmdUpdateGBSurface body
;
1118 uint32_t submit_size
;
1119 struct ttm_buffer_object
*bo
= val_buf
->bo
;
1121 BUG_ON(bo
->mem
.mem_type
!= VMW_PL_MOB
);
1123 submit_size
= sizeof(*cmd1
) + (res
->backup_dirty
? sizeof(*cmd2
) : 0);
1125 cmd1
= vmw_fifo_reserve(dev_priv
, submit_size
);
1126 if (unlikely(!cmd1
)) {
1127 DRM_ERROR("Failed reserving FIFO space for surface "
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;
1143 vmw_fifo_commit(dev_priv
, submit_size
);
1148 static int vmw_gb_surface_unbind(struct vmw_resource
*res
,
1150 struct ttm_validate_buffer
*val_buf
)
1152 struct vmw_private
*dev_priv
= res
->dev_priv
;
1153 struct ttm_buffer_object
*bo
= val_buf
->bo
;
1154 struct vmw_fence_obj
*fence
;
1157 SVGA3dCmdHeader header
;
1158 SVGA3dCmdReadbackGBSurface body
;
1161 SVGA3dCmdHeader header
;
1162 SVGA3dCmdInvalidateGBSurface body
;
1165 SVGA3dCmdHeader header
;
1166 SVGA3dCmdBindGBSurface body
;
1168 uint32_t submit_size
;
1172 BUG_ON(bo
->mem
.mem_type
!= VMW_PL_MOB
);
1174 submit_size
= sizeof(*cmd3
) + (readback
? sizeof(*cmd1
) : sizeof(*cmd2
));
1175 cmd
= vmw_fifo_reserve(dev_priv
, submit_size
);
1176 if (unlikely(!cmd
)) {
1177 DRM_ERROR("Failed reserving FIFO space for surface "
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
;
1187 cmd3
= (void *) &cmd1
[1];
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];
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
;
1201 vmw_fifo_commit(dev_priv
, submit_size
);
1204 * Create a fence object and fence the backup buffer.
1207 (void) vmw_execbuf_fence_commands(NULL
, dev_priv
,
1210 vmw_fence_single_bo(val_buf
->bo
, fence
);
1212 if (likely(fence
!= NULL
))
1213 vmw_fence_obj_unreference(&fence
);
1218 static int vmw_gb_surface_destroy(struct vmw_resource
*res
)
1220 struct vmw_private
*dev_priv
= res
->dev_priv
;
1221 struct vmw_surface
*srf
= vmw_res_to_srf(res
);
1223 SVGA3dCmdHeader header
;
1224 SVGA3dCmdDestroyGBSurface body
;
1227 if (likely(res
->id
== -1))
1230 mutex_lock(&dev_priv
->binding_mutex
);
1231 vmw_view_surface_list_destroy(dev_priv
, &srf
->view_list
);
1232 vmw_binding_res_list_scrub(&res
->binding_head
);
1234 cmd
= vmw_fifo_reserve(dev_priv
, sizeof(*cmd
));
1235 if (unlikely(!cmd
)) {
1236 DRM_ERROR("Failed reserving FIFO space for surface "
1238 mutex_unlock(&dev_priv
->binding_mutex
);
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
));
1246 mutex_unlock(&dev_priv
->binding_mutex
);
1247 vmw_resource_release_id(res
);
1248 vmw_fifo_resource_dec(dev_priv
);
1255 * vmw_gb_surface_define_ioctl - Ioctl function implementing
1256 * the user surface define functionality.
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.
1262 int vmw_gb_surface_define_ioctl(struct drm_device
*dev
, void *data
,
1263 struct drm_file
*file_priv
)
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
;
1277 uint32_t backup_handle
= 0;
1279 if (req
->multisample_count
!= 0)
1282 if (req
->mip_levels
> DRM_VMW_MAX_MIP_LEVELS
)
1285 if (unlikely(vmw_user_surface_size
== 0))
1286 vmw_user_surface_size
= ttm_round_pot(sizeof(*user_srf
)) +
1289 size
= vmw_user_surface_size
+ 128;
1291 /* Define a surface based on the parameters. */
1292 ret
= vmw_surface_gb_priv_define(dev
,
1296 req
->drm_surface_flags
& drm_vmw_surface_flag_scanout
,
1298 req
->multisample_count
,
1302 if (unlikely(ret
!= 0))
1305 user_srf
= container_of(srf
, struct vmw_user_surface
, srf
);
1306 if (drm_is_primary_client(file_priv
))
1307 user_srf
->master
= drm_master_get(file_priv
->master
);
1309 ret
= ttm_read_lock(&dev_priv
->reservation_sem
, true);
1310 if (unlikely(ret
!= 0))
1313 res
= &user_srf
->srf
.res
;
1316 if (req
->buffer_handle
!= SVGA3D_INVALID_ID
) {
1317 ret
= vmw_user_dmabuf_lookup(tfile
, req
->buffer_handle
,
1319 &user_srf
->backup_base
);
1321 if (res
->backup
->base
.num_pages
* PAGE_SIZE
<
1323 DRM_ERROR("Surface backup buffer is too small.\n");
1324 vmw_dmabuf_unreference(&res
->backup
);
1328 backup_handle
= req
->buffer_handle
;
1331 } else if (req
->drm_surface_flags
& drm_vmw_surface_flag_create_buffer
)
1332 ret
= vmw_user_dmabuf_alloc(dev_priv
, tfile
,
1334 req
->drm_surface_flags
&
1335 drm_vmw_surface_flag_shareable
,
1338 &user_srf
->backup_base
);
1340 if (unlikely(ret
!= 0)) {
1341 vmw_resource_unreference(&res
);
1345 tmp
= vmw_resource_reference(res
);
1346 ret
= ttm_prime_object_init(tfile
, res
->backup_size
, &user_srf
->prime
,
1347 req
->drm_surface_flags
&
1348 drm_vmw_surface_flag_shareable
,
1350 &vmw_user_surface_base_release
, NULL
);
1352 if (unlikely(ret
!= 0)) {
1353 vmw_resource_unreference(&tmp
);
1354 vmw_resource_unreference(&res
);
1358 rep
->handle
= user_srf
->prime
.base
.hash
.key
;
1359 rep
->backup_size
= res
->backup_size
;
1361 rep
->buffer_map_handle
=
1362 drm_vma_node_offset_addr(&res
->backup
->base
.vma_node
);
1363 rep
->buffer_size
= res
->backup
->base
.num_pages
* PAGE_SIZE
;
1364 rep
->buffer_handle
= backup_handle
;
1366 rep
->buffer_map_handle
= 0;
1367 rep
->buffer_size
= 0;
1368 rep
->buffer_handle
= SVGA3D_INVALID_ID
;
1371 vmw_resource_unreference(&res
);
1374 ttm_read_unlock(&dev_priv
->reservation_sem
);
1379 * vmw_gb_surface_reference_ioctl - Ioctl function implementing
1380 * the user surface reference functionality.
1382 * @dev: Pointer to a struct drm_device.
1383 * @data: Pointer to data copied from / to user-space.
1384 * @file_priv: Pointer to a drm file private structure.
1386 int vmw_gb_surface_reference_ioctl(struct drm_device
*dev
, void *data
,
1387 struct drm_file
*file_priv
)
1389 struct vmw_private
*dev_priv
= vmw_priv(dev
);
1390 union drm_vmw_gb_surface_reference_arg
*arg
=
1391 (union drm_vmw_gb_surface_reference_arg
*)data
;
1392 struct drm_vmw_surface_arg
*req
= &arg
->req
;
1393 struct drm_vmw_gb_surface_ref_rep
*rep
= &arg
->rep
;
1394 struct ttm_object_file
*tfile
= vmw_fpriv(file_priv
)->tfile
;
1395 struct vmw_surface
*srf
;
1396 struct vmw_user_surface
*user_srf
;
1397 struct ttm_base_object
*base
;
1398 uint32_t backup_handle
;
1401 ret
= vmw_surface_handle_reference(dev_priv
, file_priv
, req
->sid
,
1402 req
->handle_type
, &base
);
1403 if (unlikely(ret
!= 0))
1406 user_srf
= container_of(base
, struct vmw_user_surface
, prime
.base
);
1407 srf
= &user_srf
->srf
;
1408 if (!srf
->res
.backup
) {
1409 DRM_ERROR("Shared GB surface is missing a backup buffer.\n");
1410 goto out_bad_resource
;
1413 mutex_lock(&dev_priv
->cmdbuf_mutex
); /* Protect res->backup */
1414 ret
= vmw_user_dmabuf_reference(tfile
, srf
->res
.backup
,
1416 mutex_unlock(&dev_priv
->cmdbuf_mutex
);
1418 if (unlikely(ret
!= 0)) {
1419 DRM_ERROR("Could not add a reference to a GB surface "
1420 "backup buffer.\n");
1421 (void) ttm_ref_object_base_unref(tfile
, base
->hash
.key
,
1423 goto out_bad_resource
;
1426 rep
->creq
.svga3d_flags
= srf
->flags
;
1427 rep
->creq
.format
= srf
->format
;
1428 rep
->creq
.mip_levels
= srf
->mip_levels
[0];
1429 rep
->creq
.drm_surface_flags
= 0;
1430 rep
->creq
.multisample_count
= srf
->multisample_count
;
1431 rep
->creq
.autogen_filter
= srf
->autogen_filter
;
1432 rep
->creq
.array_size
= srf
->array_size
;
1433 rep
->creq
.buffer_handle
= backup_handle
;
1434 rep
->creq
.base_size
= srf
->base_size
;
1435 rep
->crep
.handle
= user_srf
->prime
.base
.hash
.key
;
1436 rep
->crep
.backup_size
= srf
->res
.backup_size
;
1437 rep
->crep
.buffer_handle
= backup_handle
;
1438 rep
->crep
.buffer_map_handle
=
1439 drm_vma_node_offset_addr(&srf
->res
.backup
->base
.vma_node
);
1440 rep
->crep
.buffer_size
= srf
->res
.backup
->base
.num_pages
* PAGE_SIZE
;
1443 ttm_base_object_unref(&base
);
1449 * vmw_surface_gb_priv_define - Define a private GB surface
1451 * @dev: Pointer to a struct drm_device
1452 * @user_accounting_size: Used to track user-space memory usage, set
1453 * to 0 for kernel mode only memory
1454 * @svga3d_flags: SVGA3d surface flags for the device
1455 * @format: requested surface format
1456 * @for_scanout: true if inteded to be used for scanout buffer
1457 * @num_mip_levels: number of MIP levels
1458 * @multisample_count:
1459 * @array_size: Surface array size.
1460 * @size: width, heigh, depth of the surface requested
1461 * @user_srf_out: allocated user_srf. Set to NULL on failure.
1463 * GB surfaces allocated by this function will not have a user mode handle, and
1464 * thus will only be visible to vmwgfx. For optimization reasons the
1465 * surface may later be given a user mode handle by another function to make
1466 * it available to user mode drivers.
1468 int vmw_surface_gb_priv_define(struct drm_device
*dev
,
1469 uint32_t user_accounting_size
,
1470 uint32_t svga3d_flags
,
1471 SVGA3dSurfaceFormat format
,
1473 uint32_t num_mip_levels
,
1474 uint32_t multisample_count
,
1475 uint32_t array_size
,
1476 struct drm_vmw_size size
,
1477 struct vmw_surface
**srf_out
)
1479 struct vmw_private
*dev_priv
= vmw_priv(dev
);
1480 struct vmw_user_surface
*user_srf
;
1481 struct vmw_surface
*srf
;
1488 uint32_t max_width
, max_height
;
1490 if (!svga3dsurface_is_screen_target_format(format
)) {
1491 DRM_ERROR("Invalid Screen Target surface format.");
1495 max_width
= min(dev_priv
->texture_max_width
,
1496 dev_priv
->stdu_max_width
);
1497 max_height
= min(dev_priv
->texture_max_height
,
1498 dev_priv
->stdu_max_height
);
1500 if (size
.width
> max_width
|| size
.height
> max_height
) {
1501 DRM_ERROR("%ux%u\n, exceeds max surface size %ux%u",
1502 size
.width
, size
.height
,
1503 max_width
, max_height
);
1507 const struct svga3d_surface_desc
*desc
;
1509 desc
= svga3dsurface_get_desc(format
);
1510 if (unlikely(desc
->block_desc
== SVGA3DBLOCKDESC_NONE
)) {
1511 DRM_ERROR("Invalid surface format.\n");
1516 /* array_size must be null for non-GL3 host. */
1517 if (array_size
> 0 && !dev_priv
->has_dx
) {
1518 DRM_ERROR("Tried to create DX surface on non-DX host.\n");
1522 ret
= ttm_read_lock(&dev_priv
->reservation_sem
, true);
1523 if (unlikely(ret
!= 0))
1526 ret
= ttm_mem_global_alloc(vmw_mem_glob(dev_priv
),
1527 user_accounting_size
, false, true);
1528 if (unlikely(ret
!= 0)) {
1529 if (ret
!= -ERESTARTSYS
)
1530 DRM_ERROR("Out of graphics memory for surface"
1535 user_srf
= kzalloc(sizeof(*user_srf
), GFP_KERNEL
);
1536 if (unlikely(!user_srf
)) {
1538 goto out_no_user_srf
;
1541 *srf_out
= &user_srf
->srf
;
1542 user_srf
->size
= user_accounting_size
;
1543 user_srf
->prime
.base
.shareable
= false;
1544 user_srf
->prime
.base
.tfile
= NULL
;
1546 srf
= &user_srf
->srf
;
1547 srf
->flags
= svga3d_flags
;
1548 srf
->format
= format
;
1549 srf
->scanout
= for_scanout
;
1550 srf
->mip_levels
[0] = num_mip_levels
;
1553 srf
->offsets
= NULL
;
1554 srf
->base_size
= size
;
1555 srf
->autogen_filter
= SVGA3D_TEX_FILTER_NONE
;
1556 srf
->array_size
= array_size
;
1557 srf
->multisample_count
= multisample_count
;
1560 num_layers
= array_size
;
1561 else if (svga3d_flags
& SVGA3D_SURFACE_CUBEMAP
)
1562 num_layers
= SVGA3D_MAX_SURFACE_FACES
;
1566 srf
->res
.backup_size
=
1567 svga3dsurface_get_serialized_size(srf
->format
,
1572 if (srf
->flags
& SVGA3D_SURFACE_BIND_STREAM_OUTPUT
)
1573 srf
->res
.backup_size
+= sizeof(SVGA3dDXSOState
);
1575 if (dev_priv
->active_display_unit
== vmw_du_screen_target
&&
1577 srf
->flags
|= SVGA3D_SURFACE_SCREENTARGET
;
1580 * From this point, the generic resource management functions
1581 * destroy the object on failure.
1583 ret
= vmw_surface_init(dev_priv
, srf
, vmw_user_surface_free
);
1585 ttm_read_unlock(&dev_priv
->reservation_sem
);
1589 ttm_mem_global_free(vmw_mem_glob(dev_priv
), user_accounting_size
);
1592 ttm_read_unlock(&dev_priv
->reservation_sem
);