xref: /linux/drivers/gpu/drm/xe/display/xe_plane_initial.c (revision be239684b18e1cdcafcf8c7face4a2f562c745ad)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2021 Intel Corporation
4  */
5 
6 /* for ioread64 */
7 #include <linux/io-64-nonatomic-lo-hi.h>
8 
9 #include "xe_ggtt.h"
10 
11 #include "i915_drv.h"
12 #include "intel_atomic_plane.h"
13 #include "intel_crtc.h"
14 #include "intel_display.h"
15 #include "intel_display_types.h"
16 #include "intel_fb.h"
17 #include "intel_fb_pin.h"
18 #include "intel_frontbuffer.h"
19 #include "intel_plane_initial.h"
20 
21 static bool
22 intel_reuse_initial_plane_obj(struct intel_crtc *this,
23 			      const struct intel_initial_plane_config plane_configs[],
24 			      struct drm_framebuffer **fb)
25 {
26 	struct drm_i915_private *i915 = to_i915(this->base.dev);
27 	struct intel_crtc *crtc;
28 
29 	for_each_intel_crtc(&i915->drm, crtc) {
30 		struct intel_plane *plane =
31 			to_intel_plane(crtc->base.primary);
32 		const struct intel_plane_state *plane_state =
33 			to_intel_plane_state(plane->base.state);
34 		const struct intel_crtc_state *crtc_state =
35 			to_intel_crtc_state(crtc->base.state);
36 
37 		if (!crtc_state->uapi.active)
38 			continue;
39 
40 		if (!plane_state->ggtt_vma)
41 			continue;
42 
43 		if (plane_configs[this->pipe].base == plane_configs[crtc->pipe].base) {
44 			*fb = plane_state->hw.fb;
45 			return true;
46 		}
47 	}
48 
49 	return false;
50 }
51 
52 static struct xe_bo *
53 initial_plane_bo(struct xe_device *xe,
54 		 struct intel_initial_plane_config *plane_config)
55 {
56 	struct xe_tile *tile0 = xe_device_get_root_tile(xe);
57 	struct xe_bo *bo;
58 	resource_size_t phys_base;
59 	u32 base, size, flags;
60 	u64 page_size = xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K ? SZ_64K : SZ_4K;
61 
62 	if (plane_config->size == 0)
63 		return NULL;
64 
65 	flags = XE_BO_CREATE_PINNED_BIT | XE_BO_SCANOUT_BIT | XE_BO_CREATE_GGTT_BIT;
66 
67 	base = round_down(plane_config->base, page_size);
68 	if (IS_DGFX(xe)) {
69 		u64 __iomem *gte = tile0->mem.ggtt->gsm;
70 		u64 pte;
71 
72 		gte += base / XE_PAGE_SIZE;
73 
74 		pte = ioread64(gte);
75 		if (!(pte & XE_GGTT_PTE_DM)) {
76 			drm_err(&xe->drm,
77 				"Initial plane programming missing DM bit\n");
78 			return NULL;
79 		}
80 
81 		phys_base = pte & ~(page_size - 1);
82 		flags |= XE_BO_CREATE_VRAM0_BIT;
83 
84 		/*
85 		 * We don't currently expect this to ever be placed in the
86 		 * stolen portion.
87 		 */
88 		if (phys_base >= tile0->mem.vram.usable_size) {
89 			drm_err(&xe->drm,
90 				"Initial plane programming using invalid range, phys_base=%pa\n",
91 				&phys_base);
92 			return NULL;
93 		}
94 
95 		drm_dbg(&xe->drm,
96 			"Using phys_base=%pa, based on initial plane programming\n",
97 			&phys_base);
98 	} else {
99 		struct ttm_resource_manager *stolen = ttm_manager_type(&xe->ttm, XE_PL_STOLEN);
100 
101 		if (!stolen)
102 			return NULL;
103 		phys_base = base;
104 		flags |= XE_BO_CREATE_STOLEN_BIT;
105 
106 		/*
107 		 * If the FB is too big, just don't use it since fbdev is not very
108 		 * important and we should probably use that space with FBC or other
109 		 * features.
110 		 */
111 		if (IS_ENABLED(CONFIG_FRAMEBUFFER_CONSOLE) &&
112 		    plane_config->size * 2 >> PAGE_SHIFT >= stolen->size)
113 			return NULL;
114 	}
115 
116 	size = round_up(plane_config->base + plane_config->size,
117 			page_size);
118 	size -= base;
119 
120 	bo = xe_bo_create_pin_map_at(xe, tile0, NULL, size, phys_base,
121 				     ttm_bo_type_kernel, flags);
122 	if (IS_ERR(bo)) {
123 		drm_dbg(&xe->drm,
124 			"Failed to create bo phys_base=%pa size %u with flags %x: %li\n",
125 			&phys_base, size, flags, PTR_ERR(bo));
126 		return NULL;
127 	}
128 
129 	return bo;
130 }
131 
132 static bool
133 intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
134 			      struct intel_initial_plane_config *plane_config)
135 {
136 	struct drm_device *dev = crtc->base.dev;
137 	struct drm_i915_private *dev_priv = to_i915(dev);
138 	struct drm_mode_fb_cmd2 mode_cmd = { 0 };
139 	struct drm_framebuffer *fb = &plane_config->fb->base;
140 	struct xe_bo *bo;
141 
142 	switch (fb->modifier) {
143 	case DRM_FORMAT_MOD_LINEAR:
144 	case I915_FORMAT_MOD_X_TILED:
145 	case I915_FORMAT_MOD_Y_TILED:
146 	case I915_FORMAT_MOD_4_TILED:
147 		break;
148 	default:
149 		drm_dbg(&dev_priv->drm,
150 			"Unsupported modifier for initial FB: 0x%llx\n",
151 			fb->modifier);
152 		return false;
153 	}
154 
155 	mode_cmd.pixel_format = fb->format->format;
156 	mode_cmd.width = fb->width;
157 	mode_cmd.height = fb->height;
158 	mode_cmd.pitches[0] = fb->pitches[0];
159 	mode_cmd.modifier[0] = fb->modifier;
160 	mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
161 
162 	bo = initial_plane_bo(dev_priv, plane_config);
163 	if (!bo)
164 		return false;
165 
166 	if (intel_framebuffer_init(to_intel_framebuffer(fb),
167 				   bo, &mode_cmd)) {
168 		drm_dbg_kms(&dev_priv->drm, "intel fb init failed\n");
169 		goto err_bo;
170 	}
171 	/* Reference handed over to fb */
172 	xe_bo_put(bo);
173 
174 	return true;
175 
176 err_bo:
177 	xe_bo_unpin_map_no_vm(bo);
178 	return false;
179 }
180 
181 static void
182 intel_find_initial_plane_obj(struct intel_crtc *crtc,
183 			     struct intel_initial_plane_config plane_configs[])
184 {
185 	struct intel_initial_plane_config *plane_config =
186 		&plane_configs[crtc->pipe];
187 	struct intel_plane *plane =
188 		to_intel_plane(crtc->base.primary);
189 	struct intel_plane_state *plane_state =
190 		to_intel_plane_state(plane->base.state);
191 	struct intel_crtc_state *crtc_state =
192 		to_intel_crtc_state(crtc->base.state);
193 	struct drm_framebuffer *fb;
194 	struct i915_vma *vma;
195 
196 	/*
197 	 * TODO:
198 	 *   Disable planes if get_initial_plane_config() failed.
199 	 *   Make sure things work if the surface base is not page aligned.
200 	 */
201 	if (!plane_config->fb)
202 		return;
203 
204 	if (intel_alloc_initial_plane_obj(crtc, plane_config))
205 		fb = &plane_config->fb->base;
206 	else if (!intel_reuse_initial_plane_obj(crtc, plane_configs, &fb))
207 		goto nofb;
208 
209 	plane_state->uapi.rotation = plane_config->rotation;
210 	intel_fb_fill_view(to_intel_framebuffer(fb),
211 			   plane_state->uapi.rotation, &plane_state->view);
212 
213 	vma = intel_pin_and_fence_fb_obj(fb, false, &plane_state->view.gtt,
214 					 false, &plane_state->flags);
215 	if (IS_ERR(vma))
216 		goto nofb;
217 
218 	plane_state->ggtt_vma = vma;
219 	plane_state->uapi.src_x = 0;
220 	plane_state->uapi.src_y = 0;
221 	plane_state->uapi.src_w = fb->width << 16;
222 	plane_state->uapi.src_h = fb->height << 16;
223 
224 	plane_state->uapi.crtc_x = 0;
225 	plane_state->uapi.crtc_y = 0;
226 	plane_state->uapi.crtc_w = fb->width;
227 	plane_state->uapi.crtc_h = fb->height;
228 
229 	plane_state->uapi.fb = fb;
230 	drm_framebuffer_get(fb);
231 
232 	plane_state->uapi.crtc = &crtc->base;
233 	intel_plane_copy_uapi_to_hw_state(plane_state, plane_state, crtc);
234 
235 	atomic_or(plane->frontbuffer_bit, &to_intel_frontbuffer(fb)->bits);
236 
237 	plane_config->vma = vma;
238 
239 	/*
240 	 * Flip to the newly created mapping ASAP, so we can re-use the
241 	 * first part of GGTT for WOPCM, prevent flickering, and prevent
242 	 * the lookup of sysmem scratch pages.
243 	 */
244 	plane->check_plane(crtc_state, plane_state);
245 	plane->async_flip(plane, crtc_state, plane_state, true);
246 	return;
247 
248 nofb:
249 	/*
250 	 * We've failed to reconstruct the BIOS FB.  Current display state
251 	 * indicates that the primary plane is visible, but has a NULL FB,
252 	 * which will lead to problems later if we don't fix it up.  The
253 	 * simplest solution is to just disable the primary plane now and
254 	 * pretend the BIOS never had it enabled.
255 	 */
256 	intel_plane_disable_noatomic(crtc, plane);
257 }
258 
259 static void plane_config_fini(struct intel_initial_plane_config *plane_config)
260 {
261 	if (plane_config->fb) {
262 		struct drm_framebuffer *fb = &plane_config->fb->base;
263 
264 		/* We may only have the stub and not a full framebuffer */
265 		if (drm_framebuffer_read_refcount(fb))
266 			drm_framebuffer_put(fb);
267 		else
268 			kfree(fb);
269 	}
270 }
271 
272 void intel_initial_plane_config(struct drm_i915_private *i915)
273 {
274 	struct intel_initial_plane_config plane_configs[I915_MAX_PIPES] = {};
275 	struct intel_crtc *crtc;
276 
277 	for_each_intel_crtc(&i915->drm, crtc) {
278 		struct intel_initial_plane_config *plane_config =
279 			&plane_configs[crtc->pipe];
280 
281 		if (!to_intel_crtc_state(crtc->base.state)->uapi.active)
282 			continue;
283 
284 		/*
285 		 * Note that reserving the BIOS fb up front prevents us
286 		 * from stuffing other stolen allocations like the ring
287 		 * on top.  This prevents some ugliness at boot time, and
288 		 * can even allow for smooth boot transitions if the BIOS
289 		 * fb is large enough for the active pipe configuration.
290 		 */
291 		i915->display.funcs.display->get_initial_plane_config(crtc, plane_config);
292 
293 		/*
294 		 * If the fb is shared between multiple heads, we'll
295 		 * just get the first one.
296 		 */
297 		intel_find_initial_plane_obj(crtc, plane_configs);
298 
299 		if (i915->display.funcs.display->fixup_initial_plane_config(crtc, plane_config))
300 			intel_crtc_wait_for_next_vblank(crtc);
301 
302 		plane_config_fini(plane_config);
303 	}
304 }
305