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