1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
4 * Copyright 2009-2023 VMware, Inc., Palo Alto, CA., USA
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "vmwgfx_bo.h"
29 #include "vmwgfx_drv.h"
30 #include "vmwgfx_resource_priv.h"
31 #include "vmwgfx_so.h"
32 #include "vmwgfx_binding.h"
33 #include "vmw_surface_cache.h"
34 #include "device_include/svga3d_surfacedefs.h"
36 #include <drm/ttm/ttm_placement.h>
38 #define SVGA3D_FLAGS_64(upper32, lower32) (((uint64_t)upper32 << 32) | lower32)
39 #define SVGA3D_FLAGS_UPPER_32(svga3d_flags) (svga3d_flags >> 32)
40 #define SVGA3D_FLAGS_LOWER_32(svga3d_flags) \
41 (svga3d_flags & ((uint64_t)U32_MAX))
44 * struct vmw_user_surface - User-space visible surface resource
46 * @prime: The TTM prime object.
47 * @base: The TTM base object handling user-space visibility.
48 * @srf: The surface metadata.
49 * @master: Master of the creating client. Used for security check.
51 struct vmw_user_surface {
52 struct ttm_prime_object prime;
53 struct vmw_surface srf;
54 struct drm_master *master;
58 * struct vmw_surface_offset - Backing store mip level offset info
60 * @face: Surface face.
62 * @bo_offset: Offset into backing store of this mip level.
65 struct vmw_surface_offset {
72 * struct vmw_surface_dirty - Surface dirty-tracker
73 * @cache: Cached layout information of the surface.
74 * @num_subres: Number of subresources.
75 * @boxes: Array of SVGA3dBoxes indicating dirty regions. One per subresource.
77 struct vmw_surface_dirty {
78 struct vmw_surface_cache cache;
83 static void vmw_user_surface_free(struct vmw_resource *res);
84 static struct vmw_resource *
85 vmw_user_surface_base_to_res(struct ttm_base_object *base);
86 static int vmw_legacy_srf_bind(struct vmw_resource *res,
87 struct ttm_validate_buffer *val_buf);
88 static int vmw_legacy_srf_unbind(struct vmw_resource *res,
90 struct ttm_validate_buffer *val_buf);
91 static int vmw_legacy_srf_create(struct vmw_resource *res);
92 static int vmw_legacy_srf_destroy(struct vmw_resource *res);
93 static int vmw_gb_surface_create(struct vmw_resource *res);
94 static int vmw_gb_surface_bind(struct vmw_resource *res,
95 struct ttm_validate_buffer *val_buf);
96 static int vmw_gb_surface_unbind(struct vmw_resource *res,
98 struct ttm_validate_buffer *val_buf);
99 static int vmw_gb_surface_destroy(struct vmw_resource *res);
101 vmw_gb_surface_define_internal(struct drm_device *dev,
102 struct drm_vmw_gb_surface_create_ext_req *req,
103 struct drm_vmw_gb_surface_create_rep *rep,
104 struct drm_file *file_priv);
106 vmw_gb_surface_reference_internal(struct drm_device *dev,
107 struct drm_vmw_surface_arg *req,
108 struct drm_vmw_gb_surface_ref_ext_rep *rep,
109 struct drm_file *file_priv);
111 static void vmw_surface_dirty_free(struct vmw_resource *res);
112 static int vmw_surface_dirty_alloc(struct vmw_resource *res);
113 static int vmw_surface_dirty_sync(struct vmw_resource *res);
114 static void vmw_surface_dirty_range_add(struct vmw_resource *res, size_t start,
116 static int vmw_surface_clean(struct vmw_resource *res);
118 static const struct vmw_user_resource_conv user_surface_conv = {
119 .object_type = VMW_RES_SURFACE,
120 .base_obj_to_res = vmw_user_surface_base_to_res,
121 .res_free = vmw_user_surface_free
124 const struct vmw_user_resource_conv *user_surface_converter =
127 static const struct vmw_res_func vmw_legacy_surface_func = {
128 .res_type = vmw_res_surface,
129 .needs_guest_memory = false,
133 .type_name = "legacy surfaces",
134 .domain = VMW_BO_DOMAIN_GMR,
135 .busy_domain = VMW_BO_DOMAIN_GMR | VMW_BO_DOMAIN_VRAM,
136 .create = &vmw_legacy_srf_create,
137 .destroy = &vmw_legacy_srf_destroy,
138 .bind = &vmw_legacy_srf_bind,
139 .unbind = &vmw_legacy_srf_unbind
142 static const struct vmw_res_func vmw_gb_surface_func = {
143 .res_type = vmw_res_surface,
144 .needs_guest_memory = true,
148 .type_name = "guest backed surfaces",
149 .domain = VMW_BO_DOMAIN_MOB,
150 .busy_domain = VMW_BO_DOMAIN_MOB,
151 .create = vmw_gb_surface_create,
152 .destroy = vmw_gb_surface_destroy,
153 .bind = vmw_gb_surface_bind,
154 .unbind = vmw_gb_surface_unbind,
155 .dirty_alloc = vmw_surface_dirty_alloc,
156 .dirty_free = vmw_surface_dirty_free,
157 .dirty_sync = vmw_surface_dirty_sync,
158 .dirty_range_add = vmw_surface_dirty_range_add,
159 .clean = vmw_surface_clean,
163 * struct vmw_surface_dma - SVGA3D DMA command
165 struct vmw_surface_dma {
166 SVGA3dCmdHeader header;
167 SVGA3dCmdSurfaceDMA body;
169 SVGA3dCmdSurfaceDMASuffix suffix;
173 * struct vmw_surface_define - SVGA3D Surface Define command
175 struct vmw_surface_define {
176 SVGA3dCmdHeader header;
177 SVGA3dCmdDefineSurface body;
181 * struct vmw_surface_destroy - SVGA3D Surface Destroy command
183 struct vmw_surface_destroy {
184 SVGA3dCmdHeader header;
185 SVGA3dCmdDestroySurface body;
190 * vmw_surface_dma_size - Compute fifo size for a dma command.
192 * @srf: Pointer to a struct vmw_surface
194 * Computes the required size for a surface dma command for backup or
195 * restoration of the surface represented by @srf.
197 static inline uint32_t vmw_surface_dma_size(const struct vmw_surface *srf)
199 return srf->metadata.num_sizes * sizeof(struct vmw_surface_dma);
204 * vmw_surface_define_size - Compute fifo size for a surface define command.
206 * @srf: Pointer to a struct vmw_surface
208 * Computes the required size for a surface define command for the definition
209 * of the surface represented by @srf.
211 static inline uint32_t vmw_surface_define_size(const struct vmw_surface *srf)
213 return sizeof(struct vmw_surface_define) + srf->metadata.num_sizes *
219 * vmw_surface_destroy_size - Compute fifo size for a surface destroy command.
221 * Computes the required size for a surface destroy command for the destruction
224 static inline uint32_t vmw_surface_destroy_size(void)
226 return sizeof(struct vmw_surface_destroy);
230 * vmw_surface_destroy_encode - Encode a surface_destroy command.
232 * @id: The surface id
233 * @cmd_space: Pointer to memory area in which the commands should be encoded.
235 static void vmw_surface_destroy_encode(uint32_t id,
238 struct vmw_surface_destroy *cmd = (struct vmw_surface_destroy *)
241 cmd->header.id = SVGA_3D_CMD_SURFACE_DESTROY;
242 cmd->header.size = sizeof(cmd->body);
247 * vmw_surface_define_encode - Encode a surface_define command.
249 * @srf: Pointer to a struct vmw_surface object.
250 * @cmd_space: Pointer to memory area in which the commands should be encoded.
252 static void vmw_surface_define_encode(const struct vmw_surface *srf,
255 struct vmw_surface_define *cmd = (struct vmw_surface_define *)
257 struct drm_vmw_size *src_size;
258 SVGA3dSize *cmd_size;
262 cmd_len = sizeof(cmd->body) + srf->metadata.num_sizes *
265 cmd->header.id = SVGA_3D_CMD_SURFACE_DEFINE;
266 cmd->header.size = cmd_len;
267 cmd->body.sid = srf->res.id;
269 * Downcast of surfaceFlags, was upcasted when received from user-space,
270 * since driver internally stores as 64 bit.
271 * For legacy surface define only 32 bit flag is supported.
273 cmd->body.surfaceFlags = (SVGA3dSurface1Flags)srf->metadata.flags;
274 cmd->body.format = srf->metadata.format;
275 for (i = 0; i < DRM_VMW_MAX_SURFACE_FACES; ++i)
276 cmd->body.face[i].numMipLevels = srf->metadata.mip_levels[i];
279 cmd_size = (SVGA3dSize *) cmd;
280 src_size = srf->metadata.sizes;
282 for (i = 0; i < srf->metadata.num_sizes; ++i, cmd_size++, src_size++) {
283 cmd_size->width = src_size->width;
284 cmd_size->height = src_size->height;
285 cmd_size->depth = src_size->depth;
290 * vmw_surface_dma_encode - Encode a surface_dma command.
292 * @srf: Pointer to a struct vmw_surface object.
293 * @cmd_space: Pointer to memory area in which the commands should be encoded.
294 * @ptr: Pointer to an SVGAGuestPtr indicating where the surface contents
295 * should be placed or read from.
296 * @to_surface: Boolean whether to DMA to the surface or from the surface.
298 static void vmw_surface_dma_encode(struct vmw_surface *srf,
300 const SVGAGuestPtr *ptr,
304 struct vmw_surface_dma *cmd = (struct vmw_surface_dma *)cmd_space;
305 const struct SVGA3dSurfaceDesc *desc =
306 vmw_surface_get_desc(srf->metadata.format);
308 for (i = 0; i < srf->metadata.num_sizes; ++i) {
309 SVGA3dCmdHeader *header = &cmd->header;
310 SVGA3dCmdSurfaceDMA *body = &cmd->body;
311 SVGA3dCopyBox *cb = &cmd->cb;
312 SVGA3dCmdSurfaceDMASuffix *suffix = &cmd->suffix;
313 const struct vmw_surface_offset *cur_offset = &srf->offsets[i];
314 const struct drm_vmw_size *cur_size = &srf->metadata.sizes[i];
316 header->id = SVGA_3D_CMD_SURFACE_DMA;
317 header->size = sizeof(*body) + sizeof(*cb) + sizeof(*suffix);
319 body->guest.ptr = *ptr;
320 body->guest.ptr.offset += cur_offset->bo_offset;
321 body->guest.pitch = vmw_surface_calculate_pitch(desc, cur_size);
322 body->host.sid = srf->res.id;
323 body->host.face = cur_offset->face;
324 body->host.mipmap = cur_offset->mip;
325 body->transfer = ((to_surface) ? SVGA3D_WRITE_HOST_VRAM :
326 SVGA3D_READ_HOST_VRAM);
333 cb->w = cur_size->width;
334 cb->h = cur_size->height;
335 cb->d = cur_size->depth;
337 suffix->suffixSize = sizeof(*suffix);
338 suffix->maximumOffset =
339 vmw_surface_get_image_buffer_size(desc, cur_size,
341 suffix->flags.discard = 0;
342 suffix->flags.unsynchronized = 0;
343 suffix->flags.reserved = 0;
350 * vmw_hw_surface_destroy - destroy a Device surface
352 * @res: Pointer to a struct vmw_resource embedded in a struct
355 * Destroys a the device surface associated with a struct vmw_surface if
356 * any, and adjusts resource count accordingly.
358 static void vmw_hw_surface_destroy(struct vmw_resource *res)
361 struct vmw_private *dev_priv = res->dev_priv;
364 if (res->func->destroy == vmw_gb_surface_destroy) {
365 (void) vmw_gb_surface_destroy(res);
371 cmd = VMW_CMD_RESERVE(dev_priv, vmw_surface_destroy_size());
375 vmw_surface_destroy_encode(res->id, cmd);
376 vmw_cmd_commit(dev_priv, vmw_surface_destroy_size());
379 * used_memory_size_atomic, or separate lock
380 * to avoid taking dev_priv::cmdbuf_mutex in
384 mutex_lock(&dev_priv->cmdbuf_mutex);
385 dev_priv->used_memory_size -= res->guest_memory_size;
386 mutex_unlock(&dev_priv->cmdbuf_mutex);
391 * vmw_legacy_srf_create - Create a device surface as part of the
392 * resource validation process.
394 * @res: Pointer to a struct vmw_surface.
396 * If the surface doesn't have a hw id.
398 * Returns -EBUSY if there wasn't sufficient device resources to
399 * complete the validation. Retry after freeing up resources.
401 * May return other errors if the kernel is out of guest resources.
403 static int vmw_legacy_srf_create(struct vmw_resource *res)
405 struct vmw_private *dev_priv = res->dev_priv;
406 struct vmw_surface *srf;
407 uint32_t submit_size;
411 if (likely(res->id != -1))
414 srf = vmw_res_to_srf(res);
415 if (unlikely(dev_priv->used_memory_size + res->guest_memory_size >=
416 dev_priv->memory_size))
420 * Alloc id for the resource.
423 ret = vmw_resource_alloc_id(res);
424 if (unlikely(ret != 0)) {
425 DRM_ERROR("Failed to allocate a surface id.\n");
429 if (unlikely(res->id >= SVGA3D_HB_MAX_SURFACE_IDS)) {
435 * Encode surface define- commands.
438 submit_size = vmw_surface_define_size(srf);
439 cmd = VMW_CMD_RESERVE(dev_priv, submit_size);
440 if (unlikely(!cmd)) {
445 vmw_surface_define_encode(srf, cmd);
446 vmw_cmd_commit(dev_priv, submit_size);
447 vmw_fifo_resource_inc(dev_priv);
450 * Surface memory usage accounting.
453 dev_priv->used_memory_size += res->guest_memory_size;
457 vmw_resource_release_id(res);
463 * vmw_legacy_srf_dma - Copy backup data to or from a legacy surface.
465 * @res: Pointer to a struct vmw_res embedded in a struct
467 * @val_buf: Pointer to a struct ttm_validate_buffer containing
468 * information about the backup buffer.
469 * @bind: Boolean wether to DMA to the surface.
471 * Transfer backup data to or from a legacy surface as part of the
472 * validation process.
473 * May return other errors if the kernel is out of guest resources.
474 * The backup buffer will be fenced or idle upon successful completion,
475 * and if the surface needs persistent backup storage, the backup buffer
476 * will also be returned reserved iff @bind is true.
478 static int vmw_legacy_srf_dma(struct vmw_resource *res,
479 struct ttm_validate_buffer *val_buf,
483 struct vmw_fence_obj *fence;
484 uint32_t submit_size;
485 struct vmw_surface *srf = vmw_res_to_srf(res);
487 struct vmw_private *dev_priv = res->dev_priv;
489 BUG_ON(!val_buf->bo);
490 submit_size = vmw_surface_dma_size(srf);
491 cmd = VMW_CMD_RESERVE(dev_priv, submit_size);
495 vmw_bo_get_guest_ptr(val_buf->bo, &ptr);
496 vmw_surface_dma_encode(srf, cmd, &ptr, bind);
498 vmw_cmd_commit(dev_priv, submit_size);
501 * Create a fence object and fence the backup buffer.
504 (void) vmw_execbuf_fence_commands(NULL, dev_priv,
507 vmw_bo_fence_single(val_buf->bo, fence);
509 if (likely(fence != NULL))
510 vmw_fence_obj_unreference(&fence);
516 * vmw_legacy_srf_bind - Perform a legacy surface bind as part of the
517 * surface validation process.
519 * @res: Pointer to a struct vmw_res embedded in a struct
521 * @val_buf: Pointer to a struct ttm_validate_buffer containing
522 * information about the backup buffer.
524 * This function will copy backup data to the surface if the
525 * backup buffer is dirty.
527 static int vmw_legacy_srf_bind(struct vmw_resource *res,
528 struct ttm_validate_buffer *val_buf)
530 if (!res->guest_memory_dirty)
533 return vmw_legacy_srf_dma(res, val_buf, true);
538 * vmw_legacy_srf_unbind - Perform a legacy surface unbind as part of the
539 * surface eviction process.
541 * @res: Pointer to a struct vmw_res embedded in a struct
543 * @readback: Readback - only true if dirty
544 * @val_buf: Pointer to a struct ttm_validate_buffer containing
545 * information about the backup buffer.
547 * This function will copy backup data from the surface.
549 static int vmw_legacy_srf_unbind(struct vmw_resource *res,
551 struct ttm_validate_buffer *val_buf)
553 if (unlikely(readback))
554 return vmw_legacy_srf_dma(res, val_buf, false);
559 * vmw_legacy_srf_destroy - Destroy a device surface as part of a
560 * resource eviction process.
562 * @res: Pointer to a struct vmw_res embedded in a struct
565 static int vmw_legacy_srf_destroy(struct vmw_resource *res)
567 struct vmw_private *dev_priv = res->dev_priv;
568 uint32_t submit_size;
571 BUG_ON(res->id == -1);
574 * Encode the dma- and surface destroy commands.
577 submit_size = vmw_surface_destroy_size();
578 cmd = VMW_CMD_RESERVE(dev_priv, submit_size);
582 vmw_surface_destroy_encode(res->id, cmd);
583 vmw_cmd_commit(dev_priv, submit_size);
586 * Surface memory usage accounting.
589 dev_priv->used_memory_size -= res->guest_memory_size;
592 * Release the surface ID.
595 vmw_resource_release_id(res);
596 vmw_fifo_resource_dec(dev_priv);
603 * vmw_surface_init - initialize a struct vmw_surface
605 * @dev_priv: Pointer to a device private struct.
606 * @srf: Pointer to the struct vmw_surface to initialize.
607 * @res_free: Pointer to a resource destructor used to free
610 static int vmw_surface_init(struct vmw_private *dev_priv,
611 struct vmw_surface *srf,
612 void (*res_free) (struct vmw_resource *res))
615 struct vmw_resource *res = &srf->res;
618 ret = vmw_resource_init(dev_priv, res, true, res_free,
619 (dev_priv->has_mob) ? &vmw_gb_surface_func :
620 &vmw_legacy_surface_func);
622 if (unlikely(ret != 0)) {
628 * The surface won't be visible to hardware until a
632 INIT_LIST_HEAD(&srf->view_list);
633 res->hw_destroy = vmw_hw_surface_destroy;
638 * vmw_user_surface_base_to_res - TTM base object to resource converter for
639 * user visible surfaces
641 * @base: Pointer to a TTM base object
643 * Returns the struct vmw_resource embedded in a struct vmw_surface
644 * for the user-visible object identified by the TTM base object @base.
646 static struct vmw_resource *
647 vmw_user_surface_base_to_res(struct ttm_base_object *base)
649 return &(container_of(base, struct vmw_user_surface,
650 prime.base)->srf.res);
654 * vmw_user_surface_free - User visible surface resource destructor
656 * @res: A struct vmw_resource embedded in a struct vmw_surface.
658 static void vmw_user_surface_free(struct vmw_resource *res)
660 struct vmw_surface *srf = vmw_res_to_srf(res);
661 struct vmw_user_surface *user_srf =
662 container_of(srf, struct vmw_user_surface, srf);
664 WARN_ON_ONCE(res->dirty);
665 if (user_srf->master)
666 drm_master_put(&user_srf->master);
668 kfree(srf->metadata.sizes);
669 kfree(srf->snooper.image);
670 ttm_prime_object_kfree(user_srf, prime);
674 * vmw_user_surface_base_release - User visible surface TTM base object destructor
676 * @p_base: Pointer to a pointer to a TTM base object
677 * embedded in a struct vmw_user_surface.
679 * Drops the base object's reference on its resource, and the
680 * pointer pointed to by *p_base is set to NULL.
682 static void vmw_user_surface_base_release(struct ttm_base_object **p_base)
684 struct ttm_base_object *base = *p_base;
685 struct vmw_user_surface *user_srf =
686 container_of(base, struct vmw_user_surface, prime.base);
687 struct vmw_resource *res = &user_srf->srf.res;
689 if (res->guest_memory_bo)
690 drm_gem_object_put(&res->guest_memory_bo->tbo.base);
693 vmw_resource_unreference(&res);
697 * vmw_surface_destroy_ioctl - Ioctl function implementing
698 * the user surface destroy functionality.
700 * @dev: Pointer to a struct drm_device.
701 * @data: Pointer to data copied from / to user-space.
702 * @file_priv: Pointer to a drm file private structure.
704 int vmw_surface_destroy_ioctl(struct drm_device *dev, void *data,
705 struct drm_file *file_priv)
707 struct drm_vmw_surface_arg *arg = (struct drm_vmw_surface_arg *)data;
708 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
710 return ttm_ref_object_base_unref(tfile, arg->sid);
714 * vmw_surface_define_ioctl - Ioctl function implementing
715 * the user surface define functionality.
717 * @dev: Pointer to a struct drm_device.
718 * @data: Pointer to data copied from / to user-space.
719 * @file_priv: Pointer to a drm file private structure.
721 int vmw_surface_define_ioctl(struct drm_device *dev, void *data,
722 struct drm_file *file_priv)
724 struct vmw_private *dev_priv = vmw_priv(dev);
725 struct vmw_user_surface *user_srf;
726 struct vmw_surface *srf;
727 struct vmw_surface_metadata *metadata;
728 struct vmw_resource *res;
729 struct vmw_resource *tmp;
730 union drm_vmw_surface_create_arg *arg =
731 (union drm_vmw_surface_create_arg *)data;
732 struct drm_vmw_surface_create_req *req = &arg->req;
733 struct drm_vmw_surface_arg *rep = &arg->rep;
734 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
737 uint32_t cur_bo_offset;
738 struct drm_vmw_size *cur_size;
739 struct vmw_surface_offset *cur_offset;
741 const SVGA3dSurfaceDesc *desc;
744 for (i = 0; i < DRM_VMW_MAX_SURFACE_FACES; ++i) {
745 if (req->mip_levels[i] > DRM_VMW_MAX_MIP_LEVELS)
747 num_sizes += req->mip_levels[i];
750 if (num_sizes > DRM_VMW_MAX_SURFACE_FACES * DRM_VMW_MAX_MIP_LEVELS ||
754 desc = vmw_surface_get_desc(req->format);
755 if (unlikely(desc->blockDesc == SVGA3DBLOCKDESC_NONE)) {
756 VMW_DEBUG_USER("Invalid format %d for surface creation.\n",
761 user_srf = kzalloc(sizeof(*user_srf), GFP_KERNEL);
762 if (unlikely(!user_srf)) {
767 srf = &user_srf->srf;
768 metadata = &srf->metadata;
771 /* Driver internally stores as 64-bit flags */
772 metadata->flags = (SVGA3dSurfaceAllFlags)req->flags;
773 metadata->format = req->format;
774 metadata->scanout = req->scanout;
776 memcpy(metadata->mip_levels, req->mip_levels,
777 sizeof(metadata->mip_levels));
778 metadata->num_sizes = num_sizes;
780 memdup_user((struct drm_vmw_size __user *)(unsigned long)
782 sizeof(*metadata->sizes) * metadata->num_sizes);
783 if (IS_ERR(metadata->sizes)) {
784 ret = PTR_ERR(metadata->sizes);
787 srf->offsets = kmalloc_array(metadata->num_sizes, sizeof(*srf->offsets),
789 if (unlikely(!srf->offsets)) {
794 metadata->base_size = *srf->metadata.sizes;
795 metadata->autogen_filter = SVGA3D_TEX_FILTER_NONE;
796 metadata->multisample_count = 0;
797 metadata->multisample_pattern = SVGA3D_MS_PATTERN_NONE;
798 metadata->quality_level = SVGA3D_MS_QUALITY_NONE;
801 cur_offset = srf->offsets;
802 cur_size = metadata->sizes;
804 for (i = 0; i < DRM_VMW_MAX_SURFACE_FACES; ++i) {
805 for (j = 0; j < metadata->mip_levels[i]; ++j) {
806 uint32_t stride = vmw_surface_calculate_pitch(
809 cur_offset->face = i;
811 cur_offset->bo_offset = cur_bo_offset;
812 cur_bo_offset += vmw_surface_get_image_buffer_size
813 (desc, cur_size, stride);
818 res->guest_memory_size = cur_bo_offset;
819 if (metadata->scanout &&
820 metadata->num_sizes == 1 &&
821 metadata->sizes[0].width == VMW_CURSOR_SNOOP_WIDTH &&
822 metadata->sizes[0].height == VMW_CURSOR_SNOOP_HEIGHT &&
823 metadata->format == VMW_CURSOR_SNOOP_FORMAT) {
824 const struct SVGA3dSurfaceDesc *desc =
825 vmw_surface_get_desc(VMW_CURSOR_SNOOP_FORMAT);
826 const u32 cursor_size_bytes = VMW_CURSOR_SNOOP_WIDTH *
827 VMW_CURSOR_SNOOP_HEIGHT *
828 desc->pitchBytesPerBlock;
829 srf->snooper.image = kzalloc(cursor_size_bytes, GFP_KERNEL);
830 if (!srf->snooper.image) {
831 DRM_ERROR("Failed to allocate cursor_image\n");
836 srf->snooper.image = NULL;
839 user_srf->prime.base.shareable = false;
840 user_srf->prime.base.tfile = NULL;
841 if (drm_is_primary_client(file_priv))
842 user_srf->master = drm_file_get_master(file_priv);
845 * From this point, the generic resource management functions
846 * destroy the object on failure.
849 ret = vmw_surface_init(dev_priv, srf, vmw_user_surface_free);
850 if (unlikely(ret != 0))
854 * A gb-aware client referencing a shared surface will
855 * expect a backup buffer to be present.
857 if (dev_priv->has_mob && req->shareable) {
858 uint32_t backup_handle;
860 ret = vmw_gem_object_create_with_handle(dev_priv,
862 res->guest_memory_size,
864 &res->guest_memory_bo);
865 if (unlikely(ret != 0)) {
866 vmw_resource_unreference(&res);
869 vmw_bo_reference(res->guest_memory_bo);
871 * We don't expose the handle to the userspace and surface
872 * already holds a gem reference
874 drm_gem_handle_delete(file_priv, backup_handle);
877 tmp = vmw_resource_reference(&srf->res);
878 ret = ttm_prime_object_init(tfile, res->guest_memory_size, &user_srf->prime,
879 req->shareable, VMW_RES_SURFACE,
880 &vmw_user_surface_base_release);
882 if (unlikely(ret != 0)) {
883 vmw_resource_unreference(&tmp);
884 vmw_resource_unreference(&res);
888 rep->sid = user_srf->prime.base.handle;
889 vmw_resource_unreference(&res);
895 kfree(metadata->sizes);
897 ttm_prime_object_kfree(user_srf, prime);
904 vmw_surface_handle_reference(struct vmw_private *dev_priv,
905 struct drm_file *file_priv,
907 enum drm_vmw_handle_type handle_type,
908 struct ttm_base_object **base_p)
910 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
911 struct vmw_user_surface *user_srf;
913 struct ttm_base_object *base;
916 if (handle_type == DRM_VMW_HANDLE_PRIME) {
917 ret = ttm_prime_fd_to_handle(tfile, u_handle, &handle);
918 if (unlikely(ret != 0))
925 base = ttm_base_object_lookup_for_ref(dev_priv->tdev, handle);
926 if (unlikely(!base)) {
927 VMW_DEBUG_USER("Could not find surface to reference.\n");
931 if (unlikely(ttm_base_object_type(base) != VMW_RES_SURFACE)) {
932 VMW_DEBUG_USER("Referenced object is not a surface.\n");
933 goto out_bad_resource;
935 if (handle_type != DRM_VMW_HANDLE_PRIME) {
936 bool require_exist = false;
938 user_srf = container_of(base, struct vmw_user_surface,
941 /* Error out if we are unauthenticated primary */
942 if (drm_is_primary_client(file_priv) &&
943 !file_priv->authenticated) {
945 goto out_bad_resource;
949 * Make sure the surface creator has the same
950 * authenticating master, or is already registered with us.
952 if (drm_is_primary_client(file_priv) &&
953 user_srf->master != file_priv->master)
954 require_exist = true;
956 if (unlikely(drm_is_render_client(file_priv)))
957 require_exist = true;
959 ret = ttm_ref_object_add(tfile, base, NULL, require_exist);
960 if (unlikely(ret != 0)) {
961 DRM_ERROR("Could not add a reference to a surface.\n");
962 goto out_bad_resource;
970 ttm_base_object_unref(&base);
972 if (handle_type == DRM_VMW_HANDLE_PRIME)
973 (void) ttm_ref_object_base_unref(tfile, handle);
979 * vmw_surface_reference_ioctl - Ioctl function implementing
980 * the user surface reference functionality.
982 * @dev: Pointer to a struct drm_device.
983 * @data: Pointer to data copied from / to user-space.
984 * @file_priv: Pointer to a drm file private structure.
986 int vmw_surface_reference_ioctl(struct drm_device *dev, void *data,
987 struct drm_file *file_priv)
989 struct vmw_private *dev_priv = vmw_priv(dev);
990 union drm_vmw_surface_reference_arg *arg =
991 (union drm_vmw_surface_reference_arg *)data;
992 struct drm_vmw_surface_arg *req = &arg->req;
993 struct drm_vmw_surface_create_req *rep = &arg->rep;
994 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
995 struct vmw_surface *srf;
996 struct vmw_user_surface *user_srf;
997 struct drm_vmw_size __user *user_sizes;
998 struct ttm_base_object *base;
1001 ret = vmw_surface_handle_reference(dev_priv, file_priv, req->sid,
1002 req->handle_type, &base);
1003 if (unlikely(ret != 0))
1006 user_srf = container_of(base, struct vmw_user_surface, prime.base);
1007 srf = &user_srf->srf;
1009 /* Downcast of flags when sending back to user space */
1010 rep->flags = (uint32_t)srf->metadata.flags;
1011 rep->format = srf->metadata.format;
1012 memcpy(rep->mip_levels, srf->metadata.mip_levels,
1013 sizeof(srf->metadata.mip_levels));
1014 user_sizes = (struct drm_vmw_size __user *)(unsigned long)
1018 ret = copy_to_user(user_sizes, &srf->metadata.base_size,
1019 sizeof(srf->metadata.base_size));
1020 if (unlikely(ret != 0)) {
1021 VMW_DEBUG_USER("copy_to_user failed %p %u\n", user_sizes,
1022 srf->metadata.num_sizes);
1023 ttm_ref_object_base_unref(tfile, base->handle);
1027 ttm_base_object_unref(&base);
1033 * vmw_gb_surface_create - Encode a surface_define command.
1035 * @res: Pointer to a struct vmw_resource embedded in a struct
1038 static int vmw_gb_surface_create(struct vmw_resource *res)
1040 struct vmw_private *dev_priv = res->dev_priv;
1041 struct vmw_surface *srf = vmw_res_to_srf(res);
1042 struct vmw_surface_metadata *metadata = &srf->metadata;
1043 uint32_t cmd_len, cmd_id, submit_len;
1046 SVGA3dCmdHeader header;
1047 SVGA3dCmdDefineGBSurface body;
1050 SVGA3dCmdHeader header;
1051 SVGA3dCmdDefineGBSurface_v2 body;
1054 SVGA3dCmdHeader header;
1055 SVGA3dCmdDefineGBSurface_v3 body;
1058 SVGA3dCmdHeader header;
1059 SVGA3dCmdDefineGBSurface_v4 body;
1062 if (likely(res->id != -1))
1065 vmw_fifo_resource_inc(dev_priv);
1066 ret = vmw_resource_alloc_id(res);
1067 if (unlikely(ret != 0)) {
1068 DRM_ERROR("Failed to allocate a surface id.\n");
1072 if (unlikely(res->id >= VMWGFX_NUM_GB_SURFACE)) {
1077 if (has_sm5_context(dev_priv) && metadata->array_size > 0) {
1078 cmd_id = SVGA_3D_CMD_DEFINE_GB_SURFACE_V4;
1079 cmd_len = sizeof(cmd4->body);
1080 submit_len = sizeof(*cmd4);
1081 } else if (has_sm4_1_context(dev_priv) && metadata->array_size > 0) {
1082 cmd_id = SVGA_3D_CMD_DEFINE_GB_SURFACE_V3;
1083 cmd_len = sizeof(cmd3->body);
1084 submit_len = sizeof(*cmd3);
1085 } else if (metadata->array_size > 0) {
1086 /* VMW_SM_4 support verified at creation time. */
1087 cmd_id = SVGA_3D_CMD_DEFINE_GB_SURFACE_V2;
1088 cmd_len = sizeof(cmd2->body);
1089 submit_len = sizeof(*cmd2);
1091 cmd_id = SVGA_3D_CMD_DEFINE_GB_SURFACE;
1092 cmd_len = sizeof(cmd->body);
1093 submit_len = sizeof(*cmd);
1096 cmd = VMW_CMD_RESERVE(dev_priv, submit_len);
1097 cmd2 = (typeof(cmd2))cmd;
1098 cmd3 = (typeof(cmd3))cmd;
1099 cmd4 = (typeof(cmd4))cmd;
1100 if (unlikely(!cmd)) {
1105 if (has_sm5_context(dev_priv) && metadata->array_size > 0) {
1106 cmd4->header.id = cmd_id;
1107 cmd4->header.size = cmd_len;
1108 cmd4->body.sid = srf->res.id;
1109 cmd4->body.surfaceFlags = metadata->flags;
1110 cmd4->body.format = metadata->format;
1111 cmd4->body.numMipLevels = metadata->mip_levels[0];
1112 cmd4->body.multisampleCount = metadata->multisample_count;
1113 cmd4->body.multisamplePattern = metadata->multisample_pattern;
1114 cmd4->body.qualityLevel = metadata->quality_level;
1115 cmd4->body.autogenFilter = metadata->autogen_filter;
1116 cmd4->body.size.width = metadata->base_size.width;
1117 cmd4->body.size.height = metadata->base_size.height;
1118 cmd4->body.size.depth = metadata->base_size.depth;
1119 cmd4->body.arraySize = metadata->array_size;
1120 cmd4->body.bufferByteStride = metadata->buffer_byte_stride;
1121 } else if (has_sm4_1_context(dev_priv) && metadata->array_size > 0) {
1122 cmd3->header.id = cmd_id;
1123 cmd3->header.size = cmd_len;
1124 cmd3->body.sid = srf->res.id;
1125 cmd3->body.surfaceFlags = metadata->flags;
1126 cmd3->body.format = metadata->format;
1127 cmd3->body.numMipLevels = metadata->mip_levels[0];
1128 cmd3->body.multisampleCount = metadata->multisample_count;
1129 cmd3->body.multisamplePattern = metadata->multisample_pattern;
1130 cmd3->body.qualityLevel = metadata->quality_level;
1131 cmd3->body.autogenFilter = metadata->autogen_filter;
1132 cmd3->body.size.width = metadata->base_size.width;
1133 cmd3->body.size.height = metadata->base_size.height;
1134 cmd3->body.size.depth = metadata->base_size.depth;
1135 cmd3->body.arraySize = metadata->array_size;
1136 } else if (metadata->array_size > 0) {
1137 cmd2->header.id = cmd_id;
1138 cmd2->header.size = cmd_len;
1139 cmd2->body.sid = srf->res.id;
1140 cmd2->body.surfaceFlags = metadata->flags;
1141 cmd2->body.format = metadata->format;
1142 cmd2->body.numMipLevels = metadata->mip_levels[0];
1143 cmd2->body.multisampleCount = metadata->multisample_count;
1144 cmd2->body.autogenFilter = metadata->autogen_filter;
1145 cmd2->body.size.width = metadata->base_size.width;
1146 cmd2->body.size.height = metadata->base_size.height;
1147 cmd2->body.size.depth = metadata->base_size.depth;
1148 cmd2->body.arraySize = metadata->array_size;
1150 cmd->header.id = cmd_id;
1151 cmd->header.size = cmd_len;
1152 cmd->body.sid = srf->res.id;
1153 cmd->body.surfaceFlags = metadata->flags;
1154 cmd->body.format = metadata->format;
1155 cmd->body.numMipLevels = metadata->mip_levels[0];
1156 cmd->body.multisampleCount = metadata->multisample_count;
1157 cmd->body.autogenFilter = metadata->autogen_filter;
1158 cmd->body.size.width = metadata->base_size.width;
1159 cmd->body.size.height = metadata->base_size.height;
1160 cmd->body.size.depth = metadata->base_size.depth;
1163 vmw_cmd_commit(dev_priv, submit_len);
1168 vmw_resource_release_id(res);
1170 vmw_fifo_resource_dec(dev_priv);
1175 static int vmw_gb_surface_bind(struct vmw_resource *res,
1176 struct ttm_validate_buffer *val_buf)
1178 struct vmw_private *dev_priv = res->dev_priv;
1180 SVGA3dCmdHeader header;
1181 SVGA3dCmdBindGBSurface body;
1184 SVGA3dCmdHeader header;
1185 SVGA3dCmdUpdateGBSurface body;
1187 uint32_t submit_size;
1188 struct ttm_buffer_object *bo = val_buf->bo;
1190 BUG_ON(bo->resource->mem_type != VMW_PL_MOB);
1192 submit_size = sizeof(*cmd1) + (res->guest_memory_dirty ? sizeof(*cmd2) : 0);
1194 cmd1 = VMW_CMD_RESERVE(dev_priv, submit_size);
1195 if (unlikely(!cmd1))
1198 cmd1->header.id = SVGA_3D_CMD_BIND_GB_SURFACE;
1199 cmd1->header.size = sizeof(cmd1->body);
1200 cmd1->body.sid = res->id;
1201 cmd1->body.mobid = bo->resource->start;
1202 if (res->guest_memory_dirty) {
1203 cmd2 = (void *) &cmd1[1];
1204 cmd2->header.id = SVGA_3D_CMD_UPDATE_GB_SURFACE;
1205 cmd2->header.size = sizeof(cmd2->body);
1206 cmd2->body.sid = res->id;
1208 vmw_cmd_commit(dev_priv, submit_size);
1210 if (res->guest_memory_bo->dirty && res->guest_memory_dirty) {
1211 /* We've just made a full upload. Cear dirty regions. */
1212 vmw_bo_dirty_clear_res(res);
1215 res->guest_memory_dirty = false;
1220 static int vmw_gb_surface_unbind(struct vmw_resource *res,
1222 struct ttm_validate_buffer *val_buf)
1224 struct vmw_private *dev_priv = res->dev_priv;
1225 struct ttm_buffer_object *bo = val_buf->bo;
1226 struct vmw_fence_obj *fence;
1229 SVGA3dCmdHeader header;
1230 SVGA3dCmdReadbackGBSurface body;
1233 SVGA3dCmdHeader header;
1234 SVGA3dCmdInvalidateGBSurface body;
1237 SVGA3dCmdHeader header;
1238 SVGA3dCmdBindGBSurface body;
1240 uint32_t submit_size;
1244 BUG_ON(bo->resource->mem_type != VMW_PL_MOB);
1246 submit_size = sizeof(*cmd3) + (readback ? sizeof(*cmd1) : sizeof(*cmd2));
1247 cmd = VMW_CMD_RESERVE(dev_priv, submit_size);
1252 cmd1 = (void *) cmd;
1253 cmd1->header.id = SVGA_3D_CMD_READBACK_GB_SURFACE;
1254 cmd1->header.size = sizeof(cmd1->body);
1255 cmd1->body.sid = res->id;
1256 cmd3 = (void *) &cmd1[1];
1258 cmd2 = (void *) cmd;
1259 cmd2->header.id = SVGA_3D_CMD_INVALIDATE_GB_SURFACE;
1260 cmd2->header.size = sizeof(cmd2->body);
1261 cmd2->body.sid = res->id;
1262 cmd3 = (void *) &cmd2[1];
1265 cmd3->header.id = SVGA_3D_CMD_BIND_GB_SURFACE;
1266 cmd3->header.size = sizeof(cmd3->body);
1267 cmd3->body.sid = res->id;
1268 cmd3->body.mobid = SVGA3D_INVALID_ID;
1270 vmw_cmd_commit(dev_priv, submit_size);
1273 * Create a fence object and fence the backup buffer.
1276 (void) vmw_execbuf_fence_commands(NULL, dev_priv,
1279 vmw_bo_fence_single(val_buf->bo, fence);
1281 if (likely(fence != NULL))
1282 vmw_fence_obj_unreference(&fence);
1287 static int vmw_gb_surface_destroy(struct vmw_resource *res)
1289 struct vmw_private *dev_priv = res->dev_priv;
1290 struct vmw_surface *srf = vmw_res_to_srf(res);
1292 SVGA3dCmdHeader header;
1293 SVGA3dCmdDestroyGBSurface body;
1296 if (likely(res->id == -1))
1299 mutex_lock(&dev_priv->binding_mutex);
1300 vmw_view_surface_list_destroy(dev_priv, &srf->view_list);
1301 vmw_binding_res_list_scrub(&res->binding_head);
1303 cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
1304 if (unlikely(!cmd)) {
1305 mutex_unlock(&dev_priv->binding_mutex);
1309 cmd->header.id = SVGA_3D_CMD_DESTROY_GB_SURFACE;
1310 cmd->header.size = sizeof(cmd->body);
1311 cmd->body.sid = res->id;
1312 vmw_cmd_commit(dev_priv, sizeof(*cmd));
1313 mutex_unlock(&dev_priv->binding_mutex);
1314 vmw_resource_release_id(res);
1315 vmw_fifo_resource_dec(dev_priv);
1321 * vmw_gb_surface_define_ioctl - Ioctl function implementing
1322 * the user surface define functionality.
1324 * @dev: Pointer to a struct drm_device.
1325 * @data: Pointer to data copied from / to user-space.
1326 * @file_priv: Pointer to a drm file private structure.
1328 int vmw_gb_surface_define_ioctl(struct drm_device *dev, void *data,
1329 struct drm_file *file_priv)
1331 union drm_vmw_gb_surface_create_arg *arg =
1332 (union drm_vmw_gb_surface_create_arg *)data;
1333 struct drm_vmw_gb_surface_create_rep *rep = &arg->rep;
1334 struct drm_vmw_gb_surface_create_ext_req req_ext;
1336 req_ext.base = arg->req;
1337 req_ext.version = drm_vmw_gb_surface_v1;
1338 req_ext.svga3d_flags_upper_32_bits = 0;
1339 req_ext.multisample_pattern = SVGA3D_MS_PATTERN_NONE;
1340 req_ext.quality_level = SVGA3D_MS_QUALITY_NONE;
1341 req_ext.buffer_byte_stride = 0;
1342 req_ext.must_be_zero = 0;
1344 return vmw_gb_surface_define_internal(dev, &req_ext, rep, file_priv);
1348 * vmw_gb_surface_reference_ioctl - Ioctl function implementing
1349 * the user surface reference functionality.
1351 * @dev: Pointer to a struct drm_device.
1352 * @data: Pointer to data copied from / to user-space.
1353 * @file_priv: Pointer to a drm file private structure.
1355 int vmw_gb_surface_reference_ioctl(struct drm_device *dev, void *data,
1356 struct drm_file *file_priv)
1358 union drm_vmw_gb_surface_reference_arg *arg =
1359 (union drm_vmw_gb_surface_reference_arg *)data;
1360 struct drm_vmw_surface_arg *req = &arg->req;
1361 struct drm_vmw_gb_surface_ref_rep *rep = &arg->rep;
1362 struct drm_vmw_gb_surface_ref_ext_rep rep_ext;
1365 ret = vmw_gb_surface_reference_internal(dev, req, &rep_ext, file_priv);
1367 if (unlikely(ret != 0))
1370 rep->creq = rep_ext.creq.base;
1371 rep->crep = rep_ext.crep;
1377 * vmw_gb_surface_define_ext_ioctl - Ioctl function implementing
1378 * the user surface define functionality.
1380 * @dev: Pointer to a struct drm_device.
1381 * @data: Pointer to data copied from / to user-space.
1382 * @file_priv: Pointer to a drm file private structure.
1384 int vmw_gb_surface_define_ext_ioctl(struct drm_device *dev, void *data,
1385 struct drm_file *file_priv)
1387 union drm_vmw_gb_surface_create_ext_arg *arg =
1388 (union drm_vmw_gb_surface_create_ext_arg *)data;
1389 struct drm_vmw_gb_surface_create_ext_req *req = &arg->req;
1390 struct drm_vmw_gb_surface_create_rep *rep = &arg->rep;
1392 return vmw_gb_surface_define_internal(dev, req, rep, file_priv);
1396 * vmw_gb_surface_reference_ext_ioctl - Ioctl function implementing
1397 * the user surface reference functionality.
1399 * @dev: Pointer to a struct drm_device.
1400 * @data: Pointer to data copied from / to user-space.
1401 * @file_priv: Pointer to a drm file private structure.
1403 int vmw_gb_surface_reference_ext_ioctl(struct drm_device *dev, void *data,
1404 struct drm_file *file_priv)
1406 union drm_vmw_gb_surface_reference_ext_arg *arg =
1407 (union drm_vmw_gb_surface_reference_ext_arg *)data;
1408 struct drm_vmw_surface_arg *req = &arg->req;
1409 struct drm_vmw_gb_surface_ref_ext_rep *rep = &arg->rep;
1411 return vmw_gb_surface_reference_internal(dev, req, rep, file_priv);
1415 * vmw_gb_surface_define_internal - Ioctl function implementing
1416 * the user surface define functionality.
1418 * @dev: Pointer to a struct drm_device.
1419 * @req: Request argument from user-space.
1420 * @rep: Response argument to user-space.
1421 * @file_priv: Pointer to a drm file private structure.
1424 vmw_gb_surface_define_internal(struct drm_device *dev,
1425 struct drm_vmw_gb_surface_create_ext_req *req,
1426 struct drm_vmw_gb_surface_create_rep *rep,
1427 struct drm_file *file_priv)
1429 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1430 struct vmw_private *dev_priv = vmw_priv(dev);
1431 struct vmw_user_surface *user_srf;
1432 struct vmw_surface_metadata metadata = {0};
1433 struct vmw_surface *srf;
1434 struct vmw_resource *res;
1435 struct vmw_resource *tmp;
1437 uint32_t backup_handle = 0;
1438 SVGA3dSurfaceAllFlags svga3d_flags_64 =
1439 SVGA3D_FLAGS_64(req->svga3d_flags_upper_32_bits,
1440 req->base.svga3d_flags);
1442 /* array_size must be null for non-GL3 host. */
1443 if (req->base.array_size > 0 && !has_sm4_context(dev_priv)) {
1444 VMW_DEBUG_USER("SM4 surface not supported.\n");
1448 if (!has_sm4_1_context(dev_priv)) {
1449 if (req->svga3d_flags_upper_32_bits != 0)
1452 if (req->base.multisample_count != 0)
1455 if (req->multisample_pattern != SVGA3D_MS_PATTERN_NONE)
1458 if (req->quality_level != SVGA3D_MS_QUALITY_NONE)
1462 VMW_DEBUG_USER("SM4.1 surface not supported.\n");
1467 if (req->buffer_byte_stride > 0 && !has_sm5_context(dev_priv)) {
1468 VMW_DEBUG_USER("SM5 surface not supported.\n");
1472 if ((svga3d_flags_64 & SVGA3D_SURFACE_MULTISAMPLE) &&
1473 req->base.multisample_count == 0) {
1474 VMW_DEBUG_USER("Invalid sample count.\n");
1478 if (req->base.mip_levels > DRM_VMW_MAX_MIP_LEVELS) {
1479 VMW_DEBUG_USER("Invalid mip level.\n");
1483 metadata.flags = svga3d_flags_64;
1484 metadata.format = req->base.format;
1485 metadata.mip_levels[0] = req->base.mip_levels;
1486 metadata.multisample_count = req->base.multisample_count;
1487 metadata.multisample_pattern = req->multisample_pattern;
1488 metadata.quality_level = req->quality_level;
1489 metadata.array_size = req->base.array_size;
1490 metadata.buffer_byte_stride = req->buffer_byte_stride;
1491 metadata.num_sizes = 1;
1492 metadata.base_size = req->base.base_size;
1493 metadata.scanout = req->base.drm_surface_flags &
1494 drm_vmw_surface_flag_scanout;
1496 /* Define a surface based on the parameters. */
1497 ret = vmw_gb_surface_define(dev_priv, &metadata, &srf);
1499 VMW_DEBUG_USER("Failed to define surface.\n");
1503 user_srf = container_of(srf, struct vmw_user_surface, srf);
1504 if (drm_is_primary_client(file_priv))
1505 user_srf->master = drm_file_get_master(file_priv);
1507 res = &user_srf->srf.res;
1509 if (req->base.buffer_handle != SVGA3D_INVALID_ID) {
1510 ret = vmw_user_bo_lookup(file_priv, req->base.buffer_handle,
1511 &res->guest_memory_bo);
1513 if (res->guest_memory_bo->tbo.base.size < res->guest_memory_size) {
1514 VMW_DEBUG_USER("Surface backup buffer too small.\n");
1515 vmw_bo_unreference(&res->guest_memory_bo);
1519 backup_handle = req->base.buffer_handle;
1522 } else if (req->base.drm_surface_flags &
1523 (drm_vmw_surface_flag_create_buffer |
1524 drm_vmw_surface_flag_coherent)) {
1525 ret = vmw_gem_object_create_with_handle(dev_priv, file_priv,
1526 res->guest_memory_size,
1528 &res->guest_memory_bo);
1530 vmw_bo_reference(res->guest_memory_bo);
1533 if (unlikely(ret != 0)) {
1534 vmw_resource_unreference(&res);
1538 if (req->base.drm_surface_flags & drm_vmw_surface_flag_coherent) {
1539 struct vmw_bo *backup = res->guest_memory_bo;
1541 ttm_bo_reserve(&backup->tbo, false, false, NULL);
1542 if (!res->func->dirty_alloc)
1545 ret = vmw_bo_dirty_add(backup);
1547 res->coherent = true;
1548 ret = res->func->dirty_alloc(res);
1550 ttm_bo_unreserve(&backup->tbo);
1552 vmw_resource_unreference(&res);
1558 tmp = vmw_resource_reference(res);
1559 ret = ttm_prime_object_init(tfile, res->guest_memory_size, &user_srf->prime,
1560 req->base.drm_surface_flags &
1561 drm_vmw_surface_flag_shareable,
1563 &vmw_user_surface_base_release);
1565 if (unlikely(ret != 0)) {
1566 vmw_resource_unreference(&tmp);
1567 vmw_resource_unreference(&res);
1571 rep->handle = user_srf->prime.base.handle;
1572 rep->backup_size = res->guest_memory_size;
1573 if (res->guest_memory_bo) {
1574 rep->buffer_map_handle =
1575 drm_vma_node_offset_addr(&res->guest_memory_bo->tbo.base.vma_node);
1576 rep->buffer_size = res->guest_memory_bo->tbo.base.size;
1577 rep->buffer_handle = backup_handle;
1579 rep->buffer_map_handle = 0;
1580 rep->buffer_size = 0;
1581 rep->buffer_handle = SVGA3D_INVALID_ID;
1583 vmw_resource_unreference(&res);
1590 * vmw_gb_surface_reference_internal - Ioctl function implementing
1591 * the user surface reference functionality.
1593 * @dev: Pointer to a struct drm_device.
1594 * @req: Pointer to user-space request surface arg.
1595 * @rep: Pointer to response to user-space.
1596 * @file_priv: Pointer to a drm file private structure.
1599 vmw_gb_surface_reference_internal(struct drm_device *dev,
1600 struct drm_vmw_surface_arg *req,
1601 struct drm_vmw_gb_surface_ref_ext_rep *rep,
1602 struct drm_file *file_priv)
1604 struct vmw_private *dev_priv = vmw_priv(dev);
1605 struct vmw_surface *srf;
1606 struct vmw_user_surface *user_srf;
1607 struct vmw_surface_metadata *metadata;
1608 struct ttm_base_object *base;
1612 ret = vmw_surface_handle_reference(dev_priv, file_priv, req->sid,
1613 req->handle_type, &base);
1614 if (unlikely(ret != 0))
1617 user_srf = container_of(base, struct vmw_user_surface, prime.base);
1618 srf = &user_srf->srf;
1619 if (!srf->res.guest_memory_bo) {
1620 DRM_ERROR("Shared GB surface is missing a backup buffer.\n");
1621 goto out_bad_resource;
1623 metadata = &srf->metadata;
1625 mutex_lock(&dev_priv->cmdbuf_mutex); /* Protect res->backup */
1626 ret = drm_gem_handle_create(file_priv, &srf->res.guest_memory_bo->tbo.base,
1628 mutex_unlock(&dev_priv->cmdbuf_mutex);
1630 drm_err(dev, "Wasn't able to create a backing handle for surface sid = %u.\n",
1632 goto out_bad_resource;
1635 rep->creq.base.svga3d_flags = SVGA3D_FLAGS_LOWER_32(metadata->flags);
1636 rep->creq.base.format = metadata->format;
1637 rep->creq.base.mip_levels = metadata->mip_levels[0];
1638 rep->creq.base.drm_surface_flags = 0;
1639 rep->creq.base.multisample_count = metadata->multisample_count;
1640 rep->creq.base.autogen_filter = metadata->autogen_filter;
1641 rep->creq.base.array_size = metadata->array_size;
1642 rep->creq.base.buffer_handle = backup_handle;
1643 rep->creq.base.base_size = metadata->base_size;
1644 rep->crep.handle = user_srf->prime.base.handle;
1645 rep->crep.backup_size = srf->res.guest_memory_size;
1646 rep->crep.buffer_handle = backup_handle;
1647 rep->crep.buffer_map_handle =
1648 drm_vma_node_offset_addr(&srf->res.guest_memory_bo->tbo.base.vma_node);
1649 rep->crep.buffer_size = srf->res.guest_memory_bo->tbo.base.size;
1651 rep->creq.version = drm_vmw_gb_surface_v1;
1652 rep->creq.svga3d_flags_upper_32_bits =
1653 SVGA3D_FLAGS_UPPER_32(metadata->flags);
1654 rep->creq.multisample_pattern = metadata->multisample_pattern;
1655 rep->creq.quality_level = metadata->quality_level;
1656 rep->creq.must_be_zero = 0;
1659 ttm_base_object_unref(&base);
1665 * vmw_subres_dirty_add - Add a dirty region to a subresource
1666 * @dirty: The surfaces's dirty tracker.
1667 * @loc_start: The location corresponding to the start of the region.
1668 * @loc_end: The location corresponding to the end of the region.
1670 * As we are assuming that @loc_start and @loc_end represent a sequential
1671 * range of backing store memory, if the region spans multiple lines then
1672 * regardless of the x coordinate, the full lines are dirtied.
1673 * Correspondingly if the region spans multiple z slices, then full rather
1674 * than partial z slices are dirtied.
1676 static void vmw_subres_dirty_add(struct vmw_surface_dirty *dirty,
1677 const struct vmw_surface_loc *loc_start,
1678 const struct vmw_surface_loc *loc_end)
1680 const struct vmw_surface_cache *cache = &dirty->cache;
1681 SVGA3dBox *box = &dirty->boxes[loc_start->sub_resource];
1682 u32 mip = loc_start->sub_resource % cache->num_mip_levels;
1683 const struct drm_vmw_size *size = &cache->mip[mip].size;
1684 u32 box_c2 = box->z + box->d;
1686 if (WARN_ON(loc_start->sub_resource >= dirty->num_subres))
1689 if (box->d == 0 || box->z > loc_start->z)
1690 box->z = loc_start->z;
1691 if (box_c2 < loc_end->z)
1692 box->d = loc_end->z - box->z;
1694 if (loc_start->z + 1 == loc_end->z) {
1695 box_c2 = box->y + box->h;
1696 if (box->h == 0 || box->y > loc_start->y)
1697 box->y = loc_start->y;
1698 if (box_c2 < loc_end->y)
1699 box->h = loc_end->y - box->y;
1701 if (loc_start->y + 1 == loc_end->y) {
1702 box_c2 = box->x + box->w;
1703 if (box->w == 0 || box->x > loc_start->x)
1704 box->x = loc_start->x;
1705 if (box_c2 < loc_end->x)
1706 box->w = loc_end->x - box->x;
1709 box->w = size->width;
1713 box->h = size->height;
1715 box->w = size->width;
1720 * vmw_subres_dirty_full - Mark a full subresource as dirty
1721 * @dirty: The surface's dirty tracker.
1722 * @subres: The subresource
1724 static void vmw_subres_dirty_full(struct vmw_surface_dirty *dirty, u32 subres)
1726 const struct vmw_surface_cache *cache = &dirty->cache;
1727 u32 mip = subres % cache->num_mip_levels;
1728 const struct drm_vmw_size *size = &cache->mip[mip].size;
1729 SVGA3dBox *box = &dirty->boxes[subres];
1734 box->w = size->width;
1735 box->h = size->height;
1736 box->d = size->depth;
1740 * vmw_surface_tex_dirty_add_range - The dirty_add_range callback for texture
1743 static void vmw_surface_tex_dirty_range_add(struct vmw_resource *res,
1744 size_t start, size_t end)
1746 struct vmw_surface_dirty *dirty =
1747 (struct vmw_surface_dirty *) res->dirty;
1748 size_t backup_end = res->guest_memory_offset + res->guest_memory_size;
1749 struct vmw_surface_loc loc1, loc2;
1750 const struct vmw_surface_cache *cache;
1752 start = max_t(size_t, start, res->guest_memory_offset) - res->guest_memory_offset;
1753 end = min(end, backup_end) - res->guest_memory_offset;
1754 cache = &dirty->cache;
1755 vmw_surface_get_loc(cache, &loc1, start);
1756 vmw_surface_get_loc(cache, &loc2, end - 1);
1757 vmw_surface_inc_loc(cache, &loc2);
1759 if (loc1.sheet != loc2.sheet) {
1763 * Multiple multisample sheets. To do this in an optimized
1764 * fashion, compute the dirty region for each sheet and the
1765 * resulting union. Since this is not a common case, just dirty
1766 * the whole surface.
1768 for (sub_res = 0; sub_res < dirty->num_subres; ++sub_res)
1769 vmw_subres_dirty_full(dirty, sub_res);
1772 if (loc1.sub_resource + 1 == loc2.sub_resource) {
1773 /* Dirty range covers a single sub-resource */
1774 vmw_subres_dirty_add(dirty, &loc1, &loc2);
1776 /* Dirty range covers multiple sub-resources */
1777 struct vmw_surface_loc loc_min, loc_max;
1780 vmw_surface_max_loc(cache, loc1.sub_resource, &loc_max);
1781 vmw_subres_dirty_add(dirty, &loc1, &loc_max);
1782 vmw_surface_min_loc(cache, loc2.sub_resource - 1, &loc_min);
1783 vmw_subres_dirty_add(dirty, &loc_min, &loc2);
1784 for (sub_res = loc1.sub_resource + 1;
1785 sub_res < loc2.sub_resource - 1; ++sub_res)
1786 vmw_subres_dirty_full(dirty, sub_res);
1791 * vmw_surface_tex_dirty_add_range - The dirty_add_range callback for buffer
1794 static void vmw_surface_buf_dirty_range_add(struct vmw_resource *res,
1795 size_t start, size_t end)
1797 struct vmw_surface_dirty *dirty =
1798 (struct vmw_surface_dirty *) res->dirty;
1799 const struct vmw_surface_cache *cache = &dirty->cache;
1800 size_t backup_end = res->guest_memory_offset + cache->mip_chain_bytes;
1801 SVGA3dBox *box = &dirty->boxes[0];
1804 box->h = box->d = 1;
1805 start = max_t(size_t, start, res->guest_memory_offset) - res->guest_memory_offset;
1806 end = min(end, backup_end) - res->guest_memory_offset;
1807 box_c2 = box->x + box->w;
1808 if (box->w == 0 || box->x > start)
1811 box->w = end - box->x;
1815 * vmw_surface_tex_dirty_add_range - The dirty_add_range callback for surfaces
1817 static void vmw_surface_dirty_range_add(struct vmw_resource *res, size_t start,
1820 struct vmw_surface *srf = vmw_res_to_srf(res);
1822 if (WARN_ON(end <= res->guest_memory_offset ||
1823 start >= res->guest_memory_offset + res->guest_memory_size))
1826 if (srf->metadata.format == SVGA3D_BUFFER)
1827 vmw_surface_buf_dirty_range_add(res, start, end);
1829 vmw_surface_tex_dirty_range_add(res, start, end);
1833 * vmw_surface_dirty_sync - The surface's dirty_sync callback.
1835 static int vmw_surface_dirty_sync(struct vmw_resource *res)
1837 struct vmw_private *dev_priv = res->dev_priv;
1839 struct vmw_surface_dirty *dirty =
1840 (struct vmw_surface_dirty *) res->dirty;
1842 const struct vmw_surface_cache *cache = &dirty->cache;
1844 SVGA3dCmdHeader header;
1845 SVGA3dCmdDXUpdateSubResource body;
1848 SVGA3dCmdHeader header;
1849 SVGA3dCmdUpdateGBImage body;
1854 for (i = 0; i < dirty->num_subres; ++i) {
1855 const SVGA3dBox *box = &dirty->boxes[i];
1864 alloc_size = num_dirty * ((has_sm4_context(dev_priv)) ? sizeof(*cmd1) : sizeof(*cmd2));
1865 cmd = VMW_CMD_RESERVE(dev_priv, alloc_size);
1872 for (i = 0; i < dirty->num_subres; ++i) {
1873 const SVGA3dBox *box = &dirty->boxes[i];
1879 * DX_UPDATE_SUBRESOURCE is aware of array surfaces.
1880 * UPDATE_GB_IMAGE is not.
1882 if (has_sm4_context(dev_priv)) {
1883 cmd1->header.id = SVGA_3D_CMD_DX_UPDATE_SUBRESOURCE;
1884 cmd1->header.size = sizeof(cmd1->body);
1885 cmd1->body.sid = res->id;
1886 cmd1->body.subResource = i;
1887 cmd1->body.box = *box;
1890 cmd2->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
1891 cmd2->header.size = sizeof(cmd2->body);
1892 cmd2->body.image.sid = res->id;
1893 cmd2->body.image.face = i / cache->num_mip_levels;
1894 cmd2->body.image.mipmap = i -
1895 (cache->num_mip_levels * cmd2->body.image.face);
1896 cmd2->body.box = *box;
1901 vmw_cmd_commit(dev_priv, alloc_size);
1903 memset(&dirty->boxes[0], 0, sizeof(dirty->boxes[0]) *
1910 * vmw_surface_dirty_alloc - The surface's dirty_alloc callback.
1912 static int vmw_surface_dirty_alloc(struct vmw_resource *res)
1914 struct vmw_surface *srf = vmw_res_to_srf(res);
1915 const struct vmw_surface_metadata *metadata = &srf->metadata;
1916 struct vmw_surface_dirty *dirty;
1924 if (metadata->array_size)
1925 num_layers = metadata->array_size;
1926 else if (metadata->flags & SVGA3D_SURFACE_CUBEMAP)
1927 num_layers *= SVGA3D_MAX_SURFACE_FACES;
1929 num_mip = metadata->mip_levels[0];
1933 num_subres = num_layers * num_mip;
1934 dirty_size = struct_size(dirty, boxes, num_subres);
1936 dirty = kvzalloc(dirty_size, GFP_KERNEL);
1942 num_samples = max_t(u32, 1, metadata->multisample_count);
1943 ret = vmw_surface_setup_cache(&metadata->base_size, metadata->format,
1944 num_mip, num_layers, num_samples,
1949 dirty->num_subres = num_subres;
1950 res->dirty = (struct vmw_resource_dirty *) dirty;
1961 * vmw_surface_dirty_free - The surface's dirty_free callback
1963 static void vmw_surface_dirty_free(struct vmw_resource *res)
1965 struct vmw_surface_dirty *dirty =
1966 (struct vmw_surface_dirty *) res->dirty;
1973 * vmw_surface_clean - The surface's clean callback
1975 static int vmw_surface_clean(struct vmw_resource *res)
1977 struct vmw_private *dev_priv = res->dev_priv;
1980 SVGA3dCmdHeader header;
1981 SVGA3dCmdReadbackGBSurface body;
1984 alloc_size = sizeof(*cmd);
1985 cmd = VMW_CMD_RESERVE(dev_priv, alloc_size);
1989 cmd->header.id = SVGA_3D_CMD_READBACK_GB_SURFACE;
1990 cmd->header.size = sizeof(cmd->body);
1991 cmd->body.sid = res->id;
1992 vmw_cmd_commit(dev_priv, alloc_size);
1998 * vmw_gb_surface_define - Define a private GB surface
2000 * @dev_priv: Pointer to a device private.
2001 * @metadata: Metadata representing the surface to create.
2002 * @user_srf_out: allocated user_srf. Set to NULL on failure.
2004 * GB surfaces allocated by this function will not have a user mode handle, and
2005 * thus will only be visible to vmwgfx. For optimization reasons the
2006 * surface may later be given a user mode handle by another function to make
2007 * it available to user mode drivers.
2009 int vmw_gb_surface_define(struct vmw_private *dev_priv,
2010 const struct vmw_surface_metadata *req,
2011 struct vmw_surface **srf_out)
2013 struct vmw_surface_metadata *metadata;
2014 struct vmw_user_surface *user_srf;
2015 struct vmw_surface *srf;
2016 u32 sample_count = 1;
2023 if (!vmw_surface_is_screen_target_format(req->format)) {
2024 VMW_DEBUG_USER("Invalid Screen Target surface format.");
2028 if (req->base_size.width > dev_priv->texture_max_width ||
2029 req->base_size.height > dev_priv->texture_max_height) {
2030 VMW_DEBUG_USER("%ux%u\n, exceed max surface size %ux%u",
2031 req->base_size.width,
2032 req->base_size.height,
2033 dev_priv->texture_max_width,
2034 dev_priv->texture_max_height);
2038 const SVGA3dSurfaceDesc *desc =
2039 vmw_surface_get_desc(req->format);
2041 if (desc->blockDesc == SVGA3DBLOCKDESC_NONE) {
2042 VMW_DEBUG_USER("Invalid surface format.\n");
2047 if (req->autogen_filter != SVGA3D_TEX_FILTER_NONE)
2050 if (req->num_sizes != 1)
2053 if (req->sizes != NULL)
2056 user_srf = kzalloc(sizeof(*user_srf), GFP_KERNEL);
2057 if (unlikely(!user_srf)) {
2062 *srf_out = &user_srf->srf;
2063 user_srf->prime.base.shareable = false;
2064 user_srf->prime.base.tfile = NULL;
2066 srf = &user_srf->srf;
2067 srf->metadata = *req;
2068 srf->offsets = NULL;
2070 metadata = &srf->metadata;
2072 if (metadata->array_size)
2073 num_layers = req->array_size;
2074 else if (metadata->flags & SVGA3D_SURFACE_CUBEMAP)
2075 num_layers = SVGA3D_MAX_SURFACE_FACES;
2077 if (metadata->flags & SVGA3D_SURFACE_MULTISAMPLE)
2078 sample_count = metadata->multisample_count;
2080 srf->res.guest_memory_size =
2081 vmw_surface_get_serialized_size_extended(
2083 metadata->base_size,
2084 metadata->mip_levels[0],
2088 if (metadata->flags & SVGA3D_SURFACE_BIND_STREAM_OUTPUT)
2089 srf->res.guest_memory_size += sizeof(SVGA3dDXSOState);
2092 * Don't set SVGA3D_SURFACE_SCREENTARGET flag for a scanout surface with
2093 * size greater than STDU max width/height. This is really a workaround
2094 * to support creation of big framebuffer requested by some user-space
2095 * for whole topology. That big framebuffer won't really be used for
2096 * binding with screen target as during prepare_fb a separate surface is
2097 * created so it's safe to ignore SVGA3D_SURFACE_SCREENTARGET flag.
2099 if (dev_priv->active_display_unit == vmw_du_screen_target &&
2100 metadata->scanout &&
2101 metadata->base_size.width <= dev_priv->stdu_max_width &&
2102 metadata->base_size.height <= dev_priv->stdu_max_height)
2103 metadata->flags |= SVGA3D_SURFACE_SCREENTARGET;
2106 * From this point, the generic resource management functions
2107 * destroy the object on failure.
2109 ret = vmw_surface_init(dev_priv, srf, vmw_user_surface_free);