1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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#define VMW_RES_EVICT_ERR_COUNT 10
36
37struct vmw_user_dma_buffer {
38 struct ttm_prime_object prime;
39 struct vmw_dma_buffer dma;
40};
41
42struct vmw_bo_user_rep {
43 uint32_t handle;
44 uint64_t map_handle;
45};
46
47struct vmw_stream {
48 struct vmw_resource res;
49 uint32_t stream_id;
50};
51
52struct vmw_user_stream {
53 struct ttm_base_object base;
54 struct vmw_stream stream;
55};
56
57
58static uint64_t vmw_user_stream_size;
59
60static const struct vmw_res_func vmw_stream_func = {
61 .res_type = vmw_res_stream,
62 .needs_backup = false,
63 .may_evict = false,
64 .type_name = "video streams",
65 .backup_placement = NULL,
66 .create = NULL,
67 .destroy = NULL,
68 .bind = NULL,
69 .unbind = NULL
70};
71
72static inline struct vmw_dma_buffer *
73vmw_dma_buffer(struct ttm_buffer_object *bo)
74{
75 return container_of(bo, struct vmw_dma_buffer, base);
76}
77
78static inline struct vmw_user_dma_buffer *
79vmw_user_dma_buffer(struct ttm_buffer_object *bo)
80{
81 struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
82 return container_of(vmw_bo, struct vmw_user_dma_buffer, dma);
83}
84
85struct vmw_resource *vmw_resource_reference(struct vmw_resource *res)
86{
87 kref_get(&res->kref);
88 return res;
89}
90
91struct vmw_resource *
92vmw_resource_reference_unless_doomed(struct vmw_resource *res)
93{
94 return kref_get_unless_zero(&res->kref) ? res : NULL;
95}
96
97
98
99
100
101
102
103
104void vmw_resource_release_id(struct vmw_resource *res)
105{
106 struct vmw_private *dev_priv = res->dev_priv;
107 struct idr *idr = &dev_priv->res_idr[res->func->res_type];
108
109 write_lock(&dev_priv->resource_lock);
110 if (res->id != -1)
111 idr_remove(idr, res->id);
112 res->id = -1;
113 write_unlock(&dev_priv->resource_lock);
114}
115
116static void vmw_resource_release(struct kref *kref)
117{
118 struct vmw_resource *res =
119 container_of(kref, struct vmw_resource, kref);
120 struct vmw_private *dev_priv = res->dev_priv;
121 int id;
122 struct idr *idr = &dev_priv->res_idr[res->func->res_type];
123
124 res->avail = false;
125 list_del_init(&res->lru_head);
126 write_unlock(&dev_priv->resource_lock);
127 if (res->backup) {
128 struct ttm_buffer_object *bo = &res->backup->base;
129
130 ttm_bo_reserve(bo, false, false, false, NULL);
131 if (!list_empty(&res->mob_head) &&
132 res->func->unbind != NULL) {
133 struct ttm_validate_buffer val_buf;
134
135 val_buf.bo = bo;
136 val_buf.shared = false;
137 res->func->unbind(res, false, &val_buf);
138 }
139 res->backup_dirty = false;
140 list_del_init(&res->mob_head);
141 ttm_bo_unreserve(bo);
142 vmw_dmabuf_unreference(&res->backup);
143 }
144
145 if (likely(res->hw_destroy != NULL)) {
146 res->hw_destroy(res);
147 mutex_lock(&dev_priv->binding_mutex);
148 vmw_context_binding_res_list_kill(&res->binding_head);
149 mutex_unlock(&dev_priv->binding_mutex);
150 }
151
152 id = res->id;
153 if (res->res_free != NULL)
154 res->res_free(res);
155 else
156 kfree(res);
157
158 write_lock(&dev_priv->resource_lock);
159
160 if (id != -1)
161 idr_remove(idr, id);
162}
163
164void vmw_resource_unreference(struct vmw_resource **p_res)
165{
166 struct vmw_resource *res = *p_res;
167 struct vmw_private *dev_priv = res->dev_priv;
168
169 *p_res = NULL;
170 write_lock(&dev_priv->resource_lock);
171 kref_put(&res->kref, vmw_resource_release);
172 write_unlock(&dev_priv->resource_lock);
173}
174
175
176
177
178
179
180
181
182
183
184int vmw_resource_alloc_id(struct vmw_resource *res)
185{
186 struct vmw_private *dev_priv = res->dev_priv;
187 int ret;
188 struct idr *idr = &dev_priv->res_idr[res->func->res_type];
189
190 BUG_ON(res->id != -1);
191
192 idr_preload(GFP_KERNEL);
193 write_lock(&dev_priv->resource_lock);
194
195 ret = idr_alloc(idr, res, 1, 0, GFP_NOWAIT);
196 if (ret >= 0)
197 res->id = ret;
198
199 write_unlock(&dev_priv->resource_lock);
200 idr_preload_end();
201 return ret < 0 ? ret : 0;
202}
203
204
205
206
207
208
209
210
211
212
213
214
215int vmw_resource_init(struct vmw_private *dev_priv, struct vmw_resource *res,
216 bool delay_id,
217 void (*res_free) (struct vmw_resource *res),
218 const struct vmw_res_func *func)
219{
220 kref_init(&res->kref);
221 res->hw_destroy = NULL;
222 res->res_free = res_free;
223 res->avail = false;
224 res->dev_priv = dev_priv;
225 res->func = func;
226 INIT_LIST_HEAD(&res->lru_head);
227 INIT_LIST_HEAD(&res->mob_head);
228 INIT_LIST_HEAD(&res->binding_head);
229 res->id = -1;
230 res->backup = NULL;
231 res->backup_offset = 0;
232 res->backup_dirty = false;
233 res->res_dirty = false;
234 if (delay_id)
235 return 0;
236 else
237 return vmw_resource_alloc_id(res);
238}
239
240
241
242
243
244
245
246
247
248
249
250
251
252void vmw_resource_activate(struct vmw_resource *res,
253 void (*hw_destroy) (struct vmw_resource *))
254{
255 struct vmw_private *dev_priv = res->dev_priv;
256
257 write_lock(&dev_priv->resource_lock);
258 res->avail = true;
259 res->hw_destroy = hw_destroy;
260 write_unlock(&dev_priv->resource_lock);
261}
262
263struct vmw_resource *vmw_resource_lookup(struct vmw_private *dev_priv,
264 struct idr *idr, int id)
265{
266 struct vmw_resource *res;
267
268 read_lock(&dev_priv->resource_lock);
269 res = idr_find(idr, id);
270 if (res && res->avail)
271 kref_get(&res->kref);
272 else
273 res = NULL;
274 read_unlock(&dev_priv->resource_lock);
275
276 if (unlikely(res == NULL))
277 return NULL;
278
279 return res;
280}
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296int vmw_user_resource_lookup_handle(struct vmw_private *dev_priv,
297 struct ttm_object_file *tfile,
298 uint32_t handle,
299 const struct vmw_user_resource_conv
300 *converter,
301 struct vmw_resource **p_res)
302{
303 struct ttm_base_object *base;
304 struct vmw_resource *res;
305 int ret = -EINVAL;
306
307 base = ttm_base_object_lookup(tfile, handle);
308 if (unlikely(base == NULL))
309 return -EINVAL;
310
311 if (unlikely(ttm_base_object_type(base) != converter->object_type))
312 goto out_bad_resource;
313
314 res = converter->base_obj_to_res(base);
315
316 read_lock(&dev_priv->resource_lock);
317 if (!res->avail || res->res_free != converter->res_free) {
318 read_unlock(&dev_priv->resource_lock);
319 goto out_bad_resource;
320 }
321
322 kref_get(&res->kref);
323 read_unlock(&dev_priv->resource_lock);
324
325 *p_res = res;
326 ret = 0;
327
328out_bad_resource:
329 ttm_base_object_unref(&base);
330
331 return ret;
332}
333
334
335
336
337
338
339int vmw_user_lookup_handle(struct vmw_private *dev_priv,
340 struct ttm_object_file *tfile,
341 uint32_t handle,
342 struct vmw_surface **out_surf,
343 struct vmw_dma_buffer **out_buf)
344{
345 struct vmw_resource *res;
346 int ret;
347
348 BUG_ON(*out_surf || *out_buf);
349
350 ret = vmw_user_resource_lookup_handle(dev_priv, tfile, handle,
351 user_surface_converter,
352 &res);
353 if (!ret) {
354 *out_surf = vmw_res_to_srf(res);
355 return 0;
356 }
357
358 *out_surf = NULL;
359 ret = vmw_user_dmabuf_lookup(tfile, handle, out_buf);
360 return ret;
361}
362
363
364
365
366
367
368
369
370
371
372
373
374static size_t vmw_dmabuf_acc_size(struct vmw_private *dev_priv, size_t size,
375 bool user)
376{
377 static size_t struct_size, user_struct_size;
378 size_t num_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
379 size_t page_array_size = ttm_round_pot(num_pages * sizeof(void *));
380
381 if (unlikely(struct_size == 0)) {
382 size_t backend_size = ttm_round_pot(vmw_tt_size);
383
384 struct_size = backend_size +
385 ttm_round_pot(sizeof(struct vmw_dma_buffer));
386 user_struct_size = backend_size +
387 ttm_round_pot(sizeof(struct vmw_user_dma_buffer));
388 }
389
390 if (dev_priv->map_mode == vmw_dma_alloc_coherent)
391 page_array_size +=
392 ttm_round_pot(num_pages * sizeof(dma_addr_t));
393
394 return ((user) ? user_struct_size : struct_size) +
395 page_array_size;
396}
397
398void vmw_dmabuf_bo_free(struct ttm_buffer_object *bo)
399{
400 struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
401
402 kfree(vmw_bo);
403}
404
405static void vmw_user_dmabuf_destroy(struct ttm_buffer_object *bo)
406{
407 struct vmw_user_dma_buffer *vmw_user_bo = vmw_user_dma_buffer(bo);
408
409 ttm_prime_object_kfree(vmw_user_bo, prime);
410}
411
412int vmw_dmabuf_init(struct vmw_private *dev_priv,
413 struct vmw_dma_buffer *vmw_bo,
414 size_t size, struct ttm_placement *placement,
415 bool interruptible,
416 void (*bo_free) (struct ttm_buffer_object *bo))
417{
418 struct ttm_bo_device *bdev = &dev_priv->bdev;
419 size_t acc_size;
420 int ret;
421 bool user = (bo_free == &vmw_user_dmabuf_destroy);
422
423 BUG_ON(!bo_free && (!user && (bo_free != vmw_dmabuf_bo_free)));
424
425 acc_size = vmw_dmabuf_acc_size(dev_priv, size, user);
426 memset(vmw_bo, 0, sizeof(*vmw_bo));
427
428 INIT_LIST_HEAD(&vmw_bo->res_list);
429
430 ret = ttm_bo_init(bdev, &vmw_bo->base, size,
431 ttm_bo_type_device, placement,
432 0, interruptible,
433 NULL, acc_size, NULL, NULL, bo_free);
434 return ret;
435}
436
437static void vmw_user_dmabuf_release(struct ttm_base_object **p_base)
438{
439 struct vmw_user_dma_buffer *vmw_user_bo;
440 struct ttm_base_object *base = *p_base;
441 struct ttm_buffer_object *bo;
442
443 *p_base = NULL;
444
445 if (unlikely(base == NULL))
446 return;
447
448 vmw_user_bo = container_of(base, struct vmw_user_dma_buffer,
449 prime.base);
450 bo = &vmw_user_bo->dma.base;
451 ttm_bo_unref(&bo);
452}
453
454static void vmw_user_dmabuf_ref_obj_release(struct ttm_base_object *base,
455 enum ttm_ref_type ref_type)
456{
457 struct vmw_user_dma_buffer *user_bo;
458 user_bo = container_of(base, struct vmw_user_dma_buffer, prime.base);
459
460 switch (ref_type) {
461 case TTM_REF_SYNCCPU_WRITE:
462 ttm_bo_synccpu_write_release(&user_bo->dma.base);
463 break;
464 default:
465 BUG();
466 }
467}
468
469
470
471
472
473
474
475
476
477
478
479
480
481int vmw_user_dmabuf_alloc(struct vmw_private *dev_priv,
482 struct ttm_object_file *tfile,
483 uint32_t size,
484 bool shareable,
485 uint32_t *handle,
486 struct vmw_dma_buffer **p_dma_buf)
487{
488 struct vmw_user_dma_buffer *user_bo;
489 struct ttm_buffer_object *tmp;
490 int ret;
491
492 user_bo = kzalloc(sizeof(*user_bo), GFP_KERNEL);
493 if (unlikely(user_bo == NULL)) {
494 DRM_ERROR("Failed to allocate a buffer.\n");
495 return -ENOMEM;
496 }
497
498 ret = vmw_dmabuf_init(dev_priv, &user_bo->dma, size,
499 (dev_priv->has_mob) ?
500 &vmw_sys_placement :
501 &vmw_vram_sys_placement, true,
502 &vmw_user_dmabuf_destroy);
503 if (unlikely(ret != 0))
504 return ret;
505
506 tmp = ttm_bo_reference(&user_bo->dma.base);
507 ret = ttm_prime_object_init(tfile,
508 size,
509 &user_bo->prime,
510 shareable,
511 ttm_buffer_type,
512 &vmw_user_dmabuf_release,
513 &vmw_user_dmabuf_ref_obj_release);
514 if (unlikely(ret != 0)) {
515 ttm_bo_unref(&tmp);
516 goto out_no_base_object;
517 }
518
519 *p_dma_buf = &user_bo->dma;
520 *handle = user_bo->prime.base.hash.key;
521
522out_no_base_object:
523 return ret;
524}
525
526
527
528
529
530
531
532
533int vmw_user_dmabuf_verify_access(struct ttm_buffer_object *bo,
534 struct ttm_object_file *tfile)
535{
536 struct vmw_user_dma_buffer *vmw_user_bo;
537
538 if (unlikely(bo->destroy != vmw_user_dmabuf_destroy))
539 return -EPERM;
540
541 vmw_user_bo = vmw_user_dma_buffer(bo);
542
543
544 if (likely(ttm_ref_object_exists(tfile, &vmw_user_bo->prime.base)))
545 return 0;
546
547 DRM_ERROR("Could not grant buffer access.\n");
548 return -EPERM;
549}
550
551
552
553
554
555
556
557
558
559
560
561
562static int vmw_user_dmabuf_synccpu_grab(struct vmw_user_dma_buffer *user_bo,
563 struct ttm_object_file *tfile,
564 uint32_t flags)
565{
566 struct ttm_buffer_object *bo = &user_bo->dma.base;
567 bool existed;
568 int ret;
569
570 if (flags & drm_vmw_synccpu_allow_cs) {
571 bool nonblock = !!(flags & drm_vmw_synccpu_dontblock);
572 long lret;
573
574 if (nonblock)
575 return reservation_object_test_signaled_rcu(bo->resv, true) ? 0 : -EBUSY;
576
577 lret = reservation_object_wait_timeout_rcu(bo->resv, true, true, MAX_SCHEDULE_TIMEOUT);
578 if (!lret)
579 return -EBUSY;
580 else if (lret < 0)
581 return lret;
582 return 0;
583 }
584
585 ret = ttm_bo_synccpu_write_grab
586 (bo, !!(flags & drm_vmw_synccpu_dontblock));
587 if (unlikely(ret != 0))
588 return ret;
589
590 ret = ttm_ref_object_add(tfile, &user_bo->prime.base,
591 TTM_REF_SYNCCPU_WRITE, &existed);
592 if (ret != 0 || existed)
593 ttm_bo_synccpu_write_release(&user_bo->dma.base);
594
595 return ret;
596}
597
598
599
600
601
602
603
604
605
606static int vmw_user_dmabuf_synccpu_release(uint32_t handle,
607 struct ttm_object_file *tfile,
608 uint32_t flags)
609{
610 if (!(flags & drm_vmw_synccpu_allow_cs))
611 return ttm_ref_object_base_unref(tfile, handle,
612 TTM_REF_SYNCCPU_WRITE);
613
614 return 0;
615}
616
617
618
619
620
621
622
623
624
625
626
627
628int vmw_user_dmabuf_synccpu_ioctl(struct drm_device *dev, void *data,
629 struct drm_file *file_priv)
630{
631 struct drm_vmw_synccpu_arg *arg =
632 (struct drm_vmw_synccpu_arg *) data;
633 struct vmw_dma_buffer *dma_buf;
634 struct vmw_user_dma_buffer *user_bo;
635 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
636 int ret;
637
638 if ((arg->flags & (drm_vmw_synccpu_read | drm_vmw_synccpu_write)) == 0
639 || (arg->flags & ~(drm_vmw_synccpu_read | drm_vmw_synccpu_write |
640 drm_vmw_synccpu_dontblock |
641 drm_vmw_synccpu_allow_cs)) != 0) {
642 DRM_ERROR("Illegal synccpu flags.\n");
643 return -EINVAL;
644 }
645
646 switch (arg->op) {
647 case drm_vmw_synccpu_grab:
648 ret = vmw_user_dmabuf_lookup(tfile, arg->handle, &dma_buf);
649 if (unlikely(ret != 0))
650 return ret;
651
652 user_bo = container_of(dma_buf, struct vmw_user_dma_buffer,
653 dma);
654 ret = vmw_user_dmabuf_synccpu_grab(user_bo, tfile, arg->flags);
655 vmw_dmabuf_unreference(&dma_buf);
656 if (unlikely(ret != 0 && ret != -ERESTARTSYS &&
657 ret != -EBUSY)) {
658 DRM_ERROR("Failed synccpu grab on handle 0x%08x.\n",
659 (unsigned int) arg->handle);
660 return ret;
661 }
662 break;
663 case drm_vmw_synccpu_release:
664 ret = vmw_user_dmabuf_synccpu_release(arg->handle, tfile,
665 arg->flags);
666 if (unlikely(ret != 0)) {
667 DRM_ERROR("Failed synccpu release on handle 0x%08x.\n",
668 (unsigned int) arg->handle);
669 return ret;
670 }
671 break;
672 default:
673 DRM_ERROR("Invalid synccpu operation.\n");
674 return -EINVAL;
675 }
676
677 return 0;
678}
679
680int vmw_dmabuf_alloc_ioctl(struct drm_device *dev, void *data,
681 struct drm_file *file_priv)
682{
683 struct vmw_private *dev_priv = vmw_priv(dev);
684 union drm_vmw_alloc_dmabuf_arg *arg =
685 (union drm_vmw_alloc_dmabuf_arg *)data;
686 struct drm_vmw_alloc_dmabuf_req *req = &arg->req;
687 struct drm_vmw_dmabuf_rep *rep = &arg->rep;
688 struct vmw_dma_buffer *dma_buf;
689 uint32_t handle;
690 int ret;
691
692 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
693 if (unlikely(ret != 0))
694 return ret;
695
696 ret = vmw_user_dmabuf_alloc(dev_priv, vmw_fpriv(file_priv)->tfile,
697 req->size, false, &handle, &dma_buf);
698 if (unlikely(ret != 0))
699 goto out_no_dmabuf;
700
701 rep->handle = handle;
702 rep->map_handle = drm_vma_node_offset_addr(&dma_buf->base.vma_node);
703 rep->cur_gmr_id = handle;
704 rep->cur_gmr_offset = 0;
705
706 vmw_dmabuf_unreference(&dma_buf);
707
708out_no_dmabuf:
709 ttm_read_unlock(&dev_priv->reservation_sem);
710
711 return ret;
712}
713
714int vmw_dmabuf_unref_ioctl(struct drm_device *dev, void *data,
715 struct drm_file *file_priv)
716{
717 struct drm_vmw_unref_dmabuf_arg *arg =
718 (struct drm_vmw_unref_dmabuf_arg *)data;
719
720 return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
721 arg->handle,
722 TTM_REF_USAGE);
723}
724
725int vmw_user_dmabuf_lookup(struct ttm_object_file *tfile,
726 uint32_t handle, struct vmw_dma_buffer **out)
727{
728 struct vmw_user_dma_buffer *vmw_user_bo;
729 struct ttm_base_object *base;
730
731 base = ttm_base_object_lookup(tfile, handle);
732 if (unlikely(base == NULL)) {
733 printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
734 (unsigned long)handle);
735 return -ESRCH;
736 }
737
738 if (unlikely(ttm_base_object_type(base) != ttm_buffer_type)) {
739 ttm_base_object_unref(&base);
740 printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
741 (unsigned long)handle);
742 return -EINVAL;
743 }
744
745 vmw_user_bo = container_of(base, struct vmw_user_dma_buffer,
746 prime.base);
747 (void)ttm_bo_reference(&vmw_user_bo->dma.base);
748 ttm_base_object_unref(&base);
749 *out = &vmw_user_bo->dma;
750
751 return 0;
752}
753
754int vmw_user_dmabuf_reference(struct ttm_object_file *tfile,
755 struct vmw_dma_buffer *dma_buf,
756 uint32_t *handle)
757{
758 struct vmw_user_dma_buffer *user_bo;
759
760 if (dma_buf->base.destroy != vmw_user_dmabuf_destroy)
761 return -EINVAL;
762
763 user_bo = container_of(dma_buf, struct vmw_user_dma_buffer, dma);
764
765 *handle = user_bo->prime.base.hash.key;
766 return ttm_ref_object_add(tfile, &user_bo->prime.base,
767 TTM_REF_USAGE, NULL);
768}
769
770
771
772
773
774static void vmw_stream_destroy(struct vmw_resource *res)
775{
776 struct vmw_private *dev_priv = res->dev_priv;
777 struct vmw_stream *stream;
778 int ret;
779
780 DRM_INFO("%s: unref\n", __func__);
781 stream = container_of(res, struct vmw_stream, res);
782
783 ret = vmw_overlay_unref(dev_priv, stream->stream_id);
784 WARN_ON(ret != 0);
785}
786
787static int vmw_stream_init(struct vmw_private *dev_priv,
788 struct vmw_stream *stream,
789 void (*res_free) (struct vmw_resource *res))
790{
791 struct vmw_resource *res = &stream->res;
792 int ret;
793
794 ret = vmw_resource_init(dev_priv, res, false, res_free,
795 &vmw_stream_func);
796
797 if (unlikely(ret != 0)) {
798 if (res_free == NULL)
799 kfree(stream);
800 else
801 res_free(&stream->res);
802 return ret;
803 }
804
805 ret = vmw_overlay_claim(dev_priv, &stream->stream_id);
806 if (ret) {
807 vmw_resource_unreference(&res);
808 return ret;
809 }
810
811 DRM_INFO("%s: claimed\n", __func__);
812
813 vmw_resource_activate(&stream->res, vmw_stream_destroy);
814 return 0;
815}
816
817static void vmw_user_stream_free(struct vmw_resource *res)
818{
819 struct vmw_user_stream *stream =
820 container_of(res, struct vmw_user_stream, stream.res);
821 struct vmw_private *dev_priv = res->dev_priv;
822
823 ttm_base_object_kfree(stream, base);
824 ttm_mem_global_free(vmw_mem_glob(dev_priv),
825 vmw_user_stream_size);
826}
827
828
829
830
831
832
833static void vmw_user_stream_base_release(struct ttm_base_object **p_base)
834{
835 struct ttm_base_object *base = *p_base;
836 struct vmw_user_stream *stream =
837 container_of(base, struct vmw_user_stream, base);
838 struct vmw_resource *res = &stream->stream.res;
839
840 *p_base = NULL;
841 vmw_resource_unreference(&res);
842}
843
844int vmw_stream_unref_ioctl(struct drm_device *dev, void *data,
845 struct drm_file *file_priv)
846{
847 struct vmw_private *dev_priv = vmw_priv(dev);
848 struct vmw_resource *res;
849 struct vmw_user_stream *stream;
850 struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
851 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
852 struct idr *idr = &dev_priv->res_idr[vmw_res_stream];
853 int ret = 0;
854
855
856 res = vmw_resource_lookup(dev_priv, idr, arg->stream_id);
857 if (unlikely(res == NULL))
858 return -EINVAL;
859
860 if (res->res_free != &vmw_user_stream_free) {
861 ret = -EINVAL;
862 goto out;
863 }
864
865 stream = container_of(res, struct vmw_user_stream, stream.res);
866 if (stream->base.tfile != tfile) {
867 ret = -EINVAL;
868 goto out;
869 }
870
871 ttm_ref_object_base_unref(tfile, stream->base.hash.key, TTM_REF_USAGE);
872out:
873 vmw_resource_unreference(&res);
874 return ret;
875}
876
877int vmw_stream_claim_ioctl(struct drm_device *dev, void *data,
878 struct drm_file *file_priv)
879{
880 struct vmw_private *dev_priv = vmw_priv(dev);
881 struct vmw_user_stream *stream;
882 struct vmw_resource *res;
883 struct vmw_resource *tmp;
884 struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
885 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
886 int ret;
887
888
889
890
891
892
893 if (unlikely(vmw_user_stream_size == 0))
894 vmw_user_stream_size = ttm_round_pot(sizeof(*stream)) + 128;
895
896 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
897 if (unlikely(ret != 0))
898 return ret;
899
900 ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
901 vmw_user_stream_size,
902 false, true);
903 if (unlikely(ret != 0)) {
904 if (ret != -ERESTARTSYS)
905 DRM_ERROR("Out of graphics memory for stream"
906 " creation.\n");
907 goto out_unlock;
908 }
909
910
911 stream = kmalloc(sizeof(*stream), GFP_KERNEL);
912 if (unlikely(stream == NULL)) {
913 ttm_mem_global_free(vmw_mem_glob(dev_priv),
914 vmw_user_stream_size);
915 ret = -ENOMEM;
916 goto out_unlock;
917 }
918
919 res = &stream->stream.res;
920 stream->base.shareable = false;
921 stream->base.tfile = NULL;
922
923
924
925
926
927 ret = vmw_stream_init(dev_priv, &stream->stream, vmw_user_stream_free);
928 if (unlikely(ret != 0))
929 goto out_unlock;
930
931 tmp = vmw_resource_reference(res);
932 ret = ttm_base_object_init(tfile, &stream->base, false, VMW_RES_STREAM,
933 &vmw_user_stream_base_release, NULL);
934
935 if (unlikely(ret != 0)) {
936 vmw_resource_unreference(&tmp);
937 goto out_err;
938 }
939
940 arg->stream_id = res->id;
941out_err:
942 vmw_resource_unreference(&res);
943out_unlock:
944 ttm_read_unlock(&dev_priv->reservation_sem);
945 return ret;
946}
947
948int vmw_user_stream_lookup(struct vmw_private *dev_priv,
949 struct ttm_object_file *tfile,
950 uint32_t *inout_id, struct vmw_resource **out)
951{
952 struct vmw_user_stream *stream;
953 struct vmw_resource *res;
954 int ret;
955
956 res = vmw_resource_lookup(dev_priv, &dev_priv->res_idr[vmw_res_stream],
957 *inout_id);
958 if (unlikely(res == NULL))
959 return -EINVAL;
960
961 if (res->res_free != &vmw_user_stream_free) {
962 ret = -EINVAL;
963 goto err_ref;
964 }
965
966 stream = container_of(res, struct vmw_user_stream, stream.res);
967 if (stream->base.tfile != tfile) {
968 ret = -EPERM;
969 goto err_ref;
970 }
971
972 *inout_id = stream->stream.stream_id;
973 *out = res;
974 return 0;
975err_ref:
976 vmw_resource_unreference(&res);
977 return ret;
978}
979
980
981
982
983
984
985
986
987
988
989
990
991
992int vmw_dumb_create(struct drm_file *file_priv,
993 struct drm_device *dev,
994 struct drm_mode_create_dumb *args)
995{
996 struct vmw_private *dev_priv = vmw_priv(dev);
997 struct vmw_dma_buffer *dma_buf;
998 int ret;
999
1000 args->pitch = args->width * ((args->bpp + 7) / 8);
1001 args->size = args->pitch * args->height;
1002
1003 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
1004 if (unlikely(ret != 0))
1005 return ret;
1006
1007 ret = vmw_user_dmabuf_alloc(dev_priv, vmw_fpriv(file_priv)->tfile,
1008 args->size, false, &args->handle,
1009 &dma_buf);
1010 if (unlikely(ret != 0))
1011 goto out_no_dmabuf;
1012
1013 vmw_dmabuf_unreference(&dma_buf);
1014out_no_dmabuf:
1015 ttm_read_unlock(&dev_priv->reservation_sem);
1016 return ret;
1017}
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029int vmw_dumb_map_offset(struct drm_file *file_priv,
1030 struct drm_device *dev, uint32_t handle,
1031 uint64_t *offset)
1032{
1033 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1034 struct vmw_dma_buffer *out_buf;
1035 int ret;
1036
1037 ret = vmw_user_dmabuf_lookup(tfile, handle, &out_buf);
1038 if (ret != 0)
1039 return -EINVAL;
1040
1041 *offset = drm_vma_node_offset_addr(&out_buf->base.vma_node);
1042 vmw_dmabuf_unreference(&out_buf);
1043 return 0;
1044}
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055int vmw_dumb_destroy(struct drm_file *file_priv,
1056 struct drm_device *dev,
1057 uint32_t handle)
1058{
1059 return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
1060 handle, TTM_REF_USAGE);
1061}
1062
1063
1064
1065
1066
1067
1068
1069
1070static int vmw_resource_buf_alloc(struct vmw_resource *res,
1071 bool interruptible)
1072{
1073 unsigned long size =
1074 (res->backup_size + PAGE_SIZE - 1) & PAGE_MASK;
1075 struct vmw_dma_buffer *backup;
1076 int ret;
1077
1078 if (likely(res->backup)) {
1079 BUG_ON(res->backup->base.num_pages * PAGE_SIZE < size);
1080 return 0;
1081 }
1082
1083 backup = kzalloc(sizeof(*backup), GFP_KERNEL);
1084 if (unlikely(backup == NULL))
1085 return -ENOMEM;
1086
1087 ret = vmw_dmabuf_init(res->dev_priv, backup, res->backup_size,
1088 res->func->backup_placement,
1089 interruptible,
1090 &vmw_dmabuf_bo_free);
1091 if (unlikely(ret != 0))
1092 goto out_no_dmabuf;
1093
1094 res->backup = backup;
1095
1096out_no_dmabuf:
1097 return ret;
1098}
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111static int vmw_resource_do_validate(struct vmw_resource *res,
1112 struct ttm_validate_buffer *val_buf)
1113{
1114 int ret = 0;
1115 const struct vmw_res_func *func = res->func;
1116
1117 if (unlikely(res->id == -1)) {
1118 ret = func->create(res);
1119 if (unlikely(ret != 0))
1120 return ret;
1121 }
1122
1123 if (func->bind &&
1124 ((func->needs_backup && list_empty(&res->mob_head) &&
1125 val_buf->bo != NULL) ||
1126 (!func->needs_backup && val_buf->bo != NULL))) {
1127 ret = func->bind(res, val_buf);
1128 if (unlikely(ret != 0))
1129 goto out_bind_failed;
1130 if (func->needs_backup)
1131 list_add_tail(&res->mob_head, &res->backup->res_list);
1132 }
1133
1134
1135
1136
1137
1138
1139
1140 res->res_dirty = true;
1141
1142 return 0;
1143
1144out_bind_failed:
1145 func->destroy(res);
1146
1147 return ret;
1148}
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162void vmw_resource_unreserve(struct vmw_resource *res,
1163 struct vmw_dma_buffer *new_backup,
1164 unsigned long new_backup_offset)
1165{
1166 struct vmw_private *dev_priv = res->dev_priv;
1167
1168 if (!list_empty(&res->lru_head))
1169 return;
1170
1171 if (new_backup && new_backup != res->backup) {
1172
1173 if (res->backup) {
1174 lockdep_assert_held(&res->backup->base.resv->lock.base);
1175 list_del_init(&res->mob_head);
1176 vmw_dmabuf_unreference(&res->backup);
1177 }
1178
1179 res->backup = vmw_dmabuf_reference(new_backup);
1180 lockdep_assert_held(&new_backup->base.resv->lock.base);
1181 list_add_tail(&res->mob_head, &new_backup->res_list);
1182 }
1183 if (new_backup)
1184 res->backup_offset = new_backup_offset;
1185
1186 if (!res->func->may_evict || res->id == -1)
1187 return;
1188
1189 write_lock(&dev_priv->resource_lock);
1190 list_add_tail(&res->lru_head,
1191 &res->dev_priv->res_lru[res->func->res_type]);
1192 write_unlock(&dev_priv->resource_lock);
1193}
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206static int
1207vmw_resource_check_buffer(struct vmw_resource *res,
1208 bool interruptible,
1209 struct ttm_validate_buffer *val_buf)
1210{
1211 struct list_head val_list;
1212 bool backup_dirty = false;
1213 int ret;
1214
1215 if (unlikely(res->backup == NULL)) {
1216 ret = vmw_resource_buf_alloc(res, interruptible);
1217 if (unlikely(ret != 0))
1218 return ret;
1219 }
1220
1221 INIT_LIST_HEAD(&val_list);
1222 val_buf->bo = ttm_bo_reference(&res->backup->base);
1223 val_buf->shared = false;
1224 list_add_tail(&val_buf->head, &val_list);
1225 ret = ttm_eu_reserve_buffers(NULL, &val_list, interruptible, NULL);
1226 if (unlikely(ret != 0))
1227 goto out_no_reserve;
1228
1229 if (res->func->needs_backup && list_empty(&res->mob_head))
1230 return 0;
1231
1232 backup_dirty = res->backup_dirty;
1233 ret = ttm_bo_validate(&res->backup->base,
1234 res->func->backup_placement,
1235 true, false);
1236
1237 if (unlikely(ret != 0))
1238 goto out_no_validate;
1239
1240 return 0;
1241
1242out_no_validate:
1243 ttm_eu_backoff_reservation(NULL, &val_list);
1244out_no_reserve:
1245 ttm_bo_unref(&val_buf->bo);
1246 if (backup_dirty)
1247 vmw_dmabuf_unreference(&res->backup);
1248
1249 return ret;
1250}
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262int vmw_resource_reserve(struct vmw_resource *res, bool no_backup)
1263{
1264 struct vmw_private *dev_priv = res->dev_priv;
1265 int ret;
1266
1267 write_lock(&dev_priv->resource_lock);
1268 list_del_init(&res->lru_head);
1269 write_unlock(&dev_priv->resource_lock);
1270
1271 if (res->func->needs_backup && res->backup == NULL &&
1272 !no_backup) {
1273 ret = vmw_resource_buf_alloc(res, true);
1274 if (unlikely(ret != 0))
1275 return ret;
1276 }
1277
1278 return 0;
1279}
1280
1281
1282
1283
1284
1285
1286
1287static void
1288vmw_resource_backoff_reservation(struct ttm_validate_buffer *val_buf)
1289{
1290 struct list_head val_list;
1291
1292 if (likely(val_buf->bo == NULL))
1293 return;
1294
1295 INIT_LIST_HEAD(&val_list);
1296 list_add_tail(&val_buf->head, &val_list);
1297 ttm_eu_backoff_reservation(NULL, &val_list);
1298 ttm_bo_unref(&val_buf->bo);
1299}
1300
1301
1302
1303
1304
1305
1306
1307
1308int vmw_resource_do_evict(struct vmw_resource *res, bool interruptible)
1309{
1310 struct ttm_validate_buffer val_buf;
1311 const struct vmw_res_func *func = res->func;
1312 int ret;
1313
1314 BUG_ON(!func->may_evict);
1315
1316 val_buf.bo = NULL;
1317 val_buf.shared = false;
1318 ret = vmw_resource_check_buffer(res, interruptible, &val_buf);
1319 if (unlikely(ret != 0))
1320 return ret;
1321
1322 if (unlikely(func->unbind != NULL &&
1323 (!func->needs_backup || !list_empty(&res->mob_head)))) {
1324 ret = func->unbind(res, res->res_dirty, &val_buf);
1325 if (unlikely(ret != 0))
1326 goto out_no_unbind;
1327 list_del_init(&res->mob_head);
1328 }
1329 ret = func->destroy(res);
1330 res->backup_dirty = true;
1331 res->res_dirty = false;
1332out_no_unbind:
1333 vmw_resource_backoff_reservation(&val_buf);
1334
1335 return ret;
1336}
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350int vmw_resource_validate(struct vmw_resource *res)
1351{
1352 int ret;
1353 struct vmw_resource *evict_res;
1354 struct vmw_private *dev_priv = res->dev_priv;
1355 struct list_head *lru_list = &dev_priv->res_lru[res->func->res_type];
1356 struct ttm_validate_buffer val_buf;
1357 unsigned err_count = 0;
1358
1359 if (likely(!res->func->may_evict))
1360 return 0;
1361
1362 val_buf.bo = NULL;
1363 val_buf.shared = false;
1364 if (res->backup)
1365 val_buf.bo = &res->backup->base;
1366 do {
1367 ret = vmw_resource_do_validate(res, &val_buf);
1368 if (likely(ret != -EBUSY))
1369 break;
1370
1371 write_lock(&dev_priv->resource_lock);
1372 if (list_empty(lru_list) || !res->func->may_evict) {
1373 DRM_ERROR("Out of device device resources "
1374 "for %s.\n", res->func->type_name);
1375 ret = -EBUSY;
1376 write_unlock(&dev_priv->resource_lock);
1377 break;
1378 }
1379
1380 evict_res = vmw_resource_reference
1381 (list_first_entry(lru_list, struct vmw_resource,
1382 lru_head));
1383 list_del_init(&evict_res->lru_head);
1384
1385 write_unlock(&dev_priv->resource_lock);
1386
1387 ret = vmw_resource_do_evict(evict_res, true);
1388 if (unlikely(ret != 0)) {
1389 write_lock(&dev_priv->resource_lock);
1390 list_add_tail(&evict_res->lru_head, lru_list);
1391 write_unlock(&dev_priv->resource_lock);
1392 if (ret == -ERESTARTSYS ||
1393 ++err_count > VMW_RES_EVICT_ERR_COUNT) {
1394 vmw_resource_unreference(&evict_res);
1395 goto out_no_validate;
1396 }
1397 }
1398
1399 vmw_resource_unreference(&evict_res);
1400 } while (1);
1401
1402 if (unlikely(ret != 0))
1403 goto out_no_validate;
1404 else if (!res->func->needs_backup && res->backup) {
1405 list_del_init(&res->mob_head);
1406 vmw_dmabuf_unreference(&res->backup);
1407 }
1408
1409 return 0;
1410
1411out_no_validate:
1412 return ret;
1413}
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427void vmw_fence_single_bo(struct ttm_buffer_object *bo,
1428 struct vmw_fence_obj *fence)
1429{
1430 struct ttm_bo_device *bdev = bo->bdev;
1431
1432 struct vmw_private *dev_priv =
1433 container_of(bdev, struct vmw_private, bdev);
1434
1435 if (fence == NULL) {
1436 vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
1437 reservation_object_add_excl_fence(bo->resv, &fence->base);
1438 fence_put(&fence->base);
1439 } else
1440 reservation_object_add_excl_fence(bo->resv, &fence->base);
1441}
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462void vmw_resource_move_notify(struct ttm_buffer_object *bo,
1463 struct ttm_mem_reg *mem)
1464{
1465 struct vmw_dma_buffer *dma_buf;
1466
1467 if (mem == NULL)
1468 return;
1469
1470 if (bo->destroy != vmw_dmabuf_bo_free &&
1471 bo->destroy != vmw_user_dmabuf_destroy)
1472 return;
1473
1474 dma_buf = container_of(bo, struct vmw_dma_buffer, base);
1475
1476 if (mem->mem_type != VMW_PL_MOB) {
1477 struct vmw_resource *res, *n;
1478 struct ttm_validate_buffer val_buf;
1479
1480 val_buf.bo = bo;
1481 val_buf.shared = false;
1482
1483 list_for_each_entry_safe(res, n, &dma_buf->res_list, mob_head) {
1484
1485 if (unlikely(res->func->unbind == NULL))
1486 continue;
1487
1488 (void) res->func->unbind(res, true, &val_buf);
1489 res->backup_dirty = true;
1490 res->res_dirty = false;
1491 list_del_init(&res->mob_head);
1492 }
1493
1494 (void) ttm_bo_wait(bo, false, false, false);
1495 }
1496}
1497
1498
1499
1500
1501
1502
1503bool vmw_resource_needs_backup(const struct vmw_resource *res)
1504{
1505 return res->func->needs_backup;
1506}
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517static void vmw_resource_evict_type(struct vmw_private *dev_priv,
1518 enum vmw_res_type type)
1519{
1520 struct list_head *lru_list = &dev_priv->res_lru[type];
1521 struct vmw_resource *evict_res;
1522 unsigned err_count = 0;
1523 int ret;
1524
1525 do {
1526 write_lock(&dev_priv->resource_lock);
1527
1528 if (list_empty(lru_list))
1529 goto out_unlock;
1530
1531 evict_res = vmw_resource_reference(
1532 list_first_entry(lru_list, struct vmw_resource,
1533 lru_head));
1534 list_del_init(&evict_res->lru_head);
1535 write_unlock(&dev_priv->resource_lock);
1536
1537 ret = vmw_resource_do_evict(evict_res, false);
1538 if (unlikely(ret != 0)) {
1539 write_lock(&dev_priv->resource_lock);
1540 list_add_tail(&evict_res->lru_head, lru_list);
1541 write_unlock(&dev_priv->resource_lock);
1542 if (++err_count > VMW_RES_EVICT_ERR_COUNT) {
1543 vmw_resource_unreference(&evict_res);
1544 return;
1545 }
1546 }
1547
1548 vmw_resource_unreference(&evict_res);
1549 } while (1);
1550
1551out_unlock:
1552 write_unlock(&dev_priv->resource_lock);
1553}
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565void vmw_resource_evict_all(struct vmw_private *dev_priv)
1566{
1567 enum vmw_res_type type;
1568
1569 mutex_lock(&dev_priv->cmdbuf_mutex);
1570
1571 for (type = 0; type < vmw_res_max; ++type)
1572 vmw_resource_evict_type(dev_priv, type);
1573
1574 mutex_unlock(&dev_priv->cmdbuf_mutex);
1575}
1576