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