1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright © 2021 Intel Corporation
4 */
5
6 #include "gem/i915_gem_lmem.h"
7 #include "gem/i915_gem_region.h"
8 #include "i915_drv.h"
9 #include "intel_atomic_plane.h"
10 #include "intel_crtc.h"
11 #include "intel_display.h"
12 #include "intel_display_types.h"
13 #include "intel_fb.h"
14 #include "intel_frontbuffer.h"
15 #include "intel_plane_initial.h"
16
intel_plane_initial_vblank_wait(struct intel_crtc * crtc)17 void intel_plane_initial_vblank_wait(struct intel_crtc *crtc)
18 {
19 intel_crtc_wait_for_next_vblank(crtc);
20 }
21
22 static bool
intel_reuse_initial_plane_obj(struct intel_crtc * this,const struct intel_initial_plane_config plane_configs[],struct drm_framebuffer ** fb,struct i915_vma ** vma)23 intel_reuse_initial_plane_obj(struct intel_crtc *this,
24 const struct intel_initial_plane_config plane_configs[],
25 struct drm_framebuffer **fb,
26 struct i915_vma **vma)
27 {
28 struct intel_display *display = to_intel_display(this);
29 struct intel_crtc *crtc;
30
31 for_each_intel_crtc(display->drm, crtc) {
32 struct intel_plane *plane =
33 to_intel_plane(crtc->base.primary);
34 const struct intel_plane_state *plane_state =
35 to_intel_plane_state(plane->base.state);
36 const struct intel_crtc_state *crtc_state =
37 to_intel_crtc_state(crtc->base.state);
38
39 if (!crtc_state->uapi.active)
40 continue;
41
42 if (!plane_state->ggtt_vma)
43 continue;
44
45 if (plane_configs[this->pipe].base == plane_configs[crtc->pipe].base) {
46 *fb = plane_state->hw.fb;
47 *vma = plane_state->ggtt_vma;
48 return true;
49 }
50 }
51
52 return false;
53 }
54
55 static bool
initial_plane_phys_lmem(struct intel_display * display,struct intel_initial_plane_config * plane_config)56 initial_plane_phys_lmem(struct intel_display *display,
57 struct intel_initial_plane_config *plane_config)
58 {
59 struct drm_i915_private *i915 = to_i915(display->drm);
60 gen8_pte_t __iomem *gte = to_gt(i915)->ggtt->gsm;
61 struct intel_memory_region *mem;
62 dma_addr_t dma_addr;
63 gen8_pte_t pte;
64 u32 base;
65
66 base = round_down(plane_config->base, I915_GTT_MIN_ALIGNMENT);
67
68 gte += base / I915_GTT_PAGE_SIZE;
69
70 pte = ioread64(gte);
71 if (!(pte & GEN12_GGTT_PTE_LM)) {
72 drm_err(display->drm,
73 "Initial plane programming missing PTE_LM bit\n");
74 return false;
75 }
76
77 dma_addr = pte & GEN12_GGTT_PTE_ADDR_MASK;
78
79 if (IS_DGFX(i915))
80 mem = i915->mm.regions[INTEL_REGION_LMEM_0];
81 else
82 mem = i915->mm.stolen_region;
83 if (!mem) {
84 drm_dbg_kms(display->drm,
85 "Initial plane memory region not initialized\n");
86 return false;
87 }
88
89 /*
90 * On lmem we don't currently expect this to
91 * ever be placed in the stolen portion.
92 */
93 if (dma_addr < mem->region.start || dma_addr > mem->region.end) {
94 drm_err(display->drm,
95 "Initial plane programming using invalid range, dma_addr=%pa (%s [%pa-%pa])\n",
96 &dma_addr, mem->region.name, &mem->region.start, &mem->region.end);
97 return false;
98 }
99
100 drm_dbg(display->drm,
101 "Using dma_addr=%pa, based on initial plane programming\n",
102 &dma_addr);
103
104 plane_config->phys_base = dma_addr - mem->region.start;
105 plane_config->mem = mem;
106
107 return true;
108 }
109
110 static bool
initial_plane_phys_smem(struct intel_display * display,struct intel_initial_plane_config * plane_config)111 initial_plane_phys_smem(struct intel_display *display,
112 struct intel_initial_plane_config *plane_config)
113 {
114 struct drm_i915_private *i915 = to_i915(display->drm);
115 struct intel_memory_region *mem;
116 u32 base;
117
118 base = round_down(plane_config->base, I915_GTT_MIN_ALIGNMENT);
119
120 mem = i915->mm.stolen_region;
121 if (!mem) {
122 drm_dbg_kms(display->drm,
123 "Initial plane memory region not initialized\n");
124 return false;
125 }
126
127 /* FIXME get and validate the dma_addr from the PTE */
128 plane_config->phys_base = base;
129 plane_config->mem = mem;
130
131 return true;
132 }
133
134 static bool
initial_plane_phys(struct intel_display * display,struct intel_initial_plane_config * plane_config)135 initial_plane_phys(struct intel_display *display,
136 struct intel_initial_plane_config *plane_config)
137 {
138 struct drm_i915_private *i915 = to_i915(display->drm);
139
140 if (IS_DGFX(i915) || HAS_LMEMBAR_SMEM_STOLEN(i915))
141 return initial_plane_phys_lmem(display, plane_config);
142 else
143 return initial_plane_phys_smem(display, plane_config);
144 }
145
146 static struct i915_vma *
initial_plane_vma(struct intel_display * display,struct intel_initial_plane_config * plane_config)147 initial_plane_vma(struct intel_display *display,
148 struct intel_initial_plane_config *plane_config)
149 {
150 struct drm_i915_private *i915 = to_i915(display->drm);
151 struct intel_memory_region *mem;
152 struct drm_i915_gem_object *obj;
153 struct drm_mm_node orig_mm = {};
154 struct i915_vma *vma;
155 resource_size_t phys_base;
156 u32 base, size;
157 u64 pinctl;
158
159 if (plane_config->size == 0)
160 return NULL;
161
162 if (!initial_plane_phys(display, plane_config))
163 return NULL;
164
165 phys_base = plane_config->phys_base;
166 mem = plane_config->mem;
167
168 base = round_down(plane_config->base, I915_GTT_MIN_ALIGNMENT);
169 size = round_up(plane_config->base + plane_config->size,
170 mem->min_page_size);
171 size -= base;
172
173 /*
174 * If the FB is too big, just don't use it since fbdev is not very
175 * important and we should probably use that space with FBC or other
176 * features.
177 */
178 if (IS_ENABLED(CONFIG_FRAMEBUFFER_CONSOLE) &&
179 mem == i915->mm.stolen_region &&
180 size * 2 > i915->dsm.usable_size) {
181 drm_dbg_kms(display->drm, "Initial FB size exceeds half of stolen, discarding\n");
182 return NULL;
183 }
184
185 obj = i915_gem_object_create_region_at(mem, phys_base, size,
186 I915_BO_ALLOC_USER |
187 I915_BO_PREALLOC);
188 if (IS_ERR(obj)) {
189 drm_dbg_kms(display->drm, "Failed to preallocate initial FB in %s\n",
190 mem->region.name);
191 return NULL;
192 }
193
194 /*
195 * Mark it WT ahead of time to avoid changing the
196 * cache_level during fbdev initialization. The
197 * unbind there would get stuck waiting for rcu.
198 */
199 i915_gem_object_set_cache_coherency(obj, HAS_WT(i915) ?
200 I915_CACHE_WT : I915_CACHE_NONE);
201
202 switch (plane_config->tiling) {
203 case I915_TILING_NONE:
204 break;
205 case I915_TILING_X:
206 case I915_TILING_Y:
207 obj->tiling_and_stride =
208 plane_config->fb->base.pitches[0] |
209 plane_config->tiling;
210 break;
211 default:
212 MISSING_CASE(plane_config->tiling);
213 goto err_obj;
214 }
215
216 /*
217 * MTL GOP likes to place the framebuffer high up in ggtt,
218 * which can cause problems for ggtt_reserve_guc_top().
219 * Try to pin it to a low ggtt address instead to avoid that.
220 */
221 base = 0;
222
223 if (base != plane_config->base) {
224 struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
225 int ret;
226
227 /*
228 * Make sure the original and new locations
229 * can't overlap. That would corrupt the original
230 * PTEs which are still being used for scanout.
231 */
232 ret = i915_gem_gtt_reserve(&ggtt->vm, NULL, &orig_mm,
233 size, plane_config->base,
234 I915_COLOR_UNEVICTABLE, PIN_NOEVICT);
235 if (ret)
236 goto err_obj;
237 }
238
239 vma = i915_vma_instance(obj, &to_gt(i915)->ggtt->vm, NULL);
240 if (IS_ERR(vma))
241 goto err_obj;
242
243 retry:
244 pinctl = PIN_GLOBAL | PIN_OFFSET_FIXED | base;
245 if (!i915_gem_object_is_lmem(obj))
246 pinctl |= PIN_MAPPABLE;
247 if (i915_vma_pin(vma, 0, 0, pinctl)) {
248 if (drm_mm_node_allocated(&orig_mm)) {
249 drm_mm_remove_node(&orig_mm);
250 /*
251 * Try again, but this time pin
252 * it to its original location.
253 */
254 base = plane_config->base;
255 goto retry;
256 }
257 goto err_obj;
258 }
259
260 if (i915_gem_object_is_tiled(obj) &&
261 !i915_vma_is_map_and_fenceable(vma))
262 goto err_obj;
263
264 if (drm_mm_node_allocated(&orig_mm))
265 drm_mm_remove_node(&orig_mm);
266
267 drm_dbg_kms(display->drm,
268 "Initial plane fb bound to 0x%x in the ggtt (original 0x%x)\n",
269 i915_ggtt_offset(vma), plane_config->base);
270
271 return vma;
272
273 err_obj:
274 if (drm_mm_node_allocated(&orig_mm))
275 drm_mm_remove_node(&orig_mm);
276 i915_gem_object_put(obj);
277 return NULL;
278 }
279
280 static bool
intel_alloc_initial_plane_obj(struct intel_crtc * crtc,struct intel_initial_plane_config * plane_config)281 intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
282 struct intel_initial_plane_config *plane_config)
283 {
284 struct intel_display *display = to_intel_display(crtc);
285 struct drm_mode_fb_cmd2 mode_cmd = {};
286 struct drm_framebuffer *fb = &plane_config->fb->base;
287 struct i915_vma *vma;
288
289 switch (fb->modifier) {
290 case DRM_FORMAT_MOD_LINEAR:
291 case I915_FORMAT_MOD_X_TILED:
292 case I915_FORMAT_MOD_Y_TILED:
293 case I915_FORMAT_MOD_4_TILED:
294 break;
295 default:
296 drm_dbg(display->drm,
297 "Unsupported modifier for initial FB: 0x%llx\n",
298 fb->modifier);
299 return false;
300 }
301
302 vma = initial_plane_vma(display, plane_config);
303 if (!vma)
304 return false;
305
306 mode_cmd.pixel_format = fb->format->format;
307 mode_cmd.width = fb->width;
308 mode_cmd.height = fb->height;
309 mode_cmd.pitches[0] = fb->pitches[0];
310 mode_cmd.modifier[0] = fb->modifier;
311 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
312
313 if (intel_framebuffer_init(to_intel_framebuffer(fb),
314 intel_bo_to_drm_bo(vma->obj), &mode_cmd)) {
315 drm_dbg_kms(display->drm, "intel fb init failed\n");
316 goto err_vma;
317 }
318
319 plane_config->vma = vma;
320 return true;
321
322 err_vma:
323 i915_vma_put(vma);
324 return false;
325 }
326
327 static void
intel_find_initial_plane_obj(struct intel_crtc * crtc,struct intel_initial_plane_config plane_configs[])328 intel_find_initial_plane_obj(struct intel_crtc *crtc,
329 struct intel_initial_plane_config plane_configs[])
330 {
331 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
332 struct intel_initial_plane_config *plane_config =
333 &plane_configs[crtc->pipe];
334 struct intel_plane *plane =
335 to_intel_plane(crtc->base.primary);
336 struct intel_plane_state *plane_state =
337 to_intel_plane_state(plane->base.state);
338 struct drm_framebuffer *fb;
339 struct i915_vma *vma;
340
341 /*
342 * TODO:
343 * Disable planes if get_initial_plane_config() failed.
344 * Make sure things work if the surface base is not page aligned.
345 */
346 if (!plane_config->fb)
347 return;
348
349 if (intel_alloc_initial_plane_obj(crtc, plane_config)) {
350 fb = &plane_config->fb->base;
351 vma = plane_config->vma;
352 goto valid_fb;
353 }
354
355 /*
356 * Failed to alloc the obj, check to see if we should share
357 * an fb with another CRTC instead
358 */
359 if (intel_reuse_initial_plane_obj(crtc, plane_configs, &fb, &vma))
360 goto valid_fb;
361
362 /*
363 * We've failed to reconstruct the BIOS FB. Current display state
364 * indicates that the primary plane is visible, but has a NULL FB,
365 * which will lead to problems later if we don't fix it up. The
366 * simplest solution is to just disable the primary plane now and
367 * pretend the BIOS never had it enabled.
368 */
369 intel_plane_disable_noatomic(crtc, plane);
370
371 return;
372
373 valid_fb:
374 plane_state->uapi.rotation = plane_config->rotation;
375 intel_fb_fill_view(to_intel_framebuffer(fb),
376 plane_state->uapi.rotation, &plane_state->view);
377
378 __i915_vma_pin(vma);
379 plane_state->ggtt_vma = i915_vma_get(vma);
380 if (intel_plane_uses_fence(plane_state) &&
381 i915_vma_pin_fence(vma) == 0 && vma->fence)
382 plane_state->flags |= PLANE_HAS_FENCE;
383
384 plane_state->uapi.src_x = 0;
385 plane_state->uapi.src_y = 0;
386 plane_state->uapi.src_w = fb->width << 16;
387 plane_state->uapi.src_h = fb->height << 16;
388
389 plane_state->uapi.crtc_x = 0;
390 plane_state->uapi.crtc_y = 0;
391 plane_state->uapi.crtc_w = fb->width;
392 plane_state->uapi.crtc_h = fb->height;
393
394 if (plane_config->tiling)
395 dev_priv->preserve_bios_swizzle = true;
396
397 plane_state->uapi.fb = fb;
398 drm_framebuffer_get(fb);
399
400 plane_state->uapi.crtc = &crtc->base;
401 intel_plane_copy_uapi_to_hw_state(plane_state, plane_state, crtc);
402
403 atomic_or(plane->frontbuffer_bit, &to_intel_frontbuffer(fb)->bits);
404 }
405
plane_config_fini(struct intel_initial_plane_config * plane_config)406 static void plane_config_fini(struct intel_initial_plane_config *plane_config)
407 {
408 if (plane_config->fb) {
409 struct drm_framebuffer *fb = &plane_config->fb->base;
410
411 /* We may only have the stub and not a full framebuffer */
412 if (drm_framebuffer_read_refcount(fb))
413 drm_framebuffer_put(fb);
414 else
415 kfree(fb);
416 }
417
418 if (plane_config->vma)
419 i915_vma_put(plane_config->vma);
420 }
421
intel_initial_plane_config(struct intel_display * display)422 void intel_initial_plane_config(struct intel_display *display)
423 {
424 struct intel_initial_plane_config plane_configs[I915_MAX_PIPES] = {};
425 struct intel_crtc *crtc;
426
427 for_each_intel_crtc(display->drm, crtc) {
428 struct intel_initial_plane_config *plane_config =
429 &plane_configs[crtc->pipe];
430
431 if (!to_intel_crtc_state(crtc->base.state)->uapi.active)
432 continue;
433
434 /*
435 * Note that reserving the BIOS fb up front prevents us
436 * from stuffing other stolen allocations like the ring
437 * on top. This prevents some ugliness at boot time, and
438 * can even allow for smooth boot transitions if the BIOS
439 * fb is large enough for the active pipe configuration.
440 */
441 display->funcs.display->get_initial_plane_config(crtc, plane_config);
442
443 /*
444 * If the fb is shared between multiple heads, we'll
445 * just get the first one.
446 */
447 intel_find_initial_plane_obj(crtc, plane_configs);
448
449 if (display->funcs.display->fixup_initial_plane_config(crtc, plane_config))
450 intel_plane_initial_vblank_wait(crtc);
451
452 plane_config_fini(plane_config);
453 }
454 }
455