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