xref: /linux/drivers/gpu/drm/xe/display/intel_fb_bo.c (revision 566ab427f827b0256d3e8ce0235d088e6a9c28bd)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright © 2021 Intel Corporation
4  */
5 
6 #include <drm/drm_modeset_helper.h>
7 #include <drm/ttm/ttm_bo.h>
8 
9 #include "intel_display_types.h"
10 #include "intel_fb.h"
11 #include "intel_fb_bo.h"
12 #include "xe_bo.h"
13 
14 void intel_fb_bo_framebuffer_fini(struct xe_bo *bo)
15 {
16 	if (bo->flags & XE_BO_FLAG_PINNED) {
17 		/* Unpin our kernel fb first */
18 		xe_bo_lock(bo, false);
19 		xe_bo_unpin(bo);
20 		xe_bo_unlock(bo);
21 	}
22 	xe_bo_put(bo);
23 }
24 
25 int intel_fb_bo_framebuffer_init(struct intel_framebuffer *intel_fb,
26 				 struct xe_bo *bo,
27 				 struct drm_mode_fb_cmd2 *mode_cmd)
28 {
29 	struct xe_device *xe = to_xe_device(bo->ttm.base.dev);
30 	int ret;
31 
32 	/*
33 	 * Some modifiers require physical alignment of 64KiB VRAM pages;
34 	 * require that the BO in those cases is created correctly.
35 	 */
36 	if (XE_IOCTL_DBG(xe, intel_fb_needs_64k_phys(mode_cmd->modifier[0]) &&
37 			     !(bo->flags & XE_BO_FLAG_NEEDS_64K)))
38 		return -EINVAL;
39 
40 	xe_bo_get(bo);
41 
42 	ret = ttm_bo_reserve(&bo->ttm, true, false, NULL);
43 	if (ret)
44 		goto err;
45 
46 	if (!(bo->flags & XE_BO_FLAG_SCANOUT)) {
47 		/*
48 		 * XE_BO_FLAG_SCANOUT should ideally be set at creation, or is
49 		 * automatically set when creating FB. We cannot change caching
50 		 * mode when the boect is VM_BINDed, so we can only set
51 		 * coherency with display when unbound.
52 		 */
53 		if (XE_IOCTL_DBG(xe, !list_empty(&bo->ttm.base.gpuva.list))) {
54 			ttm_bo_unreserve(&bo->ttm);
55 			ret = -EINVAL;
56 			goto err;
57 		}
58 		bo->flags |= XE_BO_FLAG_SCANOUT;
59 	}
60 	ttm_bo_unreserve(&bo->ttm);
61 	return 0;
62 
63 err:
64 	xe_bo_put(bo);
65 	return ret;
66 }
67 
68 struct xe_bo *intel_fb_bo_lookup_valid_bo(struct drm_i915_private *i915,
69 					  struct drm_file *filp,
70 					  const struct drm_mode_fb_cmd2 *mode_cmd)
71 {
72 	struct drm_i915_gem_object *bo;
73 	struct drm_gem_object *gem = drm_gem_object_lookup(filp, mode_cmd->handles[0]);
74 
75 	if (!gem)
76 		return ERR_PTR(-ENOENT);
77 
78 	bo = gem_to_xe_bo(gem);
79 	/* Require vram placement or dma-buf import */
80 	if (IS_DGFX(i915) &&
81 	    !xe_bo_can_migrate(gem_to_xe_bo(gem), XE_PL_VRAM0) &&
82 	    bo->ttm.type != ttm_bo_type_sg) {
83 		drm_gem_object_put(gem);
84 		return ERR_PTR(-EREMOTE);
85 	}
86 
87 	return bo;
88 }
89