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