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