xref: /dragonfly/sys/dev/drm/drm_fb_helper.c (revision aabd9311)
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_unlocked() from their ->lastclose callback.
57  * They 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  * This function is protected against concurrent connector hotadds/removals
90  * using drm_fb_helper_add_one_connector() and
91  * drm_fb_helper_remove_one_connector().
92  */
93 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
94 {
95 	struct drm_device *dev = fb_helper->dev;
96 	struct drm_connector *connector;
97 	int i;
98 
99 	mutex_lock(&dev->mode_config.mutex);
100 	drm_for_each_connector(connector, dev) {
101 		struct drm_fb_helper_connector *fb_helper_connector;
102 
103 		fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
104 		if (!fb_helper_connector)
105 			goto fail;
106 
107 		fb_helper_connector->connector = connector;
108 		fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
109 	}
110 	mutex_unlock(&dev->mode_config.mutex);
111 	return 0;
112 fail:
113 	for (i = 0; i < fb_helper->connector_count; i++) {
114 		kfree(fb_helper->connector_info[i]);
115 		fb_helper->connector_info[i] = NULL;
116 	}
117 	fb_helper->connector_count = 0;
118 	mutex_unlock(&dev->mode_config.mutex);
119 
120 	return -ENOMEM;
121 }
122 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
123 
124 int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, struct drm_connector *connector)
125 {
126 	struct drm_fb_helper_connector **temp;
127 	struct drm_fb_helper_connector *fb_helper_connector;
128 
129 	WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
130 	if (fb_helper->connector_count + 1 > fb_helper->connector_info_alloc_count) {
131 		temp = krealloc(fb_helper->connector_info, sizeof(struct drm_fb_helper_connector *) * (fb_helper->connector_count + 1), M_DRM, M_WAITOK);
132 		if (!temp)
133 			return -ENOMEM;
134 
135 		fb_helper->connector_info_alloc_count = fb_helper->connector_count + 1;
136 		fb_helper->connector_info = temp;
137 	}
138 
139 
140 	fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
141 	if (!fb_helper_connector)
142 		return -ENOMEM;
143 
144 	fb_helper_connector->connector = connector;
145 	fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
146 	return 0;
147 }
148 EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
149 
150 static void remove_from_modeset(struct drm_mode_set *set,
151 		struct drm_connector *connector)
152 {
153 	int i, j;
154 
155 	for (i = 0; i < set->num_connectors; i++) {
156 		if (set->connectors[i] == connector)
157 			break;
158 	}
159 
160 	if (i == set->num_connectors)
161 		return;
162 
163 	for (j = i + 1; j < set->num_connectors; j++) {
164 		set->connectors[j - 1] = set->connectors[j];
165 	}
166 	set->num_connectors--;
167 
168 	/*
169 	 * TODO maybe need to makes sure we set it back to !=NULL somewhere?
170 	 */
171 	if (set->num_connectors == 0) {
172 		set->fb = NULL;
173 		drm_mode_destroy(connector->dev, set->mode);
174 		set->mode = NULL;
175 	}
176 }
177 
178 int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
179 				       struct drm_connector *connector)
180 {
181 	struct drm_fb_helper_connector *fb_helper_connector;
182 	int i, j;
183 
184 	WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
185 
186 	for (i = 0; i < fb_helper->connector_count; i++) {
187 		if (fb_helper->connector_info[i]->connector == connector)
188 			break;
189 	}
190 
191 	if (i == fb_helper->connector_count)
192 		return -EINVAL;
193 	fb_helper_connector = fb_helper->connector_info[i];
194 
195 	for (j = i + 1; j < fb_helper->connector_count; j++) {
196 		fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
197 	}
198 	fb_helper->connector_count--;
199 	kfree(fb_helper_connector);
200 
201 	/* also cleanup dangling references to the connector: */
202 	for (i = 0; i < fb_helper->crtc_count; i++)
203 		remove_from_modeset(&fb_helper->crtc_info[i].mode_set, connector);
204 
205 	return 0;
206 }
207 EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
208 
209 #if 0
210 static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
211 {
212 	uint16_t *r_base, *g_base, *b_base;
213 	int i;
214 
215 	if (helper->funcs->gamma_get == NULL)
216 		return;
217 
218 	r_base = crtc->gamma_store;
219 	g_base = r_base + crtc->gamma_size;
220 	b_base = g_base + crtc->gamma_size;
221 
222 	for (i = 0; i < crtc->gamma_size; i++)
223 		helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
224 }
225 
226 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
227 {
228 	uint16_t *r_base, *g_base, *b_base;
229 
230 	if (crtc->funcs->gamma_set == NULL)
231 		return;
232 
233 	r_base = crtc->gamma_store;
234 	g_base = r_base + crtc->gamma_size;
235 	b_base = g_base + crtc->gamma_size;
236 
237 	crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
238 }
239 
240 /**
241  * drm_fb_helper_debug_enter - implementation for ->fb_debug_enter
242  * @info: fbdev registered by the helper
243  */
244 int drm_fb_helper_debug_enter(struct fb_info *info)
245 {
246 	struct drm_fb_helper *helper = info->par;
247 	const struct drm_crtc_helper_funcs *funcs;
248 	int i;
249 
250 	list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
251 		for (i = 0; i < helper->crtc_count; i++) {
252 			struct drm_mode_set *mode_set =
253 				&helper->crtc_info[i].mode_set;
254 
255 			if (!mode_set->crtc->enabled)
256 				continue;
257 
258 			funcs =	mode_set->crtc->helper_private;
259 			drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
260 			funcs->mode_set_base_atomic(mode_set->crtc,
261 						    mode_set->fb,
262 						    mode_set->x,
263 						    mode_set->y,
264 						    ENTER_ATOMIC_MODE_SET);
265 		}
266 	}
267 
268 	return 0;
269 }
270 EXPORT_SYMBOL(drm_fb_helper_debug_enter);
271 
272 /* Find the real fb for a given fb helper CRTC */
273 static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
274 {
275 	struct drm_device *dev = crtc->dev;
276 	struct drm_crtc *c;
277 
278 	drm_for_each_crtc(c, dev) {
279 		if (crtc->base.id == c->base.id)
280 			return c->primary->fb;
281 	}
282 
283 	return NULL;
284 }
285 
286 /**
287  * drm_fb_helper_debug_leave - implementation for ->fb_debug_leave
288  * @info: fbdev registered by the helper
289  */
290 int drm_fb_helper_debug_leave(struct fb_info *info)
291 {
292 	struct drm_fb_helper *helper = info->par;
293 	struct drm_crtc *crtc;
294 	const struct drm_crtc_helper_funcs *funcs;
295 	struct drm_framebuffer *fb;
296 	int i;
297 
298 	for (i = 0; i < helper->crtc_count; i++) {
299 		struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
300 		crtc = mode_set->crtc;
301 		funcs = crtc->helper_private;
302 		fb = drm_mode_config_fb(crtc);
303 
304 		if (!crtc->enabled)
305 			continue;
306 
307 		if (!fb) {
308 			DRM_ERROR("no fb to restore??\n");
309 			continue;
310 		}
311 
312 		drm_fb_helper_restore_lut_atomic(mode_set->crtc);
313 		funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
314 					    crtc->y, LEAVE_ATOMIC_MODE_SET);
315 	}
316 
317 	return 0;
318 }
319 EXPORT_SYMBOL(drm_fb_helper_debug_leave);
320 #endif
321 
322 static bool restore_fbdev_mode(struct drm_fb_helper *fb_helper)
323 {
324 	struct drm_device *dev = fb_helper->dev;
325 	struct drm_plane *plane;
326 	bool error = false;
327 	int i;
328 
329 	drm_warn_on_modeset_not_all_locked(dev);
330 
331 	drm_for_each_plane(plane, dev) {
332 		if (plane->type != DRM_PLANE_TYPE_PRIMARY)
333 			drm_plane_force_disable(plane);
334 
335 		if (dev->mode_config.rotation_property) {
336 			drm_mode_plane_set_obj_prop(plane,
337 						    dev->mode_config.rotation_property,
338 						    BIT(DRM_ROTATE_0));
339 		}
340 	}
341 
342 	for (i = 0; i < fb_helper->crtc_count; i++) {
343 		struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
344 		struct drm_crtc *crtc = mode_set->crtc;
345 		int ret;
346 
347 		if (crtc->funcs->cursor_set2) {
348 			ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
349 			if (ret)
350 				error = true;
351 		} else if (crtc->funcs->cursor_set) {
352 			ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
353 			if (ret)
354 				error = true;
355 		}
356 
357 		ret = drm_mode_set_config_internal(mode_set);
358 		if (ret)
359 			error = true;
360 	}
361 	return error;
362 }
363 
364 /**
365  * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
366  * @fb_helper: fbcon to restore
367  *
368  * This should be called from driver's drm ->lastclose callback
369  * when implementing an fbcon on top of kms using this helper. This ensures that
370  * the user isn't greeted with a black screen when e.g. X dies.
371  */
372 bool drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
373 {
374 	struct drm_device *dev = fb_helper->dev;
375 	bool ret;
376 	bool do_delayed = false;
377 
378 	drm_modeset_lock_all(dev);
379 	ret = restore_fbdev_mode(fb_helper);
380 
381 	do_delayed = fb_helper->delayed_hotplug;
382 	if (do_delayed)
383 		fb_helper->delayed_hotplug = false;
384 	drm_modeset_unlock_all(dev);
385 
386 	if (do_delayed)
387 		drm_fb_helper_hotplug_event(fb_helper);
388 	return ret;
389 }
390 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
391 
392 static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
393 {
394 	struct drm_device *dev = fb_helper->dev;
395 	struct drm_crtc *crtc;
396 	int bound = 0, crtcs_bound = 0;
397 
398 	/* Sometimes user space wants everything disabled, so don't steal the
399 	 * display if there's a master. */
400 #if 0
401 	if (dev->primary->master)
402 		return false;
403 #endif
404 
405 	drm_for_each_crtc(crtc, dev) {
406 		if (crtc->primary->fb)
407 			crtcs_bound++;
408 		if (crtc->primary->fb == fb_helper->fb)
409 			bound++;
410 	}
411 
412 	if (bound < crtcs_bound)
413 		return false;
414 
415 	return true;
416 }
417 
418 #ifdef CONFIG_MAGIC_SYSRQ
419 /*
420  * restore fbcon display for all kms driver's using this helper, used for sysrq
421  * and panic handling.
422  */
423 static bool drm_fb_helper_force_kernel_mode(void)
424 {
425 	bool ret, error = false;
426 	struct drm_fb_helper *helper;
427 
428 	if (list_empty(&kernel_fb_helper_list))
429 		return false;
430 
431 	list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
432 		struct drm_device *dev = helper->dev;
433 
434 		if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
435 			continue;
436 
437 		drm_modeset_lock_all(dev);
438 		ret = restore_fbdev_mode(helper);
439 		if (ret)
440 			error = true;
441 		drm_modeset_unlock_all(dev);
442 	}
443 	return error;
444 }
445 
446 #if 0
447 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
448 {
449 	bool ret;
450 	ret = drm_fb_helper_force_kernel_mode();
451 	if (ret == true)
452 		DRM_ERROR("Failed to restore crtc configuration\n");
453 }
454 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
455 
456 static void drm_fb_helper_sysrq(int dummy1)
457 {
458 	schedule_work(&drm_fb_helper_restore_work);
459 }
460 
461 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
462 	.handler = drm_fb_helper_sysrq,
463 	.help_msg = "force-fb(V)",
464 	.action_msg = "Restore framebuffer console",
465 };
466 #else
467 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
468 #endif
469 
470 static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
471 {
472 	struct drm_fb_helper *fb_helper = info->par;
473 	struct drm_device *dev = fb_helper->dev;
474 	struct drm_crtc *crtc;
475 	struct drm_connector *connector;
476 	int i, j;
477 
478 	/*
479 	 * For each CRTC in this fb, turn the connectors on/off.
480 	 */
481 	drm_modeset_lock_all(dev);
482 	if (!drm_fb_helper_is_bound(fb_helper)) {
483 		drm_modeset_unlock_all(dev);
484 		return;
485 	}
486 
487 	for (i = 0; i < fb_helper->crtc_count; i++) {
488 		crtc = fb_helper->crtc_info[i].mode_set.crtc;
489 
490 		if (!crtc->enabled)
491 			continue;
492 
493 		/* Walk the connectors & encoders on this fb turning them on/off */
494 		for (j = 0; j < fb_helper->connector_count; j++) {
495 			connector = fb_helper->connector_info[j]->connector;
496 			connector->funcs->dpms(connector, dpms_mode);
497 			drm_object_property_set_value(&connector->base,
498 				dev->mode_config.dpms_property, dpms_mode);
499 		}
500 	}
501 	drm_modeset_unlock_all(dev);
502 }
503 
504 /**
505  * drm_fb_helper_blank - implementation for ->fb_blank
506  * @blank: desired blanking state
507  * @info: fbdev registered by the helper
508  */
509 int drm_fb_helper_blank(int blank, struct fb_info *info)
510 {
511 	if (oops_in_progress)
512 		return -EBUSY;
513 
514 	switch (blank) {
515 	/* Display: On; HSync: On, VSync: On */
516 	case FB_BLANK_UNBLANK:
517 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
518 		break;
519 	/* Display: Off; HSync: On, VSync: On */
520 	case FB_BLANK_NORMAL:
521 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
522 		break;
523 	/* Display: Off; HSync: Off, VSync: On */
524 	case FB_BLANK_HSYNC_SUSPEND:
525 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
526 		break;
527 	/* Display: Off; HSync: On, VSync: Off */
528 	case FB_BLANK_VSYNC_SUSPEND:
529 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
530 		break;
531 	/* Display: Off; HSync: Off, VSync: Off */
532 	case FB_BLANK_POWERDOWN:
533 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
534 		break;
535 	}
536 	return 0;
537 }
538 EXPORT_SYMBOL(drm_fb_helper_blank);
539 #endif
540 
541 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
542 {
543 	int i;
544 
545 	for (i = 0; i < helper->connector_count; i++)
546 		kfree(helper->connector_info[i]);
547 	kfree(helper->connector_info);
548 	for (i = 0; i < helper->crtc_count; i++) {
549 		kfree(helper->crtc_info[i].mode_set.connectors);
550 		if (helper->crtc_info[i].mode_set.mode)
551 			drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
552 	}
553 	kfree(helper->crtc_info);
554 }
555 
556 /**
557  * drm_fb_helper_prepare - setup a drm_fb_helper structure
558  * @dev: DRM device
559  * @helper: driver-allocated fbdev helper structure to set up
560  * @funcs: pointer to structure of functions associate with this helper
561  *
562  * Sets up the bare minimum to make the framebuffer helper usable. This is
563  * useful to implement race-free initialization of the polling helpers.
564  */
565 void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
566 			   const struct drm_fb_helper_funcs *funcs)
567 {
568 	INIT_LIST_HEAD(&helper->kernel_fb_list);
569 	helper->funcs = funcs;
570 	helper->dev = dev;
571 }
572 EXPORT_SYMBOL(drm_fb_helper_prepare);
573 
574 /**
575  * drm_fb_helper_init - initialize a drm_fb_helper structure
576  * @dev: drm device
577  * @fb_helper: driver-allocated fbdev helper structure to initialize
578  * @crtc_count: maximum number of crtcs to support in this fbdev emulation
579  * @max_conn_count: max connector count
580  *
581  * This allocates the structures for the fbdev helper with the given limits.
582  * Note that this won't yet touch the hardware (through the driver interfaces)
583  * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
584  * to allow driver writes more control over the exact init sequence.
585  *
586  * Drivers must call drm_fb_helper_prepare() before calling this function.
587  *
588  * RETURNS:
589  * Zero if everything went ok, nonzero otherwise.
590  */
591 int drm_fb_helper_init(struct drm_device *dev,
592 		       struct drm_fb_helper *fb_helper,
593 		       int crtc_count, int max_conn_count)
594 {
595 	struct drm_crtc *crtc;
596 	int i;
597 
598 	if (!max_conn_count)
599 		return -EINVAL;
600 
601 	fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
602 	if (!fb_helper->crtc_info)
603 		return -ENOMEM;
604 
605 	fb_helper->crtc_count = crtc_count;
606 	fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
607 	if (!fb_helper->connector_info) {
608 		kfree(fb_helper->crtc_info);
609 		return -ENOMEM;
610 	}
611 	fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
612 	fb_helper->connector_count = 0;
613 
614 	for (i = 0; i < crtc_count; i++) {
615 		fb_helper->crtc_info[i].mode_set.connectors =
616 			kcalloc(max_conn_count,
617 				sizeof(struct drm_connector *),
618 				GFP_KERNEL);
619 
620 		if (!fb_helper->crtc_info[i].mode_set.connectors)
621 			goto out_free;
622 		fb_helper->crtc_info[i].mode_set.num_connectors = 0;
623 	}
624 
625 	i = 0;
626 	drm_for_each_crtc(crtc, dev) {
627 		fb_helper->crtc_info[i].mode_set.crtc = crtc;
628 		i++;
629 	}
630 
631 	return 0;
632 out_free:
633 	drm_fb_helper_crtc_free(fb_helper);
634 	return -ENOMEM;
635 }
636 EXPORT_SYMBOL(drm_fb_helper_init);
637 
638 /**
639  * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
640  * @fb_helper: driver-allocated fbdev helper
641  *
642  * A helper to alloc fb_info and the members cmap and apertures. Called
643  * by the driver within the fb_probe fb_helper callback function.
644  *
645  * RETURNS:
646  * fb_info pointer if things went okay, pointer containing error code
647  * otherwise
648  */
649 struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
650 {
651 	struct fb_info *info;
652 
653 	info = kmalloc(sizeof(struct fb_info), M_DRM, M_WAITOK | M_ZERO);
654 	if (!info)
655 		return ERR_PTR(-ENOMEM);
656 
657 #if 0
658 	ret = fb_alloc_cmap(&info->cmap, 256, 0);
659 	if (ret)
660 		goto err_release;
661 
662 	info->apertures = alloc_apertures(1);
663 	if (!info->apertures) {
664 		ret = -ENOMEM;
665 		goto err_free_cmap;
666 	}
667 #endif
668 
669 	fb_helper->fbdev = info;
670 
671 	return info;
672 
673 #if 0
674 err_free_cmap:
675 	fb_dealloc_cmap(&info->cmap);
676 err_release:
677 	framebuffer_release(info);
678 	return ERR_PTR(ret);
679 #endif
680 }
681 EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
682 
683 #if 0
684 /**
685  * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
686  * @fb_helper: driver-allocated fbdev helper
687  *
688  * A wrapper around unregister_framebuffer, to release the fb_info
689  * framebuffer device
690  */
691 void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
692 {
693 	if (fb_helper && fb_helper->fbdev)
694 		unregister_framebuffer(fb_helper->fbdev);
695 }
696 EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
697 
698 /**
699  * drm_fb_helper_release_fbi - dealloc fb_info and its members
700  * @fb_helper: driver-allocated fbdev helper
701  *
702  * A helper to free memory taken by fb_info and the members cmap and
703  * apertures
704  */
705 void drm_fb_helper_release_fbi(struct drm_fb_helper *fb_helper)
706 {
707 	if (fb_helper) {
708 		struct fb_info *info = fb_helper->fbdev;
709 
710 		if (info) {
711 			if (info->cmap.len)
712 				fb_dealloc_cmap(&info->cmap);
713 			framebuffer_release(info);
714 		}
715 
716 		fb_helper->fbdev = NULL;
717 	}
718 }
719 EXPORT_SYMBOL(drm_fb_helper_release_fbi);
720 #endif
721 
722 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
723 {
724 	if (!list_empty(&fb_helper->kernel_fb_list)) {
725 		list_del(&fb_helper->kernel_fb_list);
726 		if (list_empty(&kernel_fb_helper_list)) {
727 #if 0
728 			unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
729 #endif
730 		}
731 	}
732 
733 	drm_fb_helper_crtc_free(fb_helper);
734 
735 }
736 EXPORT_SYMBOL(drm_fb_helper_fini);
737 
738 #if 0
739 /**
740  * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
741  * @fb_helper: driver-allocated fbdev helper
742  *
743  * A wrapper around unlink_framebuffer implemented by fbdev core
744  */
745 void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
746 {
747 	if (fb_helper && fb_helper->fbdev)
748 		unlink_framebuffer(fb_helper->fbdev);
749 }
750 EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
751 
752 /**
753  * drm_fb_helper_sys_read - wrapper around fb_sys_read
754  * @info: fb_info struct pointer
755  * @buf: userspace buffer to read from framebuffer memory
756  * @count: number of bytes to read from framebuffer memory
757  * @ppos: read offset within framebuffer memory
758  *
759  * A wrapper around fb_sys_read implemented by fbdev core
760  */
761 ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
762 			       size_t count, loff_t *ppos)
763 {
764 	return fb_sys_read(info, buf, count, ppos);
765 }
766 EXPORT_SYMBOL(drm_fb_helper_sys_read);
767 
768 /**
769  * drm_fb_helper_sys_write - wrapper around fb_sys_write
770  * @info: fb_info struct pointer
771  * @buf: userspace buffer to write to framebuffer memory
772  * @count: number of bytes to write to framebuffer memory
773  * @ppos: write offset within framebuffer memory
774  *
775  * A wrapper around fb_sys_write implemented by fbdev core
776  */
777 ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
778 				size_t count, loff_t *ppos)
779 {
780 	return fb_sys_write(info, buf, count, ppos);
781 }
782 EXPORT_SYMBOL(drm_fb_helper_sys_write);
783 
784 /**
785  * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
786  * @info: fbdev registered by the helper
787  * @rect: info about rectangle to fill
788  *
789  * A wrapper around sys_fillrect implemented by fbdev core
790  */
791 void drm_fb_helper_sys_fillrect(struct fb_info *info,
792 				const struct fb_fillrect *rect)
793 {
794 	sys_fillrect(info, rect);
795 }
796 EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
797 
798 /**
799  * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
800  * @info: fbdev registered by the helper
801  * @area: info about area to copy
802  *
803  * A wrapper around sys_copyarea implemented by fbdev core
804  */
805 void drm_fb_helper_sys_copyarea(struct fb_info *info,
806 				const struct fb_copyarea *area)
807 {
808 	sys_copyarea(info, area);
809 }
810 EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
811 
812 /**
813  * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
814  * @info: fbdev registered by the helper
815  * @image: info about image to blit
816  *
817  * A wrapper around sys_imageblit implemented by fbdev core
818  */
819 void drm_fb_helper_sys_imageblit(struct fb_info *info,
820 				 const struct fb_image *image)
821 {
822 	sys_imageblit(info, image);
823 }
824 EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
825 
826 /**
827  * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
828  * @info: fbdev registered by the helper
829  * @rect: info about rectangle to fill
830  *
831  * A wrapper around cfb_imageblit implemented by fbdev core
832  */
833 void drm_fb_helper_cfb_fillrect(struct fb_info *info,
834 				const struct fb_fillrect *rect)
835 {
836 	cfb_fillrect(info, rect);
837 }
838 EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
839 
840 /**
841  * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
842  * @info: fbdev registered by the helper
843  * @area: info about area to copy
844  *
845  * A wrapper around cfb_copyarea implemented by fbdev core
846  */
847 void drm_fb_helper_cfb_copyarea(struct fb_info *info,
848 				const struct fb_copyarea *area)
849 {
850 	cfb_copyarea(info, area);
851 }
852 EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
853 
854 /**
855  * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
856  * @info: fbdev registered by the helper
857  * @image: info about image to blit
858  *
859  * A wrapper around cfb_imageblit implemented by fbdev core
860  */
861 void drm_fb_helper_cfb_imageblit(struct fb_info *info,
862 				 const struct fb_image *image)
863 {
864 	cfb_imageblit(info, image);
865 }
866 EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
867 
868 /**
869  * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
870  * @fb_helper: driver-allocated fbdev helper
871  * @state: desired state, zero to resume, non-zero to suspend
872  *
873  * A wrapper around fb_set_suspend implemented by fbdev core
874  */
875 void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, int state)
876 {
877 	if (fb_helper && fb_helper->fbdev)
878 		fb_set_suspend(fb_helper->fbdev, state);
879 }
880 EXPORT_SYMBOL(drm_fb_helper_set_suspend);
881 
882 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
883 		     u16 blue, u16 regno, struct fb_info *info)
884 {
885 	struct drm_fb_helper *fb_helper = info->par;
886 	struct drm_framebuffer *fb = fb_helper->fb;
887 	int pindex;
888 
889 	if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
890 		u32 *palette;
891 		u32 value;
892 		/* place color in psuedopalette */
893 		if (regno > 16)
894 			return -EINVAL;
895 		palette = (u32 *)info->pseudo_palette;
896 		red >>= (16 - info->var.red.length);
897 		green >>= (16 - info->var.green.length);
898 		blue >>= (16 - info->var.blue.length);
899 		value = (red << info->var.red.offset) |
900 			(green << info->var.green.offset) |
901 			(blue << info->var.blue.offset);
902 		if (info->var.transp.length > 0) {
903 			u32 mask = (1 << info->var.transp.length) - 1;
904 			mask <<= info->var.transp.offset;
905 			value |= mask;
906 		}
907 		palette[regno] = value;
908 		return 0;
909 	}
910 
911 	/*
912 	 * The driver really shouldn't advertise pseudo/directcolor
913 	 * visuals if it can't deal with the palette.
914 	 */
915 	if (WARN_ON(!fb_helper->funcs->gamma_set ||
916 		    !fb_helper->funcs->gamma_get))
917 		return -EINVAL;
918 
919 	pindex = regno;
920 
921 	if (fb->bits_per_pixel == 16) {
922 		pindex = regno << 3;
923 
924 		if (fb->depth == 16 && regno > 63)
925 			return -EINVAL;
926 		if (fb->depth == 15 && regno > 31)
927 			return -EINVAL;
928 
929 		if (fb->depth == 16) {
930 			u16 r, g, b;
931 			int i;
932 			if (regno < 32) {
933 				for (i = 0; i < 8; i++)
934 					fb_helper->funcs->gamma_set(crtc, red,
935 						green, blue, pindex + i);
936 			}
937 
938 			fb_helper->funcs->gamma_get(crtc, &r,
939 						    &g, &b,
940 						    pindex >> 1);
941 
942 			for (i = 0; i < 4; i++)
943 				fb_helper->funcs->gamma_set(crtc, r,
944 							    green, b,
945 							    (pindex >> 1) + i);
946 		}
947 	}
948 
949 	if (fb->depth != 16)
950 		fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
951 	return 0;
952 }
953 
954 /**
955  * drm_fb_helper_setcmap - implementation for ->fb_setcmap
956  * @cmap: cmap to set
957  * @info: fbdev registered by the helper
958  */
959 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
960 {
961 	struct drm_fb_helper *fb_helper = info->par;
962 	struct drm_device *dev = fb_helper->dev;
963 	const struct drm_crtc_helper_funcs *crtc_funcs;
964 	u16 *red, *green, *blue, *transp;
965 	struct drm_crtc *crtc;
966 	int i, j, rc = 0;
967 	int start;
968 
969 	if (oops_in_progress)
970 		return -EBUSY;
971 
972 	drm_modeset_lock_all(dev);
973 	if (!drm_fb_helper_is_bound(fb_helper)) {
974 		drm_modeset_unlock_all(dev);
975 		return -EBUSY;
976 	}
977 
978 	for (i = 0; i < fb_helper->crtc_count; i++) {
979 		crtc = fb_helper->crtc_info[i].mode_set.crtc;
980 		crtc_funcs = crtc->helper_private;
981 
982 		red = cmap->red;
983 		green = cmap->green;
984 		blue = cmap->blue;
985 		transp = cmap->transp;
986 		start = cmap->start;
987 
988 		for (j = 0; j < cmap->len; j++) {
989 			u16 hred, hgreen, hblue, htransp = 0xffff;
990 
991 			hred = *red++;
992 			hgreen = *green++;
993 			hblue = *blue++;
994 
995 			if (transp)
996 				htransp = *transp++;
997 
998 			rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
999 			if (rc)
1000 				goto out;
1001 		}
1002 		if (crtc_funcs->load_lut)
1003 			crtc_funcs->load_lut(crtc);
1004 	}
1005  out:
1006 	drm_modeset_unlock_all(dev);
1007 	return rc;
1008 }
1009 EXPORT_SYMBOL(drm_fb_helper_setcmap);
1010 
1011 /**
1012  * drm_fb_helper_check_var - implementation for ->fb_check_var
1013  * @var: screeninfo to check
1014  * @info: fbdev registered by the helper
1015  */
1016 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
1017 			    struct fb_info *info)
1018 {
1019 	struct drm_fb_helper *fb_helper = info->par;
1020 	struct drm_framebuffer *fb = fb_helper->fb;
1021 	int depth;
1022 
1023 	if (var->pixclock != 0 || in_dbg_master())
1024 		return -EINVAL;
1025 
1026 	/* Need to resize the fb object !!! */
1027 	if (var->bits_per_pixel > fb->bits_per_pixel ||
1028 	    var->xres > fb->width || var->yres > fb->height ||
1029 	    var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
1030 		DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
1031 			  "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1032 			  var->xres, var->yres, var->bits_per_pixel,
1033 			  var->xres_virtual, var->yres_virtual,
1034 			  fb->width, fb->height, fb->bits_per_pixel);
1035 		return -EINVAL;
1036 	}
1037 
1038 	switch (var->bits_per_pixel) {
1039 	case 16:
1040 		depth = (var->green.length == 6) ? 16 : 15;
1041 		break;
1042 	case 32:
1043 		depth = (var->transp.length > 0) ? 32 : 24;
1044 		break;
1045 	default:
1046 		depth = var->bits_per_pixel;
1047 		break;
1048 	}
1049 
1050 	switch (depth) {
1051 	case 8:
1052 		var->red.offset = 0;
1053 		var->green.offset = 0;
1054 		var->blue.offset = 0;
1055 		var->red.length = 8;
1056 		var->green.length = 8;
1057 		var->blue.length = 8;
1058 		var->transp.length = 0;
1059 		var->transp.offset = 0;
1060 		break;
1061 	case 15:
1062 		var->red.offset = 10;
1063 		var->green.offset = 5;
1064 		var->blue.offset = 0;
1065 		var->red.length = 5;
1066 		var->green.length = 5;
1067 		var->blue.length = 5;
1068 		var->transp.length = 1;
1069 		var->transp.offset = 15;
1070 		break;
1071 	case 16:
1072 		var->red.offset = 11;
1073 		var->green.offset = 5;
1074 		var->blue.offset = 0;
1075 		var->red.length = 5;
1076 		var->green.length = 6;
1077 		var->blue.length = 5;
1078 		var->transp.length = 0;
1079 		var->transp.offset = 0;
1080 		break;
1081 	case 24:
1082 		var->red.offset = 16;
1083 		var->green.offset = 8;
1084 		var->blue.offset = 0;
1085 		var->red.length = 8;
1086 		var->green.length = 8;
1087 		var->blue.length = 8;
1088 		var->transp.length = 0;
1089 		var->transp.offset = 0;
1090 		break;
1091 	case 32:
1092 		var->red.offset = 16;
1093 		var->green.offset = 8;
1094 		var->blue.offset = 0;
1095 		var->red.length = 8;
1096 		var->green.length = 8;
1097 		var->blue.length = 8;
1098 		var->transp.length = 8;
1099 		var->transp.offset = 24;
1100 		break;
1101 	default:
1102 		return -EINVAL;
1103 	}
1104 	return 0;
1105 }
1106 EXPORT_SYMBOL(drm_fb_helper_check_var);
1107 
1108 /**
1109  * drm_fb_helper_set_par - implementation for ->fb_set_par
1110  * @info: fbdev registered by the helper
1111  *
1112  * This will let fbcon do the mode init and is called at initialization time by
1113  * the fbdev core when registering the driver, and later on through the hotplug
1114  * callback.
1115  */
1116 int drm_fb_helper_set_par(struct fb_info *info)
1117 {
1118 	struct drm_fb_helper *fb_helper = info->par;
1119 	struct fb_var_screeninfo *var = &info->var;
1120 
1121 	if (oops_in_progress)
1122 		return -EBUSY;
1123 
1124 	if (var->pixclock != 0) {
1125 		DRM_ERROR("PIXEL CLOCK SET\n");
1126 		return -EINVAL;
1127 	}
1128 
1129 	drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
1130 
1131 	return 0;
1132 }
1133 EXPORT_SYMBOL(drm_fb_helper_set_par);
1134 
1135 /**
1136  * drm_fb_helper_pan_display - implementation for ->fb_pan_display
1137  * @var: updated screen information
1138  * @info: fbdev registered by the helper
1139  */
1140 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1141 			      struct fb_info *info)
1142 {
1143 	struct drm_fb_helper *fb_helper = info->par;
1144 	struct drm_device *dev = fb_helper->dev;
1145 	struct drm_mode_set *modeset;
1146 	int ret = 0;
1147 	int i;
1148 
1149 	if (oops_in_progress)
1150 		return -EBUSY;
1151 
1152 	drm_modeset_lock_all(dev);
1153 	if (!drm_fb_helper_is_bound(fb_helper)) {
1154 		drm_modeset_unlock_all(dev);
1155 		return -EBUSY;
1156 	}
1157 
1158 	for (i = 0; i < fb_helper->crtc_count; i++) {
1159 		modeset = &fb_helper->crtc_info[i].mode_set;
1160 
1161 		modeset->x = var->xoffset;
1162 		modeset->y = var->yoffset;
1163 
1164 		if (modeset->num_connectors) {
1165 			ret = drm_mode_set_config_internal(modeset);
1166 			if (!ret) {
1167 				info->var.xoffset = var->xoffset;
1168 				info->var.yoffset = var->yoffset;
1169 			}
1170 		}
1171 	}
1172 	drm_modeset_unlock_all(dev);
1173 	return ret;
1174 }
1175 EXPORT_SYMBOL(drm_fb_helper_pan_display);
1176 #endif
1177 
1178 #ifdef __DragonFly__
1179 static void
1180 do_restore_fbdev_mode(void *context, int pending)
1181 {
1182 	struct drm_fb_helper *fb_helper = context;
1183 
1184 	if (!fb_helper->fb)
1185 		return;
1186 
1187 	drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
1188 }
1189 
1190 static void
1191 sc_restore_fbdev_mode(void *cookie)
1192 {
1193 	struct drm_fb_helper *fb_helper = cookie;
1194 
1195 	if (!fb_helper->fb)
1196 		return;
1197 
1198 	taskqueue_enqueue(taskqueue_thread[0], &fb_helper->fb_mode_task);
1199 }
1200 #endif
1201 
1202 /*
1203  * Allocates the backing storage and sets up the fbdev info structure through
1204  * the ->fb_probe callback and then registers the fbdev and sets up the panic
1205  * notifier.
1206  */
1207 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1208 					 int preferred_bpp)
1209 {
1210 	int ret = 0;
1211 	int crtc_count = 0;
1212 	int i;
1213 	struct fb_info *info;
1214 	struct drm_fb_helper_surface_size sizes;
1215 	int gamma_size = 0;
1216 #ifdef __DragonFly__
1217 	int kms_console = 1;
1218 #endif
1219 
1220 	memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1221 	sizes.surface_depth = 24;
1222 	sizes.surface_bpp = 32;
1223 	sizes.fb_width = (unsigned)-1;
1224 	sizes.fb_height = (unsigned)-1;
1225 
1226 	/* if driver picks 8 or 16 by default use that
1227 	   for both depth/bpp */
1228 	if (preferred_bpp != sizes.surface_bpp)
1229 		sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
1230 
1231 	/* first up get a count of crtcs now in use and new min/maxes width/heights */
1232 	for (i = 0; i < fb_helper->connector_count; i++) {
1233 		struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
1234 		struct drm_cmdline_mode *cmdline_mode;
1235 
1236 		cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
1237 
1238 		if (cmdline_mode->bpp_specified) {
1239 			switch (cmdline_mode->bpp) {
1240 			case 8:
1241 				sizes.surface_depth = sizes.surface_bpp = 8;
1242 				break;
1243 			case 15:
1244 				sizes.surface_depth = 15;
1245 				sizes.surface_bpp = 16;
1246 				break;
1247 			case 16:
1248 				sizes.surface_depth = sizes.surface_bpp = 16;
1249 				break;
1250 			case 24:
1251 				sizes.surface_depth = sizes.surface_bpp = 24;
1252 				break;
1253 			case 32:
1254 				sizes.surface_depth = 24;
1255 				sizes.surface_bpp = 32;
1256 				break;
1257 			}
1258 			break;
1259 		}
1260 	}
1261 
1262 	crtc_count = 0;
1263 	for (i = 0; i < fb_helper->crtc_count; i++) {
1264 		struct drm_display_mode *desired_mode;
1265 		struct drm_mode_set *mode_set;
1266 		int x, y, j;
1267 		/* in case of tile group, are we the last tile vert or horiz?
1268 		 * If no tile group you are always the last one both vertically
1269 		 * and horizontally
1270 		 */
1271 		bool lastv = true, lasth = true;
1272 
1273 		desired_mode = fb_helper->crtc_info[i].desired_mode;
1274 		mode_set = &fb_helper->crtc_info[i].mode_set;
1275 
1276 		if (!desired_mode)
1277 			continue;
1278 
1279 		crtc_count++;
1280 
1281 		x = fb_helper->crtc_info[i].x;
1282 		y = fb_helper->crtc_info[i].y;
1283 
1284 		if (gamma_size == 0)
1285 			gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1286 
1287 		sizes.surface_width  = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
1288 		sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
1289 
1290 		for (j = 0; j < mode_set->num_connectors; j++) {
1291 			struct drm_connector *connector = mode_set->connectors[j];
1292 			if (connector->has_tile) {
1293 				lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
1294 				lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
1295 				/* cloning to multiple tiles is just crazy-talk, so: */
1296 				break;
1297 			}
1298 		}
1299 
1300 		if (lasth)
1301 			sizes.fb_width  = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
1302 		if (lastv)
1303 			sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
1304 	}
1305 
1306 	if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
1307 		/* hmm everyone went away - assume VGA cable just fell out
1308 		   and will come back later. */
1309 		DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
1310 		sizes.fb_width = sizes.surface_width = 1024;
1311 		sizes.fb_height = sizes.surface_height = 768;
1312 	}
1313 
1314 	/* push down into drivers */
1315 	ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1316 	if (ret < 0)
1317 		return ret;
1318 
1319 	info = fb_helper->fbdev;
1320 
1321 	/*
1322 	 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1323 	 * events, but at init time drm_setup_crtcs needs to be called before
1324 	 * the fb is allocated (since we need to figure out the desired size of
1325 	 * the fb before we can allocate it ...). Hence we need to fix things up
1326 	 * here again.
1327 	 */
1328 	for (i = 0; i < fb_helper->crtc_count; i++)
1329 		if (fb_helper->crtc_info[i].mode_set.num_connectors)
1330 			fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1331 
1332 #ifdef __DragonFly__
1333 	TUNABLE_INT_FETCH("kern.kms_console", &kms_console);
1334 	if (kms_console) {
1335 		TASK_INIT(&fb_helper->fb_mode_task, 0, do_restore_fbdev_mode,
1336 		    fb_helper);
1337 		info->cookie = fb_helper;
1338 		info->restore = (void *)&sc_restore_fbdev_mode;
1339 		if (register_framebuffer(info) < 0)
1340 			return -EINVAL;
1341 	}
1342 #endif
1343 
1344 #if 0
1345 	info->var.pixclock = 0;
1346 	if (register_framebuffer(info) < 0)
1347 		return -EINVAL;
1348 
1349 	dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
1350 			info->node, info->fix.id);
1351 
1352 	if (list_empty(&kernel_fb_helper_list)) {
1353 		register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
1354 	}
1355 
1356 	list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
1357 #endif
1358 
1359 	return 0;
1360 }
1361 
1362 #if 0
1363 /**
1364  * drm_fb_helper_fill_fix - initializes fixed fbdev information
1365  * @info: fbdev registered by the helper
1366  * @pitch: desired pitch
1367  * @depth: desired depth
1368  *
1369  * Helper to fill in the fixed fbdev information useful for a non-accelerated
1370  * fbdev emulations. Drivers which support acceleration methods which impose
1371  * additional constraints need to set up their own limits.
1372  *
1373  * Drivers should call this (or their equivalent setup code) from their
1374  * ->fb_probe callback.
1375  */
1376 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1377 			    uint32_t depth)
1378 {
1379 	info->fix.type = FB_TYPE_PACKED_PIXELS;
1380 	info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1381 		FB_VISUAL_TRUECOLOR;
1382 	info->fix.mmio_start = 0;
1383 	info->fix.mmio_len = 0;
1384 	info->fix.type_aux = 0;
1385 	info->fix.xpanstep = 1; /* doing it in hw */
1386 	info->fix.ypanstep = 1; /* doing it in hw */
1387 	info->fix.ywrapstep = 0;
1388 	info->fix.accel = FB_ACCEL_NONE;
1389 
1390 	info->fix.line_length = pitch;
1391 	return;
1392 }
1393 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1394 
1395 /**
1396  * drm_fb_helper_fill_var - initalizes variable fbdev information
1397  * @info: fbdev instance to set up
1398  * @fb_helper: fb helper instance to use as template
1399  * @fb_width: desired fb width
1400  * @fb_height: desired fb height
1401  *
1402  * Sets up the variable fbdev metainformation from the given fb helper instance
1403  * and the drm framebuffer allocated in fb_helper->fb.
1404  *
1405  * Drivers should call this (or their equivalent setup code) from their
1406  * ->fb_probe callback after having allocated the fbdev backing
1407  * storage framebuffer.
1408  */
1409 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
1410 			    uint32_t fb_width, uint32_t fb_height)
1411 {
1412 	struct drm_framebuffer *fb = fb_helper->fb;
1413 	info->pseudo_palette = fb_helper->pseudo_palette;
1414 	info->var.xres_virtual = fb->width;
1415 	info->var.yres_virtual = fb->height;
1416 	info->var.bits_per_pixel = fb->bits_per_pixel;
1417 	info->var.accel_flags = FB_ACCELF_TEXT;
1418 	info->var.xoffset = 0;
1419 	info->var.yoffset = 0;
1420 	info->var.activate = FB_ACTIVATE_NOW;
1421 	info->var.height = -1;
1422 	info->var.width = -1;
1423 
1424 	switch (fb->depth) {
1425 	case 8:
1426 		info->var.red.offset = 0;
1427 		info->var.green.offset = 0;
1428 		info->var.blue.offset = 0;
1429 		info->var.red.length = 8; /* 8bit DAC */
1430 		info->var.green.length = 8;
1431 		info->var.blue.length = 8;
1432 		info->var.transp.offset = 0;
1433 		info->var.transp.length = 0;
1434 		break;
1435 	case 15:
1436 		info->var.red.offset = 10;
1437 		info->var.green.offset = 5;
1438 		info->var.blue.offset = 0;
1439 		info->var.red.length = 5;
1440 		info->var.green.length = 5;
1441 		info->var.blue.length = 5;
1442 		info->var.transp.offset = 15;
1443 		info->var.transp.length = 1;
1444 		break;
1445 	case 16:
1446 		info->var.red.offset = 11;
1447 		info->var.green.offset = 5;
1448 		info->var.blue.offset = 0;
1449 		info->var.red.length = 5;
1450 		info->var.green.length = 6;
1451 		info->var.blue.length = 5;
1452 		info->var.transp.offset = 0;
1453 		break;
1454 	case 24:
1455 		info->var.red.offset = 16;
1456 		info->var.green.offset = 8;
1457 		info->var.blue.offset = 0;
1458 		info->var.red.length = 8;
1459 		info->var.green.length = 8;
1460 		info->var.blue.length = 8;
1461 		info->var.transp.offset = 0;
1462 		info->var.transp.length = 0;
1463 		break;
1464 	case 32:
1465 		info->var.red.offset = 16;
1466 		info->var.green.offset = 8;
1467 		info->var.blue.offset = 0;
1468 		info->var.red.length = 8;
1469 		info->var.green.length = 8;
1470 		info->var.blue.length = 8;
1471 		info->var.transp.offset = 24;
1472 		info->var.transp.length = 8;
1473 		break;
1474 	default:
1475 		break;
1476 	}
1477 
1478 	info->var.xres = fb_width;
1479 	info->var.yres = fb_height;
1480 }
1481 EXPORT_SYMBOL(drm_fb_helper_fill_var);
1482 #endif
1483 
1484 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1485 					       uint32_t maxX,
1486 					       uint32_t maxY)
1487 {
1488 	struct drm_connector *connector;
1489 	int count = 0;
1490 	int i;
1491 
1492 	for (i = 0; i < fb_helper->connector_count; i++) {
1493 		connector = fb_helper->connector_info[i]->connector;
1494 		count += connector->funcs->fill_modes(connector, maxX, maxY);
1495 	}
1496 
1497 	return count;
1498 }
1499 
1500 struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1501 {
1502 	struct drm_display_mode *mode;
1503 
1504 	list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1505 		if (mode->hdisplay > width ||
1506 		    mode->vdisplay > height)
1507 			continue;
1508 		if (mode->type & DRM_MODE_TYPE_PREFERRED)
1509 			return mode;
1510 	}
1511 	return NULL;
1512 }
1513 EXPORT_SYMBOL(drm_has_preferred_mode);
1514 
1515 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1516 {
1517 	return fb_connector->connector->cmdline_mode.specified;
1518 }
1519 
1520 struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1521 						      int width, int height)
1522 {
1523 	struct drm_cmdline_mode *cmdline_mode;
1524 	struct drm_display_mode *mode;
1525 	bool prefer_non_interlace;
1526 
1527 	cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
1528 	if (cmdline_mode->specified == false)
1529 		return NULL;
1530 
1531 	/* attempt to find a matching mode in the list of modes
1532 	 *  we have gotten so far, if not add a CVT mode that conforms
1533 	 */
1534 	if (cmdline_mode->rb || cmdline_mode->margins)
1535 		goto create_mode;
1536 
1537 	prefer_non_interlace = !cmdline_mode->interlace;
1538 again:
1539 	list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1540 		/* check width/height */
1541 		if (mode->hdisplay != cmdline_mode->xres ||
1542 		    mode->vdisplay != cmdline_mode->yres)
1543 			continue;
1544 
1545 		if (cmdline_mode->refresh_specified) {
1546 			if (mode->vrefresh != cmdline_mode->refresh)
1547 				continue;
1548 		}
1549 
1550 		if (cmdline_mode->interlace) {
1551 			if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1552 				continue;
1553 		} else if (prefer_non_interlace) {
1554 			if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1555 				continue;
1556 		}
1557 		return mode;
1558 	}
1559 
1560 	if (prefer_non_interlace) {
1561 		prefer_non_interlace = false;
1562 		goto again;
1563 	}
1564 
1565 create_mode:
1566 	mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1567 						 cmdline_mode);
1568 	list_add(&mode->head, &fb_helper_conn->connector->modes);
1569 	return mode;
1570 }
1571 EXPORT_SYMBOL(drm_pick_cmdline_mode);
1572 
1573 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1574 {
1575 	bool enable;
1576 
1577 	if (strict)
1578 		enable = connector->status == connector_status_connected;
1579 	else
1580 		enable = connector->status != connector_status_disconnected;
1581 
1582 	return enable;
1583 }
1584 
1585 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1586 				  bool *enabled)
1587 {
1588 	bool any_enabled = false;
1589 	struct drm_connector *connector;
1590 	int i = 0;
1591 
1592 	for (i = 0; i < fb_helper->connector_count; i++) {
1593 		connector = fb_helper->connector_info[i]->connector;
1594 		enabled[i] = drm_connector_enabled(connector, true);
1595 		DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1596 			  enabled[i] ? "yes" : "no");
1597 		any_enabled |= enabled[i];
1598 	}
1599 
1600 	if (any_enabled)
1601 		return;
1602 
1603 	for (i = 0; i < fb_helper->connector_count; i++) {
1604 		connector = fb_helper->connector_info[i]->connector;
1605 		enabled[i] = drm_connector_enabled(connector, false);
1606 	}
1607 }
1608 
1609 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1610 			      struct drm_display_mode **modes,
1611 			      struct drm_fb_offset *offsets,
1612 			      bool *enabled, int width, int height)
1613 {
1614 	int count, i, j;
1615 	bool can_clone = false;
1616 	struct drm_fb_helper_connector *fb_helper_conn;
1617 	struct drm_display_mode *dmt_mode, *mode;
1618 
1619 	/* only contemplate cloning in the single crtc case */
1620 	if (fb_helper->crtc_count > 1)
1621 		return false;
1622 
1623 	count = 0;
1624 	for (i = 0; i < fb_helper->connector_count; i++) {
1625 		if (enabled[i])
1626 			count++;
1627 	}
1628 
1629 	/* only contemplate cloning if more than one connector is enabled */
1630 	if (count <= 1)
1631 		return false;
1632 
1633 	/* check the command line or if nothing common pick 1024x768 */
1634 	can_clone = true;
1635 	for (i = 0; i < fb_helper->connector_count; i++) {
1636 		if (!enabled[i])
1637 			continue;
1638 		fb_helper_conn = fb_helper->connector_info[i];
1639 		modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1640 		if (!modes[i]) {
1641 			can_clone = false;
1642 			break;
1643 		}
1644 		for (j = 0; j < i; j++) {
1645 			if (!enabled[j])
1646 				continue;
1647 			if (!drm_mode_equal(modes[j], modes[i]))
1648 				can_clone = false;
1649 		}
1650 	}
1651 
1652 	if (can_clone) {
1653 		DRM_DEBUG_KMS("can clone using command line\n");
1654 		return true;
1655 	}
1656 
1657 	/* try and find a 1024x768 mode on each connector */
1658 	can_clone = true;
1659 	dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
1660 
1661 	for (i = 0; i < fb_helper->connector_count; i++) {
1662 
1663 		if (!enabled[i])
1664 			continue;
1665 
1666 		fb_helper_conn = fb_helper->connector_info[i];
1667 		list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1668 			if (drm_mode_equal(mode, dmt_mode))
1669 				modes[i] = mode;
1670 		}
1671 		if (!modes[i])
1672 			can_clone = false;
1673 	}
1674 
1675 	if (can_clone) {
1676 		DRM_DEBUG_KMS("can clone using 1024x768\n");
1677 		return true;
1678 	}
1679 	DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1680 	return false;
1681 }
1682 
1683 static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
1684 				struct drm_display_mode **modes,
1685 				struct drm_fb_offset *offsets,
1686 				int idx,
1687 				int h_idx, int v_idx)
1688 {
1689 	struct drm_fb_helper_connector *fb_helper_conn;
1690 	int i;
1691 	int hoffset = 0, voffset = 0;
1692 
1693 	for (i = 0; i < fb_helper->connector_count; i++) {
1694 		fb_helper_conn = fb_helper->connector_info[i];
1695 		if (!fb_helper_conn->connector->has_tile)
1696 			continue;
1697 
1698 		if (!modes[i] && (h_idx || v_idx)) {
1699 			DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
1700 				      fb_helper_conn->connector->base.id);
1701 			continue;
1702 		}
1703 		if (fb_helper_conn->connector->tile_h_loc < h_idx)
1704 			hoffset += modes[i]->hdisplay;
1705 
1706 		if (fb_helper_conn->connector->tile_v_loc < v_idx)
1707 			voffset += modes[i]->vdisplay;
1708 	}
1709 	offsets[idx].x = hoffset;
1710 	offsets[idx].y = voffset;
1711 	DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
1712 	return 0;
1713 }
1714 
1715 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1716 				 struct drm_display_mode **modes,
1717 				 struct drm_fb_offset *offsets,
1718 				 bool *enabled, int width, int height)
1719 {
1720 	struct drm_fb_helper_connector *fb_helper_conn;
1721 	int i;
1722 	uint64_t conn_configured = 0, mask;
1723 	int tile_pass = 0;
1724 	mask = (1 << fb_helper->connector_count) - 1;
1725 retry:
1726 	for (i = 0; i < fb_helper->connector_count; i++) {
1727 		fb_helper_conn = fb_helper->connector_info[i];
1728 
1729 		if (conn_configured & (1 << i))
1730 			continue;
1731 
1732 		if (enabled[i] == false) {
1733 			conn_configured |= (1 << i);
1734 			continue;
1735 		}
1736 
1737 		/* first pass over all the untiled connectors */
1738 		if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
1739 			continue;
1740 
1741 		if (tile_pass == 1) {
1742 			if (fb_helper_conn->connector->tile_h_loc != 0 ||
1743 			    fb_helper_conn->connector->tile_v_loc != 0)
1744 				continue;
1745 
1746 		} else {
1747 			if (fb_helper_conn->connector->tile_h_loc != tile_pass -1 &&
1748 			    fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
1749 			/* if this tile_pass doesn't cover any of the tiles - keep going */
1750 				continue;
1751 
1752 			/* find the tile offsets for this pass - need
1753 			   to find all tiles left and above */
1754 			drm_get_tile_offsets(fb_helper, modes, offsets,
1755 					     i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
1756 		}
1757 		DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1758 			      fb_helper_conn->connector->base.id);
1759 
1760 		/* got for command line mode first */
1761 		modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1762 		if (!modes[i]) {
1763 			DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
1764 				      fb_helper_conn->connector->base.id, fb_helper_conn->connector->tile_group ? fb_helper_conn->connector->tile_group->id : 0);
1765 			modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1766 		}
1767 		/* No preferred modes, pick one off the list */
1768 		if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1769 			list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1770 				break;
1771 		}
1772 		DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1773 			  "none");
1774 		conn_configured |= (1 << i);
1775 	}
1776 
1777 	if ((conn_configured & mask) != mask) {
1778 		tile_pass++;
1779 		goto retry;
1780 	}
1781 	return true;
1782 }
1783 
1784 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1785 			  struct drm_fb_helper_crtc **best_crtcs,
1786 			  struct drm_display_mode **modes,
1787 			  int n, int width, int height)
1788 {
1789 	int c, o;
1790 	struct drm_device *dev = fb_helper->dev;
1791 	struct drm_connector *connector;
1792 	const struct drm_connector_helper_funcs *connector_funcs;
1793 	struct drm_encoder *encoder;
1794 	int my_score, best_score, score;
1795 	struct drm_fb_helper_crtc **crtcs, *crtc;
1796 	struct drm_fb_helper_connector *fb_helper_conn;
1797 
1798 	if (n == fb_helper->connector_count)
1799 		return 0;
1800 
1801 	fb_helper_conn = fb_helper->connector_info[n];
1802 	connector = fb_helper_conn->connector;
1803 
1804 	best_crtcs[n] = NULL;
1805 	best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1806 	if (modes[n] == NULL)
1807 		return best_score;
1808 
1809 	crtcs = kzalloc(dev->mode_config.num_connector *
1810 			sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1811 	if (!crtcs)
1812 		return best_score;
1813 
1814 	my_score = 1;
1815 	if (connector->status == connector_status_connected)
1816 		my_score++;
1817 	if (drm_has_cmdline_mode(fb_helper_conn))
1818 		my_score++;
1819 	if (drm_has_preferred_mode(fb_helper_conn, width, height))
1820 		my_score++;
1821 
1822 	connector_funcs = connector->helper_private;
1823 	encoder = connector_funcs->best_encoder(connector);
1824 	if (!encoder)
1825 		goto out;
1826 
1827 	/* select a crtc for this connector and then attempt to configure
1828 	   remaining connectors */
1829 	for (c = 0; c < fb_helper->crtc_count; c++) {
1830 		crtc = &fb_helper->crtc_info[c];
1831 
1832 		if ((encoder->possible_crtcs & (1 << c)) == 0)
1833 			continue;
1834 
1835 		for (o = 0; o < n; o++)
1836 			if (best_crtcs[o] == crtc)
1837 				break;
1838 
1839 		if (o < n) {
1840 			/* ignore cloning unless only a single crtc */
1841 			if (fb_helper->crtc_count > 1)
1842 				continue;
1843 
1844 			if (!drm_mode_equal(modes[o], modes[n]))
1845 				continue;
1846 		}
1847 
1848 		crtcs[n] = crtc;
1849 		memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1850 		score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1851 						  width, height);
1852 		if (score > best_score) {
1853 			best_score = score;
1854 			memcpy(best_crtcs, crtcs,
1855 			       dev->mode_config.num_connector *
1856 			       sizeof(struct drm_fb_helper_crtc *));
1857 		}
1858 	}
1859 out:
1860 	kfree(crtcs);
1861 	return best_score;
1862 }
1863 
1864 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1865 {
1866 	struct drm_device *dev = fb_helper->dev;
1867 	struct drm_fb_helper_crtc **crtcs;
1868 	struct drm_display_mode **modes;
1869 	struct drm_fb_offset *offsets;
1870 	struct drm_mode_set *modeset;
1871 	bool *enabled;
1872 	int width, height;
1873 	int i;
1874 
1875 	DRM_DEBUG_KMS("\n");
1876 
1877 	width = dev->mode_config.max_width;
1878 	height = dev->mode_config.max_height;
1879 
1880 	crtcs = kcalloc(dev->mode_config.num_connector,
1881 			sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1882 	modes = kcalloc(dev->mode_config.num_connector,
1883 			sizeof(struct drm_display_mode *), GFP_KERNEL);
1884 	offsets = kcalloc(dev->mode_config.num_connector,
1885 			  sizeof(struct drm_fb_offset), GFP_KERNEL);
1886 	enabled = kcalloc(dev->mode_config.num_connector,
1887 			  sizeof(bool), GFP_KERNEL);
1888 	if (!crtcs || !modes || !enabled || !offsets) {
1889 		DRM_ERROR("Memory allocation failed\n");
1890 		goto out;
1891 	}
1892 
1893 
1894 	drm_enable_connectors(fb_helper, enabled);
1895 
1896 	if (!(fb_helper->funcs->initial_config &&
1897 	      fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
1898 					       offsets,
1899 					       enabled, width, height))) {
1900 		memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0]));
1901 		memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0]));
1902 		memset(offsets, 0, dev->mode_config.num_connector*sizeof(offsets[0]));
1903 
1904 		if (!drm_target_cloned(fb_helper, modes, offsets,
1905 				       enabled, width, height) &&
1906 		    !drm_target_preferred(fb_helper, modes, offsets,
1907 					  enabled, width, height))
1908 			DRM_ERROR("Unable to find initial modes\n");
1909 
1910 		DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
1911 			      width, height);
1912 
1913 		drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1914 	}
1915 
1916 	/* need to set the modesets up here for use later */
1917 	/* fill out the connector<->crtc mappings into the modesets */
1918 	for (i = 0; i < fb_helper->crtc_count; i++) {
1919 		modeset = &fb_helper->crtc_info[i].mode_set;
1920 		modeset->num_connectors = 0;
1921 		modeset->fb = NULL;
1922 	}
1923 
1924 	for (i = 0; i < fb_helper->connector_count; i++) {
1925 		struct drm_display_mode *mode = modes[i];
1926 		struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1927 		struct drm_fb_offset *offset = &offsets[i];
1928 		modeset = &fb_crtc->mode_set;
1929 
1930 		if (mode && fb_crtc) {
1931 			DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
1932 				      mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
1933 			fb_crtc->desired_mode = mode;
1934 			fb_crtc->x = offset->x;
1935 			fb_crtc->y = offset->y;
1936 			if (modeset->mode)
1937 				drm_mode_destroy(dev, modeset->mode);
1938 			modeset->mode = drm_mode_duplicate(dev,
1939 							   fb_crtc->desired_mode);
1940 			modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1941 			modeset->fb = fb_helper->fb;
1942 			modeset->x = offset->x;
1943 			modeset->y = offset->y;
1944 		}
1945 	}
1946 
1947 	/* Clear out any old modes if there are no more connected outputs. */
1948 	for (i = 0; i < fb_helper->crtc_count; i++) {
1949 		modeset = &fb_helper->crtc_info[i].mode_set;
1950 		if (modeset->num_connectors == 0) {
1951 			BUG_ON(modeset->fb);
1952 			if (modeset->mode)
1953 				drm_mode_destroy(dev, modeset->mode);
1954 			modeset->mode = NULL;
1955 		}
1956 	}
1957 out:
1958 	kfree(crtcs);
1959 	kfree(modes);
1960 	kfree(offsets);
1961 	kfree(enabled);
1962 }
1963 
1964 /**
1965  * drm_fb_helper_initial_config - setup a sane initial connector configuration
1966  * @fb_helper: fb_helper device struct
1967  * @bpp_sel: bpp value to use for the framebuffer configuration
1968  *
1969  * Scans the CRTCs and connectors and tries to put together an initial setup.
1970  * At the moment, this is a cloned configuration across all heads with
1971  * a new framebuffer object as the backing store.
1972  *
1973  * Note that this also registers the fbdev and so allows userspace to call into
1974  * the driver through the fbdev interfaces.
1975  *
1976  * This function will call down into the ->fb_probe callback to let
1977  * the driver allocate and initialize the fbdev info structure and the drm
1978  * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
1979  * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
1980  * values for the fbdev info structure.
1981  *
1982  * RETURNS:
1983  * Zero if everything went ok, nonzero otherwise.
1984  */
1985 int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1986 {
1987 	struct drm_device *dev = fb_helper->dev;
1988 	int count = 0;
1989 
1990 	mutex_lock(&dev->mode_config.mutex);
1991 	count = drm_fb_helper_probe_connector_modes(fb_helper,
1992 						    dev->mode_config.max_width,
1993 						    dev->mode_config.max_height);
1994 	mutex_unlock(&dev->mode_config.mutex);
1995 	/*
1996 	 * we shouldn't end up with no modes here.
1997 	 */
1998 	if (count == 0)
1999 		dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
2000 
2001 	drm_setup_crtcs(fb_helper);
2002 
2003 	return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
2004 }
2005 EXPORT_SYMBOL(drm_fb_helper_initial_config);
2006 
2007 /**
2008  * drm_fb_helper_hotplug_event - respond to a hotplug notification by
2009  *                               probing all the outputs attached to the fb
2010  * @fb_helper: the drm_fb_helper
2011  *
2012  * Scan the connectors attached to the fb_helper and try to put together a
2013  * setup after *notification of a change in output configuration.
2014  *
2015  * Called at runtime, takes the mode config locks to be able to check/change the
2016  * modeset configuration. Must be run from process context (which usually means
2017  * either the output polling work or a work item launched from the driver's
2018  * hotplug interrupt).
2019  *
2020  * Note that drivers may call this even before calling
2021  * drm_fb_helper_initial_config but only aftert drm_fb_helper_init. This allows
2022  * for a race-free fbcon setup and will make sure that the fbdev emulation will
2023  * not miss any hotplug events.
2024  *
2025  * RETURNS:
2026  * 0 on success and a non-zero error code otherwise.
2027  */
2028 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
2029 {
2030 	struct drm_device *dev = fb_helper->dev;
2031 	u32 max_width, max_height;
2032 
2033 	mutex_lock(&fb_helper->dev->mode_config.mutex);
2034 	if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
2035 		fb_helper->delayed_hotplug = true;
2036 		mutex_unlock(&fb_helper->dev->mode_config.mutex);
2037 		return 0;
2038 	}
2039 	DRM_DEBUG_KMS("\n");
2040 
2041 	max_width = fb_helper->fb->width;
2042 	max_height = fb_helper->fb->height;
2043 
2044 	drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height);
2045 	mutex_unlock(&fb_helper->dev->mode_config.mutex);
2046 
2047 	drm_modeset_lock_all(dev);
2048 	drm_setup_crtcs(fb_helper);
2049 	drm_modeset_unlock_all(dev);
2050 #if 0
2051 	drm_fb_helper_set_par(fb_helper->fbdev);
2052 #endif
2053 
2054 	return 0;
2055 }
2056 EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
2057 
2058 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
2059  * but the module doesn't depend on any fb console symbols.  At least
2060  * attempt to load fbcon to avoid leaving the system without a usable console.
2061  */
2062 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
2063 static int __init drm_fb_helper_modinit(void)
2064 {
2065 	const char *name = "fbcon";
2066 	struct module *fbcon;
2067 
2068 	mutex_lock(&module_mutex);
2069 	fbcon = find_module(name);
2070 	mutex_unlock(&module_mutex);
2071 
2072 	if (!fbcon)
2073 		request_module_nowait(name);
2074 	return 0;
2075 }
2076 
2077 module_init(drm_fb_helper_modinit);
2078 #endif
2079