xref: /linux/drivers/gpu/drm/i915/display/intel_fb_bo.c (revision e77a8005748547fb1f10645097f13ccdd804d7e5)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright © 2021 Intel Corporation
4  */
5 
6 #include <drm/drm_framebuffer.h>
7 
8 #include "gem/i915_gem_object.h"
9 
10 #include "i915_drv.h"
11 #include "intel_fb.h"
12 #include "intel_fb_bo.h"
13 
14 void intel_fb_bo_framebuffer_fini(struct drm_gem_object *obj)
15 {
16 	/* Nothing to do for i915 */
17 }
18 
19 int intel_fb_bo_framebuffer_init(struct intel_framebuffer *intel_fb,
20 				 struct drm_gem_object *_obj,
21 				 struct drm_mode_fb_cmd2 *mode_cmd)
22 {
23 	struct drm_i915_gem_object *obj = to_intel_bo(_obj);
24 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
25 	unsigned int tiling, stride;
26 
27 	i915_gem_object_lock(obj, NULL);
28 	tiling = i915_gem_object_get_tiling(obj);
29 	stride = i915_gem_object_get_stride(obj);
30 	i915_gem_object_unlock(obj);
31 
32 	if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
33 		/*
34 		 * If there's a fence, enforce that
35 		 * the fb modifier and tiling mode match.
36 		 */
37 		if (tiling != I915_TILING_NONE &&
38 		    tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
39 			drm_dbg_kms(&i915->drm,
40 				    "tiling_mode doesn't match fb modifier\n");
41 			return -EINVAL;
42 		}
43 	} else {
44 		if (tiling == I915_TILING_X) {
45 			mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
46 		} else if (tiling == I915_TILING_Y) {
47 			drm_dbg_kms(&i915->drm,
48 				    "No Y tiling for legacy addfb\n");
49 			return -EINVAL;
50 		}
51 	}
52 
53 	/*
54 	 * gen2/3 display engine uses the fence if present,
55 	 * so the tiling mode must match the fb modifier exactly.
56 	 */
57 	if (DISPLAY_VER(i915) < 4 &&
58 	    tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
59 		drm_dbg_kms(&i915->drm,
60 			    "tiling_mode must match fb modifier exactly on gen2/3\n");
61 		return -EINVAL;
62 	}
63 
64 	/*
65 	 * If there's a fence, enforce that
66 	 * the fb pitch and fence stride match.
67 	 */
68 	if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
69 		drm_dbg_kms(&i915->drm,
70 			    "pitch (%d) must match tiling stride (%d)\n",
71 			    mode_cmd->pitches[0], stride);
72 		return -EINVAL;
73 	}
74 
75 	return 0;
76 }
77 
78 struct drm_gem_object *
79 intel_fb_bo_lookup_valid_bo(struct drm_i915_private *i915,
80 			    struct drm_file *filp,
81 			    const struct drm_mode_fb_cmd2 *mode_cmd)
82 {
83 	struct drm_i915_gem_object *obj;
84 
85 	obj = i915_gem_object_lookup(filp, mode_cmd->handles[0]);
86 	if (!obj)
87 		return ERR_PTR(-ENOENT);
88 
89 	/* object is backed with LMEM for discrete */
90 	if (HAS_LMEM(i915) && !i915_gem_object_can_migrate(obj, INTEL_REGION_LMEM_0)) {
91 		/* object is "remote", not in local memory */
92 		i915_gem_object_put(obj);
93 		drm_dbg_kms(&i915->drm, "framebuffer must reside in local memory\n");
94 		return ERR_PTR(-EREMOTE);
95 	}
96 
97 	return intel_bo_to_drm_bo(obj);
98 }
99