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