xref: /openbsd-src/sys/dev/pci/drm/i915/display/intel_fbdev.c (revision 68dd5bb1859285b71cb62a10bf107b8ad54064d9)
1 /*
2  * Copyright © 2007 David Airlie
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *     David Airlie
25  */
26 
27 #include <linux/async.h>
28 #include <linux/console.h>
29 #include <linux/delay.h>
30 #include <linux/errno.h>
31 #include <linux/fb.h>
32 #include <linux/init.h>
33 #include <linux/kernel.h>
34 #include <linux/mm.h>
35 #include <linux/module.h>
36 #include <linux/string.h>
37 #include <linux/sysrq.h>
38 #include <linux/tty.h>
39 #include <linux/vga_switcheroo.h>
40 
41 #include <drm/drm_crtc.h>
42 #include <drm/drm_fb_helper.h>
43 #include <drm/drm_fourcc.h>
44 #include <drm/drm_gem_framebuffer_helper.h>
45 
46 #include "gem/i915_gem_lmem.h"
47 #include "gem/i915_gem_mman.h"
48 
49 #include "i915_drv.h"
50 #include "intel_display_types.h"
51 #include "intel_fb.h"
52 #include "intel_fb_pin.h"
53 #include "intel_fbdev.h"
54 #include "intel_frontbuffer.h"
55 
56 struct intel_fbdev {
57 	struct drm_fb_helper helper;
58 	struct intel_framebuffer *fb;
59 	struct i915_vma *vma;
60 	unsigned long vma_flags;
61 	async_cookie_t cookie;
62 	int preferred_bpp;
63 
64 	/* Whether or not fbdev hpd processing is temporarily suspended */
65 	bool hpd_suspended: 1;
66 	/* Set when a hotplug was received while HPD processing was suspended */
67 	bool hpd_waiting: 1;
68 
69 	/* Protects hpd_suspended */
70 	struct rwlock hpd_lock;
71 };
72 
73 static struct intel_fbdev *to_intel_fbdev(struct drm_fb_helper *fb_helper)
74 {
75 	return container_of(fb_helper, struct intel_fbdev, helper);
76 }
77 
78 static struct intel_frontbuffer *to_frontbuffer(struct intel_fbdev *ifbdev)
79 {
80 	return ifbdev->fb->frontbuffer;
81 }
82 
83 static void intel_fbdev_invalidate(struct intel_fbdev *ifbdev)
84 {
85 	intel_frontbuffer_invalidate(to_frontbuffer(ifbdev), ORIGIN_CPU);
86 }
87 
88 FB_GEN_DEFAULT_DEFERRED_IOMEM_OPS(intel_fbdev,
89 				  drm_fb_helper_damage_range,
90 				  drm_fb_helper_damage_area)
91 
92 static int intel_fbdev_set_par(struct fb_info *info)
93 {
94 	struct intel_fbdev *ifbdev = to_intel_fbdev(info->par);
95 	int ret;
96 
97 	ret = drm_fb_helper_set_par(info);
98 	if (ret == 0)
99 		intel_fbdev_invalidate(ifbdev);
100 
101 	return ret;
102 }
103 
104 static int intel_fbdev_blank(int blank, struct fb_info *info)
105 {
106 	struct intel_fbdev *ifbdev = to_intel_fbdev(info->par);
107 	int ret;
108 
109 	ret = drm_fb_helper_blank(blank, info);
110 	if (ret == 0)
111 		intel_fbdev_invalidate(ifbdev);
112 
113 	return ret;
114 }
115 
116 static int intel_fbdev_pan_display(struct fb_var_screeninfo *var,
117 				   struct fb_info *info)
118 {
119 	struct intel_fbdev *ifbdev = to_intel_fbdev(info->par);
120 	int ret;
121 
122 	ret = drm_fb_helper_pan_display(var, info);
123 	if (ret == 0)
124 		intel_fbdev_invalidate(ifbdev);
125 
126 	return ret;
127 }
128 
129 #ifdef notyet
130 static int intel_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma)
131 {
132 	struct intel_fbdev *fbdev = to_intel_fbdev(info->par);
133 	struct drm_gem_object *bo = drm_gem_fb_get_obj(&fbdev->fb->base, 0);
134 	struct drm_i915_gem_object *obj = to_intel_bo(bo);
135 
136 	return i915_gem_fb_mmap(obj, vma);
137 }
138 #endif
139 
140 static const struct fb_ops intelfb_ops = {
141 #ifdef notyet
142 	.owner = THIS_MODULE,
143 	__FB_DEFAULT_DEFERRED_OPS_RDWR(intel_fbdev),
144 	DRM_FB_HELPER_DEFAULT_OPS,
145 #endif
146 	.fb_set_par = intel_fbdev_set_par,
147 #ifdef notyet
148 	.fb_blank = intel_fbdev_blank,
149 	.fb_pan_display = intel_fbdev_pan_display,
150 	__FB_DEFAULT_DEFERRED_OPS_DRAW(intel_fbdev),
151 	.fb_mmap = intel_fbdev_mmap,
152 #endif
153 };
154 
155 static int intelfb_alloc(struct drm_fb_helper *helper,
156 			 struct drm_fb_helper_surface_size *sizes)
157 {
158 	struct intel_fbdev *ifbdev = to_intel_fbdev(helper);
159 	struct drm_framebuffer *fb;
160 	struct drm_device *dev = helper->dev;
161 	struct drm_i915_private *dev_priv = to_i915(dev);
162 	struct drm_mode_fb_cmd2 mode_cmd = {};
163 	struct drm_i915_gem_object *obj;
164 	int size;
165 
166 	/* we don't do packed 24bpp */
167 	if (sizes->surface_bpp == 24)
168 		sizes->surface_bpp = 32;
169 
170 	mode_cmd.width = sizes->surface_width;
171 	mode_cmd.height = sizes->surface_height;
172 
173 	mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
174 				    DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
175 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
176 							  sizes->surface_depth);
177 
178 	size = mode_cmd.pitches[0] * mode_cmd.height;
179 	size = PAGE_ALIGN(size);
180 
181 	obj = ERR_PTR(-ENODEV);
182 	if (HAS_LMEM(dev_priv)) {
183 		obj = i915_gem_object_create_lmem(dev_priv, size,
184 						  I915_BO_ALLOC_CONTIGUOUS |
185 						  I915_BO_ALLOC_USER);
186 	} else {
187 		/*
188 		 * If the FB is too big, just don't use it since fbdev is not very
189 		 * important and we should probably use that space with FBC or other
190 		 * features.
191 		 *
192 		 * Also skip stolen on MTL as Wa_22018444074 mitigation.
193 		 */
194 		if (!(IS_METEORLAKE(dev_priv)) && size * 2 < dev_priv->dsm.usable_size)
195 			obj = i915_gem_object_create_stolen(dev_priv, size);
196 		if (IS_ERR(obj))
197 			obj = i915_gem_object_create_shmem(dev_priv, size);
198 	}
199 
200 	if (IS_ERR(obj)) {
201 		drm_err(&dev_priv->drm, "failed to allocate framebuffer (%pe)\n", obj);
202 		return PTR_ERR(obj);
203 	}
204 
205 	fb = intel_framebuffer_create(obj, &mode_cmd);
206 	i915_gem_object_put(obj);
207 	if (IS_ERR(fb))
208 		return PTR_ERR(fb);
209 
210 	ifbdev->fb = to_intel_framebuffer(fb);
211 	return 0;
212 }
213 
214 static int intelfb_create(struct drm_fb_helper *helper,
215 			  struct drm_fb_helper_surface_size *sizes)
216 {
217 	struct intel_fbdev *ifbdev = to_intel_fbdev(helper);
218 	struct intel_framebuffer *intel_fb = ifbdev->fb;
219 	struct drm_device *dev = helper->dev;
220 	struct drm_i915_private *dev_priv = to_i915(dev);
221 	struct pci_dev *pdev = dev_priv->drm.pdev;
222 	struct i915_ggtt *ggtt = to_gt(dev_priv)->ggtt;
223 	const struct i915_gtt_view view = {
224 		.type = I915_GTT_VIEW_NORMAL,
225 	};
226 	intel_wakeref_t wakeref;
227 	struct fb_info *info;
228 	struct i915_vma *vma;
229 	unsigned long flags = 0;
230 	bool prealloc = false;
231 	void __iomem *vaddr;
232 	struct drm_i915_gem_object *obj;
233 	struct i915_gem_ww_ctx ww;
234 	int ret;
235 
236 	mutex_lock(&ifbdev->hpd_lock);
237 	ret = ifbdev->hpd_suspended ? -EAGAIN : 0;
238 	mutex_unlock(&ifbdev->hpd_lock);
239 	if (ret)
240 		return ret;
241 
242 	if (intel_fb &&
243 	    (sizes->fb_width > intel_fb->base.width ||
244 	     sizes->fb_height > intel_fb->base.height)) {
245 		drm_dbg_kms(&dev_priv->drm,
246 			    "BIOS fb too small (%dx%d), we require (%dx%d),"
247 			    " releasing it\n",
248 			    intel_fb->base.width, intel_fb->base.height,
249 			    sizes->fb_width, sizes->fb_height);
250 		drm_framebuffer_put(&intel_fb->base);
251 		intel_fb = ifbdev->fb = NULL;
252 	}
253 	if (!intel_fb || drm_WARN_ON(dev, !intel_fb_obj(&intel_fb->base))) {
254 		drm_dbg_kms(&dev_priv->drm,
255 			    "no BIOS fb, allocating a new one\n");
256 		ret = intelfb_alloc(helper, sizes);
257 		if (ret)
258 			return ret;
259 		intel_fb = ifbdev->fb;
260 	} else {
261 		drm_dbg_kms(&dev_priv->drm, "re-using BIOS fb\n");
262 		prealloc = true;
263 		sizes->fb_width = intel_fb->base.width;
264 		sizes->fb_height = intel_fb->base.height;
265 	}
266 
267 	wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
268 
269 	/* Pin the GGTT vma for our access via info->screen_base.
270 	 * This also validates that any existing fb inherited from the
271 	 * BIOS is suitable for own access.
272 	 */
273 	vma = intel_pin_and_fence_fb_obj(&ifbdev->fb->base, false,
274 					 &view, false, &flags);
275 	if (IS_ERR(vma)) {
276 		ret = PTR_ERR(vma);
277 		goto out_unlock;
278 	}
279 
280 	info = drm_fb_helper_alloc_info(helper);
281 	if (IS_ERR(info)) {
282 		drm_err(&dev_priv->drm, "Failed to allocate fb_info (%pe)\n", info);
283 		ret = PTR_ERR(info);
284 		goto out_unpin;
285 	}
286 
287 	ifbdev->helper.fb = &ifbdev->fb->base;
288 
289 	info->fbops = &intelfb_ops;
290 
291 #ifdef __linux__
292 	obj = intel_fb_obj(&intel_fb->base);
293 	if (i915_gem_object_is_lmem(obj)) {
294 		struct intel_memory_region *mem = obj->mm.region;
295 
296 		/* Use fbdev's framebuffer from lmem for discrete */
297 		info->fix.smem_start =
298 			(unsigned long)(mem->io_start +
299 					i915_gem_object_get_dma_address(obj, 0));
300 		info->fix.smem_len = obj->base.size;
301 	} else {
302 		/* Our framebuffer is the entirety of fbdev's system memory */
303 		info->fix.smem_start =
304 			(unsigned long)(ggtt->gmadr.start + i915_ggtt_offset(vma));
305 		info->fix.smem_len = vma->size;
306 	}
307 
308 	for_i915_gem_ww(&ww, ret, false) {
309 		ret = i915_gem_object_lock(vma->obj, &ww);
310 
311 		if (ret)
312 			continue;
313 
314 		vaddr = i915_vma_pin_iomap(vma);
315 		if (IS_ERR(vaddr)) {
316 			drm_err(&dev_priv->drm,
317 				"Failed to remap framebuffer into virtual memory (%pe)\n", vaddr);
318 			ret = PTR_ERR(vaddr);
319 			continue;
320 		}
321 	}
322 
323 	if (ret)
324 		goto out_unpin;
325 
326 	info->screen_base = vaddr;
327 	info->screen_size = vma->size;
328 
329 	drm_fb_helper_fill_info(info, &ifbdev->helper, sizes);
330 
331 	/* If the object is shmemfs backed, it will have given us zeroed pages.
332 	 * If the object is stolen however, it will be full of whatever
333 	 * garbage was left in there.
334 	 */
335 	if (!i915_gem_object_is_shmem(vma->obj) && !prealloc)
336 		memset_io(info->screen_base, 0, info->screen_size);
337 
338 	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
339 #else
340 {
341 	struct drm_framebuffer *fb = ifbdev->helper.fb;
342 	struct rasops_info *ri = &dev_priv->ro;
343 	bus_space_handle_t bsh;
344 	int err;
345 
346 	vaddr = i915_vma_pin_iomap(vma);
347 	if (IS_ERR(vaddr)) {
348 		DRM_ERROR("Failed to remap framebuffer into virtual memory\n");
349 		ret = PTR_ERR(vaddr);
350 		goto out_unpin;
351 	}
352 
353 	drm_fb_helper_fill_info(info, &ifbdev->helper, sizes);
354 
355 	ri->ri_bits = vaddr;
356 	ri->ri_depth = fb->format->cpp[0] * 8;
357 	ri->ri_stride = fb->pitches[0];
358 	ri->ri_width = sizes->fb_width;
359 	ri->ri_height = sizes->fb_height;
360 
361 	switch (fb->format->format) {
362 	case DRM_FORMAT_XRGB8888:
363 		ri->ri_rnum = 8;
364 		ri->ri_rpos = 16;
365 		ri->ri_gnum = 8;
366 		ri->ri_gpos = 8;
367 		ri->ri_bnum = 8;
368 		ri->ri_bpos = 0;
369 		break;
370 	case DRM_FORMAT_RGB565:
371 		ri->ri_rnum = 5;
372 		ri->ri_rpos = 11;
373 		ri->ri_gnum = 6;
374 		ri->ri_gpos = 5;
375 		ri->ri_bnum = 5;
376 		ri->ri_bpos = 0;
377 		break;
378 	}
379 
380 	if (vma->obj->stolen && !prealloc)
381 		memset(ri->ri_bits, 0, vma->node.size);
382 }
383 #endif
384 
385 	drm_dbg_kms(&dev_priv->drm, "allocated %dx%d fb: 0x%08x\n",
386 		    ifbdev->fb->base.width, ifbdev->fb->base.height,
387 		    i915_ggtt_offset(vma));
388 	ifbdev->vma = vma;
389 	ifbdev->vma_flags = flags;
390 
391 	intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
392 	vga_switcheroo_client_fb_set(pdev, info);
393 	return 0;
394 
395 out_unpin:
396 	intel_unpin_fb_vma(vma, flags);
397 out_unlock:
398 	intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
399 	return ret;
400 }
401 
402 static int intelfb_dirty(struct drm_fb_helper *helper, struct drm_clip_rect *clip)
403 {
404 	if (!(clip->x1 < clip->x2 && clip->y1 < clip->y2))
405 		return 0;
406 
407 	if (helper->fb->funcs->dirty)
408 		return helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, clip, 1);
409 
410 	return 0;
411 }
412 
413 static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
414 	.fb_probe = intelfb_create,
415 	.fb_dirty = intelfb_dirty,
416 };
417 
418 static void intel_fbdev_destroy(struct intel_fbdev *ifbdev)
419 {
420 	/* We rely on the object-free to release the VMA pinning for
421 	 * the info->screen_base mmaping. Leaking the VMA is simpler than
422 	 * trying to rectify all the possible error paths leading here.
423 	 */
424 
425 	drm_fb_helper_fini(&ifbdev->helper);
426 
427 	if (ifbdev->vma)
428 		intel_unpin_fb_vma(ifbdev->vma, ifbdev->vma_flags);
429 
430 	if (ifbdev->fb)
431 		drm_framebuffer_remove(&ifbdev->fb->base);
432 
433 	drm_fb_helper_unprepare(&ifbdev->helper);
434 	kfree(ifbdev);
435 }
436 
437 /*
438  * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
439  * The core display code will have read out the current plane configuration,
440  * so we use that to figure out if there's an object for us to use as the
441  * fb, and if so, we re-use it for the fbdev configuration.
442  *
443  * Note we only support a single fb shared across pipes for boot (mostly for
444  * fbcon), so we just find the biggest and use that.
445  */
446 static bool intel_fbdev_init_bios(struct drm_device *dev,
447 				  struct intel_fbdev *ifbdev)
448 {
449 	struct drm_i915_private *i915 = to_i915(dev);
450 	struct intel_framebuffer *fb = NULL;
451 	struct intel_crtc *crtc;
452 	unsigned int max_size = 0;
453 
454 	/* Find the largest fb */
455 	for_each_intel_crtc(dev, crtc) {
456 		struct intel_crtc_state *crtc_state =
457 			to_intel_crtc_state(crtc->base.state);
458 		struct intel_plane *plane =
459 			to_intel_plane(crtc->base.primary);
460 		struct intel_plane_state *plane_state =
461 			to_intel_plane_state(plane->base.state);
462 		struct drm_i915_gem_object *obj =
463 			intel_fb_obj(plane_state->uapi.fb);
464 
465 		if (!crtc_state->uapi.active) {
466 			drm_dbg_kms(&i915->drm,
467 				    "[CRTC:%d:%s] not active, skipping\n",
468 				    crtc->base.base.id, crtc->base.name);
469 			continue;
470 		}
471 
472 		if (!obj) {
473 			drm_dbg_kms(&i915->drm,
474 				    "[PLANE:%d:%s] no fb, skipping\n",
475 				    plane->base.base.id, plane->base.name);
476 			continue;
477 		}
478 
479 		if (obj->base.size > max_size) {
480 			drm_dbg_kms(&i915->drm,
481 				    "found possible fb from [PLANE:%d:%s]\n",
482 				    plane->base.base.id, plane->base.name);
483 			fb = to_intel_framebuffer(plane_state->uapi.fb);
484 			max_size = obj->base.size;
485 		}
486 	}
487 
488 	if (!fb) {
489 		drm_dbg_kms(&i915->drm,
490 			    "no active fbs found, not using BIOS config\n");
491 		goto out;
492 	}
493 
494 	/* Now make sure all the pipes will fit into it */
495 	for_each_intel_crtc(dev, crtc) {
496 		struct intel_crtc_state *crtc_state =
497 			to_intel_crtc_state(crtc->base.state);
498 		struct intel_plane *plane =
499 			to_intel_plane(crtc->base.primary);
500 		unsigned int cur_size;
501 
502 		if (!crtc_state->uapi.active) {
503 			drm_dbg_kms(&i915->drm,
504 				    "[CRTC:%d:%s] not active, skipping\n",
505 				    crtc->base.base.id, crtc->base.name);
506 			continue;
507 		}
508 
509 		drm_dbg_kms(&i915->drm, "checking [PLANE:%d:%s] for BIOS fb\n",
510 			    plane->base.base.id, plane->base.name);
511 
512 		/*
513 		 * See if the plane fb we found above will fit on this
514 		 * pipe.  Note we need to use the selected fb's pitch and bpp
515 		 * rather than the current pipe's, since they differ.
516 		 */
517 		cur_size = crtc_state->uapi.adjusted_mode.crtc_hdisplay;
518 		cur_size = cur_size * fb->base.format->cpp[0];
519 		if (fb->base.pitches[0] < cur_size) {
520 			drm_dbg_kms(&i915->drm,
521 				    "fb not wide enough for [PLANE:%d:%s] (%d vs %d)\n",
522 				    plane->base.base.id, plane->base.name,
523 				    cur_size, fb->base.pitches[0]);
524 			fb = NULL;
525 			break;
526 		}
527 
528 		cur_size = crtc_state->uapi.adjusted_mode.crtc_vdisplay;
529 		cur_size = intel_fb_align_height(&fb->base, 0, cur_size);
530 		cur_size *= fb->base.pitches[0];
531 		drm_dbg_kms(&i915->drm,
532 			    "[CRTC:%d:%s] area: %dx%d, bpp: %d, size: %d\n",
533 			    crtc->base.base.id, crtc->base.name,
534 			    crtc_state->uapi.adjusted_mode.crtc_hdisplay,
535 			    crtc_state->uapi.adjusted_mode.crtc_vdisplay,
536 			    fb->base.format->cpp[0] * 8,
537 			    cur_size);
538 
539 		if (cur_size > max_size) {
540 			drm_dbg_kms(&i915->drm,
541 				    "fb not big enough for [PLANE:%d:%s] (%d vs %d)\n",
542 				    plane->base.base.id, plane->base.name,
543 				    cur_size, max_size);
544 			fb = NULL;
545 			break;
546 		}
547 
548 		drm_dbg_kms(&i915->drm,
549 			    "fb big enough [PLANE:%d:%s] (%d >= %d)\n",
550 			    plane->base.base.id, plane->base.name,
551 			    max_size, cur_size);
552 	}
553 
554 	if (!fb) {
555 		drm_dbg_kms(&i915->drm,
556 			    "BIOS fb not suitable for all pipes, not using\n");
557 		goto out;
558 	}
559 
560 	ifbdev->preferred_bpp = fb->base.format->cpp[0] * 8;
561 	ifbdev->fb = fb;
562 
563 	drm_framebuffer_get(&ifbdev->fb->base);
564 
565 	/* Final pass to check if any active pipes don't have fbs */
566 	for_each_intel_crtc(dev, crtc) {
567 		struct intel_crtc_state *crtc_state =
568 			to_intel_crtc_state(crtc->base.state);
569 		struct intel_plane *plane =
570 			to_intel_plane(crtc->base.primary);
571 		struct intel_plane_state *plane_state =
572 			to_intel_plane_state(plane->base.state);
573 
574 		if (!crtc_state->uapi.active)
575 			continue;
576 
577 		drm_WARN(dev, !plane_state->uapi.fb,
578 			 "re-used BIOS config but lost an fb on [PLANE:%d:%s]\n",
579 			 plane->base.base.id, plane->base.name);
580 	}
581 
582 
583 	drm_dbg_kms(&i915->drm, "using BIOS fb for initial console\n");
584 	return true;
585 
586 out:
587 
588 	return false;
589 }
590 
591 static void intel_fbdev_suspend_worker(struct work_struct *work)
592 {
593 	intel_fbdev_set_suspend(&container_of(work,
594 					      struct drm_i915_private,
595 					      display.fbdev.suspend_work)->drm,
596 				FBINFO_STATE_RUNNING,
597 				true);
598 }
599 
600 int intel_fbdev_init(struct drm_device *dev)
601 {
602 	struct drm_i915_private *dev_priv = to_i915(dev);
603 	struct intel_fbdev *ifbdev;
604 	int ret;
605 
606 	if (drm_WARN_ON(dev, !HAS_DISPLAY(dev_priv)))
607 		return -ENODEV;
608 
609 	ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
610 	if (ifbdev == NULL)
611 		return -ENOMEM;
612 
613 	rw_init(&ifbdev->hpd_lock, "hdplk");
614 	drm_fb_helper_prepare(dev, &ifbdev->helper, 32, &intel_fb_helper_funcs);
615 
616 	if (intel_fbdev_init_bios(dev, ifbdev))
617 		ifbdev->helper.preferred_bpp = ifbdev->preferred_bpp;
618 	else
619 		ifbdev->preferred_bpp = ifbdev->helper.preferred_bpp;
620 
621 	ret = drm_fb_helper_init(dev, &ifbdev->helper);
622 	if (ret) {
623 		kfree(ifbdev);
624 		return ret;
625 	}
626 
627 	dev_priv->display.fbdev.fbdev = ifbdev;
628 	INIT_WORK(&dev_priv->display.fbdev.suspend_work, intel_fbdev_suspend_worker);
629 
630 	return 0;
631 }
632 
633 static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
634 {
635 	struct intel_fbdev *ifbdev = data;
636 
637 	/* Due to peculiar init order wrt to hpd handling this is separate. */
638 	if (drm_fb_helper_initial_config(&ifbdev->helper))
639 		intel_fbdev_unregister(to_i915(ifbdev->helper.dev));
640 }
641 
642 void intel_fbdev_initial_config_async(struct drm_i915_private *dev_priv)
643 {
644 	struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev;
645 
646 	if (!ifbdev)
647 		return;
648 
649 	ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev);
650 }
651 
652 static void intel_fbdev_sync(struct intel_fbdev *ifbdev)
653 {
654 #ifdef __linux__
655 	if (!ifbdev->cookie)
656 		return;
657 
658 	/* Only serialises with all preceding async calls, hence +1 */
659 	async_synchronize_cookie(ifbdev->cookie + 1);
660 	ifbdev->cookie = 0;
661 #endif
662 }
663 
664 void intel_fbdev_unregister(struct drm_i915_private *dev_priv)
665 {
666 	struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev;
667 
668 	if (!ifbdev)
669 		return;
670 
671 	intel_fbdev_set_suspend(&dev_priv->drm, FBINFO_STATE_SUSPENDED, true);
672 
673 #ifdef __linux__
674 	if (!current_is_async())
675 		intel_fbdev_sync(ifbdev);
676 #endif
677 
678 	drm_fb_helper_unregister_info(&ifbdev->helper);
679 }
680 
681 void intel_fbdev_fini(struct drm_i915_private *dev_priv)
682 {
683 	struct intel_fbdev *ifbdev = fetch_and_zero(&dev_priv->display.fbdev.fbdev);
684 
685 	if (!ifbdev)
686 		return;
687 
688 	intel_fbdev_destroy(ifbdev);
689 }
690 
691 /* Suspends/resumes fbdev processing of incoming HPD events. When resuming HPD
692  * processing, fbdev will perform a full connector reprobe if a hotplug event
693  * was received while HPD was suspended.
694  */
695 static void intel_fbdev_hpd_set_suspend(struct drm_i915_private *i915, int state)
696 {
697 	struct intel_fbdev *ifbdev = i915->display.fbdev.fbdev;
698 	bool send_hpd = false;
699 
700 	mutex_lock(&ifbdev->hpd_lock);
701 	ifbdev->hpd_suspended = state == FBINFO_STATE_SUSPENDED;
702 	send_hpd = !ifbdev->hpd_suspended && ifbdev->hpd_waiting;
703 	ifbdev->hpd_waiting = false;
704 	mutex_unlock(&ifbdev->hpd_lock);
705 
706 	if (send_hpd) {
707 		drm_dbg_kms(&i915->drm, "Handling delayed fbcon HPD event\n");
708 		drm_fb_helper_hotplug_event(&ifbdev->helper);
709 	}
710 }
711 
712 void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
713 {
714 #ifdef __linux__
715 	struct drm_i915_private *dev_priv = to_i915(dev);
716 	struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev;
717 	struct fb_info *info;
718 
719 	if (!ifbdev)
720 		return;
721 
722 	if (drm_WARN_ON(&dev_priv->drm, !HAS_DISPLAY(dev_priv)))
723 		return;
724 
725 	if (!ifbdev->vma)
726 		goto set_suspend;
727 
728 	info = ifbdev->helper.info;
729 
730 	if (synchronous) {
731 		/* Flush any pending work to turn the console on, and then
732 		 * wait to turn it off. It must be synchronous as we are
733 		 * about to suspend or unload the driver.
734 		 *
735 		 * Note that from within the work-handler, we cannot flush
736 		 * ourselves, so only flush outstanding work upon suspend!
737 		 */
738 		if (state != FBINFO_STATE_RUNNING)
739 			flush_work(&dev_priv->display.fbdev.suspend_work);
740 
741 		console_lock();
742 	} else {
743 		/*
744 		 * The console lock can be pretty contented on resume due
745 		 * to all the printk activity.  Try to keep it out of the hot
746 		 * path of resume if possible.
747 		 */
748 		drm_WARN_ON(dev, state != FBINFO_STATE_RUNNING);
749 		if (!console_trylock()) {
750 			/* Don't block our own workqueue as this can
751 			 * be run in parallel with other i915.ko tasks.
752 			 */
753 			queue_work(dev_priv->unordered_wq,
754 				   &dev_priv->display.fbdev.suspend_work);
755 			return;
756 		}
757 	}
758 
759 	/* On resume from hibernation: If the object is shmemfs backed, it has
760 	 * been restored from swap. If the object is stolen however, it will be
761 	 * full of whatever garbage was left in there.
762 	 */
763 	if (state == FBINFO_STATE_RUNNING &&
764 	    !i915_gem_object_is_shmem(intel_fb_obj(&ifbdev->fb->base)))
765 		memset_io(info->screen_base, 0, info->screen_size);
766 
767 	drm_fb_helper_set_suspend(&ifbdev->helper, state);
768 	console_unlock();
769 
770 set_suspend:
771 	intel_fbdev_hpd_set_suspend(dev_priv, state);
772 #endif
773 }
774 
775 void intel_fbdev_output_poll_changed(struct drm_device *dev)
776 {
777 	struct intel_fbdev *ifbdev = to_i915(dev)->display.fbdev.fbdev;
778 	bool send_hpd;
779 
780 	if (!ifbdev)
781 		return;
782 
783 	intel_fbdev_sync(ifbdev);
784 
785 	mutex_lock(&ifbdev->hpd_lock);
786 	send_hpd = !ifbdev->hpd_suspended;
787 	ifbdev->hpd_waiting = true;
788 	mutex_unlock(&ifbdev->hpd_lock);
789 
790 	if (send_hpd && (ifbdev->vma || ifbdev->helper.deferred_setup))
791 		drm_fb_helper_hotplug_event(&ifbdev->helper);
792 }
793 
794 void intel_fbdev_restore_mode(struct drm_i915_private *dev_priv)
795 {
796 	struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev;
797 
798 	if (!ifbdev)
799 		return;
800 
801 	intel_fbdev_sync(ifbdev);
802 	if (!ifbdev->vma)
803 		return;
804 
805 	if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper) == 0)
806 		intel_fbdev_invalidate(ifbdev);
807 }
808 
809 struct intel_framebuffer *intel_fbdev_framebuffer(struct intel_fbdev *fbdev)
810 {
811 	if (!fbdev || !fbdev->helper.fb)
812 		return NULL;
813 
814 	return to_intel_framebuffer(fbdev->helper.fb);
815 }
816