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