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