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