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