88b6f921ee947fbe0ce2c2007f592e1acbcbaadd
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / gpu / drm / vmwgfx / vmwgfx_resource.c
1 /**************************************************************************
2  *
3  * Copyright © 2009 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 <drm/vmwgfx_drm.h>
30 #include <drm/ttm/ttm_object.h>
31 #include <drm/ttm/ttm_placement.h>
32 #include <drm/drmP.h>
33 #include "vmwgfx_resource_priv.h"
34
35 struct vmw_user_dma_buffer {
36         struct ttm_base_object base;
37         struct vmw_dma_buffer dma;
38 };
39
40 struct vmw_bo_user_rep {
41         uint32_t handle;
42         uint64_t map_handle;
43 };
44
45 struct vmw_stream {
46         struct vmw_resource res;
47         uint32_t stream_id;
48 };
49
50 struct vmw_user_stream {
51         struct ttm_base_object base;
52         struct vmw_stream stream;
53 };
54
55
56 static uint64_t vmw_user_stream_size;
57
58 static const struct vmw_res_func vmw_stream_func = {
59         .res_type = vmw_res_stream,
60         .needs_backup = false,
61         .may_evict = false,
62         .type_name = "video streams",
63         .backup_placement = NULL,
64         .create = NULL,
65         .destroy = NULL,
66         .bind = NULL,
67         .unbind = NULL
68 };
69
70 static inline struct vmw_dma_buffer *
71 vmw_dma_buffer(struct ttm_buffer_object *bo)
72 {
73         return container_of(bo, struct vmw_dma_buffer, base);
74 }
75
76 static inline struct vmw_user_dma_buffer *
77 vmw_user_dma_buffer(struct ttm_buffer_object *bo)
78 {
79         struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
80         return container_of(vmw_bo, struct vmw_user_dma_buffer, dma);
81 }
82
83 struct vmw_resource *vmw_resource_reference(struct vmw_resource *res)
84 {
85         kref_get(&res->kref);
86         return res;
87 }
88
89
90 /**
91  * vmw_resource_release_id - release a resource id to the id manager.
92  *
93  * @res: Pointer to the resource.
94  *
95  * Release the resource id to the resource id manager and set it to -1
96  */
97 void vmw_resource_release_id(struct vmw_resource *res)
98 {
99         struct vmw_private *dev_priv = res->dev_priv;
100         struct idr *idr = &dev_priv->res_idr[res->func->res_type];
101
102         write_lock(&dev_priv->resource_lock);
103         if (res->id != -1)
104                 idr_remove(idr, res->id);
105         res->id = -1;
106         write_unlock(&dev_priv->resource_lock);
107 }
108
109 static void vmw_resource_release(struct kref *kref)
110 {
111         struct vmw_resource *res =
112             container_of(kref, struct vmw_resource, kref);
113         struct vmw_private *dev_priv = res->dev_priv;
114         int id;
115         struct idr *idr = &dev_priv->res_idr[res->func->res_type];
116
117         res->avail = false;
118         list_del_init(&res->lru_head);
119         write_unlock(&dev_priv->resource_lock);
120         if (res->backup) {
121                 struct ttm_buffer_object *bo = &res->backup->base;
122
123                 ttm_bo_reserve(bo, false, false, false, 0);
124                 if (!list_empty(&res->mob_head) &&
125                     res->func->unbind != NULL) {
126                         struct ttm_validate_buffer val_buf;
127
128                         val_buf.bo = bo;
129                         res->func->unbind(res, false, &val_buf);
130                 }
131                 res->backup_dirty = false;
132                 list_del_init(&res->mob_head);
133                 ttm_bo_unreserve(bo);
134                 vmw_dmabuf_unreference(&res->backup);
135         }
136
137         if (likely(res->hw_destroy != NULL))
138                 res->hw_destroy(res);
139
140         id = res->id;
141         if (res->res_free != NULL)
142                 res->res_free(res);
143         else
144                 kfree(res);
145
146         write_lock(&dev_priv->resource_lock);
147
148         if (id != -1)
149                 idr_remove(idr, id);
150 }
151
152 void vmw_resource_unreference(struct vmw_resource **p_res)
153 {
154         struct vmw_resource *res = *p_res;
155         struct vmw_private *dev_priv = res->dev_priv;
156
157         *p_res = NULL;
158         write_lock(&dev_priv->resource_lock);
159         kref_put(&res->kref, vmw_resource_release);
160         write_unlock(&dev_priv->resource_lock);
161 }
162
163
164 /**
165  * vmw_resource_alloc_id - release a resource id to the id manager.
166  *
167  * @res: Pointer to the resource.
168  *
169  * Allocate the lowest free resource from the resource manager, and set
170  * @res->id to that id. Returns 0 on success and -ENOMEM on failure.
171  */
172 int vmw_resource_alloc_id(struct vmw_resource *res)
173 {
174         struct vmw_private *dev_priv = res->dev_priv;
175         int ret;
176         struct idr *idr = &dev_priv->res_idr[res->func->res_type];
177
178         BUG_ON(res->id != -1);
179
180         do {
181                 if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
182                         return -ENOMEM;
183
184                 write_lock(&dev_priv->resource_lock);
185                 ret = idr_get_new_above(idr, res, 1, &res->id);
186                 write_unlock(&dev_priv->resource_lock);
187
188         } while (ret == -EAGAIN);
189
190         return ret;
191 }
192
193 /**
194  * vmw_resource_init - initialize a struct vmw_resource
195  *
196  * @dev_priv:       Pointer to a device private struct.
197  * @res:            The struct vmw_resource to initialize.
198  * @obj_type:       Resource object type.
199  * @delay_id:       Boolean whether to defer device id allocation until
200  *                  the first validation.
201  * @res_free:       Resource destructor.
202  * @func:           Resource function table.
203  */
204 int vmw_resource_init(struct vmw_private *dev_priv, struct vmw_resource *res,
205                       bool delay_id,
206                       void (*res_free) (struct vmw_resource *res),
207                       const struct vmw_res_func *func)
208 {
209         kref_init(&res->kref);
210         res->hw_destroy = NULL;
211         res->res_free = res_free;
212         res->avail = false;
213         res->dev_priv = dev_priv;
214         res->func = func;
215         INIT_LIST_HEAD(&res->lru_head);
216         INIT_LIST_HEAD(&res->mob_head);
217         res->id = -1;
218         res->backup = NULL;
219         res->backup_offset = 0;
220         res->backup_dirty = false;
221         res->res_dirty = false;
222         if (delay_id)
223                 return 0;
224         else
225                 return vmw_resource_alloc_id(res);
226 }
227
228 /**
229  * vmw_resource_activate
230  *
231  * @res:        Pointer to the newly created resource
232  * @hw_destroy: Destroy function. NULL if none.
233  *
234  * Activate a resource after the hardware has been made aware of it.
235  * Set tye destroy function to @destroy. Typically this frees the
236  * resource and destroys the hardware resources associated with it.
237  * Activate basically means that the function vmw_resource_lookup will
238  * find it.
239  */
240 void vmw_resource_activate(struct vmw_resource *res,
241                            void (*hw_destroy) (struct vmw_resource *))
242 {
243         struct vmw_private *dev_priv = res->dev_priv;
244
245         write_lock(&dev_priv->resource_lock);
246         res->avail = true;
247         res->hw_destroy = hw_destroy;
248         write_unlock(&dev_priv->resource_lock);
249 }
250
251 struct vmw_resource *vmw_resource_lookup(struct vmw_private *dev_priv,
252                                          struct idr *idr, int id)
253 {
254         struct vmw_resource *res;
255
256         read_lock(&dev_priv->resource_lock);
257         res = idr_find(idr, id);
258         if (res && res->avail)
259                 kref_get(&res->kref);
260         else
261                 res = NULL;
262         read_unlock(&dev_priv->resource_lock);
263
264         if (unlikely(res == NULL))
265                 return NULL;
266
267         return res;
268 }
269
270 /**
271  * vmw_user_resource_lookup_handle - lookup a struct resource from a
272  * TTM user-space handle and perform basic type checks
273  *
274  * @dev_priv:     Pointer to a device private struct
275  * @tfile:        Pointer to a struct ttm_object_file identifying the caller
276  * @handle:       The TTM user-space handle
277  * @converter:    Pointer to an object describing the resource type
278  * @p_res:        On successful return the location pointed to will contain
279  *                a pointer to a refcounted struct vmw_resource.
280  *
281  * If the handle can't be found or is associated with an incorrect resource
282  * type, -EINVAL will be returned.
283  */
284 int vmw_user_resource_lookup_handle(struct vmw_private *dev_priv,
285                                     struct ttm_object_file *tfile,
286                                     uint32_t handle,
287                                     const struct vmw_user_resource_conv
288                                     *converter,
289                                     struct vmw_resource **p_res)
290 {
291         struct ttm_base_object *base;
292         struct vmw_resource *res;
293         int ret = -EINVAL;
294
295         base = ttm_base_object_lookup(tfile, handle);
296         if (unlikely(base == NULL))
297                 return -EINVAL;
298
299         if (unlikely(base->object_type != converter->object_type))
300                 goto out_bad_resource;
301
302         res = converter->base_obj_to_res(base);
303
304         read_lock(&dev_priv->resource_lock);
305         if (!res->avail || res->res_free != converter->res_free) {
306                 read_unlock(&dev_priv->resource_lock);
307                 goto out_bad_resource;
308         }
309
310         kref_get(&res->kref);
311         read_unlock(&dev_priv->resource_lock);
312
313         *p_res = res;
314         ret = 0;
315
316 out_bad_resource:
317         ttm_base_object_unref(&base);
318
319         return ret;
320 }
321
322 /**
323  * Helper function that looks either a surface or dmabuf.
324  *
325  * The pointer this pointed at by out_surf and out_buf needs to be null.
326  */
327 int vmw_user_lookup_handle(struct vmw_private *dev_priv,
328                            struct ttm_object_file *tfile,
329                            uint32_t handle,
330                            struct vmw_surface **out_surf,
331                            struct vmw_dma_buffer **out_buf)
332 {
333         struct vmw_resource *res;
334         int ret;
335
336         BUG_ON(*out_surf || *out_buf);
337
338         ret = vmw_user_resource_lookup_handle(dev_priv, tfile, handle,
339                                               user_surface_converter,
340                                               &res);
341         if (!ret) {
342                 *out_surf = vmw_res_to_srf(res);
343                 return 0;
344         }
345
346         *out_surf = NULL;
347         ret = vmw_user_dmabuf_lookup(tfile, handle, out_buf);
348         return ret;
349 }
350
351 /**
352  * Buffer management.
353  */
354 void vmw_dmabuf_bo_free(struct ttm_buffer_object *bo)
355 {
356         struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
357
358         kfree(vmw_bo);
359 }
360
361 int vmw_dmabuf_init(struct vmw_private *dev_priv,
362                     struct vmw_dma_buffer *vmw_bo,
363                     size_t size, struct ttm_placement *placement,
364                     bool interruptible,
365                     void (*bo_free) (struct ttm_buffer_object *bo))
366 {
367         struct ttm_bo_device *bdev = &dev_priv->bdev;
368         size_t acc_size;
369         int ret;
370
371         BUG_ON(!bo_free);
372
373         acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct vmw_dma_buffer));
374         memset(vmw_bo, 0, sizeof(*vmw_bo));
375
376         INIT_LIST_HEAD(&vmw_bo->res_list);
377
378         ret = ttm_bo_init(bdev, &vmw_bo->base, size,
379                           ttm_bo_type_device, placement,
380                           0, interruptible,
381                           NULL, acc_size, NULL, bo_free);
382         return ret;
383 }
384
385 static void vmw_user_dmabuf_destroy(struct ttm_buffer_object *bo)
386 {
387         struct vmw_user_dma_buffer *vmw_user_bo = vmw_user_dma_buffer(bo);
388
389         ttm_base_object_kfree(vmw_user_bo, base);
390 }
391
392 static void vmw_user_dmabuf_release(struct ttm_base_object **p_base)
393 {
394         struct vmw_user_dma_buffer *vmw_user_bo;
395         struct ttm_base_object *base = *p_base;
396         struct ttm_buffer_object *bo;
397
398         *p_base = NULL;
399
400         if (unlikely(base == NULL))
401                 return;
402
403         vmw_user_bo = container_of(base, struct vmw_user_dma_buffer, base);
404         bo = &vmw_user_bo->dma.base;
405         ttm_bo_unref(&bo);
406 }
407
408 /**
409  * vmw_user_dmabuf_alloc - Allocate a user dma buffer
410  *
411  * @dev_priv: Pointer to a struct device private.
412  * @tfile: Pointer to a struct ttm_object_file on which to register the user
413  * object.
414  * @size: Size of the dma buffer.
415  * @shareable: Boolean whether the buffer is shareable with other open files.
416  * @handle: Pointer to where the handle value should be assigned.
417  * @p_dma_buf: Pointer to where the refcounted struct vmw_dma_buffer pointer
418  * should be assigned.
419  */
420 int vmw_user_dmabuf_alloc(struct vmw_private *dev_priv,
421                           struct ttm_object_file *tfile,
422                           uint32_t size,
423                           bool shareable,
424                           uint32_t *handle,
425                           struct vmw_dma_buffer **p_dma_buf)
426 {
427         struct vmw_user_dma_buffer *user_bo;
428         struct ttm_buffer_object *tmp;
429         int ret;
430
431         user_bo = kzalloc(sizeof(*user_bo), GFP_KERNEL);
432         if (unlikely(user_bo == NULL)) {
433                 DRM_ERROR("Failed to allocate a buffer.\n");
434                 return -ENOMEM;
435         }
436
437         ret = vmw_dmabuf_init(dev_priv, &user_bo->dma, size,
438                               &vmw_vram_sys_placement, true,
439                               &vmw_user_dmabuf_destroy);
440         if (unlikely(ret != 0))
441                 return ret;
442
443         tmp = ttm_bo_reference(&user_bo->dma.base);
444         ret = ttm_base_object_init(tfile,
445                                    &user_bo->base,
446                                    shareable,
447                                    ttm_buffer_type,
448                                    &vmw_user_dmabuf_release, NULL);
449         if (unlikely(ret != 0)) {
450                 ttm_bo_unref(&tmp);
451                 goto out_no_base_object;
452         }
453
454         *p_dma_buf = &user_bo->dma;
455         *handle = user_bo->base.hash.key;
456
457 out_no_base_object:
458         return ret;
459 }
460
461 int vmw_dmabuf_alloc_ioctl(struct drm_device *dev, void *data,
462                            struct drm_file *file_priv)
463 {
464         struct vmw_private *dev_priv = vmw_priv(dev);
465         union drm_vmw_alloc_dmabuf_arg *arg =
466             (union drm_vmw_alloc_dmabuf_arg *)data;
467         struct drm_vmw_alloc_dmabuf_req *req = &arg->req;
468         struct drm_vmw_dmabuf_rep *rep = &arg->rep;
469         struct vmw_dma_buffer *dma_buf;
470         uint32_t handle;
471         struct vmw_master *vmaster = vmw_master(file_priv->master);
472         int ret;
473
474         ret = ttm_read_lock(&vmaster->lock, true);
475         if (unlikely(ret != 0))
476                 return ret;
477
478         ret = vmw_user_dmabuf_alloc(dev_priv, vmw_fpriv(file_priv)->tfile,
479                                     req->size, false, &handle, &dma_buf);
480         if (unlikely(ret != 0))
481                 goto out_no_dmabuf;
482
483         rep->handle = handle;
484         rep->map_handle = dma_buf->base.addr_space_offset;
485         rep->cur_gmr_id = handle;
486         rep->cur_gmr_offset = 0;
487
488         vmw_dmabuf_unreference(&dma_buf);
489
490 out_no_dmabuf:
491         ttm_read_unlock(&vmaster->lock);
492
493         return ret;
494 }
495
496 int vmw_dmabuf_unref_ioctl(struct drm_device *dev, void *data,
497                            struct drm_file *file_priv)
498 {
499         struct drm_vmw_unref_dmabuf_arg *arg =
500             (struct drm_vmw_unref_dmabuf_arg *)data;
501
502         return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
503                                          arg->handle,
504                                          TTM_REF_USAGE);
505 }
506
507 int vmw_user_dmabuf_lookup(struct ttm_object_file *tfile,
508                            uint32_t handle, struct vmw_dma_buffer **out)
509 {
510         struct vmw_user_dma_buffer *vmw_user_bo;
511         struct ttm_base_object *base;
512
513         base = ttm_base_object_lookup(tfile, handle);
514         if (unlikely(base == NULL)) {
515                 printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
516                        (unsigned long)handle);
517                 return -ESRCH;
518         }
519
520         if (unlikely(base->object_type != ttm_buffer_type)) {
521                 ttm_base_object_unref(&base);
522                 printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
523                        (unsigned long)handle);
524                 return -EINVAL;
525         }
526
527         vmw_user_bo = container_of(base, struct vmw_user_dma_buffer, base);
528         (void)ttm_bo_reference(&vmw_user_bo->dma.base);
529         ttm_base_object_unref(&base);
530         *out = &vmw_user_bo->dma;
531
532         return 0;
533 }
534
535 int vmw_user_dmabuf_reference(struct ttm_object_file *tfile,
536                               struct vmw_dma_buffer *dma_buf)
537 {
538         struct vmw_user_dma_buffer *user_bo;
539
540         if (dma_buf->base.destroy != vmw_user_dmabuf_destroy)
541                 return -EINVAL;
542
543         user_bo = container_of(dma_buf, struct vmw_user_dma_buffer, dma);
544         return ttm_ref_object_add(tfile, &user_bo->base, TTM_REF_USAGE, NULL);
545 }
546
547 /*
548  * Stream management
549  */
550
551 static void vmw_stream_destroy(struct vmw_resource *res)
552 {
553         struct vmw_private *dev_priv = res->dev_priv;
554         struct vmw_stream *stream;
555         int ret;
556
557         DRM_INFO("%s: unref\n", __func__);
558         stream = container_of(res, struct vmw_stream, res);
559
560         ret = vmw_overlay_unref(dev_priv, stream->stream_id);
561         WARN_ON(ret != 0);
562 }
563
564 static int vmw_stream_init(struct vmw_private *dev_priv,
565                            struct vmw_stream *stream,
566                            void (*res_free) (struct vmw_resource *res))
567 {
568         struct vmw_resource *res = &stream->res;
569         int ret;
570
571         ret = vmw_resource_init(dev_priv, res, false, res_free,
572                                 &vmw_stream_func);
573
574         if (unlikely(ret != 0)) {
575                 if (res_free == NULL)
576                         kfree(stream);
577                 else
578                         res_free(&stream->res);
579                 return ret;
580         }
581
582         ret = vmw_overlay_claim(dev_priv, &stream->stream_id);
583         if (ret) {
584                 vmw_resource_unreference(&res);
585                 return ret;
586         }
587
588         DRM_INFO("%s: claimed\n", __func__);
589
590         vmw_resource_activate(&stream->res, vmw_stream_destroy);
591         return 0;
592 }
593
594 static void vmw_user_stream_free(struct vmw_resource *res)
595 {
596         struct vmw_user_stream *stream =
597             container_of(res, struct vmw_user_stream, stream.res);
598         struct vmw_private *dev_priv = res->dev_priv;
599
600         ttm_base_object_kfree(stream, base);
601         ttm_mem_global_free(vmw_mem_glob(dev_priv),
602                             vmw_user_stream_size);
603 }
604
605 /**
606  * This function is called when user space has no more references on the
607  * base object. It releases the base-object's reference on the resource object.
608  */
609
610 static void vmw_user_stream_base_release(struct ttm_base_object **p_base)
611 {
612         struct ttm_base_object *base = *p_base;
613         struct vmw_user_stream *stream =
614             container_of(base, struct vmw_user_stream, base);
615         struct vmw_resource *res = &stream->stream.res;
616
617         *p_base = NULL;
618         vmw_resource_unreference(&res);
619 }
620
621 int vmw_stream_unref_ioctl(struct drm_device *dev, void *data,
622                            struct drm_file *file_priv)
623 {
624         struct vmw_private *dev_priv = vmw_priv(dev);
625         struct vmw_resource *res;
626         struct vmw_user_stream *stream;
627         struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
628         struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
629         struct idr *idr = &dev_priv->res_idr[vmw_res_stream];
630         int ret = 0;
631
632
633         res = vmw_resource_lookup(dev_priv, idr, arg->stream_id);
634         if (unlikely(res == NULL))
635                 return -EINVAL;
636
637         if (res->res_free != &vmw_user_stream_free) {
638                 ret = -EINVAL;
639                 goto out;
640         }
641
642         stream = container_of(res, struct vmw_user_stream, stream.res);
643         if (stream->base.tfile != tfile) {
644                 ret = -EINVAL;
645                 goto out;
646         }
647
648         ttm_ref_object_base_unref(tfile, stream->base.hash.key, TTM_REF_USAGE);
649 out:
650         vmw_resource_unreference(&res);
651         return ret;
652 }
653
654 int vmw_stream_claim_ioctl(struct drm_device *dev, void *data,
655                            struct drm_file *file_priv)
656 {
657         struct vmw_private *dev_priv = vmw_priv(dev);
658         struct vmw_user_stream *stream;
659         struct vmw_resource *res;
660         struct vmw_resource *tmp;
661         struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
662         struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
663         struct vmw_master *vmaster = vmw_master(file_priv->master);
664         int ret;
665
666         /*
667          * Approximate idr memory usage with 128 bytes. It will be limited
668          * by maximum number_of streams anyway?
669          */
670
671         if (unlikely(vmw_user_stream_size == 0))
672                 vmw_user_stream_size = ttm_round_pot(sizeof(*stream)) + 128;
673
674         ret = ttm_read_lock(&vmaster->lock, true);
675         if (unlikely(ret != 0))
676                 return ret;
677
678         ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
679                                    vmw_user_stream_size,
680                                    false, true);
681         if (unlikely(ret != 0)) {
682                 if (ret != -ERESTARTSYS)
683                         DRM_ERROR("Out of graphics memory for stream"
684                                   " creation.\n");
685                 goto out_unlock;
686         }
687
688
689         stream = kmalloc(sizeof(*stream), GFP_KERNEL);
690         if (unlikely(stream == NULL)) {
691                 ttm_mem_global_free(vmw_mem_glob(dev_priv),
692                                     vmw_user_stream_size);
693                 ret = -ENOMEM;
694                 goto out_unlock;
695         }
696
697         res = &stream->stream.res;
698         stream->base.shareable = false;
699         stream->base.tfile = NULL;
700
701         /*
702          * From here on, the destructor takes over resource freeing.
703          */
704
705         ret = vmw_stream_init(dev_priv, &stream->stream, vmw_user_stream_free);
706         if (unlikely(ret != 0))
707                 goto out_unlock;
708
709         tmp = vmw_resource_reference(res);
710         ret = ttm_base_object_init(tfile, &stream->base, false, VMW_RES_STREAM,
711                                    &vmw_user_stream_base_release, NULL);
712
713         if (unlikely(ret != 0)) {
714                 vmw_resource_unreference(&tmp);
715                 goto out_err;
716         }
717
718         arg->stream_id = res->id;
719 out_err:
720         vmw_resource_unreference(&res);
721 out_unlock:
722         ttm_read_unlock(&vmaster->lock);
723         return ret;
724 }
725
726 int vmw_user_stream_lookup(struct vmw_private *dev_priv,
727                            struct ttm_object_file *tfile,
728                            uint32_t *inout_id, struct vmw_resource **out)
729 {
730         struct vmw_user_stream *stream;
731         struct vmw_resource *res;
732         int ret;
733
734         res = vmw_resource_lookup(dev_priv, &dev_priv->res_idr[vmw_res_stream],
735                                   *inout_id);
736         if (unlikely(res == NULL))
737                 return -EINVAL;
738
739         if (res->res_free != &vmw_user_stream_free) {
740                 ret = -EINVAL;
741                 goto err_ref;
742         }
743
744         stream = container_of(res, struct vmw_user_stream, stream.res);
745         if (stream->base.tfile != tfile) {
746                 ret = -EPERM;
747                 goto err_ref;
748         }
749
750         *inout_id = stream->stream.stream_id;
751         *out = res;
752         return 0;
753 err_ref:
754         vmw_resource_unreference(&res);
755         return ret;
756 }
757
758
759 int vmw_dumb_create(struct drm_file *file_priv,
760                     struct drm_device *dev,
761                     struct drm_mode_create_dumb *args)
762 {
763         struct vmw_private *dev_priv = vmw_priv(dev);
764         struct vmw_master *vmaster = vmw_master(file_priv->master);
765         struct vmw_user_dma_buffer *vmw_user_bo;
766         struct ttm_buffer_object *tmp;
767         int ret;
768
769         args->pitch = args->width * ((args->bpp + 7) / 8);
770         args->size = args->pitch * args->height;
771
772         vmw_user_bo = kzalloc(sizeof(*vmw_user_bo), GFP_KERNEL);
773         if (vmw_user_bo == NULL)
774                 return -ENOMEM;
775
776         ret = ttm_read_lock(&vmaster->lock, true);
777         if (ret != 0) {
778                 kfree(vmw_user_bo);
779                 return ret;
780         }
781
782         ret = vmw_dmabuf_init(dev_priv, &vmw_user_bo->dma, args->size,
783                               &vmw_vram_sys_placement, true,
784                               &vmw_user_dmabuf_destroy);
785         if (ret != 0)
786                 goto out_no_dmabuf;
787
788         tmp = ttm_bo_reference(&vmw_user_bo->dma.base);
789         ret = ttm_base_object_init(vmw_fpriv(file_priv)->tfile,
790                                    &vmw_user_bo->base,
791                                    false,
792                                    ttm_buffer_type,
793                                    &vmw_user_dmabuf_release, NULL);
794         if (unlikely(ret != 0))
795                 goto out_no_base_object;
796
797         args->handle = vmw_user_bo->base.hash.key;
798
799 out_no_base_object:
800         ttm_bo_unref(&tmp);
801 out_no_dmabuf:
802         ttm_read_unlock(&vmaster->lock);
803         return ret;
804 }
805
806 int vmw_dumb_map_offset(struct drm_file *file_priv,
807                         struct drm_device *dev, uint32_t handle,
808                         uint64_t *offset)
809 {
810         struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
811         struct vmw_dma_buffer *out_buf;
812         int ret;
813
814         ret = vmw_user_dmabuf_lookup(tfile, handle, &out_buf);
815         if (ret != 0)
816                 return -EINVAL;
817
818         *offset = out_buf->base.addr_space_offset;
819         vmw_dmabuf_unreference(&out_buf);
820         return 0;
821 }
822
823 int vmw_dumb_destroy(struct drm_file *file_priv,
824                      struct drm_device *dev,
825                      uint32_t handle)
826 {
827         return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
828                                          handle, TTM_REF_USAGE);
829 }
830
831 /**
832  * vmw_resource_buf_alloc - Allocate a backup buffer for a resource.
833  *
834  * @res:            The resource for which to allocate a backup buffer.
835  * @interruptible:  Whether any sleeps during allocation should be
836  *                  performed while interruptible.
837  */
838 static int vmw_resource_buf_alloc(struct vmw_resource *res,
839                                   bool interruptible)
840 {
841         unsigned long size =
842                 (res->backup_size + PAGE_SIZE - 1) & PAGE_MASK;
843         struct vmw_dma_buffer *backup;
844         int ret;
845
846         if (likely(res->backup)) {
847                 BUG_ON(res->backup->base.num_pages * PAGE_SIZE < size);
848                 return 0;
849         }
850
851         backup = kzalloc(sizeof(*backup), GFP_KERNEL);
852         if (unlikely(backup == NULL))
853                 return -ENOMEM;
854
855         ret = vmw_dmabuf_init(res->dev_priv, backup, res->backup_size,
856                               res->func->backup_placement,
857                               interruptible,
858                               &vmw_dmabuf_bo_free);
859         if (unlikely(ret != 0))
860                 goto out_no_dmabuf;
861
862         res->backup = backup;
863
864 out_no_dmabuf:
865         return ret;
866 }
867
868 /**
869  * vmw_resource_do_validate - Make a resource up-to-date and visible
870  *                            to the device.
871  *
872  * @res:            The resource to make visible to the device.
873  * @val_buf:        Information about a buffer possibly
874  *                  containing backup data if a bind operation is needed.
875  *
876  * On hardware resource shortage, this function returns -EBUSY and
877  * should be retried once resources have been freed up.
878  */
879 static int vmw_resource_do_validate(struct vmw_resource *res,
880                                     struct ttm_validate_buffer *val_buf)
881 {
882         int ret = 0;
883         const struct vmw_res_func *func = res->func;
884
885         if (unlikely(res->id == -1)) {
886                 ret = func->create(res);
887                 if (unlikely(ret != 0))
888                         return ret;
889         }
890
891         if (func->bind &&
892             ((func->needs_backup && list_empty(&res->mob_head) &&
893               val_buf->bo != NULL) ||
894              (!func->needs_backup && val_buf->bo != NULL))) {
895                 ret = func->bind(res, val_buf);
896                 if (unlikely(ret != 0))
897                         goto out_bind_failed;
898                 if (func->needs_backup)
899                         list_add_tail(&res->mob_head, &res->backup->res_list);
900         }
901
902         /*
903          * Only do this on write operations, and move to
904          * vmw_resource_unreserve if it can be called after
905          * backup buffers have been unreserved. Otherwise
906          * sort out locking.
907          */
908         res->res_dirty = true;
909
910         return 0;
911
912 out_bind_failed:
913         func->destroy(res);
914
915         return ret;
916 }
917
918 /**
919  * vmw_resource_unreserve - Unreserve a resource previously reserved for
920  * command submission.
921  *
922  * @res:               Pointer to the struct vmw_resource to unreserve.
923  * @new_backup:        Pointer to new backup buffer if command submission
924  *                     switched.
925  * @new_backup_offset: New backup offset if @new_backup is !NULL.
926  *
927  * Currently unreserving a resource means putting it back on the device's
928  * resource lru list, so that it can be evicted if necessary.
929  */
930 void vmw_resource_unreserve(struct vmw_resource *res,
931                             struct vmw_dma_buffer *new_backup,
932                             unsigned long new_backup_offset)
933 {
934         struct vmw_private *dev_priv = res->dev_priv;
935
936         if (!list_empty(&res->lru_head))
937                 return;
938
939         if (new_backup && new_backup != res->backup) {
940
941                 if (res->backup) {
942                         BUG_ON(atomic_read(&res->backup->base.reserved) == 0);
943                         list_del_init(&res->mob_head);
944                         vmw_dmabuf_unreference(&res->backup);
945                 }
946
947                 res->backup = vmw_dmabuf_reference(new_backup);
948                 BUG_ON(atomic_read(&new_backup->base.reserved) == 0);
949                 list_add_tail(&res->mob_head, &new_backup->res_list);
950         }
951         if (new_backup)
952                 res->backup_offset = new_backup_offset;
953
954         if (!res->func->may_evict)
955                 return;
956
957         write_lock(&dev_priv->resource_lock);
958         list_add_tail(&res->lru_head,
959                       &res->dev_priv->res_lru[res->func->res_type]);
960         write_unlock(&dev_priv->resource_lock);
961 }
962
963 /**
964  * vmw_resource_check_buffer - Check whether a backup buffer is needed
965  *                             for a resource and in that case, allocate
966  *                             one, reserve and validate it.
967  *
968  * @res:            The resource for which to allocate a backup buffer.
969  * @interruptible:  Whether any sleeps during allocation should be
970  *                  performed while interruptible.
971  * @val_buf:        On successful return contains data about the
972  *                  reserved and validated backup buffer.
973  */
974 int vmw_resource_check_buffer(struct vmw_resource *res,
975                               bool interruptible,
976                               struct ttm_validate_buffer *val_buf)
977 {
978         struct list_head val_list;
979         bool backup_dirty = false;
980         int ret;
981
982         if (unlikely(res->backup == NULL)) {
983                 ret = vmw_resource_buf_alloc(res, interruptible);
984                 if (unlikely(ret != 0))
985                         return ret;
986         }
987
988         INIT_LIST_HEAD(&val_list);
989         val_buf->bo = ttm_bo_reference(&res->backup->base);
990         list_add_tail(&val_buf->head, &val_list);
991         ret = ttm_eu_reserve_buffers(&val_list);
992         if (unlikely(ret != 0))
993                 goto out_no_reserve;
994
995         if (res->func->needs_backup && list_empty(&res->mob_head))
996                 return 0;
997
998         backup_dirty = res->backup_dirty;
999         ret = ttm_bo_validate(&res->backup->base,
1000                               res->func->backup_placement,
1001                               true, false, false);
1002
1003         if (unlikely(ret != 0))
1004                 goto out_no_validate;
1005
1006         return 0;
1007
1008 out_no_validate:
1009         ttm_eu_backoff_reservation(&val_list);
1010 out_no_reserve:
1011         ttm_bo_unref(&val_buf->bo);
1012         if (backup_dirty)
1013                 vmw_dmabuf_unreference(&res->backup);
1014
1015         return ret;
1016 }
1017
1018 /**
1019  * vmw_resource_reserve - Reserve a resource for command submission
1020  *
1021  * @res:            The resource to reserve.
1022  *
1023  * This function takes the resource off the LRU list and make sure
1024  * a backup buffer is present for guest-backed resources. However,
1025  * the buffer may not be bound to the resource at this point.
1026  *
1027  */
1028 int vmw_resource_reserve(struct vmw_resource *res, bool no_backup)
1029 {
1030         struct vmw_private *dev_priv = res->dev_priv;
1031         int ret;
1032
1033         write_lock(&dev_priv->resource_lock);
1034         list_del_init(&res->lru_head);
1035         write_unlock(&dev_priv->resource_lock);
1036
1037         if (res->func->needs_backup && res->backup == NULL &&
1038             !no_backup) {
1039                 ret = vmw_resource_buf_alloc(res, true);
1040                 if (unlikely(ret != 0))
1041                         return ret;
1042         }
1043
1044         return 0;
1045 }
1046
1047 /**
1048  * vmw_resource_backoff_reservation - Unreserve and unreference a
1049  *                                    backup buffer
1050  *.
1051  * @val_buf:        Backup buffer information.
1052  */
1053 void vmw_resource_backoff_reservation(struct ttm_validate_buffer *val_buf)
1054 {
1055         struct list_head val_list;
1056
1057         if (likely(val_buf->bo == NULL))
1058                 return;
1059
1060         INIT_LIST_HEAD(&val_list);
1061         list_add_tail(&val_buf->head, &val_list);
1062         ttm_eu_backoff_reservation(&val_list);
1063         ttm_bo_unref(&val_buf->bo);
1064 }
1065
1066 /**
1067  * vmw_resource_do_evict - Evict a resource, and transfer its data
1068  *                         to a backup buffer.
1069  *
1070  * @res:            The resource to evict.
1071  */
1072 int vmw_resource_do_evict(struct vmw_resource *res)
1073 {
1074         struct ttm_validate_buffer val_buf;
1075         const struct vmw_res_func *func = res->func;
1076         int ret;
1077
1078         BUG_ON(!func->may_evict);
1079
1080         val_buf.bo = NULL;
1081         ret = vmw_resource_check_buffer(res, true, &val_buf);
1082         if (unlikely(ret != 0))
1083                 return ret;
1084
1085         if (unlikely(func->unbind != NULL &&
1086                      (!func->needs_backup || !list_empty(&res->mob_head)))) {
1087                 ret = func->unbind(res, res->res_dirty, &val_buf);
1088                 if (unlikely(ret != 0))
1089                         goto out_no_unbind;
1090                 list_del_init(&res->mob_head);
1091         }
1092         ret = func->destroy(res);
1093         res->backup_dirty = true;
1094         res->res_dirty = false;
1095 out_no_unbind:
1096         vmw_resource_backoff_reservation(&val_buf);
1097
1098         return ret;
1099 }
1100
1101
1102 /**
1103  * vmw_resource_validate - Make a resource up-to-date and visible
1104  *                         to the device.
1105  *
1106  * @res:            The resource to make visible to the device.
1107  *
1108  * On succesful return, any backup DMA buffer pointed to by @res->backup will
1109  * be reserved and validated.
1110  * On hardware resource shortage, this function will repeatedly evict
1111  * resources of the same type until the validation succeeds.
1112  */
1113 int vmw_resource_validate(struct vmw_resource *res)
1114 {
1115         int ret;
1116         struct vmw_resource *evict_res;
1117         struct vmw_private *dev_priv = res->dev_priv;
1118         struct list_head *lru_list = &dev_priv->res_lru[res->func->res_type];
1119         struct ttm_validate_buffer val_buf;
1120
1121         if (likely(!res->func->may_evict))
1122                 return 0;
1123
1124         val_buf.bo = NULL;
1125         if (res->backup)
1126                 val_buf.bo = &res->backup->base;
1127         do {
1128                 ret = vmw_resource_do_validate(res, &val_buf);
1129                 if (likely(ret != -EBUSY))
1130                         break;
1131
1132                 write_lock(&dev_priv->resource_lock);
1133                 if (list_empty(lru_list) || !res->func->may_evict) {
1134                         DRM_ERROR("Out of device device id entries "
1135                                   "for %s.\n", res->func->type_name);
1136                         ret = -EBUSY;
1137                         write_unlock(&dev_priv->resource_lock);
1138                         break;
1139                 }
1140
1141                 evict_res = vmw_resource_reference
1142                         (list_first_entry(lru_list, struct vmw_resource,
1143                                           lru_head));
1144                 list_del_init(&evict_res->lru_head);
1145
1146                 write_unlock(&dev_priv->resource_lock);
1147                 vmw_resource_do_evict(evict_res);
1148                 vmw_resource_unreference(&evict_res);
1149         } while (1);
1150
1151         if (unlikely(ret != 0))
1152                 goto out_no_validate;
1153         else if (!res->func->needs_backup && res->backup) {
1154                 list_del_init(&res->mob_head);
1155                 vmw_dmabuf_unreference(&res->backup);
1156         }
1157
1158         return 0;
1159
1160 out_no_validate:
1161         return ret;
1162 }
1163
1164 /**
1165  * vmw_fence_single_bo - Utility function to fence a single TTM buffer
1166  *                       object without unreserving it.
1167  *
1168  * @bo:             Pointer to the struct ttm_buffer_object to fence.
1169  * @fence:          Pointer to the fence. If NULL, this function will
1170  *                  insert a fence into the command stream..
1171  *
1172  * Contrary to the ttm_eu version of this function, it takes only
1173  * a single buffer object instead of a list, and it also doesn't
1174  * unreserve the buffer object, which needs to be done separately.
1175  */
1176 void vmw_fence_single_bo(struct ttm_buffer_object *bo,
1177                          struct vmw_fence_obj *fence)
1178 {
1179         struct ttm_bo_device *bdev = bo->bdev;
1180         struct ttm_bo_driver *driver = bdev->driver;
1181         struct vmw_fence_obj *old_fence_obj;
1182         struct vmw_private *dev_priv =
1183                 container_of(bdev, struct vmw_private, bdev);
1184
1185         if (fence == NULL)
1186                 vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
1187         else
1188                 driver->sync_obj_ref(fence);
1189
1190         spin_lock(&bdev->fence_lock);
1191
1192         old_fence_obj = bo->sync_obj;
1193         bo->sync_obj = fence;
1194
1195         spin_unlock(&bdev->fence_lock);
1196
1197         if (old_fence_obj)
1198                 vmw_fence_obj_unreference(&old_fence_obj);
1199 }
1200
1201 /**
1202  * vmw_resource_move_notify - TTM move_notify_callback
1203  *
1204  * @bo:             The TTM buffer object about to move.
1205  * @mem:            The truct ttm_mem_reg indicating to what memory
1206  *                  region the move is taking place.
1207  *
1208  * For now does nothing.
1209  */
1210 void vmw_resource_move_notify(struct ttm_buffer_object *bo,
1211                               struct ttm_mem_reg *mem)
1212 {
1213 }
1214
1215 /**
1216  * vmw_resource_needs_backup - Return whether a resource needs a backup buffer.
1217  *
1218  * @res:            The resource being queried.
1219  */
1220 bool vmw_resource_needs_backup(const struct vmw_resource *res)
1221 {
1222         return res->func->needs_backup;
1223 }
1224
1225 /**
1226  * vmw_resource_evict_type - Evict all resources of a specific type
1227  *
1228  * @dev_priv:       Pointer to a device private struct
1229  * @type:           The resource type to evict
1230  *
1231  * To avoid thrashing starvation or as part of the hibernation sequence,
1232  * evict all evictable resources of a specific type.
1233  */
1234 static void vmw_resource_evict_type(struct vmw_private *dev_priv,
1235                                     enum vmw_res_type type)
1236 {
1237         struct list_head *lru_list = &dev_priv->res_lru[type];
1238         struct vmw_resource *evict_res;
1239
1240         do {
1241                 write_lock(&dev_priv->resource_lock);
1242
1243                 if (list_empty(lru_list))
1244                         goto out_unlock;
1245
1246                 evict_res = vmw_resource_reference(
1247                         list_first_entry(lru_list, struct vmw_resource,
1248                                          lru_head));
1249                 list_del_init(&evict_res->lru_head);
1250                 write_unlock(&dev_priv->resource_lock);
1251                 vmw_resource_do_evict(evict_res);
1252                 vmw_resource_unreference(&evict_res);
1253         } while (1);
1254
1255 out_unlock:
1256         write_unlock(&dev_priv->resource_lock);
1257 }
1258
1259 /**
1260  * vmw_resource_evict_all - Evict all evictable resources
1261  *
1262  * @dev_priv:       Pointer to a device private struct
1263  *
1264  * To avoid thrashing starvation or as part of the hibernation sequence,
1265  * evict all evictable resources. In particular this means that all
1266  * guest-backed resources that are registered with the device are
1267  * evicted and the OTable becomes clean.
1268  */
1269 void vmw_resource_evict_all(struct vmw_private *dev_priv)
1270 {
1271         enum vmw_res_type type;
1272
1273         mutex_lock(&dev_priv->cmdbuf_mutex);
1274
1275         for (type = 0; type < vmw_res_max; ++type)
1276                 vmw_resource_evict_type(dev_priv, type);
1277
1278         mutex_unlock(&dev_priv->cmdbuf_mutex);
1279 }