1 /**************************************************************************
3 * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "vmwgfx_drv.h"
29 #include <drm/vmwgfx_drm.h>
30 #include <drm/ttm/ttm_object.h>
31 #include <drm/ttm/ttm_placement.h>
33 #include "vmwgfx_resource_priv.h"
35 #define VMW_RES_EVICT_ERR_COUNT 10
37 struct vmw_user_dma_buffer {
38 struct ttm_prime_object prime;
39 struct vmw_dma_buffer dma;
42 struct vmw_bo_user_rep {
48 struct vmw_resource res;
52 struct vmw_user_stream {
53 struct ttm_base_object base;
54 struct vmw_stream stream;
58 static uint64_t vmw_user_stream_size;
60 static const struct vmw_res_func vmw_stream_func = {
61 .res_type = vmw_res_stream,
62 .needs_backup = false,
64 .type_name = "video streams",
65 .backup_placement = NULL,
72 static inline struct vmw_dma_buffer *
73 vmw_dma_buffer(struct ttm_buffer_object *bo)
75 return container_of(bo, struct vmw_dma_buffer, base);
78 static inline struct vmw_user_dma_buffer *
79 vmw_user_dma_buffer(struct ttm_buffer_object *bo)
81 struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
82 return container_of(vmw_bo, struct vmw_user_dma_buffer, dma);
85 struct vmw_resource *vmw_resource_reference(struct vmw_resource *res)
93 * vmw_resource_release_id - release a resource id to the id manager.
95 * @res: Pointer to the resource.
97 * Release the resource id to the resource id manager and set it to -1
99 void vmw_resource_release_id(struct vmw_resource *res)
101 struct vmw_private *dev_priv = res->dev_priv;
102 struct idr *idr = &dev_priv->res_idr[res->func->res_type];
104 write_lock(&dev_priv->resource_lock);
106 idr_remove(idr, res->id);
108 write_unlock(&dev_priv->resource_lock);
111 static void vmw_resource_release(struct kref *kref)
113 struct vmw_resource *res =
114 container_of(kref, struct vmw_resource, kref);
115 struct vmw_private *dev_priv = res->dev_priv;
117 struct idr *idr = &dev_priv->res_idr[res->func->res_type];
120 list_del_init(&res->lru_head);
121 write_unlock(&dev_priv->resource_lock);
123 struct ttm_buffer_object *bo = &res->backup->base;
125 ttm_bo_reserve(bo, false, false, false, 0);
126 if (!list_empty(&res->mob_head) &&
127 res->func->unbind != NULL) {
128 struct ttm_validate_buffer val_buf;
131 res->func->unbind(res, false, &val_buf);
133 res->backup_dirty = false;
134 list_del_init(&res->mob_head);
135 ttm_bo_unreserve(bo);
136 vmw_dmabuf_unreference(&res->backup);
139 if (likely(res->hw_destroy != NULL))
140 res->hw_destroy(res);
143 if (res->res_free != NULL)
148 write_lock(&dev_priv->resource_lock);
154 void vmw_resource_unreference(struct vmw_resource **p_res)
156 struct vmw_resource *res = *p_res;
157 struct vmw_private *dev_priv = res->dev_priv;
160 write_lock(&dev_priv->resource_lock);
161 kref_put(&res->kref, vmw_resource_release);
162 write_unlock(&dev_priv->resource_lock);
167 * vmw_resource_alloc_id - release a resource id to the id manager.
169 * @res: Pointer to the resource.
171 * Allocate the lowest free resource from the resource manager, and set
172 * @res->id to that id. Returns 0 on success and -ENOMEM on failure.
174 int vmw_resource_alloc_id(struct vmw_resource *res)
176 struct vmw_private *dev_priv = res->dev_priv;
178 struct idr *idr = &dev_priv->res_idr[res->func->res_type];
180 BUG_ON(res->id != -1);
182 idr_preload(GFP_KERNEL);
183 write_lock(&dev_priv->resource_lock);
185 ret = idr_alloc(idr, res, 1, 0, GFP_NOWAIT);
189 write_unlock(&dev_priv->resource_lock);
191 return ret < 0 ? ret : 0;
195 * vmw_resource_init - initialize a struct vmw_resource
197 * @dev_priv: Pointer to a device private struct.
198 * @res: The struct vmw_resource to initialize.
199 * @obj_type: Resource object type.
200 * @delay_id: Boolean whether to defer device id allocation until
201 * the first validation.
202 * @res_free: Resource destructor.
203 * @func: Resource function table.
205 int vmw_resource_init(struct vmw_private *dev_priv, struct vmw_resource *res,
207 void (*res_free) (struct vmw_resource *res),
208 const struct vmw_res_func *func)
210 kref_init(&res->kref);
211 res->hw_destroy = NULL;
212 res->res_free = res_free;
214 res->dev_priv = dev_priv;
216 INIT_LIST_HEAD(&res->lru_head);
217 INIT_LIST_HEAD(&res->mob_head);
220 res->backup_offset = 0;
221 res->backup_dirty = false;
222 res->res_dirty = false;
226 return vmw_resource_alloc_id(res);
230 * vmw_resource_activate
232 * @res: Pointer to the newly created resource
233 * @hw_destroy: Destroy function. NULL if none.
235 * Activate a resource after the hardware has been made aware of it.
236 * Set tye destroy function to @destroy. Typically this frees the
237 * resource and destroys the hardware resources associated with it.
238 * Activate basically means that the function vmw_resource_lookup will
241 void vmw_resource_activate(struct vmw_resource *res,
242 void (*hw_destroy) (struct vmw_resource *))
244 struct vmw_private *dev_priv = res->dev_priv;
246 write_lock(&dev_priv->resource_lock);
248 res->hw_destroy = hw_destroy;
249 write_unlock(&dev_priv->resource_lock);
252 struct vmw_resource *vmw_resource_lookup(struct vmw_private *dev_priv,
253 struct idr *idr, int id)
255 struct vmw_resource *res;
257 read_lock(&dev_priv->resource_lock);
258 res = idr_find(idr, id);
259 if (res && res->avail)
260 kref_get(&res->kref);
263 read_unlock(&dev_priv->resource_lock);
265 if (unlikely(res == NULL))
272 * vmw_user_resource_lookup_handle - lookup a struct resource from a
273 * TTM user-space handle and perform basic type checks
275 * @dev_priv: Pointer to a device private struct
276 * @tfile: Pointer to a struct ttm_object_file identifying the caller
277 * @handle: The TTM user-space handle
278 * @converter: Pointer to an object describing the resource type
279 * @p_res: On successful return the location pointed to will contain
280 * a pointer to a refcounted struct vmw_resource.
282 * If the handle can't be found or is associated with an incorrect resource
283 * type, -EINVAL will be returned.
285 int vmw_user_resource_lookup_handle(struct vmw_private *dev_priv,
286 struct ttm_object_file *tfile,
288 const struct vmw_user_resource_conv
290 struct vmw_resource **p_res)
292 struct ttm_base_object *base;
293 struct vmw_resource *res;
296 base = ttm_base_object_lookup(tfile, handle);
297 if (unlikely(base == NULL))
300 if (unlikely(ttm_base_object_type(base) != converter->object_type))
301 goto out_bad_resource;
303 res = converter->base_obj_to_res(base);
305 read_lock(&dev_priv->resource_lock);
306 if (!res->avail || res->res_free != converter->res_free) {
307 read_unlock(&dev_priv->resource_lock);
308 goto out_bad_resource;
311 kref_get(&res->kref);
312 read_unlock(&dev_priv->resource_lock);
318 ttm_base_object_unref(&base);
324 * Helper function that looks either a surface or dmabuf.
326 * The pointer this pointed at by out_surf and out_buf needs to be null.
328 int vmw_user_lookup_handle(struct vmw_private *dev_priv,
329 struct ttm_object_file *tfile,
331 struct vmw_surface **out_surf,
332 struct vmw_dma_buffer **out_buf)
334 struct vmw_resource *res;
337 BUG_ON(*out_surf || *out_buf);
339 ret = vmw_user_resource_lookup_handle(dev_priv, tfile, handle,
340 user_surface_converter,
343 *out_surf = vmw_res_to_srf(res);
348 ret = vmw_user_dmabuf_lookup(tfile, handle, out_buf);
357 * vmw_dmabuf_acc_size - Calculate the pinned memory usage of buffers
359 * @dev_priv: Pointer to a struct vmw_private identifying the device.
360 * @size: The requested buffer size.
361 * @user: Whether this is an ordinary dma buffer or a user dma buffer.
363 static size_t vmw_dmabuf_acc_size(struct vmw_private *dev_priv, size_t size,
366 static size_t struct_size, user_struct_size;
367 size_t num_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
368 size_t page_array_size = ttm_round_pot(num_pages * sizeof(void *));
370 if (unlikely(struct_size == 0)) {
371 size_t backend_size = ttm_round_pot(vmw_tt_size);
373 struct_size = backend_size +
374 ttm_round_pot(sizeof(struct vmw_dma_buffer));
375 user_struct_size = backend_size +
376 ttm_round_pot(sizeof(struct vmw_user_dma_buffer));
379 if (dev_priv->map_mode == vmw_dma_alloc_coherent)
381 ttm_round_pot(num_pages * sizeof(dma_addr_t));
383 return ((user) ? user_struct_size : struct_size) +
387 void vmw_dmabuf_bo_free(struct ttm_buffer_object *bo)
389 struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
394 static void vmw_user_dmabuf_destroy(struct ttm_buffer_object *bo)
396 struct vmw_user_dma_buffer *vmw_user_bo = vmw_user_dma_buffer(bo);
398 ttm_prime_object_kfree(vmw_user_bo, prime);
401 int vmw_dmabuf_init(struct vmw_private *dev_priv,
402 struct vmw_dma_buffer *vmw_bo,
403 size_t size, struct ttm_placement *placement,
405 void (*bo_free) (struct ttm_buffer_object *bo))
407 struct ttm_bo_device *bdev = &dev_priv->bdev;
410 bool user = (bo_free == &vmw_user_dmabuf_destroy);
412 BUG_ON(!bo_free && (!user && (bo_free != vmw_dmabuf_bo_free)));
414 acc_size = vmw_dmabuf_acc_size(dev_priv, size, user);
415 memset(vmw_bo, 0, sizeof(*vmw_bo));
417 INIT_LIST_HEAD(&vmw_bo->res_list);
419 ret = ttm_bo_init(bdev, &vmw_bo->base, size,
420 (user) ? ttm_bo_type_device :
421 ttm_bo_type_kernel, placement,
423 NULL, acc_size, NULL, bo_free);
427 static void vmw_user_dmabuf_release(struct ttm_base_object **p_base)
429 struct vmw_user_dma_buffer *vmw_user_bo;
430 struct ttm_base_object *base = *p_base;
431 struct ttm_buffer_object *bo;
435 if (unlikely(base == NULL))
438 vmw_user_bo = container_of(base, struct vmw_user_dma_buffer,
440 bo = &vmw_user_bo->dma.base;
444 static void vmw_user_dmabuf_ref_obj_release(struct ttm_base_object *base,
445 enum ttm_ref_type ref_type)
447 struct vmw_user_dma_buffer *user_bo;
448 user_bo = container_of(base, struct vmw_user_dma_buffer, prime.base);
451 case TTM_REF_SYNCCPU_WRITE:
452 ttm_bo_synccpu_write_release(&user_bo->dma.base);
460 * vmw_user_dmabuf_alloc - Allocate a user dma buffer
462 * @dev_priv: Pointer to a struct device private.
463 * @tfile: Pointer to a struct ttm_object_file on which to register the user
465 * @size: Size of the dma buffer.
466 * @shareable: Boolean whether the buffer is shareable with other open files.
467 * @handle: Pointer to where the handle value should be assigned.
468 * @p_dma_buf: Pointer to where the refcounted struct vmw_dma_buffer pointer
469 * should be assigned.
471 int vmw_user_dmabuf_alloc(struct vmw_private *dev_priv,
472 struct ttm_object_file *tfile,
476 struct vmw_dma_buffer **p_dma_buf)
478 struct vmw_user_dma_buffer *user_bo;
479 struct ttm_buffer_object *tmp;
482 user_bo = kzalloc(sizeof(*user_bo), GFP_KERNEL);
483 if (unlikely(user_bo == NULL)) {
484 DRM_ERROR("Failed to allocate a buffer.\n");
488 ret = vmw_dmabuf_init(dev_priv, &user_bo->dma, size,
489 (dev_priv->has_mob) ?
491 &vmw_vram_sys_placement, true,
492 &vmw_user_dmabuf_destroy);
493 if (unlikely(ret != 0))
496 tmp = ttm_bo_reference(&user_bo->dma.base);
497 ret = ttm_prime_object_init(tfile,
502 &vmw_user_dmabuf_release,
503 &vmw_user_dmabuf_ref_obj_release);
504 if (unlikely(ret != 0)) {
506 goto out_no_base_object;
509 *p_dma_buf = &user_bo->dma;
510 *handle = user_bo->prime.base.hash.key;
517 * vmw_user_dmabuf_verify_access - verify access permissions on this
520 * @bo: Pointer to the buffer object being accessed
521 * @tfile: Identifying the caller.
523 int vmw_user_dmabuf_verify_access(struct ttm_buffer_object *bo,
524 struct ttm_object_file *tfile)
526 struct vmw_user_dma_buffer *vmw_user_bo;
528 if (unlikely(bo->destroy != vmw_user_dmabuf_destroy))
531 vmw_user_bo = vmw_user_dma_buffer(bo);
532 return (vmw_user_bo->prime.base.tfile == tfile ||
533 vmw_user_bo->prime.base.shareable) ? 0 : -EPERM;
537 * vmw_user_dmabuf_synccpu_grab - Grab a struct vmw_user_dma_buffer for cpu
538 * access, idling previous GPU operations on the buffer and optionally
539 * blocking it for further command submissions.
541 * @user_bo: Pointer to the buffer object being grabbed for CPU access
542 * @tfile: Identifying the caller.
543 * @flags: Flags indicating how the grab should be performed.
545 * A blocking grab will be automatically released when @tfile is closed.
547 static int vmw_user_dmabuf_synccpu_grab(struct vmw_user_dma_buffer *user_bo,
548 struct ttm_object_file *tfile,
551 struct ttm_buffer_object *bo = &user_bo->dma.base;
555 if (flags & drm_vmw_synccpu_allow_cs) {
556 struct ttm_bo_device *bdev = bo->bdev;
558 spin_lock(&bdev->fence_lock);
559 ret = ttm_bo_wait(bo, false, true,
560 !!(flags & drm_vmw_synccpu_dontblock));
561 spin_unlock(&bdev->fence_lock);
565 ret = ttm_bo_synccpu_write_grab
566 (bo, !!(flags & drm_vmw_synccpu_dontblock));
567 if (unlikely(ret != 0))
570 ret = ttm_ref_object_add(tfile, &user_bo->prime.base,
571 TTM_REF_SYNCCPU_WRITE, &existed);
572 if (ret != 0 || existed)
573 ttm_bo_synccpu_write_release(&user_bo->dma.base);
579 * vmw_user_dmabuf_synccpu_release - Release a previous grab for CPU access,
580 * and unblock command submission on the buffer if blocked.
582 * @handle: Handle identifying the buffer object.
583 * @tfile: Identifying the caller.
584 * @flags: Flags indicating the type of release.
586 static int vmw_user_dmabuf_synccpu_release(uint32_t handle,
587 struct ttm_object_file *tfile,
590 if (!(flags & drm_vmw_synccpu_allow_cs))
591 return ttm_ref_object_base_unref(tfile, handle,
592 TTM_REF_SYNCCPU_WRITE);
598 * vmw_user_dmabuf_synccpu_release - ioctl function implementing the synccpu
601 * @dev: Identifies the drm device.
602 * @data: Pointer to the ioctl argument.
603 * @file_priv: Identifies the caller.
605 * This function checks the ioctl arguments for validity and calls the
606 * relevant synccpu functions.
608 int vmw_user_dmabuf_synccpu_ioctl(struct drm_device *dev, void *data,
609 struct drm_file *file_priv)
611 struct drm_vmw_synccpu_arg *arg =
612 (struct drm_vmw_synccpu_arg *) data;
613 struct vmw_dma_buffer *dma_buf;
614 struct vmw_user_dma_buffer *user_bo;
615 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
618 if ((arg->flags & (drm_vmw_synccpu_read | drm_vmw_synccpu_write)) == 0
619 || (arg->flags & ~(drm_vmw_synccpu_read | drm_vmw_synccpu_write |
620 drm_vmw_synccpu_dontblock |
621 drm_vmw_synccpu_allow_cs)) != 0) {
622 DRM_ERROR("Illegal synccpu flags.\n");
627 case drm_vmw_synccpu_grab:
628 ret = vmw_user_dmabuf_lookup(tfile, arg->handle, &dma_buf);
629 if (unlikely(ret != 0))
632 user_bo = container_of(dma_buf, struct vmw_user_dma_buffer,
634 ret = vmw_user_dmabuf_synccpu_grab(user_bo, tfile, arg->flags);
635 vmw_dmabuf_unreference(&dma_buf);
636 if (unlikely(ret != 0 && ret != -ERESTARTSYS &&
638 DRM_ERROR("Failed synccpu grab on handle 0x%08x.\n",
639 (unsigned int) arg->handle);
643 case drm_vmw_synccpu_release:
644 ret = vmw_user_dmabuf_synccpu_release(arg->handle, tfile,
646 if (unlikely(ret != 0)) {
647 DRM_ERROR("Failed synccpu release on handle 0x%08x.\n",
648 (unsigned int) arg->handle);
653 DRM_ERROR("Invalid synccpu operation.\n");
660 int vmw_dmabuf_alloc_ioctl(struct drm_device *dev, void *data,
661 struct drm_file *file_priv)
663 struct vmw_private *dev_priv = vmw_priv(dev);
664 union drm_vmw_alloc_dmabuf_arg *arg =
665 (union drm_vmw_alloc_dmabuf_arg *)data;
666 struct drm_vmw_alloc_dmabuf_req *req = &arg->req;
667 struct drm_vmw_dmabuf_rep *rep = &arg->rep;
668 struct vmw_dma_buffer *dma_buf;
670 struct vmw_master *vmaster = vmw_master(file_priv->master);
673 ret = ttm_read_lock(&vmaster->lock, true);
674 if (unlikely(ret != 0))
677 ret = vmw_user_dmabuf_alloc(dev_priv, vmw_fpriv(file_priv)->tfile,
678 req->size, false, &handle, &dma_buf);
679 if (unlikely(ret != 0))
682 rep->handle = handle;
683 rep->map_handle = drm_vma_node_offset_addr(&dma_buf->base.vma_node);
684 rep->cur_gmr_id = handle;
685 rep->cur_gmr_offset = 0;
687 vmw_dmabuf_unreference(&dma_buf);
690 ttm_read_unlock(&vmaster->lock);
695 int vmw_dmabuf_unref_ioctl(struct drm_device *dev, void *data,
696 struct drm_file *file_priv)
698 struct drm_vmw_unref_dmabuf_arg *arg =
699 (struct drm_vmw_unref_dmabuf_arg *)data;
701 return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
706 int vmw_user_dmabuf_lookup(struct ttm_object_file *tfile,
707 uint32_t handle, struct vmw_dma_buffer **out)
709 struct vmw_user_dma_buffer *vmw_user_bo;
710 struct ttm_base_object *base;
712 base = ttm_base_object_lookup(tfile, handle);
713 if (unlikely(base == NULL)) {
714 printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
715 (unsigned long)handle);
719 if (unlikely(ttm_base_object_type(base) != ttm_buffer_type)) {
720 ttm_base_object_unref(&base);
721 printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
722 (unsigned long)handle);
726 vmw_user_bo = container_of(base, struct vmw_user_dma_buffer,
728 (void)ttm_bo_reference(&vmw_user_bo->dma.base);
729 ttm_base_object_unref(&base);
730 *out = &vmw_user_bo->dma;
735 int vmw_user_dmabuf_reference(struct ttm_object_file *tfile,
736 struct vmw_dma_buffer *dma_buf,
739 struct vmw_user_dma_buffer *user_bo;
741 if (dma_buf->base.destroy != vmw_user_dmabuf_destroy)
744 user_bo = container_of(dma_buf, struct vmw_user_dma_buffer, dma);
746 *handle = user_bo->prime.base.hash.key;
747 return ttm_ref_object_add(tfile, &user_bo->prime.base,
748 TTM_REF_USAGE, NULL);
755 static void vmw_stream_destroy(struct vmw_resource *res)
757 struct vmw_private *dev_priv = res->dev_priv;
758 struct vmw_stream *stream;
761 DRM_INFO("%s: unref\n", __func__);
762 stream = container_of(res, struct vmw_stream, res);
764 ret = vmw_overlay_unref(dev_priv, stream->stream_id);
768 static int vmw_stream_init(struct vmw_private *dev_priv,
769 struct vmw_stream *stream,
770 void (*res_free) (struct vmw_resource *res))
772 struct vmw_resource *res = &stream->res;
775 ret = vmw_resource_init(dev_priv, res, false, res_free,
778 if (unlikely(ret != 0)) {
779 if (res_free == NULL)
782 res_free(&stream->res);
786 ret = vmw_overlay_claim(dev_priv, &stream->stream_id);
788 vmw_resource_unreference(&res);
792 DRM_INFO("%s: claimed\n", __func__);
794 vmw_resource_activate(&stream->res, vmw_stream_destroy);
798 static void vmw_user_stream_free(struct vmw_resource *res)
800 struct vmw_user_stream *stream =
801 container_of(res, struct vmw_user_stream, stream.res);
802 struct vmw_private *dev_priv = res->dev_priv;
804 ttm_base_object_kfree(stream, base);
805 ttm_mem_global_free(vmw_mem_glob(dev_priv),
806 vmw_user_stream_size);
810 * This function is called when user space has no more references on the
811 * base object. It releases the base-object's reference on the resource object.
814 static void vmw_user_stream_base_release(struct ttm_base_object **p_base)
816 struct ttm_base_object *base = *p_base;
817 struct vmw_user_stream *stream =
818 container_of(base, struct vmw_user_stream, base);
819 struct vmw_resource *res = &stream->stream.res;
822 vmw_resource_unreference(&res);
825 int vmw_stream_unref_ioctl(struct drm_device *dev, void *data,
826 struct drm_file *file_priv)
828 struct vmw_private *dev_priv = vmw_priv(dev);
829 struct vmw_resource *res;
830 struct vmw_user_stream *stream;
831 struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
832 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
833 struct idr *idr = &dev_priv->res_idr[vmw_res_stream];
837 res = vmw_resource_lookup(dev_priv, idr, arg->stream_id);
838 if (unlikely(res == NULL))
841 if (res->res_free != &vmw_user_stream_free) {
846 stream = container_of(res, struct vmw_user_stream, stream.res);
847 if (stream->base.tfile != tfile) {
852 ttm_ref_object_base_unref(tfile, stream->base.hash.key, TTM_REF_USAGE);
854 vmw_resource_unreference(&res);
858 int vmw_stream_claim_ioctl(struct drm_device *dev, void *data,
859 struct drm_file *file_priv)
861 struct vmw_private *dev_priv = vmw_priv(dev);
862 struct vmw_user_stream *stream;
863 struct vmw_resource *res;
864 struct vmw_resource *tmp;
865 struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
866 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
867 struct vmw_master *vmaster = vmw_master(file_priv->master);
871 * Approximate idr memory usage with 128 bytes. It will be limited
872 * by maximum number_of streams anyway?
875 if (unlikely(vmw_user_stream_size == 0))
876 vmw_user_stream_size = ttm_round_pot(sizeof(*stream)) + 128;
878 ret = ttm_read_lock(&vmaster->lock, true);
879 if (unlikely(ret != 0))
882 ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
883 vmw_user_stream_size,
885 if (unlikely(ret != 0)) {
886 if (ret != -ERESTARTSYS)
887 DRM_ERROR("Out of graphics memory for stream"
893 stream = kmalloc(sizeof(*stream), GFP_KERNEL);
894 if (unlikely(stream == NULL)) {
895 ttm_mem_global_free(vmw_mem_glob(dev_priv),
896 vmw_user_stream_size);
901 res = &stream->stream.res;
902 stream->base.shareable = false;
903 stream->base.tfile = NULL;
906 * From here on, the destructor takes over resource freeing.
909 ret = vmw_stream_init(dev_priv, &stream->stream, vmw_user_stream_free);
910 if (unlikely(ret != 0))
913 tmp = vmw_resource_reference(res);
914 ret = ttm_base_object_init(tfile, &stream->base, false, VMW_RES_STREAM,
915 &vmw_user_stream_base_release, NULL);
917 if (unlikely(ret != 0)) {
918 vmw_resource_unreference(&tmp);
922 arg->stream_id = res->id;
924 vmw_resource_unreference(&res);
926 ttm_read_unlock(&vmaster->lock);
930 int vmw_user_stream_lookup(struct vmw_private *dev_priv,
931 struct ttm_object_file *tfile,
932 uint32_t *inout_id, struct vmw_resource **out)
934 struct vmw_user_stream *stream;
935 struct vmw_resource *res;
938 res = vmw_resource_lookup(dev_priv, &dev_priv->res_idr[vmw_res_stream],
940 if (unlikely(res == NULL))
943 if (res->res_free != &vmw_user_stream_free) {
948 stream = container_of(res, struct vmw_user_stream, stream.res);
949 if (stream->base.tfile != tfile) {
954 *inout_id = stream->stream.stream_id;
958 vmw_resource_unreference(&res);
964 * vmw_dumb_create - Create a dumb kms buffer
966 * @file_priv: Pointer to a struct drm_file identifying the caller.
967 * @dev: Pointer to the drm device.
968 * @args: Pointer to a struct drm_mode_create_dumb structure
970 * This is a driver callback for the core drm create_dumb functionality.
971 * Note that this is very similar to the vmw_dmabuf_alloc ioctl, except
972 * that the arguments have a different format.
974 int vmw_dumb_create(struct drm_file *file_priv,
975 struct drm_device *dev,
976 struct drm_mode_create_dumb *args)
978 struct vmw_private *dev_priv = vmw_priv(dev);
979 struct vmw_master *vmaster = vmw_master(file_priv->master);
980 struct vmw_dma_buffer *dma_buf;
983 args->pitch = args->width * ((args->bpp + 7) / 8);
984 args->size = args->pitch * args->height;
986 ret = ttm_read_lock(&vmaster->lock, true);
987 if (unlikely(ret != 0))
990 ret = vmw_user_dmabuf_alloc(dev_priv, vmw_fpriv(file_priv)->tfile,
991 args->size, false, &args->handle,
993 if (unlikely(ret != 0))
996 vmw_dmabuf_unreference(&dma_buf);
998 ttm_read_unlock(&vmaster->lock);
1003 * vmw_dumb_map_offset - Return the address space offset of a dumb buffer
1005 * @file_priv: Pointer to a struct drm_file identifying the caller.
1006 * @dev: Pointer to the drm device.
1007 * @handle: Handle identifying the dumb buffer.
1008 * @offset: The address space offset returned.
1010 * This is a driver callback for the core drm dumb_map_offset functionality.
1012 int vmw_dumb_map_offset(struct drm_file *file_priv,
1013 struct drm_device *dev, uint32_t handle,
1016 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1017 struct vmw_dma_buffer *out_buf;
1020 ret = vmw_user_dmabuf_lookup(tfile, handle, &out_buf);
1024 *offset = drm_vma_node_offset_addr(&out_buf->base.vma_node);
1025 vmw_dmabuf_unreference(&out_buf);
1030 * vmw_dumb_destroy - Destroy a dumb boffer
1032 * @file_priv: Pointer to a struct drm_file identifying the caller.
1033 * @dev: Pointer to the drm device.
1034 * @handle: Handle identifying the dumb buffer.
1036 * This is a driver callback for the core drm dumb_destroy functionality.
1038 int vmw_dumb_destroy(struct drm_file *file_priv,
1039 struct drm_device *dev,
1042 return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
1043 handle, TTM_REF_USAGE);
1047 * vmw_resource_buf_alloc - Allocate a backup buffer for a resource.
1049 * @res: The resource for which to allocate a backup buffer.
1050 * @interruptible: Whether any sleeps during allocation should be
1051 * performed while interruptible.
1053 static int vmw_resource_buf_alloc(struct vmw_resource *res,
1056 unsigned long size =
1057 (res->backup_size + PAGE_SIZE - 1) & PAGE_MASK;
1058 struct vmw_dma_buffer *backup;
1061 if (likely(res->backup)) {
1062 BUG_ON(res->backup->base.num_pages * PAGE_SIZE < size);
1066 backup = kzalloc(sizeof(*backup), GFP_KERNEL);
1067 if (unlikely(backup == NULL))
1070 ret = vmw_dmabuf_init(res->dev_priv, backup, res->backup_size,
1071 res->func->backup_placement,
1073 &vmw_dmabuf_bo_free);
1074 if (unlikely(ret != 0))
1077 res->backup = backup;
1084 * vmw_resource_do_validate - Make a resource up-to-date and visible
1087 * @res: The resource to make visible to the device.
1088 * @val_buf: Information about a buffer possibly
1089 * containing backup data if a bind operation is needed.
1091 * On hardware resource shortage, this function returns -EBUSY and
1092 * should be retried once resources have been freed up.
1094 static int vmw_resource_do_validate(struct vmw_resource *res,
1095 struct ttm_validate_buffer *val_buf)
1098 const struct vmw_res_func *func = res->func;
1100 if (unlikely(res->id == -1)) {
1101 ret = func->create(res);
1102 if (unlikely(ret != 0))
1107 ((func->needs_backup && list_empty(&res->mob_head) &&
1108 val_buf->bo != NULL) ||
1109 (!func->needs_backup && val_buf->bo != NULL))) {
1110 ret = func->bind(res, val_buf);
1111 if (unlikely(ret != 0))
1112 goto out_bind_failed;
1113 if (func->needs_backup)
1114 list_add_tail(&res->mob_head, &res->backup->res_list);
1118 * Only do this on write operations, and move to
1119 * vmw_resource_unreserve if it can be called after
1120 * backup buffers have been unreserved. Otherwise
1123 res->res_dirty = true;
1134 * vmw_resource_unreserve - Unreserve a resource previously reserved for
1135 * command submission.
1137 * @res: Pointer to the struct vmw_resource to unreserve.
1138 * @new_backup: Pointer to new backup buffer if command submission
1140 * @new_backup_offset: New backup offset if @new_backup is !NULL.
1142 * Currently unreserving a resource means putting it back on the device's
1143 * resource lru list, so that it can be evicted if necessary.
1145 void vmw_resource_unreserve(struct vmw_resource *res,
1146 struct vmw_dma_buffer *new_backup,
1147 unsigned long new_backup_offset)
1149 struct vmw_private *dev_priv = res->dev_priv;
1151 if (!list_empty(&res->lru_head))
1154 if (new_backup && new_backup != res->backup) {
1157 lockdep_assert_held(&res->backup->base.resv->lock.base);
1158 list_del_init(&res->mob_head);
1159 vmw_dmabuf_unreference(&res->backup);
1162 res->backup = vmw_dmabuf_reference(new_backup);
1163 lockdep_assert_held(&new_backup->base.resv->lock.base);
1164 list_add_tail(&res->mob_head, &new_backup->res_list);
1167 res->backup_offset = new_backup_offset;
1169 if (!res->func->may_evict || res->id == -1)
1172 write_lock(&dev_priv->resource_lock);
1173 list_add_tail(&res->lru_head,
1174 &res->dev_priv->res_lru[res->func->res_type]);
1175 write_unlock(&dev_priv->resource_lock);
1179 * vmw_resource_check_buffer - Check whether a backup buffer is needed
1180 * for a resource and in that case, allocate
1181 * one, reserve and validate it.
1183 * @res: The resource for which to allocate a backup buffer.
1184 * @interruptible: Whether any sleeps during allocation should be
1185 * performed while interruptible.
1186 * @val_buf: On successful return contains data about the
1187 * reserved and validated backup buffer.
1190 vmw_resource_check_buffer(struct vmw_resource *res,
1192 struct ttm_validate_buffer *val_buf)
1194 struct list_head val_list;
1195 bool backup_dirty = false;
1198 if (unlikely(res->backup == NULL)) {
1199 ret = vmw_resource_buf_alloc(res, interruptible);
1200 if (unlikely(ret != 0))
1204 INIT_LIST_HEAD(&val_list);
1205 val_buf->bo = ttm_bo_reference(&res->backup->base);
1206 list_add_tail(&val_buf->head, &val_list);
1207 ret = ttm_eu_reserve_buffers(NULL, &val_list);
1208 if (unlikely(ret != 0))
1209 goto out_no_reserve;
1211 if (res->func->needs_backup && list_empty(&res->mob_head))
1214 backup_dirty = res->backup_dirty;
1215 ret = ttm_bo_validate(&res->backup->base,
1216 res->func->backup_placement,
1219 if (unlikely(ret != 0))
1220 goto out_no_validate;
1225 ttm_eu_backoff_reservation(NULL, &val_list);
1227 ttm_bo_unref(&val_buf->bo);
1229 vmw_dmabuf_unreference(&res->backup);
1235 * vmw_resource_reserve - Reserve a resource for command submission
1237 * @res: The resource to reserve.
1239 * This function takes the resource off the LRU list and make sure
1240 * a backup buffer is present for guest-backed resources. However,
1241 * the buffer may not be bound to the resource at this point.
1244 int vmw_resource_reserve(struct vmw_resource *res, bool no_backup)
1246 struct vmw_private *dev_priv = res->dev_priv;
1249 write_lock(&dev_priv->resource_lock);
1250 list_del_init(&res->lru_head);
1251 write_unlock(&dev_priv->resource_lock);
1253 if (res->func->needs_backup && res->backup == NULL &&
1255 ret = vmw_resource_buf_alloc(res, true);
1256 if (unlikely(ret != 0))
1264 * vmw_resource_backoff_reservation - Unreserve and unreference a
1267 * @val_buf: Backup buffer information.
1270 vmw_resource_backoff_reservation(struct ttm_validate_buffer *val_buf)
1272 struct list_head val_list;
1274 if (likely(val_buf->bo == NULL))
1277 INIT_LIST_HEAD(&val_list);
1278 list_add_tail(&val_buf->head, &val_list);
1279 ttm_eu_backoff_reservation(NULL, &val_list);
1280 ttm_bo_unref(&val_buf->bo);
1284 * vmw_resource_do_evict - Evict a resource, and transfer its data
1285 * to a backup buffer.
1287 * @res: The resource to evict.
1288 * @interruptible: Whether to wait interruptible.
1290 int vmw_resource_do_evict(struct vmw_resource *res, bool interruptible)
1292 struct ttm_validate_buffer val_buf;
1293 const struct vmw_res_func *func = res->func;
1296 BUG_ON(!func->may_evict);
1299 ret = vmw_resource_check_buffer(res, interruptible, &val_buf);
1300 if (unlikely(ret != 0))
1303 if (unlikely(func->unbind != NULL &&
1304 (!func->needs_backup || !list_empty(&res->mob_head)))) {
1305 ret = func->unbind(res, res->res_dirty, &val_buf);
1306 if (unlikely(ret != 0))
1308 list_del_init(&res->mob_head);
1310 ret = func->destroy(res);
1311 res->backup_dirty = true;
1312 res->res_dirty = false;
1314 vmw_resource_backoff_reservation(&val_buf);
1321 * vmw_resource_validate - Make a resource up-to-date and visible
1324 * @res: The resource to make visible to the device.
1326 * On succesful return, any backup DMA buffer pointed to by @res->backup will
1327 * be reserved and validated.
1328 * On hardware resource shortage, this function will repeatedly evict
1329 * resources of the same type until the validation succeeds.
1331 int vmw_resource_validate(struct vmw_resource *res)
1334 struct vmw_resource *evict_res;
1335 struct vmw_private *dev_priv = res->dev_priv;
1336 struct list_head *lru_list = &dev_priv->res_lru[res->func->res_type];
1337 struct ttm_validate_buffer val_buf;
1338 unsigned err_count = 0;
1340 if (likely(!res->func->may_evict))
1345 val_buf.bo = &res->backup->base;
1347 ret = vmw_resource_do_validate(res, &val_buf);
1348 if (likely(ret != -EBUSY))
1351 write_lock(&dev_priv->resource_lock);
1352 if (list_empty(lru_list) || !res->func->may_evict) {
1353 DRM_ERROR("Out of device device resources "
1354 "for %s.\n", res->func->type_name);
1356 write_unlock(&dev_priv->resource_lock);
1360 evict_res = vmw_resource_reference
1361 (list_first_entry(lru_list, struct vmw_resource,
1363 list_del_init(&evict_res->lru_head);
1365 write_unlock(&dev_priv->resource_lock);
1367 ret = vmw_resource_do_evict(evict_res, true);
1368 if (unlikely(ret != 0)) {
1369 write_lock(&dev_priv->resource_lock);
1370 list_add_tail(&evict_res->lru_head, lru_list);
1371 write_unlock(&dev_priv->resource_lock);
1372 if (ret == -ERESTARTSYS ||
1373 ++err_count > VMW_RES_EVICT_ERR_COUNT) {
1374 vmw_resource_unreference(&evict_res);
1375 goto out_no_validate;
1379 vmw_resource_unreference(&evict_res);
1382 if (unlikely(ret != 0))
1383 goto out_no_validate;
1384 else if (!res->func->needs_backup && res->backup) {
1385 list_del_init(&res->mob_head);
1386 vmw_dmabuf_unreference(&res->backup);
1396 * vmw_fence_single_bo - Utility function to fence a single TTM buffer
1397 * object without unreserving it.
1399 * @bo: Pointer to the struct ttm_buffer_object to fence.
1400 * @fence: Pointer to the fence. If NULL, this function will
1401 * insert a fence into the command stream..
1403 * Contrary to the ttm_eu version of this function, it takes only
1404 * a single buffer object instead of a list, and it also doesn't
1405 * unreserve the buffer object, which needs to be done separately.
1407 void vmw_fence_single_bo(struct ttm_buffer_object *bo,
1408 struct vmw_fence_obj *fence)
1410 struct ttm_bo_device *bdev = bo->bdev;
1411 struct ttm_bo_driver *driver = bdev->driver;
1412 struct vmw_fence_obj *old_fence_obj;
1413 struct vmw_private *dev_priv =
1414 container_of(bdev, struct vmw_private, bdev);
1417 vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
1419 driver->sync_obj_ref(fence);
1421 spin_lock(&bdev->fence_lock);
1423 old_fence_obj = bo->sync_obj;
1424 bo->sync_obj = fence;
1426 spin_unlock(&bdev->fence_lock);
1429 vmw_fence_obj_unreference(&old_fence_obj);
1433 * vmw_resource_move_notify - TTM move_notify_callback
1435 * @bo: The TTM buffer object about to move.
1436 * @mem: The truct ttm_mem_reg indicating to what memory
1437 * region the move is taking place.
1439 * Evicts the Guest Backed hardware resource if the backup
1440 * buffer is being moved out of MOB memory.
1441 * Note that this function should not race with the resource
1442 * validation code as long as it accesses only members of struct
1443 * resource that remain static while bo::res is !NULL and
1444 * while we have @bo reserved. struct resource::backup is *not* a
1445 * static member. The resource validation code will take care
1446 * to set @bo::res to NULL, while having @bo reserved when the
1447 * buffer is no longer bound to the resource, so @bo:res can be
1448 * used to determine whether there is a need to unbind and whether
1449 * it is safe to unbind.
1451 void vmw_resource_move_notify(struct ttm_buffer_object *bo,
1452 struct ttm_mem_reg *mem)
1454 struct vmw_dma_buffer *dma_buf;
1459 if (bo->destroy != vmw_dmabuf_bo_free &&
1460 bo->destroy != vmw_user_dmabuf_destroy)
1463 dma_buf = container_of(bo, struct vmw_dma_buffer, base);
1465 if (mem->mem_type != VMW_PL_MOB) {
1466 struct vmw_resource *res, *n;
1467 struct ttm_bo_device *bdev = bo->bdev;
1468 struct ttm_validate_buffer val_buf;
1472 list_for_each_entry_safe(res, n, &dma_buf->res_list, mob_head) {
1474 if (unlikely(res->func->unbind == NULL))
1477 (void) res->func->unbind(res, true, &val_buf);
1478 res->backup_dirty = true;
1479 res->res_dirty = false;
1480 list_del_init(&res->mob_head);
1483 spin_lock(&bdev->fence_lock);
1484 (void) ttm_bo_wait(bo, false, false, false);
1485 spin_unlock(&bdev->fence_lock);
1490 * vmw_resource_needs_backup - Return whether a resource needs a backup buffer.
1492 * @res: The resource being queried.
1494 bool vmw_resource_needs_backup(const struct vmw_resource *res)
1496 return res->func->needs_backup;
1500 * vmw_resource_evict_type - Evict all resources of a specific type
1502 * @dev_priv: Pointer to a device private struct
1503 * @type: The resource type to evict
1505 * To avoid thrashing starvation or as part of the hibernation sequence,
1506 * try to evict all evictable resources of a specific type.
1508 static void vmw_resource_evict_type(struct vmw_private *dev_priv,
1509 enum vmw_res_type type)
1511 struct list_head *lru_list = &dev_priv->res_lru[type];
1512 struct vmw_resource *evict_res;
1513 unsigned err_count = 0;
1517 write_lock(&dev_priv->resource_lock);
1519 if (list_empty(lru_list))
1522 evict_res = vmw_resource_reference(
1523 list_first_entry(lru_list, struct vmw_resource,
1525 list_del_init(&evict_res->lru_head);
1526 write_unlock(&dev_priv->resource_lock);
1528 ret = vmw_resource_do_evict(evict_res, false);
1529 if (unlikely(ret != 0)) {
1530 write_lock(&dev_priv->resource_lock);
1531 list_add_tail(&evict_res->lru_head, lru_list);
1532 write_unlock(&dev_priv->resource_lock);
1533 if (++err_count > VMW_RES_EVICT_ERR_COUNT) {
1534 vmw_resource_unreference(&evict_res);
1539 vmw_resource_unreference(&evict_res);
1543 write_unlock(&dev_priv->resource_lock);
1547 * vmw_resource_evict_all - Evict all evictable resources
1549 * @dev_priv: Pointer to a device private struct
1551 * To avoid thrashing starvation or as part of the hibernation sequence,
1552 * evict all evictable resources. In particular this means that all
1553 * guest-backed resources that are registered with the device are
1554 * evicted and the OTable becomes clean.
1556 void vmw_resource_evict_all(struct vmw_private *dev_priv)
1558 enum vmw_res_type type;
1560 mutex_lock(&dev_priv->cmdbuf_mutex);
1562 for (type = 0; type < vmw_res_max; ++type)
1563 vmw_resource_evict_type(dev_priv, type);
1565 mutex_unlock(&dev_priv->cmdbuf_mutex);