xref: /dragonfly/sys/dev/drm/drm_fb_helper.c (revision 4be47400)
1 /*
2  * Copyright (c) 2006-2009 Red Hat Inc.
3  * Copyright (c) 2006-2008 Intel Corporation
4  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
5  *
6  * DRM framebuffer helper functions
7  *
8  * Permission to use, copy, modify, distribute, and sell this software and its
9  * documentation for any purpose is hereby granted without fee, provided that
10  * the above copyright notice appear in all copies and that both that copyright
11  * notice and this permission notice appear in supporting documentation, and
12  * that the name of the copyright holders not be used in advertising or
13  * publicity pertaining to distribution of the software without specific,
14  * written prior permission.  The copyright holders make no representations
15  * about the suitability of this software for any purpose.  It is provided "as
16  * is" without express or implied warranty.
17  *
18  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24  * OF THIS SOFTWARE.
25  *
26  * Authors:
27  *      Dave Airlie <airlied@linux.ie>
28  *      Jesse Barnes <jesse.barnes@intel.com>
29  */
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31 
32 #include <linux/console.h>
33 #include <linux/kernel.h>
34 #include <linux/sysrq.h>
35 #include <linux/slab.h>
36 #include <linux/module.h>
37 #include <drm/drmP.h>
38 #include <drm/drm_crtc.h>
39 #include <drm/drm_fb_helper.h>
40 #include <drm/drm_crtc_helper.h>
41 #include <drm/drm_atomic.h>
42 #include <drm/drm_atomic_helper.h>
43 
44 #include "drm_crtc_helper_internal.h"
45 
46 static bool drm_fbdev_emulation = true;
47 module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600);
48 MODULE_PARM_DESC(fbdev_emulation,
49 		 "Enable legacy fbdev emulation [default=true]");
50 
51 static LINUX_LIST_HEAD(kernel_fb_helper_list);
52 static DEFINE_MUTEX(kernel_fb_helper_lock);
53 
54 /**
55  * DOC: fbdev helpers
56  *
57  * The fb helper functions are useful to provide an fbdev on top of a drm kernel
58  * mode setting driver. They can be used mostly independently from the crtc
59  * helper functions used by many drivers to implement the kernel mode setting
60  * interfaces.
61  *
62  * Initialization is done as a four-step process with drm_fb_helper_prepare(),
63  * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and
64  * drm_fb_helper_initial_config(). Drivers with fancier requirements than the
65  * default behaviour can override the third step with their own code.
66  * Teardown is done with drm_fb_helper_fini().
67  *
68  * At runtime drivers should restore the fbdev console by calling
69  * drm_fb_helper_restore_fbdev_mode_unlocked() from their ->lastclose callback.
70  * They should also notify the fb helper code from updates to the output
71  * configuration by calling drm_fb_helper_hotplug_event(). For easier
72  * integration with the output polling code in drm_crtc_helper.c the modeset
73  * code provides a ->output_poll_changed callback.
74  *
75  * All other functions exported by the fb helper library can be used to
76  * implement the fbdev driver interface by the driver.
77  *
78  * It is possible, though perhaps somewhat tricky, to implement race-free
79  * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
80  * helper must be called first to initialize the minimum required to make
81  * hotplug detection work. Drivers also need to make sure to properly set up
82  * the dev->mode_config.funcs member. After calling drm_kms_helper_poll_init()
83  * it is safe to enable interrupts and start processing hotplug events. At the
84  * same time, drivers should initialize all modeset objects such as CRTCs,
85  * encoders and connectors. To finish up the fbdev helper initialization, the
86  * drm_fb_helper_init() function is called. To probe for all attached displays
87  * and set up an initial configuration using the detected hardware, drivers
88  * should call drm_fb_helper_single_add_all_connectors() followed by
89  * drm_fb_helper_initial_config().
90  *
91  * If &drm_framebuffer_funcs ->dirty is set, the
92  * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will
93  * accumulate changes and schedule &drm_fb_helper ->dirty_work to run right
94  * away. This worker then calls the dirty() function ensuring that it will
95  * always run in process context since the fb_*() function could be running in
96  * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io
97  * callback it will also schedule dirty_work with the damage collected from the
98  * mmap page writes.
99  */
100 
101 #define drm_fb_helper_for_each_connector(fbh, i__) \
102 	for (({ lockdep_assert_held(&(fbh)->dev->mode_config.mutex); }), \
103 	     i__ = 0; i__ < (fbh)->connector_count; i__++)
104 
105 /**
106  * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
107  * 					       emulation helper
108  * @fb_helper: fbdev initialized with drm_fb_helper_init
109  *
110  * This functions adds all the available connectors for use with the given
111  * fb_helper. This is a separate step to allow drivers to freely assign
112  * connectors to the fbdev, e.g. if some are reserved for special purposes or
113  * not adequate to be used for the fbcon.
114  *
115  * This function is protected against concurrent connector hotadds/removals
116  * using drm_fb_helper_add_one_connector() and
117  * drm_fb_helper_remove_one_connector().
118  */
119 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
120 {
121 	struct drm_device *dev = fb_helper->dev;
122 	struct drm_connector *connector;
123 	int i, ret;
124 
125 	if (!drm_fbdev_emulation)
126 		return 0;
127 
128 	mutex_lock(&dev->mode_config.mutex);
129 	drm_for_each_connector(connector, dev) {
130 		ret = drm_fb_helper_add_one_connector(fb_helper, connector);
131 
132 		if (ret)
133 			goto fail;
134 	}
135 	mutex_unlock(&dev->mode_config.mutex);
136 	return 0;
137 fail:
138 	drm_fb_helper_for_each_connector(fb_helper, i) {
139 		struct drm_fb_helper_connector *fb_helper_connector =
140 			fb_helper->connector_info[i];
141 
142 		drm_connector_unreference(fb_helper_connector->connector);
143 
144 		kfree(fb_helper_connector);
145 		fb_helper->connector_info[i] = NULL;
146 	}
147 	fb_helper->connector_count = 0;
148 	mutex_unlock(&dev->mode_config.mutex);
149 
150 	return ret;
151 }
152 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
153 
154 int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, struct drm_connector *connector)
155 {
156 	struct drm_fb_helper_connector **temp;
157 	struct drm_fb_helper_connector *fb_helper_connector;
158 
159 	if (!drm_fbdev_emulation)
160 		return 0;
161 
162 	WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
163 	if (fb_helper->connector_count + 1 > fb_helper->connector_info_alloc_count) {
164 		temp = krealloc(fb_helper->connector_info,
165 				sizeof(struct drm_fb_helper_connector *) * (fb_helper->connector_count + 1),
166 				M_DRM, GFP_KERNEL);
167 		if (!temp)
168 			return -ENOMEM;
169 
170 		fb_helper->connector_info_alloc_count = fb_helper->connector_count + 1;
171 		fb_helper->connector_info = temp;
172 	}
173 
174 
175 	fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
176 	if (!fb_helper_connector)
177 		return -ENOMEM;
178 
179 	drm_connector_reference(connector);
180 	fb_helper_connector->connector = connector;
181 	fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
182 	return 0;
183 }
184 EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
185 
186 int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
187 				       struct drm_connector *connector)
188 {
189 	struct drm_fb_helper_connector *fb_helper_connector;
190 	int i, j;
191 
192 	if (!drm_fbdev_emulation)
193 		return 0;
194 
195 	WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
196 
197 	for (i = 0; i < fb_helper->connector_count; i++) {
198 		if (fb_helper->connector_info[i]->connector == connector)
199 			break;
200 	}
201 
202 	if (i == fb_helper->connector_count)
203 		return -EINVAL;
204 	fb_helper_connector = fb_helper->connector_info[i];
205 	drm_connector_unreference(fb_helper_connector->connector);
206 
207 	for (j = i + 1; j < fb_helper->connector_count; j++) {
208 		fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
209 	}
210 	fb_helper->connector_count--;
211 	kfree(fb_helper_connector);
212 
213 	return 0;
214 }
215 EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
216 
217 static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
218 {
219 	uint16_t *r_base, *g_base, *b_base;
220 	int i;
221 
222 	if (helper->funcs->gamma_get == NULL)
223 		return;
224 
225 	r_base = crtc->gamma_store;
226 	g_base = r_base + crtc->gamma_size;
227 	b_base = g_base + crtc->gamma_size;
228 
229 	for (i = 0; i < crtc->gamma_size; i++)
230 		helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
231 }
232 
233 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
234 {
235 	uint16_t *r_base, *g_base, *b_base;
236 
237 	if (crtc->funcs->gamma_set == NULL)
238 		return;
239 
240 	r_base = crtc->gamma_store;
241 	g_base = r_base + crtc->gamma_size;
242 	b_base = g_base + crtc->gamma_size;
243 
244 	crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
245 }
246 
247 /**
248  * drm_fb_helper_debug_enter - implementation for ->fb_debug_enter
249  * @info: fbdev registered by the helper
250  */
251 int drm_fb_helper_debug_enter(struct fb_info *info)
252 {
253 	struct drm_fb_helper *helper = info->par;
254 	const struct drm_crtc_helper_funcs *funcs;
255 	int i;
256 
257 	list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
258 		for (i = 0; i < helper->crtc_count; i++) {
259 			struct drm_mode_set *mode_set =
260 				&helper->crtc_info[i].mode_set;
261 
262 			if (!mode_set->crtc->enabled)
263 				continue;
264 
265 			funcs =	mode_set->crtc->helper_private;
266 			if (funcs->mode_set_base_atomic == NULL)
267 				continue;
268 
269 			drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
270 			funcs->mode_set_base_atomic(mode_set->crtc,
271 						    mode_set->fb,
272 						    mode_set->x,
273 						    mode_set->y,
274 						    ENTER_ATOMIC_MODE_SET);
275 		}
276 	}
277 
278 	return 0;
279 }
280 EXPORT_SYMBOL(drm_fb_helper_debug_enter);
281 
282 /* Find the real fb for a given fb helper CRTC */
283 static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
284 {
285 	struct drm_device *dev = crtc->dev;
286 	struct drm_crtc *c;
287 
288 	drm_for_each_crtc(c, dev) {
289 		if (crtc->base.id == c->base.id)
290 			return c->primary->fb;
291 	}
292 
293 	return NULL;
294 }
295 
296 /**
297  * drm_fb_helper_debug_leave - implementation for ->fb_debug_leave
298  * @info: fbdev registered by the helper
299  */
300 int drm_fb_helper_debug_leave(struct fb_info *info)
301 {
302 	struct drm_fb_helper *helper = info->par;
303 	struct drm_crtc *crtc;
304 	const struct drm_crtc_helper_funcs *funcs;
305 	struct drm_framebuffer *fb;
306 	int i;
307 
308 	for (i = 0; i < helper->crtc_count; i++) {
309 		struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
310 		crtc = mode_set->crtc;
311 		funcs = crtc->helper_private;
312 		fb = drm_mode_config_fb(crtc);
313 
314 		if (!crtc->enabled)
315 			continue;
316 
317 		if (!fb) {
318 			DRM_ERROR("no fb to restore??\n");
319 			continue;
320 		}
321 
322 		if (funcs->mode_set_base_atomic == NULL)
323 			continue;
324 
325 		drm_fb_helper_restore_lut_atomic(mode_set->crtc);
326 		funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
327 					    crtc->y, LEAVE_ATOMIC_MODE_SET);
328 	}
329 
330 	return 0;
331 }
332 EXPORT_SYMBOL(drm_fb_helper_debug_leave);
333 
334 static int restore_fbdev_mode_atomic(struct drm_fb_helper *fb_helper)
335 {
336 	struct drm_device *dev = fb_helper->dev;
337 	struct drm_plane *plane;
338 	struct drm_atomic_state *state;
339 	int i, ret;
340 	unsigned plane_mask;
341 
342 	state = drm_atomic_state_alloc(dev);
343 	if (!state)
344 		return -ENOMEM;
345 
346 	state->acquire_ctx = dev->mode_config.acquire_ctx;
347 retry:
348 	plane_mask = 0;
349 	drm_for_each_plane(plane, dev) {
350 		struct drm_plane_state *plane_state;
351 
352 		plane_state = drm_atomic_get_plane_state(state, plane);
353 		if (IS_ERR(plane_state)) {
354 			ret = PTR_ERR(plane_state);
355 			goto fail;
356 		}
357 
358 		plane_state->rotation = DRM_ROTATE_0;
359 
360 		plane->old_fb = plane->fb;
361 		plane_mask |= 1 << drm_plane_index(plane);
362 
363 		/* disable non-primary: */
364 		if (plane->type == DRM_PLANE_TYPE_PRIMARY)
365 			continue;
366 
367 		ret = __drm_atomic_helper_disable_plane(plane, plane_state);
368 		if (ret != 0)
369 			goto fail;
370 	}
371 
372 	for(i = 0; i < fb_helper->crtc_count; i++) {
373 		struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
374 
375 		ret = __drm_atomic_helper_set_config(mode_set, state);
376 		if (ret != 0)
377 			goto fail;
378 	}
379 
380 	ret = drm_atomic_commit(state);
381 
382 fail:
383 	drm_atomic_clean_old_fb(dev, plane_mask, ret);
384 
385 	if (ret == -EDEADLK)
386 		goto backoff;
387 
388 	drm_atomic_state_put(state);
389 	return ret;
390 
391 backoff:
392 	drm_atomic_state_clear(state);
393 	drm_atomic_legacy_backoff(state);
394 
395 	goto retry;
396 }
397 
398 static int restore_fbdev_mode(struct drm_fb_helper *fb_helper)
399 {
400 	struct drm_device *dev = fb_helper->dev;
401 	struct drm_plane *plane;
402 	int i;
403 
404 	drm_warn_on_modeset_not_all_locked(dev);
405 
406 	if (dev->mode_config.funcs->atomic_commit)
407 		return restore_fbdev_mode_atomic(fb_helper);
408 
409 	drm_for_each_plane(plane, dev) {
410 		if (plane->type != DRM_PLANE_TYPE_PRIMARY)
411 			drm_plane_force_disable(plane);
412 
413 		if (plane->rotation_property)
414 			drm_mode_plane_set_obj_prop(plane,
415 						    plane->rotation_property,
416 						    DRM_ROTATE_0);
417 	}
418 
419 	for (i = 0; i < fb_helper->crtc_count; i++) {
420 		struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
421 		struct drm_crtc *crtc = mode_set->crtc;
422 		int ret;
423 
424 		if (crtc->funcs->cursor_set2) {
425 			ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
426 			if (ret)
427 				return ret;
428 		} else if (crtc->funcs->cursor_set) {
429 			ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
430 			if (ret)
431 				return ret;
432 		}
433 
434 		ret = drm_mode_set_config_internal(mode_set);
435 		if (ret)
436 			return ret;
437 	}
438 
439 	return 0;
440 }
441 
442 /**
443  * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
444  * @fb_helper: fbcon to restore
445  *
446  * This should be called from driver's drm ->lastclose callback
447  * when implementing an fbcon on top of kms using this helper. This ensures that
448  * the user isn't greeted with a black screen when e.g. X dies.
449  *
450  * RETURNS:
451  * Zero if everything went ok, negative error code otherwise.
452  */
453 int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
454 {
455 	struct drm_device *dev = fb_helper->dev;
456 	bool do_delayed;
457 	int ret;
458 
459 	if (!drm_fbdev_emulation)
460 		return -ENODEV;
461 
462 	drm_modeset_lock_all(dev);
463 	ret = restore_fbdev_mode(fb_helper);
464 
465 	do_delayed = fb_helper->delayed_hotplug;
466 	if (do_delayed)
467 		fb_helper->delayed_hotplug = false;
468 	drm_modeset_unlock_all(dev);
469 
470 	if (do_delayed)
471 		drm_fb_helper_hotplug_event(fb_helper);
472 	return ret;
473 }
474 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
475 
476 static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
477 {
478 	struct drm_device *dev = fb_helper->dev;
479 	struct drm_crtc *crtc;
480 	int bound = 0, crtcs_bound = 0;
481 
482 	/* Sometimes user space wants everything disabled, so don't steal the
483 	 * display if there's a master. */
484 	if (READ_ONCE(dev->master))
485 		return false;
486 
487 	drm_for_each_crtc(crtc, dev) {
488 		if (crtc->primary->fb)
489 			crtcs_bound++;
490 		if (crtc->primary->fb == fb_helper->fb)
491 			bound++;
492 	}
493 
494 	if (bound < crtcs_bound)
495 		return false;
496 
497 	return true;
498 }
499 
500 #ifdef CONFIG_MAGIC_SYSRQ
501 /*
502  * restore fbcon display for all kms driver's using this helper, used for sysrq
503  * and panic handling.
504  */
505 static bool drm_fb_helper_force_kernel_mode(void)
506 {
507 	bool ret, error = false;
508 	struct drm_fb_helper *helper;
509 
510 	if (list_empty(&kernel_fb_helper_list))
511 		return false;
512 
513 	list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
514 		struct drm_device *dev = helper->dev;
515 
516 		if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
517 			continue;
518 
519 		drm_modeset_lock_all(dev);
520 		ret = restore_fbdev_mode(helper);
521 		if (ret)
522 			error = true;
523 		drm_modeset_unlock_all(dev);
524 	}
525 	return error;
526 }
527 
528 #if 0
529 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
530 {
531 	bool ret;
532 	ret = drm_fb_helper_force_kernel_mode();
533 	if (ret == true)
534 		DRM_ERROR("Failed to restore crtc configuration\n");
535 }
536 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
537 
538 static void drm_fb_helper_sysrq(int dummy1)
539 {
540 	schedule_work(&drm_fb_helper_restore_work);
541 }
542 
543 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
544 	.handler = drm_fb_helper_sysrq,
545 	.help_msg = "force-fb(V)",
546 	.action_msg = "Restore framebuffer console",
547 };
548 #else
549 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
550 #endif
551 #endif
552 
553 static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
554 {
555 	struct drm_fb_helper *fb_helper = info->par;
556 	struct drm_device *dev = fb_helper->dev;
557 	struct drm_crtc *crtc;
558 	struct drm_connector *connector;
559 	int i, j;
560 
561 	/*
562 	 * For each CRTC in this fb, turn the connectors on/off.
563 	 */
564 	drm_modeset_lock_all(dev);
565 	if (!drm_fb_helper_is_bound(fb_helper)) {
566 		drm_modeset_unlock_all(dev);
567 		return;
568 	}
569 
570 	for (i = 0; i < fb_helper->crtc_count; i++) {
571 		crtc = fb_helper->crtc_info[i].mode_set.crtc;
572 
573 		if (!crtc->enabled)
574 			continue;
575 
576 		/* Walk the connectors & encoders on this fb turning them on/off */
577 		drm_fb_helper_for_each_connector(fb_helper, j) {
578 			connector = fb_helper->connector_info[j]->connector;
579 			connector->funcs->dpms(connector, dpms_mode);
580 			drm_object_property_set_value(&connector->base,
581 				dev->mode_config.dpms_property, dpms_mode);
582 		}
583 	}
584 	drm_modeset_unlock_all(dev);
585 }
586 
587 /**
588  * drm_fb_helper_blank - implementation for ->fb_blank
589  * @blank: desired blanking state
590  * @info: fbdev registered by the helper
591  */
592 int drm_fb_helper_blank(int blank, struct fb_info *info)
593 {
594 	if (oops_in_progress)
595 		return -EBUSY;
596 
597 	switch (blank) {
598 	/* Display: On; HSync: On, VSync: On */
599 	case FB_BLANK_UNBLANK:
600 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
601 		break;
602 #if 0
603 	/* Display: Off; HSync: On, VSync: On */
604 	case FB_BLANK_NORMAL:
605 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
606 		break;
607 	/* Display: Off; HSync: Off, VSync: On */
608 	case FB_BLANK_HSYNC_SUSPEND:
609 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
610 		break;
611 	/* Display: Off; HSync: On, VSync: Off */
612 	case FB_BLANK_VSYNC_SUSPEND:
613 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
614 		break;
615 #endif
616 	/* Display: Off; HSync: Off, VSync: Off */
617 	case FB_BLANK_POWERDOWN:
618 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
619 		break;
620 	}
621 	return 0;
622 }
623 EXPORT_SYMBOL(drm_fb_helper_blank);
624 
625 static void drm_fb_helper_modeset_release(struct drm_fb_helper *helper,
626 					  struct drm_mode_set *modeset)
627 {
628 	int i;
629 
630 	for (i = 0; i < modeset->num_connectors; i++) {
631 		drm_connector_unreference(modeset->connectors[i]);
632 		modeset->connectors[i] = NULL;
633 	}
634 	modeset->num_connectors = 0;
635 
636 	drm_mode_destroy(helper->dev, modeset->mode);
637 	modeset->mode = NULL;
638 
639 	/* FIXME should hold a ref? */
640 	modeset->fb = NULL;
641 }
642 
643 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
644 {
645 	int i;
646 
647 	for (i = 0; i < helper->connector_count; i++) {
648 		drm_connector_unreference(helper->connector_info[i]->connector);
649 		kfree(helper->connector_info[i]);
650 	}
651 	kfree(helper->connector_info);
652 
653 	for (i = 0; i < helper->crtc_count; i++) {
654 		struct drm_mode_set *modeset = &helper->crtc_info[i].mode_set;
655 
656 		drm_fb_helper_modeset_release(helper, modeset);
657 		kfree(modeset->connectors);
658 	}
659 	kfree(helper->crtc_info);
660 }
661 
662 static void drm_fb_helper_resume_worker(struct work_struct *work)
663 {
664 #if 0
665 	struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
666 						    resume_work);
667 
668 	console_lock();
669 	fb_set_suspend(helper->fbdev, 0);
670 	console_unlock();
671 #endif
672 }
673 
674 static void drm_fb_helper_dirty_work(struct work_struct *work)
675 {
676 	struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
677 						    dirty_work);
678 	struct drm_clip_rect *clip = &helper->dirty_clip;
679 	struct drm_clip_rect clip_copy;
680 	unsigned long flags;
681 
682 	spin_lock_irqsave(&helper->dirty_lock, flags);
683 	clip_copy = *clip;
684 	clip->x1 = clip->y1 = ~0;
685 	clip->x2 = clip->y2 = 0;
686 	spin_unlock_irqrestore(&helper->dirty_lock, flags);
687 
688 	/* call dirty callback only when it has been really touched */
689 	if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2)
690 		helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1);
691 }
692 
693 /**
694  * drm_fb_helper_prepare - setup a drm_fb_helper structure
695  * @dev: DRM device
696  * @helper: driver-allocated fbdev helper structure to set up
697  * @funcs: pointer to structure of functions associate with this helper
698  *
699  * Sets up the bare minimum to make the framebuffer helper usable. This is
700  * useful to implement race-free initialization of the polling helpers.
701  */
702 void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
703 			   const struct drm_fb_helper_funcs *funcs)
704 {
705 	INIT_LIST_HEAD(&helper->kernel_fb_list);
706 	lockinit(&helper->dirty_lock, "drm_fb_helper dirty_lock", 0, LK_CANRECURSE);
707 	INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
708 	INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work);
709 	helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0;
710 	helper->funcs = funcs;
711 	helper->dev = dev;
712 }
713 EXPORT_SYMBOL(drm_fb_helper_prepare);
714 
715 /**
716  * drm_fb_helper_init - initialize a drm_fb_helper structure
717  * @dev: drm device
718  * @fb_helper: driver-allocated fbdev helper structure to initialize
719  * @crtc_count: maximum number of crtcs to support in this fbdev emulation
720  * @max_conn_count: max connector count
721  *
722  * This allocates the structures for the fbdev helper with the given limits.
723  * Note that this won't yet touch the hardware (through the driver interfaces)
724  * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
725  * to allow driver writes more control over the exact init sequence.
726  *
727  * Drivers must call drm_fb_helper_prepare() before calling this function.
728  *
729  * RETURNS:
730  * Zero if everything went ok, nonzero otherwise.
731  */
732 int drm_fb_helper_init(struct drm_device *dev,
733 		       struct drm_fb_helper *fb_helper,
734 		       int crtc_count, int max_conn_count)
735 {
736 	struct drm_crtc *crtc;
737 	int i;
738 
739 	if (!drm_fbdev_emulation)
740 		return 0;
741 
742 	if (!max_conn_count)
743 		return -EINVAL;
744 
745 	fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
746 	if (!fb_helper->crtc_info)
747 		return -ENOMEM;
748 
749 	fb_helper->crtc_count = crtc_count;
750 	fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
751 	if (!fb_helper->connector_info) {
752 		kfree(fb_helper->crtc_info);
753 		return -ENOMEM;
754 	}
755 	fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
756 	fb_helper->connector_count = 0;
757 
758 	for (i = 0; i < crtc_count; i++) {
759 		fb_helper->crtc_info[i].mode_set.connectors =
760 			kcalloc(max_conn_count,
761 				sizeof(struct drm_connector *),
762 				GFP_KERNEL);
763 
764 		if (!fb_helper->crtc_info[i].mode_set.connectors)
765 			goto out_free;
766 		fb_helper->crtc_info[i].mode_set.num_connectors = 0;
767 	}
768 
769 	i = 0;
770 	drm_for_each_crtc(crtc, dev) {
771 		fb_helper->crtc_info[i].mode_set.crtc = crtc;
772 		i++;
773 	}
774 
775 	return 0;
776 out_free:
777 	drm_fb_helper_crtc_free(fb_helper);
778 	return -ENOMEM;
779 }
780 EXPORT_SYMBOL(drm_fb_helper_init);
781 
782 /**
783  * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
784  * @fb_helper: driver-allocated fbdev helper
785  *
786  * A helper to alloc fb_info and the members cmap and apertures. Called
787  * by the driver within the fb_probe fb_helper callback function.
788  *
789  * RETURNS:
790  * fb_info pointer if things went okay, pointer containing error code
791  * otherwise
792  */
793 struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
794 {
795 	struct device *dev = fb_helper->dev->dev;
796 	struct fb_info *info;
797 
798 	info = framebuffer_alloc(0, dev);
799 	if (!info)
800 		return ERR_PTR(-ENOMEM);
801 
802 #if 0
803 	ret = fb_alloc_cmap(&info->cmap, 256, 0);
804 	if (ret)
805 		goto err_release;
806 
807 	info->apertures = alloc_apertures(1);
808 	if (!info->apertures) {
809 		ret = -ENOMEM;
810 		goto err_free_cmap;
811 	}
812 #endif
813 
814 	fb_helper->fbdev = info;
815 
816 	return info;
817 
818 #if 0
819 err_free_cmap:
820 	fb_dealloc_cmap(&info->cmap);
821 err_release:
822 	framebuffer_release(info);
823 	return ERR_PTR(ret);
824 #endif
825 }
826 EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
827 
828 /**
829  * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
830  * @fb_helper: driver-allocated fbdev helper
831  *
832  * A wrapper around unregister_framebuffer, to release the fb_info
833  * framebuffer device
834  */
835 void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
836 {
837 	if (fb_helper && fb_helper->fbdev)
838 		unregister_framebuffer(fb_helper->fbdev);
839 }
840 EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
841 
842 /**
843  * drm_fb_helper_release_fbi - dealloc fb_info and its members
844  * @fb_helper: driver-allocated fbdev helper
845  *
846  * A helper to free memory taken by fb_info and the members cmap and
847  * apertures
848  */
849 void drm_fb_helper_release_fbi(struct drm_fb_helper *fb_helper)
850 {
851 	if (fb_helper) {
852 		struct fb_info *info = fb_helper->fbdev;
853 
854 		if (info) {
855 #if 0
856 			if (info->cmap.len)
857 				fb_dealloc_cmap(&info->cmap);
858 #endif
859 			framebuffer_release(info);
860 		}
861 
862 		fb_helper->fbdev = NULL;
863 	}
864 }
865 EXPORT_SYMBOL(drm_fb_helper_release_fbi);
866 
867 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
868 {
869 	if (!drm_fbdev_emulation)
870 		return;
871 
872 	cancel_work_sync(&fb_helper->resume_work);
873 	cancel_work_sync(&fb_helper->dirty_work);
874 
875 	mutex_lock(&kernel_fb_helper_lock);
876 	if (!list_empty(&fb_helper->kernel_fb_list)) {
877 		list_del(&fb_helper->kernel_fb_list);
878 		if (list_empty(&kernel_fb_helper_list)) {
879 #if 0
880 			unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
881 #endif
882 		}
883 	}
884 	mutex_unlock(&kernel_fb_helper_lock);
885 
886 	drm_fb_helper_crtc_free(fb_helper);
887 
888 }
889 EXPORT_SYMBOL(drm_fb_helper_fini);
890 
891 #if 0
892 /**
893  * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
894  * @fb_helper: driver-allocated fbdev helper
895  *
896  * A wrapper around unlink_framebuffer implemented by fbdev core
897  */
898 void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
899 {
900 	if (fb_helper && fb_helper->fbdev)
901 		unlink_framebuffer(fb_helper->fbdev);
902 }
903 EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
904 
905 static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y,
906 				u32 width, u32 height)
907 {
908 	struct drm_fb_helper *helper = info->par;
909 	struct drm_clip_rect *clip = &helper->dirty_clip;
910 	unsigned long flags;
911 
912 	if (!helper->fb->funcs->dirty)
913 		return;
914 
915 	spin_lock_irqsave(&helper->dirty_lock, flags);
916 	clip->x1 = min_t(u32, clip->x1, x);
917 	clip->y1 = min_t(u32, clip->y1, y);
918 	clip->x2 = max_t(u32, clip->x2, x + width);
919 	clip->y2 = max_t(u32, clip->y2, y + height);
920 	spin_unlock_irqrestore(&helper->dirty_lock, flags);
921 
922 	schedule_work(&helper->dirty_work);
923 }
924 
925 /**
926  * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
927  * @info: fb_info struct pointer
928  * @pagelist: list of dirty mmap framebuffer pages
929  *
930  * This function is used as the &fb_deferred_io ->deferred_io
931  * callback function for flushing the fbdev mmap writes.
932  */
933 void drm_fb_helper_deferred_io(struct fb_info *info,
934 			       struct list_head *pagelist)
935 {
936 	unsigned long start, end, min, max;
937 	struct page *page;
938 	u32 y1, y2;
939 
940 	min = ULONG_MAX;
941 	max = 0;
942 	list_for_each_entry(page, pagelist, lru) {
943 		start = page->index << PAGE_SHIFT;
944 		end = start + PAGE_SIZE - 1;
945 		min = min(min, start);
946 		max = max(max, end);
947 	}
948 
949 	if (min < max) {
950 		y1 = min / info->fix.line_length;
951 		y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length),
952 			   info->var.yres);
953 		drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1);
954 	}
955 }
956 EXPORT_SYMBOL(drm_fb_helper_deferred_io);
957 
958 /**
959  * drm_fb_helper_sys_read - wrapper around fb_sys_read
960  * @info: fb_info struct pointer
961  * @buf: userspace buffer to read from framebuffer memory
962  * @count: number of bytes to read from framebuffer memory
963  * @ppos: read offset within framebuffer memory
964  *
965  * A wrapper around fb_sys_read implemented by fbdev core
966  */
967 ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
968 			       size_t count, loff_t *ppos)
969 {
970 	return fb_sys_read(info, buf, count, ppos);
971 }
972 EXPORT_SYMBOL(drm_fb_helper_sys_read);
973 
974 /**
975  * drm_fb_helper_sys_write - wrapper around fb_sys_write
976  * @info: fb_info struct pointer
977  * @buf: userspace buffer to write to framebuffer memory
978  * @count: number of bytes to write to framebuffer memory
979  * @ppos: write offset within framebuffer memory
980  *
981  * A wrapper around fb_sys_write implemented by fbdev core
982  */
983 ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
984 				size_t count, loff_t *ppos)
985 {
986 	ssize_t ret;
987 
988 	ret = fb_sys_write(info, buf, count, ppos);
989 	if (ret > 0)
990 		drm_fb_helper_dirty(info, 0, 0, info->var.xres,
991 				    info->var.yres);
992 
993 	return ret;
994 }
995 EXPORT_SYMBOL(drm_fb_helper_sys_write);
996 
997 /**
998  * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
999  * @info: fbdev registered by the helper
1000  * @rect: info about rectangle to fill
1001  *
1002  * A wrapper around sys_fillrect implemented by fbdev core
1003  */
1004 void drm_fb_helper_sys_fillrect(struct fb_info *info,
1005 				const struct fb_fillrect *rect)
1006 {
1007 	sys_fillrect(info, rect);
1008 	drm_fb_helper_dirty(info, rect->dx, rect->dy,
1009 			    rect->width, rect->height);
1010 }
1011 EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
1012 
1013 /**
1014  * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
1015  * @info: fbdev registered by the helper
1016  * @area: info about area to copy
1017  *
1018  * A wrapper around sys_copyarea implemented by fbdev core
1019  */
1020 void drm_fb_helper_sys_copyarea(struct fb_info *info,
1021 				const struct fb_copyarea *area)
1022 {
1023 	sys_copyarea(info, area);
1024 	drm_fb_helper_dirty(info, area->dx, area->dy,
1025 			    area->width, area->height);
1026 }
1027 EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
1028 
1029 /**
1030  * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
1031  * @info: fbdev registered by the helper
1032  * @image: info about image to blit
1033  *
1034  * A wrapper around sys_imageblit implemented by fbdev core
1035  */
1036 void drm_fb_helper_sys_imageblit(struct fb_info *info,
1037 				 const struct fb_image *image)
1038 {
1039 	sys_imageblit(info, image);
1040 	drm_fb_helper_dirty(info, image->dx, image->dy,
1041 			    image->width, image->height);
1042 }
1043 EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
1044 
1045 /**
1046  * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
1047  * @info: fbdev registered by the helper
1048  * @rect: info about rectangle to fill
1049  *
1050  * A wrapper around cfb_imageblit implemented by fbdev core
1051  */
1052 void drm_fb_helper_cfb_fillrect(struct fb_info *info,
1053 				const struct fb_fillrect *rect)
1054 {
1055 	cfb_fillrect(info, rect);
1056 	drm_fb_helper_dirty(info, rect->dx, rect->dy,
1057 			    rect->width, rect->height);
1058 }
1059 EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
1060 
1061 /**
1062  * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
1063  * @info: fbdev registered by the helper
1064  * @area: info about area to copy
1065  *
1066  * A wrapper around cfb_copyarea implemented by fbdev core
1067  */
1068 void drm_fb_helper_cfb_copyarea(struct fb_info *info,
1069 				const struct fb_copyarea *area)
1070 {
1071 	cfb_copyarea(info, area);
1072 	drm_fb_helper_dirty(info, area->dx, area->dy,
1073 			    area->width, area->height);
1074 }
1075 EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
1076 
1077 /**
1078  * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
1079  * @info: fbdev registered by the helper
1080  * @image: info about image to blit
1081  *
1082  * A wrapper around cfb_imageblit implemented by fbdev core
1083  */
1084 void drm_fb_helper_cfb_imageblit(struct fb_info *info,
1085 				 const struct fb_image *image)
1086 {
1087 	cfb_imageblit(info, image);
1088 	drm_fb_helper_dirty(info, image->dx, image->dy,
1089 			    image->width, image->height);
1090 }
1091 EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
1092 
1093 /**
1094  * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
1095  * @fb_helper: driver-allocated fbdev helper
1096  * @suspend: whether to suspend or resume
1097  *
1098  * A wrapper around fb_set_suspend implemented by fbdev core.
1099  * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
1100  * the lock yourself
1101  */
1102 void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
1103 {
1104 	if (fb_helper && fb_helper->fbdev)
1105 		fb_set_suspend(fb_helper->fbdev, suspend);
1106 }
1107 EXPORT_SYMBOL(drm_fb_helper_set_suspend);
1108 
1109 /**
1110  * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
1111  *                                      takes the console lock
1112  * @fb_helper: driver-allocated fbdev helper
1113  * @suspend: whether to suspend or resume
1114  *
1115  * A wrapper around fb_set_suspend() that takes the console lock. If the lock
1116  * isn't available on resume, a worker is tasked with waiting for the lock
1117  * to become available. The console lock can be pretty contented on resume
1118  * due to all the printk activity.
1119  *
1120  * This function can be called multiple times with the same state since
1121  * &fb_info->state is checked to see if fbdev is running or not before locking.
1122  *
1123  * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
1124  */
1125 void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
1126 					bool suspend)
1127 {
1128 	if (!fb_helper || !fb_helper->fbdev)
1129 		return;
1130 
1131 	/* make sure there's no pending/ongoing resume */
1132 	flush_work(&fb_helper->resume_work);
1133 
1134 	if (suspend) {
1135 		if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING)
1136 			return;
1137 
1138 		console_lock();
1139 
1140 	} else {
1141 		if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING)
1142 			return;
1143 
1144 		if (!console_trylock()) {
1145 			schedule_work(&fb_helper->resume_work);
1146 			return;
1147 		}
1148 	}
1149 
1150 	fb_set_suspend(fb_helper->fbdev, suspend);
1151 	console_unlock();
1152 }
1153 EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked);
1154 
1155 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
1156 		     u16 blue, u16 regno, struct fb_info *info)
1157 {
1158 	struct drm_fb_helper *fb_helper = info->par;
1159 	struct drm_framebuffer *fb = fb_helper->fb;
1160 
1161 	if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
1162 		u32 *palette;
1163 		u32 value;
1164 		/* place color in psuedopalette */
1165 		if (regno > 16)
1166 			return -EINVAL;
1167 		palette = (u32 *)info->pseudo_palette;
1168 		red >>= (16 - info->var.red.length);
1169 		green >>= (16 - info->var.green.length);
1170 		blue >>= (16 - info->var.blue.length);
1171 		value = (red << info->var.red.offset) |
1172 			(green << info->var.green.offset) |
1173 			(blue << info->var.blue.offset);
1174 		if (info->var.transp.length > 0) {
1175 			u32 mask = (1 << info->var.transp.length) - 1;
1176 			mask <<= info->var.transp.offset;
1177 			value |= mask;
1178 		}
1179 		palette[regno] = value;
1180 		return 0;
1181 	}
1182 
1183 	/*
1184 	 * The driver really shouldn't advertise pseudo/directcolor
1185 	 * visuals if it can't deal with the palette.
1186 	 */
1187 	if (WARN_ON(!fb_helper->funcs->gamma_set ||
1188 		    !fb_helper->funcs->gamma_get))
1189 		return -EINVAL;
1190 
1191 	WARN_ON(fb->bits_per_pixel != 8);
1192 
1193 	fb_helper->funcs->gamma_set(crtc, red, green, blue, regno);
1194 
1195 	return 0;
1196 }
1197 
1198 /**
1199  * drm_fb_helper_setcmap - implementation for ->fb_setcmap
1200  * @cmap: cmap to set
1201  * @info: fbdev registered by the helper
1202  */
1203 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1204 {
1205 	struct drm_fb_helper *fb_helper = info->par;
1206 	struct drm_device *dev = fb_helper->dev;
1207 	const struct drm_crtc_helper_funcs *crtc_funcs;
1208 	u16 *red, *green, *blue, *transp;
1209 	struct drm_crtc *crtc;
1210 	int i, j, rc = 0;
1211 	int start;
1212 
1213 	if (oops_in_progress)
1214 		return -EBUSY;
1215 
1216 	drm_modeset_lock_all(dev);
1217 	if (!drm_fb_helper_is_bound(fb_helper)) {
1218 		drm_modeset_unlock_all(dev);
1219 		return -EBUSY;
1220 	}
1221 
1222 	for (i = 0; i < fb_helper->crtc_count; i++) {
1223 		crtc = fb_helper->crtc_info[i].mode_set.crtc;
1224 		crtc_funcs = crtc->helper_private;
1225 
1226 		red = cmap->red;
1227 		green = cmap->green;
1228 		blue = cmap->blue;
1229 		transp = cmap->transp;
1230 		start = cmap->start;
1231 
1232 		for (j = 0; j < cmap->len; j++) {
1233 			u16 hred, hgreen, hblue, htransp = 0xffff;
1234 
1235 			hred = *red++;
1236 			hgreen = *green++;
1237 			hblue = *blue++;
1238 
1239 			if (transp)
1240 				htransp = *transp++;
1241 
1242 			rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
1243 			if (rc)
1244 				goto out;
1245 		}
1246 		if (crtc_funcs->load_lut)
1247 			crtc_funcs->load_lut(crtc);
1248 	}
1249  out:
1250 	drm_modeset_unlock_all(dev);
1251 	return rc;
1252 }
1253 EXPORT_SYMBOL(drm_fb_helper_setcmap);
1254 
1255 /**
1256  * drm_fb_helper_check_var - implementation for ->fb_check_var
1257  * @var: screeninfo to check
1258  * @info: fbdev registered by the helper
1259  */
1260 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
1261 			    struct fb_info *info)
1262 {
1263 	struct drm_fb_helper *fb_helper = info->par;
1264 	struct drm_framebuffer *fb = fb_helper->fb;
1265 	int depth;
1266 
1267 	if (var->pixclock != 0 || in_dbg_master())
1268 		return -EINVAL;
1269 
1270 	/*
1271 	 * Changes struct fb_var_screeninfo are currently not pushed back
1272 	 * to KMS, hence fail if different settings are requested.
1273 	 */
1274 	if (var->bits_per_pixel != fb->bits_per_pixel ||
1275 	    var->xres > fb->width || var->yres > fb->height ||
1276 	    var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
1277 		DRM_DEBUG("fb requested width/height/bpp can't fit in current fb "
1278 			  "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1279 			  var->xres, var->yres, var->bits_per_pixel,
1280 			  var->xres_virtual, var->yres_virtual,
1281 			  fb->width, fb->height, fb->bits_per_pixel);
1282 		return -EINVAL;
1283 	}
1284 
1285 	switch (var->bits_per_pixel) {
1286 	case 16:
1287 		depth = (var->green.length == 6) ? 16 : 15;
1288 		break;
1289 	case 32:
1290 		depth = (var->transp.length > 0) ? 32 : 24;
1291 		break;
1292 	default:
1293 		depth = var->bits_per_pixel;
1294 		break;
1295 	}
1296 
1297 	switch (depth) {
1298 	case 8:
1299 		var->red.offset = 0;
1300 		var->green.offset = 0;
1301 		var->blue.offset = 0;
1302 		var->red.length = 8;
1303 		var->green.length = 8;
1304 		var->blue.length = 8;
1305 		var->transp.length = 0;
1306 		var->transp.offset = 0;
1307 		break;
1308 	case 15:
1309 		var->red.offset = 10;
1310 		var->green.offset = 5;
1311 		var->blue.offset = 0;
1312 		var->red.length = 5;
1313 		var->green.length = 5;
1314 		var->blue.length = 5;
1315 		var->transp.length = 1;
1316 		var->transp.offset = 15;
1317 		break;
1318 	case 16:
1319 		var->red.offset = 11;
1320 		var->green.offset = 5;
1321 		var->blue.offset = 0;
1322 		var->red.length = 5;
1323 		var->green.length = 6;
1324 		var->blue.length = 5;
1325 		var->transp.length = 0;
1326 		var->transp.offset = 0;
1327 		break;
1328 	case 24:
1329 		var->red.offset = 16;
1330 		var->green.offset = 8;
1331 		var->blue.offset = 0;
1332 		var->red.length = 8;
1333 		var->green.length = 8;
1334 		var->blue.length = 8;
1335 		var->transp.length = 0;
1336 		var->transp.offset = 0;
1337 		break;
1338 	case 32:
1339 		var->red.offset = 16;
1340 		var->green.offset = 8;
1341 		var->blue.offset = 0;
1342 		var->red.length = 8;
1343 		var->green.length = 8;
1344 		var->blue.length = 8;
1345 		var->transp.length = 8;
1346 		var->transp.offset = 24;
1347 		break;
1348 	default:
1349 		return -EINVAL;
1350 	}
1351 	return 0;
1352 }
1353 EXPORT_SYMBOL(drm_fb_helper_check_var);
1354 #endif
1355 
1356 /**
1357  * drm_fb_helper_set_par - implementation for ->fb_set_par
1358  * @info: fbdev registered by the helper
1359  *
1360  * This will let fbcon do the mode init and is called at initialization time by
1361  * the fbdev core when registering the driver, and later on through the hotplug
1362  * callback.
1363  */
1364 int drm_fb_helper_set_par(struct fb_info *info)
1365 {
1366 	struct drm_fb_helper *fb_helper = info->par;
1367 #if 0
1368 	struct fb_var_screeninfo *var = &info->var;
1369 #endif
1370 
1371 	if (oops_in_progress)
1372 		return -EBUSY;
1373 
1374 #if 0
1375 	if (var->pixclock != 0) {
1376 		DRM_ERROR("PIXEL CLOCK SET\n");
1377 		return -EINVAL;
1378 	}
1379 #endif
1380 
1381 	drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
1382 
1383 	return 0;
1384 }
1385 EXPORT_SYMBOL(drm_fb_helper_set_par);
1386 
1387 #if 0
1388 static int pan_display_atomic(struct fb_var_screeninfo *var,
1389 			      struct fb_info *info)
1390 {
1391 	struct drm_fb_helper *fb_helper = info->par;
1392 	struct drm_device *dev = fb_helper->dev;
1393 	struct drm_atomic_state *state;
1394 	struct drm_plane *plane;
1395 	int i, ret;
1396 	unsigned plane_mask;
1397 
1398 	state = drm_atomic_state_alloc(dev);
1399 	if (!state)
1400 		return -ENOMEM;
1401 
1402 	state->acquire_ctx = dev->mode_config.acquire_ctx;
1403 retry:
1404 	plane_mask = 0;
1405 	for(i = 0; i < fb_helper->crtc_count; i++) {
1406 		struct drm_mode_set *mode_set;
1407 
1408 		mode_set = &fb_helper->crtc_info[i].mode_set;
1409 
1410 		mode_set->x = var->xoffset;
1411 		mode_set->y = var->yoffset;
1412 
1413 		ret = __drm_atomic_helper_set_config(mode_set, state);
1414 		if (ret != 0)
1415 			goto fail;
1416 
1417 		plane = mode_set->crtc->primary;
1418 		plane_mask |= (1 << drm_plane_index(plane));
1419 		plane->old_fb = plane->fb;
1420 	}
1421 
1422 	ret = drm_atomic_commit(state);
1423 	if (ret != 0)
1424 		goto fail;
1425 
1426 	info->var.xoffset = var->xoffset;
1427 	info->var.yoffset = var->yoffset;
1428 
1429 fail:
1430 	drm_atomic_clean_old_fb(dev, plane_mask, ret);
1431 
1432 	if (ret == -EDEADLK)
1433 		goto backoff;
1434 
1435 	drm_atomic_state_put(state);
1436 	return ret;
1437 
1438 backoff:
1439 	drm_atomic_state_clear(state);
1440 	drm_atomic_legacy_backoff(state);
1441 
1442 	goto retry;
1443 }
1444 
1445 /**
1446  * drm_fb_helper_pan_display - implementation for ->fb_pan_display
1447  * @var: updated screen information
1448  * @info: fbdev registered by the helper
1449  */
1450 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1451 			      struct fb_info *info)
1452 {
1453 	struct drm_fb_helper *fb_helper = info->par;
1454 	struct drm_device *dev = fb_helper->dev;
1455 	struct drm_mode_set *modeset;
1456 	int ret = 0;
1457 	int i;
1458 
1459 	if (oops_in_progress)
1460 		return -EBUSY;
1461 
1462 	drm_modeset_lock_all(dev);
1463 	if (!drm_fb_helper_is_bound(fb_helper)) {
1464 		drm_modeset_unlock_all(dev);
1465 		return -EBUSY;
1466 	}
1467 
1468 	if (dev->mode_config.funcs->atomic_commit) {
1469 		ret = pan_display_atomic(var, info);
1470 		goto unlock;
1471 	}
1472 
1473 	for (i = 0; i < fb_helper->crtc_count; i++) {
1474 		modeset = &fb_helper->crtc_info[i].mode_set;
1475 
1476 		modeset->x = var->xoffset;
1477 		modeset->y = var->yoffset;
1478 
1479 		if (modeset->num_connectors) {
1480 			ret = drm_mode_set_config_internal(modeset);
1481 			if (!ret) {
1482 				info->var.xoffset = var->xoffset;
1483 				info->var.yoffset = var->yoffset;
1484 			}
1485 		}
1486 	}
1487 unlock:
1488 	drm_modeset_unlock_all(dev);
1489 	return ret;
1490 }
1491 EXPORT_SYMBOL(drm_fb_helper_pan_display);
1492 #endif
1493 
1494 /*
1495  * Allocates the backing storage and sets up the fbdev info structure through
1496  * the ->fb_probe callback and then registers the fbdev and sets up the panic
1497  * notifier.
1498  */
1499 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1500 					 int preferred_bpp)
1501 {
1502 	int ret = 0;
1503 	int crtc_count = 0;
1504 	int i;
1505 	struct drm_fb_helper_surface_size sizes;
1506 	int gamma_size = 0;
1507 
1508 	memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1509 	sizes.surface_depth = 24;
1510 	sizes.surface_bpp = 32;
1511 	sizes.fb_width = (unsigned)-1;
1512 	sizes.fb_height = (unsigned)-1;
1513 
1514 	/* if driver picks 8 or 16 by default use that
1515 	   for both depth/bpp */
1516 	if (preferred_bpp != sizes.surface_bpp)
1517 		sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
1518 
1519 	/* first up get a count of crtcs now in use and new min/maxes width/heights */
1520 	drm_fb_helper_for_each_connector(fb_helper, i) {
1521 		struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
1522 		struct drm_cmdline_mode *cmdline_mode;
1523 
1524 		cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
1525 
1526 		if (cmdline_mode->bpp_specified) {
1527 			switch (cmdline_mode->bpp) {
1528 			case 8:
1529 				sizes.surface_depth = sizes.surface_bpp = 8;
1530 				break;
1531 			case 15:
1532 				sizes.surface_depth = 15;
1533 				sizes.surface_bpp = 16;
1534 				break;
1535 			case 16:
1536 				sizes.surface_depth = sizes.surface_bpp = 16;
1537 				break;
1538 			case 24:
1539 				sizes.surface_depth = sizes.surface_bpp = 24;
1540 				break;
1541 			case 32:
1542 				sizes.surface_depth = 24;
1543 				sizes.surface_bpp = 32;
1544 				break;
1545 			}
1546 			break;
1547 		}
1548 	}
1549 
1550 	crtc_count = 0;
1551 	for (i = 0; i < fb_helper->crtc_count; i++) {
1552 		struct drm_display_mode *desired_mode;
1553 		struct drm_mode_set *mode_set;
1554 		int x, y, j;
1555 		/* in case of tile group, are we the last tile vert or horiz?
1556 		 * If no tile group you are always the last one both vertically
1557 		 * and horizontally
1558 		 */
1559 		bool lastv = true, lasth = true;
1560 
1561 		desired_mode = fb_helper->crtc_info[i].desired_mode;
1562 		mode_set = &fb_helper->crtc_info[i].mode_set;
1563 
1564 		if (!desired_mode)
1565 			continue;
1566 
1567 		crtc_count++;
1568 
1569 		x = fb_helper->crtc_info[i].x;
1570 		y = fb_helper->crtc_info[i].y;
1571 
1572 		if (gamma_size == 0)
1573 			gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1574 
1575 		sizes.surface_width  = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
1576 		sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
1577 
1578 		for (j = 0; j < mode_set->num_connectors; j++) {
1579 			struct drm_connector *connector = mode_set->connectors[j];
1580 			if (connector->has_tile) {
1581 				lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
1582 				lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
1583 				/* cloning to multiple tiles is just crazy-talk, so: */
1584 				break;
1585 			}
1586 		}
1587 
1588 		if (lasth)
1589 			sizes.fb_width  = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
1590 		if (lastv)
1591 			sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
1592 	}
1593 
1594 	if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
1595 		/* hmm everyone went away - assume VGA cable just fell out
1596 		   and will come back later. */
1597 		DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
1598 		sizes.fb_width = sizes.surface_width = 1024;
1599 		sizes.fb_height = sizes.surface_height = 768;
1600 	}
1601 
1602 	/* push down into drivers */
1603 	ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1604 	if (ret < 0)
1605 		return ret;
1606 
1607 	/*
1608 	 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1609 	 * events, but at init time drm_setup_crtcs needs to be called before
1610 	 * the fb is allocated (since we need to figure out the desired size of
1611 	 * the fb before we can allocate it ...). Hence we need to fix things up
1612 	 * here again.
1613 	 */
1614 	for (i = 0; i < fb_helper->crtc_count; i++)
1615 		if (fb_helper->crtc_info[i].mode_set.num_connectors)
1616 			fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1617 
1618 	return 0;
1619 }
1620 
1621 #if 0
1622 /**
1623  * drm_fb_helper_fill_fix - initializes fixed fbdev information
1624  * @info: fbdev registered by the helper
1625  * @pitch: desired pitch
1626  * @depth: desired depth
1627  *
1628  * Helper to fill in the fixed fbdev information useful for a non-accelerated
1629  * fbdev emulations. Drivers which support acceleration methods which impose
1630  * additional constraints need to set up their own limits.
1631  *
1632  * Drivers should call this (or their equivalent setup code) from their
1633  * ->fb_probe callback.
1634  */
1635 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1636 			    uint32_t depth)
1637 {
1638 	info->fix.type = FB_TYPE_PACKED_PIXELS;
1639 	info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1640 		FB_VISUAL_TRUECOLOR;
1641 	info->fix.mmio_start = 0;
1642 	info->fix.mmio_len = 0;
1643 	info->fix.type_aux = 0;
1644 	info->fix.xpanstep = 1; /* doing it in hw */
1645 	info->fix.ypanstep = 1; /* doing it in hw */
1646 	info->fix.ywrapstep = 0;
1647 	info->fix.accel = FB_ACCEL_NONE;
1648 
1649 	info->fix.line_length = pitch;
1650 	return;
1651 }
1652 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1653 
1654 /**
1655  * drm_fb_helper_fill_var - initalizes variable fbdev information
1656  * @info: fbdev instance to set up
1657  * @fb_helper: fb helper instance to use as template
1658  * @fb_width: desired fb width
1659  * @fb_height: desired fb height
1660  *
1661  * Sets up the variable fbdev metainformation from the given fb helper instance
1662  * and the drm framebuffer allocated in fb_helper->fb.
1663  *
1664  * Drivers should call this (or their equivalent setup code) from their
1665  * ->fb_probe callback after having allocated the fbdev backing
1666  * storage framebuffer.
1667  */
1668 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
1669 			    uint32_t fb_width, uint32_t fb_height)
1670 {
1671 	struct drm_framebuffer *fb = fb_helper->fb;
1672 	info->pseudo_palette = fb_helper->pseudo_palette;
1673 	info->var.xres_virtual = fb->width;
1674 	info->var.yres_virtual = fb->height;
1675 	info->var.bits_per_pixel = fb->bits_per_pixel;
1676 	info->var.accel_flags = FB_ACCELF_TEXT;
1677 	info->var.xoffset = 0;
1678 	info->var.yoffset = 0;
1679 	info->var.activate = FB_ACTIVATE_NOW;
1680 	info->var.height = -1;
1681 	info->var.width = -1;
1682 
1683 	switch (fb->depth) {
1684 	case 8:
1685 		info->var.red.offset = 0;
1686 		info->var.green.offset = 0;
1687 		info->var.blue.offset = 0;
1688 		info->var.red.length = 8; /* 8bit DAC */
1689 		info->var.green.length = 8;
1690 		info->var.blue.length = 8;
1691 		info->var.transp.offset = 0;
1692 		info->var.transp.length = 0;
1693 		break;
1694 	case 15:
1695 		info->var.red.offset = 10;
1696 		info->var.green.offset = 5;
1697 		info->var.blue.offset = 0;
1698 		info->var.red.length = 5;
1699 		info->var.green.length = 5;
1700 		info->var.blue.length = 5;
1701 		info->var.transp.offset = 15;
1702 		info->var.transp.length = 1;
1703 		break;
1704 	case 16:
1705 		info->var.red.offset = 11;
1706 		info->var.green.offset = 5;
1707 		info->var.blue.offset = 0;
1708 		info->var.red.length = 5;
1709 		info->var.green.length = 6;
1710 		info->var.blue.length = 5;
1711 		info->var.transp.offset = 0;
1712 		break;
1713 	case 24:
1714 		info->var.red.offset = 16;
1715 		info->var.green.offset = 8;
1716 		info->var.blue.offset = 0;
1717 		info->var.red.length = 8;
1718 		info->var.green.length = 8;
1719 		info->var.blue.length = 8;
1720 		info->var.transp.offset = 0;
1721 		info->var.transp.length = 0;
1722 		break;
1723 	case 32:
1724 		info->var.red.offset = 16;
1725 		info->var.green.offset = 8;
1726 		info->var.blue.offset = 0;
1727 		info->var.red.length = 8;
1728 		info->var.green.length = 8;
1729 		info->var.blue.length = 8;
1730 		info->var.transp.offset = 24;
1731 		info->var.transp.length = 8;
1732 		break;
1733 	default:
1734 		break;
1735 	}
1736 
1737 	info->var.xres = fb_width;
1738 	info->var.yres = fb_height;
1739 }
1740 EXPORT_SYMBOL(drm_fb_helper_fill_var);
1741 #endif
1742 
1743 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1744 					       uint32_t maxX,
1745 					       uint32_t maxY)
1746 {
1747 	struct drm_connector *connector;
1748 	int count = 0;
1749 	int i;
1750 
1751 	drm_fb_helper_for_each_connector(fb_helper, i) {
1752 		connector = fb_helper->connector_info[i]->connector;
1753 		count += connector->funcs->fill_modes(connector, maxX, maxY);
1754 	}
1755 
1756 	return count;
1757 }
1758 
1759 struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1760 {
1761 	struct drm_display_mode *mode;
1762 
1763 	list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1764 		if (mode->hdisplay > width ||
1765 		    mode->vdisplay > height)
1766 			continue;
1767 		if (mode->type & DRM_MODE_TYPE_PREFERRED)
1768 			return mode;
1769 	}
1770 	return NULL;
1771 }
1772 EXPORT_SYMBOL(drm_has_preferred_mode);
1773 
1774 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1775 {
1776 	return fb_connector->connector->cmdline_mode.specified;
1777 }
1778 
1779 struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1780 						      int width, int height)
1781 {
1782 	struct drm_cmdline_mode *cmdline_mode;
1783 	struct drm_display_mode *mode;
1784 	bool prefer_non_interlace;
1785 
1786 	cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
1787 	if (cmdline_mode->specified == false)
1788 		return NULL;
1789 
1790 	/* attempt to find a matching mode in the list of modes
1791 	 *  we have gotten so far, if not add a CVT mode that conforms
1792 	 */
1793 	if (cmdline_mode->rb || cmdline_mode->margins)
1794 		goto create_mode;
1795 
1796 	prefer_non_interlace = !cmdline_mode->interlace;
1797 again:
1798 	list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1799 		/* check width/height */
1800 		if (mode->hdisplay != cmdline_mode->xres ||
1801 		    mode->vdisplay != cmdline_mode->yres)
1802 			continue;
1803 
1804 		if (cmdline_mode->refresh_specified) {
1805 			if (mode->vrefresh != cmdline_mode->refresh)
1806 				continue;
1807 		}
1808 
1809 		if (cmdline_mode->interlace) {
1810 			if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1811 				continue;
1812 		} else if (prefer_non_interlace) {
1813 			if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1814 				continue;
1815 		}
1816 		return mode;
1817 	}
1818 
1819 	if (prefer_non_interlace) {
1820 		prefer_non_interlace = false;
1821 		goto again;
1822 	}
1823 
1824 create_mode:
1825 	mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1826 						 cmdline_mode);
1827 	list_add(&mode->head, &fb_helper_conn->connector->modes);
1828 	return mode;
1829 }
1830 EXPORT_SYMBOL(drm_pick_cmdline_mode);
1831 
1832 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1833 {
1834 	bool enable;
1835 
1836 	if (strict)
1837 		enable = connector->status == connector_status_connected;
1838 	else
1839 		enable = connector->status != connector_status_disconnected;
1840 
1841 	return enable;
1842 }
1843 
1844 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1845 				  bool *enabled)
1846 {
1847 	bool any_enabled = false;
1848 	struct drm_connector *connector;
1849 	int i = 0;
1850 
1851 	drm_fb_helper_for_each_connector(fb_helper, i) {
1852 		connector = fb_helper->connector_info[i]->connector;
1853 		enabled[i] = drm_connector_enabled(connector, true);
1854 		DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1855 			  enabled[i] ? "yes" : "no");
1856 		any_enabled |= enabled[i];
1857 	}
1858 
1859 	if (any_enabled)
1860 		return;
1861 
1862 	drm_fb_helper_for_each_connector(fb_helper, i) {
1863 		connector = fb_helper->connector_info[i]->connector;
1864 		enabled[i] = drm_connector_enabled(connector, false);
1865 	}
1866 }
1867 
1868 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1869 			      struct drm_display_mode **modes,
1870 			      struct drm_fb_offset *offsets,
1871 			      bool *enabled, int width, int height)
1872 {
1873 	int count, i, j;
1874 	bool can_clone = false;
1875 	struct drm_fb_helper_connector *fb_helper_conn;
1876 	struct drm_display_mode *dmt_mode, *mode;
1877 
1878 	/* only contemplate cloning in the single crtc case */
1879 	if (fb_helper->crtc_count > 1)
1880 		return false;
1881 
1882 	count = 0;
1883 	drm_fb_helper_for_each_connector(fb_helper, i) {
1884 		if (enabled[i])
1885 			count++;
1886 	}
1887 
1888 	/* only contemplate cloning if more than one connector is enabled */
1889 	if (count <= 1)
1890 		return false;
1891 
1892 	/* check the command line or if nothing common pick 1024x768 */
1893 	can_clone = true;
1894 	drm_fb_helper_for_each_connector(fb_helper, i) {
1895 		if (!enabled[i])
1896 			continue;
1897 		fb_helper_conn = fb_helper->connector_info[i];
1898 		modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1899 		if (!modes[i]) {
1900 			can_clone = false;
1901 			break;
1902 		}
1903 		for (j = 0; j < i; j++) {
1904 			if (!enabled[j])
1905 				continue;
1906 			if (!drm_mode_equal(modes[j], modes[i]))
1907 				can_clone = false;
1908 		}
1909 	}
1910 
1911 	if (can_clone) {
1912 		DRM_DEBUG_KMS("can clone using command line\n");
1913 		return true;
1914 	}
1915 
1916 	/* try and find a 1024x768 mode on each connector */
1917 	can_clone = true;
1918 	dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
1919 
1920 	drm_fb_helper_for_each_connector(fb_helper, i) {
1921 		if (!enabled[i])
1922 			continue;
1923 
1924 		fb_helper_conn = fb_helper->connector_info[i];
1925 		list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1926 			if (drm_mode_equal(mode, dmt_mode))
1927 				modes[i] = mode;
1928 		}
1929 		if (!modes[i])
1930 			can_clone = false;
1931 	}
1932 
1933 	if (can_clone) {
1934 		DRM_DEBUG_KMS("can clone using 1024x768\n");
1935 		return true;
1936 	}
1937 	DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1938 	return false;
1939 }
1940 
1941 static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
1942 				struct drm_display_mode **modes,
1943 				struct drm_fb_offset *offsets,
1944 				int idx,
1945 				int h_idx, int v_idx)
1946 {
1947 	struct drm_fb_helper_connector *fb_helper_conn;
1948 	int i;
1949 	int hoffset = 0, voffset = 0;
1950 
1951 	drm_fb_helper_for_each_connector(fb_helper, i) {
1952 		fb_helper_conn = fb_helper->connector_info[i];
1953 		if (!fb_helper_conn->connector->has_tile)
1954 			continue;
1955 
1956 		if (!modes[i] && (h_idx || v_idx)) {
1957 			DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
1958 				      fb_helper_conn->connector->base.id);
1959 			continue;
1960 		}
1961 		if (fb_helper_conn->connector->tile_h_loc < h_idx)
1962 			hoffset += modes[i]->hdisplay;
1963 
1964 		if (fb_helper_conn->connector->tile_v_loc < v_idx)
1965 			voffset += modes[i]->vdisplay;
1966 	}
1967 	offsets[idx].x = hoffset;
1968 	offsets[idx].y = voffset;
1969 	DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
1970 	return 0;
1971 }
1972 
1973 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1974 				 struct drm_display_mode **modes,
1975 				 struct drm_fb_offset *offsets,
1976 				 bool *enabled, int width, int height)
1977 {
1978 	struct drm_fb_helper_connector *fb_helper_conn;
1979 	const u64 mask = BIT_ULL(fb_helper->connector_count) - 1;
1980 	u64 conn_configured = 0;
1981 	int tile_pass = 0;
1982 	int i;
1983 
1984 retry:
1985 	drm_fb_helper_for_each_connector(fb_helper, i) {
1986 		fb_helper_conn = fb_helper->connector_info[i];
1987 
1988 		if (conn_configured & BIT_ULL(i))
1989 			continue;
1990 
1991 		if (enabled[i] == false) {
1992 			conn_configured |= BIT_ULL(i);
1993 			continue;
1994 		}
1995 
1996 		/* first pass over all the untiled connectors */
1997 		if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
1998 			continue;
1999 
2000 		if (tile_pass == 1) {
2001 			if (fb_helper_conn->connector->tile_h_loc != 0 ||
2002 			    fb_helper_conn->connector->tile_v_loc != 0)
2003 				continue;
2004 
2005 		} else {
2006 			if (fb_helper_conn->connector->tile_h_loc != tile_pass -1 &&
2007 			    fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
2008 			/* if this tile_pass doesn't cover any of the tiles - keep going */
2009 				continue;
2010 
2011 			/* find the tile offsets for this pass - need
2012 			   to find all tiles left and above */
2013 			drm_get_tile_offsets(fb_helper, modes, offsets,
2014 					     i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
2015 		}
2016 		DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
2017 			      fb_helper_conn->connector->base.id);
2018 
2019 		/* got for command line mode first */
2020 		modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
2021 		if (!modes[i]) {
2022 			DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
2023 				      fb_helper_conn->connector->base.id, fb_helper_conn->connector->tile_group ? fb_helper_conn->connector->tile_group->id : 0);
2024 			modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
2025 		}
2026 		/* No preferred modes, pick one off the list */
2027 		if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
2028 			list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
2029 				break;
2030 		}
2031 		DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
2032 			  "none");
2033 		conn_configured |= BIT_ULL(i);
2034 	}
2035 
2036 	if ((conn_configured & mask) != mask) {
2037 		tile_pass++;
2038 		goto retry;
2039 	}
2040 	return true;
2041 }
2042 
2043 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
2044 			  struct drm_fb_helper_crtc **best_crtcs,
2045 			  struct drm_display_mode **modes,
2046 			  int n, int width, int height)
2047 {
2048 	int c, o;
2049 	struct drm_connector *connector;
2050 	const struct drm_connector_helper_funcs *connector_funcs;
2051 	struct drm_encoder *encoder;
2052 	int my_score, best_score, score;
2053 	struct drm_fb_helper_crtc **crtcs, *crtc;
2054 	struct drm_fb_helper_connector *fb_helper_conn;
2055 
2056 	if (n == fb_helper->connector_count)
2057 		return 0;
2058 
2059 	fb_helper_conn = fb_helper->connector_info[n];
2060 	connector = fb_helper_conn->connector;
2061 
2062 	best_crtcs[n] = NULL;
2063 	best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
2064 	if (modes[n] == NULL)
2065 		return best_score;
2066 
2067 	crtcs = kzalloc(fb_helper->connector_count *
2068 			sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
2069 	if (!crtcs)
2070 		return best_score;
2071 
2072 	my_score = 1;
2073 	if (connector->status == connector_status_connected)
2074 		my_score++;
2075 	if (drm_has_cmdline_mode(fb_helper_conn))
2076 		my_score++;
2077 	if (drm_has_preferred_mode(fb_helper_conn, width, height))
2078 		my_score++;
2079 
2080 	connector_funcs = connector->helper_private;
2081 
2082 	/*
2083 	 * If the DRM device implements atomic hooks and ->best_encoder() is
2084 	 * NULL we fallback to the default drm_atomic_helper_best_encoder()
2085 	 * helper.
2086 	 */
2087 	if (fb_helper->dev->mode_config.funcs->atomic_commit &&
2088 	    !connector_funcs->best_encoder)
2089 		encoder = drm_atomic_helper_best_encoder(connector);
2090 	else
2091 		encoder = connector_funcs->best_encoder(connector);
2092 
2093 	if (!encoder)
2094 		goto out;
2095 
2096 	/* select a crtc for this connector and then attempt to configure
2097 	   remaining connectors */
2098 	for (c = 0; c < fb_helper->crtc_count; c++) {
2099 		crtc = &fb_helper->crtc_info[c];
2100 
2101 		if ((encoder->possible_crtcs & (1 << c)) == 0)
2102 			continue;
2103 
2104 		for (o = 0; o < n; o++)
2105 			if (best_crtcs[o] == crtc)
2106 				break;
2107 
2108 		if (o < n) {
2109 			/* ignore cloning unless only a single crtc */
2110 			if (fb_helper->crtc_count > 1)
2111 				continue;
2112 
2113 			if (!drm_mode_equal(modes[o], modes[n]))
2114 				continue;
2115 		}
2116 
2117 		crtcs[n] = crtc;
2118 		memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
2119 		score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
2120 						  width, height);
2121 		if (score > best_score) {
2122 			best_score = score;
2123 			memcpy(best_crtcs, crtcs,
2124 			       fb_helper->connector_count *
2125 			       sizeof(struct drm_fb_helper_crtc *));
2126 		}
2127 	}
2128 out:
2129 	kfree(crtcs);
2130 	return best_score;
2131 }
2132 
2133 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper,
2134 			    u32 width, u32 height)
2135 {
2136 	struct drm_device *dev = fb_helper->dev;
2137 	struct drm_fb_helper_crtc **crtcs;
2138 	struct drm_display_mode **modes;
2139 	struct drm_fb_offset *offsets;
2140 	bool *enabled;
2141 	int i;
2142 
2143 	DRM_DEBUG_KMS("\n");
2144 	if (drm_fb_helper_probe_connector_modes(fb_helper, width, height) == 0)
2145 		DRM_DEBUG_KMS("No connectors reported connected with modes\n");
2146 
2147 	/* prevent concurrent modification of connector_count by hotplug */
2148 	lockdep_assert_held(&fb_helper->dev->mode_config.mutex);
2149 
2150 	crtcs = kcalloc(fb_helper->connector_count,
2151 			sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
2152 	modes = kcalloc(fb_helper->connector_count,
2153 			sizeof(struct drm_display_mode *), GFP_KERNEL);
2154 	offsets = kcalloc(fb_helper->connector_count,
2155 			  sizeof(struct drm_fb_offset), GFP_KERNEL);
2156 	enabled = kcalloc(fb_helper->connector_count,
2157 			  sizeof(bool), GFP_KERNEL);
2158 	if (!crtcs || !modes || !enabled || !offsets) {
2159 		DRM_ERROR("Memory allocation failed\n");
2160 		goto out;
2161 	}
2162 
2163 	drm_enable_connectors(fb_helper, enabled);
2164 
2165 	if (!(fb_helper->funcs->initial_config &&
2166 	      fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
2167 					       offsets,
2168 					       enabled, width, height))) {
2169 		memset(modes, 0, fb_helper->connector_count*sizeof(modes[0]));
2170 		memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0]));
2171 		memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0]));
2172 
2173 		if (!drm_target_cloned(fb_helper, modes, offsets,
2174 				       enabled, width, height) &&
2175 		    !drm_target_preferred(fb_helper, modes, offsets,
2176 					  enabled, width, height))
2177 			DRM_ERROR("Unable to find initial modes\n");
2178 
2179 		DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
2180 			      width, height);
2181 
2182 		drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
2183 	}
2184 
2185 	/* need to set the modesets up here for use later */
2186 	/* fill out the connector<->crtc mappings into the modesets */
2187 	for (i = 0; i < fb_helper->crtc_count; i++)
2188 		drm_fb_helper_modeset_release(fb_helper,
2189 					      &fb_helper->crtc_info[i].mode_set);
2190 
2191 	drm_fb_helper_for_each_connector(fb_helper, i) {
2192 		struct drm_display_mode *mode = modes[i];
2193 		struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
2194 		struct drm_fb_offset *offset = &offsets[i];
2195 		struct drm_mode_set *modeset = &fb_crtc->mode_set;
2196 
2197 		if (mode && fb_crtc) {
2198 			struct drm_connector *connector =
2199 				fb_helper->connector_info[i]->connector;
2200 
2201 			DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
2202 				      mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
2203 
2204 			fb_crtc->desired_mode = mode;
2205 			fb_crtc->x = offset->x;
2206 			fb_crtc->y = offset->y;
2207 			modeset->mode = drm_mode_duplicate(dev,
2208 							   fb_crtc->desired_mode);
2209 			drm_connector_reference(connector);
2210 			modeset->connectors[modeset->num_connectors++] = connector;
2211 			modeset->fb = fb_helper->fb;
2212 			modeset->x = offset->x;
2213 			modeset->y = offset->y;
2214 		}
2215 	}
2216 out:
2217 	kfree(crtcs);
2218 	kfree(modes);
2219 	kfree(offsets);
2220 	kfree(enabled);
2221 }
2222 
2223 /**
2224  * drm_fb_helper_initial_config - setup a sane initial connector configuration
2225  * @fb_helper: fb_helper device struct
2226  * @bpp_sel: bpp value to use for the framebuffer configuration
2227  *
2228  * Scans the CRTCs and connectors and tries to put together an initial setup.
2229  * At the moment, this is a cloned configuration across all heads with
2230  * a new framebuffer object as the backing store.
2231  *
2232  * Note that this also registers the fbdev and so allows userspace to call into
2233  * the driver through the fbdev interfaces.
2234  *
2235  * This function will call down into the ->fb_probe callback to let
2236  * the driver allocate and initialize the fbdev info structure and the drm
2237  * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
2238  * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
2239  * values for the fbdev info structure.
2240  *
2241  * HANG DEBUGGING:
2242  *
2243  * When you have fbcon support built-in or already loaded, this function will do
2244  * a full modeset to setup the fbdev console. Due to locking misdesign in the
2245  * VT/fbdev subsystem that entire modeset sequence has to be done while holding
2246  * console_lock. Until console_unlock is called no dmesg lines will be sent out
2247  * to consoles, not even serial console. This means when your driver crashes,
2248  * you will see absolutely nothing else but a system stuck in this function,
2249  * with no further output. Any kind of printk() you place within your own driver
2250  * or in the drm core modeset code will also never show up.
2251  *
2252  * Standard debug practice is to run the fbcon setup without taking the
2253  * console_lock as a hack, to be able to see backtraces and crashes on the
2254  * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
2255  * cmdline option.
2256  *
2257  * The other option is to just disable fbdev emulation since very likely the
2258  * first modeset from userspace will crash in the same way, and is even easier
2259  * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
2260  * kernel cmdline option.
2261  *
2262  * RETURNS:
2263  * Zero if everything went ok, nonzero otherwise.
2264  */
2265 int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
2266 {
2267 	struct drm_device *dev = fb_helper->dev;
2268 	struct fb_info *info;
2269 	int ret;
2270 #ifdef __DragonFly__
2271 	int kms_console = 1;
2272 #endif
2273 
2274 	if (!drm_fbdev_emulation)
2275 		return 0;
2276 
2277 	mutex_lock(&dev->mode_config.mutex);
2278 	drm_setup_crtcs(fb_helper,
2279 			dev->mode_config.max_width,
2280 			dev->mode_config.max_height);
2281 	ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
2282 	mutex_unlock(&dev->mode_config.mutex);
2283 	if (ret)
2284 		return ret;
2285 
2286 	info = fb_helper->fbdev;
2287 #ifdef __DragonFly__
2288 	TUNABLE_INT_FETCH("kern.kms_console", &kms_console);
2289 	if (kms_console) {
2290 		if (register_framebuffer(info) < 0)
2291 			return -EINVAL;
2292 
2293 		mutex_lock(&kernel_fb_helper_lock);
2294 		list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
2295 		mutex_unlock(&kernel_fb_helper_lock);
2296 	}
2297 #else
2298 	info->var.pixclock = 0;
2299 	ret = register_framebuffer(info);
2300 	if (ret < 0)
2301 		return ret;
2302 
2303 	dev_info(dev->dev, "fb%d: %s frame buffer device\n",
2304 		 info->node, info->fix.id);
2305 
2306 	mutex_lock(&kernel_fb_helper_lock);
2307 	if (list_empty(&kernel_fb_helper_list))
2308 		register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
2309 
2310 	list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
2311 	mutex_unlock(&kernel_fb_helper_lock);
2312 #endif
2313 
2314 	return 0;
2315 }
2316 EXPORT_SYMBOL(drm_fb_helper_initial_config);
2317 
2318 /**
2319  * drm_fb_helper_hotplug_event - respond to a hotplug notification by
2320  *                               probing all the outputs attached to the fb
2321  * @fb_helper: the drm_fb_helper
2322  *
2323  * Scan the connectors attached to the fb_helper and try to put together a
2324  * setup after notification of a change in output configuration.
2325  *
2326  * Called at runtime, takes the mode config locks to be able to check/change the
2327  * modeset configuration. Must be run from process context (which usually means
2328  * either the output polling work or a work item launched from the driver's
2329  * hotplug interrupt).
2330  *
2331  * Note that drivers may call this even before calling
2332  * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
2333  * for a race-free fbcon setup and will make sure that the fbdev emulation will
2334  * not miss any hotplug events.
2335  *
2336  * RETURNS:
2337  * 0 on success and a non-zero error code otherwise.
2338  */
2339 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
2340 {
2341 	struct drm_device *dev = fb_helper->dev;
2342 
2343 	if (!drm_fbdev_emulation)
2344 		return 0;
2345 
2346 	mutex_lock(&dev->mode_config.mutex);
2347 	if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
2348 		fb_helper->delayed_hotplug = true;
2349 		mutex_unlock(&dev->mode_config.mutex);
2350 		return 0;
2351 	}
2352 	DRM_DEBUG_KMS("\n");
2353 
2354 	drm_setup_crtcs(fb_helper, fb_helper->fb->width, fb_helper->fb->height);
2355 
2356 	mutex_unlock(&dev->mode_config.mutex);
2357 
2358 	drm_fb_helper_set_par(fb_helper->fbdev);
2359 
2360 	return 0;
2361 }
2362 EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
2363 
2364 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
2365  * but the module doesn't depend on any fb console symbols.  At least
2366  * attempt to load fbcon to avoid leaving the system without a usable console.
2367  */
2368 int __init drm_fb_helper_modinit(void)
2369 {
2370 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
2371 	const char *name = "fbcon";
2372 	struct module *fbcon;
2373 
2374 	mutex_lock(&module_mutex);
2375 	fbcon = find_module(name);
2376 	mutex_unlock(&module_mutex);
2377 
2378 	if (!fbcon)
2379 		request_module_nowait(name);
2380 #endif
2381 	return 0;
2382 }
2383 EXPORT_SYMBOL(drm_fb_helper_modinit);
2384