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