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