xref: /dragonfly/sys/dev/drm/drm_fb_helper.c (revision 31524921)
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 
31 #include <linux/kernel.h>
32 #include <linux/fb.h>
33 #include <linux/module.h>
34 #include <drm/drmP.h>
35 #include <drm/drm_crtc.h>
36 #include <drm/drm_fb_helper.h>
37 #include <drm/drm_crtc_helper.h>
38 
39 static LINUX_LIST_HEAD(kernel_fb_helper_list);
40 
41 /**
42  * DOC: fbdev helpers
43  *
44  * The fb helper functions are useful to provide an fbdev on top of a drm kernel
45  * mode setting driver. They can be used mostly independently from the crtc
46  * helper functions used by many drivers to implement the kernel mode setting
47  * interfaces.
48  *
49  * Initialization is done as a four-step process with drm_fb_helper_prepare(),
50  * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and
51  * drm_fb_helper_initial_config(). Drivers with fancier requirements than the
52  * default behaviour can override the third step with their own code.
53  * Teardown is done with drm_fb_helper_fini().
54  *
55  * At runtime drivers should restore the fbdev console by calling
56  * drm_fb_helper_restore_fbdev_mode() from their ->lastclose callback. They
57  * should also notify the fb helper code from updates to the output
58  * configuration by calling drm_fb_helper_hotplug_event(). For easier
59  * integration with the output polling code in drm_crtc_helper.c the modeset
60  * code provides a ->output_poll_changed callback.
61  *
62  * All other functions exported by the fb helper library can be used to
63  * implement the fbdev driver interface by the driver.
64  *
65  * It is possible, though perhaps somewhat tricky, to implement race-free
66  * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
67  * helper must be called first to initialize the minimum required to make
68  * hotplug detection work. Drivers also need to make sure to properly set up
69  * the dev->mode_config.funcs member. After calling drm_kms_helper_poll_init()
70  * it is safe to enable interrupts and start processing hotplug events. At the
71  * same time, drivers should initialize all modeset objects such as CRTCs,
72  * encoders and connectors. To finish up the fbdev helper initialization, the
73  * drm_fb_helper_init() function is called. To probe for all attached displays
74  * and set up an initial configuration using the detected hardware, drivers
75  * should call drm_fb_helper_single_add_all_connectors() followed by
76  * drm_fb_helper_initial_config().
77  */
78 
79 /**
80  * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
81  * 					       emulation helper
82  * @fb_helper: fbdev initialized with drm_fb_helper_init
83  *
84  * This functions adds all the available connectors for use with the given
85  * fb_helper. This is a separate step to allow drivers to freely assign
86  * connectors to the fbdev, e.g. if some are reserved for special purposes or
87  * not adequate to be used for the fbcon.
88  *
89  * Since this is part of the initial setup before the fbdev is published, no
90  * locking is required.
91  */
92 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
93 {
94 	struct drm_device *dev = fb_helper->dev;
95 	struct drm_connector *connector;
96 	int i;
97 
98 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
99 		struct drm_fb_helper_connector *fb_helper_connector;
100 
101 		fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
102 		if (!fb_helper_connector)
103 			goto fail;
104 
105 		fb_helper_connector->connector = connector;
106 		fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
107 	}
108 	return 0;
109 fail:
110 	for (i = 0; i < fb_helper->connector_count; i++) {
111 		kfree(fb_helper->connector_info[i]);
112 		fb_helper->connector_info[i] = NULL;
113 	}
114 	fb_helper->connector_count = 0;
115 	return -ENOMEM;
116 }
117 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
118 
119 int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, struct drm_connector *connector)
120 {
121 	struct drm_fb_helper_connector **temp;
122 	struct drm_fb_helper_connector *fb_helper_connector;
123 
124 	WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
125 	if (fb_helper->connector_count + 1 > fb_helper->connector_info_alloc_count) {
126 		temp = krealloc(fb_helper->connector_info, sizeof(struct drm_fb_helper_connector *) * (fb_helper->connector_count + 1), M_DRM, M_WAITOK);
127 		if (!temp)
128 			return -ENOMEM;
129 
130 		fb_helper->connector_info_alloc_count = fb_helper->connector_count + 1;
131 		fb_helper->connector_info = temp;
132 	}
133 
134 
135 	fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
136 	if (!fb_helper_connector)
137 		return -ENOMEM;
138 
139 	fb_helper_connector->connector = connector;
140 	fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
141 	return 0;
142 }
143 EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
144 
145 int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
146 				       struct drm_connector *connector)
147 {
148 	struct drm_fb_helper_connector *fb_helper_connector;
149 	int i, j;
150 
151 	WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
152 
153 	for (i = 0; i < fb_helper->connector_count; i++) {
154 		if (fb_helper->connector_info[i]->connector == connector)
155 			break;
156 	}
157 
158 	if (i == fb_helper->connector_count)
159 		return -EINVAL;
160 	fb_helper_connector = fb_helper->connector_info[i];
161 
162 	for (j = i + 1; j < fb_helper->connector_count; j++) {
163 		fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
164 	}
165 	fb_helper->connector_count--;
166 	kfree(fb_helper_connector);
167 	return 0;
168 }
169 EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
170 
171 #if 0
172 static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
173 {
174 	uint16_t *r_base, *g_base, *b_base;
175 	int i;
176 
177 	if (helper->funcs->gamma_get == NULL)
178 		return;
179 
180 	r_base = crtc->gamma_store;
181 	g_base = r_base + crtc->gamma_size;
182 	b_base = g_base + crtc->gamma_size;
183 
184 	for (i = 0; i < crtc->gamma_size; i++)
185 		helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
186 }
187 
188 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
189 {
190 	uint16_t *r_base, *g_base, *b_base;
191 
192 	if (crtc->funcs->gamma_set == NULL)
193 		return;
194 
195 	r_base = crtc->gamma_store;
196 	g_base = r_base + crtc->gamma_size;
197 	b_base = g_base + crtc->gamma_size;
198 
199 	crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
200 }
201 
202 /**
203  * drm_fb_helper_debug_enter - implementation for ->fb_debug_enter
204  * @info: fbdev registered by the helper
205  */
206 int drm_fb_helper_debug_enter(struct fb_info *info)
207 {
208 	struct drm_fb_helper *helper = info->par;
209 	struct drm_crtc_helper_funcs *funcs;
210 	int i;
211 
212 	list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
213 		for (i = 0; i < helper->crtc_count; i++) {
214 			struct drm_mode_set *mode_set =
215 				&helper->crtc_info[i].mode_set;
216 
217 			if (!mode_set->crtc->enabled)
218 				continue;
219 
220 			funcs =	mode_set->crtc->helper_private;
221 			drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
222 			funcs->mode_set_base_atomic(mode_set->crtc,
223 						    mode_set->fb,
224 						    mode_set->x,
225 						    mode_set->y,
226 						    ENTER_ATOMIC_MODE_SET);
227 		}
228 	}
229 
230 	return 0;
231 }
232 EXPORT_SYMBOL(drm_fb_helper_debug_enter);
233 
234 /* Find the real fb for a given fb helper CRTC */
235 static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
236 {
237 	struct drm_device *dev = crtc->dev;
238 	struct drm_crtc *c;
239 
240 	list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
241 		if (crtc->base.id == c->base.id)
242 			return c->primary->fb;
243 	}
244 
245 	return NULL;
246 }
247 
248 /**
249  * drm_fb_helper_debug_leave - implementation for ->fb_debug_leave
250  * @info: fbdev registered by the helper
251  */
252 int drm_fb_helper_debug_leave(struct fb_info *info)
253 {
254 	struct drm_fb_helper *helper = info->par;
255 	struct drm_crtc *crtc;
256 	struct drm_crtc_helper_funcs *funcs;
257 	struct drm_framebuffer *fb;
258 	int i;
259 
260 	for (i = 0; i < helper->crtc_count; i++) {
261 		struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
262 		crtc = mode_set->crtc;
263 		funcs = crtc->helper_private;
264 		fb = drm_mode_config_fb(crtc);
265 
266 		if (!crtc->enabled)
267 			continue;
268 
269 		if (!fb) {
270 			DRM_ERROR("no fb to restore??\n");
271 			continue;
272 		}
273 
274 		drm_fb_helper_restore_lut_atomic(mode_set->crtc);
275 		funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
276 					    crtc->y, LEAVE_ATOMIC_MODE_SET);
277 	}
278 
279 	return 0;
280 }
281 EXPORT_SYMBOL(drm_fb_helper_debug_leave);
282 #endif
283 
284 static bool restore_fbdev_mode(struct drm_fb_helper *fb_helper)
285 {
286 	struct drm_device *dev = fb_helper->dev;
287 	struct drm_plane *plane;
288 	bool error = false;
289 	int i;
290 
291 	drm_warn_on_modeset_not_all_locked(dev);
292 
293 	list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
294 		if (plane->type != DRM_PLANE_TYPE_PRIMARY)
295 			drm_plane_force_disable(plane);
296 
297 		if (dev->mode_config.rotation_property) {
298 			drm_mode_plane_set_obj_prop(plane,
299 						    dev->mode_config.rotation_property,
300 						    BIT(DRM_ROTATE_0));
301 		}
302 	}
303 
304 	for (i = 0; i < fb_helper->crtc_count; i++) {
305 		struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
306 		struct drm_crtc *crtc = mode_set->crtc;
307 		int ret;
308 
309 		if (crtc->funcs->cursor_set) {
310 			ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
311 			if (ret)
312 				error = true;
313 		}
314 
315 		ret = drm_mode_set_config_internal(mode_set);
316 		if (ret)
317 			error = true;
318 	}
319 	return error;
320 }
321 /**
322  * drm_fb_helper_restore_fbdev_mode - restore fbdev configuration
323  * @fb_helper: fbcon to restore
324  *
325  * This should be called from driver's drm ->lastclose callback
326  * when implementing an fbcon on top of kms using this helper. This ensures that
327  * the user isn't greeted with a black screen when e.g. X dies.
328  *
329  * Use this variant if you need to bypass locking (panic), or already
330  * hold all modeset locks.  Otherwise use drm_fb_helper_restore_fbdev_mode_unlocked()
331  */
332 static bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
333 {
334 	return restore_fbdev_mode(fb_helper);
335 }
336 
337 /**
338  * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
339  * @fb_helper: fbcon to restore
340  *
341  * This should be called from driver's drm ->lastclose callback
342  * when implementing an fbcon on top of kms using this helper. This ensures that
343  * the user isn't greeted with a black screen when e.g. X dies.
344  */
345 bool drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
346 {
347 	struct drm_device *dev = fb_helper->dev;
348 	bool ret;
349 	drm_modeset_lock_all(dev);
350 	ret = restore_fbdev_mode(fb_helper);
351 	drm_modeset_unlock_all(dev);
352 	return ret;
353 }
354 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
355 
356 #if 0
357 /*
358  * restore fbcon display for all kms driver's using this helper, used for sysrq
359  * and panic handling.
360  */
361 static bool drm_fb_helper_force_kernel_mode(void)
362 {
363 	bool ret, error = false;
364 	struct drm_fb_helper *helper;
365 
366 	if (list_empty(&kernel_fb_helper_list))
367 		return false;
368 
369 	list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
370 		struct drm_device *dev = helper->dev;
371 
372 		if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
373 			continue;
374 
375 		/* NOTE: we use lockless flag below to avoid grabbing other
376 		 * modeset locks.  So just trylock the underlying mutex
377 		 * directly:
378 		 */
379 		if (!mutex_trylock(&dev->mode_config.mutex)) {
380 			error = true;
381 			continue;
382 		}
383 
384 		ret = drm_fb_helper_restore_fbdev_mode(helper);
385 		if (ret)
386 			error = true;
387 
388 		mutex_unlock(&dev->mode_config.mutex);
389 	}
390 	return error;
391 }
392 
393 static int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
394 			void *panic_str)
395 {
396 	/*
397 	 * It's a waste of time and effort to switch back to text console
398 	 * if the kernel should reboot before panic messages can be seen.
399 	 */
400 	if (panic_timeout < 0)
401 		return 0;
402 
403 	pr_err("panic occurred, switching back to text console\n");
404 	return drm_fb_helper_force_kernel_mode();
405 }
406 
407 static struct notifier_block paniced = {
408 	.notifier_call = drm_fb_helper_panic,
409 };
410 #endif
411 
412 static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
413 {
414 	struct drm_device *dev = fb_helper->dev;
415 	struct drm_crtc *crtc;
416 	int bound = 0, crtcs_bound = 0;
417 
418 #if 0
419 	/* Sometimes user space wants everything disabled, so don't steal the
420 	 * display if there's a master. */
421 	if (dev->primary->master)
422 		return false;
423 #endif
424 
425 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
426 		if (crtc->primary->fb)
427 			crtcs_bound++;
428 		if (crtc->primary->fb == fb_helper->fb)
429 			bound++;
430 	}
431 
432 	if (bound < crtcs_bound)
433 		return false;
434 
435 	return true;
436 }
437 
438 #if 0
439 #ifdef CONFIG_MAGIC_SYSRQ
440 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
441 {
442 	bool ret;
443 	ret = drm_fb_helper_force_kernel_mode();
444 	if (ret == true)
445 		DRM_ERROR("Failed to restore crtc configuration\n");
446 }
447 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
448 
449 static void drm_fb_helper_sysrq(int dummy1)
450 {
451 	schedule_work(&drm_fb_helper_restore_work);
452 }
453 
454 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
455 	.handler = drm_fb_helper_sysrq,
456 	.help_msg = "force-fb(V)",
457 	.action_msg = "Restore framebuffer console",
458 };
459 #else
460 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
461 #endif
462 
463 static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
464 {
465 	struct drm_fb_helper *fb_helper = info->par;
466 	struct drm_device *dev = fb_helper->dev;
467 	struct drm_crtc *crtc;
468 	struct drm_connector *connector;
469 	int i, j;
470 
471 	/*
472 	 * fbdev->blank can be called from irq context in case of a panic.
473 	 * Since we already have our own special panic handler which will
474 	 * restore the fbdev console mode completely, just bail out early.
475 	 */
476 	if (oops_in_progress)
477 		return;
478 
479 	/*
480 	 * For each CRTC in this fb, turn the connectors on/off.
481 	 */
482 	drm_modeset_lock_all(dev);
483 	if (!drm_fb_helper_is_bound(fb_helper)) {
484 		drm_modeset_unlock_all(dev);
485 		return;
486 	}
487 
488 	for (i = 0; i < fb_helper->crtc_count; i++) {
489 		crtc = fb_helper->crtc_info[i].mode_set.crtc;
490 
491 		if (!crtc->enabled)
492 			continue;
493 
494 		/* Walk the connectors & encoders on this fb turning them on/off */
495 		for (j = 0; j < fb_helper->connector_count; j++) {
496 			connector = fb_helper->connector_info[j]->connector;
497 			connector->funcs->dpms(connector, dpms_mode);
498 			drm_object_property_set_value(&connector->base,
499 				dev->mode_config.dpms_property, dpms_mode);
500 		}
501 	}
502 	drm_modeset_unlock_all(dev);
503 }
504 
505 /**
506  * drm_fb_helper_blank - implementation for ->fb_blank
507  * @blank: desired blanking state
508  * @info: fbdev registered by the helper
509  */
510 int drm_fb_helper_blank(int blank, struct fb_info *info)
511 {
512 	switch (blank) {
513 	/* Display: On; HSync: On, VSync: On */
514 	case FB_BLANK_UNBLANK:
515 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
516 		break;
517 	/* Display: Off; HSync: On, VSync: On */
518 	case FB_BLANK_NORMAL:
519 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
520 		break;
521 	/* Display: Off; HSync: Off, VSync: On */
522 	case FB_BLANK_HSYNC_SUSPEND:
523 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
524 		break;
525 	/* Display: Off; HSync: On, VSync: Off */
526 	case FB_BLANK_VSYNC_SUSPEND:
527 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
528 		break;
529 	/* Display: Off; HSync: Off, VSync: Off */
530 	case FB_BLANK_POWERDOWN:
531 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
532 		break;
533 	}
534 	return 0;
535 }
536 EXPORT_SYMBOL(drm_fb_helper_blank);
537 #endif
538 
539 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
540 {
541 	int i;
542 
543 	for (i = 0; i < helper->connector_count; i++)
544 		kfree(helper->connector_info[i]);
545 	kfree(helper->connector_info);
546 	for (i = 0; i < helper->crtc_count; i++) {
547 		kfree(helper->crtc_info[i].mode_set.connectors);
548 		if (helper->crtc_info[i].mode_set.mode)
549 			drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
550 	}
551 	kfree(helper->crtc_info);
552 }
553 
554 /**
555  * drm_fb_helper_prepare - setup a drm_fb_helper structure
556  * @dev: DRM device
557  * @helper: driver-allocated fbdev helper structure to set up
558  * @funcs: pointer to structure of functions associate with this helper
559  *
560  * Sets up the bare minimum to make the framebuffer helper usable. This is
561  * useful to implement race-free initialization of the polling helpers.
562  */
563 void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
564 			   const struct drm_fb_helper_funcs *funcs)
565 {
566 	INIT_LIST_HEAD(&helper->kernel_fb_list);
567 	helper->funcs = funcs;
568 	helper->dev = dev;
569 }
570 EXPORT_SYMBOL(drm_fb_helper_prepare);
571 
572 /**
573  * drm_fb_helper_init - initialize a drm_fb_helper structure
574  * @dev: drm device
575  * @fb_helper: driver-allocated fbdev helper structure to initialize
576  * @crtc_count: maximum number of crtcs to support in this fbdev emulation
577  * @max_conn_count: max connector count
578  *
579  * This allocates the structures for the fbdev helper with the given limits.
580  * Note that this won't yet touch the hardware (through the driver interfaces)
581  * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
582  * to allow driver writes more control over the exact init sequence.
583  *
584  * Drivers must call drm_fb_helper_prepare() before calling this function.
585  *
586  * RETURNS:
587  * Zero if everything went ok, nonzero otherwise.
588  */
589 int drm_fb_helper_init(struct drm_device *dev,
590 		       struct drm_fb_helper *fb_helper,
591 		       int crtc_count, int max_conn_count)
592 {
593 	struct drm_crtc *crtc;
594 	int i;
595 
596 	if (!max_conn_count)
597 		return -EINVAL;
598 
599 	fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
600 	if (!fb_helper->crtc_info)
601 		return -ENOMEM;
602 
603 	fb_helper->crtc_count = crtc_count;
604 	fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
605 	if (!fb_helper->connector_info) {
606 		kfree(fb_helper->crtc_info);
607 		return -ENOMEM;
608 	}
609 	fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
610 	fb_helper->connector_count = 0;
611 
612 	for (i = 0; i < crtc_count; i++) {
613 		fb_helper->crtc_info[i].mode_set.connectors =
614 			kcalloc(max_conn_count,
615 				sizeof(struct drm_connector *),
616 				GFP_KERNEL);
617 
618 		if (!fb_helper->crtc_info[i].mode_set.connectors)
619 			goto out_free;
620 		fb_helper->crtc_info[i].mode_set.num_connectors = 0;
621 	}
622 
623 	i = 0;
624 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
625 		fb_helper->crtc_info[i].mode_set.crtc = crtc;
626 		i++;
627 	}
628 
629 	return 0;
630 out_free:
631 	drm_fb_helper_crtc_free(fb_helper);
632 	return -ENOMEM;
633 }
634 EXPORT_SYMBOL(drm_fb_helper_init);
635 
636 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
637 {
638 	if (!list_empty(&fb_helper->kernel_fb_list)) {
639 		list_del(&fb_helper->kernel_fb_list);
640 		if (list_empty(&kernel_fb_helper_list)) {
641 #if 0
642 			pr_info("drm: unregistered panic notifier\n");
643 			atomic_notifier_chain_unregister(&panic_notifier_list,
644 							 &paniced);
645 			unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
646 #endif
647 		}
648 	}
649 
650 	drm_fb_helper_crtc_free(fb_helper);
651 
652 }
653 EXPORT_SYMBOL(drm_fb_helper_fini);
654 
655 #if 0
656 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
657 		     u16 blue, u16 regno, struct fb_info *info)
658 {
659 	struct drm_fb_helper *fb_helper = info->par;
660 	struct drm_framebuffer *fb = fb_helper->fb;
661 	int pindex;
662 
663 	if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
664 		u32 *palette;
665 		u32 value;
666 		/* place color in psuedopalette */
667 		if (regno > 16)
668 			return -EINVAL;
669 		palette = (u32 *)info->pseudo_palette;
670 		red >>= (16 - info->var.red.length);
671 		green >>= (16 - info->var.green.length);
672 		blue >>= (16 - info->var.blue.length);
673 		value = (red << info->var.red.offset) |
674 			(green << info->var.green.offset) |
675 			(blue << info->var.blue.offset);
676 		if (info->var.transp.length > 0) {
677 			u32 mask = (1 << info->var.transp.length) - 1;
678 			mask <<= info->var.transp.offset;
679 			value |= mask;
680 		}
681 		palette[regno] = value;
682 		return 0;
683 	}
684 
685 	/*
686 	 * The driver really shouldn't advertise pseudo/directcolor
687 	 * visuals if it can't deal with the palette.
688 	 */
689 	if (WARN_ON(!fb_helper->funcs->gamma_set ||
690 		    !fb_helper->funcs->gamma_get))
691 		return -EINVAL;
692 
693 	pindex = regno;
694 
695 	if (fb->bits_per_pixel == 16) {
696 		pindex = regno << 3;
697 
698 		if (fb->depth == 16 && regno > 63)
699 			return -EINVAL;
700 		if (fb->depth == 15 && regno > 31)
701 			return -EINVAL;
702 
703 		if (fb->depth == 16) {
704 			u16 r, g, b;
705 			int i;
706 			if (regno < 32) {
707 				for (i = 0; i < 8; i++)
708 					fb_helper->funcs->gamma_set(crtc, red,
709 						green, blue, pindex + i);
710 			}
711 
712 			fb_helper->funcs->gamma_get(crtc, &r,
713 						    &g, &b,
714 						    pindex >> 1);
715 
716 			for (i = 0; i < 4; i++)
717 				fb_helper->funcs->gamma_set(crtc, r,
718 							    green, b,
719 							    (pindex >> 1) + i);
720 		}
721 	}
722 
723 	if (fb->depth != 16)
724 		fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
725 	return 0;
726 }
727 
728 /**
729  * drm_fb_helper_setcmap - implementation for ->fb_setcmap
730  * @cmap: cmap to set
731  * @info: fbdev registered by the helper
732  */
733 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
734 {
735 	struct drm_fb_helper *fb_helper = info->par;
736 	struct drm_device *dev = fb_helper->dev;
737 	struct drm_crtc_helper_funcs *crtc_funcs;
738 	u16 *red, *green, *blue, *transp;
739 	struct drm_crtc *crtc;
740 	int i, j, rc = 0;
741 	int start;
742 
743 	drm_modeset_lock_all(dev);
744 	if (!drm_fb_helper_is_bound(fb_helper)) {
745 		drm_modeset_unlock_all(dev);
746 		return -EBUSY;
747 	}
748 
749 	for (i = 0; i < fb_helper->crtc_count; i++) {
750 		crtc = fb_helper->crtc_info[i].mode_set.crtc;
751 		crtc_funcs = crtc->helper_private;
752 
753 		red = cmap->red;
754 		green = cmap->green;
755 		blue = cmap->blue;
756 		transp = cmap->transp;
757 		start = cmap->start;
758 
759 		for (j = 0; j < cmap->len; j++) {
760 			u16 hred, hgreen, hblue, htransp = 0xffff;
761 
762 			hred = *red++;
763 			hgreen = *green++;
764 			hblue = *blue++;
765 
766 			if (transp)
767 				htransp = *transp++;
768 
769 			rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
770 			if (rc)
771 				goto out;
772 		}
773 		if (crtc_funcs->load_lut)
774 			crtc_funcs->load_lut(crtc);
775 	}
776  out:
777 	drm_modeset_unlock_all(dev);
778 	return rc;
779 }
780 EXPORT_SYMBOL(drm_fb_helper_setcmap);
781 
782 /**
783  * drm_fb_helper_check_var - implementation for ->fb_check_var
784  * @var: screeninfo to check
785  * @info: fbdev registered by the helper
786  */
787 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
788 			    struct fb_info *info)
789 {
790 	struct drm_fb_helper *fb_helper = info->par;
791 	struct drm_framebuffer *fb = fb_helper->fb;
792 	int depth;
793 
794 	if (var->pixclock != 0 || in_dbg_master())
795 		return -EINVAL;
796 
797 	/* Need to resize the fb object !!! */
798 	if (var->bits_per_pixel > fb->bits_per_pixel ||
799 	    var->xres > fb->width || var->yres > fb->height ||
800 	    var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
801 		DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
802 			  "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
803 			  var->xres, var->yres, var->bits_per_pixel,
804 			  var->xres_virtual, var->yres_virtual,
805 			  fb->width, fb->height, fb->bits_per_pixel);
806 		return -EINVAL;
807 	}
808 
809 	switch (var->bits_per_pixel) {
810 	case 16:
811 		depth = (var->green.length == 6) ? 16 : 15;
812 		break;
813 	case 32:
814 		depth = (var->transp.length > 0) ? 32 : 24;
815 		break;
816 	default:
817 		depth = var->bits_per_pixel;
818 		break;
819 	}
820 
821 	switch (depth) {
822 	case 8:
823 		var->red.offset = 0;
824 		var->green.offset = 0;
825 		var->blue.offset = 0;
826 		var->red.length = 8;
827 		var->green.length = 8;
828 		var->blue.length = 8;
829 		var->transp.length = 0;
830 		var->transp.offset = 0;
831 		break;
832 	case 15:
833 		var->red.offset = 10;
834 		var->green.offset = 5;
835 		var->blue.offset = 0;
836 		var->red.length = 5;
837 		var->green.length = 5;
838 		var->blue.length = 5;
839 		var->transp.length = 1;
840 		var->transp.offset = 15;
841 		break;
842 	case 16:
843 		var->red.offset = 11;
844 		var->green.offset = 5;
845 		var->blue.offset = 0;
846 		var->red.length = 5;
847 		var->green.length = 6;
848 		var->blue.length = 5;
849 		var->transp.length = 0;
850 		var->transp.offset = 0;
851 		break;
852 	case 24:
853 		var->red.offset = 16;
854 		var->green.offset = 8;
855 		var->blue.offset = 0;
856 		var->red.length = 8;
857 		var->green.length = 8;
858 		var->blue.length = 8;
859 		var->transp.length = 0;
860 		var->transp.offset = 0;
861 		break;
862 	case 32:
863 		var->red.offset = 16;
864 		var->green.offset = 8;
865 		var->blue.offset = 0;
866 		var->red.length = 8;
867 		var->green.length = 8;
868 		var->blue.length = 8;
869 		var->transp.length = 8;
870 		var->transp.offset = 24;
871 		break;
872 	default:
873 		return -EINVAL;
874 	}
875 	return 0;
876 }
877 EXPORT_SYMBOL(drm_fb_helper_check_var);
878 
879 /**
880  * drm_fb_helper_set_par - implementation for ->fb_set_par
881  * @info: fbdev registered by the helper
882  *
883  * This will let fbcon do the mode init and is called at initialization time by
884  * the fbdev core when registering the driver, and later on through the hotplug
885  * callback.
886  */
887 int drm_fb_helper_set_par(struct fb_info *info)
888 {
889 	struct drm_fb_helper *fb_helper = info->par;
890 	struct fb_var_screeninfo *var = &info->var;
891 
892 	if (var->pixclock != 0) {
893 		DRM_ERROR("PIXEL CLOCK SET\n");
894 		return -EINVAL;
895 	}
896 
897 	drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
898 
899 	if (fb_helper->delayed_hotplug) {
900 		fb_helper->delayed_hotplug = false;
901 		drm_fb_helper_hotplug_event(fb_helper);
902 	}
903 	return 0;
904 }
905 EXPORT_SYMBOL(drm_fb_helper_set_par);
906 
907 /**
908  * drm_fb_helper_pan_display - implementation for ->fb_pan_display
909  * @var: updated screen information
910  * @info: fbdev registered by the helper
911  */
912 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
913 			      struct fb_info *info)
914 {
915 	struct drm_fb_helper *fb_helper = info->par;
916 	struct drm_device *dev = fb_helper->dev;
917 	struct drm_mode_set *modeset;
918 	int ret = 0;
919 	int i;
920 
921 	drm_modeset_lock_all(dev);
922 	if (!drm_fb_helper_is_bound(fb_helper)) {
923 		drm_modeset_unlock_all(dev);
924 		return -EBUSY;
925 	}
926 
927 	for (i = 0; i < fb_helper->crtc_count; i++) {
928 		modeset = &fb_helper->crtc_info[i].mode_set;
929 
930 		modeset->x = var->xoffset;
931 		modeset->y = var->yoffset;
932 
933 		if (modeset->num_connectors) {
934 			ret = drm_mode_set_config_internal(modeset);
935 			if (!ret) {
936 				info->var.xoffset = var->xoffset;
937 				info->var.yoffset = var->yoffset;
938 			}
939 		}
940 	}
941 	drm_modeset_unlock_all(dev);
942 	return ret;
943 }
944 EXPORT_SYMBOL(drm_fb_helper_pan_display);
945 #endif
946 
947 #ifdef __DragonFly__
948 static void
949 do_restore_fbdev_mode(void *context, int pending)
950 {
951 	struct drm_fb_helper *fb_helper = context;
952 	struct drm_device *dev = fb_helper->dev;
953 
954 	if (!fb_helper->fb)
955 		return;
956 
957 	drm_modeset_lock_all(dev);
958 	drm_fb_helper_restore_fbdev_mode(fb_helper);
959 	drm_modeset_unlock_all(dev);
960 }
961 
962 static void
963 sc_restore_fbdev_mode(void *cookie)
964 {
965 	struct drm_fb_helper *fb_helper = cookie;
966 
967 	if (!fb_helper->fb)
968 		return;
969 
970 	taskqueue_enqueue(taskqueue_thread[0], &fb_helper->fb_mode_task);
971 }
972 #endif
973 
974 /*
975  * Allocates the backing storage and sets up the fbdev info structure through
976  * the ->fb_probe callback and then registers the fbdev and sets up the panic
977  * notifier.
978  */
979 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
980 					 int preferred_bpp)
981 {
982 	int ret = 0;
983 	int crtc_count = 0;
984 	int i;
985 	struct fb_info *info;
986 	struct drm_fb_helper_surface_size sizes;
987 	int gamma_size = 0;
988 #ifdef __DragonFly__
989 	int kms_console = 1;
990 #endif
991 
992 	memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
993 	sizes.surface_depth = 24;
994 	sizes.surface_bpp = 32;
995 	sizes.fb_width = (unsigned)-1;
996 	sizes.fb_height = (unsigned)-1;
997 
998 	/* if driver picks 8 or 16 by default use that
999 	   for both depth/bpp */
1000 	if (preferred_bpp != sizes.surface_bpp)
1001 		sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
1002 
1003 	/* first up get a count of crtcs now in use and new min/maxes width/heights */
1004 	for (i = 0; i < fb_helper->connector_count; i++) {
1005 		struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
1006 		struct drm_cmdline_mode *cmdline_mode;
1007 
1008 		cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
1009 
1010 		if (cmdline_mode->bpp_specified) {
1011 			switch (cmdline_mode->bpp) {
1012 			case 8:
1013 				sizes.surface_depth = sizes.surface_bpp = 8;
1014 				break;
1015 			case 15:
1016 				sizes.surface_depth = 15;
1017 				sizes.surface_bpp = 16;
1018 				break;
1019 			case 16:
1020 				sizes.surface_depth = sizes.surface_bpp = 16;
1021 				break;
1022 			case 24:
1023 				sizes.surface_depth = sizes.surface_bpp = 24;
1024 				break;
1025 			case 32:
1026 				sizes.surface_depth = 24;
1027 				sizes.surface_bpp = 32;
1028 				break;
1029 			}
1030 			break;
1031 		}
1032 	}
1033 
1034 	crtc_count = 0;
1035 	for (i = 0; i < fb_helper->crtc_count; i++) {
1036 		struct drm_display_mode *desired_mode;
1037 		desired_mode = fb_helper->crtc_info[i].desired_mode;
1038 
1039 		if (desired_mode) {
1040 			if (gamma_size == 0)
1041 				gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1042 			if (desired_mode->hdisplay < sizes.fb_width)
1043 				sizes.fb_width = desired_mode->hdisplay;
1044 			if (desired_mode->vdisplay < sizes.fb_height)
1045 				sizes.fb_height = desired_mode->vdisplay;
1046 			if (desired_mode->hdisplay > sizes.surface_width)
1047 				sizes.surface_width = desired_mode->hdisplay;
1048 			if (desired_mode->vdisplay > sizes.surface_height)
1049 				sizes.surface_height = desired_mode->vdisplay;
1050 			crtc_count++;
1051 		}
1052 	}
1053 
1054 	if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
1055 		/* hmm everyone went away - assume VGA cable just fell out
1056 		   and will come back later. */
1057 		DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
1058 		sizes.fb_width = sizes.surface_width = 1024;
1059 		sizes.fb_height = sizes.surface_height = 768;
1060 	}
1061 
1062 	/* push down into drivers */
1063 	ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1064 	if (ret < 0)
1065 		return ret;
1066 
1067 	info = fb_helper->fbdev;
1068 
1069 	/*
1070 	 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1071 	 * events, but at init time drm_setup_crtcs needs to be called before
1072 	 * the fb is allocated (since we need to figure out the desired size of
1073 	 * the fb before we can allocate it ...). Hence we need to fix things up
1074 	 * here again.
1075 	 */
1076 	for (i = 0; i < fb_helper->crtc_count; i++)
1077 		if (fb_helper->crtc_info[i].mode_set.num_connectors)
1078 			fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1079 
1080 #ifdef __DragonFly__
1081 	TUNABLE_INT_FETCH("kern.kms_console", &kms_console);
1082 	if (kms_console) {
1083 		TASK_INIT(&fb_helper->fb_mode_task, 0, do_restore_fbdev_mode,
1084 		    fb_helper);
1085 		info->cookie = fb_helper;
1086 		info->restore = (void *)&sc_restore_fbdev_mode;
1087 		if (register_framebuffer(info) < 0)
1088 			return -EINVAL;
1089 	}
1090 #endif
1091 
1092 #if 0
1093 	info->var.pixclock = 0;
1094 	if (register_framebuffer(info) < 0)
1095 		return -EINVAL;
1096 
1097 	dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
1098 			info->node, info->fix.id);
1099 
1100 	/* Switch back to kernel console on panic */
1101 	/* multi card linked list maybe */
1102 	if (list_empty(&kernel_fb_helper_list)) {
1103 		dev_info(fb_helper->dev->dev, "registered panic notifier\n");
1104 		atomic_notifier_chain_register(&panic_notifier_list,
1105 					       &paniced);
1106 		register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
1107 	}
1108 
1109 	list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
1110 #endif
1111 
1112 	return 0;
1113 }
1114 
1115 #if 0
1116 /**
1117  * drm_fb_helper_fill_fix - initializes fixed fbdev information
1118  * @info: fbdev registered by the helper
1119  * @pitch: desired pitch
1120  * @depth: desired depth
1121  *
1122  * Helper to fill in the fixed fbdev information useful for a non-accelerated
1123  * fbdev emulations. Drivers which support acceleration methods which impose
1124  * additional constraints need to set up their own limits.
1125  *
1126  * Drivers should call this (or their equivalent setup code) from their
1127  * ->fb_probe callback.
1128  */
1129 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1130 			    uint32_t depth)
1131 {
1132 	info->fix.type = FB_TYPE_PACKED_PIXELS;
1133 	info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1134 		FB_VISUAL_TRUECOLOR;
1135 	info->fix.mmio_start = 0;
1136 	info->fix.mmio_len = 0;
1137 	info->fix.type_aux = 0;
1138 	info->fix.xpanstep = 1; /* doing it in hw */
1139 	info->fix.ypanstep = 1; /* doing it in hw */
1140 	info->fix.ywrapstep = 0;
1141 	info->fix.accel = FB_ACCEL_NONE;
1142 
1143 	info->fix.line_length = pitch;
1144 	return;
1145 }
1146 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1147 
1148 /**
1149  * drm_fb_helper_fill_var - initalizes variable fbdev information
1150  * @info: fbdev instance to set up
1151  * @fb_helper: fb helper instance to use as template
1152  * @fb_width: desired fb width
1153  * @fb_height: desired fb height
1154  *
1155  * Sets up the variable fbdev metainformation from the given fb helper instance
1156  * and the drm framebuffer allocated in fb_helper->fb.
1157  *
1158  * Drivers should call this (or their equivalent setup code) from their
1159  * ->fb_probe callback after having allocated the fbdev backing
1160  * storage framebuffer.
1161  */
1162 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
1163 			    uint32_t fb_width, uint32_t fb_height)
1164 {
1165 	struct drm_framebuffer *fb = fb_helper->fb;
1166 	info->pseudo_palette = fb_helper->pseudo_palette;
1167 	info->var.xres_virtual = fb->width;
1168 	info->var.yres_virtual = fb->height;
1169 	info->var.bits_per_pixel = fb->bits_per_pixel;
1170 	info->var.accel_flags = FB_ACCELF_TEXT;
1171 	info->var.xoffset = 0;
1172 	info->var.yoffset = 0;
1173 	info->var.activate = FB_ACTIVATE_NOW;
1174 	info->var.height = -1;
1175 	info->var.width = -1;
1176 
1177 	switch (fb->depth) {
1178 	case 8:
1179 		info->var.red.offset = 0;
1180 		info->var.green.offset = 0;
1181 		info->var.blue.offset = 0;
1182 		info->var.red.length = 8; /* 8bit DAC */
1183 		info->var.green.length = 8;
1184 		info->var.blue.length = 8;
1185 		info->var.transp.offset = 0;
1186 		info->var.transp.length = 0;
1187 		break;
1188 	case 15:
1189 		info->var.red.offset = 10;
1190 		info->var.green.offset = 5;
1191 		info->var.blue.offset = 0;
1192 		info->var.red.length = 5;
1193 		info->var.green.length = 5;
1194 		info->var.blue.length = 5;
1195 		info->var.transp.offset = 15;
1196 		info->var.transp.length = 1;
1197 		break;
1198 	case 16:
1199 		info->var.red.offset = 11;
1200 		info->var.green.offset = 5;
1201 		info->var.blue.offset = 0;
1202 		info->var.red.length = 5;
1203 		info->var.green.length = 6;
1204 		info->var.blue.length = 5;
1205 		info->var.transp.offset = 0;
1206 		break;
1207 	case 24:
1208 		info->var.red.offset = 16;
1209 		info->var.green.offset = 8;
1210 		info->var.blue.offset = 0;
1211 		info->var.red.length = 8;
1212 		info->var.green.length = 8;
1213 		info->var.blue.length = 8;
1214 		info->var.transp.offset = 0;
1215 		info->var.transp.length = 0;
1216 		break;
1217 	case 32:
1218 		info->var.red.offset = 16;
1219 		info->var.green.offset = 8;
1220 		info->var.blue.offset = 0;
1221 		info->var.red.length = 8;
1222 		info->var.green.length = 8;
1223 		info->var.blue.length = 8;
1224 		info->var.transp.offset = 24;
1225 		info->var.transp.length = 8;
1226 		break;
1227 	default:
1228 		break;
1229 	}
1230 
1231 	info->var.xres = fb_width;
1232 	info->var.yres = fb_height;
1233 }
1234 EXPORT_SYMBOL(drm_fb_helper_fill_var);
1235 #endif
1236 
1237 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1238 					       uint32_t maxX,
1239 					       uint32_t maxY)
1240 {
1241 	struct drm_connector *connector;
1242 	int count = 0;
1243 	int i;
1244 
1245 	for (i = 0; i < fb_helper->connector_count; i++) {
1246 		connector = fb_helper->connector_info[i]->connector;
1247 		count += connector->funcs->fill_modes(connector, maxX, maxY);
1248 	}
1249 
1250 	return count;
1251 }
1252 
1253 struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1254 {
1255 	struct drm_display_mode *mode;
1256 
1257 	list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1258 		if (mode->hdisplay > width ||
1259 		    mode->vdisplay > height)
1260 			continue;
1261 		if (mode->type & DRM_MODE_TYPE_PREFERRED)
1262 			return mode;
1263 	}
1264 	return NULL;
1265 }
1266 EXPORT_SYMBOL(drm_has_preferred_mode);
1267 
1268 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1269 {
1270 	return fb_connector->connector->cmdline_mode.specified;
1271 }
1272 
1273 struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1274 						      int width, int height)
1275 {
1276 	struct drm_cmdline_mode *cmdline_mode;
1277 	struct drm_display_mode *mode = NULL;
1278 	bool prefer_non_interlace;
1279 
1280 	cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
1281 	if (cmdline_mode->specified == false)
1282 		return mode;
1283 
1284 	/* attempt to find a matching mode in the list of modes
1285 	 *  we have gotten so far, if not add a CVT mode that conforms
1286 	 */
1287 	if (cmdline_mode->rb || cmdline_mode->margins)
1288 		goto create_mode;
1289 
1290 	prefer_non_interlace = !cmdline_mode->interlace;
1291  again:
1292 	list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1293 		/* check width/height */
1294 		if (mode->hdisplay != cmdline_mode->xres ||
1295 		    mode->vdisplay != cmdline_mode->yres)
1296 			continue;
1297 
1298 		if (cmdline_mode->refresh_specified) {
1299 			if (mode->vrefresh != cmdline_mode->refresh)
1300 				continue;
1301 		}
1302 
1303 		if (cmdline_mode->interlace) {
1304 			if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1305 				continue;
1306 		} else if (prefer_non_interlace) {
1307 			if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1308 				continue;
1309 		}
1310 		return mode;
1311 	}
1312 
1313 	if (prefer_non_interlace) {
1314 		prefer_non_interlace = false;
1315 		goto again;
1316 	}
1317 
1318 create_mode:
1319 	mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1320 						 cmdline_mode);
1321 	list_add(&mode->head, &fb_helper_conn->connector->modes);
1322 	return mode;
1323 }
1324 EXPORT_SYMBOL(drm_pick_cmdline_mode);
1325 
1326 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1327 {
1328 	bool enable;
1329 
1330 	if (strict)
1331 		enable = connector->status == connector_status_connected;
1332 	else
1333 		enable = connector->status != connector_status_disconnected;
1334 
1335 	return enable;
1336 }
1337 
1338 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1339 				  bool *enabled)
1340 {
1341 	bool any_enabled = false;
1342 	struct drm_connector *connector;
1343 	int i = 0;
1344 
1345 	for (i = 0; i < fb_helper->connector_count; i++) {
1346 		connector = fb_helper->connector_info[i]->connector;
1347 		enabled[i] = drm_connector_enabled(connector, true);
1348 		DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1349 			  enabled[i] ? "yes" : "no");
1350 		any_enabled |= enabled[i];
1351 	}
1352 
1353 	if (any_enabled)
1354 		return;
1355 
1356 	for (i = 0; i < fb_helper->connector_count; i++) {
1357 		connector = fb_helper->connector_info[i]->connector;
1358 		enabled[i] = drm_connector_enabled(connector, false);
1359 	}
1360 }
1361 
1362 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1363 			      struct drm_display_mode **modes,
1364 			      bool *enabled, int width, int height)
1365 {
1366 	int count, i, j;
1367 	bool can_clone = false;
1368 	struct drm_fb_helper_connector *fb_helper_conn;
1369 	struct drm_display_mode *dmt_mode, *mode;
1370 
1371 	/* only contemplate cloning in the single crtc case */
1372 	if (fb_helper->crtc_count > 1)
1373 		return false;
1374 
1375 	count = 0;
1376 	for (i = 0; i < fb_helper->connector_count; i++) {
1377 		if (enabled[i])
1378 			count++;
1379 	}
1380 
1381 	/* only contemplate cloning if more than one connector is enabled */
1382 	if (count <= 1)
1383 		return false;
1384 
1385 	/* check the command line or if nothing common pick 1024x768 */
1386 	can_clone = true;
1387 	for (i = 0; i < fb_helper->connector_count; i++) {
1388 		if (!enabled[i])
1389 			continue;
1390 		fb_helper_conn = fb_helper->connector_info[i];
1391 		modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1392 		if (!modes[i]) {
1393 			can_clone = false;
1394 			break;
1395 		}
1396 		for (j = 0; j < i; j++) {
1397 			if (!enabled[j])
1398 				continue;
1399 			if (!drm_mode_equal(modes[j], modes[i]))
1400 				can_clone = false;
1401 		}
1402 	}
1403 
1404 	if (can_clone) {
1405 		DRM_DEBUG_KMS("can clone using command line\n");
1406 		return true;
1407 	}
1408 
1409 	/* try and find a 1024x768 mode on each connector */
1410 	can_clone = true;
1411 	dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
1412 
1413 	for (i = 0; i < fb_helper->connector_count; i++) {
1414 
1415 		if (!enabled[i])
1416 			continue;
1417 
1418 		fb_helper_conn = fb_helper->connector_info[i];
1419 		list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1420 			if (drm_mode_equal(mode, dmt_mode))
1421 				modes[i] = mode;
1422 		}
1423 		if (!modes[i])
1424 			can_clone = false;
1425 	}
1426 
1427 	if (can_clone) {
1428 		DRM_DEBUG_KMS("can clone using 1024x768\n");
1429 		return true;
1430 	}
1431 	DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1432 	return false;
1433 }
1434 
1435 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1436 				 struct drm_display_mode **modes,
1437 				 bool *enabled, int width, int height)
1438 {
1439 	struct drm_fb_helper_connector *fb_helper_conn;
1440 	int i;
1441 
1442 	for (i = 0; i < fb_helper->connector_count; i++) {
1443 		fb_helper_conn = fb_helper->connector_info[i];
1444 
1445 		if (enabled[i] == false)
1446 			continue;
1447 
1448 		DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1449 			      fb_helper_conn->connector->base.id);
1450 
1451 		/* got for command line mode first */
1452 		modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1453 		if (!modes[i]) {
1454 			DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1455 				      fb_helper_conn->connector->base.id);
1456 			modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1457 		}
1458 		/* No preferred modes, pick one off the list */
1459 		if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1460 			list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1461 				break;
1462 		}
1463 		DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1464 			  "none");
1465 	}
1466 	return true;
1467 }
1468 
1469 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1470 			  struct drm_fb_helper_crtc **best_crtcs,
1471 			  struct drm_display_mode **modes,
1472 			  int n, int width, int height)
1473 {
1474 	int c, o;
1475 	struct drm_device *dev = fb_helper->dev;
1476 	struct drm_connector *connector;
1477 	struct drm_connector_helper_funcs *connector_funcs;
1478 	struct drm_encoder *encoder;
1479 	int my_score, best_score, score;
1480 	struct drm_fb_helper_crtc **crtcs, *crtc;
1481 	struct drm_fb_helper_connector *fb_helper_conn;
1482 
1483 	if (n == fb_helper->connector_count)
1484 		return 0;
1485 
1486 	fb_helper_conn = fb_helper->connector_info[n];
1487 	connector = fb_helper_conn->connector;
1488 
1489 	best_crtcs[n] = NULL;
1490 	best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1491 	if (modes[n] == NULL)
1492 		return best_score;
1493 
1494 	crtcs = kzalloc(dev->mode_config.num_connector *
1495 			sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1496 	if (!crtcs)
1497 		return best_score;
1498 
1499 	my_score = 1;
1500 	if (connector->status == connector_status_connected)
1501 		my_score++;
1502 	if (drm_has_cmdline_mode(fb_helper_conn))
1503 		my_score++;
1504 	if (drm_has_preferred_mode(fb_helper_conn, width, height))
1505 		my_score++;
1506 
1507 	connector_funcs = connector->helper_private;
1508 	encoder = connector_funcs->best_encoder(connector);
1509 	if (!encoder)
1510 		goto out;
1511 
1512 	/* select a crtc for this connector and then attempt to configure
1513 	   remaining connectors */
1514 	for (c = 0; c < fb_helper->crtc_count; c++) {
1515 		crtc = &fb_helper->crtc_info[c];
1516 
1517 		if ((encoder->possible_crtcs & (1 << c)) == 0)
1518 			continue;
1519 
1520 		for (o = 0; o < n; o++)
1521 			if (best_crtcs[o] == crtc)
1522 				break;
1523 
1524 		if (o < n) {
1525 			/* ignore cloning unless only a single crtc */
1526 			if (fb_helper->crtc_count > 1)
1527 				continue;
1528 
1529 			if (!drm_mode_equal(modes[o], modes[n]))
1530 				continue;
1531 		}
1532 
1533 		crtcs[n] = crtc;
1534 		memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1535 		score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1536 						  width, height);
1537 		if (score > best_score) {
1538 			best_score = score;
1539 			memcpy(best_crtcs, crtcs,
1540 			       dev->mode_config.num_connector *
1541 			       sizeof(struct drm_fb_helper_crtc *));
1542 		}
1543 	}
1544 out:
1545 	kfree(crtcs);
1546 	return best_score;
1547 }
1548 
1549 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1550 {
1551 	struct drm_device *dev = fb_helper->dev;
1552 	struct drm_fb_helper_crtc **crtcs;
1553 	struct drm_display_mode **modes;
1554 	struct drm_mode_set *modeset;
1555 	bool *enabled;
1556 	int width, height;
1557 	int i;
1558 
1559 	DRM_DEBUG_KMS("\n");
1560 
1561 	width = dev->mode_config.max_width;
1562 	height = dev->mode_config.max_height;
1563 
1564 	crtcs = kcalloc(dev->mode_config.num_connector,
1565 			sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1566 	modes = kcalloc(dev->mode_config.num_connector,
1567 			sizeof(struct drm_display_mode *), GFP_KERNEL);
1568 	enabled = kcalloc(dev->mode_config.num_connector,
1569 			  sizeof(bool), GFP_KERNEL);
1570 	if (!crtcs || !modes || !enabled) {
1571 		DRM_ERROR("Memory allocation failed\n");
1572 		goto out;
1573 	}
1574 
1575 
1576 	drm_enable_connectors(fb_helper, enabled);
1577 
1578 	if (!(fb_helper->funcs->initial_config &&
1579 	      fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
1580 					       enabled, width, height))) {
1581 		memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0]));
1582 		memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0]));
1583 
1584 		if (!drm_target_cloned(fb_helper,
1585 				       modes, enabled, width, height) &&
1586 		    !drm_target_preferred(fb_helper,
1587 					  modes, enabled, width, height))
1588 			DRM_ERROR("Unable to find initial modes\n");
1589 
1590 		DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
1591 			      width, height);
1592 
1593 		drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1594 	}
1595 
1596 	/* need to set the modesets up here for use later */
1597 	/* fill out the connector<->crtc mappings into the modesets */
1598 	for (i = 0; i < fb_helper->crtc_count; i++) {
1599 		modeset = &fb_helper->crtc_info[i].mode_set;
1600 		modeset->num_connectors = 0;
1601 		modeset->fb = NULL;
1602 	}
1603 
1604 	for (i = 0; i < fb_helper->connector_count; i++) {
1605 		struct drm_display_mode *mode = modes[i];
1606 		struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1607 		modeset = &fb_crtc->mode_set;
1608 
1609 		if (mode && fb_crtc) {
1610 			DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1611 				      mode->name, fb_crtc->mode_set.crtc->base.id);
1612 			fb_crtc->desired_mode = mode;
1613 			if (modeset->mode)
1614 				drm_mode_destroy(dev, modeset->mode);
1615 			modeset->mode = drm_mode_duplicate(dev,
1616 							   fb_crtc->desired_mode);
1617 			modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1618 			modeset->fb = fb_helper->fb;
1619 		}
1620 	}
1621 
1622 	/* Clear out any old modes if there are no more connected outputs. */
1623 	for (i = 0; i < fb_helper->crtc_count; i++) {
1624 		modeset = &fb_helper->crtc_info[i].mode_set;
1625 		if (modeset->num_connectors == 0) {
1626 			BUG_ON(modeset->fb);
1627 			BUG_ON(modeset->num_connectors);
1628 			if (modeset->mode)
1629 				drm_mode_destroy(dev, modeset->mode);
1630 			modeset->mode = NULL;
1631 		}
1632 	}
1633 out:
1634 	kfree(crtcs);
1635 	kfree(modes);
1636 	kfree(enabled);
1637 }
1638 
1639 /**
1640  * drm_fb_helper_initial_config - setup a sane initial connector configuration
1641  * @fb_helper: fb_helper device struct
1642  * @bpp_sel: bpp value to use for the framebuffer configuration
1643  *
1644  * Scans the CRTCs and connectors and tries to put together an initial setup.
1645  * At the moment, this is a cloned configuration across all heads with
1646  * a new framebuffer object as the backing store.
1647  *
1648  * Note that this also registers the fbdev and so allows userspace to call into
1649  * the driver through the fbdev interfaces.
1650  *
1651  * This function will call down into the ->fb_probe callback to let
1652  * the driver allocate and initialize the fbdev info structure and the drm
1653  * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
1654  * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
1655  * values for the fbdev info structure.
1656  *
1657  * RETURNS:
1658  * Zero if everything went ok, nonzero otherwise.
1659  */
1660 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1661 {
1662 	struct drm_device *dev = fb_helper->dev;
1663 	int count = 0;
1664 
1665 	mutex_lock(&dev->mode_config.mutex);
1666 	count = drm_fb_helper_probe_connector_modes(fb_helper,
1667 						    dev->mode_config.max_width,
1668 						    dev->mode_config.max_height);
1669 	mutex_unlock(&dev->mode_config.mutex);
1670 	/*
1671 	 * we shouldn't end up with no modes here.
1672 	 */
1673 	if (count == 0)
1674 		dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
1675 
1676 	drm_setup_crtcs(fb_helper);
1677 
1678 	return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1679 }
1680 EXPORT_SYMBOL(drm_fb_helper_initial_config);
1681 
1682 /**
1683  * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1684  *                               probing all the outputs attached to the fb
1685  * @fb_helper: the drm_fb_helper
1686  *
1687  * Scan the connectors attached to the fb_helper and try to put together a
1688  * setup after *notification of a change in output configuration.
1689  *
1690  * Called at runtime, takes the mode config locks to be able to check/change the
1691  * modeset configuration. Must be run from process context (which usually means
1692  * either the output polling work or a work item launched from the driver's
1693  * hotplug interrupt).
1694  *
1695  * Note that drivers may call this even before calling
1696  * drm_fb_helper_initial_config but only aftert drm_fb_helper_init. This allows
1697  * for a race-free fbcon setup and will make sure that the fbdev emulation will
1698  * not miss any hotplug events.
1699  *
1700  * RETURNS:
1701  * 0 on success and a non-zero error code otherwise.
1702  */
1703 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1704 {
1705 	struct drm_device *dev = fb_helper->dev;
1706 	u32 max_width, max_height;
1707 
1708 	mutex_lock(&fb_helper->dev->mode_config.mutex);
1709 	if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
1710 		fb_helper->delayed_hotplug = true;
1711 		mutex_unlock(&fb_helper->dev->mode_config.mutex);
1712 		return 0;
1713 	}
1714 	DRM_DEBUG_KMS("\n");
1715 
1716 	max_width = fb_helper->fb->width;
1717 	max_height = fb_helper->fb->height;
1718 
1719 	drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height);
1720 	mutex_unlock(&fb_helper->dev->mode_config.mutex);
1721 
1722 	drm_modeset_lock_all(dev);
1723 	drm_setup_crtcs(fb_helper);
1724 	drm_modeset_unlock_all(dev);
1725 #if 0
1726 	drm_fb_helper_set_par(fb_helper->fbdev);
1727 #endif
1728 
1729 	return 0;
1730 }
1731 EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
1732 
1733 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
1734  * but the module doesn't depend on any fb console symbols.  At least
1735  * attempt to load fbcon to avoid leaving the system without a usable console.
1736  */
1737 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
1738 static int __init drm_fb_helper_modinit(void)
1739 {
1740 	const char *name = "fbcon";
1741 	struct module *fbcon;
1742 
1743 	mutex_lock(&module_mutex);
1744 	fbcon = find_module(name);
1745 	mutex_unlock(&module_mutex);
1746 
1747 	if (!fbcon)
1748 		request_module_nowait(name);
1749 	return 0;
1750 }
1751 
1752 module_init(drm_fb_helper_modinit);
1753 #endif
1754