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