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 <drm/ttm/ttm_placement.h>
30 #include "vmwgfx_drv.h"
31 #include "vmwgfx_resource_priv.h"
32 #include "vmwgfx_so.h"
33 #include "vmwgfx_binding.h"
34 #include "device_include/svga3d_surfacedefs.h"
38 * struct vmw_user_surface - User-space visible surface resource
40 * @base: The TTM base object handling user-space visibility.
41 * @srf: The surface metadata.
42 * @size: TTM accounting size for the surface.
43 * @master: master of the creating client. Used for security check.
45 struct vmw_user_surface
{
46 struct ttm_prime_object prime
;
47 struct vmw_surface srf
;
49 struct drm_master
*master
;
50 struct ttm_base_object
*backup_base
;
54 * struct vmw_surface_offset - Backing store mip level offset info
56 * @face: Surface face.
58 * @bo_offset: Offset into backing store of this mip level.
61 struct vmw_surface_offset
{
67 static void vmw_user_surface_free(struct vmw_resource
*res
);
68 static struct vmw_resource
*
69 vmw_user_surface_base_to_res(struct ttm_base_object
*base
);
70 static int vmw_legacy_srf_bind(struct vmw_resource
*res
,
71 struct ttm_validate_buffer
*val_buf
);
72 static int vmw_legacy_srf_unbind(struct vmw_resource
*res
,
74 struct ttm_validate_buffer
*val_buf
);
75 static int vmw_legacy_srf_create(struct vmw_resource
*res
);
76 static int vmw_legacy_srf_destroy(struct vmw_resource
*res
);
77 static int vmw_gb_surface_create(struct vmw_resource
*res
);
78 static int vmw_gb_surface_bind(struct vmw_resource
*res
,
79 struct ttm_validate_buffer
*val_buf
);
80 static int vmw_gb_surface_unbind(struct vmw_resource
*res
,
82 struct ttm_validate_buffer
*val_buf
);
83 static int vmw_gb_surface_destroy(struct vmw_resource
*res
);
86 static const struct vmw_user_resource_conv user_surface_conv
= {
87 .object_type
= VMW_RES_SURFACE
,
88 .base_obj_to_res
= vmw_user_surface_base_to_res
,
89 .res_free
= vmw_user_surface_free
92 const struct vmw_user_resource_conv
*user_surface_converter
=
96 static uint64_t vmw_user_surface_size
;
98 static const struct vmw_res_func vmw_legacy_surface_func
= {
99 .res_type
= vmw_res_surface
,
100 .needs_backup
= false,
102 .type_name
= "legacy surfaces",
103 .backup_placement
= &vmw_srf_placement
,
104 .create
= &vmw_legacy_srf_create
,
105 .destroy
= &vmw_legacy_srf_destroy
,
106 .bind
= &vmw_legacy_srf_bind
,
107 .unbind
= &vmw_legacy_srf_unbind
110 static const struct vmw_res_func vmw_gb_surface_func
= {
111 .res_type
= vmw_res_surface
,
112 .needs_backup
= true,
114 .type_name
= "guest backed surfaces",
115 .backup_placement
= &vmw_mob_placement
,
116 .create
= vmw_gb_surface_create
,
117 .destroy
= vmw_gb_surface_destroy
,
118 .bind
= vmw_gb_surface_bind
,
119 .unbind
= vmw_gb_surface_unbind
123 * struct vmw_surface_dma - SVGA3D DMA command
125 struct vmw_surface_dma
{
126 SVGA3dCmdHeader header
;
127 SVGA3dCmdSurfaceDMA body
;
129 SVGA3dCmdSurfaceDMASuffix suffix
;
133 * struct vmw_surface_define - SVGA3D Surface Define command
135 struct vmw_surface_define
{
136 SVGA3dCmdHeader header
;
137 SVGA3dCmdDefineSurface body
;
141 * struct vmw_surface_destroy - SVGA3D Surface Destroy command
143 struct vmw_surface_destroy
{
144 SVGA3dCmdHeader header
;
145 SVGA3dCmdDestroySurface body
;
150 * vmw_surface_dma_size - Compute fifo size for a dma command.
152 * @srf: Pointer to a struct vmw_surface
154 * Computes the required size for a surface dma command for backup or
155 * restoration of the surface represented by @srf.
157 static inline uint32_t vmw_surface_dma_size(const struct vmw_surface
*srf
)
159 return srf
->num_sizes
* sizeof(struct vmw_surface_dma
);
164 * vmw_surface_define_size - Compute fifo size for a surface define command.
166 * @srf: Pointer to a struct vmw_surface
168 * Computes the required size for a surface define command for the definition
169 * of the surface represented by @srf.
171 static inline uint32_t vmw_surface_define_size(const struct vmw_surface
*srf
)
173 return sizeof(struct vmw_surface_define
) + srf
->num_sizes
*
179 * vmw_surface_destroy_size - Compute fifo size for a surface destroy command.
181 * Computes the required size for a surface destroy command for the destruction
184 static inline uint32_t vmw_surface_destroy_size(void)
186 return sizeof(struct vmw_surface_destroy
);
190 * vmw_surface_destroy_encode - Encode a surface_destroy command.
192 * @id: The surface id
193 * @cmd_space: Pointer to memory area in which the commands should be encoded.
195 static void vmw_surface_destroy_encode(uint32_t id
,
198 struct vmw_surface_destroy
*cmd
= (struct vmw_surface_destroy
*)
201 cmd
->header
.id
= SVGA_3D_CMD_SURFACE_DESTROY
;
202 cmd
->header
.size
= sizeof(cmd
->body
);
207 * vmw_surface_define_encode - Encode a surface_define command.
209 * @srf: Pointer to a struct vmw_surface object.
210 * @cmd_space: Pointer to memory area in which the commands should be encoded.
212 static void vmw_surface_define_encode(const struct vmw_surface
*srf
,
215 struct vmw_surface_define
*cmd
= (struct vmw_surface_define
*)
217 struct drm_vmw_size
*src_size
;
218 SVGA3dSize
*cmd_size
;
222 cmd_len
= sizeof(cmd
->body
) + srf
->num_sizes
* sizeof(SVGA3dSize
);
224 cmd
->header
.id
= SVGA_3D_CMD_SURFACE_DEFINE
;
225 cmd
->header
.size
= cmd_len
;
226 cmd
->body
.sid
= srf
->res
.id
;
227 cmd
->body
.surfaceFlags
= srf
->flags
;
228 cmd
->body
.format
= srf
->format
;
229 for (i
= 0; i
< DRM_VMW_MAX_SURFACE_FACES
; ++i
)
230 cmd
->body
.face
[i
].numMipLevels
= srf
->mip_levels
[i
];
233 cmd_size
= (SVGA3dSize
*) cmd
;
234 src_size
= srf
->sizes
;
236 for (i
= 0; i
< srf
->num_sizes
; ++i
, cmd_size
++, src_size
++) {
237 cmd_size
->width
= src_size
->width
;
238 cmd_size
->height
= src_size
->height
;
239 cmd_size
->depth
= src_size
->depth
;
244 * vmw_surface_dma_encode - Encode a surface_dma command.
246 * @srf: Pointer to a struct vmw_surface object.
247 * @cmd_space: Pointer to memory area in which the commands should be encoded.
248 * @ptr: Pointer to an SVGAGuestPtr indicating where the surface contents
249 * should be placed or read from.
250 * @to_surface: Boolean whether to DMA to the surface or from the surface.
252 static void vmw_surface_dma_encode(struct vmw_surface
*srf
,
254 const SVGAGuestPtr
*ptr
,
258 struct vmw_surface_dma
*cmd
= (struct vmw_surface_dma
*)cmd_space
;
259 const struct svga3d_surface_desc
*desc
=
260 svga3dsurface_get_desc(srf
->format
);
262 for (i
= 0; i
< srf
->num_sizes
; ++i
) {
263 SVGA3dCmdHeader
*header
= &cmd
->header
;
264 SVGA3dCmdSurfaceDMA
*body
= &cmd
->body
;
265 SVGA3dCopyBox
*cb
= &cmd
->cb
;
266 SVGA3dCmdSurfaceDMASuffix
*suffix
= &cmd
->suffix
;
267 const struct vmw_surface_offset
*cur_offset
= &srf
->offsets
[i
];
268 const struct drm_vmw_size
*cur_size
= &srf
->sizes
[i
];
270 header
->id
= SVGA_3D_CMD_SURFACE_DMA
;
271 header
->size
= sizeof(*body
) + sizeof(*cb
) + sizeof(*suffix
);
273 body
->guest
.ptr
= *ptr
;
274 body
->guest
.ptr
.offset
+= cur_offset
->bo_offset
;
275 body
->guest
.pitch
= svga3dsurface_calculate_pitch(desc
,
277 body
->host
.sid
= srf
->res
.id
;
278 body
->host
.face
= cur_offset
->face
;
279 body
->host
.mipmap
= cur_offset
->mip
;
280 body
->transfer
= ((to_surface
) ? SVGA3D_WRITE_HOST_VRAM
:
281 SVGA3D_READ_HOST_VRAM
);
288 cb
->w
= cur_size
->width
;
289 cb
->h
= cur_size
->height
;
290 cb
->d
= cur_size
->depth
;
292 suffix
->suffixSize
= sizeof(*suffix
);
293 suffix
->maximumOffset
=
294 svga3dsurface_get_image_buffer_size(desc
, cur_size
,
296 suffix
->flags
.discard
= 0;
297 suffix
->flags
.unsynchronized
= 0;
298 suffix
->flags
.reserved
= 0;
305 * vmw_hw_surface_destroy - destroy a Device surface
307 * @res: Pointer to a struct vmw_resource embedded in a struct
310 * Destroys a the device surface associated with a struct vmw_surface if
311 * any, and adjusts accounting and resource count accordingly.
313 static void vmw_hw_surface_destroy(struct vmw_resource
*res
)
316 struct vmw_private
*dev_priv
= res
->dev_priv
;
317 struct vmw_surface
*srf
;
320 if (res
->func
->destroy
== vmw_gb_surface_destroy
) {
321 (void) vmw_gb_surface_destroy(res
);
327 cmd
= vmw_fifo_reserve(dev_priv
, vmw_surface_destroy_size());
328 if (unlikely(!cmd
)) {
329 DRM_ERROR("Failed reserving FIFO space for surface "
334 vmw_surface_destroy_encode(res
->id
, cmd
);
335 vmw_fifo_commit(dev_priv
, vmw_surface_destroy_size());
338 * used_memory_size_atomic, or separate lock
339 * to avoid taking dev_priv::cmdbuf_mutex in
343 mutex_lock(&dev_priv
->cmdbuf_mutex
);
344 srf
= vmw_res_to_srf(res
);
345 dev_priv
->used_memory_size
-= res
->backup_size
;
346 mutex_unlock(&dev_priv
->cmdbuf_mutex
);
348 vmw_fifo_resource_dec(dev_priv
);
352 * vmw_legacy_srf_create - Create a device surface as part of the
353 * resource validation process.
355 * @res: Pointer to a struct vmw_surface.
357 * If the surface doesn't have a hw id.
359 * Returns -EBUSY if there wasn't sufficient device resources to
360 * complete the validation. Retry after freeing up resources.
362 * May return other errors if the kernel is out of guest resources.
364 static int vmw_legacy_srf_create(struct vmw_resource
*res
)
366 struct vmw_private
*dev_priv
= res
->dev_priv
;
367 struct vmw_surface
*srf
;
368 uint32_t submit_size
;
372 if (likely(res
->id
!= -1))
375 srf
= vmw_res_to_srf(res
);
376 if (unlikely(dev_priv
->used_memory_size
+ res
->backup_size
>=
377 dev_priv
->memory_size
))
381 * Alloc id for the resource.
384 ret
= vmw_resource_alloc_id(res
);
385 if (unlikely(ret
!= 0)) {
386 DRM_ERROR("Failed to allocate a surface id.\n");
390 if (unlikely(res
->id
>= SVGA3D_MAX_SURFACE_IDS
)) {
396 * Encode surface define- commands.
399 submit_size
= vmw_surface_define_size(srf
);
400 cmd
= vmw_fifo_reserve(dev_priv
, submit_size
);
401 if (unlikely(!cmd
)) {
402 DRM_ERROR("Failed reserving FIFO space for surface "
408 vmw_surface_define_encode(srf
, cmd
);
409 vmw_fifo_commit(dev_priv
, submit_size
);
411 * Surface memory usage accounting.
414 dev_priv
->used_memory_size
+= res
->backup_size
;
418 vmw_resource_release_id(res
);
424 * vmw_legacy_srf_dma - Copy backup data to or from a legacy surface.
426 * @res: Pointer to a struct vmw_res embedded in a struct
428 * @val_buf: Pointer to a struct ttm_validate_buffer containing
429 * information about the backup buffer.
430 * @bind: Boolean wether to DMA to the surface.
432 * Transfer backup data to or from a legacy surface as part of the
433 * validation process.
434 * May return other errors if the kernel is out of guest resources.
435 * The backup buffer will be fenced or idle upon successful completion,
436 * and if the surface needs persistent backup storage, the backup buffer
437 * will also be returned reserved iff @bind is true.
439 static int vmw_legacy_srf_dma(struct vmw_resource
*res
,
440 struct ttm_validate_buffer
*val_buf
,
444 struct vmw_fence_obj
*fence
;
445 uint32_t submit_size
;
446 struct vmw_surface
*srf
= vmw_res_to_srf(res
);
448 struct vmw_private
*dev_priv
= res
->dev_priv
;
450 BUG_ON(!val_buf
->bo
);
451 submit_size
= vmw_surface_dma_size(srf
);
452 cmd
= vmw_fifo_reserve(dev_priv
, submit_size
);
453 if (unlikely(!cmd
)) {
454 DRM_ERROR("Failed reserving FIFO space for surface "
458 vmw_bo_get_guest_ptr(val_buf
->bo
, &ptr
);
459 vmw_surface_dma_encode(srf
, cmd
, &ptr
, bind
);
461 vmw_fifo_commit(dev_priv
, submit_size
);
464 * Create a fence object and fence the backup buffer.
467 (void) vmw_execbuf_fence_commands(NULL
, dev_priv
,
470 vmw_fence_single_bo(val_buf
->bo
, fence
);
472 if (likely(fence
!= NULL
))
473 vmw_fence_obj_unreference(&fence
);
479 * vmw_legacy_srf_bind - Perform a legacy surface bind as part of the
480 * surface validation process.
482 * @res: Pointer to a struct vmw_res embedded in a struct
484 * @val_buf: Pointer to a struct ttm_validate_buffer containing
485 * information about the backup buffer.
487 * This function will copy backup data to the surface if the
488 * backup buffer is dirty.
490 static int vmw_legacy_srf_bind(struct vmw_resource
*res
,
491 struct ttm_validate_buffer
*val_buf
)
493 if (!res
->backup_dirty
)
496 return vmw_legacy_srf_dma(res
, val_buf
, true);
501 * vmw_legacy_srf_unbind - Perform a legacy surface unbind as part of the
502 * surface eviction process.
504 * @res: Pointer to a struct vmw_res embedded in a struct
506 * @val_buf: Pointer to a struct ttm_validate_buffer containing
507 * information about the backup buffer.
509 * This function will copy backup data from the surface.
511 static int vmw_legacy_srf_unbind(struct vmw_resource
*res
,
513 struct ttm_validate_buffer
*val_buf
)
515 if (unlikely(readback
))
516 return vmw_legacy_srf_dma(res
, val_buf
, false);
521 * vmw_legacy_srf_destroy - Destroy a device surface as part of a
522 * resource eviction process.
524 * @res: Pointer to a struct vmw_res embedded in a struct
527 static int vmw_legacy_srf_destroy(struct vmw_resource
*res
)
529 struct vmw_private
*dev_priv
= res
->dev_priv
;
530 uint32_t submit_size
;
533 BUG_ON(res
->id
== -1);
536 * Encode the dma- and surface destroy commands.
539 submit_size
= vmw_surface_destroy_size();
540 cmd
= vmw_fifo_reserve(dev_priv
, submit_size
);
541 if (unlikely(!cmd
)) {
542 DRM_ERROR("Failed reserving FIFO space for surface "
547 vmw_surface_destroy_encode(res
->id
, cmd
);
548 vmw_fifo_commit(dev_priv
, submit_size
);
551 * Surface memory usage accounting.
554 dev_priv
->used_memory_size
-= res
->backup_size
;
557 * Release the surface ID.
560 vmw_resource_release_id(res
);
567 * vmw_surface_init - initialize a struct vmw_surface
569 * @dev_priv: Pointer to a device private struct.
570 * @srf: Pointer to the struct vmw_surface to initialize.
571 * @res_free: Pointer to a resource destructor used to free
574 static int vmw_surface_init(struct vmw_private
*dev_priv
,
575 struct vmw_surface
*srf
,
576 void (*res_free
) (struct vmw_resource
*res
))
579 struct vmw_resource
*res
= &srf
->res
;
582 if (!dev_priv
->has_mob
)
583 vmw_fifo_resource_inc(dev_priv
);
584 ret
= vmw_resource_init(dev_priv
, res
, true, res_free
,
585 (dev_priv
->has_mob
) ? &vmw_gb_surface_func
:
586 &vmw_legacy_surface_func
);
588 if (unlikely(ret
!= 0)) {
589 if (!dev_priv
->has_mob
)
590 vmw_fifo_resource_dec(dev_priv
);
596 * The surface won't be visible to hardware until a
600 INIT_LIST_HEAD(&srf
->view_list
);
601 vmw_resource_activate(res
, vmw_hw_surface_destroy
);
606 * vmw_user_surface_base_to_res - TTM base object to resource converter for
607 * user visible surfaces
609 * @base: Pointer to a TTM base object
611 * Returns the struct vmw_resource embedded in a struct vmw_surface
612 * for the user-visible object identified by the TTM base object @base.
614 static struct vmw_resource
*
615 vmw_user_surface_base_to_res(struct ttm_base_object
*base
)
617 return &(container_of(base
, struct vmw_user_surface
,
618 prime
.base
)->srf
.res
);
622 * vmw_user_surface_free - User visible surface resource destructor
624 * @res: A struct vmw_resource embedded in a struct vmw_surface.
626 static void vmw_user_surface_free(struct vmw_resource
*res
)
628 struct vmw_surface
*srf
= vmw_res_to_srf(res
);
629 struct vmw_user_surface
*user_srf
=
630 container_of(srf
, struct vmw_user_surface
, srf
);
631 struct vmw_private
*dev_priv
= srf
->res
.dev_priv
;
632 uint32_t size
= user_srf
->size
;
634 if (user_srf
->master
)
635 drm_master_put(&user_srf
->master
);
638 kfree(srf
->snooper
.image
);
639 ttm_prime_object_kfree(user_srf
, prime
);
640 ttm_mem_global_free(vmw_mem_glob(dev_priv
), size
);
644 * vmw_user_surface_free - User visible surface TTM base object destructor
646 * @p_base: Pointer to a pointer to a TTM base object
647 * embedded in a struct vmw_user_surface.
649 * Drops the base object's reference on its resource, and the
650 * pointer pointed to by *p_base is set to NULL.
652 static void vmw_user_surface_base_release(struct ttm_base_object
**p_base
)
654 struct ttm_base_object
*base
= *p_base
;
655 struct vmw_user_surface
*user_srf
=
656 container_of(base
, struct vmw_user_surface
, prime
.base
);
657 struct vmw_resource
*res
= &user_srf
->srf
.res
;
660 if (user_srf
->backup_base
)
661 ttm_base_object_unref(&user_srf
->backup_base
);
662 vmw_resource_unreference(&res
);
666 * vmw_user_surface_destroy_ioctl - Ioctl function implementing
667 * the user surface destroy functionality.
669 * @dev: Pointer to a struct drm_device.
670 * @data: Pointer to data copied from / to user-space.
671 * @file_priv: Pointer to a drm file private structure.
673 int vmw_surface_destroy_ioctl(struct drm_device
*dev
, void *data
,
674 struct drm_file
*file_priv
)
676 struct drm_vmw_surface_arg
*arg
= (struct drm_vmw_surface_arg
*)data
;
677 struct ttm_object_file
*tfile
= vmw_fpriv(file_priv
)->tfile
;
679 return ttm_ref_object_base_unref(tfile
, arg
->sid
, TTM_REF_USAGE
);
683 * vmw_user_surface_define_ioctl - Ioctl function implementing
684 * the user surface define functionality.
686 * @dev: Pointer to a struct drm_device.
687 * @data: Pointer to data copied from / to user-space.
688 * @file_priv: Pointer to a drm file private structure.
690 int vmw_surface_define_ioctl(struct drm_device
*dev
, void *data
,
691 struct drm_file
*file_priv
)
693 struct vmw_private
*dev_priv
= vmw_priv(dev
);
694 struct vmw_user_surface
*user_srf
;
695 struct vmw_surface
*srf
;
696 struct vmw_resource
*res
;
697 struct vmw_resource
*tmp
;
698 union drm_vmw_surface_create_arg
*arg
=
699 (union drm_vmw_surface_create_arg
*)data
;
700 struct drm_vmw_surface_create_req
*req
= &arg
->req
;
701 struct drm_vmw_surface_arg
*rep
= &arg
->rep
;
702 struct ttm_object_file
*tfile
= vmw_fpriv(file_priv
)->tfile
;
703 struct ttm_operation_ctx ctx
= {
704 .interruptible
= true,
709 uint32_t cur_bo_offset
;
710 struct drm_vmw_size
*cur_size
;
711 struct vmw_surface_offset
*cur_offset
;
714 const struct svga3d_surface_desc
*desc
;
716 if (unlikely(vmw_user_surface_size
== 0))
717 vmw_user_surface_size
= ttm_round_pot(sizeof(*user_srf
)) +
721 for (i
= 0; i
< DRM_VMW_MAX_SURFACE_FACES
; ++i
) {
722 if (req
->mip_levels
[i
] > DRM_VMW_MAX_MIP_LEVELS
)
724 num_sizes
+= req
->mip_levels
[i
];
727 if (num_sizes
> DRM_VMW_MAX_SURFACE_FACES
* DRM_VMW_MAX_MIP_LEVELS
||
731 size
= vmw_user_surface_size
+ 128 +
732 ttm_round_pot(num_sizes
* sizeof(struct drm_vmw_size
)) +
733 ttm_round_pot(num_sizes
* sizeof(struct vmw_surface_offset
));
736 desc
= svga3dsurface_get_desc(req
->format
);
737 if (unlikely(desc
->block_desc
== SVGA3DBLOCKDESC_NONE
)) {
738 DRM_ERROR("Invalid surface format for surface creation.\n");
739 DRM_ERROR("Format requested is: %d\n", req
->format
);
743 ret
= ttm_read_lock(&dev_priv
->reservation_sem
, true);
744 if (unlikely(ret
!= 0))
747 ret
= ttm_mem_global_alloc(vmw_mem_glob(dev_priv
),
749 if (unlikely(ret
!= 0)) {
750 if (ret
!= -ERESTARTSYS
)
751 DRM_ERROR("Out of graphics memory for surface"
756 user_srf
= kzalloc(sizeof(*user_srf
), GFP_KERNEL
);
757 if (unlikely(!user_srf
)) {
759 goto out_no_user_srf
;
762 srf
= &user_srf
->srf
;
765 srf
->flags
= req
->flags
;
766 srf
->format
= req
->format
;
767 srf
->scanout
= req
->scanout
;
769 memcpy(srf
->mip_levels
, req
->mip_levels
, sizeof(srf
->mip_levels
));
770 srf
->num_sizes
= num_sizes
;
771 user_srf
->size
= size
;
772 srf
->sizes
= memdup_user((struct drm_vmw_size __user
*)(unsigned long)
774 sizeof(*srf
->sizes
) * srf
->num_sizes
);
775 if (IS_ERR(srf
->sizes
)) {
776 ret
= PTR_ERR(srf
->sizes
);
779 srf
->offsets
= kmalloc_array(srf
->num_sizes
,
780 sizeof(*srf
->offsets
),
782 if (unlikely(!srf
->offsets
)) {
787 srf
->base_size
= *srf
->sizes
;
788 srf
->autogen_filter
= SVGA3D_TEX_FILTER_NONE
;
789 srf
->multisample_count
= 0;
792 cur_offset
= srf
->offsets
;
793 cur_size
= srf
->sizes
;
795 for (i
= 0; i
< DRM_VMW_MAX_SURFACE_FACES
; ++i
) {
796 for (j
= 0; j
< srf
->mip_levels
[i
]; ++j
) {
797 uint32_t stride
= svga3dsurface_calculate_pitch
800 cur_offset
->face
= i
;
802 cur_offset
->bo_offset
= cur_bo_offset
;
803 cur_bo_offset
+= svga3dsurface_get_image_buffer_size
804 (desc
, cur_size
, stride
);
809 res
->backup_size
= cur_bo_offset
;
811 srf
->num_sizes
== 1 &&
812 srf
->sizes
[0].width
== 64 &&
813 srf
->sizes
[0].height
== 64 &&
814 srf
->format
== SVGA3D_A8R8G8B8
) {
816 srf
->snooper
.image
= kzalloc(64 * 64 * 4, GFP_KERNEL
);
817 if (!srf
->snooper
.image
) {
818 DRM_ERROR("Failed to allocate cursor_image\n");
823 srf
->snooper
.image
= NULL
;
826 user_srf
->prime
.base
.shareable
= false;
827 user_srf
->prime
.base
.tfile
= NULL
;
828 if (drm_is_primary_client(file_priv
))
829 user_srf
->master
= drm_master_get(file_priv
->master
);
832 * From this point, the generic resource management functions
833 * destroy the object on failure.
836 ret
= vmw_surface_init(dev_priv
, srf
, vmw_user_surface_free
);
837 if (unlikely(ret
!= 0))
841 * A gb-aware client referencing a shared surface will
842 * expect a backup buffer to be present.
844 if (dev_priv
->has_mob
&& req
->shareable
) {
845 uint32_t backup_handle
;
847 ret
= vmw_user_dmabuf_alloc(dev_priv
, tfile
,
852 &user_srf
->backup_base
);
853 if (unlikely(ret
!= 0)) {
854 vmw_resource_unreference(&res
);
859 tmp
= vmw_resource_reference(&srf
->res
);
860 ret
= ttm_prime_object_init(tfile
, res
->backup_size
, &user_srf
->prime
,
861 req
->shareable
, VMW_RES_SURFACE
,
862 &vmw_user_surface_base_release
, NULL
);
864 if (unlikely(ret
!= 0)) {
865 vmw_resource_unreference(&tmp
);
866 vmw_resource_unreference(&res
);
870 rep
->sid
= user_srf
->prime
.base
.hash
.key
;
871 vmw_resource_unreference(&res
);
873 ttm_read_unlock(&dev_priv
->reservation_sem
);
880 ttm_prime_object_kfree(user_srf
, prime
);
882 ttm_mem_global_free(vmw_mem_glob(dev_priv
), size
);
884 ttm_read_unlock(&dev_priv
->reservation_sem
);
890 vmw_surface_handle_reference(struct vmw_private
*dev_priv
,
891 struct drm_file
*file_priv
,
893 enum drm_vmw_handle_type handle_type
,
894 struct ttm_base_object
**base_p
)
896 struct ttm_object_file
*tfile
= vmw_fpriv(file_priv
)->tfile
;
897 struct vmw_user_surface
*user_srf
;
899 struct ttm_base_object
*base
;
901 bool require_exist
= false;
903 if (handle_type
== DRM_VMW_HANDLE_PRIME
) {
904 ret
= ttm_prime_fd_to_handle(tfile
, u_handle
, &handle
);
905 if (unlikely(ret
!= 0))
908 if (unlikely(drm_is_render_client(file_priv
)))
909 require_exist
= true;
911 if (READ_ONCE(vmw_fpriv(file_priv
)->locked_master
)) {
912 DRM_ERROR("Locked master refused legacy "
913 "surface reference.\n");
921 base
= ttm_base_object_lookup_for_ref(dev_priv
->tdev
, handle
);
922 if (unlikely(!base
)) {
923 DRM_ERROR("Could not find surface to reference.\n");
927 if (unlikely(ttm_base_object_type(base
) != VMW_RES_SURFACE
)) {
928 DRM_ERROR("Referenced object is not a surface.\n");
929 goto out_bad_resource
;
932 if (handle_type
!= DRM_VMW_HANDLE_PRIME
) {
933 user_srf
= container_of(base
, struct vmw_user_surface
,
937 * Make sure the surface creator has the same
938 * authenticating master, or is already registered with us.
940 if (drm_is_primary_client(file_priv
) &&
941 user_srf
->master
!= file_priv
->master
)
942 require_exist
= true;
944 ret
= ttm_ref_object_add(tfile
, base
, TTM_REF_USAGE
, NULL
,
946 if (unlikely(ret
!= 0)) {
947 DRM_ERROR("Could not add a reference to a surface.\n");
948 goto out_bad_resource
;
956 ttm_base_object_unref(&base
);
958 if (handle_type
== DRM_VMW_HANDLE_PRIME
)
959 (void) ttm_ref_object_base_unref(tfile
, handle
, TTM_REF_USAGE
);
965 * vmw_user_surface_define_ioctl - Ioctl function implementing
966 * the user surface reference functionality.
968 * @dev: Pointer to a struct drm_device.
969 * @data: Pointer to data copied from / to user-space.
970 * @file_priv: Pointer to a drm file private structure.
972 int vmw_surface_reference_ioctl(struct drm_device
*dev
, void *data
,
973 struct drm_file
*file_priv
)
975 struct vmw_private
*dev_priv
= vmw_priv(dev
);
976 union drm_vmw_surface_reference_arg
*arg
=
977 (union drm_vmw_surface_reference_arg
*)data
;
978 struct drm_vmw_surface_arg
*req
= &arg
->req
;
979 struct drm_vmw_surface_create_req
*rep
= &arg
->rep
;
980 struct ttm_object_file
*tfile
= vmw_fpriv(file_priv
)->tfile
;
981 struct vmw_surface
*srf
;
982 struct vmw_user_surface
*user_srf
;
983 struct drm_vmw_size __user
*user_sizes
;
984 struct ttm_base_object
*base
;
987 ret
= vmw_surface_handle_reference(dev_priv
, file_priv
, req
->sid
,
988 req
->handle_type
, &base
);
989 if (unlikely(ret
!= 0))
992 user_srf
= container_of(base
, struct vmw_user_surface
, prime
.base
);
993 srf
= &user_srf
->srf
;
995 rep
->flags
= srf
->flags
;
996 rep
->format
= srf
->format
;
997 memcpy(rep
->mip_levels
, srf
->mip_levels
, sizeof(srf
->mip_levels
));
998 user_sizes
= (struct drm_vmw_size __user
*)(unsigned long)
1002 ret
= copy_to_user(user_sizes
, &srf
->base_size
,
1003 sizeof(srf
->base_size
));
1004 if (unlikely(ret
!= 0)) {
1005 DRM_ERROR("copy_to_user failed %p %u\n",
1006 user_sizes
, srf
->num_sizes
);
1007 ttm_ref_object_base_unref(tfile
, base
->hash
.key
, TTM_REF_USAGE
);
1011 ttm_base_object_unref(&base
);
1017 * vmw_surface_define_encode - Encode a surface_define command.
1019 * @srf: Pointer to a struct vmw_surface object.
1020 * @cmd_space: Pointer to memory area in which the commands should be encoded.
1022 static int vmw_gb_surface_create(struct vmw_resource
*res
)
1024 struct vmw_private
*dev_priv
= res
->dev_priv
;
1025 struct vmw_surface
*srf
= vmw_res_to_srf(res
);
1026 uint32_t cmd_len
, cmd_id
, submit_len
;
1029 SVGA3dCmdHeader header
;
1030 SVGA3dCmdDefineGBSurface body
;
1033 SVGA3dCmdHeader header
;
1034 SVGA3dCmdDefineGBSurface_v2 body
;
1037 if (likely(res
->id
!= -1))
1040 vmw_fifo_resource_inc(dev_priv
);
1041 ret
= vmw_resource_alloc_id(res
);
1042 if (unlikely(ret
!= 0)) {
1043 DRM_ERROR("Failed to allocate a surface id.\n");
1047 if (unlikely(res
->id
>= VMWGFX_NUM_GB_SURFACE
)) {
1052 if (srf
->array_size
> 0) {
1053 /* has_dx checked on creation time. */
1054 cmd_id
= SVGA_3D_CMD_DEFINE_GB_SURFACE_V2
;
1055 cmd_len
= sizeof(cmd2
->body
);
1056 submit_len
= sizeof(*cmd2
);
1058 cmd_id
= SVGA_3D_CMD_DEFINE_GB_SURFACE
;
1059 cmd_len
= sizeof(cmd
->body
);
1060 submit_len
= sizeof(*cmd
);
1063 cmd
= vmw_fifo_reserve(dev_priv
, submit_len
);
1064 cmd2
= (typeof(cmd2
))cmd
;
1065 if (unlikely(!cmd
)) {
1066 DRM_ERROR("Failed reserving FIFO space for surface "
1072 if (srf
->array_size
> 0) {
1073 cmd2
->header
.id
= cmd_id
;
1074 cmd2
->header
.size
= cmd_len
;
1075 cmd2
->body
.sid
= srf
->res
.id
;
1076 cmd2
->body
.surfaceFlags
= srf
->flags
;
1077 cmd2
->body
.format
= cpu_to_le32(srf
->format
);
1078 cmd2
->body
.numMipLevels
= srf
->mip_levels
[0];
1079 cmd2
->body
.multisampleCount
= srf
->multisample_count
;
1080 cmd2
->body
.autogenFilter
= srf
->autogen_filter
;
1081 cmd2
->body
.size
.width
= srf
->base_size
.width
;
1082 cmd2
->body
.size
.height
= srf
->base_size
.height
;
1083 cmd2
->body
.size
.depth
= srf
->base_size
.depth
;
1084 cmd2
->body
.arraySize
= srf
->array_size
;
1086 cmd
->header
.id
= cmd_id
;
1087 cmd
->header
.size
= cmd_len
;
1088 cmd
->body
.sid
= srf
->res
.id
;
1089 cmd
->body
.surfaceFlags
= srf
->flags
;
1090 cmd
->body
.format
= cpu_to_le32(srf
->format
);
1091 cmd
->body
.numMipLevels
= srf
->mip_levels
[0];
1092 cmd
->body
.multisampleCount
= srf
->multisample_count
;
1093 cmd
->body
.autogenFilter
= srf
->autogen_filter
;
1094 cmd
->body
.size
.width
= srf
->base_size
.width
;
1095 cmd
->body
.size
.height
= srf
->base_size
.height
;
1096 cmd
->body
.size
.depth
= srf
->base_size
.depth
;
1099 vmw_fifo_commit(dev_priv
, submit_len
);
1104 vmw_resource_release_id(res
);
1106 vmw_fifo_resource_dec(dev_priv
);
1111 static int vmw_gb_surface_bind(struct vmw_resource
*res
,
1112 struct ttm_validate_buffer
*val_buf
)
1114 struct vmw_private
*dev_priv
= res
->dev_priv
;
1116 SVGA3dCmdHeader header
;
1117 SVGA3dCmdBindGBSurface body
;
1120 SVGA3dCmdHeader header
;
1121 SVGA3dCmdUpdateGBSurface body
;
1123 uint32_t submit_size
;
1124 struct ttm_buffer_object
*bo
= val_buf
->bo
;
1126 BUG_ON(bo
->mem
.mem_type
!= VMW_PL_MOB
);
1128 submit_size
= sizeof(*cmd1
) + (res
->backup_dirty
? sizeof(*cmd2
) : 0);
1130 cmd1
= vmw_fifo_reserve(dev_priv
, submit_size
);
1131 if (unlikely(!cmd1
)) {
1132 DRM_ERROR("Failed reserving FIFO space for surface "
1137 cmd1
->header
.id
= SVGA_3D_CMD_BIND_GB_SURFACE
;
1138 cmd1
->header
.size
= sizeof(cmd1
->body
);
1139 cmd1
->body
.sid
= res
->id
;
1140 cmd1
->body
.mobid
= bo
->mem
.start
;
1141 if (res
->backup_dirty
) {
1142 cmd2
= (void *) &cmd1
[1];
1143 cmd2
->header
.id
= SVGA_3D_CMD_UPDATE_GB_SURFACE
;
1144 cmd2
->header
.size
= sizeof(cmd2
->body
);
1145 cmd2
->body
.sid
= res
->id
;
1146 res
->backup_dirty
= false;
1148 vmw_fifo_commit(dev_priv
, submit_size
);
1153 static int vmw_gb_surface_unbind(struct vmw_resource
*res
,
1155 struct ttm_validate_buffer
*val_buf
)
1157 struct vmw_private
*dev_priv
= res
->dev_priv
;
1158 struct ttm_buffer_object
*bo
= val_buf
->bo
;
1159 struct vmw_fence_obj
*fence
;
1162 SVGA3dCmdHeader header
;
1163 SVGA3dCmdReadbackGBSurface body
;
1166 SVGA3dCmdHeader header
;
1167 SVGA3dCmdInvalidateGBSurface body
;
1170 SVGA3dCmdHeader header
;
1171 SVGA3dCmdBindGBSurface body
;
1173 uint32_t submit_size
;
1177 BUG_ON(bo
->mem
.mem_type
!= VMW_PL_MOB
);
1179 submit_size
= sizeof(*cmd3
) + (readback
? sizeof(*cmd1
) : sizeof(*cmd2
));
1180 cmd
= vmw_fifo_reserve(dev_priv
, submit_size
);
1181 if (unlikely(!cmd
)) {
1182 DRM_ERROR("Failed reserving FIFO space for surface "
1188 cmd1
= (void *) cmd
;
1189 cmd1
->header
.id
= SVGA_3D_CMD_READBACK_GB_SURFACE
;
1190 cmd1
->header
.size
= sizeof(cmd1
->body
);
1191 cmd1
->body
.sid
= res
->id
;
1192 cmd3
= (void *) &cmd1
[1];
1194 cmd2
= (void *) cmd
;
1195 cmd2
->header
.id
= SVGA_3D_CMD_INVALIDATE_GB_SURFACE
;
1196 cmd2
->header
.size
= sizeof(cmd2
->body
);
1197 cmd2
->body
.sid
= res
->id
;
1198 cmd3
= (void *) &cmd2
[1];
1201 cmd3
->header
.id
= SVGA_3D_CMD_BIND_GB_SURFACE
;
1202 cmd3
->header
.size
= sizeof(cmd3
->body
);
1203 cmd3
->body
.sid
= res
->id
;
1204 cmd3
->body
.mobid
= SVGA3D_INVALID_ID
;
1206 vmw_fifo_commit(dev_priv
, submit_size
);
1209 * Create a fence object and fence the backup buffer.
1212 (void) vmw_execbuf_fence_commands(NULL
, dev_priv
,
1215 vmw_fence_single_bo(val_buf
->bo
, fence
);
1217 if (likely(fence
!= NULL
))
1218 vmw_fence_obj_unreference(&fence
);
1223 static int vmw_gb_surface_destroy(struct vmw_resource
*res
)
1225 struct vmw_private
*dev_priv
= res
->dev_priv
;
1226 struct vmw_surface
*srf
= vmw_res_to_srf(res
);
1228 SVGA3dCmdHeader header
;
1229 SVGA3dCmdDestroyGBSurface body
;
1232 if (likely(res
->id
== -1))
1235 mutex_lock(&dev_priv
->binding_mutex
);
1236 vmw_view_surface_list_destroy(dev_priv
, &srf
->view_list
);
1237 vmw_binding_res_list_scrub(&res
->binding_head
);
1239 cmd
= vmw_fifo_reserve(dev_priv
, sizeof(*cmd
));
1240 if (unlikely(!cmd
)) {
1241 DRM_ERROR("Failed reserving FIFO space for surface "
1243 mutex_unlock(&dev_priv
->binding_mutex
);
1247 cmd
->header
.id
= SVGA_3D_CMD_DESTROY_GB_SURFACE
;
1248 cmd
->header
.size
= sizeof(cmd
->body
);
1249 cmd
->body
.sid
= res
->id
;
1250 vmw_fifo_commit(dev_priv
, sizeof(*cmd
));
1251 mutex_unlock(&dev_priv
->binding_mutex
);
1252 vmw_resource_release_id(res
);
1253 vmw_fifo_resource_dec(dev_priv
);
1260 * vmw_gb_surface_define_ioctl - Ioctl function implementing
1261 * the user surface define functionality.
1263 * @dev: Pointer to a struct drm_device.
1264 * @data: Pointer to data copied from / to user-space.
1265 * @file_priv: Pointer to a drm file private structure.
1267 int vmw_gb_surface_define_ioctl(struct drm_device
*dev
, void *data
,
1268 struct drm_file
*file_priv
)
1270 struct vmw_private
*dev_priv
= vmw_priv(dev
);
1271 struct vmw_user_surface
*user_srf
;
1272 struct vmw_surface
*srf
;
1273 struct vmw_resource
*res
;
1274 struct vmw_resource
*tmp
;
1275 union drm_vmw_gb_surface_create_arg
*arg
=
1276 (union drm_vmw_gb_surface_create_arg
*)data
;
1277 struct drm_vmw_gb_surface_create_req
*req
= &arg
->req
;
1278 struct drm_vmw_gb_surface_create_rep
*rep
= &arg
->rep
;
1279 struct ttm_object_file
*tfile
= vmw_fpriv(file_priv
)->tfile
;
1282 uint32_t backup_handle
= 0;
1284 if (req
->multisample_count
!= 0)
1287 if (req
->mip_levels
> DRM_VMW_MAX_MIP_LEVELS
)
1290 if (unlikely(vmw_user_surface_size
== 0))
1291 vmw_user_surface_size
= ttm_round_pot(sizeof(*user_srf
)) +
1294 size
= vmw_user_surface_size
+ 128;
1296 /* Define a surface based on the parameters. */
1297 ret
= vmw_surface_gb_priv_define(dev
,
1301 req
->drm_surface_flags
& drm_vmw_surface_flag_scanout
,
1303 req
->multisample_count
,
1307 if (unlikely(ret
!= 0))
1310 user_srf
= container_of(srf
, struct vmw_user_surface
, srf
);
1311 if (drm_is_primary_client(file_priv
))
1312 user_srf
->master
= drm_master_get(file_priv
->master
);
1314 ret
= ttm_read_lock(&dev_priv
->reservation_sem
, true);
1315 if (unlikely(ret
!= 0))
1318 res
= &user_srf
->srf
.res
;
1321 if (req
->buffer_handle
!= SVGA3D_INVALID_ID
) {
1322 ret
= vmw_user_dmabuf_lookup(tfile
, req
->buffer_handle
,
1324 &user_srf
->backup_base
);
1326 if (res
->backup
->base
.num_pages
* PAGE_SIZE
<
1328 DRM_ERROR("Surface backup buffer is too small.\n");
1329 vmw_dmabuf_unreference(&res
->backup
);
1333 backup_handle
= req
->buffer_handle
;
1336 } else if (req
->drm_surface_flags
& drm_vmw_surface_flag_create_buffer
)
1337 ret
= vmw_user_dmabuf_alloc(dev_priv
, tfile
,
1339 req
->drm_surface_flags
&
1340 drm_vmw_surface_flag_shareable
,
1343 &user_srf
->backup_base
);
1345 if (unlikely(ret
!= 0)) {
1346 vmw_resource_unreference(&res
);
1350 tmp
= vmw_resource_reference(res
);
1351 ret
= ttm_prime_object_init(tfile
, res
->backup_size
, &user_srf
->prime
,
1352 req
->drm_surface_flags
&
1353 drm_vmw_surface_flag_shareable
,
1355 &vmw_user_surface_base_release
, NULL
);
1357 if (unlikely(ret
!= 0)) {
1358 vmw_resource_unreference(&tmp
);
1359 vmw_resource_unreference(&res
);
1363 rep
->handle
= user_srf
->prime
.base
.hash
.key
;
1364 rep
->backup_size
= res
->backup_size
;
1366 rep
->buffer_map_handle
=
1367 drm_vma_node_offset_addr(&res
->backup
->base
.vma_node
);
1368 rep
->buffer_size
= res
->backup
->base
.num_pages
* PAGE_SIZE
;
1369 rep
->buffer_handle
= backup_handle
;
1371 rep
->buffer_map_handle
= 0;
1372 rep
->buffer_size
= 0;
1373 rep
->buffer_handle
= SVGA3D_INVALID_ID
;
1376 vmw_resource_unreference(&res
);
1379 ttm_read_unlock(&dev_priv
->reservation_sem
);
1384 * vmw_gb_surface_reference_ioctl - Ioctl function implementing
1385 * the user surface reference functionality.
1387 * @dev: Pointer to a struct drm_device.
1388 * @data: Pointer to data copied from / to user-space.
1389 * @file_priv: Pointer to a drm file private structure.
1391 int vmw_gb_surface_reference_ioctl(struct drm_device
*dev
, void *data
,
1392 struct drm_file
*file_priv
)
1394 struct vmw_private
*dev_priv
= vmw_priv(dev
);
1395 union drm_vmw_gb_surface_reference_arg
*arg
=
1396 (union drm_vmw_gb_surface_reference_arg
*)data
;
1397 struct drm_vmw_surface_arg
*req
= &arg
->req
;
1398 struct drm_vmw_gb_surface_ref_rep
*rep
= &arg
->rep
;
1399 struct ttm_object_file
*tfile
= vmw_fpriv(file_priv
)->tfile
;
1400 struct vmw_surface
*srf
;
1401 struct vmw_user_surface
*user_srf
;
1402 struct ttm_base_object
*base
;
1403 uint32_t backup_handle
;
1406 ret
= vmw_surface_handle_reference(dev_priv
, file_priv
, req
->sid
,
1407 req
->handle_type
, &base
);
1408 if (unlikely(ret
!= 0))
1411 user_srf
= container_of(base
, struct vmw_user_surface
, prime
.base
);
1412 srf
= &user_srf
->srf
;
1413 if (!srf
->res
.backup
) {
1414 DRM_ERROR("Shared GB surface is missing a backup buffer.\n");
1415 goto out_bad_resource
;
1418 mutex_lock(&dev_priv
->cmdbuf_mutex
); /* Protect res->backup */
1419 ret
= vmw_user_dmabuf_reference(tfile
, srf
->res
.backup
,
1421 mutex_unlock(&dev_priv
->cmdbuf_mutex
);
1423 if (unlikely(ret
!= 0)) {
1424 DRM_ERROR("Could not add a reference to a GB surface "
1425 "backup buffer.\n");
1426 (void) ttm_ref_object_base_unref(tfile
, base
->hash
.key
,
1428 goto out_bad_resource
;
1431 rep
->creq
.svga3d_flags
= srf
->flags
;
1432 rep
->creq
.format
= srf
->format
;
1433 rep
->creq
.mip_levels
= srf
->mip_levels
[0];
1434 rep
->creq
.drm_surface_flags
= 0;
1435 rep
->creq
.multisample_count
= srf
->multisample_count
;
1436 rep
->creq
.autogen_filter
= srf
->autogen_filter
;
1437 rep
->creq
.array_size
= srf
->array_size
;
1438 rep
->creq
.buffer_handle
= backup_handle
;
1439 rep
->creq
.base_size
= srf
->base_size
;
1440 rep
->crep
.handle
= user_srf
->prime
.base
.hash
.key
;
1441 rep
->crep
.backup_size
= srf
->res
.backup_size
;
1442 rep
->crep
.buffer_handle
= backup_handle
;
1443 rep
->crep
.buffer_map_handle
=
1444 drm_vma_node_offset_addr(&srf
->res
.backup
->base
.vma_node
);
1445 rep
->crep
.buffer_size
= srf
->res
.backup
->base
.num_pages
* PAGE_SIZE
;
1448 ttm_base_object_unref(&base
);
1454 * vmw_surface_gb_priv_define - Define a private GB surface
1456 * @dev: Pointer to a struct drm_device
1457 * @user_accounting_size: Used to track user-space memory usage, set
1458 * to 0 for kernel mode only memory
1459 * @svga3d_flags: SVGA3d surface flags for the device
1460 * @format: requested surface format
1461 * @for_scanout: true if inteded to be used for scanout buffer
1462 * @num_mip_levels: number of MIP levels
1463 * @multisample_count:
1464 * @array_size: Surface array size.
1465 * @size: width, heigh, depth of the surface requested
1466 * @user_srf_out: allocated user_srf. Set to NULL on failure.
1468 * GB surfaces allocated by this function will not have a user mode handle, and
1469 * thus will only be visible to vmwgfx. For optimization reasons the
1470 * surface may later be given a user mode handle by another function to make
1471 * it available to user mode drivers.
1473 int vmw_surface_gb_priv_define(struct drm_device
*dev
,
1474 uint32_t user_accounting_size
,
1475 uint32_t svga3d_flags
,
1476 SVGA3dSurfaceFormat format
,
1478 uint32_t num_mip_levels
,
1479 uint32_t multisample_count
,
1480 uint32_t array_size
,
1481 struct drm_vmw_size size
,
1482 struct vmw_surface
**srf_out
)
1484 struct vmw_private
*dev_priv
= vmw_priv(dev
);
1485 struct vmw_user_surface
*user_srf
;
1486 struct ttm_operation_ctx ctx
= {
1487 .interruptible
= true,
1488 .no_wait_gpu
= false
1490 struct vmw_surface
*srf
;
1497 uint32_t max_width
, max_height
;
1499 if (!svga3dsurface_is_screen_target_format(format
)) {
1500 DRM_ERROR("Invalid Screen Target surface format.");
1504 max_width
= min(dev_priv
->texture_max_width
,
1505 dev_priv
->stdu_max_width
);
1506 max_height
= min(dev_priv
->texture_max_height
,
1507 dev_priv
->stdu_max_height
);
1509 if (size
.width
> max_width
|| size
.height
> max_height
) {
1510 DRM_ERROR("%ux%u\n, exceeds max surface size %ux%u",
1511 size
.width
, size
.height
,
1512 max_width
, max_height
);
1516 const struct svga3d_surface_desc
*desc
;
1518 desc
= svga3dsurface_get_desc(format
);
1519 if (unlikely(desc
->block_desc
== SVGA3DBLOCKDESC_NONE
)) {
1520 DRM_ERROR("Invalid surface format.\n");
1525 /* array_size must be null for non-GL3 host. */
1526 if (array_size
> 0 && !dev_priv
->has_dx
) {
1527 DRM_ERROR("Tried to create DX surface on non-DX host.\n");
1531 ret
= ttm_read_lock(&dev_priv
->reservation_sem
, true);
1532 if (unlikely(ret
!= 0))
1535 ret
= ttm_mem_global_alloc(vmw_mem_glob(dev_priv
),
1536 user_accounting_size
, &ctx
);
1537 if (unlikely(ret
!= 0)) {
1538 if (ret
!= -ERESTARTSYS
)
1539 DRM_ERROR("Out of graphics memory for surface"
1544 user_srf
= kzalloc(sizeof(*user_srf
), GFP_KERNEL
);
1545 if (unlikely(!user_srf
)) {
1547 goto out_no_user_srf
;
1550 *srf_out
= &user_srf
->srf
;
1551 user_srf
->size
= user_accounting_size
;
1552 user_srf
->prime
.base
.shareable
= false;
1553 user_srf
->prime
.base
.tfile
= NULL
;
1555 srf
= &user_srf
->srf
;
1556 srf
->flags
= svga3d_flags
;
1557 srf
->format
= format
;
1558 srf
->scanout
= for_scanout
;
1559 srf
->mip_levels
[0] = num_mip_levels
;
1562 srf
->offsets
= NULL
;
1563 srf
->base_size
= size
;
1564 srf
->autogen_filter
= SVGA3D_TEX_FILTER_NONE
;
1565 srf
->array_size
= array_size
;
1566 srf
->multisample_count
= multisample_count
;
1569 num_layers
= array_size
;
1570 else if (svga3d_flags
& SVGA3D_SURFACE_CUBEMAP
)
1571 num_layers
= SVGA3D_MAX_SURFACE_FACES
;
1575 srf
->res
.backup_size
=
1576 svga3dsurface_get_serialized_size(srf
->format
,
1581 if (srf
->flags
& SVGA3D_SURFACE_BIND_STREAM_OUTPUT
)
1582 srf
->res
.backup_size
+= sizeof(SVGA3dDXSOState
);
1584 if (dev_priv
->active_display_unit
== vmw_du_screen_target
&&
1586 srf
->flags
|= SVGA3D_SURFACE_SCREENTARGET
;
1589 * From this point, the generic resource management functions
1590 * destroy the object on failure.
1592 ret
= vmw_surface_init(dev_priv
, srf
, vmw_user_surface_free
);
1594 ttm_read_unlock(&dev_priv
->reservation_sem
);
1598 ttm_mem_global_free(vmw_mem_glob(dev_priv
), user_accounting_size
);
1601 ttm_read_unlock(&dev_priv
->reservation_sem
);