forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-06 08f87f769b595151be1afeff53e144f543faa614
kernel/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
....@@ -28,9 +28,8 @@
2828
2929 #include <drm/ttm/ttm_placement.h>
3030
31
-#include <drm/drmP.h>
3231 #include "vmwgfx_drv.h"
33
-#include "drm/ttm/ttm_object.h"
32
+#include "ttm_object.h"
3433
3534
3635 /**
....@@ -259,7 +258,7 @@
259258 ret = ttm_bo_validate(bo, &placement, &ctx);
260259
261260 /* For some reason we didn't end up at the start of vram */
262
- WARN_ON(ret == 0 && bo->offset != 0);
261
+ WARN_ON(ret == 0 && bo->mem.start != 0);
263262 if (!ret)
264263 vmw_bo_pin_reserved(buf, true);
265264
....@@ -318,7 +317,7 @@
318317 {
319318 if (bo->mem.mem_type == TTM_PL_VRAM) {
320319 ptr->gmrId = SVGA_GMR_FRAMEBUFFER;
321
- ptr->offset = bo->offset;
320
+ ptr->offset = bo->mem.start << PAGE_SHIFT;
322321 } else {
323322 ptr->gmrId = bo->mem.start;
324323 ptr->offset = 0;
....@@ -342,7 +341,7 @@
342341 uint32_t old_mem_type = bo->mem.mem_type;
343342 int ret;
344343
345
- lockdep_assert_held(&bo->resv->lock.base);
344
+ dma_resv_assert_held(bo->base.resv);
346345
347346 if (pin) {
348347 if (vbo->pin_count++ > 0)
....@@ -355,10 +354,12 @@
355354
356355 pl.fpfn = 0;
357356 pl.lpfn = 0;
358
- pl.flags = TTM_PL_FLAG_VRAM | VMW_PL_FLAG_GMR | VMW_PL_FLAG_MOB
359
- | TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
357
+ pl.mem_type = bo->mem.mem_type;
358
+ pl.flags = bo->mem.placement;
360359 if (pin)
361360 pl.flags |= TTM_PL_FLAG_NO_EVICT;
361
+ else
362
+ pl.flags &= ~TTM_PL_FLAG_NO_EVICT;
362363
363364 memset(&placement, 0, sizeof(placement));
364365 placement.num_placement = 1;
....@@ -441,7 +442,8 @@
441442 struct_size = backend_size +
442443 ttm_round_pot(sizeof(struct vmw_buffer_object));
443444 user_struct_size = backend_size +
444
- ttm_round_pot(sizeof(struct vmw_user_buffer_object));
445
+ ttm_round_pot(sizeof(struct vmw_user_buffer_object)) +
446
+ TTM_OBJ_EXTRA_SIZE;
445447 }
446448
447449 if (dev_priv->map_mode == vmw_dma_alloc_coherent)
....@@ -462,6 +464,8 @@
462464 {
463465 struct vmw_buffer_object *vmw_bo = vmw_buffer_object(bo);
464466
467
+ WARN_ON(vmw_bo->dirty);
468
+ WARN_ON(!RB_EMPTY_ROOT(&vmw_bo->res_tree));
465469 vmw_bo_unmap(vmw_bo);
466470 kfree(vmw_bo);
467471 }
....@@ -475,8 +479,11 @@
475479 static void vmw_user_bo_destroy(struct ttm_buffer_object *bo)
476480 {
477481 struct vmw_user_buffer_object *vmw_user_bo = vmw_user_buffer_object(bo);
482
+ struct vmw_buffer_object *vbo = &vmw_user_bo->vbo;
478483
479
- vmw_bo_unmap(&vmw_user_bo->vbo);
484
+ WARN_ON(vbo->dirty);
485
+ WARN_ON(!RB_EMPTY_ROOT(&vbo->res_tree));
486
+ vmw_bo_unmap(vbo);
480487 ttm_prime_object_kfree(vmw_user_bo, prime);
481488 }
482489
....@@ -509,8 +516,9 @@
509516
510517 acc_size = vmw_bo_acc_size(dev_priv, size, user);
511518 memset(vmw_bo, 0, sizeof(*vmw_bo));
512
-
513
- INIT_LIST_HEAD(&vmw_bo->res_list);
519
+ BUILD_BUG_ON(TTM_MAX_BO_PRIORITY <= 3);
520
+ vmw_bo->base.priority = 3;
521
+ vmw_bo->res_tree = RB_ROOT;
514522
515523 ret = ttm_bo_init(bdev, &vmw_bo->base, size,
516524 ttm_bo_type_device, placement,
....@@ -533,7 +541,6 @@
533541 {
534542 struct vmw_user_buffer_object *vmw_user_bo;
535543 struct ttm_base_object *base = *p_base;
536
- struct ttm_buffer_object *bo;
537544
538545 *p_base = NULL;
539546
....@@ -542,8 +549,7 @@
542549
543550 vmw_user_bo = container_of(base, struct vmw_user_buffer_object,
544551 prime.base);
545
- bo = &vmw_user_bo->vbo.base;
546
- ttm_bo_unref(&bo);
552
+ ttm_bo_put(&vmw_user_bo->vbo.base);
547553 }
548554
549555
....@@ -566,7 +572,7 @@
566572
567573 switch (ref_type) {
568574 case TTM_REF_SYNCCPU_WRITE:
569
- ttm_bo_synccpu_write_release(&user_bo->vbo.base);
575
+ atomic_dec(&user_bo->vbo.cpu_writers);
570576 break;
571577 default:
572578 WARN_ONCE(true, "Undefined buffer object reference release.\n");
....@@ -596,7 +602,6 @@
596602 struct ttm_base_object **p_base)
597603 {
598604 struct vmw_user_buffer_object *user_bo;
599
- struct ttm_buffer_object *tmp;
600605 int ret;
601606
602607 user_bo = kzalloc(sizeof(*user_bo), GFP_KERNEL);
....@@ -613,7 +618,7 @@
613618 if (unlikely(ret != 0))
614619 return ret;
615620
616
- tmp = ttm_bo_reference(&user_bo->vbo.base);
621
+ ttm_bo_get(&user_bo->vbo.base);
617622 ret = ttm_prime_object_init(tfile,
618623 size,
619624 &user_bo->prime,
....@@ -622,7 +627,7 @@
622627 &vmw_user_bo_release,
623628 &vmw_user_bo_ref_obj_release);
624629 if (unlikely(ret != 0)) {
625
- ttm_bo_unref(&tmp);
630
+ ttm_bo_put(&user_bo->vbo.base);
626631 goto out_no_base_object;
627632 }
628633
....@@ -631,7 +636,7 @@
631636 *p_base = &user_bo->prime.base;
632637 kref_get(&(*p_base)->refcount);
633638 }
634
- *handle = user_bo->prime.base.hash.key;
639
+ *handle = user_bo->prime.base.handle;
635640
636641 out_no_base_object:
637642 return ret;
....@@ -683,16 +688,16 @@
683688 struct ttm_object_file *tfile,
684689 uint32_t flags)
685690 {
691
+ bool nonblock = !!(flags & drm_vmw_synccpu_dontblock);
686692 struct ttm_buffer_object *bo = &user_bo->vbo.base;
687693 bool existed;
688694 int ret;
689695
690696 if (flags & drm_vmw_synccpu_allow_cs) {
691
- bool nonblock = !!(flags & drm_vmw_synccpu_dontblock);
692697 long lret;
693698
694
- lret = reservation_object_wait_timeout_rcu
695
- (bo->resv, true, true,
699
+ lret = dma_resv_wait_timeout_rcu
700
+ (bo->base.resv, true, true,
696701 nonblock ? 0 : MAX_SCHEDULE_TIMEOUT);
697702 if (!lret)
698703 return -EBUSY;
....@@ -701,15 +706,22 @@
701706 return 0;
702707 }
703708
704
- ret = ttm_bo_synccpu_write_grab
705
- (bo, !!(flags & drm_vmw_synccpu_dontblock));
709
+ ret = ttm_bo_reserve(bo, true, nonblock, NULL);
710
+ if (unlikely(ret != 0))
711
+ return ret;
712
+
713
+ ret = ttm_bo_wait(bo, true, nonblock);
714
+ if (likely(ret == 0))
715
+ atomic_inc(&user_bo->vbo.cpu_writers);
716
+
717
+ ttm_bo_unreserve(bo);
706718 if (unlikely(ret != 0))
707719 return ret;
708720
709721 ret = ttm_ref_object_add(tfile, &user_bo->prime.base,
710722 TTM_REF_SYNCCPU_WRITE, &existed, false);
711723 if (ret != 0 || existed)
712
- ttm_bo_synccpu_write_release(&user_bo->vbo.base);
724
+ atomic_dec(&user_bo->vbo.cpu_writers);
713725
714726 return ret;
715727 }
....@@ -837,7 +849,7 @@
837849 goto out_no_bo;
838850
839851 rep->handle = handle;
840
- rep->map_handle = drm_vma_node_offset_addr(&vbo->base.vma_node);
852
+ rep->map_handle = drm_vma_node_offset_addr(&vbo->base.base.vma_node);
841853 rep->cur_gmr_id = handle;
842854 rep->cur_gmr_offset = 0;
843855
....@@ -910,7 +922,7 @@
910922
911923 vmw_user_bo = container_of(base, struct vmw_user_buffer_object,
912924 prime.base);
913
- (void)ttm_bo_reference(&vmw_user_bo->vbo.base);
925
+ ttm_bo_get(&vmw_user_bo->vbo.base);
914926 if (p_base)
915927 *p_base = base;
916928 else
....@@ -920,6 +932,47 @@
920932 return 0;
921933 }
922934
935
+/**
936
+ * vmw_user_bo_noref_lookup - Look up a vmw user buffer object without reference
937
+ * @tfile: The TTM object file the handle is registered with.
938
+ * @handle: The user buffer object handle.
939
+ *
940
+ * This function looks up a struct vmw_user_bo and returns a pointer to the
941
+ * struct vmw_buffer_object it derives from without refcounting the pointer.
942
+ * The returned pointer is only valid until vmw_user_bo_noref_release() is
943
+ * called, and the object pointed to by the returned pointer may be doomed.
944
+ * Any persistent usage of the object requires a refcount to be taken using
945
+ * ttm_bo_reference_unless_doomed(). Iff this function returns successfully it
946
+ * needs to be paired with vmw_user_bo_noref_release() and no sleeping-
947
+ * or scheduling functions may be called inbetween these function calls.
948
+ *
949
+ * Return: A struct vmw_buffer_object pointer if successful or negative
950
+ * error pointer on failure.
951
+ */
952
+struct vmw_buffer_object *
953
+vmw_user_bo_noref_lookup(struct ttm_object_file *tfile, u32 handle)
954
+{
955
+ struct vmw_user_buffer_object *vmw_user_bo;
956
+ struct ttm_base_object *base;
957
+
958
+ base = ttm_base_object_noref_lookup(tfile, handle);
959
+ if (!base) {
960
+ DRM_ERROR("Invalid buffer object handle 0x%08lx.\n",
961
+ (unsigned long)handle);
962
+ return ERR_PTR(-ESRCH);
963
+ }
964
+
965
+ if (unlikely(ttm_base_object_type(base) != ttm_buffer_type)) {
966
+ ttm_base_object_noref_release();
967
+ DRM_ERROR("Invalid buffer object handle 0x%08lx.\n",
968
+ (unsigned long)handle);
969
+ return ERR_PTR(-EINVAL);
970
+ }
971
+
972
+ vmw_user_bo = container_of(base, struct vmw_user_buffer_object,
973
+ prime.base);
974
+ return &vmw_user_bo->vbo;
975
+}
923976
924977 /**
925978 * vmw_user_bo_reference - Open a handle to a vmw user buffer object.
....@@ -940,7 +993,7 @@
940993
941994 user_bo = container_of(vbo, struct vmw_user_buffer_object, vbo);
942995
943
- *handle = user_bo->prime.base.hash.key;
996
+ *handle = user_bo->prime.base.handle;
944997 return ttm_ref_object_add(tfile, &user_bo->prime.base,
945998 TTM_REF_USAGE, NULL, false);
946999 }
....@@ -968,10 +1021,10 @@
9681021
9691022 if (fence == NULL) {
9701023 vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
971
- reservation_object_add_excl_fence(bo->resv, &fence->base);
1024
+ dma_resv_add_excl_fence(bo->base.resv, &fence->base);
9721025 dma_fence_put(&fence->base);
9731026 } else
974
- reservation_object_add_excl_fence(bo->resv, &fence->base);
1027
+ dma_resv_add_excl_fence(bo->base.resv, &fence->base);
9751028 }
9761029
9771030
....@@ -1038,7 +1091,7 @@
10381091 if (ret != 0)
10391092 return -EINVAL;
10401093
1041
- *offset = drm_vma_node_offset_addr(&out_buf->base.vma_node);
1094
+ *offset = drm_vma_node_offset_addr(&out_buf->base.base.vma_node);
10421095 vmw_bo_unreference(&out_buf);
10431096 return 0;
10441097 }
....@@ -1084,14 +1137,14 @@
10841137 * vmw_bo_move_notify - TTM move_notify_callback
10851138 *
10861139 * @bo: The TTM buffer object about to move.
1087
- * @mem: The struct ttm_mem_reg indicating to what memory
1140
+ * @mem: The struct ttm_resource indicating to what memory
10881141 * region the move is taking place.
10891142 *
10901143 * Detaches cached maps and device bindings that require that the
10911144 * buffer doesn't move.
10921145 */
10931146 void vmw_bo_move_notify(struct ttm_buffer_object *bo,
1094
- struct ttm_mem_reg *mem)
1147
+ struct ttm_resource *mem)
10951148 {
10961149 struct vmw_buffer_object *vbo;
10971150