xref: /openbsd/sys/dev/pci/drm/drm_fb_helper.c (revision cecf84d4)
1 /*	$OpenBSD: drm_fb_helper.c,v 1.9 2015/02/11 07:01:36 jsg Exp $	*/
2 /*
3  * Copyright (c) 2006-2009 Red Hat Inc.
4  * Copyright (c) 2006-2008 Intel Corporation
5  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
6  *
7  * DRM framebuffer helper functions
8  *
9  * Permission to use, copy, modify, distribute, and sell this software and its
10  * documentation for any purpose is hereby granted without fee, provided that
11  * the above copyright notice appear in all copies and that both that copyright
12  * notice and this permission notice appear in supporting documentation, and
13  * that the name of the copyright holders not be used in advertising or
14  * publicity pertaining to distribution of the software without specific,
15  * written prior permission.  The copyright holders make no representations
16  * about the suitability of this software for any purpose.  It is provided "as
17  * is" without express or implied warranty.
18  *
19  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
20  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
21  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
22  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
23  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
24  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
25  * OF THIS SOFTWARE.
26  *
27  * Authors:
28  *      Dave Airlie <airlied@linux.ie>
29  *      Jesse Barnes <jesse.barnes@intel.com>
30  */
31 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32 
33 #include "drmP.h"
34 #include "drm_crtc.h"
35 #include "drm_fb_helper.h"
36 #include "drm_crtc_helper.h"
37 
38 static DRM_LIST_HEAD(kernel_fb_helper_list);
39 
40 /**
41  * DOC: fbdev helpers
42  *
43  * The fb helper functions are useful to provide an fbdev on top of a drm kernel
44  * mode setting driver. They can be used mostly independantely from the crtc
45  * helper functions used by many drivers to implement the kernel mode setting
46  * interfaces.
47  */
48 
49 /* simple single crtc case helper function */
50 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
51 {
52 	struct drm_device *dev = fb_helper->dev;
53 	struct drm_connector *connector;
54 	int i;
55 
56 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
57 		struct drm_fb_helper_connector *fb_helper_connector;
58 
59 		fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
60 		if (!fb_helper_connector)
61 			goto fail;
62 
63 		fb_helper_connector->connector = connector;
64 		fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
65 	}
66 	return 0;
67 fail:
68 	for (i = 0; i < fb_helper->connector_count; i++) {
69 		kfree(fb_helper->connector_info[i]);
70 		fb_helper->connector_info[i] = NULL;
71 	}
72 	fb_helper->connector_count = 0;
73 	return -ENOMEM;
74 }
75 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
76 
77 static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
78 {
79 	struct drm_fb_helper_connector *fb_helper_conn;
80 	int i;
81 
82 	for (i = 0; i < fb_helper->connector_count; i++) {
83 		struct drm_cmdline_mode *mode;
84 		struct drm_connector *connector;
85 //		char *option = NULL;
86 
87 		fb_helper_conn = fb_helper->connector_info[i];
88 		connector = fb_helper_conn->connector;
89 		mode = &fb_helper_conn->cmdline_mode;
90 
91 #ifdef notyet
92 		/* do something on return - turn off connector maybe */
93 		if (fb_get_options(drm_get_connector_name(connector), &option))
94 			continue;
95 
96 		if (drm_mode_parse_command_line_for_connector(option,
97 							      connector,
98 							      mode)) {
99 			if (mode->force) {
100 				const char *s;
101 				switch (mode->force) {
102 				case DRM_FORCE_OFF:
103 					s = "OFF";
104 					break;
105 				case DRM_FORCE_ON_DIGITAL:
106 					s = "ON - dig";
107 					break;
108 				default:
109 				case DRM_FORCE_ON:
110 					s = "ON";
111 					break;
112 				}
113 
114 				DRM_INFO("forcing %s connector %s\n",
115 					 drm_get_connector_name(connector), s);
116 				connector->force = mode->force;
117 			}
118 
119 			DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
120 				      drm_get_connector_name(connector),
121 				      mode->xres, mode->yres,
122 				      mode->refresh_specified ? mode->refresh : 60,
123 				      mode->rb ? " reduced blanking" : "",
124 				      mode->margins ? " with margins" : "",
125 				      mode->interlace ?  " interlaced" : "");
126 		}
127 #endif
128 
129 	}
130 	return 0;
131 }
132 
133 static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
134 {
135 	uint16_t *r_base, *g_base, *b_base;
136 	int i;
137 
138 	r_base = crtc->gamma_store;
139 	g_base = r_base + crtc->gamma_size;
140 	b_base = g_base + crtc->gamma_size;
141 
142 	for (i = 0; i < crtc->gamma_size; i++)
143 		helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
144 }
145 
146 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
147 {
148 	uint16_t *r_base, *g_base, *b_base;
149 
150 	if (crtc->funcs->gamma_set == NULL)
151 		return;
152 
153 	r_base = crtc->gamma_store;
154 	g_base = r_base + crtc->gamma_size;
155 	b_base = g_base + crtc->gamma_size;
156 
157 	crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
158 }
159 
160 int drm_fb_helper_debug_enter(struct drm_fb_helper *helper)
161 {
162 	struct drm_crtc_helper_funcs *funcs;
163 	int i;
164 
165 	if (list_empty(&kernel_fb_helper_list))
166 		return false;
167 
168 	list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
169 		for (i = 0; i < helper->crtc_count; i++) {
170 			struct drm_mode_set *mode_set =
171 				&helper->crtc_info[i].mode_set;
172 
173 			if (!mode_set->crtc->enabled)
174 				continue;
175 
176 			funcs =	mode_set->crtc->helper_private;
177 			drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
178 			funcs->mode_set_base_atomic(mode_set->crtc,
179 						    mode_set->fb,
180 						    mode_set->x,
181 						    mode_set->y,
182 						    ENTER_ATOMIC_MODE_SET);
183 		}
184 	}
185 
186 	return 0;
187 }
188 EXPORT_SYMBOL(drm_fb_helper_debug_enter);
189 
190 /* Find the real fb for a given fb helper CRTC */
191 static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
192 {
193 	struct drm_device *dev = crtc->dev;
194 	struct drm_crtc *c;
195 
196 	list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
197 		if (crtc->base.id == c->base.id)
198 			return c->fb;
199 	}
200 
201 	return NULL;
202 }
203 
204 int drm_fb_helper_debug_leave(struct drm_fb_helper *helper)
205 {
206 	struct drm_crtc *crtc;
207 	struct drm_crtc_helper_funcs *funcs;
208 	struct drm_framebuffer *fb;
209 	int i;
210 
211 	for (i = 0; i < helper->crtc_count; i++) {
212 		struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
213 		crtc = mode_set->crtc;
214 		funcs = crtc->helper_private;
215 		fb = drm_mode_config_fb(crtc);
216 
217 		if (!crtc->enabled)
218 			continue;
219 
220 		if (!fb) {
221 			DRM_ERROR("no fb to restore??\n");
222 			continue;
223 		}
224 
225 		drm_fb_helper_restore_lut_atomic(mode_set->crtc);
226 		funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
227 					    crtc->y, LEAVE_ATOMIC_MODE_SET);
228 	}
229 
230 	return 0;
231 }
232 EXPORT_SYMBOL(drm_fb_helper_debug_leave);
233 
234 bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
235 {
236 	bool error = false;
237 	int i, ret;
238 	for (i = 0; i < fb_helper->crtc_count; i++) {
239 		struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
240 		ret = mode_set->crtc->funcs->set_config(mode_set);
241 		if (ret)
242 			error = true;
243 	}
244 	return error;
245 }
246 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode);
247 
248 static bool drm_fb_helper_force_kernel_mode(void)
249 {
250 	bool ret, error = false;
251 	struct drm_fb_helper *helper;
252 
253 	if (list_empty(&kernel_fb_helper_list))
254 		return false;
255 
256 	list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
257 #ifdef notyet
258 		if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF)
259 			continue;
260 #endif
261 
262 		ret = drm_fb_helper_restore_fbdev_mode(helper);
263 		if (ret)
264 			error = true;
265 	}
266 	return error;
267 }
268 
269 #if 0
270 int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
271 			void *panic_str)
272 {
273 	/*
274 	 * It's a waste of time and effort to switch back to text console
275 	 * if the kernel should reboot before panic messages can be seen.
276 	 */
277 	if (panic_timeout < 0)
278 		return 0;
279 
280 	pr_err("panic occurred, switching back to text console\n");
281 	return drm_fb_helper_force_kernel_mode();
282 }
283 EXPORT_SYMBOL(drm_fb_helper_panic);
284 
285 static struct notifier_block paniced = {
286 	.notifier_call = drm_fb_helper_panic,
287 };
288 #endif
289 
290 /**
291  * drm_fb_helper_restore - restore the framebuffer console (kernel) config
292  *
293  * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
294  */
295 void drm_fb_helper_restore(void)
296 {
297 	bool ret;
298 	ret = drm_fb_helper_force_kernel_mode();
299 	if (ret == true)
300 		DRM_ERROR("Failed to restore crtc configuration\n");
301 }
302 EXPORT_SYMBOL(drm_fb_helper_restore);
303 
304 #if 0
305 #ifdef CONFIG_MAGIC_SYSRQ
306 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
307 {
308 	drm_fb_helper_restore();
309 }
310 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
311 
312 static void drm_fb_helper_sysrq(int dummy1)
313 {
314 	schedule_work(&drm_fb_helper_restore_work);
315 }
316 
317 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
318 	.handler = drm_fb_helper_sysrq,
319 	.help_msg = "force-fb(V)",
320 	.action_msg = "Restore framebuffer console",
321 };
322 #else
323 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
324 #endif
325 #endif
326 
327 void drm_fb_helper_dpms(struct drm_fb_helper *fb_helper, int dpms_mode)
328 {
329 	struct drm_device *dev = fb_helper->dev;
330 	struct drm_crtc *crtc;
331 	struct drm_connector *connector;
332 	int i, j;
333 
334 	/*
335 	 * For each CRTC in this fb, turn the connectors on/off.
336 	 */
337 	mutex_lock(&dev->mode_config.mutex);
338 	for (i = 0; i < fb_helper->crtc_count; i++) {
339 		crtc = fb_helper->crtc_info[i].mode_set.crtc;
340 
341 		if (!crtc->enabled)
342 			continue;
343 
344 		/* Walk the connectors & encoders on this fb turning them on/off */
345 		for (j = 0; j < fb_helper->connector_count; j++) {
346 			connector = fb_helper->connector_info[j]->connector;
347 			connector->funcs->dpms(connector, dpms_mode);
348 			drm_object_property_set_value(&connector->base,
349 				dev->mode_config.dpms_property, dpms_mode);
350 		}
351 	}
352 	mutex_unlock(&dev->mode_config.mutex);
353 }
354 
355 #if 0
356 int drm_fb_helper_blank(int blank, struct fb_info *info)
357 {
358 	switch (blank) {
359 	/* Display: On; HSync: On, VSync: On */
360 	case FB_BLANK_UNBLANK:
361 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
362 		break;
363 	/* Display: Off; HSync: On, VSync: On */
364 	case FB_BLANK_NORMAL:
365 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
366 		break;
367 	/* Display: Off; HSync: Off, VSync: On */
368 	case FB_BLANK_HSYNC_SUSPEND:
369 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
370 		break;
371 	/* Display: Off; HSync: On, VSync: Off */
372 	case FB_BLANK_VSYNC_SUSPEND:
373 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
374 		break;
375 	/* Display: Off; HSync: Off, VSync: Off */
376 	case FB_BLANK_POWERDOWN:
377 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
378 		break;
379 	}
380 	return 0;
381 }
382 EXPORT_SYMBOL(drm_fb_helper_blank);
383 #endif
384 
385 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
386 {
387 	int i;
388 
389 	for (i = 0; i < helper->connector_count; i++)
390 		kfree(helper->connector_info[i]);
391 	kfree(helper->connector_info);
392 	for (i = 0; i < helper->crtc_count; i++) {
393 		kfree(helper->crtc_info[i].mode_set.connectors);
394 		if (helper->crtc_info[i].mode_set.mode)
395 			drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
396 	}
397 	kfree(helper->crtc_info);
398 }
399 
400 int drm_fb_helper_init(struct drm_device *dev,
401 		       struct drm_fb_helper *fb_helper,
402 		       int crtc_count, int max_conn_count)
403 {
404 	struct drm_crtc *crtc;
405 	int i;
406 
407 	fb_helper->dev = dev;
408 
409 	INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
410 
411 	fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
412 	if (!fb_helper->crtc_info)
413 		return -ENOMEM;
414 
415 	fb_helper->crtc_count = crtc_count;
416 	fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
417 	if (!fb_helper->connector_info) {
418 		kfree(fb_helper->crtc_info);
419 		return -ENOMEM;
420 	}
421 	fb_helper->connector_count = 0;
422 
423 	for (i = 0; i < crtc_count; i++) {
424 		fb_helper->crtc_info[i].mode_set.connectors =
425 			kcalloc(max_conn_count,
426 				sizeof(struct drm_connector *),
427 				GFP_KERNEL);
428 
429 		if (!fb_helper->crtc_info[i].mode_set.connectors)
430 			goto out_free;
431 		fb_helper->crtc_info[i].mode_set.num_connectors = 0;
432 	}
433 
434 	i = 0;
435 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
436 		fb_helper->crtc_info[i].mode_set.crtc = crtc;
437 		i++;
438 	}
439 
440 	return 0;
441 out_free:
442 	drm_fb_helper_crtc_free(fb_helper);
443 	return -ENOMEM;
444 }
445 EXPORT_SYMBOL(drm_fb_helper_init);
446 
447 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
448 {
449 	if (!list_empty(&fb_helper->kernel_fb_list)) {
450 		list_del(&fb_helper->kernel_fb_list);
451 		if (list_empty(&kernel_fb_helper_list)) {
452 #if 0
453 			pr_info("drm: unregistered panic notifier\n");
454 			atomic_notifier_chain_unregister(&panic_notifier_list,
455 							 &paniced);
456 			unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
457 #endif
458 		}
459 	}
460 
461 	drm_fb_helper_crtc_free(fb_helper);
462 
463 }
464 EXPORT_SYMBOL(drm_fb_helper_fini);
465 
466 #if 0
467 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
468 		     u16 blue, u16 regno, struct fb_info *info)
469 {
470 	struct drm_fb_helper *fb_helper = info->par;
471 	struct drm_framebuffer *fb = fb_helper->fb;
472 	int pindex;
473 
474 	if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
475 		u32 *palette;
476 		u32 value;
477 		/* place color in psuedopalette */
478 		if (regno > 16)
479 			return -EINVAL;
480 		palette = (u32 *)info->pseudo_palette;
481 		red >>= (16 - info->var.red.length);
482 		green >>= (16 - info->var.green.length);
483 		blue >>= (16 - info->var.blue.length);
484 		value = (red << info->var.red.offset) |
485 			(green << info->var.green.offset) |
486 			(blue << info->var.blue.offset);
487 		if (info->var.transp.length > 0) {
488 			u32 mask = (1 << info->var.transp.length) - 1;
489 			mask <<= info->var.transp.offset;
490 			value |= mask;
491 		}
492 		palette[regno] = value;
493 		return 0;
494 	}
495 
496 	pindex = regno;
497 
498 	if (fb->bits_per_pixel == 16) {
499 		pindex = regno << 3;
500 
501 		if (fb->depth == 16 && regno > 63)
502 			return -EINVAL;
503 		if (fb->depth == 15 && regno > 31)
504 			return -EINVAL;
505 
506 		if (fb->depth == 16) {
507 			u16 r, g, b;
508 			int i;
509 			if (regno < 32) {
510 				for (i = 0; i < 8; i++)
511 					fb_helper->funcs->gamma_set(crtc, red,
512 						green, blue, pindex + i);
513 			}
514 
515 			fb_helper->funcs->gamma_get(crtc, &r,
516 						    &g, &b,
517 						    pindex >> 1);
518 
519 			for (i = 0; i < 4; i++)
520 				fb_helper->funcs->gamma_set(crtc, r,
521 							    green, b,
522 							    (pindex >> 1) + i);
523 		}
524 	}
525 
526 	if (fb->depth != 16)
527 		fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
528 	return 0;
529 }
530 #endif
531 
532 #if 0
533 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
534 {
535 	struct drm_fb_helper *fb_helper = info->par;
536 	struct drm_crtc_helper_funcs *crtc_funcs;
537 	u16 *red, *green, *blue, *transp;
538 	struct drm_crtc *crtc;
539 	int i, j, rc = 0;
540 	int start;
541 
542 	for (i = 0; i < fb_helper->crtc_count; i++) {
543 		crtc = fb_helper->crtc_info[i].mode_set.crtc;
544 		crtc_funcs = crtc->helper_private;
545 
546 		red = cmap->red;
547 		green = cmap->green;
548 		blue = cmap->blue;
549 		transp = cmap->transp;
550 		start = cmap->start;
551 
552 		for (j = 0; j < cmap->len; j++) {
553 			u16 hred, hgreen, hblue, htransp = 0xffff;
554 
555 			hred = *red++;
556 			hgreen = *green++;
557 			hblue = *blue++;
558 
559 			if (transp)
560 				htransp = *transp++;
561 
562 			rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
563 			if (rc)
564 				return rc;
565 		}
566 		crtc_funcs->load_lut(crtc);
567 	}
568 	return rc;
569 }
570 EXPORT_SYMBOL(drm_fb_helper_setcmap);
571 #endif
572 
573 #if 0
574 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
575 			    struct fb_info *info)
576 {
577 	struct drm_fb_helper *fb_helper = info->par;
578 	struct drm_framebuffer *fb = fb_helper->fb;
579 	int depth;
580 
581 	if (var->pixclock != 0 || in_dbg_master())
582 		return -EINVAL;
583 
584 	/* Need to resize the fb object !!! */
585 	if (var->bits_per_pixel > fb->bits_per_pixel ||
586 	    var->xres > fb->width || var->yres > fb->height ||
587 	    var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
588 		DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
589 			  "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
590 			  var->xres, var->yres, var->bits_per_pixel,
591 			  var->xres_virtual, var->yres_virtual,
592 			  fb->width, fb->height, fb->bits_per_pixel);
593 		return -EINVAL;
594 	}
595 
596 	switch (var->bits_per_pixel) {
597 	case 16:
598 		depth = (var->green.length == 6) ? 16 : 15;
599 		break;
600 	case 32:
601 		depth = (var->transp.length > 0) ? 32 : 24;
602 		break;
603 	default:
604 		depth = var->bits_per_pixel;
605 		break;
606 	}
607 
608 	switch (depth) {
609 	case 8:
610 		var->red.offset = 0;
611 		var->green.offset = 0;
612 		var->blue.offset = 0;
613 		var->red.length = 8;
614 		var->green.length = 8;
615 		var->blue.length = 8;
616 		var->transp.length = 0;
617 		var->transp.offset = 0;
618 		break;
619 	case 15:
620 		var->red.offset = 10;
621 		var->green.offset = 5;
622 		var->blue.offset = 0;
623 		var->red.length = 5;
624 		var->green.length = 5;
625 		var->blue.length = 5;
626 		var->transp.length = 1;
627 		var->transp.offset = 15;
628 		break;
629 	case 16:
630 		var->red.offset = 11;
631 		var->green.offset = 5;
632 		var->blue.offset = 0;
633 		var->red.length = 5;
634 		var->green.length = 6;
635 		var->blue.length = 5;
636 		var->transp.length = 0;
637 		var->transp.offset = 0;
638 		break;
639 	case 24:
640 		var->red.offset = 16;
641 		var->green.offset = 8;
642 		var->blue.offset = 0;
643 		var->red.length = 8;
644 		var->green.length = 8;
645 		var->blue.length = 8;
646 		var->transp.length = 0;
647 		var->transp.offset = 0;
648 		break;
649 	case 32:
650 		var->red.offset = 16;
651 		var->green.offset = 8;
652 		var->blue.offset = 0;
653 		var->red.length = 8;
654 		var->green.length = 8;
655 		var->blue.length = 8;
656 		var->transp.length = 8;
657 		var->transp.offset = 24;
658 		break;
659 	default:
660 		return -EINVAL;
661 	}
662 	return 0;
663 }
664 EXPORT_SYMBOL(drm_fb_helper_check_var);
665 #endif
666 
667 #if 0
668 /* this will let fbcon do the mode init */
669 int drm_fb_helper_set_par(struct fb_info *info)
670 {
671 	struct drm_fb_helper *fb_helper = info->par;
672 	struct drm_device *dev = fb_helper->dev;
673 	struct fb_var_screeninfo *var = &info->var;
674 	struct drm_crtc *crtc;
675 	int ret;
676 	int i;
677 
678 	if (var->pixclock != 0) {
679 		DRM_ERROR("PIXEL CLOCK SET\n");
680 		return -EINVAL;
681 	}
682 
683 	mutex_lock(&dev->mode_config.mutex);
684 	for (i = 0; i < fb_helper->crtc_count; i++) {
685 		crtc = fb_helper->crtc_info[i].mode_set.crtc;
686 		ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
687 		if (ret) {
688 			mutex_unlock(&dev->mode_config.mutex);
689 			return ret;
690 		}
691 	}
692 	mutex_unlock(&dev->mode_config.mutex);
693 
694 	if (fb_helper->delayed_hotplug) {
695 		fb_helper->delayed_hotplug = false;
696 		drm_fb_helper_hotplug_event(fb_helper);
697 	}
698 	return 0;
699 }
700 EXPORT_SYMBOL(drm_fb_helper_set_par);
701 #endif
702 
703 #if 0
704 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
705 			      struct fb_info *info)
706 {
707 	struct drm_fb_helper *fb_helper = info->par;
708 	struct drm_device *dev = fb_helper->dev;
709 	struct drm_mode_set *modeset;
710 	struct drm_crtc *crtc;
711 	int ret = 0;
712 	int i;
713 
714 	mutex_lock(&dev->mode_config.mutex);
715 	for (i = 0; i < fb_helper->crtc_count; i++) {
716 		crtc = fb_helper->crtc_info[i].mode_set.crtc;
717 
718 		modeset = &fb_helper->crtc_info[i].mode_set;
719 
720 		modeset->x = var->xoffset;
721 		modeset->y = var->yoffset;
722 
723 		if (modeset->num_connectors) {
724 			ret = crtc->funcs->set_config(modeset);
725 			if (!ret) {
726 				info->var.xoffset = var->xoffset;
727 				info->var.yoffset = var->yoffset;
728 			}
729 		}
730 	}
731 	mutex_unlock(&dev->mode_config.mutex);
732 	return ret;
733 }
734 EXPORT_SYMBOL(drm_fb_helper_pan_display);
735 #endif
736 
737 int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
738 				  int preferred_bpp)
739 {
740 	int new_fb = 0;
741 	int crtc_count = 0;
742 	int i;
743 #if 0
744 	struct fb_info *info;
745 #endif
746 	struct drm_fb_helper_surface_size sizes;
747 	int gamma_size = 0;
748 
749 	memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
750 	sizes.surface_depth = 24;
751 	sizes.surface_bpp = 32;
752 	sizes.fb_width = (unsigned)-1;
753 	sizes.fb_height = (unsigned)-1;
754 
755 	/* if driver picks 8 or 16 by default use that
756 	   for both depth/bpp */
757 	if (preferred_bpp != sizes.surface_bpp)
758 		sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
759 
760 	/* first up get a count of crtcs now in use and new min/maxes width/heights */
761 	for (i = 0; i < fb_helper->connector_count; i++) {
762 		struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
763 		struct drm_cmdline_mode *cmdline_mode;
764 
765 		cmdline_mode = &fb_helper_conn->cmdline_mode;
766 
767 		if (cmdline_mode->bpp_specified) {
768 			switch (cmdline_mode->bpp) {
769 			case 8:
770 				sizes.surface_depth = sizes.surface_bpp = 8;
771 				break;
772 			case 15:
773 				sizes.surface_depth = 15;
774 				sizes.surface_bpp = 16;
775 				break;
776 			case 16:
777 				sizes.surface_depth = sizes.surface_bpp = 16;
778 				break;
779 			case 24:
780 				sizes.surface_depth = sizes.surface_bpp = 24;
781 				break;
782 			case 32:
783 				sizes.surface_depth = 24;
784 				sizes.surface_bpp = 32;
785 				break;
786 			}
787 			break;
788 		}
789 	}
790 
791 	crtc_count = 0;
792 	for (i = 0; i < fb_helper->crtc_count; i++) {
793 		struct drm_display_mode *desired_mode;
794 		desired_mode = fb_helper->crtc_info[i].desired_mode;
795 
796 		if (desired_mode) {
797 			if (gamma_size == 0)
798 				gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
799 			if (desired_mode->hdisplay < sizes.fb_width)
800 				sizes.fb_width = desired_mode->hdisplay;
801 			if (desired_mode->vdisplay < sizes.fb_height)
802 				sizes.fb_height = desired_mode->vdisplay;
803 			if (desired_mode->hdisplay > sizes.surface_width)
804 				sizes.surface_width = desired_mode->hdisplay;
805 			if (desired_mode->vdisplay > sizes.surface_height)
806 				sizes.surface_height = desired_mode->vdisplay;
807 			crtc_count++;
808 		}
809 	}
810 
811 	if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
812 		/* hmm everyone went away - assume VGA cable just fell out
813 		   and will come back later. */
814 		DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
815 		sizes.fb_width = sizes.surface_width = 1024;
816 		sizes.fb_height = sizes.surface_height = 768;
817 	}
818 
819 	/* push down into drivers */
820 	new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
821 	if (new_fb < 0)
822 		return new_fb;
823 
824 #if 0
825 	info = fb_helper->fbdev;
826 #endif
827 
828 	/* set the fb pointer */
829 	for (i = 0; i < fb_helper->crtc_count; i++)
830 		fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
831 
832 #if 0
833 	if (new_fb) {
834 		info->var.pixclock = 0;
835 		if (register_framebuffer(info) < 0)
836 			return -EINVAL;
837 
838 		dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
839 				info->node, info->fix.id);
840 
841 	} else {
842 		drm_fb_helper_set_par(info);
843 	}
844 
845 	/* Switch back to kernel console on panic */
846 	/* multi card linked list maybe */
847 	if (list_empty(&kernel_fb_helper_list)) {
848 		dev_info(fb_helper->dev->dev, "registered panic notifier\n");
849 		atomic_notifier_chain_register(&panic_notifier_list,
850 					       &paniced);
851 		register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
852 	}
853 #endif
854 	if (new_fb)
855 		list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
856 
857 	return 0;
858 }
859 EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
860 
861 #if 0
862 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
863 			    uint32_t depth)
864 {
865 	info->fix.type = FB_TYPE_PACKED_PIXELS;
866 	info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
867 		FB_VISUAL_TRUECOLOR;
868 	info->fix.mmio_start = 0;
869 	info->fix.mmio_len = 0;
870 	info->fix.type_aux = 0;
871 	info->fix.xpanstep = 1; /* doing it in hw */
872 	info->fix.ypanstep = 1; /* doing it in hw */
873 	info->fix.ywrapstep = 0;
874 	info->fix.accel = FB_ACCEL_NONE;
875 	info->fix.type_aux = 0;
876 
877 	info->fix.line_length = pitch;
878 	return;
879 }
880 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
881 
882 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
883 			    uint32_t fb_width, uint32_t fb_height)
884 {
885 	struct drm_framebuffer *fb = fb_helper->fb;
886 	info->pseudo_palette = fb_helper->pseudo_palette;
887 	info->var.xres_virtual = fb->width;
888 	info->var.yres_virtual = fb->height;
889 	info->var.bits_per_pixel = fb->bits_per_pixel;
890 	info->var.accel_flags = FB_ACCELF_TEXT;
891 	info->var.xoffset = 0;
892 	info->var.yoffset = 0;
893 	info->var.activate = FB_ACTIVATE_NOW;
894 	info->var.height = -1;
895 	info->var.width = -1;
896 
897 	switch (fb->depth) {
898 	case 8:
899 		info->var.red.offset = 0;
900 		info->var.green.offset = 0;
901 		info->var.blue.offset = 0;
902 		info->var.red.length = 8; /* 8bit DAC */
903 		info->var.green.length = 8;
904 		info->var.blue.length = 8;
905 		info->var.transp.offset = 0;
906 		info->var.transp.length = 0;
907 		break;
908 	case 15:
909 		info->var.red.offset = 10;
910 		info->var.green.offset = 5;
911 		info->var.blue.offset = 0;
912 		info->var.red.length = 5;
913 		info->var.green.length = 5;
914 		info->var.blue.length = 5;
915 		info->var.transp.offset = 15;
916 		info->var.transp.length = 1;
917 		break;
918 	case 16:
919 		info->var.red.offset = 11;
920 		info->var.green.offset = 5;
921 		info->var.blue.offset = 0;
922 		info->var.red.length = 5;
923 		info->var.green.length = 6;
924 		info->var.blue.length = 5;
925 		info->var.transp.offset = 0;
926 		break;
927 	case 24:
928 		info->var.red.offset = 16;
929 		info->var.green.offset = 8;
930 		info->var.blue.offset = 0;
931 		info->var.red.length = 8;
932 		info->var.green.length = 8;
933 		info->var.blue.length = 8;
934 		info->var.transp.offset = 0;
935 		info->var.transp.length = 0;
936 		break;
937 	case 32:
938 		info->var.red.offset = 16;
939 		info->var.green.offset = 8;
940 		info->var.blue.offset = 0;
941 		info->var.red.length = 8;
942 		info->var.green.length = 8;
943 		info->var.blue.length = 8;
944 		info->var.transp.offset = 24;
945 		info->var.transp.length = 8;
946 		break;
947 	default:
948 		break;
949 	}
950 
951 	info->var.xres = fb_width;
952 	info->var.yres = fb_height;
953 }
954 EXPORT_SYMBOL(drm_fb_helper_fill_var);
955 #endif
956 
957 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
958 					       uint32_t maxX,
959 					       uint32_t maxY)
960 {
961 	struct drm_connector *connector;
962 	int count = 0;
963 	int i;
964 
965 	for (i = 0; i < fb_helper->connector_count; i++) {
966 		connector = fb_helper->connector_info[i]->connector;
967 		count += connector->funcs->fill_modes(connector, maxX, maxY);
968 	}
969 
970 	return count;
971 }
972 
973 static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
974 {
975 	struct drm_display_mode *mode;
976 
977 	list_for_each_entry(mode, &fb_connector->connector->modes, head) {
978 		if (drm_mode_width(mode) > width ||
979 		    drm_mode_height(mode) > height)
980 			continue;
981 		if (mode->type & DRM_MODE_TYPE_PREFERRED)
982 			return mode;
983 	}
984 	return NULL;
985 }
986 
987 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
988 {
989 	struct drm_cmdline_mode *cmdline_mode;
990 	cmdline_mode = &fb_connector->cmdline_mode;
991 	return cmdline_mode->specified;
992 }
993 
994 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
995 						      int width, int height)
996 {
997 	struct drm_cmdline_mode *cmdline_mode;
998 	struct drm_display_mode *mode = NULL;
999 
1000 	cmdline_mode = &fb_helper_conn->cmdline_mode;
1001 	if (cmdline_mode->specified == false)
1002 		return mode;
1003 
1004 	/* attempt to find a matching mode in the list of modes
1005 	 *  we have gotten so far, if not add a CVT mode that conforms
1006 	 */
1007 	if (cmdline_mode->rb || cmdline_mode->margins)
1008 		goto create_mode;
1009 
1010 	list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1011 		/* check width/height */
1012 		if (mode->hdisplay != cmdline_mode->xres ||
1013 		    mode->vdisplay != cmdline_mode->yres)
1014 			continue;
1015 
1016 		if (cmdline_mode->refresh_specified) {
1017 			if (mode->vrefresh != cmdline_mode->refresh)
1018 				continue;
1019 		}
1020 
1021 		if (cmdline_mode->interlace) {
1022 			if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1023 				continue;
1024 		}
1025 		return mode;
1026 	}
1027 
1028 create_mode:
1029 	mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1030 						 cmdline_mode);
1031 	list_add(&mode->head, &fb_helper_conn->connector->modes);
1032 	return mode;
1033 }
1034 
1035 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1036 {
1037 	bool enable;
1038 
1039 	if (strict)
1040 		enable = connector->status == connector_status_connected;
1041 	else
1042 		enable = connector->status != connector_status_disconnected;
1043 
1044 	return enable;
1045 }
1046 
1047 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1048 				  bool *enabled)
1049 {
1050 	bool any_enabled = false;
1051 	struct drm_connector *connector;
1052 	int i = 0;
1053 
1054 	for (i = 0; i < fb_helper->connector_count; i++) {
1055 		connector = fb_helper->connector_info[i]->connector;
1056 		enabled[i] = drm_connector_enabled(connector, true);
1057 		DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1058 			  enabled[i] ? "yes" : "no");
1059 		any_enabled |= enabled[i];
1060 	}
1061 
1062 	if (any_enabled)
1063 		return;
1064 
1065 	for (i = 0; i < fb_helper->connector_count; i++) {
1066 		connector = fb_helper->connector_info[i]->connector;
1067 		enabled[i] = drm_connector_enabled(connector, false);
1068 	}
1069 }
1070 
1071 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1072 			      struct drm_display_mode **modes,
1073 			      bool *enabled, int width, int height)
1074 {
1075 	int count, i, j;
1076 	bool can_clone = false;
1077 	struct drm_fb_helper_connector *fb_helper_conn;
1078 	struct drm_display_mode *dmt_mode, *mode;
1079 
1080 	/* only contemplate cloning in the single crtc case */
1081 	if (fb_helper->crtc_count > 1)
1082 		return false;
1083 
1084 	count = 0;
1085 	for (i = 0; i < fb_helper->connector_count; i++) {
1086 		if (enabled[i])
1087 			count++;
1088 	}
1089 
1090 	/* only contemplate cloning if more than one connector is enabled */
1091 	if (count <= 1)
1092 		return false;
1093 
1094 	/* check the command line or if nothing common pick 1024x768 */
1095 	can_clone = true;
1096 	for (i = 0; i < fb_helper->connector_count; i++) {
1097 		if (!enabled[i])
1098 			continue;
1099 		fb_helper_conn = fb_helper->connector_info[i];
1100 		modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1101 		if (!modes[i]) {
1102 			can_clone = false;
1103 			break;
1104 		}
1105 		for (j = 0; j < i; j++) {
1106 			if (!enabled[j])
1107 				continue;
1108 			if (!drm_mode_equal(modes[j], modes[i]))
1109 				can_clone = false;
1110 		}
1111 	}
1112 
1113 	if (can_clone) {
1114 		DRM_DEBUG_KMS("can clone using command line\n");
1115 		return true;
1116 	}
1117 
1118 	/* try and find a 1024x768 mode on each connector */
1119 	can_clone = true;
1120 	dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
1121 
1122 	for (i = 0; i < fb_helper->connector_count; i++) {
1123 
1124 		if (!enabled[i])
1125 			continue;
1126 
1127 		fb_helper_conn = fb_helper->connector_info[i];
1128 		list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1129 			if (drm_mode_equal(mode, dmt_mode))
1130 				modes[i] = mode;
1131 		}
1132 		if (!modes[i])
1133 			can_clone = false;
1134 	}
1135 
1136 	if (can_clone) {
1137 		DRM_DEBUG_KMS("can clone using 1024x768\n");
1138 		return true;
1139 	}
1140 	DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1141 	return false;
1142 }
1143 
1144 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1145 				 struct drm_display_mode **modes,
1146 				 bool *enabled, int width, int height)
1147 {
1148 	struct drm_fb_helper_connector *fb_helper_conn;
1149 	int i;
1150 
1151 	for (i = 0; i < fb_helper->connector_count; i++) {
1152 		fb_helper_conn = fb_helper->connector_info[i];
1153 
1154 		if (enabled[i] == false)
1155 			continue;
1156 
1157 		DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1158 			      fb_helper_conn->connector->base.id);
1159 
1160 		/* got for command line mode first */
1161 		modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1162 		if (!modes[i]) {
1163 			DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1164 				      fb_helper_conn->connector->base.id);
1165 			modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1166 		}
1167 		/* No preferred modes, pick one off the list */
1168 		if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1169 			list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1170 				break;
1171 		}
1172 		DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1173 			  "none");
1174 	}
1175 	return true;
1176 }
1177 
1178 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1179 			  struct drm_fb_helper_crtc **best_crtcs,
1180 			  struct drm_display_mode **modes,
1181 			  int n, int width, int height)
1182 {
1183 	int c, o;
1184 	struct drm_device *dev = fb_helper->dev;
1185 	struct drm_connector *connector;
1186 	struct drm_connector_helper_funcs *connector_funcs;
1187 	struct drm_encoder *encoder;
1188 	struct drm_fb_helper_crtc *best_crtc;
1189 	int my_score, best_score, score;
1190 	struct drm_fb_helper_crtc **crtcs, *crtc;
1191 	struct drm_fb_helper_connector *fb_helper_conn;
1192 
1193 	if (n == fb_helper->connector_count)
1194 		return 0;
1195 
1196 	fb_helper_conn = fb_helper->connector_info[n];
1197 	connector = fb_helper_conn->connector;
1198 
1199 	best_crtcs[n] = NULL;
1200 	best_crtc = NULL;
1201 	best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1202 	if (modes[n] == NULL)
1203 		return best_score;
1204 
1205 	crtcs = kzalloc(dev->mode_config.num_connector *
1206 			sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1207 	if (!crtcs)
1208 		return best_score;
1209 
1210 	my_score = 1;
1211 	if (connector->status == connector_status_connected)
1212 		my_score++;
1213 	if (drm_has_cmdline_mode(fb_helper_conn))
1214 		my_score++;
1215 	if (drm_has_preferred_mode(fb_helper_conn, width, height))
1216 		my_score++;
1217 
1218 	connector_funcs = connector->helper_private;
1219 	encoder = connector_funcs->best_encoder(connector);
1220 	if (!encoder)
1221 		goto out;
1222 
1223 	/* select a crtc for this connector and then attempt to configure
1224 	   remaining connectors */
1225 	for (c = 0; c < fb_helper->crtc_count; c++) {
1226 		crtc = &fb_helper->crtc_info[c];
1227 
1228 		if ((encoder->possible_crtcs & (1 << c)) == 0)
1229 			continue;
1230 
1231 		for (o = 0; o < n; o++)
1232 			if (best_crtcs[o] == crtc)
1233 				break;
1234 
1235 		if (o < n) {
1236 			/* ignore cloning unless only a single crtc */
1237 			if (fb_helper->crtc_count > 1)
1238 				continue;
1239 
1240 			if (!drm_mode_equal(modes[o], modes[n]))
1241 				continue;
1242 		}
1243 
1244 		crtcs[n] = crtc;
1245 		memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1246 		score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1247 						  width, height);
1248 		if (score > best_score) {
1249 			best_crtc = crtc;
1250 			best_score = score;
1251 			memcpy(best_crtcs, crtcs,
1252 			       dev->mode_config.num_connector *
1253 			       sizeof(struct drm_fb_helper_crtc *));
1254 		}
1255 	}
1256 out:
1257 	kfree(crtcs);
1258 	return best_score;
1259 }
1260 
1261 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1262 {
1263 	struct drm_device *dev = fb_helper->dev;
1264 	struct drm_fb_helper_crtc **crtcs;
1265 	struct drm_display_mode **modes;
1266 	struct drm_mode_set *modeset;
1267 	bool *enabled;
1268 	int width, height;
1269 	int i, ret;
1270 
1271 	DRM_DEBUG_KMS("\n");
1272 
1273 	width = dev->mode_config.max_width;
1274 	height = dev->mode_config.max_height;
1275 
1276 	crtcs = kcalloc(dev->mode_config.num_connector,
1277 			sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1278 	modes = kcalloc(dev->mode_config.num_connector,
1279 			sizeof(struct drm_display_mode *), GFP_KERNEL);
1280 	enabled = kcalloc(dev->mode_config.num_connector,
1281 			  sizeof(bool), GFP_KERNEL);
1282 	if (!crtcs || !modes || !enabled) {
1283 		DRM_ERROR("Memory allocation failed\n");
1284 		goto out;
1285 	}
1286 
1287 
1288 	drm_enable_connectors(fb_helper, enabled);
1289 
1290 	ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1291 	if (!ret) {
1292 		ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1293 		if (!ret)
1294 			DRM_ERROR("Unable to find initial modes\n");
1295 	}
1296 
1297 	DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1298 
1299 	drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1300 
1301 	/* need to set the modesets up here for use later */
1302 	/* fill out the connector<->crtc mappings into the modesets */
1303 	for (i = 0; i < fb_helper->crtc_count; i++) {
1304 		modeset = &fb_helper->crtc_info[i].mode_set;
1305 		modeset->num_connectors = 0;
1306 	}
1307 
1308 	for (i = 0; i < fb_helper->connector_count; i++) {
1309 		struct drm_display_mode *mode = modes[i];
1310 		struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1311 		modeset = &fb_crtc->mode_set;
1312 
1313 		if (mode && fb_crtc) {
1314 			DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1315 				      mode->name, fb_crtc->mode_set.crtc->base.id);
1316 			fb_crtc->desired_mode = mode;
1317 			if (modeset->mode)
1318 				drm_mode_destroy(dev, modeset->mode);
1319 			modeset->mode = drm_mode_duplicate(dev,
1320 							   fb_crtc->desired_mode);
1321 			modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1322 		}
1323 	}
1324 
1325 out:
1326 	kfree(crtcs);
1327 	kfree(modes);
1328 	kfree(enabled);
1329 }
1330 
1331 /**
1332  * drm_helper_initial_config - setup a sane initial connector configuration
1333  * @fb_helper: fb_helper device struct
1334  * @bpp_sel: bpp value to use for the framebuffer configuration
1335  *
1336  * LOCKING:
1337  * Called at init time by the driver to set up the @fb_helper initial
1338  * configuration, must take the mode config lock.
1339  *
1340  * Scans the CRTCs and connectors and tries to put together an initial setup.
1341  * At the moment, this is a cloned configuration across all heads with
1342  * a new framebuffer object as the backing store.
1343  *
1344  * RETURNS:
1345  * Zero if everything went ok, nonzero otherwise.
1346  */
1347 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1348 {
1349 	struct drm_device *dev = fb_helper->dev;
1350 	int count = 0;
1351 
1352 	/* disable all the possible outputs/crtcs before entering KMS mode */
1353 	drm_helper_disable_unused_functions(fb_helper->dev);
1354 
1355 	drm_fb_helper_parse_command_line(fb_helper);
1356 
1357 	count = drm_fb_helper_probe_connector_modes(fb_helper,
1358 						    dev->mode_config.max_width,
1359 						    dev->mode_config.max_height);
1360 	/*
1361 	 * we shouldn't end up with no modes here.
1362 	 */
1363 	if (count == 0)
1364 		dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
1365 
1366 	drm_setup_crtcs(fb_helper);
1367 
1368 	return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1369 }
1370 EXPORT_SYMBOL(drm_fb_helper_initial_config);
1371 
1372 /**
1373  * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1374  *                               probing all the outputs attached to the fb
1375  * @fb_helper: the drm_fb_helper
1376  *
1377  * LOCKING:
1378  * Called at runtime, must take mode config lock.
1379  *
1380  * Scan the connectors attached to the fb_helper and try to put together a
1381  * setup after *notification of a change in output configuration.
1382  *
1383  * RETURNS:
1384  * 0 on success and a non-zero error code otherwise.
1385  */
1386 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1387 {
1388 	struct drm_device *dev = fb_helper->dev;
1389 	int count = 0;
1390 	u32 max_width, max_height, bpp_sel;
1391 	int bound = 0, crtcs_bound = 0;
1392 	struct drm_crtc *crtc;
1393 
1394 	if (!fb_helper->fb)
1395 		return 0;
1396 
1397 	mutex_lock(&dev->mode_config.mutex);
1398 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1399 		if (crtc->fb)
1400 			crtcs_bound++;
1401 		if (crtc->fb == fb_helper->fb)
1402 			bound++;
1403 	}
1404 
1405 	if (bound < crtcs_bound) {
1406 		fb_helper->delayed_hotplug = true;
1407 		mutex_unlock(&dev->mode_config.mutex);
1408 		return 0;
1409 	}
1410 	DRM_DEBUG_KMS("\n");
1411 
1412 	max_width = fb_helper->fb->width;
1413 	max_height = fb_helper->fb->height;
1414 	bpp_sel = fb_helper->fb->bits_per_pixel;
1415 
1416 	count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1417 						    max_height);
1418 	drm_setup_crtcs(fb_helper);
1419 	mutex_unlock(&dev->mode_config.mutex);
1420 
1421 	return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1422 }
1423 EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
1424 
1425 #ifdef __linux__
1426 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
1427  * but the module doesn't depend on any fb console symbols.  At least
1428  * attempt to load fbcon to avoid leaving the system without a usable console.
1429  */
1430 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
1431 static int __init drm_fb_helper_modinit(void)
1432 {
1433 	const char *name = "fbcon";
1434 	struct module *fbcon;
1435 
1436 	mutex_lock(&module_mutex);
1437 	fbcon = find_module(name);
1438 	mutex_unlock(&module_mutex);
1439 
1440 	if (!fbcon)
1441 		request_module_nowait(name);
1442 	return 0;
1443 }
1444 
1445 module_init(drm_fb_helper_modinit);
1446 #endif
1447 #endif /* __linux__ */
1448