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