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