Loading...
1/**************************************************************************
2 *
3 * Copyright © 2009-2015 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28#include "vmwgfx_drv.h"
29#include "vmwgfx_resource_priv.h"
30#include "vmwgfx_so.h"
31#include "vmwgfx_binding.h"
32#include <ttm/ttm_placement.h>
33#include "device_include/svga3d_surfacedefs.h"
34
35
36/**
37 * struct vmw_user_surface - User-space visible surface resource
38 *
39 * @base: The TTM base object handling user-space visibility.
40 * @srf: The surface metadata.
41 * @size: TTM accounting size for the surface.
42 * @master: master of the creating client. Used for security check.
43 */
44struct vmw_user_surface {
45 struct ttm_prime_object prime;
46 struct vmw_surface srf;
47 uint32_t size;
48 struct drm_master *master;
49 struct ttm_base_object *backup_base;
50};
51
52/**
53 * struct vmw_surface_offset - Backing store mip level offset info
54 *
55 * @face: Surface face.
56 * @mip: Mip level.
57 * @bo_offset: Offset into backing store of this mip level.
58 *
59 */
60struct vmw_surface_offset {
61 uint32_t face;
62 uint32_t mip;
63 uint32_t bo_offset;
64};
65
66static void vmw_user_surface_free(struct vmw_resource *res);
67static struct vmw_resource *
68vmw_user_surface_base_to_res(struct ttm_base_object *base);
69static int vmw_legacy_srf_bind(struct vmw_resource *res,
70 struct ttm_validate_buffer *val_buf);
71static int vmw_legacy_srf_unbind(struct vmw_resource *res,
72 bool readback,
73 struct ttm_validate_buffer *val_buf);
74static int vmw_legacy_srf_create(struct vmw_resource *res);
75static int vmw_legacy_srf_destroy(struct vmw_resource *res);
76static int vmw_gb_surface_create(struct vmw_resource *res);
77static int vmw_gb_surface_bind(struct vmw_resource *res,
78 struct ttm_validate_buffer *val_buf);
79static int vmw_gb_surface_unbind(struct vmw_resource *res,
80 bool readback,
81 struct ttm_validate_buffer *val_buf);
82static int vmw_gb_surface_destroy(struct vmw_resource *res);
83
84
85static const struct vmw_user_resource_conv user_surface_conv = {
86 .object_type = VMW_RES_SURFACE,
87 .base_obj_to_res = vmw_user_surface_base_to_res,
88 .res_free = vmw_user_surface_free
89};
90
91const struct vmw_user_resource_conv *user_surface_converter =
92 &user_surface_conv;
93
94
95static uint64_t vmw_user_surface_size;
96
97static const struct vmw_res_func vmw_legacy_surface_func = {
98 .res_type = vmw_res_surface,
99 .needs_backup = false,
100 .may_evict = true,
101 .type_name = "legacy surfaces",
102 .backup_placement = &vmw_srf_placement,
103 .create = &vmw_legacy_srf_create,
104 .destroy = &vmw_legacy_srf_destroy,
105 .bind = &vmw_legacy_srf_bind,
106 .unbind = &vmw_legacy_srf_unbind
107};
108
109static const struct vmw_res_func vmw_gb_surface_func = {
110 .res_type = vmw_res_surface,
111 .needs_backup = true,
112 .may_evict = true,
113 .type_name = "guest backed surfaces",
114 .backup_placement = &vmw_mob_placement,
115 .create = vmw_gb_surface_create,
116 .destroy = vmw_gb_surface_destroy,
117 .bind = vmw_gb_surface_bind,
118 .unbind = vmw_gb_surface_unbind
119};
120
121/**
122 * struct vmw_surface_dma - SVGA3D DMA command
123 */
124struct vmw_surface_dma {
125 SVGA3dCmdHeader header;
126 SVGA3dCmdSurfaceDMA body;
127 SVGA3dCopyBox cb;
128 SVGA3dCmdSurfaceDMASuffix suffix;
129};
130
131/**
132 * struct vmw_surface_define - SVGA3D Surface Define command
133 */
134struct vmw_surface_define {
135 SVGA3dCmdHeader header;
136 SVGA3dCmdDefineSurface body;
137};
138
139/**
140 * struct vmw_surface_destroy - SVGA3D Surface Destroy command
141 */
142struct vmw_surface_destroy {
143 SVGA3dCmdHeader header;
144 SVGA3dCmdDestroySurface body;
145};
146
147
148/**
149 * vmw_surface_dma_size - Compute fifo size for a dma command.
150 *
151 * @srf: Pointer to a struct vmw_surface
152 *
153 * Computes the required size for a surface dma command for backup or
154 * restoration of the surface represented by @srf.
155 */
156static inline uint32_t vmw_surface_dma_size(const struct vmw_surface *srf)
157{
158 return srf->num_sizes * sizeof(struct vmw_surface_dma);
159}
160
161
162/**
163 * vmw_surface_define_size - Compute fifo size for a surface define command.
164 *
165 * @srf: Pointer to a struct vmw_surface
166 *
167 * Computes the required size for a surface define command for the definition
168 * of the surface represented by @srf.
169 */
170static inline uint32_t vmw_surface_define_size(const struct vmw_surface *srf)
171{
172 return sizeof(struct vmw_surface_define) + srf->num_sizes *
173 sizeof(SVGA3dSize);
174}
175
176
177/**
178 * vmw_surface_destroy_size - Compute fifo size for a surface destroy command.
179 *
180 * Computes the required size for a surface destroy command for the destruction
181 * of a hw surface.
182 */
183static inline uint32_t vmw_surface_destroy_size(void)
184{
185 return sizeof(struct vmw_surface_destroy);
186}
187
188/**
189 * vmw_surface_destroy_encode - Encode a surface_destroy command.
190 *
191 * @id: The surface id
192 * @cmd_space: Pointer to memory area in which the commands should be encoded.
193 */
194static void vmw_surface_destroy_encode(uint32_t id,
195 void *cmd_space)
196{
197 struct vmw_surface_destroy *cmd = (struct vmw_surface_destroy *)
198 cmd_space;
199
200 cmd->header.id = SVGA_3D_CMD_SURFACE_DESTROY;
201 cmd->header.size = sizeof(cmd->body);
202 cmd->body.sid = id;
203}
204
205/**
206 * vmw_surface_define_encode - Encode a surface_define command.
207 *
208 * @srf: Pointer to a struct vmw_surface object.
209 * @cmd_space: Pointer to memory area in which the commands should be encoded.
210 */
211static void vmw_surface_define_encode(const struct vmw_surface *srf,
212 void *cmd_space)
213{
214 struct vmw_surface_define *cmd = (struct vmw_surface_define *)
215 cmd_space;
216 struct drm_vmw_size *src_size;
217 SVGA3dSize *cmd_size;
218 uint32_t cmd_len;
219 int i;
220
221 cmd_len = sizeof(cmd->body) + srf->num_sizes * sizeof(SVGA3dSize);
222
223 cmd->header.id = SVGA_3D_CMD_SURFACE_DEFINE;
224 cmd->header.size = cmd_len;
225 cmd->body.sid = srf->res.id;
226 cmd->body.surfaceFlags = srf->flags;
227 cmd->body.format = srf->format;
228 for (i = 0; i < DRM_VMW_MAX_SURFACE_FACES; ++i)
229 cmd->body.face[i].numMipLevels = srf->mip_levels[i];
230
231 cmd += 1;
232 cmd_size = (SVGA3dSize *) cmd;
233 src_size = srf->sizes;
234
235 for (i = 0; i < srf->num_sizes; ++i, cmd_size++, src_size++) {
236 cmd_size->width = src_size->width;
237 cmd_size->height = src_size->height;
238 cmd_size->depth = src_size->depth;
239 }
240}
241
242/**
243 * vmw_surface_dma_encode - Encode a surface_dma command.
244 *
245 * @srf: Pointer to a struct vmw_surface object.
246 * @cmd_space: Pointer to memory area in which the commands should be encoded.
247 * @ptr: Pointer to an SVGAGuestPtr indicating where the surface contents
248 * should be placed or read from.
249 * @to_surface: Boolean whether to DMA to the surface or from the surface.
250 */
251static void vmw_surface_dma_encode(struct vmw_surface *srf,
252 void *cmd_space,
253 const SVGAGuestPtr *ptr,
254 bool to_surface)
255{
256 uint32_t i;
257 struct vmw_surface_dma *cmd = (struct vmw_surface_dma *)cmd_space;
258 const struct svga3d_surface_desc *desc =
259 svga3dsurface_get_desc(srf->format);
260
261 for (i = 0; i < srf->num_sizes; ++i) {
262 SVGA3dCmdHeader *header = &cmd->header;
263 SVGA3dCmdSurfaceDMA *body = &cmd->body;
264 SVGA3dCopyBox *cb = &cmd->cb;
265 SVGA3dCmdSurfaceDMASuffix *suffix = &cmd->suffix;
266 const struct vmw_surface_offset *cur_offset = &srf->offsets[i];
267 const struct drm_vmw_size *cur_size = &srf->sizes[i];
268
269 header->id = SVGA_3D_CMD_SURFACE_DMA;
270 header->size = sizeof(*body) + sizeof(*cb) + sizeof(*suffix);
271
272 body->guest.ptr = *ptr;
273 body->guest.ptr.offset += cur_offset->bo_offset;
274 body->guest.pitch = svga3dsurface_calculate_pitch(desc,
275 cur_size);
276 body->host.sid = srf->res.id;
277 body->host.face = cur_offset->face;
278 body->host.mipmap = cur_offset->mip;
279 body->transfer = ((to_surface) ? SVGA3D_WRITE_HOST_VRAM :
280 SVGA3D_READ_HOST_VRAM);
281 cb->x = 0;
282 cb->y = 0;
283 cb->z = 0;
284 cb->srcx = 0;
285 cb->srcy = 0;
286 cb->srcz = 0;
287 cb->w = cur_size->width;
288 cb->h = cur_size->height;
289 cb->d = cur_size->depth;
290
291 suffix->suffixSize = sizeof(*suffix);
292 suffix->maximumOffset =
293 svga3dsurface_get_image_buffer_size(desc, cur_size,
294 body->guest.pitch);
295 suffix->flags.discard = 0;
296 suffix->flags.unsynchronized = 0;
297 suffix->flags.reserved = 0;
298 ++cmd;
299 }
300};
301
302
303/**
304 * vmw_hw_surface_destroy - destroy a Device surface
305 *
306 * @res: Pointer to a struct vmw_resource embedded in a struct
307 * vmw_surface.
308 *
309 * Destroys a the device surface associated with a struct vmw_surface if
310 * any, and adjusts accounting and resource count accordingly.
311 */
312static void vmw_hw_surface_destroy(struct vmw_resource *res)
313{
314
315 struct vmw_private *dev_priv = res->dev_priv;
316 struct vmw_surface *srf;
317 void *cmd;
318
319 if (res->func->destroy == vmw_gb_surface_destroy) {
320 (void) vmw_gb_surface_destroy(res);
321 return;
322 }
323
324 if (res->id != -1) {
325
326 cmd = vmw_fifo_reserve(dev_priv, vmw_surface_destroy_size());
327 if (unlikely(cmd == NULL)) {
328 DRM_ERROR("Failed reserving FIFO space for surface "
329 "destruction.\n");
330 return;
331 }
332
333 vmw_surface_destroy_encode(res->id, cmd);
334 vmw_fifo_commit(dev_priv, vmw_surface_destroy_size());
335
336 /*
337 * used_memory_size_atomic, or separate lock
338 * to avoid taking dev_priv::cmdbuf_mutex in
339 * the destroy path.
340 */
341
342 mutex_lock(&dev_priv->cmdbuf_mutex);
343 srf = vmw_res_to_srf(res);
344 dev_priv->used_memory_size -= res->backup_size;
345 mutex_unlock(&dev_priv->cmdbuf_mutex);
346 }
347 vmw_fifo_resource_dec(dev_priv);
348}
349
350/**
351 * vmw_legacy_srf_create - Create a device surface as part of the
352 * resource validation process.
353 *
354 * @res: Pointer to a struct vmw_surface.
355 *
356 * If the surface doesn't have a hw id.
357 *
358 * Returns -EBUSY if there wasn't sufficient device resources to
359 * complete the validation. Retry after freeing up resources.
360 *
361 * May return other errors if the kernel is out of guest resources.
362 */
363static int vmw_legacy_srf_create(struct vmw_resource *res)
364{
365 struct vmw_private *dev_priv = res->dev_priv;
366 struct vmw_surface *srf;
367 uint32_t submit_size;
368 uint8_t *cmd;
369 int ret;
370
371 if (likely(res->id != -1))
372 return 0;
373
374 srf = vmw_res_to_srf(res);
375 if (unlikely(dev_priv->used_memory_size + res->backup_size >=
376 dev_priv->memory_size))
377 return -EBUSY;
378
379 /*
380 * Alloc id for the resource.
381 */
382
383 ret = vmw_resource_alloc_id(res);
384 if (unlikely(ret != 0)) {
385 DRM_ERROR("Failed to allocate a surface id.\n");
386 goto out_no_id;
387 }
388
389 if (unlikely(res->id >= SVGA3D_MAX_SURFACE_IDS)) {
390 ret = -EBUSY;
391 goto out_no_fifo;
392 }
393
394 /*
395 * Encode surface define- commands.
396 */
397
398 submit_size = vmw_surface_define_size(srf);
399 cmd = vmw_fifo_reserve(dev_priv, submit_size);
400 if (unlikely(cmd == NULL)) {
401 DRM_ERROR("Failed reserving FIFO space for surface "
402 "creation.\n");
403 ret = -ENOMEM;
404 goto out_no_fifo;
405 }
406
407 vmw_surface_define_encode(srf, cmd);
408 vmw_fifo_commit(dev_priv, submit_size);
409 /*
410 * Surface memory usage accounting.
411 */
412
413 dev_priv->used_memory_size += res->backup_size;
414 return 0;
415
416out_no_fifo:
417 vmw_resource_release_id(res);
418out_no_id:
419 return ret;
420}
421
422/**
423 * vmw_legacy_srf_dma - Copy backup data to or from a legacy surface.
424 *
425 * @res: Pointer to a struct vmw_res embedded in a struct
426 * vmw_surface.
427 * @val_buf: Pointer to a struct ttm_validate_buffer containing
428 * information about the backup buffer.
429 * @bind: Boolean wether to DMA to the surface.
430 *
431 * Transfer backup data to or from a legacy surface as part of the
432 * validation process.
433 * May return other errors if the kernel is out of guest resources.
434 * The backup buffer will be fenced or idle upon successful completion,
435 * and if the surface needs persistent backup storage, the backup buffer
436 * will also be returned reserved iff @bind is true.
437 */
438static int vmw_legacy_srf_dma(struct vmw_resource *res,
439 struct ttm_validate_buffer *val_buf,
440 bool bind)
441{
442 SVGAGuestPtr ptr;
443 struct vmw_fence_obj *fence;
444 uint32_t submit_size;
445 struct vmw_surface *srf = vmw_res_to_srf(res);
446 uint8_t *cmd;
447 struct vmw_private *dev_priv = res->dev_priv;
448
449 BUG_ON(val_buf->bo == NULL);
450
451 submit_size = vmw_surface_dma_size(srf);
452 cmd = vmw_fifo_reserve(dev_priv, submit_size);
453 if (unlikely(cmd == NULL)) {
454 DRM_ERROR("Failed reserving FIFO space for surface "
455 "DMA.\n");
456 return -ENOMEM;
457 }
458 vmw_bo_get_guest_ptr(val_buf->bo, &ptr);
459 vmw_surface_dma_encode(srf, cmd, &ptr, bind);
460
461 vmw_fifo_commit(dev_priv, submit_size);
462
463 /*
464 * Create a fence object and fence the backup buffer.
465 */
466
467 (void) vmw_execbuf_fence_commands(NULL, dev_priv,
468 &fence, NULL);
469
470 vmw_fence_single_bo(val_buf->bo, fence);
471
472 if (likely(fence != NULL))
473 vmw_fence_obj_unreference(&fence);
474
475 return 0;
476}
477
478/**
479 * vmw_legacy_srf_bind - Perform a legacy surface bind as part of the
480 * surface validation process.
481 *
482 * @res: Pointer to a struct vmw_res embedded in a struct
483 * vmw_surface.
484 * @val_buf: Pointer to a struct ttm_validate_buffer containing
485 * information about the backup buffer.
486 *
487 * This function will copy backup data to the surface if the
488 * backup buffer is dirty.
489 */
490static int vmw_legacy_srf_bind(struct vmw_resource *res,
491 struct ttm_validate_buffer *val_buf)
492{
493 if (!res->backup_dirty)
494 return 0;
495
496 return vmw_legacy_srf_dma(res, val_buf, true);
497}
498
499
500/**
501 * vmw_legacy_srf_unbind - Perform a legacy surface unbind as part of the
502 * surface eviction process.
503 *
504 * @res: Pointer to a struct vmw_res embedded in a struct
505 * vmw_surface.
506 * @val_buf: Pointer to a struct ttm_validate_buffer containing
507 * information about the backup buffer.
508 *
509 * This function will copy backup data from the surface.
510 */
511static int vmw_legacy_srf_unbind(struct vmw_resource *res,
512 bool readback,
513 struct ttm_validate_buffer *val_buf)
514{
515 if (unlikely(readback))
516 return vmw_legacy_srf_dma(res, val_buf, false);
517 return 0;
518}
519
520/**
521 * vmw_legacy_srf_destroy - Destroy a device surface as part of a
522 * resource eviction process.
523 *
524 * @res: Pointer to a struct vmw_res embedded in a struct
525 * vmw_surface.
526 */
527static int vmw_legacy_srf_destroy(struct vmw_resource *res)
528{
529 struct vmw_private *dev_priv = res->dev_priv;
530 uint32_t submit_size;
531 uint8_t *cmd;
532
533 BUG_ON(res->id == -1);
534
535 /*
536 * Encode the dma- and surface destroy commands.
537 */
538
539 submit_size = vmw_surface_destroy_size();
540 cmd = vmw_fifo_reserve(dev_priv, submit_size);
541 if (unlikely(cmd == NULL)) {
542 DRM_ERROR("Failed reserving FIFO space for surface "
543 "eviction.\n");
544 return -ENOMEM;
545 }
546
547 vmw_surface_destroy_encode(res->id, cmd);
548 vmw_fifo_commit(dev_priv, submit_size);
549
550 /*
551 * Surface memory usage accounting.
552 */
553
554 dev_priv->used_memory_size -= res->backup_size;
555
556 /*
557 * Release the surface ID.
558 */
559
560 vmw_resource_release_id(res);
561
562 return 0;
563}
564
565
566/**
567 * vmw_surface_init - initialize a struct vmw_surface
568 *
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
572 * the object.
573 */
574static int vmw_surface_init(struct vmw_private *dev_priv,
575 struct vmw_surface *srf,
576 void (*res_free) (struct vmw_resource *res))
577{
578 int ret;
579 struct vmw_resource *res = &srf->res;
580
581 BUG_ON(res_free == NULL);
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);
587
588 if (unlikely(ret != 0)) {
589 if (!dev_priv->has_mob)
590 vmw_fifo_resource_dec(dev_priv);
591 res_free(res);
592 return ret;
593 }
594
595 /*
596 * The surface won't be visible to hardware until a
597 * surface validate.
598 */
599
600 INIT_LIST_HEAD(&srf->view_list);
601 vmw_resource_activate(res, vmw_hw_surface_destroy);
602 return ret;
603}
604
605/**
606 * vmw_user_surface_base_to_res - TTM base object to resource converter for
607 * user visible surfaces
608 *
609 * @base: Pointer to a TTM base object
610 *
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.
613 */
614static struct vmw_resource *
615vmw_user_surface_base_to_res(struct ttm_base_object *base)
616{
617 return &(container_of(base, struct vmw_user_surface,
618 prime.base)->srf.res);
619}
620
621/**
622 * vmw_user_surface_free - User visible surface resource destructor
623 *
624 * @res: A struct vmw_resource embedded in a struct vmw_surface.
625 */
626static void vmw_user_surface_free(struct vmw_resource *res)
627{
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;
633
634 if (user_srf->master)
635 drm_master_put(&user_srf->master);
636 kfree(srf->offsets);
637 kfree(srf->sizes);
638 kfree(srf->snooper.image);
639 ttm_prime_object_kfree(user_srf, prime);
640 ttm_mem_global_free(vmw_mem_glob(dev_priv), size);
641}
642
643/**
644 * vmw_user_surface_free - User visible surface TTM base object destructor
645 *
646 * @p_base: Pointer to a pointer to a TTM base object
647 * embedded in a struct vmw_user_surface.
648 *
649 * Drops the base object's reference on its resource, and the
650 * pointer pointed to by *p_base is set to NULL.
651 */
652static void vmw_user_surface_base_release(struct ttm_base_object **p_base)
653{
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;
658
659 *p_base = NULL;
660 if (user_srf->backup_base)
661 ttm_base_object_unref(&user_srf->backup_base);
662 vmw_resource_unreference(&res);
663}
664
665/**
666 * vmw_user_surface_destroy_ioctl - Ioctl function implementing
667 * the user surface destroy functionality.
668 *
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.
672 */
673int vmw_surface_destroy_ioctl(struct drm_device *dev, void *data,
674 struct drm_file *file_priv)
675{
676 struct drm_vmw_surface_arg *arg = (struct drm_vmw_surface_arg *)data;
677 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
678
679 return ttm_ref_object_base_unref(tfile, arg->sid, TTM_REF_USAGE);
680}
681
682/**
683 * vmw_user_surface_define_ioctl - Ioctl function implementing
684 * the user surface define functionality.
685 *
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.
689 */
690int vmw_surface_define_ioctl(struct drm_device *dev, void *data,
691 struct drm_file *file_priv)
692{
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 drm_vmw_size __user *user_sizes;
704 int ret;
705 int i, j;
706 uint32_t cur_bo_offset;
707 struct drm_vmw_size *cur_size;
708 struct vmw_surface_offset *cur_offset;
709 uint32_t num_sizes;
710 uint32_t size;
711 const struct svga3d_surface_desc *desc;
712
713 if (unlikely(vmw_user_surface_size == 0))
714 vmw_user_surface_size = ttm_round_pot(sizeof(*user_srf)) +
715 128;
716
717 num_sizes = 0;
718 for (i = 0; i < DRM_VMW_MAX_SURFACE_FACES; ++i)
719 num_sizes += req->mip_levels[i];
720
721 if (num_sizes > DRM_VMW_MAX_SURFACE_FACES *
722 DRM_VMW_MAX_MIP_LEVELS)
723 return -EINVAL;
724
725 size = vmw_user_surface_size + 128 +
726 ttm_round_pot(num_sizes * sizeof(struct drm_vmw_size)) +
727 ttm_round_pot(num_sizes * sizeof(struct vmw_surface_offset));
728
729
730 desc = svga3dsurface_get_desc(req->format);
731 if (unlikely(desc->block_desc == SVGA3DBLOCKDESC_NONE)) {
732 DRM_ERROR("Invalid surface format for surface creation.\n");
733 DRM_ERROR("Format requested is: %d\n", req->format);
734 return -EINVAL;
735 }
736
737 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
738 if (unlikely(ret != 0))
739 return ret;
740
741 ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
742 size, false, true);
743 if (unlikely(ret != 0)) {
744 if (ret != -ERESTARTSYS)
745 DRM_ERROR("Out of graphics memory for surface"
746 " creation.\n");
747 goto out_unlock;
748 }
749
750 user_srf = kzalloc(sizeof(*user_srf), GFP_KERNEL);
751 if (unlikely(user_srf == NULL)) {
752 ret = -ENOMEM;
753 goto out_no_user_srf;
754 }
755
756 srf = &user_srf->srf;
757 res = &srf->res;
758
759 srf->flags = req->flags;
760 srf->format = req->format;
761 srf->scanout = req->scanout;
762
763 memcpy(srf->mip_levels, req->mip_levels, sizeof(srf->mip_levels));
764 srf->num_sizes = num_sizes;
765 user_srf->size = size;
766
767 srf->sizes = kmalloc(srf->num_sizes * sizeof(*srf->sizes), GFP_KERNEL);
768 if (unlikely(srf->sizes == NULL)) {
769 ret = -ENOMEM;
770 goto out_no_sizes;
771 }
772 srf->offsets = kmalloc(srf->num_sizes * sizeof(*srf->offsets),
773 GFP_KERNEL);
774 if (unlikely(srf->offsets == NULL)) {
775 ret = -ENOMEM;
776 goto out_no_offsets;
777 }
778
779 user_sizes = (struct drm_vmw_size __user *)(unsigned long)
780 req->size_addr;
781
782 ret = copy_from_user(srf->sizes, user_sizes,
783 srf->num_sizes * sizeof(*srf->sizes));
784 if (unlikely(ret != 0)) {
785 ret = -EFAULT;
786 goto out_no_copy;
787 }
788
789 srf->base_size = *srf->sizes;
790 srf->autogen_filter = SVGA3D_TEX_FILTER_NONE;
791 srf->multisample_count = 0;
792
793 cur_bo_offset = 0;
794 cur_offset = srf->offsets;
795 cur_size = srf->sizes;
796
797 for (i = 0; i < DRM_VMW_MAX_SURFACE_FACES; ++i) {
798 for (j = 0; j < srf->mip_levels[i]; ++j) {
799 uint32_t stride = svga3dsurface_calculate_pitch
800 (desc, cur_size);
801
802 cur_offset->face = i;
803 cur_offset->mip = j;
804 cur_offset->bo_offset = cur_bo_offset;
805 cur_bo_offset += svga3dsurface_get_image_buffer_size
806 (desc, cur_size, stride);
807 ++cur_offset;
808 ++cur_size;
809 }
810 }
811 res->backup_size = cur_bo_offset;
812 if (srf->scanout &&
813 srf->num_sizes == 1 &&
814 srf->sizes[0].width == 64 &&
815 srf->sizes[0].height == 64 &&
816 srf->format == SVGA3D_A8R8G8B8) {
817
818 srf->snooper.image = kzalloc(64 * 64 * 4, GFP_KERNEL);
819 if (!srf->snooper.image) {
820 DRM_ERROR("Failed to allocate cursor_image\n");
821 ret = -ENOMEM;
822 goto out_no_copy;
823 }
824 } else {
825 srf->snooper.image = NULL;
826 }
827 srf->snooper.crtc = NULL;
828
829 user_srf->prime.base.shareable = false;
830 user_srf->prime.base.tfile = NULL;
831 if (drm_is_primary_client(file_priv))
832 user_srf->master = drm_master_get(file_priv->master);
833
834 /**
835 * From this point, the generic resource management functions
836 * destroy the object on failure.
837 */
838
839 ret = vmw_surface_init(dev_priv, srf, vmw_user_surface_free);
840 if (unlikely(ret != 0))
841 goto out_unlock;
842
843 /*
844 * A gb-aware client referencing a shared surface will
845 * expect a backup buffer to be present.
846 */
847 if (dev_priv->has_mob && req->shareable) {
848 uint32_t backup_handle;
849
850 ret = vmw_user_dmabuf_alloc(dev_priv, tfile,
851 res->backup_size,
852 true,
853 &backup_handle,
854 &res->backup,
855 &user_srf->backup_base);
856 if (unlikely(ret != 0)) {
857 vmw_resource_unreference(&res);
858 goto out_unlock;
859 }
860 }
861
862 tmp = vmw_resource_reference(&srf->res);
863 ret = ttm_prime_object_init(tfile, res->backup_size, &user_srf->prime,
864 req->shareable, VMW_RES_SURFACE,
865 &vmw_user_surface_base_release, NULL);
866
867 if (unlikely(ret != 0)) {
868 vmw_resource_unreference(&tmp);
869 vmw_resource_unreference(&res);
870 goto out_unlock;
871 }
872
873 rep->sid = user_srf->prime.base.hash.key;
874 vmw_resource_unreference(&res);
875
876 ttm_read_unlock(&dev_priv->reservation_sem);
877 return 0;
878out_no_copy:
879 kfree(srf->offsets);
880out_no_offsets:
881 kfree(srf->sizes);
882out_no_sizes:
883 ttm_prime_object_kfree(user_srf, prime);
884out_no_user_srf:
885 ttm_mem_global_free(vmw_mem_glob(dev_priv), size);
886out_unlock:
887 ttm_read_unlock(&dev_priv->reservation_sem);
888 return ret;
889}
890
891
892static int
893vmw_surface_handle_reference(struct vmw_private *dev_priv,
894 struct drm_file *file_priv,
895 uint32_t u_handle,
896 enum drm_vmw_handle_type handle_type,
897 struct ttm_base_object **base_p)
898{
899 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
900 struct vmw_user_surface *user_srf;
901 uint32_t handle;
902 struct ttm_base_object *base;
903 int ret;
904
905 if (handle_type == DRM_VMW_HANDLE_PRIME) {
906 ret = ttm_prime_fd_to_handle(tfile, u_handle, &handle);
907 if (unlikely(ret != 0))
908 return ret;
909 } else {
910 if (unlikely(drm_is_render_client(file_priv))) {
911 DRM_ERROR("Render client refused legacy "
912 "surface reference.\n");
913 return -EACCES;
914 }
915 if (ACCESS_ONCE(vmw_fpriv(file_priv)->locked_master)) {
916 DRM_ERROR("Locked master refused legacy "
917 "surface reference.\n");
918 return -EACCES;
919 }
920
921 handle = u_handle;
922 }
923
924 ret = -EINVAL;
925 base = ttm_base_object_lookup_for_ref(dev_priv->tdev, handle);
926 if (unlikely(base == NULL)) {
927 DRM_ERROR("Could not find surface to reference.\n");
928 goto out_no_lookup;
929 }
930
931 if (unlikely(ttm_base_object_type(base) != VMW_RES_SURFACE)) {
932 DRM_ERROR("Referenced object is not a surface.\n");
933 goto out_bad_resource;
934 }
935
936 if (handle_type != DRM_VMW_HANDLE_PRIME) {
937 user_srf = container_of(base, struct vmw_user_surface,
938 prime.base);
939
940 /*
941 * Make sure the surface creator has the same
942 * authenticating master.
943 */
944 if (drm_is_primary_client(file_priv) &&
945 user_srf->master != file_priv->master) {
946 DRM_ERROR("Trying to reference surface outside of"
947 " master domain.\n");
948 ret = -EACCES;
949 goto out_bad_resource;
950 }
951
952 ret = ttm_ref_object_add(tfile, base, TTM_REF_USAGE, NULL);
953 if (unlikely(ret != 0)) {
954 DRM_ERROR("Could not add a reference to a surface.\n");
955 goto out_bad_resource;
956 }
957 }
958
959 *base_p = base;
960 return 0;
961
962out_bad_resource:
963 ttm_base_object_unref(&base);
964out_no_lookup:
965 if (handle_type == DRM_VMW_HANDLE_PRIME)
966 (void) ttm_ref_object_base_unref(tfile, handle, TTM_REF_USAGE);
967
968 return ret;
969}
970
971/**
972 * vmw_user_surface_define_ioctl - Ioctl function implementing
973 * the user surface reference functionality.
974 *
975 * @dev: Pointer to a struct drm_device.
976 * @data: Pointer to data copied from / to user-space.
977 * @file_priv: Pointer to a drm file private structure.
978 */
979int vmw_surface_reference_ioctl(struct drm_device *dev, void *data,
980 struct drm_file *file_priv)
981{
982 struct vmw_private *dev_priv = vmw_priv(dev);
983 union drm_vmw_surface_reference_arg *arg =
984 (union drm_vmw_surface_reference_arg *)data;
985 struct drm_vmw_surface_arg *req = &arg->req;
986 struct drm_vmw_surface_create_req *rep = &arg->rep;
987 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
988 struct vmw_surface *srf;
989 struct vmw_user_surface *user_srf;
990 struct drm_vmw_size __user *user_sizes;
991 struct ttm_base_object *base;
992 int ret;
993
994 ret = vmw_surface_handle_reference(dev_priv, file_priv, req->sid,
995 req->handle_type, &base);
996 if (unlikely(ret != 0))
997 return ret;
998
999 user_srf = container_of(base, struct vmw_user_surface, prime.base);
1000 srf = &user_srf->srf;
1001
1002 rep->flags = srf->flags;
1003 rep->format = srf->format;
1004 memcpy(rep->mip_levels, srf->mip_levels, sizeof(srf->mip_levels));
1005 user_sizes = (struct drm_vmw_size __user *)(unsigned long)
1006 rep->size_addr;
1007
1008 if (user_sizes)
1009 ret = copy_to_user(user_sizes, &srf->base_size,
1010 sizeof(srf->base_size));
1011 if (unlikely(ret != 0)) {
1012 DRM_ERROR("copy_to_user failed %p %u\n",
1013 user_sizes, srf->num_sizes);
1014 ttm_ref_object_base_unref(tfile, base->hash.key, TTM_REF_USAGE);
1015 ret = -EFAULT;
1016 }
1017
1018 ttm_base_object_unref(&base);
1019
1020 return ret;
1021}
1022
1023/**
1024 * vmw_surface_define_encode - Encode a surface_define command.
1025 *
1026 * @srf: Pointer to a struct vmw_surface object.
1027 * @cmd_space: Pointer to memory area in which the commands should be encoded.
1028 */
1029static int vmw_gb_surface_create(struct vmw_resource *res)
1030{
1031 struct vmw_private *dev_priv = res->dev_priv;
1032 struct vmw_surface *srf = vmw_res_to_srf(res);
1033 uint32_t cmd_len, cmd_id, submit_len;
1034 int ret;
1035 struct {
1036 SVGA3dCmdHeader header;
1037 SVGA3dCmdDefineGBSurface body;
1038 } *cmd;
1039 struct {
1040 SVGA3dCmdHeader header;
1041 SVGA3dCmdDefineGBSurface_v2 body;
1042 } *cmd2;
1043
1044 if (likely(res->id != -1))
1045 return 0;
1046
1047 vmw_fifo_resource_inc(dev_priv);
1048 ret = vmw_resource_alloc_id(res);
1049 if (unlikely(ret != 0)) {
1050 DRM_ERROR("Failed to allocate a surface id.\n");
1051 goto out_no_id;
1052 }
1053
1054 if (unlikely(res->id >= VMWGFX_NUM_GB_SURFACE)) {
1055 ret = -EBUSY;
1056 goto out_no_fifo;
1057 }
1058
1059 if (srf->array_size > 0) {
1060 /* has_dx checked on creation time. */
1061 cmd_id = SVGA_3D_CMD_DEFINE_GB_SURFACE_V2;
1062 cmd_len = sizeof(cmd2->body);
1063 submit_len = sizeof(*cmd2);
1064 } else {
1065 cmd_id = SVGA_3D_CMD_DEFINE_GB_SURFACE;
1066 cmd_len = sizeof(cmd->body);
1067 submit_len = sizeof(*cmd);
1068 }
1069
1070 cmd = vmw_fifo_reserve(dev_priv, submit_len);
1071 cmd2 = (typeof(cmd2))cmd;
1072 if (unlikely(cmd == NULL)) {
1073 DRM_ERROR("Failed reserving FIFO space for surface "
1074 "creation.\n");
1075 ret = -ENOMEM;
1076 goto out_no_fifo;
1077 }
1078
1079 if (srf->array_size > 0) {
1080 cmd2->header.id = cmd_id;
1081 cmd2->header.size = cmd_len;
1082 cmd2->body.sid = srf->res.id;
1083 cmd2->body.surfaceFlags = srf->flags;
1084 cmd2->body.format = cpu_to_le32(srf->format);
1085 cmd2->body.numMipLevels = srf->mip_levels[0];
1086 cmd2->body.multisampleCount = srf->multisample_count;
1087 cmd2->body.autogenFilter = srf->autogen_filter;
1088 cmd2->body.size.width = srf->base_size.width;
1089 cmd2->body.size.height = srf->base_size.height;
1090 cmd2->body.size.depth = srf->base_size.depth;
1091 cmd2->body.arraySize = srf->array_size;
1092 } else {
1093 cmd->header.id = cmd_id;
1094 cmd->header.size = cmd_len;
1095 cmd->body.sid = srf->res.id;
1096 cmd->body.surfaceFlags = srf->flags;
1097 cmd->body.format = cpu_to_le32(srf->format);
1098 cmd->body.numMipLevels = srf->mip_levels[0];
1099 cmd->body.multisampleCount = srf->multisample_count;
1100 cmd->body.autogenFilter = srf->autogen_filter;
1101 cmd->body.size.width = srf->base_size.width;
1102 cmd->body.size.height = srf->base_size.height;
1103 cmd->body.size.depth = srf->base_size.depth;
1104 }
1105
1106 vmw_fifo_commit(dev_priv, submit_len);
1107
1108 return 0;
1109
1110out_no_fifo:
1111 vmw_resource_release_id(res);
1112out_no_id:
1113 vmw_fifo_resource_dec(dev_priv);
1114 return ret;
1115}
1116
1117
1118static int vmw_gb_surface_bind(struct vmw_resource *res,
1119 struct ttm_validate_buffer *val_buf)
1120{
1121 struct vmw_private *dev_priv = res->dev_priv;
1122 struct {
1123 SVGA3dCmdHeader header;
1124 SVGA3dCmdBindGBSurface body;
1125 } *cmd1;
1126 struct {
1127 SVGA3dCmdHeader header;
1128 SVGA3dCmdUpdateGBSurface body;
1129 } *cmd2;
1130 uint32_t submit_size;
1131 struct ttm_buffer_object *bo = val_buf->bo;
1132
1133 BUG_ON(bo->mem.mem_type != VMW_PL_MOB);
1134
1135 submit_size = sizeof(*cmd1) + (res->backup_dirty ? sizeof(*cmd2) : 0);
1136
1137 cmd1 = vmw_fifo_reserve(dev_priv, submit_size);
1138 if (unlikely(cmd1 == NULL)) {
1139 DRM_ERROR("Failed reserving FIFO space for surface "
1140 "binding.\n");
1141 return -ENOMEM;
1142 }
1143
1144 cmd1->header.id = SVGA_3D_CMD_BIND_GB_SURFACE;
1145 cmd1->header.size = sizeof(cmd1->body);
1146 cmd1->body.sid = res->id;
1147 cmd1->body.mobid = bo->mem.start;
1148 if (res->backup_dirty) {
1149 cmd2 = (void *) &cmd1[1];
1150 cmd2->header.id = SVGA_3D_CMD_UPDATE_GB_SURFACE;
1151 cmd2->header.size = sizeof(cmd2->body);
1152 cmd2->body.sid = res->id;
1153 res->backup_dirty = false;
1154 }
1155 vmw_fifo_commit(dev_priv, submit_size);
1156
1157 return 0;
1158}
1159
1160static int vmw_gb_surface_unbind(struct vmw_resource *res,
1161 bool readback,
1162 struct ttm_validate_buffer *val_buf)
1163{
1164 struct vmw_private *dev_priv = res->dev_priv;
1165 struct ttm_buffer_object *bo = val_buf->bo;
1166 struct vmw_fence_obj *fence;
1167
1168 struct {
1169 SVGA3dCmdHeader header;
1170 SVGA3dCmdReadbackGBSurface body;
1171 } *cmd1;
1172 struct {
1173 SVGA3dCmdHeader header;
1174 SVGA3dCmdInvalidateGBSurface body;
1175 } *cmd2;
1176 struct {
1177 SVGA3dCmdHeader header;
1178 SVGA3dCmdBindGBSurface body;
1179 } *cmd3;
1180 uint32_t submit_size;
1181 uint8_t *cmd;
1182
1183
1184 BUG_ON(bo->mem.mem_type != VMW_PL_MOB);
1185
1186 submit_size = sizeof(*cmd3) + (readback ? sizeof(*cmd1) : sizeof(*cmd2));
1187 cmd = vmw_fifo_reserve(dev_priv, submit_size);
1188 if (unlikely(cmd == NULL)) {
1189 DRM_ERROR("Failed reserving FIFO space for surface "
1190 "unbinding.\n");
1191 return -ENOMEM;
1192 }
1193
1194 if (readback) {
1195 cmd1 = (void *) cmd;
1196 cmd1->header.id = SVGA_3D_CMD_READBACK_GB_SURFACE;
1197 cmd1->header.size = sizeof(cmd1->body);
1198 cmd1->body.sid = res->id;
1199 cmd3 = (void *) &cmd1[1];
1200 } else {
1201 cmd2 = (void *) cmd;
1202 cmd2->header.id = SVGA_3D_CMD_INVALIDATE_GB_SURFACE;
1203 cmd2->header.size = sizeof(cmd2->body);
1204 cmd2->body.sid = res->id;
1205 cmd3 = (void *) &cmd2[1];
1206 }
1207
1208 cmd3->header.id = SVGA_3D_CMD_BIND_GB_SURFACE;
1209 cmd3->header.size = sizeof(cmd3->body);
1210 cmd3->body.sid = res->id;
1211 cmd3->body.mobid = SVGA3D_INVALID_ID;
1212
1213 vmw_fifo_commit(dev_priv, submit_size);
1214
1215 /*
1216 * Create a fence object and fence the backup buffer.
1217 */
1218
1219 (void) vmw_execbuf_fence_commands(NULL, dev_priv,
1220 &fence, NULL);
1221
1222 vmw_fence_single_bo(val_buf->bo, fence);
1223
1224 if (likely(fence != NULL))
1225 vmw_fence_obj_unreference(&fence);
1226
1227 return 0;
1228}
1229
1230static int vmw_gb_surface_destroy(struct vmw_resource *res)
1231{
1232 struct vmw_private *dev_priv = res->dev_priv;
1233 struct vmw_surface *srf = vmw_res_to_srf(res);
1234 struct {
1235 SVGA3dCmdHeader header;
1236 SVGA3dCmdDestroyGBSurface body;
1237 } *cmd;
1238
1239 if (likely(res->id == -1))
1240 return 0;
1241
1242 mutex_lock(&dev_priv->binding_mutex);
1243 vmw_view_surface_list_destroy(dev_priv, &srf->view_list);
1244 vmw_binding_res_list_scrub(&res->binding_head);
1245
1246 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
1247 if (unlikely(cmd == NULL)) {
1248 DRM_ERROR("Failed reserving FIFO space for surface "
1249 "destruction.\n");
1250 mutex_unlock(&dev_priv->binding_mutex);
1251 return -ENOMEM;
1252 }
1253
1254 cmd->header.id = SVGA_3D_CMD_DESTROY_GB_SURFACE;
1255 cmd->header.size = sizeof(cmd->body);
1256 cmd->body.sid = res->id;
1257 vmw_fifo_commit(dev_priv, sizeof(*cmd));
1258 mutex_unlock(&dev_priv->binding_mutex);
1259 vmw_resource_release_id(res);
1260 vmw_fifo_resource_dec(dev_priv);
1261
1262 return 0;
1263}
1264
1265
1266/**
1267 * vmw_gb_surface_define_ioctl - Ioctl function implementing
1268 * the user surface define functionality.
1269 *
1270 * @dev: Pointer to a struct drm_device.
1271 * @data: Pointer to data copied from / to user-space.
1272 * @file_priv: Pointer to a drm file private structure.
1273 */
1274int vmw_gb_surface_define_ioctl(struct drm_device *dev, void *data,
1275 struct drm_file *file_priv)
1276{
1277 struct vmw_private *dev_priv = vmw_priv(dev);
1278 struct vmw_user_surface *user_srf;
1279 struct vmw_surface *srf;
1280 struct vmw_resource *res;
1281 struct vmw_resource *tmp;
1282 union drm_vmw_gb_surface_create_arg *arg =
1283 (union drm_vmw_gb_surface_create_arg *)data;
1284 struct drm_vmw_gb_surface_create_req *req = &arg->req;
1285 struct drm_vmw_gb_surface_create_rep *rep = &arg->rep;
1286 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1287 int ret;
1288 uint32_t size;
1289 uint32_t backup_handle;
1290
1291 if (req->multisample_count != 0)
1292 return -EINVAL;
1293
1294 if (unlikely(vmw_user_surface_size == 0))
1295 vmw_user_surface_size = ttm_round_pot(sizeof(*user_srf)) +
1296 128;
1297
1298 size = vmw_user_surface_size + 128;
1299
1300 /* Define a surface based on the parameters. */
1301 ret = vmw_surface_gb_priv_define(dev,
1302 size,
1303 req->svga3d_flags,
1304 req->format,
1305 req->drm_surface_flags & drm_vmw_surface_flag_scanout,
1306 req->mip_levels,
1307 req->multisample_count,
1308 req->array_size,
1309 req->base_size,
1310 &srf);
1311 if (unlikely(ret != 0))
1312 return ret;
1313
1314 user_srf = container_of(srf, struct vmw_user_surface, srf);
1315 if (drm_is_primary_client(file_priv))
1316 user_srf->master = drm_master_get(file_priv->master);
1317
1318 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
1319 if (unlikely(ret != 0))
1320 return ret;
1321
1322 res = &user_srf->srf.res;
1323
1324
1325 if (req->buffer_handle != SVGA3D_INVALID_ID) {
1326 ret = vmw_user_dmabuf_lookup(tfile, req->buffer_handle,
1327 &res->backup,
1328 &user_srf->backup_base);
1329 if (ret == 0 && res->backup->base.num_pages * PAGE_SIZE <
1330 res->backup_size) {
1331 DRM_ERROR("Surface backup buffer is too small.\n");
1332 vmw_dmabuf_unreference(&res->backup);
1333 ret = -EINVAL;
1334 goto out_unlock;
1335 }
1336 } else if (req->drm_surface_flags & drm_vmw_surface_flag_create_buffer)
1337 ret = vmw_user_dmabuf_alloc(dev_priv, tfile,
1338 res->backup_size,
1339 req->drm_surface_flags &
1340 drm_vmw_surface_flag_shareable,
1341 &backup_handle,
1342 &res->backup,
1343 &user_srf->backup_base);
1344
1345 if (unlikely(ret != 0)) {
1346 vmw_resource_unreference(&res);
1347 goto out_unlock;
1348 }
1349
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,
1354 VMW_RES_SURFACE,
1355 &vmw_user_surface_base_release, NULL);
1356
1357 if (unlikely(ret != 0)) {
1358 vmw_resource_unreference(&tmp);
1359 vmw_resource_unreference(&res);
1360 goto out_unlock;
1361 }
1362
1363 rep->handle = user_srf->prime.base.hash.key;
1364 rep->backup_size = res->backup_size;
1365 if (res->backup) {
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;
1370 } else {
1371 rep->buffer_map_handle = 0;
1372 rep->buffer_size = 0;
1373 rep->buffer_handle = SVGA3D_INVALID_ID;
1374 }
1375
1376 vmw_resource_unreference(&res);
1377
1378out_unlock:
1379 ttm_read_unlock(&dev_priv->reservation_sem);
1380 return ret;
1381}
1382
1383/**
1384 * vmw_gb_surface_reference_ioctl - Ioctl function implementing
1385 * the user surface reference functionality.
1386 *
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.
1390 */
1391int vmw_gb_surface_reference_ioctl(struct drm_device *dev, void *data,
1392 struct drm_file *file_priv)
1393{
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;
1404 int ret = -EINVAL;
1405
1406 ret = vmw_surface_handle_reference(dev_priv, file_priv, req->sid,
1407 req->handle_type, &base);
1408 if (unlikely(ret != 0))
1409 return ret;
1410
1411 user_srf = container_of(base, struct vmw_user_surface, prime.base);
1412 srf = &user_srf->srf;
1413 if (srf->res.backup == NULL) {
1414 DRM_ERROR("Shared GB surface is missing a backup buffer.\n");
1415 goto out_bad_resource;
1416 }
1417
1418 mutex_lock(&dev_priv->cmdbuf_mutex); /* Protect res->backup */
1419 ret = vmw_user_dmabuf_reference(tfile, srf->res.backup,
1420 &backup_handle);
1421 mutex_unlock(&dev_priv->cmdbuf_mutex);
1422
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,
1427 TTM_REF_USAGE);
1428 goto out_bad_resource;
1429 }
1430
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;
1446
1447out_bad_resource:
1448 ttm_base_object_unref(&base);
1449
1450 return ret;
1451}
1452
1453/**
1454 * vmw_surface_gb_priv_define - Define a private GB surface
1455 *
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.
1467 *
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.
1472 */
1473int vmw_surface_gb_priv_define(struct drm_device *dev,
1474 uint32_t user_accounting_size,
1475 uint32_t svga3d_flags,
1476 SVGA3dSurfaceFormat format,
1477 bool for_scanout,
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)
1483{
1484 struct vmw_private *dev_priv = vmw_priv(dev);
1485 struct vmw_user_surface *user_srf;
1486 struct vmw_surface *srf;
1487 int ret;
1488 u32 num_layers;
1489
1490 *srf_out = NULL;
1491
1492 if (for_scanout) {
1493 if (!svga3dsurface_is_screen_target_format(format)) {
1494 DRM_ERROR("Invalid Screen Target surface format.");
1495 return -EINVAL;
1496 }
1497 } else {
1498 const struct svga3d_surface_desc *desc;
1499
1500 desc = svga3dsurface_get_desc(format);
1501 if (unlikely(desc->block_desc == SVGA3DBLOCKDESC_NONE)) {
1502 DRM_ERROR("Invalid surface format.\n");
1503 return -EINVAL;
1504 }
1505 }
1506
1507 /* array_size must be null for non-GL3 host. */
1508 if (array_size > 0 && !dev_priv->has_dx) {
1509 DRM_ERROR("Tried to create DX surface on non-DX host.\n");
1510 return -EINVAL;
1511 }
1512
1513 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
1514 if (unlikely(ret != 0))
1515 return ret;
1516
1517 ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
1518 user_accounting_size, false, true);
1519 if (unlikely(ret != 0)) {
1520 if (ret != -ERESTARTSYS)
1521 DRM_ERROR("Out of graphics memory for surface"
1522 " creation.\n");
1523 goto out_unlock;
1524 }
1525
1526 user_srf = kzalloc(sizeof(*user_srf), GFP_KERNEL);
1527 if (unlikely(user_srf == NULL)) {
1528 ret = -ENOMEM;
1529 goto out_no_user_srf;
1530 }
1531
1532 *srf_out = &user_srf->srf;
1533 user_srf->size = user_accounting_size;
1534 user_srf->prime.base.shareable = false;
1535 user_srf->prime.base.tfile = NULL;
1536
1537 srf = &user_srf->srf;
1538 srf->flags = svga3d_flags;
1539 srf->format = format;
1540 srf->scanout = for_scanout;
1541 srf->mip_levels[0] = num_mip_levels;
1542 srf->num_sizes = 1;
1543 srf->sizes = NULL;
1544 srf->offsets = NULL;
1545 srf->base_size = size;
1546 srf->autogen_filter = SVGA3D_TEX_FILTER_NONE;
1547 srf->array_size = array_size;
1548 srf->multisample_count = multisample_count;
1549
1550 if (array_size)
1551 num_layers = array_size;
1552 else if (svga3d_flags & SVGA3D_SURFACE_CUBEMAP)
1553 num_layers = SVGA3D_MAX_SURFACE_FACES;
1554 else
1555 num_layers = 1;
1556
1557 srf->res.backup_size =
1558 svga3dsurface_get_serialized_size(srf->format,
1559 srf->base_size,
1560 srf->mip_levels[0],
1561 num_layers);
1562
1563 if (srf->flags & SVGA3D_SURFACE_BIND_STREAM_OUTPUT)
1564 srf->res.backup_size += sizeof(SVGA3dDXSOState);
1565
1566 if (dev_priv->active_display_unit == vmw_du_screen_target &&
1567 for_scanout)
1568 srf->flags |= SVGA3D_SURFACE_SCREENTARGET;
1569
1570 /*
1571 * From this point, the generic resource management functions
1572 * destroy the object on failure.
1573 */
1574 ret = vmw_surface_init(dev_priv, srf, vmw_user_surface_free);
1575
1576 ttm_read_unlock(&dev_priv->reservation_sem);
1577 return ret;
1578
1579out_no_user_srf:
1580 ttm_mem_global_free(vmw_mem_glob(dev_priv), user_accounting_size);
1581
1582out_unlock:
1583 ttm_read_unlock(&dev_priv->reservation_sem);
1584 return ret;
1585}
1// SPDX-License-Identifier: GPL-2.0 OR MIT
2/**************************************************************************
3 *
4 * Copyright 2009-2015 VMware, Inc., Palo Alto, CA., USA
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28#include <drm/ttm/ttm_placement.h>
29
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"
35
36#define SVGA3D_FLAGS_64(upper32, lower32) (((uint64_t)upper32 << 32) | lower32)
37#define SVGA3D_FLAGS_UPPER_32(svga3d_flags) (svga3d_flags >> 32)
38#define SVGA3D_FLAGS_LOWER_32(svga3d_flags) \
39 (svga3d_flags & ((uint64_t)U32_MAX))
40
41/**
42 * struct vmw_user_surface - User-space visible surface resource
43 *
44 * @prime: The TTM prime object.
45 * @base: The TTM base object handling user-space visibility.
46 * @srf: The surface metadata.
47 * @size: TTM accounting size for the surface.
48 * @master: Master of the creating client. Used for security check.
49 * @backup_base: The TTM base object of the backup buffer.
50 */
51struct vmw_user_surface {
52 struct ttm_prime_object prime;
53 struct vmw_surface srf;
54 uint32_t size;
55 struct drm_master *master;
56 struct ttm_base_object *backup_base;
57};
58
59/**
60 * struct vmw_surface_offset - Backing store mip level offset info
61 *
62 * @face: Surface face.
63 * @mip: Mip level.
64 * @bo_offset: Offset into backing store of this mip level.
65 *
66 */
67struct vmw_surface_offset {
68 uint32_t face;
69 uint32_t mip;
70 uint32_t bo_offset;
71};
72
73/**
74 * struct vmw_surface_dirty - Surface dirty-tracker
75 * @cache: Cached layout information of the surface.
76 * @size: Accounting size for the struct vmw_surface_dirty.
77 * @num_subres: Number of subresources.
78 * @boxes: Array of SVGA3dBoxes indicating dirty regions. One per subresource.
79 */
80struct vmw_surface_dirty {
81 struct svga3dsurface_cache cache;
82 size_t size;
83 u32 num_subres;
84 SVGA3dBox boxes[];
85};
86
87static void vmw_user_surface_free(struct vmw_resource *res);
88static struct vmw_resource *
89vmw_user_surface_base_to_res(struct ttm_base_object *base);
90static int vmw_legacy_srf_bind(struct vmw_resource *res,
91 struct ttm_validate_buffer *val_buf);
92static int vmw_legacy_srf_unbind(struct vmw_resource *res,
93 bool readback,
94 struct ttm_validate_buffer *val_buf);
95static int vmw_legacy_srf_create(struct vmw_resource *res);
96static int vmw_legacy_srf_destroy(struct vmw_resource *res);
97static int vmw_gb_surface_create(struct vmw_resource *res);
98static int vmw_gb_surface_bind(struct vmw_resource *res,
99 struct ttm_validate_buffer *val_buf);
100static int vmw_gb_surface_unbind(struct vmw_resource *res,
101 bool readback,
102 struct ttm_validate_buffer *val_buf);
103static int vmw_gb_surface_destroy(struct vmw_resource *res);
104static int
105vmw_gb_surface_define_internal(struct drm_device *dev,
106 struct drm_vmw_gb_surface_create_ext_req *req,
107 struct drm_vmw_gb_surface_create_rep *rep,
108 struct drm_file *file_priv);
109static int
110vmw_gb_surface_reference_internal(struct drm_device *dev,
111 struct drm_vmw_surface_arg *req,
112 struct drm_vmw_gb_surface_ref_ext_rep *rep,
113 struct drm_file *file_priv);
114
115static void vmw_surface_dirty_free(struct vmw_resource *res);
116static int vmw_surface_dirty_alloc(struct vmw_resource *res);
117static int vmw_surface_dirty_sync(struct vmw_resource *res);
118static void vmw_surface_dirty_range_add(struct vmw_resource *res, size_t start,
119 size_t end);
120static int vmw_surface_clean(struct vmw_resource *res);
121
122static const struct vmw_user_resource_conv user_surface_conv = {
123 .object_type = VMW_RES_SURFACE,
124 .base_obj_to_res = vmw_user_surface_base_to_res,
125 .res_free = vmw_user_surface_free
126};
127
128const struct vmw_user_resource_conv *user_surface_converter =
129 &user_surface_conv;
130
131
132static uint64_t vmw_user_surface_size;
133
134static const struct vmw_res_func vmw_legacy_surface_func = {
135 .res_type = vmw_res_surface,
136 .needs_backup = false,
137 .may_evict = true,
138 .prio = 1,
139 .dirty_prio = 1,
140 .type_name = "legacy surfaces",
141 .backup_placement = &vmw_srf_placement,
142 .create = &vmw_legacy_srf_create,
143 .destroy = &vmw_legacy_srf_destroy,
144 .bind = &vmw_legacy_srf_bind,
145 .unbind = &vmw_legacy_srf_unbind
146};
147
148static const struct vmw_res_func vmw_gb_surface_func = {
149 .res_type = vmw_res_surface,
150 .needs_backup = true,
151 .may_evict = true,
152 .prio = 1,
153 .dirty_prio = 2,
154 .type_name = "guest backed surfaces",
155 .backup_placement = &vmw_mob_placement,
156 .create = vmw_gb_surface_create,
157 .destroy = vmw_gb_surface_destroy,
158 .bind = vmw_gb_surface_bind,
159 .unbind = vmw_gb_surface_unbind,
160 .dirty_alloc = vmw_surface_dirty_alloc,
161 .dirty_free = vmw_surface_dirty_free,
162 .dirty_sync = vmw_surface_dirty_sync,
163 .dirty_range_add = vmw_surface_dirty_range_add,
164 .clean = vmw_surface_clean,
165};
166
167/*
168 * struct vmw_surface_dma - SVGA3D DMA command
169 */
170struct vmw_surface_dma {
171 SVGA3dCmdHeader header;
172 SVGA3dCmdSurfaceDMA body;
173 SVGA3dCopyBox cb;
174 SVGA3dCmdSurfaceDMASuffix suffix;
175};
176
177/*
178 * struct vmw_surface_define - SVGA3D Surface Define command
179 */
180struct vmw_surface_define {
181 SVGA3dCmdHeader header;
182 SVGA3dCmdDefineSurface body;
183};
184
185/*
186 * struct vmw_surface_destroy - SVGA3D Surface Destroy command
187 */
188struct vmw_surface_destroy {
189 SVGA3dCmdHeader header;
190 SVGA3dCmdDestroySurface body;
191};
192
193
194/**
195 * vmw_surface_dma_size - Compute fifo size for a dma command.
196 *
197 * @srf: Pointer to a struct vmw_surface
198 *
199 * Computes the required size for a surface dma command for backup or
200 * restoration of the surface represented by @srf.
201 */
202static inline uint32_t vmw_surface_dma_size(const struct vmw_surface *srf)
203{
204 return srf->metadata.num_sizes * sizeof(struct vmw_surface_dma);
205}
206
207
208/**
209 * vmw_surface_define_size - Compute fifo size for a surface define command.
210 *
211 * @srf: Pointer to a struct vmw_surface
212 *
213 * Computes the required size for a surface define command for the definition
214 * of the surface represented by @srf.
215 */
216static inline uint32_t vmw_surface_define_size(const struct vmw_surface *srf)
217{
218 return sizeof(struct vmw_surface_define) + srf->metadata.num_sizes *
219 sizeof(SVGA3dSize);
220}
221
222
223/**
224 * vmw_surface_destroy_size - Compute fifo size for a surface destroy command.
225 *
226 * Computes the required size for a surface destroy command for the destruction
227 * of a hw surface.
228 */
229static inline uint32_t vmw_surface_destroy_size(void)
230{
231 return sizeof(struct vmw_surface_destroy);
232}
233
234/**
235 * vmw_surface_destroy_encode - Encode a surface_destroy command.
236 *
237 * @id: The surface id
238 * @cmd_space: Pointer to memory area in which the commands should be encoded.
239 */
240static void vmw_surface_destroy_encode(uint32_t id,
241 void *cmd_space)
242{
243 struct vmw_surface_destroy *cmd = (struct vmw_surface_destroy *)
244 cmd_space;
245
246 cmd->header.id = SVGA_3D_CMD_SURFACE_DESTROY;
247 cmd->header.size = sizeof(cmd->body);
248 cmd->body.sid = id;
249}
250
251/**
252 * vmw_surface_define_encode - Encode a surface_define command.
253 *
254 * @srf: Pointer to a struct vmw_surface object.
255 * @cmd_space: Pointer to memory area in which the commands should be encoded.
256 */
257static void vmw_surface_define_encode(const struct vmw_surface *srf,
258 void *cmd_space)
259{
260 struct vmw_surface_define *cmd = (struct vmw_surface_define *)
261 cmd_space;
262 struct drm_vmw_size *src_size;
263 SVGA3dSize *cmd_size;
264 uint32_t cmd_len;
265 int i;
266
267 cmd_len = sizeof(cmd->body) + srf->metadata.num_sizes *
268 sizeof(SVGA3dSize);
269
270 cmd->header.id = SVGA_3D_CMD_SURFACE_DEFINE;
271 cmd->header.size = cmd_len;
272 cmd->body.sid = srf->res.id;
273 /*
274 * Downcast of surfaceFlags, was upcasted when received from user-space,
275 * since driver internally stores as 64 bit.
276 * For legacy surface define only 32 bit flag is supported.
277 */
278 cmd->body.surfaceFlags = (SVGA3dSurface1Flags)srf->metadata.flags;
279 cmd->body.format = srf->metadata.format;
280 for (i = 0; i < DRM_VMW_MAX_SURFACE_FACES; ++i)
281 cmd->body.face[i].numMipLevels = srf->metadata.mip_levels[i];
282
283 cmd += 1;
284 cmd_size = (SVGA3dSize *) cmd;
285 src_size = srf->metadata.sizes;
286
287 for (i = 0; i < srf->metadata.num_sizes; ++i, cmd_size++, src_size++) {
288 cmd_size->width = src_size->width;
289 cmd_size->height = src_size->height;
290 cmd_size->depth = src_size->depth;
291 }
292}
293
294/**
295 * vmw_surface_dma_encode - Encode a surface_dma command.
296 *
297 * @srf: Pointer to a struct vmw_surface object.
298 * @cmd_space: Pointer to memory area in which the commands should be encoded.
299 * @ptr: Pointer to an SVGAGuestPtr indicating where the surface contents
300 * should be placed or read from.
301 * @to_surface: Boolean whether to DMA to the surface or from the surface.
302 */
303static void vmw_surface_dma_encode(struct vmw_surface *srf,
304 void *cmd_space,
305 const SVGAGuestPtr *ptr,
306 bool to_surface)
307{
308 uint32_t i;
309 struct vmw_surface_dma *cmd = (struct vmw_surface_dma *)cmd_space;
310 const struct svga3d_surface_desc *desc =
311 svga3dsurface_get_desc(srf->metadata.format);
312
313 for (i = 0; i < srf->metadata.num_sizes; ++i) {
314 SVGA3dCmdHeader *header = &cmd->header;
315 SVGA3dCmdSurfaceDMA *body = &cmd->body;
316 SVGA3dCopyBox *cb = &cmd->cb;
317 SVGA3dCmdSurfaceDMASuffix *suffix = &cmd->suffix;
318 const struct vmw_surface_offset *cur_offset = &srf->offsets[i];
319 const struct drm_vmw_size *cur_size = &srf->metadata.sizes[i];
320
321 header->id = SVGA_3D_CMD_SURFACE_DMA;
322 header->size = sizeof(*body) + sizeof(*cb) + sizeof(*suffix);
323
324 body->guest.ptr = *ptr;
325 body->guest.ptr.offset += cur_offset->bo_offset;
326 body->guest.pitch = svga3dsurface_calculate_pitch(desc,
327 cur_size);
328 body->host.sid = srf->res.id;
329 body->host.face = cur_offset->face;
330 body->host.mipmap = cur_offset->mip;
331 body->transfer = ((to_surface) ? SVGA3D_WRITE_HOST_VRAM :
332 SVGA3D_READ_HOST_VRAM);
333 cb->x = 0;
334 cb->y = 0;
335 cb->z = 0;
336 cb->srcx = 0;
337 cb->srcy = 0;
338 cb->srcz = 0;
339 cb->w = cur_size->width;
340 cb->h = cur_size->height;
341 cb->d = cur_size->depth;
342
343 suffix->suffixSize = sizeof(*suffix);
344 suffix->maximumOffset =
345 svga3dsurface_get_image_buffer_size(desc, cur_size,
346 body->guest.pitch);
347 suffix->flags.discard = 0;
348 suffix->flags.unsynchronized = 0;
349 suffix->flags.reserved = 0;
350 ++cmd;
351 }
352};
353
354
355/**
356 * vmw_hw_surface_destroy - destroy a Device surface
357 *
358 * @res: Pointer to a struct vmw_resource embedded in a struct
359 * vmw_surface.
360 *
361 * Destroys a the device surface associated with a struct vmw_surface if
362 * any, and adjusts accounting and resource count accordingly.
363 */
364static void vmw_hw_surface_destroy(struct vmw_resource *res)
365{
366
367 struct vmw_private *dev_priv = res->dev_priv;
368 void *cmd;
369
370 if (res->func->destroy == vmw_gb_surface_destroy) {
371 (void) vmw_gb_surface_destroy(res);
372 return;
373 }
374
375 if (res->id != -1) {
376
377 cmd = VMW_CMD_RESERVE(dev_priv, vmw_surface_destroy_size());
378 if (unlikely(!cmd))
379 return;
380
381 vmw_surface_destroy_encode(res->id, cmd);
382 vmw_cmd_commit(dev_priv, vmw_surface_destroy_size());
383
384 /*
385 * used_memory_size_atomic, or separate lock
386 * to avoid taking dev_priv::cmdbuf_mutex in
387 * the destroy path.
388 */
389
390 mutex_lock(&dev_priv->cmdbuf_mutex);
391 dev_priv->used_memory_size -= res->backup_size;
392 mutex_unlock(&dev_priv->cmdbuf_mutex);
393 }
394}
395
396/**
397 * vmw_legacy_srf_create - Create a device surface as part of the
398 * resource validation process.
399 *
400 * @res: Pointer to a struct vmw_surface.
401 *
402 * If the surface doesn't have a hw id.
403 *
404 * Returns -EBUSY if there wasn't sufficient device resources to
405 * complete the validation. Retry after freeing up resources.
406 *
407 * May return other errors if the kernel is out of guest resources.
408 */
409static int vmw_legacy_srf_create(struct vmw_resource *res)
410{
411 struct vmw_private *dev_priv = res->dev_priv;
412 struct vmw_surface *srf;
413 uint32_t submit_size;
414 uint8_t *cmd;
415 int ret;
416
417 if (likely(res->id != -1))
418 return 0;
419
420 srf = vmw_res_to_srf(res);
421 if (unlikely(dev_priv->used_memory_size + res->backup_size >=
422 dev_priv->memory_size))
423 return -EBUSY;
424
425 /*
426 * Alloc id for the resource.
427 */
428
429 ret = vmw_resource_alloc_id(res);
430 if (unlikely(ret != 0)) {
431 DRM_ERROR("Failed to allocate a surface id.\n");
432 goto out_no_id;
433 }
434
435 if (unlikely(res->id >= SVGA3D_MAX_SURFACE_IDS)) {
436 ret = -EBUSY;
437 goto out_no_fifo;
438 }
439
440 /*
441 * Encode surface define- commands.
442 */
443
444 submit_size = vmw_surface_define_size(srf);
445 cmd = VMW_CMD_RESERVE(dev_priv, submit_size);
446 if (unlikely(!cmd)) {
447 ret = -ENOMEM;
448 goto out_no_fifo;
449 }
450
451 vmw_surface_define_encode(srf, cmd);
452 vmw_cmd_commit(dev_priv, submit_size);
453 vmw_fifo_resource_inc(dev_priv);
454
455 /*
456 * Surface memory usage accounting.
457 */
458
459 dev_priv->used_memory_size += res->backup_size;
460 return 0;
461
462out_no_fifo:
463 vmw_resource_release_id(res);
464out_no_id:
465 return ret;
466}
467
468/**
469 * vmw_legacy_srf_dma - Copy backup data to or from a legacy surface.
470 *
471 * @res: Pointer to a struct vmw_res embedded in a struct
472 * vmw_surface.
473 * @val_buf: Pointer to a struct ttm_validate_buffer containing
474 * information about the backup buffer.
475 * @bind: Boolean wether to DMA to the surface.
476 *
477 * Transfer backup data to or from a legacy surface as part of the
478 * validation process.
479 * May return other errors if the kernel is out of guest resources.
480 * The backup buffer will be fenced or idle upon successful completion,
481 * and if the surface needs persistent backup storage, the backup buffer
482 * will also be returned reserved iff @bind is true.
483 */
484static int vmw_legacy_srf_dma(struct vmw_resource *res,
485 struct ttm_validate_buffer *val_buf,
486 bool bind)
487{
488 SVGAGuestPtr ptr;
489 struct vmw_fence_obj *fence;
490 uint32_t submit_size;
491 struct vmw_surface *srf = vmw_res_to_srf(res);
492 uint8_t *cmd;
493 struct vmw_private *dev_priv = res->dev_priv;
494
495 BUG_ON(!val_buf->bo);
496 submit_size = vmw_surface_dma_size(srf);
497 cmd = VMW_CMD_RESERVE(dev_priv, submit_size);
498 if (unlikely(!cmd))
499 return -ENOMEM;
500
501 vmw_bo_get_guest_ptr(val_buf->bo, &ptr);
502 vmw_surface_dma_encode(srf, cmd, &ptr, bind);
503
504 vmw_cmd_commit(dev_priv, submit_size);
505
506 /*
507 * Create a fence object and fence the backup buffer.
508 */
509
510 (void) vmw_execbuf_fence_commands(NULL, dev_priv,
511 &fence, NULL);
512
513 vmw_bo_fence_single(val_buf->bo, fence);
514
515 if (likely(fence != NULL))
516 vmw_fence_obj_unreference(&fence);
517
518 return 0;
519}
520
521/**
522 * vmw_legacy_srf_bind - Perform a legacy surface bind as part of the
523 * surface validation process.
524 *
525 * @res: Pointer to a struct vmw_res embedded in a struct
526 * vmw_surface.
527 * @val_buf: Pointer to a struct ttm_validate_buffer containing
528 * information about the backup buffer.
529 *
530 * This function will copy backup data to the surface if the
531 * backup buffer is dirty.
532 */
533static int vmw_legacy_srf_bind(struct vmw_resource *res,
534 struct ttm_validate_buffer *val_buf)
535{
536 if (!res->backup_dirty)
537 return 0;
538
539 return vmw_legacy_srf_dma(res, val_buf, true);
540}
541
542
543/**
544 * vmw_legacy_srf_unbind - Perform a legacy surface unbind as part of the
545 * surface eviction process.
546 *
547 * @res: Pointer to a struct vmw_res embedded in a struct
548 * vmw_surface.
549 * @readback: Readback - only true if dirty
550 * @val_buf: Pointer to a struct ttm_validate_buffer containing
551 * information about the backup buffer.
552 *
553 * This function will copy backup data from the surface.
554 */
555static int vmw_legacy_srf_unbind(struct vmw_resource *res,
556 bool readback,
557 struct ttm_validate_buffer *val_buf)
558{
559 if (unlikely(readback))
560 return vmw_legacy_srf_dma(res, val_buf, false);
561 return 0;
562}
563
564/**
565 * vmw_legacy_srf_destroy - Destroy a device surface as part of a
566 * resource eviction process.
567 *
568 * @res: Pointer to a struct vmw_res embedded in a struct
569 * vmw_surface.
570 */
571static int vmw_legacy_srf_destroy(struct vmw_resource *res)
572{
573 struct vmw_private *dev_priv = res->dev_priv;
574 uint32_t submit_size;
575 uint8_t *cmd;
576
577 BUG_ON(res->id == -1);
578
579 /*
580 * Encode the dma- and surface destroy commands.
581 */
582
583 submit_size = vmw_surface_destroy_size();
584 cmd = VMW_CMD_RESERVE(dev_priv, submit_size);
585 if (unlikely(!cmd))
586 return -ENOMEM;
587
588 vmw_surface_destroy_encode(res->id, cmd);
589 vmw_cmd_commit(dev_priv, submit_size);
590
591 /*
592 * Surface memory usage accounting.
593 */
594
595 dev_priv->used_memory_size -= res->backup_size;
596
597 /*
598 * Release the surface ID.
599 */
600
601 vmw_resource_release_id(res);
602 vmw_fifo_resource_dec(dev_priv);
603
604 return 0;
605}
606
607
608/**
609 * vmw_surface_init - initialize a struct vmw_surface
610 *
611 * @dev_priv: Pointer to a device private struct.
612 * @srf: Pointer to the struct vmw_surface to initialize.
613 * @res_free: Pointer to a resource destructor used to free
614 * the object.
615 */
616static int vmw_surface_init(struct vmw_private *dev_priv,
617 struct vmw_surface *srf,
618 void (*res_free) (struct vmw_resource *res))
619{
620 int ret;
621 struct vmw_resource *res = &srf->res;
622
623 BUG_ON(!res_free);
624 ret = vmw_resource_init(dev_priv, res, true, res_free,
625 (dev_priv->has_mob) ? &vmw_gb_surface_func :
626 &vmw_legacy_surface_func);
627
628 if (unlikely(ret != 0)) {
629 res_free(res);
630 return ret;
631 }
632
633 /*
634 * The surface won't be visible to hardware until a
635 * surface validate.
636 */
637
638 INIT_LIST_HEAD(&srf->view_list);
639 res->hw_destroy = vmw_hw_surface_destroy;
640 return ret;
641}
642
643/**
644 * vmw_user_surface_base_to_res - TTM base object to resource converter for
645 * user visible surfaces
646 *
647 * @base: Pointer to a TTM base object
648 *
649 * Returns the struct vmw_resource embedded in a struct vmw_surface
650 * for the user-visible object identified by the TTM base object @base.
651 */
652static struct vmw_resource *
653vmw_user_surface_base_to_res(struct ttm_base_object *base)
654{
655 return &(container_of(base, struct vmw_user_surface,
656 prime.base)->srf.res);
657}
658
659/**
660 * vmw_user_surface_free - User visible surface resource destructor
661 *
662 * @res: A struct vmw_resource embedded in a struct vmw_surface.
663 */
664static void vmw_user_surface_free(struct vmw_resource *res)
665{
666 struct vmw_surface *srf = vmw_res_to_srf(res);
667 struct vmw_user_surface *user_srf =
668 container_of(srf, struct vmw_user_surface, srf);
669 struct vmw_private *dev_priv = srf->res.dev_priv;
670 uint32_t size = user_srf->size;
671
672 WARN_ON_ONCE(res->dirty);
673 if (user_srf->master)
674 drm_master_put(&user_srf->master);
675 kfree(srf->offsets);
676 kfree(srf->metadata.sizes);
677 kfree(srf->snooper.image);
678 ttm_prime_object_kfree(user_srf, prime);
679 ttm_mem_global_free(vmw_mem_glob(dev_priv), size);
680}
681
682/**
683 * vmw_user_surface_base_release - User visible surface TTM base object destructor
684 *
685 * @p_base: Pointer to a pointer to a TTM base object
686 * embedded in a struct vmw_user_surface.
687 *
688 * Drops the base object's reference on its resource, and the
689 * pointer pointed to by *p_base is set to NULL.
690 */
691static void vmw_user_surface_base_release(struct ttm_base_object **p_base)
692{
693 struct ttm_base_object *base = *p_base;
694 struct vmw_user_surface *user_srf =
695 container_of(base, struct vmw_user_surface, prime.base);
696 struct vmw_resource *res = &user_srf->srf.res;
697
698 *p_base = NULL;
699 if (user_srf->backup_base)
700 ttm_base_object_unref(&user_srf->backup_base);
701 vmw_resource_unreference(&res);
702}
703
704/**
705 * vmw_surface_destroy_ioctl - Ioctl function implementing
706 * the user surface destroy functionality.
707 *
708 * @dev: Pointer to a struct drm_device.
709 * @data: Pointer to data copied from / to user-space.
710 * @file_priv: Pointer to a drm file private structure.
711 */
712int vmw_surface_destroy_ioctl(struct drm_device *dev, void *data,
713 struct drm_file *file_priv)
714{
715 struct drm_vmw_surface_arg *arg = (struct drm_vmw_surface_arg *)data;
716 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
717
718 return ttm_ref_object_base_unref(tfile, arg->sid, TTM_REF_USAGE);
719}
720
721/**
722 * vmw_surface_define_ioctl - Ioctl function implementing
723 * the user surface define functionality.
724 *
725 * @dev: Pointer to a struct drm_device.
726 * @data: Pointer to data copied from / to user-space.
727 * @file_priv: Pointer to a drm file private structure.
728 */
729int vmw_surface_define_ioctl(struct drm_device *dev, void *data,
730 struct drm_file *file_priv)
731{
732 struct vmw_private *dev_priv = vmw_priv(dev);
733 struct vmw_user_surface *user_srf;
734 struct vmw_surface *srf;
735 struct vmw_surface_metadata *metadata;
736 struct vmw_resource *res;
737 struct vmw_resource *tmp;
738 union drm_vmw_surface_create_arg *arg =
739 (union drm_vmw_surface_create_arg *)data;
740 struct drm_vmw_surface_create_req *req = &arg->req;
741 struct drm_vmw_surface_arg *rep = &arg->rep;
742 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
743 struct ttm_operation_ctx ctx = {
744 .interruptible = true,
745 .no_wait_gpu = false
746 };
747 int ret;
748 int i, j;
749 uint32_t cur_bo_offset;
750 struct drm_vmw_size *cur_size;
751 struct vmw_surface_offset *cur_offset;
752 uint32_t num_sizes;
753 uint32_t size;
754 const struct svga3d_surface_desc *desc;
755
756 if (unlikely(vmw_user_surface_size == 0))
757 vmw_user_surface_size = ttm_round_pot(sizeof(*user_srf)) +
758 VMW_IDA_ACC_SIZE + TTM_OBJ_EXTRA_SIZE;
759
760 num_sizes = 0;
761 for (i = 0; i < DRM_VMW_MAX_SURFACE_FACES; ++i) {
762 if (req->mip_levels[i] > DRM_VMW_MAX_MIP_LEVELS)
763 return -EINVAL;
764 num_sizes += req->mip_levels[i];
765 }
766
767 if (num_sizes > DRM_VMW_MAX_SURFACE_FACES * DRM_VMW_MAX_MIP_LEVELS ||
768 num_sizes == 0)
769 return -EINVAL;
770
771 size = vmw_user_surface_size +
772 ttm_round_pot(num_sizes * sizeof(struct drm_vmw_size)) +
773 ttm_round_pot(num_sizes * sizeof(struct vmw_surface_offset));
774
775 desc = svga3dsurface_get_desc(req->format);
776 if (unlikely(desc->block_desc == SVGA3DBLOCKDESC_NONE)) {
777 VMW_DEBUG_USER("Invalid format %d for surface creation.\n",
778 req->format);
779 return -EINVAL;
780 }
781
782 ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
783 size, &ctx);
784 if (unlikely(ret != 0)) {
785 if (ret != -ERESTARTSYS)
786 DRM_ERROR("Out of graphics memory for surface.\n");
787 goto out_unlock;
788 }
789
790 user_srf = kzalloc(sizeof(*user_srf), GFP_KERNEL);
791 if (unlikely(!user_srf)) {
792 ret = -ENOMEM;
793 goto out_no_user_srf;
794 }
795
796 srf = &user_srf->srf;
797 metadata = &srf->metadata;
798 res = &srf->res;
799
800 /* Driver internally stores as 64-bit flags */
801 metadata->flags = (SVGA3dSurfaceAllFlags)req->flags;
802 metadata->format = req->format;
803 metadata->scanout = req->scanout;
804
805 memcpy(metadata->mip_levels, req->mip_levels,
806 sizeof(metadata->mip_levels));
807 metadata->num_sizes = num_sizes;
808 user_srf->size = size;
809 metadata->sizes =
810 memdup_user((struct drm_vmw_size __user *)(unsigned long)
811 req->size_addr,
812 sizeof(*metadata->sizes) * metadata->num_sizes);
813 if (IS_ERR(metadata->sizes)) {
814 ret = PTR_ERR(metadata->sizes);
815 goto out_no_sizes;
816 }
817 srf->offsets = kmalloc_array(metadata->num_sizes, sizeof(*srf->offsets),
818 GFP_KERNEL);
819 if (unlikely(!srf->offsets)) {
820 ret = -ENOMEM;
821 goto out_no_offsets;
822 }
823
824 metadata->base_size = *srf->metadata.sizes;
825 metadata->autogen_filter = SVGA3D_TEX_FILTER_NONE;
826 metadata->multisample_count = 0;
827 metadata->multisample_pattern = SVGA3D_MS_PATTERN_NONE;
828 metadata->quality_level = SVGA3D_MS_QUALITY_NONE;
829
830 cur_bo_offset = 0;
831 cur_offset = srf->offsets;
832 cur_size = metadata->sizes;
833
834 for (i = 0; i < DRM_VMW_MAX_SURFACE_FACES; ++i) {
835 for (j = 0; j < metadata->mip_levels[i]; ++j) {
836 uint32_t stride = svga3dsurface_calculate_pitch
837 (desc, cur_size);
838
839 cur_offset->face = i;
840 cur_offset->mip = j;
841 cur_offset->bo_offset = cur_bo_offset;
842 cur_bo_offset += svga3dsurface_get_image_buffer_size
843 (desc, cur_size, stride);
844 ++cur_offset;
845 ++cur_size;
846 }
847 }
848 res->backup_size = cur_bo_offset;
849 if (metadata->scanout &&
850 metadata->num_sizes == 1 &&
851 metadata->sizes[0].width == 64 &&
852 metadata->sizes[0].height == 64 &&
853 metadata->format == SVGA3D_A8R8G8B8) {
854
855 srf->snooper.image = kzalloc(64 * 64 * 4, GFP_KERNEL);
856 if (!srf->snooper.image) {
857 DRM_ERROR("Failed to allocate cursor_image\n");
858 ret = -ENOMEM;
859 goto out_no_copy;
860 }
861 } else {
862 srf->snooper.image = NULL;
863 }
864
865 user_srf->prime.base.shareable = false;
866 user_srf->prime.base.tfile = NULL;
867 if (drm_is_primary_client(file_priv))
868 user_srf->master = drm_file_get_master(file_priv);
869
870 /**
871 * From this point, the generic resource management functions
872 * destroy the object on failure.
873 */
874
875 ret = vmw_surface_init(dev_priv, srf, vmw_user_surface_free);
876 if (unlikely(ret != 0))
877 goto out_unlock;
878
879 /*
880 * A gb-aware client referencing a shared surface will
881 * expect a backup buffer to be present.
882 */
883 if (dev_priv->has_mob && req->shareable) {
884 uint32_t backup_handle;
885
886 ret = vmw_user_bo_alloc(dev_priv, tfile,
887 res->backup_size,
888 true,
889 &backup_handle,
890 &res->backup,
891 &user_srf->backup_base);
892 if (unlikely(ret != 0)) {
893 vmw_resource_unreference(&res);
894 goto out_unlock;
895 }
896 }
897
898 tmp = vmw_resource_reference(&srf->res);
899 ret = ttm_prime_object_init(tfile, res->backup_size, &user_srf->prime,
900 req->shareable, VMW_RES_SURFACE,
901 &vmw_user_surface_base_release, NULL);
902
903 if (unlikely(ret != 0)) {
904 vmw_resource_unreference(&tmp);
905 vmw_resource_unreference(&res);
906 goto out_unlock;
907 }
908
909 rep->sid = user_srf->prime.base.handle;
910 vmw_resource_unreference(&res);
911
912 return 0;
913out_no_copy:
914 kfree(srf->offsets);
915out_no_offsets:
916 kfree(metadata->sizes);
917out_no_sizes:
918 ttm_prime_object_kfree(user_srf, prime);
919out_no_user_srf:
920 ttm_mem_global_free(vmw_mem_glob(dev_priv), size);
921out_unlock:
922 return ret;
923}
924
925
926static int
927vmw_surface_handle_reference(struct vmw_private *dev_priv,
928 struct drm_file *file_priv,
929 uint32_t u_handle,
930 enum drm_vmw_handle_type handle_type,
931 struct ttm_base_object **base_p)
932{
933 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
934 struct vmw_user_surface *user_srf;
935 uint32_t handle;
936 struct ttm_base_object *base;
937 int ret;
938
939 if (handle_type == DRM_VMW_HANDLE_PRIME) {
940 ret = ttm_prime_fd_to_handle(tfile, u_handle, &handle);
941 if (unlikely(ret != 0))
942 return ret;
943 } else {
944 handle = u_handle;
945 }
946
947 ret = -EINVAL;
948 base = ttm_base_object_lookup_for_ref(dev_priv->tdev, handle);
949 if (unlikely(!base)) {
950 VMW_DEBUG_USER("Could not find surface to reference.\n");
951 goto out_no_lookup;
952 }
953
954 if (unlikely(ttm_base_object_type(base) != VMW_RES_SURFACE)) {
955 VMW_DEBUG_USER("Referenced object is not a surface.\n");
956 goto out_bad_resource;
957 }
958
959 if (handle_type != DRM_VMW_HANDLE_PRIME) {
960 bool require_exist = false;
961
962 user_srf = container_of(base, struct vmw_user_surface,
963 prime.base);
964
965 /* Error out if we are unauthenticated primary */
966 if (drm_is_primary_client(file_priv) &&
967 !file_priv->authenticated) {
968 ret = -EACCES;
969 goto out_bad_resource;
970 }
971
972 /*
973 * Make sure the surface creator has the same
974 * authenticating master, or is already registered with us.
975 */
976 if (drm_is_primary_client(file_priv) &&
977 user_srf->master != file_priv->master)
978 require_exist = true;
979
980 if (unlikely(drm_is_render_client(file_priv)))
981 require_exist = true;
982
983 ret = ttm_ref_object_add(tfile, base, TTM_REF_USAGE, NULL,
984 require_exist);
985 if (unlikely(ret != 0)) {
986 DRM_ERROR("Could not add a reference to a surface.\n");
987 goto out_bad_resource;
988 }
989 }
990
991 *base_p = base;
992 return 0;
993
994out_bad_resource:
995 ttm_base_object_unref(&base);
996out_no_lookup:
997 if (handle_type == DRM_VMW_HANDLE_PRIME)
998 (void) ttm_ref_object_base_unref(tfile, handle, TTM_REF_USAGE);
999
1000 return ret;
1001}
1002
1003/**
1004 * vmw_surface_reference_ioctl - Ioctl function implementing
1005 * the user surface reference functionality.
1006 *
1007 * @dev: Pointer to a struct drm_device.
1008 * @data: Pointer to data copied from / to user-space.
1009 * @file_priv: Pointer to a drm file private structure.
1010 */
1011int vmw_surface_reference_ioctl(struct drm_device *dev, void *data,
1012 struct drm_file *file_priv)
1013{
1014 struct vmw_private *dev_priv = vmw_priv(dev);
1015 union drm_vmw_surface_reference_arg *arg =
1016 (union drm_vmw_surface_reference_arg *)data;
1017 struct drm_vmw_surface_arg *req = &arg->req;
1018 struct drm_vmw_surface_create_req *rep = &arg->rep;
1019 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1020 struct vmw_surface *srf;
1021 struct vmw_user_surface *user_srf;
1022 struct drm_vmw_size __user *user_sizes;
1023 struct ttm_base_object *base;
1024 int ret;
1025
1026 ret = vmw_surface_handle_reference(dev_priv, file_priv, req->sid,
1027 req->handle_type, &base);
1028 if (unlikely(ret != 0))
1029 return ret;
1030
1031 user_srf = container_of(base, struct vmw_user_surface, prime.base);
1032 srf = &user_srf->srf;
1033
1034 /* Downcast of flags when sending back to user space */
1035 rep->flags = (uint32_t)srf->metadata.flags;
1036 rep->format = srf->metadata.format;
1037 memcpy(rep->mip_levels, srf->metadata.mip_levels,
1038 sizeof(srf->metadata.mip_levels));
1039 user_sizes = (struct drm_vmw_size __user *)(unsigned long)
1040 rep->size_addr;
1041
1042 if (user_sizes)
1043 ret = copy_to_user(user_sizes, &srf->metadata.base_size,
1044 sizeof(srf->metadata.base_size));
1045 if (unlikely(ret != 0)) {
1046 VMW_DEBUG_USER("copy_to_user failed %p %u\n", user_sizes,
1047 srf->metadata.num_sizes);
1048 ttm_ref_object_base_unref(tfile, base->handle, TTM_REF_USAGE);
1049 ret = -EFAULT;
1050 }
1051
1052 ttm_base_object_unref(&base);
1053
1054 return ret;
1055}
1056
1057/**
1058 * vmw_gb_surface_create - Encode a surface_define command.
1059 *
1060 * @res: Pointer to a struct vmw_resource embedded in a struct
1061 * vmw_surface.
1062 */
1063static int vmw_gb_surface_create(struct vmw_resource *res)
1064{
1065 struct vmw_private *dev_priv = res->dev_priv;
1066 struct vmw_surface *srf = vmw_res_to_srf(res);
1067 struct vmw_surface_metadata *metadata = &srf->metadata;
1068 uint32_t cmd_len, cmd_id, submit_len;
1069 int ret;
1070 struct {
1071 SVGA3dCmdHeader header;
1072 SVGA3dCmdDefineGBSurface body;
1073 } *cmd;
1074 struct {
1075 SVGA3dCmdHeader header;
1076 SVGA3dCmdDefineGBSurface_v2 body;
1077 } *cmd2;
1078 struct {
1079 SVGA3dCmdHeader header;
1080 SVGA3dCmdDefineGBSurface_v3 body;
1081 } *cmd3;
1082 struct {
1083 SVGA3dCmdHeader header;
1084 SVGA3dCmdDefineGBSurface_v4 body;
1085 } *cmd4;
1086
1087 if (likely(res->id != -1))
1088 return 0;
1089
1090 vmw_fifo_resource_inc(dev_priv);
1091 ret = vmw_resource_alloc_id(res);
1092 if (unlikely(ret != 0)) {
1093 DRM_ERROR("Failed to allocate a surface id.\n");
1094 goto out_no_id;
1095 }
1096
1097 if (unlikely(res->id >= VMWGFX_NUM_GB_SURFACE)) {
1098 ret = -EBUSY;
1099 goto out_no_fifo;
1100 }
1101
1102 if (has_sm5_context(dev_priv) && metadata->array_size > 0) {
1103 cmd_id = SVGA_3D_CMD_DEFINE_GB_SURFACE_V4;
1104 cmd_len = sizeof(cmd4->body);
1105 submit_len = sizeof(*cmd4);
1106 } else if (has_sm4_1_context(dev_priv) && metadata->array_size > 0) {
1107 cmd_id = SVGA_3D_CMD_DEFINE_GB_SURFACE_V3;
1108 cmd_len = sizeof(cmd3->body);
1109 submit_len = sizeof(*cmd3);
1110 } else if (metadata->array_size > 0) {
1111 /* VMW_SM_4 support verified at creation time. */
1112 cmd_id = SVGA_3D_CMD_DEFINE_GB_SURFACE_V2;
1113 cmd_len = sizeof(cmd2->body);
1114 submit_len = sizeof(*cmd2);
1115 } else {
1116 cmd_id = SVGA_3D_CMD_DEFINE_GB_SURFACE;
1117 cmd_len = sizeof(cmd->body);
1118 submit_len = sizeof(*cmd);
1119 }
1120
1121 cmd = VMW_CMD_RESERVE(dev_priv, submit_len);
1122 cmd2 = (typeof(cmd2))cmd;
1123 cmd3 = (typeof(cmd3))cmd;
1124 cmd4 = (typeof(cmd4))cmd;
1125 if (unlikely(!cmd)) {
1126 ret = -ENOMEM;
1127 goto out_no_fifo;
1128 }
1129
1130 if (has_sm5_context(dev_priv) && metadata->array_size > 0) {
1131 cmd4->header.id = cmd_id;
1132 cmd4->header.size = cmd_len;
1133 cmd4->body.sid = srf->res.id;
1134 cmd4->body.surfaceFlags = metadata->flags;
1135 cmd4->body.format = metadata->format;
1136 cmd4->body.numMipLevels = metadata->mip_levels[0];
1137 cmd4->body.multisampleCount = metadata->multisample_count;
1138 cmd4->body.multisamplePattern = metadata->multisample_pattern;
1139 cmd4->body.qualityLevel = metadata->quality_level;
1140 cmd4->body.autogenFilter = metadata->autogen_filter;
1141 cmd4->body.size.width = metadata->base_size.width;
1142 cmd4->body.size.height = metadata->base_size.height;
1143 cmd4->body.size.depth = metadata->base_size.depth;
1144 cmd4->body.arraySize = metadata->array_size;
1145 cmd4->body.bufferByteStride = metadata->buffer_byte_stride;
1146 } else if (has_sm4_1_context(dev_priv) && metadata->array_size > 0) {
1147 cmd3->header.id = cmd_id;
1148 cmd3->header.size = cmd_len;
1149 cmd3->body.sid = srf->res.id;
1150 cmd3->body.surfaceFlags = metadata->flags;
1151 cmd3->body.format = metadata->format;
1152 cmd3->body.numMipLevels = metadata->mip_levels[0];
1153 cmd3->body.multisampleCount = metadata->multisample_count;
1154 cmd3->body.multisamplePattern = metadata->multisample_pattern;
1155 cmd3->body.qualityLevel = metadata->quality_level;
1156 cmd3->body.autogenFilter = metadata->autogen_filter;
1157 cmd3->body.size.width = metadata->base_size.width;
1158 cmd3->body.size.height = metadata->base_size.height;
1159 cmd3->body.size.depth = metadata->base_size.depth;
1160 cmd3->body.arraySize = metadata->array_size;
1161 } else if (metadata->array_size > 0) {
1162 cmd2->header.id = cmd_id;
1163 cmd2->header.size = cmd_len;
1164 cmd2->body.sid = srf->res.id;
1165 cmd2->body.surfaceFlags = metadata->flags;
1166 cmd2->body.format = metadata->format;
1167 cmd2->body.numMipLevels = metadata->mip_levels[0];
1168 cmd2->body.multisampleCount = metadata->multisample_count;
1169 cmd2->body.autogenFilter = metadata->autogen_filter;
1170 cmd2->body.size.width = metadata->base_size.width;
1171 cmd2->body.size.height = metadata->base_size.height;
1172 cmd2->body.size.depth = metadata->base_size.depth;
1173 cmd2->body.arraySize = metadata->array_size;
1174 } else {
1175 cmd->header.id = cmd_id;
1176 cmd->header.size = cmd_len;
1177 cmd->body.sid = srf->res.id;
1178 cmd->body.surfaceFlags = metadata->flags;
1179 cmd->body.format = metadata->format;
1180 cmd->body.numMipLevels = metadata->mip_levels[0];
1181 cmd->body.multisampleCount = metadata->multisample_count;
1182 cmd->body.autogenFilter = metadata->autogen_filter;
1183 cmd->body.size.width = metadata->base_size.width;
1184 cmd->body.size.height = metadata->base_size.height;
1185 cmd->body.size.depth = metadata->base_size.depth;
1186 }
1187
1188 vmw_cmd_commit(dev_priv, submit_len);
1189
1190 return 0;
1191
1192out_no_fifo:
1193 vmw_resource_release_id(res);
1194out_no_id:
1195 vmw_fifo_resource_dec(dev_priv);
1196 return ret;
1197}
1198
1199
1200static int vmw_gb_surface_bind(struct vmw_resource *res,
1201 struct ttm_validate_buffer *val_buf)
1202{
1203 struct vmw_private *dev_priv = res->dev_priv;
1204 struct {
1205 SVGA3dCmdHeader header;
1206 SVGA3dCmdBindGBSurface body;
1207 } *cmd1;
1208 struct {
1209 SVGA3dCmdHeader header;
1210 SVGA3dCmdUpdateGBSurface body;
1211 } *cmd2;
1212 uint32_t submit_size;
1213 struct ttm_buffer_object *bo = val_buf->bo;
1214
1215 BUG_ON(bo->resource->mem_type != VMW_PL_MOB);
1216
1217 submit_size = sizeof(*cmd1) + (res->backup_dirty ? sizeof(*cmd2) : 0);
1218
1219 cmd1 = VMW_CMD_RESERVE(dev_priv, submit_size);
1220 if (unlikely(!cmd1))
1221 return -ENOMEM;
1222
1223 cmd1->header.id = SVGA_3D_CMD_BIND_GB_SURFACE;
1224 cmd1->header.size = sizeof(cmd1->body);
1225 cmd1->body.sid = res->id;
1226 cmd1->body.mobid = bo->resource->start;
1227 if (res->backup_dirty) {
1228 cmd2 = (void *) &cmd1[1];
1229 cmd2->header.id = SVGA_3D_CMD_UPDATE_GB_SURFACE;
1230 cmd2->header.size = sizeof(cmd2->body);
1231 cmd2->body.sid = res->id;
1232 }
1233 vmw_cmd_commit(dev_priv, submit_size);
1234
1235 if (res->backup->dirty && res->backup_dirty) {
1236 /* We've just made a full upload. Cear dirty regions. */
1237 vmw_bo_dirty_clear_res(res);
1238 }
1239
1240 res->backup_dirty = false;
1241
1242 return 0;
1243}
1244
1245static int vmw_gb_surface_unbind(struct vmw_resource *res,
1246 bool readback,
1247 struct ttm_validate_buffer *val_buf)
1248{
1249 struct vmw_private *dev_priv = res->dev_priv;
1250 struct ttm_buffer_object *bo = val_buf->bo;
1251 struct vmw_fence_obj *fence;
1252
1253 struct {
1254 SVGA3dCmdHeader header;
1255 SVGA3dCmdReadbackGBSurface body;
1256 } *cmd1;
1257 struct {
1258 SVGA3dCmdHeader header;
1259 SVGA3dCmdInvalidateGBSurface body;
1260 } *cmd2;
1261 struct {
1262 SVGA3dCmdHeader header;
1263 SVGA3dCmdBindGBSurface body;
1264 } *cmd3;
1265 uint32_t submit_size;
1266 uint8_t *cmd;
1267
1268
1269 BUG_ON(bo->resource->mem_type != VMW_PL_MOB);
1270
1271 submit_size = sizeof(*cmd3) + (readback ? sizeof(*cmd1) : sizeof(*cmd2));
1272 cmd = VMW_CMD_RESERVE(dev_priv, submit_size);
1273 if (unlikely(!cmd))
1274 return -ENOMEM;
1275
1276 if (readback) {
1277 cmd1 = (void *) cmd;
1278 cmd1->header.id = SVGA_3D_CMD_READBACK_GB_SURFACE;
1279 cmd1->header.size = sizeof(cmd1->body);
1280 cmd1->body.sid = res->id;
1281 cmd3 = (void *) &cmd1[1];
1282 } else {
1283 cmd2 = (void *) cmd;
1284 cmd2->header.id = SVGA_3D_CMD_INVALIDATE_GB_SURFACE;
1285 cmd2->header.size = sizeof(cmd2->body);
1286 cmd2->body.sid = res->id;
1287 cmd3 = (void *) &cmd2[1];
1288 }
1289
1290 cmd3->header.id = SVGA_3D_CMD_BIND_GB_SURFACE;
1291 cmd3->header.size = sizeof(cmd3->body);
1292 cmd3->body.sid = res->id;
1293 cmd3->body.mobid = SVGA3D_INVALID_ID;
1294
1295 vmw_cmd_commit(dev_priv, submit_size);
1296
1297 /*
1298 * Create a fence object and fence the backup buffer.
1299 */
1300
1301 (void) vmw_execbuf_fence_commands(NULL, dev_priv,
1302 &fence, NULL);
1303
1304 vmw_bo_fence_single(val_buf->bo, fence);
1305
1306 if (likely(fence != NULL))
1307 vmw_fence_obj_unreference(&fence);
1308
1309 return 0;
1310}
1311
1312static int vmw_gb_surface_destroy(struct vmw_resource *res)
1313{
1314 struct vmw_private *dev_priv = res->dev_priv;
1315 struct vmw_surface *srf = vmw_res_to_srf(res);
1316 struct {
1317 SVGA3dCmdHeader header;
1318 SVGA3dCmdDestroyGBSurface body;
1319 } *cmd;
1320
1321 if (likely(res->id == -1))
1322 return 0;
1323
1324 mutex_lock(&dev_priv->binding_mutex);
1325 vmw_view_surface_list_destroy(dev_priv, &srf->view_list);
1326 vmw_binding_res_list_scrub(&res->binding_head);
1327
1328 cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
1329 if (unlikely(!cmd)) {
1330 mutex_unlock(&dev_priv->binding_mutex);
1331 return -ENOMEM;
1332 }
1333
1334 cmd->header.id = SVGA_3D_CMD_DESTROY_GB_SURFACE;
1335 cmd->header.size = sizeof(cmd->body);
1336 cmd->body.sid = res->id;
1337 vmw_cmd_commit(dev_priv, sizeof(*cmd));
1338 mutex_unlock(&dev_priv->binding_mutex);
1339 vmw_resource_release_id(res);
1340 vmw_fifo_resource_dec(dev_priv);
1341
1342 return 0;
1343}
1344
1345/**
1346 * vmw_gb_surface_define_ioctl - Ioctl function implementing
1347 * the user surface define functionality.
1348 *
1349 * @dev: Pointer to a struct drm_device.
1350 * @data: Pointer to data copied from / to user-space.
1351 * @file_priv: Pointer to a drm file private structure.
1352 */
1353int vmw_gb_surface_define_ioctl(struct drm_device *dev, void *data,
1354 struct drm_file *file_priv)
1355{
1356 union drm_vmw_gb_surface_create_arg *arg =
1357 (union drm_vmw_gb_surface_create_arg *)data;
1358 struct drm_vmw_gb_surface_create_rep *rep = &arg->rep;
1359 struct drm_vmw_gb_surface_create_ext_req req_ext;
1360
1361 req_ext.base = arg->req;
1362 req_ext.version = drm_vmw_gb_surface_v1;
1363 req_ext.svga3d_flags_upper_32_bits = 0;
1364 req_ext.multisample_pattern = SVGA3D_MS_PATTERN_NONE;
1365 req_ext.quality_level = SVGA3D_MS_QUALITY_NONE;
1366 req_ext.buffer_byte_stride = 0;
1367 req_ext.must_be_zero = 0;
1368
1369 return vmw_gb_surface_define_internal(dev, &req_ext, rep, file_priv);
1370}
1371
1372/**
1373 * vmw_gb_surface_reference_ioctl - Ioctl function implementing
1374 * the user surface reference functionality.
1375 *
1376 * @dev: Pointer to a struct drm_device.
1377 * @data: Pointer to data copied from / to user-space.
1378 * @file_priv: Pointer to a drm file private structure.
1379 */
1380int vmw_gb_surface_reference_ioctl(struct drm_device *dev, void *data,
1381 struct drm_file *file_priv)
1382{
1383 union drm_vmw_gb_surface_reference_arg *arg =
1384 (union drm_vmw_gb_surface_reference_arg *)data;
1385 struct drm_vmw_surface_arg *req = &arg->req;
1386 struct drm_vmw_gb_surface_ref_rep *rep = &arg->rep;
1387 struct drm_vmw_gb_surface_ref_ext_rep rep_ext;
1388 int ret;
1389
1390 ret = vmw_gb_surface_reference_internal(dev, req, &rep_ext, file_priv);
1391
1392 if (unlikely(ret != 0))
1393 return ret;
1394
1395 rep->creq = rep_ext.creq.base;
1396 rep->crep = rep_ext.crep;
1397
1398 return ret;
1399}
1400
1401/**
1402 * vmw_gb_surface_define_ext_ioctl - Ioctl function implementing
1403 * the user surface define functionality.
1404 *
1405 * @dev: Pointer to a struct drm_device.
1406 * @data: Pointer to data copied from / to user-space.
1407 * @file_priv: Pointer to a drm file private structure.
1408 */
1409int vmw_gb_surface_define_ext_ioctl(struct drm_device *dev, void *data,
1410 struct drm_file *file_priv)
1411{
1412 union drm_vmw_gb_surface_create_ext_arg *arg =
1413 (union drm_vmw_gb_surface_create_ext_arg *)data;
1414 struct drm_vmw_gb_surface_create_ext_req *req = &arg->req;
1415 struct drm_vmw_gb_surface_create_rep *rep = &arg->rep;
1416
1417 return vmw_gb_surface_define_internal(dev, req, rep, file_priv);
1418}
1419
1420/**
1421 * vmw_gb_surface_reference_ext_ioctl - Ioctl function implementing
1422 * the user surface reference functionality.
1423 *
1424 * @dev: Pointer to a struct drm_device.
1425 * @data: Pointer to data copied from / to user-space.
1426 * @file_priv: Pointer to a drm file private structure.
1427 */
1428int vmw_gb_surface_reference_ext_ioctl(struct drm_device *dev, void *data,
1429 struct drm_file *file_priv)
1430{
1431 union drm_vmw_gb_surface_reference_ext_arg *arg =
1432 (union drm_vmw_gb_surface_reference_ext_arg *)data;
1433 struct drm_vmw_surface_arg *req = &arg->req;
1434 struct drm_vmw_gb_surface_ref_ext_rep *rep = &arg->rep;
1435
1436 return vmw_gb_surface_reference_internal(dev, req, rep, file_priv);
1437}
1438
1439/**
1440 * vmw_gb_surface_define_internal - Ioctl function implementing
1441 * the user surface define functionality.
1442 *
1443 * @dev: Pointer to a struct drm_device.
1444 * @req: Request argument from user-space.
1445 * @rep: Response argument to user-space.
1446 * @file_priv: Pointer to a drm file private structure.
1447 */
1448static int
1449vmw_gb_surface_define_internal(struct drm_device *dev,
1450 struct drm_vmw_gb_surface_create_ext_req *req,
1451 struct drm_vmw_gb_surface_create_rep *rep,
1452 struct drm_file *file_priv)
1453{
1454 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1455 struct vmw_private *dev_priv = vmw_priv(dev);
1456 struct vmw_user_surface *user_srf;
1457 struct vmw_surface_metadata metadata = {0};
1458 struct vmw_surface *srf;
1459 struct vmw_resource *res;
1460 struct vmw_resource *tmp;
1461 int ret = 0;
1462 uint32_t size;
1463 uint32_t backup_handle = 0;
1464 SVGA3dSurfaceAllFlags svga3d_flags_64 =
1465 SVGA3D_FLAGS_64(req->svga3d_flags_upper_32_bits,
1466 req->base.svga3d_flags);
1467
1468 /* array_size must be null for non-GL3 host. */
1469 if (req->base.array_size > 0 && !has_sm4_context(dev_priv)) {
1470 VMW_DEBUG_USER("SM4 surface not supported.\n");
1471 return -EINVAL;
1472 }
1473
1474 if (!has_sm4_1_context(dev_priv)) {
1475 if (req->svga3d_flags_upper_32_bits != 0)
1476 ret = -EINVAL;
1477
1478 if (req->base.multisample_count != 0)
1479 ret = -EINVAL;
1480
1481 if (req->multisample_pattern != SVGA3D_MS_PATTERN_NONE)
1482 ret = -EINVAL;
1483
1484 if (req->quality_level != SVGA3D_MS_QUALITY_NONE)
1485 ret = -EINVAL;
1486
1487 if (ret) {
1488 VMW_DEBUG_USER("SM4.1 surface not supported.\n");
1489 return ret;
1490 }
1491 }
1492
1493 if (req->buffer_byte_stride > 0 && !has_sm5_context(dev_priv)) {
1494 VMW_DEBUG_USER("SM5 surface not supported.\n");
1495 return -EINVAL;
1496 }
1497
1498 if ((svga3d_flags_64 & SVGA3D_SURFACE_MULTISAMPLE) &&
1499 req->base.multisample_count == 0) {
1500 VMW_DEBUG_USER("Invalid sample count.\n");
1501 return -EINVAL;
1502 }
1503
1504 if (req->base.mip_levels > DRM_VMW_MAX_MIP_LEVELS) {
1505 VMW_DEBUG_USER("Invalid mip level.\n");
1506 return -EINVAL;
1507 }
1508
1509 if (unlikely(vmw_user_surface_size == 0))
1510 vmw_user_surface_size = ttm_round_pot(sizeof(*user_srf)) +
1511 VMW_IDA_ACC_SIZE + TTM_OBJ_EXTRA_SIZE;
1512
1513 size = vmw_user_surface_size;
1514
1515 metadata.flags = svga3d_flags_64;
1516 metadata.format = req->base.format;
1517 metadata.mip_levels[0] = req->base.mip_levels;
1518 metadata.multisample_count = req->base.multisample_count;
1519 metadata.multisample_pattern = req->multisample_pattern;
1520 metadata.quality_level = req->quality_level;
1521 metadata.array_size = req->base.array_size;
1522 metadata.buffer_byte_stride = req->buffer_byte_stride;
1523 metadata.num_sizes = 1;
1524 metadata.base_size = req->base.base_size;
1525 metadata.scanout = req->base.drm_surface_flags &
1526 drm_vmw_surface_flag_scanout;
1527
1528 /* Define a surface based on the parameters. */
1529 ret = vmw_gb_surface_define(dev_priv, size, &metadata, &srf);
1530 if (ret != 0) {
1531 VMW_DEBUG_USER("Failed to define surface.\n");
1532 return ret;
1533 }
1534
1535 user_srf = container_of(srf, struct vmw_user_surface, srf);
1536 if (drm_is_primary_client(file_priv))
1537 user_srf->master = drm_file_get_master(file_priv);
1538
1539 res = &user_srf->srf.res;
1540
1541 if (req->base.buffer_handle != SVGA3D_INVALID_ID) {
1542 ret = vmw_user_bo_lookup(tfile, req->base.buffer_handle,
1543 &res->backup,
1544 &user_srf->backup_base);
1545 if (ret == 0) {
1546 if (res->backup->base.base.size < res->backup_size) {
1547 VMW_DEBUG_USER("Surface backup buffer too small.\n");
1548 vmw_bo_unreference(&res->backup);
1549 ret = -EINVAL;
1550 goto out_unlock;
1551 } else {
1552 backup_handle = req->base.buffer_handle;
1553 }
1554 }
1555 } else if (req->base.drm_surface_flags &
1556 (drm_vmw_surface_flag_create_buffer |
1557 drm_vmw_surface_flag_coherent))
1558 ret = vmw_user_bo_alloc(dev_priv, tfile,
1559 res->backup_size,
1560 req->base.drm_surface_flags &
1561 drm_vmw_surface_flag_shareable,
1562 &backup_handle,
1563 &res->backup,
1564 &user_srf->backup_base);
1565
1566 if (unlikely(ret != 0)) {
1567 vmw_resource_unreference(&res);
1568 goto out_unlock;
1569 }
1570
1571 if (req->base.drm_surface_flags & drm_vmw_surface_flag_coherent) {
1572 struct vmw_buffer_object *backup = res->backup;
1573
1574 ttm_bo_reserve(&backup->base, false, false, NULL);
1575 if (!res->func->dirty_alloc)
1576 ret = -EINVAL;
1577 if (!ret)
1578 ret = vmw_bo_dirty_add(backup);
1579 if (!ret) {
1580 res->coherent = true;
1581 ret = res->func->dirty_alloc(res);
1582 }
1583 ttm_bo_unreserve(&backup->base);
1584 if (ret) {
1585 vmw_resource_unreference(&res);
1586 goto out_unlock;
1587 }
1588
1589 }
1590
1591 tmp = vmw_resource_reference(res);
1592 ret = ttm_prime_object_init(tfile, res->backup_size, &user_srf->prime,
1593 req->base.drm_surface_flags &
1594 drm_vmw_surface_flag_shareable,
1595 VMW_RES_SURFACE,
1596 &vmw_user_surface_base_release, NULL);
1597
1598 if (unlikely(ret != 0)) {
1599 vmw_resource_unreference(&tmp);
1600 vmw_resource_unreference(&res);
1601 goto out_unlock;
1602 }
1603
1604 rep->handle = user_srf->prime.base.handle;
1605 rep->backup_size = res->backup_size;
1606 if (res->backup) {
1607 rep->buffer_map_handle =
1608 drm_vma_node_offset_addr(&res->backup->base.base.vma_node);
1609 rep->buffer_size = res->backup->base.base.size;
1610 rep->buffer_handle = backup_handle;
1611 } else {
1612 rep->buffer_map_handle = 0;
1613 rep->buffer_size = 0;
1614 rep->buffer_handle = SVGA3D_INVALID_ID;
1615 }
1616
1617 vmw_resource_unreference(&res);
1618
1619out_unlock:
1620 return ret;
1621}
1622
1623/**
1624 * vmw_gb_surface_reference_internal - Ioctl function implementing
1625 * the user surface reference functionality.
1626 *
1627 * @dev: Pointer to a struct drm_device.
1628 * @req: Pointer to user-space request surface arg.
1629 * @rep: Pointer to response to user-space.
1630 * @file_priv: Pointer to a drm file private structure.
1631 */
1632static int
1633vmw_gb_surface_reference_internal(struct drm_device *dev,
1634 struct drm_vmw_surface_arg *req,
1635 struct drm_vmw_gb_surface_ref_ext_rep *rep,
1636 struct drm_file *file_priv)
1637{
1638 struct vmw_private *dev_priv = vmw_priv(dev);
1639 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1640 struct vmw_surface *srf;
1641 struct vmw_user_surface *user_srf;
1642 struct vmw_surface_metadata *metadata;
1643 struct ttm_base_object *base;
1644 uint32_t backup_handle;
1645 int ret;
1646
1647 ret = vmw_surface_handle_reference(dev_priv, file_priv, req->sid,
1648 req->handle_type, &base);
1649 if (unlikely(ret != 0))
1650 return ret;
1651
1652 user_srf = container_of(base, struct vmw_user_surface, prime.base);
1653 srf = &user_srf->srf;
1654 if (!srf->res.backup) {
1655 DRM_ERROR("Shared GB surface is missing a backup buffer.\n");
1656 goto out_bad_resource;
1657 }
1658 metadata = &srf->metadata;
1659
1660 mutex_lock(&dev_priv->cmdbuf_mutex); /* Protect res->backup */
1661 ret = vmw_user_bo_reference(tfile, srf->res.backup, &backup_handle);
1662 mutex_unlock(&dev_priv->cmdbuf_mutex);
1663
1664 if (unlikely(ret != 0)) {
1665 DRM_ERROR("Could not add a reference to a GB surface "
1666 "backup buffer.\n");
1667 (void) ttm_ref_object_base_unref(tfile, base->handle,
1668 TTM_REF_USAGE);
1669 goto out_bad_resource;
1670 }
1671
1672 rep->creq.base.svga3d_flags = SVGA3D_FLAGS_LOWER_32(metadata->flags);
1673 rep->creq.base.format = metadata->format;
1674 rep->creq.base.mip_levels = metadata->mip_levels[0];
1675 rep->creq.base.drm_surface_flags = 0;
1676 rep->creq.base.multisample_count = metadata->multisample_count;
1677 rep->creq.base.autogen_filter = metadata->autogen_filter;
1678 rep->creq.base.array_size = metadata->array_size;
1679 rep->creq.base.buffer_handle = backup_handle;
1680 rep->creq.base.base_size = metadata->base_size;
1681 rep->crep.handle = user_srf->prime.base.handle;
1682 rep->crep.backup_size = srf->res.backup_size;
1683 rep->crep.buffer_handle = backup_handle;
1684 rep->crep.buffer_map_handle =
1685 drm_vma_node_offset_addr(&srf->res.backup->base.base.vma_node);
1686 rep->crep.buffer_size = srf->res.backup->base.base.size;
1687
1688 rep->creq.version = drm_vmw_gb_surface_v1;
1689 rep->creq.svga3d_flags_upper_32_bits =
1690 SVGA3D_FLAGS_UPPER_32(metadata->flags);
1691 rep->creq.multisample_pattern = metadata->multisample_pattern;
1692 rep->creq.quality_level = metadata->quality_level;
1693 rep->creq.must_be_zero = 0;
1694
1695out_bad_resource:
1696 ttm_base_object_unref(&base);
1697
1698 return ret;
1699}
1700
1701/**
1702 * vmw_subres_dirty_add - Add a dirty region to a subresource
1703 * @dirty: The surfaces's dirty tracker.
1704 * @loc_start: The location corresponding to the start of the region.
1705 * @loc_end: The location corresponding to the end of the region.
1706 *
1707 * As we are assuming that @loc_start and @loc_end represent a sequential
1708 * range of backing store memory, if the region spans multiple lines then
1709 * regardless of the x coordinate, the full lines are dirtied.
1710 * Correspondingly if the region spans multiple z slices, then full rather
1711 * than partial z slices are dirtied.
1712 */
1713static void vmw_subres_dirty_add(struct vmw_surface_dirty *dirty,
1714 const struct svga3dsurface_loc *loc_start,
1715 const struct svga3dsurface_loc *loc_end)
1716{
1717 const struct svga3dsurface_cache *cache = &dirty->cache;
1718 SVGA3dBox *box = &dirty->boxes[loc_start->sub_resource];
1719 u32 mip = loc_start->sub_resource % cache->num_mip_levels;
1720 const struct drm_vmw_size *size = &cache->mip[mip].size;
1721 u32 box_c2 = box->z + box->d;
1722
1723 if (WARN_ON(loc_start->sub_resource >= dirty->num_subres))
1724 return;
1725
1726 if (box->d == 0 || box->z > loc_start->z)
1727 box->z = loc_start->z;
1728 if (box_c2 < loc_end->z)
1729 box->d = loc_end->z - box->z;
1730
1731 if (loc_start->z + 1 == loc_end->z) {
1732 box_c2 = box->y + box->h;
1733 if (box->h == 0 || box->y > loc_start->y)
1734 box->y = loc_start->y;
1735 if (box_c2 < loc_end->y)
1736 box->h = loc_end->y - box->y;
1737
1738 if (loc_start->y + 1 == loc_end->y) {
1739 box_c2 = box->x + box->w;
1740 if (box->w == 0 || box->x > loc_start->x)
1741 box->x = loc_start->x;
1742 if (box_c2 < loc_end->x)
1743 box->w = loc_end->x - box->x;
1744 } else {
1745 box->x = 0;
1746 box->w = size->width;
1747 }
1748 } else {
1749 box->y = 0;
1750 box->h = size->height;
1751 box->x = 0;
1752 box->w = size->width;
1753 }
1754}
1755
1756/**
1757 * vmw_subres_dirty_full - Mark a full subresource as dirty
1758 * @dirty: The surface's dirty tracker.
1759 * @subres: The subresource
1760 */
1761static void vmw_subres_dirty_full(struct vmw_surface_dirty *dirty, u32 subres)
1762{
1763 const struct svga3dsurface_cache *cache = &dirty->cache;
1764 u32 mip = subres % cache->num_mip_levels;
1765 const struct drm_vmw_size *size = &cache->mip[mip].size;
1766 SVGA3dBox *box = &dirty->boxes[subres];
1767
1768 box->x = 0;
1769 box->y = 0;
1770 box->z = 0;
1771 box->w = size->width;
1772 box->h = size->height;
1773 box->d = size->depth;
1774}
1775
1776/*
1777 * vmw_surface_tex_dirty_add_range - The dirty_add_range callback for texture
1778 * surfaces.
1779 */
1780static void vmw_surface_tex_dirty_range_add(struct vmw_resource *res,
1781 size_t start, size_t end)
1782{
1783 struct vmw_surface_dirty *dirty =
1784 (struct vmw_surface_dirty *) res->dirty;
1785 size_t backup_end = res->backup_offset + res->backup_size;
1786 struct svga3dsurface_loc loc1, loc2;
1787 const struct svga3dsurface_cache *cache;
1788
1789 start = max_t(size_t, start, res->backup_offset) - res->backup_offset;
1790 end = min(end, backup_end) - res->backup_offset;
1791 cache = &dirty->cache;
1792 svga3dsurface_get_loc(cache, &loc1, start);
1793 svga3dsurface_get_loc(cache, &loc2, end - 1);
1794 svga3dsurface_inc_loc(cache, &loc2);
1795
1796 if (loc1.sheet != loc2.sheet) {
1797 u32 sub_res;
1798
1799 /*
1800 * Multiple multisample sheets. To do this in an optimized
1801 * fashion, compute the dirty region for each sheet and the
1802 * resulting union. Since this is not a common case, just dirty
1803 * the whole surface.
1804 */
1805 for (sub_res = 0; sub_res < dirty->num_subres; ++sub_res)
1806 vmw_subres_dirty_full(dirty, sub_res);
1807 return;
1808 }
1809 if (loc1.sub_resource + 1 == loc2.sub_resource) {
1810 /* Dirty range covers a single sub-resource */
1811 vmw_subres_dirty_add(dirty, &loc1, &loc2);
1812 } else {
1813 /* Dirty range covers multiple sub-resources */
1814 struct svga3dsurface_loc loc_min, loc_max;
1815 u32 sub_res;
1816
1817 svga3dsurface_max_loc(cache, loc1.sub_resource, &loc_max);
1818 vmw_subres_dirty_add(dirty, &loc1, &loc_max);
1819 svga3dsurface_min_loc(cache, loc2.sub_resource - 1, &loc_min);
1820 vmw_subres_dirty_add(dirty, &loc_min, &loc2);
1821 for (sub_res = loc1.sub_resource + 1;
1822 sub_res < loc2.sub_resource - 1; ++sub_res)
1823 vmw_subres_dirty_full(dirty, sub_res);
1824 }
1825}
1826
1827/*
1828 * vmw_surface_tex_dirty_add_range - The dirty_add_range callback for buffer
1829 * surfaces.
1830 */
1831static void vmw_surface_buf_dirty_range_add(struct vmw_resource *res,
1832 size_t start, size_t end)
1833{
1834 struct vmw_surface_dirty *dirty =
1835 (struct vmw_surface_dirty *) res->dirty;
1836 const struct svga3dsurface_cache *cache = &dirty->cache;
1837 size_t backup_end = res->backup_offset + cache->mip_chain_bytes;
1838 SVGA3dBox *box = &dirty->boxes[0];
1839 u32 box_c2;
1840
1841 box->h = box->d = 1;
1842 start = max_t(size_t, start, res->backup_offset) - res->backup_offset;
1843 end = min(end, backup_end) - res->backup_offset;
1844 box_c2 = box->x + box->w;
1845 if (box->w == 0 || box->x > start)
1846 box->x = start;
1847 if (box_c2 < end)
1848 box->w = end - box->x;
1849}
1850
1851/*
1852 * vmw_surface_tex_dirty_add_range - The dirty_add_range callback for surfaces
1853 */
1854static void vmw_surface_dirty_range_add(struct vmw_resource *res, size_t start,
1855 size_t end)
1856{
1857 struct vmw_surface *srf = vmw_res_to_srf(res);
1858
1859 if (WARN_ON(end <= res->backup_offset ||
1860 start >= res->backup_offset + res->backup_size))
1861 return;
1862
1863 if (srf->metadata.format == SVGA3D_BUFFER)
1864 vmw_surface_buf_dirty_range_add(res, start, end);
1865 else
1866 vmw_surface_tex_dirty_range_add(res, start, end);
1867}
1868
1869/*
1870 * vmw_surface_dirty_sync - The surface's dirty_sync callback.
1871 */
1872static int vmw_surface_dirty_sync(struct vmw_resource *res)
1873{
1874 struct vmw_private *dev_priv = res->dev_priv;
1875 u32 i, num_dirty;
1876 struct vmw_surface_dirty *dirty =
1877 (struct vmw_surface_dirty *) res->dirty;
1878 size_t alloc_size;
1879 const struct svga3dsurface_cache *cache = &dirty->cache;
1880 struct {
1881 SVGA3dCmdHeader header;
1882 SVGA3dCmdDXUpdateSubResource body;
1883 } *cmd1;
1884 struct {
1885 SVGA3dCmdHeader header;
1886 SVGA3dCmdUpdateGBImage body;
1887 } *cmd2;
1888 void *cmd;
1889
1890 num_dirty = 0;
1891 for (i = 0; i < dirty->num_subres; ++i) {
1892 const SVGA3dBox *box = &dirty->boxes[i];
1893
1894 if (box->d)
1895 num_dirty++;
1896 }
1897
1898 if (!num_dirty)
1899 goto out;
1900
1901 alloc_size = num_dirty * ((has_sm4_context(dev_priv)) ? sizeof(*cmd1) : sizeof(*cmd2));
1902 cmd = VMW_CMD_RESERVE(dev_priv, alloc_size);
1903 if (!cmd)
1904 return -ENOMEM;
1905
1906 cmd1 = cmd;
1907 cmd2 = cmd;
1908
1909 for (i = 0; i < dirty->num_subres; ++i) {
1910 const SVGA3dBox *box = &dirty->boxes[i];
1911
1912 if (!box->d)
1913 continue;
1914
1915 /*
1916 * DX_UPDATE_SUBRESOURCE is aware of array surfaces.
1917 * UPDATE_GB_IMAGE is not.
1918 */
1919 if (has_sm4_context(dev_priv)) {
1920 cmd1->header.id = SVGA_3D_CMD_DX_UPDATE_SUBRESOURCE;
1921 cmd1->header.size = sizeof(cmd1->body);
1922 cmd1->body.sid = res->id;
1923 cmd1->body.subResource = i;
1924 cmd1->body.box = *box;
1925 cmd1++;
1926 } else {
1927 cmd2->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
1928 cmd2->header.size = sizeof(cmd2->body);
1929 cmd2->body.image.sid = res->id;
1930 cmd2->body.image.face = i / cache->num_mip_levels;
1931 cmd2->body.image.mipmap = i -
1932 (cache->num_mip_levels * cmd2->body.image.face);
1933 cmd2->body.box = *box;
1934 cmd2++;
1935 }
1936
1937 }
1938 vmw_cmd_commit(dev_priv, alloc_size);
1939 out:
1940 memset(&dirty->boxes[0], 0, sizeof(dirty->boxes[0]) *
1941 dirty->num_subres);
1942
1943 return 0;
1944}
1945
1946/*
1947 * vmw_surface_dirty_alloc - The surface's dirty_alloc callback.
1948 */
1949static int vmw_surface_dirty_alloc(struct vmw_resource *res)
1950{
1951 struct vmw_surface *srf = vmw_res_to_srf(res);
1952 const struct vmw_surface_metadata *metadata = &srf->metadata;
1953 struct vmw_surface_dirty *dirty;
1954 u32 num_layers = 1;
1955 u32 num_mip;
1956 u32 num_subres;
1957 u32 num_samples;
1958 size_t dirty_size, acc_size;
1959 static struct ttm_operation_ctx ctx = {
1960 .interruptible = false,
1961 .no_wait_gpu = false
1962 };
1963 int ret;
1964
1965 if (metadata->array_size)
1966 num_layers = metadata->array_size;
1967 else if (metadata->flags & SVGA3D_SURFACE_CUBEMAP)
1968 num_layers *= SVGA3D_MAX_SURFACE_FACES;
1969
1970 num_mip = metadata->mip_levels[0];
1971 if (!num_mip)
1972 num_mip = 1;
1973
1974 num_subres = num_layers * num_mip;
1975 dirty_size = struct_size(dirty, boxes, num_subres);
1976 acc_size = ttm_round_pot(dirty_size);
1977 ret = ttm_mem_global_alloc(vmw_mem_glob(res->dev_priv),
1978 acc_size, &ctx);
1979 if (ret) {
1980 VMW_DEBUG_USER("Out of graphics memory for surface "
1981 "dirty tracker.\n");
1982 return ret;
1983 }
1984
1985 dirty = kvzalloc(dirty_size, GFP_KERNEL);
1986 if (!dirty) {
1987 ret = -ENOMEM;
1988 goto out_no_dirty;
1989 }
1990
1991 num_samples = max_t(u32, 1, metadata->multisample_count);
1992 ret = svga3dsurface_setup_cache(&metadata->base_size, metadata->format,
1993 num_mip, num_layers, num_samples,
1994 &dirty->cache);
1995 if (ret)
1996 goto out_no_cache;
1997
1998 dirty->num_subres = num_subres;
1999 dirty->size = acc_size;
2000 res->dirty = (struct vmw_resource_dirty *) dirty;
2001
2002 return 0;
2003
2004out_no_cache:
2005 kvfree(dirty);
2006out_no_dirty:
2007 ttm_mem_global_free(vmw_mem_glob(res->dev_priv), acc_size);
2008 return ret;
2009}
2010
2011/*
2012 * vmw_surface_dirty_free - The surface's dirty_free callback
2013 */
2014static void vmw_surface_dirty_free(struct vmw_resource *res)
2015{
2016 struct vmw_surface_dirty *dirty =
2017 (struct vmw_surface_dirty *) res->dirty;
2018 size_t acc_size = dirty->size;
2019
2020 kvfree(dirty);
2021 ttm_mem_global_free(vmw_mem_glob(res->dev_priv), acc_size);
2022 res->dirty = NULL;
2023}
2024
2025/*
2026 * vmw_surface_clean - The surface's clean callback
2027 */
2028static int vmw_surface_clean(struct vmw_resource *res)
2029{
2030 struct vmw_private *dev_priv = res->dev_priv;
2031 size_t alloc_size;
2032 struct {
2033 SVGA3dCmdHeader header;
2034 SVGA3dCmdReadbackGBSurface body;
2035 } *cmd;
2036
2037 alloc_size = sizeof(*cmd);
2038 cmd = VMW_CMD_RESERVE(dev_priv, alloc_size);
2039 if (!cmd)
2040 return -ENOMEM;
2041
2042 cmd->header.id = SVGA_3D_CMD_READBACK_GB_SURFACE;
2043 cmd->header.size = sizeof(cmd->body);
2044 cmd->body.sid = res->id;
2045 vmw_cmd_commit(dev_priv, alloc_size);
2046
2047 return 0;
2048}
2049
2050/*
2051 * vmw_gb_surface_define - Define a private GB surface
2052 *
2053 * @dev_priv: Pointer to a device private.
2054 * @user_accounting_size: Used to track user-space memory usage, set
2055 * to 0 for kernel mode only memory
2056 * @metadata: Metadata representing the surface to create.
2057 * @user_srf_out: allocated user_srf. Set to NULL on failure.
2058 *
2059 * GB surfaces allocated by this function will not have a user mode handle, and
2060 * thus will only be visible to vmwgfx. For optimization reasons the
2061 * surface may later be given a user mode handle by another function to make
2062 * it available to user mode drivers.
2063 */
2064int vmw_gb_surface_define(struct vmw_private *dev_priv,
2065 uint32_t user_accounting_size,
2066 const struct vmw_surface_metadata *req,
2067 struct vmw_surface **srf_out)
2068{
2069 struct vmw_surface_metadata *metadata;
2070 struct vmw_user_surface *user_srf;
2071 struct vmw_surface *srf;
2072 struct ttm_operation_ctx ctx = {
2073 .interruptible = true,
2074 .no_wait_gpu = false
2075 };
2076 u32 sample_count = 1;
2077 u32 num_layers = 1;
2078 int ret;
2079
2080 *srf_out = NULL;
2081
2082 if (req->scanout) {
2083 if (!svga3dsurface_is_screen_target_format(req->format)) {
2084 VMW_DEBUG_USER("Invalid Screen Target surface format.");
2085 return -EINVAL;
2086 }
2087
2088 if (req->base_size.width > dev_priv->texture_max_width ||
2089 req->base_size.height > dev_priv->texture_max_height) {
2090 VMW_DEBUG_USER("%ux%u\n, exceed max surface size %ux%u",
2091 req->base_size.width,
2092 req->base_size.height,
2093 dev_priv->texture_max_width,
2094 dev_priv->texture_max_height);
2095 return -EINVAL;
2096 }
2097 } else {
2098 const struct svga3d_surface_desc *desc =
2099 svga3dsurface_get_desc(req->format);
2100
2101 if (desc->block_desc == SVGA3DBLOCKDESC_NONE) {
2102 VMW_DEBUG_USER("Invalid surface format.\n");
2103 return -EINVAL;
2104 }
2105 }
2106
2107 if (req->autogen_filter != SVGA3D_TEX_FILTER_NONE)
2108 return -EINVAL;
2109
2110 if (req->num_sizes != 1)
2111 return -EINVAL;
2112
2113 if (req->sizes != NULL)
2114 return -EINVAL;
2115
2116 ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
2117 user_accounting_size, &ctx);
2118 if (ret != 0) {
2119 if (ret != -ERESTARTSYS)
2120 DRM_ERROR("Out of graphics memory for surface.\n");
2121 goto out_unlock;
2122 }
2123
2124 user_srf = kzalloc(sizeof(*user_srf), GFP_KERNEL);
2125 if (unlikely(!user_srf)) {
2126 ret = -ENOMEM;
2127 goto out_no_user_srf;
2128 }
2129
2130 *srf_out = &user_srf->srf;
2131 user_srf->size = user_accounting_size;
2132 user_srf->prime.base.shareable = false;
2133 user_srf->prime.base.tfile = NULL;
2134
2135 srf = &user_srf->srf;
2136 srf->metadata = *req;
2137 srf->offsets = NULL;
2138
2139 metadata = &srf->metadata;
2140
2141 if (metadata->array_size)
2142 num_layers = req->array_size;
2143 else if (metadata->flags & SVGA3D_SURFACE_CUBEMAP)
2144 num_layers = SVGA3D_MAX_SURFACE_FACES;
2145
2146 if (metadata->flags & SVGA3D_SURFACE_MULTISAMPLE)
2147 sample_count = metadata->multisample_count;
2148
2149 srf->res.backup_size =
2150 svga3dsurface_get_serialized_size_extended(metadata->format,
2151 metadata->base_size,
2152 metadata->mip_levels[0],
2153 num_layers,
2154 sample_count);
2155
2156 if (metadata->flags & SVGA3D_SURFACE_BIND_STREAM_OUTPUT)
2157 srf->res.backup_size += sizeof(SVGA3dDXSOState);
2158
2159 /*
2160 * Don't set SVGA3D_SURFACE_SCREENTARGET flag for a scanout surface with
2161 * size greater than STDU max width/height. This is really a workaround
2162 * to support creation of big framebuffer requested by some user-space
2163 * for whole topology. That big framebuffer won't really be used for
2164 * binding with screen target as during prepare_fb a separate surface is
2165 * created so it's safe to ignore SVGA3D_SURFACE_SCREENTARGET flag.
2166 */
2167 if (dev_priv->active_display_unit == vmw_du_screen_target &&
2168 metadata->scanout &&
2169 metadata->base_size.width <= dev_priv->stdu_max_width &&
2170 metadata->base_size.height <= dev_priv->stdu_max_height)
2171 metadata->flags |= SVGA3D_SURFACE_SCREENTARGET;
2172
2173 /*
2174 * From this point, the generic resource management functions
2175 * destroy the object on failure.
2176 */
2177 ret = vmw_surface_init(dev_priv, srf, vmw_user_surface_free);
2178
2179 return ret;
2180
2181out_no_user_srf:
2182 ttm_mem_global_free(vmw_mem_glob(dev_priv), user_accounting_size);
2183
2184out_unlock:
2185 return ret;
2186}