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