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