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