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