xref: /dragonfly/sys/dev/drm/i915/intel_fbdev.c (revision 834a1306)
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/module.h>
29 #include <linux/kernel.h>
30 #include <linux/console.h>
31 #include <linux/errno.h>
32 #include <linux/string.h>
33 #include <linux/mm.h>
34 #include <linux/tty.h>
35 #include <linux/sysrq.h>
36 #include <linux/delay.h>
37 #include <linux/fb.h>
38 #include <linux/init.h>
39 #include <linux/vga_switcheroo.h>
40 
41 #include <drm/drmP.h>
42 #include <drm/drm_crtc.h>
43 #include <drm/drm_fb_helper.h>
44 #include "intel_drv.h"
45 #include "intel_frontbuffer.h"
46 #include <drm/i915_drm.h>
47 #include "i915_drv.h"
48 
49 static int intel_fbdev_set_par(struct fb_info *info)
50 {
51 	struct drm_fb_helper *fb_helper = info->par;
52 	struct intel_fbdev *ifbdev =
53 		container_of(fb_helper, struct intel_fbdev, helper);
54 	int ret;
55 
56 	ret = drm_fb_helper_set_par(info);
57 
58 	if (ret == 0) {
59 		mutex_lock(&fb_helper->dev->struct_mutex);
60 		intel_fb_obj_invalidate(ifbdev->fb->obj, ORIGIN_GTT);
61 		mutex_unlock(&fb_helper->dev->struct_mutex);
62 	}
63 
64 	return ret;
65 }
66 
67 static int intel_fbdev_blank(int blank, struct fb_info *info)
68 {
69 	struct drm_fb_helper *fb_helper = info->par;
70 	struct intel_fbdev *ifbdev =
71 		container_of(fb_helper, struct intel_fbdev, helper);
72 	int ret;
73 
74 	ret = drm_fb_helper_blank(blank, info);
75 
76 	if (ret == 0) {
77 		mutex_lock(&fb_helper->dev->struct_mutex);
78 		intel_fb_obj_invalidate(ifbdev->fb->obj, ORIGIN_GTT);
79 		mutex_unlock(&fb_helper->dev->struct_mutex);
80 	}
81 
82 	return ret;
83 }
84 
85 #if 0
86 static int intel_fbdev_pan_display(struct fb_var_screeninfo *var,
87 				   struct fb_info *info)
88 {
89 	struct drm_fb_helper *fb_helper = info->par;
90 	struct intel_fbdev *ifbdev =
91 		container_of(fb_helper, struct intel_fbdev, helper);
92 
93 	int ret;
94 	ret = drm_fb_helper_pan_display(var, info);
95 
96 	if (ret == 0) {
97 		mutex_lock(&fb_helper->dev->struct_mutex);
98 		intel_fb_obj_invalidate(ifbdev->fb->obj, ORIGIN_GTT);
99 		mutex_unlock(&fb_helper->dev->struct_mutex);
100 	}
101 
102 	return ret;
103 }
104 #endif
105 
106 static struct fb_ops intelfb_ops = {
107 #if 0
108 	.owner = THIS_MODULE,
109 	.fb_check_var = drm_fb_helper_check_var,
110 #endif
111 	.fb_set_par = intel_fbdev_set_par,
112 #if 0
113 	.fb_fillrect = drm_fb_helper_cfb_fillrect,
114 	.fb_copyarea = drm_fb_helper_cfb_copyarea,
115 	.fb_imageblit = drm_fb_helper_cfb_imageblit,
116 	.fb_pan_display = intel_fbdev_pan_display,
117 #endif
118 	.fb_blank = intel_fbdev_blank,
119 #if 0
120 	.fb_setcmap = drm_fb_helper_setcmap,
121 #endif
122 	.fb_debug_enter = drm_fb_helper_debug_enter,
123 #if 0
124 	.fb_debug_leave = drm_fb_helper_debug_leave,
125 #endif
126 };
127 
128 static int intelfb_alloc(struct drm_fb_helper *helper,
129 			 struct drm_fb_helper_surface_size *sizes)
130 {
131 	struct intel_fbdev *ifbdev =
132 		container_of(helper, struct intel_fbdev, helper);
133 	struct drm_framebuffer *fb;
134 	struct drm_device *dev = helper->dev;
135 	struct drm_i915_private *dev_priv = to_i915(dev);
136 	struct i915_ggtt *ggtt = &dev_priv->ggtt;
137 	struct drm_mode_fb_cmd2 mode_cmd = {};
138 	struct drm_i915_gem_object *obj = NULL;
139 	int size, ret;
140 
141 	/* we don't do packed 24bpp */
142 	if (sizes->surface_bpp == 24)
143 		sizes->surface_bpp = 32;
144 
145 	mode_cmd.width = sizes->surface_width;
146 	mode_cmd.height = sizes->surface_height;
147 
148 	mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
149 				    DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
150 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
151 							  sizes->surface_depth);
152 
153 	mutex_lock(&dev->struct_mutex);
154 
155 	size = mode_cmd.pitches[0] * mode_cmd.height;
156 	size = PAGE_ALIGN(size);
157 
158 	/* If the FB is too big, just don't use it since fbdev is not very
159 	 * important and we should probably use that space with FBC or other
160 	 * features. */
161 	if (size * 2 < ggtt->stolen_usable_size)
162 		obj = i915_gem_object_create_stolen(dev, size);
163 	if (obj == NULL)
164 		obj = i915_gem_object_create(dev, size);
165 	if (IS_ERR(obj)) {
166 		DRM_ERROR("failed to allocate framebuffer\n");
167 		ret = PTR_ERR(obj);
168 		goto out;
169 	}
170 
171 	fb = __intel_framebuffer_create(dev, &mode_cmd, obj);
172 	if (IS_ERR(fb)) {
173 		i915_gem_object_put(obj);
174 		ret = PTR_ERR(fb);
175 		goto out;
176 	}
177 
178 	mutex_unlock(&dev->struct_mutex);
179 
180 	ifbdev->fb = to_intel_framebuffer(fb);
181 
182 	return 0;
183 
184 out:
185 	mutex_unlock(&dev->struct_mutex);
186 	return ret;
187 }
188 
189 static int intelfb_create(struct drm_fb_helper *helper,
190 			  struct drm_fb_helper_surface_size *sizes)
191 {
192 	struct intel_fbdev *ifbdev =
193 		container_of(helper, struct intel_fbdev, helper);
194 	struct intel_framebuffer *intel_fb = ifbdev->fb;
195 	struct drm_device *dev = helper->dev;
196 	struct drm_i915_private *dev_priv = to_i915(dev);
197 	struct i915_ggtt *ggtt = &dev_priv->ggtt;
198 	struct fb_info *info;
199 	struct drm_framebuffer *fb;
200 	struct i915_vma *vma;
201 	struct drm_i915_gem_object *obj;
202 	bool prealloc = false;
203 	void __iomem *vaddr;
204 	int ret;
205 	device_t vga_dev;
206 
207 	if (intel_fb &&
208 	    (sizes->fb_width > intel_fb->base.width ||
209 	     sizes->fb_height > intel_fb->base.height)) {
210 		DRM_DEBUG_KMS("BIOS fb too small (%dx%d), we require (%dx%d),"
211 			      " releasing it\n",
212 			      intel_fb->base.width, intel_fb->base.height,
213 			      sizes->fb_width, sizes->fb_height);
214 		drm_framebuffer_unreference(&intel_fb->base);
215 		intel_fb = ifbdev->fb = NULL;
216 	}
217 	if (!intel_fb || WARN_ON(!intel_fb->obj)) {
218 		DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
219 		ret = intelfb_alloc(helper, sizes);
220 		if (ret)
221 			return ret;
222 		intel_fb = ifbdev->fb;
223 	} else {
224 		DRM_DEBUG_KMS("re-using BIOS fb\n");
225 		prealloc = true;
226 		sizes->fb_width = intel_fb->base.width;
227 		sizes->fb_height = intel_fb->base.height;
228 	}
229 
230 	obj = intel_fb->obj;
231 
232 	mutex_lock(&dev->struct_mutex);
233 
234 	/* Pin the GGTT vma for our access via info->screen_base.
235 	 * This also validates that any existing fb inherited from the
236 	 * BIOS is suitable for own access.
237 	 */
238 	ret = intel_pin_and_fence_fb_obj(&ifbdev->fb->base, DRM_ROTATE_0);
239 	if (ret)
240 		goto out_unlock;
241 
242 	info = drm_fb_helper_alloc_fbi(helper);
243 	if (IS_ERR(info)) {
244 		DRM_ERROR("Failed to allocate fb_info\n");
245 		ret = PTR_ERR(info);
246 		goto out_unpin;
247 	}
248 
249 	info->par = helper;
250 
251 	fb = &ifbdev->fb->base;
252 
253 	ifbdev->helper.fb = fb;
254 
255 #ifdef __DragonFly__
256 	vga_dev = device_get_parent(dev->dev->bsddev);
257 	info->width = sizes->fb_width;
258 	info->height = sizes->fb_height;
259 	info->stride = fb->pitches[0];
260 	info->depth = sizes->surface_bpp;
261 	info->paddr = ggtt->mappable_base + i915_gem_obj_ggtt_offset(obj);
262 	info->is_vga_boot_display = vga_pci_is_boot_display(vga_dev);
263 	info->fbops = intelfb_ops;
264 #endif
265 
266 #if 0
267 	strcpy(info->fix.id, "inteldrmfb");
268 
269 	info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
270 	info->fbops = &intelfb_ops;
271 #endif
272 
273 	vma = i915_gem_obj_to_ggtt(obj);
274 
275 	/* setup aperture base/size for vesafb takeover */
276 #ifndef __DragonFly__
277 	info->apertures->ranges[0].base = dev->mode_config.fb_base;
278 	info->apertures->ranges[0].size = ggtt->mappable_end;
279 
280 	info->fix.smem_start = dev->mode_config.fb_base + vma->node.start;
281 	info->fix.smem_len = vma->node.size;
282 #endif
283 
284 	vaddr = i915_vma_pin_iomap(vma);
285 	if (IS_ERR(vaddr)) {
286 		DRM_ERROR("Failed to remap framebuffer into virtual memory\n");
287 		ret = PTR_ERR(vaddr);
288 		goto out_destroy_fbi;
289 	}
290 #ifdef __DragonFly__
291 	info->vaddr = (vm_offset_t)vaddr;
292 #else
293 	info->screen_base = vaddr;
294 	info->screen_size = vma->node.size;
295 
296 	/* This driver doesn't need a VT switch to restore the mode on resume */
297 	info->skip_vt_switch = true;
298 
299 	drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
300 	drm_fb_helper_fill_var(info, &ifbdev->helper, sizes->fb_width, sizes->fb_height);
301 
302 	/* If the object is shmemfs backed, it will have given us zeroed pages.
303 	 * If the object is stolen however, it will be full of whatever
304 	 * garbage was left in there.
305 	 */
306 	if (ifbdev->fb->obj->stolen && !prealloc)
307 		memset_io(info->screen_base, 0, info->screen_size);
308 
309 	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
310 #endif
311 
312 	DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08llx, bo %p\n",
313 		      fb->width, fb->height,
314 		      i915_gem_obj_ggtt_offset(obj), obj);
315 
316 	mutex_unlock(&dev->struct_mutex);
317 	vga_switcheroo_client_fb_set(dev->pdev, info);
318 	return 0;
319 
320 out_destroy_fbi:
321 	drm_fb_helper_release_fbi(helper);
322 out_unpin:
323 	intel_unpin_fb_obj(&ifbdev->fb->base, BIT(DRM_ROTATE_0));
324 out_unlock:
325 	mutex_unlock(&dev->struct_mutex);
326 	return ret;
327 }
328 
329 /** Sets the color ramps on behalf of RandR */
330 static void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
331 				    u16 blue, int regno)
332 {
333 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
334 
335 	intel_crtc->lut_r[regno] = red >> 8;
336 	intel_crtc->lut_g[regno] = green >> 8;
337 	intel_crtc->lut_b[regno] = blue >> 8;
338 }
339 
340 static void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
341 				    u16 *blue, int regno)
342 {
343 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
344 
345 	*red = intel_crtc->lut_r[regno] << 8;
346 	*green = intel_crtc->lut_g[regno] << 8;
347 	*blue = intel_crtc->lut_b[regno] << 8;
348 }
349 
350 static struct drm_fb_helper_crtc *
351 intel_fb_helper_crtc(struct drm_fb_helper *fb_helper, struct drm_crtc *crtc)
352 {
353 	int i;
354 
355 	for (i = 0; i < fb_helper->crtc_count; i++)
356 		if (fb_helper->crtc_info[i].mode_set.crtc == crtc)
357 			return &fb_helper->crtc_info[i];
358 
359 	return NULL;
360 }
361 
362 /*
363  * Try to read the BIOS display configuration and use it for the initial
364  * fb configuration.
365  *
366  * The BIOS or boot loader will generally create an initial display
367  * configuration for us that includes some set of active pipes and displays.
368  * This routine tries to figure out which pipes and connectors are active
369  * and stuffs them into the crtcs and modes array given to us by the
370  * drm_fb_helper code.
371  *
372  * The overall sequence is:
373  *   intel_fbdev_init - from driver load
374  *     intel_fbdev_init_bios - initialize the intel_fbdev using BIOS data
375  *     drm_fb_helper_init - build fb helper structs
376  *     drm_fb_helper_single_add_all_connectors - more fb helper structs
377  *   intel_fbdev_initial_config - apply the config
378  *     drm_fb_helper_initial_config - call ->probe then register_framebuffer()
379  *         drm_setup_crtcs - build crtc config for fbdev
380  *           intel_fb_initial_config - find active connectors etc
381  *         drm_fb_helper_single_fb_probe - set up fbdev
382  *           intelfb_create - re-use or alloc fb, build out fbdev structs
383  *
384  * Note that we don't make special consideration whether we could actually
385  * switch to the selected modes without a full modeset. E.g. when the display
386  * is in VGA mode we need to recalculate watermarks and set a new high-res
387  * framebuffer anyway.
388  */
389 static bool intel_fb_initial_config(struct drm_fb_helper *fb_helper,
390 				    struct drm_fb_helper_crtc **crtcs,
391 				    struct drm_display_mode **modes,
392 				    struct drm_fb_offset *offsets,
393 				    bool *enabled, int width, int height)
394 {
395 	struct drm_device *dev = fb_helper->dev;
396 	unsigned long conn_configured, mask;
397 	unsigned int count = min(fb_helper->connector_count, BITS_PER_LONG);
398 	int i, j;
399 	bool *save_enabled;
400 	bool fallback = true;
401 	int num_connectors_enabled = 0;
402 	int num_connectors_detected = 0;
403 	int pass = 0;
404 
405 	save_enabled = kcalloc(count, sizeof(bool), GFP_KERNEL);
406 	if (!save_enabled)
407 		return false;
408 
409 	memcpy(save_enabled, enabled, count);
410 	mask = BIT(count) - 1;
411 	conn_configured = 0;
412 retry:
413 	for (i = 0; i < count; i++) {
414 		struct drm_fb_helper_connector *fb_conn;
415 		struct drm_connector *connector;
416 		struct drm_encoder *encoder;
417 		struct drm_fb_helper_crtc *new_crtc;
418 		struct intel_crtc *intel_crtc;
419 
420 		fb_conn = fb_helper->connector_info[i];
421 		connector = fb_conn->connector;
422 
423 		if (conn_configured & BIT(i))
424 			continue;
425 
426 		if (pass == 0 && !connector->has_tile)
427 			continue;
428 
429 		if (connector->status == connector_status_connected)
430 			num_connectors_detected++;
431 
432 		if (!enabled[i]) {
433 			DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
434 				      connector->name);
435 			conn_configured |= BIT(i);
436 			continue;
437 		}
438 
439 		if (connector->force == DRM_FORCE_OFF) {
440 			DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n",
441 				      connector->name);
442 			enabled[i] = false;
443 			continue;
444 		}
445 
446 		encoder = connector->state->best_encoder;
447 		if (!encoder || WARN_ON(!connector->state->crtc)) {
448 			if (connector->force > DRM_FORCE_OFF)
449 				goto bail;
450 
451 			DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
452 				      connector->name);
453 			enabled[i] = false;
454 			conn_configured |= BIT(i);
455 			continue;
456 		}
457 
458 		num_connectors_enabled++;
459 
460 		intel_crtc = to_intel_crtc(connector->state->crtc);
461 		for (j = 0; j < 256; j++) {
462 			intel_crtc->lut_r[j] = j;
463 			intel_crtc->lut_g[j] = j;
464 			intel_crtc->lut_b[j] = j;
465 		}
466 
467 		new_crtc = intel_fb_helper_crtc(fb_helper,
468 						connector->state->crtc);
469 
470 		/*
471 		 * Make sure we're not trying to drive multiple connectors
472 		 * with a single CRTC, since our cloning support may not
473 		 * match the BIOS.
474 		 */
475 		for (j = 0; j < count; j++) {
476 			if (crtcs[j] == new_crtc) {
477 				DRM_DEBUG_KMS("fallback: cloned configuration\n");
478 				goto bail;
479 			}
480 		}
481 
482 		DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
483 			      connector->name);
484 
485 		/* go for command line mode first */
486 		modes[i] = drm_pick_cmdline_mode(fb_conn, width, height);
487 
488 		/* try for preferred next */
489 		if (!modes[i]) {
490 			DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n",
491 				      connector->name, connector->has_tile);
492 			modes[i] = drm_has_preferred_mode(fb_conn, width,
493 							  height);
494 		}
495 
496 		/* No preferred mode marked by the EDID? Are there any modes? */
497 		if (!modes[i] && !list_empty(&connector->modes)) {
498 			DRM_DEBUG_KMS("using first mode listed on connector %s\n",
499 				      connector->name);
500 			modes[i] = list_first_entry(&connector->modes,
501 						    struct drm_display_mode,
502 						    head);
503 		}
504 
505 		/* last resort: use current mode */
506 		if (!modes[i]) {
507 			/*
508 			 * IMPORTANT: We want to use the adjusted mode (i.e.
509 			 * after the panel fitter upscaling) as the initial
510 			 * config, not the input mode, which is what crtc->mode
511 			 * usually contains. But since our current
512 			 * code puts a mode derived from the post-pfit timings
513 			 * into crtc->mode this works out correctly.
514 			 *
515 			 * This is crtc->mode and not crtc->state->mode for the
516 			 * fastboot check to work correctly. crtc_state->mode has
517 			 * I915_MODE_FLAG_INHERITED, which we clear to force check
518 			 * state.
519 			 */
520 			DRM_DEBUG_KMS("looking for current mode on connector %s\n",
521 				      connector->name);
522 			modes[i] = &connector->state->crtc->mode;
523 		}
524 		crtcs[i] = new_crtc;
525 
526 		DRM_DEBUG_KMS("connector %s on [CRTC:%d:%s]: %dx%d%s\n",
527 			      connector->name,
528 			      connector->state->crtc->base.id,
529 			      connector->state->crtc->name,
530 			      modes[i]->hdisplay, modes[i]->vdisplay,
531 			      modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" :"");
532 
533 		fallback = false;
534 		conn_configured |= BIT(i);
535 	}
536 
537 	if ((conn_configured & mask) != mask) {
538 		pass++;
539 		goto retry;
540 	}
541 
542 	/*
543 	 * If the BIOS didn't enable everything it could, fall back to have the
544 	 * same user experiencing of lighting up as much as possible like the
545 	 * fbdev helper library.
546 	 */
547 	if (num_connectors_enabled != num_connectors_detected &&
548 	    num_connectors_enabled < INTEL_INFO(dev)->num_pipes) {
549 		DRM_DEBUG_KMS("fallback: Not all outputs enabled\n");
550 		DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled,
551 			      num_connectors_detected);
552 		fallback = true;
553 	}
554 
555 	if (fallback) {
556 bail:
557 		DRM_DEBUG_KMS("Not using firmware configuration\n");
558 		memcpy(enabled, save_enabled, count);
559 		kfree(save_enabled);
560 		return false;
561 	}
562 
563 	kfree(save_enabled);
564 	return true;
565 }
566 
567 static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
568 	.initial_config = intel_fb_initial_config,
569 	.gamma_set = intel_crtc_fb_gamma_set,
570 	.gamma_get = intel_crtc_fb_gamma_get,
571 	.fb_probe = intelfb_create,
572 };
573 
574 static void intel_fbdev_destroy(struct intel_fbdev *ifbdev)
575 {
576 	/* We rely on the object-free to release the VMA pinning for
577 	 * the info->screen_base mmaping. Leaking the VMA is simpler than
578 	 * trying to rectify all the possible error paths leading here.
579 	 */
580 
581 	drm_fb_helper_unregister_fbi(&ifbdev->helper);
582 	drm_fb_helper_release_fbi(&ifbdev->helper);
583 
584 	drm_fb_helper_fini(&ifbdev->helper);
585 
586 	if (ifbdev->fb) {
587 		mutex_lock(&ifbdev->helper.dev->struct_mutex);
588 		intel_unpin_fb_obj(&ifbdev->fb->base, BIT(DRM_ROTATE_0));
589 		mutex_unlock(&ifbdev->helper.dev->struct_mutex);
590 
591 		drm_framebuffer_remove(&ifbdev->fb->base);
592 	}
593 
594 	kfree(ifbdev);
595 }
596 
597 /*
598  * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
599  * The core display code will have read out the current plane configuration,
600  * so we use that to figure out if there's an object for us to use as the
601  * fb, and if so, we re-use it for the fbdev configuration.
602  *
603  * Note we only support a single fb shared across pipes for boot (mostly for
604  * fbcon), so we just find the biggest and use that.
605  */
606 static bool intel_fbdev_init_bios(struct drm_device *dev,
607 				 struct intel_fbdev *ifbdev)
608 {
609 	struct intel_framebuffer *fb = NULL;
610 	struct drm_crtc *crtc;
611 	struct intel_crtc *intel_crtc;
612 	unsigned int max_size = 0;
613 
614 	/* Find the largest fb */
615 	for_each_crtc(dev, crtc) {
616 		struct drm_i915_gem_object *obj =
617 			intel_fb_obj(crtc->primary->state->fb);
618 		intel_crtc = to_intel_crtc(crtc);
619 
620 		if (!crtc->state->active || !obj) {
621 			DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
622 				      pipe_name(intel_crtc->pipe));
623 			continue;
624 		}
625 
626 		if (obj->base.size > max_size) {
627 			DRM_DEBUG_KMS("found possible fb from plane %c\n",
628 				      pipe_name(intel_crtc->pipe));
629 			fb = to_intel_framebuffer(crtc->primary->state->fb);
630 			max_size = obj->base.size;
631 		}
632 	}
633 
634 	if (!fb) {
635 		DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
636 		goto out;
637 	}
638 
639 	/* Now make sure all the pipes will fit into it */
640 	for_each_crtc(dev, crtc) {
641 		unsigned int cur_size;
642 
643 		intel_crtc = to_intel_crtc(crtc);
644 
645 		if (!crtc->state->active) {
646 			DRM_DEBUG_KMS("pipe %c not active, skipping\n",
647 				      pipe_name(intel_crtc->pipe));
648 			continue;
649 		}
650 
651 		DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
652 			      pipe_name(intel_crtc->pipe));
653 
654 		/*
655 		 * See if the plane fb we found above will fit on this
656 		 * pipe.  Note we need to use the selected fb's pitch and bpp
657 		 * rather than the current pipe's, since they differ.
658 		 */
659 		cur_size = intel_crtc->config->base.adjusted_mode.crtc_hdisplay;
660 		cur_size = cur_size * fb->base.bits_per_pixel / 8;
661 		if (fb->base.pitches[0] < cur_size) {
662 			DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
663 				      pipe_name(intel_crtc->pipe),
664 				      cur_size, fb->base.pitches[0]);
665 			fb = NULL;
666 			break;
667 		}
668 
669 		cur_size = intel_crtc->config->base.adjusted_mode.crtc_vdisplay;
670 		cur_size = intel_fb_align_height(dev, cur_size,
671 						 fb->base.pixel_format,
672 						 fb->base.modifier[0]);
673 		cur_size *= fb->base.pitches[0];
674 		DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
675 			      pipe_name(intel_crtc->pipe),
676 			      intel_crtc->config->base.adjusted_mode.crtc_hdisplay,
677 			      intel_crtc->config->base.adjusted_mode.crtc_vdisplay,
678 			      fb->base.bits_per_pixel,
679 			      cur_size);
680 
681 		if (cur_size > max_size) {
682 			DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
683 				      pipe_name(intel_crtc->pipe),
684 				      cur_size, max_size);
685 			fb = NULL;
686 			break;
687 		}
688 
689 		DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
690 			      pipe_name(intel_crtc->pipe),
691 			      max_size, cur_size);
692 	}
693 
694 	if (!fb) {
695 		DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
696 		goto out;
697 	}
698 
699 	ifbdev->preferred_bpp = fb->base.bits_per_pixel;
700 	ifbdev->fb = fb;
701 
702 	drm_framebuffer_reference(&ifbdev->fb->base);
703 
704 	/* Final pass to check if any active pipes don't have fbs */
705 	for_each_crtc(dev, crtc) {
706 		intel_crtc = to_intel_crtc(crtc);
707 
708 		if (!crtc->state->active)
709 			continue;
710 
711 		WARN(!crtc->primary->fb,
712 		     "re-used BIOS config but lost an fb on crtc %d\n",
713 		     crtc->base.id);
714 	}
715 
716 
717 	DRM_DEBUG_KMS("using BIOS fb for initial console\n");
718 	return true;
719 
720 out:
721 
722 	return false;
723 }
724 
725 static void intel_fbdev_suspend_worker(struct work_struct *work)
726 {
727 	intel_fbdev_set_suspend(&container_of(work,
728 					      struct drm_i915_private,
729 					      fbdev_suspend_work)->drm,
730 				FBINFO_STATE_RUNNING,
731 				true);
732 }
733 
734 int intel_fbdev_init(struct drm_device *dev)
735 {
736 	struct intel_fbdev *ifbdev;
737 	struct drm_i915_private *dev_priv = to_i915(dev);
738 	int ret;
739 
740 	if (WARN_ON(INTEL_INFO(dev)->num_pipes == 0))
741 		return -ENODEV;
742 
743 	ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
744 	if (ifbdev == NULL)
745 		return -ENOMEM;
746 
747 	drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
748 
749 	if (!intel_fbdev_init_bios(dev, ifbdev))
750 		ifbdev->preferred_bpp = 32;
751 
752 	ret = drm_fb_helper_init(dev, &ifbdev->helper,
753 				 INTEL_INFO(dev)->num_pipes, 4);
754 	if (ret) {
755 		kfree(ifbdev);
756 		return ret;
757 	}
758 
759 	dev_priv->fbdev = ifbdev;
760 	INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker);
761 
762 	drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
763 
764 	return 0;
765 }
766 
767 static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
768 {
769 	struct intel_fbdev *ifbdev = data;
770 
771 	/* Due to peculiar init order wrt to hpd handling this is separate. */
772 	if (drm_fb_helper_initial_config(&ifbdev->helper,
773 					 ifbdev->preferred_bpp))
774 		intel_fbdev_fini(ifbdev->helper.dev);
775 }
776 
777 void intel_fbdev_initial_config_async(struct drm_device *dev)
778 {
779 	struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
780 
781 	ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev);
782 }
783 
784 static void intel_fbdev_sync(struct intel_fbdev *ifbdev)
785 {
786 	if (!ifbdev->cookie)
787 		return;
788 
789 	/* Only serialises with all preceding async calls, hence +1 */
790 	async_synchronize_cookie(ifbdev->cookie + 1);
791 	ifbdev->cookie = 0;
792 }
793 
794 void intel_fbdev_fini(struct drm_device *dev)
795 {
796 	struct drm_i915_private *dev_priv = to_i915(dev);
797 	struct intel_fbdev *ifbdev = dev_priv->fbdev;
798 
799 	if (!ifbdev)
800 		return;
801 
802 	cancel_work_sync(&dev_priv->fbdev_suspend_work);
803 #if 0
804 	if (!current_is_async())
805 #endif
806 		intel_fbdev_sync(ifbdev);
807 
808 	intel_fbdev_destroy(ifbdev);
809 	dev_priv->fbdev = NULL;
810 }
811 
812 void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
813 {
814 #if 0
815 	struct drm_i915_private *dev_priv = to_i915(dev);
816 	struct intel_fbdev *ifbdev = dev_priv->fbdev;
817 	struct fb_info *info;
818 
819 	if (!ifbdev || !ifbdev->fb)
820 		return;
821 
822 	info = ifbdev->helper.fbdev;
823 
824 	if (synchronous) {
825 		/* Flush any pending work to turn the console on, and then
826 		 * wait to turn it off. It must be synchronous as we are
827 		 * about to suspend or unload the driver.
828 		 *
829 		 * Note that from within the work-handler, we cannot flush
830 		 * ourselves, so only flush outstanding work upon suspend!
831 		 */
832 		if (state != FBINFO_STATE_RUNNING)
833 			flush_work(&dev_priv->fbdev_suspend_work);
834 		console_lock();
835 	} else {
836 		/*
837 		 * The console lock can be pretty contented on resume due
838 		 * to all the printk activity.  Try to keep it out of the hot
839 		 * path of resume if possible.
840 		 */
841 		WARN_ON(state != FBINFO_STATE_RUNNING);
842 		if (!console_trylock()) {
843 			/* Don't block our own workqueue as this can
844 			 * be run in parallel with other i915.ko tasks.
845 			 */
846 			schedule_work(&dev_priv->fbdev_suspend_work);
847 			return;
848 		}
849 	}
850 
851 	/* On resume from hibernation: If the object is shmemfs backed, it has
852 	 * been restored from swap. If the object is stolen however, it will be
853 	 * full of whatever garbage was left in there.
854 	 */
855 	if (state == FBINFO_STATE_RUNNING && ifbdev->fb->obj->stolen)
856 		memset_io(info->screen_base, 0, info->screen_size);
857 
858 	drm_fb_helper_set_suspend(&ifbdev->helper, state);
859 	console_unlock();
860 #endif
861 }
862 
863 void intel_fbdev_output_poll_changed(struct drm_device *dev)
864 {
865 	struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
866 
867 	if (ifbdev && ifbdev->fb)
868 		drm_fb_helper_hotplug_event(&ifbdev->helper);
869 }
870 
871 void intel_fbdev_restore_mode(struct drm_device *dev)
872 {
873 	struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
874 
875 	if (!ifbdev)
876 		return;
877 
878 	intel_fbdev_sync(ifbdev);
879 	if (!ifbdev->fb)
880 		return;
881 
882 #ifdef __DragonFly__
883 	/* XXX: avoid dead-locking the Xorg on exit */
884 	if (mutex_is_locked(&dev->mode_config.mutex)) {
885 		DRM_ERROR("fubar while trying to restore kms_console\n");
886 		return; /* drm_modeset_unlock_all(dev) ? */
887 	}
888 #endif
889 
890 	if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper)) {
891 		DRM_DEBUG("failed to restore crtc mode\n");
892 	} else {
893 		mutex_lock(&dev->struct_mutex);
894 		intel_fb_obj_invalidate(ifbdev->fb->obj, ORIGIN_GTT);
895 		mutex_unlock(&dev->struct_mutex);
896 	}
897 }
898