xref: /dragonfly/sys/dev/drm/i915/intel_fbdev.c (revision 7c47e3df)
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/init.h>
38 #include <linux/vga_switcheroo.h>
39 
40 #include <drm/drmP.h>
41 #include <drm/drm_crtc.h>
42 #include <drm/drm_fb_helper.h>
43 #include "intel_drv.h"
44 #include "intel_frontbuffer.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 		i915_gem_object_put(obj);
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 pci_dev *pdev = dev_priv->drm.pdev;
197 	struct i915_ggtt *ggtt = &dev_priv->ggtt;
198 	struct fb_info *info;
199 	struct drm_framebuffer *fb;
200 	struct i915_vma *vma;
201 	bool prealloc = false;
202 	void __iomem *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 	mutex_lock(&dev->struct_mutex);
230 
231 	/* Pin the GGTT vma for our access via info->screen_base.
232 	 * This also validates that any existing fb inherited from the
233 	 * BIOS is suitable for own access.
234 	 */
235 	vma = intel_pin_and_fence_fb_obj(&ifbdev->fb->base, DRM_ROTATE_0);
236 	if (IS_ERR(vma)) {
237 		ret = PTR_ERR(vma);
238 		goto out_unlock;
239 	}
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 + vma->node.start;
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 	/* setup aperture base/size for vesafb takeover */
273 #ifndef __DragonFly__
274 	info->apertures->ranges[0].base = dev->mode_config.fb_base;
275 	info->apertures->ranges[0].size = ggtt->mappable_end;
276 
277 	info->fix.smem_start = dev->mode_config.fb_base + i915_ggtt_offset(vma);
278 	info->fix.smem_len = vma->node.size;
279 #endif
280 
281 	vaddr = i915_vma_pin_iomap(vma);
282 	if (IS_ERR(vaddr)) {
283 		DRM_ERROR("Failed to remap framebuffer into virtual memory\n");
284 		ret = PTR_ERR(vaddr);
285 		goto out_destroy_fbi;
286 	}
287 #ifdef __DragonFly__
288 	info->vaddr = (vm_offset_t)vaddr;
289 #else
290 	info->screen_base = vaddr;
291 	info->screen_size = vma->node.size;
292 
293 	/* This driver doesn't need a VT switch to restore the mode on resume */
294 	info->skip_vt_switch = true;
295 
296 	drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
297 	drm_fb_helper_fill_var(info, &ifbdev->helper, sizes->fb_width, sizes->fb_height);
298 
299 	/* If the object is shmemfs backed, it will have given us zeroed pages.
300 	 * If the object is stolen however, it will be full of whatever
301 	 * garbage was left in there.
302 	 */
303 	if (intel_fb->obj->stolen && !prealloc)
304 		memset_io(info->screen_base, 0, info->screen_size);
305 
306 	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
307 #endif
308 
309 	DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08x\n",
310 		      fb->width, fb->height, i915_ggtt_offset(vma));
311 	ifbdev->vma = vma;
312 
313 	mutex_unlock(&dev->struct_mutex);
314 	vga_switcheroo_client_fb_set(pdev, info);
315 	return 0;
316 
317 out_destroy_fbi:
318 	drm_fb_helper_release_fbi(helper);
319 out_unpin:
320 	intel_unpin_fb_vma(vma);
321 out_unlock:
322 	mutex_unlock(&dev->struct_mutex);
323 	return ret;
324 }
325 
326 /** Sets the color ramps on behalf of RandR */
327 static void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
328 				    u16 blue, int regno)
329 {
330 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
331 
332 	intel_crtc->lut_r[regno] = red >> 8;
333 	intel_crtc->lut_g[regno] = green >> 8;
334 	intel_crtc->lut_b[regno] = blue >> 8;
335 }
336 
337 static void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
338 				    u16 *blue, int regno)
339 {
340 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
341 
342 	*red = intel_crtc->lut_r[regno] << 8;
343 	*green = intel_crtc->lut_g[regno] << 8;
344 	*blue = intel_crtc->lut_b[regno] << 8;
345 }
346 
347 static struct drm_fb_helper_crtc *
348 intel_fb_helper_crtc(struct drm_fb_helper *fb_helper, struct drm_crtc *crtc)
349 {
350 	int i;
351 
352 	for (i = 0; i < fb_helper->crtc_count; i++)
353 		if (fb_helper->crtc_info[i].mode_set.crtc == crtc)
354 			return &fb_helper->crtc_info[i];
355 
356 	return NULL;
357 }
358 
359 /*
360  * Try to read the BIOS display configuration and use it for the initial
361  * fb configuration.
362  *
363  * The BIOS or boot loader will generally create an initial display
364  * configuration for us that includes some set of active pipes and displays.
365  * This routine tries to figure out which pipes and connectors are active
366  * and stuffs them into the crtcs and modes array given to us by the
367  * drm_fb_helper code.
368  *
369  * The overall sequence is:
370  *   intel_fbdev_init - from driver load
371  *     intel_fbdev_init_bios - initialize the intel_fbdev using BIOS data
372  *     drm_fb_helper_init - build fb helper structs
373  *     drm_fb_helper_single_add_all_connectors - more fb helper structs
374  *   intel_fbdev_initial_config - apply the config
375  *     drm_fb_helper_initial_config - call ->probe then register_framebuffer()
376  *         drm_setup_crtcs - build crtc config for fbdev
377  *           intel_fb_initial_config - find active connectors etc
378  *         drm_fb_helper_single_fb_probe - set up fbdev
379  *           intelfb_create - re-use or alloc fb, build out fbdev structs
380  *
381  * Note that we don't make special consideration whether we could actually
382  * switch to the selected modes without a full modeset. E.g. when the display
383  * is in VGA mode we need to recalculate watermarks and set a new high-res
384  * framebuffer anyway.
385  */
386 static bool intel_fb_initial_config(struct drm_fb_helper *fb_helper,
387 				    struct drm_fb_helper_crtc **crtcs,
388 				    struct drm_display_mode **modes,
389 				    struct drm_fb_offset *offsets,
390 				    bool *enabled, int width, int height)
391 {
392 	struct drm_i915_private *dev_priv = to_i915(fb_helper->dev);
393 	unsigned long conn_configured, conn_seq, mask;
394 	unsigned int count = min(fb_helper->connector_count, BITS_PER_LONG);
395 	int i, j;
396 	bool *save_enabled;
397 	bool fallback = true;
398 	int num_connectors_enabled = 0;
399 	int num_connectors_detected = 0;
400 
401 	save_enabled = kcalloc(count, sizeof(bool), GFP_KERNEL);
402 	if (!save_enabled)
403 		return false;
404 
405 	memcpy(save_enabled, enabled, count);
406 	mask = BIT(count) - 1;
407 	conn_configured = 0;
408 retry:
409 	conn_seq = conn_configured;
410 	for (i = 0; i < count; i++) {
411 		struct drm_fb_helper_connector *fb_conn;
412 		struct drm_connector *connector;
413 		struct drm_encoder *encoder;
414 		struct drm_fb_helper_crtc *new_crtc;
415 		struct intel_crtc *intel_crtc;
416 
417 		fb_conn = fb_helper->connector_info[i];
418 		connector = fb_conn->connector;
419 
420 		if (conn_configured & BIT(i))
421 			continue;
422 
423 		if (conn_seq == 0 && !connector->has_tile)
424 			continue;
425 
426 		if (connector->status == connector_status_connected)
427 			num_connectors_detected++;
428 
429 		if (!enabled[i]) {
430 			DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
431 				      connector->name);
432 			conn_configured |= BIT(i);
433 			continue;
434 		}
435 
436 		if (connector->force == DRM_FORCE_OFF) {
437 			DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n",
438 				      connector->name);
439 			enabled[i] = false;
440 			continue;
441 		}
442 
443 		encoder = connector->state->best_encoder;
444 		if (!encoder || WARN_ON(!connector->state->crtc)) {
445 			if (connector->force > DRM_FORCE_OFF)
446 				goto bail;
447 
448 			DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
449 				      connector->name);
450 			enabled[i] = false;
451 			conn_configured |= BIT(i);
452 			continue;
453 		}
454 
455 		num_connectors_enabled++;
456 
457 		intel_crtc = to_intel_crtc(connector->state->crtc);
458 		for (j = 0; j < 256; j++) {
459 			intel_crtc->lut_r[j] = j;
460 			intel_crtc->lut_g[j] = j;
461 			intel_crtc->lut_b[j] = j;
462 		}
463 
464 		new_crtc = intel_fb_helper_crtc(fb_helper,
465 						connector->state->crtc);
466 
467 		/*
468 		 * Make sure we're not trying to drive multiple connectors
469 		 * with a single CRTC, since our cloning support may not
470 		 * match the BIOS.
471 		 */
472 		for (j = 0; j < count; j++) {
473 			if (crtcs[j] == new_crtc) {
474 				DRM_DEBUG_KMS("fallback: cloned configuration\n");
475 				goto bail;
476 			}
477 		}
478 
479 		DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
480 			      connector->name);
481 
482 		/* go for command line mode first */
483 		modes[i] = drm_pick_cmdline_mode(fb_conn, width, height);
484 
485 		/* try for preferred next */
486 		if (!modes[i]) {
487 			DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n",
488 				      connector->name, connector->has_tile);
489 			modes[i] = drm_has_preferred_mode(fb_conn, width,
490 							  height);
491 		}
492 
493 		/* No preferred mode marked by the EDID? Are there any modes? */
494 		if (!modes[i] && !list_empty(&connector->modes)) {
495 			DRM_DEBUG_KMS("using first mode listed on connector %s\n",
496 				      connector->name);
497 			modes[i] = list_first_entry(&connector->modes,
498 						    struct drm_display_mode,
499 						    head);
500 		}
501 
502 		/* last resort: use current mode */
503 		if (!modes[i]) {
504 			/*
505 			 * IMPORTANT: We want to use the adjusted mode (i.e.
506 			 * after the panel fitter upscaling) as the initial
507 			 * config, not the input mode, which is what crtc->mode
508 			 * usually contains. But since our current
509 			 * code puts a mode derived from the post-pfit timings
510 			 * into crtc->mode this works out correctly.
511 			 *
512 			 * This is crtc->mode and not crtc->state->mode for the
513 			 * fastboot check to work correctly. crtc_state->mode has
514 			 * I915_MODE_FLAG_INHERITED, which we clear to force check
515 			 * state.
516 			 */
517 			DRM_DEBUG_KMS("looking for current mode on connector %s\n",
518 				      connector->name);
519 			modes[i] = &connector->state->crtc->mode;
520 		}
521 		crtcs[i] = new_crtc;
522 
523 		DRM_DEBUG_KMS("connector %s on [CRTC:%d:%s]: %dx%d%s\n",
524 			      connector->name,
525 			      connector->state->crtc->base.id,
526 			      connector->state->crtc->name,
527 			      modes[i]->hdisplay, modes[i]->vdisplay,
528 			      modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" :"");
529 
530 		fallback = false;
531 		conn_configured |= BIT(i);
532 	}
533 
534 	if ((conn_configured & mask) != mask && conn_configured != conn_seq)
535 		goto retry;
536 
537 	/*
538 	 * If the BIOS didn't enable everything it could, fall back to have the
539 	 * same user experiencing of lighting up as much as possible like the
540 	 * fbdev helper library.
541 	 */
542 	if (num_connectors_enabled != num_connectors_detected &&
543 	    num_connectors_enabled < INTEL_INFO(dev_priv)->num_pipes) {
544 		DRM_DEBUG_KMS("fallback: Not all outputs enabled\n");
545 		DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled,
546 			      num_connectors_detected);
547 		fallback = true;
548 	}
549 
550 	if (fallback) {
551 bail:
552 		DRM_DEBUG_KMS("Not using firmware configuration\n");
553 		memcpy(enabled, save_enabled, count);
554 		kfree(save_enabled);
555 		return false;
556 	}
557 
558 	kfree(save_enabled);
559 	return true;
560 }
561 
562 static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
563 	.initial_config = intel_fb_initial_config,
564 	.gamma_set = intel_crtc_fb_gamma_set,
565 	.gamma_get = intel_crtc_fb_gamma_get,
566 	.fb_probe = intelfb_create,
567 };
568 
569 static void intel_fbdev_destroy(struct intel_fbdev *ifbdev)
570 {
571 	/* We rely on the object-free to release the VMA pinning for
572 	 * the info->screen_base mmaping. Leaking the VMA is simpler than
573 	 * trying to rectify all the possible error paths leading here.
574 	 */
575 
576 	drm_fb_helper_unregister_fbi(&ifbdev->helper);
577 	drm_fb_helper_release_fbi(&ifbdev->helper);
578 
579 	drm_fb_helper_fini(&ifbdev->helper);
580 
581 	if (ifbdev->fb) {
582 		mutex_lock(&ifbdev->helper.dev->struct_mutex);
583 		intel_unpin_fb_vma(ifbdev->vma);
584 		mutex_unlock(&ifbdev->helper.dev->struct_mutex);
585 
586 		drm_framebuffer_remove(&ifbdev->fb->base);
587 	}
588 
589 	kfree(ifbdev);
590 }
591 
592 /*
593  * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
594  * The core display code will have read out the current plane configuration,
595  * so we use that to figure out if there's an object for us to use as the
596  * fb, and if so, we re-use it for the fbdev configuration.
597  *
598  * Note we only support a single fb shared across pipes for boot (mostly for
599  * fbcon), so we just find the biggest and use that.
600  */
601 static bool intel_fbdev_init_bios(struct drm_device *dev,
602 				 struct intel_fbdev *ifbdev)
603 {
604 	struct intel_framebuffer *fb = NULL;
605 	struct drm_crtc *crtc;
606 	struct intel_crtc *intel_crtc;
607 	unsigned int max_size = 0;
608 
609 	/* Find the largest fb */
610 	for_each_crtc(dev, crtc) {
611 		struct drm_i915_gem_object *obj =
612 			intel_fb_obj(crtc->primary->state->fb);
613 		intel_crtc = to_intel_crtc(crtc);
614 
615 		if (!crtc->state->active || !obj) {
616 			DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
617 				      pipe_name(intel_crtc->pipe));
618 			continue;
619 		}
620 
621 		if (obj->base.size > max_size) {
622 			DRM_DEBUG_KMS("found possible fb from plane %c\n",
623 				      pipe_name(intel_crtc->pipe));
624 			fb = to_intel_framebuffer(crtc->primary->state->fb);
625 			max_size = obj->base.size;
626 		}
627 	}
628 
629 	if (!fb) {
630 		DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
631 		goto out;
632 	}
633 
634 	/* Now make sure all the pipes will fit into it */
635 	for_each_crtc(dev, crtc) {
636 		unsigned int cur_size;
637 
638 		intel_crtc = to_intel_crtc(crtc);
639 
640 		if (!crtc->state->active) {
641 			DRM_DEBUG_KMS("pipe %c not active, skipping\n",
642 				      pipe_name(intel_crtc->pipe));
643 			continue;
644 		}
645 
646 		DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
647 			      pipe_name(intel_crtc->pipe));
648 
649 		/*
650 		 * See if the plane fb we found above will fit on this
651 		 * pipe.  Note we need to use the selected fb's pitch and bpp
652 		 * rather than the current pipe's, since they differ.
653 		 */
654 		cur_size = intel_crtc->config->base.adjusted_mode.crtc_hdisplay;
655 		cur_size = cur_size * fb->base.bits_per_pixel / 8;
656 		if (fb->base.pitches[0] < cur_size) {
657 			DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
658 				      pipe_name(intel_crtc->pipe),
659 				      cur_size, fb->base.pitches[0]);
660 			fb = NULL;
661 			break;
662 		}
663 
664 		cur_size = intel_crtc->config->base.adjusted_mode.crtc_vdisplay;
665 		cur_size = intel_fb_align_height(dev, cur_size,
666 						 fb->base.pixel_format,
667 						 fb->base.modifier);
668 		cur_size *= fb->base.pitches[0];
669 		DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
670 			      pipe_name(intel_crtc->pipe),
671 			      intel_crtc->config->base.adjusted_mode.crtc_hdisplay,
672 			      intel_crtc->config->base.adjusted_mode.crtc_vdisplay,
673 			      fb->base.bits_per_pixel,
674 			      cur_size);
675 
676 		if (cur_size > max_size) {
677 			DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
678 				      pipe_name(intel_crtc->pipe),
679 				      cur_size, max_size);
680 			fb = NULL;
681 			break;
682 		}
683 
684 		DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
685 			      pipe_name(intel_crtc->pipe),
686 			      max_size, cur_size);
687 	}
688 
689 	if (!fb) {
690 		DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
691 		goto out;
692 	}
693 
694 	ifbdev->preferred_bpp = fb->base.bits_per_pixel;
695 	ifbdev->fb = fb;
696 
697 	drm_framebuffer_reference(&ifbdev->fb->base);
698 
699 	/* Final pass to check if any active pipes don't have fbs */
700 	for_each_crtc(dev, crtc) {
701 		intel_crtc = to_intel_crtc(crtc);
702 
703 		if (!crtc->state->active)
704 			continue;
705 
706 		WARN(!crtc->primary->fb,
707 		     "re-used BIOS config but lost an fb on crtc %d\n",
708 		     crtc->base.id);
709 	}
710 
711 
712 	DRM_DEBUG_KMS("using BIOS fb for initial console\n");
713 	return true;
714 
715 out:
716 
717 	return false;
718 }
719 
720 static void intel_fbdev_suspend_worker(struct work_struct *work)
721 {
722 	intel_fbdev_set_suspend(&container_of(work,
723 					      struct drm_i915_private,
724 					      fbdev_suspend_work)->drm,
725 				FBINFO_STATE_RUNNING,
726 				true);
727 }
728 
729 int intel_fbdev_init(struct drm_device *dev)
730 {
731 	struct drm_i915_private *dev_priv = to_i915(dev);
732 	struct intel_fbdev *ifbdev;
733 	int ret;
734 
735 	if (WARN_ON(INTEL_INFO(dev_priv)->num_pipes == 0))
736 		return -ENODEV;
737 
738 	ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
739 	if (ifbdev == NULL)
740 		return -ENOMEM;
741 
742 	drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
743 
744 	if (!intel_fbdev_init_bios(dev, ifbdev))
745 		ifbdev->preferred_bpp = 32;
746 
747 	ret = drm_fb_helper_init(dev, &ifbdev->helper,
748 				 INTEL_INFO(dev_priv)->num_pipes, 4);
749 	if (ret) {
750 		kfree(ifbdev);
751 		return ret;
752 	}
753 
754 	dev_priv->fbdev = ifbdev;
755 	INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker);
756 
757 	drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
758 
759 	return 0;
760 }
761 
762 static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
763 {
764 	struct intel_fbdev *ifbdev = data;
765 
766 	/* Due to peculiar init order wrt to hpd handling this is separate. */
767 	if (drm_fb_helper_initial_config(&ifbdev->helper,
768 					 ifbdev->preferred_bpp))
769 		intel_fbdev_fini(ifbdev->helper.dev);
770 }
771 
772 void intel_fbdev_initial_config_async(struct drm_device *dev)
773 {
774 	struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
775 
776 	if (!ifbdev)
777 		return;
778 
779 	ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev);
780 }
781 
782 static void intel_fbdev_sync(struct intel_fbdev *ifbdev)
783 {
784 	if (!ifbdev->cookie)
785 		return;
786 
787 	/* Only serialises with all preceding async calls, hence +1 */
788 	async_synchronize_cookie(ifbdev->cookie + 1);
789 	ifbdev->cookie = 0;
790 }
791 
792 void intel_fbdev_fini(struct drm_device *dev)
793 {
794 	struct drm_i915_private *dev_priv = to_i915(dev);
795 	struct intel_fbdev *ifbdev = dev_priv->fbdev;
796 
797 	if (!ifbdev)
798 		return;
799 
800 	cancel_work_sync(&dev_priv->fbdev_suspend_work);
801 #if 0
802 	if (!current_is_async())
803 #endif
804 		intel_fbdev_sync(ifbdev);
805 
806 	intel_fbdev_destroy(ifbdev);
807 	dev_priv->fbdev = NULL;
808 }
809 
810 void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
811 {
812 #if 0
813 	struct drm_i915_private *dev_priv = to_i915(dev);
814 	struct intel_fbdev *ifbdev = dev_priv->fbdev;
815 	struct fb_info *info;
816 
817 	if (!ifbdev || !ifbdev->fb)
818 		return;
819 
820 	info = ifbdev->helper.fbdev;
821 
822 	if (synchronous) {
823 		/* Flush any pending work to turn the console on, and then
824 		 * wait to turn it off. It must be synchronous as we are
825 		 * about to suspend or unload the driver.
826 		 *
827 		 * Note that from within the work-handler, we cannot flush
828 		 * ourselves, so only flush outstanding work upon suspend!
829 		 */
830 		if (state != FBINFO_STATE_RUNNING)
831 			flush_work(&dev_priv->fbdev_suspend_work);
832 		console_lock();
833 	} else {
834 		/*
835 		 * The console lock can be pretty contented on resume due
836 		 * to all the printk activity.  Try to keep it out of the hot
837 		 * path of resume if possible.
838 		 */
839 		WARN_ON(state != FBINFO_STATE_RUNNING);
840 		if (!console_trylock()) {
841 			/* Don't block our own workqueue as this can
842 			 * be run in parallel with other i915.ko tasks.
843 			 */
844 			schedule_work(&dev_priv->fbdev_suspend_work);
845 			return;
846 		}
847 	}
848 
849 	/* On resume from hibernation: If the object is shmemfs backed, it has
850 	 * been restored from swap. If the object is stolen however, it will be
851 	 * full of whatever garbage was left in there.
852 	 */
853 	if (state == FBINFO_STATE_RUNNING && ifbdev->fb->obj->stolen)
854 		memset_io(info->screen_base, 0, info->screen_size);
855 
856 	drm_fb_helper_set_suspend(&ifbdev->helper, state);
857 	console_unlock();
858 #endif
859 }
860 
861 void intel_fbdev_output_poll_changed(struct drm_device *dev)
862 {
863 	struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
864 
865 	if (ifbdev && ifbdev->fb)
866 		drm_fb_helper_hotplug_event(&ifbdev->helper);
867 }
868 
869 void intel_fbdev_restore_mode(struct drm_device *dev)
870 {
871 	struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
872 
873 	if (!ifbdev)
874 		return;
875 
876 	intel_fbdev_sync(ifbdev);
877 	if (!ifbdev->fb)
878 		return;
879 
880 #ifdef __DragonFly__
881 	/* XXX: avoid dead-locking the Xorg on exit */
882 	if (mutex_is_locked(&dev->mode_config.mutex)) {
883 		DRM_ERROR("fubar while trying to restore kms_console\n");
884 		return; /* drm_modeset_unlock_all(dev) ? */
885 	}
886 #endif
887 
888 	if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper)) {
889 		DRM_DEBUG("failed to restore crtc mode\n");
890 	} else {
891 		mutex_lock(&dev->struct_mutex);
892 		intel_fb_obj_invalidate(ifbdev->fb->obj, ORIGIN_GTT);
893 		mutex_unlock(&dev->struct_mutex);
894 	}
895 }
896