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