xref: /dragonfly/sys/dev/drm/i915/intel_fbdev.c (revision 527b525a)
1 /*
2  * Copyright © 2007 David Airlie
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *     David Airlie
25  */
26 
27 #include <linux/async.h>
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/console.h>
31 #include <linux/errno.h>
32 #include <linux/string.h>
33 #include <linux/mm.h>
34 #include <linux/tty.h>
35 #include <linux/sysrq.h>
36 #include <linux/delay.h>
37 #include <linux/init.h>
38 #include <linux/vga_switcheroo.h>
39 
40 #include <drm/drmP.h>
41 #include <drm/drm_crtc.h>
42 #include <drm/drm_fb_helper.h>
43 #include "intel_drv.h"
44 #include "intel_frontbuffer.h"
45 #include <drm/i915_drm.h>
46 #include "i915_drv.h"
47 
48 static void intel_fbdev_invalidate(struct intel_fbdev *ifbdev)
49 {
50 	struct drm_i915_gem_object *obj = ifbdev->fb->obj;
51 	unsigned int origin = ifbdev->vma->fence ? ORIGIN_GTT : ORIGIN_CPU;
52 
53 	intel_fb_obj_invalidate(obj, origin);
54 }
55 
56 static int intel_fbdev_set_par(struct fb_info *info)
57 {
58 	struct drm_fb_helper *fb_helper = info->par;
59 	struct intel_fbdev *ifbdev =
60 		container_of(fb_helper, struct intel_fbdev, helper);
61 	int ret;
62 
63 	ret = drm_fb_helper_set_par(info);
64 	if (ret == 0)
65 		intel_fbdev_invalidate(ifbdev);
66 
67 	return ret;
68 }
69 
70 static int intel_fbdev_blank(int blank, struct fb_info *info)
71 {
72 	struct drm_fb_helper *fb_helper = info->par;
73 	struct intel_fbdev *ifbdev =
74 		container_of(fb_helper, struct intel_fbdev, helper);
75 	int ret;
76 
77 	ret = drm_fb_helper_blank(blank, info);
78 	if (ret == 0)
79 		intel_fbdev_invalidate(ifbdev);
80 
81 	return ret;
82 }
83 
84 #if 0
85 static int intel_fbdev_pan_display(struct fb_var_screeninfo *var,
86 				   struct fb_info *info)
87 {
88 	struct drm_fb_helper *fb_helper = info->par;
89 	struct intel_fbdev *ifbdev =
90 		container_of(fb_helper, struct intel_fbdev, helper);
91 	int ret;
92 
93 	ret = drm_fb_helper_pan_display(var, info);
94 	if (ret == 0)
95 		intel_fbdev_invalidate(ifbdev);
96 
97 	return ret;
98 }
99 #endif
100 
101 static struct fb_ops intelfb_ops = {
102 #if 0
103 	.owner = THIS_MODULE,
104 #endif
105 	DRM_FB_HELPER_DEFAULT_OPS,
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 };
115 
116 static int intelfb_alloc(struct drm_fb_helper *helper,
117 			 struct drm_fb_helper_surface_size *sizes)
118 {
119 	struct intel_fbdev *ifbdev =
120 		container_of(helper, struct intel_fbdev, helper);
121 	struct drm_framebuffer *fb;
122 	struct drm_device *dev = helper->dev;
123 	struct drm_i915_private *dev_priv = to_i915(dev);
124 	struct i915_ggtt *ggtt = &dev_priv->ggtt;
125 	struct drm_mode_fb_cmd2 mode_cmd = {};
126 	struct drm_i915_gem_object *obj;
127 	int size, ret;
128 
129 	/* we don't do packed 24bpp */
130 	if (sizes->surface_bpp == 24)
131 		sizes->surface_bpp = 32;
132 
133 	mode_cmd.width = sizes->surface_width;
134 	mode_cmd.height = sizes->surface_height;
135 
136 	mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
137 				    DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
138 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
139 							  sizes->surface_depth);
140 
141 	size = mode_cmd.pitches[0] * mode_cmd.height;
142 	size = PAGE_ALIGN(size);
143 
144 	/* If the FB is too big, just don't use it since fbdev is not very
145 	 * important and we should probably use that space with FBC or other
146 	 * features. */
147 	obj = NULL;
148 	if (size * 2 < ggtt->stolen_usable_size)
149 		obj = i915_gem_object_create_stolen(dev_priv, size);
150 	if (obj == NULL)
151 		obj = i915_gem_object_create(dev_priv, size);
152 	if (IS_ERR(obj)) {
153 		DRM_ERROR("failed to allocate framebuffer\n");
154 		ret = PTR_ERR(obj);
155 		goto err;
156 	}
157 
158 	fb = intel_framebuffer_create(obj, &mode_cmd);
159 	if (IS_ERR(fb)) {
160 		ret = PTR_ERR(fb);
161 		goto err_obj;
162 	}
163 
164 	ifbdev->fb = to_intel_framebuffer(fb);
165 
166 	return 0;
167 
168 err_obj:
169 	i915_gem_object_put(obj);
170 err:
171 	return ret;
172 }
173 
174 static int intelfb_create(struct drm_fb_helper *helper,
175 			  struct drm_fb_helper_surface_size *sizes)
176 {
177 	struct intel_fbdev *ifbdev =
178 		container_of(helper, struct intel_fbdev, helper);
179 	struct intel_framebuffer *intel_fb = ifbdev->fb;
180 	struct drm_device *dev = helper->dev;
181 	struct drm_i915_private *dev_priv = to_i915(dev);
182 	struct pci_dev *pdev = dev_priv->drm.pdev;
183 	struct i915_ggtt *ggtt = &dev_priv->ggtt;
184 	struct fb_info *info;
185 	struct drm_framebuffer *fb;
186 	struct i915_vma *vma;
187 	bool prealloc = false;
188 	void __iomem *vaddr;
189 	int ret;
190 	device_t vga_dev;
191 
192 	if (intel_fb &&
193 	    (sizes->fb_width > intel_fb->base.width ||
194 	     sizes->fb_height > intel_fb->base.height)) {
195 		DRM_DEBUG_KMS("BIOS fb too small (%dx%d), we require (%dx%d),"
196 			      " releasing it\n",
197 			      intel_fb->base.width, intel_fb->base.height,
198 			      sizes->fb_width, sizes->fb_height);
199 		drm_framebuffer_unreference(&intel_fb->base);
200 		intel_fb = ifbdev->fb = NULL;
201 	}
202 	if (!intel_fb || WARN_ON(!intel_fb->obj)) {
203 		DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
204 		ret = intelfb_alloc(helper, sizes);
205 		if (ret)
206 			return ret;
207 		intel_fb = ifbdev->fb;
208 	} else {
209 		DRM_DEBUG_KMS("re-using BIOS fb\n");
210 		prealloc = true;
211 		sizes->fb_width = intel_fb->base.width;
212 		sizes->fb_height = intel_fb->base.height;
213 	}
214 
215 	mutex_lock(&dev->struct_mutex);
216 
217 	/* Pin the GGTT vma for our access via info->screen_base.
218 	 * This also validates that any existing fb inherited from the
219 	 * BIOS is suitable for own access.
220 	 */
221 	vma = intel_pin_and_fence_fb_obj(&ifbdev->fb->base, DRM_ROTATE_0);
222 	if (IS_ERR(vma)) {
223 		ret = PTR_ERR(vma);
224 		goto out_unlock;
225 	}
226 
227 	info = drm_fb_helper_alloc_fbi(helper);
228 	if (IS_ERR(info)) {
229 		DRM_ERROR("Failed to allocate fb_info\n");
230 		ret = PTR_ERR(info);
231 		goto out_unpin;
232 	}
233 
234 	info->par = helper;
235 
236 	fb = &ifbdev->fb->base;
237 
238 	ifbdev->helper.fb = fb;
239 
240 #ifdef __DragonFly__
241 	vga_dev = device_get_parent(dev->dev->bsddev);
242 	info->width = sizes->fb_width;
243 	info->height = sizes->fb_height;
244 	info->stride = fb->pitches[0];
245 	info->depth = sizes->surface_bpp;
246 	info->paddr = ggtt->mappable_base + vma->node.start;
247 	info->is_vga_boot_display = vga_pci_is_boot_display(vga_dev);
248 	info->fbops = intelfb_ops;
249 
250 #else
251 	strcpy(info->fix.id, "inteldrmfb");
252 
253 	info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
254 	info->fbops = &intelfb_ops;
255 
256 	/* setup aperture base/size for vesafb takeover */
257 	info->apertures->ranges[0].base = dev->mode_config.fb_base;
258 	info->apertures->ranges[0].size = ggtt->mappable_end;
259 
260 	info->fix.smem_start = dev->mode_config.fb_base + i915_ggtt_offset(vma);
261 	info->fix.smem_len = vma->node.size;
262 #endif
263 
264 	vaddr = i915_vma_pin_iomap(vma);
265 	if (IS_ERR(vaddr)) {
266 		DRM_ERROR("Failed to remap framebuffer into virtual memory\n");
267 		ret = PTR_ERR(vaddr);
268 		goto out_unpin;
269 	}
270 #ifdef __DragonFly__
271 	info->vaddr = (vm_offset_t)vaddr;
272 #else
273 	info->screen_base = vaddr;
274 	info->screen_size = vma->node.size;
275 
276 	/* This driver doesn't need a VT switch to restore the mode on resume */
277 	info->skip_vt_switch = true;
278 
279 	drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
280 	drm_fb_helper_fill_var(info, &ifbdev->helper, sizes->fb_width, sizes->fb_height);
281 
282 	/* If the object is shmemfs backed, it will have given us zeroed pages.
283 	 * If the object is stolen however, it will be full of whatever
284 	 * garbage was left in there.
285 	 */
286 	if (intel_fb->obj->stolen && !prealloc)
287 		memset_io(info->screen_base, 0, info->screen_size);
288 #endif
289 
290 	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
291 
292 	DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08x\n",
293 		      fb->width, fb->height, i915_ggtt_offset(vma));
294 	ifbdev->vma = vma;
295 
296 	mutex_unlock(&dev->struct_mutex);
297 	vga_switcheroo_client_fb_set(pdev, info);
298 	return 0;
299 
300 out_unpin:
301 	intel_unpin_fb_vma(vma);
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_i915_private *dev_priv = to_i915(fb_helper->dev);
374 	unsigned long conn_configured, conn_seq, mask;
375 	unsigned int count = min(fb_helper->connector_count, BITS_PER_LONG);
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 
382 	save_enabled = kcalloc(count, sizeof(bool), GFP_KERNEL);
383 	if (!save_enabled)
384 		return false;
385 
386 	memcpy(save_enabled, enabled, count);
387 	mask = GENMASK(count - 1, 0);
388 	conn_configured = 0;
389 retry:
390 	conn_seq = conn_configured;
391 	for (i = 0; i < count; i++) {
392 		struct drm_fb_helper_connector *fb_conn;
393 		struct drm_connector *connector;
394 		struct drm_encoder *encoder;
395 		struct drm_fb_helper_crtc *new_crtc;
396 		struct intel_crtc *intel_crtc;
397 
398 		fb_conn = fb_helper->connector_info[i];
399 		connector = fb_conn->connector;
400 
401 		if (conn_configured & BIT(i))
402 			continue;
403 
404 		if (conn_seq == 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 |= BIT(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->state->best_encoder;
425 		if (!encoder || WARN_ON(!connector->state->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 |= BIT(i);
433 			continue;
434 		}
435 
436 		num_connectors_enabled++;
437 
438 		intel_crtc = to_intel_crtc(connector->state->crtc);
439 		for (j = 0; j < 256; j++) {
440 			intel_crtc->lut_r[j] = j;
441 			intel_crtc->lut_g[j] = j;
442 			intel_crtc->lut_b[j] = j;
443 		}
444 
445 		new_crtc = intel_fb_helper_crtc(fb_helper,
446 						connector->state->crtc);
447 
448 		/*
449 		 * Make sure we're not trying to drive multiple connectors
450 		 * with a single CRTC, since our cloning support may not
451 		 * match the BIOS.
452 		 */
453 		for (j = 0; j < count; j++) {
454 			if (crtcs[j] == new_crtc) {
455 				DRM_DEBUG_KMS("fallback: cloned configuration\n");
456 				goto bail;
457 			}
458 		}
459 
460 		DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
461 			      connector->name);
462 
463 		/* go for command line mode first */
464 		modes[i] = drm_pick_cmdline_mode(fb_conn);
465 
466 		/* try for preferred next */
467 		if (!modes[i]) {
468 			DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n",
469 				      connector->name, connector->has_tile);
470 			modes[i] = drm_has_preferred_mode(fb_conn, width,
471 							  height);
472 		}
473 
474 		/* No preferred mode marked by the EDID? Are there any modes? */
475 		if (!modes[i] && !list_empty(&connector->modes)) {
476 			DRM_DEBUG_KMS("using first mode listed on connector %s\n",
477 				      connector->name);
478 			modes[i] = list_first_entry(&connector->modes,
479 						    struct drm_display_mode,
480 						    head);
481 		}
482 
483 		/* last resort: use current mode */
484 		if (!modes[i]) {
485 			/*
486 			 * IMPORTANT: We want to use the adjusted mode (i.e.
487 			 * after the panel fitter upscaling) as the initial
488 			 * config, not the input mode, which is what crtc->mode
489 			 * usually contains. But since our current
490 			 * code puts a mode derived from the post-pfit timings
491 			 * into crtc->mode this works out correctly.
492 			 *
493 			 * This is crtc->mode and not crtc->state->mode for the
494 			 * fastboot check to work correctly. crtc_state->mode has
495 			 * I915_MODE_FLAG_INHERITED, which we clear to force check
496 			 * state.
497 			 */
498 			DRM_DEBUG_KMS("looking for current mode on connector %s\n",
499 				      connector->name);
500 			modes[i] = &connector->state->crtc->mode;
501 		}
502 		crtcs[i] = new_crtc;
503 
504 		DRM_DEBUG_KMS("connector %s on [CRTC:%d:%s]: %dx%d%s\n",
505 			      connector->name,
506 			      connector->state->crtc->base.id,
507 			      connector->state->crtc->name,
508 			      modes[i]->hdisplay, modes[i]->vdisplay,
509 			      modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" :"");
510 
511 		fallback = false;
512 		conn_configured |= BIT(i);
513 	}
514 
515 	if ((conn_configured & mask) != mask && conn_configured != conn_seq)
516 		goto retry;
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_priv)->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, count);
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 intel_fbdev *ifbdev)
551 {
552 	/* We rely on the object-free to release the VMA pinning for
553 	 * the info->screen_base mmaping. Leaking the VMA is simpler than
554 	 * trying to rectify all the possible error paths leading here.
555 	 */
556 
557 	drm_fb_helper_unregister_fbi(&ifbdev->helper);
558 
559 	drm_fb_helper_fini(&ifbdev->helper);
560 
561 	if (ifbdev->vma) {
562 		mutex_lock(&ifbdev->helper.dev->struct_mutex);
563 		intel_unpin_fb_vma(ifbdev->vma);
564 		mutex_unlock(&ifbdev->helper.dev->struct_mutex);
565 	}
566 
567 	if (ifbdev->fb)
568 		drm_framebuffer_remove(&ifbdev->fb->base);
569 
570 	kfree(ifbdev);
571 }
572 
573 /*
574  * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
575  * The core display code will have read out the current plane configuration,
576  * so we use that to figure out if there's an object for us to use as the
577  * fb, and if so, we re-use it for the fbdev configuration.
578  *
579  * Note we only support a single fb shared across pipes for boot (mostly for
580  * fbcon), so we just find the biggest and use that.
581  */
582 static bool intel_fbdev_init_bios(struct drm_device *dev,
583 				 struct intel_fbdev *ifbdev)
584 {
585 	struct intel_framebuffer *fb = NULL;
586 	struct drm_crtc *crtc;
587 	struct intel_crtc *intel_crtc;
588 	unsigned int max_size = 0;
589 
590 	/* Find the largest fb */
591 	for_each_crtc(dev, crtc) {
592 		struct drm_i915_gem_object *obj =
593 			intel_fb_obj(crtc->primary->state->fb);
594 		intel_crtc = to_intel_crtc(crtc);
595 
596 		if (!crtc->state->active || !obj) {
597 			DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
598 				      pipe_name(intel_crtc->pipe));
599 			continue;
600 		}
601 
602 		if (obj->base.size > max_size) {
603 			DRM_DEBUG_KMS("found possible fb from plane %c\n",
604 				      pipe_name(intel_crtc->pipe));
605 			fb = to_intel_framebuffer(crtc->primary->state->fb);
606 			max_size = obj->base.size;
607 		}
608 	}
609 
610 	if (!fb) {
611 		DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
612 		goto out;
613 	}
614 
615 	/* Now make sure all the pipes will fit into it */
616 	for_each_crtc(dev, crtc) {
617 		unsigned int cur_size;
618 
619 		intel_crtc = to_intel_crtc(crtc);
620 
621 		if (!crtc->state->active) {
622 			DRM_DEBUG_KMS("pipe %c not active, skipping\n",
623 				      pipe_name(intel_crtc->pipe));
624 			continue;
625 		}
626 
627 		DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
628 			      pipe_name(intel_crtc->pipe));
629 
630 		/*
631 		 * See if the plane fb we found above will fit on this
632 		 * pipe.  Note we need to use the selected fb's pitch and bpp
633 		 * rather than the current pipe's, since they differ.
634 		 */
635 		cur_size = intel_crtc->config->base.adjusted_mode.crtc_hdisplay;
636 		cur_size = cur_size * fb->base.format->cpp[0];
637 		if (fb->base.pitches[0] < cur_size) {
638 			DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
639 				      pipe_name(intel_crtc->pipe),
640 				      cur_size, fb->base.pitches[0]);
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(&fb->base, 0, cur_size);
647 		cur_size *= fb->base.pitches[0];
648 		DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
649 			      pipe_name(intel_crtc->pipe),
650 			      intel_crtc->config->base.adjusted_mode.crtc_hdisplay,
651 			      intel_crtc->config->base.adjusted_mode.crtc_vdisplay,
652 			      fb->base.format->cpp[0] * 8,
653 			      cur_size);
654 
655 		if (cur_size > max_size) {
656 			DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
657 				      pipe_name(intel_crtc->pipe),
658 				      cur_size, max_size);
659 			fb = NULL;
660 			break;
661 		}
662 
663 		DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
664 			      pipe_name(intel_crtc->pipe),
665 			      max_size, cur_size);
666 	}
667 
668 	if (!fb) {
669 		DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
670 		goto out;
671 	}
672 
673 	ifbdev->preferred_bpp = fb->base.format->cpp[0] * 8;
674 	ifbdev->fb = fb;
675 
676 	drm_framebuffer_reference(&ifbdev->fb->base);
677 
678 	/* Final pass to check if any active pipes don't have fbs */
679 	for_each_crtc(dev, crtc) {
680 		intel_crtc = to_intel_crtc(crtc);
681 
682 		if (!crtc->state->active)
683 			continue;
684 
685 		WARN(!crtc->primary->fb,
686 		     "re-used BIOS config but lost an fb on crtc %d\n",
687 		     crtc->base.id);
688 	}
689 
690 
691 	DRM_DEBUG_KMS("using BIOS fb for initial console\n");
692 	return true;
693 
694 out:
695 
696 	return false;
697 }
698 
699 static void intel_fbdev_suspend_worker(struct work_struct *work)
700 {
701 	intel_fbdev_set_suspend(&container_of(work,
702 					      struct drm_i915_private,
703 					      fbdev_suspend_work)->drm,
704 				FBINFO_STATE_RUNNING,
705 				true);
706 }
707 
708 int intel_fbdev_init(struct drm_device *dev)
709 {
710 	struct drm_i915_private *dev_priv = to_i915(dev);
711 	struct intel_fbdev *ifbdev;
712 	int ret;
713 
714 	if (WARN_ON(INTEL_INFO(dev_priv)->num_pipes == 0))
715 		return -ENODEV;
716 
717 	ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
718 	if (ifbdev == NULL)
719 		return -ENOMEM;
720 
721 	drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
722 
723 	if (!intel_fbdev_init_bios(dev, ifbdev))
724 		ifbdev->preferred_bpp = 32;
725 
726 	ret = drm_fb_helper_init(dev, &ifbdev->helper, 4);
727 	if (ret) {
728 		kfree(ifbdev);
729 		return ret;
730 	}
731 
732 	dev_priv->fbdev = ifbdev;
733 	INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker);
734 
735 	drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
736 
737 	return 0;
738 }
739 
740 static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
741 {
742 	struct intel_fbdev *ifbdev = data;
743 
744 	/* Due to peculiar init order wrt to hpd handling this is separate. */
745 	if (drm_fb_helper_initial_config(&ifbdev->helper,
746 					 ifbdev->preferred_bpp))
747 		intel_fbdev_fini(ifbdev->helper.dev);
748 }
749 
750 void intel_fbdev_initial_config_async(struct drm_device *dev)
751 {
752 	struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
753 
754 	if (!ifbdev)
755 		return;
756 
757 	ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev);
758 }
759 
760 static void intel_fbdev_sync(struct intel_fbdev *ifbdev)
761 {
762 	if (!ifbdev->cookie)
763 		return;
764 
765 	/* Only serialises with all preceding async calls, hence +1 */
766 	async_synchronize_cookie(ifbdev->cookie + 1);
767 	ifbdev->cookie = 0;
768 }
769 
770 void intel_fbdev_fini(struct drm_device *dev)
771 {
772 	struct drm_i915_private *dev_priv = to_i915(dev);
773 	struct intel_fbdev *ifbdev = dev_priv->fbdev;
774 
775 	if (!ifbdev)
776 		return;
777 
778 	cancel_work_sync(&dev_priv->fbdev_suspend_work);
779 	if (!current_is_async())
780 		intel_fbdev_sync(ifbdev);
781 
782 	intel_fbdev_destroy(ifbdev);
783 	dev_priv->fbdev = NULL;
784 }
785 
786 void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
787 {
788 #if 0
789 	struct drm_i915_private *dev_priv = to_i915(dev);
790 	struct intel_fbdev *ifbdev = dev_priv->fbdev;
791 	struct fb_info *info;
792 
793 	if (!ifbdev || !ifbdev->vma)
794 		return;
795 
796 	info = ifbdev->helper.fbdev;
797 
798 	if (synchronous) {
799 		/* Flush any pending work to turn the console on, and then
800 		 * wait to turn it off. It must be synchronous as we are
801 		 * about to suspend or unload the driver.
802 		 *
803 		 * Note that from within the work-handler, we cannot flush
804 		 * ourselves, so only flush outstanding work upon suspend!
805 		 */
806 		if (state != FBINFO_STATE_RUNNING)
807 			flush_work(&dev_priv->fbdev_suspend_work);
808 		console_lock();
809 	} else {
810 		/*
811 		 * The console lock can be pretty contented on resume due
812 		 * to all the printk activity.  Try to keep it out of the hot
813 		 * path of resume if possible.
814 		 */
815 		WARN_ON(state != FBINFO_STATE_RUNNING);
816 		if (!console_trylock()) {
817 			/* Don't block our own workqueue as this can
818 			 * be run in parallel with other i915.ko tasks.
819 			 */
820 			schedule_work(&dev_priv->fbdev_suspend_work);
821 			return;
822 		}
823 	}
824 
825 	/* On resume from hibernation: If the object is shmemfs backed, it has
826 	 * been restored from swap. If the object is stolen however, it will be
827 	 * full of whatever garbage was left in there.
828 	 */
829 	if (state == FBINFO_STATE_RUNNING && ifbdev->fb->obj->stolen)
830 		memset_io(info->screen_base, 0, info->screen_size);
831 
832 	drm_fb_helper_set_suspend(&ifbdev->helper, state);
833 	console_unlock();
834 #endif
835 }
836 
837 void intel_fbdev_output_poll_changed(struct drm_device *dev)
838 {
839 	struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
840 
841 	if (ifbdev && ifbdev->vma)
842 		drm_fb_helper_hotplug_event(&ifbdev->helper);
843 }
844 
845 void intel_fbdev_restore_mode(struct drm_device *dev)
846 {
847 	struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
848 
849 	if (!ifbdev)
850 		return;
851 
852 	intel_fbdev_sync(ifbdev);
853 	if (!ifbdev->vma)
854 		return;
855 
856 #ifdef __DragonFly__
857 	/* XXX: avoid dead-locking the Xorg on exit */
858 	if (mutex_is_locked(&dev->mode_config.mutex)) {
859 		DRM_ERROR("fubar while trying to restore kms_console\n");
860 		return; /* drm_modeset_unlock_all(dev) ? */
861 	}
862 #endif
863 
864 	if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper) == 0)
865 		intel_fbdev_invalidate(ifbdev);
866 }
867