xref: /dflybsd-src/sys/dev/drm/drm_fb_helper.c (revision 9eb960773498463a0345970327518dc99f07e96b)
15718399fSFrançois Tigeot /*
25718399fSFrançois Tigeot  * Copyright (c) 2006-2009 Red Hat Inc.
35718399fSFrançois Tigeot  * Copyright (c) 2006-2008 Intel Corporation
45718399fSFrançois Tigeot  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
55718399fSFrançois Tigeot  *
65718399fSFrançois Tigeot  * DRM framebuffer helper functions
75718399fSFrançois Tigeot  *
85718399fSFrançois Tigeot  * Permission to use, copy, modify, distribute, and sell this software and its
95718399fSFrançois Tigeot  * documentation for any purpose is hereby granted without fee, provided that
105718399fSFrançois Tigeot  * the above copyright notice appear in all copies and that both that copyright
115718399fSFrançois Tigeot  * notice and this permission notice appear in supporting documentation, and
125718399fSFrançois Tigeot  * that the name of the copyright holders not be used in advertising or
135718399fSFrançois Tigeot  * publicity pertaining to distribution of the software without specific,
145718399fSFrançois Tigeot  * written prior permission.  The copyright holders make no representations
155718399fSFrançois Tigeot  * about the suitability of this software for any purpose.  It is provided "as
165718399fSFrançois Tigeot  * is" without express or implied warranty.
175718399fSFrançois Tigeot  *
185718399fSFrançois Tigeot  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
195718399fSFrançois Tigeot  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
205718399fSFrançois Tigeot  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
215718399fSFrançois Tigeot  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
225718399fSFrançois Tigeot  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
235718399fSFrançois Tigeot  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
245718399fSFrançois Tigeot  * OF THIS SOFTWARE.
255718399fSFrançois Tigeot  *
265718399fSFrançois Tigeot  * Authors:
275718399fSFrançois Tigeot  *      Dave Airlie <airlied@linux.ie>
285718399fSFrançois Tigeot  *      Jesse Barnes <jesse.barnes@intel.com>
295718399fSFrançois Tigeot  */
304be47400SFrançois Tigeot #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
315718399fSFrançois Tigeot 
321dedbd3bSFrançois Tigeot #include <linux/console.h>
33ba55f2f5SFrançois Tigeot #include <linux/kernel.h>
341e12ee3bSFrançois Tigeot #include <linux/sysrq.h>
351e12ee3bSFrançois Tigeot #include <linux/slab.h>
36ba55f2f5SFrançois Tigeot #include <linux/module.h>
3718e26a6dSFrançois Tigeot #include <drm/drmP.h>
3818e26a6dSFrançois Tigeot #include <drm/drm_crtc.h>
3918e26a6dSFrançois Tigeot #include <drm/drm_fb_helper.h>
4018e26a6dSFrançois Tigeot #include <drm/drm_crtc_helper.h>
41352ff8bdSFrançois Tigeot #include <drm/drm_atomic.h>
42352ff8bdSFrançois Tigeot #include <drm/drm_atomic_helper.h>
43352ff8bdSFrançois Tigeot 
441dedbd3bSFrançois Tigeot #include "drm_crtc_helper_internal.h"
451dedbd3bSFrançois Tigeot 
46352ff8bdSFrançois Tigeot static bool drm_fbdev_emulation = true;
47352ff8bdSFrançois Tigeot module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600);
48352ff8bdSFrançois Tigeot MODULE_PARM_DESC(fbdev_emulation,
49352ff8bdSFrançois Tigeot 		 "Enable legacy fbdev emulation [default=true]");
505718399fSFrançois Tigeot 
51a85cb24fSFrançois Tigeot static int drm_fbdev_overalloc = CONFIG_DRM_FBDEV_OVERALLOC;
52a85cb24fSFrançois Tigeot module_param(drm_fbdev_overalloc, int, 0444);
53a85cb24fSFrançois Tigeot MODULE_PARM_DESC(drm_fbdev_overalloc,
54a85cb24fSFrançois Tigeot 		 "Overallocation of the fbdev buffer (%) [default="
55a85cb24fSFrançois Tigeot 		 __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC) "]");
56a85cb24fSFrançois Tigeot 
5728828b89SFrançois Tigeot static LINUX_LIST_HEAD(kernel_fb_helper_list);
584be47400SFrançois Tigeot static DEFINE_MUTEX(kernel_fb_helper_lock);
595718399fSFrançois Tigeot 
609edbd4a0SFrançois Tigeot /**
619edbd4a0SFrançois Tigeot  * DOC: fbdev helpers
629edbd4a0SFrançois Tigeot  *
639edbd4a0SFrançois Tigeot  * The fb helper functions are useful to provide an fbdev on top of a drm kernel
64ba55f2f5SFrançois Tigeot  * mode setting driver. They can be used mostly independently from the crtc
659edbd4a0SFrançois Tigeot  * helper functions used by many drivers to implement the kernel mode setting
669edbd4a0SFrançois Tigeot  * interfaces.
679edbd4a0SFrançois Tigeot  *
68c6f73aabSFrançois Tigeot  * Initialization is done as a four-step process with drm_fb_helper_prepare(),
69c6f73aabSFrançois Tigeot  * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and
70c6f73aabSFrançois Tigeot  * drm_fb_helper_initial_config(). Drivers with fancier requirements than the
71c6f73aabSFrançois Tigeot  * default behaviour can override the third step with their own code.
72a85cb24fSFrançois Tigeot  * Teardown is done with drm_fb_helper_fini() after the fbdev device is
73a85cb24fSFrançois Tigeot  * unregisters using drm_fb_helper_unregister_fbi().
749edbd4a0SFrançois Tigeot  *
759edbd4a0SFrançois Tigeot  * At runtime drivers should restore the fbdev console by calling
76a85cb24fSFrançois Tigeot  * drm_fb_helper_restore_fbdev_mode_unlocked() from their &drm_driver.lastclose
77a85cb24fSFrançois Tigeot  * callback.  They should also notify the fb helper code from updates to the
78a85cb24fSFrançois Tigeot  * output configuration by calling drm_fb_helper_hotplug_event(). For easier
799edbd4a0SFrançois Tigeot  * integration with the output polling code in drm_crtc_helper.c the modeset
80a85cb24fSFrançois Tigeot  * code provides a &drm_mode_config_funcs.output_poll_changed callback.
819edbd4a0SFrançois Tigeot  *
829edbd4a0SFrançois Tigeot  * All other functions exported by the fb helper library can be used to
839edbd4a0SFrançois Tigeot  * implement the fbdev driver interface by the driver.
84c6f73aabSFrançois Tigeot  *
85c6f73aabSFrançois Tigeot  * It is possible, though perhaps somewhat tricky, to implement race-free
86c6f73aabSFrançois Tigeot  * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
87c6f73aabSFrançois Tigeot  * helper must be called first to initialize the minimum required to make
88c6f73aabSFrançois Tigeot  * hotplug detection work. Drivers also need to make sure to properly set up
89a85cb24fSFrançois Tigeot  * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init()
90c6f73aabSFrançois Tigeot  * it is safe to enable interrupts and start processing hotplug events. At the
91c6f73aabSFrançois Tigeot  * same time, drivers should initialize all modeset objects such as CRTCs,
92c6f73aabSFrançois Tigeot  * encoders and connectors. To finish up the fbdev helper initialization, the
93c6f73aabSFrançois Tigeot  * drm_fb_helper_init() function is called. To probe for all attached displays
94c6f73aabSFrançois Tigeot  * and set up an initial configuration using the detected hardware, drivers
95c6f73aabSFrançois Tigeot  * should call drm_fb_helper_single_add_all_connectors() followed by
96c6f73aabSFrançois Tigeot  * drm_fb_helper_initial_config().
978621f407SFrançois Tigeot  *
98a85cb24fSFrançois Tigeot  * If &drm_framebuffer_funcs.dirty is set, the
998621f407SFrançois Tigeot  * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will
100a85cb24fSFrançois Tigeot  * accumulate changes and schedule &drm_fb_helper.dirty_work to run right
1018621f407SFrançois Tigeot  * away. This worker then calls the dirty() function ensuring that it will
1028621f407SFrançois Tigeot  * always run in process context since the fb_*() function could be running in
1038621f407SFrançois Tigeot  * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io
1048621f407SFrançois Tigeot  * callback it will also schedule dirty_work with the damage collected from the
1058621f407SFrançois Tigeot  * mmap page writes.
1069edbd4a0SFrançois Tigeot  */
1079edbd4a0SFrançois Tigeot 
1084be47400SFrançois Tigeot #define drm_fb_helper_for_each_connector(fbh, i__) \
1093f2dd94aSFrançois Tigeot 	for (({ lockdep_assert_held(&(fbh)->lock); }), \
1104be47400SFrançois Tigeot 	     i__ = 0; i__ < (fbh)->connector_count; i__++)
1114be47400SFrançois Tigeot 
__drm_fb_helper_add_one_connector(struct drm_fb_helper * fb_helper,struct drm_connector * connector)1123f2dd94aSFrançois Tigeot static int __drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
113a85cb24fSFrançois Tigeot 					     struct drm_connector *connector)
114a85cb24fSFrançois Tigeot {
115a85cb24fSFrançois Tigeot 	struct drm_fb_helper_connector *fb_conn;
116a85cb24fSFrançois Tigeot 	struct drm_fb_helper_connector **temp;
117a85cb24fSFrançois Tigeot 	unsigned int count;
118a85cb24fSFrançois Tigeot 
119a85cb24fSFrançois Tigeot 	if (!drm_fbdev_emulation)
120a85cb24fSFrançois Tigeot 		return 0;
121a85cb24fSFrançois Tigeot 
1223f2dd94aSFrançois Tigeot 	lockdep_assert_held(&fb_helper->lock);
123a85cb24fSFrançois Tigeot 
124a85cb24fSFrançois Tigeot 	count = fb_helper->connector_count + 1;
125a85cb24fSFrançois Tigeot 
126a85cb24fSFrançois Tigeot 	if (count > fb_helper->connector_info_alloc_count) {
127a85cb24fSFrançois Tigeot 		size_t size = count * sizeof(fb_conn);
128a85cb24fSFrançois Tigeot 
129a85cb24fSFrançois Tigeot 		temp = krealloc(fb_helper->connector_info, size, M_DRM,
130a85cb24fSFrançois Tigeot 				GFP_KERNEL);
131a85cb24fSFrançois Tigeot 		if (!temp)
132a85cb24fSFrançois Tigeot 			return -ENOMEM;
133a85cb24fSFrançois Tigeot 
134a85cb24fSFrançois Tigeot 		fb_helper->connector_info_alloc_count = count;
135a85cb24fSFrançois Tigeot 		fb_helper->connector_info = temp;
136a85cb24fSFrançois Tigeot 	}
137a85cb24fSFrançois Tigeot 
138a85cb24fSFrançois Tigeot 	fb_conn = kzalloc(sizeof(*fb_conn), GFP_KERNEL);
139a85cb24fSFrançois Tigeot 	if (!fb_conn)
140a85cb24fSFrançois Tigeot 		return -ENOMEM;
141a85cb24fSFrançois Tigeot 
142a85cb24fSFrançois Tigeot 	drm_connector_get(connector);
143a85cb24fSFrançois Tigeot 	fb_conn->connector = connector;
144a85cb24fSFrançois Tigeot 	fb_helper->connector_info[fb_helper->connector_count++] = fb_conn;
1453f2dd94aSFrançois Tigeot 
146a85cb24fSFrançois Tigeot 	return 0;
147a85cb24fSFrançois Tigeot }
1483f2dd94aSFrançois Tigeot 
drm_fb_helper_add_one_connector(struct drm_fb_helper * fb_helper,struct drm_connector * connector)1493f2dd94aSFrançois Tigeot int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
1503f2dd94aSFrançois Tigeot 				    struct drm_connector *connector)
1513f2dd94aSFrançois Tigeot {
1523f2dd94aSFrançois Tigeot 	int err;
1533f2dd94aSFrançois Tigeot 
1543f2dd94aSFrançois Tigeot 	mutex_lock(&fb_helper->lock);
1553f2dd94aSFrançois Tigeot 	err = __drm_fb_helper_add_one_connector(fb_helper, connector);
1563f2dd94aSFrançois Tigeot 	mutex_unlock(&fb_helper->lock);
1573f2dd94aSFrançois Tigeot 
1583f2dd94aSFrançois Tigeot 	return err;
1593f2dd94aSFrançois Tigeot }
160a85cb24fSFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
161a85cb24fSFrançois Tigeot 
1629edbd4a0SFrançois Tigeot /**
1639edbd4a0SFrançois Tigeot  * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
1649edbd4a0SFrançois Tigeot  * 					       emulation helper
1659edbd4a0SFrançois Tigeot  * @fb_helper: fbdev initialized with drm_fb_helper_init
1669edbd4a0SFrançois Tigeot  *
1679edbd4a0SFrançois Tigeot  * This functions adds all the available connectors for use with the given
1689edbd4a0SFrançois Tigeot  * fb_helper. This is a separate step to allow drivers to freely assign
1699edbd4a0SFrançois Tigeot  * connectors to the fbdev, e.g. if some are reserved for special purposes or
1709edbd4a0SFrançois Tigeot  * not adequate to be used for the fbcon.
1719edbd4a0SFrançois Tigeot  *
172a05eeebfSFrançois Tigeot  * This function is protected against concurrent connector hotadds/removals
173a05eeebfSFrançois Tigeot  * using drm_fb_helper_add_one_connector() and
174a05eeebfSFrançois Tigeot  * drm_fb_helper_remove_one_connector().
1759edbd4a0SFrançois Tigeot  */
drm_fb_helper_single_add_all_connectors(struct drm_fb_helper * fb_helper)1765718399fSFrançois Tigeot int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
1775718399fSFrançois Tigeot {
1785718399fSFrançois Tigeot 	struct drm_device *dev = fb_helper->dev;
1795718399fSFrançois Tigeot 	struct drm_connector *connector;
180a85cb24fSFrançois Tigeot 	struct drm_connector_list_iter conn_iter;
181a85cb24fSFrançois Tigeot 	int i, ret = 0;
1825718399fSFrançois Tigeot 
183352ff8bdSFrançois Tigeot 	if (!drm_fbdev_emulation)
184352ff8bdSFrançois Tigeot 		return 0;
185352ff8bdSFrançois Tigeot 
1863f2dd94aSFrançois Tigeot 	mutex_lock(&fb_helper->lock);
187a85cb24fSFrançois Tigeot 	drm_connector_list_iter_begin(dev, &conn_iter);
188a85cb24fSFrançois Tigeot 	drm_for_each_connector_iter(connector, &conn_iter) {
1893f2dd94aSFrançois Tigeot 		ret = __drm_fb_helper_add_one_connector(fb_helper, connector);
190c0e85e96SFrançois Tigeot 		if (ret)
1919edbd4a0SFrançois Tigeot 			goto fail;
1925718399fSFrançois Tigeot 	}
193a85cb24fSFrançois Tigeot 	goto out;
194a85cb24fSFrançois Tigeot 
1959edbd4a0SFrançois Tigeot fail:
1964be47400SFrançois Tigeot 	drm_fb_helper_for_each_connector(fb_helper, i) {
1974be47400SFrançois Tigeot 		struct drm_fb_helper_connector *fb_helper_connector =
1984be47400SFrançois Tigeot 			fb_helper->connector_info[i];
1994be47400SFrançois Tigeot 
200a85cb24fSFrançois Tigeot 		drm_connector_put(fb_helper_connector->connector);
2014be47400SFrançois Tigeot 
2024be47400SFrançois Tigeot 		kfree(fb_helper_connector);
2039edbd4a0SFrançois Tigeot 		fb_helper->connector_info[i] = NULL;
2045718399fSFrançois Tigeot 	}
2059edbd4a0SFrançois Tigeot 	fb_helper->connector_count = 0;
206a85cb24fSFrançois Tigeot out:
207a85cb24fSFrançois Tigeot 	drm_connector_list_iter_end(&conn_iter);
2083f2dd94aSFrançois Tigeot 	mutex_unlock(&fb_helper->lock);
209a05eeebfSFrançois Tigeot 
210c0e85e96SFrançois Tigeot 	return ret;
2119edbd4a0SFrançois Tigeot }
2129edbd4a0SFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
2135718399fSFrançois Tigeot 
__drm_fb_helper_remove_one_connector(struct drm_fb_helper * fb_helper,struct drm_connector * connector)2143f2dd94aSFrançois Tigeot static int __drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
21524edb884SFrançois Tigeot 						struct drm_connector *connector)
21624edb884SFrançois Tigeot {
21724edb884SFrançois Tigeot 	struct drm_fb_helper_connector *fb_helper_connector;
21824edb884SFrançois Tigeot 	int i, j;
21924edb884SFrançois Tigeot 
220352ff8bdSFrançois Tigeot 	if (!drm_fbdev_emulation)
221352ff8bdSFrançois Tigeot 		return 0;
222352ff8bdSFrançois Tigeot 
2233f2dd94aSFrançois Tigeot 	lockdep_assert_held(&fb_helper->lock);
22424edb884SFrançois Tigeot 
2253f2dd94aSFrançois Tigeot 	drm_fb_helper_for_each_connector(fb_helper, i) {
22624edb884SFrançois Tigeot 		if (fb_helper->connector_info[i]->connector == connector)
22724edb884SFrançois Tigeot 			break;
22824edb884SFrançois Tigeot 	}
22924edb884SFrançois Tigeot 
23024edb884SFrançois Tigeot 	if (i == fb_helper->connector_count)
23124edb884SFrançois Tigeot 		return -EINVAL;
23224edb884SFrançois Tigeot 	fb_helper_connector = fb_helper->connector_info[i];
233a85cb24fSFrançois Tigeot 	drm_connector_put(fb_helper_connector->connector);
23424edb884SFrançois Tigeot 
235a85cb24fSFrançois Tigeot 	for (j = i + 1; j < fb_helper->connector_count; j++)
23624edb884SFrançois Tigeot 		fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
237a85cb24fSFrançois Tigeot 
23824edb884SFrançois Tigeot 	fb_helper->connector_count--;
23924edb884SFrançois Tigeot 	kfree(fb_helper_connector);
240a05eeebfSFrançois Tigeot 
24124edb884SFrançois Tigeot 	return 0;
24224edb884SFrançois Tigeot }
2433f2dd94aSFrançois Tigeot 
drm_fb_helper_remove_one_connector(struct drm_fb_helper * fb_helper,struct drm_connector * connector)2443f2dd94aSFrançois Tigeot int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
2453f2dd94aSFrançois Tigeot 				       struct drm_connector *connector)
2463f2dd94aSFrançois Tigeot {
2473f2dd94aSFrançois Tigeot 	int err;
2483f2dd94aSFrançois Tigeot 
2493f2dd94aSFrançois Tigeot 	mutex_lock(&fb_helper->lock);
2503f2dd94aSFrançois Tigeot 	err = __drm_fb_helper_remove_one_connector(fb_helper, connector);
2513f2dd94aSFrançois Tigeot 	mutex_unlock(&fb_helper->lock);
2523f2dd94aSFrançois Tigeot 
2533f2dd94aSFrançois Tigeot 	return err;
2543f2dd94aSFrançois Tigeot }
25524edb884SFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
25624edb884SFrançois Tigeot 
drm_fb_helper_restore_lut_atomic(struct drm_crtc * crtc)2575718399fSFrançois Tigeot static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
2585718399fSFrançois Tigeot {
2595718399fSFrançois Tigeot 	uint16_t *r_base, *g_base, *b_base;
2605718399fSFrançois Tigeot 
2619edbd4a0SFrançois Tigeot 	if (crtc->funcs->gamma_set == NULL)
2629edbd4a0SFrançois Tigeot 		return;
2639edbd4a0SFrançois Tigeot 
2645718399fSFrançois Tigeot 	r_base = crtc->gamma_store;
2655718399fSFrançois Tigeot 	g_base = r_base + crtc->gamma_size;
2665718399fSFrançois Tigeot 	b_base = g_base + crtc->gamma_size;
2675718399fSFrançois Tigeot 
268a85cb24fSFrançois Tigeot 	crtc->funcs->gamma_set(crtc, r_base, g_base, b_base,
269a85cb24fSFrançois Tigeot 			       crtc->gamma_size, NULL);
2705718399fSFrançois Tigeot }
2715718399fSFrançois Tigeot 
2729edbd4a0SFrançois Tigeot /**
273a85cb24fSFrançois Tigeot  * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter
2749edbd4a0SFrançois Tigeot  * @info: fbdev registered by the helper
2759edbd4a0SFrançois Tigeot  */
drm_fb_helper_debug_enter(struct fb_info * info)2765718399fSFrançois Tigeot int drm_fb_helper_debug_enter(struct fb_info *info)
2775718399fSFrançois Tigeot {
2785718399fSFrançois Tigeot 	struct drm_fb_helper *helper = info->par;
279477eb7f9SFrançois Tigeot 	const struct drm_crtc_helper_funcs *funcs;
2805718399fSFrançois Tigeot 	int i;
2815718399fSFrançois Tigeot 
2825718399fSFrançois Tigeot 	list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
2835718399fSFrançois Tigeot 		for (i = 0; i < helper->crtc_count; i++) {
2845718399fSFrançois Tigeot 			struct drm_mode_set *mode_set =
2855718399fSFrançois Tigeot 				&helper->crtc_info[i].mode_set;
2865718399fSFrançois Tigeot 
2875718399fSFrançois Tigeot 			if (!mode_set->crtc->enabled)
2885718399fSFrançois Tigeot 				continue;
2895718399fSFrançois Tigeot 
2905718399fSFrançois Tigeot 			funcs =	mode_set->crtc->helper_private;
2914be47400SFrançois Tigeot 			if (funcs->mode_set_base_atomic == NULL)
2924be47400SFrançois Tigeot 				continue;
2934be47400SFrançois Tigeot 
294a85cb24fSFrançois Tigeot 			if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev))
295a85cb24fSFrançois Tigeot 				continue;
296a85cb24fSFrançois Tigeot 
2975718399fSFrançois Tigeot 			funcs->mode_set_base_atomic(mode_set->crtc,
2985718399fSFrançois Tigeot 						    mode_set->fb,
2995718399fSFrançois Tigeot 						    mode_set->x,
3005718399fSFrançois Tigeot 						    mode_set->y,
3015718399fSFrançois Tigeot 						    ENTER_ATOMIC_MODE_SET);
3025718399fSFrançois Tigeot 		}
3035718399fSFrançois Tigeot 	}
3045718399fSFrançois Tigeot 
3055718399fSFrançois Tigeot 	return 0;
3065718399fSFrançois Tigeot }
3079edbd4a0SFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_debug_enter);
3085718399fSFrançois Tigeot 
3099edbd4a0SFrançois Tigeot /**
310a85cb24fSFrançois Tigeot  * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave
3119edbd4a0SFrançois Tigeot  * @info: fbdev registered by the helper
3129edbd4a0SFrançois Tigeot  */
drm_fb_helper_debug_leave(struct fb_info * info)3135718399fSFrançois Tigeot int drm_fb_helper_debug_leave(struct fb_info *info)
3145718399fSFrançois Tigeot {
3155718399fSFrançois Tigeot 	struct drm_fb_helper *helper = info->par;
3165718399fSFrançois Tigeot 	struct drm_crtc *crtc;
317477eb7f9SFrançois Tigeot 	const struct drm_crtc_helper_funcs *funcs;
3185718399fSFrançois Tigeot 	struct drm_framebuffer *fb;
3195718399fSFrançois Tigeot 	int i;
3205718399fSFrançois Tigeot 
3215718399fSFrançois Tigeot 	for (i = 0; i < helper->crtc_count; i++) {
3225718399fSFrançois Tigeot 		struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
323a85cb24fSFrançois Tigeot 
3245718399fSFrançois Tigeot 		crtc = mode_set->crtc;
3253f2dd94aSFrançois Tigeot 		if (drm_drv_uses_atomic_modeset(crtc->dev))
3263f2dd94aSFrançois Tigeot 			continue;
3273f2dd94aSFrançois Tigeot 
3285718399fSFrançois Tigeot 		funcs = crtc->helper_private;
3293f2dd94aSFrançois Tigeot 		fb = crtc->primary->fb;
3305718399fSFrançois Tigeot 
3315718399fSFrançois Tigeot 		if (!crtc->enabled)
3325718399fSFrançois Tigeot 			continue;
3335718399fSFrançois Tigeot 
3345718399fSFrançois Tigeot 		if (!fb) {
3355718399fSFrançois Tigeot 			DRM_ERROR("no fb to restore??\n");
3365718399fSFrançois Tigeot 			continue;
3375718399fSFrançois Tigeot 		}
3385718399fSFrançois Tigeot 
3394be47400SFrançois Tigeot 		if (funcs->mode_set_base_atomic == NULL)
3404be47400SFrançois Tigeot 			continue;
3414be47400SFrançois Tigeot 
3425718399fSFrançois Tigeot 		drm_fb_helper_restore_lut_atomic(mode_set->crtc);
3435718399fSFrançois Tigeot 		funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
3445718399fSFrançois Tigeot 					    crtc->y, LEAVE_ATOMIC_MODE_SET);
3455718399fSFrançois Tigeot 	}
3465718399fSFrançois Tigeot 
3475718399fSFrançois Tigeot 	return 0;
3485718399fSFrançois Tigeot }
3499edbd4a0SFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_debug_leave);
3505718399fSFrançois Tigeot 
restore_fbdev_mode_atomic(struct drm_fb_helper * fb_helper,bool active)3513f2dd94aSFrançois Tigeot static int restore_fbdev_mode_atomic(struct drm_fb_helper *fb_helper, bool active)
352352ff8bdSFrançois Tigeot {
353352ff8bdSFrançois Tigeot 	struct drm_device *dev = fb_helper->dev;
354352ff8bdSFrançois Tigeot 	struct drm_plane *plane;
355352ff8bdSFrançois Tigeot 	struct drm_atomic_state *state;
356352ff8bdSFrançois Tigeot 	int i, ret;
357a85cb24fSFrançois Tigeot 	unsigned int plane_mask;
3583f2dd94aSFrançois Tigeot 	struct drm_modeset_acquire_ctx ctx;
3593f2dd94aSFrançois Tigeot 
3603f2dd94aSFrançois Tigeot 	drm_modeset_acquire_init(&ctx, 0);
361352ff8bdSFrançois Tigeot 
362352ff8bdSFrançois Tigeot 	state = drm_atomic_state_alloc(dev);
3633f2dd94aSFrançois Tigeot 	if (!state) {
3643f2dd94aSFrançois Tigeot 		ret = -ENOMEM;
3653f2dd94aSFrançois Tigeot 		goto out_ctx;
3663f2dd94aSFrançois Tigeot 	}
367352ff8bdSFrançois Tigeot 
3683f2dd94aSFrançois Tigeot 	state->acquire_ctx = &ctx;
369352ff8bdSFrançois Tigeot retry:
370352ff8bdSFrançois Tigeot 	plane_mask = 0;
371352ff8bdSFrançois Tigeot 	drm_for_each_plane(plane, dev) {
372352ff8bdSFrançois Tigeot 		struct drm_plane_state *plane_state;
373352ff8bdSFrançois Tigeot 
374352ff8bdSFrançois Tigeot 		plane_state = drm_atomic_get_plane_state(state, plane);
375352ff8bdSFrançois Tigeot 		if (IS_ERR(plane_state)) {
376352ff8bdSFrançois Tigeot 			ret = PTR_ERR(plane_state);
3773f2dd94aSFrançois Tigeot 			goto out_state;
378352ff8bdSFrançois Tigeot 		}
379352ff8bdSFrançois Tigeot 
3803f2dd94aSFrançois Tigeot 		plane_state->rotation = DRM_MODE_ROTATE_0;
381352ff8bdSFrançois Tigeot 
382352ff8bdSFrançois Tigeot 		plane->old_fb = plane->fb;
383352ff8bdSFrançois Tigeot 		plane_mask |= 1 << drm_plane_index(plane);
384352ff8bdSFrançois Tigeot 
385352ff8bdSFrançois Tigeot 		/* disable non-primary: */
386352ff8bdSFrançois Tigeot 		if (plane->type == DRM_PLANE_TYPE_PRIMARY)
387352ff8bdSFrançois Tigeot 			continue;
388352ff8bdSFrançois Tigeot 
389352ff8bdSFrançois Tigeot 		ret = __drm_atomic_helper_disable_plane(plane, plane_state);
390352ff8bdSFrançois Tigeot 		if (ret != 0)
3913f2dd94aSFrançois Tigeot 			goto out_state;
392352ff8bdSFrançois Tigeot 	}
393352ff8bdSFrançois Tigeot 
394352ff8bdSFrançois Tigeot 	for (i = 0; i < fb_helper->crtc_count; i++) {
395352ff8bdSFrançois Tigeot 		struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
396352ff8bdSFrançois Tigeot 
397352ff8bdSFrançois Tigeot 		ret = __drm_atomic_helper_set_config(mode_set, state);
398352ff8bdSFrançois Tigeot 		if (ret != 0)
3993f2dd94aSFrançois Tigeot 			goto out_state;
4003f2dd94aSFrançois Tigeot 
4013f2dd94aSFrançois Tigeot 		/*
4023f2dd94aSFrançois Tigeot 		 * __drm_atomic_helper_set_config() sets active when a
4033f2dd94aSFrançois Tigeot 		 * mode is set, unconditionally clear it if we force DPMS off
4043f2dd94aSFrançois Tigeot 		 */
4053f2dd94aSFrançois Tigeot 		if (!active) {
4063f2dd94aSFrançois Tigeot 			struct drm_crtc *crtc = mode_set->crtc;
4073f2dd94aSFrançois Tigeot 			struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
4083f2dd94aSFrançois Tigeot 
4093f2dd94aSFrançois Tigeot 			crtc_state->active = false;
4103f2dd94aSFrançois Tigeot 		}
411352ff8bdSFrançois Tigeot 	}
412352ff8bdSFrançois Tigeot 
413352ff8bdSFrançois Tigeot 	ret = drm_atomic_commit(state);
414352ff8bdSFrançois Tigeot 
4153f2dd94aSFrançois Tigeot out_state:
416352ff8bdSFrançois Tigeot 	drm_atomic_clean_old_fb(dev, plane_mask, ret);
417352ff8bdSFrançois Tigeot 
418352ff8bdSFrançois Tigeot 	if (ret == -EDEADLK)
419352ff8bdSFrançois Tigeot 		goto backoff;
420352ff8bdSFrançois Tigeot 
4214be47400SFrançois Tigeot 	drm_atomic_state_put(state);
4223f2dd94aSFrançois Tigeot out_ctx:
4233f2dd94aSFrançois Tigeot 	drm_modeset_drop_locks(&ctx);
4243f2dd94aSFrançois Tigeot 	drm_modeset_acquire_fini(&ctx);
4253f2dd94aSFrançois Tigeot 
426352ff8bdSFrançois Tigeot 	return ret;
427352ff8bdSFrançois Tigeot 
428352ff8bdSFrançois Tigeot backoff:
429352ff8bdSFrançois Tigeot 	drm_atomic_state_clear(state);
4303f2dd94aSFrançois Tigeot 	drm_modeset_backoff(&ctx);
431352ff8bdSFrançois Tigeot 
432352ff8bdSFrançois Tigeot 	goto retry;
433352ff8bdSFrançois Tigeot }
434352ff8bdSFrançois Tigeot 
restore_fbdev_mode_legacy(struct drm_fb_helper * fb_helper)435a85cb24fSFrançois Tigeot static int restore_fbdev_mode_legacy(struct drm_fb_helper *fb_helper)
4365718399fSFrançois Tigeot {
4379edbd4a0SFrançois Tigeot 	struct drm_device *dev = fb_helper->dev;
4389edbd4a0SFrançois Tigeot 	struct drm_plane *plane;
4393f2dd94aSFrançois Tigeot 	int i, ret = 0;
4409edbd4a0SFrançois Tigeot 
4413f2dd94aSFrançois Tigeot 	drm_modeset_lock_all(fb_helper->dev);
442a05eeebfSFrançois Tigeot 	drm_for_each_plane(plane, dev) {
443ba55f2f5SFrançois Tigeot 		if (plane->type != DRM_PLANE_TYPE_PRIMARY)
4449edbd4a0SFrançois Tigeot 			drm_plane_force_disable(plane);
4459edbd4a0SFrançois Tigeot 
4464be47400SFrançois Tigeot 		if (plane->rotation_property)
4471b13d190SFrançois Tigeot 			drm_mode_plane_set_obj_prop(plane,
4484be47400SFrançois Tigeot 						    plane->rotation_property,
4493f2dd94aSFrançois Tigeot 						    DRM_MODE_ROTATE_0);
4501b13d190SFrançois Tigeot 	}
4511b13d190SFrançois Tigeot 
4525718399fSFrançois Tigeot 	for (i = 0; i < fb_helper->crtc_count; i++) {
4535718399fSFrançois Tigeot 		struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
4549edbd4a0SFrançois Tigeot 		struct drm_crtc *crtc = mode_set->crtc;
4559edbd4a0SFrançois Tigeot 
4561e12ee3bSFrançois Tigeot 		if (crtc->funcs->cursor_set2) {
4571e12ee3bSFrançois Tigeot 			ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
4581e12ee3bSFrançois Tigeot 			if (ret)
4593f2dd94aSFrançois Tigeot 				goto out;
4601e12ee3bSFrançois Tigeot 		} else if (crtc->funcs->cursor_set) {
4619edbd4a0SFrançois Tigeot 			ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
4629edbd4a0SFrançois Tigeot 			if (ret)
4633f2dd94aSFrançois Tigeot 				goto out;
4649edbd4a0SFrançois Tigeot 		}
4659edbd4a0SFrançois Tigeot 
4669edbd4a0SFrançois Tigeot 		ret = drm_mode_set_config_internal(mode_set);
4675718399fSFrançois Tigeot 		if (ret)
4683f2dd94aSFrançois Tigeot 			goto out;
4695718399fSFrançois Tigeot 	}
4703f2dd94aSFrançois Tigeot out:
4713f2dd94aSFrançois Tigeot 	drm_modeset_unlock_all(fb_helper->dev);
4721e12ee3bSFrançois Tigeot 
4733f2dd94aSFrançois Tigeot 	return ret;
4745718399fSFrançois Tigeot }
475ba55f2f5SFrançois Tigeot 
restore_fbdev_mode(struct drm_fb_helper * fb_helper)476a85cb24fSFrançois Tigeot static int restore_fbdev_mode(struct drm_fb_helper *fb_helper)
477a85cb24fSFrançois Tigeot {
478a85cb24fSFrançois Tigeot 	struct drm_device *dev = fb_helper->dev;
479a85cb24fSFrançois Tigeot 
480a85cb24fSFrançois Tigeot 	if (drm_drv_uses_atomic_modeset(dev))
4813f2dd94aSFrançois Tigeot 		return restore_fbdev_mode_atomic(fb_helper, true);
482a85cb24fSFrançois Tigeot 	else
483a85cb24fSFrançois Tigeot 		return restore_fbdev_mode_legacy(fb_helper);
484a85cb24fSFrançois Tigeot }
485a85cb24fSFrançois Tigeot 
486ba55f2f5SFrançois Tigeot /**
487ba55f2f5SFrançois Tigeot  * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
488ba55f2f5SFrançois Tigeot  * @fb_helper: fbcon to restore
489ba55f2f5SFrançois Tigeot  *
490a85cb24fSFrançois Tigeot  * This should be called from driver's drm &drm_driver.lastclose callback
491ba55f2f5SFrançois Tigeot  * when implementing an fbcon on top of kms using this helper. This ensures that
492ba55f2f5SFrançois Tigeot  * the user isn't greeted with a black screen when e.g. X dies.
493352ff8bdSFrançois Tigeot  *
494352ff8bdSFrançois Tigeot  * RETURNS:
495352ff8bdSFrançois Tigeot  * Zero if everything went ok, negative error code otherwise.
496ba55f2f5SFrançois Tigeot  */
drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper * fb_helper)497352ff8bdSFrançois Tigeot int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
498ba55f2f5SFrançois Tigeot {
499352ff8bdSFrançois Tigeot 	bool do_delayed;
500352ff8bdSFrançois Tigeot 	int ret;
501352ff8bdSFrançois Tigeot 
502352ff8bdSFrançois Tigeot 	if (!drm_fbdev_emulation)
503352ff8bdSFrançois Tigeot 		return -ENODEV;
5042c9916cdSFrançois Tigeot 
5053f2dd94aSFrançois Tigeot 	if (READ_ONCE(fb_helper->deferred_setup))
5063f2dd94aSFrançois Tigeot 		return 0;
5073f2dd94aSFrançois Tigeot 
5083f2dd94aSFrançois Tigeot 	mutex_lock(&fb_helper->lock);
509ba55f2f5SFrançois Tigeot 	ret = restore_fbdev_mode(fb_helper);
5102c9916cdSFrançois Tigeot 
5112c9916cdSFrançois Tigeot 	do_delayed = fb_helper->delayed_hotplug;
5122c9916cdSFrançois Tigeot 	if (do_delayed)
5132c9916cdSFrançois Tigeot 		fb_helper->delayed_hotplug = false;
5143f2dd94aSFrançois Tigeot 	mutex_unlock(&fb_helper->lock);
5152c9916cdSFrançois Tigeot 
5162c9916cdSFrançois Tigeot 	if (do_delayed)
5172c9916cdSFrançois Tigeot 		drm_fb_helper_hotplug_event(fb_helper);
5183f2dd94aSFrançois Tigeot 
519ba55f2f5SFrançois Tigeot 	return ret;
520ba55f2f5SFrançois Tigeot }
521ba55f2f5SFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
5225718399fSFrançois Tigeot 
drm_fb_helper_is_bound(struct drm_fb_helper * fb_helper)523a05eeebfSFrançois Tigeot static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
524a05eeebfSFrançois Tigeot {
525a05eeebfSFrançois Tigeot 	struct drm_device *dev = fb_helper->dev;
526a05eeebfSFrançois Tigeot 	struct drm_crtc *crtc;
527a05eeebfSFrançois Tigeot 	int bound = 0, crtcs_bound = 0;
528a05eeebfSFrançois Tigeot 
529a85cb24fSFrançois Tigeot 	/*
530a85cb24fSFrançois Tigeot 	 * Sometimes user space wants everything disabled, so don't steal the
531a85cb24fSFrançois Tigeot 	 * display if there's a master.
532a85cb24fSFrançois Tigeot 	 */
5331dedbd3bSFrançois Tigeot 	if (READ_ONCE(dev->master))
534a05eeebfSFrançois Tigeot 		return false;
535a05eeebfSFrançois Tigeot 
536a05eeebfSFrançois Tigeot 	drm_for_each_crtc(crtc, dev) {
5373f2dd94aSFrançois Tigeot 		drm_modeset_lock(&crtc->mutex, NULL);
538a05eeebfSFrançois Tigeot 		if (crtc->primary->fb)
539a05eeebfSFrançois Tigeot 			crtcs_bound++;
540a05eeebfSFrançois Tigeot 		if (crtc->primary->fb == fb_helper->fb)
541a05eeebfSFrançois Tigeot 			bound++;
5423f2dd94aSFrançois Tigeot 		drm_modeset_unlock(&crtc->mutex);
543a05eeebfSFrançois Tigeot 	}
544a05eeebfSFrançois Tigeot 
545a05eeebfSFrançois Tigeot 	if (bound < crtcs_bound)
546a05eeebfSFrançois Tigeot 		return false;
547a05eeebfSFrançois Tigeot 
548a05eeebfSFrançois Tigeot 	return true;
549a05eeebfSFrançois Tigeot }
550a05eeebfSFrançois Tigeot 
551a05eeebfSFrançois Tigeot #ifdef CONFIG_MAGIC_SYSRQ
5529edbd4a0SFrançois Tigeot /*
5539edbd4a0SFrançois Tigeot  * restore fbcon display for all kms driver's using this helper, used for sysrq
5549edbd4a0SFrançois Tigeot  * and panic handling.
5559edbd4a0SFrançois Tigeot  */
drm_fb_helper_force_kernel_mode(void)5569edbd4a0SFrançois Tigeot static bool drm_fb_helper_force_kernel_mode(void)
5575718399fSFrançois Tigeot {
5585718399fSFrançois Tigeot 	bool ret, error = false;
5595718399fSFrançois Tigeot 	struct drm_fb_helper *helper;
5605718399fSFrançois Tigeot 
5615718399fSFrançois Tigeot 	if (list_empty(&kernel_fb_helper_list))
5625718399fSFrançois Tigeot 		return false;
5635718399fSFrançois Tigeot 
5645718399fSFrançois Tigeot 	list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
565ba55f2f5SFrançois Tigeot 		struct drm_device *dev = helper->dev;
566ba55f2f5SFrançois Tigeot 
567ba55f2f5SFrançois Tigeot 		if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
5685718399fSFrançois Tigeot 			continue;
5695718399fSFrançois Tigeot 
5703f2dd94aSFrançois Tigeot 		mutex_lock(&helper->lock);
571a05eeebfSFrançois Tigeot 		ret = restore_fbdev_mode(helper);
5725718399fSFrançois Tigeot 		if (ret)
5735718399fSFrançois Tigeot 			error = true;
5743f2dd94aSFrançois Tigeot 		mutex_unlock(&helper->lock);
5755718399fSFrançois Tigeot 	}
5765718399fSFrançois Tigeot 	return error;
5775718399fSFrançois Tigeot }
5785718399fSFrançois Tigeot 
579ba55f2f5SFrançois Tigeot #if 0
5809edbd4a0SFrançois Tigeot static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
5815718399fSFrançois Tigeot {
5825718399fSFrançois Tigeot 	bool ret;
583a85cb24fSFrançois Tigeot 
5845718399fSFrançois Tigeot 	ret = drm_fb_helper_force_kernel_mode();
5855718399fSFrançois Tigeot 	if (ret == true)
5865718399fSFrançois Tigeot 		DRM_ERROR("Failed to restore crtc configuration\n");
5875718399fSFrançois Tigeot }
5885718399fSFrançois Tigeot static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
5895718399fSFrançois Tigeot 
5905718399fSFrançois Tigeot static void drm_fb_helper_sysrq(int dummy1)
5915718399fSFrançois Tigeot {
5925718399fSFrançois Tigeot 	schedule_work(&drm_fb_helper_restore_work);
5935718399fSFrançois Tigeot }
5945718399fSFrançois Tigeot 
5955718399fSFrançois Tigeot static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
5965718399fSFrançois Tigeot 	.handler = drm_fb_helper_sysrq,
5975718399fSFrançois Tigeot 	.help_msg = "force-fb(V)",
5985718399fSFrançois Tigeot 	.action_msg = "Restore framebuffer console",
5995718399fSFrançois Tigeot };
6005718399fSFrançois Tigeot #else
6015718399fSFrançois Tigeot static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
6025718399fSFrançois Tigeot #endif
603ba409a88SImre Vadász #endif
6045718399fSFrançois Tigeot 
dpms_legacy(struct drm_fb_helper * fb_helper,int dpms_mode)6053f2dd94aSFrançois Tigeot static void dpms_legacy(struct drm_fb_helper *fb_helper, int dpms_mode)
6065718399fSFrançois Tigeot {
6075718399fSFrançois Tigeot 	struct drm_device *dev = fb_helper->dev;
6085718399fSFrançois Tigeot 	struct drm_crtc *crtc;
6095718399fSFrançois Tigeot 	struct drm_connector *connector;
6105718399fSFrançois Tigeot 	int i, j;
6115718399fSFrançois Tigeot 
6129edbd4a0SFrançois Tigeot 	drm_modeset_lock_all(dev);
6135718399fSFrançois Tigeot 	for (i = 0; i < fb_helper->crtc_count; i++) {
6145718399fSFrançois Tigeot 		crtc = fb_helper->crtc_info[i].mode_set.crtc;
6155718399fSFrançois Tigeot 
6165718399fSFrançois Tigeot 		if (!crtc->enabled)
6175718399fSFrançois Tigeot 			continue;
6185718399fSFrançois Tigeot 
6199edbd4a0SFrançois Tigeot 		/* Walk the connectors & encoders on this fb turning them on/off */
6204be47400SFrançois Tigeot 		drm_fb_helper_for_each_connector(fb_helper, j) {
6215718399fSFrançois Tigeot 			connector = fb_helper->connector_info[j]->connector;
6229edbd4a0SFrançois Tigeot 			connector->funcs->dpms(connector, dpms_mode);
6239edbd4a0SFrançois Tigeot 			drm_object_property_set_value(&connector->base,
6249edbd4a0SFrançois Tigeot 				dev->mode_config.dpms_property, dpms_mode);
6255718399fSFrançois Tigeot 		}
6269edbd4a0SFrançois Tigeot 	}
6279edbd4a0SFrançois Tigeot 	drm_modeset_unlock_all(dev);
6289edbd4a0SFrançois Tigeot }
6295718399fSFrançois Tigeot 
drm_fb_helper_dpms(struct fb_info * info,int dpms_mode)6303f2dd94aSFrançois Tigeot static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
6313f2dd94aSFrançois Tigeot {
6323f2dd94aSFrançois Tigeot 	struct drm_fb_helper *fb_helper = info->par;
6333f2dd94aSFrançois Tigeot 
6343f2dd94aSFrançois Tigeot 	/*
6353f2dd94aSFrançois Tigeot 	 * For each CRTC in this fb, turn the connectors on/off.
6363f2dd94aSFrançois Tigeot 	 */
6373f2dd94aSFrançois Tigeot 	mutex_lock(&fb_helper->lock);
6383f2dd94aSFrançois Tigeot 	if (!drm_fb_helper_is_bound(fb_helper)) {
6393f2dd94aSFrançois Tigeot 		mutex_unlock(&fb_helper->lock);
6403f2dd94aSFrançois Tigeot 		return;
6413f2dd94aSFrançois Tigeot 	}
6423f2dd94aSFrançois Tigeot 
6433f2dd94aSFrançois Tigeot 	if (drm_drv_uses_atomic_modeset(fb_helper->dev))
6443f2dd94aSFrançois Tigeot 		restore_fbdev_mode_atomic(fb_helper, dpms_mode == DRM_MODE_DPMS_ON);
6453f2dd94aSFrançois Tigeot 	else
6463f2dd94aSFrançois Tigeot 		dpms_legacy(fb_helper, dpms_mode);
6473f2dd94aSFrançois Tigeot 	mutex_unlock(&fb_helper->lock);
6483f2dd94aSFrançois Tigeot }
6493f2dd94aSFrançois Tigeot 
6509edbd4a0SFrançois Tigeot /**
651a85cb24fSFrançois Tigeot  * drm_fb_helper_blank - implementation for &fb_ops.fb_blank
6529edbd4a0SFrançois Tigeot  * @blank: desired blanking state
6539edbd4a0SFrançois Tigeot  * @info: fbdev registered by the helper
6545718399fSFrançois Tigeot  */
drm_fb_helper_blank(int blank,struct fb_info * info)6555718399fSFrançois Tigeot int drm_fb_helper_blank(int blank, struct fb_info *info)
6565718399fSFrançois Tigeot {
657a05eeebfSFrançois Tigeot 	if (oops_in_progress)
658a05eeebfSFrançois Tigeot 		return -EBUSY;
659a05eeebfSFrançois Tigeot 
6605718399fSFrançois Tigeot 	switch (blank) {
6615718399fSFrançois Tigeot 	/* Display: On; HSync: On, VSync: On */
6625718399fSFrançois Tigeot 	case FB_BLANK_UNBLANK:
6639edbd4a0SFrançois Tigeot 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
6645718399fSFrançois Tigeot 		break;
665ba409a88SImre Vadász #if 0
6665718399fSFrançois Tigeot 	/* Display: Off; HSync: On, VSync: On */
6675718399fSFrançois Tigeot 	case FB_BLANK_NORMAL:
6689edbd4a0SFrançois Tigeot 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
6695718399fSFrançois Tigeot 		break;
6705718399fSFrançois Tigeot 	/* Display: Off; HSync: Off, VSync: On */
6715718399fSFrançois Tigeot 	case FB_BLANK_HSYNC_SUSPEND:
6729edbd4a0SFrançois Tigeot 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
6735718399fSFrançois Tigeot 		break;
6745718399fSFrançois Tigeot 	/* Display: Off; HSync: On, VSync: Off */
6755718399fSFrançois Tigeot 	case FB_BLANK_VSYNC_SUSPEND:
6769edbd4a0SFrançois Tigeot 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
6775718399fSFrançois Tigeot 		break;
678ba409a88SImre Vadász #endif
6795718399fSFrançois Tigeot 	/* Display: Off; HSync: Off, VSync: Off */
6805718399fSFrançois Tigeot 	case FB_BLANK_POWERDOWN:
6819edbd4a0SFrançois Tigeot 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
6825718399fSFrançois Tigeot 		break;
6835718399fSFrançois Tigeot 	}
6845718399fSFrançois Tigeot 	return 0;
6855718399fSFrançois Tigeot }
6869edbd4a0SFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_blank);
6875718399fSFrançois Tigeot 
drm_fb_helper_modeset_release(struct drm_fb_helper * helper,struct drm_mode_set * modeset)6884be47400SFrançois Tigeot static void drm_fb_helper_modeset_release(struct drm_fb_helper *helper,
6894be47400SFrançois Tigeot 					  struct drm_mode_set *modeset)
6904be47400SFrançois Tigeot {
6914be47400SFrançois Tigeot 	int i;
6924be47400SFrançois Tigeot 
6934be47400SFrançois Tigeot 	for (i = 0; i < modeset->num_connectors; i++) {
694a85cb24fSFrançois Tigeot 		drm_connector_put(modeset->connectors[i]);
6954be47400SFrançois Tigeot 		modeset->connectors[i] = NULL;
6964be47400SFrançois Tigeot 	}
6974be47400SFrançois Tigeot 	modeset->num_connectors = 0;
6984be47400SFrançois Tigeot 
6994be47400SFrançois Tigeot 	drm_mode_destroy(helper->dev, modeset->mode);
7004be47400SFrançois Tigeot 	modeset->mode = NULL;
7014be47400SFrançois Tigeot 
7024be47400SFrançois Tigeot 	/* FIXME should hold a ref? */
7034be47400SFrançois Tigeot 	modeset->fb = NULL;
7044be47400SFrançois Tigeot }
7054be47400SFrançois Tigeot 
drm_fb_helper_crtc_free(struct drm_fb_helper * helper)7065718399fSFrançois Tigeot static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
7075718399fSFrançois Tigeot {
7085718399fSFrançois Tigeot 	int i;
7095718399fSFrançois Tigeot 
7108621f407SFrançois Tigeot 	for (i = 0; i < helper->connector_count; i++) {
711a85cb24fSFrançois Tigeot 		drm_connector_put(helper->connector_info[i]->connector);
7129edbd4a0SFrançois Tigeot 		kfree(helper->connector_info[i]);
7138621f407SFrançois Tigeot 	}
7149edbd4a0SFrançois Tigeot 	kfree(helper->connector_info);
7154be47400SFrançois Tigeot 
7166f486c69SFrançois Tigeot 	for (i = 0; i < helper->crtc_count; i++) {
7174be47400SFrançois Tigeot 		struct drm_mode_set *modeset = &helper->crtc_info[i].mode_set;
7184be47400SFrançois Tigeot 
7194be47400SFrançois Tigeot 		drm_fb_helper_modeset_release(helper, modeset);
7204be47400SFrançois Tigeot 		kfree(modeset->connectors);
7216f486c69SFrançois Tigeot 	}
7229edbd4a0SFrançois Tigeot 	kfree(helper->crtc_info);
7235718399fSFrançois Tigeot }
7245718399fSFrançois Tigeot 
drm_fb_helper_resume_worker(struct work_struct * work)7251dedbd3bSFrançois Tigeot static void drm_fb_helper_resume_worker(struct work_struct *work)
7261dedbd3bSFrançois Tigeot {
7271dedbd3bSFrançois Tigeot #if 0
7281dedbd3bSFrançois Tigeot 	struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
7291dedbd3bSFrançois Tigeot 						    resume_work);
7301dedbd3bSFrançois Tigeot 
7311dedbd3bSFrançois Tigeot 	console_lock();
7321dedbd3bSFrançois Tigeot 	fb_set_suspend(helper->fbdev, 0);
7331dedbd3bSFrançois Tigeot 	console_unlock();
7341dedbd3bSFrançois Tigeot #endif
7351dedbd3bSFrançois Tigeot }
7361dedbd3bSFrançois Tigeot 
drm_fb_helper_dirty_work(struct work_struct * work)7378621f407SFrançois Tigeot static void drm_fb_helper_dirty_work(struct work_struct *work)
7388621f407SFrançois Tigeot {
7398621f407SFrançois Tigeot 	struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
7408621f407SFrançois Tigeot 						    dirty_work);
7418621f407SFrançois Tigeot 	struct drm_clip_rect *clip = &helper->dirty_clip;
7428621f407SFrançois Tigeot 	struct drm_clip_rect clip_copy;
7438621f407SFrançois Tigeot 	unsigned long flags;
7448621f407SFrançois Tigeot 
7458621f407SFrançois Tigeot 	spin_lock_irqsave(&helper->dirty_lock, flags);
7468621f407SFrançois Tigeot 	clip_copy = *clip;
7478621f407SFrançois Tigeot 	clip->x1 = clip->y1 = ~0;
7488621f407SFrançois Tigeot 	clip->x2 = clip->y2 = 0;
7498621f407SFrançois Tigeot 	spin_unlock_irqrestore(&helper->dirty_lock, flags);
7508621f407SFrançois Tigeot 
7514be47400SFrançois Tigeot 	/* call dirty callback only when it has been really touched */
7524be47400SFrançois Tigeot 	if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2)
7538621f407SFrançois Tigeot 		helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1);
7548621f407SFrançois Tigeot }
7558621f407SFrançois Tigeot 
7569edbd4a0SFrançois Tigeot /**
757c6f73aabSFrançois Tigeot  * drm_fb_helper_prepare - setup a drm_fb_helper structure
758c6f73aabSFrançois Tigeot  * @dev: DRM device
759c6f73aabSFrançois Tigeot  * @helper: driver-allocated fbdev helper structure to set up
760c6f73aabSFrançois Tigeot  * @funcs: pointer to structure of functions associate with this helper
761c6f73aabSFrançois Tigeot  *
762c6f73aabSFrançois Tigeot  * Sets up the bare minimum to make the framebuffer helper usable. This is
763c6f73aabSFrançois Tigeot  * useful to implement race-free initialization of the polling helpers.
764c6f73aabSFrançois Tigeot  */
drm_fb_helper_prepare(struct drm_device * dev,struct drm_fb_helper * helper,const struct drm_fb_helper_funcs * funcs)765c6f73aabSFrançois Tigeot void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
766c6f73aabSFrançois Tigeot 			   const struct drm_fb_helper_funcs *funcs)
767c6f73aabSFrançois Tigeot {
768c6f73aabSFrançois Tigeot 	INIT_LIST_HEAD(&helper->kernel_fb_list);
769a85cb24fSFrançois Tigeot 	lockinit(&helper->dirty_lock, "drm_fb_helper dirty_lock", 0, 0);
7701dedbd3bSFrançois Tigeot 	INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
7718621f407SFrançois Tigeot 	INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work);
7728621f407SFrançois Tigeot 	helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0;
7733f2dd94aSFrançois Tigeot 	lockinit(&helper->lock, "dfbhl", 0, LK_CANRECURSE);
774c6f73aabSFrançois Tigeot 	helper->funcs = funcs;
775c6f73aabSFrançois Tigeot 	helper->dev = dev;
776c6f73aabSFrançois Tigeot }
777c6f73aabSFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_prepare);
778c6f73aabSFrançois Tigeot 
779c6f73aabSFrançois Tigeot /**
780a85cb24fSFrançois Tigeot  * drm_fb_helper_init - initialize a &struct drm_fb_helper
7819edbd4a0SFrançois Tigeot  * @dev: drm device
7829edbd4a0SFrançois Tigeot  * @fb_helper: driver-allocated fbdev helper structure to initialize
7839edbd4a0SFrançois Tigeot  * @max_conn_count: max connector count
7849edbd4a0SFrançois Tigeot  *
7859edbd4a0SFrançois Tigeot  * This allocates the structures for the fbdev helper with the given limits.
7869edbd4a0SFrançois Tigeot  * Note that this won't yet touch the hardware (through the driver interfaces)
7879edbd4a0SFrançois Tigeot  * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
7889edbd4a0SFrançois Tigeot  * to allow driver writes more control over the exact init sequence.
7899edbd4a0SFrançois Tigeot  *
790c6f73aabSFrançois Tigeot  * Drivers must call drm_fb_helper_prepare() before calling this function.
7919edbd4a0SFrançois Tigeot  *
7929edbd4a0SFrançois Tigeot  * RETURNS:
7939edbd4a0SFrançois Tigeot  * Zero if everything went ok, nonzero otherwise.
7949edbd4a0SFrançois Tigeot  */
drm_fb_helper_init(struct drm_device * dev,struct drm_fb_helper * fb_helper,int max_conn_count)7955718399fSFrançois Tigeot int drm_fb_helper_init(struct drm_device *dev,
7965718399fSFrançois Tigeot 		       struct drm_fb_helper *fb_helper,
797a85cb24fSFrançois Tigeot 		       int max_conn_count)
7985718399fSFrançois Tigeot {
7995718399fSFrançois Tigeot 	struct drm_crtc *crtc;
800a85cb24fSFrançois Tigeot 	struct drm_mode_config *config = &dev->mode_config;
8015718399fSFrançois Tigeot 	int i;
8025718399fSFrançois Tigeot 
803352ff8bdSFrançois Tigeot 	if (!drm_fbdev_emulation)
804352ff8bdSFrançois Tigeot 		return 0;
805352ff8bdSFrançois Tigeot 
806ba55f2f5SFrançois Tigeot 	if (!max_conn_count)
807ba55f2f5SFrançois Tigeot 		return -EINVAL;
808ba55f2f5SFrançois Tigeot 
809a85cb24fSFrançois Tigeot 	fb_helper->crtc_info = kcalloc(config->num_crtc, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
8104a968d2aSFrançois Tigeot 	if (!fb_helper->crtc_info)
8114a968d2aSFrançois Tigeot 		return -ENOMEM;
8125718399fSFrançois Tigeot 
813a85cb24fSFrançois Tigeot 	fb_helper->crtc_count = config->num_crtc;
81422ee5efbSFrançois Tigeot 	fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
8154a968d2aSFrançois Tigeot 	if (!fb_helper->connector_info) {
816158486a6SFrançois Tigeot 		kfree(fb_helper->crtc_info);
8174a968d2aSFrançois Tigeot 		return -ENOMEM;
8184a968d2aSFrançois Tigeot 	}
81924edb884SFrançois Tigeot 	fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
8205718399fSFrançois Tigeot 	fb_helper->connector_count = 0;
8215718399fSFrançois Tigeot 
822a85cb24fSFrançois Tigeot 	for (i = 0; i < fb_helper->crtc_count; i++) {
8235718399fSFrançois Tigeot 		fb_helper->crtc_info[i].mode_set.connectors =
82422ee5efbSFrançois Tigeot 			kcalloc(max_conn_count,
82522ee5efbSFrançois Tigeot 				sizeof(struct drm_connector *),
82622ee5efbSFrançois Tigeot 				GFP_KERNEL);
8275718399fSFrançois Tigeot 
8284a968d2aSFrançois Tigeot 		if (!fb_helper->crtc_info[i].mode_set.connectors)
8294a968d2aSFrançois Tigeot 			goto out_free;
8305718399fSFrançois Tigeot 		fb_helper->crtc_info[i].mode_set.num_connectors = 0;
8315718399fSFrançois Tigeot 	}
8325718399fSFrançois Tigeot 
8335718399fSFrançois Tigeot 	i = 0;
834a05eeebfSFrançois Tigeot 	drm_for_each_crtc(crtc, dev) {
8355718399fSFrançois Tigeot 		fb_helper->crtc_info[i].mode_set.crtc = crtc;
8365718399fSFrançois Tigeot 		i++;
8375718399fSFrançois Tigeot 	}
8384a968d2aSFrançois Tigeot 
8395718399fSFrançois Tigeot 	return 0;
8404a968d2aSFrançois Tigeot out_free:
8414a968d2aSFrançois Tigeot 	drm_fb_helper_crtc_free(fb_helper);
8424a968d2aSFrançois Tigeot 	return -ENOMEM;
8435718399fSFrançois Tigeot }
8444a968d2aSFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_init);
8455718399fSFrançois Tigeot 
846a05eeebfSFrançois Tigeot /**
847a05eeebfSFrançois Tigeot  * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
848a05eeebfSFrançois Tigeot  * @fb_helper: driver-allocated fbdev helper
849a05eeebfSFrançois Tigeot  *
850a05eeebfSFrançois Tigeot  * A helper to alloc fb_info and the members cmap and apertures. Called
851a85cb24fSFrançois Tigeot  * by the driver within the fb_probe fb_helper callback function. Drivers do not
852a85cb24fSFrançois Tigeot  * need to release the allocated fb_info structure themselves, this is
853a85cb24fSFrançois Tigeot  * automatically done when calling drm_fb_helper_fini().
854a05eeebfSFrançois Tigeot  *
855a05eeebfSFrançois Tigeot  * RETURNS:
856a05eeebfSFrançois Tigeot  * fb_info pointer if things went okay, pointer containing error code
857a05eeebfSFrançois Tigeot  * otherwise
858a05eeebfSFrançois Tigeot  */
drm_fb_helper_alloc_fbi(struct drm_fb_helper * fb_helper)859a05eeebfSFrançois Tigeot struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
860a05eeebfSFrançois Tigeot {
8613f122055SFrançois Tigeot 	struct device *dev = fb_helper->dev->dev;
862a05eeebfSFrançois Tigeot 	struct fb_info *info;
863a85cb24fSFrançois Tigeot #if 0
864a85cb24fSFrançois Tigeot 	int ret;
865a85cb24fSFrançois Tigeot #endif
866a05eeebfSFrançois Tigeot 
867f25fc9b7SImre Vadász 	info = framebuffer_alloc(0, dev);
868a05eeebfSFrançois Tigeot 	if (!info)
869a05eeebfSFrançois Tigeot 		return ERR_PTR(-ENOMEM);
870a05eeebfSFrançois Tigeot 
871a05eeebfSFrançois Tigeot #if 0
872a05eeebfSFrançois Tigeot 	ret = fb_alloc_cmap(&info->cmap, 256, 0);
873a05eeebfSFrançois Tigeot 	if (ret)
874a05eeebfSFrançois Tigeot 		goto err_release;
875a05eeebfSFrançois Tigeot 
876a05eeebfSFrançois Tigeot 	info->apertures = alloc_apertures(1);
877a05eeebfSFrançois Tigeot 	if (!info->apertures) {
878a05eeebfSFrançois Tigeot 		ret = -ENOMEM;
879a05eeebfSFrançois Tigeot 		goto err_free_cmap;
880a05eeebfSFrançois Tigeot 	}
881a05eeebfSFrançois Tigeot #endif
882a05eeebfSFrançois Tigeot 
883a05eeebfSFrançois Tigeot 	fb_helper->fbdev = info;
884a05eeebfSFrançois Tigeot 
885a05eeebfSFrançois Tigeot 	return info;
886a05eeebfSFrançois Tigeot 
887a05eeebfSFrançois Tigeot #if 0
888a05eeebfSFrançois Tigeot err_free_cmap:
889a05eeebfSFrançois Tigeot 	fb_dealloc_cmap(&info->cmap);
890a05eeebfSFrançois Tigeot err_release:
891a05eeebfSFrançois Tigeot 	framebuffer_release(info);
892a05eeebfSFrançois Tigeot 	return ERR_PTR(ret);
893a05eeebfSFrançois Tigeot #endif
894a05eeebfSFrançois Tigeot }
895a05eeebfSFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
896a05eeebfSFrançois Tigeot 
897a05eeebfSFrançois Tigeot /**
898a05eeebfSFrançois Tigeot  * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
899a05eeebfSFrançois Tigeot  * @fb_helper: driver-allocated fbdev helper
900a05eeebfSFrançois Tigeot  *
901a05eeebfSFrançois Tigeot  * A wrapper around unregister_framebuffer, to release the fb_info
902a85cb24fSFrançois Tigeot  * framebuffer device. This must be called before releasing all resources for
903a85cb24fSFrançois Tigeot  * @fb_helper by calling drm_fb_helper_fini().
904a05eeebfSFrançois Tigeot  */
drm_fb_helper_unregister_fbi(struct drm_fb_helper * fb_helper)905a05eeebfSFrançois Tigeot void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
906a05eeebfSFrançois Tigeot {
907a05eeebfSFrançois Tigeot 	if (fb_helper && fb_helper->fbdev)
908a05eeebfSFrançois Tigeot 		unregister_framebuffer(fb_helper->fbdev);
909a05eeebfSFrançois Tigeot }
910a05eeebfSFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
911a05eeebfSFrançois Tigeot 
912a05eeebfSFrançois Tigeot /**
913a85cb24fSFrançois Tigeot  * drm_fb_helper_fini - finialize a &struct drm_fb_helper
914a05eeebfSFrançois Tigeot  * @fb_helper: driver-allocated fbdev helper
915a05eeebfSFrançois Tigeot  *
916a85cb24fSFrançois Tigeot  * This cleans up all remaining resources associated with @fb_helper. Must be
917a85cb24fSFrançois Tigeot  * called after drm_fb_helper_unlink_fbi() was called.
918a05eeebfSFrançois Tigeot  */
drm_fb_helper_fini(struct drm_fb_helper * fb_helper)919a85cb24fSFrançois Tigeot void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
920a05eeebfSFrançois Tigeot {
921a85cb24fSFrançois Tigeot 	struct fb_info *info;
922a05eeebfSFrançois Tigeot 
923a85cb24fSFrançois Tigeot 	if (!drm_fbdev_emulation || !fb_helper)
924a85cb24fSFrançois Tigeot 		return;
925a85cb24fSFrançois Tigeot 
9263f2dd94aSFrançois Tigeot 	cancel_work_sync(&fb_helper->resume_work);
9273f2dd94aSFrançois Tigeot 	cancel_work_sync(&fb_helper->dirty_work);
9283f2dd94aSFrançois Tigeot 
929a85cb24fSFrançois Tigeot 	info = fb_helper->fbdev;
930a05eeebfSFrançois Tigeot 	if (info) {
9313f122055SFrançois Tigeot #if 0
932a05eeebfSFrançois Tigeot 		if (info->cmap.len)
933a05eeebfSFrançois Tigeot 			fb_dealloc_cmap(&info->cmap);
934f25fc9b7SImre Vadász #endif
9353f122055SFrançois Tigeot 		framebuffer_release(info);
936a05eeebfSFrançois Tigeot 	}
937a05eeebfSFrançois Tigeot 	fb_helper->fbdev = NULL;
938352ff8bdSFrançois Tigeot 
9394be47400SFrançois Tigeot 	mutex_lock(&kernel_fb_helper_lock);
9405718399fSFrançois Tigeot 	if (!list_empty(&fb_helper->kernel_fb_list)) {
9415718399fSFrançois Tigeot 		list_del(&fb_helper->kernel_fb_list);
9425718399fSFrançois Tigeot #if 0
943a85cb24fSFrançois Tigeot 		if (list_empty(&kernel_fb_helper_list))
9445718399fSFrançois Tigeot 			unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
9455718399fSFrançois Tigeot #endif
9465718399fSFrançois Tigeot 	}
9474be47400SFrançois Tigeot 	mutex_unlock(&kernel_fb_helper_lock);
9485718399fSFrançois Tigeot 
9493f2dd94aSFrançois Tigeot 	mutex_destroy(&fb_helper->lock);
9505718399fSFrançois Tigeot 	drm_fb_helper_crtc_free(fb_helper);
9515718399fSFrançois Tigeot 
9525718399fSFrançois Tigeot }
9539edbd4a0SFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_fini);
9545718399fSFrançois Tigeot 
9555718399fSFrançois Tigeot #if 0
956a05eeebfSFrançois Tigeot /**
957a05eeebfSFrançois Tigeot  * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
958a05eeebfSFrançois Tigeot  * @fb_helper: driver-allocated fbdev helper
959a05eeebfSFrançois Tigeot  *
960a05eeebfSFrançois Tigeot  * A wrapper around unlink_framebuffer implemented by fbdev core
961a05eeebfSFrançois Tigeot  */
962a05eeebfSFrançois Tigeot void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
963a05eeebfSFrançois Tigeot {
964a05eeebfSFrançois Tigeot 	if (fb_helper && fb_helper->fbdev)
965a05eeebfSFrançois Tigeot 		unlink_framebuffer(fb_helper->fbdev);
966a05eeebfSFrançois Tigeot }
967a05eeebfSFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
968a05eeebfSFrançois Tigeot 
9698621f407SFrançois Tigeot static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y,
9708621f407SFrançois Tigeot 				u32 width, u32 height)
9718621f407SFrançois Tigeot {
9728621f407SFrançois Tigeot 	struct drm_fb_helper *helper = info->par;
9738621f407SFrançois Tigeot 	struct drm_clip_rect *clip = &helper->dirty_clip;
9748621f407SFrançois Tigeot 	unsigned long flags;
9758621f407SFrançois Tigeot 
9768621f407SFrançois Tigeot 	if (!helper->fb->funcs->dirty)
9778621f407SFrançois Tigeot 		return;
9788621f407SFrançois Tigeot 
9798621f407SFrançois Tigeot 	spin_lock_irqsave(&helper->dirty_lock, flags);
9808621f407SFrançois Tigeot 	clip->x1 = min_t(u32, clip->x1, x);
9818621f407SFrançois Tigeot 	clip->y1 = min_t(u32, clip->y1, y);
9828621f407SFrançois Tigeot 	clip->x2 = max_t(u32, clip->x2, x + width);
9838621f407SFrançois Tigeot 	clip->y2 = max_t(u32, clip->y2, y + height);
9848621f407SFrançois Tigeot 	spin_unlock_irqrestore(&helper->dirty_lock, flags);
9858621f407SFrançois Tigeot 
9868621f407SFrançois Tigeot 	schedule_work(&helper->dirty_work);
9878621f407SFrançois Tigeot }
9888621f407SFrançois Tigeot 
9898621f407SFrançois Tigeot /**
9908621f407SFrançois Tigeot  * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
9918621f407SFrançois Tigeot  * @info: fb_info struct pointer
9928621f407SFrançois Tigeot  * @pagelist: list of dirty mmap framebuffer pages
9938621f407SFrançois Tigeot  *
994a85cb24fSFrançois Tigeot  * This function is used as the &fb_deferred_io.deferred_io
9958621f407SFrançois Tigeot  * callback function for flushing the fbdev mmap writes.
9968621f407SFrançois Tigeot  */
9978621f407SFrançois Tigeot void drm_fb_helper_deferred_io(struct fb_info *info,
9988621f407SFrançois Tigeot 			       struct list_head *pagelist)
9998621f407SFrançois Tigeot {
10008621f407SFrançois Tigeot 	unsigned long start, end, min, max;
10018621f407SFrançois Tigeot 	struct page *page;
10028621f407SFrançois Tigeot 	u32 y1, y2;
10038621f407SFrançois Tigeot 
10048621f407SFrançois Tigeot 	min = ULONG_MAX;
10058621f407SFrançois Tigeot 	max = 0;
10068621f407SFrançois Tigeot 	list_for_each_entry(page, pagelist, lru) {
10078621f407SFrançois Tigeot 		start = page->index << PAGE_SHIFT;
10088621f407SFrançois Tigeot 		end = start + PAGE_SIZE - 1;
10098621f407SFrançois Tigeot 		min = min(min, start);
10108621f407SFrançois Tigeot 		max = max(max, end);
10118621f407SFrançois Tigeot 	}
10128621f407SFrançois Tigeot 
10138621f407SFrançois Tigeot 	if (min < max) {
10148621f407SFrançois Tigeot 		y1 = min / info->fix.line_length;
10158621f407SFrançois Tigeot 		y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length),
10168621f407SFrançois Tigeot 			   info->var.yres);
10178621f407SFrançois Tigeot 		drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1);
10188621f407SFrançois Tigeot 	}
10198621f407SFrançois Tigeot }
10208621f407SFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_deferred_io);
10218621f407SFrançois Tigeot 
1022a05eeebfSFrançois Tigeot /**
1023a05eeebfSFrançois Tigeot  * drm_fb_helper_sys_read - wrapper around fb_sys_read
1024a05eeebfSFrançois Tigeot  * @info: fb_info struct pointer
1025a05eeebfSFrançois Tigeot  * @buf: userspace buffer to read from framebuffer memory
1026a05eeebfSFrançois Tigeot  * @count: number of bytes to read from framebuffer memory
1027a05eeebfSFrançois Tigeot  * @ppos: read offset within framebuffer memory
1028a05eeebfSFrançois Tigeot  *
1029a05eeebfSFrançois Tigeot  * A wrapper around fb_sys_read implemented by fbdev core
1030a05eeebfSFrançois Tigeot  */
1031a05eeebfSFrançois Tigeot ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
1032a05eeebfSFrançois Tigeot 			       size_t count, loff_t *ppos)
1033a05eeebfSFrançois Tigeot {
1034a05eeebfSFrançois Tigeot 	return fb_sys_read(info, buf, count, ppos);
1035a05eeebfSFrançois Tigeot }
1036a05eeebfSFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_sys_read);
1037a05eeebfSFrançois Tigeot 
1038a05eeebfSFrançois Tigeot /**
1039a05eeebfSFrançois Tigeot  * drm_fb_helper_sys_write - wrapper around fb_sys_write
1040a05eeebfSFrançois Tigeot  * @info: fb_info struct pointer
1041a05eeebfSFrançois Tigeot  * @buf: userspace buffer to write to framebuffer memory
1042a05eeebfSFrançois Tigeot  * @count: number of bytes to write to framebuffer memory
1043a05eeebfSFrançois Tigeot  * @ppos: write offset within framebuffer memory
1044a05eeebfSFrançois Tigeot  *
1045a05eeebfSFrançois Tigeot  * A wrapper around fb_sys_write implemented by fbdev core
1046a05eeebfSFrançois Tigeot  */
1047a05eeebfSFrançois Tigeot ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
1048a05eeebfSFrançois Tigeot 				size_t count, loff_t *ppos)
1049a05eeebfSFrançois Tigeot {
10508621f407SFrançois Tigeot 	ssize_t ret;
10518621f407SFrançois Tigeot 
10528621f407SFrançois Tigeot 	ret = fb_sys_write(info, buf, count, ppos);
10538621f407SFrançois Tigeot 	if (ret > 0)
10548621f407SFrançois Tigeot 		drm_fb_helper_dirty(info, 0, 0, info->var.xres,
10558621f407SFrançois Tigeot 				    info->var.yres);
10568621f407SFrançois Tigeot 
10578621f407SFrançois Tigeot 	return ret;
1058a05eeebfSFrançois Tigeot }
1059a05eeebfSFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_sys_write);
1060a05eeebfSFrançois Tigeot 
1061a05eeebfSFrançois Tigeot /**
1062a05eeebfSFrançois Tigeot  * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
1063a05eeebfSFrançois Tigeot  * @info: fbdev registered by the helper
1064a05eeebfSFrançois Tigeot  * @rect: info about rectangle to fill
1065a05eeebfSFrançois Tigeot  *
1066a05eeebfSFrançois Tigeot  * A wrapper around sys_fillrect implemented by fbdev core
1067a05eeebfSFrançois Tigeot  */
1068a05eeebfSFrançois Tigeot void drm_fb_helper_sys_fillrect(struct fb_info *info,
1069a05eeebfSFrançois Tigeot 				const struct fb_fillrect *rect)
1070a05eeebfSFrançois Tigeot {
1071a05eeebfSFrançois Tigeot 	sys_fillrect(info, rect);
10728621f407SFrançois Tigeot 	drm_fb_helper_dirty(info, rect->dx, rect->dy,
10738621f407SFrançois Tigeot 			    rect->width, rect->height);
1074a05eeebfSFrançois Tigeot }
1075a05eeebfSFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
1076a05eeebfSFrançois Tigeot 
1077a05eeebfSFrançois Tigeot /**
1078a05eeebfSFrançois Tigeot  * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
1079a05eeebfSFrançois Tigeot  * @info: fbdev registered by the helper
1080a05eeebfSFrançois Tigeot  * @area: info about area to copy
1081a05eeebfSFrançois Tigeot  *
1082a05eeebfSFrançois Tigeot  * A wrapper around sys_copyarea implemented by fbdev core
1083a05eeebfSFrançois Tigeot  */
1084a05eeebfSFrançois Tigeot void drm_fb_helper_sys_copyarea(struct fb_info *info,
1085a05eeebfSFrançois Tigeot 				const struct fb_copyarea *area)
1086a05eeebfSFrançois Tigeot {
1087a05eeebfSFrançois Tigeot 	sys_copyarea(info, area);
10888621f407SFrançois Tigeot 	drm_fb_helper_dirty(info, area->dx, area->dy,
10898621f407SFrançois Tigeot 			    area->width, area->height);
1090a05eeebfSFrançois Tigeot }
1091a05eeebfSFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
1092a05eeebfSFrançois Tigeot 
1093a05eeebfSFrançois Tigeot /**
1094a05eeebfSFrançois Tigeot  * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
1095a05eeebfSFrançois Tigeot  * @info: fbdev registered by the helper
1096a05eeebfSFrançois Tigeot  * @image: info about image to blit
1097a05eeebfSFrançois Tigeot  *
1098a05eeebfSFrançois Tigeot  * A wrapper around sys_imageblit implemented by fbdev core
1099a05eeebfSFrançois Tigeot  */
1100a05eeebfSFrançois Tigeot void drm_fb_helper_sys_imageblit(struct fb_info *info,
1101a05eeebfSFrançois Tigeot 				 const struct fb_image *image)
1102a05eeebfSFrançois Tigeot {
1103a05eeebfSFrançois Tigeot 	sys_imageblit(info, image);
11048621f407SFrançois Tigeot 	drm_fb_helper_dirty(info, image->dx, image->dy,
11058621f407SFrançois Tigeot 			    image->width, image->height);
1106a05eeebfSFrançois Tigeot }
1107a05eeebfSFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
1108a05eeebfSFrançois Tigeot 
1109a05eeebfSFrançois Tigeot /**
1110a05eeebfSFrançois Tigeot  * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
1111a05eeebfSFrançois Tigeot  * @info: fbdev registered by the helper
1112a05eeebfSFrançois Tigeot  * @rect: info about rectangle to fill
1113a05eeebfSFrançois Tigeot  *
1114a05eeebfSFrançois Tigeot  * A wrapper around cfb_imageblit implemented by fbdev core
1115a05eeebfSFrançois Tigeot  */
1116a05eeebfSFrançois Tigeot void drm_fb_helper_cfb_fillrect(struct fb_info *info,
1117a05eeebfSFrançois Tigeot 				const struct fb_fillrect *rect)
1118a05eeebfSFrançois Tigeot {
1119a05eeebfSFrançois Tigeot 	cfb_fillrect(info, rect);
11208621f407SFrançois Tigeot 	drm_fb_helper_dirty(info, rect->dx, rect->dy,
11218621f407SFrançois Tigeot 			    rect->width, rect->height);
1122a05eeebfSFrançois Tigeot }
1123a05eeebfSFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
1124a05eeebfSFrançois Tigeot 
1125a05eeebfSFrançois Tigeot /**
1126a05eeebfSFrançois Tigeot  * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
1127a05eeebfSFrançois Tigeot  * @info: fbdev registered by the helper
1128a05eeebfSFrançois Tigeot  * @area: info about area to copy
1129a05eeebfSFrançois Tigeot  *
1130a05eeebfSFrançois Tigeot  * A wrapper around cfb_copyarea implemented by fbdev core
1131a05eeebfSFrançois Tigeot  */
1132a05eeebfSFrançois Tigeot void drm_fb_helper_cfb_copyarea(struct fb_info *info,
1133a05eeebfSFrançois Tigeot 				const struct fb_copyarea *area)
1134a05eeebfSFrançois Tigeot {
1135a05eeebfSFrançois Tigeot 	cfb_copyarea(info, area);
11368621f407SFrançois Tigeot 	drm_fb_helper_dirty(info, area->dx, area->dy,
11378621f407SFrançois Tigeot 			    area->width, area->height);
1138a05eeebfSFrançois Tigeot }
1139a05eeebfSFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
1140a05eeebfSFrançois Tigeot 
1141a05eeebfSFrançois Tigeot /**
1142a05eeebfSFrançois Tigeot  * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
1143a05eeebfSFrançois Tigeot  * @info: fbdev registered by the helper
1144a05eeebfSFrançois Tigeot  * @image: info about image to blit
1145a05eeebfSFrançois Tigeot  *
1146a05eeebfSFrançois Tigeot  * A wrapper around cfb_imageblit implemented by fbdev core
1147a05eeebfSFrançois Tigeot  */
1148a05eeebfSFrançois Tigeot void drm_fb_helper_cfb_imageblit(struct fb_info *info,
1149a05eeebfSFrançois Tigeot 				 const struct fb_image *image)
1150a05eeebfSFrançois Tigeot {
1151a05eeebfSFrançois Tigeot 	cfb_imageblit(info, image);
11528621f407SFrançois Tigeot 	drm_fb_helper_dirty(info, image->dx, image->dy,
11538621f407SFrançois Tigeot 			    image->width, image->height);
1154a05eeebfSFrançois Tigeot }
1155a05eeebfSFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
1156a05eeebfSFrançois Tigeot 
1157a05eeebfSFrançois Tigeot /**
1158a05eeebfSFrançois Tigeot  * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
1159a05eeebfSFrançois Tigeot  * @fb_helper: driver-allocated fbdev helper
11601e12ee3bSFrançois Tigeot  * @suspend: whether to suspend or resume
1161a05eeebfSFrançois Tigeot  *
11621dedbd3bSFrançois Tigeot  * A wrapper around fb_set_suspend implemented by fbdev core.
11631dedbd3bSFrançois Tigeot  * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
11641dedbd3bSFrançois Tigeot  * the lock yourself
1165a05eeebfSFrançois Tigeot  */
11661e12ee3bSFrançois Tigeot void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
1167a05eeebfSFrançois Tigeot {
1168a05eeebfSFrançois Tigeot 	if (fb_helper && fb_helper->fbdev)
11691e12ee3bSFrançois Tigeot 		fb_set_suspend(fb_helper->fbdev, suspend);
1170a05eeebfSFrançois Tigeot }
1171a05eeebfSFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_set_suspend);
1172a05eeebfSFrançois Tigeot 
11731dedbd3bSFrançois Tigeot /**
11741dedbd3bSFrançois Tigeot  * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
11751dedbd3bSFrançois Tigeot  *                                      takes the console lock
11761dedbd3bSFrançois Tigeot  * @fb_helper: driver-allocated fbdev helper
11771dedbd3bSFrançois Tigeot  * @suspend: whether to suspend or resume
11781dedbd3bSFrançois Tigeot  *
11791dedbd3bSFrançois Tigeot  * A wrapper around fb_set_suspend() that takes the console lock. If the lock
11801dedbd3bSFrançois Tigeot  * isn't available on resume, a worker is tasked with waiting for the lock
11811dedbd3bSFrançois Tigeot  * to become available. The console lock can be pretty contented on resume
11821dedbd3bSFrançois Tigeot  * due to all the printk activity.
11831dedbd3bSFrançois Tigeot  *
11841dedbd3bSFrançois Tigeot  * This function can be called multiple times with the same state since
1185a85cb24fSFrançois Tigeot  * &fb_info.state is checked to see if fbdev is running or not before locking.
11861dedbd3bSFrançois Tigeot  *
11871dedbd3bSFrançois Tigeot  * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
11881dedbd3bSFrançois Tigeot  */
11891dedbd3bSFrançois Tigeot void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
11901dedbd3bSFrançois Tigeot 					bool suspend)
11911dedbd3bSFrançois Tigeot {
11921dedbd3bSFrançois Tigeot 	if (!fb_helper || !fb_helper->fbdev)
11931dedbd3bSFrançois Tigeot 		return;
11941dedbd3bSFrançois Tigeot 
11951dedbd3bSFrançois Tigeot 	/* make sure there's no pending/ongoing resume */
11961dedbd3bSFrançois Tigeot 	flush_work(&fb_helper->resume_work);
11971dedbd3bSFrançois Tigeot 
11981dedbd3bSFrançois Tigeot 	if (suspend) {
11991dedbd3bSFrançois Tigeot 		if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING)
12001dedbd3bSFrançois Tigeot 			return;
12011dedbd3bSFrançois Tigeot 
12021dedbd3bSFrançois Tigeot 		console_lock();
12031dedbd3bSFrançois Tigeot 
12041dedbd3bSFrançois Tigeot 	} else {
12051dedbd3bSFrançois Tigeot 		if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING)
12061dedbd3bSFrançois Tigeot 			return;
12071dedbd3bSFrançois Tigeot 
12081dedbd3bSFrançois Tigeot 		if (!console_trylock()) {
12091dedbd3bSFrançois Tigeot 			schedule_work(&fb_helper->resume_work);
12101dedbd3bSFrançois Tigeot 			return;
12111dedbd3bSFrançois Tigeot 		}
12121dedbd3bSFrançois Tigeot 	}
12131dedbd3bSFrançois Tigeot 
12141dedbd3bSFrançois Tigeot 	fb_set_suspend(fb_helper->fbdev, suspend);
12151dedbd3bSFrançois Tigeot 	console_unlock();
12161dedbd3bSFrançois Tigeot }
12171dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked);
12181dedbd3bSFrançois Tigeot 
12193f2dd94aSFrançois Tigeot static int setcmap_pseudo_palette(struct fb_cmap *cmap, struct fb_info *info)
12205718399fSFrançois Tigeot {
12213f2dd94aSFrançois Tigeot 	u32 *palette = (u32 *)info->pseudo_palette;
12223f2dd94aSFrançois Tigeot 	int i;
12235718399fSFrançois Tigeot 
12243f2dd94aSFrançois Tigeot 	if (cmap->start + cmap->len > 16)
12255718399fSFrançois Tigeot 		return -EINVAL;
12263f2dd94aSFrançois Tigeot 
12273f2dd94aSFrançois Tigeot 	for (i = 0; i < cmap->len; ++i) {
12283f2dd94aSFrançois Tigeot 		u16 red = cmap->red[i];
12293f2dd94aSFrançois Tigeot 		u16 green = cmap->green[i];
12303f2dd94aSFrançois Tigeot 		u16 blue = cmap->blue[i];
12313f2dd94aSFrançois Tigeot 		u32 value;
12323f2dd94aSFrançois Tigeot 
12333f2dd94aSFrançois Tigeot 		red >>= 16 - info->var.red.length;
12343f2dd94aSFrançois Tigeot 		green >>= 16 - info->var.green.length;
12353f2dd94aSFrançois Tigeot 		blue >>= 16 - info->var.blue.length;
12365718399fSFrançois Tigeot 		value = (red << info->var.red.offset) |
12375718399fSFrançois Tigeot 			(green << info->var.green.offset) |
12385718399fSFrançois Tigeot 			(blue << info->var.blue.offset);
12395718399fSFrançois Tigeot 		if (info->var.transp.length > 0) {
12405718399fSFrançois Tigeot 			u32 mask = (1 << info->var.transp.length) - 1;
1241a85cb24fSFrançois Tigeot 
12425718399fSFrançois Tigeot 			mask <<= info->var.transp.offset;
12435718399fSFrançois Tigeot 			value |= mask;
12445718399fSFrançois Tigeot 		}
12453f2dd94aSFrançois Tigeot 		palette[cmap->start + i] = value;
12463f2dd94aSFrançois Tigeot 	}
12473f2dd94aSFrançois Tigeot 
12485718399fSFrançois Tigeot 	return 0;
12495718399fSFrançois Tigeot }
12505718399fSFrançois Tigeot 
12513f2dd94aSFrançois Tigeot static int setcmap_legacy(struct fb_cmap *cmap, struct fb_info *info)
12523f2dd94aSFrançois Tigeot {
12533f2dd94aSFrançois Tigeot 	struct drm_fb_helper *fb_helper = info->par;
12543f2dd94aSFrançois Tigeot 	struct drm_crtc *crtc;
12553f2dd94aSFrançois Tigeot 	u16 *r, *g, *b;
12563f2dd94aSFrançois Tigeot 	int i, ret = 0;
12573f2dd94aSFrançois Tigeot 
12583f2dd94aSFrançois Tigeot 	drm_modeset_lock_all(fb_helper->dev);
12593f2dd94aSFrançois Tigeot 	for (i = 0; i < fb_helper->crtc_count; i++) {
12603f2dd94aSFrançois Tigeot 		crtc = fb_helper->crtc_info[i].mode_set.crtc;
12613f2dd94aSFrançois Tigeot 		if (!crtc->funcs->gamma_set || !crtc->gamma_size)
12629edbd4a0SFrançois Tigeot 			return -EINVAL;
12639edbd4a0SFrançois Tigeot 
12643f2dd94aSFrançois Tigeot 		if (cmap->start + cmap->len > crtc->gamma_size)
12653f2dd94aSFrançois Tigeot 			return -EINVAL;
12665718399fSFrançois Tigeot 
12673f2dd94aSFrançois Tigeot 		r = crtc->gamma_store;
12683f2dd94aSFrançois Tigeot 		g = r + crtc->gamma_size;
12693f2dd94aSFrançois Tigeot 		b = g + crtc->gamma_size;
12705718399fSFrançois Tigeot 
12713f2dd94aSFrançois Tigeot 		memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
12723f2dd94aSFrançois Tigeot 		memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
12733f2dd94aSFrançois Tigeot 		memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
12743f2dd94aSFrançois Tigeot 
12753f2dd94aSFrançois Tigeot 		ret = crtc->funcs->gamma_set(crtc, r, g, b,
12763f2dd94aSFrançois Tigeot 					     crtc->gamma_size, NULL);
12773f2dd94aSFrançois Tigeot 		if (ret)
12783f2dd94aSFrançois Tigeot 			return ret;
12793f2dd94aSFrançois Tigeot 	}
12803f2dd94aSFrançois Tigeot 	drm_modeset_unlock_all(fb_helper->dev);
12813f2dd94aSFrançois Tigeot 
12823f2dd94aSFrançois Tigeot 	return ret;
12833f2dd94aSFrançois Tigeot }
12843f2dd94aSFrançois Tigeot 
12853f2dd94aSFrançois Tigeot static struct drm_property_blob *setcmap_new_gamma_lut(struct drm_crtc *crtc,
12863f2dd94aSFrançois Tigeot 						       struct fb_cmap *cmap)
12873f2dd94aSFrançois Tigeot {
12883f2dd94aSFrançois Tigeot 	struct drm_device *dev = crtc->dev;
12893f2dd94aSFrançois Tigeot 	struct drm_property_blob *gamma_lut;
12903f2dd94aSFrançois Tigeot 	struct drm_color_lut *lut;
12913f2dd94aSFrançois Tigeot 	int size = crtc->gamma_size;
12923f2dd94aSFrançois Tigeot 	int i;
12933f2dd94aSFrançois Tigeot 
12943f2dd94aSFrançois Tigeot 	if (!size || cmap->start + cmap->len > size)
12953f2dd94aSFrançois Tigeot 		return ERR_PTR(-EINVAL);
12963f2dd94aSFrançois Tigeot 
12973f2dd94aSFrançois Tigeot 	gamma_lut = drm_property_create_blob(dev, sizeof(*lut) * size, NULL);
12983f2dd94aSFrançois Tigeot 	if (IS_ERR(gamma_lut))
12993f2dd94aSFrançois Tigeot 		return gamma_lut;
13003f2dd94aSFrançois Tigeot 
13013f2dd94aSFrançois Tigeot 	lut = (struct drm_color_lut *)gamma_lut->data;
13023f2dd94aSFrançois Tigeot 	if (cmap->start || cmap->len != size) {
13033f2dd94aSFrançois Tigeot 		u16 *r = crtc->gamma_store;
13043f2dd94aSFrançois Tigeot 		u16 *g = r + crtc->gamma_size;
13053f2dd94aSFrançois Tigeot 		u16 *b = g + crtc->gamma_size;
13063f2dd94aSFrançois Tigeot 
13073f2dd94aSFrançois Tigeot 		for (i = 0; i < cmap->start; i++) {
13083f2dd94aSFrançois Tigeot 			lut[i].red = r[i];
13093f2dd94aSFrançois Tigeot 			lut[i].green = g[i];
13103f2dd94aSFrançois Tigeot 			lut[i].blue = b[i];
13113f2dd94aSFrançois Tigeot 		}
13123f2dd94aSFrançois Tigeot 		for (i = cmap->start + cmap->len; i < size; i++) {
13133f2dd94aSFrançois Tigeot 			lut[i].red = r[i];
13143f2dd94aSFrançois Tigeot 			lut[i].green = g[i];
13153f2dd94aSFrançois Tigeot 			lut[i].blue = b[i];
13163f2dd94aSFrançois Tigeot 		}
13173f2dd94aSFrançois Tigeot 	}
13183f2dd94aSFrançois Tigeot 
13193f2dd94aSFrançois Tigeot 	for (i = 0; i < cmap->len; i++) {
13203f2dd94aSFrançois Tigeot 		lut[cmap->start + i].red = cmap->red[i];
13213f2dd94aSFrançois Tigeot 		lut[cmap->start + i].green = cmap->green[i];
13223f2dd94aSFrançois Tigeot 		lut[cmap->start + i].blue = cmap->blue[i];
13233f2dd94aSFrançois Tigeot 	}
13243f2dd94aSFrançois Tigeot 
13253f2dd94aSFrançois Tigeot 	return gamma_lut;
13263f2dd94aSFrançois Tigeot }
13273f2dd94aSFrançois Tigeot 
13283f2dd94aSFrançois Tigeot static int setcmap_atomic(struct fb_cmap *cmap, struct fb_info *info)
13293f2dd94aSFrançois Tigeot {
13303f2dd94aSFrançois Tigeot 	struct drm_fb_helper *fb_helper = info->par;
13313f2dd94aSFrançois Tigeot 	struct drm_device *dev = fb_helper->dev;
13323f2dd94aSFrançois Tigeot 	struct drm_property_blob *gamma_lut = NULL;
13333f2dd94aSFrançois Tigeot 	struct drm_modeset_acquire_ctx ctx;
13343f2dd94aSFrançois Tigeot 	struct drm_crtc_state *crtc_state;
13353f2dd94aSFrançois Tigeot 	struct drm_atomic_state *state;
13363f2dd94aSFrançois Tigeot 	struct drm_crtc *crtc;
13373f2dd94aSFrançois Tigeot 	u16 *r, *g, *b;
13383f2dd94aSFrançois Tigeot 	int i, ret = 0;
13393f2dd94aSFrançois Tigeot 	bool replaced;
13403f2dd94aSFrançois Tigeot 
13413f2dd94aSFrançois Tigeot 	drm_modeset_acquire_init(&ctx, 0);
13423f2dd94aSFrançois Tigeot 
13433f2dd94aSFrançois Tigeot 	state = drm_atomic_state_alloc(dev);
13443f2dd94aSFrançois Tigeot 	if (!state) {
13453f2dd94aSFrançois Tigeot 		ret = -ENOMEM;
13463f2dd94aSFrançois Tigeot 		goto out_ctx;
13473f2dd94aSFrançois Tigeot 	}
13483f2dd94aSFrançois Tigeot 
13493f2dd94aSFrançois Tigeot 	state->acquire_ctx = &ctx;
13503f2dd94aSFrançois Tigeot retry:
13513f2dd94aSFrançois Tigeot 	for (i = 0; i < fb_helper->crtc_count; i++) {
13523f2dd94aSFrançois Tigeot 		crtc = fb_helper->crtc_info[i].mode_set.crtc;
13533f2dd94aSFrançois Tigeot 
13543f2dd94aSFrançois Tigeot 		if (!gamma_lut)
13553f2dd94aSFrançois Tigeot 			gamma_lut = setcmap_new_gamma_lut(crtc, cmap);
13563f2dd94aSFrançois Tigeot 		if (IS_ERR(gamma_lut)) {
13573f2dd94aSFrançois Tigeot 			ret = PTR_ERR(gamma_lut);
13583f2dd94aSFrançois Tigeot 			gamma_lut = NULL;
13593f2dd94aSFrançois Tigeot 			goto out_state;
13603f2dd94aSFrançois Tigeot 		}
13613f2dd94aSFrançois Tigeot 
13623f2dd94aSFrançois Tigeot 		crtc_state = drm_atomic_get_crtc_state(state, crtc);
13633f2dd94aSFrançois Tigeot 		if (IS_ERR(crtc_state)) {
13643f2dd94aSFrançois Tigeot 			ret = PTR_ERR(crtc_state);
13653f2dd94aSFrançois Tigeot 			goto out_state;
13663f2dd94aSFrançois Tigeot 		}
13673f2dd94aSFrançois Tigeot 
13683f2dd94aSFrançois Tigeot 		replaced  = drm_property_replace_blob(&crtc_state->degamma_lut,
13693f2dd94aSFrançois Tigeot 						      NULL);
13703f2dd94aSFrançois Tigeot 		replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
13713f2dd94aSFrançois Tigeot 		replaced |= drm_property_replace_blob(&crtc_state->gamma_lut,
13723f2dd94aSFrançois Tigeot 						      gamma_lut);
13733f2dd94aSFrançois Tigeot 		crtc_state->color_mgmt_changed |= replaced;
13743f2dd94aSFrançois Tigeot 	}
13753f2dd94aSFrançois Tigeot 
13763f2dd94aSFrançois Tigeot 	ret = drm_atomic_commit(state);
13773f2dd94aSFrançois Tigeot 	if (ret)
13783f2dd94aSFrançois Tigeot 		goto out_state;
13793f2dd94aSFrançois Tigeot 
13803f2dd94aSFrançois Tigeot 	for (i = 0; i < fb_helper->crtc_count; i++) {
13813f2dd94aSFrançois Tigeot 		crtc = fb_helper->crtc_info[i].mode_set.crtc;
13823f2dd94aSFrançois Tigeot 
13833f2dd94aSFrançois Tigeot 		r = crtc->gamma_store;
13843f2dd94aSFrançois Tigeot 		g = r + crtc->gamma_size;
13853f2dd94aSFrançois Tigeot 		b = g + crtc->gamma_size;
13863f2dd94aSFrançois Tigeot 
13873f2dd94aSFrançois Tigeot 		memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
13883f2dd94aSFrançois Tigeot 		memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
13893f2dd94aSFrançois Tigeot 		memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
13903f2dd94aSFrançois Tigeot 	}
13913f2dd94aSFrançois Tigeot 
13923f2dd94aSFrançois Tigeot out_state:
13933f2dd94aSFrançois Tigeot 	if (ret == -EDEADLK)
13943f2dd94aSFrançois Tigeot 		goto backoff;
13953f2dd94aSFrançois Tigeot 
13963f2dd94aSFrançois Tigeot 	drm_property_blob_put(gamma_lut);
13973f2dd94aSFrançois Tigeot 	drm_atomic_state_put(state);
13983f2dd94aSFrançois Tigeot out_ctx:
13993f2dd94aSFrançois Tigeot 	drm_modeset_drop_locks(&ctx);
14003f2dd94aSFrançois Tigeot 	drm_modeset_acquire_fini(&ctx);
14013f2dd94aSFrançois Tigeot 
14023f2dd94aSFrançois Tigeot 	return ret;
14033f2dd94aSFrançois Tigeot 
14043f2dd94aSFrançois Tigeot backoff:
14053f2dd94aSFrançois Tigeot 	drm_atomic_state_clear(state);
14063f2dd94aSFrançois Tigeot 	drm_modeset_backoff(&ctx);
14073f2dd94aSFrançois Tigeot 	goto retry;
14085718399fSFrançois Tigeot }
1409a85cb24fSFrançois Tigeot #endif
14105718399fSFrançois Tigeot 
14119edbd4a0SFrançois Tigeot /**
1412a85cb24fSFrançois Tigeot  * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap
14139edbd4a0SFrançois Tigeot  * @cmap: cmap to set
14149edbd4a0SFrançois Tigeot  * @info: fbdev registered by the helper
14159edbd4a0SFrançois Tigeot  */
drm_fb_helper_setcmap(struct fb_cmap * cmap,struct fb_info * info)14165718399fSFrançois Tigeot int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
14175718399fSFrançois Tigeot {
1418a85cb24fSFrançois Tigeot #if 0
14195718399fSFrançois Tigeot 	struct drm_fb_helper *fb_helper = info->par;
14203f2dd94aSFrançois Tigeot 	int ret;
14215718399fSFrançois Tigeot 
1422a05eeebfSFrançois Tigeot 	if (oops_in_progress)
14232c9916cdSFrançois Tigeot 		return -EBUSY;
1424a05eeebfSFrançois Tigeot 
14253f2dd94aSFrançois Tigeot 	mutex_lock(&fb_helper->lock);
14263f2dd94aSFrançois Tigeot 
14279edbd4a0SFrançois Tigeot 	if (!drm_fb_helper_is_bound(fb_helper)) {
14283f2dd94aSFrançois Tigeot 		ret = -EBUSY;
14299edbd4a0SFrançois Tigeot 		goto out;
14305718399fSFrançois Tigeot 	}
14313f2dd94aSFrançois Tigeot 
14323f2dd94aSFrançois Tigeot 	if (info->fix.visual == FB_VISUAL_TRUECOLOR)
14333f2dd94aSFrançois Tigeot 		ret = setcmap_pseudo_palette(cmap, info);
14343f2dd94aSFrançois Tigeot 	else if (drm_drv_uses_atomic_modeset(fb_helper->dev))
14353f2dd94aSFrançois Tigeot 		ret = setcmap_atomic(cmap, info);
14363f2dd94aSFrançois Tigeot 	else
14373f2dd94aSFrançois Tigeot 		ret = setcmap_legacy(cmap, info);
14383f2dd94aSFrançois Tigeot 
14399edbd4a0SFrançois Tigeot out:
14403f2dd94aSFrançois Tigeot 	mutex_unlock(&fb_helper->lock);
14413f2dd94aSFrançois Tigeot 
14423f2dd94aSFrançois Tigeot 	return ret;
1443a85cb24fSFrançois Tigeot #endif
1444a85cb24fSFrançois Tigeot 	return 0;
14455718399fSFrançois Tigeot }
14469edbd4a0SFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_setcmap);
14475718399fSFrançois Tigeot 
14489edbd4a0SFrançois Tigeot /**
1449a85cb24fSFrançois Tigeot  * drm_fb_helper_ioctl - legacy ioctl implementation
1450a85cb24fSFrançois Tigeot  * @info: fbdev registered by the helper
1451a85cb24fSFrançois Tigeot  * @cmd: ioctl command
1452a85cb24fSFrançois Tigeot  * @arg: ioctl argument
1453a85cb24fSFrançois Tigeot  *
1454a85cb24fSFrançois Tigeot  * A helper to implement the standard fbdev ioctl. Only
1455a85cb24fSFrançois Tigeot  * FBIO_WAITFORVSYNC is implemented for now.
1456a85cb24fSFrançois Tigeot  */
drm_fb_helper_ioctl(struct fb_info * info,unsigned int cmd,unsigned long arg)1457a85cb24fSFrançois Tigeot int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd,
1458a85cb24fSFrançois Tigeot 			unsigned long arg)
1459a85cb24fSFrançois Tigeot {
1460a85cb24fSFrançois Tigeot 	struct drm_fb_helper *fb_helper = info->par;
1461a85cb24fSFrançois Tigeot #if 0
1462a85cb24fSFrançois Tigeot 	struct drm_mode_set *mode_set;
1463a85cb24fSFrançois Tigeot 	struct drm_crtc *crtc;
1464a85cb24fSFrançois Tigeot #endif
1465a85cb24fSFrançois Tigeot 	int ret = 0;
1466a85cb24fSFrançois Tigeot 
14673f2dd94aSFrançois Tigeot 	mutex_lock(&fb_helper->lock);
1468a85cb24fSFrançois Tigeot 	if (!drm_fb_helper_is_bound(fb_helper)) {
1469a85cb24fSFrançois Tigeot 		ret = -EBUSY;
1470a85cb24fSFrançois Tigeot 		goto unlock;
1471a85cb24fSFrançois Tigeot 	}
1472a85cb24fSFrançois Tigeot 
1473a85cb24fSFrançois Tigeot 	switch (cmd) {
1474a85cb24fSFrançois Tigeot #if 0
1475a85cb24fSFrançois Tigeot 	case FBIO_WAITFORVSYNC:
1476a85cb24fSFrançois Tigeot 		/*
1477a85cb24fSFrançois Tigeot 		 * Only consider the first CRTC.
1478a85cb24fSFrançois Tigeot 		 *
1479a85cb24fSFrançois Tigeot 		 * This ioctl is supposed to take the CRTC number as
1480a85cb24fSFrançois Tigeot 		 * an argument, but in fbdev times, what that number
1481a85cb24fSFrançois Tigeot 		 * was supposed to be was quite unclear, different
1482a85cb24fSFrançois Tigeot 		 * drivers were passing that argument differently
1483a85cb24fSFrançois Tigeot 		 * (some by reference, some by value), and most of the
1484a85cb24fSFrançois Tigeot 		 * userspace applications were just hardcoding 0 as an
1485a85cb24fSFrançois Tigeot 		 * argument.
1486a85cb24fSFrançois Tigeot 		 *
1487a85cb24fSFrançois Tigeot 		 * The first CRTC should be the integrated panel on
1488a85cb24fSFrançois Tigeot 		 * most drivers, so this is the best choice we can
1489a85cb24fSFrançois Tigeot 		 * make. If we're not smart enough here, one should
1490a85cb24fSFrançois Tigeot 		 * just consider switch the userspace to KMS.
1491a85cb24fSFrançois Tigeot 		 */
1492a85cb24fSFrançois Tigeot 		mode_set = &fb_helper->crtc_info[0].mode_set;
1493a85cb24fSFrançois Tigeot 		crtc = mode_set->crtc;
1494a85cb24fSFrançois Tigeot 
1495a85cb24fSFrançois Tigeot 		/*
1496a85cb24fSFrançois Tigeot 		 * Only wait for a vblank event if the CRTC is
1497a85cb24fSFrançois Tigeot 		 * enabled, otherwise just don't do anythintg,
1498a85cb24fSFrançois Tigeot 		 * not even report an error.
1499a85cb24fSFrançois Tigeot 		 */
1500a85cb24fSFrançois Tigeot 		ret = drm_crtc_vblank_get(crtc);
1501a85cb24fSFrançois Tigeot 		if (!ret) {
1502a85cb24fSFrançois Tigeot 			drm_crtc_wait_one_vblank(crtc);
1503a85cb24fSFrançois Tigeot 			drm_crtc_vblank_put(crtc);
1504a85cb24fSFrançois Tigeot 		}
1505a85cb24fSFrançois Tigeot 
1506a85cb24fSFrançois Tigeot 		ret = 0;
1507a85cb24fSFrançois Tigeot 		goto unlock;
1508a85cb24fSFrançois Tigeot #endif
1509a85cb24fSFrançois Tigeot 	default:
1510a85cb24fSFrançois Tigeot 		ret = -ENOTTY;
1511a85cb24fSFrançois Tigeot 	}
1512a85cb24fSFrançois Tigeot 
1513a85cb24fSFrançois Tigeot unlock:
15143f2dd94aSFrançois Tigeot 	mutex_unlock(&fb_helper->lock);
1515a85cb24fSFrançois Tigeot 	return ret;
1516a85cb24fSFrançois Tigeot }
1517a85cb24fSFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_ioctl);
1518a85cb24fSFrançois Tigeot 
1519a85cb24fSFrançois Tigeot /**
1520a85cb24fSFrançois Tigeot  * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var
15219edbd4a0SFrançois Tigeot  * @var: screeninfo to check
15229edbd4a0SFrançois Tigeot  * @info: fbdev registered by the helper
15239edbd4a0SFrançois Tigeot  */
drm_fb_helper_check_var(struct fb_var_screeninfo * var,struct fb_info * info)15245718399fSFrançois Tigeot int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
15255718399fSFrançois Tigeot 			    struct fb_info *info)
15265718399fSFrançois Tigeot {
1527a85cb24fSFrançois Tigeot #if 0
15285718399fSFrançois Tigeot 	struct drm_fb_helper *fb_helper = info->par;
15295718399fSFrançois Tigeot 	struct drm_framebuffer *fb = fb_helper->fb;
15305718399fSFrançois Tigeot 	int depth;
15315718399fSFrançois Tigeot 
15325718399fSFrançois Tigeot 	if (var->pixclock != 0 || in_dbg_master())
15335718399fSFrançois Tigeot 		return -EINVAL;
15345718399fSFrançois Tigeot 
15354be47400SFrançois Tigeot 	/*
15364be47400SFrançois Tigeot 	 * Changes struct fb_var_screeninfo are currently not pushed back
15374be47400SFrançois Tigeot 	 * to KMS, hence fail if different settings are requested.
15384be47400SFrançois Tigeot 	 */
1539a85cb24fSFrançois Tigeot 	if (var->bits_per_pixel != fb->format->cpp[0] * 8 ||
15405718399fSFrançois Tigeot 	    var->xres > fb->width || var->yres > fb->height ||
15415718399fSFrançois Tigeot 	    var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
15424be47400SFrançois Tigeot 		DRM_DEBUG("fb requested width/height/bpp can't fit in current fb "
15435718399fSFrançois Tigeot 			  "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
15445718399fSFrançois Tigeot 			  var->xres, var->yres, var->bits_per_pixel,
15455718399fSFrançois Tigeot 			  var->xres_virtual, var->yres_virtual,
1546a85cb24fSFrançois Tigeot 			  fb->width, fb->height, fb->format->cpp[0] * 8);
15475718399fSFrançois Tigeot 		return -EINVAL;
15485718399fSFrançois Tigeot 	}
15495718399fSFrançois Tigeot 
15505718399fSFrançois Tigeot 	switch (var->bits_per_pixel) {
15515718399fSFrançois Tigeot 	case 16:
15525718399fSFrançois Tigeot 		depth = (var->green.length == 6) ? 16 : 15;
15535718399fSFrançois Tigeot 		break;
15545718399fSFrançois Tigeot 	case 32:
15555718399fSFrançois Tigeot 		depth = (var->transp.length > 0) ? 32 : 24;
15565718399fSFrançois Tigeot 		break;
15575718399fSFrançois Tigeot 	default:
15585718399fSFrançois Tigeot 		depth = var->bits_per_pixel;
15595718399fSFrançois Tigeot 		break;
15605718399fSFrançois Tigeot 	}
15615718399fSFrançois Tigeot 
15625718399fSFrançois Tigeot 	switch (depth) {
15635718399fSFrançois Tigeot 	case 8:
15645718399fSFrançois Tigeot 		var->red.offset = 0;
15655718399fSFrançois Tigeot 		var->green.offset = 0;
15665718399fSFrançois Tigeot 		var->blue.offset = 0;
15675718399fSFrançois Tigeot 		var->red.length = 8;
15685718399fSFrançois Tigeot 		var->green.length = 8;
15695718399fSFrançois Tigeot 		var->blue.length = 8;
15705718399fSFrançois Tigeot 		var->transp.length = 0;
15715718399fSFrançois Tigeot 		var->transp.offset = 0;
15725718399fSFrançois Tigeot 		break;
15735718399fSFrançois Tigeot 	case 15:
15745718399fSFrançois Tigeot 		var->red.offset = 10;
15755718399fSFrançois Tigeot 		var->green.offset = 5;
15765718399fSFrançois Tigeot 		var->blue.offset = 0;
15775718399fSFrançois Tigeot 		var->red.length = 5;
15785718399fSFrançois Tigeot 		var->green.length = 5;
15795718399fSFrançois Tigeot 		var->blue.length = 5;
15805718399fSFrançois Tigeot 		var->transp.length = 1;
15815718399fSFrançois Tigeot 		var->transp.offset = 15;
15825718399fSFrançois Tigeot 		break;
15835718399fSFrançois Tigeot 	case 16:
15845718399fSFrançois Tigeot 		var->red.offset = 11;
15855718399fSFrançois Tigeot 		var->green.offset = 5;
15865718399fSFrançois Tigeot 		var->blue.offset = 0;
15875718399fSFrançois Tigeot 		var->red.length = 5;
15885718399fSFrançois Tigeot 		var->green.length = 6;
15895718399fSFrançois Tigeot 		var->blue.length = 5;
15905718399fSFrançois Tigeot 		var->transp.length = 0;
15915718399fSFrançois Tigeot 		var->transp.offset = 0;
15925718399fSFrançois Tigeot 		break;
15935718399fSFrançois Tigeot 	case 24:
15945718399fSFrançois Tigeot 		var->red.offset = 16;
15955718399fSFrançois Tigeot 		var->green.offset = 8;
15965718399fSFrançois Tigeot 		var->blue.offset = 0;
15975718399fSFrançois Tigeot 		var->red.length = 8;
15985718399fSFrançois Tigeot 		var->green.length = 8;
15995718399fSFrançois Tigeot 		var->blue.length = 8;
16005718399fSFrançois Tigeot 		var->transp.length = 0;
16015718399fSFrançois Tigeot 		var->transp.offset = 0;
16025718399fSFrançois Tigeot 		break;
16035718399fSFrançois Tigeot 	case 32:
16045718399fSFrançois Tigeot 		var->red.offset = 16;
16055718399fSFrançois Tigeot 		var->green.offset = 8;
16065718399fSFrançois Tigeot 		var->blue.offset = 0;
16075718399fSFrançois Tigeot 		var->red.length = 8;
16085718399fSFrançois Tigeot 		var->green.length = 8;
16095718399fSFrançois Tigeot 		var->blue.length = 8;
16105718399fSFrançois Tigeot 		var->transp.length = 8;
16115718399fSFrançois Tigeot 		var->transp.offset = 24;
16125718399fSFrançois Tigeot 		break;
16135718399fSFrançois Tigeot 	default:
16145718399fSFrançois Tigeot 		return -EINVAL;
16155718399fSFrançois Tigeot 	}
1616a85cb24fSFrançois Tigeot #endif
16175718399fSFrançois Tigeot 	return 0;
16185718399fSFrançois Tigeot }
16199edbd4a0SFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_check_var);
16205718399fSFrançois Tigeot 
16219edbd4a0SFrançois Tigeot /**
1622a85cb24fSFrançois Tigeot  * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
16239edbd4a0SFrançois Tigeot  * @info: fbdev registered by the helper
16249edbd4a0SFrançois Tigeot  *
16259edbd4a0SFrançois Tigeot  * This will let fbcon do the mode init and is called at initialization time by
16269edbd4a0SFrançois Tigeot  * the fbdev core when registering the driver, and later on through the hotplug
16279edbd4a0SFrançois Tigeot  * callback.
16289edbd4a0SFrançois Tigeot  */
drm_fb_helper_set_par(struct fb_info * info)16295718399fSFrançois Tigeot int drm_fb_helper_set_par(struct fb_info *info)
16305718399fSFrançois Tigeot {
16315718399fSFrançois Tigeot 	struct drm_fb_helper *fb_helper = info->par;
1632ba409a88SImre Vadász #if 0
16335718399fSFrançois Tigeot 	struct fb_var_screeninfo *var = &info->var;
1634ba409a88SImre Vadász #endif
16355718399fSFrançois Tigeot 
1636a05eeebfSFrançois Tigeot 	if (oops_in_progress)
1637a05eeebfSFrançois Tigeot 		return -EBUSY;
1638a05eeebfSFrançois Tigeot 
1639ba409a88SImre Vadász #if 0
16405718399fSFrançois Tigeot 	if (var->pixclock != 0) {
16415718399fSFrançois Tigeot 		DRM_ERROR("PIXEL CLOCK SET\n");
16425718399fSFrançois Tigeot 		return -EINVAL;
16435718399fSFrançois Tigeot 	}
1644ba409a88SImre Vadász #endif
16455718399fSFrançois Tigeot 
1646ba55f2f5SFrançois Tigeot 	drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
16475718399fSFrançois Tigeot 
16485718399fSFrançois Tigeot 	return 0;
16495718399fSFrançois Tigeot }
16509edbd4a0SFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_set_par);
16515718399fSFrançois Tigeot 
1652ba409a88SImre Vadász #if 0
16533f2dd94aSFrançois Tigeot static void pan_set(struct drm_fb_helper *fb_helper, int x, int y)
1654352ff8bdSFrançois Tigeot {
16553f2dd94aSFrançois Tigeot 	int i;
1656352ff8bdSFrançois Tigeot 
1657352ff8bdSFrançois Tigeot 	for (i = 0; i < fb_helper->crtc_count; i++) {
1658352ff8bdSFrançois Tigeot 		struct drm_mode_set *mode_set;
1659352ff8bdSFrançois Tigeot 
1660352ff8bdSFrançois Tigeot 		mode_set = &fb_helper->crtc_info[i].mode_set;
1661352ff8bdSFrançois Tigeot 
16623f2dd94aSFrançois Tigeot 		mode_set->x = x;
16633f2dd94aSFrançois Tigeot 		mode_set->y = y;
16643f2dd94aSFrançois Tigeot 	}
1665352ff8bdSFrançois Tigeot }
1666352ff8bdSFrançois Tigeot 
16673f2dd94aSFrançois Tigeot static int pan_display_atomic(struct fb_var_screeninfo *var,
16683f2dd94aSFrançois Tigeot 			      struct fb_info *info)
16693f2dd94aSFrançois Tigeot {
16703f2dd94aSFrançois Tigeot 	struct drm_fb_helper *fb_helper = info->par;
16713f2dd94aSFrançois Tigeot 	int ret;
1672352ff8bdSFrançois Tigeot 
16733f2dd94aSFrançois Tigeot 	pan_set(fb_helper, var->xoffset, var->yoffset);
16743f2dd94aSFrançois Tigeot 
16753f2dd94aSFrançois Tigeot 	ret = restore_fbdev_mode_atomic(fb_helper, true);
16763f2dd94aSFrançois Tigeot 	if (!ret) {
1677352ff8bdSFrançois Tigeot 		info->var.xoffset = var->xoffset;
1678352ff8bdSFrançois Tigeot 		info->var.yoffset = var->yoffset;
16793f2dd94aSFrançois Tigeot 	} else
16803f2dd94aSFrançois Tigeot 		pan_set(fb_helper, info->var.xoffset, info->var.yoffset);
1681352ff8bdSFrançois Tigeot 
1682352ff8bdSFrançois Tigeot 	return ret;
1683352ff8bdSFrançois Tigeot }
1684352ff8bdSFrançois Tigeot 
1685a85cb24fSFrançois Tigeot static int pan_display_legacy(struct fb_var_screeninfo *var,
16865718399fSFrançois Tigeot 			      struct fb_info *info)
16875718399fSFrançois Tigeot {
16885718399fSFrançois Tigeot 	struct drm_fb_helper *fb_helper = info->par;
16895718399fSFrançois Tigeot 	struct drm_mode_set *modeset;
16905718399fSFrançois Tigeot 	int ret = 0;
16915718399fSFrançois Tigeot 	int i;
16925718399fSFrançois Tigeot 
16933f2dd94aSFrançois Tigeot 	drm_modeset_lock_all(fb_helper->dev);
16949edbd4a0SFrançois Tigeot 	for (i = 0; i < fb_helper->crtc_count; i++) {
16955718399fSFrançois Tigeot 		modeset = &fb_helper->crtc_info[i].mode_set;
16965718399fSFrançois Tigeot 
16975718399fSFrançois Tigeot 		modeset->x = var->xoffset;
16985718399fSFrançois Tigeot 		modeset->y = var->yoffset;
16995718399fSFrançois Tigeot 
17005718399fSFrançois Tigeot 		if (modeset->num_connectors) {
17019edbd4a0SFrançois Tigeot 			ret = drm_mode_set_config_internal(modeset);
17025718399fSFrançois Tigeot 			if (!ret) {
17035718399fSFrançois Tigeot 				info->var.xoffset = var->xoffset;
17045718399fSFrançois Tigeot 				info->var.yoffset = var->yoffset;
17055718399fSFrançois Tigeot 			}
17065718399fSFrançois Tigeot 		}
17075718399fSFrançois Tigeot 	}
17083f2dd94aSFrançois Tigeot 	drm_modeset_unlock_all(fb_helper->dev);
1709a85cb24fSFrançois Tigeot 
17105718399fSFrançois Tigeot 	return ret;
17115718399fSFrançois Tigeot }
17125718399fSFrançois Tigeot #endif
17135718399fSFrançois Tigeot 
1714a85cb24fSFrançois Tigeot /**
1715a85cb24fSFrançois Tigeot  * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
1716a85cb24fSFrançois Tigeot  * @var: updated screen information
1717a85cb24fSFrançois Tigeot  * @info: fbdev registered by the helper
1718a85cb24fSFrançois Tigeot  */
drm_fb_helper_pan_display(struct fb_var_screeninfo * var,struct fb_info * info)1719a85cb24fSFrançois Tigeot int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1720a85cb24fSFrançois Tigeot 			      struct fb_info *info)
1721a85cb24fSFrançois Tigeot {
1722a85cb24fSFrançois Tigeot #if 0
1723a85cb24fSFrançois Tigeot 	struct drm_fb_helper *fb_helper = info->par;
1724a85cb24fSFrançois Tigeot 	struct drm_device *dev = fb_helper->dev;
1725a85cb24fSFrançois Tigeot 	int ret;
1726a85cb24fSFrançois Tigeot 
1727a85cb24fSFrançois Tigeot 	if (oops_in_progress)
1728a85cb24fSFrançois Tigeot 		return -EBUSY;
1729a85cb24fSFrançois Tigeot 
17303f2dd94aSFrançois Tigeot 	mutex_lock(&fb_helper->lock);
1731a85cb24fSFrançois Tigeot 	if (!drm_fb_helper_is_bound(fb_helper)) {
17323f2dd94aSFrançois Tigeot 		mutex_unlock(&fb_helper->lock);
1733a85cb24fSFrançois Tigeot 		return -EBUSY;
1734a85cb24fSFrançois Tigeot 	}
1735a85cb24fSFrançois Tigeot 
1736a85cb24fSFrançois Tigeot 	if (drm_drv_uses_atomic_modeset(dev))
1737a85cb24fSFrançois Tigeot 		ret = pan_display_atomic(var, info);
1738a85cb24fSFrançois Tigeot 	else
1739a85cb24fSFrançois Tigeot 		ret = pan_display_legacy(var, info);
17403f2dd94aSFrançois Tigeot 	mutex_unlock(&fb_helper->lock);
1741a85cb24fSFrançois Tigeot 
1742a85cb24fSFrançois Tigeot 	return ret;
1743a85cb24fSFrançois Tigeot #endif
1744a85cb24fSFrançois Tigeot 	return 0;
1745a85cb24fSFrançois Tigeot }
1746a85cb24fSFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_pan_display);
1747a85cb24fSFrançois Tigeot 
17489edbd4a0SFrançois Tigeot /*
17499edbd4a0SFrançois Tigeot  * Allocates the backing storage and sets up the fbdev info structure through
17503f2dd94aSFrançois Tigeot  * the ->fb_probe callback.
17519edbd4a0SFrançois Tigeot  */
drm_fb_helper_single_fb_probe(struct drm_fb_helper * fb_helper,int preferred_bpp)17529edbd4a0SFrançois Tigeot static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
17535718399fSFrançois Tigeot 					 int preferred_bpp)
17545718399fSFrançois Tigeot {
17559edbd4a0SFrançois Tigeot 	int ret = 0;
17565718399fSFrançois Tigeot 	int crtc_count = 0;
17575718399fSFrançois Tigeot 	int i;
17585718399fSFrançois Tigeot 	struct drm_fb_helper_surface_size sizes;
17595718399fSFrançois Tigeot 	int gamma_size = 0;
176078973132SSergey Zigachev #if 0
1761a85cb24fSFrançois Tigeot 	int kms_console = 1;
1762a85cb24fSFrançois Tigeot #endif
17635718399fSFrançois Tigeot 
17645718399fSFrançois Tigeot 	memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
17655718399fSFrançois Tigeot 	sizes.surface_depth = 24;
17665718399fSFrançois Tigeot 	sizes.surface_bpp = 32;
1767a85cb24fSFrançois Tigeot 	sizes.fb_width = (u32)-1;
1768a85cb24fSFrançois Tigeot 	sizes.fb_height = (u32)-1;
17695718399fSFrançois Tigeot 
1770a85cb24fSFrançois Tigeot 	/* if driver picks 8 or 16 by default use that for both depth/bpp */
17714a968d2aSFrançois Tigeot 	if (preferred_bpp != sizes.surface_bpp)
17725718399fSFrançois Tigeot 		sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
17734a968d2aSFrançois Tigeot 
17745718399fSFrançois Tigeot 	/* first up get a count of crtcs now in use and new min/maxes width/heights */
17754be47400SFrançois Tigeot 	drm_fb_helper_for_each_connector(fb_helper, i) {
17765718399fSFrançois Tigeot 		struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
17774a968d2aSFrançois Tigeot 		struct drm_cmdline_mode *cmdline_mode;
17785718399fSFrançois Tigeot 
17791b13d190SFrançois Tigeot 		cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
17805718399fSFrançois Tigeot 
17815718399fSFrançois Tigeot 		if (cmdline_mode->bpp_specified) {
17825718399fSFrançois Tigeot 			switch (cmdline_mode->bpp) {
17835718399fSFrançois Tigeot 			case 8:
17845718399fSFrançois Tigeot 				sizes.surface_depth = sizes.surface_bpp = 8;
17855718399fSFrançois Tigeot 				break;
17865718399fSFrançois Tigeot 			case 15:
17875718399fSFrançois Tigeot 				sizes.surface_depth = 15;
17885718399fSFrançois Tigeot 				sizes.surface_bpp = 16;
17895718399fSFrançois Tigeot 				break;
17905718399fSFrançois Tigeot 			case 16:
17915718399fSFrançois Tigeot 				sizes.surface_depth = sizes.surface_bpp = 16;
17925718399fSFrançois Tigeot 				break;
17935718399fSFrançois Tigeot 			case 24:
17945718399fSFrançois Tigeot 				sizes.surface_depth = sizes.surface_bpp = 24;
17955718399fSFrançois Tigeot 				break;
17965718399fSFrançois Tigeot 			case 32:
17975718399fSFrançois Tigeot 				sizes.surface_depth = 24;
17985718399fSFrançois Tigeot 				sizes.surface_bpp = 32;
17995718399fSFrançois Tigeot 				break;
18005718399fSFrançois Tigeot 			}
18015718399fSFrançois Tigeot 			break;
18025718399fSFrançois Tigeot 		}
18035718399fSFrançois Tigeot 	}
18045718399fSFrançois Tigeot 
18055718399fSFrançois Tigeot 	crtc_count = 0;
18065718399fSFrançois Tigeot 	for (i = 0; i < fb_helper->crtc_count; i++) {
18075718399fSFrançois Tigeot 		struct drm_display_mode *desired_mode;
1808477eb7f9SFrançois Tigeot 		struct drm_mode_set *mode_set;
1809477eb7f9SFrançois Tigeot 		int x, y, j;
1810477eb7f9SFrançois Tigeot 		/* in case of tile group, are we the last tile vert or horiz?
1811477eb7f9SFrançois Tigeot 		 * If no tile group you are always the last one both vertically
1812477eb7f9SFrançois Tigeot 		 * and horizontally
1813477eb7f9SFrançois Tigeot 		 */
1814477eb7f9SFrançois Tigeot 		bool lastv = true, lasth = true;
1815477eb7f9SFrançois Tigeot 
18165718399fSFrançois Tigeot 		desired_mode = fb_helper->crtc_info[i].desired_mode;
1817477eb7f9SFrançois Tigeot 		mode_set = &fb_helper->crtc_info[i].mode_set;
1818477eb7f9SFrançois Tigeot 
1819477eb7f9SFrançois Tigeot 		if (!desired_mode)
1820477eb7f9SFrançois Tigeot 			continue;
1821477eb7f9SFrançois Tigeot 
1822477eb7f9SFrançois Tigeot 		crtc_count++;
1823477eb7f9SFrançois Tigeot 
18242c9916cdSFrançois Tigeot 		x = fb_helper->crtc_info[i].x;
18252c9916cdSFrançois Tigeot 		y = fb_helper->crtc_info[i].y;
1826477eb7f9SFrançois Tigeot 
18275718399fSFrançois Tigeot 		if (gamma_size == 0)
18285718399fSFrançois Tigeot 			gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1829477eb7f9SFrançois Tigeot 
1830477eb7f9SFrançois Tigeot 		sizes.surface_width  = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
1831477eb7f9SFrançois Tigeot 		sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
1832477eb7f9SFrançois Tigeot 
1833477eb7f9SFrançois Tigeot 		for (j = 0; j < mode_set->num_connectors; j++) {
1834477eb7f9SFrançois Tigeot 			struct drm_connector *connector = mode_set->connectors[j];
1835a85cb24fSFrançois Tigeot 
1836477eb7f9SFrançois Tigeot 			if (connector->has_tile) {
1837477eb7f9SFrançois Tigeot 				lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
1838477eb7f9SFrançois Tigeot 				lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
1839477eb7f9SFrançois Tigeot 				/* cloning to multiple tiles is just crazy-talk, so: */
1840477eb7f9SFrançois Tigeot 				break;
18415718399fSFrançois Tigeot 			}
18425718399fSFrançois Tigeot 		}
18435718399fSFrançois Tigeot 
1844477eb7f9SFrançois Tigeot 		if (lasth)
1845477eb7f9SFrançois Tigeot 			sizes.fb_width  = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
1846477eb7f9SFrançois Tigeot 		if (lastv)
1847477eb7f9SFrançois Tigeot 			sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
1848477eb7f9SFrançois Tigeot 	}
1849477eb7f9SFrançois Tigeot 
18505718399fSFrançois Tigeot 	if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
18513f2dd94aSFrançois Tigeot 		DRM_INFO("Cannot find any crtc or sizes\n");
18523f2dd94aSFrançois Tigeot 
18533f2dd94aSFrançois Tigeot 		/* First time: disable all crtc's.. */
18543f2dd94aSFrançois Tigeot 		if (!fb_helper->deferred_setup && !READ_ONCE(fb_helper->dev->master))
18553f2dd94aSFrançois Tigeot 			restore_fbdev_mode(fb_helper);
18563f2dd94aSFrançois Tigeot 		return -EAGAIN;
18575718399fSFrançois Tigeot 	}
18585718399fSFrançois Tigeot 
1859a85cb24fSFrançois Tigeot 	/* Handle our overallocation */
1860a85cb24fSFrançois Tigeot 	sizes.surface_height *= drm_fbdev_overalloc;
1861a85cb24fSFrançois Tigeot 	sizes.surface_height /= 100;
1862a85cb24fSFrançois Tigeot 
18635718399fSFrançois Tigeot 	/* push down into drivers */
18649edbd4a0SFrançois Tigeot 	ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
18659edbd4a0SFrançois Tigeot 	if (ret < 0)
18669edbd4a0SFrançois Tigeot 		return ret;
18675718399fSFrançois Tigeot 
186878973132SSergey Zigachev #if 0
1869a85cb24fSFrançois Tigeot 	TUNABLE_INT_FETCH("kern.kms_console", &kms_console);
1870a85cb24fSFrançois Tigeot 	if (kms_console) {
1871a85cb24fSFrançois Tigeot 		if (register_framebuffer(fb_helper->fbdev) < 0)
1872a85cb24fSFrançois Tigeot 			return -EINVAL;
1873a85cb24fSFrançois Tigeot 	}
1874a85cb24fSFrançois Tigeot #endif
1875a85cb24fSFrançois Tigeot 
18765718399fSFrançois Tigeot 	return 0;
18775718399fSFrançois Tigeot }
18785718399fSFrançois Tigeot 
18795718399fSFrançois Tigeot #if 0
18809edbd4a0SFrançois Tigeot /**
18819edbd4a0SFrançois Tigeot  * drm_fb_helper_fill_fix - initializes fixed fbdev information
18829edbd4a0SFrançois Tigeot  * @info: fbdev registered by the helper
18839edbd4a0SFrançois Tigeot  * @pitch: desired pitch
18849edbd4a0SFrançois Tigeot  * @depth: desired depth
18859edbd4a0SFrançois Tigeot  *
18869edbd4a0SFrançois Tigeot  * Helper to fill in the fixed fbdev information useful for a non-accelerated
18879edbd4a0SFrançois Tigeot  * fbdev emulations. Drivers which support acceleration methods which impose
18889edbd4a0SFrançois Tigeot  * additional constraints need to set up their own limits.
18899edbd4a0SFrançois Tigeot  *
18909edbd4a0SFrançois Tigeot  * Drivers should call this (or their equivalent setup code) from their
1891a85cb24fSFrançois Tigeot  * &drm_fb_helper_funcs.fb_probe callback.
18929edbd4a0SFrançois Tigeot  */
18935718399fSFrançois Tigeot void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
18945718399fSFrançois Tigeot 			    uint32_t depth)
18955718399fSFrançois Tigeot {
18965718399fSFrançois Tigeot 	info->fix.type = FB_TYPE_PACKED_PIXELS;
18975718399fSFrançois Tigeot 	info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
18989edbd4a0SFrançois Tigeot 		FB_VISUAL_TRUECOLOR;
18995718399fSFrançois Tigeot 	info->fix.mmio_start = 0;
19005718399fSFrançois Tigeot 	info->fix.mmio_len = 0;
19015718399fSFrançois Tigeot 	info->fix.type_aux = 0;
19025718399fSFrançois Tigeot 	info->fix.xpanstep = 1; /* doing it in hw */
19035718399fSFrançois Tigeot 	info->fix.ypanstep = 1; /* doing it in hw */
19045718399fSFrançois Tigeot 	info->fix.ywrapstep = 0;
19055718399fSFrançois Tigeot 	info->fix.accel = FB_ACCEL_NONE;
19065718399fSFrançois Tigeot 
19075718399fSFrançois Tigeot 	info->fix.line_length = pitch;
19085718399fSFrançois Tigeot }
19099edbd4a0SFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_fill_fix);
19105718399fSFrançois Tigeot 
19119edbd4a0SFrançois Tigeot /**
19129edbd4a0SFrançois Tigeot  * drm_fb_helper_fill_var - initalizes variable fbdev information
19139edbd4a0SFrançois Tigeot  * @info: fbdev instance to set up
19149edbd4a0SFrançois Tigeot  * @fb_helper: fb helper instance to use as template
19159edbd4a0SFrançois Tigeot  * @fb_width: desired fb width
19169edbd4a0SFrançois Tigeot  * @fb_height: desired fb height
19179edbd4a0SFrançois Tigeot  *
19189edbd4a0SFrançois Tigeot  * Sets up the variable fbdev metainformation from the given fb helper instance
1919a85cb24fSFrançois Tigeot  * and the drm framebuffer allocated in &drm_fb_helper.fb.
19209edbd4a0SFrançois Tigeot  *
19219edbd4a0SFrançois Tigeot  * Drivers should call this (or their equivalent setup code) from their
1922a85cb24fSFrançois Tigeot  * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
1923a85cb24fSFrançois Tigeot  * backing storage framebuffer.
19249edbd4a0SFrançois Tigeot  */
19255718399fSFrançois Tigeot void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
19265718399fSFrançois Tigeot 			    uint32_t fb_width, uint32_t fb_height)
19275718399fSFrançois Tigeot {
19285718399fSFrançois Tigeot 	struct drm_framebuffer *fb = fb_helper->fb;
1929a85cb24fSFrançois Tigeot 
19305718399fSFrançois Tigeot 	info->pseudo_palette = fb_helper->pseudo_palette;
19315718399fSFrançois Tigeot 	info->var.xres_virtual = fb->width;
19325718399fSFrançois Tigeot 	info->var.yres_virtual = fb->height;
1933a85cb24fSFrançois Tigeot 	info->var.bits_per_pixel = fb->format->cpp[0] * 8;
19345718399fSFrançois Tigeot 	info->var.accel_flags = FB_ACCELF_TEXT;
19355718399fSFrançois Tigeot 	info->var.xoffset = 0;
19365718399fSFrançois Tigeot 	info->var.yoffset = 0;
19375718399fSFrançois Tigeot 	info->var.activate = FB_ACTIVATE_NOW;
19385718399fSFrançois Tigeot 
1939a85cb24fSFrançois Tigeot 	switch (fb->format->depth) {
19405718399fSFrançois Tigeot 	case 8:
19415718399fSFrançois Tigeot 		info->var.red.offset = 0;
19425718399fSFrançois Tigeot 		info->var.green.offset = 0;
19435718399fSFrançois Tigeot 		info->var.blue.offset = 0;
19445718399fSFrançois Tigeot 		info->var.red.length = 8; /* 8bit DAC */
19455718399fSFrançois Tigeot 		info->var.green.length = 8;
19465718399fSFrançois Tigeot 		info->var.blue.length = 8;
19475718399fSFrançois Tigeot 		info->var.transp.offset = 0;
19485718399fSFrançois Tigeot 		info->var.transp.length = 0;
19495718399fSFrançois Tigeot 		break;
19505718399fSFrançois Tigeot 	case 15:
19515718399fSFrançois Tigeot 		info->var.red.offset = 10;
19525718399fSFrançois Tigeot 		info->var.green.offset = 5;
19535718399fSFrançois Tigeot 		info->var.blue.offset = 0;
19545718399fSFrançois Tigeot 		info->var.red.length = 5;
19555718399fSFrançois Tigeot 		info->var.green.length = 5;
19565718399fSFrançois Tigeot 		info->var.blue.length = 5;
19575718399fSFrançois Tigeot 		info->var.transp.offset = 15;
19585718399fSFrançois Tigeot 		info->var.transp.length = 1;
19595718399fSFrançois Tigeot 		break;
19605718399fSFrançois Tigeot 	case 16:
19615718399fSFrançois Tigeot 		info->var.red.offset = 11;
19625718399fSFrançois Tigeot 		info->var.green.offset = 5;
19635718399fSFrançois Tigeot 		info->var.blue.offset = 0;
19645718399fSFrançois Tigeot 		info->var.red.length = 5;
19655718399fSFrançois Tigeot 		info->var.green.length = 6;
19665718399fSFrançois Tigeot 		info->var.blue.length = 5;
19675718399fSFrançois Tigeot 		info->var.transp.offset = 0;
19685718399fSFrançois Tigeot 		break;
19695718399fSFrançois Tigeot 	case 24:
19705718399fSFrançois Tigeot 		info->var.red.offset = 16;
19715718399fSFrançois Tigeot 		info->var.green.offset = 8;
19725718399fSFrançois Tigeot 		info->var.blue.offset = 0;
19735718399fSFrançois Tigeot 		info->var.red.length = 8;
19745718399fSFrançois Tigeot 		info->var.green.length = 8;
19755718399fSFrançois Tigeot 		info->var.blue.length = 8;
19765718399fSFrançois Tigeot 		info->var.transp.offset = 0;
19775718399fSFrançois Tigeot 		info->var.transp.length = 0;
19785718399fSFrançois Tigeot 		break;
19795718399fSFrançois Tigeot 	case 32:
19805718399fSFrançois Tigeot 		info->var.red.offset = 16;
19815718399fSFrançois Tigeot 		info->var.green.offset = 8;
19825718399fSFrançois Tigeot 		info->var.blue.offset = 0;
19835718399fSFrançois Tigeot 		info->var.red.length = 8;
19845718399fSFrançois Tigeot 		info->var.green.length = 8;
19855718399fSFrançois Tigeot 		info->var.blue.length = 8;
19865718399fSFrançois Tigeot 		info->var.transp.offset = 24;
19875718399fSFrançois Tigeot 		info->var.transp.length = 8;
19885718399fSFrançois Tigeot 		break;
19895718399fSFrançois Tigeot 	default:
19905718399fSFrançois Tigeot 		break;
19915718399fSFrançois Tigeot 	}
19925718399fSFrançois Tigeot 
19935718399fSFrançois Tigeot 	info->var.xres = fb_width;
19945718399fSFrançois Tigeot 	info->var.yres = fb_height;
19955718399fSFrançois Tigeot }
19969edbd4a0SFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_fill_var);
19975718399fSFrançois Tigeot #endif
19985718399fSFrançois Tigeot 
drm_fb_helper_probe_connector_modes(struct drm_fb_helper * fb_helper,uint32_t maxX,uint32_t maxY)19995718399fSFrançois Tigeot static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
20005718399fSFrançois Tigeot 						uint32_t maxX,
20015718399fSFrançois Tigeot 						uint32_t maxY)
20025718399fSFrançois Tigeot {
20035718399fSFrançois Tigeot 	struct drm_connector *connector;
20043f2dd94aSFrançois Tigeot 	int i, count = 0;
20055718399fSFrançois Tigeot 
20064be47400SFrançois Tigeot 	drm_fb_helper_for_each_connector(fb_helper, i) {
20075718399fSFrançois Tigeot 		connector = fb_helper->connector_info[i]->connector;
20085718399fSFrançois Tigeot 		count += connector->funcs->fill_modes(connector, maxX, maxY);
20095718399fSFrançois Tigeot 	}
20105718399fSFrançois Tigeot 
20115718399fSFrançois Tigeot 	return count;
20125718399fSFrançois Tigeot }
20135718399fSFrançois Tigeot 
drm_has_preferred_mode(struct drm_fb_helper_connector * fb_connector,int width,int height)2014ba55f2f5SFrançois Tigeot struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
20155718399fSFrançois Tigeot {
20165718399fSFrançois Tigeot 	struct drm_display_mode *mode;
20175718399fSFrançois Tigeot 
20185718399fSFrançois Tigeot 	list_for_each_entry(mode, &fb_connector->connector->modes, head) {
2019ba55f2f5SFrançois Tigeot 		if (mode->hdisplay > width ||
2020ba55f2f5SFrançois Tigeot 		    mode->vdisplay > height)
20215718399fSFrançois Tigeot 			continue;
20225718399fSFrançois Tigeot 		if (mode->type & DRM_MODE_TYPE_PREFERRED)
20235718399fSFrançois Tigeot 			return mode;
20245718399fSFrançois Tigeot 	}
20255718399fSFrançois Tigeot 	return NULL;
20265718399fSFrançois Tigeot }
2027ba55f2f5SFrançois Tigeot EXPORT_SYMBOL(drm_has_preferred_mode);
20285718399fSFrançois Tigeot 
drm_has_cmdline_mode(struct drm_fb_helper_connector * fb_connector)20295718399fSFrançois Tigeot static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
20305718399fSFrançois Tigeot {
20311b13d190SFrançois Tigeot 	return fb_connector->connector->cmdline_mode.specified;
20325718399fSFrançois Tigeot }
20335718399fSFrançois Tigeot 
drm_pick_cmdline_mode(struct drm_fb_helper_connector * fb_helper_conn)2034a85cb24fSFrançois Tigeot struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn)
20355718399fSFrançois Tigeot {
20365718399fSFrançois Tigeot 	struct drm_cmdline_mode *cmdline_mode;
2037477eb7f9SFrançois Tigeot 	struct drm_display_mode *mode;
2038ba55f2f5SFrançois Tigeot 	bool prefer_non_interlace;
20395718399fSFrançois Tigeot 
20401b13d190SFrançois Tigeot 	cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
20419edbd4a0SFrançois Tigeot 	if (cmdline_mode->specified == false)
2042477eb7f9SFrançois Tigeot 		return NULL;
20435718399fSFrançois Tigeot 
20445718399fSFrançois Tigeot 	/* attempt to find a matching mode in the list of modes
20455718399fSFrançois Tigeot 	 *  we have gotten so far, if not add a CVT mode that conforms
20465718399fSFrançois Tigeot 	 */
20475718399fSFrançois Tigeot 	if (cmdline_mode->rb || cmdline_mode->margins)
20485718399fSFrançois Tigeot 		goto create_mode;
20495718399fSFrançois Tigeot 
2050ba55f2f5SFrançois Tigeot 	prefer_non_interlace = !cmdline_mode->interlace;
2051ba55f2f5SFrançois Tigeot again:
20525718399fSFrançois Tigeot 	list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
20535718399fSFrançois Tigeot 		/* check width/height */
20545718399fSFrançois Tigeot 		if (mode->hdisplay != cmdline_mode->xres ||
20555718399fSFrançois Tigeot 		    mode->vdisplay != cmdline_mode->yres)
20565718399fSFrançois Tigeot 			continue;
20575718399fSFrançois Tigeot 
20585718399fSFrançois Tigeot 		if (cmdline_mode->refresh_specified) {
20595718399fSFrançois Tigeot 			if (mode->vrefresh != cmdline_mode->refresh)
20605718399fSFrançois Tigeot 				continue;
20615718399fSFrançois Tigeot 		}
20625718399fSFrançois Tigeot 
20635718399fSFrançois Tigeot 		if (cmdline_mode->interlace) {
20645718399fSFrançois Tigeot 			if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
20655718399fSFrançois Tigeot 				continue;
2066ba55f2f5SFrançois Tigeot 		} else if (prefer_non_interlace) {
2067ba55f2f5SFrançois Tigeot 			if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2068ba55f2f5SFrançois Tigeot 				continue;
20695718399fSFrançois Tigeot 		}
20705718399fSFrançois Tigeot 		return mode;
20715718399fSFrançois Tigeot 	}
20725718399fSFrançois Tigeot 
2073ba55f2f5SFrançois Tigeot 	if (prefer_non_interlace) {
2074ba55f2f5SFrançois Tigeot 		prefer_non_interlace = false;
2075ba55f2f5SFrançois Tigeot 		goto again;
2076ba55f2f5SFrançois Tigeot 	}
2077ba55f2f5SFrançois Tigeot 
20785718399fSFrançois Tigeot create_mode:
20799edbd4a0SFrançois Tigeot 	mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
20809edbd4a0SFrançois Tigeot 						 cmdline_mode);
20815718399fSFrançois Tigeot 	list_add(&mode->head, &fb_helper_conn->connector->modes);
20825718399fSFrançois Tigeot 	return mode;
20835718399fSFrançois Tigeot }
2084ba55f2f5SFrançois Tigeot EXPORT_SYMBOL(drm_pick_cmdline_mode);
20855718399fSFrançois Tigeot 
drm_connector_enabled(struct drm_connector * connector,bool strict)20865718399fSFrançois Tigeot static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
20875718399fSFrançois Tigeot {
20885718399fSFrançois Tigeot 	bool enable;
20895718399fSFrançois Tigeot 
20903f2dd94aSFrançois Tigeot 	if (connector->display_info.non_desktop)
20913f2dd94aSFrançois Tigeot 		return false;
20923f2dd94aSFrançois Tigeot 
20939edbd4a0SFrançois Tigeot 	if (strict)
20945718399fSFrançois Tigeot 		enable = connector->status == connector_status_connected;
20959edbd4a0SFrançois Tigeot 	else
20965718399fSFrançois Tigeot 		enable = connector->status != connector_status_disconnected;
20979edbd4a0SFrançois Tigeot 
20985718399fSFrançois Tigeot 	return enable;
20995718399fSFrançois Tigeot }
21005718399fSFrançois Tigeot 
drm_enable_connectors(struct drm_fb_helper * fb_helper,bool * enabled)21015718399fSFrançois Tigeot static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
21025718399fSFrançois Tigeot 				  bool *enabled)
21035718399fSFrançois Tigeot {
21045718399fSFrançois Tigeot 	bool any_enabled = false;
21055718399fSFrançois Tigeot 	struct drm_connector *connector;
21065718399fSFrançois Tigeot 	int i = 0;
21075718399fSFrançois Tigeot 
21084be47400SFrançois Tigeot 	drm_fb_helper_for_each_connector(fb_helper, i) {
21095718399fSFrançois Tigeot 		connector = fb_helper->connector_info[i]->connector;
21105718399fSFrançois Tigeot 		enabled[i] = drm_connector_enabled(connector, true);
21115718399fSFrançois Tigeot 		DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
21123f2dd94aSFrançois Tigeot 			      connector->display_info.non_desktop ? "non desktop" : enabled[i] ? "yes" : "no");
21133f2dd94aSFrançois Tigeot 
21145718399fSFrançois Tigeot 		any_enabled |= enabled[i];
21155718399fSFrançois Tigeot 	}
21165718399fSFrançois Tigeot 
21175718399fSFrançois Tigeot 	if (any_enabled)
21185718399fSFrançois Tigeot 		return;
21195718399fSFrançois Tigeot 
21204be47400SFrançois Tigeot 	drm_fb_helper_for_each_connector(fb_helper, i) {
21215718399fSFrançois Tigeot 		connector = fb_helper->connector_info[i]->connector;
21225718399fSFrançois Tigeot 		enabled[i] = drm_connector_enabled(connector, false);
21235718399fSFrançois Tigeot 	}
21245718399fSFrançois Tigeot }
21255718399fSFrançois Tigeot 
drm_target_cloned(struct drm_fb_helper * fb_helper,struct drm_display_mode ** modes,struct drm_fb_offset * offsets,bool * enabled,int width,int height)21265718399fSFrançois Tigeot static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
21275718399fSFrançois Tigeot 			      struct drm_display_mode **modes,
21282c9916cdSFrançois Tigeot 			      struct drm_fb_offset *offsets,
21295718399fSFrançois Tigeot 			      bool *enabled, int width, int height)
21305718399fSFrançois Tigeot {
21315718399fSFrançois Tigeot 	int count, i, j;
21325718399fSFrançois Tigeot 	bool can_clone = false;
21335718399fSFrançois Tigeot 	struct drm_fb_helper_connector *fb_helper_conn;
21345718399fSFrançois Tigeot 	struct drm_display_mode *dmt_mode, *mode;
21355718399fSFrançois Tigeot 
21365718399fSFrançois Tigeot 	/* only contemplate cloning in the single crtc case */
21375718399fSFrançois Tigeot 	if (fb_helper->crtc_count > 1)
21385718399fSFrançois Tigeot 		return false;
21395718399fSFrançois Tigeot 
21405718399fSFrançois Tigeot 	count = 0;
21414be47400SFrançois Tigeot 	drm_fb_helper_for_each_connector(fb_helper, i) {
21425718399fSFrançois Tigeot 		if (enabled[i])
21435718399fSFrançois Tigeot 			count++;
21445718399fSFrançois Tigeot 	}
21455718399fSFrançois Tigeot 
21465718399fSFrançois Tigeot 	/* only contemplate cloning if more than one connector is enabled */
21475718399fSFrançois Tigeot 	if (count <= 1)
21485718399fSFrançois Tigeot 		return false;
21495718399fSFrançois Tigeot 
21505718399fSFrançois Tigeot 	/* check the command line or if nothing common pick 1024x768 */
21515718399fSFrançois Tigeot 	can_clone = true;
21524be47400SFrançois Tigeot 	drm_fb_helper_for_each_connector(fb_helper, i) {
21535718399fSFrançois Tigeot 		if (!enabled[i])
21545718399fSFrançois Tigeot 			continue;
21555718399fSFrançois Tigeot 		fb_helper_conn = fb_helper->connector_info[i];
2156a85cb24fSFrançois Tigeot 		modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
21575718399fSFrançois Tigeot 		if (!modes[i]) {
21585718399fSFrançois Tigeot 			can_clone = false;
21595718399fSFrançois Tigeot 			break;
21605718399fSFrançois Tigeot 		}
21615718399fSFrançois Tigeot 		for (j = 0; j < i; j++) {
21625718399fSFrançois Tigeot 			if (!enabled[j])
21635718399fSFrançois Tigeot 				continue;
21645718399fSFrançois Tigeot 			if (!drm_mode_equal(modes[j], modes[i]))
21655718399fSFrançois Tigeot 				can_clone = false;
21665718399fSFrançois Tigeot 		}
21675718399fSFrançois Tigeot 	}
21685718399fSFrançois Tigeot 
21695718399fSFrançois Tigeot 	if (can_clone) {
21705718399fSFrançois Tigeot 		DRM_DEBUG_KMS("can clone using command line\n");
21715718399fSFrançois Tigeot 		return true;
21725718399fSFrançois Tigeot 	}
21735718399fSFrançois Tigeot 
21745718399fSFrançois Tigeot 	/* try and find a 1024x768 mode on each connector */
21755718399fSFrançois Tigeot 	can_clone = true;
2176ce3d36d7SFrançois Tigeot 	dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
21775718399fSFrançois Tigeot 
21784be47400SFrançois Tigeot 	drm_fb_helper_for_each_connector(fb_helper, i) {
21795718399fSFrançois Tigeot 		if (!enabled[i])
21805718399fSFrançois Tigeot 			continue;
21815718399fSFrançois Tigeot 
21825718399fSFrançois Tigeot 		fb_helper_conn = fb_helper->connector_info[i];
21835718399fSFrançois Tigeot 		list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
21845718399fSFrançois Tigeot 			if (drm_mode_equal(mode, dmt_mode))
21855718399fSFrançois Tigeot 				modes[i] = mode;
21865718399fSFrançois Tigeot 		}
21875718399fSFrançois Tigeot 		if (!modes[i])
21885718399fSFrançois Tigeot 			can_clone = false;
21895718399fSFrançois Tigeot 	}
21905718399fSFrançois Tigeot 
21915718399fSFrançois Tigeot 	if (can_clone) {
21925718399fSFrançois Tigeot 		DRM_DEBUG_KMS("can clone using 1024x768\n");
21935718399fSFrançois Tigeot 		return true;
21945718399fSFrançois Tigeot 	}
21955718399fSFrançois Tigeot 	DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
21965718399fSFrançois Tigeot 	return false;
21975718399fSFrançois Tigeot }
21985718399fSFrançois Tigeot 
drm_get_tile_offsets(struct drm_fb_helper * fb_helper,struct drm_display_mode ** modes,struct drm_fb_offset * offsets,int idx,int h_idx,int v_idx)21992c9916cdSFrançois Tigeot static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
22002c9916cdSFrançois Tigeot 				struct drm_display_mode **modes,
22012c9916cdSFrançois Tigeot 				struct drm_fb_offset *offsets,
22022c9916cdSFrançois Tigeot 				int idx,
22032c9916cdSFrançois Tigeot 				int h_idx, int v_idx)
22042c9916cdSFrançois Tigeot {
22052c9916cdSFrançois Tigeot 	struct drm_fb_helper_connector *fb_helper_conn;
22062c9916cdSFrançois Tigeot 	int i;
22072c9916cdSFrançois Tigeot 	int hoffset = 0, voffset = 0;
22082c9916cdSFrançois Tigeot 
22094be47400SFrançois Tigeot 	drm_fb_helper_for_each_connector(fb_helper, i) {
22102c9916cdSFrançois Tigeot 		fb_helper_conn = fb_helper->connector_info[i];
22112c9916cdSFrançois Tigeot 		if (!fb_helper_conn->connector->has_tile)
22122c9916cdSFrançois Tigeot 			continue;
22132c9916cdSFrançois Tigeot 
22142c9916cdSFrançois Tigeot 		if (!modes[i] && (h_idx || v_idx)) {
22152c9916cdSFrançois Tigeot 			DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
22162c9916cdSFrançois Tigeot 				      fb_helper_conn->connector->base.id);
22172c9916cdSFrançois Tigeot 			continue;
22182c9916cdSFrançois Tigeot 		}
22192c9916cdSFrançois Tigeot 		if (fb_helper_conn->connector->tile_h_loc < h_idx)
22202c9916cdSFrançois Tigeot 			hoffset += modes[i]->hdisplay;
22212c9916cdSFrançois Tigeot 
22222c9916cdSFrançois Tigeot 		if (fb_helper_conn->connector->tile_v_loc < v_idx)
22232c9916cdSFrançois Tigeot 			voffset += modes[i]->vdisplay;
22242c9916cdSFrançois Tigeot 	}
22252c9916cdSFrançois Tigeot 	offsets[idx].x = hoffset;
22262c9916cdSFrançois Tigeot 	offsets[idx].y = voffset;
22272c9916cdSFrançois Tigeot 	DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
22282c9916cdSFrançois Tigeot 	return 0;
22292c9916cdSFrançois Tigeot }
22302c9916cdSFrançois Tigeot 
drm_target_preferred(struct drm_fb_helper * fb_helper,struct drm_display_mode ** modes,struct drm_fb_offset * offsets,bool * enabled,int width,int height)22315718399fSFrançois Tigeot static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
22325718399fSFrançois Tigeot 				 struct drm_display_mode **modes,
22332c9916cdSFrançois Tigeot 				 struct drm_fb_offset *offsets,
22345718399fSFrançois Tigeot 				 bool *enabled, int width, int height)
22355718399fSFrançois Tigeot {
22365718399fSFrançois Tigeot 	struct drm_fb_helper_connector *fb_helper_conn;
22374be47400SFrançois Tigeot 	const u64 mask = BIT_ULL(fb_helper->connector_count) - 1;
22384be47400SFrançois Tigeot 	u64 conn_configured = 0;
22392c9916cdSFrançois Tigeot 	int tile_pass = 0;
22404be47400SFrançois Tigeot 	int i;
22414be47400SFrançois Tigeot 
22422c9916cdSFrançois Tigeot retry:
22434be47400SFrançois Tigeot 	drm_fb_helper_for_each_connector(fb_helper, i) {
22445718399fSFrançois Tigeot 		fb_helper_conn = fb_helper->connector_info[i];
22455718399fSFrançois Tigeot 
22464be47400SFrançois Tigeot 		if (conn_configured & BIT_ULL(i))
22475718399fSFrançois Tigeot 			continue;
22485718399fSFrançois Tigeot 
22492c9916cdSFrançois Tigeot 		if (enabled[i] == false) {
22504be47400SFrançois Tigeot 			conn_configured |= BIT_ULL(i);
22512c9916cdSFrançois Tigeot 			continue;
22522c9916cdSFrançois Tigeot 		}
22532c9916cdSFrançois Tigeot 
22542c9916cdSFrançois Tigeot 		/* first pass over all the untiled connectors */
22552c9916cdSFrançois Tigeot 		if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
22562c9916cdSFrançois Tigeot 			continue;
22572c9916cdSFrançois Tigeot 
22582c9916cdSFrançois Tigeot 		if (tile_pass == 1) {
22592c9916cdSFrançois Tigeot 			if (fb_helper_conn->connector->tile_h_loc != 0 ||
22602c9916cdSFrançois Tigeot 			    fb_helper_conn->connector->tile_v_loc != 0)
22612c9916cdSFrançois Tigeot 				continue;
22622c9916cdSFrançois Tigeot 
22632c9916cdSFrançois Tigeot 		} else {
22642c9916cdSFrançois Tigeot 			if (fb_helper_conn->connector->tile_h_loc != tile_pass - 1 &&
22652c9916cdSFrançois Tigeot 			    fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
22662c9916cdSFrançois Tigeot 			/* if this tile_pass doesn't cover any of the tiles - keep going */
22672c9916cdSFrançois Tigeot 				continue;
22682c9916cdSFrançois Tigeot 
2269a85cb24fSFrançois Tigeot 			/*
2270a85cb24fSFrançois Tigeot 			 * find the tile offsets for this pass - need to find
2271a85cb24fSFrançois Tigeot 			 * all tiles left and above
2272a85cb24fSFrançois Tigeot 			 */
22732c9916cdSFrançois Tigeot 			drm_get_tile_offsets(fb_helper, modes, offsets,
22742c9916cdSFrançois Tigeot 					     i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
22752c9916cdSFrançois Tigeot 		}
22765718399fSFrançois Tigeot 		DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
22775718399fSFrançois Tigeot 			      fb_helper_conn->connector->base.id);
22785718399fSFrançois Tigeot 
22795718399fSFrançois Tigeot 		/* got for command line mode first */
2280a85cb24fSFrançois Tigeot 		modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
22815718399fSFrançois Tigeot 		if (!modes[i]) {
22822c9916cdSFrançois Tigeot 			DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
22832c9916cdSFrançois Tigeot 				      fb_helper_conn->connector->base.id, fb_helper_conn->connector->tile_group ? fb_helper_conn->connector->tile_group->id : 0);
22845718399fSFrançois Tigeot 			modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
22855718399fSFrançois Tigeot 		}
22865718399fSFrançois Tigeot 		/* No preferred modes, pick one off the list */
22875718399fSFrançois Tigeot 		if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
22885718399fSFrançois Tigeot 			list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
22895718399fSFrançois Tigeot 				break;
22905718399fSFrançois Tigeot 		}
22915718399fSFrançois Tigeot 		DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
22925718399fSFrançois Tigeot 			  "none");
22934be47400SFrançois Tigeot 		conn_configured |= BIT_ULL(i);
22942c9916cdSFrançois Tigeot 	}
22952c9916cdSFrançois Tigeot 
22962c9916cdSFrançois Tigeot 	if ((conn_configured & mask) != mask) {
22972c9916cdSFrançois Tigeot 		tile_pass++;
22982c9916cdSFrançois Tigeot 		goto retry;
22995718399fSFrançois Tigeot 	}
23005718399fSFrançois Tigeot 	return true;
23015718399fSFrançois Tigeot }
23025718399fSFrançois Tigeot 
drm_pick_crtcs(struct drm_fb_helper * fb_helper,struct drm_fb_helper_crtc ** best_crtcs,struct drm_display_mode ** modes,int n,int width,int height)23035718399fSFrançois Tigeot static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
23045718399fSFrançois Tigeot 			  struct drm_fb_helper_crtc **best_crtcs,
23055718399fSFrançois Tigeot 			  struct drm_display_mode **modes,
23065718399fSFrançois Tigeot 			  int n, int width, int height)
23075718399fSFrançois Tigeot {
23085718399fSFrançois Tigeot 	int c, o;
23095718399fSFrançois Tigeot 	struct drm_connector *connector;
2310477eb7f9SFrançois Tigeot 	const struct drm_connector_helper_funcs *connector_funcs;
23115718399fSFrançois Tigeot 	struct drm_encoder *encoder;
23125718399fSFrançois Tigeot 	int my_score, best_score, score;
23135718399fSFrançois Tigeot 	struct drm_fb_helper_crtc **crtcs, *crtc;
23145718399fSFrançois Tigeot 	struct drm_fb_helper_connector *fb_helper_conn;
23155718399fSFrançois Tigeot 
23165718399fSFrançois Tigeot 	if (n == fb_helper->connector_count)
23175718399fSFrançois Tigeot 		return 0;
23185718399fSFrançois Tigeot 
23195718399fSFrançois Tigeot 	fb_helper_conn = fb_helper->connector_info[n];
23205718399fSFrançois Tigeot 	connector = fb_helper_conn->connector;
23215718399fSFrançois Tigeot 
23225718399fSFrançois Tigeot 	best_crtcs[n] = NULL;
23235718399fSFrançois Tigeot 	best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
23245718399fSFrançois Tigeot 	if (modes[n] == NULL)
23255718399fSFrançois Tigeot 		return best_score;
23265718399fSFrançois Tigeot 
23273f2dd94aSFrançois Tigeot 	crtcs = kcalloc(fb_helper->connector_count,
23289edbd4a0SFrançois Tigeot 			sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
23299edbd4a0SFrançois Tigeot 	if (!crtcs)
23309edbd4a0SFrançois Tigeot 		return best_score;
23315718399fSFrançois Tigeot 
23325718399fSFrançois Tigeot 	my_score = 1;
23335718399fSFrançois Tigeot 	if (connector->status == connector_status_connected)
23345718399fSFrançois Tigeot 		my_score++;
23355718399fSFrançois Tigeot 	if (drm_has_cmdline_mode(fb_helper_conn))
23365718399fSFrançois Tigeot 		my_score++;
23375718399fSFrançois Tigeot 	if (drm_has_preferred_mode(fb_helper_conn, width, height))
23385718399fSFrançois Tigeot 		my_score++;
23395718399fSFrançois Tigeot 
23405718399fSFrançois Tigeot 	connector_funcs = connector->helper_private;
23411dedbd3bSFrançois Tigeot 
23421dedbd3bSFrançois Tigeot 	/*
23431dedbd3bSFrançois Tigeot 	 * If the DRM device implements atomic hooks and ->best_encoder() is
23441dedbd3bSFrançois Tigeot 	 * NULL we fallback to the default drm_atomic_helper_best_encoder()
23451dedbd3bSFrançois Tigeot 	 * helper.
23461dedbd3bSFrançois Tigeot 	 */
2347a85cb24fSFrançois Tigeot 	if (drm_drv_uses_atomic_modeset(fb_helper->dev) &&
23481dedbd3bSFrançois Tigeot 	    !connector_funcs->best_encoder)
23491dedbd3bSFrançois Tigeot 		encoder = drm_atomic_helper_best_encoder(connector);
23501dedbd3bSFrançois Tigeot 	else
23515718399fSFrançois Tigeot 		encoder = connector_funcs->best_encoder(connector);
23521dedbd3bSFrançois Tigeot 
23535718399fSFrançois Tigeot 	if (!encoder)
23545718399fSFrançois Tigeot 		goto out;
23555718399fSFrançois Tigeot 
2356a85cb24fSFrançois Tigeot 	/*
2357a85cb24fSFrançois Tigeot 	 * select a crtc for this connector and then attempt to configure
2358a85cb24fSFrançois Tigeot 	 * remaining connectors
2359a85cb24fSFrançois Tigeot 	 */
23605718399fSFrançois Tigeot 	for (c = 0; c < fb_helper->crtc_count; c++) {
23615718399fSFrançois Tigeot 		crtc = &fb_helper->crtc_info[c];
23625718399fSFrançois Tigeot 
23639edbd4a0SFrançois Tigeot 		if ((encoder->possible_crtcs & (1 << c)) == 0)
23645718399fSFrançois Tigeot 			continue;
23655718399fSFrançois Tigeot 
23665718399fSFrançois Tigeot 		for (o = 0; o < n; o++)
23675718399fSFrançois Tigeot 			if (best_crtcs[o] == crtc)
23685718399fSFrançois Tigeot 				break;
23695718399fSFrançois Tigeot 
23705718399fSFrançois Tigeot 		if (o < n) {
23715718399fSFrançois Tigeot 			/* ignore cloning unless only a single crtc */
23725718399fSFrançois Tigeot 			if (fb_helper->crtc_count > 1)
23735718399fSFrançois Tigeot 				continue;
23745718399fSFrançois Tigeot 
23755718399fSFrançois Tigeot 			if (!drm_mode_equal(modes[o], modes[n]))
23765718399fSFrançois Tigeot 				continue;
23775718399fSFrançois Tigeot 		}
23785718399fSFrançois Tigeot 
23795718399fSFrançois Tigeot 		crtcs[n] = crtc;
23805718399fSFrançois Tigeot 		memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
23815718399fSFrançois Tigeot 		score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
23825718399fSFrançois Tigeot 						  width, height);
23835718399fSFrançois Tigeot 		if (score > best_score) {
23845718399fSFrançois Tigeot 			best_score = score;
23855718399fSFrançois Tigeot 			memcpy(best_crtcs, crtcs,
23868621f407SFrançois Tigeot 			       fb_helper->connector_count *
23875718399fSFrançois Tigeot 			       sizeof(struct drm_fb_helper_crtc *));
23885718399fSFrançois Tigeot 		}
23895718399fSFrançois Tigeot 	}
23905718399fSFrançois Tigeot out:
23919edbd4a0SFrançois Tigeot 	kfree(crtcs);
23925718399fSFrançois Tigeot 	return best_score;
23935718399fSFrançois Tigeot }
23945718399fSFrançois Tigeot 
drm_setup_crtcs(struct drm_fb_helper * fb_helper,u32 width,u32 height)23954be47400SFrançois Tigeot static void drm_setup_crtcs(struct drm_fb_helper *fb_helper,
23964be47400SFrançois Tigeot 			    u32 width, u32 height)
23975718399fSFrançois Tigeot {
23985718399fSFrançois Tigeot 	struct drm_device *dev = fb_helper->dev;
23995718399fSFrançois Tigeot 	struct drm_fb_helper_crtc **crtcs;
24005718399fSFrançois Tigeot 	struct drm_display_mode **modes;
24012c9916cdSFrançois Tigeot 	struct drm_fb_offset *offsets;
24025718399fSFrançois Tigeot 	bool *enabled;
24039edbd4a0SFrançois Tigeot 	int i;
24045718399fSFrançois Tigeot 
24055718399fSFrançois Tigeot 	DRM_DEBUG_KMS("\n");
24064be47400SFrançois Tigeot 	/* prevent concurrent modification of connector_count by hotplug */
24073f2dd94aSFrançois Tigeot 	lockdep_assert_held(&fb_helper->lock);
24085718399fSFrançois Tigeot 
2409c0e85e96SFrançois Tigeot 	crtcs = kcalloc(fb_helper->connector_count,
241022ee5efbSFrançois Tigeot 			sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
2411c0e85e96SFrançois Tigeot 	modes = kcalloc(fb_helper->connector_count,
241222ee5efbSFrançois Tigeot 			sizeof(struct drm_display_mode *), GFP_KERNEL);
2413c0e85e96SFrançois Tigeot 	offsets = kcalloc(fb_helper->connector_count,
24142c9916cdSFrançois Tigeot 			  sizeof(struct drm_fb_offset), GFP_KERNEL);
2415c0e85e96SFrançois Tigeot 	enabled = kcalloc(fb_helper->connector_count,
241622ee5efbSFrançois Tigeot 			  sizeof(bool), GFP_KERNEL);
24172c9916cdSFrançois Tigeot 	if (!crtcs || !modes || !enabled || !offsets) {
24189edbd4a0SFrançois Tigeot 		DRM_ERROR("Memory allocation failed\n");
24199edbd4a0SFrançois Tigeot 		goto out;
24209edbd4a0SFrançois Tigeot 	}
24219edbd4a0SFrançois Tigeot 
24223f2dd94aSFrançois Tigeot 	mutex_lock(&fb_helper->dev->mode_config.mutex);
24233f2dd94aSFrançois Tigeot 	if (drm_fb_helper_probe_connector_modes(fb_helper, width, height) == 0)
24243f2dd94aSFrançois Tigeot 		DRM_DEBUG_KMS("No connectors reported connected with modes\n");
24255718399fSFrançois Tigeot 	drm_enable_connectors(fb_helper, enabled);
24265718399fSFrançois Tigeot 
24279edbd4a0SFrançois Tigeot 	if (!(fb_helper->funcs->initial_config &&
24289edbd4a0SFrançois Tigeot 	      fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
24292c9916cdSFrançois Tigeot 					       offsets,
24309edbd4a0SFrançois Tigeot 					       enabled, width, height))) {
2431c0e85e96SFrançois Tigeot 		memset(modes, 0, fb_helper->connector_count*sizeof(modes[0]));
2432c0e85e96SFrançois Tigeot 		memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0]));
2433c0e85e96SFrançois Tigeot 		memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0]));
24345718399fSFrançois Tigeot 
24352c9916cdSFrançois Tigeot 		if (!drm_target_cloned(fb_helper, modes, offsets,
24362c9916cdSFrançois Tigeot 				       enabled, width, height) &&
24372c9916cdSFrançois Tigeot 		    !drm_target_preferred(fb_helper, modes, offsets,
24382c9916cdSFrançois Tigeot 					  enabled, width, height))
24399edbd4a0SFrançois Tigeot 			DRM_ERROR("Unable to find initial modes\n");
24409edbd4a0SFrançois Tigeot 
24419edbd4a0SFrançois Tigeot 		DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
24429edbd4a0SFrançois Tigeot 			      width, height);
24435718399fSFrançois Tigeot 
24445718399fSFrançois Tigeot 		drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
24459edbd4a0SFrançois Tigeot 	}
24463f2dd94aSFrançois Tigeot 	mutex_unlock(&fb_helper->dev->mode_config.mutex);
24475718399fSFrançois Tigeot 
24485718399fSFrançois Tigeot 	/* need to set the modesets up here for use later */
24495718399fSFrançois Tigeot 	/* fill out the connector<->crtc mappings into the modesets */
24504be47400SFrançois Tigeot 	for (i = 0; i < fb_helper->crtc_count; i++)
24514be47400SFrançois Tigeot 		drm_fb_helper_modeset_release(fb_helper,
24524be47400SFrançois Tigeot 					      &fb_helper->crtc_info[i].mode_set);
24535718399fSFrançois Tigeot 
24544be47400SFrançois Tigeot 	drm_fb_helper_for_each_connector(fb_helper, i) {
24555718399fSFrançois Tigeot 		struct drm_display_mode *mode = modes[i];
24565718399fSFrançois Tigeot 		struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
24572c9916cdSFrançois Tigeot 		struct drm_fb_offset *offset = &offsets[i];
24585718399fSFrançois Tigeot 
24595718399fSFrançois Tigeot 		if (mode && fb_crtc) {
24603f2dd94aSFrançois Tigeot 			struct drm_mode_set *modeset = &fb_crtc->mode_set;
24614be47400SFrançois Tigeot 			struct drm_connector *connector =
24624be47400SFrançois Tigeot 				fb_helper->connector_info[i]->connector;
24634be47400SFrançois Tigeot 
24642c9916cdSFrançois Tigeot 			DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
24652c9916cdSFrançois Tigeot 				      mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
24664be47400SFrançois Tigeot 
24675718399fSFrançois Tigeot 			fb_crtc->desired_mode = mode;
24682c9916cdSFrançois Tigeot 			fb_crtc->x = offset->x;
24692c9916cdSFrançois Tigeot 			fb_crtc->y = offset->y;
24705718399fSFrançois Tigeot 			modeset->mode = drm_mode_duplicate(dev,
24715718399fSFrançois Tigeot 							   fb_crtc->desired_mode);
2472a85cb24fSFrançois Tigeot 			drm_connector_get(connector);
24734be47400SFrançois Tigeot 			modeset->connectors[modeset->num_connectors++] = connector;
24742c9916cdSFrançois Tigeot 			modeset->x = offset->x;
24752c9916cdSFrançois Tigeot 			modeset->y = offset->y;
24765718399fSFrançois Tigeot 		}
24775718399fSFrançois Tigeot 	}
24789edbd4a0SFrançois Tigeot out:
24799edbd4a0SFrançois Tigeot 	kfree(crtcs);
24809edbd4a0SFrançois Tigeot 	kfree(modes);
24812c9916cdSFrançois Tigeot 	kfree(offsets);
24829edbd4a0SFrançois Tigeot 	kfree(enabled);
24835718399fSFrançois Tigeot }
24845718399fSFrançois Tigeot 
24853f2dd94aSFrançois Tigeot /*
24863f2dd94aSFrançois Tigeot  * This is a continuation of drm_setup_crtcs() that sets up anything related
24873f2dd94aSFrançois Tigeot  * to the framebuffer. During initialization, drm_setup_crtcs() is called before
24883f2dd94aSFrançois Tigeot  * the framebuffer has been allocated (fb_helper->fb and fb_helper->fbdev).
24893f2dd94aSFrançois Tigeot  * So, any setup that touches those fields needs to be done here instead of in
24903f2dd94aSFrançois Tigeot  * drm_setup_crtcs().
24913f2dd94aSFrançois Tigeot  */
drm_setup_crtcs_fb(struct drm_fb_helper * fb_helper)24923f2dd94aSFrançois Tigeot static void drm_setup_crtcs_fb(struct drm_fb_helper *fb_helper)
24933f2dd94aSFrançois Tigeot {
24943f2dd94aSFrançois Tigeot #if 0
24953f2dd94aSFrançois Tigeot 	struct fb_info *info = fb_helper->fbdev;
24963f2dd94aSFrançois Tigeot #endif
24973f2dd94aSFrançois Tigeot 	int i;
24983f2dd94aSFrançois Tigeot 
24993f2dd94aSFrançois Tigeot 	for (i = 0; i < fb_helper->crtc_count; i++)
25003f2dd94aSFrançois Tigeot 		if (fb_helper->crtc_info[i].mode_set.num_connectors)
25013f2dd94aSFrançois Tigeot 			fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
25023f2dd94aSFrançois Tigeot 
25033f2dd94aSFrançois Tigeot 	mutex_lock(&fb_helper->dev->mode_config.mutex);
25043f2dd94aSFrançois Tigeot 	drm_fb_helper_for_each_connector(fb_helper, i) {
25053f2dd94aSFrançois Tigeot 		struct drm_connector *connector =
25063f2dd94aSFrançois Tigeot 					fb_helper->connector_info[i]->connector;
25073f2dd94aSFrançois Tigeot 
25083f2dd94aSFrançois Tigeot 		/* use first connected connector for the physical dimensions */
25093f2dd94aSFrançois Tigeot 		if (connector->status == connector_status_connected) {
25103f2dd94aSFrançois Tigeot #if 0
25113f2dd94aSFrançois Tigeot 			info->var.width = connector->display_info.width_mm;
25123f2dd94aSFrançois Tigeot 			info->var.height = connector->display_info.height_mm;
25133f2dd94aSFrançois Tigeot #endif
25143f2dd94aSFrançois Tigeot 			break;
25153f2dd94aSFrançois Tigeot 		}
25163f2dd94aSFrançois Tigeot 	}
25173f2dd94aSFrançois Tigeot 	mutex_unlock(&fb_helper->dev->mode_config.mutex);
25183f2dd94aSFrançois Tigeot }
25193f2dd94aSFrançois Tigeot 
25203f2dd94aSFrançois Tigeot /* Note: Drops fb_helper->lock before returning. */
25213f2dd94aSFrançois Tigeot static int
__drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper * fb_helper,int bpp_sel)25223f2dd94aSFrançois Tigeot __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper,
25233f2dd94aSFrançois Tigeot 					  int bpp_sel)
25243f2dd94aSFrançois Tigeot {
25253f2dd94aSFrançois Tigeot 	struct drm_device *dev = fb_helper->dev;
25263f2dd94aSFrançois Tigeot 	struct fb_info *info;
25273f2dd94aSFrançois Tigeot 	unsigned int width, height;
25283f2dd94aSFrançois Tigeot 	int ret;
25293f2dd94aSFrançois Tigeot 
25303f2dd94aSFrançois Tigeot 	width = dev->mode_config.max_width;
25313f2dd94aSFrançois Tigeot 	height = dev->mode_config.max_height;
25323f2dd94aSFrançois Tigeot 
25333f2dd94aSFrançois Tigeot 	drm_setup_crtcs(fb_helper, width, height);
25343f2dd94aSFrançois Tigeot 	ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
25353f2dd94aSFrançois Tigeot 	if (ret < 0) {
25363f2dd94aSFrançois Tigeot 		if (ret == -EAGAIN) {
25373f2dd94aSFrançois Tigeot 			fb_helper->preferred_bpp = bpp_sel;
25383f2dd94aSFrançois Tigeot 			fb_helper->deferred_setup = true;
25393f2dd94aSFrançois Tigeot 			ret = 0;
25403f2dd94aSFrançois Tigeot 		}
25413f2dd94aSFrançois Tigeot 		mutex_unlock(&fb_helper->lock);
25423f2dd94aSFrançois Tigeot 
25433f2dd94aSFrançois Tigeot 		return ret;
25443f2dd94aSFrançois Tigeot 	}
25453f2dd94aSFrançois Tigeot 	drm_setup_crtcs_fb(fb_helper);
25463f2dd94aSFrançois Tigeot 
25473f2dd94aSFrançois Tigeot 	fb_helper->deferred_setup = false;
25483f2dd94aSFrançois Tigeot 
25493f2dd94aSFrançois Tigeot 	info = fb_helper->fbdev;
25503f2dd94aSFrançois Tigeot #if 0
25513f2dd94aSFrançois Tigeot 	info->var.pixclock = 0;
25523f2dd94aSFrançois Tigeot #endif
25533f2dd94aSFrançois Tigeot 
25543f2dd94aSFrançois Tigeot 	/* Need to drop locks to avoid recursive deadlock in
25553f2dd94aSFrançois Tigeot 	 * register_framebuffer. This is ok because the only thing left to do is
25563f2dd94aSFrançois Tigeot 	 * register the fbdev emulation instance in kernel_fb_helper_list. */
25573f2dd94aSFrançois Tigeot 	mutex_unlock(&fb_helper->lock);
25583f2dd94aSFrançois Tigeot 
25593f2dd94aSFrançois Tigeot 	ret = register_framebuffer(info);
25603f2dd94aSFrançois Tigeot 	if (ret < 0)
25613f2dd94aSFrançois Tigeot 		return ret;
25623f2dd94aSFrançois Tigeot #if 0
25633f2dd94aSFrançois Tigeot 	dev_info(dev->dev, "fb%d: %s frame buffer device\n",
25643f2dd94aSFrançois Tigeot 		 info->node, info->fix.id);
25653f2dd94aSFrançois Tigeot #endif
25663f2dd94aSFrançois Tigeot 
25673f2dd94aSFrançois Tigeot 	mutex_lock(&kernel_fb_helper_lock);
25683f2dd94aSFrançois Tigeot #if 0
25693f2dd94aSFrançois Tigeot 	if (list_empty(&kernel_fb_helper_list))
25703f2dd94aSFrançois Tigeot 		register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
25713f2dd94aSFrançois Tigeot #endif
25723f2dd94aSFrançois Tigeot 
25733f2dd94aSFrançois Tigeot 	list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
25743f2dd94aSFrançois Tigeot 	mutex_unlock(&kernel_fb_helper_lock);
25753f2dd94aSFrançois Tigeot 
25763f2dd94aSFrançois Tigeot 	return 0;
25773f2dd94aSFrançois Tigeot }
25783f2dd94aSFrançois Tigeot 
25795718399fSFrançois Tigeot /**
25809edbd4a0SFrançois Tigeot  * drm_fb_helper_initial_config - setup a sane initial connector configuration
25819edbd4a0SFrançois Tigeot  * @fb_helper: fb_helper device struct
25829edbd4a0SFrançois Tigeot  * @bpp_sel: bpp value to use for the framebuffer configuration
25835718399fSFrançois Tigeot  *
25849edbd4a0SFrançois Tigeot  * Scans the CRTCs and connectors and tries to put together an initial setup.
25855718399fSFrançois Tigeot  * At the moment, this is a cloned configuration across all heads with
25865718399fSFrançois Tigeot  * a new framebuffer object as the backing store.
25875718399fSFrançois Tigeot  *
25889edbd4a0SFrançois Tigeot  * Note that this also registers the fbdev and so allows userspace to call into
25899edbd4a0SFrançois Tigeot  * the driver through the fbdev interfaces.
25909edbd4a0SFrançois Tigeot  *
2591a85cb24fSFrançois Tigeot  * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
2592a85cb24fSFrançois Tigeot  * to let the driver allocate and initialize the fbdev info structure and the
2593a85cb24fSFrançois Tigeot  * drm framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
25949edbd4a0SFrançois Tigeot  * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
25959edbd4a0SFrançois Tigeot  * values for the fbdev info structure.
25969edbd4a0SFrançois Tigeot  *
2597c0e85e96SFrançois Tigeot  * HANG DEBUGGING:
2598c0e85e96SFrançois Tigeot  *
2599c0e85e96SFrançois Tigeot  * When you have fbcon support built-in or already loaded, this function will do
2600c0e85e96SFrançois Tigeot  * a full modeset to setup the fbdev console. Due to locking misdesign in the
2601c0e85e96SFrançois Tigeot  * VT/fbdev subsystem that entire modeset sequence has to be done while holding
2602c0e85e96SFrançois Tigeot  * console_lock. Until console_unlock is called no dmesg lines will be sent out
2603c0e85e96SFrançois Tigeot  * to consoles, not even serial console. This means when your driver crashes,
2604c0e85e96SFrançois Tigeot  * you will see absolutely nothing else but a system stuck in this function,
2605c0e85e96SFrançois Tigeot  * with no further output. Any kind of printk() you place within your own driver
2606c0e85e96SFrançois Tigeot  * or in the drm core modeset code will also never show up.
2607c0e85e96SFrançois Tigeot  *
2608c0e85e96SFrançois Tigeot  * Standard debug practice is to run the fbcon setup without taking the
2609c0e85e96SFrançois Tigeot  * console_lock as a hack, to be able to see backtraces and crashes on the
2610c0e85e96SFrançois Tigeot  * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
2611c0e85e96SFrançois Tigeot  * cmdline option.
2612c0e85e96SFrançois Tigeot  *
2613c0e85e96SFrançois Tigeot  * The other option is to just disable fbdev emulation since very likely the
26148621f407SFrançois Tigeot  * first modeset from userspace will crash in the same way, and is even easier
26158621f407SFrançois Tigeot  * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
2616c0e85e96SFrançois Tigeot  * kernel cmdline option.
2617c0e85e96SFrançois Tigeot  *
26185718399fSFrançois Tigeot  * RETURNS:
26195718399fSFrançois Tigeot  * Zero if everything went ok, nonzero otherwise.
26205718399fSFrançois Tigeot  */
drm_fb_helper_initial_config(struct drm_fb_helper * fb_helper,int bpp_sel)26212c9916cdSFrançois Tigeot int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
26225718399fSFrançois Tigeot {
26234be47400SFrançois Tigeot 	int ret;
26245718399fSFrançois Tigeot 
2625352ff8bdSFrançois Tigeot 	if (!drm_fbdev_emulation)
2626352ff8bdSFrançois Tigeot 		return 0;
2627352ff8bdSFrançois Tigeot 
26283f2dd94aSFrançois Tigeot 	mutex_lock(&fb_helper->lock);
262978973132SSergey Zigachev 	ret = __drm_fb_helper_initial_config_and_unlock(fb_helper, bpp_sel);
263078973132SSergey Zigachev 
26314be47400SFrançois Tigeot 	return ret;
26325718399fSFrançois Tigeot }
26339edbd4a0SFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_initial_config);
26345718399fSFrançois Tigeot 
26359edbd4a0SFrançois Tigeot /**
26369edbd4a0SFrançois Tigeot  * drm_fb_helper_hotplug_event - respond to a hotplug notification by
26379edbd4a0SFrançois Tigeot  *                               probing all the outputs attached to the fb
26389edbd4a0SFrançois Tigeot  * @fb_helper: the drm_fb_helper
26399edbd4a0SFrançois Tigeot  *
26409edbd4a0SFrançois Tigeot  * Scan the connectors attached to the fb_helper and try to put together a
26411dedbd3bSFrançois Tigeot  * setup after notification of a change in output configuration.
26429edbd4a0SFrançois Tigeot  *
26439edbd4a0SFrançois Tigeot  * Called at runtime, takes the mode config locks to be able to check/change the
26449edbd4a0SFrançois Tigeot  * modeset configuration. Must be run from process context (which usually means
26459edbd4a0SFrançois Tigeot  * either the output polling work or a work item launched from the driver's
26469edbd4a0SFrançois Tigeot  * hotplug interrupt).
26479edbd4a0SFrançois Tigeot  *
264824edb884SFrançois Tigeot  * Note that drivers may call this even before calling
26498621f407SFrançois Tigeot  * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
265024edb884SFrançois Tigeot  * for a race-free fbcon setup and will make sure that the fbdev emulation will
265124edb884SFrançois Tigeot  * not miss any hotplug events.
26529edbd4a0SFrançois Tigeot  *
26539edbd4a0SFrançois Tigeot  * RETURNS:
26549edbd4a0SFrançois Tigeot  * 0 on success and a non-zero error code otherwise.
26559edbd4a0SFrançois Tigeot  */
drm_fb_helper_hotplug_event(struct drm_fb_helper * fb_helper)26565718399fSFrançois Tigeot int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
26575718399fSFrançois Tigeot {
26583f2dd94aSFrançois Tigeot 	int err = 0;
26595718399fSFrançois Tigeot 
2660352ff8bdSFrançois Tigeot 	if (!drm_fbdev_emulation)
2661352ff8bdSFrançois Tigeot 		return 0;
2662352ff8bdSFrançois Tigeot 
26633f2dd94aSFrançois Tigeot 	mutex_lock(&fb_helper->lock);
26643f2dd94aSFrançois Tigeot 	if (fb_helper->deferred_setup) {
26653f2dd94aSFrançois Tigeot 		err = __drm_fb_helper_initial_config_and_unlock(fb_helper,
26663f2dd94aSFrançois Tigeot 				fb_helper->preferred_bpp);
26673f2dd94aSFrançois Tigeot 		return err;
26683f2dd94aSFrançois Tigeot 	}
26693f2dd94aSFrançois Tigeot 
267024edb884SFrançois Tigeot 	if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
26715718399fSFrançois Tigeot 		fb_helper->delayed_hotplug = true;
26723f2dd94aSFrançois Tigeot 		mutex_unlock(&fb_helper->lock);
26733f2dd94aSFrançois Tigeot 		return err;
26745718399fSFrançois Tigeot 	}
26753f2dd94aSFrançois Tigeot 
26765718399fSFrançois Tigeot 	DRM_DEBUG_KMS("\n");
26775718399fSFrançois Tigeot 
26784be47400SFrançois Tigeot 	drm_setup_crtcs(fb_helper, fb_helper->fb->width, fb_helper->fb->height);
26793f2dd94aSFrançois Tigeot 	drm_setup_crtcs_fb(fb_helper);
26803f2dd94aSFrançois Tigeot 	mutex_unlock(&fb_helper->lock);
26819edbd4a0SFrançois Tigeot 
26829edbd4a0SFrançois Tigeot 	drm_fb_helper_set_par(fb_helper->fbdev);
26835718399fSFrançois Tigeot 
26849edbd4a0SFrançois Tigeot 	return 0;
26859edbd4a0SFrançois Tigeot }
26869edbd4a0SFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
26879edbd4a0SFrançois Tigeot 
26889edbd4a0SFrançois Tigeot /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
26899edbd4a0SFrançois Tigeot  * but the module doesn't depend on any fb console symbols.  At least
26909edbd4a0SFrançois Tigeot  * attempt to load fbcon to avoid leaving the system without a usable console.
26919edbd4a0SFrançois Tigeot  */
drm_fb_helper_modinit(void)2692c0e85e96SFrançois Tigeot int __init drm_fb_helper_modinit(void)
26939edbd4a0SFrançois Tigeot {
2694c0e85e96SFrançois Tigeot #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
2695a85cb24fSFrançois Tigeot 	const char name[] = "fbcon";
26969edbd4a0SFrançois Tigeot 	struct module *fbcon;
26979edbd4a0SFrançois Tigeot 
26989edbd4a0SFrançois Tigeot 	mutex_lock(&module_mutex);
26999edbd4a0SFrançois Tigeot 	fbcon = find_module(name);
27009edbd4a0SFrançois Tigeot 	mutex_unlock(&module_mutex);
27019edbd4a0SFrançois Tigeot 
27029edbd4a0SFrançois Tigeot 	if (!fbcon)
27039edbd4a0SFrançois Tigeot 		request_module_nowait(name);
2704c0e85e96SFrançois Tigeot #endif
27059edbd4a0SFrançois Tigeot 	return 0;
27065718399fSFrançois Tigeot }
2707c0e85e96SFrançois Tigeot EXPORT_SYMBOL(drm_fb_helper_modinit);
2708*9eb96077SSergey Zigachev 
2709*9eb96077SSergey Zigachev /**
2710*9eb96077SSergey Zigachev  * drm_fb_helper_output_poll_changed - DRM mode config \.output_poll_changed
2711*9eb96077SSergey Zigachev  *                                     helper for fbdev emulation
2712*9eb96077SSergey Zigachev  * @dev: DRM device
2713*9eb96077SSergey Zigachev  *
2714*9eb96077SSergey Zigachev  * This function can be used as the
2715*9eb96077SSergey Zigachev  * &drm_mode_config_funcs.output_poll_changed callback for drivers that only
2716*9eb96077SSergey Zigachev  * need to call drm_fb_helper_hotplug_event().
2717*9eb96077SSergey Zigachev  */
drm_fb_helper_output_poll_changed(struct drm_device * dev)2718*9eb96077SSergey Zigachev void drm_fb_helper_output_poll_changed(struct drm_device *dev)
2719*9eb96077SSergey Zigachev {
2720*9eb96077SSergey Zigachev }
2721*9eb96077SSergey Zigachev EXPORT_SYMBOL(drm_fb_helper_output_poll_changed);
2722